chiark / gitweb /
new -t option for putting tab before filename
[chiark-utils.git] / cprogs / really.c
1 /*
2  * really.c - program for gaining privilege
3  *
4  * Copyright (C) 1992-3 Ian Jackson <iwj10@cus.cam.ac.uk>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include <unistd.h>
25 #include <pwd.h>
26 #include <grp.h>
27 #include <sys/types.h>
28 #include <errno.h>
29
30 #include "myopt.h"
31
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); }
48 }
49
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];
53
54 static void af_uidonly(const struct cmdinfo *cip, const char *value) {
55   unsigned long ul;
56   char *ep;
57
58   ul= strtoul(value,&ep,10);
59   if (*ep) { fprintf(stderr,"bad uid `%s'\n",value); exit(-1); }
60   opt_uidonly= ul;
61 }
62
63 static void af_group(const struct cmdinfo *cip, const char *value) {
64   struct group *gr;
65
66   if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
67     badusage("too many groups specified");
68   gr= getgrnam(value);
69   if (!gr) { fprintf(stderr,"unknown group `%s'\n",value); exit(-1); }
70   opt_gids[opt_ngids++]= gr->gr_gid;
71 }
72
73 static void af_gid(const struct cmdinfo *cip, const char *value) {
74   char *ep;
75   unsigned long ul;
76
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;
82 }
83
84 static void af_help(const struct cmdinfo *cip, const char *value) {
85   usagemessage(); exit(0);
86 }
87
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      },
97   {  0,              0                                            }
98 };
99
100 #ifdef REALLY_CHECK_FILE
101 static int checkroot(void) {
102   int r;
103   r= access(REALLY_CHECK_FILE,W_OK);
104   if (r) return -1;
105   return 0;
106 }
107 #endif
108 #ifdef REALLY_CHECK_GID
109 static int checkroot(void) {
110   gid_t groups[512];
111   int r, i;
112
113   r= getgid(); if (r==REALLY_CHECK_GID) return 0;
114   if (r<0) { perror("getgid check"); exit(-1); }
115   r= getgroups(sizeof(groups)/sizeof(groups[0]),groups);
116   if (r<0) { perror("getgroups check"); exit(-1); }
117   for (i=0; i<r; i++)
118     if (groups[i] == REALLY_CHECK_GID) return 0;
119   return -1;
120 }
121 #endif
122 #ifdef REALLY_CHECK_NONE
123 static int checkroot(void) {
124   return 0;
125 }
126 #endif
127
128 int main(int argc, const char *const *argv) {
129   struct passwd *pw= 0;
130   gid_t groups[512];
131   int i, j, ngroups, ngroups_in, maingid, orgmaingid, mainuid, orgmainuid, r;
132   const char *cp;
133   
134   orgmainuid= getuid();
135   if (orgmainuid && checkroot()) { perror("sorry"); exit(-1); }
136   myopt(&argv,cmdinfos);
137
138   if (opt_groupsclear && !opt_ngids)
139     badusage("-z|--groupsclear must be accompanied by some groups");
140   if (opt_user && (opt_useronly || opt_uidonly!=-1))
141     badusage("-u|--user may not be used with -i|--useronly or -I|--uidonly");
142   if (opt_user && opt_groupsclear)
143     badusage("-u|--user may not be used with -z|--groupsclear");
144   if (opt_uidonly != -1 && (uid_t)opt_uidonly != opt_uidonly)
145     badusage("-I|--uidonly value %d is out of range for a uid",opt_uidonly);
146
147   if (!opt_user && !opt_useronly && opt_uidonly==-1 && !opt_ngids) {
148     opt_uidonly= 0;
149   }
150   if (opt_user || opt_useronly) {
151     cp= opt_user ? opt_user : opt_useronly;
152     pw= getpwnam(cp);
153     if (!pw) { fprintf(stderr,"unknown user `%s'\n",cp); exit(-1); }
154     opt_uidonly= pw->pw_uid;
155   }
156   if (opt_chroot) {
157     if (chroot(opt_chroot)) { perror("chroot failed"); exit(-1); }
158   }
159   orgmaingid= getgid();
160   if (orgmaingid<0) { perror("getgid failed"); exit(-1); }
161   if (opt_user) {
162     r= initgroups(opt_user,pw->pw_gid);
163     if (r) { perror("initgroups failed"); exit(-1); }
164     maingid= pw->pw_gid;
165   } else {
166     maingid= -1;
167   }
168   if (opt_groupsclear) {
169     ngroups= 0;
170     if (opt_ngids > sizeof(groups)/sizeof(groups[0])) {
171       fputs("too many groups to set\n",stderr);
172       exit(-1);
173     }
174   } else {
175     ngroups= getgroups(0,0);
176     if (ngroups<0) { perror("getgroups(0,0) failed"); exit(-1); }
177     if (ngroups+opt_ngids > sizeof(groups)/sizeof(groups[0])) {
178       fputs("too many groups already set for total to fit\n",stderr);
179       exit(-1);
180     }
181     ngroups= getgroups(ngroups,groups);
182     if (ngroups<0) { perror("getgroups failed"); exit(-1); }
183   }
184   if (opt_ngids) {
185     maingid= opt_gids[0];
186   }
187   if (opt_ngids || opt_groupsclear) {
188     ngroups_in= ngroups; ngroups= 0;
189     for (i=0; i<ngroups_in; i++) {
190       for (j=0; j<ngroups && groups[j] != groups[i]; j++);
191       if (j<ngroups) continue;
192       groups[ngroups++]= groups[i];
193     }
194     for (i=0; i<opt_ngids; i++) {
195       for (j=0; j<ngroups && groups[j] != opt_gids[i]; j++);
196       if (j<ngroups) continue;
197       groups[ngroups++]= opt_gids[i];
198     }
199     r= setgroups(ngroups,groups);
200     if (r) { perror("setgroups failed"); exit(-1); }
201   }
202   if (maingid != -1) {
203     r= setgid(maingid); if (r) { perror("setgid failed"); exit(-1); }
204     r= setgid(maingid); if (r) { perror("2nd setgid failed"); exit(-1); }
205   }
206   if (opt_uidonly != -1) {
207     mainuid= opt_uidonly;
208   } else {
209     mainuid= orgmainuid;
210   }
211   r= setuid(mainuid); if (r) { perror("setuid failed"); exit(-1); }
212   r= setuid(mainuid); if (r) { perror("2nd setuid failed"); exit(-1); }
213   if (mainuid != 0) {
214     r= seteuid(0); if (r>=0) { fputs("could seteuid 0",stderr); exit(-1); }
215     if (errno != EPERM) {
216       perror("unexpected failure mode for seteuid 0"); exit(-1);
217     }
218   }
219   r= getuid(); if (r<0) { perror("getuid failed"); exit(-1); }
220   if (r != mainuid) { fputs("getuid mismatch",stderr); exit(-1); }
221   r= geteuid(); if (r<0) { perror("geteuid failed"); exit(-1); }
222   if (r != mainuid) { fputs("geteuid mismatch",stderr); exit(-1); }
223   if (maingid != -1) {
224     for (i=0; i<ngroups && maingid != groups[i]; i++);
225     if (i>=ngroups && maingid != orgmaingid) {
226       r= setgid(orgmaingid);
227       if (r>=0) { fputs("could setgid back",stderr); exit(-1); }
228       if (errno != EPERM) {
229         perror("unexpected failure mode for setgid back"); exit(-1);
230       }
231     }
232     r= getgid(); if (r<0) { perror("getgid failed"); exit(-1); }
233     if (r != maingid) { fputs("getgid mismatch",stderr); exit(-1); }
234     r= getegid(); if (r<0) { perror("getegid failed"); exit(-1); }
235     if (r != maingid) { fputs("getegid mismatch",stderr); exit(-1); }
236   }
237   if (!*argv) {
238     cp= getenv("SHELL");
239     if (!cp) cp= "sh";
240     execlp(cp,cp,"-i",(const char*)0);
241   } else {
242     execvp(argv[0],(char**)argv);
243   }
244   perror("exec failed");
245   exit(-1);
246 }