blob: df5e566a51b32c42507a1cc8423a121c465364ec [file] [log] [blame]
Damien Miller8ec8c3e2006-07-10 20:35:38 +10001/* $OpenBSD: readconf.c,v 1.152 2006/07/05 02:42:09 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>
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,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000105 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000106 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100107 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
108 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
109 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
110 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100111 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000112 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100113 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000114 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000115 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000116 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000117 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000118 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000119 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100120 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100121 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100122 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000123 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124} OpCodes;
125
126/* Textual representations of the tokens. */
127
Damien Miller95def091999-11-25 00:26:21 +1100128static struct {
129 const char *name;
130 OpCodes opcode;
131} keywords[] = {
132 { "forwardagent", oForwardAgent },
133 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000134 { "forwardx11trusted", oForwardX11Trusted },
Damien Millerd3a18572000-06-07 19:55:44 +1000135 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100136 { "gatewayports", oGatewayPorts },
137 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000138 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100139 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100140 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
141 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100142 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100143 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000144 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000145 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000146 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000147 { "challengeresponseauthentication", oChallengeResponseAuthentication },
148 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
149 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000150 { "kerberosauthentication", oUnsupported },
151 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000152 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000153#if defined(GSSAPI)
154 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000155 { "gssapidelegatecredentials", oGssDelegateCreds },
156#else
157 { "gssapiauthentication", oUnsupported },
158 { "gssapidelegatecredentials", oUnsupported },
159#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000160 { "fallbacktorsh", oDeprecated },
161 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100162 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100163 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100164 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100165 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000166 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100167 { "proxycommand", oProxyCommand },
168 { "port", oPort },
169 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000170 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000171 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000172 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "remoteforward", oRemoteForward },
174 { "localforward", oLocalForward },
175 { "user", oUser },
176 { "host", oHost },
177 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100178 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000179 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000180 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000181 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100182 { "connectionattempts", oConnectionAttempts },
183 { "batchmode", oBatchMode },
184 { "checkhostip", oCheckHostIP },
185 { "stricthostkeychecking", oStrictHostKeyChecking },
186 { "compression", oCompression },
187 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100188 { "tcpkeepalive", oTCPKeepAlive },
189 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100190 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100191 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000192 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000193 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000194 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000195 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000196#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000197 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000198#else
199 { "smartcarddevice", oUnsupported },
200#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100201 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000202 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000203 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100204 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000205 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000206 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000207 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100208 { "serveraliveinterval", oServerAliveInterval },
209 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000210 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000211 { "controlpath", oControlPath },
212 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100213 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100214 { "tunnel", oTunnel },
215 { "tunneldevice", oTunnelDevice },
216 { "localcommand", oLocalCommand },
217 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000218 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100219};
220
Damien Miller5428f641999-11-25 11:54:57 +1100221/*
222 * Adds a local TCP/IP port forward to options. Never returns if there is an
223 * error.
224 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
Damien Miller4af51302000-04-16 11:18:38 +1000226void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100227add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228{
Damien Miller95def091999-11-25 00:26:21 +1100229 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000230#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100231 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100232 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000233 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100234#endif
Damien Miller95def091999-11-25 00:26:21 +1100235 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
236 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
237 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100238
239 fwd->listen_host = (newfwd->listen_host == NULL) ?
240 NULL : xstrdup(newfwd->listen_host);
241 fwd->listen_port = newfwd->listen_port;
242 fwd->connect_host = xstrdup(newfwd->connect_host);
243 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244}
245
Damien Miller5428f641999-11-25 11:54:57 +1100246/*
247 * Adds a remote TCP/IP port forward to options. Never returns if there is
248 * an error.
249 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Miller4af51302000-04-16 11:18:38 +1000251void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100252add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253{
Damien Miller95def091999-11-25 00:26:21 +1100254 Forward *fwd;
255 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
256 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100257 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100258 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100259
260 fwd->listen_host = (newfwd->listen_host == NULL) ?
261 NULL : xstrdup(newfwd->listen_host);
262 fwd->listen_port = newfwd->listen_port;
263 fwd->connect_host = xstrdup(newfwd->connect_host);
264 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265}
266
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000267static void
268clear_forwardings(Options *options)
269{
270 int i;
271
Damien Millerf91ee4c2005-03-01 21:24:33 +1100272 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100273 if (options->local_forwards[i].listen_host != NULL)
274 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100275 xfree(options->local_forwards[i].connect_host);
276 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000277 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100279 if (options->remote_forwards[i].listen_host != NULL)
280 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100281 xfree(options->remote_forwards[i].connect_host);
282 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000283 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100284 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000285}
286
Damien Miller5428f641999-11-25 11:54:57 +1100287/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000288 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100289 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290
Damien Miller4af51302000-04-16 11:18:38 +1000291static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100292parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000294 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295
Damien Miller95def091999-11-25 00:26:21 +1100296 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100297 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100298 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000300 error("%s: line %d: Bad configuration option: %s",
301 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100302 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Damien Miller5428f641999-11-25 11:54:57 +1100305/*
306 * Processes a single option line as used in the configuration files. This
307 * only sets those values that have not already been set.
308 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100309#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Damien Miller2ccf6611999-11-15 15:25:10 +1100311int
312process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100313 char *line, const char *filename, int linenum,
314 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100316 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100317 int opcode, *intptr, value, value2, scale;
318 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100319 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100320 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
Damien Millerc652cac2003-05-14 13:40:54 +1000322 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100323 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000324 if (strchr(WHITESPACE, line[len]) == NULL)
325 break;
326 line[len] = '\0';
327 }
328
Damien Millerbe484b52000-07-15 14:14:16 +1000329 s = line;
330 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100331 if ((keyword = strdelim(&s)) == NULL)
332 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000333 /* Ignore leading whitespace. */
334 if (*keyword == '\0')
335 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000336 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100337 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
Damien Miller37023962000-07-11 17:31:38 +1000339 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Damien Miller95def091999-11-25 00:26:21 +1100341 switch (opcode) {
342 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100343 /* don't panic, but count bad options */
344 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100345 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000346 case oConnectTimeout:
347 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100348parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000349 arg = strdelim(&s);
350 if (!arg || *arg == '\0')
351 fatal("%s line %d: missing time value.",
352 filename, linenum);
353 if ((value = convtime(arg)) == -1)
354 fatal("%s line %d: invalid time value.",
355 filename, linenum);
356 if (*intptr == -1)
357 *intptr = value;
358 break;
359
Damien Miller95def091999-11-25 00:26:21 +1100360 case oForwardAgent:
361 intptr = &options->forward_agent;
362parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000363 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000364 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100365 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
366 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000367 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100368 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000369 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100370 value = 0;
371 else
372 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
373 if (*activep && *intptr == -1)
374 *intptr = value;
375 break;
376
377 case oForwardX11:
378 intptr = &options->forward_x11;
379 goto parse_flag;
380
Darren Tucker0a118da2003-10-15 15:54:32 +1000381 case oForwardX11Trusted:
382 intptr = &options->forward_x11_trusted;
383 goto parse_flag;
384
Damien Miller95def091999-11-25 00:26:21 +1100385 case oGatewayPorts:
386 intptr = &options->gateway_ports;
387 goto parse_flag;
388
389 case oUsePrivilegedPort:
390 intptr = &options->use_privileged_port;
391 goto parse_flag;
392
Damien Miller95def091999-11-25 00:26:21 +1100393 case oPasswordAuthentication:
394 intptr = &options->password_authentication;
395 goto parse_flag;
396
Damien Miller874d77b2000-10-14 16:23:11 +1100397 case oKbdInteractiveAuthentication:
398 intptr = &options->kbd_interactive_authentication;
399 goto parse_flag;
400
401 case oKbdInteractiveDevices:
402 charptr = &options->kbd_interactive_devices;
403 goto parse_string;
404
Damien Miller0bc1bd82000-11-13 22:57:25 +1100405 case oPubkeyAuthentication:
406 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000407 goto parse_flag;
408
Damien Miller95def091999-11-25 00:26:21 +1100409 case oRSAAuthentication:
410 intptr = &options->rsa_authentication;
411 goto parse_flag;
412
413 case oRhostsRSAAuthentication:
414 intptr = &options->rhosts_rsa_authentication;
415 goto parse_flag;
416
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000417 case oHostbasedAuthentication:
418 intptr = &options->hostbased_authentication;
419 goto parse_flag;
420
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000421 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000422 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100423 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000424
Darren Tucker0efd1552003-08-26 11:49:55 +1000425 case oGssAuthentication:
426 intptr = &options->gss_authentication;
427 goto parse_flag;
428
429 case oGssDelegateCreds:
430 intptr = &options->gss_deleg_creds;
431 goto parse_flag;
432
Damien Miller95def091999-11-25 00:26:21 +1100433 case oBatchMode:
434 intptr = &options->batch_mode;
435 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Damien Miller95def091999-11-25 00:26:21 +1100437 case oCheckHostIP:
438 intptr = &options->check_host_ip;
439 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Damien Miller37876e92003-05-15 10:19:46 +1000441 case oVerifyHostKeyDNS:
442 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100443 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000444
Damien Miller95def091999-11-25 00:26:21 +1100445 case oStrictHostKeyChecking:
446 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100447parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000448 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000449 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000450 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100451 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100452 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000453 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100454 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000455 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100456 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000457 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100458 value = 2;
459 else
460 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
461 if (*activep && *intptr == -1)
462 *intptr = value;
463 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller95def091999-11-25 00:26:21 +1100465 case oCompression:
466 intptr = &options->compression;
467 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller12c150e2003-12-17 16:31:10 +1100469 case oTCPKeepAlive:
470 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100471 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000473 case oNoHostAuthenticationForLocalhost:
474 intptr = &options->no_host_authentication_for_localhost;
475 goto parse_flag;
476
Damien Miller95def091999-11-25 00:26:21 +1100477 case oNumberOfPasswordPrompts:
478 intptr = &options->number_of_password_prompts;
479 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
Damien Miller95def091999-11-25 00:26:21 +1100481 case oCompressionLevel:
482 intptr = &options->compression_level;
483 goto parse_int;
484
Damien Millera5539d22003-04-09 20:50:06 +1000485 case oRekeyLimit:
486 intptr = &options->rekey_limit;
487 arg = strdelim(&s);
488 if (!arg || *arg == '\0')
489 fatal("%.200s line %d: Missing argument.", filename, linenum);
490 if (arg[0] < '0' || arg[0] > '9')
491 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100492 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000493 if (arg == endofnumber)
494 fatal("%.200s line %d: Bad number.", filename, linenum);
495 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100496 case '\0':
497 scale = 1;
498 break;
Damien Millera5539d22003-04-09 20:50:06 +1000499 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100500 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000501 break;
502 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100503 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000504 break;
505 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100506 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000507 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100508 default:
509 fatal("%.200s line %d: Invalid RekeyLimit suffix",
510 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000511 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100512 val64 *= scale;
513 /* detect integer wrap and too-large limits */
514 if ((val64 / scale) != orig || val64 > INT_MAX)
515 fatal("%.200s line %d: RekeyLimit too large",
516 filename, linenum);
517 if (val64 < 16)
518 fatal("%.200s line %d: RekeyLimit too small",
519 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000520 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100521 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000522 break;
523
Damien Miller95def091999-11-25 00:26:21 +1100524 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000525 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000526 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100527 fatal("%.200s line %d: Missing argument.", filename, linenum);
528 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100529 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000530 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100531 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100532 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100533 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000534 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000535 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100536 }
537 break;
538
Damien Millerd3a18572000-06-07 19:55:44 +1000539 case oXAuthLocation:
540 charptr=&options->xauth_location;
541 goto parse_string;
542
Damien Miller95def091999-11-25 00:26:21 +1100543 case oUser:
544 charptr = &options->user;
545parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000546 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000547 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100548 fatal("%.200s line %d: Missing argument.", filename, linenum);
549 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000550 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100551 break;
552
553 case oGlobalKnownHostsFile:
554 charptr = &options->system_hostfile;
555 goto parse_string;
556
557 case oUserKnownHostsFile:
558 charptr = &options->user_hostfile;
559 goto parse_string;
560
Damien Millereba71ba2000-04-29 23:57:08 +1000561 case oGlobalKnownHostsFile2:
562 charptr = &options->system_hostfile2;
563 goto parse_string;
564
565 case oUserKnownHostsFile2:
566 charptr = &options->user_hostfile2;
567 goto parse_string;
568
Damien Miller95def091999-11-25 00:26:21 +1100569 case oHostName:
570 charptr = &options->hostname;
571 goto parse_string;
572
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000573 case oHostKeyAlias:
574 charptr = &options->host_key_alias;
575 goto parse_string;
576
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000577 case oPreferredAuthentications:
578 charptr = &options->preferred_authentications;
579 goto parse_string;
580
Ben Lindstrome0f88042001-04-30 13:06:24 +0000581 case oBindAddress:
582 charptr = &options->bind_address;
583 goto parse_string;
584
Ben Lindstromae996bf2001-08-06 21:27:53 +0000585 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000586 charptr = &options->smartcard_device;
587 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000588
Damien Miller95def091999-11-25 00:26:21 +1100589 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100590 charptr = &options->proxy_command;
591parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000592 if (s == NULL)
593 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100594 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100595 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100596 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100597 return 0;
598
599 case oPort:
600 intptr = &options->port;
601parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000602 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000603 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100604 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000605 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100606 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100607
608 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000609 value = strtol(arg, &endofnumber, 0);
610 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100611 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100612 if (*activep && *intptr == -1)
613 *intptr = value;
614 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615
Damien Miller95def091999-11-25 00:26:21 +1100616 case oConnectionAttempts:
617 intptr = &options->connection_attempts;
618 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100619
Damien Miller95def091999-11-25 00:26:21 +1100620 case oCipher:
621 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000622 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000623 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000624 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000625 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100626 if (value == -1)
627 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100628 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100629 if (*activep && *intptr == -1)
630 *intptr = value;
631 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632
Damien Miller78928792000-04-12 20:17:38 +1000633 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000634 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000635 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000636 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000637 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000638 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100639 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000640 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000641 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000642 break;
643
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000644 case oMacs:
645 arg = strdelim(&s);
646 if (!arg || *arg == '\0')
647 fatal("%.200s line %d: Missing argument.", filename, linenum);
648 if (!mac_valid(arg))
649 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100650 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000651 if (*activep && options->macs == NULL)
652 options->macs = xstrdup(arg);
653 break;
654
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000655 case oHostKeyAlgorithms:
656 arg = strdelim(&s);
657 if (!arg || *arg == '\0')
658 fatal("%.200s line %d: Missing argument.", filename, linenum);
659 if (!key_names_valid2(arg))
660 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100661 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000662 if (*activep && options->hostkeyalgorithms == NULL)
663 options->hostkeyalgorithms = xstrdup(arg);
664 break;
665
Damien Miller78928792000-04-12 20:17:38 +1000666 case oProtocol:
667 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000668 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000669 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000670 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000671 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000672 if (value == SSH_PROTO_UNKNOWN)
673 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100674 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000675 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
676 *intptr = value;
677 break;
678
Damien Miller95def091999-11-25 00:26:21 +1100679 case oLogLevel:
680 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000681 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000682 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100683 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000684 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100685 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100686 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100687 *intptr = (LogLevel) value;
688 break;
689
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000690 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100691 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000692 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100693 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000694 fatal("%.200s line %d: Missing port argument.",
695 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100696 arg2 = strdelim(&s);
697 if (arg2 == NULL || *arg2 == '\0')
698 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000699 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100700
701 /* construct a string for parse_forward */
702 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
703
704 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000705 fatal("%.200s line %d: Bad forwarding specification.",
706 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000708 if (*activep) {
709 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100710 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000711 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100712 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000713 }
Damien Miller95def091999-11-25 00:26:21 +1100714 break;
715
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000716 case oDynamicForward:
717 arg = strdelim(&s);
718 if (!arg || *arg == '\0')
719 fatal("%.200s line %d: Missing port argument.",
720 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100721 memset(&fwd, '\0', sizeof(fwd));
722 fwd.connect_host = "socks";
723 fwd.listen_host = hpdelim(&arg);
724 if (fwd.listen_host == NULL ||
725 strlen(fwd.listen_host) >= NI_MAXHOST)
726 fatal("%.200s line %d: Bad forwarding specification.",
727 filename, linenum);
728 if (arg) {
729 fwd.listen_port = a2port(arg);
730 fwd.listen_host = cleanhostname(fwd.listen_host);
731 } else {
732 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000733 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100734 }
735 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000736 fatal("%.200s line %d: Badly formatted port number.",
737 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000738 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100739 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000740 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000741
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000742 case oClearAllForwardings:
743 intptr = &options->clear_forwardings;
744 goto parse_flag;
745
Damien Miller95def091999-11-25 00:26:21 +1100746 case oHost:
747 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000748 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000749 if (match_pattern(host, arg)) {
750 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100751 *activep = 1;
752 break;
753 }
Damien Millerbe484b52000-07-15 14:14:16 +1000754 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100755 return 0;
756
757 case oEscapeChar:
758 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000759 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000760 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100761 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000762 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000763 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
764 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000765 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000766 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000767 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000768 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100769 else {
770 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100771 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100772 /* NOTREACHED */
773 value = 0; /* Avoid compiler warning. */
774 }
775 if (*activep && *intptr == -1)
776 *intptr = value;
777 break;
778
Damien Miller20a8f972003-05-18 20:50:30 +1000779 case oAddressFamily:
780 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000781 if (!arg || *arg == '\0')
782 fatal("%s line %d: missing address family.",
783 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000784 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000785 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000786 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000787 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000788 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000789 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000790 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000791 else
792 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000793 if (*activep && *intptr == -1)
794 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000795 break;
796
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000797 case oEnableSSHKeysign:
798 intptr = &options->enable_ssh_keysign;
799 goto parse_flag;
800
Damien Millerbd394c32004-03-08 23:12:36 +1100801 case oIdentitiesOnly:
802 intptr = &options->identities_only;
803 goto parse_flag;
804
Damien Miller509b0102003-12-17 16:33:10 +1100805 case oServerAliveInterval:
806 intptr = &options->server_alive_interval;
807 goto parse_time;
808
809 case oServerAliveCountMax:
810 intptr = &options->server_alive_count_max;
811 goto parse_int;
812
Darren Tucker46bc0752004-05-02 22:11:30 +1000813 case oSendEnv:
814 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
815 if (strchr(arg, '=') != NULL)
816 fatal("%s line %d: Invalid environment name.",
817 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100818 if (!*activep)
819 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000820 if (options->num_send_env >= MAX_SEND_ENV)
821 fatal("%s line %d: too many send env.",
822 filename, linenum);
823 options->send_env[options->num_send_env++] =
824 xstrdup(arg);
825 }
826 break;
827
Damien Miller0e220db2004-06-15 10:34:08 +1000828 case oControlPath:
829 charptr = &options->control_path;
830 goto parse_string;
831
832 case oControlMaster:
833 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000834 arg = strdelim(&s);
835 if (!arg || *arg == '\0')
836 fatal("%.200s line %d: Missing ControlMaster argument.",
837 filename, linenum);
838 value = 0; /* To avoid compiler warning... */
839 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
840 value = SSHCTL_MASTER_YES;
841 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
842 value = SSHCTL_MASTER_NO;
843 else if (strcmp(arg, "auto") == 0)
844 value = SSHCTL_MASTER_AUTO;
845 else if (strcmp(arg, "ask") == 0)
846 value = SSHCTL_MASTER_ASK;
847 else if (strcmp(arg, "autoask") == 0)
848 value = SSHCTL_MASTER_AUTO_ASK;
849 else
850 fatal("%.200s line %d: Bad ControlMaster argument.",
851 filename, linenum);
852 if (*activep && *intptr == -1)
853 *intptr = value;
854 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000855
Damien Millere1776152005-03-01 21:47:37 +1100856 case oHashKnownHosts:
857 intptr = &options->hash_known_hosts;
858 goto parse_flag;
859
Damien Millerd27b9472005-12-13 19:29:02 +1100860 case oTunnel:
861 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100862 arg = strdelim(&s);
863 if (!arg || *arg == '\0')
864 fatal("%s line %d: Missing yes/point-to-point/"
865 "ethernet/no argument.", filename, linenum);
866 value = 0; /* silence compiler */
867 if (strcasecmp(arg, "ethernet") == 0)
868 value = SSH_TUNMODE_ETHERNET;
869 else if (strcasecmp(arg, "point-to-point") == 0)
870 value = SSH_TUNMODE_POINTOPOINT;
871 else if (strcasecmp(arg, "yes") == 0)
872 value = SSH_TUNMODE_DEFAULT;
873 else if (strcasecmp(arg, "no") == 0)
874 value = SSH_TUNMODE_NO;
875 else
876 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
877 "no argument: %s", filename, linenum, arg);
878 if (*activep)
879 *intptr = value;
880 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100881
882 case oTunnelDevice:
883 arg = strdelim(&s);
884 if (!arg || *arg == '\0')
885 fatal("%.200s line %d: Missing argument.", filename, linenum);
886 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100887 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100888 fatal("%.200s line %d: Bad tun device.", filename, linenum);
889 if (*activep) {
890 options->tun_local = value;
891 options->tun_remote = value2;
892 }
893 break;
894
895 case oLocalCommand:
896 charptr = &options->local_command;
897 goto parse_command;
898
899 case oPermitLocalCommand:
900 intptr = &options->permit_local_command;
901 goto parse_flag;
902
Ben Lindstrom4daea862002-06-09 20:04:02 +0000903 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000904 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000905 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000906 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000907
Damien Millerf9b3feb2003-05-16 11:38:32 +1000908 case oUnsupported:
909 error("%s line %d: Unsupported option \"%s\"",
910 filename, linenum, keyword);
911 return 0;
912
Damien Miller95def091999-11-25 00:26:21 +1100913 default:
914 fatal("process_config_line: Unimplemented opcode %d", opcode);
915 }
916
917 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000918 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000919 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000920 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000921 }
Damien Miller95def091999-11-25 00:26:21 +1100922 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000923}
924
925
Damien Miller5428f641999-11-25 11:54:57 +1100926/*
927 * Reads the config file and modifies the options accordingly. Options
928 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000929 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100930 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000932int
Darren Tuckerfc959702004-07-17 16:12:08 +1000933read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000934 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935{
Damien Miller95def091999-11-25 00:26:21 +1100936 FILE *f;
937 char line[1024];
938 int active, linenum;
939 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940
Damien Miller95def091999-11-25 00:26:21 +1100941 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000942 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000943 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944
Damien Miller57a44762004-04-20 20:11:57 +1000945 if (checkperm) {
946 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000947
Damien Miller33793852004-06-15 10:27:55 +1000948 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000949 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000950 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000951 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000952 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000953 }
954
Damien Miller95def091999-11-25 00:26:21 +1100955 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Miller5428f641999-11-25 11:54:57 +1100957 /*
958 * Mark that we are now processing the options. This flag is turned
959 * on/off by Host specifications.
960 */
Damien Miller95def091999-11-25 00:26:21 +1100961 active = 1;
962 linenum = 0;
963 while (fgets(line, sizeof(line), f)) {
964 /* Update line number counter. */
965 linenum++;
966 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
967 bad_options++;
968 }
969 fclose(f);
970 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000971 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100972 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000973 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974}
975
Damien Miller5428f641999-11-25 11:54:57 +1100976/*
977 * Initializes options to special values that indicate that they have not yet
978 * been set. Read_config_file will only set options with this value. Options
979 * are processed in the following order: command line, user config file,
980 * system config file. Last, fill_default_options is called.
981 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller4af51302000-04-16 11:18:38 +1000983void
Damien Miller95def091999-11-25 00:26:21 +1100984initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985{
Damien Miller95def091999-11-25 00:26:21 +1100986 memset(options, 'X', sizeof(*options));
987 options->forward_agent = -1;
988 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000989 options->forward_x11_trusted = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000990 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100991 options->gateway_ports = -1;
992 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100993 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100994 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000995 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000996 options->gss_authentication = -1;
997 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100998 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100999 options->kbd_interactive_authentication = -1;
1000 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001001 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001002 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001003 options->batch_mode = -1;
1004 options->check_host_ip = -1;
1005 options->strict_host_key_checking = -1;
1006 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001007 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001008 options->compression_level = -1;
1009 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001010 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001011 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001012 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001013 options->number_of_password_prompts = -1;
1014 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001015 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001016 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001017 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001018 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001019 options->num_identity_files = 0;
1020 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001021 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001022 options->proxy_command = NULL;
1023 options->user = NULL;
1024 options->escape_char = -1;
1025 options->system_hostfile = NULL;
1026 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001027 options->system_hostfile2 = NULL;
1028 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001029 options->num_local_forwards = 0;
1030 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001031 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001032 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001033 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001034 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001035 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001036 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001037 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001038 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001039 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001040 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001041 options->server_alive_interval = -1;
1042 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001043 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001044 options->control_path = NULL;
1045 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001046 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001047 options->tun_open = -1;
1048 options->tun_local = -1;
1049 options->tun_remote = -1;
1050 options->local_command = NULL;
1051 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052}
1053
Damien Miller5428f641999-11-25 11:54:57 +11001054/*
1055 * Called after processing other sources of option data, this fills those
1056 * options for which no value has been specified with their default values.
1057 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
Damien Miller4af51302000-04-16 11:18:38 +10001059void
Damien Miller95def091999-11-25 00:26:21 +11001060fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001062 int len;
1063
Damien Miller95def091999-11-25 00:26:21 +11001064 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001065 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001066 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001067 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001068 if (options->forward_x11_trusted == -1)
1069 options->forward_x11_trusted = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001070 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001071 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001072 if (options->gateway_ports == -1)
1073 options->gateway_ports = 0;
1074 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001075 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001076 if (options->rsa_authentication == -1)
1077 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001078 if (options->pubkey_authentication == -1)
1079 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001080 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001081 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001082 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001083 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001084 if (options->gss_deleg_creds == -1)
1085 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001086 if (options->password_authentication == -1)
1087 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001088 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001089 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001090 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001091 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001092 if (options->hostbased_authentication == -1)
1093 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001094 if (options->batch_mode == -1)
1095 options->batch_mode = 0;
1096 if (options->check_host_ip == -1)
1097 options->check_host_ip = 1;
1098 if (options->strict_host_key_checking == -1)
1099 options->strict_host_key_checking = 2; /* 2 is default */
1100 if (options->compression == -1)
1101 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001102 if (options->tcp_keep_alive == -1)
1103 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001104 if (options->compression_level == -1)
1105 options->compression_level = 6;
1106 if (options->port == -1)
1107 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001108 if (options->address_family == -1)
1109 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001110 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001111 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001112 if (options->number_of_password_prompts == -1)
1113 options->number_of_password_prompts = 3;
1114 /* Selected in ssh_login(). */
1115 if (options->cipher == -1)
1116 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001117 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001118 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001119 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001120 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001121 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001122 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001123 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001124 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001125 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001126 xmalloc(len);
1127 snprintf(options->identity_files[options->num_identity_files++],
1128 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001129 }
1130 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001131 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1132 options->identity_files[options->num_identity_files] =
1133 xmalloc(len);
1134 snprintf(options->identity_files[options->num_identity_files++],
1135 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1136
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001137 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001138 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001139 xmalloc(len);
1140 snprintf(options->identity_files[options->num_identity_files++],
1141 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001142 }
Damien Millereba71ba2000-04-29 23:57:08 +10001143 }
Damien Miller95def091999-11-25 00:26:21 +11001144 if (options->escape_char == -1)
1145 options->escape_char = '~';
1146 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001147 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001148 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001149 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001150 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001151 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001152 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001153 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001154 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001155 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001156 if (options->clear_forwardings == 1)
1157 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001158 if (options->no_host_authentication_for_localhost == - 1)
1159 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001160 if (options->identities_only == -1)
1161 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001162 if (options->enable_ssh_keysign == -1)
1163 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001164 if (options->rekey_limit == -1)
1165 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001166 if (options->verify_host_key_dns == -1)
1167 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001168 if (options->server_alive_interval == -1)
1169 options->server_alive_interval = 0;
1170 if (options->server_alive_count_max == -1)
1171 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001172 if (options->control_master == -1)
1173 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001174 if (options->hash_known_hosts == -1)
1175 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001176 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001177 options->tun_open = SSH_TUNMODE_NO;
1178 if (options->tun_local == -1)
1179 options->tun_local = SSH_TUNID_ANY;
1180 if (options->tun_remote == -1)
1181 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001182 if (options->permit_local_command == -1)
1183 options->permit_local_command = 0;
1184 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001185 /* options->proxy_command should not be set by default */
1186 /* options->user will be set in the main program if appropriate */
1187 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001188 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001189 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001191
1192/*
1193 * parse_forward
1194 * parses a string containing a port forwarding specification of the form:
1195 * [listenhost:]listenport:connecthost:connectport
1196 * returns number of arguments parsed or zero on error
1197 */
1198int
1199parse_forward(Forward *fwd, const char *fwdspec)
1200{
1201 int i;
1202 char *p, *cp, *fwdarg[4];
1203
1204 memset(fwd, '\0', sizeof(*fwd));
1205
1206 cp = p = xstrdup(fwdspec);
1207
1208 /* skip leading spaces */
1209 while (*cp && isspace(*cp))
1210 cp++;
1211
1212 for (i = 0; i < 4; ++i)
1213 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1214 break;
1215
1216 /* Check for trailing garbage in 4-arg case*/
1217 if (cp != NULL)
1218 i = 0; /* failure */
1219
1220 switch (i) {
1221 case 3:
1222 fwd->listen_host = NULL;
1223 fwd->listen_port = a2port(fwdarg[0]);
1224 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1225 fwd->connect_port = a2port(fwdarg[2]);
1226 break;
1227
1228 case 4:
1229 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1230 fwd->listen_port = a2port(fwdarg[1]);
1231 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1232 fwd->connect_port = a2port(fwdarg[3]);
1233 break;
1234 default:
1235 i = 0; /* failure */
1236 }
1237
1238 xfree(p);
1239
1240 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1241 goto fail_free;
1242
1243 if (fwd->connect_host != NULL &&
1244 strlen(fwd->connect_host) >= NI_MAXHOST)
1245 goto fail_free;
1246
1247 return (i);
1248
1249 fail_free:
1250 if (fwd->connect_host != NULL)
1251 xfree(fwd->connect_host);
1252 if (fwd->listen_host != NULL)
1253 xfree(fwd->listen_host);
1254 return (0);
1255}