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

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