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

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