Home | History | Annotate | Line # | Download | only in login
login.c revision 1.9
      1  1.1      cgd /*-
      2  1.1      cgd  * Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University
      3  1.1      cgd  * of California.  All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.5      cgd "@(#) Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University of California.\n\
     37  1.5      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.6  mycroft /*static char sccsid[] = "from: @(#)login.c	5.78 (Berkeley) 6/29/92";*/
     42  1.9  deraadt static char rcsid[] = "$Id: login.c,v 1.9 1994/05/24 06:51:03 deraadt Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd /*
     46  1.1      cgd  * login [ name ]
     47  1.1      cgd  * login -h hostname	(for telnetd, etc.)
     48  1.1      cgd  * login -f name	(for pre-authenticated login: datakit, xterm, etc.)
     49  1.1      cgd  */
     50  1.1      cgd 
     51  1.1      cgd #include <sys/param.h>
     52  1.1      cgd #include <sys/stat.h>
     53  1.1      cgd #include <sys/time.h>
     54  1.1      cgd #include <sys/resource.h>
     55  1.1      cgd #include <sys/file.h>
     56  1.1      cgd 
     57  1.1      cgd #include <signal.h>
     58  1.1      cgd #include <ttyent.h>
     59  1.1      cgd #include <syslog.h>
     60  1.5      cgd #include <setjmp.h>
     61  1.5      cgd #include <tzfile.h>
     62  1.5      cgd #include <utmp.h>
     63  1.5      cgd #include <errno.h>
     64  1.1      cgd #include <grp.h>
     65  1.1      cgd #include <pwd.h>
     66  1.5      cgd #include <unistd.h>
     67  1.1      cgd #include <stdio.h>
     68  1.5      cgd #include <stdlib.h>
     69  1.1      cgd #include <string.h>
     70  1.1      cgd #include "pathnames.h"
     71  1.1      cgd 
     72  1.5      cgd void	 badlogin __P((char *));
     73  1.5      cgd void	 checknologin __P((void));
     74  1.5      cgd void	 dolastlog __P((int));
     75  1.5      cgd void	 getloginname __P((void));
     76  1.5      cgd void	 motd __P((void));
     77  1.5      cgd int	 rootterm __P((char *));
     78  1.5      cgd void	 sigint __P((int));
     79  1.5      cgd void	 sleepexit __P((int));
     80  1.5      cgd char	*stypeof __P((char *));
     81  1.5      cgd void	 timedout __P((int));
     82  1.9  deraadt int	 pwcheck __P((char *, char *, char *, char *));
     83  1.5      cgd #ifdef KERBEROS
     84  1.5      cgd int	 klogin __P((struct passwd *, char *, char *, char *));
     85  1.5      cgd #endif
     86  1.5      cgd 
     87  1.1      cgd #define	TTYGRPNAME	"tty"		/* name of group to own ttys */
     88  1.1      cgd 
     89  1.1      cgd /*
     90  1.1      cgd  * This bounds the time given to login.  Not a define so it can
     91  1.1      cgd  * be patched on machines where it's too small.
     92  1.1      cgd  */
     93  1.1      cgd int	timeout = 300;
     94  1.5      cgd 
     95  1.1      cgd #ifdef KERBEROS
     96  1.1      cgd int	notickets = 1;
     97  1.1      cgd char	*instance;
     98  1.1      cgd char	*krbtkfile_env;
     99  1.1      cgd int	authok;
    100  1.1      cgd #endif
    101  1.1      cgd 
    102  1.1      cgd struct	passwd *pwd;
    103  1.1      cgd int	failures;
    104  1.1      cgd char	term[64], *envinit[1], *hostname, *username, *tty;
    105  1.1      cgd 
    106  1.5      cgd int
    107  1.1      cgd main(argc, argv)
    108  1.1      cgd 	int argc;
    109  1.5      cgd 	char *argv[];
    110  1.1      cgd {
    111  1.5      cgd 	extern char **environ;
    112  1.1      cgd 	register int ch;
    113  1.1      cgd 	register char *p;
    114  1.5      cgd 	struct group *gr;
    115  1.5      cgd 	struct stat st;
    116  1.5      cgd 	struct timeval tp;
    117  1.5      cgd 	struct utmp utmp;
    118  1.5      cgd 	int ask, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval, uid;
    119  1.1      cgd 	char *domain, *salt, *ttyn;
    120  1.1      cgd 	char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
    121  1.1      cgd 	char localhost[MAXHOSTNAMELEN];
    122  1.1      cgd 
    123  1.1      cgd 	(void)signal(SIGALRM, timedout);
    124  1.1      cgd 	(void)alarm((u_int)timeout);
    125  1.1      cgd 	(void)signal(SIGQUIT, SIG_IGN);
    126  1.1      cgd 	(void)signal(SIGINT, SIG_IGN);
    127  1.1      cgd 	(void)setpriority(PRIO_PROCESS, 0, 0);
    128  1.1      cgd 
    129  1.1      cgd 	openlog("login", LOG_ODELAY, LOG_AUTH);
    130  1.1      cgd 
    131  1.1      cgd 	/*
    132  1.1      cgd 	 * -p is used by getty to tell login not to destroy the environment
    133  1.5      cgd 	 * -f is used to skip a second login authentication
    134  1.1      cgd 	 * -h is used by other servers to pass the name of the remote
    135  1.1      cgd 	 *    host to login so that it may be placed in utmp and wtmp
    136  1.1      cgd 	 */
    137  1.1      cgd 	domain = NULL;
    138  1.1      cgd 	if (gethostname(localhost, sizeof(localhost)) < 0)
    139  1.1      cgd 		syslog(LOG_ERR, "couldn't get local hostname: %m");
    140  1.1      cgd 	else
    141  1.1      cgd 		domain = index(localhost, '.');
    142  1.1      cgd 
    143  1.1      cgd 	fflag = hflag = pflag = 0;
    144  1.1      cgd 	uid = getuid();
    145  1.1      cgd 	while ((ch = getopt(argc, argv, "fh:p")) != EOF)
    146  1.1      cgd 		switch (ch) {
    147  1.1      cgd 		case 'f':
    148  1.1      cgd 			fflag = 1;
    149  1.1      cgd 			break;
    150  1.1      cgd 		case 'h':
    151  1.1      cgd 			if (uid) {
    152  1.1      cgd 				(void)fprintf(stderr,
    153  1.1      cgd 				    "login: -h option: %s\n", strerror(EPERM));
    154  1.1      cgd 				exit(1);
    155  1.1      cgd 			}
    156  1.1      cgd 			hflag = 1;
    157  1.1      cgd 			if (domain && (p = index(optarg, '.')) &&
    158  1.1      cgd 			    strcasecmp(p, domain) == 0)
    159  1.1      cgd 				*p = 0;
    160  1.1      cgd 			hostname = optarg;
    161  1.1      cgd 			break;
    162  1.1      cgd 		case 'p':
    163  1.1      cgd 			pflag = 1;
    164  1.1      cgd 			break;
    165  1.1      cgd 		case '?':
    166  1.1      cgd 		default:
    167  1.1      cgd 			if (!uid)
    168  1.1      cgd 				syslog(LOG_ERR, "invalid flag %c", ch);
    169  1.1      cgd 			(void)fprintf(stderr,
    170  1.1      cgd 			    "usage: login [-fp] [-h hostname] [username]\n");
    171  1.1      cgd 			exit(1);
    172  1.1      cgd 		}
    173  1.1      cgd 	argc -= optind;
    174  1.1      cgd 	argv += optind;
    175  1.5      cgd 
    176  1.1      cgd 	if (*argv) {
    177  1.1      cgd 		username = *argv;
    178  1.1      cgd 		ask = 0;
    179  1.1      cgd 	} else
    180  1.1      cgd 		ask = 1;
    181  1.1      cgd 
    182  1.1      cgd 	for (cnt = getdtablesize(); cnt > 2; cnt--)
    183  1.5      cgd 		(void)close(cnt);
    184  1.1      cgd 
    185  1.5      cgd 	ttyn = ttyname(STDIN_FILENO);
    186  1.1      cgd 	if (ttyn == NULL || *ttyn == '\0') {
    187  1.5      cgd 		(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
    188  1.1      cgd 		ttyn = tname;
    189  1.1      cgd 	}
    190  1.1      cgd 	if (tty = rindex(ttyn, '/'))
    191  1.1      cgd 		++tty;
    192  1.1      cgd 	else
    193  1.1      cgd 		tty = ttyn;
    194  1.1      cgd 
    195  1.1      cgd 	for (cnt = 0;; ask = 1) {
    196  1.1      cgd 		if (ask) {
    197  1.1      cgd 			fflag = 0;
    198  1.1      cgd 			getloginname();
    199  1.1      cgd 		}
    200  1.5      cgd 		rootlogin = 0;
    201  1.1      cgd #ifdef	KERBEROS
    202  1.1      cgd 		if ((instance = index(username, '.')) != NULL) {
    203  1.1      cgd 			if (strncmp(instance, ".root", 5) == 0)
    204  1.5      cgd 				rootlogin = 1;
    205  1.1      cgd 			*instance++ = '\0';
    206  1.5      cgd 		} else
    207  1.1      cgd 			instance = "";
    208  1.1      cgd #endif
    209  1.1      cgd 		if (strlen(username) > UT_NAMESIZE)
    210  1.1      cgd 			username[UT_NAMESIZE] = '\0';
    211  1.1      cgd 
    212  1.1      cgd 		/*
    213  1.1      cgd 		 * Note if trying multiple user names; log failures for
    214  1.1      cgd 		 * previous user name, but don't bother logging one failure
    215  1.1      cgd 		 * for nonexistent name (mistyped username).
    216  1.1      cgd 		 */
    217  1.1      cgd 		if (failures && strcmp(tbuf, username)) {
    218  1.1      cgd 			if (failures > (pwd ? 0 : 1))
    219  1.1      cgd 				badlogin(tbuf);
    220  1.1      cgd 			failures = 0;
    221  1.1      cgd 		}
    222  1.1      cgd 		(void)strcpy(tbuf, username);
    223  1.1      cgd 
    224  1.1      cgd 		if (pwd = getpwnam(username))
    225  1.1      cgd 			salt = pwd->pw_passwd;
    226  1.1      cgd 		else
    227  1.1      cgd 			salt = "xx";
    228  1.1      cgd 
    229  1.1      cgd 		/*
    230  1.1      cgd 		 * if we have a valid account name, and it doesn't have a
    231  1.1      cgd 		 * password, or the -f option was specified and the caller
    232  1.1      cgd 		 * is root or the caller isn't changing their uid, don't
    233  1.1      cgd 		 * authenticate.
    234  1.1      cgd 		 */
    235  1.7  mycroft 		if (pwd) {
    236  1.7  mycroft 			if (pwd->pw_uid == 0)
    237  1.7  mycroft 				rootlogin = 1;
    238  1.7  mycroft 
    239  1.8  mycroft 			if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
    240  1.7  mycroft 				/* already authenticated */
    241  1.7  mycroft 				break;
    242  1.7  mycroft 			} else if (pwd->pw_passwd[0] == '\0') {
    243  1.7  mycroft 				/* pretend password okay */
    244  1.7  mycroft 				rval = 0;
    245  1.7  mycroft 				goto ttycheck;
    246  1.7  mycroft 			}
    247  1.7  mycroft 		}
    248  1.7  mycroft 
    249  1.1      cgd 		fflag = 0;
    250  1.1      cgd 
    251  1.1      cgd 		(void)setpriority(PRIO_PROCESS, 0, -4);
    252  1.1      cgd 
    253  1.1      cgd 		p = getpass("Password:");
    254  1.1      cgd 
    255  1.1      cgd 		if (pwd) {
    256  1.1      cgd #ifdef KERBEROS
    257  1.1      cgd 			rval = klogin(pwd, instance, localhost, p);
    258  1.1      cgd 			if (rval == 0)
    259  1.1      cgd 				authok = 1;
    260  1.5      cgd 			else if (rval == 1)
    261  1.9  deraadt 				rval = pwcheck(username, p, salt, pwd->pw_passwd);
    262  1.1      cgd #else
    263  1.9  deraadt 			rval = pwcheck(username, p, salt, pwd->pw_passwd);
    264  1.1      cgd #endif
    265  1.1      cgd 		}
    266  1.1      cgd 		bzero(p, strlen(p));
    267  1.1      cgd 
    268  1.1      cgd 		(void)setpriority(PRIO_PROCESS, 0, 0);
    269  1.1      cgd 
    270  1.7  mycroft 	ttycheck:
    271  1.1      cgd 		/*
    272  1.1      cgd 		 * If trying to log in as root without Kerberos,
    273  1.1      cgd 		 * but with insecure terminal, refuse the login attempt.
    274  1.1      cgd 		 */
    275  1.1      cgd #ifdef KERBEROS
    276  1.1      cgd 		if (authok == 0)
    277  1.1      cgd #endif
    278  1.7  mycroft 		if (pwd && !rval && rootlogin && !rootterm(tty)) {
    279  1.1      cgd 			(void)fprintf(stderr,
    280  1.1      cgd 			    "%s login refused on this terminal.\n",
    281  1.1      cgd 			    pwd->pw_name);
    282  1.1      cgd 			if (hostname)
    283  1.1      cgd 				syslog(LOG_NOTICE,
    284  1.1      cgd 				    "LOGIN %s REFUSED FROM %s ON TTY %s",
    285  1.1      cgd 				    pwd->pw_name, hostname, tty);
    286  1.1      cgd 			else
    287  1.1      cgd 				syslog(LOG_NOTICE,
    288  1.1      cgd 				    "LOGIN %s REFUSED ON TTY %s",
    289  1.1      cgd 				     pwd->pw_name, tty);
    290  1.1      cgd 			continue;
    291  1.1      cgd 		}
    292  1.1      cgd 
    293  1.1      cgd 		if (pwd && !rval)
    294  1.1      cgd 			break;
    295  1.1      cgd 
    296  1.1      cgd 		(void)printf("Login incorrect\n");
    297  1.1      cgd 		failures++;
    298  1.1      cgd 		/* we allow 10 tries, but after 3 we start backing off */
    299  1.1      cgd 		if (++cnt > 3) {
    300  1.1      cgd 			if (cnt >= 10) {
    301  1.1      cgd 				badlogin(username);
    302  1.1      cgd 				sleepexit(1);
    303  1.1      cgd 			}
    304  1.1      cgd 			sleep((u_int)((cnt - 3) * 5));
    305  1.1      cgd 		}
    306  1.1      cgd 	}
    307  1.1      cgd 
    308  1.1      cgd 	/* committed to login -- turn off timeout */
    309  1.1      cgd 	(void)alarm((u_int)0);
    310  1.1      cgd 
    311  1.1      cgd 	endpwent();
    312  1.1      cgd 
    313  1.1      cgd 	/* if user not super-user, check for disabled logins */
    314  1.1      cgd 	if (!rootlogin)
    315  1.1      cgd 		checknologin();
    316  1.1      cgd 
    317  1.1      cgd 	if (chdir(pwd->pw_dir) < 0) {
    318  1.1      cgd 		(void)printf("No home directory %s!\n", pwd->pw_dir);
    319  1.1      cgd 		if (chdir("/"))
    320  1.1      cgd 			exit(0);
    321  1.1      cgd 		pwd->pw_dir = "/";
    322  1.1      cgd 		(void)printf("Logging in with home = \"/\".\n");
    323  1.1      cgd 	}
    324  1.1      cgd 
    325  1.1      cgd 	quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
    326  1.1      cgd 
    327  1.1      cgd 	if (pwd->pw_change || pwd->pw_expire)
    328  1.1      cgd 		(void)gettimeofday(&tp, (struct timezone *)NULL);
    329  1.1      cgd 	if (pwd->pw_change)
    330  1.1      cgd 		if (tp.tv_sec >= pwd->pw_change) {
    331  1.1      cgd 			(void)printf("Sorry -- your password has expired.\n");
    332  1.1      cgd 			sleepexit(1);
    333  1.1      cgd 		} else if (pwd->pw_change - tp.tv_sec <
    334  1.1      cgd 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
    335  1.1      cgd 			(void)printf("Warning: your password expires on %s",
    336  1.5      cgd 			    ctime(&pwd->pw_change));
    337  1.1      cgd 	if (pwd->pw_expire)
    338  1.1      cgd 		if (tp.tv_sec >= pwd->pw_expire) {
    339  1.1      cgd 			(void)printf("Sorry -- your account has expired.\n");
    340  1.1      cgd 			sleepexit(1);
    341  1.1      cgd 		} else if (pwd->pw_expire - tp.tv_sec <
    342  1.1      cgd 		    2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
    343  1.1      cgd 			(void)printf("Warning: your account expires on %s",
    344  1.1      cgd 			    ctime(&pwd->pw_expire));
    345  1.1      cgd 
    346  1.5      cgd 	/* Nothing else left to fail -- really log in. */
    347  1.5      cgd 	bzero((void *)&utmp, sizeof(utmp));
    348  1.5      cgd 	(void)time(&utmp.ut_time);
    349  1.5      cgd 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
    350  1.5      cgd 	if (hostname)
    351  1.5      cgd 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
    352  1.5      cgd 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
    353  1.5      cgd 	login(&utmp);
    354  1.1      cgd 
    355  1.1      cgd 	dolastlog(quietlog);
    356  1.1      cgd 
    357  1.1      cgd 	(void)chown(ttyn, pwd->pw_uid,
    358  1.1      cgd 	    (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
    359  1.1      cgd 	(void)setgid(pwd->pw_gid);
    360  1.1      cgd 
    361  1.1      cgd 	initgroups(username, pwd->pw_gid);
    362  1.1      cgd 
    363  1.1      cgd 	if (*pwd->pw_shell == '\0')
    364  1.1      cgd 		pwd->pw_shell = _PATH_BSHELL;
    365  1.1      cgd 
    366  1.5      cgd 	/* Destroy environment unless user has requested its preservation. */
    367  1.1      cgd 	if (!pflag)
    368  1.1      cgd 		environ = envinit;
    369  1.1      cgd 	(void)setenv("HOME", pwd->pw_dir, 1);
    370  1.1      cgd 	(void)setenv("SHELL", pwd->pw_shell, 1);
    371  1.1      cgd 	if (term[0] == '\0')
    372  1.5      cgd 		(void)strncpy(term, stypeof(tty), sizeof(term));
    373  1.1      cgd 	(void)setenv("TERM", term, 0);
    374  1.1      cgd 	(void)setenv("LOGNAME", pwd->pw_name, 1);
    375  1.1      cgd 	(void)setenv("USER", pwd->pw_name, 1);
    376  1.1      cgd 	(void)setenv("PATH", _PATH_DEFPATH, 0);
    377  1.1      cgd #ifdef KERBEROS
    378  1.1      cgd 	if (krbtkfile_env)
    379  1.1      cgd 		(void)setenv("KRBTKFILE", krbtkfile_env, 1);
    380  1.1      cgd #endif
    381  1.1      cgd 
    382  1.1      cgd 	if (tty[sizeof("tty")-1] == 'd')
    383  1.1      cgd 		syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
    384  1.5      cgd 
    385  1.5      cgd 	/* If fflag is on, assume caller/authenticator has logged root login. */
    386  1.1      cgd 	if (rootlogin && fflag == 0)
    387  1.1      cgd 		if (hostname)
    388  1.1      cgd 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
    389  1.1      cgd 			    username, tty, hostname);
    390  1.1      cgd 		else
    391  1.1      cgd 			syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
    392  1.1      cgd 
    393  1.1      cgd #ifdef KERBEROS
    394  1.1      cgd 	if (!quietlog && notickets == 1)
    395  1.1      cgd 		(void)printf("Warning: no Kerberos tickets issued.\n");
    396  1.1      cgd #endif
    397  1.1      cgd 
    398  1.1      cgd 	if (!quietlog) {
    399  1.5      cgd 		(void)printf(
    400  1.5      cgd "Copyright (c) 1980,1983,1986,1988,1990,1991 The Regents of the University\n%s",
    401  1.5      cgd "of California.  All rights reserved.\n\n");
    402  1.1      cgd 		motd();
    403  1.5      cgd 		(void)snprintf(tbuf,
    404  1.5      cgd 		    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
    405  1.1      cgd 		if (stat(tbuf, &st) == 0 && st.st_size != 0)
    406  1.1      cgd 			(void)printf("You have %smail.\n",
    407  1.1      cgd 			    (st.st_mtime > st.st_atime) ? "new " : "");
    408  1.1      cgd 	}
    409  1.1      cgd 
    410  1.1      cgd 	(void)signal(SIGALRM, SIG_DFL);
    411  1.1      cgd 	(void)signal(SIGQUIT, SIG_DFL);
    412  1.1      cgd 	(void)signal(SIGINT, SIG_DFL);
    413  1.1      cgd 	(void)signal(SIGTSTP, SIG_IGN);
    414  1.1      cgd 
    415  1.1      cgd 	tbuf[0] = '-';
    416  1.5      cgd 	(void)strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
    417  1.1      cgd 	    p + 1 : pwd->pw_shell);
    418  1.1      cgd 
    419  1.1      cgd 	if (setlogin(pwd->pw_name) < 0)
    420  1.1      cgd 		syslog(LOG_ERR, "setlogin() failure: %m");
    421  1.1      cgd 
    422  1.5      cgd 	/* Discard permissions last so can't get killed and drop core. */
    423  1.1      cgd 	if (rootlogin)
    424  1.1      cgd 		(void) setuid(0);
    425  1.1      cgd 	else
    426  1.1      cgd 		(void) setuid(pwd->pw_uid);
    427  1.1      cgd 
    428  1.1      cgd 	execlp(pwd->pw_shell, tbuf, 0);
    429  1.1      cgd 	(void)fprintf(stderr, "%s: %s\n", pwd->pw_shell, strerror(errno));
    430  1.1      cgd 	exit(1);
    431  1.9  deraadt }
    432  1.9  deraadt 
    433  1.9  deraadt int
    434  1.9  deraadt pwcheck(user, p, salt, passwd)
    435  1.9  deraadt 	char *user, *p, *salt, *passwd;
    436  1.9  deraadt {
    437  1.9  deraadt #ifdef SKEY
    438  1.9  deraadt 	if (strcasecmp(p, "s/key") == 0) {
    439  1.9  deraadt 		if (skey_haskey(user)) {
    440  1.9  deraadt 			fprintf(stderr, "You have no s/key. ");
    441  1.9  deraadt 			return 1;
    442  1.9  deraadt 		} else {
    443  1.9  deraadt 			return skey_authenticate(user);
    444  1.9  deraadt 		}
    445  1.9  deraadt 	}
    446  1.9  deraadt #endif
    447  1.9  deraadt 	return strcmp(crypt(p, salt), passwd);
    448  1.1      cgd }
    449  1.1      cgd 
    450  1.1      cgd #ifdef	KERBEROS
    451  1.5      cgd #define	NBUFSIZ		(UT_NAMESIZE + 1 + 5)	/* .root suffix */
    452  1.1      cgd #else
    453  1.1      cgd #define	NBUFSIZ		(UT_NAMESIZE + 1)
    454  1.1      cgd #endif
    455  1.1      cgd 
    456  1.5      cgd void
    457  1.1      cgd getloginname()
    458  1.1      cgd {
    459  1.1      cgd 	register int ch;
    460  1.1      cgd 	register char *p;
    461  1.1      cgd 	static char nbuf[NBUFSIZ];
    462  1.1      cgd 
    463  1.1      cgd 	for (;;) {
    464  1.1      cgd 		(void)printf("login: ");
    465  1.1      cgd 		for (p = nbuf; (ch = getchar()) != '\n'; ) {
    466  1.1      cgd 			if (ch == EOF) {
    467  1.1      cgd 				badlogin(username);
    468  1.1      cgd 				exit(0);
    469  1.1      cgd 			}
    470  1.1      cgd 			if (p < nbuf + (NBUFSIZ - 1))
    471  1.1      cgd 				*p++ = ch;
    472  1.1      cgd 		}
    473  1.1      cgd 		if (p > nbuf)
    474  1.1      cgd 			if (nbuf[0] == '-')
    475  1.1      cgd 				(void)fprintf(stderr,
    476  1.1      cgd 				    "login names may not start with '-'.\n");
    477  1.1      cgd 			else {
    478  1.1      cgd 				*p = '\0';
    479  1.1      cgd 				username = nbuf;
    480  1.1      cgd 				break;
    481  1.1      cgd 			}
    482  1.1      cgd 	}
    483  1.1      cgd }
    484  1.1      cgd 
    485  1.5      cgd int
    486  1.1      cgd rootterm(ttyn)
    487  1.1      cgd 	char *ttyn;
    488  1.1      cgd {
    489  1.1      cgd 	struct ttyent *t;
    490  1.1      cgd 
    491  1.5      cgd 	return((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
    492  1.1      cgd }
    493  1.1      cgd 
    494  1.1      cgd jmp_buf motdinterrupt;
    495  1.1      cgd 
    496  1.5      cgd void
    497  1.1      cgd motd()
    498  1.1      cgd {
    499  1.1      cgd 	register int fd, nchars;
    500  1.1      cgd 	sig_t oldint;
    501  1.1      cgd 	char tbuf[8192];
    502  1.1      cgd 
    503  1.1      cgd 	if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
    504  1.1      cgd 		return;
    505  1.1      cgd 	oldint = signal(SIGINT, sigint);
    506  1.1      cgd 	if (setjmp(motdinterrupt) == 0)
    507  1.1      cgd 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
    508  1.1      cgd 			(void)write(fileno(stdout), tbuf, nchars);
    509  1.1      cgd 	(void)signal(SIGINT, oldint);
    510  1.1      cgd 	(void)close(fd);
    511  1.1      cgd }
    512  1.1      cgd 
    513  1.5      cgd /* ARGSUSED */
    514  1.1      cgd void
    515  1.5      cgd sigint(signo)
    516  1.5      cgd 	int signo;
    517  1.1      cgd {
    518  1.1      cgd 	longjmp(motdinterrupt, 1);
    519  1.1      cgd }
    520  1.1      cgd 
    521  1.5      cgd /* ARGSUSED */
    522  1.5      cgd void
    523  1.5      cgd timedout(signo)
    524  1.5      cgd 	int signo;
    525  1.5      cgd {
    526  1.5      cgd 	(void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
    527  1.5      cgd 	exit(0);
    528  1.5      cgd }
    529  1.5      cgd 
    530  1.5      cgd void
    531  1.1      cgd checknologin()
    532  1.1      cgd {
    533  1.1      cgd 	register int fd, nchars;
    534  1.1      cgd 	char tbuf[8192];
    535  1.1      cgd 
    536  1.1      cgd 	if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
    537  1.1      cgd 		while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
    538  1.1      cgd 			(void)write(fileno(stdout), tbuf, nchars);
    539  1.1      cgd 		sleepexit(0);
    540  1.1      cgd 	}
    541  1.1      cgd }
    542  1.1      cgd 
    543  1.5      cgd void
    544  1.1      cgd dolastlog(quiet)
    545  1.1      cgd 	int quiet;
    546  1.1      cgd {
    547  1.1      cgd 	struct lastlog ll;
    548  1.1      cgd 	int fd;
    549  1.1      cgd 
    550  1.1      cgd 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
    551  1.1      cgd 		(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
    552  1.1      cgd 		if (!quiet) {
    553  1.1      cgd 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
    554  1.1      cgd 			    ll.ll_time != 0) {
    555  1.1      cgd 				(void)printf("Last login: %.*s ",
    556  1.1      cgd 				    24-5, (char *)ctime(&ll.ll_time));
    557  1.1      cgd 				if (*ll.ll_host != '\0')
    558  1.1      cgd 					(void)printf("from %.*s\n",
    559  1.5      cgd 					    (int)sizeof(ll.ll_host),
    560  1.5      cgd 					    ll.ll_host);
    561  1.1      cgd 				else
    562  1.1      cgd 					(void)printf("on %.*s\n",
    563  1.5      cgd 					    (int)sizeof(ll.ll_line),
    564  1.5      cgd 					    ll.ll_line);
    565  1.1      cgd 			}
    566  1.1      cgd 			(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
    567  1.1      cgd 		}
    568  1.1      cgd 		bzero((void *)&ll, sizeof(ll));
    569  1.1      cgd 		(void)time(&ll.ll_time);
    570  1.5      cgd 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
    571  1.1      cgd 		if (hostname)
    572  1.5      cgd 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
    573  1.1      cgd 		(void)write(fd, (char *)&ll, sizeof(ll));
    574  1.1      cgd 		(void)close(fd);
    575  1.1      cgd 	}
    576  1.1      cgd }
    577  1.1      cgd 
    578  1.5      cgd void
    579  1.1      cgd badlogin(name)
    580  1.1      cgd 	char *name;
    581  1.1      cgd {
    582  1.1      cgd 	if (failures == 0)
    583  1.1      cgd 		return;
    584  1.1      cgd 	if (hostname) {
    585  1.1      cgd 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
    586  1.1      cgd 		    failures, failures > 1 ? "S" : "", hostname);
    587  1.1      cgd 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
    588  1.1      cgd 		    "%d LOGIN FAILURE%s FROM %s, %s",
    589  1.1      cgd 		    failures, failures > 1 ? "S" : "", hostname, name);
    590  1.1      cgd 	} else {
    591  1.1      cgd 		syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
    592  1.1      cgd 		    failures, failures > 1 ? "S" : "", tty);
    593  1.1      cgd 		syslog(LOG_AUTHPRIV|LOG_NOTICE,
    594  1.1      cgd 		    "%d LOGIN FAILURE%s ON %s, %s",
    595  1.1      cgd 		    failures, failures > 1 ? "S" : "", tty, name);
    596  1.1      cgd 	}
    597  1.1      cgd }
    598  1.1      cgd 
    599  1.1      cgd #undef	UNKNOWN
    600  1.1      cgd #define	UNKNOWN	"su"
    601  1.1      cgd 
    602  1.1      cgd char *
    603  1.1      cgd stypeof(ttyid)
    604  1.1      cgd 	char *ttyid;
    605  1.1      cgd {
    606  1.1      cgd 	struct ttyent *t;
    607  1.1      cgd 
    608  1.1      cgd 	return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
    609  1.1      cgd }
    610  1.1      cgd 
    611  1.5      cgd void
    612  1.1      cgd sleepexit(eval)
    613  1.1      cgd 	int eval;
    614  1.1      cgd {
    615  1.5      cgd 	(void)sleep((u_int)5);
    616  1.1      cgd 	exit(eval);
    617  1.1      cgd }
    618