blob: da704d803af7c056a4842015b521b5b3f0b8de4b [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 Millerf17883e2006-03-15 11:45:54 +110015RCSID("$OpenBSD: readconf.c,v 1.147 2006/02/20 17:19:54 stevesk Exp $");
16
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100022#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "cipher.h"
24#include "pathnames.h"
25#include "log.h"
26#include "readconf.h"
27#include "match.h"
28#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000029#include "kex.h"
30#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031
32/* Format of the configuration file:
33
34 # Configuration data is parsed as follows:
35 # 1. command line options
36 # 2. user-specific file
37 # 3. system-wide file
38 # Any configuration value is only changed the first time it is set.
39 # Thus, host-specific definitions should be at the beginning of the
40 # configuration file, and defaults at the end.
41
42 # Host-specific declarations. These may override anything above. A single
43 # host may match multiple declarations; these are processed in the order
44 # that they are given in.
45
46 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000047 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
49 Host fake.com
50 HostName another.host.name.real.org
51 User blaah
52 Port 34289
53 ForwardX11 no
54 ForwardAgent no
55
56 Host books.com
57 RemoteForward 9999 shadows.cs.hut.fi:9999
58 Cipher 3des
59
60 Host fascist.blob.com
61 Port 23123
62 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063 PasswordAuthentication no
64
65 Host puukko.hut.fi
66 User t35124p
67 ProxyCommand ssh-proxy %h %p
68
69 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000070 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
72 Host *.su
73 Cipher none
74 PasswordAuthentication no
75
Damien Millerd27b9472005-12-13 19:29:02 +110076 Host vpn.fake.com
77 Tunnel yes
78 TunnelDevice 3
79
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080 # Defaults for various options
81 Host *
82 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110083 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084 PasswordAuthentication yes
85 RSAAuthentication yes
86 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110088 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089 IdentityFile ~/.ssh/identity
90 Port 22
91 EscapeChar ~
92
93*/
94
95/* Keyword tokens. */
96
Damien Miller95def091999-11-25 00:26:21 +110097typedef enum {
98 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +100099 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000100 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000101 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100102 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
103 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
104 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
105 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100106 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000107 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100108 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000109 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000110 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000111 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000112 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000113 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000114 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100115 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100116 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100117 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000118 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119} OpCodes;
120
121/* Textual representations of the tokens. */
122
Damien Miller95def091999-11-25 00:26:21 +1100123static struct {
124 const char *name;
125 OpCodes opcode;
126} keywords[] = {
127 { "forwardagent", oForwardAgent },
128 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000129 { "forwardx11trusted", oForwardX11Trusted },
Damien Millerd3a18572000-06-07 19:55:44 +1000130 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100131 { "gatewayports", oGatewayPorts },
132 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000133 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100134 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100135 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
136 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100137 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100138 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000139 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000140 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000141 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000142 { "challengeresponseauthentication", oChallengeResponseAuthentication },
143 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
144 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000145 { "kerberosauthentication", oUnsupported },
146 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000147 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000148#if defined(GSSAPI)
149 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000150 { "gssapidelegatecredentials", oGssDelegateCreds },
151#else
152 { "gssapiauthentication", oUnsupported },
153 { "gssapidelegatecredentials", oUnsupported },
154#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000155 { "fallbacktorsh", oDeprecated },
156 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100157 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100158 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100159 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100160 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000161 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100162 { "proxycommand", oProxyCommand },
163 { "port", oPort },
164 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000165 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000166 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000167 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100168 { "remoteforward", oRemoteForward },
169 { "localforward", oLocalForward },
170 { "user", oUser },
171 { "host", oHost },
172 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000174 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000175 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000176 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100177 { "connectionattempts", oConnectionAttempts },
178 { "batchmode", oBatchMode },
179 { "checkhostip", oCheckHostIP },
180 { "stricthostkeychecking", oStrictHostKeyChecking },
181 { "compression", oCompression },
182 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100183 { "tcpkeepalive", oTCPKeepAlive },
184 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100185 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100186 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000187 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000188 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000189 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000190 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000191#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000192 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000193#else
194 { "smartcarddevice", oUnsupported },
195#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100196 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000197 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000198 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100199 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000200 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000201 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000202 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100203 { "serveraliveinterval", oServerAliveInterval },
204 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000205 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000206 { "controlpath", oControlPath },
207 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100208 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100209 { "tunnel", oTunnel },
210 { "tunneldevice", oTunnelDevice },
211 { "localcommand", oLocalCommand },
212 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000213 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100214};
215
Damien Miller5428f641999-11-25 11:54:57 +1100216/*
217 * Adds a local TCP/IP port forward to options. Never returns if there is an
218 * error.
219 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
Damien Miller4af51302000-04-16 11:18:38 +1000221void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100222add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223{
Damien Miller95def091999-11-25 00:26:21 +1100224 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000225#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100226 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100227 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000228 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100229#endif
Damien Miller95def091999-11-25 00:26:21 +1100230 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
231 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
232 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100233
234 fwd->listen_host = (newfwd->listen_host == NULL) ?
235 NULL : xstrdup(newfwd->listen_host);
236 fwd->listen_port = newfwd->listen_port;
237 fwd->connect_host = xstrdup(newfwd->connect_host);
238 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Miller5428f641999-11-25 11:54:57 +1100241/*
242 * Adds a remote TCP/IP port forward to options. Never returns if there is
243 * an error.
244 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller4af51302000-04-16 11:18:38 +1000246void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100247add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248{
Damien Miller95def091999-11-25 00:26:21 +1100249 Forward *fwd;
250 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
251 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100252 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100253 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100254
255 fwd->listen_host = (newfwd->listen_host == NULL) ?
256 NULL : xstrdup(newfwd->listen_host);
257 fwd->listen_port = newfwd->listen_port;
258 fwd->connect_host = xstrdup(newfwd->connect_host);
259 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260}
261
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000262static void
263clear_forwardings(Options *options)
264{
265 int i;
266
Damien Millerf91ee4c2005-03-01 21:24:33 +1100267 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100268 if (options->local_forwards[i].listen_host != NULL)
269 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100270 xfree(options->local_forwards[i].connect_host);
271 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000272 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100273 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100274 if (options->remote_forwards[i].listen_host != NULL)
275 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100276 xfree(options->remote_forwards[i].connect_host);
277 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000278 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100279 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000280}
281
Damien Miller5428f641999-11-25 11:54:57 +1100282/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000283 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100284 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller4af51302000-04-16 11:18:38 +1000286static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100287parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000289 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290
Damien Miller95def091999-11-25 00:26:21 +1100291 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100292 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100293 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000295 error("%s: line %d: Bad configuration option: %s",
296 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100297 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
Damien Miller5428f641999-11-25 11:54:57 +1100300/*
301 * Processes a single option line as used in the configuration files. This
302 * only sets those values that have not already been set.
303 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100304#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Miller2ccf6611999-11-15 15:25:10 +1100306int
307process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100308 char *line, const char *filename, int linenum,
309 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100311 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100312 int opcode, *intptr, value, value2, scale;
313 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100314 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100315 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
Damien Millerc652cac2003-05-14 13:40:54 +1000317 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100318 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000319 if (strchr(WHITESPACE, line[len]) == NULL)
320 break;
321 line[len] = '\0';
322 }
323
Damien Millerbe484b52000-07-15 14:14:16 +1000324 s = line;
325 /* Get the keyword. (Each line is supposed to begin with a keyword). */
326 keyword = strdelim(&s);
327 /* Ignore leading whitespace. */
328 if (*keyword == '\0')
329 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000330 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100331 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller37023962000-07-11 17:31:38 +1000333 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
Damien Miller95def091999-11-25 00:26:21 +1100335 switch (opcode) {
336 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100337 /* don't panic, but count bad options */
338 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100339 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000340 case oConnectTimeout:
341 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100342parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000343 arg = strdelim(&s);
344 if (!arg || *arg == '\0')
345 fatal("%s line %d: missing time value.",
346 filename, linenum);
347 if ((value = convtime(arg)) == -1)
348 fatal("%s line %d: invalid time value.",
349 filename, linenum);
350 if (*intptr == -1)
351 *intptr = value;
352 break;
353
Damien Miller95def091999-11-25 00:26:21 +1100354 case oForwardAgent:
355 intptr = &options->forward_agent;
356parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000357 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000358 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100359 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
360 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000361 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100362 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000363 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100364 value = 0;
365 else
366 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
367 if (*activep && *intptr == -1)
368 *intptr = value;
369 break;
370
371 case oForwardX11:
372 intptr = &options->forward_x11;
373 goto parse_flag;
374
Darren Tucker0a118da2003-10-15 15:54:32 +1000375 case oForwardX11Trusted:
376 intptr = &options->forward_x11_trusted;
377 goto parse_flag;
378
Damien Miller95def091999-11-25 00:26:21 +1100379 case oGatewayPorts:
380 intptr = &options->gateway_ports;
381 goto parse_flag;
382
383 case oUsePrivilegedPort:
384 intptr = &options->use_privileged_port;
385 goto parse_flag;
386
Damien Miller95def091999-11-25 00:26:21 +1100387 case oPasswordAuthentication:
388 intptr = &options->password_authentication;
389 goto parse_flag;
390
Damien Miller874d77b2000-10-14 16:23:11 +1100391 case oKbdInteractiveAuthentication:
392 intptr = &options->kbd_interactive_authentication;
393 goto parse_flag;
394
395 case oKbdInteractiveDevices:
396 charptr = &options->kbd_interactive_devices;
397 goto parse_string;
398
Damien Miller0bc1bd82000-11-13 22:57:25 +1100399 case oPubkeyAuthentication:
400 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000401 goto parse_flag;
402
Damien Miller95def091999-11-25 00:26:21 +1100403 case oRSAAuthentication:
404 intptr = &options->rsa_authentication;
405 goto parse_flag;
406
407 case oRhostsRSAAuthentication:
408 intptr = &options->rhosts_rsa_authentication;
409 goto parse_flag;
410
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000411 case oHostbasedAuthentication:
412 intptr = &options->hostbased_authentication;
413 goto parse_flag;
414
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000415 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000416 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100417 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000418
Darren Tucker0efd1552003-08-26 11:49:55 +1000419 case oGssAuthentication:
420 intptr = &options->gss_authentication;
421 goto parse_flag;
422
423 case oGssDelegateCreds:
424 intptr = &options->gss_deleg_creds;
425 goto parse_flag;
426
Damien Miller95def091999-11-25 00:26:21 +1100427 case oBatchMode:
428 intptr = &options->batch_mode;
429 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Damien Miller95def091999-11-25 00:26:21 +1100431 case oCheckHostIP:
432 intptr = &options->check_host_ip;
433 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Damien Miller37876e92003-05-15 10:19:46 +1000435 case oVerifyHostKeyDNS:
436 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100437 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000438
Damien Miller95def091999-11-25 00:26:21 +1100439 case oStrictHostKeyChecking:
440 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100441parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000442 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000443 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000444 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100445 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100446 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000447 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100448 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000449 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100450 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000451 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100452 value = 2;
453 else
454 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
455 if (*activep && *intptr == -1)
456 *intptr = value;
457 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458
Damien Miller95def091999-11-25 00:26:21 +1100459 case oCompression:
460 intptr = &options->compression;
461 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462
Damien Miller12c150e2003-12-17 16:31:10 +1100463 case oTCPKeepAlive:
464 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100465 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000467 case oNoHostAuthenticationForLocalhost:
468 intptr = &options->no_host_authentication_for_localhost;
469 goto parse_flag;
470
Damien Miller95def091999-11-25 00:26:21 +1100471 case oNumberOfPasswordPrompts:
472 intptr = &options->number_of_password_prompts;
473 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 case oCompressionLevel:
476 intptr = &options->compression_level;
477 goto parse_int;
478
Damien Millera5539d22003-04-09 20:50:06 +1000479 case oRekeyLimit:
480 intptr = &options->rekey_limit;
481 arg = strdelim(&s);
482 if (!arg || *arg == '\0')
483 fatal("%.200s line %d: Missing argument.", filename, linenum);
484 if (arg[0] < '0' || arg[0] > '9')
485 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100486 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000487 if (arg == endofnumber)
488 fatal("%.200s line %d: Bad number.", filename, linenum);
489 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100490 case '\0':
491 scale = 1;
492 break;
Damien Millera5539d22003-04-09 20:50:06 +1000493 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100494 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000495 break;
496 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100497 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000498 break;
499 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100500 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000501 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100502 default:
503 fatal("%.200s line %d: Invalid RekeyLimit suffix",
504 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000505 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100506 val64 *= scale;
507 /* detect integer wrap and too-large limits */
508 if ((val64 / scale) != orig || val64 > INT_MAX)
509 fatal("%.200s line %d: RekeyLimit too large",
510 filename, linenum);
511 if (val64 < 16)
512 fatal("%.200s line %d: RekeyLimit too small",
513 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000514 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100515 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000516 break;
517
Damien Miller95def091999-11-25 00:26:21 +1100518 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000519 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000520 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100521 fatal("%.200s line %d: Missing argument.", filename, linenum);
522 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100523 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000524 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100525 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100526 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100527 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000528 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000529 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100530 }
531 break;
532
Damien Millerd3a18572000-06-07 19:55:44 +1000533 case oXAuthLocation:
534 charptr=&options->xauth_location;
535 goto parse_string;
536
Damien Miller95def091999-11-25 00:26:21 +1100537 case oUser:
538 charptr = &options->user;
539parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000540 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000541 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100542 fatal("%.200s line %d: Missing argument.", filename, linenum);
543 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000544 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100545 break;
546
547 case oGlobalKnownHostsFile:
548 charptr = &options->system_hostfile;
549 goto parse_string;
550
551 case oUserKnownHostsFile:
552 charptr = &options->user_hostfile;
553 goto parse_string;
554
Damien Millereba71ba2000-04-29 23:57:08 +1000555 case oGlobalKnownHostsFile2:
556 charptr = &options->system_hostfile2;
557 goto parse_string;
558
559 case oUserKnownHostsFile2:
560 charptr = &options->user_hostfile2;
561 goto parse_string;
562
Damien Miller95def091999-11-25 00:26:21 +1100563 case oHostName:
564 charptr = &options->hostname;
565 goto parse_string;
566
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000567 case oHostKeyAlias:
568 charptr = &options->host_key_alias;
569 goto parse_string;
570
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000571 case oPreferredAuthentications:
572 charptr = &options->preferred_authentications;
573 goto parse_string;
574
Ben Lindstrome0f88042001-04-30 13:06:24 +0000575 case oBindAddress:
576 charptr = &options->bind_address;
577 goto parse_string;
578
Ben Lindstromae996bf2001-08-06 21:27:53 +0000579 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000580 charptr = &options->smartcard_device;
581 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000582
Damien Miller95def091999-11-25 00:26:21 +1100583 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100584 charptr = &options->proxy_command;
585parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000586 if (s == NULL)
587 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100588 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100589 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100590 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100591 return 0;
592
593 case oPort:
594 intptr = &options->port;
595parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000596 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000597 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100598 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000599 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100600 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100601
602 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000603 value = strtol(arg, &endofnumber, 0);
604 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100605 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100606 if (*activep && *intptr == -1)
607 *intptr = value;
608 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609
Damien Miller95def091999-11-25 00:26:21 +1100610 case oConnectionAttempts:
611 intptr = &options->connection_attempts;
612 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100613
Damien Miller95def091999-11-25 00:26:21 +1100614 case oCipher:
615 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000616 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000617 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000618 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000619 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100620 if (value == -1)
621 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100622 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100623 if (*activep && *intptr == -1)
624 *intptr = value;
625 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Damien Miller78928792000-04-12 20:17:38 +1000627 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000628 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000629 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000630 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000631 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000632 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100633 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000634 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000635 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000636 break;
637
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000638 case oMacs:
639 arg = strdelim(&s);
640 if (!arg || *arg == '\0')
641 fatal("%.200s line %d: Missing argument.", filename, linenum);
642 if (!mac_valid(arg))
643 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100644 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000645 if (*activep && options->macs == NULL)
646 options->macs = xstrdup(arg);
647 break;
648
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000649 case oHostKeyAlgorithms:
650 arg = strdelim(&s);
651 if (!arg || *arg == '\0')
652 fatal("%.200s line %d: Missing argument.", filename, linenum);
653 if (!key_names_valid2(arg))
654 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100655 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000656 if (*activep && options->hostkeyalgorithms == NULL)
657 options->hostkeyalgorithms = xstrdup(arg);
658 break;
659
Damien Miller78928792000-04-12 20:17:38 +1000660 case oProtocol:
661 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000662 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000663 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000664 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000665 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000666 if (value == SSH_PROTO_UNKNOWN)
667 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100668 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000669 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
670 *intptr = value;
671 break;
672
Damien Miller95def091999-11-25 00:26:21 +1100673 case oLogLevel:
674 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000675 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000676 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100677 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000678 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100679 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100680 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100681 *intptr = (LogLevel) value;
682 break;
683
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000684 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100685 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000686 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100687 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000688 fatal("%.200s line %d: Missing port argument.",
689 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100690 arg2 = strdelim(&s);
691 if (arg2 == NULL || *arg2 == '\0')
692 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000693 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100694
695 /* construct a string for parse_forward */
696 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
697
698 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000699 fatal("%.200s line %d: Bad forwarding specification.",
700 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100701
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000702 if (*activep) {
703 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100704 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000705 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100706 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000707 }
Damien Miller95def091999-11-25 00:26:21 +1100708 break;
709
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000710 case oDynamicForward:
711 arg = strdelim(&s);
712 if (!arg || *arg == '\0')
713 fatal("%.200s line %d: Missing port argument.",
714 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100715 memset(&fwd, '\0', sizeof(fwd));
716 fwd.connect_host = "socks";
717 fwd.listen_host = hpdelim(&arg);
718 if (fwd.listen_host == NULL ||
719 strlen(fwd.listen_host) >= NI_MAXHOST)
720 fatal("%.200s line %d: Bad forwarding specification.",
721 filename, linenum);
722 if (arg) {
723 fwd.listen_port = a2port(arg);
724 fwd.listen_host = cleanhostname(fwd.listen_host);
725 } else {
726 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000727 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100728 }
729 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000730 fatal("%.200s line %d: Badly formatted port number.",
731 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000732 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100733 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000734 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000735
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000736 case oClearAllForwardings:
737 intptr = &options->clear_forwardings;
738 goto parse_flag;
739
Damien Miller95def091999-11-25 00:26:21 +1100740 case oHost:
741 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000742 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000743 if (match_pattern(host, arg)) {
744 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100745 *activep = 1;
746 break;
747 }
Damien Millerbe484b52000-07-15 14:14:16 +1000748 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100749 return 0;
750
751 case oEscapeChar:
752 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000753 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000754 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100755 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000756 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000757 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
758 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000759 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000760 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000761 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000762 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100763 else {
764 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100765 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100766 /* NOTREACHED */
767 value = 0; /* Avoid compiler warning. */
768 }
769 if (*activep && *intptr == -1)
770 *intptr = value;
771 break;
772
Damien Miller20a8f972003-05-18 20:50:30 +1000773 case oAddressFamily:
774 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000775 if (!arg || *arg == '\0')
776 fatal("%s line %d: missing address family.",
777 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000778 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000779 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000780 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000781 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000782 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000783 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000784 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000785 else
786 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000787 if (*activep && *intptr == -1)
788 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000789 break;
790
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000791 case oEnableSSHKeysign:
792 intptr = &options->enable_ssh_keysign;
793 goto parse_flag;
794
Damien Millerbd394c32004-03-08 23:12:36 +1100795 case oIdentitiesOnly:
796 intptr = &options->identities_only;
797 goto parse_flag;
798
Damien Miller509b0102003-12-17 16:33:10 +1100799 case oServerAliveInterval:
800 intptr = &options->server_alive_interval;
801 goto parse_time;
802
803 case oServerAliveCountMax:
804 intptr = &options->server_alive_count_max;
805 goto parse_int;
806
Darren Tucker46bc0752004-05-02 22:11:30 +1000807 case oSendEnv:
808 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
809 if (strchr(arg, '=') != NULL)
810 fatal("%s line %d: Invalid environment name.",
811 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100812 if (!*activep)
813 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000814 if (options->num_send_env >= MAX_SEND_ENV)
815 fatal("%s line %d: too many send env.",
816 filename, linenum);
817 options->send_env[options->num_send_env++] =
818 xstrdup(arg);
819 }
820 break;
821
Damien Miller0e220db2004-06-15 10:34:08 +1000822 case oControlPath:
823 charptr = &options->control_path;
824 goto parse_string;
825
826 case oControlMaster:
827 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000828 arg = strdelim(&s);
829 if (!arg || *arg == '\0')
830 fatal("%.200s line %d: Missing ControlMaster argument.",
831 filename, linenum);
832 value = 0; /* To avoid compiler warning... */
833 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
834 value = SSHCTL_MASTER_YES;
835 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
836 value = SSHCTL_MASTER_NO;
837 else if (strcmp(arg, "auto") == 0)
838 value = SSHCTL_MASTER_AUTO;
839 else if (strcmp(arg, "ask") == 0)
840 value = SSHCTL_MASTER_ASK;
841 else if (strcmp(arg, "autoask") == 0)
842 value = SSHCTL_MASTER_AUTO_ASK;
843 else
844 fatal("%.200s line %d: Bad ControlMaster argument.",
845 filename, linenum);
846 if (*activep && *intptr == -1)
847 *intptr = value;
848 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000849
Damien Millere1776152005-03-01 21:47:37 +1100850 case oHashKnownHosts:
851 intptr = &options->hash_known_hosts;
852 goto parse_flag;
853
Damien Millerd27b9472005-12-13 19:29:02 +1100854 case oTunnel:
855 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100856 arg = strdelim(&s);
857 if (!arg || *arg == '\0')
858 fatal("%s line %d: Missing yes/point-to-point/"
859 "ethernet/no argument.", filename, linenum);
860 value = 0; /* silence compiler */
861 if (strcasecmp(arg, "ethernet") == 0)
862 value = SSH_TUNMODE_ETHERNET;
863 else if (strcasecmp(arg, "point-to-point") == 0)
864 value = SSH_TUNMODE_POINTOPOINT;
865 else if (strcasecmp(arg, "yes") == 0)
866 value = SSH_TUNMODE_DEFAULT;
867 else if (strcasecmp(arg, "no") == 0)
868 value = SSH_TUNMODE_NO;
869 else
870 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
871 "no argument: %s", filename, linenum, arg);
872 if (*activep)
873 *intptr = value;
874 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100875
876 case oTunnelDevice:
877 arg = strdelim(&s);
878 if (!arg || *arg == '\0')
879 fatal("%.200s line %d: Missing argument.", filename, linenum);
880 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100881 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100882 fatal("%.200s line %d: Bad tun device.", filename, linenum);
883 if (*activep) {
884 options->tun_local = value;
885 options->tun_remote = value2;
886 }
887 break;
888
889 case oLocalCommand:
890 charptr = &options->local_command;
891 goto parse_command;
892
893 case oPermitLocalCommand:
894 intptr = &options->permit_local_command;
895 goto parse_flag;
896
Ben Lindstrom4daea862002-06-09 20:04:02 +0000897 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000898 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000899 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000900 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000901
Damien Millerf9b3feb2003-05-16 11:38:32 +1000902 case oUnsupported:
903 error("%s line %d: Unsupported option \"%s\"",
904 filename, linenum, keyword);
905 return 0;
906
Damien Miller95def091999-11-25 00:26:21 +1100907 default:
908 fatal("process_config_line: Unimplemented opcode %d", opcode);
909 }
910
911 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000912 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000913 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000914 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000915 }
Damien Miller95def091999-11-25 00:26:21 +1100916 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917}
918
919
Damien Miller5428f641999-11-25 11:54:57 +1100920/*
921 * Reads the config file and modifies the options accordingly. Options
922 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000923 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100924 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000926int
Darren Tuckerfc959702004-07-17 16:12:08 +1000927read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000928 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929{
Damien Miller95def091999-11-25 00:26:21 +1100930 FILE *f;
931 char line[1024];
932 int active, linenum;
933 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Miller95def091999-11-25 00:26:21 +1100935 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000936 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000937 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938
Damien Miller57a44762004-04-20 20:11:57 +1000939 if (checkperm) {
940 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000941
Damien Miller33793852004-06-15 10:27:55 +1000942 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000943 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000944 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000945 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000946 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000947 }
948
Damien Miller95def091999-11-25 00:26:21 +1100949 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Damien Miller5428f641999-11-25 11:54:57 +1100951 /*
952 * Mark that we are now processing the options. This flag is turned
953 * on/off by Host specifications.
954 */
Damien Miller95def091999-11-25 00:26:21 +1100955 active = 1;
956 linenum = 0;
957 while (fgets(line, sizeof(line), f)) {
958 /* Update line number counter. */
959 linenum++;
960 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
961 bad_options++;
962 }
963 fclose(f);
964 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000965 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100966 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000967 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968}
969
Damien Miller5428f641999-11-25 11:54:57 +1100970/*
971 * Initializes options to special values that indicate that they have not yet
972 * been set. Read_config_file will only set options with this value. Options
973 * are processed in the following order: command line, user config file,
974 * system config file. Last, fill_default_options is called.
975 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller4af51302000-04-16 11:18:38 +1000977void
Damien Miller95def091999-11-25 00:26:21 +1100978initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979{
Damien Miller95def091999-11-25 00:26:21 +1100980 memset(options, 'X', sizeof(*options));
981 options->forward_agent = -1;
982 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000983 options->forward_x11_trusted = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000984 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100985 options->gateway_ports = -1;
986 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100987 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100988 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000989 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000990 options->gss_authentication = -1;
991 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100992 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100993 options->kbd_interactive_authentication = -1;
994 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100995 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000996 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100997 options->batch_mode = -1;
998 options->check_host_ip = -1;
999 options->strict_host_key_checking = -1;
1000 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001001 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001002 options->compression_level = -1;
1003 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001004 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001005 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001006 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001007 options->number_of_password_prompts = -1;
1008 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001009 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001010 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001011 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001012 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001013 options->num_identity_files = 0;
1014 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001015 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001016 options->proxy_command = NULL;
1017 options->user = NULL;
1018 options->escape_char = -1;
1019 options->system_hostfile = NULL;
1020 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001021 options->system_hostfile2 = NULL;
1022 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001023 options->num_local_forwards = 0;
1024 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001025 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001026 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001027 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001028 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001029 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001030 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001031 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001032 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001033 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001034 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001035 options->server_alive_interval = -1;
1036 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001037 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001038 options->control_path = NULL;
1039 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001040 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001041 options->tun_open = -1;
1042 options->tun_local = -1;
1043 options->tun_remote = -1;
1044 options->local_command = NULL;
1045 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001046}
1047
Damien Miller5428f641999-11-25 11:54:57 +11001048/*
1049 * Called after processing other sources of option data, this fills those
1050 * options for which no value has been specified with their default values.
1051 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052
Damien Miller4af51302000-04-16 11:18:38 +10001053void
Damien Miller95def091999-11-25 00:26:21 +11001054fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001056 int len;
1057
Damien Miller95def091999-11-25 00:26:21 +11001058 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001059 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001060 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001061 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001062 if (options->forward_x11_trusted == -1)
1063 options->forward_x11_trusted = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001064 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001065 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001066 if (options->gateway_ports == -1)
1067 options->gateway_ports = 0;
1068 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001069 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001070 if (options->rsa_authentication == -1)
1071 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001072 if (options->pubkey_authentication == -1)
1073 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001074 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001075 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001076 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001077 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001078 if (options->gss_deleg_creds == -1)
1079 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001080 if (options->password_authentication == -1)
1081 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001082 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001083 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001084 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001085 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001086 if (options->hostbased_authentication == -1)
1087 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001088 if (options->batch_mode == -1)
1089 options->batch_mode = 0;
1090 if (options->check_host_ip == -1)
1091 options->check_host_ip = 1;
1092 if (options->strict_host_key_checking == -1)
1093 options->strict_host_key_checking = 2; /* 2 is default */
1094 if (options->compression == -1)
1095 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001096 if (options->tcp_keep_alive == -1)
1097 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001098 if (options->compression_level == -1)
1099 options->compression_level = 6;
1100 if (options->port == -1)
1101 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001102 if (options->address_family == -1)
1103 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001104 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001105 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001106 if (options->number_of_password_prompts == -1)
1107 options->number_of_password_prompts = 3;
1108 /* Selected in ssh_login(). */
1109 if (options->cipher == -1)
1110 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001111 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001112 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001113 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001114 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001115 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001116 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001117 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001118 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001119 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001120 xmalloc(len);
1121 snprintf(options->identity_files[options->num_identity_files++],
1122 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001123 }
1124 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001125 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1126 options->identity_files[options->num_identity_files] =
1127 xmalloc(len);
1128 snprintf(options->identity_files[options->num_identity_files++],
1129 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1130
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001131 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001132 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001133 xmalloc(len);
1134 snprintf(options->identity_files[options->num_identity_files++],
1135 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001136 }
Damien Millereba71ba2000-04-29 23:57:08 +10001137 }
Damien Miller95def091999-11-25 00:26:21 +11001138 if (options->escape_char == -1)
1139 options->escape_char = '~';
1140 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001141 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001142 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001143 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001144 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001145 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001146 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001147 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001148 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001149 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001150 if (options->clear_forwardings == 1)
1151 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001152 if (options->no_host_authentication_for_localhost == - 1)
1153 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001154 if (options->identities_only == -1)
1155 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001156 if (options->enable_ssh_keysign == -1)
1157 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001158 if (options->rekey_limit == -1)
1159 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001160 if (options->verify_host_key_dns == -1)
1161 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001162 if (options->server_alive_interval == -1)
1163 options->server_alive_interval = 0;
1164 if (options->server_alive_count_max == -1)
1165 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001166 if (options->control_master == -1)
1167 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001168 if (options->hash_known_hosts == -1)
1169 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001170 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001171 options->tun_open = SSH_TUNMODE_NO;
1172 if (options->tun_local == -1)
1173 options->tun_local = SSH_TUNID_ANY;
1174 if (options->tun_remote == -1)
1175 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001176 if (options->permit_local_command == -1)
1177 options->permit_local_command = 0;
1178 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001179 /* options->proxy_command should not be set by default */
1180 /* options->user will be set in the main program if appropriate */
1181 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001182 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001183 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001185
1186/*
1187 * parse_forward
1188 * parses a string containing a port forwarding specification of the form:
1189 * [listenhost:]listenport:connecthost:connectport
1190 * returns number of arguments parsed or zero on error
1191 */
1192int
1193parse_forward(Forward *fwd, const char *fwdspec)
1194{
1195 int i;
1196 char *p, *cp, *fwdarg[4];
1197
1198 memset(fwd, '\0', sizeof(*fwd));
1199
1200 cp = p = xstrdup(fwdspec);
1201
1202 /* skip leading spaces */
1203 while (*cp && isspace(*cp))
1204 cp++;
1205
1206 for (i = 0; i < 4; ++i)
1207 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1208 break;
1209
1210 /* Check for trailing garbage in 4-arg case*/
1211 if (cp != NULL)
1212 i = 0; /* failure */
1213
1214 switch (i) {
1215 case 3:
1216 fwd->listen_host = NULL;
1217 fwd->listen_port = a2port(fwdarg[0]);
1218 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1219 fwd->connect_port = a2port(fwdarg[2]);
1220 break;
1221
1222 case 4:
1223 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1224 fwd->listen_port = a2port(fwdarg[1]);
1225 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1226 fwd->connect_port = a2port(fwdarg[3]);
1227 break;
1228 default:
1229 i = 0; /* failure */
1230 }
1231
1232 xfree(p);
1233
1234 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1235 goto fail_free;
1236
1237 if (fwd->connect_host != NULL &&
1238 strlen(fwd->connect_host) >= NI_MAXHOST)
1239 goto fail_free;
1240
1241 return (i);
1242
1243 fail_free:
1244 if (fwd->connect_host != NULL)
1245 xfree(fwd->connect_host);
1246 if (fwd->listen_host != NULL)
1247 xfree(fwd->listen_host);
1248 return (0);
1249}