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

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