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

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