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

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