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