blob: e0386935f43c44b1f1f90cbd1c0fac5d70d7e86b [file] [log] [blame]
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001/* $OpenBSD: readconf.c,v 1.223 2014/12/04 02:24:32 djm 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>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100031#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110032#ifdef HAVE_PATHS_H
33# include <paths.h>
34#endif
Damien Miller194fd902013-10-15 12:13:05 +110035#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100036#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100037#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100040#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100041#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100042#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100043#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000044#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
45# include <vis.h>
46#endif
Damien Millerc7b06362006-03-15 11:53:45 +110047
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100050#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "cipher.h"
52#include "pathnames.h"
53#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "key.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100055#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "readconf.h"
57#include "match.h"
Damien Millerd7834352006-08-05 12:39:39 +100058#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "kex.h"
60#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110061#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000062#include "myproposal.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
64/* Format of the configuration file:
65
66 # Configuration data is parsed as follows:
67 # 1. command line options
68 # 2. user-specific file
69 # 3. system-wide file
70 # Any configuration value is only changed the first time it is set.
71 # Thus, host-specific definitions should be at the beginning of the
72 # configuration file, and defaults at the end.
73
74 # Host-specific declarations. These may override anything above. A single
75 # host may match multiple declarations; these are processed in the order
76 # that they are given in.
77
78 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000079 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
81 Host fake.com
82 HostName another.host.name.real.org
83 User blaah
84 Port 34289
85 ForwardX11 no
86 ForwardAgent no
87
88 Host books.com
89 RemoteForward 9999 shadows.cs.hut.fi:9999
90 Cipher 3des
91
92 Host fascist.blob.com
93 Port 23123
94 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095 PasswordAuthentication no
96
97 Host puukko.hut.fi
98 User t35124p
99 ProxyCommand ssh-proxy %h %p
100
101 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000102 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104 Host *.su
105 Cipher none
106 PasswordAuthentication no
107
Damien Millerd27b9472005-12-13 19:29:02 +1100108 Host vpn.fake.com
109 Tunnel yes
110 TunnelDevice 3
111
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112 # Defaults for various options
113 Host *
114 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 PasswordAuthentication yes
117 RSAAuthentication yes
118 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100120 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121 IdentityFile ~/.ssh/identity
122 Port 22
123 EscapeChar ~
124
125*/
126
127/* Keyword tokens. */
128
Damien Miller95def091999-11-25 00:26:21 +1100129typedef enum {
130 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100131 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000132 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
133 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000134 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000135 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100136 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
Damien Miller194fd902013-10-15 12:13:05 +1100137 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100138 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
139 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100140 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000141 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000142 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000143 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000144 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100145 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000146 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000147 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000148 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100149 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000150 oSendEnv, oControlPath, oControlMaster, oControlPersist,
151 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100152 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Miller7cc194f2014-02-04 11:12:56 +1100153 oVisualHostKey, oUseRoaming,
Damien Miller1262b662013-08-21 02:44:24 +1000154 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100155 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
156 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000157 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
Darren Tucker07636982013-05-16 20:30:03 +1000158 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159} OpCodes;
160
161/* Textual representations of the tokens. */
162
Damien Miller95def091999-11-25 00:26:21 +1100163static struct {
164 const char *name;
165 OpCodes opcode;
166} keywords[] = {
167 { "forwardagent", oForwardAgent },
168 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000169 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000170 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000171 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000172 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100173 { "gatewayports", oGatewayPorts },
174 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000175 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100176 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100177 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
178 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100180 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000181 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000182 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000183 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000184 { "challengeresponseauthentication", oChallengeResponseAuthentication },
185 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
186 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000187 { "kerberosauthentication", oUnsupported },
188 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000189 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000190#if defined(GSSAPI)
191 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000192 { "gssapidelegatecredentials", oGssDelegateCreds },
193#else
194 { "gssapiauthentication", oUnsupported },
195 { "gssapidelegatecredentials", oUnsupported },
196#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000197 { "fallbacktorsh", oDeprecated },
198 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100199 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100200 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100201 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100202 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000203 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100204 { "proxycommand", oProxyCommand },
205 { "port", oPort },
206 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000207 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000208 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000209 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100210 { "remoteforward", oRemoteForward },
211 { "localforward", oLocalForward },
212 { "user", oUser },
213 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100214 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100215 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100216 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000217 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100218 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000219 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100220 { "connectionattempts", oConnectionAttempts },
221 { "batchmode", oBatchMode },
222 { "checkhostip", oCheckHostIP },
223 { "stricthostkeychecking", oStrictHostKeyChecking },
224 { "compression", oCompression },
225 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100226 { "tcpkeepalive", oTCPKeepAlive },
227 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100228 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100229 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000230 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000231 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000232 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000233 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100234#ifdef ENABLE_PKCS11
235 { "smartcarddevice", oPKCS11Provider },
236 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000237#else
238 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100239 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000240#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100241 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000242 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000243 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100244 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000245 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000246 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000247 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100248 { "serveraliveinterval", oServerAliveInterval },
249 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000250 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000251 { "controlpath", oControlPath },
252 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000253 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100254 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100255 { "tunnel", oTunnel },
256 { "tunneldevice", oTunnelDevice },
257 { "localcommand", oLocalCommand },
258 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000259 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000260 { "useroaming", oUseRoaming },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000261 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100262 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000263 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000264 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100265 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100266 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
267 { "canonicalizehostname", oCanonicalizeHostname },
268 { "canonicalizemaxdots", oCanonicalizeMaxDots },
269 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000270 { "streamlocalbindmask", oStreamLocalBindMask },
271 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000272 { "revokedhostkeys", oRevokedHostKeys },
Darren Tucker07636982013-05-16 20:30:03 +1000273 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100274
Ben Lindstrom65366a82001-12-06 16:32:47 +0000275 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100276};
277
Damien Miller5428f641999-11-25 11:54:57 +1100278/*
279 * Adds a local TCP/IP port forward to options. Never returns if there is an
280 * error.
281 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282
Damien Miller4af51302000-04-16 11:18:38 +1000283void
Damien Miller7acefbb2014-07-18 14:11:24 +1000284add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285{
Damien Miller7acefbb2014-07-18 14:11:24 +1000286 struct Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000287#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100288 extern uid_t original_real_uid;
Damien Miller7acefbb2014-07-18 14:11:24 +1000289 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
290 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000291 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100292#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000293 options->local_forwards = xrealloc(options->local_forwards,
294 options->num_local_forwards + 1,
295 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100296 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100297
Damien Miller1a0442f2008-11-05 16:30:06 +1100298 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100299 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000300 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100301 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100302 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000303 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304}
305
Damien Miller5428f641999-11-25 11:54:57 +1100306/*
307 * Adds a remote TCP/IP port forward to options. Never returns if there is
308 * an error.
309 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Damien Miller4af51302000-04-16 11:18:38 +1000311void
Damien Miller7acefbb2014-07-18 14:11:24 +1000312add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313{
Damien Miller7acefbb2014-07-18 14:11:24 +1000314 struct Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000315
316 options->remote_forwards = xrealloc(options->remote_forwards,
317 options->num_remote_forwards + 1,
318 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100319 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100320
Damien Miller1a0442f2008-11-05 16:30:06 +1100321 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100322 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000323 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100324 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100325 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000326 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100327 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000328 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329}
330
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000331static void
332clear_forwardings(Options *options)
333{
334 int i;
335
Damien Millerf91ee4c2005-03-01 21:24:33 +1100336 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000337 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000338 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000339 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100341 }
Damien Miller232cfb12010-06-26 09:50:30 +1000342 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000343 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000344 options->local_forwards = NULL;
345 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000346 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100347 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000348 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000349 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000350 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000351 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100352 }
Damien Miller232cfb12010-06-26 09:50:30 +1000353 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000354 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000355 options->remote_forwards = NULL;
356 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000357 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100358 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000359}
360
Darren Tucker19104782013-04-05 11:13:08 +1100361void
362add_identity_file(Options *options, const char *dir, const char *filename,
363 int userprovided)
364{
365 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000366 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100367
368 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
369 fatal("Too many identity files specified (max %d)",
370 SSH_MAX_IDENTITY_FILES);
371
372 if (dir == NULL) /* no dir, filename is absolute */
373 path = xstrdup(filename);
374 else
375 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
376
Damien Miller15271902014-05-15 13:47:56 +1000377 /* Avoid registering duplicates */
378 for (i = 0; i < options->num_identity_files; i++) {
379 if (options->identity_file_userprovided[i] == userprovided &&
380 strcmp(options->identity_files[i], path) == 0) {
381 debug2("%s: ignoring duplicate key %s", __func__, path);
382 free(path);
383 return;
384 }
385 }
386
Darren Tucker19104782013-04-05 11:13:08 +1100387 options->identity_file_userprovided[options->num_identity_files] =
388 userprovided;
389 options->identity_files[options->num_identity_files++] = path;
390}
391
Damien Miller194fd902013-10-15 12:13:05 +1100392int
393default_ssh_port(void)
394{
395 static int port;
396 struct servent *sp;
397
398 if (port == 0) {
399 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
400 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
401 }
402 return port;
403}
404
405/*
406 * Execute a command in a shell.
407 * Return its exit status or -1 on abnormal exit.
408 */
409static int
410execute_in_shell(const char *cmd)
411{
412 char *shell, *command_string;
413 pid_t pid;
414 int devnull, status;
415 extern uid_t original_real_uid;
416
417 if ((shell = getenv("SHELL")) == NULL)
418 shell = _PATH_BSHELL;
419
420 /*
421 * Use "exec" to avoid "sh -c" processes on some platforms
422 * (e.g. Solaris)
423 */
424 xasprintf(&command_string, "exec %s", cmd);
425
426 /* Need this to redirect subprocess stdin/out */
427 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
428 fatal("open(/dev/null): %s", strerror(errno));
429
430 debug("Executing command: '%.500s'", cmd);
431
432 /* Fork and execute the command. */
433 if ((pid = fork()) == 0) {
434 char *argv[4];
435
436 /* Child. Permanently give up superuser privileges. */
437 permanently_drop_suid(original_real_uid);
438
439 /* Redirect child stdin and stdout. Leave stderr */
440 if (dup2(devnull, STDIN_FILENO) == -1)
441 fatal("dup2: %s", strerror(errno));
442 if (dup2(devnull, STDOUT_FILENO) == -1)
443 fatal("dup2: %s", strerror(errno));
444 if (devnull > STDERR_FILENO)
445 close(devnull);
446 closefrom(STDERR_FILENO + 1);
447
448 argv[0] = shell;
449 argv[1] = "-c";
450 argv[2] = command_string;
451 argv[3] = NULL;
452
453 execv(argv[0], argv);
454 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
455 /* Die with signal to make this error apparent to parent. */
456 signal(SIGTERM, SIG_DFL);
457 kill(getpid(), SIGTERM);
458 _exit(1);
459 }
460 /* Parent. */
461 if (pid < 0)
462 fatal("%s: fork: %.100s", __func__, strerror(errno));
463
464 close(devnull);
465 free(command_string);
466
467 while (waitpid(pid, &status, 0) == -1) {
468 if (errno != EINTR && errno != EAGAIN)
469 fatal("%s: waitpid: %s", __func__, strerror(errno));
470 }
471 if (!WIFEXITED(status)) {
472 error("command '%.100s' exited abnormally", cmd);
473 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000474 }
Damien Miller194fd902013-10-15 12:13:05 +1100475 debug3("command returned status %d", WEXITSTATUS(status));
476 return WEXITSTATUS(status);
477}
478
479/*
480 * Parse and execute a Match directive.
481 */
482static int
483match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000484 const char *host_arg, const char *original_host, int post_canon,
485 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100486{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000487 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100488 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000489 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100490 size_t len;
491 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
492
493 /*
494 * Configuration is likely to be incomplete at this point so we
495 * must be prepared to use default values.
496 */
497 port = options->port <= 0 ? default_ssh_port() : options->port;
498 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100499 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100500 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100501 host = percent_expand(options->hostname,
502 "h", host_arg, (char *)NULL);
503 } else
504 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100505
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000506 debug2("checking match for '%s' host %s originally %s",
507 cp, host, original_host);
508 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
509 criteria = NULL;
510 this_result = 1;
511 if ((negate = attrib[0] == '!'))
512 attrib++;
513 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100514 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000515 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100516 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000517 error("%.200s line %d: '%s' cannot be combined "
518 "with other Match attributes",
519 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100520 result = -1;
521 goto out;
522 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000523 if (result)
524 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100525 goto out;
526 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000527 attributes++;
528 if (strcasecmp(attrib, "canonical") == 0) {
529 r = !!post_canon; /* force bitmask member to boolean */
530 if (r == (negate ? 1 : 0))
531 this_result = result = 0;
532 debug3("%.200s line %d: %smatched '%s'",
533 filename, linenum,
534 this_result ? "" : "not ", oattrib);
535 continue;
536 }
537 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100538 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
539 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100540 result = -1;
541 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100542 }
543 len = strlen(arg);
544 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000545 criteria = xstrdup(host);
546 r = match_hostname(host, arg, len) == 1;
547 if (r == (negate ? 1 : 0))
548 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100549 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000550 criteria = xstrdup(original_host);
551 r = match_hostname(original_host, arg, len) == 1;
552 if (r == (negate ? 1 : 0))
553 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100554 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000555 criteria = xstrdup(ruser);
556 r = match_pattern_list(ruser, arg, len, 0) == 1;
557 if (r == (negate ? 1 : 0))
558 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100559 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000560 criteria = xstrdup(pw->pw_name);
561 r = match_pattern_list(pw->pw_name, arg, len, 0) == 1;
562 if (r == (negate ? 1 : 0))
563 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100564 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100565 if (gethostname(thishost, sizeof(thishost)) == -1)
566 fatal("gethostname: %s", strerror(errno));
567 strlcpy(shorthost, thishost, sizeof(shorthost));
568 shorthost[strcspn(thishost, ".")] = '\0';
569 snprintf(portstr, sizeof(portstr), "%d", port);
570
571 cmd = percent_expand(arg,
572 "L", shorthost,
573 "d", pw->pw_dir,
574 "h", host,
575 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000576 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100577 "p", portstr,
578 "r", ruser,
579 "u", pw->pw_name,
580 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100581 if (result != 1) {
582 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000583 debug3("%.200s line %d: skipped exec "
584 "\"%.100s\"", filename, linenum, cmd);
585 free(cmd);
586 continue;
Damien Miller06287802014-02-24 15:56:45 +1100587 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000588 r = execute_in_shell(cmd);
589 if (r == -1) {
590 fatal("%.200s line %d: match exec "
591 "'%.100s' error", filename,
592 linenum, cmd);
593 }
594 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100595 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000596 /* Force exit status to boolean */
597 r = r == 0;
598 if (r == (negate ? 1 : 0))
599 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100600 } else {
601 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100602 result = -1;
603 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100604 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000605 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
606 filename, linenum, this_result ? "": "not ",
607 oattrib, criteria);
608 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100609 }
Damien Millercf31f382013-10-24 21:02:56 +1100610 if (attributes == 0) {
611 error("One or more attributes required for Match");
612 result = -1;
613 goto out;
614 }
Damien Miller084bcd22013-10-23 16:30:51 +1100615 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 if (result != -1)
617 debug2("match %sfound", result ? "" : "not ");
618 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100619 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100620 return result;
621}
622
Damien Miller0faf7472013-10-17 11:47:23 +1100623/* Check and prepare a domain name: removes trailing '.' and lowercases */
624static void
625valid_domain(char *name, const char *filename, int linenum)
626{
627 size_t i, l = strlen(name);
628 u_char c, last = '\0';
629
630 if (l == 0)
631 fatal("%s line %d: empty hostname suffix", filename, linenum);
632 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
633 fatal("%s line %d: hostname suffix \"%.100s\" "
634 "starts with invalid character", filename, linenum, name);
635 for (i = 0; i < l; i++) {
636 c = tolower((u_char)name[i]);
637 name[i] = (char)c;
638 if (last == '.' && c == '.')
639 fatal("%s line %d: hostname suffix \"%.100s\" contains "
640 "consecutive separators", filename, linenum, name);
641 if (c != '.' && c != '-' && !isalnum(c) &&
642 c != '_') /* technically invalid, but common */
643 fatal("%s line %d: hostname suffix \"%.100s\" contains "
644 "invalid characters", filename, linenum, name);
645 last = c;
646 }
647 if (name[l - 1] == '.')
648 name[l - 1] = '\0';
649}
650
Damien Miller5428f641999-11-25 11:54:57 +1100651/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000652 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100653 */
Damien Miller4af51302000-04-16 11:18:38 +1000654static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000655parse_token(const char *cp, const char *filename, int linenum,
656 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000657{
Darren Tucker07636982013-05-16 20:30:03 +1000658 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659
Damien Miller95def091999-11-25 00:26:21 +1100660 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000661 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100662 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000663 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
664 strlen(ignored_unknown), 1) == 1)
665 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000666 error("%s: line %d: Bad configuration option: %s",
667 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100668 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000669}
670
Damien Millere9fc72e2013-10-15 12:14:12 +1100671/* Multistate option parsing */
672struct multistate {
673 char *key;
674 int value;
675};
676static const struct multistate multistate_flag[] = {
677 { "true", 1 },
678 { "false", 0 },
679 { "yes", 1 },
680 { "no", 0 },
681 { NULL, -1 }
682};
683static const struct multistate multistate_yesnoask[] = {
684 { "true", 1 },
685 { "false", 0 },
686 { "yes", 1 },
687 { "no", 0 },
688 { "ask", 2 },
689 { NULL, -1 }
690};
691static const struct multistate multistate_addressfamily[] = {
692 { "inet", AF_INET },
693 { "inet6", AF_INET6 },
694 { "any", AF_UNSPEC },
695 { NULL, -1 }
696};
697static const struct multistate multistate_controlmaster[] = {
698 { "true", SSHCTL_MASTER_YES },
699 { "yes", SSHCTL_MASTER_YES },
700 { "false", SSHCTL_MASTER_NO },
701 { "no", SSHCTL_MASTER_NO },
702 { "auto", SSHCTL_MASTER_AUTO },
703 { "ask", SSHCTL_MASTER_ASK },
704 { "autoask", SSHCTL_MASTER_AUTO_ASK },
705 { NULL, -1 }
706};
707static const struct multistate multistate_tunnel[] = {
708 { "ethernet", SSH_TUNMODE_ETHERNET },
709 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
710 { "true", SSH_TUNMODE_DEFAULT },
711 { "yes", SSH_TUNMODE_DEFAULT },
712 { "false", SSH_TUNMODE_NO },
713 { "no", SSH_TUNMODE_NO },
714 { NULL, -1 }
715};
716static const struct multistate multistate_requesttty[] = {
717 { "true", REQUEST_TTY_YES },
718 { "yes", REQUEST_TTY_YES },
719 { "false", REQUEST_TTY_NO },
720 { "no", REQUEST_TTY_NO },
721 { "force", REQUEST_TTY_FORCE },
722 { "auto", REQUEST_TTY_AUTO },
723 { NULL, -1 }
724};
Damien Miller38505592013-10-17 11:48:13 +1100725static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100726 { "true", SSH_CANONICALISE_YES },
727 { "false", SSH_CANONICALISE_NO },
728 { "yes", SSH_CANONICALISE_YES },
729 { "no", SSH_CANONICALISE_NO },
730 { "always", SSH_CANONICALISE_ALWAYS },
731 { NULL, -1 }
732};
Damien Millere9fc72e2013-10-15 12:14:12 +1100733
Damien Miller5428f641999-11-25 11:54:57 +1100734/*
735 * Processes a single option line as used in the configuration files. This
736 * only sets those values that have not already been set.
737 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100738#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100739int
Damien Miller194fd902013-10-15 12:13:05 +1100740process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000741 const char *original_host, char *line, const char *filename,
742 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743{
Damien Miller295ee632011-05-29 21:42:31 +1000744 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
745 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000746 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100747 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100748 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000749 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100750 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000751 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100752 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100753 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754
Damien Miller194fd902013-10-15 12:13:05 +1100755 if (activep == NULL) { /* We are processing a command line directive */
756 cmdline = 1;
757 activep = &cmdline;
758 }
759
Damien Millerc652cac2003-05-14 13:40:54 +1000760 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100761 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000762 if (strchr(WHITESPACE, line[len]) == NULL)
763 break;
764 line[len] = '\0';
765 }
766
Damien Millerbe484b52000-07-15 14:14:16 +1000767 s = line;
768 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100769 if ((keyword = strdelim(&s)) == NULL)
770 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000771 /* Ignore leading whitespace. */
772 if (*keyword == '\0')
773 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000774 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100775 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000776 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100777 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Darren Tucker07636982013-05-16 20:30:03 +1000779 opcode = parse_token(keyword, filename, linenum,
780 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781
Damien Miller95def091999-11-25 00:26:21 +1100782 switch (opcode) {
783 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100784 /* don't panic, but count bad options */
785 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100786 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000787 case oIgnoredUnknownOption:
788 debug("%s line %d: Ignored unknown option \"%s\"",
789 filename, linenum, keyword);
790 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000791 case oConnectTimeout:
792 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100793parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000794 arg = strdelim(&s);
795 if (!arg || *arg == '\0')
796 fatal("%s line %d: missing time value.",
797 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000798 if (strcmp(arg, "none") == 0)
799 value = -1;
800 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000801 fatal("%s line %d: invalid time value.",
802 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100803 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000804 *intptr = value;
805 break;
806
Damien Miller95def091999-11-25 00:26:21 +1100807 case oForwardAgent:
808 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100809 parse_flag:
810 multistate_ptr = multistate_flag;
811 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000812 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000813 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100814 fatal("%s line %d: missing argument.",
815 filename, linenum);
816 value = -1;
817 for (i = 0; multistate_ptr[i].key != NULL; i++) {
818 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
819 value = multistate_ptr[i].value;
820 break;
821 }
822 }
823 if (value == -1)
824 fatal("%s line %d: unsupported option \"%s\".",
825 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100826 if (*activep && *intptr == -1)
827 *intptr = value;
828 break;
829
830 case oForwardX11:
831 intptr = &options->forward_x11;
832 goto parse_flag;
833
Darren Tucker0a118da2003-10-15 15:54:32 +1000834 case oForwardX11Trusted:
835 intptr = &options->forward_x11_trusted;
836 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000837
Damien Miller1ab6a512010-06-26 10:02:24 +1000838 case oForwardX11Timeout:
839 intptr = &options->forward_x11_timeout;
840 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000841
Damien Miller95def091999-11-25 00:26:21 +1100842 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000843 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100844 goto parse_flag;
845
Darren Tuckere7d4b192006-07-12 22:17:10 +1000846 case oExitOnForwardFailure:
847 intptr = &options->exit_on_forward_failure;
848 goto parse_flag;
849
Damien Miller95def091999-11-25 00:26:21 +1100850 case oUsePrivilegedPort:
851 intptr = &options->use_privileged_port;
852 goto parse_flag;
853
Damien Miller95def091999-11-25 00:26:21 +1100854 case oPasswordAuthentication:
855 intptr = &options->password_authentication;
856 goto parse_flag;
857
Damien Miller874d77b2000-10-14 16:23:11 +1100858 case oKbdInteractiveAuthentication:
859 intptr = &options->kbd_interactive_authentication;
860 goto parse_flag;
861
862 case oKbdInteractiveDevices:
863 charptr = &options->kbd_interactive_devices;
864 goto parse_string;
865
Damien Miller0bc1bd82000-11-13 22:57:25 +1100866 case oPubkeyAuthentication:
867 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000868 goto parse_flag;
869
Damien Miller95def091999-11-25 00:26:21 +1100870 case oRSAAuthentication:
871 intptr = &options->rsa_authentication;
872 goto parse_flag;
873
874 case oRhostsRSAAuthentication:
875 intptr = &options->rhosts_rsa_authentication;
876 goto parse_flag;
877
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000878 case oHostbasedAuthentication:
879 intptr = &options->hostbased_authentication;
880 goto parse_flag;
881
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000882 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000883 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100884 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000885
Darren Tucker0efd1552003-08-26 11:49:55 +1000886 case oGssAuthentication:
887 intptr = &options->gss_authentication;
888 goto parse_flag;
889
890 case oGssDelegateCreds:
891 intptr = &options->gss_deleg_creds;
892 goto parse_flag;
893
Damien Miller95def091999-11-25 00:26:21 +1100894 case oBatchMode:
895 intptr = &options->batch_mode;
896 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897
Damien Miller95def091999-11-25 00:26:21 +1100898 case oCheckHostIP:
899 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000900 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901
Damien Miller37876e92003-05-15 10:19:46 +1000902 case oVerifyHostKeyDNS:
903 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100904 multistate_ptr = multistate_yesnoask;
905 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000906
Damien Miller95def091999-11-25 00:26:21 +1100907 case oStrictHostKeyChecking:
908 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100909 multistate_ptr = multistate_yesnoask;
910 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911
Damien Miller95def091999-11-25 00:26:21 +1100912 case oCompression:
913 intptr = &options->compression;
914 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915
Damien Miller12c150e2003-12-17 16:31:10 +1100916 case oTCPKeepAlive:
917 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100918 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000920 case oNoHostAuthenticationForLocalhost:
921 intptr = &options->no_host_authentication_for_localhost;
922 goto parse_flag;
923
Damien Miller95def091999-11-25 00:26:21 +1100924 case oNumberOfPasswordPrompts:
925 intptr = &options->number_of_password_prompts;
926 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927
Damien Miller95def091999-11-25 00:26:21 +1100928 case oCompressionLevel:
929 intptr = &options->compression_level;
930 goto parse_int;
931
Damien Millera5539d22003-04-09 20:50:06 +1000932 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000933 arg = strdelim(&s);
934 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000935 fatal("%.200s line %d: Missing argument.", filename,
936 linenum);
937 if (strcmp(arg, "default") == 0) {
938 val64 = 0;
939 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000940 if (scan_scaled(arg, &val64) == -1)
941 fatal("%.200s line %d: Bad number '%s': %s",
942 filename, linenum, arg, strerror(errno));
943 /* check for too-large or too-small limits */
944 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000945 fatal("%.200s line %d: RekeyLimit too large",
946 filename, linenum);
947 if (val64 != 0 && val64 < 16)
948 fatal("%.200s line %d: RekeyLimit too small",
949 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000950 }
Damien Miller3dff1762008-02-10 22:25:52 +1100951 if (*activep && options->rekey_limit == -1)
952 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000953 if (s != NULL) { /* optional rekey interval present */
954 if (strcmp(s, "none") == 0) {
955 (void)strdelim(&s); /* discard */
956 break;
957 }
958 intptr = &options->rekey_interval;
959 goto parse_time;
960 }
Damien Millera5539d22003-04-09 20:50:06 +1000961 break;
962
Damien Miller95def091999-11-25 00:26:21 +1100963 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000964 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000965 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100966 fatal("%.200s line %d: Missing argument.", filename, linenum);
967 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100968 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000969 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100970 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100971 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000972 add_identity_file(options, NULL,
973 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +1100974 }
975 break;
976
Damien Millerd3a18572000-06-07 19:55:44 +1000977 case oXAuthLocation:
978 charptr=&options->xauth_location;
979 goto parse_string;
980
Damien Miller95def091999-11-25 00:26:21 +1100981 case oUser:
982 charptr = &options->user;
983parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000984 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000985 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000986 fatal("%.200s line %d: Missing argument.",
987 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100988 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000989 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100990 break;
991
992 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000993 cpptr = (char **)&options->system_hostfiles;
994 uintptr = &options->num_system_hostfiles;
995 max_entries = SSH_MAX_HOSTS_FILES;
996parse_char_array:
997 if (*activep && *uintptr == 0) {
998 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
999 if ((*uintptr) >= max_entries)
1000 fatal("%s line %d: "
1001 "too many authorized keys files.",
1002 filename, linenum);
1003 cpptr[(*uintptr)++] = xstrdup(arg);
1004 }
1005 }
1006 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001007
1008 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001009 cpptr = (char **)&options->user_hostfiles;
1010 uintptr = &options->num_user_hostfiles;
1011 max_entries = SSH_MAX_HOSTS_FILES;
1012 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001013
Damien Miller95def091999-11-25 00:26:21 +11001014 case oHostName:
1015 charptr = &options->hostname;
1016 goto parse_string;
1017
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001018 case oHostKeyAlias:
1019 charptr = &options->host_key_alias;
1020 goto parse_string;
1021
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001022 case oPreferredAuthentications:
1023 charptr = &options->preferred_authentications;
1024 goto parse_string;
1025
Ben Lindstrome0f88042001-04-30 13:06:24 +00001026 case oBindAddress:
1027 charptr = &options->bind_address;
1028 goto parse_string;
1029
Damien Miller7ea845e2010-02-12 09:21:02 +11001030 case oPKCS11Provider:
1031 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001032 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001033
Damien Miller95def091999-11-25 00:26:21 +11001034 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001035 charptr = &options->proxy_command;
1036parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001037 if (s == NULL)
1038 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001039 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001040 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001041 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001042 return 0;
1043
1044 case oPort:
1045 intptr = &options->port;
1046parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001047 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001048 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001049 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001050 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001051 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001052
1053 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001054 value = strtol(arg, &endofnumber, 0);
1055 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001056 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001057 if (*activep && *intptr == -1)
1058 *intptr = value;
1059 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060
Damien Miller95def091999-11-25 00:26:21 +11001061 case oConnectionAttempts:
1062 intptr = &options->connection_attempts;
1063 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001064
Damien Miller95def091999-11-25 00:26:21 +11001065 case oCipher:
1066 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001067 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001068 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001069 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001070 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001071 if (value == -1)
1072 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001073 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001074 if (*activep && *intptr == -1)
1075 *intptr = value;
1076 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077
Damien Miller78928792000-04-12 20:17:38 +10001078 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001079 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001080 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001081 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001082 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001083 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001084 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001085 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001086 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001087 break;
1088
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001089 case oMacs:
1090 arg = strdelim(&s);
1091 if (!arg || *arg == '\0')
1092 fatal("%.200s line %d: Missing argument.", filename, linenum);
1093 if (!mac_valid(arg))
1094 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001095 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001096 if (*activep && options->macs == NULL)
1097 options->macs = xstrdup(arg);
1098 break;
1099
Damien Millerd5f62bf2010-09-24 22:11:14 +10001100 case oKexAlgorithms:
1101 arg = strdelim(&s);
1102 if (!arg || *arg == '\0')
1103 fatal("%.200s line %d: Missing argument.",
1104 filename, linenum);
1105 if (!kex_names_valid(arg))
1106 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1107 filename, linenum, arg ? arg : "<NONE>");
1108 if (*activep && options->kex_algorithms == NULL)
1109 options->kex_algorithms = xstrdup(arg);
1110 break;
1111
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001112 case oHostKeyAlgorithms:
1113 arg = strdelim(&s);
1114 if (!arg || *arg == '\0')
1115 fatal("%.200s line %d: Missing argument.", filename, linenum);
1116 if (!key_names_valid2(arg))
1117 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001118 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001119 if (*activep && options->hostkeyalgorithms == NULL)
1120 options->hostkeyalgorithms = xstrdup(arg);
1121 break;
1122
Damien Miller78928792000-04-12 20:17:38 +10001123 case oProtocol:
1124 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001125 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001126 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001127 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001128 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001129 if (value == SSH_PROTO_UNKNOWN)
1130 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001131 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001132 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1133 *intptr = value;
1134 break;
1135
Damien Miller95def091999-11-25 00:26:21 +11001136 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001137 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001138 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001139 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001140 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001141 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001142 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001143 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1144 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001145 break;
1146
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001147 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001148 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001149 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001150 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001151 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001152 fatal("%.200s line %d: Missing port argument.",
1153 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001154
Damien Millera699d952008-11-03 19:27:34 +11001155 if (opcode == oLocalForward ||
1156 opcode == oRemoteForward) {
1157 arg2 = strdelim(&s);
1158 if (arg2 == NULL || *arg2 == '\0')
1159 fatal("%.200s line %d: Missing target argument.",
1160 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001161
Damien Millera699d952008-11-03 19:27:34 +11001162 /* construct a string for parse_forward */
1163 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1164 } else if (opcode == oDynamicForward) {
1165 strlcpy(fwdarg, arg, sizeof(fwdarg));
1166 }
1167
1168 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001169 opcode == oDynamicForward ? 1 : 0,
1170 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001171 fatal("%.200s line %d: Bad forwarding specification.",
1172 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001173
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001174 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001175 if (opcode == oLocalForward ||
1176 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001177 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001178 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001179 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001180 }
Damien Miller95def091999-11-25 00:26:21 +11001181 break;
1182
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001183 case oClearAllForwardings:
1184 intptr = &options->clear_forwardings;
1185 goto parse_flag;
1186
Damien Miller95def091999-11-25 00:26:21 +11001187 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001188 if (cmdline)
1189 fatal("Host directive not supported as a command-line "
1190 "option");
Damien Miller95def091999-11-25 00:26:21 +11001191 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001192 arg2 = NULL;
1193 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1194 negated = *arg == '!';
1195 if (negated)
1196 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001197 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001198 if (negated) {
1199 debug("%.200s line %d: Skipping Host "
1200 "block because of negated match "
1201 "for %.100s", filename, linenum,
1202 arg);
1203 *activep = 0;
1204 break;
1205 }
1206 if (!*activep)
1207 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001208 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001209 }
Damien Millerfe924212011-05-15 08:44:45 +10001210 }
1211 if (*activep)
1212 debug("%.200s line %d: Applying options for %.100s",
1213 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001214 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001215 return 0;
1216
Damien Miller194fd902013-10-15 12:13:05 +11001217 case oMatch:
1218 if (cmdline)
1219 fatal("Host directive not supported as a command-line "
1220 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001221 value = match_cfg_line(options, &s, pw, host, original_host,
1222 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001223 if (value < 0)
1224 fatal("%.200s line %d: Bad Match condition", filename,
1225 linenum);
1226 *activep = value;
1227 break;
1228
Damien Miller95def091999-11-25 00:26:21 +11001229 case oEscapeChar:
1230 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001231 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001232 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001233 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001234 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001235 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1236 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001237 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001238 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001239 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001240 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001241 else {
1242 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001243 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001244 /* NOTREACHED */
1245 value = 0; /* Avoid compiler warning. */
1246 }
1247 if (*activep && *intptr == -1)
1248 *intptr = value;
1249 break;
1250
Damien Miller20a8f972003-05-18 20:50:30 +10001251 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001252 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001253 multistate_ptr = multistate_addressfamily;
1254 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001255
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001256 case oEnableSSHKeysign:
1257 intptr = &options->enable_ssh_keysign;
1258 goto parse_flag;
1259
Damien Millerbd394c32004-03-08 23:12:36 +11001260 case oIdentitiesOnly:
1261 intptr = &options->identities_only;
1262 goto parse_flag;
1263
Damien Miller509b0102003-12-17 16:33:10 +11001264 case oServerAliveInterval:
1265 intptr = &options->server_alive_interval;
1266 goto parse_time;
1267
1268 case oServerAliveCountMax:
1269 intptr = &options->server_alive_count_max;
1270 goto parse_int;
1271
Darren Tucker46bc0752004-05-02 22:11:30 +10001272 case oSendEnv:
1273 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1274 if (strchr(arg, '=') != NULL)
1275 fatal("%s line %d: Invalid environment name.",
1276 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001277 if (!*activep)
1278 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001279 if (options->num_send_env >= MAX_SEND_ENV)
1280 fatal("%s line %d: too many send env.",
1281 filename, linenum);
1282 options->send_env[options->num_send_env++] =
1283 xstrdup(arg);
1284 }
1285 break;
1286
Damien Miller0e220db2004-06-15 10:34:08 +10001287 case oControlPath:
1288 charptr = &options->control_path;
1289 goto parse_string;
1290
1291 case oControlMaster:
1292 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001293 multistate_ptr = multistate_controlmaster;
1294 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001295
Damien Millere11e1ea2010-08-03 16:04:46 +10001296 case oControlPersist:
1297 /* no/false/yes/true, or a time spec */
1298 intptr = &options->control_persist;
1299 arg = strdelim(&s);
1300 if (!arg || *arg == '\0')
1301 fatal("%.200s line %d: Missing ControlPersist"
1302 " argument.", filename, linenum);
1303 value = 0;
1304 value2 = 0; /* timeout */
1305 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1306 value = 0;
1307 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1308 value = 1;
1309 else if ((value2 = convtime(arg)) >= 0)
1310 value = 1;
1311 else
1312 fatal("%.200s line %d: Bad ControlPersist argument.",
1313 filename, linenum);
1314 if (*activep && *intptr == -1) {
1315 *intptr = value;
1316 options->control_persist_timeout = value2;
1317 }
1318 break;
1319
Damien Millere1776152005-03-01 21:47:37 +11001320 case oHashKnownHosts:
1321 intptr = &options->hash_known_hosts;
1322 goto parse_flag;
1323
Damien Millerd27b9472005-12-13 19:29:02 +11001324 case oTunnel:
1325 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001326 multistate_ptr = multistate_tunnel;
1327 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001328
1329 case oTunnelDevice:
1330 arg = strdelim(&s);
1331 if (!arg || *arg == '\0')
1332 fatal("%.200s line %d: Missing argument.", filename, linenum);
1333 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001334 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001335 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1336 if (*activep) {
1337 options->tun_local = value;
1338 options->tun_remote = value2;
1339 }
1340 break;
1341
1342 case oLocalCommand:
1343 charptr = &options->local_command;
1344 goto parse_command;
1345
1346 case oPermitLocalCommand:
1347 intptr = &options->permit_local_command;
1348 goto parse_flag;
1349
Damien Miller10288242008-06-30 00:04:03 +10001350 case oVisualHostKey:
1351 intptr = &options->visual_host_key;
1352 goto parse_flag;
1353
Damien Miller0dac6fb2010-11-20 15:19:38 +11001354 case oIPQoS:
1355 arg = strdelim(&s);
1356 if ((value = parse_ipqos(arg)) == -1)
1357 fatal("%s line %d: Bad IPQoS value: %s",
1358 filename, linenum, arg);
1359 arg = strdelim(&s);
1360 if (arg == NULL)
1361 value2 = value;
1362 else if ((value2 = parse_ipqos(arg)) == -1)
1363 fatal("%s line %d: Bad IPQoS value: %s",
1364 filename, linenum, arg);
1365 if (*activep) {
1366 options->ip_qos_interactive = value;
1367 options->ip_qos_bulk = value2;
1368 }
1369 break;
1370
Darren Tucker71e4d542009-07-06 07:12:27 +10001371 case oUseRoaming:
1372 intptr = &options->use_roaming;
1373 goto parse_flag;
1374
Damien Miller21771e22011-05-15 08:45:50 +10001375 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001376 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001377 multistate_ptr = multistate_requesttty;
1378 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001379
Darren Tucker07636982013-05-16 20:30:03 +10001380 case oIgnoreUnknown:
1381 charptr = &options->ignored_unknown;
1382 goto parse_string;
1383
Damien Miller1262b662013-08-21 02:44:24 +10001384 case oProxyUseFdpass:
1385 intptr = &options->proxy_use_fdpass;
1386 goto parse_flag;
1387
Damien Miller0faf7472013-10-17 11:47:23 +11001388 case oCanonicalDomains:
1389 value = options->num_canonical_domains != 0;
1390 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1391 valid_domain(arg, filename, linenum);
1392 if (!*activep || value)
1393 continue;
1394 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1395 fatal("%s line %d: too many hostname suffixes.",
1396 filename, linenum);
1397 options->canonical_domains[
1398 options->num_canonical_domains++] = xstrdup(arg);
1399 }
1400 break;
1401
Damien Miller38505592013-10-17 11:48:13 +11001402 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001403 value = options->num_permitted_cnames != 0;
1404 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1405 /* Either '*' for everything or 'list:list' */
1406 if (strcmp(arg, "*") == 0)
1407 arg2 = arg;
1408 else {
1409 lowercase(arg);
1410 if ((arg2 = strchr(arg, ':')) == NULL ||
1411 arg2[1] == '\0') {
1412 fatal("%s line %d: "
1413 "Invalid permitted CNAME \"%s\"",
1414 filename, linenum, arg);
1415 }
1416 *arg2 = '\0';
1417 arg2++;
1418 }
1419 if (!*activep || value)
1420 continue;
1421 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1422 fatal("%s line %d: too many permitted CNAMEs.",
1423 filename, linenum);
1424 cname = options->permitted_cnames +
1425 options->num_permitted_cnames++;
1426 cname->source_list = xstrdup(arg);
1427 cname->target_list = xstrdup(arg2);
1428 }
1429 break;
1430
Damien Miller38505592013-10-17 11:48:13 +11001431 case oCanonicalizeHostname:
1432 intptr = &options->canonicalize_hostname;
1433 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001434 goto parse_multistate;
1435
Damien Miller38505592013-10-17 11:48:13 +11001436 case oCanonicalizeMaxDots:
1437 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001438 goto parse_int;
1439
Damien Miller38505592013-10-17 11:48:13 +11001440 case oCanonicalizeFallbackLocal:
1441 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001442 goto parse_flag;
1443
Damien Miller7acefbb2014-07-18 14:11:24 +10001444 case oStreamLocalBindMask:
1445 arg = strdelim(&s);
1446 if (!arg || *arg == '\0')
1447 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1448 /* Parse mode in octal format */
1449 value = strtol(arg, &endofnumber, 8);
1450 if (arg == endofnumber || value < 0 || value > 0777)
1451 fatal("%.200s line %d: Bad mask.", filename, linenum);
1452 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1453 break;
1454
1455 case oStreamLocalBindUnlink:
1456 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1457 goto parse_flag;
1458
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001459 case oRevokedHostKeys:
1460 charptr = &options->revoked_host_keys;
1461 goto parse_string;
1462
Ben Lindstrom4daea862002-06-09 20:04:02 +00001463 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001464 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001465 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001466 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001467
Damien Millerf9b3feb2003-05-16 11:38:32 +10001468 case oUnsupported:
1469 error("%s line %d: Unsupported option \"%s\"",
1470 filename, linenum, keyword);
1471 return 0;
1472
Damien Miller95def091999-11-25 00:26:21 +11001473 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001474 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001475 }
1476
1477 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001478 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001479 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001480 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001481 }
Damien Miller95def091999-11-25 00:26:21 +11001482 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483}
1484
1485
Damien Miller5428f641999-11-25 11:54:57 +11001486/*
1487 * Reads the config file and modifies the options accordingly. Options
1488 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001489 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001490 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001492int
Damien Miller194fd902013-10-15 12:13:05 +11001493read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001494 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495{
Damien Miller95def091999-11-25 00:26:21 +11001496 FILE *f;
1497 char line[1024];
1498 int active, linenum;
1499 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500
Damien Miller57a44762004-04-20 20:11:57 +10001501 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001502 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503
Darren Tuckeraefa3682013-04-05 11:18:35 +11001504 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001505 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001506
Damien Miller33793852004-06-15 10:27:55 +10001507 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001508 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001509 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001510 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001511 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001512 }
1513
Damien Miller95def091999-11-25 00:26:21 +11001514 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515
Damien Miller5428f641999-11-25 11:54:57 +11001516 /*
1517 * Mark that we are now processing the options. This flag is turned
1518 * on/off by Host specifications.
1519 */
Damien Miller95def091999-11-25 00:26:21 +11001520 active = 1;
1521 linenum = 0;
1522 while (fgets(line, sizeof(line), f)) {
1523 /* Update line number counter. */
1524 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001525 if (process_config_line(options, pw, host, original_host,
1526 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001527 bad_options++;
1528 }
1529 fclose(f);
1530 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001531 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001532 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001533 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001534}
1535
Damien Miller13f97b22014-02-24 15:57:55 +11001536/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1537int
1538option_clear_or_none(const char *o)
1539{
1540 return o == NULL || strcasecmp(o, "none") == 0;
1541}
1542
Damien Miller5428f641999-11-25 11:54:57 +11001543/*
1544 * Initializes options to special values that indicate that they have not yet
1545 * been set. Read_config_file will only set options with this value. Options
1546 * are processed in the following order: command line, user config file,
1547 * system config file. Last, fill_default_options is called.
1548 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
Damien Miller4af51302000-04-16 11:18:38 +10001550void
Damien Miller95def091999-11-25 00:26:21 +11001551initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552{
Damien Miller95def091999-11-25 00:26:21 +11001553 memset(options, 'X', sizeof(*options));
1554 options->forward_agent = -1;
1555 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001556 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001557 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001558 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001559 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001560 options->fwd_opts.gateway_ports = -1;
1561 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1562 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001563 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001564 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001565 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001566 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001567 options->gss_authentication = -1;
1568 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001569 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001570 options->kbd_interactive_authentication = -1;
1571 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001572 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001573 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001574 options->batch_mode = -1;
1575 options->check_host_ip = -1;
1576 options->strict_host_key_checking = -1;
1577 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001578 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001579 options->compression_level = -1;
1580 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001581 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001582 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001583 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001584 options->number_of_password_prompts = -1;
1585 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001586 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001587 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001588 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001589 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001590 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001591 options->num_identity_files = 0;
1592 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001593 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001594 options->proxy_command = NULL;
1595 options->user = NULL;
1596 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001597 options->num_system_hostfiles = 0;
1598 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001599 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001600 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001601 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001602 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001603 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001604 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001605 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001606 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001607 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001608 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001609 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001610 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001611 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001612 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001613 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001614 options->server_alive_interval = -1;
1615 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001616 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001617 options->control_path = NULL;
1618 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001619 options->control_persist = -1;
1620 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001621 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001622 options->tun_open = -1;
1623 options->tun_local = -1;
1624 options->tun_remote = -1;
1625 options->local_command = NULL;
1626 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001627 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001628 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001629 options->ip_qos_interactive = -1;
1630 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001631 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001632 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001633 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001634 options->num_canonical_domains = 0;
1635 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001636 options->canonicalize_max_dots = -1;
1637 options->canonicalize_fallback_local = -1;
1638 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001639 options->revoked_host_keys = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001640}
1641
Damien Miller5428f641999-11-25 11:54:57 +11001642/*
Damien Miller13f97b22014-02-24 15:57:55 +11001643 * A petite version of fill_default_options() that just fills the options
1644 * needed for hostname canonicalization to proceed.
1645 */
1646void
1647fill_default_options_for_canonicalization(Options *options)
1648{
1649 if (options->canonicalize_max_dots == -1)
1650 options->canonicalize_max_dots = 1;
1651 if (options->canonicalize_fallback_local == -1)
1652 options->canonicalize_fallback_local = 1;
1653 if (options->canonicalize_hostname == -1)
1654 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1655}
1656
1657/*
Damien Miller5428f641999-11-25 11:54:57 +11001658 * Called after processing other sources of option data, this fills those
1659 * options for which no value has been specified with their default values.
1660 */
Damien Miller4af51302000-04-16 11:18:38 +10001661void
Damien Miller95def091999-11-25 00:26:21 +11001662fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001663{
Damien Miller95def091999-11-25 00:26:21 +11001664 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001665 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001666 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001667 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001668 if (options->forward_x11_trusted == -1)
1669 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001670 if (options->forward_x11_timeout == -1)
1671 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001672 if (options->exit_on_forward_failure == -1)
1673 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001674 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001675 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001676 if (options->fwd_opts.gateway_ports == -1)
1677 options->fwd_opts.gateway_ports = 0;
1678 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1679 options->fwd_opts.streamlocal_bind_mask = 0177;
1680 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1681 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001682 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001683 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001684 if (options->rsa_authentication == -1)
1685 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001686 if (options->pubkey_authentication == -1)
1687 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001688 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001689 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001690 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001691 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001692 if (options->gss_deleg_creds == -1)
1693 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001694 if (options->password_authentication == -1)
1695 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001696 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001697 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001698 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001699 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001700 if (options->hostbased_authentication == -1)
1701 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001702 if (options->batch_mode == -1)
1703 options->batch_mode = 0;
1704 if (options->check_host_ip == -1)
1705 options->check_host_ip = 1;
1706 if (options->strict_host_key_checking == -1)
1707 options->strict_host_key_checking = 2; /* 2 is default */
1708 if (options->compression == -1)
1709 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001710 if (options->tcp_keep_alive == -1)
1711 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001712 if (options->compression_level == -1)
1713 options->compression_level = 6;
1714 if (options->port == -1)
1715 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001716 if (options->address_family == -1)
1717 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001718 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001719 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001720 if (options->number_of_password_prompts == -1)
1721 options->number_of_password_prompts = 3;
1722 /* Selected in ssh_login(). */
1723 if (options->cipher == -1)
1724 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001725 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001726 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001727 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001728 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001729 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001730 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001731 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001732 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001733 add_identity_file(options, "~/",
1734 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001735 }
1736 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001737 add_identity_file(options, "~/",
1738 _PATH_SSH_CLIENT_ID_RSA, 0);
1739 add_identity_file(options, "~/",
1740 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001741#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001742 add_identity_file(options, "~/",
1743 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001744#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001745 add_identity_file(options, "~/",
1746 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001747 }
Damien Millereba71ba2000-04-29 23:57:08 +10001748 }
Damien Miller95def091999-11-25 00:26:21 +11001749 if (options->escape_char == -1)
1750 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001751 if (options->num_system_hostfiles == 0) {
1752 options->system_hostfiles[options->num_system_hostfiles++] =
1753 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1754 options->system_hostfiles[options->num_system_hostfiles++] =
1755 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1756 }
1757 if (options->num_user_hostfiles == 0) {
1758 options->user_hostfiles[options->num_user_hostfiles++] =
1759 xstrdup(_PATH_SSH_USER_HOSTFILE);
1760 options->user_hostfiles[options->num_user_hostfiles++] =
1761 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1762 }
Damien Millerfcd93202002-02-05 12:26:34 +11001763 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001764 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001765 if (options->clear_forwardings == 1)
1766 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001767 if (options->no_host_authentication_for_localhost == - 1)
1768 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001769 if (options->identities_only == -1)
1770 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001771 if (options->enable_ssh_keysign == -1)
1772 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001773 if (options->rekey_limit == -1)
1774 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001775 if (options->rekey_interval == -1)
1776 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001777 if (options->verify_host_key_dns == -1)
1778 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001779 if (options->server_alive_interval == -1)
1780 options->server_alive_interval = 0;
1781 if (options->server_alive_count_max == -1)
1782 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001783 if (options->control_master == -1)
1784 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001785 if (options->control_persist == -1) {
1786 options->control_persist = 0;
1787 options->control_persist_timeout = 0;
1788 }
Damien Millere1776152005-03-01 21:47:37 +11001789 if (options->hash_known_hosts == -1)
1790 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001791 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001792 options->tun_open = SSH_TUNMODE_NO;
1793 if (options->tun_local == -1)
1794 options->tun_local = SSH_TUNID_ANY;
1795 if (options->tun_remote == -1)
1796 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001797 if (options->permit_local_command == -1)
1798 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001799 if (options->use_roaming == -1)
1800 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001801 if (options->visual_host_key == -1)
1802 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001803 if (options->ip_qos_interactive == -1)
1804 options->ip_qos_interactive = IPTOS_LOWDELAY;
1805 if (options->ip_qos_bulk == -1)
1806 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001807 if (options->request_tty == -1)
1808 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001809 if (options->proxy_use_fdpass == -1)
1810 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001811 if (options->canonicalize_max_dots == -1)
1812 options->canonicalize_max_dots = 1;
1813 if (options->canonicalize_fallback_local == -1)
1814 options->canonicalize_fallback_local = 1;
1815 if (options->canonicalize_hostname == -1)
1816 options->canonicalize_hostname = SSH_CANONICALISE_NO;
Damien Millere9fc72e2013-10-15 12:14:12 +11001817#define CLEAR_ON_NONE(v) \
1818 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001819 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001820 free(v); \
1821 v = NULL; \
1822 } \
1823 } while(0)
1824 CLEAR_ON_NONE(options->local_command);
1825 CLEAR_ON_NONE(options->proxy_command);
1826 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001827 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001828 /* options->user will be set in the main program if appropriate */
1829 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001830 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001831 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001832}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001833
Damien Miller7acefbb2014-07-18 14:11:24 +10001834struct fwdarg {
1835 char *arg;
1836 int ispath;
1837};
1838
1839/*
1840 * parse_fwd_field
1841 * parses the next field in a port forwarding specification.
1842 * sets fwd to the parsed field and advances p past the colon
1843 * or sets it to NULL at end of string.
1844 * returns 0 on success, else non-zero.
1845 */
1846static int
1847parse_fwd_field(char **p, struct fwdarg *fwd)
1848{
1849 char *ep, *cp = *p;
1850 int ispath = 0;
1851
1852 if (*cp == '\0') {
1853 *p = NULL;
1854 return -1; /* end of string */
1855 }
1856
1857 /*
1858 * A field escaped with square brackets is used literally.
1859 * XXX - allow ']' to be escaped via backslash?
1860 */
1861 if (*cp == '[') {
1862 /* find matching ']' */
1863 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1864 if (*ep == '/')
1865 ispath = 1;
1866 }
1867 /* no matching ']' or not at end of field. */
1868 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1869 return -1;
1870 /* NUL terminate the field and advance p past the colon */
1871 *ep++ = '\0';
1872 if (*ep != '\0')
1873 *ep++ = '\0';
1874 fwd->arg = cp + 1;
1875 fwd->ispath = ispath;
1876 *p = ep;
1877 return 0;
1878 }
1879
1880 for (cp = *p; *cp != '\0'; cp++) {
1881 switch (*cp) {
1882 case '\\':
1883 memmove(cp, cp + 1, strlen(cp + 1) + 1);
1884 cp++;
1885 break;
1886 case '/':
1887 ispath = 1;
1888 break;
1889 case ':':
1890 *cp++ = '\0';
1891 goto done;
1892 }
1893 }
1894done:
1895 fwd->arg = *p;
1896 fwd->ispath = ispath;
1897 *p = cp;
1898 return 0;
1899}
1900
Damien Millerf91ee4c2005-03-01 21:24:33 +11001901/*
1902 * parse_forward
1903 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001904 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10001905 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
1906 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11001907 * dynamicfwd == 1
1908 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001909 * returns number of arguments parsed or zero on error
1910 */
1911int
Damien Miller7acefbb2014-07-18 14:11:24 +10001912parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001913{
Damien Miller7acefbb2014-07-18 14:11:24 +10001914 struct fwdarg fwdargs[4];
1915 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001916 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001917
Damien Miller7acefbb2014-07-18 14:11:24 +10001918 memset(fwd, 0, sizeof(*fwd));
1919 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11001920
1921 cp = p = xstrdup(fwdspec);
1922
1923 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001924 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001925 cp++;
1926
Damien Miller7acefbb2014-07-18 14:11:24 +10001927 for (i = 0; i < 4; ++i) {
1928 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001929 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10001930 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001931
Damien Millerf4b39532008-11-03 19:28:21 +11001932 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10001933 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001934 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10001935 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001936
1937 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001938 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10001939 if (fwdargs[0].ispath) {
1940 fwd->listen_path = xstrdup(fwdargs[0].arg);
1941 fwd->listen_port = PORT_STREAMLOCAL;
1942 } else {
1943 fwd->listen_host = NULL;
1944 fwd->listen_port = a2port(fwdargs[0].arg);
1945 }
Damien Millera699d952008-11-03 19:27:34 +11001946 fwd->connect_host = xstrdup("socks");
1947 break;
1948
1949 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10001950 if (fwdargs[0].ispath && fwdargs[1].ispath) {
1951 fwd->listen_path = xstrdup(fwdargs[0].arg);
1952 fwd->listen_port = PORT_STREAMLOCAL;
1953 fwd->connect_path = xstrdup(fwdargs[1].arg);
1954 fwd->connect_port = PORT_STREAMLOCAL;
1955 } else if (fwdargs[1].ispath) {
1956 fwd->listen_host = NULL;
1957 fwd->listen_port = a2port(fwdargs[0].arg);
1958 fwd->connect_path = xstrdup(fwdargs[1].arg);
1959 fwd->connect_port = PORT_STREAMLOCAL;
1960 } else {
1961 fwd->listen_host = xstrdup(fwdargs[0].arg);
1962 fwd->listen_port = a2port(fwdargs[1].arg);
1963 fwd->connect_host = xstrdup("socks");
1964 }
Damien Millera699d952008-11-03 19:27:34 +11001965 break;
1966
Damien Millerf91ee4c2005-03-01 21:24:33 +11001967 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10001968 if (fwdargs[0].ispath) {
1969 fwd->listen_path = xstrdup(fwdargs[0].arg);
1970 fwd->listen_port = PORT_STREAMLOCAL;
1971 fwd->connect_host = xstrdup(fwdargs[1].arg);
1972 fwd->connect_port = a2port(fwdargs[2].arg);
1973 } else if (fwdargs[2].ispath) {
1974 fwd->listen_host = xstrdup(fwdargs[0].arg);
1975 fwd->listen_port = a2port(fwdargs[1].arg);
1976 fwd->connect_path = xstrdup(fwdargs[2].arg);
1977 fwd->connect_port = PORT_STREAMLOCAL;
1978 } else {
1979 fwd->listen_host = NULL;
1980 fwd->listen_port = a2port(fwdargs[0].arg);
1981 fwd->connect_host = xstrdup(fwdargs[1].arg);
1982 fwd->connect_port = a2port(fwdargs[2].arg);
1983 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001984 break;
1985
1986 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10001987 fwd->listen_host = xstrdup(fwdargs[0].arg);
1988 fwd->listen_port = a2port(fwdargs[1].arg);
1989 fwd->connect_host = xstrdup(fwdargs[2].arg);
1990 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001991 break;
1992 default:
1993 i = 0; /* failure */
1994 }
1995
Darren Tuckera627d422013-06-02 07:31:17 +10001996 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001997
Damien Millera699d952008-11-03 19:27:34 +11001998 if (dynamicfwd) {
1999 if (!(i == 1 || i == 2))
2000 goto fail_free;
2001 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002002 if (!(i == 3 || i == 4)) {
2003 if (fwd->connect_path == NULL &&
2004 fwd->listen_path == NULL)
2005 goto fail_free;
2006 }
2007 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002008 goto fail_free;
2009 }
2010
Damien Miller7acefbb2014-07-18 14:11:24 +10002011 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2012 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002013 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002014 if (fwd->connect_host != NULL &&
2015 strlen(fwd->connect_host) >= NI_MAXHOST)
2016 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002017 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2018 if (fwd->connect_path != NULL &&
2019 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2020 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002021 if (fwd->listen_host != NULL &&
2022 strlen(fwd->listen_host) >= NI_MAXHOST)
2023 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002024 if (fwd->listen_path != NULL &&
2025 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2026 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002027
2028 return (i);
2029
2030 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002031 free(fwd->connect_host);
2032 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002033 free(fwd->connect_path);
2034 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002035 free(fwd->listen_host);
2036 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002037 free(fwd->listen_path);
2038 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002039 return (0);
2040}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002041
2042/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2043static const char *
2044fmt_multistate_int(int val, const struct multistate *m)
2045{
2046 u_int i;
2047
2048 for (i = 0; m[i].key != NULL; i++) {
2049 if (m[i].value == val)
2050 return m[i].key;
2051 }
2052 return "UNKNOWN";
2053}
2054
2055static const char *
2056fmt_intarg(OpCodes code, int val)
2057{
2058 if (val == -1)
2059 return "unset";
2060 switch (code) {
2061 case oAddressFamily:
2062 return fmt_multistate_int(val, multistate_addressfamily);
2063 case oVerifyHostKeyDNS:
2064 case oStrictHostKeyChecking:
2065 return fmt_multistate_int(val, multistate_yesnoask);
2066 case oControlMaster:
2067 return fmt_multistate_int(val, multistate_controlmaster);
2068 case oTunnel:
2069 return fmt_multistate_int(val, multistate_tunnel);
2070 case oRequestTTY:
2071 return fmt_multistate_int(val, multistate_requesttty);
2072 case oCanonicalizeHostname:
2073 return fmt_multistate_int(val, multistate_canonicalizehostname);
2074 case oProtocol:
2075 switch (val) {
2076 case SSH_PROTO_1:
2077 return "1";
2078 case SSH_PROTO_2:
2079 return "2";
2080 case (SSH_PROTO_1|SSH_PROTO_2):
2081 return "2,1";
2082 default:
2083 return "UNKNOWN";
2084 }
2085 default:
2086 switch (val) {
2087 case 0:
2088 return "no";
2089 case 1:
2090 return "yes";
2091 default:
2092 return "UNKNOWN";
2093 }
2094 }
2095}
2096
2097static const char *
2098lookup_opcode_name(OpCodes code)
2099{
2100 u_int i;
2101
2102 for (i = 0; keywords[i].name != NULL; i++)
2103 if (keywords[i].opcode == code)
2104 return(keywords[i].name);
2105 return "UNKNOWN";
2106}
2107
2108static void
2109dump_cfg_int(OpCodes code, int val)
2110{
2111 printf("%s %d\n", lookup_opcode_name(code), val);
2112}
2113
2114static void
2115dump_cfg_fmtint(OpCodes code, int val)
2116{
2117 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2118}
2119
2120static void
2121dump_cfg_string(OpCodes code, const char *val)
2122{
2123 if (val == NULL)
2124 return;
2125 printf("%s %s\n", lookup_opcode_name(code), val);
2126}
2127
2128static void
2129dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2130{
2131 u_int i;
2132
2133 for (i = 0; i < count; i++)
2134 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2135}
2136
2137static void
2138dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2139{
2140 u_int i;
2141
2142 printf("%s", lookup_opcode_name(code));
2143 for (i = 0; i < count; i++)
2144 printf(" %s", vals[i]);
2145 printf("\n");
2146}
2147
2148static void
2149dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2150{
2151 const struct Forward *fwd;
2152 u_int i;
2153
2154 /* oDynamicForward */
2155 for (i = 0; i < count; i++) {
2156 fwd = &fwds[i];
2157 if (code == oDynamicForward &&
2158 strcmp(fwd->connect_host, "socks") != 0)
2159 continue;
2160 if (code == oLocalForward &&
2161 strcmp(fwd->connect_host, "socks") == 0)
2162 continue;
2163 printf("%s", lookup_opcode_name(code));
2164 if (fwd->listen_port == PORT_STREAMLOCAL)
2165 printf(" %s", fwd->listen_path);
2166 else if (fwd->listen_host == NULL)
2167 printf(" %d", fwd->listen_port);
2168 else {
2169 printf(" [%s]:%d",
2170 fwd->listen_host, fwd->listen_port);
2171 }
2172 if (code != oDynamicForward) {
2173 if (fwd->connect_port == PORT_STREAMLOCAL)
2174 printf(" %s", fwd->connect_path);
2175 else if (fwd->connect_host == NULL)
2176 printf(" %d", fwd->connect_port);
2177 else {
2178 printf(" [%s]:%d",
2179 fwd->connect_host, fwd->connect_port);
2180 }
2181 }
2182 printf("\n");
2183 }
2184}
2185
2186void
2187dump_client_config(Options *o, const char *host)
2188{
2189 int i;
2190 char vbuf[5];
2191
2192 /* Most interesting options first: user, host, port */
2193 dump_cfg_string(oUser, o->user);
2194 dump_cfg_string(oHostName, host);
2195 dump_cfg_int(oPort, o->port);
2196
2197 /* Flag options */
2198 dump_cfg_fmtint(oAddressFamily, o->address_family);
2199 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2200 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2201 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2202 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2203 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2204 dump_cfg_fmtint(oCompression, o->compression);
2205 dump_cfg_fmtint(oControlMaster, o->control_master);
2206 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2207 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
2208 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2209 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2210 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2211 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2212#ifdef GSSAPI
2213 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2214 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2215#endif /* GSSAPI */
2216 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2217 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2218 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2219 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2220 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2221 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2222 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2223 dump_cfg_fmtint(oProtocol, o->protocol);
2224 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2225 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2226 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2227 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2228 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2229 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2230 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2231 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2232 dump_cfg_fmtint(oTunnel, o->tun_open);
2233 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2234 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2235 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
2236
2237 /* Integer options */
2238 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2239 dump_cfg_int(oCompressionLevel, o->compression_level);
2240 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2241 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2242 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2243 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2244 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2245
2246 /* String options */
2247 dump_cfg_string(oBindAddress, o->bind_address);
2248 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2249 dump_cfg_string(oControlPath, o->control_path);
2250 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms ? o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
2251 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
2252 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2253 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2254 dump_cfg_string(oLocalCommand, o->local_command);
2255 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2256 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2257 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2258 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2259 dump_cfg_string(oProxyCommand, o->proxy_command);
2260 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002261 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002262
2263 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2264 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2265 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2266
2267 /* String array options */
2268 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2269 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2270 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2271 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2272 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2273
2274 /* Special cases */
2275
2276 /* oConnectTimeout */
2277 if (o->connection_timeout == -1)
2278 printf("connecttimeout none\n");
2279 else
2280 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2281
2282 /* oTunnelDevice */
2283 printf("tunneldevice");
2284 if (o->tun_local == SSH_TUNID_ANY)
2285 printf(" any");
2286 else
2287 printf(" %d", o->tun_local);
2288 if (o->tun_remote == SSH_TUNID_ANY)
2289 printf(":any");
2290 else
2291 printf(":%d", o->tun_remote);
2292 printf("\n");
2293
2294 /* oCanonicalizePermittedCNAMEs */
2295 if ( o->num_permitted_cnames > 0) {
2296 printf("canonicalizePermittedcnames");
2297 for (i = 0; i < o->num_permitted_cnames; i++) {
2298 printf(" %s:%s", o->permitted_cnames[i].source_list,
2299 o->permitted_cnames[i].target_list);
2300 }
2301 printf("\n");
2302 }
2303
2304 /* oCipher */
2305 if (o->cipher != SSH_CIPHER_NOT_SET)
2306 printf("Cipher %s\n", cipher_name(o->cipher));
2307
2308 /* oControlPersist */
2309 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2310 dump_cfg_fmtint(oControlPersist, o->control_persist);
2311 else
2312 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2313
2314 /* oEscapeChar */
2315 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2316 printf("escapechar none\n");
2317 else {
2318 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2319 printf("escapechar %s\n", vbuf);
2320 }
2321
2322 /* oIPQoS */
2323 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2324 printf("%s\n", iptos2str(o->ip_qos_bulk));
2325
2326 /* oRekeyLimit */
2327 printf("rekeylimit %lld %d\n",
2328 (long long)o->rekey_limit, o->rekey_interval);
2329
2330 /* oStreamLocalBindMask */
2331 printf("streamlocalbindmask 0%o\n",
2332 o->fwd_opts.streamlocal_bind_mask);
2333}