Home | History | Annotate | Line # | Download | only in su
su_pam.c revision 1.5
      1  1.5  christos /*	$NetBSD: su_pam.c,v 1.5 2005/02/25 21:49:43 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1988 The Regents of the University of California.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  christos  *    may be used to endorse or promote products derived from this software
     17  1.1  christos  *    without specific prior written permission.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  christos  * SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #include <sys/cdefs.h>
     33  1.1  christos #ifndef lint
     34  1.1  christos __COPYRIGHT(
     35  1.1  christos     "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
     36  1.1  christos  All rights reserved.\n");
     37  1.1  christos #endif /* not lint */
     38  1.1  christos 
     39  1.1  christos #ifndef lint
     40  1.1  christos #if 0
     41  1.1  christos static char sccsid[] = "@(#)su.c	8.3 (Berkeley) 4/2/94";*/
     42  1.1  christos #else
     43  1.5  christos __RCSID("$NetBSD: su_pam.c,v 1.5 2005/02/25 21:49:43 christos Exp $");
     44  1.1  christos #endif
     45  1.1  christos #endif /* not lint */
     46  1.1  christos 
     47  1.1  christos #include <sys/param.h>
     48  1.1  christos #include <sys/time.h>
     49  1.1  christos #include <sys/resource.h>
     50  1.1  christos #include <sys/wait.h>
     51  1.1  christos #include <err.h>
     52  1.1  christos #include <errno.h>
     53  1.1  christos #include <grp.h>
     54  1.1  christos #include <paths.h>
     55  1.1  christos #include <pwd.h>
     56  1.1  christos #include <stdio.h>
     57  1.1  christos #include <stdlib.h>
     58  1.1  christos #include <string.h>
     59  1.1  christos #include <syslog.h>
     60  1.1  christos #include <time.h>
     61  1.1  christos #include <tzfile.h>
     62  1.1  christos #include <unistd.h>
     63  1.1  christos #include <login_cap.h>
     64  1.1  christos 
     65  1.1  christos #include <security/pam_appl.h>
     66  1.1  christos #include <security/openpam.h>   /* for openpam_ttyconv() */
     67  1.1  christos 
     68  1.2  christos static const struct pam_conv pamc = { &openpam_ttyconv, NULL };
     69  1.1  christos 
     70  1.1  christos static int chshell(const char *);
     71  1.1  christos static char *ontty(void);
     72  1.1  christos 
     73  1.1  christos int main(int, char **);
     74  1.1  christos 
     75  1.1  christos #define	ARGSTRX	"-dflm"
     76  1.1  christos 
     77  1.1  christos #ifdef LOGIN_CAP
     78  1.1  christos #define ARGSTR	ARGSTRX "c:"
     79  1.1  christos #else
     80  1.1  christos #define ARGSTR ARGSTRX
     81  1.1  christos #endif
     82  1.1  christos 
     83  1.1  christos int
     84  1.1  christos main(int argc, char **argv)
     85  1.1  christos {
     86  1.1  christos 	extern char **environ;
     87  1.1  christos 	struct passwd *pwd;
     88  1.1  christos 	char *p;
     89  1.1  christos 	uid_t ruid;
     90  1.2  christos 	int asme, ch, asthem, fastlogin, prio, gohome, setwhat;
     91  1.1  christos 	enum { UNSET, YES, NO } iscsh = UNSET;
     92  1.1  christos 	char *user, *shell, *avshell, *username, **np;
     93  1.1  christos 	char *class;
     94  1.1  christos 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
     95  1.1  christos 	int pam_err;
     96  1.1  christos 	char hostname[MAXHOSTNAMELEN];
     97  1.1  christos 	char *tty;
     98  1.1  christos 	const char *func;
     99  1.1  christos 	const void *newuser;
    100  1.1  christos 	login_cap_t *lc;
    101  1.2  christos 	pam_handle_t *pamh = NULL;
    102  1.1  christos #ifdef PAM_DEBUG
    103  1.1  christos 	extern int _openpam_debug;
    104  1.1  christos 	_openpam_debug = 1;
    105  1.1  christos #endif
    106  1.1  christos 
    107  1.1  christos 	asme = asthem = fastlogin = 0;
    108  1.1  christos 	gohome = 1;
    109  1.1  christos 	shell = class = NULL;
    110  1.1  christos 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    111  1.1  christos 		switch((char)ch) {
    112  1.1  christos 		case 'c':
    113  1.1  christos 			class = optarg;
    114  1.1  christos 			break;
    115  1.1  christos 		case 'd':
    116  1.1  christos 			asme = 0;
    117  1.1  christos 			asthem = 1;
    118  1.1  christos 			gohome = 0;
    119  1.1  christos 			break;
    120  1.1  christos 		case 'f':
    121  1.1  christos 			fastlogin = 1;
    122  1.1  christos 			break;
    123  1.1  christos 		case '-':
    124  1.1  christos 		case 'l':
    125  1.1  christos 			asme = 0;
    126  1.1  christos 			asthem = 1;
    127  1.1  christos 			break;
    128  1.1  christos 		case 'm':
    129  1.1  christos 			asme = 1;
    130  1.1  christos 			asthem = 0;
    131  1.1  christos 			break;
    132  1.1  christos 		case '?':
    133  1.1  christos 		default:
    134  1.1  christos 			(void)fprintf(stderr,
    135  1.1  christos 			    "Usage: %s [%s] [login [shell arguments]]\n",
    136  1.1  christos 			    getprogname(), ARGSTR);
    137  1.1  christos 			exit(1);
    138  1.1  christos 		}
    139  1.1  christos 	argv += optind;
    140  1.1  christos 
    141  1.1  christos 	/* Lower the priority so su runs faster */
    142  1.1  christos 	errno = 0;
    143  1.1  christos 	prio = getpriority(PRIO_PROCESS, 0);
    144  1.1  christos 	if (errno)
    145  1.1  christos 		prio = 0;
    146  1.1  christos 	if (prio > -2)
    147  1.1  christos 		(void)setpriority(PRIO_PROCESS, 0, -2);
    148  1.1  christos 	openlog("su", 0, LOG_AUTH);
    149  1.1  christos 
    150  1.1  christos 	/* get current login name and shell */
    151  1.1  christos 	ruid = getuid();
    152  1.1  christos 	username = getlogin();
    153  1.1  christos 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    154  1.1  christos 	    pwd->pw_uid != ruid)
    155  1.1  christos 		pwd = getpwuid(ruid);
    156  1.1  christos 	if (pwd == NULL)
    157  1.1  christos 		errx(1, "who are you?");
    158  1.1  christos 	if ((username = strdup(pwd->pw_name)) == NULL)
    159  1.1  christos 		err(1, "strdup");
    160  1.1  christos 
    161  1.1  christos 
    162  1.1  christos 	if (asme) {
    163  1.1  christos 		if (pwd->pw_shell && *pwd->pw_shell) {
    164  1.1  christos 			strlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
    165  1.1  christos 			shell = shellbuf;
    166  1.1  christos 		} else {
    167  1.1  christos 			shell = _PATH_BSHELL;
    168  1.1  christos 			iscsh = NO;
    169  1.1  christos 		}
    170  1.1  christos 	}
    171  1.1  christos 	/* get target login information, default to root */
    172  1.1  christos 	user = *argv ? *argv : "root";
    173  1.1  christos 	np = *argv ? argv : argv - 1;
    174  1.1  christos 
    175  1.1  christos 	if ((pwd = getpwnam(user)) == NULL)
    176  1.1  christos 		errx(1, "unknown login %s", user);
    177  1.1  christos 
    178  1.1  christos 	/*
    179  1.1  christos 	 * PAM initialization
    180  1.1  christos 	 */
    181  1.1  christos #define PAM_END(msg) do { func = msg; goto done; } while (/*CONSTCOND*/0)
    182  1.1  christos 
    183  1.1  christos 	if ((pam_err = pam_start("su", user, &pamc, &pamh)) != PAM_SUCCESS) {
    184  1.1  christos 		if (pamh != NULL)
    185  1.1  christos 			PAM_END("pam_start");
    186  1.1  christos 		/* Things went really bad... */
    187  1.3  christos 		syslog(LOG_ERR, "pam_start failed: %s",
    188  1.3  christos 		    pam_strerror(pamh, pam_err));
    189  1.1  christos 		errx(1, "pam_start failed");
    190  1.1  christos 	}
    191  1.1  christos 
    192  1.1  christos #define PAM_END_ITEM(item)	PAM_END("pam_set_item(" # item ")")
    193  1.4      manu #define PAM_SET_ITEM(item, var) 					    \
    194  1.4      manu 	if ((pam_err = pam_set_item(pamh, (item), (var))) != PAM_SUCCESS)   \
    195  1.1  christos 		PAM_END_ITEM(item)
    196  1.1  christos 
    197  1.1  christos 	/*
    198  1.1  christos 	 * Fill hostname, username and tty
    199  1.1  christos 	 */
    200  1.1  christos 	PAM_SET_ITEM(PAM_RUSER, username);
    201  1.1  christos 	if (gethostname(hostname, sizeof(hostname)) != -1)
    202  1.1  christos 		PAM_SET_ITEM(PAM_RHOST, hostname);
    203  1.1  christos 
    204  1.1  christos 	if ((tty = ttyname(STDERR_FILENO)) != NULL)
    205  1.1  christos 		PAM_SET_ITEM(PAM_TTY, tty);
    206  1.1  christos 
    207  1.1  christos 	/*
    208  1.1  christos 	 * Authentication
    209  1.1  christos 	 */
    210  1.1  christos 	if ((pam_err = pam_authenticate(pamh, 0)) != PAM_SUCCESS) {
    211  1.3  christos 		syslog(LOG_WARNING, "BAD SU %s to %s%s: %s",
    212  1.3  christos 		    username, user, ontty(), pam_strerror(pamh, pam_err));
    213  1.1  christos 		pam_end(pamh, pam_err);
    214  1.3  christos 		errx(1, "Sorry: %s", pam_strerror(pamh, pam_err));
    215  1.1  christos 	}
    216  1.1  christos 
    217  1.1  christos 	/*
    218  1.1  christos 	 * Authorization
    219  1.1  christos 	 */
    220  1.1  christos 	switch(pam_err = pam_acct_mgmt(pamh, 0)) {
    221  1.1  christos 	case PAM_NEW_AUTHTOK_REQD:
    222  1.1  christos 		pam_err = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
    223  1.1  christos 		if (pam_err != PAM_SUCCESS)
    224  1.1  christos 			PAM_END("pam_chauthok");
    225  1.1  christos 		break;
    226  1.1  christos 	case PAM_SUCCESS:
    227  1.1  christos 		break;
    228  1.1  christos 	default:
    229  1.1  christos 		PAM_END("pam_acct_mgmt");
    230  1.1  christos 		break;
    231  1.1  christos 	}
    232  1.1  christos 
    233  1.1  christos 	/*
    234  1.1  christos 	 * pam_authenticate might have changed the target user.
    235  1.1  christos 	 * refresh pwd and user
    236  1.1  christos 	 */
    237  1.1  christos 	pam_err = pam_get_item(pamh, PAM_USER, &newuser);
    238  1.1  christos 	if (pam_err != PAM_SUCCESS) {
    239  1.1  christos 		syslog(LOG_WARNING,
    240  1.1  christos 		    "pam_get_item(PAM_USER): %s", pam_strerror(pamh, pam_err));
    241  1.1  christos 	} else {
    242  1.1  christos 		user = (char *)newuser;
    243  1.1  christos 		if ((pwd = getpwnam(user)) == NULL) {
    244  1.1  christos 			pam_end(pamh, pam_err);
    245  1.1  christos 			syslog(LOG_ERR, "unknown login: %s", username);
    246  1.1  christos 			errx(1, "unknown login: %s", username);
    247  1.1  christos 		}
    248  1.1  christos 	}
    249  1.1  christos 
    250  1.1  christos #define ERRX_PAM_END(args) do {			\
    251  1.1  christos 	pam_end(pamh, pam_err);			\
    252  1.1  christos 	errx args;				\
    253  1.1  christos } while (/* CONSTOCOND */0)
    254  1.1  christos 
    255  1.1  christos #define ERR_PAM_END(args) do {			\
    256  1.1  christos 	pam_end(pamh, pam_err);			\
    257  1.1  christos 	err args;				\
    258  1.1  christos } while (/* CONSTOCOND */0)
    259  1.1  christos 
    260  1.1  christos 	/* force the usage of specified class */
    261  1.1  christos 	if (class) {
    262  1.1  christos 		if (ruid)
    263  1.1  christos 			ERRX_PAM_END((1, "Only root may use -c"));
    264  1.1  christos 
    265  1.1  christos 		pwd->pw_class = class;
    266  1.1  christos 	}
    267  1.2  christos 
    268  1.1  christos 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
    269  1.1  christos 		ERRX_PAM_END((1, "Unknown class %s\n", pwd->pw_class));
    270  1.1  christos 
    271  1.1  christos 	if (asme) {
    272  1.1  christos 		/* if asme and non-standard target shell, must be root */
    273  1.1  christos 		if (!chshell(pwd->pw_shell) && ruid)
    274  1.1  christos 			ERRX_PAM_END((1,"permission denied (shell)."));
    275  1.1  christos 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    276  1.1  christos 		shell = pwd->pw_shell;
    277  1.1  christos 		iscsh = UNSET;
    278  1.1  christos 	} else {
    279  1.1  christos 		shell = _PATH_BSHELL;
    280  1.1  christos 		iscsh = NO;
    281  1.1  christos 	}
    282  1.1  christos 
    283  1.1  christos 	if ((p = strrchr(shell, '/')) != NULL)
    284  1.1  christos 		avshell = p + 1;
    285  1.1  christos 	else
    286  1.1  christos 		avshell = shell;
    287  1.1  christos 
    288  1.1  christos 	/* if we're forking a csh, we want to slightly muck the args */
    289  1.1  christos 	if (iscsh == UNSET)
    290  1.1  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    291  1.1  christos 
    292  1.2  christos 	/*
    293  1.2  christos 	 * Initialize the supplemental groups before pam gets to them,
    294  1.2  christos 	 * so that other pam modules get a chance to add more when
    295  1.2  christos 	 * we do setcred. Note, we don't relinguish our set-userid yet
    296  1.1  christos 	 */
    297  1.2  christos 	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) < 0)
    298  1.2  christos 		ERR_PAM_END((1, "setting user context"));
    299  1.2  christos 
    300  1.2  christos 	if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS)
    301  1.2  christos 		PAM_END("pam_setcred");
    302  1.1  christos 
    303  1.1  christos 	/*
    304  1.1  christos 	 * Manage session.
    305  1.1  christos 	 */
    306  1.1  christos 	if (asthem) {
    307  1.1  christos 		pid_t pid, xpid;
    308  1.1  christos 		void *oint;
    309  1.1  christos 		void *oabrt;
    310  1.1  christos 		int status;
    311  1.1  christos 
    312  1.1  christos  		if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS)
    313  1.1  christos 			PAM_END("pam_open_session");
    314  1.1  christos 
    315  1.1  christos 		/*
    316  1.1  christos 		 * In order to call pam_close_session after the
    317  1.1  christos 		 * command terminates, we need to fork.
    318  1.1  christos 		 * Make sure signals cannot kill the parent.
    319  1.1  christos 		 * This is copied from crontab(8), which has to
    320  1.1  christos 		 * cope with a similar situation. XXX FreeBSD
    321  1.1  christos 		 * has a much more complicated code (CVS logs
    322  1.1  christos 		 * tell about workaround in libpthread, but we
    323  1.1  christos 		 * might miss useful stuff)
    324  1.1  christos 		 */
    325  1.1  christos 		oint = signal(SIGINT, SIG_IGN);
    326  1.1  christos 		oabrt = signal(SIGABRT, SIG_IGN);
    327  1.1  christos 
    328  1.1  christos 		switch (pid = fork()) {
    329  1.1  christos 		case -1:
    330  1.1  christos 			pam_err = pam_close_session(pamh, 0);
    331  1.1  christos 			if (pam_err != PAM_SUCCESS) {
    332  1.1  christos 				syslog(LOG_ERR, "pam_close_session: %s",
    333  1.1  christos 				    pam_strerror(pamh, pam_err));
    334  1.1  christos 				warnx("pam_close_session: %s",
    335  1.1  christos 				    pam_strerror(pamh, pam_err));
    336  1.1  christos 			}
    337  1.1  christos 			ERR_PAM_END((1, "fork"));
    338  1.1  christos 			break;
    339  1.1  christos 
    340  1.1  christos 		case 0:	/* Child */
    341  1.1  christos 			break;
    342  1.1  christos 
    343  1.1  christos 		default:
    344  1.1  christos 			/*
    345  1.1  christos 			 * Parent: wait for the child to terminate
    346  1.1  christos 			 * and call pam_close_session.
    347  1.1  christos 			 */
    348  1.5  christos 			if ((xpid = waitpid(pid, &status, 0)) != pid) {
    349  1.1  christos 				pam_err = pam_close_session(pamh, 0);
    350  1.1  christos 				if (pam_err != PAM_SUCCESS) {
    351  1.1  christos 					syslog(LOG_ERR,
    352  1.1  christos 					    "pam_close_session: %s",
    353  1.1  christos 					    pam_strerror(pamh, pam_err));
    354  1.1  christos 					warnx("pam_close_session: %s",
    355  1.1  christos 					    pam_strerror(pamh, pam_err));
    356  1.1  christos 				}
    357  1.5  christos 				if (xpid == -1) {
    358  1.5  christos 					ERR_PAM_END((1,
    359  1.5  christos 					    "error waiting for pid %d", pid));
    360  1.5  christos 				} else {
    361  1.5  christos 					// Can't happen.
    362  1.5  christos 					ERRX_PAM_END((1,
    363  1.5  christos 					    "wrong PID: %d != %d", pid, xpid));
    364  1.5  christos 				}
    365  1.1  christos 			}
    366  1.1  christos 
    367  1.1  christos 			(void)signal(SIGINT, oint);
    368  1.1  christos 			(void)signal(SIGABRT, oabrt);
    369  1.1  christos 
    370  1.1  christos  			pam_err = pam_close_session(pamh, 0);
    371  1.1  christos 			if (pam_err != PAM_SUCCESS)
    372  1.1  christos 				PAM_END("pam_open_session");
    373  1.1  christos 
    374  1.1  christos 			pam_end(pamh, PAM_SUCCESS);
    375  1.1  christos 			exit(0);
    376  1.1  christos 			break;
    377  1.1  christos 		}
    378  1.1  christos 	}
    379  1.1  christos 
    380  1.1  christos 	/*
    381  1.1  christos 	 * The child: starting here, we don't have to care about
    382  1.1  christos 	 * handling PAM issues if we exit, the parent will do the
    383  1.1  christos 	 * job when we exit.
    384  1.1  christos 	 */
    385  1.1  christos #undef PAM_END
    386  1.1  christos #undef ERR_PAM_END
    387  1.1  christos #undef ERRX_PAM_END
    388  1.1  christos 
    389  1.1  christos 	if (!asme) {
    390  1.1  christos 		if (asthem) {
    391  1.1  christos 			char **pamenv;
    392  1.1  christos 
    393  1.1  christos 			p = getenv("TERM");
    394  1.1  christos 			/*
    395  1.1  christos 			 * Create an empty environment
    396  1.1  christos 			 */
    397  1.1  christos 			if ((environ = malloc(sizeof(char *))) == NULL)
    398  1.1  christos 				err(1, NULL);
    399  1.1  christos 			environ[0] = NULL;
    400  1.1  christos 
    401  1.1  christos 			/*
    402  1.1  christos 			 * Add PAM environement, before the LOGIN_CAP stuff:
    403  1.1  christos 			 * if the login class is unspecified, we'll get the
    404  1.1  christos 			 * same data from PAM, if -c was used, the specified
    405  1.1  christos 			 * class must override PAM.
    406  1.1  christos 	 		 */
    407  1.1  christos 			if ((pamenv = pam_getenvlist(pamh)) != NULL) {
    408  1.1  christos 				char **envitem;
    409  1.1  christos 
    410  1.1  christos 				/*
    411  1.1  christos 				 * XXX Here FreeBSD filters out
    412  1.1  christos 				 * SHELL, LOGNAME, MAIL, CDPATH, IFS, PATH
    413  1.1  christos 				 * how could we get untrusted data here?
    414  1.1  christos 				 */
    415  1.1  christos 				for (envitem = pamenv; *envitem; envitem++) {
    416  1.1  christos 					putenv(*envitem);
    417  1.1  christos 					free(*envitem);
    418  1.1  christos 				}
    419  1.1  christos 
    420  1.1  christos 				free(pamenv);
    421  1.1  christos 			}
    422  1.1  christos 
    423  1.1  christos 			if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
    424  1.1  christos 				err(1, "setting user context");
    425  1.1  christos 			if (p)
    426  1.1  christos 				(void)setenv("TERM", p, 1);
    427  1.1  christos 			if (gohome && chdir(pwd->pw_dir) < 0)
    428  1.1  christos 				errx(1, "no directory");
    429  1.1  christos 		}
    430  1.1  christos 
    431  1.1  christos 		if (asthem || pwd->pw_uid)
    432  1.1  christos 			(void)setenv("USER", pwd->pw_name, 1);
    433  1.1  christos 		(void)setenv("HOME", pwd->pw_dir, 1);
    434  1.1  christos 		(void)setenv("SHELL", shell, 1);
    435  1.1  christos 	}
    436  1.1  christos 	(void)setenv("SU_FROM", username, 1);
    437  1.1  christos 
    438  1.1  christos 	if (iscsh == YES) {
    439  1.1  christos 		if (fastlogin)
    440  1.1  christos 			*np-- = "-f";
    441  1.1  christos 		if (asme)
    442  1.1  christos 			*np-- = "-m";
    443  1.1  christos 	} else {
    444  1.1  christos 		if (fastlogin)
    445  1.1  christos 			unsetenv("ENV");
    446  1.1  christos 	}
    447  1.1  christos 
    448  1.1  christos 	if (asthem) {
    449  1.1  christos 		avshellbuf[0] = '-';
    450  1.1  christos 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    451  1.1  christos 		avshell = avshellbuf;
    452  1.1  christos 	} else if (iscsh == YES) {
    453  1.1  christos 		/* csh strips the first character... */
    454  1.1  christos 		avshellbuf[0] = '_';
    455  1.1  christos 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    456  1.1  christos 		avshell = avshellbuf;
    457  1.1  christos 	}
    458  1.1  christos 	*np = avshell;
    459  1.1  christos 
    460  1.1  christos 	if (ruid != 0)
    461  1.1  christos 		syslog(LOG_NOTICE, "%s to %s%s",
    462  1.1  christos 		    username, pwd->pw_name, ontty());
    463  1.1  christos 
    464  1.2  christos 	/*
    465  1.2  christos 	 * Set user context, except for umask, and the stuff
    466  1.2  christos 	 * we have done before.
    467  1.2  christos 	 */
    468  1.2  christos 	setwhat = LOGIN_SETALL & ~(LOGIN_SETENV|LOGIN_SETUMASK|
    469  1.2  christos 	    LOGIN_SETLOGIN|LOGIN_SETPATH|LOGIN_SETGROUP);
    470  1.2  christos 
    471  1.2  christos 	/*
    472  1.2  christos 	 * Don't touch resource/priority settings if -m has been used
    473  1.2  christos 	 * or -l and -c hasn't, and we're not su'ing to root.
    474  1.2  christos 	 */
    475  1.2  christos 	if ((asme || (!asthem && class == NULL)) && pwd->pw_uid)
    476  1.2  christos 		setwhat &= ~(LOGIN_SETPRIORITY|LOGIN_SETRESOURCES);
    477  1.2  christos 
    478  1.2  christos 	if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) == -1)
    479  1.2  christos 		err(1, "setusercontext");
    480  1.1  christos 
    481  1.1  christos 	(void)execv(shell, np);
    482  1.1  christos 	err(1, "%s", shell);
    483  1.1  christos done:
    484  1.4      manu 	syslog(LOG_ERR, "%s: %s", func, pam_strerror(pamh, pam_err));
    485  1.4      manu 	warnx("%s: %s", func, pam_strerror(pamh, pam_err));
    486  1.4      manu 	pam_end(pamh, pam_err);
    487  1.1  christos 	return 1;
    488  1.1  christos }
    489  1.1  christos 
    490  1.1  christos static int
    491  1.1  christos chshell(const char *sh)
    492  1.1  christos {
    493  1.1  christos 	const char *cp;
    494  1.1  christos 
    495  1.1  christos 	setusershell();
    496  1.1  christos 	while ((cp = getusershell()) != NULL)
    497  1.1  christos 		if (!strcmp(cp, sh))
    498  1.1  christos 			return 1;
    499  1.1  christos 	return 0;
    500  1.1  christos }
    501  1.1  christos 
    502  1.1  christos static char *
    503  1.1  christos ontty(void)
    504  1.1  christos {
    505  1.1  christos 	char *p;
    506  1.1  christos 	static char buf[MAXPATHLEN + 4];
    507  1.1  christos 
    508  1.1  christos 	buf[0] = 0;
    509  1.1  christos 	if ((p = ttyname(STDERR_FILENO)) != NULL)
    510  1.1  christos 		(void)snprintf(buf, sizeof buf, " on %s", p);
    511  1.1  christos 	return buf;
    512  1.1  christos }
    513