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

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