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

1.147   ! djm         1: /* $OpenBSD: auth.c,v 1.146 2020/01/31 22:42:45 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.125     markus     29: #include <sys/wait.h>
1.22      markus     30:
1.140     djm        31: #include <stdlib.h>
1.70      stevesk    32: #include <errno.h>
1.79      dtucker    33: #include <fcntl.h>
1.77      djm        34: #include <login_cap.h>
1.61      stevesk    35: #include <paths.h>
1.68      stevesk    36: #include <pwd.h>
1.69      stevesk    37: #include <stdarg.h>
1.74      stevesk    38: #include <stdio.h>
1.72      stevesk    39: #include <string.h>
1.80      djm        40: #include <unistd.h>
1.109     deraadt    41: #include <limits.h>
1.114     djm        42: #include <netdb.h>
1.135     djm        43: #include <time.h>
1.1       markus     44:
                     45: #include "xmalloc.h"
1.13      markus     46: #include "match.h"
1.14      markus     47: #include "groupaccess.h"
                     48: #include "log.h"
1.131     markus     49: #include "sshbuf.h"
1.106     millert    50: #include "misc.h"
1.1       markus     51: #include "servconf.h"
1.131     markus     52: #include "sshkey.h"
1.75      deraadt    53: #include "hostfile.h"
1.2       markus     54: #include "auth.h"
1.13      markus     55: #include "auth-options.h"
1.14      markus     56: #include "canohost.h"
1.24      markus     57: #include "uidswap.h"
1.42      markus     58: #include "packet.h"
1.75      deraadt    59: #ifdef GSSAPI
                     60: #include "ssh-gss.h"
                     61: #endif
1.85      djm        62: #include "authfile.h"
1.67      dtucker    63: #include "monitor_wrap.h"
1.107     djm        64: #include "ssherr.h"
1.103     djm        65: #include "compat.h"
1.126     djm        66: #include "channels.h"
1.136     djm        67:
1.1       markus     68: /* import */
                     69: extern ServerOptions options;
1.146     djm        70: extern struct include_list includes;
1.67      dtucker    71: extern int use_privsep;
1.126     djm        72: extern struct sshauthopt *auth_opts;
1.1       markus     73:
1.42      markus     74: /* Debugging messages */
1.131     markus     75: static struct sshbuf *auth_debug;
1.42      markus     76:
1.1       markus     77: /*
1.12      markus     78:  * Check if the user is allowed to log in via ssh. If user is listed
                     79:  * in DenyUsers or one of user's groups is listed in DenyGroups, false
                     80:  * will be returned. If AllowUsers isn't empty and user isn't listed
                     81:  * there, or if AllowGroups isn't empty and one of user's groups isn't
                     82:  * listed there, false will be returned.
1.1       markus     83:  * If the user's shell is not executable, false will be returned.
1.4       markus     84:  * Otherwise true is returned.
1.1       markus     85:  */
1.5       markus     86: int
1.138     djm        87: allowed_user(struct ssh *ssh, struct passwd * pw)
1.1       markus     88: {
                     89:        struct stat st;
1.35      markus     90:        const char *hostname = NULL, *ipaddr = NULL;
1.117     djm        91:        int r;
1.60      djm        92:        u_int i;
1.1       markus     93:
                     94:        /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12      markus     95:        if (!pw || !pw->pw_name)
1.1       markus     96:                return 0;
                     97:
1.7       deraadt    98:        /*
1.84      djm        99:         * Deny if shell does not exist or is not executable unless we
                    100:         * are chrooting.
1.7       deraadt   101:         */
1.84      djm       102:        if (options.chroot_directory == NULL ||
                    103:            strcasecmp(options.chroot_directory, "none") == 0) {
                    104:                char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
                    105:                    _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
                    106:
1.139     deraadt   107:                if (stat(shell, &st) == -1) {
1.84      djm       108:                        logit("User %.100s not allowed because shell %.100s "
                    109:                            "does not exist", pw->pw_name, shell);
1.102     djm       110:                        free(shell);
1.84      djm       111:                        return 0;
                    112:                }
                    113:                if (S_ISREG(st.st_mode) == 0 ||
                    114:                    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
                    115:                        logit("User %.100s not allowed because shell %.100s "
                    116:                            "is not executable", pw->pw_name, shell);
1.102     djm       117:                        free(shell);
1.84      djm       118:                        return 0;
                    119:                }
1.102     djm       120:                free(shell);
1.34      stevesk   121:        }
1.1       markus    122:
1.58      dtucker   123:        if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
                    124:            options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.114     djm       125:                hostname = auth_get_canonical_hostname(ssh, options.use_dns);
                    126:                ipaddr = ssh_remote_ipaddr(ssh);
1.35      markus    127:        }
                    128:
1.1       markus    129:        /* Return false if user is listed in DenyUsers */
                    130:        if (options.num_deny_users > 0) {
1.119     dtucker   131:                for (i = 0; i < options.num_deny_users; i++) {
1.117     djm       132:                        r = match_user(pw->pw_name, hostname, ipaddr,
                    133:                            options.deny_users[i]);
                    134:                        if (r < 0) {
                    135:                                fatal("Invalid DenyUsers pattern \"%.100s\"",
                    136:                                    options.deny_users[i]);
1.118     djm       137:                        } else if (r != 0) {
1.57      dtucker   138:                                logit("User %.100s from %.100s not allowed "
                    139:                                    "because listed in DenyUsers",
                    140:                                    pw->pw_name, hostname);
1.1       markus    141:                                return 0;
1.34      stevesk   142:                        }
1.119     dtucker   143:                }
1.1       markus    144:        }
                    145:        /* Return false if AllowUsers isn't empty and user isn't listed there */
                    146:        if (options.num_allow_users > 0) {
1.117     djm       147:                for (i = 0; i < options.num_allow_users; i++) {
                    148:                        r = match_user(pw->pw_name, hostname, ipaddr,
                    149:                            options.allow_users[i]);
                    150:                        if (r < 0) {
                    151:                                fatal("Invalid AllowUsers pattern \"%.100s\"",
                    152:                                    options.allow_users[i]);
                    153:                        } else if (r == 1)
1.1       markus    154:                                break;
1.117     djm       155:                }
1.1       markus    156:                /* i < options.num_allow_users iff we break for loop */
1.34      stevesk   157:                if (i >= options.num_allow_users) {
1.57      dtucker   158:                        logit("User %.100s from %.100s not allowed because "
                    159:                            "not listed in AllowUsers", pw->pw_name, hostname);
1.1       markus    160:                        return 0;
1.34      stevesk   161:                }
1.1       markus    162:        }
                    163:        if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12      markus    164:                /* Get the user's group access list (primary and supplementary) */
