blob: 66090e30594e182a7f5d05179c11493a4a0f0791 [file] [log] [blame]
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001/* $OpenBSD: readconf.c,v 1.234 2015/04/24 01:36:00 deraadt 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 size_t len;
496 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
497
498 /*
499 * Configuration is likely to be incomplete at this point so we
500 * must be prepared to use default values.
501 */
502 port = options->port <= 0 ? default_ssh_port() : options->port;
503 ruser = options->user == NULL ? pw->pw_name : options->user;
Damien Miller084bcd22013-10-23 16:30:51 +1100504 if (options->hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100505 /* NB. Please keep in sync with ssh.c:main() */
Damien Miller084bcd22013-10-23 16:30:51 +1100506 host = percent_expand(options->hostname,
507 "h", host_arg, (char *)NULL);
508 } else
509 host = xstrdup(host_arg);
Damien Miller194fd902013-10-15 12:13:05 +1100510
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000511 debug2("checking match for '%s' host %s originally %s",
512 cp, host, original_host);
513 while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
514 criteria = NULL;
515 this_result = 1;
516 if ((negate = attrib[0] == '!'))
517 attrib++;
518 /* criteria "all" and "canonical" have no argument */
Damien Millercf31f382013-10-24 21:02:56 +1100519 if (strcasecmp(attrib, "all") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000520 if (attributes > 1 ||
Damien Millercf31f382013-10-24 21:02:56 +1100521 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000522 error("%.200s line %d: '%s' cannot be combined "
523 "with other Match attributes",
524 filename, linenum, oattrib);
Damien Millercf31f382013-10-24 21:02:56 +1100525 result = -1;
526 goto out;
527 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000528 if (result)
529 result = negate ? 0 : 1;
Damien Millercf31f382013-10-24 21:02:56 +1100530 goto out;
531 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000532 attributes++;
533 if (strcasecmp(attrib, "canonical") == 0) {
534 r = !!post_canon; /* force bitmask member to boolean */
535 if (r == (negate ? 1 : 0))
536 this_result = result = 0;
537 debug3("%.200s line %d: %smatched '%s'",
538 filename, linenum,
539 this_result ? "" : "not ", oattrib);
540 continue;
541 }
542 /* All other criteria require an argument */
Damien Miller194fd902013-10-15 12:13:05 +1100543 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
544 error("Missing Match criteria for %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100545 result = -1;
546 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100547 }
548 len = strlen(arg);
549 if (strcasecmp(attrib, "host") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000550 criteria = xstrdup(host);
551 r = match_hostname(host, arg, len) == 1;
552 if (r == (negate ? 1 : 0))
553 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100554 } else if (strcasecmp(attrib, "originalhost") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000555 criteria = xstrdup(original_host);
556 r = match_hostname(original_host, arg, len) == 1;
557 if (r == (negate ? 1 : 0))
558 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100559 } else if (strcasecmp(attrib, "user") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000560 criteria = xstrdup(ruser);
561 r = match_pattern_list(ruser, arg, len, 0) == 1;
562 if (r == (negate ? 1 : 0))
563 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100564 } else if (strcasecmp(attrib, "localuser") == 0) {
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000565 criteria = xstrdup(pw->pw_name);
566 r = match_pattern_list(pw->pw_name, arg, len, 0) == 1;
567 if (r == (negate ? 1 : 0))
568 this_result = result = 0;
Damien Miller8a04be72013-10-23 16:29:40 +1100569 } else if (strcasecmp(attrib, "exec") == 0) {
Damien Miller194fd902013-10-15 12:13:05 +1100570 if (gethostname(thishost, sizeof(thishost)) == -1)
571 fatal("gethostname: %s", strerror(errno));
572 strlcpy(shorthost, thishost, sizeof(shorthost));
573 shorthost[strcspn(thishost, ".")] = '\0';
574 snprintf(portstr, sizeof(portstr), "%d", port);
575
576 cmd = percent_expand(arg,
577 "L", shorthost,
578 "d", pw->pw_dir,
579 "h", host,
580 "l", thishost,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000581 "n", original_host,
Damien Miller194fd902013-10-15 12:13:05 +1100582 "p", portstr,
583 "r", ruser,
584 "u", pw->pw_name,
585 (char *)NULL);
Damien Miller06287802014-02-24 15:56:45 +1100586 if (result != 1) {
587 /* skip execution if prior predicate failed */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000588 debug3("%.200s line %d: skipped exec "
589 "\"%.100s\"", filename, linenum, cmd);
590 free(cmd);
591 continue;
Damien Miller06287802014-02-24 15:56:45 +1100592 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000593 r = execute_in_shell(cmd);
594 if (r == -1) {
595 fatal("%.200s line %d: match exec "
596 "'%.100s' error", filename,
597 linenum, cmd);
598 }
599 criteria = xstrdup(cmd);
Damien Miller194fd902013-10-15 12:13:05 +1100600 free(cmd);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000601 /* Force exit status to boolean */
602 r = r == 0;
603 if (r == (negate ? 1 : 0))
604 this_result = result = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100605 } else {
606 error("Unsupported Match attribute %s", attrib);
Damien Miller084bcd22013-10-23 16:30:51 +1100607 result = -1;
608 goto out;
Damien Miller194fd902013-10-15 12:13:05 +1100609 }
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000610 debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
611 filename, linenum, this_result ? "": "not ",
612 oattrib, criteria);
613 free(criteria);
Damien Miller194fd902013-10-15 12:13:05 +1100614 }
Damien Millercf31f382013-10-24 21:02:56 +1100615 if (attributes == 0) {
616 error("One or more attributes required for Match");
617 result = -1;
618 goto out;
619 }
Damien Miller084bcd22013-10-23 16:30:51 +1100620 out:
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000621 if (result != -1)
622 debug2("match %sfound", result ? "" : "not ");
623 *condition = cp;
Damien Miller084bcd22013-10-23 16:30:51 +1100624 free(host);
Damien Miller194fd902013-10-15 12:13:05 +1100625 return result;
626}
627
Damien Miller0faf7472013-10-17 11:47:23 +1100628/* Check and prepare a domain name: removes trailing '.' and lowercases */
629static void
630valid_domain(char *name, const char *filename, int linenum)
631{
632 size_t i, l = strlen(name);
633 u_char c, last = '\0';
634
635 if (l == 0)
636 fatal("%s line %d: empty hostname suffix", filename, linenum);
637 if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0]))
638 fatal("%s line %d: hostname suffix \"%.100s\" "
639 "starts with invalid character", filename, linenum, name);
640 for (i = 0; i < l; i++) {
641 c = tolower((u_char)name[i]);
642 name[i] = (char)c;
643 if (last == '.' && c == '.')
644 fatal("%s line %d: hostname suffix \"%.100s\" contains "
645 "consecutive separators", filename, linenum, name);
646 if (c != '.' && c != '-' && !isalnum(c) &&
647 c != '_') /* technically invalid, but common */
648 fatal("%s line %d: hostname suffix \"%.100s\" contains "
649 "invalid characters", filename, linenum, name);
650 last = c;
651 }
652 if (name[l - 1] == '.')
653 name[l - 1] = '\0';
654}
655
Damien Miller5428f641999-11-25 11:54:57 +1100656/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000657 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100658 */
Damien Miller4af51302000-04-16 11:18:38 +1000659static OpCodes
Darren Tucker07636982013-05-16 20:30:03 +1000660parse_token(const char *cp, const char *filename, int linenum,
661 const char *ignored_unknown)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662{
Darren Tucker07636982013-05-16 20:30:03 +1000663 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664
Damien Miller95def091999-11-25 00:26:21 +1100665 for (i = 0; keywords[i].name; i++)
Darren Tucker07636982013-05-16 20:30:03 +1000666 if (strcmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100667 return keywords[i].opcode;
Darren Tucker07636982013-05-16 20:30:03 +1000668 if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown,
669 strlen(ignored_unknown), 1) == 1)
670 return oIgnoredUnknownOption;
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000671 error("%s: line %d: Bad configuration option: %s",
672 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100673 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674}
675
Damien Millere9fc72e2013-10-15 12:14:12 +1100676/* Multistate option parsing */
677struct multistate {
678 char *key;
679 int value;
680};
681static const struct multistate multistate_flag[] = {
682 { "true", 1 },
683 { "false", 0 },
684 { "yes", 1 },
685 { "no", 0 },
686 { NULL, -1 }
687};
688static const struct multistate multistate_yesnoask[] = {
689 { "true", 1 },
690 { "false", 0 },
691 { "yes", 1 },
692 { "no", 0 },
693 { "ask", 2 },
694 { NULL, -1 }
695};
696static const struct multistate multistate_addressfamily[] = {
697 { "inet", AF_INET },
698 { "inet6", AF_INET6 },
699 { "any", AF_UNSPEC },
700 { NULL, -1 }
701};
702static const struct multistate multistate_controlmaster[] = {
703 { "true", SSHCTL_MASTER_YES },
704 { "yes", SSHCTL_MASTER_YES },
705 { "false", SSHCTL_MASTER_NO },
706 { "no", SSHCTL_MASTER_NO },
707 { "auto", SSHCTL_MASTER_AUTO },
708 { "ask", SSHCTL_MASTER_ASK },
709 { "autoask", SSHCTL_MASTER_AUTO_ASK },
710 { NULL, -1 }
711};
712static const struct multistate multistate_tunnel[] = {
713 { "ethernet", SSH_TUNMODE_ETHERNET },
714 { "point-to-point", SSH_TUNMODE_POINTOPOINT },
715 { "true", SSH_TUNMODE_DEFAULT },
716 { "yes", SSH_TUNMODE_DEFAULT },
717 { "false", SSH_TUNMODE_NO },
718 { "no", SSH_TUNMODE_NO },
719 { NULL, -1 }
720};
721static const struct multistate multistate_requesttty[] = {
722 { "true", REQUEST_TTY_YES },
723 { "yes", REQUEST_TTY_YES },
724 { "false", REQUEST_TTY_NO },
725 { "no", REQUEST_TTY_NO },
726 { "force", REQUEST_TTY_FORCE },
727 { "auto", REQUEST_TTY_AUTO },
728 { NULL, -1 }
729};
Damien Miller38505592013-10-17 11:48:13 +1100730static const struct multistate multistate_canonicalizehostname[] = {
Damien Miller0faf7472013-10-17 11:47:23 +1100731 { "true", SSH_CANONICALISE_YES },
732 { "false", SSH_CANONICALISE_NO },
733 { "yes", SSH_CANONICALISE_YES },
734 { "no", SSH_CANONICALISE_NO },
735 { "always", SSH_CANONICALISE_ALWAYS },
736 { NULL, -1 }
737};
Damien Millere9fc72e2013-10-15 12:14:12 +1100738
Damien Miller5428f641999-11-25 11:54:57 +1100739/*
740 * Processes a single option line as used in the configuration files. This
741 * only sets those values that have not already been set.
742 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100743#define WHITESPACE " \t\r\n"
Damien Miller2ccf6611999-11-15 15:25:10 +1100744int
Damien Miller194fd902013-10-15 12:13:05 +1100745process_config_line(Options *options, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000746 const char *original_host, char *line, const char *filename,
747 int linenum, int *activep, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748{
Damien Miller295ee632011-05-29 21:42:31 +1000749 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
750 char **cpptr, fwdarg[256];
Darren Tucker07636982013-05-16 20:30:03 +1000751 u_int i, *uintptr, max_entries = 0;
Damien Miller194fd902013-10-15 12:13:05 +1100752 int negated, opcode, *intptr, value, value2, cmdline = 0;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100753 LogLevel *log_level_ptr;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000754 long long val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100755 size_t len;
Damien Miller7acefbb2014-07-18 14:11:24 +1000756 struct Forward fwd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100757 const struct multistate *multistate_ptr;
Damien Miller0faf7472013-10-17 11:47:23 +1100758 struct allowed_cname *cname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759
Damien Miller194fd902013-10-15 12:13:05 +1100760 if (activep == NULL) { /* We are processing a command line directive */
761 cmdline = 1;
762 activep = &cmdline;
763 }
764
Damien Millerc652cac2003-05-14 13:40:54 +1000765 /* Strip trailing whitespace */
djm@openbsd.org26e0bcf2015-03-30 00:00:29 +0000766 if ((len = strlen(line)) == 0)
767 return 0;
768 for (len--; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000769 if (strchr(WHITESPACE, line[len]) == NULL)
770 break;
771 line[len] = '\0';
772 }
773
Damien Millerbe484b52000-07-15 14:14:16 +1000774 s = line;
775 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100776 if ((keyword = strdelim(&s)) == NULL)
777 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000778 /* Ignore leading whitespace. */
779 if (*keyword == '\0')
780 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000781 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100782 return 0;
Darren Tucker07636982013-05-16 20:30:03 +1000783 /* Match lowercase keyword */
Damien Millere9fc72e2013-10-15 12:14:12 +1100784 lowercase(keyword);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Darren Tucker07636982013-05-16 20:30:03 +1000786 opcode = parse_token(keyword, filename, linenum,
787 options->ignored_unknown);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788
Damien Miller95def091999-11-25 00:26:21 +1100789 switch (opcode) {
790 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100791 /* don't panic, but count bad options */
792 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100793 /* NOTREACHED */
Darren Tucker07636982013-05-16 20:30:03 +1000794 case oIgnoredUnknownOption:
795 debug("%s line %d: Ignored unknown option \"%s\"",
796 filename, linenum, keyword);
797 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000798 case oConnectTimeout:
799 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100800parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000801 arg = strdelim(&s);
802 if (!arg || *arg == '\0')
803 fatal("%s line %d: missing time value.",
804 filename, linenum);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000805 if (strcmp(arg, "none") == 0)
806 value = -1;
807 else if ((value = convtime(arg)) == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000808 fatal("%s line %d: invalid time value.",
809 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100810 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000811 *intptr = value;
812 break;
813
Damien Miller95def091999-11-25 00:26:21 +1100814 case oForwardAgent:
815 intptr = &options->forward_agent;
Damien Millere9fc72e2013-10-15 12:14:12 +1100816 parse_flag:
817 multistate_ptr = multistate_flag;
818 parse_multistate:
Damien Millerbe484b52000-07-15 14:14:16 +1000819 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000820 if (!arg || *arg == '\0')
Damien Millere9fc72e2013-10-15 12:14:12 +1100821 fatal("%s line %d: missing argument.",
822 filename, linenum);
823 value = -1;
824 for (i = 0; multistate_ptr[i].key != NULL; i++) {
825 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
826 value = multistate_ptr[i].value;
827 break;
828 }
829 }
830 if (value == -1)
831 fatal("%s line %d: unsupported option \"%s\".",
832 filename, linenum, arg);
Damien Miller95def091999-11-25 00:26:21 +1100833 if (*activep && *intptr == -1)
834 *intptr = value;
835 break;
836
837 case oForwardX11:
838 intptr = &options->forward_x11;
839 goto parse_flag;
840
Darren Tucker0a118da2003-10-15 15:54:32 +1000841 case oForwardX11Trusted:
842 intptr = &options->forward_x11_trusted;
843 goto parse_flag;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000844
Damien Miller1ab6a512010-06-26 10:02:24 +1000845 case oForwardX11Timeout:
846 intptr = &options->forward_x11_timeout;
847 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000848
Damien Miller95def091999-11-25 00:26:21 +1100849 case oGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +1000850 intptr = &options->fwd_opts.gateway_ports;
Damien Miller95def091999-11-25 00:26:21 +1100851 goto parse_flag;
852
Darren Tuckere7d4b192006-07-12 22:17:10 +1000853 case oExitOnForwardFailure:
854 intptr = &options->exit_on_forward_failure;
855 goto parse_flag;
856
Damien Miller95def091999-11-25 00:26:21 +1100857 case oUsePrivilegedPort:
858 intptr = &options->use_privileged_port;
859 goto parse_flag;
860
Damien Miller95def091999-11-25 00:26:21 +1100861 case oPasswordAuthentication:
862 intptr = &options->password_authentication;
863 goto parse_flag;
864
Damien Miller874d77b2000-10-14 16:23:11 +1100865 case oKbdInteractiveAuthentication:
866 intptr = &options->kbd_interactive_authentication;
867 goto parse_flag;
868
869 case oKbdInteractiveDevices:
870 charptr = &options->kbd_interactive_devices;
871 goto parse_string;
872
Damien Miller0bc1bd82000-11-13 22:57:25 +1100873 case oPubkeyAuthentication:
874 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000875 goto parse_flag;
876
Damien Miller95def091999-11-25 00:26:21 +1100877 case oRSAAuthentication:
878 intptr = &options->rsa_authentication;
879 goto parse_flag;
880
881 case oRhostsRSAAuthentication:
882 intptr = &options->rhosts_rsa_authentication;
883 goto parse_flag;
884
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000885 case oHostbasedAuthentication:
886 intptr = &options->hostbased_authentication;
887 goto parse_flag;
888
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000889 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000890 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100891 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000892
Darren Tucker0efd1552003-08-26 11:49:55 +1000893 case oGssAuthentication:
894 intptr = &options->gss_authentication;
895 goto parse_flag;
896
897 case oGssDelegateCreds:
898 intptr = &options->gss_deleg_creds;
899 goto parse_flag;
900
Damien Miller95def091999-11-25 00:26:21 +1100901 case oBatchMode:
902 intptr = &options->batch_mode;
903 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904
Damien Miller95def091999-11-25 00:26:21 +1100905 case oCheckHostIP:
906 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000907 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908
Damien Miller37876e92003-05-15 10:19:46 +1000909 case oVerifyHostKeyDNS:
910 intptr = &options->verify_host_key_dns;
Damien Millere9fc72e2013-10-15 12:14:12 +1100911 multistate_ptr = multistate_yesnoask;
912 goto parse_multistate;
Damien Miller37876e92003-05-15 10:19:46 +1000913
Damien Miller95def091999-11-25 00:26:21 +1100914 case oStrictHostKeyChecking:
915 intptr = &options->strict_host_key_checking;
Damien Millere9fc72e2013-10-15 12:14:12 +1100916 multistate_ptr = multistate_yesnoask;
917 goto parse_multistate;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
Damien Miller95def091999-11-25 00:26:21 +1100919 case oCompression:
920 intptr = &options->compression;
921 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922
Damien Miller12c150e2003-12-17 16:31:10 +1100923 case oTCPKeepAlive:
924 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100925 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000927 case oNoHostAuthenticationForLocalhost:
928 intptr = &options->no_host_authentication_for_localhost;
929 goto parse_flag;
930
Damien Miller95def091999-11-25 00:26:21 +1100931 case oNumberOfPasswordPrompts:
932 intptr = &options->number_of_password_prompts;
933 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Miller95def091999-11-25 00:26:21 +1100935 case oCompressionLevel:
936 intptr = &options->compression_level;
937 goto parse_int;
938
Damien Millera5539d22003-04-09 20:50:06 +1000939 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000940 arg = strdelim(&s);
941 if (!arg || *arg == '\0')
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000942 fatal("%.200s line %d: Missing argument.", filename,
943 linenum);
944 if (strcmp(arg, "default") == 0) {
945 val64 = 0;
946 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +1000947 if (scan_scaled(arg, &val64) == -1)
948 fatal("%.200s line %d: Bad number '%s': %s",
949 filename, linenum, arg, strerror(errno));
950 /* check for too-large or too-small limits */
951 if (val64 > UINT_MAX)
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000952 fatal("%.200s line %d: RekeyLimit too large",
953 filename, linenum);
954 if (val64 != 0 && val64 < 16)
955 fatal("%.200s line %d: RekeyLimit too small",
956 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000957 }
Damien Miller3dff1762008-02-10 22:25:52 +1100958 if (*activep && options->rekey_limit == -1)
959 options->rekey_limit = (u_int32_t)val64;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000960 if (s != NULL) { /* optional rekey interval present */
961 if (strcmp(s, "none") == 0) {
962 (void)strdelim(&s); /* discard */
963 break;
964 }
965 intptr = &options->rekey_interval;
966 goto parse_time;
967 }
Damien Millera5539d22003-04-09 20:50:06 +1000968 break;
969
Damien Miller95def091999-11-25 00:26:21 +1100970 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000971 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000972 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100973 fatal("%.200s line %d: Missing argument.", filename, linenum);
974 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100975 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000976 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100977 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100978 filename, linenum, SSH_MAX_IDENTITY_FILES);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000979 add_identity_file(options, NULL,
980 arg, flags & SSHCONF_USERCONF);
Damien Miller95def091999-11-25 00:26:21 +1100981 }
982 break;
983
Damien Millerd3a18572000-06-07 19:55:44 +1000984 case oXAuthLocation:
985 charptr=&options->xauth_location;
986 goto parse_string;
987
Damien Miller95def091999-11-25 00:26:21 +1100988 case oUser:
989 charptr = &options->user;
990parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000991 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000992 if (!arg || *arg == '\0')
Damien Miller295ee632011-05-29 21:42:31 +1000993 fatal("%.200s line %d: Missing argument.",
994 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100995 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000996 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100997 break;
998
999 case oGlobalKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001000 cpptr = (char **)&options->system_hostfiles;
1001 uintptr = &options->num_system_hostfiles;
1002 max_entries = SSH_MAX_HOSTS_FILES;
1003parse_char_array:
1004 if (*activep && *uintptr == 0) {
1005 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1006 if ((*uintptr) >= max_entries)
1007 fatal("%s line %d: "
1008 "too many authorized keys files.",
1009 filename, linenum);
1010 cpptr[(*uintptr)++] = xstrdup(arg);
1011 }
1012 }
1013 return 0;
Damien Miller95def091999-11-25 00:26:21 +11001014
1015 case oUserKnownHostsFile:
Damien Miller295ee632011-05-29 21:42:31 +10001016 cpptr = (char **)&options->user_hostfiles;
1017 uintptr = &options->num_user_hostfiles;
1018 max_entries = SSH_MAX_HOSTS_FILES;
1019 goto parse_char_array;
Damien Millereba71ba2000-04-29 23:57:08 +10001020
Damien Miller95def091999-11-25 00:26:21 +11001021 case oHostName:
1022 charptr = &options->hostname;
1023 goto parse_string;
1024
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001025 case oHostKeyAlias:
1026 charptr = &options->host_key_alias;
1027 goto parse_string;
1028
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001029 case oPreferredAuthentications:
1030 charptr = &options->preferred_authentications;
1031 goto parse_string;
1032
Ben Lindstrome0f88042001-04-30 13:06:24 +00001033 case oBindAddress:
1034 charptr = &options->bind_address;
1035 goto parse_string;
1036
Damien Miller7ea845e2010-02-12 09:21:02 +11001037 case oPKCS11Provider:
1038 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001039 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +00001040
Damien Miller95def091999-11-25 00:26:21 +11001041 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +11001042 charptr = &options->proxy_command;
1043parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +10001044 if (s == NULL)
1045 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +11001046 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +11001047 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +11001048 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +11001049 return 0;
1050
1051 case oPort:
1052 intptr = &options->port;
1053parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +10001054 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001055 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001056 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001057 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +11001058 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +11001059
1060 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +10001061 value = strtol(arg, &endofnumber, 0);
1062 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +11001063 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001064 if (*activep && *intptr == -1)
1065 *intptr = value;
1066 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Damien Miller95def091999-11-25 00:26:21 +11001068 case oConnectionAttempts:
1069 intptr = &options->connection_attempts;
1070 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +11001071
Damien Miller95def091999-11-25 00:26:21 +11001072 case oCipher:
1073 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +10001074 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001075 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001076 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001077 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +11001078 if (value == -1)
1079 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001080 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +11001081 if (*activep && *intptr == -1)
1082 *intptr = value;
1083 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001084
Damien Miller78928792000-04-12 20:17:38 +10001085 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +10001086 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001087 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001088 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001089 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +10001090 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001091 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001092 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +10001093 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +10001094 break;
1095
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001096 case oMacs:
1097 arg = strdelim(&s);
1098 if (!arg || *arg == '\0')
1099 fatal("%.200s line %d: Missing argument.", filename, linenum);
1100 if (!mac_valid(arg))
1101 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001102 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001103 if (*activep && options->macs == NULL)
1104 options->macs = xstrdup(arg);
1105 break;
1106
Damien Millerd5f62bf2010-09-24 22:11:14 +10001107 case oKexAlgorithms:
1108 arg = strdelim(&s);
1109 if (!arg || *arg == '\0')
1110 fatal("%.200s line %d: Missing argument.",
1111 filename, linenum);
1112 if (!kex_names_valid(arg))
1113 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1114 filename, linenum, arg ? arg : "<NONE>");
1115 if (*activep && options->kex_algorithms == NULL)
1116 options->kex_algorithms = xstrdup(arg);
1117 break;
1118
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001119 case oHostKeyAlgorithms:
1120 arg = strdelim(&s);
1121 if (!arg || *arg == '\0')
1122 fatal("%.200s line %d: Missing argument.", filename, linenum);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001123 if (!sshkey_names_valid2(arg, 1))
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001124 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001125 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001126 if (*activep && options->hostkeyalgorithms == NULL)
1127 options->hostkeyalgorithms = xstrdup(arg);
1128 break;
1129
Damien Miller78928792000-04-12 20:17:38 +10001130 case oProtocol:
1131 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +10001132 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001133 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +10001134 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001135 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +10001136 if (value == SSH_PROTO_UNKNOWN)
1137 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001138 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +10001139 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
1140 *intptr = value;
1141 break;
1142
Damien Miller95def091999-11-25 00:26:21 +11001143 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001144 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +10001145 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001146 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001147 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001148 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001149 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001150 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1151 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +11001152 break;
1153
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001154 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +11001155 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +11001156 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +10001157 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001158 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001159 fatal("%.200s line %d: Missing port argument.",
1160 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001161
Damien Millera699d952008-11-03 19:27:34 +11001162 if (opcode == oLocalForward ||
1163 opcode == oRemoteForward) {
1164 arg2 = strdelim(&s);
1165 if (arg2 == NULL || *arg2 == '\0')
1166 fatal("%.200s line %d: Missing target argument.",
1167 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001168
Damien Millera699d952008-11-03 19:27:34 +11001169 /* construct a string for parse_forward */
1170 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
1171 } else if (opcode == oDynamicForward) {
1172 strlcpy(fwdarg, arg, sizeof(fwdarg));
1173 }
1174
1175 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +11001176 opcode == oDynamicForward ? 1 : 0,
1177 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001178 fatal("%.200s line %d: Bad forwarding specification.",
1179 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001180
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001181 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +11001182 if (opcode == oLocalForward ||
1183 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001184 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001185 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001186 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +00001187 }
Damien Miller95def091999-11-25 00:26:21 +11001188 break;
1189
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001190 case oClearAllForwardings:
1191 intptr = &options->clear_forwardings;
1192 goto parse_flag;
1193
Damien Miller95def091999-11-25 00:26:21 +11001194 case oHost:
Damien Miller194fd902013-10-15 12:13:05 +11001195 if (cmdline)
1196 fatal("Host directive not supported as a command-line "
1197 "option");
Damien Miller95def091999-11-25 00:26:21 +11001198 *activep = 0;
Damien Millerfe924212011-05-15 08:44:45 +10001199 arg2 = NULL;
1200 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1201 negated = *arg == '!';
1202 if (negated)
1203 arg++;
Damien Miller37023962000-07-11 17:31:38 +10001204 if (match_pattern(host, arg)) {
Damien Millerfe924212011-05-15 08:44:45 +10001205 if (negated) {
1206 debug("%.200s line %d: Skipping Host "
1207 "block because of negated match "
1208 "for %.100s", filename, linenum,
1209 arg);
1210 *activep = 0;
1211 break;
1212 }
1213 if (!*activep)
1214 arg2 = arg; /* logged below */
Damien Miller95def091999-11-25 00:26:21 +11001215 *activep = 1;
Damien Miller95def091999-11-25 00:26:21 +11001216 }
Damien Millerfe924212011-05-15 08:44:45 +10001217 }
1218 if (*activep)
1219 debug("%.200s line %d: Applying options for %.100s",
1220 filename, linenum, arg2);
Damien Millerbe484b52000-07-15 14:14:16 +10001221 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +11001222 return 0;
1223
Damien Miller194fd902013-10-15 12:13:05 +11001224 case oMatch:
1225 if (cmdline)
1226 fatal("Host directive not supported as a command-line "
1227 "option");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001228 value = match_cfg_line(options, &s, pw, host, original_host,
1229 flags & SSHCONF_POSTCANON, filename, linenum);
Damien Miller194fd902013-10-15 12:13:05 +11001230 if (value < 0)
1231 fatal("%.200s line %d: Bad Match condition", filename,
1232 linenum);
1233 *activep = value;
1234 break;
1235
Damien Miller95def091999-11-25 00:26:21 +11001236 case oEscapeChar:
1237 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +10001238 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +10001239 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +11001240 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +10001241 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +00001242 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1243 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +10001244 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +00001245 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +10001246 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001247 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +11001248 else {
1249 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001250 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +11001251 /* NOTREACHED */
1252 value = 0; /* Avoid compiler warning. */
1253 }
1254 if (*activep && *intptr == -1)
1255 *intptr = value;
1256 break;
1257
Damien Miller20a8f972003-05-18 20:50:30 +10001258 case oAddressFamily:
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001259 intptr = &options->address_family;
Damien Millere9fc72e2013-10-15 12:14:12 +11001260 multistate_ptr = multistate_addressfamily;
1261 goto parse_multistate;
Damien Miller20a8f972003-05-18 20:50:30 +10001262
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001263 case oEnableSSHKeysign:
1264 intptr = &options->enable_ssh_keysign;
1265 goto parse_flag;
1266
Damien Millerbd394c32004-03-08 23:12:36 +11001267 case oIdentitiesOnly:
1268 intptr = &options->identities_only;
1269 goto parse_flag;
1270
Damien Miller509b0102003-12-17 16:33:10 +11001271 case oServerAliveInterval:
1272 intptr = &options->server_alive_interval;
1273 goto parse_time;
1274
1275 case oServerAliveCountMax:
1276 intptr = &options->server_alive_count_max;
1277 goto parse_int;
1278
Darren Tucker46bc0752004-05-02 22:11:30 +10001279 case oSendEnv:
1280 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1281 if (strchr(arg, '=') != NULL)
1282 fatal("%s line %d: Invalid environment name.",
1283 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +11001284 if (!*activep)
1285 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001286 if (options->num_send_env >= MAX_SEND_ENV)
1287 fatal("%s line %d: too many send env.",
1288 filename, linenum);
1289 options->send_env[options->num_send_env++] =
1290 xstrdup(arg);
1291 }
1292 break;
1293
Damien Miller0e220db2004-06-15 10:34:08 +10001294 case oControlPath:
1295 charptr = &options->control_path;
1296 goto parse_string;
1297
1298 case oControlMaster:
1299 intptr = &options->control_master;
Damien Millere9fc72e2013-10-15 12:14:12 +11001300 multistate_ptr = multistate_controlmaster;
1301 goto parse_multistate;
Damien Miller0e220db2004-06-15 10:34:08 +10001302
Damien Millere11e1ea2010-08-03 16:04:46 +10001303 case oControlPersist:
1304 /* no/false/yes/true, or a time spec */
1305 intptr = &options->control_persist;
1306 arg = strdelim(&s);
1307 if (!arg || *arg == '\0')
1308 fatal("%.200s line %d: Missing ControlPersist"
1309 " argument.", filename, linenum);
1310 value = 0;
1311 value2 = 0; /* timeout */
1312 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1313 value = 0;
1314 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1315 value = 1;
1316 else if ((value2 = convtime(arg)) >= 0)
1317 value = 1;
1318 else
1319 fatal("%.200s line %d: Bad ControlPersist argument.",
1320 filename, linenum);
1321 if (*activep && *intptr == -1) {
1322 *intptr = value;
1323 options->control_persist_timeout = value2;
1324 }
1325 break;
1326
Damien Millere1776152005-03-01 21:47:37 +11001327 case oHashKnownHosts:
1328 intptr = &options->hash_known_hosts;
1329 goto parse_flag;
1330
Damien Millerd27b9472005-12-13 19:29:02 +11001331 case oTunnel:
1332 intptr = &options->tun_open;
Damien Millere9fc72e2013-10-15 12:14:12 +11001333 multistate_ptr = multistate_tunnel;
1334 goto parse_multistate;
Damien Millerd27b9472005-12-13 19:29:02 +11001335
1336 case oTunnelDevice:
1337 arg = strdelim(&s);
1338 if (!arg || *arg == '\0')
1339 fatal("%.200s line %d: Missing argument.", filename, linenum);
1340 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +11001341 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +11001342 fatal("%.200s line %d: Bad tun device.", filename, linenum);
1343 if (*activep) {
1344 options->tun_local = value;
1345 options->tun_remote = value2;
1346 }
1347 break;
1348
1349 case oLocalCommand:
1350 charptr = &options->local_command;
1351 goto parse_command;
1352
1353 case oPermitLocalCommand:
1354 intptr = &options->permit_local_command;
1355 goto parse_flag;
1356
Damien Miller10288242008-06-30 00:04:03 +10001357 case oVisualHostKey:
1358 intptr = &options->visual_host_key;
1359 goto parse_flag;
1360
Damien Miller0dac6fb2010-11-20 15:19:38 +11001361 case oIPQoS:
1362 arg = strdelim(&s);
1363 if ((value = parse_ipqos(arg)) == -1)
1364 fatal("%s line %d: Bad IPQoS value: %s",
1365 filename, linenum, arg);
1366 arg = strdelim(&s);
1367 if (arg == NULL)
1368 value2 = value;
1369 else if ((value2 = parse_ipqos(arg)) == -1)
1370 fatal("%s line %d: Bad IPQoS value: %s",
1371 filename, linenum, arg);
1372 if (*activep) {
1373 options->ip_qos_interactive = value;
1374 options->ip_qos_bulk = value2;
1375 }
1376 break;
1377
Darren Tucker71e4d542009-07-06 07:12:27 +10001378 case oUseRoaming:
1379 intptr = &options->use_roaming;
1380 goto parse_flag;
1381
Damien Miller21771e22011-05-15 08:45:50 +10001382 case oRequestTTY:
Damien Miller21771e22011-05-15 08:45:50 +10001383 intptr = &options->request_tty;
Damien Millere9fc72e2013-10-15 12:14:12 +11001384 multistate_ptr = multistate_requesttty;
1385 goto parse_multistate;
Damien Miller21771e22011-05-15 08:45:50 +10001386
Darren Tucker07636982013-05-16 20:30:03 +10001387 case oIgnoreUnknown:
1388 charptr = &options->ignored_unknown;
1389 goto parse_string;
1390
Damien Miller1262b662013-08-21 02:44:24 +10001391 case oProxyUseFdpass:
1392 intptr = &options->proxy_use_fdpass;
1393 goto parse_flag;
1394
Damien Miller0faf7472013-10-17 11:47:23 +11001395 case oCanonicalDomains:
1396 value = options->num_canonical_domains != 0;
1397 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1398 valid_domain(arg, filename, linenum);
1399 if (!*activep || value)
1400 continue;
1401 if (options->num_canonical_domains >= MAX_CANON_DOMAINS)
1402 fatal("%s line %d: too many hostname suffixes.",
1403 filename, linenum);
1404 options->canonical_domains[
1405 options->num_canonical_domains++] = xstrdup(arg);
1406 }
1407 break;
1408
Damien Miller38505592013-10-17 11:48:13 +11001409 case oCanonicalizePermittedCNAMEs:
Damien Miller0faf7472013-10-17 11:47:23 +11001410 value = options->num_permitted_cnames != 0;
1411 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1412 /* Either '*' for everything or 'list:list' */
1413 if (strcmp(arg, "*") == 0)
1414 arg2 = arg;
1415 else {
1416 lowercase(arg);
1417 if ((arg2 = strchr(arg, ':')) == NULL ||
1418 arg2[1] == '\0') {
1419 fatal("%s line %d: "
1420 "Invalid permitted CNAME \"%s\"",
1421 filename, linenum, arg);
1422 }
1423 *arg2 = '\0';
1424 arg2++;
1425 }
1426 if (!*activep || value)
1427 continue;
1428 if (options->num_permitted_cnames >= MAX_CANON_DOMAINS)
1429 fatal("%s line %d: too many permitted CNAMEs.",
1430 filename, linenum);
1431 cname = options->permitted_cnames +
1432 options->num_permitted_cnames++;
1433 cname->source_list = xstrdup(arg);
1434 cname->target_list = xstrdup(arg2);
1435 }
1436 break;
1437
Damien Miller38505592013-10-17 11:48:13 +11001438 case oCanonicalizeHostname:
1439 intptr = &options->canonicalize_hostname;
1440 multistate_ptr = multistate_canonicalizehostname;
Damien Miller0faf7472013-10-17 11:47:23 +11001441 goto parse_multistate;
1442
Damien Miller38505592013-10-17 11:48:13 +11001443 case oCanonicalizeMaxDots:
1444 intptr = &options->canonicalize_max_dots;
Damien Miller0faf7472013-10-17 11:47:23 +11001445 goto parse_int;
1446
Damien Miller38505592013-10-17 11:48:13 +11001447 case oCanonicalizeFallbackLocal:
1448 intptr = &options->canonicalize_fallback_local;
Damien Miller0faf7472013-10-17 11:47:23 +11001449 goto parse_flag;
1450
Damien Miller7acefbb2014-07-18 14:11:24 +10001451 case oStreamLocalBindMask:
1452 arg = strdelim(&s);
1453 if (!arg || *arg == '\0')
1454 fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum);
1455 /* Parse mode in octal format */
1456 value = strtol(arg, &endofnumber, 8);
1457 if (arg == endofnumber || value < 0 || value > 0777)
1458 fatal("%.200s line %d: Bad mask.", filename, linenum);
1459 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1460 break;
1461
1462 case oStreamLocalBindUnlink:
1463 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1464 goto parse_flag;
1465
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001466 case oRevokedHostKeys:
1467 charptr = &options->revoked_host_keys;
1468 goto parse_string;
1469
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001470 case oFingerprintHash:
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001471 intptr = &options->fingerprint_hash;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001472 arg = strdelim(&s);
1473 if (!arg || *arg == '\0')
1474 fatal("%.200s line %d: Missing argument.",
1475 filename, linenum);
1476 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1477 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1478 filename, linenum, arg);
djm@openbsd.orge752c6d2015-01-08 13:44:36 +00001479 if (*activep && *intptr == -1)
1480 *intptr = value;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001481 break;
1482
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001483 case oUpdateHostkeys:
1484 intptr = &options->update_hostkeys;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001485 multistate_ptr = multistate_yesnoask;
1486 goto parse_multistate;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001487
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001488 case oHostbasedKeyTypes:
1489 charptr = &options->hostbased_key_types;
1490 arg = strdelim(&s);
1491 if (!arg || *arg == '\0')
1492 fatal("%.200s line %d: Missing argument.",
1493 filename, linenum);
1494 if (!sshkey_names_valid2(arg, 1))
1495 fatal("%s line %d: Bad key types '%s'.",
1496 filename, linenum, arg ? arg : "<NONE>");
1497 if (*activep && *charptr == NULL)
1498 *charptr = xstrdup(arg);
1499 break;
1500
Ben Lindstrom4daea862002-06-09 20:04:02 +00001501 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001502 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001503 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001504 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001505
Damien Millerf9b3feb2003-05-16 11:38:32 +10001506 case oUnsupported:
1507 error("%s line %d: Unsupported option \"%s\"",
1508 filename, linenum, keyword);
1509 return 0;
1510
Damien Miller95def091999-11-25 00:26:21 +11001511 default:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001512 fatal("%s: Unimplemented opcode %d", __func__, opcode);
Damien Miller95def091999-11-25 00:26:21 +11001513 }
1514
1515 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001516 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001517 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001518 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001519 }
Damien Miller95def091999-11-25 00:26:21 +11001520 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001521}
1522
1523
Damien Miller5428f641999-11-25 11:54:57 +11001524/*
1525 * Reads the config file and modifies the options accordingly. Options
1526 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001527 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001528 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001530int
Damien Miller194fd902013-10-15 12:13:05 +11001531read_config_file(const char *filename, struct passwd *pw, const char *host,
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001532 const char *original_host, Options *options, int flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533{
Damien Miller95def091999-11-25 00:26:21 +11001534 FILE *f;
1535 char line[1024];
1536 int active, linenum;
1537 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001538
Damien Miller57a44762004-04-20 20:11:57 +10001539 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001540 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001541
Darren Tuckeraefa3682013-04-05 11:18:35 +11001542 if (flags & SSHCONF_CHECKPERM) {
Damien Miller57a44762004-04-20 20:11:57 +10001543 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001544
Damien Miller33793852004-06-15 10:27:55 +10001545 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001546 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001547 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001548 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001549 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001550 }
1551
Damien Miller95def091999-11-25 00:26:21 +11001552 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553
Damien Miller5428f641999-11-25 11:54:57 +11001554 /*
1555 * Mark that we are now processing the options. This flag is turned
1556 * on/off by Host specifications.
1557 */
Damien Miller95def091999-11-25 00:26:21 +11001558 active = 1;
1559 linenum = 0;
1560 while (fgets(line, sizeof(line), f)) {
1561 /* Update line number counter. */
1562 linenum++;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001563 if (process_config_line(options, pw, host, original_host,
1564 line, filename, linenum, &active, flags) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001565 bad_options++;
1566 }
1567 fclose(f);
1568 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001569 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001570 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001571 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001572}
1573
Damien Miller13f97b22014-02-24 15:57:55 +11001574/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
1575int
1576option_clear_or_none(const char *o)
1577{
1578 return o == NULL || strcasecmp(o, "none") == 0;
1579}
1580
Damien Miller5428f641999-11-25 11:54:57 +11001581/*
1582 * Initializes options to special values that indicate that they have not yet
1583 * been set. Read_config_file will only set options with this value. Options
1584 * are processed in the following order: command line, user config file,
1585 * system config file. Last, fill_default_options is called.
1586 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001587
Damien Miller4af51302000-04-16 11:18:38 +10001588void
Damien Miller95def091999-11-25 00:26:21 +11001589initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001590{
Damien Miller95def091999-11-25 00:26:21 +11001591 memset(options, 'X', sizeof(*options));
1592 options->forward_agent = -1;
1593 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001594 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001595 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001596 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001597 options->xauth_location = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10001598 options->fwd_opts.gateway_ports = -1;
1599 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
1600 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Miller95def091999-11-25 00:26:21 +11001601 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001602 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001603 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001604 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001605 options->gss_authentication = -1;
1606 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001607 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001608 options->kbd_interactive_authentication = -1;
1609 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001610 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001611 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001612 options->batch_mode = -1;
1613 options->check_host_ip = -1;
1614 options->strict_host_key_checking = -1;
1615 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001616 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001617 options->compression_level = -1;
1618 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001619 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001620 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001621 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001622 options->number_of_password_prompts = -1;
1623 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001624 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001625 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001626 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001627 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001628 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001629 options->num_identity_files = 0;
1630 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001631 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001632 options->proxy_command = NULL;
1633 options->user = NULL;
1634 options->escape_char = -1;
Damien Miller295ee632011-05-29 21:42:31 +10001635 options->num_system_hostfiles = 0;
1636 options->num_user_hostfiles = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001637 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001638 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001639 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001640 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001641 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001642 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001643 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001644 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001645 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001646 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001647 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001648 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001649 options->rekey_limit = - 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001650 options->rekey_interval = -1;
Damien Miller37876e92003-05-15 10:19:46 +10001651 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001652 options->server_alive_interval = -1;
1653 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001654 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001655 options->control_path = NULL;
1656 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001657 options->control_persist = -1;
1658 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001659 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001660 options->tun_open = -1;
1661 options->tun_local = -1;
1662 options->tun_remote = -1;
1663 options->local_command = NULL;
1664 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001665 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001666 options->visual_host_key = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001667 options->ip_qos_interactive = -1;
1668 options->ip_qos_bulk = -1;
Damien Miller21771e22011-05-15 08:45:50 +10001669 options->request_tty = -1;
Damien Miller1262b662013-08-21 02:44:24 +10001670 options->proxy_use_fdpass = -1;
Darren Tucker07636982013-05-16 20:30:03 +10001671 options->ignored_unknown = NULL;
Damien Miller0faf7472013-10-17 11:47:23 +11001672 options->num_canonical_domains = 0;
1673 options->num_permitted_cnames = 0;
Damien Miller38505592013-10-17 11:48:13 +11001674 options->canonicalize_max_dots = -1;
1675 options->canonicalize_fallback_local = -1;
1676 options->canonicalize_hostname = -1;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001677 options->revoked_host_keys = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001678 options->fingerprint_hash = -1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001679 options->update_hostkeys = -1;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001680 options->hostbased_key_types = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001681}
1682
Damien Miller5428f641999-11-25 11:54:57 +11001683/*
Damien Miller13f97b22014-02-24 15:57:55 +11001684 * A petite version of fill_default_options() that just fills the options
1685 * needed for hostname canonicalization to proceed.
1686 */
1687void
1688fill_default_options_for_canonicalization(Options *options)
1689{
1690 if (options->canonicalize_max_dots == -1)
1691 options->canonicalize_max_dots = 1;
1692 if (options->canonicalize_fallback_local == -1)
1693 options->canonicalize_fallback_local = 1;
1694 if (options->canonicalize_hostname == -1)
1695 options->canonicalize_hostname = SSH_CANONICALISE_NO;
1696}
1697
1698/*
Damien Miller5428f641999-11-25 11:54:57 +11001699 * Called after processing other sources of option data, this fills those
1700 * options for which no value has been specified with their default values.
1701 */
Damien Miller4af51302000-04-16 11:18:38 +10001702void
Damien Miller95def091999-11-25 00:26:21 +11001703fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001704{
Damien Miller95def091999-11-25 00:26:21 +11001705 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001706 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001707 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001708 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001709 if (options->forward_x11_trusted == -1)
1710 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001711 if (options->forward_x11_timeout == -1)
1712 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001713 if (options->exit_on_forward_failure == -1)
1714 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001715 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001716 options->xauth_location = _PATH_XAUTH;
Damien Miller7acefbb2014-07-18 14:11:24 +10001717 if (options->fwd_opts.gateway_ports == -1)
1718 options->fwd_opts.gateway_ports = 0;
1719 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1720 options->fwd_opts.streamlocal_bind_mask = 0177;
1721 if (options->fwd_opts.streamlocal_bind_unlink == -1)
1722 options->fwd_opts.streamlocal_bind_unlink = 0;
Damien Miller95def091999-11-25 00:26:21 +11001723 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001724 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001725 if (options->rsa_authentication == -1)
1726 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001727 if (options->pubkey_authentication == -1)
1728 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001729 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001730 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001731 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001732 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001733 if (options->gss_deleg_creds == -1)
1734 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001735 if (options->password_authentication == -1)
1736 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001737 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001738 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001739 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001740 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001741 if (options->hostbased_authentication == -1)
1742 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001743 if (options->batch_mode == -1)
1744 options->batch_mode = 0;
1745 if (options->check_host_ip == -1)
1746 options->check_host_ip = 1;
1747 if (options->strict_host_key_checking == -1)
1748 options->strict_host_key_checking = 2; /* 2 is default */
1749 if (options->compression == -1)
1750 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001751 if (options->tcp_keep_alive == -1)
1752 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001753 if (options->compression_level == -1)
1754 options->compression_level = 6;
1755 if (options->port == -1)
1756 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001757 if (options->address_family == -1)
1758 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001759 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001760 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001761 if (options->number_of_password_prompts == -1)
1762 options->number_of_password_prompts = 3;
1763 /* Selected in ssh_login(). */
1764 if (options->cipher == -1)
1765 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001766 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001767 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001768 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001769 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001770 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001771 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001772 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001773 if (options->protocol & SSH_PROTO_1) {
Darren Tucker19104782013-04-05 11:13:08 +11001774 add_identity_file(options, "~/",
1775 _PATH_SSH_CLIENT_IDENTITY, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001776 }
1777 if (options->protocol & SSH_PROTO_2) {
Darren Tucker19104782013-04-05 11:13:08 +11001778 add_identity_file(options, "~/",
1779 _PATH_SSH_CLIENT_ID_RSA, 0);
1780 add_identity_file(options, "~/",
1781 _PATH_SSH_CLIENT_ID_DSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001782#ifdef OPENSSL_HAS_ECC
Darren Tucker19104782013-04-05 11:13:08 +11001783 add_identity_file(options, "~/",
1784 _PATH_SSH_CLIENT_ID_ECDSA, 0);
Damien Miller6af914a2010-09-10 11:39:26 +10001785#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +11001786 add_identity_file(options, "~/",
1787 _PATH_SSH_CLIENT_ID_ED25519, 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001788 }
Damien Millereba71ba2000-04-29 23:57:08 +10001789 }
Damien Miller95def091999-11-25 00:26:21 +11001790 if (options->escape_char == -1)
1791 options->escape_char = '~';
Damien Miller295ee632011-05-29 21:42:31 +10001792 if (options->num_system_hostfiles == 0) {
1793 options->system_hostfiles[options->num_system_hostfiles++] =
1794 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1795 options->system_hostfiles[options->num_system_hostfiles++] =
1796 xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1797 }
1798 if (options->num_user_hostfiles == 0) {
1799 options->user_hostfiles[options->num_user_hostfiles++] =
1800 xstrdup(_PATH_SSH_USER_HOSTFILE);
1801 options->user_hostfiles[options->num_user_hostfiles++] =
1802 xstrdup(_PATH_SSH_USER_HOSTFILE2);
1803 }
Damien Millerfcd93202002-02-05 12:26:34 +11001804 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001805 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001806 if (options->clear_forwardings == 1)
1807 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001808 if (options->no_host_authentication_for_localhost == - 1)
1809 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001810 if (options->identities_only == -1)
1811 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001812 if (options->enable_ssh_keysign == -1)
1813 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001814 if (options->rekey_limit == -1)
1815 options->rekey_limit = 0;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001816 if (options->rekey_interval == -1)
1817 options->rekey_interval = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001818 if (options->verify_host_key_dns == -1)
1819 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001820 if (options->server_alive_interval == -1)
1821 options->server_alive_interval = 0;
1822 if (options->server_alive_count_max == -1)
1823 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001824 if (options->control_master == -1)
1825 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001826 if (options->control_persist == -1) {
1827 options->control_persist = 0;
1828 options->control_persist_timeout = 0;
1829 }
Damien Millere1776152005-03-01 21:47:37 +11001830 if (options->hash_known_hosts == -1)
1831 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001832 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001833 options->tun_open = SSH_TUNMODE_NO;
1834 if (options->tun_local == -1)
1835 options->tun_local = SSH_TUNID_ANY;
1836 if (options->tun_remote == -1)
1837 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001838 if (options->permit_local_command == -1)
1839 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001840 if (options->use_roaming == -1)
1841 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001842 if (options->visual_host_key == -1)
1843 options->visual_host_key = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001844 if (options->ip_qos_interactive == -1)
1845 options->ip_qos_interactive = IPTOS_LOWDELAY;
1846 if (options->ip_qos_bulk == -1)
1847 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller21771e22011-05-15 08:45:50 +10001848 if (options->request_tty == -1)
1849 options->request_tty = REQUEST_TTY_AUTO;
Damien Miller1262b662013-08-21 02:44:24 +10001850 if (options->proxy_use_fdpass == -1)
1851 options->proxy_use_fdpass = 0;
Damien Miller38505592013-10-17 11:48:13 +11001852 if (options->canonicalize_max_dots == -1)
1853 options->canonicalize_max_dots = 1;
1854 if (options->canonicalize_fallback_local == -1)
1855 options->canonicalize_fallback_local = 1;
1856 if (options->canonicalize_hostname == -1)
1857 options->canonicalize_hostname = SSH_CANONICALISE_NO;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001858 if (options->fingerprint_hash == -1)
1859 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001860 if (options->update_hostkeys == -1)
djm@openbsd.org15ad7502015-02-02 07:41:40 +00001861 options->update_hostkeys = 0;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001862 if (options->hostbased_key_types == NULL)
1863 options->hostbased_key_types = xstrdup("*");
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001864
Damien Millere9fc72e2013-10-15 12:14:12 +11001865#define CLEAR_ON_NONE(v) \
1866 do { \
Damien Miller13f97b22014-02-24 15:57:55 +11001867 if (option_clear_or_none(v)) { \
Damien Millere9fc72e2013-10-15 12:14:12 +11001868 free(v); \
1869 v = NULL; \
1870 } \
1871 } while(0)
1872 CLEAR_ON_NONE(options->local_command);
1873 CLEAR_ON_NONE(options->proxy_command);
1874 CLEAR_ON_NONE(options->control_path);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001875 CLEAR_ON_NONE(options->revoked_host_keys);
Damien Miller95def091999-11-25 00:26:21 +11001876 /* options->user will be set in the main program if appropriate */
1877 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001878 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001879 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001880}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001881
Damien Miller7acefbb2014-07-18 14:11:24 +10001882struct fwdarg {
1883 char *arg;
1884 int ispath;
1885};
1886
1887/*
1888 * parse_fwd_field
1889 * parses the next field in a port forwarding specification.
1890 * sets fwd to the parsed field and advances p past the colon
1891 * or sets it to NULL at end of string.
1892 * returns 0 on success, else non-zero.
1893 */
1894static int
1895parse_fwd_field(char **p, struct fwdarg *fwd)
1896{
1897 char *ep, *cp = *p;
1898 int ispath = 0;
1899
1900 if (*cp == '\0') {
1901 *p = NULL;
1902 return -1; /* end of string */
1903 }
1904
1905 /*
1906 * A field escaped with square brackets is used literally.
1907 * XXX - allow ']' to be escaped via backslash?
1908 */
1909 if (*cp == '[') {
1910 /* find matching ']' */
1911 for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
1912 if (*ep == '/')
1913 ispath = 1;
1914 }
1915 /* no matching ']' or not at end of field. */
1916 if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
1917 return -1;
1918 /* NUL terminate the field and advance p past the colon */
1919 *ep++ = '\0';
1920 if (*ep != '\0')
1921 *ep++ = '\0';
1922 fwd->arg = cp + 1;
1923 fwd->ispath = ispath;
1924 *p = ep;
1925 return 0;
1926 }
1927
1928 for (cp = *p; *cp != '\0'; cp++) {
1929 switch (*cp) {
1930 case '\\':
1931 memmove(cp, cp + 1, strlen(cp + 1) + 1);
1932 cp++;
1933 break;
1934 case '/':
1935 ispath = 1;
1936 break;
1937 case ':':
1938 *cp++ = '\0';
1939 goto done;
1940 }
1941 }
1942done:
1943 fwd->arg = *p;
1944 fwd->ispath = ispath;
1945 *p = cp;
1946 return 0;
1947}
1948
Damien Millerf91ee4c2005-03-01 21:24:33 +11001949/*
1950 * parse_forward
1951 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001952 * dynamicfwd == 0
Damien Miller7acefbb2014-07-18 14:11:24 +10001953 * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
1954 * listenpath:connectpath
Damien Millera699d952008-11-03 19:27:34 +11001955 * dynamicfwd == 1
1956 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001957 * returns number of arguments parsed or zero on error
1958 */
1959int
Damien Miller7acefbb2014-07-18 14:11:24 +10001960parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001961{
Damien Miller7acefbb2014-07-18 14:11:24 +10001962 struct fwdarg fwdargs[4];
1963 char *p, *cp;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001964 int i;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001965
Damien Miller7acefbb2014-07-18 14:11:24 +10001966 memset(fwd, 0, sizeof(*fwd));
1967 memset(fwdargs, 0, sizeof(fwdargs));
Damien Millerf91ee4c2005-03-01 21:24:33 +11001968
1969 cp = p = xstrdup(fwdspec);
1970
1971 /* skip leading spaces */
Damien Millerfdb23062013-11-21 13:57:15 +11001972 while (isspace((u_char)*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001973 cp++;
1974
Damien Miller7acefbb2014-07-18 14:11:24 +10001975 for (i = 0; i < 4; ++i) {
1976 if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001977 break;
Damien Miller7acefbb2014-07-18 14:11:24 +10001978 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001979
Damien Millerf4b39532008-11-03 19:28:21 +11001980 /* Check for trailing garbage */
Damien Miller7acefbb2014-07-18 14:11:24 +10001981 if (cp != NULL && *cp != '\0') {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001982 i = 0; /* failure */
Damien Miller7acefbb2014-07-18 14:11:24 +10001983 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001984
1985 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001986 case 1:
Damien Miller7acefbb2014-07-18 14:11:24 +10001987 if (fwdargs[0].ispath) {
1988 fwd->listen_path = xstrdup(fwdargs[0].arg);
1989 fwd->listen_port = PORT_STREAMLOCAL;
1990 } else {
1991 fwd->listen_host = NULL;
1992 fwd->listen_port = a2port(fwdargs[0].arg);
1993 }
Damien Millera699d952008-11-03 19:27:34 +11001994 fwd->connect_host = xstrdup("socks");
1995 break;
1996
1997 case 2:
Damien Miller7acefbb2014-07-18 14:11:24 +10001998 if (fwdargs[0].ispath && fwdargs[1].ispath) {
1999 fwd->listen_path = xstrdup(fwdargs[0].arg);
2000 fwd->listen_port = PORT_STREAMLOCAL;
2001 fwd->connect_path = xstrdup(fwdargs[1].arg);
2002 fwd->connect_port = PORT_STREAMLOCAL;
2003 } else if (fwdargs[1].ispath) {
2004 fwd->listen_host = NULL;
2005 fwd->listen_port = a2port(fwdargs[0].arg);
2006 fwd->connect_path = xstrdup(fwdargs[1].arg);
2007 fwd->connect_port = PORT_STREAMLOCAL;
2008 } else {
2009 fwd->listen_host = xstrdup(fwdargs[0].arg);
2010 fwd->listen_port = a2port(fwdargs[1].arg);
2011 fwd->connect_host = xstrdup("socks");
2012 }
Damien Millera699d952008-11-03 19:27:34 +11002013 break;
2014
Damien Millerf91ee4c2005-03-01 21:24:33 +11002015 case 3:
Damien Miller7acefbb2014-07-18 14:11:24 +10002016 if (fwdargs[0].ispath) {
2017 fwd->listen_path = xstrdup(fwdargs[0].arg);
2018 fwd->listen_port = PORT_STREAMLOCAL;
2019 fwd->connect_host = xstrdup(fwdargs[1].arg);
2020 fwd->connect_port = a2port(fwdargs[2].arg);
2021 } else if (fwdargs[2].ispath) {
2022 fwd->listen_host = xstrdup(fwdargs[0].arg);
2023 fwd->listen_port = a2port(fwdargs[1].arg);
2024 fwd->connect_path = xstrdup(fwdargs[2].arg);
2025 fwd->connect_port = PORT_STREAMLOCAL;
2026 } else {
2027 fwd->listen_host = NULL;
2028 fwd->listen_port = a2port(fwdargs[0].arg);
2029 fwd->connect_host = xstrdup(fwdargs[1].arg);
2030 fwd->connect_port = a2port(fwdargs[2].arg);
2031 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11002032 break;
2033
2034 case 4:
Damien Miller7acefbb2014-07-18 14:11:24 +10002035 fwd->listen_host = xstrdup(fwdargs[0].arg);
2036 fwd->listen_port = a2port(fwdargs[1].arg);
2037 fwd->connect_host = xstrdup(fwdargs[2].arg);
2038 fwd->connect_port = a2port(fwdargs[3].arg);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002039 break;
2040 default:
2041 i = 0; /* failure */
2042 }
2043
Darren Tuckera627d422013-06-02 07:31:17 +10002044 free(p);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002045
Damien Millera699d952008-11-03 19:27:34 +11002046 if (dynamicfwd) {
2047 if (!(i == 1 || i == 2))
2048 goto fail_free;
2049 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002050 if (!(i == 3 || i == 4)) {
2051 if (fwd->connect_path == NULL &&
2052 fwd->listen_path == NULL)
2053 goto fail_free;
2054 }
2055 if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
Damien Millera699d952008-11-03 19:27:34 +11002056 goto fail_free;
2057 }
2058
Damien Miller7acefbb2014-07-18 14:11:24 +10002059 if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2060 (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11002061 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002062 if (fwd->connect_host != NULL &&
2063 strlen(fwd->connect_host) >= NI_MAXHOST)
2064 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002065 /* XXX - if connecting to a remote socket, max sun len may not match this host */
2066 if (fwd->connect_path != NULL &&
2067 strlen(fwd->connect_path) >= PATH_MAX_SUN)
2068 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11002069 if (fwd->listen_host != NULL &&
2070 strlen(fwd->listen_host) >= NI_MAXHOST)
2071 goto fail_free;
Damien Miller7acefbb2014-07-18 14:11:24 +10002072 if (fwd->listen_path != NULL &&
2073 strlen(fwd->listen_path) >= PATH_MAX_SUN)
2074 goto fail_free;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002075
2076 return (i);
2077
2078 fail_free:
Darren Tuckera627d422013-06-02 07:31:17 +10002079 free(fwd->connect_host);
2080 fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002081 free(fwd->connect_path);
2082 fwd->connect_path = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002083 free(fwd->listen_host);
2084 fwd->listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10002085 free(fwd->listen_path);
2086 fwd->listen_path = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002087 return (0);
2088}
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002089
2090/* XXX the following is a near-vebatim copy from servconf.c; refactor */
2091static const char *
2092fmt_multistate_int(int val, const struct multistate *m)
2093{
2094 u_int i;
2095
2096 for (i = 0; m[i].key != NULL; i++) {
2097 if (m[i].value == val)
2098 return m[i].key;
2099 }
2100 return "UNKNOWN";
2101}
2102
2103static const char *
2104fmt_intarg(OpCodes code, int val)
2105{
2106 if (val == -1)
2107 return "unset";
2108 switch (code) {
2109 case oAddressFamily:
2110 return fmt_multistate_int(val, multistate_addressfamily);
2111 case oVerifyHostKeyDNS:
2112 case oStrictHostKeyChecking:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002113 case oUpdateHostkeys:
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002114 return fmt_multistate_int(val, multistate_yesnoask);
2115 case oControlMaster:
2116 return fmt_multistate_int(val, multistate_controlmaster);
2117 case oTunnel:
2118 return fmt_multistate_int(val, multistate_tunnel);
2119 case oRequestTTY:
2120 return fmt_multistate_int(val, multistate_requesttty);
2121 case oCanonicalizeHostname:
2122 return fmt_multistate_int(val, multistate_canonicalizehostname);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002123 case oFingerprintHash:
2124 return ssh_digest_alg_name(val);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002125 case oProtocol:
2126 switch (val) {
2127 case SSH_PROTO_1:
2128 return "1";
2129 case SSH_PROTO_2:
2130 return "2";
2131 case (SSH_PROTO_1|SSH_PROTO_2):
2132 return "2,1";
2133 default:
2134 return "UNKNOWN";
2135 }
2136 default:
2137 switch (val) {
2138 case 0:
2139 return "no";
2140 case 1:
2141 return "yes";
2142 default:
2143 return "UNKNOWN";
2144 }
2145 }
2146}
2147
2148static const char *
2149lookup_opcode_name(OpCodes code)
2150{
2151 u_int i;
2152
2153 for (i = 0; keywords[i].name != NULL; i++)
2154 if (keywords[i].opcode == code)
2155 return(keywords[i].name);
2156 return "UNKNOWN";
2157}
2158
2159static void
2160dump_cfg_int(OpCodes code, int val)
2161{
2162 printf("%s %d\n", lookup_opcode_name(code), val);
2163}
2164
2165static void
2166dump_cfg_fmtint(OpCodes code, int val)
2167{
2168 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2169}
2170
2171static void
2172dump_cfg_string(OpCodes code, const char *val)
2173{
2174 if (val == NULL)
2175 return;
2176 printf("%s %s\n", lookup_opcode_name(code), val);
2177}
2178
2179static void
2180dump_cfg_strarray(OpCodes code, u_int count, char **vals)
2181{
2182 u_int i;
2183
2184 for (i = 0; i < count; i++)
2185 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2186}
2187
2188static void
2189dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
2190{
2191 u_int i;
2192
2193 printf("%s", lookup_opcode_name(code));
2194 for (i = 0; i < count; i++)
2195 printf(" %s", vals[i]);
2196 printf("\n");
2197}
2198
2199static void
2200dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
2201{
2202 const struct Forward *fwd;
2203 u_int i;
2204
2205 /* oDynamicForward */
2206 for (i = 0; i < count; i++) {
2207 fwd = &fwds[i];
2208 if (code == oDynamicForward &&
2209 strcmp(fwd->connect_host, "socks") != 0)
2210 continue;
2211 if (code == oLocalForward &&
2212 strcmp(fwd->connect_host, "socks") == 0)
2213 continue;
2214 printf("%s", lookup_opcode_name(code));
2215 if (fwd->listen_port == PORT_STREAMLOCAL)
2216 printf(" %s", fwd->listen_path);
2217 else if (fwd->listen_host == NULL)
2218 printf(" %d", fwd->listen_port);
2219 else {
2220 printf(" [%s]:%d",
2221 fwd->listen_host, fwd->listen_port);
2222 }
2223 if (code != oDynamicForward) {
2224 if (fwd->connect_port == PORT_STREAMLOCAL)
2225 printf(" %s", fwd->connect_path);
2226 else if (fwd->connect_host == NULL)
2227 printf(" %d", fwd->connect_port);
2228 else {
2229 printf(" [%s]:%d",
2230 fwd->connect_host, fwd->connect_port);
2231 }
2232 }
2233 printf("\n");
2234 }
2235}
2236
2237void
2238dump_client_config(Options *o, const char *host)
2239{
2240 int i;
2241 char vbuf[5];
2242
2243 /* Most interesting options first: user, host, port */
2244 dump_cfg_string(oUser, o->user);
2245 dump_cfg_string(oHostName, host);
2246 dump_cfg_int(oPort, o->port);
2247
2248 /* Flag options */
2249 dump_cfg_fmtint(oAddressFamily, o->address_family);
2250 dump_cfg_fmtint(oBatchMode, o->batch_mode);
2251 dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
2252 dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
2253 dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
2254 dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
2255 dump_cfg_fmtint(oCompression, o->compression);
2256 dump_cfg_fmtint(oControlMaster, o->control_master);
2257 dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
2258 dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002259 dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002260 dump_cfg_fmtint(oForwardAgent, o->forward_agent);
2261 dump_cfg_fmtint(oForwardX11, o->forward_x11);
2262 dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
2263 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
2264#ifdef GSSAPI
2265 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
2266 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
2267#endif /* GSSAPI */
2268 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
2269 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
2270 dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
2271 dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
2272 dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
2273 dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
2274 dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
2275 dump_cfg_fmtint(oProtocol, o->protocol);
2276 dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
2277 dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
2278 dump_cfg_fmtint(oRequestTTY, o->request_tty);
2279 dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2280 dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication);
2281 dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
2282 dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
2283 dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
2284 dump_cfg_fmtint(oTunnel, o->tun_open);
2285 dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port);
2286 dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
2287 dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002288 dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002289
2290 /* Integer options */
2291 dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
2292 dump_cfg_int(oCompressionLevel, o->compression_level);
2293 dump_cfg_int(oConnectionAttempts, o->connection_attempts);
2294 dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
2295 dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
2296 dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
2297 dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
2298
2299 /* String options */
2300 dump_cfg_string(oBindAddress, o->bind_address);
2301 dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT);
2302 dump_cfg_string(oControlPath, o->control_path);
2303 dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms ? o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
2304 dump_cfg_string(oHostKeyAlias, o->host_key_alias);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002305 dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002306 dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
2307 dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
2308 dump_cfg_string(oLocalCommand, o->local_command);
2309 dump_cfg_string(oLogLevel, log_level_name(o->log_level));
2310 dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
2311 dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
2312 dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
2313 dump_cfg_string(oProxyCommand, o->proxy_command);
djm@openbsd.org5e39a492014-12-04 02:24:32 +00002314 dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002315 dump_cfg_string(oXAuthLocation, o->xauth_location);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002316
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002317 /* Forwards */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00002318 dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
2319 dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
2320 dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
2321
2322 /* String array options */
2323 dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
2324 dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
2325 dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
2326 dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
2327 dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
2328
2329 /* Special cases */
2330
2331 /* oConnectTimeout */
2332 if (o->connection_timeout == -1)
2333 printf("connecttimeout none\n");
2334 else
2335 dump_cfg_int(oConnectTimeout, o->connection_timeout);
2336
2337 /* oTunnelDevice */
2338 printf("tunneldevice");
2339 if (o->tun_local == SSH_TUNID_ANY)
2340 printf(" any");
2341 else
2342 printf(" %d", o->tun_local);
2343 if (o->tun_remote == SSH_TUNID_ANY)
2344 printf(":any");
2345 else
2346 printf(":%d", o->tun_remote);
2347 printf("\n");
2348
2349 /* oCanonicalizePermittedCNAMEs */
2350 if ( o->num_permitted_cnames > 0) {
2351 printf("canonicalizePermittedcnames");
2352 for (i = 0; i < o->num_permitted_cnames; i++) {
2353 printf(" %s:%s", o->permitted_cnames[i].source_list,
2354 o->permitted_cnames[i].target_list);
2355 }
2356 printf("\n");
2357 }
2358
2359 /* oCipher */
2360 if (o->cipher != SSH_CIPHER_NOT_SET)
2361 printf("Cipher %s\n", cipher_name(o->cipher));
2362
2363 /* oControlPersist */
2364 if (o->control_persist == 0 || o->control_persist_timeout == 0)
2365 dump_cfg_fmtint(oControlPersist, o->control_persist);
2366 else
2367 dump_cfg_int(oControlPersist, o->control_persist_timeout);
2368
2369 /* oEscapeChar */
2370 if (o->escape_char == SSH_ESCAPECHAR_NONE)
2371 printf("escapechar none\n");
2372 else {
2373 vis(vbuf, o->escape_char, VIS_WHITE, 0);
2374 printf("escapechar %s\n", vbuf);
2375 }
2376
2377 /* oIPQoS */
2378 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2379 printf("%s\n", iptos2str(o->ip_qos_bulk));
2380
2381 /* oRekeyLimit */
2382 printf("rekeylimit %lld %d\n",
2383 (long long)o->rekey_limit, o->rekey_interval);
2384
2385 /* oStreamLocalBindMask */
2386 printf("streamlocalbindmask 0%o\n",
2387 o->fwd_opts.streamlocal_bind_mask);
2388}