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

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.138   ! djm        13: RCSID("$OpenBSD: servconf.c,v 1.137 2004/08/13 11:09:24 dtucker 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.138   ! djm        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.133     dtucker    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.131     djm        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.133     dtucker   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.138   ! djm       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.133     dtucker   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.131     djm       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.138   ! djm       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.133     dtucker   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.131     djm       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.138   ! djm       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.138   ! djm       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;
                    408:
1.94      markus    409:        cp = line;
                    410:        arg = strdelim(&cp);
                    411:        /* Ignore leading whitespace */
                    412:        if (*arg == '\0')
                    413:                arg = strdelim(&cp);
                    414:        if (!arg || !*arg || *arg == '#')
                    415:                return 0;
                    416:        intptr = NULL;
                    417:        charptr = NULL;
                    418:        opcode = parse_token(arg, filename, linenum);
                    419:        switch (opcode) {
                    420:        case sBadOption:
                    421:                return -1;
                    422:        case sPort:
                    423:                /* ignore ports from configfile if cmdline specifies ports */
                    424:                if (options->ports_from_cmdline)
                    425:                        return 0;
                    426:                if (options->listen_addrs != NULL)
                    427:                        fatal("%s line %d: ports must be specified before "
1.98      stevesk   428:                            "ListenAddress.", filename, linenum);
1.94      markus    429:                if (options->num_ports >= MAX_PORTS)
                    430:                        fatal("%s line %d: too many ports.",
                    431:                            filename, linenum);
1.48      provos    432:                arg = strdelim(&cp);
1.94      markus    433:                if (!arg || *arg == '\0')
                    434:                        fatal("%s line %d: missing port number.",
                    435:                            filename, linenum);
                    436:                options->ports[options->num_ports++] = a2port(arg);
                    437:                if (options->ports[options->num_ports-1] == 0)
                    438:                        fatal("%s line %d: Badly formatted port number.",
                    439:                            filename, linenum);
                    440:                break;
1.29      markus    441:
1.94      markus    442:        case sServerKeyBits:
                    443:                intptr = &options->server_key_bits;
1.25      markus    444: parse_int:
1.94      markus    445:                arg = strdelim(&cp);
                    446:                if (!arg || *arg == '\0')
                    447:                        fatal("%s line %d: missing integer value.",
                    448:                            filename, linenum);
                    449:                value = atoi(arg);
                    450:                if (*intptr == -1)
                    451:                        *intptr = value;
                    452:                break;
1.25      markus    453:
1.94      markus    454:        case sLoginGraceTime:
                    455:                intptr = &options->login_grace_time;
1.81      stevesk   456: parse_time:
1.94      markus    457:                arg = strdelim(&cp);
                    458:                if (!arg || *arg == '\0')
                    459:                        fatal("%s line %d: missing time value.",
                    460:                            filename, linenum);
                    461:                if ((value = convtime(arg)) == -1)
                    462:                        fatal("%s line %d: invalid time value.",
                    463:                            filename, linenum);
                    464:                if (*intptr == -1)
                    465:                        *intptr = value;
                    466:                break;
                    467:
                    468:        case sKeyRegenerationTime:
                    469:                intptr = &options->key_regeneration_time;
                    470:                goto parse_time;
                    471:
                    472:        case sListenAddress:
                    473:                arg = strdelim(&cp);
                    474:                if (!arg || *arg == '\0' || strncmp(arg, "[]", 2) == 0)
                    475:                        fatal("%s line %d: missing inet addr.",
                    476:                            filename, linenum);
                    477:                if (*arg == '[') {
                    478:                        if ((p = strchr(arg, ']')) == NULL)
                    479:                                fatal("%s line %d: bad ipv6 inet addr usage.",
                    480:                                    filename, linenum);
                    481:                        arg++;
                    482:                        memmove(p, p+1, strlen(p+1)+1);
                    483:                } else if (((p = strchr(arg, ':')) == NULL) ||
                    484:                            (strchr(p+1, ':') != NULL)) {
                    485:                        add_listen_addr(options, arg, 0);
1.81      stevesk   486:                        break;
1.94      markus    487:                }
                    488:                if (*p == ':') {
                    489:                        u_short port;
1.25      markus    490:
1.94      markus    491:                        p++;
                    492:                        if (*p == '\0')
                    493:                                fatal("%s line %d: bad inet addr:port usage.",
                    494:                                    filename, linenum);
                    495:                        else {
                    496:                                *(p-1) = '\0';
                    497:                                if ((port = a2port(p)) == 0)
                    498:                                        fatal("%s line %d: bad port number.",
1.74      stevesk   499:                                            filename, linenum);
1.94      markus    500:                                add_listen_addr(options, arg, port);
1.74      stevesk   501:                        }
1.94      markus    502:                } else if (*p == '\0')
                    503:                        add_listen_addr(options, arg, 0);
                    504:                else
                    505:                        fatal("%s line %d: bad inet addr usage.",
                    506:                            filename, linenum);
1.138   ! djm       507:                break;
        !           508:
        !           509:        case sAddressFamily:
        !           510:                arg = strdelim(&cp);
        !           511:                intptr = &options->address_family;
        !           512:                if (options->listen_addrs != NULL)
        !           513:                        fatal("%s line %d: address family must be specified before "
        !           514:                            "ListenAddress.", filename, linenum);
        !           515:                if (strcasecmp(arg, "inet") == 0)
        !           516:                        value = AF_INET;
        !           517:                else if (strcasecmp(arg, "inet6") == 0)
        !           518:                        value = AF_INET6;
        !           519:                else if (strcasecmp(arg, "any") == 0)
        !           520:                        value = AF_UNSPEC;
        !           521:                else
        !           522:                        fatal("%s line %d: unsupported address family \"%s\".",
        !           523:                            filename, linenum, arg);
        !           524:                if (*intptr == -1)
        !           525:                        *intptr = value;
1.94      markus    526:                break;
                    527:
                    528:        case sHostKeyFile:
                    529:                intptr = &options->num_host_key_files;
                    530:                if (*intptr >= MAX_HOSTKEYS)
                    531:                        fatal("%s line %d: too many host keys specified (max %d).",
                    532:                            filename, linenum, MAX_HOSTKEYS);
                    533:                charptr = &options->host_key_files[*intptr];
                    534: parse_filename:
                    535:                arg = strdelim(&cp);
                    536:                if (!arg || *arg == '\0')
                    537:                        fatal("%s line %d: missing file name.",
                    538:                            filename, linenum);
                    539:                if (*charptr == NULL) {
                    540:                        *charptr = tilde_expand_filename(arg, getuid());
                    541:                        /* increase optional counter */
                    542:                        if (intptr != NULL)
                    543:                                *intptr = *intptr + 1;
                    544:                }
                    545:                break;
1.76      stevesk   546:
1.94      markus    547:        case sPidFile:
                    548:                charptr = &options->pid_file;
                    549:                goto parse_filename;
1.25      markus    550:
1.94      markus    551:        case sPermitRootLogin:
                    552:                intptr = &options->permit_root_login;
                    553:                arg = strdelim(&cp);
                    554:                if (!arg || *arg == '\0')
                    555:                        fatal("%s line %d: missing yes/"
                    556:                            "without-password/forced-commands-only/no "
                    557:                            "argument.", filename, linenum);
                    558:                value = 0;      /* silence compiler */
                    559:                if (strcmp(arg, "without-password") == 0)
                    560:                        value = PERMIT_NO_PASSWD;
                    561:                else if (strcmp(arg, "forced-commands-only") == 0)
                    562:                        value = PERMIT_FORCED_ONLY;
                    563:                else if (strcmp(arg, "yes") == 0)
                    564:                        value = PERMIT_YES;
                    565:                else if (strcmp(arg, "no") == 0)
                    566:                        value = PERMIT_NO;
                    567:                else
                    568:                        fatal("%s line %d: Bad yes/"
                    569:                            "without-password/forced-commands-only/no "
                    570:                            "argument: %s", filename, linenum, arg);
                    571:                if (*intptr == -1)
                    572:                        *intptr = value;
                    573:                break;
1.36      markus    574:
1.94      markus    575:        case sIgnoreRhosts:
                    576:                intptr = &options->ignore_rhosts;
1.25      markus    577: parse_flag:
1.94      markus    578:                arg = strdelim(&cp);
                    579:                if (!arg || *arg == '\0')
                    580:                        fatal("%s line %d: missing yes/no argument.",
                    581:                            filename, linenum);
                    582:                value = 0;      /* silence compiler */
                    583:                if (strcmp(arg, "yes") == 0)
                    584:                        value = 1;
                    585:                else if (strcmp(arg, "no") == 0)
                    586:                        value = 0;
                    587:                else
                    588:                        fatal("%s line %d: Bad yes/no argument: %s",
                    589:                                filename, linenum, arg);
                    590:                if (*intptr == -1)
                    591:                        *intptr = value;
                    592:                break;
                    593:
                    594:        case sIgnoreUserKnownHosts:
                    595:                intptr = &options->ignore_user_known_hosts;
                    596:                goto parse_flag;
                    597:
                    598:        case sRhostsRSAAuthentication:
                    599:                intptr = &options->rhosts_rsa_authentication;
                    600:                goto parse_flag;
                    601:
                    602:        case sHostbasedAuthentication:
                    603:                intptr = &options->hostbased_authentication;
                    604:                goto parse_flag;
                    605:
                    606:        case sHostbasedUsesNameFromPacketOnly:
                    607:                intptr = &options->hostbased_uses_name_from_packet_only;
                    608:                goto parse_flag;
                    609:
                    610:        case sRSAAuthentication:
                    611:                intptr = &options->rsa_authentication;
                    612:                goto parse_flag;
                    613:
                    614:        case sPubkeyAuthentication:
                    615:                intptr = &options->pubkey_authentication;
                    616:                goto parse_flag;
1.119     jakob     617:
1.94      markus    618:        case sKerberosAuthentication:
                    619:                intptr = &options->kerberos_authentication;
                    620:                goto parse_flag;
                    621:
                    622:        case sKerberosOrLocalPasswd:
                    623:                intptr = &options->kerberos_or_local_passwd;
                    624:                goto parse_flag;
                    625:
                    626:        case sKerberosTicketCleanup:
                    627:                intptr = &options->kerberos_ticket_cleanup;
1.130     jakob     628:                goto parse_flag;
                    629:
                    630:        case sKerberosGetAFSToken:
                    631:                intptr = &options->kerberos_get_afs_token;
1.125     markus    632:                goto parse_flag;
                    633:
                    634:        case sGssAuthentication:
                    635:                intptr = &options->gss_authentication;
                    636:                goto parse_flag;
                    637:
                    638:        case sGssCleanupCreds:
                    639:                intptr = &options->gss_cleanup_creds;
1.94      markus    640:                goto parse_flag;
                    641:
                    642:        case sPasswordAuthentication:
                    643:                intptr = &options->password_authentication;
                    644:                goto parse_flag;
                    645:
                    646:        case sKbdInteractiveAuthentication:
                    647:                intptr = &options->kbd_interactive_authentication;
                    648:                goto parse_flag;
                    649:
                    650:        case sChallengeResponseAuthentication:
                    651:                intptr = &options->challenge_response_authentication;
                    652:                goto parse_flag;
                    653:
                    654:        case sPrintMotd:
                    655:                intptr = &options->print_motd;
                    656:                goto parse_flag;
                    657:
                    658:        case sPrintLastLog:
                    659:                intptr = &options->print_lastlog;
                    660:                goto parse_flag;
                    661:
                    662:        case sX11Forwarding:
                    663:                intptr = &options->x11_forwarding;
                    664:                goto parse_flag;
                    665:
                    666:        case sX11DisplayOffset:
                    667:                intptr = &options->x11_display_offset;
                    668:                goto parse_int;
1.99      stevesk   669:
                    670:        case sX11UseLocalhost:
                    671:                intptr = &options->x11_use_localhost;
                    672:                goto parse_flag;
1.94      markus    673:
                    674:        case sXAuthLocation:
                    675:                charptr = &options->xauth_location;
                    676:                goto parse_filename;
                    677:
                    678:        case sStrictModes:
                    679:                intptr = &options->strict_modes;
                    680:                goto parse_flag;
                    681:
1.129     markus    682:        case sTCPKeepAlive:
                    683:                intptr = &options->tcp_keep_alive;
1.94      markus    684:                goto parse_flag;
                    685:
                    686:        case sEmptyPasswd:
                    687:                intptr = &options->permit_empty_passwd;
1.113     markus    688:                goto parse_flag;
                    689:
                    690:        case sPermitUserEnvironment:
                    691:                intptr = &options->permit_user_env;
1.94      markus    692:                goto parse_flag;
                    693:
                    694:        case sUseLogin:
                    695:                intptr = &options->use_login;
1.111     markus    696:                goto parse_flag;
                    697:
                    698:        case sCompression:
                    699:                intptr = &options->compression;
1.94      markus    700:                goto parse_flag;
                    701:
                    702:        case sGatewayPorts:
                    703:                intptr = &options->gateway_ports;
                    704:                goto parse_flag;
1.25      markus    705:
1.122     markus    706:        case sUseDNS:
                    707:                intptr = &options->use_dns;
1.94      markus    708:                goto parse_flag;
1.53      markus    709:
1.94      markus    710:        case sLogFacility:
                    711:                intptr = (int *) &options->log_facility;
                    712:                arg = strdelim(&cp);
                    713:                value = log_facility_number(arg);
1.101     markus    714:                if (value == SYSLOG_FACILITY_NOT_SET)
1.94      markus    715:                        fatal("%.200s line %d: unsupported log facility '%s'",
                    716:                            filename, linenum, arg ? arg : "<NONE>");
                    717:                if (*intptr == -1)
                    718:                        *intptr = (SyslogFacility) value;
                    719:                break;
1.25      markus    720:
1.94      markus    721:        case sLogLevel:
                    722:                intptr = (int *) &options->log_level;
                    723:                arg = strdelim(&cp);
                    724:                value = log_level_number(arg);
1.101     markus    725:                if (value == SYSLOG_LEVEL_NOT_SET)
1.94      markus    726:                        fatal("%.200s line %d: unsupported log level '%s'",
                    727:                            filename, linenum, arg ? arg : "<NONE>");
                    728:                if (*intptr == -1)
                    729:                        *intptr = (LogLevel) value;
                    730:                break;
                    731:
                    732:        case sAllowTcpForwarding:
                    733:                intptr = &options->allow_tcp_forwarding;
                    734:                goto parse_flag;
1.102     provos    735:
                    736:        case sUsePrivilegeSeparation:
                    737:                intptr = &use_privsep;
                    738:                goto parse_flag;
1.94      markus    739:
                    740:        case sAllowUsers:
                    741:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    742:                        if (options->num_allow_users >= MAX_ALLOW_USERS)
                    743:                                fatal("%s line %d: too many allow users.",
                    744:                                    filename, linenum);
1.112     deraadt   745:                        options->allow_users[options->num_allow_users++] =
                    746:                            xstrdup(arg);
1.94      markus    747:                }
                    748:                break;
1.25      markus    749:
1.94      markus    750:        case sDenyUsers:
                    751:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    752:                        if (options->num_deny_users >= MAX_DENY_USERS)
                    753:                                fatal( "%s line %d: too many deny users.",
                    754:                                    filename, linenum);
1.112     deraadt   755:                        options->deny_users[options->num_deny_users++] =
                    756:                            xstrdup(arg);
1.94      markus    757:                }
                    758:                break;
