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