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