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

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