blob: d7f1cf036a421c4801e817ba5d9d23d7a047c36e [file] [log] [blame]
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001/* $OpenBSD: readconf.c,v 1.226 2015/01/13 07:39:19 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100031#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110032#ifdef HAVE_PATHS_H
33# include <paths.h>
34#endif
Damien Miller194fd902013-10-15 12:13:05 +110035#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100036#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100037#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100040#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100041#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100042#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100043#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000044#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
45# include <vis.h>
46#endif
Damien Millerc7b06362006-03-15 11:53:45 +110047
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100050#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "cipher.h"
52#include "pathnames.h"
53#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "key.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100055#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "readconf.h"
57#include "match.h"
Damien Millerd7834352006-08-05 12:39:39 +100058#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "kex.h"
60#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110061#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000062#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000063#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65/* Format of the configuration file:
66
67 # Configuration data is parsed as follows:
68 # 1. command line options
69 # 2. user-specific file
70 # 3. system-wide file
71 # Any configuration value is only changed the first time it is set.
72 # Thus, host-specific definitions should be at the beginning of the
73 # configuration file, and defaults at the end.
74
75 # Host-specific declarations. These may override anything above. A single
76 # host may match multiple declarations; these are processed in the order
77 # that they are given in.
78
79 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000080 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82 Host fake.com
83 HostName another.host.name.real.org
84 User blaah
85 Port 34289
86 ForwardX11 no
87 ForwardAgent no
88
89 Host books.com
90 RemoteForward 9999 shadows.cs.hut.fi:9999
91 Cipher 3des
92
93 Host fascist.blob.com
94 Port 23123
95 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 PasswordAuthentication no
97
98 Host puukko.hut.fi
99 User t35124p
100 ProxyCommand ssh-proxy %h %p
101
102 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000103 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105 Host *.su
106 Cipher none
107 PasswordAuthentication no
108
Damien Millerd27b9472005-12-13 19:29:02 +1100109 Host vpn.fake.com
110 Tunnel yes
111 TunnelDevice 3
112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 # Defaults for various options
114 Host *
115 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 PasswordAuthentication yes
118 RSAAuthentication yes
119 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100121 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 IdentityFile ~/.ssh/identity
123 Port 22
124 EscapeChar ~
125
126*/
127
128/* Keyword tokens. */
129
Damien Miller95def091999-11-25 00:26:21 +1100130typedef enum {
131 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100132 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000133 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
134 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000135 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000136 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100137 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
Damien Miller194fd902013-10-15 12:13:05 +1100138 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100139 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
140 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100141 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000142 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000143 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000144 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000145 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100146 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000147 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000148 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000149 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100150 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000151 oSendEnv, oControlPath, oControlMaster, oControlPersist,
152 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100153 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Miller7cc194f2014-02-04 11:12:56 +1100154 oVisualHostKey, oUseRoaming,
Damien Miller1262b662013-08-21 02:44:24 +1000155 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100156 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
157 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000158 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000159 oFingerprintHash,
Darren Tucker07636982013-05-16 20:30:03 +1000160 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161} OpCodes;
162
163/* Textual representations of the tokens. */
164
Damien Miller95def091999-11-25 00:26:21 +1100165static struct {
166 const char *name;
167 OpCodes opcode;
168} keywords[] = {
169 { "forwardagent", oForwardAgent },
170 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000171 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000172 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000173 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000174 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100175 { "gatewayports", oGatewayPorts },
176 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000177 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100178 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100179 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
180 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100181 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000183 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000184 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000185 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000186 { "challengeresponseauthentication", oChallengeResponseAuthentication },
187 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
188 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000189 { "kerberosauthentication", oUnsupported },
190 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000191 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000192#if defined(GSSAPI)
193 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000194 { "gssapidelegatecredentials", oGssDelegateCreds },
195#else
196 { "gssapiauthentication", oUnsupported },
197 { "gssapidelegatecredentials", oUnsupported },
198#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000199 { "fallbacktorsh", oDeprecated },
200 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100201 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100202 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100203 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100204 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000205 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100206 { "proxycommand", oProxyCommand },
207 { "port", oPort },
208 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000209 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000210 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000211 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100212 { "remoteforward", oRemoteForward },
213 { "localforward", oLocalForward },
214 { "user", oUser },
215 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100216 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100217 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100218 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000219 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100220 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000221 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100222 { "connectionattempts", oConnectionAttempts },
223 { "batchmode", oBatchMode },
224 { "checkhostip", oCheckHostIP },
225 { "stricthostkeychecking", oStrictHostKeyChecking },
226 { "compression", oCompression },
227 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100228 { "tcpkeepalive", oTCPKeepAlive },
229 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100230 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100231 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000232 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000233 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000234 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000235 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100236#ifdef ENABLE_PKCS11
237 { "smartcarddevice", oPKCS11Provider },
238 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000239#else
240 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100241 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000242#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100243 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000244 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000245 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100246 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000247 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000248 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000249 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100250 { "serveraliveinterval", oServerAliveInterval },
251 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000252 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000253 { "controlpath", oControlPath },
254 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000255 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100256 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100257 { "tunnel", oTunnel },
258 { "tunneldevice", oTunnelDevice },
259 { "localcommand", oLocalCommand },
260 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000261 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000262 { "useroaming", oUseRoaming },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000263 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100264 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000265 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000266 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100267 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100268 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
269 { "canonicalizehostname", oCanonicalizeHostname },
270 { "canonicalizemaxdots", oCanonicalizeMaxDots },
271 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000272 { "streamlocalbindmask", oStreamLocalBindMask },
273 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000274 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000275 { "fingerprinthash", oFingerprintHash },
Darren Tucker07636982013-05-16 20:30:03 +1000276 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100277
Ben Lindstrom65366a82001-12-06 16:32:47 +0000278 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100279};
280
Damien Miller5428f641999-11-25 11:54:57 +1100281/*
282 * Adds a local TCP/IP port forward to options. Never returns if there is an
283 * error.
284 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller4af51302000-04-16 11:18:38 +1000286void
Damien Miller7acefbb2014-07-18 14:11:24 +1000287add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
Damien Miller7acefbb2014-07-18 14:11:24 +1000289 struct Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000290#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100291 extern uid_t original_real_uid;
Damien Miller7acefbb2014-07-18 14:11:24 +1000292 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
293 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000294 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100295#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000296 options->local_forwards = xrealloc(options->local_forwards,
297 options->num_local_forwards + 1,
298 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100299 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100300
Damien Miller1a0442f2008-11-05 16:30:06 +1100301 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100302 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000303 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100304 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100305 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000306 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307}
308
Damien Miller5428f641999-11-25 11:54:57 +1100309/*
310 * Adds a remote TCP/IP port forward to options. Never returns if there is
311 * an error.
312 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Damien Miller4af51302000-04-16 11:18:38 +1000314void
Damien Miller7acefbb2014-07-18 14:11:24 +1000315add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316{
Damien Miller7acefbb2014-07-18 14:11:24 +1000317 struct Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000318
319 options->remote_forwards = xrealloc(options->remote_forwards,
320 options->num_remote_forwards + 1,
321 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100322 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100323
Damien Miller1a0442f2008-11-05 16:30:06 +1100324 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100325 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000326 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100327 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100328 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000329 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100330 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000331 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332}
333
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000334static void
335clear_forwardings(Options *options)
336{
337 int i;
338
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000340 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000341 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000342 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000343 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100344 }
Damien Miller232cfb12010-06-26 09:50:30 +1000345 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000346 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000347 options->local_forwards = NULL;
348 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000349 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100350 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000351 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000352 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000353 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000354 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100355 }
Damien Miller232cfb12010-06-26 09:50:30 +1000356 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000357 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000358 options->remote_forwards = NULL;
359 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000360 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100361 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000362}
363
Darren Tucker19104782013-04-05 11:13:08 +1100364void
365add_identity_file(Options *options, const char *dir, const char *filename,
366 int userprovided)
367{
368 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000369 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100370
371 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
372 fatal("Too many identity files specified (max %d)",
373 SSH_MAX_IDENTITY_FILES);
374
375 if (dir == NULL) /* no dir, filename is absolute */
376 path = xstrdup(filename);
377 else
378 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
379
Damien Miller15271902014-05-15 13:47:56 +1000380 /* Avoid registering duplicates */
381 for (i = 0; i < options->num_identity_files; i++) {
382 if (options->identity_file_userprovided[i] == userprovided &&
383 strcmp(options->identity_files[i], path) == 0) {
384 debug2("%s: ignoring duplicate key %s", __func__, path);
385 free(path);
386 return;
387 }
388 }
389
Darren Tucker19104782013-04-05 11:13:08 +1100390 options->identity_file_userprovided[options->num_identity_files] =
391 userprovided;
392 options->identity_files[options->num_identity_files++] = path;
393}
394
Damien Miller194fd902013-10-15 12:13:05 +1100395int
396default_ssh_port(void)
397{
398 static int port;
399 struct servent *sp;
400
401 if (port == 0) {
402 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
403 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
404 }
405 return port;
406}
407
408/*
409 * Execute a command in a shell.
410 * Return its exit status or -1 on abnormal exit.
411 */
412static int
413execute_in_shell(const char *cmd)
414{
415 char *shell, *command_string;
416 pid_t pid;
417 int devnull, status;
418 extern uid_t original_real_uid;
419
420 if ((shell = getenv("SHELL")) == NULL)
421 shell = _PATH_BSHELL;
422
423 /*
424 * Use "exec" to avoid "sh -c" processes on some platforms
425 * (e.g. Solaris)
426 */
427 xasprintf(&command_string, "exec %s", cmd);
428
429 /* Need this to redirect subprocess stdin/out */
430 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
431 fatal("open(/dev/null): %s", strerror(errno));
432
433 debug("Executing command: '%.500s'", cmd);
434
435 /* Fork and execute the command. */
436 if ((pid = fork()) == 0) {
437 char *argv[4];
438
439 /* Child. Permanently give up superuser privileges. */
440 permanently_drop_suid(original_real_uid);
441
442 /* Redirect child stdin and stdout. Leave stderr */
443 if (dup2(devnull, STDIN_FILENO) == -1)
444 fatal("dup2: %s", strerror(errno));
445 if (dup2(devnull, STDOUT_FILENO) == -1)
446 fatal("dup2: %s", strerror(errno));
447 if (devnull > STDERR_FILENO)
448 close(devnull);
449 closefrom(STDERR_FILENO + 1);
450
451 argv[0] = shell;
452 argv[1] = "-c";
453 argv[2] = command_string;
454 argv[3] = NULL;
455
456 execv(argv[0], argv);
457 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
458 /* Die with signal to make this error apparent to parent. */
459 signal(SIGTERM, SIG_DFL);
460 kill(getpid(), SIGTERM);
461 _exit(1);
462 }
463 /* Parent. */
464 if (pid < 0)
465 fatal("%s: fork: %.100s", __func__, strerror(errno));
466
467 close(devnull);
468 free(command_string);
469
470 while (waitpid(pid, &status, 0) == -1) {
471 if (errno != EINTR && errno != EAGAIN)
472 fatal("%s: waitpid: %s", __func__, strerror(errno));
473 }
474 if (!WIFEXITED(status)) {
475 error("command '%.100s' exited abnormally", cmd);
476 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000477 }
Damien Miller194fd902013-10-15 12:13:05 +1100478 debug3("command returned status %d", WEXITSTATUS(status));
479 return WEXITSTATUS(status);
480}
481
482/*
483 * Parse and execute a Match directive.
484 */
485static int
486match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000487 const char *host_arg, const char *original_host, int post_canon,
488 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100489{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000490 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100491 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000492 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100493 size_t len;
494 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
495
496 /*
497 * Configuration is likely to be incomplete at this point so we
498 * must be prepared to use default values.
499 */
500 port = options->port <= 0 ? default_ssh_port() : options->port;
501 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100502 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100503 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100504 host = percent_expand(options->hostname,
505 "h", host_arg, (char *)NULL);
506 } else
507 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100508
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000509 debug2("checking match for '%s' host %s originally %s",
510 cp, host, original_host);
511 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
512 criteria = NULL;
513 this_result = 1;
514 if ((negate = attrib[0] == '!'))
515 attrib++;
516 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100517 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000518 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100519 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000520 error("%.200s line %d: '%s' cannot be combined "
521 "with other Match attributes",
522 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100523 result = -1;
524 goto out;
525 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000526 if (result)
527 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100528 goto out;
529 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000530 attributes++;
531 if (strcasecmp(attrib, "canonical") == 0) {
532 r = !!post_canon; /* force bitmask member to boolean */
533 if (r == (negate ? 1 : 0))
534 this_result = result = 0;
535 debug3("%.200s line %d: %smatched '%s'",
536 filename, linenum,
537 this_result ? "" : "not ", oattrib);
538 continue;
539 }
540 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100541 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
542 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100543 result = -1;
544 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100545 }
546 len = strlen(arg);
547 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 criteria = xstrdup(host);
549 r = match_hostname(host, arg, len) == 1;
550 if (r == (negate ? 1 : 0))
551 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100552 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000553 criteria = xstrdup(original_host);
554 r = match_hostname(original_host, arg, len) == 1;
555 if (r == (negate ? 1 : 0))
556 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100557 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000558 criteria = xstrdup(ruser);
559 r = match_pattern_list(ruser, arg, len, 0) == 1;
560 if (r == (negate ? 1 : 0))
561 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100562 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000563 criteria = xstrdup(pw->pw_name);
564 r = match_pattern_list(pw->pw_name, arg, len, 0) == 1;
565 if (r == (negate ? 1 : 0))
566 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100567 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100568 if (gethostname(thishost, sizeof(thishost)) == -1)
569 fatal("gethostname: %s", strerror(errno));
570 strlcpy(shorthost, thishost, sizeof(shorthost));
571 shorthost[strcspn(thishost, ".")] = '\0';
572 snprintf(portstr, sizeof(portstr), "%d", port);
573
574 cmd = percent_expand(arg,
575 "L", shorthost,
576 "d", pw->pw_dir,
577 "h", host,
578 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100580 "p", portstr,
581 "r", ruser,
582 "u", pw->pw_name,
583 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100584 if (result != 1) {
585 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000586 debug3("%.200s line %d: skipped exec "
587 "\"%.100s\"", filename, linenum, cmd);
588 free(cmd);
589 continue;
Damien Miller06287802014-02-24 15:56:45 +1100590 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000591 r = execute_in_shell(cmd);
592 if (r == -1) {
593 fatal("%.200s line %d: match exec "
594 "'%.100s' error", filename,
595 linenum, cmd);
596 }
597 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100598 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000599 /* Force exit status to boolean */
600 r = r == 0;
601 if (r == (negate ? 1 : 0))
602 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100603 } else {
604 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100605 result = -1;
606 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100607 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000608 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
609 filename, linenum, this_result ? "": "not ",
610 oattrib, criteria);
611 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100612 }
Damien Millercf31f382013-10-24 21:02:56 +1100613 if (attributes == 0) {
614 error("One or more attributes required for Match");
615 result = -1;
616 goto out;
617 }
Damien Miller084bcd22013-10-23 16:30:51 +1100618 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000619 if (result != -1)
620 debug2("match %sfound", result ? "" : "not ");
621 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100622 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100623 return result;
624}
625
Damien Miller0faf7472013-10-17 11:47:23 +1100626/* Check and prepare a domain name: removes trailing '.' and lowercases */
627static void
628valid_domain(char *name, const char *filename, int linenum)
629{
630 size_t i, l = strlen(name);
631 u_char c, last = '\0';
632
633 if (l == 0)
634 fatal("%s line %d: empty hostname suffix", filename, linenum);
635 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
636 fatal("%s line %d: hostname suffix \"%.100s\" "
637 "starts with invalid character", filename, linenum, name);
638 for (i = 0; i < l; i++) {
639 c = tolower((u_char)name[i]);
640 name[i] = (char)c;
641 if (last == '.' && c == '.')
642 fatal("%s line %d: hostname suffix \"%.100s\" contains "
643 "consecutive separators", filename, linenum, name);
644 if (c != '.' && c != '-' && !isalnum(c) &&
645 c != '_') /* technically invalid, but common */
646 fatal("%s line %d: hostname suffix \"%.100s\" contains "
647 "invalid characters", filename, linenum, name);
648 last = c;
649 }
650 if (name[l - 1] == '.')
651 name[l - 1] = '\0';
652}
653
Damien Miller5428f641999-11-25 11:54:57 +1100654/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000655 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100656 */
Damien Miller4af51302000-04-16 11:18:38 +1000657static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000658parse_token(const char *cp, const char *filename, int linenum,
659 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660{
Darren Tucker07636982013-05-16 20:30:03 +1000661 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller95def091999-11-25 00:26:21 +1100663 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000664 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100665 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000666 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
667 strlen(ignored_unknown), 1) == 1)
668 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000669 error("%s: line %d: Bad configuration option: %s",
670 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100671 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672}
673
Damien Millere9fc72e2013-10-15 12:14:12 +1100674/* Multistate option parsing */
675struct multistate {
676 char *key;
677 int value;
678};
679static const struct multistate multistate_flag[] = {
680 { "true", 1 },
681 { "false", 0 },
682 { "yes", 1 },
683 { "no", 0 },
684 { NULL, -1 }
685};
686static const struct multistate multistate_yesnoask[] = {
687 { "true", 1 },
688 { "false", 0 },
689 { "yes", 1 },
690 { "no", 0 },
691 { "ask", 2 },
692 { NULL, -1 }
693};
694static const struct multistate multistate_addressfamily[] = {
695 { "inet", AF_INET },
696 { "inet6", AF_INET6 },
697 { "any", AF_UNSPEC },
698 { NULL, -1 }
699};
700static const struct multistate multistate_controlmaster[] = {
701 { "true", SSHCTL_MASTER_YES },
702 { "yes", SSHCTL_MASTER_YES },
703 { "false", SSHCTL_MASTER_NO },
704 { "no", SSHCTL_MASTER_NO },
705 { "auto", SSHCTL_MASTER_AUTO },
706 { "ask", SSHCTL_MASTER_ASK },
707 { "autoask", SSHCTL_MASTER_AUTO_ASK },
708 { NULL, -1 }
709};
710static const struct multistate multistate_tunnel[] = {
711 { "ethernet", SSH_TUNMODE_ETHERNET },
712 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
713 { "true", SSH_TUNMODE_DEFAULT },
714 { "yes", SSH_TUNMODE_DEFAULT },
715 { "false", SSH_TUNMODE_NO },
716 { "no", SSH_TUNMODE_NO },
717 { NULL, -1 }
718};
719static const struct multistate multistate_requesttty[] = {
720 { "true", REQUEST_TTY_YES },
721 { "yes", REQUEST_TTY_YES },
722 { "false", REQUEST_TTY_NO },
723 { "no", REQUEST_TTY_NO },
724 { "force", REQUEST_TTY_FORCE },
725 { "auto", REQUEST_TTY_AUTO },
726 { NULL, -1 }
727};
Damien Miller38505592013-10-17 11:48:13 +1100728static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100729 { "true", SSH_CANONICALISE_YES },
730 { "false", SSH_CANONICALISE_NO },
731 { "yes", SSH_CANONICALISE_YES },
732 { "no", SSH_CANONICALISE_NO },
733 { "always", SSH_CANONICALISE_ALWAYS },
734 { NULL, -1 }
735};
Damien Millere9fc72e2013-10-15 12:14:12 +1100736
Damien Miller5428f641999-11-25 11:54:57 +1100737/*
738 * Processes a single option line as used in the configuration files. This
739 * only sets those values that have not already been set.
740 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100741#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100742int
Damien Miller194fd902013-10-15 12:13:05 +1100743process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000744 const char *original_host, char *line, const char *filename,
745 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746{
Damien Miller295ee632011-05-29 21:42:31 +1000747 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
748 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000749 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100750 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100751 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000752 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100753 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000754 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100755 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100756 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757
Damien Miller194fd902013-10-15 12:13:05 +1100758 if (activep == NULL) { /* We are processing a command line directive */
759 cmdline = 1;
760 activep = &cmdline;
761 }
762
Damien Millerc652cac2003-05-14 13:40:54 +1000763 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100764 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000765 if (strchr(WHITESPACE, line[len]) == NULL)
766 break;
767 line[len] = '\0';
768 }
769
Damien Millerbe484b52000-07-15 14:14:16 +1000770 s = line;
771 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100772 if ((keyword = strdelim(&s)) == NULL)
773 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000774 /* Ignore leading whitespace. */
775 if (*keyword == '\0')
776 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000777 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100778 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000779 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100780 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781
Darren Tucker07636982013-05-16 20:30:03 +1000782 opcode = parse_token(keyword, filename, linenum,
783 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784
Damien Miller95def091999-11-25 00:26:21 +1100785 switch (opcode) {
786 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100787 /* don't panic, but count bad options */
788 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100789 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000790 case oIgnoredUnknownOption:
791 debug("%s line %d: Ignored unknown option \"%s\"",
792 filename, linenum, keyword);
793 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000794 case oConnectTimeout:
795 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100796parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000797 arg = strdelim(&s);
798 if (!arg || *arg == '\0')
799 fatal("%s line %d: missing time value.",
800 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000801 if (strcmp(arg, "none") == 0)
802 value = -1;
803 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000804 fatal("%s line %d: invalid time value.",
805 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100806 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000807 *intptr = value;
808 break;
809
Damien Miller95def091999-11-25 00:26:21 +1100810 case oForwardAgent:
811 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100812 parse_flag:
813 multistate_ptr = multistate_flag;
814 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000815 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000816 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100817 fatal("%s line %d: missing argument.",
818 filename, linenum);
819 value = -1;
820 for (i = 0; multistate_ptr[i].key != NULL; i++) {
821 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
822 value = multistate_ptr[i].value;
823 break;
824 }
825 }
826 if (value == -1)
827 fatal("%s line %d: unsupported option \"%s\".",
828 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100829 if (*activep && *intptr == -1)
830 *intptr = value;
831 break;
832
833 case oForwardX11:
834 intptr = &options->forward_x11;
835 goto parse_flag;
836
Darren Tucker0a118da2003-10-15 15:54:32 +1000837 case oForwardX11Trusted:
838 intptr = &options->forward_x11_trusted;
839 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000840
Damien Miller1ab6a512010-06-26 10:02:24 +1000841 case oForwardX11Timeout:
842 intptr = &options->forward_x11_timeout;
843 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000844
Damien Miller95def091999-11-25 00:26:21 +1100845 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000846 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100847 goto parse_flag;
848
Darren Tuckere7d4b192006-07-12 22:17:10 +1000849 case oExitOnForwardFailure:
850 intptr = &options->exit_on_forward_failure;
851 goto parse_flag;
852
Damien Miller95def091999-11-25 00:26:21 +1100853 case oUsePrivilegedPort:
854 intptr = &options->use_privileged_port;
855 goto parse_flag;
856
Damien Miller95def091999-11-25 00:26:21 +1100857 case oPasswordAuthentication:
858 intptr = &options->password_authentication;
859 goto parse_flag;
860
Damien Miller874d77b2000-10-14 16:23:11 +1100861 case oKbdInteractiveAuthentication:
862 intptr = &options->kbd_interactive_authentication;
863 goto parse_flag;
864
865 case oKbdInteractiveDevices:
866 charptr = &options->kbd_interactive_devices;
867 goto parse_string;
868
Damien Miller0bc1bd82000-11-13 22:57:25 +1100869 case oPubkeyAuthentication:
870 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000871 goto parse_flag;
872
Damien Miller95def091999-11-25 00:26:21 +1100873 case oRSAAuthentication:
874 intptr = &options->rsa_authentication;
875 goto parse_flag;
876
877 case oRhostsRSAAuthentication:
878 intptr = &options->rhosts_rsa_authentication;
879 goto parse_flag;
880
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000881 case oHostbasedAuthentication:
882 intptr = &options->hostbased_authentication;
883 goto parse_flag;
884
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000885 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000886 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100887 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000888
Darren Tucker0efd1552003-08-26 11:49:55 +1000889 case oGssAuthentication:
890 intptr = &options->gss_authentication;
891 goto parse_flag;
892
893 case oGssDelegateCreds:
894 intptr = &options->gss_deleg_creds;
895 goto parse_flag;
896
Damien Miller95def091999-11-25 00:26:21 +1100897 case oBatchMode:
898 intptr = &options->batch_mode;
899 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
Damien Miller95def091999-11-25 00:26:21 +1100901 case oCheckHostIP:
902 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000903 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904
Damien Miller37876e92003-05-15 10:19:46 +1000905 case oVerifyHostKeyDNS:
906 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100907 multistate_ptr = multistate_yesnoask;
908 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000909
Damien Miller95def091999-11-25 00:26:21 +1100910 case oStrictHostKeyChecking:
911 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100912 multistate_ptr = multistate_yesnoask;
913 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller95def091999-11-25 00:26:21 +1100915 case oCompression:
916 intptr = &options->compression;
917 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Damien Miller12c150e2003-12-17 16:31:10 +1100919 case oTCPKeepAlive:
920 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100921 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000923 case oNoHostAuthenticationForLocalhost:
924 intptr = &options->no_host_authentication_for_localhost;
925 goto parse_flag;
926
Damien Miller95def091999-11-25 00:26:21 +1100927 case oNumberOfPasswordPrompts:
928 intptr = &options->number_of_password_prompts;
929 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930
Damien Miller95def091999-11-25 00:26:21 +1100931 case oCompressionLevel:
932 intptr = &options->compression_level;
933 goto parse_int;
934
Damien Millera5539d22003-04-09 20:50:06 +1000935 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000936 arg = strdelim(&s);
937 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000938 fatal("%.200s line %d: Missing argument.", filename,
939 linenum);
940 if (strcmp(arg, "default") == 0) {
941 val64 = 0;
942 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000943 if (scan_scaled(arg, &val64) == -1)
944 fatal("%.200s line %d: Bad number '%s': %s",
945 filename, linenum, arg, strerror(errno));
946 /* check for too-large or too-small limits */
947 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000948 fatal("%.200s line %d: RekeyLimit too large",
949 filename, linenum);
950 if (val64 != 0 && val64 < 16)
951 fatal("%.200s line %d: RekeyLimit too small",
952 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000953 }
Damien Miller3dff1762008-02-10 22:25:52 +1100954 if (*activep && options->rekey_limit == -1)
955 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000956 if (s != NULL) { /* optional rekey interval present */
957 if (strcmp(s, "none") == 0) {
958 (void)strdelim(&s); /* discard */
959 break;
960 }
961 intptr = &options->rekey_interval;
962 goto parse_time;
963 }
Damien Millera5539d22003-04-09 20:50:06 +1000964 break;
965
Damien Miller95def091999-11-25 00:26:21 +1100966 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000967 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000968 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100969 fatal("%.200s line %d: Missing argument.", filename, linenum);
970 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100971 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000972 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100973 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100974 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000975 add_identity_file(options, NULL,
976 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +1100977 }
978 break;
979
Damien Millerd3a18572000-06-07 19:55:44 +1000980 case oXAuthLocation:
981 charptr=&options->xauth_location;
982 goto parse_string;
983
Damien Miller95def091999-11-25 00:26:21 +1100984 case oUser:
985 charptr = &options->user;
986parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000987 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000988 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000989 fatal("%.200s line %d: Missing argument.",
990 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100991 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000992 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100993 break;
994
995 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000996 cpptr = (char **)&options->system_hostfiles;
997 uintptr = &options->num_system_hostfiles;
998 max_entries = SSH_MAX_HOSTS_FILES;
999parse_char_array:
1000 if (*activep && *uintptr == 0) {
1001 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1002 if ((*uintptr) >= max_entries)
1003 fatal("%s line %d: "
1004 "too many authorized keys files.",
1005 filename, linenum);
1006 cpptr[(*uintptr)++] = xstrdup(arg);
1007 }
1008 }
1009 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001010
1011 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001012 cpptr = (char **)&options->user_hostfiles;
1013 uintptr = &options->num_user_hostfiles;
1014 max_entries = SSH_MAX_HOSTS_FILES;
1015 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001016
Damien Miller95def091999-11-25 00:26:21 +11001017 case oHostName:
1018 charptr = &options->hostname;
1019 goto parse_string;
1020
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001021 case oHostKeyAlias:
1022 charptr = &options->host_key_alias;
1023 goto parse_string;
1024
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001025 case oPreferredAuthentications:
1026 charptr = &options->preferred_authentications;
1027 goto parse_string;
1028
Ben Lindstrome0f88042001-04-30 13:06:24 +00001029 case oBindAddress:
1030 charptr = &options->bind_address;
1031 goto parse_string;
1032
Damien Miller7ea845e2010-02-12 09:21:02 +11001033 case oPKCS11Provider:
1034 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001035 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001036
Damien Miller95def091999-11-25 00:26:21 +11001037 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001038 charptr = &options->proxy_command;
1039parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001040 if (s == NULL)
1041 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001042 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001043 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001044 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001045 return 0;
1046
1047 case oPort:
1048 intptr = &options->port;
1049parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001050 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001051 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001052 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001053 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001054 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001055
1056 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001057 value = strtol(arg, &endofnumber, 0);
1058 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001059 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001060 if (*activep && *intptr == -1)
1061 *intptr = value;
1062 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063
Damien Miller95def091999-11-25 00:26:21 +11001064 case oConnectionAttempts:
1065 intptr = &options->connection_attempts;
1066 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001067
Damien Miller95def091999-11-25 00:26:21 +11001068 case oCipher:
1069 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001070 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001071 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001072 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001073 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001074 if (value == -1)
1075 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001076 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001077 if (*activep && *intptr == -1)
1078 *intptr = value;
1079 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080
Damien Miller78928792000-04-12 20:17:38 +10001081 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001082 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001083 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001084 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001085 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001086 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001087 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001088 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001089 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001090 break;
1091
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001092 case oMacs:
1093 arg = strdelim(&s);
1094 if (!arg || *arg == '\0')
1095 fatal("%.200s line %d: Missing argument.", filename, linenum);
1096 if (!mac_valid(arg))
1097 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001098 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001099 if (*activep && options->macs == NULL)
1100 options->macs = xstrdup(arg);
1101 break;
1102
Damien Millerd5f62bf2010-09-24 22:11:14 +10001103 case oKexAlgorithms:
1104 arg = strdelim(&s);
1105 if (!arg || *arg == '\0')
1106 fatal("%.200s line %d: Missing argument.",
1107 filename, linenum);
1108 if (!kex_names_valid(arg))
1109 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1110 filename, linenum, arg ? arg : "<NONE>");
1111 if (*activep && options->kex_algorithms == NULL)
1112 options->kex_algorithms = xstrdup(arg);
1113 break;
1114
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001115 case oHostKeyAlgorithms:
1116 arg = strdelim(&s);
1117 if (!arg || *arg == '\0')
1118 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001119 if (!sshkey_names_valid2(arg, 1))
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001120 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001121 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001122 if (*activep && options->hostkeyalgorithms == NULL)
1123 options->hostkeyalgorithms = xstrdup(arg);
1124 break;
1125
Damien Miller78928792000-04-12 20:17:38 +10001126 case oProtocol:
1127 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001128 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001129 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001130 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001131 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001132 if (value == SSH_PROTO_UNKNOWN)
1133 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001134 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001135 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1136 *intptr = value;
1137 break;
1138
Damien Miller95def091999-11-25 00:26:21 +11001139 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001140 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001141 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001142 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001143 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001144 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001145 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001146 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1147 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001148 break;
1149
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001150 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001151 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001152 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001153 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001154 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001155 fatal("%.200s line %d: Missing port argument.",
1156 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001157
Damien Millera699d952008-11-03 19:27:34 +11001158 if (opcode == oLocalForward ||
1159 opcode == oRemoteForward) {
1160 arg2 = strdelim(&s);
1161 if (arg2 == NULL || *arg2 == '\0')
1162 fatal("%.200s line %d: Missing target argument.",
1163 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001164
Damien Millera699d952008-11-03 19:27:34 +11001165 /* construct a string for parse_forward */
1166 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1167 } else if (opcode == oDynamicForward) {
1168 strlcpy(fwdarg, arg, sizeof(fwdarg));
1169 }
1170
1171 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001172 opcode == oDynamicForward ? 1 : 0,
1173 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001174 fatal("%.200s line %d: Bad forwarding specification.",
1175 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001176
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001177 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001178 if (opcode == oLocalForward ||
1179 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001180 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001181 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001182 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001183 }
Damien Miller95def091999-11-25 00:26:21 +11001184 break;
1185
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001186 case oClearAllForwardings:
1187 intptr = &options->clear_forwardings;
1188 goto parse_flag;
1189
Damien Miller95def091999-11-25 00:26:21 +11001190 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001191 if (cmdline)
1192 fatal("Host directive not supported as a command-line "
1193 "option");
Damien Miller95def091999-11-25 00:26:21 +11001194 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001195 arg2 = NULL;
1196 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1197 negated = *arg == '!';
1198 if (negated)
1199 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001200 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001201 if (negated) {
1202 debug("%.200s line %d: Skipping Host "
1203 "block because of negated match "
1204 "for %.100s", filename, linenum,
1205 arg);
1206 *activep = 0;
1207 break;
1208 }
1209 if (!*activep)
1210 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001211 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001212 }
Damien Millerfe924212011-05-15 08:44:45 +10001213 }
1214 if (*activep)
1215 debug("%.200s line %d: Applying options for %.100s",
1216 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001217 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001218 return 0;
1219
Damien Miller194fd902013-10-15 12:13:05 +11001220 case oMatch:
1221 if (cmdline)
1222 fatal("Host directive not supported as a command-line "
1223 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001224 value = match_cfg_line(options, &s, pw, host, original_host,
1225 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001226 if (value < 0)
1227 fatal("%.200s line %d: Bad Match condition", filename,
1228 linenum);
1229 *activep = value;
1230 break;
1231
Damien Miller95def091999-11-25 00:26:21 +11001232 case oEscapeChar:
1233 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001234 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001235 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001236 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001237 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001238 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1239 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001240 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001241 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001242 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001243 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001244 else {
1245 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001246 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001247 /* NOTREACHED */
1248 value = 0; /* Avoid compiler warning. */
1249 }
1250 if (*activep && *intptr == -1)
1251 *intptr = value;
1252 break;
1253
Damien Miller20a8f972003-05-18 20:50:30 +10001254 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001255 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001256 multistate_ptr = multistate_addressfamily;
1257 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001258
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001259 case oEnableSSHKeysign:
1260 intptr = &options->enable_ssh_keysign;
1261 goto parse_flag;
1262
Damien Millerbd394c32004-03-08 23:12:36 +11001263 case oIdentitiesOnly:
1264 intptr = &options->identities_only;
1265 goto parse_flag;
1266
Damien Miller509b0102003-12-17 16:33:10 +11001267 case oServerAliveInterval:
1268 intptr = &options->server_alive_interval;
1269 goto parse_time;
1270
1271 case oServerAliveCountMax:
1272 intptr = &options->server_alive_count_max;
1273 goto parse_int;
1274
Darren Tucker46bc0752004-05-02 22:11:30 +10001275 case oSendEnv:
1276 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1277 if (strchr(arg, '=') != NULL)
1278 fatal("%s line %d: Invalid environment name.",
1279 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001280 if (!*activep)
1281 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001282 if (options->num_send_env >= MAX_SEND_ENV)
1283 fatal("%s line %d: too many send env.",
1284 filename, linenum);
1285 options->send_env[options->num_send_env++] =
1286 xstrdup(arg);
1287 }
1288 break;
1289
Damien Miller0e220db2004-06-15 10:34:08 +10001290 case oControlPath:
1291 charptr = &options->control_path;
1292 goto parse_string;
1293
1294 case oControlMaster:
1295 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001296 multistate_ptr = multistate_controlmaster;
1297 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001298
Damien Millere11e1ea2010-08-03 16:04:46 +10001299 case oControlPersist:
1300 /* no/false/yes/true, or a time spec */
1301 intptr = &options->control_persist;
1302 arg = strdelim(&s);
1303 if (!arg || *arg == '\0')
1304 fatal("%.200s line %d: Missing ControlPersist"
1305 " argument.", filename, linenum);
1306 value = 0;
1307 value2 = 0; /* timeout */
1308 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1309 value = 0;
1310 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1311 value = 1;
1312 else if ((value2 = convtime(arg)) >= 0)
1313 value = 1;
1314 else
1315 fatal("%.200s line %d: Bad ControlPersist argument.",
1316 filename, linenum);
1317 if (*activep && *intptr == -1) {
1318 *intptr = value;
1319 options->control_persist_timeout = value2;
1320 }
1321 break;
1322
Damien Millere1776152005-03-01 21:47:37 +11001323 case oHashKnownHosts:
1324 intptr = &options->hash_known_hosts;
1325 goto parse_flag;
1326
Damien Millerd27b9472005-12-13 19:29:02 +11001327 case oTunnel:
1328 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001329 multistate_ptr = multistate_tunnel;
1330 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001331
1332 case oTunnelDevice:
1333 arg = strdelim(&s);
1334 if (!arg || *arg == '\0')
1335 fatal("%.200s line %d: Missing argument.", filename, linenum);
1336 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001337 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001338 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1339 if (*activep) {
1340 options->tun_local = value;
1341 options->tun_remote = value2;
1342 }
1343 break;
1344
1345 case oLocalCommand:
1346 charptr = &options->local_command;
1347 goto parse_command;
1348
1349 case oPermitLocalCommand:
1350 intptr = &options->permit_local_command;
1351 goto parse_flag;
1352
Damien Miller10288242008-06-30 00:04:03 +10001353 case oVisualHostKey:
1354 intptr = &options->visual_host_key;
1355 goto parse_flag;
1356
Damien Miller0dac6fb2010-11-20 15:19:38 +11001357 case oIPQoS:
1358 arg = strdelim(&s);
1359 if ((value = parse_ipqos(arg)) == -1)
1360 fatal("%s line %d: Bad IPQoS value: %s",
1361 filename, linenum, arg);
1362 arg = strdelim(&s);
1363 if (arg == NULL)
1364 value2 = value;
1365 else if ((value2 = parse_ipqos(arg)) == -1)
1366 fatal("%s line %d: Bad IPQoS value: %s",
1367 filename, linenum, arg);
1368 if (*activep) {
1369 options->ip_qos_interactive = value;
1370 options->ip_qos_bulk = value2;
1371 }
1372 break;
1373
Darren Tucker71e4d542009-07-06 07:12:27 +10001374 case oUseRoaming:
1375 intptr = &options->use_roaming;
1376 goto parse_flag;
1377
Damien Miller21771e22011-05-15 08:45:50 +10001378 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001379 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001380 multistate_ptr = multistate_requesttty;
1381 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001382
Darren Tucker07636982013-05-16 20:30:03 +10001383 case oIgnoreUnknown:
1384 charptr = &options->ignored_unknown;
1385 goto parse_string;
1386
Damien Miller1262b662013-08-21 02:44:24 +10001387 case oProxyUseFdpass:
1388 intptr = &options->proxy_use_fdpass;
1389 goto parse_flag;
1390
Damien Miller0faf7472013-10-17 11:47:23 +11001391 case oCanonicalDomains:
1392 value = options->num_canonical_domains != 0;
1393 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1394 valid_domain(arg, filename, linenum);
1395 if (!*activep || value)
1396 continue;
1397 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1398 fatal("%s line %d: too many hostname suffixes.",
1399 filename, linenum);
1400 options->canonical_domains[
1401 options->num_canonical_domains++] = xstrdup(arg);
1402 }
1403 break;
1404
Damien Miller38505592013-10-17 11:48:13 +11001405 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001406 value = options->num_permitted_cnames != 0;
1407 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1408 /* Either '*' for everything or 'list:list' */
1409 if (strcmp(arg, "*") == 0)
1410 arg2 = arg;
1411 else {
1412 lowercase(arg);
1413 if ((arg2 = strchr(arg, ':')) == NULL ||
1414 arg2[1] == '\0') {
1415 fatal("%s line %d: "
1416 "Invalid permitted CNAME \"%s\"",
1417 filename, linenum, arg);
1418 }
1419 *arg2 = '\0';
1420 arg2++;
1421 }
1422 if (!*activep || value)
1423 continue;
1424 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1425 fatal("%s line %d: too many permitted CNAMEs.",
1426 filename, linenum);
1427 cname = options->permitted_cnames +
1428 options->num_permitted_cnames++;
1429 cname->source_list = xstrdup(arg);
1430 cname->target_list = xstrdup(arg2);
1431 }
1432 break;
1433
Damien Miller38505592013-10-17 11:48:13 +11001434 case oCanonicalizeHostname:
1435 intptr = &options->canonicalize_hostname;
1436 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001437 goto parse_multistate;
1438
Damien Miller38505592013-10-17 11:48:13 +11001439 case oCanonicalizeMaxDots:
1440 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001441 goto parse_int;
1442
Damien Miller38505592013-10-17 11:48:13 +11001443 case oCanonicalizeFallbackLocal:
1444 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001445 goto parse_flag;
1446
Damien Miller7acefbb2014-07-18 14:11:24 +10001447 case oStreamLocalBindMask:
1448 arg = strdelim(&s);
1449 if (!arg || *arg == '\0')
1450 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1451 /* Parse mode in octal format */
1452 value = strtol(arg, &endofnumber, 8);
1453 if (arg == endofnumber || value < 0 || value > 0777)
1454 fatal("%.200s line %d: Bad mask.", filename, linenum);
1455 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1456 break;
1457
1458 case oStreamLocalBindUnlink:
1459 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1460 goto parse_flag;
1461
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001462 case oRevokedHostKeys:
1463 charptr = &options->revoked_host_keys;
1464 goto parse_string;
1465
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001466 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001467 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001468 arg = strdelim(&s);
1469 if (!arg || *arg == '\0')
1470 fatal("%.200s line %d: Missing argument.",
1471 filename, linenum);
1472 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1473 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1474 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001475 if (*activep && *intptr == -1)
1476 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001477 break;
1478
Ben Lindstrom4daea862002-06-09 20:04:02 +00001479 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001480 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001481 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001482 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001483
Damien Millerf9b3feb2003-05-16 11:38:32 +10001484 case oUnsupported:
1485 error("%s line %d: Unsupported option \"%s\"",
1486 filename, linenum, keyword);
1487 return 0;
1488
Damien Miller95def091999-11-25 00:26:21 +11001489 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001490 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001491 }
1492
1493 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001494 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001495 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001496 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001497 }
Damien Miller95def091999-11-25 00:26:21 +11001498 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499}
1500
1501
Damien Miller5428f641999-11-25 11:54:57 +11001502/*
1503 * Reads the config file and modifies the options accordingly. Options
1504 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001505 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001506 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001508int
Damien Miller194fd902013-10-15 12:13:05 +11001509read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001510 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511{
Damien Miller95def091999-11-25 00:26:21 +11001512 FILE *f;
1513 char line[1024];
1514 int active, linenum;
1515 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516
Damien Miller57a44762004-04-20 20:11:57 +10001517 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001518 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519
Darren Tuckeraefa3682013-04-05 11:18:35 +11001520 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001521 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001522
Damien Miller33793852004-06-15 10:27:55 +10001523 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001524 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001525 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001526 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001527 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001528 }
1529
Damien Miller95def091999-11-25 00:26:21 +11001530 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001531
Damien Miller5428f641999-11-25 11:54:57 +11001532 /*
1533 * Mark that we are now processing the options. This flag is turned
1534 * on/off by Host specifications.
1535 */
Damien Miller95def091999-11-25 00:26:21 +11001536 active = 1;
1537 linenum = 0;
1538 while (fgets(line, sizeof(line), f)) {
1539 /* Update line number counter. */
1540 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001541 if (process_config_line(options, pw, host, original_host,
1542 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001543 bad_options++;
1544 }
1545 fclose(f);
1546 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001547 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001548 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001549 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001550}
1551
Damien Miller13f97b22014-02-24 15:57:55 +11001552/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1553int
1554option_clear_or_none(const char *o)
1555{
1556 return o == NULL || strcasecmp(o, "none") == 0;
1557}
1558
Damien Miller5428f641999-11-25 11:54:57 +11001559/*
1560 * Initializes options to special values that indicate that they have not yet
1561 * been set. Read_config_file will only set options with this value. Options
1562 * are processed in the following order: command line, user config file,
1563 * system config file. Last, fill_default_options is called.
1564 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565
Damien Miller4af51302000-04-16 11:18:38 +10001566void
Damien Miller95def091999-11-25 00:26:21 +11001567initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001568{
Damien Miller95def091999-11-25 00:26:21 +11001569 memset(options, 'X', sizeof(*options));
1570 options->forward_agent = -1;
1571 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001572 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001573 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001574 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001575 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001576 options->fwd_opts.gateway_ports = -1;
1577 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1578 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001579 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001580 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001581 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001582 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001583 options->gss_authentication = -1;
1584 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001585 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001586 options->kbd_interactive_authentication = -1;
1587 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001588 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001589 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001590 options->batch_mode = -1;
1591 options->check_host_ip = -1;
1592 options->strict_host_key_checking = -1;
1593 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001594 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001595 options->compression_level = -1;
1596 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001597 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001598 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001599 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001600 options->number_of_password_prompts = -1;
1601 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001602 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001603 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001604 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001605 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001606 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001607 options->num_identity_files = 0;
1608 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001609 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001610 options->proxy_command = NULL;
1611 options->user = NULL;
1612 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001613 options->num_system_hostfiles = 0;
1614 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001615 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001616 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001617 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001618 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001619 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001620 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001621 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001622 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001623 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001624 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001625 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001626 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001627 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001628 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001629 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001630 options->server_alive_interval = -1;
1631 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001632 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001633 options->control_path = NULL;
1634 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001635 options->control_persist = -1;
1636 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001637 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001638 options->tun_open = -1;
1639 options->tun_local = -1;
1640 options->tun_remote = -1;
1641 options->local_command = NULL;
1642 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001643 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001644 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001645 options->ip_qos_interactive = -1;
1646 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001647 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001648 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001649 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001650 options->num_canonical_domains = 0;
1651 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001652 options->canonicalize_max_dots = -1;
1653 options->canonicalize_fallback_local = -1;
1654 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001655 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001656 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001657}
1658
Damien Miller5428f641999-11-25 11:54:57 +11001659/*
Damien Miller13f97b22014-02-24 15:57:55 +11001660 * A petite version of fill_default_options() that just fills the options
1661 * needed for hostname canonicalization to proceed.
1662 */
1663void
1664fill_default_options_for_canonicalization(Options *options)
1665{
1666 if (options->canonicalize_max_dots == -1)
1667 options->canonicalize_max_dots = 1;
1668 if (options->canonicalize_fallback_local == -1)
1669 options->canonicalize_fallback_local = 1;
1670 if (options->canonicalize_hostname == -1)
1671 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1672}
1673
1674/*
Damien Miller5428f641999-11-25 11:54:57 +11001675 * Called after processing other sources of option data, this fills those
1676 * options for which no value has been specified with their default values.
1677 */
Damien Miller4af51302000-04-16 11:18:38 +10001678void
Damien Miller95def091999-11-25 00:26:21 +11001679fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001680{
Damien Miller95def091999-11-25 00:26:21 +11001681 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001682 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001683 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001684 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001685 if (options->forward_x11_trusted == -1)
1686 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001687 if (options->forward_x11_timeout == -1)
1688 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001689 if (options->exit_on_forward_failure == -1)
1690 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001691 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001692 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001693 if (options->fwd_opts.gateway_ports == -1)
1694 options->fwd_opts.gateway_ports = 0;
1695 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1696 options->fwd_opts.streamlocal_bind_mask = 0177;
1697 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1698 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001699 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001700 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001701 if (options->rsa_authentication == -1)
1702 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001703 if (options->pubkey_authentication == -1)
1704 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001705 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001706 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001707 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001708 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001709 if (options->gss_deleg_creds == -1)
1710 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001711 if (options->password_authentication == -1)
1712 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001713 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001714 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001715 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001716 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001717 if (options->hostbased_authentication == -1)
1718 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001719 if (options->batch_mode == -1)
1720 options->batch_mode = 0;
1721 if (options->check_host_ip == -1)
1722 options->check_host_ip = 1;
1723 if (options->strict_host_key_checking == -1)
1724 options->strict_host_key_checking = 2; /* 2 is default */
1725 if (options->compression == -1)
1726 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001727 if (options->tcp_keep_alive == -1)
1728 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001729 if (options->compression_level == -1)
1730 options->compression_level = 6;
1731 if (options->port == -1)
1732 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001733 if (options->address_family == -1)
1734 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001735 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001736 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001737 if (options->number_of_password_prompts == -1)
1738 options->number_of_password_prompts = 3;
1739 /* Selected in ssh_login(). */
1740 if (options->cipher == -1)
1741 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001742 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001743 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001744 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001745 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001746 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001747 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001748 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001749 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001750 add_identity_file(options, "~/",
1751 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001752 }
1753 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001754 add_identity_file(options, "~/",
1755 _PATH_SSH_CLIENT_ID_RSA, 0);
1756 add_identity_file(options, "~/",
1757 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001758#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001759 add_identity_file(options, "~/",
1760 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001761#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001762 add_identity_file(options, "~/",
1763 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001764 }
Damien Millereba71ba2000-04-29 23:57:08 +10001765 }
Damien Miller95def091999-11-25 00:26:21 +11001766 if (options->escape_char == -1)
1767 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001768 if (options->num_system_hostfiles == 0) {
1769 options->system_hostfiles[options->num_system_hostfiles++] =
1770 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1771 options->system_hostfiles[options->num_system_hostfiles++] =
1772 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1773 }
1774 if (options->num_user_hostfiles == 0) {
1775 options->user_hostfiles[options->num_user_hostfiles++] =
1776 xstrdup(_PATH_SSH_USER_HOSTFILE);
1777 options->user_hostfiles[options->num_user_hostfiles++] =
1778 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1779 }
Damien Millerfcd93202002-02-05 12:26:34 +11001780 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001781 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001782 if (options->clear_forwardings == 1)
1783 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001784 if (options->no_host_authentication_for_localhost == - 1)
1785 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001786 if (options->identities_only == -1)
1787 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001788 if (options->enable_ssh_keysign == -1)
1789 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001790 if (options->rekey_limit == -1)
1791 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001792 if (options->rekey_interval == -1)
1793 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001794 if (options->verify_host_key_dns == -1)
1795 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001796 if (options->server_alive_interval == -1)
1797 options->server_alive_interval = 0;
1798 if (options->server_alive_count_max == -1)
1799 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001800 if (options->control_master == -1)
1801 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001802 if (options->control_persist == -1) {
1803 options->control_persist = 0;
1804 options->control_persist_timeout = 0;
1805 }
Damien Millere1776152005-03-01 21:47:37 +11001806 if (options->hash_known_hosts == -1)
1807 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001808 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001809 options->tun_open = SSH_TUNMODE_NO;
1810 if (options->tun_local == -1)
1811 options->tun_local = SSH_TUNID_ANY;
1812 if (options->tun_remote == -1)
1813 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001814 if (options->permit_local_command == -1)
1815 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001816 if (options->use_roaming == -1)
1817 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001818 if (options->visual_host_key == -1)
1819 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001820 if (options->ip_qos_interactive == -1)
1821 options->ip_qos_interactive = IPTOS_LOWDELAY;
1822 if (options->ip_qos_bulk == -1)
1823 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001824 if (options->request_tty == -1)
1825 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001826 if (options->proxy_use_fdpass == -1)
1827 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001828 if (options->canonicalize_max_dots == -1)
1829 options->canonicalize_max_dots = 1;
1830 if (options->canonicalize_fallback_local == -1)
1831 options->canonicalize_fallback_local = 1;
1832 if (options->canonicalize_hostname == -1)
1833 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001834 if (options->fingerprint_hash == -1)
1835 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
1836
Damien Millere9fc72e2013-10-15 12:14:12 +11001837#define CLEAR_ON_NONE(v) \
1838 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001839 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001840 free(v); \
1841 v = NULL; \
1842 } \
1843 } while(0)
1844 CLEAR_ON_NONE(options->local_command);
1845 CLEAR_ON_NONE(options->proxy_command);
1846 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001847 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001848 /* options->user will be set in the main program if appropriate */
1849 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001850 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001851 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001853
Damien Miller7acefbb2014-07-18 14:11:24 +10001854struct fwdarg {
1855 char *arg;
1856 int ispath;
1857};
1858
1859/*
1860 * parse_fwd_field
1861 * parses the next field in a port forwarding specification.
1862 * sets fwd to the parsed field and advances p past the colon
1863 * or sets it to NULL at end of string.
1864 * returns 0 on success, else non-zero.
1865 */
1866static int
1867parse_fwd_field(char **p, struct fwdarg *fwd)
1868{
1869 char *ep, *cp = *p;
1870 int ispath = 0;
1871
1872 if (*cp == '\0') {
1873 *p = NULL;
1874 return -1; /* end of string */
1875 }
1876
1877 /*
1878 * A field escaped with square brackets is used literally.
1879 * XXX - allow ']' to be escaped via backslash?
1880 */
1881 if (*cp == '[') {
1882 /* find matching ']' */
1883 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1884 if (*ep == '/')
1885 ispath = 1;
1886 }
1887 /* no matching ']' or not at end of field. */
1888 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1889 return -1;
1890 /* NUL terminate the field and advance p past the colon */
1891 *ep++ = '\0';
1892 if (*ep != '\0')
1893 *ep++ = '\0';
1894 fwd->arg = cp + 1;
1895 fwd->ispath = ispath;
1896 *p = ep;
1897 return 0;
1898 }
1899
1900 for (cp = *p; *cp != '\0'; cp++) {
1901 switch (*cp) {
1902 case '\\':
1903 memmove(cp, cp + 1, strlen(cp + 1) + 1);
1904 cp++;
1905 break;
1906 case '/':
1907 ispath = 1;
1908 break;
1909 case ':':
1910 *cp++ = '\0';
1911 goto done;
1912 }
1913 }
1914done:
1915 fwd->arg = *p;
1916 fwd->ispath = ispath;
1917 *p = cp;
1918 return 0;
1919}
1920
Damien Millerf91ee4c2005-03-01 21:24:33 +11001921/*
1922 * parse_forward
1923 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001924 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10001925 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
1926 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11001927 * dynamicfwd == 1
1928 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001929 * returns number of arguments parsed or zero on error
1930 */
1931int
Damien Miller7acefbb2014-07-18 14:11:24 +10001932parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001933{
Damien Miller7acefbb2014-07-18 14:11:24 +10001934 struct fwdarg fwdargs[4];
1935 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001936 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001937
Damien Miller7acefbb2014-07-18 14:11:24 +10001938 memset(fwd, 0, sizeof(*fwd));
1939 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11001940
1941 cp = p = xstrdup(fwdspec);
1942
1943 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001944 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001945 cp++;
1946
Damien Miller7acefbb2014-07-18 14:11:24 +10001947 for (i = 0; i < 4; ++i) {
1948 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001949 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10001950 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001951
Damien Millerf4b39532008-11-03 19:28:21 +11001952 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10001953 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001954 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10001955 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001956
1957 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001958 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10001959 if (fwdargs[0].ispath) {
1960 fwd->listen_path = xstrdup(fwdargs[0].arg);
1961 fwd->listen_port = PORT_STREAMLOCAL;
1962 } else {
1963 fwd->listen_host = NULL;
1964 fwd->listen_port = a2port(fwdargs[0].arg);
1965 }
Damien Millera699d952008-11-03 19:27:34 +11001966 fwd->connect_host = xstrdup("socks");
1967 break;
1968
1969 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10001970 if (fwdargs[0].ispath && fwdargs[1].ispath) {
1971 fwd->listen_path = xstrdup(fwdargs[0].arg);
1972 fwd->listen_port = PORT_STREAMLOCAL;
1973 fwd->connect_path = xstrdup(fwdargs[1].arg);
1974 fwd->connect_port = PORT_STREAMLOCAL;
1975 } else if (fwdargs[1].ispath) {
1976 fwd->listen_host = NULL;
1977 fwd->listen_port = a2port(fwdargs[0].arg);
1978 fwd->connect_path = xstrdup(fwdargs[1].arg);
1979 fwd->connect_port = PORT_STREAMLOCAL;
1980 } else {
1981 fwd->listen_host = xstrdup(fwdargs[0].arg);
1982 fwd->listen_port = a2port(fwdargs[1].arg);
1983 fwd->connect_host = xstrdup("socks");
1984 }
Damien Millera699d952008-11-03 19:27:34 +11001985 break;
1986
Damien Millerf91ee4c2005-03-01 21:24:33 +11001987 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10001988 if (fwdargs[0].ispath) {
1989 fwd->listen_path = xstrdup(fwdargs[0].arg);
1990 fwd->listen_port = PORT_STREAMLOCAL;
1991 fwd->connect_host = xstrdup(fwdargs[1].arg);
1992 fwd->connect_port = a2port(fwdargs[2].arg);
1993 } else if (fwdargs[2].ispath) {
1994 fwd->listen_host = xstrdup(fwdargs[0].arg);
1995 fwd->listen_port = a2port(fwdargs[1].arg);
1996 fwd->connect_path = xstrdup(fwdargs[2].arg);
1997 fwd->connect_port = PORT_STREAMLOCAL;
1998 } else {
1999 fwd->listen_host = NULL;
2000 fwd->listen_port = a2port(fwdargs[0].arg);
2001 fwd->connect_host = xstrdup(fwdargs[1].arg);
2002 fwd->connect_port = a2port(fwdargs[2].arg);
2003 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002004 break;
2005
2006 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002007 fwd->listen_host = xstrdup(fwdargs[0].arg);
2008 fwd->listen_port = a2port(fwdargs[1].arg);
2009 fwd->connect_host = xstrdup(fwdargs[2].arg);
2010 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002011 break;
2012 default:
2013 i = 0; /* failure */
2014 }
2015
Darren Tuckera627d422013-06-02 07:31:17 +10002016 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002017
Damien Millera699d952008-11-03 19:27:34 +11002018 if (dynamicfwd) {
2019 if (!(i == 1 || i == 2))
2020 goto fail_free;
2021 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002022 if (!(i == 3 || i == 4)) {
2023 if (fwd->connect_path == NULL &&
2024 fwd->listen_path == NULL)
2025 goto fail_free;
2026 }
2027 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002028 goto fail_free;
2029 }
2030
Damien Miller7acefbb2014-07-18 14:11:24 +10002031 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2032 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002033 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002034 if (fwd->connect_host != NULL &&
2035 strlen(fwd->connect_host) >= NI_MAXHOST)
2036 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002037 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2038 if (fwd->connect_path != NULL &&
2039 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2040 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002041 if (fwd->listen_host != NULL &&
2042 strlen(fwd->listen_host) >= NI_MAXHOST)
2043 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002044 if (fwd->listen_path != NULL &&
2045 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2046 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002047
2048 return (i);
2049
2050 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002051 free(fwd->connect_host);
2052 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002053 free(fwd->connect_path);
2054 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002055 free(fwd->listen_host);
2056 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002057 free(fwd->listen_path);
2058 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002059 return (0);
2060}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002061
2062/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2063static const char *
2064fmt_multistate_int(int val, const struct multistate *m)
2065{
2066 u_int i;
2067
2068 for (i = 0; m[i].key != NULL; i++) {
2069 if (m[i].value == val)
2070 return m[i].key;
2071 }
2072 return "UNKNOWN";
2073}
2074
2075static const char *
2076fmt_intarg(OpCodes code, int val)
2077{
2078 if (val == -1)
2079 return "unset";
2080 switch (code) {
2081 case oAddressFamily:
2082 return fmt_multistate_int(val, multistate_addressfamily);
2083 case oVerifyHostKeyDNS:
2084 case oStrictHostKeyChecking:
2085 return fmt_multistate_int(val, multistate_yesnoask);
2086 case oControlMaster:
2087 return fmt_multistate_int(val, multistate_controlmaster);
2088 case oTunnel:
2089 return fmt_multistate_int(val, multistate_tunnel);
2090 case oRequestTTY:
2091 return fmt_multistate_int(val, multistate_requesttty);
2092 case oCanonicalizeHostname:
2093 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002094 case oFingerprintHash:
2095 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002096 case oProtocol:
2097 switch (val) {
2098 case SSH_PROTO_1:
2099 return "1";
2100 case SSH_PROTO_2:
2101 return "2";
2102 case (SSH_PROTO_1|SSH_PROTO_2):
2103 return "2,1";
2104 default:
2105 return "UNKNOWN";
2106 }
2107 default:
2108 switch (val) {
2109 case 0:
2110 return "no";
2111 case 1:
2112 return "yes";
2113 default:
2114 return "UNKNOWN";
2115 }
2116 }
2117}
2118
2119static const char *
2120lookup_opcode_name(OpCodes code)
2121{
2122 u_int i;
2123
2124 for (i = 0; keywords[i].name != NULL; i++)
2125 if (keywords[i].opcode == code)
2126 return(keywords[i].name);
2127 return "UNKNOWN";
2128}
2129
2130static void
2131dump_cfg_int(OpCodes code, int val)
2132{
2133 printf("%s %d\n", lookup_opcode_name(code), val);
2134}
2135
2136static void
2137dump_cfg_fmtint(OpCodes code, int val)
2138{
2139 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2140}
2141
2142static void
2143dump_cfg_string(OpCodes code, const char *val)
2144{
2145 if (val == NULL)
2146 return;
2147 printf("%s %s\n", lookup_opcode_name(code), val);
2148}
2149
2150static void
2151dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2152{
2153 u_int i;
2154
2155 for (i = 0; i < count; i++)
2156 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2157}
2158
2159static void
2160dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2161{
2162 u_int i;
2163
2164 printf("%s", lookup_opcode_name(code));
2165 for (i = 0; i < count; i++)
2166 printf(" %s", vals[i]);
2167 printf("\n");
2168}
2169
2170static void
2171dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2172{
2173 const struct Forward *fwd;
2174 u_int i;
2175
2176 /* oDynamicForward */
2177 for (i = 0; i < count; i++) {
2178 fwd = &fwds[i];
2179 if (code == oDynamicForward &&
2180 strcmp(fwd->connect_host, "socks") != 0)
2181 continue;
2182 if (code == oLocalForward &&
2183 strcmp(fwd->connect_host, "socks") == 0)
2184 continue;
2185 printf("%s", lookup_opcode_name(code));
2186 if (fwd->listen_port == PORT_STREAMLOCAL)
2187 printf(" %s", fwd->listen_path);
2188 else if (fwd->listen_host == NULL)
2189 printf(" %d", fwd->listen_port);
2190 else {
2191 printf(" [%s]:%d",
2192 fwd->listen_host, fwd->listen_port);
2193 }
2194 if (code != oDynamicForward) {
2195 if (fwd->connect_port == PORT_STREAMLOCAL)
2196 printf(" %s", fwd->connect_path);
2197 else if (fwd->connect_host == NULL)
2198 printf(" %d", fwd->connect_port);
2199 else {
2200 printf(" [%s]:%d",
2201 fwd->connect_host, fwd->connect_port);
2202 }
2203 }
2204 printf("\n");
2205 }
2206}
2207
2208void
2209dump_client_config(Options *o, const char *host)
2210{
2211 int i;
2212 char vbuf[5];
2213
2214 /* Most interesting options first: user, host, port */
2215 dump_cfg_string(oUser, o->user);
2216 dump_cfg_string(oHostName, host);
2217 dump_cfg_int(oPort, o->port);
2218
2219 /* Flag options */
2220 dump_cfg_fmtint(oAddressFamily, o->address_family);
2221 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2222 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2223 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2224 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2225 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2226 dump_cfg_fmtint(oCompression, o->compression);
2227 dump_cfg_fmtint(oControlMaster, o->control_master);
2228 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2229 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002230 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002231 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2232 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2233 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2234 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2235#ifdef GSSAPI
2236 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2237 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2238#endif /* GSSAPI */
2239 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2240 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2241 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2242 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2243 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2244 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2245 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2246 dump_cfg_fmtint(oProtocol, o->protocol);
2247 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2248 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2249 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2250 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2251 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2252 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2253 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2254 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2255 dump_cfg_fmtint(oTunnel, o->tun_open);
2256 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2257 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2258 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
2259
2260 /* Integer options */
2261 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2262 dump_cfg_int(oCompressionLevel, o->compression_level);
2263 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2264 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2265 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2266 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2267 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2268
2269 /* String options */
2270 dump_cfg_string(oBindAddress, o->bind_address);
2271 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2272 dump_cfg_string(oControlPath, o->control_path);
2273 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms ? o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
2274 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
2275 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2276 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2277 dump_cfg_string(oLocalCommand, o->local_command);
2278 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2279 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2280 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2281 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2282 dump_cfg_string(oProxyCommand, o->proxy_command);
2283 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002284 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002285
2286 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2287 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2288 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2289
2290 /* String array options */
2291 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2292 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2293 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2294 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2295 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2296
2297 /* Special cases */
2298
2299 /* oConnectTimeout */
2300 if (o->connection_timeout == -1)
2301 printf("connecttimeout none\n");
2302 else
2303 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2304
2305 /* oTunnelDevice */
2306 printf("tunneldevice");
2307 if (o->tun_local == SSH_TUNID_ANY)
2308 printf(" any");
2309 else
2310 printf(" %d", o->tun_local);
2311 if (o->tun_remote == SSH_TUNID_ANY)
2312 printf(":any");
2313 else
2314 printf(":%d", o->tun_remote);
2315 printf("\n");
2316
2317 /* oCanonicalizePermittedCNAMEs */
2318 if ( o->num_permitted_cnames > 0) {
2319 printf("canonicalizePermittedcnames");
2320 for (i = 0; i < o->num_permitted_cnames; i++) {
2321 printf(" %s:%s", o->permitted_cnames[i].source_list,
2322 o->permitted_cnames[i].target_list);
2323 }
2324 printf("\n");
2325 }
2326
2327 /* oCipher */
2328 if (o->cipher != SSH_CIPHER_NOT_SET)
2329 printf("Cipher %s\n", cipher_name(o->cipher));
2330
2331 /* oControlPersist */
2332 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2333 dump_cfg_fmtint(oControlPersist, o->control_persist);
2334 else
2335 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2336
2337 /* oEscapeChar */
2338 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2339 printf("escapechar none\n");
2340 else {
2341 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2342 printf("escapechar %s\n", vbuf);
2343 }
2344
2345 /* oIPQoS */
2346 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2347 printf("%s\n", iptos2str(o->ip_qos_bulk));
2348
2349 /* oRekeyLimit */
2350 printf("rekeylimit %lld %d\n",
2351 (long long)o->rekey_limit, o->rekey_interval);
2352
2353 /* oStreamLocalBindMask */
2354 printf("streamlocalbindmask 0%o\n",
2355 o->fwd_opts.streamlocal_bind_mask);
2356}