File: really.c

package info (click to toggle)
chiark-utils 8.0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,084 kB
  • sloc: ansic: 4,640; perl: 4,281; sh: 671; python: 465; makefile: 286; tcl: 228
file content (252 lines) | stat: -rw-r--r-- 8,655 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * really.c - program for gaining privilege
 *
 * Copyright (C) 1992-3 Ian Jackson <ian@davenant.greenend.org.uk>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3,
 * or (at your option) any later version.
 *
 * This 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 file; if not, consult the Free Software
 * Foundation's website at www.fsf.org, or the GNU Project website at
 * www.gnu.org.
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <errno.h>

#include "myopt.h"

void usagemessage(void) {
  if (fputs("usage: really [<really-option> ...] [--]"
            " [<command> [<argument/option> ...]]\n"
            "really-options specifying the user:\n"
            " if no options given, set the uid to 0;\n"
            " -u|--user <username>     also sets their default group list\n"
            " -i|--useronly <username> } set the uid\n"
            " -I|--uidonly <uid>       }  but inherits the group list\n"
            "really-options specifying the group:\n"
            " -z|--groupsclear         only groups specified are to be used\n"
            " -g|--group <groupname>   } add this to\n"
            " -G|--gid <gid>           }  the group list\n"
            "other really-options:\n"
	    " -h|--help                display this message\n"
	    " -R|--chroot <dir>        chroot (but *not* chdir - danger!)\n",
            stderr) == EOF) { perror("write usage"); exit(-1); }
}

static const char *opt_user, *opt_useronly, *opt_chroot;
static int opt_groupsclear= 0, opt_ngids= 0, opt_uidonly= -1;
static int opt_gids[512];

static void af_uidonly(const struct cmdinfo *cip, const char *value) {
  unsigned long ul;
  char *ep;

  ul= strtoul(value,&ep,10);
  if (*ep) { fprintf(stderr,"bad uid `%s'\n",value); exit(-1); }
  opt_uidonly= ul;
}

static void af_group(const struct cmdinfo *cip, const char *value) {
  struct group *gr;

  if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
    badusage("too many groups specified");
  gr= getgrnam(value);
  if (!gr) { fprintf(stderr,"unknown group `%s'\n",value); exit(-1); }
  opt_gids[opt_ngids++]= gr->gr_gid;
}

static void af_gid(const struct cmdinfo *cip, const char *value) {
  char *ep;
  unsigned long ul;

  if (opt_ngids >= sizeof(opt_gids)/sizeof(opt_gids[0]))
    badusage("too many gids specified");
  ul= strtoul(value,&ep,0);
  if ((*ep) || (uid_t)ul != ul || ul>INT_MAX) badusage("bad gid `%s'",value);
  opt_gids[opt_ngids++]= ul;
}

static void af_help(const struct cmdinfo *cip, const char *value) {
  usagemessage(); exit(0);
}

static const struct cmdinfo cmdinfos[]= {
  { "user",         'u',  1,  0, &opt_user,          0,           },
  { "useronly",     'i',  1,  0, &opt_useronly,      0            },
  { "uidonly",      'I',  1,  0, 0,                  af_uidonly   },
  { "groupsclear",  'z',  0,  &opt_groupsclear, 0,   0,        1  },
  { "group",        'g',  1,  0, 0,                  af_group     },
  { "gid",          'G',  1,  0, 0,                  af_gid       },
  { "chroot",       'R',  1,  0, &opt_chroot,        0            },
  { "help",         'h',  0,  0, 0,                  af_help      },
  {  0,              0                                            }
};

#ifdef REALLY_CHECK_FILE
static int checkroot(void) {
  int r;
  r= access(REALLY_CHECK_FILE,   W_OK);
  if (!r) return 0;
#ifdef REALLY_CHECK_FILE_2
  r= access(REALLY_CHECK_FILE_2, W_OK);
  if (!r) return 0;
  /* If all fails we return the errno from file _2 */
#endif /*REALLY_CHECK_FILE_2*/
  return -1;
}
#endif
#ifdef REALLY_CHECK_GID
static int checkroot(void) {
  gid_t groups[512];
  int r, i;

  r= getgid(); if (r==REALLY_CHECK_GID) return 0;
  if (r<0) { perror("getgid check"); exit(-1); }
  r= getgroups(sizeof(groups)/sizeof(groups[0]),groups);
  if (r<0) { perror("getgroups check"); exit(-1); }
  for (i=0; i<r; i++)
    if (groups[i] == REALLY_CHECK_GID) return 0;
  return -1;
}
#endif
#ifdef REALLY_CHECK_NONE
static int checkroot(void) {
  return 0;
}
#endif

