readconf.c revision 1.34 1 1.30 kim /* $NetBSD: readconf.c,v 1.34 2021/04/19 14:40:15 christos Exp $ */
2 1.34 christos /* $OpenBSD: readconf.c,v 1.353 2021/04/03 06:18:40 djm Exp $ */
3 1.32 christos
4 1.1 christos /*
5 1.1 christos * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
6 1.1 christos * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
7 1.1 christos * All rights reserved
8 1.1 christos * Functions for reading the configuration files.
9 1.1 christos *
10 1.1 christos * As far as I am concerned, the code I have written for this software
11 1.1 christos * can be used freely for any purpose. Any derived versions of this
12 1.1 christos * software must be clearly marked as such, and if the derived work is
13 1.1 christos * incompatible with the protocol description in the RFC file, it must be
14 1.1 christos * called by a name other than "ssh" or "Secure Shell".
15 1.1 christos */
16 1.1 christos
17 1.2 christos #include "includes.h"
18 1.30 kim __RCSID("$NetBSD: readconf.c,v 1.34 2021/04/19 14:40:15 christos Exp $");
19 1.1 christos #include <sys/types.h>
20 1.1 christos #include <sys/stat.h>
21 1.1 christos #include <sys/socket.h>
22 1.12 christos #include <sys/wait.h>
23 1.12 christos #include <sys/un.h>
24 1.1 christos
25 1.1 christos #include <netinet/in.h>
26 1.5 christos #include <netinet/ip.h>
27 1.1 christos
28 1.1 christos #include <ctype.h>
29 1.1 christos #include <errno.h>
30 1.12 christos #include <fcntl.h>
31 1.19 christos #include <glob.h>
32 1.1 christos #include <netdb.h>
33 1.12 christos #include <paths.h>
34 1.12 christos #include <pwd.h>
35 1.1 christos #include <signal.h>
36 1.1 christos #include <stdio.h>
37 1.1 christos #include <string.h>
38 1.29 christos #include <stdarg.h>
39 1.1 christos #include <unistd.h>
40 1.2 christos #include <limits.h>
41 1.11 christos #include <util.h>
42 1.13 christos #include <vis.h>
43 1.1 christos
44 1.1 christos #include "xmalloc.h"
45 1.1 christos #include "ssh.h"
46 1.25 christos #include "ssherr.h"
47 1.1 christos #include "compat.h"
48 1.1 christos #include "cipher.h"
49 1.1 christos #include "pathnames.h"
50 1.1 christos #include "log.h"
51 1.13 christos #include "sshkey.h"
52 1.12 christos #include "misc.h"
53 1.1 christos #include "readconf.h"
54 1.1 christos #include "match.h"
55 1.1 christos #include "kex.h"
56 1.1 christos #include "mac.h"
57 1.11 christos #include "fmt_scaled.h"
58 1.12 christos #include "uidswap.h"
59 1.13 christos #include "myproposal.h"
60 1.13 christos #include "digest.h"
61 1.1 christos
62 1.1 christos /* Format of the configuration file:
63 1.1 christos
64 1.1 christos # Configuration data is parsed as follows:
65 1.1 christos # 1. command line options
66 1.1 christos # 2. user-specific file
67 1.1 christos # 3. system-wide file
68 1.1 christos # Any configuration value is only changed the first time it is set.
69 1.1 christos # Thus, host-specific definitions should be at the beginning of the
70 1.1 christos # configuration file, and defaults at the end.
71 1.1 christos
72 1.1 christos # Host-specific declarations. These may override anything above. A single
73 1.1 christos # host may match multiple declarations; these are processed in the order
74 1.1 christos # that they are given in.
75 1.1 christos
76 1.1 christos Host *.ngs.fi ngs.fi
77 1.1 christos User foo
78 1.1 christos
79 1.1 christos Host fake.com
80 1.28 christos Hostname another.host.name.real.org
81 1.1 christos User blaah
82 1.1 christos Port 34289
83 1.1 christos ForwardX11 no
84 1.1 christos ForwardAgent no
85 1.1 christos
86 1.1 christos Host books.com
87 1.1 christos RemoteForward 9999 shadows.cs.hut.fi:9999
88 1.21 christos Ciphers 3des-cbc
89 1.1 christos
90 1.1 christos Host fascist.blob.com
91 1.1 christos Port 23123
92 1.1 christos User tylonen
93 1.1 christos PasswordAuthentication no
94 1.1 christos
95 1.1 christos Host puukko.hut.fi
96 1.1 christos User t35124p
97 1.1 christos ProxyCommand ssh-proxy %h %p
98 1.1 christos
99 1.1 christos Host *.fr
100 1.1 christos PublicKeyAuthentication no
101 1.1 christos
102 1.1 christos Host *.su
103 1.21 christos Ciphers aes128-ctr
104 1.1 christos PasswordAuthentication no
105 1.1 christos
106 1.1 christos Host vpn.fake.com
107 1.1 christos Tunnel yes
108 1.1 christos TunnelDevice 3
109 1.1 christos
110 1.1 christos # Defaults for various options
111 1.1 christos Host *
112 1.1 christos ForwardAgent no
113 1.1 christos ForwardX11 no
114 1.1 christos PasswordAuthentication yes
115 1.1 christos StrictHostKeyChecking yes
116 1.1 christos TcpKeepAlive no
117 1.1 christos IdentityFile ~/.ssh/identity
118 1.1 christos Port 22
119 1.1 christos EscapeChar ~
120 1.1 christos
121 1.1 christos */
122 1.1 christos
123 1.19 christos static int read_config_file_depth(const char *filename, struct passwd *pw,
124 1.19 christos const char *host, const char *original_host, Options *options,
125 1.27 christos int flags, int *activep, int *want_final_pass, int depth);
126 1.19 christos static int process_config_line_depth(Options *options, struct passwd *pw,
127 1.19 christos const char *host, const char *original_host, char *line,
128 1.27 christos const char *filename, int linenum, int *activep, int flags,
129 1.27 christos int *want_final_pass, int depth);
130 1.19 christos
131 1.1 christos /* Keyword tokens. */
132 1.1 christos
133 1.1 christos typedef enum {
134 1.1 christos oBadOption,
135 1.19 christos oHost, oMatch, oInclude,
136 1.4 adam oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
137 1.4 adam oGatewayPorts, oExitOnForwardFailure,
138 1.29 christos oPasswordAuthentication,
139 1.1 christos oChallengeResponseAuthentication, oXAuthLocation,
140 1.2 christos #if defined(KRB4) || defined(KRB5)
141 1.2 christos oKerberosAuthentication,
142 1.2 christos #endif
143 1.2 christos #if defined(AFS) || defined(KRB5)
144 1.2 christos oKerberosTgtPassing,
145 1.2 christos #endif
146 1.2 christos #ifdef AFS
147 1.2 christos oAFSTokenPassing,
148 1.2 christos #endif
149 1.29 christos oIdentityFile, oHostname, oPort, oRemoteForward, oLocalForward,
150 1.33 christos oPermitRemoteOpen,
151 1.19 christos oCertificateFile, oAddKeysToAgent, oIdentityAgent,
152 1.29 christos oUser, oEscapeChar, oProxyCommand,
153 1.1 christos oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
154 1.1 christos oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
155 1.29 christos oTCPKeepAlive, oNumberOfPasswordPrompts,
156 1.33 christos oLogFacility, oLogLevel, oLogVerbose, oCiphers, oMacs,
157 1.13 christos oPubkeyAuthentication,
158 1.1 christos oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
159 1.1 christos oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
160 1.23 christos oHostKeyAlgorithms, oBindAddress, oBindInterface, oPKCS11Provider,
161 1.1 christos oClearAllForwardings, oNoHostAuthenticationForLocalhost,
162 1.1 christos oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
163 1.1 christos oAddressFamily, oGssAuthentication, oGssDelegateCreds,
164 1.1 christos oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
165 1.25 christos oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
166 1.4 adam oHashKnownHosts,
167 1.22 christos oTunnel, oTunnelDevice,
168 1.22 christos oLocalCommand, oPermitLocalCommand, oRemoteCommand,
169 1.17 christos oVisualHostKey,
170 1.12 christos oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
171 1.12 christos oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
172 1.12 christos oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs,
173 1.13 christos oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys,
174 1.33 christos oFingerprintHash, oUpdateHostkeys, oHostbasedAcceptedAlgorithms,
175 1.33 christos oPubkeyAcceptedAlgorithms, oCASignatureAlgorithms, oProxyJump,
176 1.33 christos oSecurityKeyProvider, oKnownHostsCommand,
177 1.2 christos oNoneEnabled, oTcpRcvBufPoll, oTcpRcvBuf, oNoneSwitch, oHPNDisabled,
178 1.2 christos oHPNBufferSize,
179 1.7 tls oSendVersionFirst,
180 1.22 christos oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
181 1.1 christos } OpCodes;
182 1.1 christos
183 1.1 christos /* Textual representations of the tokens. */
184 1.1 christos
185 1.1 christos static struct {
186 1.1 christos const char *name;
187 1.1 christos OpCodes opcode;
188 1.1 christos } keywords[] = {
189 1.21 christos /* Deprecated options */
190 1.22 christos { "protocol", oIgnore }, /* NB. silently ignored */
191 1.22 christos { "cipher", oDeprecated },
192 1.21 christos { "fallbacktorsh", oDeprecated },
193 1.21 christos { "globalknownhostsfile2", oDeprecated },
194 1.21 christos { "rhostsauthentication", oDeprecated },
195 1.21 christos { "userknownhostsfile2", oDeprecated },
196 1.21 christos { "useroaming", oDeprecated },
197 1.21 christos { "usersh", oDeprecated },
198 1.25 christos { "useprivilegedport", oDeprecated },
199 1.21 christos
200 1.21 christos /* Unsupported options */
201 1.30 kim #ifdef AFS
202 1.30 kim { "afstokenpassing", oAFSTokenPassing },
203 1.30 kim #else
204 1.21 christos { "afstokenpassing", oUnsupported },
205 1.30 kim #endif
206 1.30 kim #if defined(KRB4) || defined(KRB5)
207 1.30 kim { "kerberosauthentication", oKerberosAuthentication },
208 1.30 kim #else
209 1.21 christos { "kerberosauthentication", oUnsupported },
210 1.30 kim #endif
211 1.30 kim #if defined(AFS) || defined(KRB5)
212 1.30 kim { "kerberostgtpassing", oKerberosTgtPassing },
213 1.30 kim { "kerberos5tgtpassing", oKerberosTgtPassing }, /* alias */
214 1.30 kim { "kerberos4tgtpassing", oKerberosTgtPassing }, /* alias */
215 1.30 kim #else
216 1.21 christos { "kerberostgtpassing", oUnsupported },
217 1.30 kim { "kerberos5tgtpassing", oUnsupported },
218 1.30 kim { "kerberos4tgtpassing", oUnsupported },
219 1.30 kim #endif
220 1.29 christos { "rsaauthentication", oUnsupported },
221 1.29 christos { "rhostsrsaauthentication", oUnsupported },
222 1.29 christos { "compressionlevel", oUnsupported },
223 1.21 christos
224 1.21 christos /* Sometimes-unsupported options */
225 1.21 christos #if defined(GSSAPI)
226 1.21 christos { "gssapiauthentication", oGssAuthentication },
227 1.21 christos { "gssapidelegatecredentials", oGssDelegateCreds },
228 1.21 christos # else
229 1.21 christos { "gssapiauthentication", oUnsupported },
230 1.21 christos { "gssapidelegatecredentials", oUnsupported },
231 1.21 christos #endif
232 1.21 christos #ifdef ENABLE_PKCS11
233 1.27 christos { "pkcs11provider", oPKCS11Provider },
234 1.21 christos { "smartcarddevice", oPKCS11Provider },
235 1.21 christos # else
236 1.21 christos { "smartcarddevice", oUnsupported },
237 1.21 christos { "pkcs11provider", oUnsupported },
238 1.21 christos #endif
239 1.21 christos
240 1.1 christos { "forwardagent", oForwardAgent },
241 1.1 christos { "forwardx11", oForwardX11 },
242 1.1 christos { "forwardx11trusted", oForwardX11Trusted },
243 1.4 adam { "forwardx11timeout", oForwardX11Timeout },
244 1.1 christos { "exitonforwardfailure", oExitOnForwardFailure },
245 1.1 christos { "xauthlocation", oXAuthLocation },
246 1.1 christos { "gatewayports", oGatewayPorts },
247 1.1 christos { "passwordauthentication", oPasswordAuthentication },
248 1.1 christos { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
249 1.1 christos { "kbdinteractivedevices", oKbdInteractiveDevices },
250 1.1 christos { "pubkeyauthentication", oPubkeyAuthentication },
251 1.1 christos { "dsaauthentication", oPubkeyAuthentication }, /* alias */
252 1.1 christos { "hostbasedauthentication", oHostbasedAuthentication },
253 1.1 christos { "challengeresponseauthentication", oChallengeResponseAuthentication },
254 1.1 christos { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
255 1.1 christos { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
256 1.1 christos #if defined(GSSAPI)
257 1.1 christos { "gssapiauthentication", oGssAuthentication },
258 1.1 christos { "gssapidelegatecredentials", oGssDelegateCreds },
259 1.1 christos #else
260 1.1 christos { "gssapiauthentication", oUnsupported },
261 1.1 christos { "gssapidelegatecredentials", oUnsupported },
262 1.1 christos #endif
263 1.1 christos { "identityfile", oIdentityFile },
264 1.1 christos { "identityfile2", oIdentityFile }, /* obsolete */
265 1.1 christos { "identitiesonly", oIdentitiesOnly },
266 1.18 christos { "certificatefile", oCertificateFile },
267 1.18 christos { "addkeystoagent", oAddKeysToAgent },
268 1.19 christos { "identityagent", oIdentityAgent },
269 1.28 christos { "hostname", oHostname },
270 1.1 christos { "hostkeyalias", oHostKeyAlias },
271 1.1 christos { "proxycommand", oProxyCommand },
272 1.1 christos { "port", oPort },
273 1.1 christos { "ciphers", oCiphers },
274 1.1 christos { "macs", oMacs },
275 1.1 christos { "remoteforward", oRemoteForward },
276 1.1 christos { "localforward", oLocalForward },
277 1.33 christos { "permitremoteopen", oPermitRemoteOpen },
278 1.1 christos { "user", oUser },
279 1.1 christos { "host", oHost },
280 1.12 christos { "match", oMatch },
281 1.1 christos { "escapechar", oEscapeChar },
282 1.1 christos { "globalknownhostsfile", oGlobalKnownHostsFile },
283 1.1 christos { "userknownhostsfile", oUserKnownHostsFile },
284 1.1 christos { "connectionattempts", oConnectionAttempts },
285 1.1 christos { "batchmode", oBatchMode },
286 1.1 christos { "checkhostip", oCheckHostIP },
287 1.1 christos { "stricthostkeychecking", oStrictHostKeyChecking },
288 1.1 christos { "compression", oCompression },
289 1.1 christos { "tcpkeepalive", oTCPKeepAlive },
290 1.1 christos { "keepalive", oTCPKeepAlive }, /* obsolete */
291 1.1 christos { "numberofpasswordprompts", oNumberOfPasswordPrompts },
292 1.22 christos { "syslogfacility", oLogFacility },
293 1.1 christos { "loglevel", oLogLevel },
294 1.33 christos { "logverbose", oLogVerbose },
295 1.1 christos { "dynamicforward", oDynamicForward },
296 1.1 christos { "preferredauthentications", oPreferredAuthentications },
297 1.1 christos { "hostkeyalgorithms", oHostKeyAlgorithms },
298 1.27 christos { "casignaturealgorithms", oCASignatureAlgorithms },
299 1.1 christos { "bindaddress", oBindAddress },
300 1.23 christos { "bindinterface", oBindInterface },
301 1.1 christos { "clearallforwardings", oClearAllForwardings },
302 1.1 christos { "enablesshkeysign", oEnableSSHKeysign },
303 1.1 christos { "verifyhostkeydns", oVerifyHostKeyDNS },
304 1.1 christos { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
305 1.1 christos { "rekeylimit", oRekeyLimit },
306 1.1 christos { "connecttimeout", oConnectTimeout },
307 1.1 christos { "addressfamily", oAddressFamily },
308 1.1 christos { "serveraliveinterval", oServerAliveInterval },
309 1.1 christos { "serveralivecountmax", oServerAliveCountMax },
310 1.1 christos { "sendenv", oSendEnv },
311 1.25 christos { "setenv", oSetEnv },
312 1.1 christos { "controlpath", oControlPath },
313 1.1 christos { "controlmaster", oControlMaster },
314 1.4 adam { "controlpersist", oControlPersist },
315 1.1 christos { "hashknownhosts", oHashKnownHosts },
316 1.19 christos { "include", oInclude },
317 1.1 christos { "tunnel", oTunnel },
318 1.1 christos { "tunneldevice", oTunnelDevice },
319 1.1 christos { "localcommand", oLocalCommand },
320 1.1 christos { "permitlocalcommand", oPermitLocalCommand },
321 1.22 christos { "remotecommand", oRemoteCommand },
322 1.1 christos { "visualhostkey", oVisualHostKey },
323 1.5 christos { "kexalgorithms", oKexAlgorithms },
324 1.5 christos { "ipqos", oIPQoS },
325 1.6 christos { "requesttty", oRequestTTY },
326 1.12 christos { "proxyusefdpass", oProxyUseFdpass },
327 1.12 christos { "canonicaldomains", oCanonicalDomains },
328 1.12 christos { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal },
329 1.12 christos { "canonicalizehostname", oCanonicalizeHostname },
330 1.12 christos { "canonicalizemaxdots", oCanonicalizeMaxDots },
331 1.12 christos { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs },
332 1.12 christos { "streamlocalbindmask", oStreamLocalBindMask },
333 1.12 christos { "streamlocalbindunlink", oStreamLocalBindUnlink },
334 1.13 christos { "revokedhostkeys", oRevokedHostKeys },
335 1.13 christos { "fingerprinthash", oFingerprintHash },
336 1.13 christos { "updatehostkeys", oUpdateHostkeys },
337 1.33 christos { "hostbasedalgorithms", oHostbasedAcceptedAlgorithms },
338 1.33 christos { "hostbasedkeytypes", oHostbasedAcceptedAlgorithms }, /* obsolete */
339 1.33 christos { "pubkeyacceptedalgorithms", oPubkeyAcceptedAlgorithms },
340 1.33 christos { "pubkeyacceptedkeytypes", oPubkeyAcceptedAlgorithms }, /* obsolete */
341 1.19 christos { "proxyjump", oProxyJump },
342 1.2 christos { "noneenabled", oNoneEnabled },
343 1.2 christos { "tcprcvbufpoll", oTcpRcvBufPoll },
344 1.2 christos { "tcprcvbuf", oTcpRcvBuf },
345 1.2 christos { "noneswitch", oNoneSwitch },
346 1.2 christos { "hpndisabled", oHPNDisabled },
347 1.2 christos { "hpnbuffersize", oHPNBufferSize },
348 1.7 tls { "sendversionfirst", oSendVersionFirst },
349 1.12 christos { "ignoreunknown", oIgnoreUnknown },
350 1.29 christos { "proxyjump", oProxyJump },
351 1.29 christos { "securitykeyprovider", oSecurityKeyProvider },
352 1.33 christos { "knownhostscommand", oKnownHostsCommand },
353 1.19 christos
354 1.1 christos { NULL, oBadOption }
355 1.1 christos };
356 1.1 christos
357 1.33 christos static const char *lookup_opcode_name(OpCodes code);
358 1.29 christos
359 1.29 christos const char *
360 1.29 christos kex_default_pk_alg(void)
361 1.29 christos {
362 1.33 christos static char *pkalgs;
363 1.33 christos
364 1.33 christos if (pkalgs == NULL) {
365 1.33 christos char *all_key;
366 1.33 christos
367 1.33 christos all_key = sshkey_alg_list(0, 0, 1, ',');
368 1.33 christos pkalgs = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
369 1.33 christos free(all_key);
370 1.33 christos }
371 1.33 christos return pkalgs;
372 1.29 christos }
373 1.29 christos
374 1.31 christos char *
375 1.31 christos ssh_connection_hash(const char *thishost, const char *host, const char *portstr,
376 1.31 christos const char *user)
377 1.31 christos {
378 1.31 christos struct ssh_digest_ctx *md;
379 1.31 christos u_char conn_hash[SSH_DIGEST_MAX_LENGTH];
380 1.31 christos
381 1.31 christos if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL ||
382 1.31 christos ssh_digest_update(md, thishost, strlen(thishost)) < 0 ||
383 1.31 christos ssh_digest_update(md, host, strlen(host)) < 0 ||
384 1.31 christos ssh_digest_update(md, portstr, strlen(portstr)) < 0 ||
385 1.31 christos ssh_digest_update(md, user, strlen(user)) < 0 ||
386 1.31 christos ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0)
387 1.33 christos fatal_f("mux digest failed");
388 1.31 christos ssh_digest_free(md);
389 1.31 christos return tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1));
390 1.31 christos }
391 1.31 christos
392 1.1 christos /*
393 1.1 christos * Adds a local TCP/IP port forward to options. Never returns if there is an
394 1.1 christos * error.
395 1.1 christos */
396 1.1 christos
397 1.1 christos void
398 1.12 christos add_local_forward(Options *options, const struct Forward *newfwd)
399 1.1 christos {
400 1.12 christos struct Forward *fwd;
401 1.19 christos int i;
402 1.4 adam
403 1.19 christos /* Don't add duplicates */
404 1.19 christos for (i = 0; i < options->num_local_forwards; i++) {
405 1.19 christos if (forward_equals(newfwd, options->local_forwards + i))
406 1.19 christos return;
407 1.19 christos }
408 1.14 christos options->local_forwards = xreallocarray(options->local_forwards,
409 1.4 adam options->num_local_forwards + 1,
410 1.4 adam sizeof(*options->local_forwards));
411 1.1 christos fwd = &options->local_forwards[options->num_local_forwards++];
412 1.1 christos
413 1.1 christos fwd->listen_host = newfwd->listen_host;
414 1.1 christos fwd->listen_port = newfwd->listen_port;
415 1.12 christos fwd->listen_path = newfwd->listen_path;
416 1.1 christos fwd->connect_host = newfwd->connect_host;
417 1.1 christos fwd->connect_port = newfwd->connect_port;
418 1.12 christos fwd->connect_path = newfwd->connect_path;
419 1.1 christos }
420 1.1 christos
421 1.1 christos /*
422 1.1 christos * Adds a remote TCP/IP port forward to options. Never returns if there is
423 1.1 christos * an error.
424 1.1 christos */
425 1.1 christos
426 1.1 christos void
427 1.12 christos add_remote_forward(Options *options, const struct Forward *newfwd)
428 1.1 christos {
429 1.12 christos struct Forward *fwd;
430 1.19 christos int i;
431 1.4 adam
432 1.19 christos /* Don't add duplicates */
433 1.19 christos for (i = 0; i < options->num_remote_forwards; i++) {
434 1.19 christos if (forward_equals(newfwd, options->remote_forwards + i))
435 1.19 christos return;
436 1.19 christos }
437 1.14 christos options->remote_forwards = xreallocarray(options->remote_forwards,
438 1.4 adam options->num_remote_forwards + 1,
439 1.4 adam sizeof(*options->remote_forwards));
440 1.1 christos fwd = &options->remote_forwards[options->num_remote_forwards++];
441 1.1 christos
442 1.1 christos fwd->listen_host = newfwd->listen_host;
443 1.1 christos fwd->listen_port = newfwd->listen_port;
444 1.12 christos fwd->listen_path = newfwd->listen_path;
445 1.1 christos fwd->connect_host = newfwd->connect_host;
446 1.1 christos fwd->connect_port = newfwd->connect_port;
447 1.12 christos fwd->connect_path = newfwd->connect_path;
448 1.8 christos fwd->handle = newfwd->handle;
449 1.4 adam fwd->allocated_port = 0;
450 1.1 christos }
451 1.1 christos
452 1.1 christos static void
453 1.1 christos clear_forwardings(Options *options)
454 1.1 christos {
455 1.1 christos int i;
456 1.1 christos
457 1.1 christos for (i = 0; i < options->num_local_forwards; i++) {
458 1.11 christos free(options->local_forwards[i].listen_host);
459 1.12 christos free(options->local_forwards[i].listen_path);
460 1.11 christos free(options->local_forwards[i].connect_host);
461 1.12 christos free(options->local_forwards[i].connect_path);
462 1.1 christos }
463 1.4 adam if (options->num_local_forwards > 0) {
464 1.11 christos free(options->local_forwards);
465 1.4 adam options->local_forwards = NULL;
466 1.4 adam }
467 1.1 christos options->num_local_forwards = 0;
468 1.1 christos for (i = 0; i < options->num_remote_forwards; i++) {
469 1.11 christos free(options->remote_forwards[i].listen_host);
470 1.12 christos free(options->remote_forwards[i].listen_path);
471 1.11 christos free(options->remote_forwards[i].connect_host);
472 1.12 christos free(options->remote_forwards[i].connect_path);
473 1.1 christos }
474 1.4 adam if (options->num_remote_forwards > 0) {
475 1.11 christos free(options->remote_forwards);
476 1.4 adam options->remote_forwards = NULL;
477 1.4 adam }
478 1.1 christos options->num_remote_forwards = 0;
479 1.1 christos options->tun_open = SSH_TUNMODE_NO;
480 1.1 christos }
481 1.1 christos
482 1.9 mlelstv void
483 1.18 christos add_certificate_file(Options *options, const char *path, int userprovided)
484 1.18 christos {
485 1.18 christos int i;
486 1.18 christos
487 1.18 christos if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES)
488 1.18 christos fatal("Too many certificate files specified (max %d)",
489 1.18 christos SSH_MAX_CERTIFICATE_FILES);
490 1.18 christos
491 1.18 christos /* Avoid registering duplicates */
492 1.18 christos for (i = 0; i < options->num_certificate_files; i++) {
493 1.18 christos if (options->certificate_file_userprovided[i] == userprovided &&
494 1.18 christos strcmp(options->certificate_files[i], path) == 0) {
495 1.33 christos debug2_f("ignoring duplicate key %s", path);
496 1.18 christos return;
497 1.18 christos }
498 1.18 christos }
499 1.18 christos
500 1.18 christos options->certificate_file_userprovided[options->num_certificate_files] =
501 1.18 christos userprovided;
502 1.18 christos options->certificate_files[options->num_certificate_files++] =
503 1.18 christos xstrdup(path);
504 1.18 christos }
505 1.18 christos
506 1.18 christos void
507 1.9 mlelstv add_identity_file(Options *options, const char *dir, const char *filename,
508 1.9 mlelstv int userprovided)
509 1.9 mlelstv {
510 1.9 mlelstv char *path;
511 1.12 christos int i;
512 1.9 mlelstv
513 1.9 mlelstv if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
514 1.9 mlelstv fatal("Too many identity files specified (max %d)",
515 1.9 mlelstv SSH_MAX_IDENTITY_FILES);
516 1.9 mlelstv
517 1.9 mlelstv if (dir == NULL) /* no dir, filename is absolute */
518 1.9 mlelstv path = xstrdup(filename);
519 1.22 christos else if (xasprintf(&path, "%s%s", dir, filename) >= PATH_MAX)
520 1.22 christos fatal("Identity file path %s too long", path);
521 1.9 mlelstv
522 1.12 christos /* Avoid registering duplicates */
523 1.12 christos for (i = 0; i < options->num_identity_files; i++) {
524 1.12 christos if (options->identity_file_userprovided[i] == userprovided &&
525 1.12 christos strcmp(options->identity_files[i], path) == 0) {
526 1.33 christos debug2_f("ignoring duplicate key %s", path);
527 1.12 christos free(path);
528 1.12 christos return;
529 1.12 christos }
530 1.12 christos }
531 1.12 christos
532 1.9 mlelstv options->identity_file_userprovided[options->num_identity_files] =
533 1.9 mlelstv userprovided;
534 1.9 mlelstv options->identity_files[options->num_identity_files++] = path;
535 1.9 mlelstv }
536 1.9 mlelstv
537 1.12 christos int
538 1.12 christos default_ssh_port(void)
539 1.12 christos {
540 1.12 christos static int port;
541 1.12 christos struct servent *sp;
542 1.12 christos
543 1.12 christos if (port == 0) {
544 1.12 christos sp = getservbyname(SSH_SERVICE_NAME, "tcp");
545 1.12 christos port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
546 1.12 christos }
547 1.12 christos return port;
548 1.12 christos }
549 1.12 christos
550 1.12 christos /*
551 1.12 christos * Execute a command in a shell.
552 1.12 christos * Return its exit status or -1 on abnormal exit.
553 1.12 christos */
554 1.12 christos static int
555 1.12 christos execute_in_shell(const char *cmd)
556 1.12 christos {
557 1.12 christos const char *shell;
558 1.12 christos pid_t pid;
559 1.33 christos int status;
560 1.12 christos
561 1.12 christos if ((shell = getenv("SHELL")) == NULL)
562 1.12 christos shell = _PATH_BSHELL;
563 1.12 christos
564 1.28 christos if (access(shell, X_OK) == -1) {
565 1.28 christos fatal("Shell \"%s\" is not executable: %s",
566 1.28 christos shell, strerror(errno));
567 1.28 christos }
568 1.28 christos
569 1.12 christos debug("Executing command: '%.500s'", cmd);
570 1.12 christos
571 1.12 christos /* Fork and execute the command. */
572 1.12 christos if ((pid = fork()) == 0) {
573 1.12 christos char *argv[4];
574 1.12 christos
575 1.33 christos if (stdfd_devnull(1, 1, 0) == -1)
576 1.33 christos fatal_f("stdfd_devnull failed");
577 1.33 christos closefrom(STDERR_FILENO + 1);
578 1.12 christos
579 1.12 christos argv[0] = __UNCONST(shell);
580 1.12 christos argv[1] = __UNCONST("-c");
581 1.18 christos argv[2] = xstrdup(cmd);
582 1.12 christos argv[3] = NULL;
583 1.12 christos
584 1.12 christos execv(argv[0], argv);
585 1.12 christos error("Unable to execute '%.100s': %s", cmd, strerror(errno));
586 1.12 christos /* Die with signal to make this error apparent to parent. */
587 1.29 christos ssh_signal(SIGTERM, SIG_DFL);
588 1.12 christos kill(getpid(), SIGTERM);
589 1.12 christos _exit(1);
590 1.12 christos }
591 1.12 christos /* Parent. */
592 1.28 christos if (pid == -1)
593 1.33 christos fatal_f("fork: %.100s", strerror(errno));
594 1.12 christos
595 1.12 christos while (waitpid(pid, &status, 0) == -1) {
596 1.12 christos if (errno != EINTR && errno != EAGAIN)
597 1.33 christos fatal_f("waitpid: %s", strerror(errno));
598 1.12 christos }
599 1.12 christos if (!WIFEXITED(status)) {
600 1.12 christos error("command '%.100s' exited abnormally", cmd);
601 1.12 christos return -1;
602 1.13 christos }
603 1.12 christos debug3("command returned status %d", WEXITSTATUS(status));
604 1.12 christos return WEXITSTATUS(status);
605 1.12 christos }
606 1.12 christos
607 1.12 christos /*
608 1.12 christos * Parse and execute a Match directive.
609 1.12 christos */
610 1.12 christos static int
611 1.12 christos match_cfg_line(Options *options, char **condition, struct passwd *pw,
612 1.27 christos const char *host_arg, const char *original_host, int final_pass,
613 1.27 christos int *want_final_pass, const char *filename, int linenum)
614 1.12 christos {
615 1.13 christos char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria;
616 1.12 christos const char *ruser;
617 1.13 christos int r, port, this_result, result = 1, attributes = 0, negate;
618 1.12 christos char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
619 1.25 christos char uidstr[32];
620 1.12 christos
621 1.12 christos /*
622 1.12 christos * Configuration is likely to be incomplete at this point so we
623 1.12 christos * must be prepared to use default values.
624 1.12 christos */
625 1.12 christos port = options->port <= 0 ? default_ssh_port() : options->port;
626 1.12 christos ruser = options->user == NULL ? pw->pw_name : options->user;
627 1.27 christos if (final_pass) {
628 1.18 christos host = xstrdup(options->hostname);
629 1.18 christos } else if (options->hostname != NULL) {
630 1.12 christos /* NB. Please keep in sync with ssh.c:main() */
631 1.12 christos host = percent_expand(options->hostname,
632 1.12 christos "h", host_arg, (char *)NULL);
633 1.18 christos } else {
634 1.12 christos host = xstrdup(host_arg);
635 1.18 christos }
636 1.12 christos
637 1.13 christos debug2("checking match for '%s' host %s originally %s",
638 1.13 christos cp, host, original_host);
639 1.13 christos while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') {
640 1.13 christos criteria = NULL;
641 1.13 christos this_result = 1;
642 1.13 christos if ((negate = attrib[0] == '!'))
643 1.13 christos attrib++;
644 1.13 christos /* criteria "all" and "canonical" have no argument */
645 1.12 christos if (strcasecmp(attrib, "all") == 0) {
646 1.13 christos if (attributes > 1 ||
647 1.12 christos ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
648 1.13 christos error("%.200s line %d: '%s' cannot be combined "
649 1.13 christos "with other Match attributes",
650 1.13 christos filename, linenum, oattrib);
651 1.12 christos result = -1;
652 1.12 christos goto out;
653 1.12 christos }
654 1.13 christos if (result)
655 1.13 christos result = negate ? 0 : 1;
656 1.12 christos goto out;
657 1.12 christos }
658 1.13 christos attributes++;
659 1.27 christos if (strcasecmp(attrib, "canonical") == 0 ||
660 1.27 christos strcasecmp(attrib, "final") == 0) {
661 1.27 christos /*
662 1.27 christos * If the config requests "Match final" then remember
663 1.27 christos * this so we can perform a second pass later.
664 1.27 christos */
665 1.27 christos if (strcasecmp(attrib, "final") == 0 &&
666 1.27 christos want_final_pass != NULL)
667 1.27 christos *want_final_pass = 1;
668 1.27 christos r = !!final_pass; /* force bitmask member to boolean */
669 1.13 christos if (r == (negate ? 1 : 0))
670 1.13 christos this_result = result = 0;
671 1.13 christos debug3("%.200s line %d: %smatched '%s'",
672 1.13 christos filename, linenum,
673 1.13 christos this_result ? "" : "not ", oattrib);
674 1.13 christos continue;
675 1.13 christos }
676 1.13 christos /* All other criteria require an argument */
677 1.12 christos if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
678 1.12 christos error("Missing Match criteria for %s", attrib);
679 1.12 christos result = -1;
680 1.12 christos goto out;
681 1.12 christos }
682 1.12 christos if (strcasecmp(attrib, "host") == 0) {
683 1.13 christos criteria = xstrdup(host);
684 1.14 christos r = match_hostname(host, arg) == 1;
685 1.13 christos if (r == (negate ? 1 : 0))
686 1.13 christos this_result = result = 0;
687 1.12 christos } else if (strcasecmp(attrib, "originalhost") == 0) {
688 1.13 christos criteria = xstrdup(original_host);
689 1.14 christos r = match_hostname(original_host, arg) == 1;
690 1.13 christos if (r == (negate ? 1 : 0))
691 1.13 christos this_result = result = 0;
692 1.12 christos } else if (strcasecmp(attrib, "user") == 0) {
693 1.13 christos criteria = xstrdup(ruser);
694 1.14 christos r = match_pattern_list(ruser, arg, 0) == 1;
695 1.13 christos if (r == (negate ? 1 : 0))
696 1.13 christos this_result = result = 0;
697 1.12 christos } else if (strcasecmp(attrib, "localuser") == 0) {
698 1.13 christos criteria = xstrdup(pw->pw_name);
699 1.14 christos r = match_pattern_list(pw->pw_name, arg, 0) == 1;
700 1.13 christos if (r == (negate ? 1 : 0))
701 1.13 christos this_result = result = 0;
702 1.12 christos } else if (strcasecmp(attrib, "exec") == 0) {
703 1.32 christos char *conn_hash_hex, *keyalias;
704 1.31 christos
705 1.12 christos if (gethostname(thishost, sizeof(thishost)) == -1)
706 1.12 christos fatal("gethostname: %s", strerror(errno));
707 1.12 christos strlcpy(shorthost, thishost, sizeof(shorthost));
708 1.12 christos shorthost[strcspn(thishost, ".")] = '\0';
709 1.12 christos snprintf(portstr, sizeof(portstr), "%d", port);
710 1.25 christos snprintf(uidstr, sizeof(uidstr), "%llu",
711 1.25 christos (unsigned long long)pw->pw_uid);
712 1.31 christos conn_hash_hex = ssh_connection_hash(thishost, host,
713 1.34 christos portstr, ruser);
714 1.32 christos keyalias = options->host_key_alias ?
715 1.32 christos options->host_key_alias : host;
716 1.12 christos
717 1.12 christos cmd = percent_expand(arg,
718 1.31 christos "C", conn_hash_hex,
719 1.12 christos "L", shorthost,
720 1.12 christos "d", pw->pw_dir,
721 1.12 christos "h", host,
722 1.32 christos "k", keyalias,
723 1.12 christos "l", thishost,
724 1.13 christos "n", original_host,
725 1.12 christos "p", portstr,
726 1.12 christos "r", ruser,
727 1.12 christos "u", pw->pw_name,
728 1.25 christos "i", uidstr,
729 1.12 christos (char *)NULL);
730 1.31 christos free(conn_hash_hex);
731 1.12 christos if (result != 1) {
732 1.12 christos /* skip execution if prior predicate failed */
733 1.13 christos debug3("%.200s line %d: skipped exec "
734 1.13 christos "\"%.100s\"", filename, linenum, cmd);
735 1.13 christos free(cmd);
736 1.13 christos continue;
737 1.13 christos }
738 1.13 christos r = execute_in_shell(cmd);
739 1.13 christos if (r == -1) {
740 1.13 christos fatal("%.200s line %d: match exec "
741 1.13 christos "'%.100s' error", filename,
742 1.13 christos linenum, cmd);
743 1.12 christos }
744 1.13 christos criteria = xstrdup(cmd);
745 1.12 christos free(cmd);
746 1.13 christos /* Force exit status to boolean */
747 1.13 christos r = r == 0;
748 1.13 christos if (r == (negate ? 1 : 0))
749 1.13 christos this_result = result = 0;
750 1.12 christos } else {
751 1.12 christos error("Unsupported Match attribute %s", attrib);
752 1.12 christos result = -1;
753 1.12 christos goto out;
754 1.12 christos }
755 1.13 christos debug3("%.200s line %d: %smatched '%s \"%.100s\"' ",
756 1.13 christos filename, linenum, this_result ? "": "not ",
757 1.13 christos oattrib, criteria);
758 1.13 christos free(criteria);
759 1.12 christos }
760 1.12 christos if (attributes == 0) {
761 1.12 christos error("One or more attributes required for Match");
762 1.12 christos result = -1;
763 1.12 christos goto out;
764 1.12 christos }
765 1.13 christos out:
766 1.13 christos if (result != -1)
767 1.13 christos debug2("match %sfound", result ? "" : "not ");
768 1.12 christos *condition = cp;
769 1.12 christos free(host);
770 1.12 christos return result;
771 1.12 christos }
772 1.12 christos
773 1.25 christos /* Remove environment variable by pattern */
774 1.25 christos static void
775 1.25 christos rm_env(Options *options, const char *arg, const char *filename, int linenum)
776 1.25 christos {
777 1.32 christos int i, j, onum_send_env = options->num_send_env;
778 1.25 christos char *cp;
779 1.25 christos
780 1.25 christos /* Remove an environment variable */
781 1.25 christos for (i = 0; i < options->num_send_env; ) {
782 1.25 christos cp = xstrdup(options->send_env[i]);
783 1.25 christos if (!match_pattern(cp, arg + 1)) {
784 1.25 christos free(cp);
785 1.25 christos i++;
786 1.25 christos continue;
787 1.25 christos }
788 1.25 christos debug3("%s line %d: removing environment %s",
789 1.25 christos filename, linenum, cp);
790 1.25 christos free(cp);
791 1.25 christos free(options->send_env[i]);
792 1.25 christos options->send_env[i] = NULL;
793 1.25 christos for (j = i; j < options->num_send_env - 1; j++) {
794 1.25 christos options->send_env[j] = options->send_env[j + 1];
795 1.25 christos options->send_env[j + 1] = NULL;
796 1.25 christos }
797 1.25 christos options->num_send_env--;
798 1.25 christos /* NB. don't increment i */
799 1.25 christos }
800 1.32 christos if (onum_send_env != options->num_send_env) {
801 1.32 christos options->send_env = xrecallocarray(options->send_env,
802 1.32 christos onum_send_env, options->num_send_env,
803 1.32 christos sizeof(*options->send_env));
804 1.32 christos }
805 1.25 christos }
806 1.25 christos
807 1.1 christos /*
808 1.1 christos * Returns the number of the token pointed to by cp or oBadOption.
809 1.1 christos */
810 1.1 christos static OpCodes
811 1.11 christos parse_token(const char *cp, const char *filename, int linenum,
812 1.11 christos const char *ignored_unknown)
813 1.1 christos {
814 1.11 christos int i;
815 1.1 christos
816 1.1 christos for (i = 0; keywords[i].name; i++)
817 1.11 christos if (strcmp(cp, keywords[i].name) == 0)
818 1.1 christos return keywords[i].opcode;
819 1.14 christos if (ignored_unknown != NULL &&
820 1.14 christos match_pattern_list(cp, ignored_unknown, 1) == 1)
821 1.11 christos return oIgnoredUnknownOption;
822 1.1 christos error("%s: line %d: Bad configuration option: %s",
823 1.1 christos filename, linenum, cp);
824 1.1 christos return oBadOption;
825 1.1 christos }
826 1.1 christos
827 1.12 christos /* Multistate option parsing */
828 1.12 christos struct multistate {
829 1.12 christos const char *key;
830 1.12 christos int value;
831 1.12 christos };
832 1.12 christos static const struct multistate multistate_flag[] = {
833 1.12 christos { "true", 1 },
834 1.12 christos { "false", 0 },
835 1.12 christos { "yes", 1 },
836 1.12 christos { "no", 0 },
837 1.12 christos { NULL, -1 }
838 1.12 christos };
839 1.12 christos static const struct multistate multistate_yesnoask[] = {
840 1.12 christos { "true", 1 },
841 1.12 christos { "false", 0 },
842 1.12 christos { "yes", 1 },
843 1.12 christos { "no", 0 },
844 1.12 christos { "ask", 2 },
845 1.12 christos { NULL, -1 }
846 1.12 christos };
847 1.22 christos static const struct multistate multistate_strict_hostkey[] = {
848 1.22 christos { "true", SSH_STRICT_HOSTKEY_YES },
849 1.22 christos { "false", SSH_STRICT_HOSTKEY_OFF },
850 1.22 christos { "yes", SSH_STRICT_HOSTKEY_YES },
851 1.22 christos { "no", SSH_STRICT_HOSTKEY_OFF },
852 1.22 christos { "ask", SSH_STRICT_HOSTKEY_ASK },
853 1.22 christos { "off", SSH_STRICT_HOSTKEY_OFF },
854 1.22 christos { "accept-new", SSH_STRICT_HOSTKEY_NEW },
855 1.22 christos { NULL, -1 }
856 1.22 christos };
857 1.18 christos static const struct multistate multistate_yesnoaskconfirm[] = {
858 1.18 christos { "true", 1 },
859 1.18 christos { "false", 0 },
860 1.18 christos { "yes", 1 },
861 1.18 christos { "no", 0 },
862 1.18 christos { "ask", 2 },
863 1.18 christos { "confirm", 3 },
864 1.18 christos { NULL, -1 }
865 1.18 christos };
866 1.12 christos static const struct multistate multistate_addressfamily[] = {
867 1.12 christos { "inet", AF_INET },
868 1.12 christos { "inet6", AF_INET6 },
869 1.12 christos { "any", AF_UNSPEC },
870 1.12 christos { NULL, -1 }
871 1.12 christos };
872 1.12 christos static const struct multistate multistate_controlmaster[] = {
873 1.12 christos { "true", SSHCTL_MASTER_YES },
874 1.12 christos { "yes", SSHCTL_MASTER_YES },
875 1.12 christos { "false", SSHCTL_MASTER_NO },
876 1.12 christos { "no", SSHCTL_MASTER_NO },
877 1.12 christos { "auto", SSHCTL_MASTER_AUTO },
878 1.12 christos { "ask", SSHCTL_MASTER_ASK },
879 1.12 christos { "autoask", SSHCTL_MASTER_AUTO_ASK },
880 1.12 christos { NULL, -1 }
881 1.12 christos };
882 1.12 christos static const struct multistate multistate_tunnel[] = {
883 1.12 christos { "ethernet", SSH_TUNMODE_ETHERNET },
884 1.12 christos { "point-to-point", SSH_TUNMODE_POINTOPOINT },
885 1.12 christos { "true", SSH_TUNMODE_DEFAULT },
886 1.12 christos { "yes", SSH_TUNMODE_DEFAULT },
887 1.12 christos { "false", SSH_TUNMODE_NO },
888 1.12 christos { "no", SSH_TUNMODE_NO },
889 1.12 christos { NULL, -1 }
890 1.12 christos };
891 1.12 christos static const struct multistate multistate_requesttty[] = {
892 1.12 christos { "true", REQUEST_TTY_YES },
893 1.12 christos { "yes", REQUEST_TTY_YES },
894 1.12 christos { "false", REQUEST_TTY_NO },
895 1.12 christos { "no", REQUEST_TTY_NO },
896 1.12 christos { "force", REQUEST_TTY_FORCE },
897 1.12 christos { "auto", REQUEST_TTY_AUTO },
898 1.12 christos { NULL, -1 }
899 1.12 christos };
900 1.12 christos static const struct multistate multistate_canonicalizehostname[] = {
901 1.12 christos { "true", SSH_CANONICALISE_YES },
902 1.12 christos { "false", SSH_CANONICALISE_NO },
903 1.12 christos { "yes", SSH_CANONICALISE_YES },
904 1.12 christos { "no", SSH_CANONICALISE_NO },
905 1.12 christos { "always", SSH_CANONICALISE_ALWAYS },
906 1.12 christos { NULL, -1 }
907 1.12 christos };
908 1.29 christos static const struct multistate multistate_compression[] = {
909 1.29 christos #ifdef WITH_ZLIB
910 1.29 christos { "yes", COMP_ZLIB },
911 1.29 christos #endif
912 1.29 christos { "no", COMP_NONE },
913 1.29 christos { NULL, -1 }
914 1.29 christos };
915 1.12 christos
916 1.32 christos static int
917 1.32 christos parse_multistate_value(const char *arg, const char *filename, int linenum,
918 1.32 christos const struct multistate *multistate_ptr)
919 1.32 christos {
920 1.32 christos int i;
921 1.32 christos
922 1.33 christos if (!arg || *arg == '\0') {
923 1.33 christos error("%s line %d: missing argument.", filename, linenum);
924 1.33 christos return -1;
925 1.33 christos }
926 1.32 christos for (i = 0; multistate_ptr[i].key != NULL; i++) {
927 1.32 christos if (strcasecmp(arg, multistate_ptr[i].key) == 0)
928 1.32 christos return multistate_ptr[i].value;
929 1.32 christos }
930 1.32 christos return -1;
931 1.32 christos }
932 1.32 christos
933 1.1 christos /*
934 1.1 christos * Processes a single option line as used in the configuration files. This
935 1.1 christos * only sets those values that have not already been set.
936 1.1 christos */
937 1.1 christos int
938 1.12 christos process_config_line(Options *options, struct passwd *pw, const char *host,
939 1.13 christos const char *original_host, char *line, const char *filename,
940 1.13 christos int linenum, int *activep, int flags)
941 1.1 christos {
942 1.19 christos return process_config_line_depth(options, pw, host, original_host,
943 1.27 christos line, filename, linenum, activep, flags, NULL, 0);
944 1.19 christos }
945 1.19 christos
946 1.19 christos #define WHITESPACE " \t\r\n"
947 1.19 christos static int
948 1.19 christos process_config_line_depth(Options *options, struct passwd *pw, const char *host,
949 1.19 christos const char *original_host, char *line, const char *filename,
950 1.27 christos int linenum, int *activep, int flags, int *want_final_pass, int depth)
951 1.19 christos {
952 1.33 christos char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, *p, ch;
953 1.33 christos char **cpptr, ***cppptr, fwdarg[256];
954 1.33 christos u_int i, *uintptr, uvalue, max_entries = 0;
955 1.19 christos int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0;
956 1.22 christos int remotefwd, dynamicfwd;
957 1.1 christos LogLevel *log_level_ptr;
958 1.22 christos SyslogFacility *log_facility_ptr;
959 1.11 christos long long val64;
960 1.1 christos size_t len;
961 1.12 christos struct Forward fwd;
962 1.12 christos const struct multistate *multistate_ptr;
963 1.12 christos struct allowed_cname *cname;
964 1.19 christos glob_t gl;
965 1.23 christos const char *errstr;
966 1.12 christos
967 1.12 christos if (activep == NULL) { /* We are processing a command line directive */
968 1.12 christos cmdline = 1;
969 1.12 christos activep = &cmdline;
970 1.12 christos }
971 1.1 christos
972 1.21 christos /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
973 1.14 christos if ((len = strlen(line)) == 0)
974 1.14 christos return 0;
975 1.14 christos for (len--; len > 0; len--) {
976 1.21 christos if (strchr(WHITESPACE "\f", line[len]) == NULL)
977 1.1 christos break;
978 1.1 christos line[len] = '\0';
979 1.1 christos }
980 1.1 christos
981 1.1 christos s = line;
982 1.1 christos /* Get the keyword. (Each line is supposed to begin with a keyword). */
983 1.1 christos if ((keyword = strdelim(&s)) == NULL)
984 1.1 christos return 0;
985 1.1 christos /* Ignore leading whitespace. */
986 1.1 christos if (*keyword == '\0')
987 1.1 christos keyword = strdelim(&s);
988 1.1 christos if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
989 1.1 christos return 0;
990 1.11 christos /* Match lowercase keyword */
991 1.12 christos lowercase(keyword);
992 1.1 christos
993 1.11 christos opcode = parse_token(keyword, filename, linenum,
994 1.11 christos options->ignored_unknown);
995 1.1 christos
996 1.1 christos switch (opcode) {
997 1.1 christos case oBadOption:
998 1.1 christos /* don't panic, but count bad options */
999 1.1 christos return -1;
1000 1.22 christos case oIgnore:
1001 1.22 christos return 0;
1002 1.11 christos case oIgnoredUnknownOption:
1003 1.11 christos debug("%s line %d: Ignored unknown option \"%s\"",
1004 1.11 christos filename, linenum, keyword);
1005 1.11 christos return 0;
1006 1.1 christos case oConnectTimeout:
1007 1.1 christos intptr = &options->connection_timeout;
1008 1.1 christos parse_time:
1009 1.1 christos arg = strdelim(&s);
1010 1.33 christos if (!arg || *arg == '\0') {
1011 1.33 christos error("%s line %d: missing time value.",
1012 1.1 christos filename, linenum);
1013 1.33 christos return -1;
1014 1.33 christos }
1015 1.13 christos if (strcmp(arg, "none") == 0)
1016 1.13 christos value = -1;
1017 1.33 christos else if ((value = convtime(arg)) == -1) {
1018 1.33 christos error("%s line %d: invalid time value.",
1019 1.1 christos filename, linenum);
1020 1.33 christos return -1;
1021 1.33 christos }
1022 1.1 christos if (*activep && *intptr == -1)
1023 1.1 christos *intptr = value;
1024 1.1 christos break;
1025 1.1 christos
1026 1.1 christos case oForwardAgent:
1027 1.1 christos intptr = &options->forward_agent;
1028 1.29 christos
1029 1.29 christos arg = strdelim(&s);
1030 1.33 christos if (!arg || *arg == '\0') {
1031 1.33 christos error("%s line %d: missing argument.",
1032 1.29 christos filename, linenum);
1033 1.33 christos return -1;
1034 1.33 christos }
1035 1.29 christos
1036 1.29 christos value = -1;
1037 1.29 christos multistate_ptr = multistate_flag;
1038 1.29 christos for (i = 0; multistate_ptr[i].key != NULL; i++) {
1039 1.29 christos if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1040 1.29 christos value = multistate_ptr[i].value;
1041 1.29 christos break;
1042 1.29 christos }
1043 1.29 christos }
1044 1.29 christos if (value != -1) {
1045 1.29 christos if (*activep && *intptr == -1)
1046 1.29 christos *intptr = value;
1047 1.29 christos break;
1048 1.29 christos }
1049 1.29 christos /* ForwardAgent wasn't 'yes' or 'no', assume a path */
1050 1.29 christos if (*activep && *intptr == -1)
1051 1.29 christos *intptr = 1;
1052 1.29 christos
1053 1.29 christos charptr = &options->forward_agent_sock_path;
1054 1.29 christos goto parse_agent_path;
1055 1.29 christos
1056 1.29 christos case oForwardX11:
1057 1.29 christos intptr = &options->forward_x11;
1058 1.12 christos parse_flag:
1059 1.12 christos multistate_ptr = multistate_flag;
1060 1.12 christos parse_multistate:
1061 1.1 christos arg = strdelim(&s);
1062 1.32 christos if ((value = parse_multistate_value(arg, filename, linenum,
1063 1.34 christos multistate_ptr)) == -1) {
1064 1.33 christos error("%s line %d: unsupported option \"%s\".",
1065 1.12 christos filename, linenum, arg);
1066 1.33 christos return -1;
1067 1.32 christos }
1068 1.1 christos if (*activep && *intptr == -1)
1069 1.1 christos *intptr = value;
1070 1.1 christos break;
1071 1.1 christos
1072 1.1 christos case oForwardX11Trusted:
1073 1.1 christos intptr = &options->forward_x11_trusted;
1074 1.1 christos goto parse_flag;
1075 1.13 christos
1076 1.4 adam case oForwardX11Timeout:
1077 1.4 adam intptr = &options->forward_x11_timeout;
1078 1.4 adam goto parse_time;
1079 1.1 christos
1080 1.1 christos case oGatewayPorts:
1081 1.12 christos intptr = &options->fwd_opts.gateway_ports;
1082 1.1 christos goto parse_flag;
1083 1.1 christos
1084 1.1 christos case oExitOnForwardFailure:
1085 1.1 christos intptr = &options->exit_on_forward_failure;
1086 1.1 christos goto parse_flag;
1087 1.1 christos
1088 1.1 christos case oPasswordAuthentication:
1089 1.1 christos intptr = &options->password_authentication;
1090 1.1 christos goto parse_flag;
1091 1.1 christos
1092 1.1 christos case oKbdInteractiveAuthentication:
1093 1.1 christos intptr = &options->kbd_interactive_authentication;
1094 1.1 christos goto parse_flag;
1095 1.1 christos
1096 1.1 christos case oKbdInteractiveDevices:
1097 1.1 christos charptr = &options->kbd_interactive_devices;
1098 1.1 christos goto parse_string;
1099 1.1 christos
1100 1.1 christos case oPubkeyAuthentication:
1101 1.1 christos intptr = &options->pubkey_authentication;
1102 1.1 christos goto parse_flag;
1103 1.1 christos
1104 1.1 christos case oHostbasedAuthentication:
1105 1.1 christos intptr = &options->hostbased_authentication;
1106 1.1 christos goto parse_flag;
1107 1.1 christos
1108 1.1 christos case oChallengeResponseAuthentication:
1109 1.1 christos intptr = &options->challenge_response_authentication;
1110 1.1 christos goto parse_flag;
1111 1.1 christos
1112 1.2 christos #if defined(KRB4) || defined(KRB5)
1113 1.2 christos case oKerberosAuthentication:
1114 1.2 christos intptr = &options->kerberos_authentication;
1115 1.2 christos goto parse_flag;
1116 1.2 christos #endif
1117 1.2 christos #if defined(AFS) || defined(KRB5)
1118 1.2 christos case oKerberosTgtPassing:
1119 1.2 christos intptr = &options->kerberos_tgt_passing;
1120 1.2 christos goto parse_flag;
1121 1.2 christos #endif
1122 1.2 christos
1123 1.1 christos case oGssAuthentication:
1124 1.1 christos intptr = &options->gss_authentication;
1125 1.1 christos goto parse_flag;
1126 1.1 christos
1127 1.2 christos #ifdef AFS
1128 1.2 christos case oAFSTokenPassing:
1129 1.2 christos intptr = &options->afs_token_passing;
1130 1.2 christos goto parse_flag;
1131 1.2 christos #endif
1132 1.2 christos
1133 1.1 christos case oGssDelegateCreds:
1134 1.1 christos intptr = &options->gss_deleg_creds;
1135 1.1 christos goto parse_flag;
1136 1.1 christos
1137 1.1 christos case oBatchMode:
1138 1.1 christos intptr = &options->batch_mode;
1139 1.1 christos goto parse_flag;
1140 1.1 christos
1141 1.1 christos case oCheckHostIP:
1142 1.1 christos intptr = &options->check_host_ip;
1143 1.1 christos goto parse_flag;
1144 1.1 christos
1145 1.2 christos case oNoneEnabled:
1146 1.2 christos intptr = &options->none_enabled;
1147 1.2 christos goto parse_flag;
1148 1.2 christos
1149 1.2 christos /* we check to see if the command comes from the */
1150 1.2 christos /* command line or not. If it does then enable it */
1151 1.2 christos /* otherwise fail. NONE should never be a default configuration */
1152 1.2 christos case oNoneSwitch:
1153 1.2 christos if(strcmp(filename,"command-line")==0)
1154 1.2 christos {
1155 1.2 christos intptr = &options->none_switch;
1156 1.2 christos goto parse_flag;
1157 1.2 christos } else {
1158 1.2 christos error("NoneSwitch is found in %.200s.\nYou may only use this configuration option from the command line", filename);
1159 1.2 christos error("Continuing...");
1160 1.2 christos debug("NoneSwitch directive found in %.200s.", filename);
1161 1.2 christos return 0;
1162 1.2 christos }
1163 1.2 christos
1164 1.2 christos case oHPNDisabled:
1165 1.2 christos intptr = &options->hpn_disabled;
1166 1.2 christos goto parse_flag;
1167 1.2 christos
1168 1.2 christos case oHPNBufferSize:
1169 1.2 christos intptr = &options->hpn_buffer_size;
1170 1.2 christos goto parse_int;
1171 1.2 christos
1172 1.2 christos case oTcpRcvBufPoll:
1173 1.2 christos intptr = &options->tcp_rcv_buf_poll;
1174 1.2 christos goto parse_flag;
1175 1.2 christos
1176 1.1 christos case oVerifyHostKeyDNS:
1177 1.1 christos intptr = &options->verify_host_key_dns;
1178 1.12 christos multistate_ptr = multistate_yesnoask;
1179 1.12 christos goto parse_multistate;
1180 1.1 christos
1181 1.1 christos case oStrictHostKeyChecking:
1182 1.1 christos intptr = &options->strict_host_key_checking;
1183 1.22 christos multistate_ptr = multistate_strict_hostkey;
1184 1.12 christos goto parse_multistate;
1185 1.1 christos
1186 1.1 christos case oCompression:
1187 1.1 christos intptr = &options->compression;
1188 1.29 christos multistate_ptr = multistate_compression;
1189 1.29 christos goto parse_multistate;
1190 1.1 christos
1191 1.1 christos case oTCPKeepAlive:
1192 1.1 christos intptr = &options->tcp_keep_alive;
1193 1.1 christos goto parse_flag;
1194 1.1 christos
1195 1.1 christos case oNoHostAuthenticationForLocalhost:
1196 1.1 christos intptr = &options->no_host_authentication_for_localhost;
1197 1.1 christos goto parse_flag;
1198 1.1 christos
1199 1.1 christos case oNumberOfPasswordPrompts:
1200 1.1 christos intptr = &options->number_of_password_prompts;
1201 1.1 christos goto parse_int;
1202 1.1 christos
1203 1.1 christos case oRekeyLimit:
1204 1.1 christos arg = strdelim(&s);
1205 1.33 christos if (!arg || *arg == '\0') {
1206 1.33 christos error("%.200s line %d: Missing argument.", filename,
1207 1.11 christos linenum);
1208 1.33 christos return -1;
1209 1.33 christos }
1210 1.11 christos if (strcmp(arg, "default") == 0) {
1211 1.11 christos val64 = 0;
1212 1.11 christos } else {
1213 1.33 christos if (scan_scaled(arg, &val64) == -1) {
1214 1.33 christos error("%.200s line %d: Bad number '%s': %s",
1215 1.11 christos filename, linenum, arg, strerror(errno));
1216 1.33 christos return -1;
1217 1.33 christos }
1218 1.33 christos if (val64 != 0 && val64 < 16) {
1219 1.33 christos error("%.200s line %d: RekeyLimit too small",
1220 1.11 christos filename, linenum);
1221 1.33 christos return -1;
1222 1.33 christos }
1223 1.1 christos }
1224 1.1 christos if (*activep && options->rekey_limit == -1)
1225 1.18 christos options->rekey_limit = val64;
1226 1.11 christos if (s != NULL) { /* optional rekey interval present */
1227 1.11 christos if (strcmp(s, "none") == 0) {
1228 1.11 christos (void)strdelim(&s); /* discard */
1229 1.11 christos break;
1230 1.11 christos }
1231 1.11 christos intptr = &options->rekey_interval;
1232 1.11 christos goto parse_time;
1233 1.11 christos }
1234 1.1 christos break;
1235 1.1 christos
1236 1.1 christos case oIdentityFile:
1237 1.1 christos arg = strdelim(&s);
1238 1.33 christos if (!arg || *arg == '\0') {
1239 1.33 christos error("%.200s line %d: Missing argument.",
1240 1.33 christos filename, linenum);
1241 1.33 christos return -1;
1242 1.33 christos }
1243 1.1 christos if (*activep) {
1244 1.1 christos intptr = &options->num_identity_files;
1245 1.33 christos if (*intptr >= SSH_MAX_IDENTITY_FILES) {
1246 1.33 christos error("%.200s line %d: Too many identity files "
1247 1.33 christos "specified (max %d).", filename, linenum,
1248 1.33 christos SSH_MAX_IDENTITY_FILES);
1249 1.33 christos return -1;
1250 1.33 christos }
1251 1.13 christos add_identity_file(options, NULL,
1252 1.13 christos arg, flags & SSHCONF_USERCONF);
1253 1.1 christos }
1254 1.1 christos break;
1255 1.1 christos
1256 1.18 christos case oCertificateFile:
1257 1.18 christos arg = strdelim(&s);
1258 1.33 christos if (!arg || *arg == '\0') {
1259 1.33 christos error("%.200s line %d: Missing argument.",
1260 1.18 christos filename, linenum);
1261 1.33 christos return -1;
1262 1.33 christos }
1263 1.18 christos if (*activep) {
1264 1.18 christos intptr = &options->num_certificate_files;
1265 1.18 christos if (*intptr >= SSH_MAX_CERTIFICATE_FILES) {
1266 1.33 christos error("%.200s line %d: Too many certificate "
1267 1.18 christos "files specified (max %d).",
1268 1.18 christos filename, linenum,
1269 1.18 christos SSH_MAX_CERTIFICATE_FILES);
1270 1.33 christos return -1;
1271 1.18 christos }
1272 1.18 christos add_certificate_file(options, arg,
1273 1.18 christos flags & SSHCONF_USERCONF);
1274 1.18 christos }
1275 1.18 christos break;
1276 1.18 christos
1277 1.1 christos case oXAuthLocation:
1278 1.1 christos charptr=&options->xauth_location;
1279 1.1 christos goto parse_string;
1280 1.1 christos
1281 1.1 christos case oUser:
1282 1.1 christos charptr = &options->user;
1283 1.1 christos parse_string:
1284 1.1 christos arg = strdelim(&s);
1285 1.33 christos if (!arg || *arg == '\0') {
1286 1.33 christos error("%.200s line %d: Missing argument.",
1287 1.6 christos filename, linenum);
1288 1.33 christos return -1;
1289 1.33 christos }
1290 1.1 christos if (*activep && *charptr == NULL)
1291 1.1 christos *charptr = xstrdup(arg);
1292 1.1 christos break;
1293 1.1 christos
1294 1.1 christos case oGlobalKnownHostsFile:
1295 1.6 christos cpptr = (char **)&options->system_hostfiles;
1296 1.6 christos uintptr = &options->num_system_hostfiles;
1297 1.6 christos max_entries = SSH_MAX_HOSTS_FILES;
1298 1.6 christos parse_char_array:
1299 1.6 christos if (*activep && *uintptr == 0) {
1300 1.6 christos while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1301 1.33 christos if ((*uintptr) >= max_entries) {
1302 1.33 christos error("%s line %d: too many known "
1303 1.33 christos "hosts files.", filename, linenum);
1304 1.33 christos return -1;
1305 1.33 christos }
1306 1.6 christos cpptr[(*uintptr)++] = xstrdup(arg);
1307 1.6 christos }
1308 1.6 christos }
1309 1.6 christos return 0;
1310 1.1 christos
1311 1.1 christos case oUserKnownHostsFile:
1312 1.6 christos cpptr = (char **)&options->user_hostfiles;
1313 1.6 christos uintptr = &options->num_user_hostfiles;
1314 1.6 christos max_entries = SSH_MAX_HOSTS_FILES;
1315 1.6 christos goto parse_char_array;
1316 1.1 christos
1317 1.28 christos case oHostname:
1318 1.1 christos charptr = &options->hostname;
1319 1.1 christos goto parse_string;
1320 1.1 christos
1321 1.1 christos case oHostKeyAlias:
1322 1.1 christos charptr = &options->host_key_alias;
1323 1.1 christos goto parse_string;
1324 1.1 christos
1325 1.1 christos case oPreferredAuthentications:
1326 1.1 christos charptr = &options->preferred_authentications;
1327 1.1 christos goto parse_string;
1328 1.1 christos
1329 1.1 christos case oBindAddress:
1330 1.1 christos charptr = &options->bind_address;
1331 1.1 christos goto parse_string;
1332 1.1 christos
1333 1.23 christos case oBindInterface:
1334 1.23 christos charptr = &options->bind_interface;
1335 1.23 christos goto parse_string;
1336 1.23 christos
1337 1.4 adam case oPKCS11Provider:
1338 1.4 adam charptr = &options->pkcs11_provider;
1339 1.1 christos goto parse_string;
1340 1.1 christos
1341 1.29 christos case oSecurityKeyProvider:
1342 1.29 christos charptr = &options->sk_provider;
1343 1.29 christos goto parse_string;
1344 1.29 christos
1345 1.33 christos case oKnownHostsCommand:
1346 1.33 christos charptr = &options->known_hosts_command;
1347 1.33 christos goto parse_command;
1348 1.33 christos
1349 1.1 christos case oProxyCommand:
1350 1.1 christos charptr = &options->proxy_command;
1351 1.19 christos /* Ignore ProxyCommand if ProxyJump already specified */
1352 1.19 christos if (options->jump_host != NULL)
1353 1.19 christos charptr = &options->jump_host; /* Skip below */
1354 1.1 christos parse_command:
1355 1.33 christos if (s == NULL) {
1356 1.33 christos error("%.200s line %d: Missing argument.",
1357 1.33 christos filename, linenum);
1358 1.33 christos return -1;
1359 1.33 christos }
1360 1.1 christos len = strspn(s, WHITESPACE "=");
1361 1.1 christos if (*activep && *charptr == NULL)
1362 1.1 christos *charptr = xstrdup(s + len);
1363 1.1 christos return 0;
1364 1.1 christos
1365 1.19 christos case oProxyJump:
1366 1.19 christos if (s == NULL) {
1367 1.33 christos error("%.200s line %d: Missing argument.",
1368 1.19 christos filename, linenum);
1369 1.33 christos return -1;
1370 1.19 christos }
1371 1.19 christos len = strspn(s, WHITESPACE "=");
1372 1.19 christos if (parse_jump(s + len, options, *activep) == -1) {
1373 1.33 christos error("%.200s line %d: Invalid ProxyJump \"%s\"",
1374 1.19 christos filename, linenum, s + len);
1375 1.33 christos return -1;
1376 1.19 christos }
1377 1.19 christos return 0;
1378 1.19 christos
1379 1.1 christos case oPort:
1380 1.27 christos arg = strdelim(&s);
1381 1.33 christos if (!arg || *arg == '\0') {
1382 1.33 christos error("%.200s line %d: Missing argument.",
1383 1.27 christos filename, linenum);
1384 1.33 christos return -1;
1385 1.33 christos }
1386 1.27 christos value = a2port(arg);
1387 1.33 christos if (value <= 0) {
1388 1.33 christos error("%.200s line %d: Bad port '%s'.",
1389 1.27 christos filename, linenum, arg);
1390 1.33 christos return -1;
1391 1.33 christos }
1392 1.27 christos if (*activep && options->port == -1)
1393 1.27 christos options->port = value;
1394 1.27 christos break;
1395 1.27 christos
1396 1.27 christos case oConnectionAttempts:
1397 1.27 christos intptr = &options->connection_attempts;
1398 1.1 christos parse_int:
1399 1.1 christos arg = strdelim(&s);
1400 1.33 christos if ((errstr = atoi_err(arg, &value)) != NULL) {
1401 1.33 christos error("%s line %d: integer value %s.",
1402 1.23 christos filename, linenum, errstr);
1403 1.33 christos return -1;
1404 1.33 christos }
1405 1.1 christos if (*activep && *intptr == -1)
1406 1.1 christos *intptr = value;
1407 1.1 christos break;
1408 1.1 christos
1409 1.2 christos case oTcpRcvBuf:
1410 1.2 christos intptr = &options->tcp_rcv_buf;
1411 1.2 christos goto parse_int;
1412 1.2 christos
1413 1.1 christos case oCiphers:
1414 1.1 christos arg = strdelim(&s);
1415 1.33 christos if (!arg || *arg == '\0') {
1416 1.33 christos error("%.200s line %d: Missing argument.",
1417 1.33 christos filename, linenum);
1418 1.33 christos return -1;
1419 1.33 christos }
1420 1.28 christos if (*arg != '-' &&
1421 1.33 christos !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg)){
1422 1.33 christos error("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1423 1.1 christos filename, linenum, arg ? arg : "<NONE>");
1424 1.33 christos return -1;
1425 1.33 christos }
1426 1.1 christos if (*activep && options->ciphers == NULL)
1427 1.1 christos options->ciphers = xstrdup(arg);
1428 1.1 christos break;
1429 1.1 christos
1430 1.1 christos case oMacs:
1431 1.1 christos arg = strdelim(&s);
1432 1.33 christos if (!arg || *arg == '\0') {
1433 1.33 christos error("%.200s line %d: Missing argument.",
1434 1.33 christos filename, linenum);
1435 1.33 christos return -1;
1436 1.33 christos }
1437 1.28 christos if (*arg != '-' &&
1438 1.33 christos !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg)) {
1439 1.33 christos error("%.200s line %d: Bad SSH2 MAC spec '%s'.",
1440 1.1 christos filename, linenum, arg ? arg : "<NONE>");
1441 1.33 christos return -1;
1442 1.33 christos }
1443 1.1 christos if (*activep && options->macs == NULL)
1444 1.1 christos options->macs = xstrdup(arg);
1445 1.1 christos break;
1446 1.1 christos
1447 1.5 christos case oKexAlgorithms:
1448 1.5 christos arg = strdelim(&s);
1449 1.33 christos if (!arg || *arg == '\0') {
1450 1.33 christos error("%.200s line %d: Missing argument.",
1451 1.5 christos filename, linenum);
1452 1.33 christos return -1;
1453 1.33 christos }
1454 1.21 christos if (*arg != '-' &&
1455 1.28 christos !kex_names_valid(*arg == '+' || *arg == '^' ?
1456 1.33 christos arg + 1 : arg)) {
1457 1.33 christos error("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
1458 1.5 christos filename, linenum, arg ? arg : "<NONE>");
1459 1.33 christos return -1;
1460 1.33 christos }
1461 1.5 christos if (*activep && options->kex_algorithms == NULL)
1462 1.5 christos options->kex_algorithms = xstrdup(arg);
1463 1.5 christos break;
1464 1.5 christos
1465 1.1 christos case oHostKeyAlgorithms:
1466 1.16 christos charptr = &options->hostkeyalgorithms;
1467 1.33 christos parse_pubkey_algos:
1468 1.1 christos arg = strdelim(&s);
1469 1.33 christos if (!arg || *arg == '\0') {
1470 1.33 christos error("%.200s line %d: Missing argument.",
1471 1.16 christos filename, linenum);
1472 1.33 christos return -1;
1473 1.33 christos }
1474 1.21 christos if (*arg != '-' &&
1475 1.28 christos !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1476 1.33 christos arg + 1 : arg, 1)) {
1477 1.33 christos error("%s line %d: Bad key types '%s'.",
1478 1.33 christos filename, linenum, arg ? arg : "<NONE>");
1479 1.33 christos return -1;
1480 1.33 christos }
1481 1.16 christos if (*activep && *charptr == NULL)
1482 1.16 christos *charptr = xstrdup(arg);
1483 1.1 christos break;
1484 1.1 christos
1485 1.27 christos case oCASignatureAlgorithms:
1486 1.27 christos charptr = &options->ca_sign_algorithms;
1487 1.33 christos goto parse_pubkey_algos;
1488 1.27 christos
1489 1.1 christos case oLogLevel:
1490 1.1 christos log_level_ptr = &options->log_level;
1491 1.1 christos arg = strdelim(&s);
1492 1.1 christos value = log_level_number(arg);
1493 1.33 christos if (value == SYSLOG_LEVEL_NOT_SET) {
1494 1.33 christos error("%.200s line %d: unsupported log level '%s'",
1495 1.1 christos filename, linenum, arg ? arg : "<NONE>");
1496 1.33 christos return -1;
1497 1.33 christos }
1498 1.1 christos if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
1499 1.1 christos *log_level_ptr = (LogLevel) value;
1500 1.1 christos break;
1501 1.1 christos
1502 1.22 christos case oLogFacility:
1503 1.22 christos log_facility_ptr = &options->log_facility;
1504 1.22 christos arg = strdelim(&s);
1505 1.22 christos value = log_facility_number(arg);
1506 1.33 christos if (value == SYSLOG_FACILITY_NOT_SET) {
1507 1.33 christos error("%.200s line %d: unsupported log facility '%s'",
1508 1.22 christos filename, linenum, arg ? arg : "<NONE>");
1509 1.33 christos return -1;
1510 1.33 christos }
1511 1.22 christos if (*log_facility_ptr == -1)
1512 1.22 christos *log_facility_ptr = (SyslogFacility) value;
1513 1.22 christos break;
1514 1.22 christos
1515 1.33 christos case oLogVerbose:
1516 1.33 christos cppptr = &options->log_verbose;
1517 1.33 christos uintptr = &options->num_log_verbose;
1518 1.33 christos if (*activep && *uintptr == 0) {
1519 1.33 christos while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1520 1.33 christos *cppptr = xrecallocarray(*cppptr, *uintptr,
1521 1.33 christos *uintptr + 1, sizeof(**cppptr));
1522 1.33 christos (*cppptr)[(*uintptr)++] = xstrdup(arg);
1523 1.33 christos }
1524 1.33 christos }
1525 1.33 christos return 0;
1526 1.33 christos
1527 1.1 christos case oLocalForward:
1528 1.1 christos case oRemoteForward:
1529 1.1 christos case oDynamicForward:
1530 1.1 christos arg = strdelim(&s);
1531 1.33 christos if (!arg || *arg == '\0') {
1532 1.33 christos error("%.200s line %d: Missing argument.",
1533 1.1 christos filename, linenum);
1534 1.33 christos return -1;
1535 1.33 christos }
1536 1.1 christos
1537 1.22 christos remotefwd = (opcode == oRemoteForward);
1538 1.22 christos dynamicfwd = (opcode == oDynamicForward);
1539 1.22 christos
1540 1.22 christos if (!dynamicfwd) {
1541 1.1 christos arg2 = strdelim(&s);
1542 1.22 christos if (arg2 == NULL || *arg2 == '\0') {
1543 1.22 christos if (remotefwd)
1544 1.22 christos dynamicfwd = 1;
1545 1.33 christos else {
1546 1.33 christos error("%.200s line %d: Missing target "
1547 1.22 christos "argument.", filename, linenum);
1548 1.33 christos return -1;
1549 1.33 christos }
1550 1.22 christos } else {
1551 1.22 christos /* construct a string for parse_forward */
1552 1.22 christos snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg,
1553 1.22 christos arg2);
1554 1.22 christos }
1555 1.22 christos }
1556 1.22 christos if (dynamicfwd)
1557 1.1 christos strlcpy(fwdarg, arg, sizeof(fwdarg));
1558 1.1 christos
1559 1.33 christos if (parse_forward(&fwd, fwdarg, dynamicfwd, remotefwd) == 0) {
1560 1.33 christos error("%.200s line %d: Bad forwarding specification.",
1561 1.1 christos filename, linenum);
1562 1.33 christos return -1;
1563 1.33 christos }
1564 1.1 christos
1565 1.1 christos if (*activep) {
1566 1.22 christos if (remotefwd) {
1567 1.22 christos add_remote_forward(options, &fwd);
1568 1.22 christos } else {
1569 1.1 christos add_local_forward(options, &fwd);
1570 1.22 christos }
1571 1.1 christos }
1572 1.1 christos break;
1573 1.1 christos
1574 1.33 christos case oPermitRemoteOpen:
1575 1.33 christos uintptr = &options->num_permitted_remote_opens;
1576 1.33 christos cppptr = &options->permitted_remote_opens;
1577 1.33 christos arg = strdelim(&s);
1578 1.33 christos if (!arg || *arg == '\0')
1579 1.33 christos fatal("%s line %d: missing %s specification",
1580 1.33 christos filename, linenum, lookup_opcode_name(opcode));
1581 1.33 christos uvalue = *uintptr; /* modified later */
1582 1.33 christos if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
1583 1.33 christos if (*activep && uvalue == 0) {
1584 1.33 christos *uintptr = 1;
1585 1.33 christos *cppptr = xcalloc(1, sizeof(**cppptr));
1586 1.33 christos (*cppptr)[0] = xstrdup(arg);
1587 1.33 christos }
1588 1.33 christos break;
1589 1.33 christos }
1590 1.33 christos for (; arg != NULL && *arg != '\0'; arg = strdelim(&s)) {
1591 1.33 christos arg2 = xstrdup(arg);
1592 1.33 christos ch = '\0';
1593 1.33 christos p = hpdelim2(&arg, &ch);
1594 1.33 christos if (p == NULL || ch == '/') {
1595 1.33 christos fatal("%s line %d: missing host in %s",
1596 1.33 christos filename, linenum,
1597 1.33 christos lookup_opcode_name(opcode));
1598 1.33 christos }
1599 1.33 christos p = cleanhostname(p);
1600 1.33 christos /*
1601 1.33 christos * don't want to use permitopen_port to avoid
1602 1.33 christos * dependency on channels.[ch] here.
1603 1.33 christos */
1604 1.33 christos if (arg == NULL ||
1605 1.33 christos (strcmp(arg, "*") != 0 && a2port(arg) <= 0)) {
1606 1.33 christos fatal("%s line %d: bad port number in %s",
1607 1.33 christos filename, linenum,
1608 1.33 christos lookup_opcode_name(opcode));
1609 1.33 christos }
1610 1.33 christos if (*activep && uvalue == 0) {
1611 1.33 christos opt_array_append(filename, linenum,
1612 1.33 christos lookup_opcode_name(opcode),
1613 1.33 christos cppptr, uintptr, arg2);
1614 1.33 christos }
1615 1.33 christos free(arg2);
1616 1.33 christos }
1617 1.33 christos break;
1618 1.33 christos
1619 1.1 christos case oClearAllForwardings:
1620 1.1 christos intptr = &options->clear_forwardings;
1621 1.1 christos goto parse_flag;
1622 1.1 christos
1623 1.1 christos case oHost:
1624 1.33 christos if (cmdline) {
1625 1.33 christos error("Host directive not supported as a command-line "
1626 1.12 christos "option");
1627 1.33 christos return -1;
1628 1.33 christos }
1629 1.1 christos *activep = 0;
1630 1.6 christos arg2 = NULL;
1631 1.6 christos while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1632 1.19 christos if ((flags & SSHCONF_NEVERMATCH) != 0)
1633 1.19 christos break;
1634 1.6 christos negated = *arg == '!';
1635 1.6 christos if (negated)
1636 1.6 christos arg++;
1637 1.1 christos if (match_pattern(host, arg)) {
1638 1.6 christos if (negated) {
1639 1.6 christos debug("%.200s line %d: Skipping Host "
1640 1.6 christos "block because of negated match "
1641 1.6 christos "for %.100s", filename, linenum,
1642 1.6 christos arg);
1643 1.6 christos *activep = 0;
1644 1.6 christos break;
1645 1.6 christos }
1646 1.6 christos if (!*activep)
1647 1.6 christos arg2 = arg; /* logged below */
1648 1.1 christos *activep = 1;
1649 1.1 christos }
1650 1.6 christos }
1651 1.6 christos if (*activep)
1652 1.6 christos debug("%.200s line %d: Applying options for %.100s",
1653 1.6 christos filename, linenum, arg2);
1654 1.1 christos /* Avoid garbage check below, as strdelim is done. */
1655 1.1 christos return 0;
1656 1.1 christos
1657 1.12 christos case oMatch:
1658 1.33 christos if (cmdline) {
1659 1.33 christos error("Host directive not supported as a command-line "
1660 1.12 christos "option");
1661 1.33 christos return -1;
1662 1.33 christos }
1663 1.13 christos value = match_cfg_line(options, &s, pw, host, original_host,
1664 1.27 christos flags & SSHCONF_FINAL, want_final_pass,
1665 1.27 christos filename, linenum);
1666 1.33 christos if (value < 0) {
1667 1.33 christos error("%.200s line %d: Bad Match condition", filename,
1668 1.12 christos linenum);
1669 1.33 christos return -1;
1670 1.33 christos }
1671 1.19 christos *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value;
1672 1.12 christos break;
1673 1.12 christos
1674 1.1 christos case oEscapeChar:
1675 1.1 christos intptr = &options->escape_char;
1676 1.1 christos arg = strdelim(&s);
1677 1.33 christos if (!arg || *arg == '\0') {
1678 1.33 christos error("%.200s line %d: Missing argument.",
1679 1.33 christos filename, linenum);
1680 1.33 christos return -1;
1681 1.33 christos }
1682 1.14 christos if (strcmp(arg, "none") == 0)
1683 1.14 christos value = SSH_ESCAPECHAR_NONE;
1684 1.14 christos else if (arg[1] == '\0')
1685 1.14 christos value = (u_char) arg[0];
1686 1.14 christos else if (arg[0] == '^' && arg[2] == 0 &&
1687 1.1 christos (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
1688 1.1 christos value = (u_char) arg[1] & 31;
1689 1.1 christos else {
1690 1.33 christos error("%.200s line %d: Bad escape character.",
1691 1.1 christos filename, linenum);
1692 1.33 christos return -1;
1693 1.1 christos }
1694 1.1 christos if (*activep && *intptr == -1)
1695 1.1 christos *intptr = value;
1696 1.1 christos break;
1697 1.1 christos
1698 1.1 christos case oAddressFamily:
1699 1.1 christos intptr = &options->address_family;
1700 1.12 christos multistate_ptr = multistate_addressfamily;
1701 1.12 christos goto parse_multistate;
1702 1.1 christos
1703 1.1 christos case oEnableSSHKeysign:
1704 1.1 christos intptr = &options->enable_ssh_keysign;
1705 1.1 christos goto parse_flag;
1706 1.1 christos
1707 1.1 christos case oIdentitiesOnly:
1708 1.1 christos intptr = &options->identities_only;
1709 1.1 christos goto parse_flag;
1710 1.1 christos
1711 1.1 christos case oServerAliveInterval:
1712 1.1 christos intptr = &options->server_alive_interval;
1713 1.1 christos goto parse_time;
1714 1.1 christos
1715 1.1 christos case oServerAliveCountMax:
1716 1.1 christos intptr = &options->server_alive_count_max;
1717 1.1 christos goto parse_int;
1718 1.1 christos
1719 1.1 christos case oSendEnv:
1720 1.1 christos while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1721 1.33 christos if (strchr(arg, '=') != NULL) {
1722 1.33 christos error("%s line %d: Invalid environment name.",
1723 1.1 christos filename, linenum);
1724 1.33 christos return -1;
1725 1.33 christos }
1726 1.1 christos if (!*activep)
1727 1.1 christos continue;
1728 1.25 christos if (*arg == '-') {
1729 1.25 christos /* Removing an env var */
1730 1.25 christos rm_env(options, arg, filename, linenum);
1731 1.25 christos continue;
1732 1.25 christos } else {
1733 1.25 christos /* Adding an env var */
1734 1.33 christos if (options->num_send_env >= INT_MAX) {
1735 1.33 christos error("%s line %d: too many send env.",
1736 1.25 christos filename, linenum);
1737 1.33 christos return -1;
1738 1.33 christos }
1739 1.25 christos options->send_env = xrecallocarray(
1740 1.25 christos options->send_env, options->num_send_env,
1741 1.25 christos options->num_send_env + 1,
1742 1.25 christos sizeof(*options->send_env));
1743 1.25 christos options->send_env[options->num_send_env++] =
1744 1.25 christos xstrdup(arg);
1745 1.25 christos }
1746 1.25 christos }
1747 1.25 christos break;
1748 1.25 christos
1749 1.25 christos case oSetEnv:
1750 1.25 christos value = options->num_setenv;
1751 1.25 christos while ((arg = strdelimw(&s)) != NULL && *arg != '\0') {
1752 1.33 christos if (strchr(arg, '=') == NULL) {
1753 1.33 christos error("%s line %d: Invalid SetEnv.",
1754 1.25 christos filename, linenum);
1755 1.33 christos return -1;
1756 1.33 christos }
1757 1.25 christos if (!*activep || value != 0)
1758 1.25 christos continue;
1759 1.25 christos /* Adding a setenv var */
1760 1.33 christos if (options->num_setenv >= INT_MAX) {
1761 1.33 christos error("%s line %d: too many SetEnv.",
1762 1.1 christos filename, linenum);
1763 1.33 christos return -1;
1764 1.33 christos }
1765 1.25 christos options->setenv = xrecallocarray(
1766 1.25 christos options->setenv, options->num_setenv,
1767 1.25 christos options->num_setenv + 1, sizeof(*options->setenv));
1768 1.25 christos options->setenv[options->num_setenv++] = xstrdup(arg);
1769 1.1 christos }
1770 1.1 christos break;
1771 1.1 christos
1772 1.1 christos case oControlPath:
1773 1.1 christos charptr = &options->control_path;
1774 1.1 christos goto parse_string;
1775 1.1 christos
1776 1.1 christos case oControlMaster:
1777 1.1 christos intptr = &options->control_master;
1778 1.12 christos multistate_ptr = multistate_controlmaster;
1779 1.12 christos goto parse_multistate;
1780 1.1 christos
1781 1.4 adam case oControlPersist:
1782 1.4 adam /* no/false/yes/true, or a time spec */
1783 1.4 adam intptr = &options->control_persist;
1784 1.4 adam arg = strdelim(&s);
1785 1.33 christos if (!arg || *arg == '\0') {
1786 1.33 christos error("%.200s line %d: Missing ControlPersist"
1787 1.4 adam " argument.", filename, linenum);
1788 1.33 christos return -1;
1789 1.33 christos }
1790 1.4 adam value = 0;
1791 1.4 adam value2 = 0; /* timeout */
1792 1.4 adam if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1793 1.4 adam value = 0;
1794 1.4 adam else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1795 1.4 adam value = 1;
1796 1.4 adam else if ((value2 = convtime(arg)) >= 0)
1797 1.4 adam value = 1;
1798 1.33 christos else {
1799 1.33 christos error("%.200s line %d: Bad ControlPersist argument.",
1800 1.4 adam filename, linenum);
1801 1.33 christos return -1;
1802 1.33 christos }
1803 1.4 adam if (*activep && *intptr == -1) {
1804 1.4 adam *intptr = value;
1805 1.4 adam options->control_persist_timeout = value2;
1806 1.4 adam }
1807 1.4 adam break;
1808 1.4 adam
1809 1.1 christos case oHashKnownHosts:
1810 1.1 christos intptr = &options->hash_known_hosts;
1811 1.1 christos goto parse_flag;
1812 1.1 christos
1813 1.1 christos case oTunnel:
1814 1.1 christos intptr = &options->tun_open;
1815 1.12 christos multistate_ptr = multistate_tunnel;
1816 1.12 christos goto parse_multistate;
1817 1.1 christos
1818 1.1 christos case oTunnelDevice:
1819 1.1 christos arg = strdelim(&s);
1820 1.33 christos if (!arg || *arg == '\0') {
1821 1.33 christos error("%.200s line %d: Missing argument.",
1822 1.33 christos filename, linenum);
1823 1.33 christos return -1;
1824 1.33 christos }
1825 1.1 christos value = a2tun(arg, &value2);
1826 1.33 christos if (value == SSH_TUNID_ERR) {
1827 1.33 christos error("%.200s line %d: Bad tun device.",
1828 1.33 christos filename, linenum);
1829 1.33 christos return -1;
1830 1.33 christos }
1831 1.1 christos if (*activep) {
1832 1.1 christos options->tun_local = value;
1833 1.1 christos options->tun_remote = value2;
1834 1.1 christos }
1835 1.1 christos break;
1836 1.1 christos
1837 1.1 christos case oLocalCommand:
1838 1.1 christos charptr = &options->local_command;
1839 1.1 christos goto parse_command;
1840 1.1 christos
1841 1.1 christos case oPermitLocalCommand:
1842 1.1 christos intptr = &options->permit_local_command;
1843 1.1 christos goto parse_flag;
1844 1.1 christos
1845 1.22 christos case oRemoteCommand:
1846 1.22 christos charptr = &options->remote_command;
1847 1.22 christos goto parse_command;
1848 1.22 christos
1849 1.1 christos case oVisualHostKey:
1850 1.1 christos intptr = &options->visual_host_key;
1851 1.1 christos goto parse_flag;
1852 1.1 christos
1853 1.19 christos case oInclude:
1854 1.33 christos if (cmdline) {
1855 1.33 christos error("Include directive not supported as a "
1856 1.19 christos "command-line option");
1857 1.33 christos return -1;
1858 1.33 christos }
1859 1.19 christos value = 0;
1860 1.19 christos while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1861 1.19 christos /*
1862 1.19 christos * Ensure all paths are anchored. User configuration
1863 1.19 christos * files may begin with '~/' but system configurations
1864 1.19 christos * must not. If the path is relative, then treat it
1865 1.19 christos * as living in ~/.ssh for user configurations or
1866 1.19 christos * /etc/ssh for system ones.
1867 1.19 christos */
1868 1.33 christos if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0) {
1869 1.33 christos error("%.200s line %d: bad include path %s.",
1870 1.19 christos filename, linenum, arg);
1871 1.33 christos return -1;
1872 1.33 christos }
1873 1.27 christos if (!path_absolute(arg) && *arg != '~') {
1874 1.19 christos xasprintf(&arg2, "%s/%s",
1875 1.19 christos (flags & SSHCONF_USERCONF) ?
1876 1.19 christos "~/" _PATH_SSH_USER_DIR : SSHDIR, arg);
1877 1.19 christos } else
1878 1.19 christos arg2 = xstrdup(arg);
1879 1.19 christos memset(&gl, 0, sizeof(gl));
1880 1.19 christos r = glob(arg2, GLOB_TILDE | GLOB_LIMIT, NULL, &gl);
1881 1.19 christos if (r == GLOB_NOMATCH) {
1882 1.19 christos debug("%.200s line %d: include %s matched no "
1883 1.19 christos "files",filename, linenum, arg2);
1884 1.21 christos free(arg2);
1885 1.19 christos continue;
1886 1.33 christos } else if (r != 0) {
1887 1.33 christos error("%.200s line %d: glob failed for %s.",
1888 1.19 christos filename, linenum, arg2);
1889 1.33 christos return -1;
1890 1.33 christos }
1891 1.19 christos free(arg2);
1892 1.19 christos oactive = *activep;
1893 1.29 christos for (i = 0; i < gl.gl_pathc; i++) {
1894 1.19 christos debug3("%.200s line %d: Including file %s "
1895 1.19 christos "depth %d%s", filename, linenum,
1896 1.19 christos gl.gl_pathv[i], depth,
1897 1.19 christos oactive ? "" : " (parse only)");
1898 1.19 christos r = read_config_file_depth(gl.gl_pathv[i],
1899 1.19 christos pw, host, original_host, options,
1900 1.19 christos flags | SSHCONF_CHECKPERM |
1901 1.19 christos (oactive ? 0 : SSHCONF_NEVERMATCH),
1902 1.27 christos activep, want_final_pass, depth + 1);
1903 1.21 christos if (r != 1 && errno != ENOENT) {
1904 1.33 christos error("Can't open user config file "
1905 1.21 christos "%.100s: %.100s", gl.gl_pathv[i],
1906 1.21 christos strerror(errno));
1907 1.33 christos globfree(&gl);
1908 1.33 christos return -1;
1909 1.21 christos }
1910 1.19 christos /*
1911 1.19 christos * don't let Match in includes clobber the
1912 1.19 christos * containing file's Match state.
1913 1.19 christos */
1914 1.19 christos *activep = oactive;
1915 1.19 christos if (r != 1)
1916 1.19 christos value = -1;
1917 1.19 christos }
1918 1.19 christos globfree(&gl);
1919 1.19 christos }
1920 1.19 christos if (value != 0)
1921 1.19 christos return value;
1922 1.19 christos break;
1923 1.19 christos
1924 1.5 christos case oIPQoS:
1925 1.5 christos arg = strdelim(&s);
1926 1.33 christos if ((value = parse_ipqos(arg)) == -1) {
1927 1.33 christos error("%s line %d: Bad IPQoS value: %s",
1928 1.5 christos filename, linenum, arg);
1929 1.33 christos return -1;
1930 1.33 christos }
1931 1.5 christos arg = strdelim(&s);
1932 1.5 christos if (arg == NULL)
1933 1.5 christos value2 = value;
1934 1.33 christos else if ((value2 = parse_ipqos(arg)) == -1) {
1935 1.33 christos error("%s line %d: Bad IPQoS value: %s",
1936 1.5 christos filename, linenum, arg);
1937 1.33 christos return -1;
1938 1.33 christos }
1939 1.5 christos if (*activep) {
1940 1.5 christos options->ip_qos_interactive = value;
1941 1.5 christos options->ip_qos_bulk = value2;
1942 1.5 christos }
1943 1.5 christos break;
1944 1.5 christos
1945 1.6 christos case oRequestTTY:
1946 1.6 christos intptr = &options->request_tty;
1947 1.12 christos multistate_ptr = multistate_requesttty;
1948 1.12 christos goto parse_multistate;
1949 1.6 christos
1950 1.7 tls case oSendVersionFirst:
1951 1.7 tls intptr = &options->send_version_first;
1952 1.7 tls goto parse_flag;
1953 1.7 tls
1954 1.11 christos case oIgnoreUnknown:
1955 1.11 christos charptr = &options->ignored_unknown;
1956 1.11 christos goto parse_string;
1957 1.11 christos
1958 1.12 christos case oProxyUseFdpass:
1959 1.12 christos intptr = &options->proxy_use_fdpass;
1960 1.12 christos goto parse_flag;
1961 1.12 christos
1962 1.12 christos case oCanonicalDomains:
1963 1.12 christos value = options->num_canonical_domains != 0;
1964 1.12 christos while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1965 1.23 christos if (!valid_domain(arg, 1, &errstr)) {
1966 1.33 christos error("%s line %d: %s", filename, linenum,
1967 1.23 christos errstr);
1968 1.33 christos return -1;
1969 1.23 christos }
1970 1.12 christos if (!*activep || value)
1971 1.12 christos continue;
1972 1.33 christos if (options->num_canonical_domains >=
1973 1.33 christos MAX_CANON_DOMAINS) {
1974 1.33 christos error("%s line %d: too many hostname suffixes.",
1975 1.12 christos filename, linenum);
1976 1.33 christos return -1;
1977 1.33 christos }
1978 1.12 christos options->canonical_domains[
1979 1.12 christos options->num_canonical_domains++] = xstrdup(arg);
1980 1.12 christos }
1981 1.12 christos break;
1982 1.12 christos
1983 1.12 christos case oCanonicalizePermittedCNAMEs:
1984 1.12 christos value = options->num_permitted_cnames != 0;
1985 1.12 christos while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1986 1.12 christos /* Either '*' for everything or 'list:list' */
1987 1.12 christos if (strcmp(arg, "*") == 0)
1988 1.12 christos arg2 = arg;
1989 1.12 christos else {
1990 1.12 christos lowercase(arg);
1991 1.12 christos if ((arg2 = strchr(arg, ':')) == NULL ||
1992 1.12 christos arg2[1] == '\0') {
1993 1.33 christos error("%s line %d: "
1994 1.12 christos "Invalid permitted CNAME \"%s\"",
1995 1.12 christos filename, linenum, arg);
1996 1.33 christos return -1;
1997 1.12 christos }
1998 1.12 christos *arg2 = '\0';
1999 1.12 christos arg2++;
2000 1.12 christos }
2001 1.12 christos if (!*activep || value)
2002 1.12 christos continue;
2003 1.33 christos if (options->num_permitted_cnames >=
2004 1.33 christos MAX_CANON_DOMAINS) {
2005 1.33 christos error("%s line %d: too many permitted CNAMEs.",
2006 1.12 christos filename, linenum);
2007 1.33 christos return -1;
2008 1.33 christos }
2009 1.12 christos cname = options->permitted_cnames +
2010 1.12 christos options->num_permitted_cnames++;
2011 1.12 christos cname->source_list = xstrdup(arg);
2012 1.12 christos cname->target_list = xstrdup(arg2);
2013 1.12 christos }
2014 1.12 christos break;
2015 1.12 christos
2016 1.12 christos case oCanonicalizeHostname:
2017 1.12 christos intptr = &options->canonicalize_hostname;
2018 1.12 christos multistate_ptr = multistate_canonicalizehostname;
2019 1.12 christos goto parse_multistate;
2020 1.12 christos
2021 1.12 christos case oCanonicalizeMaxDots:
2022 1.12 christos intptr = &options->canonicalize_max_dots;
2023 1.12 christos goto parse_int;
2024 1.12 christos
2025 1.12 christos case oCanonicalizeFallbackLocal:
2026 1.12 christos intptr = &options->canonicalize_fallback_local;
2027 1.12 christos goto parse_flag;
2028 1.12 christos
2029 1.12 christos case oStreamLocalBindMask:
2030 1.12 christos arg = strdelim(&s);
2031 1.33 christos if (!arg || *arg == '\0') {
2032 1.33 christos error("%.200s line %d: Missing StreamLocalBindMask "
2033 1.33 christos "argument.", filename, linenum);
2034 1.33 christos return -1;
2035 1.33 christos }
2036 1.12 christos /* Parse mode in octal format */
2037 1.12 christos value = strtol(arg, &endofnumber, 8);
2038 1.33 christos if (arg == endofnumber || value < 0 || value > 0777) {
2039 1.33 christos error("%.200s line %d: Bad mask.", filename, linenum);
2040 1.33 christos return -1;
2041 1.33 christos }
2042 1.12 christos options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
2043 1.12 christos break;
2044 1.12 christos
2045 1.12 christos case oStreamLocalBindUnlink:
2046 1.12 christos intptr = &options->fwd_opts.streamlocal_bind_unlink;
2047 1.12 christos goto parse_flag;
2048 1.12 christos
2049 1.13 christos case oRevokedHostKeys:
2050 1.13 christos charptr = &options->revoked_host_keys;
2051 1.13 christos goto parse_string;
2052 1.13 christos
2053 1.13 christos case oFingerprintHash:
2054 1.13 christos intptr = &options->fingerprint_hash;
2055 1.13 christos arg = strdelim(&s);
2056 1.33 christos if (!arg || *arg == '\0') {
2057 1.33 christos error("%.200s line %d: Missing argument.",
2058 1.13 christos filename, linenum);
2059 1.33 christos return -1;
2060 1.33 christos }
2061 1.33 christos if ((value = ssh_digest_alg_by_name(arg)) == -1) {
2062 1.33 christos error("%.200s line %d: Invalid hash algorithm \"%s\".",
2063 1.13 christos filename, linenum, arg);
2064 1.33 christos return -1;
2065 1.33 christos }
2066 1.13 christos if (*activep && *intptr == -1)
2067 1.13 christos *intptr = value;
2068 1.13 christos break;
2069 1.13 christos
2070 1.13 christos case oUpdateHostkeys:
2071 1.13 christos intptr = &options->update_hostkeys;
2072 1.13 christos multistate_ptr = multistate_yesnoask;
2073 1.13 christos goto parse_multistate;
2074 1.13 christos
2075 1.33 christos case oHostbasedAcceptedAlgorithms:
2076 1.33 christos charptr = &options->hostbased_accepted_algos;
2077 1.33 christos goto parse_pubkey_algos;
2078 1.33 christos
2079 1.33 christos case oPubkeyAcceptedAlgorithms:
2080 1.33 christos charptr = &options->pubkey_accepted_algos;
2081 1.33 christos goto parse_pubkey_algos;
2082 1.13 christos
2083 1.18 christos case oAddKeysToAgent:
2084 1.32 christos arg = strdelim(&s);
2085 1.32 christos arg2 = strdelim(&s);
2086 1.32 christos value = parse_multistate_value(arg, filename, linenum,
2087 1.34 christos multistate_yesnoaskconfirm);
2088 1.32 christos value2 = 0; /* unlimited lifespan by default */
2089 1.32 christos if (value == 3 && arg2 != NULL) {
2090 1.32 christos /* allow "AddKeysToAgent confirm 5m" */
2091 1.33 christos if ((value2 = convtime(arg2)) == -1 ||
2092 1.33 christos value2 > INT_MAX) {
2093 1.33 christos error("%s line %d: invalid time value.",
2094 1.32 christos filename, linenum);
2095 1.33 christos return -1;
2096 1.33 christos }
2097 1.32 christos } else if (value == -1 && arg2 == NULL) {
2098 1.33 christos if ((value2 = convtime(arg)) == -1 ||
2099 1.33 christos value2 > INT_MAX) {
2100 1.33 christos error("%s line %d: unsupported option",
2101 1.32 christos filename, linenum);
2102 1.33 christos return -1;
2103 1.33 christos }
2104 1.32 christos value = 1; /* yes */
2105 1.32 christos } else if (value == -1 || arg2 != NULL) {
2106 1.33 christos error("%s line %d: unsupported option",
2107 1.32 christos filename, linenum);
2108 1.33 christos return -1;
2109 1.32 christos }
2110 1.32 christos if (*activep && options->add_keys_to_agent == -1) {
2111 1.32 christos options->add_keys_to_agent = value;
2112 1.32 christos options->add_keys_to_agent_lifespan = value2;
2113 1.32 christos }
2114 1.32 christos break;
2115 1.18 christos
2116 1.19 christos case oIdentityAgent:
2117 1.19 christos charptr = &options->identity_agent;
2118 1.27 christos arg = strdelim(&s);
2119 1.33 christos if (!arg || *arg == '\0') {
2120 1.33 christos error("%.200s line %d: Missing argument.",
2121 1.27 christos filename, linenum);
2122 1.33 christos return -1;
2123 1.33 christos }
2124 1.29 christos parse_agent_path:
2125 1.27 christos /* Extra validation if the string represents an env var. */
2126 1.33 christos if ((arg2 = dollar_expand(&r, arg)) == NULL || r) {
2127 1.33 christos error("%.200s line %d: Invalid environment expansion "
2128 1.32 christos "%s.", filename, linenum, arg);
2129 1.33 christos return -1;
2130 1.33 christos }
2131 1.32 christos free(arg2);
2132 1.32 christos /* check for legacy environment format */
2133 1.33 christos if (arg[0] == '$' && arg[1] != '{' &&
2134 1.33 christos !valid_env_name(arg + 1)) {
2135 1.33 christos error("%.200s line %d: Invalid environment name %s.",
2136 1.27 christos filename, linenum, arg);
2137 1.33 christos return -1;
2138 1.27 christos }
2139 1.27 christos if (*activep && *charptr == NULL)
2140 1.27 christos *charptr = xstrdup(arg);
2141 1.27 christos break;
2142 1.19 christos
2143 1.1 christos case oDeprecated:
2144 1.1 christos debug("%s line %d: Deprecated option \"%s\"",
2145 1.1 christos filename, linenum, keyword);
2146 1.1 christos return 0;
2147 1.1 christos
2148 1.1 christos case oUnsupported:
2149 1.1 christos error("%s line %d: Unsupported option \"%s\"",
2150 1.1 christos filename, linenum, keyword);
2151 1.1 christos return 0;
2152 1.1 christos
2153 1.1 christos default:
2154 1.33 christos error("%s line %d: Unimplemented opcode %d",
2155 1.33 christos filename, linenum, opcode);
2156 1.1 christos }
2157 1.1 christos
2158 1.1 christos /* Check that there is no garbage at end of line. */
2159 1.1 christos if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
2160 1.33 christos error("%.200s line %d: garbage at end of line; \"%.200s\".",
2161 1.1 christos filename, linenum, arg);
2162 1.33 christos return -1;
2163 1.1 christos }
2164 1.1 christos return 0;
2165 1.1 christos }
2166 1.1 christos
2167 1.1 christos /*
2168 1.1 christos * Reads the config file and modifies the options accordingly. Options
2169 1.1 christos * should already be initialized before this call. This never returns if
2170 1.1 christos * there is an error. If the file does not exist, this returns 0.
2171 1.1 christos */
2172 1.1 christos int
2173 1.12 christos read_config_file(const char *filename, struct passwd *pw, const char *host,
2174 1.27 christos const char *original_host, Options *options, int flags,
2175 1.27 christos int *want_final_pass)
2176 1.1 christos {
2177 1.19 christos int active = 1;
2178 1.19 christos
2179 1.19 christos return read_config_file_depth(filename, pw, host, original_host,
2180 1.27 christos options, flags, &active, want_final_pass, 0);
2181 1.19 christos }
2182 1.19 christos
2183 1.19 christos #define READCONF_MAX_DEPTH 16
2184 1.19 christos static int
2185 1.19 christos read_config_file_depth(const char *filename, struct passwd *pw,
2186 1.19 christos const char *host, const char *original_host, Options *options,
2187 1.27 christos int flags, int *activep, int *want_final_pass, int depth)
2188 1.19 christos {
2189 1.1 christos FILE *f;
2190 1.33 christos char *cp, *line = NULL;
2191 1.25 christos size_t linesize = 0;
2192 1.19 christos int linenum;
2193 1.1 christos int bad_options = 0;
2194 1.1 christos
2195 1.19 christos if (depth < 0 || depth > READCONF_MAX_DEPTH)
2196 1.19 christos fatal("Too many recursive configuration includes");
2197 1.19 christos
2198 1.1 christos if ((f = fopen(filename, "r")) == NULL)
2199 1.1 christos return 0;
2200 1.1 christos
2201 1.9 mlelstv if (flags & SSHCONF_CHECKPERM) {
2202 1.1 christos struct stat sb;
2203 1.1 christos
2204 1.1 christos if (fstat(fileno(f), &sb) == -1)
2205 1.1 christos fatal("fstat %s: %s", filename, strerror(errno));
2206 1.1 christos if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
2207 1.1 christos (sb.st_mode & 022) != 0))
2208 1.1 christos fatal("Bad owner or permissions on %s", filename);
2209 1.1 christos }
2210 1.1 christos
2211 1.1 christos debug("Reading configuration data %.200s", filename);
2212 1.1 christos
2213 1.1 christos /*
2214 1.1 christos * Mark that we are now processing the options. This flag is turned
2215 1.1 christos * on/off by Host specifications.
2216 1.1 christos */
2217 1.1 christos linenum = 0;
2218 1.25 christos while (getline(&line, &linesize, f) != -1) {
2219 1.1 christos /* Update line number counter. */
2220 1.1 christos linenum++;
2221 1.33 christos /*
2222 1.33 christos * Trim out comments and strip whitespace.
2223 1.33 christos * NB - preserve newlines, they are needed to reproduce
2224 1.33 christos * line numbers later for error messages.
2225 1.33 christos */
2226 1.33 christos if ((cp = strchr(line, '#')) != NULL)
2227 1.33 christos *cp = '\0';
2228 1.19 christos if (process_config_line_depth(options, pw, host, original_host,
2229 1.27 christos line, filename, linenum, activep, flags, want_final_pass,
2230 1.27 christos depth) != 0)
2231 1.1 christos bad_options++;
2232 1.1 christos }
2233 1.25 christos free(line);
2234 1.1 christos fclose(f);
2235 1.1 christos if (bad_options > 0)
2236 1.1 christos fatal("%s: terminating, %d bad configuration options",
2237 1.1 christos filename, bad_options);
2238 1.1 christos return 1;
2239 1.1 christos }
2240 1.1 christos
2241 1.12 christos /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
2242 1.12 christos int
2243 1.12 christos option_clear_or_none(const char *o)
2244 1.12 christos {
2245 1.12 christos return o == NULL || strcasecmp(o, "none") == 0;
2246 1.12 christos }
2247 1.12 christos
2248 1.1 christos /*
2249 1.1 christos * Initializes options to special values that indicate that they have not yet
2250 1.1 christos * been set. Read_config_file will only set options with this value. Options
2251 1.1 christos * are processed in the following order: command line, user config file,
2252 1.1 christos * system config file. Last, fill_default_options is called.
2253 1.1 christos */
2254 1.1 christos
2255 1.1 christos void
2256 1.1 christos initialize_options(Options * options)
2257 1.1 christos {
2258 1.1 christos memset(options, 'X', sizeof(*options));
2259 1.1 christos options->forward_agent = -1;
2260 1.29 christos options->forward_agent_sock_path = NULL;
2261 1.1 christos options->forward_x11 = -1;
2262 1.1 christos options->forward_x11_trusted = -1;
2263 1.4 adam options->forward_x11_timeout = -1;
2264 1.19 christos options->stdio_forward_host = NULL;
2265 1.19 christos options->stdio_forward_port = 0;
2266 1.19 christos options->clear_forwardings = -1;
2267 1.1 christos options->exit_on_forward_failure = -1;
2268 1.1 christos options->xauth_location = NULL;
2269 1.12 christos options->fwd_opts.gateway_ports = -1;
2270 1.12 christos options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
2271 1.12 christos options->fwd_opts.streamlocal_bind_unlink = -1;
2272 1.1 christos options->pubkey_authentication = -1;
2273 1.1 christos options->challenge_response_authentication = -1;
2274 1.2 christos #if defined(KRB4) || defined(KRB5)
2275 1.2 christos options->kerberos_authentication = -1;
2276 1.2 christos #endif
2277 1.2 christos #if defined(AFS) || defined(KRB5)
2278 1.2 christos options->kerberos_tgt_passing = -1;
2279 1.2 christos #endif
2280 1.2 christos #ifdef AFS
2281 1.2 christos options->afs_token_passing = -1;
2282 1.2 christos #endif
2283 1.1 christos options->gss_authentication = -1;
2284 1.1 christos options->gss_deleg_creds = -1;
2285 1.1 christos options->password_authentication = -1;
2286 1.1 christos options->kbd_interactive_authentication = -1;
2287 1.1 christos options->kbd_interactive_devices = NULL;
2288 1.1 christos options->hostbased_authentication = -1;
2289 1.1 christos options->batch_mode = -1;
2290 1.1 christos options->check_host_ip = -1;
2291 1.1 christos options->strict_host_key_checking = -1;
2292 1.1 christos options->compression = -1;
2293 1.1 christos options->tcp_keep_alive = -1;
2294 1.1 christos options->port = -1;
2295 1.1 christos options->address_family = -1;
2296 1.1 christos options->connection_attempts = -1;
2297 1.1 christos options->connection_timeout = -1;
2298 1.1 christos options->number_of_password_prompts = -1;
2299 1.1 christos options->ciphers = NULL;
2300 1.1 christos options->macs = NULL;
2301 1.5 christos options->kex_algorithms = NULL;
2302 1.1 christos options->hostkeyalgorithms = NULL;
2303 1.27 christos options->ca_sign_algorithms = NULL;
2304 1.1 christos options->num_identity_files = 0;
2305 1.33 christos memset(options->identity_keys, 0, sizeof(options->identity_keys));
2306 1.18 christos options->num_certificate_files = 0;
2307 1.33 christos memset(options->certificates, 0, sizeof(options->certificates));
2308 1.1 christos options->hostname = NULL;
2309 1.1 christos options->host_key_alias = NULL;
2310 1.1 christos options->proxy_command = NULL;
2311 1.19 christos options->jump_user = NULL;
2312 1.19 christos options->jump_host = NULL;
2313 1.19 christos options->jump_port = -1;
2314 1.19 christos options->jump_extra = NULL;
2315 1.1 christos options->user = NULL;
2316 1.1 christos options->escape_char = -1;
2317 1.6 christos options->num_system_hostfiles = 0;
2318 1.6 christos options->num_user_hostfiles = 0;
2319 1.4 adam options->local_forwards = NULL;
2320 1.1 christos options->num_local_forwards = 0;
2321 1.4 adam options->remote_forwards = NULL;
2322 1.1 christos options->num_remote_forwards = 0;
2323 1.33 christos options->permitted_remote_opens = NULL;
2324 1.33 christos options->num_permitted_remote_opens = 0;
2325 1.22 christos options->log_facility = SYSLOG_FACILITY_NOT_SET;
2326 1.1 christos options->log_level = SYSLOG_LEVEL_NOT_SET;
2327 1.33 christos options->num_log_verbose = 0;
2328 1.33 christos options->log_verbose = NULL;
2329 1.1 christos options->preferred_authentications = NULL;
2330 1.1 christos options->bind_address = NULL;
2331 1.23 christos options->bind_interface = NULL;
2332 1.4 adam options->pkcs11_provider = NULL;
2333 1.29 christos options->sk_provider = NULL;
2334 1.1 christos options->enable_ssh_keysign = - 1;
2335 1.1 christos options->no_host_authentication_for_localhost = - 1;
2336 1.1 christos options->identities_only = - 1;
2337 1.1 christos options->rekey_limit = - 1;
2338 1.11 christos options->rekey_interval = -1;
2339 1.1 christos options->verify_host_key_dns = -1;
2340 1.1 christos options->server_alive_interval = -1;
2341 1.1 christos options->server_alive_count_max = -1;
2342 1.25 christos options->send_env = NULL;
2343 1.1 christos options->num_send_env = 0;
2344 1.25 christos options->setenv = NULL;
2345 1.25 christos options->num_setenv = 0;
2346 1.1 christos options->control_path = NULL;
2347 1.1 christos options->control_master = -1;
2348 1.4 adam options->control_persist = -1;
2349 1.4 adam options->control_persist_timeout = 0;
2350 1.1 christos options->hash_known_hosts = -1;
2351 1.1 christos options->tun_open = -1;
2352 1.1 christos options->tun_local = -1;
2353 1.1 christos options->tun_remote = -1;
2354 1.1 christos options->local_command = NULL;
2355 1.1 christos options->permit_local_command = -1;
2356 1.22 christos options->remote_command = NULL;
2357 1.18 christos options->add_keys_to_agent = -1;
2358 1.32 christos options->add_keys_to_agent_lifespan = -1;
2359 1.19 christos options->identity_agent = NULL;
2360 1.1 christos options->visual_host_key = -1;
2361 1.5 christos options->ip_qos_interactive = -1;
2362 1.5 christos options->ip_qos_bulk = -1;
2363 1.6 christos options->request_tty = -1;
2364 1.12 christos options->proxy_use_fdpass = -1;
2365 1.11 christos options->ignored_unknown = NULL;
2366 1.12 christos options->num_canonical_domains = 0;
2367 1.12 christos options->num_permitted_cnames = 0;
2368 1.12 christos options->canonicalize_max_dots = -1;
2369 1.12 christos options->canonicalize_fallback_local = -1;
2370 1.12 christos options->canonicalize_hostname = -1;
2371 1.13 christos options->revoked_host_keys = NULL;
2372 1.13 christos options->fingerprint_hash = -1;
2373 1.13 christos options->update_hostkeys = -1;
2374 1.33 christos options->hostbased_accepted_algos = NULL;
2375 1.33 christos options->pubkey_accepted_algos = NULL;
2376 1.33 christos options->known_hosts_command = NULL;
2377 1.2 christos options->none_switch = -1;
2378 1.2 christos options->none_enabled = -1;
2379 1.2 christos options->hpn_disabled = -1;
2380 1.2 christos options->hpn_buffer_size = -1;
2381 1.2 christos options->tcp_rcv_buf_poll = -1;
2382 1.2 christos options->tcp_rcv_buf = -1;
2383 1.7 tls options->send_version_first = -1;
2384 1.1 christos }
2385 1.1 christos
2386 1.1 christos /*
2387 1.12 christos * A petite version of fill_default_options() that just fills the options
2388 1.12 christos * needed for hostname canonicalization to proceed.
2389 1.12 christos */
2390 1.12 christos void
2391 1.12 christos fill_default_options_for_canonicalization(Options *options)
2392 1.12 christos {
2393 1.12 christos if (options->canonicalize_max_dots == -1)
2394 1.12 christos options->canonicalize_max_dots = 1;
2395 1.12 christos if (options->canonicalize_fallback_local == -1)
2396 1.12 christos options->canonicalize_fallback_local = 1;
2397 1.12 christos if (options->canonicalize_hostname == -1)
2398 1.12 christos options->canonicalize_hostname = SSH_CANONICALISE_NO;
2399 1.12 christos }
2400 1.12 christos
2401 1.12 christos /*
2402 1.1 christos * Called after processing other sources of option data, this fills those
2403 1.1 christos * options for which no value has been specified with their default values.
2404 1.1 christos */
2405 1.33 christos int
2406 1.1 christos fill_default_options(Options * options)
2407 1.1 christos {
2408 1.27 christos char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
2409 1.29 christos char *def_cipher, *def_mac, *def_kex, *def_key, *def_sig;
2410 1.33 christos int ret = 0, r;
2411 1.25 christos
2412 1.1 christos if (options->forward_agent == -1)
2413 1.1 christos options->forward_agent = 0;
2414 1.1 christos if (options->forward_x11 == -1)
2415 1.1 christos options->forward_x11 = 0;
2416 1.1 christos if (options->forward_x11_trusted == -1)
2417 1.1 christos options->forward_x11_trusted = 0;
2418 1.4 adam if (options->forward_x11_timeout == -1)
2419 1.4 adam options->forward_x11_timeout = 1200;
2420 1.19 christos /*
2421 1.19 christos * stdio forwarding (-W) changes the default for these but we defer
2422 1.19 christos * setting the values so they can be overridden.
2423 1.19 christos */
2424 1.1 christos if (options->exit_on_forward_failure == -1)
2425 1.19 christos options->exit_on_forward_failure =
2426 1.19 christos options->stdio_forward_host != NULL ? 1 : 0;
2427 1.19 christos if (options->clear_forwardings == -1)
2428 1.19 christos options->clear_forwardings =
2429 1.19 christos options->stdio_forward_host != NULL ? 1 : 0;
2430 1.19 christos if (options->clear_forwardings == 1)
2431 1.19 christos clear_forwardings(options);
2432 1.19 christos
2433 1.1 christos if (options->xauth_location == NULL)
2434 1.33 christos options->xauth_location = xstrdup(_PATH_XAUTH);
2435 1.12 christos if (options->fwd_opts.gateway_ports == -1)
2436 1.12 christos options->fwd_opts.gateway_ports = 0;
2437 1.12 christos if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
2438 1.12 christos options->fwd_opts.streamlocal_bind_mask = 0177;
2439 1.12 christos if (options->fwd_opts.streamlocal_bind_unlink == -1)
2440 1.12 christos options->fwd_opts.streamlocal_bind_unlink = 0;
2441 1.1 christos if (options->pubkey_authentication == -1)
2442 1.1 christos options->pubkey_authentication = 1;
2443 1.1 christos if (options->challenge_response_authentication == -1)
2444 1.1 christos options->challenge_response_authentication = 1;
2445 1.2 christos #if defined(KRB4) || defined(KRB5)
2446 1.2 christos if (options->kerberos_authentication == -1)
2447 1.2 christos options->kerberos_authentication = 1;
2448 1.2 christos #endif
2449 1.2 christos #if defined(AFS) || defined(KRB5)
2450 1.2 christos if (options->kerberos_tgt_passing == -1)
2451 1.2 christos options->kerberos_tgt_passing = 1;
2452 1.2 christos #endif
2453 1.2 christos #ifdef AFS
2454 1.2 christos if (options->afs_token_passing == -1)
2455 1.2 christos options->afs_token_passing = 1;
2456 1.2 christos #endif
2457 1.1 christos if (options->gss_authentication == -1)
2458 1.1 christos options->gss_authentication = 0;
2459 1.1 christos if (options->gss_deleg_creds == -1)
2460 1.1 christos options->gss_deleg_creds = 0;
2461 1.1 christos if (options->password_authentication == -1)
2462 1.1 christos options->password_authentication = 1;
2463 1.1 christos if (options->kbd_interactive_authentication == -1)
2464 1.1 christos options->kbd_interactive_authentication = 1;
2465 1.1 christos if (options->hostbased_authentication == -1)
2466 1.1 christos options->hostbased_authentication = 0;
2467 1.1 christos if (options->batch_mode == -1)
2468 1.1 christos options->batch_mode = 0;
2469 1.1 christos if (options->check_host_ip == -1)
2470 1.33 christos options->check_host_ip = 0;
2471 1.1 christos if (options->strict_host_key_checking == -1)
2472 1.22 christos options->strict_host_key_checking = SSH_STRICT_HOSTKEY_ASK;
2473 1.1 christos if (options->compression == -1)
2474 1.1 christos options->compression = 0;
2475 1.1 christos if (options->tcp_keep_alive == -1)
2476 1.1 christos options->tcp_keep_alive = 1;
2477 1.1 christos if (options->port == -1)
2478 1.1 christos options->port = 0; /* Filled in ssh_connect. */
2479 1.1 christos if (options->address_family == -1)
2480 1.1 christos options->address_family = AF_UNSPEC;
2481 1.1 christos if (options->connection_attempts == -1)
2482 1.1 christos options->connection_attempts = 1;
2483 1.1 christos if (options->number_of_password_prompts == -1)
2484 1.1 christos options->number_of_password_prompts = 3;
2485 1.1 christos /* options->hostkeyalgorithms, default set in myproposals.h */
2486 1.32 christos if (options->add_keys_to_agent == -1) {
2487 1.18 christos options->add_keys_to_agent = 0;
2488 1.32 christos options->add_keys_to_agent_lifespan = 0;
2489 1.32 christos }
2490 1.1 christos if (options->num_identity_files == 0) {
2491 1.22 christos add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_RSA, 0);
2492 1.22 christos add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_DSA, 0);
2493 1.22 christos add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_ECDSA, 0);
2494 1.22 christos add_identity_file(options, "~/",
2495 1.29 christos _PATH_SSH_CLIENT_ID_ECDSA_SK, 0);
2496 1.29 christos add_identity_file(options, "~/",
2497 1.22 christos _PATH_SSH_CLIENT_ID_ED25519, 0);
2498 1.29 christos add_identity_file(options, "~/",
2499 1.29 christos _PATH_SSH_CLIENT_ID_ED25519_SK, 0);
2500 1.23 christos add_identity_file(options, "~/", _PATH_SSH_CLIENT_ID_XMSS, 0);
2501 1.1 christos }
2502 1.1 christos if (options->escape_char == -1)
2503 1.1 christos options->escape_char = '~';
2504 1.6 christos if (options->num_system_hostfiles == 0) {
2505 1.6 christos options->system_hostfiles[options->num_system_hostfiles++] =
2506 1.6 christos xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
2507 1.6 christos options->system_hostfiles[options->num_system_hostfiles++] =
2508 1.6 christos xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
2509 1.6 christos }
2510 1.33 christos if (options->update_hostkeys == -1) {
2511 1.33 christos if (options->verify_host_key_dns <= 0 &&
2512 1.33 christos (options->num_user_hostfiles == 0 ||
2513 1.33 christos (options->num_user_hostfiles == 1 && strcmp(options->
2514 1.33 christos user_hostfiles[0], _PATH_SSH_USER_HOSTFILE) == 0)))
2515 1.33 christos options->update_hostkeys = SSH_UPDATE_HOSTKEYS_YES;
2516 1.33 christos else
2517 1.29 christos options->update_hostkeys = SSH_UPDATE_HOSTKEYS_NO;
2518 1.33 christos }
2519 1.6 christos if (options->num_user_hostfiles == 0) {
2520 1.6 christos options->user_hostfiles[options->num_user_hostfiles++] =
2521 1.6 christos xstrdup(_PATH_SSH_USER_HOSTFILE);
2522 1.6 christos options->user_hostfiles[options->num_user_hostfiles++] =
2523 1.6 christos xstrdup(_PATH_SSH_USER_HOSTFILE2);
2524 1.6 christos }
2525 1.1 christos if (options->log_level == SYSLOG_LEVEL_NOT_SET)
2526 1.1 christos options->log_level = SYSLOG_LEVEL_INFO;
2527 1.22 christos if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2528 1.22 christos options->log_facility = SYSLOG_FACILITY_USER;
2529 1.1 christos if (options->no_host_authentication_for_localhost == - 1)
2530 1.1 christos options->no_host_authentication_for_localhost = 0;
2531 1.1 christos if (options->identities_only == -1)
2532 1.1 christos options->identities_only = 0;
2533 1.1 christos if (options->enable_ssh_keysign == -1)
2534 1.1 christos options->enable_ssh_keysign = 0;
2535 1.1 christos if (options->rekey_limit == -1)
2536 1.1 christos options->rekey_limit = 0;
2537 1.11 christos if (options->rekey_interval == -1)
2538 1.11 christos options->rekey_interval = 0;
2539 1.1 christos if (options->verify_host_key_dns == -1)
2540 1.1 christos options->verify_host_key_dns = 0;
2541 1.1 christos if (options->server_alive_interval == -1)
2542 1.1 christos options->server_alive_interval = 0;
2543 1.1 christos if (options->server_alive_count_max == -1)
2544 1.1 christos options->server_alive_count_max = 3;
2545 1.2 christos if (options->none_switch == -1)
2546 1.2 christos options->none_switch = 0;
2547 1.2 christos if (options->hpn_disabled == -1)
2548 1.2 christos options->hpn_disabled = 0;
2549 1.2 christos if (options->hpn_buffer_size > -1)
2550 1.2 christos {
2551 1.2 christos /* if a user tries to set the size to 0 set it to 1KB */
2552 1.2 christos if (options->hpn_buffer_size == 0)
2553 1.2 christos options->hpn_buffer_size = 1024;
2554 1.2 christos /*limit the buffer to 64MB*/
2555 1.2 christos if (options->hpn_buffer_size > 65536)
2556 1.2 christos {
2557 1.2 christos options->hpn_buffer_size = 65536*1024;
2558 1.2 christos debug("User requested buffer larger than 64MB. Request reverted to 64MB");
2559 1.2 christos }
2560 1.2 christos debug("hpn_buffer_size set to %d", options->hpn_buffer_size);
2561 1.2 christos }
2562 1.2 christos if (options->tcp_rcv_buf == 0)
2563 1.2 christos options->tcp_rcv_buf = 1;
2564 1.2 christos if (options->tcp_rcv_buf > -1)
2565 1.2 christos options->tcp_rcv_buf *=1024;
2566 1.2 christos if (options->tcp_rcv_buf_poll == -1)
2567 1.2 christos options->tcp_rcv_buf_poll = 1;
2568 1.1 christos if (options->control_master == -1)
2569 1.1 christos options->control_master = 0;
2570 1.4 adam if (options->control_persist == -1) {
2571 1.4 adam options->control_persist = 0;
2572 1.4 adam options->control_persist_timeout = 0;
2573 1.4 adam }
2574 1.1 christos if (options->hash_known_hosts == -1)
2575 1.1 christos options->hash_known_hosts = 0;
2576 1.1 christos if (options->tun_open == -1)
2577 1.1 christos options->tun_open = SSH_TUNMODE_NO;
2578 1.1 christos if (options->tun_local == -1)
2579 1.1 christos options->tun_local = SSH_TUNID_ANY;
2580 1.1 christos if (options->tun_remote == -1)
2581 1.1 christos options->tun_remote = SSH_TUNID_ANY;
2582 1.1 christos if (options->permit_local_command == -1)
2583 1.1 christos options->permit_local_command = 0;
2584 1.1 christos if (options->visual_host_key == -1)
2585 1.1 christos options->visual_host_key = 0;
2586 1.5 christos if (options->ip_qos_interactive == -1)
2587 1.25 christos options->ip_qos_interactive = IPTOS_DSCP_AF21;
2588 1.5 christos if (options->ip_qos_bulk == -1)
2589 1.25 christos options->ip_qos_bulk = IPTOS_DSCP_CS1;
2590 1.6 christos if (options->request_tty == -1)
2591 1.6 christos options->request_tty = REQUEST_TTY_AUTO;
2592 1.12 christos if (options->proxy_use_fdpass == -1)
2593 1.12 christos options->proxy_use_fdpass = 0;
2594 1.12 christos if (options->canonicalize_max_dots == -1)
2595 1.12 christos options->canonicalize_max_dots = 1;
2596 1.12 christos if (options->canonicalize_fallback_local == -1)
2597 1.12 christos options->canonicalize_fallback_local = 1;
2598 1.12 christos if (options->canonicalize_hostname == -1)
2599 1.12 christos options->canonicalize_hostname = SSH_CANONICALISE_NO;
2600 1.13 christos if (options->fingerprint_hash == -1)
2601 1.13 christos options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
2602 1.29 christos if (options->sk_provider == NULL)
2603 1.29 christos options->sk_provider = xstrdup("internal");
2604 1.25 christos
2605 1.25 christos /* Expand KEX name lists */
2606 1.25 christos all_cipher = cipher_alg_list(',', 0);
2607 1.25 christos all_mac = mac_alg_list(',');
2608 1.25 christos all_kex = kex_alg_list(',');
2609 1.25 christos all_key = sshkey_alg_list(0, 0, 1, ',');
2610 1.27 christos all_sig = sshkey_alg_list(0, 1, 1, ',');
2611 1.29 christos /* remove unsupported algos from default lists */
2612 1.32 christos def_cipher = match_filter_allowlist(KEX_CLIENT_ENCRYPT, all_cipher);
2613 1.32 christos def_mac = match_filter_allowlist(KEX_CLIENT_MAC, all_mac);
2614 1.32 christos def_kex = match_filter_allowlist(KEX_CLIENT_KEX, all_kex);
2615 1.32 christos def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
2616 1.32 christos def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
2617 1.25 christos #define ASSEMBLE(what, defaults, all) \
2618 1.25 christos do { \
2619 1.25 christos if ((r = kex_assemble_names(&options->what, \
2620 1.33 christos defaults, all)) != 0) { \
2621 1.33 christos error_fr(r, "%s", #what); \
2622 1.33 christos goto fail; \
2623 1.33 christos } \
2624 1.25 christos } while (0)
2625 1.29 christos ASSEMBLE(ciphers, def_cipher, all_cipher);
2626 1.29 christos ASSEMBLE(macs, def_mac, all_mac);
2627 1.29 christos ASSEMBLE(kex_algorithms, def_kex, all_kex);
2628 1.33 christos ASSEMBLE(hostbased_accepted_algos, def_key, all_key);
2629 1.33 christos ASSEMBLE(pubkey_accepted_algos, def_key, all_key);
2630 1.29 christos ASSEMBLE(ca_sign_algorithms, def_sig, all_sig);
2631 1.25 christos #undef ASSEMBLE
2632 1.13 christos
2633 1.7 tls if (options->send_version_first == -1)
2634 1.7 tls options->send_version_first = 1;
2635 1.12 christos #define CLEAR_ON_NONE(v) \
2636 1.12 christos do { \
2637 1.12 christos if (option_clear_or_none(v)) { \
2638 1.12 christos free(v); \
2639 1.12 christos v = NULL; \
2640 1.12 christos } \
2641 1.12 christos } while(0)
2642 1.12 christos CLEAR_ON_NONE(options->local_command);
2643 1.22 christos CLEAR_ON_NONE(options->remote_command);
2644 1.12 christos CLEAR_ON_NONE(options->proxy_command);
2645 1.12 christos CLEAR_ON_NONE(options->control_path);
2646 1.13 christos CLEAR_ON_NONE(options->revoked_host_keys);
2647 1.27 christos CLEAR_ON_NONE(options->pkcs11_provider);
2648 1.29 christos CLEAR_ON_NONE(options->sk_provider);
2649 1.33 christos CLEAR_ON_NONE(options->known_hosts_command);
2650 1.25 christos if (options->jump_host != NULL &&
2651 1.25 christos strcmp(options->jump_host, "none") == 0 &&
2652 1.25 christos options->jump_port == 0 && options->jump_user == NULL) {
2653 1.25 christos free(options->jump_host);
2654 1.25 christos options->jump_host = NULL;
2655 1.25 christos }
2656 1.19 christos /* options->identity_agent distinguishes NULL from 'none' */
2657 1.1 christos /* options->user will be set in the main program if appropriate */
2658 1.1 christos /* options->hostname will be set in the main program if appropriate */
2659 1.1 christos /* options->host_key_alias should not be set by default */
2660 1.1 christos /* options->preferred_authentications will be set in ssh */
2661 1.33 christos
2662 1.33 christos /* success */
2663 1.33 christos ret = 0;
2664 1.33 christos fail:
2665 1.33 christos free(all_cipher);
2666 1.33 christos free(all_mac);
2667 1.33 christos free(all_kex);
2668 1.33 christos free(all_key);
2669 1.33 christos free(all_sig);
2670 1.33 christos free(def_cipher);
2671 1.33 christos free(def_mac);
2672 1.33 christos free(def_kex);
2673 1.33 christos free(def_key);
2674 1.33 christos free(def_sig);
2675 1.33 christos return ret;
2676 1.33 christos }
2677 1.33 christos
2678 1.33 christos void
2679 1.33 christos free_options(Options *o)
2680 1.33 christos {
2681 1.33 christos int i;
2682 1.33 christos
2683 1.33 christos if (o == NULL)
2684 1.33 christos return;
2685 1.33 christos
2686 1.33 christos #define FREE_ARRAY(type, n, a) \
2687 1.33 christos do { \
2688 1.33 christos type _i; \
2689 1.33 christos for (_i = 0; _i < (n); _i++) \
2690 1.33 christos free((a)[_i]); \
2691 1.33 christos } while (0)
2692 1.33 christos
2693 1.33 christos free(o->forward_agent_sock_path);
2694 1.33 christos free(o->xauth_location);
2695 1.33 christos FREE_ARRAY(u_int, o->num_log_verbose, o->log_verbose);
2696 1.33 christos free(o->log_verbose);
2697 1.33 christos free(o->ciphers);
2698 1.33 christos free(o->macs);
2699 1.33 christos free(o->hostkeyalgorithms);
2700 1.33 christos free(o->kex_algorithms);
2701 1.33 christos free(o->ca_sign_algorithms);
2702 1.33 christos free(o->hostname);
2703 1.33 christos free(o->host_key_alias);
2704 1.33 christos free(o->proxy_command);
2705 1.33 christos free(o->user);
2706 1.33 christos FREE_ARRAY(u_int, o->num_system_hostfiles, o->system_hostfiles);
2707 1.33 christos FREE_ARRAY(u_int, o->num_user_hostfiles, o->user_hostfiles);
2708 1.33 christos free(o->preferred_authentications);
2709 1.33 christos free(o->bind_address);
2710 1.33 christos free(o->bind_interface);
2711 1.33 christos free(o->pkcs11_provider);
2712 1.33 christos free(o->sk_provider);
2713 1.33 christos for (i = 0; i < o->num_identity_files; i++) {
2714 1.33 christos free(o->identity_files[i]);
2715 1.33 christos sshkey_free(o->identity_keys[i]);
2716 1.33 christos }
2717 1.33 christos for (i = 0; i < o->num_certificate_files; i++) {
2718 1.33 christos free(o->certificate_files[i]);
2719 1.33 christos sshkey_free(o->certificates[i]);
2720 1.33 christos }
2721 1.33 christos free(o->identity_agent);
2722 1.33 christos for (i = 0; i < o->num_local_forwards; i++) {
2723 1.33 christos free(o->local_forwards[i].listen_host);
2724 1.33 christos free(o->local_forwards[i].listen_path);
2725 1.33 christos free(o->local_forwards[i].connect_host);
2726 1.33 christos free(o->local_forwards[i].connect_path);
2727 1.33 christos }
2728 1.33 christos free(o->local_forwards);
2729 1.33 christos for (i = 0; i < o->num_remote_forwards; i++) {
2730 1.33 christos free(o->remote_forwards[i].listen_host);
2731 1.33 christos free(o->remote_forwards[i].listen_path);
2732 1.33 christos free(o->remote_forwards[i].connect_host);
2733 1.33 christos free(o->remote_forwards[i].connect_path);
2734 1.33 christos }
2735 1.33 christos free(o->remote_forwards);
2736 1.33 christos free(o->stdio_forward_host);
2737 1.33 christos FREE_ARRAY(int, o->num_send_env, o->send_env);
2738 1.33 christos free(o->send_env);
2739 1.33 christos FREE_ARRAY(int, o->num_setenv, o->setenv);
2740 1.33 christos free(o->setenv);
2741 1.33 christos free(o->control_path);
2742 1.33 christos free(o->local_command);
2743 1.33 christos free(o->remote_command);
2744 1.33 christos FREE_ARRAY(int, o->num_canonical_domains, o->canonical_domains);
2745 1.33 christos for (i = 0; i < o->num_permitted_cnames; i++) {
2746 1.33 christos free(o->permitted_cnames[i].source_list);
2747 1.33 christos free(o->permitted_cnames[i].target_list);
2748 1.33 christos }
2749 1.33 christos free(o->revoked_host_keys);
2750 1.33 christos free(o->hostbased_accepted_algos);
2751 1.33 christos free(o->pubkey_accepted_algos);
2752 1.33 christos free(o->jump_user);
2753 1.33 christos free(o->jump_host);
2754 1.33 christos free(o->jump_extra);
2755 1.33 christos free(o->ignored_unknown);
2756 1.33 christos explicit_bzero(o, sizeof(*o));
2757 1.33 christos #undef FREE_ARRAY
2758 1.1 christos }
2759 1.1 christos
2760 1.12 christos struct fwdarg {
2761 1.12 christos char *arg;
2762 1.12 christos int ispath;
2763 1.12 christos };
2764 1.12 christos
2765 1.12 christos /*
2766 1.12 christos * parse_fwd_field
2767 1.12 christos * parses the next field in a port forwarding specification.
2768 1.12 christos * sets fwd to the parsed field and advances p past the colon
2769 1.12 christos * or sets it to NULL at end of string.
2770 1.12 christos * returns 0 on success, else non-zero.
2771 1.12 christos */
2772 1.12 christos static int
2773 1.12 christos parse_fwd_field(char **p, struct fwdarg *fwd)
2774 1.12 christos {
2775 1.12 christos char *ep, *cp = *p;
2776 1.12 christos int ispath = 0;
2777 1.12 christos
2778 1.12 christos if (*cp == '\0') {
2779 1.12 christos *p = NULL;
2780 1.12 christos return -1; /* end of string */
2781 1.12 christos }
2782 1.12 christos
2783 1.12 christos /*
2784 1.12 christos * A field escaped with square brackets is used literally.
2785 1.12 christos * XXX - allow ']' to be escaped via backslash?
2786 1.12 christos */
2787 1.12 christos if (*cp == '[') {
2788 1.12 christos /* find matching ']' */
2789 1.12 christos for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) {
2790 1.12 christos if (*ep == '/')
2791 1.12 christos ispath = 1;
2792 1.12 christos }
2793 1.12 christos /* no matching ']' or not at end of field. */
2794 1.12 christos if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0'))
2795 1.12 christos return -1;
2796 1.12 christos /* NUL terminate the field and advance p past the colon */
2797 1.12 christos *ep++ = '\0';
2798 1.12 christos if (*ep != '\0')
2799 1.12 christos *ep++ = '\0';
2800 1.12 christos fwd->arg = cp + 1;
2801 1.12 christos fwd->ispath = ispath;
2802 1.12 christos *p = ep;
2803 1.12 christos return 0;
2804 1.12 christos }
2805 1.12 christos
2806 1.12 christos for (cp = *p; *cp != '\0'; cp++) {
2807 1.12 christos switch (*cp) {
2808 1.12 christos case '\\':
2809 1.12 christos memmove(cp, cp + 1, strlen(cp + 1) + 1);
2810 1.14 christos if (*cp == '\0')
2811 1.14 christos return -1;
2812 1.12 christos break;
2813 1.12 christos case '/':
2814 1.12 christos ispath = 1;
2815 1.12 christos break;
2816 1.12 christos case ':':
2817 1.12 christos *cp++ = '\0';
2818 1.12 christos goto done;
2819 1.12 christos }
2820 1.12 christos }
2821 1.12 christos done:
2822 1.12 christos fwd->arg = *p;
2823 1.12 christos fwd->ispath = ispath;
2824 1.12 christos *p = cp;
2825 1.12 christos return 0;
2826 1.12 christos }
2827 1.12 christos
2828 1.1 christos /*
2829 1.1 christos * parse_forward
2830 1.1 christos * parses a string containing a port forwarding specification of the form:
2831 1.1 christos * dynamicfwd == 0
2832 1.12 christos * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath
2833 1.12 christos * listenpath:connectpath
2834 1.1 christos * dynamicfwd == 1
2835 1.1 christos * [listenhost:]listenport
2836 1.1 christos * returns number of arguments parsed or zero on error
2837 1.1 christos */
2838 1.1 christos int
2839 1.12 christos parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
2840 1.1 christos {
2841 1.12 christos struct fwdarg fwdargs[4];
2842 1.12 christos char *p, *cp;
2843 1.32 christos int i, err;
2844 1.1 christos
2845 1.12 christos memset(fwd, 0, sizeof(*fwd));
2846 1.12 christos memset(fwdargs, 0, sizeof(fwdargs));
2847 1.1 christos
2848 1.32 christos /*
2849 1.32 christos * We expand environment variables before checking if we think they're
2850 1.32 christos * paths so that if ${VAR} expands to a fully qualified path it is
2851 1.32 christos * treated as a path.
2852 1.32 christos */
2853 1.32 christos cp = p = dollar_expand(&err, fwdspec);
2854 1.32 christos if (p == NULL || err)
2855 1.32 christos return 0;
2856 1.1 christos
2857 1.1 christos /* skip leading spaces */
2858 1.12 christos while (isspace((u_char)*cp))
2859 1.1 christos cp++;
2860 1.1 christos
2861 1.12 christos for (i = 0; i < 4; ++i) {
2862 1.12 christos if (parse_fwd_field(&cp, &fwdargs[i]) != 0)
2863 1.1 christos break;
2864 1.12 christos }
2865 1.1 christos
2866 1.1 christos /* Check for trailing garbage */
2867 1.12 christos if (cp != NULL && *cp != '\0') {
2868 1.1 christos i = 0; /* failure */
2869 1.12 christos }
2870 1.1 christos
2871 1.1 christos switch (i) {
2872 1.1 christos case 1:
2873 1.12 christos if (fwdargs[0].ispath) {
2874 1.12 christos fwd->listen_path = xstrdup(fwdargs[0].arg);
2875 1.12 christos fwd->listen_port = PORT_STREAMLOCAL;
2876 1.12 christos } else {
2877 1.12 christos fwd->listen_host = NULL;
2878 1.12 christos fwd->listen_port = a2port(fwdargs[0].arg);
2879 1.12 christos }
2880 1.1 christos fwd->connect_host = xstrdup("socks");
2881 1.1 christos break;
2882 1.1 christos
2883 1.1 christos case 2:
2884 1.12 christos if (fwdargs[0].ispath && fwdargs[1].ispath) {
2885 1.12 christos fwd->listen_path = xstrdup(fwdargs[0].arg);
2886 1.12 christos fwd->listen_port = PORT_STREAMLOCAL;
2887 1.12 christos fwd->connect_path = xstrdup(fwdargs[1].arg);
2888 1.12 christos fwd->connect_port = PORT_STREAMLOCAL;
2889 1.12 christos } else if (fwdargs[1].ispath) {
2890 1.12 christos fwd->listen_host = NULL;
2891 1.12 christos fwd->listen_port = a2port(fwdargs[0].arg);
2892 1.12 christos fwd->connect_path = xstrdup(fwdargs[1].arg);
2893 1.12 christos fwd->connect_port = PORT_STREAMLOCAL;
2894 1.12 christos } else {
2895 1.12 christos fwd->listen_host = xstrdup(fwdargs[0].arg);
2896 1.12 christos fwd->listen_port = a2port(fwdargs[1].arg);
2897 1.12 christos fwd->connect_host = xstrdup("socks");
2898 1.12 christos }
2899 1.1 christos break;
2900 1.1 christos
2901 1.1 christos case 3:
2902 1.12 christos if (fwdargs[0].ispath) {
2903 1.12 christos fwd->listen_path = xstrdup(fwdargs[0].arg);
2904 1.12 christos fwd->listen_port = PORT_STREAMLOCAL;
2905 1.12 christos fwd->connect_host = xstrdup(fwdargs[1].arg);
2906 1.12 christos fwd->connect_port = a2port(fwdargs[2].arg);
2907 1.12 christos } else if (fwdargs[2].ispath) {
2908 1.12 christos fwd->listen_host = xstrdup(fwdargs[0].arg);
2909 1.12 christos fwd->listen_port = a2port(fwdargs[1].arg);
2910 1.12 christos fwd->connect_path = xstrdup(fwdargs[2].arg);
2911 1.12 christos fwd->connect_port = PORT_STREAMLOCAL;
2912 1.12 christos } else {
2913 1.12 christos fwd->listen_host = NULL;
2914 1.12 christos fwd->listen_port = a2port(fwdargs[0].arg);
2915 1.12 christos fwd->connect_host = xstrdup(fwdargs[1].arg);
2916 1.12 christos fwd->connect_port = a2port(fwdargs[2].arg);
2917 1.12 christos }
2918 1.1 christos break;
2919 1.1 christos
2920 1.1 christos case 4:
2921 1.12 christos fwd->listen_host = xstrdup(fwdargs[0].arg);
2922 1.12 christos fwd->listen_port = a2port(fwdargs[1].arg);
2923 1.12 christos fwd->connect_host = xstrdup(fwdargs[2].arg);
2924 1.12 christos fwd->connect_port = a2port(fwdargs[3].arg);
2925 1.1 christos break;
2926 1.1 christos default:
2927 1.1 christos i = 0; /* failure */
2928 1.1 christos }
2929 1.1 christos
2930 1.11 christos free(p);
2931 1.1 christos
2932 1.1 christos if (dynamicfwd) {
2933 1.1 christos if (!(i == 1 || i == 2))
2934 1.1 christos goto fail_free;
2935 1.1 christos } else {
2936 1.12 christos if (!(i == 3 || i == 4)) {
2937 1.12 christos if (fwd->connect_path == NULL &&
2938 1.12 christos fwd->listen_path == NULL)
2939 1.12 christos goto fail_free;
2940 1.12 christos }
2941 1.12 christos if (fwd->connect_port <= 0 && fwd->connect_path == NULL)
2942 1.1 christos goto fail_free;
2943 1.1 christos }
2944 1.1 christos
2945 1.12 christos if ((fwd->listen_port < 0 && fwd->listen_path == NULL) ||
2946 1.12 christos (!remotefwd && fwd->listen_port == 0))
2947 1.1 christos goto fail_free;
2948 1.1 christos if (fwd->connect_host != NULL &&
2949 1.1 christos strlen(fwd->connect_host) >= NI_MAXHOST)
2950 1.1 christos goto fail_free;
2951 1.12 christos /* XXX - if connecting to a remote socket, max sun len may not match this host */
2952 1.12 christos if (fwd->connect_path != NULL &&
2953 1.12 christos strlen(fwd->connect_path) >= PATH_MAX_SUN)
2954 1.12 christos goto fail_free;
2955 1.1 christos if (fwd->listen_host != NULL &&
2956 1.1 christos strlen(fwd->listen_host) >= NI_MAXHOST)
2957 1.1 christos goto fail_free;
2958 1.12 christos if (fwd->listen_path != NULL &&
2959 1.12 christos strlen(fwd->listen_path) >= PATH_MAX_SUN)
2960 1.12 christos goto fail_free;
2961 1.1 christos
2962 1.1 christos return (i);
2963 1.1 christos
2964 1.1 christos fail_free:
2965 1.11 christos free(fwd->connect_host);
2966 1.11 christos fwd->connect_host = NULL;
2967 1.12 christos free(fwd->connect_path);
2968 1.12 christos fwd->connect_path = NULL;
2969 1.11 christos free(fwd->listen_host);
2970 1.11 christos fwd->listen_host = NULL;
2971 1.12 christos free(fwd->listen_path);
2972 1.12 christos fwd->listen_path = NULL;
2973 1.1 christos return (0);
2974 1.1 christos }
2975 1.13 christos
2976 1.19 christos int
2977 1.19 christos parse_jump(const char *s, Options *o, int active)
2978 1.19 christos {
2979 1.19 christos char *orig, *sdup, *cp;
2980 1.19 christos char *host = NULL, *user = NULL;
2981 1.33 christos int r, ret = -1, port = -1, first;
2982 1.19 christos
2983 1.19 christos active &= o->proxy_command == NULL && o->jump_host == NULL;
2984 1.19 christos
2985 1.19 christos orig = sdup = xstrdup(s);
2986 1.19 christos first = active;
2987 1.19 christos do {
2988 1.25 christos if (strcasecmp(s, "none") == 0)
2989 1.25 christos break;
2990 1.19 christos if ((cp = strrchr(sdup, ',')) == NULL)
2991 1.19 christos cp = sdup; /* last */
2992 1.19 christos else
2993 1.19 christos *cp++ = '\0';
2994 1.19 christos
2995 1.19 christos if (first) {
2996 1.19 christos /* First argument and configuration is active */
2997 1.33 christos r = parse_ssh_uri(cp, &user, &host, &port);
2998 1.33 christos if (r == -1 || (r == 1 &&
2999 1.33 christos parse_user_host_port(cp, &user, &host, &port) != 0))
3000 1.19 christos goto out;
3001 1.19 christos } else {
3002 1.19 christos /* Subsequent argument or inactive configuration */
3003 1.33 christos r = parse_ssh_uri(cp, NULL, NULL, NULL);
3004 1.33 christos if (r == -1 || (r == 1 &&
3005 1.33 christos parse_user_host_port(cp, NULL, NULL, NULL) != 0))
3006 1.19 christos goto out;
3007 1.19 christos }
3008 1.19 christos first = 0; /* only check syntax for subsequent hosts */
3009 1.19 christos } while (cp != sdup);
3010 1.19 christos /* success */
3011 1.19 christos if (active) {
3012 1.25 christos if (strcasecmp(s, "none") == 0) {
3013 1.25 christos o->jump_host = xstrdup("none");
3014 1.25 christos o->jump_port = 0;
3015 1.25 christos } else {
3016 1.25 christos o->jump_user = user;
3017 1.25 christos o->jump_host = host;
3018 1.25 christos o->jump_port = port;
3019 1.25 christos o->proxy_command = xstrdup("none");
3020 1.25 christos user = host = NULL;
3021 1.25 christos if ((cp = strrchr(s, ',')) != NULL && cp != s) {
3022 1.25 christos o->jump_extra = xstrdup(s);
3023 1.25 christos o->jump_extra[cp - s] = '\0';
3024 1.25 christos }
3025 1.19 christos }
3026 1.19 christos }
3027 1.19 christos ret = 0;
3028 1.19 christos out:
3029 1.19 christos free(orig);
3030 1.19 christos free(user);
3031 1.19 christos free(host);
3032 1.19 christos return ret;
3033 1.19 christos }
3034 1.19 christos
3035 1.23 christos int
3036 1.23 christos parse_ssh_uri(const char *uri, char **userp, char **hostp, int *portp)
3037 1.23 christos {
3038 1.33 christos char *user = NULL, *host = NULL, *path = NULL;
3039 1.33 christos int r, port;
3040 1.23 christos
3041 1.33 christos r = parse_uri("ssh", uri, &user, &host, &port, &path);
3042 1.23 christos if (r == 0 && path != NULL)
3043 1.23 christos r = -1; /* path not allowed */
3044 1.33 christos if (r == 0) {
3045 1.33 christos if (userp != NULL) {
3046 1.33 christos *userp = user;
3047 1.33 christos user = NULL;
3048 1.33 christos }
3049 1.33 christos if (hostp != NULL) {
3050 1.33 christos *hostp = host;
3051 1.33 christos host = NULL;
3052 1.33 christos }
3053 1.33 christos if (portp != NULL)
3054 1.33 christos *portp = port;
3055 1.33 christos }
3056 1.33 christos free(user);
3057 1.33 christos free(host);
3058 1.33 christos free(path);
3059 1.23 christos return r;
3060 1.23 christos }
3061 1.23 christos
3062 1.13 christos /* XXX the following is a near-vebatim copy from servconf.c; refactor */
3063 1.13 christos static const char *
3064 1.13 christos fmt_multistate_int(int val, const struct multistate *m)
3065 1.13 christos {
3066 1.13 christos u_int i;
3067 1.13 christos
3068 1.13 christos for (i = 0; m[i].key != NULL; i++) {
3069 1.13 christos if (m[i].value == val)
3070 1.13 christos return m[i].key;
3071 1.13 christos }
3072 1.13 christos return "UNKNOWN";
3073 1.13 christos }
3074 1.13 christos
3075 1.13 christos static const char *
3076 1.13 christos fmt_intarg(OpCodes code, int val)
3077 1.13 christos {
3078 1.13 christos if (val == -1)
3079 1.13 christos return "unset";
3080 1.13 christos switch (code) {
3081 1.13 christos case oAddressFamily:
3082 1.13 christos return fmt_multistate_int(val, multistate_addressfamily);
3083 1.13 christos case oVerifyHostKeyDNS:
3084 1.13 christos case oUpdateHostkeys:
3085 1.13 christos return fmt_multistate_int(val, multistate_yesnoask);
3086 1.22 christos case oStrictHostKeyChecking:
3087 1.22 christos return fmt_multistate_int(val, multistate_strict_hostkey);
3088 1.13 christos case oControlMaster:
3089 1.13 christos return fmt_multistate_int(val, multistate_controlmaster);
3090 1.13 christos case oTunnel:
3091 1.13 christos return fmt_multistate_int(val, multistate_tunnel);
3092 1.13 christos case oRequestTTY:
3093 1.13 christos return fmt_multistate_int(val, multistate_requesttty);
3094 1.13 christos case oCanonicalizeHostname:
3095 1.13 christos return fmt_multistate_int(val, multistate_canonicalizehostname);
3096 1.25 christos case oAddKeysToAgent:
3097 1.25 christos return fmt_multistate_int(val, multistate_yesnoaskconfirm);
3098 1.13 christos case oFingerprintHash:
3099 1.13 christos return ssh_digest_alg_name(val);
3100 1.13 christos default:
3101 1.13 christos switch (val) {
3102 1.13 christos case 0:
3103 1.13 christos return "no";
3104 1.13 christos case 1:
3105 1.13 christos return "yes";
3106 1.13 christos default:
3107 1.13 christos return "UNKNOWN";
3108 1.13 christos }
3109 1.13 christos }
3110 1.13 christos }
3111 1.13 christos
3112 1.13 christos static const char *
3113 1.13 christos lookup_opcode_name(OpCodes code)
3114 1.13 christos {
3115 1.13 christos u_int i;
3116 1.13 christos
3117 1.13 christos for (i = 0; keywords[i].name != NULL; i++)
3118 1.13 christos if (keywords[i].opcode == code)
3119 1.13 christos return(keywords[i].name);
3120 1.13 christos return "UNKNOWN";
3121 1.13 christos }
3122 1.13 christos
3123 1.13 christos static void
3124 1.13 christos dump_cfg_int(OpCodes code, int val)
3125 1.13 christos {
3126 1.13 christos printf("%s %d\n", lookup_opcode_name(code), val);
3127 1.13 christos }
3128 1.13 christos
3129 1.13 christos static void
3130 1.13 christos dump_cfg_fmtint(OpCodes code, int val)
3131 1.13 christos {
3132 1.13 christos printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
3133 1.13 christos }
3134 1.13 christos
3135 1.13 christos static void
3136 1.13 christos dump_cfg_string(OpCodes code, const char *val)
3137 1.13 christos {
3138 1.13 christos if (val == NULL)
3139 1.13 christos return;
3140 1.13 christos printf("%s %s\n", lookup_opcode_name(code), val);
3141 1.13 christos }
3142 1.13 christos
3143 1.13 christos static void
3144 1.13 christos dump_cfg_strarray(OpCodes code, u_int count, char **vals)
3145 1.13 christos {
3146 1.13 christos u_int i;
3147 1.13 christos
3148 1.13 christos for (i = 0; i < count; i++)
3149 1.13 christos printf("%s %s\n", lookup_opcode_name(code), vals[i]);
3150 1.13 christos }
3151 1.13 christos
3152 1.13 christos static void
3153 1.13 christos dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals)
3154 1.13 christos {
3155 1.13 christos u_int i;
3156 1.13 christos
3157 1.13 christos printf("%s", lookup_opcode_name(code));
3158 1.13 christos for (i = 0; i < count; i++)
3159 1.13 christos printf(" %s", vals[i]);
3160 1.13 christos printf("\n");
3161 1.13 christos }
3162 1.13 christos
3163 1.13 christos static void
3164 1.13 christos dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds)
3165 1.13 christos {
3166 1.13 christos const struct Forward *fwd;
3167 1.13 christos u_int i;
3168 1.13 christos
3169 1.13 christos /* oDynamicForward */
3170 1.13 christos for (i = 0; i < count; i++) {
3171 1.13 christos fwd = &fwds[i];
3172 1.21 christos if (code == oDynamicForward && fwd->connect_host != NULL &&
3173 1.13 christos strcmp(fwd->connect_host, "socks") != 0)
3174 1.13 christos continue;
3175 1.21 christos if (code == oLocalForward && fwd->connect_host != NULL &&
3176 1.13 christos strcmp(fwd->connect_host, "socks") == 0)
3177 1.13 christos continue;
3178 1.13 christos printf("%s", lookup_opcode_name(code));
3179 1.13 christos if (fwd->listen_port == PORT_STREAMLOCAL)
3180 1.13 christos printf(" %s", fwd->listen_path);
3181 1.13 christos else if (fwd->listen_host == NULL)
3182 1.13 christos printf(" %d", fwd->listen_port);
3183 1.13 christos else {
3184 1.13 christos printf(" [%s]:%d",
3185 1.13 christos fwd->listen_host, fwd->listen_port);
3186 1.13 christos }
3187 1.13 christos if (code != oDynamicForward) {
3188 1.13 christos if (fwd->connect_port == PORT_STREAMLOCAL)
3189 1.13 christos printf(" %s", fwd->connect_path);
3190 1.13 christos else if (fwd->connect_host == NULL)
3191 1.13 christos printf(" %d", fwd->connect_port);
3192 1.13 christos else {
3193 1.13 christos printf(" [%s]:%d",
3194 1.13 christos fwd->connect_host, fwd->connect_port);
3195 1.13 christos }
3196 1.13 christos }
3197 1.13 christos printf("\n");
3198 1.13 christos }
3199 1.13 christos }
3200 1.13 christos
3201 1.13 christos void
3202 1.13 christos dump_client_config(Options *o, const char *host)
3203 1.13 christos {
3204 1.29 christos int i, r;
3205 1.25 christos char buf[8], *all_key;
3206 1.13 christos
3207 1.29 christos /*
3208 1.29 christos * Expand HostKeyAlgorithms name lists. This isn't handled in
3209 1.29 christos * fill_default_options() like the other algorithm lists because
3210 1.29 christos * the host key algorithms are by default dynamically chosen based
3211 1.29 christos * on the host's keys found in known_hosts.
3212 1.29 christos */
3213 1.25 christos all_key = sshkey_alg_list(0, 0, 1, ',');
3214 1.29 christos if ((r = kex_assemble_names(&o->hostkeyalgorithms, kex_default_pk_alg(),
3215 1.29 christos all_key)) != 0)
3216 1.33 christos fatal_fr(r, "expand HostKeyAlgorithms");
3217 1.25 christos free(all_key);
3218 1.18 christos
3219 1.13 christos /* Most interesting options first: user, host, port */
3220 1.13 christos dump_cfg_string(oUser, o->user);
3221 1.28 christos dump_cfg_string(oHostname, host);
3222 1.13 christos dump_cfg_int(oPort, o->port);
3223 1.13 christos
3224 1.13 christos /* Flag options */
3225 1.13 christos dump_cfg_fmtint(oAddressFamily, o->address_family);
3226 1.13 christos dump_cfg_fmtint(oBatchMode, o->batch_mode);
3227 1.13 christos dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local);
3228 1.13 christos dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname);
3229 1.13 christos dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication);
3230 1.13 christos dump_cfg_fmtint(oCheckHostIP, o->check_host_ip);
3231 1.13 christos dump_cfg_fmtint(oCompression, o->compression);
3232 1.13 christos dump_cfg_fmtint(oControlMaster, o->control_master);
3233 1.13 christos dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign);
3234 1.19 christos dump_cfg_fmtint(oClearAllForwardings, o->clear_forwardings);
3235 1.13 christos dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure);
3236 1.13 christos dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash);
3237 1.13 christos dump_cfg_fmtint(oForwardX11, o->forward_x11);
3238 1.13 christos dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted);
3239 1.13 christos dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
3240 1.13 christos #ifdef GSSAPI
3241 1.13 christos dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
3242 1.13 christos dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
3243 1.13 christos #endif /* GSSAPI */
3244 1.13 christos dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
3245 1.13 christos dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
3246 1.13 christos dump_cfg_fmtint(oIdentitiesOnly, o->identities_only);
3247 1.13 christos dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication);
3248 1.13 christos dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost);
3249 1.13 christos dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication);
3250 1.13 christos dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command);
3251 1.13 christos dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass);
3252 1.13 christos dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication);
3253 1.13 christos dump_cfg_fmtint(oRequestTTY, o->request_tty);
3254 1.13 christos dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
3255 1.13 christos dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking);
3256 1.13 christos dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive);
3257 1.13 christos dump_cfg_fmtint(oTunnel, o->tun_open);
3258 1.13 christos dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns);
3259 1.13 christos dump_cfg_fmtint(oVisualHostKey, o->visual_host_key);
3260 1.13 christos dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys);
3261 1.13 christos
3262 1.13 christos /* Integer options */
3263 1.13 christos dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots);
3264 1.13 christos dump_cfg_int(oConnectionAttempts, o->connection_attempts);
3265 1.13 christos dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout);
3266 1.13 christos dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts);
3267 1.13 christos dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max);
3268 1.13 christos dump_cfg_int(oServerAliveInterval, o->server_alive_interval);
3269 1.13 christos
3270 1.13 christos /* String options */
3271 1.13 christos dump_cfg_string(oBindAddress, o->bind_address);
3272 1.23 christos dump_cfg_string(oBindInterface, o->bind_interface);
3273 1.29 christos dump_cfg_string(oCiphers, o->ciphers);
3274 1.13 christos dump_cfg_string(oControlPath, o->control_path);
3275 1.18 christos dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms);
3276 1.13 christos dump_cfg_string(oHostKeyAlias, o->host_key_alias);
3277 1.33 christos dump_cfg_string(oHostbasedAcceptedAlgorithms, o->hostbased_accepted_algos);
3278 1.19 christos dump_cfg_string(oIdentityAgent, o->identity_agent);
3279 1.25 christos dump_cfg_string(oIgnoreUnknown, o->ignored_unknown);
3280 1.13 christos dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
3281 1.29 christos dump_cfg_string(oKexAlgorithms, o->kex_algorithms);
3282 1.29 christos dump_cfg_string(oCASignatureAlgorithms, o->ca_sign_algorithms);
3283 1.13 christos dump_cfg_string(oLocalCommand, o->local_command);
3284 1.22 christos dump_cfg_string(oRemoteCommand, o->remote_command);
3285 1.13 christos dump_cfg_string(oLogLevel, log_level_name(o->log_level));
3286 1.29 christos dump_cfg_string(oMacs, o->macs);
3287 1.21 christos #ifdef ENABLE_PKCS11
3288 1.13 christos dump_cfg_string(oPKCS11Provider, o->pkcs11_provider);
3289 1.21 christos #endif
3290 1.29 christos dump_cfg_string(oSecurityKeyProvider, o->sk_provider);
3291 1.13 christos dump_cfg_string(oPreferredAuthentications, o->preferred_authentications);
3292 1.33 christos dump_cfg_string(oPubkeyAcceptedAlgorithms, o->pubkey_accepted_algos);
3293 1.13 christos dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys);
3294 1.13 christos dump_cfg_string(oXAuthLocation, o->xauth_location);
3295 1.33 christos dump_cfg_string(oKnownHostsCommand, o->known_hosts_command);
3296 1.13 christos
3297 1.13 christos /* Forwards */
3298 1.13 christos dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards);
3299 1.13 christos dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards);
3300 1.13 christos dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards);
3301 1.13 christos
3302 1.13 christos /* String array options */
3303 1.13 christos dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files);
3304 1.13 christos dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains);
3305 1.25 christos dump_cfg_strarray(oCertificateFile, o->num_certificate_files, o->certificate_files);
3306 1.13 christos dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles);
3307 1.13 christos dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles);
3308 1.13 christos dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env);
3309 1.25 christos dump_cfg_strarray(oSetEnv, o->num_setenv, o->setenv);
3310 1.33 christos dump_cfg_strarray_oneline(oLogVerbose,
3311 1.33 christos o->num_log_verbose, o->log_verbose);
3312 1.13 christos
3313 1.13 christos /* Special cases */
3314 1.13 christos
3315 1.33 christos /* PermitRemoteOpen */
3316 1.33 christos if (o->num_permitted_remote_opens == 0)
3317 1.33 christos printf("%s any\n", lookup_opcode_name(oPermitRemoteOpen));
3318 1.33 christos else
3319 1.33 christos dump_cfg_strarray_oneline(oPermitRemoteOpen,
3320 1.33 christos o->num_permitted_remote_opens, o->permitted_remote_opens);
3321 1.33 christos
3322 1.32 christos /* AddKeysToAgent */
3323 1.32 christos if (o->add_keys_to_agent_lifespan <= 0)
3324 1.32 christos dump_cfg_fmtint(oAddKeysToAgent, o->add_keys_to_agent);
3325 1.32 christos else {
3326 1.32 christos printf("addkeystoagent%s %d\n",
3327 1.32 christos o->add_keys_to_agent == 3 ? " confirm" : "",
3328 1.32 christos o->add_keys_to_agent_lifespan);
3329 1.32 christos }
3330 1.32 christos
3331 1.29 christos /* oForwardAgent */
3332 1.29 christos if (o->forward_agent_sock_path == NULL)
3333 1.29 christos dump_cfg_fmtint(oForwardAgent, o->forward_agent);
3334 1.29 christos else
3335 1.29 christos dump_cfg_string(oForwardAgent, o->forward_agent_sock_path);
3336 1.29 christos
3337 1.13 christos /* oConnectTimeout */
3338 1.13 christos if (o->connection_timeout == -1)
3339 1.13 christos printf("connecttimeout none\n");
3340 1.13 christos else
3341 1.13 christos dump_cfg_int(oConnectTimeout, o->connection_timeout);
3342 1.13 christos
3343 1.13 christos /* oTunnelDevice */
3344 1.13 christos printf("tunneldevice");
3345 1.13 christos if (o->tun_local == SSH_TUNID_ANY)
3346 1.13 christos printf(" any");
3347 1.13 christos else
3348 1.13 christos printf(" %d", o->tun_local);
3349 1.13 christos if (o->tun_remote == SSH_TUNID_ANY)
3350 1.13 christos printf(":any");
3351 1.13 christos else
3352 1.13 christos printf(":%d", o->tun_remote);
3353 1.13 christos printf("\n");
3354 1.13 christos
3355 1.13 christos /* oCanonicalizePermittedCNAMEs */
3356 1.13 christos if ( o->num_permitted_cnames > 0) {
3357 1.13 christos printf("canonicalizePermittedcnames");
3358 1.13 christos for (i = 0; i < o->num_permitted_cnames; i++) {
3359 1.13 christos printf(" %s:%s", o->permitted_cnames[i].source_list,
3360 1.13 christos o->permitted_cnames[i].target_list);
3361 1.13 christos }
3362 1.13 christos printf("\n");
3363 1.13 christos }
3364 1.13 christos
3365 1.13 christos /* oControlPersist */
3366 1.13 christos if (o->control_persist == 0 || o->control_persist_timeout == 0)
3367 1.13 christos dump_cfg_fmtint(oControlPersist, o->control_persist);
3368 1.13 christos else
3369 1.13 christos dump_cfg_int(oControlPersist, o->control_persist_timeout);
3370 1.13 christos
3371 1.13 christos /* oEscapeChar */
3372 1.13 christos if (o->escape_char == SSH_ESCAPECHAR_NONE)
3373 1.13 christos printf("escapechar none\n");
3374 1.13 christos else {
3375 1.19 christos vis(buf, o->escape_char, VIS_WHITE, 0);
3376 1.19 christos printf("escapechar %s\n", buf);
3377 1.13 christos }
3378 1.13 christos
3379 1.13 christos /* oIPQoS */
3380 1.13 christos printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
3381 1.13 christos printf("%s\n", iptos2str(o->ip_qos_bulk));
3382 1.13 christos
3383 1.13 christos /* oRekeyLimit */
3384 1.18 christos printf("rekeylimit %llu %d\n",
3385 1.18 christos (unsigned long long)o->rekey_limit, o->rekey_interval);
3386 1.13 christos
3387 1.13 christos /* oStreamLocalBindMask */
3388 1.13 christos printf("streamlocalbindmask 0%o\n",
3389 1.13 christos o->fwd_opts.streamlocal_bind_mask);
3390 1.19 christos
3391 1.25 christos /* oLogFacility */
3392 1.25 christos printf("syslogfacility %s\n", log_facility_name(o->log_facility));
3393 1.25 christos
3394 1.19 christos /* oProxyCommand / oProxyJump */
3395 1.19 christos if (o->jump_host == NULL)
3396 1.19 christos dump_cfg_string(oProxyCommand, o->proxy_command);
3397 1.19 christos else {
3398 1.19 christos /* Check for numeric addresses */
3399 1.19 christos i = strchr(o->jump_host, ':') != NULL ||
3400 1.19 christos strspn(o->jump_host, "1234567890.") == strlen(o->jump_host);
3401 1.19 christos snprintf(buf, sizeof(buf), "%d", o->jump_port);
3402 1.19 christos printf("proxyjump %s%s%s%s%s%s%s%s%s\n",
3403 1.19 christos /* optional additional jump spec */
3404 1.19 christos o->jump_extra == NULL ? "" : o->jump_extra,
3405 1.19 christos o->jump_extra == NULL ? "" : ",",
3406 1.19 christos /* optional user */
3407 1.19 christos o->jump_user == NULL ? "" : o->jump_user,
3408 1.19 christos o->jump_user == NULL ? "" : "@",
3409 1.19 christos /* opening [ if hostname is numeric */
3410 1.19 christos i ? "[" : "",
3411 1.19 christos /* mandatory hostname */
3412 1.19 christos o->jump_host,
3413 1.19 christos /* closing ] if hostname is numeric */
3414 1.19 christos i ? "]" : "",
3415 1.19 christos /* optional port number */
3416 1.19 christos o->jump_port <= 0 ? "" : ":",
3417 1.19 christos o->jump_port <= 0 ? "" : buf);
3418 1.19 christos }
3419 1.13 christos }
3420