2 * really.c - program for gaining privilege
4 * Copyright (C) 1992-3 Ian Jackson <iwj10@cus.cam.ac.uk>
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
11 * This is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this file; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/types.h>
32 void usagemessage(void) {
33 if (fputs("usage: really [<user-option>] [<group-option> ...] [--]"
34 " [<command> [<argument/option> ...]]\n"
36 " if no options given, set the uid to 0;\n"
37 " -u|--user <username> also sets their default group list\n"
38 " -i|--useronly <username> } set the uid\n"
39 " -I|--uidonly <uid> } but inherits the group list\n"
41 " -z|--groupsclear only groups specified are to be used\n"
42 " -g|--group <groupname> } add this to\n"
43 " -G|--gid <gid> } the group list\n",
44 stderr) == EOF) { perror("write usage"); exit(-1); }
47 static const char *opt_user, *opt_useronly;
48 static int opt_groupsclear= 0, opt_ngids= 0, opt_uidonly= -1;
49 static int opt_gids[512];
51 static void af_uidonly(const struct cmdinfo *cip, const char *value) {
55 ul= strtoul(value,&ep,10);
56 if (*ep) { fprintf(stderr,"bad uid `%s'\n",value); exit(-1); }
60 static void af_group(const struct cmdinfo *cip, const char *value) {
63 if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
64 badusage("too many groups specified");
66 if (!gr) { fprintf(stderr,"unknown group `%s'\n",value); exit(-1); }
67 opt_gids[opt_ngids++]= gr->gr_gid;
70 static void af_gid(const struct cmdinfo *cip, const char *value) {
74 if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
75 badusage("too many gids specified");
76 ul= strtoul(value,&ep,0);
77 if ((*ep) || (uid_t)ul != ul || ul>INT_MAX) badusage("bad gid `%s'",value);
78 opt_gids[opt_ngids++]= ul;
81 static void af_help(const struct cmdinfo *cip, const char *value) {
82 usagemessage(); exit(0);
85 static const struct cmdinfo cmdinfos[]= {
86 { "user", 'u', 1, 0, &opt_user, 0, },
87 { "useronly", 'i', 1, 0, &opt_useronly, 0 },
88 { "uidonly", 'I', 1, 0, 0, af_uidonly },
89 { "groupsclear", 'z', 0, &opt_groupsclear, 0, 0, 1 },
90 { "group", 'g', 1, 0, 0, af_group },
91 { "gid", 'G', 1, 0, 0, af_gid },
92 { "help", 'h', 0, 0, 0, af_help },
96 #ifdef REALLY_CHECK_FILE
97 static void checkroot(void) {
99 r= access(REALLY_CHECK_FILE,W_OK);
100 if (r) { perror("sorry"); exit(-1); }
103 #ifdef REALLY_CHECK_GID
104 static void checkroot(void) {
108 r= getgid(); if (r==REALLY_CHECK_GID) return;
109 if (r<0) { perror("getgid check"); exit(-1); }
110 r= getgroups(sizeof(groups)/sizeof(groups[0]),groups);
111 if (r<0) { perror("getgroups check"); exit(-1); }
113 if (groups[i] == REALLY_CHECK_GID) return;
114 fputs("sorry\n",stderr); exit(-1);
117 #ifdef REALLY_CHECK_NONE
118 static void checkroot(void) {
122 int main(int argc, const char *const *argv) {
123 struct passwd *pw= 0;
125 int i, j, ngroups, ngroups_in, maingid, orgmaingid, mainuid, orgmainuid, r;
129 myopt(&argv,cmdinfos);
131 if (opt_groupsclear && !opt_ngids)
132 badusage("-z|--groupsclear must be accompanied by some groups");
133 if (opt_user && (opt_useronly || opt_uidonly!=-1))
134 badusage("-u|--user may not be used with -i|--useronly or -I|--uidonly");
135 if (opt_user && opt_groupsclear)
136 badusage("-u|--user may not be used with -z|--groupsclear");
137 if (opt_uidonly != -1 && (uid_t)opt_uidonly != opt_uidonly)
138 badusage("-I|--uidonly value %d is out of range for a uid",opt_uidonly);
140 if (!opt_user && !opt_useronly && opt_uidonly==-1 && !opt_ngids) {
143 if (opt_user || opt_useronly) {
144 cp= opt_user ? opt_user : opt_useronly;
146 if (!pw) { fprintf(stderr,"unknown user `%s'\n",cp); exit(-1); }
147 opt_uidonly= pw->pw_uid;
149 orgmaingid= getgid();
150 orgmainuid= getuid();
151 if (orgmaingid<0) { perror("getgid failed"); exit(-1); }
153 r= initgroups(opt_user,pw->pw_gid);
154 if (r) { perror("initgroups failed"); exit(-1); }
159 if (opt_groupsclear) {
161 if (opt_ngids > sizeof(groups)/sizeof(groups[0])) {
162 fputs("too many groups to set\n",stderr);
166 ngroups= getgroups(0,0);
167 if (ngroups<0) { perror("getgroups(0,0) failed"); exit(-1); }
168 if (ngroups+opt_ngids > sizeof(groups)/sizeof(groups[0])) {
169 fputs("too many groups already set for total to fit\n",stderr);
172 ngroups= getgroups(ngroups,groups);
173 if (ngroups<0) { perror("getgroups failed"); exit(-1); }
176 maingid= opt_gids[0];
178 if (opt_ngids || opt_groupsclear) {
179 ngroups_in= ngroups; ngroups= 0;
180 for (i=0; i<ngroups_in; i++) {
181 for (j=0; j<ngroups && groups[j] != groups[i]; j++);
182 if (j<ngroups) continue;
183 groups[ngroups++]= groups[i];
185 for (i=0; i<opt_ngids; i++) {
186 for (j=0; j<ngroups && groups[j] != opt_gids[i]; j++);
187 if (j<ngroups) continue;
188 groups[ngroups++]= opt_gids[i];
190 r= setgroups(ngroups,groups);
191 if (r) { perror("setgroups failed"); exit(-1); }
194 r= setgid(maingid); if (r) { perror("setgid failed"); exit(-1); }
195 r= setgid(maingid); if (r) { perror("2nd setgid failed"); exit(-1); }
197 if (opt_uidonly != -1) {
198 mainuid= opt_uidonly;
202 r= setuid(mainuid); if (r) { perror("setuid failed"); exit(-1); }
203 r= setuid(mainuid); if (r) { perror("2nd setuid failed"); exit(-1); }
205 r= seteuid(0); if (r>=0) { fputs("could seteuid 0",stderr); exit(-1); }
206 if (errno != EPERM) {
207 perror("unexpected failure mode for seteuid 0"); exit(-1);
210 r= getuid(); if (r<0) { perror("getuid failed"); exit(-1); }
211 if (r != mainuid) { fputs("getuid mismatch",stderr); exit(-1); }
212 r= geteuid(); if (r<0) { perror("geteuid failed"); exit(-1); }
213 if (r != mainuid) { fputs("geteuid mismatch",stderr); exit(-1); }
215 for (i=0; i<ngroups && maingid != groups[i]; i++);
216 if (i>=ngroups && maingid != orgmaingid) {
217 r= setgid(orgmaingid);
218 if (r>=0) { fputs("could setgid back",stderr); exit(-1); }
219 if (errno != EPERM) {
220 perror("unexpected failure mode for setgid back"); exit(-1);
223 r= getgid(); if (r<0) { perror("getgid failed"); exit(-1); }
224 if (r != maingid) { fputs("getgid mismatch",stderr); exit(-1); }
225 r= getegid(); if (r<0) { perror("getegid failed"); exit(-1); }
226 if (r != maingid) { fputs("getegid mismatch",stderr); exit(-1); }
231 execlp(cp,cp,"-i",(const char*)0);
233 execvp(argv[0],(char**)argv);
235 perror("exec failed");