blob: 69d4553af2177284b629dfb178bb116a004e4a75 [file] [log] [blame]
djm@openbsd.org5a622842016-02-08 23:40:12 +00001/* $OpenBSD: readconf.c,v 1.250 2016/02/08 23:40:12 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
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;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000525 if (post_canon) {
526 host = xstrdup(options->hostname);
527 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100528 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100529 host = percent_expand(options->hostname,
530 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000531 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100532 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000533 }
Damien Miller194fd902013-10-15 12:13:05 +1100534
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000535 debug2("checking match for '%s' host %s originally %s",
536 cp, host, original_host);
537 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
538 criteria = NULL;
539 this_result = 1;
540 if ((negate = attrib[0] == '!'))
541 attrib++;
542 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100543 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000544 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100545 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000546 error("%.200s line %d: '%s' cannot be combined "
547 "with other Match attributes",
548 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100549 result = -1;
550 goto out;
551 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000552 if (result)
553 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100554 goto out;
555 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000556 attributes++;
557 if (strcasecmp(attrib, "canonical") == 0) {
558 r = !!post_canon; /* force bitmask member to boolean */
559 if (r == (negate ? 1 : 0))
560 this_result = result = 0;
561 debug3("%.200s line %d: %smatched '%s'",
562 filename, linenum,
563 this_result ? "" : "not ", oattrib);
564 continue;
565 }
566 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100567 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
568 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100569 result = -1;
570 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100571 }
Damien Miller194fd902013-10-15 12:13:05 +1100572 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000573 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000574 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000575 if (r == (negate ? 1 : 0))
576 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100577 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000578 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000579 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000580 if (r == (negate ? 1 : 0))
581 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100582 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000583 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000584 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000585 if (r == (negate ? 1 : 0))
586 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100587 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000588 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000589 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000590 if (r == (negate ? 1 : 0))
591 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100592 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100593 if (gethostname(thishost, sizeof(thishost)) == -1)
594 fatal("gethostname: %s", strerror(errno));
595 strlcpy(shorthost, thishost, sizeof(shorthost));
596 shorthost[strcspn(thishost, ".")] = '\0';
597 snprintf(portstr, sizeof(portstr), "%d", port);
598
599 cmd = percent_expand(arg,
600 "L", shorthost,
601 "d", pw->pw_dir,
602 "h", host,
603 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000604 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100605 "p", portstr,
606 "r", ruser,
607 "u", pw->pw_name,
608 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100609 if (result != 1) {
610 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000611 debug3("%.200s line %d: skipped exec "
612 "\"%.100s\"", filename, linenum, cmd);
613 free(cmd);
614 continue;
Damien Miller06287802014-02-24 15:56:45 +1100615 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000616 r = execute_in_shell(cmd);
617 if (r == -1) {
618 fatal("%.200s line %d: match exec "
619 "'%.100s' error", filename,
620 linenum, cmd);
621 }
622 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100623 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000624 /* Force exit status to boolean */
625 r = r == 0;
626 if (r == (negate ? 1 : 0))
627 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100628 } else {
629 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100630 result = -1;
631 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100632 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000633 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
634 filename, linenum, this_result ? "": "not ",
635 oattrib, criteria);
636 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100637 }
Damien Millercf31f382013-10-24 21:02:56 +1100638 if (attributes == 0) {
639 error("One or more attributes required for Match");
640 result = -1;
641 goto out;
642 }
Damien Miller084bcd22013-10-23 16:30:51 +1100643 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000644 if (result != -1)
645 debug2("match %sfound", result ? "" : "not ");
646 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100647 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100648 return result;
649}
650
Damien Miller0faf7472013-10-17 11:47:23 +1100651/* Check and prepare a domain name: removes trailing '.' and lowercases */
652static void
653valid_domain(char *name, const char *filename, int linenum)
654{
655 size_t i, l = strlen(name);
656 u_char c, last = '\0';
657
658 if (l == 0)
659 fatal("%s line %d: empty hostname suffix", filename, linenum);
660 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
661 fatal("%s line %d: hostname suffix \"%.100s\" "
662 "starts with invalid character", filename, linenum, name);
663 for (i = 0; i < l; i++) {
664 c = tolower((u_char)name[i]);
665 name[i] = (char)c;
666 if (last == '.' && c == '.')
667 fatal("%s line %d: hostname suffix \"%.100s\" contains "
668 "consecutive separators", filename, linenum, name);
669 if (c != '.' && c != '-' && !isalnum(c) &&
670 c != '_') /* technically invalid, but common */
671 fatal("%s line %d: hostname suffix \"%.100s\" contains "
672 "invalid characters", filename, linenum, name);
673 last = c;
674 }
675 if (name[l - 1] == '.')
676 name[l - 1] = '\0';
677}
678
Damien Miller5428f641999-11-25 11:54:57 +1100679/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000680 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100681 */
Damien Miller4af51302000-04-16 11:18:38 +1000682static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000683parse_token(const char *cp, const char *filename, int linenum,
684 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685{
Darren Tucker07636982013-05-16 20:30:03 +1000686 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
Damien Miller95def091999-11-25 00:26:21 +1100688 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000689 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100690 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000691 if (ignored_unknown != NULL &&
692 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000693 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000694 error("%s: line %d: Bad configuration option: %s",
695 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100696 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697}
698
Damien Millere9fc72e2013-10-15 12:14:12 +1100699/* Multistate option parsing */
700struct multistate {
701 char *key;
702 int value;
703};
704static const struct multistate multistate_flag[] = {
705 { "true", 1 },
706 { "false", 0 },
707 { "yes", 1 },
708 { "no", 0 },
709 { NULL, -1 }
710};
711static const struct multistate multistate_yesnoask[] = {
712 { "true", 1 },
713 { "false", 0 },
714 { "yes", 1 },
715 { "no", 0 },
716 { "ask", 2 },
717 { NULL, -1 }
718};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000719static const struct multistate multistate_yesnoaskconfirm[] = {
720 { "true", 1 },
721 { "false", 0 },
722 { "yes", 1 },
723 { "no", 0 },
724 { "ask", 2 },
725 { "confirm", 3 },
726 { NULL, -1 }
727};
Damien Millere9fc72e2013-10-15 12:14:12 +1100728static const struct multistate multistate_addressfamily[] = {
729 { "inet", AF_INET },
730 { "inet6", AF_INET6 },
731 { "any", AF_UNSPEC },
732 { NULL, -1 }
733};
734static const struct multistate multistate_controlmaster[] = {
735 { "true", SSHCTL_MASTER_YES },
736 { "yes", SSHCTL_MASTER_YES },
737 { "false", SSHCTL_MASTER_NO },
738 { "no", SSHCTL_MASTER_NO },
739 { "auto", SSHCTL_MASTER_AUTO },
740 { "ask", SSHCTL_MASTER_ASK },
741 { "autoask", SSHCTL_MASTER_AUTO_ASK },
742 { NULL, -1 }
743};
744static const struct multistate multistate_tunnel[] = {
745 { "ethernet", SSH_TUNMODE_ETHERNET },
746 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
747 { "true", SSH_TUNMODE_DEFAULT },
748 { "yes", SSH_TUNMODE_DEFAULT },
749 { "false", SSH_TUNMODE_NO },
750 { "no", SSH_TUNMODE_NO },
751 { NULL, -1 }
752};
753static const struct multistate multistate_requesttty[] = {
754 { "true", REQUEST_TTY_YES },
755 { "yes", REQUEST_TTY_YES },
756 { "false", REQUEST_TTY_NO },
757 { "no", REQUEST_TTY_NO },
758 { "force", REQUEST_TTY_FORCE },
759 { "auto", REQUEST_TTY_AUTO },
760 { NULL, -1 }
761};
Damien Miller38505592013-10-17 11:48:13 +1100762static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100763 { "true", SSH_CANONICALISE_YES },
764 { "false", SSH_CANONICALISE_NO },
765 { "yes", SSH_CANONICALISE_YES },
766 { "no", SSH_CANONICALISE_NO },
767 { "always", SSH_CANONICALISE_ALWAYS },
768 { NULL, -1 }
769};
Damien Millere9fc72e2013-10-15 12:14:12 +1100770
Damien Miller5428f641999-11-25 11:54:57 +1100771/*
772 * Processes a single option line as used in the configuration files. This
773 * only sets those values that have not already been set.
774 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100775#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100776int
Damien Miller194fd902013-10-15 12:13:05 +1100777process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000778 const char *original_host, char *line, const char *filename,
779 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780{
Damien Miller295ee632011-05-29 21:42:31 +1000781 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
782 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000783 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100784 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100785 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000786 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100787 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000788 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100789 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100790 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791
Damien Miller194fd902013-10-15 12:13:05 +1100792 if (activep == NULL) { /* We are processing a command line directive */
793 cmdline = 1;
794 activep = &cmdline;
795 }
796
Damien Millerc652cac2003-05-14 13:40:54 +1000797 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000798 if ((len = strlen(line)) == 0)
799 return 0;
800 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000801 if (strchr(WHITESPACE, line[len]) == NULL)
802 break;
803 line[len] = '\0';
804 }
805
Damien Millerbe484b52000-07-15 14:14:16 +1000806 s = line;
807 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100808 if ((keyword = strdelim(&s)) == NULL)
809 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000810 /* Ignore leading whitespace. */
811 if (*keyword == '\0')
812 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000813 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100814 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000815 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100816 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817
Darren Tucker07636982013-05-16 20:30:03 +1000818 opcode = parse_token(keyword, filename, linenum,
819 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820
Damien Miller95def091999-11-25 00:26:21 +1100821 switch (opcode) {
822 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100823 /* don't panic, but count bad options */
824 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100825 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000826 case oIgnoredUnknownOption:
827 debug("%s line %d: Ignored unknown option \"%s\"",
828 filename, linenum, keyword);
829 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000830 case oConnectTimeout:
831 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100832parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000833 arg = strdelim(&s);
834 if (!arg || *arg == '\0')
835 fatal("%s line %d: missing time value.",
836 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000837 if (strcmp(arg, "none") == 0)
838 value = -1;
839 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000840 fatal("%s line %d: invalid time value.",
841 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100842 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000843 *intptr = value;
844 break;
845
Damien Miller95def091999-11-25 00:26:21 +1100846 case oForwardAgent:
847 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100848 parse_flag:
849 multistate_ptr = multistate_flag;
850 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000851 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000852 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100853 fatal("%s line %d: missing argument.",
854 filename, linenum);
855 value = -1;
856 for (i = 0; multistate_ptr[i].key != NULL; i++) {
857 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
858 value = multistate_ptr[i].value;
859 break;
860 }
861 }
862 if (value == -1)
863 fatal("%s line %d: unsupported option \"%s\".",
864 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100865 if (*activep && *intptr == -1)
866 *intptr = value;
867 break;
868
869 case oForwardX11:
870 intptr = &options->forward_x11;
871 goto parse_flag;
872
Darren Tucker0a118da2003-10-15 15:54:32 +1000873 case oForwardX11Trusted:
874 intptr = &options->forward_x11_trusted;
875 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000876
Damien Miller1ab6a512010-06-26 10:02:24 +1000877 case oForwardX11Timeout:
878 intptr = &options->forward_x11_timeout;
879 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000880
Damien Miller95def091999-11-25 00:26:21 +1100881 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000882 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100883 goto parse_flag;
884
Darren Tuckere7d4b192006-07-12 22:17:10 +1000885 case oExitOnForwardFailure:
886 intptr = &options->exit_on_forward_failure;
887 goto parse_flag;
888
Damien Miller95def091999-11-25 00:26:21 +1100889 case oUsePrivilegedPort:
890 intptr = &options->use_privileged_port;
891 goto parse_flag;
892
Damien Miller95def091999-11-25 00:26:21 +1100893 case oPasswordAuthentication:
894 intptr = &options->password_authentication;
895 goto parse_flag;
896
Damien Miller874d77b2000-10-14 16:23:11 +1100897 case oKbdInteractiveAuthentication:
898 intptr = &options->kbd_interactive_authentication;
899 goto parse_flag;
900
901 case oKbdInteractiveDevices:
902 charptr = &options->kbd_interactive_devices;
903 goto parse_string;
904
Damien Miller0bc1bd82000-11-13 22:57:25 +1100905 case oPubkeyAuthentication:
906 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000907 goto parse_flag;
908
Damien Miller95def091999-11-25 00:26:21 +1100909 case oRSAAuthentication:
910 intptr = &options->rsa_authentication;
911 goto parse_flag;
912
913 case oRhostsRSAAuthentication:
914 intptr = &options->rhosts_rsa_authentication;
915 goto parse_flag;
916
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000917 case oHostbasedAuthentication:
918 intptr = &options->hostbased_authentication;
919 goto parse_flag;
920
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000921 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000922 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100923 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000924
Darren Tucker0efd1552003-08-26 11:49:55 +1000925 case oGssAuthentication:
926 intptr = &options->gss_authentication;
927 goto parse_flag;
928
929 case oGssDelegateCreds:
930 intptr = &options->gss_deleg_creds;
931 goto parse_flag;
932
Damien Miller95def091999-11-25 00:26:21 +1100933 case oBatchMode:
934 intptr = &options->batch_mode;
935 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Damien Miller95def091999-11-25 00:26:21 +1100937 case oCheckHostIP:
938 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000939 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940
Damien Miller37876e92003-05-15 10:19:46 +1000941 case oVerifyHostKeyDNS:
942 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100943 multistate_ptr = multistate_yesnoask;
944 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000945
Damien Miller95def091999-11-25 00:26:21 +1100946 case oStrictHostKeyChecking:
947 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100948 multistate_ptr = multistate_yesnoask;
949 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Damien Miller95def091999-11-25 00:26:21 +1100951 case oCompression:
952 intptr = &options->compression;
953 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954
Damien Miller12c150e2003-12-17 16:31:10 +1100955 case oTCPKeepAlive:
956 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100957 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000959 case oNoHostAuthenticationForLocalhost:
960 intptr = &options->no_host_authentication_for_localhost;
961 goto parse_flag;
962
Damien Miller95def091999-11-25 00:26:21 +1100963 case oNumberOfPasswordPrompts:
964 intptr = &options->number_of_password_prompts;
965 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Damien Miller95def091999-11-25 00:26:21 +1100967 case oCompressionLevel:
968 intptr = &options->compression_level;
969 goto parse_int;
970
Damien Millera5539d22003-04-09 20:50:06 +1000971 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000972 arg = strdelim(&s);
973 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000974 fatal("%.200s line %d: Missing argument.", filename,
975 linenum);
976 if (strcmp(arg, "default") == 0) {
977 val64 = 0;
978 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000979 if (scan_scaled(arg, &val64) == -1)
980 fatal("%.200s line %d: Bad number '%s': %s",
981 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000982 if (val64 != 0 && val64 < 16)
983 fatal("%.200s line %d: RekeyLimit too small",
984 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000985 }
Damien Miller3dff1762008-02-10 22:25:52 +1100986 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +0000987 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000988 if (s != NULL) { /* optional rekey interval present */
989 if (strcmp(s, "none") == 0) {
990 (void)strdelim(&s); /* discard */
991 break;
992 }
993 intptr = &options->rekey_interval;
994 goto parse_time;
995 }
Damien Millera5539d22003-04-09 20:50:06 +1000996 break;
997
Damien Miller95def091999-11-25 00:26:21 +1100998 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000999 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001000 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001001 fatal("%.200s line %d: Missing argument.", filename, linenum);
1002 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001003 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001004 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001005 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001006 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001007 add_identity_file(options, NULL,
1008 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001009 }
1010 break;
1011
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001012 case oCertificateFile:
1013 arg = strdelim(&s);
1014 if (!arg || *arg == '\0')
1015 fatal("%.200s line %d: Missing argument.",
1016 filename, linenum);
1017 if (*activep) {
1018 intptr = &options->num_certificate_files;
1019 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1020 fatal("%.200s line %d: Too many certificate "
1021 "files specified (max %d).",
1022 filename, linenum,
1023 SSH_MAX_CERTIFICATE_FILES);
1024 }
1025 add_certificate_file(options, arg,
1026 flags & SSHCONF_USERCONF);
1027 }
1028 break;
1029
Damien Millerd3a18572000-06-07 19:55:44 +10001030 case oXAuthLocation:
1031 charptr=&options->xauth_location;
1032 goto parse_string;
1033
Damien Miller95def091999-11-25 00:26:21 +11001034 case oUser:
1035 charptr = &options->user;
1036parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001037 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001038 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001039 fatal("%.200s line %d: Missing argument.",
1040 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001041 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001042 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001043 break;
1044
1045 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001046 cpptr = (char **)&options->system_hostfiles;
1047 uintptr = &options->num_system_hostfiles;
1048 max_entries = SSH_MAX_HOSTS_FILES;
1049parse_char_array:
1050 if (*activep && *uintptr == 0) {
1051 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1052 if ((*uintptr) >= max_entries)
1053 fatal("%s line %d: "
1054 "too many authorized keys files.",
1055 filename, linenum);
1056 cpptr[(*uintptr)++] = xstrdup(arg);
1057 }
1058 }
1059 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001060
1061 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001062 cpptr = (char **)&options->user_hostfiles;
1063 uintptr = &options->num_user_hostfiles;
1064 max_entries = SSH_MAX_HOSTS_FILES;
1065 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001066
Damien Miller95def091999-11-25 00:26:21 +11001067 case oHostName:
1068 charptr = &options->hostname;
1069 goto parse_string;
1070
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001071 case oHostKeyAlias:
1072 charptr = &options->host_key_alias;
1073 goto parse_string;
1074
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001075 case oPreferredAuthentications:
1076 charptr = &options->preferred_authentications;
1077 goto parse_string;
1078
Ben Lindstrome0f88042001-04-30 13:06:24 +00001079 case oBindAddress:
1080 charptr = &options->bind_address;
1081 goto parse_string;
1082
Damien Miller7ea845e2010-02-12 09:21:02 +11001083 case oPKCS11Provider:
1084 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001085 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001086
Damien Miller95def091999-11-25 00:26:21 +11001087 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001088 charptr = &options->proxy_command;
1089parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001090 if (s == NULL)
1091 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001092 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001093 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001094 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001095 return 0;
1096
1097 case oPort:
1098 intptr = &options->port;
1099parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001100 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001101 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001102 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001103 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001104 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001105
1106 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001107 value = strtol(arg, &endofnumber, 0);
1108 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001109 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001110 if (*activep && *intptr == -1)
1111 *intptr = value;
1112 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113
Damien Miller95def091999-11-25 00:26:21 +11001114 case oConnectionAttempts:
1115 intptr = &options->connection_attempts;
1116 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001117
Damien Miller95def091999-11-25 00:26:21 +11001118 case oCipher:
1119 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001120 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001121 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001122 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001123 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001124 if (value == -1)
1125 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001126 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001127 if (*activep && *intptr == -1)
1128 *intptr = value;
1129 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130
Damien Miller78928792000-04-12 20:17:38 +10001131 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001132 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001133 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001134 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001135 if (!ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001136 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001137 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001138 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001139 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001140 break;
1141
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001142 case oMacs:
1143 arg = strdelim(&s);
1144 if (!arg || *arg == '\0')
1145 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001146 if (!mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001147 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001148 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001149 if (*activep && options->macs == NULL)
1150 options->macs = xstrdup(arg);
1151 break;
1152
Damien Millerd5f62bf2010-09-24 22:11:14 +10001153 case oKexAlgorithms:
1154 arg = strdelim(&s);
1155 if (!arg || *arg == '\0')
1156 fatal("%.200s line %d: Missing argument.",
1157 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001158 if (!kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001159 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1160 filename, linenum, arg ? arg : "<NONE>");
1161 if (*activep && options->kex_algorithms == NULL)
1162 options->kex_algorithms = xstrdup(arg);
1163 break;
1164
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001165 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001166 charptr = &options->hostkeyalgorithms;
1167parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001168 arg = strdelim(&s);
1169 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001170 fatal("%.200s line %d: Missing argument.",
1171 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001172 if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001173 fatal("%s line %d: Bad key types '%s'.",
1174 filename, linenum, arg ? arg : "<NONE>");
1175 if (*activep && *charptr == NULL)
1176 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001177 break;
1178
Damien Miller78928792000-04-12 20:17:38 +10001179 case oProtocol:
1180 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001181 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001182 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001183 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001184 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001185 if (value == SSH_PROTO_UNKNOWN)
1186 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001187 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001188 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1189 *intptr = value;
1190 break;
1191
Damien Miller95def091999-11-25 00:26:21 +11001192 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001193 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001194 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001195 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001196 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001197 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001198 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001199 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1200 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001201 break;
1202
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001203 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001204 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001205 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001206 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001207 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001208 fatal("%.200s line %d: Missing port argument.",
1209 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001210
Damien Millera699d952008-11-03 19:27:34 +11001211 if (opcode == oLocalForward ||
1212 opcode == oRemoteForward) {
1213 arg2 = strdelim(&s);
1214 if (arg2 == NULL || *arg2 == '\0')
1215 fatal("%.200s line %d: Missing target argument.",
1216 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001217
Damien Millera699d952008-11-03 19:27:34 +11001218 /* construct a string for parse_forward */
1219 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1220 } else if (opcode == oDynamicForward) {
1221 strlcpy(fwdarg, arg, sizeof(fwdarg));
1222 }
1223
1224 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001225 opcode == oDynamicForward ? 1 : 0,
1226 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001227 fatal("%.200s line %d: Bad forwarding specification.",
1228 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001229
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001230 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001231 if (opcode == oLocalForward ||
1232 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001233 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001234 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001235 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001236 }
Damien Miller95def091999-11-25 00:26:21 +11001237 break;
1238
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001239 case oClearAllForwardings:
1240 intptr = &options->clear_forwardings;
1241 goto parse_flag;
1242
Damien Miller95def091999-11-25 00:26:21 +11001243 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001244 if (cmdline)
1245 fatal("Host directive not supported as a command-line "
1246 "option");
Damien Miller95def091999-11-25 00:26:21 +11001247 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001248 arg2 = NULL;
1249 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1250 negated = *arg == '!';
1251 if (negated)
1252 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001253 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001254 if (negated) {
1255 debug("%.200s line %d: Skipping Host "
1256 "block because of negated match "
1257 "for %.100s", filename, linenum,
1258 arg);
1259 *activep = 0;
1260 break;
1261 }
1262 if (!*activep)
1263 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001264 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001265 }
Damien Millerfe924212011-05-15 08:44:45 +10001266 }
1267 if (*activep)
1268 debug("%.200s line %d: Applying options for %.100s",
1269 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001270 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001271 return 0;
1272
Damien Miller194fd902013-10-15 12:13:05 +11001273 case oMatch:
1274 if (cmdline)
1275 fatal("Host directive not supported as a command-line "
1276 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001277 value = match_cfg_line(options, &s, pw, host, original_host,
1278 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001279 if (value < 0)
1280 fatal("%.200s line %d: Bad Match condition", filename,
1281 linenum);
1282 *activep = value;
1283 break;
1284
Damien Miller95def091999-11-25 00:26:21 +11001285 case oEscapeChar:
1286 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001287 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001288 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001289 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001290 if (strcmp(arg, "none") == 0)
1291 value = SSH_ESCAPECHAR_NONE;
1292 else if (arg[1] == '\0')
1293 value = (u_char) arg[0];
1294 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001295 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1296 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001297 else {
1298 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001299 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001300 /* NOTREACHED */
1301 value = 0; /* Avoid compiler warning. */
1302 }
1303 if (*activep && *intptr == -1)
1304 *intptr = value;
1305 break;
1306
Damien Miller20a8f972003-05-18 20:50:30 +10001307 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001308 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001309 multistate_ptr = multistate_addressfamily;
1310 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001311
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001312 case oEnableSSHKeysign:
1313 intptr = &options->enable_ssh_keysign;
1314 goto parse_flag;
1315
Damien Millerbd394c32004-03-08 23:12:36 +11001316 case oIdentitiesOnly:
1317 intptr = &options->identities_only;
1318 goto parse_flag;
1319
Damien Miller509b0102003-12-17 16:33:10 +11001320 case oServerAliveInterval:
1321 intptr = &options->server_alive_interval;
1322 goto parse_time;
1323
1324 case oServerAliveCountMax:
1325 intptr = &options->server_alive_count_max;
1326 goto parse_int;
1327
Darren Tucker46bc0752004-05-02 22:11:30 +10001328 case oSendEnv:
1329 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1330 if (strchr(arg, '=') != NULL)
1331 fatal("%s line %d: Invalid environment name.",
1332 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001333 if (!*activep)
1334 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001335 if (options->num_send_env >= MAX_SEND_ENV)
1336 fatal("%s line %d: too many send env.",
1337 filename, linenum);
1338 options->send_env[options->num_send_env++] =
1339 xstrdup(arg);
1340 }
1341 break;
1342
Damien Miller0e220db2004-06-15 10:34:08 +10001343 case oControlPath:
1344 charptr = &options->control_path;
1345 goto parse_string;
1346
1347 case oControlMaster:
1348 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001349 multistate_ptr = multistate_controlmaster;
1350 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001351
Damien Millere11e1ea2010-08-03 16:04:46 +10001352 case oControlPersist:
1353 /* no/false/yes/true, or a time spec */
1354 intptr = &options->control_persist;
1355 arg = strdelim(&s);
1356 if (!arg || *arg == '\0')
1357 fatal("%.200s line %d: Missing ControlPersist"
1358 " argument.", filename, linenum);
1359 value = 0;
1360 value2 = 0; /* timeout */
1361 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1362 value = 0;
1363 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1364 value = 1;
1365 else if ((value2 = convtime(arg)) >= 0)
1366 value = 1;
1367 else
1368 fatal("%.200s line %d: Bad ControlPersist argument.",
1369 filename, linenum);
1370 if (*activep && *intptr == -1) {
1371 *intptr = value;
1372 options->control_persist_timeout = value2;
1373 }
1374 break;
1375
Damien Millere1776152005-03-01 21:47:37 +11001376 case oHashKnownHosts:
1377 intptr = &options->hash_known_hosts;
1378 goto parse_flag;
1379
Damien Millerd27b9472005-12-13 19:29:02 +11001380 case oTunnel:
1381 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001382 multistate_ptr = multistate_tunnel;
1383 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001384
1385 case oTunnelDevice:
1386 arg = strdelim(&s);
1387 if (!arg || *arg == '\0')
1388 fatal("%.200s line %d: Missing argument.", filename, linenum);
1389 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001390 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001391 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1392 if (*activep) {
1393 options->tun_local = value;
1394 options->tun_remote = value2;
1395 }
1396 break;
1397
1398 case oLocalCommand:
1399 charptr = &options->local_command;
1400 goto parse_command;
1401
1402 case oPermitLocalCommand:
1403 intptr = &options->permit_local_command;
1404 goto parse_flag;
1405
Damien Miller10288242008-06-30 00:04:03 +10001406 case oVisualHostKey:
1407 intptr = &options->visual_host_key;
1408 goto parse_flag;
1409
Damien Miller0dac6fb2010-11-20 15:19:38 +11001410 case oIPQoS:
1411 arg = strdelim(&s);
1412 if ((value = parse_ipqos(arg)) == -1)
1413 fatal("%s line %d: Bad IPQoS value: %s",
1414 filename, linenum, arg);
1415 arg = strdelim(&s);
1416 if (arg == NULL)
1417 value2 = value;
1418 else if ((value2 = parse_ipqos(arg)) == -1)
1419 fatal("%s line %d: Bad IPQoS value: %s",
1420 filename, linenum, arg);
1421 if (*activep) {
1422 options->ip_qos_interactive = value;
1423 options->ip_qos_bulk = value2;
1424 }
1425 break;
1426
Damien Miller21771e22011-05-15 08:45:50 +10001427 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001428 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001429 multistate_ptr = multistate_requesttty;
1430 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001431
Darren Tucker07636982013-05-16 20:30:03 +10001432 case oIgnoreUnknown:
1433 charptr = &options->ignored_unknown;
1434 goto parse_string;
1435
Damien Miller1262b662013-08-21 02:44:24 +10001436 case oProxyUseFdpass:
1437 intptr = &options->proxy_use_fdpass;
1438 goto parse_flag;
1439
Damien Miller0faf7472013-10-17 11:47:23 +11001440 case oCanonicalDomains:
1441 value = options->num_canonical_domains != 0;
1442 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1443 valid_domain(arg, filename, linenum);
1444 if (!*activep || value)
1445 continue;
1446 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1447 fatal("%s line %d: too many hostname suffixes.",
1448 filename, linenum);
1449 options->canonical_domains[
1450 options->num_canonical_domains++] = xstrdup(arg);
1451 }
1452 break;
1453
Damien Miller38505592013-10-17 11:48:13 +11001454 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001455 value = options->num_permitted_cnames != 0;
1456 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1457 /* Either '*' for everything or 'list:list' */
1458 if (strcmp(arg, "*") == 0)
1459 arg2 = arg;
1460 else {
1461 lowercase(arg);
1462 if ((arg2 = strchr(arg, ':')) == NULL ||
1463 arg2[1] == '\0') {
1464 fatal("%s line %d: "
1465 "Invalid permitted CNAME \"%s\"",
1466 filename, linenum, arg);
1467 }
1468 *arg2 = '\0';
1469 arg2++;
1470 }
1471 if (!*activep || value)
1472 continue;
1473 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1474 fatal("%s line %d: too many permitted CNAMEs.",
1475 filename, linenum);
1476 cname = options->permitted_cnames +
1477 options->num_permitted_cnames++;
1478 cname->source_list = xstrdup(arg);
1479 cname->target_list = xstrdup(arg2);
1480 }
1481 break;
1482
Damien Miller38505592013-10-17 11:48:13 +11001483 case oCanonicalizeHostname:
1484 intptr = &options->canonicalize_hostname;
1485 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001486 goto parse_multistate;
1487
Damien Miller38505592013-10-17 11:48:13 +11001488 case oCanonicalizeMaxDots:
1489 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001490 goto parse_int;
1491
Damien Miller38505592013-10-17 11:48:13 +11001492 case oCanonicalizeFallbackLocal:
1493 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001494 goto parse_flag;
1495
Damien Miller7acefbb2014-07-18 14:11:24 +10001496 case oStreamLocalBindMask:
1497 arg = strdelim(&s);
1498 if (!arg || *arg == '\0')
1499 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1500 /* Parse mode in octal format */
1501 value = strtol(arg, &endofnumber, 8);
1502 if (arg == endofnumber || value < 0 || value > 0777)
1503 fatal("%.200s line %d: Bad mask.", filename, linenum);
1504 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1505 break;
1506
1507 case oStreamLocalBindUnlink:
1508 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1509 goto parse_flag;
1510
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001511 case oRevokedHostKeys:
1512 charptr = &options->revoked_host_keys;
1513 goto parse_string;
1514
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001515 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001516 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001517 arg = strdelim(&s);
1518 if (!arg || *arg == '\0')
1519 fatal("%.200s line %d: Missing argument.",
1520 filename, linenum);
1521 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1522 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1523 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001524 if (*activep && *intptr == -1)
1525 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001526 break;
1527
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001528 case oUpdateHostkeys:
1529 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001530 multistate_ptr = multistate_yesnoask;
1531 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001532
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001533 case oHostbasedKeyTypes:
1534 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001535 goto parse_keytypes;
1536
1537 case oPubkeyAcceptedKeyTypes:
1538 charptr = &options->pubkey_key_types;
1539 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001540
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001541 case oAddKeysToAgent:
1542 intptr = &options->add_keys_to_agent;
1543 multistate_ptr = multistate_yesnoaskconfirm;
1544 goto parse_multistate;
1545
Ben Lindstrom4daea862002-06-09 20:04:02 +00001546 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001547 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001548 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001549 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001550
Damien Millerf9b3feb2003-05-16 11:38:32 +10001551 case oUnsupported:
1552 error("%s line %d: Unsupported option \"%s\"",
1553 filename, linenum, keyword);
1554 return 0;
1555
Damien Miller95def091999-11-25 00:26:21 +11001556 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001557 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001558 }
1559
1560 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001561 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001562 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001563 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001564 }
Damien Miller95def091999-11-25 00:26:21 +11001565 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001566}
1567
1568
Damien Miller5428f641999-11-25 11:54:57 +11001569/*
1570 * Reads the config file and modifies the options accordingly. Options
1571 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001572 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001573 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001574
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001575int
Damien Miller194fd902013-10-15 12:13:05 +11001576read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001577 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001578{
Damien Miller95def091999-11-25 00:26:21 +11001579 FILE *f;
1580 char line[1024];
1581 int active, linenum;
1582 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Damien Miller57a44762004-04-20 20:11:57 +10001584 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001585 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001586
Darren Tuckeraefa3682013-04-05 11:18:35 +11001587 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001588 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001589
Damien Miller33793852004-06-15 10:27:55 +10001590 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001591 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001592 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001593 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001594 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001595 }
1596
Damien Miller95def091999-11-25 00:26:21 +11001597 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001598
Damien Miller5428f641999-11-25 11:54:57 +11001599 /*
1600 * Mark that we are now processing the options. This flag is turned
1601 * on/off by Host specifications.
1602 */
Damien Miller95def091999-11-25 00:26:21 +11001603 active = 1;
1604 linenum = 0;
1605 while (fgets(line, sizeof(line), f)) {
1606 /* Update line number counter. */
1607 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001608 if (process_config_line(options, pw, host, original_host,
1609 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001610 bad_options++;
1611 }
1612 fclose(f);
1613 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001614 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001615 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001616 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001617}
1618
Damien Miller13f97b22014-02-24 15:57:55 +11001619/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1620int
1621option_clear_or_none(const char *o)
1622{
1623 return o == NULL || strcasecmp(o, "none") == 0;
1624}
1625
Damien Miller5428f641999-11-25 11:54:57 +11001626/*
1627 * Initializes options to special values that indicate that they have not yet
1628 * been set. Read_config_file will only set options with this value. Options
1629 * are processed in the following order: command line, user config file,
1630 * system config file. Last, fill_default_options is called.
1631 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632
Damien Miller4af51302000-04-16 11:18:38 +10001633void
Damien Miller95def091999-11-25 00:26:21 +11001634initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635{
Damien Miller95def091999-11-25 00:26:21 +11001636 memset(options, 'X', sizeof(*options));
1637 options->forward_agent = -1;
1638 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001639 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001640 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001641 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001642 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001643 options->fwd_opts.gateway_ports = -1;
1644 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1645 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001646 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001647 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001648 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001649 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001650 options->gss_authentication = -1;
1651 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001652 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001653 options->kbd_interactive_authentication = -1;
1654 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001655 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001656 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001657 options->batch_mode = -1;
1658 options->check_host_ip = -1;
1659 options->strict_host_key_checking = -1;
1660 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001661 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001662 options->compression_level = -1;
1663 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001664 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001665 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001666 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001667 options->number_of_password_prompts = -1;
1668 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001669 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001670 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001671 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001672 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001673 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001674 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001675 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001676 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001677 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001678 options->proxy_command = NULL;
1679 options->user = NULL;
1680 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001681 options->num_system_hostfiles = 0;
1682 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001683 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001684 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001685 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001686 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001687 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001688 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001689 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001690 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001691 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001692 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001693 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001694 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001695 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001696 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001697 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001698 options->server_alive_interval = -1;
1699 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001700 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001701 options->control_path = NULL;
1702 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001703 options->control_persist = -1;
1704 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001705 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001706 options->tun_open = -1;
1707 options->tun_local = -1;
1708 options->tun_remote = -1;
1709 options->local_command = NULL;
1710 options->permit_local_command = -1;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001711 options->add_keys_to_agent = -1;
Damien Miller10288242008-06-30 00:04:03 +10001712 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001713 options->ip_qos_interactive = -1;
1714 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001715 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001716 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001717 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001718 options->num_canonical_domains = 0;
1719 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001720 options->canonicalize_max_dots = -1;
1721 options->canonicalize_fallback_local = -1;
1722 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001723 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001724 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001725 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001726 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001727 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001728}
1729
Damien Miller5428f641999-11-25 11:54:57 +11001730/*
Damien Miller13f97b22014-02-24 15:57:55 +11001731 * A petite version of fill_default_options() that just fills the options
1732 * needed for hostname canonicalization to proceed.
1733 */
1734void
1735fill_default_options_for_canonicalization(Options *options)
1736{
1737 if (options->canonicalize_max_dots == -1)
1738 options->canonicalize_max_dots = 1;
1739 if (options->canonicalize_fallback_local == -1)
1740 options->canonicalize_fallback_local = 1;
1741 if (options->canonicalize_hostname == -1)
1742 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1743}
1744
1745/*
Damien Miller5428f641999-11-25 11:54:57 +11001746 * Called after processing other sources of option data, this fills those
1747 * options for which no value has been specified with their default values.
1748 */
Damien Miller4af51302000-04-16 11:18:38 +10001749void
Damien Miller95def091999-11-25 00:26:21 +11001750fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001751{
Damien Miller95def091999-11-25 00:26:21 +11001752 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001753 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001754 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001755 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001756 if (options->forward_x11_trusted == -1)
1757 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001758 if (options->forward_x11_timeout == -1)
1759 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001760 if (options->exit_on_forward_failure == -1)
1761 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001762 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001763 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001764 if (options->fwd_opts.gateway_ports == -1)
1765 options->fwd_opts.gateway_ports = 0;
1766 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1767 options->fwd_opts.streamlocal_bind_mask = 0177;
1768 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1769 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001770 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001771 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001772 if (options->rsa_authentication == -1)
1773 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001774 if (options->pubkey_authentication == -1)
1775 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001776 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001777 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001778 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001779 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001780 if (options->gss_deleg_creds == -1)
1781 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001782 if (options->password_authentication == -1)
1783 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001784 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001785 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001786 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001787 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001788 if (options->hostbased_authentication == -1)
1789 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001790 if (options->batch_mode == -1)
1791 options->batch_mode = 0;
1792 if (options->check_host_ip == -1)
1793 options->check_host_ip = 1;
1794 if (options->strict_host_key_checking == -1)
1795 options->strict_host_key_checking = 2; /* 2 is default */
1796 if (options->compression == -1)
1797 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001798 if (options->tcp_keep_alive == -1)
1799 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001800 if (options->compression_level == -1)
1801 options->compression_level = 6;
1802 if (options->port == -1)
1803 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001804 if (options->address_family == -1)
1805 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001806 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001807 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001808 if (options->number_of_password_prompts == -1)
1809 options->number_of_password_prompts = 3;
1810 /* Selected in ssh_login(). */
1811 if (options->cipher == -1)
1812 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001813 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001814 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001815 options->protocol = SSH_PROTO_2;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001816 if (options->add_keys_to_agent == -1)
1817 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001818 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001819 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001820 add_identity_file(options, "~/",
1821 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001822 }
1823 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001824 add_identity_file(options, "~/",
1825 _PATH_SSH_CLIENT_ID_RSA, 0);
1826 add_identity_file(options, "~/",
1827 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001828#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001829 add_identity_file(options, "~/",
1830 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001831#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001832 add_identity_file(options, "~/",
1833 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001834 }
Damien Millereba71ba2000-04-29 23:57:08 +10001835 }
Damien Miller95def091999-11-25 00:26:21 +11001836 if (options->escape_char == -1)
1837 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001838 if (options->num_system_hostfiles == 0) {
1839 options->system_hostfiles[options->num_system_hostfiles++] =
1840 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1841 options->system_hostfiles[options->num_system_hostfiles++] =
1842 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1843 }
1844 if (options->num_user_hostfiles == 0) {
1845 options->user_hostfiles[options->num_user_hostfiles++] =
1846 xstrdup(_PATH_SSH_USER_HOSTFILE);
1847 options->user_hostfiles[options->num_user_hostfiles++] =
1848 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1849 }
Damien Millerfcd93202002-02-05 12:26:34 +11001850 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001851 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001852 if (options->clear_forwardings == 1)
1853 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001854 if (options->no_host_authentication_for_localhost == - 1)
1855 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001856 if (options->identities_only == -1)
1857 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001858 if (options->enable_ssh_keysign == -1)
1859 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001860 if (options->rekey_limit == -1)
1861 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001862 if (options->rekey_interval == -1)
1863 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001864 if (options->verify_host_key_dns == -1)
1865 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001866 if (options->server_alive_interval == -1)
1867 options->server_alive_interval = 0;
1868 if (options->server_alive_count_max == -1)
1869 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001870 if (options->control_master == -1)
1871 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001872 if (options->control_persist == -1) {
1873 options->control_persist = 0;
1874 options->control_persist_timeout = 0;
1875 }
Damien Millere1776152005-03-01 21:47:37 +11001876 if (options->hash_known_hosts == -1)
1877 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001878 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001879 options->tun_open = SSH_TUNMODE_NO;
1880 if (options->tun_local == -1)
1881 options->tun_local = SSH_TUNID_ANY;
1882 if (options->tun_remote == -1)
1883 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001884 if (options->permit_local_command == -1)
1885 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10001886 if (options->visual_host_key == -1)
1887 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001888 if (options->ip_qos_interactive == -1)
1889 options->ip_qos_interactive = IPTOS_LOWDELAY;
1890 if (options->ip_qos_bulk == -1)
1891 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001892 if (options->request_tty == -1)
1893 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001894 if (options->proxy_use_fdpass == -1)
1895 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001896 if (options->canonicalize_max_dots == -1)
1897 options->canonicalize_max_dots = 1;
1898 if (options->canonicalize_fallback_local == -1)
1899 options->canonicalize_fallback_local = 1;
1900 if (options->canonicalize_hostname == -1)
1901 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001902 if (options->fingerprint_hash == -1)
1903 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001904 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00001905 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001906 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
1907 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
1908 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
1909 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1910 &options->hostbased_key_types) != 0 ||
1911 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1912 &options->pubkey_key_types) != 0)
1913 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001914
Damien Millere9fc72e2013-10-15 12:14:12 +11001915#define CLEAR_ON_NONE(v) \
1916 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001917 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001918 free(v); \
1919 v = NULL; \
1920 } \
1921 } while(0)
1922 CLEAR_ON_NONE(options->local_command);
1923 CLEAR_ON_NONE(options->proxy_command);
1924 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001925 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001926 /* options->user will be set in the main program if appropriate */
1927 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001928 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001929 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001930}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001931
Damien Miller7acefbb2014-07-18 14:11:24 +10001932struct fwdarg {
1933 char *arg;
1934 int ispath;
1935};
1936
1937/*
1938 * parse_fwd_field
1939 * parses the next field in a port forwarding specification.
1940 * sets fwd to the parsed field and advances p past the colon
1941 * or sets it to NULL at end of string.
1942 * returns 0 on success, else non-zero.
1943 */
1944static int
1945parse_fwd_field(char **p, struct fwdarg *fwd)
1946{
1947 char *ep, *cp = *p;
1948 int ispath = 0;
1949
1950 if (*cp == '\0') {
1951 *p = NULL;
1952 return -1; /* end of string */
1953 }
1954
1955 /*
1956 * A field escaped with square brackets is used literally.
1957 * XXX - allow ']' to be escaped via backslash?
1958 */
1959 if (*cp == '[') {
1960 /* find matching ']' */
1961 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1962 if (*ep == '/')
1963 ispath = 1;
1964 }
1965 /* no matching ']' or not at end of field. */
1966 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1967 return -1;
1968 /* NUL terminate the field and advance p past the colon */
1969 *ep++ = '\0';
1970 if (*ep != '\0')
1971 *ep++ = '\0';
1972 fwd->arg = cp + 1;
1973 fwd->ispath = ispath;
1974 *p = ep;
1975 return 0;
1976 }
1977
1978 for (cp = *p; *cp != '\0'; cp++) {
1979 switch (*cp) {
1980 case '\\':
1981 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00001982 if (*cp == '\0')
1983 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10001984 break;
1985 case '/':
1986 ispath = 1;
1987 break;
1988 case ':':
1989 *cp++ = '\0';
1990 goto done;
1991 }
1992 }
1993done:
1994 fwd->arg = *p;
1995 fwd->ispath = ispath;
1996 *p = cp;
1997 return 0;
1998}
1999
Damien Millerf91ee4c2005-03-01 21:24:33 +11002000/*
2001 * parse_forward
2002 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002003 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002004 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2005 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002006 * dynamicfwd == 1
2007 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002008 * returns number of arguments parsed or zero on error
2009 */
2010int
Damien Miller7acefbb2014-07-18 14:11:24 +10002011parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002012{
Damien Miller7acefbb2014-07-18 14:11:24 +10002013 struct fwdarg fwdargs[4];
2014 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002015 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002016
Damien Miller7acefbb2014-07-18 14:11:24 +10002017 memset(fwd, 0, sizeof(*fwd));
2018 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002019
2020 cp = p = xstrdup(fwdspec);
2021
2022 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002023 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002024 cp++;
2025
Damien Miller7acefbb2014-07-18 14:11:24 +10002026 for (i = 0; i < 4; ++i) {
2027 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002028 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002029 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002030
Damien Millerf4b39532008-11-03 19:28:21 +11002031 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002032 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002033 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002034 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002035
2036 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002037 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002038 if (fwdargs[0].ispath) {
2039 fwd->listen_path = xstrdup(fwdargs[0].arg);
2040 fwd->listen_port = PORT_STREAMLOCAL;
2041 } else {
2042 fwd->listen_host = NULL;
2043 fwd->listen_port = a2port(fwdargs[0].arg);
2044 }
Damien Millera699d952008-11-03 19:27:34 +11002045 fwd->connect_host = xstrdup("socks");
2046 break;
2047
2048 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002049 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2050 fwd->listen_path = xstrdup(fwdargs[0].arg);
2051 fwd->listen_port = PORT_STREAMLOCAL;
2052 fwd->connect_path = xstrdup(fwdargs[1].arg);
2053 fwd->connect_port = PORT_STREAMLOCAL;
2054 } else if (fwdargs[1].ispath) {
2055 fwd->listen_host = NULL;
2056 fwd->listen_port = a2port(fwdargs[0].arg);
2057 fwd->connect_path = xstrdup(fwdargs[1].arg);
2058 fwd->connect_port = PORT_STREAMLOCAL;
2059 } else {
2060 fwd->listen_host = xstrdup(fwdargs[0].arg);
2061 fwd->listen_port = a2port(fwdargs[1].arg);
2062 fwd->connect_host = xstrdup("socks");
2063 }
Damien Millera699d952008-11-03 19:27:34 +11002064 break;
2065
Damien Millerf91ee4c2005-03-01 21:24:33 +11002066 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002067 if (fwdargs[0].ispath) {
2068 fwd->listen_path = xstrdup(fwdargs[0].arg);
2069 fwd->listen_port = PORT_STREAMLOCAL;
2070 fwd->connect_host = xstrdup(fwdargs[1].arg);
2071 fwd->connect_port = a2port(fwdargs[2].arg);
2072 } else if (fwdargs[2].ispath) {
2073 fwd->listen_host = xstrdup(fwdargs[0].arg);
2074 fwd->listen_port = a2port(fwdargs[1].arg);
2075 fwd->connect_path = xstrdup(fwdargs[2].arg);
2076 fwd->connect_port = PORT_STREAMLOCAL;
2077 } else {
2078 fwd->listen_host = NULL;
2079 fwd->listen_port = a2port(fwdargs[0].arg);
2080 fwd->connect_host = xstrdup(fwdargs[1].arg);
2081 fwd->connect_port = a2port(fwdargs[2].arg);
2082 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002083 break;
2084
2085 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002086 fwd->listen_host = xstrdup(fwdargs[0].arg);
2087 fwd->listen_port = a2port(fwdargs[1].arg);
2088 fwd->connect_host = xstrdup(fwdargs[2].arg);
2089 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002090 break;
2091 default:
2092 i = 0; /* failure */
2093 }
2094
Darren Tuckera627d422013-06-02 07:31:17 +10002095 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002096
Damien Millera699d952008-11-03 19:27:34 +11002097 if (dynamicfwd) {
2098 if (!(i == 1 || i == 2))
2099 goto fail_free;
2100 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002101 if (!(i == 3 || i == 4)) {
2102 if (fwd->connect_path == NULL &&
2103 fwd->listen_path == NULL)
2104 goto fail_free;
2105 }
2106 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002107 goto fail_free;
2108 }
2109
Damien Miller7acefbb2014-07-18 14:11:24 +10002110 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2111 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002112 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002113 if (fwd->connect_host != NULL &&
2114 strlen(fwd->connect_host) >= NI_MAXHOST)
2115 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002116 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2117 if (fwd->connect_path != NULL &&
2118 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2119 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002120 if (fwd->listen_host != NULL &&
2121 strlen(fwd->listen_host) >= NI_MAXHOST)
2122 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002123 if (fwd->listen_path != NULL &&
2124 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2125 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002126
2127 return (i);
2128
2129 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002130 free(fwd->connect_host);
2131 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002132 free(fwd->connect_path);
2133 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002134 free(fwd->listen_host);
2135 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002136 free(fwd->listen_path);
2137 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002138 return (0);
2139}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002140
2141/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2142static const char *
2143fmt_multistate_int(int val, const struct multistate *m)
2144{
2145 u_int i;
2146
2147 for (i = 0; m[i].key != NULL; i++) {
2148 if (m[i].value == val)
2149 return m[i].key;
2150 }
2151 return "UNKNOWN";
2152}
2153
2154static const char *
2155fmt_intarg(OpCodes code, int val)
2156{
2157 if (val == -1)
2158 return "unset";
2159 switch (code) {
2160 case oAddressFamily:
2161 return fmt_multistate_int(val, multistate_addressfamily);
2162 case oVerifyHostKeyDNS:
2163 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002164 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002165 return fmt_multistate_int(val, multistate_yesnoask);
2166 case oControlMaster:
2167 return fmt_multistate_int(val, multistate_controlmaster);
2168 case oTunnel:
2169 return fmt_multistate_int(val, multistate_tunnel);
2170 case oRequestTTY:
2171 return fmt_multistate_int(val, multistate_requesttty);
2172 case oCanonicalizeHostname:
2173 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002174 case oFingerprintHash:
2175 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002176 case oProtocol:
2177 switch (val) {
2178 case SSH_PROTO_1:
2179 return "1";
2180 case SSH_PROTO_2:
2181 return "2";
2182 case (SSH_PROTO_1|SSH_PROTO_2):
2183 return "2,1";
2184 default:
2185 return "UNKNOWN";
2186 }
2187 default:
2188 switch (val) {
2189 case 0:
2190 return "no";
2191 case 1:
2192 return "yes";
2193 default:
2194 return "UNKNOWN";
2195 }
2196 }
2197}
2198
2199static const char *
2200lookup_opcode_name(OpCodes code)
2201{
2202 u_int i;
2203
2204 for (i = 0; keywords[i].name != NULL; i++)
2205 if (keywords[i].opcode == code)
2206 return(keywords[i].name);
2207 return "UNKNOWN";
2208}
2209
2210static void
2211dump_cfg_int(OpCodes code, int val)
2212{
2213 printf("%s %d\n", lookup_opcode_name(code), val);
2214}
2215
2216static void
2217dump_cfg_fmtint(OpCodes code, int val)
2218{
2219 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2220}
2221
2222static void
2223dump_cfg_string(OpCodes code, const char *val)
2224{
2225 if (val == NULL)
2226 return;
2227 printf("%s %s\n", lookup_opcode_name(code), val);
2228}
2229
2230static void
2231dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2232{
2233 u_int i;
2234
2235 for (i = 0; i < count; i++)
2236 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2237}
2238
2239static void
2240dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2241{
2242 u_int i;
2243
2244 printf("%s", lookup_opcode_name(code));
2245 for (i = 0; i < count; i++)
2246 printf(" %s", vals[i]);
2247 printf("\n");
2248}
2249
2250static void
2251dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2252{
2253 const struct Forward *fwd;
2254 u_int i;
2255
2256 /* oDynamicForward */
2257 for (i = 0; i < count; i++) {
2258 fwd = &fwds[i];
2259 if (code == oDynamicForward &&
2260 strcmp(fwd->connect_host, "socks") != 0)
2261 continue;
2262 if (code == oLocalForward &&
2263 strcmp(fwd->connect_host, "socks") == 0)
2264 continue;
2265 printf("%s", lookup_opcode_name(code));
2266 if (fwd->listen_port == PORT_STREAMLOCAL)
2267 printf(" %s", fwd->listen_path);
2268 else if (fwd->listen_host == NULL)
2269 printf(" %d", fwd->listen_port);
2270 else {
2271 printf(" [%s]:%d",
2272 fwd->listen_host, fwd->listen_port);
2273 }
2274 if (code != oDynamicForward) {
2275 if (fwd->connect_port == PORT_STREAMLOCAL)
2276 printf(" %s", fwd->connect_path);
2277 else if (fwd->connect_host == NULL)
2278 printf(" %d", fwd->connect_port);
2279 else {
2280 printf(" [%s]:%d",
2281 fwd->connect_host, fwd->connect_port);
2282 }
2283 }
2284 printf("\n");
2285 }
2286}
2287
2288void
2289dump_client_config(Options *o, const char *host)
2290{
2291 int i;
2292 char vbuf[5];
2293
djm@openbsd.org60a92472015-08-21 23:53:08 +00002294 /* This is normally prepared in ssh_kex2 */
2295 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2296 fatal("%s: kex_assemble_names failed", __func__);
2297
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002298 /* Most interesting options first: user, host, port */
2299 dump_cfg_string(oUser, o->user);
2300 dump_cfg_string(oHostName, host);
2301 dump_cfg_int(oPort, o->port);
2302
2303 /* Flag options */
2304 dump_cfg_fmtint(oAddressFamily, o->address_family);
2305 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2306 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2307 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2308 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2309 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2310 dump_cfg_fmtint(oCompression, o->compression);
2311 dump_cfg_fmtint(oControlMaster, o->control_master);
2312 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2313 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002314 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002315 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2316 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2317 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2318 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2319#ifdef GSSAPI
2320 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2321 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2322#endif /* GSSAPI */
2323 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2324 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2325 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2326 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2327 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2328 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2329 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2330 dump_cfg_fmtint(oProtocol, o->protocol);
2331 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2332 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2333 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2334 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2335 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2336 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2337 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2338 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2339 dump_cfg_fmtint(oTunnel, o->tun_open);
2340 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2341 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2342 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002343 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002344
2345 /* Integer options */
2346 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2347 dump_cfg_int(oCompressionLevel, o->compression_level);
2348 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2349 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2350 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2351 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2352 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2353
2354 /* String options */
2355 dump_cfg_string(oBindAddress, o->bind_address);
2356 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2357 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002358 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002359 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002360 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002361 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2362 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2363 dump_cfg_string(oLocalCommand, o->local_command);
2364 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2365 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2366 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2367 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2368 dump_cfg_string(oProxyCommand, o->proxy_command);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002369 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002370 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002371 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002372
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002373 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002374 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2375 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2376 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2377
2378 /* String array options */
2379 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2380 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2381 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2382 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2383 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2384
2385 /* Special cases */
2386
2387 /* oConnectTimeout */
2388 if (o->connection_timeout == -1)
2389 printf("connecttimeout none\n");
2390 else
2391 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2392
2393 /* oTunnelDevice */
2394 printf("tunneldevice");
2395 if (o->tun_local == SSH_TUNID_ANY)
2396 printf(" any");
2397 else
2398 printf(" %d", o->tun_local);
2399 if (o->tun_remote == SSH_TUNID_ANY)
2400 printf(":any");
2401 else
2402 printf(":%d", o->tun_remote);
2403 printf("\n");
2404
2405 /* oCanonicalizePermittedCNAMEs */
2406 if ( o->num_permitted_cnames > 0) {
2407 printf("canonicalizePermittedcnames");
2408 for (i = 0; i < o->num_permitted_cnames; i++) {
2409 printf(" %s:%s", o->permitted_cnames[i].source_list,
2410 o->permitted_cnames[i].target_list);
2411 }
2412 printf("\n");
2413 }
2414
2415 /* oCipher */
2416 if (o->cipher != SSH_CIPHER_NOT_SET)
2417 printf("Cipher %s\n", cipher_name(o->cipher));
2418
2419 /* oControlPersist */
2420 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2421 dump_cfg_fmtint(oControlPersist, o->control_persist);
2422 else
2423 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2424
2425 /* oEscapeChar */
2426 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2427 printf("escapechar none\n");
2428 else {
2429 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2430 printf("escapechar %s\n", vbuf);
2431 }
2432
2433 /* oIPQoS */
2434 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2435 printf("%s\n", iptos2str(o->ip_qos_bulk));
2436
2437 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002438 printf("rekeylimit %llu %d\n",
2439 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002440
2441 /* oStreamLocalBindMask */
2442 printf("streamlocalbindmask 0%o\n",
2443 o->fwd_opts.streamlocal_bind_mask);
2444}