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

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