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

1.1       deraadt     1: /*
1.26      deraadt     2:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      3:  *                    All rights reserved
1.34      markus      4:  *
1.51      deraadt     5:  * As far as I am concerned, the code I have written for this software
                      6:  * can be used freely for any purpose.  Any derived versions of this
                      7:  * software must be clearly marked as such, and if the derived work is
                      8:  * incompatible with the protocol description in the RFC file, it must be
                      9:  * called by a name other than "ssh" or "Secure Shell".
1.26      deraadt    10:  */
1.1       deraadt    11:
                     12: #include "includes.h"
1.130.2.2! brad       13: RCSID("$OpenBSD: servconf.c,v 1.139 2005/03/01 10:09:52 djm Exp $");
1.1       deraadt    14:
                     15: #include "ssh.h"
1.62      markus     16: #include "log.h"
1.1       deraadt    17: #include "servconf.h"
                     18: #include "xmalloc.h"
1.33      markus     19: #include "compat.h"
1.60      markus     20: #include "pathnames.h"
1.62      markus     21: #include "misc.h"
                     22: #include "cipher.h"
1.66      markus     23: #include "kex.h"
                     24: #include "mac.h"
1.1       deraadt    25:
1.84      itojun     26: static void add_listen_addr(ServerOptions *, char *, u_short);
                     27: static void add_one_listen_addr(ServerOptions *, char *, u_short);
1.29      markus     28:
1.102     provos     29: /* Use of privilege separation or not */
                     30: extern int use_privsep;
1.62      markus     31:
1.1       deraadt    32: /* Initializes the server options to their default values. */
                     33:
1.34      markus     34: void
1.25      markus     35: initialize_server_options(ServerOptions *options)
1.1       deraadt    36: {
1.25      markus     37:        memset(options, 0, sizeof(*options));
1.29      markus     38:        options->num_ports = 0;
                     39:        options->ports_from_cmdline = 0;
                     40:        options->listen_addrs = NULL;
1.130.2.2! brad       41:        options->address_family = -1;
1.54      markus     42:        options->num_host_key_files = 0;
1.36      markus     43:        options->pid_file = NULL;
1.25      markus     44:        options->server_key_bits = -1;
                     45:        options->login_grace_time = -1;
                     46:        options->key_regeneration_time = -1;
1.67      markus     47:        options->permit_root_login = PERMIT_NOT_SET;
1.25      markus     48:        options->ignore_rhosts = -1;
                     49:        options->ignore_user_known_hosts = -1;
                     50:        options->print_motd = -1;
1.72      stevesk    51:        options->print_lastlog = -1;
1.25      markus     52:        options->x11_forwarding = -1;
                     53:        options->x11_display_offset = -1;
1.99      stevesk    54:        options->x11_use_localhost = -1;
1.42      markus     55:        options->xauth_location = NULL;
1.25      markus     56:        options->strict_modes = -1;
1.129     markus     57:        options->tcp_keep_alive = -1;
1.101     markus     58:        options->log_facility = SYSLOG_FACILITY_NOT_SET;
                     59:        options->log_level = SYSLOG_LEVEL_NOT_SET;
1.25      markus     60:        options->rhosts_rsa_authentication = -1;
1.75      markus     61:        options->hostbased_authentication = -1;
                     62:        options->hostbased_uses_name_from_packet_only = -1;
1.25      markus     63:        options->rsa_authentication = -1;
1.54      markus     64:        options->pubkey_authentication = -1;
1.25      markus     65:        options->kerberos_authentication = -1;
                     66:        options->kerberos_or_local_passwd = -1;
                     67:        options->kerberos_ticket_cleanup = -1;
1.130     jakob      68:        options->kerberos_get_afs_token = -1;
1.125     markus     69:        options->gss_authentication=-1;
                     70:        options->gss_cleanup_creds = -1;
1.25      markus     71:        options->password_authentication = -1;
1.52      markus     72:        options->kbd_interactive_authentication = -1;
1.80      markus     73:        options->challenge_response_authentication = -1;
1.25      markus     74:        options->permit_empty_passwd = -1;
1.113     markus     75:        options->permit_user_env = -1;
1.25      markus     76:        options->use_login = -1;
1.111     markus     77:        options->compression = -1;
1.53      markus     78:        options->allow_tcp_forwarding = -1;
1.25      markus     79:        options->num_allow_users = 0;
                     80:        options->num_deny_users = 0;
                     81:        options->num_allow_groups = 0;
                     82:        options->num_deny_groups = 0;
1.33      markus     83:        options->ciphers = NULL;
1.66      markus     84:        options->macs = NULL;
1.33      markus     85:        options->protocol = SSH_PROTO_UNKNOWN;
1.38      markus     86:        options->gateway_ports = -1;
1.43      jakob      87:        options->num_subsystems = 0;
1.50      markus     88:        options->max_startups_begin = -1;
                     89:        options->max_startups_rate = -1;
1.46      markus     90:        options->max_startups = -1;
1.130.2.1  brad       91:        options->max_authtries = -1;
1.57      markus     92:        options->banner = NULL;
1.122     markus     93:        options->use_dns = -1;
1.77      beck       94:        options->client_alive_interval = -1;
                     95:        options->client_alive_count_max = -1;
1.82      markus     96:        options->authorized_keys_file = NULL;
                     97:        options->authorized_keys_file2 = NULL;
1.130.2.1  brad       98:        options->num_accept_env = 0;
1.102     provos     99:
                    100:        /* Needs to be accessable in many places */
                    101:        use_privsep = -1;
1.1       deraadt   102: }
                    103:
