blob: 1d61145c4edbb00d704db095c543c5c0e711bc50 [file] [log] [blame]
Darren Tucker9c16ac92008-06-13 04:40:35 +10001/* $OpenBSD: readconf.c,v 1.166 2008/06/11 21:01:35 grunk 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 Millerded319c2006-09-01 15:38:36 +100027#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100028#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110031
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
36#include "pathnames.h"
37#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "readconf.h"
40#include "match.h"
41#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043#include "kex.h"
44#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46/* Format of the configuration file:
47
48 # Configuration data is parsed as follows:
49 # 1. command line options
50 # 2. user-specific file
51 # 3. system-wide file
52 # Any configuration value is only changed the first time it is set.
53 # Thus, host-specific definitions should be at the beginning of the
54 # configuration file, and defaults at the end.
55
56 # Host-specific declarations. These may override anything above. A single
57 # host may match multiple declarations; these are processed in the order
58 # that they are given in.
59
60 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000061 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63 Host fake.com
64 HostName another.host.name.real.org
65 User blaah
66 Port 34289
67 ForwardX11 no
68 ForwardAgent no
69
70 Host books.com
71 RemoteForward 9999 shadows.cs.hut.fi:9999
72 Cipher 3des
73
74 Host fascist.blob.com
75 Port 23123
76 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication no
78
79 Host puukko.hut.fi
80 User t35124p
81 ProxyCommand ssh-proxy %h %p
82
83 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000084 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86 Host *.su
87 Cipher none
88 PasswordAuthentication no
89
Damien Millerd27b9472005-12-13 19:29:02 +110090 Host vpn.fake.com
91 Tunnel yes
92 TunnelDevice 3
93
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 # Defaults for various options
95 Host *
96 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 PasswordAuthentication yes
99 RSAAuthentication yes
100 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100102 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 IdentityFile ~/.ssh/identity
104 Port 22
105 EscapeChar ~
106
107*/
108
109/* Keyword tokens. */
110
Damien Miller95def091999-11-25 00:26:21 +1100111typedef enum {
112 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000113 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000114 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000115 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000116 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100117 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
118 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
119 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
120 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100121 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000122 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000124 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000125 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000126 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000127 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000128 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000129 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100130 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100131 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100132 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000133 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134} OpCodes;
135
136/* Textual representations of the tokens. */
137
Damien Miller95def091999-11-25 00:26:21 +1100138static struct {
139 const char *name;
140 OpCodes opcode;
141} keywords[] = {
142 { "forwardagent", oForwardAgent },
143 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000144 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000145 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000146 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100147 { "gatewayports", oGatewayPorts },
148 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000149 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100150 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100151 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
152 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100153 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100154 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000155 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000156 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000157 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000158 { "challengeresponseauthentication", oChallengeResponseAuthentication },
159 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
160 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000161 { "kerberosauthentication", oUnsupported },
162 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000163 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000164#if defined(GSSAPI)
165 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000166 { "gssapidelegatecredentials", oGssDelegateCreds },
167#else
168 { "gssapiauthentication", oUnsupported },
169 { "gssapidelegatecredentials", oUnsupported },
170#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000171 { "fallbacktorsh", oDeprecated },
172 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100174 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100175 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100176 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000177 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100178 { "proxycommand", oProxyCommand },
179 { "port", oPort },
180 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000181 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000182 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000183 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100184 { "remoteforward", oRemoteForward },
185 { "localforward", oLocalForward },
186 { "user", oUser },
187 { "host", oHost },
188 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100189 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000190 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000191 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000192 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100193 { "connectionattempts", oConnectionAttempts },
194 { "batchmode", oBatchMode },
195 { "checkhostip", oCheckHostIP },
196 { "stricthostkeychecking", oStrictHostKeyChecking },
197 { "compression", oCompression },
198 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100199 { "tcpkeepalive", oTCPKeepAlive },
200 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100201 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100202 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000203 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000204 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000205 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000206 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000207#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000208 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000209#else
210 { "smartcarddevice", oUnsupported },
211#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100212 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000213 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000214 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100215 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000216 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000217 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000218 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100219 { "serveraliveinterval", oServerAliveInterval },
220 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000221 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000222 { "controlpath", oControlPath },
223 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100224 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100225 { "tunnel", oTunnel },
226 { "tunneldevice", oTunnelDevice },
227 { "localcommand", oLocalCommand },
228 { "permitlocalcommand", oPermitLocalCommand },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000229 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100230};
231
Damien Miller5428f641999-11-25 11:54:57 +1100232/*
233 * Adds a local TCP/IP port forward to options. Never returns if there is an
234 * error.
235 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Miller4af51302000-04-16 11:18:38 +1000237void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100238add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Damien Miller95def091999-11-25 00:26:21 +1100240 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000241#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100242 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100243 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000244 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100245#endif
Damien Miller95def091999-11-25 00:26:21 +1100246 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
247 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
248 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100249
250 fwd->listen_host = (newfwd->listen_host == NULL) ?
251 NULL : xstrdup(newfwd->listen_host);
252 fwd->listen_port = newfwd->listen_port;
253 fwd->connect_host = xstrdup(newfwd->connect_host);
254 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255}
256
Damien Miller5428f641999-11-25 11:54:57 +1100257/*
258 * Adds a remote TCP/IP port forward to options. Never returns if there is
259 * an error.
260 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller4af51302000-04-16 11:18:38 +1000262void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264{
Damien Miller95def091999-11-25 00:26:21 +1100265 Forward *fwd;
266 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
267 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100268 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100269 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100270
271 fwd->listen_host = (newfwd->listen_host == NULL) ?
272 NULL : xstrdup(newfwd->listen_host);
273 fwd->listen_port = newfwd->listen_port;
274 fwd->connect_host = xstrdup(newfwd->connect_host);
275 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
277
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000278static void
279clear_forwardings(Options *options)
280{
281 int i;
282
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100284 if (options->local_forwards[i].listen_host != NULL)
285 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100286 xfree(options->local_forwards[i].connect_host);
287 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000288 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100289 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100290 if (options->remote_forwards[i].listen_host != NULL)
291 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100292 xfree(options->remote_forwards[i].connect_host);
293 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000294 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100295 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000296}
297
Damien Miller5428f641999-11-25 11:54:57 +1100298/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000299 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100300 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301
Damien Miller4af51302000-04-16 11:18:38 +1000302static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100303parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000305 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306
Damien Miller95def091999-11-25 00:26:21 +1100307 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100308 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100309 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000311 error("%s: line %d: Bad configuration option: %s",
312 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100313 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314}
315
Damien Miller5428f641999-11-25 11:54:57 +1100316/*
317 * Processes a single option line as used in the configuration files. This
318 * only sets those values that have not already been set.
319 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100320#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
Damien Miller2ccf6611999-11-15 15:25:10 +1100322int
323process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100324 char *line, const char *filename, int linenum,
325 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100327 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100328 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100329 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100330 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100331 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100332 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
Damien Millerc652cac2003-05-14 13:40:54 +1000334 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100335 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000336 if (strchr(WHITESPACE, line[len]) == NULL)
337 break;
338 line[len] = '\0';
339 }
340
Damien Millerbe484b52000-07-15 14:14:16 +1000341 s = line;
342 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100343 if ((keyword = strdelim(&s)) == NULL)
344 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000345 /* Ignore leading whitespace. */
346 if (*keyword == '\0')
347 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000348 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100349 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller37023962000-07-11 17:31:38 +1000351 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller95def091999-11-25 00:26:21 +1100353 switch (opcode) {
354 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100355 /* don't panic, but count bad options */
356 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100357 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000358 case oConnectTimeout:
359 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100360parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000361 arg = strdelim(&s);
362 if (!arg || *arg == '\0')
363 fatal("%s line %d: missing time value.",
364 filename, linenum);
365 if ((value = convtime(arg)) == -1)
366 fatal("%s line %d: invalid time value.",
367 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100368 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000369 *intptr = value;
370 break;
371
Damien Miller95def091999-11-25 00:26:21 +1100372 case oForwardAgent:
373 intptr = &options->forward_agent;
374parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000375 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000376 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100377 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
378 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000379 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100380 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000381 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100382 value = 0;
383 else
384 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
385 if (*activep && *intptr == -1)
386 *intptr = value;
387 break;
388
389 case oForwardX11:
390 intptr = &options->forward_x11;
391 goto parse_flag;
392
Darren Tucker0a118da2003-10-15 15:54:32 +1000393 case oForwardX11Trusted:
394 intptr = &options->forward_x11_trusted;
395 goto parse_flag;
396
Damien Miller95def091999-11-25 00:26:21 +1100397 case oGatewayPorts:
398 intptr = &options->gateway_ports;
399 goto parse_flag;
400
Darren Tuckere7d4b192006-07-12 22:17:10 +1000401 case oExitOnForwardFailure:
402 intptr = &options->exit_on_forward_failure;
403 goto parse_flag;
404
Damien Miller95def091999-11-25 00:26:21 +1100405 case oUsePrivilegedPort:
406 intptr = &options->use_privileged_port;
407 goto parse_flag;
408
Damien Miller95def091999-11-25 00:26:21 +1100409 case oPasswordAuthentication:
410 intptr = &options->password_authentication;
411 goto parse_flag;
412
Damien Miller874d77b2000-10-14 16:23:11 +1100413 case oKbdInteractiveAuthentication:
414 intptr = &options->kbd_interactive_authentication;
415 goto parse_flag;
416
417 case oKbdInteractiveDevices:
418 charptr = &options->kbd_interactive_devices;
419 goto parse_string;
420
Damien Miller0bc1bd82000-11-13 22:57:25 +1100421 case oPubkeyAuthentication:
422 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000423 goto parse_flag;
424
Damien Miller95def091999-11-25 00:26:21 +1100425 case oRSAAuthentication:
426 intptr = &options->rsa_authentication;
427 goto parse_flag;
428
429 case oRhostsRSAAuthentication:
430 intptr = &options->rhosts_rsa_authentication;
431 goto parse_flag;
432
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000433 case oHostbasedAuthentication:
434 intptr = &options->hostbased_authentication;
435 goto parse_flag;
436
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000437 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000438 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100439 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000440
Darren Tucker0efd1552003-08-26 11:49:55 +1000441 case oGssAuthentication:
442 intptr = &options->gss_authentication;
443 goto parse_flag;
444
445 case oGssDelegateCreds:
446 intptr = &options->gss_deleg_creds;
447 goto parse_flag;
448
Damien Miller95def091999-11-25 00:26:21 +1100449 case oBatchMode:
450 intptr = &options->batch_mode;
451 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452
Damien Miller95def091999-11-25 00:26:21 +1100453 case oCheckHostIP:
454 intptr = &options->check_host_ip;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000455 arg = strdelim(&s);
456 if (!arg || *arg == '\0')
457 fatal("%.200s line %d: Missing CheckHostIP argument.",
458 filename, linenum);
459 value = 0; /* To avoid compiler warning... */
460 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
461 value = SSHCTL_CHECKHOSTIP_YES;
462 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
463 value = SSHCTL_CHECKHOSTIP_NO;
464 else if (strcmp(arg, "fingerprint") == 0)
465 value = SSHCTL_CHECKHOSTIP_FPR;
466 else
467 fatal("%.200s line %d: Bad CheckHostIP argument.",
468 filename, linenum);
469 if (*activep && *intptr == -1)
470 *intptr = value;
471 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472
Damien Miller37876e92003-05-15 10:19:46 +1000473 case oVerifyHostKeyDNS:
474 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100475 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000476
Damien Miller95def091999-11-25 00:26:21 +1100477 case oStrictHostKeyChecking:
478 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100479parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000480 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000481 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000482 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100483 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100484 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000485 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100486 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000487 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100488 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000489 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100490 value = 2;
491 else
492 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
493 if (*activep && *intptr == -1)
494 *intptr = value;
495 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
Damien Miller95def091999-11-25 00:26:21 +1100497 case oCompression:
498 intptr = &options->compression;
499 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller12c150e2003-12-17 16:31:10 +1100501 case oTCPKeepAlive:
502 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100503 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000505 case oNoHostAuthenticationForLocalhost:
506 intptr = &options->no_host_authentication_for_localhost;
507 goto parse_flag;
508
Damien Miller95def091999-11-25 00:26:21 +1100509 case oNumberOfPasswordPrompts:
510 intptr = &options->number_of_password_prompts;
511 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller95def091999-11-25 00:26:21 +1100513 case oCompressionLevel:
514 intptr = &options->compression_level;
515 goto parse_int;
516
Damien Millera5539d22003-04-09 20:50:06 +1000517 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000518 arg = strdelim(&s);
519 if (!arg || *arg == '\0')
520 fatal("%.200s line %d: Missing argument.", filename, linenum);
521 if (arg[0] < '0' || arg[0] > '9')
522 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100523 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000524 if (arg == endofnumber)
525 fatal("%.200s line %d: Bad number.", filename, linenum);
526 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100527 case '\0':
528 scale = 1;
529 break;
Damien Millera5539d22003-04-09 20:50:06 +1000530 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100531 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000532 break;
533 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100534 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000535 break;
536 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100537 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000538 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100539 default:
540 fatal("%.200s line %d: Invalid RekeyLimit suffix",
541 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000542 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100543 val64 *= scale;
544 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100545 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100546 fatal("%.200s line %d: RekeyLimit too large",
547 filename, linenum);
548 if (val64 < 16)
549 fatal("%.200s line %d: RekeyLimit too small",
550 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100551 if (*activep && options->rekey_limit == -1)
552 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000553 break;
554
Damien Miller95def091999-11-25 00:26:21 +1100555 case oIdentityFile:
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) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100560 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000561 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100562 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100563 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100564 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000565 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000566 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100567 }
568 break;
569
Damien Millerd3a18572000-06-07 19:55:44 +1000570 case oXAuthLocation:
571 charptr=&options->xauth_location;
572 goto parse_string;
573
Damien Miller95def091999-11-25 00:26:21 +1100574 case oUser:
575 charptr = &options->user;
576parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000577 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000578 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100579 fatal("%.200s line %d: Missing argument.", filename, linenum);
580 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000581 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100582 break;
583
584 case oGlobalKnownHostsFile:
585 charptr = &options->system_hostfile;
586 goto parse_string;
587
588 case oUserKnownHostsFile:
589 charptr = &options->user_hostfile;
590 goto parse_string;
591
Damien Millereba71ba2000-04-29 23:57:08 +1000592 case oGlobalKnownHostsFile2:
593 charptr = &options->system_hostfile2;
594 goto parse_string;
595
596 case oUserKnownHostsFile2:
597 charptr = &options->user_hostfile2;
598 goto parse_string;
599
Damien Miller95def091999-11-25 00:26:21 +1100600 case oHostName:
601 charptr = &options->hostname;
602 goto parse_string;
603
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000604 case oHostKeyAlias:
605 charptr = &options->host_key_alias;
606 goto parse_string;
607
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000608 case oPreferredAuthentications:
609 charptr = &options->preferred_authentications;
610 goto parse_string;
611
Ben Lindstrome0f88042001-04-30 13:06:24 +0000612 case oBindAddress:
613 charptr = &options->bind_address;
614 goto parse_string;
615
Ben Lindstromae996bf2001-08-06 21:27:53 +0000616 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000617 charptr = &options->smartcard_device;
618 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000619
Damien Miller95def091999-11-25 00:26:21 +1100620 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100621 charptr = &options->proxy_command;
622parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000623 if (s == NULL)
624 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100625 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100626 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100627 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100628 return 0;
629
630 case oPort:
631 intptr = &options->port;
632parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000633 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000634 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100635 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000636 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100637 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100638
639 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000640 value = strtol(arg, &endofnumber, 0);
641 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100642 fatal("%.200s line %d: Bad number.", filename, linenum);
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 Miller95def091999-11-25 00:26:21 +1100647 case oConnectionAttempts:
648 intptr = &options->connection_attempts;
649 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100650
Damien Miller95def091999-11-25 00:26:21 +1100651 case oCipher:
652 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000653 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000654 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000655 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000656 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100657 if (value == -1)
658 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100659 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100660 if (*activep && *intptr == -1)
661 *intptr = value;
662 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663
Damien Miller78928792000-04-12 20:17:38 +1000664 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000665 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000666 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000667 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000668 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000669 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100670 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000671 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000672 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000673 break;
674
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000675 case oMacs:
676 arg = strdelim(&s);
677 if (!arg || *arg == '\0')
678 fatal("%.200s line %d: Missing argument.", filename, linenum);
679 if (!mac_valid(arg))
680 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100681 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000682 if (*activep && options->macs == NULL)
683 options->macs = xstrdup(arg);
684 break;
685
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000686 case oHostKeyAlgorithms:
687 arg = strdelim(&s);
688 if (!arg || *arg == '\0')
689 fatal("%.200s line %d: Missing argument.", filename, linenum);
690 if (!key_names_valid2(arg))
691 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100692 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000693 if (*activep && options->hostkeyalgorithms == NULL)
694 options->hostkeyalgorithms = xstrdup(arg);
695 break;
696
Damien Miller78928792000-04-12 20:17:38 +1000697 case oProtocol:
698 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000699 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000700 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000701 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000702 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000703 if (value == SSH_PROTO_UNKNOWN)
704 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100705 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000706 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
707 *intptr = value;
708 break;
709
Damien Miller95def091999-11-25 00:26:21 +1100710 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100711 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000712 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000713 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100714 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000715 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100716 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100717 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
718 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100719 break;
720
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000721 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100722 case oRemoteForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000723 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100724 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000725 fatal("%.200s line %d: Missing port argument.",
726 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100727 arg2 = strdelim(&s);
728 if (arg2 == NULL || *arg2 == '\0')
729 fatal("%.200s line %d: Missing target argument.",
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000730 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100731
732 /* construct a string for parse_forward */
733 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
734
735 if (parse_forward(&fwd, fwdarg) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000736 fatal("%.200s line %d: Bad forwarding specification.",
737 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100738
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000739 if (*activep) {
740 if (opcode == oLocalForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100741 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000742 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100743 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000744 }
Damien Miller95def091999-11-25 00:26:21 +1100745 break;
746
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000747 case oDynamicForward:
748 arg = strdelim(&s);
749 if (!arg || *arg == '\0')
750 fatal("%.200s line %d: Missing port argument.",
751 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100752 memset(&fwd, '\0', sizeof(fwd));
753 fwd.connect_host = "socks";
754 fwd.listen_host = hpdelim(&arg);
755 if (fwd.listen_host == NULL ||
756 strlen(fwd.listen_host) >= NI_MAXHOST)
757 fatal("%.200s line %d: Bad forwarding specification.",
758 filename, linenum);
759 if (arg) {
760 fwd.listen_port = a2port(arg);
761 fwd.listen_host = cleanhostname(fwd.listen_host);
762 } else {
763 fwd.listen_port = a2port(fwd.listen_host);
Damien Miller43f6db62005-08-12 22:11:18 +1000764 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100765 }
766 if (fwd.listen_port == 0)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000767 fatal("%.200s line %d: Badly formatted port number.",
768 filename, linenum);
Ben Lindstrom525a0932001-09-12 17:35:27 +0000769 if (*activep)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100770 add_local_forward(options, &fwd);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000771 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000772
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000773 case oClearAllForwardings:
774 intptr = &options->clear_forwardings;
775 goto parse_flag;
776
Damien Miller95def091999-11-25 00:26:21 +1100777 case oHost:
778 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000779 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000780 if (match_pattern(host, arg)) {
781 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100782 *activep = 1;
783 break;
784 }
Damien Millerbe484b52000-07-15 14:14:16 +1000785 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100786 return 0;
787
788 case oEscapeChar:
789 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000790 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000791 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100792 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000793 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000794 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
795 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000796 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000797 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000798 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000799 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100800 else {
801 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100802 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100803 /* NOTREACHED */
804 value = 0; /* Avoid compiler warning. */
805 }
806 if (*activep && *intptr == -1)
807 *intptr = value;
808 break;
809
Damien Miller20a8f972003-05-18 20:50:30 +1000810 case oAddressFamily:
811 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000812 if (!arg || *arg == '\0')
813 fatal("%s line %d: missing address family.",
814 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000815 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000816 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000817 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000818 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000819 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000820 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000821 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000822 else
823 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000824 if (*activep && *intptr == -1)
825 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000826 break;
827
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000828 case oEnableSSHKeysign:
829 intptr = &options->enable_ssh_keysign;
830 goto parse_flag;
831
Damien Millerbd394c32004-03-08 23:12:36 +1100832 case oIdentitiesOnly:
833 intptr = &options->identities_only;
834 goto parse_flag;
835
Damien Miller509b0102003-12-17 16:33:10 +1100836 case oServerAliveInterval:
837 intptr = &options->server_alive_interval;
838 goto parse_time;
839
840 case oServerAliveCountMax:
841 intptr = &options->server_alive_count_max;
842 goto parse_int;
843
Darren Tucker46bc0752004-05-02 22:11:30 +1000844 case oSendEnv:
845 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
846 if (strchr(arg, '=') != NULL)
847 fatal("%s line %d: Invalid environment name.",
848 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100849 if (!*activep)
850 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000851 if (options->num_send_env >= MAX_SEND_ENV)
852 fatal("%s line %d: too many send env.",
853 filename, linenum);
854 options->send_env[options->num_send_env++] =
855 xstrdup(arg);
856 }
857 break;
858
Damien Miller0e220db2004-06-15 10:34:08 +1000859 case oControlPath:
860 charptr = &options->control_path;
861 goto parse_string;
862
863 case oControlMaster:
864 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000865 arg = strdelim(&s);
866 if (!arg || *arg == '\0')
867 fatal("%.200s line %d: Missing ControlMaster argument.",
868 filename, linenum);
869 value = 0; /* To avoid compiler warning... */
870 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
871 value = SSHCTL_MASTER_YES;
872 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
873 value = SSHCTL_MASTER_NO;
874 else if (strcmp(arg, "auto") == 0)
875 value = SSHCTL_MASTER_AUTO;
876 else if (strcmp(arg, "ask") == 0)
877 value = SSHCTL_MASTER_ASK;
878 else if (strcmp(arg, "autoask") == 0)
879 value = SSHCTL_MASTER_AUTO_ASK;
880 else
881 fatal("%.200s line %d: Bad ControlMaster argument.",
882 filename, linenum);
883 if (*activep && *intptr == -1)
884 *intptr = value;
885 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000886
Damien Millere1776152005-03-01 21:47:37 +1100887 case oHashKnownHosts:
888 intptr = &options->hash_known_hosts;
889 goto parse_flag;
890
Damien Millerd27b9472005-12-13 19:29:02 +1100891 case oTunnel:
892 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100893 arg = strdelim(&s);
894 if (!arg || *arg == '\0')
895 fatal("%s line %d: Missing yes/point-to-point/"
896 "ethernet/no argument.", filename, linenum);
897 value = 0; /* silence compiler */
898 if (strcasecmp(arg, "ethernet") == 0)
899 value = SSH_TUNMODE_ETHERNET;
900 else if (strcasecmp(arg, "point-to-point") == 0)
901 value = SSH_TUNMODE_POINTOPOINT;
902 else if (strcasecmp(arg, "yes") == 0)
903 value = SSH_TUNMODE_DEFAULT;
904 else if (strcasecmp(arg, "no") == 0)
905 value = SSH_TUNMODE_NO;
906 else
907 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
908 "no argument: %s", filename, linenum, arg);
909 if (*activep)
910 *intptr = value;
911 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100912
913 case oTunnelDevice:
914 arg = strdelim(&s);
915 if (!arg || *arg == '\0')
916 fatal("%.200s line %d: Missing argument.", filename, linenum);
917 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100918 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100919 fatal("%.200s line %d: Bad tun device.", filename, linenum);
920 if (*activep) {
921 options->tun_local = value;
922 options->tun_remote = value2;
923 }
924 break;
925
926 case oLocalCommand:
927 charptr = &options->local_command;
928 goto parse_command;
929
930 case oPermitLocalCommand:
931 intptr = &options->permit_local_command;
932 goto parse_flag;
933
Ben Lindstrom4daea862002-06-09 20:04:02 +0000934 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000935 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000936 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000937 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000938
Damien Millerf9b3feb2003-05-16 11:38:32 +1000939 case oUnsupported:
940 error("%s line %d: Unsupported option \"%s\"",
941 filename, linenum, keyword);
942 return 0;
943
Damien Miller95def091999-11-25 00:26:21 +1100944 default:
945 fatal("process_config_line: Unimplemented opcode %d", opcode);
946 }
947
948 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000949 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000950 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000951 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000952 }
Damien Miller95def091999-11-25 00:26:21 +1100953 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954}
955
956
Damien Miller5428f641999-11-25 11:54:57 +1100957/*
958 * Reads the config file and modifies the options accordingly. Options
959 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000960 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100961 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000963int
Darren Tuckerfc959702004-07-17 16:12:08 +1000964read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000965 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966{
Damien Miller95def091999-11-25 00:26:21 +1100967 FILE *f;
968 char line[1024];
969 int active, linenum;
970 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Damien Miller95def091999-11-25 00:26:21 +1100972 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000973 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000974 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Damien Miller57a44762004-04-20 20:11:57 +1000976 if (checkperm) {
977 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000978
Damien Miller33793852004-06-15 10:27:55 +1000979 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000980 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000981 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000982 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000983 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000984 }
985
Damien Miller95def091999-11-25 00:26:21 +1100986 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller5428f641999-11-25 11:54:57 +1100988 /*
989 * Mark that we are now processing the options. This flag is turned
990 * on/off by Host specifications.
991 */
Damien Miller95def091999-11-25 00:26:21 +1100992 active = 1;
993 linenum = 0;
994 while (fgets(line, sizeof(line), f)) {
995 /* Update line number counter. */
996 linenum++;
997 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
998 bad_options++;
999 }
1000 fclose(f);
1001 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001002 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001003 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001004 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005}
1006
Damien Miller5428f641999-11-25 11:54:57 +11001007/*
1008 * Initializes options to special values that indicate that they have not yet
1009 * been set. Read_config_file will only set options with this value. Options
1010 * are processed in the following order: command line, user config file,
1011 * system config file. Last, fill_default_options is called.
1012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
Damien Miller4af51302000-04-16 11:18:38 +10001014void
Damien Miller95def091999-11-25 00:26:21 +11001015initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016{
Damien Miller95def091999-11-25 00:26:21 +11001017 memset(options, 'X', sizeof(*options));
1018 options->forward_agent = -1;
1019 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001020 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001021 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001022 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001023 options->gateway_ports = -1;
1024 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001025 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001026 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001027 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001028 options->gss_authentication = -1;
1029 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001030 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001031 options->kbd_interactive_authentication = -1;
1032 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001033 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001034 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001035 options->batch_mode = -1;
1036 options->check_host_ip = -1;
1037 options->strict_host_key_checking = -1;
1038 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001039 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001040 options->compression_level = -1;
1041 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001042 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001043 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001044 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001045 options->number_of_password_prompts = -1;
1046 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001047 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001048 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001049 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001050 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001051 options->num_identity_files = 0;
1052 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001053 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001054 options->proxy_command = NULL;
1055 options->user = NULL;
1056 options->escape_char = -1;
1057 options->system_hostfile = NULL;
1058 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001059 options->system_hostfile2 = NULL;
1060 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001061 options->num_local_forwards = 0;
1062 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001063 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001064 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001065 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001066 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001067 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001068 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001069 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001070 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001071 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001072 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001073 options->server_alive_interval = -1;
1074 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001075 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001076 options->control_path = NULL;
1077 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001078 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001079 options->tun_open = -1;
1080 options->tun_local = -1;
1081 options->tun_remote = -1;
1082 options->local_command = NULL;
1083 options->permit_local_command = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001084}
1085
Damien Miller5428f641999-11-25 11:54:57 +11001086/*
1087 * Called after processing other sources of option data, this fills those
1088 * options for which no value has been specified with their default values.
1089 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090
Damien Miller4af51302000-04-16 11:18:38 +10001091void
Damien Miller95def091999-11-25 00:26:21 +11001092fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001094 int len;
1095
Damien Miller95def091999-11-25 00:26:21 +11001096 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001097 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001098 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001099 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001100 if (options->forward_x11_trusted == -1)
1101 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001102 if (options->exit_on_forward_failure == -1)
1103 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001104 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001105 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001106 if (options->gateway_ports == -1)
1107 options->gateway_ports = 0;
1108 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001109 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001110 if (options->rsa_authentication == -1)
1111 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001112 if (options->pubkey_authentication == -1)
1113 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001114 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001115 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001116 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001117 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001118 if (options->gss_deleg_creds == -1)
1119 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001120 if (options->password_authentication == -1)
1121 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001122 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001123 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001124 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001125 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001126 if (options->hostbased_authentication == -1)
1127 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001128 if (options->batch_mode == -1)
1129 options->batch_mode = 0;
1130 if (options->check_host_ip == -1)
1131 options->check_host_ip = 1;
1132 if (options->strict_host_key_checking == -1)
1133 options->strict_host_key_checking = 2; /* 2 is default */
1134 if (options->compression == -1)
1135 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001136 if (options->tcp_keep_alive == -1)
1137 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001138 if (options->compression_level == -1)
1139 options->compression_level = 6;
1140 if (options->port == -1)
1141 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001142 if (options->address_family == -1)
1143 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001144 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001145 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001146 if (options->number_of_password_prompts == -1)
1147 options->number_of_password_prompts = 3;
1148 /* Selected in ssh_login(). */
1149 if (options->cipher == -1)
1150 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001151 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001152 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001153 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001154 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001155 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001156 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001157 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001158 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001159 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001160 xmalloc(len);
1161 snprintf(options->identity_files[options->num_identity_files++],
1162 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001163 }
1164 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001165 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1166 options->identity_files[options->num_identity_files] =
1167 xmalloc(len);
1168 snprintf(options->identity_files[options->num_identity_files++],
1169 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1170
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001171 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001172 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001173 xmalloc(len);
1174 snprintf(options->identity_files[options->num_identity_files++],
1175 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001176 }
Damien Millereba71ba2000-04-29 23:57:08 +10001177 }
Damien Miller95def091999-11-25 00:26:21 +11001178 if (options->escape_char == -1)
1179 options->escape_char = '~';
1180 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001181 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001182 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001183 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001184 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001185 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001186 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001187 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001188 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001189 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001190 if (options->clear_forwardings == 1)
1191 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001192 if (options->no_host_authentication_for_localhost == - 1)
1193 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001194 if (options->identities_only == -1)
1195 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001196 if (options->enable_ssh_keysign == -1)
1197 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001198 if (options->rekey_limit == -1)
1199 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001200 if (options->verify_host_key_dns == -1)
1201 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001202 if (options->server_alive_interval == -1)
1203 options->server_alive_interval = 0;
1204 if (options->server_alive_count_max == -1)
1205 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001206 if (options->control_master == -1)
1207 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001208 if (options->hash_known_hosts == -1)
1209 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001210 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001211 options->tun_open = SSH_TUNMODE_NO;
1212 if (options->tun_local == -1)
1213 options->tun_local = SSH_TUNID_ANY;
1214 if (options->tun_remote == -1)
1215 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001216 if (options->permit_local_command == -1)
1217 options->permit_local_command = 0;
1218 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001219 /* options->proxy_command should not be set by default */
1220 /* options->user will be set in the main program if appropriate */
1221 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001222 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001223 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001224}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001225
1226/*
1227 * parse_forward
1228 * parses a string containing a port forwarding specification of the form:
1229 * [listenhost:]listenport:connecthost:connectport
1230 * returns number of arguments parsed or zero on error
1231 */
1232int
1233parse_forward(Forward *fwd, const char *fwdspec)
1234{
1235 int i;
1236 char *p, *cp, *fwdarg[4];
1237
1238 memset(fwd, '\0', sizeof(*fwd));
1239
1240 cp = p = xstrdup(fwdspec);
1241
1242 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001243 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001244 cp++;
1245
1246 for (i = 0; i < 4; ++i)
1247 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1248 break;
1249
1250 /* Check for trailing garbage in 4-arg case*/
1251 if (cp != NULL)
1252 i = 0; /* failure */
1253
1254 switch (i) {
1255 case 3:
1256 fwd->listen_host = NULL;
1257 fwd->listen_port = a2port(fwdarg[0]);
1258 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1259 fwd->connect_port = a2port(fwdarg[2]);
1260 break;
1261
1262 case 4:
1263 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1264 fwd->listen_port = a2port(fwdarg[1]);
1265 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1266 fwd->connect_port = a2port(fwdarg[3]);
1267 break;
1268 default:
1269 i = 0; /* failure */
1270 }
1271
1272 xfree(p);
1273
Damien Miller5a4456c2007-10-26 14:27:22 +10001274 if (fwd->listen_port == 0 || fwd->connect_port == 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001275 goto fail_free;
1276
1277 if (fwd->connect_host != NULL &&
1278 strlen(fwd->connect_host) >= NI_MAXHOST)
1279 goto fail_free;
1280
1281 return (i);
1282
1283 fail_free:
1284 if (fwd->connect_host != NULL)
1285 xfree(fwd->connect_host);
1286 if (fwd->listen_host != NULL)
1287 xfree(fwd->listen_host);
1288 return (0);
1289}