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

1.115   ! dtucker     1: /* $OpenBSD: auth.c,v 1.114 2016/03/07 19:02:43 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.114     djm        28: #include <sys/socket.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.109     deraadt    40: #include <limits.h>
1.114     djm        41: #include <netdb.h>
1.1       markus     42:
                     43: #include "xmalloc.h"
1.13      markus     44: #include "match.h"
1.14      markus     45: #include "groupaccess.h"
                     46: #include "log.h"
1.75      deraadt    47: #include "buffer.h"
1.106     millert    48: #include "misc.h"
1.1       markus     49: #include "servconf.h"
1.75      deraadt    50: #include "key.h"
                     51: #include "hostfile.h"
1.2       markus     52: #include "auth.h"
1.13      markus     53: #include "auth-options.h"
1.14      markus     54: #include "canohost.h"
1.24      markus     55: #include "uidswap.h"
1.42      markus     56: #include "packet.h"
1.75      deraadt    57: #ifdef GSSAPI
                     58: #include "ssh-gss.h"
                     59: #endif
1.85      djm        60: #include "authfile.h"
1.67      dtucker    61: #include "monitor_wrap.h"
1.107     djm        62: #include "authfile.h"
                     63: #include "ssherr.h"
1.103     djm        64: #include "compat.h"
1.2       markus     65:
1.1       markus     66: /* import */
                     67: extern ServerOptions options;
1.67      dtucker    68: extern int use_privsep;
1.1       markus     69:
1.42      markus     70: /* Debugging messages */
                     71: Buffer auth_debug;
                     72: int auth_debug_init;
                     73:
1.1       markus     74: /*
1.12      markus     75:  * Check if the user is allowed to log in via ssh. If user is listed
                     76:  * in DenyUsers or one of user's groups is listed in DenyGroups, false
                     77:  * will be returned. If AllowUsers isn't empty and user isn't listed
                     78:  * there, or if AllowGroups isn't empty and one of user's groups isn't
                     79:  * listed there, false will be returned.
1.1       markus     80:  * If the user's shell is not executable, false will be returned.
1.4       markus     81:  * Otherwise true is returned.
1.1       markus     82:  */
1.5       markus     83: int
1.1       markus     84: allowed_user(struct passwd * pw)
                     85: {
1.114     djm        86:        struct ssh *ssh = active_state; /* XXX */
1.1       markus     87:        struct stat st;
1.35      markus     88:        const char *hostname = NULL, *ipaddr = NULL;
1.60      djm        89:        u_int i;
1.1       markus     90:
                     91:        /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12      markus     92:        if (!pw || !pw->pw_name)
1.1       markus     93:                return 0;
                     94:
1.7       deraadt    95:        /*
1.84      djm        96:         * Deny if shell does not exist or is not executable unless we
                     97:         * are chrooting.
1.7       deraadt    98:         */
1.84      djm        99:        if (options.chroot_directory == NULL ||
                    100:            strcasecmp(options.chroot_directory, "none") == 0) {
                    101:                char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
                    102:                    _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
                    103:
                    104:                if (stat(shell, &st) != 0) {
                    105:                        logit("User %.100s not allowed because shell %.100s "
                    106:                            "does not exist", pw->pw_name, shell);
1.102     djm       107:                        free(shell);
1.84      djm       108:                        return 0;
                    109:                }
                    110:                if (S_ISREG(st.st_mode) == 0 ||
                    111:                    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
                    112:                        logit("User %.100s not allowed because shell %.100s "
                    113:                            "is not executable", pw->pw_name, shell);
1.102     djm       114:                        free(shell);
1.84      djm       115:                        return 0;
                    116:                }
1.102     djm       117:                free(shell);
1.34      stevesk   118:        }
1.1       markus    119:
1.58      dtucker   120:        if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
                    121:            options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.114     djm       122:                hostname = auth_get_canonical_hostname(ssh, options.use_dns);
                    123:                ipaddr = ssh_remote_ipaddr(ssh);
1.35      markus    124:        }
                    125:
