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

1.161   ! djm         1: /* $OpenBSD: auth.c,v 1.160 2023/03/05 05:34:09 dtucker 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.126     djm        65: #include "channels.h"
1.136     djm        66:
1.1       markus     67: /* import */
                     68: extern ServerOptions options;
1.146     djm        69: extern struct include_list includes;
1.126     djm        70: extern struct sshauthopt *auth_opts;
1.1       markus     71:
1.42      markus     72: /* Debugging messages */
1.131     markus     73: static struct sshbuf *auth_debug;
1.42      markus     74:
1.1       markus     75: /*
1.12      markus     76:  * Check if the user is allowed to log in via ssh. If user is listed
                     77:  * in DenyUsers or one of user's groups is listed in DenyGroups, false
                     78:  * will be returned. If AllowUsers isn't empty and user isn't listed
                     79:  * there, or if AllowGroups isn't empty and one of user's groups isn't
                     80:  * listed there, false will be returned.
1.1       markus     81:  * If the user's shell is not executable, false will be returned.
1.4       markus     82:  * Otherwise true is returned.
1.1       markus     83:  */
1.5       markus     84: int
1.138     djm        85: allowed_user(struct ssh *ssh, struct passwd * pw)
1.1       markus     86: {
                     87:        struct stat st;
1.35      markus     88:        const char *hostname = NULL, *ipaddr = NULL;
1.117     djm        89:        int r;
1.60      djm        90:        u_int i;
1.1       markus     91:
                     92:        /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12      markus     93:        if (!pw || !pw->pw_name)
1.1       markus     94:                return 0;
                     95:
1.7       deraadt    96:        /*
1.84      djm        97:         * Deny if shell does not exist or is not executable unless we
                     98:         * are chrooting.
1.7       deraadt    99:         */
1.84      djm       100:        if (options.chroot_directory == NULL ||
                    101:            strcasecmp(options.chroot_directory, "none") == 0) {
                    102:                char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
                    103:                    _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
                    104:
1.139     deraadt   105:                if (stat(shell, &st) == -1) {
1.84      djm       106:                        logit("User %.100s not allowed because shell %.100s "
                    107:                            "does not exist", pw->pw_name, shell);
1.102     djm       108:                        free(shell);
1.84      djm       109:                        return 0;
                    110:                }
                    111:                if (S_ISREG(st.st_mode) == 0 ||
                    112:                    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
                    113:                        logit("User %.100s not allowed because shell %.100s "
                    114:                            "is not executable", pw->pw_name, shell);
1.102     djm       115:                        free(shell);
1.84      djm       116:                        return 0;
                    117:                }
1.102     djm       118:                free(shell);
1.34      stevesk   119:        }
1.1       markus    120:
1.58      dtucker   121:        if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
                    122:            options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.114     djm       123:                hostname = auth_get_canonical_hostname(ssh, options.use_dns);
                    124:                ipaddr = ssh_remote_ipaddr(ssh);
1.35      markus    125:        }
                    126:
1.1       markus    127:        /* Return false if user is listed in DenyUsers */
                    128:        if (options.num_deny_users > 0) {
1.119     dtucker   129:                for (i = 0; i < options.num_deny_users; i++) {
1.117     djm       130:                        r = match_user(pw->pw_name, hostname, ipaddr,
                    131:                            options.deny_users[i]);
                    132:                        if (r < 0) {
                    133:                                fatal("Invalid DenyUsers pattern \"%.100s\"",
                    134:                                    options.deny_users[i]);
1.118     djm       135:                        } else if (r != 0) {
1.57      dtucker   136:                                logit("User %.100s from %.100s not allowed "
                    137:                                    "because listed in DenyUsers",
                    138:                                    pw->pw_name, hostname);
1.1       markus    139:                                return 0;
1.34      stevesk   140:                        }
1.119     dtucker   141:                }
1.1       markus    142:        }
                    143:        /* Return false if AllowUsers isn't empty and user isn't listed there */
                    144:        if (options.num_allow_users > 0) {
1.117     djm       145:                for (i = 0; i < options.num_allow_users; i++) {
                    146:                        r = match_user(pw->pw_name, hostname, ipaddr,
                    147:                            options.allow_users[i]);
                    148:                        if (r < 0) {
                    149:                                fatal("Invalid AllowUsers pattern \"%.100s\"",
                    150:                                    options.allow_users[i]);
                    151:                        } else if (r == 1)
1.1       markus    152:                                break;
1.117     djm       153:                }
1.1       markus    154:                /* i < options.num_allow_users iff we break for loop */
1.34      stevesk   155:                if (i >= options.num_allow_users) {
1.57      dtucker   156:                        logit("User %.100s from %.100s not allowed because "
                    157:                            "not listed in AllowUsers", pw->pw_name, hostname);
1.1       markus    158:                        return 0;
1.34      stevesk   159:                }
1.1       markus    160:        }
                    161:        if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12      markus    162:                /* Get the user's group access list (primary and supplementary) */
