[BACK]Return to login.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / login

Annotation of src/usr.bin/login/login.c, Revision 1.56

1.56    ! jmc         1: /*     $OpenBSD: login.c,v 1.55 2004/08/30 23:00:22 millert Exp $      */
1.3       deraadt     2: /*     $NetBSD: login.c,v 1.13 1996/05/15 23:50:16 jtc Exp $   */
1.1       deraadt     3:
                      4: /*-
                      5:  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
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.56    ! jmc        76: static const char rcsid[] = "$OpenBSD: login.c,v 1.55 2004/08/30 23:00:22 millert 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.56    ! jmc       266:                            "usage: login [-fp] [-h hostname] [-L local-ip] "
        !           267:                            "[-R remote-ip] [-u username]\n\t[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.49      millert   279:        /*
                    280:         * If effective user is not root, just run su(1) to emulate login(1).
                    281:         */
                    282:        if (geteuid() != 0) {
                    283:                char *av[5], **ap;
                    284:
                    285:                auth_close(as);
                    286:                closelog();
1.54      millert   287:                closefrom(STDERR_FILENO + 1);
1.49      millert   288:
                    289:                ap = av;
                    290:                *ap++ = _PATH_SU;
                    291:                *ap++ = "-L";
                    292:                if (!pflag)
                    293:                        *ap++ = "-l";
                    294:                if (!ask)
                    295:                        *ap++ = username;
                    296:                *ap = NULL;
                    297:                execv(_PATH_SU, av);
                    298:                warn("unable to exec %s", _PATH_SU);
                    299:                _exit(1);
                    300:        }
                    301:
1.1       deraadt   302:        ttyn = ttyname(STDIN_FILENO);
                    303:        if (ttyn == NULL || *ttyn == '\0') {
                    304:                (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
                    305:                ttyn = tname;
                    306:        }
1.12      millert   307:        if ((tty = strrchr(ttyn, '/')))
1.1       deraadt   308:                ++tty;
                    309:        else
                    310:                tty = ttyn;
                    311:
1.49      millert   312:        /*
                    313:         * Since login deals with sensitive information, turn off coredumps.
                    314:         */
                    315:        if (getrlimit(RLIMIT_CORE, &scds) < 0) {
                    316:                syslog(LOG_ERR, "couldn't get core dump size: %m");
                    317:                scds.rlim_cur = scds.rlim_max = QUAD_MIN;
                    318:        }
                    319:        cds.rlim_cur = cds.rlim_max = 0;
                    320:        if (setrlimit(RLIMIT_CORE, &cds) < 0) {
                    321:                syslog(LOG_ERR, "couldn't set core dump size to 0: %m");
                    322:                scds.rlim_cur = scds.rlim_max = QUAD_MIN;
                    323:        }
                    324:
                    325:        (void)signal(SIGALRM, timedout);
                    326:        if (argc > 1) {
                    327:                needto = 0;
                    328:                (void)alarm(timeout);
                    329:        } else
                    330:                needto = 1;
                    331:        (void)signal(SIGQUIT, SIG_IGN);
                    332:        (void)signal(SIGINT, SIG_IGN);
                    333:        (void)signal(SIGHUP, SIG_IGN);
                    334:        (void)setpriority(PRIO_PROCESS, 0, 0);
                    335:
1.36      millert   336: #ifdef notyet
                    337:        /* XXX - we don't (yet) support per-tty auth stuff */
                    338:        /* BSDi uses a ttys.conf file but we could just overload /etc/ttys */
                    339:        /*
                    340:         * Classify the attempt.
                    341:         * By default we use the value in the ttys file.
                    342:         * If there is a classify script we run that as
                    343:         *
                    344:         *      classify [-f] [username]
                    345:         */
                    346:        if (type = getttyauth(tty))
                    347:                auth_setoption(as, "auth_type", type);
                    348: #endif
                    349:
                    350:        /* get the default login class */
1.47      deraadt   351:        if ((lc = login_getclass(0)) == NULL) { /* get the default class */
1.36      millert   352:                warnx("Failure to retrieve default class");
                    353:                quickexit(1);
                    354:        }
1.37      millert   355:        timeout = (u_int)login_getcapnum(lc, "login-timeout", 300, 300);
1.36      millert   356:        if ((script = login_getcapstr(lc, "classify", NULL, NULL)) != NULL) {
                    357:                unsetenv("AUTH_TYPE");
                    358:                unsetenv("REMOTE_NAME");
                    359:                if (script[0] != '/') {
                    360:                        syslog(LOG_ERR, "Invalid classify script: %s", script);
                    361:                        warnx("Classification failure");
                    362:                        quickexit(1);
                    363:                }
                    364:                shell = strrchr(script, '/') + 1;
                    365:                auth_setstate(as, AUTH_OKAY);
1.47      deraadt   366:                auth_call(as, script, shell,
1.49      millert   367:                    fflag ? "-f" : username, fflag ? username : 0, (char *)0);
1.36      millert   368:                if (!(auth_getstate(as) & AUTH_ALLOW))
                    369:                        quickexit(1);
                    370:                auth_setenv(as);
                    371:                if ((p = getenv("AUTH_TYPE")) != NULL &&
                    372:                    strncmp(p, "auth-", 5) == 0)
                    373:                        type = p;
                    374:                if ((p = getenv("REMOTE_NAME")) != NULL)
                    375:                        hostname = p;
                    376:                /*
                    377:                 * we may have changed some values, reset them
                    378:                 */
                    379:                auth_clroptions(as);
                    380:                if (type)
                    381:                        auth_setoption(as, "auth_type", type);
                    382:                if (fqdn)
                    383:                        auth_setoption(as, "fqdn", fqdn);
                    384:                if (hostname)
                    385:                        auth_setoption(as, "hostname", hostname);
                    386:                if (lipaddr)
                    387:                        auth_setoption(as, "local_addr", lipaddr);
                    388:                if (ripaddr)
                    389:                        auth_setoption(as, "remote_addr", ripaddr);
                    390:        }
                    391:
                    392:        /*
                    393:         * Request the things like the approval script print things
                    394:         * to stdout (in particular, the nologins files)
                    395:         */
                    396:        auth_setitem(as, AUTHV_INTERACTIVE, "True");
                    397:
1.1       deraadt   398:        for (cnt = 0;; ask = 1) {
1.36      millert   399:                /*
                    400:                 * Clean up our current authentication session.
                    401:                 * Options are not cleared so we need to clear any
                    402:                 * we might set below.
                    403:                 */
                    404:                auth_clean(as);
                    405:                auth_clroption(as, "style");
                    406:                auth_clroption(as, "lastchance");
                    407:
                    408:                lastchance = 0;
                    409:
1.1       deraadt   410:                if (ask) {
                    411:                        fflag = 0;
                    412:                        getloginname();
                    413:                }
1.36      millert   414:                if (needto) {
                    415:                        needto = 0;
                    416:                        alarm(timeout);
                    417:                }
1.47      deraadt   418:                if ((style = strchr(username, ':')) != NULL)
1.36      millert   419:                        *style++ = '\0';
                    420:                if (fullname)
                    421:                        free(fullname);
                    422:                if (auth_setitem(as, AUTHV_NAME, username) < 0 ||
                    423:                    (fullname = strdup(username)) == NULL) {
                    424:                        syslog(LOG_ERR, "%m");
                    425:                        warn(NULL);
                    426:                        quickexit(1);
                    427:                }
1.1       deraadt   428:                rootlogin = 0;
1.55      millert   429:                if ((instance = strchr(username, '/')) != NULL) {
1.40      millert   430:                        if (strncmp(instance + 1, "root", 4) == 0)
1.1       deraadt   431:                                rootlogin = 1;
                    432:                        *instance++ = '\0';
                    433:                } else
                    434:                        instance = "";
1.36      millert   435:
1.1       deraadt   436:                if (strlen(username) > UT_NAMESIZE)
                    437:                        username[UT_NAMESIZE] = '\0';
                    438:
                    439:                /*
                    440:                 * Note if trying multiple user names; log failures for
                    441:                 * previous user name, but don't bother logging one failure
                    442:                 * for nonexistent name (mistyped username).
                    443:                 */
                    444:                if (failures && strcmp(tbuf, username)) {
                    445:                        if (failures > (pwd ? 0 : 1))
                    446:                                badlogin(tbuf);
                    447:                        failures = 0;
                    448:                }
1.36      millert   449:                (void)strlcpy(tbuf, username, sizeof(tbuf));
1.1       deraadt   450:
1.36      millert   451:                if ((pwd = getpwnam(username)) != NULL &&
                    452:                    auth_setpwd(as, pwd) < 0) {
                    453:                        syslog(LOG_ERR, "%m");
                    454:                        warn(NULL);
                    455:                        quickexit(1);
                    456:                }
1.1       deraadt   457:
1.36      millert   458:                lc = login_getclass(pwd ? pwd->pw_class : NULL);
                    459:                if (!lc)
                    460:                        goto failed;
1.1       deraadt   461:
1.36      millert   462:                style = login_getstyle(lc, style, type);
                    463:                if (!style)
                    464:                        goto failed;
1.1       deraadt   465:
1.36      millert   466:                /*
1.37      millert   467:                 * We allow "login-tries" attempts to login but start
                    468:                 * slowing down after "login-backoff" attempts.
                    469:                 */
                    470:                tries = (int)login_getcapnum(lc, "login-tries", 10, 10);
                    471:                backoff = (int)login_getcapnum(lc, "login-backoff", 3, 3);
                    472:
                    473:                /*
1.36      millert   474:                 * Turn off the fflag if we have an an invalid user
                    475:                 * or we are not root and we are trying to change uids.
                    476:                 */
                    477:                if (!pwd || (uid && uid != pwd->pw_uid))
                    478:                        fflag = 0;
1.1       deraadt   479:
1.36      millert   480:                if (pwd && pwd->pw_uid == 0)
                    481:                        rootlogin = 1;
1.25      millert   482:
1.36      millert   483:                /*
                    484:                 * If we do not have the force flag authenticate the user
                    485:                 */
1.44      millert   486:                if (!fflag) {
1.36      millert   487:                        lastchance =
                    488:                            login_getcaptime(lc, "password-dead", 0, 0) != 0;
                    489:                        if (lastchance)
                    490:                                auth_setoption(as, "lastchance", "yes");
                    491:                        /*
                    492:                         * Once we start asking for a password
                    493:                         *  we want to log a failure on a hup.
                    494:                         */
                    495:                        signal(SIGHUP, sighup);
                    496:                        auth_verify(as, style, NULL, lc->lc_class, NULL);
                    497:                        authok = auth_getstate(as);
                    498:                        /*
                    499:                         * If their password expired and it has not been
                    500:                         * too long since then, give the user one last
                    501:                         * chance to change their password
                    502:                         */
                    503:                        if ((authok & AUTH_PWEXPIRED) && lastchance) {
                    504:                                authok = AUTH_OKAY;
                    505:                        } else
                    506:                                lastchance = 0;
                    507:                        if ((authok & AUTH_ALLOW) == 0)
                    508:                                goto failed;
                    509:                        if (auth_setoption(as, "style", style) < 0) {
                    510:                                syslog(LOG_ERR, "%m");
                    511:                                warn(NULL);
                    512:                                quickexit(1);
1.25      millert   513:                        }
1.1       deraadt   514:                }
1.36      millert   515:                /*
                    516:                 * explicitly reject users without password file entries
                    517:                 */
1.47      deraadt   518:                if (pwd == NULL)
1.36      millert   519:                        goto failed;
1.1       deraadt   520:
                    521:                /*
1.36      millert   522:                 * If trying to log in as root on an insecure terminal,
                    523:                 * refuse the login attempt unless the authentication
                    524:                 * style explicitly says a root login is okay.
1.1       deraadt   525:                 */
1.44      millert   526:                if (pwd && rootlogin && !rootterm(tty))
1.36      millert   527:                        goto failed;
1.29      millert   528:
1.36      millert   529:                if (fflag) {
                    530:                        type = 0;
                    531:                        style = "forced";
                    532:                }
                    533:                break;
                    534:
                    535: failed:
                    536:                if (authok & AUTH_SILENT)
                    537:                        quickexit(0);
1.29      millert   538:                if (rootlogin && !rootterm(tty)) {
1.36      millert   539:                        warnx("%s login refused on this terminal.",
                    540:                            fullname);
1.1       deraadt   541:                        if (hostname)
                    542:                                syslog(LOG_NOTICE,
1.14      millert   543:                                    "LOGIN %s REFUSED FROM %s%s%s ON TTY %s",
1.36      millert   544:                                    fullname, rusername ? rusername : "",
1.14      millert   545:                                    rusername ? "@" : "", hostname, tty);
1.1       deraadt   546:                        else
                    547:                                syslog(LOG_NOTICE,
                    548:                                    "LOGIN %s REFUSED ON TTY %s",
1.47      deraadt   549:                                    fullname, tty);
1.36      millert   550:                } else {
                    551:                        if (!as || (p = auth_getvalue(as, "errormsg")) == NULL)
                    552:                                p = "Login incorrect";
                    553:                        (void)printf("%s\n", p);
                    554:                }
1.1       deraadt   555:                failures++;
1.13      millert   556:                if (pwd)
1.14      millert   557:                        log_failedlogin(pwd->pw_uid, hostname, rusername, tty);
1.37      millert   558:                /*
                    559:                 * By default, we allow 10 tries, but after 3 we start
                    560:                 * backing off to slow down password guessers.
                    561:                 */
                    562:                if (++cnt > backoff) {
                    563:                        if (cnt >= tries) {
1.1       deraadt   564:                                badlogin(username);
                    565:                                sleepexit(1);
                    566:                        }
1.37      millert   567:                        sleep((u_int)((cnt - backoff) * tries / 2));
1.1       deraadt   568:                }
                    569:        }
                    570:
                    571:        /* committed to login -- turn off timeout */
1.36      millert   572:        (void)alarm(0);
1.1       deraadt   573:
                    574:        endpwent();
                    575:
1.30      millert   576:        shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
                    577:        if (*shell == '\0')
                    578:                shell = _PATH_BSHELL;
                    579:        else if (strlen(shell) >= MAXPATHLEN) {
                    580:                syslog(LOG_ERR, "shell path too long: %s", shell);
                    581:                warnx("invalid shell");
1.36      millert   582:                quickexit(1);
1.24      art       583:        }
1.1       deraadt   584:
                    585:        /* Destroy environment unless user has requested its preservation. */
1.24      art       586:        if (!pflag) {
1.22      deraadt   587:                if ((environ = calloc(1, sizeof (char *))) == NULL)
                    588:                        err(1, "calloc");
1.24      art       589:        } else {
1.9       millert   590:                char **cpp, **cpp2;
                    591:
                    592:                for (cpp2 = cpp = environ; *cpp; cpp++) {
                    593:                        if (strncmp(*cpp, "LD_", 3) &&
1.29      millert   594:                            strncmp(*cpp, "ENV=", 4) &&
                    595:                            strncmp(*cpp, "BASH_ENV=", 9) &&
1.9       millert   596:                            strncmp(*cpp, "IFS=", 4))
                    597:                                *cpp2++ = *cpp;
                    598:                }
                    599:                *cpp2 = 0;
                    600:        }
1.30      millert   601:        /* Note: setusercontext(3) will set PATH */
1.34      deraadt   602:        if (setenv("HOME", pwd->pw_dir, 1) == -1 ||
                    603:            setenv("SHELL", shell, 1) == -1) {
                    604:                warn("unable to setenv()");
1.36      millert   605:                quickexit(1);
1.34      deraadt   606:        }
1.1       deraadt   607:        if (term[0] == '\0')
1.29      millert   608:                (void)strlcpy(term, stypeof(tty), sizeof(term));
1.34      deraadt   609:        if (setenv("TERM", term, 0) == -1 ||
                    610:            setenv("LOGNAME", pwd->pw_name, 1) == -1 ||
                    611:            setenv("USER", pwd->pw_name, 1) == -1) {
                    612:                warn("unable to setenv()");
1.36      millert   613:                quickexit(1);
1.34      deraadt   614:        }
                    615:        if (hostname) {
                    616:                if (setenv("REMOTEHOST", hostname, 1) == -1) {
                    617:                        warn("unable to setenv()");
1.36      millert   618:                        quickexit(1);
1.34      deraadt   619:                }
                    620:        }
                    621:        if (rusername) {
                    622:                if (setenv("REMOTEUSER", rusername, 1) == -1) {
                    623:                        warn("unable to setenv()");
1.36      millert   624:                        quickexit(1);
1.34      deraadt   625:                }
                    626:        }
1.36      millert   627:
                    628:        if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH)) {
                    629:                warn("unable to set user context");
                    630:                quickexit(1);
                    631:        }
                    632:        auth_setenv(as);
                    633:
                    634:        /* if user not super-user, check for disabled logins */
                    635:        if (!rootlogin)
                    636:                auth_checknologin(lc);
                    637:
                    638:        setegid(pwd->pw_gid);
                    639:        seteuid(pwd->pw_uid);
                    640:
                    641:        homeless = chdir(pwd->pw_dir);
                    642:        if (homeless) {
                    643:                if (login_getcapbool(lc, "requirehome", 0)) {
                    644:                        (void)printf("No home directory %s!\n", pwd->pw_dir);
                    645:                        quickexit(1);
1.34      deraadt   646:                }
1.36      millert   647:                if (chdir("/"))
                    648:                        quickexit(0);
1.34      deraadt   649:        }
1.36      millert   650:
                    651:        quietlog = ((strcmp(pwd->pw_shell, "/sbin/nologin") == 0) ||
                    652:            login_getcapbool(lc, "hushlogin", 0) ||
                    653:            (access(_PATH_HUSHLOGIN, F_OK) == 0));
                    654:
                    655:        seteuid(0);
                    656:        setegid(0);     /* XXX use a saved gid instead? */
                    657:
                    658:        if ((p = auth_getvalue(as, "warnmsg")) != NULL)
                    659:                (void)printf("WARNING: %s\n\n", p);
                    660:
                    661:        expire = auth_check_expire(as);
                    662:        if (expire < 0) {
                    663:                (void)printf("Sorry -- your account has expired.\n");
                    664:                quickexit(1);
                    665:        } else if (expire > 0 && !quietlog) {
                    666:                warning = login_getcaptime(lc, "expire-warn",
                    667:                    2 * DAYSPERWEEK * SECSPERDAY, 2 * DAYSPERWEEK * SECSPERDAY);
                    668:                if (expire < warning)
1.47      deraadt   669:                        (void)printf("Warning: your account expires on %s",
1.36      millert   670:                            ctime(&pwd->pw_expire));
1.34      deraadt   671:        }
1.36      millert   672:
                    673:        /* Nothing else left to fail -- really log in. */
                    674:        (void)signal(SIGHUP, SIG_DFL);
                    675:        memset(&utmp, 0, sizeof(utmp));
                    676:        (void)time(&utmp.ut_time);
                    677:        (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
                    678:        if (hostname)
                    679:                (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
                    680:        (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
                    681:        login(&utmp);
                    682:
                    683:        if (!quietlog)
                    684:                (void)check_failedlogin(pwd->pw_uid);
                    685:        dolastlog(quietlog);
                    686:
                    687:        login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
                    688:
                    689:        (void)chown(ttyn, pwd->pw_uid,
                    690:            (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
                    691:
1.1       deraadt   692:        /* If fflag is on, assume caller/authenticator has logged root login. */
1.24      art       693:        if (rootlogin && fflag == 0) {
1.1       deraadt   694:                if (hostname)
1.14      millert   695:                        syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s%s%s",
                    696:                            username, tty, rusername ? rusername : "",
                    697:                            rusername ? "@" : "", hostname);
1.1       deraadt   698:                else
                    699:                        syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
1.24      art       700:        }
1.1       deraadt   701:
                    702:        if (!quietlog) {
1.36      millert   703:                if ((copyright =
                    704:                    login_getcapstr(lc, "copyright", NULL, NULL)) != NULL)
                    705:                        auth_cat(copyright);
1.1       deraadt   706:                motd();
                    707:                (void)snprintf(tbuf,
                    708:                    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
                    709:                if (stat(tbuf, &st) == 0 && st.st_size != 0)
                    710:                        (void)printf("You have %smail.\n",
                    711:                            (st.st_mtime > st.st_atime) ? "new " : "");
                    712:        }
                    713:
                    714:        (void)signal(SIGALRM, SIG_DFL);
                    715:        (void)signal(SIGQUIT, SIG_DFL);
1.36      millert   716:        (void)signal(SIGHUP, SIG_DFL);
1.1       deraadt   717:        (void)signal(SIGINT, SIG_DFL);
                    718:        (void)signal(SIGTSTP, SIG_IGN);
                    719:
                    720:        tbuf[0] = '-';
1.30      millert   721:        (void)strlcpy(tbuf + 1, (p = strrchr(shell, '/')) ?
1.36      millert   722:            p + 1 : shell, sizeof(tbuf) - 1);
                    723:
                    724:        if ((scds.rlim_cur != QUAD_MIN || scds.rlim_max != QUAD_MIN) &&
                    725:            setrlimit(RLIMIT_CORE, &scds) < 0)
                    726:                syslog(LOG_ERR, "couldn't reset core dump size: %m");
                    727:
                    728:        if (lastchance)
1.47      deraadt   729:                (void)printf("WARNING: Your password has expired."
                    730:                    "  You must change your password, now!\n");
1.1       deraadt   731:
1.38      millert   732:        if (setusercontext(lc, pwd, rootlogin ? 0 : pwd->pw_uid,
1.36      millert   733:            LOGIN_SETALL & ~LOGIN_SETPATH) < 0) {
1.30      millert   734:                warn("unable to set user context");
1.36      millert   735:                quickexit(1);
                    736:        }
                    737:
                    738:        if (homeless) {
                    739:                (void)printf("No home directory %s!\n", pwd->pw_dir);
                    740:                (void)printf("Logging in with home = \"/\".\n");
                    741:                (void)setenv("HOME", "/", 1);
1.15      tholo     742:        }
1.30      millert   743:
1.36      millert   744:        if (auth_approval(as, lc, NULL, "login") == 0) {
                    745:                if (auth_getstate(as) & AUTH_EXPIRED)
                    746:                        (void)printf("Sorry -- your account has expired.\n");
                    747:                else
                    748:                        (void)printf("approval failure\n");
                    749:                quickexit(1);
                    750:        }
                    751:
                    752:        /*
                    753:         * The last thing we do is discard all of the open file descriptors.
                    754:         * Last because the C library may have some open.
                    755:         */
1.53      deraadt   756:        closefrom(STDERR_FILENO + 1);
1.36      millert   757:
                    758:        /*
                    759:         * Close the authentication session, make sure it is marked
                    760:         * as okay so no files are removed.
                    761:         */
                    762:        auth_setstate(as, AUTH_OKAY);
                    763:        auth_close(as);
                    764:
1.42      deraadt   765:        execlp(shell, tbuf, (char *)NULL);
1.30      millert   766:        err(1, "%s", shell);
1.1       deraadt   767: }
                    768:
                    769: /*
1.36      millert   770:  * Allow for a '.' and 16 characters for any instance as well as
                    771:  * space for a ':' and 16 charcters defining the authentication type.
1.1       deraadt   772:  */
1.36      millert   773: #define NBUFSIZ                (UT_NAMESIZE + 1 + 16 + 1 + 16)
1.1       deraadt   774:
                    775: void
1.47      deraadt   776: getloginname(void)
1.1       deraadt   777: {
1.47      deraadt   778:        static char nbuf[NBUFSIZ], *p;
1.1       deraadt   779:        int ch;
                    780:
                    781:        for (;;) {
                    782:                (void)printf("login: ");
                    783:                for (p = nbuf; (ch = getchar()) != '\n'; ) {
                    784:                        if (ch == EOF) {
                    785:                                badlogin(username);
1.36      millert   786:                                quickexit(0);
1.1       deraadt   787:                        }
                    788:                        if (p < nbuf + (NBUFSIZ - 1))
                    789:                                *p++ = ch;
                    790:                }
1.24      art       791:                if (p > nbuf) {
1.1       deraadt   792:                        if (nbuf[0] == '-')
                    793:                                (void)fprintf(stderr,
                    794:                                    "login names may not start with '-'.\n");
                    795:                        else {
                    796:                                *p = '\0';
                    797:                                username = nbuf;
                    798:                                break;
                    799:                        }
1.24      art       800:                }
1.1       deraadt   801:        }
                    802: }
                    803:
                    804: int
1.47      deraadt   805: rootterm(char *ttyn)
1.1       deraadt   806: {
                    807:        struct ttyent *t;
                    808:
1.36      millert   809:        /* XXX - stash output of getttynam() elsewhere */
1.1       deraadt   810:        return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
                    811: }
                    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;
1.48      millert   818:        struct sigaction sa, osa;
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;
1.48      millert   824:
                    825:        memset(&sa, 0, sizeof(sa));
                    826:        sa.sa_handler = sigint;
                    827:        sigemptyset(&sa.sa_mask);
                    828:        sa.sa_flags = 0;                /* don't set SA_RESTART */
                    829:        (void)sigaction(SIGINT, &sa, &osa);
                    830:
                    831:        /* read and spew motd until EOF, error, or SIGINT */
                    832:        while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 &&
                    833:            write(STDOUT_FILENO, tbuf, nchars) == nchars)
                    834:                ;
                    835:
                    836:        (void)sigaction(SIGINT, &osa, NULL);
1.1       deraadt   837:        (void)close(fd);
                    838: }
                    839:
                    840: /* ARGSUSED */
                    841: void
1.47      deraadt   842: sigint(int signo)
1.1       deraadt   843: {
1.52      otto      844:        return;                 /* just interrupt syscall */
1.1       deraadt   845: }
                    846:
                    847: /* ARGSUSED */
                    848: void
1.47      deraadt   849: timedout(int signo)
1.1       deraadt   850: {
1.47      deraadt   851:        char warn[1024];
                    852:
                    853:        snprintf(warn, sizeof warn,
                    854:            "Login timed out after %d seconds\n", timeout);
                    855:        write(STDERR_FILENO, warn, strlen(warn));
1.37      millert   856:        if (username)
                    857:                badlogin(username);
1.47      deraadt   858:        _exit(0);
1.1       deraadt   859: }
                    860:
                    861: void
1.47      deraadt   862: dolastlog(int quiet)
1.1       deraadt   863: {
                    864:        struct lastlog ll;
                    865:        int fd;
                    866:
                    867:        if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1.26      millert   868:                (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), SEEK_SET);
1.1       deraadt   869:                if (!quiet) {
                    870:                        if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
                    871:                            ll.ll_time != 0) {
                    872:                                (void)printf("Last login: %.*s ",
                    873:                                    24-5, (char *)ctime(&ll.ll_time));
1.14      millert   874:                                (void)printf("on %.*s",
                    875:                                    (int)sizeof(ll.ll_line),
                    876:                                    ll.ll_line);
1.1       deraadt   877:                                if (*ll.ll_host != '\0')
1.14      millert   878:                                        (void)printf(" from %.*s",
1.1       deraadt   879:                                            (int)sizeof(ll.ll_host),
                    880:                                            ll.ll_host);
1.14      millert   881:                                (void)putchar('\n');
1.1       deraadt   882:                        }
1.26      millert   883:                        (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll),
                    884:                            SEEK_SET);
1.1       deraadt   885:                }
                    886:                memset((void *)&ll, 0, sizeof(ll));
                    887:                (void)time(&ll.ll_time);
                    888:                (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
                    889:                if (hostname)
                    890:                        (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
                    891:                (void)write(fd, (char *)&ll, sizeof(ll));
                    892:                (void)close(fd);
                    893:        }
                    894: }
                    895:
                    896: void