1.34      stevesk   165:                if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57      dtucker   166:                        logit("User %.100s from %.100s not allowed because "
                    167:                            "not in any group", pw->pw_name, hostname);
1.1       markus    168:                        return 0;
1.34      stevesk   169:                }
1.1       markus    170:
1.12      markus    171:                /* Return false if one of user's groups is listed in DenyGroups */
                    172:                if (options.num_deny_groups > 0)
                    173:                        if (ga_match(options.deny_groups,
                    174:                            options.num_deny_groups)) {
                    175:                                ga_free();
1.57      dtucker   176:                                logit("User %.100s from %.100s not allowed "
                    177:                                    "because a group is listed in DenyGroups",
                    178:                                    pw->pw_name, hostname);
1.1       markus    179:                                return 0;
1.12      markus    180:                        }
1.1       markus    181:                /*
1.12      markus    182:                 * Return false if AllowGroups isn't empty and one of user's groups
1.1       markus    183:                 * isn't listed there
                    184:                 */
1.12      markus    185:                if (options.num_allow_groups > 0)
                    186:                        if (!ga_match(options.allow_groups,
                    187:                            options.num_allow_groups)) {
                    188:                                ga_free();
1.57      dtucker   189:                                logit("User %.100s from %.100s not allowed "
                    190:                                    "because none of user's groups are listed "
                    191:                                    "in AllowGroups", pw->pw_name, hostname);
1.1       markus    192:                                return 0;
1.12      markus    193:                        }
                    194:                ga_free();
1.1       markus    195:        }
                    196:        /* We found no reason not to let this user try to log on... */
                    197:        return 1;
1.13      markus    198: }
                    199:
1.122     djm       200: /*
                    201:  * Formats any key left in authctxt->auth_method_key for inclusion in
                    202:  * auth_log()'s message. Also includes authxtct->auth_method_info if present.
                    203:  */
                    204: static char *
                    205: format_method_key(Authctxt *authctxt)
1.103     djm       206: {
1.122     djm       207:        const struct sshkey *key = authctxt->auth_method_key;
                    208:        const char *methinfo = authctxt->auth_method_info;
1.133     djm       209:        char *fp, *cafp, *ret = NULL;
1.122     djm       210:
                    211:        if (key == NULL)
                    212:                return NULL;
                    213:
1.131     markus    214:        if (sshkey_is_cert(key)) {
1.133     djm       215:                fp = sshkey_fingerprint(key,
1.122     djm       216:                    options.fingerprint_hash, SSH_FP_DEFAULT);
1.133     djm       217:                cafp = sshkey_fingerprint(key->cert->signature_key,
                    218:                    options.fingerprint_hash, SSH_FP_DEFAULT);
                    219:                xasprintf(&ret, "%s %s ID %s (serial %llu) CA %s %s%s%s",
                    220:                    sshkey_type(key), fp == NULL ? "(null)" : fp,
                    221:                    key->cert->key_id,
1.122     djm       222:                    (unsigned long long)key->cert->serial,
                    223:                    sshkey_type(key->cert->signature_key),
1.133     djm       224:                    cafp == NULL ? "(null)" : cafp,
1.122     djm       225:                    methinfo == NULL ? "" : ", ",
                    226:                    methinfo == NULL ? "" : methinfo);
                    227:                free(fp);
1.133     djm       228:                free(cafp);
1.122     djm       229:        } else {
                    230:                fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    231:                    SSH_FP_DEFAULT);
                    232:                xasprintf(&ret, "%s %s%s%s", sshkey_type(key),
                    233:                    fp == NULL ? "(null)" : fp,
                    234:                    methinfo == NULL ? "" : ", ",
                    235:                    methinfo == NULL ? "" : methinfo);
                    236:                free(fp);
                    237:        }
                    238:        return ret;
1.103     djm       239: }
                    240:
                    241: void
1.138     djm       242: auth_log(struct ssh *ssh, int authenticated, int partial,
1.103     djm       243:     const char *method, const char *submethod)
1.13      markus    244: {
1.138     djm       245:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.133     djm       246:        int level = SYSLOG_LEVEL_VERBOSE;
1.122     djm       247:        const char *authmsg;
                    248:        char *extra = NULL;
1.67      dtucker   249:
                    250:        if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
                    251:                return;
1.13      markus    252:
                    253:        /* Raise logging level */
                    254:        if (authenticated == 1 ||
                    255:            !authctxt->valid ||
1.54      dtucker   256:            authctxt->failures >= options.max_authtries / 2 ||
1.13      markus    257:            strcmp(method, "password") == 0)
1.133     djm       258:                level = SYSLOG_LEVEL_INFO;
1.13      markus    259:
                    260:        if (authctxt->postponed)
                    261:                authmsg = "Postponed";
1.98      djm       262:        else if (partial)
                    263:                authmsg = "Partial";
1.13      markus    264:        else
                    265:                authmsg = authenticated ? "Accepted" : "Failed";
                    266:
1.122     djm       267:        if ((extra = format_method_key(authctxt)) == NULL) {
                    268:                if (authctxt->auth_method_info != NULL)
                    269:                        extra = xstrdup(authctxt->auth_method_info);
                    270:        }
                    271:
1.133     djm       272:        do_log2(level, "%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s",
1.13      markus    273:            authmsg,
                    274:            method,
1.98      djm       275:            submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
1.56      markus    276:            authctxt->valid ? "" : "invalid user ",
1.29      markus    277:            authctxt->user,
1.114     djm       278:            ssh_remote_ipaddr(ssh),
                    279:            ssh_remote_port(ssh),
1.122     djm       280:            extra != NULL ? ": " : "",
                    281:            extra != NULL ? extra : "");
                    282:
                    283:        free(extra);
1.105     djm       284: }
                    285:
                    286: void