1.34      markus    104: void
1.25      markus    105: fill_default_server_options(ServerOptions *options)
1.1       deraadt   106: {
1.54      markus    107:        if (options->protocol == SSH_PROTO_UNKNOWN)
                    108:                options->protocol = SSH_PROTO_1|SSH_PROTO_2;
                    109:        if (options->num_host_key_files == 0) {
                    110:                /* fill default hostkeys for protocols */
                    111:                if (options->protocol & SSH_PROTO_1)
1.97      stevesk   112:                        options->host_key_files[options->num_host_key_files++] =
                    113:                            _PATH_HOST_KEY_FILE;
                    114:                if (options->protocol & SSH_PROTO_2) {
                    115:                        options->host_key_files[options->num_host_key_files++] =
                    116:                            _PATH_HOST_RSA_KEY_FILE;
                    117:                        options->host_key_files[options->num_host_key_files++] =
                    118:                            _PATH_HOST_DSA_KEY_FILE;
                    119:                }
1.54      markus    120:        }
1.29      markus    121:        if (options->num_ports == 0)
                    122:                options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
                    123:        if (options->listen_addrs == NULL)
1.76      stevesk   124:                add_listen_addr(options, NULL, 0);
1.36      markus    125:        if (options->pid_file == NULL)
1.60      markus    126:                options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
1.25      markus    127:        if (options->server_key_bits == -1)
                    128:                options->server_key_bits = 768;
                    129:        if (options->login_grace_time == -1)
1.115     stevesk   130:                options->login_grace_time = 120;
1.25      markus    131:        if (options->key_regeneration_time == -1)
                    132:                options->key_regeneration_time = 3600;
1.67      markus    133:        if (options->permit_root_login == PERMIT_NOT_SET)
                    134:                options->permit_root_login = PERMIT_YES;
1.25      markus    135:        if (options->ignore_rhosts == -1)
1.30      markus    136:                options->ignore_rhosts = 1;
1.25      markus    137:        if (options->ignore_user_known_hosts == -1)
                    138:                options->ignore_user_known_hosts = 0;
                    139:        if (options->print_motd == -1)
                    140:                options->print_motd = 1;
1.72      stevesk   141:        if (options->print_lastlog == -1)
                    142:                options->print_lastlog = 1;
1.25      markus    143:        if (options->x11_forwarding == -1)
1.30      markus    144:                options->x11_forwarding = 0;
1.25      markus    145:        if (options->x11_display_offset == -1)
1.30      markus    146:                options->x11_display_offset = 10;
1.99      stevesk   147:        if (options->x11_use_localhost == -1)
                    148:                options->x11_use_localhost = 1;
1.42      markus    149:        if (options->xauth_location == NULL)
1.83      markus    150:                options->xauth_location = _PATH_XAUTH;
1.25      markus    151:        if (options->strict_modes == -1)
                    152:                options->strict_modes = 1;
1.129     markus    153:        if (options->tcp_keep_alive == -1)
                    154:                options->tcp_keep_alive = 1;
1.101     markus    155:        if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
1.25      markus    156:                options->log_facility = SYSLOG_FACILITY_AUTH;
1.101     markus    157:        if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.58      markus    158:                options->log_level = SYSLOG_LEVEL_INFO;
1.25      markus    159:        if (options->rhosts_rsa_authentication == -1)
1.30      markus    160:                options->rhosts_rsa_authentication = 0;
1.75      markus    161:        if (options->hostbased_authentication == -1)
                    162:                options->hostbased_authentication = 0;
                    163:        if (options->hostbased_uses_name_from_packet_only == -1)
                    164:                options->hostbased_uses_name_from_packet_only = 0;
1.25      markus    165:        if (options->rsa_authentication == -1)
                    166:                options->rsa_authentication = 1;
1.54      markus    167:        if (options->pubkey_authentication == -1)
                    168:                options->pubkey_authentication = 1;
1.25      markus    169:        if (options->kerberos_authentication == -1)
1.107     markus    170:                options->kerberos_authentication = 0;
1.25      markus    171:        if (options->kerberos_or_local_passwd == -1)
                    172:                options->kerberos_or_local_passwd = 1;
                    173:        if (options->kerberos_ticket_cleanup == -1)
                    174:                options->kerberos_ticket_cleanup = 1;
1.130     jakob     175:        if (options->kerberos_get_afs_token == -1)
                    176:                options->kerberos_get_afs_token = 0;
1.125     markus    177:        if (options->gss_authentication == -1)
                    178:                options->gss_authentication = 0;
                    179:        if (options->gss_cleanup_creds == -1)
                    180:                options->gss_cleanup_creds = 1;
1.25      markus    181:        if (options->password_authentication == -1)
                    182:                options->password_authentication = 1;
1.52      markus    183:        if (options->kbd_interactive_authentication == -1)
                    184:                options->kbd_interactive_authentication = 0;
1.80      markus    185:        if (options->challenge_response_authentication == -1)
                    186:                options->challenge_response_authentication = 1;
1.25      markus    187:        if (options->permit_empty_passwd == -1)
1.30      markus    188:                options->permit_empty_passwd = 0;
1.113     markus    189:        if (options->permit_user_env == -1)
                    190:                options->permit_user_env = 0;
1.25      markus    191:        if (options->use_login == -1)
                    192:                options->use_login = 0;
1.111     markus    193:        if (options->compression == -1)
                    194:                options->compression = 1;
1.53      markus    195:        if (options->allow_tcp_forwarding == -1)
                    196:                options->allow_tcp_forwarding = 1;
1.38      markus    197:        if (options->gateway_ports == -1)
                    198:                options->gateway_ports = 0;
1.46      markus    199:        if (options->max_startups == -1)
                    200:                options->max_startups = 10;
1.50      markus    201:        if (options->max_startups_rate == -1)
                    202:                options->max_startups_rate = 100;               /* 100% */
                    203:        if (options->max_startups_begin == -1)
                    204:                options->max_startups_begin = options->max_startups;
1.130.2.1  brad      205:        if (options->max_authtries == -1)
                    206:                options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
1.122     markus    207:        if (options->use_dns == -1)
                    208:                options->use_dns = 1;
1.77      beck      209:        if (options->client_alive_interval == -1)
1.95      deraadt   210:                options->client_alive_interval = 0;
1.77      beck      211:        if (options->client_alive_count_max == -1)
                    212:                options->client_alive_count_max = 3;
1.90      markus    213:        if (options->authorized_keys_file2 == NULL) {
                    214:                /* authorized_keys_file2 falls back to authorized_keys_file */
                    215:                if (options->authorized_keys_file != NULL)
                    216:                        options->authorized_keys_file2 = options->authorized_keys_file;
                    217:                else
                    218:                        options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
                    219:        }
                    220:        if (options->authorized_keys_file == NULL)
                    221:                options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
1.102     provos    222:
1.110     markus    223:        /* Turn privilege separation on by default */
1.102     provos    224:        if (use_privsep == -1)
1.110     markus    225:                use_privsep = 1;
1.1       deraadt   226: }
                    227:
                    228: /* Keyword tokens. */
