djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: readconf.c,v 1.252 2016/04/15 00:30:19 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Functions for reading the configuration files. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 13 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 14 | |
| 15 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 16 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 19 | #include <sys/socket.h> |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 20 | #include <sys/wait.h> |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 21 | #include <sys/un.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 22 | |
| 23 | #include <netinet/in.h> |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 24 | #include <netinet/in_systm.h> |
| 25 | #include <netinet/ip.h> |
Darren Tucker | 0eeafcd | 2014-01-31 14:18:51 +1100 | [diff] [blame] | 26 | #include <arpa/inet.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 28 | #include <ctype.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 29 | #include <errno.h> |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 30 | #include <fcntl.h> |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 31 | #include <limits.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 32 | #include <netdb.h> |
Darren Tucker | a335766 | 2014-01-18 00:03:57 +1100 | [diff] [blame] | 33 | #ifdef HAVE_PATHS_H |
| 34 | # include <paths.h> |
| 35 | #endif |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 36 | #include <pwd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 38 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 39 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 40 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 41 | #include <unistd.h> |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 42 | #ifdef USE_SYSTEM_GLOB |
| 43 | # include <glob.h> |
| 44 | #else |
| 45 | # include "openbsd-compat/glob.h" |
| 46 | #endif |
Darren Tucker | e194ba4 | 2013-05-16 20:47:31 +1000 | [diff] [blame] | 47 | #ifdef HAVE_UTIL_H |
Darren Tucker | b7ee852 | 2013-05-16 20:33:10 +1000 | [diff] [blame] | 48 | #include <util.h> |
Darren Tucker | e194ba4 | 2013-05-16 20:47:31 +1000 | [diff] [blame] | 49 | #endif |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 50 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) |
| 51 | # include <vis.h> |
| 52 | #endif |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 53 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 55 | #include "ssh.h" |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 56 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 57 | #include "cipher.h" |
| 58 | #include "pathnames.h" |
| 59 | #include "log.h" |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 60 | #include "sshkey.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 61 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 62 | #include "readconf.h" |
| 63 | #include "match.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 64 | #include "kex.h" |
| 65 | #include "mac.h" |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 66 | #include "uidswap.h" |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 67 | #include "myproposal.h" |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 68 | #include "digest.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
| 70 | /* Format of the configuration file: |
| 71 | |
| 72 | # Configuration data is parsed as follows: |
| 73 | # 1. command line options |
| 74 | # 2. user-specific file |
| 75 | # 3. system-wide file |
| 76 | # Any configuration value is only changed the first time it is set. |
| 77 | # Thus, host-specific definitions should be at the beginning of the |
| 78 | # configuration file, and defaults at the end. |
| 79 | |
| 80 | # Host-specific declarations. These may override anything above. A single |
| 81 | # host may match multiple declarations; these are processed in the order |
| 82 | # that they are given in. |
| 83 | |
| 84 | Host *.ngs.fi ngs.fi |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 85 | User foo |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | |
| 87 | Host fake.com |
| 88 | HostName another.host.name.real.org |
| 89 | User blaah |
| 90 | Port 34289 |
| 91 | ForwardX11 no |
| 92 | ForwardAgent no |
| 93 | |
| 94 | Host books.com |
| 95 | RemoteForward 9999 shadows.cs.hut.fi:9999 |
| 96 | Cipher 3des |
| 97 | |
| 98 | Host fascist.blob.com |
| 99 | Port 23123 |
| 100 | User tylonen |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | PasswordAuthentication no |
| 102 | |
| 103 | Host puukko.hut.fi |
| 104 | User t35124p |
| 105 | ProxyCommand ssh-proxy %h %p |
| 106 | |
| 107 | Host *.fr |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 108 | PublicKeyAuthentication no |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 109 | |
| 110 | Host *.su |
| 111 | Cipher none |
| 112 | PasswordAuthentication no |
| 113 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 114 | Host vpn.fake.com |
| 115 | Tunnel yes |
| 116 | TunnelDevice 3 |
| 117 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | # Defaults for various options |
| 119 | Host * |
| 120 | ForwardAgent no |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 121 | ForwardX11 no |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | PasswordAuthentication yes |
| 123 | RSAAuthentication yes |
| 124 | RhostsRSAAuthentication yes |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | StrictHostKeyChecking yes |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 126 | TcpKeepAlive no |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 127 | IdentityFile ~/.ssh/identity |
| 128 | Port 22 |
| 129 | EscapeChar ~ |
| 130 | |
| 131 | */ |
| 132 | |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 133 | static int read_config_file_depth(const char *filename, struct passwd *pw, |
| 134 | const char *host, const char *original_host, Options *options, |
| 135 | int flags, int *activep, int depth); |
| 136 | static int process_config_line_depth(Options *options, struct passwd *pw, |
| 137 | const char *host, const char *original_host, char *line, |
| 138 | const char *filename, int linenum, int *activep, int flags, int depth); |
| 139 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | /* Keyword tokens. */ |
| 141 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 142 | typedef enum { |
| 143 | oBadOption, |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 144 | oHost, oMatch, oInclude, |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 145 | oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout, |
| 146 | oGatewayPorts, oExitOnForwardFailure, |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 147 | oPasswordAuthentication, oRSAAuthentication, |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 148 | oChallengeResponseAuthentication, oXAuthLocation, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 149 | oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward, |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 150 | oCertificateFile, oAddKeysToAgent, |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 151 | oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts, |
| 153 | oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression, |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 154 | oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts, |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 155 | oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs, |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 156 | oPubkeyAuthentication, |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 157 | oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias, |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 158 | oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication, |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 159 | oHostKeyAlgorithms, oBindAddress, oPKCS11Provider, |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 160 | oClearAllForwardings, oNoHostAuthenticationForLocalhost, |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 161 | oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 162 | oAddressFamily, oGssAuthentication, oGssDelegateCreds, |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 163 | oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly, |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 164 | oSendEnv, oControlPath, oControlMaster, oControlPersist, |
| 165 | oHashKnownHosts, |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 166 | oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand, |
markus@openbsd.org | a306863 | 2016-01-14 16:17:39 +0000 | [diff] [blame] | 167 | oVisualHostKey, |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 168 | oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass, |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 169 | oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots, |
| 170 | oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs, |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 171 | oStreamLocalBindMask, oStreamLocalBindUnlink, oRevokedHostKeys, |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 172 | oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes, |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 173 | oPubkeyAcceptedKeyTypes, |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 174 | oIgnoredUnknownOption, oDeprecated, oUnsupported |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | } OpCodes; |
| 176 | |
| 177 | /* Textual representations of the tokens. */ |
| 178 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | static struct { |
| 180 | const char *name; |
| 181 | OpCodes opcode; |
| 182 | } keywords[] = { |
| 183 | { "forwardagent", oForwardAgent }, |
| 184 | { "forwardx11", oForwardX11 }, |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 185 | { "forwardx11trusted", oForwardX11Trusted }, |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 186 | { "forwardx11timeout", oForwardX11Timeout }, |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 187 | { "exitonforwardfailure", oExitOnForwardFailure }, |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 188 | { "xauthlocation", oXAuthLocation }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 189 | { "gatewayports", oGatewayPorts }, |
| 190 | { "useprivilegedport", oUsePrivilegedPort }, |
Darren Tucker | ec960f2 | 2003-08-13 20:37:05 +1000 | [diff] [blame] | 191 | { "rhostsauthentication", oDeprecated }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 192 | { "passwordauthentication", oPasswordAuthentication }, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 193 | { "kbdinteractiveauthentication", oKbdInteractiveAuthentication }, |
| 194 | { "kbdinteractivedevices", oKbdInteractiveDevices }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 195 | { "rsaauthentication", oRSAAuthentication }, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 196 | { "pubkeyauthentication", oPubkeyAuthentication }, |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 197 | { "dsaauthentication", oPubkeyAuthentication }, /* alias */ |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 198 | { "rhostsrsaauthentication", oRhostsRSAAuthentication }, |
Ben Lindstrom | d69dab3 | 2001-04-12 23:36:05 +0000 | [diff] [blame] | 199 | { "hostbasedauthentication", oHostbasedAuthentication }, |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 200 | { "challengeresponseauthentication", oChallengeResponseAuthentication }, |
| 201 | { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */ |
| 202 | { "tisauthentication", oChallengeResponseAuthentication }, /* alias */ |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 203 | { "kerberosauthentication", oUnsupported }, |
| 204 | { "kerberostgtpassing", oUnsupported }, |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 205 | { "afstokenpassing", oUnsupported }, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 206 | #if defined(GSSAPI) |
| 207 | { "gssapiauthentication", oGssAuthentication }, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 208 | { "gssapidelegatecredentials", oGssDelegateCreds }, |
| 209 | #else |
| 210 | { "gssapiauthentication", oUnsupported }, |
| 211 | { "gssapidelegatecredentials", oUnsupported }, |
| 212 | #endif |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 213 | { "fallbacktorsh", oDeprecated }, |
| 214 | { "usersh", oDeprecated }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 215 | { "identityfile", oIdentityFile }, |
Damien Miller | 5bc6aae | 2009-01-28 16:27:31 +1100 | [diff] [blame] | 216 | { "identityfile2", oIdentityFile }, /* obsolete */ |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 217 | { "identitiesonly", oIdentitiesOnly }, |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 218 | { "certificatefile", oCertificateFile }, |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 219 | { "addkeystoagent", oAddKeysToAgent }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 220 | { "hostname", oHostName }, |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 221 | { "hostkeyalias", oHostKeyAlias }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 222 | { "proxycommand", oProxyCommand }, |
| 223 | { "port", oPort }, |
| 224 | { "cipher", oCipher }, |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 225 | { "ciphers", oCiphers }, |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 226 | { "macs", oMacs }, |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 227 | { "protocol", oProtocol }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 228 | { "remoteforward", oRemoteForward }, |
| 229 | { "localforward", oLocalForward }, |
| 230 | { "user", oUser }, |
| 231 | { "host", oHost }, |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 232 | { "match", oMatch }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 233 | { "escapechar", oEscapeChar }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 234 | { "globalknownhostsfile", oGlobalKnownHostsFile }, |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 235 | { "globalknownhostsfile2", oDeprecated }, |
Damien Miller | 5bc6aae | 2009-01-28 16:27:31 +1100 | [diff] [blame] | 236 | { "userknownhostsfile", oUserKnownHostsFile }, |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 237 | { "userknownhostsfile2", oDeprecated }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 238 | { "connectionattempts", oConnectionAttempts }, |
| 239 | { "batchmode", oBatchMode }, |
| 240 | { "checkhostip", oCheckHostIP }, |
| 241 | { "stricthostkeychecking", oStrictHostKeyChecking }, |
| 242 | { "compression", oCompression }, |
| 243 | { "compressionlevel", oCompressionLevel }, |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 244 | { "tcpkeepalive", oTCPKeepAlive }, |
| 245 | { "keepalive", oTCPKeepAlive }, /* obsolete */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 246 | { "numberofpasswordprompts", oNumberOfPasswordPrompts }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 247 | { "loglevel", oLogLevel }, |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 248 | { "dynamicforward", oDynamicForward }, |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 249 | { "preferredauthentications", oPreferredAuthentications }, |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 250 | { "hostkeyalgorithms", oHostKeyAlgorithms }, |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 251 | { "bindaddress", oBindAddress }, |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 252 | #ifdef ENABLE_PKCS11 |
| 253 | { "smartcarddevice", oPKCS11Provider }, |
| 254 | { "pkcs11provider", oPKCS11Provider }, |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 255 | #else |
| 256 | { "smartcarddevice", oUnsupported }, |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 257 | { "pkcs11provider", oUnsupported }, |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 258 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 259 | { "clearallforwardings", oClearAllForwardings }, |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 260 | { "enablesshkeysign", oEnableSSHKeysign }, |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 261 | { "verifyhostkeydns", oVerifyHostKeyDNS }, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 262 | { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost }, |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 263 | { "rekeylimit", oRekeyLimit }, |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 264 | { "connecttimeout", oConnectTimeout }, |
Damien Miller | 20a8f97 | 2003-05-18 20:50:30 +1000 | [diff] [blame] | 265 | { "addressfamily", oAddressFamily }, |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 266 | { "serveraliveinterval", oServerAliveInterval }, |
| 267 | { "serveralivecountmax", oServerAliveCountMax }, |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 268 | { "sendenv", oSendEnv }, |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 269 | { "controlpath", oControlPath }, |
| 270 | { "controlmaster", oControlMaster }, |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 271 | { "controlpersist", oControlPersist }, |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 272 | { "hashknownhosts", oHashKnownHosts }, |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 273 | { "include", oInclude }, |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 274 | { "tunnel", oTunnel }, |
| 275 | { "tunneldevice", oTunnelDevice }, |
| 276 | { "localcommand", oLocalCommand }, |
| 277 | { "permitlocalcommand", oPermitLocalCommand }, |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 278 | { "visualhostkey", oVisualHostKey }, |
markus@openbsd.org | a306863 | 2016-01-14 16:17:39 +0000 | [diff] [blame] | 279 | { "useroaming", oDeprecated }, |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 280 | { "kexalgorithms", oKexAlgorithms }, |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 281 | { "ipqos", oIPQoS }, |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 282 | { "requesttty", oRequestTTY }, |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 283 | { "proxyusefdpass", oProxyUseFdpass }, |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 284 | { "canonicaldomains", oCanonicalDomains }, |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 285 | { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal }, |
| 286 | { "canonicalizehostname", oCanonicalizeHostname }, |
| 287 | { "canonicalizemaxdots", oCanonicalizeMaxDots }, |
| 288 | { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs }, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 289 | { "streamlocalbindmask", oStreamLocalBindMask }, |
| 290 | { "streamlocalbindunlink", oStreamLocalBindUnlink }, |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 291 | { "revokedhostkeys", oRevokedHostKeys }, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 292 | { "fingerprinthash", oFingerprintHash }, |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 293 | { "updatehostkeys", oUpdateHostkeys }, |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 294 | { "hostbasedkeytypes", oHostbasedKeyTypes }, |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 295 | { "pubkeyacceptedkeytypes", oPubkeyAcceptedKeyTypes }, |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 296 | { "ignoreunknown", oIgnoreUnknown }, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 297 | |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 298 | { NULL, oBadOption } |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 299 | }; |
| 300 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 301 | /* |
| 302 | * Adds a local TCP/IP port forward to options. Never returns if there is an |
| 303 | * error. |
| 304 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 305 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 306 | void |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 307 | add_local_forward(Options *options, const struct Forward *newfwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 308 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 309 | struct Forward *fwd; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | extern uid_t original_real_uid; |
Darren Tucker | 5f41f03 | 2016-04-08 21:14:13 +1000 | [diff] [blame] | 311 | int i; |
djm@openbsd.org | 0ccbd5e | 2016-04-06 06:42:17 +0000 | [diff] [blame] | 312 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 313 | if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0 && |
| 314 | newfwd->listen_path == NULL) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 315 | fatal("Privileged ports can only be forwarded by root."); |
djm@openbsd.org | 0ccbd5e | 2016-04-06 06:42:17 +0000 | [diff] [blame] | 316 | /* Don't add duplicates */ |
| 317 | for (i = 0; i < options->num_local_forwards; i++) { |
| 318 | if (forward_equals(newfwd, options->local_forwards + i)) |
| 319 | return; |
| 320 | } |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 321 | options->local_forwards = xreallocarray(options->local_forwards, |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 322 | options->num_local_forwards + 1, |
| 323 | sizeof(*options->local_forwards)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | fwd = &options->local_forwards[options->num_local_forwards++]; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 325 | |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 326 | fwd->listen_host = newfwd->listen_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 327 | fwd->listen_port = newfwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 328 | fwd->listen_path = newfwd->listen_path; |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 329 | fwd->connect_host = newfwd->connect_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 330 | fwd->connect_port = newfwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 331 | fwd->connect_path = newfwd->connect_path; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 332 | } |
| 333 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 334 | /* |
| 335 | * Adds a remote TCP/IP port forward to options. Never returns if there is |
| 336 | * an error. |
| 337 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 338 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 339 | void |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 340 | add_remote_forward(Options *options, const struct Forward *newfwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 341 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 342 | struct Forward *fwd; |
djm@openbsd.org | 0ccbd5e | 2016-04-06 06:42:17 +0000 | [diff] [blame] | 343 | int i; |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 344 | |
djm@openbsd.org | 0ccbd5e | 2016-04-06 06:42:17 +0000 | [diff] [blame] | 345 | /* Don't add duplicates */ |
| 346 | for (i = 0; i < options->num_remote_forwards; i++) { |
| 347 | if (forward_equals(newfwd, options->remote_forwards + i)) |
| 348 | return; |
| 349 | } |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 350 | options->remote_forwards = xreallocarray(options->remote_forwards, |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 351 | options->num_remote_forwards + 1, |
| 352 | sizeof(*options->remote_forwards)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 353 | fwd = &options->remote_forwards[options->num_remote_forwards++]; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 354 | |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 355 | fwd->listen_host = newfwd->listen_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 356 | fwd->listen_port = newfwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 357 | fwd->listen_path = newfwd->listen_path; |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 358 | fwd->connect_host = newfwd->connect_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 359 | fwd->connect_port = newfwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 360 | fwd->connect_path = newfwd->connect_path; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 361 | fwd->handle = newfwd->handle; |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 362 | fwd->allocated_port = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 363 | } |
| 364 | |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 365 | static void |
| 366 | clear_forwardings(Options *options) |
| 367 | { |
| 368 | int i; |
| 369 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 370 | for (i = 0; i < options->num_local_forwards; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 371 | free(options->local_forwards[i].listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 372 | free(options->local_forwards[i].listen_path); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 373 | free(options->local_forwards[i].connect_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 374 | free(options->local_forwards[i].connect_path); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 375 | } |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 376 | if (options->num_local_forwards > 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 377 | free(options->local_forwards); |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 378 | options->local_forwards = NULL; |
| 379 | } |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 380 | options->num_local_forwards = 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 381 | for (i = 0; i < options->num_remote_forwards; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 382 | free(options->remote_forwards[i].listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 383 | free(options->remote_forwards[i].listen_path); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 384 | free(options->remote_forwards[i].connect_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 385 | free(options->remote_forwards[i].connect_path); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 386 | } |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 387 | if (options->num_remote_forwards > 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 388 | free(options->remote_forwards); |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 389 | options->remote_forwards = NULL; |
| 390 | } |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 391 | options->num_remote_forwards = 0; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 392 | options->tun_open = SSH_TUNMODE_NO; |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 395 | void |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 396 | add_certificate_file(Options *options, const char *path, int userprovided) |
| 397 | { |
| 398 | int i; |
| 399 | |
| 400 | if (options->num_certificate_files >= SSH_MAX_CERTIFICATE_FILES) |
| 401 | fatal("Too many certificate files specified (max %d)", |
| 402 | SSH_MAX_CERTIFICATE_FILES); |
| 403 | |
| 404 | /* Avoid registering duplicates */ |
| 405 | for (i = 0; i < options->num_certificate_files; i++) { |
| 406 | if (options->certificate_file_userprovided[i] == userprovided && |
| 407 | strcmp(options->certificate_files[i], path) == 0) { |
| 408 | debug2("%s: ignoring duplicate key %s", __func__, path); |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | options->certificate_file_userprovided[options->num_certificate_files] = |
| 414 | userprovided; |
| 415 | options->certificate_files[options->num_certificate_files++] = |
| 416 | xstrdup(path); |
| 417 | } |
| 418 | |
| 419 | void |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 420 | add_identity_file(Options *options, const char *dir, const char *filename, |
| 421 | int userprovided) |
| 422 | { |
| 423 | char *path; |
Damien Miller | 1527190 | 2014-05-15 13:47:56 +1000 | [diff] [blame] | 424 | int i; |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 425 | |
| 426 | if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES) |
| 427 | fatal("Too many identity files specified (max %d)", |
| 428 | SSH_MAX_IDENTITY_FILES); |
| 429 | |
| 430 | if (dir == NULL) /* no dir, filename is absolute */ |
| 431 | path = xstrdup(filename); |
| 432 | else |
| 433 | (void)xasprintf(&path, "%.100s%.100s", dir, filename); |
| 434 | |
Damien Miller | 1527190 | 2014-05-15 13:47:56 +1000 | [diff] [blame] | 435 | /* Avoid registering duplicates */ |
| 436 | for (i = 0; i < options->num_identity_files; i++) { |
| 437 | if (options->identity_file_userprovided[i] == userprovided && |
| 438 | strcmp(options->identity_files[i], path) == 0) { |
| 439 | debug2("%s: ignoring duplicate key %s", __func__, path); |
| 440 | free(path); |
| 441 | return; |
| 442 | } |
| 443 | } |
| 444 | |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 445 | options->identity_file_userprovided[options->num_identity_files] = |
| 446 | userprovided; |
| 447 | options->identity_files[options->num_identity_files++] = path; |
| 448 | } |
| 449 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 450 | int |
| 451 | default_ssh_port(void) |
| 452 | { |
| 453 | static int port; |
| 454 | struct servent *sp; |
| 455 | |
| 456 | if (port == 0) { |
| 457 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); |
| 458 | port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; |
| 459 | } |
| 460 | return port; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Execute a command in a shell. |
| 465 | * Return its exit status or -1 on abnormal exit. |
| 466 | */ |
| 467 | static int |
| 468 | execute_in_shell(const char *cmd) |
| 469 | { |
dtucker@openbsd.org | 97e184e | 2015-10-25 23:14:03 +0000 | [diff] [blame] | 470 | char *shell; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 471 | pid_t pid; |
| 472 | int devnull, status; |
| 473 | extern uid_t original_real_uid; |
| 474 | |
| 475 | if ((shell = getenv("SHELL")) == NULL) |
| 476 | shell = _PATH_BSHELL; |
| 477 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 478 | /* Need this to redirect subprocess stdin/out */ |
| 479 | if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) |
| 480 | fatal("open(/dev/null): %s", strerror(errno)); |
| 481 | |
| 482 | debug("Executing command: '%.500s'", cmd); |
| 483 | |
| 484 | /* Fork and execute the command. */ |
| 485 | if ((pid = fork()) == 0) { |
djm@openbsd.org | a4aef3e | 2015-10-27 08:54:52 +0000 | [diff] [blame] | 486 | char *argv[4]; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 487 | |
| 488 | /* Child. Permanently give up superuser privileges. */ |
| 489 | permanently_drop_suid(original_real_uid); |
| 490 | |
| 491 | /* Redirect child stdin and stdout. Leave stderr */ |
| 492 | if (dup2(devnull, STDIN_FILENO) == -1) |
| 493 | fatal("dup2: %s", strerror(errno)); |
| 494 | if (dup2(devnull, STDOUT_FILENO) == -1) |
| 495 | fatal("dup2: %s", strerror(errno)); |
| 496 | if (devnull > STDERR_FILENO) |
| 497 | close(devnull); |
| 498 | closefrom(STDERR_FILENO + 1); |
| 499 | |
djm@openbsd.org | a4aef3e | 2015-10-27 08:54:52 +0000 | [diff] [blame] | 500 | argv[0] = shell; |
| 501 | argv[1] = "-c"; |
| 502 | argv[2] = xstrdup(cmd); |
| 503 | argv[3] = NULL; |
| 504 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 505 | execv(argv[0], argv); |
| 506 | error("Unable to execute '%.100s': %s", cmd, strerror(errno)); |
| 507 | /* Die with signal to make this error apparent to parent. */ |
| 508 | signal(SIGTERM, SIG_DFL); |
| 509 | kill(getpid(), SIGTERM); |
| 510 | _exit(1); |
| 511 | } |
| 512 | /* Parent. */ |
| 513 | if (pid < 0) |
| 514 | fatal("%s: fork: %.100s", __func__, strerror(errno)); |
| 515 | |
| 516 | close(devnull); |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 517 | |
| 518 | while (waitpid(pid, &status, 0) == -1) { |
| 519 | if (errno != EINTR && errno != EAGAIN) |
| 520 | fatal("%s: waitpid: %s", __func__, strerror(errno)); |
| 521 | } |
| 522 | if (!WIFEXITED(status)) { |
| 523 | error("command '%.100s' exited abnormally", cmd); |
| 524 | return -1; |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 525 | } |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 526 | debug3("command returned status %d", WEXITSTATUS(status)); |
| 527 | return WEXITSTATUS(status); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Parse and execute a Match directive. |
| 532 | */ |
| 533 | static int |
| 534 | match_cfg_line(Options *options, char **condition, struct passwd *pw, |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 535 | const char *host_arg, const char *original_host, int post_canon, |
| 536 | const char *filename, int linenum) |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 537 | { |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 538 | char *arg, *oattrib, *attrib, *cmd, *cp = *condition, *host, *criteria; |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 539 | const char *ruser; |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 540 | int r, port, this_result, result = 1, attributes = 0, negate; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 541 | char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV]; |
| 542 | |
| 543 | /* |
| 544 | * Configuration is likely to be incomplete at this point so we |
| 545 | * must be prepared to use default values. |
| 546 | */ |
| 547 | port = options->port <= 0 ? default_ssh_port() : options->port; |
| 548 | ruser = options->user == NULL ? pw->pw_name : options->user; |
djm@openbsd.org | 5a62284 | 2016-02-08 23:40:12 +0000 | [diff] [blame] | 549 | if (post_canon) { |
| 550 | host = xstrdup(options->hostname); |
| 551 | } else if (options->hostname != NULL) { |
Damien Miller | eff5cad | 2013-10-23 16:31:10 +1100 | [diff] [blame] | 552 | /* NB. Please keep in sync with ssh.c:main() */ |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 553 | host = percent_expand(options->hostname, |
| 554 | "h", host_arg, (char *)NULL); |
djm@openbsd.org | 5a62284 | 2016-02-08 23:40:12 +0000 | [diff] [blame] | 555 | } else { |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 556 | host = xstrdup(host_arg); |
djm@openbsd.org | 5a62284 | 2016-02-08 23:40:12 +0000 | [diff] [blame] | 557 | } |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 558 | |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 559 | debug2("checking match for '%s' host %s originally %s", |
| 560 | cp, host, original_host); |
| 561 | while ((oattrib = attrib = strdelim(&cp)) && *attrib != '\0') { |
| 562 | criteria = NULL; |
| 563 | this_result = 1; |
| 564 | if ((negate = attrib[0] == '!')) |
| 565 | attrib++; |
| 566 | /* criteria "all" and "canonical" have no argument */ |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 567 | if (strcasecmp(attrib, "all") == 0) { |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 568 | if (attributes > 1 || |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 569 | ((arg = strdelim(&cp)) != NULL && *arg != '\0')) { |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 570 | error("%.200s line %d: '%s' cannot be combined " |
| 571 | "with other Match attributes", |
| 572 | filename, linenum, oattrib); |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 573 | result = -1; |
| 574 | goto out; |
| 575 | } |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 576 | if (result) |
| 577 | result = negate ? 0 : 1; |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 578 | goto out; |
| 579 | } |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 580 | attributes++; |
| 581 | if (strcasecmp(attrib, "canonical") == 0) { |
| 582 | r = !!post_canon; /* force bitmask member to boolean */ |
| 583 | if (r == (negate ? 1 : 0)) |
| 584 | this_result = result = 0; |
| 585 | debug3("%.200s line %d: %smatched '%s'", |
| 586 | filename, linenum, |
| 587 | this_result ? "" : "not ", oattrib); |
| 588 | continue; |
| 589 | } |
| 590 | /* All other criteria require an argument */ |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 591 | if ((arg = strdelim(&cp)) == NULL || *arg == '\0') { |
| 592 | error("Missing Match criteria for %s", attrib); |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 593 | result = -1; |
| 594 | goto out; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 595 | } |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 596 | if (strcasecmp(attrib, "host") == 0) { |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 597 | criteria = xstrdup(host); |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 598 | r = match_hostname(host, arg) == 1; |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 599 | if (r == (negate ? 1 : 0)) |
| 600 | this_result = result = 0; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 601 | } else if (strcasecmp(attrib, "originalhost") == 0) { |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 602 | criteria = xstrdup(original_host); |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 603 | r = match_hostname(original_host, arg) == 1; |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 604 | if (r == (negate ? 1 : 0)) |
| 605 | this_result = result = 0; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 606 | } else if (strcasecmp(attrib, "user") == 0) { |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 607 | criteria = xstrdup(ruser); |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 608 | r = match_pattern_list(ruser, arg, 0) == 1; |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 609 | if (r == (negate ? 1 : 0)) |
| 610 | this_result = result = 0; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 611 | } else if (strcasecmp(attrib, "localuser") == 0) { |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 612 | criteria = xstrdup(pw->pw_name); |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 613 | r = match_pattern_list(pw->pw_name, arg, 0) == 1; |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 614 | if (r == (negate ? 1 : 0)) |
| 615 | this_result = result = 0; |
Damien Miller | 8a04be7 | 2013-10-23 16:29:40 +1100 | [diff] [blame] | 616 | } else if (strcasecmp(attrib, "exec") == 0) { |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 617 | if (gethostname(thishost, sizeof(thishost)) == -1) |
| 618 | fatal("gethostname: %s", strerror(errno)); |
| 619 | strlcpy(shorthost, thishost, sizeof(shorthost)); |
| 620 | shorthost[strcspn(thishost, ".")] = '\0'; |
| 621 | snprintf(portstr, sizeof(portstr), "%d", port); |
| 622 | |
| 623 | cmd = percent_expand(arg, |
| 624 | "L", shorthost, |
| 625 | "d", pw->pw_dir, |
| 626 | "h", host, |
| 627 | "l", thishost, |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 628 | "n", original_host, |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 629 | "p", portstr, |
| 630 | "r", ruser, |
| 631 | "u", pw->pw_name, |
| 632 | (char *)NULL); |
Damien Miller | 0628780 | 2014-02-24 15:56:45 +1100 | [diff] [blame] | 633 | if (result != 1) { |
| 634 | /* skip execution if prior predicate failed */ |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 635 | debug3("%.200s line %d: skipped exec " |
| 636 | "\"%.100s\"", filename, linenum, cmd); |
| 637 | free(cmd); |
| 638 | continue; |
Damien Miller | 0628780 | 2014-02-24 15:56:45 +1100 | [diff] [blame] | 639 | } |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 640 | r = execute_in_shell(cmd); |
| 641 | if (r == -1) { |
| 642 | fatal("%.200s line %d: match exec " |
| 643 | "'%.100s' error", filename, |
| 644 | linenum, cmd); |
| 645 | } |
| 646 | criteria = xstrdup(cmd); |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 647 | free(cmd); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 648 | /* Force exit status to boolean */ |
| 649 | r = r == 0; |
| 650 | if (r == (negate ? 1 : 0)) |
| 651 | this_result = result = 0; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 652 | } else { |
| 653 | error("Unsupported Match attribute %s", attrib); |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 654 | result = -1; |
| 655 | goto out; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 656 | } |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 657 | debug3("%.200s line %d: %smatched '%s \"%.100s\"' ", |
| 658 | filename, linenum, this_result ? "": "not ", |
| 659 | oattrib, criteria); |
| 660 | free(criteria); |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 661 | } |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 662 | if (attributes == 0) { |
| 663 | error("One or more attributes required for Match"); |
| 664 | result = -1; |
| 665 | goto out; |
| 666 | } |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 667 | out: |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 668 | if (result != -1) |
| 669 | debug2("match %sfound", result ? "" : "not "); |
| 670 | *condition = cp; |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 671 | free(host); |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 672 | return result; |
| 673 | } |
| 674 | |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 675 | /* Check and prepare a domain name: removes trailing '.' and lowercases */ |
| 676 | static void |
| 677 | valid_domain(char *name, const char *filename, int linenum) |
| 678 | { |
| 679 | size_t i, l = strlen(name); |
| 680 | u_char c, last = '\0'; |
| 681 | |
| 682 | if (l == 0) |
| 683 | fatal("%s line %d: empty hostname suffix", filename, linenum); |
| 684 | if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0])) |
| 685 | fatal("%s line %d: hostname suffix \"%.100s\" " |
| 686 | "starts with invalid character", filename, linenum, name); |
| 687 | for (i = 0; i < l; i++) { |
| 688 | c = tolower((u_char)name[i]); |
| 689 | name[i] = (char)c; |
| 690 | if (last == '.' && c == '.') |
| 691 | fatal("%s line %d: hostname suffix \"%.100s\" contains " |
| 692 | "consecutive separators", filename, linenum, name); |
| 693 | if (c != '.' && c != '-' && !isalnum(c) && |
| 694 | c != '_') /* technically invalid, but common */ |
| 695 | fatal("%s line %d: hostname suffix \"%.100s\" contains " |
| 696 | "invalid characters", filename, linenum, name); |
| 697 | last = c; |
| 698 | } |
| 699 | if (name[l - 1] == '.') |
| 700 | name[l - 1] = '\0'; |
| 701 | } |
| 702 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 703 | /* |
Ben Lindstrom | 3704c26 | 2001-04-02 18:20:03 +0000 | [diff] [blame] | 704 | * Returns the number of the token pointed to by cp or oBadOption. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 705 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 706 | static OpCodes |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 707 | parse_token(const char *cp, const char *filename, int linenum, |
| 708 | const char *ignored_unknown) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 709 | { |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 710 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 711 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 712 | for (i = 0; keywords[i].name; i++) |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 713 | if (strcmp(cp, keywords[i].name) == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 714 | return keywords[i].opcode; |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 715 | if (ignored_unknown != NULL && |
| 716 | match_pattern_list(cp, ignored_unknown, 1) == 1) |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 717 | return oIgnoredUnknownOption; |
Ben Lindstrom | b5cdc66 | 2001-04-16 02:13:26 +0000 | [diff] [blame] | 718 | error("%s: line %d: Bad configuration option: %s", |
| 719 | filename, linenum, cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 720 | return oBadOption; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 721 | } |
| 722 | |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 723 | /* Multistate option parsing */ |
| 724 | struct multistate { |
| 725 | char *key; |
| 726 | int value; |
| 727 | }; |
| 728 | static const struct multistate multistate_flag[] = { |
| 729 | { "true", 1 }, |
| 730 | { "false", 0 }, |
| 731 | { "yes", 1 }, |
| 732 | { "no", 0 }, |
| 733 | { NULL, -1 } |
| 734 | }; |
| 735 | static const struct multistate multistate_yesnoask[] = { |
| 736 | { "true", 1 }, |
| 737 | { "false", 0 }, |
| 738 | { "yes", 1 }, |
| 739 | { "no", 0 }, |
| 740 | { "ask", 2 }, |
| 741 | { NULL, -1 } |
| 742 | }; |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 743 | static const struct multistate multistate_yesnoaskconfirm[] = { |
| 744 | { "true", 1 }, |
| 745 | { "false", 0 }, |
| 746 | { "yes", 1 }, |
| 747 | { "no", 0 }, |
| 748 | { "ask", 2 }, |
| 749 | { "confirm", 3 }, |
| 750 | { NULL, -1 } |
| 751 | }; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 752 | static const struct multistate multistate_addressfamily[] = { |
| 753 | { "inet", AF_INET }, |
| 754 | { "inet6", AF_INET6 }, |
| 755 | { "any", AF_UNSPEC }, |
| 756 | { NULL, -1 } |
| 757 | }; |
| 758 | static const struct multistate multistate_controlmaster[] = { |
| 759 | { "true", SSHCTL_MASTER_YES }, |
| 760 | { "yes", SSHCTL_MASTER_YES }, |
| 761 | { "false", SSHCTL_MASTER_NO }, |
| 762 | { "no", SSHCTL_MASTER_NO }, |
| 763 | { "auto", SSHCTL_MASTER_AUTO }, |
| 764 | { "ask", SSHCTL_MASTER_ASK }, |
| 765 | { "autoask", SSHCTL_MASTER_AUTO_ASK }, |
| 766 | { NULL, -1 } |
| 767 | }; |
| 768 | static const struct multistate multistate_tunnel[] = { |
| 769 | { "ethernet", SSH_TUNMODE_ETHERNET }, |
| 770 | { "point-to-point", SSH_TUNMODE_POINTOPOINT }, |
| 771 | { "true", SSH_TUNMODE_DEFAULT }, |
| 772 | { "yes", SSH_TUNMODE_DEFAULT }, |
| 773 | { "false", SSH_TUNMODE_NO }, |
| 774 | { "no", SSH_TUNMODE_NO }, |
| 775 | { NULL, -1 } |
| 776 | }; |
| 777 | static const struct multistate multistate_requesttty[] = { |
| 778 | { "true", REQUEST_TTY_YES }, |
| 779 | { "yes", REQUEST_TTY_YES }, |
| 780 | { "false", REQUEST_TTY_NO }, |
| 781 | { "no", REQUEST_TTY_NO }, |
| 782 | { "force", REQUEST_TTY_FORCE }, |
| 783 | { "auto", REQUEST_TTY_AUTO }, |
| 784 | { NULL, -1 } |
| 785 | }; |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 786 | static const struct multistate multistate_canonicalizehostname[] = { |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 787 | { "true", SSH_CANONICALISE_YES }, |
| 788 | { "false", SSH_CANONICALISE_NO }, |
| 789 | { "yes", SSH_CANONICALISE_YES }, |
| 790 | { "no", SSH_CANONICALISE_NO }, |
| 791 | { "always", SSH_CANONICALISE_ALWAYS }, |
| 792 | { NULL, -1 } |
| 793 | }; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 794 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 795 | /* |
| 796 | * Processes a single option line as used in the configuration files. This |
| 797 | * only sets those values that have not already been set. |
| 798 | */ |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 799 | int |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 800 | process_config_line(Options *options, struct passwd *pw, const char *host, |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 801 | const char *original_host, char *line, const char *filename, |
| 802 | int linenum, int *activep, int flags) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 803 | { |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 804 | return process_config_line_depth(options, pw, host, original_host, |
| 805 | line, filename, linenum, activep, flags, 0); |
| 806 | } |
| 807 | |
| 808 | #define WHITESPACE " \t\r\n" |
| 809 | static int |
| 810 | process_config_line_depth(Options *options, struct passwd *pw, const char *host, |
| 811 | const char *original_host, char *line, const char *filename, |
| 812 | int linenum, int *activep, int flags, int depth) |
| 813 | { |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 814 | char *s, **charptr, *endofnumber, *keyword, *arg, *arg2; |
| 815 | char **cpptr, fwdarg[256]; |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 816 | u_int i, *uintptr, max_entries = 0; |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 817 | int r, oactive, negated, opcode, *intptr, value, value2, cmdline = 0; |
Darren Tucker | 1e44c5d | 2008-01-01 20:32:26 +1100 | [diff] [blame] | 818 | LogLevel *log_level_ptr; |
Darren Tucker | 9113d0c | 2013-05-16 20:48:14 +1000 | [diff] [blame] | 819 | long long val64; |
Damien Miller | 61f08ac | 2003-02-24 11:56:27 +1100 | [diff] [blame] | 820 | size_t len; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 821 | struct Forward fwd; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 822 | const struct multistate *multistate_ptr; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 823 | struct allowed_cname *cname; |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 824 | glob_t gl; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 825 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 826 | if (activep == NULL) { /* We are processing a command line directive */ |
| 827 | cmdline = 1; |
| 828 | activep = &cmdline; |
| 829 | } |
| 830 | |
Damien Miller | c652cac | 2003-05-14 13:40:54 +1000 | [diff] [blame] | 831 | /* Strip trailing whitespace */ |
djm@openbsd.org | 26e0bcf | 2015-03-30 00:00:29 +0000 | [diff] [blame] | 832 | if ((len = strlen(line)) == 0) |
| 833 | return 0; |
| 834 | for (len--; len > 0; len--) { |
Damien Miller | c652cac | 2003-05-14 13:40:54 +1000 | [diff] [blame] | 835 | if (strchr(WHITESPACE, line[len]) == NULL) |
| 836 | break; |
| 837 | line[len] = '\0'; |
| 838 | } |
| 839 | |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 840 | s = line; |
| 841 | /* Get the keyword. (Each line is supposed to begin with a keyword). */ |
Damien Miller | 928b236 | 2006-03-26 13:53:32 +1100 | [diff] [blame] | 842 | if ((keyword = strdelim(&s)) == NULL) |
| 843 | return 0; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 844 | /* Ignore leading whitespace. */ |
| 845 | if (*keyword == '\0') |
| 846 | keyword = strdelim(&s); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 847 | if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 848 | return 0; |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 849 | /* Match lowercase keyword */ |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 850 | lowercase(keyword); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 851 | |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 852 | opcode = parse_token(keyword, filename, linenum, |
| 853 | options->ignored_unknown); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 854 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 855 | switch (opcode) { |
| 856 | case oBadOption: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 857 | /* don't panic, but count bad options */ |
| 858 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 859 | /* NOTREACHED */ |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 860 | case oIgnoredUnknownOption: |
| 861 | debug("%s line %d: Ignored unknown option \"%s\"", |
| 862 | filename, linenum, keyword); |
| 863 | return 0; |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 864 | case oConnectTimeout: |
| 865 | intptr = &options->connection_timeout; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 866 | parse_time: |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 867 | arg = strdelim(&s); |
| 868 | if (!arg || *arg == '\0') |
| 869 | fatal("%s line %d: missing time value.", |
| 870 | filename, linenum); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 871 | if (strcmp(arg, "none") == 0) |
| 872 | value = -1; |
| 873 | else if ((value = convtime(arg)) == -1) |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 874 | fatal("%s line %d: invalid time value.", |
| 875 | filename, linenum); |
Darren Tucker | a52c5b6 | 2007-02-19 22:09:45 +1100 | [diff] [blame] | 876 | if (*activep && *intptr == -1) |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 877 | *intptr = value; |
| 878 | break; |
| 879 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 880 | case oForwardAgent: |
| 881 | intptr = &options->forward_agent; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 882 | parse_flag: |
| 883 | multistate_ptr = multistate_flag; |
| 884 | parse_multistate: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 885 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 886 | if (!arg || *arg == '\0') |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 887 | fatal("%s line %d: missing argument.", |
| 888 | filename, linenum); |
| 889 | value = -1; |
| 890 | for (i = 0; multistate_ptr[i].key != NULL; i++) { |
| 891 | if (strcasecmp(arg, multistate_ptr[i].key) == 0) { |
| 892 | value = multistate_ptr[i].value; |
| 893 | break; |
| 894 | } |
| 895 | } |
| 896 | if (value == -1) |
| 897 | fatal("%s line %d: unsupported option \"%s\".", |
| 898 | filename, linenum, arg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 899 | if (*activep && *intptr == -1) |
| 900 | *intptr = value; |
| 901 | break; |
| 902 | |
| 903 | case oForwardX11: |
| 904 | intptr = &options->forward_x11; |
| 905 | goto parse_flag; |
| 906 | |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 907 | case oForwardX11Trusted: |
| 908 | intptr = &options->forward_x11_trusted; |
| 909 | goto parse_flag; |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 910 | |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 911 | case oForwardX11Timeout: |
| 912 | intptr = &options->forward_x11_timeout; |
| 913 | goto parse_time; |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 914 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 915 | case oGatewayPorts: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 916 | intptr = &options->fwd_opts.gateway_ports; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 917 | goto parse_flag; |
| 918 | |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 919 | case oExitOnForwardFailure: |
| 920 | intptr = &options->exit_on_forward_failure; |
| 921 | goto parse_flag; |
| 922 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 923 | case oUsePrivilegedPort: |
| 924 | intptr = &options->use_privileged_port; |
| 925 | goto parse_flag; |
| 926 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 927 | case oPasswordAuthentication: |
| 928 | intptr = &options->password_authentication; |
| 929 | goto parse_flag; |
| 930 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 931 | case oKbdInteractiveAuthentication: |
| 932 | intptr = &options->kbd_interactive_authentication; |
| 933 | goto parse_flag; |
| 934 | |
| 935 | case oKbdInteractiveDevices: |
| 936 | charptr = &options->kbd_interactive_devices; |
| 937 | goto parse_string; |
| 938 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 939 | case oPubkeyAuthentication: |
| 940 | intptr = &options->pubkey_authentication; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 941 | goto parse_flag; |
| 942 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 943 | case oRSAAuthentication: |
| 944 | intptr = &options->rsa_authentication; |
| 945 | goto parse_flag; |
| 946 | |
| 947 | case oRhostsRSAAuthentication: |
| 948 | intptr = &options->rhosts_rsa_authentication; |
| 949 | goto parse_flag; |
| 950 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 951 | case oHostbasedAuthentication: |
| 952 | intptr = &options->hostbased_authentication; |
| 953 | goto parse_flag; |
| 954 | |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 955 | case oChallengeResponseAuthentication: |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 956 | intptr = &options->challenge_response_authentication; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 957 | goto parse_flag; |
Damien Miller | 2aa0ab4 | 2003-05-15 12:05:28 +1000 | [diff] [blame] | 958 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 959 | case oGssAuthentication: |
| 960 | intptr = &options->gss_authentication; |
| 961 | goto parse_flag; |
| 962 | |
| 963 | case oGssDelegateCreds: |
| 964 | intptr = &options->gss_deleg_creds; |
| 965 | goto parse_flag; |
| 966 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 967 | case oBatchMode: |
| 968 | intptr = &options->batch_mode; |
| 969 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 970 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 971 | case oCheckHostIP: |
| 972 | intptr = &options->check_host_ip; |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 973 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 974 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 975 | case oVerifyHostKeyDNS: |
| 976 | intptr = &options->verify_host_key_dns; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 977 | multistate_ptr = multistate_yesnoask; |
| 978 | goto parse_multistate; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 979 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 980 | case oStrictHostKeyChecking: |
| 981 | intptr = &options->strict_host_key_checking; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 982 | multistate_ptr = multistate_yesnoask; |
| 983 | goto parse_multistate; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 984 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 985 | case oCompression: |
| 986 | intptr = &options->compression; |
| 987 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 988 | |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 989 | case oTCPKeepAlive: |
| 990 | intptr = &options->tcp_keep_alive; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 991 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 992 | |
Ben Lindstrom | 3cecc9a | 2001-10-03 17:39:38 +0000 | [diff] [blame] | 993 | case oNoHostAuthenticationForLocalhost: |
| 994 | intptr = &options->no_host_authentication_for_localhost; |
| 995 | goto parse_flag; |
| 996 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 997 | case oNumberOfPasswordPrompts: |
| 998 | intptr = &options->number_of_password_prompts; |
| 999 | goto parse_int; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1000 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1001 | case oCompressionLevel: |
| 1002 | intptr = &options->compression_level; |
| 1003 | goto parse_int; |
| 1004 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1005 | case oRekeyLimit: |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1006 | arg = strdelim(&s); |
| 1007 | if (!arg || *arg == '\0') |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1008 | fatal("%.200s line %d: Missing argument.", filename, |
| 1009 | linenum); |
| 1010 | if (strcmp(arg, "default") == 0) { |
| 1011 | val64 = 0; |
| 1012 | } else { |
Darren Tucker | b7ee852 | 2013-05-16 20:33:10 +1000 | [diff] [blame] | 1013 | if (scan_scaled(arg, &val64) == -1) |
| 1014 | fatal("%.200s line %d: Bad number '%s': %s", |
| 1015 | filename, linenum, arg, strerror(errno)); |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1016 | if (val64 != 0 && val64 < 16) |
| 1017 | fatal("%.200s line %d: RekeyLimit too small", |
| 1018 | filename, linenum); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1019 | } |
Damien Miller | 3dff176 | 2008-02-10 22:25:52 +1100 | [diff] [blame] | 1020 | if (*activep && options->rekey_limit == -1) |
dtucker@openbsd.org | 921ff00 | 2016-01-29 02:54:45 +0000 | [diff] [blame] | 1021 | options->rekey_limit = val64; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1022 | if (s != NULL) { /* optional rekey interval present */ |
| 1023 | if (strcmp(s, "none") == 0) { |
| 1024 | (void)strdelim(&s); /* discard */ |
| 1025 | break; |
| 1026 | } |
| 1027 | intptr = &options->rekey_interval; |
| 1028 | goto parse_time; |
| 1029 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1030 | break; |
| 1031 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1032 | case oIdentityFile: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1033 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1034 | if (!arg || *arg == '\0') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1035 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
| 1036 | if (*activep) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1037 | intptr = &options->num_identity_files; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1038 | if (*intptr >= SSH_MAX_IDENTITY_FILES) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1039 | fatal("%.200s line %d: Too many identity files specified (max %d).", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1040 | filename, linenum, SSH_MAX_IDENTITY_FILES); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 1041 | add_identity_file(options, NULL, |
| 1042 | arg, flags & SSHCONF_USERCONF); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1043 | } |
| 1044 | break; |
| 1045 | |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1046 | case oCertificateFile: |
| 1047 | arg = strdelim(&s); |
| 1048 | if (!arg || *arg == '\0') |
| 1049 | fatal("%.200s line %d: Missing argument.", |
| 1050 | filename, linenum); |
| 1051 | if (*activep) { |
| 1052 | intptr = &options->num_certificate_files; |
| 1053 | if (*intptr >= SSH_MAX_CERTIFICATE_FILES) { |
| 1054 | fatal("%.200s line %d: Too many certificate " |
| 1055 | "files specified (max %d).", |
| 1056 | filename, linenum, |
| 1057 | SSH_MAX_CERTIFICATE_FILES); |
| 1058 | } |
| 1059 | add_certificate_file(options, arg, |
| 1060 | flags & SSHCONF_USERCONF); |
| 1061 | } |
| 1062 | break; |
| 1063 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1064 | case oXAuthLocation: |
| 1065 | charptr=&options->xauth_location; |
| 1066 | goto parse_string; |
| 1067 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1068 | case oUser: |
| 1069 | charptr = &options->user; |
| 1070 | parse_string: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1071 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1072 | if (!arg || *arg == '\0') |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1073 | fatal("%.200s line %d: Missing argument.", |
| 1074 | filename, linenum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1075 | if (*activep && *charptr == NULL) |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1076 | *charptr = xstrdup(arg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1077 | break; |
| 1078 | |
| 1079 | case oGlobalKnownHostsFile: |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1080 | cpptr = (char **)&options->system_hostfiles; |
| 1081 | uintptr = &options->num_system_hostfiles; |
| 1082 | max_entries = SSH_MAX_HOSTS_FILES; |
| 1083 | parse_char_array: |
| 1084 | if (*activep && *uintptr == 0) { |
| 1085 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1086 | if ((*uintptr) >= max_entries) |
| 1087 | fatal("%s line %d: " |
| 1088 | "too many authorized keys files.", |
| 1089 | filename, linenum); |
| 1090 | cpptr[(*uintptr)++] = xstrdup(arg); |
| 1091 | } |
| 1092 | } |
| 1093 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1094 | |
| 1095 | case oUserKnownHostsFile: |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1096 | cpptr = (char **)&options->user_hostfiles; |
| 1097 | uintptr = &options->num_user_hostfiles; |
| 1098 | max_entries = SSH_MAX_HOSTS_FILES; |
| 1099 | goto parse_char_array; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1100 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1101 | case oHostName: |
| 1102 | charptr = &options->hostname; |
| 1103 | goto parse_string; |
| 1104 | |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 1105 | case oHostKeyAlias: |
| 1106 | charptr = &options->host_key_alias; |
| 1107 | goto parse_string; |
| 1108 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1109 | case oPreferredAuthentications: |
| 1110 | charptr = &options->preferred_authentications; |
| 1111 | goto parse_string; |
| 1112 | |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 1113 | case oBindAddress: |
| 1114 | charptr = &options->bind_address; |
| 1115 | goto parse_string; |
| 1116 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1117 | case oPKCS11Provider: |
| 1118 | charptr = &options->pkcs11_provider; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 1119 | goto parse_string; |
Ben Lindstrom | ae996bf | 2001-08-06 21:27:53 +0000 | [diff] [blame] | 1120 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1121 | case oProxyCommand: |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1122 | charptr = &options->proxy_command; |
| 1123 | parse_command: |
Darren Tucker | a99c1b7 | 2003-06-28 12:40:12 +1000 | [diff] [blame] | 1124 | if (s == NULL) |
| 1125 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 61f08ac | 2003-02-24 11:56:27 +1100 | [diff] [blame] | 1126 | len = strspn(s, WHITESPACE "="); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1127 | if (*activep && *charptr == NULL) |
Damien Miller | 61f08ac | 2003-02-24 11:56:27 +1100 | [diff] [blame] | 1128 | *charptr = xstrdup(s + len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1129 | return 0; |
| 1130 | |
| 1131 | case oPort: |
| 1132 | intptr = &options->port; |
| 1133 | parse_int: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1134 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1135 | if (!arg || *arg == '\0') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1136 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1137 | if (arg[0] < '0' || arg[0] > '9') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1138 | fatal("%.200s line %d: Bad number.", filename, linenum); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1139 | |
| 1140 | /* Octal, decimal, or hex format? */ |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1141 | value = strtol(arg, &endofnumber, 0); |
| 1142 | if (arg == endofnumber) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1143 | fatal("%.200s line %d: Bad number.", filename, linenum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1144 | if (*activep && *intptr == -1) |
| 1145 | *intptr = value; |
| 1146 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1147 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1148 | case oConnectionAttempts: |
| 1149 | intptr = &options->connection_attempts; |
| 1150 | goto parse_int; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 1151 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1152 | case oCipher: |
| 1153 | intptr = &options->cipher; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1154 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1155 | if (!arg || *arg == '\0') |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1156 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1157 | value = cipher_number(arg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1158 | if (value == -1) |
| 1159 | fatal("%.200s line %d: Bad cipher '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1160 | filename, linenum, arg ? arg : "<NONE>"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1161 | if (*activep && *intptr == -1) |
| 1162 | *intptr = value; |
| 1163 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1164 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1165 | case oCiphers: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1166 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1167 | if (!arg || *arg == '\0') |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1168 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 1169 | if (!ciphers_valid(*arg == '+' ? arg + 1 : arg)) |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1170 | fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1171 | filename, linenum, arg ? arg : "<NONE>"); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1172 | if (*activep && options->ciphers == NULL) |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1173 | options->ciphers = xstrdup(arg); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1174 | break; |
| 1175 | |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1176 | case oMacs: |
| 1177 | arg = strdelim(&s); |
| 1178 | if (!arg || *arg == '\0') |
| 1179 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 1180 | if (!mac_valid(*arg == '+' ? arg + 1 : arg)) |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1181 | fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1182 | filename, linenum, arg ? arg : "<NONE>"); |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1183 | if (*activep && options->macs == NULL) |
| 1184 | options->macs = xstrdup(arg); |
| 1185 | break; |
| 1186 | |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 1187 | case oKexAlgorithms: |
| 1188 | arg = strdelim(&s); |
| 1189 | if (!arg || *arg == '\0') |
| 1190 | fatal("%.200s line %d: Missing argument.", |
| 1191 | filename, linenum); |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 1192 | if (!kex_names_valid(*arg == '+' ? arg + 1 : arg)) |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 1193 | fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.", |
| 1194 | filename, linenum, arg ? arg : "<NONE>"); |
| 1195 | if (*activep && options->kex_algorithms == NULL) |
| 1196 | options->kex_algorithms = xstrdup(arg); |
| 1197 | break; |
| 1198 | |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1199 | case oHostKeyAlgorithms: |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1200 | charptr = &options->hostkeyalgorithms; |
| 1201 | parse_keytypes: |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1202 | arg = strdelim(&s); |
| 1203 | if (!arg || *arg == '\0') |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1204 | fatal("%.200s line %d: Missing argument.", |
| 1205 | filename, linenum); |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 1206 | if (!sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1)) |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1207 | fatal("%s line %d: Bad key types '%s'.", |
| 1208 | filename, linenum, arg ? arg : "<NONE>"); |
| 1209 | if (*activep && *charptr == NULL) |
| 1210 | *charptr = xstrdup(arg); |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1211 | break; |
| 1212 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1213 | case oProtocol: |
| 1214 | intptr = &options->protocol; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1215 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1216 | if (!arg || *arg == '\0') |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1217 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1218 | value = proto_spec(arg); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1219 | if (value == SSH_PROTO_UNKNOWN) |
| 1220 | fatal("%.200s line %d: Bad protocol spec '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1221 | filename, linenum, arg ? arg : "<NONE>"); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1222 | if (*activep && *intptr == SSH_PROTO_UNKNOWN) |
| 1223 | *intptr = value; |
| 1224 | break; |
| 1225 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1226 | case oLogLevel: |
Darren Tucker | 1e44c5d | 2008-01-01 20:32:26 +1100 | [diff] [blame] | 1227 | log_level_ptr = &options->log_level; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1228 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1229 | value = log_level_number(arg); |
Damien Miller | fcd9320 | 2002-02-05 12:26:34 +1100 | [diff] [blame] | 1230 | if (value == SYSLOG_LEVEL_NOT_SET) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 1231 | fatal("%.200s line %d: unsupported log level '%s'", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1232 | filename, linenum, arg ? arg : "<NONE>"); |
Darren Tucker | 1e44c5d | 2008-01-01 20:32:26 +1100 | [diff] [blame] | 1233 | if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET) |
| 1234 | *log_level_ptr = (LogLevel) value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1235 | break; |
| 1236 | |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1237 | case oLocalForward: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1238 | case oRemoteForward: |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1239 | case oDynamicForward: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1240 | arg = strdelim(&s); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1241 | if (arg == NULL || *arg == '\0') |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1242 | fatal("%.200s line %d: Missing port argument.", |
| 1243 | filename, linenum); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1244 | |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1245 | if (opcode == oLocalForward || |
| 1246 | opcode == oRemoteForward) { |
| 1247 | arg2 = strdelim(&s); |
| 1248 | if (arg2 == NULL || *arg2 == '\0') |
| 1249 | fatal("%.200s line %d: Missing target argument.", |
| 1250 | filename, linenum); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1251 | |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1252 | /* construct a string for parse_forward */ |
| 1253 | snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2); |
| 1254 | } else if (opcode == oDynamicForward) { |
| 1255 | strlcpy(fwdarg, arg, sizeof(fwdarg)); |
| 1256 | } |
| 1257 | |
| 1258 | if (parse_forward(&fwd, fwdarg, |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 1259 | opcode == oDynamicForward ? 1 : 0, |
| 1260 | opcode == oRemoteForward ? 1 : 0) == 0) |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1261 | fatal("%.200s line %d: Bad forwarding specification.", |
| 1262 | filename, linenum); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1263 | |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1264 | if (*activep) { |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1265 | if (opcode == oLocalForward || |
| 1266 | opcode == oDynamicForward) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1267 | add_local_forward(options, &fwd); |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1268 | else if (opcode == oRemoteForward) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1269 | add_remote_forward(options, &fwd); |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1270 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1271 | break; |
| 1272 | |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 1273 | case oClearAllForwardings: |
| 1274 | intptr = &options->clear_forwardings; |
| 1275 | goto parse_flag; |
| 1276 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1277 | case oHost: |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1278 | if (cmdline) |
| 1279 | fatal("Host directive not supported as a command-line " |
| 1280 | "option"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1281 | *activep = 0; |
Damien Miller | fe92421 | 2011-05-15 08:44:45 +1000 | [diff] [blame] | 1282 | arg2 = NULL; |
| 1283 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1284 | if ((flags & SSHCONF_NEVERMATCH) != 0) |
| 1285 | break; |
Damien Miller | fe92421 | 2011-05-15 08:44:45 +1000 | [diff] [blame] | 1286 | negated = *arg == '!'; |
| 1287 | if (negated) |
| 1288 | arg++; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1289 | if (match_pattern(host, arg)) { |
Damien Miller | fe92421 | 2011-05-15 08:44:45 +1000 | [diff] [blame] | 1290 | if (negated) { |
| 1291 | debug("%.200s line %d: Skipping Host " |
| 1292 | "block because of negated match " |
| 1293 | "for %.100s", filename, linenum, |
| 1294 | arg); |
| 1295 | *activep = 0; |
| 1296 | break; |
| 1297 | } |
| 1298 | if (!*activep) |
| 1299 | arg2 = arg; /* logged below */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1300 | *activep = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1301 | } |
Damien Miller | fe92421 | 2011-05-15 08:44:45 +1000 | [diff] [blame] | 1302 | } |
| 1303 | if (*activep) |
| 1304 | debug("%.200s line %d: Applying options for %.100s", |
| 1305 | filename, linenum, arg2); |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1306 | /* Avoid garbage check below, as strdelim is done. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1307 | return 0; |
| 1308 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1309 | case oMatch: |
| 1310 | if (cmdline) |
| 1311 | fatal("Host directive not supported as a command-line " |
| 1312 | "option"); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 1313 | value = match_cfg_line(options, &s, pw, host, original_host, |
| 1314 | flags & SSHCONF_POSTCANON, filename, linenum); |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1315 | if (value < 0) |
| 1316 | fatal("%.200s line %d: Bad Match condition", filename, |
| 1317 | linenum); |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1318 | *activep = (flags & SSHCONF_NEVERMATCH) ? 0 : value; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1319 | break; |
| 1320 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1321 | case oEscapeChar: |
| 1322 | intptr = &options->escape_char; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1323 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1324 | if (!arg || *arg == '\0') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1325 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
djm@openbsd.org | 0882332 | 2015-05-22 04:45:52 +0000 | [diff] [blame] | 1326 | if (strcmp(arg, "none") == 0) |
| 1327 | value = SSH_ESCAPECHAR_NONE; |
| 1328 | else if (arg[1] == '\0') |
| 1329 | value = (u_char) arg[0]; |
| 1330 | else if (arg[0] == '^' && arg[2] == 0 && |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1331 | (u_char) arg[1] >= 64 && (u_char) arg[1] < 128) |
| 1332 | value = (u_char) arg[1] & 31; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1333 | else { |
| 1334 | fatal("%.200s line %d: Bad escape character.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1335 | filename, linenum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1336 | /* NOTREACHED */ |
| 1337 | value = 0; /* Avoid compiler warning. */ |
| 1338 | } |
| 1339 | if (*activep && *intptr == -1) |
| 1340 | *intptr = value; |
| 1341 | break; |
| 1342 | |
Damien Miller | 20a8f97 | 2003-05-18 20:50:30 +1000 | [diff] [blame] | 1343 | case oAddressFamily: |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 1344 | intptr = &options->address_family; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1345 | multistate_ptr = multistate_addressfamily; |
| 1346 | goto parse_multistate; |
Damien Miller | 20a8f97 | 2003-05-18 20:50:30 +1000 | [diff] [blame] | 1347 | |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 1348 | case oEnableSSHKeysign: |
| 1349 | intptr = &options->enable_ssh_keysign; |
| 1350 | goto parse_flag; |
| 1351 | |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1352 | case oIdentitiesOnly: |
| 1353 | intptr = &options->identities_only; |
| 1354 | goto parse_flag; |
| 1355 | |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1356 | case oServerAliveInterval: |
| 1357 | intptr = &options->server_alive_interval; |
| 1358 | goto parse_time; |
| 1359 | |
| 1360 | case oServerAliveCountMax: |
| 1361 | intptr = &options->server_alive_count_max; |
| 1362 | goto parse_int; |
| 1363 | |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1364 | case oSendEnv: |
| 1365 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1366 | if (strchr(arg, '=') != NULL) |
| 1367 | fatal("%s line %d: Invalid environment name.", |
| 1368 | filename, linenum); |
Damien Miller | f8e7acc | 2005-03-05 11:22:50 +1100 | [diff] [blame] | 1369 | if (!*activep) |
| 1370 | continue; |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1371 | if (options->num_send_env >= MAX_SEND_ENV) |
| 1372 | fatal("%s line %d: too many send env.", |
| 1373 | filename, linenum); |
| 1374 | options->send_env[options->num_send_env++] = |
| 1375 | xstrdup(arg); |
| 1376 | } |
| 1377 | break; |
| 1378 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1379 | case oControlPath: |
| 1380 | charptr = &options->control_path; |
| 1381 | goto parse_string; |
| 1382 | |
| 1383 | case oControlMaster: |
| 1384 | intptr = &options->control_master; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1385 | multistate_ptr = multistate_controlmaster; |
| 1386 | goto parse_multistate; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1387 | |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1388 | case oControlPersist: |
| 1389 | /* no/false/yes/true, or a time spec */ |
| 1390 | intptr = &options->control_persist; |
| 1391 | arg = strdelim(&s); |
| 1392 | if (!arg || *arg == '\0') |
| 1393 | fatal("%.200s line %d: Missing ControlPersist" |
| 1394 | " argument.", filename, linenum); |
| 1395 | value = 0; |
| 1396 | value2 = 0; /* timeout */ |
| 1397 | if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0) |
| 1398 | value = 0; |
| 1399 | else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0) |
| 1400 | value = 1; |
| 1401 | else if ((value2 = convtime(arg)) >= 0) |
| 1402 | value = 1; |
| 1403 | else |
| 1404 | fatal("%.200s line %d: Bad ControlPersist argument.", |
| 1405 | filename, linenum); |
| 1406 | if (*activep && *intptr == -1) { |
| 1407 | *intptr = value; |
| 1408 | options->control_persist_timeout = value2; |
| 1409 | } |
| 1410 | break; |
| 1411 | |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 1412 | case oHashKnownHosts: |
| 1413 | intptr = &options->hash_known_hosts; |
| 1414 | goto parse_flag; |
| 1415 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1416 | case oTunnel: |
| 1417 | intptr = &options->tun_open; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1418 | multistate_ptr = multistate_tunnel; |
| 1419 | goto parse_multistate; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1420 | |
| 1421 | case oTunnelDevice: |
| 1422 | arg = strdelim(&s); |
| 1423 | if (!arg || *arg == '\0') |
| 1424 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
| 1425 | value = a2tun(arg, &value2); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1426 | if (value == SSH_TUNID_ERR) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1427 | fatal("%.200s line %d: Bad tun device.", filename, linenum); |
| 1428 | if (*activep) { |
| 1429 | options->tun_local = value; |
| 1430 | options->tun_remote = value2; |
| 1431 | } |
| 1432 | break; |
| 1433 | |
| 1434 | case oLocalCommand: |
| 1435 | charptr = &options->local_command; |
| 1436 | goto parse_command; |
| 1437 | |
| 1438 | case oPermitLocalCommand: |
| 1439 | intptr = &options->permit_local_command; |
| 1440 | goto parse_flag; |
| 1441 | |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1442 | case oVisualHostKey: |
| 1443 | intptr = &options->visual_host_key; |
| 1444 | goto parse_flag; |
| 1445 | |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1446 | case oInclude: |
| 1447 | if (cmdline) |
| 1448 | fatal("Include directive not supported as a " |
| 1449 | "command-line option"); |
| 1450 | value = 0; |
| 1451 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1452 | /* |
| 1453 | * Ensure all paths are anchored. User configuration |
| 1454 | * files may begin with '~/' but system configurations |
| 1455 | * must not. If the path is relative, then treat it |
| 1456 | * as living in ~/.ssh for user configurations or |
| 1457 | * /etc/ssh for system ones. |
| 1458 | */ |
| 1459 | if (*arg == '~' && (flags & SSHCONF_USERCONF) == 0) |
| 1460 | fatal("%.200s line %d: bad include path %s.", |
| 1461 | filename, linenum, arg); |
| 1462 | if (*arg != '/' && *arg != '~') { |
| 1463 | xasprintf(&arg2, "%s/%s", |
| 1464 | (flags & SSHCONF_USERCONF) ? |
| 1465 | "~/" _PATH_SSH_USER_DIR : SSHDIR, arg); |
| 1466 | } else |
| 1467 | arg2 = xstrdup(arg); |
| 1468 | memset(&gl, 0, sizeof(gl)); |
| 1469 | r = glob(arg2, GLOB_TILDE, NULL, &gl); |
| 1470 | if (r == GLOB_NOMATCH) { |
| 1471 | debug("%.200s line %d: include %s matched no " |
| 1472 | "files",filename, linenum, arg2); |
| 1473 | continue; |
| 1474 | } else if (r != 0 || gl.gl_pathc < 0) |
| 1475 | fatal("%.200s line %d: glob failed for %s.", |
| 1476 | filename, linenum, arg2); |
| 1477 | free(arg2); |
| 1478 | oactive = *activep; |
| 1479 | for (i = 0; i < (u_int)gl.gl_pathc; i++) { |
| 1480 | debug3("%.200s line %d: Including file %s " |
| 1481 | "depth %d%s", filename, linenum, |
| 1482 | gl.gl_pathv[i], depth, |
| 1483 | oactive ? "" : " (parse only)"); |
| 1484 | r = read_config_file_depth(gl.gl_pathv[i], |
| 1485 | pw, host, original_host, options, |
| 1486 | flags | SSHCONF_CHECKPERM | |
| 1487 | (oactive ? 0 : SSHCONF_NEVERMATCH), |
| 1488 | activep, depth + 1); |
| 1489 | /* |
| 1490 | * don't let Match in includes clobber the |
| 1491 | * containing file's Match state. |
| 1492 | */ |
| 1493 | *activep = oactive; |
| 1494 | if (r != 1) |
| 1495 | value = -1; |
| 1496 | } |
| 1497 | globfree(&gl); |
| 1498 | } |
| 1499 | if (value != 0) |
| 1500 | return value; |
| 1501 | break; |
| 1502 | |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1503 | case oIPQoS: |
| 1504 | arg = strdelim(&s); |
| 1505 | if ((value = parse_ipqos(arg)) == -1) |
| 1506 | fatal("%s line %d: Bad IPQoS value: %s", |
| 1507 | filename, linenum, arg); |
| 1508 | arg = strdelim(&s); |
| 1509 | if (arg == NULL) |
| 1510 | value2 = value; |
| 1511 | else if ((value2 = parse_ipqos(arg)) == -1) |
| 1512 | fatal("%s line %d: Bad IPQoS value: %s", |
| 1513 | filename, linenum, arg); |
| 1514 | if (*activep) { |
| 1515 | options->ip_qos_interactive = value; |
| 1516 | options->ip_qos_bulk = value2; |
| 1517 | } |
| 1518 | break; |
| 1519 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1520 | case oRequestTTY: |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1521 | intptr = &options->request_tty; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1522 | multistate_ptr = multistate_requesttty; |
| 1523 | goto parse_multistate; |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1524 | |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 1525 | case oIgnoreUnknown: |
| 1526 | charptr = &options->ignored_unknown; |
| 1527 | goto parse_string; |
| 1528 | |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 1529 | case oProxyUseFdpass: |
| 1530 | intptr = &options->proxy_use_fdpass; |
| 1531 | goto parse_flag; |
| 1532 | |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1533 | case oCanonicalDomains: |
| 1534 | value = options->num_canonical_domains != 0; |
| 1535 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1536 | valid_domain(arg, filename, linenum); |
| 1537 | if (!*activep || value) |
| 1538 | continue; |
| 1539 | if (options->num_canonical_domains >= MAX_CANON_DOMAINS) |
| 1540 | fatal("%s line %d: too many hostname suffixes.", |
| 1541 | filename, linenum); |
| 1542 | options->canonical_domains[ |
| 1543 | options->num_canonical_domains++] = xstrdup(arg); |
| 1544 | } |
| 1545 | break; |
| 1546 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1547 | case oCanonicalizePermittedCNAMEs: |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1548 | value = options->num_permitted_cnames != 0; |
| 1549 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1550 | /* Either '*' for everything or 'list:list' */ |
| 1551 | if (strcmp(arg, "*") == 0) |
| 1552 | arg2 = arg; |
| 1553 | else { |
| 1554 | lowercase(arg); |
| 1555 | if ((arg2 = strchr(arg, ':')) == NULL || |
| 1556 | arg2[1] == '\0') { |
| 1557 | fatal("%s line %d: " |
| 1558 | "Invalid permitted CNAME \"%s\"", |
| 1559 | filename, linenum, arg); |
| 1560 | } |
| 1561 | *arg2 = '\0'; |
| 1562 | arg2++; |
| 1563 | } |
| 1564 | if (!*activep || value) |
| 1565 | continue; |
| 1566 | if (options->num_permitted_cnames >= MAX_CANON_DOMAINS) |
| 1567 | fatal("%s line %d: too many permitted CNAMEs.", |
| 1568 | filename, linenum); |
| 1569 | cname = options->permitted_cnames + |
| 1570 | options->num_permitted_cnames++; |
| 1571 | cname->source_list = xstrdup(arg); |
| 1572 | cname->target_list = xstrdup(arg2); |
| 1573 | } |
| 1574 | break; |
| 1575 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1576 | case oCanonicalizeHostname: |
| 1577 | intptr = &options->canonicalize_hostname; |
| 1578 | multistate_ptr = multistate_canonicalizehostname; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1579 | goto parse_multistate; |
| 1580 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1581 | case oCanonicalizeMaxDots: |
| 1582 | intptr = &options->canonicalize_max_dots; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1583 | goto parse_int; |
| 1584 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1585 | case oCanonicalizeFallbackLocal: |
| 1586 | intptr = &options->canonicalize_fallback_local; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1587 | goto parse_flag; |
| 1588 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1589 | case oStreamLocalBindMask: |
| 1590 | arg = strdelim(&s); |
| 1591 | if (!arg || *arg == '\0') |
| 1592 | fatal("%.200s line %d: Missing StreamLocalBindMask argument.", filename, linenum); |
| 1593 | /* Parse mode in octal format */ |
| 1594 | value = strtol(arg, &endofnumber, 8); |
| 1595 | if (arg == endofnumber || value < 0 || value > 0777) |
| 1596 | fatal("%.200s line %d: Bad mask.", filename, linenum); |
| 1597 | options->fwd_opts.streamlocal_bind_mask = (mode_t)value; |
| 1598 | break; |
| 1599 | |
| 1600 | case oStreamLocalBindUnlink: |
| 1601 | intptr = &options->fwd_opts.streamlocal_bind_unlink; |
| 1602 | goto parse_flag; |
| 1603 | |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1604 | case oRevokedHostKeys: |
| 1605 | charptr = &options->revoked_host_keys; |
| 1606 | goto parse_string; |
| 1607 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1608 | case oFingerprintHash: |
djm@openbsd.org | e752c6d | 2015-01-08 13:44:36 +0000 | [diff] [blame] | 1609 | intptr = &options->fingerprint_hash; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1610 | arg = strdelim(&s); |
| 1611 | if (!arg || *arg == '\0') |
| 1612 | fatal("%.200s line %d: Missing argument.", |
| 1613 | filename, linenum); |
| 1614 | if ((value = ssh_digest_alg_by_name(arg)) == -1) |
| 1615 | fatal("%.200s line %d: Invalid hash algorithm \"%s\".", |
| 1616 | filename, linenum, arg); |
djm@openbsd.org | e752c6d | 2015-01-08 13:44:36 +0000 | [diff] [blame] | 1617 | if (*activep && *intptr == -1) |
| 1618 | *intptr = value; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1619 | break; |
| 1620 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1621 | case oUpdateHostkeys: |
| 1622 | intptr = &options->update_hostkeys; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1623 | multistate_ptr = multistate_yesnoask; |
| 1624 | goto parse_multistate; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1625 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1626 | case oHostbasedKeyTypes: |
| 1627 | charptr = &options->hostbased_key_types; |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1628 | goto parse_keytypes; |
| 1629 | |
| 1630 | case oPubkeyAcceptedKeyTypes: |
| 1631 | charptr = &options->pubkey_key_types; |
| 1632 | goto parse_keytypes; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1633 | |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1634 | case oAddKeysToAgent: |
| 1635 | intptr = &options->add_keys_to_agent; |
| 1636 | multistate_ptr = multistate_yesnoaskconfirm; |
| 1637 | goto parse_multistate; |
| 1638 | |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 1639 | case oDeprecated: |
Ben Lindstrom | 2e17b08 | 2002-06-09 20:13:27 +0000 | [diff] [blame] | 1640 | debug("%s line %d: Deprecated option \"%s\"", |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 1641 | filename, linenum, keyword); |
Ben Lindstrom | 2e17b08 | 2002-06-09 20:13:27 +0000 | [diff] [blame] | 1642 | return 0; |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 1643 | |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 1644 | case oUnsupported: |
| 1645 | error("%s line %d: Unsupported option \"%s\"", |
| 1646 | filename, linenum, keyword); |
| 1647 | return 0; |
| 1648 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1649 | default: |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 1650 | fatal("%s: Unimplemented opcode %d", __func__, opcode); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* Check that there is no garbage at end of line. */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1654 | if ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1655 | fatal("%.200s line %d: garbage at end of line; \"%.200s\".", |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 1656 | filename, linenum, arg); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1657 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1658 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1659 | } |
| 1660 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1661 | /* |
| 1662 | * Reads the config file and modifies the options accordingly. Options |
| 1663 | * should already be initialized before this call. This never returns if |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1664 | * there is an error. If the file does not exist, this returns 0. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1665 | */ |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1666 | int |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1667 | read_config_file(const char *filename, struct passwd *pw, const char *host, |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 1668 | const char *original_host, Options *options, int flags) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1669 | { |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1670 | int active = 1; |
| 1671 | |
| 1672 | return read_config_file_depth(filename, pw, host, original_host, |
| 1673 | options, flags, &active, 0); |
| 1674 | } |
| 1675 | |
| 1676 | #define READCONF_MAX_DEPTH 16 |
| 1677 | static int |
| 1678 | read_config_file_depth(const char *filename, struct passwd *pw, |
| 1679 | const char *host, const char *original_host, Options *options, |
| 1680 | int flags, int *activep, int depth) |
| 1681 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1682 | FILE *f; |
| 1683 | char line[1024]; |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1684 | int linenum; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1685 | int bad_options = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1686 | |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1687 | if (depth < 0 || depth > READCONF_MAX_DEPTH) |
| 1688 | fatal("Too many recursive configuration includes"); |
| 1689 | |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1690 | if ((f = fopen(filename, "r")) == NULL) |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1691 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1692 | |
Darren Tucker | aefa368 | 2013-04-05 11:18:35 +1100 | [diff] [blame] | 1693 | if (flags & SSHCONF_CHECKPERM) { |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1694 | struct stat sb; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1695 | |
Damien Miller | 3379385 | 2004-06-15 10:27:55 +1000 | [diff] [blame] | 1696 | if (fstat(fileno(f), &sb) == -1) |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1697 | fatal("fstat %s: %s", filename, strerror(errno)); |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1698 | if (((sb.st_uid != 0 && sb.st_uid != getuid()) || |
Damien Miller | 3379385 | 2004-06-15 10:27:55 +1000 | [diff] [blame] | 1699 | (sb.st_mode & 022) != 0)) |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1700 | fatal("Bad owner or permissions on %s", filename); |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1701 | } |
| 1702 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1703 | debug("Reading configuration data %.200s", filename); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1704 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1705 | /* |
| 1706 | * Mark that we are now processing the options. This flag is turned |
| 1707 | * on/off by Host specifications. |
| 1708 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1709 | linenum = 0; |
| 1710 | while (fgets(line, sizeof(line), f)) { |
| 1711 | /* Update line number counter. */ |
| 1712 | linenum++; |
djm@openbsd.org | dc7990b | 2016-04-15 00:30:19 +0000 | [diff] [blame^] | 1713 | if (process_config_line_depth(options, pw, host, original_host, |
| 1714 | line, filename, linenum, activep, flags, depth) != 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1715 | bad_options++; |
| 1716 | } |
| 1717 | fclose(f); |
| 1718 | if (bad_options > 0) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 1719 | fatal("%s: terminating, %d bad configuration options", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1720 | filename, bad_options); |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1721 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1722 | } |
| 1723 | |
Damien Miller | 13f97b2 | 2014-02-24 15:57:55 +1100 | [diff] [blame] | 1724 | /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */ |
| 1725 | int |
| 1726 | option_clear_or_none(const char *o) |
| 1727 | { |
| 1728 | return o == NULL || strcasecmp(o, "none") == 0; |
| 1729 | } |
| 1730 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1731 | /* |
| 1732 | * Initializes options to special values that indicate that they have not yet |
| 1733 | * been set. Read_config_file will only set options with this value. Options |
| 1734 | * are processed in the following order: command line, user config file, |
| 1735 | * system config file. Last, fill_default_options is called. |
| 1736 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1737 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1738 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1739 | initialize_options(Options * options) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1740 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1741 | memset(options, 'X', sizeof(*options)); |
| 1742 | options->forward_agent = -1; |
| 1743 | options->forward_x11 = -1; |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 1744 | options->forward_x11_trusted = -1; |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1745 | options->forward_x11_timeout = -1; |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 1746 | options->exit_on_forward_failure = -1; |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1747 | options->xauth_location = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1748 | options->fwd_opts.gateway_ports = -1; |
| 1749 | options->fwd_opts.streamlocal_bind_mask = (mode_t)-1; |
| 1750 | options->fwd_opts.streamlocal_bind_unlink = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1751 | options->use_privileged_port = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1752 | options->rsa_authentication = -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1753 | options->pubkey_authentication = -1; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1754 | options->challenge_response_authentication = -1; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1755 | options->gss_authentication = -1; |
| 1756 | options->gss_deleg_creds = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1757 | options->password_authentication = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1758 | options->kbd_interactive_authentication = -1; |
| 1759 | options->kbd_interactive_devices = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1760 | options->rhosts_rsa_authentication = -1; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1761 | options->hostbased_authentication = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1762 | options->batch_mode = -1; |
| 1763 | options->check_host_ip = -1; |
| 1764 | options->strict_host_key_checking = -1; |
| 1765 | options->compression = -1; |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 1766 | options->tcp_keep_alive = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1767 | options->compression_level = -1; |
| 1768 | options->port = -1; |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 1769 | options->address_family = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1770 | options->connection_attempts = -1; |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 1771 | options->connection_timeout = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1772 | options->number_of_password_prompts = -1; |
| 1773 | options->cipher = -1; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1774 | options->ciphers = NULL; |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1775 | options->macs = NULL; |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 1776 | options->kex_algorithms = NULL; |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1777 | options->hostkeyalgorithms = NULL; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1778 | options->protocol = SSH_PROTO_UNKNOWN; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1779 | options->num_identity_files = 0; |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1780 | options->num_certificate_files = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1781 | options->hostname = NULL; |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 1782 | options->host_key_alias = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1783 | options->proxy_command = NULL; |
| 1784 | options->user = NULL; |
| 1785 | options->escape_char = -1; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1786 | options->num_system_hostfiles = 0; |
| 1787 | options->num_user_hostfiles = 0; |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 1788 | options->local_forwards = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1789 | options->num_local_forwards = 0; |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 1790 | options->remote_forwards = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1791 | options->num_remote_forwards = 0; |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 1792 | options->clear_forwardings = -1; |
Damien Miller | fcd9320 | 2002-02-05 12:26:34 +1100 | [diff] [blame] | 1793 | options->log_level = SYSLOG_LEVEL_NOT_SET; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1794 | options->preferred_authentications = NULL; |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 1795 | options->bind_address = NULL; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1796 | options->pkcs11_provider = NULL; |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 1797 | options->enable_ssh_keysign = - 1; |
Ben Lindstrom | 3cecc9a | 2001-10-03 17:39:38 +0000 | [diff] [blame] | 1798 | options->no_host_authentication_for_localhost = - 1; |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1799 | options->identities_only = - 1; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1800 | options->rekey_limit = - 1; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1801 | options->rekey_interval = -1; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1802 | options->verify_host_key_dns = -1; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1803 | options->server_alive_interval = -1; |
| 1804 | options->server_alive_count_max = -1; |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1805 | options->num_send_env = 0; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1806 | options->control_path = NULL; |
| 1807 | options->control_master = -1; |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1808 | options->control_persist = -1; |
| 1809 | options->control_persist_timeout = 0; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 1810 | options->hash_known_hosts = -1; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1811 | options->tun_open = -1; |
| 1812 | options->tun_local = -1; |
| 1813 | options->tun_remote = -1; |
| 1814 | options->local_command = NULL; |
| 1815 | options->permit_local_command = -1; |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1816 | options->add_keys_to_agent = -1; |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1817 | options->visual_host_key = -1; |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1818 | options->ip_qos_interactive = -1; |
| 1819 | options->ip_qos_bulk = -1; |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1820 | options->request_tty = -1; |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 1821 | options->proxy_use_fdpass = -1; |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 1822 | options->ignored_unknown = NULL; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1823 | options->num_canonical_domains = 0; |
| 1824 | options->num_permitted_cnames = 0; |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1825 | options->canonicalize_max_dots = -1; |
| 1826 | options->canonicalize_fallback_local = -1; |
| 1827 | options->canonicalize_hostname = -1; |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1828 | options->revoked_host_keys = NULL; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1829 | options->fingerprint_hash = -1; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1830 | options->update_hostkeys = -1; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1831 | options->hostbased_key_types = NULL; |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1832 | options->pubkey_key_types = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1833 | } |
| 1834 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1835 | /* |
Damien Miller | 13f97b2 | 2014-02-24 15:57:55 +1100 | [diff] [blame] | 1836 | * A petite version of fill_default_options() that just fills the options |
| 1837 | * needed for hostname canonicalization to proceed. |
| 1838 | */ |
| 1839 | void |
| 1840 | fill_default_options_for_canonicalization(Options *options) |
| 1841 | { |
| 1842 | if (options->canonicalize_max_dots == -1) |
| 1843 | options->canonicalize_max_dots = 1; |
| 1844 | if (options->canonicalize_fallback_local == -1) |
| 1845 | options->canonicalize_fallback_local = 1; |
| 1846 | if (options->canonicalize_hostname == -1) |
| 1847 | options->canonicalize_hostname = SSH_CANONICALISE_NO; |
| 1848 | } |
| 1849 | |
| 1850 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1851 | * Called after processing other sources of option data, this fills those |
| 1852 | * options for which no value has been specified with their default values. |
| 1853 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1854 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1855 | fill_default_options(Options * options) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1856 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1857 | if (options->forward_agent == -1) |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1858 | options->forward_agent = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1859 | if (options->forward_x11 == -1) |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 1860 | options->forward_x11 = 0; |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 1861 | if (options->forward_x11_trusted == -1) |
| 1862 | options->forward_x11_trusted = 0; |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1863 | if (options->forward_x11_timeout == -1) |
| 1864 | options->forward_x11_timeout = 1200; |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 1865 | if (options->exit_on_forward_failure == -1) |
| 1866 | options->exit_on_forward_failure = 0; |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1867 | if (options->xauth_location == NULL) |
Ben Lindstrom | 1bf11f6 | 2001-06-09 01:48:01 +0000 | [diff] [blame] | 1868 | options->xauth_location = _PATH_XAUTH; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1869 | if (options->fwd_opts.gateway_ports == -1) |
| 1870 | options->fwd_opts.gateway_ports = 0; |
| 1871 | if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1) |
| 1872 | options->fwd_opts.streamlocal_bind_mask = 0177; |
| 1873 | if (options->fwd_opts.streamlocal_bind_unlink == -1) |
| 1874 | options->fwd_opts.streamlocal_bind_unlink = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1875 | if (options->use_privileged_port == -1) |
Ben Lindstrom | cebc858 | 2001-03-08 03:39:10 +0000 | [diff] [blame] | 1876 | options->use_privileged_port = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1877 | if (options->rsa_authentication == -1) |
| 1878 | options->rsa_authentication = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1879 | if (options->pubkey_authentication == -1) |
| 1880 | options->pubkey_authentication = 1; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1881 | if (options->challenge_response_authentication == -1) |
Ben Lindstrom | 0076d75 | 2001-08-06 20:53:26 +0000 | [diff] [blame] | 1882 | options->challenge_response_authentication = 1; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1883 | if (options->gss_authentication == -1) |
Darren Tucker | a044f47 | 2003-10-15 15:52:03 +1000 | [diff] [blame] | 1884 | options->gss_authentication = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1885 | if (options->gss_deleg_creds == -1) |
| 1886 | options->gss_deleg_creds = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1887 | if (options->password_authentication == -1) |
| 1888 | options->password_authentication = 1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1889 | if (options->kbd_interactive_authentication == -1) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 1890 | options->kbd_interactive_authentication = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1891 | if (options->rhosts_rsa_authentication == -1) |
Ben Lindstrom | 2bf8276 | 2002-06-11 15:53:05 +0000 | [diff] [blame] | 1892 | options->rhosts_rsa_authentication = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1893 | if (options->hostbased_authentication == -1) |
| 1894 | options->hostbased_authentication = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1895 | if (options->batch_mode == -1) |
| 1896 | options->batch_mode = 0; |
| 1897 | if (options->check_host_ip == -1) |
| 1898 | options->check_host_ip = 1; |
| 1899 | if (options->strict_host_key_checking == -1) |
| 1900 | options->strict_host_key_checking = 2; /* 2 is default */ |
| 1901 | if (options->compression == -1) |
| 1902 | options->compression = 0; |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 1903 | if (options->tcp_keep_alive == -1) |
| 1904 | options->tcp_keep_alive = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1905 | if (options->compression_level == -1) |
| 1906 | options->compression_level = 6; |
| 1907 | if (options->port == -1) |
| 1908 | options->port = 0; /* Filled in ssh_connect. */ |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 1909 | if (options->address_family == -1) |
| 1910 | options->address_family = AF_UNSPEC; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1911 | if (options->connection_attempts == -1) |
Ben Lindstrom | f9cedb9 | 2001-08-06 21:07:11 +0000 | [diff] [blame] | 1912 | options->connection_attempts = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1913 | if (options->number_of_password_prompts == -1) |
| 1914 | options->number_of_password_prompts = 3; |
| 1915 | /* Selected in ssh_login(). */ |
| 1916 | if (options->cipher == -1) |
| 1917 | options->cipher = SSH_CIPHER_NOT_SET; |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1918 | /* options->hostkeyalgorithms, default set in myproposals.h */ |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1919 | if (options->protocol == SSH_PROTO_UNKNOWN) |
Darren Tucker | bad5076 | 2009-10-11 21:51:08 +1100 | [diff] [blame] | 1920 | options->protocol = SSH_PROTO_2; |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1921 | if (options->add_keys_to_agent == -1) |
| 1922 | options->add_keys_to_agent = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1923 | if (options->num_identity_files == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1924 | if (options->protocol & SSH_PROTO_1) { |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1925 | add_identity_file(options, "~/", |
| 1926 | _PATH_SSH_CLIENT_IDENTITY, 0); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1927 | } |
| 1928 | if (options->protocol & SSH_PROTO_2) { |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1929 | add_identity_file(options, "~/", |
| 1930 | _PATH_SSH_CLIENT_ID_RSA, 0); |
| 1931 | add_identity_file(options, "~/", |
| 1932 | _PATH_SSH_CLIENT_ID_DSA, 0); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1933 | #ifdef OPENSSL_HAS_ECC |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1934 | add_identity_file(options, "~/", |
| 1935 | _PATH_SSH_CLIENT_ID_ECDSA, 0); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1936 | #endif |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 1937 | add_identity_file(options, "~/", |
| 1938 | _PATH_SSH_CLIENT_ID_ED25519, 0); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1939 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1940 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1941 | if (options->escape_char == -1) |
| 1942 | options->escape_char = '~'; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1943 | if (options->num_system_hostfiles == 0) { |
| 1944 | options->system_hostfiles[options->num_system_hostfiles++] = |
| 1945 | xstrdup(_PATH_SSH_SYSTEM_HOSTFILE); |
| 1946 | options->system_hostfiles[options->num_system_hostfiles++] = |
| 1947 | xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2); |
| 1948 | } |
| 1949 | if (options->num_user_hostfiles == 0) { |
| 1950 | options->user_hostfiles[options->num_user_hostfiles++] = |
| 1951 | xstrdup(_PATH_SSH_USER_HOSTFILE); |
| 1952 | options->user_hostfiles[options->num_user_hostfiles++] = |
| 1953 | xstrdup(_PATH_SSH_USER_HOSTFILE2); |
| 1954 | } |
Damien Miller | fcd9320 | 2002-02-05 12:26:34 +1100 | [diff] [blame] | 1955 | if (options->log_level == SYSLOG_LEVEL_NOT_SET) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 1956 | options->log_level = SYSLOG_LEVEL_INFO; |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 1957 | if (options->clear_forwardings == 1) |
| 1958 | clear_forwardings(options); |
Ben Lindstrom | 3cecc9a | 2001-10-03 17:39:38 +0000 | [diff] [blame] | 1959 | if (options->no_host_authentication_for_localhost == - 1) |
| 1960 | options->no_host_authentication_for_localhost = 0; |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1961 | if (options->identities_only == -1) |
| 1962 | options->identities_only = 0; |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 1963 | if (options->enable_ssh_keysign == -1) |
| 1964 | options->enable_ssh_keysign = 0; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1965 | if (options->rekey_limit == -1) |
| 1966 | options->rekey_limit = 0; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1967 | if (options->rekey_interval == -1) |
| 1968 | options->rekey_interval = 0; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1969 | if (options->verify_host_key_dns == -1) |
| 1970 | options->verify_host_key_dns = 0; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1971 | if (options->server_alive_interval == -1) |
| 1972 | options->server_alive_interval = 0; |
| 1973 | if (options->server_alive_count_max == -1) |
| 1974 | options->server_alive_count_max = 3; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1975 | if (options->control_master == -1) |
| 1976 | options->control_master = 0; |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1977 | if (options->control_persist == -1) { |
| 1978 | options->control_persist = 0; |
| 1979 | options->control_persist_timeout = 0; |
| 1980 | } |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 1981 | if (options->hash_known_hosts == -1) |
| 1982 | options->hash_known_hosts = 0; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1983 | if (options->tun_open == -1) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1984 | options->tun_open = SSH_TUNMODE_NO; |
| 1985 | if (options->tun_local == -1) |
| 1986 | options->tun_local = SSH_TUNID_ANY; |
| 1987 | if (options->tun_remote == -1) |
| 1988 | options->tun_remote = SSH_TUNID_ANY; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1989 | if (options->permit_local_command == -1) |
| 1990 | options->permit_local_command = 0; |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1991 | if (options->visual_host_key == -1) |
| 1992 | options->visual_host_key = 0; |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1993 | if (options->ip_qos_interactive == -1) |
| 1994 | options->ip_qos_interactive = IPTOS_LOWDELAY; |
| 1995 | if (options->ip_qos_bulk == -1) |
| 1996 | options->ip_qos_bulk = IPTOS_THROUGHPUT; |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1997 | if (options->request_tty == -1) |
| 1998 | options->request_tty = REQUEST_TTY_AUTO; |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 1999 | if (options->proxy_use_fdpass == -1) |
| 2000 | options->proxy_use_fdpass = 0; |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 2001 | if (options->canonicalize_max_dots == -1) |
| 2002 | options->canonicalize_max_dots = 1; |
| 2003 | if (options->canonicalize_fallback_local == -1) |
| 2004 | options->canonicalize_fallback_local = 1; |
| 2005 | if (options->canonicalize_hostname == -1) |
| 2006 | options->canonicalize_hostname = SSH_CANONICALISE_NO; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 2007 | if (options->fingerprint_hash == -1) |
| 2008 | options->fingerprint_hash = SSH_FP_HASH_DEFAULT; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2009 | if (options->update_hostkeys == -1) |
djm@openbsd.org | 15ad750 | 2015-02-02 07:41:40 +0000 | [diff] [blame] | 2010 | options->update_hostkeys = 0; |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 2011 | if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 || |
| 2012 | kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 || |
| 2013 | kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 || |
| 2014 | kex_assemble_names(KEX_DEFAULT_PK_ALG, |
| 2015 | &options->hostbased_key_types) != 0 || |
| 2016 | kex_assemble_names(KEX_DEFAULT_PK_ALG, |
| 2017 | &options->pubkey_key_types) != 0) |
| 2018 | fatal("%s: kex_assemble_names failed", __func__); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 2019 | |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 2020 | #define CLEAR_ON_NONE(v) \ |
| 2021 | do { \ |
Damien Miller | 13f97b2 | 2014-02-24 15:57:55 +1100 | [diff] [blame] | 2022 | if (option_clear_or_none(v)) { \ |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 2023 | free(v); \ |
| 2024 | v = NULL; \ |
| 2025 | } \ |
| 2026 | } while(0) |
| 2027 | CLEAR_ON_NONE(options->local_command); |
| 2028 | CLEAR_ON_NONE(options->proxy_command); |
| 2029 | CLEAR_ON_NONE(options->control_path); |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 2030 | CLEAR_ON_NONE(options->revoked_host_keys); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2031 | /* options->user will be set in the main program if appropriate */ |
| 2032 | /* options->hostname will be set in the main program if appropriate */ |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 2033 | /* options->host_key_alias should not be set by default */ |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2034 | /* options->preferred_authentications will be set in ssh */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2035 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2036 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2037 | struct fwdarg { |
| 2038 | char *arg; |
| 2039 | int ispath; |
| 2040 | }; |
| 2041 | |
| 2042 | /* |
| 2043 | * parse_fwd_field |
| 2044 | * parses the next field in a port forwarding specification. |
| 2045 | * sets fwd to the parsed field and advances p past the colon |
| 2046 | * or sets it to NULL at end of string. |
| 2047 | * returns 0 on success, else non-zero. |
| 2048 | */ |
| 2049 | static int |
| 2050 | parse_fwd_field(char **p, struct fwdarg *fwd) |
| 2051 | { |
| 2052 | char *ep, *cp = *p; |
| 2053 | int ispath = 0; |
| 2054 | |
| 2055 | if (*cp == '\0') { |
| 2056 | *p = NULL; |
| 2057 | return -1; /* end of string */ |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * A field escaped with square brackets is used literally. |
| 2062 | * XXX - allow ']' to be escaped via backslash? |
| 2063 | */ |
| 2064 | if (*cp == '[') { |
| 2065 | /* find matching ']' */ |
| 2066 | for (ep = cp + 1; *ep != ']' && *ep != '\0'; ep++) { |
| 2067 | if (*ep == '/') |
| 2068 | ispath = 1; |
| 2069 | } |
| 2070 | /* no matching ']' or not at end of field. */ |
| 2071 | if (ep[0] != ']' || (ep[1] != ':' && ep[1] != '\0')) |
| 2072 | return -1; |
| 2073 | /* NUL terminate the field and advance p past the colon */ |
| 2074 | *ep++ = '\0'; |
| 2075 | if (*ep != '\0') |
| 2076 | *ep++ = '\0'; |
| 2077 | fwd->arg = cp + 1; |
| 2078 | fwd->ispath = ispath; |
| 2079 | *p = ep; |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
| 2083 | for (cp = *p; *cp != '\0'; cp++) { |
| 2084 | switch (*cp) { |
| 2085 | case '\\': |
| 2086 | memmove(cp, cp + 1, strlen(cp + 1) + 1); |
djm@openbsd.org | 78c2a4f | 2015-06-26 05:13:20 +0000 | [diff] [blame] | 2087 | if (*cp == '\0') |
| 2088 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2089 | break; |
| 2090 | case '/': |
| 2091 | ispath = 1; |
| 2092 | break; |
| 2093 | case ':': |
| 2094 | *cp++ = '\0'; |
| 2095 | goto done; |
| 2096 | } |
| 2097 | } |
| 2098 | done: |
| 2099 | fwd->arg = *p; |
| 2100 | fwd->ispath = ispath; |
| 2101 | *p = cp; |
| 2102 | return 0; |
| 2103 | } |
| 2104 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2105 | /* |
| 2106 | * parse_forward |
| 2107 | * parses a string containing a port forwarding specification of the form: |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 2108 | * dynamicfwd == 0 |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2109 | * [listenhost:]listenport|listenpath:connecthost:connectport|connectpath |
| 2110 | * listenpath:connectpath |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 2111 | * dynamicfwd == 1 |
| 2112 | * [listenhost:]listenport |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2113 | * returns number of arguments parsed or zero on error |
| 2114 | */ |
| 2115 | int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2116 | parse_forward(struct Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2117 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2118 | struct fwdarg fwdargs[4]; |
| 2119 | char *p, *cp; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2120 | int i; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2121 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2122 | memset(fwd, 0, sizeof(*fwd)); |
| 2123 | memset(fwdargs, 0, sizeof(fwdargs)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2124 | |
| 2125 | cp = p = xstrdup(fwdspec); |
| 2126 | |
| 2127 | /* skip leading spaces */ |
Damien Miller | fdb2306 | 2013-11-21 13:57:15 +1100 | [diff] [blame] | 2128 | while (isspace((u_char)*cp)) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2129 | cp++; |
| 2130 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2131 | for (i = 0; i < 4; ++i) { |
| 2132 | if (parse_fwd_field(&cp, &fwdargs[i]) != 0) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2133 | break; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2134 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2135 | |
Damien Miller | f4b3953 | 2008-11-03 19:28:21 +1100 | [diff] [blame] | 2136 | /* Check for trailing garbage */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2137 | if (cp != NULL && *cp != '\0') { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2138 | i = 0; /* failure */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2139 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2140 | |
| 2141 | switch (i) { |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 2142 | case 1: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2143 | if (fwdargs[0].ispath) { |
| 2144 | fwd->listen_path = xstrdup(fwdargs[0].arg); |
| 2145 | fwd->listen_port = PORT_STREAMLOCAL; |
| 2146 | } else { |
| 2147 | fwd->listen_host = NULL; |
| 2148 | fwd->listen_port = a2port(fwdargs[0].arg); |
| 2149 | } |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 2150 | fwd->connect_host = xstrdup("socks"); |
| 2151 | break; |
| 2152 | |
| 2153 | case 2: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2154 | if (fwdargs[0].ispath && fwdargs[1].ispath) { |
| 2155 | fwd->listen_path = xstrdup(fwdargs[0].arg); |
| 2156 | fwd->listen_port = PORT_STREAMLOCAL; |
| 2157 | fwd->connect_path = xstrdup(fwdargs[1].arg); |
| 2158 | fwd->connect_port = PORT_STREAMLOCAL; |
| 2159 | } else if (fwdargs[1].ispath) { |
| 2160 | fwd->listen_host = NULL; |
| 2161 | fwd->listen_port = a2port(fwdargs[0].arg); |
| 2162 | fwd->connect_path = xstrdup(fwdargs[1].arg); |
| 2163 | fwd->connect_port = PORT_STREAMLOCAL; |
| 2164 | } else { |
| 2165 | fwd->listen_host = xstrdup(fwdargs[0].arg); |
| 2166 | fwd->listen_port = a2port(fwdargs[1].arg); |
| 2167 | fwd->connect_host = xstrdup("socks"); |
| 2168 | } |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 2169 | break; |
| 2170 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2171 | case 3: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2172 | if (fwdargs[0].ispath) { |
| 2173 | fwd->listen_path = xstrdup(fwdargs[0].arg); |
| 2174 | fwd->listen_port = PORT_STREAMLOCAL; |
| 2175 | fwd->connect_host = xstrdup(fwdargs[1].arg); |
| 2176 | fwd->connect_port = a2port(fwdargs[2].arg); |
| 2177 | } else if (fwdargs[2].ispath) { |
| 2178 | fwd->listen_host = xstrdup(fwdargs[0].arg); |
| 2179 | fwd->listen_port = a2port(fwdargs[1].arg); |
| 2180 | fwd->connect_path = xstrdup(fwdargs[2].arg); |
| 2181 | fwd->connect_port = PORT_STREAMLOCAL; |
| 2182 | } else { |
| 2183 | fwd->listen_host = NULL; |
| 2184 | fwd->listen_port = a2port(fwdargs[0].arg); |
| 2185 | fwd->connect_host = xstrdup(fwdargs[1].arg); |
| 2186 | fwd->connect_port = a2port(fwdargs[2].arg); |
| 2187 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2188 | break; |
| 2189 | |
| 2190 | case 4: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2191 | fwd->listen_host = xstrdup(fwdargs[0].arg); |
| 2192 | fwd->listen_port = a2port(fwdargs[1].arg); |
| 2193 | fwd->connect_host = xstrdup(fwdargs[2].arg); |
| 2194 | fwd->connect_port = a2port(fwdargs[3].arg); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2195 | break; |
| 2196 | default: |
| 2197 | i = 0; /* failure */ |
| 2198 | } |
| 2199 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2200 | free(p); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2201 | |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 2202 | if (dynamicfwd) { |
| 2203 | if (!(i == 1 || i == 2)) |
| 2204 | goto fail_free; |
| 2205 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2206 | if (!(i == 3 || i == 4)) { |
| 2207 | if (fwd->connect_path == NULL && |
| 2208 | fwd->listen_path == NULL) |
| 2209 | goto fail_free; |
| 2210 | } |
| 2211 | if (fwd->connect_port <= 0 && fwd->connect_path == NULL) |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 2212 | goto fail_free; |
| 2213 | } |
| 2214 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2215 | if ((fwd->listen_port < 0 && fwd->listen_path == NULL) || |
| 2216 | (!remotefwd && fwd->listen_port == 0)) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2217 | goto fail_free; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2218 | if (fwd->connect_host != NULL && |
| 2219 | strlen(fwd->connect_host) >= NI_MAXHOST) |
| 2220 | goto fail_free; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2221 | /* XXX - if connecting to a remote socket, max sun len may not match this host */ |
| 2222 | if (fwd->connect_path != NULL && |
| 2223 | strlen(fwd->connect_path) >= PATH_MAX_SUN) |
| 2224 | goto fail_free; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2225 | if (fwd->listen_host != NULL && |
| 2226 | strlen(fwd->listen_host) >= NI_MAXHOST) |
| 2227 | goto fail_free; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2228 | if (fwd->listen_path != NULL && |
| 2229 | strlen(fwd->listen_path) >= PATH_MAX_SUN) |
| 2230 | goto fail_free; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2231 | |
| 2232 | return (i); |
| 2233 | |
| 2234 | fail_free: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2235 | free(fwd->connect_host); |
| 2236 | fwd->connect_host = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2237 | free(fwd->connect_path); |
| 2238 | fwd->connect_path = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2239 | free(fwd->listen_host); |
| 2240 | fwd->listen_host = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2241 | free(fwd->listen_path); |
| 2242 | fwd->listen_path = NULL; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2243 | return (0); |
| 2244 | } |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2245 | |
| 2246 | /* XXX the following is a near-vebatim copy from servconf.c; refactor */ |
| 2247 | static const char * |
| 2248 | fmt_multistate_int(int val, const struct multistate *m) |
| 2249 | { |
| 2250 | u_int i; |
| 2251 | |
| 2252 | for (i = 0; m[i].key != NULL; i++) { |
| 2253 | if (m[i].value == val) |
| 2254 | return m[i].key; |
| 2255 | } |
| 2256 | return "UNKNOWN"; |
| 2257 | } |
| 2258 | |
| 2259 | static const char * |
| 2260 | fmt_intarg(OpCodes code, int val) |
| 2261 | { |
| 2262 | if (val == -1) |
| 2263 | return "unset"; |
| 2264 | switch (code) { |
| 2265 | case oAddressFamily: |
| 2266 | return fmt_multistate_int(val, multistate_addressfamily); |
| 2267 | case oVerifyHostKeyDNS: |
| 2268 | case oStrictHostKeyChecking: |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2269 | case oUpdateHostkeys: |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2270 | return fmt_multistate_int(val, multistate_yesnoask); |
| 2271 | case oControlMaster: |
| 2272 | return fmt_multistate_int(val, multistate_controlmaster); |
| 2273 | case oTunnel: |
| 2274 | return fmt_multistate_int(val, multistate_tunnel); |
| 2275 | case oRequestTTY: |
| 2276 | return fmt_multistate_int(val, multistate_requesttty); |
| 2277 | case oCanonicalizeHostname: |
| 2278 | return fmt_multistate_int(val, multistate_canonicalizehostname); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 2279 | case oFingerprintHash: |
| 2280 | return ssh_digest_alg_name(val); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2281 | case oProtocol: |
| 2282 | switch (val) { |
| 2283 | case SSH_PROTO_1: |
| 2284 | return "1"; |
| 2285 | case SSH_PROTO_2: |
| 2286 | return "2"; |
| 2287 | case (SSH_PROTO_1|SSH_PROTO_2): |
| 2288 | return "2,1"; |
| 2289 | default: |
| 2290 | return "UNKNOWN"; |
| 2291 | } |
| 2292 | default: |
| 2293 | switch (val) { |
| 2294 | case 0: |
| 2295 | return "no"; |
| 2296 | case 1: |
| 2297 | return "yes"; |
| 2298 | default: |
| 2299 | return "UNKNOWN"; |
| 2300 | } |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | static const char * |
| 2305 | lookup_opcode_name(OpCodes code) |
| 2306 | { |
| 2307 | u_int i; |
| 2308 | |
| 2309 | for (i = 0; keywords[i].name != NULL; i++) |
| 2310 | if (keywords[i].opcode == code) |
| 2311 | return(keywords[i].name); |
| 2312 | return "UNKNOWN"; |
| 2313 | } |
| 2314 | |
| 2315 | static void |
| 2316 | dump_cfg_int(OpCodes code, int val) |
| 2317 | { |
| 2318 | printf("%s %d\n", lookup_opcode_name(code), val); |
| 2319 | } |
| 2320 | |
| 2321 | static void |
| 2322 | dump_cfg_fmtint(OpCodes code, int val) |
| 2323 | { |
| 2324 | printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val)); |
| 2325 | } |
| 2326 | |
| 2327 | static void |
| 2328 | dump_cfg_string(OpCodes code, const char *val) |
| 2329 | { |
| 2330 | if (val == NULL) |
| 2331 | return; |
| 2332 | printf("%s %s\n", lookup_opcode_name(code), val); |
| 2333 | } |
| 2334 | |
| 2335 | static void |
| 2336 | dump_cfg_strarray(OpCodes code, u_int count, char **vals) |
| 2337 | { |
| 2338 | u_int i; |
| 2339 | |
| 2340 | for (i = 0; i < count; i++) |
| 2341 | printf("%s %s\n", lookup_opcode_name(code), vals[i]); |
| 2342 | } |
| 2343 | |
| 2344 | static void |
| 2345 | dump_cfg_strarray_oneline(OpCodes code, u_int count, char **vals) |
| 2346 | { |
| 2347 | u_int i; |
| 2348 | |
| 2349 | printf("%s", lookup_opcode_name(code)); |
| 2350 | for (i = 0; i < count; i++) |
| 2351 | printf(" %s", vals[i]); |
| 2352 | printf("\n"); |
| 2353 | } |
| 2354 | |
| 2355 | static void |
| 2356 | dump_cfg_forwards(OpCodes code, u_int count, const struct Forward *fwds) |
| 2357 | { |
| 2358 | const struct Forward *fwd; |
| 2359 | u_int i; |
| 2360 | |
| 2361 | /* oDynamicForward */ |
| 2362 | for (i = 0; i < count; i++) { |
| 2363 | fwd = &fwds[i]; |
| 2364 | if (code == oDynamicForward && |
| 2365 | strcmp(fwd->connect_host, "socks") != 0) |
| 2366 | continue; |
| 2367 | if (code == oLocalForward && |
| 2368 | strcmp(fwd->connect_host, "socks") == 0) |
| 2369 | continue; |
| 2370 | printf("%s", lookup_opcode_name(code)); |
| 2371 | if (fwd->listen_port == PORT_STREAMLOCAL) |
| 2372 | printf(" %s", fwd->listen_path); |
| 2373 | else if (fwd->listen_host == NULL) |
| 2374 | printf(" %d", fwd->listen_port); |
| 2375 | else { |
| 2376 | printf(" [%s]:%d", |
| 2377 | fwd->listen_host, fwd->listen_port); |
| 2378 | } |
| 2379 | if (code != oDynamicForward) { |
| 2380 | if (fwd->connect_port == PORT_STREAMLOCAL) |
| 2381 | printf(" %s", fwd->connect_path); |
| 2382 | else if (fwd->connect_host == NULL) |
| 2383 | printf(" %d", fwd->connect_port); |
| 2384 | else { |
| 2385 | printf(" [%s]:%d", |
| 2386 | fwd->connect_host, fwd->connect_port); |
| 2387 | } |
| 2388 | } |
| 2389 | printf("\n"); |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | void |
| 2394 | dump_client_config(Options *o, const char *host) |
| 2395 | { |
| 2396 | int i; |
| 2397 | char vbuf[5]; |
| 2398 | |
djm@openbsd.org | 60a9247 | 2015-08-21 23:53:08 +0000 | [diff] [blame] | 2399 | /* This is normally prepared in ssh_kex2 */ |
| 2400 | if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0) |
| 2401 | fatal("%s: kex_assemble_names failed", __func__); |
| 2402 | |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2403 | /* Most interesting options first: user, host, port */ |
| 2404 | dump_cfg_string(oUser, o->user); |
| 2405 | dump_cfg_string(oHostName, host); |
| 2406 | dump_cfg_int(oPort, o->port); |
| 2407 | |
| 2408 | /* Flag options */ |
| 2409 | dump_cfg_fmtint(oAddressFamily, o->address_family); |
| 2410 | dump_cfg_fmtint(oBatchMode, o->batch_mode); |
| 2411 | dump_cfg_fmtint(oCanonicalizeFallbackLocal, o->canonicalize_fallback_local); |
| 2412 | dump_cfg_fmtint(oCanonicalizeHostname, o->canonicalize_hostname); |
| 2413 | dump_cfg_fmtint(oChallengeResponseAuthentication, o->challenge_response_authentication); |
| 2414 | dump_cfg_fmtint(oCheckHostIP, o->check_host_ip); |
| 2415 | dump_cfg_fmtint(oCompression, o->compression); |
| 2416 | dump_cfg_fmtint(oControlMaster, o->control_master); |
| 2417 | dump_cfg_fmtint(oEnableSSHKeysign, o->enable_ssh_keysign); |
| 2418 | dump_cfg_fmtint(oExitOnForwardFailure, o->exit_on_forward_failure); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 2419 | dump_cfg_fmtint(oFingerprintHash, o->fingerprint_hash); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2420 | dump_cfg_fmtint(oForwardAgent, o->forward_agent); |
| 2421 | dump_cfg_fmtint(oForwardX11, o->forward_x11); |
| 2422 | dump_cfg_fmtint(oForwardX11Trusted, o->forward_x11_trusted); |
| 2423 | dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports); |
| 2424 | #ifdef GSSAPI |
| 2425 | dump_cfg_fmtint(oGssAuthentication, o->gss_authentication); |
| 2426 | dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds); |
| 2427 | #endif /* GSSAPI */ |
| 2428 | dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts); |
| 2429 | dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication); |
| 2430 | dump_cfg_fmtint(oIdentitiesOnly, o->identities_only); |
| 2431 | dump_cfg_fmtint(oKbdInteractiveAuthentication, o->kbd_interactive_authentication); |
| 2432 | dump_cfg_fmtint(oNoHostAuthenticationForLocalhost, o->no_host_authentication_for_localhost); |
| 2433 | dump_cfg_fmtint(oPasswordAuthentication, o->password_authentication); |
| 2434 | dump_cfg_fmtint(oPermitLocalCommand, o->permit_local_command); |
| 2435 | dump_cfg_fmtint(oProtocol, o->protocol); |
| 2436 | dump_cfg_fmtint(oProxyUseFdpass, o->proxy_use_fdpass); |
| 2437 | dump_cfg_fmtint(oPubkeyAuthentication, o->pubkey_authentication); |
| 2438 | dump_cfg_fmtint(oRequestTTY, o->request_tty); |
| 2439 | dump_cfg_fmtint(oRhostsRSAAuthentication, o->rhosts_rsa_authentication); |
| 2440 | dump_cfg_fmtint(oRSAAuthentication, o->rsa_authentication); |
| 2441 | dump_cfg_fmtint(oStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink); |
| 2442 | dump_cfg_fmtint(oStrictHostKeyChecking, o->strict_host_key_checking); |
| 2443 | dump_cfg_fmtint(oTCPKeepAlive, o->tcp_keep_alive); |
| 2444 | dump_cfg_fmtint(oTunnel, o->tun_open); |
| 2445 | dump_cfg_fmtint(oUsePrivilegedPort, o->use_privileged_port); |
| 2446 | dump_cfg_fmtint(oVerifyHostKeyDNS, o->verify_host_key_dns); |
| 2447 | dump_cfg_fmtint(oVisualHostKey, o->visual_host_key); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2448 | dump_cfg_fmtint(oUpdateHostkeys, o->update_hostkeys); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2449 | |
| 2450 | /* Integer options */ |
| 2451 | dump_cfg_int(oCanonicalizeMaxDots, o->canonicalize_max_dots); |
| 2452 | dump_cfg_int(oCompressionLevel, o->compression_level); |
| 2453 | dump_cfg_int(oConnectionAttempts, o->connection_attempts); |
| 2454 | dump_cfg_int(oForwardX11Timeout, o->forward_x11_timeout); |
| 2455 | dump_cfg_int(oNumberOfPasswordPrompts, o->number_of_password_prompts); |
| 2456 | dump_cfg_int(oServerAliveCountMax, o->server_alive_count_max); |
| 2457 | dump_cfg_int(oServerAliveInterval, o->server_alive_interval); |
| 2458 | |
| 2459 | /* String options */ |
| 2460 | dump_cfg_string(oBindAddress, o->bind_address); |
| 2461 | dump_cfg_string(oCiphers, o->ciphers ? o->ciphers : KEX_CLIENT_ENCRYPT); |
| 2462 | dump_cfg_string(oControlPath, o->control_path); |
djm@openbsd.org | 60a9247 | 2015-08-21 23:53:08 +0000 | [diff] [blame] | 2463 | dump_cfg_string(oHostKeyAlgorithms, o->hostkeyalgorithms); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2464 | dump_cfg_string(oHostKeyAlias, o->host_key_alias); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2465 | dump_cfg_string(oHostbasedKeyTypes, o->hostbased_key_types); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2466 | dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices); |
| 2467 | dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX); |
| 2468 | dump_cfg_string(oLocalCommand, o->local_command); |
| 2469 | dump_cfg_string(oLogLevel, log_level_name(o->log_level)); |
| 2470 | dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC); |
| 2471 | dump_cfg_string(oPKCS11Provider, o->pkcs11_provider); |
| 2472 | dump_cfg_string(oPreferredAuthentications, o->preferred_authentications); |
| 2473 | dump_cfg_string(oProxyCommand, o->proxy_command); |
djm@openbsd.org | 0c46bbe | 2015-10-07 15:59:12 +0000 | [diff] [blame] | 2474 | dump_cfg_string(oPubkeyAcceptedKeyTypes, o->pubkey_key_types); |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 2475 | dump_cfg_string(oRevokedHostKeys, o->revoked_host_keys); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2476 | dump_cfg_string(oXAuthLocation, o->xauth_location); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2477 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2478 | /* Forwards */ |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2479 | dump_cfg_forwards(oDynamicForward, o->num_local_forwards, o->local_forwards); |
| 2480 | dump_cfg_forwards(oLocalForward, o->num_local_forwards, o->local_forwards); |
| 2481 | dump_cfg_forwards(oRemoteForward, o->num_remote_forwards, o->remote_forwards); |
| 2482 | |
| 2483 | /* String array options */ |
| 2484 | dump_cfg_strarray(oIdentityFile, o->num_identity_files, o->identity_files); |
| 2485 | dump_cfg_strarray_oneline(oCanonicalDomains, o->num_canonical_domains, o->canonical_domains); |
| 2486 | dump_cfg_strarray_oneline(oGlobalKnownHostsFile, o->num_system_hostfiles, o->system_hostfiles); |
| 2487 | dump_cfg_strarray_oneline(oUserKnownHostsFile, o->num_user_hostfiles, o->user_hostfiles); |
| 2488 | dump_cfg_strarray(oSendEnv, o->num_send_env, o->send_env); |
| 2489 | |
| 2490 | /* Special cases */ |
| 2491 | |
| 2492 | /* oConnectTimeout */ |
| 2493 | if (o->connection_timeout == -1) |
| 2494 | printf("connecttimeout none\n"); |
| 2495 | else |
| 2496 | dump_cfg_int(oConnectTimeout, o->connection_timeout); |
| 2497 | |
| 2498 | /* oTunnelDevice */ |
| 2499 | printf("tunneldevice"); |
| 2500 | if (o->tun_local == SSH_TUNID_ANY) |
| 2501 | printf(" any"); |
| 2502 | else |
| 2503 | printf(" %d", o->tun_local); |
| 2504 | if (o->tun_remote == SSH_TUNID_ANY) |
| 2505 | printf(":any"); |
| 2506 | else |
| 2507 | printf(":%d", o->tun_remote); |
| 2508 | printf("\n"); |
| 2509 | |
| 2510 | /* oCanonicalizePermittedCNAMEs */ |
| 2511 | if ( o->num_permitted_cnames > 0) { |
| 2512 | printf("canonicalizePermittedcnames"); |
| 2513 | for (i = 0; i < o->num_permitted_cnames; i++) { |
| 2514 | printf(" %s:%s", o->permitted_cnames[i].source_list, |
| 2515 | o->permitted_cnames[i].target_list); |
| 2516 | } |
| 2517 | printf("\n"); |
| 2518 | } |
| 2519 | |
| 2520 | /* oCipher */ |
| 2521 | if (o->cipher != SSH_CIPHER_NOT_SET) |
| 2522 | printf("Cipher %s\n", cipher_name(o->cipher)); |
| 2523 | |
| 2524 | /* oControlPersist */ |
| 2525 | if (o->control_persist == 0 || o->control_persist_timeout == 0) |
| 2526 | dump_cfg_fmtint(oControlPersist, o->control_persist); |
| 2527 | else |
| 2528 | dump_cfg_int(oControlPersist, o->control_persist_timeout); |
| 2529 | |
| 2530 | /* oEscapeChar */ |
| 2531 | if (o->escape_char == SSH_ESCAPECHAR_NONE) |
| 2532 | printf("escapechar none\n"); |
| 2533 | else { |
| 2534 | vis(vbuf, o->escape_char, VIS_WHITE, 0); |
| 2535 | printf("escapechar %s\n", vbuf); |
| 2536 | } |
| 2537 | |
| 2538 | /* oIPQoS */ |
| 2539 | printf("ipqos %s ", iptos2str(o->ip_qos_interactive)); |
| 2540 | printf("%s\n", iptos2str(o->ip_qos_bulk)); |
| 2541 | |
| 2542 | /* oRekeyLimit */ |
dtucker@openbsd.org | 921ff00 | 2016-01-29 02:54:45 +0000 | [diff] [blame] | 2543 | printf("rekeylimit %llu %d\n", |
| 2544 | (unsigned long long)o->rekey_limit, o->rekey_interval); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 2545 | |
| 2546 | /* oStreamLocalBindMask */ |
| 2547 | printf("streamlocalbindmask 0%o\n", |
| 2548 | o->fwd_opts.streamlocal_bind_mask); |
| 2549 | } |