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