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

Annotation of src/usr.bin/ssh/servconf.c, Revision 1.334

1.226     dtucker     1:
1.334   ! djm         2: /* $OpenBSD: servconf.c,v 1.333 2018/06/19 02:59:41 djm Exp $ */
1.1       deraadt     3: /*
1.26      deraadt     4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
1.34      markus      6:  *
1.51      deraadt     7:  * As far as I am concerned, the code I have written for this software
                      8:  * can be used freely for any purpose.  Any derived versions of this
                      9:  * software must be clearly marked as such, and if the derived work is
                     10:  * incompatible with the protocol description in the RFC file, it must be
                     11:  * called by a name other than "ssh" or "Secure Shell".
1.26      deraadt    12:  */
1.1       deraadt    13:
1.152     stevesk    14: #include <sys/types.h>
                     15: #include <sys/socket.h>
1.179     djm        16: #include <sys/queue.h>
1.315     djm        17: #include <sys/sysctl.h>
1.154     stevesk    18:
1.213     djm        19: #include <netinet/in.h>
                     20: #include <netinet/ip.h>
1.315     djm        21: #include <net/route.h>
1.213     djm        22:
1.235     dtucker    23: #include <ctype.h>
1.154     stevesk    24: #include <netdb.h>
1.165     dtucker    25: #include <pwd.h>
1.162     stevesk    26: #include <stdio.h>
1.161     stevesk    27: #include <stdlib.h>
1.160     stevesk    28: #include <string.h>
1.164     deraadt    29: #include <signal.h>
1.155     stevesk    30: #include <unistd.h>
1.259     deraadt    31: #include <limits.h>
1.164     deraadt    32: #include <stdarg.h>
1.182     dtucker    33: #include <errno.h>
1.236     dtucker    34: #include <util.h>
1.1       deraadt    35:
1.164     deraadt    36: #include "xmalloc.h"
1.1       deraadt    37: #include "ssh.h"
1.62      markus     38: #include "log.h"
1.164     deraadt    39: #include "buffer.h"
1.251     millert    40: #include "misc.h"
1.1       deraadt    41: #include "servconf.h"
1.33      markus     42: #include "compat.h"
1.60      markus     43: #include "pathnames.h"
1.62      markus     44: #include "cipher.h"
1.164     deraadt    45: #include "key.h"
1.66      markus     46: #include "kex.h"
                     47: #include "mac.h"
1.153     dtucker    48: #include "match.h"
1.156     dtucker    49: #include "channels.h"
1.165     dtucker    50: #include "groupaccess.h"
1.226     dtucker    51: #include "canohost.h"
                     52: #include "packet.h"
1.232     djm        53: #include "hostfile.h"
                     54: #include "auth.h"
1.252     djm        55: #include "myproposal.h"
1.256     djm        56: #include "digest.h"
1.1       deraadt    57:
1.315     djm        58: static void add_listen_addr(ServerOptions *, const char *,
                     59:     const char *, int);
                     60: static void add_one_listen_addr(ServerOptions *, const char *,
                     61:     const char *, int);
1.29      markus     62:
1.102     provos     63: /* Use of privilege separation or not */
                     64: extern int use_privsep;
1.153     dtucker    65: extern Buffer cfg;
1.62      markus     66:
1.1       deraadt    67: /* Initializes the server options to their default values. */
                     68:
1.34      markus     69: void
1.25      markus     70: initialize_server_options(ServerOptions *options)
1.1       deraadt    71: {
1.25      markus     72:        memset(options, 0, sizeof(*options));
1.29      markus     73:        options->num_ports = 0;
                     74:        options->ports_from_cmdline = 0;
1.266     dtucker    75:        options->queued_listen_addrs = NULL;
                     76:        options->num_queued_listens = 0;
1.29      markus     77:        options->listen_addrs = NULL;
1.315     djm        78:        options->num_listen_addrs = 0;
1.138     djm        79:        options->address_family = -1;
1.316     djm        80:        options->routing_domain = NULL;
1.54      markus     81:        options->num_host_key_files = 0;
1.203     djm        82:        options->num_host_cert_files = 0;
1.240     markus     83:        options->host_key_agent = NULL;
1.36      markus     84:        options->pid_file = NULL;
1.25      markus     85:        options->login_grace_time = -1;
1.279     chris      86:        options->permit_root_login = PERMIT_NOT_SET;
1.25      markus     87:        options->ignore_rhosts = -1;
                     88:        options->ignore_user_known_hosts = -1;
                     89:        options->print_motd = -1;
1.72      stevesk    90:        options->print_lastlog = -1;
1.25      markus     91:        options->x11_forwarding = -1;
                     92:        options->x11_display_offset = -1;
1.99      stevesk    93:        options->x11_use_localhost = -1;
1.244     djm        94:        options->permit_tty = -1;
1.250     djm        95:        options->permit_user_rc = -1;
1.42      markus     96:        options->xauth_location = NULL;
1.25      markus     97:        options->strict_modes = -1;
1.129     markus     98:        options->tcp_keep_alive = -1;
1.101     markus     99:        options->log_facility = SYSLOG_FACILITY_NOT_SET;
                    100:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.75      markus    101:        options->hostbased_authentication = -1;
                    102:        options->hostbased_uses_name_from_packet_only = -1;
1.258     djm       103:        options->hostbased_key_types = NULL;
1.276     markus    104:        options->hostkeyalgorithms = NULL;
1.54      markus    105:        options->pubkey_authentication = -1;
1.258     djm       106:        options->pubkey_key_types = NULL;
1.25      markus    107:        options->kerberos_authentication = -1;
                    108:        options->kerberos_or_local_passwd = -1;
                    109:        options->kerberos_ticket_cleanup = -1;
1.130     jakob     110:        options->kerberos_get_afs_token = -1;
1.125     markus    111:        options->gss_authentication=-1;
                    112:        options->gss_cleanup_creds = -1;
1.271     djm       113:        options->gss_strict_acceptor = -1;
1.25      markus    114:        options->password_authentication = -1;
1.52      markus    115:        options->kbd_interactive_authentication = -1;
1.80      markus    116:        options->challenge_response_authentication = -1;
1.25      markus    117:        options->permit_empty_passwd = -1;
1.113     markus    118:        options->permit_user_env = -1;
1.334   ! djm       119:        options->permit_user_env_whitelist = NULL;
1.111     markus    120:        options->compression = -1;
1.235     dtucker   121:        options->rekey_limit = -1;
                    122:        options->rekey_interval = -1;
1.53      markus    123:        options->allow_tcp_forwarding = -1;
1.251     millert   124:        options->allow_streamlocal_forwarding = -1;
1.178     pyr       125:        options->allow_agent_forwarding = -1;
1.25      markus    126:        options->num_allow_users = 0;
                    127:        options->num_deny_users = 0;
                    128:        options->num_allow_groups = 0;
                    129:        options->num_deny_groups = 0;
1.33      markus    130:        options->ciphers = NULL;
1.66      markus    131:        options->macs = NULL;
1.211     djm       132:        options->kex_algorithms = NULL;
1.251     millert   133:        options->fwd_opts.gateway_ports = -1;
                    134:        options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
                    135:        options->fwd_opts.streamlocal_bind_unlink = -1;
1.43      jakob     136:        options->num_subsystems = 0;
1.50      markus    137:        options->max_startups_begin = -1;
                    138:        options->max_startups_rate = -1;
1.46      markus    139:        options->max_startups = -1;
1.133     dtucker   140:        options->max_authtries = -1;
1.180     djm       141:        options->max_sessions = -1;
1.57      markus    142:        options->banner = NULL;
1.122     markus    143:        options->use_dns = -1;
1.77      beck      144:        options->client_alive_interval = -1;
                    145:        options->client_alive_count_max = -1;
1.219     djm       146:        options->num_authkeys_files = 0;
1.131     djm       147:        options->num_accept_env = 0;
1.332     djm       148:        options->num_setenv = 0;
1.145     reyk      149:        options->permit_tun = -1;
1.310     djm       150:        options->permitted_opens = NULL;
1.330     djm       151:        options->permitted_listens = NULL;
1.158     dtucker   152:        options->adm_forced_command = NULL;
1.176     djm       153:        options->chroot_directory = NULL;
1.231     djm       154:        options->authorized_keys_command = NULL;
                    155:        options->authorized_keys_command_user = NULL;
1.204     djm       156:        options->revoked_keys_file = NULL;
                    157:        options->trusted_user_ca_keys = NULL;
1.208     djm       158:        options->authorized_principals_file = NULL;
1.270     djm       159:        options->authorized_principals_command = NULL;
                    160:        options->authorized_principals_command_user = NULL;
1.213     djm       161:        options->ip_qos_interactive = -1;
                    162:        options->ip_qos_bulk = -1;
1.225     djm       163:        options->version_addendum = NULL;
1.256     djm       164:        options->fingerprint_hash = -1;
1.301     djm       165:        options->disable_forwarding = -1;
1.309     djm       166:        options->expose_userauth_info = -1;
1.1       deraadt   167: }
                    168:
1.257     djm       169: /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
                    170: static int
                    171: option_clear_or_none(const char *o)
                    172: {
                    173:        return o == NULL || strcasecmp(o, "none") == 0;
                    174: }
                    175:
1.282     djm       176: static void
                    177: assemble_algorithms(ServerOptions *o)
                    178: {
                    179:        if (kex_assemble_names(KEX_SERVER_ENCRYPT, &o->ciphers) != 0 ||
                    180:            kex_assemble_names(KEX_SERVER_MAC, &o->macs) != 0 ||
                    181:            kex_assemble_names(KEX_SERVER_KEX, &o->kex_algorithms) != 0 ||
                    182:            kex_assemble_names(KEX_DEFAULT_PK_ALG,
                    183:            &o->hostkeyalgorithms) != 0 ||
                    184:            kex_assemble_names(KEX_DEFAULT_PK_ALG,
                    185:            &o->hostbased_key_types) != 0 ||
                    186:            kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->pubkey_key_types) != 0)
                    187:                fatal("kex_assemble_names failed");
                    188: }
                    189:
1.314     djm       190: static void
                    191: array_append(const char *file, const int line, const char *directive,
                    192:     char ***array, u_int *lp, const char *s)
                    193: {
                    194:
                    195:        if (*lp >= INT_MAX)
                    196:                fatal("%s line %d: Too many %s entries", file, line, directive);
                    197:
                    198:        *array = xrecallocarray(*array, *lp, *lp + 1, sizeof(**array));
                    199:        (*array)[*lp] = xstrdup(s);
                    200:        (*lp)++;
                    201: }
                    202:
                    203: void
                    204: servconf_add_hostkey(const char *file, const int line,
                    205:     ServerOptions *options, const char *path)
                    206: {
                    207:        char *apath = derelativise_path(path);
                    208:
                    209:        array_append(file, line, "HostKey",
                    210:            &options->host_key_files, &options->num_host_key_files, apath);
                    211:        free(apath);
                    212: }
                    213:
                    214: void
                    215: servconf_add_hostcert(const char *file, const int line,
                    216:     ServerOptions *options, const char *path)
                    217: {
                    218:        char *apath = derelativise_path(path);
                    219:
                    220:        array_append(file, line, "HostCertificate",
                    221:            &options->host_cert_files, &options->num_host_cert_files, apath);
                    222:        free(apath);
                    223: }
                    224:
1.34      markus    225: void
1.25      markus    226: fill_default_server_options(ServerOptions *options)
1.1       deraadt   227: {
1.314     djm       228:        u_int i;
1.257     djm       229:
1.54      markus    230:        if (options->num_host_key_files == 0) {
1.293     naddy     231:                /* fill default hostkeys */
1.314     djm       232:                servconf_add_hostkey("[default]", 0, options,
                    233:                    _PATH_HOST_RSA_KEY_FILE);
                    234:                servconf_add_hostkey("[default]", 0, options,
                    235:                    _PATH_HOST_ECDSA_KEY_FILE);
                    236:                servconf_add_hostkey("[default]", 0, options,
                    237:                    _PATH_HOST_ED25519_KEY_FILE);
1.326     markus    238: #ifdef WITH_XMSS
1.325     markus    239:                servconf_add_hostkey("[default]", 0, options,
                    240:                    _PATH_HOST_XMSS_KEY_FILE);
1.326     markus    241: #endif /* WITH_XMSS */
1.54      markus    242:        }
1.203     djm       243:        /* No certificates by default */
1.29      markus    244:        if (options->num_ports == 0)
                    245:                options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
1.266     dtucker   246:        if (options->address_family == -1)
                    247:                options->address_family = AF_UNSPEC;
1.29      markus    248:        if (options->listen_addrs == NULL)
1.315     djm       249:                add_listen_addr(options, NULL, NULL, 0);
1.36      markus    250:        if (options->pid_file == NULL)
1.257     djm       251:                options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
1.25      markus    252:        if (options->login_grace_time == -1)
1.115     stevesk   253:                options->login_grace_time = 120;
1.67      markus    254:        if (options->permit_root_login == PERMIT_NOT_SET)
1.279     chris     255:                options->permit_root_login = PERMIT_NO_PASSWD;
1.25      markus    256:        if (options->ignore_rhosts == -1)
1.30      markus    257:                options->ignore_rhosts = 1;
1.25      markus    258:        if (options->ignore_user_known_hosts == -1)
                    259:                options->ignore_user_known_hosts = 0;
                    260:        if (options->print_motd == -1)
                    261:                options->print_motd = 1;
1.72      stevesk   262:        if (options->print_lastlog == -1)
                    263:                options->print_lastlog = 1;
1.25      markus    264:        if (options->x11_forwarding == -1)
1.30      markus    265:                options->x11_forwarding = 0;
1.25      markus    266:        if (options->x11_display_offset == -1)
1.30      markus    267:                options->x11_display_offset = 10;
1.99      stevesk   268:        if (options->x11_use_localhost == -1)
                    269:                options->x11_use_localhost = 1;
1.42      markus    270:        if (options->xauth_location == NULL)
1.257     djm       271:                options->xauth_location = xstrdup(_PATH_XAUTH);
1.244     djm       272:        if (options->permit_tty == -1)
                    273:                options->permit_tty = 1;
1.250     djm       274:        if (options->permit_user_rc == -1)
                    275:                options->permit_user_rc = 1;
1.25      markus    276:        if (options->strict_modes == -1)
                    277:                options->strict_modes = 1;
1.129     markus    278:        if (options->tcp_keep_alive == -1)
                    279:                options->tcp_keep_alive = 1;
1.101     markus    280:        if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
1.25      markus    281:                options->log_facility = SYSLOG_FACILITY_AUTH;
1.101     markus    282:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.58      markus    283:                options->log_level = SYSLOG_LEVEL_INFO;
1.75      markus    284:        if (options->hostbased_authentication == -1)
                    285:                options->hostbased_authentication = 0;
                    286:        if (options->hostbased_uses_name_from_packet_only == -1)
                    287:                options->hostbased_uses_name_from_packet_only = 0;
1.54      markus    288:        if (options->pubkey_authentication == -1)
                    289:                options->pubkey_authentication = 1;
1.25      markus    290:        if (options->kerberos_authentication == -1)
1.107     markus    291:                options->kerberos_authentication = 0;
1.25      markus    292:        if (options->kerberos_or_local_passwd == -1)
                    293:                options->kerberos_or_local_passwd = 1;
                    294:        if (options->kerberos_ticket_cleanup == -1)
                    295:                options->kerberos_ticket_cleanup = 1;
1.130     jakob     296:        if (options->kerberos_get_afs_token == -1)
                    297:                options->kerberos_get_afs_token = 0;
1.125     markus    298:        if (options->gss_authentication == -1)
                    299:                options->gss_authentication = 0;
                    300:        if (options->gss_cleanup_creds == -1)
                    301:                options->gss_cleanup_creds = 1;
1.271     djm       302:        if (options->gss_strict_acceptor == -1)
1.302     djm       303:                options->gss_strict_acceptor = 1;
1.25      markus    304:        if (options->password_authentication == -1)
                    305:                options->password_authentication = 1;
1.52      markus    306:        if (options->kbd_interactive_authentication == -1)
                    307:                options->kbd_interactive_authentication = 0;
1.80      markus    308:        if (options->challenge_response_authentication == -1)
                    309:                options->challenge_response_authentication = 1;
1.25      markus    310:        if (options->permit_empty_passwd == -1)
1.30      markus    311:                options->permit_empty_passwd = 0;
1.334   ! djm       312:        if (options->permit_user_env == -1) {
1.113     markus    313:                options->permit_user_env = 0;
1.334   ! djm       314:                options->permit_user_env_whitelist = NULL;
        !           315:        }
1.111     markus    316:        if (options->compression == -1)
1.143     markus    317:                options->compression = COMP_DELAYED;
1.235     dtucker   318:        if (options->rekey_limit == -1)
                    319:                options->rekey_limit = 0;
                    320:        if (options->rekey_interval == -1)
                    321:                options->rekey_interval = 0;
1.53      markus    322:        if (options->allow_tcp_forwarding == -1)
1.233     djm       323:                options->allow_tcp_forwarding = FORWARD_ALLOW;
1.251     millert   324:        if (options->allow_streamlocal_forwarding == -1)
                    325:                options->allow_streamlocal_forwarding = FORWARD_ALLOW;
1.178     pyr       326:        if (options->allow_agent_forwarding == -1)
                    327:                options->allow_agent_forwarding = 1;
1.251     millert   328:        if (options->fwd_opts.gateway_ports == -1)
                    329:                options->fwd_opts.gateway_ports = 0;
1.46      markus    330:        if (options->max_startups == -1)
1.234     dtucker   331:                options->max_startups = 100;
1.50      markus    332:        if (options->max_startups_rate == -1)
1.234     dtucker   333:                options->max_startups_rate = 30;                /* 30% */
1.50      markus    334:        if (options->max_startups_begin == -1)
1.234     dtucker   335:                options->max_startups_begin = 10;
1.133     dtucker   336:        if (options->max_authtries == -1)
                    337:                options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
1.180     djm       338:        if (options->max_sessions == -1)
                    339:                options->max_sessions = DEFAULT_SESSIONS_MAX;
1.122     markus    340:        if (options->use_dns == -1)
1.260     deraadt   341:                options->use_dns = 0;
1.77      beck      342:        if (options->client_alive_interval == -1)
1.95      deraadt   343:                options->client_alive_interval = 0;
1.77      beck      344:        if (options->client_alive_count_max == -1)
                    345:                options->client_alive_count_max = 3;
1.219     djm       346:        if (options->num_authkeys_files == 0) {
1.314     djm       347:                array_append("[default]", 0, "AuthorizedKeysFiles",
                    348:                    &options->authorized_keys_files,
                    349:                    &options->num_authkeys_files,
                    350:                    _PATH_SSH_USER_PERMITTED_KEYS);
                    351:                array_append("[default]", 0, "AuthorizedKeysFiles",
                    352:                    &options->authorized_keys_files,
                    353:                    &options->num_authkeys_files,
                    354:                    _PATH_SSH_USER_PERMITTED_KEYS2);
1.219     djm       355:        }
1.145     reyk      356:        if (options->permit_tun == -1)
1.146     reyk      357:                options->permit_tun = SSH_TUNMODE_NO;
1.213     djm       358:        if (options->ip_qos_interactive == -1)
1.327     job       359:                options->ip_qos_interactive = IPTOS_DSCP_AF21;
1.213     djm       360:        if (options->ip_qos_bulk == -1)
1.327     job       361:                options->ip_qos_bulk = IPTOS_DSCP_CS1;
1.225     djm       362:        if (options->version_addendum == NULL)
                    363:                options->version_addendum = xstrdup("");
1.251     millert   364:        if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
                    365:                options->fwd_opts.streamlocal_bind_mask = 0177;
                    366:        if (options->fwd_opts.streamlocal_bind_unlink == -1)
                    367:                options->fwd_opts.streamlocal_bind_unlink = 0;
1.256     djm       368:        if (options->fingerprint_hash == -1)
                    369:                options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1.301     djm       370:        if (options->disable_forwarding == -1)
                    371:                options->disable_forwarding = 0;
1.309     djm       372:        if (options->expose_userauth_info == -1)
                    373:                options->expose_userauth_info = 0;
1.277     djm       374:
1.282     djm       375:        assemble_algorithms(options);
1.277     djm       376:
1.285     djm       377:        /* Turn privilege separation and sandboxing on by default */
1.102     provos    378:        if (use_privsep == -1)
1.285     djm       379:                use_privsep = PRIVSEP_ON;
1.257     djm       380:
                    381: #define CLEAR_ON_NONE(v) \
                    382:        do { \
                    383:                if (option_clear_or_none(v)) { \
                    384:                        free(v); \
                    385:                        v = NULL; \
                    386:                } \
                    387:        } while(0)
                    388:        CLEAR_ON_NONE(options->pid_file);
                    389:        CLEAR_ON_NONE(options->xauth_location);
                    390:        CLEAR_ON_NONE(options->banner);
                    391:        CLEAR_ON_NONE(options->trusted_user_ca_keys);
                    392:        CLEAR_ON_NONE(options->revoked_keys_file);
1.267     djm       393:        CLEAR_ON_NONE(options->authorized_principals_file);
1.283     djm       394:        CLEAR_ON_NONE(options->adm_forced_command);
                    395:        CLEAR_ON_NONE(options->chroot_directory);
1.316     djm       396:        CLEAR_ON_NONE(options->routing_domain);
1.257     djm       397:        for (i = 0; i < options->num_host_key_files; i++)
                    398:                CLEAR_ON_NONE(options->host_key_files[i]);
                    399:        for (i = 0; i < options->num_host_cert_files; i++)
                    400:                CLEAR_ON_NONE(options->host_cert_files[i]);
                    401: #undef CLEAR_ON_NONE
1.291     djm       402:
                    403:        /* Similar handling for AuthenticationMethods=any */
                    404:        if (options->num_auth_methods == 1 &&
                    405:            strcmp(options->auth_methods[0], "any") == 0) {
                    406:                free(options->auth_methods[0]);
                    407:                options->auth_methods[0] = NULL;
                    408:                options->num_auth_methods = 0;
                    409:        }
                    410:
1.1       deraadt   411: }
                    412:
                    413: /* Keyword tokens. */
1.25      markus    414: typedef enum {
                    415:        sBadOption,             /* == unknown option */
1.293     naddy     416:        sPort, sHostKeyFile, sLoginGraceTime,
                    417:        sPermitRootLogin, sLogFacility, sLogLevel,
1.25      markus    418:        sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
1.323     dtucker   419:        sKerberosGetAFSToken, sChallengeResponseAuthentication,
1.138     djm       420:        sPasswordAuthentication, sKbdInteractiveAuthentication,
                    421:        sListenAddress, sAddressFamily,
1.72      stevesk   422:        sPrintMotd, sPrintLastLog, sIgnoreRhosts,
1.99      stevesk   423:        sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
1.244     djm       424:        sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
1.294     djm       425:        sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
1.235     dtucker   426:        sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
1.293     naddy     427:        sIgnoreUserKnownHosts, sCiphers, sMacs, sPidFile,
1.258     djm       428:        sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
                    429:        sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
1.122     markus    430:        sBanner, sUseDNS, sHostbasedAuthentication,
1.258     djm       431:        sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
1.276     markus    432:        sHostKeyAlgorithms,
1.258     djm       433:        sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
1.271     djm       434:        sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
1.332     djm       435:        sAcceptEnv, sSetEnv, sPermitTunnel,
1.330     djm       436:        sMatch, sPermitOpen, sPermitListen, sForceCommand, sChrootDirectory,
1.200     dtucker   437:        sUsePrivilegeSeparation, sAllowAgentForwarding,
1.249     djm       438:        sHostCertificate,
1.208     djm       439:        sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
1.270     djm       440:        sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
1.225     djm       441:        sKexAlgorithms, sIPQoS, sVersionAddendum,
1.231     djm       442:        sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
1.250     djm       443:        sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
1.251     millert   444:        sStreamLocalBindMask, sStreamLocalBindUnlink,
1.301     djm       445:        sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
1.316     djm       446:        sExposeAuthInfo, sRDomain,
1.295     djm       447:        sDeprecated, sIgnore, sUnsupported
1.1       deraadt   448: } ServerOpCodes;
                    449:
1.153     dtucker   450: #define SSHCFG_GLOBAL  0x01    /* allowed in main section of sshd_config */
                    451: #define SSHCFG_MATCH   0x02    /* allowed inside a Match section */
                    452: #define SSHCFG_ALL     (SSHCFG_GLOBAL|SSHCFG_MATCH)
                    453:
1.1       deraadt   454: /* Textual representation of the tokens. */
1.25      markus    455: static struct {
                    456:        const char *name;
                    457:        ServerOpCodes opcode;
1.153     dtucker   458:        u_int flags;
1.25      markus    459: } keywords[] = {
1.153     dtucker   460:        { "port", sPort, SSHCFG_GLOBAL },
                    461:        { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
                    462:        { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL },          /* alias */
1.240     markus    463:        { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
1.153     dtucker   464:        { "pidfile", sPidFile, SSHCFG_GLOBAL },
1.293     naddy     465:        { "serverkeybits", sDeprecated, SSHCFG_GLOBAL },
1.153     dtucker   466:        { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
1.293     naddy     467:        { "keyregenerationinterval", sDeprecated, SSHCFG_GLOBAL },
1.175     dtucker   468:        { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
1.153     dtucker   469:        { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
1.308     djm       470:        { "loglevel", sLogLevel, SSHCFG_ALL },
1.153     dtucker   471:        { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
1.293     naddy     472:        { "rhostsrsaauthentication", sDeprecated, SSHCFG_ALL },
1.168     dtucker   473:        { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
1.209     djm       474:        { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
1.258     djm       475:        { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
1.276     markus    476:        { "hostkeyalgorithms", sHostKeyAlgorithms, SSHCFG_GLOBAL },
1.293     naddy     477:        { "rsaauthentication", sDeprecated, SSHCFG_ALL },
1.168     dtucker   478:        { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
1.258     djm       479:        { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
1.153     dtucker   480:        { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
1.123     markus    481: #ifdef KRB5
1.168     dtucker   482:        { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
1.153     dtucker   483:        { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
                    484:        { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
                    485:        { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
1.121     jakob     486: #else
1.168     dtucker   487:        { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
1.153     dtucker   488:        { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
                    489:        { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
                    490:        { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
1.126     markus    491: #endif
1.153     dtucker   492:        { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
                    493:        { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
1.125     markus    494: #ifdef GSSAPI
1.168     dtucker   495:        { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
1.153     dtucker   496:        { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
1.271     djm       497:        { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
1.125     markus    498: #else
1.168     dtucker   499:        { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
1.153     dtucker   500:        { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
1.271     djm       501:        { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
1.125     markus    502: #endif
1.168     dtucker   503:        { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
                    504:        { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
1.170     dtucker   505:        { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
1.153     dtucker   506:        { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
                    507:        { "checkmail", sDeprecated, SSHCFG_GLOBAL },
                    508:        { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
                    509:        { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
                    510:        { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
                    511:        { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
                    512:        { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
                    513:        { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
1.157     dtucker   514:        { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
                    515:        { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
                    516:        { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
1.153     dtucker   517:        { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
                    518:        { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
1.188     djm       519:        { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
1.153     dtucker   520:        { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
1.294     djm       521:        { "uselogin", sDeprecated, SSHCFG_GLOBAL },
1.153     dtucker   522:        { "compression", sCompression, SSHCFG_GLOBAL },
1.235     dtucker   523:        { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
1.153     dtucker   524:        { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
                    525:        { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL },  /* obsolete alias */
                    526:        { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
1.178     pyr       527:        { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
1.227     markus    528:        { "allowusers", sAllowUsers, SSHCFG_ALL },
                    529:        { "denyusers", sDenyUsers, SSHCFG_ALL },
                    530:        { "allowgroups", sAllowGroups, SSHCFG_ALL },
                    531:        { "denygroups", sDenyGroups, SSHCFG_ALL },
1.153     dtucker   532:        { "ciphers", sCiphers, SSHCFG_GLOBAL },
                    533:        { "macs", sMacs, SSHCFG_GLOBAL },
1.295     djm       534:        { "protocol", sIgnore, SSHCFG_GLOBAL },
1.153     dtucker   535:        { "gatewayports", sGatewayPorts, SSHCFG_ALL },
                    536:        { "subsystem", sSubsystem, SSHCFG_GLOBAL },
                    537:        { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
1.184     dtucker   538:        { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
1.180     djm       539:        { "maxsessions", sMaxSessions, SSHCFG_ALL },
1.168     dtucker   540:        { "banner", sBanner, SSHCFG_ALL },
1.153     dtucker   541:        { "usedns", sUseDNS, SSHCFG_GLOBAL },
                    542:        { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
                    543:        { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
1.300     markus    544:        { "clientaliveinterval", sClientAliveInterval, SSHCFG_ALL },
                    545:        { "clientalivecountmax", sClientAliveCountMax, SSHCFG_ALL },
1.209     djm       546:        { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
1.219     djm       547:        { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
1.306     djm       548:        { "useprivilegeseparation", sDeprecated, SSHCFG_GLOBAL},
1.227     markus    549:        { "acceptenv", sAcceptEnv, SSHCFG_ALL },
1.332     djm       550:        { "setenv", sSetEnv, SSHCFG_ALL },
1.209     djm       551:        { "permittunnel", sPermitTunnel, SSHCFG_ALL },
1.244     djm       552:        { "permittty", sPermitTTY, SSHCFG_ALL },
1.250     djm       553:        { "permituserrc", sPermitUserRC, SSHCFG_ALL },
1.153     dtucker   554:        { "match", sMatch, SSHCFG_ALL },
1.156     dtucker   555:        { "permitopen", sPermitOpen, SSHCFG_ALL },
1.330     djm       556:        { "permitlisten", sPermitListen, SSHCFG_ALL },
1.158     dtucker   557:        { "forcecommand", sForceCommand, SSHCFG_ALL },
1.176     djm       558:        { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
1.203     djm       559:        { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
1.204     djm       560:        { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
                    561:        { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
1.209     djm       562:        { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
1.211     djm       563:        { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
1.213     djm       564:        { "ipqos", sIPQoS, SSHCFG_ALL },
1.231     djm       565:        { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
                    566:        { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
1.270     djm       567:        { "authorizedprincipalscommand", sAuthorizedPrincipalsCommand, SSHCFG_ALL },
                    568:        { "authorizedprincipalscommanduser", sAuthorizedPrincipalsCommandUser, SSHCFG_ALL },
1.225     djm       569:        { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
1.232     djm       570:        { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
1.251     millert   571:        { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
                    572:        { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
                    573:        { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
1.256     djm       574:        { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
1.301     djm       575:        { "disableforwarding", sDisableForwarding, SSHCFG_ALL },
1.309     djm       576:        { "exposeauthinfo", sExposeAuthInfo, SSHCFG_ALL },
1.316     djm       577:        { "rdomain", sRDomain, SSHCFG_ALL },
1.153     dtucker   578:        { NULL, sBadOption, 0 }
1.1       deraadt   579: };
                    580:
1.182     dtucker   581: static struct {
                    582:        int val;
                    583:        char *text;
                    584: } tunmode_desc[] = {
                    585:        { SSH_TUNMODE_NO, "no" },
                    586:        { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
                    587:        { SSH_TUNMODE_ETHERNET, "ethernet" },
                    588:        { SSH_TUNMODE_YES, "yes" },
                    589:        { -1, NULL }
                    590: };
                    591:
1.329     djm       592: /* Returns an opcode name from its number */
                    593:
                    594: static const char *
                    595: lookup_opcode_name(ServerOpCodes code)
                    596: {
                    597:        u_int i;
                    598:
                    599:        for (i = 0; keywords[i].name != NULL; i++)
                    600:                if (keywords[i].opcode == code)
                    601:                        return(keywords[i].name);
                    602:        return "UNKNOWN";
                    603: }
                    604:
                    605:
1.27      markus    606: /*
1.73      stevesk   607:  * Returns the number of the token pointed to by cp or sBadOption.
1.27      markus    608:  */
1.1       deraadt   609:
1.34      markus    610: static ServerOpCodes
1.25      markus    611: parse_token(const char *cp, const char *filename,
1.153     dtucker   612:            int linenum, u_int *flags)
1.1       deraadt   613: {
1.55      markus    614:        u_int i;
1.1       deraadt   615:
1.25      markus    616:        for (i = 0; keywords[i].name; i++)
1.153     dtucker   617:                if (strcasecmp(cp, keywords[i].name) == 0) {
                    618:                        *flags = keywords[i].flags;
1.25      markus    619:                        return keywords[i].opcode;
1.153     dtucker   620:                }
1.25      markus    621:
1.78      stevesk   622:        error("%s: line %d: Bad configuration option: %s",
                    623:            filename, linenum, cp);
1.25      markus    624:        return sBadOption;
1.1       deraadt   625: }
                    626:
1.202     djm       627: char *
                    628: derelativise_path(const char *path)
                    629: {
1.259     deraadt   630:        char *expanded, *ret, cwd[PATH_MAX];
1.202     djm       631:
1.257     djm       632:        if (strcasecmp(path, "none") == 0)
                    633:                return xstrdup("none");
1.202     djm       634:        expanded = tilde_expand_filename(path, getuid());
                    635:        if (*expanded == '/')
                    636:                return expanded;
1.207     djm       637:        if (getcwd(cwd, sizeof(cwd)) == NULL)
1.202     djm       638:                fatal("%s: getcwd: %s", __func__, strerror(errno));
                    639:        xasprintf(&ret, "%s/%s", cwd, expanded);
1.239     djm       640:        free(expanded);
1.202     djm       641:        return ret;
                    642: }
                    643:
1.84      itojun    644: static void
1.315     djm       645: add_listen_addr(ServerOptions *options, const char *addr,
                    646:     const char *rdomain, int port)
1.74      stevesk   647: {
1.142     djm       648:        u_int i;
1.74      stevesk   649:
1.315     djm       650:        if (port > 0)
                    651:                add_one_listen_addr(options, addr, rdomain, port);
                    652:        else {
                    653:                for (i = 0; i < options->num_ports; i++) {
                    654:                        add_one_listen_addr(options, addr, rdomain,
                    655:                            options->ports[i]);
                    656:                }
                    657:        }
1.74      stevesk   658: }
                    659:
1.84      itojun    660: static void
1.315     djm       661: add_one_listen_addr(ServerOptions *options, const char *addr,
                    662:     const char *rdomain, int port)
1.29      markus    663: {
                    664:        struct addrinfo hints, *ai, *aitop;
                    665:        char strport[NI_MAXSERV];
                    666:        int gaierr;
1.315     djm       667:        u_int i;
                    668:
                    669:        /* Find listen_addrs entry for this rdomain */
                    670:        for (i = 0; i < options->num_listen_addrs; i++) {
                    671:                if (rdomain == NULL && options->listen_addrs[i].rdomain == NULL)
                    672:                        break;
                    673:                if (rdomain == NULL || options->listen_addrs[i].rdomain == NULL)
                    674:                        continue;
                    675:                if (strcmp(rdomain, options->listen_addrs[i].rdomain) == 0)
                    676:                        break;
                    677:        }
                    678:        if (i >= options->num_listen_addrs) {
                    679:                /* No entry for this rdomain; allocate one */
                    680:                if (i >= INT_MAX)
                    681:                        fatal("%s: too many listen addresses", __func__);
                    682:                options->listen_addrs = xrecallocarray(options->listen_addrs,
                    683:                    options->num_listen_addrs, options->num_listen_addrs + 1,
                    684:                    sizeof(*options->listen_addrs));
                    685:                i = options->num_listen_addrs++;
                    686:                if (rdomain != NULL)
                    687:                        options->listen_addrs[i].rdomain = xstrdup(rdomain);
                    688:        }
                    689:        /* options->listen_addrs[i] points to the addresses for this rdomain */
1.29      markus    690:
1.74      stevesk   691:        memset(&hints, 0, sizeof(hints));
1.138     djm       692:        hints.ai_family = options->address_family;
1.74      stevesk   693:        hints.ai_socktype = SOCK_STREAM;
                    694:        hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
1.194     djm       695:        snprintf(strport, sizeof strport, "%d", port);
1.74      stevesk   696:        if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
                    697:                fatal("bad addr or host: %s (%s)",
                    698:                    addr ? addr : "<NULL>",
1.173     dtucker   699:                    ssh_gai_strerror(gaierr));
1.74      stevesk   700:        for (ai = aitop; ai->ai_next; ai = ai->ai_next)
                    701:                ;
1.315     djm       702:        ai->ai_next = options->listen_addrs[i].addrs;
                    703:        options->listen_addrs[i].addrs = aitop;
                    704: }
                    705:
                    706: /* Returns nonzero if the routing domain name is valid */
                    707: static int
                    708: valid_rdomain(const char *name)
                    709: {
                    710:        const char *errstr;
                    711:        long long num;
                    712:        struct rt_tableinfo info;
                    713:        int mib[6];
                    714:        size_t miblen = sizeof(mib);
                    715:
                    716:        if (name == NULL)
                    717:                return 1;
                    718:
                    719:        num = strtonum(name, 0, 255, &errstr);
                    720:        if (errstr != NULL)
                    721:                return 0;
                    722:
                    723:        /* Check whether the table actually exists */
                    724:        memset(mib, 0, sizeof(mib));
                    725:        mib[0] = CTL_NET;
                    726:        mib[1] = PF_ROUTE;
                    727:        mib[4] = NET_RT_TABLE;
                    728:        mib[5] = (int)num;
                    729:        if (sysctl(mib, 6, &info, &miblen, NULL, 0) == -1)
                    730:                return 0;
                    731:
                    732:        return 1;
1.29      markus    733: }
                    734:
1.266     dtucker   735: /*
                    736:  * Queue a ListenAddress to be processed once we have all of the Ports
                    737:  * and AddressFamily options.
                    738:  */
                    739: static void
1.315     djm       740: queue_listen_addr(ServerOptions *options, const char *addr,
                    741:     const char *rdomain, int port)
1.266     dtucker   742: {
1.315     djm       743:        struct queued_listenaddr *qla;
                    744:
                    745:        options->queued_listen_addrs = xrecallocarray(
                    746:            options->queued_listen_addrs,
                    747:            options->num_queued_listens, options->num_queued_listens + 1,
                    748:            sizeof(*options->queued_listen_addrs));
                    749:        qla = &options->queued_listen_addrs[options->num_queued_listens++];
                    750:        qla->addr = xstrdup(addr);
                    751:        qla->port = port;
                    752:        qla->rdomain = rdomain == NULL ? NULL : xstrdup(rdomain);
1.266     dtucker   753: }
                    754:
                    755: /*
                    756:  * Process queued (text) ListenAddress entries.
                    757:  */
                    758: static void
                    759: process_queued_listen_addrs(ServerOptions *options)
                    760: {
                    761:        u_int i;
1.315     djm       762:        struct queued_listenaddr *qla;
1.266     dtucker   763:
                    764:        if (options->num_ports == 0)
                    765:                options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
                    766:        if (options->address_family == -1)
                    767:                options->address_family = AF_UNSPEC;
                    768:
                    769:        for (i = 0; i < options->num_queued_listens; i++) {
1.315     djm       770:                qla = &options->queued_listen_addrs[i];
                    771:                add_listen_addr(options, qla->addr, qla->rdomain, qla->port);
                    772:                free(qla->addr);
                    773:                free(qla->rdomain);
1.266     dtucker   774:        }
                    775:        free(options->queued_listen_addrs);
                    776:        options->queued_listen_addrs = NULL;
                    777:        options->num_queued_listens = 0;
                    778: }
                    779:
1.310     djm       780: /*
1.329     djm       781:  * Inform channels layer of permitopen options for a single forwarding
                    782:  * direction (local/remote).
1.310     djm       783:  */
1.329     djm       784: static void
                    785: process_permitopen_list(struct ssh *ssh, ServerOpCodes opcode,
                    786:     char **opens, u_int num_opens)
1.310     djm       787: {
                    788:        u_int i;
                    789:        int port;
                    790:        char *host, *arg, *oarg;
1.329     djm       791:        int where = opcode == sPermitOpen ? FORWARD_LOCAL : FORWARD_REMOTE;
                    792:        const char *what = lookup_opcode_name(opcode);
1.310     djm       793:
1.329     djm       794:        channel_clear_permission(ssh, FORWARD_ADM, where);
                    795:        if (num_opens == 0)
1.310     djm       796:                return; /* permit any */
                    797:
                    798:        /* handle keywords: "any" / "none" */
1.329     djm       799:        if (num_opens == 1 && strcmp(opens[0], "any") == 0)
1.310     djm       800:                return;
1.329     djm       801:        if (num_opens == 1 && strcmp(opens[0], "none") == 0) {
                    802:                channel_disable_admin(ssh, where);
1.310     djm       803:                return;
                    804:        }
                    805:        /* Otherwise treat it as a list of permitted host:port */
1.329     djm       806:        for (i = 0; i < num_opens; i++) {
                    807:                oarg = arg = xstrdup(opens[i]);
1.310     djm       808:                host = hpdelim(&arg);
                    809:                if (host == NULL)
1.329     djm       810:                        fatal("%s: missing host in %s", __func__, what);
1.310     djm       811:                host = cleanhostname(host);
                    812:                if (arg == NULL || ((port = permitopen_port(arg)) < 0))
1.329     djm       813:                        fatal("%s: bad port number in %s", __func__, what);
1.310     djm       814:                /* Send it to channels layer */
1.329     djm       815:                channel_add_permission(ssh, FORWARD_ADM,
                    816:                    where, host, port);
1.310     djm       817:                free(oarg);
                    818:        }
                    819: }
                    820:
1.329     djm       821: /*
                    822:  * Inform channels layer of permitopen options from configuration.
                    823:  */
                    824: void
                    825: process_permitopen(struct ssh *ssh, ServerOptions *options)
                    826: {
                    827:        process_permitopen_list(ssh, sPermitOpen,
                    828:            options->permitted_opens, options->num_permitted_opens);
1.330     djm       829:        process_permitopen_list(ssh, sPermitListen,
                    830:            options->permitted_listens,
                    831:            options->num_permitted_listens);
1.329     djm       832: }
                    833:
1.226     dtucker   834: struct connection_info *
                    835: get_connection_info(int populate, int use_dns)
                    836: {
1.286     djm       837:        struct ssh *ssh = active_state; /* XXX */
1.226     dtucker   838:        static struct connection_info ci;
                    839:
                    840:        if (!populate)
                    841:                return &ci;
1.286     djm       842:        ci.host = auth_get_canonical_hostname(ssh, use_dns);
                    843:        ci.address = ssh_remote_ipaddr(ssh);
                    844:        ci.laddress = ssh_local_ipaddr(ssh);
                    845:        ci.lport = ssh_local_port(ssh);
1.317     djm       846:        ci.rdomain = ssh_packet_rdomain_in(ssh);
1.226     dtucker   847:        return &ci;
                    848: }
                    849:
1.153     dtucker   850: /*
                    851:  * The strategy for the Match blocks is that the config file is parsed twice.
                    852:  *
                    853:  * The first time is at startup.  activep is initialized to 1 and the
                    854:  * directives in the global context are processed and acted on.  Hitting a
                    855:  * Match directive unsets activep and the directives inside the block are
                    856:  * checked for syntax only.
                    857:  *
                    858:  * The second time is after a connection has been established but before
                    859:  * authentication.  activep is initialized to 2 and global config directives
                    860:  * are ignored since they have already been processed.  If the criteria in a
                    861:  * Match block is met, activep is set and the subsequent directives
                    862:  * processed and actioned until EOF or another Match block unsets it.  Any
                    863:  * options set are copied into the main server config.
                    864:  *
                    865:  * Potential additions/improvements:
1.295     djm       866:  *  - Add Match support for pre-kex directives, eg. Ciphers.
1.153     dtucker   867:  *
                    868:  *  - Add a Tag directive (idea from David Leonard) ala pf, eg:
                    869:  *     Match Address 192.168.0.*
                    870:  *             Tag trusted
                    871:  *     Match Group wheel
                    872:  *             Tag trusted
                    873:  *     Match Tag trusted
                    874:  *             AllowTcpForwarding yes
                    875:  *             GatewayPorts clientspecified
                    876:  *             [...]
                    877:  *
                    878:  *  - Add a PermittedChannelRequests directive
                    879:  *     Match Group shell
                    880:  *             PermittedChannelRequests session,forwarded-tcpip
                    881:  */
                    882:
                    883: static int
1.165     dtucker   884: match_cfg_line_group(const char *grps, int line, const char *user)
                    885: {
                    886:        int result = 0;
                    887:        struct passwd *pw;
                    888:
                    889:        if (user == NULL)
                    890:                goto out;
                    891:
                    892:        if ((pw = getpwnam(user)) == NULL) {
                    893:                debug("Can't match group at line %d because user %.100s does "
                    894:                    "not exist", line, user);
                    895:        } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
                    896:                debug("Can't Match group because user %.100s not in any group "
                    897:                    "at line %d", user, line);
1.186     djm       898:        } else if (ga_match_pattern_list(grps) != 1) {
                    899:                debug("user %.100s does not match group list %.100s at line %d",
                    900:                    user, grps, line);
1.165     dtucker   901:        } else {
1.186     djm       902:                debug("user %.100s matched group list %.100s at line %d", user,
                    903:                    grps, line);
1.165     dtucker   904:                result = 1;
                    905:        }
                    906: out:
                    907:        ga_free();
                    908:        return result;
                    909: }
                    910:
1.319     dtucker   911: static void
                    912: match_test_missing_fatal(const char *criteria, const char *attrib)
                    913: {
                    914:        fatal("'Match %s' in configuration but '%s' not in connection "
                    915:            "test specification.", criteria, attrib);
                    916: }
                    917:
1.226     dtucker   918: /*
1.230     dtucker   919:  * All of the attributes on a single Match line are ANDed together, so we need
1.242     dtucker   920:  * to check every attribute and set the result to zero if any attribute does
1.230     dtucker   921:  * not match.
1.226     dtucker   922:  */
1.165     dtucker   923: static int
1.226     dtucker   924: match_cfg_line(char **condition, int line, struct connection_info *ci)
1.153     dtucker   925: {
1.243     dtucker   926:        int result = 1, attributes = 0, port;
1.153     dtucker   927:        char *arg, *attrib, *cp = *condition;
                    928:
1.226     dtucker   929:        if (ci == NULL)
1.153     dtucker   930:                debug3("checking syntax for 'Match %s'", cp);
                    931:        else
1.226     dtucker   932:                debug3("checking match for '%s' user %s host %s addr %s "
                    933:                    "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
                    934:                    ci->host ? ci->host : "(null)",
                    935:                    ci->address ? ci->address : "(null)",
                    936:                    ci->laddress ? ci->laddress : "(null)", ci->lport);
1.153     dtucker   937:
                    938:        while ((attrib = strdelim(&cp)) && *attrib != '\0') {
1.243     dtucker   939:                attributes++;
                    940:                if (strcasecmp(attrib, "all") == 0) {
                    941:                        if (attributes != 1 ||
                    942:                            ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
                    943:                                error("'all' cannot be combined with other "
                    944:                                    "Match attributes");
                    945:                                return -1;
                    946:                        }
                    947:                        *condition = cp;
                    948:                        return 1;
                    949:                }
1.153     dtucker   950:                if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
                    951:                        error("Missing Match criteria for %s", attrib);
                    952:                        return -1;
                    953:                }
                    954:                if (strcasecmp(attrib, "user") == 0) {
1.319     dtucker   955:                        if (ci == NULL) {
1.153     dtucker   956:                                result = 0;
                    957:                                continue;
                    958:                        }
1.319     dtucker   959:                        if (ci->user == NULL)
                    960:                                match_test_missing_fatal("User", "user");
1.269     djm       961:                        if (match_pattern_list(ci->user, arg, 0) != 1)
1.153     dtucker   962:                                result = 0;
                    963:                        else
                    964:                                debug("user %.100s matched 'User %.100s' at "
1.226     dtucker   965:                                    "line %d", ci->user, arg, line);
1.165     dtucker   966:                } else if (strcasecmp(attrib, "group") == 0) {
1.319     dtucker   967:                        if (ci == NULL) {
1.226     dtucker   968:                                result = 0;
                    969:                                continue;
                    970:                        }
1.319     dtucker   971:                        if (ci->user == NULL)
                    972:                                match_test_missing_fatal("Group", "user");
1.226     dtucker   973:                        switch (match_cfg_line_group(arg, line, ci->user)) {
1.165     dtucker   974:                        case -1:
                    975:                                return -1;
                    976:                        case 0:
                    977:                                result = 0;
                    978:                        }
1.153     dtucker   979:                } else if (strcasecmp(attrib, "host") == 0) {
1.319     dtucker   980:                        if (ci == NULL) {
1.153     dtucker   981:                                result = 0;
                    982:                                continue;
                    983:                        }
1.319     dtucker   984:                        if (ci->host == NULL)
                    985:                                match_test_missing_fatal("Host", "host");
1.269     djm       986:                        if (match_hostname(ci->host, arg) != 1)
1.153     dtucker   987:                                result = 0;
                    988:                        else
                    989:                                debug("connection from %.100s matched 'Host "
1.226     dtucker   990:                                    "%.100s' at line %d", ci->host, arg, line);
1.153     dtucker   991:                } else if (strcasecmp(attrib, "address") == 0) {
1.319     dtucker   992:                        if (ci == NULL) {
1.226     dtucker   993:                                result = 0;
                    994:                                continue;
                    995:                        }
1.319     dtucker   996:                        if (ci->address == NULL)
                    997:                                match_test_missing_fatal("Address", "addr");
1.226     dtucker   998:                        switch (addr_match_list(ci->address, arg)) {
1.181     djm       999:                        case 1:
                   1000:                                debug("connection from %.100s matched 'Address "
1.226     dtucker  1001:                                    "%.100s' at line %d", ci->address, arg, line);
1.181     djm      1002:                                break;
                   1003:                        case 0:
1.183     djm      1004:                        case -1:
1.153     dtucker  1005:                                result = 0;
1.181     djm      1006:                                break;
1.183     djm      1007:                        case -2:
1.181     djm      1008:                                return -1;
1.153     dtucker  1009:                        }
1.226     dtucker  1010:                } else if (strcasecmp(attrib, "localaddress") == 0){
1.319     dtucker  1011:                        if (ci == NULL) {
1.226     dtucker  1012:                                result = 0;
                   1013:                                continue;
                   1014:                        }
1.319     dtucker  1015:                        if (ci->laddress == NULL)
                   1016:                                match_test_missing_fatal("LocalAddress",
                   1017:                                    "laddr");
1.226     dtucker  1018:                        switch (addr_match_list(ci->laddress, arg)) {
                   1019:                        case 1:
                   1020:                                debug("connection from %.100s matched "
                   1021:                                    "'LocalAddress %.100s' at line %d",
                   1022:                                    ci->laddress, arg, line);
                   1023:                                break;
                   1024:                        case 0:
                   1025:                        case -1:
                   1026:                                result = 0;
                   1027:                                break;
                   1028:                        case -2:
                   1029:                                return -1;
                   1030:                        }
                   1031:                } else if (strcasecmp(attrib, "localport") == 0) {
                   1032:                        if ((port = a2port(arg)) == -1) {
                   1033:                                error("Invalid LocalPort '%s' on Match line",
                   1034:                                    arg);
                   1035:                                return -1;
                   1036:                        }
1.319     dtucker  1037:                        if (ci == NULL) {
1.226     dtucker  1038:                                result = 0;
                   1039:                                continue;
                   1040:                        }
1.319     dtucker  1041:                        if (ci->lport == 0)
                   1042:                                match_test_missing_fatal("LocalPort", "lport");
1.226     dtucker  1043:                        /* TODO support port lists */
                   1044:                        if (port == ci->lport)
                   1045:                                debug("connection from %.100s matched "
                   1046:                                    "'LocalPort %d' at line %d",
                   1047:                                    ci->laddress, port, line);
                   1048:                        else
                   1049:                                result = 0;
1.317     djm      1050:                } else if (strcasecmp(attrib, "rdomain") == 0) {
                   1051:                        if (ci == NULL || ci->rdomain == NULL) {
                   1052:                                result = 0;
                   1053:                                continue;
                   1054:                        }
                   1055:                        if (match_pattern_list(ci->rdomain, arg, 0) != 1)
                   1056:                                result = 0;
                   1057:                        else
                   1058:                                debug("user %.100s matched 'RDomain %.100s' at "
                   1059:                                    "line %d", ci->rdomain, arg, line);
1.153     dtucker  1060:                } else {
                   1061:                        error("Unsupported Match attribute %s", attrib);
                   1062:                        return -1;
                   1063:                }
1.243     dtucker  1064:        }
                   1065:        if (attributes == 0) {
                   1066:                error("One or more attributes required for Match");
                   1067:                return -1;
1.153     dtucker  1068:        }
1.226     dtucker  1069:        if (ci != NULL)
1.153     dtucker  1070:                debug3("match %sfound", result ? "" : "not ");
                   1071:        *condition = cp;
                   1072:        return result;
                   1073: }
                   1074:
1.158     dtucker  1075: #define WHITESPACE " \t\r\n"
                   1076:
1.220     djm      1077: /* Multistate option parsing */
                   1078: struct multistate {
                   1079:        char *key;
                   1080:        int value;
                   1081: };
1.320     djm      1082: static const struct multistate multistate_flag[] = {
                   1083:        { "yes",                        1 },
                   1084:        { "no",                         0 },
                   1085:        { NULL, -1 }
                   1086: };
1.220     djm      1087: static const struct multistate multistate_addressfamily[] = {
                   1088:        { "inet",                       AF_INET },
                   1089:        { "inet6",                      AF_INET6 },
                   1090:        { "any",                        AF_UNSPEC },
                   1091:        { NULL, -1 }
                   1092: };
                   1093: static const struct multistate multistate_permitrootlogin[] = {
                   1094:        { "without-password",           PERMIT_NO_PASSWD },
1.280     deraadt  1095:        { "prohibit-password",          PERMIT_NO_PASSWD },
1.220     djm      1096:        { "forced-commands-only",       PERMIT_FORCED_ONLY },
                   1097:        { "yes",                        PERMIT_YES },
                   1098:        { "no",                         PERMIT_NO },
                   1099:        { NULL, -1 }
                   1100: };
                   1101: static const struct multistate multistate_compression[] = {
1.297     djm      1102:        { "yes",                        COMP_DELAYED },
1.220     djm      1103:        { "delayed",                    COMP_DELAYED },
                   1104:        { "no",                         COMP_NONE },
                   1105:        { NULL, -1 }
                   1106: };
                   1107: static const struct multistate multistate_gatewayports[] = {
                   1108:        { "clientspecified",            2 },
                   1109:        { "yes",                        1 },
                   1110:        { "no",                         0 },
1.222     djm      1111:        { NULL, -1 }
                   1112: };
1.233     djm      1113: static const struct multistate multistate_tcpfwd[] = {
                   1114:        { "yes",                        FORWARD_ALLOW },
                   1115:        { "all",                        FORWARD_ALLOW },
                   1116:        { "no",                         FORWARD_DENY },
                   1117:        { "remote",                     FORWARD_REMOTE },
                   1118:        { "local",                      FORWARD_LOCAL },
                   1119:        { NULL, -1 }
                   1120: };
1.220     djm      1121:
1.94      markus   1122: int
                   1123: process_server_config_line(ServerOptions *options, char *line,
1.226     dtucker  1124:     const char *filename, int linenum, int *activep,
                   1125:     struct connection_info *connectinfo)
1.1       deraadt  1126: {
1.329     djm      1127:        char *cp, ***chararrayptr, **charptr, *arg, *arg2, *p;
1.237     dtucker  1128:        int cmdline = 0, *intptr, value, value2, n, port;
1.174     dtucker  1129:        SyslogFacility *log_facility_ptr;
                   1130:        LogLevel *log_level_ptr;
1.25      markus   1131:        ServerOpCodes opcode;
1.329     djm      1132:        u_int i, *uintptr, uvalue, flags = 0;
1.151     djm      1133:        size_t len;
1.237     dtucker  1134:        long long val64;
1.220     djm      1135:        const struct multistate *multistate_ptr;
1.322     dtucker  1136:        const char *errstr;
1.303     djm      1137:
                   1138:        /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
                   1139:        if ((len = strlen(line)) == 0)
                   1140:                return 0;
                   1141:        for (len--; len > 0; len--) {
                   1142:                if (strchr(WHITESPACE "\f", line[len]) == NULL)
                   1143:                        break;
                   1144:                line[len] = '\0';
                   1145:        }
1.25      markus   1146:
1.94      markus   1147:        cp = line;
1.148     dtucker  1148:        if ((arg = strdelim(&cp)) == NULL)
1.147     djm      1149:                return 0;
1.94      markus   1150:        /* Ignore leading whitespace */
                   1151:        if (*arg == '\0')
                   1152:                arg = strdelim(&cp);
                   1153:        if (!arg || !*arg || *arg == '#')
                   1154:                return 0;
                   1155:        intptr = NULL;
                   1156:        charptr = NULL;
1.153     dtucker  1157:        opcode = parse_token(arg, filename, linenum, &flags);
                   1158:
                   1159:        if (activep == NULL) { /* We are processing a command line directive */
                   1160:                cmdline = 1;
                   1161:                activep = &cmdline;
                   1162:        }
                   1163:        if (*activep && opcode != sMatch)
                   1164:                debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
                   1165:        if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
1.226     dtucker  1166:                if (connectinfo == NULL) {
1.153     dtucker  1167:                        fatal("%s line %d: Directive '%s' is not allowed "
                   1168:                            "within a Match block", filename, linenum, arg);
                   1169:                } else { /* this is a directive we have already processed */
                   1170:                        while (arg)
                   1171:                                arg = strdelim(&cp);
                   1172:                        return 0;
                   1173:                }
                   1174:        }
                   1175:
1.94      markus   1176:        switch (opcode) {
                   1177:        case sBadOption:
                   1178:                return -1;
                   1179:        case sPort:
                   1180:                /* ignore ports from configfile if cmdline specifies ports */
                   1181:                if (options->ports_from_cmdline)
                   1182:                        return 0;
                   1183:                if (options->num_ports >= MAX_PORTS)
                   1184:                        fatal("%s line %d: too many ports.",
                   1185:                            filename, linenum);
1.48      provos   1186:                arg = strdelim(&cp);
1.94      markus   1187:                if (!arg || *arg == '\0')
                   1188:                        fatal("%s line %d: missing port number.",
                   1189:                            filename, linenum);
                   1190:                options->ports[options->num_ports++] = a2port(arg);
1.194     djm      1191:                if (options->ports[options->num_ports-1] <= 0)
1.94      markus   1192:                        fatal("%s line %d: Badly formatted port number.",
                   1193:                            filename, linenum);
                   1194:                break;
1.29      markus   1195:
1.94      markus   1196:        case sLoginGraceTime:
                   1197:                intptr = &options->login_grace_time;
1.180     djm      1198:  parse_time:
1.94      markus   1199:                arg = strdelim(&cp);
                   1200:                if (!arg || *arg == '\0')
                   1201:                        fatal("%s line %d: missing time value.",
                   1202:                            filename, linenum);
                   1203:                if ((value = convtime(arg)) == -1)
                   1204:                        fatal("%s line %d: invalid time value.",
                   1205:                            filename, linenum);
1.268     djm      1206:                if (*activep && *intptr == -1)
1.94      markus   1207:                        *intptr = value;
                   1208:                break;
                   1209:
                   1210:        case sListenAddress:
                   1211:                arg = strdelim(&cp);
1.139     djm      1212:                if (arg == NULL || *arg == '\0')
                   1213:                        fatal("%s line %d: missing address",
1.94      markus   1214:                            filename, linenum);
1.144     dtucker  1215:                /* check for bare IPv6 address: no "[]" and 2 or more ":" */
                   1216:                if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
                   1217:                    && strchr(p+1, ':') != NULL) {
1.315     djm      1218:                        port = 0;
                   1219:                        p = arg;
                   1220:                } else {
                   1221:                        p = hpdelim(&arg);
                   1222:                        if (p == NULL)
                   1223:                                fatal("%s line %d: bad address:port usage",
                   1224:                                    filename, linenum);
                   1225:                        p = cleanhostname(p);
                   1226:                        if (arg == NULL)
                   1227:                                port = 0;
                   1228:                        else if ((port = a2port(arg)) <= 0)
                   1229:                                fatal("%s line %d: bad port number",
                   1230:                                    filename, linenum);
                   1231:                }
                   1232:                /* Optional routing table */
                   1233:                arg2 = NULL;
                   1234:                if ((arg = strdelim(&cp)) != NULL) {
                   1235:                        if (strcmp(arg, "rdomain") != 0 ||
                   1236:                            (arg2 = strdelim(&cp)) == NULL)
                   1237:                                fatal("%s line %d: bad ListenAddress syntax",
                   1238:                                    filename, linenum);
                   1239:                        if (!valid_rdomain(arg2))
                   1240:                                fatal("%s line %d: bad routing domain",
                   1241:                                    filename, linenum);
1.144     dtucker  1242:                }
1.139     djm      1243:
1.315     djm      1244:                queue_listen_addr(options, p, arg2, port);
1.25      markus   1245:
1.138     djm      1246:                break;
                   1247:
                   1248:        case sAddressFamily:
1.220     djm      1249:                intptr = &options->address_family;
                   1250:                multistate_ptr = multistate_addressfamily;
                   1251:  parse_multistate:
1.138     djm      1252:                arg = strdelim(&cp);
1.141     markus   1253:                if (!arg || *arg == '\0')
1.220     djm      1254:                        fatal("%s line %d: missing argument.",
1.141     markus   1255:                            filename, linenum);
1.220     djm      1256:                value = -1;
                   1257:                for (i = 0; multistate_ptr[i].key != NULL; i++) {
                   1258:                        if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
                   1259:                                value = multistate_ptr[i].value;
                   1260:                                break;
                   1261:                        }
                   1262:                }
                   1263:                if (value == -1)
                   1264:                        fatal("%s line %d: unsupported option \"%s\".",
1.138     djm      1265:                            filename, linenum, arg);
1.220     djm      1266:                if (*activep && *intptr == -1)
1.138     djm      1267:                        *intptr = value;
1.94      markus   1268:                break;
                   1269:
                   1270:        case sHostKeyFile:
                   1271:                arg = strdelim(&cp);
                   1272:                if (!arg || *arg == '\0')
                   1273:                        fatal("%s line %d: missing file name.",
                   1274:                            filename, linenum);
1.314     djm      1275:                if (*activep)
                   1276:                        servconf_add_hostkey(filename, linenum, options, arg);
1.94      markus   1277:                break;
1.76      stevesk  1278:
1.240     markus   1279:        case sHostKeyAgent:
                   1280:                charptr = &options->host_key_agent;
                   1281:                arg = strdelim(&cp);
                   1282:                if (!arg || *arg == '\0')
                   1283:                        fatal("%s line %d: missing socket name.",
                   1284:                            filename, linenum);
                   1285:                if (*activep && *charptr == NULL)
                   1286:                        *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
                   1287:                            xstrdup(arg) : derelativise_path(arg);
                   1288:                break;
                   1289:
1.203     djm      1290:        case sHostCertificate:
1.314     djm      1291:                arg = strdelim(&cp);
                   1292:                if (!arg || *arg == '\0')
                   1293:                        fatal("%s line %d: missing file name.",
                   1294:                            filename, linenum);
                   1295:                if (*activep)
                   1296:                        servconf_add_hostcert(filename, linenum, options, arg);
                   1297:                break;
1.203     djm      1298:
1.94      markus   1299:        case sPidFile:
                   1300:                charptr = &options->pid_file;
1.314     djm      1301:  parse_filename:
                   1302:                arg = strdelim(&cp);
                   1303:                if (!arg || *arg == '\0')
                   1304:                        fatal("%s line %d: missing file name.",
                   1305:                            filename, linenum);
                   1306:                if (*activep && *charptr == NULL) {
                   1307:                        *charptr = derelativise_path(arg);
                   1308:                        /* increase optional counter */
                   1309:                        if (intptr != NULL)
                   1310:                                *intptr = *intptr + 1;
                   1311:                }
                   1312:                break;
1.25      markus   1313:
1.94      markus   1314:        case sPermitRootLogin:
                   1315:                intptr = &options->permit_root_login;
1.220     djm      1316:                multistate_ptr = multistate_permitrootlogin;
                   1317:                goto parse_multistate;
1.36      markus   1318:
1.94      markus   1319:        case sIgnoreRhosts:
                   1320:                intptr = &options->ignore_rhosts;
1.180     djm      1321:  parse_flag:
1.320     djm      1322:                multistate_ptr = multistate_flag;
                   1323:                goto parse_multistate;
1.94      markus   1324:
                   1325:        case sIgnoreUserKnownHosts:
                   1326:                intptr = &options->ignore_user_known_hosts;
                   1327:                goto parse_flag;
                   1328:
                   1329:        case sHostbasedAuthentication:
                   1330:                intptr = &options->hostbased_authentication;
                   1331:                goto parse_flag;
                   1332:
                   1333:        case sHostbasedUsesNameFromPacketOnly:
                   1334:                intptr = &options->hostbased_uses_name_from_packet_only;
                   1335:                goto parse_flag;
                   1336:
1.258     djm      1337:        case sHostbasedAcceptedKeyTypes:
                   1338:                charptr = &options->hostbased_key_types;
                   1339:  parse_keytypes:
                   1340:                arg = strdelim(&cp);
                   1341:                if (!arg || *arg == '\0')
                   1342:                        fatal("%s line %d: Missing argument.",
                   1343:                            filename, linenum);
1.304     djm      1344:                if (*arg != '-' &&
                   1345:                    !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
1.258     djm      1346:                        fatal("%s line %d: Bad key types '%s'.",
                   1347:                            filename, linenum, arg ? arg : "<NONE>");
                   1348:                if (*activep && *charptr == NULL)
                   1349:                        *charptr = xstrdup(arg);
                   1350:                break;
                   1351:
1.276     markus   1352:        case sHostKeyAlgorithms:
                   1353:                charptr = &options->hostkeyalgorithms;
                   1354:                goto parse_keytypes;
                   1355:
1.94      markus   1356:        case sPubkeyAuthentication:
                   1357:                intptr = &options->pubkey_authentication;
                   1358:                goto parse_flag;
1.119     jakob    1359:
1.258     djm      1360:        case sPubkeyAcceptedKeyTypes:
                   1361:                charptr = &options->pubkey_key_types;
                   1362:                goto parse_keytypes;
                   1363:
1.94      markus   1364:        case sKerberosAuthentication:
                   1365:                intptr = &options->kerberos_authentication;
                   1366:                goto parse_flag;
                   1367:
                   1368:        case sKerberosOrLocalPasswd:
                   1369:                intptr = &options->kerberos_or_local_passwd;
                   1370:                goto parse_flag;
                   1371:
                   1372:        case sKerberosTicketCleanup:
                   1373:                intptr = &options->kerberos_ticket_cleanup;
1.130     jakob    1374:                goto parse_flag;
                   1375:
                   1376:        case sKerberosGetAFSToken:
                   1377:                intptr = &options->kerberos_get_afs_token;
1.125     markus   1378:                goto parse_flag;
                   1379:
                   1380:        case sGssAuthentication:
                   1381:                intptr = &options->gss_authentication;
                   1382:                goto parse_flag;
                   1383:
                   1384:        case sGssCleanupCreds:
                   1385:                intptr = &options->gss_cleanup_creds;
1.271     djm      1386:                goto parse_flag;
                   1387:
                   1388:        case sGssStrictAcceptor:
                   1389:                intptr = &options->gss_strict_acceptor;
1.94      markus   1390:                goto parse_flag;
                   1391:
                   1392:        case sPasswordAuthentication:
                   1393:                intptr = &options->password_authentication;
                   1394:                goto parse_flag;
                   1395:
                   1396:        case sKbdInteractiveAuthentication:
                   1397:                intptr = &options->kbd_interactive_authentication;
                   1398:                goto parse_flag;
                   1399:
                   1400:        case sChallengeResponseAuthentication:
                   1401:                intptr = &options->challenge_response_authentication;
                   1402:                goto parse_flag;
                   1403:
                   1404:        case sPrintMotd:
                   1405:                intptr = &options->print_motd;
                   1406:                goto parse_flag;
                   1407:
                   1408:        case sPrintLastLog:
                   1409:                intptr = &options->print_lastlog;
                   1410:                goto parse_flag;
                   1411:
                   1412:        case sX11Forwarding:
                   1413:                intptr = &options->x11_forwarding;
                   1414:                goto parse_flag;
                   1415:
                   1416:        case sX11DisplayOffset:
                   1417:                intptr = &options->x11_display_offset;
1.293     naddy    1418:  parse_int:
                   1419:                arg = strdelim(&cp);
1.322     dtucker  1420:                if ((errstr = atoi_err(arg, &value)) != NULL)
                   1421:                        fatal("%s line %d: integer value %s.",
                   1422:                            filename, linenum, errstr);
1.293     naddy    1423:                if (*activep && *intptr == -1)
                   1424:                        *intptr = value;
                   1425:                break;
1.99      stevesk  1426:
                   1427:        case sX11UseLocalhost:
                   1428:                intptr = &options->x11_use_localhost;
                   1429:                goto parse_flag;
1.94      markus   1430:
                   1431:        case sXAuthLocation:
                   1432:                charptr = &options->xauth_location;
                   1433:                goto parse_filename;
                   1434:
1.244     djm      1435:        case sPermitTTY:
                   1436:                intptr = &options->permit_tty;
                   1437:                goto parse_flag;
                   1438:
1.250     djm      1439:        case sPermitUserRC:
                   1440:                intptr = &options->permit_user_rc;
                   1441:                goto parse_flag;
                   1442:
1.94      markus   1443:        case sStrictModes:
                   1444:                intptr = &options->strict_modes;
                   1445:                goto parse_flag;
                   1446:
1.129     markus   1447:        case sTCPKeepAlive:
                   1448:                intptr = &options->tcp_keep_alive;
1.94      markus   1449:                goto parse_flag;
                   1450:
                   1451:        case sEmptyPasswd:
                   1452:                intptr = &options->permit_empty_passwd;
1.113     markus   1453:                goto parse_flag;
                   1454:
                   1455:        case sPermitUserEnvironment:
                   1456:                intptr = &options->permit_user_env;
1.334   ! djm      1457:                charptr = &options->permit_user_env_whitelist;
        !          1458:                arg = strdelim(&cp);
        !          1459:                if (!arg || *arg == '\0')
        !          1460:                        fatal("%s line %d: missing argument.",
        !          1461:                            filename, linenum);
        !          1462:                value = 0;
        !          1463:                p = NULL;
        !          1464:                if (strcmp(arg, "yes") == 0)
        !          1465:                        value = 1;
        !          1466:                else if (strcmp(arg, "no") == 0)
        !          1467:                        value = 0;
        !          1468:                else {
        !          1469:                        /* Pattern-list specified */
        !          1470:                        value = 1;
        !          1471:                        p = xstrdup(arg);
        !          1472:                }
        !          1473:                if (*activep && *intptr == -1) {
        !          1474:                        *intptr = value;
        !          1475:                        *charptr = p;
        !          1476:                        p = NULL;
        !          1477:                }
        !          1478:                free(p);
        !          1479:                break;
1.94      markus   1480:
1.111     markus   1481:        case sCompression:
                   1482:                intptr = &options->compression;
1.220     djm      1483:                multistate_ptr = multistate_compression;
                   1484:                goto parse_multistate;
1.94      markus   1485:
1.235     dtucker  1486:        case sRekeyLimit:
                   1487:                arg = strdelim(&cp);
                   1488:                if (!arg || *arg == '\0')
                   1489:                        fatal("%.200s line %d: Missing argument.", filename,
                   1490:                            linenum);
                   1491:                if (strcmp(arg, "default") == 0) {
                   1492:                        val64 = 0;
                   1493:                } else {
1.236     dtucker  1494:                        if (scan_scaled(arg, &val64) == -1)
                   1495:                                fatal("%.200s line %d: Bad number '%s': %s",
                   1496:                                    filename, linenum, arg, strerror(errno));
1.235     dtucker  1497:                        if (val64 != 0 && val64 < 16)
                   1498:                                fatal("%.200s line %d: RekeyLimit too small",
                   1499:                                    filename, linenum);
                   1500:                }
                   1501:                if (*activep && options->rekey_limit == -1)
1.284     dtucker  1502:                        options->rekey_limit = val64;
1.235     dtucker  1503:                if (cp != NULL) { /* optional rekey interval present */
                   1504:                        if (strcmp(cp, "none") == 0) {
                   1505:                                (void)strdelim(&cp);    /* discard */
                   1506:                                break;
                   1507:                        }
                   1508:                        intptr = &options->rekey_interval;
                   1509:                        goto parse_time;
                   1510:                }
                   1511:                break;
                   1512:
1.94      markus   1513:        case sGatewayPorts:
1.251     millert  1514:                intptr = &options->fwd_opts.gateway_ports;
1.220     djm      1515:                multistate_ptr = multistate_gatewayports;
                   1516:                goto parse_multistate;
1.25      markus   1517:
1.122     markus   1518:        case sUseDNS:
                   1519:                intptr = &options->use_dns;
1.94      markus   1520:                goto parse_flag;
1.53      markus   1521:
1.94      markus   1522:        case sLogFacility:
1.174     dtucker  1523:                log_facility_ptr = &options->log_facility;
1.94      markus   1524:                arg = strdelim(&cp);
                   1525:                value = log_facility_number(arg);
1.101     markus   1526:                if (value == SYSLOG_FACILITY_NOT_SET)
1.94      markus   1527:                        fatal("%.200s line %d: unsupported log facility '%s'",
                   1528:                            filename, linenum, arg ? arg : "<NONE>");
1.174     dtucker  1529:                if (*log_facility_ptr == -1)
                   1530:                        *log_facility_ptr = (SyslogFacility) value;
1.94      markus   1531:                break;
1.25      markus   1532:
1.94      markus   1533:        case sLogLevel:
1.174     dtucker  1534:                log_level_ptr = &options->log_level;
1.94      markus   1535:                arg = strdelim(&cp);
                   1536:                value = log_level_number(arg);
1.101     markus   1537:                if (value == SYSLOG_LEVEL_NOT_SET)
1.94      markus   1538:                        fatal("%.200s line %d: unsupported log level '%s'",
                   1539:                            filename, linenum, arg ? arg : "<NONE>");
1.308     djm      1540:                if (*activep && *log_level_ptr == -1)
1.174     dtucker  1541:                        *log_level_ptr = (LogLevel) value;
1.94      markus   1542:                break;
                   1543:
                   1544:        case sAllowTcpForwarding:
                   1545:                intptr = &options->allow_tcp_forwarding;
1.233     djm      1546:                multistate_ptr = multistate_tcpfwd;
                   1547:                goto parse_multistate;
1.102     provos   1548:
1.251     millert  1549:        case sAllowStreamLocalForwarding:
                   1550:                intptr = &options->allow_streamlocal_forwarding;
                   1551:                multistate_ptr = multistate_tcpfwd;
                   1552:                goto parse_multistate;
                   1553:
1.178     pyr      1554:        case sAllowAgentForwarding:
                   1555:                intptr = &options->allow_agent_forwarding;
                   1556:                goto parse_flag;
                   1557:
1.301     djm      1558:        case sDisableForwarding:
                   1559:                intptr = &options->disable_forwarding;
                   1560:                goto parse_flag;
                   1561:
1.94      markus   1562:        case sAllowUsers:
                   1563:                while ((arg = strdelim(&cp)) && *arg != '\0') {
1.299     djm      1564:                        if (match_user(NULL, NULL, NULL, arg) == -1)
                   1565:                                fatal("%s line %d: invalid AllowUsers pattern: "
                   1566:                                    "\"%.100s\"", filename, linenum, arg);
1.227     markus   1567:                        if (!*activep)
                   1568:                                continue;
1.314     djm      1569:                        array_append(filename, linenum, "AllowUsers",
                   1570:                            &options->allow_users, &options->num_allow_users,
                   1571:                            arg);
1.94      markus   1572:                }
                   1573:                break;
1.25      markus   1574:
1.94      markus   1575:        case sDenyUsers:
                   1576:                while ((arg = strdelim(&cp)) && *arg != '\0') {
1.299     djm      1577:                        if (match_user(NULL, NULL, NULL, arg) == -1)
                   1578:                                fatal("%s line %d: invalid DenyUsers pattern: "
                   1579:                                    "\"%.100s\"", filename, linenum, arg);
1.227     markus   1580:                        if (!*activep)
                   1581:                                continue;
1.314     djm      1582:                        array_append(filename, linenum, "DenyUsers",
                   1583:                            &options->deny_users, &options->num_deny_users,
                   1584:                            arg);
1.94      markus   1585:                }
                   1586:                break;
1.25      markus   1587:
1.94      markus   1588:        case sAllowGroups:
                   1589:                while ((arg = strdelim(&cp)) && *arg != '\0') {
1.227     markus   1590:                        if (!*activep)
                   1591:                                continue;
1.314     djm      1592:                        array_append(filename, linenum, "AllowGroups",
                   1593:                            &options->allow_groups, &options->num_allow_groups,
                   1594:                            arg);
1.94      markus   1595:                }
                   1596:                break;
1.33      markus   1597:
1.94      markus   1598:        case sDenyGroups:
                   1599:                while ((arg = strdelim(&cp)) && *arg != '\0') {
1.227     markus   1600:                        if (!*activep)
                   1601:                                continue;
1.314     djm      1602:                        array_append(filename, linenum, "DenyGroups",
                   1603:                            &options->deny_groups, &options->num_deny_groups,
                   1604:                            arg);
1.94      markus   1605:                }
                   1606:                break;
1.66      markus   1607:
1.94      markus   1608:        case sCiphers:
                   1609:                arg = strdelim(&cp);
                   1610:                if (!arg || *arg == '\0')
                   1611:                        fatal("%s line %d: Missing argument.", filename, linenum);
1.304     djm      1612:                if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
1.94      markus   1613:                        fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
                   1614:                            filename, linenum, arg ? arg : "<NONE>");
                   1615:                if (options->ciphers == NULL)
                   1616:                        options->ciphers = xstrdup(arg);
                   1617:                break;
1.33      markus   1618:
1.94      markus   1619:        case sMacs:
                   1620:                arg = strdelim(&cp);
                   1621:                if (!arg || *arg == '\0')
                   1622:                        fatal("%s line %d: Missing argument.", filename, linenum);
1.304     djm      1623:                if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
1.94      markus   1624:                        fatal("%s line %d: Bad SSH2 mac spec '%s'.",
                   1625:                            filename, linenum, arg ? arg : "<NONE>");
                   1626:                if (options->macs == NULL)
                   1627:                        options->macs = xstrdup(arg);
1.211     djm      1628:                break;
                   1629:
                   1630:        case sKexAlgorithms:
                   1631:                arg = strdelim(&cp);
                   1632:                if (!arg || *arg == '\0')
                   1633:                        fatal("%s line %d: Missing argument.",
                   1634:                            filename, linenum);
1.304     djm      1635:                if (*arg != '-' &&
                   1636:                    !kex_names_valid(*arg == '+' ? arg + 1 : arg))
1.211     djm      1637:                        fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
                   1638:                            filename, linenum, arg ? arg : "<NONE>");
                   1639:                if (options->kex_algorithms == NULL)
                   1640:                        options->kex_algorithms = xstrdup(arg);
1.94      markus   1641:                break;
1.43      jakob    1642:
1.94      markus   1643:        case sSubsystem:
                   1644:                if (options->num_subsystems >= MAX_SUBSYSTEMS) {
                   1645:                        fatal("%s line %d: too many subsystems defined.",
1.95      deraadt  1646:                            filename, linenum);
1.94      markus   1647:                }
                   1648:                arg = strdelim(&cp);
                   1649:                if (!arg || *arg == '\0')
                   1650:                        fatal("%s line %d: Missing subsystem name.",
1.95      deraadt  1651:                            filename, linenum);
1.153     dtucker  1652:                if (!*activep) {
                   1653:                        arg = strdelim(&cp);
                   1654:                        break;
                   1655:                }
1.94      markus   1656:                for (i = 0; i < options->num_subsystems; i++)
                   1657:                        if (strcmp(arg, options->subsystem_name[i]) == 0)
                   1658:                                fatal("%s line %d: Subsystem '%s' already defined.",
1.95      deraadt  1659:                                    filename, linenum, arg);
1.94      markus   1660:                options->subsystem_name[options->num_subsystems] = xstrdup(arg);
                   1661:                arg = strdelim(&cp);
                   1662:                if (!arg || *arg == '\0')
                   1663:                        fatal("%s line %d: Missing subsystem command.",
1.95      deraadt  1664:                            filename, linenum);
1.94      markus   1665:                options->subsystem_command[options->num_subsystems] = xstrdup(arg);
1.151     djm      1666:
                   1667:                /* Collect arguments (separate to executable) */
                   1668:                p = xstrdup(arg);
                   1669:                len = strlen(p) + 1;
                   1670:                while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
                   1671:                        len += 1 + strlen(arg);
1.264     deraadt  1672:                        p = xreallocarray(p, 1, len);
1.151     djm      1673:                        strlcat(p, " ", len);
                   1674:                        strlcat(p, arg, len);
                   1675:                }
                   1676:                options->subsystem_args[options->num_subsystems] = p;
1.94      markus   1677:                options->num_subsystems++;
                   1678:                break;
1.46      markus   1679:
1.94      markus   1680:        case sMaxStartups:
                   1681:                arg = strdelim(&cp);
                   1682:                if (!arg || *arg == '\0')
                   1683:                        fatal("%s line %d: Missing MaxStartups spec.",
1.95      deraadt  1684:                            filename, linenum);
1.94      markus   1685:                if ((n = sscanf(arg, "%d:%d:%d",
                   1686:                    &options->max_startups_begin,
                   1687:                    &options->max_startups_rate,
                   1688:                    &options->max_startups)) == 3) {
                   1689:                        if (options->max_startups_begin >
                   1690:                            options->max_startups ||
                   1691:                            options->max_startups_rate > 100 ||
                   1692:                            options->max_startups_rate < 1)
1.50      markus   1693:                                fatal("%s line %d: Illegal MaxStartups spec.",
1.87      stevesk  1694:                                    filename, linenum);
1.94      markus   1695:                } else if (n != 1)
                   1696:                        fatal("%s line %d: Illegal MaxStartups spec.",
                   1697:                            filename, linenum);
                   1698:                else
                   1699:                        options->max_startups = options->max_startups_begin;
                   1700:                break;
1.133     dtucker  1701:
                   1702:        case sMaxAuthTries:
                   1703:                intptr = &options->max_authtries;
                   1704:                goto parse_int;
1.94      markus   1705:
1.180     djm      1706:        case sMaxSessions:
                   1707:                intptr = &options->max_sessions;
                   1708:                goto parse_int;
                   1709:
1.94      markus   1710:        case sBanner:
                   1711:                charptr = &options->banner;
                   1712:                goto parse_filename;
1.176     djm      1713:
1.94      markus   1714:        /*
                   1715:         * These options can contain %X options expanded at
                   1716:         * connect time, so that you can specify paths like:
                   1717:         *
                   1718:         * AuthorizedKeysFile   /etc/ssh_keys/%u
                   1719:         */
                   1720:        case sAuthorizedKeysFile:
1.219     djm      1721:                if (*activep && options->num_authkeys_files == 0) {
                   1722:                        while ((arg = strdelim(&cp)) && *arg != '\0') {
1.314     djm      1723:                                arg = tilde_expand_filename(arg, getuid());
                   1724:                                array_append(filename, linenum,
                   1725:                                    "AuthorizedKeysFile",
                   1726:                                    &options->authorized_keys_files,
                   1727:                                    &options->num_authkeys_files, arg);
                   1728:                                free(arg);
1.219     djm      1729:                        }
                   1730:                }
                   1731:                return 0;
                   1732:
1.208     djm      1733:        case sAuthorizedPrincipalsFile:
                   1734:                charptr = &options->authorized_principals_file;
1.205     djm      1735:                arg = strdelim(&cp);
                   1736:                if (!arg || *arg == '\0')
                   1737:                        fatal("%s line %d: missing file name.",
                   1738:                            filename, linenum);
                   1739:                if (*activep && *charptr == NULL) {
1.206     markus   1740:                        *charptr = tilde_expand_filename(arg, getuid());
1.205     djm      1741:                        /* increase optional counter */
                   1742:                        if (intptr != NULL)
                   1743:                                *intptr = *intptr + 1;
                   1744:                }
                   1745:                break;
1.94      markus   1746:
                   1747:        case sClientAliveInterval:
                   1748:                intptr = &options->client_alive_interval;
                   1749:                goto parse_time;
                   1750:
                   1751:        case sClientAliveCountMax:
                   1752:                intptr = &options->client_alive_count_max;
                   1753:                goto parse_int;
1.131     djm      1754:
                   1755:        case sAcceptEnv:
                   1756:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                   1757:                        if (strchr(arg, '=') != NULL)
                   1758:                                fatal("%s line %d: Invalid environment name.",
                   1759:                                    filename, linenum);
1.153     dtucker  1760:                        if (!*activep)
1.227     markus   1761:                                continue;
1.314     djm      1762:                        array_append(filename, linenum, "AcceptEnv",
                   1763:                            &options->accept_env, &options->num_accept_env,
                   1764:                            arg);
1.131     djm      1765:                }
                   1766:                break;
1.145     reyk     1767:
1.332     djm      1768:        case sSetEnv:
                   1769:                uvalue = options->num_setenv;
                   1770:                while ((arg = strdelimw(&cp)) && *arg != '\0') {
                   1771:                        if (strchr(arg, '=') == NULL)
                   1772:                                fatal("%s line %d: Invalid environment.",
                   1773:                                    filename, linenum);
                   1774:                        if (!*activep || uvalue != 0)
                   1775:                                continue;
                   1776:                        array_append(filename, linenum, "SetEnv",
                   1777:                            &options->setenv, &options->num_setenv, arg);
                   1778:                }
                   1779:                break;
                   1780:
1.145     reyk     1781:        case sPermitTunnel:
                   1782:                intptr = &options->permit_tun;
1.146     reyk     1783:                arg = strdelim(&cp);
                   1784:                if (!arg || *arg == '\0')
                   1785:                        fatal("%s line %d: Missing yes/point-to-point/"
                   1786:                            "ethernet/no argument.", filename, linenum);
1.182     dtucker  1787:                value = -1;
                   1788:                for (i = 0; tunmode_desc[i].val != -1; i++)
                   1789:                        if (strcmp(tunmode_desc[i].text, arg) == 0) {
                   1790:                                value = tunmode_desc[i].val;
                   1791:                                break;
                   1792:                        }
                   1793:                if (value == -1)
1.146     reyk     1794:                        fatal("%s line %d: Bad yes/point-to-point/ethernet/"
                   1795:                            "no argument: %s", filename, linenum, arg);
1.268     djm      1796:                if (*activep && *intptr == -1)
1.146     reyk     1797:                        *intptr = value;
                   1798:                break;
1.94      markus   1799:
1.153     dtucker  1800:        case sMatch:
                   1801:                if (cmdline)
                   1802:                        fatal("Match directive not supported as a command-line "
                   1803:                           "option");
1.226     dtucker  1804:                value = match_cfg_line(&cp, linenum, connectinfo);
1.153     dtucker  1805:                if (value < 0)
                   1806:                        fatal("%s line %d: Bad Match condition", filename,
                   1807:                            linenum);
                   1808:                *activep = value;
1.156     dtucker  1809:                break;
                   1810:
1.330     djm      1811:        case sPermitListen:
1.156     dtucker  1812:        case sPermitOpen:
1.330     djm      1813:                if (opcode == sPermitListen) {
                   1814:                        uintptr = &options->num_permitted_listens;
                   1815:                        chararrayptr = &options->permitted_listens;
1.329     djm      1816:                } else {
                   1817:                        uintptr = &options->num_permitted_opens;
                   1818:                        chararrayptr = &options->permitted_opens;
                   1819:                }
1.156     dtucker  1820:                arg = strdelim(&cp);
                   1821:                if (!arg || *arg == '\0')
1.329     djm      1822:                        fatal("%s line %d: missing %s specification",
                   1823:                            filename, linenum, lookup_opcode_name(opcode));
                   1824:                uvalue = *uintptr;      /* modified later */
1.310     djm      1825:                if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
1.329     djm      1826:                        if (*activep && uvalue == 0) {
                   1827:                                *uintptr = 1;
                   1828:                                *chararrayptr = xcalloc(1,
                   1829:                                    sizeof(**chararrayptr));
                   1830:                                (*chararrayptr)[0] = xstrdup(arg);
1.311     dtucker  1831:                        }
1.156     dtucker  1832:                        break;
                   1833:                }
1.159     dtucker  1834:                for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1.333     djm      1835:                        if (opcode == sPermitListen &&
                   1836:                            strchr(arg, ':') == NULL) {
                   1837:                                /*
                   1838:                                 * Allow bare port number for PermitListen
                   1839:                                 * to indicate a wildcard listen host.
                   1840:                                 */
                   1841:                                xasprintf(&arg2, "*:%s", arg);
                   1842:                        } else {
                   1843:                                arg2 = xstrdup(arg);
                   1844:                                p = hpdelim(&arg);
                   1845:                                if (p == NULL) {
                   1846:                                        fatal("%s line %d: missing host in %s",
                   1847:                                            filename, linenum,
                   1848:                                            lookup_opcode_name(opcode));
                   1849:                                }
                   1850:                                p = cleanhostname(p);
1.329     djm      1851:                        }
                   1852:                        if (arg == NULL ||
                   1853:                            ((port = permitopen_port(arg)) < 0)) {
                   1854:                                fatal("%s line %d: bad port number in %s",
                   1855:                                    filename, linenum,
                   1856:                                    lookup_opcode_name(opcode));
                   1857:                        }
                   1858:                        if (*activep && uvalue == 0) {
1.314     djm      1859:                                array_append(filename, linenum,
1.329     djm      1860:                                    lookup_opcode_name(opcode),
                   1861:                                    chararrayptr, uintptr, arg2);
1.314     djm      1862:                        }
                   1863:                        free(arg2);
1.159     dtucker  1864:                }
1.153     dtucker  1865:                break;
                   1866:
1.158     dtucker  1867:        case sForceCommand:
1.262     dtucker  1868:                if (cp == NULL || *cp == '\0')
1.158     dtucker  1869:                        fatal("%.200s line %d: Missing argument.", filename,
                   1870:                            linenum);
                   1871:                len = strspn(cp, WHITESPACE);
                   1872:                if (*activep && options->adm_forced_command == NULL)
                   1873:                        options->adm_forced_command = xstrdup(cp + len);
                   1874:                return 0;
                   1875:
1.176     djm      1876:        case sChrootDirectory:
                   1877:                charptr = &options->chroot_directory;
1.177     djm      1878:
                   1879:                arg = strdelim(&cp);
                   1880:                if (!arg || *arg == '\0')
                   1881:                        fatal("%s line %d: missing file name.",
                   1882:                            filename, linenum);
                   1883:                if (*activep && *charptr == NULL)
                   1884:                        *charptr = xstrdup(arg);
                   1885:                break;
1.176     djm      1886:
1.204     djm      1887:        case sTrustedUserCAKeys:
                   1888:                charptr = &options->trusted_user_ca_keys;
                   1889:                goto parse_filename;
                   1890:
                   1891:        case sRevokedKeys:
                   1892:                charptr = &options->revoked_keys_file;
                   1893:                goto parse_filename;
                   1894:
1.213     djm      1895:        case sIPQoS:
                   1896:                arg = strdelim(&cp);
                   1897:                if ((value = parse_ipqos(arg)) == -1)
                   1898:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1899:                            filename, linenum, arg);
                   1900:                arg = strdelim(&cp);
                   1901:                if (arg == NULL)
                   1902:                        value2 = value;
                   1903:                else if ((value2 = parse_ipqos(arg)) == -1)
                   1904:                        fatal("%s line %d: Bad IPQoS value: %s",
                   1905:                            filename, linenum, arg);
                   1906:                if (*activep) {
                   1907:                        options->ip_qos_interactive = value;
                   1908:                        options->ip_qos_bulk = value2;
                   1909:                }
                   1910:                break;
                   1911:
1.225     djm      1912:        case sVersionAddendum:
1.262     dtucker  1913:                if (cp == NULL || *cp == '\0')
1.225     djm      1914:                        fatal("%.200s line %d: Missing argument.", filename,
                   1915:                            linenum);
                   1916:                len = strspn(cp, WHITESPACE);
                   1917:                if (*activep && options->version_addendum == NULL) {
                   1918:                        if (strcasecmp(cp + len, "none") == 0)
                   1919:                                options->version_addendum = xstrdup("");
                   1920:                        else if (strchr(cp + len, '\r') != NULL)
                   1921:                                fatal("%.200s line %d: Invalid argument",
                   1922:                                    filename, linenum);
                   1923:                        else
                   1924:                                options->version_addendum = xstrdup(cp + len);
                   1925:                }
                   1926:                return 0;
                   1927:
1.231     djm      1928:        case sAuthorizedKeysCommand:
1.255     jsg      1929:                if (cp == NULL)
                   1930:                        fatal("%.200s line %d: Missing argument.", filename,
                   1931:                            linenum);
1.231     djm      1932:                len = strspn(cp, WHITESPACE);
                   1933:                if (*activep && options->authorized_keys_command == NULL) {
                   1934:                        if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
                   1935:                                fatal("%.200s line %d: AuthorizedKeysCommand "
                   1936:                                    "must be an absolute path",
                   1937:                                    filename, linenum);
                   1938:                        options->authorized_keys_command = xstrdup(cp + len);
                   1939:                }
                   1940:                return 0;
                   1941:
                   1942:        case sAuthorizedKeysCommandUser:
                   1943:                charptr = &options->authorized_keys_command_user;
                   1944:
                   1945:                arg = strdelim(&cp);
1.255     jsg      1946:                if (!arg || *arg == '\0')
                   1947:                        fatal("%s line %d: missing AuthorizedKeysCommandUser "
                   1948:                            "argument.", filename, linenum);
1.231     djm      1949:                if (*activep && *charptr == NULL)
                   1950:                        *charptr = xstrdup(arg);
                   1951:                break;
                   1952:
1.270     djm      1953:        case sAuthorizedPrincipalsCommand:
                   1954:                if (cp == NULL)
                   1955:                        fatal("%.200s line %d: Missing argument.", filename,
                   1956:                            linenum);
                   1957:                len = strspn(cp, WHITESPACE);
                   1958:                if (*activep &&
                   1959:                    options->authorized_principals_command == NULL) {
                   1960:                        if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
                   1961:                                fatal("%.200s line %d: "
                   1962:                                    "AuthorizedPrincipalsCommand must be "
                   1963:                                    "an absolute path", filename, linenum);
                   1964:                        options->authorized_principals_command =
                   1965:                            xstrdup(cp + len);
                   1966:                }
                   1967:                return 0;
                   1968:
                   1969:        case sAuthorizedPrincipalsCommandUser:
                   1970:                charptr = &options->authorized_principals_command_user;
                   1971:
                   1972:                arg = strdelim(&cp);
                   1973:                if (!arg || *arg == '\0')
                   1974:                        fatal("%s line %d: missing "
                   1975:                            "AuthorizedPrincipalsCommandUser argument.",
                   1976:                            filename, linenum);
                   1977:                if (*activep && *charptr == NULL)
                   1978:                        *charptr = xstrdup(arg);
                   1979:                break;
                   1980:
1.232     djm      1981:        case sAuthenticationMethods:
1.268     djm      1982:                if (options->num_auth_methods == 0) {
1.291     djm      1983:                        value = 0; /* seen "any" pseudo-method */
1.328     djm      1984:                        value2 = 0; /* successfully parsed any method */
1.232     djm      1985:                        while ((arg = strdelim(&cp)) && *arg != '\0') {
1.291     djm      1986:                                if (strcmp(arg, "any") == 0) {
                   1987:                                        if (options->num_auth_methods > 0) {
                   1988:                                                fatal("%s line %d: \"any\" "
                   1989:                                                    "must appear alone in "
                   1990:                                                    "AuthenticationMethods",
                   1991:                                                    filename, linenum);
                   1992:                                        }
                   1993:                                        value = 1;
                   1994:                                } else if (value) {
                   1995:                                        fatal("%s line %d: \"any\" must appear "
                   1996:                                            "alone in AuthenticationMethods",
                   1997:                                            filename, linenum);
                   1998:                                } else if (auth2_methods_valid(arg, 0) != 0) {
1.232     djm      1999:                                        fatal("%s line %d: invalid "
                   2000:                                            "authentication method list.",
                   2001:                                            filename, linenum);
1.291     djm      2002:                                }
1.292     djm      2003:                                value2 = 1;
1.268     djm      2004:                                if (!*activep)
                   2005:                                        continue;
1.314     djm      2006:                                array_append(filename, linenum,
                   2007:                                    "AuthenticationMethods",
                   2008:                                    &options->auth_methods,
                   2009:                                    &options->num_auth_methods, arg);
1.232     djm      2010:                        }
1.292     djm      2011:                        if (value2 == 0) {
1.291     djm      2012:                                fatal("%s line %d: no AuthenticationMethods "
                   2013:                                    "specified", filename, linenum);
                   2014:                        }
1.232     djm      2015:                }
                   2016:                return 0;
                   2017:
1.251     millert  2018:        case sStreamLocalBindMask:
                   2019:                arg = strdelim(&cp);
                   2020:                if (!arg || *arg == '\0')
1.268     djm      2021:                        fatal("%s line %d: missing StreamLocalBindMask "
                   2022:                            "argument.", filename, linenum);
1.251     millert  2023:                /* Parse mode in octal format */
                   2024:                value = strtol(arg, &p, 8);
                   2025:                if (arg == p || value < 0 || value > 0777)
                   2026:                        fatal("%s line %d: Bad mask.", filename, linenum);
1.268     djm      2027:                if (*activep)
                   2028:                        options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1.251     millert  2029:                break;
                   2030:
                   2031:        case sStreamLocalBindUnlink:
                   2032:                intptr = &options->fwd_opts.streamlocal_bind_unlink;
                   2033:                goto parse_flag;
                   2034:
1.256     djm      2035:        case sFingerprintHash:
                   2036:                arg = strdelim(&cp);
                   2037:                if (!arg || *arg == '\0')
                   2038:                        fatal("%.200s line %d: Missing argument.",
                   2039:                            filename, linenum);
                   2040:                if ((value = ssh_digest_alg_by_name(arg)) == -1)
                   2041:                        fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
                   2042:                            filename, linenum, arg);
                   2043:                if (*activep)
                   2044:                        options->fingerprint_hash = value;
                   2045:                break;
                   2046:
1.309     djm      2047:        case sExposeAuthInfo:
                   2048:                intptr = &options->expose_userauth_info;
                   2049:                goto parse_flag;
                   2050:
1.316     djm      2051:        case sRDomain:
                   2052:                charptr = &options->routing_domain;
                   2053:                arg = strdelim(&cp);
                   2054:                if (!arg || *arg == '\0')
                   2055:                        fatal("%.200s line %d: Missing argument.",
                   2056:                            filename, linenum);
                   2057:                if (strcasecmp(arg, "none") != 0 && strcmp(arg, "%D") != 0 &&
                   2058:                    !valid_rdomain(arg))
                   2059:                        fatal("%s line %d: bad routing domain",
                   2060:                            filename, linenum);
                   2061:                if (*activep && *charptr == NULL)
                   2062:                        *charptr = xstrdup(arg);
1.321     dtucker  2063:                break;
1.316     djm      2064:
1.94      markus   2065:        case sDeprecated:
1.295     djm      2066:        case sIgnore:
1.121     jakob    2067:        case sUnsupported:
1.295     djm      2068:                do_log2(opcode == sIgnore ?
                   2069:                    SYSLOG_LEVEL_DEBUG2 : SYSLOG_LEVEL_INFO,
                   2070:                    "%s line %d: %s option %s", filename, linenum,
                   2071:                    opcode == sUnsupported ? "Unsupported" : "Deprecated", arg);
1.94      markus   2072:                while (arg)
                   2073:                    arg = strdelim(&cp);
                   2074:                break;
                   2075:
                   2076:        default:
                   2077:                fatal("%s line %d: Missing handler for opcode %s (%d)",
                   2078:                    filename, linenum, arg, opcode);
                   2079:        }
                   2080:        if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
                   2081:                fatal("%s line %d: garbage at end of line; \"%.200s\".",
                   2082:                    filename, linenum, arg);
                   2083:        return 0;
                   2084: }
                   2085:
                   2086: /* Reads the server configuration file. */
1.25      markus   2087:
1.94      markus   2088: void
1.134     djm      2089: load_server_config(const char *filename, Buffer *conf)
1.94      markus   2090: {
1.331     markus   2091:        char *line = NULL, *cp;
                   2092:        size_t linesize = 0;
1.94      markus   2093:        FILE *f;
1.229     dtucker  2094:        int lineno = 0;
1.81      stevesk  2095:
1.134     djm      2096:        debug2("%s: filename %s", __func__, filename);
                   2097:        if ((f = fopen(filename, "r")) == NULL) {
1.94      markus   2098:                perror(filename);
                   2099:                exit(1);
                   2100:        }
1.134     djm      2101:        buffer_clear(conf);
1.331     markus   2102:        while (getline(&line, &linesize, f) != -1) {
1.229     dtucker  2103:                lineno++;
1.134     djm      2104:                /*
                   2105:                 * Trim out comments and strip whitespace
1.135     deraadt  2106:                 * NB - preserve newlines, they are needed to reproduce
1.134     djm      2107:                 * line numbers later for error messages
                   2108:                 */
                   2109:                if ((cp = strchr(line, '#')) != NULL)
                   2110:                        memcpy(cp, "\n", 2);
                   2111:                cp = line + strspn(line, " \t\r");
                   2112:
                   2113:                buffer_append(conf, cp, strlen(cp));
                   2114:        }
1.331     markus   2115:        free(line);
1.134     djm      2116:        buffer_append(conf, "\0", 1);
                   2117:        fclose(f);
                   2118:        debug2("%s: done config len = %d", __func__, buffer_len(conf));
                   2119: }
                   2120:
                   2121: void
1.226     dtucker  2122: parse_server_match_config(ServerOptions *options,
                   2123:    struct connection_info *connectinfo)
1.153     dtucker  2124: {
                   2125:        ServerOptions mo;
                   2126:
                   2127:        initialize_server_options(&mo);
1.226     dtucker  2128:        parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
1.168     dtucker  2129:        copy_set_server_options(options, &mo, 0);
1.153     dtucker  2130: }
                   2131:
1.226     dtucker  2132: int parse_server_match_testspec(struct connection_info *ci, char *spec)
                   2133: {
                   2134:        char *p;
                   2135:
                   2136:        while ((p = strsep(&spec, ",")) && *p != '\0') {
                   2137:                if (strncmp(p, "addr=", 5) == 0) {
                   2138:                        ci->address = xstrdup(p + 5);
                   2139:                } else if (strncmp(p, "host=", 5) == 0) {
                   2140:                        ci->host = xstrdup(p + 5);
                   2141:                } else if (strncmp(p, "user=", 5) == 0) {
                   2142:                        ci->user = xstrdup(p + 5);
                   2143:                } else if (strncmp(p, "laddr=", 6) == 0) {
                   2144:                        ci->laddress = xstrdup(p + 6);
1.317     djm      2145:                } else if (strncmp(p, "rdomain=", 8) == 0) {
                   2146:                        ci->rdomain = xstrdup(p + 8);
1.226     dtucker  2147:                } else if (strncmp(p, "lport=", 6) == 0) {
                   2148:                        ci->lport = a2port(p + 6);
                   2149:                        if (ci->lport == -1) {
                   2150:                                fprintf(stderr, "Invalid port '%s' in test mode"
                   2151:                                   " specification %s\n", p+6, p);
                   2152:                                return -1;
                   2153:                        }
                   2154:                } else {
                   2155:                        fprintf(stderr, "Invalid test mode specification %s\n",
                   2156:                           p);
                   2157:                        return -1;
                   2158:                }
                   2159:        }
                   2160:        return 0;
                   2161: }
                   2162:
1.168     dtucker  2163: /*
                   2164:  * Copy any supported values that are set.
                   2165:  *
1.195     jj       2166:  * If the preauth flag is set, we do not bother copying the string or
1.168     dtucker  2167:  * array values that are not used pre-authentication, because any that we
1.328     djm      2168:  * do use must be explicitly sent in mm_getpwnamallow().
1.168     dtucker  2169:  */
1.153     dtucker  2170: void
1.168     dtucker  2171: copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
1.153     dtucker  2172: {
1.247     djm      2173: #define M_CP_INTOPT(n) do {\
                   2174:        if (src->n != -1) \
                   2175:                dst->n = src->n; \
                   2176: } while (0)
                   2177:
1.168     dtucker  2178:        M_CP_INTOPT(password_authentication);
                   2179:        M_CP_INTOPT(gss_authentication);
                   2180:        M_CP_INTOPT(pubkey_authentication);
                   2181:        M_CP_INTOPT(kerberos_authentication);
                   2182:        M_CP_INTOPT(hostbased_authentication);
1.209     djm      2183:        M_CP_INTOPT(hostbased_uses_name_from_packet_only);
1.168     dtucker  2184:        M_CP_INTOPT(kbd_interactive_authentication);
1.175     dtucker  2185:        M_CP_INTOPT(permit_root_login);
1.188     djm      2186:        M_CP_INTOPT(permit_empty_passwd);
1.168     dtucker  2187:
                   2188:        M_CP_INTOPT(allow_tcp_forwarding);
1.251     millert  2189:        M_CP_INTOPT(allow_streamlocal_forwarding);
1.178     pyr      2190:        M_CP_INTOPT(allow_agent_forwarding);
1.301     djm      2191:        M_CP_INTOPT(disable_forwarding);
1.309     djm      2192:        M_CP_INTOPT(expose_userauth_info);
1.209     djm      2193:        M_CP_INTOPT(permit_tun);
1.251     millert  2194:        M_CP_INTOPT(fwd_opts.gateway_ports);
1.289     djm      2195:        M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
1.168     dtucker  2196:        M_CP_INTOPT(x11_display_offset);
                   2197:        M_CP_INTOPT(x11_forwarding);
                   2198:        M_CP_INTOPT(x11_use_localhost);
1.244     djm      2199:        M_CP_INTOPT(permit_tty);
1.250     djm      2200:        M_CP_INTOPT(permit_user_rc);
1.180     djm      2201:        M_CP_INTOPT(max_sessions);
1.184     dtucker  2202:        M_CP_INTOPT(max_authtries);
1.300     markus   2203:        M_CP_INTOPT(client_alive_count_max);
                   2204:        M_CP_INTOPT(client_alive_interval);
1.213     djm      2205:        M_CP_INTOPT(ip_qos_interactive);
                   2206:        M_CP_INTOPT(ip_qos_bulk);
1.235     dtucker  2207:        M_CP_INTOPT(rekey_limit);
                   2208:        M_CP_INTOPT(rekey_interval);
1.308     djm      2209:        M_CP_INTOPT(log_level);
1.289     djm      2210:
                   2211:        /*
                   2212:         * The bind_mask is a mode_t that may be unsigned, so we can't use
                   2213:         * M_CP_INTOPT - it does a signed comparison that causes compiler
                   2214:         * warnings.
                   2215:         */
1.290     dtucker  2216:        if (src->fwd_opts.streamlocal_bind_mask != (mode_t)-1) {
1.289     djm      2217:                dst->fwd_opts.streamlocal_bind_mask =
                   2218:                    src->fwd_opts.streamlocal_bind_mask;
                   2219:        }
1.247     djm      2220:
                   2221:        /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
                   2222: #define M_CP_STROPT(n) do {\
                   2223:        if (src->n != NULL && dst->n != src->n) { \
                   2224:                free(dst->n); \
                   2225:                dst->n = src->n; \
                   2226:        } \
                   2227: } while(0)
1.314     djm      2228: #define M_CP_STRARRAYOPT(s, num_s) do {\
                   2229:        u_int i; \
                   2230:        if (src->num_s != 0) { \
                   2231:                for (i = 0; i < dst->num_s; i++) \
                   2232:                        free(dst->s[i]); \
                   2233:                free(dst->s); \
                   2234:                dst->s = xcalloc(src->num_s, sizeof(*dst->s)); \
                   2235:                for (i = 0; i < src->num_s; i++) \
                   2236:                        dst->s[i] = xstrdup(src->s[i]); \
                   2237:                dst->num_s = src->num_s; \
1.312     djm      2238:        } \
                   2239: } while(0)
1.168     dtucker  2240:
1.218     djm      2241:        /* See comment in servconf.h */
                   2242:        COPY_MATCH_STRING_OPTS();
1.282     djm      2243:
                   2244:        /* Arguments that accept '+...' need to be expanded */
                   2245:        assemble_algorithms(dst);
1.216     djm      2246:
1.217     dtucker  2247:        /*
                   2248:         * The only things that should be below this point are string options
                   2249:         * which are only used after authentication.
                   2250:         */
1.168     dtucker  2251:        if (preauth)
                   2252:                return;
1.219     djm      2253:
1.283     djm      2254:        /* These options may be "none" to clear a global setting */
1.168     dtucker  2255:        M_CP_STROPT(adm_forced_command);
1.283     djm      2256:        if (option_clear_or_none(dst->adm_forced_command)) {
                   2257:                free(dst->adm_forced_command);
                   2258:                dst->adm_forced_command = NULL;
                   2259:        }
1.176     djm      2260:        M_CP_STROPT(chroot_directory);
1.283     djm      2261:        if (option_clear_or_none(dst->chroot_directory)) {
                   2262:                free(dst->chroot_directory);
                   2263:                dst->chroot_directory = NULL;
                   2264:        }
1.153     dtucker  2265: }
1.168     dtucker  2266:
                   2267: #undef M_CP_INTOPT
                   2268: #undef M_CP_STROPT
1.219     djm      2269: #undef M_CP_STRARRAYOPT
1.153     dtucker  2270:
                   2271: void
                   2272: parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1.226     dtucker  2273:     struct connection_info *connectinfo)
1.134     djm      2274: {
1.153     dtucker  2275:        int active, linenum, bad_options = 0;
1.136     dtucker  2276:        char *cp, *obuf, *cbuf;
1.134     djm      2277:
                   2278:        debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
                   2279:
1.287     djm      2280:        if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
                   2281:                fatal("%s: sshbuf_dup_string failed", __func__);
1.226     dtucker  2282:        active = connectinfo ? 0 : 1;
1.137     dtucker  2283:        linenum = 1;
1.140     deraadt  2284:        while ((cp = strsep(&cbuf, "\n")) != NULL) {
1.134     djm      2285:                if (process_server_config_line(options, cp, filename,
1.226     dtucker  2286:                    linenum++, &active, connectinfo) != 0)
1.94      markus   2287:                        bad_options++;
1.1       deraadt  2288:        }
1.239     djm      2289:        free(obuf);
1.78      stevesk  2290:        if (bad_options > 0)
                   2291:                fatal("%s: terminating, %d bad configuration options",
                   2292:                    filename, bad_options);
1.266     dtucker  2293:        process_queued_listen_addrs(options);
1.182     dtucker  2294: }
                   2295:
                   2296: static const char *
1.221     djm      2297: fmt_multistate_int(int val, const struct multistate *m)
                   2298: {
                   2299:        u_int i;
                   2300:
                   2301:        for (i = 0; m[i].key != NULL; i++) {
                   2302:                if (m[i].value == val)
                   2303:                        return m[i].key;
                   2304:        }
                   2305:        return "UNKNOWN";
                   2306: }
                   2307:
                   2308: static const char *
1.182     dtucker  2309: fmt_intarg(ServerOpCodes code, int val)
                   2310: {
1.221     djm      2311:        if (val == -1)
                   2312:                return "unset";
                   2313:        switch (code) {
                   2314:        case sAddressFamily:
                   2315:                return fmt_multistate_int(val, multistate_addressfamily);
                   2316:        case sPermitRootLogin:
                   2317:                return fmt_multistate_int(val, multistate_permitrootlogin);
                   2318:        case sGatewayPorts:
                   2319:                return fmt_multistate_int(val, multistate_gatewayports);
                   2320:        case sCompression:
                   2321:                return fmt_multistate_int(val, multistate_compression);
1.233     djm      2322:        case sAllowTcpForwarding:
                   2323:                return fmt_multistate_int(val, multistate_tcpfwd);
1.251     millert  2324:        case sAllowStreamLocalForwarding:
                   2325:                return fmt_multistate_int(val, multistate_tcpfwd);
1.256     djm      2326:        case sFingerprintHash:
                   2327:                return ssh_digest_alg_name(val);
1.221     djm      2328:        default:
                   2329:                switch (val) {
                   2330:                case 0:
                   2331:                        return "no";
                   2332:                case 1:
                   2333:                        return "yes";
                   2334:                default:
                   2335:                        return "UNKNOWN";
                   2336:                }
1.182     dtucker  2337:        }
                   2338: }
                   2339:
                   2340: static void
                   2341: dump_cfg_int(ServerOpCodes code, int val)
                   2342: {
                   2343:        printf("%s %d\n", lookup_opcode_name(code), val);
                   2344: }
                   2345:
                   2346: static void
1.263     dtucker  2347: dump_cfg_oct(ServerOpCodes code, int val)
                   2348: {
                   2349:        printf("%s 0%o\n", lookup_opcode_name(code), val);
                   2350: }
                   2351:
                   2352: static void
1.182     dtucker  2353: dump_cfg_fmtint(ServerOpCodes code, int val)
                   2354: {
                   2355:        printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
                   2356: }
                   2357:
                   2358: static void
                   2359: dump_cfg_string(ServerOpCodes code, const char *val)
                   2360: {
1.257     djm      2361:        printf("%s %s\n", lookup_opcode_name(code),
                   2362:            val == NULL ? "none" : val);
1.182     dtucker  2363: }
                   2364:
                   2365: static void
                   2366: dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
                   2367: {
                   2368:        u_int i;
                   2369:
                   2370:        for (i = 0; i < count; i++)
1.219     djm      2371:                printf("%s %s\n", lookup_opcode_name(code), vals[i]);
                   2372: }
                   2373:
                   2374: static void
                   2375: dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
                   2376: {
                   2377:        u_int i;
                   2378:
1.291     djm      2379:        if (count <= 0 && code != sAuthenticationMethods)
1.261     dtucker  2380:                return;
1.219     djm      2381:        printf("%s", lookup_opcode_name(code));
                   2382:        for (i = 0; i < count; i++)
                   2383:                printf(" %s",  vals[i]);
1.291     djm      2384:        if (code == sAuthenticationMethods && count == 0)
                   2385:                printf(" any");
1.219     djm      2386:        printf("\n");
1.182     dtucker  2387: }
                   2388:
1.315     djm      2389: static char *
                   2390: format_listen_addrs(struct listenaddr *la)
1.182     dtucker  2391: {
1.315     djm      2392:        int r;
1.182     dtucker  2393:        struct addrinfo *ai;
1.315     djm      2394:        char addr[NI_MAXHOST], port[NI_MAXSERV];
1.263     dtucker  2395:        char *laddr1 = xstrdup(""), *laddr2 = NULL;
1.182     dtucker  2396:
1.263     dtucker  2397:        /*
                   2398:         * ListenAddress must be after Port.  add_one_listen_addr pushes
                   2399:         * addresses onto a stack, so to maintain ordering we need to
                   2400:         * print these in reverse order.
                   2401:         */
1.315     djm      2402:        for (ai = la->addrs; ai; ai = ai->ai_next) {
                   2403:                if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1.182     dtucker  2404:                    sizeof(addr), port, sizeof(port),
                   2405:                    NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1.315     djm      2406:                        error("getnameinfo: %.100s", ssh_gai_strerror(r));
                   2407:                        continue;
                   2408:                }
                   2409:                laddr2 = laddr1;
                   2410:                if (ai->ai_family == AF_INET6) {
                   2411:                        xasprintf(&laddr1, "listenaddress [%s]:%s%s%s\n%s",
                   2412:                            addr, port,
                   2413:                            la->rdomain == NULL ? "" : " rdomain ",
                   2414:                            la->rdomain == NULL ? "" : la->rdomain,
                   2415:                            laddr2);
1.182     dtucker  2416:                } else {
1.315     djm      2417:                        xasprintf(&laddr1, "listenaddress %s:%s%s%s\n%s",
                   2418:                            addr, port,
                   2419:                            la->rdomain == NULL ? "" : " rdomain ",
                   2420:                            la->rdomain == NULL ? "" : la->rdomain,
                   2421:                            laddr2);
1.182     dtucker  2422:                }
1.315     djm      2423:                free(laddr2);
                   2424:        }
                   2425:        return laddr1;
                   2426: }
                   2427:
                   2428: void
                   2429: dump_config(ServerOptions *o)
                   2430: {
                   2431:        char *s;
                   2432:        u_int i;
                   2433:
                   2434:        /* these are usually at the top of the config */
                   2435:        for (i = 0; i < o->num_ports; i++)
                   2436:                printf("port %d\n", o->ports[i]);
                   2437:        dump_cfg_fmtint(sAddressFamily, o->address_family);
                   2438:
                   2439:        for (i = 0; i < o->num_listen_addrs; i++) {
                   2440:                s = format_listen_addrs(&o->listen_addrs[i]);
                   2441:                printf("%s", s);
                   2442:                free(s);
1.182     dtucker  2443:        }
                   2444:
                   2445:        /* integer arguments */
                   2446:        dump_cfg_int(sLoginGraceTime, o->login_grace_time);
                   2447:        dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
                   2448:        dump_cfg_int(sMaxAuthTries, o->max_authtries);
1.189     djm      2449:        dump_cfg_int(sMaxSessions, o->max_sessions);
1.182     dtucker  2450:        dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
                   2451:        dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1.263     dtucker  2452:        dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
1.182     dtucker  2453:
                   2454:        /* formatted integer arguments */
                   2455:        dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
                   2456:        dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
                   2457:        dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
                   2458:        dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
                   2459:        dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
                   2460:            o->hostbased_uses_name_from_packet_only);
                   2461:        dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
1.187     djm      2462: #ifdef KRB5
1.182     dtucker  2463:        dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
                   2464:        dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
                   2465:        dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
                   2466:        dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
1.187     djm      2467: #endif
                   2468: #ifdef GSSAPI
1.182     dtucker  2469:        dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
                   2470:        dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
1.187     djm      2471: #endif
1.182     dtucker  2472:        dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
                   2473:        dump_cfg_fmtint(sKbdInteractiveAuthentication,
                   2474:            o->kbd_interactive_authentication);
                   2475:        dump_cfg_fmtint(sChallengeResponseAuthentication,
                   2476:            o->challenge_response_authentication);
                   2477:        dump_cfg_fmtint(sPrintMotd, o->print_motd);
                   2478:        dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
                   2479:        dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
                   2480:        dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1.244     djm      2481:        dump_cfg_fmtint(sPermitTTY, o->permit_tty);
1.250     djm      2482:        dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
1.182     dtucker  2483:        dump_cfg_fmtint(sStrictModes, o->strict_modes);
                   2484:        dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
                   2485:        dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
                   2486:        dump_cfg_fmtint(sCompression, o->compression);
1.251     millert  2487:        dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
1.182     dtucker  2488:        dump_cfg_fmtint(sUseDNS, o->use_dns);
                   2489:        dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1.261     dtucker  2490:        dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
1.301     djm      2491:        dump_cfg_fmtint(sDisableForwarding, o->disable_forwarding);
1.251     millert  2492:        dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
1.288     djm      2493:        dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
1.256     djm      2494:        dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
1.309     djm      2495:        dump_cfg_fmtint(sExposeAuthInfo, o->expose_userauth_info);
1.182     dtucker  2496:
                   2497:        /* string arguments */
                   2498:        dump_cfg_string(sPidFile, o->pid_file);
                   2499:        dump_cfg_string(sXAuthLocation, o->xauth_location);
1.252     djm      2500:        dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
                   2501:        dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
1.182     dtucker  2502:        dump_cfg_string(sBanner, o->banner);
                   2503:        dump_cfg_string(sForceCommand, o->adm_forced_command);
1.201     dtucker  2504:        dump_cfg_string(sChrootDirectory, o->chroot_directory);
1.204     djm      2505:        dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
                   2506:        dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
1.208     djm      2507:        dump_cfg_string(sAuthorizedPrincipalsFile,
                   2508:            o->authorized_principals_file);
1.261     dtucker  2509:        dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
                   2510:            ? "none" : o->version_addendum);
1.231     djm      2511:        dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
                   2512:        dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
1.270     djm      2513:        dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
                   2514:        dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
1.240     markus   2515:        dump_cfg_string(sHostKeyAgent, o->host_key_agent);
1.252     djm      2516:        dump_cfg_string(sKexAlgorithms,
1.253     djm      2517:            o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
1.258     djm      2518:        dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
                   2519:            o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
1.276     markus   2520:        dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
                   2521:            o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
1.258     djm      2522:        dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
                   2523:            o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
1.316     djm      2524:        dump_cfg_string(sRDomain, o->routing_domain);
1.182     dtucker  2525:
                   2526:        /* string arguments requiring a lookup */
                   2527:        dump_cfg_string(sLogLevel, log_level_name(o->log_level));
                   2528:        dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
                   2529:
                   2530:        /* string array arguments */
1.219     djm      2531:        dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
                   2532:            o->authorized_keys_files);
1.182     dtucker  2533:        dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
                   2534:             o->host_key_files);
1.261     dtucker  2535:        dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
1.203     djm      2536:             o->host_cert_files);
1.182     dtucker  2537:        dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
                   2538:        dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
                   2539:        dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
                   2540:        dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
                   2541:        dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1.332     djm      2542:        dump_cfg_strarray(sSetEnv, o->num_setenv, o->setenv);
1.232     djm      2543:        dump_cfg_strarray_oneline(sAuthenticationMethods,
                   2544:            o->num_auth_methods, o->auth_methods);
1.182     dtucker  2545:
                   2546:        /* other arguments */
                   2547:        for (i = 0; i < o->num_subsystems; i++)
                   2548:                printf("subsystem %s %s\n", o->subsystem_name[i],
                   2549:                    o->subsystem_args[i]);
                   2550:
                   2551:        printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
                   2552:            o->max_startups_rate, o->max_startups);
                   2553:
1.318     djm      2554:        s = NULL;
                   2555:        for (i = 0; tunmode_desc[i].val != -1; i++) {
1.182     dtucker  2556:                if (tunmode_desc[i].val == o->permit_tun) {
                   2557:                        s = tunmode_desc[i].text;
                   2558:                        break;
                   2559:                }
1.318     djm      2560:        }
1.182     dtucker  2561:        dump_cfg_string(sPermitTunnel, s);
1.213     djm      2562:
1.214     stevesk  2563:        printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
                   2564:        printf("%s\n", iptos2str(o->ip_qos_bulk));
1.235     dtucker  2565:
1.284     dtucker  2566:        printf("rekeylimit %llu %d\n", (unsigned long long)o->rekey_limit,
1.241     djm      2567:            o->rekey_interval);
1.182     dtucker  2568:
1.310     djm      2569:        printf("permitopen");
                   2570:        if (o->num_permitted_opens == 0)
                   2571:                printf(" any");
                   2572:        else {
                   2573:                for (i = 0; i < o->num_permitted_opens; i++)
                   2574:                        printf(" %s", o->permitted_opens[i]);
1.329     djm      2575:        }
                   2576:        printf("\n");
1.330     djm      2577:        printf("permitlisten");
                   2578:        if (o->num_permitted_listens == 0)
1.329     djm      2579:                printf(" any");
                   2580:        else {
1.330     djm      2581:                for (i = 0; i < o->num_permitted_listens; i++)
                   2582:                        printf(" %s", o->permitted_listens[i]);
1.310     djm      2583:        }
                   2584:        printf("\n");
1.334   ! djm      2585:
        !          2586:        if (o->permit_user_env_whitelist == NULL) {
        !          2587:                dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
        !          2588:        } else {
        !          2589:                printf("permituserenvironment %s\n",
        !          2590:                    o->permit_user_env_whitelist);
        !          2591:        }
        !          2592:
1.1       deraadt  2593: }