blob: c57ea0c823c320ac71a71261c96464f3ab262dce [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: readconf.c,v 1.159 2006/08/03 03:34:42 deraadt 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 Millerd7834352006-08-05 12:39:39 +100026#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100027#include <stdio.h>
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 "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100032#include "ssh.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"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "readconf.h"
39#include "match.h"
40#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000042#include "kex.h"
43#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45/* Format of the configuration file:
46
47 # Configuration data is parsed as follows:
48 # 1. command line options
49 # 2. user-specific file
50 # 3. system-wide file
51 # Any configuration value is only changed the first time it is set.
52 # Thus, host-specific definitions should be at the beginning of the
53 # configuration file, and defaults at the end.
54
55 # Host-specific declarations. These may override anything above. A single
56 # host may match multiple declarations; these are processed in the order
57 # that they are given in.
58
59 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000060 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62 Host fake.com
63 HostName another.host.name.real.org
64 User blaah
65 Port 34289
66 ForwardX11 no
67 ForwardAgent no
68
69 Host books.com
70 RemoteForward 9999 shadows.cs.hut.fi:9999
71 Cipher 3des
72
73 Host fascist.blob.com
74 Port 23123
75 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076 PasswordAuthentication no
77
78 Host puukko.hut.fi
79 User t35124p
80 ProxyCommand ssh-proxy %h %p
81
82 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000083 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
85 Host *.su
86 Cipher none
87 PasswordAuthentication no
88
Damien Millerd27b9472005-12-13 19:29:02 +110089 Host vpn.fake.com
90 Tunnel yes
91 TunnelDevice 3
92
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093 # Defaults for various options
94 Host *
95 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110096 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097 PasswordAuthentication yes
98 RSAAuthentication yes
99 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100101 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102 IdentityFile ~/.ssh/identity
103 Port 22
104 EscapeChar ~
105
106*/
107
108/* Keyword tokens. */
109
Damien Miller95def091999-11-25 00:26:21 +1100110typedef enum {
111 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000112 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000113 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000114 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000115 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100116 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
117 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
118 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
119 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100120 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000121 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000123 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000124 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000125 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000126 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000127 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000128 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100129 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100130 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100131 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000132 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133} OpCodes;
134
135/* Textual representations of the tokens. */
136
Damien Miller95def091999-11-25 00:26:21 +1100137static struct {
138 const char *name;
139 OpCodes opcode;
140} keywords[] = {
141 { "forwardagent", oForwardAgent },
142 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000143 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000144 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000145 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100146 { "gatewayports", oGatewayPorts },
147 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000148 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100149 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100150 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
151 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100152 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100153 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000154 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000155 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000156 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000157 { "challengeresponseauthentication", oChallengeResponseAuthentication },
158 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
159 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000160 { "kerberosauthentication", oUnsupported },
161 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000162 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000163#if defined(GSSAPI)
164 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000165 { "gssapidelegatecredentials", oGssDelegateCreds },
166#else
167 { "gssapiauthentication", oUnsupported },
168 { "gssapidelegatecredentials", oUnsupported },
169#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000170 { "fallbacktorsh", oDeprecated },
171 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100172 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100173 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100174 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100175 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000176 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "proxycommand", oProxyCommand },
178 { "port", oPort },
179 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000180 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000181 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000182 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100183 { "remoteforward", oRemoteForward },
184 { "localforward", oLocalForward },
185 { "user", oUser },
186 { "host", oHost },
187 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100188 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000189 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000190 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000191 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100192 { "connectionattempts", oConnectionAttempts },
193 { "batchmode", oBatchMode },
194 { "checkhostip", oCheckHostIP },
195 { "stricthostkeychecking", oStrictHostKeyChecking },
196 { "compression", oCompression },
197 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100198 { "tcpkeepalive", oTCPKeepAlive },
199 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100200 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100201 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000202 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000203 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000204 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000205 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000206#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000207 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000208#else
209 { "smartcarddevice", oUnsupported },
210#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100211 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000212 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000213 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100214 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000215 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000216 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000217 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100218 { "serveraliveinterval", oServerAliveInterval },
219 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000220 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000221 { "controlpath", oControlPath },
222 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100223 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100224 { "tunnel", oTunnel },
225 { "tunneldevice", oTunnelDevice },
226 { "localcommand", oLocalCommand },
227 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000228 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100229};
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
232 * Adds a local TCP/IP port forward to options. Never returns if there is an
233 * error.
234 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Damien Miller4af51302000-04-16 11:18:38 +1000236void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100237add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Damien Miller95def091999-11-25 00:26:21 +1100239 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000240#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100241 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100242 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000243 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100244#endif
Damien Miller95def091999-11-25 00:26:21 +1100245 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
246 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
247 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100248
249 fwd->listen_host = (newfwd->listen_host == NULL) ?
250 NULL : xstrdup(newfwd->listen_host);
251 fwd->listen_port = newfwd->listen_port;
252 fwd->connect_host = xstrdup(newfwd->connect_host);
253 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
Damien Miller5428f641999-11-25 11:54:57 +1100256/*
257 * Adds a remote TCP/IP port forward to options. Never returns if there is
258 * an error.
259 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller4af51302000-04-16 11:18:38 +1000261void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100262add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263{
Damien Miller95def091999-11-25 00:26:21 +1100264 Forward *fwd;
265 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
266 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100267 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100268 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100269
270 fwd->listen_host = (newfwd->listen_host == NULL) ?
271 NULL : xstrdup(newfwd->listen_host);
272 fwd->listen_port = newfwd->listen_port;
273 fwd->connect_host = xstrdup(newfwd->connect_host);
274 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275}
276
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000277static void
278clear_forwardings(Options *options)
279{
280 int i;
281
Damien Millerf91ee4c2005-03-01 21:24:33 +1100282 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100283 if (options->local_forwards[i].listen_host != NULL)
284 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100285 xfree(options->local_forwards[i].connect_host);
286 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000287 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100289 if (options->remote_forwards[i].listen_host != NULL)
290 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291 xfree(options->remote_forwards[i].connect_host);
292 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000293 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100294 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000295}
296
Damien Miller5428f641999-11-25 11:54:57 +1100297/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000298 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100299 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300
Damien Miller4af51302000-04-16 11:18:38 +1000301static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100302parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000304 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Miller95def091999-11-25 00:26:21 +1100306 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100307 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100308 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000310 error("%s: line %d: Bad configuration option: %s",
311 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100312 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313}
314
Damien Miller5428f641999-11-25 11:54:57 +1100315/*
316 * Processes a single option line as used in the configuration files. This
317 * only sets those values that have not already been set.
318 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100319#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Damien Miller2ccf6611999-11-15 15:25:10 +1100321int
322process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100323 char *line, const char *filename, int linenum,
324 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100326 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100327 int opcode, *intptr, value, value2, scale;
328 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100329 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100330 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Millerc652cac2003-05-14 13:40:54 +1000332 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100333 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000334 if (strchr(WHITESPACE, line[len]) == NULL)
335 break;
336 line[len] = '\0';
337 }
338
Damien Millerbe484b52000-07-15 14:14:16 +1000339 s = line;
340 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100341 if ((keyword = strdelim(&s)) == NULL)
342 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000343 /* Ignore leading whitespace. */
344 if (*keyword == '\0')
345 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000346 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100347 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348
Damien Miller37023962000-07-11 17:31:38 +1000349 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller95def091999-11-25 00:26:21 +1100351 switch (opcode) {
352 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100353 /* don't panic, but count bad options */
354 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100355 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000356 case oConnectTimeout:
357 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100358parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000359 arg = strdelim(&s);
360 if (!arg || *arg == '\0')
361 fatal("%s line %d: missing time value.",
362 filename, linenum);
363 if ((value = convtime(arg)) == -1)
364 fatal("%s line %d: invalid time value.",
365 filename, linenum);
366 if (*intptr == -1)
367 *intptr = value;
368 break;
369
Damien Miller95def091999-11-25 00:26:21 +1100370 case oForwardAgent:
371 intptr = &options->forward_agent;
372parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000373 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000374 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100375 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
376 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000377 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100378 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000379 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100380 value = 0;
381 else
382 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
383 if (*activep && *intptr == -1)
384 *intptr = value;
385 break;
386
387 case oForwardX11:
388 intptr = &options->forward_x11;
389 goto parse_flag;
390
Darren Tucker0a118da2003-10-15 15:54:32 +1000391 case oForwardX11Trusted:
392 intptr = &options->forward_x11_trusted;
393 goto parse_flag;
394
Damien Miller95def091999-11-25 00:26:21 +1100395 case oGatewayPorts:
396 intptr = &options->gateway_ports;
397 goto parse_flag;
398
Darren Tuckere7d4b192006-07-12 22:17:10 +1000399 case oExitOnForwardFailure:
400 intptr = &options->exit_on_forward_failure;
401 goto parse_flag;
402
Damien Miller95def091999-11-25 00:26:21 +1100403 case oUsePrivilegedPort:
404 intptr = &options->use_privileged_port;
405 goto parse_flag;
406
Damien Miller95def091999-11-25 00:26:21 +1100407 case oPasswordAuthentication:
408 intptr = &options->password_authentication;
409 goto parse_flag;
410
Damien Miller874d77b2000-10-14 16:23:11 +1100411 case oKbdInteractiveAuthentication:
412 intptr = &options->kbd_interactive_authentication;
413 goto parse_flag;
414
415 case oKbdInteractiveDevices:
416 charptr = &options->kbd_interactive_devices;
417 goto parse_string;
418
Damien Miller0bc1bd82000-11-13 22:57:25 +1100419 case oPubkeyAuthentication:
420 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000421 goto parse_flag;
422
Damien Miller95def091999-11-25 00:26:21 +1100423 case oRSAAuthentication:
424 intptr = &options->rsa_authentication;
425 goto parse_flag;
426
427 case oRhostsRSAAuthentication:
428 intptr = &options->rhosts_rsa_authentication;
429 goto parse_flag;
430
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000431 case oHostbasedAuthentication:
432 intptr = &options->hostbased_authentication;
433 goto parse_flag;
434
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000435 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000436 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100437 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000438
Darren Tucker0efd1552003-08-26 11:49:55 +1000439 case oGssAuthentication:
440 intptr = &options->gss_authentication;
441 goto parse_flag;
442
443 case oGssDelegateCreds:
444 intptr = &options->gss_deleg_creds;
445 goto parse_flag;
446
Damien Miller95def091999-11-25 00:26:21 +1100447 case oBatchMode:
448 intptr = &options->batch_mode;
449 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Damien Miller95def091999-11-25 00:26:21 +1100451 case oCheckHostIP:
452 intptr = &options->check_host_ip;
453 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Damien Miller37876e92003-05-15 10:19:46 +1000455 case oVerifyHostKeyDNS:
456 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100457 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000458
Damien Miller95def091999-11-25 00:26:21 +1100459 case oStrictHostKeyChecking:
460 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100461parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000462 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000463 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000464 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100465 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100466 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000467 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100468 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000469 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100470 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000471 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100472 value = 2;
473 else
474 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
475 if (*activep && *intptr == -1)
476 *intptr = value;
477 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Damien Miller95def091999-11-25 00:26:21 +1100479 case oCompression:
480 intptr = &options->compression;
481 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482
Damien Miller12c150e2003-12-17 16:31:10 +1100483 case oTCPKeepAlive:
484 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100485 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000487 case oNoHostAuthenticationForLocalhost:
488 intptr = &options->no_host_authentication_for_localhost;
489 goto parse_flag;
490
Damien Miller95def091999-11-25 00:26:21 +1100491 case oNumberOfPasswordPrompts:
492 intptr = &options->number_of_password_prompts;
493 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
Damien Miller95def091999-11-25 00:26:21 +1100495 case oCompressionLevel:
496 intptr = &options->compression_level;
497 goto parse_int;
498
Damien Millera5539d22003-04-09 20:50:06 +1000499 case oRekeyLimit:
500 intptr = &options->rekey_limit;
501 arg = strdelim(&s);
502 if (!arg || *arg == '\0')
503 fatal("%.200s line %d: Missing argument.", filename, linenum);
504 if (arg[0] < '0' || arg[0] > '9')
505 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100506 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000507 if (arg == endofnumber)
508 fatal("%.200s line %d: Bad number.", filename, linenum);
509 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100510 case '\0':
511 scale = 1;
512 break;
Damien Millera5539d22003-04-09 20:50:06 +1000513 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100514 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000515 break;
516 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100517 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000518 break;
519 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100520 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000521 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100522 default:
523 fatal("%.200s line %d: Invalid RekeyLimit suffix",
524 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000525 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100526 val64 *= scale;
527 /* detect integer wrap and too-large limits */
528 if ((val64 / scale) != orig || val64 > INT_MAX)
529 fatal("%.200s line %d: RekeyLimit too large",
530 filename, linenum);
531 if (val64 < 16)
532 fatal("%.200s line %d: RekeyLimit too small",
533 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000534 if (*activep && *intptr == -1)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100535 *intptr = (int)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000536 break;
537
Damien Miller95def091999-11-25 00:26:21 +1100538 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000539 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000540 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100541 fatal("%.200s line %d: Missing argument.", filename, linenum);
542 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100543 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000544 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100545 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100546 filename, linenum, SSH_MAX_IDENTITY_FILES);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100547 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000548 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000549 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100550 }
551 break;
552
Damien Millerd3a18572000-06-07 19:55:44 +1000553 case oXAuthLocation:
554 charptr=&options->xauth_location;
555 goto parse_string;
556
Damien Miller95def091999-11-25 00:26:21 +1100557 case oUser:
558 charptr = &options->user;
559parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000560 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000561 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100562 fatal("%.200s line %d: Missing argument.", filename, linenum);
563 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000564 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100565 break;
566
567 case oGlobalKnownHostsFile:
568 charptr = &options->system_hostfile;
569 goto parse_string;
570
571 case oUserKnownHostsFile:
572 charptr = &options->user_hostfile;
573 goto parse_string;
574
Damien Millereba71ba2000-04-29 23:57:08 +1000575 case oGlobalKnownHostsFile2:
576 charptr = &options->system_hostfile2;
577 goto parse_string;
578
579 case oUserKnownHostsFile2:
580 charptr = &options->user_hostfile2;
581 goto parse_string;
582
Damien Miller95def091999-11-25 00:26:21 +1100583 case oHostName:
584 charptr = &options->hostname;
585 goto parse_string;
586
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000587 case oHostKeyAlias:
588 charptr = &options->host_key_alias;
589 goto parse_string;
590
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000591 case oPreferredAuthentications:
592 charptr = &options->preferred_authentications;
593 goto parse_string;
594
Ben Lindstrome0f88042001-04-30 13:06:24 +0000595 case oBindAddress:
596 charptr = &options->bind_address;
597 goto parse_string;
598
Ben Lindstromae996bf2001-08-06 21:27:53 +0000599 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000600 charptr = &options->smartcard_device;
601 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000602
Damien Miller95def091999-11-25 00:26:21 +1100603 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100604 charptr = &options->proxy_command;
605parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000606 if (s == NULL)
607 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100608 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100609 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100610 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100611 return 0;
612
613 case oPort:
614 intptr = &options->port;
615parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000616 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000617 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100618 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000619 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100620 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100621
622 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000623 value = strtol(arg, &endofnumber, 0);
624 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100625 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100626 if (*activep && *intptr == -1)
627 *intptr = value;
628 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629
Damien Miller95def091999-11-25 00:26:21 +1100630 case oConnectionAttempts:
631 intptr = &options->connection_attempts;
632 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100633
Damien Miller95def091999-11-25 00:26:21 +1100634 case oCipher:
635 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000636 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000637 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000638 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000639 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100640 if (value == -1)
641 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100642 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100643 if (*activep && *intptr == -1)
644 *intptr = value;
645 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646
Damien Miller78928792000-04-12 20:17:38 +1000647 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000648 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000649 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000650 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000651 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000652 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100653 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000654 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000655 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000656 break;
657
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000658 case oMacs:
659 arg = strdelim(&s);
660 if (!arg || *arg == '\0')
661 fatal("%.200s line %d: Missing argument.", filename, linenum);
662 if (!mac_valid(arg))
663 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100664 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000665 if (*activep && options->macs == NULL)
666 options->macs = xstrdup(arg);
667 break;
668
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000669 case oHostKeyAlgorithms:
670 arg = strdelim(&s);
671 if (!arg || *arg == '\0')
672 fatal("%.200s line %d: Missing argument.", filename, linenum);
673 if (!key_names_valid2(arg))
674 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100675 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000676 if (*activep && options->hostkeyalgorithms == NULL)
677 options->hostkeyalgorithms = xstrdup(arg);
678 break;
679
Damien Miller78928792000-04-12 20:17:38 +1000680 case oProtocol:
681 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000682 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000683 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000684 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000685 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000686 if (value == SSH_PROTO_UNKNOWN)
687 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100688 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000689 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
690 *intptr = value;
691 break;
692
Damien Miller95def091999-11-25 00:26:21 +1100693 case oLogLevel:
694 intptr = (int *) &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000695 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000696 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100697 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000698 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100699 filename, linenum, arg ? arg : "<NONE>");
Damien Millerfcd93202002-02-05 12:26:34 +1100700 if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100701 *intptr = (LogLevel) value;
702 break;
703
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000704 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100705 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000706 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000708 fatal("%.200s line %d: Missing port argument.",
709 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100710 arg2 = strdelim(&s);
711 if (arg2 == NULL || *arg2 == '\0')
712 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000713 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100714
715 /* construct a string for parse_forward */
716 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
717
718 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000719 fatal("%.200s line %d: Bad forwarding specification.",
720 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100721
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000722 if (*activep) {
723 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100724 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000725 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100726 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000727 }
Damien Miller95def091999-11-25 00:26:21 +1100728 break;
729
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000730 case oDynamicForward:
731 arg = strdelim(&s);
732 if (!arg || *arg == '\0')
733 fatal("%.200s line %d: Missing port argument.",
734 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100735 memset(&fwd, '\0', sizeof(fwd));
736 fwd.connect_host = "socks";
737 fwd.listen_host = hpdelim(&arg);
738 if (fwd.listen_host == NULL ||
739 strlen(fwd.listen_host) >= NI_MAXHOST)
740 fatal("%.200s line %d: Bad forwarding specification.",
741 filename, linenum);
742 if (arg) {
743 fwd.listen_port = a2port(arg);
744 fwd.listen_host = cleanhostname(fwd.listen_host);
745 } else {
746 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000747 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100748 }
749 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000750 fatal("%.200s line %d: Badly formatted port number.",
751 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000752 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100753 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000754 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000755
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000756 case oClearAllForwardings:
757 intptr = &options->clear_forwardings;
758 goto parse_flag;
759
Damien Miller95def091999-11-25 00:26:21 +1100760 case oHost:
761 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000762 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000763 if (match_pattern(host, arg)) {
764 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100765 *activep = 1;
766 break;
767 }
Damien Millerbe484b52000-07-15 14:14:16 +1000768 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100769 return 0;
770
771 case oEscapeChar:
772 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000773 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000774 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100775 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000776 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000777 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
778 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000779 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000780 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000781 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000782 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100783 else {
784 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100785 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100786 /* NOTREACHED */
787 value = 0; /* Avoid compiler warning. */
788 }
789 if (*activep && *intptr == -1)
790 *intptr = value;
791 break;
792
Damien Miller20a8f972003-05-18 20:50:30 +1000793 case oAddressFamily:
794 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000795 if (!arg || *arg == '\0')
796 fatal("%s line %d: missing address family.",
797 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000798 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000799 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000800 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000801 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000802 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000803 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000804 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000805 else
806 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000807 if (*activep && *intptr == -1)
808 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000809 break;
810
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000811 case oEnableSSHKeysign:
812 intptr = &options->enable_ssh_keysign;
813 goto parse_flag;
814
Damien Millerbd394c32004-03-08 23:12:36 +1100815 case oIdentitiesOnly:
816 intptr = &options->identities_only;
817 goto parse_flag;
818
Damien Miller509b0102003-12-17 16:33:10 +1100819 case oServerAliveInterval:
820 intptr = &options->server_alive_interval;
821 goto parse_time;
822
823 case oServerAliveCountMax:
824 intptr = &options->server_alive_count_max;
825 goto parse_int;
826
Darren Tucker46bc0752004-05-02 22:11:30 +1000827 case oSendEnv:
828 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
829 if (strchr(arg, '=') != NULL)
830 fatal("%s line %d: Invalid environment name.",
831 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100832 if (!*activep)
833 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000834 if (options->num_send_env >= MAX_SEND_ENV)
835 fatal("%s line %d: too many send env.",
836 filename, linenum);
837 options->send_env[options->num_send_env++] =
838 xstrdup(arg);
839 }
840 break;
841
Damien Miller0e220db2004-06-15 10:34:08 +1000842 case oControlPath:
843 charptr = &options->control_path;
844 goto parse_string;
845
846 case oControlMaster:
847 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000848 arg = strdelim(&s);
849 if (!arg || *arg == '\0')
850 fatal("%.200s line %d: Missing ControlMaster argument.",
851 filename, linenum);
852 value = 0; /* To avoid compiler warning... */
853 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
854 value = SSHCTL_MASTER_YES;
855 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
856 value = SSHCTL_MASTER_NO;
857 else if (strcmp(arg, "auto") == 0)
858 value = SSHCTL_MASTER_AUTO;
859 else if (strcmp(arg, "ask") == 0)
860 value = SSHCTL_MASTER_ASK;
861 else if (strcmp(arg, "autoask") == 0)
862 value = SSHCTL_MASTER_AUTO_ASK;
863 else
864 fatal("%.200s line %d: Bad ControlMaster argument.",
865 filename, linenum);
866 if (*activep && *intptr == -1)
867 *intptr = value;
868 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000869
Damien Millere1776152005-03-01 21:47:37 +1100870 case oHashKnownHosts:
871 intptr = &options->hash_known_hosts;
872 goto parse_flag;
873
Damien Millerd27b9472005-12-13 19:29:02 +1100874 case oTunnel:
875 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100876 arg = strdelim(&s);
877 if (!arg || *arg == '\0')
878 fatal("%s line %d: Missing yes/point-to-point/"
879 "ethernet/no argument.", filename, linenum);
880 value = 0; /* silence compiler */
881 if (strcasecmp(arg, "ethernet") == 0)
882 value = SSH_TUNMODE_ETHERNET;
883 else if (strcasecmp(arg, "point-to-point") == 0)
884 value = SSH_TUNMODE_POINTOPOINT;
885 else if (strcasecmp(arg, "yes") == 0)
886 value = SSH_TUNMODE_DEFAULT;
887 else if (strcasecmp(arg, "no") == 0)
888 value = SSH_TUNMODE_NO;
889 else
890 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
891 "no argument: %s", filename, linenum, arg);
892 if (*activep)
893 *intptr = value;
894 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100895
896 case oTunnelDevice:
897 arg = strdelim(&s);
898 if (!arg || *arg == '\0')
899 fatal("%.200s line %d: Missing argument.", filename, linenum);
900 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100901 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100902 fatal("%.200s line %d: Bad tun device.", filename, linenum);
903 if (*activep) {
904 options->tun_local = value;
905 options->tun_remote = value2;
906 }
907 break;
908
909 case oLocalCommand:
910 charptr = &options->local_command;
911 goto parse_command;
912
913 case oPermitLocalCommand:
914 intptr = &options->permit_local_command;
915 goto parse_flag;
916
Ben Lindstrom4daea862002-06-09 20:04:02 +0000917 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000918 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000919 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000920 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000921
Damien Millerf9b3feb2003-05-16 11:38:32 +1000922 case oUnsupported:
923 error("%s line %d: Unsupported option \"%s\"",
924 filename, linenum, keyword);
925 return 0;
926
Damien Miller95def091999-11-25 00:26:21 +1100927 default:
928 fatal("process_config_line: Unimplemented opcode %d", opcode);
929 }
930
931 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000932 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000933 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000934 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000935 }
Damien Miller95def091999-11-25 00:26:21 +1100936 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937}
938
939
Damien Miller5428f641999-11-25 11:54:57 +1100940/*
941 * Reads the config file and modifies the options accordingly. Options
942 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000943 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100944 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000946int
Darren Tuckerfc959702004-07-17 16:12:08 +1000947read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000948 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949{
Damien Miller95def091999-11-25 00:26:21 +1100950 FILE *f;
951 char line[1024];
952 int active, linenum;
953 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954
Damien Miller95def091999-11-25 00:26:21 +1100955 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000956 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000957 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Damien Miller57a44762004-04-20 20:11:57 +1000959 if (checkperm) {
960 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000961
Damien Miller33793852004-06-15 10:27:55 +1000962 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000963 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000964 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000965 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000966 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000967 }
968
Damien Miller95def091999-11-25 00:26:21 +1100969 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Damien Miller5428f641999-11-25 11:54:57 +1100971 /*
972 * Mark that we are now processing the options. This flag is turned
973 * on/off by Host specifications.
974 */
Damien Miller95def091999-11-25 00:26:21 +1100975 active = 1;
976 linenum = 0;
977 while (fgets(line, sizeof(line), f)) {
978 /* Update line number counter. */
979 linenum++;
980 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
981 bad_options++;
982 }
983 fclose(f);
984 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000985 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100986 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000987 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988}
989
Damien Miller5428f641999-11-25 11:54:57 +1100990/*
991 * Initializes options to special values that indicate that they have not yet
992 * been set. Read_config_file will only set options with this value. Options
993 * are processed in the following order: command line, user config file,
994 * system config file. Last, fill_default_options is called.
995 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996
Damien Miller4af51302000-04-16 11:18:38 +1000997void
Damien Miller95def091999-11-25 00:26:21 +1100998initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999{
Damien Miller95def091999-11-25 00:26:21 +11001000 memset(options, 'X', sizeof(*options));
1001 options->forward_agent = -1;
1002 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001003 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001004 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001005 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001006 options->gateway_ports = -1;
1007 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001008 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001009 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001010 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001011 options->gss_authentication = -1;
1012 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001013 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001014 options->kbd_interactive_authentication = -1;
1015 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001016 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001017 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001018 options->batch_mode = -1;
1019 options->check_host_ip = -1;
1020 options->strict_host_key_checking = -1;
1021 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001022 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001023 options->compression_level = -1;
1024 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001025 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001026 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001027 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001028 options->number_of_password_prompts = -1;
1029 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001030 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001031 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001032 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001033 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001034 options->num_identity_files = 0;
1035 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001036 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001037 options->proxy_command = NULL;
1038 options->user = NULL;
1039 options->escape_char = -1;
1040 options->system_hostfile = NULL;
1041 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001042 options->system_hostfile2 = NULL;
1043 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001044 options->num_local_forwards = 0;
1045 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001046 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001047 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001048 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001049 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001050 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001051 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001052 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001053 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001054 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001055 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001056 options->server_alive_interval = -1;
1057 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001058 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001059 options->control_path = NULL;
1060 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001061 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001062 options->tun_open = -1;
1063 options->tun_local = -1;
1064 options->tun_remote = -1;
1065 options->local_command = NULL;
1066 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067}
1068
Damien Miller5428f641999-11-25 11:54:57 +11001069/*
1070 * Called after processing other sources of option data, this fills those
1071 * options for which no value has been specified with their default values.
1072 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073
Damien Miller4af51302000-04-16 11:18:38 +10001074void
Damien Miller95def091999-11-25 00:26:21 +11001075fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001077 int len;
1078
Damien Miller95def091999-11-25 00:26:21 +11001079 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001080 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001081 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001082 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001083 if (options->forward_x11_trusted == -1)
1084 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001085 if (options->exit_on_forward_failure == -1)
1086 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001087 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001088 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001089 if (options->gateway_ports == -1)
1090 options->gateway_ports = 0;
1091 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001092 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001093 if (options->rsa_authentication == -1)
1094 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001095 if (options->pubkey_authentication == -1)
1096 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001097 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001098 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001099 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001100 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001101 if (options->gss_deleg_creds == -1)
1102 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001103 if (options->password_authentication == -1)
1104 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001105 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001106 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001107 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001108 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001109 if (options->hostbased_authentication == -1)
1110 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001111 if (options->batch_mode == -1)
1112 options->batch_mode = 0;
1113 if (options->check_host_ip == -1)
1114 options->check_host_ip = 1;
1115 if (options->strict_host_key_checking == -1)
1116 options->strict_host_key_checking = 2; /* 2 is default */
1117 if (options->compression == -1)
1118 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001119 if (options->tcp_keep_alive == -1)
1120 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001121 if (options->compression_level == -1)
1122 options->compression_level = 6;
1123 if (options->port == -1)
1124 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001125 if (options->address_family == -1)
1126 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001127 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001128 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001129 if (options->number_of_password_prompts == -1)
1130 options->number_of_password_prompts = 3;
1131 /* Selected in ssh_login(). */
1132 if (options->cipher == -1)
1133 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001134 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001135 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001136 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001137 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001138 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001139 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001140 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001141 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001142 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001143 xmalloc(len);
1144 snprintf(options->identity_files[options->num_identity_files++],
1145 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001146 }
1147 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001148 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1149 options->identity_files[options->num_identity_files] =
1150 xmalloc(len);
1151 snprintf(options->identity_files[options->num_identity_files++],
1152 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1153
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001154 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001155 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001156 xmalloc(len);
1157 snprintf(options->identity_files[options->num_identity_files++],
1158 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001159 }
Damien Millereba71ba2000-04-29 23:57:08 +10001160 }
Damien Miller95def091999-11-25 00:26:21 +11001161 if (options->escape_char == -1)
1162 options->escape_char = '~';
1163 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001164 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001165 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001166 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001167 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001168 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001169 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001170 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001171 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001172 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001173 if (options->clear_forwardings == 1)
1174 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001175 if (options->no_host_authentication_for_localhost == - 1)
1176 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001177 if (options->identities_only == -1)
1178 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001179 if (options->enable_ssh_keysign == -1)
1180 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001181 if (options->rekey_limit == -1)
1182 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001183 if (options->verify_host_key_dns == -1)
1184 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001185 if (options->server_alive_interval == -1)
1186 options->server_alive_interval = 0;
1187 if (options->server_alive_count_max == -1)
1188 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001189 if (options->control_master == -1)
1190 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001191 if (options->hash_known_hosts == -1)
1192 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001193 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001194 options->tun_open = SSH_TUNMODE_NO;
1195 if (options->tun_local == -1)
1196 options->tun_local = SSH_TUNID_ANY;
1197 if (options->tun_remote == -1)
1198 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001199 if (options->permit_local_command == -1)
1200 options->permit_local_command = 0;
1201 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001202 /* options->proxy_command should not be set by default */
1203 /* options->user will be set in the main program if appropriate */
1204 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001205 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001206 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001208
1209/*
1210 * parse_forward
1211 * parses a string containing a port forwarding specification of the form:
1212 * [listenhost:]listenport:connecthost:connectport
1213 * returns number of arguments parsed or zero on error
1214 */
1215int
1216parse_forward(Forward *fwd, const char *fwdspec)
1217{
1218 int i;
1219 char *p, *cp, *fwdarg[4];
1220
1221 memset(fwd, '\0', sizeof(*fwd));
1222
1223 cp = p = xstrdup(fwdspec);
1224
1225 /* skip leading spaces */
1226 while (*cp && isspace(*cp))
1227 cp++;
1228
1229 for (i = 0; i < 4; ++i)
1230 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1231 break;
1232
1233 /* Check for trailing garbage in 4-arg case*/
1234 if (cp != NULL)
1235 i = 0; /* failure */
1236
1237 switch (i) {
1238 case 3:
1239 fwd->listen_host = NULL;
1240 fwd->listen_port = a2port(fwdarg[0]);
1241 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1242 fwd->connect_port = a2port(fwdarg[2]);
1243 break;
1244
1245 case 4:
1246 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1247 fwd->listen_port = a2port(fwdarg[1]);
1248 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1249 fwd->connect_port = a2port(fwdarg[3]);
1250 break;
1251 default:
1252 i = 0; /* failure */
1253 }
1254
1255 xfree(p);
1256
1257 if (fwd->listen_port == 0 && fwd->connect_port == 0)
1258 goto fail_free;
1259
1260 if (fwd->connect_host != NULL &&
1261 strlen(fwd->connect_host) >= NI_MAXHOST)
1262 goto fail_free;
1263
1264 return (i);
1265
1266 fail_free:
1267 if (fwd->connect_host != NULL)
1268 xfree(fwd->connect_host);
1269 if (fwd->listen_host != NULL)
1270 xfree(fwd->listen_host);
1271 return (0);
1272}