Home | History | Annotate | Line # | Download | only in su
su.c revision 1.64
      1  1.64  christos /*	$NetBSD: su.c,v 1.64 2005/01/10 03:11:50 christos Exp $	*/
      2  1.12  christos 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1988 The Regents of the University of California.
      5   1.1       cgd  * All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.56       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.19     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.19     lukem __COPYRIGHT(
     35  1.19     lukem     "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
     36  1.19     lukem  All rights reserved.\n");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40  1.12  christos #if 0
     41  1.13       tls static char sccsid[] = "@(#)su.c	8.3 (Berkeley) 4/2/94";*/
     42  1.12  christos #else
     43  1.64  christos __RCSID("$NetBSD: su.c,v 1.64 2005/01/10 03:11:50 christos Exp $");
     44  1.12  christos #endif
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47   1.1       cgd #include <sys/param.h>
     48   1.1       cgd #include <sys/time.h>
     49  1.63      manu #include <sys/resource.h>
     50  1.13       tls #include <err.h>
     51  1.11  christos #include <errno.h>
     52  1.17     lukem #include <grp.h>
     53  1.17     lukem #include <paths.h>
     54  1.17     lukem #include <pwd.h>
     55  1.17     lukem #include <stdio.h>
     56  1.27  wsanchez #ifdef SKEY
     57  1.17     lukem #include <skey.h>
     58  1.27  wsanchez #endif
     59   1.7       jtc #include <stdlib.h>
     60   1.1       cgd #include <string.h>
     61  1.17     lukem #include <syslog.h>
     62  1.21    kleink #include <time.h>
     63  1.17     lukem #include <tzfile.h>
     64   1.1       cgd #include <unistd.h>
     65   1.1       cgd 
     66  1.37       mjl #ifdef LOGIN_CAP
     67  1.37       mjl #include <login_cap.h>
     68  1.37       mjl #endif
     69  1.37       mjl 
     70  1.64  christos #ifdef KERBEROS
     71  1.41     assar #include <des.h>
     72  1.41     assar #include <krb.h>
     73   1.1       cgd #include <netdb.h>
     74   1.1       cgd 
     75  1.64  christos static int kerberos(char *, char *, int);
     76  1.64  christos static int koktologin(char *, char *, char *);
     77  1.41     assar 
     78  1.41     assar #endif
     79  1.41     assar 
     80  1.64  christos #ifdef KERBEROS5
     81  1.41     assar #include <krb5.h>
     82  1.41     assar 
     83  1.64  christos static int kerberos5(char *, char *, int);
     84  1.41     assar 
     85  1.41     assar #endif
     86  1.41     assar 
     87  1.41     assar #if defined(KERBEROS) || defined(KERBEROS5)
     88  1.41     assar 
     89  1.54      jmmv #define	ARGSTRX	"-Kdflm"
     90   1.1       cgd 
     91   1.1       cgd int use_kerberos = 1;
     92  1.11  christos 
     93   1.1       cgd #else
     94  1.54      jmmv #define	ARGSTRX	"-dflm"
     95   1.1       cgd #endif
     96   1.1       cgd 
     97  1.57  christos #ifndef	SU_GROUP
     98  1.57  christos #define	SU_GROUP	"wheel"
     99  1.18     lukem #endif
    100  1.18     lukem 
    101  1.37       mjl #ifdef LOGIN_CAP
    102  1.37       mjl #define ARGSTR	ARGSTRX "c:"
    103  1.37       mjl #else
    104  1.37       mjl #define ARGSTR ARGSTRX
    105  1.37       mjl #endif
    106  1.37       mjl 
    107  1.64  christos static int chshell(const char *);
    108  1.64  christos static char *ontty(void);
    109  1.64  christos static int check_ingroup(int, const char *, const char *, int);
    110  1.11  christos 
    111  1.64  christos int main(int, char **);
    112   1.2       sef 
    113   1.7       jtc int
    114  1.64  christos main(int argc, char **argv)
    115   1.1       cgd {
    116   1.1       cgd 	extern char **environ;
    117  1.13       tls 	struct passwd *pwd;
    118  1.17     lukem 	char *p;
    119  1.28  christos #ifdef BSD4_4
    120  1.17     lukem 	struct timeval tp;
    121  1.28  christos #endif
    122  1.11  christos 	uid_t ruid;
    123  1.54      jmmv 	int asme, ch, asthem, fastlogin, prio, gohome;
    124   1.1       cgd 	enum { UNSET, YES, NO } iscsh = UNSET;
    125  1.35  christos 	char *user, *shell, *avshell, *username, **np;
    126  1.37       mjl 	char *userpass, *class;
    127  1.24       mrg 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
    128  1.37       mjl 	time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
    129  1.37       mjl #ifdef LOGIN_CAP
    130  1.37       mjl 	login_cap_t *lc;
    131  1.37       mjl #endif
    132   1.1       cgd 
    133   1.1       cgd 	asme = asthem = fastlogin = 0;
    134  1.54      jmmv 	gohome = 1;
    135  1.37       mjl 	shell = class = NULL;
    136  1.19     lukem 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    137   1.1       cgd 		switch((char)ch) {
    138  1.41     assar #if defined(KERBEROS) || defined(KERBEROS5)
    139   1.1       cgd 		case 'K':
    140   1.1       cgd 			use_kerberos = 0;
    141   1.1       cgd 			break;
    142   1.1       cgd #endif
    143  1.37       mjl #ifdef LOGIN_CAP
    144  1.37       mjl 		case 'c':
    145  1.37       mjl 			class = optarg;
    146  1.37       mjl 			break;
    147  1.37       mjl #endif
    148  1.54      jmmv 		case 'd':
    149  1.54      jmmv 			asme = 0;
    150  1.54      jmmv 			asthem = 1;
    151  1.54      jmmv 			gohome = 0;
    152  1.54      jmmv 			break;
    153   1.1       cgd 		case 'f':
    154   1.1       cgd 			fastlogin = 1;
    155   1.1       cgd 			break;
    156   1.1       cgd 		case '-':
    157   1.1       cgd 		case 'l':
    158   1.1       cgd 			asme = 0;
    159   1.1       cgd 			asthem = 1;
    160   1.1       cgd 			break;
    161   1.1       cgd 		case 'm':
    162   1.1       cgd 			asme = 1;
    163   1.1       cgd 			asthem = 0;
    164   1.1       cgd 			break;
    165   1.1       cgd 		case '?':
    166   1.1       cgd 		default:
    167  1.11  christos 			(void)fprintf(stderr,
    168  1.58      jmmv 			    "usage: %s [%s] [login [shell arguments]]\n",
    169  1.47       cgd 			    getprogname(), ARGSTR);
    170   1.1       cgd 			exit(1);
    171   1.1       cgd 		}
    172   1.1       cgd 	argv += optind;
    173  1.30  christos 
    174  1.44       erh 	/* Lower the priority so su runs faster */
    175   1.1       cgd 	errno = 0;
    176   1.1       cgd 	prio = getpriority(PRIO_PROCESS, 0);
    177   1.1       cgd 	if (errno)
    178   1.1       cgd 		prio = 0;
    179  1.44       erh 	if (prio > -2)
    180  1.44       erh 		(void)setpriority(PRIO_PROCESS, 0, -2);
    181  1.45     lukem 	openlog("su", 0, LOG_AUTH);
    182   1.1       cgd 
    183   1.1       cgd 	/* get current login name and shell */
    184   1.1       cgd 	ruid = getuid();
    185   1.1       cgd 	username = getlogin();
    186   1.1       cgd 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    187   1.1       cgd 	    pwd->pw_uid != ruid)
    188   1.1       cgd 		pwd = getpwuid(ruid);
    189  1.23       mrg 	if (pwd == NULL)
    190  1.63      manu 		errx(1, "who are you?");
    191   1.1       cgd 	username = strdup(pwd->pw_name);
    192  1.30  christos 	userpass = strdup(pwd->pw_passwd);
    193  1.30  christos 	if (username == NULL || userpass == NULL)
    194  1.63      manu 		err(1, "strdup");
    195   1.1       cgd 
    196  1.37       mjl 
    197  1.26      ross 	if (asme) {
    198  1.24       mrg 		if (pwd->pw_shell && *pwd->pw_shell) {
    199  1.50    itojun 			strlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
    200  1.50    itojun 			shell = shellbuf;
    201  1.24       mrg 		} else {
    202  1.24       mrg 			shell = _PATH_BSHELL;
    203  1.24       mrg 			iscsh = NO;
    204  1.24       mrg 		}
    205  1.26      ross 	}
    206  1.63      manu 	/* get target login information, default to root */
    207  1.63      manu 	user = *argv ? *argv : "root";
    208   1.2       sef 	np = *argv ? argv : argv-1;
    209   1.2       sef 
    210  1.23       mrg 	if ((pwd = getpwnam(user)) == NULL)
    211  1.63      manu 		errx(1, "unknown login %s", user);
    212  1.37       mjl 
    213  1.37       mjl #ifdef LOGIN_CAP
    214  1.37       mjl 	/* force the usage of specified class */
    215  1.37       mjl 	if (class) {
    216  1.37       mjl 		if (ruid)
    217  1.63      manu 			errx(1, "Only root may use -c");
    218  1.37       mjl 
    219  1.37       mjl 		pwd->pw_class = class;
    220  1.37       mjl 	}
    221  1.63      manu 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
    222  1.63      manu 		errx(1, "Unknown class %s\n", pwd->pw_class);
    223  1.37       mjl 
    224  1.37       mjl 	pw_warntime = login_getcaptime(lc, "password-warn",
    225  1.37       mjl                                     _PASSWORD_WARNDAYS * SECSPERDAY,
    226  1.37       mjl                                     _PASSWORD_WARNDAYS * SECSPERDAY);
    227  1.37       mjl #endif
    228  1.37       mjl 
    229  1.24       mrg 	if (ruid
    230  1.41     assar #ifdef KERBEROS5
    231  1.41     assar 	    && (!use_kerberos || kerberos5(username, user, pwd->pw_uid))
    232  1.41     assar #endif
    233   1.1       cgd #ifdef KERBEROS
    234  1.24       mrg 	    && (!use_kerberos || kerberos(username, user, pwd->pw_uid))
    235   1.1       cgd #endif
    236  1.24       mrg 	    ) {
    237  1.30  christos 		char *pass = pwd->pw_passwd;
    238  1.30  christos 		int ok = pwd->pw_uid != 0;
    239  1.30  christos 
    240  1.57  christos #ifdef SU_ROOTAUTH
    241  1.34       kim 		/*
    242  1.34       kim 		 * Allow those in group rootauth to su to root, by supplying
    243  1.34       kim 		 * their own password.
    244  1.34       kim 		 */
    245  1.46       sjg 		if (!ok) {
    246  1.57  christos 			if ((ok = check_ingroup(-1, SU_ROOTAUTH, username, 0))) {
    247  1.46       sjg 				pass = userpass;
    248  1.46       sjg 				user = username;
    249  1.34       kim 			}
    250  1.46       sjg 		}
    251  1.34       kim #endif
    252  1.24       mrg 		/*
    253  1.57  christos 		 * Only allow those in group SU_GROUP to su to root,
    254  1.24       mrg 		 * but only if that group has any members.
    255  1.57  christos 		 * If SU_GROUP has no members, allow anyone to su root
    256  1.24       mrg 		 */
    257  1.33       abs 		if (!ok) {
    258  1.57  christos 			ok = check_ingroup(-1, SU_GROUP, username, 1);
    259   1.1       cgd 		}
    260  1.30  christos 		if (!ok)
    261  1.63      manu 			errx(1,
    262  1.30  christos 	    "you are not listed in the correct secondary group (%s) to su %s.",
    263  1.63      manu 					    SU_GROUP, user);
    264   1.1       cgd 		/* if target requires a password, verify it */
    265  1.30  christos 		if (*pass) {
    266   1.1       cgd 			p = getpass("Password:");
    267  1.10   deraadt #ifdef SKEY
    268  1.10   deraadt 			if (strcasecmp(p, "s/key") == 0) {
    269  1.63      manu 				if (skey_haskey(user))
    270  1.63      manu 					errx(1, "Sorry, you have no s/key.");
    271  1.63      manu 				else {
    272  1.10   deraadt 					if (skey_authenticate(user)) {
    273  1.10   deraadt 						goto badlogin;
    274  1.10   deraadt 					}
    275  1.10   deraadt 				}
    276  1.10   deraadt 
    277  1.10   deraadt 			} else
    278  1.10   deraadt #endif
    279  1.30  christos 			if (strcmp(pass, crypt(p, pass))) {
    280  1.31  christos #ifdef SKEY
    281  1.10   deraadt badlogin:
    282  1.27  wsanchez #endif
    283   1.1       cgd 				fprintf(stderr, "Sorry\n");
    284  1.45     lukem 				syslog(LOG_WARNING,
    285   1.1       cgd 					"BAD SU %s to %s%s", username,
    286  1.30  christos 					pwd->pw_name, ontty());
    287   1.1       cgd 				exit(1);
    288   1.1       cgd 			}
    289   1.1       cgd 		}
    290   1.1       cgd 	}
    291   1.1       cgd 
    292   1.1       cgd 	if (asme) {
    293   1.1       cgd 		/* if asme and non-standard target shell, must be root */
    294  1.13       tls 		if (!chshell(pwd->pw_shell) && ruid)
    295  1.63      manu 			errx(1,"permission denied (shell).");
    296   1.1       cgd 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    297   1.1       cgd 		shell = pwd->pw_shell;
    298   1.1       cgd 		iscsh = UNSET;
    299   1.1       cgd 	} else {
    300   1.1       cgd 		shell = _PATH_BSHELL;
    301   1.1       cgd 		iscsh = NO;
    302   1.1       cgd 	}
    303   1.1       cgd 
    304  1.17     lukem 	if ((p = strrchr(shell, '/')) != NULL)
    305   1.9       cgd 		avshell = p+1;
    306   1.9       cgd 	else
    307   1.9       cgd 		avshell = shell;
    308   1.9       cgd 
    309   1.1       cgd 	/* if we're forking a csh, we want to slightly muck the args */
    310   1.9       cgd 	if (iscsh == UNSET)
    311  1.11  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    312   1.1       cgd 
    313   1.1       cgd 	/* set permissions */
    314  1.44       erh #ifdef LOGIN_CAP
    315  1.44       erh 	if (setusercontext(lc, pwd, pwd->pw_uid,
    316  1.44       erh 	    (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
    317  1.44       erh 	    LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER))
    318  1.44       erh 		err(1, "setting user context");
    319  1.44       erh #else
    320  1.13       tls 	if (setgid(pwd->pw_gid) < 0)
    321  1.13       tls 		err(1, "setgid");
    322  1.13       tls 	if (initgroups(user, pwd->pw_gid))
    323  1.13       tls 		errx(1, "initgroups failed");
    324  1.13       tls 	if (setuid(pwd->pw_uid) < 0)
    325  1.13       tls 		err(1, "setuid");
    326  1.37       mjl #endif
    327   1.1       cgd 
    328   1.1       cgd 	if (!asme) {
    329   1.1       cgd 		if (asthem) {
    330   1.1       cgd 			p = getenv("TERM");
    331  1.35  christos 			/* Create an empty environment */
    332  1.35  christos 			if ((environ = malloc(sizeof(char *))) == NULL)
    333  1.48    simonb 				err(1, NULL);
    334  1.35  christos 			environ[0] = NULL;
    335  1.37       mjl #ifdef LOGIN_CAP
    336  1.44       erh 			if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
    337  1.37       mjl 				err(1, "setting user context");
    338  1.37       mjl #else
    339   1.8   mycroft 			(void)setenv("PATH", _PATH_DEFPATH, 1);
    340  1.37       mjl #endif
    341  1.11  christos 			if (p)
    342  1.11  christos 				(void)setenv("TERM", p, 1);
    343  1.54      jmmv 			if (gohome && chdir(pwd->pw_dir) < 0)
    344  1.13       tls 				errx(1, "no directory");
    345  1.37       mjl 		}
    346  1.38       mjl 
    347   1.5       jtc 		if (asthem || pwd->pw_uid)
    348   1.1       cgd 			(void)setenv("USER", pwd->pw_name, 1);
    349   1.1       cgd 		(void)setenv("HOME", pwd->pw_dir, 1);
    350   1.1       cgd 		(void)setenv("SHELL", shell, 1);
    351   1.1       cgd 	}
    352  1.39       abs 	(void)setenv("SU_FROM", username, 1);
    353   1.1       cgd 
    354  1.12  christos 	if (iscsh == YES) {
    355  1.12  christos 		if (fastlogin)
    356  1.12  christos 			*np-- = "-f";
    357  1.12  christos 		if (asme)
    358  1.12  christos 			*np-- = "-m";
    359  1.52  christos 	} else {
    360  1.53   mycroft 		if (fastlogin)
    361  1.53   mycroft 			unsetenv("ENV");
    362  1.12  christos 	}
    363   1.1       cgd 
    364   1.8   mycroft 	if (asthem) {
    365   1.8   mycroft 		avshellbuf[0] = '-';
    366  1.50    itojun 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    367   1.8   mycroft 		avshell = avshellbuf;
    368   1.8   mycroft 	} else if (iscsh == YES) {
    369   1.8   mycroft 		/* csh strips the first character... */
    370   1.8   mycroft 		avshellbuf[0] = '_';
    371  1.50    itojun 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    372   1.8   mycroft 		avshell = avshellbuf;
    373   1.8   mycroft 	}
    374  1.12  christos 	*np = avshell;
    375  1.11  christos 
    376  1.28  christos #ifdef BSD4_4
    377  1.17     lukem 	if (pwd->pw_change || pwd->pw_expire)
    378  1.17     lukem 		(void)gettimeofday(&tp, (struct timezone *)NULL);
    379  1.26      ross 	if (pwd->pw_change) {
    380  1.17     lukem 		if (tp.tv_sec >= pwd->pw_change) {
    381  1.17     lukem 			(void)printf("%s -- %s's password has expired.\n",
    382  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    383  1.17     lukem 			if (ruid != 0)
    384  1.17     lukem 				exit(1);
    385  1.37       mjl 		} else if (pwd->pw_change - tp.tv_sec < pw_warntime)
    386  1.17     lukem 			(void)printf("Warning: %s's password expires on %s",
    387  1.17     lukem 				     user, ctime(&pwd->pw_change));
    388  1.26      ross 	}
    389  1.26      ross 	if (pwd->pw_expire) {
    390  1.17     lukem 		if (tp.tv_sec >= pwd->pw_expire) {
    391  1.17     lukem 			(void)printf("%s -- %s's account has expired.\n",
    392  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    393  1.17     lukem 			if (ruid != 0)
    394  1.17     lukem 				exit(1);
    395  1.17     lukem 		} else if (pwd->pw_expire - tp.tv_sec <
    396  1.17     lukem 		    _PASSWORD_WARNDAYS * SECSPERDAY)
    397  1.17     lukem 			(void)printf("Warning: %s's account expires on %s",
    398  1.17     lukem 				     user, ctime(&pwd->pw_expire));
    399  1.26      ross  	}
    400  1.28  christos #endif
    401   1.1       cgd 	if (ruid != 0)
    402  1.45     lukem 		syslog(LOG_NOTICE, "%s to %s%s",
    403  1.30  christos 		    username, pwd->pw_name, ontty());
    404   1.1       cgd 
    405  1.44       erh 	/* Raise our priority back to what we had before */
    406   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, prio);
    407   1.1       cgd 
    408  1.12  christos 	execv(shell, np);
    409  1.13       tls 	err(1, "%s", shell);
    410  1.27  wsanchez         /* NOTREACHED */
    411   1.1       cgd }
    412  1.11  christos 
    413  1.63      manu 
    414  1.11  christos static int
    415  1.64  christos chshell(const char *sh)
    416   1.1       cgd {
    417  1.25   mycroft 	const char *cp;
    418   1.1       cgd 
    419  1.55       jrf 	setusershell();
    420   1.1       cgd 	while ((cp = getusershell()) != NULL)
    421   1.1       cgd 		if (!strcmp(cp, sh))
    422  1.64  christos 			return 1;
    423  1.64  christos 	return 0;
    424   1.1       cgd }
    425   1.1       cgd 
    426  1.11  christos static char *
    427  1.64  christos ontty(void)
    428   1.1       cgd {
    429  1.19     lukem 	char *p;
    430   1.1       cgd 	static char buf[MAXPATHLEN + 4];
    431   1.1       cgd 
    432   1.1       cgd 	buf[0] = 0;
    433  1.17     lukem 	if ((p = ttyname(STDERR_FILENO)) != NULL)
    434  1.15       mrg 		(void)snprintf(buf, sizeof buf, " on %s", p);
    435  1.64  christos 	return buf;
    436   1.1       cgd }
    437  1.41     assar 
    438  1.41     assar #ifdef KERBEROS5
    439  1.41     assar static int
    440  1.64  christos kerberos5(char *username, char *user, int uid)
    441  1.41     assar {
    442  1.41     assar 	krb5_error_code ret;
    443  1.41     assar 	krb5_context context;
    444  1.41     assar 	krb5_principal princ = NULL;
    445  1.41     assar 	krb5_ccache ccache, ccache2;
    446  1.41     assar 	char *cc_name;
    447  1.43     assar 	const char *filename;
    448  1.41     assar 
    449  1.41     assar 	ret = krb5_init_context(&context);
    450  1.41     assar 	if (ret)
    451  1.41     assar 		return (1);
    452  1.41     assar 
    453  1.41     assar 	if (strcmp (user, "root") == 0)
    454  1.41     assar 		ret = krb5_make_principal(context, &princ,
    455  1.41     assar 					  NULL, username, "root", NULL);
    456  1.41     assar 	else
    457  1.41     assar 		ret = krb5_make_principal(context, &princ,
    458  1.42     assar 					  NULL, user, NULL);
    459  1.41     assar 	if (ret)
    460  1.41     assar 		goto fail;
    461  1.41     assar 	if (!krb5_kuserok(context, princ, user) && !uid) {
    462  1.41     assar 		warnx ("kerberos5: not in %s's ACL.", user);
    463  1.41     assar 		goto fail;
    464  1.41     assar 	}
    465  1.41     assar 	ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache);
    466  1.41     assar 	if (ret)
    467  1.41     assar 		goto fail;
    468  1.41     assar 	ret = krb5_verify_user_lrealm(context, princ, ccache, NULL, TRUE,
    469  1.41     assar 				      NULL);
    470  1.41     assar 	if (ret) {
    471  1.41     assar 		krb5_cc_destroy(context, ccache);
    472  1.41     assar 		switch (ret) {
    473  1.41     assar 		case KRB5_LIBOS_PWDINTR :
    474  1.41     assar 			break;
    475  1.41     assar 		case KRB5KRB_AP_ERR_BAD_INTEGRITY:
    476  1.41     assar 		case KRB5KRB_AP_ERR_MODIFIED:
    477  1.41     assar 			krb5_warnx(context, "Password incorrect");
    478  1.41     assar 			break;
    479  1.41     assar 		default :
    480  1.41     assar 			krb5_warn(context, ret, "krb5_verify_user");
    481  1.41     assar 			break;
    482  1.41     assar 		}
    483  1.41     assar 		goto fail;
    484  1.41     assar 	}
    485  1.41     assar 	ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache2);
    486  1.41     assar 	if (ret) {
    487  1.41     assar 		krb5_cc_destroy(context, ccache);
    488  1.41     assar 		goto fail;
    489  1.41     assar 	}
    490  1.41     assar 	ret = krb5_cc_copy_cache(context, ccache, ccache2);
    491  1.41     assar 	if (ret) {
    492  1.41     assar 		krb5_cc_destroy(context, ccache);
    493  1.41     assar 		krb5_cc_destroy(context, ccache2);
    494  1.41     assar 		goto fail;
    495  1.41     assar 	}
    496  1.41     assar 
    497  1.43     assar 	filename = krb5_cc_get_name(context, ccache2);
    498  1.41     assar 	asprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
    499  1.43     assar 		 filename);
    500  1.43     assar 	if (chown (filename, uid, -1) < 0) {
    501  1.43     assar 		warn("chown %s", filename);
    502  1.43     assar 		free(cc_name);
    503  1.43     assar 		krb5_cc_destroy(context, ccache);
    504  1.43     assar 		krb5_cc_destroy(context, ccache2);
    505  1.43     assar 		goto fail;
    506  1.43     assar 	}
    507  1.43     assar 
    508  1.41     assar 	setenv("KRB5CCNAME", cc_name, 1);
    509  1.41     assar 	free(cc_name);
    510  1.41     assar 	krb5_cc_close(context, ccache2);
    511  1.41     assar 	krb5_cc_destroy(context, ccache);
    512  1.43     assar 	return (0);
    513  1.41     assar 
    514  1.41     assar  fail:
    515  1.41     assar 	if (princ != NULL)
    516  1.41     assar 		krb5_free_principal (context, princ);
    517  1.41     assar 	krb5_free_context (context);
    518  1.41     assar 	return (1);
    519  1.41     assar }
    520  1.41     assar #endif
    521   1.1       cgd 
    522   1.1       cgd #ifdef KERBEROS
    523  1.11  christos static int
    524  1.64  christos kerberos(char *username, char *user, int uid)
    525   1.1       cgd {
    526   1.1       cgd 	KTEXT_ST ticket;
    527   1.1       cgd 	AUTH_DAT authdata;
    528   1.1       cgd 	struct hostent *hp;
    529   1.1       cgd 	int kerno;
    530   1.1       cgd 	u_long faddr;
    531   1.1       cgd 	char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
    532  1.22       mrg 	char hostname[MAXHOSTNAMELEN + 1], savehost[MAXHOSTNAMELEN + 1];
    533   1.1       cgd 
    534  1.40     assar 	if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
    535   1.1       cgd 		return (1);
    536   1.1       cgd 	if (koktologin(username, lrealm, user) && !uid) {
    537  1.13       tls 		warnx("kerberos: not in %s's ACL.", user);
    538   1.1       cgd 		return (1);
    539   1.1       cgd 	}
    540  1.29    scottr 	(void)snprintf(krbtkfile, sizeof krbtkfile, "%s_%s_%d", TKT_ROOT,
    541  1.15       mrg 	    user, getuid());
    542   1.1       cgd 
    543   1.1       cgd 	(void)setenv("KRBTKFILE", krbtkfile, 1);
    544   1.1       cgd 	(void)krb_set_tkt_string(krbtkfile);
    545   1.1       cgd 	/*
    546   1.1       cgd 	 * Set real as well as effective ID to 0 for the moment,
    547   1.1       cgd 	 * to make the kerberos library do the right thing.
    548   1.1       cgd 	 */
    549   1.1       cgd 	if (setuid(0) < 0) {
    550  1.13       tls 		warn("setuid");
    551   1.1       cgd 		return (1);
    552   1.1       cgd 	}
    553   1.1       cgd 
    554   1.1       cgd 	/*
    555   1.1       cgd 	 * Little trick here -- if we are su'ing to root,
    556   1.1       cgd 	 * we need to get a ticket for "xxx.root", where xxx represents
    557   1.1       cgd 	 * the name of the person su'ing.  Otherwise (non-root case),
    558   1.1       cgd 	 * we need to get a ticket for "yyy.", where yyy represents
    559   1.1       cgd 	 * the name of the person being su'd to, and the instance is null
    560   1.1       cgd 	 *
    561   1.1       cgd 	 * We should have a way to set the ticket lifetime,
    562   1.1       cgd 	 * with a system default for root.
    563   1.1       cgd 	 */
    564  1.40     assar 	{
    565  1.40     assar 		char prompt[128];
    566  1.40     assar 		char passw[256];
    567  1.40     assar 
    568  1.40     assar 		(void)snprintf (prompt, sizeof(prompt),
    569  1.40     assar 			  "%s's Password: ",
    570  1.40     assar 			  krb_unparse_name_long ((uid == 0 ? username : user),
    571  1.40     assar 						 (uid == 0 ? "root" : ""),
    572  1.40     assar 						 lrealm));
    573  1.40     assar 		if (des_read_pw_string (passw, sizeof (passw), prompt, 0)) {
    574  1.40     assar 			memset (passw, 0, sizeof (passw));
    575  1.40     assar 			return (1);
    576  1.40     assar 		}
    577  1.40     assar 		if (strlen(passw) == 0)
    578  1.40     assar 			return (1); /* Empty passwords are not allowed */
    579  1.40     assar 
    580  1.40     assar 		kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
    581  1.40     assar 					  (uid == 0 ? "root" : ""), lrealm,
    582  1.40     assar 					  KRB_TICKET_GRANTING_TICKET,
    583  1.40     assar 					  lrealm,
    584  1.40     assar 					  DEFAULT_TKT_LIFE,
    585  1.40     assar 					  passw);
    586  1.40     assar 		memset (passw, 0, strlen (passw));
    587  1.40     assar 	}
    588   1.1       cgd 
    589   1.1       cgd 	if (kerno != KSUCCESS) {
    590   1.1       cgd 		if (kerno == KDC_PR_UNKNOWN) {
    591  1.13       tls 			warnx("kerberos: principal unknown: %s.%s@%s",
    592   1.1       cgd 				(uid == 0 ? username : user),
    593   1.1       cgd 				(uid == 0 ? "root" : ""), lrealm);
    594   1.1       cgd 			return (1);
    595   1.1       cgd 		}
    596  1.13       tls 		warnx("kerberos: unable to su: %s", krb_err_txt[kerno]);
    597  1.45     lukem 		syslog(LOG_WARNING,
    598   1.1       cgd 		    "BAD Kerberos SU: %s to %s%s: %s",
    599   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    600   1.1       cgd 		return (1);
    601   1.1       cgd 	}
    602   1.1       cgd 
    603   1.1       cgd 	if (chown(krbtkfile, uid, -1) < 0) {
    604  1.13       tls 		warn("chown");
    605   1.1       cgd 		(void)unlink(krbtkfile);
    606   1.1       cgd 		return (1);
    607   1.1       cgd 	}
    608   1.1       cgd 
    609   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, -2);
    610   1.1       cgd 
    611   1.1       cgd 	if (gethostname(hostname, sizeof(hostname)) == -1) {
    612  1.13       tls 		warn("gethostname");
    613   1.1       cgd 		dest_tkt();
    614   1.1       cgd 		return (1);
    615   1.1       cgd 	}
    616  1.22       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    617   1.1       cgd 
    618  1.40     assar 	(void)strlcpy(savehost, krb_get_phost(hostname), sizeof(savehost));
    619   1.1       cgd 	savehost[sizeof(savehost) - 1] = '\0';
    620   1.1       cgd 
    621   1.1       cgd 	kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
    622   1.1       cgd 
    623   1.1       cgd 	if (kerno == KDC_PR_UNKNOWN) {
    624  1.13       tls 		warnx("Warning: TGT not verified.");
    625  1.45     lukem 		syslog(LOG_WARNING,
    626   1.1       cgd 		    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
    627   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno],
    628   1.1       cgd 		    "rcmd", savehost);
    629   1.1       cgd 	} else if (kerno != KSUCCESS) {
    630  1.13       tls 		warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
    631  1.45     lukem 		syslog(LOG_WARNING, "failed su: %s to %s%s: %s",
    632   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    633   1.1       cgd 		dest_tkt();
    634   1.1       cgd 		return (1);
    635   1.1       cgd 	} else {
    636   1.1       cgd 		if (!(hp = gethostbyname(hostname))) {
    637  1.13       tls 			warnx("can't get addr of %s", hostname);
    638   1.1       cgd 			dest_tkt();
    639   1.1       cgd 			return (1);
    640   1.1       cgd 		}
    641  1.13       tls 		memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
    642   1.1       cgd 
    643   1.1       cgd 		if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
    644   1.1       cgd 		    &authdata, "")) != KSUCCESS) {
    645  1.49    itojun 			warnx("kerberos: unable to verify rcmd ticket: %s",
    646   1.1       cgd 			    krb_err_txt[kerno]);
    647  1.45     lukem 			syslog(LOG_WARNING,
    648   1.1       cgd 			    "failed su: %s to %s%s: %s", username,
    649   1.1       cgd 			     user, ontty(), krb_err_txt[kerno]);
    650   1.1       cgd 			dest_tkt();
    651   1.1       cgd 			return (1);
    652   1.1       cgd 		}
    653   1.1       cgd 	}
    654   1.1       cgd 	return (0);
    655   1.1       cgd }
    656   1.1       cgd 
    657  1.11  christos static int
    658  1.64  christos koktologin(char *name, char *realm, char *toname)
    659   1.1       cgd {
    660  1.40     assar 	return krb_kuserok(name,
    661  1.40     assar 			   strcmp (toname, "root") == 0 ? "root" : "",
    662  1.40     assar 			   realm,
    663  1.40     assar 			   toname);
    664   1.1       cgd }
    665   1.1       cgd #endif
    666  1.46       sjg 
    667  1.46       sjg static int
    668  1.64  christos check_ingroup (int gid, const char *gname, const char *user, int ifempty)
    669  1.46       sjg {
    670  1.46       sjg 	struct group *gr;
    671  1.46       sjg 	char **g;
    672  1.46       sjg #ifdef SU_INDIRECT_GROUP
    673  1.46       sjg 	char **gr_mem;
    674  1.46       sjg 	int n = 0;
    675  1.46       sjg 	int i = 0;
    676  1.46       sjg #endif
    677  1.46       sjg 	int ok = 0;
    678  1.46       sjg 
    679  1.46       sjg 	if (gname == NULL)
    680  1.46       sjg 		gr = getgrgid((gid_t) gid);
    681  1.46       sjg 	else
    682  1.46       sjg 		gr = getgrnam(gname);
    683  1.46       sjg 
    684  1.46       sjg 	/*
    685  1.46       sjg 	 * XXX we are relying on the fact that we only set ifempty when
    686  1.57  christos 	 * calling to check for SU_GROUP and that is the only time a
    687  1.46       sjg 	 * missing group is acceptable.
    688  1.46       sjg 	 */
    689  1.46       sjg 	if (gr == NULL)
    690  1.46       sjg 		return ifempty;
    691  1.46       sjg 	if (!*gr->gr_mem)		/* empty */
    692  1.46       sjg 		return ifempty;
    693  1.46       sjg 
    694  1.46       sjg 	/*
    695  1.46       sjg 	 * Ok, first see if user is in gr_mem
    696  1.46       sjg 	 */
    697  1.46       sjg 	for (g = gr->gr_mem; *g; ++g) {
    698  1.46       sjg 		if (strcmp(*g, user) == 0)
    699  1.46       sjg 			return 1;	/* ok */
    700  1.46       sjg #ifdef SU_INDIRECT_GROUP
    701  1.46       sjg 		++n;			/* count them */
    702  1.46       sjg #endif
    703  1.46       sjg 	}
    704  1.46       sjg #ifdef SU_INDIRECT_GROUP
    705  1.46       sjg 	/*
    706  1.46       sjg 	 * No.
    707  1.46       sjg 	 * Now we need to duplicate the gr_mem list, and recurse for
    708  1.46       sjg 	 * each member to see if it is a group, and if so whether user is
    709  1.46       sjg 	 * in it.
    710  1.46       sjg 	 */
    711  1.46       sjg 	gr_mem = malloc((n + 1) * sizeof (char *));
    712  1.46       sjg 	for  (g = gr->gr_mem, i = 0; *g; ++g) {
    713  1.51    itojun 		gr_mem[i] = strdup(*g);
    714  1.51    itojun 		if (!gr_mem[i])
    715  1.51    itojun 			err(1, "strdup");
    716  1.51    itojun 		i++;
    717  1.46       sjg 	}
    718  1.46       sjg 	gr_mem[i++] = NULL;
    719  1.46       sjg 
    720  1.46       sjg 	for  (g = gr_mem; ok == 0 && *g; ++g) {
    721  1.46       sjg 		/*
    722  1.46       sjg 		 * If we get this far we don't accept empty/missing groups.
    723  1.46       sjg 		 */
    724  1.46       sjg 		ok = check_ingroup(-1, *g, user, 0);
    725  1.46       sjg 	}
    726  1.46       sjg 	for  (g = gr_mem; *g; ++g) {
    727  1.46       sjg 		free(*g);
    728  1.46       sjg 	}
    729  1.46       sjg 	free(gr_mem);
    730  1.46       sjg #endif
    731  1.46       sjg 	return ok;
    732  1.46       sjg }
    733