1.1       markus    126:        /* Return false if user is listed in DenyUsers */
                    127:        if (options.num_deny_users > 0) {
                    128:                for (i = 0; i < options.num_deny_users; i++)
1.39      markus    129:                        if (match_user(pw->pw_name, hostname, ipaddr,
1.34      stevesk   130:                            options.deny_users[i])) {
1.57      dtucker   131:                                logit("User %.100s from %.100s not allowed "
                    132:                                    "because listed in DenyUsers",
                    133:                                    pw->pw_name, hostname);
1.1       markus    134:                                return 0;
1.34      stevesk   135:                        }
1.1       markus    136:        }
                    137:        /* Return false if AllowUsers isn't empty and user isn't listed there */
                    138:        if (options.num_allow_users > 0) {
                    139:                for (i = 0; i < options.num_allow_users; i++)
1.39      markus    140:                        if (match_user(pw->pw_name, hostname, ipaddr,
1.26      markus    141:                            options.allow_users[i]))
1.1       markus    142:                                break;
                    143:                /* i < options.num_allow_users iff we break for loop */
1.34      stevesk   144:                if (i >= options.num_allow_users) {
1.57      dtucker   145:                        logit("User %.100s from %.100s not allowed because "
                    146:                            "not listed in AllowUsers", pw->pw_name, hostname);
1.1       markus    147:                        return 0;
1.34      stevesk   148:                }
1.1       markus    149:        }
                    150:        if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12      markus    151:                /* Get the user's group access list (primary and supplementary) */
1.34      stevesk   152:                if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57      dtucker   153:                        logit("User %.100s from %.100s not allowed because "
                    154:                            "not in any group", pw->pw_name, hostname);
1.1       markus    155:                        return 0;
1.34      stevesk   156:                }
1.1       markus    157:
1.12      markus    158:                /* Return false if one of user's groups is listed in DenyGroups */
                    159:                if (options.num_deny_groups > 0)
                    160:                        if (ga_match(options.deny_groups,
                    161:                            options.num_deny_groups)) {
                    162:                                ga_free();
1.57      dtucker   163:                                logit("User %.100s from %.100s not allowed "
                    164:                                    "because a group is listed in DenyGroups",
                    165:                                    pw->pw_name, hostname);
1.1       markus    166:                                return 0;
1.12      markus    167:                        }
1.1       markus    168:                /*
1.12      markus    169:                 * Return false if AllowGroups isn't empty and one of user's groups
1.1       markus    170:                 * isn't listed there
                    171:                 */
1.12      markus    172:                if (options.num_allow_groups > 0)
                    173:                        if (!ga_match(options.allow_groups,
                    174:                            options.num_allow_groups)) {
                    175:                                ga_free();
1.57      dtucker   176:                                logit("User %.100s from %.100s not allowed "
                    177:                                    "because none of user's groups are listed "
                    178:                                    "in AllowGroups", pw->pw_name, hostname);
1.1       markus    179:                                return 0;
1.12      markus    180:                        }
                    181:                ga_free();
1.1       markus    182:        }
                    183:        /* We found no reason not to let this user try to log on... */
                    184:        return 1;
1.13      markus    185: }
                    186:
                    187: void
1.103     djm       188: auth_info(Authctxt *authctxt, const char *fmt, ...)
                    189: {
                    190:        va_list ap;
                    191:         int i;
                    192:
                    193:        free(authctxt->info);
                    194:        authctxt->info = NULL;
                    195:
                    196:        va_start(ap, fmt);
                    197:        i = vasprintf(&authctxt->info, fmt, ap);
                    198:        va_end(ap);
                    199:
                    200:        if (i < 0 || authctxt->info == NULL)
                    201:                fatal("vasprintf failed");
                    202: }
                    203:
                    204: void