1.34      stevesk   163:                if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57      dtucker   164:                        logit("User %.100s from %.100s not allowed because "
                    165:                            "not in any group", pw->pw_name, hostname);
1.1       markus    166:                        return 0;
1.34      stevesk   167:                }
1.1       markus    168:
1.12      markus    169:                /* Return false if one of user's groups is listed in DenyGroups */
                    170:                if (options.num_deny_groups > 0)
                    171:                        if (ga_match(options.deny_groups,
                    172:                            options.num_deny_groups)) {
                    173:                                ga_free();
1.57      dtucker   174:                                logit("User %.100s from %.100s not allowed "
                    175:                                    "because a group is listed in DenyGroups",
                    176:                                    pw->pw_name, hostname);
1.1       markus    177:                                return 0;
1.12      markus    178:                        }
1.1       markus    179:                /*
1.12      markus    180:                 * Return false if AllowGroups isn't empty and one of user's groups
1.1       markus    181:                 * isn't listed there
                    182:                 */
1.12      markus    183:                if (options.num_allow_groups > 0)
                    184:                        if (!ga_match(options.allow_groups,
                    185:                            options.num_allow_groups)) {
                    186:                                ga_free();
1.57      dtucker   187:                                logit("User %.100s from %.100s not allowed "
                    188:                                    "because none of user's groups are listed "
                    189:                                    "in AllowGroups", pw->pw_name, hostname);
1.1       markus    190:                                return 0;
1.12      markus    191:                        }
                    192:                ga_free();
1.1       markus    193:        }
                    194:        /* We found no reason not to let this user try to log on... */
                    195:        return 1;
1.13      markus    196: }
                    197:
1.122     djm       198: /*
                    199:  * Formats any key left in authctxt->auth_method_key for inclusion in
                    200:  * auth_log()'s message. Also includes authxtct->auth_method_info if present.
                    201:  */
                    202: static char *
                    203: format_method_key(Authctxt *authctxt)
1.103     djm       204: {
1.122     djm       205:        const struct sshkey *key = authctxt->auth_method_key;
                    206:        const char *methinfo = authctxt->auth_method_info;
1.133     djm       207:        char *fp, *cafp, *ret = NULL;
1.122     djm       208:
                    209:        if (key == NULL)
                    210:                return NULL;
                    211:
1.131     markus    212:        if (sshkey_is_cert(key)) {
1.133     djm       213:                fp = sshkey_fingerprint(key,
1.122     djm       214:                    options.fingerprint_hash, SSH_FP_DEFAULT);
1.133     djm       215:                cafp = sshkey_fingerprint(key->cert->signature_key,
                    216:                    options.fingerprint_hash, SSH_FP_DEFAULT);
                    217:                xasprintf(&ret, "%s %s ID %s (serial %llu) CA %s %s%s%s",
                    218:                    sshkey_type(key), fp == NULL ? "(null)" : fp,
                    219:                    key->cert->key_id,
1.122     djm       220:                    (unsigned long long)key->cert->serial,
                    221:                    sshkey_type(key->cert->signature_key),
1.133     djm       222:                    cafp == NULL ? "(null)" : cafp,
1.122     djm       223:                    methinfo == NULL ? "" : ", ",
                    224:                    methinfo == NULL ? "" : methinfo);
                    225:                free(fp);
1.133     djm       226:                free(cafp);
1.122     djm       227:        } else {
                    228:                fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    229:                    SSH_FP_DEFAULT);
                    230:                xasprintf(&ret, "%s %s%s%s", sshkey_type(key),
                    231:                    fp == NULL ? "(null)" : fp,
                    232:                    methinfo == NULL ? "" : ", ",
                    233:                    methinfo == NULL ? "" : methinfo);
                    234:                free(fp);
                    235:        }
                    236:        return ret;
