[BACK]Return to auth.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/auth.c, Revision 1.111

1.111   ! djm         1: /* $OpenBSD: auth.c,v 1.110 2015/02/25 17:29:38 djm Exp $ */
1.1       markus      2: /*
1.19      deraadt     3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.9       deraadt     4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       markus     24:  */
                     25:
1.62      stevesk    26: #include <sys/types.h>
                     27: #include <sys/stat.h>
1.22      markus     28:
1.70      stevesk    29: #include <errno.h>
1.79      dtucker    30: #include <fcntl.h>
1.22      markus     31: #include <libgen.h>
1.77      djm        32: #include <login_cap.h>
1.61      stevesk    33: #include <paths.h>
1.68      stevesk    34: #include <pwd.h>
1.69      stevesk    35: #include <stdarg.h>
1.74      stevesk    36: #include <stdio.h>
1.72      stevesk    37: #include <string.h>
1.80      djm        38: #include <unistd.h>
1.109     deraadt    39: #include <limits.h>
1.1       markus     40:
                     41: #include "xmalloc.h"
1.13      markus     42: #include "match.h"
1.14      markus     43: #include "groupaccess.h"
                     44: #include "log.h"
1.75      deraadt    45: #include "buffer.h"
1.106     millert    46: #include "misc.h"
1.1       markus     47: #include "servconf.h"
1.75      deraadt    48: #include "key.h"
                     49: #include "hostfile.h"
1.2       markus     50: #include "auth.h"
1.13      markus     51: #include "auth-options.h"
1.14      markus     52: #include "canohost.h"
1.24      markus     53: #include "uidswap.h"
1.42      markus     54: #include "packet.h"
1.75      deraadt    55: #ifdef GSSAPI
                     56: #include "ssh-gss.h"
                     57: #endif
1.85      djm        58: #include "authfile.h"
1.67      dtucker    59: #include "monitor_wrap.h"
1.107     djm        60: #include "authfile.h"
                     61: #include "ssherr.h"
1.103     djm        62: #include "compat.h"
1.2       markus     63:
1.1       markus     64: /* import */
                     65: extern ServerOptions options;
1.67      dtucker    66: extern int use_privsep;
1.1       markus     67:
1.42      markus     68: /* Debugging messages */
                     69: Buffer auth_debug;
                     70: int auth_debug_init;
                     71:
1.1       markus     72: /*
1.12      markus     73:  * Check if the user is allowed to log in via ssh. If user is listed
                     74:  * in DenyUsers or one of user's groups is listed in DenyGroups, false
                     75:  * will be returned. If AllowUsers isn't empty and user isn't listed
                     76:  * there, or if AllowGroups isn't empty and one of user's groups isn't
                     77:  * listed there, false will be returned.
1.1       markus     78:  * If the user's shell is not executable, false will be returned.
1.4       markus     79:  * Otherwise true is returned.
1.1       markus     80:  */
1.5       markus     81: int
1.1       markus     82: allowed_user(struct passwd * pw)
                     83: {
                     84:        struct stat st;
1.35      markus     85:        const char *hostname = NULL, *ipaddr = NULL;
1.60      djm        86:        u_int i;
1.1       markus     87:
                     88:        /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12      markus     89:        if (!pw || !pw->pw_name)
1.1       markus     90:                return 0;
                     91:
1.7       deraadt    92:        /*
1.84      djm        93:         * Deny if shell does not exist or is not executable unless we
                     94:         * are chrooting.
1.7       deraadt    95:         */
1.84      djm        96:        if (options.chroot_directory == NULL ||
                     97:            strcasecmp(options.chroot_directory, "none") == 0) {
                     98:                char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
                     99:                    _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
                    100:
                    101:                if (stat(shell, &st) != 0) {
                    102:                        logit("User %.100s not allowed because shell %.100s "
                    103:                            "does not exist", pw->pw_name, shell);
1.102     djm       104:                        free(shell);
1.84      djm       105:                        return 0;
                    106:                }
                    107:                if (S_ISREG(st.st_mode) == 0 ||
                    108:                    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
                    109:                        logit("User %.100s not allowed because shell %.100s "
                    110:                            "is not executable", pw->pw_name, shell);
1.102     djm       111:                        free(shell);
1.84      djm       112:                        return 0;
                    113:                }
1.102     djm       114:                free(shell);
1.34      stevesk   115:        }
1.1       markus    116:
1.58      dtucker   117:        if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
                    118:            options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.48      markus    119:                hostname = get_canonical_hostname(options.use_dns);
1.35      markus    120:                ipaddr = get_remote_ipaddr();
                    121:        }
                    122:
1.1       markus    123:        /* Return false if user is listed in DenyUsers */
                    124:        if (options.num_deny_users > 0) {
                    125:                for (i = 0; i < options.num_deny_users; i++)
1.39      markus    126:                        if (match_user(pw->pw_name, hostname, ipaddr,
1.34      stevesk   127:                            options.deny_users[i])) {
1.57      dtucker   128:                                logit("User %.100s from %.100s not allowed "
                    129:                                    "because listed in DenyUsers",
                    130:                                    pw->pw_name, hostname);
1.1       markus    131:                                return 0;
1.34      stevesk   132:                        }
1.1       markus    133:        }
                    134:        /* Return false if AllowUsers isn't empty and user isn't listed there */
                    135:        if (options.num_allow_users > 0) {
                    136:                for (i = 0; i < options.num_allow_users; i++)
1.39      markus    137:                        if (match_user(pw->pw_name, hostname, ipaddr,
1.26      markus    138:                            options.allow_users[i]))
1.1       markus    139:                                break;
                    140:                /* i < options.num_allow_users iff we break for loop */
1.34      stevesk   141:                if (i >= options.num_allow_users) {
1.57      dtucker   142:                        logit("User %.100s from %.100s not allowed because "
                    143:                            "not listed in AllowUsers", pw->pw_name, hostname);
1.1       markus    144:                        return 0;
1.34      stevesk   145:                }
1.1       markus    146:        }
                    147:        if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12      markus    148:                /* Get the user's group access list (primary and supplementary) */
1.34      stevesk   149:                if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57      dtucker   150:                        logit("User %.100s from %.100s not allowed because "
                    151:                            "not in any group", pw->pw_name, hostname);
1.1       markus    152:                        return 0;
1.34      stevesk   153:                }
1.1       markus    154:
1.12      markus    155:                /* Return false if one of user's groups is listed in DenyGroups */
                    156:                if (options.num_deny_groups > 0)
                    157:                        if (ga_match(options.deny_groups,
                    158:                            options.num_deny_groups)) {
                    159:                                ga_free();
1.57      dtucker   160:                                logit("User %.100s from %.100s not allowed "
                    161:                                    "because a group is listed in DenyGroups",
                    162:                                    pw->pw_name, hostname);
1.1       markus    163:                                return 0;
1.12      markus    164:                        }
1.1       markus    165:                /*
1.12      markus    166:                 * Return false if AllowGroups isn't empty and one of user's groups
1.1       markus    167:                 * isn't listed there
                    168:                 */
1.12      markus    169:                if (options.num_allow_groups > 0)
                    170:                        if (!ga_match(options.allow_groups,
                    171:                            options.num_allow_groups)) {
                    172:                                ga_free();
1.57      dtucker   173:                                logit("User %.100s from %.100s not allowed "
                    174:                                    "because none of user's groups are listed "
                    175:                                    "in AllowGroups", pw->pw_name, hostname);
1.1       markus    176:                                return 0;
1.12      markus    177:                        }
                    178:                ga_free();
1.1       markus    179:        }
                    180:        /* We found no reason not to let this user try to log on... */
                    181:        return 1;
1.13      markus    182: }
                    183:
                    184: void
1.103     djm       185: auth_info(Authctxt *authctxt, const char *fmt, ...)
                    186: {
                    187:        va_list ap;
                    188:         int i;
                    189:
                    190:        free(authctxt->info);
                    191:        authctxt->info = NULL;
                    192:
                    193:        va_start(ap, fmt);
                    194:        i = vasprintf(&authctxt->info, fmt, ap);
                    195:        va_end(ap);
                    196:
                    197:        if (i < 0 || authctxt->info == NULL)
                    198:                fatal("vasprintf failed");
                    199: }
                    200:
                    201: void
