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

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