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

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