blob: 42ec340f3d5d5d447df2b55d0965503279d2b716 [file] [log] [blame]
Darren Tucker45150472006-07-12 22:34:17 +10001/* $OpenBSD: servconf.c,v 1.153 2006/07/12 11:34:58 dtucker 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 Millerd4a8b7e1999-10-27 13:42:43 +100018#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000019#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "servconf.h"
21#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100022#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "misc.h"
25#include "cipher.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000026#include "kex.h"
27#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100028#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029
Ben Lindstrombba81212001-06-25 05:01:22 +000030static void add_listen_addr(ServerOptions *, char *, u_short);
31static void add_one_listen_addr(ServerOptions *, char *, u_short);
Damien Miller34132e52000-01-14 15:45:46 +110032
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033/* Use of privilege separation or not */
34extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100035extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037/* Initializes the server options to their default values. */
38
Damien Miller4af51302000-04-16 11:18:38 +100039void
Damien Miller95def091999-11-25 00:26:21 +110040initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041{
Damien Miller95def091999-11-25 00:26:21 +110042 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110043
44 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100045 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110046
47 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110048 options->num_ports = 0;
49 options->ports_from_cmdline = 0;
50 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110051 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110052 options->num_host_key_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100053 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110054 options->server_key_bits = -1;
55 options->login_grace_time = -1;
56 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000057 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110058 options->ignore_rhosts = -1;
59 options->ignore_user_known_hosts = -1;
60 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000061 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110062 options->x11_forwarding = -1;
63 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110064 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100065 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110066 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110067 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110068 options->log_facility = SYSLOG_FACILITY_NOT_SET;
69 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110070 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000071 options->hostbased_authentication = -1;
72 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110073 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110074 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110075 options->kerberos_authentication = -1;
76 options->kerberos_or_local_passwd = -1;
77 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +110078 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +100079 options->gss_authentication=-1;
80 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +110081 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +110082 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +000083 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110084 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +000085 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +110086 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +000087 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +110088 options->allow_tcp_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +110089 options->num_allow_users = 0;
90 options->num_deny_users = 0;
91 options->num_allow_groups = 0;
92 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +100093 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000094 options->macs = NULL;
Damien Miller78928792000-04-12 20:17:38 +100095 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +100096 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +100097 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +100098 options->max_startups_begin = -1;
99 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000100 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000101 options->max_authtries = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000102 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000103 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000104 options->client_alive_interval = -1;
105 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000106 options->authorized_keys_file = NULL;
107 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000108 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100109 options->permit_tun = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110}
111
Damien Miller4af51302000-04-16 11:18:38 +1000112void
Damien Miller95def091999-11-25 00:26:21 +1100113fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114{
Damien Miller726273e2001-11-12 11:40:11 +1100115 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000116 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000117 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100118
119 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100120 if (options->protocol == SSH_PROTO_UNKNOWN)
121 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
122 if (options->num_host_key_files == 0) {
123 /* fill default hostkeys for protocols */
124 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100125 options->host_key_files[options->num_host_key_files++] =
126 _PATH_HOST_KEY_FILE;
127 if (options->protocol & SSH_PROTO_2) {
128 options->host_key_files[options->num_host_key_files++] =
129 _PATH_HOST_RSA_KEY_FILE;
130 options->host_key_files[options->num_host_key_files++] =
131 _PATH_HOST_DSA_KEY_FILE;
132 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100133 }
Damien Miller34132e52000-01-14 15:45:46 +1100134 if (options->num_ports == 0)
135 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
136 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000137 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000138 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000139 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100140 if (options->server_key_bits == -1)
141 options->server_key_bits = 768;
142 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000143 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100144 if (options->key_regeneration_time == -1)
145 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000146 if (options->permit_root_login == PERMIT_NOT_SET)
147 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100148 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100149 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100150 if (options->ignore_user_known_hosts == -1)
151 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100152 if (options->print_motd == -1)
153 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000154 if (options->print_lastlog == -1)
155 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100156 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100157 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100158 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100159 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100160 if (options->x11_use_localhost == -1)
161 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000162 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000163 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100164 if (options->strict_modes == -1)
165 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100166 if (options->tcp_keep_alive == -1)
167 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100168 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100169 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100170 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000171 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100172 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100173 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000174 if (options->hostbased_authentication == -1)
175 options->hostbased_authentication = 0;
176 if (options->hostbased_uses_name_from_packet_only == -1)
177 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100178 if (options->rsa_authentication == -1)
179 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100180 if (options->pubkey_authentication == -1)
181 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100182 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000183 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100184 if (options->kerberos_or_local_passwd == -1)
185 options->kerberos_or_local_passwd = 1;
186 if (options->kerberos_ticket_cleanup == -1)
187 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100188 if (options->kerberos_get_afs_token == -1)
189 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000190 if (options->gss_authentication == -1)
191 options->gss_authentication = 0;
192 if (options->gss_cleanup_creds == -1)
193 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100194 if (options->password_authentication == -1)
195 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100196 if (options->kbd_interactive_authentication == -1)
197 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000198 if (options->challenge_response_authentication == -1)
199 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100200 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100201 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000202 if (options->permit_user_env == -1)
203 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100204 if (options->use_login == -1)
205 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000206 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000207 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100208 if (options->allow_tcp_forwarding == -1)
209 options->allow_tcp_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000210 if (options->gateway_ports == -1)
211 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000212 if (options->max_startups == -1)
213 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000214 if (options->max_startups_rate == -1)
215 options->max_startups_rate = 100; /* 100% */
216 if (options->max_startups_begin == -1)
217 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000218 if (options->max_authtries == -1)
219 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000220 if (options->use_dns == -1)
221 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000222 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000224 if (options->client_alive_count_max == -1)
225 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100226 if (options->authorized_keys_file2 == NULL) {
227 /* authorized_keys_file2 falls back to authorized_keys_file */
228 if (options->authorized_keys_file != NULL)
229 options->authorized_keys_file2 = options->authorized_keys_file;
230 else
231 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
232 }
233 if (options->authorized_keys_file == NULL)
234 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
Damien Millerd27b9472005-12-13 19:29:02 +1100235 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100236 options->permit_tun = SSH_TUNMODE_NO;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237
Ben Lindstromfb62a692002-06-06 19:47:11 +0000238 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000240 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000241
Tim Rice40017b02002-07-14 13:36:49 -0700242#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000243 if (use_privsep && options->compression == 1) {
244 error("This platform does not support both privilege "
245 "separation and compression");
246 error("Compression disabled");
247 options->compression = 0;
248 }
249#endif
250
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251}
252
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100254typedef enum {
255 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100256 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000257 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100258 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100259 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
260 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000261 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100262 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100263 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000264 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100265 sPasswordAuthentication, sKbdInteractiveAuthentication,
266 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000267 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100268 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100269 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000270 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100271 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000272 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000273 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
274 sMaxStartups, sMaxAuthTries,
Damien Miller3a961dc2003-06-03 10:25:48 +1000275 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100276 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000277 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100278 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Darren Tucker45150472006-07-12 22:34:17 +1000279 sMatch,
Ben Lindstromc7431342002-03-22 03:11:49 +0000280 sUsePrivilegeSeparation,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000281 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282} ServerOpCodes;
283
Darren Tucker45150472006-07-12 22:34:17 +1000284#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
285#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
286#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
287
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100289static struct {
290 const char *name;
291 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000292 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100293} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100294 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000295#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000296 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000297#else
Darren Tucker45150472006-07-12 22:34:17 +1000298 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000299#endif
Darren Tucker45150472006-07-12 22:34:17 +1000300 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100301 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000302 { "port", sPort, SSHCFG_GLOBAL },
303 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
304 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
305 { "pidfile", sPidFile, SSHCFG_GLOBAL },
306 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
307 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
308 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
309 { "permitrootlogin", sPermitRootLogin, SSHCFG_GLOBAL },
310 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
311 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
312 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
313 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_GLOBAL },
314 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_GLOBAL },
315 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
316 { "rsaauthentication", sRSAAuthentication, SSHCFG_GLOBAL },
317 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL },
318 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000319#ifdef KRB5
Darren Tucker45150472006-07-12 22:34:17 +1000320 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_GLOBAL },
321 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
322 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100323#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000324 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000325#else
Darren Tucker45150472006-07-12 22:34:17 +1000326 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100327#endif
328#else
Darren Tucker45150472006-07-12 22:34:17 +1000329 { "kerberosauthentication", sUnsupported, SSHCFG_GLOBAL },
330 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
331 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
332 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000333#endif
Darren Tucker45150472006-07-12 22:34:17 +1000334 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
335 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000336#ifdef GSSAPI
Darren Tucker45150472006-07-12 22:34:17 +1000337 { "gssapiauthentication", sGssAuthentication, SSHCFG_GLOBAL },
338 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000339#else
Darren Tucker45150472006-07-12 22:34:17 +1000340 { "gssapiauthentication", sUnsupported, SSHCFG_GLOBAL },
341 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000342#endif
Darren Tucker45150472006-07-12 22:34:17 +1000343 { "passwordauthentication", sPasswordAuthentication, SSHCFG_GLOBAL },
344 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_GLOBAL },
345 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
346 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
347 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
348 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
349 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
350 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
351 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
352 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
353 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
354 { "x11forwarding", sX11Forwarding, SSHCFG_GLOBAL },
355 { "x11displayoffset", sX11DisplayOffset, SSHCFG_GLOBAL },
356 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_GLOBAL },
357 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
358 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
359 { "permitemptypasswords", sEmptyPasswd, SSHCFG_GLOBAL },
360 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
361 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
362 { "compression", sCompression, SSHCFG_GLOBAL },
363 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
364 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
365 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
366 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
367 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
368 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
369 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
370 { "ciphers", sCiphers, SSHCFG_GLOBAL },
371 { "macs", sMacs, SSHCFG_GLOBAL },
372 { "protocol", sProtocol, SSHCFG_GLOBAL },
373 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
374 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
375 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
376 { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
377 { "banner", sBanner, SSHCFG_GLOBAL },
378 { "usedns", sUseDNS, SSHCFG_GLOBAL },
379 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
380 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
381 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
382 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
383 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
384 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
385 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL },
386 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
387 { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
388 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389};
390
Damien Miller5428f641999-11-25 11:54:57 +1100391/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000392 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100393 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394
Damien Miller4af51302000-04-16 11:18:38 +1000395static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100396parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000397 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000399 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Damien Miller95def091999-11-25 00:26:21 +1100401 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000402 if (strcasecmp(cp, keywords[i].name) == 0) {
403 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100404 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000405 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000407 error("%s: line %d: Bad configuration option: %s",
408 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100409 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410}
411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static void
Ben Lindstrom19066a12001-04-12 23:39:26 +0000413add_listen_addr(ServerOptions *options, char *addr, u_short port)
Damien Miller34132e52000-01-14 15:45:46 +1100414{
Damien Millereccb9de2005-06-17 12:59:34 +1000415 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100416
417 if (options->num_ports == 0)
418 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100419 if (options->address_family == -1)
420 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000421 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000422 for (i = 0; i < options->num_ports; i++)
423 add_one_listen_addr(options, addr, options->ports[i]);
424 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000425 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000426}
427
Ben Lindstrombba81212001-06-25 05:01:22 +0000428static void
Ben Lindstromc510af42001-04-07 17:25:48 +0000429add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
430{
431 struct addrinfo hints, *ai, *aitop;
432 char strport[NI_MAXSERV];
433 int gaierr;
434
435 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100436 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000437 hints.ai_socktype = SOCK_STREAM;
438 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Ben Lindstrome1353632002-06-23 21:29:23 +0000439 snprintf(strport, sizeof strport, "%u", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000440 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
441 fatal("bad addr or host: %s (%s)",
442 addr ? addr : "<NULL>",
443 gai_strerror(gaierr));
444 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
445 ;
446 ai->ai_next = options->listen_addrs;
447 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100448}
449
Darren Tucker45150472006-07-12 22:34:17 +1000450/*
451 * The strategy for the Match blocks is that the config file is parsed twice.
452 *
453 * The first time is at startup. activep is initialized to 1 and the
454 * directives in the global context are processed and acted on. Hitting a
455 * Match directive unsets activep and the directives inside the block are
456 * checked for syntax only.
457 *
458 * The second time is after a connection has been established but before
459 * authentication. activep is initialized to 2 and global config directives
460 * are ignored since they have already been processed. If the criteria in a
461 * Match block is met, activep is set and the subsequent directives
462 * processed and actioned until EOF or another Match block unsets it. Any
463 * options set are copied into the main server config.
464 *
465 * Potential additions/improvements:
466 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
467 *
468 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
469 * Match Address 192.168.0.*
470 * Tag trusted
471 * Match Group wheel
472 * Tag trusted
473 * Match Tag trusted
474 * AllowTcpForwarding yes
475 * GatewayPorts clientspecified
476 * [...]
477 *
478 * - Add a PermittedChannelRequests directive
479 * Match Group shell
480 * PermittedChannelRequests session,forwarded-tcpip
481 */
482
483static int
484match_cfg_line(char **condition, int line, const char *user, const char *host,
485 const char *address)
486{
487 int result = 1;
488 char *arg, *attrib, *cp = *condition;
489 size_t len;
490
491 if (user == NULL)
492 debug3("checking syntax for 'Match %s'", cp);
493 else
494 debug3("checking match for '%s' user %s host %s addr %s", cp,
495 user ? user : "(null)", host ? host : "(null)",
496 address ? address : "(null)");
497
498 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
499 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
500 error("Missing Match criteria for %s", attrib);
501 return -1;
502 }
503 len = strlen(arg);
504 if (strcasecmp(attrib, "user") == 0) {
505 if (!user) {
506 result = 0;
507 continue;
508 }
509 if (match_pattern_list(user, arg, len, 0) != 1)
510 result = 0;
511 else
512 debug("user %.100s matched 'User %.100s' at "
513 "line %d", user, arg, line);
514 } else if (strcasecmp(attrib, "host") == 0) {
515 if (!host) {
516 result = 0;
517 continue;
518 }
519 if (match_hostname(host, arg, len) != 1)
520 result = 0;
521 else
522 debug("connection from %.100s matched 'Host "
523 "%.100s' at line %d", host, arg, line);
524 } else if (strcasecmp(attrib, "address") == 0) {
525 debug("address '%s' arg '%s'", address, arg);
526 if (!address) {
527 result = 0;
528 continue;
529 }
530 if (match_hostname(address, arg, len) != 1)
531 result = 0;
532 else
533 debug("connection from %.100s matched 'Address "
534 "%.100s' at line %d", address, arg, line);
535 } else {
536 error("Unsupported Match attribute %s", attrib);
537 return -1;
538 }
539 }
540 if (user != NULL)
541 debug3("match %sfound", result ? "" : "not ");
542 *condition = cp;
543 return result;
544}
545
Ben Lindstromade03f62001-12-06 18:22:17 +0000546int
547process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000548 const char *filename, int linenum, int *activep, const char *user,
549 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000550{
551 char *cp, **charptr, *arg, *p;
Darren Tucker45150472006-07-12 22:34:17 +1000552 int cmdline = 0, *intptr, value, n;
Ben Lindstromade03f62001-12-06 18:22:17 +0000553 ServerOpCodes opcode;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100554 u_short port;
Darren Tucker45150472006-07-12 22:34:17 +1000555 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000556 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000557
558 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100559 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100560 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000561 /* Ignore leading whitespace */
562 if (*arg == '\0')
563 arg = strdelim(&cp);
564 if (!arg || !*arg || *arg == '#')
565 return 0;
566 intptr = NULL;
567 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000568 opcode = parse_token(arg, filename, linenum, &flags);
569
570 if (activep == NULL) { /* We are processing a command line directive */
571 cmdline = 1;
572 activep = &cmdline;
573 }
574 if (*activep && opcode != sMatch)
575 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
576 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
577 if (user == NULL) {
578 fatal("%s line %d: Directive '%s' is not allowed "
579 "within a Match block", filename, linenum, arg);
580 } else { /* this is a directive we have already processed */
581 while (arg)
582 arg = strdelim(&cp);
583 return 0;
584 }
585 }
586
Ben Lindstromade03f62001-12-06 18:22:17 +0000587 switch (opcode) {
588 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000589 case sUsePAM:
590 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000591 goto parse_flag;
592
593 /* Standard Options */
594 case sBadOption:
595 return -1;
596 case sPort:
597 /* ignore ports from configfile if cmdline specifies ports */
598 if (options->ports_from_cmdline)
599 return 0;
600 if (options->listen_addrs != NULL)
601 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100602 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000603 if (options->num_ports >= MAX_PORTS)
604 fatal("%s line %d: too many ports.",
605 filename, linenum);
606 arg = strdelim(&cp);
607 if (!arg || *arg == '\0')
608 fatal("%s line %d: missing port number.",
609 filename, linenum);
610 options->ports[options->num_ports++] = a2port(arg);
611 if (options->ports[options->num_ports-1] == 0)
612 fatal("%s line %d: Badly formatted port number.",
613 filename, linenum);
614 break;
615
616 case sServerKeyBits:
617 intptr = &options->server_key_bits;
618parse_int:
619 arg = strdelim(&cp);
620 if (!arg || *arg == '\0')
621 fatal("%s line %d: missing integer value.",
622 filename, linenum);
623 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000624 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000625 *intptr = value;
626 break;
627
628 case sLoginGraceTime:
629 intptr = &options->login_grace_time;
630parse_time:
631 arg = strdelim(&cp);
632 if (!arg || *arg == '\0')
633 fatal("%s line %d: missing time value.",
634 filename, linenum);
635 if ((value = convtime(arg)) == -1)
636 fatal("%s line %d: invalid time value.",
637 filename, linenum);
638 if (*intptr == -1)
639 *intptr = value;
640 break;
641
642 case sKeyRegenerationTime:
643 intptr = &options->key_regeneration_time;
644 goto parse_time;
645
646 case sListenAddress:
647 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100648 if (arg == NULL || *arg == '\0')
649 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000650 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000651 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
652 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
653 && strchr(p+1, ':') != NULL) {
654 add_listen_addr(options, arg, 0);
655 break;
656 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100657 p = hpdelim(&arg);
658 if (p == NULL)
659 fatal("%s line %d: bad address:port usage",
660 filename, linenum);
661 p = cleanhostname(p);
662 if (arg == NULL)
663 port = 0;
664 else if ((port = a2port(arg)) == 0)
665 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000666
Damien Millerf91ee4c2005-03-01 21:24:33 +1100667 add_listen_addr(options, p, port);
668
Ben Lindstromade03f62001-12-06 18:22:17 +0000669 break;
670
Darren Tucker0f383232005-01-20 10:57:56 +1100671 case sAddressFamily:
672 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000673 if (!arg || *arg == '\0')
674 fatal("%s line %d: missing address family.",
675 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100676 intptr = &options->address_family;
677 if (options->listen_addrs != NULL)
678 fatal("%s line %d: address family must be specified before "
679 "ListenAddress.", filename, linenum);
680 if (strcasecmp(arg, "inet") == 0)
681 value = AF_INET;
682 else if (strcasecmp(arg, "inet6") == 0)
683 value = AF_INET6;
684 else if (strcasecmp(arg, "any") == 0)
685 value = AF_UNSPEC;
686 else
687 fatal("%s line %d: unsupported address family \"%s\".",
688 filename, linenum, arg);
689 if (*intptr == -1)
690 *intptr = value;
691 break;
692
Ben Lindstromade03f62001-12-06 18:22:17 +0000693 case sHostKeyFile:
694 intptr = &options->num_host_key_files;
695 if (*intptr >= MAX_HOSTKEYS)
696 fatal("%s line %d: too many host keys specified (max %d).",
697 filename, linenum, MAX_HOSTKEYS);
698 charptr = &options->host_key_files[*intptr];
699parse_filename:
700 arg = strdelim(&cp);
701 if (!arg || *arg == '\0')
702 fatal("%s line %d: missing file name.",
703 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000704 if (*activep && *charptr == NULL) {
Ben Lindstromade03f62001-12-06 18:22:17 +0000705 *charptr = tilde_expand_filename(arg, getuid());
706 /* increase optional counter */
707 if (intptr != NULL)
708 *intptr = *intptr + 1;
709 }
710 break;
711
712 case sPidFile:
713 charptr = &options->pid_file;
714 goto parse_filename;
715
716 case sPermitRootLogin:
717 intptr = &options->permit_root_login;
718 arg = strdelim(&cp);
719 if (!arg || *arg == '\0')
720 fatal("%s line %d: missing yes/"
721 "without-password/forced-commands-only/no "
722 "argument.", filename, linenum);
723 value = 0; /* silence compiler */
724 if (strcmp(arg, "without-password") == 0)
725 value = PERMIT_NO_PASSWD;
726 else if (strcmp(arg, "forced-commands-only") == 0)
727 value = PERMIT_FORCED_ONLY;
728 else if (strcmp(arg, "yes") == 0)
729 value = PERMIT_YES;
730 else if (strcmp(arg, "no") == 0)
731 value = PERMIT_NO;
732 else
733 fatal("%s line %d: Bad yes/"
734 "without-password/forced-commands-only/no "
735 "argument: %s", filename, linenum, arg);
736 if (*intptr == -1)
737 *intptr = value;
738 break;
739
740 case sIgnoreRhosts:
741 intptr = &options->ignore_rhosts;
742parse_flag:
743 arg = strdelim(&cp);
744 if (!arg || *arg == '\0')
745 fatal("%s line %d: missing yes/no argument.",
746 filename, linenum);
747 value = 0; /* silence compiler */
748 if (strcmp(arg, "yes") == 0)
749 value = 1;
750 else if (strcmp(arg, "no") == 0)
751 value = 0;
752 else
753 fatal("%s line %d: Bad yes/no argument: %s",
754 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000755 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000756 *intptr = value;
757 break;
758
759 case sIgnoreUserKnownHosts:
760 intptr = &options->ignore_user_known_hosts;
761 goto parse_flag;
762
Ben Lindstromade03f62001-12-06 18:22:17 +0000763 case sRhostsRSAAuthentication:
764 intptr = &options->rhosts_rsa_authentication;
765 goto parse_flag;
766
767 case sHostbasedAuthentication:
768 intptr = &options->hostbased_authentication;
769 goto parse_flag;
770
771 case sHostbasedUsesNameFromPacketOnly:
772 intptr = &options->hostbased_uses_name_from_packet_only;
773 goto parse_flag;
774
775 case sRSAAuthentication:
776 intptr = &options->rsa_authentication;
777 goto parse_flag;
778
779 case sPubkeyAuthentication:
780 intptr = &options->pubkey_authentication;
781 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000782
Ben Lindstromade03f62001-12-06 18:22:17 +0000783 case sKerberosAuthentication:
784 intptr = &options->kerberos_authentication;
785 goto parse_flag;
786
787 case sKerberosOrLocalPasswd:
788 intptr = &options->kerberos_or_local_passwd;
789 goto parse_flag;
790
791 case sKerberosTicketCleanup:
792 intptr = &options->kerberos_ticket_cleanup;
793 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000794
Darren Tucker22ef5082003-12-31 11:37:34 +1100795 case sKerberosGetAFSToken:
796 intptr = &options->kerberos_get_afs_token;
797 goto parse_flag;
798
Darren Tucker0efd1552003-08-26 11:49:55 +1000799 case sGssAuthentication:
800 intptr = &options->gss_authentication;
801 goto parse_flag;
802
803 case sGssCleanupCreds:
804 intptr = &options->gss_cleanup_creds;
805 goto parse_flag;
806
Ben Lindstromade03f62001-12-06 18:22:17 +0000807 case sPasswordAuthentication:
808 intptr = &options->password_authentication;
809 goto parse_flag;
810
811 case sKbdInteractiveAuthentication:
812 intptr = &options->kbd_interactive_authentication;
813 goto parse_flag;
814
815 case sChallengeResponseAuthentication:
816 intptr = &options->challenge_response_authentication;
817 goto parse_flag;
818
819 case sPrintMotd:
820 intptr = &options->print_motd;
821 goto parse_flag;
822
823 case sPrintLastLog:
824 intptr = &options->print_lastlog;
825 goto parse_flag;
826
827 case sX11Forwarding:
828 intptr = &options->x11_forwarding;
829 goto parse_flag;
830
831 case sX11DisplayOffset:
832 intptr = &options->x11_display_offset;
833 goto parse_int;
834
Damien Miller95c249f2002-02-05 12:11:34 +1100835 case sX11UseLocalhost:
836 intptr = &options->x11_use_localhost;
837 goto parse_flag;
838
Ben Lindstromade03f62001-12-06 18:22:17 +0000839 case sXAuthLocation:
840 charptr = &options->xauth_location;
841 goto parse_filename;
842
843 case sStrictModes:
844 intptr = &options->strict_modes;
845 goto parse_flag;
846
Damien Miller12c150e2003-12-17 16:31:10 +1100847 case sTCPKeepAlive:
848 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000849 goto parse_flag;
850
851 case sEmptyPasswd:
852 intptr = &options->permit_empty_passwd;
853 goto parse_flag;
854
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000855 case sPermitUserEnvironment:
856 intptr = &options->permit_user_env;
857 goto parse_flag;
858
Ben Lindstromade03f62001-12-06 18:22:17 +0000859 case sUseLogin:
860 intptr = &options->use_login;
861 goto parse_flag;
862
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000863 case sCompression:
864 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +1000865 arg = strdelim(&cp);
866 if (!arg || *arg == '\0')
867 fatal("%s line %d: missing yes/no/delayed "
868 "argument.", filename, linenum);
869 value = 0; /* silence compiler */
870 if (strcmp(arg, "delayed") == 0)
871 value = COMP_DELAYED;
872 else if (strcmp(arg, "yes") == 0)
873 value = COMP_ZLIB;
874 else if (strcmp(arg, "no") == 0)
875 value = COMP_NONE;
876 else
877 fatal("%s line %d: Bad yes/no/delayed "
878 "argument: %s", filename, linenum, arg);
879 if (*intptr == -1)
880 *intptr = value;
881 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000882
Ben Lindstromade03f62001-12-06 18:22:17 +0000883 case sGatewayPorts:
884 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100885 arg = strdelim(&cp);
886 if (!arg || *arg == '\0')
887 fatal("%s line %d: missing yes/no/clientspecified "
888 "argument.", filename, linenum);
889 value = 0; /* silence compiler */
890 if (strcmp(arg, "clientspecified") == 0)
891 value = 2;
892 else if (strcmp(arg, "yes") == 0)
893 value = 1;
894 else if (strcmp(arg, "no") == 0)
895 value = 0;
896 else
897 fatal("%s line %d: Bad yes/no/clientspecified "
898 "argument: %s", filename, linenum, arg);
899 if (*intptr == -1)
900 *intptr = value;
901 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000902
Damien Miller3a961dc2003-06-03 10:25:48 +1000903 case sUseDNS:
904 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +0000905 goto parse_flag;
906
907 case sLogFacility:
908 intptr = (int *) &options->log_facility;
909 arg = strdelim(&cp);
910 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100911 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000912 fatal("%.200s line %d: unsupported log facility '%s'",
913 filename, linenum, arg ? arg : "<NONE>");
914 if (*intptr == -1)
915 *intptr = (SyslogFacility) value;
916 break;
917
918 case sLogLevel:
919 intptr = (int *) &options->log_level;
920 arg = strdelim(&cp);
921 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100922 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000923 fatal("%.200s line %d: unsupported log level '%s'",
924 filename, linenum, arg ? arg : "<NONE>");
925 if (*intptr == -1)
926 *intptr = (LogLevel) value;
927 break;
928
929 case sAllowTcpForwarding:
930 intptr = &options->allow_tcp_forwarding;
931 goto parse_flag;
932
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933 case sUsePrivilegeSeparation:
934 intptr = &use_privsep;
935 goto parse_flag;
936
Ben Lindstromade03f62001-12-06 18:22:17 +0000937 case sAllowUsers:
938 while ((arg = strdelim(&cp)) && *arg != '\0') {
939 if (options->num_allow_users >= MAX_ALLOW_USERS)
940 fatal("%s line %d: too many allow users.",
941 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000942 options->allow_users[options->num_allow_users++] =
943 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000944 }
945 break;
946
947 case sDenyUsers:
948 while ((arg = strdelim(&cp)) && *arg != '\0') {
949 if (options->num_deny_users >= MAX_DENY_USERS)
950 fatal( "%s line %d: too many deny users.",
951 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000952 options->deny_users[options->num_deny_users++] =
953 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000954 }
955 break;
956
957 case sAllowGroups:
958 while ((arg = strdelim(&cp)) && *arg != '\0') {
959 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
960 fatal("%s line %d: too many allow groups.",
961 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000962 options->allow_groups[options->num_allow_groups++] =
963 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000964 }
965 break;
966
967 case sDenyGroups:
968 while ((arg = strdelim(&cp)) && *arg != '\0') {
969 if (options->num_deny_groups >= MAX_DENY_GROUPS)
970 fatal("%s line %d: too many deny groups.",
971 filename, linenum);
972 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
973 }
974 break;
975
976 case sCiphers:
977 arg = strdelim(&cp);
978 if (!arg || *arg == '\0')
979 fatal("%s line %d: Missing argument.", filename, linenum);
980 if (!ciphers_valid(arg))
981 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
982 filename, linenum, arg ? arg : "<NONE>");
983 if (options->ciphers == NULL)
984 options->ciphers = xstrdup(arg);
985 break;
986
987 case sMacs:
988 arg = strdelim(&cp);
989 if (!arg || *arg == '\0')
990 fatal("%s line %d: Missing argument.", filename, linenum);
991 if (!mac_valid(arg))
992 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
993 filename, linenum, arg ? arg : "<NONE>");
994 if (options->macs == NULL)
995 options->macs = xstrdup(arg);
996 break;
997
998 case sProtocol:
999 intptr = &options->protocol;
1000 arg = strdelim(&cp);
1001 if (!arg || *arg == '\0')
1002 fatal("%s line %d: Missing argument.", filename, linenum);
1003 value = proto_spec(arg);
1004 if (value == SSH_PROTO_UNKNOWN)
1005 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001006 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001007 if (*intptr == SSH_PROTO_UNKNOWN)
1008 *intptr = value;
1009 break;
1010
1011 case sSubsystem:
1012 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1013 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001014 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001015 }
1016 arg = strdelim(&cp);
1017 if (!arg || *arg == '\0')
1018 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001019 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001020 if (!*activep) {
1021 arg = strdelim(&cp);
1022 break;
1023 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001024 for (i = 0; i < options->num_subsystems; i++)
1025 if (strcmp(arg, options->subsystem_name[i]) == 0)
1026 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001027 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001028 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1029 arg = strdelim(&cp);
1030 if (!arg || *arg == '\0')
1031 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001032 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001033 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001034
1035 /* Collect arguments (separate to executable) */
1036 p = xstrdup(arg);
1037 len = strlen(p) + 1;
1038 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1039 len += 1 + strlen(arg);
1040 p = xrealloc(p, 1, len);
1041 strlcat(p, " ", len);
1042 strlcat(p, arg, len);
1043 }
1044 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001045 options->num_subsystems++;
1046 break;
1047
1048 case sMaxStartups:
1049 arg = strdelim(&cp);
1050 if (!arg || *arg == '\0')
1051 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001052 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001053 if ((n = sscanf(arg, "%d:%d:%d",
1054 &options->max_startups_begin,
1055 &options->max_startups_rate,
1056 &options->max_startups)) == 3) {
1057 if (options->max_startups_begin >
1058 options->max_startups ||
1059 options->max_startups_rate > 100 ||
1060 options->max_startups_rate < 1)
1061 fatal("%s line %d: Illegal MaxStartups spec.",
1062 filename, linenum);
1063 } else if (n != 1)
1064 fatal("%s line %d: Illegal MaxStartups spec.",
1065 filename, linenum);
1066 else
1067 options->max_startups = options->max_startups_begin;
1068 break;
1069
Darren Tucker89413db2004-05-24 10:36:23 +10001070 case sMaxAuthTries:
1071 intptr = &options->max_authtries;
1072 goto parse_int;
1073
Ben Lindstromade03f62001-12-06 18:22:17 +00001074 case sBanner:
1075 charptr = &options->banner;
1076 goto parse_filename;
1077 /*
1078 * These options can contain %X options expanded at
1079 * connect time, so that you can specify paths like:
1080 *
1081 * AuthorizedKeysFile /etc/ssh_keys/%u
1082 */
1083 case sAuthorizedKeysFile:
1084 case sAuthorizedKeysFile2:
1085 charptr = (opcode == sAuthorizedKeysFile ) ?
1086 &options->authorized_keys_file :
1087 &options->authorized_keys_file2;
1088 goto parse_filename;
1089
1090 case sClientAliveInterval:
1091 intptr = &options->client_alive_interval;
1092 goto parse_time;
1093
1094 case sClientAliveCountMax:
1095 intptr = &options->client_alive_count_max;
1096 goto parse_int;
1097
Darren Tucker46bc0752004-05-02 22:11:30 +10001098 case sAcceptEnv:
1099 while ((arg = strdelim(&cp)) && *arg != '\0') {
1100 if (strchr(arg, '=') != NULL)
1101 fatal("%s line %d: Invalid environment name.",
1102 filename, linenum);
1103 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1104 fatal("%s line %d: too many allow env.",
1105 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001106 if (!*activep)
1107 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001108 options->accept_env[options->num_accept_env++] =
1109 xstrdup(arg);
1110 }
1111 break;
1112
Damien Millerd27b9472005-12-13 19:29:02 +11001113 case sPermitTunnel:
1114 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001115 arg = strdelim(&cp);
1116 if (!arg || *arg == '\0')
1117 fatal("%s line %d: Missing yes/point-to-point/"
1118 "ethernet/no argument.", filename, linenum);
1119 value = 0; /* silence compiler */
1120 if (strcasecmp(arg, "ethernet") == 0)
1121 value = SSH_TUNMODE_ETHERNET;
1122 else if (strcasecmp(arg, "point-to-point") == 0)
1123 value = SSH_TUNMODE_POINTOPOINT;
1124 else if (strcasecmp(arg, "yes") == 0)
1125 value = SSH_TUNMODE_YES;
1126 else if (strcasecmp(arg, "no") == 0)
1127 value = SSH_TUNMODE_NO;
1128 else
1129 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1130 "no argument: %s", filename, linenum, arg);
1131 if (*intptr == -1)
1132 *intptr = value;
1133 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001134
Darren Tucker45150472006-07-12 22:34:17 +10001135 case sMatch:
1136 if (cmdline)
1137 fatal("Match directive not supported as a command-line "
1138 "option");
1139 value = match_cfg_line(&cp, linenum, user, host, address);
1140 if (value < 0)
1141 fatal("%s line %d: Bad Match condition", filename,
1142 linenum);
1143 *activep = value;
1144 break;
1145
Ben Lindstromade03f62001-12-06 18:22:17 +00001146 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001147 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001148 filename, linenum, arg);
1149 while (arg)
1150 arg = strdelim(&cp);
1151 break;
1152
Damien Millerf9b3feb2003-05-16 11:38:32 +10001153 case sUnsupported:
1154 logit("%s line %d: Unsupported option %s",
1155 filename, linenum, arg);
1156 while (arg)
1157 arg = strdelim(&cp);
1158 break;
1159
Ben Lindstromade03f62001-12-06 18:22:17 +00001160 default:
1161 fatal("%s line %d: Missing handler for opcode %s (%d)",
1162 filename, linenum, arg, opcode);
1163 }
1164 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1165 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1166 filename, linenum, arg);
1167 return 0;
1168}
1169
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170/* Reads the server configuration file. */
1171
Damien Miller4af51302000-04-16 11:18:38 +10001172void
Darren Tucker645ab752004-06-25 13:33:20 +10001173load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174{
Darren Tucker645ab752004-06-25 13:33:20 +10001175 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001176 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177
Darren Tucker645ab752004-06-25 13:33:20 +10001178 debug2("%s: filename %s", __func__, filename);
1179 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001180 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001182 }
Darren Tucker645ab752004-06-25 13:33:20 +10001183 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001184 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001185 /*
1186 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001187 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001188 * line numbers later for error messages
1189 */
1190 if ((cp = strchr(line, '#')) != NULL)
1191 memcpy(cp, "\n", 2);
1192 cp = line + strspn(line, " \t\r");
1193
1194 buffer_append(conf, cp, strlen(cp));
1195 }
1196 buffer_append(conf, "\0", 1);
1197 fclose(f);
1198 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1199}
1200
1201void
Darren Tucker45150472006-07-12 22:34:17 +10001202parse_server_match_config(ServerOptions *options, const char *user,
1203 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001204{
Darren Tucker45150472006-07-12 22:34:17 +10001205 ServerOptions mo;
1206
1207 initialize_server_options(&mo);
1208 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
1209 copy_set_server_options(options, &mo);
1210}
1211
1212/* Copy any (supported) values that are set */
1213void
1214copy_set_server_options(ServerOptions *dst, ServerOptions *src)
1215{
1216 if (src->allow_tcp_forwarding != -1)
1217 dst->allow_tcp_forwarding = src->allow_tcp_forwarding;
1218 if (src->gateway_ports != -1)
1219 dst->gateway_ports = src->gateway_ports;
1220}
1221
1222void
1223parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1224 const char *user, const char *host, const char *address)
1225{
1226 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001227 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001228
1229 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1230
Darren Tucker9fbac712004-08-12 22:41:44 +10001231 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001232 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001233 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001234 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001235 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001236 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001237 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001238 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001239 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001240 if (bad_options > 0)
1241 fatal("%s: terminating, %d bad configuration options",
1242 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243}