1.25      markus    229: typedef enum {
                    230:        sBadOption,             /* == unknown option */
                    231:        sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
                    232:        sPermitRootLogin, sLogFacility, sLogLevel,
1.124     markus    233:        sRhostsRSAAuthentication, sRSAAuthentication,
1.25      markus    234:        sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
1.130     jakob     235:        sKerberosGetAFSToken,
1.123     markus    236:        sKerberosTgtPassing, sChallengeResponseAuthentication,
1.130.2.2! brad      237:        sPasswordAuthentication, sKbdInteractiveAuthentication,
        !           238:        sListenAddress, sAddressFamily,
1.72      stevesk   239:        sPrintMotd, sPrintLastLog, sIgnoreRhosts,
1.99      stevesk   240:        sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
1.129     markus    241:        sStrictModes, sEmptyPasswd, sTCPKeepAlive,
1.113     markus    242:        sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
1.53      markus    243:        sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
1.66      markus    244:        sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
1.130.2.1  brad      245:        sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
                    246:        sMaxStartups, sMaxAuthTries,
1.122     markus    247:        sBanner, sUseDNS, sHostbasedAuthentication,
1.95      deraadt   248:        sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
1.89      jakob     249:        sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
1.130.2.1  brad      250:        sGssAuthentication, sGssCleanupCreds, sAcceptEnv,
1.105     stevesk   251:        sUsePrivilegeSeparation,
1.121     jakob     252:        sDeprecated, sUnsupported
1.1       deraadt   253: } ServerOpCodes;
                    254:
                    255: /* Textual representation of the tokens. */
1.25      markus    256: static struct {
                    257:        const char *name;
                    258:        ServerOpCodes opcode;
                    259: } keywords[] = {
                    260:        { "port", sPort },
                    261:        { "hostkey", sHostKeyFile },
1.54      markus    262:        { "hostdsakey", sHostKeyFile },                                 /* alias */
1.65      stevesk   263:        { "pidfile", sPidFile },
1.25      markus    264:        { "serverkeybits", sServerKeyBits },
                    265:        { "logingracetime", sLoginGraceTime },
                    266:        { "keyregenerationinterval", sKeyRegenerationTime },
                    267:        { "permitrootlogin", sPermitRootLogin },
                    268:        { "syslogfacility", sLogFacility },
                    269:        { "loglevel", sLogLevel },
1.124     markus    270:        { "rhostsauthentication", sDeprecated },
1.25      markus    271:        { "rhostsrsaauthentication", sRhostsRSAAuthentication },
1.75      markus    272:        { "hostbasedauthentication", sHostbasedAuthentication },
                    273:        { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly },
1.25      markus    274:        { "rsaauthentication", sRSAAuthentication },
1.54      markus    275:        { "pubkeyauthentication", sPubkeyAuthentication },
                    276:        { "dsaauthentication", sPubkeyAuthentication },                 /* alias */
1.123     markus    277: #ifdef KRB5
1.25      markus    278:        { "kerberosauthentication", sKerberosAuthentication },
                    279:        { "kerberosorlocalpasswd", sKerberosOrLocalPasswd },
                    280:        { "kerberosticketcleanup", sKerberosTicketCleanup },
1.130     jakob     281:        { "kerberosgetafstoken", sKerberosGetAFSToken },
1.121     jakob     282: #else
                    283:        { "kerberosauthentication", sUnsupported },
                    284:        { "kerberosorlocalpasswd", sUnsupported },
                    285:        { "kerberosticketcleanup", sUnsupported },
1.130     jakob     286:        { "kerberosgetafstoken", sUnsupported },
1.126     markus    287: #endif
1.121     jakob     288:        { "kerberostgtpassing", sUnsupported },
                    289:        { "afstokenpassing", sUnsupported },
1.125     markus    290: #ifdef GSSAPI
                    291:        { "gssapiauthentication", sGssAuthentication },
1.128     markus    292:        { "gssapicleanupcredentials", sGssCleanupCreds },
1.125     markus    293: #else
                    294:        { "gssapiauthentication", sUnsupported },
1.128     markus    295:        { "gssapicleanupcredentials", sUnsupported },
1.125     markus    296: #endif
1.25      markus    297:        { "passwordauthentication", sPasswordAuthentication },
1.52      markus    298:        { "kbdinteractiveauthentication", sKbdInteractiveAuthentication },
1.63      markus    299:        { "challengeresponseauthentication", sChallengeResponseAuthentication },
                    300:        { "skeyauthentication", sChallengeResponseAuthentication }, /* alias */
1.89      jakob     301:        { "checkmail", sDeprecated },
1.25      markus    302:        { "listenaddress", sListenAddress },
1.130.2.2! brad      303:        { "addressfamily", sAddressFamily },
1.25      markus    304:        { "printmotd", sPrintMotd },
1.72      stevesk   305:        { "printlastlog", sPrintLastLog },
1.25      markus    306:        { "ignorerhosts", sIgnoreRhosts },
                    307:        { "ignoreuserknownhosts", sIgnoreUserKnownHosts },
                    308:        { "x11forwarding", sX11Forwarding },
                    309:        { "x11displayoffset", sX11DisplayOffset },
1.99      stevesk   310:        { "x11uselocalhost", sX11UseLocalhost },
1.42      markus    311:        { "xauthlocation", sXAuthLocation },
1.25      markus    312:        { "strictmodes", sStrictModes },
                    313:        { "permitemptypasswords", sEmptyPasswd },
1.113     markus    314:        { "permituserenvironment", sPermitUserEnvironment },
1.25      markus    315:        { "uselogin", sUseLogin },
1.111     markus    316:        { "compression", sCompression },
1.129     markus    317:        { "tcpkeepalive", sTCPKeepAlive },
                    318:        { "keepalive", sTCPKeepAlive },                         /* obsolete alias */
1.53      markus    319:        { "allowtcpforwarding", sAllowTcpForwarding },
1.25      markus    320:        { "allowusers", sAllowUsers },
                    321:        { "denyusers", sDenyUsers },
                    322:        { "allowgroups", sAllowGroups },
                    323:        { "denygroups", sDenyGroups },
1.33      markus    324:        { "ciphers", sCiphers },
1.66      markus    325:        { "macs", sMacs },
1.33      markus    326:        { "protocol", sProtocol },
1.38      markus    327:        { "gatewayports", sGatewayPorts },
1.43      jakob     328:        { "subsystem", sSubsystem },
1.46      markus    329:        { "maxstartups", sMaxStartups },
1.130.2.1  brad      330:        { "maxauthtries", sMaxAuthTries },
1.57      markus    331:        { "banner", sBanner },
1.122     markus    332:        { "usedns", sUseDNS },
                    333:        { "verifyreversemapping", sDeprecated },
                    334:        { "reversemappingcheck", sDeprecated },
1.77      beck      335:        { "clientaliveinterval", sClientAliveInterval },
                    336:        { "clientalivecountmax", sClientAliveCountMax },
1.82      markus    337:        { "authorizedkeysfile", sAuthorizedKeysFile },
                    338:        { "authorizedkeysfile2", sAuthorizedKeysFile2 },
1.102     provos    339:        { "useprivilegeseparation", sUsePrivilegeSeparation},
1.130.2.1  brad      340:        { "acceptenv", sAcceptEnv },
1.92      stevesk   341:        { NULL, sBadOption }
1.1       deraadt   342: };
                    343:
