blob: 2a5620479c7444cbe265488b956c4862228d7575 [file] [log] [blame]
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001/* $OpenBSD: readconf.c,v 1.249 2016/01/29 02:54:45 dtucker 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>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100043#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100044#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000045#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
46# include <vis.h>
47#endif
Damien Millerc7b06362006-03-15 11:53:45 +110048
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "cipher.h"
53#include "pathnames.h"
54#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000055#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100056#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "readconf.h"
58#include "match.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,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000138 oCertificateFile, oAddKeysToAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100139 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100140 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
141 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100142 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000143 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000144 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000145 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000146 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100147 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000148 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000149 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000150 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100151 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000152 oSendEnv, oControlPath, oControlMaster, oControlPersist,
153 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100154 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000155 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000156 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100157 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
158 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000159 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000160 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000161 oPubkeyAcceptedKeyTypes,
Darren Tucker07636982013-05-16 20:30:03 +1000162 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163} OpCodes;
164
165/* Textual representations of the tokens. */
166
Damien Miller95def091999-11-25 00:26:21 +1100167static struct {
168 const char *name;
169 OpCodes opcode;
170} keywords[] = {
171 { "forwardagent", oForwardAgent },
172 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000173 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000174 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000175 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000176 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "gatewayports", oGatewayPorts },
178 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000179 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100181 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
182 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100183 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100184 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000185 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000186 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000187 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000188 { "challengeresponseauthentication", oChallengeResponseAuthentication },
189 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
190 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000191 { "kerberosauthentication", oUnsupported },
192 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000193 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000194#if defined(GSSAPI)
195 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000196 { "gssapidelegatecredentials", oGssDelegateCreds },
197#else
198 { "gssapiauthentication", oUnsupported },
199 { "gssapidelegatecredentials", oUnsupported },
200#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000201 { "fallbacktorsh", oDeprecated },
202 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100203 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100204 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100205 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000206 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000207 { "addkeystoagent", oAddKeysToAgent },
Damien Miller95def091999-11-25 00:26:21 +1100208 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000209 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100210 { "proxycommand", oProxyCommand },
211 { "port", oPort },
212 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000213 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000214 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000215 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100216 { "remoteforward", oRemoteForward },
217 { "localforward", oLocalForward },
218 { "user", oUser },
219 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100220 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100221 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100222 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000223 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100224 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000225 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100226 { "connectionattempts", oConnectionAttempts },
227 { "batchmode", oBatchMode },
228 { "checkhostip", oCheckHostIP },
229 { "stricthostkeychecking", oStrictHostKeyChecking },
230 { "compression", oCompression },
231 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100232 { "tcpkeepalive", oTCPKeepAlive },
233 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100234 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100235 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000236 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000237 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000238 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000239 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100240#ifdef ENABLE_PKCS11
241 { "smartcarddevice", oPKCS11Provider },
242 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000243#else
244 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100245 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000246#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100247 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000248 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000249 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100250 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000251 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000252 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000253 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100254 { "serveraliveinterval", oServerAliveInterval },
255 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000256 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000257 { "controlpath", oControlPath },
258 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000259 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100260 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100261 { "tunnel", oTunnel },
262 { "tunneldevice", oTunnelDevice },
263 { "localcommand", oLocalCommand },
264 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000265 { "visualhostkey", oVisualHostKey },
markus@openbsd.orga3068632016-01-14 16:17:39 +0000266 { "useroaming", oDeprecated },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000267 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100268 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000269 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000270 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100271 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100272 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
273 { "canonicalizehostname", oCanonicalizeHostname },
274 { "canonicalizemaxdots", oCanonicalizeMaxDots },
275 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000276 { "streamlocalbindmask", oStreamLocalBindMask },
277 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000278 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000279 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000280 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000281 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000282 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000283 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100284
Ben Lindstrom65366a82001-12-06 16:32:47 +0000285 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100286};
287
Damien Miller5428f641999-11-25 11:54:57 +1100288/*
289 * Adds a local TCP/IP port forward to options. Never returns if there is an
290 * error.
291 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Miller4af51302000-04-16 11:18:38 +1000293void
Damien Miller7acefbb2014-07-18 14:11:24 +1000294add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295{
Damien Miller7acefbb2014-07-18 14:11:24 +1000296 struct Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000297#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100298 extern uid_t original_real_uid;
Damien Miller7acefbb2014-07-18 14:11:24 +1000299 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
300 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000301 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100302#endif
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000303 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000304 options->num_local_forwards + 1,
305 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100306 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100307
Damien Miller1a0442f2008-11-05 16:30:06 +1100308 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100309 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000310 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100311 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100312 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000313 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314}
315
Damien Miller5428f641999-11-25 11:54:57 +1100316/*
317 * Adds a remote TCP/IP port forward to options. Never returns if there is
318 * an error.
319 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Damien Miller4af51302000-04-16 11:18:38 +1000321void
Damien Miller7acefbb2014-07-18 14:11:24 +1000322add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323{
Damien Miller7acefbb2014-07-18 14:11:24 +1000324 struct Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000325
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000326 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000327 options->num_remote_forwards + 1,
328 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100329 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100330
Damien Miller1a0442f2008-11-05 16:30:06 +1100331 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100332 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000333 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100334 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100335 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000336 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100337 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000338 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339}
340
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000341static void
342clear_forwardings(Options *options)
343{
344 int i;
345
Damien Millerf91ee4c2005-03-01 21:24:33 +1100346 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000347 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000348 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000349 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000350 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100351 }
Damien Miller232cfb12010-06-26 09:50:30 +1000352 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000353 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000354 options->local_forwards = NULL;
355 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000356 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100357 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000358 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000359 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000360 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000361 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100362 }
Damien Miller232cfb12010-06-26 09:50:30 +1000363 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000364 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000365 options->remote_forwards = NULL;
366 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000367 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100368 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000369}
370
Darren Tucker19104782013-04-05 11:13:08 +1100371void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000372add_certificate_file(Options *options, const char *path, int userprovided)
373{
374 int i;
375
376 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
377 fatal("Too many certificate files specified (max %d)",
378 SSH_MAX_CERTIFICATE_FILES);
379
380 /* Avoid registering duplicates */
381 for (i = 0; i < options->num_certificate_files; i++) {
382 if (options->certificate_file_userprovided[i] == userprovided &&
383 strcmp(options->certificate_files[i], path) == 0) {
384 debug2("%s: ignoring duplicate key %s", __func__, path);
385 return;
386 }
387 }
388
389 options->certificate_file_userprovided[options->num_certificate_files] =
390 userprovided;
391 options->certificate_files[options->num_certificate_files++] =
392 xstrdup(path);
393}
394
395void
Darren Tucker19104782013-04-05 11:13:08 +1100396add_identity_file(Options *options, const char *dir, const char *filename,
397 int userprovided)
398{
399 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000400 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100401
402 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
403 fatal("Too many identity files specified (max %d)",
404 SSH_MAX_IDENTITY_FILES);
405
406 if (dir == NULL) /* no dir, filename is absolute */
407 path = xstrdup(filename);
408 else
409 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
410
Damien Miller15271902014-05-15 13:47:56 +1000411 /* Avoid registering duplicates */
412 for (i = 0; i < options->num_identity_files; i++) {
413 if (options->identity_file_userprovided[i] == userprovided &&
414 strcmp(options->identity_files[i], path) == 0) {
415 debug2("%s: ignoring duplicate key %s", __func__, path);
416 free(path);
417 return;
418 }
419 }
420
Darren Tucker19104782013-04-05 11:13:08 +1100421 options->identity_file_userprovided[options->num_identity_files] =
422 userprovided;
423 options->identity_files[options->num_identity_files++] = path;
424}
425
Damien Miller194fd902013-10-15 12:13:05 +1100426int
427default_ssh_port(void)
428{
429 static int port;
430 struct servent *sp;
431
432 if (port == 0) {
433 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
434 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
435 }
436 return port;
437}
438
439/*
440 * Execute a command in a shell.
441 * Return its exit status or -1 on abnormal exit.
442 */
443static int
444execute_in_shell(const char *cmd)
445{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000446 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100447 pid_t pid;
448 int devnull, status;
449 extern uid_t original_real_uid;
450
451 if ((shell = getenv("SHELL")) == NULL)
452 shell = _PATH_BSHELL;
453
Damien Miller194fd902013-10-15 12:13:05 +1100454 /* Need this to redirect subprocess stdin/out */
455 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
456 fatal("open(/dev/null): %s", strerror(errno));
457
458 debug("Executing command: '%.500s'", cmd);
459
460 /* Fork and execute the command. */
461 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000462 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100463
464 /* Child. Permanently give up superuser privileges. */
465 permanently_drop_suid(original_real_uid);
466
467 /* Redirect child stdin and stdout. Leave stderr */
468 if (dup2(devnull, STDIN_FILENO) == -1)
469 fatal("dup2: %s", strerror(errno));
470 if (dup2(devnull, STDOUT_FILENO) == -1)
471 fatal("dup2: %s", strerror(errno));
472 if (devnull > STDERR_FILENO)
473 close(devnull);
474 closefrom(STDERR_FILENO + 1);
475
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000476 argv[0] = shell;
477 argv[1] = "-c";
478 argv[2] = xstrdup(cmd);
479 argv[3] = NULL;
480
Damien Miller194fd902013-10-15 12:13:05 +1100481 execv(argv[0], argv);
482 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
483 /* Die with signal to make this error apparent to parent. */
484 signal(SIGTERM, SIG_DFL);
485 kill(getpid(), SIGTERM);
486 _exit(1);
487 }
488 /* Parent. */
489 if (pid < 0)
490 fatal("%s: fork: %.100s", __func__, strerror(errno));
491
492 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100493
494 while (waitpid(pid, &status, 0) == -1) {
495 if (errno != EINTR && errno != EAGAIN)
496 fatal("%s: waitpid: %s", __func__, strerror(errno));
497 }
498 if (!WIFEXITED(status)) {
499 error("command '%.100s' exited abnormally", cmd);
500 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000501 }
Damien Miller194fd902013-10-15 12:13:05 +1100502 debug3("command returned status %d", WEXITSTATUS(status));
503 return WEXITSTATUS(status);
504}
505
506/*
507 * Parse and execute a Match directive.
508 */
509static int
510match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000511 const char *host_arg, const char *original_host, int post_canon,
512 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100513{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000514 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100515 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000516 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100517 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
518
519 /*
520 * Configuration is likely to be incomplete at this point so we
521 * must be prepared to use default values.
522 */
523 port = options->port <= 0 ? default_ssh_port() : options->port;
524 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100525 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100526 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100527 host = percent_expand(options->hostname,
528 "h", host_arg, (char *)NULL);
529 } else
530 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100531
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000532 debug2("checking match for '%s' host %s originally %s",
533 cp, host, original_host);
534 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
535 criteria = NULL;
536 this_result = 1;
537 if ((negate = attrib[0] == '!'))
538 attrib++;
539 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100540 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000541 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100542 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000543 error("%.200s line %d: '%s' cannot be combined "
544 "with other Match attributes",
545 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100546 result = -1;
547 goto out;
548 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000549 if (result)
550 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100551 goto out;
552 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000553 attributes++;
554 if (strcasecmp(attrib, "canonical") == 0) {
555 r = !!post_canon; /* force bitmask member to boolean */
556 if (r == (negate ? 1 : 0))
557 this_result = result = 0;
558 debug3("%.200s line %d: %smatched '%s'",
559 filename, linenum,
560 this_result ? "" : "not ", oattrib);
561 continue;
562 }
563 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100564 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
565 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100566 result = -1;
567 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100568 }
Damien Miller194fd902013-10-15 12:13:05 +1100569 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000570 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000571 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000572 if (r == (negate ? 1 : 0))
573 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100574 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000575 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000576 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000577 if (r == (negate ? 1 : 0))
578 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100579 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000580 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000581 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000582 if (r == (negate ? 1 : 0))
583 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100584 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000585 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000586 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000587 if (r == (negate ? 1 : 0))
588 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100589 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100590 if (gethostname(thishost, sizeof(thishost)) == -1)
591 fatal("gethostname: %s", strerror(errno));
592 strlcpy(shorthost, thishost, sizeof(shorthost));
593 shorthost[strcspn(thishost, ".")] = '\0';
594 snprintf(portstr, sizeof(portstr), "%d", port);
595
596 cmd = percent_expand(arg,
597 "L", shorthost,
598 "d", pw->pw_dir,
599 "h", host,
600 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000601 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100602 "p", portstr,
603 "r", ruser,
604 "u", pw->pw_name,
605 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100606 if (result != 1) {
607 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000608 debug3("%.200s line %d: skipped exec "
609 "\"%.100s\"", filename, linenum, cmd);
610 free(cmd);
611 continue;
Damien Miller06287802014-02-24 15:56:45 +1100612 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000613 r = execute_in_shell(cmd);
614 if (r == -1) {
615 fatal("%.200s line %d: match exec "
616 "'%.100s' error", filename,
617 linenum, cmd);
618 }
619 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100620 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 /* Force exit status to boolean */
622 r = r == 0;
623 if (r == (negate ? 1 : 0))
624 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100625 } else {
626 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100627 result = -1;
628 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100629 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000630 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
631 filename, linenum, this_result ? "": "not ",
632 oattrib, criteria);
633 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100634 }
Damien Millercf31f382013-10-24 21:02:56 +1100635 if (attributes == 0) {
636 error("One or more attributes required for Match");
637 result = -1;
638 goto out;
639 }
Damien Miller084bcd22013-10-23 16:30:51 +1100640 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000641 if (result != -1)
642 debug2("match %sfound", result ? "" : "not ");
643 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100644 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100645 return result;
646}
647
Damien Miller0faf7472013-10-17 11:47:23 +1100648/* Check and prepare a domain name: removes trailing '.' and lowercases */
649static void
650valid_domain(char *name, const char *filename, int linenum)
651{
652 size_t i, l = strlen(name);
653 u_char c, last = '\0';
654
655 if (l == 0)
656 fatal("%s line %d: empty hostname suffix", filename, linenum);
657 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
658 fatal("%s line %d: hostname suffix \"%.100s\" "
659 "starts with invalid character", filename, linenum, name);
660 for (i = 0; i < l; i++) {
661 c = tolower((u_char)name[i]);
662 name[i] = (char)c;
663 if (last == '.' && c == '.')
664 fatal("%s line %d: hostname suffix \"%.100s\" contains "
665 "consecutive separators", filename, linenum, name);
666 if (c != '.' && c != '-' && !isalnum(c) &&
667 c != '_') /* technically invalid, but common */
668 fatal("%s line %d: hostname suffix \"%.100s\" contains "
669 "invalid characters", filename, linenum, name);
670 last = c;
671 }
672 if (name[l - 1] == '.')
673 name[l - 1] = '\0';
674}
675
Damien Miller5428f641999-11-25 11:54:57 +1100676/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000677 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100678 */
Damien Miller4af51302000-04-16 11:18:38 +1000679static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000680parse_token(const char *cp, const char *filename, int linenum,
681 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682{
Darren Tucker07636982013-05-16 20:30:03 +1000683 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Miller95def091999-11-25 00:26:21 +1100685 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000686 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100687 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000688 if (ignored_unknown != NULL &&
689 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000690 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000691 error("%s: line %d: Bad configuration option: %s",
692 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100693 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694}
695
Damien Millere9fc72e2013-10-15 12:14:12 +1100696/* Multistate option parsing */
697struct multistate {
698 char *key;
699 int value;
700};
701static const struct multistate multistate_flag[] = {
702 { "true", 1 },
703 { "false", 0 },
704 { "yes", 1 },
705 { "no", 0 },
706 { NULL, -1 }
707};
708static const struct multistate multistate_yesnoask[] = {
709 { "true", 1 },
710 { "false", 0 },
711 { "yes", 1 },
712 { "no", 0 },
713 { "ask", 2 },
714 { NULL, -1 }
715};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000716static const struct multistate multistate_yesnoaskconfirm[] = {
717 { "true", 1 },
718 { "false", 0 },
719 { "yes", 1 },
720 { "no", 0 },
721 { "ask", 2 },
722 { "confirm", 3 },
723 { NULL, -1 }
724};
Damien Millere9fc72e2013-10-15 12:14:12 +1100725static const struct multistate multistate_addressfamily[] = {
726 { "inet", AF_INET },
727 { "inet6", AF_INET6 },
728 { "any", AF_UNSPEC },
729 { NULL, -1 }
730};
731static const struct multistate multistate_controlmaster[] = {
732 { "true", SSHCTL_MASTER_YES },
733 { "yes", SSHCTL_MASTER_YES },
734 { "false", SSHCTL_MASTER_NO },
735 { "no", SSHCTL_MASTER_NO },
736 { "auto", SSHCTL_MASTER_AUTO },
737 { "ask", SSHCTL_MASTER_ASK },
738 { "autoask", SSHCTL_MASTER_AUTO_ASK },
739 { NULL, -1 }
740};
741static const struct multistate multistate_tunnel[] = {
742 { "ethernet", SSH_TUNMODE_ETHERNET },
743 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
744 { "true", SSH_TUNMODE_DEFAULT },
745 { "yes", SSH_TUNMODE_DEFAULT },
746 { "false", SSH_TUNMODE_NO },
747 { "no", SSH_TUNMODE_NO },
748 { NULL, -1 }
749};
750static const struct multistate multistate_requesttty[] = {
751 { "true", REQUEST_TTY_YES },
752 { "yes", REQUEST_TTY_YES },
753 { "false", REQUEST_TTY_NO },
754 { "no", REQUEST_TTY_NO },
755 { "force", REQUEST_TTY_FORCE },
756 { "auto", REQUEST_TTY_AUTO },
757 { NULL, -1 }
758};
Damien Miller38505592013-10-17 11:48:13 +1100759static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100760 { "true", SSH_CANONICALISE_YES },
761 { "false", SSH_CANONICALISE_NO },
762 { "yes", SSH_CANONICALISE_YES },
763 { "no", SSH_CANONICALISE_NO },
764 { "always", SSH_CANONICALISE_ALWAYS },
765 { NULL, -1 }
766};
Damien Millere9fc72e2013-10-15 12:14:12 +1100767
Damien Miller5428f641999-11-25 11:54:57 +1100768/*
769 * Processes a single option line as used in the configuration files. This
770 * only sets those values that have not already been set.
771 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100772#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100773int
Damien Miller194fd902013-10-15 12:13:05 +1100774process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000775 const char *original_host, char *line, const char *filename,
776 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777{
Damien Miller295ee632011-05-29 21:42:31 +1000778 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
779 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000780 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100781 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100782 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000783 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100784 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000785 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100786 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100787 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788
Damien Miller194fd902013-10-15 12:13:05 +1100789 if (activep == NULL) { /* We are processing a command line directive */
790 cmdline = 1;
791 activep = &cmdline;
792 }
793
Damien Millerc652cac2003-05-14 13:40:54 +1000794 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000795 if ((len = strlen(line)) == 0)
796 return 0;
797 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000798 if (strchr(WHITESPACE, line[len]) == NULL)
799 break;
800 line[len] = '\0';
801 }
802
Damien Millerbe484b52000-07-15 14:14:16 +1000803 s = line;
804 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100805 if ((keyword = strdelim(&s)) == NULL)
806 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000807 /* Ignore leading whitespace. */
808 if (*keyword == '\0')
809 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000810 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100811 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000812 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100813 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Darren Tucker07636982013-05-16 20:30:03 +1000815 opcode = parse_token(keyword, filename, linenum,
816 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817
Damien Miller95def091999-11-25 00:26:21 +1100818 switch (opcode) {
819 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100820 /* don't panic, but count bad options */
821 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100822 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000823 case oIgnoredUnknownOption:
824 debug("%s line %d: Ignored unknown option \"%s\"",
825 filename, linenum, keyword);
826 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000827 case oConnectTimeout:
828 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100829parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000830 arg = strdelim(&s);
831 if (!arg || *arg == '\0')
832 fatal("%s line %d: missing time value.",
833 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000834 if (strcmp(arg, "none") == 0)
835 value = -1;
836 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000837 fatal("%s line %d: invalid time value.",
838 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100839 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000840 *intptr = value;
841 break;
842
Damien Miller95def091999-11-25 00:26:21 +1100843 case oForwardAgent:
844 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100845 parse_flag:
846 multistate_ptr = multistate_flag;
847 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000848 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000849 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100850 fatal("%s line %d: missing argument.",
851 filename, linenum);
852 value = -1;
853 for (i = 0; multistate_ptr[i].key != NULL; i++) {
854 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
855 value = multistate_ptr[i].value;
856 break;
857 }
858 }
859 if (value == -1)
860 fatal("%s line %d: unsupported option \"%s\".",
861 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100862 if (*activep && *intptr == -1)
863 *intptr = value;
864 break;
865
866 case oForwardX11:
867 intptr = &options->forward_x11;
868 goto parse_flag;
869
Darren Tucker0a118da2003-10-15 15:54:32 +1000870 case oForwardX11Trusted:
871 intptr = &options->forward_x11_trusted;
872 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000873
Damien Miller1ab6a512010-06-26 10:02:24 +1000874 case oForwardX11Timeout:
875 intptr = &options->forward_x11_timeout;
876 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000877
Damien Miller95def091999-11-25 00:26:21 +1100878 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000879 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100880 goto parse_flag;
881
Darren Tuckere7d4b192006-07-12 22:17:10 +1000882 case oExitOnForwardFailure:
883 intptr = &options->exit_on_forward_failure;
884 goto parse_flag;
885
Damien Miller95def091999-11-25 00:26:21 +1100886 case oUsePrivilegedPort:
887 intptr = &options->use_privileged_port;
888 goto parse_flag;
889
Damien Miller95def091999-11-25 00:26:21 +1100890 case oPasswordAuthentication:
891 intptr = &options->password_authentication;
892 goto parse_flag;
893
Damien Miller874d77b2000-10-14 16:23:11 +1100894 case oKbdInteractiveAuthentication:
895 intptr = &options->kbd_interactive_authentication;
896 goto parse_flag;
897
898 case oKbdInteractiveDevices:
899 charptr = &options->kbd_interactive_devices;
900 goto parse_string;
901
Damien Miller0bc1bd82000-11-13 22:57:25 +1100902 case oPubkeyAuthentication:
903 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000904 goto parse_flag;
905
Damien Miller95def091999-11-25 00:26:21 +1100906 case oRSAAuthentication:
907 intptr = &options->rsa_authentication;
908 goto parse_flag;
909
910 case oRhostsRSAAuthentication:
911 intptr = &options->rhosts_rsa_authentication;
912 goto parse_flag;
913
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000914 case oHostbasedAuthentication:
915 intptr = &options->hostbased_authentication;
916 goto parse_flag;
917
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000918 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000919 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100920 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000921
Darren Tucker0efd1552003-08-26 11:49:55 +1000922 case oGssAuthentication:
923 intptr = &options->gss_authentication;
924 goto parse_flag;
925
926 case oGssDelegateCreds:
927 intptr = &options->gss_deleg_creds;
928 goto parse_flag;
929
Damien Miller95def091999-11-25 00:26:21 +1100930 case oBatchMode:
931 intptr = &options->batch_mode;
932 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933
Damien Miller95def091999-11-25 00:26:21 +1100934 case oCheckHostIP:
935 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000936 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937
Damien Miller37876e92003-05-15 10:19:46 +1000938 case oVerifyHostKeyDNS:
939 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100940 multistate_ptr = multistate_yesnoask;
941 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000942
Damien Miller95def091999-11-25 00:26:21 +1100943 case oStrictHostKeyChecking:
944 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100945 multistate_ptr = multistate_yesnoask;
946 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947
Damien Miller95def091999-11-25 00:26:21 +1100948 case oCompression:
949 intptr = &options->compression;
950 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller12c150e2003-12-17 16:31:10 +1100952 case oTCPKeepAlive:
953 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100954 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000956 case oNoHostAuthenticationForLocalhost:
957 intptr = &options->no_host_authentication_for_localhost;
958 goto parse_flag;
959
Damien Miller95def091999-11-25 00:26:21 +1100960 case oNumberOfPasswordPrompts:
961 intptr = &options->number_of_password_prompts;
962 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
Damien Miller95def091999-11-25 00:26:21 +1100964 case oCompressionLevel:
965 intptr = &options->compression_level;
966 goto parse_int;
967
Damien Millera5539d22003-04-09 20:50:06 +1000968 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000969 arg = strdelim(&s);
970 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000971 fatal("%.200s line %d: Missing argument.", filename,
972 linenum);
973 if (strcmp(arg, "default") == 0) {
974 val64 = 0;
975 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000976 if (scan_scaled(arg, &val64) == -1)
977 fatal("%.200s line %d: Bad number '%s': %s",
978 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000979 if (val64 != 0 && val64 < 16)
980 fatal("%.200s line %d: RekeyLimit too small",
981 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000982 }
Damien Miller3dff1762008-02-10 22:25:52 +1100983 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000984 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000985 if (s != NULL) { /* optional rekey interval present */
986 if (strcmp(s, "none") == 0) {
987 (void)strdelim(&s); /* discard */
988 break;
989 }
990 intptr = &options->rekey_interval;
991 goto parse_time;
992 }
Damien Millera5539d22003-04-09 20:50:06 +1000993 break;
994
Damien Miller95def091999-11-25 00:26:21 +1100995 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000996 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000997 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100998 fatal("%.200s line %d: Missing argument.", filename, linenum);
999 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001000 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001001 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001002 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001003 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001004 add_identity_file(options, NULL,
1005 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001006 }
1007 break;
1008
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001009 case oCertificateFile:
1010 arg = strdelim(&s);
1011 if (!arg || *arg == '\0')
1012 fatal("%.200s line %d: Missing argument.",
1013 filename, linenum);
1014 if (*activep) {
1015 intptr = &options->num_certificate_files;
1016 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1017 fatal("%.200s line %d: Too many certificate "
1018 "files specified (max %d).",
1019 filename, linenum,
1020 SSH_MAX_CERTIFICATE_FILES);
1021 }
1022 add_certificate_file(options, arg,
1023 flags & SSHCONF_USERCONF);
1024 }
1025 break;
1026
Damien Millerd3a18572000-06-07 19:55:44 +10001027 case oXAuthLocation:
1028 charptr=&options->xauth_location;
1029 goto parse_string;
1030
Damien Miller95def091999-11-25 00:26:21 +11001031 case oUser:
1032 charptr = &options->user;
1033parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001034 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001035 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001036 fatal("%.200s line %d: Missing argument.",
1037 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001038 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001039 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001040 break;
1041
1042 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001043 cpptr = (char **)&options->system_hostfiles;
1044 uintptr = &options->num_system_hostfiles;
1045 max_entries = SSH_MAX_HOSTS_FILES;
1046parse_char_array:
1047 if (*activep && *uintptr == 0) {
1048 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1049 if ((*uintptr) >= max_entries)
1050 fatal("%s line %d: "
1051 "too many authorized keys files.",
1052 filename, linenum);
1053 cpptr[(*uintptr)++] = xstrdup(arg);
1054 }
1055 }
1056 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001057
1058 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001059 cpptr = (char **)&options->user_hostfiles;
1060 uintptr = &options->num_user_hostfiles;
1061 max_entries = SSH_MAX_HOSTS_FILES;
1062 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001063
Damien Miller95def091999-11-25 00:26:21 +11001064 case oHostName:
1065 charptr = &options->hostname;
1066 goto parse_string;
1067
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001068 case oHostKeyAlias:
1069 charptr = &options->host_key_alias;
1070 goto parse_string;
1071
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001072 case oPreferredAuthentications:
1073 charptr = &options->preferred_authentications;
1074 goto parse_string;
1075
Ben Lindstrome0f88042001-04-30 13:06:24 +00001076 case oBindAddress:
1077 charptr = &options->bind_address;
1078 goto parse_string;
1079
Damien Miller7ea845e2010-02-12 09:21:02 +11001080 case oPKCS11Provider:
1081 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001082 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001083
Damien Miller95def091999-11-25 00:26:21 +11001084 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001085 charptr = &options->proxy_command;
1086parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001087 if (s == NULL)
1088 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001089 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001090 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001091 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001092 return 0;
1093
1094 case oPort:
1095 intptr = &options->port;
1096parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001097 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001098 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001099 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001100 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001101 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001102
1103 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001104 value = strtol(arg, &endofnumber, 0);
1105 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001106 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001107 if (*activep && *intptr == -1)
1108 *intptr = value;
1109 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110
Damien Miller95def091999-11-25 00:26:21 +11001111 case oConnectionAttempts:
1112 intptr = &options->connection_attempts;
1113 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001114
Damien Miller95def091999-11-25 00:26:21 +11001115 case oCipher:
1116 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001117 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001118 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001119 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001120 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001121 if (value == -1)
1122 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001123 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001124 if (*activep && *intptr == -1)
1125 *intptr = value;
1126 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
Damien Miller78928792000-04-12 20:17:38 +10001128 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001129 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001130 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001131 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001132 if (!ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001133 fatal("%.200s line %d: Bad SSH2 cipher 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 && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001136 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001137 break;
1138
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001139 case oMacs:
1140 arg = strdelim(&s);
1141 if (!arg || *arg == '\0')
1142 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001143 if (!mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001144 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001145 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001146 if (*activep && options->macs == NULL)
1147 options->macs = xstrdup(arg);
1148 break;
1149
Damien Millerd5f62bf2010-09-24 22:11:14 +10001150 case oKexAlgorithms:
1151 arg = strdelim(&s);
1152 if (!arg || *arg == '\0')
1153 fatal("%.200s line %d: Missing argument.",
1154 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001155 if (!kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001156 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1157 filename, linenum, arg ? arg : "<NONE>");
1158 if (*activep && options->kex_algorithms == NULL)
1159 options->kex_algorithms = xstrdup(arg);
1160 break;
1161
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001162 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001163 charptr = &options->hostkeyalgorithms;
1164parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001165 arg = strdelim(&s);
1166 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001167 fatal("%.200s line %d: Missing argument.",
1168 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001169 if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001170 fatal("%s line %d: Bad key types '%s'.",
1171 filename, linenum, arg ? arg : "<NONE>");
1172 if (*activep && *charptr == NULL)
1173 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001174 break;
1175
Damien Miller78928792000-04-12 20:17:38 +10001176 case oProtocol:
1177 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001178 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001179 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001180 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001181 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001182 if (value == SSH_PROTO_UNKNOWN)
1183 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001184 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001185 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1186 *intptr = value;
1187 break;
1188
Damien Miller95def091999-11-25 00:26:21 +11001189 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001190 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001191 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001192 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001193 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001194 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001195 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001196 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1197 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001198 break;
1199
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001200 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001201 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001202 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001203 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001204 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001205 fatal("%.200s line %d: Missing port argument.",
1206 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001207
Damien Millera699d952008-11-03 19:27:34 +11001208 if (opcode == oLocalForward ||
1209 opcode == oRemoteForward) {
1210 arg2 = strdelim(&s);
1211 if (arg2 == NULL || *arg2 == '\0')
1212 fatal("%.200s line %d: Missing target argument.",
1213 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001214
Damien Millera699d952008-11-03 19:27:34 +11001215 /* construct a string for parse_forward */
1216 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1217 } else if (opcode == oDynamicForward) {
1218 strlcpy(fwdarg, arg, sizeof(fwdarg));
1219 }
1220
1221 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001222 opcode == oDynamicForward ? 1 : 0,
1223 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001224 fatal("%.200s line %d: Bad forwarding specification.",
1225 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001226
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001227 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001228 if (opcode == oLocalForward ||
1229 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001230 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001231 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001232 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001233 }
Damien Miller95def091999-11-25 00:26:21 +11001234 break;
1235
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001236 case oClearAllForwardings:
1237 intptr = &options->clear_forwardings;
1238 goto parse_flag;
1239
Damien Miller95def091999-11-25 00:26:21 +11001240 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001241 if (cmdline)
1242 fatal("Host directive not supported as a command-line "
1243 "option");
Damien Miller95def091999-11-25 00:26:21 +11001244 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001245 arg2 = NULL;
1246 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1247 negated = *arg == '!';
1248 if (negated)
1249 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001250 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001251 if (negated) {
1252 debug("%.200s line %d: Skipping Host "
1253 "block because of negated match "
1254 "for %.100s", filename, linenum,
1255 arg);
1256 *activep = 0;
1257 break;
1258 }
1259 if (!*activep)
1260 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001261 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001262 }
Damien Millerfe924212011-05-15 08:44:45 +10001263 }
1264 if (*activep)
1265 debug("%.200s line %d: Applying options for %.100s",
1266 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001267 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001268 return 0;
1269
Damien Miller194fd902013-10-15 12:13:05 +11001270 case oMatch:
1271 if (cmdline)
1272 fatal("Host directive not supported as a command-line "
1273 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001274 value = match_cfg_line(options, &s, pw, host, original_host,
1275 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001276 if (value < 0)
1277 fatal("%.200s line %d: Bad Match condition", filename,
1278 linenum);
1279 *activep = value;
1280 break;
1281
Damien Miller95def091999-11-25 00:26:21 +11001282 case oEscapeChar:
1283 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001284 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001285 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001286 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001287 if (strcmp(arg, "none") == 0)
1288 value = SSH_ESCAPECHAR_NONE;
1289 else if (arg[1] == '\0')
1290 value = (u_char) arg[0];
1291 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001292 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1293 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001294 else {
1295 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001296 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001297 /* NOTREACHED */
1298 value = 0; /* Avoid compiler warning. */
1299 }
1300 if (*activep && *intptr == -1)
1301 *intptr = value;
1302 break;
1303
Damien Miller20a8f972003-05-18 20:50:30 +10001304 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001305 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001306 multistate_ptr = multistate_addressfamily;
1307 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001308
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001309 case oEnableSSHKeysign:
1310 intptr = &options->enable_ssh_keysign;
1311 goto parse_flag;
1312
Damien Millerbd394c32004-03-08 23:12:36 +11001313 case oIdentitiesOnly:
1314 intptr = &options->identities_only;
1315 goto parse_flag;
1316
Damien Miller509b0102003-12-17 16:33:10 +11001317 case oServerAliveInterval:
1318 intptr = &options->server_alive_interval;
1319 goto parse_time;
1320
1321 case oServerAliveCountMax:
1322 intptr = &options->server_alive_count_max;
1323 goto parse_int;
1324
Darren Tucker46bc0752004-05-02 22:11:30 +10001325 case oSendEnv:
1326 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1327 if (strchr(arg, '=') != NULL)
1328 fatal("%s line %d: Invalid environment name.",
1329 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001330 if (!*activep)
1331 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001332 if (options->num_send_env >= MAX_SEND_ENV)
1333 fatal("%s line %d: too many send env.",
1334 filename, linenum);
1335 options->send_env[options->num_send_env++] =
1336 xstrdup(arg);
1337 }
1338 break;
1339
Damien Miller0e220db2004-06-15 10:34:08 +10001340 case oControlPath:
1341 charptr = &options->control_path;
1342 goto parse_string;
1343
1344 case oControlMaster:
1345 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001346 multistate_ptr = multistate_controlmaster;
1347 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001348
Damien Millere11e1ea2010-08-03 16:04:46 +10001349 case oControlPersist:
1350 /* no/false/yes/true, or a time spec */
1351 intptr = &options->control_persist;
1352 arg = strdelim(&s);
1353 if (!arg || *arg == '\0')
1354 fatal("%.200s line %d: Missing ControlPersist"
1355 " argument.", filename, linenum);
1356 value = 0;
1357 value2 = 0; /* timeout */
1358 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1359 value = 0;
1360 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1361 value = 1;
1362 else if ((value2 = convtime(arg)) >= 0)
1363 value = 1;
1364 else
1365 fatal("%.200s line %d: Bad ControlPersist argument.",
1366 filename, linenum);
1367 if (*activep && *intptr == -1) {
1368 *intptr = value;
1369 options->control_persist_timeout = value2;
1370 }
1371 break;
1372
Damien Millere1776152005-03-01 21:47:37 +11001373 case oHashKnownHosts:
1374 intptr = &options->hash_known_hosts;
1375 goto parse_flag;
1376
Damien Millerd27b9472005-12-13 19:29:02 +11001377 case oTunnel:
1378 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001379 multistate_ptr = multistate_tunnel;
1380 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001381
1382 case oTunnelDevice:
1383 arg = strdelim(&s);
1384 if (!arg || *arg == '\0')
1385 fatal("%.200s line %d: Missing argument.", filename, linenum);
1386 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001387 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001388 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1389 if (*activep) {
1390 options->tun_local = value;
1391 options->tun_remote = value2;
1392 }
1393 break;
1394
1395 case oLocalCommand:
1396 charptr = &options->local_command;
1397 goto parse_command;
1398
1399 case oPermitLocalCommand:
1400 intptr = &options->permit_local_command;
1401 goto parse_flag;
1402
Damien Miller10288242008-06-30 00:04:03 +10001403 case oVisualHostKey:
1404 intptr = &options->visual_host_key;
1405 goto parse_flag;
1406
Damien Miller0dac6fb2010-11-20 15:19:38 +11001407 case oIPQoS:
1408 arg = strdelim(&s);
1409 if ((value = parse_ipqos(arg)) == -1)
1410 fatal("%s line %d: Bad IPQoS value: %s",
1411 filename, linenum, arg);
1412 arg = strdelim(&s);
1413 if (arg == NULL)
1414 value2 = value;
1415 else if ((value2 = parse_ipqos(arg)) == -1)
1416 fatal("%s line %d: Bad IPQoS value: %s",
1417 filename, linenum, arg);
1418 if (*activep) {
1419 options->ip_qos_interactive = value;
1420 options->ip_qos_bulk = value2;
1421 }
1422 break;
1423
Damien Miller21771e22011-05-15 08:45:50 +10001424 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001425 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001426 multistate_ptr = multistate_requesttty;
1427 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001428
Darren Tucker07636982013-05-16 20:30:03 +10001429 case oIgnoreUnknown:
1430 charptr = &options->ignored_unknown;
1431 goto parse_string;
1432
Damien Miller1262b662013-08-21 02:44:24 +10001433 case oProxyUseFdpass:
1434 intptr = &options->proxy_use_fdpass;
1435 goto parse_flag;
1436
Damien Miller0faf7472013-10-17 11:47:23 +11001437 case oCanonicalDomains:
1438 value = options->num_canonical_domains != 0;
1439 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1440 valid_domain(arg, filename, linenum);
1441 if (!*activep || value)
1442 continue;
1443 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1444 fatal("%s line %d: too many hostname suffixes.",
1445 filename, linenum);
1446 options->canonical_domains[
1447 options->num_canonical_domains++] = xstrdup(arg);
1448 }
1449 break;
1450
Damien Miller38505592013-10-17 11:48:13 +11001451 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001452 value = options->num_permitted_cnames != 0;
1453 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1454 /* Either '*' for everything or 'list:list' */
1455 if (strcmp(arg, "*") == 0)
1456 arg2 = arg;
1457 else {
1458 lowercase(arg);
1459 if ((arg2 = strchr(arg, ':')) == NULL ||
1460 arg2[1] == '\0') {
1461 fatal("%s line %d: "
1462 "Invalid permitted CNAME \"%s\"",
1463 filename, linenum, arg);
1464 }
1465 *arg2 = '\0';
1466 arg2++;
1467 }
1468 if (!*activep || value)
1469 continue;
1470 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1471 fatal("%s line %d: too many permitted CNAMEs.",
1472 filename, linenum);
1473 cname = options->permitted_cnames +
1474 options->num_permitted_cnames++;
1475 cname->source_list = xstrdup(arg);
1476 cname->target_list = xstrdup(arg2);
1477 }
1478 break;
1479
Damien Miller38505592013-10-17 11:48:13 +11001480 case oCanonicalizeHostname:
1481 intptr = &options->canonicalize_hostname;
1482 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001483 goto parse_multistate;
1484
Damien Miller38505592013-10-17 11:48:13 +11001485 case oCanonicalizeMaxDots:
1486 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001487 goto parse_int;
1488
Damien Miller38505592013-10-17 11:48:13 +11001489 case oCanonicalizeFallbackLocal:
1490 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001491 goto parse_flag;
1492
Damien Miller7acefbb2014-07-18 14:11:24 +10001493 case oStreamLocalBindMask:
1494 arg = strdelim(&s);
1495 if (!arg || *arg == '\0')
1496 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1497 /* Parse mode in octal format */
1498 value = strtol(arg, &endofnumber, 8);
1499 if (arg == endofnumber || value < 0 || value > 0777)
1500 fatal("%.200s line %d: Bad mask.", filename, linenum);
1501 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1502 break;
1503
1504 case oStreamLocalBindUnlink:
1505 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1506 goto parse_flag;
1507
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001508 case oRevokedHostKeys:
1509 charptr = &options->revoked_host_keys;
1510 goto parse_string;
1511
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001512 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001513 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001514 arg = strdelim(&s);
1515 if (!arg || *arg == '\0')
1516 fatal("%.200s line %d: Missing argument.",
1517 filename, linenum);
1518 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1519 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1520 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001521 if (*activep && *intptr == -1)
1522 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001523 break;
1524
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001525 case oUpdateHostkeys:
1526 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001527 multistate_ptr = multistate_yesnoask;
1528 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001529
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001530 case oHostbasedKeyTypes:
1531 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001532 goto parse_keytypes;
1533
1534 case oPubkeyAcceptedKeyTypes:
1535 charptr = &options->pubkey_key_types;
1536 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001537
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001538 case oAddKeysToAgent:
1539 intptr = &options->add_keys_to_agent;
1540 multistate_ptr = multistate_yesnoaskconfirm;
1541 goto parse_multistate;
1542
Ben Lindstrom4daea862002-06-09 20:04:02 +00001543 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001544 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001545 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001546 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001547
Damien Millerf9b3feb2003-05-16 11:38:32 +10001548 case oUnsupported:
1549 error("%s line %d: Unsupported option \"%s\"",
1550 filename, linenum, keyword);
1551 return 0;
1552
Damien Miller95def091999-11-25 00:26:21 +11001553 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001554 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001555 }
1556
1557 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001558 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001559 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001560 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001561 }
Damien Miller95def091999-11-25 00:26:21 +11001562 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001563}
1564
1565
Damien Miller5428f641999-11-25 11:54:57 +11001566/*
1567 * Reads the config file and modifies the options accordingly. Options
1568 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001569 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001570 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001571
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001572int
Damien Miller194fd902013-10-15 12:13:05 +11001573read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001574 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575{
Damien Miller95def091999-11-25 00:26:21 +11001576 FILE *f;
1577 char line[1024];
1578 int active, linenum;
1579 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580
Damien Miller57a44762004-04-20 20:11:57 +10001581 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001582 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Darren Tuckeraefa3682013-04-05 11:18:35 +11001584 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001585 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001586
Damien Miller33793852004-06-15 10:27:55 +10001587 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001588 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001589 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001590 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001591 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001592 }
1593
Damien Miller95def091999-11-25 00:26:21 +11001594 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001595
Damien Miller5428f641999-11-25 11:54:57 +11001596 /*
1597 * Mark that we are now processing the options. This flag is turned
1598 * on/off by Host specifications.
1599 */
Damien Miller95def091999-11-25 00:26:21 +11001600 active = 1;
1601 linenum = 0;
1602 while (fgets(line, sizeof(line), f)) {
1603 /* Update line number counter. */
1604 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001605 if (process_config_line(options, pw, host, original_host,
1606 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001607 bad_options++;
1608 }
1609 fclose(f);
1610 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001611 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001612 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001613 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001614}
1615
Damien Miller13f97b22014-02-24 15:57:55 +11001616/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1617int
1618option_clear_or_none(const char *o)
1619{
1620 return o == NULL || strcasecmp(o, "none") == 0;
1621}
1622
Damien Miller5428f641999-11-25 11:54:57 +11001623/*
1624 * Initializes options to special values that indicate that they have not yet
1625 * been set. Read_config_file will only set options with this value. Options
1626 * are processed in the following order: command line, user config file,
1627 * system config file. Last, fill_default_options is called.
1628 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001629
Damien Miller4af51302000-04-16 11:18:38 +10001630void
Damien Miller95def091999-11-25 00:26:21 +11001631initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632{
Damien Miller95def091999-11-25 00:26:21 +11001633 memset(options, 'X', sizeof(*options));
1634 options->forward_agent = -1;
1635 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001636 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001637 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001638 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001639 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001640 options->fwd_opts.gateway_ports = -1;
1641 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1642 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001643 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001644 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001645 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001646 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001647 options->gss_authentication = -1;
1648 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001649 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001650 options->kbd_interactive_authentication = -1;
1651 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001652 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001653 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001654 options->batch_mode = -1;
1655 options->check_host_ip = -1;
1656 options->strict_host_key_checking = -1;
1657 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001658 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001659 options->compression_level = -1;
1660 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001661 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001662 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001663 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001664 options->number_of_password_prompts = -1;
1665 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001666 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001667 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001668 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001669 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001670 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001671 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001672 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001673 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001674 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001675 options->proxy_command = NULL;
1676 options->user = NULL;
1677 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001678 options->num_system_hostfiles = 0;
1679 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001680 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001681 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001682 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001683 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001684 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001685 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001686 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001687 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001688 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001689 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001690 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001691 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001692 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001693 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001694 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001695 options->server_alive_interval = -1;
1696 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001697 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001698 options->control_path = NULL;
1699 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001700 options->control_persist = -1;
1701 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001702 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001703 options->tun_open = -1;
1704 options->tun_local = -1;
1705 options->tun_remote = -1;
1706 options->local_command = NULL;
1707 options->permit_local_command = -1;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001708 options->add_keys_to_agent = -1;
Damien Miller10288242008-06-30 00:04:03 +10001709 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001710 options->ip_qos_interactive = -1;
1711 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001712 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001713 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001714 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001715 options->num_canonical_domains = 0;
1716 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001717 options->canonicalize_max_dots = -1;
1718 options->canonicalize_fallback_local = -1;
1719 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001720 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001721 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001722 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001723 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001724 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001725}
1726
Damien Miller5428f641999-11-25 11:54:57 +11001727/*
Damien Miller13f97b22014-02-24 15:57:55 +11001728 * A petite version of fill_default_options() that just fills the options
1729 * needed for hostname canonicalization to proceed.
1730 */
1731void
1732fill_default_options_for_canonicalization(Options *options)
1733{
1734 if (options->canonicalize_max_dots == -1)
1735 options->canonicalize_max_dots = 1;
1736 if (options->canonicalize_fallback_local == -1)
1737 options->canonicalize_fallback_local = 1;
1738 if (options->canonicalize_hostname == -1)
1739 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1740}
1741
1742/*
Damien Miller5428f641999-11-25 11:54:57 +11001743 * Called after processing other sources of option data, this fills those
1744 * options for which no value has been specified with their default values.
1745 */
Damien Miller4af51302000-04-16 11:18:38 +10001746void
Damien Miller95def091999-11-25 00:26:21 +11001747fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748{
Damien Miller95def091999-11-25 00:26:21 +11001749 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001750 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001751 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001752 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001753 if (options->forward_x11_trusted == -1)
1754 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001755 if (options->forward_x11_timeout == -1)
1756 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001757 if (options->exit_on_forward_failure == -1)
1758 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001759 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001760 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001761 if (options->fwd_opts.gateway_ports == -1)
1762 options->fwd_opts.gateway_ports = 0;
1763 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1764 options->fwd_opts.streamlocal_bind_mask = 0177;
1765 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1766 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001767 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001768 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001769 if (options->rsa_authentication == -1)
1770 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001771 if (options->pubkey_authentication == -1)
1772 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001773 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001774 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001775 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001776 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001777 if (options->gss_deleg_creds == -1)
1778 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001779 if (options->password_authentication == -1)
1780 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001781 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001782 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001783 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001784 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001785 if (options->hostbased_authentication == -1)
1786 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001787 if (options->batch_mode == -1)
1788 options->batch_mode = 0;
1789 if (options->check_host_ip == -1)
1790 options->check_host_ip = 1;
1791 if (options->strict_host_key_checking == -1)
1792 options->strict_host_key_checking = 2; /* 2 is default */
1793 if (options->compression == -1)
1794 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001795 if (options->tcp_keep_alive == -1)
1796 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001797 if (options->compression_level == -1)
1798 options->compression_level = 6;
1799 if (options->port == -1)
1800 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001801 if (options->address_family == -1)
1802 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001803 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001804 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001805 if (options->number_of_password_prompts == -1)
1806 options->number_of_password_prompts = 3;
1807 /* Selected in ssh_login(). */
1808 if (options->cipher == -1)
1809 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001810 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001811 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001812 options->protocol = SSH_PROTO_2;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001813 if (options->add_keys_to_agent == -1)
1814 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001815 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001816 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001817 add_identity_file(options, "~/",
1818 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001819 }
1820 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001821 add_identity_file(options, "~/",
1822 _PATH_SSH_CLIENT_ID_RSA, 0);
1823 add_identity_file(options, "~/",
1824 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001825#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001826 add_identity_file(options, "~/",
1827 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001828#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001829 add_identity_file(options, "~/",
1830 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001831 }
Damien Millereba71ba2000-04-29 23:57:08 +10001832 }
Damien Miller95def091999-11-25 00:26:21 +11001833 if (options->escape_char == -1)
1834 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001835 if (options->num_system_hostfiles == 0) {
1836 options->system_hostfiles[options->num_system_hostfiles++] =
1837 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1838 options->system_hostfiles[options->num_system_hostfiles++] =
1839 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1840 }
1841 if (options->num_user_hostfiles == 0) {
1842 options->user_hostfiles[options->num_user_hostfiles++] =
1843 xstrdup(_PATH_SSH_USER_HOSTFILE);
1844 options->user_hostfiles[options->num_user_hostfiles++] =
1845 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1846 }
Damien Millerfcd93202002-02-05 12:26:34 +11001847 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001848 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001849 if (options->clear_forwardings == 1)
1850 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001851 if (options->no_host_authentication_for_localhost == - 1)
1852 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001853 if (options->identities_only == -1)
1854 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001855 if (options->enable_ssh_keysign == -1)
1856 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001857 if (options->rekey_limit == -1)
1858 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001859 if (options->rekey_interval == -1)
1860 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001861 if (options->verify_host_key_dns == -1)
1862 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001863 if (options->server_alive_interval == -1)
1864 options->server_alive_interval = 0;
1865 if (options->server_alive_count_max == -1)
1866 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001867 if (options->control_master == -1)
1868 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001869 if (options->control_persist == -1) {
1870 options->control_persist = 0;
1871 options->control_persist_timeout = 0;
1872 }
Damien Millere1776152005-03-01 21:47:37 +11001873 if (options->hash_known_hosts == -1)
1874 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001875 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001876 options->tun_open = SSH_TUNMODE_NO;
1877 if (options->tun_local == -1)
1878 options->tun_local = SSH_TUNID_ANY;
1879 if (options->tun_remote == -1)
1880 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001881 if (options->permit_local_command == -1)
1882 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10001883 if (options->visual_host_key == -1)
1884 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001885 if (options->ip_qos_interactive == -1)
1886 options->ip_qos_interactive = IPTOS_LOWDELAY;
1887 if (options->ip_qos_bulk == -1)
1888 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001889 if (options->request_tty == -1)
1890 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001891 if (options->proxy_use_fdpass == -1)
1892 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001893 if (options->canonicalize_max_dots == -1)
1894 options->canonicalize_max_dots = 1;
1895 if (options->canonicalize_fallback_local == -1)
1896 options->canonicalize_fallback_local = 1;
1897 if (options->canonicalize_hostname == -1)
1898 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001899 if (options->fingerprint_hash == -1)
1900 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001901 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00001902 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001903 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
1904 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
1905 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
1906 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1907 &options->hostbased_key_types) != 0 ||
1908 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1909 &options->pubkey_key_types) != 0)
1910 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001911
Damien Millere9fc72e2013-10-15 12:14:12 +11001912#define CLEAR_ON_NONE(v) \
1913 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001914 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001915 free(v); \
1916 v = NULL; \
1917 } \
1918 } while(0)
1919 CLEAR_ON_NONE(options->local_command);
1920 CLEAR_ON_NONE(options->proxy_command);
1921 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001922 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001923 /* options->user will be set in the main program if appropriate */
1924 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001925 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001926 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001927}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001928
Damien Miller7acefbb2014-07-18 14:11:24 +10001929struct fwdarg {
1930 char *arg;
1931 int ispath;
1932};
1933
1934/*
1935 * parse_fwd_field
1936 * parses the next field in a port forwarding specification.
1937 * sets fwd to the parsed field and advances p past the colon
1938 * or sets it to NULL at end of string.
1939 * returns 0 on success, else non-zero.
1940 */
1941static int
1942parse_fwd_field(char **p, struct fwdarg *fwd)
1943{
1944 char *ep, *cp = *p;
1945 int ispath = 0;
1946
1947 if (*cp == '\0') {
1948 *p = NULL;
1949 return -1; /* end of string */
1950 }
1951
1952 /*
1953 * A field escaped with square brackets is used literally.
1954 * XXX - allow ']' to be escaped via backslash?
1955 */
1956 if (*cp == '[') {
1957 /* find matching ']' */
1958 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1959 if (*ep == '/')
1960 ispath = 1;
1961 }
1962 /* no matching ']' or not at end of field. */
1963 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1964 return -1;
1965 /* NUL terminate the field and advance p past the colon */
1966 *ep++ = '\0';
1967 if (*ep != '\0')
1968 *ep++ = '\0';
1969 fwd->arg = cp + 1;
1970 fwd->ispath = ispath;
1971 *p = ep;
1972 return 0;
1973 }
1974
1975 for (cp = *p; *cp != '\0'; cp++) {
1976 switch (*cp) {
1977 case '\\':
1978 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00001979 if (*cp == '\0')
1980 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10001981 break;
1982 case '/':
1983 ispath = 1;
1984 break;
1985 case ':':
1986 *cp++ = '\0';
1987 goto done;
1988 }
1989 }
1990done:
1991 fwd->arg = *p;
1992 fwd->ispath = ispath;
1993 *p = cp;
1994 return 0;
1995}
1996
Damien Millerf91ee4c2005-03-01 21:24:33 +11001997/*
1998 * parse_forward
1999 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002000 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002001 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2002 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002003 * dynamicfwd == 1
2004 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002005 * returns number of arguments parsed or zero on error
2006 */
2007int
Damien Miller7acefbb2014-07-18 14:11:24 +10002008parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002009{
Damien Miller7acefbb2014-07-18 14:11:24 +10002010 struct fwdarg fwdargs[4];
2011 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002012 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002013
Damien Miller7acefbb2014-07-18 14:11:24 +10002014 memset(fwd, 0, sizeof(*fwd));
2015 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002016
2017 cp = p = xstrdup(fwdspec);
2018
2019 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002020 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002021 cp++;
2022
Damien Miller7acefbb2014-07-18 14:11:24 +10002023 for (i = 0; i < 4; ++i) {
2024 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002025 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002026 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002027
Damien Millerf4b39532008-11-03 19:28:21 +11002028 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002029 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002030 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002031 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002032
2033 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002034 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002035 if (fwdargs[0].ispath) {
2036 fwd->listen_path = xstrdup(fwdargs[0].arg);
2037 fwd->listen_port = PORT_STREAMLOCAL;
2038 } else {
2039 fwd->listen_host = NULL;
2040 fwd->listen_port = a2port(fwdargs[0].arg);
2041 }
Damien Millera699d952008-11-03 19:27:34 +11002042 fwd->connect_host = xstrdup("socks");
2043 break;
2044
2045 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002046 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2047 fwd->listen_path = xstrdup(fwdargs[0].arg);
2048 fwd->listen_port = PORT_STREAMLOCAL;
2049 fwd->connect_path = xstrdup(fwdargs[1].arg);
2050 fwd->connect_port = PORT_STREAMLOCAL;
2051 } else if (fwdargs[1].ispath) {
2052 fwd->listen_host = NULL;
2053 fwd->listen_port = a2port(fwdargs[0].arg);
2054 fwd->connect_path = xstrdup(fwdargs[1].arg);
2055 fwd->connect_port = PORT_STREAMLOCAL;
2056 } else {
2057 fwd->listen_host = xstrdup(fwdargs[0].arg);
2058 fwd->listen_port = a2port(fwdargs[1].arg);
2059 fwd->connect_host = xstrdup("socks");
2060 }
Damien Millera699d952008-11-03 19:27:34 +11002061 break;
2062
Damien Millerf91ee4c2005-03-01 21:24:33 +11002063 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002064 if (fwdargs[0].ispath) {
2065 fwd->listen_path = xstrdup(fwdargs[0].arg);
2066 fwd->listen_port = PORT_STREAMLOCAL;
2067 fwd->connect_host = xstrdup(fwdargs[1].arg);
2068 fwd->connect_port = a2port(fwdargs[2].arg);
2069 } else if (fwdargs[2].ispath) {
2070 fwd->listen_host = xstrdup(fwdargs[0].arg);
2071 fwd->listen_port = a2port(fwdargs[1].arg);
2072 fwd->connect_path = xstrdup(fwdargs[2].arg);
2073 fwd->connect_port = PORT_STREAMLOCAL;
2074 } else {
2075 fwd->listen_host = NULL;
2076 fwd->listen_port = a2port(fwdargs[0].arg);
2077 fwd->connect_host = xstrdup(fwdargs[1].arg);
2078 fwd->connect_port = a2port(fwdargs[2].arg);
2079 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002080 break;
2081
2082 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002083 fwd->listen_host = xstrdup(fwdargs[0].arg);
2084 fwd->listen_port = a2port(fwdargs[1].arg);
2085 fwd->connect_host = xstrdup(fwdargs[2].arg);
2086 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002087 break;
2088 default:
2089 i = 0; /* failure */
2090 }
2091
Darren Tuckera627d422013-06-02 07:31:17 +10002092 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002093
Damien Millera699d952008-11-03 19:27:34 +11002094 if (dynamicfwd) {
2095 if (!(i == 1 || i == 2))
2096 goto fail_free;
2097 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002098 if (!(i == 3 || i == 4)) {
2099 if (fwd->connect_path == NULL &&
2100 fwd->listen_path == NULL)
2101 goto fail_free;
2102 }
2103 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002104 goto fail_free;
2105 }
2106
Damien Miller7acefbb2014-07-18 14:11:24 +10002107 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2108 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002109 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002110 if (fwd->connect_host != NULL &&
2111 strlen(fwd->connect_host) >= NI_MAXHOST)
2112 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002113 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2114 if (fwd->connect_path != NULL &&
2115 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2116 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002117 if (fwd->listen_host != NULL &&
2118 strlen(fwd->listen_host) >= NI_MAXHOST)
2119 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002120 if (fwd->listen_path != NULL &&
2121 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2122 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002123
2124 return (i);
2125
2126 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002127 free(fwd->connect_host);
2128 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002129 free(fwd->connect_path);
2130 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002131 free(fwd->listen_host);
2132 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002133 free(fwd->listen_path);
2134 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002135 return (0);
2136}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002137
2138/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2139static const char *
2140fmt_multistate_int(int val, const struct multistate *m)
2141{
2142 u_int i;
2143
2144 for (i = 0; m[i].key != NULL; i++) {
2145 if (m[i].value == val)
2146 return m[i].key;
2147 }
2148 return "UNKNOWN";
2149}
2150
2151static const char *
2152fmt_intarg(OpCodes code, int val)
2153{
2154 if (val == -1)
2155 return "unset";
2156 switch (code) {
2157 case oAddressFamily:
2158 return fmt_multistate_int(val, multistate_addressfamily);
2159 case oVerifyHostKeyDNS:
2160 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002161 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002162 return fmt_multistate_int(val, multistate_yesnoask);
2163 case oControlMaster:
2164 return fmt_multistate_int(val, multistate_controlmaster);
2165 case oTunnel:
2166 return fmt_multistate_int(val, multistate_tunnel);
2167 case oRequestTTY:
2168 return fmt_multistate_int(val, multistate_requesttty);
2169 case oCanonicalizeHostname:
2170 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002171 case oFingerprintHash:
2172 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002173 case oProtocol:
2174 switch (val) {
2175 case SSH_PROTO_1:
2176 return "1";
2177 case SSH_PROTO_2:
2178 return "2";
2179 case (SSH_PROTO_1|SSH_PROTO_2):
2180 return "2,1";
2181 default:
2182 return "UNKNOWN";
2183 }
2184 default:
2185 switch (val) {
2186 case 0:
2187 return "no";
2188 case 1:
2189 return "yes";
2190 default:
2191 return "UNKNOWN";
2192 }
2193 }
2194}
2195
2196static const char *
2197lookup_opcode_name(OpCodes code)
2198{
2199 u_int i;
2200
2201 for (i = 0; keywords[i].name != NULL; i++)
2202 if (keywords[i].opcode == code)
2203 return(keywords[i].name);
2204 return "UNKNOWN";
2205}
2206
2207static void
2208dump_cfg_int(OpCodes code, int val)
2209{
2210 printf("%s %d\n", lookup_opcode_name(code), val);
2211}
2212
2213static void
2214dump_cfg_fmtint(OpCodes code, int val)
2215{
2216 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2217}
2218
2219static void
2220dump_cfg_string(OpCodes code, const char *val)
2221{
2222 if (val == NULL)
2223 return;
2224 printf("%s %s\n", lookup_opcode_name(code), val);
2225}
2226
2227static void
2228dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2229{
2230 u_int i;
2231
2232 for (i = 0; i < count; i++)
2233 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2234}
2235
2236static void
2237dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2238{
2239 u_int i;
2240
2241 printf("%s", lookup_opcode_name(code));
2242 for (i = 0; i < count; i++)
2243 printf(" %s", vals[i]);
2244 printf("\n");
2245}
2246
2247static void
2248dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2249{
2250 const struct Forward *fwd;
2251 u_int i;
2252
2253 /* oDynamicForward */
2254 for (i = 0; i < count; i++) {
2255 fwd = &fwds[i];
2256 if (code == oDynamicForward &&
2257 strcmp(fwd->connect_host, "socks") != 0)
2258 continue;
2259 if (code == oLocalForward &&
2260 strcmp(fwd->connect_host, "socks") == 0)
2261 continue;
2262 printf("%s", lookup_opcode_name(code));
2263 if (fwd->listen_port == PORT_STREAMLOCAL)
2264 printf(" %s", fwd->listen_path);
2265 else if (fwd->listen_host == NULL)
2266 printf(" %d", fwd->listen_port);
2267 else {
2268 printf(" [%s]:%d",
2269 fwd->listen_host, fwd->listen_port);
2270 }
2271 if (code != oDynamicForward) {
2272 if (fwd->connect_port == PORT_STREAMLOCAL)
2273 printf(" %s", fwd->connect_path);
2274 else if (fwd->connect_host == NULL)
2275 printf(" %d", fwd->connect_port);
2276 else {
2277 printf(" [%s]:%d",
2278 fwd->connect_host, fwd->connect_port);
2279 }
2280 }
2281 printf("\n");
2282 }
2283}
2284
2285void
2286dump_client_config(Options *o, const char *host)
2287{
2288 int i;
2289 char vbuf[5];
2290
djm@openbsd.org60a92472015-08-21 23:53:08 +00002291 /* This is normally prepared in ssh_kex2 */
2292 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2293 fatal("%s: kex_assemble_names failed", __func__);
2294
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002295 /* Most interesting options first: user, host, port */
2296 dump_cfg_string(oUser, o->user);
2297 dump_cfg_string(oHostName, host);
2298 dump_cfg_int(oPort, o->port);
2299
2300 /* Flag options */
2301 dump_cfg_fmtint(oAddressFamily, o->address_family);
2302 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2303 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2304 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2305 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2306 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2307 dump_cfg_fmtint(oCompression, o->compression);
2308 dump_cfg_fmtint(oControlMaster, o->control_master);
2309 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2310 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002311 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002312 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2313 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2314 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2315 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2316#ifdef GSSAPI
2317 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2318 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2319#endif /* GSSAPI */
2320 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2321 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2322 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2323 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2324 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2325 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2326 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2327 dump_cfg_fmtint(oProtocol, o->protocol);
2328 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2329 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2330 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2331 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2332 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2333 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2334 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2335 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2336 dump_cfg_fmtint(oTunnel, o->tun_open);
2337 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2338 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2339 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002340 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002341
2342 /* Integer options */
2343 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2344 dump_cfg_int(oCompressionLevel, o->compression_level);
2345 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2346 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2347 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2348 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2349 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2350
2351 /* String options */
2352 dump_cfg_string(oBindAddress, o->bind_address);
2353 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2354 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002355 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002356 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002357 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002358 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2359 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2360 dump_cfg_string(oLocalCommand, o->local_command);
2361 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2362 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2363 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2364 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2365 dump_cfg_string(oProxyCommand, o->proxy_command);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002366 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002367 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002368 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002369
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002370 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002371 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2372 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2373 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2374
2375 /* String array options */
2376 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2377 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2378 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2379 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2380 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2381
2382 /* Special cases */
2383
2384 /* oConnectTimeout */
2385 if (o->connection_timeout == -1)
2386 printf("connecttimeout none\n");
2387 else
2388 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2389
2390 /* oTunnelDevice */
2391 printf("tunneldevice");
2392 if (o->tun_local == SSH_TUNID_ANY)
2393 printf(" any");
2394 else
2395 printf(" %d", o->tun_local);
2396 if (o->tun_remote == SSH_TUNID_ANY)
2397 printf(":any");
2398 else
2399 printf(":%d", o->tun_remote);
2400 printf("\n");
2401
2402 /* oCanonicalizePermittedCNAMEs */
2403 if ( o->num_permitted_cnames > 0) {
2404 printf("canonicalizePermittedcnames");
2405 for (i = 0; i < o->num_permitted_cnames; i++) {
2406 printf(" %s:%s", o->permitted_cnames[i].source_list,
2407 o->permitted_cnames[i].target_list);
2408 }
2409 printf("\n");
2410 }
2411
2412 /* oCipher */
2413 if (o->cipher != SSH_CIPHER_NOT_SET)
2414 printf("Cipher %s\n", cipher_name(o->cipher));
2415
2416 /* oControlPersist */
2417 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2418 dump_cfg_fmtint(oControlPersist, o->control_persist);
2419 else
2420 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2421
2422 /* oEscapeChar */
2423 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2424 printf("escapechar none\n");
2425 else {
2426 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2427 printf("escapechar %s\n", vbuf);
2428 }
2429
2430 /* oIPQoS */
2431 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2432 printf("%s\n", iptos2str(o->ip_qos_bulk));
2433
2434 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002435 printf("rekeylimit %llu %d\n",
2436 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002437
2438 /* oStreamLocalBindMask */
2439 printf("streamlocalbindmask 0%o\n",
2440 o->fwd_opts.streamlocal_bind_mask);
2441}