1.138     djm       287: auth_maxtries_exceeded(struct ssh *ssh)
1.105     djm       288: {
1.138     djm       289:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.114     djm       290:
1.110     djm       291:        error("maximum authentication attempts exceeded for "
1.116     markus    292:            "%s%.100s from %.200s port %d ssh2",
1.105     djm       293:            authctxt->valid ? "" : "invalid user ",
                    294:            authctxt->user,
1.114     djm       295:            ssh_remote_ipaddr(ssh),
1.116     markus    296:            ssh_remote_port(ssh));
1.138     djm       297:        ssh_packet_disconnect(ssh, "Too many authentication failures");
1.105     djm       298:        /* NOTREACHED */
1.13      markus    299: }
                    300:
                    301: /*
1.17      markus    302:  * Check whether root logins are disallowed.
1.13      markus    303:  */
                    304: int
1.126     djm       305: auth_root_allowed(struct ssh *ssh, const char *method)
1.13      markus    306: {
1.17      markus    307:        switch (options.permit_root_login) {
                    308:        case PERMIT_YES:
1.13      markus    309:                return 1;
1.17      markus    310:        case PERMIT_NO_PASSWD:
1.112     deraadt   311:                if (strcmp(method, "publickey") == 0 ||
                    312:                    strcmp(method, "hostbased") == 0 ||
1.113     djm       313:                    strcmp(method, "gssapi-with-mic") == 0)
1.17      markus    314:                        return 1;
                    315:                break;
                    316:        case PERMIT_FORCED_ONLY:
1.126     djm       317:                if (auth_opts->force_command != NULL) {
1.47      itojun    318:                        logit("Root login accepted for forced command.");
1.17      markus    319:                        return 1;
                    320:                }
                    321:                break;
1.13      markus    322:        }
1.114     djm       323:        logit("ROOT LOGIN REFUSED FROM %.200s port %d",
                    324:            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.22      markus    325:        return 0;
                    326: }
                    327:
                    328:
                    329: /*
                    330:  * Given a template and a passwd structure, build a filename
                    331:  * by substituting % tokenised options. Currently, %% becomes '%',
                    332:  * %h becomes the home directory and %u the username.
                    333:  *
                    334:  * This returns a buffer allocated by xmalloc.
                    335:  */
1.93      djm       336: char *
1.59      djm       337: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22      markus    338: {
1.129     djm       339:        char *file, uidstr[32], ret[PATH_MAX];
1.65      djm       340:        int i;
1.22      markus    341:
1.129     djm       342:        snprintf(uidstr, sizeof(uidstr), "%llu",
                    343:            (unsigned long long)pw->pw_uid);
1.59      djm       344:        file = percent_expand(filename, "h", pw->pw_dir,
1.129     djm       345:            "u", pw->pw_name, "U", uidstr, (char *)NULL);
1.22      markus    346:
                    347:        /*
                    348:         * Ensure that filename starts anchored. If not, be backward
                    349:         * compatible and prepend the '%h/'
                    350:         */
1.134     djm       351:        if (path_absolute(file))
1.59      djm       352:                return (file);
                    353:
1.65      djm       354:        i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
                    355:        if (i < 0 || (size_t)i >= sizeof(ret))
1.59      djm       356:                fatal("expand_authorized_keys: path too long");
1.102     djm       357:        free(file);
1.65      djm       358:        return (xstrdup(ret));
1.22      markus    359: }
1.24      markus    360:
1.87      djm       361: char *
                    362: authorized_principals_file(struct passwd *pw)
                    363: {
1.111     djm       364:        if (options.authorized_principals_file == NULL)
1.87      djm       365:                return NULL;
                    366:        return expand_authorized_keys(options.authorized_principals_file, pw);
                    367: }
                    368:
1.24      markus    369: /* return ok if key exists in sysfile or userfile */
                    370: HostStatus
1.121     markus    371: check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host,
1.24      markus    372:     const char *sysfile, const char *userfile)
                    373: {
                    374:        char *user_hostfile;
                    375:        struct stat st;
1.30      stevesk   376:        HostStatus host_status;
1.91      djm       377:        struct hostkeys *hostkeys;
                    378:        const struct hostkey_entry *found;
1.24      markus    379:
1.91      djm       380:        hostkeys = init_hostkeys();
                    381:        load_hostkeys(hostkeys, host, sysfile);
                    382:        if (userfile != NULL) {
1.24      markus    383:                user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
                    384:                if (options.strict_modes &&
                    385:                    (stat(user_hostfile, &st) == 0) &&
                    386:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31      deraadt   387:                    (st.st_mode & 022) != 0)) {
1.47      itojun    388:                        logit("Authentication refused for %.100s: "
1.24      markus    389:                            "bad owner or modes for %.200s",
                    390:                            pw->pw_name, user_hostfile);
1.88      djm       391:                        auth_debug_add("Ignored %.200s: bad ownership or modes",
                    392:                            user_hostfile);
1.24      markus    393:                } else {
                    394:                        temporarily_use_uid(pw);
1.91      djm       395:                        load_hostkeys(hostkeys, host, user_hostfile);
1.24      markus    396:                        restore_uid();
                    397:                }
1.102     djm       398:                free(user_hostfile);
1.24      markus    399:        }
1.91      djm       400:        host_status = check_key_in_hostkeys(hostkeys, key, &found);
                    401:        if (host_status == HOST_REVOKED)
                    402:                error("WARNING: revoked key for %s attempted authentication",
1.142     djm       403:                    host);
1.91      djm       404:        else if (host_status == HOST_OK)
                    405:                debug("%s: key for %s found at %s:%ld", __func__,
                    406:                    found->host, found->file, found->line);
                    407:        else
                    408:                debug("%s: key for host %s not found", __func__, host);
                    409:
                    410:        free_hostkeys(hostkeys);
