1 /* $OpenBSD: readconf.c,v 1.189 2010/09/22 05:01:29 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Functions for reading the configuration files.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
17 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <netinet/in.h>
36 #include "pathnames.h"
46 /* Format of the configuration file:
48 # Configuration data is parsed as follows:
49 # 1. command line options
50 # 2. user-specific file
52 # Any configuration value is only changed the first time it is set.
53 # Thus, host-specific definitions should be at the beginning of the
54 # configuration file, and defaults at the end.
56 # Host-specific declarations. These may override anything above. A single
57 # host may match multiple declarations; these are processed in the order
58 # that they are given in.
64 HostName another.host.name.real.org
71 RemoteForward 9999 shadows.cs.hut.fi:9999
77 PasswordAuthentication no
81 ProxyCommand ssh-proxy %h %p
84 PublicKeyAuthentication no
88 PasswordAuthentication no
94 # Defaults for various options
98 PasswordAuthentication yes
100 RhostsRSAAuthentication yes
101 StrictHostKeyChecking yes
103 IdentityFile ~/.ssh/identity
109 /* Keyword tokens. */
113 oForwardAgent
, oForwardX11
, oForwardX11Trusted
, oForwardX11Timeout
,
114 oGatewayPorts
, oExitOnForwardFailure
,
115 oPasswordAuthentication
, oRSAAuthentication
,
116 oChallengeResponseAuthentication
, oXAuthLocation
,
117 oIdentityFile
, oHostName
, oPort
, oCipher
, oRemoteForward
, oLocalForward
,
118 oUser
, oHost
, oEscapeChar
, oRhostsRSAAuthentication
, oProxyCommand
,
119 oGlobalKnownHostsFile
, oUserKnownHostsFile
, oConnectionAttempts
,
120 oBatchMode
, oCheckHostIP
, oStrictHostKeyChecking
, oCompression
,
121 oCompressionLevel
, oTCPKeepAlive
, oNumberOfPasswordPrompts
,
122 oUsePrivilegedPort
, oLogLevel
, oCiphers
, oProtocol
, oMacs
,
123 oGlobalKnownHostsFile2
, oUserKnownHostsFile2
, oPubkeyAuthentication
,
124 oKbdInteractiveAuthentication
, oKbdInteractiveDevices
, oHostKeyAlias
,
125 oDynamicForward
, oPreferredAuthentications
, oHostbasedAuthentication
,
126 oHostKeyAlgorithms
, oBindAddress
, oPKCS11Provider
,
127 oClearAllForwardings
, oNoHostAuthenticationForLocalhost
,
128 oEnableSSHKeysign
, oRekeyLimit
, oVerifyHostKeyDNS
, oConnectTimeout
,
129 oAddressFamily
, oGssAuthentication
, oGssDelegateCreds
,
130 oServerAliveInterval
, oServerAliveCountMax
, oIdentitiesOnly
,
131 oSendEnv
, oControlPath
, oControlMaster
, oControlPersist
,
133 oTunnel
, oTunnelDevice
, oLocalCommand
, oPermitLocalCommand
,
134 oVisualHostKey
, oUseRoaming
, oZeroKnowledgePasswordAuthentication
,
136 oDeprecated
, oUnsupported
139 /* Textual representations of the tokens. */
145 { "forwardagent", oForwardAgent
},
146 { "forwardx11", oForwardX11
},
147 { "forwardx11trusted", oForwardX11Trusted
},
148 { "forwardx11timeout", oForwardX11Timeout
},
149 { "exitonforwardfailure", oExitOnForwardFailure
},
150 { "xauthlocation", oXAuthLocation
},
151 { "gatewayports", oGatewayPorts
},
152 { "useprivilegedport", oUsePrivilegedPort
},
153 { "rhostsauthentication", oDeprecated
},
154 { "passwordauthentication", oPasswordAuthentication
},
155 { "kbdinteractiveauthentication", oKbdInteractiveAuthentication
},
156 { "kbdinteractivedevices", oKbdInteractiveDevices
},
157 { "rsaauthentication", oRSAAuthentication
},
158 { "pubkeyauthentication", oPubkeyAuthentication
},
159 { "dsaauthentication", oPubkeyAuthentication
}, /* alias */
160 { "rhostsrsaauthentication", oRhostsRSAAuthentication
},
161 { "hostbasedauthentication", oHostbasedAuthentication
},
162 { "challengeresponseauthentication", oChallengeResponseAuthentication
},
163 { "skeyauthentication", oChallengeResponseAuthentication
}, /* alias */
164 { "tisauthentication", oChallengeResponseAuthentication
}, /* alias */
165 { "kerberosauthentication", oUnsupported
},
166 { "kerberostgtpassing", oUnsupported
},
167 { "afstokenpassing", oUnsupported
},
169 { "gssapiauthentication", oGssAuthentication
},
170 { "gssapidelegatecredentials", oGssDelegateCreds
},
172 { "gssapiauthentication", oUnsupported
},
173 { "gssapidelegatecredentials", oUnsupported
},
175 { "fallbacktorsh", oDeprecated
},
176 { "usersh", oDeprecated
},
177 { "identityfile", oIdentityFile
},
178 { "identityfile2", oIdentityFile
}, /* obsolete */
179 { "identitiesonly", oIdentitiesOnly
},
180 { "hostname", oHostName
},
181 { "hostkeyalias", oHostKeyAlias
},
182 { "proxycommand", oProxyCommand
},
184 { "cipher", oCipher
},
185 { "ciphers", oCiphers
},
187 { "protocol", oProtocol
},
188 { "remoteforward", oRemoteForward
},
189 { "localforward", oLocalForward
},
192 { "escapechar", oEscapeChar
},
193 { "globalknownhostsfile", oGlobalKnownHostsFile
},
194 { "globalknownhostsfile2", oGlobalKnownHostsFile2
}, /* obsolete */
195 { "userknownhostsfile", oUserKnownHostsFile
},
196 { "userknownhostsfile2", oUserKnownHostsFile2
}, /* obsolete */
197 { "connectionattempts", oConnectionAttempts
},
198 { "batchmode", oBatchMode
},
199 { "checkhostip", oCheckHostIP
},
200 { "stricthostkeychecking", oStrictHostKeyChecking
},
201 { "compression", oCompression
},
202 { "compressionlevel", oCompressionLevel
},
203 { "tcpkeepalive", oTCPKeepAlive
},
204 { "keepalive", oTCPKeepAlive
}, /* obsolete */
205 { "numberofpasswordprompts", oNumberOfPasswordPrompts
},
206 { "loglevel", oLogLevel
},
207 { "dynamicforward", oDynamicForward
},
208 { "preferredauthentications", oPreferredAuthentications
},
209 { "hostkeyalgorithms", oHostKeyAlgorithms
},
210 { "bindaddress", oBindAddress
},
212 { "smartcarddevice", oPKCS11Provider
},
213 { "pkcs11provider", oPKCS11Provider
},
215 { "smartcarddevice", oUnsupported
},
216 { "pkcs11provider", oUnsupported
},
218 { "clearallforwardings", oClearAllForwardings
},
219 { "enablesshkeysign", oEnableSSHKeysign
},
220 { "verifyhostkeydns", oVerifyHostKeyDNS
},
221 { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost
},
222 { "rekeylimit", oRekeyLimit
},
223 { "connecttimeout", oConnectTimeout
},
224 { "addressfamily", oAddressFamily
},
225 { "serveraliveinterval", oServerAliveInterval
},
226 { "serveralivecountmax", oServerAliveCountMax
},
227 { "sendenv", oSendEnv
},
228 { "controlpath", oControlPath
},
229 { "controlmaster", oControlMaster
},
230 { "controlpersist", oControlPersist
},
231 { "hashknownhosts", oHashKnownHosts
},
232 { "tunnel", oTunnel
},
233 { "tunneldevice", oTunnelDevice
},
234 { "localcommand", oLocalCommand
},
235 { "permitlocalcommand", oPermitLocalCommand
},
236 { "visualhostkey", oVisualHostKey
},
237 { "useroaming", oUseRoaming
},
239 { "zeroknowledgepasswordauthentication",
240 oZeroKnowledgePasswordAuthentication
},
242 { "zeroknowledgepasswordauthentication", oUnsupported
},
244 { "kexalgorithms", oKexAlgorithms
},
250 * Adds a local TCP/IP port forward to options. Never returns if there is an
255 add_local_forward(Options
*options
, const Forward
*newfwd
)
258 #ifndef NO_IPPORT_RESERVED_CONCEPT
259 extern uid_t original_real_uid
;
260 if (newfwd
->listen_port
< IPPORT_RESERVED
&& original_real_uid
!= 0)
261 fatal("Privileged ports can only be forwarded by root.");
263 options
->local_forwards
= xrealloc(options
->local_forwards
,
264 options
->num_local_forwards
+ 1,
265 sizeof(*options
->local_forwards
));
266 fwd
= &options
->local_forwards
[options
->num_local_forwards
++];
268 fwd
->listen_host
= newfwd
->listen_host
;
269 fwd
->listen_port
= newfwd
->listen_port
;
270 fwd
->connect_host
= newfwd
->connect_host
;
271 fwd
->connect_port
= newfwd
->connect_port
;
275 * Adds a remote TCP/IP port forward to options. Never returns if there is
280 add_remote_forward(Options
*options
, const Forward
*newfwd
)
284 options
->remote_forwards
= xrealloc(options
->remote_forwards
,
285 options
->num_remote_forwards
+ 1,
286 sizeof(*options
->remote_forwards
));
287 fwd
= &options
->remote_forwards
[options
->num_remote_forwards
++];
289 fwd
->listen_host
= newfwd
->listen_host
;
290 fwd
->listen_port
= newfwd
->listen_port
;
291 fwd
->connect_host
= newfwd
->connect_host
;
292 fwd
->connect_port
= newfwd
->connect_port
;
293 fwd
->allocated_port
= 0;
297 clear_forwardings(Options
*options
)
301 for (i
= 0; i
< options
->num_local_forwards
; i
++) {
302 if (options
->local_forwards
[i
].listen_host
!= NULL
)
303 xfree(options
->local_forwards
[i
].listen_host
);
304 xfree(options
->local_forwards
[i
].connect_host
);
306 if (options
->num_local_forwards
> 0) {
307 xfree(options
->local_forwards
);
308 options
->local_forwards
= NULL
;
310 options
->num_local_forwards
= 0;
311 for (i
= 0; i
< options
->num_remote_forwards
; i
++) {
312 if (options
->remote_forwards
[i
].listen_host
!= NULL
)
313 xfree(options
->remote_forwards
[i
].listen_host
);
314 xfree(options
->remote_forwards
[i
].connect_host
);
316 if (options
->num_remote_forwards
> 0) {
317 xfree(options
->remote_forwards
);
318 options
->remote_forwards
= NULL
;
320 options
->num_remote_forwards
= 0;
321 options
->tun_open
= SSH_TUNMODE_NO
;
325 * Returns the number of the token pointed to by cp or oBadOption.
329 parse_token(const char *cp
, const char *filename
, int linenum
)
333 for (i
= 0; keywords
[i
].name
; i
++)
334 if (strcasecmp(cp
, keywords
[i
].name
) == 0)
335 return keywords
[i
].opcode
;
337 error("%s: line %d: Bad configuration option: %s",
338 filename
, linenum
, cp
);
343 * Processes a single option line as used in the configuration files. This
344 * only sets those values that have not already been set.
346 #define WHITESPACE " \t\r\n"
349 process_config_line(Options
*options
, const char *host
,
350 char *line
, const char *filename
, int linenum
,
353 char *s
, **charptr
, *endofnumber
, *keyword
, *arg
, *arg2
, fwdarg
[256];
354 int opcode
, *intptr
, value
, value2
, scale
;
355 LogLevel
*log_level_ptr
;
356 long long orig
, val64
;
360 /* Strip trailing whitespace */
361 for (len
= strlen(line
) - 1; len
> 0; len
--) {
362 if (strchr(WHITESPACE
, line
[len
]) == NULL
)
368 /* Get the keyword. (Each line is supposed to begin with a keyword). */
369 if ((keyword
= strdelim(&s
)) == NULL
)
371 /* Ignore leading whitespace. */
372 if (*keyword
== '\0')
373 keyword
= strdelim(&s
);
374 if (keyword
== NULL
|| !*keyword
|| *keyword
== '\n' || *keyword
== '#')
377 opcode
= parse_token(keyword
, filename
, linenum
);
381 /* don't panic, but count bad options */
384 case oConnectTimeout
:
385 intptr
= &options
->connection_timeout
;
388 if (!arg
|| *arg
== '\0')
389 fatal("%s line %d: missing time value.",
391 if ((value
= convtime(arg
)) == -1)
392 fatal("%s line %d: invalid time value.",
394 if (*activep
&& *intptr
== -1)
399 intptr
= &options
->forward_agent
;
402 if (!arg
|| *arg
== '\0')
403 fatal("%.200s line %d: Missing yes/no argument.", filename
, linenum
);
404 value
= 0; /* To avoid compiler warning... */
405 if (strcmp(arg
, "yes") == 0 || strcmp(arg
, "true") == 0)
407 else if (strcmp(arg
, "no") == 0 || strcmp(arg
, "false") == 0)
410 fatal("%.200s line %d: Bad yes/no argument.", filename
, linenum
);
411 if (*activep
&& *intptr
== -1)
416 intptr
= &options
->forward_x11
;
419 case oForwardX11Trusted
:
420 intptr
= &options
->forward_x11_trusted
;
423 case oForwardX11Timeout
:
424 intptr
= &options
->forward_x11_timeout
;
428 intptr
= &options
->gateway_ports
;
431 case oExitOnForwardFailure
:
432 intptr
= &options
->exit_on_forward_failure
;
435 case oUsePrivilegedPort
:
436 intptr
= &options
->use_privileged_port
;
439 case oPasswordAuthentication
:
440 intptr
= &options
->password_authentication
;
443 case oZeroKnowledgePasswordAuthentication
:
444 intptr
= &options
->zero_knowledge_password_authentication
;
447 case oKbdInteractiveAuthentication
:
448 intptr
= &options
->kbd_interactive_authentication
;
451 case oKbdInteractiveDevices
:
452 charptr
= &options
->kbd_interactive_devices
;
455 case oPubkeyAuthentication
:
456 intptr
= &options
->pubkey_authentication
;
459 case oRSAAuthentication
:
460 intptr
= &options
->rsa_authentication
;
463 case oRhostsRSAAuthentication
:
464 intptr
= &options
->rhosts_rsa_authentication
;
467 case oHostbasedAuthentication
:
468 intptr
= &options
->hostbased_authentication
;
471 case oChallengeResponseAuthentication
:
472 intptr
= &options
->challenge_response_authentication
;
475 case oGssAuthentication
:
476 intptr
= &options
->gss_authentication
;
479 case oGssDelegateCreds
:
480 intptr
= &options
->gss_deleg_creds
;
484 intptr
= &options
->batch_mode
;
488 intptr
= &options
->check_host_ip
;
491 case oVerifyHostKeyDNS
:
492 intptr
= &options
->verify_host_key_dns
;
495 case oStrictHostKeyChecking
:
496 intptr
= &options
->strict_host_key_checking
;
499 if (!arg
|| *arg
== '\0')
500 fatal("%.200s line %d: Missing yes/no/ask argument.",
502 value
= 0; /* To avoid compiler warning... */
503 if (strcmp(arg
, "yes") == 0 || strcmp(arg
, "true") == 0)
505 else if (strcmp(arg
, "no") == 0 || strcmp(arg
, "false") == 0)
507 else if (strcmp(arg
, "ask") == 0)
510 fatal("%.200s line %d: Bad yes/no/ask argument.", filename
, linenum
);
511 if (*activep
&& *intptr
== -1)
516 intptr
= &options
->compression
;
520 intptr
= &options
->tcp_keep_alive
;
523 case oNoHostAuthenticationForLocalhost
:
524 intptr
= &options
->no_host_authentication_for_localhost
;
527 case oNumberOfPasswordPrompts
:
528 intptr
= &options
->number_of_password_prompts
;
531 case oCompressionLevel
:
532 intptr
= &options
->compression_level
;
537 if (!arg
|| *arg
== '\0')
538 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
539 if (arg
[0] < '0' || arg
[0] > '9')
540 fatal("%.200s line %d: Bad number.", filename
, linenum
);
541 orig
= val64
= strtoll(arg
, &endofnumber
, 10);
542 if (arg
== endofnumber
)
543 fatal("%.200s line %d: Bad number.", filename
, linenum
);
544 switch (toupper(*endofnumber
)) {
558 fatal("%.200s line %d: Invalid RekeyLimit suffix",
562 /* detect integer wrap and too-large limits */
563 if ((val64
/ scale
) != orig
|| val64
> UINT_MAX
)
564 fatal("%.200s line %d: RekeyLimit too large",
567 fatal("%.200s line %d: RekeyLimit too small",
569 if (*activep
&& options
->rekey_limit
== -1)
570 options
->rekey_limit
= (u_int32_t
)val64
;
575 if (!arg
|| *arg
== '\0')
576 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
578 intptr
= &options
->num_identity_files
;
579 if (*intptr
>= SSH_MAX_IDENTITY_FILES
)
580 fatal("%.200s line %d: Too many identity files specified (max %d).",
581 filename
, linenum
, SSH_MAX_IDENTITY_FILES
);
582 charptr
= &options
->identity_files
[*intptr
];
583 *charptr
= xstrdup(arg
);
584 *intptr
= *intptr
+ 1;
589 charptr
=&options
->xauth_location
;
593 charptr
= &options
->user
;
596 if (!arg
|| *arg
== '\0')
597 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
598 if (*activep
&& *charptr
== NULL
)
599 *charptr
= xstrdup(arg
);
602 case oGlobalKnownHostsFile
:
603 charptr
= &options
->system_hostfile
;
606 case oUserKnownHostsFile
:
607 charptr
= &options
->user_hostfile
;
610 case oGlobalKnownHostsFile2
:
611 charptr
= &options
->system_hostfile2
;
614 case oUserKnownHostsFile2
:
615 charptr
= &options
->user_hostfile2
;
619 charptr
= &options
->hostname
;
623 charptr
= &options
->host_key_alias
;
626 case oPreferredAuthentications
:
627 charptr
= &options
->preferred_authentications
;
631 charptr
= &options
->bind_address
;
634 case oPKCS11Provider
:
635 charptr
= &options
->pkcs11_provider
;
639 charptr
= &options
->proxy_command
;
642 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
643 len
= strspn(s
, WHITESPACE
"=");
644 if (*activep
&& *charptr
== NULL
)
645 *charptr
= xstrdup(s
+ len
);
649 intptr
= &options
->port
;
652 if (!arg
|| *arg
== '\0')
653 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
654 if (arg
[0] < '0' || arg
[0] > '9')
655 fatal("%.200s line %d: Bad number.", filename
, linenum
);
657 /* Octal, decimal, or hex format? */
658 value
= strtol(arg
, &endofnumber
, 0);
659 if (arg
== endofnumber
)
660 fatal("%.200s line %d: Bad number.", filename
, linenum
);
661 if (*activep
&& *intptr
== -1)
665 case oConnectionAttempts
:
666 intptr
= &options
->connection_attempts
;
670 intptr
= &options
->cipher
;
672 if (!arg
|| *arg
== '\0')
673 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
674 value
= cipher_number(arg
);
676 fatal("%.200s line %d: Bad cipher '%s'.",
677 filename
, linenum
, arg
? arg
: "<NONE>");
678 if (*activep
&& *intptr
== -1)
684 if (!arg
|| *arg
== '\0')
685 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
686 if (!ciphers_valid(arg
))
687 fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
688 filename
, linenum
, arg
? arg
: "<NONE>");
689 if (*activep
&& options
->ciphers
== NULL
)
690 options
->ciphers
= xstrdup(arg
);
695 if (!arg
|| *arg
== '\0')
696 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
698 fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
699 filename
, linenum
, arg
? arg
: "<NONE>");
700 if (*activep
&& options
->macs
== NULL
)
701 options
->macs
= xstrdup(arg
);
706 if (!arg
|| *arg
== '\0')
707 fatal("%.200s line %d: Missing argument.",
709 if (!kex_names_valid(arg
))
710 fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
711 filename
, linenum
, arg
? arg
: "<NONE>");
712 if (*activep
&& options
->kex_algorithms
== NULL
)
713 options
->kex_algorithms
= xstrdup(arg
);
716 case oHostKeyAlgorithms
:
718 if (!arg
|| *arg
== '\0')
719 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
720 if (!key_names_valid2(arg
))
721 fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
722 filename
, linenum
, arg
? arg
: "<NONE>");
723 if (*activep
&& options
->hostkeyalgorithms
== NULL
)
724 options
->hostkeyalgorithms
= xstrdup(arg
);
728 intptr
= &options
->protocol
;
730 if (!arg
|| *arg
== '\0')
731 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
732 value
= proto_spec(arg
);
733 if (value
== SSH_PROTO_UNKNOWN
)
734 fatal("%.200s line %d: Bad protocol spec '%s'.",
735 filename
, linenum
, arg
? arg
: "<NONE>");
736 if (*activep
&& *intptr
== SSH_PROTO_UNKNOWN
)
741 log_level_ptr
= &options
->log_level
;
743 value
= log_level_number(arg
);
744 if (value
== SYSLOG_LEVEL_NOT_SET
)
745 fatal("%.200s line %d: unsupported log level '%s'",
746 filename
, linenum
, arg
? arg
: "<NONE>");
747 if (*activep
&& *log_level_ptr
== SYSLOG_LEVEL_NOT_SET
)
748 *log_level_ptr
= (LogLevel
) value
;
753 case oDynamicForward
:
755 if (arg
== NULL
|| *arg
== '\0')
756 fatal("%.200s line %d: Missing port argument.",
759 if (opcode
== oLocalForward
||
760 opcode
== oRemoteForward
) {
762 if (arg2
== NULL
|| *arg2
== '\0')
763 fatal("%.200s line %d: Missing target argument.",
766 /* construct a string for parse_forward */
767 snprintf(fwdarg
, sizeof(fwdarg
), "%s:%s", arg
, arg2
);
768 } else if (opcode
== oDynamicForward
) {
769 strlcpy(fwdarg
, arg
, sizeof(fwdarg
));
772 if (parse_forward(&fwd
, fwdarg
,
773 opcode
== oDynamicForward
? 1 : 0,
774 opcode
== oRemoteForward
? 1 : 0) == 0)
775 fatal("%.200s line %d: Bad forwarding specification.",
779 if (opcode
== oLocalForward
||
780 opcode
== oDynamicForward
)
781 add_local_forward(options
, &fwd
);
782 else if (opcode
== oRemoteForward
)
783 add_remote_forward(options
, &fwd
);
787 case oClearAllForwardings
:
788 intptr
= &options
->clear_forwardings
;
793 while ((arg
= strdelim(&s
)) != NULL
&& *arg
!= '\0')
794 if (match_pattern(host
, arg
)) {
795 debug("Applying options for %.100s", arg
);
799 /* Avoid garbage check below, as strdelim is done. */
803 intptr
= &options
->escape_char
;
805 if (!arg
|| *arg
== '\0')
806 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
807 if (arg
[0] == '^' && arg
[2] == 0 &&
808 (u_char
) arg
[1] >= 64 && (u_char
) arg
[1] < 128)
809 value
= (u_char
) arg
[1] & 31;
810 else if (strlen(arg
) == 1)
811 value
= (u_char
) arg
[0];
812 else if (strcmp(arg
, "none") == 0)
813 value
= SSH_ESCAPECHAR_NONE
;
815 fatal("%.200s line %d: Bad escape character.",
818 value
= 0; /* Avoid compiler warning. */
820 if (*activep
&& *intptr
== -1)
826 if (!arg
|| *arg
== '\0')
827 fatal("%s line %d: missing address family.",
829 intptr
= &options
->address_family
;
830 if (strcasecmp(arg
, "inet") == 0)
832 else if (strcasecmp(arg
, "inet6") == 0)
834 else if (strcasecmp(arg
, "any") == 0)
837 fatal("Unsupported AddressFamily \"%s\"", arg
);
838 if (*activep
&& *intptr
== -1)
842 case oEnableSSHKeysign
:
843 intptr
= &options
->enable_ssh_keysign
;
846 case oIdentitiesOnly
:
847 intptr
= &options
->identities_only
;
850 case oServerAliveInterval
:
851 intptr
= &options
->server_alive_interval
;
854 case oServerAliveCountMax
:
855 intptr
= &options
->server_alive_count_max
;
859 while ((arg
= strdelim(&s
)) != NULL
&& *arg
!= '\0') {
860 if (strchr(arg
, '=') != NULL
)
861 fatal("%s line %d: Invalid environment name.",
865 if (options
->num_send_env
>= MAX_SEND_ENV
)
866 fatal("%s line %d: too many send env.",
868 options
->send_env
[options
->num_send_env
++] =
874 charptr
= &options
->control_path
;
878 intptr
= &options
->control_master
;
880 if (!arg
|| *arg
== '\0')
881 fatal("%.200s line %d: Missing ControlMaster argument.",
883 value
= 0; /* To avoid compiler warning... */
884 if (strcmp(arg
, "yes") == 0 || strcmp(arg
, "true") == 0)
885 value
= SSHCTL_MASTER_YES
;
886 else if (strcmp(arg
, "no") == 0 || strcmp(arg
, "false") == 0)
887 value
= SSHCTL_MASTER_NO
;
888 else if (strcmp(arg
, "auto") == 0)
889 value
= SSHCTL_MASTER_AUTO
;
890 else if (strcmp(arg
, "ask") == 0)
891 value
= SSHCTL_MASTER_ASK
;
892 else if (strcmp(arg
, "autoask") == 0)
893 value
= SSHCTL_MASTER_AUTO_ASK
;
895 fatal("%.200s line %d: Bad ControlMaster argument.",
897 if (*activep
&& *intptr
== -1)
901 case oControlPersist
:
902 /* no/false/yes/true, or a time spec */
903 intptr
= &options
->control_persist
;
905 if (!arg
|| *arg
== '\0')
906 fatal("%.200s line %d: Missing ControlPersist"
907 " argument.", filename
, linenum
);
909 value2
= 0; /* timeout */
910 if (strcmp(arg
, "no") == 0 || strcmp(arg
, "false") == 0)
912 else if (strcmp(arg
, "yes") == 0 || strcmp(arg
, "true") == 0)
914 else if ((value2
= convtime(arg
)) >= 0)
917 fatal("%.200s line %d: Bad ControlPersist argument.",
919 if (*activep
&& *intptr
== -1) {
921 options
->control_persist_timeout
= value2
;
925 case oHashKnownHosts
:
926 intptr
= &options
->hash_known_hosts
;
930 intptr
= &options
->tun_open
;
932 if (!arg
|| *arg
== '\0')
933 fatal("%s line %d: Missing yes/point-to-point/"
934 "ethernet/no argument.", filename
, linenum
);
935 value
= 0; /* silence compiler */
936 if (strcasecmp(arg
, "ethernet") == 0)
937 value
= SSH_TUNMODE_ETHERNET
;
938 else if (strcasecmp(arg
, "point-to-point") == 0)
939 value
= SSH_TUNMODE_POINTOPOINT
;
940 else if (strcasecmp(arg
, "yes") == 0)
941 value
= SSH_TUNMODE_DEFAULT
;
942 else if (strcasecmp(arg
, "no") == 0)
943 value
= SSH_TUNMODE_NO
;
945 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
946 "no argument: %s", filename
, linenum
, arg
);
953 if (!arg
|| *arg
== '\0')
954 fatal("%.200s line %d: Missing argument.", filename
, linenum
);
955 value
= a2tun(arg
, &value2
);
956 if (value
== SSH_TUNID_ERR
)
957 fatal("%.200s line %d: Bad tun device.", filename
, linenum
);
959 options
->tun_local
= value
;
960 options
->tun_remote
= value2
;
965 charptr
= &options
->local_command
;
968 case oPermitLocalCommand
:
969 intptr
= &options
->permit_local_command
;
973 intptr
= &options
->visual_host_key
;
977 intptr
= &options
->use_roaming
;
981 debug("%s line %d: Deprecated option \"%s\"",
982 filename
, linenum
, keyword
);
986 error("%s line %d: Unsupported option \"%s\"",
987 filename
, linenum
, keyword
);
991 fatal("process_config_line: Unimplemented opcode %d", opcode
);
994 /* Check that there is no garbage at end of line. */
995 if ((arg
= strdelim(&s
)) != NULL
&& *arg
!= '\0') {
996 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
997 filename
, linenum
, arg
);
1004 * Reads the config file and modifies the options accordingly. Options
1005 * should already be initialized before this call. This never returns if
1006 * there is an error. If the file does not exist, this returns 0.
1010 read_config_file(const char *filename
, const char *host
, Options
*options
,
1015 int active
, linenum
;
1016 int bad_options
= 0;
1018 if ((f
= fopen(filename
, "r")) == NULL
)
1024 if (fstat(fileno(f
), &sb
) == -1)
1025 fatal("fstat %s: %s", filename
, strerror(errno
));
1026 if (((sb
.st_uid
!= 0 && sb
.st_uid
!= getuid()) ||
1027 (sb
.st_mode
& 022) != 0))
1028 fatal("Bad owner or permissions on %s", filename
);
1031 debug("Reading configuration data %.200s", filename
);
1034 * Mark that we are now processing the options. This flag is turned
1035 * on/off by Host specifications.
1039 while (fgets(line
, sizeof(line
), f
)) {
1040 /* Update line number counter. */
1042 if (process_config_line(options
, host
, line
, filename
, linenum
, &active
) != 0)
1046 if (bad_options
> 0)
1047 fatal("%s: terminating, %d bad configuration options",
1048 filename
, bad_options
);
1053 * Initializes options to special values that indicate that they have not yet
1054 * been set. Read_config_file will only set options with this value. Options
1055 * are processed in the following order: command line, user config file,
1056 * system config file. Last, fill_default_options is called.
1060 initialize_options(Options
* options
)
1062 memset(options
, 'X', sizeof(*options
));
1063 options
->forward_agent
= -1;
1064 options
->forward_x11
= -1;
1065 options
->forward_x11_trusted
= -1;
1066 options
->forward_x11_timeout
= -1;
1067 options
->exit_on_forward_failure
= -1;
1068 options
->xauth_location
= NULL
;
1069 options
->gateway_ports
= -1;
1070 options
->use_privileged_port
= -1;
1071 options
->rsa_authentication
= -1;
1072 options
->pubkey_authentication
= -1;
1073 options
->challenge_response_authentication
= -1;
1074 options
->gss_authentication
= -1;
1075 options
->gss_deleg_creds
= -1;
1076 options
->password_authentication
= -1;
1077 options
->kbd_interactive_authentication
= -1;
1078 options
->kbd_interactive_devices
= NULL
;
1079 options
->rhosts_rsa_authentication
= -1;
1080 options
->hostbased_authentication
= -1;
1081 options
->batch_mode
= -1;
1082 options
->check_host_ip
= -1;
1083 options
->strict_host_key_checking
= -1;
1084 options
->compression
= -1;
1085 options
->tcp_keep_alive
= -1;
1086 options
->compression_level
= -1;
1088 options
->address_family
= -1;
1089 options
->connection_attempts
= -1;
1090 options
->connection_timeout
= -1;
1091 options
->number_of_password_prompts
= -1;
1092 options
->cipher
= -1;
1093 options
->ciphers
= NULL
;
1094 options
->macs
= NULL
;
1095 options
->kex_algorithms
= NULL
;
1096 options
->hostkeyalgorithms
= NULL
;
1097 options
->protocol
= SSH_PROTO_UNKNOWN
;
1098 options
->num_identity_files
= 0;
1099 options
->hostname
= NULL
;
1100 options
->host_key_alias
= NULL
;
1101 options
->proxy_command
= NULL
;
1102 options
->user
= NULL
;
1103 options
->escape_char
= -1;
1104 options
->system_hostfile
= NULL
;
1105 options
->user_hostfile
= NULL
;
1106 options
->system_hostfile2
= NULL
;
1107 options
->user_hostfile2
= NULL
;
1108 options
->local_forwards
= NULL
;
1109 options
->num_local_forwards
= 0;
1110 options
->remote_forwards
= NULL
;
1111 options
->num_remote_forwards
= 0;
1112 options
->clear_forwardings
= -1;
1113 options
->log_level
= SYSLOG_LEVEL_NOT_SET
;
1114 options
->preferred_authentications
= NULL
;
1115 options
->bind_address
= NULL
;
1116 options
->pkcs11_provider
= NULL
;
1117 options
->enable_ssh_keysign
= - 1;
1118 options
->no_host_authentication_for_localhost
= - 1;
1119 options
->identities_only
= - 1;
1120 options
->rekey_limit
= - 1;
1121 options
->verify_host_key_dns
= -1;
1122 options
->server_alive_interval
= -1;
1123 options
->server_alive_count_max
= -1;
1124 options
->num_send_env
= 0;
1125 options
->control_path
= NULL
;
1126 options
->control_master
= -1;
1127 options
->control_persist
= -1;
1128 options
->control_persist_timeout
= 0;
1129 options
->hash_known_hosts
= -1;
1130 options
->tun_open
= -1;
1131 options
->tun_local
= -1;
1132 options
->tun_remote
= -1;
1133 options
->local_command
= NULL
;
1134 options
->permit_local_command
= -1;
1135 options
->use_roaming
= -1;
1136 options
->visual_host_key
= -1;
1137 options
->zero_knowledge_password_authentication
= -1;
1141 * Called after processing other sources of option data, this fills those
1142 * options for which no value has been specified with their default values.
1146 fill_default_options(Options
* options
)
1150 if (options
->forward_agent
== -1)
1151 options
->forward_agent
= 0;
1152 if (options
->forward_x11
== -1)
1153 options
->forward_x11
= 0;
1154 if (options
->forward_x11_trusted
== -1)
1155 options
->forward_x11_trusted
= 0;
1156 if (options
->forward_x11_timeout
== -1)
1157 options
->forward_x11_timeout
= 1200;
1158 if (options
->exit_on_forward_failure
== -1)
1159 options
->exit_on_forward_failure
= 0;
1160 if (options
->xauth_location
== NULL
)
1161 options
->xauth_location
= _PATH_XAUTH
;
1162 if (options
->gateway_ports
== -1)
1163 options
->gateway_ports
= 0;
1164 if (options
->use_privileged_port
== -1)
1165 options
->use_privileged_port
= 0;
1166 if (options
->rsa_authentication
== -1)
1167 options
->rsa_authentication
= 1;
1168 if (options
->pubkey_authentication
== -1)
1169 options
->pubkey_authentication
= 1;
1170 if (options
->challenge_response_authentication
== -1)
1171 options
->challenge_response_authentication
= 1;
1172 if (options
->gss_authentication
== -1)
1173 options
->gss_authentication
= 0;
1174 if (options
->gss_deleg_creds
== -1)
1175 options
->gss_deleg_creds
= 0;
1176 if (options
->password_authentication
== -1)
1177 options
->password_authentication
= 1;
1178 if (options
->kbd_interactive_authentication
== -1)
1179 options
->kbd_interactive_authentication
= 1;
1180 if (options
->rhosts_rsa_authentication
== -1)
1181 options
->rhosts_rsa_authentication
= 0;
1182 if (options
->hostbased_authentication
== -1)
1183 options
->hostbased_authentication
= 0;
1184 if (options
->batch_mode
== -1)
1185 options
->batch_mode
= 0;
1186 if (options
->check_host_ip
== -1)
1187 options
->check_host_ip
= 1;
1188 if (options
->strict_host_key_checking
== -1)
1189 options
->strict_host_key_checking
= 2; /* 2 is default */
1190 if (options
->compression
== -1)
1191 options
->compression
= 0;
1192 if (options
->tcp_keep_alive
== -1)
1193 options
->tcp_keep_alive
= 1;
1194 if (options
->compression_level
== -1)
1195 options
->compression_level
= 6;
1196 if (options
->port
== -1)
1197 options
->port
= 0; /* Filled in ssh_connect. */
1198 if (options
->address_family
== -1)
1199 options
->address_family
= AF_UNSPEC
;
1200 if (options
->connection_attempts
== -1)
1201 options
->connection_attempts
= 1;
1202 if (options
->number_of_password_prompts
== -1)
1203 options
->number_of_password_prompts
= 3;
1204 /* Selected in ssh_login(). */
1205 if (options
->cipher
== -1)
1206 options
->cipher
= SSH_CIPHER_NOT_SET
;
1207 /* options->ciphers, default set in myproposals.h */
1208 /* options->macs, default set in myproposals.h */
1209 /* options->kex_algorithms, default set in myproposals.h */
1210 /* options->hostkeyalgorithms, default set in myproposals.h */
1211 if (options
->protocol
== SSH_PROTO_UNKNOWN
)
1212 options
->protocol
= SSH_PROTO_2
;
1213 if (options
->num_identity_files
== 0) {
1214 if (options
->protocol
& SSH_PROTO_1
) {
1215 len
= 2 + strlen(_PATH_SSH_CLIENT_IDENTITY
) + 1;
1216 options
->identity_files
[options
->num_identity_files
] =
1218 snprintf(options
->identity_files
[options
->num_identity_files
++],
1219 len
, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY
);
1221 if (options
->protocol
& SSH_PROTO_2
) {
1222 len
= 2 + strlen(_PATH_SSH_CLIENT_ID_RSA
) + 1;
1223 options
->identity_files
[options
->num_identity_files
] =
1225 snprintf(options
->identity_files
[options
->num_identity_files
++],
1226 len
, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA
);
1228 len
= 2 + strlen(_PATH_SSH_CLIENT_ID_DSA
) + 1;
1229 options
->identity_files
[options
->num_identity_files
] =
1231 snprintf(options
->identity_files
[options
->num_identity_files
++],
1232 len
, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA
);
1233 #ifdef OPENSSL_HAS_ECC
1234 len
= 2 + strlen(_PATH_SSH_CLIENT_ID_ECDSA
) + 1;
1235 options
->identity_files
[options
->num_identity_files
] =
1237 snprintf(options
->identity_files
[options
->num_identity_files
++],
1238 len
, "~/%.100s", _PATH_SSH_CLIENT_ID_ECDSA
);
1242 if (options
->escape_char
== -1)
1243 options
->escape_char
= '~';
1244 if (options
->system_hostfile
== NULL
)
1245 options
->system_hostfile
= _PATH_SSH_SYSTEM_HOSTFILE
;
1246 if (options
->user_hostfile
== NULL
)
1247 options
->user_hostfile
= _PATH_SSH_USER_HOSTFILE
;
1248 if (options
->system_hostfile2
== NULL
)
1249 options
->system_hostfile2
= _PATH_SSH_SYSTEM_HOSTFILE2
;
1250 if (options
->user_hostfile2
== NULL
)
1251 options
->user_hostfile2
= _PATH_SSH_USER_HOSTFILE2
;
1252 if (options
->log_level
== SYSLOG_LEVEL_NOT_SET
)
1253 options
->log_level
= SYSLOG_LEVEL_INFO
;
1254 if (options
->clear_forwardings
== 1)
1255 clear_forwardings(options
);
1256 if (options
->no_host_authentication_for_localhost
== - 1)
1257 options
->no_host_authentication_for_localhost
= 0;
1258 if (options
->identities_only
== -1)
1259 options
->identities_only
= 0;
1260 if (options
->enable_ssh_keysign
== -1)
1261 options
->enable_ssh_keysign
= 0;
1262 if (options
->rekey_limit
== -1)
1263 options
->rekey_limit
= 0;
1264 if (options
->verify_host_key_dns
== -1)
1265 options
->verify_host_key_dns
= 0;
1266 if (options
->server_alive_interval
== -1)
1267 options
->server_alive_interval
= 0;
1268 if (options
->server_alive_count_max
== -1)
1269 options
->server_alive_count_max
= 3;
1270 if (options
->control_master
== -1)
1271 options
->control_master
= 0;
1272 if (options
->control_persist
== -1) {
1273 options
->control_persist
= 0;
1274 options
->control_persist_timeout
= 0;
1276 if (options
->hash_known_hosts
== -1)
1277 options
->hash_known_hosts
= 0;
1278 if (options
->tun_open
== -1)
1279 options
->tun_open
= SSH_TUNMODE_NO
;
1280 if (options
->tun_local
== -1)
1281 options
->tun_local
= SSH_TUNID_ANY
;
1282 if (options
->tun_remote
== -1)
1283 options
->tun_remote
= SSH_TUNID_ANY
;
1284 if (options
->permit_local_command
== -1)
1285 options
->permit_local_command
= 0;
1286 if (options
->use_roaming
== -1)
1287 options
->use_roaming
= 1;
1288 if (options
->visual_host_key
== -1)
1289 options
->visual_host_key
= 0;
1290 if (options
->zero_knowledge_password_authentication
== -1)
1291 options
->zero_knowledge_password_authentication
= 0;
1292 /* options->local_command should not be set by default */
1293 /* options->proxy_command should not be set by default */
1294 /* options->user will be set in the main program if appropriate */
1295 /* options->hostname will be set in the main program if appropriate */
1296 /* options->host_key_alias should not be set by default */
1297 /* options->preferred_authentications will be set in ssh */
1302 * parses a string containing a port forwarding specification of the form:
1304 * [listenhost:]listenport:connecthost:connectport
1306 * [listenhost:]listenport
1307 * returns number of arguments parsed or zero on error
1310 parse_forward(Forward
*fwd
, const char *fwdspec
, int dynamicfwd
, int remotefwd
)
1313 char *p
, *cp
, *fwdarg
[4];
1315 memset(fwd
, '\0', sizeof(*fwd
));
1317 cp
= p
= xstrdup(fwdspec
);
1319 /* skip leading spaces */
1320 while (isspace(*cp
))
1323 for (i
= 0; i
< 4; ++i
)
1324 if ((fwdarg
[i
] = hpdelim(&cp
)) == NULL
)
1327 /* Check for trailing garbage */
1329 i
= 0; /* failure */
1333 fwd
->listen_host
= NULL
;
1334 fwd
->listen_port
= a2port(fwdarg
[0]);
1335 fwd
->connect_host
= xstrdup("socks");
1339 fwd
->listen_host
= xstrdup(cleanhostname(fwdarg
[0]));
1340 fwd
->listen_port
= a2port(fwdarg
[1]);
1341 fwd
->connect_host
= xstrdup("socks");
1345 fwd
->listen_host
= NULL
;
1346 fwd
->listen_port
= a2port(fwdarg
[0]);
1347 fwd
->connect_host
= xstrdup(cleanhostname(fwdarg
[1]));
1348 fwd
->connect_port
= a2port(fwdarg
[2]);
1352 fwd
->listen_host
= xstrdup(cleanhostname(fwdarg
[0]));
1353 fwd
->listen_port
= a2port(fwdarg
[1]);
1354 fwd
->connect_host
= xstrdup(cleanhostname(fwdarg
[2]));
1355 fwd
->connect_port
= a2port(fwdarg
[3]);
1358 i
= 0; /* failure */
1364 if (!(i
== 1 || i
== 2))
1367 if (!(i
== 3 || i
== 4))
1369 if (fwd
->connect_port
<= 0)
1373 if (fwd
->listen_port
< 0 || (!remotefwd
&& fwd
->listen_port
== 0))
1376 if (fwd
->connect_host
!= NULL
&&
1377 strlen(fwd
->connect_host
) >= NI_MAXHOST
)
1379 if (fwd
->listen_host
!= NULL
&&
1380 strlen(fwd
->listen_host
) >= NI_MAXHOST
)
1387 if (fwd
->connect_host
!= NULL
) {
1388 xfree(fwd
->connect_host
);
1389 fwd
->connect_host
= NULL
;
1391 if (fwd
->listen_host
!= NULL
) {
1392 xfree(fwd
->listen_host
);
1393 fwd
->listen_host
= NULL
;