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