1.24      markus    411:
                    412:        return host_status;
                    413: }
                    414:
1.87      djm       415: static FILE *
                    416: auth_openfile(const char *file, struct passwd *pw, int strict_modes,
                    417:     int log_missing, char *file_type)
1.79      dtucker   418: {
                    419:        char line[1024];
                    420:        struct stat st;
                    421:        int fd;
                    422:        FILE *f;
                    423:
1.81      dtucker   424:        if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
1.87      djm       425:                if (log_missing || errno != ENOENT)
                    426:                        debug("Could not open %s '%s': %s", file_type, file,
1.81      dtucker   427:                           strerror(errno));
1.79      dtucker   428:                return NULL;
1.81      dtucker   429:        }
1.79      dtucker   430:
1.139     deraadt   431:        if (fstat(fd, &st) == -1) {
1.79      dtucker   432:                close(fd);
                    433:                return NULL;
                    434:        }
                    435:        if (!S_ISREG(st.st_mode)) {
1.87      djm       436:                logit("User %s %s %s is not a regular file",
                    437:                    pw->pw_name, file_type, file);
1.79      dtucker   438:                close(fd);
                    439:                return NULL;
                    440:        }
                    441:        unset_nonblock(fd);
                    442:        if ((f = fdopen(fd, "r")) == NULL) {
                    443:                close(fd);
                    444:                return NULL;
                    445:        }
1.90      djm       446:        if (strict_modes &&
1.123     djm       447:            safe_path_fd(fileno(f), file, pw, line, sizeof(line)) != 0) {
1.79      dtucker   448:                fclose(f);
                    449:                logit("Authentication refused: %s", line);
1.88      djm       450:                auth_debug_add("Ignored %s: %s", file_type, line);
1.79      dtucker   451:                return NULL;
                    452:        }
                    453:
                    454:        return f;
1.87      djm       455: }
                    456:
                    457:
                    458: FILE *
                    459: auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
                    460: {
                    461:        return auth_openfile(file, pw, strict_modes, 1, "authorized keys");
                    462: }
                    463:
                    464: FILE *
                    465: auth_openprincipals(const char *file, struct passwd *pw, int strict_modes)
                    466: {
                    467:        return auth_openfile(file, pw, strict_modes, 0,
                    468:            "authorized principals");
1.37      provos    469: }
                    470:
                    471: struct passwd *
1.138     djm       472: getpwnamallow(struct ssh *ssh, const char *user)
1.37      provos    473: {
1.38      provos    474:        extern login_cap_t *lc;
                    475:        auth_session_t *as;
1.37      provos    476:        struct passwd *pw;
1.137     djm       477:        struct connection_info *ci;
1.71      dtucker   478:
1.137     djm       479:        ci = get_connection_info(ssh, 1, options.use_dns);
1.96      dtucker   480:        ci->user = user;
1.146     djm       481:        parse_server_match_config(&options, &includes, ci);
1.120     djm       482:        log_change_level(options.log_level);
1.124     djm       483:        process_permitopen(ssh, &options);
1.37      provos    484:
                    485:        pw = getpwnam(user);
1.45      stevesk   486:        if (pw == NULL) {
1.114     djm       487:                logit("Invalid user %.100s from %.100s port %d",
                    488:                    user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.45      stevesk   489:                return (NULL);
                    490:        }
1.138     djm       491:        if (!allowed_user(ssh, pw))
1.38      provos    492:                return (NULL);
                    493:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    494:                debug("unable to get login class: %s", user);
                    495:                return (NULL);
                    496:        }
                    497:        if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43      millert   498:            auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38      provos    499:                debug("Approval failure for %s", user);
1.37      provos    500:                pw = NULL;
1.38      provos    501:        }
                    502:        if (as != NULL)
                    503:                auth_close(as);
1.41      markus    504:        if (pw != NULL)
                    505:                return (pwcopy(pw));
                    506:        return (NULL);
1.85      djm       507: }
                    508:
                    509: /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
                    510: int
1.121     markus    511: auth_key_is_revoked(struct sshkey *key)
1.85      djm       512: {
1.107     djm       513:        char *fp = NULL;
                    514:        int r;
1.85      djm       515:
                    516:        if (options.revoked_keys_file == NULL)
                    517:                return 0;
1.108     djm       518:        if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    519:            SSH_FP_DEFAULT)) == NULL) {
1.107     djm       520:                r = SSH_ERR_ALLOC_FAIL;
                    521:                error("%s: fingerprint key: %s", __func__, ssh_err(r));
                    522:                goto out;
                    523:        }
                    524:
                    525:        r = sshkey_check_revoked(key, options.revoked_keys_file);
                    526:        switch (r) {
1.100     djm       527:        case 0:
1.107     djm       528:                break; /* not revoked */
                    529:        case SSH_ERR_KEY_REVOKED:
                    530:                error("Authentication key %s %s revoked by file %s",
                    531:                    sshkey_type(key), fp, options.revoked_keys_file);
                    532:                goto out;
1.100     djm       533:        default:
1.107     djm       534:                error("Error checking authentication key %s %s in "
                    535:                    "revoked keys file %s: %s", sshkey_type(key), fp,
                    536:                    options.revoked_keys_file, ssh_err(r));
                    537:                goto out;
1.100     djm       538:        }
1.107     djm       539:
                    540:        /* Success */
                    541:        r = 0;
                    542:
                    543:  out:
                    544:        free(fp);
                    545:        return r == 0 ? 0 : 1;