1.103     djm       237: }
                    238:
                    239: void
1.138     djm       240: auth_log(struct ssh *ssh, int authenticated, int partial,
1.103     djm       241:     const char *method, const char *submethod)
1.13      markus    242: {
1.138     djm       243:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.133     djm       244:        int level = SYSLOG_LEVEL_VERBOSE;
1.122     djm       245:        const char *authmsg;
                    246:        char *extra = NULL;
1.67      dtucker   247:
1.161   ! djm       248:        if (!mm_is_monitor() && !authctxt->postponed)
1.67      dtucker   249:                return;
1.13      markus    250:
                    251:        /* Raise logging level */
                    252:        if (authenticated == 1 ||
                    253:            !authctxt->valid ||
1.54      dtucker   254:            authctxt->failures >= options.max_authtries / 2 ||
1.13      markus    255:            strcmp(method, "password") == 0)
1.133     djm       256:                level = SYSLOG_LEVEL_INFO;
1.13      markus    257:
                    258:        if (authctxt->postponed)
                    259:                authmsg = "Postponed";
1.98      djm       260:        else if (partial)
                    261:                authmsg = "Partial";
1.13      markus    262:        else
                    263:                authmsg = authenticated ? "Accepted" : "Failed";
                    264:
1.122     djm       265:        if ((extra = format_method_key(authctxt)) == NULL) {
                    266:                if (authctxt->auth_method_info != NULL)
                    267:                        extra = xstrdup(authctxt->auth_method_info);
                    268:        }
                    269:
1.133     djm       270:        do_log2(level, "%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s",
1.13      markus    271:            authmsg,
                    272:            method,
1.98      djm       273:            submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
1.56      markus    274:            authctxt->valid ? "" : "invalid user ",
1.29      markus    275:            authctxt->user,
1.114     djm       276:            ssh_remote_ipaddr(ssh),
                    277:            ssh_remote_port(ssh),
1.122     djm       278:            extra != NULL ? ": " : "",
                    279:            extra != NULL ? extra : "");
                    280:
                    281:        free(extra);
1.105     djm       282: }
                    283:
                    284: void
1.138     djm       285: auth_maxtries_exceeded(struct ssh *ssh)
1.105     djm       286: {
1.138     djm       287:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.114     djm       288:
1.110     djm       289:        error("maximum authentication attempts exceeded for "
1.116     markus    290:            "%s%.100s from %.200s port %d ssh2",
1.105     djm       291:            authctxt->valid ? "" : "invalid user ",
                    292:            authctxt->user,
1.114     djm       293:            ssh_remote_ipaddr(ssh),
1.116     markus    294:            ssh_remote_port(ssh));
1.138     djm       295:        ssh_packet_disconnect(ssh, "Too many authentication failures");
1.105     djm       296:        /* NOTREACHED */
1.13      markus    297: }
                    298:
                    299: /*
1.17      markus    300:  * Check whether root logins are disallowed.
1.13      markus    301:  */
                    302: int
