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