1.27      markus    344: /*
1.73      stevesk   345:  * Returns the number of the token pointed to by cp or sBadOption.
1.27      markus    346:  */
1.1       deraadt   347:
1.34      markus    348: static ServerOpCodes
1.25      markus    349: parse_token(const char *cp, const char *filename,
                    350:            int linenum)
1.1       deraadt   351: {
1.55      markus    352:        u_int i;
1.1       deraadt   353:
1.25      markus    354:        for (i = 0; keywords[i].name; i++)
1.28      markus    355:                if (strcasecmp(cp, keywords[i].name) == 0)
1.25      markus    356:                        return keywords[i].opcode;
                    357:
1.78      stevesk   358:        error("%s: line %d: Bad configuration option: %s",
                    359:            filename, linenum, cp);
1.25      markus    360:        return sBadOption;
1.1       deraadt   361: }
                    362:
1.84      itojun    363: static void
1.76      stevesk   364: add_listen_addr(ServerOptions *options, char *addr, u_short port)
1.74      stevesk   365: {
                    366:        int i;
                    367:
                    368:        if (options->num_ports == 0)
                    369:                options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
1.130.2.2! brad      370:        if (options->address_family == -1)
        !           371:                options->address_family = AF_UNSPEC;
1.76      stevesk   372:        if (port == 0)
1.74      stevesk   373:                for (i = 0; i < options->num_ports; i++)
                    374:                        add_one_listen_addr(options, addr, options->ports[i]);
                    375:        else
1.76      stevesk   376:                add_one_listen_addr(options, addr, port);
1.74      stevesk   377: }
                    378:
1.84      itojun    379: static void
1.74      stevesk   380: add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
1.29      markus    381: {
                    382:        struct addrinfo hints, *ai, *aitop;
                    383:        char strport[NI_MAXSERV];
                    384:        int gaierr;
                    385:
1.74      stevesk   386:        memset(&hints, 0, sizeof(hints));
1.130.2.2! brad      387:        hints.ai_family = options->address_family;
1.74      stevesk   388:        hints.ai_socktype = SOCK_STREAM;
                    389:        hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
1.112     deraadt   390:        snprintf(strport, sizeof strport, "%u", port);
1.74      stevesk   391:        if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
                    392:                fatal("bad addr or host: %s (%s)",
                    393:                    addr ? addr : "<NULL>",
                    394:                    gai_strerror(gaierr));
                    395:        for (ai = aitop; ai->ai_next; ai = ai->ai_next)
                    396:                ;
                    397:        ai->ai_next = options->listen_addrs;
                    398:        options->listen_addrs = aitop;
1.29      markus    399: }
                    400:
1.94      markus    401: int
                    402: process_server_config_line(ServerOptions *options, char *line,
                    403:     const char *filename, int linenum)
