blob: 869a1c4d8c448d260270fe45e2d7b2a01a1f2c02 [file] [log] [blame]
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001/* $OpenBSD: readconf.c,v 1.221 2014/10/08 22:20:25 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,
Damien Miller7acefbb2014-07-18 14:11:24 +1000157 oStreamLocalBindMask, oStreamLocalBindUnlink,
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 },
Darren Tucker07636982013-05-16 20:30:03 +1000272 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100273
Ben Lindstrom65366a82001-12-06 16:32:47 +0000274 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100275};
276
Damien Miller5428f641999-11-25 11:54:57 +1100277/*
278 * Adds a local TCP/IP port forward to options. Never returns if there is an
279 * error.
280 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller4af51302000-04-16 11:18:38 +1000282void
Damien Miller7acefbb2014-07-18 14:11:24 +1000283add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284{
Damien Miller7acefbb2014-07-18 14:11:24 +1000285 struct Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000286#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100287 extern uid_t original_real_uid;
Damien Miller7acefbb2014-07-18 14:11:24 +1000288 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
289 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000290 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100291#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000292 options->local_forwards = xrealloc(options->local_forwards,
293 options->num_local_forwards + 1,
294 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100295 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296
Damien Miller1a0442f2008-11-05 16:30:06 +1100297 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100298 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000299 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100300 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100301 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000302 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Damien Miller5428f641999-11-25 11:54:57 +1100305/*
306 * Adds a remote TCP/IP port forward to options. Never returns if there is
307 * an error.
308 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309
Damien Miller4af51302000-04-16 11:18:38 +1000310void
Damien Miller7acefbb2014-07-18 14:11:24 +1000311add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Miller7acefbb2014-07-18 14:11:24 +1000313 struct Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000314
315 options->remote_forwards = xrealloc(options->remote_forwards,
316 options->num_remote_forwards + 1,
317 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100318 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100319
Damien Miller1a0442f2008-11-05 16:30:06 +1100320 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100321 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000322 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100323 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100324 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000325 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100326 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000327 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
329
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000330static void
331clear_forwardings(Options *options)
332{
333 int i;
334
Damien Millerf91ee4c2005-03-01 21:24:33 +1100335 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000336 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000337 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000338 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000339 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100340 }
Damien Miller232cfb12010-06-26 09:50:30 +1000341 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000342 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000343 options->local_forwards = NULL;
344 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000345 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100346 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000347 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000348 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000349 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000350 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100351 }
Damien Miller232cfb12010-06-26 09:50:30 +1000352 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000353 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000354 options->remote_forwards = NULL;
355 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000356 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100357 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000358}
359
Darren Tucker19104782013-04-05 11:13:08 +1100360void
361add_identity_file(Options *options, const char *dir, const char *filename,
362 int userprovided)
363{
364 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000365 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100366
367 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
368 fatal("Too many identity files specified (max %d)",
369 SSH_MAX_IDENTITY_FILES);
370
371 if (dir == NULL) /* no dir, filename is absolute */
372 path = xstrdup(filename);
373 else
374 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
375
Damien Miller15271902014-05-15 13:47:56 +1000376 /* Avoid registering duplicates */
377 for (i = 0; i < options->num_identity_files; i++) {
378 if (options->identity_file_userprovided[i] == userprovided &&
379 strcmp(options->identity_files[i], path) == 0) {
380 debug2("%s: ignoring duplicate key %s", __func__, path);
381 free(path);
382 return;
383 }
384 }
385
Darren Tucker19104782013-04-05 11:13:08 +1100386 options->identity_file_userprovided[options->num_identity_files] =
387 userprovided;
388 options->identity_files[options->num_identity_files++] = path;
389}
390
Damien Miller194fd902013-10-15 12:13:05 +1100391int
392default_ssh_port(void)
393{
394 static int port;
395 struct servent *sp;
396
397 if (port == 0) {
398 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
399 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
400 }
401 return port;
402}
403
404/*
405 * Execute a command in a shell.
406 * Return its exit status or -1 on abnormal exit.
407 */
408static int
409execute_in_shell(const char *cmd)
410{
411 char *shell, *command_string;
412 pid_t pid;
413 int devnull, status;
414 extern uid_t original_real_uid;
415
416 if ((shell = getenv("SHELL")) == NULL)
417 shell = _PATH_BSHELL;
418
419 /*
420 * Use "exec" to avoid "sh -c" processes on some platforms
421 * (e.g. Solaris)
422 */
423 xasprintf(&command_string, "exec %s", cmd);
424
425 /* Need this to redirect subprocess stdin/out */
426 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
427 fatal("open(/dev/null): %s", strerror(errno));
428
429 debug("Executing command: '%.500s'", cmd);
430
431 /* Fork and execute the command. */
432 if ((pid = fork()) == 0) {
433 char *argv[4];
434
435 /* Child. Permanently give up superuser privileges. */
436 permanently_drop_suid(original_real_uid);
437
438 /* Redirect child stdin and stdout. Leave stderr */
439 if (dup2(devnull, STDIN_FILENO) == -1)
440 fatal("dup2: %s", strerror(errno));
441 if (dup2(devnull, STDOUT_FILENO) == -1)
442 fatal("dup2: %s", strerror(errno));
443 if (devnull > STDERR_FILENO)
444 close(devnull);
445 closefrom(STDERR_FILENO + 1);
446
447 argv[0] = shell;
448 argv[1] = "-c";
449 argv[2] = command_string;
450 argv[3] = NULL;
451
452 execv(argv[0], argv);
453 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
454 /* Die with signal to make this error apparent to parent. */
455 signal(SIGTERM, SIG_DFL);
456 kill(getpid(), SIGTERM);
457 _exit(1);
458 }
459 /* Parent. */
460 if (pid < 0)
461 fatal("%s: fork: %.100s", __func__, strerror(errno));
462
463 close(devnull);
464 free(command_string);
465
466 while (waitpid(pid, &status, 0) == -1) {
467 if (errno != EINTR && errno != EAGAIN)
468 fatal("%s: waitpid: %s", __func__, strerror(errno));
469 }
470 if (!WIFEXITED(status)) {
471 error("command '%.100s' exited abnormally", cmd);
472 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000473 }
Damien Miller194fd902013-10-15 12:13:05 +1100474 debug3("command returned status %d", WEXITSTATUS(status));
475 return WEXITSTATUS(status);
476}
477
478/*
479 * Parse and execute a Match directive.
480 */
481static int
482match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000483 const char *host_arg, const char *original_host, int post_canon,
484 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100485{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000486 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100487 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000488 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100489 size_t len;
490 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
491
492 /*
493 * Configuration is likely to be incomplete at this point so we
494 * must be prepared to use default values.
495 */
496 port = options->port <= 0 ? default_ssh_port() : options->port;
497 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100498 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100499 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100500 host = percent_expand(options->hostname,
501 "h", host_arg, (char *)NULL);
502 } else
503 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100504
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000505 debug2("checking match for '%s' host %s originally %s",
506 cp, host, original_host);
507 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
508 criteria = NULL;
509 this_result = 1;
510 if ((negate = attrib[0] == '!'))
511 attrib++;
512 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100513 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000514 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100515 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000516 error("%.200s line %d: '%s' cannot be combined "
517 "with other Match attributes",
518 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100519 result = -1;
520 goto out;
521 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000522 if (result)
523 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100524 goto out;
525 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000526 attributes++;
527 if (strcasecmp(attrib, "canonical") == 0) {
528 r = !!post_canon; /* force bitmask member to boolean */
529 if (r == (negate ? 1 : 0))
530 this_result = result = 0;
531 debug3("%.200s line %d: %smatched '%s'",
532 filename, linenum,
533 this_result ? "" : "not ", oattrib);
534 continue;
535 }
536 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100537 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
538 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100539 result = -1;
540 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100541 }
542 len = strlen(arg);
543 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000544 criteria = xstrdup(host);
545 r = match_hostname(host, arg, len) == 1;
546 if (r == (negate ? 1 : 0))
547 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100548 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000549 criteria = xstrdup(original_host);
550 r = match_hostname(original_host, arg, len) == 1;
551 if (r == (negate ? 1 : 0))
552 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100553 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000554 criteria = xstrdup(ruser);
555 r = match_pattern_list(ruser, arg, len, 0) == 1;
556 if (r == (negate ? 1 : 0))
557 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100558 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000559 criteria = xstrdup(pw->pw_name);
560 r = match_pattern_list(pw->pw_name, arg, len, 0) == 1;
561 if (r == (negate ? 1 : 0))
562 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100563 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100564 if (gethostname(thishost, sizeof(thishost)) == -1)
565 fatal("gethostname: %s", strerror(errno));
566 strlcpy(shorthost, thishost, sizeof(shorthost));
567 shorthost[strcspn(thishost, ".")] = '\0';
568 snprintf(portstr, sizeof(portstr), "%d", port);
569
570 cmd = percent_expand(arg,
571 "L", shorthost,
572 "d", pw->pw_dir,
573 "h", host,
574 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000575 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100576 "p", portstr,
577 "r", ruser,
578 "u", pw->pw_name,
579 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100580 if (result != 1) {
581 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 debug3("%.200s line %d: skipped exec "
583 "\"%.100s\"", filename, linenum, cmd);
584 free(cmd);
585 continue;
Damien Miller06287802014-02-24 15:56:45 +1100586 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000587 r = execute_in_shell(cmd);
588 if (r == -1) {
589 fatal("%.200s line %d: match exec "
590 "'%.100s' error", filename,
591 linenum, cmd);
592 }
593 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100594 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000595 /* Force exit status to boolean */
596 r = r == 0;
597 if (r == (negate ? 1 : 0))
598 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100599 } else {
600 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100601 result = -1;
602 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100603 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000604 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
605 filename, linenum, this_result ? "": "not ",
606 oattrib, criteria);
607 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100608 }
Damien Millercf31f382013-10-24 21:02:56 +1100609 if (attributes == 0) {
610 error("One or more attributes required for Match");
611 result = -1;
612 goto out;
613 }
Damien Miller084bcd22013-10-23 16:30:51 +1100614 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000615 if (result != -1)
616 debug2("match %sfound", result ? "" : "not ");
617 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100618 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100619 return result;
620}
621
Damien Miller0faf7472013-10-17 11:47:23 +1100622/* Check and prepare a domain name: removes trailing '.' and lowercases */
623static void
624valid_domain(char *name, const char *filename, int linenum)
625{
626 size_t i, l = strlen(name);
627 u_char c, last = '\0';
628
629 if (l == 0)
630 fatal("%s line %d: empty hostname suffix", filename, linenum);
631 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
632 fatal("%s line %d: hostname suffix \"%.100s\" "
633 "starts with invalid character", filename, linenum, name);
634 for (i = 0; i < l; i++) {
635 c = tolower((u_char)name[i]);
636 name[i] = (char)c;
637 if (last == '.' && c == '.')
638 fatal("%s line %d: hostname suffix \"%.100s\" contains "
639 "consecutive separators", filename, linenum, name);
640 if (c != '.' && c != '-' && !isalnum(c) &&
641 c != '_') /* technically invalid, but common */
642 fatal("%s line %d: hostname suffix \"%.100s\" contains "
643 "invalid characters", filename, linenum, name);
644 last = c;
645 }
646 if (name[l - 1] == '.')
647 name[l - 1] = '\0';
648}
649
Damien Miller5428f641999-11-25 11:54:57 +1100650/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000651 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100652 */
Damien Miller4af51302000-04-16 11:18:38 +1000653static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000654parse_token(const char *cp, const char *filename, int linenum,
655 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656{
Darren Tucker07636982013-05-16 20:30:03 +1000657 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658
Damien Miller95def091999-11-25 00:26:21 +1100659 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000660 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100661 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000662 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
663 strlen(ignored_unknown), 1) == 1)
664 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000665 error("%s: line %d: Bad configuration option: %s",
666 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100667 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668}
669
Damien Millere9fc72e2013-10-15 12:14:12 +1100670/* Multistate option parsing */
671struct multistate {
672 char *key;
673 int value;
674};
675static const struct multistate multistate_flag[] = {
676 { "true", 1 },
677 { "false", 0 },
678 { "yes", 1 },
679 { "no", 0 },
680 { NULL, -1 }
681};
682static const struct multistate multistate_yesnoask[] = {
683 { "true", 1 },
684 { "false", 0 },
685 { "yes", 1 },
686 { "no", 0 },
687 { "ask", 2 },
688 { NULL, -1 }
689};
690static const struct multistate multistate_addressfamily[] = {
691 { "inet", AF_INET },
692 { "inet6", AF_INET6 },
693 { "any", AF_UNSPEC },
694 { NULL, -1 }
695};
696static const struct multistate multistate_controlmaster[] = {
697 { "true", SSHCTL_MASTER_YES },
698 { "yes", SSHCTL_MASTER_YES },
699 { "false", SSHCTL_MASTER_NO },
700 { "no", SSHCTL_MASTER_NO },
701 { "auto", SSHCTL_MASTER_AUTO },
702 { "ask", SSHCTL_MASTER_ASK },
703 { "autoask", SSHCTL_MASTER_AUTO_ASK },
704 { NULL, -1 }
705};
706static const struct multistate multistate_tunnel[] = {
707 { "ethernet", SSH_TUNMODE_ETHERNET },
708 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
709 { "true", SSH_TUNMODE_DEFAULT },
710 { "yes", SSH_TUNMODE_DEFAULT },
711 { "false", SSH_TUNMODE_NO },
712 { "no", SSH_TUNMODE_NO },
713 { NULL, -1 }
714};
715static const struct multistate multistate_requesttty[] = {
716 { "true", REQUEST_TTY_YES },
717 { "yes", REQUEST_TTY_YES },
718 { "false", REQUEST_TTY_NO },
719 { "no", REQUEST_TTY_NO },
720 { "force", REQUEST_TTY_FORCE },
721 { "auto", REQUEST_TTY_AUTO },
722 { NULL, -1 }
723};
Damien Miller38505592013-10-17 11:48:13 +1100724static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100725 { "true", SSH_CANONICALISE_YES },
726 { "false", SSH_CANONICALISE_NO },
727 { "yes", SSH_CANONICALISE_YES },
728 { "no", SSH_CANONICALISE_NO },
729 { "always", SSH_CANONICALISE_ALWAYS },
730 { NULL, -1 }
731};
Damien Millere9fc72e2013-10-15 12:14:12 +1100732
Damien Miller5428f641999-11-25 11:54:57 +1100733/*
734 * Processes a single option line as used in the configuration files. This
735 * only sets those values that have not already been set.
736 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100737#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100738int
Damien Miller194fd902013-10-15 12:13:05 +1100739process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000740 const char *original_host, char *line, const char *filename,
741 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742{
Damien Miller295ee632011-05-29 21:42:31 +1000743 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
744 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000745 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100746 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100747 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000748 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100749 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000750 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100751 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100752 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753
Damien Miller194fd902013-10-15 12:13:05 +1100754 if (activep == NULL) { /* We are processing a command line directive */
755 cmdline = 1;
756 activep = &cmdline;
757 }
758
Damien Millerc652cac2003-05-14 13:40:54 +1000759 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100760 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000761 if (strchr(WHITESPACE, line[len]) == NULL)
762 break;
763 line[len] = '\0';
764 }
765
Damien Millerbe484b52000-07-15 14:14:16 +1000766 s = line;
767 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100768 if ((keyword = strdelim(&s)) == NULL)
769 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000770 /* Ignore leading whitespace. */
771 if (*keyword == '\0')
772 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000773 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100774 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000775 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100776 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777
Darren Tucker07636982013-05-16 20:30:03 +1000778 opcode = parse_token(keyword, filename, linenum,
779 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780
Damien Miller95def091999-11-25 00:26:21 +1100781 switch (opcode) {
782 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100783 /* don't panic, but count bad options */
784 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100785 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000786 case oIgnoredUnknownOption:
787 debug("%s line %d: Ignored unknown option \"%s\"",
788 filename, linenum, keyword);
789 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000790 case oConnectTimeout:
791 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100792parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000793 arg = strdelim(&s);
794 if (!arg || *arg == '\0')
795 fatal("%s line %d: missing time value.",
796 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000797 if (strcmp(arg, "none") == 0)
798 value = -1;
799 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000800 fatal("%s line %d: invalid time value.",
801 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100802 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000803 *intptr = value;
804 break;
805
Damien Miller95def091999-11-25 00:26:21 +1100806 case oForwardAgent:
807 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100808 parse_flag:
809 multistate_ptr = multistate_flag;
810 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000811 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000812 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100813 fatal("%s line %d: missing argument.",
814 filename, linenum);
815 value = -1;
816 for (i = 0; multistate_ptr[i].key != NULL; i++) {
817 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
818 value = multistate_ptr[i].value;
819 break;
820 }
821 }
822 if (value == -1)
823 fatal("%s line %d: unsupported option \"%s\".",
824 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100825 if (*activep && *intptr == -1)
826 *intptr = value;
827 break;
828
829 case oForwardX11:
830 intptr = &options->forward_x11;
831 goto parse_flag;
832
Darren Tucker0a118da2003-10-15 15:54:32 +1000833 case oForwardX11Trusted:
834 intptr = &options->forward_x11_trusted;
835 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000836
Damien Miller1ab6a512010-06-26 10:02:24 +1000837 case oForwardX11Timeout:
838 intptr = &options->forward_x11_timeout;
839 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000840
Damien Miller95def091999-11-25 00:26:21 +1100841 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000842 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100843 goto parse_flag;
844
Darren Tuckere7d4b192006-07-12 22:17:10 +1000845 case oExitOnForwardFailure:
846 intptr = &options->exit_on_forward_failure;
847 goto parse_flag;
848
Damien Miller95def091999-11-25 00:26:21 +1100849 case oUsePrivilegedPort:
850 intptr = &options->use_privileged_port;
851 goto parse_flag;
852
Damien Miller95def091999-11-25 00:26:21 +1100853 case oPasswordAuthentication:
854 intptr = &options->password_authentication;
855 goto parse_flag;
856
Damien Miller874d77b2000-10-14 16:23:11 +1100857 case oKbdInteractiveAuthentication:
858 intptr = &options->kbd_interactive_authentication;
859 goto parse_flag;
860
861 case oKbdInteractiveDevices:
862 charptr = &options->kbd_interactive_devices;
863 goto parse_string;
864
Damien Miller0bc1bd82000-11-13 22:57:25 +1100865 case oPubkeyAuthentication:
866 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000867 goto parse_flag;
868
Damien Miller95def091999-11-25 00:26:21 +1100869 case oRSAAuthentication:
870 intptr = &options->rsa_authentication;
871 goto parse_flag;
872
873 case oRhostsRSAAuthentication:
874 intptr = &options->rhosts_rsa_authentication;
875 goto parse_flag;
876
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000877 case oHostbasedAuthentication:
878 intptr = &options->hostbased_authentication;
879 goto parse_flag;
880
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000881 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000882 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100883 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000884
Darren Tucker0efd1552003-08-26 11:49:55 +1000885 case oGssAuthentication:
886 intptr = &options->gss_authentication;
887 goto parse_flag;
888
889 case oGssDelegateCreds:
890 intptr = &options->gss_deleg_creds;
891 goto parse_flag;
892
Damien Miller95def091999-11-25 00:26:21 +1100893 case oBatchMode:
894 intptr = &options->batch_mode;
895 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896
Damien Miller95def091999-11-25 00:26:21 +1100897 case oCheckHostIP:
898 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000899 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
Damien Miller37876e92003-05-15 10:19:46 +1000901 case oVerifyHostKeyDNS:
902 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100903 multistate_ptr = multistate_yesnoask;
904 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000905
Damien Miller95def091999-11-25 00:26:21 +1100906 case oStrictHostKeyChecking:
907 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100908 multistate_ptr = multistate_yesnoask;
909 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910
Damien Miller95def091999-11-25 00:26:21 +1100911 case oCompression:
912 intptr = &options->compression;
913 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller12c150e2003-12-17 16:31:10 +1100915 case oTCPKeepAlive:
916 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100917 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000919 case oNoHostAuthenticationForLocalhost:
920 intptr = &options->no_host_authentication_for_localhost;
921 goto parse_flag;
922
Damien Miller95def091999-11-25 00:26:21 +1100923 case oNumberOfPasswordPrompts:
924 intptr = &options->number_of_password_prompts;
925 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926
Damien Miller95def091999-11-25 00:26:21 +1100927 case oCompressionLevel:
928 intptr = &options->compression_level;
929 goto parse_int;
930
Damien Millera5539d22003-04-09 20:50:06 +1000931 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000932 arg = strdelim(&s);
933 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000934 fatal("%.200s line %d: Missing argument.", filename,
935 linenum);
936 if (strcmp(arg, "default") == 0) {
937 val64 = 0;
938 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000939 if (scan_scaled(arg, &val64) == -1)
940 fatal("%.200s line %d: Bad number '%s': %s",
941 filename, linenum, arg, strerror(errno));
942 /* check for too-large or too-small limits */
943 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000944 fatal("%.200s line %d: RekeyLimit too large",
945 filename, linenum);
946 if (val64 != 0 && val64 < 16)
947 fatal("%.200s line %d: RekeyLimit too small",
948 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000949 }
Damien Miller3dff1762008-02-10 22:25:52 +1100950 if (*activep && options->rekey_limit == -1)
951 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000952 if (s != NULL) { /* optional rekey interval present */
953 if (strcmp(s, "none") == 0) {
954 (void)strdelim(&s); /* discard */
955 break;
956 }
957 intptr = &options->rekey_interval;
958 goto parse_time;
959 }
Damien Millera5539d22003-04-09 20:50:06 +1000960 break;
961
Damien Miller95def091999-11-25 00:26:21 +1100962 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000963 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000964 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100965 fatal("%.200s line %d: Missing argument.", filename, linenum);
966 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100967 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000968 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100969 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100970 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000971 add_identity_file(options, NULL,
972 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +1100973 }
974 break;
975
Damien Millerd3a18572000-06-07 19:55:44 +1000976 case oXAuthLocation:
977 charptr=&options->xauth_location;
978 goto parse_string;
979
Damien Miller95def091999-11-25 00:26:21 +1100980 case oUser:
981 charptr = &options->user;
982parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000983 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000984 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000985 fatal("%.200s line %d: Missing argument.",
986 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100987 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000988 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100989 break;
990
991 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000992 cpptr = (char **)&options->system_hostfiles;
993 uintptr = &options->num_system_hostfiles;
994 max_entries = SSH_MAX_HOSTS_FILES;
995parse_char_array:
996 if (*activep && *uintptr == 0) {
997 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
998 if ((*uintptr) >= max_entries)
999 fatal("%s line %d: "
1000 "too many authorized keys files.",
1001 filename, linenum);
1002 cpptr[(*uintptr)++] = xstrdup(arg);
1003 }
1004 }
1005 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001006
1007 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001008 cpptr = (char **)&options->user_hostfiles;
1009 uintptr = &options->num_user_hostfiles;
1010 max_entries = SSH_MAX_HOSTS_FILES;
1011 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001012
Damien Miller95def091999-11-25 00:26:21 +11001013 case oHostName:
1014 charptr = &options->hostname;
1015 goto parse_string;
1016
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001017 case oHostKeyAlias:
1018 charptr = &options->host_key_alias;
1019 goto parse_string;
1020
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001021 case oPreferredAuthentications:
1022 charptr = &options->preferred_authentications;
1023 goto parse_string;
1024
Ben Lindstrome0f88042001-04-30 13:06:24 +00001025 case oBindAddress:
1026 charptr = &options->bind_address;
1027 goto parse_string;
1028
Damien Miller7ea845e2010-02-12 09:21:02 +11001029 case oPKCS11Provider:
1030 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001031 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001032
Damien Miller95def091999-11-25 00:26:21 +11001033 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001034 charptr = &options->proxy_command;
1035parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001036 if (s == NULL)
1037 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001038 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001039 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001040 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001041 return 0;
1042
1043 case oPort:
1044 intptr = &options->port;
1045parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001046 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001047 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001048 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001049 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001050 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001051
1052 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001053 value = strtol(arg, &endofnumber, 0);
1054 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001055 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001056 if (*activep && *intptr == -1)
1057 *intptr = value;
1058 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059
Damien Miller95def091999-11-25 00:26:21 +11001060 case oConnectionAttempts:
1061 intptr = &options->connection_attempts;
1062 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001063
Damien Miller95def091999-11-25 00:26:21 +11001064 case oCipher:
1065 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001066 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001067 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001068 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001069 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001070 if (value == -1)
1071 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001072 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001073 if (*activep && *intptr == -1)
1074 *intptr = value;
1075 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076
Damien Miller78928792000-04-12 20:17:38 +10001077 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001078 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001079 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001080 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001081 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001082 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001083 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001084 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001085 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001086 break;
1087
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001088 case oMacs:
1089 arg = strdelim(&s);
1090 if (!arg || *arg == '\0')
1091 fatal("%.200s line %d: Missing argument.", filename, linenum);
1092 if (!mac_valid(arg))
1093 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001094 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001095 if (*activep && options->macs == NULL)
1096 options->macs = xstrdup(arg);
1097 break;
1098
Damien Millerd5f62bf2010-09-24 22:11:14 +10001099 case oKexAlgorithms:
1100 arg = strdelim(&s);
1101 if (!arg || *arg == '\0')
1102 fatal("%.200s line %d: Missing argument.",
1103 filename, linenum);
1104 if (!kex_names_valid(arg))
1105 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1106 filename, linenum, arg ? arg : "<NONE>");
1107 if (*activep && options->kex_algorithms == NULL)
1108 options->kex_algorithms = xstrdup(arg);
1109 break;
1110
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001111 case oHostKeyAlgorithms:
1112 arg = strdelim(&s);
1113 if (!arg || *arg == '\0')
1114 fatal("%.200s line %d: Missing argument.", filename, linenum);
1115 if (!key_names_valid2(arg))
1116 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001117 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001118 if (*activep && options->hostkeyalgorithms == NULL)
1119 options->hostkeyalgorithms = xstrdup(arg);
1120 break;
1121
Damien Miller78928792000-04-12 20:17:38 +10001122 case oProtocol:
1123 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001124 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001125 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001126 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001127 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001128 if (value == SSH_PROTO_UNKNOWN)
1129 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001130 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001131 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1132 *intptr = value;
1133 break;
1134
Damien Miller95def091999-11-25 00:26:21 +11001135 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001136 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001137 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001138 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001139 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001140 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001141 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001142 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1143 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001144 break;
1145
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001146 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001147 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001148 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001149 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001150 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001151 fatal("%.200s line %d: Missing port argument.",
1152 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001153
Damien Millera699d952008-11-03 19:27:34 +11001154 if (opcode == oLocalForward ||
1155 opcode == oRemoteForward) {
1156 arg2 = strdelim(&s);
1157 if (arg2 == NULL || *arg2 == '\0')
1158 fatal("%.200s line %d: Missing target argument.",
1159 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001160
Damien Millera699d952008-11-03 19:27:34 +11001161 /* construct a string for parse_forward */
1162 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1163 } else if (opcode == oDynamicForward) {
1164 strlcpy(fwdarg, arg, sizeof(fwdarg));
1165 }
1166
1167 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001168 opcode == oDynamicForward ? 1 : 0,
1169 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001170 fatal("%.200s line %d: Bad forwarding specification.",
1171 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001172
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001173 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001174 if (opcode == oLocalForward ||
1175 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001176 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001177 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001178 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001179 }
Damien Miller95def091999-11-25 00:26:21 +11001180 break;
1181
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001182 case oClearAllForwardings:
1183 intptr = &options->clear_forwardings;
1184 goto parse_flag;
1185
Damien Miller95def091999-11-25 00:26:21 +11001186 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001187 if (cmdline)
1188 fatal("Host directive not supported as a command-line "
1189 "option");
Damien Miller95def091999-11-25 00:26:21 +11001190 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001191 arg2 = NULL;
1192 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1193 negated = *arg == '!';
1194 if (negated)
1195 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001196 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001197 if (negated) {
1198 debug("%.200s line %d: Skipping Host "
1199 "block because of negated match "
1200 "for %.100s", filename, linenum,
1201 arg);
1202 *activep = 0;
1203 break;
1204 }
1205 if (!*activep)
1206 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001207 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001208 }
Damien Millerfe924212011-05-15 08:44:45 +10001209 }
1210 if (*activep)
1211 debug("%.200s line %d: Applying options for %.100s",
1212 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001213 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001214 return 0;
1215
Damien Miller194fd902013-10-15 12:13:05 +11001216 case oMatch:
1217 if (cmdline)
1218 fatal("Host directive not supported as a command-line "
1219 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001220 value = match_cfg_line(options, &s, pw, host, original_host,
1221 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001222 if (value < 0)
1223 fatal("%.200s line %d: Bad Match condition", filename,
1224 linenum);
1225 *activep = value;
1226 break;
1227
Damien Miller95def091999-11-25 00:26:21 +11001228 case oEscapeChar:
1229 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001230 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001231 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001232 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001233 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001234 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1235 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001236 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001237 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001238 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001239 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001240 else {
1241 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001242 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001243 /* NOTREACHED */
1244 value = 0; /* Avoid compiler warning. */
1245 }
1246 if (*activep && *intptr == -1)
1247 *intptr = value;
1248 break;
1249
Damien Miller20a8f972003-05-18 20:50:30 +10001250 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001251 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001252 multistate_ptr = multistate_addressfamily;
1253 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001254
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001255 case oEnableSSHKeysign:
1256 intptr = &options->enable_ssh_keysign;
1257 goto parse_flag;
1258
Damien Millerbd394c32004-03-08 23:12:36 +11001259 case oIdentitiesOnly:
1260 intptr = &options->identities_only;
1261 goto parse_flag;
1262
Damien Miller509b0102003-12-17 16:33:10 +11001263 case oServerAliveInterval:
1264 intptr = &options->server_alive_interval;
1265 goto parse_time;
1266
1267 case oServerAliveCountMax:
1268 intptr = &options->server_alive_count_max;
1269 goto parse_int;
1270
Darren Tucker46bc0752004-05-02 22:11:30 +10001271 case oSendEnv:
1272 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1273 if (strchr(arg, '=') != NULL)
1274 fatal("%s line %d: Invalid environment name.",
1275 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001276 if (!*activep)
1277 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001278 if (options->num_send_env >= MAX_SEND_ENV)
1279 fatal("%s line %d: too many send env.",
1280 filename, linenum);
1281 options->send_env[options->num_send_env++] =
1282 xstrdup(arg);
1283 }
1284 break;
1285
Damien Miller0e220db2004-06-15 10:34:08 +10001286 case oControlPath:
1287 charptr = &options->control_path;
1288 goto parse_string;
1289
1290 case oControlMaster:
1291 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001292 multistate_ptr = multistate_controlmaster;
1293 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001294
Damien Millere11e1ea2010-08-03 16:04:46 +10001295 case oControlPersist:
1296 /* no/false/yes/true, or a time spec */
1297 intptr = &options->control_persist;
1298 arg = strdelim(&s);
1299 if (!arg || *arg == '\0')
1300 fatal("%.200s line %d: Missing ControlPersist"
1301 " argument.", filename, linenum);
1302 value = 0;
1303 value2 = 0; /* timeout */
1304 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1305 value = 0;
1306 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1307 value = 1;
1308 else if ((value2 = convtime(arg)) >= 0)
1309 value = 1;
1310 else
1311 fatal("%.200s line %d: Bad ControlPersist argument.",
1312 filename, linenum);
1313 if (*activep && *intptr == -1) {
1314 *intptr = value;
1315 options->control_persist_timeout = value2;
1316 }
1317 break;
1318
Damien Millere1776152005-03-01 21:47:37 +11001319 case oHashKnownHosts:
1320 intptr = &options->hash_known_hosts;
1321 goto parse_flag;
1322
Damien Millerd27b9472005-12-13 19:29:02 +11001323 case oTunnel:
1324 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001325 multistate_ptr = multistate_tunnel;
1326 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001327
1328 case oTunnelDevice:
1329 arg = strdelim(&s);
1330 if (!arg || *arg == '\0')
1331 fatal("%.200s line %d: Missing argument.", filename, linenum);
1332 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001333 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001334 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1335 if (*activep) {
1336 options->tun_local = value;
1337 options->tun_remote = value2;
1338 }
1339 break;
1340
1341 case oLocalCommand:
1342 charptr = &options->local_command;
1343 goto parse_command;
1344
1345 case oPermitLocalCommand:
1346 intptr = &options->permit_local_command;
1347 goto parse_flag;
1348
Damien Miller10288242008-06-30 00:04:03 +10001349 case oVisualHostKey:
1350 intptr = &options->visual_host_key;
1351 goto parse_flag;
1352
Damien Miller0dac6fb2010-11-20 15:19:38 +11001353 case oIPQoS:
1354 arg = strdelim(&s);
1355 if ((value = parse_ipqos(arg)) == -1)
1356 fatal("%s line %d: Bad IPQoS value: %s",
1357 filename, linenum, arg);
1358 arg = strdelim(&s);
1359 if (arg == NULL)
1360 value2 = value;
1361 else if ((value2 = parse_ipqos(arg)) == -1)
1362 fatal("%s line %d: Bad IPQoS value: %s",
1363 filename, linenum, arg);
1364 if (*activep) {
1365 options->ip_qos_interactive = value;
1366 options->ip_qos_bulk = value2;
1367 }
1368 break;
1369
Darren Tucker71e4d542009-07-06 07:12:27 +10001370 case oUseRoaming:
1371 intptr = &options->use_roaming;
1372 goto parse_flag;
1373
Damien Miller21771e22011-05-15 08:45:50 +10001374 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001375 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001376 multistate_ptr = multistate_requesttty;
1377 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001378
Darren Tucker07636982013-05-16 20:30:03 +10001379 case oIgnoreUnknown:
1380 charptr = &options->ignored_unknown;
1381 goto parse_string;
1382
Damien Miller1262b662013-08-21 02:44:24 +10001383 case oProxyUseFdpass:
1384 intptr = &options->proxy_use_fdpass;
1385 goto parse_flag;
1386
Damien Miller0faf7472013-10-17 11:47:23 +11001387 case oCanonicalDomains:
1388 value = options->num_canonical_domains != 0;
1389 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1390 valid_domain(arg, filename, linenum);
1391 if (!*activep || value)
1392 continue;
1393 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1394 fatal("%s line %d: too many hostname suffixes.",
1395 filename, linenum);
1396 options->canonical_domains[
1397 options->num_canonical_domains++] = xstrdup(arg);
1398 }
1399 break;
1400
Damien Miller38505592013-10-17 11:48:13 +11001401 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001402 value = options->num_permitted_cnames != 0;
1403 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1404 /* Either '*' for everything or 'list:list' */
1405 if (strcmp(arg, "*") == 0)
1406 arg2 = arg;
1407 else {
1408 lowercase(arg);
1409 if ((arg2 = strchr(arg, ':')) == NULL ||
1410 arg2[1] == '\0') {
1411 fatal("%s line %d: "
1412 "Invalid permitted CNAME \"%s\"",
1413 filename, linenum, arg);
1414 }
1415 *arg2 = '\0';
1416 arg2++;
1417 }
1418 if (!*activep || value)
1419 continue;
1420 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1421 fatal("%s line %d: too many permitted CNAMEs.",
1422 filename, linenum);
1423 cname = options->permitted_cnames +
1424 options->num_permitted_cnames++;
1425 cname->source_list = xstrdup(arg);
1426 cname->target_list = xstrdup(arg2);
1427 }
1428 break;
1429
Damien Miller38505592013-10-17 11:48:13 +11001430 case oCanonicalizeHostname:
1431 intptr = &options->canonicalize_hostname;
1432 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001433 goto parse_multistate;
1434
Damien Miller38505592013-10-17 11:48:13 +11001435 case oCanonicalizeMaxDots:
1436 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001437 goto parse_int;
1438
Damien Miller38505592013-10-17 11:48:13 +11001439 case oCanonicalizeFallbackLocal:
1440 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001441 goto parse_flag;
1442
Damien Miller7acefbb2014-07-18 14:11:24 +10001443 case oStreamLocalBindMask:
1444 arg = strdelim(&s);
1445 if (!arg || *arg == '\0')
1446 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1447 /* Parse mode in octal format */
1448 value = strtol(arg, &endofnumber, 8);
1449 if (arg == endofnumber || value < 0 || value > 0777)
1450 fatal("%.200s line %d: Bad mask.", filename, linenum);
1451 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1452 break;
1453
1454 case oStreamLocalBindUnlink:
1455 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1456 goto parse_flag;
1457
Ben Lindstrom4daea862002-06-09 20:04:02 +00001458 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001459 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001460 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001461 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001462
Damien Millerf9b3feb2003-05-16 11:38:32 +10001463 case oUnsupported:
1464 error("%s line %d: Unsupported option \"%s\"",
1465 filename, linenum, keyword);
1466 return 0;
1467
Damien Miller95def091999-11-25 00:26:21 +11001468 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001469 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001470 }
1471
1472 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001473 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001474 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001475 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001476 }
Damien Miller95def091999-11-25 00:26:21 +11001477 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478}
1479
1480
Damien Miller5428f641999-11-25 11:54:57 +11001481/*
1482 * Reads the config file and modifies the options accordingly. Options
1483 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001484 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001485 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001487int
Damien Miller194fd902013-10-15 12:13:05 +11001488read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001489 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490{
Damien Miller95def091999-11-25 00:26:21 +11001491 FILE *f;
1492 char line[1024];
1493 int active, linenum;
1494 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495
Damien Miller57a44762004-04-20 20:11:57 +10001496 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001497 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498
Darren Tuckeraefa3682013-04-05 11:18:35 +11001499 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001500 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001501
Damien Miller33793852004-06-15 10:27:55 +10001502 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001503 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001504 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001505 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001506 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001507 }
1508
Damien Miller95def091999-11-25 00:26:21 +11001509 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510
Damien Miller5428f641999-11-25 11:54:57 +11001511 /*
1512 * Mark that we are now processing the options. This flag is turned
1513 * on/off by Host specifications.
1514 */
Damien Miller95def091999-11-25 00:26:21 +11001515 active = 1;
1516 linenum = 0;
1517 while (fgets(line, sizeof(line), f)) {
1518 /* Update line number counter. */
1519 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001520 if (process_config_line(options, pw, host, original_host,
1521 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001522 bad_options++;
1523 }
1524 fclose(f);
1525 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001526 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001527 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001528 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529}
1530
Damien Miller13f97b22014-02-24 15:57:55 +11001531/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1532int
1533option_clear_or_none(const char *o)
1534{
1535 return o == NULL || strcasecmp(o, "none") == 0;
1536}
1537
Damien Miller5428f641999-11-25 11:54:57 +11001538/*
1539 * Initializes options to special values that indicate that they have not yet
1540 * been set. Read_config_file will only set options with this value. Options
1541 * are processed in the following order: command line, user config file,
1542 * system config file. Last, fill_default_options is called.
1543 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001544
Damien Miller4af51302000-04-16 11:18:38 +10001545void
Damien Miller95def091999-11-25 00:26:21 +11001546initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001547{
Damien Miller95def091999-11-25 00:26:21 +11001548 memset(options, 'X', sizeof(*options));
1549 options->forward_agent = -1;
1550 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001551 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001552 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001553 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001554 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001555 options->fwd_opts.gateway_ports = -1;
1556 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1557 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001558 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001559 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001560 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001561 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001562 options->gss_authentication = -1;
1563 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001564 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001565 options->kbd_interactive_authentication = -1;
1566 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001567 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001568 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001569 options->batch_mode = -1;
1570 options->check_host_ip = -1;
1571 options->strict_host_key_checking = -1;
1572 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001573 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001574 options->compression_level = -1;
1575 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001576 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001577 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001578 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001579 options->number_of_password_prompts = -1;
1580 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001581 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001582 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001583 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001584 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001585 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001586 options->num_identity_files = 0;
1587 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001588 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001589 options->proxy_command = NULL;
1590 options->user = NULL;
1591 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001592 options->num_system_hostfiles = 0;
1593 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001594 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001595 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001596 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001597 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001598 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001599 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001600 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001601 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001602 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001603 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001604 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001605 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001606 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001607 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001608 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001609 options->server_alive_interval = -1;
1610 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001611 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001612 options->control_path = NULL;
1613 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001614 options->control_persist = -1;
1615 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001616 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001617 options->tun_open = -1;
1618 options->tun_local = -1;
1619 options->tun_remote = -1;
1620 options->local_command = NULL;
1621 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001622 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001623 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001624 options->ip_qos_interactive = -1;
1625 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001626 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001627 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001628 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001629 options->num_canonical_domains = 0;
1630 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001631 options->canonicalize_max_dots = -1;
1632 options->canonicalize_fallback_local = -1;
1633 options->canonicalize_hostname = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001634}
1635
Damien Miller5428f641999-11-25 11:54:57 +11001636/*
Damien Miller13f97b22014-02-24 15:57:55 +11001637 * A petite version of fill_default_options() that just fills the options
1638 * needed for hostname canonicalization to proceed.
1639 */
1640void
1641fill_default_options_for_canonicalization(Options *options)
1642{
1643 if (options->canonicalize_max_dots == -1)
1644 options->canonicalize_max_dots = 1;
1645 if (options->canonicalize_fallback_local == -1)
1646 options->canonicalize_fallback_local = 1;
1647 if (options->canonicalize_hostname == -1)
1648 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1649}
1650
1651/*
Damien Miller5428f641999-11-25 11:54:57 +11001652 * Called after processing other sources of option data, this fills those
1653 * options for which no value has been specified with their default values.
1654 */
Damien Miller4af51302000-04-16 11:18:38 +10001655void
Damien Miller95def091999-11-25 00:26:21 +11001656fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001657{
Damien Miller95def091999-11-25 00:26:21 +11001658 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001659 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001660 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001661 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001662 if (options->forward_x11_trusted == -1)
1663 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001664 if (options->forward_x11_timeout == -1)
1665 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001666 if (options->exit_on_forward_failure == -1)
1667 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001668 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001669 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001670 if (options->fwd_opts.gateway_ports == -1)
1671 options->fwd_opts.gateway_ports = 0;
1672 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1673 options->fwd_opts.streamlocal_bind_mask = 0177;
1674 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1675 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001676 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001677 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001678 if (options->rsa_authentication == -1)
1679 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001680 if (options->pubkey_authentication == -1)
1681 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001682 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001683 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001684 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001685 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001686 if (options->gss_deleg_creds == -1)
1687 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001688 if (options->password_authentication == -1)
1689 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001690 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001691 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001692 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001693 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001694 if (options->hostbased_authentication == -1)
1695 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001696 if (options->batch_mode == -1)
1697 options->batch_mode = 0;
1698 if (options->check_host_ip == -1)
1699 options->check_host_ip = 1;
1700 if (options->strict_host_key_checking == -1)
1701 options->strict_host_key_checking = 2; /* 2 is default */
1702 if (options->compression == -1)
1703 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001704 if (options->tcp_keep_alive == -1)
1705 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001706 if (options->compression_level == -1)
1707 options->compression_level = 6;
1708 if (options->port == -1)
1709 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001710 if (options->address_family == -1)
1711 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001712 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001713 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001714 if (options->number_of_password_prompts == -1)
1715 options->number_of_password_prompts = 3;
1716 /* Selected in ssh_login(). */
1717 if (options->cipher == -1)
1718 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001719 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001720 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001721 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001722 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001723 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001724 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001725 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001726 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001727 add_identity_file(options, "~/",
1728 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001729 }
1730 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001731 add_identity_file(options, "~/",
1732 _PATH_SSH_CLIENT_ID_RSA, 0);
1733 add_identity_file(options, "~/",
1734 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001735#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001736 add_identity_file(options, "~/",
1737 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001738#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001739 add_identity_file(options, "~/",
1740 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001741 }
Damien Millereba71ba2000-04-29 23:57:08 +10001742 }
Damien Miller95def091999-11-25 00:26:21 +11001743 if (options->escape_char == -1)
1744 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001745 if (options->num_system_hostfiles == 0) {
1746 options->system_hostfiles[options->num_system_hostfiles++] =
1747 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1748 options->system_hostfiles[options->num_system_hostfiles++] =
1749 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1750 }
1751 if (options->num_user_hostfiles == 0) {
1752 options->user_hostfiles[options->num_user_hostfiles++] =
1753 xstrdup(_PATH_SSH_USER_HOSTFILE);
1754 options->user_hostfiles[options->num_user_hostfiles++] =
1755 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1756 }
Damien Millerfcd93202002-02-05 12:26:34 +11001757 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001758 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001759 if (options->clear_forwardings == 1)
1760 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001761 if (options->no_host_authentication_for_localhost == - 1)
1762 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001763 if (options->identities_only == -1)
1764 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001765 if (options->enable_ssh_keysign == -1)
1766 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001767 if (options->rekey_limit == -1)
1768 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001769 if (options->rekey_interval == -1)
1770 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001771 if (options->verify_host_key_dns == -1)
1772 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001773 if (options->server_alive_interval == -1)
1774 options->server_alive_interval = 0;
1775 if (options->server_alive_count_max == -1)
1776 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001777 if (options->control_master == -1)
1778 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001779 if (options->control_persist == -1) {
1780 options->control_persist = 0;
1781 options->control_persist_timeout = 0;
1782 }
Damien Millere1776152005-03-01 21:47:37 +11001783 if (options->hash_known_hosts == -1)
1784 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001785 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001786 options->tun_open = SSH_TUNMODE_NO;
1787 if (options->tun_local == -1)
1788 options->tun_local = SSH_TUNID_ANY;
1789 if (options->tun_remote == -1)
1790 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001791 if (options->permit_local_command == -1)
1792 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001793 if (options->use_roaming == -1)
1794 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001795 if (options->visual_host_key == -1)
1796 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001797 if (options->ip_qos_interactive == -1)
1798 options->ip_qos_interactive = IPTOS_LOWDELAY;
1799 if (options->ip_qos_bulk == -1)
1800 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001801 if (options->request_tty == -1)
1802 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001803 if (options->proxy_use_fdpass == -1)
1804 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001805 if (options->canonicalize_max_dots == -1)
1806 options->canonicalize_max_dots = 1;
1807 if (options->canonicalize_fallback_local == -1)
1808 options->canonicalize_fallback_local = 1;
1809 if (options->canonicalize_hostname == -1)
1810 options->canonicalize_hostname = SSH_CANONICALISE_NO;
Damien Millere9fc72e2013-10-15 12:14:12 +11001811#define CLEAR_ON_NONE(v) \
1812 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001813 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001814 free(v); \
1815 v = NULL; \
1816 } \
1817 } while(0)
1818 CLEAR_ON_NONE(options->local_command);
1819 CLEAR_ON_NONE(options->proxy_command);
1820 CLEAR_ON_NONE(options->control_path);
Damien Miller95def091999-11-25 00:26:21 +11001821 /* options->user will be set in the main program if appropriate */
1822 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001823 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001824 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001825}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001826
Damien Miller7acefbb2014-07-18 14:11:24 +10001827struct fwdarg {
1828 char *arg;
1829 int ispath;
1830};
1831
1832/*
1833 * parse_fwd_field
1834 * parses the next field in a port forwarding specification.
1835 * sets fwd to the parsed field and advances p past the colon
1836 * or sets it to NULL at end of string.
1837 * returns 0 on success, else non-zero.
1838 */
1839static int
1840parse_fwd_field(char **p, struct fwdarg *fwd)
1841{
1842 char *ep, *cp = *p;
1843 int ispath = 0;
1844
1845 if (*cp == '\0') {
1846 *p = NULL;
1847 return -1; /* end of string */
1848 }
1849
1850 /*
1851 * A field escaped with square brackets is used literally.
1852 * XXX - allow ']' to be escaped via backslash?
1853 */
1854 if (*cp == '[') {
1855 /* find matching ']' */
1856 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1857 if (*ep == '/')
1858 ispath = 1;
1859 }
1860 /* no matching ']' or not at end of field. */
1861 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1862 return -1;
1863 /* NUL terminate the field and advance p past the colon */
1864 *ep++ = '\0';
1865 if (*ep != '\0')
1866 *ep++ = '\0';
1867 fwd->arg = cp + 1;
1868 fwd->ispath = ispath;
1869 *p = ep;
1870 return 0;
1871 }
1872
1873 for (cp = *p; *cp != '\0'; cp++) {
1874 switch (*cp) {
1875 case '\\':
1876 memmove(cp, cp + 1, strlen(cp + 1) + 1);
1877 cp++;
1878 break;
1879 case '/':
1880 ispath = 1;
1881 break;
1882 case ':':
1883 *cp++ = '\0';
1884 goto done;
1885 }
1886 }
1887done:
1888 fwd->arg = *p;
1889 fwd->ispath = ispath;
1890 *p = cp;
1891 return 0;
1892}
1893
Damien Millerf91ee4c2005-03-01 21:24:33 +11001894/*
1895 * parse_forward
1896 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001897 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10001898 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
1899 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11001900 * dynamicfwd == 1
1901 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001902 * returns number of arguments parsed or zero on error
1903 */
1904int
Damien Miller7acefbb2014-07-18 14:11:24 +10001905parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001906{
Damien Miller7acefbb2014-07-18 14:11:24 +10001907 struct fwdarg fwdargs[4];
1908 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001909 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001910
Damien Miller7acefbb2014-07-18 14:11:24 +10001911 memset(fwd, 0, sizeof(*fwd));
1912 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11001913
1914 cp = p = xstrdup(fwdspec);
1915
1916 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001917 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001918 cp++;
1919
Damien Miller7acefbb2014-07-18 14:11:24 +10001920 for (i = 0; i < 4; ++i) {
1921 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001922 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10001923 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001924
Damien Millerf4b39532008-11-03 19:28:21 +11001925 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10001926 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001927 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10001928 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001929
1930 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001931 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10001932 if (fwdargs[0].ispath) {
1933 fwd->listen_path = xstrdup(fwdargs[0].arg);
1934 fwd->listen_port = PORT_STREAMLOCAL;
1935 } else {
1936 fwd->listen_host = NULL;
1937 fwd->listen_port = a2port(fwdargs[0].arg);
1938 }
Damien Millera699d952008-11-03 19:27:34 +11001939 fwd->connect_host = xstrdup("socks");
1940 break;
1941
1942 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10001943 if (fwdargs[0].ispath && fwdargs[1].ispath) {
1944 fwd->listen_path = xstrdup(fwdargs[0].arg);
1945 fwd->listen_port = PORT_STREAMLOCAL;
1946 fwd->connect_path = xstrdup(fwdargs[1].arg);
1947 fwd->connect_port = PORT_STREAMLOCAL;
1948 } else if (fwdargs[1].ispath) {
1949 fwd->listen_host = NULL;
1950 fwd->listen_port = a2port(fwdargs[0].arg);
1951 fwd->connect_path = xstrdup(fwdargs[1].arg);
1952 fwd->connect_port = PORT_STREAMLOCAL;
1953 } else {
1954 fwd->listen_host = xstrdup(fwdargs[0].arg);
1955 fwd->listen_port = a2port(fwdargs[1].arg);
1956 fwd->connect_host = xstrdup("socks");
1957 }
Damien Millera699d952008-11-03 19:27:34 +11001958 break;
1959
Damien Millerf91ee4c2005-03-01 21:24:33 +11001960 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10001961 if (fwdargs[0].ispath) {
1962 fwd->listen_path = xstrdup(fwdargs[0].arg);
1963 fwd->listen_port = PORT_STREAMLOCAL;
1964 fwd->connect_host = xstrdup(fwdargs[1].arg);
1965 fwd->connect_port = a2port(fwdargs[2].arg);
1966 } else if (fwdargs[2].ispath) {
1967 fwd->listen_host = xstrdup(fwdargs[0].arg);
1968 fwd->listen_port = a2port(fwdargs[1].arg);
1969 fwd->connect_path = xstrdup(fwdargs[2].arg);
1970 fwd->connect_port = PORT_STREAMLOCAL;
1971 } else {
1972 fwd->listen_host = NULL;
1973 fwd->listen_port = a2port(fwdargs[0].arg);
1974 fwd->connect_host = xstrdup(fwdargs[1].arg);
1975 fwd->connect_port = a2port(fwdargs[2].arg);
1976 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001977 break;
1978
1979 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10001980 fwd->listen_host = xstrdup(fwdargs[0].arg);
1981 fwd->listen_port = a2port(fwdargs[1].arg);
1982 fwd->connect_host = xstrdup(fwdargs[2].arg);
1983 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001984 break;
1985 default:
1986 i = 0; /* failure */
1987 }
1988
Darren Tuckera627d422013-06-02 07:31:17 +10001989 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001990
Damien Millera699d952008-11-03 19:27:34 +11001991 if (dynamicfwd) {
1992 if (!(i == 1 || i == 2))
1993 goto fail_free;
1994 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001995 if (!(i == 3 || i == 4)) {
1996 if (fwd->connect_path == NULL &&
1997 fwd->listen_path == NULL)
1998 goto fail_free;
1999 }
2000 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002001 goto fail_free;
2002 }
2003
Damien Miller7acefbb2014-07-18 14:11:24 +10002004 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2005 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002006 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002007 if (fwd->connect_host != NULL &&
2008 strlen(fwd->connect_host) >= NI_MAXHOST)
2009 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002010 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2011 if (fwd->connect_path != NULL &&
2012 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2013 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002014 if (fwd->listen_host != NULL &&
2015 strlen(fwd->listen_host) >= NI_MAXHOST)
2016 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002017 if (fwd->listen_path != NULL &&
2018 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2019 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002020
2021 return (i);
2022
2023 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002024 free(fwd->connect_host);
2025 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002026 free(fwd->connect_path);
2027 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002028 free(fwd->listen_host);
2029 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002030 free(fwd->listen_path);
2031 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002032 return (0);
2033}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002034
2035/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2036static const char *
2037fmt_multistate_int(int val, const struct multistate *m)
2038{
2039 u_int i;
2040
2041 for (i = 0; m[i].key != NULL; i++) {
2042 if (m[i].value == val)
2043 return m[i].key;
2044 }
2045 return "UNKNOWN";
2046}
2047
2048static const char *
2049fmt_intarg(OpCodes code, int val)
2050{
2051 if (val == -1)
2052 return "unset";
2053 switch (code) {
2054 case oAddressFamily:
2055 return fmt_multistate_int(val, multistate_addressfamily);
2056 case oVerifyHostKeyDNS:
2057 case oStrictHostKeyChecking:
2058 return fmt_multistate_int(val, multistate_yesnoask);
2059 case oControlMaster:
2060 return fmt_multistate_int(val, multistate_controlmaster);
2061 case oTunnel:
2062 return fmt_multistate_int(val, multistate_tunnel);
2063 case oRequestTTY:
2064 return fmt_multistate_int(val, multistate_requesttty);
2065 case oCanonicalizeHostname:
2066 return fmt_multistate_int(val, multistate_canonicalizehostname);
2067 case oProtocol:
2068 switch (val) {
2069 case SSH_PROTO_1:
2070 return "1";
2071 case SSH_PROTO_2:
2072 return "2";
2073 case (SSH_PROTO_1|SSH_PROTO_2):
2074 return "2,1";
2075 default:
2076 return "UNKNOWN";
2077 }
2078 default:
2079 switch (val) {
2080 case 0:
2081 return "no";
2082 case 1:
2083 return "yes";
2084 default:
2085 return "UNKNOWN";
2086 }
2087 }
2088}
2089
2090static const char *
2091lookup_opcode_name(OpCodes code)
2092{
2093 u_int i;
2094
2095 for (i = 0; keywords[i].name != NULL; i++)
2096 if (keywords[i].opcode == code)
2097 return(keywords[i].name);
2098 return "UNKNOWN";
2099}
2100
2101static void
2102dump_cfg_int(OpCodes code, int val)
2103{
2104 printf("%s %d\n", lookup_opcode_name(code), val);
2105}
2106
2107static void
2108dump_cfg_fmtint(OpCodes code, int val)
2109{
2110 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2111}
2112
2113static void
2114dump_cfg_string(OpCodes code, const char *val)
2115{
2116 if (val == NULL)
2117 return;
2118 printf("%s %s\n", lookup_opcode_name(code), val);
2119}
2120
2121static void
2122dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2123{
2124 u_int i;
2125
2126 for (i = 0; i < count; i++)
2127 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2128}
2129
2130static void
2131dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2132{
2133 u_int i;
2134
2135 printf("%s", lookup_opcode_name(code));
2136 for (i = 0; i < count; i++)
2137 printf(" %s", vals[i]);
2138 printf("\n");
2139}
2140
2141static void
2142dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2143{
2144 const struct Forward *fwd;
2145 u_int i;
2146
2147 /* oDynamicForward */
2148 for (i = 0; i < count; i++) {
2149 fwd = &fwds[i];
2150 if (code == oDynamicForward &&
2151 strcmp(fwd->connect_host, "socks") != 0)
2152 continue;
2153 if (code == oLocalForward &&
2154 strcmp(fwd->connect_host, "socks") == 0)
2155 continue;
2156 printf("%s", lookup_opcode_name(code));
2157 if (fwd->listen_port == PORT_STREAMLOCAL)
2158 printf(" %s", fwd->listen_path);
2159 else if (fwd->listen_host == NULL)
2160 printf(" %d", fwd->listen_port);
2161 else {
2162 printf(" [%s]:%d",
2163 fwd->listen_host, fwd->listen_port);
2164 }
2165 if (code != oDynamicForward) {
2166 if (fwd->connect_port == PORT_STREAMLOCAL)
2167 printf(" %s", fwd->connect_path);
2168 else if (fwd->connect_host == NULL)
2169 printf(" %d", fwd->connect_port);
2170 else {
2171 printf(" [%s]:%d",
2172 fwd->connect_host, fwd->connect_port);
2173 }
2174 }
2175 printf("\n");
2176 }
2177}
2178
2179void
2180dump_client_config(Options *o, const char *host)
2181{
2182 int i;
2183 char vbuf[5];
2184
2185 /* Most interesting options first: user, host, port */
2186 dump_cfg_string(oUser, o->user);
2187 dump_cfg_string(oHostName, host);
2188 dump_cfg_int(oPort, o->port);
2189
2190 /* Flag options */
2191 dump_cfg_fmtint(oAddressFamily, o->address_family);
2192 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2193 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2194 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2195 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2196 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2197 dump_cfg_fmtint(oCompression, o->compression);
2198 dump_cfg_fmtint(oControlMaster, o->control_master);
2199 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2200 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
2201 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2202 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2203 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2204 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2205#ifdef GSSAPI
2206 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2207 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2208#endif /* GSSAPI */
2209 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2210 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2211 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2212 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2213 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2214 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2215 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2216 dump_cfg_fmtint(oProtocol, o->protocol);
2217 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2218 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2219 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2220 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2221 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2222 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2223 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2224 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2225 dump_cfg_fmtint(oTunnel, o->tun_open);
2226 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2227 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2228 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
2229
2230 /* Integer options */
2231 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2232 dump_cfg_int(oCompressionLevel, o->compression_level);
2233 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2234 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2235 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2236 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2237 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2238
2239 /* String options */
2240 dump_cfg_string(oBindAddress, o->bind_address);
2241 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2242 dump_cfg_string(oControlPath, o->control_path);
2243 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms ? o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
2244 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
2245 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2246 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2247 dump_cfg_string(oLocalCommand, o->local_command);
2248 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2249 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2250 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2251 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2252 dump_cfg_string(oProxyCommand, o->proxy_command);
2253 dump_cfg_string(oXAuthLocation, o->xauth_location);
2254
2255 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2256 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2257 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2258
2259 /* String array options */
2260 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2261 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2262 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2263 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2264 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2265
2266 /* Special cases */
2267
2268 /* oConnectTimeout */
2269 if (o->connection_timeout == -1)
2270 printf("connecttimeout none\n");
2271 else
2272 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2273
2274 /* oTunnelDevice */
2275 printf("tunneldevice");
2276 if (o->tun_local == SSH_TUNID_ANY)
2277 printf(" any");
2278 else
2279 printf(" %d", o->tun_local);
2280 if (o->tun_remote == SSH_TUNID_ANY)
2281 printf(":any");
2282 else
2283 printf(":%d", o->tun_remote);
2284 printf("\n");
2285
2286 /* oCanonicalizePermittedCNAMEs */
2287 if ( o->num_permitted_cnames > 0) {
2288 printf("canonicalizePermittedcnames");
2289 for (i = 0; i < o->num_permitted_cnames; i++) {
2290 printf(" %s:%s", o->permitted_cnames[i].source_list,
2291 o->permitted_cnames[i].target_list);
2292 }
2293 printf("\n");
2294 }
2295
2296 /* oCipher */
2297 if (o->cipher != SSH_CIPHER_NOT_SET)
2298 printf("Cipher %s\n", cipher_name(o->cipher));
2299
2300 /* oControlPersist */
2301 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2302 dump_cfg_fmtint(oControlPersist, o->control_persist);
2303 else
2304 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2305
2306 /* oEscapeChar */
2307 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2308 printf("escapechar none\n");
2309 else {
2310 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2311 printf("escapechar %s\n", vbuf);
2312 }
2313
2314 /* oIPQoS */
2315 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2316 printf("%s\n", iptos2str(o->ip_qos_bulk));
2317
2318 /* oRekeyLimit */
2319 printf("rekeylimit %lld %d\n",
2320 (long long)o->rekey_limit, o->rekey_interval);
2321
2322 /* oStreamLocalBindMask */
2323 printf("streamlocalbindmask 0%o\n",
2324 o->fwd_opts.streamlocal_bind_mask);
2325}