1.126     djm       303: auth_root_allowed(struct ssh *ssh, const char *method)
1.13      markus    304: {
1.17      markus    305:        switch (options.permit_root_login) {
                    306:        case PERMIT_YES:
1.13      markus    307:                return 1;
1.17      markus    308:        case PERMIT_NO_PASSWD:
1.112     deraadt   309:                if (strcmp(method, "publickey") == 0 ||
                    310:                    strcmp(method, "hostbased") == 0 ||
1.113     djm       311:                    strcmp(method, "gssapi-with-mic") == 0)
1.17      markus    312:                        return 1;
                    313:                break;
                    314:        case PERMIT_FORCED_ONLY:
1.126     djm       315:                if (auth_opts->force_command != NULL) {
1.47      itojun    316:                        logit("Root login accepted for forced command.");
1.17      markus    317:                        return 1;
                    318:                }
                    319:                break;
1.13      markus    320:        }
1.114     djm       321:        logit("ROOT LOGIN REFUSED FROM %.200s port %d",
                    322:            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.22      markus    323:        return 0;
                    324: }
                    325:
                    326:
                    327: /*
                    328:  * Given a template and a passwd structure, build a filename
                    329:  * by substituting % tokenised options. Currently, %% becomes '%',
                    330:  * %h becomes the home directory and %u the username.
                    331:  *
                    332:  * This returns a buffer allocated by xmalloc.
                    333:  */
1.93      djm       334: char *
1.59      djm       335: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22      markus    336: {
1.129     djm       337:        char *file, uidstr[32], ret[PATH_MAX];
1.65      djm       338:        int i;
1.22      markus    339:
1.129     djm       340:        snprintf(uidstr, sizeof(uidstr), "%llu",
                    341:            (unsigned long long)pw->pw_uid);
1.59      djm       342:        file = percent_expand(filename, "h", pw->pw_dir,
1.129     djm       343:            "u", pw->pw_name, "U", uidstr, (char *)NULL);
1.22      markus    344:
                    345:        /*
                    346:         * Ensure that filename starts anchored. If not, be backward
                    347:         * compatible and prepend the '%h/'
                    348:         */
1.134     djm       349:        if (path_absolute(file))
1.59      djm       350:                return (file);
                    351:
1.65      djm       352:        i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
                    353:        if (i < 0 || (size_t)i >= sizeof(ret))
1.59      djm       354:                fatal("expand_authorized_keys: path too long");
1.102     djm       355:        free(file);
1.65      djm       356:        return (xstrdup(ret));
1.22      markus    357: }
1.24      markus    358:
1.87      djm       359: char *
                    360: authorized_principals_file(struct passwd *pw)
                    361: {
1.111     djm       362:        if (options.authorized_principals_file == NULL)
1.87      djm       363:                return NULL;
                    364:        return expand_authorized_keys(options.authorized_principals_file, pw);
                    365: }
                    366:
1.24      markus    367: /* return ok if key exists in sysfile or userfile */
                    368: HostStatus
1.121     markus    369: check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host,
1.24      markus    370:     const char *sysfile, const char *userfile)
                    371: {
                    372:        char *user_hostfile;
                    373:        struct stat st;
1.30      stevesk   374:        HostStatus host_status;
1.91      djm       375:        struct hostkeys *hostkeys;
                    376:        const struct hostkey_entry *found;
1.24      markus    377:
1.91      djm       378:        hostkeys = init_hostkeys();
1.150     djm       379:        load_hostkeys(hostkeys, host, sysfile, 0);
1.91      djm       380:        if (userfile != NULL) {
1.24      markus    381:                user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
                    382:                if (options.strict_modes &&
                    383:                    (stat(user_hostfile, &st) == 0) &&
                    384:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31      deraadt   385:                    (st.st_mode & 022) != 0)) {
1.47      itojun    386:                        logit("Authentication refused for %.100s: "
1.24      markus    387:                            "bad owner or modes for %.200s",
                    388:                            pw->pw_name, user_hostfile);
1.88      djm       389:                        auth_debug_add("Ignored %.200s: bad ownership or modes",
                    390:                            user_hostfile);
1.24      markus    391:                } else {
                    392:                        temporarily_use_uid(pw);
1.150     djm       393:                        load_hostkeys(hostkeys, host, user_hostfile, 0);
1.24      markus    394:                        restore_uid();
                    395:                }
1.102     djm       396:                free(user_hostfile);
1.24      markus    397:        }
1.91      djm       398:        host_status = check_key_in_hostkeys(hostkeys, key, &found);
                    399:        if (host_status == HOST_REVOKED)
                    400:                error("WARNING: revoked key for %s attempted authentication",
1.142     djm       401:                    host);
1.91      djm       402:        else if (host_status == HOST_OK)
1.149     djm       403:                debug_f("key for %s found at %s:%ld",
1.91      djm       404:                    found->host, found->file, found->line);
                    405:        else
