[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.52

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