blob: db7d0bbbfabfb6adf731e9e623fec657bb7eb888 [file] [log] [blame]
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00001/* $OpenBSD: readconf.c,v 1.237 2015/06/26 05:13:20 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,
Damien Miller194fd902013-10-15 12:13:05 +1100138 oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
Damien Miller95def091999-11-25 00:26:21 +1100139 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
140 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100141 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000142 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000143 oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000144 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000145 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100146 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000147 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000148 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000149 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100150 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000151 oSendEnv, oControlPath, oControlMaster, oControlPersist,
152 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100153 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Miller7cc194f2014-02-04 11:12:56 +1100154 oVisualHostKey, oUseRoaming,
Damien Miller1262b662013-08-21 02:44:24 +1000155 oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
Damien Miller38505592013-10-17 11:48:13 +1100156 oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
157 oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000158 oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000159 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
Darren Tucker07636982013-05-16 20:30:03 +1000160 oIgnoredUnknownOption, oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161} OpCodes;
162
163/* Textual representations of the tokens. */
164
Damien Miller95def091999-11-25 00:26:21 +1100165static struct {
166 const char *name;
167 OpCodes opcode;
168} keywords[] = {
169 { "forwardagent", oForwardAgent },
170 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000171 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000172 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000173 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000174 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100175 { "gatewayports", oGatewayPorts },
176 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000177 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100178 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100179 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
180 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100181 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000183 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000184 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000185 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000186 { "challengeresponseauthentication", oChallengeResponseAuthentication },
187 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
188 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000189 { "kerberosauthentication", oUnsupported },
190 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000191 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000192#if defined(GSSAPI)
193 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000194 { "gssapidelegatecredentials", oGssDelegateCreds },
195#else
196 { "gssapiauthentication", oUnsupported },
197 { "gssapidelegatecredentials", oUnsupported },
198#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000199 { "fallbacktorsh", oDeprecated },
200 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100201 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100202 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100203 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100204 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000205 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100206 { "proxycommand", oProxyCommand },
207 { "port", oPort },
208 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000209 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000210 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000211 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100212 { "remoteforward", oRemoteForward },
213 { "localforward", oLocalForward },
214 { "user", oUser },
215 { "host", oHost },
Damien Miller194fd902013-10-15 12:13:05 +1100216 { "match", oMatch },
Damien Miller95def091999-11-25 00:26:21 +1100217 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100218 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller295ee632011-05-29 21:42:31 +1000219 { "globalknownhostsfile2", oDeprecated },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100220 { "userknownhostsfile", oUserKnownHostsFile },
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000221 { "userknownhostsfile2", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100222 { "connectionattempts", oConnectionAttempts },
223 { "batchmode", oBatchMode },
224 { "checkhostip", oCheckHostIP },
225 { "stricthostkeychecking", oStrictHostKeyChecking },
226 { "compression", oCompression },
227 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100228 { "tcpkeepalive", oTCPKeepAlive },
229 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100230 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100231 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000232 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000233 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000234 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000235 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100236#ifdef ENABLE_PKCS11
237 { "smartcarddevice", oPKCS11Provider },
238 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000239#else
240 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100241 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000242#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100243 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000244 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000245 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100246 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000247 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000248 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000249 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100250 { "serveraliveinterval", oServerAliveInterval },
251 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000252 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000253 { "controlpath", oControlPath },
254 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000255 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100256 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100257 { "tunnel", oTunnel },
258 { "tunneldevice", oTunnelDevice },
259 { "localcommand", oLocalCommand },
260 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000261 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000262 { "useroaming", oUseRoaming },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000263 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100264 { "ipqos", oIPQoS },
Damien Miller21771e22011-05-15 08:45:50 +1000265 { "requesttty", oRequestTTY },
Damien Miller1262b662013-08-21 02:44:24 +1000266 { "proxyusefdpass", oProxyUseFdpass },
Damien Miller0faf7472013-10-17 11:47:23 +1100267 { "canonicaldomains", oCanonicalDomains },
Damien Miller38505592013-10-17 11:48:13 +1100268 { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
269 { "canonicalizehostname", oCanonicalizeHostname },
270 { "canonicalizemaxdots", oCanonicalizeMaxDots },
271 { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
Damien Miller7acefbb2014-07-18 14:11:24 +1000272 { "streamlocalbindmask", oStreamLocalBindMask },
273 { "streamlocalbindunlink", oStreamLocalBindUnlink },
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000274 { "revokedhostkeys", oRevokedHostKeys },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000275 { "fingerprinthash", oFingerprintHash },
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000276 { "updatehostkeys", oUpdateHostkeys },
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000277 { "hostbasedkeytypes", oHostbasedKeyTypes },
Darren Tucker07636982013-05-16 20:30:03 +1000278 { "ignoreunknown", oIgnoreUnknown },
Damien Miller01ed2272008-11-05 16:20:46 +1100279
Ben Lindstrom65366a82001-12-06 16:32:47 +0000280 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100281};
282
Damien Miller5428f641999-11-25 11:54:57 +1100283/*
284 * Adds a local TCP/IP port forward to options. Never returns if there is an
285 * error.
286 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Damien Miller4af51302000-04-16 11:18:38 +1000288void
Damien Miller7acefbb2014-07-18 14:11:24 +1000289add_local_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290{
Damien Miller7acefbb2014-07-18 14:11:24 +1000291 struct Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000292#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100293 extern uid_t original_real_uid;
Damien Miller7acefbb2014-07-18 14:11:24 +1000294 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 &&
295 newfwd->listen_path == NULL)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000296 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100297#endif
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000298 options->local_forwards = xreallocarray(options->local_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000299 options->num_local_forwards + 1,
300 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100301 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100302
Damien Miller1a0442f2008-11-05 16:30:06 +1100303 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100304 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000305 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100306 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100307 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000308 fwd->connect_path = newfwd->connect_path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309}
310
Damien Miller5428f641999-11-25 11:54:57 +1100311/*
312 * Adds a remote TCP/IP port forward to options. Never returns if there is
313 * an error.
314 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315
Damien Miller4af51302000-04-16 11:18:38 +1000316void
Damien Miller7acefbb2014-07-18 14:11:24 +1000317add_remote_forward(Options *options, const struct Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318{
Damien Miller7acefbb2014-07-18 14:11:24 +1000319 struct Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000320
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000321 options->remote_forwards = xreallocarray(options->remote_forwards,
Damien Miller232cfb12010-06-26 09:50:30 +1000322 options->num_remote_forwards + 1,
323 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100324 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100325
Damien Miller1a0442f2008-11-05 16:30:06 +1100326 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100327 fwd->listen_port = newfwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000328 fwd->listen_path = newfwd->listen_path;
Damien Miller1a0442f2008-11-05 16:30:06 +1100329 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100330 fwd->connect_port = newfwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +1000331 fwd->connect_path = newfwd->connect_path;
Darren Tucker68afb8c2011-10-02 18:59:03 +1100332 fwd->handle = newfwd->handle;
Damien Miller388f6fc2010-05-21 14:57:35 +1000333 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334}
335
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000336static void
337clear_forwardings(Options *options)
338{
339 int i;
340
Damien Millerf91ee4c2005-03-01 21:24:33 +1100341 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000342 free(options->local_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000343 free(options->local_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000344 free(options->local_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000345 free(options->local_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100346 }
Damien Miller232cfb12010-06-26 09:50:30 +1000347 if (options->num_local_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000348 free(options->local_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000349 options->local_forwards = NULL;
350 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000351 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100352 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000353 free(options->remote_forwards[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000354 free(options->remote_forwards[i].listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000355 free(options->remote_forwards[i].connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000356 free(options->remote_forwards[i].connect_path);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100357 }
Damien Miller232cfb12010-06-26 09:50:30 +1000358 if (options->num_remote_forwards > 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000359 free(options->remote_forwards);
Damien Miller232cfb12010-06-26 09:50:30 +1000360 options->remote_forwards = NULL;
361 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000362 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100363 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000364}
365
Darren Tucker19104782013-04-05 11:13:08 +1100366void
367add_identity_file(Options *options, const char *dir, const char *filename,
368 int userprovided)
369{
370 char *path;
Damien Miller15271902014-05-15 13:47:56 +1000371 int i;
Darren Tucker19104782013-04-05 11:13:08 +1100372
373 if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
374 fatal("Too many identity files specified (max %d)",
375 SSH_MAX_IDENTITY_FILES);
376
377 if (dir == NULL) /* no dir, filename is absolute */
378 path = xstrdup(filename);
379 else
380 (void)xasprintf(&path, "%.100s%.100s", dir, filename);
381
Damien Miller15271902014-05-15 13:47:56 +1000382 /* Avoid registering duplicates */
383 for (i = 0; i < options->num_identity_files; i++) {
384 if (options->identity_file_userprovided[i] == userprovided &&
385 strcmp(options->identity_files[i], path) == 0) {
386 debug2("%s: ignoring duplicate key %s", __func__, path);
387 free(path);
388 return;
389 }
390 }
391
Darren Tucker19104782013-04-05 11:13:08 +1100392 options->identity_file_userprovided[options->num_identity_files] =
393 userprovided;
394 options->identity_files[options->num_identity_files++] = path;
395}
396
Damien Miller194fd902013-10-15 12:13:05 +1100397int
398default_ssh_port(void)
399{
400 static int port;
401 struct servent *sp;
402
403 if (port == 0) {
404 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
405 port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
406 }
407 return port;
408}
409
410/*
411 * Execute a command in a shell.
412 * Return its exit status or -1 on abnormal exit.
413 */
414static int
415execute_in_shell(const char *cmd)
416{
417 char *shell, *command_string;
418 pid_t pid;
419 int devnull, status;
420 extern uid_t original_real_uid;
421
422 if ((shell = getenv("SHELL")) == NULL)
423 shell = _PATH_BSHELL;
424
425 /*
426 * Use "exec" to avoid "sh -c" processes on some platforms
427 * (e.g. Solaris)
428 */
429 xasprintf(&command_string, "exec %s", cmd);
430
431 /* Need this to redirect subprocess stdin/out */
432 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
433 fatal("open(/dev/null): %s", strerror(errno));
434
435 debug("Executing command: '%.500s'", cmd);
436
437 /* Fork and execute the command. */
438 if ((pid = fork()) == 0) {
439 char *argv[4];
440
441 /* Child. Permanently give up superuser privileges. */
442 permanently_drop_suid(original_real_uid);
443
444 /* Redirect child stdin and stdout. Leave stderr */
445 if (dup2(devnull, STDIN_FILENO) == -1)
446 fatal("dup2: %s", strerror(errno));
447 if (dup2(devnull, STDOUT_FILENO) == -1)
448 fatal("dup2: %s", strerror(errno));
449 if (devnull > STDERR_FILENO)
450 close(devnull);
451 closefrom(STDERR_FILENO + 1);
452
453 argv[0] = shell;
454 argv[1] = "-c";
455 argv[2] = command_string;
456 argv[3] = NULL;
457
458 execv(argv[0], argv);
459 error("Unable to execute '%.100s': %s", cmd, strerror(errno));
460 /* Die with signal to make this error apparent to parent. */
461 signal(SIGTERM, SIG_DFL);
462 kill(getpid(), SIGTERM);
463 _exit(1);
464 }
465 /* Parent. */
466 if (pid < 0)
467 fatal("%s: fork: %.100s", __func__, strerror(errno));
468
469 close(devnull);
470 free(command_string);
471
472 while (waitpid(pid, &status, 0) == -1) {
473 if (errno != EINTR && errno != EAGAIN)
474 fatal("%s: waitpid: %s", __func__, strerror(errno));
475 }
476 if (!WIFEXITED(status)) {
477 error("command '%.100s' exited abnormally", cmd);
478 return -1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000479 }
Damien Miller194fd902013-10-15 12:13:05 +1100480 debug3("command returned status %d", WEXITSTATUS(status));
481 return WEXITSTATUS(status);
482}
483
484/*
485 * Parse and execute a Match directive.
486 */
487static int
488match_cfg_line(Options *options, char **condition, struct passwd *pw,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000489 const char *host_arg, const char *original_host, int post_canon,
490 const char *filename, int linenum)
Damien Miller194fd902013-10-15 12:13:05 +1100491{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000492 char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
Damien Miller084bcd22013-10-23 16:30:51 +1100493 const char *ruser;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000494 int r, port, this_result, result = 1, attributes = 0, negate;
Damien Miller194fd902013-10-15 12:13:05 +1100495 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
496
497 /*
498 * Configuration is likely to be incomplete at this point so we
499 * must be prepared to use default values.
500 */
501 port = options->port <= 0 ? default_ssh_port() : options->port;
502 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100503 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100504 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100505 host = percent_expand(options->hostname,
506 "h", host_arg, (char *)NULL);
507 } else
508 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100509
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000510 debug2("checking match for '%s' host %s originally %s",
511 cp, host, original_host);
512 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
513 criteria = NULL;
514 this_result = 1;
515 if ((negate = attrib[0] == '!'))
516 attrib++;
517 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100518 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000519 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100520 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000521 error("%.200s line %d: '%s' cannot be combined "
522 "with other Match attributes",
523 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100524 result = -1;
525 goto out;
526 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000527 if (result)
528 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100529 goto out;
530 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000531 attributes++;
532 if (strcasecmp(attrib, "canonical") == 0) {
533 r = !!post_canon; /* force bitmask member to boolean */
534 if (r == (negate ? 1 : 0))
535 this_result = result = 0;
536 debug3("%.200s line %d: %smatched '%s'",
537 filename, linenum,
538 this_result ? "" : "not ", oattrib);
539 continue;
540 }
541 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100542 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
543 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100544 result = -1;
545 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100546 }
Damien Miller194fd902013-10-15 12:13:05 +1100547 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000548 criteria = xstrdup(host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000549 r = match_hostname(host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000550 if (r == (negate ? 1 : 0))
551 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100552 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000553 criteria = xstrdup(original_host);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000554 r = match_hostname(original_host, arg) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000555 if (r == (negate ? 1 : 0))
556 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100557 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000558 criteria = xstrdup(ruser);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000559 r = match_pattern_list(ruser, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000560 if (r == (negate ? 1 : 0))
561 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100562 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000563 criteria = xstrdup(pw->pw_name);
djm@openbsd.orge661a862015-05-04 06:10:48 +0000564 r = match_pattern_list(pw->pw_name, arg, 0) == 1;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000565 if (r == (negate ? 1 : 0))
566 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100567 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100568 if (gethostname(thishost, sizeof(thishost)) == -1)
569 fatal("gethostname: %s", strerror(errno));
570 strlcpy(shorthost, thishost, sizeof(shorthost));
571 shorthost[strcspn(thishost, ".")] = '\0';
572 snprintf(portstr, sizeof(portstr), "%d", port);
573
574 cmd = percent_expand(arg,
575 "L", shorthost,
576 "d", pw->pw_dir,
577 "h", host,
578 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000579 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100580 "p", portstr,
581 "r", ruser,
582 "u", pw->pw_name,
583 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100584 if (result != 1) {
585 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000586 debug3("%.200s line %d: skipped exec "
587 "\"%.100s\"", filename, linenum, cmd);
588 free(cmd);
589 continue;
Damien Miller06287802014-02-24 15:56:45 +1100590 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000591 r = execute_in_shell(cmd);
592 if (r == -1) {
593 fatal("%.200s line %d: match exec "
594 "'%.100s' error", filename,
595 linenum, cmd);
596 }
597 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100598 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000599 /* Force exit status to boolean */
600 r = r == 0;
601 if (r == (negate ? 1 : 0))
602 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100603 } else {
604 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100605 result = -1;
606 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100607 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000608 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
609 filename, linenum, this_result ? "": "not ",
610 oattrib, criteria);
611 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100612 }
Damien Millercf31f382013-10-24 21:02:56 +1100613 if (attributes == 0) {
614 error("One or more attributes required for Match");
615 result = -1;
616 goto out;
617 }
Damien Miller084bcd22013-10-23 16:30:51 +1100618 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000619 if (result != -1)
620 debug2("match %sfound", result ? "" : "not ");
621 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100622 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100623 return result;
624}
625
Damien Miller0faf7472013-10-17 11:47:23 +1100626/* Check and prepare a domain name: removes trailing '.' and lowercases */
627static void
628valid_domain(char *name, const char *filename, int linenum)
629{
630 size_t i, l = strlen(name);
631 u_char c, last = '\0';
632
633 if (l == 0)
634 fatal("%s line %d: empty hostname suffix", filename, linenum);
635 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
636 fatal("%s line %d: hostname suffix \"%.100s\" "
637 "starts with invalid character", filename, linenum, name);
638 for (i = 0; i < l; i++) {
639 c = tolower((u_char)name[i]);
640 name[i] = (char)c;
641 if (last == '.' && c == '.')
642 fatal("%s line %d: hostname suffix \"%.100s\" contains "
643 "consecutive separators", filename, linenum, name);
644 if (c != '.' && c != '-' && !isalnum(c) &&
645 c != '_') /* technically invalid, but common */
646 fatal("%s line %d: hostname suffix \"%.100s\" contains "
647 "invalid characters", filename, linenum, name);
648 last = c;
649 }
650 if (name[l - 1] == '.')
651 name[l - 1] = '\0';
652}
653
Damien Miller5428f641999-11-25 11:54:57 +1100654/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000655 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100656 */
Damien Miller4af51302000-04-16 11:18:38 +1000657static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000658parse_token(const char *cp, const char *filename, int linenum,
659 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660{
Darren Tucker07636982013-05-16 20:30:03 +1000661 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller95def091999-11-25 00:26:21 +1100663 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000664 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100665 return keywords[i].opcode;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000666 if (ignored_unknown != NULL &&
667 match_pattern_list(cp, ignored_unknown, 1) == 1)
Darren Tucker07636982013-05-16 20:30:03 +1000668 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000669 error("%s: line %d: Bad configuration option: %s",
670 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100671 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672}
673
Damien Millere9fc72e2013-10-15 12:14:12 +1100674/* Multistate option parsing */
675struct multistate {
676 char *key;
677 int value;
678};
679static const struct multistate multistate_flag[] = {
680 { "true", 1 },
681 { "false", 0 },
682 { "yes", 1 },
683 { "no", 0 },
684 { NULL, -1 }
685};
686static const struct multistate multistate_yesnoask[] = {
687 { "true", 1 },
688 { "false", 0 },
689 { "yes", 1 },
690 { "no", 0 },
691 { "ask", 2 },
692 { NULL, -1 }
693};
694static const struct multistate multistate_addressfamily[] = {
695 { "inet", AF_INET },
696 { "inet6", AF_INET6 },
697 { "any", AF_UNSPEC },
698 { NULL, -1 }
699};
700static const struct multistate multistate_controlmaster[] = {
701 { "true", SSHCTL_MASTER_YES },
702 { "yes", SSHCTL_MASTER_YES },
703 { "false", SSHCTL_MASTER_NO },
704 { "no", SSHCTL_MASTER_NO },
705 { "auto", SSHCTL_MASTER_AUTO },
706 { "ask", SSHCTL_MASTER_ASK },
707 { "autoask", SSHCTL_MASTER_AUTO_ASK },
708 { NULL, -1 }
709};
710static const struct multistate multistate_tunnel[] = {
711 { "ethernet", SSH_TUNMODE_ETHERNET },
712 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
713 { "true", SSH_TUNMODE_DEFAULT },
714 { "yes", SSH_TUNMODE_DEFAULT },
715 { "false", SSH_TUNMODE_NO },
716 { "no", SSH_TUNMODE_NO },
717 { NULL, -1 }
718};
719static const struct multistate multistate_requesttty[] = {
720 { "true", REQUEST_TTY_YES },
721 { "yes", REQUEST_TTY_YES },
722 { "false", REQUEST_TTY_NO },
723 { "no", REQUEST_TTY_NO },
724 { "force", REQUEST_TTY_FORCE },
725 { "auto", REQUEST_TTY_AUTO },
726 { NULL, -1 }
727};
Damien Miller38505592013-10-17 11:48:13 +1100728static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100729 { "true", SSH_CANONICALISE_YES },
730 { "false", SSH_CANONICALISE_NO },
731 { "yes", SSH_CANONICALISE_YES },
732 { "no", SSH_CANONICALISE_NO },
733 { "always", SSH_CANONICALISE_ALWAYS },
734 { NULL, -1 }
735};
Damien Millere9fc72e2013-10-15 12:14:12 +1100736
Damien Miller5428f641999-11-25 11:54:57 +1100737/*
738 * Processes a single option line as used in the configuration files. This
739 * only sets those values that have not already been set.
740 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100741#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100742int
Damien Miller194fd902013-10-15 12:13:05 +1100743process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000744 const char *original_host, char *line, const char *filename,
745 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746{
Damien Miller295ee632011-05-29 21:42:31 +1000747 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
748 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000749 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100750 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100751 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000752 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100753 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000754 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100755 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100756 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757
Damien Miller194fd902013-10-15 12:13:05 +1100758 if (activep == NULL) { /* We are processing a command line directive */
759 cmdline = 1;
760 activep = &cmdline;
761 }
762
Damien Millerc652cac2003-05-14 13:40:54 +1000763 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000764 if ((len = strlen(line)) == 0)
765 return 0;
766 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000767 if (strchr(WHITESPACE, line[len]) == NULL)
768 break;
769 line[len] = '\0';
770 }
771
Damien Millerbe484b52000-07-15 14:14:16 +1000772 s = line;
773 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100774 if ((keyword = strdelim(&s)) == NULL)
775 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000776 /* Ignore leading whitespace. */
777 if (*keyword == '\0')
778 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000779 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100780 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000781 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100782 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783
Darren Tucker07636982013-05-16 20:30:03 +1000784 opcode = parse_token(keyword, filename, linenum,
785 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786
Damien Miller95def091999-11-25 00:26:21 +1100787 switch (opcode) {
788 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100789 /* don't panic, but count bad options */
790 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100791 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000792 case oIgnoredUnknownOption:
793 debug("%s line %d: Ignored unknown option \"%s\"",
794 filename, linenum, keyword);
795 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000796 case oConnectTimeout:
797 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100798parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000799 arg = strdelim(&s);
800 if (!arg || *arg == '\0')
801 fatal("%s line %d: missing time value.",
802 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000803 if (strcmp(arg, "none") == 0)
804 value = -1;
805 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000806 fatal("%s line %d: invalid time value.",
807 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100808 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000809 *intptr = value;
810 break;
811
Damien Miller95def091999-11-25 00:26:21 +1100812 case oForwardAgent:
813 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100814 parse_flag:
815 multistate_ptr = multistate_flag;
816 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000817 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000818 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100819 fatal("%s line %d: missing argument.",
820 filename, linenum);
821 value = -1;
822 for (i = 0; multistate_ptr[i].key != NULL; i++) {
823 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
824 value = multistate_ptr[i].value;
825 break;
826 }
827 }
828 if (value == -1)
829 fatal("%s line %d: unsupported option \"%s\".",
830 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100831 if (*activep && *intptr == -1)
832 *intptr = value;
833 break;
834
835 case oForwardX11:
836 intptr = &options->forward_x11;
837 goto parse_flag;
838
Darren Tucker0a118da2003-10-15 15:54:32 +1000839 case oForwardX11Trusted:
840 intptr = &options->forward_x11_trusted;
841 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000842
Damien Miller1ab6a512010-06-26 10:02:24 +1000843 case oForwardX11Timeout:
844 intptr = &options->forward_x11_timeout;
845 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000846
Damien Miller95def091999-11-25 00:26:21 +1100847 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000848 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100849 goto parse_flag;
850
Darren Tuckere7d4b192006-07-12 22:17:10 +1000851 case oExitOnForwardFailure:
852 intptr = &options->exit_on_forward_failure;
853 goto parse_flag;
854
Damien Miller95def091999-11-25 00:26:21 +1100855 case oUsePrivilegedPort:
856 intptr = &options->use_privileged_port;
857 goto parse_flag;
858
Damien Miller95def091999-11-25 00:26:21 +1100859 case oPasswordAuthentication:
860 intptr = &options->password_authentication;
861 goto parse_flag;
862
Damien Miller874d77b2000-10-14 16:23:11 +1100863 case oKbdInteractiveAuthentication:
864 intptr = &options->kbd_interactive_authentication;
865 goto parse_flag;
866
867 case oKbdInteractiveDevices:
868 charptr = &options->kbd_interactive_devices;
869 goto parse_string;
870
Damien Miller0bc1bd82000-11-13 22:57:25 +1100871 case oPubkeyAuthentication:
872 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000873 goto parse_flag;
874
Damien Miller95def091999-11-25 00:26:21 +1100875 case oRSAAuthentication:
876 intptr = &options->rsa_authentication;
877 goto parse_flag;
878
879 case oRhostsRSAAuthentication:
880 intptr = &options->rhosts_rsa_authentication;
881 goto parse_flag;
882
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000883 case oHostbasedAuthentication:
884 intptr = &options->hostbased_authentication;
885 goto parse_flag;
886
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000887 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000888 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100889 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000890
Darren Tucker0efd1552003-08-26 11:49:55 +1000891 case oGssAuthentication:
892 intptr = &options->gss_authentication;
893 goto parse_flag;
894
895 case oGssDelegateCreds:
896 intptr = &options->gss_deleg_creds;
897 goto parse_flag;
898
Damien Miller95def091999-11-25 00:26:21 +1100899 case oBatchMode:
900 intptr = &options->batch_mode;
901 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Damien Miller95def091999-11-25 00:26:21 +1100903 case oCheckHostIP:
904 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000905 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906
Damien Miller37876e92003-05-15 10:19:46 +1000907 case oVerifyHostKeyDNS:
908 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100909 multistate_ptr = multistate_yesnoask;
910 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000911
Damien Miller95def091999-11-25 00:26:21 +1100912 case oStrictHostKeyChecking:
913 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100914 multistate_ptr = multistate_yesnoask;
915 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916
Damien Miller95def091999-11-25 00:26:21 +1100917 case oCompression:
918 intptr = &options->compression;
919 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Damien Miller12c150e2003-12-17 16:31:10 +1100921 case oTCPKeepAlive:
922 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100923 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000925 case oNoHostAuthenticationForLocalhost:
926 intptr = &options->no_host_authentication_for_localhost;
927 goto parse_flag;
928
Damien Miller95def091999-11-25 00:26:21 +1100929 case oNumberOfPasswordPrompts:
930 intptr = &options->number_of_password_prompts;
931 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932
Damien Miller95def091999-11-25 00:26:21 +1100933 case oCompressionLevel:
934 intptr = &options->compression_level;
935 goto parse_int;
936
Damien Millera5539d22003-04-09 20:50:06 +1000937 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000938 arg = strdelim(&s);
939 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000940 fatal("%.200s line %d: Missing argument.", filename,
941 linenum);
942 if (strcmp(arg, "default") == 0) {
943 val64 = 0;
944 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000945 if (scan_scaled(arg, &val64) == -1)
946 fatal("%.200s line %d: Bad number '%s': %s",
947 filename, linenum, arg, strerror(errno));
948 /* check for too-large or too-small limits */
949 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000950 fatal("%.200s line %d: RekeyLimit too large",
951 filename, linenum);
952 if (val64 != 0 && val64 < 16)
953 fatal("%.200s line %d: RekeyLimit too small",
954 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000955 }
Damien Miller3dff1762008-02-10 22:25:52 +1100956 if (*activep && options->rekey_limit == -1)
957 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000958 if (s != NULL) { /* optional rekey interval present */
959 if (strcmp(s, "none") == 0) {
960 (void)strdelim(&s); /* discard */
961 break;
962 }
963 intptr = &options->rekey_interval;
964 goto parse_time;
965 }
Damien Millera5539d22003-04-09 20:50:06 +1000966 break;
967
Damien Miller95def091999-11-25 00:26:21 +1100968 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000969 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000970 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100971 fatal("%.200s line %d: Missing argument.", filename, linenum);
972 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100973 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000974 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100975 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100976 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000977 add_identity_file(options, NULL,
978 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +1100979 }
980 break;
981
Damien Millerd3a18572000-06-07 19:55:44 +1000982 case oXAuthLocation:
983 charptr=&options->xauth_location;
984 goto parse_string;
985
Damien Miller95def091999-11-25 00:26:21 +1100986 case oUser:
987 charptr = &options->user;
988parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000989 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000990 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000991 fatal("%.200s line %d: Missing argument.",
992 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100993 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000994 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100995 break;
996
997 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +1000998 cpptr = (char **)&options->system_hostfiles;
999 uintptr = &options->num_system_hostfiles;
1000 max_entries = SSH_MAX_HOSTS_FILES;
1001parse_char_array:
1002 if (*activep && *uintptr == 0) {
1003 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1004 if ((*uintptr) >= max_entries)
1005 fatal("%s line %d: "
1006 "too many authorized keys files.",
1007 filename, linenum);
1008 cpptr[(*uintptr)++] = xstrdup(arg);
1009 }
1010 }
1011 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001012
1013 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001014 cpptr = (char **)&options->user_hostfiles;
1015 uintptr = &options->num_user_hostfiles;
1016 max_entries = SSH_MAX_HOSTS_FILES;
1017 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001018
Damien Miller95def091999-11-25 00:26:21 +11001019 case oHostName:
1020 charptr = &options->hostname;
1021 goto parse_string;
1022
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001023 case oHostKeyAlias:
1024 charptr = &options->host_key_alias;
1025 goto parse_string;
1026
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001027 case oPreferredAuthentications:
1028 charptr = &options->preferred_authentications;
1029 goto parse_string;
1030
Ben Lindstrome0f88042001-04-30 13:06:24 +00001031 case oBindAddress:
1032 charptr = &options->bind_address;
1033 goto parse_string;
1034
Damien Miller7ea845e2010-02-12 09:21:02 +11001035 case oPKCS11Provider:
1036 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001037 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001038
Damien Miller95def091999-11-25 00:26:21 +11001039 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001040 charptr = &options->proxy_command;
1041parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001042 if (s == NULL)
1043 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001044 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001045 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001046 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001047 return 0;
1048
1049 case oPort:
1050 intptr = &options->port;
1051parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001052 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001053 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001054 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001055 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001056 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001057
1058 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001059 value = strtol(arg, &endofnumber, 0);
1060 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001061 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001062 if (*activep && *intptr == -1)
1063 *intptr = value;
1064 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Damien Miller95def091999-11-25 00:26:21 +11001066 case oConnectionAttempts:
1067 intptr = &options->connection_attempts;
1068 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001069
Damien Miller95def091999-11-25 00:26:21 +11001070 case oCipher:
1071 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001072 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001073 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001074 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001075 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001076 if (value == -1)
1077 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001078 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001079 if (*activep && *intptr == -1)
1080 *intptr = value;
1081 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082
Damien Miller78928792000-04-12 20:17:38 +10001083 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001084 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001085 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001086 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001087 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001088 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001089 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001090 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001091 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001092 break;
1093
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001094 case oMacs:
1095 arg = strdelim(&s);
1096 if (!arg || *arg == '\0')
1097 fatal("%.200s line %d: Missing argument.", filename, linenum);
1098 if (!mac_valid(arg))
1099 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001100 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001101 if (*activep && options->macs == NULL)
1102 options->macs = xstrdup(arg);
1103 break;
1104
Damien Millerd5f62bf2010-09-24 22:11:14 +10001105 case oKexAlgorithms:
1106 arg = strdelim(&s);
1107 if (!arg || *arg == '\0')
1108 fatal("%.200s line %d: Missing argument.",
1109 filename, linenum);
1110 if (!kex_names_valid(arg))
1111 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1112 filename, linenum, arg ? arg : "<NONE>");
1113 if (*activep && options->kex_algorithms == NULL)
1114 options->kex_algorithms = xstrdup(arg);
1115 break;
1116
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001117 case oHostKeyAlgorithms:
1118 arg = strdelim(&s);
1119 if (!arg || *arg == '\0')
1120 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001121 if (!sshkey_names_valid2(arg, 1))
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001122 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001123 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001124 if (*activep && options->hostkeyalgorithms == NULL)
1125 options->hostkeyalgorithms = xstrdup(arg);
1126 break;
1127
Damien Miller78928792000-04-12 20:17:38 +10001128 case oProtocol:
1129 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001130 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001131 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001132 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001133 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001134 if (value == SSH_PROTO_UNKNOWN)
1135 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001136 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001137 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1138 *intptr = value;
1139 break;
1140
Damien Miller95def091999-11-25 00:26:21 +11001141 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001142 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001143 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001144 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001145 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001146 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001147 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001148 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1149 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001150 break;
1151
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001152 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001153 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001154 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001155 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001156 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001157 fatal("%.200s line %d: Missing port argument.",
1158 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001159
Damien Millera699d952008-11-03 19:27:34 +11001160 if (opcode == oLocalForward ||
1161 opcode == oRemoteForward) {
1162 arg2 = strdelim(&s);
1163 if (arg2 == NULL || *arg2 == '\0')
1164 fatal("%.200s line %d: Missing target argument.",
1165 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001166
Damien Millera699d952008-11-03 19:27:34 +11001167 /* construct a string for parse_forward */
1168 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1169 } else if (opcode == oDynamicForward) {
1170 strlcpy(fwdarg, arg, sizeof(fwdarg));
1171 }
1172
1173 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001174 opcode == oDynamicForward ? 1 : 0,
1175 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001176 fatal("%.200s line %d: Bad forwarding specification.",
1177 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001178
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001179 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001180 if (opcode == oLocalForward ||
1181 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001182 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001183 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001184 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001185 }
Damien Miller95def091999-11-25 00:26:21 +11001186 break;
1187
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001188 case oClearAllForwardings:
1189 intptr = &options->clear_forwardings;
1190 goto parse_flag;
1191
Damien Miller95def091999-11-25 00:26:21 +11001192 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001193 if (cmdline)
1194 fatal("Host directive not supported as a command-line "
1195 "option");
Damien Miller95def091999-11-25 00:26:21 +11001196 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001197 arg2 = NULL;
1198 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1199 negated = *arg == '!';
1200 if (negated)
1201 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001202 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001203 if (negated) {
1204 debug("%.200s line %d: Skipping Host "
1205 "block because of negated match "
1206 "for %.100s", filename, linenum,
1207 arg);
1208 *activep = 0;
1209 break;
1210 }
1211 if (!*activep)
1212 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001213 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001214 }
Damien Millerfe924212011-05-15 08:44:45 +10001215 }
1216 if (*activep)
1217 debug("%.200s line %d: Applying options for %.100s",
1218 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001219 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001220 return 0;
1221
Damien Miller194fd902013-10-15 12:13:05 +11001222 case oMatch:
1223 if (cmdline)
1224 fatal("Host directive not supported as a command-line "
1225 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001226 value = match_cfg_line(options, &s, pw, host, original_host,
1227 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001228 if (value < 0)
1229 fatal("%.200s line %d: Bad Match condition", filename,
1230 linenum);
1231 *activep = value;
1232 break;
1233
Damien Miller95def091999-11-25 00:26:21 +11001234 case oEscapeChar:
1235 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001236 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001237 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001238 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org08823322015-05-22 04:45:52 +00001239 if (strcmp(arg, "none") == 0)
1240 value = SSH_ESCAPECHAR_NONE;
1241 else if (arg[1] == '\0')
1242 value = (u_char) arg[0];
1243 else if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001244 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1245 value = (u_char) arg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +11001246 else {
1247 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001248 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001249 /* NOTREACHED */
1250 value = 0; /* Avoid compiler warning. */
1251 }
1252 if (*activep && *intptr == -1)
1253 *intptr = value;
1254 break;
1255
Damien Miller20a8f972003-05-18 20:50:30 +10001256 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001257 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001258 multistate_ptr = multistate_addressfamily;
1259 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001260
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001261 case oEnableSSHKeysign:
1262 intptr = &options->enable_ssh_keysign;
1263 goto parse_flag;
1264
Damien Millerbd394c32004-03-08 23:12:36 +11001265 case oIdentitiesOnly:
1266 intptr = &options->identities_only;
1267 goto parse_flag;
1268
Damien Miller509b0102003-12-17 16:33:10 +11001269 case oServerAliveInterval:
1270 intptr = &options->server_alive_interval;
1271 goto parse_time;
1272
1273 case oServerAliveCountMax:
1274 intptr = &options->server_alive_count_max;
1275 goto parse_int;
1276
Darren Tucker46bc0752004-05-02 22:11:30 +10001277 case oSendEnv:
1278 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1279 if (strchr(arg, '=') != NULL)
1280 fatal("%s line %d: Invalid environment name.",
1281 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001282 if (!*activep)
1283 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001284 if (options->num_send_env >= MAX_SEND_ENV)
1285 fatal("%s line %d: too many send env.",
1286 filename, linenum);
1287 options->send_env[options->num_send_env++] =
1288 xstrdup(arg);
1289 }
1290 break;
1291
Damien Miller0e220db2004-06-15 10:34:08 +10001292 case oControlPath:
1293 charptr = &options->control_path;
1294 goto parse_string;
1295
1296 case oControlMaster:
1297 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001298 multistate_ptr = multistate_controlmaster;
1299 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001300
Damien Millere11e1ea2010-08-03 16:04:46 +10001301 case oControlPersist:
1302 /* no/false/yes/true, or a time spec */
1303 intptr = &options->control_persist;
1304 arg = strdelim(&s);
1305 if (!arg || *arg == '\0')
1306 fatal("%.200s line %d: Missing ControlPersist"
1307 " argument.", filename, linenum);
1308 value = 0;
1309 value2 = 0; /* timeout */
1310 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1311 value = 0;
1312 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1313 value = 1;
1314 else if ((value2 = convtime(arg)) >= 0)
1315 value = 1;
1316 else
1317 fatal("%.200s line %d: Bad ControlPersist argument.",
1318 filename, linenum);
1319 if (*activep && *intptr == -1) {
1320 *intptr = value;
1321 options->control_persist_timeout = value2;
1322 }
1323 break;
1324
Damien Millere1776152005-03-01 21:47:37 +11001325 case oHashKnownHosts:
1326 intptr = &options->hash_known_hosts;
1327 goto parse_flag;
1328
Damien Millerd27b9472005-12-13 19:29:02 +11001329 case oTunnel:
1330 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001331 multistate_ptr = multistate_tunnel;
1332 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001333
1334 case oTunnelDevice:
1335 arg = strdelim(&s);
1336 if (!arg || *arg == '\0')
1337 fatal("%.200s line %d: Missing argument.", filename, linenum);
1338 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001339 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001340 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1341 if (*activep) {
1342 options->tun_local = value;
1343 options->tun_remote = value2;
1344 }
1345 break;
1346
1347 case oLocalCommand:
1348 charptr = &options->local_command;
1349 goto parse_command;
1350
1351 case oPermitLocalCommand:
1352 intptr = &options->permit_local_command;
1353 goto parse_flag;
1354
Damien Miller10288242008-06-30 00:04:03 +10001355 case oVisualHostKey:
1356 intptr = &options->visual_host_key;
1357 goto parse_flag;
1358
Damien Miller0dac6fb2010-11-20 15:19:38 +11001359 case oIPQoS:
1360 arg = strdelim(&s);
1361 if ((value = parse_ipqos(arg)) == -1)
1362 fatal("%s line %d: Bad IPQoS value: %s",
1363 filename, linenum, arg);
1364 arg = strdelim(&s);
1365 if (arg == NULL)
1366 value2 = value;
1367 else if ((value2 = parse_ipqos(arg)) == -1)
1368 fatal("%s line %d: Bad IPQoS value: %s",
1369 filename, linenum, arg);
1370 if (*activep) {
1371 options->ip_qos_interactive = value;
1372 options->ip_qos_bulk = value2;
1373 }
1374 break;
1375
Darren Tucker71e4d542009-07-06 07:12:27 +10001376 case oUseRoaming:
1377 intptr = &options->use_roaming;
1378 goto parse_flag;
1379
Damien Miller21771e22011-05-15 08:45:50 +10001380 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001381 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001382 multistate_ptr = multistate_requesttty;
1383 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001384
Darren Tucker07636982013-05-16 20:30:03 +10001385 case oIgnoreUnknown:
1386 charptr = &options->ignored_unknown;
1387 goto parse_string;
1388
Damien Miller1262b662013-08-21 02:44:24 +10001389 case oProxyUseFdpass:
1390 intptr = &options->proxy_use_fdpass;
1391 goto parse_flag;
1392
Damien Miller0faf7472013-10-17 11:47:23 +11001393 case oCanonicalDomains:
1394 value = options->num_canonical_domains != 0;
1395 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1396 valid_domain(arg, filename, linenum);
1397 if (!*activep || value)
1398 continue;
1399 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1400 fatal("%s line %d: too many hostname suffixes.",
1401 filename, linenum);
1402 options->canonical_domains[
1403 options->num_canonical_domains++] = xstrdup(arg);
1404 }
1405 break;
1406
Damien Miller38505592013-10-17 11:48:13 +11001407 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001408 value = options->num_permitted_cnames != 0;
1409 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1410 /* Either '*' for everything or 'list:list' */
1411 if (strcmp(arg, "*") == 0)
1412 arg2 = arg;
1413 else {
1414 lowercase(arg);
1415 if ((arg2 = strchr(arg, ':')) == NULL ||
1416 arg2[1] == '\0') {
1417 fatal("%s line %d: "
1418 "Invalid permitted CNAME \"%s\"",
1419 filename, linenum, arg);
1420 }
1421 *arg2 = '\0';
1422 arg2++;
1423 }
1424 if (!*activep || value)
1425 continue;
1426 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1427 fatal("%s line %d: too many permitted CNAMEs.",
1428 filename, linenum);
1429 cname = options->permitted_cnames +
1430 options->num_permitted_cnames++;
1431 cname->source_list = xstrdup(arg);
1432 cname->target_list = xstrdup(arg2);
1433 }
1434 break;
1435
Damien Miller38505592013-10-17 11:48:13 +11001436 case oCanonicalizeHostname:
1437 intptr = &options->canonicalize_hostname;
1438 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001439 goto parse_multistate;
1440
Damien Miller38505592013-10-17 11:48:13 +11001441 case oCanonicalizeMaxDots:
1442 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001443 goto parse_int;
1444
Damien Miller38505592013-10-17 11:48:13 +11001445 case oCanonicalizeFallbackLocal:
1446 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001447 goto parse_flag;
1448
Damien Miller7acefbb2014-07-18 14:11:24 +10001449 case oStreamLocalBindMask:
1450 arg = strdelim(&s);
1451 if (!arg || *arg == '\0')
1452 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1453 /* Parse mode in octal format */
1454 value = strtol(arg, &endofnumber, 8);
1455 if (arg == endofnumber || value < 0 || value > 0777)
1456 fatal("%.200s line %d: Bad mask.", filename, linenum);
1457 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1458 break;
1459
1460 case oStreamLocalBindUnlink:
1461 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1462 goto parse_flag;
1463
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001464 case oRevokedHostKeys:
1465 charptr = &options->revoked_host_keys;
1466 goto parse_string;
1467
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001468 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001469 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001470 arg = strdelim(&s);
1471 if (!arg || *arg == '\0')
1472 fatal("%.200s line %d: Missing argument.",
1473 filename, linenum);
1474 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1475 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1476 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001477 if (*activep && *intptr == -1)
1478 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001479 break;
1480
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001481 case oUpdateHostkeys:
1482 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001483 multistate_ptr = multistate_yesnoask;
1484 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001485
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001486 case oHostbasedKeyTypes:
1487 charptr = &options->hostbased_key_types;
1488 arg = strdelim(&s);
1489 if (!arg || *arg == '\0')
1490 fatal("%.200s line %d: Missing argument.",
1491 filename, linenum);
1492 if (!sshkey_names_valid2(arg, 1))
1493 fatal("%s line %d: Bad key types '%s'.",
1494 filename, linenum, arg ? arg : "<NONE>");
1495 if (*activep && *charptr == NULL)
1496 *charptr = xstrdup(arg);
1497 break;
1498
Ben Lindstrom4daea862002-06-09 20:04:02 +00001499 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001500 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001501 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001502 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001503
Damien Millerf9b3feb2003-05-16 11:38:32 +10001504 case oUnsupported:
1505 error("%s line %d: Unsupported option \"%s\"",
1506 filename, linenum, keyword);
1507 return 0;
1508
Damien Miller95def091999-11-25 00:26:21 +11001509 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001510 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001511 }
1512
1513 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001514 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001515 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001516 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001517 }
Damien Miller95def091999-11-25 00:26:21 +11001518 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519}
1520
1521
Damien Miller5428f641999-11-25 11:54:57 +11001522/*
1523 * Reads the config file and modifies the options accordingly. Options
1524 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001525 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001526 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001528int
Damien Miller194fd902013-10-15 12:13:05 +11001529read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001530 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001531{
Damien Miller95def091999-11-25 00:26:21 +11001532 FILE *f;
1533 char line[1024];
1534 int active, linenum;
1535 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536
Damien Miller57a44762004-04-20 20:11:57 +10001537 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001538 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539
Darren Tuckeraefa3682013-04-05 11:18:35 +11001540 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001541 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001542
Damien Miller33793852004-06-15 10:27:55 +10001543 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001544 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001545 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001546 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001547 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001548 }
1549
Damien Miller95def091999-11-25 00:26:21 +11001550 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001551
Damien Miller5428f641999-11-25 11:54:57 +11001552 /*
1553 * Mark that we are now processing the options. This flag is turned
1554 * on/off by Host specifications.
1555 */
Damien Miller95def091999-11-25 00:26:21 +11001556 active = 1;
1557 linenum = 0;
1558 while (fgets(line, sizeof(line), f)) {
1559 /* Update line number counter. */
1560 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001561 if (process_config_line(options, pw, host, original_host,
1562 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001563 bad_options++;
1564 }
1565 fclose(f);
1566 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001567 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001568 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001569 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001570}
1571
Damien Miller13f97b22014-02-24 15:57:55 +11001572/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1573int
1574option_clear_or_none(const char *o)
1575{
1576 return o == NULL || strcasecmp(o, "none") == 0;
1577}
1578
Damien Miller5428f641999-11-25 11:54:57 +11001579/*
1580 * Initializes options to special values that indicate that they have not yet
1581 * been set. Read_config_file will only set options with this value. Options
1582 * are processed in the following order: command line, user config file,
1583 * system config file. Last, fill_default_options is called.
1584 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001585
Damien Miller4af51302000-04-16 11:18:38 +10001586void
Damien Miller95def091999-11-25 00:26:21 +11001587initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001588{
Damien Miller95def091999-11-25 00:26:21 +11001589 memset(options, 'X', sizeof(*options));
1590 options->forward_agent = -1;
1591 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001592 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001593 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001594 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001595 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001596 options->fwd_opts.gateway_ports = -1;
1597 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1598 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001599 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001600 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001601 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001602 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001603 options->gss_authentication = -1;
1604 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001605 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001606 options->kbd_interactive_authentication = -1;
1607 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001608 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001609 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001610 options->batch_mode = -1;
1611 options->check_host_ip = -1;
1612 options->strict_host_key_checking = -1;
1613 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001614 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001615 options->compression_level = -1;
1616 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001617 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001618 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001619 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001620 options->number_of_password_prompts = -1;
1621 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001622 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001623 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001624 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001625 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001626 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001627 options->num_identity_files = 0;
1628 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001629 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001630 options->proxy_command = NULL;
1631 options->user = NULL;
1632 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001633 options->num_system_hostfiles = 0;
1634 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001635 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001636 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001637 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001638 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001639 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001640 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001641 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001642 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001643 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001644 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001645 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001646 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001647 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001648 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001649 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001650 options->server_alive_interval = -1;
1651 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001652 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001653 options->control_path = NULL;
1654 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001655 options->control_persist = -1;
1656 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001657 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001658 options->tun_open = -1;
1659 options->tun_local = -1;
1660 options->tun_remote = -1;
1661 options->local_command = NULL;
1662 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001663 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001664 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001665 options->ip_qos_interactive = -1;
1666 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001667 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001668 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001669 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001670 options->num_canonical_domains = 0;
1671 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001672 options->canonicalize_max_dots = -1;
1673 options->canonicalize_fallback_local = -1;
1674 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001675 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001676 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001677 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001678 options->hostbased_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001679}
1680
Damien Miller5428f641999-11-25 11:54:57 +11001681/*
Damien Miller13f97b22014-02-24 15:57:55 +11001682 * A petite version of fill_default_options() that just fills the options
1683 * needed for hostname canonicalization to proceed.
1684 */
1685void
1686fill_default_options_for_canonicalization(Options *options)
1687{
1688 if (options->canonicalize_max_dots == -1)
1689 options->canonicalize_max_dots = 1;
1690 if (options->canonicalize_fallback_local == -1)
1691 options->canonicalize_fallback_local = 1;
1692 if (options->canonicalize_hostname == -1)
1693 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1694}
1695
1696/*
Damien Miller5428f641999-11-25 11:54:57 +11001697 * Called after processing other sources of option data, this fills those
1698 * options for which no value has been specified with their default values.
1699 */
Damien Miller4af51302000-04-16 11:18:38 +10001700void
Damien Miller95def091999-11-25 00:26:21 +11001701fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001702{
Damien Miller95def091999-11-25 00:26:21 +11001703 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001704 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001705 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001706 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001707 if (options->forward_x11_trusted == -1)
1708 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001709 if (options->forward_x11_timeout == -1)
1710 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001711 if (options->exit_on_forward_failure == -1)
1712 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001713 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001714 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001715 if (options->fwd_opts.gateway_ports == -1)
1716 options->fwd_opts.gateway_ports = 0;
1717 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1718 options->fwd_opts.streamlocal_bind_mask = 0177;
1719 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1720 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001721 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001722 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001723 if (options->rsa_authentication == -1)
1724 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001725 if (options->pubkey_authentication == -1)
1726 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001727 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001728 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001729 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001730 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001731 if (options->gss_deleg_creds == -1)
1732 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001733 if (options->password_authentication == -1)
1734 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001735 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001736 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001737 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001738 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001739 if (options->hostbased_authentication == -1)
1740 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001741 if (options->batch_mode == -1)
1742 options->batch_mode = 0;
1743 if (options->check_host_ip == -1)
1744 options->check_host_ip = 1;
1745 if (options->strict_host_key_checking == -1)
1746 options->strict_host_key_checking = 2; /* 2 is default */
1747 if (options->compression == -1)
1748 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001749 if (options->tcp_keep_alive == -1)
1750 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001751 if (options->compression_level == -1)
1752 options->compression_level = 6;
1753 if (options->port == -1)
1754 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001755 if (options->address_family == -1)
1756 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001757 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001758 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001759 if (options->number_of_password_prompts == -1)
1760 options->number_of_password_prompts = 3;
1761 /* Selected in ssh_login(). */
1762 if (options->cipher == -1)
1763 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001764 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001765 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001766 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001767 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001768 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001769 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001770 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001771 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001772 add_identity_file(options, "~/",
1773 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001774 }
1775 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001776 add_identity_file(options, "~/",
1777 _PATH_SSH_CLIENT_ID_RSA, 0);
1778 add_identity_file(options, "~/",
1779 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001780#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001781 add_identity_file(options, "~/",
1782 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001783#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001784 add_identity_file(options, "~/",
1785 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001786 }
Damien Millereba71ba2000-04-29 23:57:08 +10001787 }
Damien Miller95def091999-11-25 00:26:21 +11001788 if (options->escape_char == -1)
1789 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001790 if (options->num_system_hostfiles == 0) {
1791 options->system_hostfiles[options->num_system_hostfiles++] =
1792 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1793 options->system_hostfiles[options->num_system_hostfiles++] =
1794 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1795 }
1796 if (options->num_user_hostfiles == 0) {
1797 options->user_hostfiles[options->num_user_hostfiles++] =
1798 xstrdup(_PATH_SSH_USER_HOSTFILE);
1799 options->user_hostfiles[options->num_user_hostfiles++] =
1800 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1801 }
Damien Millerfcd93202002-02-05 12:26:34 +11001802 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001803 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001804 if (options->clear_forwardings == 1)
1805 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001806 if (options->no_host_authentication_for_localhost == - 1)
1807 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001808 if (options->identities_only == -1)
1809 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001810 if (options->enable_ssh_keysign == -1)
1811 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001812 if (options->rekey_limit == -1)
1813 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001814 if (options->rekey_interval == -1)
1815 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001816 if (options->verify_host_key_dns == -1)
1817 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001818 if (options->server_alive_interval == -1)
1819 options->server_alive_interval = 0;
1820 if (options->server_alive_count_max == -1)
1821 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001822 if (options->control_master == -1)
1823 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001824 if (options->control_persist == -1) {
1825 options->control_persist = 0;
1826 options->control_persist_timeout = 0;
1827 }
Damien Millere1776152005-03-01 21:47:37 +11001828 if (options->hash_known_hosts == -1)
1829 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001830 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001831 options->tun_open = SSH_TUNMODE_NO;
1832 if (options->tun_local == -1)
1833 options->tun_local = SSH_TUNID_ANY;
1834 if (options->tun_remote == -1)
1835 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001836 if (options->permit_local_command == -1)
1837 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001838 if (options->use_roaming == -1)
1839 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001840 if (options->visual_host_key == -1)
1841 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001842 if (options->ip_qos_interactive == -1)
1843 options->ip_qos_interactive = IPTOS_LOWDELAY;
1844 if (options->ip_qos_bulk == -1)
1845 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001846 if (options->request_tty == -1)
1847 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001848 if (options->proxy_use_fdpass == -1)
1849 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001850 if (options->canonicalize_max_dots == -1)
1851 options->canonicalize_max_dots = 1;
1852 if (options->canonicalize_fallback_local == -1)
1853 options->canonicalize_fallback_local = 1;
1854 if (options->canonicalize_hostname == -1)
1855 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001856 if (options->fingerprint_hash == -1)
1857 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001858 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00001859 options->update_hostkeys = 0;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001860 if (options->hostbased_key_types == NULL)
1861 options->hostbased_key_types = xstrdup("*");
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001862
Damien Millere9fc72e2013-10-15 12:14:12 +11001863#define CLEAR_ON_NONE(v) \
1864 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001865 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001866 free(v); \
1867 v = NULL; \
1868 } \
1869 } while(0)
1870 CLEAR_ON_NONE(options->local_command);
1871 CLEAR_ON_NONE(options->proxy_command);
1872 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001873 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001874 /* options->user will be set in the main program if appropriate */
1875 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001876 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001877 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001879
Damien Miller7acefbb2014-07-18 14:11:24 +10001880struct fwdarg {
1881 char *arg;
1882 int ispath;
1883};
1884
1885/*
1886 * parse_fwd_field
1887 * parses the next field in a port forwarding specification.
1888 * sets fwd to the parsed field and advances p past the colon
1889 * or sets it to NULL at end of string.
1890 * returns 0 on success, else non-zero.
1891 */
1892static int
1893parse_fwd_field(char **p, struct fwdarg *fwd)
1894{
1895 char *ep, *cp = *p;
1896 int ispath = 0;
1897
1898 if (*cp == '\0') {
1899 *p = NULL;
1900 return -1; /* end of string */
1901 }
1902
1903 /*
1904 * A field escaped with square brackets is used literally.
1905 * XXX - allow ']' to be escaped via backslash?
1906 */
1907 if (*cp == '[') {
1908 /* find matching ']' */
1909 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1910 if (*ep == '/')
1911 ispath = 1;
1912 }
1913 /* no matching ']' or not at end of field. */
1914 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1915 return -1;
1916 /* NUL terminate the field and advance p past the colon */
1917 *ep++ = '\0';
1918 if (*ep != '\0')
1919 *ep++ = '\0';
1920 fwd->arg = cp + 1;
1921 fwd->ispath = ispath;
1922 *p = ep;
1923 return 0;
1924 }
1925
1926 for (cp = *p; *cp != '\0'; cp++) {
1927 switch (*cp) {
1928 case '\\':
1929 memmove(cp, cp + 1, strlen(cp + 1) + 1);
djm@openbsd.org78c2a4f2015-06-26 05:13:20 +00001930 if (*cp == '\0')
1931 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10001932 break;
1933 case '/':
1934 ispath = 1;
1935 break;
1936 case ':':
1937 *cp++ = '\0';
1938 goto done;
1939 }
1940 }
1941done:
1942 fwd->arg = *p;
1943 fwd->ispath = ispath;
1944 *p = cp;
1945 return 0;
1946}
1947
Damien Millerf91ee4c2005-03-01 21:24:33 +11001948/*
1949 * parse_forward
1950 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001951 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10001952 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
1953 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11001954 * dynamicfwd == 1
1955 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001956 * returns number of arguments parsed or zero on error
1957 */
1958int
Damien Miller7acefbb2014-07-18 14:11:24 +10001959parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001960{
Damien Miller7acefbb2014-07-18 14:11:24 +10001961 struct fwdarg fwdargs[4];
1962 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001963 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001964
Damien Miller7acefbb2014-07-18 14:11:24 +10001965 memset(fwd, 0, sizeof(*fwd));
1966 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11001967
1968 cp = p = xstrdup(fwdspec);
1969
1970 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001971 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001972 cp++;
1973
Damien Miller7acefbb2014-07-18 14:11:24 +10001974 for (i = 0; i < 4; ++i) {
1975 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001976 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10001977 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001978
Damien Millerf4b39532008-11-03 19:28:21 +11001979 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10001980 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001981 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10001982 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001983
1984 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001985 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10001986 if (fwdargs[0].ispath) {
1987 fwd->listen_path = xstrdup(fwdargs[0].arg);
1988 fwd->listen_port = PORT_STREAMLOCAL;
1989 } else {
1990 fwd->listen_host = NULL;
1991 fwd->listen_port = a2port(fwdargs[0].arg);
1992 }
Damien Millera699d952008-11-03 19:27:34 +11001993 fwd->connect_host = xstrdup("socks");
1994 break;
1995
1996 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10001997 if (fwdargs[0].ispath && fwdargs[1].ispath) {
1998 fwd->listen_path = xstrdup(fwdargs[0].arg);
1999 fwd->listen_port = PORT_STREAMLOCAL;
2000 fwd->connect_path = xstrdup(fwdargs[1].arg);
2001 fwd->connect_port = PORT_STREAMLOCAL;
2002 } else if (fwdargs[1].ispath) {
2003 fwd->listen_host = NULL;
2004 fwd->listen_port = a2port(fwdargs[0].arg);
2005 fwd->connect_path = xstrdup(fwdargs[1].arg);
2006 fwd->connect_port = PORT_STREAMLOCAL;
2007 } else {
2008 fwd->listen_host = xstrdup(fwdargs[0].arg);
2009 fwd->listen_port = a2port(fwdargs[1].arg);
2010 fwd->connect_host = xstrdup("socks");
2011 }
Damien Millera699d952008-11-03 19:27:34 +11002012 break;
2013
Damien Millerf91ee4c2005-03-01 21:24:33 +11002014 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002015 if (fwdargs[0].ispath) {
2016 fwd->listen_path = xstrdup(fwdargs[0].arg);
2017 fwd->listen_port = PORT_STREAMLOCAL;
2018 fwd->connect_host = xstrdup(fwdargs[1].arg);
2019 fwd->connect_port = a2port(fwdargs[2].arg);
2020 } else if (fwdargs[2].ispath) {
2021 fwd->listen_host = xstrdup(fwdargs[0].arg);
2022 fwd->listen_port = a2port(fwdargs[1].arg);
2023 fwd->connect_path = xstrdup(fwdargs[2].arg);
2024 fwd->connect_port = PORT_STREAMLOCAL;
2025 } else {
2026 fwd->listen_host = NULL;
2027 fwd->listen_port = a2port(fwdargs[0].arg);
2028 fwd->connect_host = xstrdup(fwdargs[1].arg);
2029 fwd->connect_port = a2port(fwdargs[2].arg);
2030 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002031 break;
2032
2033 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002034 fwd->listen_host = xstrdup(fwdargs[0].arg);
2035 fwd->listen_port = a2port(fwdargs[1].arg);
2036 fwd->connect_host = xstrdup(fwdargs[2].arg);
2037 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002038 break;
2039 default:
2040 i = 0; /* failure */
2041 }
2042
Darren Tuckera627d422013-06-02 07:31:17 +10002043 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002044
Damien Millera699d952008-11-03 19:27:34 +11002045 if (dynamicfwd) {
2046 if (!(i == 1 || i == 2))
2047 goto fail_free;
2048 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002049 if (!(i == 3 || i == 4)) {
2050 if (fwd->connect_path == NULL &&
2051 fwd->listen_path == NULL)
2052 goto fail_free;
2053 }
2054 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002055 goto fail_free;
2056 }
2057
Damien Miller7acefbb2014-07-18 14:11:24 +10002058 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2059 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002060 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002061 if (fwd->connect_host != NULL &&
2062 strlen(fwd->connect_host) >= NI_MAXHOST)
2063 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002064 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2065 if (fwd->connect_path != NULL &&
2066 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2067 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002068 if (fwd->listen_host != NULL &&
2069 strlen(fwd->listen_host) >= NI_MAXHOST)
2070 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002071 if (fwd->listen_path != NULL &&
2072 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2073 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002074
2075 return (i);
2076
2077 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002078 free(fwd->connect_host);
2079 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002080 free(fwd->connect_path);
2081 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002082 free(fwd->listen_host);
2083 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002084 free(fwd->listen_path);
2085 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002086 return (0);
2087}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002088
2089/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2090static const char *
2091fmt_multistate_int(int val, const struct multistate *m)
2092{
2093 u_int i;
2094
2095 for (i = 0; m[i].key != NULL; i++) {
2096 if (m[i].value == val)
2097 return m[i].key;
2098 }
2099 return "UNKNOWN";
2100}
2101
2102static const char *
2103fmt_intarg(OpCodes code, int val)
2104{
2105 if (val == -1)
2106 return "unset";
2107 switch (code) {
2108 case oAddressFamily:
2109 return fmt_multistate_int(val, multistate_addressfamily);
2110 case oVerifyHostKeyDNS:
2111 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002112 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002113 return fmt_multistate_int(val, multistate_yesnoask);
2114 case oControlMaster:
2115 return fmt_multistate_int(val, multistate_controlmaster);
2116 case oTunnel:
2117 return fmt_multistate_int(val, multistate_tunnel);
2118 case oRequestTTY:
2119 return fmt_multistate_int(val, multistate_requesttty);
2120 case oCanonicalizeHostname:
2121 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002122 case oFingerprintHash:
2123 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002124 case oProtocol:
2125 switch (val) {
2126 case SSH_PROTO_1:
2127 return "1";
2128 case SSH_PROTO_2:
2129 return "2";
2130 case (SSH_PROTO_1|SSH_PROTO_2):
2131 return "2,1";
2132 default:
2133 return "UNKNOWN";
2134 }
2135 default:
2136 switch (val) {
2137 case 0:
2138 return "no";
2139 case 1:
2140 return "yes";
2141 default:
2142 return "UNKNOWN";
2143 }
2144 }
2145}
2146
2147static const char *
2148lookup_opcode_name(OpCodes code)
2149{
2150 u_int i;
2151
2152 for (i = 0; keywords[i].name != NULL; i++)
2153 if (keywords[i].opcode == code)
2154 return(keywords[i].name);
2155 return "UNKNOWN";
2156}
2157
2158static void
2159dump_cfg_int(OpCodes code, int val)
2160{
2161 printf("%s %d\n", lookup_opcode_name(code), val);
2162}
2163
2164static void
2165dump_cfg_fmtint(OpCodes code, int val)
2166{
2167 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2168}
2169
2170static void
2171dump_cfg_string(OpCodes code, const char *val)
2172{
2173 if (val == NULL)
2174 return;
2175 printf("%s %s\n", lookup_opcode_name(code), val);
2176}
2177
2178static void
2179dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2180{
2181 u_int i;
2182
2183 for (i = 0; i < count; i++)
2184 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2185}
2186
2187static void
2188dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2189{
2190 u_int i;
2191
2192 printf("%s", lookup_opcode_name(code));
2193 for (i = 0; i < count; i++)
2194 printf(" %s", vals[i]);
2195 printf("\n");
2196}
2197
2198static void
2199dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2200{
2201 const struct Forward *fwd;
2202 u_int i;
2203
2204 /* oDynamicForward */
2205 for (i = 0; i < count; i++) {
2206 fwd = &fwds[i];
2207 if (code == oDynamicForward &&
2208 strcmp(fwd->connect_host, "socks") != 0)
2209 continue;
2210 if (code == oLocalForward &&
2211 strcmp(fwd->connect_host, "socks") == 0)
2212 continue;
2213 printf("%s", lookup_opcode_name(code));
2214 if (fwd->listen_port == PORT_STREAMLOCAL)
2215 printf(" %s", fwd->listen_path);
2216 else if (fwd->listen_host == NULL)
2217 printf(" %d", fwd->listen_port);
2218 else {
2219 printf(" [%s]:%d",
2220 fwd->listen_host, fwd->listen_port);
2221 }
2222 if (code != oDynamicForward) {
2223 if (fwd->connect_port == PORT_STREAMLOCAL)
2224 printf(" %s", fwd->connect_path);
2225 else if (fwd->connect_host == NULL)
2226 printf(" %d", fwd->connect_port);
2227 else {
2228 printf(" [%s]:%d",
2229 fwd->connect_host, fwd->connect_port);
2230 }
2231 }
2232 printf("\n");
2233 }
2234}
2235
2236void
2237dump_client_config(Options *o, const char *host)
2238{
2239 int i;
2240 char vbuf[5];
2241
2242 /* Most interesting options first: user, host, port */
2243 dump_cfg_string(oUser, o->user);
2244 dump_cfg_string(oHostName, host);
2245 dump_cfg_int(oPort, o->port);
2246
2247 /* Flag options */
2248 dump_cfg_fmtint(oAddressFamily, o->address_family);
2249 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2250 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2251 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2252 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2253 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2254 dump_cfg_fmtint(oCompression, o->compression);
2255 dump_cfg_fmtint(oControlMaster, o->control_master);
2256 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2257 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002258 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002259 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2260 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2261 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2262 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2263#ifdef GSSAPI
2264 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2265 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2266#endif /* GSSAPI */
2267 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2268 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2269 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2270 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2271 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2272 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2273 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2274 dump_cfg_fmtint(oProtocol, o->protocol);
2275 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2276 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2277 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2278 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2279 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2280 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2281 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2282 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2283 dump_cfg_fmtint(oTunnel, o->tun_open);
2284 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2285 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2286 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002287 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002288
2289 /* Integer options */
2290 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2291 dump_cfg_int(oCompressionLevel, o->compression_level);
2292 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2293 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2294 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2295 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2296 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2297
2298 /* String options */
2299 dump_cfg_string(oBindAddress, o->bind_address);
2300 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2301 dump_cfg_string(oControlPath, o->control_path);
2302 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms ? o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
2303 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002304 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002305 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2306 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2307 dump_cfg_string(oLocalCommand, o->local_command);
2308 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2309 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2310 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2311 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2312 dump_cfg_string(oProxyCommand, o->proxy_command);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002313 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002314 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002315
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002316 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002317 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2318 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2319 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2320
2321 /* String array options */
2322 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2323 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2324 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2325 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2326 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2327
2328 /* Special cases */
2329
2330 /* oConnectTimeout */
2331 if (o->connection_timeout == -1)
2332 printf("connecttimeout none\n");
2333 else
2334 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2335
2336 /* oTunnelDevice */
2337 printf("tunneldevice");
2338 if (o->tun_local == SSH_TUNID_ANY)
2339 printf(" any");
2340 else
2341 printf(" %d", o->tun_local);
2342 if (o->tun_remote == SSH_TUNID_ANY)
2343 printf(":any");
2344 else
2345 printf(":%d", o->tun_remote);
2346 printf("\n");
2347
2348 /* oCanonicalizePermittedCNAMEs */
2349 if ( o->num_permitted_cnames > 0) {
2350 printf("canonicalizePermittedcnames");
2351 for (i = 0; i < o->num_permitted_cnames; i++) {
2352 printf(" %s:%s", o->permitted_cnames[i].source_list,
2353 o->permitted_cnames[i].target_list);
2354 }
2355 printf("\n");
2356 }
2357
2358 /* oCipher */
2359 if (o->cipher != SSH_CIPHER_NOT_SET)
2360 printf("Cipher %s\n", cipher_name(o->cipher));
2361
2362 /* oControlPersist */
2363 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2364 dump_cfg_fmtint(oControlPersist, o->control_persist);
2365 else
2366 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2367
2368 /* oEscapeChar */
2369 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2370 printf("escapechar none\n");
2371 else {
2372 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2373 printf("escapechar %s\n", vbuf);
2374 }
2375
2376 /* oIPQoS */
2377 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2378 printf("%s\n", iptos2str(o->ip_qos_bulk));
2379
2380 /* oRekeyLimit */
2381 printf("rekeylimit %lld %d\n",
2382 (long long)o->rekey_limit, o->rekey_interval);
2383
2384 /* oStreamLocalBindMask */
2385 printf("streamlocalbindmask 0%o\n",
2386 o->fwd_opts.streamlocal_bind_mask);
2387}