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

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