blob: d25f93012eb4921b75079ca0bb755c17cd274897 [file] [log] [blame]
Darren Tuckere7d4b192006-07-12 22:17:10 +10001/* $OpenBSD: readconf.c,v 1.153 2006/07/11 18:50:48 markus 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>
24
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100027#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "cipher.h"
29#include "pathnames.h"
30#include "log.h"
31#include "readconf.h"
32#include "match.h"
33#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000034#include "kex.h"
35#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37/* Format of the configuration file:
38
39 # Configuration data is parsed as follows:
40 # 1. command line options
41 # 2. user-specific file
42 # 3. system-wide file
43 # Any configuration value is only changed the first time it is set.
44 # Thus, host-specific definitions should be at the beginning of the
45 # configuration file, and defaults at the end.
46
47 # Host-specific declarations. These may override anything above. A single
48 # host may match multiple declarations; these are processed in the order
49 # that they are given in.
50
51 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000052 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
54 Host fake.com
55 HostName another.host.name.real.org
56 User blaah
57 Port 34289
58 ForwardX11 no
59 ForwardAgent no
60
61 Host books.com
62 RemoteForward 9999 shadows.cs.hut.fi:9999
63 Cipher 3des
64
65 Host fascist.blob.com
66 Port 23123
67 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068 PasswordAuthentication no
69
70 Host puukko.hut.fi
71 User t35124p
72 ProxyCommand ssh-proxy %h %p
73
74 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000075 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
77 Host *.su
78 Cipher none
79 PasswordAuthentication no
80
Damien Millerd27b9472005-12-13 19:29:02 +110081 Host vpn.fake.com
82 Tunnel yes
83 TunnelDevice 3
84
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085 # Defaults for various options
86 Host *
87 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110088 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089 PasswordAuthentication yes
90 RSAAuthentication yes
91 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110093 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 IdentityFile ~/.ssh/identity
95 Port 22
96 EscapeChar ~
97
98*/
99
100/* Keyword tokens. */
101
Damien Miller95def091999-11-25 00:26:21 +1100102typedef enum {
103 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000104 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000105 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000106 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000107 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100108 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
109 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
110 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
111 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100112 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000113 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100114 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000115 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000116 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000117 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000118 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000119 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000120 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100121 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100122 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100123 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000124 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125} OpCodes;
126
127/* Textual representations of the tokens. */
128
Damien Miller95def091999-11-25 00:26:21 +1100129static struct {
130 const char *name;
131 OpCodes opcode;
132} keywords[] = {
133 { "forwardagent", oForwardAgent },
134 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000135 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000136 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000137 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100138 { "gatewayports", oGatewayPorts },
139 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000140 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100141 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100142 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
143 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100144 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100145 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000146 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000147 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000148 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000149 { "challengeresponseauthentication", oChallengeResponseAuthentication },
150 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
151 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000152 { "kerberosauthentication", oUnsupported },
153 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000154 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000155#if defined(GSSAPI)
156 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000157 { "gssapidelegatecredentials", oGssDelegateCreds },
158#else
159 { "gssapiauthentication", oUnsupported },
160 { "gssapidelegatecredentials", oUnsupported },
161#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000162 { "fallbacktorsh", oDeprecated },
163 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100164 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100165 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100166 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100167 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000168 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100169 { "proxycommand", oProxyCommand },
170 { "port", oPort },
171 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000172 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000173 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000174 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100175 { "remoteforward", oRemoteForward },
176 { "localforward", oLocalForward },
177 { "user", oUser },
178 { "host", oHost },
179 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000181 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000182 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000183 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100184 { "connectionattempts", oConnectionAttempts },
185 { "batchmode", oBatchMode },
186 { "checkhostip", oCheckHostIP },
187 { "stricthostkeychecking", oStrictHostKeyChecking },
188 { "compression", oCompression },
189 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100190 { "tcpkeepalive", oTCPKeepAlive },
191 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100192 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100193 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000194 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000195 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000196 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000197 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000198#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000199 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000200#else
201 { "smartcarddevice", oUnsupported },
202#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100203 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000204 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000205 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100206 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000207 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000208 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000209 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100210 { "serveraliveinterval", oServerAliveInterval },
211 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000212 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000213 { "controlpath", oControlPath },
214 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100215 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100216 { "tunnel", oTunnel },
217 { "tunneldevice", oTunnelDevice },
218 { "localcommand", oLocalCommand },
219 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000220 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100221};
222
Damien Miller5428f641999-11-25 11:54:57 +1100223/*
224 * Adds a local TCP/IP port forward to options. Never returns if there is an
225 * error.
226 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227
Damien Miller4af51302000-04-16 11:18:38 +1000228void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100229add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230{
Damien Miller95def091999-11-25 00:26:21 +1100231 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000232#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100233 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100234 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000235 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100236#endif
Damien Miller95def091999-11-25 00:26:21 +1100237 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
238 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
239 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100240
241 fwd->listen_host = (newfwd->listen_host == NULL) ?
242 NULL : xstrdup(newfwd->listen_host);
243 fwd->listen_port = newfwd->listen_port;
244 fwd->connect_host = xstrdup(newfwd->connect_host);
245 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246}
247
Damien Miller5428f641999-11-25 11:54:57 +1100248/*
249 * Adds a remote TCP/IP port forward to options. Never returns if there is
250 * an error.
251 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller4af51302000-04-16 11:18:38 +1000253void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100254add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255{
Damien Miller95def091999-11-25 00:26:21 +1100256 Forward *fwd;
257 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
258 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100259 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100260 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100261
262 fwd->listen_host = (newfwd->listen_host == NULL) ?
263 NULL : xstrdup(newfwd->listen_host);
264 fwd->listen_port = newfwd->listen_port;
265 fwd->connect_host = xstrdup(newfwd->connect_host);
266 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267}
268
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000269static void
270clear_forwardings(Options *options)
271{
272 int i;
273
Damien Millerf91ee4c2005-03-01 21:24:33 +1100274 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100275 if (options->local_forwards[i].listen_host != NULL)
276 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100277 xfree(options->local_forwards[i].connect_host);
278 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000279 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100281 if (options->remote_forwards[i].listen_host != NULL)
282 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 xfree(options->remote_forwards[i].connect_host);
284 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000285 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100286 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000287}
288
Damien Miller5428f641999-11-25 11:54:57 +1100289/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000290 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100291 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Miller4af51302000-04-16 11:18:38 +1000293static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100294parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000296 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller95def091999-11-25 00:26:21 +1100298 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100299 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100300 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000302 error("%s: line %d: Bad configuration option: %s",
303 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100304 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305}
306
Damien Miller5428f641999-11-25 11:54:57 +1100307/*
308 * Processes a single option line as used in the configuration files. This
309 * only sets those values that have not already been set.
310 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100311#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312
Damien Miller2ccf6611999-11-15 15:25:10 +1100313int
314process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100315 char *line, const char *filename, int linenum,
316 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100318 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100319 int opcode, *intptr, value, value2, scale;
320 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100321 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100322 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Millerc652cac2003-05-14 13:40:54 +1000324 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100325 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000326 if (strchr(WHITESPACE, line[len]) == NULL)
327 break;
328 line[len] = '\0';
329 }
330
Damien Millerbe484b52000-07-15 14:14:16 +1000331 s = line;
332 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100333 if ((keyword = strdelim(&s)) == NULL)
334 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000335 /* Ignore leading whitespace. */
336 if (*keyword == '\0')
337 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000338 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100339 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Damien Miller37023962000-07-11 17:31:38 +1000341 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342
Damien Miller95def091999-11-25 00:26:21 +1100343 switch (opcode) {
344 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100345 /* don't panic, but count bad options */
346 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100347 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000348 case oConnectTimeout:
349 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100350parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000351 arg = strdelim(&s);
352 if (!arg || *arg == '\0')
353 fatal("%s line %d: missing time value.",
354 filename, linenum);
355 if ((value = convtime(arg)) == -1)
356 fatal("%s line %d: invalid time value.",
357 filename, linenum);
358 if (*intptr == -1)
359 *intptr = value;
360 break;
361
Damien Miller95def091999-11-25 00:26:21 +1100362 case oForwardAgent:
363 intptr = &options->forward_agent;
364parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000365 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000366 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100367 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
368 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000369 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100370 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000371 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100372 value = 0;
373 else
374 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
375 if (*activep && *intptr == -1)
376 *intptr = value;
377 break;
378
379 case oForwardX11:
380 intptr = &options->forward_x11;
381 goto parse_flag;
382
Darren Tucker0a118da2003-10-15 15:54:32 +1000383 case oForwardX11Trusted:
384 intptr = &options->forward_x11_trusted;
385 goto parse_flag;
386
Damien Miller95def091999-11-25 00:26:21 +1100387 case oGatewayPorts:
388 intptr = &options->gateway_ports;
389 goto parse_flag;
390
Darren Tuckere7d4b192006-07-12 22:17:10 +1000391 case oExitOnForwardFailure:
392 intptr = &options->exit_on_forward_failure;
393 goto parse_flag;
394
Damien Miller95def091999-11-25 00:26:21 +1100395 case oUsePrivilegedPort:
396 intptr = &options->use_privileged_port;
397 goto parse_flag;
398
Damien Miller95def091999-11-25 00:26:21 +1100399 case oPasswordAuthentication:
400 intptr = &options->password_authentication;
401 goto parse_flag;
402
Damien Miller874d77b2000-10-14 16:23:11 +1100403 case oKbdInteractiveAuthentication:
404 intptr = &options->kbd_interactive_authentication;
405 goto parse_flag;
406
407 case oKbdInteractiveDevices:
408 charptr = &options->kbd_interactive_devices;
409 goto parse_string;
410
Damien Miller0bc1bd82000-11-13 22:57:25 +1100411 case oPubkeyAuthentication:
412 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000413 goto parse_flag;
414
Damien Miller95def091999-11-25 00:26:21 +1100415 case oRSAAuthentication:
416 intptr = &options->rsa_authentication;
417 goto parse_flag;
418
419 case oRhostsRSAAuthentication:
420 intptr = &options->rhosts_rsa_authentication;
421 goto parse_flag;
422
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000423 case oHostbasedAuthentication:
424 intptr = &options->hostbased_authentication;
425 goto parse_flag;
426
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000427 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000428 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100429 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000430
Darren Tucker0efd1552003-08-26 11:49:55 +1000431 case oGssAuthentication:
432 intptr = &options->gss_authentication;
433 goto parse_flag;
434
435 case oGssDelegateCreds:
436 intptr = &options->gss_deleg_creds;
437 goto parse_flag;
438
Damien Miller95def091999-11-25 00:26:21 +1100439 case oBatchMode:
440 intptr = &options->batch_mode;
441 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
Damien Miller95def091999-11-25 00:26:21 +1100443 case oCheckHostIP:
444 intptr = &options->check_host_ip;
445 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446
Damien Miller37876e92003-05-15 10:19:46 +1000447 case oVerifyHostKeyDNS:
448 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100449 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000450
Damien Miller95def091999-11-25 00:26:21 +1100451 case oStrictHostKeyChecking:
452 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100453parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000454 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000455 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000456 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100457 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100458 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000459 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100460 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000461 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100462 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000463 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100464 value = 2;
465 else
466 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
467 if (*activep && *intptr == -1)
468 *intptr = value;
469 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
Damien Miller95def091999-11-25 00:26:21 +1100471 case oCompression:
472 intptr = &options->compression;
473 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller12c150e2003-12-17 16:31:10 +1100475 case oTCPKeepAlive:
476 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100477 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000479 case oNoHostAuthenticationForLocalhost:
480 intptr = &options->no_host_authentication_for_localhost;
481 goto parse_flag;
482
Damien Miller95def091999-11-25 00:26:21 +1100483 case oNumberOfPasswordPrompts:
484 intptr = &options->number_of_password_prompts;
485 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller95def091999-11-25 00:26:21 +1100487 case oCompressionLevel:
488 intptr = &options->compression_level;
489 goto parse_int;
490
Damien Millera5539d22003-04-09 20:50:06 +1000491 case oRekeyLimit:
492 intptr = &options->rekey_limit;
493 arg = strdelim(&s);
494 if (!arg || *arg == '\0')
495 fatal("%.200s line %d: Missing argument.", filename, linenum);
496 if (arg[0] < '0' || arg[0] > '9')
497 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100498 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000499 if (arg == endofnumber)
500 fatal("%.200s line %d: Bad number.", filename, linenum);
501 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100502 case '\0':
503 scale = 1;
504 break;
Damien Millera5539d22003-04-09 20:50:06 +1000505 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100506 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000507 break;
508 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100509 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000510 break;
511 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100512 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000513 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100514 default:
515 fatal("%.200s line %d: Invalid RekeyLimit suffix",
516 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000517 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100518 val64 *= scale;
519 /* detect integer wrap and too-large limits */
520 if ((val64 / scale) != orig || val64 > INT_MAX)
521 fatal("%.200s line %d: RekeyLimit too large",
522 filename, linenum);
523 if (val64 < 16)
524 fatal("%.200s line %d: RekeyLimit too small",
525 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000526 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100527 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000528 break;
529
Damien Miller95def091999-11-25 00:26:21 +1100530 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000531 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000532 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100533 fatal("%.200s line %d: Missing argument.", filename, linenum);
534 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100535 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000536 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100537 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100538 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100539 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000540 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000541 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100542 }
543 break;
544
Damien Millerd3a18572000-06-07 19:55:44 +1000545 case oXAuthLocation:
546 charptr=&options->xauth_location;
547 goto parse_string;
548
Damien Miller95def091999-11-25 00:26:21 +1100549 case oUser:
550 charptr = &options->user;
551parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000552 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000553 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100554 fatal("%.200s line %d: Missing argument.", filename, linenum);
555 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000556 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100557 break;
558
559 case oGlobalKnownHostsFile:
560 charptr = &options->system_hostfile;
561 goto parse_string;
562
563 case oUserKnownHostsFile:
564 charptr = &options->user_hostfile;
565 goto parse_string;
566
Damien Millereba71ba2000-04-29 23:57:08 +1000567 case oGlobalKnownHostsFile2:
568 charptr = &options->system_hostfile2;
569 goto parse_string;
570
571 case oUserKnownHostsFile2:
572 charptr = &options->user_hostfile2;
573 goto parse_string;
574
Damien Miller95def091999-11-25 00:26:21 +1100575 case oHostName:
576 charptr = &options->hostname;
577 goto parse_string;
578
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000579 case oHostKeyAlias:
580 charptr = &options->host_key_alias;
581 goto parse_string;
582
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000583 case oPreferredAuthentications:
584 charptr = &options->preferred_authentications;
585 goto parse_string;
586
Ben Lindstrome0f88042001-04-30 13:06:24 +0000587 case oBindAddress:
588 charptr = &options->bind_address;
589 goto parse_string;
590
Ben Lindstromae996bf2001-08-06 21:27:53 +0000591 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000592 charptr = &options->smartcard_device;
593 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000594
Damien Miller95def091999-11-25 00:26:21 +1100595 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100596 charptr = &options->proxy_command;
597parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000598 if (s == NULL)
599 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100600 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100601 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100602 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100603 return 0;
604
605 case oPort:
606 intptr = &options->port;
607parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000608 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000609 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100610 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000611 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100612 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100613
614 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000615 value = strtol(arg, &endofnumber, 0);
616 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100617 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100618 if (*activep && *intptr == -1)
619 *intptr = value;
620 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
Damien Miller95def091999-11-25 00:26:21 +1100622 case oConnectionAttempts:
623 intptr = &options->connection_attempts;
624 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100625
Damien Miller95def091999-11-25 00:26:21 +1100626 case oCipher:
627 intptr = &options->cipher;
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 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100632 if (value == -1)
633 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100634 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100635 if (*activep && *intptr == -1)
636 *intptr = value;
637 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638
Damien Miller78928792000-04-12 20:17:38 +1000639 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000640 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000641 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000642 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000643 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000644 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100645 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000646 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000647 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000648 break;
649
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000650 case oMacs:
651 arg = strdelim(&s);
652 if (!arg || *arg == '\0')
653 fatal("%.200s line %d: Missing argument.", filename, linenum);
654 if (!mac_valid(arg))
655 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100656 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000657 if (*activep && options->macs == NULL)
658 options->macs = xstrdup(arg);
659 break;
660
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000661 case oHostKeyAlgorithms:
662 arg = strdelim(&s);
663 if (!arg || *arg == '\0')
664 fatal("%.200s line %d: Missing argument.", filename, linenum);
665 if (!key_names_valid2(arg))
666 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100667 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000668 if (*activep && options->hostkeyalgorithms == NULL)
669 options->hostkeyalgorithms = xstrdup(arg);
670 break;
671
Damien Miller78928792000-04-12 20:17:38 +1000672 case oProtocol:
673 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000674 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000675 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000676 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000677 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000678 if (value == SSH_PROTO_UNKNOWN)
679 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100680 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000681 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
682 *intptr = value;
683 break;
684
Damien Miller95def091999-11-25 00:26:21 +1100685 case oLogLevel:
686 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000687 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000688 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100689 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000690 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100691 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100692 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100693 *intptr = (LogLevel) value;
694 break;
695
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000696 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100697 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000698 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100699 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000700 fatal("%.200s line %d: Missing port argument.",
701 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100702 arg2 = strdelim(&s);
703 if (arg2 == NULL || *arg2 == '\0')
704 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000705 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100706
707 /* construct a string for parse_forward */
708 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
709
710 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000711 fatal("%.200s line %d: Bad forwarding specification.",
712 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100713
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000714 if (*activep) {
715 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100716 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000717 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100718 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000719 }
Damien Miller95def091999-11-25 00:26:21 +1100720 break;
721
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000722 case oDynamicForward:
723 arg = strdelim(&s);
724 if (!arg || *arg == '\0')
725 fatal("%.200s line %d: Missing port argument.",
726 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100727 memset(&fwd, '\0', sizeof(fwd));
728 fwd.connect_host = "socks";
729 fwd.listen_host = hpdelim(&arg);
730 if (fwd.listen_host == NULL ||
731 strlen(fwd.listen_host) >= NI_MAXHOST)
732 fatal("%.200s line %d: Bad forwarding specification.",
733 filename, linenum);
734 if (arg) {
735 fwd.listen_port = a2port(arg);
736 fwd.listen_host = cleanhostname(fwd.listen_host);
737 } else {
738 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000739 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100740 }
741 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000742 fatal("%.200s line %d: Badly formatted port number.",
743 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000744 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100745 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000746 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000747
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000748 case oClearAllForwardings:
749 intptr = &options->clear_forwardings;
750 goto parse_flag;
751
Damien Miller95def091999-11-25 00:26:21 +1100752 case oHost:
753 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000754 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000755 if (match_pattern(host, arg)) {
756 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100757 *activep = 1;
758 break;
759 }
Damien Millerbe484b52000-07-15 14:14:16 +1000760 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100761 return 0;
762
763 case oEscapeChar:
764 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000765 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000766 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100767 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000768 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000769 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
770 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000771 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000772 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000773 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000774 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100775 else {
776 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100777 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100778 /* NOTREACHED */
779 value = 0; /* Avoid compiler warning. */
780 }
781 if (*activep && *intptr == -1)
782 *intptr = value;
783 break;
784
Damien Miller20a8f972003-05-18 20:50:30 +1000785 case oAddressFamily:
786 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000787 if (!arg || *arg == '\0')
788 fatal("%s line %d: missing address family.",
789 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000790 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000791 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000792 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000793 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000794 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000795 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000796 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000797 else
798 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000799 if (*activep && *intptr == -1)
800 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000801 break;
802
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000803 case oEnableSSHKeysign:
804 intptr = &options->enable_ssh_keysign;
805 goto parse_flag;
806
Damien Millerbd394c32004-03-08 23:12:36 +1100807 case oIdentitiesOnly:
808 intptr = &options->identities_only;
809 goto parse_flag;
810
Damien Miller509b0102003-12-17 16:33:10 +1100811 case oServerAliveInterval:
812 intptr = &options->server_alive_interval;
813 goto parse_time;
814
815 case oServerAliveCountMax:
816 intptr = &options->server_alive_count_max;
817 goto parse_int;
818
Darren Tucker46bc0752004-05-02 22:11:30 +1000819 case oSendEnv:
820 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
821 if (strchr(arg, '=') != NULL)
822 fatal("%s line %d: Invalid environment name.",
823 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100824 if (!*activep)
825 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000826 if (options->num_send_env >= MAX_SEND_ENV)
827 fatal("%s line %d: too many send env.",
828 filename, linenum);
829 options->send_env[options->num_send_env++] =
830 xstrdup(arg);
831 }
832 break;
833
Damien Miller0e220db2004-06-15 10:34:08 +1000834 case oControlPath:
835 charptr = &options->control_path;
836 goto parse_string;
837
838 case oControlMaster:
839 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000840 arg = strdelim(&s);
841 if (!arg || *arg == '\0')
842 fatal("%.200s line %d: Missing ControlMaster argument.",
843 filename, linenum);
844 value = 0; /* To avoid compiler warning... */
845 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
846 value = SSHCTL_MASTER_YES;
847 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
848 value = SSHCTL_MASTER_NO;
849 else if (strcmp(arg, "auto") == 0)
850 value = SSHCTL_MASTER_AUTO;
851 else if (strcmp(arg, "ask") == 0)
852 value = SSHCTL_MASTER_ASK;
853 else if (strcmp(arg, "autoask") == 0)
854 value = SSHCTL_MASTER_AUTO_ASK;
855 else
856 fatal("%.200s line %d: Bad ControlMaster argument.",
857 filename, linenum);
858 if (*activep && *intptr == -1)
859 *intptr = value;
860 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000861
Damien Millere1776152005-03-01 21:47:37 +1100862 case oHashKnownHosts:
863 intptr = &options->hash_known_hosts;
864 goto parse_flag;
865
Damien Millerd27b9472005-12-13 19:29:02 +1100866 case oTunnel:
867 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100868 arg = strdelim(&s);
869 if (!arg || *arg == '\0')
870 fatal("%s line %d: Missing yes/point-to-point/"
871 "ethernet/no argument.", filename, linenum);
872 value = 0; /* silence compiler */
873 if (strcasecmp(arg, "ethernet") == 0)
874 value = SSH_TUNMODE_ETHERNET;
875 else if (strcasecmp(arg, "point-to-point") == 0)
876 value = SSH_TUNMODE_POINTOPOINT;
877 else if (strcasecmp(arg, "yes") == 0)
878 value = SSH_TUNMODE_DEFAULT;
879 else if (strcasecmp(arg, "no") == 0)
880 value = SSH_TUNMODE_NO;
881 else
882 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
883 "no argument: %s", filename, linenum, arg);
884 if (*activep)
885 *intptr = value;
886 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100887
888 case oTunnelDevice:
889 arg = strdelim(&s);
890 if (!arg || *arg == '\0')
891 fatal("%.200s line %d: Missing argument.", filename, linenum);
892 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100893 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100894 fatal("%.200s line %d: Bad tun device.", filename, linenum);
895 if (*activep) {
896 options->tun_local = value;
897 options->tun_remote = value2;
898 }
899 break;
900
901 case oLocalCommand:
902 charptr = &options->local_command;
903 goto parse_command;
904
905 case oPermitLocalCommand:
906 intptr = &options->permit_local_command;
907 goto parse_flag;
908
Ben Lindstrom4daea862002-06-09 20:04:02 +0000909 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000910 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000911 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000912 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000913
Damien Millerf9b3feb2003-05-16 11:38:32 +1000914 case oUnsupported:
915 error("%s line %d: Unsupported option \"%s\"",
916 filename, linenum, keyword);
917 return 0;
918
Damien Miller95def091999-11-25 00:26:21 +1100919 default:
920 fatal("process_config_line: Unimplemented opcode %d", opcode);
921 }
922
923 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000924 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000925 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000926 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000927 }
Damien Miller95def091999-11-25 00:26:21 +1100928 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929}
930
931
Damien Miller5428f641999-11-25 11:54:57 +1100932/*
933 * Reads the config file and modifies the options accordingly. Options
934 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000935 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100936 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000938int
Darren Tuckerfc959702004-07-17 16:12:08 +1000939read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000940 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941{
Damien Miller95def091999-11-25 00:26:21 +1100942 FILE *f;
943 char line[1024];
944 int active, linenum;
945 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946
Damien Miller95def091999-11-25 00:26:21 +1100947 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000948 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000949 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Damien Miller57a44762004-04-20 20:11:57 +1000951 if (checkperm) {
952 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000953
Damien Miller33793852004-06-15 10:27:55 +1000954 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000955 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000956 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000957 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000958 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000959 }
960
Damien Miller95def091999-11-25 00:26:21 +1100961 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Damien Miller5428f641999-11-25 11:54:57 +1100963 /*
964 * Mark that we are now processing the options. This flag is turned
965 * on/off by Host specifications.
966 */
Damien Miller95def091999-11-25 00:26:21 +1100967 active = 1;
968 linenum = 0;
969 while (fgets(line, sizeof(line), f)) {
970 /* Update line number counter. */
971 linenum++;
972 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
973 bad_options++;
974 }
975 fclose(f);
976 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000977 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100978 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000979 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980}
981
Damien Miller5428f641999-11-25 11:54:57 +1100982/*
983 * Initializes options to special values that indicate that they have not yet
984 * been set. Read_config_file will only set options with this value. Options
985 * are processed in the following order: command line, user config file,
986 * system config file. Last, fill_default_options is called.
987 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Damien Miller4af51302000-04-16 11:18:38 +1000989void
Damien Miller95def091999-11-25 00:26:21 +1100990initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991{
Damien Miller95def091999-11-25 00:26:21 +1100992 memset(options, 'X', sizeof(*options));
993 options->forward_agent = -1;
994 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000995 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +1000996 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000997 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100998 options->gateway_ports = -1;
999 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001000 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001001 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001002 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001003 options->gss_authentication = -1;
1004 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001005 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001006 options->kbd_interactive_authentication = -1;
1007 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001008 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001009 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001010 options->batch_mode = -1;
1011 options->check_host_ip = -1;
1012 options->strict_host_key_checking = -1;
1013 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001014 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001015 options->compression_level = -1;
1016 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001017 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001018 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001019 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001020 options->number_of_password_prompts = -1;
1021 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001022 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001023 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001024 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001025 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001026 options->num_identity_files = 0;
1027 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001028 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001029 options->proxy_command = NULL;
1030 options->user = NULL;
1031 options->escape_char = -1;
1032 options->system_hostfile = NULL;
1033 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001034 options->system_hostfile2 = NULL;
1035 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001036 options->num_local_forwards = 0;
1037 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001038 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001039 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001040 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001041 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001042 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001043 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001044 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001045 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001046 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001047 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001048 options->server_alive_interval = -1;
1049 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001050 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001051 options->control_path = NULL;
1052 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001053 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001054 options->tun_open = -1;
1055 options->tun_local = -1;
1056 options->tun_remote = -1;
1057 options->local_command = NULL;
1058 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059}
1060
Damien Miller5428f641999-11-25 11:54:57 +11001061/*
1062 * Called after processing other sources of option data, this fills those
1063 * options for which no value has been specified with their default values.
1064 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Damien Miller4af51302000-04-16 11:18:38 +10001066void
Damien Miller95def091999-11-25 00:26:21 +11001067fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001069 int len;
1070
Damien Miller95def091999-11-25 00:26:21 +11001071 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001072 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001073 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001074 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001075 if (options->forward_x11_trusted == -1)
1076 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001077 if (options->exit_on_forward_failure == -1)
1078 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001079 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001080 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001081 if (options->gateway_ports == -1)
1082 options->gateway_ports = 0;
1083 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001084 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001085 if (options->rsa_authentication == -1)
1086 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001087 if (options->pubkey_authentication == -1)
1088 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001089 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001090 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001091 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001092 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001093 if (options->gss_deleg_creds == -1)
1094 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001095 if (options->password_authentication == -1)
1096 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001097 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001098 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001099 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001100 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001101 if (options->hostbased_authentication == -1)
1102 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001103 if (options->batch_mode == -1)
1104 options->batch_mode = 0;
1105 if (options->check_host_ip == -1)
1106 options->check_host_ip = 1;
1107 if (options->strict_host_key_checking == -1)
1108 options->strict_host_key_checking = 2; /* 2 is default */
1109 if (options->compression == -1)
1110 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001111 if (options->tcp_keep_alive == -1)
1112 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001113 if (options->compression_level == -1)
1114 options->compression_level = 6;
1115 if (options->port == -1)
1116 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001117 if (options->address_family == -1)
1118 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001119 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001120 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001121 if (options->number_of_password_prompts == -1)
1122 options->number_of_password_prompts = 3;
1123 /* Selected in ssh_login(). */
1124 if (options->cipher == -1)
1125 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001126 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001127 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001128 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001129 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001130 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001131 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001132 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001133 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001134 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001135 xmalloc(len);
1136 snprintf(options->identity_files[options->num_identity_files++],
1137 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001138 }
1139 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001140 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1141 options->identity_files[options->num_identity_files] =
1142 xmalloc(len);
1143 snprintf(options->identity_files[options->num_identity_files++],
1144 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1145
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001146 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001147 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001148 xmalloc(len);
1149 snprintf(options->identity_files[options->num_identity_files++],
1150 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001151 }
Damien Millereba71ba2000-04-29 23:57:08 +10001152 }
Damien Miller95def091999-11-25 00:26:21 +11001153 if (options->escape_char == -1)
1154 options->escape_char = '~';
1155 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001156 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001157 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001158 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001159 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001160 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001161 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001162 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001163 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001164 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001165 if (options->clear_forwardings == 1)
1166 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001167 if (options->no_host_authentication_for_localhost == - 1)
1168 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001169 if (options->identities_only == -1)
1170 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001171 if (options->enable_ssh_keysign == -1)
1172 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001173 if (options->rekey_limit == -1)
1174 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001175 if (options->verify_host_key_dns == -1)
1176 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001177 if (options->server_alive_interval == -1)
1178 options->server_alive_interval = 0;
1179 if (options->server_alive_count_max == -1)
1180 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001181 if (options->control_master == -1)
1182 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001183 if (options->hash_known_hosts == -1)
1184 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001185 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001186 options->tun_open = SSH_TUNMODE_NO;
1187 if (options->tun_local == -1)
1188 options->tun_local = SSH_TUNID_ANY;
1189 if (options->tun_remote == -1)
1190 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001191 if (options->permit_local_command == -1)
1192 options->permit_local_command = 0;
1193 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001194 /* options->proxy_command should not be set by default */
1195 /* options->user will be set in the main program if appropriate */
1196 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001197 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001198 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001200
1201/*
1202 * parse_forward
1203 * parses a string containing a port forwarding specification of the form:
1204 * [listenhost:]listenport:connecthost:connectport
1205 * returns number of arguments parsed or zero on error
1206 */
1207int
1208parse_forward(Forward *fwd, const char *fwdspec)
1209{
1210 int i;
1211 char *p, *cp, *fwdarg[4];
1212
1213 memset(fwd, '\0', sizeof(*fwd));
1214
1215 cp = p = xstrdup(fwdspec);
1216
1217 /* skip leading spaces */
1218 while (*cp && isspace(*cp))
1219 cp++;
1220
1221 for (i = 0; i < 4; ++i)
1222 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1223 break;
1224
1225 /* Check for trailing garbage in 4-arg case*/
1226 if (cp != NULL)
1227 i = 0; /* failure */
1228
1229 switch (i) {
1230 case 3:
1231 fwd->listen_host = NULL;
1232 fwd->listen_port = a2port(fwdarg[0]);
1233 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1234 fwd->connect_port = a2port(fwdarg[2]);
1235 break;
1236
1237 case 4:
1238 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1239 fwd->listen_port = a2port(fwdarg[1]);
1240 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1241 fwd->connect_port = a2port(fwdarg[3]);
1242 break;
1243 default:
1244 i = 0; /* failure */
1245 }
1246
1247 xfree(p);
1248
1249 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1250 goto fail_free;
1251
1252 if (fwd->connect_host != NULL &&
1253 strlen(fwd->connect_host) >= NI_MAXHOST)
1254 goto fail_free;
1255
1256 return (i);
1257
1258 fail_free:
1259 if (fwd->connect_host != NULL)
1260 xfree(fwd->connect_host);
1261 if (fwd->listen_host != NULL)
1262 xfree(fwd->listen_host);
1263 return (0);
1264}