blob: 6fe372796ca27c3b2cb75d68617659bb424a69a3 [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 Millerb8fe89c2006-07-24 14:51:00 +100025#include <netdb.h>
Damien Millere3476ed2006-07-24 14:13:33 +100026#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100027#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110028
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "xmalloc.h"
Damien Miller78928792000-04-12 20:17:38 +100031#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000032#include "cipher.h"
33#include "pathnames.h"
34#include "log.h"
35#include "readconf.h"
36#include "match.h"
37#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000038#include "kex.h"
39#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
41/* Format of the configuration file:
42
43 # Configuration data is parsed as follows:
44 # 1. command line options
45 # 2. user-specific file
46 # 3. system-wide file
47 # Any configuration value is only changed the first time it is set.
48 # Thus, host-specific definitions should be at the beginning of the
49 # configuration file, and defaults at the end.
50
51 # Host-specific declarations. These may override anything above. A single
52 # host may match multiple declarations; these are processed in the order
53 # that they are given in.
54
55 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000056 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
58 Host fake.com
59 HostName another.host.name.real.org
60 User blaah
61 Port 34289
62 ForwardX11 no
63 ForwardAgent no
64
65 Host books.com
66 RemoteForward 9999 shadows.cs.hut.fi:9999
67 Cipher 3des
68
69 Host fascist.blob.com
70 Port 23123
71 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072 PasswordAuthentication no
73
74 Host puukko.hut.fi
75 User t35124p
76 ProxyCommand ssh-proxy %h %p
77
78 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000079 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
81 Host *.su
82 Cipher none
83 PasswordAuthentication no
84
Damien Millerd27b9472005-12-13 19:29:02 +110085 Host vpn.fake.com
86 Tunnel yes
87 TunnelDevice 3
88
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089 # Defaults for various options
90 Host *
91 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110092 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093 PasswordAuthentication yes
94 RSAAuthentication yes
95 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +110097 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 IdentityFile ~/.ssh/identity
99 Port 22
100 EscapeChar ~
101
102*/
103
104/* Keyword tokens. */
105
Damien Miller95def091999-11-25 00:26:21 +1100106typedef enum {
107 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000108 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000109 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000110 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000111 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100112 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
113 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
114 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
115 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100116 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000117 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100118 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000119 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000120 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000121 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000122 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000123 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000124 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100125 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100126 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100127 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000128 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129} OpCodes;
130
131/* Textual representations of the tokens. */
132
Damien Miller95def091999-11-25 00:26:21 +1100133static struct {
134 const char *name;
135 OpCodes opcode;
136} keywords[] = {
137 { "forwardagent", oForwardAgent },
138 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000139 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000140 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000141 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100142 { "gatewayports", oGatewayPorts },
143 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000144 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100145 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100146 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
147 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100148 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100149 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000150 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000151 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000152 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000153 { "challengeresponseauthentication", oChallengeResponseAuthentication },
154 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
155 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000156 { "kerberosauthentication", oUnsupported },
157 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000158 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000159#if defined(GSSAPI)
160 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000161 { "gssapidelegatecredentials", oGssDelegateCreds },
162#else
163 { "gssapiauthentication", oUnsupported },
164 { "gssapidelegatecredentials", oUnsupported },
165#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000166 { "fallbacktorsh", oDeprecated },
167 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100168 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100169 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100170 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100171 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000172 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "proxycommand", oProxyCommand },
174 { "port", oPort },
175 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000176 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000177 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000178 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "remoteforward", oRemoteForward },
180 { "localforward", oLocalForward },
181 { "user", oUser },
182 { "host", oHost },
183 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100184 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000185 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000186 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000187 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100188 { "connectionattempts", oConnectionAttempts },
189 { "batchmode", oBatchMode },
190 { "checkhostip", oCheckHostIP },
191 { "stricthostkeychecking", oStrictHostKeyChecking },
192 { "compression", oCompression },
193 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100194 { "tcpkeepalive", oTCPKeepAlive },
195 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100196 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100197 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000198 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000199 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000200 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000201 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000202#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000203 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000204#else
205 { "smartcarddevice", oUnsupported },
206#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100207 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000208 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000209 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100210 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000211 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000212 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000213 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100214 { "serveraliveinterval", oServerAliveInterval },
215 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000216 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000217 { "controlpath", oControlPath },
218 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100219 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100220 { "tunnel", oTunnel },
221 { "tunneldevice", oTunnelDevice },
222 { "localcommand", oLocalCommand },
223 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000224 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100225};
226
Damien Miller5428f641999-11-25 11:54:57 +1100227/*
228 * Adds a local TCP/IP port forward to options. Never returns if there is an
229 * error.
230 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Miller4af51302000-04-16 11:18:38 +1000232void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100233add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234{
Damien Miller95def091999-11-25 00:26:21 +1100235 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000236#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100237 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100238 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000239 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100240#endif
Damien Miller95def091999-11-25 00:26:21 +1100241 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
242 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
243 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100244
245 fwd->listen_host = (newfwd->listen_host == NULL) ?
246 NULL : xstrdup(newfwd->listen_host);
247 fwd->listen_port = newfwd->listen_port;
248 fwd->connect_host = xstrdup(newfwd->connect_host);
249 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250}
251
Damien Miller5428f641999-11-25 11:54:57 +1100252/*
253 * Adds a remote TCP/IP port forward to options. Never returns if there is
254 * an error.
255 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller4af51302000-04-16 11:18:38 +1000257void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100258add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259{
Damien Miller95def091999-11-25 00:26:21 +1100260 Forward *fwd;
261 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
262 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100263 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100264 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100265
266 fwd->listen_host = (newfwd->listen_host == NULL) ?
267 NULL : xstrdup(newfwd->listen_host);
268 fwd->listen_port = newfwd->listen_port;
269 fwd->connect_host = xstrdup(newfwd->connect_host);
270 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271}
272
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000273static void
274clear_forwardings(Options *options)
275{
276 int i;
277
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100279 if (options->local_forwards[i].listen_host != NULL)
280 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100281 xfree(options->local_forwards[i].connect_host);
282 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000283 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100284 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100285 if (options->remote_forwards[i].listen_host != NULL)
286 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100287 xfree(options->remote_forwards[i].connect_host);
288 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000289 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100290 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000291}
292
Damien Miller5428f641999-11-25 11:54:57 +1100293/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000294 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100295 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296
Damien Miller4af51302000-04-16 11:18:38 +1000297static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100298parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000300 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301
Damien Miller95def091999-11-25 00:26:21 +1100302 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100303 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100304 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000306 error("%s: line %d: Bad configuration option: %s",
307 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100308 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309}
310
Damien Miller5428f641999-11-25 11:54:57 +1100311/*
312 * Processes a single option line as used in the configuration files. This
313 * only sets those values that have not already been set.
314 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100315#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
Damien Miller2ccf6611999-11-15 15:25:10 +1100317int
318process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100319 char *line, const char *filename, int linenum,
320 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100322 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100323 int opcode, *intptr, value, value2, scale;
324 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100325 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100326 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Millerc652cac2003-05-14 13:40:54 +1000328 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100329 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000330 if (strchr(WHITESPACE, line[len]) == NULL)
331 break;
332 line[len] = '\0';
333 }
334
Damien Millerbe484b52000-07-15 14:14:16 +1000335 s = line;
336 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100337 if ((keyword = strdelim(&s)) == NULL)
338 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000339 /* Ignore leading whitespace. */
340 if (*keyword == '\0')
341 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000342 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100343 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
Damien Miller37023962000-07-11 17:31:38 +1000345 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Miller95def091999-11-25 00:26:21 +1100347 switch (opcode) {
348 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100349 /* don't panic, but count bad options */
350 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100351 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000352 case oConnectTimeout:
353 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100354parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000355 arg = strdelim(&s);
356 if (!arg || *arg == '\0')
357 fatal("%s line %d: missing time value.",
358 filename, linenum);
359 if ((value = convtime(arg)) == -1)
360 fatal("%s line %d: invalid time value.",
361 filename, linenum);
362 if (*intptr == -1)
363 *intptr = value;
364 break;
365
Damien Miller95def091999-11-25 00:26:21 +1100366 case oForwardAgent:
367 intptr = &options->forward_agent;
368parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000369 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000370 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100371 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
372 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000373 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100374 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000375 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100376 value = 0;
377 else
378 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
379 if (*activep && *intptr == -1)
380 *intptr = value;
381 break;
382
383 case oForwardX11:
384 intptr = &options->forward_x11;
385 goto parse_flag;
386
Darren Tucker0a118da2003-10-15 15:54:32 +1000387 case oForwardX11Trusted:
388 intptr = &options->forward_x11_trusted;
389 goto parse_flag;
390
Damien Miller95def091999-11-25 00:26:21 +1100391 case oGatewayPorts:
392 intptr = &options->gateway_ports;
393 goto parse_flag;
394
Darren Tuckere7d4b192006-07-12 22:17:10 +1000395 case oExitOnForwardFailure:
396 intptr = &options->exit_on_forward_failure;
397 goto parse_flag;
398
Damien Miller95def091999-11-25 00:26:21 +1100399 case oUsePrivilegedPort:
400 intptr = &options->use_privileged_port;
401 goto parse_flag;
402
Damien Miller95def091999-11-25 00:26:21 +1100403 case oPasswordAuthentication:
404 intptr = &options->password_authentication;
405 goto parse_flag;
406
Damien Miller874d77b2000-10-14 16:23:11 +1100407 case oKbdInteractiveAuthentication:
408 intptr = &options->kbd_interactive_authentication;
409 goto parse_flag;
410
411 case oKbdInteractiveDevices:
412 charptr = &options->kbd_interactive_devices;
413 goto parse_string;
414
Damien Miller0bc1bd82000-11-13 22:57:25 +1100415 case oPubkeyAuthentication:
416 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000417 goto parse_flag;
418
Damien Miller95def091999-11-25 00:26:21 +1100419 case oRSAAuthentication:
420 intptr = &options->rsa_authentication;
421 goto parse_flag;
422
423 case oRhostsRSAAuthentication:
424 intptr = &options->rhosts_rsa_authentication;
425 goto parse_flag;
426
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000427 case oHostbasedAuthentication:
428 intptr = &options->hostbased_authentication;
429 goto parse_flag;
430
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000431 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000432 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100433 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000434
Darren Tucker0efd1552003-08-26 11:49:55 +1000435 case oGssAuthentication:
436 intptr = &options->gss_authentication;
437 goto parse_flag;
438
439 case oGssDelegateCreds:
440 intptr = &options->gss_deleg_creds;
441 goto parse_flag;
442
Damien Miller95def091999-11-25 00:26:21 +1100443 case oBatchMode:
444 intptr = &options->batch_mode;
445 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446
Damien Miller95def091999-11-25 00:26:21 +1100447 case oCheckHostIP:
448 intptr = &options->check_host_ip;
449 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Damien Miller37876e92003-05-15 10:19:46 +1000451 case oVerifyHostKeyDNS:
452 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100453 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000454
Damien Miller95def091999-11-25 00:26:21 +1100455 case oStrictHostKeyChecking:
456 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100457parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000458 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000459 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000460 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100461 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100462 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000463 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100464 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000465 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100466 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000467 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100468 value = 2;
469 else
470 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
471 if (*activep && *intptr == -1)
472 *intptr = value;
473 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 case oCompression:
476 intptr = &options->compression;
477 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Damien Miller12c150e2003-12-17 16:31:10 +1100479 case oTCPKeepAlive:
480 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100481 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000483 case oNoHostAuthenticationForLocalhost:
484 intptr = &options->no_host_authentication_for_localhost;
485 goto parse_flag;
486
Damien Miller95def091999-11-25 00:26:21 +1100487 case oNumberOfPasswordPrompts:
488 intptr = &options->number_of_password_prompts;
489 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller95def091999-11-25 00:26:21 +1100491 case oCompressionLevel:
492 intptr = &options->compression_level;
493 goto parse_int;
494
Damien Millera5539d22003-04-09 20:50:06 +1000495 case oRekeyLimit:
496 intptr = &options->rekey_limit;
497 arg = strdelim(&s);
498 if (!arg || *arg == '\0')
499 fatal("%.200s line %d: Missing argument.", filename, linenum);
500 if (arg[0] < '0' || arg[0] > '9')
501 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100502 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000503 if (arg == endofnumber)
504 fatal("%.200s line %d: Bad number.", filename, linenum);
505 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100506 case '\0':
507 scale = 1;
508 break;
Damien Millera5539d22003-04-09 20:50:06 +1000509 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100510 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000511 break;
512 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100513 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000514 break;
515 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100516 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000517 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100518 default:
519 fatal("%.200s line %d: Invalid RekeyLimit suffix",
520 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000521 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100522 val64 *= scale;
523 /* detect integer wrap and too-large limits */
524 if ((val64 / scale) != orig || val64 > INT_MAX)
525 fatal("%.200s line %d: RekeyLimit too large",
526 filename, linenum);
527 if (val64 < 16)
528 fatal("%.200s line %d: RekeyLimit too small",
529 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000530 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100531 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000532 break;
533
Damien Miller95def091999-11-25 00:26:21 +1100534 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000535 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000536 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100537 fatal("%.200s line %d: Missing argument.", filename, linenum);
538 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100539 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000540 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100541 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100542 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100543 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000544 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000545 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100546 }
547 break;
548
Damien Millerd3a18572000-06-07 19:55:44 +1000549 case oXAuthLocation:
550 charptr=&options->xauth_location;
551 goto parse_string;
552
Damien Miller95def091999-11-25 00:26:21 +1100553 case oUser:
554 charptr = &options->user;
555parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000556 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000557 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100558 fatal("%.200s line %d: Missing argument.", filename, linenum);
559 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000560 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100561 break;
562
563 case oGlobalKnownHostsFile:
564 charptr = &options->system_hostfile;
565 goto parse_string;
566
567 case oUserKnownHostsFile:
568 charptr = &options->user_hostfile;
569 goto parse_string;
570
Damien Millereba71ba2000-04-29 23:57:08 +1000571 case oGlobalKnownHostsFile2:
572 charptr = &options->system_hostfile2;
573 goto parse_string;
574
575 case oUserKnownHostsFile2:
576 charptr = &options->user_hostfile2;
577 goto parse_string;
578
Damien Miller95def091999-11-25 00:26:21 +1100579 case oHostName:
580 charptr = &options->hostname;
581 goto parse_string;
582
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000583 case oHostKeyAlias:
584 charptr = &options->host_key_alias;
585 goto parse_string;
586
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000587 case oPreferredAuthentications:
588 charptr = &options->preferred_authentications;
589 goto parse_string;
590
Ben Lindstrome0f88042001-04-30 13:06:24 +0000591 case oBindAddress:
592 charptr = &options->bind_address;
593 goto parse_string;
594
Ben Lindstromae996bf2001-08-06 21:27:53 +0000595 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000596 charptr = &options->smartcard_device;
597 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000598
Damien Miller95def091999-11-25 00:26:21 +1100599 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100600 charptr = &options->proxy_command;
601parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000602 if (s == NULL)
603 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100604 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100605 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100606 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100607 return 0;
608
609 case oPort:
610 intptr = &options->port;
611parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000612 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000613 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100614 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000615 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100616 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100617
618 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000619 value = strtol(arg, &endofnumber, 0);
620 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100621 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100622 if (*activep && *intptr == -1)
623 *intptr = value;
624 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625
Damien Miller95def091999-11-25 00:26:21 +1100626 case oConnectionAttempts:
627 intptr = &options->connection_attempts;
628 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100629
Damien Miller95def091999-11-25 00:26:21 +1100630 case oCipher:
631 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000632 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000633 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000634 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000635 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100636 if (value == -1)
637 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100638 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100639 if (*activep && *intptr == -1)
640 *intptr = value;
641 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller78928792000-04-12 20:17:38 +1000643 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000644 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000645 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000646 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000647 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000648 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100649 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000650 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000651 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000652 break;
653
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000654 case oMacs:
655 arg = strdelim(&s);
656 if (!arg || *arg == '\0')
657 fatal("%.200s line %d: Missing argument.", filename, linenum);
658 if (!mac_valid(arg))
659 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100660 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000661 if (*activep && options->macs == NULL)
662 options->macs = xstrdup(arg);
663 break;
664
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000665 case oHostKeyAlgorithms:
666 arg = strdelim(&s);
667 if (!arg || *arg == '\0')
668 fatal("%.200s line %d: Missing argument.", filename, linenum);
669 if (!key_names_valid2(arg))
670 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100671 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000672 if (*activep && options->hostkeyalgorithms == NULL)
673 options->hostkeyalgorithms = xstrdup(arg);
674 break;
675
Damien Miller78928792000-04-12 20:17:38 +1000676 case oProtocol:
677 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000678 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000679 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000680 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000681 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000682 if (value == SSH_PROTO_UNKNOWN)
683 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100684 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000685 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
686 *intptr = value;
687 break;
688
Damien Miller95def091999-11-25 00:26:21 +1100689 case oLogLevel:
690 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000691 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000692 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100693 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000694 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100695 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100696 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100697 *intptr = (LogLevel) value;
698 break;
699
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000700 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100701 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000702 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100703 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000704 fatal("%.200s line %d: Missing port argument.",
705 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100706 arg2 = strdelim(&s);
707 if (arg2 == NULL || *arg2 == '\0')
708 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000709 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100710
711 /* construct a string for parse_forward */
712 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
713
714 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000715 fatal("%.200s line %d: Bad forwarding specification.",
716 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100717
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000718 if (*activep) {
719 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100720 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000721 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100722 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000723 }
Damien Miller95def091999-11-25 00:26:21 +1100724 break;
725
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000726 case oDynamicForward:
727 arg = strdelim(&s);
728 if (!arg || *arg == '\0')
729 fatal("%.200s line %d: Missing port argument.",
730 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100731 memset(&fwd, '\0', sizeof(fwd));
732 fwd.connect_host = "socks";
733 fwd.listen_host = hpdelim(&arg);
734 if (fwd.listen_host == NULL ||
735 strlen(fwd.listen_host) >= NI_MAXHOST)
736 fatal("%.200s line %d: Bad forwarding specification.",
737 filename, linenum);
738 if (arg) {
739 fwd.listen_port = a2port(arg);
740 fwd.listen_host = cleanhostname(fwd.listen_host);
741 } else {
742 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000743 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100744 }
745 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000746 fatal("%.200s line %d: Badly formatted port number.",
747 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000748 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100749 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000750 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000751
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000752 case oClearAllForwardings:
753 intptr = &options->clear_forwardings;
754 goto parse_flag;
755
Damien Miller95def091999-11-25 00:26:21 +1100756 case oHost:
757 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000758 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000759 if (match_pattern(host, arg)) {
760 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100761 *activep = 1;
762 break;
763 }
Damien Millerbe484b52000-07-15 14:14:16 +1000764 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100765 return 0;
766
767 case oEscapeChar:
768 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000769 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000770 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100771 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000772 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000773 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
774 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000775 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000776 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000777 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000778 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100779 else {
780 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100781 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100782 /* NOTREACHED */
783 value = 0; /* Avoid compiler warning. */
784 }
785 if (*activep && *intptr == -1)
786 *intptr = value;
787 break;
788
Damien Miller20a8f972003-05-18 20:50:30 +1000789 case oAddressFamily:
790 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000791 if (!arg || *arg == '\0')
792 fatal("%s line %d: missing address family.",
793 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000794 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000795 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000796 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000797 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000798 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000799 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000800 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000801 else
802 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000803 if (*activep && *intptr == -1)
804 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000805 break;
806
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000807 case oEnableSSHKeysign:
808 intptr = &options->enable_ssh_keysign;
809 goto parse_flag;
810
Damien Millerbd394c32004-03-08 23:12:36 +1100811 case oIdentitiesOnly:
812 intptr = &options->identities_only;
813 goto parse_flag;
814
Damien Miller509b0102003-12-17 16:33:10 +1100815 case oServerAliveInterval:
816 intptr = &options->server_alive_interval;
817 goto parse_time;
818
819 case oServerAliveCountMax:
820 intptr = &options->server_alive_count_max;
821 goto parse_int;
822
Darren Tucker46bc0752004-05-02 22:11:30 +1000823 case oSendEnv:
824 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
825 if (strchr(arg, '=') != NULL)
826 fatal("%s line %d: Invalid environment name.",
827 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100828 if (!*activep)
829 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000830 if (options->num_send_env >= MAX_SEND_ENV)
831 fatal("%s line %d: too many send env.",
832 filename, linenum);
833 options->send_env[options->num_send_env++] =
834 xstrdup(arg);
835 }
836 break;
837
Damien Miller0e220db2004-06-15 10:34:08 +1000838 case oControlPath:
839 charptr = &options->control_path;
840 goto parse_string;
841
842 case oControlMaster:
843 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000844 arg = strdelim(&s);
845 if (!arg || *arg == '\0')
846 fatal("%.200s line %d: Missing ControlMaster argument.",
847 filename, linenum);
848 value = 0; /* To avoid compiler warning... */
849 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
850 value = SSHCTL_MASTER_YES;
851 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
852 value = SSHCTL_MASTER_NO;
853 else if (strcmp(arg, "auto") == 0)
854 value = SSHCTL_MASTER_AUTO;
855 else if (strcmp(arg, "ask") == 0)
856 value = SSHCTL_MASTER_ASK;
857 else if (strcmp(arg, "autoask") == 0)
858 value = SSHCTL_MASTER_AUTO_ASK;
859 else
860 fatal("%.200s line %d: Bad ControlMaster argument.",
861 filename, linenum);
862 if (*activep && *intptr == -1)
863 *intptr = value;
864 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000865
Damien Millere1776152005-03-01 21:47:37 +1100866 case oHashKnownHosts:
867 intptr = &options->hash_known_hosts;
868 goto parse_flag;
869
Damien Millerd27b9472005-12-13 19:29:02 +1100870 case oTunnel:
871 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100872 arg = strdelim(&s);
873 if (!arg || *arg == '\0')
874 fatal("%s line %d: Missing yes/point-to-point/"
875 "ethernet/no argument.", filename, linenum);
876 value = 0; /* silence compiler */
877 if (strcasecmp(arg, "ethernet") == 0)
878 value = SSH_TUNMODE_ETHERNET;
879 else if (strcasecmp(arg, "point-to-point") == 0)
880 value = SSH_TUNMODE_POINTOPOINT;
881 else if (strcasecmp(arg, "yes") == 0)
882 value = SSH_TUNMODE_DEFAULT;
883 else if (strcasecmp(arg, "no") == 0)
884 value = SSH_TUNMODE_NO;
885 else
886 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
887 "no argument: %s", filename, linenum, arg);
888 if (*activep)
889 *intptr = value;
890 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100891
892 case oTunnelDevice:
893 arg = strdelim(&s);
894 if (!arg || *arg == '\0')
895 fatal("%.200s line %d: Missing argument.", filename, linenum);
896 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100897 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100898 fatal("%.200s line %d: Bad tun device.", filename, linenum);
899 if (*activep) {
900 options->tun_local = value;
901 options->tun_remote = value2;
902 }
903 break;
904
905 case oLocalCommand:
906 charptr = &options->local_command;
907 goto parse_command;
908
909 case oPermitLocalCommand:
910 intptr = &options->permit_local_command;
911 goto parse_flag;
912
Ben Lindstrom4daea862002-06-09 20:04:02 +0000913 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000914 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000915 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000916 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000917
Damien Millerf9b3feb2003-05-16 11:38:32 +1000918 case oUnsupported:
919 error("%s line %d: Unsupported option \"%s\"",
920 filename, linenum, keyword);
921 return 0;
922
Damien Miller95def091999-11-25 00:26:21 +1100923 default:
924 fatal("process_config_line: Unimplemented opcode %d", opcode);
925 }
926
927 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000928 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000929 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000930 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000931 }
Damien Miller95def091999-11-25 00:26:21 +1100932 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933}
934
935
Damien Miller5428f641999-11-25 11:54:57 +1100936/*
937 * Reads the config file and modifies the options accordingly. Options
938 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000939 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100940 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000942int
Darren Tuckerfc959702004-07-17 16:12:08 +1000943read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000944 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945{
Damien Miller95def091999-11-25 00:26:21 +1100946 FILE *f;
947 char line[1024];
948 int active, linenum;
949 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Damien Miller95def091999-11-25 00:26:21 +1100951 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000952 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000953 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954
Damien Miller57a44762004-04-20 20:11:57 +1000955 if (checkperm) {
956 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000957
Damien Miller33793852004-06-15 10:27:55 +1000958 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000959 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000960 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000961 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000962 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000963 }
964
Damien Miller95def091999-11-25 00:26:21 +1100965 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Damien Miller5428f641999-11-25 11:54:57 +1100967 /*
968 * Mark that we are now processing the options. This flag is turned
969 * on/off by Host specifications.
970 */
Damien Miller95def091999-11-25 00:26:21 +1100971 active = 1;
972 linenum = 0;
973 while (fgets(line, sizeof(line), f)) {
974 /* Update line number counter. */
975 linenum++;
976 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
977 bad_options++;
978 }
979 fclose(f);
980 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000981 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100982 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000983 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984}
985
Damien Miller5428f641999-11-25 11:54:57 +1100986/*
987 * Initializes options to special values that indicate that they have not yet
988 * been set. Read_config_file will only set options with this value. Options
989 * are processed in the following order: command line, user config file,
990 * system config file. Last, fill_default_options is called.
991 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller4af51302000-04-16 11:18:38 +1000993void
Damien Miller95def091999-11-25 00:26:21 +1100994initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995{
Damien Miller95def091999-11-25 00:26:21 +1100996 memset(options, 'X', sizeof(*options));
997 options->forward_agent = -1;
998 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000999 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001000 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001001 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001002 options->gateway_ports = -1;
1003 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001004 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001005 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001006 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001007 options->gss_authentication = -1;
1008 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001009 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001010 options->kbd_interactive_authentication = -1;
1011 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001012 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001013 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001014 options->batch_mode = -1;
1015 options->check_host_ip = -1;
1016 options->strict_host_key_checking = -1;
1017 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001018 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001019 options->compression_level = -1;
1020 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001021 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001022 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001023 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001024 options->number_of_password_prompts = -1;
1025 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001026 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001027 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001028 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001029 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001030 options->num_identity_files = 0;
1031 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001032 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001033 options->proxy_command = NULL;
1034 options->user = NULL;
1035 options->escape_char = -1;
1036 options->system_hostfile = NULL;
1037 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001038 options->system_hostfile2 = NULL;
1039 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001040 options->num_local_forwards = 0;
1041 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001042 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001043 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001044 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001045 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001046 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001047 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001048 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001049 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001050 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001051 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001052 options->server_alive_interval = -1;
1053 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001054 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001055 options->control_path = NULL;
1056 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001057 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001058 options->tun_open = -1;
1059 options->tun_local = -1;
1060 options->tun_remote = -1;
1061 options->local_command = NULL;
1062 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063}
1064
Damien Miller5428f641999-11-25 11:54:57 +11001065/*
1066 * Called after processing other sources of option data, this fills those
1067 * options for which no value has been specified with their default values.
1068 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069
Damien Miller4af51302000-04-16 11:18:38 +10001070void
Damien Miller95def091999-11-25 00:26:21 +11001071fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001073 int len;
1074
Damien Miller95def091999-11-25 00:26:21 +11001075 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001076 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001077 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001078 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001079 if (options->forward_x11_trusted == -1)
1080 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001081 if (options->exit_on_forward_failure == -1)
1082 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001083 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001084 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001085 if (options->gateway_ports == -1)
1086 options->gateway_ports = 0;
1087 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001088 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001089 if (options->rsa_authentication == -1)
1090 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001091 if (options->pubkey_authentication == -1)
1092 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001093 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001094 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001095 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001096 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001097 if (options->gss_deleg_creds == -1)
1098 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001099 if (options->password_authentication == -1)
1100 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001101 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001102 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001103 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001104 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001105 if (options->hostbased_authentication == -1)
1106 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001107 if (options->batch_mode == -1)
1108 options->batch_mode = 0;
1109 if (options->check_host_ip == -1)
1110 options->check_host_ip = 1;
1111 if (options->strict_host_key_checking == -1)
1112 options->strict_host_key_checking = 2; /* 2 is default */
1113 if (options->compression == -1)
1114 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001115 if (options->tcp_keep_alive == -1)
1116 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001117 if (options->compression_level == -1)
1118 options->compression_level = 6;
1119 if (options->port == -1)
1120 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001121 if (options->address_family == -1)
1122 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001123 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001124 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001125 if (options->number_of_password_prompts == -1)
1126 options->number_of_password_prompts = 3;
1127 /* Selected in ssh_login(). */
1128 if (options->cipher == -1)
1129 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001130 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001131 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001132 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001133 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001134 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001135 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001136 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001137 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001138 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001139 xmalloc(len);
1140 snprintf(options->identity_files[options->num_identity_files++],
1141 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001142 }
1143 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001144 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1145 options->identity_files[options->num_identity_files] =
1146 xmalloc(len);
1147 snprintf(options->identity_files[options->num_identity_files++],
1148 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1149
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001150 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001151 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001152 xmalloc(len);
1153 snprintf(options->identity_files[options->num_identity_files++],
1154 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001155 }
Damien Millereba71ba2000-04-29 23:57:08 +10001156 }
Damien Miller95def091999-11-25 00:26:21 +11001157 if (options->escape_char == -1)
1158 options->escape_char = '~';
1159 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001160 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001161 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001162 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001163 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001164 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001165 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001166 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001167 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001168 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001169 if (options->clear_forwardings == 1)
1170 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001171 if (options->no_host_authentication_for_localhost == - 1)
1172 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001173 if (options->identities_only == -1)
1174 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001175 if (options->enable_ssh_keysign == -1)
1176 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001177 if (options->rekey_limit == -1)
1178 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001179 if (options->verify_host_key_dns == -1)
1180 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001181 if (options->server_alive_interval == -1)
1182 options->server_alive_interval = 0;
1183 if (options->server_alive_count_max == -1)
1184 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001185 if (options->control_master == -1)
1186 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001187 if (options->hash_known_hosts == -1)
1188 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001189 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001190 options->tun_open = SSH_TUNMODE_NO;
1191 if (options->tun_local == -1)
1192 options->tun_local = SSH_TUNID_ANY;
1193 if (options->tun_remote == -1)
1194 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001195 if (options->permit_local_command == -1)
1196 options->permit_local_command = 0;
1197 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001198 /* options->proxy_command should not be set by default */
1199 /* options->user will be set in the main program if appropriate */
1200 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001201 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001202 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001204
1205/*
1206 * parse_forward
1207 * parses a string containing a port forwarding specification of the form:
1208 * [listenhost:]listenport:connecthost:connectport
1209 * returns number of arguments parsed or zero on error
1210 */
1211int
1212parse_forward(Forward *fwd, const char *fwdspec)
1213{
1214 int i;
1215 char *p, *cp, *fwdarg[4];
1216
1217 memset(fwd, '\0', sizeof(*fwd));
1218
1219 cp = p = xstrdup(fwdspec);
1220
1221 /* skip leading spaces */
1222 while (*cp && isspace(*cp))
1223 cp++;
1224
1225 for (i = 0; i < 4; ++i)
1226 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1227 break;
1228
1229 /* Check for trailing garbage in 4-arg case*/
1230 if (cp != NULL)
1231 i = 0; /* failure */
1232
1233 switch (i) {
1234 case 3:
1235 fwd->listen_host = NULL;
1236 fwd->listen_port = a2port(fwdarg[0]);
1237 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1238 fwd->connect_port = a2port(fwdarg[2]);
1239 break;
1240
1241 case 4:
1242 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1243 fwd->listen_port = a2port(fwdarg[1]);
1244 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1245 fwd->connect_port = a2port(fwdarg[3]);
1246 break;
1247 default:
1248 i = 0; /* failure */
1249 }
1250
1251 xfree(p);
1252
1253 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1254 goto fail_free;
1255
1256 if (fwd->connect_host != NULL &&
1257 strlen(fwd->connect_host) >= NI_MAXHOST)
1258 goto fail_free;
1259
1260 return (i);
1261
1262 fail_free:
1263 if (fwd->connect_host != NULL)
1264 xfree(fwd->connect_host);
1265 if (fwd->listen_host != NULL)
1266 xfree(fwd->listen_host);
1267 return (0);
1268}