blob: bc5cf61883c75ec39ebebcbb1bbda032621d5ed2 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerb59d4fe2006-03-15 11:30:38 +110015RCSID("$OpenBSD: readconf.c,v 1.146 2006/02/12 10:44:18 djm Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100019#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "cipher.h"
21#include "pathnames.h"
22#include "log.h"
23#include "readconf.h"
24#include "match.h"
25#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000026#include "kex.h"
27#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
29/* Format of the configuration file:
30
31 # Configuration data is parsed as follows:
32 # 1. command line options
33 # 2. user-specific file
34 # 3. system-wide file
35 # Any configuration value is only changed the first time it is set.
36 # Thus, host-specific definitions should be at the beginning of the
37 # configuration file, and defaults at the end.
38
39 # Host-specific declarations. These may override anything above. A single
40 # host may match multiple declarations; these are processed in the order
41 # that they are given in.
42
43 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000044 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46 Host fake.com
47 HostName another.host.name.real.org
48 User blaah
49 Port 34289
50 ForwardX11 no
51 ForwardAgent no
52
53 Host books.com
54 RemoteForward 9999 shadows.cs.hut.fi:9999
55 Cipher 3des
56
57 Host fascist.blob.com
58 Port 23123
59 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060 PasswordAuthentication no
61
62 Host puukko.hut.fi
63 User t35124p
64 ProxyCommand ssh-proxy %h %p
65
66 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000067 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
69 Host *.su
70 Cipher none
71 PasswordAuthentication no
72
Damien Millerd27b9472005-12-13 19:29:02 +110073 Host vpn.fake.com
74 Tunnel yes
75 TunnelDevice 3
76
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 # Defaults for various options
78 Host *
79 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110080 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081 PasswordAuthentication yes
82 RSAAuthentication yes
83 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110085 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086 IdentityFile ~/.ssh/identity
87 Port 22
88 EscapeChar ~
89
90*/
91
92/* Keyword tokens. */
93
Damien Miller95def091999-11-25 00:26:21 +110094typedef enum {
95 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +100096 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000097 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +000098 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +110099 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
100 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
101 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
102 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100103 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000104 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100105 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000106 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000107 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000108 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000109 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000110 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000111 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100112 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100113 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100114 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000115 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116} OpCodes;
117
118/* Textual representations of the tokens. */
119
Damien Miller95def091999-11-25 00:26:21 +1100120static struct {
121 const char *name;
122 OpCodes opcode;
123} keywords[] = {
124 { "forwardagent", oForwardAgent },
125 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000126 { "forwardx11trusted", oForwardX11Trusted },
Damien Millerd3a18572000-06-07 19:55:44 +1000127 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100128 { "gatewayports", oGatewayPorts },
129 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000130 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100131 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100132 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
133 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100134 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100135 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000136 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000137 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000138 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000139 { "challengeresponseauthentication", oChallengeResponseAuthentication },
140 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
141 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000142 { "kerberosauthentication", oUnsupported },
143 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000144 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000145#if defined(GSSAPI)
146 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000147 { "gssapidelegatecredentials", oGssDelegateCreds },
148#else
149 { "gssapiauthentication", oUnsupported },
150 { "gssapidelegatecredentials", oUnsupported },
151#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000152 { "fallbacktorsh", oDeprecated },
153 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100154 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100155 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100156 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100157 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000158 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100159 { "proxycommand", oProxyCommand },
160 { "port", oPort },
161 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000162 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000163 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000164 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100165 { "remoteforward", oRemoteForward },
166 { "localforward", oLocalForward },
167 { "user", oUser },
168 { "host", oHost },
169 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000171 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000172 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000173 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100174 { "connectionattempts", oConnectionAttempts },
175 { "batchmode", oBatchMode },
176 { "checkhostip", oCheckHostIP },
177 { "stricthostkeychecking", oStrictHostKeyChecking },
178 { "compression", oCompression },
179 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100180 { "tcpkeepalive", oTCPKeepAlive },
181 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100182 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100183 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000184 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000185 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000186 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000187 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000188#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000189 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000190#else
191 { "smartcarddevice", oUnsupported },
192#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100193 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000194 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000195 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100196 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000197 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000198 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000199 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100200 { "serveraliveinterval", oServerAliveInterval },
201 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000202 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000203 { "controlpath", oControlPath },
204 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100205 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100206 { "tunnel", oTunnel },
207 { "tunneldevice", oTunnelDevice },
208 { "localcommand", oLocalCommand },
209 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000210 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100211};
212
Damien Miller5428f641999-11-25 11:54:57 +1100213/*
214 * Adds a local TCP/IP port forward to options. Never returns if there is an
215 * error.
216 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller4af51302000-04-16 11:18:38 +1000218void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100219add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220{
Damien Miller95def091999-11-25 00:26:21 +1100221 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000222#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100223 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100224 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000225 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100226#endif
Damien Miller95def091999-11-25 00:26:21 +1100227 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
228 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
229 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100230
231 fwd->listen_host = (newfwd->listen_host == NULL) ?
232 NULL : xstrdup(newfwd->listen_host);
233 fwd->listen_port = newfwd->listen_port;
234 fwd->connect_host = xstrdup(newfwd->connect_host);
235 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236}
237
Damien Miller5428f641999-11-25 11:54:57 +1100238/*
239 * Adds a remote TCP/IP port forward to options. Never returns if there is
240 * an error.
241 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller4af51302000-04-16 11:18:38 +1000243void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100244add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245{
Damien Miller95def091999-11-25 00:26:21 +1100246 Forward *fwd;
247 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
248 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100249 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100250 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100251
252 fwd->listen_host = (newfwd->listen_host == NULL) ?
253 NULL : xstrdup(newfwd->listen_host);
254 fwd->listen_port = newfwd->listen_port;
255 fwd->connect_host = xstrdup(newfwd->connect_host);
256 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257}
258
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000259static void
260clear_forwardings(Options *options)
261{
262 int i;
263
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100265 if (options->local_forwards[i].listen_host != NULL)
266 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100267 xfree(options->local_forwards[i].connect_host);
268 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000269 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100270 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100271 if (options->remote_forwards[i].listen_host != NULL)
272 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100273 xfree(options->remote_forwards[i].connect_host);
274 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000275 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100276 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000277}
278
Damien Miller5428f641999-11-25 11:54:57 +1100279/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000280 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100281 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282
Damien Miller4af51302000-04-16 11:18:38 +1000283static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100284parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000286 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Damien Miller95def091999-11-25 00:26:21 +1100288 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100289 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100290 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000292 error("%s: line %d: Bad configuration option: %s",
293 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100294 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295}
296
Damien Miller5428f641999-11-25 11:54:57 +1100297/*
298 * Processes a single option line as used in the configuration files. This
299 * only sets those values that have not already been set.
300 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100301#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller2ccf6611999-11-15 15:25:10 +1100303int
304process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100305 char *line, const char *filename, int linenum,
306 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100308 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100309 int opcode, *intptr, value, value2, scale;
310 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100311 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100312 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Damien Millerc652cac2003-05-14 13:40:54 +1000314 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100315 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000316 if (strchr(WHITESPACE, line[len]) == NULL)
317 break;
318 line[len] = '\0';
319 }
320
Damien Millerbe484b52000-07-15 14:14:16 +1000321 s = line;
322 /* Get the keyword. (Each line is supposed to begin with a keyword). */
323 keyword = strdelim(&s);
324 /* Ignore leading whitespace. */
325 if (*keyword == '\0')
326 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000327 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100328 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Damien Miller37023962000-07-11 17:31:38 +1000330 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller95def091999-11-25 00:26:21 +1100332 switch (opcode) {
333 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100334 /* don't panic, but count bad options */
335 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100336 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000337 case oConnectTimeout:
338 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100339parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000340 arg = strdelim(&s);
341 if (!arg || *arg == '\0')
342 fatal("%s line %d: missing time value.",
343 filename, linenum);
344 if ((value = convtime(arg)) == -1)
345 fatal("%s line %d: invalid time value.",
346 filename, linenum);
347 if (*intptr == -1)
348 *intptr = value;
349 break;
350
Damien Miller95def091999-11-25 00:26:21 +1100351 case oForwardAgent:
352 intptr = &options->forward_agent;
353parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000354 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000355 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100356 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
357 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000358 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100359 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000360 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100361 value = 0;
362 else
363 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
364 if (*activep && *intptr == -1)
365 *intptr = value;
366 break;
367
368 case oForwardX11:
369 intptr = &options->forward_x11;
370 goto parse_flag;
371
Darren Tucker0a118da2003-10-15 15:54:32 +1000372 case oForwardX11Trusted:
373 intptr = &options->forward_x11_trusted;
374 goto parse_flag;
375
Damien Miller95def091999-11-25 00:26:21 +1100376 case oGatewayPorts:
377 intptr = &options->gateway_ports;
378 goto parse_flag;
379
380 case oUsePrivilegedPort:
381 intptr = &options->use_privileged_port;
382 goto parse_flag;
383
Damien Miller95def091999-11-25 00:26:21 +1100384 case oPasswordAuthentication:
385 intptr = &options->password_authentication;
386 goto parse_flag;
387
Damien Miller874d77b2000-10-14 16:23:11 +1100388 case oKbdInteractiveAuthentication:
389 intptr = &options->kbd_interactive_authentication;
390 goto parse_flag;
391
392 case oKbdInteractiveDevices:
393 charptr = &options->kbd_interactive_devices;
394 goto parse_string;
395
Damien Miller0bc1bd82000-11-13 22:57:25 +1100396 case oPubkeyAuthentication:
397 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000398 goto parse_flag;
399
Damien Miller95def091999-11-25 00:26:21 +1100400 case oRSAAuthentication:
401 intptr = &options->rsa_authentication;
402 goto parse_flag;
403
404 case oRhostsRSAAuthentication:
405 intptr = &options->rhosts_rsa_authentication;
406 goto parse_flag;
407
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000408 case oHostbasedAuthentication:
409 intptr = &options->hostbased_authentication;
410 goto parse_flag;
411
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000412 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000413 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100414 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000415
Darren Tucker0efd1552003-08-26 11:49:55 +1000416 case oGssAuthentication:
417 intptr = &options->gss_authentication;
418 goto parse_flag;
419
420 case oGssDelegateCreds:
421 intptr = &options->gss_deleg_creds;
422 goto parse_flag;
423
Damien Miller95def091999-11-25 00:26:21 +1100424 case oBatchMode:
425 intptr = &options->batch_mode;
426 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427
Damien Miller95def091999-11-25 00:26:21 +1100428 case oCheckHostIP:
429 intptr = &options->check_host_ip;
430 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Damien Miller37876e92003-05-15 10:19:46 +1000432 case oVerifyHostKeyDNS:
433 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100434 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000435
Damien Miller95def091999-11-25 00:26:21 +1100436 case oStrictHostKeyChecking:
437 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100438parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000439 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000440 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000441 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100442 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100443 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000444 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100445 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000446 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100447 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000448 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100449 value = 2;
450 else
451 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
452 if (*activep && *intptr == -1)
453 *intptr = value;
454 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455
Damien Miller95def091999-11-25 00:26:21 +1100456 case oCompression:
457 intptr = &options->compression;
458 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller12c150e2003-12-17 16:31:10 +1100460 case oTCPKeepAlive:
461 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100462 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000464 case oNoHostAuthenticationForLocalhost:
465 intptr = &options->no_host_authentication_for_localhost;
466 goto parse_flag;
467
Damien Miller95def091999-11-25 00:26:21 +1100468 case oNumberOfPasswordPrompts:
469 intptr = &options->number_of_password_prompts;
470 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471
Damien Miller95def091999-11-25 00:26:21 +1100472 case oCompressionLevel:
473 intptr = &options->compression_level;
474 goto parse_int;
475
Damien Millera5539d22003-04-09 20:50:06 +1000476 case oRekeyLimit:
477 intptr = &options->rekey_limit;
478 arg = strdelim(&s);
479 if (!arg || *arg == '\0')
480 fatal("%.200s line %d: Missing argument.", filename, linenum);
481 if (arg[0] < '0' || arg[0] > '9')
482 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100483 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000484 if (arg == endofnumber)
485 fatal("%.200s line %d: Bad number.", filename, linenum);
486 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100487 case '\0':
488 scale = 1;
489 break;
Damien Millera5539d22003-04-09 20:50:06 +1000490 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100491 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000492 break;
493 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100494 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000495 break;
496 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100497 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000498 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100499 default:
500 fatal("%.200s line %d: Invalid RekeyLimit suffix",
501 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000502 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100503 val64 *= scale;
504 /* detect integer wrap and too-large limits */
505 if ((val64 / scale) != orig || val64 > INT_MAX)
506 fatal("%.200s line %d: RekeyLimit too large",
507 filename, linenum);
508 if (val64 < 16)
509 fatal("%.200s line %d: RekeyLimit too small",
510 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000511 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100512 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000513 break;
514
Damien Miller95def091999-11-25 00:26:21 +1100515 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000516 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000517 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100518 fatal("%.200s line %d: Missing argument.", filename, linenum);
519 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100520 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000521 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100522 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100523 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100524 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000525 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000526 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100527 }
528 break;
529
Damien Millerd3a18572000-06-07 19:55:44 +1000530 case oXAuthLocation:
531 charptr=&options->xauth_location;
532 goto parse_string;
533
Damien Miller95def091999-11-25 00:26:21 +1100534 case oUser:
535 charptr = &options->user;
536parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000537 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000538 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100539 fatal("%.200s line %d: Missing argument.", filename, linenum);
540 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000541 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100542 break;
543
544 case oGlobalKnownHostsFile:
545 charptr = &options->system_hostfile;
546 goto parse_string;
547
548 case oUserKnownHostsFile:
549 charptr = &options->user_hostfile;
550 goto parse_string;
551
Damien Millereba71ba2000-04-29 23:57:08 +1000552 case oGlobalKnownHostsFile2:
553 charptr = &options->system_hostfile2;
554 goto parse_string;
555
556 case oUserKnownHostsFile2:
557 charptr = &options->user_hostfile2;
558 goto parse_string;
559
Damien Miller95def091999-11-25 00:26:21 +1100560 case oHostName:
561 charptr = &options->hostname;
562 goto parse_string;
563
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000564 case oHostKeyAlias:
565 charptr = &options->host_key_alias;
566 goto parse_string;
567
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000568 case oPreferredAuthentications:
569 charptr = &options->preferred_authentications;
570 goto parse_string;
571
Ben Lindstrome0f88042001-04-30 13:06:24 +0000572 case oBindAddress:
573 charptr = &options->bind_address;
574 goto parse_string;
575
Ben Lindstromae996bf2001-08-06 21:27:53 +0000576 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000577 charptr = &options->smartcard_device;
578 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000579
Damien Miller95def091999-11-25 00:26:21 +1100580 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100581 charptr = &options->proxy_command;
582parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000583 if (s == NULL)
584 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100585 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100586 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100587 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100588 return 0;
589
590 case oPort:
591 intptr = &options->port;
592parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000593 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000594 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100595 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000596 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100597 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100598
599 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000600 value = strtol(arg, &endofnumber, 0);
601 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100602 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100603 if (*activep && *intptr == -1)
604 *intptr = value;
605 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606
Damien Miller95def091999-11-25 00:26:21 +1100607 case oConnectionAttempts:
608 intptr = &options->connection_attempts;
609 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100610
Damien Miller95def091999-11-25 00:26:21 +1100611 case oCipher:
612 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000613 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000614 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000615 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000616 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100617 if (value == -1)
618 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100619 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100620 if (*activep && *intptr == -1)
621 *intptr = value;
622 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623
Damien Miller78928792000-04-12 20:17:38 +1000624 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000625 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000626 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000627 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000628 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000629 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100630 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000631 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000632 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000633 break;
634
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000635 case oMacs:
636 arg = strdelim(&s);
637 if (!arg || *arg == '\0')
638 fatal("%.200s line %d: Missing argument.", filename, linenum);
639 if (!mac_valid(arg))
640 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100641 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000642 if (*activep && options->macs == NULL)
643 options->macs = xstrdup(arg);
644 break;
645
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000646 case oHostKeyAlgorithms:
647 arg = strdelim(&s);
648 if (!arg || *arg == '\0')
649 fatal("%.200s line %d: Missing argument.", filename, linenum);
650 if (!key_names_valid2(arg))
651 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100652 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000653 if (*activep && options->hostkeyalgorithms == NULL)
654 options->hostkeyalgorithms = xstrdup(arg);
655 break;
656
Damien Miller78928792000-04-12 20:17:38 +1000657 case oProtocol:
658 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000659 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000660 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000661 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000662 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000663 if (value == SSH_PROTO_UNKNOWN)
664 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100665 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000666 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
667 *intptr = value;
668 break;
669
Damien Miller95def091999-11-25 00:26:21 +1100670 case oLogLevel:
671 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000672 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000673 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100674 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000675 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100676 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100677 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100678 *intptr = (LogLevel) value;
679 break;
680
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000681 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100682 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000683 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100684 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000685 fatal("%.200s line %d: Missing port argument.",
686 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100687 arg2 = strdelim(&s);
688 if (arg2 == NULL || *arg2 == '\0')
689 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000690 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100691
692 /* construct a string for parse_forward */
693 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
694
695 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000696 fatal("%.200s line %d: Bad forwarding specification.",
697 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100698
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000699 if (*activep) {
700 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100701 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000702 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100703 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000704 }
Damien Miller95def091999-11-25 00:26:21 +1100705 break;
706
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000707 case oDynamicForward:
708 arg = strdelim(&s);
709 if (!arg || *arg == '\0')
710 fatal("%.200s line %d: Missing port argument.",
711 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100712 memset(&fwd, '\0', sizeof(fwd));
713 fwd.connect_host = "socks";
714 fwd.listen_host = hpdelim(&arg);
715 if (fwd.listen_host == NULL ||
716 strlen(fwd.listen_host) >= NI_MAXHOST)
717 fatal("%.200s line %d: Bad forwarding specification.",
718 filename, linenum);
719 if (arg) {
720 fwd.listen_port = a2port(arg);
721 fwd.listen_host = cleanhostname(fwd.listen_host);
722 } else {
723 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000724 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100725 }
726 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000727 fatal("%.200s line %d: Badly formatted port number.",
728 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000729 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100730 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000731 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000732
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000733 case oClearAllForwardings:
734 intptr = &options->clear_forwardings;
735 goto parse_flag;
736
Damien Miller95def091999-11-25 00:26:21 +1100737 case oHost:
738 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000739 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000740 if (match_pattern(host, arg)) {
741 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100742 *activep = 1;
743 break;
744 }
Damien Millerbe484b52000-07-15 14:14:16 +1000745 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100746 return 0;
747
748 case oEscapeChar:
749 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000750 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000751 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100752 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000753 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000754 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
755 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000756 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000757 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000758 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000759 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100760 else {
761 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100762 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100763 /* NOTREACHED */
764 value = 0; /* Avoid compiler warning. */
765 }
766 if (*activep && *intptr == -1)
767 *intptr = value;
768 break;
769
Damien Miller20a8f972003-05-18 20:50:30 +1000770 case oAddressFamily:
771 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000772 if (!arg || *arg == '\0')
773 fatal("%s line %d: missing address family.",
774 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000775 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000776 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000777 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000778 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000779 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000780 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000781 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000782 else
783 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000784 if (*activep && *intptr == -1)
785 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000786 break;
787
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000788 case oEnableSSHKeysign:
789 intptr = &options->enable_ssh_keysign;
790 goto parse_flag;
791
Damien Millerbd394c32004-03-08 23:12:36 +1100792 case oIdentitiesOnly:
793 intptr = &options->identities_only;
794 goto parse_flag;
795
Damien Miller509b0102003-12-17 16:33:10 +1100796 case oServerAliveInterval:
797 intptr = &options->server_alive_interval;
798 goto parse_time;
799
800 case oServerAliveCountMax:
801 intptr = &options->server_alive_count_max;
802 goto parse_int;
803
Darren Tucker46bc0752004-05-02 22:11:30 +1000804 case oSendEnv:
805 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
806 if (strchr(arg, '=') != NULL)
807 fatal("%s line %d: Invalid environment name.",
808 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100809 if (!*activep)
810 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000811 if (options->num_send_env >= MAX_SEND_ENV)
812 fatal("%s line %d: too many send env.",
813 filename, linenum);
814 options->send_env[options->num_send_env++] =
815 xstrdup(arg);
816 }
817 break;
818
Damien Miller0e220db2004-06-15 10:34:08 +1000819 case oControlPath:
820 charptr = &options->control_path;
821 goto parse_string;
822
823 case oControlMaster:
824 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000825 arg = strdelim(&s);
826 if (!arg || *arg == '\0')
827 fatal("%.200s line %d: Missing ControlMaster argument.",
828 filename, linenum);
829 value = 0; /* To avoid compiler warning... */
830 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
831 value = SSHCTL_MASTER_YES;
832 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
833 value = SSHCTL_MASTER_NO;
834 else if (strcmp(arg, "auto") == 0)
835 value = SSHCTL_MASTER_AUTO;
836 else if (strcmp(arg, "ask") == 0)
837 value = SSHCTL_MASTER_ASK;
838 else if (strcmp(arg, "autoask") == 0)
839 value = SSHCTL_MASTER_AUTO_ASK;
840 else
841 fatal("%.200s line %d: Bad ControlMaster argument.",
842 filename, linenum);
843 if (*activep && *intptr == -1)
844 *intptr = value;
845 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000846
Damien Millere1776152005-03-01 21:47:37 +1100847 case oHashKnownHosts:
848 intptr = &options->hash_known_hosts;
849 goto parse_flag;
850
Damien Millerd27b9472005-12-13 19:29:02 +1100851 case oTunnel:
852 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100853 arg = strdelim(&s);
854 if (!arg || *arg == '\0')
855 fatal("%s line %d: Missing yes/point-to-point/"
856 "ethernet/no argument.", filename, linenum);
857 value = 0; /* silence compiler */
858 if (strcasecmp(arg, "ethernet") == 0)
859 value = SSH_TUNMODE_ETHERNET;
860 else if (strcasecmp(arg, "point-to-point") == 0)
861 value = SSH_TUNMODE_POINTOPOINT;
862 else if (strcasecmp(arg, "yes") == 0)
863 value = SSH_TUNMODE_DEFAULT;
864 else if (strcasecmp(arg, "no") == 0)
865 value = SSH_TUNMODE_NO;
866 else
867 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
868 "no argument: %s", filename, linenum, arg);
869 if (*activep)
870 *intptr = value;
871 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100872
873 case oTunnelDevice:
874 arg = strdelim(&s);
875 if (!arg || *arg == '\0')
876 fatal("%.200s line %d: Missing argument.", filename, linenum);
877 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100878 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100879 fatal("%.200s line %d: Bad tun device.", filename, linenum);
880 if (*activep) {
881 options->tun_local = value;
882 options->tun_remote = value2;
883 }
884 break;
885
886 case oLocalCommand:
887 charptr = &options->local_command;
888 goto parse_command;
889
890 case oPermitLocalCommand:
891 intptr = &options->permit_local_command;
892 goto parse_flag;
893
Ben Lindstrom4daea862002-06-09 20:04:02 +0000894 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000895 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000896 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000897 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000898
Damien Millerf9b3feb2003-05-16 11:38:32 +1000899 case oUnsupported:
900 error("%s line %d: Unsupported option \"%s\"",
901 filename, linenum, keyword);
902 return 0;
903
Damien Miller95def091999-11-25 00:26:21 +1100904 default:
905 fatal("process_config_line: Unimplemented opcode %d", opcode);
906 }
907
908 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000909 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000910 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000911 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000912 }
Damien Miller95def091999-11-25 00:26:21 +1100913 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914}
915
916
Damien Miller5428f641999-11-25 11:54:57 +1100917/*
918 * Reads the config file and modifies the options accordingly. Options
919 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000920 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100921 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000923int
Darren Tuckerfc959702004-07-17 16:12:08 +1000924read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000925 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926{
Damien Miller95def091999-11-25 00:26:21 +1100927 FILE *f;
928 char line[1024];
929 int active, linenum;
930 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931
Damien Miller95def091999-11-25 00:26:21 +1100932 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000933 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000934 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935
Damien Miller57a44762004-04-20 20:11:57 +1000936 if (checkperm) {
937 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000938
Damien Miller33793852004-06-15 10:27:55 +1000939 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000940 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000941 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000942 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000943 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000944 }
945
Damien Miller95def091999-11-25 00:26:21 +1100946 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947
Damien Miller5428f641999-11-25 11:54:57 +1100948 /*
949 * Mark that we are now processing the options. This flag is turned
950 * on/off by Host specifications.
951 */
Damien Miller95def091999-11-25 00:26:21 +1100952 active = 1;
953 linenum = 0;
954 while (fgets(line, sizeof(line), f)) {
955 /* Update line number counter. */
956 linenum++;
957 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
958 bad_options++;
959 }
960 fclose(f);
961 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000962 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100963 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000964 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965}
966
Damien Miller5428f641999-11-25 11:54:57 +1100967/*
968 * Initializes options to special values that indicate that they have not yet
969 * been set. Read_config_file will only set options with this value. Options
970 * are processed in the following order: command line, user config file,
971 * system config file. Last, fill_default_options is called.
972 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller4af51302000-04-16 11:18:38 +1000974void
Damien Miller95def091999-11-25 00:26:21 +1100975initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976{
Damien Miller95def091999-11-25 00:26:21 +1100977 memset(options, 'X', sizeof(*options));
978 options->forward_agent = -1;
979 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000980 options->forward_x11_trusted = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000981 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100982 options->gateway_ports = -1;
983 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100984 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100985 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000986 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000987 options->gss_authentication = -1;
988 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100989 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100990 options->kbd_interactive_authentication = -1;
991 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100992 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000993 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100994 options->batch_mode = -1;
995 options->check_host_ip = -1;
996 options->strict_host_key_checking = -1;
997 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100998 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +1100999 options->compression_level = -1;
1000 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001001 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001002 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001003 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001004 options->number_of_password_prompts = -1;
1005 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001006 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001007 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001008 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001009 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001010 options->num_identity_files = 0;
1011 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001012 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001013 options->proxy_command = NULL;
1014 options->user = NULL;
1015 options->escape_char = -1;
1016 options->system_hostfile = NULL;
1017 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001018 options->system_hostfile2 = NULL;
1019 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001020 options->num_local_forwards = 0;
1021 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001022 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001023 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001024 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001025 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001026 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001027 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001028 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001029 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001030 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001031 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001032 options->server_alive_interval = -1;
1033 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001034 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001035 options->control_path = NULL;
1036 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001037 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001038 options->tun_open = -1;
1039 options->tun_local = -1;
1040 options->tun_remote = -1;
1041 options->local_command = NULL;
1042 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043}
1044
Damien Miller5428f641999-11-25 11:54:57 +11001045/*
1046 * Called after processing other sources of option data, this fills those
1047 * options for which no value has been specified with their default values.
1048 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049
Damien Miller4af51302000-04-16 11:18:38 +10001050void
Damien Miller95def091999-11-25 00:26:21 +11001051fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001053 int len;
1054
Damien Miller95def091999-11-25 00:26:21 +11001055 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001056 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001057 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001058 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001059 if (options->forward_x11_trusted == -1)
1060 options->forward_x11_trusted = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001061 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001062 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001063 if (options->gateway_ports == -1)
1064 options->gateway_ports = 0;
1065 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001066 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001067 if (options->rsa_authentication == -1)
1068 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001069 if (options->pubkey_authentication == -1)
1070 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001071 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001072 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001073 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001074 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001075 if (options->gss_deleg_creds == -1)
1076 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001077 if (options->password_authentication == -1)
1078 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001079 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001080 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001081 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001082 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001083 if (options->hostbased_authentication == -1)
1084 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001085 if (options->batch_mode == -1)
1086 options->batch_mode = 0;
1087 if (options->check_host_ip == -1)
1088 options->check_host_ip = 1;
1089 if (options->strict_host_key_checking == -1)
1090 options->strict_host_key_checking = 2; /* 2 is default */
1091 if (options->compression == -1)
1092 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001093 if (options->tcp_keep_alive == -1)
1094 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001095 if (options->compression_level == -1)
1096 options->compression_level = 6;
1097 if (options->port == -1)
1098 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001099 if (options->address_family == -1)
1100 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001101 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001102 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001103 if (options->number_of_password_prompts == -1)
1104 options->number_of_password_prompts = 3;
1105 /* Selected in ssh_login(). */
1106 if (options->cipher == -1)
1107 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001108 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001109 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001110 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001111 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001112 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001113 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001114 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001115 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001116 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001117 xmalloc(len);
1118 snprintf(options->identity_files[options->num_identity_files++],
1119 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001120 }
1121 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001122 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1123 options->identity_files[options->num_identity_files] =
1124 xmalloc(len);
1125 snprintf(options->identity_files[options->num_identity_files++],
1126 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1127
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001128 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001129 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001130 xmalloc(len);
1131 snprintf(options->identity_files[options->num_identity_files++],
1132 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001133 }
Damien Millereba71ba2000-04-29 23:57:08 +10001134 }
Damien Miller95def091999-11-25 00:26:21 +11001135 if (options->escape_char == -1)
1136 options->escape_char = '~';
1137 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001138 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001139 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001140 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001141 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001142 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001143 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001144 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001145 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001146 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001147 if (options->clear_forwardings == 1)
1148 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001149 if (options->no_host_authentication_for_localhost == - 1)
1150 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001151 if (options->identities_only == -1)
1152 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001153 if (options->enable_ssh_keysign == -1)
1154 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001155 if (options->rekey_limit == -1)
1156 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001157 if (options->verify_host_key_dns == -1)
1158 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001159 if (options->server_alive_interval == -1)
1160 options->server_alive_interval = 0;
1161 if (options->server_alive_count_max == -1)
1162 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001163 if (options->control_master == -1)
1164 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001165 if (options->hash_known_hosts == -1)
1166 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001167 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001168 options->tun_open = SSH_TUNMODE_NO;
1169 if (options->tun_local == -1)
1170 options->tun_local = SSH_TUNID_ANY;
1171 if (options->tun_remote == -1)
1172 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001173 if (options->permit_local_command == -1)
1174 options->permit_local_command = 0;
1175 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001176 /* options->proxy_command should not be set by default */
1177 /* options->user will be set in the main program if appropriate */
1178 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001179 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001180 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001182
1183/*
1184 * parse_forward
1185 * parses a string containing a port forwarding specification of the form:
1186 * [listenhost:]listenport:connecthost:connectport
1187 * returns number of arguments parsed or zero on error
1188 */
1189int
1190parse_forward(Forward *fwd, const char *fwdspec)
1191{
1192 int i;
1193 char *p, *cp, *fwdarg[4];
1194
1195 memset(fwd, '\0', sizeof(*fwd));
1196
1197 cp = p = xstrdup(fwdspec);
1198
1199 /* skip leading spaces */
1200 while (*cp && isspace(*cp))
1201 cp++;
1202
1203 for (i = 0; i < 4; ++i)
1204 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1205 break;
1206
1207 /* Check for trailing garbage in 4-arg case*/
1208 if (cp != NULL)
1209 i = 0; /* failure */
1210
1211 switch (i) {
1212 case 3:
1213 fwd->listen_host = NULL;
1214 fwd->listen_port = a2port(fwdarg[0]);
1215 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1216 fwd->connect_port = a2port(fwdarg[2]);
1217 break;
1218
1219 case 4:
1220 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1221 fwd->listen_port = a2port(fwdarg[1]);
1222 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1223 fwd->connect_port = a2port(fwdarg[3]);
1224 break;
1225 default:
1226 i = 0; /* failure */
1227 }
1228
1229 xfree(p);
1230
1231 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1232 goto fail_free;
1233
1234 if (fwd->connect_host != NULL &&
1235 strlen(fwd->connect_host) >= NI_MAXHOST)
1236 goto fail_free;
1237
1238 return (i);
1239
1240 fail_free:
1241 if (fwd->connect_host != NULL)
1242 xfree(fwd->connect_host);
1243 if (fwd->listen_host != NULL)
1244 xfree(fwd->listen_host);
1245 return (0);
1246}