1.149     djm       406:                debug_f("key for host %s not found", host);
1.91      djm       407:
                    408:        free_hostkeys(hostkeys);
1.24      markus    409:
                    410:        return host_status;
1.37      provos    411: }
                    412:
                    413: struct passwd *
1.138     djm       414: getpwnamallow(struct ssh *ssh, const char *user)
1.37      provos    415: {
1.38      provos    416:        extern login_cap_t *lc;
                    417:        auth_session_t *as;
1.37      provos    418:        struct passwd *pw;
1.137     djm       419:        struct connection_info *ci;
1.148     djm       420:        u_int i;
1.71      dtucker   421:
1.161   ! djm       422:        ci = server_get_connection_info(ssh, 1, options.use_dns);
1.96      dtucker   423:        ci->user = user;
1.146     djm       424:        parse_server_match_config(&options, &includes, ci);
1.120     djm       425:        log_change_level(options.log_level);
1.148     djm       426:        log_verbose_reset();
                    427:        for (i = 0; i < options.num_log_verbose; i++)
                    428:                log_verbose_add(options.log_verbose[i]);
1.161   ! djm       429:        server_process_permitopen(ssh);
1.37      provos    430:
                    431:        pw = getpwnam(user);
1.45      stevesk   432:        if (pw == NULL) {
1.114     djm       433:                logit("Invalid user %.100s from %.100s port %d",
                    434:                    user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.45      stevesk   435:                return (NULL);
                    436:        }
1.138     djm       437:        if (!allowed_user(ssh, pw))
1.38      provos    438:                return (NULL);
                    439:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    440:                debug("unable to get login class: %s", user);
                    441:                return (NULL);
                    442:        }
                    443:        if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43      millert   444:            auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38      provos    445:                debug("Approval failure for %s", user);
1.37      provos    446:                pw = NULL;
1.38      provos    447:        }
                    448:        if (as != NULL)
                    449:                auth_close(as);
1.41      markus    450:        if (pw != NULL)
                    451:                return (pwcopy(pw));
                    452:        return (NULL);
1.85      djm       453: }
                    454:
                    455: /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
                    456: int
1.121     markus    457: auth_key_is_revoked(struct sshkey *key)
1.85      djm       458: {
1.107     djm       459:        char *fp = NULL;
                    460:        int r;
1.85      djm       461:
                    462:        if (options.revoked_keys_file == NULL)
                    463:                return 0;
1.108     djm       464:        if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    465:            SSH_FP_DEFAULT)) == NULL) {
1.107     djm       466:                r = SSH_ERR_ALLOC_FAIL;
1.149     djm       467:                error_fr(r, "fingerprint key");
1.107     djm       468:                goto out;
                    469:        }
                    470:
                    471:        r = sshkey_check_revoked(key, options.revoked_keys_file);
                    472:        switch (r) {
1.100     djm       473:        case 0:
1.107     djm       474:                break; /* not revoked */
                    475:        case SSH_ERR_KEY_REVOKED:
                    476:                error("Authentication key %s %s revoked by file %s",
                    477:                    sshkey_type(key), fp, options.revoked_keys_file);
                    478:                goto out;
1.100     djm       479:        default:
1.149     djm       480:                error_r(r, "Error checking authentication key %s %s in "
                    481:                    "revoked keys file %s", sshkey_type(key), fp,
                    482:                    options.revoked_keys_file);
1.107     djm       483:                goto out;
1.100     djm       484:        }
1.107     djm       485:
                    486:        /* Success */
                    487:        r = 0;
                    488:
                    489:  out:
                    490:        free(fp);
                    491:        return r == 0 ? 0 : 1;
1.42      markus    492: }
                    493:
                    494: void
                    495: auth_debug_add(const char *fmt,...)
                    496: {
                    497:        char buf[1024];
                    498:        va_list args;
1.131     markus    499:        int r;
1.42      markus    500:
                    501:        va_start(args, fmt);
                    502:        vsnprintf(buf, sizeof(buf), fmt, args);
                    503:        va_end(args);
1.159     dtucker   504:        debug3("%s", buf);
                    505:        if (auth_debug != NULL)
                    506:                if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0)
                    507:                        fatal_fr(r, "sshbuf_put_cstring");
