Annotation of src/usr.bin/login/login.c, Revision 1.47
1.47 ! deraadt 1: /* $OpenBSD: login.c,v 1.46 2002/03/30 18:16:05 vincent 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: */
1.36 millert 36: /*-
37: * Copyright (c) 1995 Berkeley Software Design, Inc. All rights reserved.
38: *
39: * Redistribution and use in source and binary forms, with or without
40: * modification, are permitted provided that the following conditions
41: * are met:
42: * 1. Redistributions of source code must retain the above copyright
43: * notice, this list of conditions and the following disclaimer.
44: * 2. Redistributions in binary form must reproduce the above copyright
45: * notice, this list of conditions and the following disclaimer in the
46: * documentation and/or other materials provided with the distribution.
47: * 3. All advertising materials mentioning features or use of this software
48: * must display the following acknowledgement:
49: * This product includes software developed by Berkeley Software Design,
50: * Inc.
51: * 4. The name of Berkeley Software Design, Inc. may not be used to endorse
52: * or promote products derived from this software without specific prior
53: * written permission.
54: *
55: * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
56: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58: * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
59: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65: * SUCH DAMAGE.
66: *
67: * BSDI $From: login.c,v 2.28 1999/09/08 22:35:36 prb Exp $
68: */
1.1 deraadt 69:
70: #ifndef lint
71: static char copyright[] =
72: "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
73: The Regents of the University of California. All rights reserved.\n";
74: #endif /* not lint */
75:
76: #ifndef lint
77: #if 0
78: static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
79: #endif
1.47 ! deraadt 80: static char rcsid[] = "$OpenBSD: login.c,v 1.46 2002/03/30 18:16:05 vincent Exp $";
1.1 deraadt 81: #endif /* not lint */
82:
83: /*
84: * login [ name ]
85: * login -h hostname (for telnetd, etc.)
86: * login -f name (for pre-authenticated login: datakit, xterm, etc.)
87: */
88:
89: #include <sys/param.h>
1.36 millert 90: #include <sys/socket.h>
1.1 deraadt 91: #include <sys/stat.h>
92: #include <sys/time.h>
93: #include <sys/resource.h>
1.11 millert 94: #include <sys/wait.h>
1.1 deraadt 95:
96: #include <err.h>
97: #include <errno.h>
1.27 millert 98: #include <fcntl.h>
1.1 deraadt 99: #include <grp.h>
1.30 millert 100: #include <login_cap.h>
1.36 millert 101: #include <netdb.h>
1.1 deraadt 102: #include <pwd.h>
103: #include <setjmp.h>
104: #include <signal.h>
1.36 millert 105: #include <stdarg.h>
1.1 deraadt 106: #include <stdio.h>
107: #include <stdlib.h>
108: #include <string.h>
109: #include <syslog.h>
110: #include <ttyent.h>
111: #include <tzfile.h>
112: #include <unistd.h>
113: #include <utmp.h>
1.3 deraadt 114: #include <util.h>
1.36 millert 115: #include <bsd_auth.h>
116:
117: #ifdef KERBEROS
118: #include <kerberosIV/kafs.h>
119: #endif
1.1 deraadt 120:
121: #include "pathnames.h"
122:
1.45 millert 123: void badlogin(char *);
124: void dolastlog(int);
125: void getloginname(void);
126: void motd(void);
127: void quickexit(int);
128: int rootterm(char *);
129: void sigint(int);
130: void sighup(int);
131: void sleepexit(int);
132: char *stypeof(char *);
133: void timedout(int);
134: int main(int, char **);
1.1 deraadt 135:
1.45 millert 136: extern int check_failedlogin(uid_t);
137: extern void log_failedlogin(uid_t, char *, char *, char *);
1.1 deraadt 138:
139: #define TTYGRPNAME "tty" /* name of group to own ttys */
140:
141: /*
1.37 millert 142: * This bounds the time given to login; may be overridden by /etc/login.conf.
1.1 deraadt 143: */
1.30 millert 144: u_int timeout = 300;
1.1 deraadt 145:
1.36 millert 146: struct passwd *pwd;
1.30 millert 147: login_cap_t *lc = NULL;
1.36 millert 148: auth_session_t *as = NULL;
1.30 millert 149: int failures;
1.36 millert 150: int needbanner = 1;
1.30 millert 151: char term[64], *hostname, *tty;
1.36 millert 152: char *style;
1.30 millert 153: char *username = NULL, *rusername = NULL;
1.1 deraadt 154:
1.47 ! deraadt 155: extern char **environ;
! 156:
1.1 deraadt 157: int
1.47 ! deraadt 158: main(int argc, char *argv[])
1.1 deraadt 159: {
1.47 ! deraadt 160: char *domain, *p, *ttyn, *shell, *fullname, *instance;
! 161: char *lipaddr, *script, *ripaddr, *style, *type, *fqdn;
! 162: char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
! 163: char localhost[MAXHOSTNAMELEN], *copyright;
! 164: int ask, ch, cnt, fflag, pflag, quietlog, rootlogin, lastchance;
! 165: int error, homeless, needto, authok, tries, backoff;
1.36 millert 166: struct addrinfo *ai, hints;
1.47 ! deraadt 167: struct rlimit cds, scds;
! 168: quad_t expire, warning;
! 169: struct utmp utmp;
1.1 deraadt 170: struct group *gr;
171: struct stat st;
172: uid_t uid;
173:
174: (void)signal(SIGALRM, timedout);
1.36 millert 175: if (argc > 1) {
176: needto = 0;
177: (void)alarm(timeout);
178: } else
179: needto = 1;
1.1 deraadt 180: (void)signal(SIGQUIT, SIG_IGN);
181: (void)signal(SIGINT, SIG_IGN);
1.36 millert 182: (void)signal(SIGHUP, SIG_IGN);
1.1 deraadt 183: (void)setpriority(PRIO_PROCESS, 0, 0);
184:
185: openlog("login", LOG_ODELAY, LOG_AUTH);
186:
1.37 millert 187: fqdn = lipaddr = ripaddr = fullname = type = NULL;
188: authok = 0;
189: tries = 10;
190: backoff = 3;
1.36 millert 191:
192: /*
193: * Since login deals with sensitive information, turn off coredumps.
194: */
195: if (getrlimit(RLIMIT_CORE, &scds) < 0) {
196: syslog(LOG_ERR, "couldn't get core dump size: %m");
197: scds.rlim_cur = scds.rlim_max = QUAD_MIN;
198: }
199: cds.rlim_cur = cds.rlim_max = 0;
200: if (setrlimit(RLIMIT_CORE, &cds) < 0) {
201: syslog(LOG_ERR, "couldn't set core dump size to 0: %m");
202: scds.rlim_cur = scds.rlim_max = QUAD_MIN;
203: }
204:
1.1 deraadt 205: /*
206: * -p is used by getty to tell login not to destroy the environment
207: * -f is used to skip a second login authentication
208: * -h is used by other servers to pass the name of the remote
209: * host to login so that it may be placed in utmp and wtmp
210: */
211: domain = NULL;
212: if (gethostname(localhost, sizeof(localhost)) < 0)
213: syslog(LOG_ERR, "couldn't get local hostname: %m");
214: else
215: domain = strchr(localhost, '.');
1.21 deraadt 216: if (domain) {
217: domain++;
218: if (*domain && strchr(domain, '.') == NULL)
219: domain = localhost;
220: }
1.1 deraadt 221:
1.36 millert 222: if ((as = auth_open()) == NULL) {
1.39 millert 223: syslog(LOG_ERR, "auth_open: %m");
224: err(1, "unable to initialize BSD authentication");
1.36 millert 225: }
1.39 millert 226: auth_setoption(as, "login", "yes");
1.36 millert 227:
228: fflag = pflag = 0;
1.1 deraadt 229: uid = getuid();
1.36 millert 230: while ((ch = getopt(argc, argv, "fh:pu:L:R:")) != -1)
1.1 deraadt 231: switch (ch) {
232: case 'f':
233: fflag = 1;
234: break;
235: case 'h':
1.36 millert 236: if (uid) {
1.46 vincent 237: warnx("-h option: %s", strerror(EPERM));
1.36 millert 238: quickexit(1);
239: }
240: if ((fqdn = strdup(optarg)) == NULL) {
241: warn(NULL);
242: quickexit(1);
243: }
244: auth_setoption(as, "fqdn", fqdn);
1.1 deraadt 245: if (domain && (p = strchr(optarg, '.')) &&
1.21 deraadt 246: strcasecmp(p+1, domain) == 0)
1.1 deraadt 247: *p = 0;
248: hostname = optarg;
1.36 millert 249: auth_setoption(as, "hostname", hostname);
250: break;
251: case 'L':
252: if (uid) {
253: warnx("-L option: %s", strerror(EPERM));
254: quickexit(1);
255: }
256: if (lipaddr) {
257: warnx("duplicate -L option");
258: quickexit(1);
259: }
260: lipaddr = optarg;
261: memset(&hints, 0, sizeof(hints));
262: hints.ai_family = PF_UNSPEC;
263: hints.ai_flags = AI_CANONNAME;
264: error = getaddrinfo(lipaddr, NULL, &hints, &ai);
265: if (!error) {
266: strlcpy(localhost, ai->ai_canonname,
267: sizeof(localhost));
268: freeaddrinfo(ai);
269: } else
270: strlcpy(localhost, lipaddr, sizeof(localhost));
271: auth_setoption(as, "local_addr", lipaddr);
1.1 deraadt 272: break;
273: case 'p':
274: pflag = 1;
275: break;
1.36 millert 276: case 'R':
277: if (uid) {
278: warnx("-R option: %s", strerror(EPERM));
279: quickexit(1);
280: }
281: if (ripaddr) {
282: warnx("duplicate -R option");
283: quickexit(1);
284: }
285: ripaddr = optarg;
286: auth_setoption(as, "remote_addr", ripaddr);
287: break;
1.14 millert 288: case 'u':
1.36 millert 289: if (uid) {
290: warnx("-u option: %s", strerror(EPERM));
291: quickexit(1);
292: }
1.14 millert 293: rusername = optarg;
294: break;
1.1 deraadt 295: default:
296: if (!uid)
297: syslog(LOG_ERR, "invalid flag %c", ch);
298: (void)fprintf(stderr,
1.47 ! deraadt 299: "usage: login [-fp] [-h hostname] [-L lipaddr] "
! 300: "[-R ripaddr] [-u username] [user]\n");
1.36 millert 301: quickexit(1);
1.1 deraadt 302: }
303: argc -= optind;
304: argv += optind;
305:
306: if (*argv) {
307: username = *argv;
308: ask = 0;
309: } else
310: ask = 1;
311:
312: for (cnt = getdtablesize(); cnt > 2; cnt--)
313: (void)close(cnt);
314:
315: ttyn = ttyname(STDIN_FILENO);
316: if (ttyn == NULL || *ttyn == '\0') {
317: (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
318: ttyn = tname;
319: }
1.12 millert 320: if ((tty = strrchr(ttyn, '/')))
1.1 deraadt 321: ++tty;
322: else
323: tty = ttyn;
324:
1.36 millert 325: #ifdef notyet
326: /* XXX - we don't (yet) support per-tty auth stuff */
327: /* BSDi uses a ttys.conf file but we could just overload /etc/ttys */
328: /*
329: * Classify the attempt.
330: * By default we use the value in the ttys file.
331: * If there is a classify script we run that as
332: *
333: * classify [-f] [username]
334: */
335: if (type = getttyauth(tty))
336: auth_setoption(as, "auth_type", type);
337: #endif
338:
339: /* get the default login class */
1.47 ! deraadt 340: if ((lc = login_getclass(0)) == NULL) { /* get the default class */
1.36 millert 341: warnx("Failure to retrieve default class");
342: quickexit(1);
343: }
1.37 millert 344: timeout = (u_int)login_getcapnum(lc, "login-timeout", 300, 300);
1.36 millert 345: if ((script = login_getcapstr(lc, "classify", NULL, NULL)) != NULL) {
346: unsetenv("AUTH_TYPE");
347: unsetenv("REMOTE_NAME");
348: if (script[0] != '/') {
349: syslog(LOG_ERR, "Invalid classify script: %s", script);
350: warnx("Classification failure");
351: quickexit(1);
352: }
353: shell = strrchr(script, '/') + 1;
354: auth_setstate(as, AUTH_OKAY);
1.47 ! deraadt 355: auth_call(as, script, shell,
1.36 millert 356: fflag ? "-f" : username, fflag ? username : 0, 0);
357: if (!(auth_getstate(as) & AUTH_ALLOW))
358: quickexit(1);
359: auth_setenv(as);
360: if ((p = getenv("AUTH_TYPE")) != NULL &&
361: strncmp(p, "auth-", 5) == 0)
362: type = p;
363: if ((p = getenv("REMOTE_NAME")) != NULL)
364: hostname = p;
365: /*
366: * we may have changed some values, reset them
367: */
368: auth_clroptions(as);
369: if (type)
370: auth_setoption(as, "auth_type", type);
371: if (fqdn)
372: auth_setoption(as, "fqdn", fqdn);
373: if (hostname)
374: auth_setoption(as, "hostname", hostname);
375: if (lipaddr)
376: auth_setoption(as, "local_addr", lipaddr);
377: if (ripaddr)
378: auth_setoption(as, "remote_addr", ripaddr);
379: }
380:
381: /*
382: * Request the things like the approval script print things
383: * to stdout (in particular, the nologins files)
384: */
385: auth_setitem(as, AUTHV_INTERACTIVE, "True");
386:
1.1 deraadt 387: for (cnt = 0;; ask = 1) {
1.36 millert 388: /*
389: * Clean up our current authentication session.
390: * Options are not cleared so we need to clear any
391: * we might set below.
392: */
393: auth_clean(as);
394: auth_clroption(as, "style");
395: auth_clroption(as, "lastchance");
396:
397: lastchance = 0;
398:
1.1 deraadt 399: if (ask) {
400: fflag = 0;
401: getloginname();
402: }
1.36 millert 403: if (needto) {
404: needto = 0;
405: alarm(timeout);
406: }
1.47 ! deraadt 407: if ((style = strchr(username, ':')) != NULL)
1.36 millert 408: *style++ = '\0';
409: if (fullname)
410: free(fullname);
411: if (auth_setitem(as, AUTHV_NAME, username) < 0 ||
412: (fullname = strdup(username)) == NULL) {
413: syslog(LOG_ERR, "%m");
414: warn(NULL);
415: quickexit(1);
416: }
1.1 deraadt 417: rootlogin = 0;
1.40 millert 418: if ((instance = strpbrk(username, "./")) != NULL) {
419: if (strncmp(instance + 1, "root", 4) == 0)
1.1 deraadt 420: rootlogin = 1;
421: *instance++ = '\0';
422: } else
423: instance = "";
1.36 millert 424:
1.1 deraadt 425: if (strlen(username) > UT_NAMESIZE)
426: username[UT_NAMESIZE] = '\0';
427:
428: /*
429: * Note if trying multiple user names; log failures for
430: * previous user name, but don't bother logging one failure
431: * for nonexistent name (mistyped username).
432: */
433: if (failures && strcmp(tbuf, username)) {
434: if (failures > (pwd ? 0 : 1))
435: badlogin(tbuf);
436: failures = 0;
437: }
1.36 millert 438: (void)strlcpy(tbuf, username, sizeof(tbuf));
1.1 deraadt 439:
1.36 millert 440: if ((pwd = getpwnam(username)) != NULL &&
441: auth_setpwd(as, pwd) < 0) {
442: syslog(LOG_ERR, "%m");
443: warn(NULL);
444: quickexit(1);
445: }
1.1 deraadt 446:
1.36 millert 447: lc = login_getclass(pwd ? pwd->pw_class : NULL);
448: if (!lc)
449: goto failed;
1.1 deraadt 450:
1.36 millert 451: style = login_getstyle(lc, style, type);
452: if (!style)
453: goto failed;
1.1 deraadt 454:
1.36 millert 455: /*
1.37 millert 456: * We allow "login-tries" attempts to login but start
457: * slowing down after "login-backoff" attempts.
458: */
459: tries = (int)login_getcapnum(lc, "login-tries", 10, 10);
460: backoff = (int)login_getcapnum(lc, "login-backoff", 3, 3);
461:
462: /*
1.36 millert 463: * Turn off the fflag if we have an an invalid user
464: * or we are not root and we are trying to change uids.
465: */
466: if (!pwd || (uid && uid != pwd->pw_uid))
467: fflag = 0;
1.1 deraadt 468:
1.36 millert 469: if (pwd && pwd->pw_uid == 0)
470: rootlogin = 1;
1.25 millert 471:
1.36 millert 472: /*
473: * If we do not have the force flag authenticate the user
474: */
1.44 millert 475: if (!fflag) {
1.36 millert 476: lastchance =
477: login_getcaptime(lc, "password-dead", 0, 0) != 0;
478: if (lastchance)
479: auth_setoption(as, "lastchance", "yes");
480: /*
481: * Once we start asking for a password
482: * we want to log a failure on a hup.
483: */
484: signal(SIGHUP, sighup);
485: auth_verify(as, style, NULL, lc->lc_class, NULL);
486: authok = auth_getstate(as);
487: /*
488: * If their password expired and it has not been
489: * too long since then, give the user one last
490: * chance to change their password
491: */
492: if ((authok & AUTH_PWEXPIRED) && lastchance) {
493: authok = AUTH_OKAY;
494: } else
495: lastchance = 0;
496: if ((authok & AUTH_ALLOW) == 0)
497: goto failed;
498: if (auth_setoption(as, "style", style) < 0) {
499: syslog(LOG_ERR, "%m");
500: warn(NULL);
501: quickexit(1);
1.25 millert 502: }
1.1 deraadt 503: }
1.36 millert 504: /*
505: * explicitly reject users without password file entries
506: */
1.47 ! deraadt 507: if (pwd == NULL)
1.36 millert 508: goto failed;
1.1 deraadt 509:
510: /*
1.36 millert 511: * If trying to log in as root on an insecure terminal,
512: * refuse the login attempt unless the authentication
513: * style explicitly says a root login is okay.
1.1 deraadt 514: */
1.44 millert 515: if (pwd && rootlogin && !rootterm(tty))
1.36 millert 516: goto failed;
1.29 millert 517:
1.36 millert 518: if (fflag) {
519: type = 0;
520: style = "forced";
521: }
522: break;
523:
524: failed:
525: if (authok & AUTH_SILENT)
526: quickexit(0);
1.29 millert 527: if (rootlogin && !rootterm(tty)) {
1.36 millert 528: warnx("%s login refused on this terminal.",
529: fullname);
1.1 deraadt 530: if (hostname)
531: syslog(LOG_NOTICE,
1.14 millert 532: "LOGIN %s REFUSED FROM %s%s%s ON TTY %s",
1.36 millert 533: fullname, rusername ? rusername : "",
1.14 millert 534: rusername ? "@" : "", hostname, tty);
1.1 deraadt 535: else
536: syslog(LOG_NOTICE,
537: "LOGIN %s REFUSED ON TTY %s",
1.47 ! deraadt 538: fullname, tty);
1.36 millert 539: } else {
540: if (!as || (p = auth_getvalue(as, "errormsg")) == NULL)
541: p = "Login incorrect";
542: (void)printf("%s\n", p);
543: }
1.1 deraadt 544: failures++;
1.13 millert 545: if (pwd)
1.14 millert 546: log_failedlogin(pwd->pw_uid, hostname, rusername, tty);
1.37 millert 547: /*
548: * By default, we allow 10 tries, but after 3 we start
549: * backing off to slow down password guessers.
550: */
551: if (++cnt > backoff) {
552: if (cnt >= tries) {
1.1 deraadt 553: badlogin(username);
554: sleepexit(1);
555: }
1.37 millert 556: sleep((u_int)((cnt - backoff) * tries / 2));
1.1 deraadt 557: }
558: }
559:
560: /* committed to login -- turn off timeout */
1.36 millert 561: (void)alarm(0);
1.1 deraadt 562:
563: endpwent();
564:
1.30 millert 565: shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
566: if (*shell == '\0')
567: shell = _PATH_BSHELL;
568: else if (strlen(shell) >= MAXPATHLEN) {
569: syslog(LOG_ERR, "shell path too long: %s", shell);
570: warnx("invalid shell");
1.36 millert 571: quickexit(1);
1.24 art 572: }
1.1 deraadt 573:
574: /* Destroy environment unless user has requested its preservation. */
1.24 art 575: if (!pflag) {
1.22 deraadt 576: if ((environ = calloc(1, sizeof (char *))) == NULL)
577: err(1, "calloc");
1.24 art 578: } else {
1.9 millert 579: char **cpp, **cpp2;
580:
581: for (cpp2 = cpp = environ; *cpp; cpp++) {
582: if (strncmp(*cpp, "LD_", 3) &&
1.29 millert 583: strncmp(*cpp, "ENV=", 4) &&
584: strncmp(*cpp, "BASH_ENV=", 9) &&
1.9 millert 585: strncmp(*cpp, "IFS=", 4))
586: *cpp2++ = *cpp;
587: }
588: *cpp2 = 0;
589: }
1.30 millert 590: /* Note: setusercontext(3) will set PATH */
1.34 deraadt 591: if (setenv("HOME", pwd->pw_dir, 1) == -1 ||
592: setenv("SHELL", shell, 1) == -1) {
593: warn("unable to setenv()");
1.36 millert 594: quickexit(1);
1.34 deraadt 595: }
1.1 deraadt 596: if (term[0] == '\0')
1.29 millert 597: (void)strlcpy(term, stypeof(tty), sizeof(term));
1.34 deraadt 598: if (setenv("TERM", term, 0) == -1 ||
599: setenv("LOGNAME", pwd->pw_name, 1) == -1 ||
600: setenv("USER", pwd->pw_name, 1) == -1) {
601: warn("unable to setenv()");
1.36 millert 602: quickexit(1);
1.34 deraadt 603: }
604: if (hostname) {
605: if (setenv("REMOTEHOST", hostname, 1) == -1) {
606: warn("unable to setenv()");
1.36 millert 607: quickexit(1);
1.34 deraadt 608: }
609: }
610: if (rusername) {
611: if (setenv("REMOTEUSER", rusername, 1) == -1) {
612: warn("unable to setenv()");
1.36 millert 613: quickexit(1);
1.34 deraadt 614: }
615: }
1.36 millert 616:
617: if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH)) {
618: warn("unable to set user context");
619: quickexit(1);
620: }
621: auth_setenv(as);
622:
623: /* if user not super-user, check for disabled logins */
624: if (!rootlogin)
625: auth_checknologin(lc);
626:
627: setegid(pwd->pw_gid);
628: seteuid(pwd->pw_uid);
629:
630: homeless = chdir(pwd->pw_dir);
631: if (homeless) {
632: if (login_getcapbool(lc, "requirehome", 0)) {
633: (void)printf("No home directory %s!\n", pwd->pw_dir);
634: quickexit(1);
1.34 deraadt 635: }
1.36 millert 636: if (chdir("/"))
637: quickexit(0);
1.34 deraadt 638: }
1.36 millert 639:
640: quietlog = ((strcmp(pwd->pw_shell, "/sbin/nologin") == 0) ||
641: login_getcapbool(lc, "hushlogin", 0) ||
642: (access(_PATH_HUSHLOGIN, F_OK) == 0));
643:
644: seteuid(0);
645: setegid(0); /* XXX use a saved gid instead? */
646:
647: if ((p = auth_getvalue(as, "warnmsg")) != NULL)
648: (void)printf("WARNING: %s\n\n", p);
649:
650: expire = auth_check_expire(as);
651: if (expire < 0) {
652: (void)printf("Sorry -- your account has expired.\n");
653: quickexit(1);
654: } else if (expire > 0 && !quietlog) {
655: warning = login_getcaptime(lc, "expire-warn",
656: 2 * DAYSPERWEEK * SECSPERDAY, 2 * DAYSPERWEEK * SECSPERDAY);
657: if (expire < warning)
1.47 ! deraadt 658: (void)printf("Warning: your account expires on %s",
1.36 millert 659: ctime(&pwd->pw_expire));
1.34 deraadt 660: }
1.36 millert 661:
662: /* Nothing else left to fail -- really log in. */
663: (void)signal(SIGHUP, SIG_DFL);
664: memset(&utmp, 0, sizeof(utmp));
665: (void)time(&utmp.ut_time);
666: (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
667: if (hostname)
668: (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
669: (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
670: login(&utmp);
671:
672: if (!quietlog)
673: (void)check_failedlogin(pwd->pw_uid);
674: dolastlog(quietlog);
675:
676: login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
677:
678: (void)chown(ttyn, pwd->pw_uid,
679: (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
680:
1.1 deraadt 681: /* If fflag is on, assume caller/authenticator has logged root login. */
1.24 art 682: if (rootlogin && fflag == 0) {
1.1 deraadt 683: if (hostname)
1.14 millert 684: syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s%s%s",
685: username, tty, rusername ? rusername : "",
686: rusername ? "@" : "", hostname);
1.1 deraadt 687: else
688: syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
1.24 art 689: }
1.1 deraadt 690:
691: if (!quietlog) {
1.36 millert 692: if ((copyright =
693: login_getcapstr(lc, "copyright", NULL, NULL)) != NULL)
694: auth_cat(copyright);
1.1 deraadt 695: motd();
696: (void)snprintf(tbuf,
697: sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
698: if (stat(tbuf, &st) == 0 && st.st_size != 0)
699: (void)printf("You have %smail.\n",
700: (st.st_mtime > st.st_atime) ? "new " : "");
701: }
702:
703: (void)signal(SIGALRM, SIG_DFL);
704: (void)signal(SIGQUIT, SIG_DFL);
1.36 millert 705: (void)signal(SIGHUP, SIG_DFL);
1.1 deraadt 706: (void)signal(SIGINT, SIG_DFL);
707: (void)signal(SIGTSTP, SIG_IGN);
708:
709: tbuf[0] = '-';
1.30 millert 710: (void)strlcpy(tbuf + 1, (p = strrchr(shell, '/')) ?
1.36 millert 711: p + 1 : shell, sizeof(tbuf) - 1);
712:
713: if ((scds.rlim_cur != QUAD_MIN || scds.rlim_max != QUAD_MIN) &&
714: setrlimit(RLIMIT_CORE, &scds) < 0)
715: syslog(LOG_ERR, "couldn't reset core dump size: %m");
716:
717: if (lastchance)
1.47 ! deraadt 718: (void)printf("WARNING: Your password has expired."
! 719: " You must change your password, now!\n");
1.1 deraadt 720:
1.38 millert 721: if (setusercontext(lc, pwd, rootlogin ? 0 : pwd->pw_uid,
1.36 millert 722: LOGIN_SETALL & ~LOGIN_SETPATH) < 0) {
1.30 millert 723: warn("unable to set user context");
1.36 millert 724: quickexit(1);
725: }
726:
727: if (homeless) {
728: (void)printf("No home directory %s!\n", pwd->pw_dir);
729: (void)printf("Logging in with home = \"/\".\n");
730: (void)setenv("HOME", "/", 1);
1.15 tholo 731: }
1.30 millert 732:
1.36 millert 733: if (auth_approval(as, lc, NULL, "login") == 0) {
734: if (auth_getstate(as) & AUTH_EXPIRED)
735: (void)printf("Sorry -- your account has expired.\n");
736: else
737: (void)printf("approval failure\n");
738: quickexit(1);
739: }
740:
741: /*
742: * The last thing we do is discard all of the open file descriptors.
743: * Last because the C library may have some open.
744: *
745: * XXX
746: * Assume that stdin, stdout and stderr are 0, 1 and 2, and that
747: * STDERR_FILENO is 2.
748: */
749: for (cnt = getdtablesize(); cnt > STDERR_FILENO; cnt--)
750: (void)close(cnt);
751:
752: /*
753: * Close the authentication session, make sure it is marked
754: * as okay so no files are removed.
755: */
756: auth_setstate(as, AUTH_OKAY);
757: auth_close(as);
758:
1.24 art 759: #ifdef KERBEROS
1.47 ! deraadt 760: kgettokens(pwd->pw_dir);
1.24 art 761: #endif
1.1 deraadt 762:
1.42 deraadt 763: execlp(shell, tbuf, (char *)NULL);
1.30 millert 764: err(1, "%s", shell);
1.1 deraadt 765: }
766:
767: /*
1.36 millert 768: * Allow for a '.' and 16 characters for any instance as well as
769: * space for a ':' and 16 charcters defining the authentication type.
1.1 deraadt 770: */
1.36 millert 771: #define NBUFSIZ (UT_NAMESIZE + 1 + 16 + 1 + 16)
1.1 deraadt 772:
773: void
1.47 ! deraadt 774: getloginname(void)
1.1 deraadt 775: {
1.47 ! deraadt 776: static char nbuf[NBUFSIZ], *p;
1.1 deraadt 777: int ch;
778:
779: for (;;) {
780: (void)printf("login: ");
781: for (p = nbuf; (ch = getchar()) != '\n'; ) {
782: if (ch == EOF) {
783: badlogin(username);
1.36 millert 784: quickexit(0);
1.1 deraadt 785: }
786: if (p < nbuf + (NBUFSIZ - 1))
787: *p++ = ch;
788: }
1.24 art 789: if (p > nbuf) {
1.1 deraadt 790: if (nbuf[0] == '-')
791: (void)fprintf(stderr,
792: "login names may not start with '-'.\n");
793: else {
794: *p = '\0';
795: username = nbuf;
796: break;
797: }
1.24 art 798: }
1.1 deraadt 799: }
800: }
801:
802: int
1.47 ! deraadt 803: rootterm(char *ttyn)
1.1 deraadt 804: {
805: struct ttyent *t;
806:
1.36 millert 807: /* XXX - stash output of getttynam() elsewhere */
1.1 deraadt 808: return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
809: }
810:
811: jmp_buf motdinterrupt;
812:
813: void
1.47 ! deraadt 814: motd(void)
1.1 deraadt 815: {
1.47 ! deraadt 816: char tbuf[8192], *motd;
1.1 deraadt 817: int fd, nchars;
818: sig_t oldint;
1.30 millert 819:
820: motd = login_getcapstr(lc, "welcome", _PATH_MOTDFILE, _PATH_MOTDFILE);
1.1 deraadt 821:
1.30 millert 822: if ((fd = open(motd, O_RDONLY, 0)) < 0)
1.1 deraadt 823: return;
824: oldint = signal(SIGINT, sigint);
825: if (setjmp(motdinterrupt) == 0)
826: while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
827: (void)write(fileno(stdout), tbuf, nchars);
828: (void)signal(SIGINT, oldint);
829: (void)close(fd);
830: }
831:
832: /* ARGSUSED */
833: void
1.47 ! deraadt 834: sigint(int signo)
1.1 deraadt 835: {
836: longjmp(motdinterrupt, 1);
837: }
838:
839: /* ARGSUSED */
840: void
1.47 ! deraadt 841: timedout(int signo)
1.1 deraadt 842: {
1.47 ! deraadt 843: char warn[1024];
! 844:
! 845: snprintf(warn, sizeof warn,
! 846: "Login timed out after %d seconds\n", timeout);
! 847: write(STDERR_FILENO, warn, strlen(warn));
1.37 millert 848: if (username)
849: badlogin(username);
1.47 ! deraadt 850: _exit(0);
1.1 deraadt 851: }
852:
853: void
1.47 ! deraadt 854: dolastlog(int quiet)
1.1 deraadt 855: {
856: struct lastlog ll;
857: int fd;
858:
859: if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1.26 millert 860: (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), SEEK_SET);
1.1 deraadt 861: if (!quiet) {
862: if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
863: ll.ll_time != 0) {
864: (void)printf("Last login: %.*s ",
865: 24-5, (char *)ctime(&ll.ll_time));
1.14 millert 866: (void)printf("on %.*s",
867: (int)sizeof(ll.ll_line),
868: ll.ll_line);
1.1 deraadt 869: if (*ll.ll_host != '\0')
1.14 millert 870: (void)printf(" from %.*s",
1.1 deraadt 871: (int)sizeof(ll.ll_host),
872: ll.ll_host);
1.14 millert 873: (void)putchar('\n');
1.1 deraadt 874: }
1.26 millert 875: (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll),
876: SEEK_SET);
1.1 deraadt 877: }
878: memset((void *)&ll, 0, sizeof(ll));
879: (void)time(&ll.ll_time);
880: (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
881: if (hostname)
882: (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
883: (void)write(fd, (char *)&ll, sizeof(ll));
884: (void)close(fd);
885: }
886: }
887:
888: void
1.47 ! deraadt 889: badlogin(char *name)
1.1 deraadt 890: {
1.47 ! deraadt 891: struct syslog_data sdata = SYSLOG_DATA_INIT;
! 892:
1.1 deraadt 893: if (failures == 0)
894: return;
895: if (hostname) {
1.47 ! deraadt 896: syslog_r(LOG_NOTICE, &sdata,
! 897: "%d LOGIN FAILURE%s FROM %s%s%s",
1.14 millert 898: failures, failures > 1 ? "S" : "",
899: rusername ? rusername : "", rusername ? "@" : "", hostname);
1.47 ! deraadt 900: syslog_r(LOG_AUTHPRIV|LOG_NOTICE, &sdata,
1.14 millert 901: "%d LOGIN FAILURE%s FROM %s%s%s, %s",
902: failures, failures > 1 ? "S" : "",
903: rusername ? rusername : "", rusername ? "@" : "",
904: hostname, name);
1.1 deraadt 905: } else {
1.47 ! deraadt 906: syslog_r(LOG_NOTICE, &sdata,
! 907: "%d LOGIN FAILURE%s ON %s",
1.1 deraadt 908: failures, failures > 1 ? "S" : "", tty);
1.47 ! deraadt 909: syslog_r(LOG_AUTHPRIV|LOG_NOTICE, &sdata,
1.1 deraadt 910: "%d LOGIN FAILURE%s ON %s, %s",
911: failures, failures > 1 ? "S" : "", tty, name);
912: }
913: }
914:
915: #undef UNKNOWN
916: #define UNKNOWN "su"
917:
918: char *
1.47 ! deraadt 919: stypeof(char *ttyid)
1.1 deraadt 920: {
921: struct ttyent *t;
922:
1.30 millert 923: return (ttyid && (t = getttynam(ttyid)) ? t->ty_type :
924: login_getcapstr(lc, "term", UNKNOWN, UNKNOWN));
1.1 deraadt 925: }
926:
927: void
1.47 ! deraadt 928: sleepexit(int eval)
1.1 deraadt 929: {
1.36 millert 930: auth_close(as);
1.1 deraadt 931: (void)sleep(5);
932: exit(eval);
1.11 millert 933: }
934:
935: void
1.47 ! deraadt 936: quickexit(int eval)
1.36 millert 937: {
938: if (as)
939: auth_close(as);
940: exit(eval);
941: }
942:
943:
944: void
1.47 ! deraadt 945: sighup(int signum)
1.11 millert 946: {
947: if (username)
948: badlogin(username);
1.47 ! deraadt 949: _exit(0);
1.1 deraadt 950: }
1.36 millert 951:
952: #ifdef KERBEROS
953: void
1.47 ! deraadt 954: kgettokens(char *homedir)
1.36 millert 955: {
1.47 ! deraadt 956:
1.36 millert 957: /* buy AFS-tokens for homedir */
1.47 ! deraadt 958: if (k_hasafs()) {
1.36 millert 959: char cell[128];
1.47 ! deraadt 960:
1.36 millert 961: k_setpag();
1.47 ! deraadt 962: if (k_afs_cell_of_file(homedir, cell, sizeof(cell)) == 0)
1.36 millert 963: krb_afslog(cell, 0);
964: krb_afslog(0, 0);
965: }
966: }
967: #endif