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

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