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