1.1       deraadt   404: {
1.74      stevesk   405:        char *cp, **charptr, *arg, *p;
1.112     deraadt   406:        int *intptr, value, i, n;
1.25      markus    407:        ServerOpCodes opcode;
1.130.2.2! brad      408:        u_short port;
1.25      markus    409:
1.94      markus    410:        cp = line;
                    411:        arg = strdelim(&cp);
                    412:        /* Ignore leading whitespace */
                    413:        if (*arg == '\0')
                    414:                arg = strdelim(&cp);
                    415:        if (!arg || !*arg || *arg == '#')
                    416:                return 0;
                    417:        intptr = NULL;
                    418:        charptr = NULL;
                    419:        opcode = parse_token(arg, filename, linenum);
                    420:        switch (opcode) {
                    421:        case sBadOption:
                    422:                return -1;
                    423:        case sPort:
                    424:                /* ignore ports from configfile if cmdline specifies ports */
                    425:                if (options->ports_from_cmdline)
                    426:                        return 0;
                    427:                if (options->listen_addrs != NULL)
                    428:                        fatal("%s line %d: ports must be specified before "
1.98      stevesk   429:                            "ListenAddress.", filename, linenum);
1.94      markus    430:                if (options->num_ports >= MAX_PORTS)
                    431:                        fatal("%s line %d: too many ports.",
                    432:                            filename, linenum);
1.48      provos    433:                arg = strdelim(&cp);
1.94      markus    434:                if (!arg || *arg == '\0')
                    435:                        fatal("%s line %d: missing port number.",
                    436:                            filename, linenum);
                    437:                options->ports[options->num_ports++] = a2port(arg);
                    438:                if (options->ports[options->num_ports-1] == 0)
                    439:                        fatal("%s line %d: Badly formatted port number.",
                    440:                            filename, linenum);
                    441:                break;
1.29      markus    442:
1.94      markus    443:        case sServerKeyBits:
                    444:                intptr = &options->server_key_bits;
1.25      markus    445: parse_int:
1.94      markus    446:                arg = strdelim(&cp);
                    447:                if (!arg || *arg == '\0')
                    448:                        fatal("%s line %d: missing integer value.",
                    449:                            filename, linenum);
                    450:                value = atoi(arg);
                    451:                if (*intptr == -1)
                    452:                        *intptr = value;
                    453:                break;
1.25      markus    454:
1.94      markus    455:        case sLoginGraceTime:
                    456:                intptr = &options->login_grace_time;
1.81      stevesk   457: parse_time:
1.94      markus    458:                arg = strdelim(&cp);
                    459:                if (!arg || *arg == '\0')
                    460:                        fatal("%s line %d: missing time value.",
                    461:                            filename, linenum);
                    462:                if ((value = convtime(arg)) == -1)
                    463:                        fatal("%s line %d: invalid time value.",
                    464:                            filename, linenum);
                    465:                if (*intptr == -1)
                    466:                        *intptr = value;
                    467:                break;
                    468:
                    469:        case sKeyRegenerationTime:
                    470:                intptr = &options->key_regeneration_time;
                    471:                goto parse_time;
                    472:
                    473:        case sListenAddress:
                    474:                arg = strdelim(&cp);
1.130.2.2! brad      475:                if (arg == NULL || *arg == '\0')
        !           476:                        fatal("%s line %d: missing address",
1.94      markus    477:                            filename, linenum);
1.130.2.2! brad      478:                p = hpdelim(&arg);
        !           479:                if (p == NULL)
        !           480:                        fatal("%s line %d: bad address:port usage",
        !           481:                            filename, linenum);
        !           482:                p = cleanhostname(p);
        !           483:                if (arg == NULL)
        !           484:                        port = 0;
        !           485:                else if ((port = a2port(arg)) == 0)
        !           486:                        fatal("%s line %d: bad port number", filename, linenum);
1.25      markus    487:
1.130.2.2! brad      488:                add_listen_addr(options, p, port);
        !           489:
        !           490:                break;
        !           491:
        !           492:        case sAddressFamily:
        !           493:                arg = strdelim(&cp);
        !           494:                intptr = &options->address_family;
        !           495:                if (options->listen_addrs != NULL)
        !           496:                        fatal("%s line %d: address family must be specified before "
        !           497:                            "ListenAddress.", filename, linenum);
        !           498:                if (strcasecmp(arg, "inet") == 0)
        !           499:                        value = AF_INET;
        !           500:                else if (strcasecmp(arg, "inet6") == 0)
        !           501:                        value = AF_INET6;
        !           502:                else if (strcasecmp(arg, "any") == 0)
        !           503:                        value = AF_UNSPEC;
1.94      markus    504:                else
1.130.2.2! brad      505:                        fatal("%s line %d: unsupported address family \"%s\".",
        !           506:                            filename, linenum, arg);
        !           507:                if (*intptr == -1)
        !           508:                        *intptr = value;
1.94      markus    509:                break;
                    510:
                    511:        case sHostKeyFile:
                    512:                intptr = &options->num_host_key_files;
                    513:                if (*intptr >= MAX_HOSTKEYS)
                    514:                        fatal("%s line %d: too many host keys specified (max %d).",
                    515:                            filename, linenum, MAX_HOSTKEYS);
                    516:                charptr = &options->host_key_files[*intptr];
                    517: parse_filename:
                    518:                arg = strdelim(&cp);
                    519:                if (!arg || *arg == '\0')
                    520:                        fatal("%s line %d: missing file name.",
                    521:                            filename, linenum);
                    522:                if (*charptr == NULL) {
                    523:                        *charptr = tilde_expand_filename(arg, getuid());
                    524:                        /* increase optional counter */
                    525:                        if (intptr != NULL)
                    526:                                *intptr = *intptr + 1;
                    527:                }
                    528:                break;
1.76      stevesk   529:
1.94      markus    530:        case sPidFile:
                    531:                charptr = &options->pid_file;
                    532:                goto parse_filename;
1.25      markus    533:
1.94      markus    534:        case sPermitRootLogin:
                    535:                intptr = &options->permit_root_login;
                    536:                arg = strdelim(&cp);
                    537:                if (!arg || *arg == '\0')
                    538:                        fatal("%s line %d: missing yes/"
                    539:                            "without-password/forced-commands-only/no "
                    540:                            "argument.", filename, linenum);
                    541:                value = 0;      /* silence compiler */
                    542:                if (strcmp(arg, "without-password") == 0)
                    543:                        value = PERMIT_NO_PASSWD;
                    544:                else if (strcmp(arg, "forced-commands-only") == 0)
                    545:                        value = PERMIT_FORCED_ONLY;
                    546:                else if (strcmp(arg, "yes") == 0)
                    547:                        value = PERMIT_YES;
                    548:                else if (strcmp(arg, "no") == 0)
                    549:                        value = PERMIT_NO;
                    550:                else
                    551:                        fatal("%s line %d: Bad yes/"
                    552:                            "without-password/forced-commands-only/no "
                    553:                            "argument: %s", filename, linenum, arg);
                    554:                if (*intptr == -1)
                    555:                        *intptr = value;
                    556:                break;
1.36      markus    557:
1.94      markus    558:        case sIgnoreRhosts:
                    559:                intptr = &options->ignore_rhosts;
1.25      markus    560: parse_flag:
1.94      markus    561:                arg = strdelim(&cp);
                    562:                if (!arg || *arg == '\0')
                    563:                        fatal("%s line %d: missing yes/no argument.",
                    564:                            filename, linenum);
                    565:                value = 0;      /* silence compiler */
                    566:                if (strcmp(arg, "yes") == 0)
                    567:                        value = 1;
                    568:                else if (strcmp(arg, "no") == 0)
                    569:                        value = 0;
                    570:                else
                    571:                        fatal("%s line %d: Bad yes/no argument: %s",
                    572:                                filename, linenum, arg);
                    573:                if (*intptr == -1)
                    574:                        *intptr = value;
                    575:                break;
                    576:
                    577:        case sIgnoreUserKnownHosts:
                    578:                intptr = &options->ignore_user_known_hosts;
                    579:                goto parse_flag;
                    580:
                    581:        case sRhostsRSAAuthentication:
                    582:                intptr = &options->rhosts_rsa_authentication;
                    583:                goto parse_flag;
                    584:
                    585:        case sHostbasedAuthentication:
                    586:                intptr = &options->hostbased_authentication;
                    587:                goto parse_flag;
                    588:
                    589:        case sHostbasedUsesNameFromPacketOnly:
                    590:                intptr = &options->hostbased_uses_name_from_packet_only;
                    591:                goto parse_flag;
                    592:
                    593:        case sRSAAuthentication:
                    594:                intptr = &options->rsa_authentication;
                    595:                goto parse_flag;
                    596:
                    597:        case sPubkeyAuthentication:
                    598:                intptr = &options->pubkey_authentication;
                    599:                goto parse_flag;
1.119     jakob     600:
1.94      markus    601:        case sKerberosAuthentication:
                    602:                intptr = &options->kerberos_authentication;
                    603:                goto parse_flag;
                    604:
                    605:        case sKerberosOrLocalPasswd:
                    606:                intptr = &options->kerberos_or_local_passwd;
                    607:                goto parse_flag;
                    608:
                    609:        case sKerberosTicketCleanup:
                    610:                intptr = &options->kerberos_ticket_cleanup;
1.130     jakob     611:                goto parse_flag;
                    612:
                    613:        case sKerberosGetAFSToken:
                    614:                intptr = &options->kerberos_get_afs_token;
1.125     markus    615:                goto parse_flag;
                    616:
                    617:        case sGssAuthentication:
                    618:                intptr = &options->gss_authentication;
                    619:                goto parse_flag;
                    620:
                    621:        case sGssCleanupCreds:
                    622:                intptr = &options->gss_cleanup_creds;
1.94      markus    623:                goto parse_flag;
                    624:
                    625:        case sPasswordAuthentication:
                    626:                intptr = &options->password_authentication;
                    627:                goto parse_flag;
                    628:
                    629:        case sKbdInteractiveAuthentication:
                    630:                intptr = &options->kbd_interactive_authentication;
                    631:                goto parse_flag;
                    632:
                    633:        case sChallengeResponseAuthentication:
                    634:                intptr = &options->challenge_response_authentication;
                    635:                goto parse_flag;
                    636:
                    637:        case sPrintMotd:
                    638:                intptr = &options->print_motd;
                    639:                goto parse_flag;
                    640:
                    641:        case sPrintLastLog:
                    642:                intptr = &options->print_lastlog;
                    643:                goto parse_flag;
                    644:
                    645:        case sX11Forwarding:
                    646:                intptr = &options->x11_forwarding;
                    647:                goto parse_flag;
                    648:
                    649:        case sX11DisplayOffset:
                    650:                intptr = &options->x11_display_offset;
                    651:                goto parse_int;
1.99      stevesk   652:
                    653:        case sX11UseLocalhost:
                    654:                intptr = &options->x11_use_localhost;
                    655:                goto parse_flag;
1.94      markus    656:
                    657:        case sXAuthLocation:
                    658:                charptr = &options->xauth_location;
                    659:                goto parse_filename;
                    660:
                    661:        case sStrictModes:
                    662:                intptr = &options->strict_modes;
                    663:                goto parse_flag;
                    664:
1.129     markus    665:        case sTCPKeepAlive:
                    666:                intptr = &options->tcp_keep_alive;
1.94      markus    667:                goto parse_flag;
                    668:
                    669:        case sEmptyPasswd:
                    670:                intptr = &options->permit_empty_passwd;
1.113     markus    671:                goto parse_flag;
                    672:
                    673:        case sPermitUserEnvironment:
                    674:                intptr = &options->permit_user_env;
1.94      markus    675:                goto parse_flag;
                    676:
                    677:        case sUseLogin:
                    678:                intptr = &options->use_login;
1.111     markus    679:                goto parse_flag;
                    680:
                    681:        case sCompression:
                    682:                intptr = &options->compression;
1.94      markus    683:                goto parse_flag;
                    684:
                    685:        case sGatewayPorts:
                    686:                intptr = &options->gateway_ports;
1.130.2.2! brad      687:                arg = strdelim(&cp);
        !           688:                if (!arg || *arg == '\0')
        !           689:                        fatal("%s line %d: missing yes/no/clientspecified "
        !           690:                            "argument.", filename, linenum);
        !           691:                value = 0;      /* silence compiler */
        !           692:                if (strcmp(arg, "clientspecified") == 0)
        !           693:                        value = 2;
        !           694:                else if (strcmp(arg, "yes") == 0)
        !           695:                        value = 1;
        !           696:                else if (strcmp(arg, "no") == 0)
        !           697:                        value = 0;
        !           698:                else
        !           699:                        fatal("%s line %d: Bad yes/no/clientspecified "
        !           700:                            "argument: %s", filename, linenum, arg);
        !           701:                if (*intptr == -1)
        !           702:                        *intptr = value;
        !           703:                break;
1.25      markus    704:
1.122     markus    705:        case sUseDNS:
                    706:                intptr = &options->use_dns;
1.94      markus    707:                goto parse_flag;
1.53      markus    708:
1.94      markus    709:        case sLogFacility:
                    710:                intptr = (int *) &options->log_facility;
                    711:                arg = strdelim(&cp);
                    712:                value = log_facility_number(arg);
1.101     markus    713:                if (value == SYSLOG_FACILITY_NOT_SET)
1.94      markus    714:                        fatal("%.200s line %d: unsupported log facility '%s'",
                    715:                            filename, linenum, arg ? arg : "<NONE>");
                    716:                if (*intptr == -1)
                    717:                        *intptr = (SyslogFacility) value;
                    718:                break;
1.25      markus    719:
1.94      markus    720:        case sLogLevel:
                    721:                intptr = (int *) &options->log_level;
                    722:                arg = strdelim(&cp);
                    723:                value = log_level_number(arg);
1.101     markus    724:                if (value == SYSLOG_LEVEL_NOT_SET)
1.94      markus    725:                        fatal("%.200s line %d: unsupported log level '%s'",
                    726:                            filename, linenum, arg ? arg : "<NONE>");
                    727:                if (*intptr == -1)
                    728:                        *intptr = (LogLevel) value;
                    729:                break;
                    730:
                    731:        case sAllowTcpForwarding:
                    732:                intptr = &options->allow_tcp_forwarding;
                    733:                goto parse_flag;
1.102     provos    734:
                    735:        case sUsePrivilegeSeparation:
                    736:                intptr = &use_privsep;
                    737:                goto parse_flag;
1.94      markus    738:
                    739:        case sAllowUsers:
                    740:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    741:                        if (options->num_allow_users >= MAX_ALLOW_USERS)
                    742:                                fatal("%s line %d: too many allow users.",
                    743:                                    filename, linenum);
1.112     deraadt   744:                        options->allow_users[options->num_allow_users++] =
                    745:                            xstrdup(arg);
1.94      markus    746:                }
                    747:                break;
1.25      markus    748:
1.94      markus    749:        case sDenyUsers:
                    750:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    751:                        if (options->num_deny_users >= MAX_DENY_USERS)
                    752:                                fatal( "%s line %d: too many deny users.",
                    753:                                    filename, linenum);
1.112     deraadt   754:                        options->deny_users[options->num_deny_users++] =
                    755:                            xstrdup(arg);
1.94      markus    756:                }
                    757:                break;
