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

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