int main(int argc, const char *const *argv) {
  struct passwd *pw= 0;
  gid_t groups[512];
  int i, j, ngroups, ngroups_in, maingid, orgmaingid, mainuid, orgmainuid, r;
  const char *cp;
  
  orgmainuid= getuid();
  if (orgmainuid && checkroot()) { perror("sorry"); exit(-1); }
  myopt(&argv,cmdinfos);

  if (opt_groupsclear && !opt_ngids)
    badusage("-z|--groupsclear must be accompanied by some groups");
  if (opt_user && (opt_useronly || opt_uidonly!=-1))
    badusage("-u|--user may not be used with -i|--useronly or -I|--uidonly");
  if (opt_user && opt_groupsclear)
    badusage("-u|--user may not be used with -z|--groupsclear");
  if (opt_uidonly != -1 && (uid_t)opt_uidonly != opt_uidonly)
    badusage("-I|--uidonly value %d is out of range for a uid",opt_uidonly);

  if (!opt_user && !opt_useronly && opt_uidonly==-1 && !opt_ngids) {
    opt_uidonly= 0;
  }
  if (opt_user || opt_useronly) {
    cp= opt_user ? opt_user : opt_useronly;
    pw= getpwnam(cp);
    if (!pw) { fprintf(stderr,"unknown user `%s'\n",cp); exit(-1); }
    opt_uidonly= pw->pw_uid;
  }
  if (opt_chroot) {
    if (chroot(opt_chroot)) { perror("chroot failed"); exit(-1); }
  }
  orgmaingid= getgid();
  if (orgmaingid<0) { perror("getgid failed"); exit(-1); }
  if (opt_user) {
    r= initgroups(opt_user,pw->pw_gid);
    if (r) { perror("initgroups failed"); exit(-1); }
    maingid= pw->pw_gid;
  } else {
    maingid= -1;
  }
  if (opt_groupsclear) {
    ngroups= 0;
    if (opt_ngids > sizeof(groups)/sizeof(groups[0])) {
      fputs("too many groups to set\n",stderr);
      exit(-1);
    }
  } else {
    ngroups= getgroups(0,0);
    if (ngroups<0) { perror("getgroups(0,0) failed"); exit(-1); }
    if (ngroups+opt_ngids > sizeof(groups)/sizeof(groups[0])) {
      fputs("too many groups already set for total to fit\n",stderr);
      exit(-1);
    }
    ngroups= getgroups(ngroups,groups);
    if (ngroups<0) { perror("getgroups failed"); exit(-1); }
  }
  if (opt_ngids) {
    maingid= opt_gids[0];
  }
  if (opt_ngids || opt_groupsclear) {
    ngroups_in= ngroups; ngroups= 0;
    for (i=0; i<ngroups_in; i++) {
      for (j=0; j<ngroups && groups[j] != groups[i]; j++);
      if (j<ngroups) continue;
      groups[ngroups++]= groups[i];
    }
    for (i=0; i<opt_ngids; i++) {
      for (j=0; j<ngroups && groups[j] != opt_gids[i]; j++);
      if (j<ngroups) continue;
      groups[ngroups++]= opt_gids[i];
    }
    r= setgroups(ngroups,groups);
    if (r) { perror("setgroups failed"); exit(-1); }
  }
  if (maingid != -1) {
    r= setgid(maingid); if (r) { perror("setgid failed"); exit(-1); }
    r= setgid(maingid); if (r) { perror("2nd setgid failed"); exit(-1); }
  }
  if (opt_uidonly != -1) {
    mainuid= opt_uidonly;
  } else {
    mainuid= orgmainuid;
  }
  r= setuid(mainuid); if (r) { perror("setuid failed"); exit(-1); }
  r= setuid(mainuid); if (r) { perror("2nd setuid failed"); exit(-1); }
  if (mainuid != 0) {
    r= seteuid(0); if (r>=0) { fputs("could seteuid 0",stderr); exit(-1); }
    if (errno != EPERM) {
      perror("unexpected failure mode for seteuid 0"); exit(-1);
    }
  }
  r= getuid(); if (r<0) { perror("getuid failed"); exit(-1); }
  if (r != mainuid) { fputs("getuid mismatch",stderr); exit(-1); }
  r= geteuid(); if (r<0) { perror("geteuid failed"); exit(-1); }
  if (r != mainuid) { fputs("geteuid mismatch",stderr); exit(-1); }
  if (maingid != -1) {
    for (i=0; i<ngroups && maingid != groups[i]; i++);
    if (i>=ngroups && maingid != orgmaingid) {
      r= setgid(orgmaingid);
      if (r>=0) { fputs("could setgid back",stderr); exit(-1); }
      if (errno != EPERM) {
        perror("unexpected failure mode for setgid back"); exit(-1);
      }
    }
    r= getgid(); if (r<0) { perror("getgid failed"); exit(-1); }
    if (r != maingid) { fputs("getgid mismatch",stderr); exit(-1); }
    r= getegid(); if (r<0) { perror("getegid failed"); exit(-1); }
    if (r != maingid) { fputs("getegid mismatch",stderr); exit(-1); }
  }
  if (!*argv) {
    cp= getenv("SHELL");
    if (!cp) cp= "sh";
    execlp(cp,cp,"-i",(const char*)0);
  } else {
    execvp(argv[0],(char**)argv);
  }
  perror("exec failed");
  exit(-1);
}