1.25      markus    759:
1.94      markus    760:        case sAllowGroups:
                    761:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    762:                        if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
                    763:                                fatal("%s line %d: too many allow groups.",
                    764:                                    filename, linenum);
1.112     deraadt   765:                        options->allow_groups[options->num_allow_groups++] =
                    766:                            xstrdup(arg);
1.94      markus    767:                }
                    768:                break;
1.33      markus    769:
1.94      markus    770:        case sDenyGroups:
                    771:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    772:                        if (options->num_deny_groups >= MAX_DENY_GROUPS)
                    773:                                fatal("%s line %d: too many deny groups.",
                    774:                                    filename, linenum);
                    775:                        options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
                    776:                }
                    777:                break;
1.66      markus    778:
1.94      markus    779:        case sCiphers:
                    780:                arg = strdelim(&cp);
                    781:                if (!arg || *arg == '\0')
                    782:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    783:                if (!ciphers_valid(arg))
                    784:                        fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
                    785:                            filename, linenum, arg ? arg : "<NONE>");
                    786:                if (options->ciphers == NULL)
                    787:                        options->ciphers = xstrdup(arg);
                    788:                break;
1.33      markus    789:
1.94      markus    790:        case sMacs:
                    791:                arg = strdelim(&cp);
                    792:                if (!arg || *arg == '\0')
                    793:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    794:                if (!mac_valid(arg))
                    795:                        fatal("%s line %d: Bad SSH2 mac spec '%s'.",
                    796:                            filename, linenum, arg ? arg : "<NONE>");
                    797:                if (options->macs == NULL)
                    798:                        options->macs = xstrdup(arg);
                    799:                break;
