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

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