1.42      markus    546: }
                    547:
                    548: void
                    549: auth_debug_add(const char *fmt,...)
                    550: {
                    551:        char buf[1024];
                    552:        va_list args;
1.131     markus    553:        int r;
1.42      markus    554:
1.131     markus    555:        if (auth_debug == NULL)
1.42      markus    556:                return;
                    557:
                    558:        va_start(args, fmt);
                    559:        vsnprintf(buf, sizeof(buf), fmt, args);
                    560:        va_end(args);
1.131     markus    561:        if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0)
                    562:                fatal("%s: sshbuf_put_cstring: %s", __func__, ssh_err(r));
1.42      markus    563: }
                    564:
                    565: void
1.138     djm       566: auth_debug_send(struct ssh *ssh)
1.42      markus    567: {
                    568:        char *msg;
1.131     markus    569:        int r;
1.42      markus    570:
1.131     markus    571:        if (auth_debug == NULL)
1.42      markus    572:                return;
1.131     markus    573:        while (sshbuf_len(auth_debug) != 0) {
                    574:                if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0)
                    575:                        fatal("%s: sshbuf_get_cstring: %s",
                    576:                            __func__, ssh_err(r));
                    577:                ssh_packet_send_debug(ssh, "%s", msg);
1.102     djm       578:                free(msg);
1.42      markus    579:        }
                    580: }
                    581:
                    582: void
                    583: auth_debug_reset(void)
                    584: {
1.131     markus    585:        if (auth_debug != NULL)
                    586:                sshbuf_reset(auth_debug);
                    587:        else if ((auth_debug = sshbuf_new()) == NULL)
                    588:                fatal("%s: sshbuf_new failed", __func__);
1.49      markus    589: }
                    590:
                    591: struct passwd *
                    592: fakepw(void)
                    593: {
                    594:        static struct passwd fake;
                    595:
                    596:        memset(&fake, 0, sizeof(fake));
                    597:        fake.pw_name = "NOUSER";
                    598:        fake.pw_passwd =
1.51      djm       599:            "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49      markus    600:        fake.pw_gecos = "NOUSER";
1.53      deraadt   601:        fake.pw_uid = (uid_t)-1;
                    602:        fake.pw_gid = (gid_t)-1;
1.49      markus    603:        fake.pw_class = "";
                    604:        fake.pw_dir = "/nonexist";
                    605:        fake.pw_shell = "/nonexist";
                    606:
                    607:        return (&fake);
1.114     djm       608: }
                    609:
                    610: /*
                    611:  * Returns the remote DNS hostname as a string. The returned string must not
                    612:  * be freed. NB. this will usually trigger a DNS query the first time it is
                    613:  * called.
                    614:  * This function does additional checks on the hostname to mitigate some
                    615:  * attacks on legacy rhosts-style authentication.
                    616:  * XXX is RhostsRSAAuthentication vulnerable to these?
                    617:  * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
                    618:  */
                    619:
                    620: static char *
                    621: remote_hostname(struct ssh *ssh)
                    622: {
                    623:        struct sockaddr_storage from;
                    624:        socklen_t fromlen;
                    625:        struct addrinfo hints, *ai, *aitop;
                    626:        char name[NI_MAXHOST], ntop2[NI_MAXHOST];
                    627:        const char *ntop = ssh_remote_ipaddr(ssh);
                    628:
                    629:        /* Get IP address of client. */
                    630:        fromlen = sizeof(from);
                    631:        memset(&from, 0, sizeof(from));
                    632:        if (getpeername(ssh_packet_get_connection_in(ssh),
1.139     deraadt   633:            (struct sockaddr *)&from, &fromlen) == -1) {
1.114     djm       634:                debug("getpeername failed: %.100s", strerror(errno));
1.144     tobhe     635:                return xstrdup(ntop);
1.114     djm       636:        }
                    637:
                    638:        debug3("Trying to reverse map address %.100s.", ntop);
                    639:        /* Map the IP address to a host name. */
                    640:        if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
                    641:            NULL, 0, NI_NAMEREQD) != 0) {
                    642:                /* Host name not found.  Use ip address. */
1.144     tobhe     643:                return xstrdup(ntop);
1.114     djm       644:        }
                    645:
                    646:        /*
                    647:         * if reverse lookup result looks like a numeric hostname,
                    648:         * someone is trying to trick us by PTR record like following:
                    649:         *      1.1.1.10.in-addr.arpa.  IN PTR  2.3.4.5
                    650:         */
                    651:        memset(&hints, 0, sizeof(hints));
                    652:        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
                    653:        hints.ai_flags = AI_NUMERICHOST;
                    654:        if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
                    655:                logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
                    656:                    name, ntop);
                    657:                freeaddrinfo(ai);
