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