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