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

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