blob: 27be8df687782963d042f312160540f18404028c [file] [log] [blame]
Damien Millera699d952008-11-03 19:27:34 +11001/* $OpenBSD: readconf.c,v 1.168 2008/11/01 17:40:33 stevesk 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 Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Millerc7b06362006-03-15 11:53:45 +110023#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100024#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100025#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100026#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100027#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100028#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
Damien Millerc7b06362006-03-15 11:53:45 +110031
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
36#include "pathnames.h"
37#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "readconf.h"
40#include "match.h"
41#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043#include "kex.h"
44#include "mac.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46/* Format of the configuration file:
47
48 # Configuration data is parsed as follows:
49 # 1. command line options
50 # 2. user-specific file
51 # 3. system-wide file
52 # Any configuration value is only changed the first time it is set.
53 # Thus, host-specific definitions should be at the beginning of the
54 # configuration file, and defaults at the end.
55
56 # Host-specific declarations. These may override anything above. A single
57 # host may match multiple declarations; these are processed in the order
58 # that they are given in.
59
60 Host *.ngs.fi ngs.fi
Ben Lindstrom4daea862002-06-09 20:04:02 +000061 User foo
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63 Host fake.com
64 HostName another.host.name.real.org
65 User blaah
66 Port 34289
67 ForwardX11 no
68 ForwardAgent no
69
70 Host books.com
71 RemoteForward 9999 shadows.cs.hut.fi:9999
72 Cipher 3des
73
74 Host fascist.blob.com
75 Port 23123
76 User tylonen
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 PasswordAuthentication no
78
79 Host puukko.hut.fi
80 User t35124p
81 ProxyCommand ssh-proxy %h %p
82
83 Host *.fr
Ben Lindstrom4daea862002-06-09 20:04:02 +000084 PublicKeyAuthentication no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86 Host *.su
87 Cipher none
88 PasswordAuthentication no
89
Damien Millerd27b9472005-12-13 19:29:02 +110090 Host vpn.fake.com
91 Tunnel yes
92 TunnelDevice 3
93
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 # Defaults for various options
95 Host *
96 ForwardAgent no
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 ForwardX11 no
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 PasswordAuthentication yes
99 RSAAuthentication yes
100 RhostsRSAAuthentication yes
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 StrictHostKeyChecking yes
Damien Miller12c150e2003-12-17 16:31:10 +1100102 TcpKeepAlive no
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 IdentityFile ~/.ssh/identity
104 Port 22
105 EscapeChar ~
106
107*/
108
109/* Keyword tokens. */
110
Damien Miller95def091999-11-25 00:26:21 +1100111typedef enum {
112 oBadOption,
Darren Tucker0a118da2003-10-15 15:54:32 +1000113 oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000114 oExitOnForwardFailure,
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000115 oPasswordAuthentication, oRSAAuthentication,
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000116 oChallengeResponseAuthentication, oXAuthLocation,
Damien Miller95def091999-11-25 00:26:21 +1100117 oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
118 oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
119 oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
120 oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
Damien Miller12c150e2003-12-17 16:31:10 +1100121 oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000122 oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123 oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000124 oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000125 oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000126 oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
Ben Lindstrom4daea862002-06-09 20:04:02 +0000127 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
Damien Millerb78d5eb2003-05-16 11:39:04 +1000128 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
Darren Tucker0efd1552003-08-26 11:49:55 +1000129 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
Damien Millerbd394c32004-03-08 23:12:36 +1100130 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
Damien Millere1776152005-03-01 21:47:37 +1100131 oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
Damien Millerd27b9472005-12-13 19:29:02 +1100132 oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
Damien Miller10288242008-06-30 00:04:03 +1000133 oVisualHostKey,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000134 oDeprecated, oUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135} OpCodes;
136
137/* Textual representations of the tokens. */
138
Damien Miller95def091999-11-25 00:26:21 +1100139static struct {
140 const char *name;
141 OpCodes opcode;
142} keywords[] = {
143 { "forwardagent", oForwardAgent },
144 { "forwardx11", oForwardX11 },
Darren Tucker0a118da2003-10-15 15:54:32 +1000145 { "forwardx11trusted", oForwardX11Trusted },
Darren Tuckere7d4b192006-07-12 22:17:10 +1000146 { "exitonforwardfailure", oExitOnForwardFailure },
Damien Millerd3a18572000-06-07 19:55:44 +1000147 { "xauthlocation", oXAuthLocation },
Damien Miller95def091999-11-25 00:26:21 +1100148 { "gatewayports", oGatewayPorts },
149 { "useprivilegedport", oUsePrivilegedPort },
Darren Tuckerec960f22003-08-13 20:37:05 +1000150 { "rhostsauthentication", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100151 { "passwordauthentication", oPasswordAuthentication },
Damien Miller874d77b2000-10-14 16:23:11 +1100152 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
153 { "kbdinteractivedevices", oKbdInteractiveDevices },
Damien Miller95def091999-11-25 00:26:21 +1100154 { "rsaauthentication", oRSAAuthentication },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100155 { "pubkeyauthentication", oPubkeyAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000156 { "dsaauthentication", oPubkeyAuthentication }, /* alias */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000157 { "rhostsrsaauthentication", oRhostsRSAAuthentication },
Ben Lindstromd69dab32001-04-12 23:36:05 +0000158 { "hostbasedauthentication", oHostbasedAuthentication },
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000159 { "challengeresponseauthentication", oChallengeResponseAuthentication },
160 { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
161 { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
Damien Millerf9b3feb2003-05-16 11:38:32 +1000162 { "kerberosauthentication", oUnsupported },
163 { "kerberostgtpassing", oUnsupported },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000164 { "afstokenpassing", oUnsupported },
Darren Tucker0efd1552003-08-26 11:49:55 +1000165#if defined(GSSAPI)
166 { "gssapiauthentication", oGssAuthentication },
Darren Tucker0efd1552003-08-26 11:49:55 +1000167 { "gssapidelegatecredentials", oGssDelegateCreds },
168#else
169 { "gssapiauthentication", oUnsupported },
170 { "gssapidelegatecredentials", oUnsupported },
171#endif
Ben Lindstrom4daea862002-06-09 20:04:02 +0000172 { "fallbacktorsh", oDeprecated },
173 { "usersh", oDeprecated },
Damien Miller95def091999-11-25 00:26:21 +1100174 { "identityfile", oIdentityFile },
Damien Miller0bc1bd82000-11-13 22:57:25 +1100175 { "identityfile2", oIdentityFile }, /* alias */
Damien Millerbd394c32004-03-08 23:12:36 +1100176 { "identitiesonly", oIdentitiesOnly },
Damien Miller95def091999-11-25 00:26:21 +1100177 { "hostname", oHostName },
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000178 { "hostkeyalias", oHostKeyAlias },
Damien Miller95def091999-11-25 00:26:21 +1100179 { "proxycommand", oProxyCommand },
180 { "port", oPort },
181 { "cipher", oCipher },
Damien Miller78928792000-04-12 20:17:38 +1000182 { "ciphers", oCiphers },
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000183 { "macs", oMacs },
Damien Miller78928792000-04-12 20:17:38 +1000184 { "protocol", oProtocol },
Damien Miller95def091999-11-25 00:26:21 +1100185 { "remoteforward", oRemoteForward },
186 { "localforward", oLocalForward },
187 { "user", oUser },
188 { "host", oHost },
189 { "escapechar", oEscapeChar },
Damien Miller95def091999-11-25 00:26:21 +1100190 { "globalknownhostsfile", oGlobalKnownHostsFile },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000191 { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
Damien Millereba71ba2000-04-29 23:57:08 +1000192 { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000193 { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100194 { "connectionattempts", oConnectionAttempts },
195 { "batchmode", oBatchMode },
196 { "checkhostip", oCheckHostIP },
197 { "stricthostkeychecking", oStrictHostKeyChecking },
198 { "compression", oCompression },
199 { "compressionlevel", oCompressionLevel },
Damien Miller12c150e2003-12-17 16:31:10 +1100200 { "tcpkeepalive", oTCPKeepAlive },
201 { "keepalive", oTCPKeepAlive }, /* obsolete */
Damien Miller95def091999-11-25 00:26:21 +1100202 { "numberofpasswordprompts", oNumberOfPasswordPrompts },
Damien Miller95def091999-11-25 00:26:21 +1100203 { "loglevel", oLogLevel },
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000204 { "dynamicforward", oDynamicForward },
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000205 { "preferredauthentications", oPreferredAuthentications },
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000206 { "hostkeyalgorithms", oHostKeyAlgorithms },
Ben Lindstrome0f88042001-04-30 13:06:24 +0000207 { "bindaddress", oBindAddress },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000208#ifdef SMARTCARD
Ben Lindstromae996bf2001-08-06 21:27:53 +0000209 { "smartcarddevice", oSmartcardDevice },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000210#else
211 { "smartcarddevice", oUnsupported },
212#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100213 { "clearallforwardings", oClearAllForwardings },
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000214 { "enablesshkeysign", oEnableSSHKeysign },
Damien Miller37876e92003-05-15 10:19:46 +1000215 { "verifyhostkeydns", oVerifyHostKeyDNS },
Damien Miller9f0f5c62001-12-21 14:45:46 +1100216 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
Damien Millera5539d22003-04-09 20:50:06 +1000217 { "rekeylimit", oRekeyLimit },
Damien Millerb78d5eb2003-05-16 11:39:04 +1000218 { "connecttimeout", oConnectTimeout },
Damien Miller20a8f972003-05-18 20:50:30 +1000219 { "addressfamily", oAddressFamily },
Damien Miller509b0102003-12-17 16:33:10 +1100220 { "serveraliveinterval", oServerAliveInterval },
221 { "serveralivecountmax", oServerAliveCountMax },
Darren Tucker46bc0752004-05-02 22:11:30 +1000222 { "sendenv", oSendEnv },
Damien Miller0e220db2004-06-15 10:34:08 +1000223 { "controlpath", oControlPath },
224 { "controlmaster", oControlMaster },
Damien Millere1776152005-03-01 21:47:37 +1100225 { "hashknownhosts", oHashKnownHosts },
Damien Millerd27b9472005-12-13 19:29:02 +1100226 { "tunnel", oTunnel },
227 { "tunneldevice", oTunnelDevice },
228 { "localcommand", oLocalCommand },
229 { "permitlocalcommand", oPermitLocalCommand },
Damien Miller10288242008-06-30 00:04:03 +1000230 { "visualhostkey", oVisualHostKey },
Ben Lindstrom65366a82001-12-06 16:32:47 +0000231 { NULL, oBadOption }
Damien Miller5ce662a1999-11-11 17:57:39 +1100232};
233
Damien Miller5428f641999-11-25 11:54:57 +1100234/*
235 * Adds a local TCP/IP port forward to options. Never returns if there is an
236 * error.
237 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Damien Miller4af51302000-04-16 11:18:38 +1000239void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100240add_local_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241{
Damien Miller95def091999-11-25 00:26:21 +1100242 Forward *fwd;
Ben Lindstrom99a4e142002-07-09 14:06:40 +0000243#ifndef NO_IPPORT_RESERVED_CONCEPT
Damien Miller95def091999-11-25 00:26:21 +1100244 extern uid_t original_real_uid;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100245 if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000246 fatal("Privileged ports can only be forwarded by root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100247#endif
Damien Miller95def091999-11-25 00:26:21 +1100248 if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
249 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
250 fwd = &options->local_forwards[options->num_local_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100251
252 fwd->listen_host = (newfwd->listen_host == NULL) ?
253 NULL : xstrdup(newfwd->listen_host);
254 fwd->listen_port = newfwd->listen_port;
255 fwd->connect_host = xstrdup(newfwd->connect_host);
256 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257}
258
Damien Miller5428f641999-11-25 11:54:57 +1100259/*
260 * Adds a remote TCP/IP port forward to options. Never returns if there is
261 * an error.
262 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Miller4af51302000-04-16 11:18:38 +1000264void
Damien Millerf91ee4c2005-03-01 21:24:33 +1100265add_remote_forward(Options *options, const Forward *newfwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266{
Damien Miller95def091999-11-25 00:26:21 +1100267 Forward *fwd;
268 if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
269 fatal("Too many remote forwards (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100270 SSH_MAX_FORWARDS_PER_DIRECTION);
Damien Miller95def091999-11-25 00:26:21 +1100271 fwd = &options->remote_forwards[options->num_remote_forwards++];
Damien Millerf91ee4c2005-03-01 21:24:33 +1100272
273 fwd->listen_host = (newfwd->listen_host == NULL) ?
274 NULL : xstrdup(newfwd->listen_host);
275 fwd->listen_port = newfwd->listen_port;
276 fwd->connect_host = xstrdup(newfwd->connect_host);
277 fwd->connect_port = newfwd->connect_port;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278}
279
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000280static void
281clear_forwardings(Options *options)
282{
283 int i;
284
Damien Millerf91ee4c2005-03-01 21:24:33 +1100285 for (i = 0; i < options->num_local_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100286 if (options->local_forwards[i].listen_host != NULL)
287 xfree(options->local_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 xfree(options->local_forwards[i].connect_host);
289 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000290 options->num_local_forwards = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291 for (i = 0; i < options->num_remote_forwards; i++) {
Darren Tucker1d55ca72005-03-14 22:58:40 +1100292 if (options->remote_forwards[i].listen_host != NULL)
293 xfree(options->remote_forwards[i].listen_host);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100294 xfree(options->remote_forwards[i].connect_host);
295 }
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000296 options->num_remote_forwards = 0;
Damien Miller7b58e802005-12-13 19:33:19 +1100297 options->tun_open = SSH_TUNMODE_NO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000298}
299
Damien Miller5428f641999-11-25 11:54:57 +1100300/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000301 * Returns the number of the token pointed to by cp or oBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100302 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller4af51302000-04-16 11:18:38 +1000304static OpCodes
Damien Miller95def091999-11-25 00:26:21 +1100305parse_token(const char *cp, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000307 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Miller95def091999-11-25 00:26:21 +1100309 for (i = 0; keywords[i].name; i++)
Damien Miller5428f641999-11-25 11:54:57 +1100310 if (strcasecmp(cp, keywords[i].name) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100311 return keywords[i].opcode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000313 error("%s: line %d: Bad configuration option: %s",
314 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100315 return oBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316}
317
Damien Miller5428f641999-11-25 11:54:57 +1100318/*
319 * Processes a single option line as used in the configuration files. This
320 * only sets those values that have not already been set.
321 */
Damien Miller61f08ac2003-02-24 11:56:27 +1100322#define WHITESPACE " \t\r\n"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Miller2ccf6611999-11-15 15:25:10 +1100324int
325process_config_line(Options *options, const char *host,
Damien Miller95def091999-11-25 00:26:21 +1100326 char *line, const char *filename, int linenum,
327 int *activep)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328{
Damien Millerf91ee4c2005-03-01 21:24:33 +1100329 char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
Damien Millerb59d4fe2006-03-15 11:30:38 +1100330 int opcode, *intptr, value, value2, scale;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100331 LogLevel *log_level_ptr;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100332 long long orig, val64;
Damien Miller61f08ac2003-02-24 11:56:27 +1100333 size_t len;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Millerc652cac2003-05-14 13:40:54 +1000336 /* Strip trailing whitespace */
Darren Tucker47eede72005-03-14 23:08:12 +1100337 for (len = strlen(line) - 1; len > 0; len--) {
Damien Millerc652cac2003-05-14 13:40:54 +1000338 if (strchr(WHITESPACE, line[len]) == NULL)
339 break;
340 line[len] = '\0';
341 }
342
Damien Millerbe484b52000-07-15 14:14:16 +1000343 s = line;
344 /* Get the keyword. (Each line is supposed to begin with a keyword). */
Damien Miller928b2362006-03-26 13:53:32 +1100345 if ((keyword = strdelim(&s)) == NULL)
346 return 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000347 /* Ignore leading whitespace. */
348 if (*keyword == '\0')
349 keyword = strdelim(&s);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000350 if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
Damien Miller95def091999-11-25 00:26:21 +1100351 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller37023962000-07-11 17:31:38 +1000353 opcode = parse_token(keyword, filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller95def091999-11-25 00:26:21 +1100355 switch (opcode) {
356 case oBadOption:
Damien Miller5428f641999-11-25 11:54:57 +1100357 /* don't panic, but count bad options */
358 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100359 /* NOTREACHED */
Damien Millerb78d5eb2003-05-16 11:39:04 +1000360 case oConnectTimeout:
361 intptr = &options->connection_timeout;
Damien Miller509b0102003-12-17 16:33:10 +1100362parse_time:
Damien Millerb78d5eb2003-05-16 11:39:04 +1000363 arg = strdelim(&s);
364 if (!arg || *arg == '\0')
365 fatal("%s line %d: missing time value.",
366 filename, linenum);
367 if ((value = convtime(arg)) == -1)
368 fatal("%s line %d: invalid time value.",
369 filename, linenum);
Darren Tuckera52c5b62007-02-19 22:09:45 +1100370 if (*activep && *intptr == -1)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000371 *intptr = value;
372 break;
373
Damien Miller95def091999-11-25 00:26:21 +1100374 case oForwardAgent:
375 intptr = &options->forward_agent;
376parse_flag:
Damien Millerbe484b52000-07-15 14:14:16 +1000377 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000378 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100379 fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
380 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000381 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100382 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000383 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100384 value = 0;
385 else
386 fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
387 if (*activep && *intptr == -1)
388 *intptr = value;
389 break;
390
391 case oForwardX11:
392 intptr = &options->forward_x11;
393 goto parse_flag;
394
Darren Tucker0a118da2003-10-15 15:54:32 +1000395 case oForwardX11Trusted:
396 intptr = &options->forward_x11_trusted;
397 goto parse_flag;
398
Damien Miller95def091999-11-25 00:26:21 +1100399 case oGatewayPorts:
400 intptr = &options->gateway_ports;
401 goto parse_flag;
402
Darren Tuckere7d4b192006-07-12 22:17:10 +1000403 case oExitOnForwardFailure:
404 intptr = &options->exit_on_forward_failure;
405 goto parse_flag;
406
Damien Miller95def091999-11-25 00:26:21 +1100407 case oUsePrivilegedPort:
408 intptr = &options->use_privileged_port;
409 goto parse_flag;
410
Damien Miller95def091999-11-25 00:26:21 +1100411 case oPasswordAuthentication:
412 intptr = &options->password_authentication;
413 goto parse_flag;
414
Damien Miller874d77b2000-10-14 16:23:11 +1100415 case oKbdInteractiveAuthentication:
416 intptr = &options->kbd_interactive_authentication;
417 goto parse_flag;
418
419 case oKbdInteractiveDevices:
420 charptr = &options->kbd_interactive_devices;
421 goto parse_string;
422
Damien Miller0bc1bd82000-11-13 22:57:25 +1100423 case oPubkeyAuthentication:
424 intptr = &options->pubkey_authentication;
Damien Millere247cc42000-05-07 12:03:14 +1000425 goto parse_flag;
426
Damien Miller95def091999-11-25 00:26:21 +1100427 case oRSAAuthentication:
428 intptr = &options->rsa_authentication;
429 goto parse_flag;
430
431 case oRhostsRSAAuthentication:
432 intptr = &options->rhosts_rsa_authentication;
433 goto parse_flag;
434
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000435 case oHostbasedAuthentication:
436 intptr = &options->hostbased_authentication;
437 goto parse_flag;
438
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000439 case oChallengeResponseAuthentication:
Ben Lindstrom551ea372001-06-05 18:56:16 +0000440 intptr = &options->challenge_response_authentication;
Damien Miller95def091999-11-25 00:26:21 +1100441 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000442
Darren Tucker0efd1552003-08-26 11:49:55 +1000443 case oGssAuthentication:
444 intptr = &options->gss_authentication;
445 goto parse_flag;
446
447 case oGssDelegateCreds:
448 intptr = &options->gss_deleg_creds;
449 goto parse_flag;
450
Damien Miller95def091999-11-25 00:26:21 +1100451 case oBatchMode:
452 intptr = &options->batch_mode;
453 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Damien Miller95def091999-11-25 00:26:21 +1100455 case oCheckHostIP:
456 intptr = &options->check_host_ip;
Damien Miller10288242008-06-30 00:04:03 +1000457 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458
Damien Miller37876e92003-05-15 10:19:46 +1000459 case oVerifyHostKeyDNS:
460 intptr = &options->verify_host_key_dns;
Damien Miller150b5572003-11-17 21:19:29 +1100461 goto parse_yesnoask;
Damien Miller37876e92003-05-15 10:19:46 +1000462
Damien Miller95def091999-11-25 00:26:21 +1100463 case oStrictHostKeyChecking:
464 intptr = &options->strict_host_key_checking;
Damien Miller150b5572003-11-17 21:19:29 +1100465parse_yesnoask:
Damien Millerbe484b52000-07-15 14:14:16 +1000466 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000467 if (!arg || *arg == '\0')
Ben Lindstrom5ed8acd2001-01-29 08:00:54 +0000468 fatal("%.200s line %d: Missing yes/no/ask argument.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100469 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100470 value = 0; /* To avoid compiler warning... */
Damien Miller37023962000-07-11 17:31:38 +1000471 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100472 value = 1;
Damien Miller37023962000-07-11 17:31:38 +1000473 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100474 value = 0;
Damien Miller37023962000-07-11 17:31:38 +1000475 else if (strcmp(arg, "ask") == 0)
Damien Miller95def091999-11-25 00:26:21 +1100476 value = 2;
477 else
478 fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
479 if (*activep && *intptr == -1)
480 *intptr = value;
481 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482
Damien Miller95def091999-11-25 00:26:21 +1100483 case oCompression:
484 intptr = &options->compression;
485 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller12c150e2003-12-17 16:31:10 +1100487 case oTCPKeepAlive:
488 intptr = &options->tcp_keep_alive;
Damien Miller95def091999-11-25 00:26:21 +1100489 goto parse_flag;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000491 case oNoHostAuthenticationForLocalhost:
492 intptr = &options->no_host_authentication_for_localhost;
493 goto parse_flag;
494
Damien Miller95def091999-11-25 00:26:21 +1100495 case oNumberOfPasswordPrompts:
496 intptr = &options->number_of_password_prompts;
497 goto parse_int;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Damien Miller95def091999-11-25 00:26:21 +1100499 case oCompressionLevel:
500 intptr = &options->compression_level;
501 goto parse_int;
502
Damien Millera5539d22003-04-09 20:50:06 +1000503 case oRekeyLimit:
Damien Millera5539d22003-04-09 20:50:06 +1000504 arg = strdelim(&s);
505 if (!arg || *arg == '\0')
506 fatal("%.200s line %d: Missing argument.", filename, linenum);
507 if (arg[0] < '0' || arg[0] > '9')
508 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Millerb59d4fe2006-03-15 11:30:38 +1100509 orig = val64 = strtoll(arg, &endofnumber, 10);
Damien Millera5539d22003-04-09 20:50:06 +1000510 if (arg == endofnumber)
511 fatal("%.200s line %d: Bad number.", filename, linenum);
512 switch (toupper(*endofnumber)) {
Damien Millerb59d4fe2006-03-15 11:30:38 +1100513 case '\0':
514 scale = 1;
515 break;
Damien Millera5539d22003-04-09 20:50:06 +1000516 case 'K':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100517 scale = 1<<10;
Damien Millera5539d22003-04-09 20:50:06 +1000518 break;
519 case 'M':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100520 scale = 1<<20;
Damien Millera5539d22003-04-09 20:50:06 +1000521 break;
522 case 'G':
Damien Millerb59d4fe2006-03-15 11:30:38 +1100523 scale = 1<<30;
Damien Millera5539d22003-04-09 20:50:06 +1000524 break;
Damien Millerb59d4fe2006-03-15 11:30:38 +1100525 default:
526 fatal("%.200s line %d: Invalid RekeyLimit suffix",
527 filename, linenum);
Damien Millera5539d22003-04-09 20:50:06 +1000528 }
Damien Millerb59d4fe2006-03-15 11:30:38 +1100529 val64 *= scale;
530 /* detect integer wrap and too-large limits */
Damien Miller3dff1762008-02-10 22:25:52 +1100531 if ((val64 / scale) != orig || val64 > UINT_MAX)
Damien Millerb59d4fe2006-03-15 11:30:38 +1100532 fatal("%.200s line %d: RekeyLimit too large",
533 filename, linenum);
534 if (val64 < 16)
535 fatal("%.200s line %d: RekeyLimit too small",
536 filename, linenum);
Damien Miller3dff1762008-02-10 22:25:52 +1100537 if (*activep && options->rekey_limit == -1)
538 options->rekey_limit = (u_int32_t)val64;
Damien Millera5539d22003-04-09 20:50:06 +1000539 break;
540
Damien Miller95def091999-11-25 00:26:21 +1100541 case oIdentityFile:
Damien Millerbe484b52000-07-15 14:14:16 +1000542 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000543 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100544 fatal("%.200s line %d: Missing argument.", filename, linenum);
545 if (*activep) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100546 intptr = &options->num_identity_files;
Damien Millereba71ba2000-04-29 23:57:08 +1000547 if (*intptr >= SSH_MAX_IDENTITY_FILES)
Damien Miller95def091999-11-25 00:26:21 +1100548 fatal("%.200s line %d: Too many identity files specified (max %d).",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100549 filename, linenum, SSH_MAX_IDENTITY_FILES);
Darren Tuckercb0e1752007-02-19 22:12:53 +1100550 charptr = &options->identity_files[*intptr];
Damien Miller37023962000-07-11 17:31:38 +1000551 *charptr = xstrdup(arg);
Damien Millereba71ba2000-04-29 23:57:08 +1000552 *intptr = *intptr + 1;
Damien Miller95def091999-11-25 00:26:21 +1100553 }
554 break;
555
Damien Millerd3a18572000-06-07 19:55:44 +1000556 case oXAuthLocation:
557 charptr=&options->xauth_location;
558 goto parse_string;
559
Damien Miller95def091999-11-25 00:26:21 +1100560 case oUser:
561 charptr = &options->user;
562parse_string:
Damien Millerbe484b52000-07-15 14:14:16 +1000563 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000564 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100565 fatal("%.200s line %d: Missing argument.", filename, linenum);
566 if (*activep && *charptr == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000567 *charptr = xstrdup(arg);
Damien Miller95def091999-11-25 00:26:21 +1100568 break;
569
570 case oGlobalKnownHostsFile:
571 charptr = &options->system_hostfile;
572 goto parse_string;
573
574 case oUserKnownHostsFile:
575 charptr = &options->user_hostfile;
576 goto parse_string;
577
Damien Millereba71ba2000-04-29 23:57:08 +1000578 case oGlobalKnownHostsFile2:
579 charptr = &options->system_hostfile2;
580 goto parse_string;
581
582 case oUserKnownHostsFile2:
583 charptr = &options->user_hostfile2;
584 goto parse_string;
585
Damien Miller95def091999-11-25 00:26:21 +1100586 case oHostName:
587 charptr = &options->hostname;
588 goto parse_string;
589
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000590 case oHostKeyAlias:
591 charptr = &options->host_key_alias;
592 goto parse_string;
593
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000594 case oPreferredAuthentications:
595 charptr = &options->preferred_authentications;
596 goto parse_string;
597
Ben Lindstrome0f88042001-04-30 13:06:24 +0000598 case oBindAddress:
599 charptr = &options->bind_address;
600 goto parse_string;
601
Ben Lindstromae996bf2001-08-06 21:27:53 +0000602 case oSmartcardDevice:
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000603 charptr = &options->smartcard_device;
604 goto parse_string;
Ben Lindstromae996bf2001-08-06 21:27:53 +0000605
Damien Miller95def091999-11-25 00:26:21 +1100606 case oProxyCommand:
Damien Millerd27b9472005-12-13 19:29:02 +1100607 charptr = &options->proxy_command;
608parse_command:
Darren Tuckera99c1b72003-06-28 12:40:12 +1000609 if (s == NULL)
610 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller61f08ac2003-02-24 11:56:27 +1100611 len = strspn(s, WHITESPACE "=");
Damien Miller95def091999-11-25 00:26:21 +1100612 if (*activep && *charptr == NULL)
Damien Miller61f08ac2003-02-24 11:56:27 +1100613 *charptr = xstrdup(s + len);
Damien Miller95def091999-11-25 00:26:21 +1100614 return 0;
615
616 case oPort:
617 intptr = &options->port;
618parse_int:
Damien Millerbe484b52000-07-15 14:14:16 +1000619 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000620 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100621 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000622 if (arg[0] < '0' || arg[0] > '9')
Damien Miller95def091999-11-25 00:26:21 +1100623 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller5428f641999-11-25 11:54:57 +1100624
625 /* Octal, decimal, or hex format? */
Damien Miller37023962000-07-11 17:31:38 +1000626 value = strtol(arg, &endofnumber, 0);
627 if (arg == endofnumber)
Damien Miller5428f641999-11-25 11:54:57 +1100628 fatal("%.200s line %d: Bad number.", filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100629 if (*activep && *intptr == -1)
630 *intptr = value;
631 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632
Damien Miller95def091999-11-25 00:26:21 +1100633 case oConnectionAttempts:
634 intptr = &options->connection_attempts;
635 goto parse_int;
Damien Miller5ce662a1999-11-11 17:57:39 +1100636
Damien Miller95def091999-11-25 00:26:21 +1100637 case oCipher:
638 intptr = &options->cipher;
Damien Millerbe484b52000-07-15 14:14:16 +1000639 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000640 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000641 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000642 value = cipher_number(arg);
Damien Miller95def091999-11-25 00:26:21 +1100643 if (value == -1)
644 fatal("%.200s line %d: Bad cipher '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100645 filename, linenum, arg ? arg : "<NONE>");
Damien Miller95def091999-11-25 00:26:21 +1100646 if (*activep && *intptr == -1)
647 *intptr = value;
648 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000649
Damien Miller78928792000-04-12 20:17:38 +1000650 case oCiphers:
Damien Millerbe484b52000-07-15 14:14:16 +1000651 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000652 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000653 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000654 if (!ciphers_valid(arg))
Damien Miller30c3d422000-05-09 11:02:59 +1000655 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100656 filename, linenum, arg ? arg : "<NONE>");
Damien Miller78928792000-04-12 20:17:38 +1000657 if (*activep && options->ciphers == NULL)
Damien Miller37023962000-07-11 17:31:38 +1000658 options->ciphers = xstrdup(arg);
Damien Miller78928792000-04-12 20:17:38 +1000659 break;
660
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000661 case oMacs:
662 arg = strdelim(&s);
663 if (!arg || *arg == '\0')
664 fatal("%.200s line %d: Missing argument.", filename, linenum);
665 if (!mac_valid(arg))
666 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100667 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000668 if (*activep && options->macs == NULL)
669 options->macs = xstrdup(arg);
670 break;
671
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000672 case oHostKeyAlgorithms:
673 arg = strdelim(&s);
674 if (!arg || *arg == '\0')
675 fatal("%.200s line %d: Missing argument.", filename, linenum);
676 if (!key_names_valid2(arg))
677 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100678 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000679 if (*activep && options->hostkeyalgorithms == NULL)
680 options->hostkeyalgorithms = xstrdup(arg);
681 break;
682
Damien Miller78928792000-04-12 20:17:38 +1000683 case oProtocol:
684 intptr = &options->protocol;
Damien Millerbe484b52000-07-15 14:14:16 +1000685 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000686 if (!arg || *arg == '\0')
Damien Millerb1715dc2000-05-30 13:44:51 +1000687 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000688 value = proto_spec(arg);
Damien Miller78928792000-04-12 20:17:38 +1000689 if (value == SSH_PROTO_UNKNOWN)
690 fatal("%.200s line %d: Bad protocol 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 && *intptr == SSH_PROTO_UNKNOWN)
693 *intptr = value;
694 break;
695
Damien Miller95def091999-11-25 00:26:21 +1100696 case oLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100697 log_level_ptr = &options->log_level;
Damien Millerbe484b52000-07-15 14:14:16 +1000698 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000699 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100700 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000701 fatal("%.200s line %d: unsupported log level '%s'",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100703 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
704 *log_level_ptr = (LogLevel) value;
Damien Miller95def091999-11-25 00:26:21 +1100705 break;
706
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000707 case oLocalForward:
Damien Miller95def091999-11-25 00:26:21 +1100708 case oRemoteForward:
Damien Millera699d952008-11-03 19:27:34 +1100709 case oDynamicForward:
Damien Millerbe484b52000-07-15 14:14:16 +1000710 arg = strdelim(&s);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100711 if (arg == NULL || *arg == '\0')
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000712 fatal("%.200s line %d: Missing port argument.",
713 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100714
Damien Millera699d952008-11-03 19:27:34 +1100715 if (opcode == oLocalForward ||
716 opcode == oRemoteForward) {
717 arg2 = strdelim(&s);
718 if (arg2 == NULL || *arg2 == '\0')
719 fatal("%.200s line %d: Missing target argument.",
720 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100721
Damien Millera699d952008-11-03 19:27:34 +1100722 /* construct a string for parse_forward */
723 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
724 } else if (opcode == oDynamicForward) {
725 strlcpy(fwdarg, arg, sizeof(fwdarg));
726 }
727
728 if (parse_forward(&fwd, fwdarg,
729 opcode == oDynamicForward ? 1 : 0) == 0)
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000730 fatal("%.200s line %d: Bad forwarding specification.",
731 filename, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100732
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000733 if (*activep) {
Damien Millera699d952008-11-03 19:27:34 +1100734 if (opcode == oLocalForward ||
735 opcode == oDynamicForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100736 add_local_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000737 else if (opcode == oRemoteForward)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100738 add_remote_forward(options, &fwd);
Ben Lindstrom62c25a42001-09-12 18:01:59 +0000739 }
Damien Miller95def091999-11-25 00:26:21 +1100740 break;
741
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000742 case oClearAllForwardings:
743 intptr = &options->clear_forwardings;
744 goto parse_flag;
745
Damien Miller95def091999-11-25 00:26:21 +1100746 case oHost:
747 *activep = 0;
Damien Millerbe484b52000-07-15 14:14:16 +1000748 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
Damien Miller37023962000-07-11 17:31:38 +1000749 if (match_pattern(host, arg)) {
750 debug("Applying options for %.100s", arg);
Damien Miller95def091999-11-25 00:26:21 +1100751 *activep = 1;
752 break;
753 }
Damien Millerbe484b52000-07-15 14:14:16 +1000754 /* Avoid garbage check below, as strdelim is done. */
Damien Miller95def091999-11-25 00:26:21 +1100755 return 0;
756
757 case oEscapeChar:
758 intptr = &options->escape_char;
Damien Millerbe484b52000-07-15 14:14:16 +1000759 arg = strdelim(&s);
Damien Miller37023962000-07-11 17:31:38 +1000760 if (!arg || *arg == '\0')
Damien Miller95def091999-11-25 00:26:21 +1100761 fatal("%.200s line %d: Missing argument.", filename, linenum);
Damien Miller37023962000-07-11 17:31:38 +1000762 if (arg[0] == '^' && arg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000763 (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
764 value = (u_char) arg[1] & 31;
Damien Miller37023962000-07-11 17:31:38 +1000765 else if (strlen(arg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000766 value = (u_char) arg[0];
Damien Miller37023962000-07-11 17:31:38 +1000767 else if (strcmp(arg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000768 value = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100769 else {
770 fatal("%.200s line %d: Bad escape character.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100771 filename, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100772 /* NOTREACHED */
773 value = 0; /* Avoid compiler warning. */
774 }
775 if (*activep && *intptr == -1)
776 *intptr = value;
777 break;
778
Damien Miller20a8f972003-05-18 20:50:30 +1000779 case oAddressFamily:
780 arg = strdelim(&s);
Damien Miller17b23d82005-05-26 12:11:56 +1000781 if (!arg || *arg == '\0')
782 fatal("%s line %d: missing address family.",
783 filename, linenum);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000784 intptr = &options->address_family;
Damien Miller20a8f972003-05-18 20:50:30 +1000785 if (strcasecmp(arg, "inet") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000786 value = AF_INET;
Damien Miller20a8f972003-05-18 20:50:30 +1000787 else if (strcasecmp(arg, "inet6") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000788 value = AF_INET6;
Damien Miller20a8f972003-05-18 20:50:30 +1000789 else if (strcasecmp(arg, "any") == 0)
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000790 value = AF_UNSPEC;
Damien Miller20a8f972003-05-18 20:50:30 +1000791 else
792 fatal("Unsupported AddressFamily \"%s\"", arg);
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000793 if (*activep && *intptr == -1)
794 *intptr = value;
Damien Miller20a8f972003-05-18 20:50:30 +1000795 break;
796
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000797 case oEnableSSHKeysign:
798 intptr = &options->enable_ssh_keysign;
799 goto parse_flag;
800
Damien Millerbd394c32004-03-08 23:12:36 +1100801 case oIdentitiesOnly:
802 intptr = &options->identities_only;
803 goto parse_flag;
804
Damien Miller509b0102003-12-17 16:33:10 +1100805 case oServerAliveInterval:
806 intptr = &options->server_alive_interval;
807 goto parse_time;
808
809 case oServerAliveCountMax:
810 intptr = &options->server_alive_count_max;
811 goto parse_int;
812
Darren Tucker46bc0752004-05-02 22:11:30 +1000813 case oSendEnv:
814 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
815 if (strchr(arg, '=') != NULL)
816 fatal("%s line %d: Invalid environment name.",
817 filename, linenum);
Damien Millerf8e7acc2005-03-05 11:22:50 +1100818 if (!*activep)
819 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +1000820 if (options->num_send_env >= MAX_SEND_ENV)
821 fatal("%s line %d: too many send env.",
822 filename, linenum);
823 options->send_env[options->num_send_env++] =
824 xstrdup(arg);
825 }
826 break;
827
Damien Miller0e220db2004-06-15 10:34:08 +1000828 case oControlPath:
829 charptr = &options->control_path;
830 goto parse_string;
831
832 case oControlMaster:
833 intptr = &options->control_master;
Damien Millerd14b1e72005-06-16 13:19:41 +1000834 arg = strdelim(&s);
835 if (!arg || *arg == '\0')
836 fatal("%.200s line %d: Missing ControlMaster argument.",
837 filename, linenum);
838 value = 0; /* To avoid compiler warning... */
839 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
840 value = SSHCTL_MASTER_YES;
841 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
842 value = SSHCTL_MASTER_NO;
843 else if (strcmp(arg, "auto") == 0)
844 value = SSHCTL_MASTER_AUTO;
845 else if (strcmp(arg, "ask") == 0)
846 value = SSHCTL_MASTER_ASK;
847 else if (strcmp(arg, "autoask") == 0)
848 value = SSHCTL_MASTER_AUTO_ASK;
849 else
850 fatal("%.200s line %d: Bad ControlMaster argument.",
851 filename, linenum);
852 if (*activep && *intptr == -1)
853 *intptr = value;
854 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000855
Damien Millere1776152005-03-01 21:47:37 +1100856 case oHashKnownHosts:
857 intptr = &options->hash_known_hosts;
858 goto parse_flag;
859
Damien Millerd27b9472005-12-13 19:29:02 +1100860 case oTunnel:
861 intptr = &options->tun_open;
Damien Miller7b58e802005-12-13 19:33:19 +1100862 arg = strdelim(&s);
863 if (!arg || *arg == '\0')
864 fatal("%s line %d: Missing yes/point-to-point/"
865 "ethernet/no argument.", filename, linenum);
866 value = 0; /* silence compiler */
867 if (strcasecmp(arg, "ethernet") == 0)
868 value = SSH_TUNMODE_ETHERNET;
869 else if (strcasecmp(arg, "point-to-point") == 0)
870 value = SSH_TUNMODE_POINTOPOINT;
871 else if (strcasecmp(arg, "yes") == 0)
872 value = SSH_TUNMODE_DEFAULT;
873 else if (strcasecmp(arg, "no") == 0)
874 value = SSH_TUNMODE_NO;
875 else
876 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
877 "no argument: %s", filename, linenum, arg);
878 if (*activep)
879 *intptr = value;
880 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100881
882 case oTunnelDevice:
883 arg = strdelim(&s);
884 if (!arg || *arg == '\0')
885 fatal("%.200s line %d: Missing argument.", filename, linenum);
886 value = a2tun(arg, &value2);
Damien Miller7b58e802005-12-13 19:33:19 +1100887 if (value == SSH_TUNID_ERR)
Damien Millerd27b9472005-12-13 19:29:02 +1100888 fatal("%.200s line %d: Bad tun device.", filename, linenum);
889 if (*activep) {
890 options->tun_local = value;
891 options->tun_remote = value2;
892 }
893 break;
894
895 case oLocalCommand:
896 charptr = &options->local_command;
897 goto parse_command;
898
899 case oPermitLocalCommand:
900 intptr = &options->permit_local_command;
901 goto parse_flag;
902
Damien Miller10288242008-06-30 00:04:03 +1000903 case oVisualHostKey:
904 intptr = &options->visual_host_key;
905 goto parse_flag;
906
Ben Lindstrom4daea862002-06-09 20:04:02 +0000907 case oDeprecated:
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000908 debug("%s line %d: Deprecated option \"%s\"",
Ben Lindstrom4daea862002-06-09 20:04:02 +0000909 filename, linenum, keyword);
Ben Lindstrom2e17b082002-06-09 20:13:27 +0000910 return 0;
Ben Lindstrom4daea862002-06-09 20:04:02 +0000911
Damien Millerf9b3feb2003-05-16 11:38:32 +1000912 case oUnsupported:
913 error("%s line %d: Unsupported option \"%s\"",
914 filename, linenum, keyword);
915 return 0;
916
Damien Miller95def091999-11-25 00:26:21 +1100917 default:
918 fatal("process_config_line: Unimplemented opcode %d", opcode);
919 }
920
921 /* Check that there is no garbage at end of line. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000922 if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
Damien Miller37023962000-07-11 17:31:38 +1000923 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000924 filename, linenum, arg);
Damien Miller37023962000-07-11 17:31:38 +1000925 }
Damien Miller95def091999-11-25 00:26:21 +1100926 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927}
928
929
Damien Miller5428f641999-11-25 11:54:57 +1100930/*
931 * Reads the config file and modifies the options accordingly. Options
932 * should already be initialized before this call. This never returns if
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000933 * there is an error. If the file does not exist, this returns 0.
Damien Miller5428f641999-11-25 11:54:57 +1100934 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000936int
Darren Tuckerfc959702004-07-17 16:12:08 +1000937read_config_file(const char *filename, const char *host, Options *options,
Damien Miller57a44762004-04-20 20:11:57 +1000938 int checkperm)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939{
Damien Miller95def091999-11-25 00:26:21 +1100940 FILE *f;
941 char line[1024];
942 int active, linenum;
943 int bad_options = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944
Damien Miller95def091999-11-25 00:26:21 +1100945 /* Open the file. */
Damien Miller57a44762004-04-20 20:11:57 +1000946 if ((f = fopen(filename, "r")) == NULL)
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000947 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller57a44762004-04-20 20:11:57 +1000949 if (checkperm) {
950 struct stat sb;
Darren Tuckerfc959702004-07-17 16:12:08 +1000951
Damien Miller33793852004-06-15 10:27:55 +1000952 if (fstat(fileno(f), &sb) == -1)
Damien Miller57a44762004-04-20 20:11:57 +1000953 fatal("fstat %s: %s", filename, strerror(errno));
Damien Miller57a44762004-04-20 20:11:57 +1000954 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
Damien Miller33793852004-06-15 10:27:55 +1000955 (sb.st_mode & 022) != 0))
Damien Miller57a44762004-04-20 20:11:57 +1000956 fatal("Bad owner or permissions on %s", filename);
Damien Miller57a44762004-04-20 20:11:57 +1000957 }
958
Damien Miller95def091999-11-25 00:26:21 +1100959 debug("Reading configuration data %.200s", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960
Damien Miller5428f641999-11-25 11:54:57 +1100961 /*
962 * Mark that we are now processing the options. This flag is turned
963 * on/off by Host specifications.
964 */
Damien Miller95def091999-11-25 00:26:21 +1100965 active = 1;
966 linenum = 0;
967 while (fgets(line, sizeof(line), f)) {
968 /* Update line number counter. */
969 linenum++;
970 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
971 bad_options++;
972 }
973 fclose(f);
974 if (bad_options > 0)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000975 fatal("%s: terminating, %d bad configuration options",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100976 filename, bad_options);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000977 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978}
979
Damien Miller5428f641999-11-25 11:54:57 +1100980/*
981 * Initializes options to special values that indicate that they have not yet
982 * been set. Read_config_file will only set options with this value. Options
983 * are processed in the following order: command line, user config file,
984 * system config file. Last, fill_default_options is called.
985 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller4af51302000-04-16 11:18:38 +1000987void
Damien Miller95def091999-11-25 00:26:21 +1100988initialize_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989{
Damien Miller95def091999-11-25 00:26:21 +1100990 memset(options, 'X', sizeof(*options));
991 options->forward_agent = -1;
992 options->forward_x11 = -1;
Darren Tucker0a118da2003-10-15 15:54:32 +1000993 options->forward_x11_trusted = -1;
Darren Tuckere7d4b192006-07-12 22:17:10 +1000994 options->exit_on_forward_failure = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000995 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100996 options->gateway_ports = -1;
997 options->use_privileged_port = -1;
Damien Miller95def091999-11-25 00:26:21 +1100998 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100999 options->pubkey_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001000 options->challenge_response_authentication = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001001 options->gss_authentication = -1;
1002 options->gss_deleg_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +11001003 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001004 options->kbd_interactive_authentication = -1;
1005 options->kbd_interactive_devices = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001006 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001007 options->hostbased_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +11001008 options->batch_mode = -1;
1009 options->check_host_ip = -1;
1010 options->strict_host_key_checking = -1;
1011 options->compression = -1;
Damien Miller12c150e2003-12-17 16:31:10 +11001012 options->tcp_keep_alive = -1;
Damien Miller95def091999-11-25 00:26:21 +11001013 options->compression_level = -1;
1014 options->port = -1;
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001015 options->address_family = -1;
Damien Miller95def091999-11-25 00:26:21 +11001016 options->connection_attempts = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +10001017 options->connection_timeout = -1;
Damien Miller95def091999-11-25 00:26:21 +11001018 options->number_of_password_prompts = -1;
1019 options->cipher = -1;
Damien Miller78928792000-04-12 20:17:38 +10001020 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001021 options->macs = NULL;
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001022 options->hostkeyalgorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +10001023 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller95def091999-11-25 00:26:21 +11001024 options->num_identity_files = 0;
1025 options->hostname = NULL;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001026 options->host_key_alias = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001027 options->proxy_command = NULL;
1028 options->user = NULL;
1029 options->escape_char = -1;
1030 options->system_hostfile = NULL;
1031 options->user_hostfile = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001032 options->system_hostfile2 = NULL;
1033 options->user_hostfile2 = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001034 options->num_local_forwards = 0;
1035 options->num_remote_forwards = 0;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001036 options->clear_forwardings = -1;
Damien Millerfcd93202002-02-05 12:26:34 +11001037 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001038 options->preferred_authentications = NULL;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001039 options->bind_address = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001040 options->smartcard_device = NULL;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001041 options->enable_ssh_keysign = - 1;
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001042 options->no_host_authentication_for_localhost = - 1;
Damien Millerbd394c32004-03-08 23:12:36 +11001043 options->identities_only = - 1;
Damien Millera5539d22003-04-09 20:50:06 +10001044 options->rekey_limit = - 1;
Damien Miller37876e92003-05-15 10:19:46 +10001045 options->verify_host_key_dns = -1;
Damien Miller509b0102003-12-17 16:33:10 +11001046 options->server_alive_interval = -1;
1047 options->server_alive_count_max = -1;
Darren Tucker46bc0752004-05-02 22:11:30 +10001048 options->num_send_env = 0;
Damien Miller0e220db2004-06-15 10:34:08 +10001049 options->control_path = NULL;
1050 options->control_master = -1;
Damien Millere1776152005-03-01 21:47:37 +11001051 options->hash_known_hosts = -1;
Damien Millerd27b9472005-12-13 19:29:02 +11001052 options->tun_open = -1;
1053 options->tun_local = -1;
1054 options->tun_remote = -1;
1055 options->local_command = NULL;
1056 options->permit_local_command = -1;
Damien Miller10288242008-06-30 00:04:03 +10001057 options->visual_host_key = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058}
1059
Damien Miller5428f641999-11-25 11:54:57 +11001060/*
1061 * Called after processing other sources of option data, this fills those
1062 * options for which no value has been specified with their default values.
1063 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064
Damien Miller4af51302000-04-16 11:18:38 +10001065void
Damien Miller95def091999-11-25 00:26:21 +11001066fill_default_options(Options * options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067{
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001068 int len;
1069
Damien Miller95def091999-11-25 00:26:21 +11001070 if (options->forward_agent == -1)
Damien Millerb1715dc2000-05-30 13:44:51 +10001071 options->forward_agent = 0;
Damien Miller95def091999-11-25 00:26:21 +11001072 if (options->forward_x11 == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +11001073 options->forward_x11 = 0;
Darren Tucker0a118da2003-10-15 15:54:32 +10001074 if (options->forward_x11_trusted == -1)
1075 options->forward_x11_trusted = 0;
Darren Tuckere7d4b192006-07-12 22:17:10 +10001076 if (options->exit_on_forward_failure == -1)
1077 options->exit_on_forward_failure = 0;
Damien Millerd3a18572000-06-07 19:55:44 +10001078 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +00001079 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +11001080 if (options->gateway_ports == -1)
1081 options->gateway_ports = 0;
1082 if (options->use_privileged_port == -1)
Ben Lindstromcebc8582001-03-08 03:39:10 +00001083 options->use_privileged_port = 0;
Damien Miller95def091999-11-25 00:26:21 +11001084 if (options->rsa_authentication == -1)
1085 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001086 if (options->pubkey_authentication == -1)
1087 options->pubkey_authentication = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +00001088 if (options->challenge_response_authentication == -1)
Ben Lindstrom0076d752001-08-06 20:53:26 +00001089 options->challenge_response_authentication = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +10001090 if (options->gss_authentication == -1)
Darren Tuckera044f472003-10-15 15:52:03 +10001091 options->gss_authentication = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001092 if (options->gss_deleg_creds == -1)
1093 options->gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +11001094 if (options->password_authentication == -1)
1095 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +11001096 if (options->kbd_interactive_authentication == -1)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001097 options->kbd_interactive_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +11001098 if (options->rhosts_rsa_authentication == -1)
Ben Lindstrom2bf82762002-06-11 15:53:05 +00001099 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001100 if (options->hostbased_authentication == -1)
1101 options->hostbased_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +11001102 if (options->batch_mode == -1)
1103 options->batch_mode = 0;
1104 if (options->check_host_ip == -1)
1105 options->check_host_ip = 1;
1106 if (options->strict_host_key_checking == -1)
1107 options->strict_host_key_checking = 2; /* 2 is default */
1108 if (options->compression == -1)
1109 options->compression = 0;
Damien Miller12c150e2003-12-17 16:31:10 +11001110 if (options->tcp_keep_alive == -1)
1111 options->tcp_keep_alive = 1;
Damien Miller95def091999-11-25 00:26:21 +11001112 if (options->compression_level == -1)
1113 options->compression_level = 6;
1114 if (options->port == -1)
1115 options->port = 0; /* Filled in ssh_connect. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001116 if (options->address_family == -1)
1117 options->address_family = AF_UNSPEC;
Damien Miller95def091999-11-25 00:26:21 +11001118 if (options->connection_attempts == -1)
Ben Lindstromf9cedb92001-08-06 21:07:11 +00001119 options->connection_attempts = 1;
Damien Miller95def091999-11-25 00:26:21 +11001120 if (options->number_of_password_prompts == -1)
1121 options->number_of_password_prompts = 3;
1122 /* Selected in ssh_login(). */
1123 if (options->cipher == -1)
1124 options->cipher = SSH_CIPHER_NOT_SET;
Damien Miller30c3d422000-05-09 11:02:59 +10001125 /* options->ciphers, default set in myproposals.h */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001126 /* options->macs, default set in myproposals.h */
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001127 /* options->hostkeyalgorithms, default set in myproposals.h */
Damien Miller78928792000-04-12 20:17:38 +10001128 if (options->protocol == SSH_PROTO_UNKNOWN)
Ben Lindstrom6b776432001-03-22 01:24:04 +00001129 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
Damien Miller95def091999-11-25 00:26:21 +11001130 if (options->num_identity_files == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001131 if (options->protocol & SSH_PROTO_1) {
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001132 len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001133 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001134 xmalloc(len);
1135 snprintf(options->identity_files[options->num_identity_files++],
1136 len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001137 }
1138 if (options->protocol & SSH_PROTO_2) {
Ben Lindstromb00d4fb2001-03-05 06:03:03 +00001139 len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1140 options->identity_files[options->num_identity_files] =
1141 xmalloc(len);
1142 snprintf(options->identity_files[options->num_identity_files++],
1143 len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1144
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001145 len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001146 options->identity_files[options->num_identity_files] =
Ben Lindstrom4f7a64a2001-02-10 22:50:09 +00001147 xmalloc(len);
1148 snprintf(options->identity_files[options->num_identity_files++],
1149 len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001150 }
Damien Millereba71ba2000-04-29 23:57:08 +10001151 }
Damien Miller95def091999-11-25 00:26:21 +11001152 if (options->escape_char == -1)
1153 options->escape_char = '~';
1154 if (options->system_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001155 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
Damien Miller95def091999-11-25 00:26:21 +11001156 if (options->user_hostfile == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001157 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
Damien Millereba71ba2000-04-29 23:57:08 +10001158 if (options->system_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001159 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
Damien Millereba71ba2000-04-29 23:57:08 +10001160 if (options->user_hostfile2 == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001161 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
Damien Millerfcd93202002-02-05 12:26:34 +11001162 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001163 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001164 if (options->clear_forwardings == 1)
1165 clear_forwardings(options);
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +00001166 if (options->no_host_authentication_for_localhost == - 1)
1167 options->no_host_authentication_for_localhost = 0;
Damien Millerbd394c32004-03-08 23:12:36 +11001168 if (options->identities_only == -1)
1169 options->identities_only = 0;
Ben Lindstromb6df73b2002-11-09 15:52:31 +00001170 if (options->enable_ssh_keysign == -1)
1171 options->enable_ssh_keysign = 0;
Damien Millera5539d22003-04-09 20:50:06 +10001172 if (options->rekey_limit == -1)
1173 options->rekey_limit = 0;
Damien Miller37876e92003-05-15 10:19:46 +10001174 if (options->verify_host_key_dns == -1)
1175 options->verify_host_key_dns = 0;
Damien Miller509b0102003-12-17 16:33:10 +11001176 if (options->server_alive_interval == -1)
1177 options->server_alive_interval = 0;
1178 if (options->server_alive_count_max == -1)
1179 options->server_alive_count_max = 3;
Damien Miller0e220db2004-06-15 10:34:08 +10001180 if (options->control_master == -1)
1181 options->control_master = 0;
Damien Millere1776152005-03-01 21:47:37 +11001182 if (options->hash_known_hosts == -1)
1183 options->hash_known_hosts = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001184 if (options->tun_open == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001185 options->tun_open = SSH_TUNMODE_NO;
1186 if (options->tun_local == -1)
1187 options->tun_local = SSH_TUNID_ANY;
1188 if (options->tun_remote == -1)
1189 options->tun_remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +11001190 if (options->permit_local_command == -1)
1191 options->permit_local_command = 0;
Damien Miller10288242008-06-30 00:04:03 +10001192 if (options->visual_host_key == -1)
1193 options->visual_host_key = 0;
Damien Millerd27b9472005-12-13 19:29:02 +11001194 /* options->local_command should not be set by default */
Damien Miller95def091999-11-25 00:26:21 +11001195 /* options->proxy_command should not be set by default */
1196 /* options->user will be set in the main program if appropriate */
1197 /* options->hostname will be set in the main program if appropriate */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +00001198 /* options->host_key_alias should not be set by default */
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001199 /* options->preferred_authentications will be set in ssh */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200}
Damien Millerf91ee4c2005-03-01 21:24:33 +11001201
1202/*
1203 * parse_forward
1204 * parses a string containing a port forwarding specification of the form:
Damien Millera699d952008-11-03 19:27:34 +11001205 * dynamicfwd == 0
Damien Millerf91ee4c2005-03-01 21:24:33 +11001206 * [listenhost:]listenport:connecthost:connectport
Damien Millera699d952008-11-03 19:27:34 +11001207 * dynamicfwd == 1
1208 * [listenhost:]listenport
Damien Millerf91ee4c2005-03-01 21:24:33 +11001209 * returns number of arguments parsed or zero on error
1210 */
1211int
Damien Millera699d952008-11-03 19:27:34 +11001212parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001213{
1214 int i;
1215 char *p, *cp, *fwdarg[4];
1216
1217 memset(fwd, '\0', sizeof(*fwd));
1218
1219 cp = p = xstrdup(fwdspec);
1220
1221 /* skip leading spaces */
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001222 while (isspace(*cp))
Damien Millerf91ee4c2005-03-01 21:24:33 +11001223 cp++;
1224
1225 for (i = 0; i < 4; ++i)
1226 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1227 break;
1228
1229 /* Check for trailing garbage in 4-arg case*/
1230 if (cp != NULL)
1231 i = 0; /* failure */
1232
1233 switch (i) {
Damien Millera699d952008-11-03 19:27:34 +11001234 case 1:
1235 fwd->listen_host = NULL;
1236 fwd->listen_port = a2port(fwdarg[0]);
1237 fwd->connect_host = xstrdup("socks");
1238 break;
1239
1240 case 2:
1241 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1242 fwd->listen_port = a2port(fwdarg[1]);
1243 fwd->connect_host = xstrdup("socks");
1244 break;
1245
Damien Millerf91ee4c2005-03-01 21:24:33 +11001246 case 3:
1247 fwd->listen_host = NULL;
1248 fwd->listen_port = a2port(fwdarg[0]);
1249 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1250 fwd->connect_port = a2port(fwdarg[2]);
1251 break;
1252
1253 case 4:
1254 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1255 fwd->listen_port = a2port(fwdarg[1]);
1256 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1257 fwd->connect_port = a2port(fwdarg[3]);
1258 break;
1259 default:
1260 i = 0; /* failure */
1261 }
1262
1263 xfree(p);
1264
Damien Millera699d952008-11-03 19:27:34 +11001265 if (dynamicfwd) {
1266 if (!(i == 1 || i == 2))
1267 goto fail_free;
1268 } else {
1269 if (!(i == 3 || i == 4))
1270 goto fail_free;
1271 if (fwd->connect_port == 0)
1272 goto fail_free;
1273 }
1274
1275 if (fwd->listen_port == 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001276 goto fail_free;
1277
1278 if (fwd->connect_host != NULL &&
1279 strlen(fwd->connect_host) >= NI_MAXHOST)
1280 goto fail_free;
1281
1282 return (i);
1283
1284 fail_free:
1285 if (fwd->connect_host != NULL)
1286 xfree(fwd->connect_host);
1287 if (fwd->listen_host != NULL)
1288 xfree(fwd->listen_host);
1289 return (0);
1290}