1.47      deraadt   897: badlogin(char *name)
1.1       deraadt   898: {
1.47      deraadt   899:        struct syslog_data sdata = SYSLOG_DATA_INIT;
                    900:
1.1       deraadt   901:        if (failures == 0)
                    902:                return;
                    903:        if (hostname) {
1.47      deraadt   904:                syslog_r(LOG_NOTICE, &sdata,
                    905:                    "%d LOGIN FAILURE%s FROM %s%s%s",
1.14      millert   906:                    failures, failures > 1 ? "S" : "",
                    907:                    rusername ? rusername : "", rusername ? "@" : "", hostname);
1.47      deraadt   908:                syslog_r(LOG_AUTHPRIV|LOG_NOTICE, &sdata,
1.14      millert   909:                    "%d LOGIN FAILURE%s FROM %s%s%s, %s",
                    910:                    failures, failures > 1 ? "S" : "",
                    911:                    rusername ? rusername : "", rusername ? "@" : "",
                    912:                    hostname, name);
1.1       deraadt   913:        } else {
1.47      deraadt   914:                syslog_r(LOG_NOTICE, &sdata,
                    915:                    "%d LOGIN FAILURE%s ON %s",
1.1       deraadt   916:                    failures, failures > 1 ? "S" : "", tty);
1.47      deraadt   917:                syslog_r(LOG_AUTHPRIV|LOG_NOTICE, &sdata,
1.1       deraadt   918:                    "%d LOGIN FAILURE%s ON %s, %s",
                    919:                    failures, failures > 1 ? "S" : "", tty, name);
                    920:        }
                    921: }
                    922:
                    923: #undef UNKNOWN
                    924: #define        UNKNOWN "su"
                    925:
                    926: char *
1.47      deraadt   927: stypeof(char *ttyid)
1.1       deraadt   928: {
                    929:        struct ttyent *t;
                    930:
1.30      millert   931:        return (ttyid && (t = getttynam(ttyid)) ? t->ty_type :
                    932:            login_getcapstr(lc, "term", UNKNOWN, UNKNOWN));
1.1       deraadt   933: }
                    934:
                    935: void
1.47      deraadt   936: sleepexit(int eval)
1.1       deraadt   937: {
1.36      millert   938:        auth_close(as);
1.1       deraadt   939:        (void)sleep(5);
                    940:        exit(eval);
1.11      millert   941: }
                    942:
                    943: void
1.47      deraadt   944: quickexit(int eval)
1.36      millert   945: {
                    946:        if (as)
                    947:                auth_close(as);
                    948:        exit(eval);
                    949: }
                    950:
                    951:
                    952: void
1.47      deraadt   953: sighup(int signum)
1.11      millert   954: {
                    955:        if (username)
                    956:                badlogin(username);
1.47      deraadt   957:        _exit(0);
1.1       deraadt   958: }