1.98      djm       205: auth_log(Authctxt *authctxt, int authenticated, int partial,
1.103     djm       206:     const char *method, const char *submethod)
1.13      markus    207: {
1.114     djm       208:        struct ssh *ssh = active_state; /* XXX */
1.13      markus    209:        void (*authlog) (const char *fmt,...) = verbose;
                    210:        char *authmsg;
1.67      dtucker   211:
                    212:        if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
                    213:                return;
1.13      markus    214:
                    215:        /* Raise logging level */
                    216:        if (authenticated == 1 ||
                    217:            !authctxt->valid ||
1.54      dtucker   218:            authctxt->failures >= options.max_authtries / 2 ||
1.13      markus    219:            strcmp(method, "password") == 0)
1.47      itojun    220:                authlog = logit;
1.13      markus    221:
                    222:        if (authctxt->postponed)
                    223:                authmsg = "Postponed";
1.98      djm       224:        else if (partial)
                    225:                authmsg = "Partial";
1.13      markus    226:        else
                    227:                authmsg = authenticated ? "Accepted" : "Failed";
                    228:
1.103     djm       229:        authlog("%s %s%s%s for %s%.100s from %.200s port %d %s%s%s",
1.13      markus    230:            authmsg,
                    231:            method,
1.98      djm       232:            submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
1.56      markus    233:            authctxt->valid ? "" : "invalid user ",
1.29      markus    234:            authctxt->user,
1.114     djm       235:            ssh_remote_ipaddr(ssh),
                    236:            ssh_remote_port(ssh),
1.103     djm       237:            compat20 ? "ssh2" : "ssh1",
                    238:            authctxt->info != NULL ? ": " : "",
                    239:            authctxt->info != NULL ? authctxt->info : "");
                    240:        free(authctxt->info);
                    241:        authctxt->info = NULL;
1.105     djm       242: }
                    243:
                    244: void
                    245: auth_maxtries_exceeded(Authctxt *authctxt)
                    246: {
1.114     djm       247:        struct ssh *ssh = active_state; /* XXX */
                    248:
1.110     djm       249:        error("maximum authentication attempts exceeded for "
1.105     djm       250:            "%s%.100s from %.200s port %d %s",
                    251:            authctxt->valid ? "" : "invalid user ",
                    252:            authctxt->user,
1.114     djm       253:            ssh_remote_ipaddr(ssh),
                    254:            ssh_remote_port(ssh),
1.105     djm       255:            compat20 ? "ssh2" : "ssh1");
1.110     djm       256:        packet_disconnect("Too many authentication failures");
1.105     djm       257:        /* NOTREACHED */
1.13      markus    258: }
                    259:
                    260: /*
1.17      markus    261:  * Check whether root logins are disallowed.
1.13      markus    262:  */
                    263: int