1.43      jakob     800:
1.94      markus    801:        case sProtocol:
                    802:                intptr = &options->protocol;
                    803:                arg = strdelim(&cp);
                    804:                if (!arg || *arg == '\0')
                    805:                        fatal("%s line %d: Missing argument.", filename, linenum);
                    806:                value = proto_spec(arg);
                    807:                if (value == SSH_PROTO_UNKNOWN)
                    808:                        fatal("%s line %d: Bad protocol spec '%s'.",
1.95      deraadt   809:                            filename, linenum, arg ? arg : "<NONE>");
1.94      markus    810:                if (*intptr == SSH_PROTO_UNKNOWN)
                    811:                        *intptr = value;
                    812:                break;
                    813:
                    814:        case sSubsystem:
                    815:                if (options->num_subsystems >= MAX_SUBSYSTEMS) {
                    816:                        fatal("%s line %d: too many subsystems defined.",
1.95      deraadt   817:                            filename, linenum);
1.94      markus    818:                }
                    819:                arg = strdelim(&cp);
                    820:                if (!arg || *arg == '\0')
                    821:                        fatal("%s line %d: Missing subsystem name.",
1.95      deraadt   822:                            filename, linenum);
1.94      markus    823:                for (i = 0; i < options->num_subsystems; i++)
                    824:                        if (strcmp(arg, options->subsystem_name[i]) == 0)
                    825:                                fatal("%s line %d: Subsystem '%s' already defined.",
1.95      deraadt   826:                                    filename, linenum, arg);
1.94      markus    827:                options->subsystem_name[options->num_subsystems] = xstrdup(arg);
                    828:                arg = strdelim(&cp);
                    829:                if (!arg || *arg == '\0')
                    830:                        fatal("%s line %d: Missing subsystem command.",
1.95      deraadt   831:                            filename, linenum);
1.94      markus    832:                options->subsystem_command[options->num_subsystems] = xstrdup(arg);
                    833:                options->num_subsystems++;
                    834:                break;
