blob: 73271e84eac0977a9c886179538b5b4eeb94683e [file] [log] [blame]
Damien Millere6b3b612006-07-24 14:01:23 +10001/* $OpenBSD: readconf.c,v 1.156 2006/07/17 01:31: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>
Darren Tucker39972492006-07-12 22:22:46 +100024#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100025#if defined(HAVE_NETDB_H)
26# include <netdb.h>
27#endif
Damien Millere6b3b612006-07-24 14:01:23 +100028#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110029
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100032#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "cipher.h"
34#include "pathnames.h"
35#include "log.h"
36#include "readconf.h"
37#include "match.h"
38#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000039#include "kex.h"
40#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42/* Format of the configuration file:
43
44 # Configuration data is parsed as follows:
45 # 1. command line options
46 # 2. user-specific file
47 # 3. system-wide file
48 # Any configuration value is only changed the first time it is set.
49 # Thus, host-specific definitions should be at the beginning of the
50 # configuration file, and defaults at the end.
51
52 # Host-specific declarations. These may override anything above. A single
53 # host may match multiple declarations; these are processed in the order
54 # that they are given in.
55
56 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000057 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59 Host fake.com
60 HostName another.host.name.real.org
61 User blaah
62 Port 34289
63 ForwardX11 no
64 ForwardAgent no
65
66 Host books.com
67 RemoteForward 9999 shadows.cs.hut.fi:9999
68 Cipher 3des
69
70 Host fascist.blob.com
71 Port 23123
72 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073 PasswordAuthentication no
74
75 Host puukko.hut.fi
76 User t35124p
77 ProxyCommand ssh-proxy %h %p
78
79 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000080 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82 Host *.su
83 Cipher none
84 PasswordAuthentication no
85
Damien Millerd27b9472005-12-13 19:29:02 +110086 Host vpn.fake.com
87 Tunnel yes
88 TunnelDevice 3
89
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 # Defaults for various options
91 Host *
92 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110093 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 PasswordAuthentication yes
95 RSAAuthentication yes
96 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110098 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099 IdentityFile ~/.ssh/identity
100 Port 22
101 EscapeChar ~
102
103*/
104
105/* Keyword tokens. */
106
Damien Miller95def091999-11-25 00:26:21 +1100107typedef enum {
108 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000109 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000110 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000111 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000112 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100113 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
114 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
115 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
116 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100117 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000118 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100119 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000120 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000121 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000122 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000123 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000124 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000125 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100126 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100127 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100128 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000129 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130} OpCodes;
131
132/* Textual representations of the tokens. */
133
Damien Miller95def091999-11-25 00:26:21 +1100134static struct {
135 const char *name;
136 OpCodes opcode;
137} keywords[] = {
138 { "forwardagent", oForwardAgent },
139 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000140 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000141 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000142 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100143 { "gatewayports", oGatewayPorts },
144 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000145 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100146 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100147 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
148 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100149 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100150 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000151 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000152 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000153 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000154 { "challengeresponseauthentication", oChallengeResponseAuthentication },
155 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
156 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000157 { "kerberosauthentication", oUnsupported },
158 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000159 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000160#if defined(GSSAPI)
161 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000162 { "gssapidelegatecredentials", oGssDelegateCreds },
163#else
164 { "gssapiauthentication", oUnsupported },
165 { "gssapidelegatecredentials", oUnsupported },
166#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000167 { "fallbacktorsh", oDeprecated },
168 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100169 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100171 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100172 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000173 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100174 { "proxycommand", oProxyCommand },
175 { "port", oPort },
176 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000177 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000178 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000179 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "remoteforward", oRemoteForward },
181 { "localforward", oLocalForward },
182 { "user", oUser },
183 { "host", oHost },
184 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100185 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000186 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000187 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000188 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100189 { "connectionattempts", oConnectionAttempts },
190 { "batchmode", oBatchMode },
191 { "checkhostip", oCheckHostIP },
192 { "stricthostkeychecking", oStrictHostKeyChecking },
193 { "compression", oCompression },
194 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100195 { "tcpkeepalive", oTCPKeepAlive },
196 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100197 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100198 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000199 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000200 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000201 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000202 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000203#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000204 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000205#else
206 { "smartcarddevice", oUnsupported },
207#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100208 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000209 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000210 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100211 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000212 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000213 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000214 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100215 { "serveraliveinterval", oServerAliveInterval },
216 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000217 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000218 { "controlpath", oControlPath },
219 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100220 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100221 { "tunnel", oTunnel },
222 { "tunneldevice", oTunnelDevice },
223 { "localcommand", oLocalCommand },
224 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000225 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100226};
227
Damien Miller5428f641999-11-25 11:54:57 +1100228/*
229 * Adds a local TCP/IP port forward to options. Never returns if there is an
230 * error.
231 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232
Damien Miller4af51302000-04-16 11:18:38 +1000233void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100234add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235{
Damien Miller95def091999-11-25 00:26:21 +1100236 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000237#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100238 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100239 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000240 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100241#endif
Damien Miller95def091999-11-25 00:26:21 +1100242 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
243 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
244 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100245
246 fwd->listen_host = (newfwd->listen_host == NULL) ?
247 NULL : xstrdup(newfwd->listen_host);
248 fwd->listen_port = newfwd->listen_port;
249 fwd->connect_host = xstrdup(newfwd->connect_host);
250 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251}
252
Damien Miller5428f641999-11-25 11:54:57 +1100253/*
254 * Adds a remote TCP/IP port forward to options. Never returns if there is
255 * an error.
256 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller4af51302000-04-16 11:18:38 +1000258void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100259add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260{
Damien Miller95def091999-11-25 00:26:21 +1100261 Forward *fwd;
262 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
263 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100264 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100265 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100266
267 fwd->listen_host = (newfwd->listen_host == NULL) ?
268 NULL : xstrdup(newfwd->listen_host);
269 fwd->listen_port = newfwd->listen_port;
270 fwd->connect_host = xstrdup(newfwd->connect_host);
271 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272}
273
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000274static void
275clear_forwardings(Options *options)
276{
277 int i;
278
Damien Millerf91ee4c2005-03-01 21:24:33 +1100279 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100280 if (options->local_forwards[i].listen_host != NULL)
281 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100282 xfree(options->local_forwards[i].connect_host);
283 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000284 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100285 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100286 if (options->remote_forwards[i].listen_host != NULL)
287 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 xfree(options->remote_forwards[i].connect_host);
289 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000290 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100291 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000292}
293
Damien Miller5428f641999-11-25 11:54:57 +1100294/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000295 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100296 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller4af51302000-04-16 11:18:38 +1000298static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100299parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000301 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller95def091999-11-25 00:26:21 +1100303 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100304 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100305 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000307 error("%s: line %d: Bad configuration option: %s",
308 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100309 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310}
311
Damien Miller5428f641999-11-25 11:54:57 +1100312/*
313 * Processes a single option line as used in the configuration files. This
314 * only sets those values that have not already been set.
315 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100316#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Damien Miller2ccf6611999-11-15 15:25:10 +1100318int
319process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100320 char *line, const char *filename, int linenum,
321 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100323 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100324 int opcode, *intptr, value, value2, scale;
325 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100326 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100327 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
Damien Millerc652cac2003-05-14 13:40:54 +1000329 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100330 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000331 if (strchr(WHITESPACE, line[len]) == NULL)
332 break;
333 line[len] = '\0';
334 }
335
Damien Millerbe484b52000-07-15 14:14:16 +1000336 s = line;
337 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100338 if ((keyword = strdelim(&s)) == NULL)
339 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000340 /* Ignore leading whitespace. */
341 if (*keyword == '\0')
342 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000343 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100344 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller37023962000-07-11 17:31:38 +1000346 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller95def091999-11-25 00:26:21 +1100348 switch (opcode) {
349 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100350 /* don't panic, but count bad options */
351 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100352 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000353 case oConnectTimeout:
354 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100355parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000356 arg = strdelim(&s);
357 if (!arg || *arg == '\0')
358 fatal("%s line %d: missing time value.",
359 filename, linenum);
360 if ((value = convtime(arg)) == -1)
361 fatal("%s line %d: invalid time value.",
362 filename, linenum);
363 if (*intptr == -1)
364 *intptr = value;
365 break;
366
Damien Miller95def091999-11-25 00:26:21 +1100367 case oForwardAgent:
368 intptr = &options->forward_agent;
369parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000370 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000371 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100372 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
373 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000374 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100375 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000376 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100377 value = 0;
378 else
379 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
380 if (*activep && *intptr == -1)
381 *intptr = value;
382 break;
383
384 case oForwardX11:
385 intptr = &options->forward_x11;
386 goto parse_flag;
387
Darren Tucker0a118da2003-10-15 15:54:32 +1000388 case oForwardX11Trusted:
389 intptr = &options->forward_x11_trusted;
390 goto parse_flag;
391
Damien Miller95def091999-11-25 00:26:21 +1100392 case oGatewayPorts:
393 intptr = &options->gateway_ports;
394 goto parse_flag;
395
Darren Tuckere7d4b192006-07-12 22:17:10 +1000396 case oExitOnForwardFailure:
397 intptr = &options->exit_on_forward_failure;
398 goto parse_flag;
399
Damien Miller95def091999-11-25 00:26:21 +1100400 case oUsePrivilegedPort:
401 intptr = &options->use_privileged_port;
402 goto parse_flag;
403
Damien Miller95def091999-11-25 00:26:21 +1100404 case oPasswordAuthentication:
405 intptr = &options->password_authentication;
406 goto parse_flag;
407
Damien Miller874d77b2000-10-14 16:23:11 +1100408 case oKbdInteractiveAuthentication:
409 intptr = &options->kbd_interactive_authentication;
410 goto parse_flag;
411
412 case oKbdInteractiveDevices:
413 charptr = &options->kbd_interactive_devices;
414 goto parse_string;
415
Damien Miller0bc1bd82000-11-13 22:57:25 +1100416 case oPubkeyAuthentication:
417 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000418 goto parse_flag;
419
Damien Miller95def091999-11-25 00:26:21 +1100420 case oRSAAuthentication:
421 intptr = &options->rsa_authentication;
422 goto parse_flag;
423
424 case oRhostsRSAAuthentication:
425 intptr = &options->rhosts_rsa_authentication;
426 goto parse_flag;
427
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000428 case oHostbasedAuthentication:
429 intptr = &options->hostbased_authentication;
430 goto parse_flag;
431
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000432 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000433 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100434 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000435
Darren Tucker0efd1552003-08-26 11:49:55 +1000436 case oGssAuthentication:
437 intptr = &options->gss_authentication;
438 goto parse_flag;
439
440 case oGssDelegateCreds:
441 intptr = &options->gss_deleg_creds;
442 goto parse_flag;
443
Damien Miller95def091999-11-25 00:26:21 +1100444 case oBatchMode:
445 intptr = &options->batch_mode;
446 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
Damien Miller95def091999-11-25 00:26:21 +1100448 case oCheckHostIP:
449 intptr = &options->check_host_ip;
450 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Miller37876e92003-05-15 10:19:46 +1000452 case oVerifyHostKeyDNS:
453 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100454 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000455
Damien Miller95def091999-11-25 00:26:21 +1100456 case oStrictHostKeyChecking:
457 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100458parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000459 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000460 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000461 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100462 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100463 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000464 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100465 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000466 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100467 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000468 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100469 value = 2;
470 else
471 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
472 if (*activep && *intptr == -1)
473 *intptr = value;
474 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475
Damien Miller95def091999-11-25 00:26:21 +1100476 case oCompression:
477 intptr = &options->compression;
478 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479
Damien Miller12c150e2003-12-17 16:31:10 +1100480 case oTCPKeepAlive:
481 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100482 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000484 case oNoHostAuthenticationForLocalhost:
485 intptr = &options->no_host_authentication_for_localhost;
486 goto parse_flag;
487
Damien Miller95def091999-11-25 00:26:21 +1100488 case oNumberOfPasswordPrompts:
489 intptr = &options->number_of_password_prompts;
490 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Damien Miller95def091999-11-25 00:26:21 +1100492 case oCompressionLevel:
493 intptr = &options->compression_level;
494 goto parse_int;
495
Damien Millera5539d22003-04-09 20:50:06 +1000496 case oRekeyLimit:
497 intptr = &options->rekey_limit;
498 arg = strdelim(&s);
499 if (!arg || *arg == '\0')
500 fatal("%.200s line %d: Missing argument.", filename, linenum);
501 if (arg[0] < '0' || arg[0] > '9')
502 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100503 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000504 if (arg == endofnumber)
505 fatal("%.200s line %d: Bad number.", filename, linenum);
506 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100507 case '\0':
508 scale = 1;
509 break;
Damien Millera5539d22003-04-09 20:50:06 +1000510 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100511 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000512 break;
513 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100514 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000515 break;
516 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100517 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000518 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100519 default:
520 fatal("%.200s line %d: Invalid RekeyLimit suffix",
521 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000522 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100523 val64 *= scale;
524 /* detect integer wrap and too-large limits */
525 if ((val64 / scale) != orig || val64 > INT_MAX)
526 fatal("%.200s line %d: RekeyLimit too large",
527 filename, linenum);
528 if (val64 < 16)
529 fatal("%.200s line %d: RekeyLimit too small",
530 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000531 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100532 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000533 break;
534
Damien Miller95def091999-11-25 00:26:21 +1100535 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000536 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000537 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100538 fatal("%.200s line %d: Missing argument.", filename, linenum);
539 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100540 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000541 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100542 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100543 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100544 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000545 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000546 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100547 }
548 break;
549
Damien Millerd3a18572000-06-07 19:55:44 +1000550 case oXAuthLocation:
551 charptr=&options->xauth_location;
552 goto parse_string;
553
Damien Miller95def091999-11-25 00:26:21 +1100554 case oUser:
555 charptr = &options->user;
556parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000557 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000558 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100559 fatal("%.200s line %d: Missing argument.", filename, linenum);
560 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000561 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100562 break;
563
564 case oGlobalKnownHostsFile:
565 charptr = &options->system_hostfile;
566 goto parse_string;
567
568 case oUserKnownHostsFile:
569 charptr = &options->user_hostfile;
570 goto parse_string;
571
Damien Millereba71ba2000-04-29 23:57:08 +1000572 case oGlobalKnownHostsFile2:
573 charptr = &options->system_hostfile2;
574 goto parse_string;
575
576 case oUserKnownHostsFile2:
577 charptr = &options->user_hostfile2;
578 goto parse_string;
579
Damien Miller95def091999-11-25 00:26:21 +1100580 case oHostName:
581 charptr = &options->hostname;
582 goto parse_string;
583
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000584 case oHostKeyAlias:
585 charptr = &options->host_key_alias;
586 goto parse_string;
587
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000588 case oPreferredAuthentications:
589 charptr = &options->preferred_authentications;
590 goto parse_string;
591
Ben Lindstrome0f88042001-04-30 13:06:24 +0000592 case oBindAddress:
593 charptr = &options->bind_address;
594 goto parse_string;
595
Ben Lindstromae996bf2001-08-06 21:27:53 +0000596 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000597 charptr = &options->smartcard_device;
598 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000599
Damien Miller95def091999-11-25 00:26:21 +1100600 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100601 charptr = &options->proxy_command;
602parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000603 if (s == NULL)
604 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100605 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100606 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100607 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100608 return 0;
609
610 case oPort:
611 intptr = &options->port;
612parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000613 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000614 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100615 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000616 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100617 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100618
619 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000620 value = strtol(arg, &endofnumber, 0);
621 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100622 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100623 if (*activep && *intptr == -1)
624 *intptr = value;
625 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Damien Miller95def091999-11-25 00:26:21 +1100627 case oConnectionAttempts:
628 intptr = &options->connection_attempts;
629 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100630
Damien Miller95def091999-11-25 00:26:21 +1100631 case oCipher:
632 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000633 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000634 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000635 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000636 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100637 if (value == -1)
638 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100639 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100640 if (*activep && *intptr == -1)
641 *intptr = value;
642 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643
Damien Miller78928792000-04-12 20:17:38 +1000644 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000645 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000646 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000647 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000648 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000649 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100650 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000651 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000652 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000653 break;
654
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000655 case oMacs:
656 arg = strdelim(&s);
657 if (!arg || *arg == '\0')
658 fatal("%.200s line %d: Missing argument.", filename, linenum);
659 if (!mac_valid(arg))
660 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100661 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000662 if (*activep && options->macs == NULL)
663 options->macs = xstrdup(arg);
664 break;
665
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000666 case oHostKeyAlgorithms:
667 arg = strdelim(&s);
668 if (!arg || *arg == '\0')
669 fatal("%.200s line %d: Missing argument.", filename, linenum);
670 if (!key_names_valid2(arg))
671 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100672 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000673 if (*activep && options->hostkeyalgorithms == NULL)
674 options->hostkeyalgorithms = xstrdup(arg);
675 break;
676
Damien Miller78928792000-04-12 20:17:38 +1000677 case oProtocol:
678 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000679 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000680 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000681 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000682 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000683 if (value == SSH_PROTO_UNKNOWN)
684 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100685 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000686 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
687 *intptr = value;
688 break;
689
Damien Miller95def091999-11-25 00:26:21 +1100690 case oLogLevel:
691 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000692 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000693 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100694 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000695 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100696 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100697 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100698 *intptr = (LogLevel) value;
699 break;
700
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000701 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100702 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000703 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100704 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000705 fatal("%.200s line %d: Missing port argument.",
706 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707 arg2 = strdelim(&s);
708 if (arg2 == NULL || *arg2 == '\0')
709 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000710 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100711
712 /* construct a string for parse_forward */
713 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
714
715 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000716 fatal("%.200s line %d: Bad forwarding specification.",
717 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100718
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000719 if (*activep) {
720 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100721 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000722 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100723 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000724 }
Damien Miller95def091999-11-25 00:26:21 +1100725 break;
726
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000727 case oDynamicForward:
728 arg = strdelim(&s);
729 if (!arg || *arg == '\0')
730 fatal("%.200s line %d: Missing port argument.",
731 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100732 memset(&fwd, '\0', sizeof(fwd));
733 fwd.connect_host = "socks";
734 fwd.listen_host = hpdelim(&arg);
735 if (fwd.listen_host == NULL ||
736 strlen(fwd.listen_host) >= NI_MAXHOST)
737 fatal("%.200s line %d: Bad forwarding specification.",
738 filename, linenum);
739 if (arg) {
740 fwd.listen_port = a2port(arg);
741 fwd.listen_host = cleanhostname(fwd.listen_host);
742 } else {
743 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000744 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100745 }
746 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000747 fatal("%.200s line %d: Badly formatted port number.",
748 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000749 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100750 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000751 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000752
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000753 case oClearAllForwardings:
754 intptr = &options->clear_forwardings;
755 goto parse_flag;
756
Damien Miller95def091999-11-25 00:26:21 +1100757 case oHost:
758 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000759 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000760 if (match_pattern(host, arg)) {
761 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100762 *activep = 1;
763 break;
764 }
Damien Millerbe484b52000-07-15 14:14:16 +1000765 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100766 return 0;
767
768 case oEscapeChar:
769 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000770 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000771 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100772 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000773 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000774 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
775 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000776 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000777 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000778 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000779 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100780 else {
781 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100782 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100783 /* NOTREACHED */
784 value = 0; /* Avoid compiler warning. */
785 }
786 if (*activep && *intptr == -1)
787 *intptr = value;
788 break;
789
Damien Miller20a8f972003-05-18 20:50:30 +1000790 case oAddressFamily:
791 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000792 if (!arg || *arg == '\0')
793 fatal("%s line %d: missing address family.",
794 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000795 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000796 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000797 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000798 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000799 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000800 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000801 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000802 else
803 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000804 if (*activep && *intptr == -1)
805 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000806 break;
807
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000808 case oEnableSSHKeysign:
809 intptr = &options->enable_ssh_keysign;
810 goto parse_flag;
811
Damien Millerbd394c32004-03-08 23:12:36 +1100812 case oIdentitiesOnly:
813 intptr = &options->identities_only;
814 goto parse_flag;
815
Damien Miller509b0102003-12-17 16:33:10 +1100816 case oServerAliveInterval:
817 intptr = &options->server_alive_interval;
818 goto parse_time;
819
820 case oServerAliveCountMax:
821 intptr = &options->server_alive_count_max;
822 goto parse_int;
823
Darren Tucker46bc0752004-05-02 22:11:30 +1000824 case oSendEnv:
825 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
826 if (strchr(arg, '=') != NULL)
827 fatal("%s line %d: Invalid environment name.",
828 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100829 if (!*activep)
830 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000831 if (options->num_send_env >= MAX_SEND_ENV)
832 fatal("%s line %d: too many send env.",
833 filename, linenum);
834 options->send_env[options->num_send_env++] =
835 xstrdup(arg);
836 }
837 break;
838
Damien Miller0e220db2004-06-15 10:34:08 +1000839 case oControlPath:
840 charptr = &options->control_path;
841 goto parse_string;
842
843 case oControlMaster:
844 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000845 arg = strdelim(&s);
846 if (!arg || *arg == '\0')
847 fatal("%.200s line %d: Missing ControlMaster argument.",
848 filename, linenum);
849 value = 0; /* To avoid compiler warning... */
850 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
851 value = SSHCTL_MASTER_YES;
852 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
853 value = SSHCTL_MASTER_NO;
854 else if (strcmp(arg, "auto") == 0)
855 value = SSHCTL_MASTER_AUTO;
856 else if (strcmp(arg, "ask") == 0)
857 value = SSHCTL_MASTER_ASK;
858 else if (strcmp(arg, "autoask") == 0)
859 value = SSHCTL_MASTER_AUTO_ASK;
860 else
861 fatal("%.200s line %d: Bad ControlMaster argument.",
862 filename, linenum);
863 if (*activep && *intptr == -1)
864 *intptr = value;
865 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000866
Damien Millere1776152005-03-01 21:47:37 +1100867 case oHashKnownHosts:
868 intptr = &options->hash_known_hosts;
869 goto parse_flag;
870
Damien Millerd27b9472005-12-13 19:29:02 +1100871 case oTunnel:
872 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100873 arg = strdelim(&s);
874 if (!arg || *arg == '\0')
875 fatal("%s line %d: Missing yes/point-to-point/"
876 "ethernet/no argument.", filename, linenum);
877 value = 0; /* silence compiler */
878 if (strcasecmp(arg, "ethernet") == 0)
879 value = SSH_TUNMODE_ETHERNET;
880 else if (strcasecmp(arg, "point-to-point") == 0)
881 value = SSH_TUNMODE_POINTOPOINT;
882 else if (strcasecmp(arg, "yes") == 0)
883 value = SSH_TUNMODE_DEFAULT;
884 else if (strcasecmp(arg, "no") == 0)
885 value = SSH_TUNMODE_NO;
886 else
887 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
888 "no argument: %s", filename, linenum, arg);
889 if (*activep)
890 *intptr = value;
891 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100892
893 case oTunnelDevice:
894 arg = strdelim(&s);
895 if (!arg || *arg == '\0')
896 fatal("%.200s line %d: Missing argument.", filename, linenum);
897 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100898 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100899 fatal("%.200s line %d: Bad tun device.", filename, linenum);
900 if (*activep) {
901 options->tun_local = value;
902 options->tun_remote = value2;
903 }
904 break;
905
906 case oLocalCommand:
907 charptr = &options->local_command;
908 goto parse_command;
909
910 case oPermitLocalCommand:
911 intptr = &options->permit_local_command;
912 goto parse_flag;
913
Ben Lindstrom4daea862002-06-09 20:04:02 +0000914 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000915 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000916 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000917 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000918
Damien Millerf9b3feb2003-05-16 11:38:32 +1000919 case oUnsupported:
920 error("%s line %d: Unsupported option \"%s\"",
921 filename, linenum, keyword);
922 return 0;
923
Damien Miller95def091999-11-25 00:26:21 +1100924 default:
925 fatal("process_config_line: Unimplemented opcode %d", opcode);
926 }
927
928 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000929 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000930 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000931 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000932 }
Damien Miller95def091999-11-25 00:26:21 +1100933 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934}
935
936
Damien Miller5428f641999-11-25 11:54:57 +1100937/*
938 * Reads the config file and modifies the options accordingly. Options
939 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000940 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100941 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000943int
Darren Tuckerfc959702004-07-17 16:12:08 +1000944read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000945 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946{
Damien Miller95def091999-11-25 00:26:21 +1100947 FILE *f;
948 char line[1024];
949 int active, linenum;
950 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller95def091999-11-25 00:26:21 +1100952 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000953 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000954 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
Damien Miller57a44762004-04-20 20:11:57 +1000956 if (checkperm) {
957 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000958
Damien Miller33793852004-06-15 10:27:55 +1000959 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000960 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000961 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000962 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000963 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000964 }
965
Damien Miller95def091999-11-25 00:26:21 +1100966 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Damien Miller5428f641999-11-25 11:54:57 +1100968 /*
969 * Mark that we are now processing the options. This flag is turned
970 * on/off by Host specifications.
971 */
Damien Miller95def091999-11-25 00:26:21 +1100972 active = 1;
973 linenum = 0;
974 while (fgets(line, sizeof(line), f)) {
975 /* Update line number counter. */
976 linenum++;
977 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
978 bad_options++;
979 }
980 fclose(f);
981 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000982 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100983 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000984 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985}
986
Damien Miller5428f641999-11-25 11:54:57 +1100987/*
988 * Initializes options to special values that indicate that they have not yet
989 * been set. Read_config_file will only set options with this value. Options
990 * are processed in the following order: command line, user config file,
991 * system config file. Last, fill_default_options is called.
992 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
Damien Miller4af51302000-04-16 11:18:38 +1000994void
Damien Miller95def091999-11-25 00:26:21 +1100995initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996{
Damien Miller95def091999-11-25 00:26:21 +1100997 memset(options, 'X', sizeof(*options));
998 options->forward_agent = -1;
999 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001000 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001001 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001002 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001003 options->gateway_ports = -1;
1004 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001005 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001006 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001007 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001008 options->gss_authentication = -1;
1009 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001010 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001011 options->kbd_interactive_authentication = -1;
1012 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001013 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001014 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001015 options->batch_mode = -1;
1016 options->check_host_ip = -1;
1017 options->strict_host_key_checking = -1;
1018 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001019 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001020 options->compression_level = -1;
1021 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001022 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001023 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001024 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001025 options->number_of_password_prompts = -1;
1026 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001027 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001028 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001029 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001030 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001031 options->num_identity_files = 0;
1032 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001033 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001034 options->proxy_command = NULL;
1035 options->user = NULL;
1036 options->escape_char = -1;
1037 options->system_hostfile = NULL;
1038 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001039 options->system_hostfile2 = NULL;
1040 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001041 options->num_local_forwards = 0;
1042 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001043 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001044 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001045 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001046 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001047 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001048 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001049 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001050 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001051 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001052 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001053 options->server_alive_interval = -1;
1054 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001055 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001056 options->control_path = NULL;
1057 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001058 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001059 options->tun_open = -1;
1060 options->tun_local = -1;
1061 options->tun_remote = -1;
1062 options->local_command = NULL;
1063 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064}
1065
Damien Miller5428f641999-11-25 11:54:57 +11001066/*
1067 * Called after processing other sources of option data, this fills those
1068 * options for which no value has been specified with their default values.
1069 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070
Damien Miller4af51302000-04-16 11:18:38 +10001071void
Damien Miller95def091999-11-25 00:26:21 +11001072fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001074 int len;
1075
Damien Miller95def091999-11-25 00:26:21 +11001076 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001077 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001078 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001079 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001080 if (options->forward_x11_trusted == -1)
1081 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001082 if (options->exit_on_forward_failure == -1)
1083 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001084 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001085 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001086 if (options->gateway_ports == -1)
1087 options->gateway_ports = 0;
1088 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001089 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001090 if (options->rsa_authentication == -1)
1091 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001092 if (options->pubkey_authentication == -1)
1093 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001094 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001095 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001096 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001097 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001098 if (options->gss_deleg_creds == -1)
1099 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001100 if (options->password_authentication == -1)
1101 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001102 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001103 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001104 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001105 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001106 if (options->hostbased_authentication == -1)
1107 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001108 if (options->batch_mode == -1)
1109 options->batch_mode = 0;
1110 if (options->check_host_ip == -1)
1111 options->check_host_ip = 1;
1112 if (options->strict_host_key_checking == -1)
1113 options->strict_host_key_checking = 2; /* 2 is default */
1114 if (options->compression == -1)
1115 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001116 if (options->tcp_keep_alive == -1)
1117 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001118 if (options->compression_level == -1)
1119 options->compression_level = 6;
1120 if (options->port == -1)
1121 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001122 if (options->address_family == -1)
1123 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001124 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001125 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001126 if (options->number_of_password_prompts == -1)
1127 options->number_of_password_prompts = 3;
1128 /* Selected in ssh_login(). */
1129 if (options->cipher == -1)
1130 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001131 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001132 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001133 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001134 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001135 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001136 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001137 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001138 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001139 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001140 xmalloc(len);
1141 snprintf(options->identity_files[options->num_identity_files++],
1142 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001143 }
1144 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001145 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1146 options->identity_files[options->num_identity_files] =
1147 xmalloc(len);
1148 snprintf(options->identity_files[options->num_identity_files++],
1149 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1150
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001151 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001152 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001153 xmalloc(len);
1154 snprintf(options->identity_files[options->num_identity_files++],
1155 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001156 }
Damien Millereba71ba2000-04-29 23:57:08 +10001157 }
Damien Miller95def091999-11-25 00:26:21 +11001158 if (options->escape_char == -1)
1159 options->escape_char = '~';
1160 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001161 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001162 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001163 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001164 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001165 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001166 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001167 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001168 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001169 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001170 if (options->clear_forwardings == 1)
1171 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001172 if (options->no_host_authentication_for_localhost == - 1)
1173 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001174 if (options->identities_only == -1)
1175 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001176 if (options->enable_ssh_keysign == -1)
1177 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001178 if (options->rekey_limit == -1)
1179 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001180 if (options->verify_host_key_dns == -1)
1181 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001182 if (options->server_alive_interval == -1)
1183 options->server_alive_interval = 0;
1184 if (options->server_alive_count_max == -1)
1185 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001186 if (options->control_master == -1)
1187 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001188 if (options->hash_known_hosts == -1)
1189 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001190 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001191 options->tun_open = SSH_TUNMODE_NO;
1192 if (options->tun_local == -1)
1193 options->tun_local = SSH_TUNID_ANY;
1194 if (options->tun_remote == -1)
1195 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001196 if (options->permit_local_command == -1)
1197 options->permit_local_command = 0;
1198 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001199 /* options->proxy_command should not be set by default */
1200 /* options->user will be set in the main program if appropriate */
1201 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001202 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001203 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001205
1206/*
1207 * parse_forward
1208 * parses a string containing a port forwarding specification of the form:
1209 * [listenhost:]listenport:connecthost:connectport
1210 * returns number of arguments parsed or zero on error
1211 */
1212int
1213parse_forward(Forward *fwd, const char *fwdspec)
1214{
1215 int i;
1216 char *p, *cp, *fwdarg[4];
1217
1218 memset(fwd, '\0', sizeof(*fwd));
1219
1220 cp = p = xstrdup(fwdspec);
1221
1222 /* skip leading spaces */
1223 while (*cp && isspace(*cp))
1224 cp++;
1225
1226 for (i = 0; i < 4; ++i)
1227 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1228 break;
1229
1230 /* Check for trailing garbage in 4-arg case*/
1231 if (cp != NULL)
1232 i = 0; /* failure */
1233
1234 switch (i) {
1235 case 3:
1236 fwd->listen_host = NULL;
1237 fwd->listen_port = a2port(fwdarg[0]);
1238 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1239 fwd->connect_port = a2port(fwdarg[2]);
1240 break;
1241
1242 case 4:
1243 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1244 fwd->listen_port = a2port(fwdarg[1]);
1245 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1246 fwd->connect_port = a2port(fwdarg[3]);
1247 break;
1248 default:
1249 i = 0; /* failure */
1250 }
1251
1252 xfree(p);
1253
1254 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1255 goto fail_free;
1256
1257 if (fwd->connect_host != NULL &&
1258 strlen(fwd->connect_host) >= NI_MAXHOST)
1259 goto fail_free;
1260
1261 return (i);
1262
1263 fail_free:
1264 if (fwd->connect_host != NULL)
1265 xfree(fwd->connect_host);
1266 if (fwd->listen_host != NULL)
1267 xfree(fwd->listen_host);
1268 return (0);
1269}