File: viewsudo.c

package info (click to toggle)
umview 0.8.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 5,472 kB
  • sloc: ansic: 67,309; sh: 11,160; ruby: 914; makefile: 424; python: 141
file content (155 lines) | stat: -rw-r--r-- 3,280 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*   This is part of um-ViewOS
 *   The user-mode implementation of OSVIEW -- A Process with a View
 *
 *   viewsudo user command
 *   
 *   Copyright 2010 Renzo Davoli University of Bologna - Italy
 *   
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2 or 
 *   (at your option) any later version, as published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *   $Id: um_add_service.c 775 2009-09-01 21:15:23Z rd235 $
 *
 */   
#include <config.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <getopt.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <libgen.h>
#include <um_lib.h>

static struct option long_options[] = {
	{"help", 1, 0, 'h'},
	{0, 0, 0, 0}
};

char *command;
char *user;
char *group;
int uid;
int gid;
int ngroups;
gid_t *groups;

void usage(char *argv0)
{
	char *name=basename(argv0);
	fprintf(stderr,
			"Usage: %s [options] [LOGIN]\n"
			"\n"
			"Options:\n"
			"  -u  username|#uid\n"
			"  -g  groupname|#gid\n"
			"\n",
			name
			);
	exit(2);
}

int main(int argc, char *argv[])
{
	pid_t pid;
	int status;
	int c;
	struct passwd *pwd;

	/* outside viewos use sudo(1) */
	if (um_check_viewos()==0) 
		execvp("sudo",argv);

	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "u:g:h",
				long_options, &option_index);
		if (c == -1)
			break;
		switch (c) {
			case 'u':user=optarg;
							 break;
			case 'g':group=optarg;
							 break;
			case 'h': usage(argv[0]);
								break;
		}
	}
	if (argc==optind)
		usage(argv[0]);

	if (user) {
		if (*user == '#')
			uid=atoi(user+1);
		else {
			pwd=getpwnam(user);
			if (pwd == NULL) {
				fprintf(stderr,"Unknown id: %s\n",user);
				exit(1);
			}
			uid=pwd->pw_uid;
		}
	} else {
		if (group)
			uid=getuid();
	}
	if (pwd==NULL)
		pwd=getpwuid(uid);
	if (pwd) {
		gid=pwd->pw_gid;
		setenv("HOME",pwd->pw_dir,1);
	} 
	if (group) {
		if (*group == '#')
			gid=atoi(group+1);
		else {
			struct group *grp=getgrnam(group);
			if (grp == NULL) {
				fprintf(stderr,"Unknown group id: %s\n",group);
				exit(1);
			}
			gid=grp->gr_gid;
		}
	}

	if (pwd) {
		getgrouplist(pwd->pw_name,gid,NULL,&ngroups);
		groups=malloc(ngroups * sizeof (gid_t));
		if (groups == NULL)
			ngroups=0;
		else
			getgrouplist(pwd->pw_name,gid,groups,&ngroups);
	}

	switch (pid=fork()) {
		case -1: exit(1);
		case 0: 
						 if (setresuid(uid,uid,uid) < 0)
							 perror(argv[0]);
						 else {
							 setresgid(gid,gid,gid);
							 setgroups(ngroups,groups);
							 execvp(argv[optind],argv+optind);
							 perror(argv[optind]);
						 }
						 exit(1);
						 break;
		default:
						 waitpid(pid,&status,0);
						 return(WEXITSTATUS(status));
	}
	return 0;
}