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

1.163   ! stevesk     1: /* $OpenBSD: servconf.c,v 1.162 2006/08/01 23:22:47 stevesk Exp $ */
1.1       deraadt     2: /*
1.26      deraadt     3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
1.34      markus      5:  *
1.51      deraadt     6:  * As far as I am concerned, the code I have written for this software
                      7:  * can be used freely for any purpose.  Any derived versions of this
                      8:  * software must be clearly marked as such, and if the derived work is
                      9:  * incompatible with the protocol description in the RFC file, it must be
                     10:  * called by a name other than "ssh" or "Secure Shell".
1.26      deraadt    11:  */
1.1       deraadt    12:
                     13: #include "includes.h"
1.152     stevesk    14:
                     15: #include <sys/types.h>
                     16: #include <sys/socket.h>
1.154     stevesk    17:
                     18: #include <netdb.h>
1.162     stevesk    19: #include <stdio.h>
1.161     stevesk    20: #include <stdlib.h>
1.160     stevesk    21: #include <string.h>
1.155     stevesk    22: #include <unistd.h>
1.1       deraadt    23:
                     24: #include "ssh.h"
1.62      markus     25: #include "log.h"
1.1       deraadt    26: #include "servconf.h"
                     27: #include "xmalloc.h"
1.33      markus     28: #include "compat.h"
1.60      markus     29: #include "pathnames.h"
1.62      markus     30: #include "misc.h"
                     31: #include "cipher.h"
1.66      markus     32: #include "kex.h"
                     33: #include "mac.h"
1.153     dtucker    34: #include "match.h"
1.156     dtucker    35: #include "channels.h"
1.1       deraadt    36:
1.84      itojun     37: static void add_listen_addr(ServerOptions *, char *, u_short);
                     38: static void add_one_listen_addr(ServerOptions *, char *, u_short);
1.29      markus     39:
1.102     provos     40: /* Use of privilege separation or not */
                     41: extern int use_privsep;
1.153     dtucker    42: extern Buffer cfg;
1.62      markus     43:
1.1       deraadt    44: /* Initializes the server options to their default values. */
                     45:
1.34      markus     46: void
1.25      markus     47: initialize_server_options(ServerOptions *options)
1.1       deraadt    48: {
1.25      markus     49:        memset(options, 0, sizeof(*options));
1.29      markus     50:        options->num_ports = 0;
                     51:        options->ports_from_cmdline = 0;
                     52:        options->listen_addrs = NULL;
1.138     djm        53:        options->address_family = -1;
1.54      markus     54:        options->num_host_key_files = 0;
1.36      markus     55:        options->pid_file = NULL;
1.25      markus     56:        options->server_key_bits = -1;
                     57:        options->login_grace_time = -1;
                     58:        options->key_regeneration_time = -1;
1.67      markus     59:        options->permit_root_login = PERMIT_NOT_SET;
1.25      markus     60:        options->ignore_rhosts = -1;
                     61:        options->ignore_user_known_hosts = -1;
                     62:        options->print_motd = -1;
1.72      stevesk    63:        options->print_lastlog = -1;
1.25      markus     64:        options->x11_forwarding = -1;
                     65:        options->x11_display_offset = -1;
1.99      stevesk    66:        options->x11_use_localhost = -1;
1.42      markus     67:        options->xauth_location = NULL;
1.25      markus     68:        options->strict_modes = -1;
1.129     markus     69:        options->tcp_keep_alive = -1;
1.101     markus     70:        options->log_facility = SYSLOG_FACILITY_NOT_SET;
                     71:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.25      markus     72:        options->rhosts_rsa_authentication = -1;
1.75      markus     73:        options->hostbased_authentication = -1;
                     74:        options->hostbased_uses_name_from_packet_only = -1;
1.25      markus     75:        options->rsa_authentication = -1;
1.54      markus     76:        options->pubkey_authentication = -1;
1.25      markus     77:        options->kerberos_authentication = -1;
                     78:        options->kerberos_or_local_passwd = -1;
                     79:        options->kerberos_ticket_cleanup = -1;
1.130     jakob      80:        options->kerberos_get_afs_token = -1;
1.125     markus     81:        options->gss_authentication=-1;
                     82:        options->gss_cleanup_creds = -1;
1.25      markus     83:        options->password_authentication = -1;
1.52      markus     84:        options->kbd_interactive_authentication = -1;
1.80      markus     85:        options->challenge_response_authentication = -1;
1.25      markus     86:        options->permit_empty_passwd = -1;
1.113     markus     87:        options->permit_user_env = -1;
1.25      markus     88:        options->use_login = -1;
1.111     markus     89:        options->compression = -1;
1.53      markus     90:        options->allow_tcp_forwarding = -1;
1.25      markus     91:        options->num_allow_users = 0;
                     92:        options->num_deny_users = 0;
                     93:        options->num_allow_groups = 0;
                     94:        options->num_deny_groups = 0;
1.33      markus     95:        options->ciphers = NULL;
1.66      markus     96:        options->macs = NULL;
1.33      markus     97:        options->protocol = SSH_PROTO_UNKNOWN;
1.38      markus     98:        options->gateway_ports = -1;
1.43      jakob      99:        options->num_subsystems = 0;
1.50      markus    100:        options->max_startups_begin = -1;
                    101:        options->max_startups_rate = -1;
1.46      markus    102:        options->max_startups = -1;
1.133     dtucker   103:        options->max_authtries = -1;
1.57      markus    104:        options->banner = NULL;
1.122     markus    105:        options->use_dns = -1;
1.77      beck      106:        options->client_alive_interval = -1;
                    107:        options->client_alive_count_max = -1;
1.82      markus    108:        options->authorized_keys_file = NULL;
                    109:        options->authorized_keys_file2 = NULL;
1.131     djm       110:        options->num_accept_env = 0;
1.145     reyk      111:        options->permit_tun = -1;
1.159     dtucker   112:        options->num_permitted_opens = -1;
1.158     dtucker   113:        options->adm_forced_command = NULL;
1.1       deraadt   114: }
                    115:
1.34      markus    116: void
1.25      markus    117: fill_default_server_options(ServerOptions *options)
1.1       deraadt   118: {
1.54      markus    119:        if (options->protocol == SSH_PROTO_UNKNOWN)
                    120:                options->protocol = SSH_PROTO_1|SSH_PROTO_2;
                    121:        if (options->num_host_key_files == 0) {
                    122:                /* fill default hostkeys for protocols */
                    123:                if (options->protocol & SSH_PROTO_1)
1.97      stevesk   124:                        options->host_key_files[options->num_host_key_files++] =
                    125:                            _PATH_HOST_KEY_FILE;
                    126:                if (options->protocol & SSH_PROTO_2) {
                    127:                        options->host_key_files[options->num_host_key_files++] =
                    128:                            _PATH_HOST_RSA_KEY_FILE;
                    129:                        options->host_key_files[options->num_host_key_files++] =
                    130:                            _PATH_HOST_DSA_KEY_FILE;
                    131:                }
1.54      markus    132:        }
1.29      markus    133:        if (options->num_ports == 0)
                    134:                options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
                    135:        if (options->listen_addrs == NULL)
1.76      stevesk   136:                add_listen_addr(options, NULL, 0);
1.36      markus    137:        if (options->pid_file == NULL)
1.60      markus    138:                options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
1.25      markus    139:        if (options->server_key_bits == -1)
                    140:                options->server_key_bits = 768;
                    141:        if (options->login_grace_time == -1)
1.115     stevesk   142:                options->login_grace_time = 120;
1.25      markus    143:        if (options->key_regeneration_time == -1)
                    144:                options->key_regeneration_time = 3600;
1.67      markus    145:        if (options->permit_root_login == PERMIT_NOT_SET)
                    146:                options->permit_root_login = PERMIT_YES;
1.25      markus    147:        if (options->ignore_rhosts == -1)
1.30      markus    148:                options->ignore_rhosts = 1;
1.25      markus    149:        if (options->ignore_user_known_hosts == -1)
                    150:                options->ignore_user_known_hosts = 0;
                    151:        if (options->print_motd == -1)
                    152:                options->print_motd = 1;
1.72      stevesk   153:        if (options->print_lastlog == -1)
                    154:                options->print_lastlog = 1;
1.25      markus    155:        if (options->x11_forwarding == -1)
1.30      markus    156:                options->x11_forwarding = 0;
1.25      markus    157:        if (options->x11_display_offset == -1)
1.30      markus    158:                options->x11_display_offset = 10;
1.99      stevesk   159:        if (options->x11_use_localhost == -1)
                    160:                options->x11_use_localhost = 1;
1.42      markus    161:        if (options->xauth_location == NULL)
1.83      markus    162:                options->xauth_location = _PATH_XAUTH;
1.25      markus    163:        if (options->strict_modes == -1)
                    164:                options->strict_modes = 1;
1.129     markus    165:        if (options->tcp_keep_alive == -1)
                    166:                options->tcp_keep_alive = 1;
1.101     markus    167:        if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
1.25      markus    168:                options->log_facility = SYSLOG_FACILITY_AUTH;
1.101     markus    169:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.58      markus    170:                options->log_level = SYSLOG_LEVEL_INFO;
1.25      markus    171:        if (options->rhosts_rsa_authentication == -1)
1.30      markus    172:                options->rhosts_rsa_authentication = 0;
1.75      markus    173:        if (options->hostbased_authentication == -1)
                    174:                options->hostbased_authentication = 0;
                    175:        if (options->hostbased_uses_name_from_packet_only == -1)
                    176:                options->hostbased_uses_name_from_packet_only = 0;
1.25      markus    177:        if (options->rsa_authentication == -1)
                    178:                options->rsa_authentication = 1;
1.54      markus    179:        if (options->pubkey_authentication == -1)
                    180:                options->pubkey_authentication = 1;
1.25      markus    181:        if (options->kerberos_authentication == -1)
1.107     markus    182:                options->kerberos_authentication = 0;
1.25      markus    183:        if (options->kerberos_or_local_passwd == -1)
                    184:                options->kerberos_or_local_passwd = 1;
                    185:        if (options->kerberos_ticket_cleanup == -1)
                    186:                options->kerberos_ticket_cleanup = 1;
1.130     jakob     187:        if (options->kerberos_get_afs_token == -1)
                    188:                options->kerberos_get_afs_token = 0;
1.125     markus    189:        if (options->gss_authentication == -1)
                    190:                options->gss_authentication = 0;
                    191:        if (options->gss_cleanup_creds == -1)
                    192:                options->gss_cleanup_creds = 1;
1.25      markus    193:        if (options->password_authentication == -1)
                    194:                options->password_authentication = 1;
1.52      markus    195:        if (options->kbd_interactive_authentication == -1)
                    196:                options->kbd_interactive_authentication = 0;
1.80      markus    197:        if (options->challenge_response_authentication == -1)
                    198:                options->challenge_response_authentication = 1;
1.25      markus    199:        if (options->permit_empty_passwd == -1)
1.30      markus    200:                options->permit_empty_passwd = 0;
1.113     markus    201:        if (options->permit_user_env == -1)
                    202:                options->permit_user_env = 0;
1.25      markus    203:        if (options->use_login == -1)
                    204:                options->use_login = 0;
1.111     markus    205:        if (options->compression == -1)
1.143     markus    206:                options->compression = COMP_DELAYED;
1.53      markus    207:        if (options->allow_tcp_forwarding == -1)
                    208:                options->allow_tcp_forwarding = 1;
1.38      markus    209:        if (options->gateway_ports == -1)
                    210:                options->gateway_ports = 0;
1.46      markus    211:        if (options->max_startups == -1)
                    212:                options->max_startups = 10;
1.50      markus    213:        if (options->max_startups_rate == -1)
                    214:                options->max_startups_rate = 100;               /* 100% */
                    215:        if (options->max_startups_begin == -1)
                    216:                options->max_startups_begin = options->max_startups;
1.133     dtucker   217:        if (options->max_authtries == -1)
                    218:                options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
1.122     markus    219:        if (options->use_dns == -1)
                    220:                options->use_dns = 1;
1.77      beck      221:        if (options->client_alive_interval == -1)
1.95      deraadt   222:                options->client_alive_interval = 0;
1.77      beck      223:        if (options->client_alive_count_max == -1)
                    224:                options->client_alive_count_max = 3;
1.90      markus    225:        if (options->authorized_keys_file2 == NULL) {
                    226:                /* authorized_keys_file2 falls back to authorized_keys_file */
                    227:                if (options->authorized_keys_file != NULL)
                    228:                        options->authorized_keys_file2 = options->authorized_keys_file;
                    229:                else
                    230:                        options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
                    231:        }
                    232:        if (options->authorized_keys_file == NULL)
                    233:                options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
1.145     reyk      234:        if (options->permit_tun == -1)
1.146     reyk      235:                options->permit_tun = SSH_TUNMODE_NO;
1.102     provos    236:
1.110     markus    237:        /* Turn privilege separation on by default */
1.102     provos    238:        if (use_privsep == -1)
1.110     markus    239:                use_privsep = 1;
1.1       deraadt   240: }
                    241:
                    242: /* Keyword tokens. */
1.25      markus    243: typedef enum {
                    244:        sBadOption,             /* == unknown option */
                    245:        sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
                    246:        sPermitRootLogin, sLogFacility, sLogLevel,
1.124     markus    247:        sRhostsRSAAuthentication, sRSAAuthentication,
1.25      markus    248:        sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
1.130     jakob     249:        sKerberosGetAFSToken,
1.123     markus    250:        sKerberosTgtPassing, sChallengeResponseAuthentication,
1.138     djm       251:        sPasswordAuthentication, sKbdInteractiveAuthentication,
                    252:        sListenAddress, sAddressFamily,
1.72      stevesk   253:        sPrintMotd, sPrintLastLog, sIgnoreRhosts,
1.99      stevesk   254:        sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
1.129     markus    255:        sStrictModes, sEmptyPasswd, sTCPKeepAlive,
1.113     markus    256:        sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
1.53      markus    257:        sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
1.66      markus    258:        sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
1.133     dtucker   259:        sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
                    260:        sMaxStartups, sMaxAuthTries,
1.122     markus    261:        sBanner, sUseDNS, sHostbasedAuthentication,
1.95      deraadt   262:        sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
1.89      jakob     263:        sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
1.145     reyk      264:        sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
1.158     dtucker   265:        sMatch, sPermitOpen, sForceCommand,
1.105     stevesk   266:        sUsePrivilegeSeparation,
1.121     jakob     267:        sDeprecated, sUnsupported
1.1       deraadt   268: } ServerOpCodes;
                    269:
1.153     dtucker   270: #define SSHCFG_GLOBAL  0x01    /* allowed in main section of sshd_config */
                    271: #define SSHCFG_MATCH   0x02    /* allowed inside a Match section */
                    272: #define SSHCFG_ALL     (SSHCFG_GLOBAL|SSHCFG_MATCH)
                    273:
1.1       deraadt   274: /* Textual representation of the tokens. */
1.25      markus    275: static struct {
                    276:        const char *name;
                    277:        ServerOpCodes opcode;
1.153     dtucker   278:        u_int flags;
1.25      markus    279: } keywords[] = {
1.153     dtucker   280:        { "port", sPort, SSHCFG_GLOBAL },
                    281:        { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
                    282:        { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL },          /* alias */
                    283:        { "pidfile", sPidFile, SSHCFG_GLOBAL },
                    284:        { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
                    285:        { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
                    286:        { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
                    287:        { "permitrootlogin", sPermitRootLogin, SSHCFG_GLOBAL },
                    288:        { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
                    289:        { "loglevel", sLogLevel, SSHCFG_GLOBAL },
                    290:        { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
                    291:        { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_GLOBAL },
                    292:        { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_GLOBAL },
                    293:        { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
                    294:        { "rsaauthentication", sRSAAuthentication, SSHCFG_GLOBAL },
                    295:        { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL },
                    296:        { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
1.123     markus    297: #ifdef KRB5
1.153     dtucker   298:        { "kerberosauthentication", sKerberosAuthentication, SSHCFG_GLOBAL },
                    299:        { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
                    300:        { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
                    301:        { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
1.121     jakob     302: #else
1.153     dtucker   303:        { "kerberosauthentication", sUnsupported, SSHCFG_GLOBAL },
                    304:        { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
                    305:        { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
                    306:        { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
1.126     markus    307: #endif
1.153     dtucker   308:        { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
                    309:        { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
1.125     markus    310: #ifdef GSSAPI
1.153     dtucker   311:        { "gssapiauthentication", sGssAuthentication, SSHCFG_GLOBAL },
                    312:        { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
1.125     markus    313: #else
1.153     dtucker   314:        { "gssapiauthentication", sUnsupported, SSHCFG_GLOBAL },
                    315:        { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
1.125     markus    316: #endif
1.153     dtucker   317:        { "passwordauthentication", sPasswordAuthentication, SSHCFG_GLOBAL },
                    318:        { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_GLOBAL },
                    319:        { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
                    320:        { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
                    321:        { "checkmail", sDeprecated, SSHCFG_GLOBAL },
                    322:        { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
                    323:        { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
                    324:        { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
                    325:        { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
                    326:        { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
                    327:        { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
1.157     dtucker   328:        { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
                    329:        { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
                    330:        { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
1.153     dtucker   331:        { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
                    332:        { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
                    333:        { "permitemptypasswords", sEmptyPasswd, SSHCFG_GLOBAL },
                    334:        { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
                    335:        { "uselogin", sUseLogin, SSHCFG_GLOBAL },
                    336:        { "compression", sCompression, SSHCFG_GLOBAL },
                    337:        { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
                    338:        { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL },  /* obsolete alias */
                    339:        { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
                    340:        { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
                    341:        { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
                    342:        { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
                    343:        { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
                    344:        { "ciphers", sCiphers, SSHCFG_GLOBAL },
                    345:        { "macs", sMacs, SSHCFG_GLOBAL },
                    346:        { "protocol", sProtocol, SSHCFG_GLOBAL },
                    347:        { "gatewayports", sGatewayPorts, SSHCFG_ALL },
                    348:        { "subsystem", sSubsystem, SSHCFG_GLOBAL },
                    349:        { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
                    350:        { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
                    351:        { "banner", sBanner, SSHCFG_GLOBAL },
                    352:        { "usedns", sUseDNS, SSHCFG_GLOBAL },
                    353:        { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
                    354:        { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
                    355:        { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
                    356:        { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
                    357:        { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
                    358:        { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
                    359:        { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
                    360:        { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
                    361:        { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
                    362:        { "match", sMatch, SSHCFG_ALL },
1.156     dtucker   363:        { "permitopen", sPermitOpen, SSHCFG_ALL },
1.158     dtucker   364:        { "forcecommand", sForceCommand, SSHCFG_ALL },
1.153     dtucker   365:        { NULL, sBadOption, 0 }
1.1       deraadt   366: };
                    367:
1.27      markus    368: /*
1.73      stevesk   369:  * Returns the number of the token pointed to by cp or sBadOption.
1.27      markus    370:  */
1.1       deraadt   371:
1.34      markus    372: static ServerOpCodes
1.25      markus    373: parse_token(const char *cp, const char *filename,
1.153     dtucker   374:            int linenum, u_int *flags)
1.1       deraadt   375: {
1.55      markus    376:        u_int i;
1.1       deraadt   377:
1.25      markus    378:        for (i = 0; keywords[i].name; i++)
1.153     dtucker   379:                if (strcasecmp(cp, keywords[i].name) == 0) {
                    380:                        *flags = keywords[i].flags;
1.25      markus    381:                        return keywords[i].opcode;
1.153     dtucker   382:                }
1.25      markus    383:
1.78      stevesk   384:        error("%s: line %d: Bad configuration option: %s",
                    385:            filename, linenum, cp);
1.25      markus    386:        return sBadOption;
1.1       deraadt   387: }
                    388:
1.84      itojun    389: static void
1.76      stevesk   390: add_listen_addr(ServerOptions *options, char *addr, u_short port)
1.74      stevesk   391: {
1.142     djm       392:        u_int i;
1.74      stevesk   393:
                    394:        if (options->num_ports == 0)
                    395:                options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
1.138     djm       396:        if (options->address_family == -1)
                    397:                options->address_family = AF_UNSPEC;
1.76      stevesk   398:        if (port == 0)
1.74      stevesk   399:                for (i = 0; i < options->num_ports; i++)
                    400:                        add_one_listen_addr(options, addr, options->ports[i]);
                    401:        else
1.76      stevesk   402:                add_one_listen_addr(options, addr, port);
1.74      stevesk   403: }
                    404:
1.84      itojun    405: static void
1.74      stevesk   406: add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
1.29      markus    407: {
                    408:        struct addrinfo hints, *ai, *aitop;
                    409:        char strport[NI_MAXSERV];
                    410:        int gaierr;
                    411:
1.74      stevesk   412:        memset(&hints, 0, sizeof(hints));
1.138     djm       413:        hints.ai_family = options->address_family;
1.74      stevesk   414:        hints.ai_socktype = SOCK_STREAM;
                    415:        hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
1.112     deraadt   416:        snprintf(strport, sizeof strport, "%u", port);
1.74      stevesk   417:        if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
                    418:                fatal("bad addr or host: %s (%s)",
                    419:                    addr ? addr : "<NULL>",
                    420:                    gai_strerror(gaierr));
                    421:        for (ai = aitop; ai->ai_next; ai = ai->ai_next)
                    422:                ;
                    423:        ai->ai_next = options->listen_addrs;
                    424:        options->listen_addrs = aitop;
1.29      markus    425: }
                    426:
1.153     dtucker   427: /*
                    428:  * The strategy for the Match blocks is that the config file is parsed twice.
                    429:  *
                    430:  * The first time is at startup.  activep is initialized to 1 and the
                    431:  * directives in the global context are processed and acted on.  Hitting a
                    432:  * Match directive unsets activep and the directives inside the block are
                    433:  * checked for syntax only.
                    434:  *
                    435:  * The second time is after a connection has been established but before
                    436:  * authentication.  activep is initialized to 2 and global config directives
                    437:  * are ignored since they have already been processed.  If the criteria in a
                    438:  * Match block is met, activep is set and the subsequent directives
                    439:  * processed and actioned until EOF or another Match block unsets it.  Any
                    440:  * options set are copied into the main server config.
                    441:  *
                    442:  * Potential additions/improvements:
                    443:  *  - Add Match support for pre-kex directives, eg Protocol, Ciphers.
                    444:  *
                    445:  *  - Add a Tag directive (idea from David Leonard) ala pf, eg:
                    446:  *     Match Address 192.168.0.*
                    447:  *             Tag trusted
                    448:  *     Match Group wheel
                    449:  *             Tag trusted
                    450:  *     Match Tag trusted
                    451:  *             AllowTcpForwarding yes
                    452:  *             GatewayPorts clientspecified
                    453:  *             [...]
                    454:  *
                    455:  *  - Add a PermittedChannelRequests directive
                    456:  *     Match Group shell
                    457:  *             PermittedChannelRequests session,forwarded-tcpip
                    458:  */
                    459:
                    460: static int
                    461: match_cfg_line(char **condition, int line, const char *user, const char *host,
                    462:     const char *address)
                    463: {
                    464:        int result = 1;
                    465:        char *arg, *attrib, *cp = *condition;
                    466:        size_t len;
                    467:
                    468:        if (user == NULL)
                    469:                debug3("checking syntax for 'Match %s'", cp);
                    470:        else
                    471:                debug3("checking match for '%s' user %s host %s addr %s", cp,
                    472:                    user ? user : "(null)", host ? host : "(null)",
                    473:                    address ? address : "(null)");
                    474:
                    475:        while ((attrib = strdelim(&cp)) && *attrib != '\0') {
                    476:                if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
                    477:                        error("Missing Match criteria for %s", attrib);
                    478:                        return -1;
                    479:                }
                    480:                len = strlen(arg);
                    481:                if (strcasecmp(attrib, "user") == 0) {
                    482:                        if (!user) {
                    483:                                result = 0;
                    484:                                continue;
                    485:                        }
                    486:                        if (match_pattern_list(user, arg, len, 0) != 1)
                    487:                                result = 0;
                    488:                        else
                    489:                                debug("user %.100s matched 'User %.100s' at "
                    490:                                    "line %d", user, arg, line);
                    491:                } else if (strcasecmp(attrib, "host") == 0) {
                    492:                        if (!host) {
                    493:                                result = 0;
                    494:                                continue;
                    495:                        }
                    496:                        if (match_hostname(host, arg, len) != 1)
                    497:                                result = 0;
                    498:                        else
                    499:                                debug("connection from %.100s matched 'Host "
                    500:                                    "%.100s' at line %d", host, arg, line);
                    501:                } else if (strcasecmp(attrib, "address") == 0) {
                    502:                        debug("address '%s' arg '%s'", address, arg);
                    503:                        if (!address) {
                    504:                                result = 0;
                    505:                                continue;
                    506:                        }
                    507:                        if (match_hostname(address, arg, len) != 1)
                    508:                                result = 0;
                    509:                        else
                    510:                                debug("connection from %.100s matched 'Address "
                    511:                                    "%.100s' at line %d", address, arg, line);
                    512:                } else {
                    513:                        error("Unsupported Match attribute %s", attrib);
                    514:                        return -1;
                    515:                }
                    516:        }
                    517:        if (user != NULL)
                    518:                debug3("match %sfound", result ? "" : "not ");
                    519:        *condition = cp;
                    520:        return result;
                    521: }
                    522:
1.158     dtucker   523: #define WHITESPACE " \t\r\n"
                    524:
1.94      markus    525: int
                    526: process_server_config_line(ServerOptions *options, char *line,
1.153     dtucker   527:     const char *filename, int linenum, int *activep, const char *user,
                    528:     const char *host, const char *address)
1.1       deraadt   529: {
1.74      stevesk   530:        char *cp, **charptr, *arg, *p;
1.153     dtucker   531:        int cmdline = 0, *intptr, value, n;
1.25      markus    532:        ServerOpCodes opcode;
1.139     djm       533:        u_short port;
1.153     dtucker   534:        u_int i, flags = 0;
1.151     djm       535:        size_t len;
1.25      markus    536:
1.94      markus    537:        cp = line;
1.148     dtucker   538:        if ((arg = strdelim(&cp)) == NULL)
1.147     djm       539:                return 0;
1.94      markus    540:        /* Ignore leading whitespace */
                    541:        if (*arg == '\0')
                    542:                arg = strdelim(&cp);
                    543:        if (!arg || !*arg || *arg == '#')
                    544:                return 0;
                    545:        intptr = NULL;
                    546:        charptr = NULL;
1.153     dtucker   547:        opcode = parse_token(arg, filename, linenum, &flags);
                    548:
                    549:        if (activep == NULL) { /* We are processing a command line directive */
                    550:                cmdline = 1;
                    551:                activep = &cmdline;
                    552:        }
                    553:        if (*activep && opcode != sMatch)
                    554:                debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
                    555:        if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
                    556:                if (user == NULL) {
                    557:                        fatal("%s line %d: Directive '%s' is not allowed "
                    558:                            "within a Match block", filename, linenum, arg);
                    559:                } else { /* this is a directive we have already processed */
                    560:                        while (arg)
                    561:                                arg = strdelim(&cp);
                    562:                        return 0;
                    563:                }
                    564:        }
                    565:
1.94      markus    566:        switch (opcode) {
                    567:        case sBadOption:
                    568:                return -1;
                    569:        case sPort:
                    570:                /* ignore ports from configfile if cmdline specifies ports */
                    571:                if (options->ports_from_cmdline)
                    572:                        return 0;
                    573:                if (options->listen_addrs != NULL)
                    574:                        fatal("%s line %d: ports must be specified before "
1.98      stevesk   575:                            "ListenAddress.", filename, linenum);
1.94      markus    576:                if (options->num_ports >= MAX_PORTS)
                    577:                        fatal("%s line %d: too many ports.",
                    578:                            filename, linenum);
1.48      provos    579:                arg = strdelim(&cp);
1.94      markus    580:                if (!arg || *arg == '\0')
                    581:                        fatal("%s line %d: missing port number.",
                    582:                            filename, linenum);
                    583:                options->ports[options->num_ports++] = a2port(arg);
                    584:                if (options->ports[options->num_ports-1] == 0)
                    585:                        fatal("%s line %d: Badly formatted port number.",
                    586:                            filename, linenum);
                    587:                break;
1.29      markus    588:
1.94      markus    589:        case sServerKeyBits:
                    590:                intptr = &options->server_key_bits;
1.25      markus    591: parse_int:
1.94      markus    592:                arg = strdelim(&cp);
                    593:                if (!arg || *arg == '\0')
                    594:                        fatal("%s line %d: missing integer value.",
                    595:                            filename, linenum);
                    596:                value = atoi(arg);
1.153     dtucker   597:                if (*activep && *intptr == -1)
1.94      markus    598:                        *intptr = value;
                    599:                break;
1.25      markus    600:
1.94      markus    601:        case sLoginGraceTime:
                    602:                intptr = &options->login_grace_time;
1.81      stevesk   603: parse_time:
1.94      markus    604:                arg = strdelim(&cp);
                    605:                if (!arg || *arg == '\0')
                    606:                        fatal("%s line %d: missing time value.",
                    607:                            filename, linenum);
                    608:                if ((value = convtime(arg)) == -1)
                    609:                        fatal("%s line %d: invalid time value.",
                    610:                            filename, linenum);
                    611:                if (*intptr == -1)
                    612:                        *intptr = value;
                    613:                break;
                    614:
                    615:        case sKeyRegenerationTime:
                    616:                intptr = &options->key_regeneration_time;
                    617:                goto parse_time;
                    618:
                    619:        case sListenAddress:
                    620:                arg = strdelim(&cp);
1.139     djm       621:                if (arg == NULL || *arg == '\0')
                    622:                        fatal("%s line %d: missing address",
1.94      markus    623:                            filename, linenum);
1.144     dtucker   624:                /* check for bare IPv6 address: no "[]" and 2 or more ":" */
                    625:                if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
                    626:                    && strchr(p+1, ':') != NULL) {
                    627:                        add_listen_addr(options, arg, 0);
                    628:                        break;
                    629:                }
1.139     djm       630:                p = hpdelim(&arg);
                    631:                if (p == NULL)
                    632:                        fatal("%s line %d: bad address:port usage",
                    633:                            filename, linenum);
                    634:                p = cleanhostname(p);
                    635:                if (arg == NULL)
                    636:                        port = 0;
                    637:                else if ((port = a2port(arg)) == 0)
                    638:                        fatal("%s line %d: bad port number", filename, linenum);
                    639:
                    640:                add_listen_addr(options, p, port);
1.25      markus    641:
1.138     djm       642:                break;
                    643:
                    644:        case sAddressFamily:
                    645:                arg = strdelim(&cp);
1.141     markus    646:                if (!arg || *arg == '\0')
                    647:                        fatal("%s line %d: missing address family.",
                    648:                            filename, linenum);
1.138     djm       649:                intptr = &options->address_family;
                    650:                if (options->listen_addrs != NULL)
                    651:                        fatal("%s line %d: address family must be specified before "
                    652:                            "ListenAddress.", filename, linenum);
                    653:                if (strcasecmp(arg, "inet") == 0)
                    654:                        value = AF_INET;
                    655:                else if (strcasecmp(arg, "inet6") == 0)
                    656:                        value = AF_INET6;
                    657:                else if (strcasecmp(arg, "any") == 0)
                    658:                        value = AF_UNSPEC;
                    659:                else
                    660:                        fatal("%s line %d: unsupported address family \"%s\".",
                    661:                            filename, linenum, arg);
                    662:                if (*intptr == -1)
                    663:                        *intptr = value;
1.94      markus    664:                break;
                    665:
                    666:        case sHostKeyFile:
                    667:                intptr = &options->num_host_key_files;
                    668:                if (*intptr >= MAX_HOSTKEYS)
                    669:                        fatal("%s line %d: too many host keys specified (max %d).",
                    670:                            filename, linenum, MAX_HOSTKEYS);
                    671:                charptr = &options->host_key_files[*intptr];
                    672: parse_filename:
                    673:                arg = strdelim(&cp);
                    674:                if (!arg || *arg == '\0')
                    675:                        fatal("%s line %d: missing file name.",
                    676:                            filename, linenum);
1.153     dtucker   677:                if (*activep && *charptr == NULL) {
1.94      markus    678:                        *charptr = tilde_expand_filename(arg, getuid());
                    679:                        /* increase optional counter */
                    680:                        if (intptr != NULL)
                    681:                                *intptr = *intptr + 1;
                    682:                }
                    683:                break;
1.76      stevesk   684:
1.94      markus    685:        case sPidFile:
                    686:                charptr = &options->pid_file;
                    687:                goto parse_filename;
1.25      markus    688:
1.94      markus    689:        case sPermitRootLogin:
                    690:                intptr = &options->permit_root_login;
                    691:                arg = strdelim(&cp);
                    692:                if (!arg || *arg == '\0')
                    693:                        fatal("%s line %d: missing yes/"
                    694:                            "without-password/forced-commands-only/no "
                    695:                            "argument.", filename, linenum);
                    696:                value = 0;      /* silence compiler */
                    697:                if (strcmp(arg, "without-password") == 0)
                    698:                        value = PERMIT_NO_PASSWD;
                    699:                else if (strcmp(arg, "forced-commands-only") == 0)
                    700:                        value = PERMIT_FORCED_ONLY;
                    701:                else if (strcmp(arg, "yes") == 0)
                    702:                        value = PERMIT_YES;
                    703:                else if (strcmp(arg, "no") == 0)
                    704:                        value = PERMIT_NO;
                    705:                else
                    706:                        fatal("%s line %d: Bad yes/"
                    707:                            "without-password/forced-commands-only/no "
                    708:                            "argument: %s", filename, linenum, arg);
                    709:                if (*intptr == -1)
                    710:                        *intptr = value;
                    711:                break;
1.36      markus    712:
1.94      markus    713:        case sIgnoreRhosts:
                    714:                intptr = &options->ignore_rhosts;
1.25      markus    715: parse_flag:
1.94      markus    716:                arg = strdelim(&cp);
                    717:                if (!arg || *arg == '\0')
                    718:                        fatal("%s line %d: missing yes/no argument.",
                    719:                            filename, linenum);
                    720:                value = 0;      /* silence compiler */
                    721:                if (strcmp(arg, "yes") == 0)
                    722:                        value = 1;
                    723:                else if (strcmp(arg, "no") == 0)
                    724:                        value = 0;
                    725:                else
                    726:                        fatal("%s line %d: Bad yes/no argument: %s",
                    727:                                filename, linenum, arg);
1.153     dtucker   728:                if (*activep && *intptr == -1)
1.94      markus    729:                        *intptr = value;
                    730:                break;
                    731:
                    732:        case sIgnoreUserKnownHosts:
                    733:                intptr = &options->ignore_user_known_hosts;
                    734:                goto parse_flag;
                    735:
                    736:        case sRhostsRSAAuthentication:
                    737:                intptr = &options->rhosts_rsa_authentication;
                    738:                goto parse_flag;
                    739:
                    740:        case sHostbasedAuthentication:
                    741:                intptr = &options->hostbased_authentication;
                    742:                goto parse_flag;
                    743:
                    744:        case sHostbasedUsesNameFromPacketOnly:
                    745:                intptr = &options->hostbased_uses_name_from_packet_only;
                    746:                goto parse_flag;
                    747:
                    748:        case sRSAAuthentication:
                    749:                intptr = &options->rsa_authentication;
                    750:                goto parse_flag;
                    751:
                    752:        case sPubkeyAuthentication:
                    753:                intptr = &options->pubkey_authentication;
                    754:                goto parse_flag;
1.119     jakob     755:
1.94      markus    756:        case sKerberosAuthentication:
                    757:                intptr = &options->kerberos_authentication;
                    758:                goto parse_flag;
                    759:
                    760:        case sKerberosOrLocalPasswd:
                    761:                intptr = &options->kerberos_or_local_passwd;
                    762:                goto parse_flag;
                    763:
                    764:        case sKerberosTicketCleanup:
                    765:                intptr = &options->kerberos_ticket_cleanup;
1.130     jakob     766:                goto parse_flag;
                    767:
                    768:        case sKerberosGetAFSToken:
                    769:                intptr = &options->kerberos_get_afs_token;
1.125     markus    770:                goto parse_flag;
                    771:
                    772:        case sGssAuthentication:
                    773:                intptr = &options->gss_authentication;
                    774:                goto parse_flag;
                    775:
                    776:        case sGssCleanupCreds:
                    777:                intptr = &options->gss_cleanup_creds;
1.94      markus    778:                goto parse_flag;
                    779:
                    780:        case sPasswordAuthentication:
                    781:                intptr = &options->password_authentication;
                    782:                goto parse_flag;
                    783:
                    784:        case sKbdInteractiveAuthentication:
                    785:                intptr = &options->kbd_interactive_authentication;
                    786:                goto parse_flag;
                    787:
                    788:        case sChallengeResponseAuthentication:
                    789:                intptr = &options->challenge_response_authentication;
                    790:                goto parse_flag;
                    791:
                    792:        case sPrintMotd:
                    793:                intptr = &options->print_motd;
                    794:                goto parse_flag;
                    795:
                    796:        case sPrintLastLog:
                    797:                intptr = &options->print_lastlog;
                    798:                goto parse_flag;
                    799:
                    800:        case sX11Forwarding:
                    801:                intptr = &options->x11_forwarding;
                    802:                goto parse_flag;
                    803:
                    804:        case sX11DisplayOffset:
                    805:                intptr = &options->x11_display_offset;
                    806:                goto parse_int;
1.99      stevesk   807:
                    808:        case sX11UseLocalhost:
                    809:                intptr = &options->x11_use_localhost;
                    810:                goto parse_flag;
1.94      markus    811:
                    812:        case sXAuthLocation:
                    813:                charptr = &options->xauth_location;
                    814:                goto parse_filename;
                    815:
                    816:        case sStrictModes:
                    817:                intptr = &options->strict_modes;
                    818:                goto parse_flag;
                    819:
1.129     markus    820:        case sTCPKeepAlive:
                    821:                intptr = &options->tcp_keep_alive;
1.94      markus    822:                goto parse_flag;
                    823:
                    824:        case sEmptyPasswd:
                    825:                intptr = &options->permit_empty_passwd;
1.113     markus    826:                goto parse_flag;
                    827:
                    828:        case sPermitUserEnvironment:
                    829:                intptr = &options->permit_user_env;
1.94      markus    830:                goto parse_flag;
                    831:
                    832:        case sUseLogin:
                    833:                intptr = &options->use_login;
1.111     markus    834:                goto parse_flag;
                    835:
                    836:        case sCompression:
                    837:                intptr = &options->compression;
1.143     markus    838:                arg = strdelim(&cp);
                    839:                if (!arg || *arg == '\0')
                    840:                        fatal("%s line %d: missing yes/no/delayed "
                    841:                            "argument.", filename, linenum);
                    842:                value = 0;      /* silence compiler */
                    843:                if (strcmp(arg, "delayed") == 0)
                    844:                        value = COMP_DELAYED;
                    845:                else if (strcmp(arg, "yes") == 0)
                    846:                        value = COMP_ZLIB;
                    847:                else if (strcmp(arg, "no") == 0)
                    848:                        value = COMP_NONE;
                    849:                else
                    850:                        fatal("%s line %d: Bad yes/no/delayed "
                    851:                            "argument: %s", filename, linenum, arg);
                    852:                if (*intptr == -1)
                    853:                        *intptr = value;
                    854:                break;
1.94      markus    855:
                    856:        case sGatewayPorts:
                    857:                intptr = &options->gateway_ports;
1.139     djm       858:                arg = strdelim(&cp);
                    859:                if (!arg || *arg == '\0')
                    860:                        fatal("%s line %d: missing yes/no/clientspecified "
                    861:                            "argument.", filename, linenum);
                    862:                value = 0;      /* silence compiler */
                    863:                if (strcmp(arg, "clientspecified") == 0)
                    864:                        value = 2;
                    865:                else if (strcmp(arg, "yes") == 0)
                    866:                        value = 1;
                    867:                else if (strcmp(arg, "no") == 0)
                    868:                        value = 0;
                    869:                else
                    870:                        fatal("%s line %d: Bad yes/no/clientspecified "
                    871:                            "argument: %s", filename, linenum, arg);
                    872:                if (*intptr == -1)
                    873:                        *intptr = value;
                    874:                break;
1.25      markus    875:
1.122     markus    876:        case sUseDNS:
                    877:                intptr = &options->use_dns;
1.94      markus    878:                goto parse_flag;
1.53      markus    879:
1.94      markus    880:        case sLogFacility:
                    881:                intptr = (int *) &options->log_facility;
                    882:                arg = strdelim(&cp);
                    883:                value = log_facility_number(arg);
1.101     markus    884:                if (value == SYSLOG_FACILITY_NOT_SET)
1.94      markus    885:                        fatal("%.200s line %d: unsupported log facility '%s'",
                    886:                            filename, linenum, arg ? arg : "<NONE>");
                    887:                if (*intptr == -1)
                    888:                        *intptr = (SyslogFacility) value;
                    889:                break;
1.25      markus    890:
1.94      markus    891:        case sLogLevel:
                    892:                intptr = (int *) &options->log_level;
                    893:                arg = strdelim(&cp);
                    894:                value = log_level_number(arg);
1.101     markus    895:                if (value == SYSLOG_LEVEL_NOT_SET)
1.94      markus    896:                        fatal("%.200s line %d: unsupported log level '%s'",
                    897:                            filename, linenum, arg ? arg : "<NONE>");
                    898:                if (*intptr == -1)
                    899:                        *intptr = (LogLevel) value;
                    900:                break;
                    901:
                    902:        case sAllowTcpForwarding:
                    903:                intptr = &options->allow_tcp_forwarding;
                    904:                goto parse_flag;
1.102     provos    905:
                    906:        case sUsePrivilegeSeparation:
                    907:                intptr = &use_privsep;
                    908:                goto parse_flag;
1.94      markus    909:
                    910:        case sAllowUsers:
                    911:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    912:                        if (options->num_allow_users >= MAX_ALLOW_USERS)
                    913:                                fatal("%s line %d: too many allow users.",
                    914:                                    filename, linenum);
1.112     deraadt   915:                        options->allow_users[options->num_allow_users++] =
                    916:                            xstrdup(arg);
1.94      markus    917:                }
                    918:                break;
1.25      markus    919:
1.94      markus    920:        case sDenyUsers:
                    921:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    922:                        if (options->num_deny_users >= MAX_DENY_USERS)
1.163   ! stevesk   923:                                fatal("%s line %d: too many deny users.",
1.94      markus    924:                                    filename, linenum);
1.112     deraadt   925:                        options->deny_users[options->num_deny_users++] =
                    926:                            xstrdup(arg);
1.94      markus    927:                }
                    928:                break;
1.25      markus    929:
1.94      markus    930:        case sAllowGroups:
                    931:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    932:                        if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
                    933:                                fatal("%s line %d: too many allow groups.",
                    934:                                    filename, linenum);
1.112     deraadt   935:                        options->allow_groups[options->num_allow_groups++] =
                    936:                            xstrdup(arg);
1.94      markus    937:                }
                    938:                break;
1.33      markus    939:
1.94      markus    940:        case sDenyGroups:
                    941:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    942:                        if (options->num_deny_groups >= MAX_DENY_GROUPS)
                    943:                                fatal("%s line %d: too many deny groups.",
                    944:                                    filename, linenum);
                    945:                        options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
                    946:                }
                    947:                break;
1.66      markus    948:
1.94      markus    949:        case sCiphers:
                    950:                arg = strdelim(&cp);
                    951:                if (!arg || *arg == '\0')
                    952:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    953:                if (!ciphers_valid(arg))
                    954:                        fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
                    955:                            filename, linenum, arg ? arg : "<NONE>");
                    956:                if (options->ciphers == NULL)
                    957:                        options->ciphers = xstrdup(arg);
                    958:                break;
1.33      markus    959:
1.94      markus    960:        case sMacs:
                    961:                arg = strdelim(&cp);
                    962:                if (!arg || *arg == '\0')
                    963:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    964:                if (!mac_valid(arg))
                    965:                        fatal("%s line %d: Bad SSH2 mac spec '%s'.",
                    966:                            filename, linenum, arg ? arg : "<NONE>");
                    967:                if (options->macs == NULL)
                    968:                        options->macs = xstrdup(arg);
                    969:                break;
1.43      jakob     970:
1.94      markus    971:        case sProtocol:
                    972:                intptr = &options->protocol;
                    973:                arg = strdelim(&cp);
                    974:                if (!arg || *arg == '\0')
                    975:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    976:                value = proto_spec(arg);
                    977:                if (value == SSH_PROTO_UNKNOWN)
                    978:                        fatal("%s line %d: Bad protocol spec '%s'.",
1.95      deraadt   979:                            filename, linenum, arg ? arg : "<NONE>");
1.94      markus    980:                if (*intptr == SSH_PROTO_UNKNOWN)
                    981:                        *intptr = value;
                    982:                break;
                    983:
                    984:        case sSubsystem:
                    985:                if (options->num_subsystems >= MAX_SUBSYSTEMS) {
                    986:                        fatal("%s line %d: too many subsystems defined.",
1.95      deraadt   987:                            filename, linenum);
1.94      markus    988:                }
                    989:                arg = strdelim(&cp);
                    990:                if (!arg || *arg == '\0')
                    991:                        fatal("%s line %d: Missing subsystem name.",
1.95      deraadt   992:                            filename, linenum);
1.153     dtucker   993:                if (!*activep) {
                    994:                        arg = strdelim(&cp);
                    995:                        break;
                    996:                }
1.94      markus    997:                for (i = 0; i < options->num_subsystems; i++)
                    998:                        if (strcmp(arg, options->subsystem_name[i]) == 0)
                    999:                                fatal("%s line %d: Subsystem '%s' already defined.",
1.95      deraadt  1000:                                    filename, linenum, arg);
1.94      markus   1001:                options->subsystem_name[options->num_subsystems] = xstrdup(arg);
                   1002:                arg = strdelim(&cp);
                   1003:                if (!arg || *arg == '\0')
                   1004:                        fatal("%s line %d: Missing subsystem command.",
1.95      deraadt  1005:                            filename, linenum);
1.94      markus   1006:                options->subsystem_command[options->num_subsystems] = xstrdup(arg);
1.151     djm      1007:
                   1008:                /* Collect arguments (separate to executable) */
                   1009:                p = xstrdup(arg);
                   1010:                len = strlen(p) + 1;
                   1011:                while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
                   1012:                        len += 1 + strlen(arg);
                   1013:                        p = xrealloc(p, 1, len);
                   1014:                        strlcat(p, " ", len);
                   1015:                        strlcat(p, arg, len);
                   1016:                }
                   1017:                options->subsystem_args[options->num_subsystems] = p;
1.94      markus   1018:                options->num_subsystems++;
                   1019:                break;
1.46      markus   1020:
1.94      markus   1021:        case sMaxStartups:
                   1022:                arg = strdelim(&cp);
                   1023:                if (!arg || *arg == '\0')
                   1024:                        fatal("%s line %d: Missing MaxStartups spec.",
1.95      deraadt  1025:                            filename, linenum);
1.94      markus   1026:                if ((n = sscanf(arg, "%d:%d:%d",
                   1027:                    &options->max_startups_begin,
                   1028:                    &options->max_startups_rate,
                   1029:                    &options->max_startups)) == 3) {
                   1030:                        if (options->max_startups_begin >
                   1031:                            options->max_startups ||
                   1032:                            options->max_startups_rate > 100 ||
                   1033:                            options->max_startups_rate < 1)
1.50      markus   1034:                                fatal("%s line %d: Illegal MaxStartups spec.",
1.87      stevesk  1035:                                    filename, linenum);
1.94      markus   1036:                } else if (n != 1)
                   1037:                        fatal("%s line %d: Illegal MaxStartups spec.",
                   1038:                            filename, linenum);
                   1039:                else
                   1040:                        options->max_startups = options->max_startups_begin;
                   1041:                break;
1.133     dtucker  1042:
                   1043:        case sMaxAuthTries:
                   1044:                intptr = &options->max_authtries;
                   1045:                goto parse_int;
1.94      markus   1046:
                   1047:        case sBanner:
                   1048:                charptr = &options->banner;
                   1049:                goto parse_filename;
                   1050:        /*
                   1051:         * These options can contain %X options expanded at
                   1052:         * connect time, so that you can specify paths like:
                   1053:         *
                   1054:         * AuthorizedKeysFile   /etc/ssh_keys/%u
                   1055:         */
                   1056:        case sAuthorizedKeysFile:
                   1057:        case sAuthorizedKeysFile2:
1.163   ! stevesk  1058:                charptr = (opcode == sAuthorizedKeysFile) ?
1.94      markus   1059:                    &options->authorized_keys_file :
                   1060:                    &options->authorized_keys_file2;
                   1061:                goto parse_filename;
                   1062:
                   1063:        case sClientAliveInterval:
                   1064:                intptr = &options->client_alive_interval;
                   1065:                goto parse_time;
                   1066:
                   1067:        case sClientAliveCountMax:
                   1068:                intptr = &options->client_alive_count_max;
                   1069:                goto parse_int;
1.131     djm      1070:
                   1071:        case sAcceptEnv:
                   1072:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                   1073:                        if (strchr(arg, '=') != NULL)
                   1074:                                fatal("%s line %d: Invalid environment name.",
                   1075:                                    filename, linenum);
                   1076:                        if (options->num_accept_env >= MAX_ACCEPT_ENV)
                   1077:                                fatal("%s line %d: too many allow env.",
                   1078:                                    filename, linenum);
1.153     dtucker  1079:                        if (!*activep)
                   1080:                                break;
1.131     djm      1081:                        options->accept_env[options->num_accept_env++] =
                   1082:                            xstrdup(arg);
                   1083:                }
                   1084:                break;
1.145     reyk     1085:
                   1086:        case sPermitTunnel:
                   1087:                intptr = &options->permit_tun;
1.146     reyk     1088:                arg = strdelim(&cp);
                   1089:                if (!arg || *arg == '\0')
                   1090:                        fatal("%s line %d: Missing yes/point-to-point/"
                   1091:                            "ethernet/no argument.", filename, linenum);
                   1092:                value = 0;      /* silence compiler */
                   1093:                if (strcasecmp(arg, "ethernet") == 0)
                   1094:                        value = SSH_TUNMODE_ETHERNET;
                   1095:                else if (strcasecmp(arg, "point-to-point") == 0)
                   1096:                        value = SSH_TUNMODE_POINTOPOINT;
                   1097:                else if (strcasecmp(arg, "yes") == 0)
                   1098:                        value = SSH_TUNMODE_YES;
                   1099:                else if (strcasecmp(arg, "no") == 0)
                   1100:                        value = SSH_TUNMODE_NO;
                   1101:                else
                   1102:                        fatal("%s line %d: Bad yes/point-to-point/ethernet/"
                   1103:                            "no argument: %s", filename, linenum, arg);
                   1104:                if (*intptr == -1)
                   1105:                        *intptr = value;
                   1106:                break;
1.94      markus   1107:
1.153     dtucker  1108:        case sMatch:
                   1109:                if (cmdline)
                   1110:                        fatal("Match directive not supported as a command-line "
                   1111:                           "option");
                   1112:                value = match_cfg_line(&cp, linenum, user, host, address);
                   1113:                if (value < 0)
                   1114:                        fatal("%s line %d: Bad Match condition", filename,
                   1115:                            linenum);
                   1116:                *activep = value;
1.156     dtucker  1117:                break;
                   1118:
                   1119:        case sPermitOpen:
                   1120:                arg = strdelim(&cp);
                   1121:                if (!arg || *arg == '\0')
                   1122:                        fatal("%s line %d: missing PermitOpen specification",
                   1123:                            filename, linenum);
                   1124:                if (strcmp(arg, "any") == 0) {
1.159     dtucker  1125:                        if (*activep) {
1.156     dtucker  1126:                                channel_clear_adm_permitted_opens();
1.159     dtucker  1127:                                options->num_permitted_opens = 0;
                   1128:                        }
1.156     dtucker  1129:                        break;
                   1130:                }
1.159     dtucker  1131:                for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
                   1132:                        p = hpdelim(&arg);
                   1133:                        if (p == NULL)
                   1134:                                fatal("%s line %d: missing host in PermitOpen",
                   1135:                                    filename, linenum);
                   1136:                        p = cleanhostname(p);
                   1137:                        if (arg == NULL || (port = a2port(arg)) == 0)
                   1138:                                fatal("%s line %d: bad port number in "
                   1139:                                    "PermitOpen", filename, linenum);
                   1140:                        if (*activep && options->num_permitted_opens == -1) {
                   1141:                                channel_clear_adm_permitted_opens();
                   1142:                                options->num_permitted_opens =
                   1143:                                    channel_add_adm_permitted_opens(p, port);
                   1144:                        }
                   1145:                }
1.153     dtucker  1146:                break;
                   1147:
1.158     dtucker  1148:        case sForceCommand:
                   1149:                if (cp == NULL)
                   1150:                        fatal("%.200s line %d: Missing argument.", filename,
                   1151:                            linenum);
                   1152:                len = strspn(cp, WHITESPACE);
                   1153:                if (*activep && options->adm_forced_command == NULL)
                   1154:                        options->adm_forced_command = xstrdup(cp + len);
                   1155:                return 0;
                   1156:
1.94      markus   1157:        case sDeprecated:
1.117     itojun   1158:                logit("%s line %d: Deprecated option %s",
1.121     jakob    1159:                    filename, linenum, arg);
                   1160:                while (arg)
                   1161:                    arg = strdelim(&cp);
                   1162:                break;
                   1163:
                   1164:        case sUnsupported:
                   1165:                logit("%s line %d: Unsupported option %s",
1.94      markus   1166:                    filename, linenum, arg);
                   1167:                while (arg)
                   1168:                    arg = strdelim(&cp);
                   1169:                break;
                   1170:
                   1171:        default:
                   1172:                fatal("%s line %d: Missing handler for opcode %s (%d)",
                   1173:                    filename, linenum, arg, opcode);
                   1174:        }
                   1175:        if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
                   1176:                fatal("%s line %d: garbage at end of line; \"%.200s\".",
                   1177:                    filename, linenum, arg);
                   1178:        return 0;
                   1179: }
                   1180:
                   1181: /* Reads the server configuration file. */
1.25      markus   1182:
1.94      markus   1183: void
1.134     djm      1184: load_server_config(const char *filename, Buffer *conf)
1.94      markus   1185: {
1.134     djm      1186:        char line[1024], *cp;
1.94      markus   1187:        FILE *f;
1.81      stevesk  1188:
1.134     djm      1189:        debug2("%s: filename %s", __func__, filename);
                   1190:        if ((f = fopen(filename, "r")) == NULL) {
1.94      markus   1191:                perror(filename);
                   1192:                exit(1);
                   1193:        }
1.134     djm      1194:        buffer_clear(conf);
                   1195:        while (fgets(line, sizeof(line), f)) {
                   1196:                /*
                   1197:                 * Trim out comments and strip whitespace
1.135     deraadt  1198:                 * NB - preserve newlines, they are needed to reproduce
1.134     djm      1199:                 * line numbers later for error messages
                   1200:                 */
                   1201:                if ((cp = strchr(line, '#')) != NULL)
                   1202:                        memcpy(cp, "\n", 2);
                   1203:                cp = line + strspn(line, " \t\r");
                   1204:
                   1205:                buffer_append(conf, cp, strlen(cp));
                   1206:        }
                   1207:        buffer_append(conf, "\0", 1);
                   1208:        fclose(f);
                   1209:        debug2("%s: done config len = %d", __func__, buffer_len(conf));
                   1210: }
                   1211:
                   1212: void
1.153     dtucker  1213: parse_server_match_config(ServerOptions *options, const char *user,
                   1214:     const char *host, const char *address)
                   1215: {
                   1216:        ServerOptions mo;
                   1217:
                   1218:        initialize_server_options(&mo);
                   1219:        parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
                   1220:        copy_set_server_options(options, &mo);
                   1221: }
                   1222:
                   1223: /* Copy any (supported) values that are set */
                   1224: void
                   1225: copy_set_server_options(ServerOptions *dst, ServerOptions *src)
                   1226: {
                   1227:        if (src->allow_tcp_forwarding != -1)
                   1228:                dst->allow_tcp_forwarding = src->allow_tcp_forwarding;
                   1229:        if (src->gateway_ports != -1)
                   1230:                dst->gateway_ports = src->gateway_ports;
1.158     dtucker  1231:        if (src->adm_forced_command != NULL) {
                   1232:                if (dst->adm_forced_command != NULL)
                   1233:                        xfree(dst->adm_forced_command);
                   1234:                dst->adm_forced_command = src->adm_forced_command;
                   1235:        }
1.157     dtucker  1236:        if (src->x11_display_offset != -1)
                   1237:                dst->x11_display_offset = src->x11_display_offset;
                   1238:        if (src->x11_forwarding != -1)
                   1239:                dst->x11_forwarding = src->x11_forwarding;
                   1240:        if (src->x11_use_localhost != -1)
                   1241:                dst->x11_use_localhost = src->x11_use_localhost;
1.153     dtucker  1242: }
                   1243:
                   1244: void
                   1245: parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
                   1246:     const char *user, const char *host, const char *address)
1.134     djm      1247: {
1.153     dtucker  1248:        int active, linenum, bad_options = 0;
1.136     dtucker  1249:        char *cp, *obuf, *cbuf;
1.134     djm      1250:
                   1251:        debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
                   1252:
1.136     dtucker  1253:        obuf = cbuf = xstrdup(buffer_ptr(conf));
1.153     dtucker  1254:        active = user ? 0 : 1;
1.137     dtucker  1255:        linenum = 1;
1.140     deraadt  1256:        while ((cp = strsep(&cbuf, "\n")) != NULL) {
1.134     djm      1257:                if (process_server_config_line(options, cp, filename,
1.153     dtucker  1258:                    linenum++, &active, user, host, address) != 0)
1.94      markus   1259:                        bad_options++;
1.1       deraadt  1260:        }
1.136     dtucker  1261:        xfree(obuf);
1.78      stevesk  1262:        if (bad_options > 0)
                   1263:                fatal("%s: terminating, %d bad configuration options",
                   1264:                    filename, bad_options);
1.1       deraadt  1265: }