blob: 2a77ea14fd9eaecfa83a083b5f1470de64e54f10 [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 Miller20a8f972003-05-18 20:50:30 +100015RCSID("$OpenBSD: readconf.c,v 1.112 2003/05/16 03:27:12 djm 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,
Damien Miller2aa0ab42003-05-15 12:05:28 +100097 oKerberosAuthentication, oKerberosTgtPassing, oAFSTokenPassing,
Damien Miller95def091999-11-25 00:26:21 +110098 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
99 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
100 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
101 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000102 oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000103 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100104 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000105 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000106 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000107 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000108 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000109 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Damien Miller20a8f972003-05-18 20:50:30 +1000110 oAddressFamily,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000111 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112} OpCodes;
113
114/* Textual representations of the tokens. */
115
Damien Miller95def091999-11-25 00:26:21 +1100116static struct {
117 const char *name;
118 OpCodes opcode;
119} keywords[] = {
120 { "forwardagent", oForwardAgent },
121 { "forwardx11", oForwardX11 },
Damien Millerd3a18572000-06-07 19:55:44 +1000122 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100123 { "gatewayports", oGatewayPorts },
124 { "useprivilegedport", oUsePrivilegedPort },
125 { "rhostsauthentication", oRhostsAuthentication },
126 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100127 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
128 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100129 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100130 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000131 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000132 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000133 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000134 { "challengeresponseauthentication", oChallengeResponseAuthentication },
135 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
136 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000137#if defined(KRB4) || defined(KRB5)
Damien Miller95def091999-11-25 00:26:21 +1100138 { "kerberosauthentication", oKerberosAuthentication },
Damien Miller95def091999-11-25 00:26:21 +1100139 { "kerberostgtpassing", oKerberosTgtPassing },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000140#else
141 { "kerberosauthentication", oUnsupported },
142 { "kerberostgtpassing", oUnsupported },
143#endif
144#if defined(AFS)
Damien Miller95def091999-11-25 00:26:21 +1100145 { "afstokenpassing", oAFSTokenPassing },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000146#else
147 { "afstokenpassing", oUnsupported },
148#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000149 { "fallbacktorsh", oDeprecated },
150 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100151 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100152 { "identityfile2", oIdentityFile }, /* alias */
Damien Miller95def091999-11-25 00:26:21 +1100153 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000154 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100155 { "proxycommand", oProxyCommand },
156 { "port", oPort },
157 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000158 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000159 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000160 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100161 { "remoteforward", oRemoteForward },
162 { "localforward", oLocalForward },
163 { "user", oUser },
164 { "host", oHost },
165 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100166 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000167 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000168 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000169 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100170 { "connectionattempts", oConnectionAttempts },
171 { "batchmode", oBatchMode },
172 { "checkhostip", oCheckHostIP },
173 { "stricthostkeychecking", oStrictHostKeyChecking },
174 { "compression", oCompression },
175 { "compressionlevel", oCompressionLevel },
176 { "keepalive", oKeepAlives },
177 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100178 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000179 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000180 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000181 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000182 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000183#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000184 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000185#else
186 { "smartcarddevice", oUnsupported },
187#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100188 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000189 { "enablesshkeysign", oEnableSSHKeysign },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000190#ifdef DNS
Damien Miller37876e92003-05-15 10:19:46 +1000191 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000192#else
193 { "verifyhostkeydns", oUnsupported },
194#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100195 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000196 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000197 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000198 { "addressfamily", oAddressFamily },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000199 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100200};
201
Damien Miller5428f641999-11-25 11:54:57 +1100202/*
203 * Adds a local TCP/IP port forward to options. Never returns if there is an
204 * error.
205 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Miller4af51302000-04-16 11:18:38 +1000207void
Damien Milleraae6c611999-12-06 11:47:28 +1100208add_local_forward(Options *options, u_short port, const char *host,
209 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210{
Damien Miller95def091999-11-25 00:26:21 +1100211 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000212#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100213 extern uid_t original_real_uid;
Damien Miller95def091999-11-25 00:26:21 +1100214 if (port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000215 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100216#endif
Damien Miller95def091999-11-25 00:26:21 +1100217 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
218 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
219 fwd = &options->local_forwards[options->num_local_forwards++];
220 fwd->port = port;
221 fwd->host = xstrdup(host);
222 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223}
224
Damien Miller5428f641999-11-25 11:54:57 +1100225/*
226 * Adds a remote TCP/IP port forward to options. Never returns if there is
227 * an error.
228 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
Damien Miller4af51302000-04-16 11:18:38 +1000230void
Damien Milleraae6c611999-12-06 11:47:28 +1100231add_remote_forward(Options *options, u_short port, const char *host,
232 u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233{
Damien Miller95def091999-11-25 00:26:21 +1100234 Forward *fwd;
235 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
236 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100237 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100238 fwd = &options->remote_forwards[options->num_remote_forwards++];
239 fwd->port = port;
240 fwd->host = xstrdup(host);
241 fwd->host_port = host_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242}
243
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000244static void
245clear_forwardings(Options *options)
246{
247 int i;
248
249 for (i = 0; i < options->num_local_forwards; i++)
250 xfree(options->local_forwards[i].host);
251 options->num_local_forwards = 0;
252 for (i = 0; i < options->num_remote_forwards; i++)
253 xfree(options->remote_forwards[i].host);
254 options->num_remote_forwards = 0;
255}
256
Damien Miller5428f641999-11-25 11:54:57 +1100257/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000258 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100259 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller4af51302000-04-16 11:18:38 +1000261static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100262parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000264 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265
Damien Miller95def091999-11-25 00:26:21 +1100266 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100267 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100268 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000270 error("%s: line %d: Bad configuration option: %s",
271 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100272 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
274
Damien Miller5428f641999-11-25 11:54:57 +1100275/*
276 * Processes a single option line as used in the configuration files. This
277 * only sets those values that have not already been set.
278 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100279#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280
Damien Miller2ccf6611999-11-15 15:25:10 +1100281int
282process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100283 char *line, const char *filename, int linenum,
284 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285{
Damien Miller61f08ac2003-02-24 11:56:27 +1100286 char buf[256], *s, **charptr, *endofnumber, *keyword, *arg;
Damien Milleraae6c611999-12-06 11:47:28 +1100287 int opcode, *intptr, value;
Damien Miller61f08ac2003-02-24 11:56:27 +1100288 size_t len;
Damien Milleraae6c611999-12-06 11:47:28 +1100289 u_short fwd_port, fwd_host_port;
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000290 char sfwd_host_port[6];
Damien Miller20a8f972003-05-18 20:50:30 +1000291 extern int IPv4or6;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Millerc652cac2003-05-14 13:40:54 +1000293 /* Strip trailing whitespace */
294 for(len = strlen(line) - 1; len > 0; len--) {
295 if (strchr(WHITESPACE, line[len]) == NULL)
296 break;
297 line[len] = '\0';
298 }
299
Damien Millerbe484b52000-07-15 14:14:16 +1000300 s = line;
301 /* Get the keyword. (Each line is supposed to begin with a keyword). */
302 keyword = strdelim(&s);
303 /* Ignore leading whitespace. */
304 if (*keyword == '\0')
305 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000306 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100307 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Miller37023962000-07-11 17:31:38 +1000309 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Damien Miller95def091999-11-25 00:26:21 +1100311 switch (opcode) {
312 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100313 /* don't panic, but count bad options */
314 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100315 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000316 case oConnectTimeout:
317 intptr = &options->connection_timeout;
318/* parse_time: */
319 arg = strdelim(&s);
320 if (!arg || *arg == '\0')
321 fatal("%s line %d: missing time value.",
322 filename, linenum);
323 if ((value = convtime(arg)) == -1)
324 fatal("%s line %d: invalid time value.",
325 filename, linenum);
326 if (*intptr == -1)
327 *intptr = value;
328 break;
329
Damien Miller95def091999-11-25 00:26:21 +1100330 case oForwardAgent:
331 intptr = &options->forward_agent;
332parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000333 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000334 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100335 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
336 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000337 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100338 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000339 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100340 value = 0;
341 else
342 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
343 if (*activep && *intptr == -1)
344 *intptr = value;
345 break;
346
347 case oForwardX11:
348 intptr = &options->forward_x11;
349 goto parse_flag;
350
351 case oGatewayPorts:
352 intptr = &options->gateway_ports;
353 goto parse_flag;
354
355 case oUsePrivilegedPort:
356 intptr = &options->use_privileged_port;
357 goto parse_flag;
358
359 case oRhostsAuthentication:
360 intptr = &options->rhosts_authentication;
361 goto parse_flag;
362
363 case oPasswordAuthentication:
364 intptr = &options->password_authentication;
365 goto parse_flag;
366
Damien Miller874d77b2000-10-14 16:23:11 +1100367 case oKbdInteractiveAuthentication:
368 intptr = &options->kbd_interactive_authentication;
369 goto parse_flag;
370
371 case oKbdInteractiveDevices:
372 charptr = &options->kbd_interactive_devices;
373 goto parse_string;
374
Damien Miller0bc1bd82000-11-13 22:57:25 +1100375 case oPubkeyAuthentication:
376 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000377 goto parse_flag;
378
Damien Miller95def091999-11-25 00:26:21 +1100379 case oRSAAuthentication:
380 intptr = &options->rsa_authentication;
381 goto parse_flag;
382
383 case oRhostsRSAAuthentication:
384 intptr = &options->rhosts_rsa_authentication;
385 goto parse_flag;
386
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000387 case oHostbasedAuthentication:
388 intptr = &options->hostbased_authentication;
389 goto parse_flag;
390
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000391 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000392 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100393 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000394
Damien Miller95def091999-11-25 00:26:21 +1100395 case oKerberosAuthentication:
396 intptr = &options->kerberos_authentication;
397 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000398
Damien Miller95def091999-11-25 00:26:21 +1100399 case oKerberosTgtPassing:
400 intptr = &options->kerberos_tgt_passing;
401 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000402
Damien Miller95def091999-11-25 00:26:21 +1100403 case oAFSTokenPassing:
404 intptr = &options->afs_token_passing;
405 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000406
Damien Miller95def091999-11-25 00:26:21 +1100407 case oBatchMode:
408 intptr = &options->batch_mode;
409 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Damien Miller95def091999-11-25 00:26:21 +1100411 case oCheckHostIP:
412 intptr = &options->check_host_ip;
413 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller37876e92003-05-15 10:19:46 +1000415 case oVerifyHostKeyDNS:
416 intptr = &options->verify_host_key_dns;
417 goto parse_flag;
418
Damien Miller95def091999-11-25 00:26:21 +1100419 case oStrictHostKeyChecking:
420 intptr = &options->strict_host_key_checking;
Damien Millerbe484b52000-07-15 14:14:16 +1000421 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000422 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000423 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100424 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100425 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000426 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100427 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000428 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100429 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000430 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100431 value = 2;
432 else
433 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
434 if (*activep && *intptr == -1)
435 *intptr = value;
436 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Damien Miller95def091999-11-25 00:26:21 +1100438 case oCompression:
439 intptr = &options->compression;
440 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Damien Miller95def091999-11-25 00:26:21 +1100442 case oKeepAlives:
443 intptr = &options->keepalives;
444 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000446 case oNoHostAuthenticationForLocalhost:
447 intptr = &options->no_host_authentication_for_localhost;
448 goto parse_flag;
449
Damien Miller95def091999-11-25 00:26:21 +1100450 case oNumberOfPasswordPrompts:
451 intptr = &options->number_of_password_prompts;
452 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453
Damien Miller95def091999-11-25 00:26:21 +1100454 case oCompressionLevel:
455 intptr = &options->compression_level;
456 goto parse_int;
457
Damien Millera5539d22003-04-09 20:50:06 +1000458 case oRekeyLimit:
459 intptr = &options->rekey_limit;
460 arg = strdelim(&s);
461 if (!arg || *arg == '\0')
462 fatal("%.200s line %d: Missing argument.", filename, linenum);
463 if (arg[0] < '0' || arg[0] > '9')
464 fatal("%.200s line %d: Bad number.", filename, linenum);
465 value = strtol(arg, &endofnumber, 10);
466 if (arg == endofnumber)
467 fatal("%.200s line %d: Bad number.", filename, linenum);
468 switch (toupper(*endofnumber)) {
469 case 'K':
470 value *= 1<<10;
471 break;
472 case 'M':
473 value *= 1<<20;
474 break;
475 case 'G':
476 value *= 1<<30;
477 break;
478 }
479 if (*activep && *intptr == -1)
480 *intptr = value;
481 break;
482
Damien Miller95def091999-11-25 00:26:21 +1100483 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000484 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000485 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100486 fatal("%.200s line %d: Missing argument.", filename, linenum);
487 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000489 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100490 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100491 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000493 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000494 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100495 }
496 break;
497
Damien Millerd3a18572000-06-07 19:55:44 +1000498 case oXAuthLocation:
499 charptr=&options->xauth_location;
500 goto parse_string;
501
Damien Miller95def091999-11-25 00:26:21 +1100502 case oUser:
503 charptr = &options->user;
504parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000505 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000506 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100507 fatal("%.200s line %d: Missing argument.", filename, linenum);
508 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000509 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100510 break;
511
512 case oGlobalKnownHostsFile:
513 charptr = &options->system_hostfile;
514 goto parse_string;
515
516 case oUserKnownHostsFile:
517 charptr = &options->user_hostfile;
518 goto parse_string;
519
Damien Millereba71ba2000-04-29 23:57:08 +1000520 case oGlobalKnownHostsFile2:
521 charptr = &options->system_hostfile2;
522 goto parse_string;
523
524 case oUserKnownHostsFile2:
525 charptr = &options->user_hostfile2;
526 goto parse_string;
527
Damien Miller95def091999-11-25 00:26:21 +1100528 case oHostName:
529 charptr = &options->hostname;
530 goto parse_string;
531
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000532 case oHostKeyAlias:
533 charptr = &options->host_key_alias;
534 goto parse_string;
535
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000536 case oPreferredAuthentications:
537 charptr = &options->preferred_authentications;
538 goto parse_string;
539
Ben Lindstrome0f88042001-04-30 13:06:24 +0000540 case oBindAddress:
541 charptr = &options->bind_address;
542 goto parse_string;
543
Ben Lindstromae996bf2001-08-06 21:27:53 +0000544 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000545 charptr = &options->smartcard_device;
546 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000547
Damien Miller95def091999-11-25 00:26:21 +1100548 case oProxyCommand:
549 charptr = &options->proxy_command;
Damien Miller61f08ac2003-02-24 11:56:27 +1100550 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100551 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100552 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100553 return 0;
554
555 case oPort:
556 intptr = &options->port;
557parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000558 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000559 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100560 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000561 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100562 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100563
564 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000565 value = strtol(arg, &endofnumber, 0);
566 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100567 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100568 if (*activep && *intptr == -1)
569 *intptr = value;
570 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
Damien Miller95def091999-11-25 00:26:21 +1100572 case oConnectionAttempts:
573 intptr = &options->connection_attempts;
574 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100575
Damien Miller95def091999-11-25 00:26:21 +1100576 case oCipher:
577 intptr = &options->cipher;
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 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100582 if (value == -1)
583 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100584 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100585 if (*activep && *intptr == -1)
586 *intptr = value;
587 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588
Damien Miller78928792000-04-12 20:17:38 +1000589 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000590 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000591 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000592 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000593 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000594 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100595 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000596 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000597 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000598 break;
599
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000600 case oMacs:
601 arg = strdelim(&s);
602 if (!arg || *arg == '\0')
603 fatal("%.200s line %d: Missing argument.", filename, linenum);
604 if (!mac_valid(arg))
605 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100606 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000607 if (*activep && options->macs == NULL)
608 options->macs = xstrdup(arg);
609 break;
610
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000611 case oHostKeyAlgorithms:
612 arg = strdelim(&s);
613 if (!arg || *arg == '\0')
614 fatal("%.200s line %d: Missing argument.", filename, linenum);
615 if (!key_names_valid2(arg))
616 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100617 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000618 if (*activep && options->hostkeyalgorithms == NULL)
619 options->hostkeyalgorithms = xstrdup(arg);
620 break;
621
Damien Miller78928792000-04-12 20:17:38 +1000622 case oProtocol:
623 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000624 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000625 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000626 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000627 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000628 if (value == SSH_PROTO_UNKNOWN)
629 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100630 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000631 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
632 *intptr = value;
633 break;
634
Damien Miller95def091999-11-25 00:26:21 +1100635 case oLogLevel:
636 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000637 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000638 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100639 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000640 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100641 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100642 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100643 *intptr = (LogLevel) value;
644 break;
645
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000646 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100647 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000648 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000649 if (!arg || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000650 fatal("%.200s line %d: Missing port argument.",
651 filename, linenum);
652 if ((fwd_port = a2port(arg)) == 0)
653 fatal("%.200s line %d: Bad listen port.",
654 filename, linenum);
Damien Millerbe484b52000-07-15 14:14:16 +1000655 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000656 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100657 fatal("%.200s line %d: Missing second argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000658 filename, linenum);
659 if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 &&
660 sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2)
661 fatal("%.200s line %d: Bad forwarding specification.",
662 filename, linenum);
663 if ((fwd_host_port = a2port(sfwd_host_port)) == 0)
664 fatal("%.200s line %d: Bad forwarding port.",
665 filename, linenum);
666 if (*activep) {
667 if (opcode == oLocalForward)
668 add_local_forward(options, fwd_port, buf,
669 fwd_host_port);
670 else if (opcode == oRemoteForward)
671 add_remote_forward(options, fwd_port, buf,
672 fwd_host_port);
673 }
Damien Miller95def091999-11-25 00:26:21 +1100674 break;
675
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000676 case oDynamicForward:
677 arg = strdelim(&s);
678 if (!arg || *arg == '\0')
679 fatal("%.200s line %d: Missing port argument.",
680 filename, linenum);
Ben Lindstrom19066a12001-04-12 23:39:26 +0000681 fwd_port = a2port(arg);
682 if (fwd_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000683 fatal("%.200s line %d: Badly formatted port number.",
684 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000685 if (*activep)
686 add_local_forward(options, fwd_port, "socks4", 0);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000687 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000688
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000689 case oClearAllForwardings:
690 intptr = &options->clear_forwardings;
691 goto parse_flag;
692
Damien Miller95def091999-11-25 00:26:21 +1100693 case oHost:
694 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000695 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000696 if (match_pattern(host, arg)) {
697 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100698 *activep = 1;
699 break;
700 }
Damien Millerbe484b52000-07-15 14:14:16 +1000701 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100702 return 0;
703
704 case oEscapeChar:
705 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000706 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000707 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100708 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000709 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000710 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
711 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000712 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000713 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000714 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000715 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100716 else {
717 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100718 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100719 /* NOTREACHED */
720 value = 0; /* Avoid compiler warning. */
721 }
722 if (*activep && *intptr == -1)
723 *intptr = value;
724 break;
725
Damien Miller20a8f972003-05-18 20:50:30 +1000726 case oAddressFamily:
727 arg = strdelim(&s);
728 if (strcasecmp(arg, "inet") == 0)
729 IPv4or6 = AF_INET;
730 else if (strcasecmp(arg, "inet6") == 0)
731 IPv4or6 = AF_INET6;
732 else if (strcasecmp(arg, "any") == 0)
733 IPv4or6 = AF_UNSPEC;
734 else
735 fatal("Unsupported AddressFamily \"%s\"", arg);
736 break;
737
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000738 case oEnableSSHKeysign:
739 intptr = &options->enable_ssh_keysign;
740 goto parse_flag;
741
Ben Lindstrom4daea862002-06-09 20:04:02 +0000742 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000743 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000744 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000745 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000746
Damien Millerf9b3feb2003-05-16 11:38:32 +1000747 case oUnsupported:
748 error("%s line %d: Unsupported option \"%s\"",
749 filename, linenum, keyword);
750 return 0;
751
Damien Miller95def091999-11-25 00:26:21 +1100752 default:
753 fatal("process_config_line: Unimplemented opcode %d", opcode);
754 }
755
756 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000757 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000758 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100759 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000760 }
Damien Miller95def091999-11-25 00:26:21 +1100761 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762}
763
764
Damien Miller5428f641999-11-25 11:54:57 +1100765/*
766 * Reads the config file and modifies the options accordingly. Options
767 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000768 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100769 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000771int
Damien Miller95def091999-11-25 00:26:21 +1100772read_config_file(const char *filename, const char *host, Options *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000773{
Damien Miller95def091999-11-25 00:26:21 +1100774 FILE *f;
775 char line[1024];
776 int active, linenum;
777 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Miller95def091999-11-25 00:26:21 +1100779 /* Open the file. */
780 f = fopen(filename, "r");
781 if (!f)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000782 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783
Damien Miller95def091999-11-25 00:26:21 +1100784 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller5428f641999-11-25 11:54:57 +1100786 /*
787 * Mark that we are now processing the options. This flag is turned
788 * on/off by Host specifications.
789 */
Damien Miller95def091999-11-25 00:26:21 +1100790 active = 1;
791 linenum = 0;
792 while (fgets(line, sizeof(line), f)) {
793 /* Update line number counter. */
794 linenum++;
795 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
796 bad_options++;
797 }
798 fclose(f);
799 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000800 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100801 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000802 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803}
804
Damien Miller5428f641999-11-25 11:54:57 +1100805/*
806 * Initializes options to special values that indicate that they have not yet
807 * been set. Read_config_file will only set options with this value. Options
808 * are processed in the following order: command line, user config file,
809 * system config file. Last, fill_default_options is called.
810 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811
Damien Miller4af51302000-04-16 11:18:38 +1000812void
Damien Miller95def091999-11-25 00:26:21 +1100813initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814{
Damien Miller95def091999-11-25 00:26:21 +1100815 memset(options, 'X', sizeof(*options));
816 options->forward_agent = -1;
817 options->forward_x11 = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000818 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100819 options->gateway_ports = -1;
820 options->use_privileged_port = -1;
821 options->rhosts_authentication = -1;
822 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100823 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000824 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100825 options->kerberos_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100826 options->kerberos_tgt_passing = -1;
827 options->afs_token_passing = -1;
Damien Miller95def091999-11-25 00:26:21 +1100828 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100829 options->kbd_interactive_authentication = -1;
830 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100831 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000832 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100833 options->batch_mode = -1;
834 options->check_host_ip = -1;
835 options->strict_host_key_checking = -1;
836 options->compression = -1;
837 options->keepalives = -1;
838 options->compression_level = -1;
839 options->port = -1;
840 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000841 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +1100842 options->number_of_password_prompts = -1;
843 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +1000844 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000845 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000846 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000847 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +1100848 options->num_identity_files = 0;
849 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000850 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100851 options->proxy_command = NULL;
852 options->user = NULL;
853 options->escape_char = -1;
854 options->system_hostfile = NULL;
855 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000856 options->system_hostfile2 = NULL;
857 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100858 options->num_local_forwards = 0;
859 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000860 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100861 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000862 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000863 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000864 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000865 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000866 options->no_host_authentication_for_localhost = - 1;
Damien Millera5539d22003-04-09 20:50:06 +1000867 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +1000868 options->verify_host_key_dns = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869}
870
Damien Miller5428f641999-11-25 11:54:57 +1100871/*
872 * Called after processing other sources of option data, this fills those
873 * options for which no value has been specified with their default values.
874 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875
Damien Miller4af51302000-04-16 11:18:38 +1000876void
Damien Miller95def091999-11-25 00:26:21 +1100877fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000879 int len;
880
Damien Miller95def091999-11-25 00:26:21 +1100881 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +1000882 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +1100883 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100884 options->forward_x11 = 0;
Damien Millerd3a18572000-06-07 19:55:44 +1000885 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000886 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100887 if (options->gateway_ports == -1)
888 options->gateway_ports = 0;
889 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +0000890 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +1100891 if (options->rhosts_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +0000892 options->rhosts_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100893 if (options->rsa_authentication == -1)
894 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100895 if (options->pubkey_authentication == -1)
896 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000897 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +0000898 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100899 if (options->kerberos_authentication == -1)
900 options->kerberos_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100901 if (options->kerberos_tgt_passing == -1)
902 options->kerberos_tgt_passing = 1;
903 if (options->afs_token_passing == -1)
904 options->afs_token_passing = 1;
Damien Miller95def091999-11-25 00:26:21 +1100905 if (options->password_authentication == -1)
906 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100907 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000908 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100909 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +0000910 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000911 if (options->hostbased_authentication == -1)
912 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100913 if (options->batch_mode == -1)
914 options->batch_mode = 0;
915 if (options->check_host_ip == -1)
916 options->check_host_ip = 1;
917 if (options->strict_host_key_checking == -1)
918 options->strict_host_key_checking = 2; /* 2 is default */
919 if (options->compression == -1)
920 options->compression = 0;
921 if (options->keepalives == -1)
922 options->keepalives = 1;
923 if (options->compression_level == -1)
924 options->compression_level = 6;
925 if (options->port == -1)
926 options->port = 0; /* Filled in ssh_connect. */
927 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000928 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100929 if (options->number_of_password_prompts == -1)
930 options->number_of_password_prompts = 3;
931 /* Selected in ssh_login(). */
932 if (options->cipher == -1)
933 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +1000934 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000935 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000936 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +1000937 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +0000938 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +1100939 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100940 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000941 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100942 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000943 xmalloc(len);
944 snprintf(options->identity_files[options->num_identity_files++],
945 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100946 }
947 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +0000948 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
949 options->identity_files[options->num_identity_files] =
950 xmalloc(len);
951 snprintf(options->identity_files[options->num_identity_files++],
952 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
953
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000954 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100955 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +0000956 xmalloc(len);
957 snprintf(options->identity_files[options->num_identity_files++],
958 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100959 }
Damien Millereba71ba2000-04-29 23:57:08 +1000960 }
Damien Miller95def091999-11-25 00:26:21 +1100961 if (options->escape_char == -1)
962 options->escape_char = '~';
963 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000964 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +1100965 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000966 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +1000967 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000968 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +1000969 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000970 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +1100971 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000972 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000973 if (options->clear_forwardings == 1)
974 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000975 if (options->no_host_authentication_for_localhost == - 1)
976 options->no_host_authentication_for_localhost = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000977 if (options->enable_ssh_keysign == -1)
978 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +1000979 if (options->rekey_limit == -1)
980 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000981 if (options->verify_host_key_dns == -1)
982 options->verify_host_key_dns = 0;
Damien Miller95def091999-11-25 00:26:21 +1100983 /* options->proxy_command should not be set by default */
984 /* options->user will be set in the main program if appropriate */
985 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000986 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000987 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988}