blob: 392fc52c23f3137ff8b44730b25dc50f9acb3dce [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 Millerc7b06362006-03-15 11:53:45 +110015RCSID("$OpenBSD: readconf.c,v 1.148 2006/02/22 00:04:44 stevesk Exp $");
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). */
328 keyword = strdelim(&s);
329 /* Ignore leading whitespace. */
330 if (*keyword == '\0')
331 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000332 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100333 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
Damien Miller37023962000-07-11 17:31:38 +1000335 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Damien Miller95def091999-11-25 00:26:21 +1100337 switch (opcode) {
338 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100339 /* don't panic, but count bad options */
340 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100341 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000342 case oConnectTimeout:
343 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100344parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000345 arg = strdelim(&s);
346 if (!arg || *arg == '\0')
347 fatal("%s line %d: missing time value.",
348 filename, linenum);
349 if ((value = convtime(arg)) == -1)
350 fatal("%s line %d: invalid time value.",
351 filename, linenum);
352 if (*intptr == -1)
353 *intptr = value;
354 break;
355
Damien Miller95def091999-11-25 00:26:21 +1100356 case oForwardAgent:
357 intptr = &options->forward_agent;
358parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000359 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000360 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100361 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
362 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000363 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100364 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000365 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100366 value = 0;
367 else
368 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
369 if (*activep && *intptr == -1)
370 *intptr = value;
371 break;
372
373 case oForwardX11:
374 intptr = &options->forward_x11;
375 goto parse_flag;
376
Darren Tucker0a118da2003-10-15 15:54:32 +1000377 case oForwardX11Trusted:
378 intptr = &options->forward_x11_trusted;
379 goto parse_flag;
380
Damien Miller95def091999-11-25 00:26:21 +1100381 case oGatewayPorts:
382 intptr = &options->gateway_ports;
383 goto parse_flag;
384
385 case oUsePrivilegedPort:
386 intptr = &options->use_privileged_port;
387 goto parse_flag;
388
Damien Miller95def091999-11-25 00:26:21 +1100389 case oPasswordAuthentication:
390 intptr = &options->password_authentication;
391 goto parse_flag;
392
Damien Miller874d77b2000-10-14 16:23:11 +1100393 case oKbdInteractiveAuthentication:
394 intptr = &options->kbd_interactive_authentication;
395 goto parse_flag;
396
397 case oKbdInteractiveDevices:
398 charptr = &options->kbd_interactive_devices;
399 goto parse_string;
400
Damien Miller0bc1bd82000-11-13 22:57:25 +1100401 case oPubkeyAuthentication:
402 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000403 goto parse_flag;
404
Damien Miller95def091999-11-25 00:26:21 +1100405 case oRSAAuthentication:
406 intptr = &options->rsa_authentication;
407 goto parse_flag;
408
409 case oRhostsRSAAuthentication:
410 intptr = &options->rhosts_rsa_authentication;
411 goto parse_flag;
412
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000413 case oHostbasedAuthentication:
414 intptr = &options->hostbased_authentication;
415 goto parse_flag;
416
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000417 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000418 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100419 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000420
Darren Tucker0efd1552003-08-26 11:49:55 +1000421 case oGssAuthentication:
422 intptr = &options->gss_authentication;
423 goto parse_flag;
424
425 case oGssDelegateCreds:
426 intptr = &options->gss_deleg_creds;
427 goto parse_flag;
428
Damien Miller95def091999-11-25 00:26:21 +1100429 case oBatchMode:
430 intptr = &options->batch_mode;
431 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Damien Miller95def091999-11-25 00:26:21 +1100433 case oCheckHostIP:
434 intptr = &options->check_host_ip;
435 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Damien Miller37876e92003-05-15 10:19:46 +1000437 case oVerifyHostKeyDNS:
438 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100439 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000440
Damien Miller95def091999-11-25 00:26:21 +1100441 case oStrictHostKeyChecking:
442 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100443parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000444 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000445 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000446 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100447 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100448 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000449 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100450 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000451 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100452 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000453 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100454 value = 2;
455 else
456 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
457 if (*activep && *intptr == -1)
458 *intptr = value;
459 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Damien Miller95def091999-11-25 00:26:21 +1100461 case oCompression:
462 intptr = &options->compression;
463 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller12c150e2003-12-17 16:31:10 +1100465 case oTCPKeepAlive:
466 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100467 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000469 case oNoHostAuthenticationForLocalhost:
470 intptr = &options->no_host_authentication_for_localhost;
471 goto parse_flag;
472
Damien Miller95def091999-11-25 00:26:21 +1100473 case oNumberOfPasswordPrompts:
474 intptr = &options->number_of_password_prompts;
475 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476
Damien Miller95def091999-11-25 00:26:21 +1100477 case oCompressionLevel:
478 intptr = &options->compression_level;
479 goto parse_int;
480
Damien Millera5539d22003-04-09 20:50:06 +1000481 case oRekeyLimit:
482 intptr = &options->rekey_limit;
483 arg = strdelim(&s);
484 if (!arg || *arg == '\0')
485 fatal("%.200s line %d: Missing argument.", filename, linenum);
486 if (arg[0] < '0' || arg[0] > '9')
487 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100488 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000489 if (arg == endofnumber)
490 fatal("%.200s line %d: Bad number.", filename, linenum);
491 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100492 case '\0':
493 scale = 1;
494 break;
Damien Millera5539d22003-04-09 20:50:06 +1000495 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100496 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000497 break;
498 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100499 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000500 break;
501 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100502 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000503 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100504 default:
505 fatal("%.200s line %d: Invalid RekeyLimit suffix",
506 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000507 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100508 val64 *= scale;
509 /* detect integer wrap and too-large limits */
510 if ((val64 / scale) != orig || val64 > INT_MAX)
511 fatal("%.200s line %d: RekeyLimit too large",
512 filename, linenum);
513 if (val64 < 16)
514 fatal("%.200s line %d: RekeyLimit too small",
515 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000516 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100517 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000518 break;
519
Damien Miller95def091999-11-25 00:26:21 +1100520 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000521 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000522 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100523 fatal("%.200s line %d: Missing argument.", filename, linenum);
524 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100525 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000526 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100527 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100528 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100529 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000530 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000531 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100532 }
533 break;
534
Damien Millerd3a18572000-06-07 19:55:44 +1000535 case oXAuthLocation:
536 charptr=&options->xauth_location;
537 goto parse_string;
538
Damien Miller95def091999-11-25 00:26:21 +1100539 case oUser:
540 charptr = &options->user;
541parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000542 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000543 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100544 fatal("%.200s line %d: Missing argument.", filename, linenum);
545 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000546 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100547 break;
548
549 case oGlobalKnownHostsFile:
550 charptr = &options->system_hostfile;
551 goto parse_string;
552
553 case oUserKnownHostsFile:
554 charptr = &options->user_hostfile;
555 goto parse_string;
556
Damien Millereba71ba2000-04-29 23:57:08 +1000557 case oGlobalKnownHostsFile2:
558 charptr = &options->system_hostfile2;
559 goto parse_string;
560
561 case oUserKnownHostsFile2:
562 charptr = &options->user_hostfile2;
563 goto parse_string;
564
Damien Miller95def091999-11-25 00:26:21 +1100565 case oHostName:
566 charptr = &options->hostname;
567 goto parse_string;
568
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000569 case oHostKeyAlias:
570 charptr = &options->host_key_alias;
571 goto parse_string;
572
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000573 case oPreferredAuthentications:
574 charptr = &options->preferred_authentications;
575 goto parse_string;
576
Ben Lindstrome0f88042001-04-30 13:06:24 +0000577 case oBindAddress:
578 charptr = &options->bind_address;
579 goto parse_string;
580
Ben Lindstromae996bf2001-08-06 21:27:53 +0000581 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000582 charptr = &options->smartcard_device;
583 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000584
Damien Miller95def091999-11-25 00:26:21 +1100585 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100586 charptr = &options->proxy_command;
587parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000588 if (s == NULL)
589 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100590 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100591 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100592 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100593 return 0;
594
595 case oPort:
596 intptr = &options->port;
597parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000598 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000599 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100600 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000601 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100602 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100603
604 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000605 value = strtol(arg, &endofnumber, 0);
606 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100607 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100608 if (*activep && *intptr == -1)
609 *intptr = value;
610 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Damien Miller95def091999-11-25 00:26:21 +1100612 case oConnectionAttempts:
613 intptr = &options->connection_attempts;
614 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100615
Damien Miller95def091999-11-25 00:26:21 +1100616 case oCipher:
617 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000618 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000619 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000620 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000621 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100622 if (value == -1)
623 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100624 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100625 if (*activep && *intptr == -1)
626 *intptr = value;
627 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Damien Miller78928792000-04-12 20:17:38 +1000629 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000630 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000631 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000632 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000633 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000634 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100635 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000636 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000637 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000638 break;
639
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000640 case oMacs:
641 arg = strdelim(&s);
642 if (!arg || *arg == '\0')
643 fatal("%.200s line %d: Missing argument.", filename, linenum);
644 if (!mac_valid(arg))
645 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100646 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000647 if (*activep && options->macs == NULL)
648 options->macs = xstrdup(arg);
649 break;
650
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000651 case oHostKeyAlgorithms:
652 arg = strdelim(&s);
653 if (!arg || *arg == '\0')
654 fatal("%.200s line %d: Missing argument.", filename, linenum);
655 if (!key_names_valid2(arg))
656 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100657 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000658 if (*activep && options->hostkeyalgorithms == NULL)
659 options->hostkeyalgorithms = xstrdup(arg);
660 break;
661
Damien Miller78928792000-04-12 20:17:38 +1000662 case oProtocol:
663 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000664 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000665 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000666 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000667 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000668 if (value == SSH_PROTO_UNKNOWN)
669 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100670 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000671 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
672 *intptr = value;
673 break;
674
Damien Miller95def091999-11-25 00:26:21 +1100675 case oLogLevel:
676 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000677 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000678 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100679 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000680 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100681 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100682 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100683 *intptr = (LogLevel) value;
684 break;
685
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000686 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100687 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000688 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100689 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000690 fatal("%.200s line %d: Missing port argument.",
691 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100692 arg2 = strdelim(&s);
693 if (arg2 == NULL || *arg2 == '\0')
694 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000695 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100696
697 /* construct a string for parse_forward */
698 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
699
700 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000701 fatal("%.200s line %d: Bad forwarding specification.",
702 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100703
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000704 if (*activep) {
705 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100706 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000707 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100708 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000709 }
Damien Miller95def091999-11-25 00:26:21 +1100710 break;
711
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000712 case oDynamicForward:
713 arg = strdelim(&s);
714 if (!arg || *arg == '\0')
715 fatal("%.200s line %d: Missing port argument.",
716 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100717 memset(&fwd, '\0', sizeof(fwd));
718 fwd.connect_host = "socks";
719 fwd.listen_host = hpdelim(&arg);
720 if (fwd.listen_host == NULL ||
721 strlen(fwd.listen_host) >= NI_MAXHOST)
722 fatal("%.200s line %d: Bad forwarding specification.",
723 filename, linenum);
724 if (arg) {
725 fwd.listen_port = a2port(arg);
726 fwd.listen_host = cleanhostname(fwd.listen_host);
727 } else {
728 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000729 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100730 }
731 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000732 fatal("%.200s line %d: Badly formatted port number.",
733 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000734 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100735 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000736 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000737
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000738 case oClearAllForwardings:
739 intptr = &options->clear_forwardings;
740 goto parse_flag;
741
Damien Miller95def091999-11-25 00:26:21 +1100742 case oHost:
743 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000744 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000745 if (match_pattern(host, arg)) {
746 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100747 *activep = 1;
748 break;
749 }
Damien Millerbe484b52000-07-15 14:14:16 +1000750 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100751 return 0;
752
753 case oEscapeChar:
754 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000755 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000756 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100757 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000758 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000759 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
760 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000761 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000762 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000763 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000764 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100765 else {
766 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100767 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100768 /* NOTREACHED */
769 value = 0; /* Avoid compiler warning. */
770 }
771 if (*activep && *intptr == -1)
772 *intptr = value;
773 break;
774
Damien Miller20a8f972003-05-18 20:50:30 +1000775 case oAddressFamily:
776 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000777 if (!arg || *arg == '\0')
778 fatal("%s line %d: missing address family.",
779 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000780 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000781 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000782 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000783 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000784 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000785 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000786 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000787 else
788 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000789 if (*activep && *intptr == -1)
790 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000791 break;
792
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000793 case oEnableSSHKeysign:
794 intptr = &options->enable_ssh_keysign;
795 goto parse_flag;
796
Damien Millerbd394c32004-03-08 23:12:36 +1100797 case oIdentitiesOnly:
798 intptr = &options->identities_only;
799 goto parse_flag;
800
Damien Miller509b0102003-12-17 16:33:10 +1100801 case oServerAliveInterval:
802 intptr = &options->server_alive_interval;
803 goto parse_time;
804
805 case oServerAliveCountMax:
806 intptr = &options->server_alive_count_max;
807 goto parse_int;
808
Darren Tucker46bc0752004-05-02 22:11:30 +1000809 case oSendEnv:
810 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
811 if (strchr(arg, '=') != NULL)
812 fatal("%s line %d: Invalid environment name.",
813 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100814 if (!*activep)
815 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000816 if (options->num_send_env >= MAX_SEND_ENV)
817 fatal("%s line %d: too many send env.",
818 filename, linenum);
819 options->send_env[options->num_send_env++] =
820 xstrdup(arg);
821 }
822 break;
823
Damien Miller0e220db2004-06-15 10:34:08 +1000824 case oControlPath:
825 charptr = &options->control_path;
826 goto parse_string;
827
828 case oControlMaster:
829 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000830 arg = strdelim(&s);
831 if (!arg || *arg == '\0')
832 fatal("%.200s line %d: Missing ControlMaster argument.",
833 filename, linenum);
834 value = 0; /* To avoid compiler warning... */
835 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
836 value = SSHCTL_MASTER_YES;
837 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
838 value = SSHCTL_MASTER_NO;
839 else if (strcmp(arg, "auto") == 0)
840 value = SSHCTL_MASTER_AUTO;
841 else if (strcmp(arg, "ask") == 0)
842 value = SSHCTL_MASTER_ASK;
843 else if (strcmp(arg, "autoask") == 0)
844 value = SSHCTL_MASTER_AUTO_ASK;
845 else
846 fatal("%.200s line %d: Bad ControlMaster argument.",
847 filename, linenum);
848 if (*activep && *intptr == -1)
849 *intptr = value;
850 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000851
Damien Millere1776152005-03-01 21:47:37 +1100852 case oHashKnownHosts:
853 intptr = &options->hash_known_hosts;
854 goto parse_flag;
855
Damien Millerd27b9472005-12-13 19:29:02 +1100856 case oTunnel:
857 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100858 arg = strdelim(&s);
859 if (!arg || *arg == '\0')
860 fatal("%s line %d: Missing yes/point-to-point/"
861 "ethernet/no argument.", filename, linenum);
862 value = 0; /* silence compiler */
863 if (strcasecmp(arg, "ethernet") == 0)
864 value = SSH_TUNMODE_ETHERNET;
865 else if (strcasecmp(arg, "point-to-point") == 0)
866 value = SSH_TUNMODE_POINTOPOINT;
867 else if (strcasecmp(arg, "yes") == 0)
868 value = SSH_TUNMODE_DEFAULT;
869 else if (strcasecmp(arg, "no") == 0)
870 value = SSH_TUNMODE_NO;
871 else
872 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
873 "no argument: %s", filename, linenum, arg);
874 if (*activep)
875 *intptr = value;
876 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100877
878 case oTunnelDevice:
879 arg = strdelim(&s);
880 if (!arg || *arg == '\0')
881 fatal("%.200s line %d: Missing argument.", filename, linenum);
882 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100883 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100884 fatal("%.200s line %d: Bad tun device.", filename, linenum);
885 if (*activep) {
886 options->tun_local = value;
887 options->tun_remote = value2;
888 }
889 break;
890
891 case oLocalCommand:
892 charptr = &options->local_command;
893 goto parse_command;
894
895 case oPermitLocalCommand:
896 intptr = &options->permit_local_command;
897 goto parse_flag;
898
Ben Lindstrom4daea862002-06-09 20:04:02 +0000899 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000900 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000901 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000902 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000903
Damien Millerf9b3feb2003-05-16 11:38:32 +1000904 case oUnsupported:
905 error("%s line %d: Unsupported option \"%s\"",
906 filename, linenum, keyword);
907 return 0;
908
Damien Miller95def091999-11-25 00:26:21 +1100909 default:
910 fatal("process_config_line: Unimplemented opcode %d", opcode);
911 }
912
913 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000914 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000915 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000916 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000917 }
Damien Miller95def091999-11-25 00:26:21 +1100918 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919}
920
921
Damien Miller5428f641999-11-25 11:54:57 +1100922/*
923 * Reads the config file and modifies the options accordingly. Options
924 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000925 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100926 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000928int
Darren Tuckerfc959702004-07-17 16:12:08 +1000929read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000930 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931{
Damien Miller95def091999-11-25 00:26:21 +1100932 FILE *f;
933 char line[1024];
934 int active, linenum;
935 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Damien Miller95def091999-11-25 00:26:21 +1100937 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000938 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000939 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940
Damien Miller57a44762004-04-20 20:11:57 +1000941 if (checkperm) {
942 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000943
Damien Miller33793852004-06-15 10:27:55 +1000944 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000945 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000946 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000947 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000948 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000949 }
950
Damien Miller95def091999-11-25 00:26:21 +1100951 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Damien Miller5428f641999-11-25 11:54:57 +1100953 /*
954 * Mark that we are now processing the options. This flag is turned
955 * on/off by Host specifications.
956 */
Damien Miller95def091999-11-25 00:26:21 +1100957 active = 1;
958 linenum = 0;
959 while (fgets(line, sizeof(line), f)) {
960 /* Update line number counter. */
961 linenum++;
962 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
963 bad_options++;
964 }
965 fclose(f);
966 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000967 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100968 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000969 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970}
971
Damien Miller5428f641999-11-25 11:54:57 +1100972/*
973 * Initializes options to special values that indicate that they have not yet
974 * been set. Read_config_file will only set options with this value. Options
975 * are processed in the following order: command line, user config file,
976 * system config file. Last, fill_default_options is called.
977 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller4af51302000-04-16 11:18:38 +1000979void
Damien Miller95def091999-11-25 00:26:21 +1100980initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981{
Damien Miller95def091999-11-25 00:26:21 +1100982 memset(options, 'X', sizeof(*options));
983 options->forward_agent = -1;
984 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000985 options->forward_x11_trusted = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000986 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100987 options->gateway_ports = -1;
988 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100989 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100990 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000991 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000992 options->gss_authentication = -1;
993 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100994 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100995 options->kbd_interactive_authentication = -1;
996 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100997 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000998 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100999 options->batch_mode = -1;
1000 options->check_host_ip = -1;
1001 options->strict_host_key_checking = -1;
1002 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001003 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001004 options->compression_level = -1;
1005 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001006 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001007 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001008 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001009 options->number_of_password_prompts = -1;
1010 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001011 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001012 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001013 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001014 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001015 options->num_identity_files = 0;
1016 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001017 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001018 options->proxy_command = NULL;
1019 options->user = NULL;
1020 options->escape_char = -1;
1021 options->system_hostfile = NULL;
1022 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001023 options->system_hostfile2 = NULL;
1024 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001025 options->num_local_forwards = 0;
1026 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001027 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001028 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001029 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001030 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001031 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001032 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001033 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001034 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001035 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001036 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001037 options->server_alive_interval = -1;
1038 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001039 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001040 options->control_path = NULL;
1041 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001042 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001043 options->tun_open = -1;
1044 options->tun_local = -1;
1045 options->tun_remote = -1;
1046 options->local_command = NULL;
1047 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048}
1049
Damien Miller5428f641999-11-25 11:54:57 +11001050/*
1051 * Called after processing other sources of option data, this fills those
1052 * options for which no value has been specified with their default values.
1053 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001054
Damien Miller4af51302000-04-16 11:18:38 +10001055void
Damien Miller95def091999-11-25 00:26:21 +11001056fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001058 int len;
1059
Damien Miller95def091999-11-25 00:26:21 +11001060 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001061 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001062 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001063 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001064 if (options->forward_x11_trusted == -1)
1065 options->forward_x11_trusted = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001066 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001067 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001068 if (options->gateway_ports == -1)
1069 options->gateway_ports = 0;
1070 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001071 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001072 if (options->rsa_authentication == -1)
1073 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001074 if (options->pubkey_authentication == -1)
1075 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001076 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001077 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001078 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001079 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001080 if (options->gss_deleg_creds == -1)
1081 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001082 if (options->password_authentication == -1)
1083 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001084 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001085 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001086 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001087 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001088 if (options->hostbased_authentication == -1)
1089 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001090 if (options->batch_mode == -1)
1091 options->batch_mode = 0;
1092 if (options->check_host_ip == -1)
1093 options->check_host_ip = 1;
1094 if (options->strict_host_key_checking == -1)
1095 options->strict_host_key_checking = 2; /* 2 is default */
1096 if (options->compression == -1)
1097 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001098 if (options->tcp_keep_alive == -1)
1099 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001100 if (options->compression_level == -1)
1101 options->compression_level = 6;
1102 if (options->port == -1)
1103 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001104 if (options->address_family == -1)
1105 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001106 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001107 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001108 if (options->number_of_password_prompts == -1)
1109 options->number_of_password_prompts = 3;
1110 /* Selected in ssh_login(). */
1111 if (options->cipher == -1)
1112 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001113 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001114 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001115 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001116 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001117 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001118 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001119 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001120 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001121 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001122 xmalloc(len);
1123 snprintf(options->identity_files[options->num_identity_files++],
1124 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001125 }
1126 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001127 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1128 options->identity_files[options->num_identity_files] =
1129 xmalloc(len);
1130 snprintf(options->identity_files[options->num_identity_files++],
1131 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1132
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001133 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001134 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001135 xmalloc(len);
1136 snprintf(options->identity_files[options->num_identity_files++],
1137 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001138 }
Damien Millereba71ba2000-04-29 23:57:08 +10001139 }
Damien Miller95def091999-11-25 00:26:21 +11001140 if (options->escape_char == -1)
1141 options->escape_char = '~';
1142 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001143 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001144 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001145 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001146 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001147 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001148 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001149 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001150 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001151 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001152 if (options->clear_forwardings == 1)
1153 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001154 if (options->no_host_authentication_for_localhost == - 1)
1155 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001156 if (options->identities_only == -1)
1157 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001158 if (options->enable_ssh_keysign == -1)
1159 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001160 if (options->rekey_limit == -1)
1161 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001162 if (options->verify_host_key_dns == -1)
1163 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001164 if (options->server_alive_interval == -1)
1165 options->server_alive_interval = 0;
1166 if (options->server_alive_count_max == -1)
1167 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001168 if (options->control_master == -1)
1169 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001170 if (options->hash_known_hosts == -1)
1171 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001172 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001173 options->tun_open = SSH_TUNMODE_NO;
1174 if (options->tun_local == -1)
1175 options->tun_local = SSH_TUNID_ANY;
1176 if (options->tun_remote == -1)
1177 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001178 if (options->permit_local_command == -1)
1179 options->permit_local_command = 0;
1180 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001181 /* options->proxy_command should not be set by default */
1182 /* options->user will be set in the main program if appropriate */
1183 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001184 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001185 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001187
1188/*
1189 * parse_forward
1190 * parses a string containing a port forwarding specification of the form:
1191 * [listenhost:]listenport:connecthost:connectport
1192 * returns number of arguments parsed or zero on error
1193 */
1194int
1195parse_forward(Forward *fwd, const char *fwdspec)
1196{
1197 int i;
1198 char *p, *cp, *fwdarg[4];
1199
1200 memset(fwd, '\0', sizeof(*fwd));
1201
1202 cp = p = xstrdup(fwdspec);
1203
1204 /* skip leading spaces */
1205 while (*cp && isspace(*cp))
1206 cp++;
1207
1208 for (i = 0; i < 4; ++i)
1209 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1210 break;
1211
1212 /* Check for trailing garbage in 4-arg case*/
1213 if (cp != NULL)
1214 i = 0; /* failure */
1215
1216 switch (i) {
1217 case 3:
1218 fwd->listen_host = NULL;
1219 fwd->listen_port = a2port(fwdarg[0]);
1220 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1221 fwd->connect_port = a2port(fwdarg[2]);
1222 break;
1223
1224 case 4:
1225 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1226 fwd->listen_port = a2port(fwdarg[1]);
1227 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1228 fwd->connect_port = a2port(fwdarg[3]);
1229 break;
1230 default:
1231 i = 0; /* failure */
1232 }
1233
1234 xfree(p);
1235
1236 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1237 goto fail_free;
1238
1239 if (fwd->connect_host != NULL &&
1240 strlen(fwd->connect_host) >= NI_MAXHOST)
1241 goto fail_free;
1242
1243 return (i);
1244
1245 fail_free:
1246 if (fwd->connect_host != NULL)
1247 xfree(fwd->connect_host);
1248 if (fwd->listen_host != NULL)
1249 xfree(fwd->listen_host);
1250 return (0);
1251}