Home | History | Annotate | Line # | Download | only in su
su.c revision 1.65.12.1
      1  1.65.12.1      matt /*	$NetBSD: su.c,v 1.65.12.1 2007/11/06 23:36:16 matt 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.65.12.1      matt __RCSID("$NetBSD: su.c,v 1.65.12.1 2007/11/06 23:36:16 matt 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.65.12.1      matt #include <util.h>
     66        1.1       cgd 
     67       1.37       mjl #ifdef LOGIN_CAP
     68       1.37       mjl #include <login_cap.h>
     69       1.37       mjl #endif
     70       1.37       mjl 
     71       1.64  christos #ifdef KERBEROS5
     72       1.41     assar #include <krb5.h>
     73       1.41     assar #endif
     74       1.41     assar 
     75  1.65.12.1      matt #ifdef ALLOW_GROUP_CHANGE
     76  1.65.12.1      matt #include "grutil.h"
     77  1.65.12.1      matt #endif
     78  1.65.12.1      matt #include "suutil.h"
     79       1.41     assar 
     80  1.65.12.1      matt #ifdef KERBEROS5
     81       1.54      jmmv #define	ARGSTRX	"-Kdflm"
     82  1.65.12.1      matt static int kerberos5(char *, const char *, uid_t);
     83        1.1       cgd int use_kerberos = 1;
     84        1.1       cgd #else
     85       1.54      jmmv #define	ARGSTRX	"-dflm"
     86        1.1       cgd #endif
     87        1.1       cgd 
     88       1.57  christos #ifndef	SU_GROUP
     89       1.57  christos #define	SU_GROUP	"wheel"
     90       1.18     lukem #endif
     91       1.18     lukem 
     92  1.65.12.1      matt #define GROUP_PASSWORD	"Group Password:"
     93  1.65.12.1      matt 
     94       1.37       mjl #ifdef LOGIN_CAP
     95       1.37       mjl #define ARGSTR	ARGSTRX "c:"
     96       1.37       mjl #else
     97       1.37       mjl #define ARGSTR ARGSTRX
     98       1.37       mjl #endif
     99       1.37       mjl 
    100       1.64  christos static int check_ingroup(int, const char *, const char *, int);
    101       1.11  christos 
    102        1.7       jtc int
    103       1.64  christos main(int argc, char **argv)
    104        1.1       cgd {
    105        1.1       cgd 	extern char **environ;
    106       1.13       tls 	struct passwd *pwd;
    107       1.17     lukem 	char *p;
    108       1.28  christos #ifdef BSD4_4
    109       1.17     lukem 	struct timeval tp;
    110       1.28  christos #endif
    111       1.11  christos 	uid_t ruid;
    112       1.54      jmmv 	int asme, ch, asthem, fastlogin, prio, gohome;
    113        1.1       cgd 	enum { UNSET, YES, NO } iscsh = UNSET;
    114  1.65.12.1      matt 	const char *user, *shell, *avshell;
    115  1.65.12.1      matt 	char *username, **np;
    116       1.37       mjl 	char *userpass, *class;
    117       1.24       mrg 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
    118       1.37       mjl 	time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
    119       1.37       mjl #ifdef LOGIN_CAP
    120       1.37       mjl 	login_cap_t *lc;
    121       1.37       mjl #endif
    122  1.65.12.1      matt #ifdef ALLOW_GROUP_CHANGE
    123  1.65.12.1      matt 	char *gname;
    124  1.65.12.1      matt #endif
    125        1.1       cgd 
    126        1.1       cgd 	asme = asthem = fastlogin = 0;
    127       1.54      jmmv 	gohome = 1;
    128       1.37       mjl 	shell = class = NULL;
    129       1.19     lukem 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    130        1.1       cgd 		switch((char)ch) {
    131  1.65.12.1      matt #ifdef KERBEROS5
    132        1.1       cgd 		case 'K':
    133        1.1       cgd 			use_kerberos = 0;
    134        1.1       cgd 			break;
    135        1.1       cgd #endif
    136       1.37       mjl #ifdef LOGIN_CAP
    137       1.37       mjl 		case 'c':
    138       1.37       mjl 			class = optarg;
    139       1.37       mjl 			break;
    140       1.37       mjl #endif
    141       1.54      jmmv 		case 'd':
    142       1.54      jmmv 			asme = 0;
    143       1.54      jmmv 			asthem = 1;
    144       1.54      jmmv 			gohome = 0;
    145       1.54      jmmv 			break;
    146        1.1       cgd 		case 'f':
    147        1.1       cgd 			fastlogin = 1;
    148        1.1       cgd 			break;
    149        1.1       cgd 		case '-':
    150        1.1       cgd 		case 'l':
    151        1.1       cgd 			asme = 0;
    152        1.1       cgd 			asthem = 1;
    153        1.1       cgd 			break;
    154        1.1       cgd 		case 'm':
    155        1.1       cgd 			asme = 1;
    156        1.1       cgd 			asthem = 0;
    157        1.1       cgd 			break;
    158        1.1       cgd 		case '?':
    159        1.1       cgd 		default:
    160       1.11  christos 			(void)fprintf(stderr,
    161  1.65.12.1      matt #ifdef ALLOW_GROUP_CHANGE
    162  1.65.12.1      matt 			    "usage: %s [%s] [login[:group] [shell arguments]]\n",
    163  1.65.12.1      matt #else
    164       1.58      jmmv 			    "usage: %s [%s] [login [shell arguments]]\n",
    165  1.65.12.1      matt #endif
    166       1.47       cgd 			    getprogname(), ARGSTR);
    167  1.65.12.1      matt 			exit(EXIT_FAILURE);
    168        1.1       cgd 		}
    169        1.1       cgd 	argv += optind;
    170       1.30  christos 
    171       1.44       erh 	/* Lower the priority so su runs faster */
    172        1.1       cgd 	errno = 0;
    173        1.1       cgd 	prio = getpriority(PRIO_PROCESS, 0);
    174        1.1       cgd 	if (errno)
    175        1.1       cgd 		prio = 0;
    176       1.44       erh 	if (prio > -2)
    177       1.44       erh 		(void)setpriority(PRIO_PROCESS, 0, -2);
    178       1.45     lukem 	openlog("su", 0, LOG_AUTH);
    179        1.1       cgd 
    180        1.1       cgd 	/* get current login name and shell */
    181        1.1       cgd 	ruid = getuid();
    182        1.1       cgd 	username = getlogin();
    183        1.1       cgd 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    184        1.1       cgd 	    pwd->pw_uid != ruid)
    185        1.1       cgd 		pwd = getpwuid(ruid);
    186       1.23       mrg 	if (pwd == NULL)
    187  1.65.12.1      matt 		errx(EXIT_FAILURE, "who are you?");
    188  1.65.12.1      matt 	username = estrdup(pwd->pw_name);
    189  1.65.12.1      matt 	userpass = estrdup(pwd->pw_passwd);
    190       1.37       mjl 
    191       1.26      ross 	if (asme) {
    192       1.24       mrg 		if (pwd->pw_shell && *pwd->pw_shell) {
    193  1.65.12.1      matt 			(void)estrlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
    194       1.50    itojun 			shell = shellbuf;
    195       1.24       mrg 		} else {
    196       1.24       mrg 			shell = _PATH_BSHELL;
    197       1.24       mrg 			iscsh = NO;
    198       1.24       mrg 		}
    199       1.26      ross 	}
    200       1.63      manu 	/* get target login information, default to root */
    201       1.63      manu 	user = *argv ? *argv : "root";
    202  1.65.12.1      matt 	np = *argv ? argv : argv - 1;
    203        1.2       sef 
    204  1.65.12.1      matt #ifdef ALLOW_GROUP_CHANGE
    205  1.65.12.1      matt 	if ((p = strchr(user, ':')) != NULL) {
    206  1.65.12.1      matt 		*p = '\0';
    207  1.65.12.1      matt 		gname = ++p;
    208  1.65.12.1      matt 	}
    209  1.65.12.1      matt 	else
    210  1.65.12.1      matt 		gname = NULL;
    211  1.65.12.1      matt 
    212  1.65.12.1      matt #ifdef ALLOW_EMPTY_USER
    213  1.65.12.1      matt 	if (user[0] == '\0' && gname != NULL)
    214  1.65.12.1      matt 		user = username;
    215  1.65.12.1      matt #endif
    216  1.65.12.1      matt #endif
    217       1.23       mrg 	if ((pwd = getpwnam(user)) == NULL)
    218  1.65.12.1      matt 		errx(EXIT_FAILURE, "unknown login `%s'", user);
    219       1.37       mjl 
    220       1.37       mjl #ifdef LOGIN_CAP
    221       1.37       mjl 	/* force the usage of specified class */
    222       1.37       mjl 	if (class) {
    223       1.37       mjl 		if (ruid)
    224  1.65.12.1      matt 			errx(EXIT_FAILURE, "Only root may use -c");
    225       1.37       mjl 
    226       1.37       mjl 		pwd->pw_class = class;
    227       1.37       mjl 	}
    228       1.63      manu 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
    229  1.65.12.1      matt 		errx(EXIT_FAILURE, "Unknown class %s\n", pwd->pw_class);
    230       1.37       mjl 
    231  1.65.12.1      matt 	pw_warntime = (time_t)login_getcaptime(lc, "password-warn",
    232  1.65.12.1      matt 	    _PASSWORD_WARNDAYS * SECSPERDAY,
    233  1.65.12.1      matt 	    _PASSWORD_WARNDAYS * SECSPERDAY);
    234       1.37       mjl #endif
    235       1.37       mjl 
    236       1.24       mrg 	if (ruid
    237       1.41     assar #ifdef KERBEROS5
    238       1.41     assar 	    && (!use_kerberos || kerberos5(username, user, pwd->pw_uid))
    239       1.41     assar #endif
    240       1.24       mrg 	    ) {
    241       1.30  christos 		char *pass = pwd->pw_passwd;
    242       1.30  christos 		int ok = pwd->pw_uid != 0;
    243       1.30  christos 
    244       1.57  christos #ifdef SU_ROOTAUTH
    245       1.34       kim 		/*
    246       1.34       kim 		 * Allow those in group rootauth to su to root, by supplying
    247       1.34       kim 		 * their own password.
    248       1.34       kim 		 */
    249       1.46       sjg 		if (!ok) {
    250       1.57  christos 			if ((ok = check_ingroup(-1, SU_ROOTAUTH, username, 0))) {
    251       1.46       sjg 				pass = userpass;
    252       1.46       sjg 				user = username;
    253       1.34       kim 			}
    254       1.46       sjg 		}
    255       1.34       kim #endif
    256       1.24       mrg 		/*
    257       1.57  christos 		 * Only allow those in group SU_GROUP to su to root,
    258       1.24       mrg 		 * but only if that group has any members.
    259       1.57  christos 		 * If SU_GROUP has no members, allow anyone to su root
    260       1.24       mrg 		 */
    261  1.65.12.1      matt 		if (!ok)
    262       1.57  christos 			ok = check_ingroup(-1, SU_GROUP, username, 1);
    263       1.30  christos 		if (!ok)
    264  1.65.12.1      matt 			errx(EXIT_FAILURE,
    265       1.30  christos 	    "you are not listed in the correct secondary group (%s) to su %s.",
    266       1.63      manu 					    SU_GROUP, user);
    267        1.1       cgd 		/* if target requires a password, verify it */
    268  1.65.12.1      matt 		if (*pass && pwd->pw_uid != ruid) {	/* XXX - OK? */
    269        1.1       cgd 			p = getpass("Password:");
    270       1.10   deraadt #ifdef SKEY
    271       1.10   deraadt 			if (strcasecmp(p, "s/key") == 0) {
    272       1.63      manu 				if (skey_haskey(user))
    273  1.65.12.1      matt 					errx(EXIT_FAILURE,
    274  1.65.12.1      matt 					    "Sorry, you have no s/key.");
    275       1.63      manu 				else {
    276       1.10   deraadt 					if (skey_authenticate(user)) {
    277       1.10   deraadt 						goto badlogin;
    278       1.10   deraadt 					}
    279       1.10   deraadt 				}
    280       1.10   deraadt 
    281       1.10   deraadt 			} else
    282       1.10   deraadt #endif
    283  1.65.12.1      matt 			if (strcmp(pass, crypt(p, pass)) != 0) {
    284       1.31  christos #ifdef SKEY
    285  1.65.12.1      matt  badlogin:
    286       1.27  wsanchez #endif
    287  1.65.12.1      matt 				(void)fprintf(stderr, "Sorry\n");
    288       1.45     lukem 				syslog(LOG_WARNING,
    289        1.1       cgd 					"BAD SU %s to %s%s", username,
    290       1.30  christos 					pwd->pw_name, ontty());
    291  1.65.12.1      matt 				exit(EXIT_FAILURE);
    292        1.1       cgd 			}
    293        1.1       cgd 		}
    294        1.1       cgd 	}
    295        1.1       cgd 
    296        1.1       cgd 	if (asme) {
    297        1.1       cgd 		/* if asme and non-standard target shell, must be root */
    298  1.65.12.1      matt 		if (chshell(pwd->pw_shell) == 0 && ruid)
    299  1.65.12.1      matt 			errx(EXIT_FAILURE, "permission denied (shell).");
    300        1.1       cgd 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    301        1.1       cgd 		shell = pwd->pw_shell;
    302        1.1       cgd 		iscsh = UNSET;
    303        1.1       cgd 	} else {
    304        1.1       cgd 		shell = _PATH_BSHELL;
    305        1.1       cgd 		iscsh = NO;
    306        1.1       cgd 	}
    307        1.1       cgd 
    308       1.17     lukem 	if ((p = strrchr(shell, '/')) != NULL)
    309        1.9       cgd 		avshell = p+1;
    310        1.9       cgd 	else
    311        1.9       cgd 		avshell = shell;
    312        1.9       cgd 
    313        1.1       cgd 	/* if we're forking a csh, we want to slightly muck the args */
    314        1.9       cgd 	if (iscsh == UNSET)
    315       1.11  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    316        1.1       cgd 
    317        1.1       cgd 	/* set permissions */
    318       1.44       erh #ifdef LOGIN_CAP
    319  1.65.12.1      matt # ifdef ALLOW_GROUP_CHANGE
    320  1.65.12.1      matt 	/* if we aren't changing users, keep the current group members */
    321  1.65.12.1      matt 	if (ruid != pwd->pw_uid &&
    322  1.65.12.1      matt 	    setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) == -1)
    323  1.65.12.1      matt 		err(EXIT_FAILURE, "setting user context");
    324  1.65.12.1      matt 
    325  1.65.12.1      matt 	addgroup(lc, gname, pwd, ruid, GROUP_PASSWORD);
    326  1.65.12.1      matt 
    327  1.65.12.1      matt 	if (setusercontext(lc, pwd, pwd->pw_uid,
    328  1.65.12.1      matt 		(u_int)(asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
    329  1.65.12.1      matt 		LOGIN_SETRESOURCES | LOGIN_SETUSER) == -1)
    330  1.65.12.1      matt 		err(EXIT_FAILURE, "setting user context");
    331  1.65.12.1      matt # else
    332       1.44       erh 	if (setusercontext(lc, pwd, pwd->pw_uid,
    333  1.65.12.1      matt 		(u_int)(asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
    334  1.65.12.1      matt 		LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER) == -1)
    335  1.65.12.1      matt 		err(EXIT_FAILURE, "setting user context");
    336  1.65.12.1      matt # endif
    337       1.44       erh #else
    338  1.65.12.1      matt 	if (setgid(pwd->pw_gid) == -1)
    339  1.65.12.1      matt 		err(EXIT_FAILURE, "setgid");
    340  1.65.12.1      matt 	/* if we aren't changing users, keep the current group members */
    341  1.65.12.1      matt 	if (ruid != pwd->pw_uid && initgroups(user, pwd->pw_gid) != 0)
    342  1.65.12.1      matt 		errx(EXIT_FAILURE, "initgroups failed");
    343  1.65.12.1      matt # ifdef ALLOW_GROUP_CHANGE
    344  1.65.12.1      matt 	addgroup(/*EMPTY*/, gname, pwd, ruid, GROUP_PASSWORD);
    345  1.65.12.1      matt # endif
    346  1.65.12.1      matt 	if (setuid(pwd->pw_uid) == -1)
    347  1.65.12.1      matt 		err(EXIT_FAILURE, "setuid");
    348       1.37       mjl #endif
    349        1.1       cgd 
    350        1.1       cgd 	if (!asme) {
    351        1.1       cgd 		if (asthem) {
    352        1.1       cgd 			p = getenv("TERM");
    353       1.35  christos 			/* Create an empty environment */
    354  1.65.12.1      matt 			environ = emalloc(sizeof(char *));
    355       1.35  christos 			environ[0] = NULL;
    356       1.37       mjl #ifdef LOGIN_CAP
    357  1.65.12.1      matt 			if (setusercontext(lc, pwd, pwd->pw_uid,
    358  1.65.12.1      matt 				LOGIN_SETPATH) == -1)
    359  1.65.12.1      matt 				err(EXIT_FAILURE, "setting user context");
    360       1.37       mjl #else
    361        1.8   mycroft 			(void)setenv("PATH", _PATH_DEFPATH, 1);
    362       1.37       mjl #endif
    363       1.11  christos 			if (p)
    364       1.11  christos 				(void)setenv("TERM", p, 1);
    365  1.65.12.1      matt 			if (gohome && chdir(pwd->pw_dir) == -1)
    366  1.65.12.1      matt 				errx(EXIT_FAILURE, "no directory");
    367  1.65.12.1      matt 		}
    368       1.38       mjl 
    369       1.65    kleink 		if (asthem || pwd->pw_uid) {
    370       1.65    kleink 			(void)setenv("LOGNAME", pwd->pw_name, 1);
    371        1.1       cgd 			(void)setenv("USER", pwd->pw_name, 1);
    372       1.65    kleink 		}
    373        1.1       cgd 		(void)setenv("HOME", pwd->pw_dir, 1);
    374        1.1       cgd 		(void)setenv("SHELL", shell, 1);
    375        1.1       cgd 	}
    376       1.39       abs 	(void)setenv("SU_FROM", username, 1);
    377        1.1       cgd 
    378       1.12  christos 	if (iscsh == YES) {
    379       1.12  christos 		if (fastlogin)
    380  1.65.12.1      matt 			*np-- = __UNCONST("-f");
    381       1.12  christos 		if (asme)
    382  1.65.12.1      matt 			*np-- = __UNCONST("-m");
    383       1.52  christos 	} else {
    384       1.53   mycroft 		if (fastlogin)
    385  1.65.12.1      matt 			(void)unsetenv("ENV");
    386       1.12  christos 	}
    387        1.1       cgd 
    388        1.8   mycroft 	if (asthem) {
    389        1.8   mycroft 		avshellbuf[0] = '-';
    390  1.65.12.1      matt 		(void)estrlcpy(avshellbuf + 1, avshell, sizeof(avshellbuf) - 1);
    391        1.8   mycroft 		avshell = avshellbuf;
    392        1.8   mycroft 	} else if (iscsh == YES) {
    393        1.8   mycroft 		/* csh strips the first character... */
    394        1.8   mycroft 		avshellbuf[0] = '_';
    395  1.65.12.1      matt 		(void)estrlcpy(avshellbuf + 1, avshell, sizeof(avshellbuf) - 1);
    396        1.8   mycroft 		avshell = avshellbuf;
    397        1.8   mycroft 	}
    398  1.65.12.1      matt 	*np = __UNCONST(avshell);
    399       1.11  christos 
    400       1.28  christos #ifdef BSD4_4
    401       1.17     lukem 	if (pwd->pw_change || pwd->pw_expire)
    402       1.17     lukem 		(void)gettimeofday(&tp, (struct timezone *)NULL);
    403       1.26      ross 	if (pwd->pw_change) {
    404       1.17     lukem 		if (tp.tv_sec >= pwd->pw_change) {
    405       1.17     lukem 			(void)printf("%s -- %s's password has expired.\n",
    406       1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    407       1.17     lukem 			if (ruid != 0)
    408  1.65.12.1      matt 				exit(EXIT_FAILURE);
    409       1.37       mjl 		} else if (pwd->pw_change - tp.tv_sec < pw_warntime)
    410       1.17     lukem 			(void)printf("Warning: %s's password expires on %s",
    411       1.17     lukem 				     user, ctime(&pwd->pw_change));
    412       1.26      ross 	}
    413       1.26      ross 	if (pwd->pw_expire) {
    414       1.17     lukem 		if (tp.tv_sec >= pwd->pw_expire) {
    415       1.17     lukem 			(void)printf("%s -- %s's account has expired.\n",
    416       1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    417       1.17     lukem 			if (ruid != 0)
    418  1.65.12.1      matt 				exit(EXIT_FAILURE);
    419       1.17     lukem 		} else if (pwd->pw_expire - tp.tv_sec <
    420       1.17     lukem 		    _PASSWORD_WARNDAYS * SECSPERDAY)
    421       1.17     lukem 			(void)printf("Warning: %s's account expires on %s",
    422       1.17     lukem 				     user, ctime(&pwd->pw_expire));
    423  1.65.12.1      matt 	}
    424       1.28  christos #endif
    425        1.1       cgd 	if (ruid != 0)
    426       1.45     lukem 		syslog(LOG_NOTICE, "%s to %s%s",
    427       1.30  christos 		    username, pwd->pw_name, ontty());
    428        1.1       cgd 
    429       1.44       erh 	/* Raise our priority back to what we had before */
    430        1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, prio);
    431        1.1       cgd 
    432  1.65.12.1      matt 	(void)execv(shell, np);
    433  1.65.12.1      matt 	err(EXIT_FAILURE, "%s", shell);
    434  1.65.12.1      matt 	/* NOTREACHED */
    435        1.1       cgd }
    436        1.1       cgd 
    437       1.41     assar 
    438       1.41     assar #ifdef KERBEROS5
    439       1.41     assar static int
    440  1.65.12.1      matt kerberos5(char *username, const char *user, uid_t 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.65.12.1      matt 		return 1;
    452       1.41     assar 
    453  1.65.12.1      matt 	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.65.12.1      matt 		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.65.12.1      matt 	(void)easprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
    499       1.43     assar 		 filename);
    500  1.65.12.1      matt 	if (chown(filename, uid, NOGROUP) == -1) {
    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.65.12.1      matt 	(void)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.65.12.1      matt 	return 0;
    513       1.41     assar 
    514       1.41     assar  fail:
    515       1.41     assar 	if (princ != NULL)
    516  1.65.12.1      matt 		krb5_free_principal(context, princ);
    517  1.65.12.1      matt 	krb5_free_context(context);
    518  1.65.12.1      matt 	return 1;
    519       1.41     assar }
    520  1.65.12.1      matt #endif /* KERBEROS5 */
    521        1.1       cgd 
    522       1.11  christos static int
    523  1.65.12.1      matt check_ingroup(int gid, const char *gname, const char *user, int ifempty)
    524       1.46       sjg {
    525       1.46       sjg 	struct group *gr;
    526       1.46       sjg 	char **g;
    527       1.46       sjg #ifdef SU_INDIRECT_GROUP
    528       1.46       sjg 	char **gr_mem;
    529       1.46       sjg 	int n = 0;
    530       1.46       sjg 	int i = 0;
    531       1.46       sjg #endif
    532       1.46       sjg 	int ok = 0;
    533       1.46       sjg 
    534       1.46       sjg 	if (gname == NULL)
    535       1.46       sjg 		gr = getgrgid((gid_t) gid);
    536       1.46       sjg 	else
    537       1.46       sjg 		gr = getgrnam(gname);
    538       1.46       sjg 
    539       1.46       sjg 	/*
    540       1.46       sjg 	 * XXX we are relying on the fact that we only set ifempty when
    541       1.57  christos 	 * calling to check for SU_GROUP and that is the only time a
    542       1.46       sjg 	 * missing group is acceptable.
    543       1.46       sjg 	 */
    544       1.46       sjg 	if (gr == NULL)
    545       1.46       sjg 		return ifempty;
    546       1.46       sjg 	if (!*gr->gr_mem)		/* empty */
    547       1.46       sjg 		return ifempty;
    548       1.46       sjg 
    549       1.46       sjg 	/*
    550       1.46       sjg 	 * Ok, first see if user is in gr_mem
    551       1.46       sjg 	 */
    552       1.46       sjg 	for (g = gr->gr_mem; *g; ++g) {
    553       1.46       sjg 		if (strcmp(*g, user) == 0)
    554       1.46       sjg 			return 1;	/* ok */
    555       1.46       sjg #ifdef SU_INDIRECT_GROUP
    556       1.46       sjg 		++n;			/* count them */
    557       1.46       sjg #endif
    558       1.46       sjg 	}
    559       1.46       sjg #ifdef SU_INDIRECT_GROUP
    560       1.46       sjg 	/*
    561       1.46       sjg 	 * No.
    562       1.46       sjg 	 * Now we need to duplicate the gr_mem list, and recurse for
    563       1.46       sjg 	 * each member to see if it is a group, and if so whether user is
    564       1.46       sjg 	 * in it.
    565       1.46       sjg 	 */
    566  1.65.12.1      matt 	gr_mem = emalloc((n + 1) * sizeof (char *));
    567  1.65.12.1      matt 	for (g = gr->gr_mem, i = 0; *g; ++g) {
    568  1.65.12.1      matt 		gr_mem[i] = estrdup(*g);
    569       1.51    itojun 		i++;
    570       1.46       sjg 	}
    571       1.46       sjg 	gr_mem[i++] = NULL;
    572  1.65.12.1      matt 
    573  1.65.12.1      matt 	for (g = gr_mem; ok == 0 && *g; ++g) {
    574       1.46       sjg 		/*
    575       1.46       sjg 		 * If we get this far we don't accept empty/missing groups.
    576       1.46       sjg 		 */
    577       1.46       sjg 		ok = check_ingroup(-1, *g, user, 0);
    578       1.46       sjg 	}
    579  1.65.12.1      matt 	for (g = gr_mem; *g; ++g) {
    580       1.46       sjg 		free(*g);
    581       1.46       sjg 	}
    582       1.46       sjg 	free(gr_mem);
    583       1.46       sjg #endif
    584       1.46       sjg 	return ok;
    585       1.46       sjg }
    586