1.98      djm       264: auth_root_allowed(const char *method)
1.13      markus    265: {
1.114     djm       266:        struct ssh *ssh = active_state; /* XXX */
                    267:
1.17      markus    268:        switch (options.permit_root_login) {
                    269:        case PERMIT_YES:
1.13      markus    270:                return 1;
1.17      markus    271:        case PERMIT_NO_PASSWD:
1.112     deraadt   272:                if (strcmp(method, "publickey") == 0 ||
                    273:                    strcmp(method, "hostbased") == 0 ||
1.113     djm       274:                    strcmp(method, "gssapi-with-mic") == 0)
1.17      markus    275:                        return 1;
                    276:                break;
                    277:        case PERMIT_FORCED_ONLY:
                    278:                if (forced_command) {
1.47      itojun    279:                        logit("Root login accepted for forced command.");
1.17      markus    280:                        return 1;
                    281:                }
                    282:                break;
1.13      markus    283:        }
1.114     djm       284:        logit("ROOT LOGIN REFUSED FROM %.200s port %d",
                    285:            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.22      markus    286:        return 0;
                    287: }
                    288:
                    289:
                    290: /*
                    291:  * Given a template and a passwd structure, build a filename
                    292:  * by substituting % tokenised options. Currently, %% becomes '%',
                    293:  * %h becomes the home directory and %u the username.
                    294:  *
                    295:  * This returns a buffer allocated by xmalloc.
                    296:  */
1.93      djm       297: char *
1.59      djm       298: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22      markus    299: {
1.109     deraadt   300:        char *file, ret[PATH_MAX];
1.65      djm       301:        int i;
1.22      markus    302:
1.59      djm       303:        file = percent_expand(filename, "h", pw->pw_dir,
                    304:            "u", pw->pw_name, (char *)NULL);
1.22      markus    305:
                    306:        /*
                    307:         * Ensure that filename starts anchored. If not, be backward
                    308:         * compatible and prepend the '%h/'
                    309:         */
1.59      djm       310:        if (*file == '/')
                    311:                return (file);
                    312:
1.65      djm       313:        i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
                    314:        if (i < 0 || (size_t)i >= sizeof(ret))
1.59      djm       315:                fatal("expand_authorized_keys: path too long");
1.102     djm       316:        free(file);
1.65      djm       317:        return (xstrdup(ret));
1.22      markus    318: }
1.24      markus    319:
1.87      djm       320: char *
                    321: authorized_principals_file(struct passwd *pw)
                    322: {
1.111     djm       323:        if (options.authorized_principals_file == NULL)
1.87      djm       324:                return NULL;
                    325:        return expand_authorized_keys(options.authorized_principals_file, pw);
                    326: }
                    327:
1.24      markus    328: /* return ok if key exists in sysfile or userfile */
                    329: HostStatus
                    330: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
                    331:     const char *sysfile, const char *userfile)
                    332: {
                    333:        char *user_hostfile;
                    334:        struct stat st;
1.30      stevesk   335:        HostStatus host_status;
1.91      djm       336:        struct hostkeys *hostkeys;
                    337:        const struct hostkey_entry *found;
1.24      markus    338:
1.91      djm       339:        hostkeys = init_hostkeys();
                    340:        load_hostkeys(hostkeys, host, sysfile);
                    341:        if (userfile != NULL) {
1.24      markus    342:                user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
                    343:                if (options.strict_modes &&
                    344:                    (stat(user_hostfile, &st) == 0) &&
                    345:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31      deraadt   346:                    (st.st_mode & 022) != 0)) {
1.47      itojun    347:                        logit("Authentication refused for %.100s: "
1.24      markus    348:                            "bad owner or modes for %.200s",
                    349:                            pw->pw_name, user_hostfile);
1.88      djm       350:                        auth_debug_add("Ignored %.200s: bad ownership or modes",
                    351:                            user_hostfile);
1.24      markus    352:                } else {
                    353:                        temporarily_use_uid(pw);
1.91      djm       354:                        load_hostkeys(hostkeys, host, user_hostfile);
1.24      markus    355:                        restore_uid();
                    356:                }
1.102     djm       357:                free(user_hostfile);
1.24      markus    358:        }
1.91      djm       359:        host_status = check_key_in_hostkeys(hostkeys, key, &found);
                    360:        if (host_status == HOST_REVOKED)
                    361:                error("WARNING: revoked key for %s attempted authentication",
                    362:                    found->host);
                    363:        else if (host_status == HOST_OK)
                    364:                debug("%s: key for %s found at %s:%ld", __func__,
                    365:                    found->host, found->file, found->line);
                    366:        else
                    367:                debug("%s: key for host %s not found", __func__, host);
                    368:
                    369:        free_hostkeys(hostkeys);
1.24      markus    370:
                    371:        return host_status;
                    372: }
                    373:
1.22      markus    374: /*
1.97      djm       375:  * Check a given path for security. This is defined as all components
1.44      stevesk   376:  * of the path to the file must be owned by either the owner of
1.23      markus    377:  * of the file or root and no directories must be group or world writable.
1.22      markus    378:  *
                    379:  * XXX Should any specific check be done for sym links ?
                    380:  *
1.101     dtucker   381:  * Takes a file name, its stat information (preferably from fstat() to
1.97      djm       382:  * avoid races), the uid of the expected owner, their home directory and an
1.22      markus    383:  * error buffer plus max size as arguments.
                    384:  *
                    385:  * Returns 0 on success and -1 on failure
                    386:  */
1.97      djm       387: int
                    388: auth_secure_path(const char *name, struct stat *stp, const char *pw_dir,
                    389:     uid_t uid, char *err, size_t errlen)
1.22      markus    390: {
1.109     deraadt   391:        char buf[PATH_MAX], homedir[PATH_MAX];
1.22      markus    392:        char *cp;
1.46      markus    393:        int comparehome = 0;
1.22      markus    394:        struct stat st;
                    395:
1.97      djm       396:        if (realpath(name, buf) == NULL) {
                    397:                snprintf(err, errlen, "realpath %s failed: %s", name,
1.22      markus    398:                    strerror(errno));
                    399:                return -1;
                    400:        }
1.97      djm       401:        if (pw_dir != NULL && realpath(pw_dir, homedir) != NULL)
1.46      markus    402:                comparehome = 1;
1.22      markus    403:
1.97      djm       404:        if (!S_ISREG(stp->st_mode)) {
                    405:                snprintf(err, errlen, "%s is not a regular file", buf);
                    406:                return -1;
                    407:        }
                    408:        if ((stp->st_uid != 0 && stp->st_uid != uid) ||
                    409:            (stp->st_mode & 022) != 0) {
1.22      markus    410:                snprintf(err, errlen, "bad ownership or modes for file %s",
                    411:                    buf);
                    412:                return -1;
                    413:        }
                    414:
                    415:        /* for each component of the canonical path, walking upwards */
                    416:        for (;;) {
                    417:                if ((cp = dirname(buf)) == NULL) {
                    418:                        snprintf(err, errlen, "dirname() failed");
                    419:                        return -1;
                    420:                }
                    421:                strlcpy(buf, cp, sizeof(buf));
1.25      provos    422:
1.22      markus    423:                if (stat(buf, &st) < 0 ||
                    424:                    (st.st_uid != 0 && st.st_uid != uid) ||
                    425:                    (st.st_mode & 022) != 0) {
1.31      deraadt   426:                        snprintf(err, errlen,
1.22      markus    427:                            "bad ownership or modes for directory %s", buf);
                    428:                        return -1;
                    429:                }
                    430:
1.82      dtucker   431:                /* If are past the homedir then we can stop */
1.94      djm       432:                if (comparehome && strcmp(homedir, buf) == 0)
1.27      markus    433:                        break;
1.94      djm       434:
1.22      markus    435:                /*
                    436:                 * dirname should always complete with a "/" path,
                    437:                 * but we can be paranoid and check for "." too
                    438:                 */
                    439:                if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
                    440:                        break;
                    441:        }
1.17      markus    442:        return 0;
1.97      djm       443: }
                    444:
                    445: /*
                    446:  * Version of secure_path() that accepts an open file descriptor to
                    447:  * avoid races.
                    448:  *
                    449:  * Returns 0 on success and -1 on failure
                    450:  */
                    451: static int
                    452: secure_filename(FILE *f, const char *file, struct passwd *pw,
                    453:     char *err, size_t errlen)
                    454: {
                    455:        struct stat st;
                    456:
                    457:        /* check the open file to avoid races */
                    458:        if (fstat(fileno(f), &st) < 0) {
                    459:                snprintf(err, errlen, "cannot stat file %s: %s",
1.99      dtucker   460:                    file, strerror(errno));
1.97      djm       461:                return -1;
                    462:        }
                    463:        return auth_secure_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen);
1.79      dtucker   464: }
                    465:
1.87      djm       466: static FILE *
                    467: auth_openfile(const char *file, struct passwd *pw, int strict_modes,
                    468:     int log_missing, char *file_type)
1.79      dtucker   469: {
                    470:        char line[1024];
                    471:        struct stat st;
                    472:        int fd;
                    473:        FILE *f;
                    474:
1.81      dtucker   475:        if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
1.87      djm       476:                if (log_missing || errno != ENOENT)
                    477:                        debug("Could not open %s '%s': %s", file_type, file,
1.81      dtucker   478:                           strerror(errno));
1.79      dtucker   479:                return NULL;
1.81      dtucker   480:        }
1.79      dtucker   481:
                    482:        if (fstat(fd, &st) < 0) {
                    483:                close(fd);
                    484:                return NULL;
                    485:        }
                    486:        if (!S_ISREG(st.st_mode)) {
1.87      djm       487:                logit("User %s %s %s is not a regular file",
                    488:                    pw->pw_name, file_type, file);
1.79      dtucker   489:                close(fd);
                    490:                return NULL;
                    491:        }
                    492:        unset_nonblock(fd);
                    493:        if ((f = fdopen(fd, "r")) == NULL) {
                    494:                close(fd);
                    495:                return NULL;
                    496:        }
1.90      djm       497:        if (strict_modes &&
1.79      dtucker   498:            secure_filename(f, file, pw, line, sizeof(line)) != 0) {
                    499:                fclose(f);
                    500:                logit("Authentication refused: %s", line);
1.88      djm       501:                auth_debug_add("Ignored %s: %s", file_type, line);
1.79      dtucker   502:                return NULL;
                    503:        }
                    504:
                    505:        return f;
1.87      djm       506: }
                    507:
                    508:
                    509: FILE *
                    510: auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
                    511: {
                    512:        return auth_openfile(file, pw, strict_modes, 1, "authorized keys");
                    513: }
                    514:
                    515: FILE *
                    516: auth_openprincipals(const char *file, struct passwd *pw, int strict_modes)
                    517: {
                    518:        return auth_openfile(file, pw, strict_modes, 0,
                    519:            "authorized principals");
1.37      provos    520: }
                    521:
                    522: struct passwd *
                    523: getpwnamallow(const char *user)
                    524: {
1.114     djm       525:        struct ssh *ssh = active_state; /* XXX */
1.38      provos    526:        extern login_cap_t *lc;
                    527:        auth_session_t *as;
1.37      provos    528:        struct passwd *pw;
1.96      dtucker   529:        struct connection_info *ci = get_connection_info(1, options.use_dns);
1.71      dtucker   530:
1.96      dtucker   531:        ci->user = user;
                    532:        parse_server_match_config(&options, ci);
1.37      provos    533:
                    534:        pw = getpwnam(user);
1.45      stevesk   535:        if (pw == NULL) {
1.114     djm       536:                logit("Invalid user %.100s from %.100s port %d",
                    537:                    user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.45      stevesk   538:                return (NULL);
                    539:        }
                    540:        if (!allowed_user(pw))
1.38      provos    541:                return (NULL);
                    542:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    543:                debug("unable to get login class: %s", user);
                    544:                return (NULL);
                    545:        }
                    546:        if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43      millert   547:            auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38      provos    548:                debug("Approval failure for %s", user);
1.37      provos    549:                pw = NULL;
1.38      provos    550:        }
                    551:        if (as != NULL)
                    552:                auth_close(as);
1.41      markus    553:        if (pw != NULL)
                    554:                return (pwcopy(pw));
                    555:        return (NULL);