1.25      markus    758:
1.94      markus    759:        case sAllowGroups:
                    760:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    761:                        if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
                    762:                                fatal("%s line %d: too many allow groups.",
                    763:                                    filename, linenum);
1.112     deraadt   764:                        options->allow_groups[options->num_allow_groups++] =
                    765:                            xstrdup(arg);
1.94      markus    766:                }
                    767:                break;
1.33      markus    768:
1.94      markus    769:        case sDenyGroups:
                    770:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    771:                        if (options->num_deny_groups >= MAX_DENY_GROUPS)
                    772:                                fatal("%s line %d: too many deny groups.",
                    773:                                    filename, linenum);
                    774:                        options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
                    775:                }
                    776:                break;
1.66      markus    777:
1.94      markus    778:        case sCiphers:
                    779:                arg = strdelim(&cp);
                    780:                if (!arg || *arg == '\0')
                    781:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    782:                if (!ciphers_valid(arg))
                    783:                        fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
                    784:                            filename, linenum, arg ? arg : "<NONE>");
                    785:                if (options->ciphers == NULL)
                    786:                        options->ciphers = xstrdup(arg);
                    787:                break;
1.33      markus    788:
1.94      markus    789:        case sMacs:
                    790:                arg = strdelim(&cp);
                    791:                if (!arg || *arg == '\0')
                    792:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    793:                if (!mac_valid(arg))
                    794:                        fatal("%s line %d: Bad SSH2 mac spec '%s'.",
                    795:                            filename, linenum, arg ? arg : "<NONE>");
                    796:                if (options->macs == NULL)
                    797:                        options->macs = xstrdup(arg);
                    798:                break;
