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