blob: c9c463b2948dea332268186443798c8c483e6020 [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"
Damien Miller37876e92003-05-15 10:19:46 +100015RCSID("$OpenBSD: readconf.c,v 1.107 2003/05/14 18:16:20 jakob 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
60 RhostsAuthentication no
61 PasswordAuthentication no
62
63 Host puukko.hut.fi
64 User t35124p
65 ProxyCommand ssh-proxy %h %p
66
67 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000068 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
70 Host *.su
71 Cipher none
72 PasswordAuthentication no
73
74 # Defaults for various options
75 Host *
76 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110077 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078 RhostsAuthentication yes
79 PasswordAuthentication yes
80 RSAAuthentication yes
81 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082 StrictHostKeyChecking yes
83 KeepAlives no
84 IdentityFile ~/.ssh/identity
85 Port 22
86 EscapeChar ~
87
88*/
89
90/* Keyword tokens. */
91
Damien Miller95def091999-11-25 00:26:21 +110092typedef enum {
93 oBadOption,
94 oForwardAgent, oForwardX11, oGatewayPorts, oRhostsAuthentication,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000095 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +000096 oChallengeResponseAuthentication, oXAuthLocation,
Ben Lindstromec95ed92001-07-04 04:21:14 +000097#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +110098 oKerberosAuthentication,
Ben Lindstromec95ed92001-07-04 04:21:14 +000099#endif
100#if defined(AFS) || defined(KRB5)
101 oKerberosTgtPassing,
102#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103#ifdef AFS
Ben Lindstromec95ed92001-07-04 04:21:14 +0000104 oAFSTokenPassing,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105#endif
Damien Miller95def091999-11-25 00:26:21 +1100106 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
107 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
108 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
109 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000110 oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000111 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100112 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000113 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000114 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000115 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000116 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Miller37876e92003-05-15 10:19:46 +1000117 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000118 oDeprecated
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119} OpCodes;
120
121/* Textual representations of the tokens. */
122
Damien Miller95def091999-11-25 00:26:21 +1100123static struct {
124 const char *name;
125 OpCodes opcode;
126} keywords[] = {
127 { "forwardagent", oForwardAgent },
128 { "forwardx11", oForwardX11 },
Damien Millerd3a18572000-06-07 19:55:44 +1000129 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100130 { "gatewayports", oGatewayPorts },
131 { "useprivilegedport", oUsePrivilegedPort },
132 { "rhostsauthentication", oRhostsAuthentication },
133 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100134 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
135 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100136 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000138 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000139 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000140 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000141 { "challengeresponseauthentication", oChallengeResponseAuthentication },
142 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
143 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000144#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100145 { "kerberosauthentication", oKerberosAuthentication },
Ben Lindstromec95ed92001-07-04 04:21:14 +0000146#endif
147#if defined(AFS) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100148 { "kerberostgtpassing", oKerberosTgtPassing },
Ben Lindstromec95ed92001-07-04 04:21:14 +0000149#endif
150#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100151 { "afstokenpassing", oAFSTokenPassing },
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000153 { "fallbacktorsh", oDeprecated },
154 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100155 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100156 { "identityfile2", oIdentityFile }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100157 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000158 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100159 { "proxycommand", oProxyCommand },
160 { "port", oPort },
161 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000162 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000163 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000164 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100165 { "remoteforward", oRemoteForward },
166 { "localforward", oLocalForward },
167 { "user", oUser },
168 { "host", oHost },
169 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000171 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000172 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000173 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100174 { "connectionattempts", oConnectionAttempts },
175 { "batchmode", oBatchMode },
176 { "checkhostip", oCheckHostIP },
177 { "stricthostkeychecking", oStrictHostKeyChecking },
178 { "compression", oCompression },
179 { "compressionlevel", oCompressionLevel },
180 { "keepalive", oKeepAlives },
181 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100182 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000183 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000184 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000185 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000186 { "bindaddress", oBindAddress },
Ben Lindstromae996bf2001-08-06 21:27:53 +0000187 { "smartcarddevice", oSmartcardDevice },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100188 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000189 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000190 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100191 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000192 { "rekeylimit", oRekeyLimit },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000193 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100194};
195
Damien Miller5428f641999-11-25 11:54:57 +1100196/*
197 * Adds a local TCP/IP port forward to options. Never returns if there is an
198 * error.
199 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Miller4af51302000-04-16 11:18:38 +1000201void
Damien Milleraae6c611999-12-06 11:47:28 +1100202add_local_forward(Options *options, u_short port, const char *host,
203 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204{
Damien Miller95def091999-11-25 00:26:21 +1100205 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000206#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100207 extern uid_t original_real_uid;
Damien Miller95def091999-11-25 00:26:21 +1100208 if (port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000209 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100210#endif
Damien Miller95def091999-11-25 00:26:21 +1100211 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
212 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
213 fwd = &options->local_forwards[options->num_local_forwards++];
214 fwd->port = port;
215 fwd->host = xstrdup(host);
216 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217}
218
Damien Miller5428f641999-11-25 11:54:57 +1100219/*
220 * Adds a remote TCP/IP port forward to options. Never returns if there is
221 * an error.
222 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller4af51302000-04-16 11:18:38 +1000224void
Damien Milleraae6c611999-12-06 11:47:28 +1100225add_remote_forward(Options *options, u_short port, const char *host,
226 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227{
Damien Miller95def091999-11-25 00:26:21 +1100228 Forward *fwd;
229 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
230 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100231 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100232 fwd = &options->remote_forwards[options->num_remote_forwards++];
233 fwd->port = port;
234 fwd->host = xstrdup(host);
235 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236}
237
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000238static void
239clear_forwardings(Options *options)
240{
241 int i;
242
243 for (i = 0; i < options->num_local_forwards; i++)
244 xfree(options->local_forwards[i].host);
245 options->num_local_forwards = 0;
246 for (i = 0; i < options->num_remote_forwards; i++)
247 xfree(options->remote_forwards[i].host);
248 options->num_remote_forwards = 0;
249}
250
Damien Miller5428f641999-11-25 11:54:57 +1100251/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000252 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100253 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller4af51302000-04-16 11:18:38 +1000255static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100256parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000258 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Miller95def091999-11-25 00:26:21 +1100260 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100261 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100262 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000264 error("%s: line %d: Bad configuration option: %s",
265 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100266 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267}
268
Damien Miller5428f641999-11-25 11:54:57 +1100269/*
270 * Processes a single option line as used in the configuration files. This
271 * only sets those values that have not already been set.
272 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100273#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274
Damien Miller2ccf6611999-11-15 15:25:10 +1100275int
276process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100277 char *line, const char *filename, int linenum,
278 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279{
Damien Miller61f08ac2003-02-24 11:56:27 +1100280 char buf[256], *s, **charptr, *endofnumber, *keyword, *arg;
Damien Milleraae6c611999-12-06 11:47:28 +1100281 int opcode, *intptr, value;
Damien Miller61f08ac2003-02-24 11:56:27 +1100282 size_t len;
Damien Milleraae6c611999-12-06 11:47:28 +1100283 u_short fwd_port, fwd_host_port;
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000284 char sfwd_host_port[6];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Millerc652cac2003-05-14 13:40:54 +1000286 /* Strip trailing whitespace */
287 for(len = strlen(line) - 1; len > 0; len--) {
288 if (strchr(WHITESPACE, line[len]) == NULL)
289 break;
290 line[len] = '\0';
291 }
292
Damien Millerbe484b52000-07-15 14:14:16 +1000293 s = line;
294 /* Get the keyword. (Each line is supposed to begin with a keyword). */
295 keyword = strdelim(&s);
296 /* Ignore leading whitespace. */
297 if (*keyword == '\0')
298 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000299 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100300 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301
Damien Miller37023962000-07-11 17:31:38 +1000302 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 switch (opcode) {
305 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100306 /* don't panic, but count bad options */
307 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100308 /* NOTREACHED */
309 case oForwardAgent:
310 intptr = &options->forward_agent;
311parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000312 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000313 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100314 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
315 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000316 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100317 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000318 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100319 value = 0;
320 else
321 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
322 if (*activep && *intptr == -1)
323 *intptr = value;
324 break;
325
326 case oForwardX11:
327 intptr = &options->forward_x11;
328 goto parse_flag;
329
330 case oGatewayPorts:
331 intptr = &options->gateway_ports;
332 goto parse_flag;
333
334 case oUsePrivilegedPort:
335 intptr = &options->use_privileged_port;
336 goto parse_flag;
337
338 case oRhostsAuthentication:
339 intptr = &options->rhosts_authentication;
340 goto parse_flag;
341
342 case oPasswordAuthentication:
343 intptr = &options->password_authentication;
344 goto parse_flag;
345
Damien Miller874d77b2000-10-14 16:23:11 +1100346 case oKbdInteractiveAuthentication:
347 intptr = &options->kbd_interactive_authentication;
348 goto parse_flag;
349
350 case oKbdInteractiveDevices:
351 charptr = &options->kbd_interactive_devices;
352 goto parse_string;
353
Damien Miller0bc1bd82000-11-13 22:57:25 +1100354 case oPubkeyAuthentication:
355 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000356 goto parse_flag;
357
Damien Miller95def091999-11-25 00:26:21 +1100358 case oRSAAuthentication:
359 intptr = &options->rsa_authentication;
360 goto parse_flag;
361
362 case oRhostsRSAAuthentication:
363 intptr = &options->rhosts_rsa_authentication;
364 goto parse_flag;
365
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000366 case oHostbasedAuthentication:
367 intptr = &options->hostbased_authentication;
368 goto parse_flag;
369
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000370 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000371 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100372 goto parse_flag;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000373#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100374 case oKerberosAuthentication:
375 intptr = &options->kerberos_authentication;
376 goto parse_flag;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000377#endif
378#if defined(AFS) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100379 case oKerberosTgtPassing:
380 intptr = &options->kerberos_tgt_passing;
381 goto parse_flag;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000382#endif
383#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100384 case oAFSTokenPassing:
385 intptr = &options->afs_token_passing;
386 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387#endif
Damien Miller95def091999-11-25 00:26:21 +1100388 case oBatchMode:
389 intptr = &options->batch_mode;
390 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Miller95def091999-11-25 00:26:21 +1100392 case oCheckHostIP:
393 intptr = &options->check_host_ip;
394 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller37876e92003-05-15 10:19:46 +1000396 case oVerifyHostKeyDNS:
397 intptr = &options->verify_host_key_dns;
398 goto parse_flag;
399
Damien Miller95def091999-11-25 00:26:21 +1100400 case oStrictHostKeyChecking:
401 intptr = &options->strict_host_key_checking;
Damien Millerbe484b52000-07-15 14:14:16 +1000402 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000403 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000404 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100405 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100406 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000407 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100408 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000409 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100410 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000411 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100412 value = 2;
413 else
414 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
415 if (*activep && *intptr == -1)
416 *intptr = value;
417 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller95def091999-11-25 00:26:21 +1100419 case oCompression:
420 intptr = &options->compression;
421 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Damien Miller95def091999-11-25 00:26:21 +1100423 case oKeepAlives:
424 intptr = &options->keepalives;
425 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000427 case oNoHostAuthenticationForLocalhost:
428 intptr = &options->no_host_authentication_for_localhost;
429 goto parse_flag;
430
Damien Miller95def091999-11-25 00:26:21 +1100431 case oNumberOfPasswordPrompts:
432 intptr = &options->number_of_password_prompts;
433 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Damien Miller95def091999-11-25 00:26:21 +1100435 case oCompressionLevel:
436 intptr = &options->compression_level;
437 goto parse_int;
438
Damien Millera5539d22003-04-09 20:50:06 +1000439 case oRekeyLimit:
440 intptr = &options->rekey_limit;
441 arg = strdelim(&s);
442 if (!arg || *arg == '\0')
443 fatal("%.200s line %d: Missing argument.", filename, linenum);
444 if (arg[0] < '0' || arg[0] > '9')
445 fatal("%.200s line %d: Bad number.", filename, linenum);
446 value = strtol(arg, &endofnumber, 10);
447 if (arg == endofnumber)
448 fatal("%.200s line %d: Bad number.", filename, linenum);
449 switch (toupper(*endofnumber)) {
450 case 'K':
451 value *= 1<<10;
452 break;
453 case 'M':
454 value *= 1<<20;
455 break;
456 case 'G':
457 value *= 1<<30;
458 break;
459 }
460 if (*activep && *intptr == -1)
461 *intptr = value;
462 break;
463
Damien Miller95def091999-11-25 00:26:21 +1100464 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000465 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000466 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100467 fatal("%.200s line %d: Missing argument.", filename, linenum);
468 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000470 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100471 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100472 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000474 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000475 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100476 }
477 break;
478
Damien Millerd3a18572000-06-07 19:55:44 +1000479 case oXAuthLocation:
480 charptr=&options->xauth_location;
481 goto parse_string;
482
Damien Miller95def091999-11-25 00:26:21 +1100483 case oUser:
484 charptr = &options->user;
485parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000486 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000487 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100488 fatal("%.200s line %d: Missing argument.", filename, linenum);
489 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000490 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100491 break;
492
493 case oGlobalKnownHostsFile:
494 charptr = &options->system_hostfile;
495 goto parse_string;
496
497 case oUserKnownHostsFile:
498 charptr = &options->user_hostfile;
499 goto parse_string;
500
Damien Millereba71ba2000-04-29 23:57:08 +1000501 case oGlobalKnownHostsFile2:
502 charptr = &options->system_hostfile2;
503 goto parse_string;
504
505 case oUserKnownHostsFile2:
506 charptr = &options->user_hostfile2;
507 goto parse_string;
508
Damien Miller95def091999-11-25 00:26:21 +1100509 case oHostName:
510 charptr = &options->hostname;
511 goto parse_string;
512
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000513 case oHostKeyAlias:
514 charptr = &options->host_key_alias;
515 goto parse_string;
516
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000517 case oPreferredAuthentications:
518 charptr = &options->preferred_authentications;
519 goto parse_string;
520
Ben Lindstrome0f88042001-04-30 13:06:24 +0000521 case oBindAddress:
522 charptr = &options->bind_address;
523 goto parse_string;
524
Ben Lindstromae996bf2001-08-06 21:27:53 +0000525 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000526 charptr = &options->smartcard_device;
527 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000528
Damien Miller95def091999-11-25 00:26:21 +1100529 case oProxyCommand:
530 charptr = &options->proxy_command;
Damien Miller61f08ac2003-02-24 11:56:27 +1100531 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100532 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100533 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100534 return 0;
535
536 case oPort:
537 intptr = &options->port;
538parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000539 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000540 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100541 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000542 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100543 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100544
545 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000546 value = strtol(arg, &endofnumber, 0);
547 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100548 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100549 if (*activep && *intptr == -1)
550 *intptr = value;
551 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552
Damien Miller95def091999-11-25 00:26:21 +1100553 case oConnectionAttempts:
554 intptr = &options->connection_attempts;
555 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100556
Damien Miller95def091999-11-25 00:26:21 +1100557 case oCipher:
558 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000559 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000560 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000561 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000562 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100563 if (value == -1)
564 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100565 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100566 if (*activep && *intptr == -1)
567 *intptr = value;
568 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Damien Miller78928792000-04-12 20:17:38 +1000570 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000571 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000572 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000573 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000574 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000575 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100576 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000577 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000578 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000579 break;
580
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000581 case oMacs:
582 arg = strdelim(&s);
583 if (!arg || *arg == '\0')
584 fatal("%.200s line %d: Missing argument.", filename, linenum);
585 if (!mac_valid(arg))
586 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100587 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000588 if (*activep && options->macs == NULL)
589 options->macs = xstrdup(arg);
590 break;
591
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000592 case oHostKeyAlgorithms:
593 arg = strdelim(&s);
594 if (!arg || *arg == '\0')
595 fatal("%.200s line %d: Missing argument.", filename, linenum);
596 if (!key_names_valid2(arg))
597 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100598 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000599 if (*activep && options->hostkeyalgorithms == NULL)
600 options->hostkeyalgorithms = xstrdup(arg);
601 break;
602
Damien Miller78928792000-04-12 20:17:38 +1000603 case oProtocol:
604 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000605 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000606 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000607 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000608 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000609 if (value == SSH_PROTO_UNKNOWN)
610 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100611 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000612 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
613 *intptr = value;
614 break;
615
Damien Miller95def091999-11-25 00:26:21 +1100616 case oLogLevel:
617 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000618 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000619 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100620 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000621 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100622 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100623 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100624 *intptr = (LogLevel) value;
625 break;
626
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000627 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100628 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000629 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000630 if (!arg || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000631 fatal("%.200s line %d: Missing port argument.",
632 filename, linenum);
633 if ((fwd_port = a2port(arg)) == 0)
634 fatal("%.200s line %d: Bad listen port.",
635 filename, linenum);
Damien Millerbe484b52000-07-15 14:14:16 +1000636 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000637 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100638 fatal("%.200s line %d: Missing second argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000639 filename, linenum);
640 if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 &&
641 sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2)
642 fatal("%.200s line %d: Bad forwarding specification.",
643 filename, linenum);
644 if ((fwd_host_port = a2port(sfwd_host_port)) == 0)
645 fatal("%.200s line %d: Bad forwarding port.",
646 filename, linenum);
647 if (*activep) {
648 if (opcode == oLocalForward)
649 add_local_forward(options, fwd_port, buf,
650 fwd_host_port);
651 else if (opcode == oRemoteForward)
652 add_remote_forward(options, fwd_port, buf,
653 fwd_host_port);
654 }
Damien Miller95def091999-11-25 00:26:21 +1100655 break;
656
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000657 case oDynamicForward:
658 arg = strdelim(&s);
659 if (!arg || *arg == '\0')
660 fatal("%.200s line %d: Missing port argument.",
661 filename, linenum);
Ben Lindstrom19066a12001-04-12 23:39:26 +0000662 fwd_port = a2port(arg);
663 if (fwd_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000664 fatal("%.200s line %d: Badly formatted port number.",
665 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000666 if (*activep)
667 add_local_forward(options, fwd_port, "socks4", 0);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000668 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000669
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000670 case oClearAllForwardings:
671 intptr = &options->clear_forwardings;
672 goto parse_flag;
673
Damien Miller95def091999-11-25 00:26:21 +1100674 case oHost:
675 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000676 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000677 if (match_pattern(host, arg)) {
678 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100679 *activep = 1;
680 break;
681 }
Damien Millerbe484b52000-07-15 14:14:16 +1000682 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100683 return 0;
684
685 case oEscapeChar:
686 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000687 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000688 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100689 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000690 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000691 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
692 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000693 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000694 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000695 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000696 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100697 else {
698 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100699 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100700 /* NOTREACHED */
701 value = 0; /* Avoid compiler warning. */
702 }
703 if (*activep && *intptr == -1)
704 *intptr = value;
705 break;
706
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000707 case oEnableSSHKeysign:
708 intptr = &options->enable_ssh_keysign;
709 goto parse_flag;
710
Ben Lindstrom4daea862002-06-09 20:04:02 +0000711 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000712 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000713 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000714 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000715
Damien Miller95def091999-11-25 00:26:21 +1100716 default:
717 fatal("process_config_line: Unimplemented opcode %d", opcode);
718 }
719
720 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000721 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000722 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100723 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000724 }
Damien Miller95def091999-11-25 00:26:21 +1100725 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726}
727
728
Damien Miller5428f641999-11-25 11:54:57 +1100729/*
730 * Reads the config file and modifies the options accordingly. Options
731 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000732 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100733 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000735int
Damien Miller95def091999-11-25 00:26:21 +1100736read_config_file(const char *filename, const char *host, Options *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737{
Damien Miller95def091999-11-25 00:26:21 +1100738 FILE *f;
739 char line[1024];
740 int active, linenum;
741 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742
Damien Miller95def091999-11-25 00:26:21 +1100743 /* Open the file. */
744 f = fopen(filename, "r");
745 if (!f)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000746 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
Damien Miller95def091999-11-25 00:26:21 +1100748 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749
Damien Miller5428f641999-11-25 11:54:57 +1100750 /*
751 * Mark that we are now processing the options. This flag is turned
752 * on/off by Host specifications.
753 */
Damien Miller95def091999-11-25 00:26:21 +1100754 active = 1;
755 linenum = 0;
756 while (fgets(line, sizeof(line), f)) {
757 /* Update line number counter. */
758 linenum++;
759 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
760 bad_options++;
761 }
762 fclose(f);
763 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000764 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100765 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000766 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767}
768
Damien Miller5428f641999-11-25 11:54:57 +1100769/*
770 * Initializes options to special values that indicate that they have not yet
771 * been set. Read_config_file will only set options with this value. Options
772 * are processed in the following order: command line, user config file,
773 * system config file. Last, fill_default_options is called.
774 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775
Damien Miller4af51302000-04-16 11:18:38 +1000776void
Damien Miller95def091999-11-25 00:26:21 +1100777initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778{
Damien Miller95def091999-11-25 00:26:21 +1100779 memset(options, 'X', sizeof(*options));
780 options->forward_agent = -1;
781 options->forward_x11 = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000782 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100783 options->gateway_ports = -1;
784 options->use_privileged_port = -1;
785 options->rhosts_authentication = -1;
786 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100787 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000788 options->challenge_response_authentication = -1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000789#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100790 options->kerberos_authentication = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000792#if defined(AFS) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100793 options->kerberos_tgt_passing = -1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000794#endif
795#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100796 options->afs_token_passing = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797#endif
Damien Miller95def091999-11-25 00:26:21 +1100798 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100799 options->kbd_interactive_authentication = -1;
800 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100801 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000802 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100803 options->batch_mode = -1;
804 options->check_host_ip = -1;
805 options->strict_host_key_checking = -1;
806 options->compression = -1;
807 options->keepalives = -1;
808 options->compression_level = -1;
809 options->port = -1;
810 options->connection_attempts = -1;
811 options->number_of_password_prompts = -1;
812 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +1000813 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000814 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000815 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000816 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +1100817 options->num_identity_files = 0;
818 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000819 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100820 options->proxy_command = NULL;
821 options->user = NULL;
822 options->escape_char = -1;
823 options->system_hostfile = NULL;
824 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000825 options->system_hostfile2 = NULL;
826 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100827 options->num_local_forwards = 0;
828 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000829 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100830 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000831 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000832 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000833 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000834 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000835 options->no_host_authentication_for_localhost = - 1;
Damien Millera5539d22003-04-09 20:50:06 +1000836 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +1000837 options->verify_host_key_dns = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838}
839
Damien Miller5428f641999-11-25 11:54:57 +1100840/*
841 * Called after processing other sources of option data, this fills those
842 * options for which no value has been specified with their default values.
843 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844
Damien Miller4af51302000-04-16 11:18:38 +1000845void
Damien Miller95def091999-11-25 00:26:21 +1100846fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000848 int len;
849
Damien Miller95def091999-11-25 00:26:21 +1100850 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +1000851 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +1100852 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100853 options->forward_x11 = 0;
Damien Millerd3a18572000-06-07 19:55:44 +1000854 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000855 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100856 if (options->gateway_ports == -1)
857 options->gateway_ports = 0;
858 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +0000859 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +1100860 if (options->rhosts_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +0000861 options->rhosts_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100862 if (options->rsa_authentication == -1)
863 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100864 if (options->pubkey_authentication == -1)
865 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000866 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +0000867 options->challenge_response_authentication = 1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000868#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100869 if (options->kerberos_authentication == -1)
870 options->kerberos_authentication = 1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000871#endif
872#if defined(AFS) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100873 if (options->kerberos_tgt_passing == -1)
874 options->kerberos_tgt_passing = 1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000875#endif
876#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100877 if (options->afs_token_passing == -1)
878 options->afs_token_passing = 1;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000879#endif
Damien Miller95def091999-11-25 00:26:21 +1100880 if (options->password_authentication == -1)
881 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100882 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000883 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100884 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +0000885 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000886 if (options->hostbased_authentication == -1)
887 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100888 if (options->batch_mode == -1)
889 options->batch_mode = 0;
890 if (options->check_host_ip == -1)
891 options->check_host_ip = 1;
892 if (options->strict_host_key_checking == -1)
893 options->strict_host_key_checking = 2; /* 2 is default */
894 if (options->compression == -1)
895 options->compression = 0;
896 if (options->keepalives == -1)
897 options->keepalives = 1;
898 if (options->compression_level == -1)
899 options->compression_level = 6;
900 if (options->port == -1)
901 options->port = 0; /* Filled in ssh_connect. */
902 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000903 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100904 if (options->number_of_password_prompts == -1)
905 options->number_of_password_prompts = 3;
906 /* Selected in ssh_login(). */
907 if (options->cipher == -1)
908 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +1000909 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000910 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000911 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +1000912 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +0000913 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +1100914 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100915 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000916 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100917 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000918 xmalloc(len);
919 snprintf(options->identity_files[options->num_identity_files++],
920 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100921 }
922 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +0000923 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
924 options->identity_files[options->num_identity_files] =
925 xmalloc(len);
926 snprintf(options->identity_files[options->num_identity_files++],
927 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
928
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000929 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 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_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100934 }
Damien Millereba71ba2000-04-29 23:57:08 +1000935 }
Damien Miller95def091999-11-25 00:26:21 +1100936 if (options->escape_char == -1)
937 options->escape_char = '~';
938 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000939 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +1100940 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000941 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +1000942 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000943 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +1000944 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000945 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +1100946 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000947 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000948 if (options->clear_forwardings == 1)
949 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000950 if (options->no_host_authentication_for_localhost == - 1)
951 options->no_host_authentication_for_localhost = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000952 if (options->enable_ssh_keysign == -1)
953 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +1000954 if (options->rekey_limit == -1)
955 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000956 if (options->verify_host_key_dns == -1)
957 options->verify_host_key_dns = 0;
Damien Miller95def091999-11-25 00:26:21 +1100958 /* options->proxy_command should not be set by default */
959 /* options->user will be set in the main program if appropriate */
960 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000961 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000962 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963}