1.43      jakob     799:
1.94      markus    800:        case sProtocol:
                    801:                intptr = &options->protocol;
                    802:                arg = strdelim(&cp);
                    803:                if (!arg || *arg == '\0')
                    804:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    805:                value = proto_spec(arg);
                    806:                if (value == SSH_PROTO_UNKNOWN)
                    807:                        fatal("%s line %d: Bad protocol spec '%s'.",
1.95      deraadt   808:                            filename, linenum, arg ? arg : "<NONE>");
1.94      markus    809:                if (*intptr == SSH_PROTO_UNKNOWN)
                    810:                        *intptr = value;
                    811:                break;
                    812:
                    813:        case sSubsystem:
                    814:                if (options->num_subsystems >= MAX_SUBSYSTEMS) {
                    815:                        fatal("%s line %d: too many subsystems defined.",
1.95      deraadt   816:                            filename, linenum);
1.94      markus    817:                }
                    818:                arg = strdelim(&cp);
                    819:                if (!arg || *arg == '\0')
                    820:                        fatal("%s line %d: Missing subsystem name.",
1.95      deraadt   821:                            filename, linenum);
1.94      markus    822:                for (i = 0; i < options->num_subsystems; i++)
                    823:                        if (strcmp(arg, options->subsystem_name[i]) == 0)
                    824:                                fatal("%s line %d: Subsystem '%s' already defined.",
1.95      deraadt   825:                                    filename, linenum, arg);
1.94      markus    826:                options->subsystem_name[options->num_subsystems] = xstrdup(arg);
                    827:                arg = strdelim(&cp);
                    828:                if (!arg || *arg == '\0')
                    829:                        fatal("%s line %d: Missing subsystem command.",
1.95      deraadt   830:                            filename, linenum);
1.94      markus    831:                options->subsystem_command[options->num_subsystems] = xstrdup(arg);
                    832:                options->num_subsystems++;
                    833:                break;
