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