1.98      djm       202: auth_log(Authctxt *authctxt, int authenticated, int partial,
1.103     djm       203:     const char *method, const char *submethod)
1.13      markus    204: {
                    205:        void (*authlog) (const char *fmt,...) = verbose;
                    206:        char *authmsg;
1.67      dtucker   207:
                    208:        if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
                    209:                return;
1.13      markus    210:
                    211:        /* Raise logging level */
                    212:        if (authenticated == 1 ||
                    213:            !authctxt->valid ||
1.54      dtucker   214:            authctxt->failures >= options.max_authtries / 2 ||
1.13      markus    215:            strcmp(method, "password") == 0)
1.47      itojun    216:                authlog = logit;
1.13      markus    217:
                    218:        if (authctxt->postponed)
                    219:                authmsg = "Postponed";
1.98      djm       220:        else if (partial)
                    221:                authmsg = "Partial";
1.13      markus    222:        else
                    223:                authmsg = authenticated ? "Accepted" : "Failed";
                    224:
1.103     djm       225:        authlog("%s %s%s%s for %s%.100s from %.200s port %d %s%s%s",
1.13      markus    226:            authmsg,
                    227:            method,
1.98      djm       228:            submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
1.56      markus    229:            authctxt->valid ? "" : "invalid user ",
1.29      markus    230:            authctxt->user,
1.13      markus    231:            get_remote_ipaddr(),
                    232:            get_remote_port(),
1.103     djm       233:            compat20 ? "ssh2" : "ssh1",
                    234:            authctxt->info != NULL ? ": " : "",
                    235:            authctxt->info != NULL ? authctxt->info : "");
                    236:        free(authctxt->info);
                    237:        authctxt->info = NULL;
1.105     djm       238: }
                    239:
                    240: void
                    241: auth_maxtries_exceeded(Authctxt *authctxt)
                    242: {
1.110     djm       243:        error("maximum authentication attempts exceeded for "
1.105     djm       244:            "%s%.100s from %.200s port %d %s",
                    245:            authctxt->valid ? "" : "invalid user ",
                    246:            authctxt->user,
                    247:            get_remote_ipaddr(),
                    248:            get_remote_port(),
                    249:            compat20 ? "ssh2" : "ssh1");
1.110     djm       250:        packet_disconnect("Too many authentication failures");
1.105     djm       251:        /* NOTREACHED */
1.13      markus    252: }
                    253:
                    254: /*
1.17      markus    255:  * Check whether root logins are disallowed.
1.13      markus    256:  */
                    257: int
1.98      djm       258: auth_root_allowed(const char *method)
1.13      markus    259: {
1.17      markus    260:        switch (options.permit_root_login) {
                    261:        case PERMIT_YES:
1.13      markus    262:                return 1;
1.17      markus    263:        case PERMIT_NO_PASSWD:
                    264:                if (strcmp(method, "password") != 0)
                    265:                        return 1;
                    266:                break;
                    267:        case PERMIT_FORCED_ONLY:
                    268:                if (forced_command) {
1.47      itojun    269:                        logit("Root login accepted for forced command.");
1.17      markus    270:                        return 1;
                    271:                }
                    272:                break;
1.13      markus    273:        }
1.47      itojun    274:        logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
1.22      markus    275:        return 0;
                    276: }
                    277:
                    278:
                    279: /*
                    280:  * Given a template and a passwd structure, build a filename
                    281:  * by substituting % tokenised options. Currently, %% becomes '%',
                    282:  * %h becomes the home directory and %u the username.
                    283:  *
                    284:  * This returns a buffer allocated by xmalloc.
                    285:  */
1.93      djm       286: char *
1.59      djm       287: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22      markus    288: {
1.109     deraadt   289:        char *file, ret[PATH_MAX];
1.65      djm       290:        int i;
1.22      markus    291:
1.59      djm       292:        file = percent_expand(filename, "h", pw->pw_dir,
                    293:            "u", pw->pw_name, (char *)NULL);
1.22      markus    294:
                    295:        /*
                    296:         * Ensure that filename starts anchored. If not, be backward
                    297:         * compatible and prepend the '%h/'
                    298:         */
1.59      djm       299:        if (*file == '/')
                    300:                return (file);
                    301:
1.65      djm       302:        i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
                    303:        if (i < 0 || (size_t)i >= sizeof(ret))
1.59      djm       304:                fatal("expand_authorized_keys: path too long");
1.102     djm       305:        free(file);
1.65      djm       306:        return (xstrdup(ret));
1.22      markus    307: }
1.24      markus    308:
1.87      djm       309: char *
                    310: authorized_principals_file(struct passwd *pw)
                    311: {
1.111   ! djm       312:        if (options.authorized_principals_file == NULL)
1.87      djm       313:                return NULL;
                    314:        return expand_authorized_keys(options.authorized_principals_file, pw);
                    315: }
                    316:
1.24      markus    317: /* return ok if key exists in sysfile or userfile */
                    318: HostStatus
                    319: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
                    320:     const char *sysfile, const char *userfile)
                    321: {
                    322:        char *user_hostfile;
                    323:        struct stat st;
1.30      stevesk   324:        HostStatus host_status;
1.91      djm       325:        struct hostkeys *hostkeys;
                    326:        const struct hostkey_entry *found;
1.24      markus    327:
1.91      djm       328:        hostkeys = init_hostkeys();
                    329:        load_hostkeys(hostkeys, host, sysfile);
                    330:        if (userfile != NULL) {
1.24      markus    331:                user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
                    332:                if (options.strict_modes &&
                    333:                    (stat(user_hostfile, &st) == 0) &&
                    334:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31      deraadt   335:                    (st.st_mode & 022) != 0)) {
1.47      itojun    336:                        logit("Authentication refused for %.100s: "
1.24      markus    337:                            "bad owner or modes for %.200s",
                    338:                            pw->pw_name, user_hostfile);
1.88      djm       339:                        auth_debug_add("Ignored %.200s: bad ownership or modes",
                    340:                            user_hostfile);
1.24      markus    341:                } else {
                    342:                        temporarily_use_uid(pw);
1.91      djm       343:                        load_hostkeys(hostkeys, host, user_hostfile);
1.24      markus    344:                        restore_uid();
                    345:                }
1.102     djm       346:                free(user_hostfile);
1.24      markus    347:        }
1.91      djm       348:        host_status = check_key_in_hostkeys(hostkeys, key, &found);
                    349:        if (host_status == HOST_REVOKED)
                    350:                error("WARNING: revoked key for %s attempted authentication",
                    351:                    found->host);
                    352:        else if (host_status == HOST_OK)
                    353:                debug("%s: key for %s found at %s:%ld", __func__,
                    354:                    found->host, found->file, found->line);
                    355:        else
                    356:                debug("%s: key for host %s not found", __func__, host);
                    357:
                    358:        free_hostkeys(hostkeys);
1.24      markus    359:
                    360:        return host_status;
                    361: }
                    362:
1.22      markus    363: /*
1.97      djm       364:  * Check a given path for security. This is defined as all components
1.44      stevesk   365:  * of the path to the file must be owned by either the owner of
1.23      markus    366:  * of the file or root and no directories must be group or world writable.
1.22      markus    367:  *
                    368:  * XXX Should any specific check be done for sym links ?
                    369:  *
1.101     dtucker   370:  * Takes a file name, its stat information (preferably from fstat() to
1.97      djm       371:  * avoid races), the uid of the expected owner, their home directory and an
1.22      markus    372:  * error buffer plus max size as arguments.
                    373:  *
                    374:  * Returns 0 on success and -1 on failure
                    375:  */
1.97      djm       376: int
                    377: auth_secure_path(const char *name, struct stat *stp, const char *pw_dir,
                    378:     uid_t uid, char *err, size_t errlen)
