blob: a122d176d22de9c064990efc8e7779e335239e2f [file] [log] [blame]
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001/* $OpenBSD: readconf.c,v 1.227 2015/01/15 09:40:00 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"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000054#include "sshkey.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"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000058#include "kex.h"
59#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110060#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000061#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000062#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
64/* Format of the configuration file:
65
66 # Configuration data is parsed as follows:
67 # 1. command line options
68 # 2. user-specific file
69 # 3. system-wide file
70 # Any configuration value is only changed the first time it is set.
71 # Thus, host-specific definitions should be at the beginning of the
72 # configuration file, and defaults at the end.
73
74 # Host-specific declarations. These may override anything above. A single
75 # host may match multiple declarations; these are processed in the order
76 # that they are given in.
77
78 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000079 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
81 Host fake.com
82 HostName another.host.name.real.org
83 User blaah
84 Port 34289
85 ForwardX11 no
86 ForwardAgent no
87
88 Host books.com
89 RemoteForward 9999 shadows.cs.hut.fi:9999
90 Cipher 3des
91
92 Host fascist.blob.com
93 Port 23123
94 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095 PasswordAuthentication no
96
97 Host puukko.hut.fi
98 User t35124p
99 ProxyCommand ssh-proxy %h %p
100
101 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000102 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104 Host *.su
105 Cipher none
106 PasswordAuthentication no
107
Damien Millerd27b9472005-12-13 19:29:02 +1100108 Host vpn.fake.com
109 Tunnel yes
110 TunnelDevice 3
111
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112 # Defaults for various options
113 Host *
114 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 PasswordAuthentication yes
117 RSAAuthentication yes
118 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100120 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121 IdentityFile ~/.ssh/identity
122 Port 22
123 EscapeChar ~
124
125*/
126
127/* Keyword tokens. */
128
Damien Miller95def091999-11-25 00:26:21 +1100129typedef enum {
130 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100131 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000132 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
133 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000134 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000135 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100136 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
Damien Miller194fd902013-10-15 12:13:05 +1100137 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100138 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
139 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100140 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000141 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000142 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000143 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000144 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100145 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000146 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000147 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000148 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100149 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000150 oSendEnv, oControlPath, oControlMaster, oControlPersist,
151 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100152 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Miller7cc194f2014-02-04 11:12:56 +1100153 oVisualHostKey, oUseRoaming,
Damien Miller1262b662013-08-21 02:44:24 +1000154 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100155 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
156 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000157 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000158 oFingerprintHash,
Darren Tucker07636982013-05-16 20:30:03 +1000159 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160} OpCodes;
161
162/* Textual representations of the tokens. */
163
Damien Miller95def091999-11-25 00:26:21 +1100164static struct {
165 const char *name;
166 OpCodes opcode;
167} keywords[] = {
168 { "forwardagent", oForwardAgent },
169 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000170 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000171 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000172 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000173 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100174 { "gatewayports", oGatewayPorts },
175 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000176 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100178 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
179 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100181 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000182 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000183 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000184 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000185 { "challengeresponseauthentication", oChallengeResponseAuthentication },
186 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
187 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000188 { "kerberosauthentication", oUnsupported },
189 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000190 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000191#if defined(GSSAPI)
192 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000193 { "gssapidelegatecredentials", oGssDelegateCreds },
194#else
195 { "gssapiauthentication", oUnsupported },
196 { "gssapidelegatecredentials", oUnsupported },
197#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000198 { "fallbacktorsh", oDeprecated },
199 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100200 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100201 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100202 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100203 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000204 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100205 { "proxycommand", oProxyCommand },
206 { "port", oPort },
207 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000208 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000209 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000210 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100211 { "remoteforward", oRemoteForward },
212 { "localforward", oLocalForward },
213 { "user", oUser },
214 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100215 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100216 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100217 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000218 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100219 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000220 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100221 { "connectionattempts", oConnectionAttempts },
222 { "batchmode", oBatchMode },
223 { "checkhostip", oCheckHostIP },
224 { "stricthostkeychecking", oStrictHostKeyChecking },
225 { "compression", oCompression },
226 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100227 { "tcpkeepalive", oTCPKeepAlive },
228 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100229 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100230 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000231 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000232 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000233 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000234 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100235#ifdef ENABLE_PKCS11
236 { "smartcarddevice", oPKCS11Provider },
237 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000238#else
239 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100240 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000241#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100242 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000243 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000244 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100245 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000246 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000247 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000248 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100249 { "serveraliveinterval", oServerAliveInterval },
250 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000251 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000252 { "controlpath", oControlPath },
253 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000254 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100255 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100256 { "tunnel", oTunnel },
257 { "tunneldevice", oTunnelDevice },
258 { "localcommand", oLocalCommand },
259 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000260 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000261 { "useroaming", oUseRoaming },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000262 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100263 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000264 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000265 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100266 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100267 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
268 { "canonicalizehostname", oCanonicalizeHostname },
269 { "canonicalizemaxdots", oCanonicalizeMaxDots },
270 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000271 { "streamlocalbindmask", oStreamLocalBindMask },
272 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000273 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000274 { "fingerprinthash", oFingerprintHash },
Darren Tucker07636982013-05-16 20:30:03 +1000275 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100276
Ben Lindstrom65366a82001-12-06 16:32:47 +0000277 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100278};
279
Damien Miller5428f641999-11-25 11:54:57 +1100280/*
281 * Adds a local TCP/IP port forward to options. Never returns if there is an
282 * error.
283 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284
Damien Miller4af51302000-04-16 11:18:38 +1000285void
Damien Miller7acefbb2014-07-18 14:11:24 +1000286add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287{
Damien Miller7acefbb2014-07-18 14:11:24 +1000288 struct Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000289#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100290 extern uid_t original_real_uid;
Damien Miller7acefbb2014-07-18 14:11:24 +1000291 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
292 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000293 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100294#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000295 options->local_forwards = xrealloc(options->local_forwards,
296 options->num_local_forwards + 1,
297 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100298 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100299
Damien Miller1a0442f2008-11-05 16:30:06 +1100300 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100301 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000302 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100303 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100304 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000305 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
Damien Miller5428f641999-11-25 11:54:57 +1100308/*
309 * Adds a remote TCP/IP port forward to options. Never returns if there is
310 * an error.
311 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312
Damien Miller4af51302000-04-16 11:18:38 +1000313void
Damien Miller7acefbb2014-07-18 14:11:24 +1000314add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315{
Damien Miller7acefbb2014-07-18 14:11:24 +1000316 struct Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000317
318 options->remote_forwards = xrealloc(options->remote_forwards,
319 options->num_remote_forwards + 1,
320 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100321 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100322
Damien Miller1a0442f2008-11-05 16:30:06 +1100323 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100324 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000325 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100326 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100327 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000328 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100329 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000330 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331}
332
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000333static void
334clear_forwardings(Options *options)
335{
336 int i;
337
Damien Millerf91ee4c2005-03-01 21:24:33 +1100338 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000339 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000341 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100343 }
Damien Miller232cfb12010-06-26 09:50:30 +1000344 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000345 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000346 options->local_forwards = NULL;
347 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000348 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100349 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000350 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000351 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000352 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000353 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100354 }
Damien Miller232cfb12010-06-26 09:50:30 +1000355 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000356 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000357 options->remote_forwards = NULL;
358 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000359 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100360 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000361}
362
Darren Tucker19104782013-04-05 11:13:08 +1100363void
364add_identity_file(Options *options, const char *dir, const char *filename,
365 int userprovided)
366{
367 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000368 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100369
370 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
371 fatal("Too many identity files specified (max %d)",
372 SSH_MAX_IDENTITY_FILES);
373
374 if (dir == NULL) /* no dir, filename is absolute */
375 path = xstrdup(filename);
376 else
377 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
378
Damien Miller15271902014-05-15 13:47:56 +1000379 /* Avoid registering duplicates */
380 for (i = 0; i < options->num_identity_files; i++) {
381 if (options->identity_file_userprovided[i] == userprovided &&
382 strcmp(options->identity_files[i], path) == 0) {
383 debug2("%s: ignoring duplicate key %s", __func__, path);
384 free(path);
385 return;
386 }
387 }
388
Darren Tucker19104782013-04-05 11:13:08 +1100389 options->identity_file_userprovided[options->num_identity_files] =
390 userprovided;
391 options->identity_files[options->num_identity_files++] = path;
392}
393
Damien Miller194fd902013-10-15 12:13:05 +1100394int
395default_ssh_port(void)
396{
397 static int port;
398 struct servent *sp;
399
400 if (port == 0) {
401 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
402 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
403 }
404 return port;
405}
406
407/*
408 * Execute a command in a shell.
409 * Return its exit status or -1 on abnormal exit.
410 */
411static int
412execute_in_shell(const char *cmd)
413{
414 char *shell, *command_string;
415 pid_t pid;
416 int devnull, status;
417 extern uid_t original_real_uid;
418
419 if ((shell = getenv("SHELL")) == NULL)
420 shell = _PATH_BSHELL;
421
422 /*
423 * Use "exec" to avoid "sh -c" processes on some platforms
424 * (e.g. Solaris)
425 */
426 xasprintf(&command_string, "exec %s", cmd);
427
428 /* Need this to redirect subprocess stdin/out */
429 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
430 fatal("open(/dev/null): %s", strerror(errno));
431
432 debug("Executing command: '%.500s'", cmd);
433
434 /* Fork and execute the command. */
435 if ((pid = fork()) == 0) {
436 char *argv[4];
437
438 /* Child. Permanently give up superuser privileges. */
439 permanently_drop_suid(original_real_uid);
440
441 /* Redirect child stdin and stdout. Leave stderr */
442 if (dup2(devnull, STDIN_FILENO) == -1)
443 fatal("dup2: %s", strerror(errno));
444 if (dup2(devnull, STDOUT_FILENO) == -1)
445 fatal("dup2: %s", strerror(errno));
446 if (devnull > STDERR_FILENO)
447 close(devnull);
448 closefrom(STDERR_FILENO + 1);
449
450 argv[0] = shell;
451 argv[1] = "-c";
452 argv[2] = command_string;
453 argv[3] = NULL;
454
455 execv(argv[0], argv);
456 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
457 /* Die with signal to make this error apparent to parent. */
458 signal(SIGTERM, SIG_DFL);
459 kill(getpid(), SIGTERM);
460 _exit(1);
461 }
462 /* Parent. */
463 if (pid < 0)
464 fatal("%s: fork: %.100s", __func__, strerror(errno));
465
466 close(devnull);
467 free(command_string);
468
469 while (waitpid(pid, &status, 0) == -1) {
470 if (errno != EINTR && errno != EAGAIN)
471 fatal("%s: waitpid: %s", __func__, strerror(errno));
472 }
473 if (!WIFEXITED(status)) {
474 error("command '%.100s' exited abnormally", cmd);
475 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000476 }
Damien Miller194fd902013-10-15 12:13:05 +1100477 debug3("command returned status %d", WEXITSTATUS(status));
478 return WEXITSTATUS(status);
479}
480
481/*
482 * Parse and execute a Match directive.
483 */
484static int
485match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000486 const char *host_arg, const char *original_host, int post_canon,
487 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100488{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000489 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100490 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000491 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100492 size_t len;
493 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
494
495 /*
496 * Configuration is likely to be incomplete at this point so we
497 * must be prepared to use default values.
498 */
499 port = options->port <= 0 ? default_ssh_port() : options->port;
500 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100501 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100502 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100503 host = percent_expand(options->hostname,
504 "h", host_arg, (char *)NULL);
505 } else
506 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100507
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000508 debug2("checking match for '%s' host %s originally %s",
509 cp, host, original_host);
510 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
511 criteria = NULL;
512 this_result = 1;
513 if ((negate = attrib[0] == '!'))
514 attrib++;
515 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100516 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000517 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100518 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000519 error("%.200s line %d: '%s' cannot be combined "
520 "with other Match attributes",
521 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100522 result = -1;
523 goto out;
524 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000525 if (result)
526 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100527 goto out;
528 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000529 attributes++;
530 if (strcasecmp(attrib, "canonical") == 0) {
531 r = !!post_canon; /* force bitmask member to boolean */
532 if (r == (negate ? 1 : 0))
533 this_result = result = 0;
534 debug3("%.200s line %d: %smatched '%s'",
535 filename, linenum,
536 this_result ? "" : "not ", oattrib);
537 continue;
538 }
539 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100540 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
541 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100542 result = -1;
543 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100544 }
545 len = strlen(arg);
546 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000547 criteria = xstrdup(host);
548 r = match_hostname(host, arg, len) == 1;
549 if (r == (negate ? 1 : 0))
550 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100551 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000552 criteria = xstrdup(original_host);
553 r = match_hostname(original_host, arg, len) == 1;
554 if (r == (negate ? 1 : 0))
555 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100556 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000557 criteria = xstrdup(ruser);
558 r = match_pattern_list(ruser, arg, len, 0) == 1;
559 if (r == (negate ? 1 : 0))
560 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100561 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000562 criteria = xstrdup(pw->pw_name);
563 r = match_pattern_list(pw->pw_name, arg, len, 0) == 1;
564 if (r == (negate ? 1 : 0))
565 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100566 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100567 if (gethostname(thishost, sizeof(thishost)) == -1)
568 fatal("gethostname: %s", strerror(errno));
569 strlcpy(shorthost, thishost, sizeof(shorthost));
570 shorthost[strcspn(thishost, ".")] = '\0';
571 snprintf(portstr, sizeof(portstr), "%d", port);
572
573 cmd = percent_expand(arg,
574 "L", shorthost,
575 "d", pw->pw_dir,
576 "h", host,
577 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000578 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100579 "p", portstr,
580 "r", ruser,
581 "u", pw->pw_name,
582 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100583 if (result != 1) {
584 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000585 debug3("%.200s line %d: skipped exec "
586 "\"%.100s\"", filename, linenum, cmd);
587 free(cmd);
588 continue;
Damien Miller06287802014-02-24 15:56:45 +1100589 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000590 r = execute_in_shell(cmd);
591 if (r == -1) {
592 fatal("%.200s line %d: match exec "
593 "'%.100s' error", filename,
594 linenum, cmd);
595 }
596 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100597 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000598 /* Force exit status to boolean */
599 r = r == 0;
600 if (r == (negate ? 1 : 0))
601 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100602 } else {
603 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100604 result = -1;
605 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100606 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000607 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
608 filename, linenum, this_result ? "": "not ",
609 oattrib, criteria);
610 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100611 }
Damien Millercf31f382013-10-24 21:02:56 +1100612 if (attributes == 0) {
613 error("One or more attributes required for Match");
614 result = -1;
615 goto out;
616 }
Damien Miller084bcd22013-10-23 16:30:51 +1100617 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000618 if (result != -1)
619 debug2("match %sfound", result ? "" : "not ");
620 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100621 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100622 return result;
623}
624
Damien Miller0faf7472013-10-17 11:47:23 +1100625/* Check and prepare a domain name: removes trailing '.' and lowercases */
626static void
627valid_domain(char *name, const char *filename, int linenum)
628{
629 size_t i, l = strlen(name);
630 u_char c, last = '\0';
631
632 if (l == 0)
633 fatal("%s line %d: empty hostname suffix", filename, linenum);
634 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
635 fatal("%s line %d: hostname suffix \"%.100s\" "
636 "starts with invalid character", filename, linenum, name);
637 for (i = 0; i < l; i++) {
638 c = tolower((u_char)name[i]);
639 name[i] = (char)c;
640 if (last == '.' && c == '.')
641 fatal("%s line %d: hostname suffix \"%.100s\" contains "
642 "consecutive separators", filename, linenum, name);
643 if (c != '.' && c != '-' && !isalnum(c) &&
644 c != '_') /* technically invalid, but common */
645 fatal("%s line %d: hostname suffix \"%.100s\" contains "
646 "invalid characters", filename, linenum, name);
647 last = c;
648 }
649 if (name[l - 1] == '.')
650 name[l - 1] = '\0';
651}
652
Damien Miller5428f641999-11-25 11:54:57 +1100653/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000654 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100655 */
Damien Miller4af51302000-04-16 11:18:38 +1000656static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000657parse_token(const char *cp, const char *filename, int linenum,
658 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659{
Darren Tucker07636982013-05-16 20:30:03 +1000660 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Damien Miller95def091999-11-25 00:26:21 +1100662 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000663 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100664 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000665 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
666 strlen(ignored_unknown), 1) == 1)
667 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000668 error("%s: line %d: Bad configuration option: %s",
669 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100670 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671}
672
Damien Millere9fc72e2013-10-15 12:14:12 +1100673/* Multistate option parsing */
674struct multistate {
675 char *key;
676 int value;
677};
678static const struct multistate multistate_flag[] = {
679 { "true", 1 },
680 { "false", 0 },
681 { "yes", 1 },
682 { "no", 0 },
683 { NULL, -1 }
684};
685static const struct multistate multistate_yesnoask[] = {
686 { "true", 1 },
687 { "false", 0 },
688 { "yes", 1 },
689 { "no", 0 },
690 { "ask", 2 },
691 { NULL, -1 }
692};
693static const struct multistate multistate_addressfamily[] = {
694 { "inet", AF_INET },
695 { "inet6", AF_INET6 },
696 { "any", AF_UNSPEC },
697 { NULL, -1 }
698};
699static const struct multistate multistate_controlmaster[] = {
700 { "true", SSHCTL_MASTER_YES },
701 { "yes", SSHCTL_MASTER_YES },
702 { "false", SSHCTL_MASTER_NO },
703 { "no", SSHCTL_MASTER_NO },
704 { "auto", SSHCTL_MASTER_AUTO },
705 { "ask", SSHCTL_MASTER_ASK },
706 { "autoask", SSHCTL_MASTER_AUTO_ASK },
707 { NULL, -1 }
708};
709static const struct multistate multistate_tunnel[] = {
710 { "ethernet", SSH_TUNMODE_ETHERNET },
711 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
712 { "true", SSH_TUNMODE_DEFAULT },
713 { "yes", SSH_TUNMODE_DEFAULT },
714 { "false", SSH_TUNMODE_NO },
715 { "no", SSH_TUNMODE_NO },
716 { NULL, -1 }
717};
718static const struct multistate multistate_requesttty[] = {
719 { "true", REQUEST_TTY_YES },
720 { "yes", REQUEST_TTY_YES },
721 { "false", REQUEST_TTY_NO },
722 { "no", REQUEST_TTY_NO },
723 { "force", REQUEST_TTY_FORCE },
724 { "auto", REQUEST_TTY_AUTO },
725 { NULL, -1 }
726};
Damien Miller38505592013-10-17 11:48:13 +1100727static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100728 { "true", SSH_CANONICALISE_YES },
729 { "false", SSH_CANONICALISE_NO },
730 { "yes", SSH_CANONICALISE_YES },
731 { "no", SSH_CANONICALISE_NO },
732 { "always", SSH_CANONICALISE_ALWAYS },
733 { NULL, -1 }
734};
Damien Millere9fc72e2013-10-15 12:14:12 +1100735
Damien Miller5428f641999-11-25 11:54:57 +1100736/*
737 * Processes a single option line as used in the configuration files. This
738 * only sets those values that have not already been set.
739 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100740#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100741int
Damien Miller194fd902013-10-15 12:13:05 +1100742process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000743 const char *original_host, char *line, const char *filename,
744 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745{
Damien Miller295ee632011-05-29 21:42:31 +1000746 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
747 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000748 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100749 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100750 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000751 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100752 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000753 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100754 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100755 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756
Damien Miller194fd902013-10-15 12:13:05 +1100757 if (activep == NULL) { /* We are processing a command line directive */
758 cmdline = 1;
759 activep = &cmdline;
760 }
761
Damien Millerc652cac2003-05-14 13:40:54 +1000762 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100763 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000764 if (strchr(WHITESPACE, line[len]) == NULL)
765 break;
766 line[len] = '\0';
767 }
768
Damien Millerbe484b52000-07-15 14:14:16 +1000769 s = line;
770 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100771 if ((keyword = strdelim(&s)) == NULL)
772 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000773 /* Ignore leading whitespace. */
774 if (*keyword == '\0')
775 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000776 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100777 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000778 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100779 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780
Darren Tucker07636982013-05-16 20:30:03 +1000781 opcode = parse_token(keyword, filename, linenum,
782 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783
Damien Miller95def091999-11-25 00:26:21 +1100784 switch (opcode) {
785 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100786 /* don't panic, but count bad options */
787 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100788 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000789 case oIgnoredUnknownOption:
790 debug("%s line %d: Ignored unknown option \"%s\"",
791 filename, linenum, keyword);
792 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000793 case oConnectTimeout:
794 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100795parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000796 arg = strdelim(&s);
797 if (!arg || *arg == '\0')
798 fatal("%s line %d: missing time value.",
799 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000800 if (strcmp(arg, "none") == 0)
801 value = -1;
802 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000803 fatal("%s line %d: invalid time value.",
804 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100805 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000806 *intptr = value;
807 break;
808
Damien Miller95def091999-11-25 00:26:21 +1100809 case oForwardAgent:
810 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100811 parse_flag:
812 multistate_ptr = multistate_flag;
813 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000814 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000815 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100816 fatal("%s line %d: missing argument.",
817 filename, linenum);
818 value = -1;
819 for (i = 0; multistate_ptr[i].key != NULL; i++) {
820 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
821 value = multistate_ptr[i].value;
822 break;
823 }
824 }
825 if (value == -1)
826 fatal("%s line %d: unsupported option \"%s\".",
827 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100828 if (*activep && *intptr == -1)
829 *intptr = value;
830 break;
831
832 case oForwardX11:
833 intptr = &options->forward_x11;
834 goto parse_flag;
835
Darren Tucker0a118da2003-10-15 15:54:32 +1000836 case oForwardX11Trusted:
837 intptr = &options->forward_x11_trusted;
838 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000839
Damien Miller1ab6a512010-06-26 10:02:24 +1000840 case oForwardX11Timeout:
841 intptr = &options->forward_x11_timeout;
842 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000843
Damien Miller95def091999-11-25 00:26:21 +1100844 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000845 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100846 goto parse_flag;
847
Darren Tuckere7d4b192006-07-12 22:17:10 +1000848 case oExitOnForwardFailure:
849 intptr = &options->exit_on_forward_failure;
850 goto parse_flag;
851
Damien Miller95def091999-11-25 00:26:21 +1100852 case oUsePrivilegedPort:
853 intptr = &options->use_privileged_port;
854 goto parse_flag;
855
Damien Miller95def091999-11-25 00:26:21 +1100856 case oPasswordAuthentication:
857 intptr = &options->password_authentication;
858 goto parse_flag;
859
Damien Miller874d77b2000-10-14 16:23:11 +1100860 case oKbdInteractiveAuthentication:
861 intptr = &options->kbd_interactive_authentication;
862 goto parse_flag;
863
864 case oKbdInteractiveDevices:
865 charptr = &options->kbd_interactive_devices;
866 goto parse_string;
867
Damien Miller0bc1bd82000-11-13 22:57:25 +1100868 case oPubkeyAuthentication:
869 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000870 goto parse_flag;
871
Damien Miller95def091999-11-25 00:26:21 +1100872 case oRSAAuthentication:
873 intptr = &options->rsa_authentication;
874 goto parse_flag;
875
876 case oRhostsRSAAuthentication:
877 intptr = &options->rhosts_rsa_authentication;
878 goto parse_flag;
879
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000880 case oHostbasedAuthentication:
881 intptr = &options->hostbased_authentication;
882 goto parse_flag;
883
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000884 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000885 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100886 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000887
Darren Tucker0efd1552003-08-26 11:49:55 +1000888 case oGssAuthentication:
889 intptr = &options->gss_authentication;
890 goto parse_flag;
891
892 case oGssDelegateCreds:
893 intptr = &options->gss_deleg_creds;
894 goto parse_flag;
895
Damien Miller95def091999-11-25 00:26:21 +1100896 case oBatchMode:
897 intptr = &options->batch_mode;
898 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899
Damien Miller95def091999-11-25 00:26:21 +1100900 case oCheckHostIP:
901 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000902 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903
Damien Miller37876e92003-05-15 10:19:46 +1000904 case oVerifyHostKeyDNS:
905 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100906 multistate_ptr = multistate_yesnoask;
907 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000908
Damien Miller95def091999-11-25 00:26:21 +1100909 case oStrictHostKeyChecking:
910 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100911 multistate_ptr = multistate_yesnoask;
912 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913
Damien Miller95def091999-11-25 00:26:21 +1100914 case oCompression:
915 intptr = &options->compression;
916 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917
Damien Miller12c150e2003-12-17 16:31:10 +1100918 case oTCPKeepAlive:
919 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100920 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000922 case oNoHostAuthenticationForLocalhost:
923 intptr = &options->no_host_authentication_for_localhost;
924 goto parse_flag;
925
Damien Miller95def091999-11-25 00:26:21 +1100926 case oNumberOfPasswordPrompts:
927 intptr = &options->number_of_password_prompts;
928 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929
Damien Miller95def091999-11-25 00:26:21 +1100930 case oCompressionLevel:
931 intptr = &options->compression_level;
932 goto parse_int;
933
Damien Millera5539d22003-04-09 20:50:06 +1000934 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000935 arg = strdelim(&s);
936 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000937 fatal("%.200s line %d: Missing argument.", filename,
938 linenum);
939 if (strcmp(arg, "default") == 0) {
940 val64 = 0;
941 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000942 if (scan_scaled(arg, &val64) == -1)
943 fatal("%.200s line %d: Bad number '%s': %s",
944 filename, linenum, arg, strerror(errno));
945 /* check for too-large or too-small limits */
946 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000947 fatal("%.200s line %d: RekeyLimit too large",
948 filename, linenum);
949 if (val64 != 0 && val64 < 16)
950 fatal("%.200s line %d: RekeyLimit too small",
951 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000952 }
Damien Miller3dff1762008-02-10 22:25:52 +1100953 if (*activep && options->rekey_limit == -1)
954 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000955 if (s != NULL) { /* optional rekey interval present */
956 if (strcmp(s, "none") == 0) {
957 (void)strdelim(&s); /* discard */
958 break;
959 }
960 intptr = &options->rekey_interval;
961 goto parse_time;
962 }
Damien Millera5539d22003-04-09 20:50:06 +1000963 break;
964
Damien Miller95def091999-11-25 00:26:21 +1100965 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000966 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000967 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100968 fatal("%.200s line %d: Missing argument.", filename, linenum);
969 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100970 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000971 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100972 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100973 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000974 add_identity_file(options, NULL,
975 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +1100976 }
977 break;
978
Damien Millerd3a18572000-06-07 19:55:44 +1000979 case oXAuthLocation:
980 charptr=&options->xauth_location;
981 goto parse_string;
982
Damien Miller95def091999-11-25 00:26:21 +1100983 case oUser:
984 charptr = &options->user;
985parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000986 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000987 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000988 fatal("%.200s line %d: Missing argument.",
989 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100990 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000991 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100992 break;
993
994 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000995 cpptr = (char **)&options->system_hostfiles;
996 uintptr = &options->num_system_hostfiles;
997 max_entries = SSH_MAX_HOSTS_FILES;
998parse_char_array:
999 if (*activep && *uintptr == 0) {
1000 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1001 if ((*uintptr) >= max_entries)
1002 fatal("%s line %d: "
1003 "too many authorized keys files.",
1004 filename, linenum);
1005 cpptr[(*uintptr)++] = xstrdup(arg);
1006 }
1007 }
1008 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001009
1010 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001011 cpptr = (char **)&options->user_hostfiles;
1012 uintptr = &options->num_user_hostfiles;
1013 max_entries = SSH_MAX_HOSTS_FILES;
1014 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001015
Damien Miller95def091999-11-25 00:26:21 +11001016 case oHostName:
1017 charptr = &options->hostname;
1018 goto parse_string;
1019
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001020 case oHostKeyAlias:
1021 charptr = &options->host_key_alias;
1022 goto parse_string;
1023
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001024 case oPreferredAuthentications:
1025 charptr = &options->preferred_authentications;
1026 goto parse_string;
1027
Ben Lindstrome0f88042001-04-30 13:06:24 +00001028 case oBindAddress:
1029 charptr = &options->bind_address;
1030 goto parse_string;
1031
Damien Miller7ea845e2010-02-12 09:21:02 +11001032 case oPKCS11Provider:
1033 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001034 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001035
Damien Miller95def091999-11-25 00:26:21 +11001036 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001037 charptr = &options->proxy_command;
1038parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001039 if (s == NULL)
1040 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001041 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001042 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001043 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001044 return 0;
1045
1046 case oPort:
1047 intptr = &options->port;
1048parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001049 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001050 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001051 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001052 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001053 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001054
1055 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001056 value = strtol(arg, &endofnumber, 0);
1057 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001058 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001059 if (*activep && *intptr == -1)
1060 *intptr = value;
1061 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001062
Damien Miller95def091999-11-25 00:26:21 +11001063 case oConnectionAttempts:
1064 intptr = &options->connection_attempts;
1065 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001066
Damien Miller95def091999-11-25 00:26:21 +11001067 case oCipher:
1068 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001069 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001070 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001071 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001072 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001073 if (value == -1)
1074 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001075 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001076 if (*activep && *intptr == -1)
1077 *intptr = value;
1078 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079
Damien Miller78928792000-04-12 20:17:38 +10001080 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001081 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001082 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001083 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001084 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001085 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001086 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001087 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001088 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001089 break;
1090
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001091 case oMacs:
1092 arg = strdelim(&s);
1093 if (!arg || *arg == '\0')
1094 fatal("%.200s line %d: Missing argument.", filename, linenum);
1095 if (!mac_valid(arg))
1096 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001097 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001098 if (*activep && options->macs == NULL)
1099 options->macs = xstrdup(arg);
1100 break;
1101
Damien Millerd5f62bf2010-09-24 22:11:14 +10001102 case oKexAlgorithms:
1103 arg = strdelim(&s);
1104 if (!arg || *arg == '\0')
1105 fatal("%.200s line %d: Missing argument.",
1106 filename, linenum);
1107 if (!kex_names_valid(arg))
1108 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1109 filename, linenum, arg ? arg : "<NONE>");
1110 if (*activep && options->kex_algorithms == NULL)
1111 options->kex_algorithms = xstrdup(arg);
1112 break;
1113
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001114 case oHostKeyAlgorithms:
1115 arg = strdelim(&s);
1116 if (!arg || *arg == '\0')
1117 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001118 if (!sshkey_names_valid2(arg, 1))
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001119 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001120 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001121 if (*activep && options->hostkeyalgorithms == NULL)
1122 options->hostkeyalgorithms = xstrdup(arg);
1123 break;
1124
Damien Miller78928792000-04-12 20:17:38 +10001125 case oProtocol:
1126 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001127 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001128 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001129 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001130 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001131 if (value == SSH_PROTO_UNKNOWN)
1132 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001133 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001134 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1135 *intptr = value;
1136 break;
1137
Damien Miller95def091999-11-25 00:26:21 +11001138 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001139 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001140 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001141 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001142 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001143 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001144 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001145 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1146 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001147 break;
1148
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001149 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001150 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001151 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001152 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001153 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001154 fatal("%.200s line %d: Missing port argument.",
1155 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001156
Damien Millera699d952008-11-03 19:27:34 +11001157 if (opcode == oLocalForward ||
1158 opcode == oRemoteForward) {
1159 arg2 = strdelim(&s);
1160 if (arg2 == NULL || *arg2 == '\0')
1161 fatal("%.200s line %d: Missing target argument.",
1162 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001163
Damien Millera699d952008-11-03 19:27:34 +11001164 /* construct a string for parse_forward */
1165 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1166 } else if (opcode == oDynamicForward) {
1167 strlcpy(fwdarg, arg, sizeof(fwdarg));
1168 }
1169
1170 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001171 opcode == oDynamicForward ? 1 : 0,
1172 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001173 fatal("%.200s line %d: Bad forwarding specification.",
1174 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001175
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001176 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001177 if (opcode == oLocalForward ||
1178 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001179 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001180 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001181 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001182 }
Damien Miller95def091999-11-25 00:26:21 +11001183 break;
1184
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001185 case oClearAllForwardings:
1186 intptr = &options->clear_forwardings;
1187 goto parse_flag;
1188
Damien Miller95def091999-11-25 00:26:21 +11001189 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001190 if (cmdline)
1191 fatal("Host directive not supported as a command-line "
1192 "option");
Damien Miller95def091999-11-25 00:26:21 +11001193 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001194 arg2 = NULL;
1195 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1196 negated = *arg == '!';
1197 if (negated)
1198 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001199 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001200 if (negated) {
1201 debug("%.200s line %d: Skipping Host "
1202 "block because of negated match "
1203 "for %.100s", filename, linenum,
1204 arg);
1205 *activep = 0;
1206 break;
1207 }
1208 if (!*activep)
1209 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001210 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001211 }
Damien Millerfe924212011-05-15 08:44:45 +10001212 }
1213 if (*activep)
1214 debug("%.200s line %d: Applying options for %.100s",
1215 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001216 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001217 return 0;
1218
Damien Miller194fd902013-10-15 12:13:05 +11001219 case oMatch:
1220 if (cmdline)
1221 fatal("Host directive not supported as a command-line "
1222 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001223 value = match_cfg_line(options, &s, pw, host, original_host,
1224 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001225 if (value < 0)
1226 fatal("%.200s line %d: Bad Match condition", filename,
1227 linenum);
1228 *activep = value;
1229 break;
1230
Damien Miller95def091999-11-25 00:26:21 +11001231 case oEscapeChar:
1232 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001233 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001234 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001235 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001236 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001237 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1238 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001239 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001240 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001241 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001242 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001243 else {
1244 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001245 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001246 /* NOTREACHED */
1247 value = 0; /* Avoid compiler warning. */
1248 }
1249 if (*activep && *intptr == -1)
1250 *intptr = value;
1251 break;
1252
Damien Miller20a8f972003-05-18 20:50:30 +10001253 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001254 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001255 multistate_ptr = multistate_addressfamily;
1256 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001257
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001258 case oEnableSSHKeysign:
1259 intptr = &options->enable_ssh_keysign;
1260 goto parse_flag;
1261
Damien Millerbd394c32004-03-08 23:12:36 +11001262 case oIdentitiesOnly:
1263 intptr = &options->identities_only;
1264 goto parse_flag;
1265
Damien Miller509b0102003-12-17 16:33:10 +11001266 case oServerAliveInterval:
1267 intptr = &options->server_alive_interval;
1268 goto parse_time;
1269
1270 case oServerAliveCountMax:
1271 intptr = &options->server_alive_count_max;
1272 goto parse_int;
1273
Darren Tucker46bc0752004-05-02 22:11:30 +10001274 case oSendEnv:
1275 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1276 if (strchr(arg, '=') != NULL)
1277 fatal("%s line %d: Invalid environment name.",
1278 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001279 if (!*activep)
1280 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001281 if (options->num_send_env >= MAX_SEND_ENV)
1282 fatal("%s line %d: too many send env.",
1283 filename, linenum);
1284 options->send_env[options->num_send_env++] =
1285 xstrdup(arg);
1286 }
1287 break;
1288
Damien Miller0e220db2004-06-15 10:34:08 +10001289 case oControlPath:
1290 charptr = &options->control_path;
1291 goto parse_string;
1292
1293 case oControlMaster:
1294 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001295 multistate_ptr = multistate_controlmaster;
1296 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001297
Damien Millere11e1ea2010-08-03 16:04:46 +10001298 case oControlPersist:
1299 /* no/false/yes/true, or a time spec */
1300 intptr = &options->control_persist;
1301 arg = strdelim(&s);
1302 if (!arg || *arg == '\0')
1303 fatal("%.200s line %d: Missing ControlPersist"
1304 " argument.", filename, linenum);
1305 value = 0;
1306 value2 = 0; /* timeout */
1307 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1308 value = 0;
1309 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1310 value = 1;
1311 else if ((value2 = convtime(arg)) >= 0)
1312 value = 1;
1313 else
1314 fatal("%.200s line %d: Bad ControlPersist argument.",
1315 filename, linenum);
1316 if (*activep && *intptr == -1) {
1317 *intptr = value;
1318 options->control_persist_timeout = value2;
1319 }
1320 break;
1321
Damien Millere1776152005-03-01 21:47:37 +11001322 case oHashKnownHosts:
1323 intptr = &options->hash_known_hosts;
1324 goto parse_flag;
1325
Damien Millerd27b9472005-12-13 19:29:02 +11001326 case oTunnel:
1327 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001328 multistate_ptr = multistate_tunnel;
1329 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001330
1331 case oTunnelDevice:
1332 arg = strdelim(&s);
1333 if (!arg || *arg == '\0')
1334 fatal("%.200s line %d: Missing argument.", filename, linenum);
1335 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001336 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001337 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1338 if (*activep) {
1339 options->tun_local = value;
1340 options->tun_remote = value2;
1341 }
1342 break;
1343
1344 case oLocalCommand:
1345 charptr = &options->local_command;
1346 goto parse_command;
1347
1348 case oPermitLocalCommand:
1349 intptr = &options->permit_local_command;
1350 goto parse_flag;
1351
Damien Miller10288242008-06-30 00:04:03 +10001352 case oVisualHostKey:
1353 intptr = &options->visual_host_key;
1354 goto parse_flag;
1355
Damien Miller0dac6fb2010-11-20 15:19:38 +11001356 case oIPQoS:
1357 arg = strdelim(&s);
1358 if ((value = parse_ipqos(arg)) == -1)
1359 fatal("%s line %d: Bad IPQoS value: %s",
1360 filename, linenum, arg);
1361 arg = strdelim(&s);
1362 if (arg == NULL)
1363 value2 = value;
1364 else if ((value2 = parse_ipqos(arg)) == -1)
1365 fatal("%s line %d: Bad IPQoS value: %s",
1366 filename, linenum, arg);
1367 if (*activep) {
1368 options->ip_qos_interactive = value;
1369 options->ip_qos_bulk = value2;
1370 }
1371 break;
1372
Darren Tucker71e4d542009-07-06 07:12:27 +10001373 case oUseRoaming:
1374 intptr = &options->use_roaming;
1375 goto parse_flag;
1376
Damien Miller21771e22011-05-15 08:45:50 +10001377 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001378 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001379 multistate_ptr = multistate_requesttty;
1380 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001381
Darren Tucker07636982013-05-16 20:30:03 +10001382 case oIgnoreUnknown:
1383 charptr = &options->ignored_unknown;
1384 goto parse_string;
1385
Damien Miller1262b662013-08-21 02:44:24 +10001386 case oProxyUseFdpass:
1387 intptr = &options->proxy_use_fdpass;
1388 goto parse_flag;
1389
Damien Miller0faf7472013-10-17 11:47:23 +11001390 case oCanonicalDomains:
1391 value = options->num_canonical_domains != 0;
1392 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1393 valid_domain(arg, filename, linenum);
1394 if (!*activep || value)
1395 continue;
1396 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1397 fatal("%s line %d: too many hostname suffixes.",
1398 filename, linenum);
1399 options->canonical_domains[
1400 options->num_canonical_domains++] = xstrdup(arg);
1401 }
1402 break;
1403
Damien Miller38505592013-10-17 11:48:13 +11001404 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001405 value = options->num_permitted_cnames != 0;
1406 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1407 /* Either '*' for everything or 'list:list' */
1408 if (strcmp(arg, "*") == 0)
1409 arg2 = arg;
1410 else {
1411 lowercase(arg);
1412 if ((arg2 = strchr(arg, ':')) == NULL ||
1413 arg2[1] == '\0') {
1414 fatal("%s line %d: "
1415 "Invalid permitted CNAME \"%s\"",
1416 filename, linenum, arg);
1417 }
1418 *arg2 = '\0';
1419 arg2++;
1420 }
1421 if (!*activep || value)
1422 continue;
1423 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1424 fatal("%s line %d: too many permitted CNAMEs.",
1425 filename, linenum);
1426 cname = options->permitted_cnames +
1427 options->num_permitted_cnames++;
1428 cname->source_list = xstrdup(arg);
1429 cname->target_list = xstrdup(arg2);
1430 }
1431 break;
1432
Damien Miller38505592013-10-17 11:48:13 +11001433 case oCanonicalizeHostname:
1434 intptr = &options->canonicalize_hostname;
1435 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001436 goto parse_multistate;
1437
Damien Miller38505592013-10-17 11:48:13 +11001438 case oCanonicalizeMaxDots:
1439 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001440 goto parse_int;
1441
Damien Miller38505592013-10-17 11:48:13 +11001442 case oCanonicalizeFallbackLocal:
1443 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001444 goto parse_flag;
1445
Damien Miller7acefbb2014-07-18 14:11:24 +10001446 case oStreamLocalBindMask:
1447 arg = strdelim(&s);
1448 if (!arg || *arg == '\0')
1449 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1450 /* Parse mode in octal format */
1451 value = strtol(arg, &endofnumber, 8);
1452 if (arg == endofnumber || value < 0 || value > 0777)
1453 fatal("%.200s line %d: Bad mask.", filename, linenum);
1454 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1455 break;
1456
1457 case oStreamLocalBindUnlink:
1458 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1459 goto parse_flag;
1460
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001461 case oRevokedHostKeys:
1462 charptr = &options->revoked_host_keys;
1463 goto parse_string;
1464
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001465 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001466 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001467 arg = strdelim(&s);
1468 if (!arg || *arg == '\0')
1469 fatal("%.200s line %d: Missing argument.",
1470 filename, linenum);
1471 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1472 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1473 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001474 if (*activep && *intptr == -1)
1475 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001476 break;
1477
Ben Lindstrom4daea862002-06-09 20:04:02 +00001478 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001479 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001480 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001481 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001482
Damien Millerf9b3feb2003-05-16 11:38:32 +10001483 case oUnsupported:
1484 error("%s line %d: Unsupported option \"%s\"",
1485 filename, linenum, keyword);
1486 return 0;
1487
Damien Miller95def091999-11-25 00:26:21 +11001488 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001489 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001490 }
1491
1492 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001493 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001494 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001495 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001496 }
Damien Miller95def091999-11-25 00:26:21 +11001497 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498}
1499
1500
Damien Miller5428f641999-11-25 11:54:57 +11001501/*
1502 * Reads the config file and modifies the options accordingly. Options
1503 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001504 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001505 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001507int
Damien Miller194fd902013-10-15 12:13:05 +11001508read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001509 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510{
Damien Miller95def091999-11-25 00:26:21 +11001511 FILE *f;
1512 char line[1024];
1513 int active, linenum;
1514 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515
Damien Miller57a44762004-04-20 20:11:57 +10001516 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001517 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518
Darren Tuckeraefa3682013-04-05 11:18:35 +11001519 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001520 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001521
Damien Miller33793852004-06-15 10:27:55 +10001522 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001523 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001524 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001525 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001526 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001527 }
1528
Damien Miller95def091999-11-25 00:26:21 +11001529 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530
Damien Miller5428f641999-11-25 11:54:57 +11001531 /*
1532 * Mark that we are now processing the options. This flag is turned
1533 * on/off by Host specifications.
1534 */
Damien Miller95def091999-11-25 00:26:21 +11001535 active = 1;
1536 linenum = 0;
1537 while (fgets(line, sizeof(line), f)) {
1538 /* Update line number counter. */
1539 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001540 if (process_config_line(options, pw, host, original_host,
1541 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001542 bad_options++;
1543 }
1544 fclose(f);
1545 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001546 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001547 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001548 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549}
1550
Damien Miller13f97b22014-02-24 15:57:55 +11001551/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1552int
1553option_clear_or_none(const char *o)
1554{
1555 return o == NULL || strcasecmp(o, "none") == 0;
1556}
1557
Damien Miller5428f641999-11-25 11:54:57 +11001558/*
1559 * Initializes options to special values that indicate that they have not yet
1560 * been set. Read_config_file will only set options with this value. Options
1561 * are processed in the following order: command line, user config file,
1562 * system config file. Last, fill_default_options is called.
1563 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564
Damien Miller4af51302000-04-16 11:18:38 +10001565void
Damien Miller95def091999-11-25 00:26:21 +11001566initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567{
Damien Miller95def091999-11-25 00:26:21 +11001568 memset(options, 'X', sizeof(*options));
1569 options->forward_agent = -1;
1570 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001571 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001572 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001573 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001574 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001575 options->fwd_opts.gateway_ports = -1;
1576 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1577 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001578 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001579 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001580 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001581 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001582 options->gss_authentication = -1;
1583 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001584 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001585 options->kbd_interactive_authentication = -1;
1586 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001587 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001588 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001589 options->batch_mode = -1;
1590 options->check_host_ip = -1;
1591 options->strict_host_key_checking = -1;
1592 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001593 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001594 options->compression_level = -1;
1595 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001596 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001597 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001598 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001599 options->number_of_password_prompts = -1;
1600 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001601 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001602 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001603 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001604 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001605 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001606 options->num_identity_files = 0;
1607 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001608 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001609 options->proxy_command = NULL;
1610 options->user = NULL;
1611 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001612 options->num_system_hostfiles = 0;
1613 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001614 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001615 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001616 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001617 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001618 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001619 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001620 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001621 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001622 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001623 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001624 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001625 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001626 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001627 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001628 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001629 options->server_alive_interval = -1;
1630 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001631 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001632 options->control_path = NULL;
1633 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001634 options->control_persist = -1;
1635 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001636 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001637 options->tun_open = -1;
1638 options->tun_local = -1;
1639 options->tun_remote = -1;
1640 options->local_command = NULL;
1641 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001642 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001643 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001644 options->ip_qos_interactive = -1;
1645 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001646 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001647 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001648 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001649 options->num_canonical_domains = 0;
1650 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001651 options->canonicalize_max_dots = -1;
1652 options->canonicalize_fallback_local = -1;
1653 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001654 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001655 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001656}
1657
Damien Miller5428f641999-11-25 11:54:57 +11001658/*
Damien Miller13f97b22014-02-24 15:57:55 +11001659 * A petite version of fill_default_options() that just fills the options
1660 * needed for hostname canonicalization to proceed.
1661 */
1662void
1663fill_default_options_for_canonicalization(Options *options)
1664{
1665 if (options->canonicalize_max_dots == -1)
1666 options->canonicalize_max_dots = 1;
1667 if (options->canonicalize_fallback_local == -1)
1668 options->canonicalize_fallback_local = 1;
1669 if (options->canonicalize_hostname == -1)
1670 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1671}
1672
1673/*
Damien Miller5428f641999-11-25 11:54:57 +11001674 * Called after processing other sources of option data, this fills those
1675 * options for which no value has been specified with their default values.
1676 */
Damien Miller4af51302000-04-16 11:18:38 +10001677void
Damien Miller95def091999-11-25 00:26:21 +11001678fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001679{
Damien Miller95def091999-11-25 00:26:21 +11001680 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001681 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001682 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001683 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001684 if (options->forward_x11_trusted == -1)
1685 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001686 if (options->forward_x11_timeout == -1)
1687 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001688 if (options->exit_on_forward_failure == -1)
1689 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001690 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001691 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001692 if (options->fwd_opts.gateway_ports == -1)
1693 options->fwd_opts.gateway_ports = 0;
1694 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1695 options->fwd_opts.streamlocal_bind_mask = 0177;
1696 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1697 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001698 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001699 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001700 if (options->rsa_authentication == -1)
1701 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001702 if (options->pubkey_authentication == -1)
1703 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001704 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001705 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001706 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001707 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001708 if (options->gss_deleg_creds == -1)
1709 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001710 if (options->password_authentication == -1)
1711 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001712 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001713 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001714 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001715 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001716 if (options->hostbased_authentication == -1)
1717 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001718 if (options->batch_mode == -1)
1719 options->batch_mode = 0;
1720 if (options->check_host_ip == -1)
1721 options->check_host_ip = 1;
1722 if (options->strict_host_key_checking == -1)
1723 options->strict_host_key_checking = 2; /* 2 is default */
1724 if (options->compression == -1)
1725 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001726 if (options->tcp_keep_alive == -1)
1727 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001728 if (options->compression_level == -1)
1729 options->compression_level = 6;
1730 if (options->port == -1)
1731 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001732 if (options->address_family == -1)
1733 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001734 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001735 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001736 if (options->number_of_password_prompts == -1)
1737 options->number_of_password_prompts = 3;
1738 /* Selected in ssh_login(). */
1739 if (options->cipher == -1)
1740 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001741 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001742 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001743 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001744 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001745 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001746 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001747 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001748 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001749 add_identity_file(options, "~/",
1750 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001751 }
1752 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001753 add_identity_file(options, "~/",
1754 _PATH_SSH_CLIENT_ID_RSA, 0);
1755 add_identity_file(options, "~/",
1756 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001757#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001758 add_identity_file(options, "~/",
1759 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001760#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001761 add_identity_file(options, "~/",
1762 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001763 }
Damien Millereba71ba2000-04-29 23:57:08 +10001764 }
Damien Miller95def091999-11-25 00:26:21 +11001765 if (options->escape_char == -1)
1766 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001767 if (options->num_system_hostfiles == 0) {
1768 options->system_hostfiles[options->num_system_hostfiles++] =
1769 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1770 options->system_hostfiles[options->num_system_hostfiles++] =
1771 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1772 }
1773 if (options->num_user_hostfiles == 0) {
1774 options->user_hostfiles[options->num_user_hostfiles++] =
1775 xstrdup(_PATH_SSH_USER_HOSTFILE);
1776 options->user_hostfiles[options->num_user_hostfiles++] =
1777 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1778 }
Damien Millerfcd93202002-02-05 12:26:34 +11001779 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001780 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001781 if (options->clear_forwardings == 1)
1782 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001783 if (options->no_host_authentication_for_localhost == - 1)
1784 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001785 if (options->identities_only == -1)
1786 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001787 if (options->enable_ssh_keysign == -1)
1788 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001789 if (options->rekey_limit == -1)
1790 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001791 if (options->rekey_interval == -1)
1792 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001793 if (options->verify_host_key_dns == -1)
1794 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001795 if (options->server_alive_interval == -1)
1796 options->server_alive_interval = 0;
1797 if (options->server_alive_count_max == -1)
1798 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001799 if (options->control_master == -1)
1800 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001801 if (options->control_persist == -1) {
1802 options->control_persist = 0;
1803 options->control_persist_timeout = 0;
1804 }
Damien Millere1776152005-03-01 21:47:37 +11001805 if (options->hash_known_hosts == -1)
1806 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001807 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001808 options->tun_open = SSH_TUNMODE_NO;
1809 if (options->tun_local == -1)
1810 options->tun_local = SSH_TUNID_ANY;
1811 if (options->tun_remote == -1)
1812 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001813 if (options->permit_local_command == -1)
1814 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001815 if (options->use_roaming == -1)
1816 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001817 if (options->visual_host_key == -1)
1818 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001819 if (options->ip_qos_interactive == -1)
1820 options->ip_qos_interactive = IPTOS_LOWDELAY;
1821 if (options->ip_qos_bulk == -1)
1822 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001823 if (options->request_tty == -1)
1824 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001825 if (options->proxy_use_fdpass == -1)
1826 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001827 if (options->canonicalize_max_dots == -1)
1828 options->canonicalize_max_dots = 1;
1829 if (options->canonicalize_fallback_local == -1)
1830 options->canonicalize_fallback_local = 1;
1831 if (options->canonicalize_hostname == -1)
1832 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001833 if (options->fingerprint_hash == -1)
1834 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1835
Damien Millere9fc72e2013-10-15 12:14:12 +11001836#define CLEAR_ON_NONE(v) \
1837 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001838 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001839 free(v); \
1840 v = NULL; \
1841 } \
1842 } while(0)
1843 CLEAR_ON_NONE(options->local_command);
1844 CLEAR_ON_NONE(options->proxy_command);
1845 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001846 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001847 /* options->user will be set in the main program if appropriate */
1848 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001849 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001850 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001851}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001852
Damien Miller7acefbb2014-07-18 14:11:24 +10001853struct fwdarg {
1854 char *arg;
1855 int ispath;
1856};
1857
1858/*
1859 * parse_fwd_field
1860 * parses the next field in a port forwarding specification.
1861 * sets fwd to the parsed field and advances p past the colon
1862 * or sets it to NULL at end of string.
1863 * returns 0 on success, else non-zero.
1864 */
1865static int
1866parse_fwd_field(char **p, struct fwdarg *fwd)
1867{
1868 char *ep, *cp = *p;
1869 int ispath = 0;
1870
1871 if (*cp == '\0') {
1872 *p = NULL;
1873 return -1; /* end of string */
1874 }
1875
1876 /*
1877 * A field escaped with square brackets is used literally.
1878 * XXX - allow ']' to be escaped via backslash?
1879 */
1880 if (*cp == '[') {
1881 /* find matching ']' */
1882 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1883 if (*ep == '/')
1884 ispath = 1;
1885 }
1886 /* no matching ']' or not at end of field. */
1887 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1888 return -1;
1889 /* NUL terminate the field and advance p past the colon */
1890 *ep++ = '\0';
1891 if (*ep != '\0')
1892 *ep++ = '\0';
1893 fwd->arg = cp + 1;
1894 fwd->ispath = ispath;
1895 *p = ep;
1896 return 0;
1897 }
1898
1899 for (cp = *p; *cp != '\0'; cp++) {
1900 switch (*cp) {
1901 case '\\':
1902 memmove(cp, cp + 1, strlen(cp + 1) + 1);
1903 cp++;
1904 break;
1905 case '/':
1906 ispath = 1;
1907 break;
1908 case ':':
1909 *cp++ = '\0';
1910 goto done;
1911 }
1912 }
1913done:
1914 fwd->arg = *p;
1915 fwd->ispath = ispath;
1916 *p = cp;
1917 return 0;
1918}
1919
Damien Millerf91ee4c2005-03-01 21:24:33 +11001920/*
1921 * parse_forward
1922 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001923 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10001924 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
1925 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11001926 * dynamicfwd == 1
1927 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001928 * returns number of arguments parsed or zero on error
1929 */
1930int
Damien Miller7acefbb2014-07-18 14:11:24 +10001931parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001932{
Damien Miller7acefbb2014-07-18 14:11:24 +10001933 struct fwdarg fwdargs[4];
1934 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001935 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001936
Damien Miller7acefbb2014-07-18 14:11:24 +10001937 memset(fwd, 0, sizeof(*fwd));
1938 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11001939
1940 cp = p = xstrdup(fwdspec);
1941
1942 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001943 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001944 cp++;
1945
Damien Miller7acefbb2014-07-18 14:11:24 +10001946 for (i = 0; i < 4; ++i) {
1947 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001948 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10001949 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001950
Damien Millerf4b39532008-11-03 19:28:21 +11001951 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10001952 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001953 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10001954 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001955
1956 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001957 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10001958 if (fwdargs[0].ispath) {
1959 fwd->listen_path = xstrdup(fwdargs[0].arg);
1960 fwd->listen_port = PORT_STREAMLOCAL;
1961 } else {
1962 fwd->listen_host = NULL;
1963 fwd->listen_port = a2port(fwdargs[0].arg);
1964 }
Damien Millera699d952008-11-03 19:27:34 +11001965 fwd->connect_host = xstrdup("socks");
1966 break;
1967
1968 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10001969 if (fwdargs[0].ispath && fwdargs[1].ispath) {
1970 fwd->listen_path = xstrdup(fwdargs[0].arg);
1971 fwd->listen_port = PORT_STREAMLOCAL;
1972 fwd->connect_path = xstrdup(fwdargs[1].arg);
1973 fwd->connect_port = PORT_STREAMLOCAL;
1974 } else if (fwdargs[1].ispath) {
1975 fwd->listen_host = NULL;
1976 fwd->listen_port = a2port(fwdargs[0].arg);
1977 fwd->connect_path = xstrdup(fwdargs[1].arg);
1978 fwd->connect_port = PORT_STREAMLOCAL;
1979 } else {
1980 fwd->listen_host = xstrdup(fwdargs[0].arg);
1981 fwd->listen_port = a2port(fwdargs[1].arg);
1982 fwd->connect_host = xstrdup("socks");
1983 }
Damien Millera699d952008-11-03 19:27:34 +11001984 break;
1985
Damien Millerf91ee4c2005-03-01 21:24:33 +11001986 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10001987 if (fwdargs[0].ispath) {
1988 fwd->listen_path = xstrdup(fwdargs[0].arg);
1989 fwd->listen_port = PORT_STREAMLOCAL;
1990 fwd->connect_host = xstrdup(fwdargs[1].arg);
1991 fwd->connect_port = a2port(fwdargs[2].arg);
1992 } else if (fwdargs[2].ispath) {
1993 fwd->listen_host = xstrdup(fwdargs[0].arg);
1994 fwd->listen_port = a2port(fwdargs[1].arg);
1995 fwd->connect_path = xstrdup(fwdargs[2].arg);
1996 fwd->connect_port = PORT_STREAMLOCAL;
1997 } else {
1998 fwd->listen_host = NULL;
1999 fwd->listen_port = a2port(fwdargs[0].arg);
2000 fwd->connect_host = xstrdup(fwdargs[1].arg);
2001 fwd->connect_port = a2port(fwdargs[2].arg);
2002 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002003 break;
2004
2005 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002006 fwd->listen_host = xstrdup(fwdargs[0].arg);
2007 fwd->listen_port = a2port(fwdargs[1].arg);
2008 fwd->connect_host = xstrdup(fwdargs[2].arg);
2009 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002010 break;
2011 default:
2012 i = 0; /* failure */
2013 }
2014
Darren Tuckera627d422013-06-02 07:31:17 +10002015 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002016
Damien Millera699d952008-11-03 19:27:34 +11002017 if (dynamicfwd) {
2018 if (!(i == 1 || i == 2))
2019 goto fail_free;
2020 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002021 if (!(i == 3 || i == 4)) {
2022 if (fwd->connect_path == NULL &&
2023 fwd->listen_path == NULL)
2024 goto fail_free;
2025 }
2026 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002027 goto fail_free;
2028 }
2029
Damien Miller7acefbb2014-07-18 14:11:24 +10002030 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2031 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002032 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002033 if (fwd->connect_host != NULL &&
2034 strlen(fwd->connect_host) >= NI_MAXHOST)
2035 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002036 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2037 if (fwd->connect_path != NULL &&
2038 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2039 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002040 if (fwd->listen_host != NULL &&
2041 strlen(fwd->listen_host) >= NI_MAXHOST)
2042 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002043 if (fwd->listen_path != NULL &&
2044 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2045 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002046
2047 return (i);
2048
2049 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002050 free(fwd->connect_host);
2051 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002052 free(fwd->connect_path);
2053 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002054 free(fwd->listen_host);
2055 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002056 free(fwd->listen_path);
2057 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002058 return (0);
2059}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002060
2061/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2062static const char *
2063fmt_multistate_int(int val, const struct multistate *m)
2064{
2065 u_int i;
2066
2067 for (i = 0; m[i].key != NULL; i++) {
2068 if (m[i].value == val)
2069 return m[i].key;
2070 }
2071 return "UNKNOWN";
2072}
2073
2074static const char *
2075fmt_intarg(OpCodes code, int val)
2076{
2077 if (val == -1)
2078 return "unset";
2079 switch (code) {
2080 case oAddressFamily:
2081 return fmt_multistate_int(val, multistate_addressfamily);
2082 case oVerifyHostKeyDNS:
2083 case oStrictHostKeyChecking:
2084 return fmt_multistate_int(val, multistate_yesnoask);
2085 case oControlMaster:
2086 return fmt_multistate_int(val, multistate_controlmaster);
2087 case oTunnel:
2088 return fmt_multistate_int(val, multistate_tunnel);
2089 case oRequestTTY:
2090 return fmt_multistate_int(val, multistate_requesttty);
2091 case oCanonicalizeHostname:
2092 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002093 case oFingerprintHash:
2094 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002095 case oProtocol:
2096 switch (val) {
2097 case SSH_PROTO_1:
2098 return "1";
2099 case SSH_PROTO_2:
2100 return "2";
2101 case (SSH_PROTO_1|SSH_PROTO_2):
2102 return "2,1";
2103 default:
2104 return "UNKNOWN";
2105 }
2106 default:
2107 switch (val) {
2108 case 0:
2109 return "no";
2110 case 1:
2111 return "yes";
2112 default:
2113 return "UNKNOWN";
2114 }
2115 }
2116}
2117
2118static const char *
2119lookup_opcode_name(OpCodes code)
2120{
2121 u_int i;
2122
2123 for (i = 0; keywords[i].name != NULL; i++)
2124 if (keywords[i].opcode == code)
2125 return(keywords[i].name);
2126 return "UNKNOWN";
2127}
2128
2129static void
2130dump_cfg_int(OpCodes code, int val)
2131{
2132 printf("%s %d\n", lookup_opcode_name(code), val);
2133}
2134
2135static void
2136dump_cfg_fmtint(OpCodes code, int val)
2137{
2138 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2139}
2140
2141static void
2142dump_cfg_string(OpCodes code, const char *val)
2143{
2144 if (val == NULL)
2145 return;
2146 printf("%s %s\n", lookup_opcode_name(code), val);
2147}
2148
2149static void
2150dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2151{
2152 u_int i;
2153
2154 for (i = 0; i < count; i++)
2155 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2156}
2157
2158static void
2159dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2160{
2161 u_int i;
2162
2163 printf("%s", lookup_opcode_name(code));
2164 for (i = 0; i < count; i++)
2165 printf(" %s", vals[i]);
2166 printf("\n");
2167}
2168
2169static void
2170dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2171{
2172 const struct Forward *fwd;
2173 u_int i;
2174
2175 /* oDynamicForward */
2176 for (i = 0; i < count; i++) {
2177 fwd = &fwds[i];
2178 if (code == oDynamicForward &&
2179 strcmp(fwd->connect_host, "socks") != 0)
2180 continue;
2181 if (code == oLocalForward &&
2182 strcmp(fwd->connect_host, "socks") == 0)
2183 continue;
2184 printf("%s", lookup_opcode_name(code));
2185 if (fwd->listen_port == PORT_STREAMLOCAL)
2186 printf(" %s", fwd->listen_path);
2187 else if (fwd->listen_host == NULL)
2188 printf(" %d", fwd->listen_port);
2189 else {
2190 printf(" [%s]:%d",
2191 fwd->listen_host, fwd->listen_port);
2192 }
2193 if (code != oDynamicForward) {
2194 if (fwd->connect_port == PORT_STREAMLOCAL)
2195 printf(" %s", fwd->connect_path);
2196 else if (fwd->connect_host == NULL)
2197 printf(" %d", fwd->connect_port);
2198 else {
2199 printf(" [%s]:%d",
2200 fwd->connect_host, fwd->connect_port);
2201 }
2202 }
2203 printf("\n");
2204 }
2205}
2206
2207void
2208dump_client_config(Options *o, const char *host)
2209{
2210 int i;
2211 char vbuf[5];
2212
2213 /* Most interesting options first: user, host, port */
2214 dump_cfg_string(oUser, o->user);
2215 dump_cfg_string(oHostName, host);
2216 dump_cfg_int(oPort, o->port);
2217
2218 /* Flag options */
2219 dump_cfg_fmtint(oAddressFamily, o->address_family);
2220 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2221 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2222 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2223 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2224 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2225 dump_cfg_fmtint(oCompression, o->compression);
2226 dump_cfg_fmtint(oControlMaster, o->control_master);
2227 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2228 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002229 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002230 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2231 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2232 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2233 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2234#ifdef GSSAPI
2235 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2236 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2237#endif /* GSSAPI */
2238 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2239 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2240 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2241 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2242 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2243 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2244 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2245 dump_cfg_fmtint(oProtocol, o->protocol);
2246 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2247 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2248 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2249 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2250 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2251 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2252 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2253 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2254 dump_cfg_fmtint(oTunnel, o->tun_open);
2255 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2256 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2257 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
2258
2259 /* Integer options */
2260 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2261 dump_cfg_int(oCompressionLevel, o->compression_level);
2262 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2263 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2264 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2265 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2266 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2267
2268 /* String options */
2269 dump_cfg_string(oBindAddress, o->bind_address);
2270 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2271 dump_cfg_string(oControlPath, o->control_path);
2272 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms ? o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
2273 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
2274 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2275 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2276 dump_cfg_string(oLocalCommand, o->local_command);
2277 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2278 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2279 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2280 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2281 dump_cfg_string(oProxyCommand, o->proxy_command);
2282 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002283 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002284
2285 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2286 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2287 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2288
2289 /* String array options */
2290 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2291 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2292 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2293 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2294 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2295
2296 /* Special cases */
2297
2298 /* oConnectTimeout */
2299 if (o->connection_timeout == -1)
2300 printf("connecttimeout none\n");
2301 else
2302 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2303
2304 /* oTunnelDevice */
2305 printf("tunneldevice");
2306 if (o->tun_local == SSH_TUNID_ANY)
2307 printf(" any");
2308 else
2309 printf(" %d", o->tun_local);
2310 if (o->tun_remote == SSH_TUNID_ANY)
2311 printf(":any");
2312 else
2313 printf(":%d", o->tun_remote);
2314 printf("\n");
2315
2316 /* oCanonicalizePermittedCNAMEs */
2317 if ( o->num_permitted_cnames > 0) {
2318 printf("canonicalizePermittedcnames");
2319 for (i = 0; i < o->num_permitted_cnames; i++) {
2320 printf(" %s:%s", o->permitted_cnames[i].source_list,
2321 o->permitted_cnames[i].target_list);
2322 }
2323 printf("\n");
2324 }
2325
2326 /* oCipher */
2327 if (o->cipher != SSH_CIPHER_NOT_SET)
2328 printf("Cipher %s\n", cipher_name(o->cipher));
2329
2330 /* oControlPersist */
2331 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2332 dump_cfg_fmtint(oControlPersist, o->control_persist);
2333 else
2334 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2335
2336 /* oEscapeChar */
2337 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2338 printf("escapechar none\n");
2339 else {
2340 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2341 printf("escapechar %s\n", vbuf);
2342 }
2343
2344 /* oIPQoS */
2345 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2346 printf("%s\n", iptos2str(o->ip_qos_bulk));
2347
2348 /* oRekeyLimit */
2349 printf("rekeylimit %lld %d\n",
2350 (long long)o->rekey_limit, o->rekey_interval);
2351
2352 /* oStreamLocalBindMask */
2353 printf("streamlocalbindmask 0%o\n",
2354 o->fwd_opts.streamlocal_bind_mask);
2355}