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

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