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

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