Annotation of src/usr.bin/su/su.c, Revision 1.5
1.5 ! deraadt 1: /* $OpenBSD: su.c,v 1.4 1996/06/26 05:39:34 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.5 ! deraadt 44: static char rcsid[] = "$OpenBSD: su.c,v 1.4 1996/06/26 05:39:34 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);
244: (void)setenv("TERM", p, 1);
1.5 ! deraadt 245:
! 246: seteuid(pwd->pw_uid);
! 247: setegid(pwd->pw_gid);
1.1 deraadt 248: if (chdir(pwd->pw_dir) < 0) {
249: fprintf(stderr, "su: no directory\n");
250: exit(1);
251: }
1.5 ! deraadt 252: seteuid(0);
! 253: setegid(0); /* XXX use a saved gid instead? */
1.1 deraadt 254: }
255: if (asthem || pwd->pw_uid)
256: (void)setenv("USER", pwd->pw_name, 1);
257: (void)setenv("HOME", pwd->pw_dir, 1);
258: (void)setenv("SHELL", shell, 1);
259: }
260:
261: if (iscsh == YES) {
262: if (fastlogin)
263: *np-- = "-f";
264: if (asme)
265: *np-- = "-m";
266: }
267:
268: if (asthem) {
269: avshellbuf[0] = '-';
270: strcpy(avshellbuf+1, avshell);
271: avshell = avshellbuf;
272: } else if (iscsh == YES) {
273: /* csh strips the first character... */
274: avshellbuf[0] = '_';
275: strcpy(avshellbuf+1, avshell);
276: avshell = avshellbuf;
277: }
278:
279: *np = avshell;
280:
281: if (ruid != 0)
282: syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
283: username, user, ontty());
284:
285: (void)setpriority(PRIO_PROCESS, 0, prio);
286:
287: execv(shell, np);
288: (void)fprintf(stderr, "su: %s not found.\n", shell);
289: exit(1);
290: }
291:
292: int
293: chshell(sh)
294: char *sh;
295: {
296: register char *cp;
297: char *getusershell();
298:
299: while ((cp = getusershell()) != NULL)
300: if (!strcmp(cp, sh))
301: return (1);
302: return (0);
303: }
304:
305: char *
306: ontty()
307: {
308: char *p, *ttyname();
309: static char buf[MAXPATHLEN + 4];
310:
311: buf[0] = 0;
312: if (p = ttyname(STDERR_FILENO))
313: sprintf(buf, " on %s", p);
314: return (buf);
315: }
316:
317: #ifdef KERBEROS
318: kerberos(username, user, uid)
319: char *username, *user;
320: int uid;
321: {
322: KTEXT_ST ticket;
323: AUTH_DAT authdata;
324: struct hostent *hp;
325: register char *p;
326: int kerno;
327: u_long faddr;
328: char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
329: char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
330: char *ontty(), *krb_get_phost();
331:
332: if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
333: return (1);
334: if (koktologin(username, lrealm, user) && !uid) {
335: (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
336: return (1);
337: }
338: (void)sprintf(krbtkfile, "%s_%s_%d", TKT_ROOT, user, getuid());
339:
340: (void)setenv("KRBTKFILE", krbtkfile, 1);
341: (void)krb_set_tkt_string(krbtkfile);
342: /*
343: * Set real as well as effective ID to 0 for the moment,
344: * to make the kerberos library do the right thing.
345: */
346: if (setuid(0) < 0) {
347: perror("su: setuid");
348: return (1);
349: }
350:
351: /*
352: * Little trick here -- if we are su'ing to root,
353: * we need to get a ticket for "xxx.root", where xxx represents
354: * the name of the person su'ing. Otherwise (non-root case),
355: * we need to get a ticket for "yyy.", where yyy represents
356: * the name of the person being su'd to, and the instance is null
357: *
358: * We should have a way to set the ticket lifetime,
359: * with a system default for root.
360: */
361: kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
362: (uid == 0 ? "root" : ""), lrealm,
363: "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
364:
365: if (kerno != KSUCCESS) {
366: if (kerno == KDC_PR_UNKNOWN) {
367: fprintf(stderr, "principal unknown: %s.%s@%s\n",
368: (uid == 0 ? username : user),
369: (uid == 0 ? "root" : ""), lrealm);
370: return (1);
371: }
372: (void)fprintf(stderr, "su: unable to su: %s\n",
373: krb_err_txt[kerno]);
374: syslog(LOG_NOTICE|LOG_AUTH,
375: "BAD Kerberos SU: %s to %s%s: %s",
376: username, user, ontty(), krb_err_txt[kerno]);
377: return (1);
378: }
379:
380: if (chown(krbtkfile, uid, -1) < 0) {
381: perror("su: chown:");
382: (void)unlink(krbtkfile);
383: return (1);
384: }
385:
386: (void)setpriority(PRIO_PROCESS, 0, -2);
387:
388: if (gethostname(hostname, sizeof(hostname)) == -1) {
389: perror("su: gethostname");
390: dest_tkt();
391: return (1);
392: }
393:
394: (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
395: savehost[sizeof(savehost) - 1] = '\0';
396:
397: kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
398:
399: if (kerno == KDC_PR_UNKNOWN) {
400: (void)fprintf(stderr, "Warning: TGT not verified.\n");
401: syslog(LOG_NOTICE|LOG_AUTH,
402: "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
403: username, user, ontty(), krb_err_txt[kerno],
404: "rcmd", savehost);
405: } else if (kerno != KSUCCESS) {
406: (void)fprintf(stderr, "Unable to use TGT: %s\n",
407: krb_err_txt[kerno]);
408: syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
409: username, user, ontty(), krb_err_txt[kerno]);
410: dest_tkt();
411: return (1);
412: } else {
413: if (!(hp = gethostbyname(hostname))) {
414: (void)fprintf(stderr, "su: can't get addr of %s\n",
415: hostname);
416: dest_tkt();
417: return (1);
418: }
419: (void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
420:
421: if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
422: &authdata, "")) != KSUCCESS) {
423: (void)fprintf(stderr,
424: "su: unable to verify rcmd ticket: %s\n",
425: krb_err_txt[kerno]);
426: syslog(LOG_NOTICE|LOG_AUTH,
427: "failed su: %s to %s%s: %s", username,
428: user, ontty(), krb_err_txt[kerno]);
429: dest_tkt();
430: return (1);
431: }
432: }
433: return (0);
434: }
435:
436: koktologin(name, realm, toname)
437: char *name, *realm, *toname;
438: {
439: register AUTH_DAT *kdata;
440: AUTH_DAT kdata_st;
441:
442: kdata = &kdata_st;
443: bzero((caddr_t) kdata, sizeof(*kdata));
444: (void)strcpy(kdata->pname, name);
445: (void)strcpy(kdata->pinst,
446: ((strcmp(toname, "root") == 0) ? "root" : ""));
447: (void)strcpy(kdata->prealm, realm);
448: return (kuserok(kdata, toname));
449: }
450: #endif