1.144     tobhe     658:                return xstrdup(ntop);
1.114     djm       659:        }
                    660:
                    661:        /* Names are stored in lowercase. */
                    662:        lowercase(name);
                    663:
                    664:        /*
                    665:         * Map it back to an IP address and check that the given
                    666:         * address actually is an address of this host.  This is
                    667:         * necessary because anyone with access to a name server can
                    668:         * define arbitrary names for an IP address. Mapping from
                    669:         * name to IP address can be trusted better (but can still be
                    670:         * fooled if the intruder has access to the name server of
                    671:         * the domain).
                    672:         */
                    673:        memset(&hints, 0, sizeof(hints));
                    674:        hints.ai_family = from.ss_family;
                    675:        hints.ai_socktype = SOCK_STREAM;
                    676:        if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
                    677:                logit("reverse mapping checking getaddrinfo for %.700s "
1.115     dtucker   678:                    "[%s] failed.", name, ntop);
1.144     tobhe     679:                return xstrdup(ntop);
1.114     djm       680:        }
                    681:        /* Look for the address from the list of addresses. */
                    682:        for (ai = aitop; ai; ai = ai->ai_next) {
                    683:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
                    684:                    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
                    685:                    (strcmp(ntop, ntop2) == 0))
                    686:                                break;
                    687:        }
                    688:        freeaddrinfo(aitop);
                    689:        /* If we reached the end of the list, the address was not there. */
                    690:        if (ai == NULL) {
                    691:                /* Address not found for the host name. */
                    692:                logit("Address %.100s maps to %.600s, but this does not "
1.115     dtucker   693:                    "map back to the address.", ntop, name);
1.144     tobhe     694:                return xstrdup(ntop);
1.114     djm       695:        }
1.144     tobhe     696:        return xstrdup(name);
1.114     djm       697: }
                    698:
                    699: /*
                    700:  * Return the canonical name of the host in the other side of the current
                    701:  * connection.  The host name is cached, so it is efficient to call this
                    702:  * several times.
                    703:  */
                    704:
                    705: const char *
                    706: auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
                    707: {
                    708:        static char *dnsname;
                    709:
                    710:        if (!use_dns)
                    711:                return ssh_remote_ipaddr(ssh);
                    712:        else if (dnsname != NULL)
                    713:                return dnsname;
                    714:        else {
                    715:                dnsname = remote_hostname(ssh);
                    716:                return dnsname;
                    717:        }
1.125     markus    718: }
                    719:
                    720: /*
1.132     martijn   721:  * Runs command in a subprocess with a minimal environment.
1.125     markus    722:  * Returns pid on success, 0 on failure.
                    723:  * The child stdout and stderr maybe captured, left attached or sent to
                    724:  * /dev/null depending on the contents of flags.
                    725:  * "tag" is prepended to log messages.
                    726:  * NB. "command" is only used for logging; the actual command executed is
                    727:  * av[0].
                    728:  */
                    729: pid_t
                    730: subprocess(const char *tag, struct passwd *pw, const char *command,
                    731:     int ac, char **av, FILE **child, u_int flags)
                    732: {
                    733:        FILE *f = NULL;
                    734:        struct stat st;
                    735:        int fd, devnull, p[2], i;
                    736:        pid_t pid;
                    737:        char *cp, errmsg[512];
                    738:        u_int envsize;
                    739:        char **child_env;
                    740:
                    741:        if (child != NULL)
                    742:                *child = NULL;
                    743:
                    744:        debug3("%s: %s command \"%s\" running as %s (flags 0x%x)", __func__,
                    745:            tag, command, pw->pw_name, flags);
                    746:
                    747:        /* Check consistency */
                    748:        if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 &&
                    749:            (flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0) {
                    750:                error("%s: inconsistent flags", __func__);
                    751:                return 0;
                    752:        }
                    753:        if (((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0) != (child == NULL)) {
                    754:                error("%s: inconsistent flags/output", __func__);
                    755:                return 0;
                    756:        }
                    757:
                    758:        /*
                    759:         * If executing an explicit binary, then verify the it exists
                    760:         * and appears safe-ish to execute
                    761:         */
1.134     djm       762:        if (!path_absolute(av[0])) {
1.125     markus    763:                error("%s path is not absolute", tag);
                    764:                return 0;
                    765:        }
                    766:        temporarily_use_uid(pw);
1.139     deraadt   767:        if (stat(av[0], &st) == -1) {
1.125     markus    768:                error("Could not stat %s \"%s\": %s", tag,
                    769:                    av[0], strerror(errno));
                    770:                restore_uid();
                    771:                return 0;
                    772:        }
                    773:        if (safe_path(av[0], &st, NULL, 0, errmsg, sizeof(errmsg)) != 0) {
                    774:                error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
                    775:                restore_uid();
                    776:                return 0;
                    777:        }
                    778:        /* Prepare to keep the child's stdout if requested */
1.139     deraadt   779:        if (pipe(p) == -1) {
1.125     markus    780:                error("%s: pipe: %s", tag, strerror(errno));
                    781:                restore_uid();
                    782:                return 0;
                    783:        }
                    784:        restore_uid();
                    785:
                    786:        switch ((pid = fork())) {
                    787:        case -1: /* error */
                    788:                error("%s: fork: %s", tag, strerror(errno));
                    789:                close(p[0]);
                    790:                close(p[1]);
                    791:                return 0;
                    792:        case 0: /* child */
                    793:                /* Prepare a minimal environment for the child. */
                    794:                envsize = 5;
                    795:                child_env = xcalloc(sizeof(*child_env), envsize);
                    796:                child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH);
                    797:                child_set_env(&child_env, &envsize, "USER", pw->pw_name);
                    798:                child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name);
                    799:                child_set_env(&child_env, &envsize, "HOME", pw->pw_dir);
                    800:                if ((cp = getenv("LANG")) != NULL)
                    801:                        child_set_env(&child_env, &envsize, "LANG", cp);
                    802:
                    803:                for (i = 0; i < NSIG; i++)
1.145     dtucker   804:                        ssh_signal(i, SIG_DFL);
1.125     markus    805:
                    806:                if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
                    807:                        error("%s: open %s: %s", tag, _PATH_DEVNULL,
                    808:                            strerror(errno));
                    809:                        _exit(1);
                    810:                }
                    811:                if (dup2(devnull, STDIN_FILENO) == -1) {
                    812:                        error("%s: dup2: %s", tag, strerror(errno));
                    813:                        _exit(1);
                    814:                }
                    815:
                    816:                /* Set up stdout as requested; leave stderr in place for now. */
                    817:                fd = -1;
                    818:                if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0)
                    819:                        fd = p[1];
                    820:                else if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0)
                    821:                        fd = devnull;
                    822:                if (fd != -1 && dup2(fd, STDOUT_FILENO) == -1) {
                    823:                        error("%s: dup2: %s", tag, strerror(errno));
                    824:                        _exit(1);
                    825:                }
                    826:                closefrom(STDERR_FILENO + 1);
                    827:
                    828:                /* Don't use permanently_set_uid() here to avoid fatal() */
