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

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