1.42      markus    508: }
                    509:
                    510: void
1.138     djm       511: auth_debug_send(struct ssh *ssh)
1.42      markus    512: {
                    513:        char *msg;
1.131     markus    514:        int r;
1.42      markus    515:
1.131     markus    516:        if (auth_debug == NULL)
1.42      markus    517:                return;
1.131     markus    518:        while (sshbuf_len(auth_debug) != 0) {
                    519:                if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0)
1.149     djm       520:                        fatal_fr(r, "sshbuf_get_cstring");
1.131     markus    521:                ssh_packet_send_debug(ssh, "%s", msg);
1.102     djm       522:                free(msg);
1.42      markus    523:        }
                    524: }
                    525:
                    526: void
                    527: auth_debug_reset(void)
                    528: {
1.131     markus    529:        if (auth_debug != NULL)
                    530:                sshbuf_reset(auth_debug);
                    531:        else if ((auth_debug = sshbuf_new()) == NULL)
1.149     djm       532:                fatal_f("sshbuf_new failed");
1.49      markus    533: }
                    534:
                    535: struct passwd *
                    536: fakepw(void)
                    537: {
1.154     djm       538:        static int done = 0;
1.49      markus    539:        static struct passwd fake;
1.154     djm       540:        const char hashchars[] = "./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                    541:            "abcdefghijklmnopqrstuvwxyz0123456789"; /* from bcrypt.c */
                    542:        char *cp;
                    543:
                    544:        if (done)
                    545:                return (&fake);
1.49      markus    546:
                    547:        memset(&fake, 0, sizeof(fake));
                    548:        fake.pw_name = "NOUSER";
1.154     djm       549:        fake.pw_passwd = xstrdup("$2a$10$"
                    550:            "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                    551:        for (cp = fake.pw_passwd + 7; *cp != '\0'; cp++)
                    552:                *cp = hashchars[arc4random_uniform(sizeof(hashchars) - 1)];
1.49      markus    553:        fake.pw_gecos = "NOUSER";
1.53      deraadt   554:        fake.pw_uid = (uid_t)-1;
                    555:        fake.pw_gid = (gid_t)-1;
1.49      markus    556:        fake.pw_class = "";
                    557:        fake.pw_dir = "/nonexist";
                    558:        fake.pw_shell = "/nonexist";
1.154     djm       559:        done = 1;
1.49      markus    560:
                    561:        return (&fake);
1.114     djm       562: }
                    563:
                    564: /*
                    565:  * Return the canonical name of the host in the other side of the current
                    566:  * connection.  The host name is cached, so it is efficient to call this
                    567:  * several times.
                    568:  */
                    569:
                    570: const char *
                    571: auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
                    572: {
                    573:        static char *dnsname;
                    574:
                    575:        if (!use_dns)
                    576:                return ssh_remote_ipaddr(ssh);
1.161   ! djm       577:        if (dnsname != NULL)
1.114     djm       578:                return dnsname;
1.161   ! djm       579:        dnsname = ssh_remote_hostname(ssh);
        !           580:        return dnsname;
1.126     djm       581: }
                    582:
                    583: /* These functions link key/cert options to the auth framework */
                    584:
                    585: /* Log sshauthopt options locally and (optionally) for remote transmission */
                    586: void
                    587: auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote)
                    588: {
                    589:        int do_env = options.permit_user_env && opts->nenv > 0;
                    590:        int do_permitopen = opts->npermitopen > 0 &&
                    591:            (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0;
1.130     djm       592:        int do_permitlisten = opts->npermitlisten > 0 &&
                    593:            (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0;
1.126     djm       594:        size_t i;
1.127     djm       595:        char msg[1024], buf[64];
1.126     djm       596:
1.127     djm       597:        snprintf(buf, sizeof(buf), "%d", opts->force_tun_device);
1.126     djm       598:        /* Try to keep this alphabetically sorted */
1.147     djm       599:        snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1.126     djm       600:            opts->permit_agent_forwarding_flag ? " agent-forwarding" : "",
                    601:            opts->force_command == NULL ? "" : " command",
                    602:            do_env ?  " environment" : "",
1.127     djm       603:            opts->valid_before == 0 ? "" : "expires",
1.147     djm       604:            opts->no_require_user_presence ? " no-touch-required" : "",
1.126     djm       605:            do_permitopen ?  " permitopen" : "",
1.130     djm       606:            do_permitlisten ?  " permitlisten" : "",
1.126     djm       607:            opts->permit_port_forwarding_flag ? " port-forwarding" : "",
                    608:            opts->cert_principals == NULL ? "" : " principals",
                    609:            opts->permit_pty_flag ? " pty" : "",
1.147     djm       610:            opts->require_verify ? " uv" : "",
1.126     djm       611:            opts->force_tun_device == -1 ? "" : " tun=",
1.127     djm       612:            opts->force_tun_device == -1 ? "" : buf,
1.126     djm       613:            opts->permit_user_rc ? " user-rc" : "",
1.147     djm       614:            opts->permit_x11_forwarding_flag ? " x11-forwarding" : "");
1.126     djm       615:
                    616:        debug("%s: %s", loc, msg);
                    617:        if (do_remote)
                    618:                auth_debug_add("%s: %s", loc, msg);
                    619:
                    620:        if (options.permit_user_env) {
                    621:                for (i = 0; i < opts->nenv; i++) {
                    622:                        debug("%s: environment: %s", loc, opts->env[i]);
                    623:                        if (do_remote) {
                    624:                                auth_debug_add("%s: environment: %s",
                    625:                                    loc, opts->env[i]);
                    626:                        }
                    627:                }
                    628:        }
                    629:
                    630:        /* Go into a little more details for the local logs. */
1.127     djm       631:        if (opts->valid_before != 0) {
                    632:                format_absolute_time(opts->valid_before, buf, sizeof(buf));
                    633:                debug("%s: expires at %s", loc, buf);
                    634:        }
1.126     djm       635:        if (opts->cert_principals != NULL) {
                    636:                debug("%s: authorized principals: \"%s\"",
                    637:                    loc, opts->cert_principals);
                    638:        }
                    639:        if (opts->force_command != NULL)
                    640:                debug("%s: forced command: \"%s\"", loc, opts->force_command);
1.130     djm       641:        if (do_permitopen) {
1.126     djm       642:                for (i = 0; i < opts->npermitopen; i++) {
                    643:                        debug("%s: permitted open: %s",
                    644:                            loc, opts->permitopen[i]);
1.130     djm       645:                }
                    646:        }
                    647:        if (do_permitlisten) {
                    648:                for (i = 0; i < opts->npermitlisten; i++) {
                    649:                        debug("%s: permitted listen: %s",
                    650:                            loc, opts->permitlisten[i]);
1.126     djm       651:                }
                    652:        }
                    653: }
                    654:
                    655: /* Activate a new set of key/cert options; merging with what is there. */
                    656: int
                    657: auth_activate_options(struct ssh *ssh, struct sshauthopt *opts)
                    658: {
                    659:        struct sshauthopt *old = auth_opts;
                    660:        const char *emsg = NULL;
                    661:
1.149     djm       662:        debug_f("setting new authentication options");
1.126     djm       663:        if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) {
                    664:                error("Inconsistent authentication options: %s", emsg);
                    665:                return -1;
                    666:        }
                    667:        return 0;
                    668: }
                    669:
                    670: /* Disable forwarding, etc for the session */
                    671: void
                    672: auth_restrict_session(struct ssh *ssh)
                    673: {
                    674:        struct sshauthopt *restricted;
                    675:
1.149     djm       676:        debug_f("restricting session");
1.126     djm       677:
                    678:        /* A blank sshauthopt defaults to permitting nothing */
1.155     dtucker   679:        if ((restricted = sshauthopt_new()) == NULL)
                    680:                fatal_f("sshauthopt_new failed");
1.128     djm       681:        restricted->permit_pty_flag = 1;
1.126     djm       682:        restricted->restricted = 1;
                    683:
                    684:        if (auth_activate_options(ssh, restricted) != 0)
1.149     djm       685:                fatal_f("failed to restrict session");
1.126     djm       686:        sshauthopt_free(restricted);
1.1       markus    687: }