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