blob: d2e17230383fe99efe89a0c0e40e04ac148e10bf [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: readconf.c,v 1.154 2006/07/11 20:07:25 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
20
21#include <netinet/in.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Millerc7b06362006-03-15 11:53:45 +110023#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100024#include <errno.h>
Damien Millerc7b06362006-03-15 11:53:45 +110025
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100028#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "cipher.h"
30#include "pathnames.h"
31#include "log.h"
32#include "readconf.h"
33#include "match.h"
34#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000035#include "kex.h"
36#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38/* Format of the configuration file:
39
40 # Configuration data is parsed as follows:
41 # 1. command line options
42 # 2. user-specific file
43 # 3. system-wide file
44 # Any configuration value is only changed the first time it is set.
45 # Thus, host-specific definitions should be at the beginning of the
46 # configuration file, and defaults at the end.
47
48 # Host-specific declarations. These may override anything above. A single
49 # host may match multiple declarations; these are processed in the order
50 # that they are given in.
51
52 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000053 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
55 Host fake.com
56 HostName another.host.name.real.org
57 User blaah
58 Port 34289
59 ForwardX11 no
60 ForwardAgent no
61
62 Host books.com
63 RemoteForward 9999 shadows.cs.hut.fi:9999
64 Cipher 3des
65
66 Host fascist.blob.com
67 Port 23123
68 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069 PasswordAuthentication no
70
71 Host puukko.hut.fi
72 User t35124p
73 ProxyCommand ssh-proxy %h %p
74
75 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000076 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
78 Host *.su
79 Cipher none
80 PasswordAuthentication no
81
Damien Millerd27b9472005-12-13 19:29:02 +110082 Host vpn.fake.com
83 Tunnel yes
84 TunnelDevice 3
85
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086 # Defaults for various options
87 Host *
88 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110089 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 PasswordAuthentication yes
91 RSAAuthentication yes
92 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110094 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095 IdentityFile ~/.ssh/identity
96 Port 22
97 EscapeChar ~
98
99*/
100
101/* Keyword tokens. */
102
Damien Miller95def091999-11-25 00:26:21 +1100103typedef enum {
104 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000105 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000106 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000107 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000108 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100109 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
110 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
111 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
112 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100113 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000114 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000116 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000117 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000118 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000119 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000120 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000121 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100122 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100123 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100124 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000125 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126} OpCodes;
127
128/* Textual representations of the tokens. */
129
Damien Miller95def091999-11-25 00:26:21 +1100130static struct {
131 const char *name;
132 OpCodes opcode;
133} keywords[] = {
134 { "forwardagent", oForwardAgent },
135 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000136 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000137 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000138 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100139 { "gatewayports", oGatewayPorts },
140 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000141 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100142 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100143 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
144 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100145 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100146 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000147 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000148 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000149 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000150 { "challengeresponseauthentication", oChallengeResponseAuthentication },
151 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
152 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000153 { "kerberosauthentication", oUnsupported },
154 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000155 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000156#if defined(GSSAPI)
157 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000158 { "gssapidelegatecredentials", oGssDelegateCreds },
159#else
160 { "gssapiauthentication", oUnsupported },
161 { "gssapidelegatecredentials", oUnsupported },
162#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000163 { "fallbacktorsh", oDeprecated },
164 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100165 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100166 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100167 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100168 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000169 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "proxycommand", oProxyCommand },
171 { "port", oPort },
172 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000173 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000174 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000175 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100176 { "remoteforward", oRemoteForward },
177 { "localforward", oLocalForward },
178 { "user", oUser },
179 { "host", oHost },
180 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100181 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000182 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000183 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000184 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100185 { "connectionattempts", oConnectionAttempts },
186 { "batchmode", oBatchMode },
187 { "checkhostip", oCheckHostIP },
188 { "stricthostkeychecking", oStrictHostKeyChecking },
189 { "compression", oCompression },
190 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100191 { "tcpkeepalive", oTCPKeepAlive },
192 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100193 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100194 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000195 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000196 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000197 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000198 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000199#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000200 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000201#else
202 { "smartcarddevice", oUnsupported },
203#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100204 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000205 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000206 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100207 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000208 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000209 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000210 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100211 { "serveraliveinterval", oServerAliveInterval },
212 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000213 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000214 { "controlpath", oControlPath },
215 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100216 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100217 { "tunnel", oTunnel },
218 { "tunneldevice", oTunnelDevice },
219 { "localcommand", oLocalCommand },
220 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000221 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100222};
223
Damien Miller5428f641999-11-25 11:54:57 +1100224/*
225 * Adds a local TCP/IP port forward to options. Never returns if there is an
226 * error.
227 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Damien Miller4af51302000-04-16 11:18:38 +1000229void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100230add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231{
Damien Miller95def091999-11-25 00:26:21 +1100232 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000233#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100234 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100235 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000236 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100237#endif
Damien Miller95def091999-11-25 00:26:21 +1100238 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
239 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
240 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100241
242 fwd->listen_host = (newfwd->listen_host == NULL) ?
243 NULL : xstrdup(newfwd->listen_host);
244 fwd->listen_port = newfwd->listen_port;
245 fwd->connect_host = xstrdup(newfwd->connect_host);
246 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247}
248
Damien Miller5428f641999-11-25 11:54:57 +1100249/*
250 * Adds a remote TCP/IP port forward to options. Never returns if there is
251 * an error.
252 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller4af51302000-04-16 11:18:38 +1000254void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100255add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256{
Damien Miller95def091999-11-25 00:26:21 +1100257 Forward *fwd;
258 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
259 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100260 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100261 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100262
263 fwd->listen_host = (newfwd->listen_host == NULL) ?
264 NULL : xstrdup(newfwd->listen_host);
265 fwd->listen_port = newfwd->listen_port;
266 fwd->connect_host = xstrdup(newfwd->connect_host);
267 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268}
269
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000270static void
271clear_forwardings(Options *options)
272{
273 int i;
274
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100276 if (options->local_forwards[i].listen_host != NULL)
277 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278 xfree(options->local_forwards[i].connect_host);
279 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000280 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100281 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100282 if (options->remote_forwards[i].listen_host != NULL)
283 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100284 xfree(options->remote_forwards[i].connect_host);
285 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000286 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100287 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000288}
289
Damien Miller5428f641999-11-25 11:54:57 +1100290/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000291 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100292 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293
Damien Miller4af51302000-04-16 11:18:38 +1000294static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100295parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000297 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Miller95def091999-11-25 00:26:21 +1100299 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100300 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100301 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000303 error("%s: line %d: Bad configuration option: %s",
304 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100305 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
Damien Miller5428f641999-11-25 11:54:57 +1100308/*
309 * Processes a single option line as used in the configuration files. This
310 * only sets those values that have not already been set.
311 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100312#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Damien Miller2ccf6611999-11-15 15:25:10 +1100314int
315process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100316 char *line, const char *filename, int linenum,
317 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100319 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100320 int opcode, *intptr, value, value2, scale;
321 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100322 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100323 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Millerc652cac2003-05-14 13:40:54 +1000325 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100326 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000327 if (strchr(WHITESPACE, line[len]) == NULL)
328 break;
329 line[len] = '\0';
330 }
331
Damien Millerbe484b52000-07-15 14:14:16 +1000332 s = line;
333 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100334 if ((keyword = strdelim(&s)) == NULL)
335 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000336 /* Ignore leading whitespace. */
337 if (*keyword == '\0')
338 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000339 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100340 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Miller37023962000-07-11 17:31:38 +1000342 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343
Damien Miller95def091999-11-25 00:26:21 +1100344 switch (opcode) {
345 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100346 /* don't panic, but count bad options */
347 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100348 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000349 case oConnectTimeout:
350 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100351parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000352 arg = strdelim(&s);
353 if (!arg || *arg == '\0')
354 fatal("%s line %d: missing time value.",
355 filename, linenum);
356 if ((value = convtime(arg)) == -1)
357 fatal("%s line %d: invalid time value.",
358 filename, linenum);
359 if (*intptr == -1)
360 *intptr = value;
361 break;
362
Damien Miller95def091999-11-25 00:26:21 +1100363 case oForwardAgent:
364 intptr = &options->forward_agent;
365parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000366 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000367 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100368 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
369 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000370 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100371 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000372 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100373 value = 0;
374 else
375 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
376 if (*activep && *intptr == -1)
377 *intptr = value;
378 break;
379
380 case oForwardX11:
381 intptr = &options->forward_x11;
382 goto parse_flag;
383
Darren Tucker0a118da2003-10-15 15:54:32 +1000384 case oForwardX11Trusted:
385 intptr = &options->forward_x11_trusted;
386 goto parse_flag;
387
Damien Miller95def091999-11-25 00:26:21 +1100388 case oGatewayPorts:
389 intptr = &options->gateway_ports;
390 goto parse_flag;
391
Darren Tuckere7d4b192006-07-12 22:17:10 +1000392 case oExitOnForwardFailure:
393 intptr = &options->exit_on_forward_failure;
394 goto parse_flag;
395
Damien Miller95def091999-11-25 00:26:21 +1100396 case oUsePrivilegedPort:
397 intptr = &options->use_privileged_port;
398 goto parse_flag;
399
Damien Miller95def091999-11-25 00:26:21 +1100400 case oPasswordAuthentication:
401 intptr = &options->password_authentication;
402 goto parse_flag;
403
Damien Miller874d77b2000-10-14 16:23:11 +1100404 case oKbdInteractiveAuthentication:
405 intptr = &options->kbd_interactive_authentication;
406 goto parse_flag;
407
408 case oKbdInteractiveDevices:
409 charptr = &options->kbd_interactive_devices;
410 goto parse_string;
411
Damien Miller0bc1bd82000-11-13 22:57:25 +1100412 case oPubkeyAuthentication:
413 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000414 goto parse_flag;
415
Damien Miller95def091999-11-25 00:26:21 +1100416 case oRSAAuthentication:
417 intptr = &options->rsa_authentication;
418 goto parse_flag;
419
420 case oRhostsRSAAuthentication:
421 intptr = &options->rhosts_rsa_authentication;
422 goto parse_flag;
423
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000424 case oHostbasedAuthentication:
425 intptr = &options->hostbased_authentication;
426 goto parse_flag;
427
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000428 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000429 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100430 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000431
Darren Tucker0efd1552003-08-26 11:49:55 +1000432 case oGssAuthentication:
433 intptr = &options->gss_authentication;
434 goto parse_flag;
435
436 case oGssDelegateCreds:
437 intptr = &options->gss_deleg_creds;
438 goto parse_flag;
439
Damien Miller95def091999-11-25 00:26:21 +1100440 case oBatchMode:
441 intptr = &options->batch_mode;
442 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443
Damien Miller95def091999-11-25 00:26:21 +1100444 case oCheckHostIP:
445 intptr = &options->check_host_ip;
446 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
Damien Miller37876e92003-05-15 10:19:46 +1000448 case oVerifyHostKeyDNS:
449 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100450 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000451
Damien Miller95def091999-11-25 00:26:21 +1100452 case oStrictHostKeyChecking:
453 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100454parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000455 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000456 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000457 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100458 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100459 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000460 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100461 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000462 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100463 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000464 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100465 value = 2;
466 else
467 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
468 if (*activep && *intptr == -1)
469 *intptr = value;
470 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471
Damien Miller95def091999-11-25 00:26:21 +1100472 case oCompression:
473 intptr = &options->compression;
474 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475
Damien Miller12c150e2003-12-17 16:31:10 +1100476 case oTCPKeepAlive:
477 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100478 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000480 case oNoHostAuthenticationForLocalhost:
481 intptr = &options->no_host_authentication_for_localhost;
482 goto parse_flag;
483
Damien Miller95def091999-11-25 00:26:21 +1100484 case oNumberOfPasswordPrompts:
485 intptr = &options->number_of_password_prompts;
486 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
Damien Miller95def091999-11-25 00:26:21 +1100488 case oCompressionLevel:
489 intptr = &options->compression_level;
490 goto parse_int;
491
Damien Millera5539d22003-04-09 20:50:06 +1000492 case oRekeyLimit:
493 intptr = &options->rekey_limit;
494 arg = strdelim(&s);
495 if (!arg || *arg == '\0')
496 fatal("%.200s line %d: Missing argument.", filename, linenum);
497 if (arg[0] < '0' || arg[0] > '9')
498 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100499 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000500 if (arg == endofnumber)
501 fatal("%.200s line %d: Bad number.", filename, linenum);
502 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100503 case '\0':
504 scale = 1;
505 break;
Damien Millera5539d22003-04-09 20:50:06 +1000506 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100507 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000508 break;
509 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100510 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000511 break;
512 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100513 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000514 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100515 default:
516 fatal("%.200s line %d: Invalid RekeyLimit suffix",
517 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000518 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100519 val64 *= scale;
520 /* detect integer wrap and too-large limits */
521 if ((val64 / scale) != orig || val64 > INT_MAX)
522 fatal("%.200s line %d: RekeyLimit too large",
523 filename, linenum);
524 if (val64 < 16)
525 fatal("%.200s line %d: RekeyLimit too small",
526 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000527 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100528 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000529 break;
530
Damien Miller95def091999-11-25 00:26:21 +1100531 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000532 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000533 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100534 fatal("%.200s line %d: Missing argument.", filename, linenum);
535 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100536 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000537 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100538 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100539 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100540 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000541 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000542 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100543 }
544 break;
545
Damien Millerd3a18572000-06-07 19:55:44 +1000546 case oXAuthLocation:
547 charptr=&options->xauth_location;
548 goto parse_string;
549
Damien Miller95def091999-11-25 00:26:21 +1100550 case oUser:
551 charptr = &options->user;
552parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000553 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000554 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100555 fatal("%.200s line %d: Missing argument.", filename, linenum);
556 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000557 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100558 break;
559
560 case oGlobalKnownHostsFile:
561 charptr = &options->system_hostfile;
562 goto parse_string;
563
564 case oUserKnownHostsFile:
565 charptr = &options->user_hostfile;
566 goto parse_string;
567
Damien Millereba71ba2000-04-29 23:57:08 +1000568 case oGlobalKnownHostsFile2:
569 charptr = &options->system_hostfile2;
570 goto parse_string;
571
572 case oUserKnownHostsFile2:
573 charptr = &options->user_hostfile2;
574 goto parse_string;
575
Damien Miller95def091999-11-25 00:26:21 +1100576 case oHostName:
577 charptr = &options->hostname;
578 goto parse_string;
579
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000580 case oHostKeyAlias:
581 charptr = &options->host_key_alias;
582 goto parse_string;
583
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000584 case oPreferredAuthentications:
585 charptr = &options->preferred_authentications;
586 goto parse_string;
587
Ben Lindstrome0f88042001-04-30 13:06:24 +0000588 case oBindAddress:
589 charptr = &options->bind_address;
590 goto parse_string;
591
Ben Lindstromae996bf2001-08-06 21:27:53 +0000592 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000593 charptr = &options->smartcard_device;
594 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000595
Damien Miller95def091999-11-25 00:26:21 +1100596 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100597 charptr = &options->proxy_command;
598parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000599 if (s == NULL)
600 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100601 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100602 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100603 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100604 return 0;
605
606 case oPort:
607 intptr = &options->port;
608parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000609 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000610 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100611 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000612 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100613 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100614
615 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000616 value = strtol(arg, &endofnumber, 0);
617 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100618 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100619 if (*activep && *intptr == -1)
620 *intptr = value;
621 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622
Damien Miller95def091999-11-25 00:26:21 +1100623 case oConnectionAttempts:
624 intptr = &options->connection_attempts;
625 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100626
Damien Miller95def091999-11-25 00:26:21 +1100627 case oCipher:
628 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000629 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000630 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000631 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000632 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100633 if (value == -1)
634 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100635 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100636 if (*activep && *intptr == -1)
637 *intptr = value;
638 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639
Damien Miller78928792000-04-12 20:17:38 +1000640 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000641 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000642 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000643 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000644 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000645 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100646 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000647 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000648 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000649 break;
650
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000651 case oMacs:
652 arg = strdelim(&s);
653 if (!arg || *arg == '\0')
654 fatal("%.200s line %d: Missing argument.", filename, linenum);
655 if (!mac_valid(arg))
656 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100657 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000658 if (*activep && options->macs == NULL)
659 options->macs = xstrdup(arg);
660 break;
661
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000662 case oHostKeyAlgorithms:
663 arg = strdelim(&s);
664 if (!arg || *arg == '\0')
665 fatal("%.200s line %d: Missing argument.", filename, linenum);
666 if (!key_names_valid2(arg))
667 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100668 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000669 if (*activep && options->hostkeyalgorithms == NULL)
670 options->hostkeyalgorithms = xstrdup(arg);
671 break;
672
Damien Miller78928792000-04-12 20:17:38 +1000673 case oProtocol:
674 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000675 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000676 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000677 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000678 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000679 if (value == SSH_PROTO_UNKNOWN)
680 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100681 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000682 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
683 *intptr = value;
684 break;
685
Damien Miller95def091999-11-25 00:26:21 +1100686 case oLogLevel:
687 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000688 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000689 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100690 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000691 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100692 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100693 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100694 *intptr = (LogLevel) value;
695 break;
696
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000697 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100698 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000699 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100700 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000701 fatal("%.200s line %d: Missing port argument.",
702 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100703 arg2 = strdelim(&s);
704 if (arg2 == NULL || *arg2 == '\0')
705 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000706 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707
708 /* construct a string for parse_forward */
709 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
710
711 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000712 fatal("%.200s line %d: Bad forwarding specification.",
713 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100714
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000715 if (*activep) {
716 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100717 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000718 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100719 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000720 }
Damien Miller95def091999-11-25 00:26:21 +1100721 break;
722
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000723 case oDynamicForward:
724 arg = strdelim(&s);
725 if (!arg || *arg == '\0')
726 fatal("%.200s line %d: Missing port argument.",
727 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100728 memset(&fwd, '\0', sizeof(fwd));
729 fwd.connect_host = "socks";
730 fwd.listen_host = hpdelim(&arg);
731 if (fwd.listen_host == NULL ||
732 strlen(fwd.listen_host) >= NI_MAXHOST)
733 fatal("%.200s line %d: Bad forwarding specification.",
734 filename, linenum);
735 if (arg) {
736 fwd.listen_port = a2port(arg);
737 fwd.listen_host = cleanhostname(fwd.listen_host);
738 } else {
739 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000740 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100741 }
742 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000743 fatal("%.200s line %d: Badly formatted port number.",
744 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000745 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100746 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000747 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000748
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000749 case oClearAllForwardings:
750 intptr = &options->clear_forwardings;
751 goto parse_flag;
752
Damien Miller95def091999-11-25 00:26:21 +1100753 case oHost:
754 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000755 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000756 if (match_pattern(host, arg)) {
757 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100758 *activep = 1;
759 break;
760 }
Damien Millerbe484b52000-07-15 14:14:16 +1000761 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100762 return 0;
763
764 case oEscapeChar:
765 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000766 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000767 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100768 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000769 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000770 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
771 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000772 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000773 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000774 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000775 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100776 else {
777 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100778 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100779 /* NOTREACHED */
780 value = 0; /* Avoid compiler warning. */
781 }
782 if (*activep && *intptr == -1)
783 *intptr = value;
784 break;
785
Damien Miller20a8f972003-05-18 20:50:30 +1000786 case oAddressFamily:
787 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000788 if (!arg || *arg == '\0')
789 fatal("%s line %d: missing address family.",
790 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000791 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000792 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000793 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000794 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000795 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000796 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000797 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000798 else
799 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000800 if (*activep && *intptr == -1)
801 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000802 break;
803
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000804 case oEnableSSHKeysign:
805 intptr = &options->enable_ssh_keysign;
806 goto parse_flag;
807
Damien Millerbd394c32004-03-08 23:12:36 +1100808 case oIdentitiesOnly:
809 intptr = &options->identities_only;
810 goto parse_flag;
811
Damien Miller509b0102003-12-17 16:33:10 +1100812 case oServerAliveInterval:
813 intptr = &options->server_alive_interval;
814 goto parse_time;
815
816 case oServerAliveCountMax:
817 intptr = &options->server_alive_count_max;
818 goto parse_int;
819
Darren Tucker46bc0752004-05-02 22:11:30 +1000820 case oSendEnv:
821 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
822 if (strchr(arg, '=') != NULL)
823 fatal("%s line %d: Invalid environment name.",
824 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100825 if (!*activep)
826 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000827 if (options->num_send_env >= MAX_SEND_ENV)
828 fatal("%s line %d: too many send env.",
829 filename, linenum);
830 options->send_env[options->num_send_env++] =
831 xstrdup(arg);
832 }
833 break;
834
Damien Miller0e220db2004-06-15 10:34:08 +1000835 case oControlPath:
836 charptr = &options->control_path;
837 goto parse_string;
838
839 case oControlMaster:
840 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000841 arg = strdelim(&s);
842 if (!arg || *arg == '\0')
843 fatal("%.200s line %d: Missing ControlMaster argument.",
844 filename, linenum);
845 value = 0; /* To avoid compiler warning... */
846 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
847 value = SSHCTL_MASTER_YES;
848 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
849 value = SSHCTL_MASTER_NO;
850 else if (strcmp(arg, "auto") == 0)
851 value = SSHCTL_MASTER_AUTO;
852 else if (strcmp(arg, "ask") == 0)
853 value = SSHCTL_MASTER_ASK;
854 else if (strcmp(arg, "autoask") == 0)
855 value = SSHCTL_MASTER_AUTO_ASK;
856 else
857 fatal("%.200s line %d: Bad ControlMaster argument.",
858 filename, linenum);
859 if (*activep && *intptr == -1)
860 *intptr = value;
861 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000862
Damien Millere1776152005-03-01 21:47:37 +1100863 case oHashKnownHosts:
864 intptr = &options->hash_known_hosts;
865 goto parse_flag;
866
Damien Millerd27b9472005-12-13 19:29:02 +1100867 case oTunnel:
868 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100869 arg = strdelim(&s);
870 if (!arg || *arg == '\0')
871 fatal("%s line %d: Missing yes/point-to-point/"
872 "ethernet/no argument.", filename, linenum);
873 value = 0; /* silence compiler */
874 if (strcasecmp(arg, "ethernet") == 0)
875 value = SSH_TUNMODE_ETHERNET;
876 else if (strcasecmp(arg, "point-to-point") == 0)
877 value = SSH_TUNMODE_POINTOPOINT;
878 else if (strcasecmp(arg, "yes") == 0)
879 value = SSH_TUNMODE_DEFAULT;
880 else if (strcasecmp(arg, "no") == 0)
881 value = SSH_TUNMODE_NO;
882 else
883 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
884 "no argument: %s", filename, linenum, arg);
885 if (*activep)
886 *intptr = value;
887 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100888
889 case oTunnelDevice:
890 arg = strdelim(&s);
891 if (!arg || *arg == '\0')
892 fatal("%.200s line %d: Missing argument.", filename, linenum);
893 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100894 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100895 fatal("%.200s line %d: Bad tun device.", filename, linenum);
896 if (*activep) {
897 options->tun_local = value;
898 options->tun_remote = value2;
899 }
900 break;
901
902 case oLocalCommand:
903 charptr = &options->local_command;
904 goto parse_command;
905
906 case oPermitLocalCommand:
907 intptr = &options->permit_local_command;
908 goto parse_flag;
909
Ben Lindstrom4daea862002-06-09 20:04:02 +0000910 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000911 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000912 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000913 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000914
Damien Millerf9b3feb2003-05-16 11:38:32 +1000915 case oUnsupported:
916 error("%s line %d: Unsupported option \"%s\"",
917 filename, linenum, keyword);
918 return 0;
919
Damien Miller95def091999-11-25 00:26:21 +1100920 default:
921 fatal("process_config_line: Unimplemented opcode %d", opcode);
922 }
923
924 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000925 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000926 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000927 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000928 }
Damien Miller95def091999-11-25 00:26:21 +1100929 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930}
931
932
Damien Miller5428f641999-11-25 11:54:57 +1100933/*
934 * Reads the config file and modifies the options accordingly. Options
935 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000936 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100937 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000939int
Darren Tuckerfc959702004-07-17 16:12:08 +1000940read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000941 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942{
Damien Miller95def091999-11-25 00:26:21 +1100943 FILE *f;
944 char line[1024];
945 int active, linenum;
946 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947
Damien Miller95def091999-11-25 00:26:21 +1100948 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000949 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000950 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller57a44762004-04-20 20:11:57 +1000952 if (checkperm) {
953 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000954
Damien Miller33793852004-06-15 10:27:55 +1000955 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000956 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000957 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000958 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000959 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000960 }
961
Damien Miller95def091999-11-25 00:26:21 +1100962 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
Damien Miller5428f641999-11-25 11:54:57 +1100964 /*
965 * Mark that we are now processing the options. This flag is turned
966 * on/off by Host specifications.
967 */
Damien Miller95def091999-11-25 00:26:21 +1100968 active = 1;
969 linenum = 0;
970 while (fgets(line, sizeof(line), f)) {
971 /* Update line number counter. */
972 linenum++;
973 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
974 bad_options++;
975 }
976 fclose(f);
977 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000978 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100979 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000980 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981}
982
Damien Miller5428f641999-11-25 11:54:57 +1100983/*
984 * Initializes options to special values that indicate that they have not yet
985 * been set. Read_config_file will only set options with this value. Options
986 * are processed in the following order: command line, user config file,
987 * system config file. Last, fill_default_options is called.
988 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989
Damien Miller4af51302000-04-16 11:18:38 +1000990void
Damien Miller95def091999-11-25 00:26:21 +1100991initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992{
Damien Miller95def091999-11-25 00:26:21 +1100993 memset(options, 'X', sizeof(*options));
994 options->forward_agent = -1;
995 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000996 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +1000997 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000998 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100999 options->gateway_ports = -1;
1000 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001001 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001002 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001003 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001004 options->gss_authentication = -1;
1005 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001006 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001007 options->kbd_interactive_authentication = -1;
1008 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001009 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001010 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001011 options->batch_mode = -1;
1012 options->check_host_ip = -1;
1013 options->strict_host_key_checking = -1;
1014 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001015 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001016 options->compression_level = -1;
1017 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001018 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001019 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001020 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001021 options->number_of_password_prompts = -1;
1022 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001023 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001024 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001025 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001026 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001027 options->num_identity_files = 0;
1028 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001029 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001030 options->proxy_command = NULL;
1031 options->user = NULL;
1032 options->escape_char = -1;
1033 options->system_hostfile = NULL;
1034 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001035 options->system_hostfile2 = NULL;
1036 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001037 options->num_local_forwards = 0;
1038 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001039 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001040 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001041 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001042 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001043 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001044 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001045 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001046 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001047 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001048 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001049 options->server_alive_interval = -1;
1050 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001051 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001052 options->control_path = NULL;
1053 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001054 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001055 options->tun_open = -1;
1056 options->tun_local = -1;
1057 options->tun_remote = -1;
1058 options->local_command = NULL;
1059 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060}
1061
Damien Miller5428f641999-11-25 11:54:57 +11001062/*
1063 * Called after processing other sources of option data, this fills those
1064 * options for which no value has been specified with their default values.
1065 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001066
Damien Miller4af51302000-04-16 11:18:38 +10001067void
Damien Miller95def091999-11-25 00:26:21 +11001068fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001070 int len;
1071
Damien Miller95def091999-11-25 00:26:21 +11001072 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001073 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001074 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001075 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001076 if (options->forward_x11_trusted == -1)
1077 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001078 if (options->exit_on_forward_failure == -1)
1079 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001080 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001081 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001082 if (options->gateway_ports == -1)
1083 options->gateway_ports = 0;
1084 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001085 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001086 if (options->rsa_authentication == -1)
1087 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001088 if (options->pubkey_authentication == -1)
1089 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001090 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001091 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001092 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001093 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001094 if (options->gss_deleg_creds == -1)
1095 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001096 if (options->password_authentication == -1)
1097 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001098 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001099 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001100 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001101 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001102 if (options->hostbased_authentication == -1)
1103 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001104 if (options->batch_mode == -1)
1105 options->batch_mode = 0;
1106 if (options->check_host_ip == -1)
1107 options->check_host_ip = 1;
1108 if (options->strict_host_key_checking == -1)
1109 options->strict_host_key_checking = 2; /* 2 is default */
1110 if (options->compression == -1)
1111 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001112 if (options->tcp_keep_alive == -1)
1113 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001114 if (options->compression_level == -1)
1115 options->compression_level = 6;
1116 if (options->port == -1)
1117 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001118 if (options->address_family == -1)
1119 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001120 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001121 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001122 if (options->number_of_password_prompts == -1)
1123 options->number_of_password_prompts = 3;
1124 /* Selected in ssh_login(). */
1125 if (options->cipher == -1)
1126 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001127 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001128 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001129 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001130 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001131 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001132 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001133 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001134 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001135 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001136 xmalloc(len);
1137 snprintf(options->identity_files[options->num_identity_files++],
1138 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001139 }
1140 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001141 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1142 options->identity_files[options->num_identity_files] =
1143 xmalloc(len);
1144 snprintf(options->identity_files[options->num_identity_files++],
1145 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1146
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001147 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001148 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001149 xmalloc(len);
1150 snprintf(options->identity_files[options->num_identity_files++],
1151 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001152 }
Damien Millereba71ba2000-04-29 23:57:08 +10001153 }
Damien Miller95def091999-11-25 00:26:21 +11001154 if (options->escape_char == -1)
1155 options->escape_char = '~';
1156 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001157 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001158 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001159 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001160 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001161 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001162 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001163 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001164 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001165 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001166 if (options->clear_forwardings == 1)
1167 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001168 if (options->no_host_authentication_for_localhost == - 1)
1169 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001170 if (options->identities_only == -1)
1171 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001172 if (options->enable_ssh_keysign == -1)
1173 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001174 if (options->rekey_limit == -1)
1175 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001176 if (options->verify_host_key_dns == -1)
1177 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001178 if (options->server_alive_interval == -1)
1179 options->server_alive_interval = 0;
1180 if (options->server_alive_count_max == -1)
1181 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001182 if (options->control_master == -1)
1183 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001184 if (options->hash_known_hosts == -1)
1185 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001186 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001187 options->tun_open = SSH_TUNMODE_NO;
1188 if (options->tun_local == -1)
1189 options->tun_local = SSH_TUNID_ANY;
1190 if (options->tun_remote == -1)
1191 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001192 if (options->permit_local_command == -1)
1193 options->permit_local_command = 0;
1194 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001195 /* options->proxy_command should not be set by default */
1196 /* options->user will be set in the main program if appropriate */
1197 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001198 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001199 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001201
1202/*
1203 * parse_forward
1204 * parses a string containing a port forwarding specification of the form:
1205 * [listenhost:]listenport:connecthost:connectport
1206 * returns number of arguments parsed or zero on error
1207 */
1208int
1209parse_forward(Forward *fwd, const char *fwdspec)
1210{
1211 int i;
1212 char *p, *cp, *fwdarg[4];
1213
1214 memset(fwd, '\0', sizeof(*fwd));
1215
1216 cp = p = xstrdup(fwdspec);
1217
1218 /* skip leading spaces */
1219 while (*cp && isspace(*cp))
1220 cp++;
1221
1222 for (i = 0; i < 4; ++i)
1223 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1224 break;
1225
1226 /* Check for trailing garbage in 4-arg case*/
1227 if (cp != NULL)
1228 i = 0; /* failure */
1229
1230 switch (i) {
1231 case 3:
1232 fwd->listen_host = NULL;
1233 fwd->listen_port = a2port(fwdarg[0]);
1234 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1235 fwd->connect_port = a2port(fwdarg[2]);
1236 break;
1237
1238 case 4:
1239 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1240 fwd->listen_port = a2port(fwdarg[1]);
1241 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1242 fwd->connect_port = a2port(fwdarg[3]);
1243 break;
1244 default:
1245 i = 0; /* failure */
1246 }
1247
1248 xfree(p);
1249
1250 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1251 goto fail_free;
1252
1253 if (fwd->connect_host != NULL &&
1254 strlen(fwd->connect_host) >= NI_MAXHOST)
1255 goto fail_free;
1256
1257 return (i);
1258
1259 fail_free:
1260 if (fwd->connect_host != NULL)
1261 xfree(fwd->connect_host);
1262 if (fwd->listen_host != NULL)
1263 xfree(fwd->listen_host);
1264 return (0);
1265}