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

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