1.85      djm       556: }
                    557:
                    558: /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
                    559: int
                    560: auth_key_is_revoked(Key *key)
                    561: {
1.107     djm       562:        char *fp = NULL;
                    563:        int r;
1.85      djm       564:
                    565:        if (options.revoked_keys_file == NULL)
                    566:                return 0;
1.108     djm       567:        if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    568:            SSH_FP_DEFAULT)) == NULL) {
1.107     djm       569:                r = SSH_ERR_ALLOC_FAIL;
                    570:                error("%s: fingerprint key: %s", __func__, ssh_err(r));
                    571:                goto out;
                    572:        }
                    573:
                    574:        r = sshkey_check_revoked(key, options.revoked_keys_file);
                    575:        switch (r) {
1.100     djm       576:        case 0:
1.107     djm       577:                break; /* not revoked */
                    578:        case SSH_ERR_KEY_REVOKED:
                    579:                error("Authentication key %s %s revoked by file %s",
                    580:                    sshkey_type(key), fp, options.revoked_keys_file);
                    581:                goto out;
1.100     djm       582:        default:
1.107     djm       583:                error("Error checking authentication key %s %s in "
                    584:                    "revoked keys file %s: %s", sshkey_type(key), fp,
                    585:                    options.revoked_keys_file, ssh_err(r));
                    586:                goto out;
1.100     djm       587:        }
1.107     djm       588:
                    589:        /* Success */
                    590:        r = 0;
                    591:
                    592:  out:
                    593:        free(fp);
                    594:        return r == 0 ? 0 : 1;
