blob: c692f7dd229479fd26f08c4f01380d7e407d688e [file] [log] [blame]
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +00001/* $OpenBSD: readconf.c,v 1.251 2016/04/06 06:42:17 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
Damien Miller194fd902013-10-15 12:13:05 +110020#include <sys/wait.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100021#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022
23#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110024#include <netinet/in_systm.h>
25#include <netinet/ip.h>
Darren Tucker0eeafcd2014-01-31 14:18:51 +110026#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millerc7b06362006-03-15 11:53:45 +110028#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
Damien Miller194fd902013-10-15 12:13:05 +110030#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100032#include <netdb.h>
Darren Tuckera3357662014-01-18 00:03:57 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller194fd902013-10-15 12:13:05 +110036#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100043#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100044#endif
djm@openbsd.org957fbce2014-10-08 22:20:25 +000045#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
46# include <vis.h>
47#endif
Damien Millerc7b06362006-03-15 11:53:45 +110048
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "cipher.h"
53#include "pathnames.h"
54#include "log.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000055#include "sshkey.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100056#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "readconf.h"
58#include "match.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "kex.h"
60#include "mac.h"
Damien Miller194fd902013-10-15 12:13:05 +110061#include "uidswap.h"
djm@openbsd.org957fbce2014-10-08 22:20:25 +000062#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000063#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65/* Format of the configuration file:
66
67 # Configuration data is parsed as follows:
68 # 1. command line options
69 # 2. user-specific file
70 # 3. system-wide file
71 # Any configuration value is only changed the first time it is set.
72 # Thus, host-specific definitions should be at the beginning of the
73 # configuration file, and defaults at the end.
74
75 # Host-specific declarations. These may override anything above. A single
76 # host may match multiple declarations; these are processed in the order
77 # that they are given in.
78
79 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000080 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82 Host fake.com
83 HostName another.host.name.real.org
84 User blaah
85 Port 34289
86 ForwardX11 no
87 ForwardAgent no
88
89 Host books.com
90 RemoteForward 9999 shadows.cs.hut.fi:9999
91 Cipher 3des
92
93 Host fascist.blob.com
94 Port 23123
95 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 PasswordAuthentication no
97
98 Host puukko.hut.fi
99 User t35124p
100 ProxyCommand ssh-proxy %h %p
101
102 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +0000103 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105 Host *.su
106 Cipher none
107 PasswordAuthentication no
108
Damien Millerd27b9472005-12-13 19:29:02 +1100109 Host vpn.fake.com
110 Tunnel yes
111 TunnelDevice 3
112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 # Defaults for various options
114 Host *
115 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 PasswordAuthentication yes
118 RSAAuthentication yes
119 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100121 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 IdentityFile ~/.ssh/identity
123 Port 22
124 EscapeChar ~
125
126*/
127
128/* Keyword tokens. */
129
Damien Miller95def091999-11-25 00:26:21 +1100130typedef enum {
131 oBadOption,
Damien Miller194fd902013-10-15 12:13:05 +1100132 oHost, oMatch,
Damien Miller1ab6a512010-06-26 10:02:24 +1000133 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
134 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000135 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000136 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100137 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000138 oCertificateFile, oAddKeysToAgent,
Damien Miller194fd902013-10-15 12:13:05 +1100139 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100140 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
141 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100142 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000143 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000144 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000145 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000146 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100147 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000148 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000149 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000150 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100151 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000152 oSendEnv, oControlPath, oControlMaster, oControlPersist,
153 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100154 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
markus@openbsd.orga3068632016-01-14 16:17:39 +0000155 oVisualHostKey,
Damien Miller1262b662013-08-21 02:44:24 +1000156 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100157 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
158 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000159 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000160 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000161 oPubkeyAcceptedKeyTypes,
Darren Tucker07636982013-05-16 20:30:03 +1000162 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163} OpCodes;
164
165/* Textual representations of the tokens. */
166
Damien Miller95def091999-11-25 00:26:21 +1100167static struct {
168 const char *name;
169 OpCodes opcode;
170} keywords[] = {
171 { "forwardagent", oForwardAgent },
172 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000173 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000174 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000175 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000176 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "gatewayports", oGatewayPorts },
178 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000179 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100180 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100181 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
182 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100183 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100184 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000185 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000186 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000187 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000188 { "challengeresponseauthentication", oChallengeResponseAuthentication },
189 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
190 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000191 { "kerberosauthentication", oUnsupported },
192 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000193 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000194#if defined(GSSAPI)
195 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000196 { "gssapidelegatecredentials", oGssDelegateCreds },
197#else
198 { "gssapiauthentication", oUnsupported },
199 { "gssapidelegatecredentials", oUnsupported },
200#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000201 { "fallbacktorsh", oDeprecated },
202 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100203 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100204 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100205 { "identitiesonly", oIdentitiesOnly },
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000206 { "certificatefile", oCertificateFile },
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000207 { "addkeystoagent", oAddKeysToAgent },
Damien Miller95def091999-11-25 00:26:21 +1100208 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000209 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100210 { "proxycommand", oProxyCommand },
211 { "port", oPort },
212 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000213 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000214 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000215 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100216 { "remoteforward", oRemoteForward },
217 { "localforward", oLocalForward },
218 { "user", oUser },
219 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100220 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100221 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100222 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000223 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100224 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000225 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100226 { "connectionattempts", oConnectionAttempts },
227 { "batchmode", oBatchMode },
228 { "checkhostip", oCheckHostIP },
229 { "stricthostkeychecking", oStrictHostKeyChecking },
230 { "compression", oCompression },
231 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100232 { "tcpkeepalive", oTCPKeepAlive },
233 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100234 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100235 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000236 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000237 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000238 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000239 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100240#ifdef ENABLE_PKCS11
241 { "smartcarddevice", oPKCS11Provider },
242 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000243#else
244 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100245 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000246#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100247 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000248 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000249 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100250 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000251 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000252 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000253 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100254 { "serveraliveinterval", oServerAliveInterval },
255 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000256 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000257 { "controlpath", oControlPath },
258 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000259 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100260 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100261 { "tunnel", oTunnel },
262 { "tunneldevice", oTunnelDevice },
263 { "localcommand", oLocalCommand },
264 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000265 { "visualhostkey", oVisualHostKey },
markus@openbsd.orga3068632016-01-14 16:17:39 +0000266 { "useroaming", oDeprecated },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000267 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100268 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000269 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000270 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100271 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100272 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
273 { "canonicalizehostname", oCanonicalizeHostname },
274 { "canonicalizemaxdots", oCanonicalizeMaxDots },
275 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000276 { "streamlocalbindmask", oStreamLocalBindMask },
277 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000278 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000279 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000280 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000281 { "hostbasedkeytypes", oHostbasedKeyTypes },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000282 { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000283 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100284
Ben Lindstrom65366a82001-12-06 16:32:47 +0000285 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100286};
287
Damien Miller5428f641999-11-25 11:54:57 +1100288/*
289 * Adds a local TCP/IP port forward to options. Never returns if there is an
290 * error.
291 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Miller4af51302000-04-16 11:18:38 +1000293void
Damien Miller7acefbb2014-07-18 14:11:24 +1000294add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295{
Damien Miller7acefbb2014-07-18 14:11:24 +1000296 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000297 int i;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000298#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100299 extern uid_t original_real_uid;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000300
Damien Miller7acefbb2014-07-18 14:11:24 +1000301 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
302 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000303 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100304#endif
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000305 /* Don't add duplicates */
306 for (i = 0; i < options->num_local_forwards; i++) {
307 if (forward_equals(newfwd, options->local_forwards + i))
308 return;
309 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000310 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000311 options->num_local_forwards + 1,
312 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100313 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100314
Damien Miller1a0442f2008-11-05 16:30:06 +1100315 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100316 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000317 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100318 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100319 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000320 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321}
322
Damien Miller5428f641999-11-25 11:54:57 +1100323/*
324 * Adds a remote TCP/IP port forward to options. Never returns if there is
325 * an error.
326 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Miller4af51302000-04-16 11:18:38 +1000328void
Damien Miller7acefbb2014-07-18 14:11:24 +1000329add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330{
Damien Miller7acefbb2014-07-18 14:11:24 +1000331 struct Forward *fwd;
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000332 int i;
Damien Miller232cfb12010-06-26 09:50:30 +1000333
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +0000334 /* Don't add duplicates */
335 for (i = 0; i < options->num_remote_forwards; i++) {
336 if (forward_equals(newfwd, options->remote_forwards + i))
337 return;
338 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000339 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000340 options->num_remote_forwards + 1,
341 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100342 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100343
Damien Miller1a0442f2008-11-05 16:30:06 +1100344 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100345 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000346 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100347 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100348 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000349 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100350 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000351 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352}
353
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000354static void
355clear_forwardings(Options *options)
356{
357 int i;
358
Damien Millerf91ee4c2005-03-01 21:24:33 +1100359 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000360 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000361 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000362 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000363 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100364 }
Damien Miller232cfb12010-06-26 09:50:30 +1000365 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000366 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000367 options->local_forwards = NULL;
368 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000369 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100370 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000371 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000372 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000373 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000374 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100375 }
Damien Miller232cfb12010-06-26 09:50:30 +1000376 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000377 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000378 options->remote_forwards = NULL;
379 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000380 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100381 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000382}
383
Darren Tucker19104782013-04-05 11:13:08 +1100384void
djm@openbsd.org4e44a792015-09-24 06:15:11 +0000385add_certificate_file(Options *options, const char *path, int userprovided)
386{
387 int i;
388
389 if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
390 fatal("Too many certificate files specified (max %d)",
391 SSH_MAX_CERTIFICATE_FILES);
392
393 /* Avoid registering duplicates */
394 for (i = 0; i < options->num_certificate_files; i++) {
395 if (options->certificate_file_userprovided[i] == userprovided &&
396 strcmp(options->certificate_files[i], path) == 0) {
397 debug2("%s: ignoring duplicate key %s", __func__, path);
398 return;
399 }
400 }
401
402 options->certificate_file_userprovided[options->num_certificate_files] =
403 userprovided;
404 options->certificate_files[options->num_certificate_files++] =
405 xstrdup(path);
406}
407
408void
Darren Tucker19104782013-04-05 11:13:08 +1100409add_identity_file(Options *options, const char *dir, const char *filename,
410 int userprovided)
411{
412 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000413 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100414
415 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
416 fatal("Too many identity files specified (max %d)",
417 SSH_MAX_IDENTITY_FILES);
418
419 if (dir == NULL) /* no dir, filename is absolute */
420 path = xstrdup(filename);
421 else
422 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
423
Damien Miller15271902014-05-15 13:47:56 +1000424 /* Avoid registering duplicates */
425 for (i = 0; i < options->num_identity_files; i++) {
426 if (options->identity_file_userprovided[i] == userprovided &&
427 strcmp(options->identity_files[i], path) == 0) {
428 debug2("%s: ignoring duplicate key %s", __func__, path);
429 free(path);
430 return;
431 }
432 }
433
Darren Tucker19104782013-04-05 11:13:08 +1100434 options->identity_file_userprovided[options->num_identity_files] =
435 userprovided;
436 options->identity_files[options->num_identity_files++] = path;
437}
438
Damien Miller194fd902013-10-15 12:13:05 +1100439int
440default_ssh_port(void)
441{
442 static int port;
443 struct servent *sp;
444
445 if (port == 0) {
446 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
447 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
448 }
449 return port;
450}
451
452/*
453 * Execute a command in a shell.
454 * Return its exit status or -1 on abnormal exit.
455 */
456static int
457execute_in_shell(const char *cmd)
458{
dtucker@openbsd.org97e184e2015-10-25 23:14:03 +0000459 char *shell;
Damien Miller194fd902013-10-15 12:13:05 +1100460 pid_t pid;
461 int devnull, status;
462 extern uid_t original_real_uid;
463
464 if ((shell = getenv("SHELL")) == NULL)
465 shell = _PATH_BSHELL;
466
Damien Miller194fd902013-10-15 12:13:05 +1100467 /* Need this to redirect subprocess stdin/out */
468 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
469 fatal("open(/dev/null): %s", strerror(errno));
470
471 debug("Executing command: '%.500s'", cmd);
472
473 /* Fork and execute the command. */
474 if ((pid = fork()) == 0) {
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000475 char *argv[4];
Damien Miller194fd902013-10-15 12:13:05 +1100476
477 /* Child. Permanently give up superuser privileges. */
478 permanently_drop_suid(original_real_uid);
479
480 /* Redirect child stdin and stdout. Leave stderr */
481 if (dup2(devnull, STDIN_FILENO) == -1)
482 fatal("dup2: %s", strerror(errno));
483 if (dup2(devnull, STDOUT_FILENO) == -1)
484 fatal("dup2: %s", strerror(errno));
485 if (devnull > STDERR_FILENO)
486 close(devnull);
487 closefrom(STDERR_FILENO + 1);
488
djm@openbsd.orga4aef3e2015-10-27 08:54:52 +0000489 argv[0] = shell;
490 argv[1] = "-c";
491 argv[2] = xstrdup(cmd);
492 argv[3] = NULL;
493
Damien Miller194fd902013-10-15 12:13:05 +1100494 execv(argv[0], argv);
495 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
496 /* Die with signal to make this error apparent to parent. */
497 signal(SIGTERM, SIG_DFL);
498 kill(getpid(), SIGTERM);
499 _exit(1);
500 }
501 /* Parent. */
502 if (pid < 0)
503 fatal("%s: fork: %.100s", __func__, strerror(errno));
504
505 close(devnull);
Damien Miller194fd902013-10-15 12:13:05 +1100506
507 while (waitpid(pid, &status, 0) == -1) {
508 if (errno != EINTR && errno != EAGAIN)
509 fatal("%s: waitpid: %s", __func__, strerror(errno));
510 }
511 if (!WIFEXITED(status)) {
512 error("command '%.100s' exited abnormally", cmd);
513 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000514 }
Damien Miller194fd902013-10-15 12:13:05 +1100515 debug3("command returned status %d", WEXITSTATUS(status));
516 return WEXITSTATUS(status);
517}
518
519/*
520 * Parse and execute a Match directive.
521 */
522static int
523match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000524 const char *host_arg, const char *original_host, int post_canon,
525 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100526{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000527 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100528 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000529 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100530 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
531
532 /*
533 * Configuration is likely to be incomplete at this point so we
534 * must be prepared to use default values.
535 */
536 port = options->port <= 0 ? default_ssh_port() : options->port;
537 ruser = options->user == NULL ? pw->pw_name : options->user;
djm@openbsd.org5a622842016-02-08 23:40:12 +0000538 if (post_canon) {
539 host = xstrdup(options->hostname);
540 } else if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100541 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100542 host = percent_expand(options->hostname,
543 "h", host_arg, (char *)NULL);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000544 } else {
Damien Miller084bcd22013-10-23 16:30:51 +1100545 host = xstrdup(host_arg);
djm@openbsd.org5a622842016-02-08 23:40:12 +0000546 }
Damien Miller194fd902013-10-15 12:13:05 +1100547
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 debug2("checking match for '%s' host %s originally %s",
549 cp, host, original_host);
550 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
551 criteria = NULL;
552 this_result = 1;
553 if ((negate = attrib[0] == '!'))
554 attrib++;
555 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100556 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000557 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100558 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000559 error("%.200s line %d: '%s' cannot be combined "
560 "with other Match attributes",
561 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100562 result = -1;
563 goto out;
564 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000565 if (result)
566 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100567 goto out;
568 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000569 attributes++;
570 if (strcasecmp(attrib, "canonical") == 0) {
571 r = !!post_canon; /* force bitmask member to boolean */
572 if (r == (negate ? 1 : 0))
573 this_result = result = 0;
574 debug3("%.200s line %d: %smatched '%s'",
575 filename, linenum,
576 this_result ? "" : "not ", oattrib);
577 continue;
578 }
579 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100580 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
581 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100582 result = -1;
583 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100584 }
Damien Miller194fd902013-10-15 12:13:05 +1100585 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000586 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000587 r = match_hostname(host, arg) == 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, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000591 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000592 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000593 if (r == (negate ? 1 : 0))
594 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100595 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000596 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000597 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000598 if (r == (negate ? 1 : 0))
599 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100600 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000601 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000602 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000603 if (r == (negate ? 1 : 0))
604 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100605 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100606 if (gethostname(thishost, sizeof(thishost)) == -1)
607 fatal("gethostname: %s", strerror(errno));
608 strlcpy(shorthost, thishost, sizeof(shorthost));
609 shorthost[strcspn(thishost, ".")] = '\0';
610 snprintf(portstr, sizeof(portstr), "%d", port);
611
612 cmd = percent_expand(arg,
613 "L", shorthost,
614 "d", pw->pw_dir,
615 "h", host,
616 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000617 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100618 "p", portstr,
619 "r", ruser,
620 "u", pw->pw_name,
621 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100622 if (result != 1) {
623 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000624 debug3("%.200s line %d: skipped exec "
625 "\"%.100s\"", filename, linenum, cmd);
626 free(cmd);
627 continue;
Damien Miller06287802014-02-24 15:56:45 +1100628 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000629 r = execute_in_shell(cmd);
630 if (r == -1) {
631 fatal("%.200s line %d: match exec "
632 "'%.100s' error", filename,
633 linenum, cmd);
634 }
635 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100636 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000637 /* Force exit status to boolean */
638 r = r == 0;
639 if (r == (negate ? 1 : 0))
640 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100641 } else {
642 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100643 result = -1;
644 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100645 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000646 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
647 filename, linenum, this_result ? "": "not ",
648 oattrib, criteria);
649 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100650 }
Damien Millercf31f382013-10-24 21:02:56 +1100651 if (attributes == 0) {
652 error("One or more attributes required for Match");
653 result = -1;
654 goto out;
655 }
Damien Miller084bcd22013-10-23 16:30:51 +1100656 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000657 if (result != -1)
658 debug2("match %sfound", result ? "" : "not ");
659 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100660 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100661 return result;
662}
663
Damien Miller0faf7472013-10-17 11:47:23 +1100664/* Check and prepare a domain name: removes trailing '.' and lowercases */
665static void
666valid_domain(char *name, const char *filename, int linenum)
667{
668 size_t i, l = strlen(name);
669 u_char c, last = '\0';
670
671 if (l == 0)
672 fatal("%s line %d: empty hostname suffix", filename, linenum);
673 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
674 fatal("%s line %d: hostname suffix \"%.100s\" "
675 "starts with invalid character", filename, linenum, name);
676 for (i = 0; i < l; i++) {
677 c = tolower((u_char)name[i]);
678 name[i] = (char)c;
679 if (last == '.' && c == '.')
680 fatal("%s line %d: hostname suffix \"%.100s\" contains "
681 "consecutive separators", filename, linenum, name);
682 if (c != '.' && c != '-' && !isalnum(c) &&
683 c != '_') /* technically invalid, but common */
684 fatal("%s line %d: hostname suffix \"%.100s\" contains "
685 "invalid characters", filename, linenum, name);
686 last = c;
687 }
688 if (name[l - 1] == '.')
689 name[l - 1] = '\0';
690}
691
Damien Miller5428f641999-11-25 11:54:57 +1100692/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000693 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100694 */
Damien Miller4af51302000-04-16 11:18:38 +1000695static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000696parse_token(const char *cp, const char *filename, int linenum,
697 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698{
Darren Tucker07636982013-05-16 20:30:03 +1000699 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700
Damien Miller95def091999-11-25 00:26:21 +1100701 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000702 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100703 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000704 if (ignored_unknown != NULL &&
705 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000706 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000707 error("%s: line %d: Bad configuration option: %s",
708 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100709 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710}
711
Damien Millere9fc72e2013-10-15 12:14:12 +1100712/* Multistate option parsing */
713struct multistate {
714 char *key;
715 int value;
716};
717static const struct multistate multistate_flag[] = {
718 { "true", 1 },
719 { "false", 0 },
720 { "yes", 1 },
721 { "no", 0 },
722 { NULL, -1 }
723};
724static const struct multistate multistate_yesnoask[] = {
725 { "true", 1 },
726 { "false", 0 },
727 { "yes", 1 },
728 { "no", 0 },
729 { "ask", 2 },
730 { NULL, -1 }
731};
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000732static const struct multistate multistate_yesnoaskconfirm[] = {
733 { "true", 1 },
734 { "false", 0 },
735 { "yes", 1 },
736 { "no", 0 },
737 { "ask", 2 },
738 { "confirm", 3 },
739 { NULL, -1 }
740};
Damien Millere9fc72e2013-10-15 12:14:12 +1100741static const struct multistate multistate_addressfamily[] = {
742 { "inet", AF_INET },
743 { "inet6", AF_INET6 },
744 { "any", AF_UNSPEC },
745 { NULL, -1 }
746};
747static const struct multistate multistate_controlmaster[] = {
748 { "true", SSHCTL_MASTER_YES },
749 { "yes", SSHCTL_MASTER_YES },
750 { "false", SSHCTL_MASTER_NO },
751 { "no", SSHCTL_MASTER_NO },
752 { "auto", SSHCTL_MASTER_AUTO },
753 { "ask", SSHCTL_MASTER_ASK },
754 { "autoask", SSHCTL_MASTER_AUTO_ASK },
755 { NULL, -1 }
756};
757static const struct multistate multistate_tunnel[] = {
758 { "ethernet", SSH_TUNMODE_ETHERNET },
759 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
760 { "true", SSH_TUNMODE_DEFAULT },
761 { "yes", SSH_TUNMODE_DEFAULT },
762 { "false", SSH_TUNMODE_NO },
763 { "no", SSH_TUNMODE_NO },
764 { NULL, -1 }
765};
766static const struct multistate multistate_requesttty[] = {
767 { "true", REQUEST_TTY_YES },
768 { "yes", REQUEST_TTY_YES },
769 { "false", REQUEST_TTY_NO },
770 { "no", REQUEST_TTY_NO },
771 { "force", REQUEST_TTY_FORCE },
772 { "auto", REQUEST_TTY_AUTO },
773 { NULL, -1 }
774};
Damien Miller38505592013-10-17 11:48:13 +1100775static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100776 { "true", SSH_CANONICALISE_YES },
777 { "false", SSH_CANONICALISE_NO },
778 { "yes", SSH_CANONICALISE_YES },
779 { "no", SSH_CANONICALISE_NO },
780 { "always", SSH_CANONICALISE_ALWAYS },
781 { NULL, -1 }
782};
Damien Millere9fc72e2013-10-15 12:14:12 +1100783
Damien Miller5428f641999-11-25 11:54:57 +1100784/*
785 * Processes a single option line as used in the configuration files. This
786 * only sets those values that have not already been set.
787 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100788#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100789int
Damien Miller194fd902013-10-15 12:13:05 +1100790process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000791 const char *original_host, char *line, const char *filename,
792 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793{
Damien Miller295ee632011-05-29 21:42:31 +1000794 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
795 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000796 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100797 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100798 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000799 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100800 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000801 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100802 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100803 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804
Damien Miller194fd902013-10-15 12:13:05 +1100805 if (activep == NULL) { /* We are processing a command line directive */
806 cmdline = 1;
807 activep = &cmdline;
808 }
809
Damien Millerc652cac2003-05-14 13:40:54 +1000810 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000811 if ((len = strlen(line)) == 0)
812 return 0;
813 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000814 if (strchr(WHITESPACE, line[len]) == NULL)
815 break;
816 line[len] = '\0';
817 }
818
Damien Millerbe484b52000-07-15 14:14:16 +1000819 s = line;
820 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100821 if ((keyword = strdelim(&s)) == NULL)
822 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000823 /* Ignore leading whitespace. */
824 if (*keyword == '\0')
825 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000826 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100827 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000828 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100829 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830
Darren Tucker07636982013-05-16 20:30:03 +1000831 opcode = parse_token(keyword, filename, linenum,
832 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000833
Damien Miller95def091999-11-25 00:26:21 +1100834 switch (opcode) {
835 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100836 /* don't panic, but count bad options */
837 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100838 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000839 case oIgnoredUnknownOption:
840 debug("%s line %d: Ignored unknown option \"%s\"",
841 filename, linenum, keyword);
842 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000843 case oConnectTimeout:
844 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100845parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000846 arg = strdelim(&s);
847 if (!arg || *arg == '\0')
848 fatal("%s line %d: missing time value.",
849 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000850 if (strcmp(arg, "none") == 0)
851 value = -1;
852 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000853 fatal("%s line %d: invalid time value.",
854 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100855 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000856 *intptr = value;
857 break;
858
Damien Miller95def091999-11-25 00:26:21 +1100859 case oForwardAgent:
860 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100861 parse_flag:
862 multistate_ptr = multistate_flag;
863 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000864 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000865 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100866 fatal("%s line %d: missing argument.",
867 filename, linenum);
868 value = -1;
869 for (i = 0; multistate_ptr[i].key != NULL; i++) {
870 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
871 value = multistate_ptr[i].value;
872 break;
873 }
874 }
875 if (value == -1)
876 fatal("%s line %d: unsupported option \"%s\".",
877 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100878 if (*activep && *intptr == -1)
879 *intptr = value;
880 break;
881
882 case oForwardX11:
883 intptr = &options->forward_x11;
884 goto parse_flag;
885
Darren Tucker0a118da2003-10-15 15:54:32 +1000886 case oForwardX11Trusted:
887 intptr = &options->forward_x11_trusted;
888 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000889
Damien Miller1ab6a512010-06-26 10:02:24 +1000890 case oForwardX11Timeout:
891 intptr = &options->forward_x11_timeout;
892 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000893
Damien Miller95def091999-11-25 00:26:21 +1100894 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000895 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100896 goto parse_flag;
897
Darren Tuckere7d4b192006-07-12 22:17:10 +1000898 case oExitOnForwardFailure:
899 intptr = &options->exit_on_forward_failure;
900 goto parse_flag;
901
Damien Miller95def091999-11-25 00:26:21 +1100902 case oUsePrivilegedPort:
903 intptr = &options->use_privileged_port;
904 goto parse_flag;
905
Damien Miller95def091999-11-25 00:26:21 +1100906 case oPasswordAuthentication:
907 intptr = &options->password_authentication;
908 goto parse_flag;
909
Damien Miller874d77b2000-10-14 16:23:11 +1100910 case oKbdInteractiveAuthentication:
911 intptr = &options->kbd_interactive_authentication;
912 goto parse_flag;
913
914 case oKbdInteractiveDevices:
915 charptr = &options->kbd_interactive_devices;
916 goto parse_string;
917
Damien Miller0bc1bd82000-11-13 22:57:25 +1100918 case oPubkeyAuthentication:
919 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000920 goto parse_flag;
921
Damien Miller95def091999-11-25 00:26:21 +1100922 case oRSAAuthentication:
923 intptr = &options->rsa_authentication;
924 goto parse_flag;
925
926 case oRhostsRSAAuthentication:
927 intptr = &options->rhosts_rsa_authentication;
928 goto parse_flag;
929
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000930 case oHostbasedAuthentication:
931 intptr = &options->hostbased_authentication;
932 goto parse_flag;
933
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000934 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000935 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100936 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000937
Darren Tucker0efd1552003-08-26 11:49:55 +1000938 case oGssAuthentication:
939 intptr = &options->gss_authentication;
940 goto parse_flag;
941
942 case oGssDelegateCreds:
943 intptr = &options->gss_deleg_creds;
944 goto parse_flag;
945
Damien Miller95def091999-11-25 00:26:21 +1100946 case oBatchMode:
947 intptr = &options->batch_mode;
948 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949
Damien Miller95def091999-11-25 00:26:21 +1100950 case oCheckHostIP:
951 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000952 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
Damien Miller37876e92003-05-15 10:19:46 +1000954 case oVerifyHostKeyDNS:
955 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100956 multistate_ptr = multistate_yesnoask;
957 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000958
Damien Miller95def091999-11-25 00:26:21 +1100959 case oStrictHostKeyChecking:
960 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100961 multistate_ptr = multistate_yesnoask;
962 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
Damien Miller95def091999-11-25 00:26:21 +1100964 case oCompression:
965 intptr = &options->compression;
966 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Damien Miller12c150e2003-12-17 16:31:10 +1100968 case oTCPKeepAlive:
969 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100970 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000972 case oNoHostAuthenticationForLocalhost:
973 intptr = &options->no_host_authentication_for_localhost;
974 goto parse_flag;
975
Damien Miller95def091999-11-25 00:26:21 +1100976 case oNumberOfPasswordPrompts:
977 intptr = &options->number_of_password_prompts;
978 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller95def091999-11-25 00:26:21 +1100980 case oCompressionLevel:
981 intptr = &options->compression_level;
982 goto parse_int;
983
Damien Millera5539d22003-04-09 20:50:06 +1000984 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000985 arg = strdelim(&s);
986 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000987 fatal("%.200s line %d: Missing argument.", filename,
988 linenum);
989 if (strcmp(arg, "default") == 0) {
990 val64 = 0;
991 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000992 if (scan_scaled(arg, &val64) == -1)
993 fatal("%.200s line %d: Bad number '%s': %s",
994 filename, linenum, arg, strerror(errno));
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000995 if (val64 != 0 && val64 < 16)
996 fatal("%.200s line %d: RekeyLimit too small",
997 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000998 }
Damien Miller3dff1762008-02-10 22:25:52 +1100999 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001000 options->rekey_limit = val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001001 if (s != NULL) { /* optional rekey interval present */
1002 if (strcmp(s, "none") == 0) {
1003 (void)strdelim(&s); /* discard */
1004 break;
1005 }
1006 intptr = &options->rekey_interval;
1007 goto parse_time;
1008 }
Damien Millera5539d22003-04-09 20:50:06 +10001009 break;
1010
Damien Miller95def091999-11-25 00:26:21 +11001011 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +10001012 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001013 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001014 fatal("%.200s line %d: Missing argument.", filename, linenum);
1015 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001016 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +10001017 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +11001018 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001019 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001020 add_identity_file(options, NULL,
1021 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +11001022 }
1023 break;
1024
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001025 case oCertificateFile:
1026 arg = strdelim(&s);
1027 if (!arg || *arg == '\0')
1028 fatal("%.200s line %d: Missing argument.",
1029 filename, linenum);
1030 if (*activep) {
1031 intptr = &options->num_certificate_files;
1032 if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1033 fatal("%.200s line %d: Too many certificate "
1034 "files specified (max %d).",
1035 filename, linenum,
1036 SSH_MAX_CERTIFICATE_FILES);
1037 }
1038 add_certificate_file(options, arg,
1039 flags & SSHCONF_USERCONF);
1040 }
1041 break;
1042
Damien Millerd3a18572000-06-07 19:55:44 +10001043 case oXAuthLocation:
1044 charptr=&options->xauth_location;
1045 goto parse_string;
1046
Damien Miller95def091999-11-25 00:26:21 +11001047 case oUser:
1048 charptr = &options->user;
1049parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +10001050 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001051 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +10001052 fatal("%.200s line %d: Missing argument.",
1053 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001054 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001055 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +11001056 break;
1057
1058 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001059 cpptr = (char **)&options->system_hostfiles;
1060 uintptr = &options->num_system_hostfiles;
1061 max_entries = SSH_MAX_HOSTS_FILES;
1062parse_char_array:
1063 if (*activep && *uintptr == 0) {
1064 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1065 if ((*uintptr) >= max_entries)
1066 fatal("%s line %d: "
1067 "too many authorized keys files.",
1068 filename, linenum);
1069 cpptr[(*uintptr)++] = xstrdup(arg);
1070 }
1071 }
1072 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001073
1074 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001075 cpptr = (char **)&options->user_hostfiles;
1076 uintptr = &options->num_user_hostfiles;
1077 max_entries = SSH_MAX_HOSTS_FILES;
1078 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001079
Damien Miller95def091999-11-25 00:26:21 +11001080 case oHostName:
1081 charptr = &options->hostname;
1082 goto parse_string;
1083
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001084 case oHostKeyAlias:
1085 charptr = &options->host_key_alias;
1086 goto parse_string;
1087
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001088 case oPreferredAuthentications:
1089 charptr = &options->preferred_authentications;
1090 goto parse_string;
1091
Ben Lindstrome0f88042001-04-30 13:06:24 +00001092 case oBindAddress:
1093 charptr = &options->bind_address;
1094 goto parse_string;
1095
Damien Miller7ea845e2010-02-12 09:21:02 +11001096 case oPKCS11Provider:
1097 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001098 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001099
Damien Miller95def091999-11-25 00:26:21 +11001100 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001101 charptr = &options->proxy_command;
1102parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001103 if (s == NULL)
1104 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001105 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001106 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001107 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001108 return 0;
1109
1110 case oPort:
1111 intptr = &options->port;
1112parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001113 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001114 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001115 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001116 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001117 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001118
1119 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001120 value = strtol(arg, &endofnumber, 0);
1121 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001122 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001123 if (*activep && *intptr == -1)
1124 *intptr = value;
1125 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126
Damien Miller95def091999-11-25 00:26:21 +11001127 case oConnectionAttempts:
1128 intptr = &options->connection_attempts;
1129 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001130
Damien Miller95def091999-11-25 00:26:21 +11001131 case oCipher:
1132 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001133 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001134 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001135 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001136 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001137 if (value == -1)
1138 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001139 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001140 if (*activep && *intptr == -1)
1141 *intptr = value;
1142 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143
Damien Miller78928792000-04-12 20:17:38 +10001144 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001145 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001146 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001147 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001148 if (!ciphers_valid(*arg == '+' ? arg + 1 : arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001149 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001150 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001151 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001152 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001153 break;
1154
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001155 case oMacs:
1156 arg = strdelim(&s);
1157 if (!arg || *arg == '\0')
1158 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001159 if (!mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001160 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001161 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001162 if (*activep && options->macs == NULL)
1163 options->macs = xstrdup(arg);
1164 break;
1165
Damien Millerd5f62bf2010-09-24 22:11:14 +10001166 case oKexAlgorithms:
1167 arg = strdelim(&s);
1168 if (!arg || *arg == '\0')
1169 fatal("%.200s line %d: Missing argument.",
1170 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001171 if (!kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001172 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1173 filename, linenum, arg ? arg : "<NONE>");
1174 if (*activep && options->kex_algorithms == NULL)
1175 options->kex_algorithms = xstrdup(arg);
1176 break;
1177
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001178 case oHostKeyAlgorithms:
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001179 charptr = &options->hostkeyalgorithms;
1180parse_keytypes:
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001181 arg = strdelim(&s);
1182 if (!arg || *arg == '\0')
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001183 fatal("%.200s line %d: Missing argument.",
1184 filename, linenum);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001185 if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001186 fatal("%s line %d: Bad key types '%s'.",
1187 filename, linenum, arg ? arg : "<NONE>");
1188 if (*activep && *charptr == NULL)
1189 *charptr = xstrdup(arg);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001190 break;
1191
Damien Miller78928792000-04-12 20:17:38 +10001192 case oProtocol:
1193 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001194 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001195 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001196 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001197 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001198 if (value == SSH_PROTO_UNKNOWN)
1199 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001200 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001201 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1202 *intptr = value;
1203 break;
1204
Damien Miller95def091999-11-25 00:26:21 +11001205 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001206 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001207 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001208 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001209 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001210 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001211 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001212 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1213 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001214 break;
1215
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001216 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001217 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001218 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001219 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001220 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001221 fatal("%.200s line %d: Missing port argument.",
1222 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001223
Damien Millera699d952008-11-03 19:27:34 +11001224 if (opcode == oLocalForward ||
1225 opcode == oRemoteForward) {
1226 arg2 = strdelim(&s);
1227 if (arg2 == NULL || *arg2 == '\0')
1228 fatal("%.200s line %d: Missing target argument.",
1229 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001230
Damien Millera699d952008-11-03 19:27:34 +11001231 /* construct a string for parse_forward */
1232 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1233 } else if (opcode == oDynamicForward) {
1234 strlcpy(fwdarg, arg, sizeof(fwdarg));
1235 }
1236
1237 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001238 opcode == oDynamicForward ? 1 : 0,
1239 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001240 fatal("%.200s line %d: Bad forwarding specification.",
1241 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001242
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001243 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001244 if (opcode == oLocalForward ||
1245 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001246 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001247 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001248 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001249 }
Damien Miller95def091999-11-25 00:26:21 +11001250 break;
1251
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001252 case oClearAllForwardings:
1253 intptr = &options->clear_forwardings;
1254 goto parse_flag;
1255
Damien Miller95def091999-11-25 00:26:21 +11001256 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001257 if (cmdline)
1258 fatal("Host directive not supported as a command-line "
1259 "option");
Damien Miller95def091999-11-25 00:26:21 +11001260 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001261 arg2 = NULL;
1262 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1263 negated = *arg == '!';
1264 if (negated)
1265 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001266 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001267 if (negated) {
1268 debug("%.200s line %d: Skipping Host "
1269 "block because of negated match "
1270 "for %.100s", filename, linenum,
1271 arg);
1272 *activep = 0;
1273 break;
1274 }
1275 if (!*activep)
1276 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001277 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001278 }
Damien Millerfe924212011-05-15 08:44:45 +10001279 }
1280 if (*activep)
1281 debug("%.200s line %d: Applying options for %.100s",
1282 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001283 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001284 return 0;
1285
Damien Miller194fd902013-10-15 12:13:05 +11001286 case oMatch:
1287 if (cmdline)
1288 fatal("Host directive not supported as a command-line "
1289 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001290 value = match_cfg_line(options, &s, pw, host, original_host,
1291 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001292 if (value < 0)
1293 fatal("%.200s line %d: Bad Match condition", filename,
1294 linenum);
1295 *activep = value;
1296 break;
1297
Damien Miller95def091999-11-25 00:26:21 +11001298 case oEscapeChar:
1299 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001300 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001301 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001302 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001303 if (strcmp(arg, "none") == 0)
1304 value = SSH_ESCAPECHAR_NONE;
1305 else if (arg[1] == '\0')
1306 value = (u_char) arg[0];
1307 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001308 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1309 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001310 else {
1311 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001312 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001313 /* NOTREACHED */
1314 value = 0; /* Avoid compiler warning. */
1315 }
1316 if (*activep && *intptr == -1)
1317 *intptr = value;
1318 break;
1319
Damien Miller20a8f972003-05-18 20:50:30 +10001320 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001321 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001322 multistate_ptr = multistate_addressfamily;
1323 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001324
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001325 case oEnableSSHKeysign:
1326 intptr = &options->enable_ssh_keysign;
1327 goto parse_flag;
1328
Damien Millerbd394c32004-03-08 23:12:36 +11001329 case oIdentitiesOnly:
1330 intptr = &options->identities_only;
1331 goto parse_flag;
1332
Damien Miller509b0102003-12-17 16:33:10 +11001333 case oServerAliveInterval:
1334 intptr = &options->server_alive_interval;
1335 goto parse_time;
1336
1337 case oServerAliveCountMax:
1338 intptr = &options->server_alive_count_max;
1339 goto parse_int;
1340
Darren Tucker46bc0752004-05-02 22:11:30 +10001341 case oSendEnv:
1342 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1343 if (strchr(arg, '=') != NULL)
1344 fatal("%s line %d: Invalid environment name.",
1345 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001346 if (!*activep)
1347 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001348 if (options->num_send_env >= MAX_SEND_ENV)
1349 fatal("%s line %d: too many send env.",
1350 filename, linenum);
1351 options->send_env[options->num_send_env++] =
1352 xstrdup(arg);
1353 }
1354 break;
1355
Damien Miller0e220db2004-06-15 10:34:08 +10001356 case oControlPath:
1357 charptr = &options->control_path;
1358 goto parse_string;
1359
1360 case oControlMaster:
1361 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001362 multistate_ptr = multistate_controlmaster;
1363 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001364
Damien Millere11e1ea2010-08-03 16:04:46 +10001365 case oControlPersist:
1366 /* no/false/yes/true, or a time spec */
1367 intptr = &options->control_persist;
1368 arg = strdelim(&s);
1369 if (!arg || *arg == '\0')
1370 fatal("%.200s line %d: Missing ControlPersist"
1371 " argument.", filename, linenum);
1372 value = 0;
1373 value2 = 0; /* timeout */
1374 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1375 value = 0;
1376 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1377 value = 1;
1378 else if ((value2 = convtime(arg)) >= 0)
1379 value = 1;
1380 else
1381 fatal("%.200s line %d: Bad ControlPersist argument.",
1382 filename, linenum);
1383 if (*activep && *intptr == -1) {
1384 *intptr = value;
1385 options->control_persist_timeout = value2;
1386 }
1387 break;
1388
Damien Millere1776152005-03-01 21:47:37 +11001389 case oHashKnownHosts:
1390 intptr = &options->hash_known_hosts;
1391 goto parse_flag;
1392
Damien Millerd27b9472005-12-13 19:29:02 +11001393 case oTunnel:
1394 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001395 multistate_ptr = multistate_tunnel;
1396 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001397
1398 case oTunnelDevice:
1399 arg = strdelim(&s);
1400 if (!arg || *arg == '\0')
1401 fatal("%.200s line %d: Missing argument.", filename, linenum);
1402 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001403 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001404 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1405 if (*activep) {
1406 options->tun_local = value;
1407 options->tun_remote = value2;
1408 }
1409 break;
1410
1411 case oLocalCommand:
1412 charptr = &options->local_command;
1413 goto parse_command;
1414
1415 case oPermitLocalCommand:
1416 intptr = &options->permit_local_command;
1417 goto parse_flag;
1418
Damien Miller10288242008-06-30 00:04:03 +10001419 case oVisualHostKey:
1420 intptr = &options->visual_host_key;
1421 goto parse_flag;
1422
Damien Miller0dac6fb2010-11-20 15:19:38 +11001423 case oIPQoS:
1424 arg = strdelim(&s);
1425 if ((value = parse_ipqos(arg)) == -1)
1426 fatal("%s line %d: Bad IPQoS value: %s",
1427 filename, linenum, arg);
1428 arg = strdelim(&s);
1429 if (arg == NULL)
1430 value2 = value;
1431 else if ((value2 = parse_ipqos(arg)) == -1)
1432 fatal("%s line %d: Bad IPQoS value: %s",
1433 filename, linenum, arg);
1434 if (*activep) {
1435 options->ip_qos_interactive = value;
1436 options->ip_qos_bulk = value2;
1437 }
1438 break;
1439
Damien Miller21771e22011-05-15 08:45:50 +10001440 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001441 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001442 multistate_ptr = multistate_requesttty;
1443 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001444
Darren Tucker07636982013-05-16 20:30:03 +10001445 case oIgnoreUnknown:
1446 charptr = &options->ignored_unknown;
1447 goto parse_string;
1448
Damien Miller1262b662013-08-21 02:44:24 +10001449 case oProxyUseFdpass:
1450 intptr = &options->proxy_use_fdpass;
1451 goto parse_flag;
1452
Damien Miller0faf7472013-10-17 11:47:23 +11001453 case oCanonicalDomains:
1454 value = options->num_canonical_domains != 0;
1455 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1456 valid_domain(arg, filename, linenum);
1457 if (!*activep || value)
1458 continue;
1459 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1460 fatal("%s line %d: too many hostname suffixes.",
1461 filename, linenum);
1462 options->canonical_domains[
1463 options->num_canonical_domains++] = xstrdup(arg);
1464 }
1465 break;
1466
Damien Miller38505592013-10-17 11:48:13 +11001467 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001468 value = options->num_permitted_cnames != 0;
1469 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1470 /* Either '*' for everything or 'list:list' */
1471 if (strcmp(arg, "*") == 0)
1472 arg2 = arg;
1473 else {
1474 lowercase(arg);
1475 if ((arg2 = strchr(arg, ':')) == NULL ||
1476 arg2[1] == '\0') {
1477 fatal("%s line %d: "
1478 "Invalid permitted CNAME \"%s\"",
1479 filename, linenum, arg);
1480 }
1481 *arg2 = '\0';
1482 arg2++;
1483 }
1484 if (!*activep || value)
1485 continue;
1486 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1487 fatal("%s line %d: too many permitted CNAMEs.",
1488 filename, linenum);
1489 cname = options->permitted_cnames +
1490 options->num_permitted_cnames++;
1491 cname->source_list = xstrdup(arg);
1492 cname->target_list = xstrdup(arg2);
1493 }
1494 break;
1495
Damien Miller38505592013-10-17 11:48:13 +11001496 case oCanonicalizeHostname:
1497 intptr = &options->canonicalize_hostname;
1498 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001499 goto parse_multistate;
1500
Damien Miller38505592013-10-17 11:48:13 +11001501 case oCanonicalizeMaxDots:
1502 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001503 goto parse_int;
1504
Damien Miller38505592013-10-17 11:48:13 +11001505 case oCanonicalizeFallbackLocal:
1506 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001507 goto parse_flag;
1508
Damien Miller7acefbb2014-07-18 14:11:24 +10001509 case oStreamLocalBindMask:
1510 arg = strdelim(&s);
1511 if (!arg || *arg == '\0')
1512 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1513 /* Parse mode in octal format */
1514 value = strtol(arg, &endofnumber, 8);
1515 if (arg == endofnumber || value < 0 || value > 0777)
1516 fatal("%.200s line %d: Bad mask.", filename, linenum);
1517 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1518 break;
1519
1520 case oStreamLocalBindUnlink:
1521 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1522 goto parse_flag;
1523
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001524 case oRevokedHostKeys:
1525 charptr = &options->revoked_host_keys;
1526 goto parse_string;
1527
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001528 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001529 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001530 arg = strdelim(&s);
1531 if (!arg || *arg == '\0')
1532 fatal("%.200s line %d: Missing argument.",
1533 filename, linenum);
1534 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1535 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1536 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001537 if (*activep && *intptr == -1)
1538 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001539 break;
1540
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001541 case oUpdateHostkeys:
1542 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001543 multistate_ptr = multistate_yesnoask;
1544 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001545
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001546 case oHostbasedKeyTypes:
1547 charptr = &options->hostbased_key_types;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001548 goto parse_keytypes;
1549
1550 case oPubkeyAcceptedKeyTypes:
1551 charptr = &options->pubkey_key_types;
1552 goto parse_keytypes;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001553
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001554 case oAddKeysToAgent:
1555 intptr = &options->add_keys_to_agent;
1556 multistate_ptr = multistate_yesnoaskconfirm;
1557 goto parse_multistate;
1558
Ben Lindstrom4daea862002-06-09 20:04:02 +00001559 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001560 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001561 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001562 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001563
Damien Millerf9b3feb2003-05-16 11:38:32 +10001564 case oUnsupported:
1565 error("%s line %d: Unsupported option \"%s\"",
1566 filename, linenum, keyword);
1567 return 0;
1568
Damien Miller95def091999-11-25 00:26:21 +11001569 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001570 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001571 }
1572
1573 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001574 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001575 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001576 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001577 }
Damien Miller95def091999-11-25 00:26:21 +11001578 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001579}
1580
1581
Damien Miller5428f641999-11-25 11:54:57 +11001582/*
1583 * Reads the config file and modifies the options accordingly. Options
1584 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001585 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001586 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001587
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001588int
Damien Miller194fd902013-10-15 12:13:05 +11001589read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001590 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591{
Damien Miller95def091999-11-25 00:26:21 +11001592 FILE *f;
1593 char line[1024];
1594 int active, linenum;
1595 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001596
Damien Miller57a44762004-04-20 20:11:57 +10001597 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001598 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001599
Darren Tuckeraefa3682013-04-05 11:18:35 +11001600 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001601 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001602
Damien Miller33793852004-06-15 10:27:55 +10001603 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001604 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001605 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001606 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001607 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001608 }
1609
Damien Miller95def091999-11-25 00:26:21 +11001610 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001611
Damien Miller5428f641999-11-25 11:54:57 +11001612 /*
1613 * Mark that we are now processing the options. This flag is turned
1614 * on/off by Host specifications.
1615 */
Damien Miller95def091999-11-25 00:26:21 +11001616 active = 1;
1617 linenum = 0;
1618 while (fgets(line, sizeof(line), f)) {
1619 /* Update line number counter. */
1620 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001621 if (process_config_line(options, pw, host, original_host,
1622 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001623 bad_options++;
1624 }
1625 fclose(f);
1626 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001627 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001628 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001629 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001630}
1631
Damien Miller13f97b22014-02-24 15:57:55 +11001632/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1633int
1634option_clear_or_none(const char *o)
1635{
1636 return o == NULL || strcasecmp(o, "none") == 0;
1637}
1638
Damien Miller5428f641999-11-25 11:54:57 +11001639/*
1640 * Initializes options to special values that indicate that they have not yet
1641 * been set. Read_config_file will only set options with this value. Options
1642 * are processed in the following order: command line, user config file,
1643 * system config file. Last, fill_default_options is called.
1644 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001645
Damien Miller4af51302000-04-16 11:18:38 +10001646void
Damien Miller95def091999-11-25 00:26:21 +11001647initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001648{
Damien Miller95def091999-11-25 00:26:21 +11001649 memset(options, 'X', sizeof(*options));
1650 options->forward_agent = -1;
1651 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001652 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001653 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001654 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001655 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001656 options->fwd_opts.gateway_ports = -1;
1657 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1658 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001659 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001660 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001661 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001662 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001663 options->gss_authentication = -1;
1664 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001665 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001666 options->kbd_interactive_authentication = -1;
1667 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001668 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001669 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001670 options->batch_mode = -1;
1671 options->check_host_ip = -1;
1672 options->strict_host_key_checking = -1;
1673 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001674 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001675 options->compression_level = -1;
1676 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001677 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001678 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001679 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001680 options->number_of_password_prompts = -1;
1681 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001682 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001683 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001684 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001685 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001686 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001687 options->num_identity_files = 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001688 options->num_certificate_files = 0;
Damien Miller95def091999-11-25 00:26:21 +11001689 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001690 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001691 options->proxy_command = NULL;
1692 options->user = NULL;
1693 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001694 options->num_system_hostfiles = 0;
1695 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001696 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001697 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001698 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001699 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001700 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001701 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001702 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001703 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001704 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001705 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001706 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001707 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001708 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001709 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001710 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001711 options->server_alive_interval = -1;
1712 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001713 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001714 options->control_path = NULL;
1715 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001716 options->control_persist = -1;
1717 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001718 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001719 options->tun_open = -1;
1720 options->tun_local = -1;
1721 options->tun_remote = -1;
1722 options->local_command = NULL;
1723 options->permit_local_command = -1;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001724 options->add_keys_to_agent = -1;
Damien Miller10288242008-06-30 00:04:03 +10001725 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001726 options->ip_qos_interactive = -1;
1727 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001728 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001729 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001730 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001731 options->num_canonical_domains = 0;
1732 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001733 options->canonicalize_max_dots = -1;
1734 options->canonicalize_fallback_local = -1;
1735 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001736 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001737 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001738 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001739 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001740 options->pubkey_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001741}
1742
Damien Miller5428f641999-11-25 11:54:57 +11001743/*
Damien Miller13f97b22014-02-24 15:57:55 +11001744 * A petite version of fill_default_options() that just fills the options
1745 * needed for hostname canonicalization to proceed.
1746 */
1747void
1748fill_default_options_for_canonicalization(Options *options)
1749{
1750 if (options->canonicalize_max_dots == -1)
1751 options->canonicalize_max_dots = 1;
1752 if (options->canonicalize_fallback_local == -1)
1753 options->canonicalize_fallback_local = 1;
1754 if (options->canonicalize_hostname == -1)
1755 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1756}
1757
1758/*
Damien Miller5428f641999-11-25 11:54:57 +11001759 * Called after processing other sources of option data, this fills those
1760 * options for which no value has been specified with their default values.
1761 */
Damien Miller4af51302000-04-16 11:18:38 +10001762void
Damien Miller95def091999-11-25 00:26:21 +11001763fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001764{
Damien Miller95def091999-11-25 00:26:21 +11001765 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001766 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001767 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001768 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001769 if (options->forward_x11_trusted == -1)
1770 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001771 if (options->forward_x11_timeout == -1)
1772 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001773 if (options->exit_on_forward_failure == -1)
1774 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001775 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001776 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001777 if (options->fwd_opts.gateway_ports == -1)
1778 options->fwd_opts.gateway_ports = 0;
1779 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1780 options->fwd_opts.streamlocal_bind_mask = 0177;
1781 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1782 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001783 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001784 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001785 if (options->rsa_authentication == -1)
1786 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001787 if (options->pubkey_authentication == -1)
1788 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001789 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001790 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001791 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001792 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001793 if (options->gss_deleg_creds == -1)
1794 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001795 if (options->password_authentication == -1)
1796 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001797 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001798 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001799 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001800 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001801 if (options->hostbased_authentication == -1)
1802 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001803 if (options->batch_mode == -1)
1804 options->batch_mode = 0;
1805 if (options->check_host_ip == -1)
1806 options->check_host_ip = 1;
1807 if (options->strict_host_key_checking == -1)
1808 options->strict_host_key_checking = 2; /* 2 is default */
1809 if (options->compression == -1)
1810 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001811 if (options->tcp_keep_alive == -1)
1812 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001813 if (options->compression_level == -1)
1814 options->compression_level = 6;
1815 if (options->port == -1)
1816 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001817 if (options->address_family == -1)
1818 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001819 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001820 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001821 if (options->number_of_password_prompts == -1)
1822 options->number_of_password_prompts = 3;
1823 /* Selected in ssh_login(). */
1824 if (options->cipher == -1)
1825 options->cipher = SSH_CIPHER_NOT_SET;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001826 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001827 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001828 options->protocol = SSH_PROTO_2;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001829 if (options->add_keys_to_agent == -1)
1830 options->add_keys_to_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001831 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001832 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001833 add_identity_file(options, "~/",
1834 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001835 }
1836 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001837 add_identity_file(options, "~/",
1838 _PATH_SSH_CLIENT_ID_RSA, 0);
1839 add_identity_file(options, "~/",
1840 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001841#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001842 add_identity_file(options, "~/",
1843 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001844#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001845 add_identity_file(options, "~/",
1846 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001847 }
Damien Millereba71ba2000-04-29 23:57:08 +10001848 }
Damien Miller95def091999-11-25 00:26:21 +11001849 if (options->escape_char == -1)
1850 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001851 if (options->num_system_hostfiles == 0) {
1852 options->system_hostfiles[options->num_system_hostfiles++] =
1853 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1854 options->system_hostfiles[options->num_system_hostfiles++] =
1855 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1856 }
1857 if (options->num_user_hostfiles == 0) {
1858 options->user_hostfiles[options->num_user_hostfiles++] =
1859 xstrdup(_PATH_SSH_USER_HOSTFILE);
1860 options->user_hostfiles[options->num_user_hostfiles++] =
1861 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1862 }
Damien Millerfcd93202002-02-05 12:26:34 +11001863 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001864 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001865 if (options->clear_forwardings == 1)
1866 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001867 if (options->no_host_authentication_for_localhost == - 1)
1868 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001869 if (options->identities_only == -1)
1870 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001871 if (options->enable_ssh_keysign == -1)
1872 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001873 if (options->rekey_limit == -1)
1874 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001875 if (options->rekey_interval == -1)
1876 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001877 if (options->verify_host_key_dns == -1)
1878 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001879 if (options->server_alive_interval == -1)
1880 options->server_alive_interval = 0;
1881 if (options->server_alive_count_max == -1)
1882 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001883 if (options->control_master == -1)
1884 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001885 if (options->control_persist == -1) {
1886 options->control_persist = 0;
1887 options->control_persist_timeout = 0;
1888 }
Damien Millere1776152005-03-01 21:47:37 +11001889 if (options->hash_known_hosts == -1)
1890 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001891 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001892 options->tun_open = SSH_TUNMODE_NO;
1893 if (options->tun_local == -1)
1894 options->tun_local = SSH_TUNID_ANY;
1895 if (options->tun_remote == -1)
1896 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001897 if (options->permit_local_command == -1)
1898 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10001899 if (options->visual_host_key == -1)
1900 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001901 if (options->ip_qos_interactive == -1)
1902 options->ip_qos_interactive = IPTOS_LOWDELAY;
1903 if (options->ip_qos_bulk == -1)
1904 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001905 if (options->request_tty == -1)
1906 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001907 if (options->proxy_use_fdpass == -1)
1908 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001909 if (options->canonicalize_max_dots == -1)
1910 options->canonicalize_max_dots = 1;
1911 if (options->canonicalize_fallback_local == -1)
1912 options->canonicalize_fallback_local = 1;
1913 if (options->canonicalize_hostname == -1)
1914 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001915 if (options->fingerprint_hash == -1)
1916 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001917 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00001918 options->update_hostkeys = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001919 if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
1920 kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
1921 kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
1922 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1923 &options->hostbased_key_types) != 0 ||
1924 kex_assemble_names(KEX_DEFAULT_PK_ALG,
1925 &options->pubkey_key_types) != 0)
1926 fatal("%s: kex_assemble_names failed", __func__);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001927
Damien Millere9fc72e2013-10-15 12:14:12 +11001928#define CLEAR_ON_NONE(v) \
1929 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001930 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001931 free(v); \
1932 v = NULL; \
1933 } \
1934 } while(0)
1935 CLEAR_ON_NONE(options->local_command);
1936 CLEAR_ON_NONE(options->proxy_command);
1937 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001938 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001939 /* options->user will be set in the main program if appropriate */
1940 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001941 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001942 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001943}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001944
Damien Miller7acefbb2014-07-18 14:11:24 +10001945struct fwdarg {
1946 char *arg;
1947 int ispath;
1948};
1949
1950/*
1951 * parse_fwd_field
1952 * parses the next field in a port forwarding specification.
1953 * sets fwd to the parsed field and advances p past the colon
1954 * or sets it to NULL at end of string.
1955 * returns 0 on success, else non-zero.
1956 */
1957static int
1958parse_fwd_field(char **p, struct fwdarg *fwd)
1959{
1960 char *ep, *cp = *p;
1961 int ispath = 0;
1962
1963 if (*cp == '\0') {
1964 *p = NULL;
1965 return -1; /* end of string */
1966 }
1967
1968 /*
1969 * A field escaped with square brackets is used literally.
1970 * XXX - allow ']' to be escaped via backslash?
1971 */
1972 if (*cp == '[') {
1973 /* find matching ']' */
1974 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1975 if (*ep == '/')
1976 ispath = 1;
1977 }
1978 /* no matching ']' or not at end of field. */
1979 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1980 return -1;
1981 /* NUL terminate the field and advance p past the colon */
1982 *ep++ = '\0';
1983 if (*ep != '\0')
1984 *ep++ = '\0';
1985 fwd->arg = cp + 1;
1986 fwd->ispath = ispath;
1987 *p = ep;
1988 return 0;
1989 }
1990
1991 for (cp = *p; *cp != '\0'; cp++) {
1992 switch (*cp) {
1993 case '\\':
1994 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00001995 if (*cp == '\0')
1996 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10001997 break;
1998 case '/':
1999 ispath = 1;
2000 break;
2001 case ':':
2002 *cp++ = '\0';
2003 goto done;
2004 }
2005 }
2006done:
2007 fwd->arg = *p;
2008 fwd->ispath = ispath;
2009 *p = cp;
2010 return 0;
2011}
2012
Damien Millerf91ee4c2005-03-01 21:24:33 +11002013/*
2014 * parse_forward
2015 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11002016 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10002017 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2018 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11002019 * dynamicfwd == 1
2020 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11002021 * returns number of arguments parsed or zero on error
2022 */
2023int
Damien Miller7acefbb2014-07-18 14:11:24 +10002024parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002025{
Damien Miller7acefbb2014-07-18 14:11:24 +10002026 struct fwdarg fwdargs[4];
2027 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002028 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002029
Damien Miller7acefbb2014-07-18 14:11:24 +10002030 memset(fwd, 0, sizeof(*fwd));
2031 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002032
2033 cp = p = xstrdup(fwdspec);
2034
2035 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11002036 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002037 cp++;
2038
Damien Miller7acefbb2014-07-18 14:11:24 +10002039 for (i = 0; i < 4; ++i) {
2040 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11002041 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10002042 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002043
Damien Millerf4b39532008-11-03 19:28:21 +11002044 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10002045 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002046 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10002047 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002048
2049 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11002050 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10002051 if (fwdargs[0].ispath) {
2052 fwd->listen_path = xstrdup(fwdargs[0].arg);
2053 fwd->listen_port = PORT_STREAMLOCAL;
2054 } else {
2055 fwd->listen_host = NULL;
2056 fwd->listen_port = a2port(fwdargs[0].arg);
2057 }
Damien Millera699d952008-11-03 19:27:34 +11002058 fwd->connect_host = xstrdup("socks");
2059 break;
2060
2061 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10002062 if (fwdargs[0].ispath && fwdargs[1].ispath) {
2063 fwd->listen_path = xstrdup(fwdargs[0].arg);
2064 fwd->listen_port = PORT_STREAMLOCAL;
2065 fwd->connect_path = xstrdup(fwdargs[1].arg);
2066 fwd->connect_port = PORT_STREAMLOCAL;
2067 } else if (fwdargs[1].ispath) {
2068 fwd->listen_host = NULL;
2069 fwd->listen_port = a2port(fwdargs[0].arg);
2070 fwd->connect_path = xstrdup(fwdargs[1].arg);
2071 fwd->connect_port = PORT_STREAMLOCAL;
2072 } else {
2073 fwd->listen_host = xstrdup(fwdargs[0].arg);
2074 fwd->listen_port = a2port(fwdargs[1].arg);
2075 fwd->connect_host = xstrdup("socks");
2076 }
Damien Millera699d952008-11-03 19:27:34 +11002077 break;
2078
Damien Millerf91ee4c2005-03-01 21:24:33 +11002079 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002080 if (fwdargs[0].ispath) {
2081 fwd->listen_path = xstrdup(fwdargs[0].arg);
2082 fwd->listen_port = PORT_STREAMLOCAL;
2083 fwd->connect_host = xstrdup(fwdargs[1].arg);
2084 fwd->connect_port = a2port(fwdargs[2].arg);
2085 } else if (fwdargs[2].ispath) {
2086 fwd->listen_host = xstrdup(fwdargs[0].arg);
2087 fwd->listen_port = a2port(fwdargs[1].arg);
2088 fwd->connect_path = xstrdup(fwdargs[2].arg);
2089 fwd->connect_port = PORT_STREAMLOCAL;
2090 } else {
2091 fwd->listen_host = NULL;
2092 fwd->listen_port = a2port(fwdargs[0].arg);
2093 fwd->connect_host = xstrdup(fwdargs[1].arg);
2094 fwd->connect_port = a2port(fwdargs[2].arg);
2095 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002096 break;
2097
2098 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002099 fwd->listen_host = xstrdup(fwdargs[0].arg);
2100 fwd->listen_port = a2port(fwdargs[1].arg);
2101 fwd->connect_host = xstrdup(fwdargs[2].arg);
2102 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002103 break;
2104 default:
2105 i = 0; /* failure */
2106 }
2107
Darren Tuckera627d422013-06-02 07:31:17 +10002108 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002109
Damien Millera699d952008-11-03 19:27:34 +11002110 if (dynamicfwd) {
2111 if (!(i == 1 || i == 2))
2112 goto fail_free;
2113 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002114 if (!(i == 3 || i == 4)) {
2115 if (fwd->connect_path == NULL &&
2116 fwd->listen_path == NULL)
2117 goto fail_free;
2118 }
2119 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002120 goto fail_free;
2121 }
2122
Damien Miller7acefbb2014-07-18 14:11:24 +10002123 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2124 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002125 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002126 if (fwd->connect_host != NULL &&
2127 strlen(fwd->connect_host) >= NI_MAXHOST)
2128 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002129 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2130 if (fwd->connect_path != NULL &&
2131 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2132 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002133 if (fwd->listen_host != NULL &&
2134 strlen(fwd->listen_host) >= NI_MAXHOST)
2135 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002136 if (fwd->listen_path != NULL &&
2137 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2138 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002139
2140 return (i);
2141
2142 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002143 free(fwd->connect_host);
2144 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002145 free(fwd->connect_path);
2146 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002147 free(fwd->listen_host);
2148 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002149 free(fwd->listen_path);
2150 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002151 return (0);
2152}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002153
2154/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2155static const char *
2156fmt_multistate_int(int val, const struct multistate *m)
2157{
2158 u_int i;
2159
2160 for (i = 0; m[i].key != NULL; i++) {
2161 if (m[i].value == val)
2162 return m[i].key;
2163 }
2164 return "UNKNOWN";
2165}
2166
2167static const char *
2168fmt_intarg(OpCodes code, int val)
2169{
2170 if (val == -1)
2171 return "unset";
2172 switch (code) {
2173 case oAddressFamily:
2174 return fmt_multistate_int(val, multistate_addressfamily);
2175 case oVerifyHostKeyDNS:
2176 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002177 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002178 return fmt_multistate_int(val, multistate_yesnoask);
2179 case oControlMaster:
2180 return fmt_multistate_int(val, multistate_controlmaster);
2181 case oTunnel:
2182 return fmt_multistate_int(val, multistate_tunnel);
2183 case oRequestTTY:
2184 return fmt_multistate_int(val, multistate_requesttty);
2185 case oCanonicalizeHostname:
2186 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002187 case oFingerprintHash:
2188 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002189 case oProtocol:
2190 switch (val) {
2191 case SSH_PROTO_1:
2192 return "1";
2193 case SSH_PROTO_2:
2194 return "2";
2195 case (SSH_PROTO_1|SSH_PROTO_2):
2196 return "2,1";
2197 default:
2198 return "UNKNOWN";
2199 }
2200 default:
2201 switch (val) {
2202 case 0:
2203 return "no";
2204 case 1:
2205 return "yes";
2206 default:
2207 return "UNKNOWN";
2208 }
2209 }
2210}
2211
2212static const char *
2213lookup_opcode_name(OpCodes code)
2214{
2215 u_int i;
2216
2217 for (i = 0; keywords[i].name != NULL; i++)
2218 if (keywords[i].opcode == code)
2219 return(keywords[i].name);
2220 return "UNKNOWN";
2221}
2222
2223static void
2224dump_cfg_int(OpCodes code, int val)
2225{
2226 printf("%s %d\n", lookup_opcode_name(code), val);
2227}
2228
2229static void
2230dump_cfg_fmtint(OpCodes code, int val)
2231{
2232 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2233}
2234
2235static void
2236dump_cfg_string(OpCodes code, const char *val)
2237{
2238 if (val == NULL)
2239 return;
2240 printf("%s %s\n", lookup_opcode_name(code), val);
2241}
2242
2243static void
2244dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2245{
2246 u_int i;
2247
2248 for (i = 0; i < count; i++)
2249 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2250}
2251
2252static void
2253dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2254{
2255 u_int i;
2256
2257 printf("%s", lookup_opcode_name(code));
2258 for (i = 0; i < count; i++)
2259 printf(" %s", vals[i]);
2260 printf("\n");
2261}
2262
2263static void
2264dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2265{
2266 const struct Forward *fwd;
2267 u_int i;
2268
2269 /* oDynamicForward */
2270 for (i = 0; i < count; i++) {
2271 fwd = &fwds[i];
2272 if (code == oDynamicForward &&
2273 strcmp(fwd->connect_host, "socks") != 0)
2274 continue;
2275 if (code == oLocalForward &&
2276 strcmp(fwd->connect_host, "socks") == 0)
2277 continue;
2278 printf("%s", lookup_opcode_name(code));
2279 if (fwd->listen_port == PORT_STREAMLOCAL)
2280 printf(" %s", fwd->listen_path);
2281 else if (fwd->listen_host == NULL)
2282 printf(" %d", fwd->listen_port);
2283 else {
2284 printf(" [%s]:%d",
2285 fwd->listen_host, fwd->listen_port);
2286 }
2287 if (code != oDynamicForward) {
2288 if (fwd->connect_port == PORT_STREAMLOCAL)
2289 printf(" %s", fwd->connect_path);
2290 else if (fwd->connect_host == NULL)
2291 printf(" %d", fwd->connect_port);
2292 else {
2293 printf(" [%s]:%d",
2294 fwd->connect_host, fwd->connect_port);
2295 }
2296 }
2297 printf("\n");
2298 }
2299}
2300
2301void
2302dump_client_config(Options *o, const char *host)
2303{
2304 int i;
2305 char vbuf[5];
2306
djm@openbsd.org60a92472015-08-21 23:53:08 +00002307 /* This is normally prepared in ssh_kex2 */
2308 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
2309 fatal("%s: kex_assemble_names failed", __func__);
2310
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002311 /* Most interesting options first: user, host, port */
2312 dump_cfg_string(oUser, o->user);
2313 dump_cfg_string(oHostName, host);
2314 dump_cfg_int(oPort, o->port);
2315
2316 /* Flag options */
2317 dump_cfg_fmtint(oAddressFamily, o->address_family);
2318 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2319 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2320 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2321 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2322 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2323 dump_cfg_fmtint(oCompression, o->compression);
2324 dump_cfg_fmtint(oControlMaster, o->control_master);
2325 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2326 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002327 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002328 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2329 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2330 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2331 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2332#ifdef GSSAPI
2333 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2334 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2335#endif /* GSSAPI */
2336 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2337 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2338 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2339 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2340 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2341 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2342 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2343 dump_cfg_fmtint(oProtocol, o->protocol);
2344 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2345 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2346 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2347 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2348 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2349 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2350 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2351 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2352 dump_cfg_fmtint(oTunnel, o->tun_open);
2353 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2354 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2355 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002356 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002357
2358 /* Integer options */
2359 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2360 dump_cfg_int(oCompressionLevel, o->compression_level);
2361 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2362 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2363 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2364 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2365 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2366
2367 /* String options */
2368 dump_cfg_string(oBindAddress, o->bind_address);
2369 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2370 dump_cfg_string(oControlPath, o->control_path);
djm@openbsd.org60a92472015-08-21 23:53:08 +00002371 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002372 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002373 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002374 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2375 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2376 dump_cfg_string(oLocalCommand, o->local_command);
2377 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2378 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2379 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2380 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2381 dump_cfg_string(oProxyCommand, o->proxy_command);
djm@openbsd.org0c46bbe2015-10-07 15:59:12 +00002382 dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002383 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002384 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002385
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002386 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002387 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2388 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2389 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2390
2391 /* String array options */
2392 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2393 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2394 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2395 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2396 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2397
2398 /* Special cases */
2399
2400 /* oConnectTimeout */
2401 if (o->connection_timeout == -1)
2402 printf("connecttimeout none\n");
2403 else
2404 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2405
2406 /* oTunnelDevice */
2407 printf("tunneldevice");
2408 if (o->tun_local == SSH_TUNID_ANY)
2409 printf(" any");
2410 else
2411 printf(" %d", o->tun_local);
2412 if (o->tun_remote == SSH_TUNID_ANY)
2413 printf(":any");
2414 else
2415 printf(":%d", o->tun_remote);
2416 printf("\n");
2417
2418 /* oCanonicalizePermittedCNAMEs */
2419 if ( o->num_permitted_cnames > 0) {
2420 printf("canonicalizePermittedcnames");
2421 for (i = 0; i < o->num_permitted_cnames; i++) {
2422 printf(" %s:%s", o->permitted_cnames[i].source_list,
2423 o->permitted_cnames[i].target_list);
2424 }
2425 printf("\n");
2426 }
2427
2428 /* oCipher */
2429 if (o->cipher != SSH_CIPHER_NOT_SET)
2430 printf("Cipher %s\n", cipher_name(o->cipher));
2431
2432 /* oControlPersist */
2433 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2434 dump_cfg_fmtint(oControlPersist, o->control_persist);
2435 else
2436 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2437
2438 /* oEscapeChar */
2439 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2440 printf("escapechar none\n");
2441 else {
2442 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2443 printf("escapechar %s\n", vbuf);
2444 }
2445
2446 /* oIPQoS */
2447 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2448 printf("%s\n", iptos2str(o->ip_qos_bulk));
2449
2450 /* oRekeyLimit */
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002451 printf("rekeylimit %llu %d\n",
2452 (unsigned long long)o->rekey_limit, o->rekey_interval);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002453
2454 /* oStreamLocalBindMask */
2455 printf("streamlocalbindmask 0%o\n",
2456 o->fwd_opts.streamlocal_bind_mask);
2457}