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