1.42      markus    595: }
                    596:
                    597: void
                    598: auth_debug_add(const char *fmt,...)
                    599: {
                    600:        char buf[1024];
                    601:        va_list args;
                    602:
                    603:        if (!auth_debug_init)
                    604:                return;
                    605:
                    606:        va_start(args, fmt);
                    607:        vsnprintf(buf, sizeof(buf), fmt, args);
                    608:        va_end(args);
                    609:        buffer_put_cstring(&auth_debug, buf);
                    610: }
                    611:
                    612: void
                    613: auth_debug_send(void)
                    614: {
                    615:        char *msg;
                    616:
                    617:        if (!auth_debug_init)
                    618:                return;
                    619:        while (buffer_len(&auth_debug)) {
                    620:                msg = buffer_get_string(&auth_debug, NULL);
                    621:                packet_send_debug("%s", msg);
1.102     djm       622:                free(msg);
1.42      markus    623:        }
                    624: }
                    625:
                    626: void
                    627: auth_debug_reset(void)
                    628: {
                    629:        if (auth_debug_init)
                    630:                buffer_clear(&auth_debug);
                    631:        else {
                    632:                buffer_init(&auth_debug);
                    633:                auth_debug_init = 1;
                    634:        }
1.49      markus    635: }
                    636:
                    637: struct passwd *
                    638: fakepw(void)
                    639: {
                    640:        static struct passwd fake;
                    641:
                    642:        memset(&fake, 0, sizeof(fake));
                    643:        fake.pw_name = "NOUSER";
                    644:        fake.pw_passwd =
1.51      djm       645:            "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49      markus    646:        fake.pw_gecos = "NOUSER";
1.53      deraadt   647:        fake.pw_uid = (uid_t)-1;
                    648:        fake.pw_gid = (gid_t)-1;
1.49      markus    649:        fake.pw_class = "";
                    650:        fake.pw_dir = "/nonexist";
                    651:        fake.pw_shell = "/nonexist";
                    652:
                    653:        return (&fake);
1.114     djm       654: }
                    655:
                    656: /*
                    657:  * Returns the remote DNS hostname as a string. The returned string must not
                    658:  * be freed. NB. this will usually trigger a DNS query the first time it is
                    659:  * called.
                    660:  * This function does additional checks on the hostname to mitigate some
                    661:  * attacks on legacy rhosts-style authentication.
                    662:  * XXX is RhostsRSAAuthentication vulnerable to these?
                    663:  * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
                    664:  */
                    665:
                    666: static char *
                    667: remote_hostname(struct ssh *ssh)
                    668: {
                    669:        struct sockaddr_storage from;
                    670:        socklen_t fromlen;
                    671:        struct addrinfo hints, *ai, *aitop;
                    672:        char name[NI_MAXHOST], ntop2[NI_MAXHOST];
                    673:        const char *ntop = ssh_remote_ipaddr(ssh);
                    674:
                    675:        /* Get IP address of client. */
                    676:        fromlen = sizeof(from);
                    677:        memset(&from, 0, sizeof(from));
                    678:        if (getpeername(ssh_packet_get_connection_in(ssh),
                    679:            (struct sockaddr *)&from, &fromlen) < 0) {
                    680:                debug("getpeername failed: %.100s", strerror(errno));
                    681:                return strdup(ntop);
                    682:        }
                    683:
                    684:        debug3("Trying to reverse map address %.100s.", ntop);
                    685:        /* Map the IP address to a host name. */
                    686:        if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
                    687:            NULL, 0, NI_NAMEREQD) != 0) {
                    688:                /* Host name not found.  Use ip address. */
                    689:                return strdup(ntop);
                    690:        }
                    691:
                    692:        /*
                    693:         * if reverse lookup result looks like a numeric hostname,
                    694:         * someone is trying to trick us by PTR record like following:
                    695:         *      1.1.1.10.in-addr.arpa.  IN PTR  2.3.4.5
                    696:         */
                    697:        memset(&hints, 0, sizeof(hints));
                    698:        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
                    699:        hints.ai_flags = AI_NUMERICHOST;
                    700:        if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
                    701:                logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
                    702:                    name, ntop);
                    703:                freeaddrinfo(ai);
                    704:                return strdup(ntop);
                    705:        }
                    706:
                    707:        /* Names are stored in lowercase. */
                    708:        lowercase(name);
                    709:
                    710:        /*
                    711:         * Map it back to an IP address and check that the given
                    712:         * address actually is an address of this host.  This is
                    713:         * necessary because anyone with access to a name server can
                    714:         * define arbitrary names for an IP address. Mapping from
                    715:         * name to IP address can be trusted better (but can still be
                    716:         * fooled if the intruder has access to the name server of
                    717:         * the domain).
                    718:         */
                    719:        memset(&hints, 0, sizeof(hints));
                    720:        hints.ai_family = from.ss_family;
                    721:        hints.ai_socktype = SOCK_STREAM;
                    722:        if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
                    723:                logit("reverse mapping checking getaddrinfo for %.700s "
1.115   ! dtucker   724:                    "[%s] failed.", name, ntop);
1.114     djm       725:                return strdup(ntop);
                    726:        }
                    727:        /* Look for the address from the list of addresses. */
                    728:        for (ai = aitop; ai; ai = ai->ai_next) {
                    729:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
                    730:                    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
                    731:                    (strcmp(ntop, ntop2) == 0))
                    732:                                break;
                    733:        }
                    734:        freeaddrinfo(aitop);
                    735:        /* If we reached the end of the list, the address was not there. */
                    736:        if (ai == NULL) {
                    737:                /* Address not found for the host name. */
                    738:                logit("Address %.100s maps to %.600s, but this does not "
1.115   ! dtucker   739:                    "map back to the address.", ntop, name);
1.114     djm       740:                return strdup(ntop);
                    741:        }
                    742:        return strdup(name);
                    743: }
                    744:
                    745: /*
                    746:  * Return the canonical name of the host in the other side of the current
                    747:  * connection.  The host name is cached, so it is efficient to call this
                    748:  * several times.
                    749:  */
                    750:
                    751: const char *
                    752: auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
                    753: {
                    754:        static char *dnsname;
                    755:
                    756:        if (!use_dns)
                    757:                return ssh_remote_ipaddr(ssh);
                    758:        else if (dnsname != NULL)
                    759:                return dnsname;
                    760:        else {
                    761:                dnsname = remote_hostname(ssh);
                    762:                return dnsname;
                    763:        }
1.1       markus    764: }