1.22      markus    379: {
1.109     deraadt   380:        char buf[PATH_MAX], homedir[PATH_MAX];
1.22      markus    381:        char *cp;
1.46      markus    382:        int comparehome = 0;
1.22      markus    383:        struct stat st;
                    384:
1.97      djm       385:        if (realpath(name, buf) == NULL) {
                    386:                snprintf(err, errlen, "realpath %s failed: %s", name,
1.22      markus    387:                    strerror(errno));
                    388:                return -1;
                    389:        }
1.97      djm       390:        if (pw_dir != NULL && realpath(pw_dir, homedir) != NULL)
1.46      markus    391:                comparehome = 1;
1.22      markus    392:
1.97      djm       393:        if (!S_ISREG(stp->st_mode)) {
                    394:                snprintf(err, errlen, "%s is not a regular file", buf);
                    395:                return -1;
                    396:        }
                    397:        if ((stp->st_uid != 0 && stp->st_uid != uid) ||
                    398:            (stp->st_mode & 022) != 0) {
1.22      markus    399:                snprintf(err, errlen, "bad ownership or modes for file %s",
                    400:                    buf);
                    401:                return -1;
                    402:        }
                    403:
                    404:        /* for each component of the canonical path, walking upwards */
                    405:        for (;;) {
                    406:                if ((cp = dirname(buf)) == NULL) {
                    407:                        snprintf(err, errlen, "dirname() failed");
                    408:                        return -1;
                    409:                }
                    410:                strlcpy(buf, cp, sizeof(buf));
1.25      provos    411:
1.22      markus    412:                if (stat(buf, &st) < 0 ||
                    413:                    (st.st_uid != 0 && st.st_uid != uid) ||
                    414:                    (st.st_mode & 022) != 0) {
1.31      deraadt   415:                        snprintf(err, errlen,
1.22      markus    416:                            "bad ownership or modes for directory %s", buf);
                    417:                        return -1;
                    418:                }
                    419:
1.82      dtucker   420:                /* If are past the homedir then we can stop */
1.94      djm       421:                if (comparehome && strcmp(homedir, buf) == 0)
1.27      markus    422:                        break;
1.94      djm       423:
1.22      markus    424:                /*
                    425:                 * dirname should always complete with a "/" path,
                    426:                 * but we can be paranoid and check for "." too
                    427:                 */
                    428:                if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
                    429:                        break;
                    430:        }
1.17      markus    431:        return 0;
1.97      djm       432: }
                    433:
                    434: /*
                    435:  * Version of secure_path() that accepts an open file descriptor to
                    436:  * avoid races.
                    437:  *
                    438:  * Returns 0 on success and -1 on failure
                    439:  */
                    440: static int
                    441: secure_filename(FILE *f, const char *file, struct passwd *pw,
                    442:     char *err, size_t errlen)
                    443: {
                    444:        struct stat st;
                    445:
                    446:        /* check the open file to avoid races */
                    447:        if (fstat(fileno(f), &st) < 0) {
                    448:                snprintf(err, errlen, "cannot stat file %s: %s",
1.99      dtucker   449:                    file, strerror(errno));
1.97      djm       450:                return -1;
                    451:        }
                    452:        return auth_secure_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen);
1.79      dtucker   453: }
                    454:
1.87      djm       455: static FILE *
                    456: auth_openfile(const char *file, struct passwd *pw, int strict_modes,
                    457:     int log_missing, char *file_type)
1.79      dtucker   458: {
                    459:        char line[1024];
                    460:        struct stat st;
                    461:        int fd;
                    462:        FILE *f;
                    463:
1.81      dtucker   464:        if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
1.87      djm       465:                if (log_missing || errno != ENOENT)
                    466:                        debug("Could not open %s '%s': %s", file_type, file,
1.81      dtucker   467:                           strerror(errno));
1.79      dtucker   468:                return NULL;
1.81      dtucker   469:        }
1.79      dtucker   470:
                    471:        if (fstat(fd, &st) < 0) {
                    472:                close(fd);
                    473:                return NULL;
                    474:        }
                    475:        if (!S_ISREG(st.st_mode)) {
1.87      djm       476:                logit("User %s %s %s is not a regular file",
                    477:                    pw->pw_name, file_type, file);
1.79      dtucker   478:                close(fd);
                    479:                return NULL;
                    480:        }
                    481:        unset_nonblock(fd);
                    482:        if ((f = fdopen(fd, "r")) == NULL) {
                    483:                close(fd);
                    484:                return NULL;
                    485:        }
1.90      djm       486:        if (strict_modes &&
1.79      dtucker   487:            secure_filename(f, file, pw, line, sizeof(line)) != 0) {
                    488:                fclose(f);
                    489:                logit("Authentication refused: %s", line);
1.88      djm       490:                auth_debug_add("Ignored %s: %s", file_type, line);
1.79      dtucker   491:                return NULL;
                    492:        }
                    493:
                    494:        return f;
1.87      djm       495: }
                    496:
                    497:
                    498: FILE *
                    499: auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
                    500: {
                    501:        return auth_openfile(file, pw, strict_modes, 1, "authorized keys");
                    502: }
                    503:
                    504: FILE *
                    505: auth_openprincipals(const char *file, struct passwd *pw, int strict_modes)
                    506: {
                    507:        return auth_openfile(file, pw, strict_modes, 0,
                    508:            "authorized principals");
1.37      provos    509: }
                    510:
                    511: struct passwd *
                    512: getpwnamallow(const char *user)
                    513: {
1.38      provos    514:        extern login_cap_t *lc;
                    515:        auth_session_t *as;
1.37      provos    516:        struct passwd *pw;
1.96      dtucker   517:        struct connection_info *ci = get_connection_info(1, options.use_dns);
1.71      dtucker   518:
1.96      dtucker   519:        ci->user = user;
                    520:        parse_server_match_config(&options, ci);
1.37      provos    521:
                    522:        pw = getpwnam(user);
1.45      stevesk   523:        if (pw == NULL) {
1.55      markus    524:                logit("Invalid user %.100s from %.100s",
1.45      stevesk   525:                    user, get_remote_ipaddr());
                    526:                return (NULL);
                    527:        }
                    528:        if (!allowed_user(pw))
1.38      provos    529:                return (NULL);
                    530:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    531:                debug("unable to get login class: %s", user);
                    532:                return (NULL);
                    533:        }
                    534:        if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43      millert   535:            auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38      provos    536:                debug("Approval failure for %s", user);