1.46      markus    835:
1.94      markus    836:        case sMaxStartups:
                    837:                arg = strdelim(&cp);
                    838:                if (!arg || *arg == '\0')
                    839:                        fatal("%s line %d: Missing MaxStartups spec.",
1.95      deraadt   840:                            filename, linenum);
1.94      markus    841:                if ((n = sscanf(arg, "%d:%d:%d",
                    842:                    &options->max_startups_begin,
                    843:                    &options->max_startups_rate,
                    844:                    &options->max_startups)) == 3) {
                    845:                        if (options->max_startups_begin >
                    846:                            options->max_startups ||
                    847:                            options->max_startups_rate > 100 ||
                    848:                            options->max_startups_rate < 1)
1.50      markus    849:                                fatal("%s line %d: Illegal MaxStartups spec.",
1.87      stevesk   850:                                    filename, linenum);
1.94      markus    851:                } else if (n != 1)
                    852:                        fatal("%s line %d: Illegal MaxStartups spec.",
                    853:                            filename, linenum);
                    854:                else
                    855:                        options->max_startups = options->max_startups_begin;
                    856:                break;
1.133     dtucker   857:
                    858:        case sMaxAuthTries:
                    859:                intptr = &options->max_authtries;
                    860:                goto parse_int;
1.94      markus    861:
                    862:        case sBanner:
                    863:                charptr = &options->banner;
                    864:                goto parse_filename;
                    865:        /*
                    866:         * These options can contain %X options expanded at
                    867:         * connect time, so that you can specify paths like:
                    868:         *
                    869:         * AuthorizedKeysFile   /etc/ssh_keys/%u
                    870:         */
                    871:        case sAuthorizedKeysFile:
                    872:        case sAuthorizedKeysFile2:
                    873:                charptr = (opcode == sAuthorizedKeysFile ) ?
                    874:                    &options->authorized_keys_file :
                    875:                    &options->authorized_keys_file2;
                    876:                goto parse_filename;
                    877:
                    878:        case sClientAliveInterval:
                    879:                intptr = &options->client_alive_interval;
                    880:                goto parse_time;
                    881:
                    882:        case sClientAliveCountMax:
                    883:                intptr = &options->client_alive_count_max;
                    884:                goto parse_int;
