Home | History | Annotate | Line # | Download | only in su
su.c revision 1.63
      1  1.63      manu /*	$NetBSD: su.c,v 1.63 2005/01/09 21:32:38 manu 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.63      manu __RCSID("$NetBSD: su.c,v 1.63 2005/01/09 21:32:38 manu 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.59      manu #ifdef USE_PAM
     51  1.59      manu #include <sys/wait.h>
     52  1.59      manu #endif
     53  1.13       tls #include <err.h>
     54  1.11  christos #include <errno.h>
     55  1.17     lukem #include <grp.h>
     56  1.17     lukem #include <paths.h>
     57  1.17     lukem #include <pwd.h>
     58  1.17     lukem #include <stdio.h>
     59  1.27  wsanchez #ifdef SKEY
     60  1.17     lukem #include <skey.h>
     61  1.27  wsanchez #endif
     62   1.7       jtc #include <stdlib.h>
     63   1.1       cgd #include <string.h>
     64  1.17     lukem #include <syslog.h>
     65  1.21    kleink #include <time.h>
     66  1.17     lukem #include <tzfile.h>
     67   1.1       cgd #include <unistd.h>
     68   1.1       cgd 
     69  1.59      manu #ifdef USE_PAM
     70  1.59      manu #include <security/pam_appl.h>
     71  1.59      manu #include <security/openpam.h>   /* for openpam_ttyconv() */
     72  1.63      manu 
     73  1.63      manu static pam_handle_t *pamh = NULL;
     74  1.63      manu static const struct pam_conv pamc = { &openpam_ttyconv, NULL };
     75  1.59      manu #endif
     76  1.59      manu 
     77  1.37       mjl #ifdef LOGIN_CAP
     78  1.37       mjl #include <login_cap.h>
     79  1.37       mjl #endif
     80  1.37       mjl 
     81  1.63      manu #if defined(KERBEROS) && !defined(USE_PAM)
     82  1.41     assar #include <des.h>
     83  1.41     assar #include <krb.h>
     84   1.1       cgd #include <netdb.h>
     85   1.1       cgd 
     86  1.41     assar static int kerberos __P((char *, char *, int));
     87  1.41     assar static int koktologin __P((char *, char *, char *));
     88  1.41     assar 
     89  1.41     assar #endif
     90  1.41     assar 
     91  1.63      manu #if defined(KERBEROS5) && !defined(USE_PAM)
     92  1.41     assar #include <krb5.h>
     93  1.41     assar 
     94  1.41     assar static int kerberos5 __P((char *, char *, int));
     95  1.41     assar 
     96  1.41     assar #endif
     97  1.41     assar 
     98  1.41     assar #if defined(KERBEROS) || defined(KERBEROS5)
     99  1.41     assar 
    100  1.54      jmmv #define	ARGSTRX	"-Kdflm"
    101   1.1       cgd 
    102   1.1       cgd int use_kerberos = 1;
    103  1.11  christos 
    104   1.1       cgd #else
    105  1.54      jmmv #define	ARGSTRX	"-dflm"
    106   1.1       cgd #endif
    107   1.1       cgd 
    108  1.57  christos #ifndef	SU_GROUP
    109  1.57  christos #define	SU_GROUP	"wheel"
    110  1.18     lukem #endif
    111  1.18     lukem 
    112  1.37       mjl #ifdef LOGIN_CAP
    113  1.37       mjl #define ARGSTR	ARGSTRX "c:"
    114  1.37       mjl #else
    115  1.37       mjl #define ARGSTR ARGSTRX
    116  1.37       mjl #endif
    117  1.37       mjl 
    118  1.11  christos int main __P((int, char **));
    119  1.11  christos 
    120  1.25   mycroft static int chshell __P((const char *));
    121  1.11  christos static char *ontty __P((void));
    122  1.63      manu #ifndef USE_PAM
    123  1.46       sjg static int check_ingroup __P((int, const char *, const char *, int));
    124  1.63      manu #endif
    125  1.11  christos 
    126   1.2       sef 
    127  1.63      manu #ifndef USE_PAM
    128   1.7       jtc int
    129   1.1       cgd main(argc, argv)
    130   1.1       cgd 	int argc;
    131   1.1       cgd 	char **argv;
    132   1.1       cgd {
    133   1.1       cgd 	extern char **environ;
    134  1.13       tls 	struct passwd *pwd;
    135  1.17     lukem 	char *p;
    136  1.28  christos #ifdef BSD4_4
    137  1.17     lukem 	struct timeval tp;
    138  1.28  christos #endif
    139  1.11  christos 	uid_t ruid;
    140  1.54      jmmv 	int asme, ch, asthem, fastlogin, prio, gohome;
    141   1.1       cgd 	enum { UNSET, YES, NO } iscsh = UNSET;
    142  1.35  christos 	char *user, *shell, *avshell, *username, **np;
    143  1.37       mjl 	char *userpass, *class;
    144  1.24       mrg 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
    145  1.37       mjl 	time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
    146  1.37       mjl #ifdef LOGIN_CAP
    147  1.37       mjl 	login_cap_t *lc;
    148  1.37       mjl #endif
    149   1.1       cgd 
    150   1.1       cgd 	asme = asthem = fastlogin = 0;
    151  1.54      jmmv 	gohome = 1;
    152  1.37       mjl 	shell = class = NULL;
    153  1.19     lukem 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    154   1.1       cgd 		switch((char)ch) {
    155  1.41     assar #if defined(KERBEROS) || defined(KERBEROS5)
    156   1.1       cgd 		case 'K':
    157   1.1       cgd 			use_kerberos = 0;
    158   1.1       cgd 			break;
    159   1.1       cgd #endif
    160  1.37       mjl #ifdef LOGIN_CAP
    161  1.37       mjl 		case 'c':
    162  1.37       mjl 			class = optarg;
    163  1.37       mjl 			break;
    164  1.37       mjl #endif
    165  1.54      jmmv 		case 'd':
    166  1.54      jmmv 			asme = 0;
    167  1.54      jmmv 			asthem = 1;
    168  1.54      jmmv 			gohome = 0;
    169  1.54      jmmv 			break;
    170   1.1       cgd 		case 'f':
    171   1.1       cgd 			fastlogin = 1;
    172   1.1       cgd 			break;
    173   1.1       cgd 		case '-':
    174   1.1       cgd 		case 'l':
    175   1.1       cgd 			asme = 0;
    176   1.1       cgd 			asthem = 1;
    177   1.1       cgd 			break;
    178   1.1       cgd 		case 'm':
    179   1.1       cgd 			asme = 1;
    180   1.1       cgd 			asthem = 0;
    181   1.1       cgd 			break;
    182   1.1       cgd 		case '?':
    183   1.1       cgd 		default:
    184  1.11  christos 			(void)fprintf(stderr,
    185  1.58      jmmv 			    "usage: %s [%s] [login [shell arguments]]\n",
    186  1.47       cgd 			    getprogname(), ARGSTR);
    187   1.1       cgd 			exit(1);
    188   1.1       cgd 		}
    189   1.1       cgd 	argv += optind;
    190  1.30  christos 
    191  1.44       erh 	/* Lower the priority so su runs faster */
    192   1.1       cgd 	errno = 0;
    193   1.1       cgd 	prio = getpriority(PRIO_PROCESS, 0);
    194   1.1       cgd 	if (errno)
    195   1.1       cgd 		prio = 0;
    196  1.44       erh 	if (prio > -2)
    197  1.44       erh 		(void)setpriority(PRIO_PROCESS, 0, -2);
    198  1.45     lukem 	openlog("su", 0, LOG_AUTH);
    199   1.1       cgd 
    200   1.1       cgd 	/* get current login name and shell */
    201   1.1       cgd 	ruid = getuid();
    202   1.1       cgd 	username = getlogin();
    203   1.1       cgd 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    204   1.1       cgd 	    pwd->pw_uid != ruid)
    205   1.1       cgd 		pwd = getpwuid(ruid);
    206  1.23       mrg 	if (pwd == NULL)
    207  1.63      manu 		errx(1, "who are you?");
    208   1.1       cgd 	username = strdup(pwd->pw_name);
    209  1.30  christos 	userpass = strdup(pwd->pw_passwd);
    210  1.30  christos 	if (username == NULL || userpass == NULL)
    211  1.63      manu 		err(1, "strdup");
    212   1.1       cgd 
    213  1.37       mjl 
    214  1.26      ross 	if (asme) {
    215  1.24       mrg 		if (pwd->pw_shell && *pwd->pw_shell) {
    216  1.50    itojun 			strlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
    217  1.50    itojun 			shell = shellbuf;
    218  1.24       mrg 		} else {
    219  1.24       mrg 			shell = _PATH_BSHELL;
    220  1.24       mrg 			iscsh = NO;
    221  1.24       mrg 		}
    222  1.26      ross 	}
    223  1.63      manu 	/* get target login information, default to root */
    224  1.63      manu 	user = *argv ? *argv : "root";
    225   1.2       sef 	np = *argv ? argv : argv-1;
    226   1.2       sef 
    227  1.23       mrg 	if ((pwd = getpwnam(user)) == NULL)
    228  1.63      manu 		errx(1, "unknown login %s", user);
    229  1.37       mjl 
    230  1.37       mjl #ifdef LOGIN_CAP
    231  1.37       mjl 	/* force the usage of specified class */
    232  1.37       mjl 	if (class) {
    233  1.37       mjl 		if (ruid)
    234  1.63      manu 			errx(1, "Only root may use -c");
    235  1.37       mjl 
    236  1.37       mjl 		pwd->pw_class = class;
    237  1.37       mjl 	}
    238  1.63      manu 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
    239  1.63      manu 		errx(1, "Unknown class %s\n", pwd->pw_class);
    240  1.37       mjl 
    241  1.37       mjl 	pw_warntime = login_getcaptime(lc, "password-warn",
    242  1.37       mjl                                     _PASSWORD_WARNDAYS * SECSPERDAY,
    243  1.37       mjl                                     _PASSWORD_WARNDAYS * SECSPERDAY);
    244  1.37       mjl #endif
    245  1.37       mjl 
    246  1.24       mrg 	if (ruid
    247  1.41     assar #ifdef KERBEROS5
    248  1.41     assar 	    && (!use_kerberos || kerberos5(username, user, pwd->pw_uid))
    249  1.41     assar #endif
    250   1.1       cgd #ifdef KERBEROS
    251  1.24       mrg 	    && (!use_kerberos || kerberos(username, user, pwd->pw_uid))
    252   1.1       cgd #endif
    253  1.24       mrg 	    ) {
    254  1.30  christos 		char *pass = pwd->pw_passwd;
    255  1.30  christos 		int ok = pwd->pw_uid != 0;
    256  1.30  christos 
    257  1.57  christos #ifdef SU_ROOTAUTH
    258  1.34       kim 		/*
    259  1.34       kim 		 * Allow those in group rootauth to su to root, by supplying
    260  1.34       kim 		 * their own password.
    261  1.34       kim 		 */
    262  1.46       sjg 		if (!ok) {
    263  1.57  christos 			if ((ok = check_ingroup(-1, SU_ROOTAUTH, username, 0))) {
    264  1.46       sjg 				pass = userpass;
    265  1.46       sjg 				user = username;
    266  1.34       kim 			}
    267  1.46       sjg 		}
    268  1.34       kim #endif
    269  1.24       mrg 		/*
    270  1.57  christos 		 * Only allow those in group SU_GROUP to su to root,
    271  1.24       mrg 		 * but only if that group has any members.
    272  1.57  christos 		 * If SU_GROUP has no members, allow anyone to su root
    273  1.24       mrg 		 */
    274  1.33       abs 		if (!ok) {
    275  1.57  christos 			ok = check_ingroup(-1, SU_GROUP, username, 1);
    276   1.1       cgd 		}
    277  1.30  christos 		if (!ok)
    278  1.63      manu 			errx(1,
    279  1.30  christos 	    "you are not listed in the correct secondary group (%s) to su %s.",
    280  1.63      manu 					    SU_GROUP, user);
    281   1.1       cgd 		/* if target requires a password, verify it */
    282  1.30  christos 		if (*pass) {
    283   1.1       cgd 			p = getpass("Password:");
    284  1.10   deraadt #ifdef SKEY
    285  1.10   deraadt 			if (strcasecmp(p, "s/key") == 0) {
    286  1.63      manu 				if (skey_haskey(user))
    287  1.63      manu 					errx(1, "Sorry, you have no s/key.");
    288  1.63      manu 				else {
    289  1.10   deraadt 					if (skey_authenticate(user)) {
    290  1.10   deraadt 						goto badlogin;
    291  1.10   deraadt 					}
    292  1.10   deraadt 				}
    293  1.10   deraadt 
    294  1.10   deraadt 			} else
    295  1.10   deraadt #endif
    296  1.30  christos 			if (strcmp(pass, crypt(p, pass))) {
    297  1.31  christos #ifdef SKEY
    298  1.10   deraadt badlogin:
    299  1.27  wsanchez #endif
    300   1.1       cgd 				fprintf(stderr, "Sorry\n");
    301  1.45     lukem 				syslog(LOG_WARNING,
    302   1.1       cgd 					"BAD SU %s to %s%s", username,
    303  1.30  christos 					pwd->pw_name, ontty());
    304   1.1       cgd 				exit(1);
    305   1.1       cgd 			}
    306   1.1       cgd 		}
    307   1.1       cgd 	}
    308   1.1       cgd 
    309   1.1       cgd 	if (asme) {
    310   1.1       cgd 		/* if asme and non-standard target shell, must be root */
    311  1.13       tls 		if (!chshell(pwd->pw_shell) && ruid)
    312  1.63      manu 			errx(1,"permission denied (shell).");
    313   1.1       cgd 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    314   1.1       cgd 		shell = pwd->pw_shell;
    315   1.1       cgd 		iscsh = UNSET;
    316   1.1       cgd 	} else {
    317   1.1       cgd 		shell = _PATH_BSHELL;
    318   1.1       cgd 		iscsh = NO;
    319   1.1       cgd 	}
    320   1.1       cgd 
    321  1.17     lukem 	if ((p = strrchr(shell, '/')) != NULL)
    322   1.9       cgd 		avshell = p+1;
    323   1.9       cgd 	else
    324   1.9       cgd 		avshell = shell;
    325   1.9       cgd 
    326   1.1       cgd 	/* if we're forking a csh, we want to slightly muck the args */
    327   1.9       cgd 	if (iscsh == UNSET)
    328  1.11  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    329   1.1       cgd 
    330   1.1       cgd 	/* set permissions */
    331  1.44       erh #ifdef LOGIN_CAP
    332  1.44       erh 	if (setusercontext(lc, pwd, pwd->pw_uid,
    333  1.44       erh 	    (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
    334  1.44       erh 	    LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER))
    335  1.44       erh 		err(1, "setting user context");
    336  1.44       erh #else
    337  1.13       tls 	if (setgid(pwd->pw_gid) < 0)
    338  1.13       tls 		err(1, "setgid");
    339  1.13       tls 	if (initgroups(user, pwd->pw_gid))
    340  1.13       tls 		errx(1, "initgroups failed");
    341  1.13       tls 	if (setuid(pwd->pw_uid) < 0)
    342  1.13       tls 		err(1, "setuid");
    343  1.37       mjl #endif
    344   1.1       cgd 
    345   1.1       cgd 	if (!asme) {
    346   1.1       cgd 		if (asthem) {
    347   1.1       cgd 			p = getenv("TERM");
    348  1.35  christos 			/* Create an empty environment */
    349  1.35  christos 			if ((environ = malloc(sizeof(char *))) == NULL)
    350  1.48    simonb 				err(1, NULL);
    351  1.35  christos 			environ[0] = NULL;
    352  1.37       mjl #ifdef LOGIN_CAP
    353  1.44       erh 			if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
    354  1.37       mjl 				err(1, "setting user context");
    355  1.37       mjl #else
    356   1.8   mycroft 			(void)setenv("PATH", _PATH_DEFPATH, 1);
    357  1.37       mjl #endif
    358  1.11  christos 			if (p)
    359  1.11  christos 				(void)setenv("TERM", p, 1);
    360  1.54      jmmv 			if (gohome && chdir(pwd->pw_dir) < 0)
    361  1.13       tls 				errx(1, "no directory");
    362  1.37       mjl 		}
    363  1.38       mjl 
    364   1.5       jtc 		if (asthem || pwd->pw_uid)
    365   1.1       cgd 			(void)setenv("USER", pwd->pw_name, 1);
    366   1.1       cgd 		(void)setenv("HOME", pwd->pw_dir, 1);
    367   1.1       cgd 		(void)setenv("SHELL", shell, 1);
    368   1.1       cgd 	}
    369  1.39       abs 	(void)setenv("SU_FROM", username, 1);
    370   1.1       cgd 
    371  1.12  christos 	if (iscsh == YES) {
    372  1.12  christos 		if (fastlogin)
    373  1.12  christos 			*np-- = "-f";
    374  1.12  christos 		if (asme)
    375  1.12  christos 			*np-- = "-m";
    376  1.52  christos 	} else {
    377  1.53   mycroft 		if (fastlogin)
    378  1.53   mycroft 			unsetenv("ENV");
    379  1.12  christos 	}
    380   1.1       cgd 
    381   1.8   mycroft 	if (asthem) {
    382   1.8   mycroft 		avshellbuf[0] = '-';
    383  1.50    itojun 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    384   1.8   mycroft 		avshell = avshellbuf;
    385   1.8   mycroft 	} else if (iscsh == YES) {
    386   1.8   mycroft 		/* csh strips the first character... */
    387   1.8   mycroft 		avshellbuf[0] = '_';
    388  1.50    itojun 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    389   1.8   mycroft 		avshell = avshellbuf;
    390   1.8   mycroft 	}
    391  1.12  christos 	*np = avshell;
    392  1.11  christos 
    393  1.28  christos #ifdef BSD4_4
    394  1.17     lukem 	if (pwd->pw_change || pwd->pw_expire)
    395  1.17     lukem 		(void)gettimeofday(&tp, (struct timezone *)NULL);
    396  1.26      ross 	if (pwd->pw_change) {
    397  1.17     lukem 		if (tp.tv_sec >= pwd->pw_change) {
    398  1.17     lukem 			(void)printf("%s -- %s's password has expired.\n",
    399  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    400  1.17     lukem 			if (ruid != 0)
    401  1.17     lukem 				exit(1);
    402  1.37       mjl 		} else if (pwd->pw_change - tp.tv_sec < pw_warntime)
    403  1.17     lukem 			(void)printf("Warning: %s's password expires on %s",
    404  1.17     lukem 				     user, ctime(&pwd->pw_change));
    405  1.26      ross 	}
    406  1.26      ross 	if (pwd->pw_expire) {
    407  1.17     lukem 		if (tp.tv_sec >= pwd->pw_expire) {
    408  1.17     lukem 			(void)printf("%s -- %s's account has expired.\n",
    409  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    410  1.17     lukem 			if (ruid != 0)
    411  1.17     lukem 				exit(1);
    412  1.17     lukem 		} else if (pwd->pw_expire - tp.tv_sec <
    413  1.17     lukem 		    _PASSWORD_WARNDAYS * SECSPERDAY)
    414  1.17     lukem 			(void)printf("Warning: %s's account expires on %s",
    415  1.17     lukem 				     user, ctime(&pwd->pw_expire));
    416  1.26      ross  	}
    417  1.28  christos #endif
    418   1.1       cgd 	if (ruid != 0)
    419  1.45     lukem 		syslog(LOG_NOTICE, "%s to %s%s",
    420  1.30  christos 		    username, pwd->pw_name, ontty());
    421   1.1       cgd 
    422  1.44       erh 	/* Raise our priority back to what we had before */
    423   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, prio);
    424   1.1       cgd 
    425  1.12  christos 	execv(shell, np);
    426  1.13       tls 	err(1, "%s", shell);
    427  1.27  wsanchez         /* NOTREACHED */
    428   1.1       cgd }
    429  1.11  christos 
    430  1.63      manu #else /* USE_PAM */
    431  1.63      manu 
    432  1.63      manu int
    433  1.63      manu main(argc, argv)
    434  1.63      manu 	int argc;
    435  1.63      manu 	char **argv;
    436  1.63      manu {
    437  1.63      manu 	extern char **environ;
    438  1.63      manu 	struct passwd *pwd;
    439  1.63      manu 	char *p;
    440  1.63      manu 	uid_t ruid;
    441  1.63      manu 	int asme, ch, asthem, fastlogin, prio, gohome;
    442  1.63      manu 	enum { UNSET, YES, NO } iscsh = UNSET;
    443  1.63      manu 	char *user, *shell, *avshell, *username, **np;
    444  1.63      manu 	char *class;
    445  1.63      manu 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
    446  1.63      manu 	int pam_err;
    447  1.63      manu 	char hostname[MAXHOSTNAMELEN];
    448  1.63      manu 	char *tty;
    449  1.63      manu 	const void *newuser;
    450  1.63      manu #ifdef LOGIN_CAP
    451  1.63      manu 	login_cap_t *lc;
    452  1.63      manu #endif
    453  1.63      manu 
    454  1.63      manu 	asme = asthem = fastlogin = 0;
    455  1.63      manu 	gohome = 1;
    456  1.63      manu 	shell = class = NULL;
    457  1.63      manu 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    458  1.63      manu 		switch((char)ch) {
    459  1.63      manu #if defined(KERBEROS) || defined(KERBEROS5)
    460  1.63      manu 		case 'K':
    461  1.63      manu 			fprintf(stderr, "%s: -K is not supported anymore\n",
    462  1.63      manu 			    getprogname());
    463  1.63      manu 			use_kerberos = 0;
    464  1.63      manu 			break;
    465  1.63      manu #endif
    466  1.63      manu #ifdef LOGIN_CAP
    467  1.63      manu 		case 'c':
    468  1.63      manu 			class = optarg;
    469  1.63      manu 			break;
    470  1.63      manu #endif
    471  1.63      manu 		case 'd':
    472  1.63      manu 			asme = 0;
    473  1.63      manu 			asthem = 1;
    474  1.63      manu 			gohome = 0;
    475  1.63      manu 			break;
    476  1.63      manu 		case 'f':
    477  1.63      manu 			fastlogin = 1;
    478  1.63      manu 			break;
    479  1.63      manu 		case '-':
    480  1.63      manu 		case 'l':
    481  1.63      manu 			asme = 0;
    482  1.63      manu 			asthem = 1;
    483  1.63      manu 			break;
    484  1.63      manu 		case 'm':
    485  1.63      manu 			asme = 1;
    486  1.63      manu 			asthem = 0;
    487  1.63      manu 			break;
    488  1.63      manu 		case '?':
    489  1.63      manu 		default:
    490  1.63      manu 			(void)fprintf(stderr,
    491  1.63      manu 			    "usage: %s [%s] [login [shell arguments]]\n",
    492  1.63      manu 			    getprogname(), ARGSTR);
    493  1.63      manu 			exit(1);
    494  1.63      manu 		}
    495  1.63      manu 	argv += optind;
    496  1.63      manu 
    497  1.63      manu 	/* Lower the priority so su runs faster */
    498  1.63      manu 	errno = 0;
    499  1.63      manu 	prio = getpriority(PRIO_PROCESS, 0);
    500  1.63      manu 	if (errno)
    501  1.63      manu 		prio = 0;
    502  1.63      manu 	if (prio > -2)
    503  1.63      manu 		(void)setpriority(PRIO_PROCESS, 0, -2);
    504  1.63      manu 	openlog("su", 0, LOG_AUTH);
    505  1.63      manu 
    506  1.63      manu 	/* get current login name and shell */
    507  1.63      manu 	ruid = getuid();
    508  1.63      manu 	username = getlogin();
    509  1.63      manu 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    510  1.63      manu 	    pwd->pw_uid != ruid)
    511  1.63      manu 		pwd = getpwuid(ruid);
    512  1.63      manu 	if (pwd == NULL)
    513  1.63      manu 		errx(1, "who are you?");
    514  1.63      manu 	if ((username = strdup(pwd->pw_name)) == NULL)
    515  1.63      manu 		err(1, "strdup");
    516  1.63      manu 
    517  1.63      manu 
    518  1.63      manu 	if (asme) {
    519  1.63      manu 		if (pwd->pw_shell && *pwd->pw_shell) {
    520  1.63      manu 			strlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
    521  1.63      manu 			shell = shellbuf;
    522  1.63      manu 		} else {
    523  1.63      manu 			shell = _PATH_BSHELL;
    524  1.63      manu 			iscsh = NO;
    525  1.63      manu 		}
    526  1.63      manu 	}
    527  1.63      manu 	/* get target login information, default to root */
    528  1.63      manu 	user = *argv ? *argv : "root";
    529  1.63      manu 	np = *argv ? argv : argv-1;
    530  1.63      manu 
    531  1.63      manu 	if ((pwd = getpwnam(user)) == NULL)
    532  1.63      manu 		errx(1, "unknown login %s", user);
    533  1.63      manu 
    534  1.63      manu 	/*
    535  1.63      manu 	 * PAM initialization
    536  1.63      manu 	 */
    537  1.63      manu #define PAM_END(func) do {                                              \
    538  1.63      manu 	syslog(LOG_ERR, "%s: %s", func, pam_strerror(pamh, pam_err));   \
    539  1.63      manu 	warnx("%s: %s", func, pam_strerror(pamh, pam_err));             \
    540  1.63      manu 	pam_end(pamh, pam_err);                                         \
    541  1.63      manu 	exit(1);                                                        \
    542  1.63      manu } while (/* CONSTCOND */0)
    543  1.63      manu 
    544  1.63      manu 	if ((pam_err = pam_start("su", user, &pamc, &pamh)) != PAM_SUCCESS) {
    545  1.63      manu 		if (pamh != NULL)
    546  1.63      manu 			PAM_END("pam_start");
    547  1.63      manu 		/* Things went really bad... */
    548  1.63      manu 		syslog(LOG_ERR, "pam_start failed");
    549  1.63      manu 		errx(1, "pam_start failed");
    550  1.63      manu 	}
    551  1.63      manu 
    552  1.63      manu 	/*
    553  1.63      manu 	 * Fill hostname, username and tty
    554  1.63      manu 	 */
    555  1.63      manu 	if ((pam_err = pam_set_item(pamh, PAM_RUSER, username)) != PAM_SUCCESS)
    556  1.63      manu 		PAM_END("pam_set_item(PAM_RUSER)");
    557  1.63      manu 
    558  1.63      manu 	if ((gethostname(hostname, sizeof(hostname)) == 0) &&
    559  1.63      manu 	    ((pam_err = pam_set_item(pamh,
    560  1.63      manu 	    PAM_RHOST, hostname) != PAM_SUCCESS)))
    561  1.63      manu 		PAM_END("pam_set_item(PAM_RHOST)");
    562  1.63      manu 
    563  1.63      manu 	if (((tty = ttyname(STDERR_FILENO)) != NULL) &&
    564  1.63      manu 	    ((pam_err = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS))
    565  1.63      manu 		PAM_END("pam_set_item(PAM_TTY)");
    566  1.63      manu 
    567  1.63      manu 	/*
    568  1.63      manu 	 * Authentication
    569  1.63      manu 	 */
    570  1.63      manu 	if ((pam_err = pam_authenticate(pamh, 0)) != PAM_SUCCESS) {
    571  1.63      manu 		syslog(LOG_WARNING, "BAD SU %s to %s%s",
    572  1.63      manu 		    username, user, ontty());
    573  1.63      manu 		pam_end(pamh, pam_err);
    574  1.63      manu 		errx(1, "Sorry");
    575  1.63      manu 	}
    576  1.63      manu 
    577  1.63      manu 	/*
    578  1.63      manu 	 * Authorization
    579  1.63      manu 	 */
    580  1.63      manu 	switch(pam_err = pam_acct_mgmt(pamh, 0)) {
    581  1.63      manu 	case PAM_NEW_AUTHTOK_REQD:
    582  1.63      manu 		pam_err = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
    583  1.63      manu 		if (pam_err != PAM_SUCCESS)
    584  1.63      manu 			PAM_END("pam_chauthok");
    585  1.63      manu 		break;
    586  1.63      manu 	case PAM_SUCCESS:
    587  1.63      manu 		break;
    588  1.63      manu 	default:
    589  1.63      manu 		PAM_END("pam_acct_mgmt");
    590  1.63      manu 		break;
    591  1.63      manu 	}
    592  1.63      manu 
    593  1.63      manu 	/*
    594  1.63      manu 	 * pam_authenticate might have changed the target user.
    595  1.63      manu 	 * refresh pwd and user
    596  1.63      manu 	 */
    597  1.63      manu 	pam_err = pam_get_item(pamh, PAM_USER, &newuser);
    598  1.63      manu 	if (pam_err != PAM_SUCCESS) {
    599  1.63      manu 		syslog(LOG_WARNING,
    600  1.63      manu 		    "pam_get_item(PAM_USER): %s", pam_strerror(pamh, pam_err));
    601  1.63      manu 	} else {
    602  1.63      manu 		user = (char *)newuser;
    603  1.63      manu 		if ((pwd = getpwnam(user)) == NULL) {
    604  1.63      manu 			pam_end(pamh, pam_err);
    605  1.63      manu 			syslog(LOG_ERR, "unknown login: %s", username);
    606  1.63      manu 			errx(1, "unknown login: %s", username);
    607  1.63      manu 		}
    608  1.63      manu 	}
    609  1.63      manu 
    610  1.63      manu #define ERRX_PAM_END(args) do {			\
    611  1.63      manu 	pam_end(pamh, pam_err);			\
    612  1.63      manu 	errx args;				\
    613  1.63      manu } while (/* CONSTOCOND */0)
    614  1.63      manu 
    615  1.63      manu #define ERR_PAM_END(args) do {			\
    616  1.63      manu 	pam_end(pamh, pam_err);			\
    617  1.63      manu 	err args;				\
    618  1.63      manu } while (/* CONSTOCOND */0)
    619  1.63      manu 
    620  1.63      manu #ifdef LOGIN_CAP
    621  1.63      manu 	/* force the usage of specified class */
    622  1.63      manu 	if (class) {
    623  1.63      manu 		if (ruid)
    624  1.63      manu 			ERRX_PAM_END((1, "Only root may use -c"));
    625  1.63      manu 
    626  1.63      manu 		pwd->pw_class = class;
    627  1.63      manu 	}
    628  1.63      manu 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
    629  1.63      manu 		ERRX_PAM_END((1, "Unknown class %s\n", pwd->pw_class));
    630  1.63      manu #endif
    631  1.63      manu 
    632  1.63      manu 	if (asme) {
    633  1.63      manu 		/* if asme and non-standard target shell, must be root */
    634  1.63      manu 		if (!chshell(pwd->pw_shell) && ruid)
    635  1.63      manu 			ERRX_PAM_END((1,"permission denied (shell)."));
    636  1.63      manu 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    637  1.63      manu 		shell = pwd->pw_shell;
    638  1.63      manu 		iscsh = UNSET;
    639  1.63      manu 	} else {
    640  1.63      manu 		shell = _PATH_BSHELL;
    641  1.63      manu 		iscsh = NO;
    642  1.63      manu 	}
    643  1.63      manu 
    644  1.63      manu 	if ((p = strrchr(shell, '/')) != NULL)
    645  1.63      manu 		avshell = p+1;
    646  1.63      manu 	else
    647  1.63      manu 		avshell = shell;
    648  1.63      manu 
    649  1.63      manu 	/* if we're forking a csh, we want to slightly muck the args */
    650  1.63      manu 	if (iscsh == UNSET)
    651  1.63      manu 		iscsh = strstr(avshell, "csh") ? YES : NO;
    652  1.63      manu 
    653  1.63      manu 	/*
    654  1.63      manu 	 * Set permissions. We change the user credentials (UID) here
    655  1.63      manu 	 * XXX PAM should come before LOGIN_CAP so that the class
    656  1.63      manu 	 * specified through -c can override PAM. But as we might drop
    657  1.63      manu 	 * root UID on both operations, it is not possible to do that.
    658  1.63      manu 	 * If a login class was specified, skip PAM.
    659  1.63      manu 	 */
    660  1.63      manu #ifdef LOGIN_CAP
    661  1.63      manu 	if (class) {
    662  1.63      manu 		if (setusercontext(lc, pwd, pwd->pw_uid,
    663  1.63      manu 		    (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
    664  1.63      manu 		    LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER))
    665  1.63      manu 			ERR_PAM_END((1, "setting user context"));
    666  1.63      manu 	} else
    667  1.63      manu #endif
    668  1.63      manu 	{
    669  1.63      manu 		pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED);
    670  1.63      manu 		if (pam_err != PAM_SUCCESS)
    671  1.63      manu 			PAM_END("pam_setcred");
    672  1.63      manu 	}
    673  1.63      manu 
    674  1.63      manu 	/*
    675  1.63      manu 	 * Manage session.
    676  1.63      manu 	 */
    677  1.63      manu 	if (asthem) {
    678  1.63      manu 		pid_t pid, xpid;
    679  1.63      manu 		void *oint;
    680  1.63      manu 		void *oabrt;
    681  1.63      manu 		int status;
    682  1.63      manu 
    683  1.63      manu  		if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS)
    684  1.63      manu 			PAM_END("pam_open_session");
    685  1.63      manu 
    686  1.63      manu 		/*
    687  1.63      manu 		 * In order to call pam_close_session after the
    688  1.63      manu 		 * command terminates, we need to fork.
    689  1.63      manu 		 * Make sure signals cannot kill the parent.
    690  1.63      manu 		 * This is copied from crontab(8), which has to
    691  1.63      manu 		 * cope with a similar situation. XXX FreeBSD
    692  1.63      manu 		 * has a much more complicated code (CVS logs
    693  1.63      manu 		 * tell about workaround in libpthread, but we
    694  1.63      manu 		 * might miss useful stuff)
    695  1.63      manu 		 */
    696  1.63      manu 		oint = signal(SIGINT, SIG_IGN);
    697  1.63      manu 		oabrt = signal(SIGABRT, SIG_IGN);
    698  1.63      manu 
    699  1.63      manu 		switch (pid = fork()) {
    700  1.63      manu 		case -1:
    701  1.63      manu 			pam_err = pam_close_session(pamh, 0);
    702  1.63      manu 			if (pam_err != PAM_SUCCESS) {
    703  1.63      manu 				syslog(LOG_ERR, "pam_close_session: %s",
    704  1.63      manu 				    pam_strerror(pamh, pam_err));
    705  1.63      manu 				warnx("pam_close_session: %s",
    706  1.63      manu 				    pam_strerror(pamh, pam_err));
    707  1.63      manu 			}
    708  1.63      manu 			ERR_PAM_END((1, "fork"));
    709  1.63      manu 			break;
    710  1.63      manu 
    711  1.63      manu 		case 0:	/* Child */
    712  1.63      manu 			break;
    713  1.63      manu 
    714  1.63      manu 		default:
    715  1.63      manu 			/*
    716  1.63      manu 			 * Parent: wait for the child to terminate
    717  1.63      manu 			 * and call pam_close_session.
    718  1.63      manu 			 */
    719  1.63      manu 			if ((xpid = wait(&status)) != pid) {
    720  1.63      manu 				pam_err = pam_close_session(pamh, 0);
    721  1.63      manu 				if (pam_err != PAM_SUCCESS) {
    722  1.63      manu 					syslog(LOG_ERR,
    723  1.63      manu 					    "pam_close_session: %s",
    724  1.63      manu 					    pam_strerror(pamh, pam_err));
    725  1.63      manu 					warnx("pam_close_session: %s",
    726  1.63      manu 					    pam_strerror(pamh, pam_err));
    727  1.63      manu 				}
    728  1.63      manu 				ERRX_PAM_END((1,
    729  1.63      manu 				    "wrong PID: %d != %d", pid, xpid));
    730  1.63      manu 			}
    731  1.63      manu 
    732  1.63      manu 			(void)signal(SIGINT, oint);
    733  1.63      manu 			(void)signal(SIGABRT, oabrt);
    734  1.63      manu 
    735  1.63      manu  			pam_err = pam_close_session(pamh, 0);
    736  1.63      manu 			if (pam_err != PAM_SUCCESS)
    737  1.63      manu 				PAM_END("pam_open_session");
    738  1.63      manu 
    739  1.63      manu 			pam_end(pamh, PAM_SUCCESS);
    740  1.63      manu 			exit(0);
    741  1.63      manu 			break;
    742  1.63      manu 		}
    743  1.63      manu 	}
    744  1.63      manu 
    745  1.63      manu 	/*
    746  1.63      manu 	 * The child: starting here, we don't have to care about
    747  1.63      manu 	 * handling PAM issues if we exit, the parent will do the
    748  1.63      manu 	 * job when we exit.
    749  1.63      manu 	 */
    750  1.63      manu #undef PAM_END
    751  1.63      manu #undef ERR_PAM_END
    752  1.63      manu #undef ERRX_PAM_END
    753  1.63      manu 
    754  1.63      manu 	if (!asme) {
    755  1.63      manu 		if (asthem) {
    756  1.63      manu 			char **pamenv;
    757  1.63      manu 
    758  1.63      manu 			p = getenv("TERM");
    759  1.63      manu 			/*
    760  1.63      manu 			 * Create an empty environment
    761  1.63      manu 			 */
    762  1.63      manu 			if ((environ = malloc(sizeof(char *))) == NULL)
    763  1.63      manu 				err(1, NULL);
    764  1.63      manu 			environ[0] = NULL;
    765  1.63      manu 
    766  1.63      manu 			/*
    767  1.63      manu 			 * Add PAM environement, before the LOGIN_CAP stuff:
    768  1.63      manu 			 * if the login class is unspecified, we'll get the
    769  1.63      manu 			 * same data from PAM, if -c was used, the specified
    770  1.63      manu 			 * class must override PAM.
    771  1.63      manu 	 		 */
    772  1.63      manu 			if ((pamenv = pam_getenvlist(pamh)) != NULL) {
    773  1.63      manu 				char **envitem;
    774  1.63      manu 
    775  1.63      manu 				/*
    776  1.63      manu 				 * XXX Here FreeBSD filters out
    777  1.63      manu 				 * SHELL, LOGNAME, MAIL, CDPATH, IFS, PATH
    778  1.63      manu 				 * how could we get untrusted data here?
    779  1.63      manu 				 */
    780  1.63      manu 				for (envitem = pamenv; *envitem; envitem++) {
    781  1.63      manu 					putenv(*envitem);
    782  1.63      manu 					free(*envitem);
    783  1.63      manu 				}
    784  1.63      manu 
    785  1.63      manu 				free(pamenv);
    786  1.63      manu 			}
    787  1.63      manu 
    788  1.63      manu #ifdef LOGIN_CAP
    789  1.63      manu 			if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
    790  1.63      manu 				err(1, "setting user context");
    791  1.63      manu #else
    792  1.63      manu 			(void)setenv("PATH", _PATH_DEFPATH, 1);
    793  1.63      manu #endif
    794  1.63      manu 			if (p)
    795  1.63      manu 				(void)setenv("TERM", p, 1);
    796  1.63      manu 			if (gohome && chdir(pwd->pw_dir) < 0)
    797  1.63      manu 				errx(1, "no directory");
    798  1.63      manu 		}
    799  1.63      manu 
    800  1.63      manu 		if (asthem || pwd->pw_uid)
    801  1.63      manu 			(void)setenv("USER", pwd->pw_name, 1);
    802  1.63      manu 		(void)setenv("HOME", pwd->pw_dir, 1);
    803  1.63      manu 		(void)setenv("SHELL", shell, 1);
    804  1.63      manu 	}
    805  1.63      manu 	(void)setenv("SU_FROM", username, 1);
    806  1.63      manu 
    807  1.63      manu 	if (iscsh == YES) {
    808  1.63      manu 		if (fastlogin)
    809  1.63      manu 			*np-- = "-f";
    810  1.63      manu 		if (asme)
    811  1.63      manu 			*np-- = "-m";
    812  1.63      manu 	} else {
    813  1.63      manu 		if (fastlogin)
    814  1.63      manu 			unsetenv("ENV");
    815  1.63      manu 	}
    816  1.63      manu 
    817  1.63      manu 	if (asthem) {
    818  1.63      manu 		avshellbuf[0] = '-';
    819  1.63      manu 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    820  1.63      manu 		avshell = avshellbuf;
    821  1.63      manu 	} else if (iscsh == YES) {
    822  1.63      manu 		/* csh strips the first character... */
    823  1.63      manu 		avshellbuf[0] = '_';
    824  1.63      manu 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    825  1.63      manu 		avshell = avshellbuf;
    826  1.63      manu 	}
    827  1.63      manu 	*np = avshell;
    828  1.63      manu 
    829  1.63      manu 	if (ruid != 0)
    830  1.63      manu 		syslog(LOG_NOTICE, "%s to %s%s",
    831  1.63      manu 		    username, pwd->pw_name, ontty());
    832  1.63      manu 
    833  1.63      manu 	/* Raise our priority back to what we had before */
    834  1.63      manu 	(void)setpriority(PRIO_PROCESS, 0, prio);
    835  1.63      manu 
    836  1.63      manu 	execv(shell, np);
    837  1.63      manu 	err(1, "%s", shell);
    838  1.63      manu         /* NOTREACHED */
    839  1.63      manu }
    840  1.63      manu #endif /* USE_PAM */
    841  1.63      manu 
    842  1.11  christos static int
    843   1.1       cgd chshell(sh)
    844  1.25   mycroft 	const char *sh;
    845   1.1       cgd {
    846  1.25   mycroft 	const char *cp;
    847   1.1       cgd 
    848  1.55       jrf 	setusershell();
    849   1.1       cgd 	while ((cp = getusershell()) != NULL)
    850   1.1       cgd 		if (!strcmp(cp, sh))
    851   1.1       cgd 			return (1);
    852   1.1       cgd 	return (0);
    853   1.1       cgd }
    854   1.1       cgd 
    855  1.11  christos static char *
    856   1.1       cgd ontty()
    857   1.1       cgd {
    858  1.19     lukem 	char *p;
    859   1.1       cgd 	static char buf[MAXPATHLEN + 4];
    860   1.1       cgd 
    861   1.1       cgd 	buf[0] = 0;
    862  1.17     lukem 	if ((p = ttyname(STDERR_FILENO)) != NULL)
    863  1.15       mrg 		(void)snprintf(buf, sizeof buf, " on %s", p);
    864   1.1       cgd 	return (buf);
    865   1.1       cgd }
    866  1.41     assar 
    867  1.63      manu #ifndef USE_PAM
    868  1.41     assar #ifdef KERBEROS5
    869  1.41     assar static int
    870  1.41     assar kerberos5(username, user, uid)
    871  1.41     assar 	char *username, *user;
    872  1.41     assar 	int uid;
    873  1.41     assar {
    874  1.41     assar 	krb5_error_code ret;
    875  1.41     assar 	krb5_context context;
    876  1.41     assar 	krb5_principal princ = NULL;
    877  1.41     assar 	krb5_ccache ccache, ccache2;
    878  1.41     assar 	char *cc_name;
    879  1.43     assar 	const char *filename;
    880  1.41     assar 
    881  1.41     assar 	ret = krb5_init_context(&context);
    882  1.41     assar 	if (ret)
    883  1.41     assar 		return (1);
    884  1.41     assar 
    885  1.41     assar 	if (strcmp (user, "root") == 0)
    886  1.41     assar 		ret = krb5_make_principal(context, &princ,
    887  1.41     assar 					  NULL, username, "root", NULL);
    888  1.41     assar 	else
    889  1.41     assar 		ret = krb5_make_principal(context, &princ,
    890  1.42     assar 					  NULL, user, NULL);
    891  1.41     assar 	if (ret)
    892  1.41     assar 		goto fail;
    893  1.41     assar 	if (!krb5_kuserok(context, princ, user) && !uid) {
    894  1.41     assar 		warnx ("kerberos5: not in %s's ACL.", user);
    895  1.41     assar 		goto fail;
    896  1.41     assar 	}
    897  1.41     assar 	ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache);
    898  1.41     assar 	if (ret)
    899  1.41     assar 		goto fail;
    900  1.41     assar 	ret = krb5_verify_user_lrealm(context, princ, ccache, NULL, TRUE,
    901  1.41     assar 				      NULL);
    902  1.41     assar 	if (ret) {
    903  1.41     assar 		krb5_cc_destroy(context, ccache);
    904  1.41     assar 		switch (ret) {
    905  1.41     assar 		case KRB5_LIBOS_PWDINTR :
    906  1.41     assar 			break;
    907  1.41     assar 		case KRB5KRB_AP_ERR_BAD_INTEGRITY:
    908  1.41     assar 		case KRB5KRB_AP_ERR_MODIFIED:
    909  1.41     assar 			krb5_warnx(context, "Password incorrect");
    910  1.41     assar 			break;
    911  1.41     assar 		default :
    912  1.41     assar 			krb5_warn(context, ret, "krb5_verify_user");
    913  1.41     assar 			break;
    914  1.41     assar 		}
    915  1.41     assar 		goto fail;
    916  1.41     assar 	}
    917  1.41     assar 	ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache2);
    918  1.41     assar 	if (ret) {
    919  1.41     assar 		krb5_cc_destroy(context, ccache);
    920  1.41     assar 		goto fail;
    921  1.41     assar 	}
    922  1.41     assar 	ret = krb5_cc_copy_cache(context, ccache, ccache2);
    923  1.41     assar 	if (ret) {
    924  1.41     assar 		krb5_cc_destroy(context, ccache);
    925  1.41     assar 		krb5_cc_destroy(context, ccache2);
    926  1.41     assar 		goto fail;
    927  1.41     assar 	}
    928  1.41     assar 
    929  1.43     assar 	filename = krb5_cc_get_name(context, ccache2);
    930  1.41     assar 	asprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
    931  1.43     assar 		 filename);
    932  1.43     assar 	if (chown (filename, uid, -1) < 0) {
    933  1.43     assar 		warn("chown %s", filename);
    934  1.43     assar 		free(cc_name);
    935  1.43     assar 		krb5_cc_destroy(context, ccache);
    936  1.43     assar 		krb5_cc_destroy(context, ccache2);
    937  1.43     assar 		goto fail;
    938  1.43     assar 	}
    939  1.43     assar 
    940  1.41     assar 	setenv("KRB5CCNAME", cc_name, 1);
    941  1.41     assar 	free(cc_name);
    942  1.41     assar 	krb5_cc_close(context, ccache2);
    943  1.41     assar 	krb5_cc_destroy(context, ccache);
    944  1.43     assar 	return (0);
    945  1.41     assar 
    946  1.41     assar  fail:
    947  1.41     assar 	if (princ != NULL)
    948  1.41     assar 		krb5_free_principal (context, princ);
    949  1.41     assar 	krb5_free_context (context);
    950  1.41     assar 	return (1);
    951  1.41     assar }
    952  1.41     assar #endif
    953   1.1       cgd 
    954   1.1       cgd #ifdef KERBEROS
    955  1.11  christos static int
    956   1.1       cgd kerberos(username, user, uid)
    957   1.1       cgd 	char *username, *user;
    958   1.1       cgd 	int uid;
    959   1.1       cgd {
    960   1.1       cgd 	KTEXT_ST ticket;
    961   1.1       cgd 	AUTH_DAT authdata;
    962   1.1       cgd 	struct hostent *hp;
    963   1.1       cgd 	int kerno;
    964   1.1       cgd 	u_long faddr;
    965   1.1       cgd 	char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
    966  1.22       mrg 	char hostname[MAXHOSTNAMELEN + 1], savehost[MAXHOSTNAMELEN + 1];
    967   1.1       cgd 
    968  1.40     assar 	if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
    969   1.1       cgd 		return (1);
    970   1.1       cgd 	if (koktologin(username, lrealm, user) && !uid) {
    971  1.13       tls 		warnx("kerberos: not in %s's ACL.", user);
    972   1.1       cgd 		return (1);
    973   1.1       cgd 	}
    974  1.29    scottr 	(void)snprintf(krbtkfile, sizeof krbtkfile, "%s_%s_%d", TKT_ROOT,
    975  1.15       mrg 	    user, getuid());
    976   1.1       cgd 
    977   1.1       cgd 	(void)setenv("KRBTKFILE", krbtkfile, 1);
    978   1.1       cgd 	(void)krb_set_tkt_string(krbtkfile);
    979   1.1       cgd 	/*
    980   1.1       cgd 	 * Set real as well as effective ID to 0 for the moment,
    981   1.1       cgd 	 * to make the kerberos library do the right thing.
    982   1.1       cgd 	 */
    983   1.1       cgd 	if (setuid(0) < 0) {
    984  1.13       tls 		warn("setuid");
    985   1.1       cgd 		return (1);
    986   1.1       cgd 	}
    987   1.1       cgd 
    988   1.1       cgd 	/*
    989   1.1       cgd 	 * Little trick here -- if we are su'ing to root,
    990   1.1       cgd 	 * we need to get a ticket for "xxx.root", where xxx represents
    991   1.1       cgd 	 * the name of the person su'ing.  Otherwise (non-root case),
    992   1.1       cgd 	 * we need to get a ticket for "yyy.", where yyy represents
    993   1.1       cgd 	 * the name of the person being su'd to, and the instance is null
    994   1.1       cgd 	 *
    995   1.1       cgd 	 * We should have a way to set the ticket lifetime,
    996   1.1       cgd 	 * with a system default for root.
    997   1.1       cgd 	 */
    998  1.40     assar 	{
    999  1.40     assar 		char prompt[128];
   1000  1.40     assar 		char passw[256];
   1001  1.40     assar 
   1002  1.40     assar 		(void)snprintf (prompt, sizeof(prompt),
   1003  1.40     assar 			  "%s's Password: ",
   1004  1.40     assar 			  krb_unparse_name_long ((uid == 0 ? username : user),
   1005  1.40     assar 						 (uid == 0 ? "root" : ""),
   1006  1.40     assar 						 lrealm));
   1007  1.40     assar 		if (des_read_pw_string (passw, sizeof (passw), prompt, 0)) {
   1008  1.40     assar 			memset (passw, 0, sizeof (passw));
   1009  1.40     assar 			return (1);
   1010  1.40     assar 		}
   1011  1.40     assar 		if (strlen(passw) == 0)
   1012  1.40     assar 			return (1); /* Empty passwords are not allowed */
   1013  1.40     assar 
   1014  1.40     assar 		kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
   1015  1.40     assar 					  (uid == 0 ? "root" : ""), lrealm,
   1016  1.40     assar 					  KRB_TICKET_GRANTING_TICKET,
   1017  1.40     assar 					  lrealm,
   1018  1.40     assar 					  DEFAULT_TKT_LIFE,
   1019  1.40     assar 					  passw);
   1020  1.40     assar 		memset (passw, 0, strlen (passw));
   1021  1.40     assar 	}
   1022   1.1       cgd 
   1023   1.1       cgd 	if (kerno != KSUCCESS) {
   1024   1.1       cgd 		if (kerno == KDC_PR_UNKNOWN) {
   1025  1.13       tls 			warnx("kerberos: principal unknown: %s.%s@%s",
   1026   1.1       cgd 				(uid == 0 ? username : user),
   1027   1.1       cgd 				(uid == 0 ? "root" : ""), lrealm);
   1028   1.1       cgd 			return (1);
   1029   1.1       cgd 		}
   1030  1.13       tls 		warnx("kerberos: unable to su: %s", krb_err_txt[kerno]);
   1031  1.45     lukem 		syslog(LOG_WARNING,
   1032   1.1       cgd 		    "BAD Kerberos SU: %s to %s%s: %s",
   1033   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
   1034   1.1       cgd 		return (1);
   1035   1.1       cgd 	}
   1036   1.1       cgd 
   1037   1.1       cgd 	if (chown(krbtkfile, uid, -1) < 0) {
   1038  1.13       tls 		warn("chown");
   1039   1.1       cgd 		(void)unlink(krbtkfile);
   1040   1.1       cgd 		return (1);
   1041   1.1       cgd 	}
   1042   1.1       cgd 
   1043   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, -2);
   1044   1.1       cgd 
   1045   1.1       cgd 	if (gethostname(hostname, sizeof(hostname)) == -1) {
   1046  1.13       tls 		warn("gethostname");
   1047   1.1       cgd 		dest_tkt();
   1048   1.1       cgd 		return (1);
   1049   1.1       cgd 	}
   1050  1.22       mrg 	hostname[sizeof(hostname) - 1] = '\0';
   1051   1.1       cgd 
   1052  1.40     assar 	(void)strlcpy(savehost, krb_get_phost(hostname), sizeof(savehost));
   1053   1.1       cgd 	savehost[sizeof(savehost) - 1] = '\0';
   1054   1.1       cgd 
   1055   1.1       cgd 	kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
   1056   1.1       cgd 
   1057   1.1       cgd 	if (kerno == KDC_PR_UNKNOWN) {
   1058  1.13       tls 		warnx("Warning: TGT not verified.");
   1059  1.45     lukem 		syslog(LOG_WARNING,
   1060   1.1       cgd 		    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
   1061   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno],
   1062   1.1       cgd 		    "rcmd", savehost);
   1063   1.1       cgd 	} else if (kerno != KSUCCESS) {
   1064  1.13       tls 		warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
   1065  1.45     lukem 		syslog(LOG_WARNING, "failed su: %s to %s%s: %s",
   1066   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
   1067   1.1       cgd 		dest_tkt();
   1068   1.1       cgd 		return (1);
   1069   1.1       cgd 	} else {
   1070   1.1       cgd 		if (!(hp = gethostbyname(hostname))) {
   1071  1.13       tls 			warnx("can't get addr of %s", hostname);
   1072   1.1       cgd 			dest_tkt();
   1073   1.1       cgd 			return (1);
   1074   1.1       cgd 		}
   1075  1.13       tls 		memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
   1076   1.1       cgd 
   1077   1.1       cgd 		if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
   1078   1.1       cgd 		    &authdata, "")) != KSUCCESS) {
   1079  1.49    itojun 			warnx("kerberos: unable to verify rcmd ticket: %s",
   1080   1.1       cgd 			    krb_err_txt[kerno]);
   1081  1.45     lukem 			syslog(LOG_WARNING,
   1082   1.1       cgd 			    "failed su: %s to %s%s: %s", username,
   1083   1.1       cgd 			     user, ontty(), krb_err_txt[kerno]);
   1084   1.1       cgd 			dest_tkt();
   1085   1.1       cgd 			return (1);
   1086   1.1       cgd 		}
   1087   1.1       cgd 	}
   1088   1.1       cgd 	return (0);
   1089   1.1       cgd }
   1090   1.1       cgd 
   1091  1.11  christos static int
   1092   1.1       cgd koktologin(name, realm, toname)
   1093   1.1       cgd 	char *name, *realm, *toname;
   1094   1.1       cgd {
   1095  1.40     assar 	return krb_kuserok(name,
   1096  1.40     assar 			   strcmp (toname, "root") == 0 ? "root" : "",
   1097  1.40     assar 			   realm,
   1098  1.40     assar 			   toname);
   1099   1.1       cgd }
   1100   1.1       cgd #endif
   1101  1.46       sjg 
   1102  1.46       sjg static int
   1103  1.46       sjg check_ingroup (gid, gname, user, ifempty)
   1104  1.46       sjg 	int gid;
   1105  1.46       sjg 	const char *gname;
   1106  1.46       sjg 	const char *user;
   1107  1.46       sjg 	int ifempty;
   1108  1.46       sjg {
   1109  1.46       sjg 	struct group *gr;
   1110  1.46       sjg 	char **g;
   1111  1.46       sjg #ifdef SU_INDIRECT_GROUP
   1112  1.46       sjg 	char **gr_mem;
   1113  1.46       sjg 	int n = 0;
   1114  1.46       sjg 	int i = 0;
   1115  1.46       sjg #endif
   1116  1.46       sjg 	int ok = 0;
   1117  1.46       sjg 
   1118  1.46       sjg 	if (gname == NULL)
   1119  1.46       sjg 		gr = getgrgid((gid_t) gid);
   1120  1.46       sjg 	else
   1121  1.46       sjg 		gr = getgrnam(gname);
   1122  1.46       sjg 
   1123  1.46       sjg 	/*
   1124  1.46       sjg 	 * XXX we are relying on the fact that we only set ifempty when
   1125  1.57  christos 	 * calling to check for SU_GROUP and that is the only time a
   1126  1.46       sjg 	 * missing group is acceptable.
   1127  1.46       sjg 	 */
   1128  1.46       sjg 	if (gr == NULL)
   1129  1.46       sjg 		return ifempty;
   1130  1.46       sjg 	if (!*gr->gr_mem)		/* empty */
   1131  1.46       sjg 		return ifempty;
   1132  1.46       sjg 
   1133  1.46       sjg 	/*
   1134  1.46       sjg 	 * Ok, first see if user is in gr_mem
   1135  1.46       sjg 	 */
   1136  1.46       sjg 	for (g = gr->gr_mem; *g; ++g) {
   1137  1.46       sjg 		if (strcmp(*g, user) == 0)
   1138  1.46       sjg 			return 1;	/* ok */
   1139  1.46       sjg #ifdef SU_INDIRECT_GROUP
   1140  1.46       sjg 		++n;			/* count them */
   1141  1.46       sjg #endif
   1142  1.46       sjg 	}
   1143  1.46       sjg #ifdef SU_INDIRECT_GROUP
   1144  1.46       sjg 	/*
   1145  1.46       sjg 	 * No.
   1146  1.46       sjg 	 * Now we need to duplicate the gr_mem list, and recurse for
   1147  1.46       sjg 	 * each member to see if it is a group, and if so whether user is
   1148  1.46       sjg 	 * in it.
   1149  1.46       sjg 	 */
   1150  1.46       sjg 	gr_mem = malloc((n + 1) * sizeof (char *));
   1151  1.46       sjg 	for  (g = gr->gr_mem, i = 0; *g; ++g) {
   1152  1.51    itojun 		gr_mem[i] = strdup(*g);
   1153  1.51    itojun 		if (!gr_mem[i])
   1154  1.51    itojun 			err(1, "strdup");
   1155  1.51    itojun 		i++;
   1156  1.46       sjg 	}
   1157  1.46       sjg 	gr_mem[i++] = NULL;
   1158  1.46       sjg 
   1159  1.46       sjg 	for  (g = gr_mem; ok == 0 && *g; ++g) {
   1160  1.46       sjg 		/*
   1161  1.46       sjg 		 * If we get this far we don't accept empty/missing groups.
   1162  1.46       sjg 		 */
   1163  1.46       sjg 		ok = check_ingroup(-1, *g, user, 0);
   1164  1.46       sjg 	}
   1165  1.46       sjg 	for  (g = gr_mem; *g; ++g) {
   1166  1.46       sjg 		free(*g);
   1167  1.46       sjg 	}
   1168  1.46       sjg 	free(gr_mem);
   1169  1.46       sjg #endif
   1170  1.46       sjg 	return ok;
   1171  1.46       sjg }
   1172  1.63      manu #endif /* USE_PAM */
   1173