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

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