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