chiark / gitweb /
@@ -1,3 +1,9 @@
[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 [<user-option>] [<group-option> ...] [--]"
34             " [<command> [<argument/option> ...]]\n"
35             "user-options:\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             "group-options:\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); }
45 }
46
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];
50
51 static void af_uidonly(const struct cmdinfo *cip, const char *value) {
52   unsigned long ul;
53   char *ep;
54
55   ul= strtoul(value,&ep,10);
56   if (*ep) { fprintf(stderr,"bad uid `%s'\n",value); exit(-1); }
57   opt_uidonly= ul;
58 }
59
60 static void af_group(const struct cmdinfo *cip, const char *value) {
61   struct group *gr;
62
63   if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
64     badusage("too many groups specified");
65   gr= getgrnam(value);
66   if (!gr) { fprintf(stderr,"unknown group `%s'\n",value); exit(-1); }
67   opt_gids[opt_ngids++]= gr->gr_gid;
68 }
69
70 static void af_gid(const struct cmdinfo *cip, const char *value) {
71   char *ep;
72   unsigned long ul;
73
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;
79 }
80
81 static void af_help(const struct cmdinfo *cip, const char *value) {
82   usagemessage(); exit(0);
83 }
84
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      },
93   {  0,              0                                            }
94 };
95
96 #ifdef REALLY_CHECK_FILE
97 static void checkroot(void) {
98   int r;
99   r= access(REALLY_CHECK_FILE,W_OK);
100   if (r) { perror("sorry"); exit(-1); }
101 }
102 #endif
103 #ifdef REALLY_CHECK_GID
104 static void checkroot(void) {
105   gid_t groups[512];
106   int r, i;
107
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); }
112   for (i=0; i<r; i++)
113     if (groups[i] == REALLY_CHECK_GID) return;
114   fputs("sorry\n",stderr); exit(-1);
115 }
116 #endif
117 #ifdef REALLY_CHECK_NONE
118 static void checkroot(void) {
119 }
120 #endif
121
122 int main(int argc, const char *const *argv) {
123   struct passwd *pw= 0;
124   gid_t groups[512];
125   int i, j, ngroups, ngroups_in, maingid, orgmaingid, mainuid, orgmainuid, r;
126   const char *cp;
127   
128   checkroot();
129   myopt(&argv,cmdinfos);
130
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);
139
140   if (!opt_user && !opt_useronly && opt_uidonly==-1 && !opt_ngids) {
141     opt_uidonly= 0;
142   }
143   if (opt_user || opt_useronly) {
144     cp= opt_user ? opt_user : opt_useronly;
145     pw= getpwnam(cp);
146     if (!pw) { fprintf(stderr,"unknown user `%s'\n",cp); exit(-1); }
147     opt_uidonly= pw->pw_uid;
148   }
149   orgmaingid= getgid();
150   orgmainuid= getuid();
151   if (orgmaingid<0) { perror("getgid failed"); exit(-1); }
152   if (opt_user) {
153     r= initgroups(opt_user,pw->pw_gid);
154     if (r) { perror("initgroups failed"); exit(-1); }
155     maingid= pw->pw_gid;
156   } else {
157     maingid= -1;
158   }
159   if (opt_groupsclear) {
160     ngroups= 0;
161     if (opt_ngids > sizeof(groups)/sizeof(groups[0])) {
162       fputs("too many groups to set\n",stderr);
163       exit(-1);
164     }
165   } else {
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);
170       exit(-1);
171     }
172     ngroups= getgroups(ngroups,groups);
173     if (ngroups<0) { perror("getgroups failed"); exit(-1); }
174   }
175   if (opt_ngids) {
176     maingid= opt_gids[0];
177   }
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];
184     }
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];
189     }
190     r= setgroups(ngroups,groups);
191     if (r) { perror("setgroups failed"); exit(-1); }
192   }
193   if (maingid != -1) {
194     r= setgid(maingid); if (r) { perror("setgid failed"); exit(-1); }
195     r= setgid(maingid); if (r) { perror("2nd setgid failed"); exit(-1); }
196   }
197   if (opt_uidonly != -1) {
198     mainuid= opt_uidonly;
199   } else {
200     mainuid= orgmainuid;
201   }
202   r= setuid(mainuid); if (r) { perror("setuid failed"); exit(-1); }
203   r= setuid(mainuid); if (r) { perror("2nd setuid failed"); exit(-1); }
204   if (mainuid != 0) {
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);
208     }
209   }
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); }
214   if (maingid != -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);
221       }
222     }
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); }
227   }
228   if (!*argv) {
229     cp= getenv("SHELL");
230     if (!cp) cp= "sh";
231     execlp(cp,cp,"-i",(const char*)0);
232   } else {
233     execvp(argv[0],(char**)argv);
234   }
235   perror("exec failed");
236   exit(-1);
237 }