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

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