1.131     djm       885:
                    886:        case sAcceptEnv:
                    887:                while ((arg = strdelim(&cp)) && *arg != '\0') {
                    888:                        if (strchr(arg, '=') != NULL)
                    889:                                fatal("%s line %d: Invalid environment name.",
                    890:                                    filename, linenum);
                    891:                        if (options->num_accept_env >= MAX_ACCEPT_ENV)
                    892:                                fatal("%s line %d: too many allow env.",
                    893:                                    filename, linenum);
                    894:                        options->accept_env[options->num_accept_env++] =
                    895:                            xstrdup(arg);
                    896:                }
                    897:                break;
1.94      markus    898:
                    899:        case sDeprecated:
1.117     itojun    900:                logit("%s line %d: Deprecated option %s",
1.121     jakob     901:                    filename, linenum, arg);
                    902:                while (arg)
                    903:                    arg = strdelim(&cp);
                    904:                break;
                    905:
                    906:        case sUnsupported:
                    907:                logit("%s line %d: Unsupported option %s",
1.94      markus    908:                    filename, linenum, arg);
                    909:                while (arg)
                    910:                    arg = strdelim(&cp);
                    911:                break;
                    912:
                    913:        default:
                    914:                fatal("%s line %d: Missing handler for opcode %s (%d)",
                    915:                    filename, linenum, arg, opcode);
                    916:        }
                    917:        if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
                    918:                fatal("%s line %d: garbage at end of line; \"%.200s\".",
                    919:                    filename, linenum, arg);
                    920:        return 0;
                    921: }
                    922:
                    923: /* Reads the server configuration file. */
