blob: 01e3d23e3809848331e74350ff1bd2966b5c4d1a [file] [log] [blame]
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +00001/* $OpenBSD: readconf.c,v 1.243 2015/10/25 23:14:03 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100043#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100044#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000045#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
46# include <vis.h>
47#endif
Damien Millerc7b06362006-03-15 11:53:45 +110048
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "cipher.h"
53#include "pathnames.h"
54#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000055#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100056#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "readconf.h"
58#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "kex.h"
60#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110061#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000062#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000063#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65/* Format of the configuration file:
66
67 # Configuration data is parsed as follows:
68 # 1. command line options
69 # 2. user-specific file
70 # 3. system-wide file
71 # Any configuration value is only changed the first time it is set.
72 # Thus, host-specific definitions should be at the beginning of the
73 # configuration file, and defaults at the end.
74
75 # Host-specific declarations. These may override anything above. A single
76 # host may match multiple declarations; these are processed in the order
77 # that they are given in.
78
79 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000080 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82 Host fake.com
83 HostName another.host.name.real.org
84 User blaah
85 Port 34289
86 ForwardX11 no
87 ForwardAgent no
88
89 Host books.com
90 RemoteForward 9999 shadows.cs.hut.fi:9999
91 Cipher 3des
92
93 Host fascist.blob.com
94 Port 23123
95 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 PasswordAuthentication no
97
98 Host puukko.hut.fi
99 User t35124p
100 ProxyCommand ssh-proxy %h %p
101
102 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000103 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105 Host *.su
106 Cipher none
107 PasswordAuthentication no
108
Damien Millerd27b9472005-12-13 19:29:02 +1100109 Host vpn.fake.com
110 Tunnel yes
111 TunnelDevice 3
112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 # Defaults for various options
114 Host *
115 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 PasswordAuthentication yes
118 RSAAuthentication yes
119 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100121 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 IdentityFile ~/.ssh/identity
123 Port 22
124 EscapeChar ~
125
126*/
127
128/* Keyword tokens. */
129
Damien Miller95def091999-11-25 00:26:21 +1100130typedef enum {
131 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100132 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000133 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
134 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000135 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000136 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100137 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000138 oCertificateFile,
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,
Damien Miller7cc194f2014-02-04 11:12:56 +1100155 oVisualHostKey, oUseRoaming,
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 },
Damien Miller95def091999-11-25 00:26:21 +1100207 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000208 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100209 { "proxycommand", oProxyCommand },
210 { "port", oPort },
211 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000212 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000213 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000214 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100215 { "remoteforward", oRemoteForward },
216 { "localforward", oLocalForward },
217 { "user", oUser },
218 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100219 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100220 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100221 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000222 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100223 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000224 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100225 { "connectionattempts", oConnectionAttempts },
226 { "batchmode", oBatchMode },
227 { "checkhostip", oCheckHostIP },
228 { "stricthostkeychecking", oStrictHostKeyChecking },
229 { "compression", oCompression },
230 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100231 { "tcpkeepalive", oTCPKeepAlive },
232 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100233 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100234 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000235 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000236 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000237 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000238 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100239#ifdef ENABLE_PKCS11
240 { "smartcarddevice", oPKCS11Provider },
241 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000242#else
243 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100244 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000245#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100246 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000247 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000248 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100249 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000250 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000251 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000252 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100253 { "serveraliveinterval", oServerAliveInterval },
254 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000255 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000256 { "controlpath", oControlPath },
257 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000258 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100259 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100260 { "tunnel", oTunnel },
261 { "tunneldevice", oTunnelDevice },
262 { "localcommand", oLocalCommand },
263 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000264 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000265 { "useroaming", oUseRoaming },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000266 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100267 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000268 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000269 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100270 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100271 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
272 { "canonicalizehostname", oCanonicalizeHostname },
273 { "canonicalizemaxdots", oCanonicalizeMaxDots },
274 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000275 { "streamlocalbindmask", oStreamLocalBindMask },
276 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000277 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000278 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000279 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000280 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000281 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000282 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100283
Ben Lindstrom65366a82001-12-06 16:32:47 +0000284 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100285};
286
Damien Miller5428f641999-11-25 11:54:57 +1100287/*
288 * Adds a local TCP/IP port forward to options. Never returns if there is an
289 * error.
290 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
Damien Miller4af51302000-04-16 11:18:38 +1000292void
Damien Miller7acefbb2014-07-18 14:11:24 +1000293add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294{
Damien Miller7acefbb2014-07-18 14:11:24 +1000295 struct Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000296#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100297 extern uid_t original_real_uid;
Damien Miller7acefbb2014-07-18 14:11:24 +1000298 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
299 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000300 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100301#endif
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000302 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000303 options->num_local_forwards + 1,
304 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100305 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100306
Damien Miller1a0442f2008-11-05 16:30:06 +1100307 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100308 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000309 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100310 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100311 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000312 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313}
314
Damien Miller5428f641999-11-25 11:54:57 +1100315/*
316 * Adds a remote TCP/IP port forward to options. Never returns if there is
317 * an error.
318 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319
Damien Miller4af51302000-04-16 11:18:38 +1000320void
Damien Miller7acefbb2014-07-18 14:11:24 +1000321add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322{
Damien Miller7acefbb2014-07-18 14:11:24 +1000323 struct Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000324
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000325 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000326 options->num_remote_forwards + 1,
327 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100328 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100329
Damien Miller1a0442f2008-11-05 16:30:06 +1100330 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100331 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000332 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100333 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000335 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100336 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000337 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338}
339
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000340static void
341clear_forwardings(Options *options)
342{
343 int i;
344
Damien Millerf91ee4c2005-03-01 21:24:33 +1100345 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000346 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000347 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000348 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000349 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100350 }
Damien Miller232cfb12010-06-26 09:50:30 +1000351 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000352 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000353 options->local_forwards = NULL;
354 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000355 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100356 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000357 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000358 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000359 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000360 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100361 }
Damien Miller232cfb12010-06-26 09:50:30 +1000362 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000363 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000364 options->remote_forwards = NULL;
365 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000366 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100367 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000368}
369
Darren Tucker19104782013-04-05 11:13:08 +1100370void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000371add_certificate_file(Options *options, const char *path, int userprovided)
372{
373 int i;
374
375 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
376 fatal("Too many certificate files specified (max %d)",
377 SSH_MAX_CERTIFICATE_FILES);
378
379 /* Avoid registering duplicates */
380 for (i = 0; i < options->num_certificate_files; i++) {
381 if (options->certificate_file_userprovided[i] == userprovided &&
382 strcmp(options->certificate_files[i], path) == 0) {
383 debug2("%s: ignoring duplicate key %s", __func__, path);
384 return;
385 }
386 }
387
388 options->certificate_file_userprovided[options->num_certificate_files] =
389 userprovided;
390 options->certificate_files[options->num_certificate_files++] =
391 xstrdup(path);
392}
393
394void
Darren Tucker19104782013-04-05 11:13:08 +1100395add_identity_file(Options *options, const char *dir, const char *filename,
396 int userprovided)
397{
398 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000399 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100400
401 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
402 fatal("Too many identity files specified (max %d)",
403 SSH_MAX_IDENTITY_FILES);
404
405 if (dir == NULL) /* no dir, filename is absolute */
406 path = xstrdup(filename);
407 else
408 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
409
Damien Miller15271902014-05-15 13:47:56 +1000410 /* Avoid registering duplicates */
411 for (i = 0; i < options->num_identity_files; i++) {
412 if (options->identity_file_userprovided[i] == userprovided &&
413 strcmp(options->identity_files[i], path) == 0) {
414 debug2("%s: ignoring duplicate key %s", __func__, path);
415 free(path);
416 return;
417 }
418 }
419
Darren Tucker19104782013-04-05 11:13:08 +1100420 options->identity_file_userprovided[options->num_identity_files] =
421 userprovided;
422 options->identity_files[options->num_identity_files++] = path;
423}
424
Damien Miller194fd902013-10-15 12:13:05 +1100425int
426default_ssh_port(void)
427{
428 static int port;
429 struct servent *sp;
430
431 if (port == 0) {
432 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
433 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
434 }
435 return port;
436}
437
438/*
439 * Execute a command in a shell.
440 * Return its exit status or -1 on abnormal exit.
441 */
442static int
443execute_in_shell(const char *cmd)
444{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000445 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100446 pid_t pid;
447 int devnull, status;
448 extern uid_t original_real_uid;
449
450 if ((shell = getenv("SHELL")) == NULL)
451 shell = _PATH_BSHELL;
452
Damien Miller194fd902013-10-15 12:13:05 +1100453 /* Need this to redirect subprocess stdin/out */
454 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
455 fatal("open(/dev/null): %s", strerror(errno));
456
457 debug("Executing command: '%.500s'", cmd);
458
459 /* Fork and execute the command. */
460 if ((pid = fork()) == 0) {
461 char *argv[4];
462
463 /* Child. Permanently give up superuser privileges. */
464 permanently_drop_suid(original_real_uid);
465
466 /* Redirect child stdin and stdout. Leave stderr */
467 if (dup2(devnull, STDIN_FILENO) == -1)
468 fatal("dup2: %s", strerror(errno));
469 if (dup2(devnull, STDOUT_FILENO) == -1)
470 fatal("dup2: %s", strerror(errno));
471 if (devnull > STDERR_FILENO)
472 close(devnull);
473 closefrom(STDERR_FILENO + 1);
474
475 argv[0] = shell;
476 argv[1] = "-c";
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000477 argv[2] = cmd;
Damien Miller194fd902013-10-15 12:13:05 +1100478 argv[3] = NULL;
479
480 execv(argv[0], argv);
481 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
482 /* Die with signal to make this error apparent to parent. */
483 signal(SIGTERM, SIG_DFL);
484 kill(getpid(), SIGTERM);
485 _exit(1);
486 }
487 /* Parent. */
488 if (pid < 0)
489 fatal("%s: fork: %.100s", __func__, strerror(errno));
490
491 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100492
493 while (waitpid(pid, &status, 0) == -1) {
494 if (errno != EINTR && errno != EAGAIN)
495 fatal("%s: waitpid: %s", __func__, strerror(errno));
496 }
497 if (!WIFEXITED(status)) {
498 error("command '%.100s' exited abnormally", cmd);
499 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000500 }
Damien Miller194fd902013-10-15 12:13:05 +1100501 debug3("command returned status %d", WEXITSTATUS(status));
502 return WEXITSTATUS(status);
503}
504
505/*
506 * Parse and execute a Match directive.
507 */
508static int
509match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000510 const char *host_arg, const char *original_host, int post_canon,
511 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100512{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000513 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100514 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000515 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100516 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
517
518 /*
519 * Configuration is likely to be incomplete at this point so we
520 * must be prepared to use default values.
521 */
522 port = options->port <= 0 ? default_ssh_port() : options->port;
523 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100524 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100525 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100526 host = percent_expand(options->hostname,
527 "h", host_arg, (char *)NULL);
528 } else
529 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100530
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000531 debug2("checking match for '%s' host %s originally %s",
532 cp, host, original_host);
533 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
534 criteria = NULL;
535 this_result = 1;
536 if ((negate = attrib[0] == '!'))
537 attrib++;
538 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100539 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000540 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100541 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000542 error("%.200s line %d: '%s' cannot be combined "
543 "with other Match attributes",
544 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100545 result = -1;
546 goto out;
547 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 if (result)
549 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100550 goto out;
551 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000552 attributes++;
553 if (strcasecmp(attrib, "canonical") == 0) {
554 r = !!post_canon; /* force bitmask member to boolean */
555 if (r == (negate ? 1 : 0))
556 this_result = result = 0;
557 debug3("%.200s line %d: %smatched '%s'",
558 filename, linenum,
559 this_result ? "" : "not ", oattrib);
560 continue;
561 }
562 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100563 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
564 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100565 result = -1;
566 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100567 }
Damien Miller194fd902013-10-15 12:13:05 +1100568 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000569 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000570 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000571 if (r == (negate ? 1 : 0))
572 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100573 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000574 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000575 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000576 if (r == (negate ? 1 : 0))
577 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100578 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000580 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000581 if (r == (negate ? 1 : 0))
582 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100583 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000585 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000586 if (r == (negate ? 1 : 0))
587 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100588 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100589 if (gethostname(thishost, sizeof(thishost)) == -1)
590 fatal("gethostname: %s", strerror(errno));
591 strlcpy(shorthost, thishost, sizeof(shorthost));
592 shorthost[strcspn(thishost, ".")] = '\0';
593 snprintf(portstr, sizeof(portstr), "%d", port);
594
595 cmd = percent_expand(arg,
596 "L", shorthost,
597 "d", pw->pw_dir,
598 "h", host,
599 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000600 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100601 "p", portstr,
602 "r", ruser,
603 "u", pw->pw_name,
604 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100605 if (result != 1) {
606 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000607 debug3("%.200s line %d: skipped exec "
608 "\"%.100s\"", filename, linenum, cmd);
609 free(cmd);
610 continue;
Damien Miller06287802014-02-24 15:56:45 +1100611 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000612 r = execute_in_shell(cmd);
613 if (r == -1) {
614 fatal("%.200s line %d: match exec "
615 "'%.100s' error", filename,
616 linenum, cmd);
617 }
618 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100619 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000620 /* Force exit status to boolean */
621 r = r == 0;
622 if (r == (negate ? 1 : 0))
623 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100624 } else {
625 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100626 result = -1;
627 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100628 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000629 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
630 filename, linenum, this_result ? "": "not ",
631 oattrib, criteria);
632 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100633 }
Damien Millercf31f382013-10-24 21:02:56 +1100634 if (attributes == 0) {
635 error("One or more attributes required for Match");
636 result = -1;
637 goto out;
638 }
Damien Miller084bcd22013-10-23 16:30:51 +1100639 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000640 if (result != -1)
641 debug2("match %sfound", result ? "" : "not ");
642 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100643 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100644 return result;
645}
646
Damien Miller0faf7472013-10-17 11:47:23 +1100647/* Check and prepare a domain name: removes trailing '.' and lowercases */
648static void
649valid_domain(char *name, const char *filename, int linenum)
650{
651 size_t i, l = strlen(name);
652 u_char c, last = '\0';
653
654 if (l == 0)
655 fatal("%s line %d: empty hostname suffix", filename, linenum);
656 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
657 fatal("%s line %d: hostname suffix \"%.100s\" "
658 "starts with invalid character", filename, linenum, name);
659 for (i = 0; i < l; i++) {
660 c = tolower((u_char)name[i]);
661 name[i] = (char)c;
662 if (last == '.' && c == '.')
663 fatal("%s line %d: hostname suffix \"%.100s\" contains "
664 "consecutive separators", filename, linenum, name);
665 if (c != '.' && c != '-' && !isalnum(c) &&
666 c != '_') /* technically invalid, but common */
667 fatal("%s line %d: hostname suffix \"%.100s\" contains "
668 "invalid characters", filename, linenum, name);
669 last = c;
670 }
671 if (name[l - 1] == '.')
672 name[l - 1] = '\0';
673}
674
Damien Miller5428f641999-11-25 11:54:57 +1100675/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000676 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100677 */
Damien Miller4af51302000-04-16 11:18:38 +1000678static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000679parse_token(const char *cp, const char *filename, int linenum,
680 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681{
Darren Tucker07636982013-05-16 20:30:03 +1000682 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683
Damien Miller95def091999-11-25 00:26:21 +1100684 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000685 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100686 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000687 if (ignored_unknown != NULL &&
688 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000689 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000690 error("%s: line %d: Bad configuration option: %s",
691 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100692 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693}
694
Damien Millere9fc72e2013-10-15 12:14:12 +1100695/* Multistate option parsing */
696struct multistate {
697 char *key;
698 int value;
699};
700static const struct multistate multistate_flag[] = {
701 { "true", 1 },
702 { "false", 0 },
703 { "yes", 1 },
704 { "no", 0 },
705 { NULL, -1 }
706};
707static const struct multistate multistate_yesnoask[] = {
708 { "true", 1 },
709 { "false", 0 },
710 { "yes", 1 },
711 { "no", 0 },
712 { "ask", 2 },
713 { NULL, -1 }
714};
715static const struct multistate multistate_addressfamily[] = {
716 { "inet", AF_INET },
717 { "inet6", AF_INET6 },
718 { "any", AF_UNSPEC },
719 { NULL, -1 }
720};
721static const struct multistate multistate_controlmaster[] = {
722 { "true", SSHCTL_MASTER_YES },
723 { "yes", SSHCTL_MASTER_YES },
724 { "false", SSHCTL_MASTER_NO },
725 { "no", SSHCTL_MASTER_NO },
726 { "auto", SSHCTL_MASTER_AUTO },
727 { "ask", SSHCTL_MASTER_ASK },
728 { "autoask", SSHCTL_MASTER_AUTO_ASK },
729 { NULL, -1 }
730};
731static const struct multistate multistate_tunnel[] = {
732 { "ethernet", SSH_TUNMODE_ETHERNET },
733 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
734 { "true", SSH_TUNMODE_DEFAULT },
735 { "yes", SSH_TUNMODE_DEFAULT },
736 { "false", SSH_TUNMODE_NO },
737 { "no", SSH_TUNMODE_NO },
738 { NULL, -1 }
739};
740static const struct multistate multistate_requesttty[] = {
741 { "true", REQUEST_TTY_YES },
742 { "yes", REQUEST_TTY_YES },
743 { "false", REQUEST_TTY_NO },
744 { "no", REQUEST_TTY_NO },
745 { "force", REQUEST_TTY_FORCE },
746 { "auto", REQUEST_TTY_AUTO },
747 { NULL, -1 }
748};
Damien Miller38505592013-10-17 11:48:13 +1100749static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100750 { "true", SSH_CANONICALISE_YES },
751 { "false", SSH_CANONICALISE_NO },
752 { "yes", SSH_CANONICALISE_YES },
753 { "no", SSH_CANONICALISE_NO },
754 { "always", SSH_CANONICALISE_ALWAYS },
755 { NULL, -1 }
756};
Damien Millere9fc72e2013-10-15 12:14:12 +1100757
Damien Miller5428f641999-11-25 11:54:57 +1100758/*
759 * Processes a single option line as used in the configuration files. This
760 * only sets those values that have not already been set.
761 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100762#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100763int
Damien Miller194fd902013-10-15 12:13:05 +1100764process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000765 const char *original_host, char *line, const char *filename,
766 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767{
Damien Miller295ee632011-05-29 21:42:31 +1000768 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
769 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000770 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100771 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100772 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000773 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100774 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000775 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100776 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100777 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Miller194fd902013-10-15 12:13:05 +1100779 if (activep == NULL) { /* We are processing a command line directive */
780 cmdline = 1;
781 activep = &cmdline;
782 }
783
Damien Millerc652cac2003-05-14 13:40:54 +1000784 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000785 if ((len = strlen(line)) == 0)
786 return 0;
787 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000788 if (strchr(WHITESPACE, line[len]) == NULL)
789 break;
790 line[len] = '\0';
791 }
792
Damien Millerbe484b52000-07-15 14:14:16 +1000793 s = line;
794 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100795 if ((keyword = strdelim(&s)) == NULL)
796 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000797 /* Ignore leading whitespace. */
798 if (*keyword == '\0')
799 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000800 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100801 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000802 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100803 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804
Darren Tucker07636982013-05-16 20:30:03 +1000805 opcode = parse_token(keyword, filename, linenum,
806 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807
Damien Miller95def091999-11-25 00:26:21 +1100808 switch (opcode) {
809 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100810 /* don't panic, but count bad options */
811 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100812 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000813 case oIgnoredUnknownOption:
814 debug("%s line %d: Ignored unknown option \"%s\"",
815 filename, linenum, keyword);
816 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000817 case oConnectTimeout:
818 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100819parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000820 arg = strdelim(&s);
821 if (!arg || *arg == '\0')
822 fatal("%s line %d: missing time value.",
823 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000824 if (strcmp(arg, "none") == 0)
825 value = -1;
826 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000827 fatal("%s line %d: invalid time value.",
828 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100829 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000830 *intptr = value;
831 break;
832
Damien Miller95def091999-11-25 00:26:21 +1100833 case oForwardAgent:
834 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100835 parse_flag:
836 multistate_ptr = multistate_flag;
837 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000838 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000839 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100840 fatal("%s line %d: missing argument.",
841 filename, linenum);
842 value = -1;
843 for (i = 0; multistate_ptr[i].key != NULL; i++) {
844 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
845 value = multistate_ptr[i].value;
846 break;
847 }
848 }
849 if (value == -1)
850 fatal("%s line %d: unsupported option \"%s\".",
851 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100852 if (*activep && *intptr == -1)
853 *intptr = value;
854 break;
855
856 case oForwardX11:
857 intptr = &options->forward_x11;
858 goto parse_flag;
859
Darren Tucker0a118da2003-10-15 15:54:32 +1000860 case oForwardX11Trusted:
861 intptr = &options->forward_x11_trusted;
862 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000863
Damien Miller1ab6a512010-06-26 10:02:24 +1000864 case oForwardX11Timeout:
865 intptr = &options->forward_x11_timeout;
866 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000867
Damien Miller95def091999-11-25 00:26:21 +1100868 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000869 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100870 goto parse_flag;
871
Darren Tuckere7d4b192006-07-12 22:17:10 +1000872 case oExitOnForwardFailure:
873 intptr = &options->exit_on_forward_failure;
874 goto parse_flag;
875
Damien Miller95def091999-11-25 00:26:21 +1100876 case oUsePrivilegedPort:
877 intptr = &options->use_privileged_port;
878 goto parse_flag;
879
Damien Miller95def091999-11-25 00:26:21 +1100880 case oPasswordAuthentication:
881 intptr = &options->password_authentication;
882 goto parse_flag;
883
Damien Miller874d77b2000-10-14 16:23:11 +1100884 case oKbdInteractiveAuthentication:
885 intptr = &options->kbd_interactive_authentication;
886 goto parse_flag;
887
888 case oKbdInteractiveDevices:
889 charptr = &options->kbd_interactive_devices;
890 goto parse_string;
891
Damien Miller0bc1bd82000-11-13 22:57:25 +1100892 case oPubkeyAuthentication:
893 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000894 goto parse_flag;
895
Damien Miller95def091999-11-25 00:26:21 +1100896 case oRSAAuthentication:
897 intptr = &options->rsa_authentication;
898 goto parse_flag;
899
900 case oRhostsRSAAuthentication:
901 intptr = &options->rhosts_rsa_authentication;
902 goto parse_flag;
903
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000904 case oHostbasedAuthentication:
905 intptr = &options->hostbased_authentication;
906 goto parse_flag;
907
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000908 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000909 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100910 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000911
Darren Tucker0efd1552003-08-26 11:49:55 +1000912 case oGssAuthentication:
913 intptr = &options->gss_authentication;
914 goto parse_flag;
915
916 case oGssDelegateCreds:
917 intptr = &options->gss_deleg_creds;
918 goto parse_flag;
919
Damien Miller95def091999-11-25 00:26:21 +1100920 case oBatchMode:
921 intptr = &options->batch_mode;
922 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000923
Damien Miller95def091999-11-25 00:26:21 +1100924 case oCheckHostIP:
925 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000926 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927
Damien Miller37876e92003-05-15 10:19:46 +1000928 case oVerifyHostKeyDNS:
929 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100930 multistate_ptr = multistate_yesnoask;
931 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000932
Damien Miller95def091999-11-25 00:26:21 +1100933 case oStrictHostKeyChecking:
934 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100935 multistate_ptr = multistate_yesnoask;
936 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937
Damien Miller95def091999-11-25 00:26:21 +1100938 case oCompression:
939 intptr = &options->compression;
940 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
Damien Miller12c150e2003-12-17 16:31:10 +1100942 case oTCPKeepAlive:
943 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100944 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000946 case oNoHostAuthenticationForLocalhost:
947 intptr = &options->no_host_authentication_for_localhost;
948 goto parse_flag;
949
Damien Miller95def091999-11-25 00:26:21 +1100950 case oNumberOfPasswordPrompts:
951 intptr = &options->number_of_password_prompts;
952 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
Damien Miller95def091999-11-25 00:26:21 +1100954 case oCompressionLevel:
955 intptr = &options->compression_level;
956 goto parse_int;
957
Damien Millera5539d22003-04-09 20:50:06 +1000958 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000959 arg = strdelim(&s);
960 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000961 fatal("%.200s line %d: Missing argument.", filename,
962 linenum);
963 if (strcmp(arg, "default") == 0) {
964 val64 = 0;
965 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000966 if (scan_scaled(arg, &val64) == -1)
967 fatal("%.200s line %d: Bad number '%s': %s",
968 filename, linenum, arg, strerror(errno));
969 /* check for too-large or too-small limits */
970 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000971 fatal("%.200s line %d: RekeyLimit too large",
972 filename, linenum);
973 if (val64 != 0 && val64 < 16)
974 fatal("%.200s line %d: RekeyLimit too small",
975 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000976 }
Damien Miller3dff1762008-02-10 22:25:52 +1100977 if (*activep && options->rekey_limit == -1)
978 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000979 if (s != NULL) { /* optional rekey interval present */
980 if (strcmp(s, "none") == 0) {
981 (void)strdelim(&s); /* discard */
982 break;
983 }
984 intptr = &options->rekey_interval;
985 goto parse_time;
986 }
Damien Millera5539d22003-04-09 20:50:06 +1000987 break;
988
Damien Miller95def091999-11-25 00:26:21 +1100989 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000990 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000991 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100992 fatal("%.200s line %d: Missing argument.", filename, linenum);
993 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100994 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000995 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100996 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100997 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000998 add_identity_file(options, NULL,
999 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001000 }
1001 break;
1002
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001003 case oCertificateFile:
1004 arg = strdelim(&s);
1005 if (!arg || *arg == '\0')
1006 fatal("%.200s line %d: Missing argument.",
1007 filename, linenum);
1008 if (*activep) {
1009 intptr = &options->num_certificate_files;
1010 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1011 fatal("%.200s line %d: Too many certificate "
1012 "files specified (max %d).",
1013 filename, linenum,
1014 SSH_MAX_CERTIFICATE_FILES);
1015 }
1016 add_certificate_file(options, arg,
1017 flags & SSHCONF_USERCONF);
1018 }
1019 break;
1020
Damien Millerd3a18572000-06-07 19:55:44 +10001021 case oXAuthLocation:
1022 charptr=&options->xauth_location;
1023 goto parse_string;
1024
Damien Miller95def091999-11-25 00:26:21 +11001025 case oUser:
1026 charptr = &options->user;
1027parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001028 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001029 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001030 fatal("%.200s line %d: Missing argument.",
1031 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001032 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001033 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001034 break;
1035
1036 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001037 cpptr = (char **)&options->system_hostfiles;
1038 uintptr = &options->num_system_hostfiles;
1039 max_entries = SSH_MAX_HOSTS_FILES;
1040parse_char_array:
1041 if (*activep && *uintptr == 0) {
1042 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1043 if ((*uintptr) >= max_entries)
1044 fatal("%s line %d: "
1045 "too many authorized keys files.",
1046 filename, linenum);
1047 cpptr[(*uintptr)++] = xstrdup(arg);
1048 }
1049 }
1050 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001051
1052 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001053 cpptr = (char **)&options->user_hostfiles;
1054 uintptr = &options->num_user_hostfiles;
1055 max_entries = SSH_MAX_HOSTS_FILES;
1056 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001057
Damien Miller95def091999-11-25 00:26:21 +11001058 case oHostName:
1059 charptr = &options->hostname;
1060 goto parse_string;
1061
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001062 case oHostKeyAlias:
1063 charptr = &options->host_key_alias;
1064 goto parse_string;
1065
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001066 case oPreferredAuthentications:
1067 charptr = &options->preferred_authentications;
1068 goto parse_string;
1069
Ben Lindstrome0f88042001-04-30 13:06:24 +00001070 case oBindAddress:
1071 charptr = &options->bind_address;
1072 goto parse_string;
1073
Damien Miller7ea845e2010-02-12 09:21:02 +11001074 case oPKCS11Provider:
1075 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001076 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001077
Damien Miller95def091999-11-25 00:26:21 +11001078 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001079 charptr = &options->proxy_command;
1080parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001081 if (s == NULL)
1082 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001083 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001084 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001085 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001086 return 0;
1087
1088 case oPort:
1089 intptr = &options->port;
1090parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001091 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001092 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001093 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001094 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001095 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001096
1097 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001098 value = strtol(arg, &endofnumber, 0);
1099 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001100 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001101 if (*activep && *intptr == -1)
1102 *intptr = value;
1103 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104
Damien Miller95def091999-11-25 00:26:21 +11001105 case oConnectionAttempts:
1106 intptr = &options->connection_attempts;
1107 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001108
Damien Miller95def091999-11-25 00:26:21 +11001109 case oCipher:
1110 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001111 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001112 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001113 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001114 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001115 if (value == -1)
1116 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001117 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001118 if (*activep && *intptr == -1)
1119 *intptr = value;
1120 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121
Damien Miller78928792000-04-12 20:17:38 +10001122 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001123 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001124 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001125 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001126 if (!ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001127 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001128 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001129 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001130 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001131 break;
1132
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001133 case oMacs:
1134 arg = strdelim(&s);
1135 if (!arg || *arg == '\0')
1136 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001137 if (!mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001138 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001139 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001140 if (*activep && options->macs == NULL)
1141 options->macs = xstrdup(arg);
1142 break;
1143
Damien Millerd5f62bf2010-09-24 22:11:14 +10001144 case oKexAlgorithms:
1145 arg = strdelim(&s);
1146 if (!arg || *arg == '\0')
1147 fatal("%.200s line %d: Missing argument.",
1148 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001149 if (!kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001150 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1151 filename, linenum, arg ? arg : "<NONE>");
1152 if (*activep && options->kex_algorithms == NULL)
1153 options->kex_algorithms = xstrdup(arg);
1154 break;
1155
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001156 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001157 charptr = &options->hostkeyalgorithms;
1158parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001159 arg = strdelim(&s);
1160 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001161 fatal("%.200s line %d: Missing argument.",
1162 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001163 if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001164 fatal("%s line %d: Bad key types '%s'.",
1165 filename, linenum, arg ? arg : "<NONE>");
1166 if (*activep && *charptr == NULL)
1167 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001168 break;
1169
Damien Miller78928792000-04-12 20:17:38 +10001170 case oProtocol:
1171 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001172 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001173 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001174 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001175 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001176 if (value == SSH_PROTO_UNKNOWN)
1177 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001178 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001179 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1180 *intptr = value;
1181 break;
1182
Damien Miller95def091999-11-25 00:26:21 +11001183 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001184 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001185 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001186 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001187 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001188 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001189 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001190 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1191 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001192 break;
1193
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001194 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001195 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001196 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001197 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001198 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001199 fatal("%.200s line %d: Missing port argument.",
1200 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001201
Damien Millera699d952008-11-03 19:27:34 +11001202 if (opcode == oLocalForward ||
1203 opcode == oRemoteForward) {
1204 arg2 = strdelim(&s);
1205 if (arg2 == NULL || *arg2 == '\0')
1206 fatal("%.200s line %d: Missing target argument.",
1207 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001208
Damien Millera699d952008-11-03 19:27:34 +11001209 /* construct a string for parse_forward */
1210 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1211 } else if (opcode == oDynamicForward) {
1212 strlcpy(fwdarg, arg, sizeof(fwdarg));
1213 }
1214
1215 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001216 opcode == oDynamicForward ? 1 : 0,
1217 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001218 fatal("%.200s line %d: Bad forwarding specification.",
1219 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001220
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001221 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001222 if (opcode == oLocalForward ||
1223 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001224 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001225 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001226 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001227 }
Damien Miller95def091999-11-25 00:26:21 +11001228 break;
1229
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001230 case oClearAllForwardings:
1231 intptr = &options->clear_forwardings;
1232 goto parse_flag;
1233
Damien Miller95def091999-11-25 00:26:21 +11001234 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001235 if (cmdline)
1236 fatal("Host directive not supported as a command-line "
1237 "option");
Damien Miller95def091999-11-25 00:26:21 +11001238 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001239 arg2 = NULL;
1240 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1241 negated = *arg == '!';
1242 if (negated)
1243 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001244 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001245 if (negated) {
1246 debug("%.200s line %d: Skipping Host "
1247 "block because of negated match "
1248 "for %.100s", filename, linenum,
1249 arg);
1250 *activep = 0;
1251 break;
1252 }
1253 if (!*activep)
1254 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001255 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001256 }
Damien Millerfe924212011-05-15 08:44:45 +10001257 }
1258 if (*activep)
1259 debug("%.200s line %d: Applying options for %.100s",
1260 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001261 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001262 return 0;
1263
Damien Miller194fd902013-10-15 12:13:05 +11001264 case oMatch:
1265 if (cmdline)
1266 fatal("Host directive not supported as a command-line "
1267 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001268 value = match_cfg_line(options, &s, pw, host, original_host,
1269 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001270 if (value < 0)
1271 fatal("%.200s line %d: Bad Match condition", filename,
1272 linenum);
1273 *activep = value;
1274 break;
1275
Damien Miller95def091999-11-25 00:26:21 +11001276 case oEscapeChar:
1277 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001278 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001279 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001280 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001281 if (strcmp(arg, "none") == 0)
1282 value = SSH_ESCAPECHAR_NONE;
1283 else if (arg[1] == '\0')
1284 value = (u_char) arg[0];
1285 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001286 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1287 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001288 else {
1289 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001290 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001291 /* NOTREACHED */
1292 value = 0; /* Avoid compiler warning. */
1293 }
1294 if (*activep && *intptr == -1)
1295 *intptr = value;
1296 break;
1297
Damien Miller20a8f972003-05-18 20:50:30 +10001298 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001299 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001300 multistate_ptr = multistate_addressfamily;
1301 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001302
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001303 case oEnableSSHKeysign:
1304 intptr = &options->enable_ssh_keysign;
1305 goto parse_flag;
1306
Damien Millerbd394c32004-03-08 23:12:36 +11001307 case oIdentitiesOnly:
1308 intptr = &options->identities_only;
1309 goto parse_flag;
1310
Damien Miller509b0102003-12-17 16:33:10 +11001311 case oServerAliveInterval:
1312 intptr = &options->server_alive_interval;
1313 goto parse_time;
1314
1315 case oServerAliveCountMax:
1316 intptr = &options->server_alive_count_max;
1317 goto parse_int;
1318
Darren Tucker46bc0752004-05-02 22:11:30 +10001319 case oSendEnv:
1320 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1321 if (strchr(arg, '=') != NULL)
1322 fatal("%s line %d: Invalid environment name.",
1323 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001324 if (!*activep)
1325 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001326 if (options->num_send_env >= MAX_SEND_ENV)
1327 fatal("%s line %d: too many send env.",
1328 filename, linenum);
1329 options->send_env[options->num_send_env++] =
1330 xstrdup(arg);
1331 }
1332 break;
1333
Damien Miller0e220db2004-06-15 10:34:08 +10001334 case oControlPath:
1335 charptr = &options->control_path;
1336 goto parse_string;
1337
1338 case oControlMaster:
1339 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001340 multistate_ptr = multistate_controlmaster;
1341 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001342
Damien Millere11e1ea2010-08-03 16:04:46 +10001343 case oControlPersist:
1344 /* no/false/yes/true, or a time spec */
1345 intptr = &options->control_persist;
1346 arg = strdelim(&s);
1347 if (!arg || *arg == '\0')
1348 fatal("%.200s line %d: Missing ControlPersist"
1349 " argument.", filename, linenum);
1350 value = 0;
1351 value2 = 0; /* timeout */
1352 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1353 value = 0;
1354 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1355 value = 1;
1356 else if ((value2 = convtime(arg)) >= 0)
1357 value = 1;
1358 else
1359 fatal("%.200s line %d: Bad ControlPersist argument.",
1360 filename, linenum);
1361 if (*activep && *intptr == -1) {
1362 *intptr = value;
1363 options->control_persist_timeout = value2;
1364 }
1365 break;
1366
Damien Millere1776152005-03-01 21:47:37 +11001367 case oHashKnownHosts:
1368 intptr = &options->hash_known_hosts;
1369 goto parse_flag;
1370
Damien Millerd27b9472005-12-13 19:29:02 +11001371 case oTunnel:
1372 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001373 multistate_ptr = multistate_tunnel;
1374 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001375
1376 case oTunnelDevice:
1377 arg = strdelim(&s);
1378 if (!arg || *arg == '\0')
1379 fatal("%.200s line %d: Missing argument.", filename, linenum);
1380 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001381 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001382 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1383 if (*activep) {
1384 options->tun_local = value;
1385 options->tun_remote = value2;
1386 }
1387 break;
1388
1389 case oLocalCommand:
1390 charptr = &options->local_command;
1391 goto parse_command;
1392
1393 case oPermitLocalCommand:
1394 intptr = &options->permit_local_command;
1395 goto parse_flag;
1396
Damien Miller10288242008-06-30 00:04:03 +10001397 case oVisualHostKey:
1398 intptr = &options->visual_host_key;
1399 goto parse_flag;
1400
Damien Miller0dac6fb2010-11-20 15:19:38 +11001401 case oIPQoS:
1402 arg = strdelim(&s);
1403 if ((value = parse_ipqos(arg)) == -1)
1404 fatal("%s line %d: Bad IPQoS value: %s",
1405 filename, linenum, arg);
1406 arg = strdelim(&s);
1407 if (arg == NULL)
1408 value2 = value;
1409 else if ((value2 = parse_ipqos(arg)) == -1)
1410 fatal("%s line %d: Bad IPQoS value: %s",
1411 filename, linenum, arg);
1412 if (*activep) {
1413 options->ip_qos_interactive = value;
1414 options->ip_qos_bulk = value2;
1415 }
1416 break;
1417
Darren Tucker71e4d542009-07-06 07:12:27 +10001418 case oUseRoaming:
1419 intptr = &options->use_roaming;
1420 goto parse_flag;
1421
Damien Miller21771e22011-05-15 08:45:50 +10001422 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001423 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001424 multistate_ptr = multistate_requesttty;
1425 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001426
Darren Tucker07636982013-05-16 20:30:03 +10001427 case oIgnoreUnknown:
1428 charptr = &options->ignored_unknown;
1429 goto parse_string;
1430
Damien Miller1262b662013-08-21 02:44:24 +10001431 case oProxyUseFdpass:
1432 intptr = &options->proxy_use_fdpass;
1433 goto parse_flag;
1434
Damien Miller0faf7472013-10-17 11:47:23 +11001435 case oCanonicalDomains:
1436 value = options->num_canonical_domains != 0;
1437 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1438 valid_domain(arg, filename, linenum);
1439 if (!*activep || value)
1440 continue;
1441 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1442 fatal("%s line %d: too many hostname suffixes.",
1443 filename, linenum);
1444 options->canonical_domains[
1445 options->num_canonical_domains++] = xstrdup(arg);
1446 }
1447 break;
1448
Damien Miller38505592013-10-17 11:48:13 +11001449 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001450 value = options->num_permitted_cnames != 0;
1451 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1452 /* Either '*' for everything or 'list:list' */
1453 if (strcmp(arg, "*") == 0)
1454 arg2 = arg;
1455 else {
1456 lowercase(arg);
1457 if ((arg2 = strchr(arg, ':')) == NULL ||
1458 arg2[1] == '\0') {
1459 fatal("%s line %d: "
1460 "Invalid permitted CNAME \"%s\"",
1461 filename, linenum, arg);
1462 }
1463 *arg2 = '\0';
1464 arg2++;
1465 }
1466 if (!*activep || value)
1467 continue;
1468 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1469 fatal("%s line %d: too many permitted CNAMEs.",
1470 filename, linenum);
1471 cname = options->permitted_cnames +
1472 options->num_permitted_cnames++;
1473 cname->source_list = xstrdup(arg);
1474 cname->target_list = xstrdup(arg2);
1475 }
1476 break;
1477
Damien Miller38505592013-10-17 11:48:13 +11001478 case oCanonicalizeHostname:
1479 intptr = &options->canonicalize_hostname;
1480 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001481 goto parse_multistate;
1482
Damien Miller38505592013-10-17 11:48:13 +11001483 case oCanonicalizeMaxDots:
1484 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001485 goto parse_int;
1486
Damien Miller38505592013-10-17 11:48:13 +11001487 case oCanonicalizeFallbackLocal:
1488 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001489 goto parse_flag;
1490
Damien Miller7acefbb2014-07-18 14:11:24 +10001491 case oStreamLocalBindMask:
1492 arg = strdelim(&s);
1493 if (!arg || *arg == '\0')
1494 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1495 /* Parse mode in octal format */
1496 value = strtol(arg, &endofnumber, 8);
1497 if (arg == endofnumber || value < 0 || value > 0777)
1498 fatal("%.200s line %d: Bad mask.", filename, linenum);
1499 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1500 break;
1501
1502 case oStreamLocalBindUnlink:
1503 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1504 goto parse_flag;
1505
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001506 case oRevokedHostKeys:
1507 charptr = &options->revoked_host_keys;
1508 goto parse_string;
1509
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001510 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001511 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001512 arg = strdelim(&s);
1513 if (!arg || *arg == '\0')
1514 fatal("%.200s line %d: Missing argument.",
1515 filename, linenum);
1516 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1517 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1518 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001519 if (*activep && *intptr == -1)
1520 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001521 break;
1522
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001523 case oUpdateHostkeys:
1524 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001525 multistate_ptr = multistate_yesnoask;
1526 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001527
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001528 case oHostbasedKeyTypes:
1529 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001530 goto parse_keytypes;
1531
1532 case oPubkeyAcceptedKeyTypes:
1533 charptr = &options->pubkey_key_types;
1534 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001535
Ben Lindstrom4daea862002-06-09 20:04:02 +00001536 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001537 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001538 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001539 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001540
Damien Millerf9b3feb2003-05-16 11:38:32 +10001541 case oUnsupported:
1542 error("%s line %d: Unsupported option \"%s\"",
1543 filename, linenum, keyword);
1544 return 0;
1545
Damien Miller95def091999-11-25 00:26:21 +11001546 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001547 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001548 }
1549
1550 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001551 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001552 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001553 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001554 }
Damien Miller95def091999-11-25 00:26:21 +11001555 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556}
1557
1558
Damien Miller5428f641999-11-25 11:54:57 +11001559/*
1560 * Reads the config file and modifies the options accordingly. Options
1561 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001562 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001563 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001565int
Damien Miller194fd902013-10-15 12:13:05 +11001566read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001567 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001568{
Damien Miller95def091999-11-25 00:26:21 +11001569 FILE *f;
1570 char line[1024];
1571 int active, linenum;
1572 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573
Damien Miller57a44762004-04-20 20:11:57 +10001574 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001575 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001576
Darren Tuckeraefa3682013-04-05 11:18:35 +11001577 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001578 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001579
Damien Miller33793852004-06-15 10:27:55 +10001580 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001581 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001582 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001583 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001584 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001585 }
1586
Damien Miller95def091999-11-25 00:26:21 +11001587 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001588
Damien Miller5428f641999-11-25 11:54:57 +11001589 /*
1590 * Mark that we are now processing the options. This flag is turned
1591 * on/off by Host specifications.
1592 */
Damien Miller95def091999-11-25 00:26:21 +11001593 active = 1;
1594 linenum = 0;
1595 while (fgets(line, sizeof(line), f)) {
1596 /* Update line number counter. */
1597 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001598 if (process_config_line(options, pw, host, original_host,
1599 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001600 bad_options++;
1601 }
1602 fclose(f);
1603 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001604 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001605 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001606 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607}
1608
Damien Miller13f97b22014-02-24 15:57:55 +11001609/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1610int
1611option_clear_or_none(const char *o)
1612{
1613 return o == NULL || strcasecmp(o, "none") == 0;
1614}
1615
Damien Miller5428f641999-11-25 11:54:57 +11001616/*
1617 * Initializes options to special values that indicate that they have not yet
1618 * been set. Read_config_file will only set options with this value. Options
1619 * are processed in the following order: command line, user config file,
1620 * system config file. Last, fill_default_options is called.
1621 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001622
Damien Miller4af51302000-04-16 11:18:38 +10001623void
Damien Miller95def091999-11-25 00:26:21 +11001624initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625{
Damien Miller95def091999-11-25 00:26:21 +11001626 memset(options, 'X', sizeof(*options));
1627 options->forward_agent = -1;
1628 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001629 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001630 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001631 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001632 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001633 options->fwd_opts.gateway_ports = -1;
1634 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1635 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001636 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001637 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001638 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001639 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001640 options->gss_authentication = -1;
1641 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001642 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001643 options->kbd_interactive_authentication = -1;
1644 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001645 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001646 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001647 options->batch_mode = -1;
1648 options->check_host_ip = -1;
1649 options->strict_host_key_checking = -1;
1650 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001651 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001652 options->compression_level = -1;
1653 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001654 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001655 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001656 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001657 options->number_of_password_prompts = -1;
1658 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001659 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001660 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001661 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001662 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001663 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001664 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001665 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001666 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001667 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001668 options->proxy_command = NULL;
1669 options->user = NULL;
1670 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001671 options->num_system_hostfiles = 0;
1672 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001673 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001674 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001675 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001676 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001677 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001678 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001679 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001680 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001681 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001682 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001683 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001684 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001685 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001686 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001687 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001688 options->server_alive_interval = -1;
1689 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001690 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001691 options->control_path = NULL;
1692 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001693 options->control_persist = -1;
1694 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001695 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001696 options->tun_open = -1;
1697 options->tun_local = -1;
1698 options->tun_remote = -1;
1699 options->local_command = NULL;
1700 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001701 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001702 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001703 options->ip_qos_interactive = -1;
1704 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001705 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001706 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001707 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001708 options->num_canonical_domains = 0;
1709 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001710 options->canonicalize_max_dots = -1;
1711 options->canonicalize_fallback_local = -1;
1712 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001713 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001714 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001715 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001716 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001717 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001718}
1719
Damien Miller5428f641999-11-25 11:54:57 +11001720/*
Damien Miller13f97b22014-02-24 15:57:55 +11001721 * A petite version of fill_default_options() that just fills the options
1722 * needed for hostname canonicalization to proceed.
1723 */
1724void
1725fill_default_options_for_canonicalization(Options *options)
1726{
1727 if (options->canonicalize_max_dots == -1)
1728 options->canonicalize_max_dots = 1;
1729 if (options->canonicalize_fallback_local == -1)
1730 options->canonicalize_fallback_local = 1;
1731 if (options->canonicalize_hostname == -1)
1732 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1733}
1734
1735/*
Damien Miller5428f641999-11-25 11:54:57 +11001736 * Called after processing other sources of option data, this fills those
1737 * options for which no value has been specified with their default values.
1738 */
Damien Miller4af51302000-04-16 11:18:38 +10001739void
Damien Miller95def091999-11-25 00:26:21 +11001740fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001741{
Damien Miller95def091999-11-25 00:26:21 +11001742 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001743 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001744 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001745 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001746 if (options->forward_x11_trusted == -1)
1747 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001748 if (options->forward_x11_timeout == -1)
1749 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001750 if (options->exit_on_forward_failure == -1)
1751 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001752 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001753 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001754 if (options->fwd_opts.gateway_ports == -1)
1755 options->fwd_opts.gateway_ports = 0;
1756 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1757 options->fwd_opts.streamlocal_bind_mask = 0177;
1758 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1759 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001760 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001761 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001762 if (options->rsa_authentication == -1)
1763 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001764 if (options->pubkey_authentication == -1)
1765 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001766 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001767 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001768 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001769 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001770 if (options->gss_deleg_creds == -1)
1771 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001772 if (options->password_authentication == -1)
1773 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001774 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001775 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001776 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001777 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001778 if (options->hostbased_authentication == -1)
1779 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001780 if (options->batch_mode == -1)
1781 options->batch_mode = 0;
1782 if (options->check_host_ip == -1)
1783 options->check_host_ip = 1;
1784 if (options->strict_host_key_checking == -1)
1785 options->strict_host_key_checking = 2; /* 2 is default */
1786 if (options->compression == -1)
1787 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001788 if (options->tcp_keep_alive == -1)
1789 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001790 if (options->compression_level == -1)
1791 options->compression_level = 6;
1792 if (options->port == -1)
1793 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001794 if (options->address_family == -1)
1795 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001796 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001797 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001798 if (options->number_of_password_prompts == -1)
1799 options->number_of_password_prompts = 3;
1800 /* Selected in ssh_login(). */
1801 if (options->cipher == -1)
1802 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001803 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001804 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001805 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001806 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001807 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001808 add_identity_file(options, "~/",
1809 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001810 }
1811 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001812 add_identity_file(options, "~/",
1813 _PATH_SSH_CLIENT_ID_RSA, 0);
1814 add_identity_file(options, "~/",
1815 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001816#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001817 add_identity_file(options, "~/",
1818 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001819#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001820 add_identity_file(options, "~/",
1821 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001822 }
Damien Millereba71ba2000-04-29 23:57:08 +10001823 }
Damien Miller95def091999-11-25 00:26:21 +11001824 if (options->escape_char == -1)
1825 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001826 if (options->num_system_hostfiles == 0) {
1827 options->system_hostfiles[options->num_system_hostfiles++] =
1828 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1829 options->system_hostfiles[options->num_system_hostfiles++] =
1830 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1831 }
1832 if (options->num_user_hostfiles == 0) {
1833 options->user_hostfiles[options->num_user_hostfiles++] =
1834 xstrdup(_PATH_SSH_USER_HOSTFILE);
1835 options->user_hostfiles[options->num_user_hostfiles++] =
1836 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1837 }
Damien Millerfcd93202002-02-05 12:26:34 +11001838 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001839 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001840 if (options->clear_forwardings == 1)
1841 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001842 if (options->no_host_authentication_for_localhost == - 1)
1843 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001844 if (options->identities_only == -1)
1845 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001846 if (options->enable_ssh_keysign == -1)
1847 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001848 if (options->rekey_limit == -1)
1849 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001850 if (options->rekey_interval == -1)
1851 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001852 if (options->verify_host_key_dns == -1)
1853 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001854 if (options->server_alive_interval == -1)
1855 options->server_alive_interval = 0;
1856 if (options->server_alive_count_max == -1)
1857 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001858 if (options->control_master == -1)
1859 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001860 if (options->control_persist == -1) {
1861 options->control_persist = 0;
1862 options->control_persist_timeout = 0;
1863 }
Damien Millere1776152005-03-01 21:47:37 +11001864 if (options->hash_known_hosts == -1)
1865 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001866 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001867 options->tun_open = SSH_TUNMODE_NO;
1868 if (options->tun_local == -1)
1869 options->tun_local = SSH_TUNID_ANY;
1870 if (options->tun_remote == -1)
1871 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001872 if (options->permit_local_command == -1)
1873 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001874 if (options->use_roaming == -1)
1875 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001876 if (options->visual_host_key == -1)
1877 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001878 if (options->ip_qos_interactive == -1)
1879 options->ip_qos_interactive = IPTOS_LOWDELAY;
1880 if (options->ip_qos_bulk == -1)
1881 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001882 if (options->request_tty == -1)
1883 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001884 if (options->proxy_use_fdpass == -1)
1885 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001886 if (options->canonicalize_max_dots == -1)
1887 options->canonicalize_max_dots = 1;
1888 if (options->canonicalize_fallback_local == -1)
1889 options->canonicalize_fallback_local = 1;
1890 if (options->canonicalize_hostname == -1)
1891 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001892 if (options->fingerprint_hash == -1)
1893 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001894 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00001895 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001896 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
1897 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
1898 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
1899 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1900 &options->hostbased_key_types) != 0 ||
1901 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1902 &options->pubkey_key_types) != 0)
1903 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001904
Damien Millere9fc72e2013-10-15 12:14:12 +11001905#define CLEAR_ON_NONE(v) \
1906 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001907 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001908 free(v); \
1909 v = NULL; \
1910 } \
1911 } while(0)
1912 CLEAR_ON_NONE(options->local_command);
1913 CLEAR_ON_NONE(options->proxy_command);
1914 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001915 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001916 /* options->user will be set in the main program if appropriate */
1917 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001918 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001919 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001921
Damien Miller7acefbb2014-07-18 14:11:24 +10001922struct fwdarg {
1923 char *arg;
1924 int ispath;
1925};
1926
1927/*
1928 * parse_fwd_field
1929 * parses the next field in a port forwarding specification.
1930 * sets fwd to the parsed field and advances p past the colon
1931 * or sets it to NULL at end of string.
1932 * returns 0 on success, else non-zero.
1933 */
1934static int
1935parse_fwd_field(char **p, struct fwdarg *fwd)
1936{
1937 char *ep, *cp = *p;
1938 int ispath = 0;
1939
1940 if (*cp == '\0') {
1941 *p = NULL;
1942 return -1; /* end of string */
1943 }
1944
1945 /*
1946 * A field escaped with square brackets is used literally.
1947 * XXX - allow ']' to be escaped via backslash?
1948 */
1949 if (*cp == '[') {
1950 /* find matching ']' */
1951 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1952 if (*ep == '/')
1953 ispath = 1;
1954 }
1955 /* no matching ']' or not at end of field. */
1956 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1957 return -1;
1958 /* NUL terminate the field and advance p past the colon */
1959 *ep++ = '\0';
1960 if (*ep != '\0')
1961 *ep++ = '\0';
1962 fwd->arg = cp + 1;
1963 fwd->ispath = ispath;
1964 *p = ep;
1965 return 0;
1966 }
1967
1968 for (cp = *p; *cp != '\0'; cp++) {
1969 switch (*cp) {
1970 case '\\':
1971 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00001972 if (*cp == '\0')
1973 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10001974 break;
1975 case '/':
1976 ispath = 1;
1977 break;
1978 case ':':
1979 *cp++ = '\0';
1980 goto done;
1981 }
1982 }
1983done:
1984 fwd->arg = *p;
1985 fwd->ispath = ispath;
1986 *p = cp;
1987 return 0;
1988}
1989
Damien Millerf91ee4c2005-03-01 21:24:33 +11001990/*
1991 * parse_forward
1992 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001993 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10001994 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
1995 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11001996 * dynamicfwd == 1
1997 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001998 * returns number of arguments parsed or zero on error
1999 */
2000int
Damien Miller7acefbb2014-07-18 14:11:24 +10002001parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002002{
Damien Miller7acefbb2014-07-18 14:11:24 +10002003 struct fwdarg fwdargs[4];
2004 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002005 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002006
Damien Miller7acefbb2014-07-18 14:11:24 +10002007 memset(fwd, 0, sizeof(*fwd));
2008 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002009
2010 cp = p = xstrdup(fwdspec);
2011
2012 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002013 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002014 cp++;
2015
Damien Miller7acefbb2014-07-18 14:11:24 +10002016 for (i = 0; i < 4; ++i) {
2017 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002018 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002019 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002020
Damien Millerf4b39532008-11-03 19:28:21 +11002021 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002022 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002023 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002024 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002025
2026 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002027 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002028 if (fwdargs[0].ispath) {
2029 fwd->listen_path = xstrdup(fwdargs[0].arg);
2030 fwd->listen_port = PORT_STREAMLOCAL;
2031 } else {
2032 fwd->listen_host = NULL;
2033 fwd->listen_port = a2port(fwdargs[0].arg);
2034 }
Damien Millera699d952008-11-03 19:27:34 +11002035 fwd->connect_host = xstrdup("socks");
2036 break;
2037
2038 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002039 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2040 fwd->listen_path = xstrdup(fwdargs[0].arg);
2041 fwd->listen_port = PORT_STREAMLOCAL;
2042 fwd->connect_path = xstrdup(fwdargs[1].arg);
2043 fwd->connect_port = PORT_STREAMLOCAL;
2044 } else if (fwdargs[1].ispath) {
2045 fwd->listen_host = NULL;
2046 fwd->listen_port = a2port(fwdargs[0].arg);
2047 fwd->connect_path = xstrdup(fwdargs[1].arg);
2048 fwd->connect_port = PORT_STREAMLOCAL;
2049 } else {
2050 fwd->listen_host = xstrdup(fwdargs[0].arg);
2051 fwd->listen_port = a2port(fwdargs[1].arg);
2052 fwd->connect_host = xstrdup("socks");
2053 }
Damien Millera699d952008-11-03 19:27:34 +11002054 break;
2055
Damien Millerf91ee4c2005-03-01 21:24:33 +11002056 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002057 if (fwdargs[0].ispath) {
2058 fwd->listen_path = xstrdup(fwdargs[0].arg);
2059 fwd->listen_port = PORT_STREAMLOCAL;
2060 fwd->connect_host = xstrdup(fwdargs[1].arg);
2061 fwd->connect_port = a2port(fwdargs[2].arg);
2062 } else if (fwdargs[2].ispath) {
2063 fwd->listen_host = xstrdup(fwdargs[0].arg);
2064 fwd->listen_port = a2port(fwdargs[1].arg);
2065 fwd->connect_path = xstrdup(fwdargs[2].arg);
2066 fwd->connect_port = PORT_STREAMLOCAL;
2067 } else {
2068 fwd->listen_host = NULL;
2069 fwd->listen_port = a2port(fwdargs[0].arg);
2070 fwd->connect_host = xstrdup(fwdargs[1].arg);
2071 fwd->connect_port = a2port(fwdargs[2].arg);
2072 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002073 break;
2074
2075 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002076 fwd->listen_host = xstrdup(fwdargs[0].arg);
2077 fwd->listen_port = a2port(fwdargs[1].arg);
2078 fwd->connect_host = xstrdup(fwdargs[2].arg);
2079 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002080 break;
2081 default:
2082 i = 0; /* failure */
2083 }
2084
Darren Tuckera627d422013-06-02 07:31:17 +10002085 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002086
Damien Millera699d952008-11-03 19:27:34 +11002087 if (dynamicfwd) {
2088 if (!(i == 1 || i == 2))
2089 goto fail_free;
2090 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002091 if (!(i == 3 || i == 4)) {
2092 if (fwd->connect_path == NULL &&
2093 fwd->listen_path == NULL)
2094 goto fail_free;
2095 }
2096 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002097 goto fail_free;
2098 }
2099
Damien Miller7acefbb2014-07-18 14:11:24 +10002100 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2101 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002102 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002103 if (fwd->connect_host != NULL &&
2104 strlen(fwd->connect_host) >= NI_MAXHOST)
2105 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002106 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2107 if (fwd->connect_path != NULL &&
2108 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2109 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002110 if (fwd->listen_host != NULL &&
2111 strlen(fwd->listen_host) >= NI_MAXHOST)
2112 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002113 if (fwd->listen_path != NULL &&
2114 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2115 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002116
2117 return (i);
2118
2119 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002120 free(fwd->connect_host);
2121 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002122 free(fwd->connect_path);
2123 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002124 free(fwd->listen_host);
2125 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002126 free(fwd->listen_path);
2127 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002128 return (0);
2129}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002130
2131/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2132static const char *
2133fmt_multistate_int(int val, const struct multistate *m)
2134{
2135 u_int i;
2136
2137 for (i = 0; m[i].key != NULL; i++) {
2138 if (m[i].value == val)
2139 return m[i].key;
2140 }
2141 return "UNKNOWN";
2142}
2143
2144static const char *
2145fmt_intarg(OpCodes code, int val)
2146{
2147 if (val == -1)
2148 return "unset";
2149 switch (code) {
2150 case oAddressFamily:
2151 return fmt_multistate_int(val, multistate_addressfamily);
2152 case oVerifyHostKeyDNS:
2153 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002154 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002155 return fmt_multistate_int(val, multistate_yesnoask);
2156 case oControlMaster:
2157 return fmt_multistate_int(val, multistate_controlmaster);
2158 case oTunnel:
2159 return fmt_multistate_int(val, multistate_tunnel);
2160 case oRequestTTY:
2161 return fmt_multistate_int(val, multistate_requesttty);
2162 case oCanonicalizeHostname:
2163 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002164 case oFingerprintHash:
2165 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002166 case oProtocol:
2167 switch (val) {
2168 case SSH_PROTO_1:
2169 return "1";
2170 case SSH_PROTO_2:
2171 return "2";
2172 case (SSH_PROTO_1|SSH_PROTO_2):
2173 return "2,1";
2174 default:
2175 return "UNKNOWN";
2176 }
2177 default:
2178 switch (val) {
2179 case 0:
2180 return "no";
2181 case 1:
2182 return "yes";
2183 default:
2184 return "UNKNOWN";
2185 }
2186 }
2187}
2188
2189static const char *
2190lookup_opcode_name(OpCodes code)
2191{
2192 u_int i;
2193
2194 for (i = 0; keywords[i].name != NULL; i++)
2195 if (keywords[i].opcode == code)
2196 return(keywords[i].name);
2197 return "UNKNOWN";
2198}
2199
2200static void
2201dump_cfg_int(OpCodes code, int val)
2202{
2203 printf("%s %d\n", lookup_opcode_name(code), val);
2204}
2205
2206static void
2207dump_cfg_fmtint(OpCodes code, int val)
2208{
2209 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2210}
2211
2212static void
2213dump_cfg_string(OpCodes code, const char *val)
2214{
2215 if (val == NULL)
2216 return;
2217 printf("%s %s\n", lookup_opcode_name(code), val);
2218}
2219
2220static void
2221dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2222{
2223 u_int i;
2224
2225 for (i = 0; i < count; i++)
2226 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2227}
2228
2229static void
2230dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2231{
2232 u_int i;
2233
2234 printf("%s", lookup_opcode_name(code));
2235 for (i = 0; i < count; i++)
2236 printf(" %s", vals[i]);
2237 printf("\n");
2238}
2239
2240static void
2241dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2242{
2243 const struct Forward *fwd;
2244 u_int i;
2245
2246 /* oDynamicForward */
2247 for (i = 0; i < count; i++) {
2248 fwd = &fwds[i];
2249 if (code == oDynamicForward &&
2250 strcmp(fwd->connect_host, "socks") != 0)
2251 continue;
2252 if (code == oLocalForward &&
2253 strcmp(fwd->connect_host, "socks") == 0)
2254 continue;
2255 printf("%s", lookup_opcode_name(code));
2256 if (fwd->listen_port == PORT_STREAMLOCAL)
2257 printf(" %s", fwd->listen_path);
2258 else if (fwd->listen_host == NULL)
2259 printf(" %d", fwd->listen_port);
2260 else {
2261 printf(" [%s]:%d",
2262 fwd->listen_host, fwd->listen_port);
2263 }
2264 if (code != oDynamicForward) {
2265 if (fwd->connect_port == PORT_STREAMLOCAL)
2266 printf(" %s", fwd->connect_path);
2267 else if (fwd->connect_host == NULL)
2268 printf(" %d", fwd->connect_port);
2269 else {
2270 printf(" [%s]:%d",
2271 fwd->connect_host, fwd->connect_port);
2272 }
2273 }
2274 printf("\n");
2275 }
2276}
2277
2278void
2279dump_client_config(Options *o, const char *host)
2280{
2281 int i;
2282 char vbuf[5];
2283
djm@openbsd.org60a92472015-08-21 23:53:08 +00002284 /* This is normally prepared in ssh_kex2 */
2285 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2286 fatal("%s: kex_assemble_names failed", __func__);
2287
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002288 /* Most interesting options first: user, host, port */
2289 dump_cfg_string(oUser, o->user);
2290 dump_cfg_string(oHostName, host);
2291 dump_cfg_int(oPort, o->port);
2292
2293 /* Flag options */
2294 dump_cfg_fmtint(oAddressFamily, o->address_family);
2295 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2296 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2297 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2298 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2299 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2300 dump_cfg_fmtint(oCompression, o->compression);
2301 dump_cfg_fmtint(oControlMaster, o->control_master);
2302 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2303 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002304 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002305 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2306 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2307 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2308 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2309#ifdef GSSAPI
2310 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2311 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2312#endif /* GSSAPI */
2313 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2314 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2315 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2316 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2317 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2318 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2319 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2320 dump_cfg_fmtint(oProtocol, o->protocol);
2321 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2322 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2323 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2324 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2325 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2326 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2327 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2328 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2329 dump_cfg_fmtint(oTunnel, o->tun_open);
2330 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2331 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2332 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002333 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002334
2335 /* Integer options */
2336 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2337 dump_cfg_int(oCompressionLevel, o->compression_level);
2338 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2339 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2340 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2341 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2342 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2343
2344 /* String options */
2345 dump_cfg_string(oBindAddress, o->bind_address);
2346 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2347 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002348 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002349 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002350 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002351 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2352 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2353 dump_cfg_string(oLocalCommand, o->local_command);
2354 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2355 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2356 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2357 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2358 dump_cfg_string(oProxyCommand, o->proxy_command);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002359 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002360 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002361 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002362
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002363 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002364 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2365 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2366 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2367
2368 /* String array options */
2369 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2370 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2371 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2372 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2373 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2374
2375 /* Special cases */
2376
2377 /* oConnectTimeout */
2378 if (o->connection_timeout == -1)
2379 printf("connecttimeout none\n");
2380 else
2381 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2382
2383 /* oTunnelDevice */
2384 printf("tunneldevice");
2385 if (o->tun_local == SSH_TUNID_ANY)
2386 printf(" any");
2387 else
2388 printf(" %d", o->tun_local);
2389 if (o->tun_remote == SSH_TUNID_ANY)
2390 printf(":any");
2391 else
2392 printf(":%d", o->tun_remote);
2393 printf("\n");
2394
2395 /* oCanonicalizePermittedCNAMEs */
2396 if ( o->num_permitted_cnames > 0) {
2397 printf("canonicalizePermittedcnames");
2398 for (i = 0; i < o->num_permitted_cnames; i++) {
2399 printf(" %s:%s", o->permitted_cnames[i].source_list,
2400 o->permitted_cnames[i].target_list);
2401 }
2402 printf("\n");
2403 }
2404
2405 /* oCipher */
2406 if (o->cipher != SSH_CIPHER_NOT_SET)
2407 printf("Cipher %s\n", cipher_name(o->cipher));
2408
2409 /* oControlPersist */
2410 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2411 dump_cfg_fmtint(oControlPersist, o->control_persist);
2412 else
2413 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2414
2415 /* oEscapeChar */
2416 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2417 printf("escapechar none\n");
2418 else {
2419 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2420 printf("escapechar %s\n", vbuf);
2421 }
2422
2423 /* oIPQoS */
2424 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2425 printf("%s\n", iptos2str(o->ip_qos_bulk));
2426
2427 /* oRekeyLimit */
2428 printf("rekeylimit %lld %d\n",
2429 (long long)o->rekey_limit, o->rekey_interval);
2430
2431 /* oStreamLocalBindMask */
2432 printf("streamlocalbindmask 0%o\n",
2433 o->fwd_opts.streamlocal_bind_mask);
2434}