blob: 0cb744a1d833e70186eb7d292bddcb82867f2037 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Millere4340be2000-09-16 13:29:08 +11005 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110010 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100011
12#include "includes.h"
Damien Miller4fbf08a2002-01-22 23:35:09 +110013RCSID("$OpenBSD: servconf.c,v 1.98 2002/01/22 02:52:41 stevesk Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000014
Damien Millerc4b7fea2001-07-14 12:20:32 +100015#if defined(KRB4) || defined(KRB5)
Ben Lindstrom226cfa02001-01-22 05:34:40 +000016#include <krb.h>
17#endif
Ben Lindstromeb7a84c2001-07-04 04:48:36 +000018#ifdef AFS
19#include <kafs.h>
20#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
22#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "servconf.h"
25#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100026#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
28#include "tildexpand.h"
29#include "misc.h"
30#include "cipher.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000031#include "kex.h"
32#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033
Ben Lindstrombba81212001-06-25 05:01:22 +000034static void add_listen_addr(ServerOptions *, char *, u_short);
35static void add_one_listen_addr(ServerOptions *, char *, u_short);
Damien Miller34132e52000-01-14 15:45:46 +110036
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037/* AF_UNSPEC or AF_INET or AF_INET6 */
38extern int IPv4or6;
39
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040/* Initializes the server options to their default values. */
41
Damien Miller4af51302000-04-16 11:18:38 +100042void
Damien Miller95def091999-11-25 00:26:21 +110043initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044{
Damien Miller95def091999-11-25 00:26:21 +110045 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110046
47 /* Portable-specific options */
48 options->pam_authentication_via_kbd_int = -1;
49
50 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110051 options->num_ports = 0;
52 options->ports_from_cmdline = 0;
53 options->listen_addrs = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +110054 options->num_host_key_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100055 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110056 options->server_key_bits = -1;
57 options->login_grace_time = -1;
58 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000059 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110060 options->ignore_rhosts = -1;
61 options->ignore_user_known_hosts = -1;
62 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000063 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110064 options->x11_forwarding = -1;
65 options->x11_display_offset = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100066 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110067 options->strict_modes = -1;
68 options->keepalives = -1;
69 options->log_facility = (SyslogFacility) - 1;
70 options->log_level = (LogLevel) - 1;
71 options->rhosts_authentication = -1;
72 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000073 options->hostbased_authentication = -1;
74 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110075 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110076 options->pubkey_authentication = -1;
Ben Lindstromec95ed92001-07-04 04:21:14 +000077#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +110078 options->kerberos_authentication = -1;
79 options->kerberos_or_local_passwd = -1;
80 options->kerberos_ticket_cleanup = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +000082#if defined(AFS) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +110083 options->kerberos_tgt_passing = -1;
Ben Lindstromec95ed92001-07-04 04:21:14 +000084#endif
85#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +110086 options->afs_token_passing = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087#endif
Damien Miller95def091999-11-25 00:26:21 +110088 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +110089 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +000090 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110091 options->permit_empty_passwd = -1;
92 options->use_login = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +110093 options->allow_tcp_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +110094 options->num_allow_users = 0;
95 options->num_deny_users = 0;
96 options->num_allow_groups = 0;
97 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +100098 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000099 options->macs = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000100 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000101 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000102 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000103 options->max_startups_begin = -1;
104 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000105 options->max_startups = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000106 options->banner = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100107 options->reverse_mapping_check = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000108 options->client_alive_interval = -1;
109 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000110 options->authorized_keys_file = NULL;
111 options->authorized_keys_file2 = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112}
113
Damien Miller4af51302000-04-16 11:18:38 +1000114void
Damien Miller95def091999-11-25 00:26:21 +1100115fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116{
Damien Miller726273e2001-11-12 11:40:11 +1100117 /* Portable-specific options */
118 if (options->pam_authentication_via_kbd_int == -1)
119 options->pam_authentication_via_kbd_int = 0;
120
121 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 if (options->protocol == SSH_PROTO_UNKNOWN)
123 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
124 if (options->num_host_key_files == 0) {
125 /* fill default hostkeys for protocols */
126 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100127 options->host_key_files[options->num_host_key_files++] =
128 _PATH_HOST_KEY_FILE;
129 if (options->protocol & SSH_PROTO_2) {
130 options->host_key_files[options->num_host_key_files++] =
131 _PATH_HOST_RSA_KEY_FILE;
132 options->host_key_files[options->num_host_key_files++] =
133 _PATH_HOST_DSA_KEY_FILE;
134 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100135 }
Damien Miller34132e52000-01-14 15:45:46 +1100136 if (options->num_ports == 0)
137 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
138 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000139 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000140 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000141 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100142 if (options->server_key_bits == -1)
143 options->server_key_bits = 768;
144 if (options->login_grace_time == -1)
145 options->login_grace_time = 600;
146 if (options->key_regeneration_time == -1)
147 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000148 if (options->permit_root_login == PERMIT_NOT_SET)
149 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100150 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100151 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100152 if (options->ignore_user_known_hosts == -1)
153 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100154 if (options->print_motd == -1)
155 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000156 if (options->print_lastlog == -1)
157 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100158 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100159 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100160 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100161 options->x11_display_offset = 10;
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;
166 if (options->keepalives == -1)
167 options->keepalives = 1;
168 if (options->log_facility == (SyslogFacility) (-1))
169 options->log_facility = SYSLOG_FACILITY_AUTH;
170 if (options->log_level == (LogLevel) (-1))
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_authentication == -1)
173 options->rhosts_authentication = 0;
174 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100175 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000176 if (options->hostbased_authentication == -1)
177 options->hostbased_authentication = 0;
178 if (options->hostbased_uses_name_from_packet_only == -1)
179 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100180 if (options->rsa_authentication == -1)
181 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 if (options->pubkey_authentication == -1)
183 options->pubkey_authentication = 1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000184#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100185 if (options->kerberos_authentication == -1)
186 options->kerberos_authentication = (access(KEYFILE, R_OK) == 0);
187 if (options->kerberos_or_local_passwd == -1)
188 options->kerberos_or_local_passwd = 1;
189 if (options->kerberos_ticket_cleanup == -1)
190 options->kerberos_ticket_cleanup = 1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000191#endif
192#if defined(AFS) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100193 if (options->kerberos_tgt_passing == -1)
194 options->kerberos_tgt_passing = 0;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000195#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100196#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100197 if (options->afs_token_passing == -1)
198 options->afs_token_passing = k_hasafs();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000199#endif
Damien Miller95def091999-11-25 00:26:21 +1100200 if (options->password_authentication == -1)
201 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100202 if (options->kbd_interactive_authentication == -1)
203 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000204 if (options->challenge_response_authentication == -1)
205 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100206 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100207 options->permit_empty_passwd = 0;
Damien Miller95def091999-11-25 00:26:21 +1100208 if (options->use_login == -1)
209 options->use_login = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100210 if (options->allow_tcp_forwarding == -1)
211 options->allow_tcp_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000212 if (options->gateway_ports == -1)
213 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000214 if (options->max_startups == -1)
215 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000216 if (options->max_startups_rate == -1)
217 options->max_startups_rate = 100; /* 100% */
218 if (options->max_startups_begin == -1)
219 options->max_startups_begin = options->max_startups;
Damien Miller33804262001-02-04 23:20:18 +1100220 if (options->reverse_mapping_check == -1)
221 options->reverse_mapping_check = 0;
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 Millerd4a8b7e1999-10-27 13:42:43 +1000235}
236
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100238typedef enum {
239 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100240 /* Portable-specific options */
241 sPAMAuthenticationViaKbdInt,
242 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100243 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
244 sPermitRootLogin, sLogFacility, sLogLevel,
245 sRhostsAuthentication, sRhostsRSAAuthentication, sRSAAuthentication,
Ben Lindstromec95ed92001-07-04 04:21:14 +0000246#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100247 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000249#if defined(AFS) || defined(KRB5)
250 sKerberosTgtPassing,
251#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252#ifdef AFS
Ben Lindstromec95ed92001-07-04 04:21:14 +0000253 sAFSTokenPassing,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254#endif
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000255 sChallengeResponseAuthentication,
Damien Miller874d77b2000-10-14 16:23:11 +1100256 sPasswordAuthentication, sKbdInteractiveAuthentication, sListenAddress,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000257 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
258 sX11Forwarding, sX11DisplayOffset,
Ben Lindstrom91e98682001-09-12 16:32:14 +0000259 sStrictModes, sEmptyPasswd, sKeepAlives,
Damien Miller50a41ed2000-10-16 12:14:42 +1100260 sUseLogin, sAllowTcpForwarding,
261 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000262 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100263 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem, sMaxStartups,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 sBanner, sReverseMappingCheck, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100265 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000266 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Ben Lindstromade03f62001-12-06 18:22:17 +0000267 sDeprecated
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268} ServerOpCodes;
269
270/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100271static struct {
272 const char *name;
273 ServerOpCodes opcode;
274} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100275 /* Portable-specific options */
276 { "PAMAuthenticationViaKbdInt", sPAMAuthenticationViaKbdInt },
277 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100278 { "port", sPort },
279 { "hostkey", sHostKeyFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100280 { "hostdsakey", sHostKeyFile }, /* alias */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000281 { "pidfile", sPidFile },
Damien Miller95def091999-11-25 00:26:21 +1100282 { "serverkeybits", sServerKeyBits },
283 { "logingracetime", sLoginGraceTime },
284 { "keyregenerationinterval", sKeyRegenerationTime },
285 { "permitrootlogin", sPermitRootLogin },
286 { "syslogfacility", sLogFacility },
287 { "loglevel", sLogLevel },
288 { "rhostsauthentication", sRhostsAuthentication },
289 { "rhostsrsaauthentication", sRhostsRSAAuthentication },
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000290 { "hostbasedauthentication", sHostbasedAuthentication },
291 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly },
Damien Miller95def091999-11-25 00:26:21 +1100292 { "rsaauthentication", sRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100293 { "pubkeyauthentication", sPubkeyAuthentication },
294 { "dsaauthentication", sPubkeyAuthentication }, /* alias */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000295#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100296 { "kerberosauthentication", sKerberosAuthentication },
297 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd },
298 { "kerberosticketcleanup", sKerberosTicketCleanup },
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000300#if defined(AFS) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100301 { "kerberostgtpassing", sKerberosTgtPassing },
Ben Lindstromec95ed92001-07-04 04:21:14 +0000302#endif
303#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100304 { "afstokenpassing", sAFSTokenPassing },
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305#endif
Damien Miller95def091999-11-25 00:26:21 +1100306 { "passwordauthentication", sPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100307 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000308 { "challengeresponseauthentication", sChallengeResponseAuthentication },
309 { "skeyauthentication", sChallengeResponseAuthentication }, /* alias */
Ben Lindstrom91e98682001-09-12 16:32:14 +0000310 { "checkmail", sDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100311 { "listenaddress", sListenAddress },
312 { "printmotd", sPrintMotd },
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000313 { "printlastlog", sPrintLastLog },
Damien Miller95def091999-11-25 00:26:21 +1100314 { "ignorerhosts", sIgnoreRhosts },
315 { "ignoreuserknownhosts", sIgnoreUserKnownHosts },
316 { "x11forwarding", sX11Forwarding },
317 { "x11displayoffset", sX11DisplayOffset },
Damien Millerd3a18572000-06-07 19:55:44 +1000318 { "xauthlocation", sXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100319 { "strictmodes", sStrictModes },
320 { "permitemptypasswords", sEmptyPasswd },
321 { "uselogin", sUseLogin },
Damien Miller95def091999-11-25 00:26:21 +1100322 { "keepalive", sKeepAlives },
Damien Miller50a41ed2000-10-16 12:14:42 +1100323 { "allowtcpforwarding", sAllowTcpForwarding },
Damien Miller95def091999-11-25 00:26:21 +1100324 { "allowusers", sAllowUsers },
325 { "denyusers", sDenyUsers },
326 { "allowgroups", sAllowGroups },
327 { "denygroups", sDenyGroups },
Damien Miller78928792000-04-12 20:17:38 +1000328 { "ciphers", sCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000329 { "macs", sMacs },
Damien Miller78928792000-04-12 20:17:38 +1000330 { "protocol", sProtocol },
Damien Millere247cc42000-05-07 12:03:14 +1000331 { "gatewayports", sGatewayPorts },
Damien Millerf6d9e222000-06-18 14:50:44 +1000332 { "subsystem", sSubsystem },
Damien Miller37023962000-07-11 17:31:38 +1000333 { "maxstartups", sMaxStartups },
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000334 { "banner", sBanner },
Damien Miller33804262001-02-04 23:20:18 +1100335 { "reversemappingcheck", sReverseMappingCheck },
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000336 { "clientaliveinterval", sClientAliveInterval },
337 { "clientalivecountmax", sClientAliveCountMax },
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000338 { "authorizedkeysfile", sAuthorizedKeysFile },
339 { "authorizedkeysfile2", sAuthorizedKeysFile2 },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000340 { NULL, sBadOption }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341};
342
Damien Miller5428f641999-11-25 11:54:57 +1100343/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000344 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100345 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Miller4af51302000-04-16 11:18:38 +1000347static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100348parse_token(const char *cp, const char *filename,
349 int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000351 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller95def091999-11-25 00:26:21 +1100353 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100354 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100355 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000357 error("%s: line %d: Bad configuration option: %s",
358 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100359 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360}
361
Ben Lindstrombba81212001-06-25 05:01:22 +0000362static void
Ben Lindstrom19066a12001-04-12 23:39:26 +0000363add_listen_addr(ServerOptions *options, char *addr, u_short port)
Damien Miller34132e52000-01-14 15:45:46 +1100364{
Damien Miller34132e52000-01-14 15:45:46 +1100365 int i;
366
367 if (options->num_ports == 0)
368 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000369 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000370 for (i = 0; i < options->num_ports; i++)
371 add_one_listen_addr(options, addr, options->ports[i]);
372 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000373 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000374}
375
Ben Lindstrombba81212001-06-25 05:01:22 +0000376static void
Ben Lindstromc510af42001-04-07 17:25:48 +0000377add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
378{
379 struct addrinfo hints, *ai, *aitop;
380 char strport[NI_MAXSERV];
381 int gaierr;
382
383 memset(&hints, 0, sizeof(hints));
384 hints.ai_family = IPv4or6;
385 hints.ai_socktype = SOCK_STREAM;
386 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
387 snprintf(strport, sizeof strport, "%d", port);
388 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
389 fatal("bad addr or host: %s (%s)",
390 addr ? addr : "<NULL>",
391 gai_strerror(gaierr));
392 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
393 ;
394 ai->ai_next = options->listen_addrs;
395 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100396}
397
Ben Lindstromade03f62001-12-06 18:22:17 +0000398int
399process_server_config_line(ServerOptions *options, char *line,
400 const char *filename, int linenum)
401{
402 char *cp, **charptr, *arg, *p;
403 int *intptr, value;
404 ServerOpCodes opcode;
405 int i, n;
406
407 cp = line;
408 arg = strdelim(&cp);
409 /* Ignore leading whitespace */
410 if (*arg == '\0')
411 arg = strdelim(&cp);
412 if (!arg || !*arg || *arg == '#')
413 return 0;
414 intptr = NULL;
415 charptr = NULL;
416 opcode = parse_token(arg, filename, linenum);
417 switch (opcode) {
418 /* Portable-specific options */
419 case sPAMAuthenticationViaKbdInt:
420 intptr = &options->pam_authentication_via_kbd_int;
421 goto parse_flag;
422
423 /* Standard Options */
424 case sBadOption:
425 return -1;
426 case sPort:
427 /* ignore ports from configfile if cmdline specifies ports */
428 if (options->ports_from_cmdline)
429 return 0;
430 if (options->listen_addrs != NULL)
431 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100432 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000433 if (options->num_ports >= MAX_PORTS)
434 fatal("%s line %d: too many ports.",
435 filename, linenum);
436 arg = strdelim(&cp);
437 if (!arg || *arg == '\0')
438 fatal("%s line %d: missing port number.",
439 filename, linenum);
440 options->ports[options->num_ports++] = a2port(arg);
441 if (options->ports[options->num_ports-1] == 0)
442 fatal("%s line %d: Badly formatted port number.",
443 filename, linenum);
444 break;
445
446 case sServerKeyBits:
447 intptr = &options->server_key_bits;
448parse_int:
449 arg = strdelim(&cp);
450 if (!arg || *arg == '\0')
451 fatal("%s line %d: missing integer value.",
452 filename, linenum);
453 value = atoi(arg);
454 if (*intptr == -1)
455 *intptr = value;
456 break;
457
458 case sLoginGraceTime:
459 intptr = &options->login_grace_time;
460parse_time:
461 arg = strdelim(&cp);
462 if (!arg || *arg == '\0')
463 fatal("%s line %d: missing time value.",
464 filename, linenum);
465 if ((value = convtime(arg)) == -1)
466 fatal("%s line %d: invalid time value.",
467 filename, linenum);
468 if (*intptr == -1)
469 *intptr = value;
470 break;
471
472 case sKeyRegenerationTime:
473 intptr = &options->key_regeneration_time;
474 goto parse_time;
475
476 case sListenAddress:
477 arg = strdelim(&cp);
478 if (!arg || *arg == '\0' || strncmp(arg, "[]", 2) == 0)
479 fatal("%s line %d: missing inet addr.",
480 filename, linenum);
481 if (*arg == '[') {
482 if ((p = strchr(arg, ']')) == NULL)
483 fatal("%s line %d: bad ipv6 inet addr usage.",
484 filename, linenum);
485 arg++;
486 memmove(p, p+1, strlen(p+1)+1);
487 } else if (((p = strchr(arg, ':')) == NULL) ||
488 (strchr(p+1, ':') != NULL)) {
489 add_listen_addr(options, arg, 0);
490 break;
491 }
492 if (*p == ':') {
493 u_short port;
494
495 p++;
496 if (*p == '\0')
497 fatal("%s line %d: bad inet addr:port usage.",
498 filename, linenum);
499 else {
500 *(p-1) = '\0';
501 if ((port = a2port(p)) == 0)
502 fatal("%s line %d: bad port number.",
503 filename, linenum);
504 add_listen_addr(options, arg, port);
505 }
506 } else if (*p == '\0')
507 add_listen_addr(options, arg, 0);
508 else
509 fatal("%s line %d: bad inet addr usage.",
510 filename, linenum);
511 break;
512
513 case sHostKeyFile:
514 intptr = &options->num_host_key_files;
515 if (*intptr >= MAX_HOSTKEYS)
516 fatal("%s line %d: too many host keys specified (max %d).",
517 filename, linenum, MAX_HOSTKEYS);
518 charptr = &options->host_key_files[*intptr];
519parse_filename:
520 arg = strdelim(&cp);
521 if (!arg || *arg == '\0')
522 fatal("%s line %d: missing file name.",
523 filename, linenum);
524 if (*charptr == NULL) {
525 *charptr = tilde_expand_filename(arg, getuid());
526 /* increase optional counter */
527 if (intptr != NULL)
528 *intptr = *intptr + 1;
529 }
530 break;
531
532 case sPidFile:
533 charptr = &options->pid_file;
534 goto parse_filename;
535
536 case sPermitRootLogin:
537 intptr = &options->permit_root_login;
538 arg = strdelim(&cp);
539 if (!arg || *arg == '\0')
540 fatal("%s line %d: missing yes/"
541 "without-password/forced-commands-only/no "
542 "argument.", filename, linenum);
543 value = 0; /* silence compiler */
544 if (strcmp(arg, "without-password") == 0)
545 value = PERMIT_NO_PASSWD;
546 else if (strcmp(arg, "forced-commands-only") == 0)
547 value = PERMIT_FORCED_ONLY;
548 else if (strcmp(arg, "yes") == 0)
549 value = PERMIT_YES;
550 else if (strcmp(arg, "no") == 0)
551 value = PERMIT_NO;
552 else
553 fatal("%s line %d: Bad yes/"
554 "without-password/forced-commands-only/no "
555 "argument: %s", filename, linenum, arg);
556 if (*intptr == -1)
557 *intptr = value;
558 break;
559
560 case sIgnoreRhosts:
561 intptr = &options->ignore_rhosts;
562parse_flag:
563 arg = strdelim(&cp);
564 if (!arg || *arg == '\0')
565 fatal("%s line %d: missing yes/no argument.",
566 filename, linenum);
567 value = 0; /* silence compiler */
568 if (strcmp(arg, "yes") == 0)
569 value = 1;
570 else if (strcmp(arg, "no") == 0)
571 value = 0;
572 else
573 fatal("%s line %d: Bad yes/no argument: %s",
574 filename, linenum, arg);
575 if (*intptr == -1)
576 *intptr = value;
577 break;
578
579 case sIgnoreUserKnownHosts:
580 intptr = &options->ignore_user_known_hosts;
581 goto parse_flag;
582
583 case sRhostsAuthentication:
584 intptr = &options->rhosts_authentication;
585 goto parse_flag;
586
587 case sRhostsRSAAuthentication:
588 intptr = &options->rhosts_rsa_authentication;
589 goto parse_flag;
590
591 case sHostbasedAuthentication:
592 intptr = &options->hostbased_authentication;
593 goto parse_flag;
594
595 case sHostbasedUsesNameFromPacketOnly:
596 intptr = &options->hostbased_uses_name_from_packet_only;
597 goto parse_flag;
598
599 case sRSAAuthentication:
600 intptr = &options->rsa_authentication;
601 goto parse_flag;
602
603 case sPubkeyAuthentication:
604 intptr = &options->pubkey_authentication;
605 goto parse_flag;
606#if defined(KRB4) || defined(KRB5)
607 case sKerberosAuthentication:
608 intptr = &options->kerberos_authentication;
609 goto parse_flag;
610
611 case sKerberosOrLocalPasswd:
612 intptr = &options->kerberos_or_local_passwd;
613 goto parse_flag;
614
615 case sKerberosTicketCleanup:
616 intptr = &options->kerberos_ticket_cleanup;
617 goto parse_flag;
618#endif
619#if defined(AFS) || defined(KRB5)
620 case sKerberosTgtPassing:
621 intptr = &options->kerberos_tgt_passing;
622 goto parse_flag;
623#endif
624#ifdef AFS
625 case sAFSTokenPassing:
626 intptr = &options->afs_token_passing;
627 goto parse_flag;
628#endif
629
630 case sPasswordAuthentication:
631 intptr = &options->password_authentication;
632 goto parse_flag;
633
634 case sKbdInteractiveAuthentication:
635 intptr = &options->kbd_interactive_authentication;
636 goto parse_flag;
637
638 case sChallengeResponseAuthentication:
639 intptr = &options->challenge_response_authentication;
640 goto parse_flag;
641
642 case sPrintMotd:
643 intptr = &options->print_motd;
644 goto parse_flag;
645
646 case sPrintLastLog:
647 intptr = &options->print_lastlog;
648 goto parse_flag;
649
650 case sX11Forwarding:
651 intptr = &options->x11_forwarding;
652 goto parse_flag;
653
654 case sX11DisplayOffset:
655 intptr = &options->x11_display_offset;
656 goto parse_int;
657
658 case sXAuthLocation:
659 charptr = &options->xauth_location;
660 goto parse_filename;
661
662 case sStrictModes:
663 intptr = &options->strict_modes;
664 goto parse_flag;
665
666 case sKeepAlives:
667 intptr = &options->keepalives;
668 goto parse_flag;
669
670 case sEmptyPasswd:
671 intptr = &options->permit_empty_passwd;
672 goto parse_flag;
673
674 case sUseLogin:
675 intptr = &options->use_login;
676 goto parse_flag;
677
678 case sGatewayPorts:
679 intptr = &options->gateway_ports;
680 goto parse_flag;
681
682 case sReverseMappingCheck:
683 intptr = &options->reverse_mapping_check;
684 goto parse_flag;
685
686 case sLogFacility:
687 intptr = (int *) &options->log_facility;
688 arg = strdelim(&cp);
689 value = log_facility_number(arg);
690 if (value == (SyslogFacility) - 1)
691 fatal("%.200s line %d: unsupported log facility '%s'",
692 filename, linenum, arg ? arg : "<NONE>");
693 if (*intptr == -1)
694 *intptr = (SyslogFacility) value;
695 break;
696
697 case sLogLevel:
698 intptr = (int *) &options->log_level;
699 arg = strdelim(&cp);
700 value = log_level_number(arg);
701 if (value == (LogLevel) - 1)
702 fatal("%.200s line %d: unsupported log level '%s'",
703 filename, linenum, arg ? arg : "<NONE>");
704 if (*intptr == -1)
705 *intptr = (LogLevel) value;
706 break;
707
708 case sAllowTcpForwarding:
709 intptr = &options->allow_tcp_forwarding;
710 goto parse_flag;
711
712 case sAllowUsers:
713 while ((arg = strdelim(&cp)) && *arg != '\0') {
714 if (options->num_allow_users >= MAX_ALLOW_USERS)
715 fatal("%s line %d: too many allow users.",
716 filename, linenum);
717 options->allow_users[options->num_allow_users++] = xstrdup(arg);
718 }
719 break;
720
721 case sDenyUsers:
722 while ((arg = strdelim(&cp)) && *arg != '\0') {
723 if (options->num_deny_users >= MAX_DENY_USERS)
724 fatal( "%s line %d: too many deny users.",
725 filename, linenum);
726 options->deny_users[options->num_deny_users++] = xstrdup(arg);
727 }
728 break;
729
730 case sAllowGroups:
731 while ((arg = strdelim(&cp)) && *arg != '\0') {
732 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
733 fatal("%s line %d: too many allow groups.",
734 filename, linenum);
735 options->allow_groups[options->num_allow_groups++] = xstrdup(arg);
736 }
737 break;
738
739 case sDenyGroups:
740 while ((arg = strdelim(&cp)) && *arg != '\0') {
741 if (options->num_deny_groups >= MAX_DENY_GROUPS)
742 fatal("%s line %d: too many deny groups.",
743 filename, linenum);
744 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
745 }
746 break;
747
748 case sCiphers:
749 arg = strdelim(&cp);
750 if (!arg || *arg == '\0')
751 fatal("%s line %d: Missing argument.", filename, linenum);
752 if (!ciphers_valid(arg))
753 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
754 filename, linenum, arg ? arg : "<NONE>");
755 if (options->ciphers == NULL)
756 options->ciphers = xstrdup(arg);
757 break;
758
759 case sMacs:
760 arg = strdelim(&cp);
761 if (!arg || *arg == '\0')
762 fatal("%s line %d: Missing argument.", filename, linenum);
763 if (!mac_valid(arg))
764 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
765 filename, linenum, arg ? arg : "<NONE>");
766 if (options->macs == NULL)
767 options->macs = xstrdup(arg);
768 break;
769
770 case sProtocol:
771 intptr = &options->protocol;
772 arg = strdelim(&cp);
773 if (!arg || *arg == '\0')
774 fatal("%s line %d: Missing argument.", filename, linenum);
775 value = proto_spec(arg);
776 if (value == SSH_PROTO_UNKNOWN)
777 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100778 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +0000779 if (*intptr == SSH_PROTO_UNKNOWN)
780 *intptr = value;
781 break;
782
783 case sSubsystem:
784 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
785 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100786 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000787 }
788 arg = strdelim(&cp);
789 if (!arg || *arg == '\0')
790 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100791 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000792 for (i = 0; i < options->num_subsystems; i++)
793 if (strcmp(arg, options->subsystem_name[i]) == 0)
794 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100795 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000796 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
797 arg = strdelim(&cp);
798 if (!arg || *arg == '\0')
799 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100800 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000801 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
802 options->num_subsystems++;
803 break;
804
805 case sMaxStartups:
806 arg = strdelim(&cp);
807 if (!arg || *arg == '\0')
808 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100809 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000810 if ((n = sscanf(arg, "%d:%d:%d",
811 &options->max_startups_begin,
812 &options->max_startups_rate,
813 &options->max_startups)) == 3) {
814 if (options->max_startups_begin >
815 options->max_startups ||
816 options->max_startups_rate > 100 ||
817 options->max_startups_rate < 1)
818 fatal("%s line %d: Illegal MaxStartups spec.",
819 filename, linenum);
820 } else if (n != 1)
821 fatal("%s line %d: Illegal MaxStartups spec.",
822 filename, linenum);
823 else
824 options->max_startups = options->max_startups_begin;
825 break;
826
827 case sBanner:
828 charptr = &options->banner;
829 goto parse_filename;
830 /*
831 * These options can contain %X options expanded at
832 * connect time, so that you can specify paths like:
833 *
834 * AuthorizedKeysFile /etc/ssh_keys/%u
835 */
836 case sAuthorizedKeysFile:
837 case sAuthorizedKeysFile2:
838 charptr = (opcode == sAuthorizedKeysFile ) ?
839 &options->authorized_keys_file :
840 &options->authorized_keys_file2;
841 goto parse_filename;
842
843 case sClientAliveInterval:
844 intptr = &options->client_alive_interval;
845 goto parse_time;
846
847 case sClientAliveCountMax:
848 intptr = &options->client_alive_count_max;
849 goto parse_int;
850
851 case sDeprecated:
852 log("%s line %d: Deprecated option %s",
853 filename, linenum, arg);
854 while (arg)
855 arg = strdelim(&cp);
856 break;
857
858 default:
859 fatal("%s line %d: Missing handler for opcode %s (%d)",
860 filename, linenum, arg, opcode);
861 }
862 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
863 fatal("%s line %d: garbage at end of line; \"%.200s\".",
864 filename, linenum, arg);
865 return 0;
866}
867
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868/* Reads the server configuration file. */
869
Damien Miller4af51302000-04-16 11:18:38 +1000870void
Damien Miller95def091999-11-25 00:26:21 +1100871read_server_config(ServerOptions *options, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872{
Damien Miller95def091999-11-25 00:26:21 +1100873 FILE *f;
874 char line[1024];
Ben Lindstromade03f62001-12-06 18:22:17 +0000875 int linenum;
Damien Miller95def091999-11-25 00:26:21 +1100876 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Miller95def091999-11-25 00:26:21 +1100878 f = fopen(filename, "r");
879 if (!f) {
880 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100882 }
883 linenum = 0;
884 while (fgets(line, sizeof(line), f)) {
Ben Lindstromade03f62001-12-06 18:22:17 +0000885 /* Update line number counter. */
Damien Miller95def091999-11-25 00:26:21 +1100886 linenum++;
Ben Lindstromade03f62001-12-06 18:22:17 +0000887 if (process_server_config_line(options, line, filename, linenum) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100888 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 }
Damien Miller95def091999-11-25 00:26:21 +1100890 fclose(f);
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000891 if (bad_options > 0)
892 fatal("%s: terminating, %d bad configuration options",
893 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894}