su.c revision 1.8 1 /*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)su.c 5.26 (Berkeley) 7/6/91";*/
42 static char rcsid[] = "$Id: su.c,v 1.8 1994/01/07 16:05:42 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <syslog.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <pwd.h>
52 #include <grp.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <paths.h>
56
57 #ifdef KERBEROS
58 #include <kerberosIV/des.h>
59 #include <kerberosIV/krb.h>
60 #include <netdb.h>
61
62 #define ARGSTR "-Kflm"
63
64 int use_kerberos = 1;
65 #else
66 #define ARGSTR "-flm"
67 #endif
68
69 extern char *crypt();
70 int chshell();
71
72 int
73 main(argc, argv)
74 int argc;
75 char **argv;
76 {
77 extern char **environ;
78 extern int errno, optind;
79 register struct passwd *pwd;
80 register char *p, **g;
81 struct group *gr;
82 uid_t ruid, getuid();
83 int asme, ch, asthem, fastlogin, prio;
84 enum { UNSET, YES, NO } iscsh = UNSET;
85 char *user, *shell, *avshell, *username, *cleanenv[10], **np;
86 char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
87 char *getpass(), *getenv(), *getlogin(), *ontty();
88
89 asme = asthem = fastlogin = 0;
90 while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
91 switch((char)ch) {
92 #ifdef KERBEROS
93 case 'K':
94 use_kerberos = 0;
95 break;
96 #endif
97 case 'f':
98 fastlogin = 1;
99 break;
100 case '-':
101 case 'l':
102 asme = 0;
103 asthem = 1;
104 break;
105 case 'm':
106 asme = 1;
107 asthem = 0;
108 break;
109 case '?':
110 default:
111 (void)fprintf(stderr, "usage: su [%s] [login]\n",
112 ARGSTR);
113 exit(1);
114 }
115 argv += optind;
116
117 errno = 0;
118 prio = getpriority(PRIO_PROCESS, 0);
119 if (errno)
120 prio = 0;
121 (void)setpriority(PRIO_PROCESS, 0, -2);
122 openlog("su", LOG_CONS, 0);
123
124 /* get current login name and shell */
125 ruid = getuid();
126 username = getlogin();
127 if (username == NULL || (pwd = getpwnam(username)) == NULL ||
128 pwd->pw_uid != ruid)
129 pwd = getpwuid(ruid);
130 if (pwd == NULL) {
131 fprintf(stderr, "su: who are you?\n");
132 exit(1);
133 }
134 username = strdup(pwd->pw_name);
135 if (asme)
136 if (pwd->pw_shell && *pwd->pw_shell)
137 shell = strcpy(shellbuf, pwd->pw_shell);
138 else {
139 shell = _PATH_BSHELL;
140 iscsh = NO;
141 }
142
143 /* get target login information, default to root */
144 user = *argv ? *argv : "root";
145 np = *argv ? argv : argv-1;
146
147 if ((pwd = getpwnam(user)) == NULL) {
148 fprintf(stderr, "su: unknown login %s\n", user);
149 exit(1);
150 }
151
152 if (ruid) {
153 #ifdef KERBEROS
154 if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
155 #endif
156 {
157 /* only allow those in group zero to su to root. */
158 if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0)))
159 for (g = gr->gr_mem;; ++g) {
160 if (!*g) {
161 (void)fprintf(stderr,
162 "su: you are not in the correct group to su %s.\n",
163 user);
164 exit(1);
165 }
166 if (!strcmp(username, *g))
167 break;
168 }
169 /* if target requires a password, verify it */
170 if (*pwd->pw_passwd) {
171 p = getpass("Password:");
172 if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
173 fprintf(stderr, "Sorry\n");
174 syslog(LOG_AUTH|LOG_WARNING,
175 "BAD SU %s to %s%s", username,
176 user, ontty());
177 exit(1);
178 }
179 }
180 }
181 }
182
183 if (asme) {
184 /* if asme and non-standard target shell, must be root */
185 if (!chshell(pwd->pw_shell) && ruid) {
186 (void)fprintf(stderr,
187 "su: permission denied (shell).\n");
188 exit(1);
189 }
190 } else if (pwd->pw_shell && *pwd->pw_shell) {
191 shell = pwd->pw_shell;
192 iscsh = UNSET;
193 } else {
194 shell = _PATH_BSHELL;
195 iscsh = NO;
196 }
197
198 /* if we're forking a csh, we want to slightly muck the args */
199 if (iscsh == UNSET) {
200 if (p = rindex(shell, '/'))
201 avshell = p+1;
202 else
203 avshell = shell;
204 iscsh = strcmp(avshell, "csh") ? NO : YES;
205 }
206
207 /* set permissions */
208 if (setgid(pwd->pw_gid) < 0) {
209 perror("su: setgid");
210 exit(1);
211 }
212 if (initgroups(user, pwd->pw_gid)) {
213 (void)fprintf(stderr, "su: initgroups failed.\n");
214 exit(1);
215 }
216 if (setuid(pwd->pw_uid) < 0) {
217 perror("su: setuid");
218 exit(1);
219 }
220
221 if (!asme) {
222 if (asthem) {
223 p = getenv("TERM");
224 cleanenv[0] = NULL;
225 environ = cleanenv;
226 (void)setenv("PATH", _PATH_DEFPATH, 1);
227 (void)setenv("TERM", p, 1);
228 if (chdir(pwd->pw_dir) < 0) {
229 fprintf(stderr, "su: no directory\n");
230 exit(1);
231 }
232 }
233 if (asthem || pwd->pw_uid)
234 (void)setenv("USER", pwd->pw_name, 1);
235 (void)setenv("HOME", pwd->pw_dir, 1);
236 (void)setenv("SHELL", shell, 1);
237 }
238
239 if (iscsh == YES) {
240 if (fastlogin)
241 *np-- = "-f";
242 if (asme)
243 *np-- = "-m";
244 }
245
246 if (asthem) {
247 avshellbuf[0] = '-';
248 strcpy(avshellbuf+1, avshell);
249 avshell = avshellbuf;
250 } else if (iscsh == YES) {
251 /* csh strips the first character... */
252 avshellbuf[0] = '_';
253 strcpy(avshellbuf+1, avshell);
254 avshell = avshellbuf;
255 }
256
257 *np = avshell;
258
259 if (ruid != 0)
260 syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
261 username, user, ontty());
262
263 (void)setpriority(PRIO_PROCESS, 0, prio);
264
265 execv(shell, np);
266 (void)fprintf(stderr, "su: %s not found.\n", shell);
267 exit(1);
268 }
269
270 int
271 chshell(sh)
272 char *sh;
273 {
274 register char *cp;
275 char *getusershell();
276
277 while ((cp = getusershell()) != NULL)
278 if (!strcmp(cp, sh))
279 return (1);
280 return (0);
281 }
282
283 char *
284 ontty()
285 {
286 char *p, *ttyname();
287 static char buf[MAXPATHLEN + 4];
288
289 buf[0] = 0;
290 if (p = ttyname(STDERR_FILENO))
291 sprintf(buf, " on %s", p);
292 return (buf);
293 }
294
295 #ifdef KERBEROS
296 kerberos(username, user, uid)
297 char *username, *user;
298 int uid;
299 {
300 extern char *krb_err_txt[];
301 KTEXT_ST ticket;
302 AUTH_DAT authdata;
303 struct hostent *hp;
304 register char *p;
305 int kerno;
306 u_long faddr;
307 char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
308 char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
309 char *ontty(), *krb_get_phost();
310
311 if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
312 return (1);
313 if (koktologin(username, lrealm, user) && !uid) {
314 (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
315 return (1);
316 }
317 (void)sprintf(krbtkfile, "%s_%s_%d", TKT_ROOT, user, getuid());
318
319 (void)setenv("KRBTKFILE", krbtkfile, 1);
320 (void)krb_set_tkt_string(krbtkfile);
321 /*
322 * Set real as well as effective ID to 0 for the moment,
323 * to make the kerberos library do the right thing.
324 */
325 if (setuid(0) < 0) {
326 perror("su: setuid");
327 return (1);
328 }
329
330 /*
331 * Little trick here -- if we are su'ing to root,
332 * we need to get a ticket for "xxx.root", where xxx represents
333 * the name of the person su'ing. Otherwise (non-root case),
334 * we need to get a ticket for "yyy.", where yyy represents
335 * the name of the person being su'd to, and the instance is null
336 *
337 * We should have a way to set the ticket lifetime,
338 * with a system default for root.
339 */
340 kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
341 (uid == 0 ? "root" : ""), lrealm,
342 "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
343
344 if (kerno != KSUCCESS) {
345 if (kerno == KDC_PR_UNKNOWN) {
346 fprintf(stderr, "principal unknown: %s.%s@%s\n",
347 (uid == 0 ? username : user),
348 (uid == 0 ? "root" : ""), lrealm);
349 return (1);
350 }
351 (void)fprintf(stderr, "su: unable to su: %s\n",
352 krb_err_txt[kerno]);
353 syslog(LOG_NOTICE|LOG_AUTH,
354 "BAD Kerberos SU: %s to %s%s: %s",
355 username, user, ontty(), krb_err_txt[kerno]);
356 return (1);
357 }
358
359 if (chown(krbtkfile, uid, -1) < 0) {
360 perror("su: chown:");
361 (void)unlink(krbtkfile);
362 return (1);
363 }
364
365 (void)setpriority(PRIO_PROCESS, 0, -2);
366
367 if (gethostname(hostname, sizeof(hostname)) == -1) {
368 perror("su: gethostname");
369 dest_tkt();
370 return (1);
371 }
372
373 (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
374 savehost[sizeof(savehost) - 1] = '\0';
375
376 kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
377
378 if (kerno == KDC_PR_UNKNOWN) {
379 (void)fprintf(stderr, "Warning: TGT not verified.\n");
380 syslog(LOG_NOTICE|LOG_AUTH,
381 "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
382 username, user, ontty(), krb_err_txt[kerno],
383 "rcmd", savehost);
384 } else if (kerno != KSUCCESS) {
385 (void)fprintf(stderr, "Unable to use TGT: %s\n",
386 krb_err_txt[kerno]);
387 syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
388 username, user, ontty(), krb_err_txt[kerno]);
389 dest_tkt();
390 return (1);
391 } else {
392 if (!(hp = gethostbyname(hostname))) {
393 (void)fprintf(stderr, "su: can't get addr of %s\n",
394 hostname);
395 dest_tkt();
396 return (1);
397 }
398 (void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
399
400 if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
401 &authdata, "")) != KSUCCESS) {
402 (void)fprintf(stderr,
403 "su: unable to verify rcmd ticket: %s\n",
404 krb_err_txt[kerno]);
405 syslog(LOG_NOTICE|LOG_AUTH,
406 "failed su: %s to %s%s: %s", username,
407 user, ontty(), krb_err_txt[kerno]);
408 dest_tkt();
409 return (1);
410 }
411 }
412 return (0);
413 }
414
415 koktologin(name, realm, toname)
416 char *name, *realm, *toname;
417 {
418 register AUTH_DAT *kdata;
419 AUTH_DAT kdata_st;
420
421 kdata = &kdata_st;
422 bzero((caddr_t) kdata, sizeof(*kdata));
423 (void)strcpy(kdata->pname, name);
424 (void)strcpy(kdata->pinst,
425 ((strcmp(toname, "root") == 0) ? "root" : ""));
426 (void)strcpy(kdata->prealm, realm);
427 return (kuserok(kdata, toname));
428 }
429 #endif
430