1.139     deraadt   829:                if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1) {
1.125     markus    830:                        error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
                    831:                            strerror(errno));
                    832:                        _exit(1);
                    833:                }
1.139     deraadt   834:                if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1) {
1.125     markus    835:                        error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
                    836:                            strerror(errno));
                    837:                        _exit(1);
                    838:                }
                    839:                /* stdin is pointed to /dev/null at this point */
                    840:                if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 &&
                    841:                    dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
                    842:                        error("%s: dup2: %s", tag, strerror(errno));
                    843:                        _exit(1);
                    844:                }
                    845:
                    846:                execve(av[0], av, child_env);
                    847:                error("%s exec \"%s\": %s", tag, command, strerror(errno));
                    848:                _exit(127);
                    849:        default: /* parent */
                    850:                break;
                    851:        }
                    852:
                    853:        close(p[1]);
                    854:        if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0)
                    855:                close(p[0]);
                    856:        else if ((f = fdopen(p[0], "r")) == NULL) {
                    857:                error("%s: fdopen: %s", tag, strerror(errno));
                    858:                close(p[0]);
                    859:                /* Don't leave zombie child */
                    860:                kill(pid, SIGTERM);
                    861:                while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
                    862:                        ;
                    863:                return 0;
                    864:        }
                    865:        /* Success */
                    866:        debug3("%s: %s pid %ld", __func__, tag, (long)pid);
                    867:        if (child != NULL)
                    868:                *child = f;
                    869:        return pid;
