blob: 87e41e0c4dcf83729709a8e74920f677bc66eca7 [file] [log] [blame]
Damien Miller57c30112006-03-26 14:24:48 +11001/* $OpenBSD: readconf.c,v 1.151 2006/03/25 13:17:02 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
Damien Millerc7b06362006-03-15 11:53:45 +110020#include <ctype.h>
21
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100024#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "cipher.h"
26#include "pathnames.h"
27#include "log.h"
28#include "readconf.h"
29#include "match.h"
30#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000031#include "kex.h"
32#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
34/* Format of the configuration file:
35
36 # Configuration data is parsed as follows:
37 # 1. command line options
38 # 2. user-specific file
39 # 3. system-wide file
40 # Any configuration value is only changed the first time it is set.
41 # Thus, host-specific definitions should be at the beginning of the
42 # configuration file, and defaults at the end.
43
44 # Host-specific declarations. These may override anything above. A single
45 # host may match multiple declarations; these are processed in the order
46 # that they are given in.
47
48 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000049 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
51 Host fake.com
52 HostName another.host.name.real.org
53 User blaah
54 Port 34289
55 ForwardX11 no
56 ForwardAgent no
57
58 Host books.com
59 RemoteForward 9999 shadows.cs.hut.fi:9999
60 Cipher 3des
61
62 Host fascist.blob.com
63 Port 23123
64 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065 PasswordAuthentication no
66
67 Host puukko.hut.fi
68 User t35124p
69 ProxyCommand ssh-proxy %h %p
70
71 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000072 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
74 Host *.su
75 Cipher none
76 PasswordAuthentication no
77
Damien Millerd27b9472005-12-13 19:29:02 +110078 Host vpn.fake.com
79 Tunnel yes
80 TunnelDevice 3
81
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082 # Defaults for various options
83 Host *
84 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110085 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086 PasswordAuthentication yes
87 RSAAuthentication yes
88 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110090 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091 IdentityFile ~/.ssh/identity
92 Port 22
93 EscapeChar ~
94
95*/
96
97/* Keyword tokens. */
98
Damien Miller95def091999-11-25 00:26:21 +110099typedef enum {
100 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000101 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000102 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000103 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100104 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
105 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
106 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
107 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100108 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000109 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100110 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000111 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000112 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000113 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000114 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000115 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000116 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100117 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100118 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100119 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000120 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121} OpCodes;
122
123/* Textual representations of the tokens. */
124
Damien Miller95def091999-11-25 00:26:21 +1100125static struct {
126 const char *name;
127 OpCodes opcode;
128} keywords[] = {
129 { "forwardagent", oForwardAgent },
130 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000131 { "forwardx11trusted", oForwardX11Trusted },
Damien Millerd3a18572000-06-07 19:55:44 +1000132 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100133 { "gatewayports", oGatewayPorts },
134 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000135 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100136 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100137 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
138 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100139 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100140 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000141 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000142 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000143 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000144 { "challengeresponseauthentication", oChallengeResponseAuthentication },
145 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
146 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000147 { "kerberosauthentication", oUnsupported },
148 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000149 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000150#if defined(GSSAPI)
151 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000152 { "gssapidelegatecredentials", oGssDelegateCreds },
153#else
154 { "gssapiauthentication", oUnsupported },
155 { "gssapidelegatecredentials", oUnsupported },
156#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000157 { "fallbacktorsh", oDeprecated },
158 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100159 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100161 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100162 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000163 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100164 { "proxycommand", oProxyCommand },
165 { "port", oPort },
166 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000167 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000168 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000169 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "remoteforward", oRemoteForward },
171 { "localforward", oLocalForward },
172 { "user", oUser },
173 { "host", oHost },
174 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100175 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000176 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000177 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000178 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100179 { "connectionattempts", oConnectionAttempts },
180 { "batchmode", oBatchMode },
181 { "checkhostip", oCheckHostIP },
182 { "stricthostkeychecking", oStrictHostKeyChecking },
183 { "compression", oCompression },
184 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100185 { "tcpkeepalive", oTCPKeepAlive },
186 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100187 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100188 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000189 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000190 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000191 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000192 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000193#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000194 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000195#else
196 { "smartcarddevice", oUnsupported },
197#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100198 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000199 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000200 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100201 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000202 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000203 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000204 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100205 { "serveraliveinterval", oServerAliveInterval },
206 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000207 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000208 { "controlpath", oControlPath },
209 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100210 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100211 { "tunnel", oTunnel },
212 { "tunneldevice", oTunnelDevice },
213 { "localcommand", oLocalCommand },
214 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000215 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100216};
217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Adds a local TCP/IP port forward to options. Never returns if there is an
220 * error.
221 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222
Damien Miller4af51302000-04-16 11:18:38 +1000223void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100224add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Damien Miller95def091999-11-25 00:26:21 +1100226 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000227#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100228 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100229 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000230 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100231#endif
Damien Miller95def091999-11-25 00:26:21 +1100232 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
233 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
234 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100235
236 fwd->listen_host = (newfwd->listen_host == NULL) ?
237 NULL : xstrdup(newfwd->listen_host);
238 fwd->listen_port = newfwd->listen_port;
239 fwd->connect_host = xstrdup(newfwd->connect_host);
240 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Miller5428f641999-11-25 11:54:57 +1100243/*
244 * Adds a remote TCP/IP port forward to options. Never returns if there is
245 * an error.
246 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller4af51302000-04-16 11:18:38 +1000248void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100249add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 Forward *fwd;
252 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
253 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100254 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100255 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100256
257 fwd->listen_host = (newfwd->listen_host == NULL) ?
258 NULL : xstrdup(newfwd->listen_host);
259 fwd->listen_port = newfwd->listen_port;
260 fwd->connect_host = xstrdup(newfwd->connect_host);
261 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262}
263
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000264static void
265clear_forwardings(Options *options)
266{
267 int i;
268
Damien Millerf91ee4c2005-03-01 21:24:33 +1100269 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100270 if (options->local_forwards[i].listen_host != NULL)
271 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100272 xfree(options->local_forwards[i].connect_host);
273 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000274 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100276 if (options->remote_forwards[i].listen_host != NULL)
277 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278 xfree(options->remote_forwards[i].connect_host);
279 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000280 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100281 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000282}
283
Damien Miller5428f641999-11-25 11:54:57 +1100284/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000285 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100286 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Damien Miller4af51302000-04-16 11:18:38 +1000288static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100289parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000291 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Miller95def091999-11-25 00:26:21 +1100293 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100294 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100295 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000297 error("%s: line %d: Bad configuration option: %s",
298 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100299 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300}
301
Damien Miller5428f641999-11-25 11:54:57 +1100302/*
303 * Processes a single option line as used in the configuration files. This
304 * only sets those values that have not already been set.
305 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100306#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307
Damien Miller2ccf6611999-11-15 15:25:10 +1100308int
309process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100310 char *line, const char *filename, int linenum,
311 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100313 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100314 int opcode, *intptr, value, value2, scale;
315 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100316 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100317 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Millerc652cac2003-05-14 13:40:54 +1000319 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100320 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000321 if (strchr(WHITESPACE, line[len]) == NULL)
322 break;
323 line[len] = '\0';
324 }
325
Damien Millerbe484b52000-07-15 14:14:16 +1000326 s = line;
327 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100328 if ((keyword = strdelim(&s)) == NULL)
329 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000330 /* Ignore leading whitespace. */
331 if (*keyword == '\0')
332 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000333 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100334 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Miller37023962000-07-11 17:31:38 +1000336 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
Damien Miller95def091999-11-25 00:26:21 +1100338 switch (opcode) {
339 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100340 /* don't panic, but count bad options */
341 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100342 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000343 case oConnectTimeout:
344 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100345parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000346 arg = strdelim(&s);
347 if (!arg || *arg == '\0')
348 fatal("%s line %d: missing time value.",
349 filename, linenum);
350 if ((value = convtime(arg)) == -1)
351 fatal("%s line %d: invalid time value.",
352 filename, linenum);
353 if (*intptr == -1)
354 *intptr = value;
355 break;
356
Damien Miller95def091999-11-25 00:26:21 +1100357 case oForwardAgent:
358 intptr = &options->forward_agent;
359parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000360 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000361 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100362 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
363 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000364 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100365 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000366 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100367 value = 0;
368 else
369 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
370 if (*activep && *intptr == -1)
371 *intptr = value;
372 break;
373
374 case oForwardX11:
375 intptr = &options->forward_x11;
376 goto parse_flag;
377
Darren Tucker0a118da2003-10-15 15:54:32 +1000378 case oForwardX11Trusted:
379 intptr = &options->forward_x11_trusted;
380 goto parse_flag;
381
Damien Miller95def091999-11-25 00:26:21 +1100382 case oGatewayPorts:
383 intptr = &options->gateway_ports;
384 goto parse_flag;
385
386 case oUsePrivilegedPort:
387 intptr = &options->use_privileged_port;
388 goto parse_flag;
389
Damien Miller95def091999-11-25 00:26:21 +1100390 case oPasswordAuthentication:
391 intptr = &options->password_authentication;
392 goto parse_flag;
393
Damien Miller874d77b2000-10-14 16:23:11 +1100394 case oKbdInteractiveAuthentication:
395 intptr = &options->kbd_interactive_authentication;
396 goto parse_flag;
397
398 case oKbdInteractiveDevices:
399 charptr = &options->kbd_interactive_devices;
400 goto parse_string;
401
Damien Miller0bc1bd82000-11-13 22:57:25 +1100402 case oPubkeyAuthentication:
403 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000404 goto parse_flag;
405
Damien Miller95def091999-11-25 00:26:21 +1100406 case oRSAAuthentication:
407 intptr = &options->rsa_authentication;
408 goto parse_flag;
409
410 case oRhostsRSAAuthentication:
411 intptr = &options->rhosts_rsa_authentication;
412 goto parse_flag;
413
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000414 case oHostbasedAuthentication:
415 intptr = &options->hostbased_authentication;
416 goto parse_flag;
417
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000418 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000419 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100420 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000421
Darren Tucker0efd1552003-08-26 11:49:55 +1000422 case oGssAuthentication:
423 intptr = &options->gss_authentication;
424 goto parse_flag;
425
426 case oGssDelegateCreds:
427 intptr = &options->gss_deleg_creds;
428 goto parse_flag;
429
Damien Miller95def091999-11-25 00:26:21 +1100430 case oBatchMode:
431 intptr = &options->batch_mode;
432 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 case oCheckHostIP:
435 intptr = &options->check_host_ip;
436 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Damien Miller37876e92003-05-15 10:19:46 +1000438 case oVerifyHostKeyDNS:
439 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100440 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000441
Damien Miller95def091999-11-25 00:26:21 +1100442 case oStrictHostKeyChecking:
443 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100444parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000445 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000446 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000447 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100448 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100449 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000450 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100451 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000452 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100453 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000454 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100455 value = 2;
456 else
457 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
458 if (*activep && *intptr == -1)
459 *intptr = value;
460 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Damien Miller95def091999-11-25 00:26:21 +1100462 case oCompression:
463 intptr = &options->compression;
464 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465
Damien Miller12c150e2003-12-17 16:31:10 +1100466 case oTCPKeepAlive:
467 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100468 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000470 case oNoHostAuthenticationForLocalhost:
471 intptr = &options->no_host_authentication_for_localhost;
472 goto parse_flag;
473
Damien Miller95def091999-11-25 00:26:21 +1100474 case oNumberOfPasswordPrompts:
475 intptr = &options->number_of_password_prompts;
476 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477
Damien Miller95def091999-11-25 00:26:21 +1100478 case oCompressionLevel:
479 intptr = &options->compression_level;
480 goto parse_int;
481
Damien Millera5539d22003-04-09 20:50:06 +1000482 case oRekeyLimit:
483 intptr = &options->rekey_limit;
484 arg = strdelim(&s);
485 if (!arg || *arg == '\0')
486 fatal("%.200s line %d: Missing argument.", filename, linenum);
487 if (arg[0] < '0' || arg[0] > '9')
488 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100489 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000490 if (arg == endofnumber)
491 fatal("%.200s line %d: Bad number.", filename, linenum);
492 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100493 case '\0':
494 scale = 1;
495 break;
Damien Millera5539d22003-04-09 20:50:06 +1000496 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100497 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000498 break;
499 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100500 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000501 break;
502 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100503 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000504 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100505 default:
506 fatal("%.200s line %d: Invalid RekeyLimit suffix",
507 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000508 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100509 val64 *= scale;
510 /* detect integer wrap and too-large limits */
511 if ((val64 / scale) != orig || val64 > INT_MAX)
512 fatal("%.200s line %d: RekeyLimit too large",
513 filename, linenum);
514 if (val64 < 16)
515 fatal("%.200s line %d: RekeyLimit too small",
516 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000517 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100518 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000519 break;
520
Damien Miller95def091999-11-25 00:26:21 +1100521 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000522 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000523 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100524 fatal("%.200s line %d: Missing argument.", filename, linenum);
525 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100526 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000527 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100528 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100529 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100530 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000531 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000532 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100533 }
534 break;
535
Damien Millerd3a18572000-06-07 19:55:44 +1000536 case oXAuthLocation:
537 charptr=&options->xauth_location;
538 goto parse_string;
539
Damien Miller95def091999-11-25 00:26:21 +1100540 case oUser:
541 charptr = &options->user;
542parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000543 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000544 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100545 fatal("%.200s line %d: Missing argument.", filename, linenum);
546 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000547 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100548 break;
549
550 case oGlobalKnownHostsFile:
551 charptr = &options->system_hostfile;
552 goto parse_string;
553
554 case oUserKnownHostsFile:
555 charptr = &options->user_hostfile;
556 goto parse_string;
557
Damien Millereba71ba2000-04-29 23:57:08 +1000558 case oGlobalKnownHostsFile2:
559 charptr = &options->system_hostfile2;
560 goto parse_string;
561
562 case oUserKnownHostsFile2:
563 charptr = &options->user_hostfile2;
564 goto parse_string;
565
Damien Miller95def091999-11-25 00:26:21 +1100566 case oHostName:
567 charptr = &options->hostname;
568 goto parse_string;
569
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000570 case oHostKeyAlias:
571 charptr = &options->host_key_alias;
572 goto parse_string;
573
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000574 case oPreferredAuthentications:
575 charptr = &options->preferred_authentications;
576 goto parse_string;
577
Ben Lindstrome0f88042001-04-30 13:06:24 +0000578 case oBindAddress:
579 charptr = &options->bind_address;
580 goto parse_string;
581
Ben Lindstromae996bf2001-08-06 21:27:53 +0000582 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000583 charptr = &options->smartcard_device;
584 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000585
Damien Miller95def091999-11-25 00:26:21 +1100586 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100587 charptr = &options->proxy_command;
588parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000589 if (s == NULL)
590 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100591 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100592 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100593 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100594 return 0;
595
596 case oPort:
597 intptr = &options->port;
598parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000599 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000600 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100601 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000602 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100603 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100604
605 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000606 value = strtol(arg, &endofnumber, 0);
607 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100608 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100609 if (*activep && *intptr == -1)
610 *intptr = value;
611 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
Damien Miller95def091999-11-25 00:26:21 +1100613 case oConnectionAttempts:
614 intptr = &options->connection_attempts;
615 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100616
Damien Miller95def091999-11-25 00:26:21 +1100617 case oCipher:
618 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000619 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000620 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000621 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000622 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100623 if (value == -1)
624 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100625 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100626 if (*activep && *intptr == -1)
627 *intptr = value;
628 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629
Damien Miller78928792000-04-12 20:17:38 +1000630 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000631 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000632 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000633 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000634 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000635 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100636 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000637 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000638 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000639 break;
640
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000641 case oMacs:
642 arg = strdelim(&s);
643 if (!arg || *arg == '\0')
644 fatal("%.200s line %d: Missing argument.", filename, linenum);
645 if (!mac_valid(arg))
646 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100647 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000648 if (*activep && options->macs == NULL)
649 options->macs = xstrdup(arg);
650 break;
651
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000652 case oHostKeyAlgorithms:
653 arg = strdelim(&s);
654 if (!arg || *arg == '\0')
655 fatal("%.200s line %d: Missing argument.", filename, linenum);
656 if (!key_names_valid2(arg))
657 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100658 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000659 if (*activep && options->hostkeyalgorithms == NULL)
660 options->hostkeyalgorithms = xstrdup(arg);
661 break;
662
Damien Miller78928792000-04-12 20:17:38 +1000663 case oProtocol:
664 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000665 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000666 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000667 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000668 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000669 if (value == SSH_PROTO_UNKNOWN)
670 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100671 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000672 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
673 *intptr = value;
674 break;
675
Damien Miller95def091999-11-25 00:26:21 +1100676 case oLogLevel:
677 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000678 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000679 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100680 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000681 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100682 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100683 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100684 *intptr = (LogLevel) value;
685 break;
686
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000687 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100688 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000689 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100690 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000691 fatal("%.200s line %d: Missing port argument.",
692 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100693 arg2 = strdelim(&s);
694 if (arg2 == NULL || *arg2 == '\0')
695 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000696 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100697
698 /* construct a string for parse_forward */
699 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
700
701 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000702 fatal("%.200s line %d: Bad forwarding specification.",
703 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100704
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000705 if (*activep) {
706 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000708 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100709 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000710 }
Damien Miller95def091999-11-25 00:26:21 +1100711 break;
712
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000713 case oDynamicForward:
714 arg = strdelim(&s);
715 if (!arg || *arg == '\0')
716 fatal("%.200s line %d: Missing port argument.",
717 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100718 memset(&fwd, '\0', sizeof(fwd));
719 fwd.connect_host = "socks";
720 fwd.listen_host = hpdelim(&arg);
721 if (fwd.listen_host == NULL ||
722 strlen(fwd.listen_host) >= NI_MAXHOST)
723 fatal("%.200s line %d: Bad forwarding specification.",
724 filename, linenum);
725 if (arg) {
726 fwd.listen_port = a2port(arg);
727 fwd.listen_host = cleanhostname(fwd.listen_host);
728 } else {
729 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000730 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100731 }
732 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000733 fatal("%.200s line %d: Badly formatted port number.",
734 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000735 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100736 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000737 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000738
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000739 case oClearAllForwardings:
740 intptr = &options->clear_forwardings;
741 goto parse_flag;
742
Damien Miller95def091999-11-25 00:26:21 +1100743 case oHost:
744 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000745 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000746 if (match_pattern(host, arg)) {
747 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100748 *activep = 1;
749 break;
750 }
Damien Millerbe484b52000-07-15 14:14:16 +1000751 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100752 return 0;
753
754 case oEscapeChar:
755 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000756 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000757 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100758 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000759 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000760 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
761 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000762 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000763 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000764 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000765 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100766 else {
767 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100768 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100769 /* NOTREACHED */
770 value = 0; /* Avoid compiler warning. */
771 }
772 if (*activep && *intptr == -1)
773 *intptr = value;
774 break;
775
Damien Miller20a8f972003-05-18 20:50:30 +1000776 case oAddressFamily:
777 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000778 if (!arg || *arg == '\0')
779 fatal("%s line %d: missing address family.",
780 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000781 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000782 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000783 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000784 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000785 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000786 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000787 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000788 else
789 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000790 if (*activep && *intptr == -1)
791 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000792 break;
793
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000794 case oEnableSSHKeysign:
795 intptr = &options->enable_ssh_keysign;
796 goto parse_flag;
797
Damien Millerbd394c32004-03-08 23:12:36 +1100798 case oIdentitiesOnly:
799 intptr = &options->identities_only;
800 goto parse_flag;
801
Damien Miller509b0102003-12-17 16:33:10 +1100802 case oServerAliveInterval:
803 intptr = &options->server_alive_interval;
804 goto parse_time;
805
806 case oServerAliveCountMax:
807 intptr = &options->server_alive_count_max;
808 goto parse_int;
809
Darren Tucker46bc0752004-05-02 22:11:30 +1000810 case oSendEnv:
811 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
812 if (strchr(arg, '=') != NULL)
813 fatal("%s line %d: Invalid environment name.",
814 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100815 if (!*activep)
816 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000817 if (options->num_send_env >= MAX_SEND_ENV)
818 fatal("%s line %d: too many send env.",
819 filename, linenum);
820 options->send_env[options->num_send_env++] =
821 xstrdup(arg);
822 }
823 break;
824
Damien Miller0e220db2004-06-15 10:34:08 +1000825 case oControlPath:
826 charptr = &options->control_path;
827 goto parse_string;
828
829 case oControlMaster:
830 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000831 arg = strdelim(&s);
832 if (!arg || *arg == '\0')
833 fatal("%.200s line %d: Missing ControlMaster argument.",
834 filename, linenum);
835 value = 0; /* To avoid compiler warning... */
836 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
837 value = SSHCTL_MASTER_YES;
838 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
839 value = SSHCTL_MASTER_NO;
840 else if (strcmp(arg, "auto") == 0)
841 value = SSHCTL_MASTER_AUTO;
842 else if (strcmp(arg, "ask") == 0)
843 value = SSHCTL_MASTER_ASK;
844 else if (strcmp(arg, "autoask") == 0)
845 value = SSHCTL_MASTER_AUTO_ASK;
846 else
847 fatal("%.200s line %d: Bad ControlMaster argument.",
848 filename, linenum);
849 if (*activep && *intptr == -1)
850 *intptr = value;
851 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000852
Damien Millere1776152005-03-01 21:47:37 +1100853 case oHashKnownHosts:
854 intptr = &options->hash_known_hosts;
855 goto parse_flag;
856
Damien Millerd27b9472005-12-13 19:29:02 +1100857 case oTunnel:
858 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100859 arg = strdelim(&s);
860 if (!arg || *arg == '\0')
861 fatal("%s line %d: Missing yes/point-to-point/"
862 "ethernet/no argument.", filename, linenum);
863 value = 0; /* silence compiler */
864 if (strcasecmp(arg, "ethernet") == 0)
865 value = SSH_TUNMODE_ETHERNET;
866 else if (strcasecmp(arg, "point-to-point") == 0)
867 value = SSH_TUNMODE_POINTOPOINT;
868 else if (strcasecmp(arg, "yes") == 0)
869 value = SSH_TUNMODE_DEFAULT;
870 else if (strcasecmp(arg, "no") == 0)
871 value = SSH_TUNMODE_NO;
872 else
873 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
874 "no argument: %s", filename, linenum, arg);
875 if (*activep)
876 *intptr = value;
877 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100878
879 case oTunnelDevice:
880 arg = strdelim(&s);
881 if (!arg || *arg == '\0')
882 fatal("%.200s line %d: Missing argument.", filename, linenum);
883 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100884 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100885 fatal("%.200s line %d: Bad tun device.", filename, linenum);
886 if (*activep) {
887 options->tun_local = value;
888 options->tun_remote = value2;
889 }
890 break;
891
892 case oLocalCommand:
893 charptr = &options->local_command;
894 goto parse_command;
895
896 case oPermitLocalCommand:
897 intptr = &options->permit_local_command;
898 goto parse_flag;
899
Ben Lindstrom4daea862002-06-09 20:04:02 +0000900 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000901 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000902 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000903 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000904
Damien Millerf9b3feb2003-05-16 11:38:32 +1000905 case oUnsupported:
906 error("%s line %d: Unsupported option \"%s\"",
907 filename, linenum, keyword);
908 return 0;
909
Damien Miller95def091999-11-25 00:26:21 +1100910 default:
911 fatal("process_config_line: Unimplemented opcode %d", opcode);
912 }
913
914 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000915 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000916 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000917 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000918 }
Damien Miller95def091999-11-25 00:26:21 +1100919 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920}
921
922
Damien Miller5428f641999-11-25 11:54:57 +1100923/*
924 * Reads the config file and modifies the options accordingly. Options
925 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000926 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100927 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000929int
Darren Tuckerfc959702004-07-17 16:12:08 +1000930read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000931 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932{
Damien Miller95def091999-11-25 00:26:21 +1100933 FILE *f;
934 char line[1024];
935 int active, linenum;
936 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937
Damien Miller95def091999-11-25 00:26:21 +1100938 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000939 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000940 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
Damien Miller57a44762004-04-20 20:11:57 +1000942 if (checkperm) {
943 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000944
Damien Miller33793852004-06-15 10:27:55 +1000945 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000946 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000947 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000948 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000949 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000950 }
951
Damien Miller95def091999-11-25 00:26:21 +1100952 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
Damien Miller5428f641999-11-25 11:54:57 +1100954 /*
955 * Mark that we are now processing the options. This flag is turned
956 * on/off by Host specifications.
957 */
Damien Miller95def091999-11-25 00:26:21 +1100958 active = 1;
959 linenum = 0;
960 while (fgets(line, sizeof(line), f)) {
961 /* Update line number counter. */
962 linenum++;
963 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
964 bad_options++;
965 }
966 fclose(f);
967 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000968 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100969 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000970 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971}
972
Damien Miller5428f641999-11-25 11:54:57 +1100973/*
974 * Initializes options to special values that indicate that they have not yet
975 * been set. Read_config_file will only set options with this value. Options
976 * are processed in the following order: command line, user config file,
977 * system config file. Last, fill_default_options is called.
978 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller4af51302000-04-16 11:18:38 +1000980void
Damien Miller95def091999-11-25 00:26:21 +1100981initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982{
Damien Miller95def091999-11-25 00:26:21 +1100983 memset(options, 'X', sizeof(*options));
984 options->forward_agent = -1;
985 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000986 options->forward_x11_trusted = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000987 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100988 options->gateway_ports = -1;
989 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100990 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100991 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000992 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000993 options->gss_authentication = -1;
994 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100995 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100996 options->kbd_interactive_authentication = -1;
997 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100998 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000999 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001000 options->batch_mode = -1;
1001 options->check_host_ip = -1;
1002 options->strict_host_key_checking = -1;
1003 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001004 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001005 options->compression_level = -1;
1006 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001007 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001008 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001009 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001010 options->number_of_password_prompts = -1;
1011 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001012 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001013 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001014 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001015 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001016 options->num_identity_files = 0;
1017 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001018 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001019 options->proxy_command = NULL;
1020 options->user = NULL;
1021 options->escape_char = -1;
1022 options->system_hostfile = NULL;
1023 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001024 options->system_hostfile2 = NULL;
1025 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001026 options->num_local_forwards = 0;
1027 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001028 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001029 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001030 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001031 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001032 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001033 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001034 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001035 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001036 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001037 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001038 options->server_alive_interval = -1;
1039 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001040 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001041 options->control_path = NULL;
1042 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001043 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001044 options->tun_open = -1;
1045 options->tun_local = -1;
1046 options->tun_remote = -1;
1047 options->local_command = NULL;
1048 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049}
1050
Damien Miller5428f641999-11-25 11:54:57 +11001051/*
1052 * Called after processing other sources of option data, this fills those
1053 * options for which no value has been specified with their default values.
1054 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055
Damien Miller4af51302000-04-16 11:18:38 +10001056void
Damien Miller95def091999-11-25 00:26:21 +11001057fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001059 int len;
1060
Damien Miller95def091999-11-25 00:26:21 +11001061 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001062 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001063 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001064 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001065 if (options->forward_x11_trusted == -1)
1066 options->forward_x11_trusted = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001067 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001068 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001069 if (options->gateway_ports == -1)
1070 options->gateway_ports = 0;
1071 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001072 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001073 if (options->rsa_authentication == -1)
1074 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001075 if (options->pubkey_authentication == -1)
1076 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001077 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001078 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001079 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001080 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001081 if (options->gss_deleg_creds == -1)
1082 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001083 if (options->password_authentication == -1)
1084 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001085 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001086 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001087 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001088 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001089 if (options->hostbased_authentication == -1)
1090 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001091 if (options->batch_mode == -1)
1092 options->batch_mode = 0;
1093 if (options->check_host_ip == -1)
1094 options->check_host_ip = 1;
1095 if (options->strict_host_key_checking == -1)
1096 options->strict_host_key_checking = 2; /* 2 is default */
1097 if (options->compression == -1)
1098 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001099 if (options->tcp_keep_alive == -1)
1100 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001101 if (options->compression_level == -1)
1102 options->compression_level = 6;
1103 if (options->port == -1)
1104 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001105 if (options->address_family == -1)
1106 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001107 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001108 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001109 if (options->number_of_password_prompts == -1)
1110 options->number_of_password_prompts = 3;
1111 /* Selected in ssh_login(). */
1112 if (options->cipher == -1)
1113 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001114 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001115 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001116 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001117 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001118 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001119 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001120 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001121 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001122 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001123 xmalloc(len);
1124 snprintf(options->identity_files[options->num_identity_files++],
1125 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001126 }
1127 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001128 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1129 options->identity_files[options->num_identity_files] =
1130 xmalloc(len);
1131 snprintf(options->identity_files[options->num_identity_files++],
1132 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1133
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001134 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001135 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001136 xmalloc(len);
1137 snprintf(options->identity_files[options->num_identity_files++],
1138 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001139 }
Damien Millereba71ba2000-04-29 23:57:08 +10001140 }
Damien Miller95def091999-11-25 00:26:21 +11001141 if (options->escape_char == -1)
1142 options->escape_char = '~';
1143 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001144 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001145 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001146 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001147 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001148 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001149 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001150 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001151 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001152 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001153 if (options->clear_forwardings == 1)
1154 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001155 if (options->no_host_authentication_for_localhost == - 1)
1156 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001157 if (options->identities_only == -1)
1158 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001159 if (options->enable_ssh_keysign == -1)
1160 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001161 if (options->rekey_limit == -1)
1162 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001163 if (options->verify_host_key_dns == -1)
1164 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001165 if (options->server_alive_interval == -1)
1166 options->server_alive_interval = 0;
1167 if (options->server_alive_count_max == -1)
1168 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001169 if (options->control_master == -1)
1170 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001171 if (options->hash_known_hosts == -1)
1172 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001173 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001174 options->tun_open = SSH_TUNMODE_NO;
1175 if (options->tun_local == -1)
1176 options->tun_local = SSH_TUNID_ANY;
1177 if (options->tun_remote == -1)
1178 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001179 if (options->permit_local_command == -1)
1180 options->permit_local_command = 0;
1181 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001182 /* options->proxy_command should not be set by default */
1183 /* options->user will be set in the main program if appropriate */
1184 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001185 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001186 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001188
1189/*
1190 * parse_forward
1191 * parses a string containing a port forwarding specification of the form:
1192 * [listenhost:]listenport:connecthost:connectport
1193 * returns number of arguments parsed or zero on error
1194 */
1195int
1196parse_forward(Forward *fwd, const char *fwdspec)
1197{
1198 int i;
1199 char *p, *cp, *fwdarg[4];
1200
1201 memset(fwd, '\0', sizeof(*fwd));
1202
1203 cp = p = xstrdup(fwdspec);
1204
1205 /* skip leading spaces */
1206 while (*cp && isspace(*cp))
1207 cp++;
1208
1209 for (i = 0; i < 4; ++i)
1210 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1211 break;
1212
1213 /* Check for trailing garbage in 4-arg case*/
1214 if (cp != NULL)
1215 i = 0; /* failure */
1216
1217 switch (i) {
1218 case 3:
1219 fwd->listen_host = NULL;
1220 fwd->listen_port = a2port(fwdarg[0]);
1221 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1222 fwd->connect_port = a2port(fwdarg[2]);
1223 break;
1224
1225 case 4:
1226 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1227 fwd->listen_port = a2port(fwdarg[1]);
1228 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1229 fwd->connect_port = a2port(fwdarg[3]);
1230 break;
1231 default:
1232 i = 0; /* failure */
1233 }
1234
1235 xfree(p);
1236
1237 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1238 goto fail_free;
1239
1240 if (fwd->connect_host != NULL &&
1241 strlen(fwd->connect_host) >= NI_MAXHOST)
1242 goto fail_free;
1243
1244 return (i);
1245
1246 fail_free:
1247 if (fwd->connect_host != NULL)
1248 xfree(fwd->connect_host);
1249 if (fwd->listen_host != NULL)
1250 xfree(fwd->listen_host);
1251 return (0);
1252}