Home | History | Annotate | Line # | Download | only in login
login.c revision 1.79
      1  1.79  christos /*     $NetBSD: login.c,v 1.79 2004/11/14 18:01:21 christos Exp $       */
      2  1.12       jtc 
      3   1.1       cgd /*-
      4  1.12       jtc  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
      5  1.10    brezak  *	The Regents of the University of California.  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.73       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.24     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.24     lukem __COPYRIGHT(
     35  1.12       jtc "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
     36  1.24     lukem 	The Regents of the University of California.  All rights reserved.\n");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40  1.12       jtc #if 0
     41  1.12       jtc static char sccsid[] = "@(#)login.c	8.4 (Berkeley) 4/2/94";
     42  1.12       jtc #endif
     43  1.79  christos __RCSID("$NetBSD: login.c,v 1.79 2004/11/14 18:01:21 christos Exp $");
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.1       cgd /*
     47   1.1       cgd  * login [ name ]
     48   1.1       cgd  * login -h hostname	(for telnetd, etc.)
     49   1.1       cgd  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
     50   1.1       cgd  */
     51   1.1       cgd 
     52   1.1       cgd #include <sys/param.h>
     53   1.1       cgd #include <sys/stat.h>
     54   1.1       cgd #include <sys/time.h>
     55   1.1       cgd #include <sys/resource.h>
     56   1.1       cgd #include <sys/file.h>
     57  1.38       mrg #include <sys/wait.h>
     58  1.67  christos #include <sys/socket.h>
     59   1.1       cgd 
     60  1.12       jtc #include <err.h>
     61   1.5       cgd #include <errno.h>
     62   1.1       cgd #include <grp.h>
     63   1.1       cgd #include <pwd.h>
     64  1.12       jtc #include <setjmp.h>
     65  1.12       jtc #include <signal.h>
     66  1.19     veego #include <stdio.h>
     67   1.5       cgd #include <stdlib.h>
     68   1.1       cgd #include <string.h>
     69  1.12       jtc #include <syslog.h>
     70  1.36    kleink #include <time.h>
     71  1.12       jtc #include <ttyent.h>
     72  1.12       jtc #include <tzfile.h>
     73  1.12       jtc #include <unistd.h>
     74  1.67  christos #ifdef SUPPORT_UTMP
     75  1.12       jtc #include <utmp.h>
     76  1.66  christos #endif
     77  1.67  christos #ifdef SUPPORT_UTMPX
     78  1.66  christos #include <utmpx.h>
     79  1.66  christos #endif
     80  1.13       jtc #include <util.h>
     81  1.29   mycroft #ifdef SKEY
     82  1.29   mycroft #include <skey.h>
     83  1.29   mycroft #endif
     84  1.25   mycroft #ifdef KERBEROS5
     85  1.45  christos #include <krb5/krb5.h>
     86  1.54     aidan #include <com_err.h>
     87  1.25   mycroft #endif
     88  1.48       mjl #ifdef LOGIN_CAP
     89  1.48       mjl #include <login_cap.h>
     90  1.48       mjl #endif
     91  1.79  christos #include <vis.h>
     92  1.25   mycroft 
     93   1.1       cgd #include "pathnames.h"
     94   1.1       cgd 
     95  1.57     aidan #ifdef KERBEROS5
     96  1.57     aidan int login_krb5_get_tickets = 1;
     97  1.57     aidan int login_krb4_get_tickets = 0;
     98  1.57     aidan int login_krb5_forwardable_tgt = 0;
     99  1.57     aidan int login_krb5_retain_ccache = 0;
    100  1.57     aidan #endif
    101  1.57     aidan 
    102   1.5       cgd void	 badlogin __P((char *));
    103  1.48       mjl void	 checknologin __P((char *));
    104  1.67  christos #ifdef SUPPORT_UTMP
    105  1.66  christos static void	 doutmp __P((void));
    106  1.66  christos static void	 dolastlog __P((int));
    107  1.66  christos #endif
    108  1.67  christos #ifdef SUPPORT_UTMPX
    109  1.66  christos static void	 doutmpx __P((void));
    110  1.66  christos static void	 dolastlogx __P((int));
    111  1.66  christos #endif
    112  1.66  christos static void	 update_db __P((int));
    113   1.5       cgd void	 getloginname __P((void));
    114  1.24     lukem int	 main __P((int, char *[]));
    115  1.48       mjl void	 motd __P((char *));
    116   1.5       cgd int	 rootterm __P((char *));
    117   1.5       cgd void	 sigint __P((int));
    118   1.5       cgd void	 sleepexit __P((int));
    119  1.39   mycroft const	 char *stypeof __P((const char *));
    120   1.5       cgd void	 timedout __P((int));
    121  1.57     aidan #if defined(KERBEROS)
    122   1.5       cgd int	 klogin __P((struct passwd *, char *, char *, char *));
    123  1.12       jtc void	 kdestroy __P((void));
    124   1.5       cgd #endif
    125  1.44     aidan #ifdef KERBEROS5
    126  1.57     aidan int	 k5login __P((struct passwd *, char *, char *, char *));
    127  1.57     aidan void	 k5destroy __P((void));
    128  1.57     aidan int	 k5_read_creds __P((char*));
    129  1.57     aidan int	 k5_write_creds __P((void));
    130  1.57     aidan #endif
    131  1.57     aidan #if defined(KERBEROS) || defined(KERBEROS5)
    132  1.57     aidan void	 dofork __P((void));
    133  1.44     aidan #endif
    134  1.79  christos void	 decode_ss __P((const char *));
    135  1.79  christos void	 usage __P((void));
    136  1.12       jtc 
    137   1.1       cgd #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
    138   1.1       cgd 
    139  1.50       mjl #define DEFAULT_BACKOFF 3
    140  1.50       mjl #define DEFAULT_RETRIES 10
    141  1.48       mjl 
    142   1.1       cgd /*
    143   1.1       cgd  * This bounds the time given to login.  Not a define so it can
    144   1.1       cgd  * be patched on machines where it's too small.
    145   1.1       cgd  */
    146  1.12       jtc u_int	timeout = 300;
    147   1.5       cgd 
    148  1.10    brezak #if defined(KERBEROS) || defined(KERBEROS5)
    149   1.1       cgd int	notickets = 1;
    150   1.1       cgd char	*instance;
    151  1.57     aidan int	has_ccache = 0;
    152  1.57     aidan #endif
    153  1.57     aidan #ifdef KERBEROS
    154  1.57     aidan extern char	*krbtkfile_env;
    155  1.60   thorpej extern int	krb_configured;
    156   1.1       cgd #endif
    157  1.25   mycroft #ifdef KERBEROS5
    158  1.25   mycroft extern krb5_context kcontext;
    159  1.44     aidan extern int	have_forward;
    160  1.57     aidan extern char	*krb5tkfile_env;
    161  1.60   thorpej extern int	krb5_configured;
    162  1.60   thorpej #endif
    163  1.60   thorpej 
    164  1.60   thorpej #if defined(KERBEROS) && defined(KERBEROS5)
    165  1.60   thorpej #define	KERBEROS_CONFIGURED	(krb_configured || krb5_configured)
    166  1.60   thorpej #elif defined(KERBEROS)
    167  1.60   thorpej #define	KERBEROS_CONFIGURED	krb_configured
    168  1.60   thorpej #elif defined(KERBEROS5)
    169  1.60   thorpej #define	KERBEROS_CONFIGURED	krb5_configured
    170  1.25   mycroft #endif
    171   1.1       cgd 
    172   1.1       cgd struct	passwd *pwd;
    173  1.79  christos int	failures, have_ss;
    174  1.70    itojun char	term[64], *envinit[1], *hostname, *username, *tty, *nested;
    175  1.66  christos struct timeval now;
    176  1.66  christos struct sockaddr_storage ss;
    177   1.1       cgd 
    178  1.77     lukem extern const char copyrightstr[];
    179  1.34   thorpej 
    180   1.5       cgd int
    181   1.1       cgd main(argc, argv)
    182   1.1       cgd 	int argc;
    183   1.5       cgd 	char *argv[];
    184   1.1       cgd {
    185   1.5       cgd 	extern char **environ;
    186   1.5       cgd 	struct group *gr;
    187   1.5       cgd 	struct stat st;
    188  1.29   mycroft 	int ask, ch, cnt, fflag, hflag, pflag, sflag, quietlog, rootlogin, rval;
    189  1.44     aidan 	int Fflag;
    190  1.33   hubertf 	uid_t uid, saved_uid;
    191  1.35   hubertf 	gid_t saved_gid, saved_gids[NGROUPS_MAX];
    192  1.35   hubertf 	int nsaved_gids;
    193  1.39   mycroft 	char *domain, *p, *ttyn, *pwprompt;
    194  1.39   mycroft 	const char *salt;
    195   1.1       cgd 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
    196  1.37       mrg 	char localhost[MAXHOSTNAMELEN + 1];
    197  1.29   mycroft 	int need_chpass, require_chpass;
    198  1.48       mjl 	int login_retries = DEFAULT_RETRIES,
    199  1.48       mjl 	    login_backoff = DEFAULT_BACKOFF;
    200  1.48       mjl 	time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
    201  1.25   mycroft #ifdef KERBEROS5
    202  1.25   mycroft 	krb5_error_code kerror;
    203  1.25   mycroft #endif
    204  1.57     aidan #if defined(KERBEROS) || defined(KERBEROS5)
    205  1.57     aidan 	int got_tickets = 0;
    206  1.57     aidan #endif
    207  1.48       mjl #ifdef LOGIN_CAP
    208  1.49       mjl 	char *shell = NULL;
    209  1.48       mjl 	login_cap_t *lc = NULL;
    210  1.48       mjl #endif
    211   1.1       cgd 
    212  1.20     lukem 	tbuf[0] = '\0';
    213  1.20     lukem 	rval = 0;
    214  1.20     lukem 	pwprompt = NULL;
    215  1.70    itojun 	nested = NULL;
    216  1.29   mycroft 	need_chpass = require_chpass = 0;
    217  1.20     lukem 
    218   1.1       cgd 	(void)signal(SIGALRM, timedout);
    219  1.12       jtc 	(void)alarm(timeout);
    220   1.1       cgd 	(void)signal(SIGQUIT, SIG_IGN);
    221   1.1       cgd 	(void)signal(SIGINT, SIG_IGN);
    222   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, 0);
    223   1.1       cgd 
    224  1.63     lukem 	openlog("login", 0, LOG_AUTH);
    225   1.1       cgd 
    226   1.1       cgd 	/*
    227   1.1       cgd 	 * -p is used by getty to tell login not to destroy the environment
    228   1.5       cgd 	 * -f is used to skip a second login authentication
    229  1.66  christos 	 * -h is used by other servers to pass the name of the remote host to
    230  1.66  christos 	 *    login so that it may be placed in utmp/utmpx and wtmp/wtmpx
    231  1.79  christos 	 * -a in addition to -h, a server my supply -a to pass the actual
    232  1.79  christos 	 *    server address.
    233  1.20     lukem 	 * -s is used to force use of S/Key or equivalent.
    234   1.1       cgd 	 */
    235   1.1       cgd 	domain = NULL;
    236   1.1       cgd 	if (gethostname(localhost, sizeof(localhost)) < 0)
    237   1.1       cgd 		syslog(LOG_ERR, "couldn't get local hostname: %m");
    238   1.1       cgd 	else
    239  1.12       jtc 		domain = strchr(localhost, '.');
    240  1.37       mrg 	localhost[sizeof(localhost) - 1] = '\0';
    241   1.1       cgd 
    242  1.44     aidan 	Fflag = fflag = hflag = pflag = sflag = 0;
    243  1.79  christos 	have_ss = 0;
    244  1.44     aidan #ifdef KERBEROS5
    245  1.44     aidan 	have_forward = 0;
    246  1.44     aidan #endif
    247   1.1       cgd 	uid = getuid();
    248  1.79  christos 	while ((ch = getopt(argc, argv, "a:Ffh:ps")) != -1)
    249   1.1       cgd 		switch (ch) {
    250  1.79  christos 		case 'a':
    251  1.79  christos 			if (uid)
    252  1.79  christos 				errx(1, "-a option: %s", strerror(EPERM));
    253  1.79  christos 			decode_ss(optarg);
    254  1.79  christos 			break;
    255  1.44     aidan 		case 'F':
    256  1.44     aidan 			Fflag = 1;
    257  1.44     aidan 			/* FALLTHROUGH */
    258   1.1       cgd 		case 'f':
    259   1.1       cgd 			fflag = 1;
    260   1.1       cgd 			break;
    261   1.1       cgd 		case 'h':
    262  1.12       jtc 			if (uid)
    263  1.12       jtc 				errx(1, "-h option: %s", strerror(EPERM));
    264   1.1       cgd 			hflag = 1;
    265  1.79  christos #ifdef notdef
    266  1.23     mikel 			if (domain && (p = strchr(optarg, '.')) != NULL &&
    267   1.1       cgd 			    strcasecmp(p, domain) == 0)
    268  1.79  christos 				*p = '\0';
    269  1.79  christos #endif
    270   1.1       cgd 			hostname = optarg;
    271   1.1       cgd 			break;
    272   1.1       cgd 		case 'p':
    273   1.1       cgd 			pflag = 1;
    274   1.1       cgd 			break;
    275  1.20     lukem 		case 's':
    276  1.29   mycroft 			sflag = 1;
    277  1.20     lukem 			break;
    278  1.29   mycroft 		default:
    279   1.1       cgd 		case '?':
    280  1.79  christos 			usage();
    281  1.79  christos 			break;
    282   1.1       cgd 		}
    283   1.1       cgd 	argc -= optind;
    284   1.1       cgd 	argv += optind;
    285   1.5       cgd 
    286   1.1       cgd 	if (*argv) {
    287   1.1       cgd 		username = *argv;
    288   1.1       cgd 		ask = 0;
    289   1.1       cgd 	} else
    290   1.1       cgd 		ask = 1;
    291   1.1       cgd 
    292   1.1       cgd 	for (cnt = getdtablesize(); cnt > 2; cnt--)
    293   1.5       cgd 		(void)close(cnt);
    294   1.1       cgd 
    295   1.5       cgd 	ttyn = ttyname(STDIN_FILENO);
    296   1.1       cgd 	if (ttyn == NULL || *ttyn == '\0') {
    297   1.5       cgd 		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
    298   1.1       cgd 		ttyn = tname;
    299   1.1       cgd 	}
    300  1.23     mikel 	if ((tty = strrchr(ttyn, '/')) != NULL)
    301   1.1       cgd 		++tty;
    302   1.1       cgd 	else
    303   1.1       cgd 		tty = ttyn;
    304  1.25   mycroft 
    305  1.70    itojun 	if (issetugid()) {
    306  1.70    itojun 		nested = strdup(user_from_uid(getuid(), 0));
    307  1.70    itojun 		if (nested == NULL) {
    308  1.70    itojun                 	syslog(LOG_ERR, "strdup: %m");
    309  1.70    itojun                 	sleepexit(1);
    310  1.70    itojun 		}
    311  1.70    itojun 	}
    312  1.70    itojun 
    313  1.48       mjl #ifdef LOGIN_CAP
    314  1.48       mjl 	/* Get "login-retries" and "login-backoff" from default class */
    315  1.55     enami 	if ((lc = login_getclass(NULL)) != NULL) {
    316  1.55     enami 		login_retries = (int)login_getcapnum(lc, "login-retries",
    317  1.55     enami 		    DEFAULT_RETRIES, DEFAULT_RETRIES);
    318  1.55     enami 		login_backoff = (int)login_getcapnum(lc, "login-backoff",
    319  1.55     enami 		    DEFAULT_BACKOFF, DEFAULT_BACKOFF);
    320  1.48       mjl 		login_close(lc);
    321  1.48       mjl 		lc = NULL;
    322  1.55     enami 	}
    323  1.48       mjl #endif
    324  1.48       mjl 
    325  1.25   mycroft #ifdef KERBEROS5
    326  1.25   mycroft 	kerror = krb5_init_context(&kcontext);
    327  1.25   mycroft 	if (kerror) {
    328  1.61   thorpej 		/*
    329  1.61   thorpej 		 * If Kerberos is not configured, that is, we are
    330  1.61   thorpej 		 * not using Kerberos, do not log the error message.
    331  1.61   thorpej 		 * However, if Kerberos is configured,  and the
    332  1.61   thorpej 		 * context init fails for some other reason, we need
    333  1.61   thorpej 		 * to issue a no tickets warning to the user when the
    334  1.61   thorpej 		 * login succeeds.
    335  1.61   thorpej 		 */
    336  1.61   thorpej 		if (kerror != ENXIO) {	/* XXX NetBSD-local Heimdal hack */
    337  1.61   thorpej 			syslog(LOG_NOTICE,
    338  1.61   thorpej 			    "%s when initializing Kerberos context",
    339  1.61   thorpej 			    error_message(kerror));
    340  1.61   thorpej 			krb5_configured = 1;
    341  1.61   thorpej 		}
    342  1.57     aidan 		login_krb5_get_tickets = 0;
    343  1.58     aidan 	}
    344  1.64       cgd #endif /* KERBEROS5 */
    345   1.1       cgd 
    346   1.1       cgd 	for (cnt = 0;; ask = 1) {
    347  1.57     aidan #if defined(KERBEROS)
    348  1.10    brezak 	        kdestroy();
    349  1.10    brezak #endif
    350  1.57     aidan #if defined(KERBEROS5)
    351  1.57     aidan 		if (login_krb5_get_tickets)
    352  1.57     aidan 			k5destroy();
    353  1.57     aidan #endif
    354   1.1       cgd 		if (ask) {
    355   1.1       cgd 			fflag = 0;
    356   1.1       cgd 			getloginname();
    357   1.1       cgd 		}
    358   1.5       cgd 		rootlogin = 0;
    359  1.29   mycroft #ifdef KERBEROS
    360  1.29   mycroft 		if ((instance = strchr(username, '.')) != NULL)
    361   1.1       cgd 			*instance++ = '\0';
    362  1.29   mycroft 		else
    363   1.1       cgd 			instance = "";
    364   1.1       cgd #endif
    365  1.10    brezak #ifdef KERBEROS5
    366  1.29   mycroft 		if ((instance = strchr(username, '/')) != NULL)
    367  1.10    brezak 			*instance++ = '\0';
    368  1.29   mycroft 		else
    369  1.10    brezak 			instance = "";
    370  1.10    brezak #endif
    371  1.16  sommerfe 		if (strlen(username) > MAXLOGNAME)
    372  1.16  sommerfe 			username[MAXLOGNAME] = '\0';
    373   1.1       cgd 
    374   1.1       cgd 		/*
    375   1.1       cgd 		 * Note if trying multiple user names; log failures for
    376   1.1       cgd 		 * previous user name, but don't bother logging one failure
    377   1.1       cgd 		 * for nonexistent name (mistyped username).
    378   1.1       cgd 		 */
    379   1.1       cgd 		if (failures && strcmp(tbuf, username)) {
    380   1.1       cgd 			if (failures > (pwd ? 0 : 1))
    381   1.1       cgd 				badlogin(tbuf);
    382   1.1       cgd 			failures = 0;
    383   1.1       cgd 		}
    384  1.71    itojun 		(void)strlcpy(tbuf, username, sizeof(tbuf));
    385   1.1       cgd 
    386  1.23     mikel 		if ((pwd = getpwnam(username)) != NULL)
    387   1.1       cgd 			salt = pwd->pw_passwd;
    388   1.1       cgd 		else
    389   1.1       cgd 			salt = "xx";
    390   1.1       cgd 
    391  1.48       mjl #ifdef LOGIN_CAP
    392  1.48       mjl 		/*
    393  1.48       mjl 		 * Establish the class now, before we might goto
    394  1.48       mjl 		 * within the next block. pwd can be NULL since it
    395  1.52       mjl 		 * falls back to the "default" class if it is.
    396  1.52       mjl 		 */
    397  1.52       mjl 		lc = login_getclass(pwd ? pwd->pw_class : NULL);
    398  1.48       mjl #endif
    399   1.1       cgd 		/*
    400   1.1       cgd 		 * if we have a valid account name, and it doesn't have a
    401   1.1       cgd 		 * password, or the -f option was specified and the caller
    402   1.1       cgd 		 * is root or the caller isn't changing their uid, don't
    403   1.1       cgd 		 * authenticate.
    404   1.1       cgd 		 */
    405   1.7   mycroft 		if (pwd) {
    406   1.7   mycroft 			if (pwd->pw_uid == 0)
    407   1.7   mycroft 				rootlogin = 1;
    408   1.7   mycroft 
    409   1.8   mycroft 			if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
    410   1.7   mycroft 				/* already authenticated */
    411  1.44     aidan #ifdef KERBEROS5
    412  1.57     aidan 				if (login_krb5_get_tickets && Fflag)
    413  1.44     aidan 					k5_read_creds(username);
    414  1.44     aidan #endif
    415   1.7   mycroft 				break;
    416   1.7   mycroft 			} else if (pwd->pw_passwd[0] == '\0') {
    417   1.7   mycroft 				/* pretend password okay */
    418   1.7   mycroft 				rval = 0;
    419   1.7   mycroft 				goto ttycheck;
    420   1.7   mycroft 			}
    421   1.7   mycroft 		}
    422   1.7   mycroft 
    423   1.1       cgd 		fflag = 0;
    424   1.1       cgd 
    425   1.1       cgd 		(void)setpriority(PRIO_PROCESS, 0, -4);
    426   1.1       cgd 
    427  1.27   mycroft #ifdef SKEY
    428  1.29   mycroft 		if (skey_haskey(username) == 0) {
    429  1.29   mycroft 			static char skprompt[80];
    430  1.59   thorpej 			const char *skinfo = skey_keyinfo(username);
    431  1.20     lukem 
    432  1.75    itojun 			(void)snprintf(skprompt, sizeof(skprompt),
    433  1.29   mycroft 			    "Password [%s]:",
    434  1.29   mycroft 			    skinfo ? skinfo : "error getting challenge");
    435  1.29   mycroft 			pwprompt = skprompt;
    436  1.29   mycroft 		} else
    437  1.27   mycroft #endif
    438  1.29   mycroft 			pwprompt = "Password:";
    439  1.27   mycroft 
    440  1.29   mycroft 		p = getpass(pwprompt);
    441   1.1       cgd 
    442  1.29   mycroft 		if (pwd == NULL) {
    443  1.29   mycroft 			rval = 1;
    444  1.29   mycroft 			goto skip;
    445  1.29   mycroft 		}
    446  1.29   mycroft #ifdef KERBEROS
    447  1.57     aidan 		if (
    448  1.57     aidan #ifdef KERBEROS5
    449  1.57     aidan 		    /* allow a user to get both krb4 and krb5 tickets, if
    450  1.57     aidan 		     * desired.  If krb5 is compiled in, the default action
    451  1.57     aidan 		     * is to ignore krb4 and get krb5 tickets, but the user
    452  1.57     aidan 		     * can override this in the krb5.conf. */
    453  1.57     aidan 		    login_krb4_get_tickets &&
    454  1.57     aidan #endif
    455  1.57     aidan 		    klogin(pwd, instance, localhost, p) == 0) {
    456  1.29   mycroft 			rval = 0;
    457  1.57     aidan 			got_tickets = 1;
    458  1.29   mycroft 		}
    459   1.1       cgd #endif
    460  1.29   mycroft #ifdef KERBEROS5
    461  1.57     aidan 		if (login_krb5_get_tickets &&
    462  1.57     aidan 		    k5login(pwd, instance, localhost, p) == 0) {
    463  1.29   mycroft 			rval = 0;
    464  1.57     aidan 			got_tickets = 1;
    465  1.57     aidan 		}
    466  1.57     aidan #endif
    467  1.57     aidan #if defined(KERBEROS) || defined(KERBEROS5)
    468  1.57     aidan 		if (got_tickets)
    469  1.29   mycroft 			goto skip;
    470  1.29   mycroft #endif
    471  1.20     lukem #ifdef SKEY
    472  1.29   mycroft 		if (skey_haskey(username) == 0 &&
    473  1.29   mycroft 		    skey_passcheck(username, p) != -1) {
    474  1.29   mycroft 			rval = 0;
    475  1.29   mycroft 			goto skip;
    476  1.29   mycroft 		}
    477  1.20     lukem #endif
    478  1.29   mycroft 		if (!sflag && *pwd->pw_passwd != '\0' &&
    479  1.29   mycroft 		    !strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd)) {
    480  1.29   mycroft 			rval = 0;
    481  1.29   mycroft 			require_chpass = 1;
    482  1.29   mycroft 			goto skip;
    483  1.29   mycroft 		}
    484  1.29   mycroft 		rval = 1;
    485  1.29   mycroft 
    486  1.29   mycroft 	skip:
    487  1.29   mycroft 		memset(p, 0, strlen(p));
    488   1.1       cgd 
    489   1.1       cgd 		(void)setpriority(PRIO_PROCESS, 0, 0);
    490   1.1       cgd 
    491   1.7   mycroft 	ttycheck:
    492   1.1       cgd 		/*
    493   1.1       cgd 		 * If trying to log in as root without Kerberos,
    494   1.1       cgd 		 * but with insecure terminal, refuse the login attempt.
    495   1.1       cgd 		 */
    496   1.7   mycroft 		if (pwd && !rval && rootlogin && !rootterm(tty)) {
    497   1.1       cgd 			(void)fprintf(stderr,
    498   1.1       cgd 			    "%s login refused on this terminal.\n",
    499   1.1       cgd 			    pwd->pw_name);
    500   1.1       cgd 			if (hostname)
    501   1.1       cgd 				syslog(LOG_NOTICE,
    502   1.1       cgd 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
    503   1.1       cgd 				    pwd->pw_name, hostname, tty);
    504   1.1       cgd 			else
    505   1.1       cgd 				syslog(LOG_NOTICE,
    506   1.1       cgd 				    "LOGIN %s REFUSED ON TTY %s",
    507   1.1       cgd 				     pwd->pw_name, tty);
    508   1.1       cgd 			continue;
    509   1.1       cgd 		}
    510   1.1       cgd 
    511   1.1       cgd 		if (pwd && !rval)
    512   1.1       cgd 			break;
    513   1.1       cgd 
    514   1.1       cgd 		(void)printf("Login incorrect\n");
    515   1.1       cgd 		failures++;
    516  1.48       mjl 		cnt++;
    517   1.1       cgd 		/* we allow 10 tries, but after 3 we start backing off */
    518  1.50       mjl 		if (cnt > login_backoff) {
    519  1.48       mjl 			if (cnt >= login_retries) {
    520   1.1       cgd 				badlogin(username);
    521   1.1       cgd 				sleepexit(1);
    522   1.1       cgd 			}
    523   1.1       cgd 			sleep((u_int)((cnt - 3) * 5));
    524   1.1       cgd 		}
    525   1.1       cgd 	}
    526   1.1       cgd 
    527   1.1       cgd 	/* committed to login -- turn off timeout */
    528   1.1       cgd 	(void)alarm((u_int)0);
    529   1.1       cgd 
    530   1.1       cgd 	endpwent();
    531   1.1       cgd 
    532   1.1       cgd 	/* if user not super-user, check for disabled logins */
    533  1.48       mjl #ifdef LOGIN_CAP
    534  1.56     enami         if (!login_getcapbool(lc, "ignorenologin", rootlogin))
    535  1.52       mjl 		checknologin(login_getcapstr(lc, "nologin", NULL, NULL));
    536  1.48       mjl #else
    537  1.48       mjl         if (!rootlogin)
    538  1.48       mjl                 checknologin(NULL);
    539  1.48       mjl #endif
    540   1.1       cgd 
    541  1.48       mjl #ifdef LOGIN_CAP
    542  1.48       mjl         quietlog = login_getcapbool(lc, "hushlogin", 0);
    543  1.48       mjl #else
    544  1.48       mjl         quietlog = 0;
    545  1.48       mjl #endif
    546  1.33   hubertf 	/* Temporarily give up special privileges so we can change */
    547  1.33   hubertf 	/* into NFS-mounted homes that are exported for non-root */
    548  1.33   hubertf 	/* access and have mode 7x0 */
    549  1.33   hubertf 	saved_uid = geteuid();
    550  1.35   hubertf 	saved_gid = getegid();
    551  1.35   hubertf 	nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
    552  1.35   hubertf 
    553  1.35   hubertf 	(void)setegid(pwd->pw_gid);
    554  1.35   hubertf 	initgroups(username, pwd->pw_gid);
    555  1.35   hubertf 	(void)seteuid(pwd->pw_uid);
    556  1.33   hubertf 
    557   1.1       cgd 	if (chdir(pwd->pw_dir) < 0) {
    558  1.48       mjl #ifdef LOGIN_CAP
    559  1.48       mjl                 if (login_getcapbool(lc, "requirehome", 0)) {
    560  1.55     enami 			(void)printf("Home directory %s required\n",
    561  1.55     enami 			    pwd->pw_dir);
    562  1.48       mjl                         sleepexit(1);
    563  1.48       mjl 		}
    564  1.48       mjl #endif
    565   1.1       cgd 		(void)printf("No home directory %s!\n", pwd->pw_dir);
    566   1.1       cgd 		if (chdir("/"))
    567   1.1       cgd 			exit(0);
    568   1.1       cgd 		pwd->pw_dir = "/";
    569   1.1       cgd 		(void)printf("Logging in with home = \"/\".\n");
    570   1.1       cgd 	}
    571   1.1       cgd 
    572  1.55     enami 	if (!quietlog)
    573  1.48       mjl 		quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
    574  1.33   hubertf 
    575  1.33   hubertf 	/* regain special privileges */
    576  1.33   hubertf 	(void)seteuid(saved_uid);
    577  1.35   hubertf 	setgroups(nsaved_gids, saved_gids);
    578  1.35   hubertf 	(void)setegid(saved_gid);
    579   1.1       cgd 
    580  1.48       mjl #ifdef LOGIN_CAP
    581  1.48       mjl         pw_warntime = login_getcaptime(lc, "password-warn",
    582  1.55     enami             _PASSWORD_WARNDAYS * SECSPERDAY,
    583  1.55     enami             _PASSWORD_WARNDAYS * SECSPERDAY);
    584  1.48       mjl #endif
    585  1.48       mjl 
    586  1.66  christos 	(void)gettimeofday(&now, (struct timezone *)NULL);
    587  1.40      ross 	if (pwd->pw_expire) {
    588  1.66  christos 		if (now.tv_sec >= pwd->pw_expire) {
    589   1.1       cgd 			(void)printf("Sorry -- your account has expired.\n");
    590   1.1       cgd 			sleepexit(1);
    591  1.66  christos 		} else if (pwd->pw_expire - now.tv_sec < pw_warntime &&
    592  1.55     enami 		    !quietlog)
    593   1.1       cgd 			(void)printf("Warning: your account expires on %s",
    594   1.1       cgd 			    ctime(&pwd->pw_expire));
    595  1.40      ross 	}
    596  1.40      ross 	if (pwd->pw_change) {
    597  1.30   mycroft 		if (pwd->pw_change == _PASSWORD_CHGNOW)
    598  1.30   mycroft 			need_chpass = 1;
    599  1.66  christos 		else if (now.tv_sec >= pwd->pw_change) {
    600  1.31   mycroft 			(void)printf("Sorry -- your password has expired.\n");
    601  1.31   mycroft 			sleepexit(1);
    602  1.66  christos 		} else if (pwd->pw_change - now.tv_sec < pw_warntime &&
    603  1.55     enami 		    !quietlog)
    604  1.30   mycroft 			(void)printf("Warning: your password expires on %s",
    605  1.30   mycroft 			    ctime(&pwd->pw_change));
    606   1.1       cgd 
    607  1.40      ross 	}
    608   1.5       cgd 	/* Nothing else left to fail -- really log in. */
    609  1.66  christos 	update_db(quietlog);
    610   1.1       cgd 
    611   1.1       cgd 	(void)chown(ttyn, pwd->pw_uid,
    612   1.1       cgd 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
    613  1.15       gwr 
    614  1.15       gwr 	if (ttyaction(ttyn, "login", pwd->pw_name))
    615  1.15       gwr 		(void)printf("Warning: ttyaction failed.\n");
    616  1.15       gwr 
    617  1.10    brezak #if defined(KERBEROS) || defined(KERBEROS5)
    618  1.10    brezak 	/* Fork so that we can call kdestroy */
    619  1.57     aidan 	if (
    620  1.57     aidan #ifdef KERBEROS5
    621  1.57     aidan 	    ! login_krb5_retain_ccache &&
    622  1.57     aidan #endif
    623  1.57     aidan 	    has_ccache)
    624  1.29   mycroft 		dofork();
    625  1.10    brezak #endif
    626  1.53       mjl 
    627  1.53       mjl 	/* Destroy environment unless user has requested its preservation. */
    628  1.53       mjl 	if (!pflag)
    629  1.53       mjl 		environ = envinit;
    630  1.53       mjl 
    631  1.48       mjl #ifdef LOGIN_CAP
    632  1.70    itojun 	if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
    633  1.70    itojun 	    LOGIN_SETLOGIN) != 0) {
    634  1.70    itojun 		syslog(LOG_ERR, "setusercontext failed");
    635  1.70    itojun 		exit(1);
    636  1.70    itojun 	}
    637  1.70    itojun 	if (setusercontext(lc, pwd, pwd->pw_uid,
    638  1.70    itojun 	    (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETLOGIN))) != 0) {
    639  1.52       mjl 		syslog(LOG_ERR, "setusercontext failed");
    640  1.52       mjl 		exit(1);
    641  1.48       mjl 	}
    642  1.52       mjl #else
    643   1.1       cgd 	(void)setgid(pwd->pw_gid);
    644   1.1       cgd 
    645   1.1       cgd 	initgroups(username, pwd->pw_gid);
    646  1.48       mjl 
    647  1.70    itojun 	if (nested == NULL && setlogin(pwd->pw_name) < 0)
    648  1.48       mjl 		syslog(LOG_ERR, "setlogin() failure: %m");
    649  1.48       mjl 
    650  1.48       mjl 	/* Discard permissions last so can't get killed and drop core. */
    651  1.48       mjl 	if (rootlogin)
    652  1.48       mjl 		(void)setuid(0);
    653  1.48       mjl 	else
    654  1.48       mjl 		(void)setuid(pwd->pw_uid);
    655  1.52       mjl #endif
    656   1.1       cgd 
    657   1.1       cgd 	if (*pwd->pw_shell == '\0')
    658   1.1       cgd 		pwd->pw_shell = _PATH_BSHELL;
    659  1.48       mjl #ifdef LOGIN_CAP
    660  1.55     enami 	if ((shell = login_getcapstr(lc, "shell", NULL, NULL)) != NULL) {
    661  1.55     enami 		if ((shell = strdup(shell)) == NULL) {
    662  1.63     lukem                 	syslog(LOG_ERR, "Cannot alloc mem");
    663  1.48       mjl                 	sleepexit(1);
    664  1.48       mjl 		}
    665  1.48       mjl 		pwd->pw_shell = shell;
    666  1.48       mjl 	}
    667  1.48       mjl #endif
    668  1.48       mjl 
    669   1.1       cgd 	(void)setenv("HOME", pwd->pw_dir, 1);
    670   1.1       cgd 	(void)setenv("SHELL", pwd->pw_shell, 1);
    671  1.48       mjl 	if (term[0] == '\0') {
    672  1.55     enami 		char *tt = (char *)stypeof(tty);
    673  1.48       mjl #ifdef LOGIN_CAP
    674  1.55     enami 		if (tt == NULL)
    675  1.48       mjl 			tt = login_getcapstr(lc, "term", NULL, NULL);
    676  1.48       mjl #endif
    677  1.48       mjl 		/* unknown term -> "su" */
    678  1.71    itojun 		(void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
    679  1.55     enami 	}
    680   1.1       cgd 	(void)setenv("TERM", term, 0);
    681   1.1       cgd 	(void)setenv("LOGNAME", pwd->pw_name, 1);
    682   1.1       cgd 	(void)setenv("USER", pwd->pw_name, 1);
    683  1.51       mjl 
    684  1.48       mjl #ifdef LOGIN_CAP
    685  1.52       mjl 	setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH);
    686  1.48       mjl #else
    687   1.1       cgd 	(void)setenv("PATH", _PATH_DEFPATH, 0);
    688  1.48       mjl #endif
    689  1.51       mjl 
    690   1.1       cgd #ifdef KERBEROS
    691   1.1       cgd 	if (krbtkfile_env)
    692   1.1       cgd 		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
    693   1.1       cgd #endif
    694  1.10    brezak #ifdef KERBEROS5
    695  1.57     aidan 	if (krb5tkfile_env)
    696  1.57     aidan 		(void)setenv("KRB5CCNAME", krb5tkfile_env, 1);
    697  1.10    brezak #endif
    698   1.1       cgd 
    699  1.12       jtc 	if (tty[sizeof("tty")-1] == 'd')
    700  1.12       jtc 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
    701  1.12       jtc 
    702   1.5       cgd 	/* If fflag is on, assume caller/authenticator has logged root login. */
    703  1.40      ross 	if (rootlogin && fflag == 0) {
    704   1.1       cgd 		if (hostname)
    705   1.1       cgd 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
    706   1.1       cgd 			    username, tty, hostname);
    707   1.1       cgd 		else
    708  1.55     enami 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
    709  1.55     enami 			    username, tty);
    710  1.40      ross 	}
    711   1.1       cgd 
    712  1.10    brezak #if defined(KERBEROS) || defined(KERBEROS5)
    713  1.60   thorpej 	if (KERBEROS_CONFIGURED && !quietlog && notickets == 1)
    714   1.1       cgd 		(void)printf("Warning: no Kerberos tickets issued.\n");
    715   1.1       cgd #endif
    716   1.1       cgd 
    717   1.1       cgd 	if (!quietlog) {
    718  1.51       mjl 		char *fname;
    719  1.48       mjl #ifdef LOGIN_CAP
    720  1.48       mjl 		fname = login_getcapstr(lc, "copyright", NULL, NULL);
    721  1.55     enami 		if (fname != NULL && access(fname, F_OK) == 0)
    722  1.48       mjl 			motd(fname);
    723  1.48       mjl 		else
    724  1.55     enami #endif
    725  1.69    itojun 			(void)printf("%s", copyrightstr);
    726  1.48       mjl 
    727  1.48       mjl #ifdef LOGIN_CAP
    728  1.48       mjl                 fname = login_getcapstr(lc, "welcome", NULL, NULL);
    729  1.48       mjl                 if (fname == NULL || access(fname, F_OK) != 0)
    730  1.48       mjl #endif
    731  1.48       mjl                         fname = _PATH_MOTDFILE;
    732  1.48       mjl                 motd(fname);
    733  1.48       mjl 
    734   1.5       cgd 		(void)snprintf(tbuf,
    735   1.5       cgd 		    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
    736   1.1       cgd 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
    737   1.1       cgd 			(void)printf("You have %smail.\n",
    738   1.1       cgd 			    (st.st_mtime > st.st_atime) ? "new " : "");
    739   1.1       cgd 	}
    740   1.1       cgd 
    741  1.48       mjl #ifdef LOGIN_CAP
    742  1.48       mjl 	login_close(lc);
    743  1.48       mjl #endif
    744  1.48       mjl 
    745   1.1       cgd 	(void)signal(SIGALRM, SIG_DFL);
    746   1.1       cgd 	(void)signal(SIGQUIT, SIG_DFL);
    747   1.1       cgd 	(void)signal(SIGINT, SIG_DFL);
    748   1.1       cgd 	(void)signal(SIGTSTP, SIG_IGN);
    749   1.1       cgd 
    750   1.1       cgd 	tbuf[0] = '-';
    751  1.71    itojun 	(void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
    752  1.71    itojun 	    p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
    753   1.1       cgd 
    754  1.24     lukem 	/* Wait to change password until we're unprivileged */
    755  1.24     lukem 	if (need_chpass) {
    756  1.29   mycroft 		if (!require_chpass)
    757  1.24     lukem 			(void)printf(
    758  1.24     lukem "Warning: your password has expired. Please change it as soon as possible.\n");
    759  1.29   mycroft 		else {
    760  1.38       mrg 			int	status;
    761  1.38       mrg 
    762  1.29   mycroft 			(void)printf(
    763  1.24     lukem 		    "Your password has expired. Please choose a new one.\n");
    764  1.38       mrg 			switch (fork()) {
    765  1.38       mrg 			case -1:
    766  1.38       mrg 				warn("fork");
    767  1.29   mycroft 				sleepexit(1);
    768  1.38       mrg 			case 0:
    769  1.38       mrg 				execl(_PATH_BINPASSWD, "passwd", 0);
    770  1.38       mrg 				_exit(1);
    771  1.38       mrg 			default:
    772  1.38       mrg 				if (wait(&status) == -1 ||
    773  1.38       mrg 				    WEXITSTATUS(status))
    774  1.38       mrg 					sleepexit(1);
    775  1.38       mrg 			}
    776  1.29   mycroft 		}
    777  1.24     lukem 	}
    778   1.1       cgd 
    779  1.44     aidan #ifdef KERBEROS5
    780  1.57     aidan 	if (login_krb5_get_tickets)
    781  1.57     aidan 		k5_write_creds();
    782  1.44     aidan #endif
    783   1.1       cgd 	execlp(pwd->pw_shell, tbuf, 0);
    784  1.12       jtc 	err(1, "%s", pwd->pw_shell);
    785   1.1       cgd }
    786  1.57     aidan 
    787  1.10    brezak #if defined(KERBEROS) || defined(KERBEROS5)
    788  1.16  sommerfe #define	NBUFSIZ		(MAXLOGNAME + 1 + 5)	/* .root suffix */
    789   1.1       cgd #else
    790  1.16  sommerfe #define	NBUFSIZ		(MAXLOGNAME + 1)
    791  1.10    brezak #endif
    792  1.10    brezak 
    793  1.10    brezak #if defined(KERBEROS) || defined(KERBEROS5)
    794  1.10    brezak /*
    795  1.10    brezak  * This routine handles cleanup stuff, and the like.
    796  1.10    brezak  * It exists only in the child process.
    797  1.10    brezak  */
    798  1.10    brezak #include <sys/wait.h>
    799  1.10    brezak void
    800  1.10    brezak dofork()
    801  1.10    brezak {
    802  1.38       mrg 	int child;
    803  1.38       mrg 
    804  1.38       mrg 	if (!(child = fork()))
    805  1.38       mrg 		return; /* Child process */
    806  1.38       mrg 
    807  1.55     enami 	/*
    808  1.55     enami 	 * Setup stuff?  This would be things we could do in parallel
    809  1.55     enami 	 * with login
    810  1.55     enami 	 */
    811  1.55     enami 	(void)chdir("/");	/* Let's not keep the fs busy... */
    812  1.10    brezak 
    813  1.38       mrg 	/* If we're the parent, watch the child until it dies */
    814  1.38       mrg 	while (wait(0) != child)
    815  1.38       mrg 		;
    816  1.10    brezak 
    817  1.38       mrg 	/* Cleanup stuff */
    818  1.38       mrg 	/* Run kdestroy to destroy tickets */
    819  1.57     aidan #ifdef KERBEROS
    820  1.38       mrg 	kdestroy();
    821  1.57     aidan #endif
    822  1.57     aidan #ifdef KERBEROS5
    823  1.57     aidan 	if (login_krb5_get_tickets)
    824  1.57     aidan 		k5destroy();
    825  1.57     aidan #endif
    826  1.10    brezak 
    827  1.38       mrg 	/* Leave */
    828  1.38       mrg 	exit(0);
    829  1.10    brezak }
    830   1.1       cgd #endif
    831   1.1       cgd 
    832   1.5       cgd void
    833   1.1       cgd getloginname()
    834   1.1       cgd {
    835  1.12       jtc 	int ch;
    836  1.12       jtc 	char *p;
    837   1.1       cgd 	static char nbuf[NBUFSIZ];
    838   1.1       cgd 
    839   1.1       cgd 	for (;;) {
    840   1.1       cgd 		(void)printf("login: ");
    841   1.1       cgd 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
    842   1.1       cgd 			if (ch == EOF) {
    843   1.1       cgd 				badlogin(username);
    844   1.1       cgd 				exit(0);
    845   1.1       cgd 			}
    846   1.1       cgd 			if (p < nbuf + (NBUFSIZ - 1))
    847   1.1       cgd 				*p++ = ch;
    848   1.1       cgd 		}
    849  1.40      ross 		if (p > nbuf) {
    850   1.1       cgd 			if (nbuf[0] == '-')
    851   1.1       cgd 				(void)fprintf(stderr,
    852   1.1       cgd 				    "login names may not start with '-'.\n");
    853   1.1       cgd 			else {
    854   1.1       cgd 				*p = '\0';
    855   1.1       cgd 				username = nbuf;
    856   1.1       cgd 				break;
    857   1.1       cgd 			}
    858  1.40      ross 		}
    859   1.1       cgd 	}
    860   1.1       cgd }
    861   1.1       cgd 
    862   1.5       cgd int
    863   1.1       cgd rootterm(ttyn)
    864   1.1       cgd 	char *ttyn;
    865   1.1       cgd {
    866   1.1       cgd 	struct ttyent *t;
    867   1.1       cgd 
    868  1.12       jtc 	return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
    869   1.1       cgd }
    870   1.1       cgd 
    871   1.1       cgd jmp_buf motdinterrupt;
    872   1.1       cgd 
    873   1.5       cgd void
    874  1.48       mjl motd(fname)
    875  1.48       mjl 	char *fname;
    876   1.1       cgd {
    877  1.12       jtc 	int fd, nchars;
    878   1.1       cgd 	sig_t oldint;
    879   1.1       cgd 	char tbuf[8192];
    880   1.1       cgd 
    881  1.48       mjl 	if ((fd = open(fname ? fname : _PATH_MOTDFILE, O_RDONLY, 0)) < 0)
    882   1.1       cgd 		return;
    883   1.1       cgd 	oldint = signal(SIGINT, sigint);
    884   1.1       cgd 	if (setjmp(motdinterrupt) == 0)
    885   1.1       cgd 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
    886   1.1       cgd 			(void)write(fileno(stdout), tbuf, nchars);
    887   1.1       cgd 	(void)signal(SIGINT, oldint);
    888   1.1       cgd 	(void)close(fd);
    889   1.1       cgd }
    890   1.1       cgd 
    891   1.5       cgd /* ARGSUSED */
    892   1.1       cgd void
    893   1.5       cgd sigint(signo)
    894   1.5       cgd 	int signo;
    895   1.1       cgd {
    896  1.38       mrg 
    897   1.1       cgd 	longjmp(motdinterrupt, 1);
    898   1.1       cgd }
    899   1.1       cgd 
    900   1.5       cgd /* ARGSUSED */
    901   1.5       cgd void
    902   1.5       cgd timedout(signo)
    903   1.5       cgd 	int signo;
    904   1.5       cgd {
    905  1.38       mrg 
    906   1.5       cgd 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
    907   1.5       cgd 	exit(0);
    908   1.5       cgd }
    909   1.5       cgd 
    910   1.5       cgd void
    911  1.48       mjl checknologin(fname)
    912  1.48       mjl 	char *fname;
    913   1.1       cgd {
    914  1.12       jtc 	int fd, nchars;
    915   1.1       cgd 	char tbuf[8192];
    916   1.1       cgd 
    917  1.48       mjl 	if ((fd = open(fname ? fname : _PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
    918   1.1       cgd 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
    919   1.1       cgd 			(void)write(fileno(stdout), tbuf, nchars);
    920   1.1       cgd 		sleepexit(0);
    921   1.1       cgd 	}
    922   1.1       cgd }
    923   1.1       cgd 
    924  1.66  christos static void
    925  1.66  christos update_db(int quietlog)
    926  1.66  christos {
    927  1.70    itojun 	if (nested != NULL) {
    928  1.70    itojun 		if (hostname != NULL)
    929  1.70    itojun 			syslog(LOG_NOTICE, "%s to %s on tty %s from %s",
    930  1.70    itojun 			    nested, pwd->pw_name, tty, hostname);
    931  1.70    itojun 		else
    932  1.70    itojun 			syslog(LOG_NOTICE, "%s to %s on tty %s", nested,
    933  1.70    itojun 			    pwd->pw_name, tty);
    934  1.70    itojun 
    935  1.70    itojun 		return;
    936  1.70    itojun 	}
    937  1.79  christos 	if (hostname != NULL && have_ss == 0) {
    938  1.66  christos 		socklen_t len = sizeof(ss);
    939  1.66  christos 		(void)getpeername(STDIN_FILENO, (struct sockaddr *)&ss, &len);
    940  1.66  christos 	}
    941  1.66  christos 	(void)gettimeofday(&now, NULL);
    942  1.67  christos #ifdef SUPPORT_UTMPX
    943  1.66  christos 	doutmpx();
    944  1.66  christos 	dolastlogx(quietlog);
    945  1.66  christos 	quietlog = 1;
    946  1.66  christos #endif
    947  1.67  christos #ifdef SUPPORT_UTMP
    948  1.66  christos 	doutmp();
    949  1.66  christos 	dolastlog(quietlog);
    950  1.66  christos #endif
    951  1.66  christos }
    952  1.66  christos 
    953  1.67  christos #ifdef SUPPORT_UTMPX
    954  1.66  christos static void
    955  1.66  christos doutmpx()
    956  1.66  christos {
    957  1.66  christos 	struct utmpx utmpx;
    958  1.66  christos 	char *t;
    959  1.66  christos 
    960  1.66  christos 	memset((void *)&utmpx, 0, sizeof(utmpx));
    961  1.66  christos 	utmpx.ut_tv = now;
    962  1.66  christos 	(void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
    963  1.66  christos 	if (hostname) {
    964  1.66  christos 		(void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
    965  1.66  christos 		utmpx.ut_ss = ss;
    966  1.66  christos 	}
    967  1.66  christos 	(void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
    968  1.66  christos 	utmpx.ut_type = USER_PROCESS;
    969  1.66  christos 	utmpx.ut_pid = getpid();
    970  1.66  christos 	t = tty + strlen(tty);
    971  1.66  christos 	if (t - tty >= sizeof(utmpx.ut_id)) {
    972  1.66  christos 	    (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
    973  1.66  christos 		sizeof(utmpx.ut_id));
    974  1.66  christos 	} else {
    975  1.66  christos 	    (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
    976  1.66  christos 	}
    977  1.66  christos 	if (pututxline(&utmpx) == NULL)
    978  1.66  christos 		syslog(LOG_NOTICE, "Cannot update utmpx %m");
    979  1.66  christos 	endutxent();
    980  1.66  christos 	if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
    981  1.66  christos 		syslog(LOG_NOTICE, "Cannot update wtmpx %m");
    982  1.66  christos }
    983  1.66  christos 
    984  1.66  christos static void
    985  1.66  christos dolastlogx(quiet)
    986  1.66  christos 	int quiet;
    987  1.66  christos {
    988  1.66  christos 	struct lastlogx ll;
    989  1.74       wiz 	if (getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL) {
    990  1.66  christos 		time_t t = (time_t)ll.ll_tv.tv_sec;
    991  1.66  christos 		(void)printf("Last login: %.24s ", ctime(&t));
    992  1.66  christos 		if (*ll.ll_host != '\0')
    993  1.66  christos 			(void)printf("from %.*s ",
    994  1.66  christos 			    (int)sizeof(ll.ll_host),
    995  1.66  christos 			    ll.ll_host);
    996  1.66  christos 		(void)printf("on %.*s\n",
    997  1.66  christos 		    (int)sizeof(ll.ll_line),
    998  1.66  christos 		    ll.ll_line);
    999  1.66  christos 	}
   1000  1.66  christos 	ll.ll_tv = now;
   1001  1.66  christos 	(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
   1002  1.66  christos 	if (hostname) {
   1003  1.66  christos 		(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
   1004  1.66  christos 		ll.ll_ss = ss;
   1005  1.66  christos 	}
   1006  1.66  christos 	if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
   1007  1.66  christos 		syslog(LOG_NOTICE, "Cannot update lastlogx %m");
   1008  1.66  christos }
   1009  1.66  christos #endif
   1010  1.66  christos 
   1011  1.67  christos #ifdef SUPPORT_UTMP
   1012  1.66  christos static void
   1013  1.66  christos doutmp()
   1014  1.66  christos {
   1015  1.66  christos 	struct utmp utmp;
   1016  1.66  christos 
   1017  1.66  christos 	(void)memset((void *)&utmp, 0, sizeof(utmp));
   1018  1.66  christos 	utmp.ut_time = now.tv_sec;
   1019  1.66  christos 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
   1020  1.66  christos 	if (hostname)
   1021  1.66  christos 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
   1022  1.66  christos 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
   1023  1.66  christos 	login(&utmp);
   1024  1.66  christos }
   1025  1.66  christos 
   1026  1.66  christos static void
   1027   1.1       cgd dolastlog(quiet)
   1028   1.1       cgd 	int quiet;
   1029   1.1       cgd {
   1030   1.1       cgd 	struct lastlog ll;
   1031   1.1       cgd 	int fd;
   1032   1.1       cgd 
   1033   1.1       cgd 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
   1034  1.26    kleink 		(void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
   1035   1.1       cgd 		if (!quiet) {
   1036   1.1       cgd 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
   1037   1.1       cgd 			    ll.ll_time != 0) {
   1038  1.66  christos 				(void)printf("Last login: %.24s ",
   1039  1.66  christos 				    ctime(&ll.ll_time));
   1040   1.1       cgd 				if (*ll.ll_host != '\0')
   1041  1.66  christos 					(void)printf("from %.*s ",
   1042   1.5       cgd 					    (int)sizeof(ll.ll_host),
   1043   1.5       cgd 					    ll.ll_host);
   1044  1.66  christos 				(void)printf("on %.*s\n",
   1045  1.66  christos 				    (int)sizeof(ll.ll_line), ll.ll_line);
   1046   1.1       cgd 			}
   1047  1.26    kleink 			(void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)),
   1048  1.26    kleink 			    SEEK_SET);
   1049   1.1       cgd 		}
   1050  1.12       jtc 		memset((void *)&ll, 0, sizeof(ll));
   1051  1.66  christos 		ll.ll_time = now.tv_sec;
   1052   1.5       cgd 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
   1053   1.1       cgd 		if (hostname)
   1054   1.5       cgd 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
   1055   1.1       cgd 		(void)write(fd, (char *)&ll, sizeof(ll));
   1056   1.1       cgd 		(void)close(fd);
   1057   1.1       cgd 	}
   1058   1.1       cgd }
   1059  1.66  christos #endif
   1060   1.1       cgd 
   1061   1.5       cgd void
   1062   1.1       cgd badlogin(name)
   1063   1.1       cgd 	char *name;
   1064   1.1       cgd {
   1065  1.55     enami 
   1066   1.1       cgd 	if (failures == 0)
   1067   1.1       cgd 		return;
   1068   1.1       cgd 	if (hostname) {
   1069   1.1       cgd 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
   1070   1.1       cgd 		    failures, failures > 1 ? "S" : "", hostname);
   1071   1.1       cgd 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
   1072   1.1       cgd 		    "%d LOGIN FAILURE%s FROM %s, %s",
   1073   1.1       cgd 		    failures, failures > 1 ? "S" : "", hostname, name);
   1074   1.1       cgd 	} else {
   1075   1.1       cgd 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
   1076   1.1       cgd 		    failures, failures > 1 ? "S" : "", tty);
   1077   1.1       cgd 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
   1078   1.1       cgd 		    "%d LOGIN FAILURE%s ON %s, %s",
   1079   1.1       cgd 		    failures, failures > 1 ? "S" : "", tty, name);
   1080   1.1       cgd 	}
   1081   1.1       cgd }
   1082   1.1       cgd 
   1083  1.39   mycroft const char *
   1084   1.1       cgd stypeof(ttyid)
   1085  1.39   mycroft 	const char *ttyid;
   1086   1.1       cgd {
   1087   1.1       cgd 	struct ttyent *t;
   1088   1.1       cgd 
   1089  1.48       mjl 	return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : NULL);
   1090   1.1       cgd }
   1091   1.1       cgd 
   1092   1.5       cgd void
   1093   1.1       cgd sleepexit(eval)
   1094   1.1       cgd 	int eval;
   1095   1.1       cgd {
   1096  1.38       mrg 
   1097  1.12       jtc 	(void)sleep(5);
   1098   1.1       cgd 	exit(eval);
   1099   1.1       cgd }
   1100  1.79  christos 
   1101  1.79  christos void
   1102  1.79  christos decode_ss(arg)
   1103  1.79  christos 	const char *arg;
   1104  1.79  christos {
   1105  1.79  christos 	struct sockaddr_storage *ssp;
   1106  1.79  christos 	size_t len = strlen(arg);
   1107  1.79  christos 
   1108  1.79  christos 	if (len > sizeof(*ssp) * 4 + 1 || len < sizeof(*ssp))
   1109  1.79  christos 		errx(1, "Bad argument");
   1110  1.79  christos 
   1111  1.79  christos 	if ((ssp = malloc(len)) == NULL)
   1112  1.79  christos 		err(1, NULL);
   1113  1.79  christos 
   1114  1.79  christos 	if (strunvis((char *)ssp, arg) != sizeof(*ssp))
   1115  1.79  christos 		errx(1, "Decoding error");
   1116  1.79  christos 
   1117  1.79  christos 	(void)memcpy(&ss, ssp, sizeof(ss));
   1118  1.79  christos 	have_ss = 1;
   1119  1.79  christos }
   1120  1.79  christos 
   1121  1.79  christos void
   1122  1.79  christos usage()
   1123  1.79  christos {
   1124  1.79  christos 	(void)fprintf(stderr,
   1125  1.79  christos 	    "Usage: %s [-Ffps] [-a address] [-h hostname] [username]\n",
   1126  1.79  christos 	    getprogname());
   1127  1.79  christos 	exit(1);
   1128  1.79  christos }
   1129