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

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