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

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