Home | History | Annotate | Line # | Download | only in su
su_pam.c revision 1.14
      1  1.14  christos /*	$NetBSD: su_pam.c,v 1.14 2008/04/05 15:59:39 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.14  christos __RCSID("$NetBSD: su_pam.c,v 1.14 2008/04/05 15:59:39 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.6  christos #include <signal.h>
     57   1.1  christos #include <stdio.h>
     58   1.1  christos #include <stdlib.h>
     59   1.1  christos #include <string.h>
     60   1.1  christos #include <syslog.h>
     61   1.1  christos #include <time.h>
     62   1.1  christos #include <tzfile.h>
     63   1.1  christos #include <unistd.h>
     64  1.13  christos #include <util.h>
     65   1.1  christos #include <login_cap.h>
     66   1.1  christos 
     67   1.1  christos #include <security/pam_appl.h>
     68   1.1  christos #include <security/openpam.h>   /* for openpam_ttyconv() */
     69   1.1  christos 
     70  1.13  christos #ifdef ALLOW_GROUP_CHANGE
     71  1.13  christos #include "grutil.h"
     72  1.13  christos #endif
     73  1.13  christos #include "suutil.h"
     74   1.1  christos 
     75  1.13  christos static const struct pam_conv pamc = { &openpam_ttyconv, NULL };
     76   1.1  christos 
     77   1.1  christos #define	ARGSTRX	"-dflm"
     78   1.1  christos 
     79   1.1  christos #ifdef LOGIN_CAP
     80   1.1  christos #define ARGSTR	ARGSTRX "c:"
     81   1.1  christos #else
     82   1.1  christos #define ARGSTR ARGSTRX
     83   1.1  christos #endif
     84   1.1  christos 
     85  1.13  christos static void logit(const char *, ...);
     86  1.13  christos 
     87   1.1  christos int
     88   1.1  christos main(int argc, char **argv)
     89   1.1  christos {
     90   1.1  christos 	extern char **environ;
     91   1.7  christos 	struct passwd *pwd, pwres;
     92   1.1  christos 	char *p;
     93   1.1  christos 	uid_t ruid;
     94  1.13  christos 	int asme, ch, asthem, fastlogin, prio, gohome;
     95  1.13  christos 	u_int setwhat;
     96   1.1  christos 	enum { UNSET, YES, NO } iscsh = UNSET;
     97  1.13  christos 	const char *user, *shell, *avshell;
     98  1.13  christos 	char *username, *class;
     99  1.13  christos 	char **np;
    100   1.1  christos 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
    101   1.1  christos 	int pam_err;
    102   1.1  christos 	char hostname[MAXHOSTNAMELEN];
    103   1.1  christos 	char *tty;
    104   1.1  christos 	const char *func;
    105   1.1  christos 	const void *newuser;
    106   1.1  christos 	login_cap_t *lc;
    107   1.2  christos 	pam_handle_t *pamh = NULL;
    108   1.8        he 	char pwbuf[1024];
    109   1.1  christos #ifdef PAM_DEBUG
    110   1.1  christos 	extern int _openpam_debug;
    111   1.8        he 
    112   1.1  christos 	_openpam_debug = 1;
    113   1.1  christos #endif
    114  1.13  christos #ifdef ALLOW_GROUP_CHANGE
    115  1.13  christos 	char *gname;
    116  1.13  christos #endif
    117   1.1  christos 
    118  1.14  christos 	(void)setprogname(argv[0]);
    119   1.1  christos 	asme = asthem = fastlogin = 0;
    120   1.1  christos 	gohome = 1;
    121   1.1  christos 	shell = class = NULL;
    122   1.1  christos 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    123   1.1  christos 		switch((char)ch) {
    124   1.1  christos 		case 'c':
    125   1.1  christos 			class = optarg;
    126   1.1  christos 			break;
    127   1.1  christos 		case 'd':
    128   1.1  christos 			asme = 0;
    129   1.1  christos 			asthem = 1;
    130   1.1  christos 			gohome = 0;
    131   1.1  christos 			break;
    132   1.1  christos 		case 'f':
    133   1.1  christos 			fastlogin = 1;
    134   1.1  christos 			break;
    135   1.1  christos 		case '-':
    136   1.1  christos 		case 'l':
    137   1.1  christos 			asme = 0;
    138   1.1  christos 			asthem = 1;
    139   1.1  christos 			break;
    140   1.1  christos 		case 'm':
    141   1.1  christos 			asme = 1;
    142   1.1  christos 			asthem = 0;
    143   1.1  christos 			break;
    144   1.1  christos 		case '?':
    145   1.1  christos 		default:
    146   1.1  christos 			(void)fprintf(stderr,
    147  1.13  christos #ifdef ALLOW_GROUP_CHANGE
    148  1.13  christos 			    "Usage: %s [%s] [login[:group] [shell arguments]]\n",
    149  1.13  christos #else
    150   1.1  christos 			    "Usage: %s [%s] [login [shell arguments]]\n",
    151  1.13  christos #endif
    152   1.1  christos 			    getprogname(), ARGSTR);
    153   1.7  christos 			exit(EXIT_FAILURE);
    154   1.1  christos 		}
    155   1.1  christos 	argv += optind;
    156   1.1  christos 
    157   1.1  christos 	/* Lower the priority so su runs faster */
    158   1.1  christos 	errno = 0;
    159   1.1  christos 	prio = getpriority(PRIO_PROCESS, 0);
    160   1.1  christos 	if (errno)
    161   1.1  christos 		prio = 0;
    162   1.1  christos 	if (prio > -2)
    163   1.1  christos 		(void)setpriority(PRIO_PROCESS, 0, -2);
    164   1.1  christos 	openlog("su", 0, LOG_AUTH);
    165   1.1  christos 
    166   1.1  christos 	/* get current login name and shell */
    167   1.1  christos 	ruid = getuid();
    168   1.1  christos 	username = getlogin();
    169  1.13  christos 	if (username == NULL ||
    170   1.7  christos 	    getpwnam_r(username, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
    171   1.9  christos 	    pwd == NULL || pwd->pw_uid != ruid) {
    172   1.7  christos 		if (getpwuid_r(ruid, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0)
    173   1.7  christos 			pwd = NULL;
    174   1.7  christos 	}
    175   1.1  christos 	if (pwd == NULL)
    176   1.7  christos 		errx(EXIT_FAILURE, "who are you?");
    177  1.13  christos 	username = estrdup(pwd->pw_name);
    178   1.1  christos 
    179   1.1  christos 	if (asme) {
    180   1.1  christos 		if (pwd->pw_shell && *pwd->pw_shell) {
    181  1.13  christos 			(void)estrlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
    182   1.1  christos 			shell = shellbuf;
    183   1.1  christos 		} else {
    184   1.1  christos 			shell = _PATH_BSHELL;
    185   1.1  christos 			iscsh = NO;
    186   1.1  christos 		}
    187   1.1  christos 	}
    188   1.1  christos 	/* get target login information, default to root */
    189   1.1  christos 	user = *argv ? *argv : "root";
    190   1.1  christos 	np = *argv ? argv : argv - 1;
    191   1.1  christos 
    192  1.13  christos #ifdef ALLOW_GROUP_CHANGE
    193  1.13  christos 	if ((p = strchr(user, ':')) != NULL) {
    194  1.13  christos 		*p = '\0';
    195  1.13  christos 		gname = ++p;
    196  1.13  christos 	}
    197  1.13  christos 	else
    198  1.13  christos 		gname = NULL;
    199  1.13  christos 
    200  1.13  christos #ifdef ALLOW_EMPTY_USER
    201  1.13  christos 	if (user[0] == '\0')
    202  1.13  christos 		user = username;
    203  1.13  christos #endif
    204  1.13  christos #endif
    205   1.9  christos 	if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
    206   1.9  christos 	    pwd == NULL)
    207   1.7  christos 		errx(EXIT_FAILURE, "unknown login %s", user);
    208   1.1  christos 
    209   1.1  christos 	/*
    210   1.1  christos 	 * PAM initialization
    211   1.1  christos 	 */
    212  1.13  christos #define PAM_END(msg) do { func = msg; goto done;} /* NOTREACHED */ while (/*CONSTCOND*/0)
    213   1.1  christos 
    214   1.1  christos 	if ((pam_err = pam_start("su", user, &pamc, &pamh)) != PAM_SUCCESS) {
    215   1.1  christos 		if (pamh != NULL)
    216   1.1  christos 			PAM_END("pam_start");
    217   1.1  christos 		/* Things went really bad... */
    218   1.3  christos 		syslog(LOG_ERR, "pam_start failed: %s",
    219   1.3  christos 		    pam_strerror(pamh, pam_err));
    220   1.7  christos 		errx(EXIT_FAILURE, "pam_start failed");
    221   1.1  christos 	}
    222   1.1  christos 
    223   1.1  christos #define PAM_END_ITEM(item)	PAM_END("pam_set_item(" # item ")")
    224   1.4      manu #define PAM_SET_ITEM(item, var) 					    \
    225   1.4      manu 	if ((pam_err = pam_set_item(pamh, (item), (var))) != PAM_SUCCESS)   \
    226   1.1  christos 		PAM_END_ITEM(item)
    227   1.1  christos 
    228   1.1  christos 	/*
    229   1.1  christos 	 * Fill hostname, username and tty
    230   1.1  christos 	 */
    231   1.1  christos 	PAM_SET_ITEM(PAM_RUSER, username);
    232   1.1  christos 	if (gethostname(hostname, sizeof(hostname)) != -1)
    233   1.1  christos 		PAM_SET_ITEM(PAM_RHOST, hostname);
    234   1.1  christos 
    235   1.1  christos 	if ((tty = ttyname(STDERR_FILENO)) != NULL)
    236   1.1  christos 		PAM_SET_ITEM(PAM_TTY, tty);
    237   1.1  christos 
    238  1.13  christos 	/*
    239  1.13  christos 	 * Authentication
    240   1.1  christos 	 */
    241   1.1  christos 	if ((pam_err = pam_authenticate(pamh, 0)) != PAM_SUCCESS) {
    242   1.3  christos 		syslog(LOG_WARNING, "BAD SU %s to %s%s: %s",
    243   1.3  christos 		    username, user, ontty(), pam_strerror(pamh, pam_err));
    244  1.13  christos 		(void)pam_end(pamh, pam_err);
    245   1.7  christos 		errx(EXIT_FAILURE, "Sorry: %s", pam_strerror(pamh, pam_err));
    246   1.1  christos 	}
    247   1.1  christos 
    248   1.1  christos 	/*
    249   1.1  christos 	 * Authorization
    250   1.1  christos 	 */
    251   1.1  christos 	switch(pam_err = pam_acct_mgmt(pamh, 0)) {
    252   1.1  christos 	case PAM_NEW_AUTHTOK_REQD:
    253   1.1  christos 		pam_err = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
    254   1.1  christos 		if (pam_err != PAM_SUCCESS)
    255   1.1  christos 			PAM_END("pam_chauthok");
    256   1.1  christos 		break;
    257   1.1  christos 	case PAM_SUCCESS:
    258   1.1  christos 		break;
    259   1.1  christos 	default:
    260   1.1  christos 		PAM_END("pam_acct_mgmt");
    261   1.1  christos 		break;
    262   1.1  christos 	}
    263   1.1  christos 
    264   1.1  christos 	/*
    265  1.13  christos 	 * pam_authenticate might have changed the target user.
    266   1.1  christos 	 * refresh pwd and user
    267   1.1  christos 	 */
    268   1.1  christos 	pam_err = pam_get_item(pamh, PAM_USER, &newuser);
    269   1.1  christos 	if (pam_err != PAM_SUCCESS) {
    270   1.1  christos 		syslog(LOG_WARNING,
    271   1.1  christos 		    "pam_get_item(PAM_USER): %s", pam_strerror(pamh, pam_err));
    272   1.1  christos 	} else {
    273  1.13  christos 		user = (char *)__UNCONST(newuser);
    274   1.9  christos 		if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
    275   1.9  christos 		    pwd == NULL) {
    276  1.13  christos 			(void)pam_end(pamh, pam_err);
    277   1.1  christos 			syslog(LOG_ERR, "unknown login: %s", username);
    278   1.7  christos 			errx(EXIT_FAILURE, "unknown login: %s", username);
    279   1.1  christos 		}
    280   1.1  christos 	}
    281   1.1  christos 
    282   1.1  christos #define ERRX_PAM_END(args) do {			\
    283  1.13  christos 	(void)pam_end(pamh, pam_err);		\
    284   1.1  christos 	errx args;				\
    285  1.13  christos } while (/* CONSTCOND */0)
    286   1.1  christos 
    287   1.1  christos #define ERR_PAM_END(args) do {			\
    288  1.13  christos 	(void)pam_end(pamh, pam_err);		\
    289   1.1  christos 	err args;				\
    290  1.13  christos } while (/* CONSTCOND */0)
    291  1.13  christos 
    292   1.1  christos 	/* force the usage of specified class */
    293   1.1  christos 	if (class) {
    294  1.13  christos 		if (ruid)
    295   1.7  christos 			ERRX_PAM_END((EXIT_FAILURE, "Only root may use -c"));
    296   1.1  christos 
    297   1.1  christos 		pwd->pw_class = class;
    298   1.1  christos 	}
    299   1.2  christos 
    300   1.1  christos 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
    301   1.7  christos 		ERRX_PAM_END((EXIT_FAILURE,
    302   1.7  christos 		    "Unknown class %s\n", pwd->pw_class));
    303   1.1  christos 
    304   1.1  christos 	if (asme) {
    305   1.1  christos 		/* if asme and non-standard target shell, must be root */
    306  1.13  christos 		if (chshell(pwd->pw_shell) == 0 && ruid)
    307   1.7  christos 			ERRX_PAM_END((EXIT_FAILURE,
    308   1.7  christos 			    "permission denied (shell)."));
    309   1.1  christos 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    310   1.1  christos 		shell = pwd->pw_shell;
    311   1.1  christos 		iscsh = UNSET;
    312   1.1  christos 	} else {
    313   1.1  christos 		shell = _PATH_BSHELL;
    314   1.1  christos 		iscsh = NO;
    315   1.1  christos 	}
    316   1.1  christos 
    317   1.1  christos 	if ((p = strrchr(shell, '/')) != NULL)
    318   1.1  christos 		avshell = p + 1;
    319   1.1  christos 	else
    320   1.1  christos 		avshell = shell;
    321   1.1  christos 
    322   1.1  christos 	/* if we're forking a csh, we want to slightly muck the args */
    323   1.1  christos 	if (iscsh == UNSET)
    324   1.1  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    325   1.1  christos 
    326   1.2  christos 	/*
    327   1.2  christos 	 * Initialize the supplemental groups before pam gets to them,
    328   1.2  christos 	 * so that other pam modules get a chance to add more when
    329   1.2  christos 	 * we do setcred. Note, we don't relinguish our set-userid yet
    330   1.1  christos 	 */
    331  1.13  christos 	/* if we aren't changing users, keep the current group members */
    332  1.13  christos 	if (ruid != pwd->pw_uid &&
    333  1.13  christos 	    setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) == -1)
    334   1.7  christos 		ERR_PAM_END((EXIT_FAILURE, "setting user context"));
    335   1.2  christos 
    336  1.13  christos #ifdef ALLOW_GROUP_CHANGE
    337  1.13  christos 	addgroup(lc, gname, pwd, ruid, "Group Password:");
    338  1.13  christos #endif
    339   1.2  christos 	if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS)
    340   1.2  christos 		PAM_END("pam_setcred");
    341   1.1  christos 
    342   1.1  christos 	/*
    343  1.13  christos 	 * Manage session.
    344   1.1  christos 	 */
    345   1.1  christos 	if (asthem) {
    346   1.1  christos 		pid_t pid, xpid;
    347   1.6  christos 		int status = 1;
    348   1.6  christos 		struct sigaction sa, sa_int, sa_pipe, sa_quit;
    349   1.6  christos 		int fds[2];
    350   1.1  christos 
    351   1.1  christos  		if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS)
    352   1.1  christos 			PAM_END("pam_open_session");
    353   1.1  christos 
    354   1.1  christos 		/*
    355   1.1  christos 		 * In order to call pam_close_session after the
    356  1.13  christos 		 * command terminates, we need to fork.
    357   1.1  christos 		 */
    358   1.6  christos 		sa.sa_flags = SA_RESTART;
    359   1.6  christos 		sa.sa_handler = SIG_IGN;
    360  1.13  christos 		(void)sigemptyset(&sa.sa_mask);
    361   1.6  christos 		(void)sigaction(SIGINT, &sa, &sa_int);
    362   1.6  christos 		(void)sigaction(SIGQUIT, &sa, &sa_quit);
    363   1.6  christos 		(void)sigaction(SIGPIPE, &sa, &sa_pipe);
    364   1.6  christos 		sa.sa_handler = SIG_DFL;
    365   1.6  christos 		(void)sigaction(SIGTSTP, &sa, NULL);
    366   1.6  christos 		/*
    367   1.6  christos 		 * Use a pipe to guarantee the order of execution of
    368   1.6  christos 		 * the parent and the child.
    369   1.6  christos 		 */
    370   1.6  christos 		if (pipe(fds) == -1) {
    371   1.6  christos 			warn("pipe failed");
    372   1.6  christos 			goto out;
    373   1.6  christos 		}
    374  1.13  christos 
    375   1.1  christos 		switch (pid = fork()) {
    376   1.1  christos 		case -1:
    377   1.6  christos 			logit("fork failed (%s)", strerror(errno));
    378   1.6  christos 			goto out;
    379   1.1  christos 
    380   1.1  christos 		case 0:	/* Child */
    381   1.6  christos 			(void)close(fds[1]);
    382   1.6  christos 			(void)read(fds[0], &status, 1);
    383   1.6  christos 			(void)close(fds[0]);
    384   1.6  christos 			(void)sigaction(SIGINT, &sa_int, NULL);
    385   1.6  christos 			(void)sigaction(SIGQUIT, &sa_quit, NULL);
    386   1.6  christos 			(void)sigaction(SIGPIPE, &sa_pipe, NULL);
    387   1.1  christos 			break;
    388   1.1  christos 
    389   1.1  christos 		default:
    390   1.6  christos 			sa.sa_handler = SIG_IGN;
    391   1.6  christos 			(void)sigaction(SIGTTOU, &sa, NULL);
    392   1.6  christos 			(void)close(fds[0]);
    393   1.6  christos 			(void)setpgid(pid, pid);
    394   1.6  christos 			(void)tcsetpgrp(STDERR_FILENO, pid);
    395   1.6  christos 			(void)close(fds[1]);
    396   1.6  christos 			(void)sigaction(SIGPIPE, &sa_pipe, NULL);
    397   1.1  christos 			/*
    398   1.1  christos 			 * Parent: wait for the child to terminate
    399   1.1  christos 			 * and call pam_close_session.
    400   1.1  christos 			 */
    401   1.6  christos 			while ((xpid = waitpid(pid, &status, WUNTRACED))
    402   1.6  christos 			    == pid) {
    403   1.6  christos 				if (WIFSTOPPED(status)) {
    404   1.6  christos 					(void)kill(getpid(), SIGSTOP);
    405   1.6  christos 					(void)tcsetpgrp(STDERR_FILENO,
    406   1.6  christos 					    getpgid(pid));
    407   1.6  christos 					(void)kill(pid, SIGCONT);
    408   1.6  christos 					status = 1;
    409   1.6  christos 					continue;
    410   1.5  christos 				}
    411   1.6  christos 				break;
    412   1.1  christos 			}
    413   1.1  christos 
    414   1.6  christos 			(void)tcsetpgrp(STDERR_FILENO, getpgid(0));
    415   1.6  christos 
    416   1.6  christos 			if (xpid == -1) {
    417   1.6  christos 			    logit("Error waiting for pid %d (%s)", pid,
    418   1.6  christos 				strerror(errno));
    419   1.6  christos 			} else if (xpid != pid) {
    420   1.6  christos 			    /* Can't happen. */
    421   1.6  christos 			    logit("Wrong PID: %d != %d", pid, xpid);
    422   1.6  christos 			}
    423   1.6  christos out:
    424   1.6  christos 			pam_err = pam_setcred(pamh, PAM_DELETE_CRED);
    425   1.1  christos 			if (pam_err != PAM_SUCCESS)
    426  1.13  christos 				logit("pam_setcred: %s",
    427   1.6  christos 				    pam_strerror(pamh, pam_err));
    428   1.6  christos 			pam_err = pam_close_session(pamh, 0);
    429   1.6  christos 			if (pam_err != PAM_SUCCESS)
    430  1.13  christos 				logit("pam_close_session: %s",
    431   1.6  christos 				    pam_strerror(pamh, pam_err));
    432  1.13  christos 			(void)pam_end(pamh, pam_err);
    433   1.6  christos 			exit(WEXITSTATUS(status));
    434  1.13  christos 			break;
    435   1.1  christos 		}
    436   1.1  christos 	}
    437   1.1  christos 
    438  1.13  christos 	/*
    439   1.1  christos 	 * The child: starting here, we don't have to care about
    440  1.13  christos 	 * handling PAM issues if we exit, the parent will do the
    441   1.1  christos 	 * job when we exit.
    442   1.1  christos 	 */
    443   1.1  christos #undef PAM_END
    444   1.1  christos #undef ERR_PAM_END
    445   1.1  christos #undef ERRX_PAM_END
    446   1.1  christos 
    447   1.1  christos 	if (!asme) {
    448   1.1  christos 		if (asthem) {
    449   1.1  christos 			char **pamenv;
    450   1.1  christos 
    451   1.1  christos 			p = getenv("TERM");
    452  1.13  christos 			/*
    453  1.13  christos 			 * Create an empty environment
    454   1.1  christos 			 */
    455  1.13  christos 			environ = emalloc(sizeof(char *));
    456   1.1  christos 			environ[0] = NULL;
    457   1.1  christos 
    458  1.13  christos 			/*
    459   1.1  christos 			 * Add PAM environement, before the LOGIN_CAP stuff:
    460   1.1  christos 			 * if the login class is unspecified, we'll get the
    461   1.1  christos 			 * same data from PAM, if -c was used, the specified
    462   1.1  christos 			 * class must override PAM.
    463   1.1  christos 	 		 */
    464   1.1  christos 			if ((pamenv = pam_getenvlist(pamh)) != NULL) {
    465   1.1  christos 				char **envitem;
    466   1.1  christos 
    467  1.13  christos 				/*
    468  1.13  christos 				 * XXX Here FreeBSD filters out
    469   1.1  christos 				 * SHELL, LOGNAME, MAIL, CDPATH, IFS, PATH
    470   1.1  christos 				 * how could we get untrusted data here?
    471   1.1  christos 				 */
    472   1.1  christos 				for (envitem = pamenv; *envitem; envitem++) {
    473  1.13  christos 					(void)putenv(*envitem);
    474  1.13  christos 					free(*envitem);
    475   1.1  christos 				}
    476   1.1  christos 
    477   1.1  christos 				free(pamenv);
    478   1.1  christos 			}
    479   1.1  christos 
    480  1.13  christos 			if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH |
    481  1.13  christos 				LOGIN_SETENV | LOGIN_SETUMASK) == -1)
    482   1.7  christos 				err(EXIT_FAILURE, "setting user context");
    483   1.1  christos 			if (p)
    484   1.1  christos 				(void)setenv("TERM", p, 1);
    485  1.13  christos 			if (gohome && chdir(pwd->pw_dir) == -1)
    486   1.7  christos 				errx(EXIT_FAILURE, "no directory");
    487  1.13  christos 		}
    488   1.1  christos 
    489  1.10    kleink 		if (asthem || pwd->pw_uid) {
    490  1.10    kleink 			(void)setenv("LOGNAME", pwd->pw_name, 1);
    491   1.1  christos 			(void)setenv("USER", pwd->pw_name, 1);
    492  1.10    kleink 		}
    493   1.1  christos 		(void)setenv("HOME", pwd->pw_dir, 1);
    494   1.1  christos 		(void)setenv("SHELL", shell, 1);
    495   1.1  christos 	}
    496   1.1  christos 	(void)setenv("SU_FROM", username, 1);
    497   1.1  christos 
    498   1.1  christos 	if (iscsh == YES) {
    499   1.1  christos 		if (fastlogin)
    500  1.13  christos 			*np-- = __UNCONST("-f");
    501   1.1  christos 		if (asme)
    502  1.13  christos 			*np-- = __UNCONST("-m");
    503   1.1  christos 	} else {
    504   1.1  christos 		if (fastlogin)
    505  1.13  christos 			(void)unsetenv("ENV");
    506   1.1  christos 	}
    507   1.1  christos 
    508   1.1  christos 	if (asthem) {
    509   1.1  christos 		avshellbuf[0] = '-';
    510  1.13  christos 		(void)estrlcpy(avshellbuf + 1, avshell, sizeof(avshellbuf) - 1);
    511   1.1  christos 		avshell = avshellbuf;
    512   1.1  christos 	} else if (iscsh == YES) {
    513   1.1  christos 		/* csh strips the first character... */
    514   1.1  christos 		avshellbuf[0] = '_';
    515  1.13  christos 		(void)estrlcpy(avshellbuf + 1, avshell, sizeof(avshellbuf) - 1);
    516   1.1  christos 		avshell = avshellbuf;
    517   1.1  christos 	}
    518  1.13  christos 	*np = __UNCONST(avshell);
    519   1.1  christos 
    520   1.1  christos 	if (ruid != 0)
    521   1.1  christos 		syslog(LOG_NOTICE, "%s to %s%s",
    522   1.1  christos 		    username, pwd->pw_name, ontty());
    523   1.1  christos 
    524  1.12   mlelstv 	/* Raise our priority back to what we had before */
    525  1.12   mlelstv 	(void)setpriority(PRIO_PROCESS, 0, prio);
    526  1.12   mlelstv 
    527   1.2  christos 	/*
    528   1.2  christos 	 * Set user context, except for umask, and the stuff
    529   1.2  christos 	 * we have done before.
    530   1.2  christos 	 */
    531  1.13  christos 	setwhat = LOGIN_SETALL & ~(LOGIN_SETENV | LOGIN_SETUMASK |
    532  1.13  christos 	    LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP);
    533   1.2  christos 
    534   1.2  christos 	/*
    535   1.2  christos 	 * Don't touch resource/priority settings if -m has been used
    536   1.2  christos 	 * or -l and -c hasn't, and we're not su'ing to root.
    537   1.2  christos 	 */
    538   1.2  christos 	if ((asme || (!asthem && class == NULL)) && pwd->pw_uid)
    539  1.13  christos 		setwhat &= ~(LOGIN_SETPRIORITY | LOGIN_SETRESOURCES);
    540   1.2  christos 
    541   1.2  christos 	if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) == -1)
    542   1.7  christos 		err(EXIT_FAILURE, "setusercontext");
    543   1.1  christos 
    544   1.1  christos 	(void)execv(shell, np);
    545   1.7  christos 	err(EXIT_FAILURE, "%s", shell);
    546   1.1  christos done:
    547   1.6  christos 	logit("%s: %s", func, pam_strerror(pamh, pam_err));
    548  1.13  christos 	(void)pam_end(pamh, pam_err);
    549   1.7  christos 	return EXIT_FAILURE;
    550   1.1  christos }
    551   1.1  christos 
    552   1.6  christos static void
    553   1.6  christos logit(const char *fmt, ...)
    554   1.6  christos {
    555   1.6  christos 	va_list ap;
    556   1.6  christos 
    557   1.6  christos 	va_start(ap, fmt);
    558   1.6  christos 	vwarnx(fmt, ap);
    559   1.6  christos 	vsyslog(LOG_ERR, fmt, ap);
    560   1.6  christos 	va_end(ap);
    561   1.6  christos }
    562