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

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