1.46      markus    834:
1.94      markus    835:        case sMaxStartups:
                    836:                arg = strdelim(&cp);
                    837:                if (!arg || *arg == '\0')
                    838:                        fatal("%s line %d: Missing MaxStartups spec.",
1.95      deraadt   839:                            filename, linenum);
1.94      markus    840:                if ((n = sscanf(arg, "%d:%d:%d",
                    841:                    &options->max_startups_begin,
                    842:                    &options->max_startups_rate,
                    843:                    &options->max_startups)) == 3) {
                    844:                        if (options->max_startups_begin >
                    845:                            options->max_startups ||
                    846:                            options->max_startups_rate > 100 ||
                    847:                            options->max_startups_rate < 1)
1.50      markus    848:                                fatal("%s line %d: Illegal MaxStartups spec.",
1.87      stevesk   849:                                    filename, linenum);
1.94      markus    850:                } else if (n != 1)
                    851:                        fatal("%s line %d: Illegal MaxStartups spec.",
                    852:                            filename, linenum);
                    853:                else
                    854:                        options->max_startups = options->max_startups_begin;
                    855:                break;
                    856:
1.130.2.1  brad      857:        case sMaxAuthTries:
                    858:                intptr = &options->max_authtries;
                    859:                goto parse_int;
                    860:
1.94      markus    861:        case sBanner:
                    862:                charptr = &options->banner;
                    863:                goto parse_filename;
                    864:        /*
                    865:         * These options can contain %X options expanded at
                    866:         * connect time, so that you can specify paths like:
                    867:         *
                    868:         * AuthorizedKeysFile   /etc/ssh_keys/%u
                    869:         */
                    870:        case sAuthorizedKeysFile:
                    871:        case sAuthorizedKeysFile2:
                    872:                charptr = (opcode == sAuthorizedKeysFile ) ?
                    873:                    &options->authorized_keys_file :
                    874:                    &options->authorized_keys_file2;
                    875:                goto parse_filename;
                    876:
                    877:        case sClientAliveInterval:
                    878:                intptr = &options->client_alive_interval;
                    879:                goto parse_time;
                    880:
                    881:        case sClientAliveCountMax:
                    882:                intptr = &options->client_alive_count_max;
                    883:                goto parse_int;
                    884:
1.130.2.1  brad      885:        case sAcceptEnv:
                    886:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    887:                        if (strchr(arg, '=') != NULL)
                    888:                                fatal("%s line %d: Invalid environment name.",
                    889:                                    filename, linenum);
                    890:                        if (options->num_accept_env >= MAX_ACCEPT_ENV)
                    891:                                fatal("%s line %d: too many allow env.",
                    892:                                    filename, linenum);
                    893:                        options->accept_env[options->num_accept_env++] =
                    894:                            xstrdup(arg);
                    895:                }
                    896:                break;
                    897:
1.94      markus    898:        case sDeprecated:
1.117     itojun    899:                logit("%s line %d: Deprecated option %s",
1.121     jakob     900:                    filename, linenum, arg);
                    901:                while (arg)
                    902:                    arg = strdelim(&cp);
                    903:                break;
                    904:
                    905:        case sUnsupported:
                    906:                logit("%s line %d: Unsupported option %s",
1.94      markus    907:                    filename, linenum, arg);
                    908:                while (arg)
                    909:                    arg = strdelim(&cp);
                    910:                break;
                    911:
                    912:        default:
                    913:                fatal("%s line %d: Missing handler for opcode %s (%d)",
                    914:                    filename, linenum, arg, opcode);
                    915:        }
                    916:        if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
                    917:                fatal("%s line %d: garbage at end of line; \"%.200s\".",
                    918:                    filename, linenum, arg);
                    919:        return 0;
                    920: }
                    921:
                    922: /* Reads the server configuration file. */
1.25      markus    923:
1.94      markus    924: void
1.130.2.1  brad      925: load_server_config(const char *filename, Buffer *conf)
1.94      markus    926: {
1.130.2.1  brad      927:        char line[1024], *cp;
1.94      markus    928:        FILE *f;
1.81      stevesk   929:
1.130.2.1  brad      930:        debug2("%s: filename %s", __func__, filename);
                    931:        if ((f = fopen(filename, "r")) == NULL) {
1.94      markus    932:                perror(filename);
                    933:                exit(1);
                    934:        }
1.130.2.1  brad      935:        buffer_clear(conf);
1.94      markus    936:        while (fgets(line, sizeof(line), f)) {
1.130.2.1  brad      937:                /*
                    938:                 * Trim out comments and strip whitespace
                    939:                 * NB - preserve newlines, they are needed to reproduce
                    940:                 * line numbers later for error messages
                    941:                 */
                    942:                if ((cp = strchr(line, '#')) != NULL)
                    943:                        memcpy(cp, "\n", 2);
                    944:                cp = line + strspn(line, " \t\r");
                    945:
                    946:                buffer_append(conf, cp, strlen(cp));
1.1       deraadt   947:        }
1.130.2.1  brad      948:        buffer_append(conf, "\0", 1);
1.25      markus    949:        fclose(f);
1.130.2.1  brad      950:        debug2("%s: done config len = %d", __func__, buffer_len(conf));
                    951: }
                    952:
                    953: void
                    954: parse_server_config(ServerOptions *options, const char *filename, Buffer *conf)
                    955: {
                    956:        int linenum, bad_options = 0;
                    957:        char *cp, *obuf, *cbuf;
                    958:
                    959:        debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
                    960:
                    961:        obuf = cbuf = xstrdup(buffer_ptr(conf));
                    962:        linenum = 1;
                    963:        while((cp = strsep(&cbuf, "\n")) != NULL) {
                    964:                if (process_server_config_line(options, cp, filename,
                    965:                    linenum++) != 0)
                    966:                        bad_options++;
                    967:        }
                    968:        xfree(obuf);
1.78      stevesk   969:        if (bad_options > 0)
                    970:                fatal("%s: terminating, %d bad configuration options",
                    971:                    filename, bad_options);
1.1       deraadt   972: }