1.25      markus    924:
1.94      markus    925: void
1.134     djm       926: load_server_config(const char *filename, Buffer *conf)
1.94      markus    927: {
1.134     djm       928:        char line[1024], *cp;
1.94      markus    929:        FILE *f;
1.81      stevesk   930:
1.134     djm       931:        debug2("%s: filename %s", __func__, filename);
                    932:        if ((f = fopen(filename, "r")) == NULL) {
1.94      markus    933:                perror(filename);
                    934:                exit(1);
                    935:        }
1.134     djm       936:        buffer_clear(conf);
                    937:        while (fgets(line, sizeof(line), f)) {
                    938:                /*
                    939:                 * Trim out comments and strip whitespace
1.135     deraadt   940:                 * NB - preserve newlines, they are needed to reproduce
1.134     djm       941:                 * line numbers later for error messages
                    942:                 */
                    943:                if ((cp = strchr(line, '#')) != NULL)
                    944:                        memcpy(cp, "\n", 2);
                    945:                cp = line + strspn(line, " \t\r");
                    946:
                    947:                buffer_append(conf, cp, strlen(cp));
                    948:        }
                    949:        buffer_append(conf, "\0", 1);
                    950:        fclose(f);
                    951:        debug2("%s: done config len = %d", __func__, buffer_len(conf));
                    952: }
                    953:
                    954: void
                    955: parse_server_config(ServerOptions *options, const char *filename, Buffer *conf)
                    956: {
                    957:        int linenum, bad_options = 0;
1.136     dtucker   958:        char *cp, *obuf, *cbuf;
1.134     djm       959:
                    960:        debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
                    961:
1.136     dtucker   962:        obuf = cbuf = xstrdup(buffer_ptr(conf));
1.137     dtucker   963:        linenum = 1;
1.134     djm       964:        while((cp = strsep(&cbuf, "\n")) != NULL) {
                    965:                if (process_server_config_line(options, cp, filename,
                    966:                    linenum++) != 0)
1.94      markus    967:                        bad_options++;
1.1       deraadt   968:        }
1.136     dtucker   969:        xfree(obuf);
1.78      stevesk   970:        if (bad_options > 0)
                    971:                fatal("%s: terminating, %d bad configuration options",
                    972:                    filename, bad_options);
1.1       deraadt   973: }