blob: 432b80ed0d5cbdde884fe2628501b7a5843de70a [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: readconf.c,v 1.157 2006/07/22 20:48:23 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 Millere3476ed2006-07-24 14:13:33 +100028#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100029#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110030
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100033#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "cipher.h"
35#include "pathnames.h"
36#include "log.h"
37#include "readconf.h"
38#include "match.h"
39#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000040#include "kex.h"
41#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43/* Format of the configuration file:
44
45 # Configuration data is parsed as follows:
46 # 1. command line options
47 # 2. user-specific file
48 # 3. system-wide file
49 # Any configuration value is only changed the first time it is set.
50 # Thus, host-specific definitions should be at the beginning of the
51 # configuration file, and defaults at the end.
52
53 # Host-specific declarations. These may override anything above. A single
54 # host may match multiple declarations; these are processed in the order
55 # that they are given in.
56
57 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000058 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
60 Host fake.com
61 HostName another.host.name.real.org
62 User blaah
63 Port 34289
64 ForwardX11 no
65 ForwardAgent no
66
67 Host books.com
68 RemoteForward 9999 shadows.cs.hut.fi:9999
69 Cipher 3des
70
71 Host fascist.blob.com
72 Port 23123
73 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074 PasswordAuthentication no
75
76 Host puukko.hut.fi
77 User t35124p
78 ProxyCommand ssh-proxy %h %p
79
80 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000081 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
83 Host *.su
84 Cipher none
85 PasswordAuthentication no
86
Damien Millerd27b9472005-12-13 19:29:02 +110087 Host vpn.fake.com
88 Tunnel yes
89 TunnelDevice 3
90
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091 # Defaults for various options
92 Host *
93 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110094 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095 PasswordAuthentication yes
96 RSAAuthentication yes
97 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110099 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100 IdentityFile ~/.ssh/identity
101 Port 22
102 EscapeChar ~
103
104*/
105
106/* Keyword tokens. */
107
Damien Miller95def091999-11-25 00:26:21 +1100108typedef enum {
109 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000110 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000111 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000112 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000113 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100114 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
115 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
116 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
117 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100118 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000119 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100120 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000121 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000122 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000123 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000124 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000125 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000126 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100127 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100128 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100129 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000130 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131} OpCodes;
132
133/* Textual representations of the tokens. */
134
Damien Miller95def091999-11-25 00:26:21 +1100135static struct {
136 const char *name;
137 OpCodes opcode;
138} keywords[] = {
139 { "forwardagent", oForwardAgent },
140 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000141 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000142 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000143 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100144 { "gatewayports", oGatewayPorts },
145 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000146 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100147 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100148 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
149 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100150 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100151 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000152 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000153 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000154 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000155 { "challengeresponseauthentication", oChallengeResponseAuthentication },
156 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
157 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000158 { "kerberosauthentication", oUnsupported },
159 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000160 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000161#if defined(GSSAPI)
162 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000163 { "gssapidelegatecredentials", oGssDelegateCreds },
164#else
165 { "gssapiauthentication", oUnsupported },
166 { "gssapidelegatecredentials", oUnsupported },
167#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000168 { "fallbacktorsh", oDeprecated },
169 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100170 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100171 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100172 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000174 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100175 { "proxycommand", oProxyCommand },
176 { "port", oPort },
177 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000178 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000179 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000180 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100181 { "remoteforward", oRemoteForward },
182 { "localforward", oLocalForward },
183 { "user", oUser },
184 { "host", oHost },
185 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100186 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000187 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000188 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000189 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100190 { "connectionattempts", oConnectionAttempts },
191 { "batchmode", oBatchMode },
192 { "checkhostip", oCheckHostIP },
193 { "stricthostkeychecking", oStrictHostKeyChecking },
194 { "compression", oCompression },
195 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100196 { "tcpkeepalive", oTCPKeepAlive },
197 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100198 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100199 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000200 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000201 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000202 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000203 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000204#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000205 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000206#else
207 { "smartcarddevice", oUnsupported },
208#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100209 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000210 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000211 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100212 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000213 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000214 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000215 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100216 { "serveraliveinterval", oServerAliveInterval },
217 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000218 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000219 { "controlpath", oControlPath },
220 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100221 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100222 { "tunnel", oTunnel },
223 { "tunneldevice", oTunnelDevice },
224 { "localcommand", oLocalCommand },
225 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000226 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100227};
228
Damien Miller5428f641999-11-25 11:54:57 +1100229/*
230 * Adds a local TCP/IP port forward to options. Never returns if there is an
231 * error.
232 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Miller4af51302000-04-16 11:18:38 +1000234void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100235add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236{
Damien Miller95def091999-11-25 00:26:21 +1100237 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000238#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100239 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100240 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000241 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100242#endif
Damien Miller95def091999-11-25 00:26:21 +1100243 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
244 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
245 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100246
247 fwd->listen_host = (newfwd->listen_host == NULL) ?
248 NULL : xstrdup(newfwd->listen_host);
249 fwd->listen_port = newfwd->listen_port;
250 fwd->connect_host = xstrdup(newfwd->connect_host);
251 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}
253
Damien Miller5428f641999-11-25 11:54:57 +1100254/*
255 * Adds a remote TCP/IP port forward to options. Never returns if there is
256 * an error.
257 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller4af51302000-04-16 11:18:38 +1000259void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100260add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261{
Damien Miller95def091999-11-25 00:26:21 +1100262 Forward *fwd;
263 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
264 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100265 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100266 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100267
268 fwd->listen_host = (newfwd->listen_host == NULL) ?
269 NULL : xstrdup(newfwd->listen_host);
270 fwd->listen_port = newfwd->listen_port;
271 fwd->connect_host = xstrdup(newfwd->connect_host);
272 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
274
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000275static void
276clear_forwardings(Options *options)
277{
278 int i;
279
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100281 if (options->local_forwards[i].listen_host != NULL)
282 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 xfree(options->local_forwards[i].connect_host);
284 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000285 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100286 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100287 if (options->remote_forwards[i].listen_host != NULL)
288 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100289 xfree(options->remote_forwards[i].connect_host);
290 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000291 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100292 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000293}
294
Damien Miller5428f641999-11-25 11:54:57 +1100295/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000296 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100297 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Miller4af51302000-04-16 11:18:38 +1000299static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100300parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000302 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100305 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100306 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000308 error("%s: line %d: Bad configuration option: %s",
309 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100310 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311}
312
Damien Miller5428f641999-11-25 11:54:57 +1100313/*
314 * Processes a single option line as used in the configuration files. This
315 * only sets those values that have not already been set.
316 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100317#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Miller2ccf6611999-11-15 15:25:10 +1100319int
320process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100321 char *line, const char *filename, int linenum,
322 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100324 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100325 int opcode, *intptr, value, value2, scale;
326 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100327 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100328 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Damien Millerc652cac2003-05-14 13:40:54 +1000330 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100331 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000332 if (strchr(WHITESPACE, line[len]) == NULL)
333 break;
334 line[len] = '\0';
335 }
336
Damien Millerbe484b52000-07-15 14:14:16 +1000337 s = line;
338 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100339 if ((keyword = strdelim(&s)) == NULL)
340 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000341 /* Ignore leading whitespace. */
342 if (*keyword == '\0')
343 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000344 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100345 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Miller37023962000-07-11 17:31:38 +1000347 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348
Damien Miller95def091999-11-25 00:26:21 +1100349 switch (opcode) {
350 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100351 /* don't panic, but count bad options */
352 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100353 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000354 case oConnectTimeout:
355 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100356parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000357 arg = strdelim(&s);
358 if (!arg || *arg == '\0')
359 fatal("%s line %d: missing time value.",
360 filename, linenum);
361 if ((value = convtime(arg)) == -1)
362 fatal("%s line %d: invalid time value.",
363 filename, linenum);
364 if (*intptr == -1)
365 *intptr = value;
366 break;
367
Damien Miller95def091999-11-25 00:26:21 +1100368 case oForwardAgent:
369 intptr = &options->forward_agent;
370parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000371 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000372 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100373 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
374 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000375 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100376 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000377 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100378 value = 0;
379 else
380 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
381 if (*activep && *intptr == -1)
382 *intptr = value;
383 break;
384
385 case oForwardX11:
386 intptr = &options->forward_x11;
387 goto parse_flag;
388
Darren Tucker0a118da2003-10-15 15:54:32 +1000389 case oForwardX11Trusted:
390 intptr = &options->forward_x11_trusted;
391 goto parse_flag;
392
Damien Miller95def091999-11-25 00:26:21 +1100393 case oGatewayPorts:
394 intptr = &options->gateway_ports;
395 goto parse_flag;
396
Darren Tuckere7d4b192006-07-12 22:17:10 +1000397 case oExitOnForwardFailure:
398 intptr = &options->exit_on_forward_failure;
399 goto parse_flag;
400
Damien Miller95def091999-11-25 00:26:21 +1100401 case oUsePrivilegedPort:
402 intptr = &options->use_privileged_port;
403 goto parse_flag;
404
Damien Miller95def091999-11-25 00:26:21 +1100405 case oPasswordAuthentication:
406 intptr = &options->password_authentication;
407 goto parse_flag;
408
Damien Miller874d77b2000-10-14 16:23:11 +1100409 case oKbdInteractiveAuthentication:
410 intptr = &options->kbd_interactive_authentication;
411 goto parse_flag;
412
413 case oKbdInteractiveDevices:
414 charptr = &options->kbd_interactive_devices;
415 goto parse_string;
416
Damien Miller0bc1bd82000-11-13 22:57:25 +1100417 case oPubkeyAuthentication:
418 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000419 goto parse_flag;
420
Damien Miller95def091999-11-25 00:26:21 +1100421 case oRSAAuthentication:
422 intptr = &options->rsa_authentication;
423 goto parse_flag;
424
425 case oRhostsRSAAuthentication:
426 intptr = &options->rhosts_rsa_authentication;
427 goto parse_flag;
428
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000429 case oHostbasedAuthentication:
430 intptr = &options->hostbased_authentication;
431 goto parse_flag;
432
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000433 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000434 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100435 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000436
Darren Tucker0efd1552003-08-26 11:49:55 +1000437 case oGssAuthentication:
438 intptr = &options->gss_authentication;
439 goto parse_flag;
440
441 case oGssDelegateCreds:
442 intptr = &options->gss_deleg_creds;
443 goto parse_flag;
444
Damien Miller95def091999-11-25 00:26:21 +1100445 case oBatchMode:
446 intptr = &options->batch_mode;
447 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448
Damien Miller95def091999-11-25 00:26:21 +1100449 case oCheckHostIP:
450 intptr = &options->check_host_ip;
451 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452
Damien Miller37876e92003-05-15 10:19:46 +1000453 case oVerifyHostKeyDNS:
454 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100455 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000456
Damien Miller95def091999-11-25 00:26:21 +1100457 case oStrictHostKeyChecking:
458 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100459parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000460 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000461 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000462 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100463 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100464 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000465 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100466 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000467 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100468 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000469 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100470 value = 2;
471 else
472 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
473 if (*activep && *intptr == -1)
474 *intptr = value;
475 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476
Damien Miller95def091999-11-25 00:26:21 +1100477 case oCompression:
478 intptr = &options->compression;
479 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
Damien Miller12c150e2003-12-17 16:31:10 +1100481 case oTCPKeepAlive:
482 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100483 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000485 case oNoHostAuthenticationForLocalhost:
486 intptr = &options->no_host_authentication_for_localhost;
487 goto parse_flag;
488
Damien Miller95def091999-11-25 00:26:21 +1100489 case oNumberOfPasswordPrompts:
490 intptr = &options->number_of_password_prompts;
491 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492
Damien Miller95def091999-11-25 00:26:21 +1100493 case oCompressionLevel:
494 intptr = &options->compression_level;
495 goto parse_int;
496
Damien Millera5539d22003-04-09 20:50:06 +1000497 case oRekeyLimit:
498 intptr = &options->rekey_limit;
499 arg = strdelim(&s);
500 if (!arg || *arg == '\0')
501 fatal("%.200s line %d: Missing argument.", filename, linenum);
502 if (arg[0] < '0' || arg[0] > '9')
503 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100504 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000505 if (arg == endofnumber)
506 fatal("%.200s line %d: Bad number.", filename, linenum);
507 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100508 case '\0':
509 scale = 1;
510 break;
Damien Millera5539d22003-04-09 20:50:06 +1000511 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100512 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000513 break;
514 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100515 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000516 break;
517 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100518 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000519 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100520 default:
521 fatal("%.200s line %d: Invalid RekeyLimit suffix",
522 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000523 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100524 val64 *= scale;
525 /* detect integer wrap and too-large limits */
526 if ((val64 / scale) != orig || val64 > INT_MAX)
527 fatal("%.200s line %d: RekeyLimit too large",
528 filename, linenum);
529 if (val64 < 16)
530 fatal("%.200s line %d: RekeyLimit too small",
531 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000532 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100533 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000534 break;
535
Damien Miller95def091999-11-25 00:26:21 +1100536 case oIdentityFile:
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) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100541 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000542 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100543 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100544 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100545 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000546 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000547 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100548 }
549 break;
550
Damien Millerd3a18572000-06-07 19:55:44 +1000551 case oXAuthLocation:
552 charptr=&options->xauth_location;
553 goto parse_string;
554
Damien Miller95def091999-11-25 00:26:21 +1100555 case oUser:
556 charptr = &options->user;
557parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000558 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000559 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100560 fatal("%.200s line %d: Missing argument.", filename, linenum);
561 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000562 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100563 break;
564
565 case oGlobalKnownHostsFile:
566 charptr = &options->system_hostfile;
567 goto parse_string;
568
569 case oUserKnownHostsFile:
570 charptr = &options->user_hostfile;
571 goto parse_string;
572
Damien Millereba71ba2000-04-29 23:57:08 +1000573 case oGlobalKnownHostsFile2:
574 charptr = &options->system_hostfile2;
575 goto parse_string;
576
577 case oUserKnownHostsFile2:
578 charptr = &options->user_hostfile2;
579 goto parse_string;
580
Damien Miller95def091999-11-25 00:26:21 +1100581 case oHostName:
582 charptr = &options->hostname;
583 goto parse_string;
584
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000585 case oHostKeyAlias:
586 charptr = &options->host_key_alias;
587 goto parse_string;
588
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000589 case oPreferredAuthentications:
590 charptr = &options->preferred_authentications;
591 goto parse_string;
592
Ben Lindstrome0f88042001-04-30 13:06:24 +0000593 case oBindAddress:
594 charptr = &options->bind_address;
595 goto parse_string;
596
Ben Lindstromae996bf2001-08-06 21:27:53 +0000597 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000598 charptr = &options->smartcard_device;
599 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000600
Damien Miller95def091999-11-25 00:26:21 +1100601 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100602 charptr = &options->proxy_command;
603parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000604 if (s == NULL)
605 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100606 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100607 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100608 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100609 return 0;
610
611 case oPort:
612 intptr = &options->port;
613parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000614 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000615 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100616 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000617 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100618 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100619
620 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000621 value = strtol(arg, &endofnumber, 0);
622 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100623 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100624 if (*activep && *intptr == -1)
625 *intptr = value;
626 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627
Damien Miller95def091999-11-25 00:26:21 +1100628 case oConnectionAttempts:
629 intptr = &options->connection_attempts;
630 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100631
Damien Miller95def091999-11-25 00:26:21 +1100632 case oCipher:
633 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000634 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000635 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000636 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000637 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100638 if (value == -1)
639 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100640 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100641 if (*activep && *intptr == -1)
642 *intptr = value;
643 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644
Damien Miller78928792000-04-12 20:17:38 +1000645 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000646 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000647 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000648 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000649 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000650 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100651 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000652 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000653 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000654 break;
655
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000656 case oMacs:
657 arg = strdelim(&s);
658 if (!arg || *arg == '\0')
659 fatal("%.200s line %d: Missing argument.", filename, linenum);
660 if (!mac_valid(arg))
661 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100662 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000663 if (*activep && options->macs == NULL)
664 options->macs = xstrdup(arg);
665 break;
666
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000667 case oHostKeyAlgorithms:
668 arg = strdelim(&s);
669 if (!arg || *arg == '\0')
670 fatal("%.200s line %d: Missing argument.", filename, linenum);
671 if (!key_names_valid2(arg))
672 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100673 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000674 if (*activep && options->hostkeyalgorithms == NULL)
675 options->hostkeyalgorithms = xstrdup(arg);
676 break;
677
Damien Miller78928792000-04-12 20:17:38 +1000678 case oProtocol:
679 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000680 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000681 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000682 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000683 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000684 if (value == SSH_PROTO_UNKNOWN)
685 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100686 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000687 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
688 *intptr = value;
689 break;
690
Damien Miller95def091999-11-25 00:26:21 +1100691 case oLogLevel:
692 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000693 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000694 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100695 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000696 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100697 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100698 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100699 *intptr = (LogLevel) value;
700 break;
701
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000702 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100703 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000704 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100705 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000706 fatal("%.200s line %d: Missing port argument.",
707 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100708 arg2 = strdelim(&s);
709 if (arg2 == NULL || *arg2 == '\0')
710 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000711 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100712
713 /* construct a string for parse_forward */
714 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
715
716 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000717 fatal("%.200s line %d: Bad forwarding specification.",
718 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100719
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000720 if (*activep) {
721 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100722 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000723 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100724 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000725 }
Damien Miller95def091999-11-25 00:26:21 +1100726 break;
727
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000728 case oDynamicForward:
729 arg = strdelim(&s);
730 if (!arg || *arg == '\0')
731 fatal("%.200s line %d: Missing port argument.",
732 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100733 memset(&fwd, '\0', sizeof(fwd));
734 fwd.connect_host = "socks";
735 fwd.listen_host = hpdelim(&arg);
736 if (fwd.listen_host == NULL ||
737 strlen(fwd.listen_host) >= NI_MAXHOST)
738 fatal("%.200s line %d: Bad forwarding specification.",
739 filename, linenum);
740 if (arg) {
741 fwd.listen_port = a2port(arg);
742 fwd.listen_host = cleanhostname(fwd.listen_host);
743 } else {
744 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000745 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100746 }
747 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000748 fatal("%.200s line %d: Badly formatted port number.",
749 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000750 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100751 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000752 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000753
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000754 case oClearAllForwardings:
755 intptr = &options->clear_forwardings;
756 goto parse_flag;
757
Damien Miller95def091999-11-25 00:26:21 +1100758 case oHost:
759 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000760 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000761 if (match_pattern(host, arg)) {
762 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100763 *activep = 1;
764 break;
765 }
Damien Millerbe484b52000-07-15 14:14:16 +1000766 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100767 return 0;
768
769 case oEscapeChar:
770 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000771 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000772 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100773 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000774 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000775 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
776 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000777 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000778 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000779 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000780 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100781 else {
782 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100783 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100784 /* NOTREACHED */
785 value = 0; /* Avoid compiler warning. */
786 }
787 if (*activep && *intptr == -1)
788 *intptr = value;
789 break;
790
Damien Miller20a8f972003-05-18 20:50:30 +1000791 case oAddressFamily:
792 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000793 if (!arg || *arg == '\0')
794 fatal("%s line %d: missing address family.",
795 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000796 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000797 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000798 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000799 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000800 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000801 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000802 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000803 else
804 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000805 if (*activep && *intptr == -1)
806 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000807 break;
808
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000809 case oEnableSSHKeysign:
810 intptr = &options->enable_ssh_keysign;
811 goto parse_flag;
812
Damien Millerbd394c32004-03-08 23:12:36 +1100813 case oIdentitiesOnly:
814 intptr = &options->identities_only;
815 goto parse_flag;
816
Damien Miller509b0102003-12-17 16:33:10 +1100817 case oServerAliveInterval:
818 intptr = &options->server_alive_interval;
819 goto parse_time;
820
821 case oServerAliveCountMax:
822 intptr = &options->server_alive_count_max;
823 goto parse_int;
824
Darren Tucker46bc0752004-05-02 22:11:30 +1000825 case oSendEnv:
826 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
827 if (strchr(arg, '=') != NULL)
828 fatal("%s line %d: Invalid environment name.",
829 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100830 if (!*activep)
831 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000832 if (options->num_send_env >= MAX_SEND_ENV)
833 fatal("%s line %d: too many send env.",
834 filename, linenum);
835 options->send_env[options->num_send_env++] =
836 xstrdup(arg);
837 }
838 break;
839
Damien Miller0e220db2004-06-15 10:34:08 +1000840 case oControlPath:
841 charptr = &options->control_path;
842 goto parse_string;
843
844 case oControlMaster:
845 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000846 arg = strdelim(&s);
847 if (!arg || *arg == '\0')
848 fatal("%.200s line %d: Missing ControlMaster argument.",
849 filename, linenum);
850 value = 0; /* To avoid compiler warning... */
851 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
852 value = SSHCTL_MASTER_YES;
853 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
854 value = SSHCTL_MASTER_NO;
855 else if (strcmp(arg, "auto") == 0)
856 value = SSHCTL_MASTER_AUTO;
857 else if (strcmp(arg, "ask") == 0)
858 value = SSHCTL_MASTER_ASK;
859 else if (strcmp(arg, "autoask") == 0)
860 value = SSHCTL_MASTER_AUTO_ASK;
861 else
862 fatal("%.200s line %d: Bad ControlMaster argument.",
863 filename, linenum);
864 if (*activep && *intptr == -1)
865 *intptr = value;
866 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000867
Damien Millere1776152005-03-01 21:47:37 +1100868 case oHashKnownHosts:
869 intptr = &options->hash_known_hosts;
870 goto parse_flag;
871
Damien Millerd27b9472005-12-13 19:29:02 +1100872 case oTunnel:
873 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100874 arg = strdelim(&s);
875 if (!arg || *arg == '\0')
876 fatal("%s line %d: Missing yes/point-to-point/"
877 "ethernet/no argument.", filename, linenum);
878 value = 0; /* silence compiler */
879 if (strcasecmp(arg, "ethernet") == 0)
880 value = SSH_TUNMODE_ETHERNET;
881 else if (strcasecmp(arg, "point-to-point") == 0)
882 value = SSH_TUNMODE_POINTOPOINT;
883 else if (strcasecmp(arg, "yes") == 0)
884 value = SSH_TUNMODE_DEFAULT;
885 else if (strcasecmp(arg, "no") == 0)
886 value = SSH_TUNMODE_NO;
887 else
888 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
889 "no argument: %s", filename, linenum, arg);
890 if (*activep)
891 *intptr = value;
892 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100893
894 case oTunnelDevice:
895 arg = strdelim(&s);
896 if (!arg || *arg == '\0')
897 fatal("%.200s line %d: Missing argument.", filename, linenum);
898 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100899 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100900 fatal("%.200s line %d: Bad tun device.", filename, linenum);
901 if (*activep) {
902 options->tun_local = value;
903 options->tun_remote = value2;
904 }
905 break;
906
907 case oLocalCommand:
908 charptr = &options->local_command;
909 goto parse_command;
910
911 case oPermitLocalCommand:
912 intptr = &options->permit_local_command;
913 goto parse_flag;
914
Ben Lindstrom4daea862002-06-09 20:04:02 +0000915 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000916 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000917 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000918 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000919
Damien Millerf9b3feb2003-05-16 11:38:32 +1000920 case oUnsupported:
921 error("%s line %d: Unsupported option \"%s\"",
922 filename, linenum, keyword);
923 return 0;
924
Damien Miller95def091999-11-25 00:26:21 +1100925 default:
926 fatal("process_config_line: Unimplemented opcode %d", opcode);
927 }
928
929 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000930 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000931 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000932 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000933 }
Damien Miller95def091999-11-25 00:26:21 +1100934 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935}
936
937
Damien Miller5428f641999-11-25 11:54:57 +1100938/*
939 * Reads the config file and modifies the options accordingly. Options
940 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000941 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100942 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000944int
Darren Tuckerfc959702004-07-17 16:12:08 +1000945read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000946 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947{
Damien Miller95def091999-11-25 00:26:21 +1100948 FILE *f;
949 char line[1024];
950 int active, linenum;
951 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Damien Miller95def091999-11-25 00:26:21 +1100953 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000954 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000955 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Miller57a44762004-04-20 20:11:57 +1000957 if (checkperm) {
958 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000959
Damien Miller33793852004-06-15 10:27:55 +1000960 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000961 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000962 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000963 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000964 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000965 }
966
Damien Miller95def091999-11-25 00:26:21 +1100967 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller5428f641999-11-25 11:54:57 +1100969 /*
970 * Mark that we are now processing the options. This flag is turned
971 * on/off by Host specifications.
972 */
Damien Miller95def091999-11-25 00:26:21 +1100973 active = 1;
974 linenum = 0;
975 while (fgets(line, sizeof(line), f)) {
976 /* Update line number counter. */
977 linenum++;
978 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
979 bad_options++;
980 }
981 fclose(f);
982 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000983 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100984 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000985 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986}
987
Damien Miller5428f641999-11-25 11:54:57 +1100988/*
989 * Initializes options to special values that indicate that they have not yet
990 * been set. Read_config_file will only set options with this value. Options
991 * are processed in the following order: command line, user config file,
992 * system config file. Last, fill_default_options is called.
993 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller4af51302000-04-16 11:18:38 +1000995void
Damien Miller95def091999-11-25 00:26:21 +1100996initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997{
Damien Miller95def091999-11-25 00:26:21 +1100998 memset(options, 'X', sizeof(*options));
999 options->forward_agent = -1;
1000 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001001 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001002 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001003 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001004 options->gateway_ports = -1;
1005 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001006 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001007 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001008 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001009 options->gss_authentication = -1;
1010 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001011 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001012 options->kbd_interactive_authentication = -1;
1013 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001014 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001015 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001016 options->batch_mode = -1;
1017 options->check_host_ip = -1;
1018 options->strict_host_key_checking = -1;
1019 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001020 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001021 options->compression_level = -1;
1022 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001023 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001024 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001025 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001026 options->number_of_password_prompts = -1;
1027 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001028 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001029 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001030 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001031 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001032 options->num_identity_files = 0;
1033 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001034 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001035 options->proxy_command = NULL;
1036 options->user = NULL;
1037 options->escape_char = -1;
1038 options->system_hostfile = NULL;
1039 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001040 options->system_hostfile2 = NULL;
1041 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001042 options->num_local_forwards = 0;
1043 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001044 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001045 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001046 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001047 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001048 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001049 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001050 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001051 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001052 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001053 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001054 options->server_alive_interval = -1;
1055 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001056 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001057 options->control_path = NULL;
1058 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001059 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001060 options->tun_open = -1;
1061 options->tun_local = -1;
1062 options->tun_remote = -1;
1063 options->local_command = NULL;
1064 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065}
1066
Damien Miller5428f641999-11-25 11:54:57 +11001067/*
1068 * Called after processing other sources of option data, this fills those
1069 * options for which no value has been specified with their default values.
1070 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071
Damien Miller4af51302000-04-16 11:18:38 +10001072void
Damien Miller95def091999-11-25 00:26:21 +11001073fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001075 int len;
1076
Damien Miller95def091999-11-25 00:26:21 +11001077 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001078 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001079 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001080 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001081 if (options->forward_x11_trusted == -1)
1082 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001083 if (options->exit_on_forward_failure == -1)
1084 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001085 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001086 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001087 if (options->gateway_ports == -1)
1088 options->gateway_ports = 0;
1089 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001090 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001091 if (options->rsa_authentication == -1)
1092 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001093 if (options->pubkey_authentication == -1)
1094 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001095 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001096 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001097 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001098 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001099 if (options->gss_deleg_creds == -1)
1100 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001101 if (options->password_authentication == -1)
1102 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001103 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001104 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001105 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001106 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001107 if (options->hostbased_authentication == -1)
1108 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001109 if (options->batch_mode == -1)
1110 options->batch_mode = 0;
1111 if (options->check_host_ip == -1)
1112 options->check_host_ip = 1;
1113 if (options->strict_host_key_checking == -1)
1114 options->strict_host_key_checking = 2; /* 2 is default */
1115 if (options->compression == -1)
1116 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001117 if (options->tcp_keep_alive == -1)
1118 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001119 if (options->compression_level == -1)
1120 options->compression_level = 6;
1121 if (options->port == -1)
1122 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001123 if (options->address_family == -1)
1124 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001125 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001126 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001127 if (options->number_of_password_prompts == -1)
1128 options->number_of_password_prompts = 3;
1129 /* Selected in ssh_login(). */
1130 if (options->cipher == -1)
1131 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001132 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001133 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001134 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001135 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001136 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001137 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001138 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001139 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001140 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001141 xmalloc(len);
1142 snprintf(options->identity_files[options->num_identity_files++],
1143 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001144 }
1145 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001146 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1147 options->identity_files[options->num_identity_files] =
1148 xmalloc(len);
1149 snprintf(options->identity_files[options->num_identity_files++],
1150 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1151
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001152 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001153 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001154 xmalloc(len);
1155 snprintf(options->identity_files[options->num_identity_files++],
1156 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001157 }
Damien Millereba71ba2000-04-29 23:57:08 +10001158 }
Damien Miller95def091999-11-25 00:26:21 +11001159 if (options->escape_char == -1)
1160 options->escape_char = '~';
1161 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001162 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001163 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001164 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001165 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001166 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001167 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001168 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001169 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001170 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001171 if (options->clear_forwardings == 1)
1172 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001173 if (options->no_host_authentication_for_localhost == - 1)
1174 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001175 if (options->identities_only == -1)
1176 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001177 if (options->enable_ssh_keysign == -1)
1178 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001179 if (options->rekey_limit == -1)
1180 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001181 if (options->verify_host_key_dns == -1)
1182 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001183 if (options->server_alive_interval == -1)
1184 options->server_alive_interval = 0;
1185 if (options->server_alive_count_max == -1)
1186 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001187 if (options->control_master == -1)
1188 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001189 if (options->hash_known_hosts == -1)
1190 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001191 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001192 options->tun_open = SSH_TUNMODE_NO;
1193 if (options->tun_local == -1)
1194 options->tun_local = SSH_TUNID_ANY;
1195 if (options->tun_remote == -1)
1196 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001197 if (options->permit_local_command == -1)
1198 options->permit_local_command = 0;
1199 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001200 /* options->proxy_command should not be set by default */
1201 /* options->user will be set in the main program if appropriate */
1202 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001203 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001204 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001206
1207/*
1208 * parse_forward
1209 * parses a string containing a port forwarding specification of the form:
1210 * [listenhost:]listenport:connecthost:connectport
1211 * returns number of arguments parsed or zero on error
1212 */
1213int
1214parse_forward(Forward *fwd, const char *fwdspec)
1215{
1216 int i;
1217 char *p, *cp, *fwdarg[4];
1218
1219 memset(fwd, '\0', sizeof(*fwd));
1220
1221 cp = p = xstrdup(fwdspec);
1222
1223 /* skip leading spaces */
1224 while (*cp && isspace(*cp))
1225 cp++;
1226
1227 for (i = 0; i < 4; ++i)
1228 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1229 break;
1230
1231 /* Check for trailing garbage in 4-arg case*/
1232 if (cp != NULL)
1233 i = 0; /* failure */
1234
1235 switch (i) {
1236 case 3:
1237 fwd->listen_host = NULL;
1238 fwd->listen_port = a2port(fwdarg[0]);
1239 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1240 fwd->connect_port = a2port(fwdarg[2]);
1241 break;
1242
1243 case 4:
1244 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1245 fwd->listen_port = a2port(fwdarg[1]);
1246 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1247 fwd->connect_port = a2port(fwdarg[3]);
1248 break;
1249 default:
1250 i = 0; /* failure */
1251 }
1252
1253 xfree(p);
1254
1255 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1256 goto fail_free;
1257
1258 if (fwd->connect_host != NULL &&
1259 strlen(fwd->connect_host) >= NI_MAXHOST)
1260 goto fail_free;
1261
1262 return (i);
1263
1264 fail_free:
1265 if (fwd->connect_host != NULL)
1266 xfree(fwd->connect_host);
1267 if (fwd->listen_host != NULL)
1268 xfree(fwd->listen_host);
1269 return (0);
1270}