blob: 20e3f1a861662d093cd454d2c9f55eeb0570367f [file] [log] [blame]
Damien Millerbe43ebf2006-07-24 13:51:51 +10001/* $OpenBSD: servconf.c,v 1.154 2006/07/12 22:28:52 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * 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".
Damien Miller95def091999-11-25 00:26:21 +110011 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
Damien Millere3b60b52006-07-10 21:08:03 +100015#include <sys/types.h>
16#include <sys/socket.h>
17
Damien Millerbe43ebf2006-07-24 13:51:51 +100018#if defined(HAVE_NETDB_H)
19# include <netdb.h>
20#endif
21
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "servconf.h"
25#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100026#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "misc.h"
29#include "cipher.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000030#include "kex.h"
31#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100032#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033
Ben Lindstrombba81212001-06-25 05:01:22 +000034static void add_listen_addr(ServerOptions *, char *, u_short);
35static void add_one_listen_addr(ServerOptions *, char *, u_short);
Damien Miller34132e52000-01-14 15:45:46 +110036
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000037/* Use of privilege separation or not */
38extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100039extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041/* Initializes the server options to their default values. */
42
Damien Miller4af51302000-04-16 11:18:38 +100043void
Damien Miller95def091999-11-25 00:26:21 +110044initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045{
Damien Miller95def091999-11-25 00:26:21 +110046 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110047
48 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100049 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110050
51 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110052 options->num_ports = 0;
53 options->ports_from_cmdline = 0;
54 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110055 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110056 options->num_host_key_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100057 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110058 options->server_key_bits = -1;
59 options->login_grace_time = -1;
60 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000061 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110062 options->ignore_rhosts = -1;
63 options->ignore_user_known_hosts = -1;
64 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000065 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110066 options->x11_forwarding = -1;
67 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110068 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100069 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110070 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110071 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110072 options->log_facility = SYSLOG_FACILITY_NOT_SET;
73 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110074 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000075 options->hostbased_authentication = -1;
76 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110077 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110078 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110079 options->kerberos_authentication = -1;
80 options->kerberos_or_local_passwd = -1;
81 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +110082 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +100083 options->gss_authentication=-1;
84 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +110085 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +110086 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +000087 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110088 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +000089 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +110090 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +000091 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +110092 options->allow_tcp_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +110093 options->num_allow_users = 0;
94 options->num_deny_users = 0;
95 options->num_allow_groups = 0;
96 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +100097 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000098 options->macs = NULL;
Damien Miller78928792000-04-12 20:17:38 +100099 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000100 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000101 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000102 options->max_startups_begin = -1;
103 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000104 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000105 options->max_authtries = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000106 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000107 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000108 options->client_alive_interval = -1;
109 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000110 options->authorized_keys_file = NULL;
111 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000112 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100113 options->permit_tun = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114}
115
Damien Miller4af51302000-04-16 11:18:38 +1000116void
Damien Miller95def091999-11-25 00:26:21 +1100117fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118{
Damien Miller726273e2001-11-12 11:40:11 +1100119 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000120 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000121 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100122
123 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100124 if (options->protocol == SSH_PROTO_UNKNOWN)
125 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
126 if (options->num_host_key_files == 0) {
127 /* fill default hostkeys for protocols */
128 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100129 options->host_key_files[options->num_host_key_files++] =
130 _PATH_HOST_KEY_FILE;
131 if (options->protocol & SSH_PROTO_2) {
132 options->host_key_files[options->num_host_key_files++] =
133 _PATH_HOST_RSA_KEY_FILE;
134 options->host_key_files[options->num_host_key_files++] =
135 _PATH_HOST_DSA_KEY_FILE;
136 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 }
Damien Miller34132e52000-01-14 15:45:46 +1100138 if (options->num_ports == 0)
139 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
140 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000141 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000142 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000143 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100144 if (options->server_key_bits == -1)
145 options->server_key_bits = 768;
146 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000147 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100148 if (options->key_regeneration_time == -1)
149 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000150 if (options->permit_root_login == PERMIT_NOT_SET)
151 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100152 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100153 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100154 if (options->ignore_user_known_hosts == -1)
155 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100156 if (options->print_motd == -1)
157 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000158 if (options->print_lastlog == -1)
159 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100160 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100161 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100162 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100163 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100164 if (options->x11_use_localhost == -1)
165 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000166 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000167 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100168 if (options->strict_modes == -1)
169 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100170 if (options->tcp_keep_alive == -1)
171 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100172 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100173 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100174 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000175 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100176 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100177 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000178 if (options->hostbased_authentication == -1)
179 options->hostbased_authentication = 0;
180 if (options->hostbased_uses_name_from_packet_only == -1)
181 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100182 if (options->rsa_authentication == -1)
183 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100184 if (options->pubkey_authentication == -1)
185 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100186 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000187 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100188 if (options->kerberos_or_local_passwd == -1)
189 options->kerberos_or_local_passwd = 1;
190 if (options->kerberos_ticket_cleanup == -1)
191 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100192 if (options->kerberos_get_afs_token == -1)
193 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000194 if (options->gss_authentication == -1)
195 options->gss_authentication = 0;
196 if (options->gss_cleanup_creds == -1)
197 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100198 if (options->password_authentication == -1)
199 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100200 if (options->kbd_interactive_authentication == -1)
201 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000202 if (options->challenge_response_authentication == -1)
203 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100204 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100205 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000206 if (options->permit_user_env == -1)
207 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100208 if (options->use_login == -1)
209 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000210 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000211 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100212 if (options->allow_tcp_forwarding == -1)
213 options->allow_tcp_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000214 if (options->gateway_ports == -1)
215 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000216 if (options->max_startups == -1)
217 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000218 if (options->max_startups_rate == -1)
219 options->max_startups_rate = 100; /* 100% */
220 if (options->max_startups_begin == -1)
221 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000222 if (options->max_authtries == -1)
223 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000224 if (options->use_dns == -1)
225 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000226 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100227 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000228 if (options->client_alive_count_max == -1)
229 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100230 if (options->authorized_keys_file2 == NULL) {
231 /* authorized_keys_file2 falls back to authorized_keys_file */
232 if (options->authorized_keys_file != NULL)
233 options->authorized_keys_file2 = options->authorized_keys_file;
234 else
235 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
236 }
237 if (options->authorized_keys_file == NULL)
238 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
Damien Millerd27b9472005-12-13 19:29:02 +1100239 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100240 options->permit_tun = SSH_TUNMODE_NO;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241
Ben Lindstromfb62a692002-06-06 19:47:11 +0000242 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000244 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000245
Tim Rice40017b02002-07-14 13:36:49 -0700246#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000247 if (use_privsep && options->compression == 1) {
248 error("This platform does not support both privilege "
249 "separation and compression");
250 error("Compression disabled");
251 options->compression = 0;
252 }
253#endif
254
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255}
256
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100258typedef enum {
259 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100260 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000261 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100262 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100263 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
264 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000265 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100266 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100267 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000268 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100269 sPasswordAuthentication, sKbdInteractiveAuthentication,
270 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000271 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100272 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100273 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000274 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100275 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000276 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000277 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
278 sMaxStartups, sMaxAuthTries,
Damien Miller3a961dc2003-06-03 10:25:48 +1000279 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100280 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000281 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100282 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Darren Tucker45150472006-07-12 22:34:17 +1000283 sMatch,
Ben Lindstromc7431342002-03-22 03:11:49 +0000284 sUsePrivilegeSeparation,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000285 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286} ServerOpCodes;
287
Darren Tucker45150472006-07-12 22:34:17 +1000288#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
289#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
290#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
291
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100293static struct {
294 const char *name;
295 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000296 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100297} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100298 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000299#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000300 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000301#else
Darren Tucker45150472006-07-12 22:34:17 +1000302 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000303#endif
Darren Tucker45150472006-07-12 22:34:17 +1000304 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100305 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000306 { "port", sPort, SSHCFG_GLOBAL },
307 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
308 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
309 { "pidfile", sPidFile, SSHCFG_GLOBAL },
310 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
311 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
312 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
313 { "permitrootlogin", sPermitRootLogin, SSHCFG_GLOBAL },
314 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
315 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
316 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
317 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_GLOBAL },
318 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_GLOBAL },
319 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
320 { "rsaauthentication", sRSAAuthentication, SSHCFG_GLOBAL },
321 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL },
322 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000323#ifdef KRB5
Darren Tucker45150472006-07-12 22:34:17 +1000324 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_GLOBAL },
325 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
326 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100327#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000328 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000329#else
Darren Tucker45150472006-07-12 22:34:17 +1000330 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100331#endif
332#else
Darren Tucker45150472006-07-12 22:34:17 +1000333 { "kerberosauthentication", sUnsupported, SSHCFG_GLOBAL },
334 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
335 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
336 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000337#endif
Darren Tucker45150472006-07-12 22:34:17 +1000338 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
339 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000340#ifdef GSSAPI
Darren Tucker45150472006-07-12 22:34:17 +1000341 { "gssapiauthentication", sGssAuthentication, SSHCFG_GLOBAL },
342 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000343#else
Darren Tucker45150472006-07-12 22:34:17 +1000344 { "gssapiauthentication", sUnsupported, SSHCFG_GLOBAL },
345 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000346#endif
Darren Tucker45150472006-07-12 22:34:17 +1000347 { "passwordauthentication", sPasswordAuthentication, SSHCFG_GLOBAL },
348 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_GLOBAL },
349 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
350 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
351 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
352 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
353 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
354 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
355 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
356 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
357 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
358 { "x11forwarding", sX11Forwarding, SSHCFG_GLOBAL },
359 { "x11displayoffset", sX11DisplayOffset, SSHCFG_GLOBAL },
360 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_GLOBAL },
361 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
362 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
363 { "permitemptypasswords", sEmptyPasswd, SSHCFG_GLOBAL },
364 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
365 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
366 { "compression", sCompression, SSHCFG_GLOBAL },
367 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
368 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
369 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
370 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
371 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
372 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
373 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
374 { "ciphers", sCiphers, SSHCFG_GLOBAL },
375 { "macs", sMacs, SSHCFG_GLOBAL },
376 { "protocol", sProtocol, SSHCFG_GLOBAL },
377 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
378 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
379 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
380 { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
381 { "banner", sBanner, SSHCFG_GLOBAL },
382 { "usedns", sUseDNS, SSHCFG_GLOBAL },
383 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
384 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
385 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
386 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
387 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
388 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
389 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL },
390 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
391 { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
392 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393};
394
Damien Miller5428f641999-11-25 11:54:57 +1100395/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000396 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100397 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398
Damien Miller4af51302000-04-16 11:18:38 +1000399static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100400parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000401 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000403 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Damien Miller95def091999-11-25 00:26:21 +1100405 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000406 if (strcasecmp(cp, keywords[i].name) == 0) {
407 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100408 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000409 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000411 error("%s: line %d: Bad configuration option: %s",
412 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100413 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414}
415
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
Ben Lindstrom19066a12001-04-12 23:39:26 +0000417add_listen_addr(ServerOptions *options, char *addr, u_short port)
Damien Miller34132e52000-01-14 15:45:46 +1100418{
Damien Millereccb9de2005-06-17 12:59:34 +1000419 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100420
421 if (options->num_ports == 0)
422 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100423 if (options->address_family == -1)
424 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000425 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000426 for (i = 0; i < options->num_ports; i++)
427 add_one_listen_addr(options, addr, options->ports[i]);
428 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000429 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000430}
431
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static void
Ben Lindstromc510af42001-04-07 17:25:48 +0000433add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
434{
435 struct addrinfo hints, *ai, *aitop;
436 char strport[NI_MAXSERV];
437 int gaierr;
438
439 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100440 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000441 hints.ai_socktype = SOCK_STREAM;
442 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Ben Lindstrome1353632002-06-23 21:29:23 +0000443 snprintf(strport, sizeof strport, "%u", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000444 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
445 fatal("bad addr or host: %s (%s)",
446 addr ? addr : "<NULL>",
447 gai_strerror(gaierr));
448 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
449 ;
450 ai->ai_next = options->listen_addrs;
451 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100452}
453
Darren Tucker45150472006-07-12 22:34:17 +1000454/*
455 * The strategy for the Match blocks is that the config file is parsed twice.
456 *
457 * The first time is at startup. activep is initialized to 1 and the
458 * directives in the global context are processed and acted on. Hitting a
459 * Match directive unsets activep and the directives inside the block are
460 * checked for syntax only.
461 *
462 * The second time is after a connection has been established but before
463 * authentication. activep is initialized to 2 and global config directives
464 * are ignored since they have already been processed. If the criteria in a
465 * Match block is met, activep is set and the subsequent directives
466 * processed and actioned until EOF or another Match block unsets it. Any
467 * options set are copied into the main server config.
468 *
469 * Potential additions/improvements:
470 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
471 *
472 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
473 * Match Address 192.168.0.*
474 * Tag trusted
475 * Match Group wheel
476 * Tag trusted
477 * Match Tag trusted
478 * AllowTcpForwarding yes
479 * GatewayPorts clientspecified
480 * [...]
481 *
482 * - Add a PermittedChannelRequests directive
483 * Match Group shell
484 * PermittedChannelRequests session,forwarded-tcpip
485 */
486
487static int
488match_cfg_line(char **condition, int line, const char *user, const char *host,
489 const char *address)
490{
491 int result = 1;
492 char *arg, *attrib, *cp = *condition;
493 size_t len;
494
495 if (user == NULL)
496 debug3("checking syntax for 'Match %s'", cp);
497 else
498 debug3("checking match for '%s' user %s host %s addr %s", cp,
499 user ? user : "(null)", host ? host : "(null)",
500 address ? address : "(null)");
501
502 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
503 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
504 error("Missing Match criteria for %s", attrib);
505 return -1;
506 }
507 len = strlen(arg);
508 if (strcasecmp(attrib, "user") == 0) {
509 if (!user) {
510 result = 0;
511 continue;
512 }
513 if (match_pattern_list(user, arg, len, 0) != 1)
514 result = 0;
515 else
516 debug("user %.100s matched 'User %.100s' at "
517 "line %d", user, arg, line);
518 } else if (strcasecmp(attrib, "host") == 0) {
519 if (!host) {
520 result = 0;
521 continue;
522 }
523 if (match_hostname(host, arg, len) != 1)
524 result = 0;
525 else
526 debug("connection from %.100s matched 'Host "
527 "%.100s' at line %d", host, arg, line);
528 } else if (strcasecmp(attrib, "address") == 0) {
529 debug("address '%s' arg '%s'", address, arg);
530 if (!address) {
531 result = 0;
532 continue;
533 }
534 if (match_hostname(address, arg, len) != 1)
535 result = 0;
536 else
537 debug("connection from %.100s matched 'Address "
538 "%.100s' at line %d", address, arg, line);
539 } else {
540 error("Unsupported Match attribute %s", attrib);
541 return -1;
542 }
543 }
544 if (user != NULL)
545 debug3("match %sfound", result ? "" : "not ");
546 *condition = cp;
547 return result;
548}
549
Ben Lindstromade03f62001-12-06 18:22:17 +0000550int
551process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000552 const char *filename, int linenum, int *activep, const char *user,
553 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000554{
555 char *cp, **charptr, *arg, *p;
Darren Tucker45150472006-07-12 22:34:17 +1000556 int cmdline = 0, *intptr, value, n;
Ben Lindstromade03f62001-12-06 18:22:17 +0000557 ServerOpCodes opcode;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100558 u_short port;
Darren Tucker45150472006-07-12 22:34:17 +1000559 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000560 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000561
562 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100563 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100564 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000565 /* Ignore leading whitespace */
566 if (*arg == '\0')
567 arg = strdelim(&cp);
568 if (!arg || !*arg || *arg == '#')
569 return 0;
570 intptr = NULL;
571 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000572 opcode = parse_token(arg, filename, linenum, &flags);
573
574 if (activep == NULL) { /* We are processing a command line directive */
575 cmdline = 1;
576 activep = &cmdline;
577 }
578 if (*activep && opcode != sMatch)
579 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
580 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
581 if (user == NULL) {
582 fatal("%s line %d: Directive '%s' is not allowed "
583 "within a Match block", filename, linenum, arg);
584 } else { /* this is a directive we have already processed */
585 while (arg)
586 arg = strdelim(&cp);
587 return 0;
588 }
589 }
590
Ben Lindstromade03f62001-12-06 18:22:17 +0000591 switch (opcode) {
592 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000593 case sUsePAM:
594 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000595 goto parse_flag;
596
597 /* Standard Options */
598 case sBadOption:
599 return -1;
600 case sPort:
601 /* ignore ports from configfile if cmdline specifies ports */
602 if (options->ports_from_cmdline)
603 return 0;
604 if (options->listen_addrs != NULL)
605 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100606 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000607 if (options->num_ports >= MAX_PORTS)
608 fatal("%s line %d: too many ports.",
609 filename, linenum);
610 arg = strdelim(&cp);
611 if (!arg || *arg == '\0')
612 fatal("%s line %d: missing port number.",
613 filename, linenum);
614 options->ports[options->num_ports++] = a2port(arg);
615 if (options->ports[options->num_ports-1] == 0)
616 fatal("%s line %d: Badly formatted port number.",
617 filename, linenum);
618 break;
619
620 case sServerKeyBits:
621 intptr = &options->server_key_bits;
622parse_int:
623 arg = strdelim(&cp);
624 if (!arg || *arg == '\0')
625 fatal("%s line %d: missing integer value.",
626 filename, linenum);
627 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000628 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000629 *intptr = value;
630 break;
631
632 case sLoginGraceTime:
633 intptr = &options->login_grace_time;
634parse_time:
635 arg = strdelim(&cp);
636 if (!arg || *arg == '\0')
637 fatal("%s line %d: missing time value.",
638 filename, linenum);
639 if ((value = convtime(arg)) == -1)
640 fatal("%s line %d: invalid time value.",
641 filename, linenum);
642 if (*intptr == -1)
643 *intptr = value;
644 break;
645
646 case sKeyRegenerationTime:
647 intptr = &options->key_regeneration_time;
648 goto parse_time;
649
650 case sListenAddress:
651 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100652 if (arg == NULL || *arg == '\0')
653 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000654 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000655 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
656 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
657 && strchr(p+1, ':') != NULL) {
658 add_listen_addr(options, arg, 0);
659 break;
660 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100661 p = hpdelim(&arg);
662 if (p == NULL)
663 fatal("%s line %d: bad address:port usage",
664 filename, linenum);
665 p = cleanhostname(p);
666 if (arg == NULL)
667 port = 0;
668 else if ((port = a2port(arg)) == 0)
669 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000670
Damien Millerf91ee4c2005-03-01 21:24:33 +1100671 add_listen_addr(options, p, port);
672
Ben Lindstromade03f62001-12-06 18:22:17 +0000673 break;
674
Darren Tucker0f383232005-01-20 10:57:56 +1100675 case sAddressFamily:
676 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000677 if (!arg || *arg == '\0')
678 fatal("%s line %d: missing address family.",
679 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100680 intptr = &options->address_family;
681 if (options->listen_addrs != NULL)
682 fatal("%s line %d: address family must be specified before "
683 "ListenAddress.", filename, linenum);
684 if (strcasecmp(arg, "inet") == 0)
685 value = AF_INET;
686 else if (strcasecmp(arg, "inet6") == 0)
687 value = AF_INET6;
688 else if (strcasecmp(arg, "any") == 0)
689 value = AF_UNSPEC;
690 else
691 fatal("%s line %d: unsupported address family \"%s\".",
692 filename, linenum, arg);
693 if (*intptr == -1)
694 *intptr = value;
695 break;
696
Ben Lindstromade03f62001-12-06 18:22:17 +0000697 case sHostKeyFile:
698 intptr = &options->num_host_key_files;
699 if (*intptr >= MAX_HOSTKEYS)
700 fatal("%s line %d: too many host keys specified (max %d).",
701 filename, linenum, MAX_HOSTKEYS);
702 charptr = &options->host_key_files[*intptr];
703parse_filename:
704 arg = strdelim(&cp);
705 if (!arg || *arg == '\0')
706 fatal("%s line %d: missing file name.",
707 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000708 if (*activep && *charptr == NULL) {
Ben Lindstromade03f62001-12-06 18:22:17 +0000709 *charptr = tilde_expand_filename(arg, getuid());
710 /* increase optional counter */
711 if (intptr != NULL)
712 *intptr = *intptr + 1;
713 }
714 break;
715
716 case sPidFile:
717 charptr = &options->pid_file;
718 goto parse_filename;
719
720 case sPermitRootLogin:
721 intptr = &options->permit_root_login;
722 arg = strdelim(&cp);
723 if (!arg || *arg == '\0')
724 fatal("%s line %d: missing yes/"
725 "without-password/forced-commands-only/no "
726 "argument.", filename, linenum);
727 value = 0; /* silence compiler */
728 if (strcmp(arg, "without-password") == 0)
729 value = PERMIT_NO_PASSWD;
730 else if (strcmp(arg, "forced-commands-only") == 0)
731 value = PERMIT_FORCED_ONLY;
732 else if (strcmp(arg, "yes") == 0)
733 value = PERMIT_YES;
734 else if (strcmp(arg, "no") == 0)
735 value = PERMIT_NO;
736 else
737 fatal("%s line %d: Bad yes/"
738 "without-password/forced-commands-only/no "
739 "argument: %s", filename, linenum, arg);
740 if (*intptr == -1)
741 *intptr = value;
742 break;
743
744 case sIgnoreRhosts:
745 intptr = &options->ignore_rhosts;
746parse_flag:
747 arg = strdelim(&cp);
748 if (!arg || *arg == '\0')
749 fatal("%s line %d: missing yes/no argument.",
750 filename, linenum);
751 value = 0; /* silence compiler */
752 if (strcmp(arg, "yes") == 0)
753 value = 1;
754 else if (strcmp(arg, "no") == 0)
755 value = 0;
756 else
757 fatal("%s line %d: Bad yes/no argument: %s",
758 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000759 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000760 *intptr = value;
761 break;
762
763 case sIgnoreUserKnownHosts:
764 intptr = &options->ignore_user_known_hosts;
765 goto parse_flag;
766
Ben Lindstromade03f62001-12-06 18:22:17 +0000767 case sRhostsRSAAuthentication:
768 intptr = &options->rhosts_rsa_authentication;
769 goto parse_flag;
770
771 case sHostbasedAuthentication:
772 intptr = &options->hostbased_authentication;
773 goto parse_flag;
774
775 case sHostbasedUsesNameFromPacketOnly:
776 intptr = &options->hostbased_uses_name_from_packet_only;
777 goto parse_flag;
778
779 case sRSAAuthentication:
780 intptr = &options->rsa_authentication;
781 goto parse_flag;
782
783 case sPubkeyAuthentication:
784 intptr = &options->pubkey_authentication;
785 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000786
Ben Lindstromade03f62001-12-06 18:22:17 +0000787 case sKerberosAuthentication:
788 intptr = &options->kerberos_authentication;
789 goto parse_flag;
790
791 case sKerberosOrLocalPasswd:
792 intptr = &options->kerberos_or_local_passwd;
793 goto parse_flag;
794
795 case sKerberosTicketCleanup:
796 intptr = &options->kerberos_ticket_cleanup;
797 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000798
Darren Tucker22ef5082003-12-31 11:37:34 +1100799 case sKerberosGetAFSToken:
800 intptr = &options->kerberos_get_afs_token;
801 goto parse_flag;
802
Darren Tucker0efd1552003-08-26 11:49:55 +1000803 case sGssAuthentication:
804 intptr = &options->gss_authentication;
805 goto parse_flag;
806
807 case sGssCleanupCreds:
808 intptr = &options->gss_cleanup_creds;
809 goto parse_flag;
810
Ben Lindstromade03f62001-12-06 18:22:17 +0000811 case sPasswordAuthentication:
812 intptr = &options->password_authentication;
813 goto parse_flag;
814
815 case sKbdInteractiveAuthentication:
816 intptr = &options->kbd_interactive_authentication;
817 goto parse_flag;
818
819 case sChallengeResponseAuthentication:
820 intptr = &options->challenge_response_authentication;
821 goto parse_flag;
822
823 case sPrintMotd:
824 intptr = &options->print_motd;
825 goto parse_flag;
826
827 case sPrintLastLog:
828 intptr = &options->print_lastlog;
829 goto parse_flag;
830
831 case sX11Forwarding:
832 intptr = &options->x11_forwarding;
833 goto parse_flag;
834
835 case sX11DisplayOffset:
836 intptr = &options->x11_display_offset;
837 goto parse_int;
838
Damien Miller95c249f2002-02-05 12:11:34 +1100839 case sX11UseLocalhost:
840 intptr = &options->x11_use_localhost;
841 goto parse_flag;
842
Ben Lindstromade03f62001-12-06 18:22:17 +0000843 case sXAuthLocation:
844 charptr = &options->xauth_location;
845 goto parse_filename;
846
847 case sStrictModes:
848 intptr = &options->strict_modes;
849 goto parse_flag;
850
Damien Miller12c150e2003-12-17 16:31:10 +1100851 case sTCPKeepAlive:
852 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000853 goto parse_flag;
854
855 case sEmptyPasswd:
856 intptr = &options->permit_empty_passwd;
857 goto parse_flag;
858
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000859 case sPermitUserEnvironment:
860 intptr = &options->permit_user_env;
861 goto parse_flag;
862
Ben Lindstromade03f62001-12-06 18:22:17 +0000863 case sUseLogin:
864 intptr = &options->use_login;
865 goto parse_flag;
866
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000867 case sCompression:
868 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +1000869 arg = strdelim(&cp);
870 if (!arg || *arg == '\0')
871 fatal("%s line %d: missing yes/no/delayed "
872 "argument.", filename, linenum);
873 value = 0; /* silence compiler */
874 if (strcmp(arg, "delayed") == 0)
875 value = COMP_DELAYED;
876 else if (strcmp(arg, "yes") == 0)
877 value = COMP_ZLIB;
878 else if (strcmp(arg, "no") == 0)
879 value = COMP_NONE;
880 else
881 fatal("%s line %d: Bad yes/no/delayed "
882 "argument: %s", filename, linenum, arg);
883 if (*intptr == -1)
884 *intptr = value;
885 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000886
Ben Lindstromade03f62001-12-06 18:22:17 +0000887 case sGatewayPorts:
888 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100889 arg = strdelim(&cp);
890 if (!arg || *arg == '\0')
891 fatal("%s line %d: missing yes/no/clientspecified "
892 "argument.", filename, linenum);
893 value = 0; /* silence compiler */
894 if (strcmp(arg, "clientspecified") == 0)
895 value = 2;
896 else if (strcmp(arg, "yes") == 0)
897 value = 1;
898 else if (strcmp(arg, "no") == 0)
899 value = 0;
900 else
901 fatal("%s line %d: Bad yes/no/clientspecified "
902 "argument: %s", filename, linenum, arg);
903 if (*intptr == -1)
904 *intptr = value;
905 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000906
Damien Miller3a961dc2003-06-03 10:25:48 +1000907 case sUseDNS:
908 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +0000909 goto parse_flag;
910
911 case sLogFacility:
912 intptr = (int *) &options->log_facility;
913 arg = strdelim(&cp);
914 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100915 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000916 fatal("%.200s line %d: unsupported log facility '%s'",
917 filename, linenum, arg ? arg : "<NONE>");
918 if (*intptr == -1)
919 *intptr = (SyslogFacility) value;
920 break;
921
922 case sLogLevel:
923 intptr = (int *) &options->log_level;
924 arg = strdelim(&cp);
925 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100926 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000927 fatal("%.200s line %d: unsupported log level '%s'",
928 filename, linenum, arg ? arg : "<NONE>");
929 if (*intptr == -1)
930 *intptr = (LogLevel) value;
931 break;
932
933 case sAllowTcpForwarding:
934 intptr = &options->allow_tcp_forwarding;
935 goto parse_flag;
936
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000937 case sUsePrivilegeSeparation:
938 intptr = &use_privsep;
939 goto parse_flag;
940
Ben Lindstromade03f62001-12-06 18:22:17 +0000941 case sAllowUsers:
942 while ((arg = strdelim(&cp)) && *arg != '\0') {
943 if (options->num_allow_users >= MAX_ALLOW_USERS)
944 fatal("%s line %d: too many allow users.",
945 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000946 options->allow_users[options->num_allow_users++] =
947 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000948 }
949 break;
950
951 case sDenyUsers:
952 while ((arg = strdelim(&cp)) && *arg != '\0') {
953 if (options->num_deny_users >= MAX_DENY_USERS)
954 fatal( "%s line %d: too many deny users.",
955 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000956 options->deny_users[options->num_deny_users++] =
957 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000958 }
959 break;
960
961 case sAllowGroups:
962 while ((arg = strdelim(&cp)) && *arg != '\0') {
963 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
964 fatal("%s line %d: too many allow groups.",
965 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000966 options->allow_groups[options->num_allow_groups++] =
967 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000968 }
969 break;
970
971 case sDenyGroups:
972 while ((arg = strdelim(&cp)) && *arg != '\0') {
973 if (options->num_deny_groups >= MAX_DENY_GROUPS)
974 fatal("%s line %d: too many deny groups.",
975 filename, linenum);
976 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
977 }
978 break;
979
980 case sCiphers:
981 arg = strdelim(&cp);
982 if (!arg || *arg == '\0')
983 fatal("%s line %d: Missing argument.", filename, linenum);
984 if (!ciphers_valid(arg))
985 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
986 filename, linenum, arg ? arg : "<NONE>");
987 if (options->ciphers == NULL)
988 options->ciphers = xstrdup(arg);
989 break;
990
991 case sMacs:
992 arg = strdelim(&cp);
993 if (!arg || *arg == '\0')
994 fatal("%s line %d: Missing argument.", filename, linenum);
995 if (!mac_valid(arg))
996 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
997 filename, linenum, arg ? arg : "<NONE>");
998 if (options->macs == NULL)
999 options->macs = xstrdup(arg);
1000 break;
1001
1002 case sProtocol:
1003 intptr = &options->protocol;
1004 arg = strdelim(&cp);
1005 if (!arg || *arg == '\0')
1006 fatal("%s line %d: Missing argument.", filename, linenum);
1007 value = proto_spec(arg);
1008 if (value == SSH_PROTO_UNKNOWN)
1009 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001010 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001011 if (*intptr == SSH_PROTO_UNKNOWN)
1012 *intptr = value;
1013 break;
1014
1015 case sSubsystem:
1016 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1017 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001018 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001019 }
1020 arg = strdelim(&cp);
1021 if (!arg || *arg == '\0')
1022 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001023 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001024 if (!*activep) {
1025 arg = strdelim(&cp);
1026 break;
1027 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001028 for (i = 0; i < options->num_subsystems; i++)
1029 if (strcmp(arg, options->subsystem_name[i]) == 0)
1030 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001031 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001032 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1033 arg = strdelim(&cp);
1034 if (!arg || *arg == '\0')
1035 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001036 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001037 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001038
1039 /* Collect arguments (separate to executable) */
1040 p = xstrdup(arg);
1041 len = strlen(p) + 1;
1042 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1043 len += 1 + strlen(arg);
1044 p = xrealloc(p, 1, len);
1045 strlcat(p, " ", len);
1046 strlcat(p, arg, len);
1047 }
1048 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001049 options->num_subsystems++;
1050 break;
1051
1052 case sMaxStartups:
1053 arg = strdelim(&cp);
1054 if (!arg || *arg == '\0')
1055 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001056 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001057 if ((n = sscanf(arg, "%d:%d:%d",
1058 &options->max_startups_begin,
1059 &options->max_startups_rate,
1060 &options->max_startups)) == 3) {
1061 if (options->max_startups_begin >
1062 options->max_startups ||
1063 options->max_startups_rate > 100 ||
1064 options->max_startups_rate < 1)
1065 fatal("%s line %d: Illegal MaxStartups spec.",
1066 filename, linenum);
1067 } else if (n != 1)
1068 fatal("%s line %d: Illegal MaxStartups spec.",
1069 filename, linenum);
1070 else
1071 options->max_startups = options->max_startups_begin;
1072 break;
1073
Darren Tucker89413db2004-05-24 10:36:23 +10001074 case sMaxAuthTries:
1075 intptr = &options->max_authtries;
1076 goto parse_int;
1077
Ben Lindstromade03f62001-12-06 18:22:17 +00001078 case sBanner:
1079 charptr = &options->banner;
1080 goto parse_filename;
1081 /*
1082 * These options can contain %X options expanded at
1083 * connect time, so that you can specify paths like:
1084 *
1085 * AuthorizedKeysFile /etc/ssh_keys/%u
1086 */
1087 case sAuthorizedKeysFile:
1088 case sAuthorizedKeysFile2:
1089 charptr = (opcode == sAuthorizedKeysFile ) ?
1090 &options->authorized_keys_file :
1091 &options->authorized_keys_file2;
1092 goto parse_filename;
1093
1094 case sClientAliveInterval:
1095 intptr = &options->client_alive_interval;
1096 goto parse_time;
1097
1098 case sClientAliveCountMax:
1099 intptr = &options->client_alive_count_max;
1100 goto parse_int;
1101
Darren Tucker46bc0752004-05-02 22:11:30 +10001102 case sAcceptEnv:
1103 while ((arg = strdelim(&cp)) && *arg != '\0') {
1104 if (strchr(arg, '=') != NULL)
1105 fatal("%s line %d: Invalid environment name.",
1106 filename, linenum);
1107 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1108 fatal("%s line %d: too many allow env.",
1109 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001110 if (!*activep)
1111 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001112 options->accept_env[options->num_accept_env++] =
1113 xstrdup(arg);
1114 }
1115 break;
1116
Damien Millerd27b9472005-12-13 19:29:02 +11001117 case sPermitTunnel:
1118 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001119 arg = strdelim(&cp);
1120 if (!arg || *arg == '\0')
1121 fatal("%s line %d: Missing yes/point-to-point/"
1122 "ethernet/no argument.", filename, linenum);
1123 value = 0; /* silence compiler */
1124 if (strcasecmp(arg, "ethernet") == 0)
1125 value = SSH_TUNMODE_ETHERNET;
1126 else if (strcasecmp(arg, "point-to-point") == 0)
1127 value = SSH_TUNMODE_POINTOPOINT;
1128 else if (strcasecmp(arg, "yes") == 0)
1129 value = SSH_TUNMODE_YES;
1130 else if (strcasecmp(arg, "no") == 0)
1131 value = SSH_TUNMODE_NO;
1132 else
1133 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1134 "no argument: %s", filename, linenum, arg);
1135 if (*intptr == -1)
1136 *intptr = value;
1137 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001138
Darren Tucker45150472006-07-12 22:34:17 +10001139 case sMatch:
1140 if (cmdline)
1141 fatal("Match directive not supported as a command-line "
1142 "option");
1143 value = match_cfg_line(&cp, linenum, user, host, address);
1144 if (value < 0)
1145 fatal("%s line %d: Bad Match condition", filename,
1146 linenum);
1147 *activep = value;
1148 break;
1149
Ben Lindstromade03f62001-12-06 18:22:17 +00001150 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001151 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001152 filename, linenum, arg);
1153 while (arg)
1154 arg = strdelim(&cp);
1155 break;
1156
Damien Millerf9b3feb2003-05-16 11:38:32 +10001157 case sUnsupported:
1158 logit("%s line %d: Unsupported option %s",
1159 filename, linenum, arg);
1160 while (arg)
1161 arg = strdelim(&cp);
1162 break;
1163
Ben Lindstromade03f62001-12-06 18:22:17 +00001164 default:
1165 fatal("%s line %d: Missing handler for opcode %s (%d)",
1166 filename, linenum, arg, opcode);
1167 }
1168 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1169 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1170 filename, linenum, arg);
1171 return 0;
1172}
1173
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174/* Reads the server configuration file. */
1175
Damien Miller4af51302000-04-16 11:18:38 +10001176void
Darren Tucker645ab752004-06-25 13:33:20 +10001177load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178{
Darren Tucker645ab752004-06-25 13:33:20 +10001179 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001180 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181
Darren Tucker645ab752004-06-25 13:33:20 +10001182 debug2("%s: filename %s", __func__, filename);
1183 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001184 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001186 }
Darren Tucker645ab752004-06-25 13:33:20 +10001187 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001188 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001189 /*
1190 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001191 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001192 * line numbers later for error messages
1193 */
1194 if ((cp = strchr(line, '#')) != NULL)
1195 memcpy(cp, "\n", 2);
1196 cp = line + strspn(line, " \t\r");
1197
1198 buffer_append(conf, cp, strlen(cp));
1199 }
1200 buffer_append(conf, "\0", 1);
1201 fclose(f);
1202 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1203}
1204
1205void
Darren Tucker45150472006-07-12 22:34:17 +10001206parse_server_match_config(ServerOptions *options, const char *user,
1207 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001208{
Darren Tucker45150472006-07-12 22:34:17 +10001209 ServerOptions mo;
1210
1211 initialize_server_options(&mo);
1212 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
1213 copy_set_server_options(options, &mo);
1214}
1215
1216/* Copy any (supported) values that are set */
1217void
1218copy_set_server_options(ServerOptions *dst, ServerOptions *src)
1219{
1220 if (src->allow_tcp_forwarding != -1)
1221 dst->allow_tcp_forwarding = src->allow_tcp_forwarding;
1222 if (src->gateway_ports != -1)
1223 dst->gateway_ports = src->gateway_ports;
1224}
1225
1226void
1227parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1228 const char *user, const char *host, const char *address)
1229{
1230 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001231 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001232
1233 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1234
Darren Tucker9fbac712004-08-12 22:41:44 +10001235 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001236 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001237 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001238 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001239 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001240 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001241 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001243 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001244 if (bad_options > 0)
1245 fatal("%s: terminating, %d bad configuration options",
1246 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}