1.37      provos    537:                pw = NULL;
1.38      provos    538:        }
                    539:        if (as != NULL)
                    540:                auth_close(as);
1.41      markus    541:        if (pw != NULL)
                    542:                return (pwcopy(pw));
                    543:        return (NULL);
1.85      djm       544: }
                    545:
                    546: /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
                    547: int
                    548: auth_key_is_revoked(Key *key)
                    549: {
1.107     djm       550:        char *fp = NULL;
                    551:        int r;
1.85      djm       552:
                    553:        if (options.revoked_keys_file == NULL)
                    554:                return 0;
1.108     djm       555:        if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    556:            SSH_FP_DEFAULT)) == NULL) {
1.107     djm       557:                r = SSH_ERR_ALLOC_FAIL;
                    558:                error("%s: fingerprint key: %s", __func__, ssh_err(r));
                    559:                goto out;
                    560:        }
                    561:
                    562:        r = sshkey_check_revoked(key, options.revoked_keys_file);
                    563:        switch (r) {
1.100     djm       564:        case 0:
1.107     djm       565:                break; /* not revoked */
                    566:        case SSH_ERR_KEY_REVOKED:
                    567:                error("Authentication key %s %s revoked by file %s",
                    568:                    sshkey_type(key), fp, options.revoked_keys_file);
                    569:                goto out;
1.100     djm       570:        default:
1.107     djm       571:                error("Error checking authentication key %s %s in "
                    572:                    "revoked keys file %s: %s", sshkey_type(key), fp,
                    573:                    options.revoked_keys_file, ssh_err(r));
                    574:                goto out;
1.100     djm       575:        }
1.107     djm       576:
                    577:        /* Success */
                    578:        r = 0;
                    579:
                    580:  out:
                    581:        free(fp);
                    582:        return r == 0 ? 0 : 1;
1.42      markus    583: }
                    584:
                    585: void
                    586: auth_debug_add(const char *fmt,...)
                    587: {
                    588:        char buf[1024];
                    589:        va_list args;
                    590:
                    591:        if (!auth_debug_init)
                    592:                return;
                    593:
                    594:        va_start(args, fmt);
                    595:        vsnprintf(buf, sizeof(buf), fmt, args);
                    596:        va_end(args);
                    597:        buffer_put_cstring(&auth_debug, buf);
                    598: }
                    599:
                    600: void
                    601: auth_debug_send(void)
                    602: {
                    603:        char *msg;
                    604:
                    605:        if (!auth_debug_init)
                    606:                return;
                    607:        while (buffer_len(&auth_debug)) {
                    608:                msg = buffer_get_string(&auth_debug, NULL);
                    609:                packet_send_debug("%s", msg);
1.102     djm       610:                free(msg);
1.42      markus    611:        }
                    612: }
                    613:
                    614: void
                    615: auth_debug_reset(void)
                    616: {
                    617:        if (auth_debug_init)
                    618:                buffer_clear(&auth_debug);
                    619:        else {
                    620:                buffer_init(&auth_debug);
                    621:                auth_debug_init = 1;
                    622:        }
1.49      markus    623: }
                    624:
                    625: struct passwd *
                    626: fakepw(void)
                    627: {
                    628:        static struct passwd fake;
                    629:
                    630:        memset(&fake, 0, sizeof(fake));
                    631:        fake.pw_name = "NOUSER";
                    632:        fake.pw_passwd =
1.51      djm       633:            "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49      markus    634:        fake.pw_gecos = "NOUSER";
1.53      deraadt   635:        fake.pw_uid = (uid_t)-1;
                    636:        fake.pw_gid = (gid_t)-1;
1.49      markus    637:        fake.pw_class = "";
                    638:        fake.pw_dir = "/nonexist";
                    639:        fake.pw_shell = "/nonexist";
                    640:
                    641:        return (&fake);
1.1       markus    642: }