blob: 94bf7e1b4bdda3a2fb070bc5f739e24a971e5075 [file] [log] [blame]
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00001/* $OpenBSD: readconf.c,v 1.242 2015/10/07 15:59:12 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100043#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100044#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000045#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
46# include <vis.h>
47#endif
Damien Millerc7b06362006-03-15 11:53:45 +110048
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "cipher.h"
53#include "pathnames.h"
54#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000055#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100056#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "readconf.h"
58#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "kex.h"
60#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110061#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000062#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000063#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65/* Format of the configuration file:
66
67 # Configuration data is parsed as follows:
68 # 1. command line options
69 # 2. user-specific file
70 # 3. system-wide file
71 # Any configuration value is only changed the first time it is set.
72 # Thus, host-specific definitions should be at the beginning of the
73 # configuration file, and defaults at the end.
74
75 # Host-specific declarations. These may override anything above. A single
76 # host may match multiple declarations; these are processed in the order
77 # that they are given in.
78
79 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000080 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82 Host fake.com
83 HostName another.host.name.real.org
84 User blaah
85 Port 34289
86 ForwardX11 no
87 ForwardAgent no
88
89 Host books.com
90 RemoteForward 9999 shadows.cs.hut.fi:9999
91 Cipher 3des
92
93 Host fascist.blob.com
94 Port 23123
95 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 PasswordAuthentication no
97
98 Host puukko.hut.fi
99 User t35124p
100 ProxyCommand ssh-proxy %h %p
101
102 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000103 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105 Host *.su
106 Cipher none
107 PasswordAuthentication no
108
Damien Millerd27b9472005-12-13 19:29:02 +1100109 Host vpn.fake.com
110 Tunnel yes
111 TunnelDevice 3
112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 # Defaults for various options
114 Host *
115 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 PasswordAuthentication yes
118 RSAAuthentication yes
119 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100121 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 IdentityFile ~/.ssh/identity
123 Port 22
124 EscapeChar ~
125
126*/
127
128/* Keyword tokens. */
129
Damien Miller95def091999-11-25 00:26:21 +1100130typedef enum {
131 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100132 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000133 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
134 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000135 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000136 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100137 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
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{
445 char *shell, *command_string;
446 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
453 /*
454 * Use "exec" to avoid "sh -c" processes on some platforms
455 * (e.g. Solaris)
456 */
457 xasprintf(&command_string, "exec %s", cmd);
458
459 /* Need this to redirect subprocess stdin/out */
460 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
461 fatal("open(/dev/null): %s", strerror(errno));
462
463 debug("Executing command: '%.500s'", cmd);
464
465 /* Fork and execute the command. */
466 if ((pid = fork()) == 0) {
467 char *argv[4];
468
469 /* Child. Permanently give up superuser privileges. */
470 permanently_drop_suid(original_real_uid);
471
472 /* Redirect child stdin and stdout. Leave stderr */
473 if (dup2(devnull, STDIN_FILENO) == -1)
474 fatal("dup2: %s", strerror(errno));
475 if (dup2(devnull, STDOUT_FILENO) == -1)
476 fatal("dup2: %s", strerror(errno));
477 if (devnull > STDERR_FILENO)
478 close(devnull);
479 closefrom(STDERR_FILENO + 1);
480
481 argv[0] = shell;
482 argv[1] = "-c";
483 argv[2] = command_string;
484 argv[3] = NULL;
485
486 execv(argv[0], argv);
487 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
488 /* Die with signal to make this error apparent to parent. */
489 signal(SIGTERM, SIG_DFL);
490 kill(getpid(), SIGTERM);
491 _exit(1);
492 }
493 /* Parent. */
494 if (pid < 0)
495 fatal("%s: fork: %.100s", __func__, strerror(errno));
496
497 close(devnull);
498 free(command_string);
499
500 while (waitpid(pid, &status, 0) == -1) {
501 if (errno != EINTR && errno != EAGAIN)
502 fatal("%s: waitpid: %s", __func__, strerror(errno));
503 }
504 if (!WIFEXITED(status)) {
505 error("command '%.100s' exited abnormally", cmd);
506 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000507 }
Damien Miller194fd902013-10-15 12:13:05 +1100508 debug3("command returned status %d", WEXITSTATUS(status));
509 return WEXITSTATUS(status);
510}
511
512/*
513 * Parse and execute a Match directive.
514 */
515static int
516match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000517 const char *host_arg, const char *original_host, int post_canon,
518 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100519{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000520 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100521 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000522 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100523 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
524
525 /*
526 * Configuration is likely to be incomplete at this point so we
527 * must be prepared to use default values.
528 */
529 port = options->port <= 0 ? default_ssh_port() : options->port;
530 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100531 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100532 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100533 host = percent_expand(options->hostname,
534 "h", host_arg, (char *)NULL);
535 } else
536 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100537
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000538 debug2("checking match for '%s' host %s originally %s",
539 cp, host, original_host);
540 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
541 criteria = NULL;
542 this_result = 1;
543 if ((negate = attrib[0] == '!'))
544 attrib++;
545 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100546 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000547 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100548 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000549 error("%.200s line %d: '%s' cannot be combined "
550 "with other Match attributes",
551 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100552 result = -1;
553 goto out;
554 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000555 if (result)
556 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100557 goto out;
558 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000559 attributes++;
560 if (strcasecmp(attrib, "canonical") == 0) {
561 r = !!post_canon; /* force bitmask member to boolean */
562 if (r == (negate ? 1 : 0))
563 this_result = result = 0;
564 debug3("%.200s line %d: %smatched '%s'",
565 filename, linenum,
566 this_result ? "" : "not ", oattrib);
567 continue;
568 }
569 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100570 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
571 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100572 result = -1;
573 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100574 }
Damien Miller194fd902013-10-15 12:13:05 +1100575 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000576 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000577 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000578 if (r == (negate ? 1 : 0))
579 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100580 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000581 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000582 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000583 if (r == (negate ? 1 : 0))
584 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100585 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000586 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000587 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000588 if (r == (negate ? 1 : 0))
589 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100590 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000591 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000592 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000593 if (r == (negate ? 1 : 0))
594 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100595 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100596 if (gethostname(thishost, sizeof(thishost)) == -1)
597 fatal("gethostname: %s", strerror(errno));
598 strlcpy(shorthost, thishost, sizeof(shorthost));
599 shorthost[strcspn(thishost, ".")] = '\0';
600 snprintf(portstr, sizeof(portstr), "%d", port);
601
602 cmd = percent_expand(arg,
603 "L", shorthost,
604 "d", pw->pw_dir,
605 "h", host,
606 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000607 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100608 "p", portstr,
609 "r", ruser,
610 "u", pw->pw_name,
611 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100612 if (result != 1) {
613 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000614 debug3("%.200s line %d: skipped exec "
615 "\"%.100s\"", filename, linenum, cmd);
616 free(cmd);
617 continue;
Damien Miller06287802014-02-24 15:56:45 +1100618 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000619 r = execute_in_shell(cmd);
620 if (r == -1) {
621 fatal("%.200s line %d: match exec "
622 "'%.100s' error", filename,
623 linenum, cmd);
624 }
625 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100626 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000627 /* Force exit status to boolean */
628 r = r == 0;
629 if (r == (negate ? 1 : 0))
630 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100631 } else {
632 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100633 result = -1;
634 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100635 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000636 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
637 filename, linenum, this_result ? "": "not ",
638 oattrib, criteria);
639 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100640 }
Damien Millercf31f382013-10-24 21:02:56 +1100641 if (attributes == 0) {
642 error("One or more attributes required for Match");
643 result = -1;
644 goto out;
645 }
Damien Miller084bcd22013-10-23 16:30:51 +1100646 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000647 if (result != -1)
648 debug2("match %sfound", result ? "" : "not ");
649 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100650 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100651 return result;
652}
653
Damien Miller0faf7472013-10-17 11:47:23 +1100654/* Check and prepare a domain name: removes trailing '.' and lowercases */
655static void
656valid_domain(char *name, const char *filename, int linenum)
657{
658 size_t i, l = strlen(name);
659 u_char c, last = '\0';
660
661 if (l == 0)
662 fatal("%s line %d: empty hostname suffix", filename, linenum);
663 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
664 fatal("%s line %d: hostname suffix \"%.100s\" "
665 "starts with invalid character", filename, linenum, name);
666 for (i = 0; i < l; i++) {
667 c = tolower((u_char)name[i]);
668 name[i] = (char)c;
669 if (last == '.' && c == '.')
670 fatal("%s line %d: hostname suffix \"%.100s\" contains "
671 "consecutive separators", filename, linenum, name);
672 if (c != '.' && c != '-' && !isalnum(c) &&
673 c != '_') /* technically invalid, but common */
674 fatal("%s line %d: hostname suffix \"%.100s\" contains "
675 "invalid characters", filename, linenum, name);
676 last = c;
677 }
678 if (name[l - 1] == '.')
679 name[l - 1] = '\0';
680}
681
Damien Miller5428f641999-11-25 11:54:57 +1100682/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000683 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100684 */
Damien Miller4af51302000-04-16 11:18:38 +1000685static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000686parse_token(const char *cp, const char *filename, int linenum,
687 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688{
Darren Tucker07636982013-05-16 20:30:03 +1000689 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000690
Damien Miller95def091999-11-25 00:26:21 +1100691 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000692 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100693 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000694 if (ignored_unknown != NULL &&
695 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000696 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000697 error("%s: line %d: Bad configuration option: %s",
698 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100699 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700}
701
Damien Millere9fc72e2013-10-15 12:14:12 +1100702/* Multistate option parsing */
703struct multistate {
704 char *key;
705 int value;
706};
707static const struct multistate multistate_flag[] = {
708 { "true", 1 },
709 { "false", 0 },
710 { "yes", 1 },
711 { "no", 0 },
712 { NULL, -1 }
713};
714static const struct multistate multistate_yesnoask[] = {
715 { "true", 1 },
716 { "false", 0 },
717 { "yes", 1 },
718 { "no", 0 },
719 { "ask", 2 },
720 { NULL, -1 }
721};
722static const struct multistate multistate_addressfamily[] = {
723 { "inet", AF_INET },
724 { "inet6", AF_INET6 },
725 { "any", AF_UNSPEC },
726 { NULL, -1 }
727};
728static const struct multistate multistate_controlmaster[] = {
729 { "true", SSHCTL_MASTER_YES },
730 { "yes", SSHCTL_MASTER_YES },
731 { "false", SSHCTL_MASTER_NO },
732 { "no", SSHCTL_MASTER_NO },
733 { "auto", SSHCTL_MASTER_AUTO },
734 { "ask", SSHCTL_MASTER_ASK },
735 { "autoask", SSHCTL_MASTER_AUTO_ASK },
736 { NULL, -1 }
737};
738static const struct multistate multistate_tunnel[] = {
739 { "ethernet", SSH_TUNMODE_ETHERNET },
740 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
741 { "true", SSH_TUNMODE_DEFAULT },
742 { "yes", SSH_TUNMODE_DEFAULT },
743 { "false", SSH_TUNMODE_NO },
744 { "no", SSH_TUNMODE_NO },
745 { NULL, -1 }
746};
747static const struct multistate multistate_requesttty[] = {
748 { "true", REQUEST_TTY_YES },
749 { "yes", REQUEST_TTY_YES },
750 { "false", REQUEST_TTY_NO },
751 { "no", REQUEST_TTY_NO },
752 { "force", REQUEST_TTY_FORCE },
753 { "auto", REQUEST_TTY_AUTO },
754 { NULL, -1 }
755};
Damien Miller38505592013-10-17 11:48:13 +1100756static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100757 { "true", SSH_CANONICALISE_YES },
758 { "false", SSH_CANONICALISE_NO },
759 { "yes", SSH_CANONICALISE_YES },
760 { "no", SSH_CANONICALISE_NO },
761 { "always", SSH_CANONICALISE_ALWAYS },
762 { NULL, -1 }
763};
Damien Millere9fc72e2013-10-15 12:14:12 +1100764
Damien Miller5428f641999-11-25 11:54:57 +1100765/*
766 * Processes a single option line as used in the configuration files. This
767 * only sets those values that have not already been set.
768 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100769#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100770int
Damien Miller194fd902013-10-15 12:13:05 +1100771process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000772 const char *original_host, char *line, const char *filename,
773 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774{
Damien Miller295ee632011-05-29 21:42:31 +1000775 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
776 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000777 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100778 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100779 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000780 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100781 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000782 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100783 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100784 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller194fd902013-10-15 12:13:05 +1100786 if (activep == NULL) { /* We are processing a command line directive */
787 cmdline = 1;
788 activep = &cmdline;
789 }
790
Damien Millerc652cac2003-05-14 13:40:54 +1000791 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000792 if ((len = strlen(line)) == 0)
793 return 0;
794 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000795 if (strchr(WHITESPACE, line[len]) == NULL)
796 break;
797 line[len] = '\0';
798 }
799
Damien Millerbe484b52000-07-15 14:14:16 +1000800 s = line;
801 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100802 if ((keyword = strdelim(&s)) == NULL)
803 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000804 /* Ignore leading whitespace. */
805 if (*keyword == '\0')
806 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000807 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100808 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000809 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100810 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811
Darren Tucker07636982013-05-16 20:30:03 +1000812 opcode = parse_token(keyword, filename, linenum,
813 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Damien Miller95def091999-11-25 00:26:21 +1100815 switch (opcode) {
816 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100817 /* don't panic, but count bad options */
818 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100819 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000820 case oIgnoredUnknownOption:
821 debug("%s line %d: Ignored unknown option \"%s\"",
822 filename, linenum, keyword);
823 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000824 case oConnectTimeout:
825 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100826parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000827 arg = strdelim(&s);
828 if (!arg || *arg == '\0')
829 fatal("%s line %d: missing time value.",
830 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000831 if (strcmp(arg, "none") == 0)
832 value = -1;
833 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000834 fatal("%s line %d: invalid time value.",
835 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100836 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000837 *intptr = value;
838 break;
839
Damien Miller95def091999-11-25 00:26:21 +1100840 case oForwardAgent:
841 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100842 parse_flag:
843 multistate_ptr = multistate_flag;
844 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000845 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000846 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100847 fatal("%s line %d: missing argument.",
848 filename, linenum);
849 value = -1;
850 for (i = 0; multistate_ptr[i].key != NULL; i++) {
851 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
852 value = multistate_ptr[i].value;
853 break;
854 }
855 }
856 if (value == -1)
857 fatal("%s line %d: unsupported option \"%s\".",
858 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100859 if (*activep && *intptr == -1)
860 *intptr = value;
861 break;
862
863 case oForwardX11:
864 intptr = &options->forward_x11;
865 goto parse_flag;
866
Darren Tucker0a118da2003-10-15 15:54:32 +1000867 case oForwardX11Trusted:
868 intptr = &options->forward_x11_trusted;
869 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000870
Damien Miller1ab6a512010-06-26 10:02:24 +1000871 case oForwardX11Timeout:
872 intptr = &options->forward_x11_timeout;
873 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000874
Damien Miller95def091999-11-25 00:26:21 +1100875 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000876 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100877 goto parse_flag;
878
Darren Tuckere7d4b192006-07-12 22:17:10 +1000879 case oExitOnForwardFailure:
880 intptr = &options->exit_on_forward_failure;
881 goto parse_flag;
882
Damien Miller95def091999-11-25 00:26:21 +1100883 case oUsePrivilegedPort:
884 intptr = &options->use_privileged_port;
885 goto parse_flag;
886
Damien Miller95def091999-11-25 00:26:21 +1100887 case oPasswordAuthentication:
888 intptr = &options->password_authentication;
889 goto parse_flag;
890
Damien Miller874d77b2000-10-14 16:23:11 +1100891 case oKbdInteractiveAuthentication:
892 intptr = &options->kbd_interactive_authentication;
893 goto parse_flag;
894
895 case oKbdInteractiveDevices:
896 charptr = &options->kbd_interactive_devices;
897 goto parse_string;
898
Damien Miller0bc1bd82000-11-13 22:57:25 +1100899 case oPubkeyAuthentication:
900 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000901 goto parse_flag;
902
Damien Miller95def091999-11-25 00:26:21 +1100903 case oRSAAuthentication:
904 intptr = &options->rsa_authentication;
905 goto parse_flag;
906
907 case oRhostsRSAAuthentication:
908 intptr = &options->rhosts_rsa_authentication;
909 goto parse_flag;
910
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000911 case oHostbasedAuthentication:
912 intptr = &options->hostbased_authentication;
913 goto parse_flag;
914
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000915 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000916 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100917 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000918
Darren Tucker0efd1552003-08-26 11:49:55 +1000919 case oGssAuthentication:
920 intptr = &options->gss_authentication;
921 goto parse_flag;
922
923 case oGssDelegateCreds:
924 intptr = &options->gss_deleg_creds;
925 goto parse_flag;
926
Damien Miller95def091999-11-25 00:26:21 +1100927 case oBatchMode:
928 intptr = &options->batch_mode;
929 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930
Damien Miller95def091999-11-25 00:26:21 +1100931 case oCheckHostIP:
932 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000933 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Miller37876e92003-05-15 10:19:46 +1000935 case oVerifyHostKeyDNS:
936 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100937 multistate_ptr = multistate_yesnoask;
938 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000939
Damien Miller95def091999-11-25 00:26:21 +1100940 case oStrictHostKeyChecking:
941 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100942 multistate_ptr = multistate_yesnoask;
943 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944
Damien Miller95def091999-11-25 00:26:21 +1100945 case oCompression:
946 intptr = &options->compression;
947 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller12c150e2003-12-17 16:31:10 +1100949 case oTCPKeepAlive:
950 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100951 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000953 case oNoHostAuthenticationForLocalhost:
954 intptr = &options->no_host_authentication_for_localhost;
955 goto parse_flag;
956
Damien Miller95def091999-11-25 00:26:21 +1100957 case oNumberOfPasswordPrompts:
958 intptr = &options->number_of_password_prompts;
959 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960
Damien Miller95def091999-11-25 00:26:21 +1100961 case oCompressionLevel:
962 intptr = &options->compression_level;
963 goto parse_int;
964
Damien Millera5539d22003-04-09 20:50:06 +1000965 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000966 arg = strdelim(&s);
967 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000968 fatal("%.200s line %d: Missing argument.", filename,
969 linenum);
970 if (strcmp(arg, "default") == 0) {
971 val64 = 0;
972 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000973 if (scan_scaled(arg, &val64) == -1)
974 fatal("%.200s line %d: Bad number '%s': %s",
975 filename, linenum, arg, strerror(errno));
976 /* check for too-large or too-small limits */
977 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000978 fatal("%.200s line %d: RekeyLimit too large",
979 filename, linenum);
980 if (val64 != 0 && val64 < 16)
981 fatal("%.200s line %d: RekeyLimit too small",
982 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000983 }
Damien Miller3dff1762008-02-10 22:25:52 +1100984 if (*activep && options->rekey_limit == -1)
985 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000986 if (s != NULL) { /* optional rekey interval present */
987 if (strcmp(s, "none") == 0) {
988 (void)strdelim(&s); /* discard */
989 break;
990 }
991 intptr = &options->rekey_interval;
992 goto parse_time;
993 }
Damien Millera5539d22003-04-09 20:50:06 +1000994 break;
995
Damien Miller95def091999-11-25 00:26:21 +1100996 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000997 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000998 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100999 fatal("%.200s line %d: Missing argument.", filename, linenum);
1000 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001001 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001002 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001003 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001004 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001005 add_identity_file(options, NULL,
1006 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001007 }
1008 break;
1009
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001010 case oCertificateFile:
1011 arg = strdelim(&s);
1012 if (!arg || *arg == '\0')
1013 fatal("%.200s line %d: Missing argument.",
1014 filename, linenum);
1015 if (*activep) {
1016 intptr = &options->num_certificate_files;
1017 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1018 fatal("%.200s line %d: Too many certificate "
1019 "files specified (max %d).",
1020 filename, linenum,
1021 SSH_MAX_CERTIFICATE_FILES);
1022 }
1023 add_certificate_file(options, arg,
1024 flags & SSHCONF_USERCONF);
1025 }
1026 break;
1027
Damien Millerd3a18572000-06-07 19:55:44 +10001028 case oXAuthLocation:
1029 charptr=&options->xauth_location;
1030 goto parse_string;
1031
Damien Miller95def091999-11-25 00:26:21 +11001032 case oUser:
1033 charptr = &options->user;
1034parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001035 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001036 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001037 fatal("%.200s line %d: Missing argument.",
1038 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001039 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001040 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001041 break;
1042
1043 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001044 cpptr = (char **)&options->system_hostfiles;
1045 uintptr = &options->num_system_hostfiles;
1046 max_entries = SSH_MAX_HOSTS_FILES;
1047parse_char_array:
1048 if (*activep && *uintptr == 0) {
1049 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1050 if ((*uintptr) >= max_entries)
1051 fatal("%s line %d: "
1052 "too many authorized keys files.",
1053 filename, linenum);
1054 cpptr[(*uintptr)++] = xstrdup(arg);
1055 }
1056 }
1057 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001058
1059 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001060 cpptr = (char **)&options->user_hostfiles;
1061 uintptr = &options->num_user_hostfiles;
1062 max_entries = SSH_MAX_HOSTS_FILES;
1063 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001064
Damien Miller95def091999-11-25 00:26:21 +11001065 case oHostName:
1066 charptr = &options->hostname;
1067 goto parse_string;
1068
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001069 case oHostKeyAlias:
1070 charptr = &options->host_key_alias;
1071 goto parse_string;
1072
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001073 case oPreferredAuthentications:
1074 charptr = &options->preferred_authentications;
1075 goto parse_string;
1076
Ben Lindstrome0f88042001-04-30 13:06:24 +00001077 case oBindAddress:
1078 charptr = &options->bind_address;
1079 goto parse_string;
1080
Damien Miller7ea845e2010-02-12 09:21:02 +11001081 case oPKCS11Provider:
1082 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001083 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001084
Damien Miller95def091999-11-25 00:26:21 +11001085 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001086 charptr = &options->proxy_command;
1087parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001088 if (s == NULL)
1089 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001090 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001091 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001092 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001093 return 0;
1094
1095 case oPort:
1096 intptr = &options->port;
1097parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001098 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001099 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001100 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001101 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001102 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001103
1104 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001105 value = strtol(arg, &endofnumber, 0);
1106 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001107 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001108 if (*activep && *intptr == -1)
1109 *intptr = value;
1110 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001111
Damien Miller95def091999-11-25 00:26:21 +11001112 case oConnectionAttempts:
1113 intptr = &options->connection_attempts;
1114 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001115
Damien Miller95def091999-11-25 00:26:21 +11001116 case oCipher:
1117 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001118 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001119 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001120 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001121 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001122 if (value == -1)
1123 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001124 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001125 if (*activep && *intptr == -1)
1126 *intptr = value;
1127 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128
Damien Miller78928792000-04-12 20:17:38 +10001129 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001130 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001131 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001132 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001133 if (!ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001134 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001135 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001136 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001137 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001138 break;
1139
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001140 case oMacs:
1141 arg = strdelim(&s);
1142 if (!arg || *arg == '\0')
1143 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001144 if (!mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001145 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001146 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001147 if (*activep && options->macs == NULL)
1148 options->macs = xstrdup(arg);
1149 break;
1150
Damien Millerd5f62bf2010-09-24 22:11:14 +10001151 case oKexAlgorithms:
1152 arg = strdelim(&s);
1153 if (!arg || *arg == '\0')
1154 fatal("%.200s line %d: Missing argument.",
1155 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001156 if (!kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001157 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1158 filename, linenum, arg ? arg : "<NONE>");
1159 if (*activep && options->kex_algorithms == NULL)
1160 options->kex_algorithms = xstrdup(arg);
1161 break;
1162
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001163 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001164 charptr = &options->hostkeyalgorithms;
1165parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001166 arg = strdelim(&s);
1167 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001168 fatal("%.200s line %d: Missing argument.",
1169 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001170 if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001171 fatal("%s line %d: Bad key types '%s'.",
1172 filename, linenum, arg ? arg : "<NONE>");
1173 if (*activep && *charptr == NULL)
1174 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001175 break;
1176
Damien Miller78928792000-04-12 20:17:38 +10001177 case oProtocol:
1178 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001179 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001180 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001181 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001182 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001183 if (value == SSH_PROTO_UNKNOWN)
1184 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001185 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001186 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1187 *intptr = value;
1188 break;
1189
Damien Miller95def091999-11-25 00:26:21 +11001190 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001191 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001192 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001193 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001194 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001195 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001196 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001197 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1198 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001199 break;
1200
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001201 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001202 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001203 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001204 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001205 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001206 fatal("%.200s line %d: Missing port argument.",
1207 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001208
Damien Millera699d952008-11-03 19:27:34 +11001209 if (opcode == oLocalForward ||
1210 opcode == oRemoteForward) {
1211 arg2 = strdelim(&s);
1212 if (arg2 == NULL || *arg2 == '\0')
1213 fatal("%.200s line %d: Missing target argument.",
1214 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001215
Damien Millera699d952008-11-03 19:27:34 +11001216 /* construct a string for parse_forward */
1217 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1218 } else if (opcode == oDynamicForward) {
1219 strlcpy(fwdarg, arg, sizeof(fwdarg));
1220 }
1221
1222 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001223 opcode == oDynamicForward ? 1 : 0,
1224 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001225 fatal("%.200s line %d: Bad forwarding specification.",
1226 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001227
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001228 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001229 if (opcode == oLocalForward ||
1230 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001231 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001232 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001233 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001234 }
Damien Miller95def091999-11-25 00:26:21 +11001235 break;
1236
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001237 case oClearAllForwardings:
1238 intptr = &options->clear_forwardings;
1239 goto parse_flag;
1240
Damien Miller95def091999-11-25 00:26:21 +11001241 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001242 if (cmdline)
1243 fatal("Host directive not supported as a command-line "
1244 "option");
Damien Miller95def091999-11-25 00:26:21 +11001245 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001246 arg2 = NULL;
1247 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1248 negated = *arg == '!';
1249 if (negated)
1250 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001251 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001252 if (negated) {
1253 debug("%.200s line %d: Skipping Host "
1254 "block because of negated match "
1255 "for %.100s", filename, linenum,
1256 arg);
1257 *activep = 0;
1258 break;
1259 }
1260 if (!*activep)
1261 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001262 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001263 }
Damien Millerfe924212011-05-15 08:44:45 +10001264 }
1265 if (*activep)
1266 debug("%.200s line %d: Applying options for %.100s",
1267 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001268 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001269 return 0;
1270
Damien Miller194fd902013-10-15 12:13:05 +11001271 case oMatch:
1272 if (cmdline)
1273 fatal("Host directive not supported as a command-line "
1274 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001275 value = match_cfg_line(options, &s, pw, host, original_host,
1276 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001277 if (value < 0)
1278 fatal("%.200s line %d: Bad Match condition", filename,
1279 linenum);
1280 *activep = value;
1281 break;
1282
Damien Miller95def091999-11-25 00:26:21 +11001283 case oEscapeChar:
1284 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001285 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001286 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001287 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001288 if (strcmp(arg, "none") == 0)
1289 value = SSH_ESCAPECHAR_NONE;
1290 else if (arg[1] == '\0')
1291 value = (u_char) arg[0];
1292 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001293 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1294 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001295 else {
1296 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001297 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001298 /* NOTREACHED */
1299 value = 0; /* Avoid compiler warning. */
1300 }
1301 if (*activep && *intptr == -1)
1302 *intptr = value;
1303 break;
1304
Damien Miller20a8f972003-05-18 20:50:30 +10001305 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001306 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001307 multistate_ptr = multistate_addressfamily;
1308 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001309
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001310 case oEnableSSHKeysign:
1311 intptr = &options->enable_ssh_keysign;
1312 goto parse_flag;
1313
Damien Millerbd394c32004-03-08 23:12:36 +11001314 case oIdentitiesOnly:
1315 intptr = &options->identities_only;
1316 goto parse_flag;
1317
Damien Miller509b0102003-12-17 16:33:10 +11001318 case oServerAliveInterval:
1319 intptr = &options->server_alive_interval;
1320 goto parse_time;
1321
1322 case oServerAliveCountMax:
1323 intptr = &options->server_alive_count_max;
1324 goto parse_int;
1325
Darren Tucker46bc0752004-05-02 22:11:30 +10001326 case oSendEnv:
1327 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1328 if (strchr(arg, '=') != NULL)
1329 fatal("%s line %d: Invalid environment name.",
1330 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001331 if (!*activep)
1332 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001333 if (options->num_send_env >= MAX_SEND_ENV)
1334 fatal("%s line %d: too many send env.",
1335 filename, linenum);
1336 options->send_env[options->num_send_env++] =
1337 xstrdup(arg);
1338 }
1339 break;
1340
Damien Miller0e220db2004-06-15 10:34:08 +10001341 case oControlPath:
1342 charptr = &options->control_path;
1343 goto parse_string;
1344
1345 case oControlMaster:
1346 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001347 multistate_ptr = multistate_controlmaster;
1348 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001349
Damien Millere11e1ea2010-08-03 16:04:46 +10001350 case oControlPersist:
1351 /* no/false/yes/true, or a time spec */
1352 intptr = &options->control_persist;
1353 arg = strdelim(&s);
1354 if (!arg || *arg == '\0')
1355 fatal("%.200s line %d: Missing ControlPersist"
1356 " argument.", filename, linenum);
1357 value = 0;
1358 value2 = 0; /* timeout */
1359 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1360 value = 0;
1361 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1362 value = 1;
1363 else if ((value2 = convtime(arg)) >= 0)
1364 value = 1;
1365 else
1366 fatal("%.200s line %d: Bad ControlPersist argument.",
1367 filename, linenum);
1368 if (*activep && *intptr == -1) {
1369 *intptr = value;
1370 options->control_persist_timeout = value2;
1371 }
1372 break;
1373
Damien Millere1776152005-03-01 21:47:37 +11001374 case oHashKnownHosts:
1375 intptr = &options->hash_known_hosts;
1376 goto parse_flag;
1377
Damien Millerd27b9472005-12-13 19:29:02 +11001378 case oTunnel:
1379 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001380 multistate_ptr = multistate_tunnel;
1381 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001382
1383 case oTunnelDevice:
1384 arg = strdelim(&s);
1385 if (!arg || *arg == '\0')
1386 fatal("%.200s line %d: Missing argument.", filename, linenum);
1387 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001388 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001389 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1390 if (*activep) {
1391 options->tun_local = value;
1392 options->tun_remote = value2;
1393 }
1394 break;
1395
1396 case oLocalCommand:
1397 charptr = &options->local_command;
1398 goto parse_command;
1399
1400 case oPermitLocalCommand:
1401 intptr = &options->permit_local_command;
1402 goto parse_flag;
1403
Damien Miller10288242008-06-30 00:04:03 +10001404 case oVisualHostKey:
1405 intptr = &options->visual_host_key;
1406 goto parse_flag;
1407
Damien Miller0dac6fb2010-11-20 15:19:38 +11001408 case oIPQoS:
1409 arg = strdelim(&s);
1410 if ((value = parse_ipqos(arg)) == -1)
1411 fatal("%s line %d: Bad IPQoS value: %s",
1412 filename, linenum, arg);
1413 arg = strdelim(&s);
1414 if (arg == NULL)
1415 value2 = value;
1416 else if ((value2 = parse_ipqos(arg)) == -1)
1417 fatal("%s line %d: Bad IPQoS value: %s",
1418 filename, linenum, arg);
1419 if (*activep) {
1420 options->ip_qos_interactive = value;
1421 options->ip_qos_bulk = value2;
1422 }
1423 break;
1424
Darren Tucker71e4d542009-07-06 07:12:27 +10001425 case oUseRoaming:
1426 intptr = &options->use_roaming;
1427 goto parse_flag;
1428
Damien Miller21771e22011-05-15 08:45:50 +10001429 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001430 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001431 multistate_ptr = multistate_requesttty;
1432 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001433
Darren Tucker07636982013-05-16 20:30:03 +10001434 case oIgnoreUnknown:
1435 charptr = &options->ignored_unknown;
1436 goto parse_string;
1437
Damien Miller1262b662013-08-21 02:44:24 +10001438 case oProxyUseFdpass:
1439 intptr = &options->proxy_use_fdpass;
1440 goto parse_flag;
1441
Damien Miller0faf7472013-10-17 11:47:23 +11001442 case oCanonicalDomains:
1443 value = options->num_canonical_domains != 0;
1444 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1445 valid_domain(arg, filename, linenum);
1446 if (!*activep || value)
1447 continue;
1448 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1449 fatal("%s line %d: too many hostname suffixes.",
1450 filename, linenum);
1451 options->canonical_domains[
1452 options->num_canonical_domains++] = xstrdup(arg);
1453 }
1454 break;
1455
Damien Miller38505592013-10-17 11:48:13 +11001456 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001457 value = options->num_permitted_cnames != 0;
1458 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1459 /* Either '*' for everything or 'list:list' */
1460 if (strcmp(arg, "*") == 0)
1461 arg2 = arg;
1462 else {
1463 lowercase(arg);
1464 if ((arg2 = strchr(arg, ':')) == NULL ||
1465 arg2[1] == '\0') {
1466 fatal("%s line %d: "
1467 "Invalid permitted CNAME \"%s\"",
1468 filename, linenum, arg);
1469 }
1470 *arg2 = '\0';
1471 arg2++;
1472 }
1473 if (!*activep || value)
1474 continue;
1475 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1476 fatal("%s line %d: too many permitted CNAMEs.",
1477 filename, linenum);
1478 cname = options->permitted_cnames +
1479 options->num_permitted_cnames++;
1480 cname->source_list = xstrdup(arg);
1481 cname->target_list = xstrdup(arg2);
1482 }
1483 break;
1484
Damien Miller38505592013-10-17 11:48:13 +11001485 case oCanonicalizeHostname:
1486 intptr = &options->canonicalize_hostname;
1487 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001488 goto parse_multistate;
1489
Damien Miller38505592013-10-17 11:48:13 +11001490 case oCanonicalizeMaxDots:
1491 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001492 goto parse_int;
1493
Damien Miller38505592013-10-17 11:48:13 +11001494 case oCanonicalizeFallbackLocal:
1495 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001496 goto parse_flag;
1497
Damien Miller7acefbb2014-07-18 14:11:24 +10001498 case oStreamLocalBindMask:
1499 arg = strdelim(&s);
1500 if (!arg || *arg == '\0')
1501 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1502 /* Parse mode in octal format */
1503 value = strtol(arg, &endofnumber, 8);
1504 if (arg == endofnumber || value < 0 || value > 0777)
1505 fatal("%.200s line %d: Bad mask.", filename, linenum);
1506 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1507 break;
1508
1509 case oStreamLocalBindUnlink:
1510 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1511 goto parse_flag;
1512
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001513 case oRevokedHostKeys:
1514 charptr = &options->revoked_host_keys;
1515 goto parse_string;
1516
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001517 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001518 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001519 arg = strdelim(&s);
1520 if (!arg || *arg == '\0')
1521 fatal("%.200s line %d: Missing argument.",
1522 filename, linenum);
1523 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1524 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1525 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001526 if (*activep && *intptr == -1)
1527 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001528 break;
1529
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001530 case oUpdateHostkeys:
1531 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001532 multistate_ptr = multistate_yesnoask;
1533 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001534
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001535 case oHostbasedKeyTypes:
1536 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001537 goto parse_keytypes;
1538
1539 case oPubkeyAcceptedKeyTypes:
1540 charptr = &options->pubkey_key_types;
1541 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001542
Ben Lindstrom4daea862002-06-09 20:04:02 +00001543 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001544 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001545 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001546 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001547
Damien Millerf9b3feb2003-05-16 11:38:32 +10001548 case oUnsupported:
1549 error("%s line %d: Unsupported option \"%s\"",
1550 filename, linenum, keyword);
1551 return 0;
1552
Damien Miller95def091999-11-25 00:26:21 +11001553 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001554 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001555 }
1556
1557 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001558 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001559 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001560 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001561 }
Damien Miller95def091999-11-25 00:26:21 +11001562 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001563}
1564
1565
Damien Miller5428f641999-11-25 11:54:57 +11001566/*
1567 * Reads the config file and modifies the options accordingly. Options
1568 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001569 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001570 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001571
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001572int
Damien Miller194fd902013-10-15 12:13:05 +11001573read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001574 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575{
Damien Miller95def091999-11-25 00:26:21 +11001576 FILE *f;
1577 char line[1024];
1578 int active, linenum;
1579 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580
Damien Miller57a44762004-04-20 20:11:57 +10001581 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001582 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Darren Tuckeraefa3682013-04-05 11:18:35 +11001584 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001585 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001586
Damien Miller33793852004-06-15 10:27:55 +10001587 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001588 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001589 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001590 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001591 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001592 }
1593
Damien Miller95def091999-11-25 00:26:21 +11001594 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001595
Damien Miller5428f641999-11-25 11:54:57 +11001596 /*
1597 * Mark that we are now processing the options. This flag is turned
1598 * on/off by Host specifications.
1599 */
Damien Miller95def091999-11-25 00:26:21 +11001600 active = 1;
1601 linenum = 0;
1602 while (fgets(line, sizeof(line), f)) {
1603 /* Update line number counter. */
1604 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001605 if (process_config_line(options, pw, host, original_host,
1606 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001607 bad_options++;
1608 }
1609 fclose(f);
1610 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001611 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001612 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001613 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001614}
1615
Damien Miller13f97b22014-02-24 15:57:55 +11001616/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1617int
1618option_clear_or_none(const char *o)
1619{
1620 return o == NULL || strcasecmp(o, "none") == 0;
1621}
1622
Damien Miller5428f641999-11-25 11:54:57 +11001623/*
1624 * Initializes options to special values that indicate that they have not yet
1625 * been set. Read_config_file will only set options with this value. Options
1626 * are processed in the following order: command line, user config file,
1627 * system config file. Last, fill_default_options is called.
1628 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001629
Damien Miller4af51302000-04-16 11:18:38 +10001630void
Damien Miller95def091999-11-25 00:26:21 +11001631initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632{
Damien Miller95def091999-11-25 00:26:21 +11001633 memset(options, 'X', sizeof(*options));
1634 options->forward_agent = -1;
1635 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001636 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001637 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001638 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001639 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001640 options->fwd_opts.gateway_ports = -1;
1641 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1642 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001643 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001644 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001645 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001646 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001647 options->gss_authentication = -1;
1648 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001649 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001650 options->kbd_interactive_authentication = -1;
1651 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001652 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001653 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001654 options->batch_mode = -1;
1655 options->check_host_ip = -1;
1656 options->strict_host_key_checking = -1;
1657 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001658 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001659 options->compression_level = -1;
1660 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001661 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001662 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001663 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001664 options->number_of_password_prompts = -1;
1665 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001666 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001667 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001668 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001669 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001670 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001671 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001672 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001673 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001674 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001675 options->proxy_command = NULL;
1676 options->user = NULL;
1677 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001678 options->num_system_hostfiles = 0;
1679 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001680 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001681 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001682 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001683 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001684 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001685 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001686 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001687 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001688 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001689 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001690 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001691 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001692 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001693 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001694 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001695 options->server_alive_interval = -1;
1696 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001697 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001698 options->control_path = NULL;
1699 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001700 options->control_persist = -1;
1701 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001702 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001703 options->tun_open = -1;
1704 options->tun_local = -1;
1705 options->tun_remote = -1;
1706 options->local_command = NULL;
1707 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001708 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001709 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001710 options->ip_qos_interactive = -1;
1711 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001712 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001713 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001714 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001715 options->num_canonical_domains = 0;
1716 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001717 options->canonicalize_max_dots = -1;
1718 options->canonicalize_fallback_local = -1;
1719 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001720 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001721 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001722 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001723 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001724 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001725}
1726
Damien Miller5428f641999-11-25 11:54:57 +11001727/*
Damien Miller13f97b22014-02-24 15:57:55 +11001728 * A petite version of fill_default_options() that just fills the options
1729 * needed for hostname canonicalization to proceed.
1730 */
1731void
1732fill_default_options_for_canonicalization(Options *options)
1733{
1734 if (options->canonicalize_max_dots == -1)
1735 options->canonicalize_max_dots = 1;
1736 if (options->canonicalize_fallback_local == -1)
1737 options->canonicalize_fallback_local = 1;
1738 if (options->canonicalize_hostname == -1)
1739 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1740}
1741
1742/*
Damien Miller5428f641999-11-25 11:54:57 +11001743 * Called after processing other sources of option data, this fills those
1744 * options for which no value has been specified with their default values.
1745 */
Damien Miller4af51302000-04-16 11:18:38 +10001746void
Damien Miller95def091999-11-25 00:26:21 +11001747fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748{
Damien Miller95def091999-11-25 00:26:21 +11001749 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001750 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001751 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001752 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001753 if (options->forward_x11_trusted == -1)
1754 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001755 if (options->forward_x11_timeout == -1)
1756 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001757 if (options->exit_on_forward_failure == -1)
1758 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001759 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001760 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001761 if (options->fwd_opts.gateway_ports == -1)
1762 options->fwd_opts.gateway_ports = 0;
1763 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1764 options->fwd_opts.streamlocal_bind_mask = 0177;
1765 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1766 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001767 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001768 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001769 if (options->rsa_authentication == -1)
1770 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001771 if (options->pubkey_authentication == -1)
1772 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001773 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001774 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001775 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001776 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001777 if (options->gss_deleg_creds == -1)
1778 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001779 if (options->password_authentication == -1)
1780 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001781 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001782 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001783 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001784 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001785 if (options->hostbased_authentication == -1)
1786 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001787 if (options->batch_mode == -1)
1788 options->batch_mode = 0;
1789 if (options->check_host_ip == -1)
1790 options->check_host_ip = 1;
1791 if (options->strict_host_key_checking == -1)
1792 options->strict_host_key_checking = 2; /* 2 is default */
1793 if (options->compression == -1)
1794 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001795 if (options->tcp_keep_alive == -1)
1796 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001797 if (options->compression_level == -1)
1798 options->compression_level = 6;
1799 if (options->port == -1)
1800 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001801 if (options->address_family == -1)
1802 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001803 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001804 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001805 if (options->number_of_password_prompts == -1)
1806 options->number_of_password_prompts = 3;
1807 /* Selected in ssh_login(). */
1808 if (options->cipher == -1)
1809 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001810 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001811 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001812 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001813 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001814 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001815 add_identity_file(options, "~/",
1816 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001817 }
1818 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001819 add_identity_file(options, "~/",
1820 _PATH_SSH_CLIENT_ID_RSA, 0);
1821 add_identity_file(options, "~/",
1822 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001823#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001824 add_identity_file(options, "~/",
1825 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001826#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001827 add_identity_file(options, "~/",
1828 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001829 }
Damien Millereba71ba2000-04-29 23:57:08 +10001830 }
Damien Miller95def091999-11-25 00:26:21 +11001831 if (options->escape_char == -1)
1832 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001833 if (options->num_system_hostfiles == 0) {
1834 options->system_hostfiles[options->num_system_hostfiles++] =
1835 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1836 options->system_hostfiles[options->num_system_hostfiles++] =
1837 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1838 }
1839 if (options->num_user_hostfiles == 0) {
1840 options->user_hostfiles[options->num_user_hostfiles++] =
1841 xstrdup(_PATH_SSH_USER_HOSTFILE);
1842 options->user_hostfiles[options->num_user_hostfiles++] =
1843 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1844 }
Damien Millerfcd93202002-02-05 12:26:34 +11001845 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001846 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001847 if (options->clear_forwardings == 1)
1848 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001849 if (options->no_host_authentication_for_localhost == - 1)
1850 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001851 if (options->identities_only == -1)
1852 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001853 if (options->enable_ssh_keysign == -1)
1854 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001855 if (options->rekey_limit == -1)
1856 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001857 if (options->rekey_interval == -1)
1858 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001859 if (options->verify_host_key_dns == -1)
1860 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001861 if (options->server_alive_interval == -1)
1862 options->server_alive_interval = 0;
1863 if (options->server_alive_count_max == -1)
1864 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001865 if (options->control_master == -1)
1866 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001867 if (options->control_persist == -1) {
1868 options->control_persist = 0;
1869 options->control_persist_timeout = 0;
1870 }
Damien Millere1776152005-03-01 21:47:37 +11001871 if (options->hash_known_hosts == -1)
1872 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001873 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001874 options->tun_open = SSH_TUNMODE_NO;
1875 if (options->tun_local == -1)
1876 options->tun_local = SSH_TUNID_ANY;
1877 if (options->tun_remote == -1)
1878 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001879 if (options->permit_local_command == -1)
1880 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001881 if (options->use_roaming == -1)
1882 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001883 if (options->visual_host_key == -1)
1884 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001885 if (options->ip_qos_interactive == -1)
1886 options->ip_qos_interactive = IPTOS_LOWDELAY;
1887 if (options->ip_qos_bulk == -1)
1888 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001889 if (options->request_tty == -1)
1890 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001891 if (options->proxy_use_fdpass == -1)
1892 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001893 if (options->canonicalize_max_dots == -1)
1894 options->canonicalize_max_dots = 1;
1895 if (options->canonicalize_fallback_local == -1)
1896 options->canonicalize_fallback_local = 1;
1897 if (options->canonicalize_hostname == -1)
1898 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001899 if (options->fingerprint_hash == -1)
1900 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001901 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00001902 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001903 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
1904 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
1905 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
1906 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1907 &options->hostbased_key_types) != 0 ||
1908 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1909 &options->pubkey_key_types) != 0)
1910 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001911
Damien Millere9fc72e2013-10-15 12:14:12 +11001912#define CLEAR_ON_NONE(v) \
1913 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001914 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001915 free(v); \
1916 v = NULL; \
1917 } \
1918 } while(0)
1919 CLEAR_ON_NONE(options->local_command);
1920 CLEAR_ON_NONE(options->proxy_command);
1921 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001922 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001923 /* options->user will be set in the main program if appropriate */
1924 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001925 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001926 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001927}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001928
Damien Miller7acefbb2014-07-18 14:11:24 +10001929struct fwdarg {
1930 char *arg;
1931 int ispath;
1932};
1933
1934/*
1935 * parse_fwd_field
1936 * parses the next field in a port forwarding specification.
1937 * sets fwd to the parsed field and advances p past the colon
1938 * or sets it to NULL at end of string.
1939 * returns 0 on success, else non-zero.
1940 */
1941static int
1942parse_fwd_field(char **p, struct fwdarg *fwd)
1943{
1944 char *ep, *cp = *p;
1945 int ispath = 0;
1946
1947 if (*cp == '\0') {
1948 *p = NULL;
1949 return -1; /* end of string */
1950 }
1951
1952 /*
1953 * A field escaped with square brackets is used literally.
1954 * XXX - allow ']' to be escaped via backslash?
1955 */
1956 if (*cp == '[') {
1957 /* find matching ']' */
1958 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1959 if (*ep == '/')
1960 ispath = 1;
1961 }
1962 /* no matching ']' or not at end of field. */
1963 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1964 return -1;
1965 /* NUL terminate the field and advance p past the colon */
1966 *ep++ = '\0';
1967 if (*ep != '\0')
1968 *ep++ = '\0';
1969 fwd->arg = cp + 1;
1970 fwd->ispath = ispath;
1971 *p = ep;
1972 return 0;
1973 }
1974
1975 for (cp = *p; *cp != '\0'; cp++) {
1976 switch (*cp) {
1977 case '\\':
1978 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00001979 if (*cp == '\0')
1980 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10001981 break;
1982 case '/':
1983 ispath = 1;
1984 break;
1985 case ':':
1986 *cp++ = '\0';
1987 goto done;
1988 }
1989 }
1990done:
1991 fwd->arg = *p;
1992 fwd->ispath = ispath;
1993 *p = cp;
1994 return 0;
1995}
1996
Damien Millerf91ee4c2005-03-01 21:24:33 +11001997/*
1998 * parse_forward
1999 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002000 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002001 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2002 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002003 * dynamicfwd == 1
2004 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002005 * returns number of arguments parsed or zero on error
2006 */
2007int
Damien Miller7acefbb2014-07-18 14:11:24 +10002008parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002009{
Damien Miller7acefbb2014-07-18 14:11:24 +10002010 struct fwdarg fwdargs[4];
2011 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002012 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002013
Damien Miller7acefbb2014-07-18 14:11:24 +10002014 memset(fwd, 0, sizeof(*fwd));
2015 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002016
2017 cp = p = xstrdup(fwdspec);
2018
2019 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002020 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002021 cp++;
2022
Damien Miller7acefbb2014-07-18 14:11:24 +10002023 for (i = 0; i < 4; ++i) {
2024 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002025 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002026 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002027
Damien Millerf4b39532008-11-03 19:28:21 +11002028 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002029 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002030 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002031 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002032
2033 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002034 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002035 if (fwdargs[0].ispath) {
2036 fwd->listen_path = xstrdup(fwdargs[0].arg);
2037 fwd->listen_port = PORT_STREAMLOCAL;
2038 } else {
2039 fwd->listen_host = NULL;
2040 fwd->listen_port = a2port(fwdargs[0].arg);
2041 }
Damien Millera699d952008-11-03 19:27:34 +11002042 fwd->connect_host = xstrdup("socks");
2043 break;
2044
2045 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002046 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2047 fwd->listen_path = xstrdup(fwdargs[0].arg);
2048 fwd->listen_port = PORT_STREAMLOCAL;
2049 fwd->connect_path = xstrdup(fwdargs[1].arg);
2050 fwd->connect_port = PORT_STREAMLOCAL;
2051 } else if (fwdargs[1].ispath) {
2052 fwd->listen_host = NULL;
2053 fwd->listen_port = a2port(fwdargs[0].arg);
2054 fwd->connect_path = xstrdup(fwdargs[1].arg);
2055 fwd->connect_port = PORT_STREAMLOCAL;
2056 } else {
2057 fwd->listen_host = xstrdup(fwdargs[0].arg);
2058 fwd->listen_port = a2port(fwdargs[1].arg);
2059 fwd->connect_host = xstrdup("socks");
2060 }
Damien Millera699d952008-11-03 19:27:34 +11002061 break;
2062
Damien Millerf91ee4c2005-03-01 21:24:33 +11002063 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002064 if (fwdargs[0].ispath) {
2065 fwd->listen_path = xstrdup(fwdargs[0].arg);
2066 fwd->listen_port = PORT_STREAMLOCAL;
2067 fwd->connect_host = xstrdup(fwdargs[1].arg);
2068 fwd->connect_port = a2port(fwdargs[2].arg);
2069 } else if (fwdargs[2].ispath) {
2070 fwd->listen_host = xstrdup(fwdargs[0].arg);
2071 fwd->listen_port = a2port(fwdargs[1].arg);
2072 fwd->connect_path = xstrdup(fwdargs[2].arg);
2073 fwd->connect_port = PORT_STREAMLOCAL;
2074 } else {
2075 fwd->listen_host = NULL;
2076 fwd->listen_port = a2port(fwdargs[0].arg);
2077 fwd->connect_host = xstrdup(fwdargs[1].arg);
2078 fwd->connect_port = a2port(fwdargs[2].arg);
2079 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002080 break;
2081
2082 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002083 fwd->listen_host = xstrdup(fwdargs[0].arg);
2084 fwd->listen_port = a2port(fwdargs[1].arg);
2085 fwd->connect_host = xstrdup(fwdargs[2].arg);
2086 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002087 break;
2088 default:
2089 i = 0; /* failure */
2090 }
2091
Darren Tuckera627d422013-06-02 07:31:17 +10002092 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002093
Damien Millera699d952008-11-03 19:27:34 +11002094 if (dynamicfwd) {
2095 if (!(i == 1 || i == 2))
2096 goto fail_free;
2097 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002098 if (!(i == 3 || i == 4)) {
2099 if (fwd->connect_path == NULL &&
2100 fwd->listen_path == NULL)
2101 goto fail_free;
2102 }
2103 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002104 goto fail_free;
2105 }
2106
Damien Miller7acefbb2014-07-18 14:11:24 +10002107 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2108 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002109 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002110 if (fwd->connect_host != NULL &&
2111 strlen(fwd->connect_host) >= NI_MAXHOST)
2112 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002113 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2114 if (fwd->connect_path != NULL &&
2115 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2116 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002117 if (fwd->listen_host != NULL &&
2118 strlen(fwd->listen_host) >= NI_MAXHOST)
2119 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002120 if (fwd->listen_path != NULL &&
2121 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2122 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002123
2124 return (i);
2125
2126 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002127 free(fwd->connect_host);
2128 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002129 free(fwd->connect_path);
2130 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002131 free(fwd->listen_host);
2132 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002133 free(fwd->listen_path);
2134 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002135 return (0);
2136}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002137
2138/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2139static const char *
2140fmt_multistate_int(int val, const struct multistate *m)
2141{
2142 u_int i;
2143
2144 for (i = 0; m[i].key != NULL; i++) {
2145 if (m[i].value == val)
2146 return m[i].key;
2147 }
2148 return "UNKNOWN";
2149}
2150
2151static const char *
2152fmt_intarg(OpCodes code, int val)
2153{
2154 if (val == -1)
2155 return "unset";
2156 switch (code) {
2157 case oAddressFamily:
2158 return fmt_multistate_int(val, multistate_addressfamily);
2159 case oVerifyHostKeyDNS:
2160 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002161 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002162 return fmt_multistate_int(val, multistate_yesnoask);
2163 case oControlMaster:
2164 return fmt_multistate_int(val, multistate_controlmaster);
2165 case oTunnel:
2166 return fmt_multistate_int(val, multistate_tunnel);
2167 case oRequestTTY:
2168 return fmt_multistate_int(val, multistate_requesttty);
2169 case oCanonicalizeHostname:
2170 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002171 case oFingerprintHash:
2172 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002173 case oProtocol:
2174 switch (val) {
2175 case SSH_PROTO_1:
2176 return "1";
2177 case SSH_PROTO_2:
2178 return "2";
2179 case (SSH_PROTO_1|SSH_PROTO_2):
2180 return "2,1";
2181 default:
2182 return "UNKNOWN";
2183 }
2184 default:
2185 switch (val) {
2186 case 0:
2187 return "no";
2188 case 1:
2189 return "yes";
2190 default:
2191 return "UNKNOWN";
2192 }
2193 }
2194}
2195
2196static const char *
2197lookup_opcode_name(OpCodes code)
2198{
2199 u_int i;
2200
2201 for (i = 0; keywords[i].name != NULL; i++)
2202 if (keywords[i].opcode == code)
2203 return(keywords[i].name);
2204 return "UNKNOWN";
2205}
2206
2207static void
2208dump_cfg_int(OpCodes code, int val)
2209{
2210 printf("%s %d\n", lookup_opcode_name(code), val);
2211}
2212
2213static void
2214dump_cfg_fmtint(OpCodes code, int val)
2215{
2216 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2217}
2218
2219static void
2220dump_cfg_string(OpCodes code, const char *val)
2221{
2222 if (val == NULL)
2223 return;
2224 printf("%s %s\n", lookup_opcode_name(code), val);
2225}
2226
2227static void
2228dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2229{
2230 u_int i;
2231
2232 for (i = 0; i < count; i++)
2233 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2234}
2235
2236static void
2237dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2238{
2239 u_int i;
2240
2241 printf("%s", lookup_opcode_name(code));
2242 for (i = 0; i < count; i++)
2243 printf(" %s", vals[i]);
2244 printf("\n");
2245}
2246
2247static void
2248dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2249{
2250 const struct Forward *fwd;
2251 u_int i;
2252
2253 /* oDynamicForward */
2254 for (i = 0; i < count; i++) {
2255 fwd = &fwds[i];
2256 if (code == oDynamicForward &&
2257 strcmp(fwd->connect_host, "socks") != 0)
2258 continue;
2259 if (code == oLocalForward &&
2260 strcmp(fwd->connect_host, "socks") == 0)
2261 continue;
2262 printf("%s", lookup_opcode_name(code));
2263 if (fwd->listen_port == PORT_STREAMLOCAL)
2264 printf(" %s", fwd->listen_path);
2265 else if (fwd->listen_host == NULL)
2266 printf(" %d", fwd->listen_port);
2267 else {
2268 printf(" [%s]:%d",
2269 fwd->listen_host, fwd->listen_port);
2270 }
2271 if (code != oDynamicForward) {
2272 if (fwd->connect_port == PORT_STREAMLOCAL)
2273 printf(" %s", fwd->connect_path);
2274 else if (fwd->connect_host == NULL)
2275 printf(" %d", fwd->connect_port);
2276 else {
2277 printf(" [%s]:%d",
2278 fwd->connect_host, fwd->connect_port);
2279 }
2280 }
2281 printf("\n");
2282 }
2283}
2284
2285void
2286dump_client_config(Options *o, const char *host)
2287{
2288 int i;
2289 char vbuf[5];
2290
djm@openbsd.org60a92472015-08-21 23:53:08 +00002291 /* This is normally prepared in ssh_kex2 */
2292 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2293 fatal("%s: kex_assemble_names failed", __func__);
2294
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002295 /* Most interesting options first: user, host, port */
2296 dump_cfg_string(oUser, o->user);
2297 dump_cfg_string(oHostName, host);
2298 dump_cfg_int(oPort, o->port);
2299
2300 /* Flag options */
2301 dump_cfg_fmtint(oAddressFamily, o->address_family);
2302 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2303 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2304 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2305 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2306 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2307 dump_cfg_fmtint(oCompression, o->compression);
2308 dump_cfg_fmtint(oControlMaster, o->control_master);
2309 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2310 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002311 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002312 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2313 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2314 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2315 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2316#ifdef GSSAPI
2317 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2318 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2319#endif /* GSSAPI */
2320 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2321 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2322 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2323 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2324 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2325 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2326 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2327 dump_cfg_fmtint(oProtocol, o->protocol);
2328 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2329 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2330 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2331 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2332 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2333 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2334 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2335 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2336 dump_cfg_fmtint(oTunnel, o->tun_open);
2337 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2338 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2339 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002340 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002341
2342 /* Integer options */
2343 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2344 dump_cfg_int(oCompressionLevel, o->compression_level);
2345 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2346 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2347 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2348 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2349 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2350
2351 /* String options */
2352 dump_cfg_string(oBindAddress, o->bind_address);
2353 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2354 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002355 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002356 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002357 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002358 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2359 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2360 dump_cfg_string(oLocalCommand, o->local_command);
2361 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2362 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2363 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2364 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2365 dump_cfg_string(oProxyCommand, o->proxy_command);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002366 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002367 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002368 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002369
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002370 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002371 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2372 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2373 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2374
2375 /* String array options */
2376 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2377 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2378 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2379 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2380 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2381
2382 /* Special cases */
2383
2384 /* oConnectTimeout */
2385 if (o->connection_timeout == -1)
2386 printf("connecttimeout none\n");
2387 else
2388 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2389
2390 /* oTunnelDevice */
2391 printf("tunneldevice");
2392 if (o->tun_local == SSH_TUNID_ANY)
2393 printf(" any");
2394 else
2395 printf(" %d", o->tun_local);
2396 if (o->tun_remote == SSH_TUNID_ANY)
2397 printf(":any");
2398 else
2399 printf(":%d", o->tun_remote);
2400 printf("\n");
2401
2402 /* oCanonicalizePermittedCNAMEs */
2403 if ( o->num_permitted_cnames > 0) {
2404 printf("canonicalizePermittedcnames");
2405 for (i = 0; i < o->num_permitted_cnames; i++) {
2406 printf(" %s:%s", o->permitted_cnames[i].source_list,
2407 o->permitted_cnames[i].target_list);
2408 }
2409 printf("\n");
2410 }
2411
2412 /* oCipher */
2413 if (o->cipher != SSH_CIPHER_NOT_SET)
2414 printf("Cipher %s\n", cipher_name(o->cipher));
2415
2416 /* oControlPersist */
2417 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2418 dump_cfg_fmtint(oControlPersist, o->control_persist);
2419 else
2420 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2421
2422 /* oEscapeChar */
2423 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2424 printf("escapechar none\n");
2425 else {
2426 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2427 printf("escapechar %s\n", vbuf);
2428 }
2429
2430 /* oIPQoS */
2431 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2432 printf("%s\n", iptos2str(o->ip_qos_bulk));
2433
2434 /* oRekeyLimit */
2435 printf("rekeylimit %lld %d\n",
2436 (long long)o->rekey_limit, o->rekey_interval);
2437
2438 /* oStreamLocalBindMask */
2439 printf("streamlocalbindmask 0%o\n",
2440 o->fwd_opts.streamlocal_bind_mask);
2441}