blob: eb4a8b9eea6a6159bef5d74faebc545d035eff1b [file] [log] [blame]
Damien Miller0dac6fb2010-11-20 15:19:38 +11001/* $OpenBSD: readconf.c,v 1.190 2010/11/13 23:27:50 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for reading the configuration files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
18#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100019#include <sys/socket.h>
20
21#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110022#include <netinet/in_systm.h>
23#include <netinet/ip.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
Damien Millerc7b06362006-03-15 11:53:45 +110025#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100027#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100028#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100029#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100030#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100032#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110033
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100035#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100036#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "cipher.h"
38#include "pathnames.h"
39#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100040#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "readconf.h"
42#include "match.h"
43#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100044#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000045#include "kex.h"
46#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48/* Format of the configuration file:
49
50 # Configuration data is parsed as follows:
51 # 1. command line options
52 # 2. user-specific file
53 # 3. system-wide file
54 # Any configuration value is only changed the first time it is set.
55 # Thus, host-specific definitions should be at the beginning of the
56 # configuration file, and defaults at the end.
57
58 # Host-specific declarations. These may override anything above. A single
59 # host may match multiple declarations; these are processed in the order
60 # that they are given in.
61
62 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000063 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65 Host fake.com
66 HostName another.host.name.real.org
67 User blaah
68 Port 34289
69 ForwardX11 no
70 ForwardAgent no
71
72 Host books.com
73 RemoteForward 9999 shadows.cs.hut.fi:9999
74 Cipher 3des
75
76 Host fascist.blob.com
77 Port 23123
78 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079 PasswordAuthentication no
80
81 Host puukko.hut.fi
82 User t35124p
83 ProxyCommand ssh-proxy %h %p
84
85 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000086 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88 Host *.su
89 Cipher none
90 PasswordAuthentication no
91
Damien Millerd27b9472005-12-13 19:29:02 +110092 Host vpn.fake.com
93 Tunnel yes
94 TunnelDevice 3
95
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 # Defaults for various options
97 Host *
98 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110099 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100 PasswordAuthentication yes
101 RSAAuthentication yes
102 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100104 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105 IdentityFile ~/.ssh/identity
106 Port 22
107 EscapeChar ~
108
109*/
110
111/* Keyword tokens. */
112
Damien Miller95def091999-11-25 00:26:21 +1100113typedef enum {
114 oBadOption,
Damien Miller1ab6a512010-06-26 10:02:24 +1000115 oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
116 oGatewayPorts, oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000117 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000118 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100119 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
120 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
121 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
122 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100123 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000124 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100125 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000126 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000127 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Damien Miller7ea845e2010-02-12 09:21:02 +1100128 oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000129 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000130 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000131 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100132 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere11e1ea2010-08-03 16:04:46 +1000133 oSendEnv, oControlPath, oControlMaster, oControlPersist,
134 oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100135 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100136 oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100137 oKexAlgorithms, oIPQoS,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100138 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139} OpCodes;
140
141/* Textual representations of the tokens. */
142
Damien Miller95def091999-11-25 00:26:21 +1100143static struct {
144 const char *name;
145 OpCodes opcode;
146} keywords[] = {
147 { "forwardagent", oForwardAgent },
148 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000149 { "forwardx11trusted", oForwardX11Trusted },
Damien Miller1ab6a512010-06-26 10:02:24 +1000150 { "forwardx11timeout", oForwardX11Timeout },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000151 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000152 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100153 { "gatewayports", oGatewayPorts },
154 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000155 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100156 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100157 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
158 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100159 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000161 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000162 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000163 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000164 { "challengeresponseauthentication", oChallengeResponseAuthentication },
165 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
166 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000167 { "kerberosauthentication", oUnsupported },
168 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000169 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000170#if defined(GSSAPI)
171 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000172 { "gssapidelegatecredentials", oGssDelegateCreds },
173#else
174 { "gssapiauthentication", oUnsupported },
175 { "gssapidelegatecredentials", oUnsupported },
176#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000177 { "fallbacktorsh", oDeprecated },
178 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "identityfile", oIdentityFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100180 { "identityfile2", oIdentityFile }, /* obsolete */
Damien Millerbd394c32004-03-08 23:12:36 +1100181 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100182 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000183 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100184 { "proxycommand", oProxyCommand },
185 { "port", oPort },
186 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000187 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000188 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000189 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100190 { "remoteforward", oRemoteForward },
191 { "localforward", oLocalForward },
192 { "user", oUser },
193 { "host", oHost },
194 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100195 { "globalknownhostsfile", oGlobalKnownHostsFile },
Damien Miller5bc6aae2009-01-28 16:27:31 +1100196 { "globalknownhostsfile2", oGlobalKnownHostsFile2 }, /* obsolete */
197 { "userknownhostsfile", oUserKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000198 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100199 { "connectionattempts", oConnectionAttempts },
200 { "batchmode", oBatchMode },
201 { "checkhostip", oCheckHostIP },
202 { "stricthostkeychecking", oStrictHostKeyChecking },
203 { "compression", oCompression },
204 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100205 { "tcpkeepalive", oTCPKeepAlive },
206 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100207 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100208 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000209 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000210 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000211 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000212 { "bindaddress", oBindAddress },
Damien Miller7ea845e2010-02-12 09:21:02 +1100213#ifdef ENABLE_PKCS11
214 { "smartcarddevice", oPKCS11Provider },
215 { "pkcs11provider", oPKCS11Provider },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000216#else
217 { "smartcarddevice", oUnsupported },
Damien Miller7ea845e2010-02-12 09:21:02 +1100218 { "pkcs11provider", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000219#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100220 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000221 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000222 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000224 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000225 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000226 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100227 { "serveraliveinterval", oServerAliveInterval },
228 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000229 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000230 { "controlpath", oControlPath },
231 { "controlmaster", oControlMaster },
Damien Millere11e1ea2010-08-03 16:04:46 +1000232 { "controlpersist", oControlPersist },
Damien Millere1776152005-03-01 21:47:37 +1100233 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100234 { "tunnel", oTunnel },
235 { "tunneldevice", oTunnelDevice },
236 { "localcommand", oLocalCommand },
237 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000238 { "visualhostkey", oVisualHostKey },
Darren Tucker71e4d542009-07-06 07:12:27 +1000239 { "useroaming", oUseRoaming },
Damien Miller01ed2272008-11-05 16:20:46 +1100240#ifdef JPAKE
241 { "zeroknowledgepasswordauthentication",
242 oZeroKnowledgePasswordAuthentication },
243#else
244 { "zeroknowledgepasswordauthentication", oUnsupported },
245#endif
Damien Millerd5f62bf2010-09-24 22:11:14 +1000246 { "kexalgorithms", oKexAlgorithms },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100247 { "ipqos", oIPQoS },
Damien Miller01ed2272008-11-05 16:20:46 +1100248
Ben Lindstrom65366a82001-12-06 16:32:47 +0000249 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100250};
251
Damien Miller5428f641999-11-25 11:54:57 +1100252/*
253 * Adds a local TCP/IP port forward to options. Never returns if there is an
254 * error.
255 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller4af51302000-04-16 11:18:38 +1000257void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100258add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259{
Damien Miller95def091999-11-25 00:26:21 +1100260 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000261#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100262 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000264 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100265#endif
Damien Miller232cfb12010-06-26 09:50:30 +1000266 options->local_forwards = xrealloc(options->local_forwards,
267 options->num_local_forwards + 1,
268 sizeof(*options->local_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100269 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100270
Damien Miller1a0442f2008-11-05 16:30:06 +1100271 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100272 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100273 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100274 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275}
276
Damien Miller5428f641999-11-25 11:54:57 +1100277/*
278 * Adds a remote TCP/IP port forward to options. Never returns if there is
279 * an error.
280 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller4af51302000-04-16 11:18:38 +1000282void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284{
Damien Miller95def091999-11-25 00:26:21 +1100285 Forward *fwd;
Damien Miller232cfb12010-06-26 09:50:30 +1000286
287 options->remote_forwards = xrealloc(options->remote_forwards,
288 options->num_remote_forwards + 1,
289 sizeof(*options->remote_forwards));
Damien Miller95def091999-11-25 00:26:21 +1100290 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291
Damien Miller1a0442f2008-11-05 16:30:06 +1100292 fwd->listen_host = newfwd->listen_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100293 fwd->listen_port = newfwd->listen_port;
Damien Miller1a0442f2008-11-05 16:30:06 +1100294 fwd->connect_host = newfwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100295 fwd->connect_port = newfwd->connect_port;
Damien Miller388f6fc2010-05-21 14:57:35 +1000296 fwd->allocated_port = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297}
298
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000299static void
300clear_forwardings(Options *options)
301{
302 int i;
303
Damien Millerf91ee4c2005-03-01 21:24:33 +1100304 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100305 if (options->local_forwards[i].listen_host != NULL)
306 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100307 xfree(options->local_forwards[i].connect_host);
308 }
Damien Miller232cfb12010-06-26 09:50:30 +1000309 if (options->num_local_forwards > 0) {
310 xfree(options->local_forwards);
311 options->local_forwards = NULL;
312 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000313 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100314 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100315 if (options->remote_forwards[i].listen_host != NULL)
316 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100317 xfree(options->remote_forwards[i].connect_host);
318 }
Damien Miller232cfb12010-06-26 09:50:30 +1000319 if (options->num_remote_forwards > 0) {
320 xfree(options->remote_forwards);
321 options->remote_forwards = NULL;
322 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000323 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100324 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000325}
326
Damien Miller5428f641999-11-25 11:54:57 +1100327/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000328 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100329 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller4af51302000-04-16 11:18:38 +1000331static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100332parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000334 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Miller95def091999-11-25 00:26:21 +1100336 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100337 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100338 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000340 error("%s: line %d: Bad configuration option: %s",
341 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100342 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343}
344
Damien Miller5428f641999-11-25 11:54:57 +1100345/*
346 * Processes a single option line as used in the configuration files. This
347 * only sets those values that have not already been set.
348 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100349#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller2ccf6611999-11-15 15:25:10 +1100351int
352process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100353 char *line, const char *filename, int linenum,
354 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100356 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100357 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100358 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100359 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100360 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100361 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Millerc652cac2003-05-14 13:40:54 +1000363 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100364 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000365 if (strchr(WHITESPACE, line[len]) == NULL)
366 break;
367 line[len] = '\0';
368 }
369
Damien Millerbe484b52000-07-15 14:14:16 +1000370 s = line;
371 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100372 if ((keyword = strdelim(&s)) == NULL)
373 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000374 /* Ignore leading whitespace. */
375 if (*keyword == '\0')
376 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000377 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100378 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Damien Miller37023962000-07-11 17:31:38 +1000380 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381
Damien Miller95def091999-11-25 00:26:21 +1100382 switch (opcode) {
383 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100384 /* don't panic, but count bad options */
385 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100386 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000387 case oConnectTimeout:
388 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100389parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000390 arg = strdelim(&s);
391 if (!arg || *arg == '\0')
392 fatal("%s line %d: missing time value.",
393 filename, linenum);
394 if ((value = convtime(arg)) == -1)
395 fatal("%s line %d: invalid time value.",
396 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100397 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000398 *intptr = value;
399 break;
400
Damien Miller95def091999-11-25 00:26:21 +1100401 case oForwardAgent:
402 intptr = &options->forward_agent;
403parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000404 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000405 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100406 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
407 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000408 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100409 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000410 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100411 value = 0;
412 else
413 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
414 if (*activep && *intptr == -1)
415 *intptr = value;
416 break;
417
418 case oForwardX11:
419 intptr = &options->forward_x11;
420 goto parse_flag;
421
Darren Tucker0a118da2003-10-15 15:54:32 +1000422 case oForwardX11Trusted:
423 intptr = &options->forward_x11_trusted;
424 goto parse_flag;
Damien Miller1ab6a512010-06-26 10:02:24 +1000425
426 case oForwardX11Timeout:
427 intptr = &options->forward_x11_timeout;
428 goto parse_time;
Darren Tucker0a118da2003-10-15 15:54:32 +1000429
Damien Miller95def091999-11-25 00:26:21 +1100430 case oGatewayPorts:
431 intptr = &options->gateway_ports;
432 goto parse_flag;
433
Darren Tuckere7d4b192006-07-12 22:17:10 +1000434 case oExitOnForwardFailure:
435 intptr = &options->exit_on_forward_failure;
436 goto parse_flag;
437
Damien Miller95def091999-11-25 00:26:21 +1100438 case oUsePrivilegedPort:
439 intptr = &options->use_privileged_port;
440 goto parse_flag;
441
Damien Miller95def091999-11-25 00:26:21 +1100442 case oPasswordAuthentication:
443 intptr = &options->password_authentication;
444 goto parse_flag;
445
Damien Miller01ed2272008-11-05 16:20:46 +1100446 case oZeroKnowledgePasswordAuthentication:
447 intptr = &options->zero_knowledge_password_authentication;
448 goto parse_flag;
449
Damien Miller874d77b2000-10-14 16:23:11 +1100450 case oKbdInteractiveAuthentication:
451 intptr = &options->kbd_interactive_authentication;
452 goto parse_flag;
453
454 case oKbdInteractiveDevices:
455 charptr = &options->kbd_interactive_devices;
456 goto parse_string;
457
Damien Miller0bc1bd82000-11-13 22:57:25 +1100458 case oPubkeyAuthentication:
459 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000460 goto parse_flag;
461
Damien Miller95def091999-11-25 00:26:21 +1100462 case oRSAAuthentication:
463 intptr = &options->rsa_authentication;
464 goto parse_flag;
465
466 case oRhostsRSAAuthentication:
467 intptr = &options->rhosts_rsa_authentication;
468 goto parse_flag;
469
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000470 case oHostbasedAuthentication:
471 intptr = &options->hostbased_authentication;
472 goto parse_flag;
473
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000474 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000475 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100476 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000477
Darren Tucker0efd1552003-08-26 11:49:55 +1000478 case oGssAuthentication:
479 intptr = &options->gss_authentication;
480 goto parse_flag;
481
482 case oGssDelegateCreds:
483 intptr = &options->gss_deleg_creds;
484 goto parse_flag;
485
Damien Miller95def091999-11-25 00:26:21 +1100486 case oBatchMode:
487 intptr = &options->batch_mode;
488 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
Damien Miller95def091999-11-25 00:26:21 +1100490 case oCheckHostIP:
491 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000492 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
Damien Miller37876e92003-05-15 10:19:46 +1000494 case oVerifyHostKeyDNS:
495 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100496 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000497
Damien Miller95def091999-11-25 00:26:21 +1100498 case oStrictHostKeyChecking:
499 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100500parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000501 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000502 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000503 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100504 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100505 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000506 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100507 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000508 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100509 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000510 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100511 value = 2;
512 else
513 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
514 if (*activep && *intptr == -1)
515 *intptr = value;
516 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller95def091999-11-25 00:26:21 +1100518 case oCompression:
519 intptr = &options->compression;
520 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller12c150e2003-12-17 16:31:10 +1100522 case oTCPKeepAlive:
523 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100524 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000526 case oNoHostAuthenticationForLocalhost:
527 intptr = &options->no_host_authentication_for_localhost;
528 goto parse_flag;
529
Damien Miller95def091999-11-25 00:26:21 +1100530 case oNumberOfPasswordPrompts:
531 intptr = &options->number_of_password_prompts;
532 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller95def091999-11-25 00:26:21 +1100534 case oCompressionLevel:
535 intptr = &options->compression_level;
536 goto parse_int;
537
Damien Millera5539d22003-04-09 20:50:06 +1000538 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000539 arg = strdelim(&s);
540 if (!arg || *arg == '\0')
541 fatal("%.200s line %d: Missing argument.", filename, linenum);
542 if (arg[0] < '0' || arg[0] > '9')
543 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100544 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000545 if (arg == endofnumber)
546 fatal("%.200s line %d: Bad number.", filename, linenum);
547 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100548 case '\0':
549 scale = 1;
550 break;
Damien Millera5539d22003-04-09 20:50:06 +1000551 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100552 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000553 break;
554 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100555 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000556 break;
557 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100558 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000559 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100560 default:
561 fatal("%.200s line %d: Invalid RekeyLimit suffix",
562 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000563 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100564 val64 *= scale;
565 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100566 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100567 fatal("%.200s line %d: RekeyLimit too large",
568 filename, linenum);
569 if (val64 < 16)
570 fatal("%.200s line %d: RekeyLimit too small",
571 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100572 if (*activep && options->rekey_limit == -1)
573 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000574 break;
575
Damien Miller95def091999-11-25 00:26:21 +1100576 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000577 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000578 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100579 fatal("%.200s line %d: Missing argument.", filename, linenum);
580 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100581 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000582 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100583 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100584 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100585 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000586 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000587 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100588 }
589 break;
590
Damien Millerd3a18572000-06-07 19:55:44 +1000591 case oXAuthLocation:
592 charptr=&options->xauth_location;
593 goto parse_string;
594
Damien Miller95def091999-11-25 00:26:21 +1100595 case oUser:
596 charptr = &options->user;
597parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000598 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000599 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100600 fatal("%.200s line %d: Missing argument.", filename, linenum);
601 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000602 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100603 break;
604
605 case oGlobalKnownHostsFile:
606 charptr = &options->system_hostfile;
607 goto parse_string;
608
609 case oUserKnownHostsFile:
610 charptr = &options->user_hostfile;
611 goto parse_string;
612
Damien Millereba71ba2000-04-29 23:57:08 +1000613 case oGlobalKnownHostsFile2:
614 charptr = &options->system_hostfile2;
615 goto parse_string;
616
617 case oUserKnownHostsFile2:
618 charptr = &options->user_hostfile2;
619 goto parse_string;
620
Damien Miller95def091999-11-25 00:26:21 +1100621 case oHostName:
622 charptr = &options->hostname;
623 goto parse_string;
624
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000625 case oHostKeyAlias:
626 charptr = &options->host_key_alias;
627 goto parse_string;
628
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000629 case oPreferredAuthentications:
630 charptr = &options->preferred_authentications;
631 goto parse_string;
632
Ben Lindstrome0f88042001-04-30 13:06:24 +0000633 case oBindAddress:
634 charptr = &options->bind_address;
635 goto parse_string;
636
Damien Miller7ea845e2010-02-12 09:21:02 +1100637 case oPKCS11Provider:
638 charptr = &options->pkcs11_provider;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000639 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000640
Damien Miller95def091999-11-25 00:26:21 +1100641 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100642 charptr = &options->proxy_command;
643parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000644 if (s == NULL)
645 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100646 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100647 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100648 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100649 return 0;
650
651 case oPort:
652 intptr = &options->port;
653parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000654 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000655 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100656 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000657 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100658 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100659
660 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000661 value = strtol(arg, &endofnumber, 0);
662 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100663 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100664 if (*activep && *intptr == -1)
665 *intptr = value;
666 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Miller95def091999-11-25 00:26:21 +1100668 case oConnectionAttempts:
669 intptr = &options->connection_attempts;
670 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100671
Damien Miller95def091999-11-25 00:26:21 +1100672 case oCipher:
673 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000674 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000675 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000676 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000677 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100678 if (value == -1)
679 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100680 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100681 if (*activep && *intptr == -1)
682 *intptr = value;
683 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Miller78928792000-04-12 20:17:38 +1000685 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000686 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000687 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000688 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000689 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000690 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100691 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000692 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000693 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000694 break;
695
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000696 case oMacs:
697 arg = strdelim(&s);
698 if (!arg || *arg == '\0')
699 fatal("%.200s line %d: Missing argument.", filename, linenum);
700 if (!mac_valid(arg))
701 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000703 if (*activep && options->macs == NULL)
704 options->macs = xstrdup(arg);
705 break;
706
Damien Millerd5f62bf2010-09-24 22:11:14 +1000707 case oKexAlgorithms:
708 arg = strdelim(&s);
709 if (!arg || *arg == '\0')
710 fatal("%.200s line %d: Missing argument.",
711 filename, linenum);
712 if (!kex_names_valid(arg))
713 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
714 filename, linenum, arg ? arg : "<NONE>");
715 if (*activep && options->kex_algorithms == NULL)
716 options->kex_algorithms = xstrdup(arg);
717 break;
718
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000719 case oHostKeyAlgorithms:
720 arg = strdelim(&s);
721 if (!arg || *arg == '\0')
722 fatal("%.200s line %d: Missing argument.", filename, linenum);
723 if (!key_names_valid2(arg))
724 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100725 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000726 if (*activep && options->hostkeyalgorithms == NULL)
727 options->hostkeyalgorithms = xstrdup(arg);
728 break;
729
Damien Miller78928792000-04-12 20:17:38 +1000730 case oProtocol:
731 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000732 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000733 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000734 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000735 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000736 if (value == SSH_PROTO_UNKNOWN)
737 fatal("%.200s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100738 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000739 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
740 *intptr = value;
741 break;
742
Damien Miller95def091999-11-25 00:26:21 +1100743 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100744 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000745 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000746 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100747 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000748 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100749 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100750 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
751 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100752 break;
753
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000754 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100755 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100756 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000757 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100758 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000759 fatal("%.200s line %d: Missing port argument.",
760 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100761
Damien Millera699d952008-11-03 19:27:34 +1100762 if (opcode == oLocalForward ||
763 opcode == oRemoteForward) {
764 arg2 = strdelim(&s);
765 if (arg2 == NULL || *arg2 == '\0')
766 fatal("%.200s line %d: Missing target argument.",
767 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100768
Damien Millera699d952008-11-03 19:27:34 +1100769 /* construct a string for parse_forward */
770 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
771 } else if (opcode == oDynamicForward) {
772 strlcpy(fwdarg, arg, sizeof(fwdarg));
773 }
774
775 if (parse_forward(&fwd, fwdarg,
Damien Miller4bf648f2009-02-14 16:28:21 +1100776 opcode == oDynamicForward ? 1 : 0,
777 opcode == oRemoteForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000778 fatal("%.200s line %d: Bad forwarding specification.",
779 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100780
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000781 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100782 if (opcode == oLocalForward ||
783 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100784 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000785 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100786 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000787 }
Damien Miller95def091999-11-25 00:26:21 +1100788 break;
789
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000790 case oClearAllForwardings:
791 intptr = &options->clear_forwardings;
792 goto parse_flag;
793
Damien Miller95def091999-11-25 00:26:21 +1100794 case oHost:
795 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000796 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000797 if (match_pattern(host, arg)) {
798 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100799 *activep = 1;
800 break;
801 }
Damien Millerbe484b52000-07-15 14:14:16 +1000802 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100803 return 0;
804
805 case oEscapeChar:
806 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000807 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000808 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100809 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000810 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000811 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
812 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000813 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000814 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000815 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000816 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100817 else {
818 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100819 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100820 /* NOTREACHED */
821 value = 0; /* Avoid compiler warning. */
822 }
823 if (*activep && *intptr == -1)
824 *intptr = value;
825 break;
826
Damien Miller20a8f972003-05-18 20:50:30 +1000827 case oAddressFamily:
828 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000829 if (!arg || *arg == '\0')
830 fatal("%s line %d: missing address family.",
831 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000832 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000833 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000834 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000835 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000836 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000837 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000838 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000839 else
840 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000841 if (*activep && *intptr == -1)
842 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000843 break;
844
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000845 case oEnableSSHKeysign:
846 intptr = &options->enable_ssh_keysign;
847 goto parse_flag;
848
Damien Millerbd394c32004-03-08 23:12:36 +1100849 case oIdentitiesOnly:
850 intptr = &options->identities_only;
851 goto parse_flag;
852
Damien Miller509b0102003-12-17 16:33:10 +1100853 case oServerAliveInterval:
854 intptr = &options->server_alive_interval;
855 goto parse_time;
856
857 case oServerAliveCountMax:
858 intptr = &options->server_alive_count_max;
859 goto parse_int;
860
Darren Tucker46bc0752004-05-02 22:11:30 +1000861 case oSendEnv:
862 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
863 if (strchr(arg, '=') != NULL)
864 fatal("%s line %d: Invalid environment name.",
865 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100866 if (!*activep)
867 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000868 if (options->num_send_env >= MAX_SEND_ENV)
869 fatal("%s line %d: too many send env.",
870 filename, linenum);
871 options->send_env[options->num_send_env++] =
872 xstrdup(arg);
873 }
874 break;
875
Damien Miller0e220db2004-06-15 10:34:08 +1000876 case oControlPath:
877 charptr = &options->control_path;
878 goto parse_string;
879
880 case oControlMaster:
881 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000882 arg = strdelim(&s);
883 if (!arg || *arg == '\0')
884 fatal("%.200s line %d: Missing ControlMaster argument.",
885 filename, linenum);
886 value = 0; /* To avoid compiler warning... */
887 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
888 value = SSHCTL_MASTER_YES;
889 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
890 value = SSHCTL_MASTER_NO;
891 else if (strcmp(arg, "auto") == 0)
892 value = SSHCTL_MASTER_AUTO;
893 else if (strcmp(arg, "ask") == 0)
894 value = SSHCTL_MASTER_ASK;
895 else if (strcmp(arg, "autoask") == 0)
896 value = SSHCTL_MASTER_AUTO_ASK;
897 else
898 fatal("%.200s line %d: Bad ControlMaster argument.",
899 filename, linenum);
900 if (*activep && *intptr == -1)
901 *intptr = value;
902 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000903
Damien Millere11e1ea2010-08-03 16:04:46 +1000904 case oControlPersist:
905 /* no/false/yes/true, or a time spec */
906 intptr = &options->control_persist;
907 arg = strdelim(&s);
908 if (!arg || *arg == '\0')
909 fatal("%.200s line %d: Missing ControlPersist"
910 " argument.", filename, linenum);
911 value = 0;
912 value2 = 0; /* timeout */
913 if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
914 value = 0;
915 else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
916 value = 1;
917 else if ((value2 = convtime(arg)) >= 0)
918 value = 1;
919 else
920 fatal("%.200s line %d: Bad ControlPersist argument.",
921 filename, linenum);
922 if (*activep && *intptr == -1) {
923 *intptr = value;
924 options->control_persist_timeout = value2;
925 }
926 break;
927
Damien Millere1776152005-03-01 21:47:37 +1100928 case oHashKnownHosts:
929 intptr = &options->hash_known_hosts;
930 goto parse_flag;
931
Damien Millerd27b9472005-12-13 19:29:02 +1100932 case oTunnel:
933 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100934 arg = strdelim(&s);
935 if (!arg || *arg == '\0')
936 fatal("%s line %d: Missing yes/point-to-point/"
937 "ethernet/no argument.", filename, linenum);
938 value = 0; /* silence compiler */
939 if (strcasecmp(arg, "ethernet") == 0)
940 value = SSH_TUNMODE_ETHERNET;
941 else if (strcasecmp(arg, "point-to-point") == 0)
942 value = SSH_TUNMODE_POINTOPOINT;
943 else if (strcasecmp(arg, "yes") == 0)
944 value = SSH_TUNMODE_DEFAULT;
945 else if (strcasecmp(arg, "no") == 0)
946 value = SSH_TUNMODE_NO;
947 else
948 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
949 "no argument: %s", filename, linenum, arg);
950 if (*activep)
951 *intptr = value;
952 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100953
954 case oTunnelDevice:
955 arg = strdelim(&s);
956 if (!arg || *arg == '\0')
957 fatal("%.200s line %d: Missing argument.", filename, linenum);
958 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100959 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100960 fatal("%.200s line %d: Bad tun device.", filename, linenum);
961 if (*activep) {
962 options->tun_local = value;
963 options->tun_remote = value2;
964 }
965 break;
966
967 case oLocalCommand:
968 charptr = &options->local_command;
969 goto parse_command;
970
971 case oPermitLocalCommand:
972 intptr = &options->permit_local_command;
973 goto parse_flag;
974
Damien Miller10288242008-06-30 00:04:03 +1000975 case oVisualHostKey:
976 intptr = &options->visual_host_key;
977 goto parse_flag;
978
Damien Miller0dac6fb2010-11-20 15:19:38 +1100979 case oIPQoS:
980 arg = strdelim(&s);
981 if ((value = parse_ipqos(arg)) == -1)
982 fatal("%s line %d: Bad IPQoS value: %s",
983 filename, linenum, arg);
984 arg = strdelim(&s);
985 if (arg == NULL)
986 value2 = value;
987 else if ((value2 = parse_ipqos(arg)) == -1)
988 fatal("%s line %d: Bad IPQoS value: %s",
989 filename, linenum, arg);
990 if (*activep) {
991 options->ip_qos_interactive = value;
992 options->ip_qos_bulk = value2;
993 }
994 break;
995
Darren Tucker71e4d542009-07-06 07:12:27 +1000996 case oUseRoaming:
997 intptr = &options->use_roaming;
998 goto parse_flag;
999
Ben Lindstrom4daea862002-06-09 20:04:02 +00001000 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001001 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +00001002 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +00001003 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +00001004
Damien Millerf9b3feb2003-05-16 11:38:32 +10001005 case oUnsupported:
1006 error("%s line %d: Unsupported option \"%s\"",
1007 filename, linenum, keyword);
1008 return 0;
1009
Damien Miller95def091999-11-25 00:26:21 +11001010 default:
1011 fatal("process_config_line: Unimplemented opcode %d", opcode);
1012 }
1013
1014 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001015 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +10001016 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +10001017 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +10001018 }
Damien Miller95def091999-11-25 00:26:21 +11001019 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020}
1021
1022
Damien Miller5428f641999-11-25 11:54:57 +11001023/*
1024 * Reads the config file and modifies the options accordingly. Options
1025 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001026 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +11001027 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001029int
Darren Tuckerfc959702004-07-17 16:12:08 +10001030read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +10001031 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032{
Damien Miller95def091999-11-25 00:26:21 +11001033 FILE *f;
1034 char line[1024];
1035 int active, linenum;
1036 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037
Damien Miller57a44762004-04-20 20:11:57 +10001038 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001039 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040
Damien Miller57a44762004-04-20 20:11:57 +10001041 if (checkperm) {
1042 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +10001043
Damien Miller33793852004-06-15 10:27:55 +10001044 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +10001045 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +10001046 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +10001047 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +10001048 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +10001049 }
1050
Damien Miller95def091999-11-25 00:26:21 +11001051 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052
Damien Miller5428f641999-11-25 11:54:57 +11001053 /*
1054 * Mark that we are now processing the options. This flag is turned
1055 * on/off by Host specifications.
1056 */
Damien Miller95def091999-11-25 00:26:21 +11001057 active = 1;
1058 linenum = 0;
1059 while (fgets(line, sizeof(line), f)) {
1060 /* Update line number counter. */
1061 linenum++;
1062 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
1063 bad_options++;
1064 }
1065 fclose(f);
1066 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +00001067 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001068 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +00001069 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070}
1071
Damien Miller5428f641999-11-25 11:54:57 +11001072/*
1073 * Initializes options to special values that indicate that they have not yet
1074 * been set. Read_config_file will only set options with this value. Options
1075 * are processed in the following order: command line, user config file,
1076 * system config file. Last, fill_default_options is called.
1077 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001078
Damien Miller4af51302000-04-16 11:18:38 +10001079void
Damien Miller95def091999-11-25 00:26:21 +11001080initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081{
Damien Miller95def091999-11-25 00:26:21 +11001082 memset(options, 'X', sizeof(*options));
1083 options->forward_agent = -1;
1084 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +10001085 options->forward_x11_trusted = -1;
Damien Miller1ab6a512010-06-26 10:02:24 +10001086 options->forward_x11_timeout = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001087 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +10001088 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001089 options->gateway_ports = -1;
1090 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +11001091 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001092 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001093 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001094 options->gss_authentication = -1;
1095 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001096 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001097 options->kbd_interactive_authentication = -1;
1098 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001099 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001100 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001101 options->batch_mode = -1;
1102 options->check_host_ip = -1;
1103 options->strict_host_key_checking = -1;
1104 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001105 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001106 options->compression_level = -1;
1107 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001108 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001109 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001110 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001111 options->number_of_password_prompts = -1;
1112 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001113 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001114 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +10001115 options->kex_algorithms = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001116 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001117 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001118 options->num_identity_files = 0;
1119 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001120 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001121 options->proxy_command = NULL;
1122 options->user = NULL;
1123 options->escape_char = -1;
1124 options->system_hostfile = NULL;
1125 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001126 options->system_hostfile2 = NULL;
1127 options->user_hostfile2 = NULL;
Damien Miller232cfb12010-06-26 09:50:30 +10001128 options->local_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001129 options->num_local_forwards = 0;
Damien Miller232cfb12010-06-26 09:50:30 +10001130 options->remote_forwards = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001131 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001132 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001133 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001134 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001135 options->bind_address = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11001136 options->pkcs11_provider = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001137 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001138 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001139 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001140 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001141 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001142 options->server_alive_interval = -1;
1143 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001144 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001145 options->control_path = NULL;
1146 options->control_master = -1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001147 options->control_persist = -1;
1148 options->control_persist_timeout = 0;
Damien Millere1776152005-03-01 21:47:37 +11001149 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001150 options->tun_open = -1;
1151 options->tun_local = -1;
1152 options->tun_remote = -1;
1153 options->local_command = NULL;
1154 options->permit_local_command = -1;
Darren Tucker71e4d542009-07-06 07:12:27 +10001155 options->use_roaming = -1;
Damien Miller10288242008-06-30 00:04:03 +10001156 options->visual_host_key = -1;
Damien Miller01ed2272008-11-05 16:20:46 +11001157 options->zero_knowledge_password_authentication = -1;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001158 options->ip_qos_interactive = -1;
1159 options->ip_qos_bulk = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160}
1161
Damien Miller5428f641999-11-25 11:54:57 +11001162/*
1163 * Called after processing other sources of option data, this fills those
1164 * options for which no value has been specified with their default values.
1165 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166
Damien Miller4af51302000-04-16 11:18:38 +10001167void
Damien Miller95def091999-11-25 00:26:21 +11001168fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001170 int len;
1171
Damien Miller95def091999-11-25 00:26:21 +11001172 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001173 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001174 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001175 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001176 if (options->forward_x11_trusted == -1)
1177 options->forward_x11_trusted = 0;
Damien Miller1ab6a512010-06-26 10:02:24 +10001178 if (options->forward_x11_timeout == -1)
1179 options->forward_x11_timeout = 1200;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001180 if (options->exit_on_forward_failure == -1)
1181 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001182 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001183 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001184 if (options->gateway_ports == -1)
1185 options->gateway_ports = 0;
1186 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001187 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001188 if (options->rsa_authentication == -1)
1189 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001190 if (options->pubkey_authentication == -1)
1191 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001192 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001193 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001194 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001195 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001196 if (options->gss_deleg_creds == -1)
1197 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001198 if (options->password_authentication == -1)
1199 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001200 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001201 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001202 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001203 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001204 if (options->hostbased_authentication == -1)
1205 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001206 if (options->batch_mode == -1)
1207 options->batch_mode = 0;
1208 if (options->check_host_ip == -1)
1209 options->check_host_ip = 1;
1210 if (options->strict_host_key_checking == -1)
1211 options->strict_host_key_checking = 2; /* 2 is default */
1212 if (options->compression == -1)
1213 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001214 if (options->tcp_keep_alive == -1)
1215 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001216 if (options->compression_level == -1)
1217 options->compression_level = 6;
1218 if (options->port == -1)
1219 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001220 if (options->address_family == -1)
1221 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001222 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001223 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001224 if (options->number_of_password_prompts == -1)
1225 options->number_of_password_prompts = 3;
1226 /* Selected in ssh_login(). */
1227 if (options->cipher == -1)
1228 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001229 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001230 /* options->macs, default set in myproposals.h */
Damien Millerd5f62bf2010-09-24 22:11:14 +10001231 /* options->kex_algorithms, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001232 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001233 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +11001234 options->protocol = SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001235 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001236 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001237 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001238 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001239 xmalloc(len);
1240 snprintf(options->identity_files[options->num_identity_files++],
1241 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001242 }
1243 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001244 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1245 options->identity_files[options->num_identity_files] =
1246 xmalloc(len);
1247 snprintf(options->identity_files[options->num_identity_files++],
1248 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1249
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001250 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001251 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001252 xmalloc(len);
1253 snprintf(options->identity_files[options->num_identity_files++],
1254 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001255#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001256 len = 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA) + 1;
1257 options->identity_files[options->num_identity_files] =
1258 xmalloc(len);
1259 snprintf(options->identity_files[options->num_identity_files++],
1260 len, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA);
Damien Miller6af914a2010-09-10 11:39:26 +10001261#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001262 }
Damien Millereba71ba2000-04-29 23:57:08 +10001263 }
Damien Miller95def091999-11-25 00:26:21 +11001264 if (options->escape_char == -1)
1265 options->escape_char = '~';
1266 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001267 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001268 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001269 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001270 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001271 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001272 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001273 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001274 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001275 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001276 if (options->clear_forwardings == 1)
1277 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001278 if (options->no_host_authentication_for_localhost == - 1)
1279 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001280 if (options->identities_only == -1)
1281 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001282 if (options->enable_ssh_keysign == -1)
1283 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001284 if (options->rekey_limit == -1)
1285 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001286 if (options->verify_host_key_dns == -1)
1287 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001288 if (options->server_alive_interval == -1)
1289 options->server_alive_interval = 0;
1290 if (options->server_alive_count_max == -1)
1291 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001292 if (options->control_master == -1)
1293 options->control_master = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001294 if (options->control_persist == -1) {
1295 options->control_persist = 0;
1296 options->control_persist_timeout = 0;
1297 }
Damien Millere1776152005-03-01 21:47:37 +11001298 if (options->hash_known_hosts == -1)
1299 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001300 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001301 options->tun_open = SSH_TUNMODE_NO;
1302 if (options->tun_local == -1)
1303 options->tun_local = SSH_TUNID_ANY;
1304 if (options->tun_remote == -1)
1305 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001306 if (options->permit_local_command == -1)
1307 options->permit_local_command = 0;
Darren Tucker71e4d542009-07-06 07:12:27 +10001308 if (options->use_roaming == -1)
1309 options->use_roaming = 1;
Damien Miller10288242008-06-30 00:04:03 +10001310 if (options->visual_host_key == -1)
1311 options->visual_host_key = 0;
Damien Miller01ed2272008-11-05 16:20:46 +11001312 if (options->zero_knowledge_password_authentication == -1)
1313 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +11001314 if (options->ip_qos_interactive == -1)
1315 options->ip_qos_interactive = IPTOS_LOWDELAY;
1316 if (options->ip_qos_bulk == -1)
1317 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Millerd27b9472005-12-13 19:29:02 +11001318 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001319 /* options->proxy_command should not be set by default */
1320 /* options->user will be set in the main program if appropriate */
1321 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001322 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001323 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001324}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001325
1326/*
1327 * parse_forward
1328 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001329 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001330 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001331 * dynamicfwd == 1
1332 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001333 * returns number of arguments parsed or zero on error
1334 */
1335int
Damien Miller4bf648f2009-02-14 16:28:21 +11001336parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001337{
1338 int i;
1339 char *p, *cp, *fwdarg[4];
1340
1341 memset(fwd, '\0', sizeof(*fwd));
1342
1343 cp = p = xstrdup(fwdspec);
1344
1345 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001346 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001347 cp++;
1348
1349 for (i = 0; i < 4; ++i)
1350 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1351 break;
1352
Damien Millerf4b39532008-11-03 19:28:21 +11001353 /* Check for trailing garbage */
Damien Millerf91ee4c2005-03-01 21:24:33 +11001354 if (cp != NULL)
1355 i = 0; /* failure */
1356
1357 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001358 case 1:
1359 fwd->listen_host = NULL;
1360 fwd->listen_port = a2port(fwdarg[0]);
1361 fwd->connect_host = xstrdup("socks");
1362 break;
1363
1364 case 2:
1365 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1366 fwd->listen_port = a2port(fwdarg[1]);
1367 fwd->connect_host = xstrdup("socks");
1368 break;
1369
Damien Millerf91ee4c2005-03-01 21:24:33 +11001370 case 3:
1371 fwd->listen_host = NULL;
1372 fwd->listen_port = a2port(fwdarg[0]);
1373 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1374 fwd->connect_port = a2port(fwdarg[2]);
1375 break;
1376
1377 case 4:
1378 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1379 fwd->listen_port = a2port(fwdarg[1]);
1380 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1381 fwd->connect_port = a2port(fwdarg[3]);
1382 break;
1383 default:
1384 i = 0; /* failure */
1385 }
1386
1387 xfree(p);
1388
Damien Millera699d952008-11-03 19:27:34 +11001389 if (dynamicfwd) {
1390 if (!(i == 1 || i == 2))
1391 goto fail_free;
1392 } else {
1393 if (!(i == 3 || i == 4))
1394 goto fail_free;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001395 if (fwd->connect_port <= 0)
Damien Millera699d952008-11-03 19:27:34 +11001396 goto fail_free;
1397 }
1398
Damien Miller4bf648f2009-02-14 16:28:21 +11001399 if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001400 goto fail_free;
1401
1402 if (fwd->connect_host != NULL &&
1403 strlen(fwd->connect_host) >= NI_MAXHOST)
1404 goto fail_free;
Damien Miller4bf648f2009-02-14 16:28:21 +11001405 if (fwd->listen_host != NULL &&
1406 strlen(fwd->listen_host) >= NI_MAXHOST)
1407 goto fail_free;
1408
Damien Millerf91ee4c2005-03-01 21:24:33 +11001409
1410 return (i);
1411
1412 fail_free:
Damien Miller0d772d92008-12-09 14:12:05 +11001413 if (fwd->connect_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001414 xfree(fwd->connect_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001415 fwd->connect_host = NULL;
1416 }
1417 if (fwd->listen_host != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001418 xfree(fwd->listen_host);
Damien Miller0d772d92008-12-09 14:12:05 +11001419 fwd->listen_host = NULL;
1420 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001421 return (0);
1422}