1.126     djm       870: }
                    871:
                    872: /* These functions link key/cert options to the auth framework */
                    873:
                    874: /* Log sshauthopt options locally and (optionally) for remote transmission */
                    875: void
                    876: auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote)
                    877: {
                    878:        int do_env = options.permit_user_env && opts->nenv > 0;
                    879:        int do_permitopen = opts->npermitopen > 0 &&
                    880:            (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0;
1.130     djm       881:        int do_permitlisten = opts->npermitlisten > 0 &&
                    882:            (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0;
1.126     djm       883:        size_t i;
1.127     djm       884:        char msg[1024], buf[64];
1.126     djm       885:
1.127     djm       886:        snprintf(buf, sizeof(buf), "%d", opts->force_tun_device);
1.126     djm       887:        /* Try to keep this alphabetically sorted */
1.147   ! djm       888:        snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1.126     djm       889:            opts->permit_agent_forwarding_flag ? " agent-forwarding" : "",
                    890:            opts->force_command == NULL ? "" : " command",
                    891:            do_env ?  " environment" : "",
1.127     djm       892:            opts->valid_before == 0 ? "" : "expires",
1.147   ! djm       893:            opts->no_require_user_presence ? " no-touch-required" : "",
1.126     djm       894:            do_permitopen ?  " permitopen" : "",
1.130     djm       895:            do_permitlisten ?  " permitlisten" : "",
1.126     djm       896:            opts->permit_port_forwarding_flag ? " port-forwarding" : "",
                    897:            opts->cert_principals == NULL ? "" : " principals",
                    898:            opts->permit_pty_flag ? " pty" : "",
1.147   ! djm       899:            opts->require_verify ? " uv" : "",
1.126     djm       900:            opts->force_tun_device == -1 ? "" : " tun=",
1.127     djm       901:            opts->force_tun_device == -1 ? "" : buf,
1.126     djm       902:            opts->permit_user_rc ? " user-rc" : "",
1.147   ! djm       903:            opts->permit_x11_forwarding_flag ? " x11-forwarding" : "");
1.126     djm       904:
                    905:        debug("%s: %s", loc, msg);
                    906:        if (do_remote)
                    907:                auth_debug_add("%s: %s", loc, msg);
                    908:
                    909:        if (options.permit_user_env) {
                    910:                for (i = 0; i < opts->nenv; i++) {
                    911:                        debug("%s: environment: %s", loc, opts->env[i]);
                    912:                        if (do_remote) {
                    913:                                auth_debug_add("%s: environment: %s",
                    914:                                    loc, opts->env[i]);
                    915:                        }
                    916:                }
                    917:        }
                    918:
                    919:        /* Go into a little more details for the local logs. */
1.127     djm       920:        if (opts->valid_before != 0) {
                    921:                format_absolute_time(opts->valid_before, buf, sizeof(buf));
                    922:                debug("%s: expires at %s", loc, buf);
                    923:        }
1.126     djm       924:        if (opts->cert_principals != NULL) {
                    925:                debug("%s: authorized principals: \"%s\"",
                    926:                    loc, opts->cert_principals);
                    927:        }
                    928:        if (opts->force_command != NULL)
                    929:                debug("%s: forced command: \"%s\"", loc, opts->force_command);
1.130     djm       930:        if (do_permitopen) {
1.126     djm       931:                for (i = 0; i < opts->npermitopen; i++) {
                    932:                        debug("%s: permitted open: %s",
                    933:                            loc, opts->permitopen[i]);
1.130     djm       934:                }
                    935:        }
                    936:        if (do_permitlisten) {
                    937:                for (i = 0; i < opts->npermitlisten; i++) {
                    938:                        debug("%s: permitted listen: %s",
                    939:                            loc, opts->permitlisten[i]);
1.126     djm       940:                }
                    941:        }
                    942: }
                    943:
                    944: /* Activate a new set of key/cert options; merging with what is there. */
                    945: int
                    946: auth_activate_options(struct ssh *ssh, struct sshauthopt *opts)
                    947: {
                    948:        struct sshauthopt *old = auth_opts;
                    949:        const char *emsg = NULL;
                    950:
                    951:        debug("%s: setting new authentication options", __func__);
                    952:        if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) {
                    953:                error("Inconsistent authentication options: %s", emsg);
                    954:                return -1;
                    955:        }
                    956:        return 0;
                    957: }
                    958:
                    959: /* Disable forwarding, etc for the session */
                    960: void
                    961: auth_restrict_session(struct ssh *ssh)
                    962: {
                    963:        struct sshauthopt *restricted;
                    964:
                    965:        debug("%s: restricting session", __func__);
                    966:
                    967:        /* A blank sshauthopt defaults to permitting nothing */
                    968:        restricted = sshauthopt_new();
1.128     djm       969:        restricted->permit_pty_flag = 1;
1.126     djm       970:        restricted->restricted = 1;
                    971:
                    972:        if (auth_activate_options(ssh, restricted) != 0)
                    973:                fatal("%s: failed to restrict session", __func__);
                    974:        sshauthopt_free(restricted);
                    975: }
                    976:
                    977: int
                    978: auth_authorise_keyopts(struct ssh *ssh, struct passwd *pw,
                    979:     struct sshauthopt *opts, int allow_cert_authority, const char *loc)
                    980: {
                    981:        const char *remote_ip = ssh_remote_ipaddr(ssh);
                    982:        const char *remote_host = auth_get_canonical_hostname(ssh,
                    983:            options.use_dns);
1.127     djm       984:        time_t now = time(NULL);
                    985:        char buf[64];
1.126     djm       986:
1.127     djm       987:        /*
                    988:         * Check keys/principals file expiry time.
                    989:         * NB. validity interval in certificate is handled elsewhere.
                    990:         */
                    991:        if (opts->valid_before && now > 0 &&
                    992:            opts->valid_before < (uint64_t)now) {
                    993:                format_absolute_time(opts->valid_before, buf, sizeof(buf));
                    994:                debug("%s: entry expired at %s", loc, buf);
                    995:                auth_debug_add("%s: entry expired at %s", loc, buf);
                    996:                return -1;
                    997:        }
1.126     djm       998:        /* Consistency checks */
                    999:        if (opts->cert_principals != NULL && !opts->cert_authority) {
                   1000:                debug("%s: principals on non-CA key", loc);
                   1001:                auth_debug_add("%s: principals on non-CA key", loc);
                   1002:                /* deny access */
                   1003:                return -1;
                   1004:        }
                   1005:        /* cert-authority flag isn't valid in authorized_principals files */
                   1006:        if (!allow_cert_authority && opts->cert_authority) {
                   1007:                debug("%s: cert-authority flag invalid here", loc);
                   1008:                auth_debug_add("%s: cert-authority flag invalid here", loc);
                   1009:                /* deny access */
                   1010:                return -1;
                   1011:        }
                   1012:
                   1013:        /* Perform from= checks */
                   1014:        if (opts->required_from_host_keys != NULL) {
                   1015:                switch (match_host_and_ip(remote_host, remote_ip,
                   1016:                    opts->required_from_host_keys )) {
                   1017:                case 1:
                   1018:                        /* Host name matches. */
                   1019:                        break;
                   1020:                case -1:
                   1021:                default:
                   1022:                        debug("%s: invalid from criteria", loc);
                   1023:                        auth_debug_add("%s: invalid from criteria", loc);
                   1024:                        /* FALLTHROUGH */
                   1025:                case 0:
                   1026:                        logit("%s: Authentication tried for %.100s with "
                   1027:                            "correct key but not from a permitted "
                   1028:                            "host (host=%.200s, ip=%.200s, required=%.200s).",
                   1029:                            loc, pw->pw_name, remote_host, remote_ip,
                   1030:                            opts->required_from_host_keys);
                   1031:                        auth_debug_add("%s: Your host '%.200s' is not "
                   1032:                            "permitted to use this key for login.",
                   1033:                            loc, remote_host);
                   1034:                        /* deny access */
                   1035:                        return -1;
                   1036:                }
                   1037:        }
                   1038:        /* Check source-address restriction from certificate */
                   1039:        if (opts->required_from_host_cert != NULL) {
                   1040:                switch (addr_match_cidr_list(remote_ip,
                   1041:                    opts->required_from_host_cert)) {
                   1042:                case 1:
                   1043:                        /* accepted */
                   1044:                        break;
                   1045:                case -1:
                   1046:                default:
                   1047:                        /* invalid */
                   1048:                        error("%s: Certificate source-address invalid",
                   1049:                            loc);
                   1050:                        /* FALLTHROUGH */
                   1051:                case 0:
                   1052:                        logit("%s: Authentication tried for %.100s with valid "
                   1053:                            "certificate but not from a permitted source "
                   1054:                            "address (%.200s).", loc, pw->pw_name, remote_ip);
                   1055:                        auth_debug_add("%s: Your address '%.200s' is not "
                   1056:                            "permitted to use this certificate for login.",
                   1057:                            loc, remote_ip);
                   1058:                        return -1;
                   1059:                }
                   1060:        }
                   1061:        /*
                   1062:         *
                   1063:         * XXX this is spammy. We should report remotely only for keys
                   1064:         *     that are successful in actual auth attempts, and not PK_OK
                   1065:         *     tests.
                   1066:         */
                   1067:        auth_log_authopts(loc, opts, 1);
                   1068:
                   1069:        return 0;
1.1       markus   1070: }