blob: 5a7084fe890af95d94a49d65267697f5d5b5b2e9 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Darren Tuckera044f472003-10-15 15:52:03 +100015RCSID("$OpenBSD: readconf.c,v 1.122 2003/10/08 15:21:24 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100019#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "cipher.h"
21#include "pathnames.h"
22#include "log.h"
23#include "readconf.h"
24#include "match.h"
25#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000026#include "kex.h"
27#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
29/* Format of the configuration file:
30
31 # Configuration data is parsed as follows:
32 # 1. command line options
33 # 2. user-specific file
34 # 3. system-wide file
35 # Any configuration value is only changed the first time it is set.
36 # Thus, host-specific definitions should be at the beginning of the
37 # configuration file, and defaults at the end.
38
39 # Host-specific declarations. These may override anything above. A single
40 # host may match multiple declarations; these are processed in the order
41 # that they are given in.
42
43 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000044 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46 Host fake.com
47 HostName another.host.name.real.org
48 User blaah
49 Port 34289
50 ForwardX11 no
51 ForwardAgent no
52
53 Host books.com
54 RemoteForward 9999 shadows.cs.hut.fi:9999
55 Cipher 3des
56
57 Host fascist.blob.com
58 Port 23123
59 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060 PasswordAuthentication no
61
62 Host puukko.hut.fi
63 User t35124p
64 ProxyCommand ssh-proxy %h %p
65
66 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000067 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
69 Host *.su
70 Cipher none
71 PasswordAuthentication no
72
73 # Defaults for various options
74 Host *
75 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110076 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication yes
78 RSAAuthentication yes
79 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080 StrictHostKeyChecking yes
81 KeepAlives no
82 IdentityFile ~/.ssh/identity
83 Port 22
84 EscapeChar ~
85
86*/
87
88/* Keyword tokens. */
89
Damien Miller95def091999-11-25 00:26:21 +110090typedef enum {
91 oBadOption,
Darren Tuckerec960f22003-08-13 20:37:05 +100092 oForwardAgent, oForwardX11, oGatewayPorts,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000093 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +000094 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +110095 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
96 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
97 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
98 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +000099 oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000100 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100101 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000102 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000103 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000104 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000105 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000106 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000107 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000108 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109} OpCodes;
110
111/* Textual representations of the tokens. */
112
Damien Miller95def091999-11-25 00:26:21 +1100113static struct {
114 const char *name;
115 OpCodes opcode;
116} keywords[] = {
117 { "forwardagent", oForwardAgent },
118 { "forwardx11", oForwardX11 },
Damien Millerd3a18572000-06-07 19:55:44 +1000119 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100120 { "gatewayports", oGatewayPorts },
121 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000122 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100123 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100124 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
125 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100126 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100127 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000128 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000129 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000130 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000131 { "challengeresponseauthentication", oChallengeResponseAuthentication },
132 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
133 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000134 { "kerberosauthentication", oUnsupported },
135 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000136 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000137#if defined(GSSAPI)
138 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000139 { "gssapidelegatecredentials", oGssDelegateCreds },
140#else
141 { "gssapiauthentication", oUnsupported },
142 { "gssapidelegatecredentials", oUnsupported },
143#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000144 { "fallbacktorsh", oDeprecated },
145 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100146 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100147 { "identityfile2", oIdentityFile }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100148 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000149 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100150 { "proxycommand", oProxyCommand },
151 { "port", oPort },
152 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000153 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000154 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000155 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100156 { "remoteforward", oRemoteForward },
157 { "localforward", oLocalForward },
158 { "user", oUser },
159 { "host", oHost },
160 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100161 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000162 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000163 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000164 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100165 { "connectionattempts", oConnectionAttempts },
166 { "batchmode", oBatchMode },
167 { "checkhostip", oCheckHostIP },
168 { "stricthostkeychecking", oStrictHostKeyChecking },
169 { "compression", oCompression },
170 { "compressionlevel", oCompressionLevel },
171 { "keepalive", oKeepAlives },
172 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000174 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000175 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000176 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000177 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000178#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000179 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000180#else
181 { "smartcarddevice", oUnsupported },
182#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100183 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000184 { "enablesshkeysign", oEnableSSHKeysign },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000185#ifdef DNS
Damien Miller37876e92003-05-15 10:19:46 +1000186 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000187#else
188 { "verifyhostkeydns", oUnsupported },
189#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100190 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000191 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000192 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000193 { "addressfamily", oAddressFamily },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000194 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100195};
196
Damien Miller5428f641999-11-25 11:54:57 +1100197/*
198 * Adds a local TCP/IP port forward to options. Never returns if there is an
199 * error.
200 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Damien Miller4af51302000-04-16 11:18:38 +1000202void
Damien Milleraae6c611999-12-06 11:47:28 +1100203add_local_forward(Options *options, u_short port, const char *host,
204 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205{
Damien Miller95def091999-11-25 00:26:21 +1100206 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000207#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100208 extern uid_t original_real_uid;
Damien Miller95def091999-11-25 00:26:21 +1100209 if (port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000210 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100211#endif
Damien Miller95def091999-11-25 00:26:21 +1100212 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
213 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
214 fwd = &options->local_forwards[options->num_local_forwards++];
215 fwd->port = port;
216 fwd->host = xstrdup(host);
217 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218}
219
Damien Miller5428f641999-11-25 11:54:57 +1100220/*
221 * Adds a remote TCP/IP port forward to options. Never returns if there is
222 * an error.
223 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller4af51302000-04-16 11:18:38 +1000225void
Damien Milleraae6c611999-12-06 11:47:28 +1100226add_remote_forward(Options *options, u_short port, const char *host,
227 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228{
Damien Miller95def091999-11-25 00:26:21 +1100229 Forward *fwd;
230 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
231 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100232 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100233 fwd = &options->remote_forwards[options->num_remote_forwards++];
234 fwd->port = port;
235 fwd->host = xstrdup(host);
236 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237}
238
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000239static void
240clear_forwardings(Options *options)
241{
242 int i;
243
244 for (i = 0; i < options->num_local_forwards; i++)
245 xfree(options->local_forwards[i].host);
246 options->num_local_forwards = 0;
247 for (i = 0; i < options->num_remote_forwards; i++)
248 xfree(options->remote_forwards[i].host);
249 options->num_remote_forwards = 0;
250}
251
Damien Miller5428f641999-11-25 11:54:57 +1100252/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000253 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100254 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller4af51302000-04-16 11:18:38 +1000256static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100257parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000259 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller95def091999-11-25 00:26:21 +1100261 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100262 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100263 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000265 error("%s: line %d: Bad configuration option: %s",
266 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100267 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268}
269
Damien Miller5428f641999-11-25 11:54:57 +1100270/*
271 * Processes a single option line as used in the configuration files. This
272 * only sets those values that have not already been set.
273 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100274#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275
Damien Miller2ccf6611999-11-15 15:25:10 +1100276int
277process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100278 char *line, const char *filename, int linenum,
279 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280{
Damien Miller61f08ac2003-02-24 11:56:27 +1100281 char buf[256], *s, **charptr, *endofnumber, *keyword, *arg;
Damien Milleraae6c611999-12-06 11:47:28 +1100282 int opcode, *intptr, value;
Damien Miller61f08ac2003-02-24 11:56:27 +1100283 size_t len;
Damien Milleraae6c611999-12-06 11:47:28 +1100284 u_short fwd_port, fwd_host_port;
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000285 char sfwd_host_port[6];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Millerc652cac2003-05-14 13:40:54 +1000287 /* Strip trailing whitespace */
288 for(len = strlen(line) - 1; len > 0; len--) {
289 if (strchr(WHITESPACE, line[len]) == NULL)
290 break;
291 line[len] = '\0';
292 }
293
Damien Millerbe484b52000-07-15 14:14:16 +1000294 s = line;
295 /* Get the keyword. (Each line is supposed to begin with a keyword). */
296 keyword = strdelim(&s);
297 /* Ignore leading whitespace. */
298 if (*keyword == '\0')
299 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000300 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100301 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller37023962000-07-11 17:31:38 +1000303 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304
Damien Miller95def091999-11-25 00:26:21 +1100305 switch (opcode) {
306 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100307 /* don't panic, but count bad options */
308 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100309 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000310 case oConnectTimeout:
311 intptr = &options->connection_timeout;
312/* parse_time: */
313 arg = strdelim(&s);
314 if (!arg || *arg == '\0')
315 fatal("%s line %d: missing time value.",
316 filename, linenum);
317 if ((value = convtime(arg)) == -1)
318 fatal("%s line %d: invalid time value.",
319 filename, linenum);
320 if (*intptr == -1)
321 *intptr = value;
322 break;
323
Damien Miller95def091999-11-25 00:26:21 +1100324 case oForwardAgent:
325 intptr = &options->forward_agent;
326parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000327 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000328 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100329 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
330 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000331 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100332 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000333 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100334 value = 0;
335 else
336 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
337 if (*activep && *intptr == -1)
338 *intptr = value;
339 break;
340
341 case oForwardX11:
342 intptr = &options->forward_x11;
343 goto parse_flag;
344
345 case oGatewayPorts:
346 intptr = &options->gateway_ports;
347 goto parse_flag;
348
349 case oUsePrivilegedPort:
350 intptr = &options->use_privileged_port;
351 goto parse_flag;
352
Damien Miller95def091999-11-25 00:26:21 +1100353 case oPasswordAuthentication:
354 intptr = &options->password_authentication;
355 goto parse_flag;
356
Damien Miller874d77b2000-10-14 16:23:11 +1100357 case oKbdInteractiveAuthentication:
358 intptr = &options->kbd_interactive_authentication;
359 goto parse_flag;
360
361 case oKbdInteractiveDevices:
362 charptr = &options->kbd_interactive_devices;
363 goto parse_string;
364
Damien Miller0bc1bd82000-11-13 22:57:25 +1100365 case oPubkeyAuthentication:
366 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000367 goto parse_flag;
368
Damien Miller95def091999-11-25 00:26:21 +1100369 case oRSAAuthentication:
370 intptr = &options->rsa_authentication;
371 goto parse_flag;
372
373 case oRhostsRSAAuthentication:
374 intptr = &options->rhosts_rsa_authentication;
375 goto parse_flag;
376
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000377 case oHostbasedAuthentication:
378 intptr = &options->hostbased_authentication;
379 goto parse_flag;
380
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000381 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000382 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100383 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000384
Darren Tucker0efd1552003-08-26 11:49:55 +1000385 case oGssAuthentication:
386 intptr = &options->gss_authentication;
387 goto parse_flag;
388
389 case oGssDelegateCreds:
390 intptr = &options->gss_deleg_creds;
391 goto parse_flag;
392
Damien Miller95def091999-11-25 00:26:21 +1100393 case oBatchMode:
394 intptr = &options->batch_mode;
395 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396
Damien Miller95def091999-11-25 00:26:21 +1100397 case oCheckHostIP:
398 intptr = &options->check_host_ip;
399 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Damien Miller37876e92003-05-15 10:19:46 +1000401 case oVerifyHostKeyDNS:
402 intptr = &options->verify_host_key_dns;
403 goto parse_flag;
404
Damien Miller95def091999-11-25 00:26:21 +1100405 case oStrictHostKeyChecking:
406 intptr = &options->strict_host_key_checking;
Damien Millerbe484b52000-07-15 14:14:16 +1000407 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000408 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000409 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100410 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100411 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000412 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100413 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000414 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100415 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000416 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100417 value = 2;
418 else
419 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
420 if (*activep && *intptr == -1)
421 *intptr = value;
422 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller95def091999-11-25 00:26:21 +1100424 case oCompression:
425 intptr = &options->compression;
426 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427
Damien Miller95def091999-11-25 00:26:21 +1100428 case oKeepAlives:
429 intptr = &options->keepalives;
430 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000432 case oNoHostAuthenticationForLocalhost:
433 intptr = &options->no_host_authentication_for_localhost;
434 goto parse_flag;
435
Damien Miller95def091999-11-25 00:26:21 +1100436 case oNumberOfPasswordPrompts:
437 intptr = &options->number_of_password_prompts;
438 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439
Damien Miller95def091999-11-25 00:26:21 +1100440 case oCompressionLevel:
441 intptr = &options->compression_level;
442 goto parse_int;
443
Damien Millera5539d22003-04-09 20:50:06 +1000444 case oRekeyLimit:
445 intptr = &options->rekey_limit;
446 arg = strdelim(&s);
447 if (!arg || *arg == '\0')
448 fatal("%.200s line %d: Missing argument.", filename, linenum);
449 if (arg[0] < '0' || arg[0] > '9')
450 fatal("%.200s line %d: Bad number.", filename, linenum);
451 value = strtol(arg, &endofnumber, 10);
452 if (arg == endofnumber)
453 fatal("%.200s line %d: Bad number.", filename, linenum);
454 switch (toupper(*endofnumber)) {
455 case 'K':
456 value *= 1<<10;
457 break;
458 case 'M':
459 value *= 1<<20;
460 break;
461 case 'G':
462 value *= 1<<30;
463 break;
464 }
465 if (*activep && *intptr == -1)
466 *intptr = value;
467 break;
468
Damien Miller95def091999-11-25 00:26:21 +1100469 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000470 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000471 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100472 fatal("%.200s line %d: Missing argument.", filename, linenum);
473 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000475 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100476 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100477 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000479 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000480 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100481 }
482 break;
483
Damien Millerd3a18572000-06-07 19:55:44 +1000484 case oXAuthLocation:
485 charptr=&options->xauth_location;
486 goto parse_string;
487
Damien Miller95def091999-11-25 00:26:21 +1100488 case oUser:
489 charptr = &options->user;
490parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000491 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000492 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100493 fatal("%.200s line %d: Missing argument.", filename, linenum);
494 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000495 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100496 break;
497
498 case oGlobalKnownHostsFile:
499 charptr = &options->system_hostfile;
500 goto parse_string;
501
502 case oUserKnownHostsFile:
503 charptr = &options->user_hostfile;
504 goto parse_string;
505
Damien Millereba71ba2000-04-29 23:57:08 +1000506 case oGlobalKnownHostsFile2:
507 charptr = &options->system_hostfile2;
508 goto parse_string;
509
510 case oUserKnownHostsFile2:
511 charptr = &options->user_hostfile2;
512 goto parse_string;
513
Damien Miller95def091999-11-25 00:26:21 +1100514 case oHostName:
515 charptr = &options->hostname;
516 goto parse_string;
517
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000518 case oHostKeyAlias:
519 charptr = &options->host_key_alias;
520 goto parse_string;
521
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000522 case oPreferredAuthentications:
523 charptr = &options->preferred_authentications;
524 goto parse_string;
525
Ben Lindstrome0f88042001-04-30 13:06:24 +0000526 case oBindAddress:
527 charptr = &options->bind_address;
528 goto parse_string;
529
Ben Lindstromae996bf2001-08-06 21:27:53 +0000530 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000531 charptr = &options->smartcard_device;
532 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000533
Damien Miller95def091999-11-25 00:26:21 +1100534 case oProxyCommand:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000535 if (s == NULL)
536 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100537 charptr = &options->proxy_command;
Damien Miller61f08ac2003-02-24 11:56:27 +1100538 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100539 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100540 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100541 return 0;
542
543 case oPort:
544 intptr = &options->port;
545parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000546 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000547 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100548 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000549 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100550 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100551
552 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000553 value = strtol(arg, &endofnumber, 0);
554 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100555 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100556 if (*activep && *intptr == -1)
557 *intptr = value;
558 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559
Damien Miller95def091999-11-25 00:26:21 +1100560 case oConnectionAttempts:
561 intptr = &options->connection_attempts;
562 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100563
Damien Miller95def091999-11-25 00:26:21 +1100564 case oCipher:
565 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000566 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000567 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000568 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000569 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100570 if (value == -1)
571 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100572 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100573 if (*activep && *intptr == -1)
574 *intptr = value;
575 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576
Damien Miller78928792000-04-12 20:17:38 +1000577 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000578 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000579 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000580 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000581 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000582 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100583 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000584 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000585 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000586 break;
587
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000588 case oMacs:
589 arg = strdelim(&s);
590 if (!arg || *arg == '\0')
591 fatal("%.200s line %d: Missing argument.", filename, linenum);
592 if (!mac_valid(arg))
593 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100594 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000595 if (*activep && options->macs == NULL)
596 options->macs = xstrdup(arg);
597 break;
598
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000599 case oHostKeyAlgorithms:
600 arg = strdelim(&s);
601 if (!arg || *arg == '\0')
602 fatal("%.200s line %d: Missing argument.", filename, linenum);
603 if (!key_names_valid2(arg))
604 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000606 if (*activep && options->hostkeyalgorithms == NULL)
607 options->hostkeyalgorithms = xstrdup(arg);
608 break;
609
Damien Miller78928792000-04-12 20:17:38 +1000610 case oProtocol:
611 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000612 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000613 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000614 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000615 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000616 if (value == SSH_PROTO_UNKNOWN)
617 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100618 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000619 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
620 *intptr = value;
621 break;
622
Damien Miller95def091999-11-25 00:26:21 +1100623 case oLogLevel:
624 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000625 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000626 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100627 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000628 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100629 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100630 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100631 *intptr = (LogLevel) value;
632 break;
633
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000634 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100635 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000636 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000637 if (!arg || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000638 fatal("%.200s line %d: Missing port argument.",
639 filename, linenum);
640 if ((fwd_port = a2port(arg)) == 0)
641 fatal("%.200s line %d: Bad listen port.",
642 filename, linenum);
Damien Millerbe484b52000-07-15 14:14:16 +1000643 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000644 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100645 fatal("%.200s line %d: Missing second argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000646 filename, linenum);
647 if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 &&
648 sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2)
649 fatal("%.200s line %d: Bad forwarding specification.",
650 filename, linenum);
651 if ((fwd_host_port = a2port(sfwd_host_port)) == 0)
652 fatal("%.200s line %d: Bad forwarding port.",
653 filename, linenum);
654 if (*activep) {
655 if (opcode == oLocalForward)
656 add_local_forward(options, fwd_port, buf,
657 fwd_host_port);
658 else if (opcode == oRemoteForward)
659 add_remote_forward(options, fwd_port, buf,
660 fwd_host_port);
661 }
Damien Miller95def091999-11-25 00:26:21 +1100662 break;
663
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000664 case oDynamicForward:
665 arg = strdelim(&s);
666 if (!arg || *arg == '\0')
667 fatal("%.200s line %d: Missing port argument.",
668 filename, linenum);
Ben Lindstrom19066a12001-04-12 23:39:26 +0000669 fwd_port = a2port(arg);
670 if (fwd_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000671 fatal("%.200s line %d: Badly formatted port number.",
672 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000673 if (*activep)
Darren Tucker1c52ee32003-08-13 20:38:36 +1000674 add_local_forward(options, fwd_port, "socks", 0);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000675 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000676
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000677 case oClearAllForwardings:
678 intptr = &options->clear_forwardings;
679 goto parse_flag;
680
Damien Miller95def091999-11-25 00:26:21 +1100681 case oHost:
682 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000683 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000684 if (match_pattern(host, arg)) {
685 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100686 *activep = 1;
687 break;
688 }
Damien Millerbe484b52000-07-15 14:14:16 +1000689 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100690 return 0;
691
692 case oEscapeChar:
693 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000694 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000695 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100696 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000697 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000698 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
699 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000700 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000701 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000702 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000703 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100704 else {
705 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100706 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100707 /* NOTREACHED */
708 value = 0; /* Avoid compiler warning. */
709 }
710 if (*activep && *intptr == -1)
711 *intptr = value;
712 break;
713
Damien Miller20a8f972003-05-18 20:50:30 +1000714 case oAddressFamily:
715 arg = strdelim(&s);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000716 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000717 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000718 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000719 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000720 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000721 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000722 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000723 else
724 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000725 if (*activep && *intptr == -1)
726 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000727 break;
728
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000729 case oEnableSSHKeysign:
730 intptr = &options->enable_ssh_keysign;
731 goto parse_flag;
732
Ben Lindstrom4daea862002-06-09 20:04:02 +0000733 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000734 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000735 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000736 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000737
Damien Millerf9b3feb2003-05-16 11:38:32 +1000738 case oUnsupported:
739 error("%s line %d: Unsupported option \"%s\"",
740 filename, linenum, keyword);
741 return 0;
742
Damien Miller95def091999-11-25 00:26:21 +1100743 default:
744 fatal("process_config_line: Unimplemented opcode %d", opcode);
745 }
746
747 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000748 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000749 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100750 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000751 }
Damien Miller95def091999-11-25 00:26:21 +1100752 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753}
754
755
Damien Miller5428f641999-11-25 11:54:57 +1100756/*
757 * Reads the config file and modifies the options accordingly. Options
758 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000759 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100760 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000762int
Damien Miller95def091999-11-25 00:26:21 +1100763read_config_file(const char *filename, const char *host, Options *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000764{
Damien Miller95def091999-11-25 00:26:21 +1100765 FILE *f;
766 char line[1024];
767 int active, linenum;
768 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769
Damien Miller95def091999-11-25 00:26:21 +1100770 /* Open the file. */
771 f = fopen(filename, "r");
772 if (!f)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000773 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774
Damien Miller95def091999-11-25 00:26:21 +1100775 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776
Damien Miller5428f641999-11-25 11:54:57 +1100777 /*
778 * Mark that we are now processing the options. This flag is turned
779 * on/off by Host specifications.
780 */
Damien Miller95def091999-11-25 00:26:21 +1100781 active = 1;
782 linenum = 0;
783 while (fgets(line, sizeof(line), f)) {
784 /* Update line number counter. */
785 linenum++;
786 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
787 bad_options++;
788 }
789 fclose(f);
790 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000791 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100792 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000793 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794}
795
Damien Miller5428f641999-11-25 11:54:57 +1100796/*
797 * Initializes options to special values that indicate that they have not yet
798 * been set. Read_config_file will only set options with this value. Options
799 * are processed in the following order: command line, user config file,
800 * system config file. Last, fill_default_options is called.
801 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802
Damien Miller4af51302000-04-16 11:18:38 +1000803void
Damien Miller95def091999-11-25 00:26:21 +1100804initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805{
Damien Miller95def091999-11-25 00:26:21 +1100806 memset(options, 'X', sizeof(*options));
807 options->forward_agent = -1;
808 options->forward_x11 = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000809 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100810 options->gateway_ports = -1;
811 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100812 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000814 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000815 options->gss_authentication = -1;
816 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100817 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100818 options->kbd_interactive_authentication = -1;
819 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100820 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000821 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100822 options->batch_mode = -1;
823 options->check_host_ip = -1;
824 options->strict_host_key_checking = -1;
825 options->compression = -1;
826 options->keepalives = -1;
827 options->compression_level = -1;
828 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000829 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +1100830 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000831 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +1100832 options->number_of_password_prompts = -1;
833 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +1000834 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000835 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000836 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000837 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +1100838 options->num_identity_files = 0;
839 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000840 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100841 options->proxy_command = NULL;
842 options->user = NULL;
843 options->escape_char = -1;
844 options->system_hostfile = NULL;
845 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000846 options->system_hostfile2 = NULL;
847 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100848 options->num_local_forwards = 0;
849 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000850 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100851 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000852 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000853 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000854 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000855 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000856 options->no_host_authentication_for_localhost = - 1;
Damien Millera5539d22003-04-09 20:50:06 +1000857 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +1000858 options->verify_host_key_dns = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859}
860
Damien Miller5428f641999-11-25 11:54:57 +1100861/*
862 * Called after processing other sources of option data, this fills those
863 * options for which no value has been specified with their default values.
864 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865
Damien Miller4af51302000-04-16 11:18:38 +1000866void
Damien Miller95def091999-11-25 00:26:21 +1100867fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000869 int len;
870
Damien Miller95def091999-11-25 00:26:21 +1100871 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +1000872 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +1100873 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100874 options->forward_x11 = 0;
Damien Millerd3a18572000-06-07 19:55:44 +1000875 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000876 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100877 if (options->gateway_ports == -1)
878 options->gateway_ports = 0;
879 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +0000880 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +1100881 if (options->rsa_authentication == -1)
882 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100883 if (options->pubkey_authentication == -1)
884 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000885 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +0000886 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000887 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +1000888 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000889 if (options->gss_deleg_creds == -1)
890 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100891 if (options->password_authentication == -1)
892 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100893 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000894 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100895 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +0000896 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000897 if (options->hostbased_authentication == -1)
898 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100899 if (options->batch_mode == -1)
900 options->batch_mode = 0;
901 if (options->check_host_ip == -1)
902 options->check_host_ip = 1;
903 if (options->strict_host_key_checking == -1)
904 options->strict_host_key_checking = 2; /* 2 is default */
905 if (options->compression == -1)
906 options->compression = 0;
907 if (options->keepalives == -1)
908 options->keepalives = 1;
909 if (options->compression_level == -1)
910 options->compression_level = 6;
911 if (options->port == -1)
912 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000913 if (options->address_family == -1)
914 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +1100915 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000916 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100917 if (options->number_of_password_prompts == -1)
918 options->number_of_password_prompts = 3;
919 /* Selected in ssh_login(). */
920 if (options->cipher == -1)
921 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +1000922 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000923 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000924 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +1000925 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +0000926 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +1100927 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100928 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000929 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100930 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000931 xmalloc(len);
932 snprintf(options->identity_files[options->num_identity_files++],
933 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100934 }
935 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +0000936 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
937 options->identity_files[options->num_identity_files] =
938 xmalloc(len);
939 snprintf(options->identity_files[options->num_identity_files++],
940 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
941
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000942 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100943 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000944 xmalloc(len);
945 snprintf(options->identity_files[options->num_identity_files++],
946 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100947 }
Damien Millereba71ba2000-04-29 23:57:08 +1000948 }
Damien Miller95def091999-11-25 00:26:21 +1100949 if (options->escape_char == -1)
950 options->escape_char = '~';
951 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000952 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +1100953 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000954 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +1000955 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000956 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +1000957 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000958 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +1100959 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000960 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000961 if (options->clear_forwardings == 1)
962 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000963 if (options->no_host_authentication_for_localhost == - 1)
964 options->no_host_authentication_for_localhost = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000965 if (options->enable_ssh_keysign == -1)
966 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +1000967 if (options->rekey_limit == -1)
968 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000969 if (options->verify_host_key_dns == -1)
970 options->verify_host_key_dns = 0;
Damien Miller95def091999-11-25 00:26:21 +1100971 /* options->proxy_command should not be set by default */
972 /* options->user will be set in the main program if appropriate */
973 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000974 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000975 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976}