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

1.33    ! millert     1: /*     $OpenBSD: login.c,v 1.32 2000/08/24 20:08:06 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:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)login.c    8.4 (Berkeley) 4/2/94";
                     46: #endif
1.33    ! millert    47: static char rcsid[] = "$OpenBSD: login.c,v 1.32 2000/08/24 20:08:06 millert Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: /*
                     51:  * login [ name ]
                     52:  * login -h hostname   (for telnetd, etc.)
                     53:  * login -f name       (for pre-authenticated login: datakit, xterm, etc.)
                     54:  */
                     55:
                     56: #include <sys/param.h>
                     57: #include <sys/stat.h>
                     58: #include <sys/time.h>
                     59: #include <sys/resource.h>
1.11      millert    60: #include <sys/wait.h>
1.1       deraadt    61:
                     62: #include <err.h>
                     63: #include <errno.h>
1.27      millert    64: #include <fcntl.h>
1.1       deraadt    65: #include <grp.h>
1.30      millert    66: #include <login_cap.h>
1.1       deraadt    67: #include <pwd.h>
                     68: #include <setjmp.h>
                     69: #include <signal.h>
                     70: #include <stdio.h>
                     71: #include <stdlib.h>
                     72: #include <string.h>
                     73: #include <syslog.h>
                     74: #include <ttyent.h>
                     75: #include <tzfile.h>
                     76: #include <unistd.h>
                     77: #include <utmp.h>
1.3       deraadt    78: #include <util.h>
1.24      art        79: #include <skey.h>
1.1       deraadt    80:
                     81: #include "pathnames.h"
                     82:
                     83: void    badlogin __P((char *));
                     84: void    checknologin __P((void));
                     85: void    dolastlog __P((int));
                     86: void    getloginname __P((void));
                     87: void    motd __P((void));
                     88: int     rootterm __P((char *));
                     89: void    sigint __P((int));
1.11      millert    90: void    sighup __P((int));
1.1       deraadt    91: void    sleepexit __P((int));
                     92: char   *stypeof __P((char *));
                     93: void    timedout __P((int));
                     94: int     pwcheck __P((char *, char *, char *, char *));
                     95: #if defined(KERBEROS) || defined(KERBEROS5)
                     96: int     klogin __P((struct passwd *, char *, char *, char *));
                     97: void    kdestroy __P((void));
                     98: void    dofork __P((void));
1.24      art        99: void    kgettokens __P((char *));
1.1       deraadt   100: #endif
                    101:
1.13      millert   102: extern int check_failedlogin __P((uid_t));
1.14      millert   103: extern void log_failedlogin __P((uid_t, char *, char *, char *));
1.1       deraadt   104:
                    105: #define        TTYGRPNAME      "tty"           /* name of group to own ttys */
                    106:
                    107: /*
                    108:  * This bounds the time given to login.  Not a define so it can
                    109:  * be patched on machines where it's too small.
1.30      millert   110:  * XXX - should be a login.conf variable!
1.1       deraadt   111:  */
1.30      millert   112: u_int          timeout = 300;
1.1       deraadt   113:
                    114: #if defined(KERBEROS) || defined(KERBEROS5)
1.30      millert   115: int            notickets = 1;
                    116: char           *instance;
                    117: char           *krbtkfile_env;
                    118: int            authok;
1.1       deraadt   119: #endif
                    120:
1.30      millert   121: struct passwd  *pwd;
                    122: login_cap_t    *lc = NULL;
                    123: int            failures;
                    124: char           term[64], *hostname, *tty;
                    125: char           *username = NULL, *rusername = NULL;
