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 [<really-option> ...] [--]"
34 " [<command> [<argument/option> ...]]\n"
35 "really-options specifying the user:\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"
40 "really-options specifying the group:\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 "other really-options:\n"
45 " -h|--help display this message\n"
46 " -R|--chroot <dir> chroot (but *not* chdir)\n",
47 stderr) == EOF) { perror("write usage"); exit(-1); }
50 static const char *opt_user, *opt_useronly, *opt_chroot;
51 static int opt_groupsclear= 0, opt_ngids= 0, opt_uidonly= -1;
52 static int opt_gids[512];
54 static void af_uidonly(const struct cmdinfo *cip, const char *value) {
58 ul= strtoul(value,&ep,10);
59 if (*ep) { fprintf(stderr,"bad uid `%s'\n",value); exit(-1); }
63 static void af_group(const struct cmdinfo *cip, const char *value) {
66 if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
67 badusage("too many groups specified");
69 if (!gr) { fprintf(stderr,"unknown group `%s'\n",value); exit(-1); }
70 opt_gids[opt_ngids++]= gr->gr_gid;
73 static void af_gid(const struct cmdinfo *cip, const char *value) {
77 if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
78 badusage("too many gids specified");
79 ul= strtoul(value,&ep,0);
80 if ((*ep) || (uid_t)ul != ul || ul>INT_MAX) badusage("bad gid `%s'",value);
81 opt_gids[opt_ngids++]= ul;
84 static void af_help(const struct cmdinfo *cip, const char *value) {
85 usagemessage(); exit(0);
88 static const struct cmdinfo cmdinfos[]= {
89 { "user", 'u', 1, 0, &opt_user, 0, },
90 { "useronly", 'i', 1, 0, &opt_useronly, 0 },
91 { "uidonly", 'I', 1, 0, 0, af_uidonly },
92 { "groupsclear", 'z', 0, &opt_groupsclear, 0, 0, 1 },
93 { "group", 'g', 1, 0, 0, af_group },
94 { "gid", 'G', 1, 0, 0, af_gid },
95 { "chroot", 'R', 1, 0, &opt_chroot, 0 },
96 { "help", 'h', 0, 0, 0, af_help },
100 #ifdef REALLY_CHECK_FILE
101 static void checkroot(void) {
103 r= access(REALLY_CHECK_FILE,W_OK);
104 if (r) { perror("sorry"); exit(-1); }
107 #ifdef REALLY_CHECK_GID
108 static void checkroot(void) {
112 r= getgid(); if (r==REALLY_CHECK_GID) return;
113 if (r<0) { perror("getgid check"); exit(-1); }
114 r= getgroups(sizeof(groups)/sizeof(groups[0]),groups);
115 if (r<0) { perror("getgroups check"); exit(-1); }
117 if (groups[i] == REALLY_CHECK_GID) return;
118 fputs("sorry\n",stderr); exit(-1);
121 #ifdef REALLY_CHECK_NONE
122 static void checkroot(void) {
126 int main(int argc, const char *const *argv) {
127 struct passwd *pw= 0;
129 int i, j, ngroups, ngroups_in, maingid, orgmaingid, mainuid, orgmainuid, r;
133 myopt(&argv,cmdinfos);
135 if (opt_groupsclear && !opt_ngids)
136 badusage("-z|--groupsclear must be accompanied by some groups");
137 if (opt_user && (opt_useronly || opt_uidonly!=-1))
138 badusage("-u|--user may not be used with -i|--useronly or -I|--uidonly");
139 if (opt_user && opt_groupsclear)
140 badusage("-u|--user may not be used with -z|--groupsclear");
141 if (opt_uidonly != -1 && (uid_t)opt_uidonly != opt_uidonly)
142 badusage("-I|--uidonly value %d is out of range for a uid",opt_uidonly);
144 if (!opt_user && !opt_useronly && opt_uidonly==-1 && !opt_ngids) {
147 if (opt_user || opt_useronly) {
148 cp= opt_user ? opt_user : opt_useronly;
150 if (!pw) { fprintf(stderr,"unknown user `%s'\n",cp); exit(-1); }
151 opt_uidonly= pw->pw_uid;
154 if (chroot(opt_chroot)) { perror("chroot failed"); exit(-1); }
156 orgmaingid= getgid();
157 orgmainuid= getuid();
158 if (orgmaingid<0) { perror("getgid failed"); exit(-1); }
160 r= initgroups(opt_user,pw->pw_gid);
161 if (r) { perror("initgroups failed"); exit(-1); }
166 if (opt_groupsclear) {
168 if (opt_ngids > sizeof(groups)/sizeof(groups[0])) {
169 fputs("too many groups to set\n",stderr);
173 ngroups= getgroups(0,0);
174 if (ngroups<0) { perror("getgroups(0,0) failed"); exit(-1); }
175 if (ngroups+opt_ngids > sizeof(groups)/sizeof(groups[0])) {
176 fputs("too many groups already set for total to fit\n",stderr);
179 ngroups= getgroups(ngroups,groups);
180 if (ngroups<0) { perror("getgroups failed"); exit(-1); }
183 maingid= opt_gids[0];
185 if (opt_ngids || opt_groupsclear) {
186 ngroups_in= ngroups; ngroups= 0;
187 for (i=0; i<ngroups_in; i++) {
188 for (j=0; j<ngroups && groups[j] != groups[i]; j++);
189 if (j<ngroups) continue;
190 groups[ngroups++]= groups[i];
192 for (i=0; i<opt_ngids; i++) {
193 for (j=0; j<ngroups && groups[j] != opt_gids[i]; j++);
194 if (j<ngroups) continue;
195 groups[ngroups++]= opt_gids[i];
197 r= setgroups(ngroups,groups);
198 if (r) { perror("setgroups failed"); exit(-1); }
201 r= setgid(maingid); if (r) { perror("setgid failed"); exit(-1); }
202 r= setgid(maingid); if (r) { perror("2nd setgid failed"); exit(-1); }
204 if (opt_uidonly != -1) {
205 mainuid= opt_uidonly;
209 r= setuid(mainuid); if (r) { perror("setuid failed"); exit(-1); }
210 r= setuid(mainuid); if (r) { perror("2nd setuid failed"); exit(-1); }
212 r= seteuid(0); if (r>=0) { fputs("could seteuid 0",stderr); exit(-1); }
213 if (errno != EPERM) {
214 perror("unexpected failure mode for seteuid 0"); exit(-1);
217 r= getuid(); if (r<0) { perror("getuid failed"); exit(-1); }
218 if (r != mainuid) { fputs("getuid mismatch",stderr); exit(-1); }
219 r= geteuid(); if (r<0) { perror("geteuid failed"); exit(-1); }
220 if (r != mainuid) { fputs("geteuid mismatch",stderr); exit(-1); }
222 for (i=0; i<ngroups && maingid != groups[i]; i++);
223 if (i>=ngroups && maingid != orgmaingid) {
224 r= setgid(orgmaingid);
225 if (r>=0) { fputs("could setgid back",stderr); exit(-1); }
226 if (errno != EPERM) {
227 perror("unexpected failure mode for setgid back"); exit(-1);
230 r= getgid(); if (r<0) { perror("getgid failed"); exit(-1); }
231 if (r != maingid) { fputs("getgid mismatch",stderr); exit(-1); }
232 r= getegid(); if (r<0) { perror("getegid failed"); exit(-1); }
233 if (r != maingid) { fputs("getegid mismatch",stderr); exit(-1); }
238 execlp(cp,cp,"-i",(const char*)0);
240 execvp(argv[0],(char**)argv);
242 perror("exec failed");