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

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