blob: c5b933ab9c0e8739b040b3c688a2e1417f98f522 [file] [log] [blame]
Damien Millere3b60b52006-07-10 21:08:03 +10001/* $OpenBSD: servconf.c,v 1.152 2006/07/08 21:47:12 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 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"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028
Ben Lindstrombba81212001-06-25 05:01:22 +000029static void add_listen_addr(ServerOptions *, char *, u_short);
30static void add_one_listen_addr(ServerOptions *, char *, u_short);
Damien Miller34132e52000-01-14 15:45:46 +110031
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032/* Use of privilege separation or not */
33extern int use_privsep;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035/* Initializes the server options to their default values. */
36
Damien Miller4af51302000-04-16 11:18:38 +100037void
Damien Miller95def091999-11-25 00:26:21 +110038initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039{
Damien Miller95def091999-11-25 00:26:21 +110040 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110041
42 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100043 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110044
45 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110046 options->num_ports = 0;
47 options->ports_from_cmdline = 0;
48 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110049 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110050 options->num_host_key_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100051 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110052 options->server_key_bits = -1;
53 options->login_grace_time = -1;
54 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000055 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110056 options->ignore_rhosts = -1;
57 options->ignore_user_known_hosts = -1;
58 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000059 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110060 options->x11_forwarding = -1;
61 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110062 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100063 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110064 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110065 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110066 options->log_facility = SYSLOG_FACILITY_NOT_SET;
67 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110068 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000069 options->hostbased_authentication = -1;
70 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110071 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110072 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110073 options->kerberos_authentication = -1;
74 options->kerberos_or_local_passwd = -1;
75 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +110076 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +100077 options->gss_authentication=-1;
78 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +110079 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +110080 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +000081 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110082 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +000083 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +110084 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +000085 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +110086 options->allow_tcp_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +110087 options->num_allow_users = 0;
88 options->num_deny_users = 0;
89 options->num_allow_groups = 0;
90 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +100091 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000092 options->macs = NULL;
Damien Miller78928792000-04-12 20:17:38 +100093 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +100094 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +100095 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +100096 options->max_startups_begin = -1;
97 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +100098 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +100099 options->max_authtries = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000100 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000101 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000102 options->client_alive_interval = -1;
103 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000104 options->authorized_keys_file = NULL;
105 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000106 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100107 options->permit_tun = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109 /* Needs to be accessable in many places */
110 use_privsep = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111}
112
Damien Miller4af51302000-04-16 11:18:38 +1000113void
Damien Miller95def091999-11-25 00:26:21 +1100114fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115{
Damien Miller726273e2001-11-12 11:40:11 +1100116 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000117 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000118 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100119
120 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121 if (options->protocol == SSH_PROTO_UNKNOWN)
122 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
123 if (options->num_host_key_files == 0) {
124 /* fill default hostkeys for protocols */
125 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100126 options->host_key_files[options->num_host_key_files++] =
127 _PATH_HOST_KEY_FILE;
128 if (options->protocol & SSH_PROTO_2) {
129 options->host_key_files[options->num_host_key_files++] =
130 _PATH_HOST_RSA_KEY_FILE;
131 options->host_key_files[options->num_host_key_files++] =
132 _PATH_HOST_DSA_KEY_FILE;
133 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100134 }
Damien Miller34132e52000-01-14 15:45:46 +1100135 if (options->num_ports == 0)
136 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
137 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000138 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000139 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000140 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100141 if (options->server_key_bits == -1)
142 options->server_key_bits = 768;
143 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000144 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100145 if (options->key_regeneration_time == -1)
146 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000147 if (options->permit_root_login == PERMIT_NOT_SET)
148 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100149 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100150 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100151 if (options->ignore_user_known_hosts == -1)
152 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100153 if (options->print_motd == -1)
154 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000155 if (options->print_lastlog == -1)
156 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100157 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100158 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100159 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100160 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100161 if (options->x11_use_localhost == -1)
162 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000163 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000164 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100165 if (options->strict_modes == -1)
166 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100167 if (options->tcp_keep_alive == -1)
168 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100169 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100170 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100171 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000172 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100173 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100174 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000175 if (options->hostbased_authentication == -1)
176 options->hostbased_authentication = 0;
177 if (options->hostbased_uses_name_from_packet_only == -1)
178 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100179 if (options->rsa_authentication == -1)
180 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100181 if (options->pubkey_authentication == -1)
182 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100183 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000184 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100185 if (options->kerberos_or_local_passwd == -1)
186 options->kerberos_or_local_passwd = 1;
187 if (options->kerberos_ticket_cleanup == -1)
188 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100189 if (options->kerberos_get_afs_token == -1)
190 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000191 if (options->gss_authentication == -1)
192 options->gss_authentication = 0;
193 if (options->gss_cleanup_creds == -1)
194 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100195 if (options->password_authentication == -1)
196 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100197 if (options->kbd_interactive_authentication == -1)
198 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000199 if (options->challenge_response_authentication == -1)
200 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100201 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100202 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000203 if (options->permit_user_env == -1)
204 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100205 if (options->use_login == -1)
206 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000207 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000208 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100209 if (options->allow_tcp_forwarding == -1)
210 options->allow_tcp_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000211 if (options->gateway_ports == -1)
212 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000213 if (options->max_startups == -1)
214 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000215 if (options->max_startups_rate == -1)
216 options->max_startups_rate = 100; /* 100% */
217 if (options->max_startups_begin == -1)
218 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000219 if (options->max_authtries == -1)
220 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000221 if (options->use_dns == -1)
222 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000223 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100224 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000225 if (options->client_alive_count_max == -1)
226 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100227 if (options->authorized_keys_file2 == NULL) {
228 /* authorized_keys_file2 falls back to authorized_keys_file */
229 if (options->authorized_keys_file != NULL)
230 options->authorized_keys_file2 = options->authorized_keys_file;
231 else
232 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
233 }
234 if (options->authorized_keys_file == NULL)
235 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
Damien Millerd27b9472005-12-13 19:29:02 +1100236 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100237 options->permit_tun = SSH_TUNMODE_NO;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238
Ben Lindstromfb62a692002-06-06 19:47:11 +0000239 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000241 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000242
Tim Rice40017b02002-07-14 13:36:49 -0700243#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000244 if (use_privsep && options->compression == 1) {
245 error("This platform does not support both privilege "
246 "separation and compression");
247 error("Compression disabled");
248 options->compression = 0;
249 }
250#endif
251
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}
253
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100255typedef enum {
256 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100257 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000258 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100259 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100260 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
261 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000262 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100263 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100264 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000265 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100266 sPasswordAuthentication, sKbdInteractiveAuthentication,
267 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000268 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100269 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100270 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000271 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100272 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000273 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000274 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
275 sMaxStartups, sMaxAuthTries,
Damien Miller3a961dc2003-06-03 10:25:48 +1000276 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100277 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000278 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100279 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
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
284/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100285static struct {
286 const char *name;
287 ServerOpCodes opcode;
288} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100289 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000290#ifdef USE_PAM
Damien Miller30912f72003-08-26 10:48:14 +1000291 { "usepam", sUsePAM },
Damien Miller6ac2c482003-05-16 11:42:35 +1000292#else
Damien Miller30912f72003-08-26 10:48:14 +1000293 { "usepam", sUnsupported },
Damien Miller6ac2c482003-05-16 11:42:35 +1000294#endif
Damien Miller30912f72003-08-26 10:48:14 +1000295 { "pamauthenticationviakbdint", sDeprecated },
Damien Miller726273e2001-11-12 11:40:11 +1100296 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100297 { "port", sPort },
298 { "hostkey", sHostKeyFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100299 { "hostdsakey", sHostKeyFile }, /* alias */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000300 { "pidfile", sPidFile },
Damien Miller95def091999-11-25 00:26:21 +1100301 { "serverkeybits", sServerKeyBits },
302 { "logingracetime", sLoginGraceTime },
303 { "keyregenerationinterval", sKeyRegenerationTime },
304 { "permitrootlogin", sPermitRootLogin },
305 { "syslogfacility", sLogFacility },
306 { "loglevel", sLogLevel },
Darren Tuckerec960f22003-08-13 20:37:05 +1000307 { "rhostsauthentication", sDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100308 { "rhostsrsaauthentication", sRhostsRSAAuthentication },
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000309 { "hostbasedauthentication", sHostbasedAuthentication },
310 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly },
Damien Miller95def091999-11-25 00:26:21 +1100311 { "rsaauthentication", sRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100312 { "pubkeyauthentication", sPubkeyAuthentication },
313 { "dsaauthentication", sPubkeyAuthentication }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000314#ifdef KRB5
Damien Miller95def091999-11-25 00:26:21 +1100315 { "kerberosauthentication", sKerberosAuthentication },
316 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd },
317 { "kerberosticketcleanup", sKerberosTicketCleanup },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100318#ifdef USE_AFS
Darren Tucker22ef5082003-12-31 11:37:34 +1100319 { "kerberosgetafstoken", sKerberosGetAFSToken },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000320#else
Darren Tucker409cb322004-01-05 22:36:51 +1100321 { "kerberosgetafstoken", sUnsupported },
322#endif
323#else
Damien Millerf9b3feb2003-05-16 11:38:32 +1000324 { "kerberosauthentication", sUnsupported },
325 { "kerberosorlocalpasswd", sUnsupported },
326 { "kerberosticketcleanup", sUnsupported },
Darren Tucker22ef5082003-12-31 11:37:34 +1100327 { "kerberosgetafstoken", sUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000328#endif
Damien Miller1a0c0b92003-09-02 22:51:17 +1000329 { "kerberostgtpassing", sUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000330 { "afstokenpassing", sUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000331#ifdef GSSAPI
332 { "gssapiauthentication", sGssAuthentication },
Darren Tuckera49d36e2003-10-02 16:20:54 +1000333 { "gssapicleanupcredentials", sGssCleanupCreds },
Darren Tucker0efd1552003-08-26 11:49:55 +1000334#else
335 { "gssapiauthentication", sUnsupported },
Darren Tuckera49d36e2003-10-02 16:20:54 +1000336 { "gssapicleanupcredentials", sUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000337#endif
Damien Miller95def091999-11-25 00:26:21 +1100338 { "passwordauthentication", sPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100339 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000340 { "challengeresponseauthentication", sChallengeResponseAuthentication },
341 { "skeyauthentication", sChallengeResponseAuthentication }, /* alias */
Ben Lindstrom91e98682001-09-12 16:32:14 +0000342 { "checkmail", sDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100343 { "listenaddress", sListenAddress },
Darren Tucker0f383232005-01-20 10:57:56 +1100344 { "addressfamily", sAddressFamily },
Damien Miller95def091999-11-25 00:26:21 +1100345 { "printmotd", sPrintMotd },
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000346 { "printlastlog", sPrintLastLog },
Damien Miller95def091999-11-25 00:26:21 +1100347 { "ignorerhosts", sIgnoreRhosts },
348 { "ignoreuserknownhosts", sIgnoreUserKnownHosts },
349 { "x11forwarding", sX11Forwarding },
350 { "x11displayoffset", sX11DisplayOffset },
Damien Miller95c249f2002-02-05 12:11:34 +1100351 { "x11uselocalhost", sX11UseLocalhost },
Damien Millerd3a18572000-06-07 19:55:44 +1000352 { "xauthlocation", sXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100353 { "strictmodes", sStrictModes },
354 { "permitemptypasswords", sEmptyPasswd },
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000355 { "permituserenvironment", sPermitUserEnvironment },
Damien Miller95def091999-11-25 00:26:21 +1100356 { "uselogin", sUseLogin },
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000357 { "compression", sCompression },
Damien Miller12c150e2003-12-17 16:31:10 +1100358 { "tcpkeepalive", sTCPKeepAlive },
359 { "keepalive", sTCPKeepAlive }, /* obsolete alias */
Damien Miller50a41ed2000-10-16 12:14:42 +1100360 { "allowtcpforwarding", sAllowTcpForwarding },
Damien Miller95def091999-11-25 00:26:21 +1100361 { "allowusers", sAllowUsers },
362 { "denyusers", sDenyUsers },
363 { "allowgroups", sAllowGroups },
364 { "denygroups", sDenyGroups },
Damien Miller78928792000-04-12 20:17:38 +1000365 { "ciphers", sCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000366 { "macs", sMacs },
Damien Miller78928792000-04-12 20:17:38 +1000367 { "protocol", sProtocol },
Damien Millere247cc42000-05-07 12:03:14 +1000368 { "gatewayports", sGatewayPorts },
Damien Millerf6d9e222000-06-18 14:50:44 +1000369 { "subsystem", sSubsystem },
Damien Miller37023962000-07-11 17:31:38 +1000370 { "maxstartups", sMaxStartups },
Darren Tucker89413db2004-05-24 10:36:23 +1000371 { "maxauthtries", sMaxAuthTries },
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000372 { "banner", sBanner },
Damien Miller3a961dc2003-06-03 10:25:48 +1000373 { "usedns", sUseDNS },
374 { "verifyreversemapping", sDeprecated },
375 { "reversemappingcheck", sDeprecated },
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000376 { "clientaliveinterval", sClientAliveInterval },
377 { "clientalivecountmax", sClientAliveCountMax },
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000378 { "authorizedkeysfile", sAuthorizedKeysFile },
379 { "authorizedkeysfile2", sAuthorizedKeysFile2 },
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380 { "useprivilegeseparation", sUsePrivilegeSeparation},
Darren Tucker46bc0752004-05-02 22:11:30 +1000381 { "acceptenv", sAcceptEnv },
Damien Millerd27b9472005-12-13 19:29:02 +1100382 { "permittunnel", sPermitTunnel },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000383 { NULL, sBadOption }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384};
385
Damien Miller5428f641999-11-25 11:54:57 +1100386/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000387 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100388 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389
Damien Miller4af51302000-04-16 11:18:38 +1000390static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100391parse_token(const char *cp, const char *filename,
392 int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000394 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100397 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100398 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000400 error("%s: line %d: Bad configuration option: %s",
401 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100402 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403}
404
Ben Lindstrombba81212001-06-25 05:01:22 +0000405static void
Ben Lindstrom19066a12001-04-12 23:39:26 +0000406add_listen_addr(ServerOptions *options, char *addr, u_short port)
Damien Miller34132e52000-01-14 15:45:46 +1100407{
Damien Millereccb9de2005-06-17 12:59:34 +1000408 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100409
410 if (options->num_ports == 0)
411 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100412 if (options->address_family == -1)
413 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000414 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000415 for (i = 0; i < options->num_ports; i++)
416 add_one_listen_addr(options, addr, options->ports[i]);
417 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000418 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000419}
420
Ben Lindstrombba81212001-06-25 05:01:22 +0000421static void
Ben Lindstromc510af42001-04-07 17:25:48 +0000422add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
423{
424 struct addrinfo hints, *ai, *aitop;
425 char strport[NI_MAXSERV];
426 int gaierr;
427
428 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100429 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000430 hints.ai_socktype = SOCK_STREAM;
431 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Ben Lindstrome1353632002-06-23 21:29:23 +0000432 snprintf(strport, sizeof strport, "%u", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000433 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
434 fatal("bad addr or host: %s (%s)",
435 addr ? addr : "<NULL>",
436 gai_strerror(gaierr));
437 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
438 ;
439 ai->ai_next = options->listen_addrs;
440 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100441}
442
Ben Lindstromade03f62001-12-06 18:22:17 +0000443int
444process_server_config_line(ServerOptions *options, char *line,
445 const char *filename, int linenum)
446{
447 char *cp, **charptr, *arg, *p;
Damien Millereccb9de2005-06-17 12:59:34 +1000448 int *intptr, value, n;
Ben Lindstromade03f62001-12-06 18:22:17 +0000449 ServerOpCodes opcode;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100450 u_short port;
Damien Millereccb9de2005-06-17 12:59:34 +1000451 u_int i;
Damien Miller917f9b62006-07-10 20:36:47 +1000452 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000453
454 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100455 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100456 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000457 /* Ignore leading whitespace */
458 if (*arg == '\0')
459 arg = strdelim(&cp);
460 if (!arg || !*arg || *arg == '#')
461 return 0;
462 intptr = NULL;
463 charptr = NULL;
464 opcode = parse_token(arg, filename, linenum);
465 switch (opcode) {
466 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000467 case sUsePAM:
468 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000469 goto parse_flag;
470
471 /* Standard Options */
472 case sBadOption:
473 return -1;
474 case sPort:
475 /* ignore ports from configfile if cmdline specifies ports */
476 if (options->ports_from_cmdline)
477 return 0;
478 if (options->listen_addrs != NULL)
479 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100480 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000481 if (options->num_ports >= MAX_PORTS)
482 fatal("%s line %d: too many ports.",
483 filename, linenum);
484 arg = strdelim(&cp);
485 if (!arg || *arg == '\0')
486 fatal("%s line %d: missing port number.",
487 filename, linenum);
488 options->ports[options->num_ports++] = a2port(arg);
489 if (options->ports[options->num_ports-1] == 0)
490 fatal("%s line %d: Badly formatted port number.",
491 filename, linenum);
492 break;
493
494 case sServerKeyBits:
495 intptr = &options->server_key_bits;
496parse_int:
497 arg = strdelim(&cp);
498 if (!arg || *arg == '\0')
499 fatal("%s line %d: missing integer value.",
500 filename, linenum);
501 value = atoi(arg);
502 if (*intptr == -1)
503 *intptr = value;
504 break;
505
506 case sLoginGraceTime:
507 intptr = &options->login_grace_time;
508parse_time:
509 arg = strdelim(&cp);
510 if (!arg || *arg == '\0')
511 fatal("%s line %d: missing time value.",
512 filename, linenum);
513 if ((value = convtime(arg)) == -1)
514 fatal("%s line %d: invalid time value.",
515 filename, linenum);
516 if (*intptr == -1)
517 *intptr = value;
518 break;
519
520 case sKeyRegenerationTime:
521 intptr = &options->key_regeneration_time;
522 goto parse_time;
523
524 case sListenAddress:
525 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100526 if (arg == NULL || *arg == '\0')
527 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000528 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000529 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
530 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
531 && strchr(p+1, ':') != NULL) {
532 add_listen_addr(options, arg, 0);
533 break;
534 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100535 p = hpdelim(&arg);
536 if (p == NULL)
537 fatal("%s line %d: bad address:port usage",
538 filename, linenum);
539 p = cleanhostname(p);
540 if (arg == NULL)
541 port = 0;
542 else if ((port = a2port(arg)) == 0)
543 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000544
Damien Millerf91ee4c2005-03-01 21:24:33 +1100545 add_listen_addr(options, p, port);
546
Ben Lindstromade03f62001-12-06 18:22:17 +0000547 break;
548
Darren Tucker0f383232005-01-20 10:57:56 +1100549 case sAddressFamily:
550 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000551 if (!arg || *arg == '\0')
552 fatal("%s line %d: missing address family.",
553 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100554 intptr = &options->address_family;
555 if (options->listen_addrs != NULL)
556 fatal("%s line %d: address family must be specified before "
557 "ListenAddress.", filename, linenum);
558 if (strcasecmp(arg, "inet") == 0)
559 value = AF_INET;
560 else if (strcasecmp(arg, "inet6") == 0)
561 value = AF_INET6;
562 else if (strcasecmp(arg, "any") == 0)
563 value = AF_UNSPEC;
564 else
565 fatal("%s line %d: unsupported address family \"%s\".",
566 filename, linenum, arg);
567 if (*intptr == -1)
568 *intptr = value;
569 break;
570
Ben Lindstromade03f62001-12-06 18:22:17 +0000571 case sHostKeyFile:
572 intptr = &options->num_host_key_files;
573 if (*intptr >= MAX_HOSTKEYS)
574 fatal("%s line %d: too many host keys specified (max %d).",
575 filename, linenum, MAX_HOSTKEYS);
576 charptr = &options->host_key_files[*intptr];
577parse_filename:
578 arg = strdelim(&cp);
579 if (!arg || *arg == '\0')
580 fatal("%s line %d: missing file name.",
581 filename, linenum);
582 if (*charptr == NULL) {
583 *charptr = tilde_expand_filename(arg, getuid());
584 /* increase optional counter */
585 if (intptr != NULL)
586 *intptr = *intptr + 1;
587 }
588 break;
589
590 case sPidFile:
591 charptr = &options->pid_file;
592 goto parse_filename;
593
594 case sPermitRootLogin:
595 intptr = &options->permit_root_login;
596 arg = strdelim(&cp);
597 if (!arg || *arg == '\0')
598 fatal("%s line %d: missing yes/"
599 "without-password/forced-commands-only/no "
600 "argument.", filename, linenum);
601 value = 0; /* silence compiler */
602 if (strcmp(arg, "without-password") == 0)
603 value = PERMIT_NO_PASSWD;
604 else if (strcmp(arg, "forced-commands-only") == 0)
605 value = PERMIT_FORCED_ONLY;
606 else if (strcmp(arg, "yes") == 0)
607 value = PERMIT_YES;
608 else if (strcmp(arg, "no") == 0)
609 value = PERMIT_NO;
610 else
611 fatal("%s line %d: Bad yes/"
612 "without-password/forced-commands-only/no "
613 "argument: %s", filename, linenum, arg);
614 if (*intptr == -1)
615 *intptr = value;
616 break;
617
618 case sIgnoreRhosts:
619 intptr = &options->ignore_rhosts;
620parse_flag:
621 arg = strdelim(&cp);
622 if (!arg || *arg == '\0')
623 fatal("%s line %d: missing yes/no argument.",
624 filename, linenum);
625 value = 0; /* silence compiler */
626 if (strcmp(arg, "yes") == 0)
627 value = 1;
628 else if (strcmp(arg, "no") == 0)
629 value = 0;
630 else
631 fatal("%s line %d: Bad yes/no argument: %s",
632 filename, linenum, arg);
633 if (*intptr == -1)
634 *intptr = value;
635 break;
636
637 case sIgnoreUserKnownHosts:
638 intptr = &options->ignore_user_known_hosts;
639 goto parse_flag;
640
Ben Lindstromade03f62001-12-06 18:22:17 +0000641 case sRhostsRSAAuthentication:
642 intptr = &options->rhosts_rsa_authentication;
643 goto parse_flag;
644
645 case sHostbasedAuthentication:
646 intptr = &options->hostbased_authentication;
647 goto parse_flag;
648
649 case sHostbasedUsesNameFromPacketOnly:
650 intptr = &options->hostbased_uses_name_from_packet_only;
651 goto parse_flag;
652
653 case sRSAAuthentication:
654 intptr = &options->rsa_authentication;
655 goto parse_flag;
656
657 case sPubkeyAuthentication:
658 intptr = &options->pubkey_authentication;
659 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000660
Ben Lindstromade03f62001-12-06 18:22:17 +0000661 case sKerberosAuthentication:
662 intptr = &options->kerberos_authentication;
663 goto parse_flag;
664
665 case sKerberosOrLocalPasswd:
666 intptr = &options->kerberos_or_local_passwd;
667 goto parse_flag;
668
669 case sKerberosTicketCleanup:
670 intptr = &options->kerberos_ticket_cleanup;
671 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000672
Darren Tucker22ef5082003-12-31 11:37:34 +1100673 case sKerberosGetAFSToken:
674 intptr = &options->kerberos_get_afs_token;
675 goto parse_flag;
676
Darren Tucker0efd1552003-08-26 11:49:55 +1000677 case sGssAuthentication:
678 intptr = &options->gss_authentication;
679 goto parse_flag;
680
681 case sGssCleanupCreds:
682 intptr = &options->gss_cleanup_creds;
683 goto parse_flag;
684
Ben Lindstromade03f62001-12-06 18:22:17 +0000685 case sPasswordAuthentication:
686 intptr = &options->password_authentication;
687 goto parse_flag;
688
689 case sKbdInteractiveAuthentication:
690 intptr = &options->kbd_interactive_authentication;
691 goto parse_flag;
692
693 case sChallengeResponseAuthentication:
694 intptr = &options->challenge_response_authentication;
695 goto parse_flag;
696
697 case sPrintMotd:
698 intptr = &options->print_motd;
699 goto parse_flag;
700
701 case sPrintLastLog:
702 intptr = &options->print_lastlog;
703 goto parse_flag;
704
705 case sX11Forwarding:
706 intptr = &options->x11_forwarding;
707 goto parse_flag;
708
709 case sX11DisplayOffset:
710 intptr = &options->x11_display_offset;
711 goto parse_int;
712
Damien Miller95c249f2002-02-05 12:11:34 +1100713 case sX11UseLocalhost:
714 intptr = &options->x11_use_localhost;
715 goto parse_flag;
716
Ben Lindstromade03f62001-12-06 18:22:17 +0000717 case sXAuthLocation:
718 charptr = &options->xauth_location;
719 goto parse_filename;
720
721 case sStrictModes:
722 intptr = &options->strict_modes;
723 goto parse_flag;
724
Damien Miller12c150e2003-12-17 16:31:10 +1100725 case sTCPKeepAlive:
726 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000727 goto parse_flag;
728
729 case sEmptyPasswd:
730 intptr = &options->permit_empty_passwd;
731 goto parse_flag;
732
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000733 case sPermitUserEnvironment:
734 intptr = &options->permit_user_env;
735 goto parse_flag;
736
Ben Lindstromade03f62001-12-06 18:22:17 +0000737 case sUseLogin:
738 intptr = &options->use_login;
739 goto parse_flag;
740
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000741 case sCompression:
742 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +1000743 arg = strdelim(&cp);
744 if (!arg || *arg == '\0')
745 fatal("%s line %d: missing yes/no/delayed "
746 "argument.", filename, linenum);
747 value = 0; /* silence compiler */
748 if (strcmp(arg, "delayed") == 0)
749 value = COMP_DELAYED;
750 else if (strcmp(arg, "yes") == 0)
751 value = COMP_ZLIB;
752 else if (strcmp(arg, "no") == 0)
753 value = COMP_NONE;
754 else
755 fatal("%s line %d: Bad yes/no/delayed "
756 "argument: %s", filename, linenum, arg);
757 if (*intptr == -1)
758 *intptr = value;
759 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000760
Ben Lindstromade03f62001-12-06 18:22:17 +0000761 case sGatewayPorts:
762 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100763 arg = strdelim(&cp);
764 if (!arg || *arg == '\0')
765 fatal("%s line %d: missing yes/no/clientspecified "
766 "argument.", filename, linenum);
767 value = 0; /* silence compiler */
768 if (strcmp(arg, "clientspecified") == 0)
769 value = 2;
770 else if (strcmp(arg, "yes") == 0)
771 value = 1;
772 else if (strcmp(arg, "no") == 0)
773 value = 0;
774 else
775 fatal("%s line %d: Bad yes/no/clientspecified "
776 "argument: %s", filename, linenum, arg);
777 if (*intptr == -1)
778 *intptr = value;
779 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000780
Damien Miller3a961dc2003-06-03 10:25:48 +1000781 case sUseDNS:
782 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +0000783 goto parse_flag;
784
785 case sLogFacility:
786 intptr = (int *) &options->log_facility;
787 arg = strdelim(&cp);
788 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100789 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000790 fatal("%.200s line %d: unsupported log facility '%s'",
791 filename, linenum, arg ? arg : "<NONE>");
792 if (*intptr == -1)
793 *intptr = (SyslogFacility) value;
794 break;
795
796 case sLogLevel:
797 intptr = (int *) &options->log_level;
798 arg = strdelim(&cp);
799 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100800 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000801 fatal("%.200s line %d: unsupported log level '%s'",
802 filename, linenum, arg ? arg : "<NONE>");
803 if (*intptr == -1)
804 *intptr = (LogLevel) value;
805 break;
806
807 case sAllowTcpForwarding:
808 intptr = &options->allow_tcp_forwarding;
809 goto parse_flag;
810
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000811 case sUsePrivilegeSeparation:
812 intptr = &use_privsep;
813 goto parse_flag;
814
Ben Lindstromade03f62001-12-06 18:22:17 +0000815 case sAllowUsers:
816 while ((arg = strdelim(&cp)) && *arg != '\0') {
817 if (options->num_allow_users >= MAX_ALLOW_USERS)
818 fatal("%s line %d: too many allow users.",
819 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000820 options->allow_users[options->num_allow_users++] =
821 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000822 }
823 break;
824
825 case sDenyUsers:
826 while ((arg = strdelim(&cp)) && *arg != '\0') {
827 if (options->num_deny_users >= MAX_DENY_USERS)
828 fatal( "%s line %d: too many deny users.",
829 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000830 options->deny_users[options->num_deny_users++] =
831 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000832 }
833 break;
834
835 case sAllowGroups:
836 while ((arg = strdelim(&cp)) && *arg != '\0') {
837 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
838 fatal("%s line %d: too many allow groups.",
839 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +0000840 options->allow_groups[options->num_allow_groups++] =
841 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000842 }
843 break;
844
845 case sDenyGroups:
846 while ((arg = strdelim(&cp)) && *arg != '\0') {
847 if (options->num_deny_groups >= MAX_DENY_GROUPS)
848 fatal("%s line %d: too many deny groups.",
849 filename, linenum);
850 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
851 }
852 break;
853
854 case sCiphers:
855 arg = strdelim(&cp);
856 if (!arg || *arg == '\0')
857 fatal("%s line %d: Missing argument.", filename, linenum);
858 if (!ciphers_valid(arg))
859 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
860 filename, linenum, arg ? arg : "<NONE>");
861 if (options->ciphers == NULL)
862 options->ciphers = xstrdup(arg);
863 break;
864
865 case sMacs:
866 arg = strdelim(&cp);
867 if (!arg || *arg == '\0')
868 fatal("%s line %d: Missing argument.", filename, linenum);
869 if (!mac_valid(arg))
870 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
871 filename, linenum, arg ? arg : "<NONE>");
872 if (options->macs == NULL)
873 options->macs = xstrdup(arg);
874 break;
875
876 case sProtocol:
877 intptr = &options->protocol;
878 arg = strdelim(&cp);
879 if (!arg || *arg == '\0')
880 fatal("%s line %d: Missing argument.", filename, linenum);
881 value = proto_spec(arg);
882 if (value == SSH_PROTO_UNKNOWN)
883 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100884 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +0000885 if (*intptr == SSH_PROTO_UNKNOWN)
886 *intptr = value;
887 break;
888
889 case sSubsystem:
890 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
891 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100892 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000893 }
894 arg = strdelim(&cp);
895 if (!arg || *arg == '\0')
896 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100897 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000898 for (i = 0; i < options->num_subsystems; i++)
899 if (strcmp(arg, options->subsystem_name[i]) == 0)
900 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100901 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000902 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
903 arg = strdelim(&cp);
904 if (!arg || *arg == '\0')
905 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100906 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000907 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +1000908
909 /* Collect arguments (separate to executable) */
910 p = xstrdup(arg);
911 len = strlen(p) + 1;
912 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
913 len += 1 + strlen(arg);
914 p = xrealloc(p, 1, len);
915 strlcat(p, " ", len);
916 strlcat(p, arg, len);
917 }
918 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +0000919 options->num_subsystems++;
920 break;
921
922 case sMaxStartups:
923 arg = strdelim(&cp);
924 if (!arg || *arg == '\0')
925 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100926 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000927 if ((n = sscanf(arg, "%d:%d:%d",
928 &options->max_startups_begin,
929 &options->max_startups_rate,
930 &options->max_startups)) == 3) {
931 if (options->max_startups_begin >
932 options->max_startups ||
933 options->max_startups_rate > 100 ||
934 options->max_startups_rate < 1)
935 fatal("%s line %d: Illegal MaxStartups spec.",
936 filename, linenum);
937 } else if (n != 1)
938 fatal("%s line %d: Illegal MaxStartups spec.",
939 filename, linenum);
940 else
941 options->max_startups = options->max_startups_begin;
942 break;
943
Darren Tucker89413db2004-05-24 10:36:23 +1000944 case sMaxAuthTries:
945 intptr = &options->max_authtries;
946 goto parse_int;
947
Ben Lindstromade03f62001-12-06 18:22:17 +0000948 case sBanner:
949 charptr = &options->banner;
950 goto parse_filename;
951 /*
952 * These options can contain %X options expanded at
953 * connect time, so that you can specify paths like:
954 *
955 * AuthorizedKeysFile /etc/ssh_keys/%u
956 */
957 case sAuthorizedKeysFile:
958 case sAuthorizedKeysFile2:
959 charptr = (opcode == sAuthorizedKeysFile ) ?
960 &options->authorized_keys_file :
961 &options->authorized_keys_file2;
962 goto parse_filename;
963
964 case sClientAliveInterval:
965 intptr = &options->client_alive_interval;
966 goto parse_time;
967
968 case sClientAliveCountMax:
969 intptr = &options->client_alive_count_max;
970 goto parse_int;
971
Darren Tucker46bc0752004-05-02 22:11:30 +1000972 case sAcceptEnv:
973 while ((arg = strdelim(&cp)) && *arg != '\0') {
974 if (strchr(arg, '=') != NULL)
975 fatal("%s line %d: Invalid environment name.",
976 filename, linenum);
977 if (options->num_accept_env >= MAX_ACCEPT_ENV)
978 fatal("%s line %d: too many allow env.",
979 filename, linenum);
980 options->accept_env[options->num_accept_env++] =
981 xstrdup(arg);
982 }
983 break;
984
Damien Millerd27b9472005-12-13 19:29:02 +1100985 case sPermitTunnel:
986 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +1100987 arg = strdelim(&cp);
988 if (!arg || *arg == '\0')
989 fatal("%s line %d: Missing yes/point-to-point/"
990 "ethernet/no argument.", filename, linenum);
991 value = 0; /* silence compiler */
992 if (strcasecmp(arg, "ethernet") == 0)
993 value = SSH_TUNMODE_ETHERNET;
994 else if (strcasecmp(arg, "point-to-point") == 0)
995 value = SSH_TUNMODE_POINTOPOINT;
996 else if (strcasecmp(arg, "yes") == 0)
997 value = SSH_TUNMODE_YES;
998 else if (strcasecmp(arg, "no") == 0)
999 value = SSH_TUNMODE_NO;
1000 else
1001 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1002 "no argument: %s", filename, linenum, arg);
1003 if (*intptr == -1)
1004 *intptr = value;
1005 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001006
Ben Lindstromade03f62001-12-06 18:22:17 +00001007 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001008 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001009 filename, linenum, arg);
1010 while (arg)
1011 arg = strdelim(&cp);
1012 break;
1013
Damien Millerf9b3feb2003-05-16 11:38:32 +10001014 case sUnsupported:
1015 logit("%s line %d: Unsupported option %s",
1016 filename, linenum, arg);
1017 while (arg)
1018 arg = strdelim(&cp);
1019 break;
1020
Ben Lindstromade03f62001-12-06 18:22:17 +00001021 default:
1022 fatal("%s line %d: Missing handler for opcode %s (%d)",
1023 filename, linenum, arg, opcode);
1024 }
1025 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1026 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1027 filename, linenum, arg);
1028 return 0;
1029}
1030
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031/* Reads the server configuration file. */
1032
Damien Miller4af51302000-04-16 11:18:38 +10001033void
Darren Tucker645ab752004-06-25 13:33:20 +10001034load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035{
Darren Tucker645ab752004-06-25 13:33:20 +10001036 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001037 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038
Darren Tucker645ab752004-06-25 13:33:20 +10001039 debug2("%s: filename %s", __func__, filename);
1040 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001041 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001042 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001043 }
Darren Tucker645ab752004-06-25 13:33:20 +10001044 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001045 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001046 /*
1047 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001048 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001049 * line numbers later for error messages
1050 */
1051 if ((cp = strchr(line, '#')) != NULL)
1052 memcpy(cp, "\n", 2);
1053 cp = line + strspn(line, " \t\r");
1054
1055 buffer_append(conf, cp, strlen(cp));
1056 }
1057 buffer_append(conf, "\0", 1);
1058 fclose(f);
1059 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1060}
1061
1062void
1063parse_server_config(ServerOptions *options, const char *filename, Buffer *conf)
1064{
1065 int linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001066 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001067
1068 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1069
Darren Tucker9fbac712004-08-12 22:41:44 +10001070 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker137e9c92004-08-13 21:30:24 +10001071 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001072 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001073 if (process_server_config_line(options, cp, filename,
1074 linenum++) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001075 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001077 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001078 if (bad_options > 0)
1079 fatal("%s: terminating, %d bad configuration options",
1080 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081}