1.1       deraadt   126:
                    127: int
                    128: main(argc, argv)
                    129:        int argc;
                    130:        char *argv[];
                    131: {
                    132:        extern char **environ;
                    133:        struct group *gr;
                    134:        struct stat st;
                    135:        struct timeval tp;
                    136:        struct utmp utmp;
1.14      millert   137:        int ask, ch, cnt, fflag, hflag, pflag, uflag, quietlog, rootlogin, rval;
1.1       deraadt   138:        uid_t uid;
1.30      millert   139:        char *domain, *p, *salt, *ttyn, *shell;
1.1       deraadt   140:        char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
                    141:        char localhost[MAXHOSTNAMELEN];
                    142:
                    143:        (void)signal(SIGALRM, timedout);
                    144:        (void)alarm(timeout);
                    145:        (void)signal(SIGQUIT, SIG_IGN);
                    146:        (void)signal(SIGINT, SIG_IGN);
1.11      millert   147:        (void)signal(SIGHUP, sighup);
1.1       deraadt   148:        (void)setpriority(PRIO_PROCESS, 0, 0);
                    149:
                    150:        openlog("login", LOG_ODELAY, LOG_AUTH);
                    151:
                    152:        /*
                    153:         * -p is used by getty to tell login not to destroy the environment
                    154:         * -f is used to skip a second login authentication
                    155:         * -h is used by other servers to pass the name of the remote
                    156:         *    host to login so that it may be placed in utmp and wtmp
                    157:         */
                    158:        domain = NULL;
                    159:        if (gethostname(localhost, sizeof(localhost)) < 0)
                    160:                syslog(LOG_ERR, "couldn't get local hostname: %m");
                    161:        else
                    162:                domain = strchr(localhost, '.');
1.21      deraadt   163:        if (domain) {
                    164:                domain++;
                    165:                if (*domain && strchr(domain, '.') == NULL)
                    166:                        domain = localhost;
                    167:        }
1.1       deraadt   168:
                    169:        fflag = hflag = pflag = 0;
                    170:        uid = getuid();
1.16      millert   171:        while ((ch = getopt(argc, argv, "fh:u:p")) != -1)
1.1       deraadt   172:                switch (ch) {
                    173:                case 'f':
                    174:                        fflag = 1;
                    175:                        break;
                    176:                case 'h':
                    177:                        if (uid)
                    178:                                errx(1, "-h option: %s", strerror(EPERM));
                    179:                        hflag = 1;
                    180:                        if (domain && (p = strchr(optarg, '.')) &&
1.21      deraadt   181:                            strcasecmp(p+1, domain) == 0)
1.1       deraadt   182:                                *p = 0;
                    183:                        hostname = optarg;
                    184:                        break;
                    185:                case 'p':
                    186:                        pflag = 1;
                    187:                        break;
1.14      millert   188:                case 'u':
                    189:                        if (uid)
                    190:                                errx(1, "-u option: %s", strerror(EPERM));
                    191:                        uflag = 1;
                    192:                        rusername = optarg;
                    193:                        break;
1.1       deraadt   194:                case '?':
                    195:                default:
                    196:                        if (!uid)
                    197:                                syslog(LOG_ERR, "invalid flag %c", ch);
                    198:                        (void)fprintf(stderr,
                    199:                            "usage: login [-fp] [-h hostname] [username]\n");
                    200:                        exit(1);
                    201:                }
                    202:        argc -= optind;
                    203:        argv += optind;
                    204:
                    205:        if (*argv) {
                    206:                username = *argv;
                    207:                ask = 0;
                    208:        } else
                    209:                ask = 1;
                    210:
                    211:        for (cnt = getdtablesize(); cnt > 2; cnt--)
                    212:                (void)close(cnt);
                    213:
                    214:        ttyn = ttyname(STDIN_FILENO);
                    215:        if (ttyn == NULL || *ttyn == '\0') {
                    216:                (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
                    217:                ttyn = tname;
                    218:        }
1.12      millert   219:        if ((tty = strrchr(ttyn, '/')))
1.1       deraadt   220:                ++tty;
                    221:        else
                    222:                tty = ttyn;
                    223:
                    224:        for (cnt = 0;; ask = 1) {
                    225: #if defined(KERBEROS) || defined(KERBEROS5)
                    226:                kdestroy();
                    227: #endif
                    228:                if (ask) {
                    229:                        fflag = 0;
                    230:                        getloginname();
                    231:                }
                    232:                rootlogin = 0;
1.18      flipk     233:
                    234: #if defined(KERBEROS) || defined(KERBEROS5)
                    235:                /*
                    236:                 * Why should anyone with a root instance be able
                    237:                 * to be root here?
                    238:                 */
1.17      dm        239:                instance = "";
1.18      flipk     240: #endif
1.1       deraadt   241: #ifdef KERBEROS
                    242:                if ((instance = strchr(username, '.')) != NULL) {
                    243:                        if (strncmp(instance, ".root", 5) == 0)
                    244:                                rootlogin = 1;
                    245:                        *instance++ = '\0';
                    246:                } else
                    247:                        instance = "";
                    248: #endif
                    249: #ifdef KERBEROS5
                    250:                if ((instance = strchr(username, '/')) != NULL) {
                    251:                        if (strncmp(instance, "/root", 5) == 0)
                    252:                                rootlogin = 1;
                    253:                        *instance++ = '\0';
                    254:                } else
                    255:                        instance = "";
                    256: #endif
                    257:                if (strlen(username) > UT_NAMESIZE)
                    258:                        username[UT_NAMESIZE] = '\0';
                    259:
                    260:                /*
                    261:                 * Note if trying multiple user names; log failures for
                    262:                 * previous user name, but don't bother logging one failure
                    263:                 * for nonexistent name (mistyped username).
                    264:                 */
                    265:                if (failures && strcmp(tbuf, username)) {
                    266:                        if (failures > (pwd ? 0 : 1))
                    267:                                badlogin(tbuf);
                    268:                        failures = 0;
                    269:                }
1.29      millert   270:                (void)strlcpy(tbuf, username, sizeof tbuf);
1.1       deraadt   271:
1.12      millert   272:                if ((pwd = getpwnam(username)))
1.1       deraadt   273:                        salt = pwd->pw_passwd;
                    274:                else
                    275:                        salt = "xx";
1.30      millert   276:                lc = login_getclass(pwd ? pwd->pw_class : LOGIN_DEFCLASS);
                    277:                if (!lc)
                    278:                    err(1, "unable to get login class");
1.1       deraadt   279:
                    280:                /*
1.29      millert   281:                 * If we have a valid account name, and it doesn't have a
1.1       deraadt   282:                 * password, or the -f option was specified and the caller
                    283:                 * is root or the caller isn't changing their uid, don't
                    284:                 * authenticate.
                    285:                 */
                    286:                if (pwd) {
                    287:                        if (pwd->pw_uid == 0)
                    288:                                rootlogin = 1;
                    289:
                    290:                        if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
                    291:                                /* already authenticated */
                    292:                                break;
                    293:                        } else if (pwd->pw_passwd[0] == '\0') {
                    294:                                /* pretend password okay */
                    295:                                rval = 0;
                    296:                                goto ttycheck;
                    297:                        }
                    298:                }
                    299:
                    300:                fflag = 0;
                    301:
                    302:                (void)setpriority(PRIO_PROCESS, 0, -4);
                    303:
                    304:                p = getpass("Password:");
                    305:
                    306:                if (pwd) {
                    307: #if defined(KERBEROS) || defined(KERBEROS5)
                    308:                        rval = klogin(pwd, instance, localhost, p);
                    309:                        if (rval != 0 && rootlogin && pwd->pw_uid != 0)
                    310:                                rootlogin = 0;
1.32      millert   311:                        if (rval == 1) {
                    312:                                /* Fall back on password file. */
1.1       deraadt   313:                                if (pwd->pw_uid != 0)
                    314:                                        rootlogin = 0;
                    315:                                rval = pwcheck(username, p, salt, pwd->pw_passwd);
                    316:                        }
1.32      millert   317:                        if (rval == 0)
                    318:                                authok = 1;
1.1       deraadt   319: #else
                    320:                        rval = pwcheck(username, p, salt, pwd->pw_passwd);
                    321: #endif
1.23      millert   322:                } else {
1.25      millert   323: #ifdef SKEY
                    324:                        if (strcasecmp(p, "s/key") == 0)
                    325:                                (void)skey_authenticate(username);
                    326:                        else
                    327: #endif
                    328:                        {
                    329:                                useconds_t us;
                    330:
                    331:                                /*
                    332:                                 * Sleep between 1 and 3 seconds
                    333:                                 * to emulate a crypt.
                    334:                                 */
                    335:                                us = arc4random() % 3000000;
                    336:                                usleep(us);
                    337:                        }
                    338:                        rval = 1;
1.1       deraadt   339:                }
                    340:                memset(p, 0, strlen(p));
                    341:
                    342:                (void)setpriority(PRIO_PROCESS, 0, 0);
                    343:
                    344:        ttycheck:
                    345:                /*
                    346:                 * If trying to log in as root without Kerberos,
                    347:                 * but with insecure terminal, refuse the login attempt.
                    348:                 */
                    349: #if defined(KERBEROS) || defined(KERBEROS5)
1.31      millert   350:                if (authok == 1)
1.1       deraadt   351: #endif
1.29      millert   352:                /* if logging in as root, user must be on a secure tty */
1.31      millert   353:                if (pwd && rval == 0 && (!rootlogin || rootterm(tty)))
1.29      millert   354:                        break;
                    355:
                    356:                /*
                    357:                 * We don't want to give out info to an attacker trying
                    358:                 * to guess root's password so we always say "login refused"
                    359:                 * in that case, not "Login incorrect".
                    360:                 */
                    361:                if (rootlogin && !rootterm(tty)) {
1.1       deraadt   362:                        (void)fprintf(stderr,
                    363:                            "%s login refused on this terminal.\n",
1.29      millert   364:                            pwd ? pwd->pw_name : "root");
1.1       deraadt   365:                        if (hostname)
                    366:                                syslog(LOG_NOTICE,
1.14      millert   367:                                    "LOGIN %s REFUSED FROM %s%s%s ON TTY %s",
1.29      millert   368:                                    pwd ? pwd->pw_name : "root",
                    369:                                    rusername ? rusername : "",
1.14      millert   370:                                    rusername ? "@" : "", hostname, tty);
1.1       deraadt   371:                        else
                    372:                                syslog(LOG_NOTICE,
                    373:                                    "LOGIN %s REFUSED ON TTY %s",
1.29      millert   374:                                     pwd ? pwd->pw_name : "root", tty);
                    375:                } else
                    376:                        (void)printf("Login incorrect\n");
1.1       deraadt   377:                failures++;
1.13      millert   378:                if (pwd)
1.14      millert   379:                        log_failedlogin(pwd->pw_uid, hostname, rusername, tty);
1.1       deraadt   380:                /* we allow 10 tries, but after 3 we start backing off */
                    381:                if (++cnt > 3) {
                    382:                        if (cnt >= 10) {
                    383:                                badlogin(username);
                    384:                                sleepexit(1);
                    385:                        }
                    386:                        sleep((u_int)((cnt - 3) * 5));
                    387:                }
                    388:        }
                    389:
                    390:        /* committed to login -- turn off timeout */
                    391:        (void)alarm((u_int)0);
                    392:
                    393:        endpwent();
                    394:
                    395:        /* if user not super-user, check for disabled logins */
                    396:        if (!rootlogin)
                    397:                checknologin();
                    398:
1.5       deraadt   399:        setegid(pwd->pw_gid);
                    400:        seteuid(pwd->pw_uid);
                    401:
1.1       deraadt   402:        if (chdir(pwd->pw_dir) < 0) {
                    403:                (void)printf("No home directory %s!\n", pwd->pw_dir);
1.30      millert   404:                if (login_getcapbool(lc, "requirehome", 0))
                    405:                        exit(1);
1.1       deraadt   406:                if (chdir("/"))
                    407:                        exit(0);
                    408:                pwd->pw_dir = "/";
                    409:                (void)printf("Logging in with home = \"/\".\n");
                    410:        }
                    411:
1.30      millert   412:        shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
                    413:        if (*shell == '\0')
                    414:                shell = _PATH_BSHELL;
                    415:        else if (strlen(shell) >= MAXPATHLEN) {
                    416:                syslog(LOG_ERR, "shell path too long: %s", shell);
                    417:                warnx("invalid shell");
                    418:                sleepexit(1);
                    419:        }
                    420:
1.19      downsj    421:        quietlog = ((strcmp(pwd->pw_shell, "/sbin/nologin") == 0) ||
1.30      millert   422:            login_getcapbool(lc, "hushlogin", 0) ||
                    423:            (access(_PATH_HUSHLOGIN, F_OK) == 0));
1.5       deraadt   424:
                    425:        seteuid(0);
                    426:        setegid(0);     /* XXX use a saved gid instead? */
1.1       deraadt   427:
                    428:        if (pwd->pw_change || pwd->pw_expire)
                    429:                (void)gettimeofday(&tp, (struct timezone *)NULL);
1.33    ! millert   430:        if (pwd->pw_expire) {
        !           431:                if (tp.tv_sec >= pwd->pw_expire) {
        !           432:                        (void)printf("Sorry -- your account has expired.\n");
        !           433:                        sleepexit(1);
        !           434:                } else if (!quietlog &&pwd->pw_expire - tp.tv_sec <
        !           435:                    login_getcaptime(lc, "expire-warn",
        !           436:                    2 * DAYSPERWEEK * SECSPERDAY, 2 * DAYSPERWEEK * SECSPERDAY))
        !           437:                        (void)printf("Warning: your account expires on %s",
        !           438:                            ctime(&pwd->pw_expire));
        !           439:        }
1.24      art       440:        if (pwd->pw_change) {
1.1       deraadt   441:                if (tp.tv_sec >= pwd->pw_change) {
                    442:                        (void)printf("Sorry -- your password has expired.\n");
                    443:                        sleepexit(1);
1.30      millert   444:                } else if (!quietlog && pwd->pw_change - tp.tv_sec <
                    445:                    login_getcaptime(lc, "password-warn",
                    446:                    2 * DAYSPERWEEK * SECSPERDAY, 2 * DAYSPERWEEK * SECSPERDAY))
1.1       deraadt   447:                        (void)printf("Warning: your password expires on %s",
                    448:                            ctime(&pwd->pw_change));
1.24      art       449:        }
1.1       deraadt   450:
                    451:        /* Nothing else left to fail -- really log in. */
1.11      millert   452:        (void)signal(SIGHUP, SIG_DFL);
1.1       deraadt   453:        memset((void *)&utmp, 0, sizeof(utmp));
                    454:        (void)time(&utmp.ut_time);
                    455:        (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
                    456:        if (hostname)
                    457:                (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
                    458:        (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
                    459:        login(&utmp);
                    460:
1.13      millert   461:        if (!quietlog)
                    462:                (void)check_failedlogin(pwd->pw_uid);
1.1       deraadt   463:        dolastlog(quietlog);
1.6       deraadt   464:
                    465:        login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
1.1       deraadt   466:
                    467:        (void)chown(ttyn, pwd->pw_uid,
                    468:            (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
                    469: #if defined(KERBEROS) || defined(KERBEROS5)
                    470:        /* Fork so that we can call kdestroy */
                    471:        if (krbtkfile_env)
                    472:            dofork();
                    473: #endif
                    474:
                    475:        /* Destroy environment unless user has requested its preservation. */
1.24      art       476:        if (!pflag) {
1.22      deraadt   477:                if ((environ = calloc(1, sizeof (char *))) == NULL)
                    478:                        err(1, "calloc");
1.24      art       479:        } else {
1.9       millert   480:                char **cpp, **cpp2;
                    481:
                    482:                for (cpp2 = cpp = environ; *cpp; cpp++) {
                    483:                        if (strncmp(*cpp, "LD_", 3) &&
1.29      millert   484:                            strncmp(*cpp, "ENV=", 4) &&
                    485:                            strncmp(*cpp, "BASH_ENV=", 9) &&
1.9       millert   486:                            strncmp(*cpp, "IFS=", 4))
                    487:                                *cpp2++ = *cpp;
                    488:                }
                    489:                *cpp2 = 0;
                    490:        }
1.30      millert   491:        /* Note: setusercontext(3) will set PATH */
1.1       deraadt   492:        (void)setenv("HOME", pwd->pw_dir, 1);
1.30      millert   493:        (void)setenv("SHELL", shell, 1);
1.1       deraadt   494:        if (term[0] == '\0')
1.29      millert   495:                (void)strlcpy(term, stypeof(tty), sizeof(term));
1.1       deraadt   496:        (void)setenv("TERM", term, 0);
                    497:        (void)setenv("LOGNAME", pwd->pw_name, 1);
                    498:        (void)setenv("USER", pwd->pw_name, 1);
1.14      millert   499:        if (hostname)
                    500:                (void)setenv("REMOTEHOST", hostname, 1);
                    501:        if (rusername)
                    502:                (void)setenv("REMOTEUSER", rusername, 1);
1.1       deraadt   503: #ifdef KERBEROS
                    504:        if (krbtkfile_env)
                    505:                (void)setenv("KRBTKFILE", krbtkfile_env, 1);
                    506: #endif
                    507: #ifdef KERBEROS5
                    508:        if (krbtkfile_env)
                    509:                (void)setenv("KRB5CCNAME", krbtkfile_env, 1);
                    510: #endif
                    511:        /* If fflag is on, assume caller/authenticator has logged root login. */
1.24      art       512:        if (rootlogin && fflag == 0) {
1.1       deraadt   513:                if (hostname)
1.14      millert   514:                        syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s%s%s",
                    515:                            username, tty, rusername ? rusername : "",
                    516:                            rusername ? "@" : "", hostname);
1.1       deraadt   517:                else
                    518:                        syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
1.24      art       519:        }
1.1       deraadt   520:
                    521: #if defined(KERBEROS) || defined(KERBEROS5)
                    522:        if (!quietlog && notickets == 1)
                    523:                (void)printf("Warning: no Kerberos tickets issued.\n");
                    524: #endif
                    525:
                    526:        if (!quietlog) {
1.2       deraadt   527: #if 0
1.1       deraadt   528:                (void)printf("%s\n\t%s  %s\n\n",
                    529:            "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
                    530:                    "The Regents of the University of California. ",
                    531:                    "All rights reserved.");
1.2       deraadt   532: #endif
1.1       deraadt   533:                motd();
                    534:                (void)snprintf(tbuf,
                    535:                    sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
                    536:                if (stat(tbuf, &st) == 0 && st.st_size != 0)
                    537:                        (void)printf("You have %smail.\n",
                    538:                            (st.st_mtime > st.st_atime) ? "new " : "");
                    539:        }
                    540:
                    541:        (void)signal(SIGALRM, SIG_DFL);
                    542:        (void)signal(SIGQUIT, SIG_DFL);
                    543:        (void)signal(SIGINT, SIG_DFL);
                    544:        (void)signal(SIGTSTP, SIG_IGN);
                    545:
                    546:        tbuf[0] = '-';
1.30      millert   547:        (void)strlcpy(tbuf + 1, (p = strrchr(shell, '/')) ?
                    548:            p + 1 : shell, sizeof tbuf - 1);
1.1       deraadt   549:
                    550:        /* Discard permissions last so can't get killed and drop core. */
1.30      millert   551:        if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL)) {
                    552:                warn("unable to set user context");
                    553:                exit(1);
1.15      tholo     554:        }
1.30      millert   555:
1.24      art       556: #ifdef KERBEROS
                    557:        kgettokens(pwd->pw_dir);
                    558: #endif
1.1       deraadt   559:
1.30      millert   560:        execlp(shell, tbuf, 0);
                    561:        err(1, "%s", shell);
1.1       deraadt   562: }
                    563:
                    564: int
                    565: pwcheck(user, p, salt, passwd)
                    566:        char *user, *p, *salt, *passwd;
                    567: {
                    568: #ifdef SKEY
1.8       millert   569:        if (strcasecmp(p, "s/key") == 0)
1.7       deraadt   570:                return skey_authenticate(user);
1.1       deraadt   571: #endif
                    572:        return strcmp(crypt(p, salt), passwd);
                    573: }
                    574:
                    575: #if defined(KERBEROS) || defined(KERBEROS5)
                    576: #define        NBUFSIZ         (UT_NAMESIZE + 1 + 5)   /* .root suffix */
                    577: #else
                    578: #define        NBUFSIZ         (UT_NAMESIZE + 1)
                    579: #endif
                    580:
                    581: #if defined(KERBEROS) || defined(KERBEROS5)
                    582: /*
                    583:  * This routine handles cleanup stuff, and the like.
                    584:  * It exists only in the child process.
                    585:  */
                    586: #include <sys/wait.h>
                    587: void
                    588: dofork()
                    589: {
                    590:     int child;
                    591:
                    592:     if (!(child = fork()))
                    593:            return; /* Child process */
                    594:
                    595:     /* Setup stuff?  This would be things we could do in parallel with login */
                    596:     (void) chdir("/"); /* Let's not keep the fs busy... */
                    597:
                    598:     /* If we're the parent, watch the child until it dies */
                    599:     while (wait(0) != child)
                    600:            ;
                    601:
                    602:     /* Cleanup stuff */
                    603:     /* Run kdestroy to destroy tickets */
                    604:     kdestroy();
                    605:
                    606:     /* Leave */
                    607:     exit(0);
                    608: }
                    609: #endif
                    610:
                    611: void
                    612: getloginname()
                    613: {
                    614:        int ch;
                    615:        char *p;
                    616:        static char nbuf[NBUFSIZ];
                    617:
                    618:        for (;;) {
                    619:                (void)printf("login: ");
                    620:                for (p = nbuf; (ch = getchar()) != '\n'; ) {
                    621:                        if (ch == EOF) {
                    622:                                badlogin(username);
                    623:                                exit(0);
                    624:                        }
                    625:                        if (p < nbuf + (NBUFSIZ - 1))
                    626:                                *p++ = ch;
                    627:                }
1.24      art       628:                if (p > nbuf) {
1.1       deraadt   629:                        if (nbuf[0] == '-')
                    630:                                (void)fprintf(stderr,
                    631:                                    "login names may not start with '-'.\n");
                    632:                        else {
                    633:                                *p = '\0';
                    634:                                username = nbuf;
                    635:                                break;
                    636:                        }
1.24      art       637:                }
1.1       deraadt   638:        }
                    639: }
                    640:
                    641: int
                    642: rootterm(ttyn)
                    643:        char *ttyn;
                    644: {
                    645:        struct ttyent *t;
                    646:
                    647:        return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
                    648: }
                    649:
                    650: jmp_buf motdinterrupt;
                    651:
                    652: void
                    653: motd()
                    654: {
                    655:        int fd, nchars;
                    656:        sig_t oldint;
                    657:        char tbuf[8192];
1.30      millert   658:        char *motd;
                    659:
                    660:        motd = login_getcapstr(lc, "welcome", _PATH_MOTDFILE, _PATH_MOTDFILE);
1.1       deraadt   661:
1.30      millert   662:        if ((fd = open(motd, O_RDONLY, 0)) < 0)
1.1       deraadt   663:                return;
                    664:        oldint = signal(SIGINT, sigint);
                    665:        if (setjmp(motdinterrupt) == 0)
                    666:                while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
                    667:                        (void)write(fileno(stdout), tbuf, nchars);
                    668:        (void)signal(SIGINT, oldint);
                    669:        (void)close(fd);
                    670: }
                    671:
                    672: /* ARGSUSED */
                    673: void
                    674: sigint(signo)
                    675:        int signo;
                    676: {
                    677:        longjmp(motdinterrupt, 1);
                    678: }
                    679:
                    680: /* ARGSUSED */
                    681: void
                    682: timedout(signo)
                    683:        int signo;
                    684: {
                    685:        (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
                    686:        exit(0);
                    687: }
                    688:
                    689: void
                    690: checknologin()
                    691: {
                    692:        int fd, nchars;
1.30      millert   693:        char *nologin;
1.1       deraadt   694:        char tbuf[8192];
                    695:
1.30      millert   696:        if (!login_getcapbool(lc, "ignorenologin", 0)) {
                    697:                nologin = login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
                    698:                    _PATH_NOLOGIN);
                    699:                if ((fd = open(nologin, O_RDONLY, 0)) >= 0) {
                    700:                        while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
                    701:                                (void)write(fileno(stdout), tbuf, nchars);
                    702:                        sleepexit(0);
                    703:                }
1.1       deraadt   704:        }
                    705: }
                    706:
                    707: void
                    708: dolastlog(quiet)
                    709:        int quiet;
                    710: {
                    711:        struct lastlog ll;
                    712:        int fd;
                    713:
                    714:        if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1.26      millert   715:                (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), SEEK_SET);
1.1       deraadt   716:                if (!quiet) {
                    717:                        if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
                    718:                            ll.ll_time != 0) {
                    719:                                (void)printf("Last login: %.*s ",
                    720:                                    24-5, (char *)ctime(&ll.ll_time));
1.14      millert   721:                                (void)printf("on %.*s",
                    722:                                    (int)sizeof(ll.ll_line),
                    723:                                    ll.ll_line);
1.1       deraadt   724:                                if (*ll.ll_host != '\0')
1.14      millert   725:                                        (void)printf(" from %.*s",
1.1       deraadt   726:                                            (int)sizeof(ll.ll_host),
                    727:                                            ll.ll_host);
1.14      millert   728:                                (void)putchar('\n');
1.1       deraadt   729:                        }
1.26      millert   730:                        (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll),
                    731:                            SEEK_SET);
1.1       deraadt   732:                }
                    733:                memset((void *)&ll, 0, sizeof(ll));
                    734:                (void)time(&ll.ll_time);
                    735:                (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
                    736:                if (hostname)
                    737:                        (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
                    738:                (void)write(fd, (char *)&ll, sizeof(ll));
                    739:                (void)close(fd);
                    740:        }
                    741: }
                    742:
                    743: void
                    744: badlogin(name)
                    745:        char *name;
                    746: {
                    747:        if (failures == 0)
                    748:                return;
                    749:        if (hostname) {
1.14      millert   750:                syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s%s%s",
                    751:                    failures, failures > 1 ? "S" : "",
                    752:                    rusername ? rusername : "", rusername ? "@" : "", hostname);
1.1       deraadt   753:                syslog(LOG_AUTHPRIV|LOG_NOTICE,
1.14      millert   754:                    "%d LOGIN FAILURE%s FROM %s%s%s, %s",
                    755:                    failures, failures > 1 ? "S" : "",
                    756:                    rusername ? rusername : "", rusername ? "@" : "",
                    757:                    hostname, name);
1.1       deraadt   758:        } else {
                    759:                syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
                    760:                    failures, failures > 1 ? "S" : "", tty);
                    761:                syslog(LOG_AUTHPRIV|LOG_NOTICE,
                    762:                    "%d LOGIN FAILURE%s ON %s, %s",
                    763:                    failures, failures > 1 ? "S" : "", tty, name);
                    764:        }
                    765: }
                    766:
                    767: #undef UNKNOWN
                    768: #define        UNKNOWN "su"
                    769:
                    770: char *
                    771: stypeof(ttyid)
                    772:        char *ttyid;
                    773: {
                    774:        struct ttyent *t;
                    775:
1.30      millert   776:        return (ttyid && (t = getttynam(ttyid)) ? t->ty_type :
                    777:            login_getcapstr(lc, "term", UNKNOWN, UNKNOWN));
1.1       deraadt   778: }
                    779:
                    780: void
                    781: sleepexit(eval)
                    782:        int eval;
                    783: {
                    784:        (void)sleep(5);
                    785:        exit(eval);
1.11      millert   786: }
                    787:
                    788: void
                    789: sighup(signum)
                    790:        int signum;
                    791: {
                    792:        if (username)
                    793:                badlogin(username);
1.30      millert   794:        exit(0);
1.1       deraadt   795: }