Home | History | Annotate | Line # | Download | only in login
login_pam.c revision 1.25.16.1
      1  1.25.16.1    martin /*     $NetBSD: login_pam.c,v 1.25.16.1 2020/04/08 14:09:16 martin Exp $       */
      2        1.1      manu 
      3        1.1      manu /*-
      4        1.1      manu  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
      5        1.1      manu  *	The Regents of the University of California.  All rights reserved.
      6        1.1      manu  *
      7        1.1      manu  * Redistribution and use in source and binary forms, with or without
      8        1.1      manu  * modification, are permitted provided that the following conditions
      9        1.1      manu  * are met:
     10        1.1      manu  * 1. Redistributions of source code must retain the above copyright
     11        1.1      manu  *    notice, this list of conditions and the following disclaimer.
     12        1.1      manu  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1      manu  *    notice, this list of conditions and the following disclaimer in the
     14        1.1      manu  *    documentation and/or other materials provided with the distribution.
     15        1.1      manu  * 3. Neither the name of the University nor the names of its contributors
     16        1.1      manu  *    may be used to endorse or promote products derived from this software
     17        1.1      manu  *    without specific prior written permission.
     18        1.1      manu  *
     19        1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1      manu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1      manu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1      manu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1      manu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1      manu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1      manu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1      manu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1      manu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1      manu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1      manu  * SUCH DAMAGE.
     30        1.1      manu  */
     31        1.1      manu 
     32        1.1      manu #include <sys/cdefs.h>
     33        1.1      manu #ifndef lint
     34       1.19     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\
     35       1.19     lukem  The Regents of the University of California.  All rights reserved.");
     36        1.1      manu #endif /* not lint */
     37        1.1      manu 
     38        1.1      manu #ifndef lint
     39        1.1      manu #if 0
     40        1.1      manu static char sccsid[] = "@(#)login.c	8.4 (Berkeley) 4/2/94";
     41        1.1      manu #endif
     42  1.25.16.1    martin __RCSID("$NetBSD: login_pam.c,v 1.25.16.1 2020/04/08 14:09:16 martin Exp $");
     43        1.1      manu #endif /* not lint */
     44        1.1      manu 
     45        1.1      manu /*
     46        1.1      manu  * login [ name ]
     47        1.1      manu  * login -h hostname	(for telnetd, etc.)
     48        1.1      manu  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
     49        1.1      manu  */
     50        1.1      manu 
     51        1.1      manu #include <sys/param.h>
     52        1.1      manu #include <sys/stat.h>
     53        1.1      manu #include <sys/time.h>
     54        1.1      manu #include <sys/resource.h>
     55        1.1      manu #include <sys/file.h>
     56        1.1      manu #include <sys/wait.h>
     57        1.1      manu #include <sys/socket.h>
     58        1.1      manu 
     59        1.1      manu #include <err.h>
     60        1.1      manu #include <errno.h>
     61        1.1      manu #include <grp.h>
     62        1.1      manu #include <pwd.h>
     63        1.1      manu #include <setjmp.h>
     64        1.1      manu #include <signal.h>
     65        1.1      manu #include <stdio.h>
     66        1.1      manu #include <stdlib.h>
     67        1.1      manu #include <string.h>
     68        1.1      manu #include <syslog.h>
     69        1.1      manu #include <time.h>
     70        1.1      manu #include <ttyent.h>
     71        1.1      manu #include <tzfile.h>
     72        1.1      manu #include <unistd.h>
     73        1.1      manu #include <util.h>
     74        1.1      manu #include <login_cap.h>
     75        1.1      manu #include <vis.h>
     76        1.1      manu 
     77        1.1      manu #include <security/pam_appl.h>
     78        1.1      manu #include <security/openpam.h>
     79        1.1      manu 
     80        1.1      manu #include "pathnames.h"
     81       1.20  christos #include "common.h"
     82        1.1      manu 
     83       1.20  christos #if 0
     84       1.20  christos static int	 rootterm(char *);
     85       1.20  christos #endif
     86       1.20  christos static void	 usage(void) __attribute__((__noreturn__));
     87        1.1      manu 
     88        1.1      manu static struct pam_conv pamc = { openpam_ttyconv, NULL };
     89        1.1      manu 
     90        1.1      manu #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
     91        1.1      manu 
     92        1.1      manu #define DEFAULT_BACKOFF 3
     93        1.1      manu #define DEFAULT_RETRIES 10
     94        1.1      manu 
     95       1.20  christos static struct	passwd pwres;
     96       1.20  christos static char	pwbuf[1024];
     97       1.20  christos static struct	group grs, *grp;
     98       1.20  christos static char	grbuf[1024];
     99       1.20  christos extern char **environ;
    100        1.1      manu 
    101        1.1      manu int
    102        1.1      manu main(int argc, char *argv[])
    103        1.1      manu {
    104        1.1      manu 	struct stat st;
    105       1.10  christos 	int ask, ch, cnt, fflag, pflag, quietlog, rootlogin;
    106        1.1      manu 	int auth_passed;
    107        1.1      manu 	uid_t uid, saved_uid;
    108        1.1      manu 	gid_t saved_gid, saved_gids[NGROUPS_MAX];
    109        1.1      manu 	int nsaved_gids;
    110       1.23  christos 	char *domain, *p, *ttyn;
    111        1.1      manu 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
    112        1.1      manu 	char localhost[MAXHOSTNAMELEN + 1];
    113        1.1      manu 	int login_retries = DEFAULT_RETRIES,
    114        1.1      manu 	    login_backoff = DEFAULT_BACKOFF;
    115        1.1      manu 	char *shell = NULL;
    116        1.1      manu 	login_cap_t *lc = NULL;
    117        1.1      manu 	pam_handle_t *pamh = NULL;
    118        1.1      manu 	int pam_err;
    119       1.17  christos 	sig_t oint, oabrt, oquit, oalrm;
    120        1.1      manu 	const void *newuser;
    121        1.1      manu 	int pam_silent = PAM_SILENT;
    122        1.1      manu 	pid_t xpid, pid;
    123        1.1      manu 	int status;
    124        1.1      manu 	char *saved_term;
    125        1.1      manu 	char **pamenv;
    126        1.1      manu 
    127        1.1      manu 	tbuf[0] = '\0';
    128        1.1      manu 	nested = NULL;
    129        1.1      manu 
    130       1.17  christos 	oabrt = signal(SIGABRT, SIG_IGN);
    131       1.17  christos 	oalrm = signal(SIGALRM, timedout);
    132       1.17  christos 	oint = signal(SIGINT, SIG_IGN);
    133       1.17  christos 	oquit = signal(SIGQUIT, SIG_IGN);
    134       1.17  christos 
    135        1.1      manu 	(void)alarm(timeout);
    136        1.1      manu 	(void)setpriority(PRIO_PROCESS, 0, 0);
    137        1.1      manu 
    138        1.1      manu 	openlog("login", 0, LOG_AUTH);
    139        1.1      manu 
    140        1.1      manu 	/*
    141        1.1      manu 	 * -p is used by getty to tell login not to destroy the environment
    142        1.1      manu 	 * -f is used to skip a second login authentication
    143        1.1      manu 	 * -h is used by other servers to pass the name of the remote host to
    144        1.1      manu 	 *    login so that it may be placed in utmp/utmpx and wtmp/wtmpx
    145        1.1      manu 	 * -a in addition to -h, a server my supply -a to pass the actual
    146        1.1      manu 	 *    server address.
    147        1.1      manu 	 */
    148        1.1      manu 	domain = NULL;
    149        1.1      manu 	if (gethostname(localhost, sizeof(localhost)) < 0)
    150        1.1      manu 		syslog(LOG_ERR, "couldn't get local hostname: %m");
    151        1.1      manu 	else
    152        1.1      manu 		domain = strchr(localhost, '.');
    153        1.1      manu 	localhost[sizeof(localhost) - 1] = '\0';
    154        1.1      manu 
    155       1.10  christos 	fflag = pflag = 0;
    156        1.1      manu 	have_ss = 0;
    157        1.1      manu 	uid = getuid();
    158       1.10  christos 	while ((ch = getopt(argc, argv, "a:fh:p")) != -1)
    159        1.1      manu 		switch (ch) {
    160        1.1      manu 		case 'a':
    161        1.5  christos 			if (uid) {
    162        1.5  christos 				errno = EPERM;
    163        1.5  christos 				err(EXIT_FAILURE, "-a option");
    164        1.5  christos 			}
    165        1.1      manu 			decode_ss(optarg);
    166        1.1      manu 			break;
    167        1.1      manu 		case 'f':
    168        1.1      manu 			fflag = 1;
    169        1.1      manu 			break;
    170        1.1      manu 		case 'h':
    171        1.5  christos 			if (uid) {
    172        1.5  christos 				errno = EPERM;
    173        1.5  christos 				err(EXIT_FAILURE, "-h option");
    174        1.5  christos 			}
    175        1.1      manu 			if (domain && (p = strchr(optarg, '.')) != NULL &&
    176        1.1      manu 			    strcasecmp(p, domain) == 0)
    177        1.1      manu 				*p = '\0';
    178        1.1      manu 			hostname = optarg;
    179        1.1      manu 			break;
    180        1.1      manu 		case 'p':
    181        1.1      manu 			pflag = 1;
    182        1.1      manu 			break;
    183        1.1      manu 		default:
    184        1.1      manu 		case '?':
    185        1.1      manu 			usage();
    186        1.1      manu 			break;
    187        1.1      manu 		}
    188        1.3  christos 
    189        1.3  christos 	setproctitle(NULL);
    190        1.1      manu 	argc -= optind;
    191        1.1      manu 	argv += optind;
    192        1.1      manu 
    193        1.1      manu 	if (*argv) {
    194       1.21  christos 		username = trimloginname(*argv);
    195        1.1      manu 		ask = 0;
    196        1.1      manu 	} else
    197        1.1      manu 		ask = 1;
    198        1.1      manu 
    199        1.3  christos #ifdef F_CLOSEM
    200        1.3  christos 	(void)fcntl(3, F_CLOSEM, 0);
    201        1.3  christos #else
    202        1.1      manu 	for (cnt = getdtablesize(); cnt > 2; cnt--)
    203        1.1      manu 		(void)close(cnt);
    204        1.3  christos #endif
    205        1.1      manu 
    206        1.1      manu 	ttyn = ttyname(STDIN_FILENO);
    207        1.1      manu 	if (ttyn == NULL || *ttyn == '\0') {
    208        1.1      manu 		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
    209        1.1      manu 		ttyn = tname;
    210        1.1      manu 	}
    211        1.3  christos 	if ((tty = strstr(ttyn, "/pts/")) != NULL)
    212        1.3  christos 		++tty;
    213        1.3  christos 	else if ((tty = strrchr(ttyn, '/')) != NULL)
    214        1.1      manu 		++tty;
    215        1.1      manu 	else
    216        1.1      manu 		tty = ttyn;
    217        1.1      manu 
    218        1.1      manu 	if (issetugid()) {
    219        1.1      manu 		nested = strdup(user_from_uid(getuid(), 0));
    220        1.1      manu 		if (nested == NULL) {
    221        1.1      manu                 	syslog(LOG_ERR, "strdup: %m");
    222        1.5  christos                 	sleepexit(EXIT_FAILURE);
    223        1.1      manu 		}
    224        1.1      manu 	}
    225        1.1      manu 
    226        1.1      manu 	/* Get "login-retries" and "login-backoff" from default class */
    227        1.1      manu 	if ((lc = login_getclass(NULL)) != NULL) {
    228        1.1      manu 		login_retries = (int)login_getcapnum(lc, "login-retries",
    229        1.1      manu 		    DEFAULT_RETRIES, DEFAULT_RETRIES);
    230        1.1      manu 		login_backoff = (int)login_getcapnum(lc, "login-backoff",
    231        1.1      manu 		    DEFAULT_BACKOFF, DEFAULT_BACKOFF);
    232        1.1      manu 		login_close(lc);
    233        1.1      manu 		lc = NULL;
    234        1.1      manu 	}
    235        1.1      manu 
    236        1.1      manu 
    237        1.1      manu 	for (cnt = 0;; ask = 1) {
    238        1.1      manu 		if (ask) {
    239        1.1      manu 			fflag = 0;
    240       1.22    martin 			username = trimloginname(getloginname());
    241        1.1      manu 		}
    242        1.1      manu 		rootlogin = 0;
    243        1.1      manu 		auth_passed = 0;
    244        1.1      manu 
    245        1.1      manu 		/*
    246        1.1      manu 		 * Note if trying multiple user names; log failures for
    247        1.1      manu 		 * previous user name, but don't bother logging one failure
    248        1.1      manu 		 * for nonexistent name (mistyped username).
    249        1.1      manu 		 */
    250        1.1      manu 		if (failures && strcmp(tbuf, username)) {
    251        1.1      manu 			if (failures > (pwd ? 0 : 1))
    252        1.1      manu 				badlogin(tbuf);
    253        1.1      manu 			failures = 0;
    254        1.1      manu 		}
    255        1.1      manu 
    256        1.1      manu #define PAM_END(msg) do { 						\
    257        1.1      manu 	syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err)); 	\
    258        1.1      manu 	warnx("%s: %s", msg, pam_strerror(pamh, pam_err));		\
    259        1.1      manu 	pam_end(pamh, pam_err);						\
    260        1.5  christos 	sleepexit(EXIT_FAILURE);					\
    261        1.1      manu } while (/*CONSTCOND*/0)
    262        1.1      manu 
    263        1.1      manu 		pam_err = pam_start("login", username, &pamc, &pamh);
    264        1.1      manu 		if (pam_err != PAM_SUCCESS) {
    265        1.1      manu 			if (pamh != NULL)
    266        1.1      manu 				PAM_END("pam_start");
    267        1.1      manu 			/* Things went really bad... */
    268        1.1      manu 			syslog(LOG_ERR, "pam_start failed: %s",
    269        1.1      manu 			    pam_strerror(pamh, pam_err));
    270        1.5  christos 			errx(EXIT_FAILURE, "pam_start failed");
    271        1.1      manu 		}
    272        1.1      manu 
    273        1.1      manu #define PAM_SET_ITEM(item, var)	do {					\
    274        1.1      manu 	pam_err = pam_set_item(pamh, (item), (var));			\
    275        1.1      manu 	if (pam_err != PAM_SUCCESS)					\
    276        1.1      manu 		PAM_END("pam_set_item(" # item ")");			\
    277        1.1      manu } while (/*CONSTCOND*/0)
    278        1.1      manu 
    279        1.1      manu 		/*
    280        1.4  christos 		 * Fill hostname tty, and nested user
    281        1.1      manu 		 */
    282        1.1      manu 		PAM_SET_ITEM(PAM_RHOST, hostname);
    283        1.1      manu 		PAM_SET_ITEM(PAM_TTY, tty);
    284        1.4  christos 		if (nested)
    285        1.4  christos 			PAM_SET_ITEM(PAM_NUSER, nested);
    286        1.2  christos 		if (have_ss)
    287        1.2  christos 			PAM_SET_ITEM(PAM_SOCKADDR, &ss);
    288        1.1      manu 
    289        1.6  christos 		/*
    290        1.6  christos 		 * Don't check for errors, because we don't want to give
    291        1.6  christos 		 * out any information.
    292        1.6  christos 		 */
    293        1.7     lukem 		pwd = NULL;
    294        1.6  christos 		(void)getpwnam_r(username, &pwres, pwbuf, sizeof(pwbuf), &pwd);
    295        1.1      manu 
    296        1.1      manu 		/*
    297        1.1      manu 		 * Establish the class now, before we might goto
    298        1.1      manu 		 * within the next block. pwd can be NULL since it
    299        1.1      manu 		 * falls back to the "default" class if it is.
    300        1.1      manu 		 */
    301        1.1      manu 		lc = login_getclass(pwd ? pwd->pw_class : NULL);
    302        1.1      manu 
    303        1.1      manu 		/*
    304        1.1      manu 		 * if we have a valid account name, and it doesn't have a
    305        1.1      manu 		 * password, or the -f option was specified and the caller
    306        1.1      manu 		 * is root or the caller isn't changing their uid, don't
    307        1.1      manu 		 * authenticate.
    308        1.1      manu 		 */
    309        1.1      manu 		if (pwd) {
    310        1.1      manu 			if (pwd->pw_uid == 0)
    311        1.1      manu 				rootlogin = 1;
    312        1.1      manu 
    313        1.1      manu 			if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
    314        1.1      manu 				/* already authenticated */
    315        1.1      manu 				auth_passed = 1;
    316        1.1      manu 				goto skip_auth;
    317        1.1      manu 			}
    318        1.1      manu 		}
    319        1.1      manu 
    320        1.1      manu 		(void)setpriority(PRIO_PROCESS, 0, -4);
    321        1.1      manu 
    322        1.1      manu 		switch(pam_err = pam_authenticate(pamh, pam_silent)) {
    323        1.1      manu 		case PAM_SUCCESS:
    324        1.1      manu 			/*
    325        1.1      manu 			 * PAM can change the user, refresh
    326        1.1      manu 			 * username, pwd, and lc.
    327        1.1      manu 			 */
    328        1.1      manu 			pam_err = pam_get_item(pamh, PAM_USER, &newuser);
    329        1.1      manu 			if (pam_err != PAM_SUCCESS)
    330        1.1      manu 				PAM_END("pam_get_item(PAM_USER)");
    331        1.1      manu 
    332       1.21  christos 			username = newuser;
    333        1.6  christos 			/*
    334        1.6  christos 			 * Don't check for errors, because we don't want to give
    335        1.6  christos 			 * out any information.
    336        1.6  christos 			 */
    337        1.7     lukem 			pwd = NULL;
    338        1.6  christos 			(void)getpwnam_r(username, &pwres, pwbuf, sizeof(pwbuf),
    339        1.6  christos 			    &pwd);
    340        1.1      manu 			lc = login_getpwclass(pwd);
    341        1.1      manu 			auth_passed = 1;
    342        1.1      manu 
    343        1.1      manu 			switch (pam_err = pam_acct_mgmt(pamh, pam_silent)) {
    344        1.1      manu 			case PAM_SUCCESS:
    345        1.1      manu 				break;
    346        1.1      manu 
    347        1.1      manu 			case PAM_NEW_AUTHTOK_REQD:
    348        1.1      manu 				pam_err = pam_chauthtok(pamh,
    349        1.1      manu 				    pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
    350        1.1      manu 
    351        1.1      manu 				if (pam_err != PAM_SUCCESS)
    352        1.1      manu 					PAM_END("pam_chauthtok");
    353        1.1      manu 				break;
    354        1.1      manu 
    355       1.13   jnemeth 			case PAM_AUTH_ERR:
    356       1.13   jnemeth 			case PAM_USER_UNKNOWN:
    357       1.13   jnemeth 			case PAM_MAXTRIES:
    358       1.13   jnemeth 				auth_passed = 0;
    359       1.13   jnemeth 				break;
    360       1.13   jnemeth 
    361        1.1      manu 			default:
    362        1.1      manu 				PAM_END("pam_acct_mgmt");
    363        1.1      manu 				break;
    364        1.1      manu 			}
    365        1.1      manu 			break;
    366        1.1      manu 
    367        1.1      manu 		case PAM_AUTH_ERR:
    368        1.1      manu 		case PAM_USER_UNKNOWN:
    369        1.1      manu 		case PAM_MAXTRIES:
    370        1.1      manu 			auth_passed = 0;
    371        1.1      manu 			break;
    372        1.1      manu 
    373        1.1      manu 		default:
    374        1.1      manu 			PAM_END("pam_authenticate");
    375        1.1      manu 			break;
    376        1.1      manu 		}
    377        1.1      manu 
    378        1.1      manu 		(void)setpriority(PRIO_PROCESS, 0, 0);
    379        1.1      manu 
    380        1.1      manu skip_auth:
    381        1.1      manu 		/*
    382        1.1      manu 		 * If the user exists and authentication passed,
    383        1.1      manu 		 * get out of the loop and login the user.
    384        1.1      manu 		 */
    385        1.1      manu 		if (pwd && auth_passed)
    386        1.1      manu 			break;
    387        1.1      manu 
    388       1.13   jnemeth 		(void)printf("Login incorrect or refused on this terminal.\n");
    389        1.1      manu 		failures++;
    390        1.1      manu 		cnt++;
    391       1.14   jnemeth 		/*
    392       1.14   jnemeth 		 * We allow login_retries tries, but after login_backoff
    393       1.14   jnemeth 		 * we start backing off.  These default to 10 and 3
    394       1.14   jnemeth 		 * respectively.
    395       1.14   jnemeth 		 */
    396        1.1      manu 		if (cnt > login_backoff) {
    397        1.1      manu 			if (cnt >= login_retries) {
    398        1.1      manu 				badlogin(username);
    399        1.1      manu 				pam_end(pamh, PAM_SUCCESS);
    400        1.5  christos 				sleepexit(EXIT_FAILURE);
    401        1.1      manu 			}
    402       1.14   jnemeth 			sleep((u_int)((cnt - login_backoff) * 5));
    403        1.1      manu 		}
    404        1.1      manu 	}
    405        1.1      manu 
    406        1.1      manu 	/* committed to login -- turn off timeout */
    407        1.1      manu 	(void)alarm((u_int)0);
    408        1.1      manu 
    409        1.1      manu 	endpwent();
    410        1.1      manu 
    411        1.1      manu         quietlog = login_getcapbool(lc, "hushlogin", 0);
    412        1.1      manu 
    413        1.1      manu 	/*
    414        1.1      manu 	 * Temporarily give up special privileges so we can change
    415        1.1      manu 	 * into NFS-mounted homes that are exported for non-root
    416        1.1      manu 	 * access and have mode 7x0
    417        1.1      manu 	 */
    418        1.1      manu 	saved_uid = geteuid();
    419        1.1      manu 	saved_gid = getegid();
    420        1.1      manu 	nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
    421        1.1      manu 
    422        1.1      manu 	(void)setegid(pwd->pw_gid);
    423       1.25       shm 	if (initgroups(username, pwd->pw_gid) == -1) {
    424       1.25       shm 		syslog(LOG_ERR, "initgroups failed");
    425       1.25       shm 		pam_end(pamh, PAM_SUCCESS);
    426       1.25       shm 		exit(EXIT_FAILURE);
    427       1.25       shm 	}
    428        1.1      manu 	(void)seteuid(pwd->pw_uid);
    429        1.1      manu 
    430        1.1      manu 	if (chdir(pwd->pw_dir) != 0) {
    431        1.1      manu                 if (login_getcapbool(lc, "requirehome", 0)) {
    432        1.1      manu 			(void)printf("Home directory %s required\n",
    433        1.1      manu 			    pwd->pw_dir);
    434        1.1      manu 			pam_end(pamh, PAM_SUCCESS);
    435        1.5  christos                         exit(EXIT_FAILURE);
    436        1.1      manu 		}
    437        1.1      manu 
    438        1.1      manu 		(void)printf("No home directory %s!\n", pwd->pw_dir);
    439       1.18  christos 		if (chdir("/") == -1) {
    440        1.1      manu 			pam_end(pamh, PAM_SUCCESS);
    441       1.18  christos 			exit(EXIT_FAILURE);
    442        1.1      manu 		}
    443       1.21  christos 		pwd->pw_dir = __UNCONST("/");
    444        1.1      manu 		(void)printf("Logging in with home = \"/\".\n");
    445        1.1      manu 	}
    446        1.1      manu 
    447        1.1      manu 	if (!quietlog) {
    448        1.1      manu 		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
    449       1.11  christos 		pam_silent = quietlog ? PAM_SILENT : 0;
    450        1.1      manu 	}
    451        1.1      manu 
    452        1.1      manu 	/* regain special privileges */
    453       1.25       shm 	(void)setegid(saved_gid);
    454       1.25       shm 	(void)seteuid(saved_uid);
    455       1.25       shm 	if (setgroups(nsaved_gids, saved_gids) == -1) {
    456       1.25       shm 		syslog(LOG_ERR, "setgroups failed: %m");
    457       1.25       shm 		pam_end(pamh, PAM_SUCCESS);
    458       1.25       shm 		exit(EXIT_FAILURE);
    459       1.25       shm 	}
    460        1.1      manu 
    461        1.9  christos 	(void)getgrnam_r(TTYGRPNAME, &grs, grbuf, sizeof(grbuf), &grp);
    462        1.3  christos 	(void)chown(ttyn, pwd->pw_uid,
    463        1.9  christos 	    (grp != NULL) ? grp->gr_gid : pwd->pw_gid);
    464        1.3  christos 
    465        1.4  christos 	if (ttyaction(ttyn, "login", pwd->pw_name))
    466        1.4  christos 		(void)printf("Warning: ttyaction failed.\n");
    467        1.4  christos 
    468        1.1      manu 	/* Nothing else left to fail -- really log in. */
    469       1.20  christos         update_db(quietlog, rootlogin, fflag);
    470        1.1      manu 
    471        1.1      manu 	if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
    472        1.1      manu 	    LOGIN_SETLOGIN) != 0) {
    473        1.1      manu 		syslog(LOG_ERR, "setusercontext failed");
    474        1.1      manu 		pam_end(pamh, PAM_SUCCESS);
    475        1.5  christos 		exit(EXIT_FAILURE);
    476        1.1      manu 	}
    477        1.1      manu 
    478        1.1      manu 	/*
    479        1.1      manu 	 * Establish groups
    480        1.1      manu 	 */
    481        1.1      manu 	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
    482        1.1      manu 		syslog(LOG_ERR, "setusercontext failed");
    483        1.1      manu 		pam_end(pamh, PAM_SUCCESS);
    484        1.5  christos 		exit(EXIT_FAILURE);
    485        1.1      manu 	}
    486        1.1      manu 
    487        1.1      manu 	pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
    488        1.1      manu 	if (pam_err != PAM_SUCCESS)
    489        1.1      manu 		PAM_END("pam_setcred");
    490        1.1      manu 
    491        1.1      manu 	pam_err = pam_open_session(pamh, pam_silent);
    492        1.1      manu 	if (pam_err != PAM_SUCCESS)
    493        1.1      manu 		PAM_END("pam_open_session");
    494        1.1      manu 
    495        1.1      manu 	/*
    496        1.1      manu 	 * Fork because we need to call pam_closesession as root.
    497        1.1      manu 	 * Make sure signals cannot kill the parent.
    498       1.17  christos 	 * This has been handled in the begining of main.
    499        1.1      manu 	 */
    500        1.1      manu 
    501        1.1      manu 	switch(pid = fork()) {
    502        1.1      manu 	case -1:
    503        1.1      manu 		pam_err = pam_close_session(pamh, 0);
    504        1.1      manu 		if (pam_err != PAM_SUCCESS) {
    505        1.1      manu 			syslog(LOG_ERR, "pam_close_session: %s",
    506        1.1      manu 			    pam_strerror(pamh, pam_err));
    507        1.1      manu 			warnx("pam_close_session: %s",
    508        1.1      manu 			    pam_strerror(pamh, pam_err));
    509        1.1      manu 		}
    510        1.1      manu 		syslog(LOG_ERR, "fork failed: %m");
    511        1.1      manu 		warn("fork failed");
    512        1.1      manu 		pam_end(pamh, pam_err);
    513        1.5  christos 		exit(EXIT_FAILURE);
    514        1.1      manu 		break;
    515        1.1      manu 
    516        1.1      manu 	case 0: /* Child */
    517        1.1      manu 		break;
    518        1.1      manu 
    519        1.1      manu 	default:
    520        1.1      manu 		/*
    521        1.1      manu 		 * Parent: wait for the child to terminate
    522        1.1      manu 		 * and call pam_close_session.
    523        1.1      manu 		 */
    524        1.3  christos 		if ((xpid = waitpid(pid, &status, 0)) != pid) {
    525        1.1      manu 			pam_err = pam_close_session(pamh, 0);
    526        1.1      manu 			if (pam_err != PAM_SUCCESS) {
    527        1.1      manu 				syslog(LOG_ERR,
    528        1.1      manu 				    "pam_close_session: %s",
    529        1.1      manu 				    pam_strerror(pamh, pam_err));
    530        1.1      manu 				warnx("pam_close_session: %s",
    531        1.1      manu 				    pam_strerror(pamh, pam_err));
    532        1.1      manu 			}
    533        1.1      manu 			pam_end(pamh, pam_err);
    534        1.3  christos 			if (xpid != -1)
    535        1.3  christos 				warnx("wrong PID: %d != %d", pid, xpid);
    536        1.3  christos 			else
    537        1.3  christos 				warn("wait for pid %d failed", pid);
    538        1.5  christos 			exit(EXIT_FAILURE);
    539        1.1      manu 		}
    540        1.1      manu 
    541       1.17  christos 		(void)signal(SIGABRT, oabrt);
    542       1.17  christos 		(void)signal(SIGALRM, oalrm);
    543        1.1      manu 		(void)signal(SIGINT, oint);
    544       1.17  christos 		(void)signal(SIGQUIT, oquit);
    545        1.1      manu 		if ((pam_err = pam_close_session(pamh, 0)) != PAM_SUCCESS) {
    546        1.1      manu 			syslog(LOG_ERR, "pam_close_session: %s",
    547        1.1      manu 			    pam_strerror(pamh, pam_err));
    548        1.1      manu 			warnx("pam_close_session: %s",
    549        1.1      manu 			    pam_strerror(pamh, pam_err));
    550        1.1      manu 		}
    551        1.1      manu 		pam_end(pamh, PAM_SUCCESS);
    552        1.5  christos 		exit(EXIT_SUCCESS);
    553        1.1      manu 		break;
    554        1.1      manu 	}
    555        1.1      manu 
    556        1.1      manu 	/*
    557        1.1      manu 	 * The child: starting here, we don't have to care about
    558        1.1      manu 	 * handling PAM issues if we exit, the parent will do the
    559        1.1      manu 	 * job when we exit.
    560        1.1      manu          *
    561        1.1      manu 	 * Destroy environment unless user has requested its preservation.
    562        1.1      manu 	 * Try to preserve TERM anyway.
    563        1.1      manu 	 */
    564        1.1      manu 	saved_term = getenv("TERM");
    565        1.1      manu 	if (!pflag) {
    566        1.1      manu 		environ = envinit;
    567        1.1      manu 		if (saved_term)
    568        1.1      manu 			setenv("TERM", saved_term, 0);
    569        1.1      manu 	}
    570        1.1      manu 
    571        1.1      manu 	if (*pwd->pw_shell == '\0')
    572       1.21  christos 		pwd->pw_shell = __UNCONST(_PATH_BSHELL);
    573        1.1      manu 
    574        1.1      manu 	shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
    575        1.1      manu 	if (*shell == '\0')
    576        1.1      manu 		shell = pwd->pw_shell;
    577        1.1      manu 
    578        1.1      manu 	if ((pwd->pw_shell = strdup(shell)) == NULL) {
    579        1.1      manu 		syslog(LOG_ERR, "Cannot alloc mem");
    580        1.5  christos 		exit(EXIT_FAILURE);
    581        1.1      manu 	}
    582        1.1      manu 
    583        1.1      manu 	(void)setenv("HOME", pwd->pw_dir, 1);
    584        1.1      manu 	(void)setenv("SHELL", pwd->pw_shell, 1);
    585        1.1      manu 	if (term[0] == '\0') {
    586       1.21  christos 		const char *tt = stypeof(tty);
    587        1.1      manu 
    588        1.1      manu 		if (tt == NULL)
    589        1.1      manu 			tt = login_getcapstr(lc, "term", NULL, NULL);
    590        1.1      manu 
    591        1.1      manu 		/* unknown term -> "su" */
    592        1.1      manu 		(void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
    593        1.1      manu 	}
    594        1.1      manu 	(void)setenv("TERM", term, 0);
    595        1.1      manu 	(void)setenv("LOGNAME", pwd->pw_name, 1);
    596        1.1      manu 	(void)setenv("USER", pwd->pw_name, 1);
    597        1.1      manu 
    598        1.1      manu 	/*
    599        1.1      manu 	 * Add PAM environement
    600        1.1      manu 	 */
    601        1.1      manu 	if ((pamenv = pam_getenvlist(pamh)) != NULL) {
    602        1.1      manu 		char **envitem;
    603        1.1      manu 
    604        1.1      manu 		for (envitem = pamenv; *envitem; envitem++) {
    605  1.25.16.1    martin 			if (putenv(*envitem) == -1)
    606  1.25.16.1    martin 				free(*envitem);
    607        1.1      manu 		}
    608        1.1      manu 
    609        1.1      manu 		free(pamenv);
    610        1.1      manu 	}
    611        1.1      manu 
    612        1.1      manu 	/* This drops root privs */
    613        1.1      manu 	if (setusercontext(lc, pwd, pwd->pw_uid,
    614        1.1      manu 	    (LOGIN_SETALL & ~LOGIN_SETLOGIN)) != 0) {
    615        1.1      manu 		syslog(LOG_ERR, "setusercontext failed");
    616        1.5  christos 		exit(EXIT_FAILURE);
    617        1.1      manu 	}
    618        1.1      manu 
    619        1.1      manu 	if (!quietlog) {
    620       1.21  christos 		const char *fname;
    621        1.1      manu 
    622        1.1      manu 		fname = login_getcapstr(lc, "copyright", NULL, NULL);
    623        1.1      manu 		if (fname != NULL && access(fname, F_OK) == 0)
    624        1.1      manu 			motd(fname);
    625        1.1      manu 		else
    626        1.1      manu 			(void)printf("%s", copyrightstr);
    627        1.1      manu 
    628        1.1      manu                 fname = login_getcapstr(lc, "welcome", NULL, NULL);
    629        1.1      manu                 if (fname == NULL || access(fname, F_OK) != 0)
    630        1.1      manu                         fname = _PATH_MOTDFILE;
    631        1.1      manu                 motd(fname);
    632        1.1      manu 
    633        1.1      manu 		(void)snprintf(tbuf,
    634        1.1      manu 		    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
    635        1.1      manu 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
    636        1.1      manu 			(void)printf("You have %smail.\n",
    637        1.1      manu 			    (st.st_mtime > st.st_atime) ? "new " : "");
    638        1.1      manu 	}
    639        1.1      manu 
    640        1.1      manu 	login_close(lc);
    641        1.1      manu 
    642        1.1      manu 
    643        1.1      manu 	tbuf[0] = '-';
    644        1.1      manu 	(void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
    645        1.1      manu 	    p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
    646        1.1      manu 
    647       1.17  christos 	(void)signal(SIGABRT, oabrt);
    648       1.17  christos 	(void)signal(SIGALRM, oalrm);
    649       1.16  christos 	(void)signal(SIGINT, oint);
    650       1.17  christos 	(void)signal(SIGQUIT, oquit);
    651       1.17  christos 	(void)signal(SIGTSTP, SIG_IGN);
    652       1.17  christos 
    653        1.8      matt 	execlp(pwd->pw_shell, tbuf, NULL);
    654        1.5  christos 	err(EXIT_FAILURE, "%s", pwd->pw_shell);
    655        1.1      manu }
    656        1.1      manu 
    657       1.20  christos static void
    658       1.20  christos usage(void)
    659        1.1      manu {
    660       1.20  christos 	(void)fprintf(stderr,
    661       1.20  christos 	    "Usage: %s [-fp] [-a address] [-h hostname] [username]\n",
    662       1.20  christos 	    getprogname());
    663       1.20  christos 	exit(EXIT_FAILURE);
    664        1.1      manu }
    665        1.1      manu 
    666       1.20  christos #if 0
    667       1.20  christos static int
    668        1.1      manu rootterm(char *ttyn)
    669        1.1      manu {
    670        1.1      manu 	struct ttyent *t;
    671        1.1      manu 
    672        1.1      manu 	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
    673        1.1      manu }
    674       1.20  christos #endif
    675