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

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