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

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