Damien Miller | 13f97b2 | 2014-02-24 15:57:55 +1100 | [diff] [blame^] | 1 | /* $OpenBSD: readconf.c,v 1.218 2014/02/23 20:11:36 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 | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 21 | |
| 22 | #include <netinet/in.h> |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 23 | #include <netinet/in_systm.h> |
| 24 | #include <netinet/ip.h> |
Darren Tucker | 0eeafcd | 2014-01-31 14:18:51 +1100 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 27 | #include <ctype.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 28 | #include <errno.h> |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 29 | #include <fcntl.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 30 | #include <netdb.h> |
Darren Tucker | a335766 | 2014-01-18 00:03:57 +1100 | [diff] [blame] | 31 | #ifdef HAVE_PATHS_H |
| 32 | # include <paths.h> |
| 33 | #endif |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 34 | #include <pwd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 36 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 37 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 38 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 39 | #include <unistd.h> |
Darren Tucker | e194ba4 | 2013-05-16 20:47:31 +1000 | [diff] [blame] | 40 | #ifdef HAVE_UTIL_H |
Darren Tucker | b7ee852 | 2013-05-16 20:33:10 +1000 | [diff] [blame] | 41 | #include <util.h> |
Darren Tucker | e194ba4 | 2013-05-16 20:47:31 +1000 | [diff] [blame] | 42 | #endif |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 43 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 45 | #include "ssh.h" |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 46 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 47 | #include "cipher.h" |
| 48 | #include "pathnames.h" |
| 49 | #include "log.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 50 | #include "key.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 51 | #include "readconf.h" |
| 52 | #include "match.h" |
| 53 | #include "misc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 54 | #include "buffer.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 55 | #include "kex.h" |
| 56 | #include "mac.h" |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 57 | #include "uidswap.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 58 | |
| 59 | /* Format of the configuration file: |
| 60 | |
| 61 | # Configuration data is parsed as follows: |
| 62 | # 1. command line options |
| 63 | # 2. user-specific file |
| 64 | # 3. system-wide file |
| 65 | # Any configuration value is only changed the first time it is set. |
| 66 | # Thus, host-specific definitions should be at the beginning of the |
| 67 | # configuration file, and defaults at the end. |
| 68 | |
| 69 | # Host-specific declarations. These may override anything above. A single |
| 70 | # host may match multiple declarations; these are processed in the order |
| 71 | # that they are given in. |
| 72 | |
| 73 | Host *.ngs.fi ngs.fi |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 74 | User foo |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | |
| 76 | Host fake.com |
| 77 | HostName another.host.name.real.org |
| 78 | User blaah |
| 79 | Port 34289 |
| 80 | ForwardX11 no |
| 81 | ForwardAgent no |
| 82 | |
| 83 | Host books.com |
| 84 | RemoteForward 9999 shadows.cs.hut.fi:9999 |
| 85 | Cipher 3des |
| 86 | |
| 87 | Host fascist.blob.com |
| 88 | Port 23123 |
| 89 | User tylonen |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 90 | PasswordAuthentication no |
| 91 | |
| 92 | Host puukko.hut.fi |
| 93 | User t35124p |
| 94 | ProxyCommand ssh-proxy %h %p |
| 95 | |
| 96 | Host *.fr |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 97 | PublicKeyAuthentication no |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 98 | |
| 99 | Host *.su |
| 100 | Cipher none |
| 101 | PasswordAuthentication no |
| 102 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 103 | Host vpn.fake.com |
| 104 | Tunnel yes |
| 105 | TunnelDevice 3 |
| 106 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | # Defaults for various options |
| 108 | Host * |
| 109 | ForwardAgent no |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 110 | ForwardX11 no |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | PasswordAuthentication yes |
| 112 | RSAAuthentication yes |
| 113 | RhostsRSAAuthentication yes |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 114 | StrictHostKeyChecking yes |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 115 | TcpKeepAlive no |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | IdentityFile ~/.ssh/identity |
| 117 | Port 22 |
| 118 | EscapeChar ~ |
| 119 | |
| 120 | */ |
| 121 | |
| 122 | /* Keyword tokens. */ |
| 123 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | typedef enum { |
| 125 | oBadOption, |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 126 | oHost, oMatch, |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 127 | oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout, |
| 128 | oGatewayPorts, oExitOnForwardFailure, |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 129 | oPasswordAuthentication, oRSAAuthentication, |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 130 | oChallengeResponseAuthentication, oXAuthLocation, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward, |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 132 | oUser, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts, |
| 134 | oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression, |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 135 | oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts, |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 136 | oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 137 | oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication, |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 138 | oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias, |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 139 | oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication, |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 140 | oHostKeyAlgorithms, oBindAddress, oPKCS11Provider, |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 141 | oClearAllForwardings, oNoHostAuthenticationForLocalhost, |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 142 | oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 143 | oAddressFamily, oGssAuthentication, oGssDelegateCreds, |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 144 | oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly, |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 145 | oSendEnv, oControlPath, oControlMaster, oControlPersist, |
| 146 | oHashKnownHosts, |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 147 | oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand, |
Damien Miller | 7cc194f | 2014-02-04 11:12:56 +1100 | [diff] [blame] | 148 | oVisualHostKey, oUseRoaming, |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 149 | oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass, |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 150 | oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots, |
| 151 | oCanonicalizeFallbackLocal, oCanonicalizePermittedCNAMEs, |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 152 | oIgnoredUnknownOption, oDeprecated, oUnsupported |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | } OpCodes; |
| 154 | |
| 155 | /* Textual representations of the tokens. */ |
| 156 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | static struct { |
| 158 | const char *name; |
| 159 | OpCodes opcode; |
| 160 | } keywords[] = { |
| 161 | { "forwardagent", oForwardAgent }, |
| 162 | { "forwardx11", oForwardX11 }, |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 163 | { "forwardx11trusted", oForwardX11Trusted }, |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 164 | { "forwardx11timeout", oForwardX11Timeout }, |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 165 | { "exitonforwardfailure", oExitOnForwardFailure }, |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 166 | { "xauthlocation", oXAuthLocation }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | { "gatewayports", oGatewayPorts }, |
| 168 | { "useprivilegedport", oUsePrivilegedPort }, |
Darren Tucker | ec960f2 | 2003-08-13 20:37:05 +1000 | [diff] [blame] | 169 | { "rhostsauthentication", oDeprecated }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 170 | { "passwordauthentication", oPasswordAuthentication }, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 171 | { "kbdinteractiveauthentication", oKbdInteractiveAuthentication }, |
| 172 | { "kbdinteractivedevices", oKbdInteractiveDevices }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 173 | { "rsaauthentication", oRSAAuthentication }, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 174 | { "pubkeyauthentication", oPubkeyAuthentication }, |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 175 | { "dsaauthentication", oPubkeyAuthentication }, /* alias */ |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 176 | { "rhostsrsaauthentication", oRhostsRSAAuthentication }, |
Ben Lindstrom | d69dab3 | 2001-04-12 23:36:05 +0000 | [diff] [blame] | 177 | { "hostbasedauthentication", oHostbasedAuthentication }, |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 178 | { "challengeresponseauthentication", oChallengeResponseAuthentication }, |
| 179 | { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */ |
| 180 | { "tisauthentication", oChallengeResponseAuthentication }, /* alias */ |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 181 | { "kerberosauthentication", oUnsupported }, |
| 182 | { "kerberostgtpassing", oUnsupported }, |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 183 | { "afstokenpassing", oUnsupported }, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 184 | #if defined(GSSAPI) |
| 185 | { "gssapiauthentication", oGssAuthentication }, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 186 | { "gssapidelegatecredentials", oGssDelegateCreds }, |
| 187 | #else |
| 188 | { "gssapiauthentication", oUnsupported }, |
| 189 | { "gssapidelegatecredentials", oUnsupported }, |
| 190 | #endif |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 191 | { "fallbacktorsh", oDeprecated }, |
| 192 | { "usersh", oDeprecated }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | { "identityfile", oIdentityFile }, |
Damien Miller | 5bc6aae | 2009-01-28 16:27:31 +1100 | [diff] [blame] | 194 | { "identityfile2", oIdentityFile }, /* obsolete */ |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 195 | { "identitiesonly", oIdentitiesOnly }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | { "hostname", oHostName }, |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 197 | { "hostkeyalias", oHostKeyAlias }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | { "proxycommand", oProxyCommand }, |
| 199 | { "port", oPort }, |
| 200 | { "cipher", oCipher }, |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 201 | { "ciphers", oCiphers }, |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 202 | { "macs", oMacs }, |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 203 | { "protocol", oProtocol }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | { "remoteforward", oRemoteForward }, |
| 205 | { "localforward", oLocalForward }, |
| 206 | { "user", oUser }, |
| 207 | { "host", oHost }, |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 208 | { "match", oMatch }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | { "escapechar", oEscapeChar }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | { "globalknownhostsfile", oGlobalKnownHostsFile }, |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 211 | { "globalknownhostsfile2", oDeprecated }, |
Damien Miller | 5bc6aae | 2009-01-28 16:27:31 +1100 | [diff] [blame] | 212 | { "userknownhostsfile", oUserKnownHostsFile }, |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 213 | { "userknownhostsfile2", oDeprecated }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 214 | { "connectionattempts", oConnectionAttempts }, |
| 215 | { "batchmode", oBatchMode }, |
| 216 | { "checkhostip", oCheckHostIP }, |
| 217 | { "stricthostkeychecking", oStrictHostKeyChecking }, |
| 218 | { "compression", oCompression }, |
| 219 | { "compressionlevel", oCompressionLevel }, |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 220 | { "tcpkeepalive", oTCPKeepAlive }, |
| 221 | { "keepalive", oTCPKeepAlive }, /* obsolete */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 222 | { "numberofpasswordprompts", oNumberOfPasswordPrompts }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 223 | { "loglevel", oLogLevel }, |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 224 | { "dynamicforward", oDynamicForward }, |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 225 | { "preferredauthentications", oPreferredAuthentications }, |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 226 | { "hostkeyalgorithms", oHostKeyAlgorithms }, |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 227 | { "bindaddress", oBindAddress }, |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 228 | #ifdef ENABLE_PKCS11 |
| 229 | { "smartcarddevice", oPKCS11Provider }, |
| 230 | { "pkcs11provider", oPKCS11Provider }, |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 231 | #else |
| 232 | { "smartcarddevice", oUnsupported }, |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 233 | { "pkcs11provider", oUnsupported }, |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 234 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 235 | { "clearallforwardings", oClearAllForwardings }, |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 236 | { "enablesshkeysign", oEnableSSHKeysign }, |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 237 | { "verifyhostkeydns", oVerifyHostKeyDNS }, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 238 | { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost }, |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 239 | { "rekeylimit", oRekeyLimit }, |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 240 | { "connecttimeout", oConnectTimeout }, |
Damien Miller | 20a8f97 | 2003-05-18 20:50:30 +1000 | [diff] [blame] | 241 | { "addressfamily", oAddressFamily }, |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 242 | { "serveraliveinterval", oServerAliveInterval }, |
| 243 | { "serveralivecountmax", oServerAliveCountMax }, |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 244 | { "sendenv", oSendEnv }, |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 245 | { "controlpath", oControlPath }, |
| 246 | { "controlmaster", oControlMaster }, |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 247 | { "controlpersist", oControlPersist }, |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 248 | { "hashknownhosts", oHashKnownHosts }, |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 249 | { "tunnel", oTunnel }, |
| 250 | { "tunneldevice", oTunnelDevice }, |
| 251 | { "localcommand", oLocalCommand }, |
| 252 | { "permitlocalcommand", oPermitLocalCommand }, |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 253 | { "visualhostkey", oVisualHostKey }, |
Darren Tucker | 71e4d54 | 2009-07-06 07:12:27 +1000 | [diff] [blame] | 254 | { "useroaming", oUseRoaming }, |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 255 | { "kexalgorithms", oKexAlgorithms }, |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 256 | { "ipqos", oIPQoS }, |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 257 | { "requesttty", oRequestTTY }, |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 258 | { "proxyusefdpass", oProxyUseFdpass }, |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 259 | { "canonicaldomains", oCanonicalDomains }, |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 260 | { "canonicalizefallbacklocal", oCanonicalizeFallbackLocal }, |
| 261 | { "canonicalizehostname", oCanonicalizeHostname }, |
| 262 | { "canonicalizemaxdots", oCanonicalizeMaxDots }, |
| 263 | { "canonicalizepermittedcnames", oCanonicalizePermittedCNAMEs }, |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 264 | { "ignoreunknown", oIgnoreUnknown }, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 265 | |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 266 | { NULL, oBadOption } |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 267 | }; |
| 268 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 269 | /* |
| 270 | * Adds a local TCP/IP port forward to options. Never returns if there is an |
| 271 | * error. |
| 272 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 273 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 274 | void |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 275 | add_local_forward(Options *options, const Forward *newfwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 277 | Forward *fwd; |
Ben Lindstrom | 99a4e14 | 2002-07-09 14:06:40 +0000 | [diff] [blame] | 278 | #ifndef NO_IPPORT_RESERVED_CONCEPT |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 279 | extern uid_t original_real_uid; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 280 | if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 281 | fatal("Privileged ports can only be forwarded by root."); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 282 | #endif |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 283 | options->local_forwards = xrealloc(options->local_forwards, |
| 284 | options->num_local_forwards + 1, |
| 285 | sizeof(*options->local_forwards)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 286 | fwd = &options->local_forwards[options->num_local_forwards++]; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 287 | |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 288 | fwd->listen_host = newfwd->listen_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 289 | fwd->listen_port = newfwd->listen_port; |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 290 | fwd->connect_host = newfwd->connect_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 291 | fwd->connect_port = newfwd->connect_port; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 292 | } |
| 293 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 294 | /* |
| 295 | * Adds a remote TCP/IP port forward to options. Never returns if there is |
| 296 | * an error. |
| 297 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 298 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 299 | void |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 300 | add_remote_forward(Options *options, const Forward *newfwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 301 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 302 | Forward *fwd; |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 303 | |
| 304 | options->remote_forwards = xrealloc(options->remote_forwards, |
| 305 | options->num_remote_forwards + 1, |
| 306 | sizeof(*options->remote_forwards)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 307 | fwd = &options->remote_forwards[options->num_remote_forwards++]; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 308 | |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 309 | fwd->listen_host = newfwd->listen_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 310 | fwd->listen_port = newfwd->listen_port; |
Damien Miller | 1a0442f | 2008-11-05 16:30:06 +1100 | [diff] [blame] | 311 | fwd->connect_host = newfwd->connect_host; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 312 | fwd->connect_port = newfwd->connect_port; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 313 | fwd->handle = newfwd->handle; |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 314 | fwd->allocated_port = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 315 | } |
| 316 | |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 317 | static void |
| 318 | clear_forwardings(Options *options) |
| 319 | { |
| 320 | int i; |
| 321 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 322 | for (i = 0; i < options->num_local_forwards; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 323 | free(options->local_forwards[i].listen_host); |
| 324 | free(options->local_forwards[i].connect_host); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 325 | } |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 326 | if (options->num_local_forwards > 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 327 | free(options->local_forwards); |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 328 | options->local_forwards = NULL; |
| 329 | } |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 330 | options->num_local_forwards = 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 331 | for (i = 0; i < options->num_remote_forwards; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 332 | free(options->remote_forwards[i].listen_host); |
| 333 | free(options->remote_forwards[i].connect_host); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 334 | } |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 335 | if (options->num_remote_forwards > 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 336 | free(options->remote_forwards); |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 337 | options->remote_forwards = NULL; |
| 338 | } |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 339 | options->num_remote_forwards = 0; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 340 | options->tun_open = SSH_TUNMODE_NO; |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 343 | void |
| 344 | add_identity_file(Options *options, const char *dir, const char *filename, |
| 345 | int userprovided) |
| 346 | { |
| 347 | char *path; |
| 348 | |
| 349 | if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES) |
| 350 | fatal("Too many identity files specified (max %d)", |
| 351 | SSH_MAX_IDENTITY_FILES); |
| 352 | |
| 353 | if (dir == NULL) /* no dir, filename is absolute */ |
| 354 | path = xstrdup(filename); |
| 355 | else |
| 356 | (void)xasprintf(&path, "%.100s%.100s", dir, filename); |
| 357 | |
| 358 | options->identity_file_userprovided[options->num_identity_files] = |
| 359 | userprovided; |
| 360 | options->identity_files[options->num_identity_files++] = path; |
| 361 | } |
| 362 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 363 | int |
| 364 | default_ssh_port(void) |
| 365 | { |
| 366 | static int port; |
| 367 | struct servent *sp; |
| 368 | |
| 369 | if (port == 0) { |
| 370 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); |
| 371 | port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; |
| 372 | } |
| 373 | return port; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Execute a command in a shell. |
| 378 | * Return its exit status or -1 on abnormal exit. |
| 379 | */ |
| 380 | static int |
| 381 | execute_in_shell(const char *cmd) |
| 382 | { |
| 383 | char *shell, *command_string; |
| 384 | pid_t pid; |
| 385 | int devnull, status; |
| 386 | extern uid_t original_real_uid; |
| 387 | |
| 388 | if ((shell = getenv("SHELL")) == NULL) |
| 389 | shell = _PATH_BSHELL; |
| 390 | |
| 391 | /* |
| 392 | * Use "exec" to avoid "sh -c" processes on some platforms |
| 393 | * (e.g. Solaris) |
| 394 | */ |
| 395 | xasprintf(&command_string, "exec %s", cmd); |
| 396 | |
| 397 | /* Need this to redirect subprocess stdin/out */ |
| 398 | if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) |
| 399 | fatal("open(/dev/null): %s", strerror(errno)); |
| 400 | |
| 401 | debug("Executing command: '%.500s'", cmd); |
| 402 | |
| 403 | /* Fork and execute the command. */ |
| 404 | if ((pid = fork()) == 0) { |
| 405 | char *argv[4]; |
| 406 | |
| 407 | /* Child. Permanently give up superuser privileges. */ |
| 408 | permanently_drop_suid(original_real_uid); |
| 409 | |
| 410 | /* Redirect child stdin and stdout. Leave stderr */ |
| 411 | if (dup2(devnull, STDIN_FILENO) == -1) |
| 412 | fatal("dup2: %s", strerror(errno)); |
| 413 | if (dup2(devnull, STDOUT_FILENO) == -1) |
| 414 | fatal("dup2: %s", strerror(errno)); |
| 415 | if (devnull > STDERR_FILENO) |
| 416 | close(devnull); |
| 417 | closefrom(STDERR_FILENO + 1); |
| 418 | |
| 419 | argv[0] = shell; |
| 420 | argv[1] = "-c"; |
| 421 | argv[2] = command_string; |
| 422 | argv[3] = NULL; |
| 423 | |
| 424 | execv(argv[0], argv); |
| 425 | error("Unable to execute '%.100s': %s", cmd, strerror(errno)); |
| 426 | /* Die with signal to make this error apparent to parent. */ |
| 427 | signal(SIGTERM, SIG_DFL); |
| 428 | kill(getpid(), SIGTERM); |
| 429 | _exit(1); |
| 430 | } |
| 431 | /* Parent. */ |
| 432 | if (pid < 0) |
| 433 | fatal("%s: fork: %.100s", __func__, strerror(errno)); |
| 434 | |
| 435 | close(devnull); |
| 436 | free(command_string); |
| 437 | |
| 438 | while (waitpid(pid, &status, 0) == -1) { |
| 439 | if (errno != EINTR && errno != EAGAIN) |
| 440 | fatal("%s: waitpid: %s", __func__, strerror(errno)); |
| 441 | } |
| 442 | if (!WIFEXITED(status)) { |
| 443 | error("command '%.100s' exited abnormally", cmd); |
| 444 | return -1; |
| 445 | } |
| 446 | debug3("command returned status %d", WEXITSTATUS(status)); |
| 447 | return WEXITSTATUS(status); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Parse and execute a Match directive. |
| 452 | */ |
| 453 | static int |
| 454 | match_cfg_line(Options *options, char **condition, struct passwd *pw, |
| 455 | const char *host_arg, const char *filename, int linenum) |
| 456 | { |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 457 | char *arg, *attrib, *cmd, *cp = *condition, *host; |
| 458 | const char *ruser; |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 459 | int r, port, result = 1, attributes = 0; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 460 | size_t len; |
| 461 | char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV]; |
| 462 | |
| 463 | /* |
| 464 | * Configuration is likely to be incomplete at this point so we |
| 465 | * must be prepared to use default values. |
| 466 | */ |
| 467 | port = options->port <= 0 ? default_ssh_port() : options->port; |
| 468 | ruser = options->user == NULL ? pw->pw_name : options->user; |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 469 | if (options->hostname != NULL) { |
Damien Miller | eff5cad | 2013-10-23 16:31:10 +1100 | [diff] [blame] | 470 | /* NB. Please keep in sync with ssh.c:main() */ |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 471 | host = percent_expand(options->hostname, |
| 472 | "h", host_arg, (char *)NULL); |
| 473 | } else |
| 474 | host = xstrdup(host_arg); |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 475 | |
| 476 | debug3("checking match for '%s' host %s", cp, host); |
| 477 | while ((attrib = strdelim(&cp)) && *attrib != '\0') { |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 478 | attributes++; |
| 479 | if (strcasecmp(attrib, "all") == 0) { |
| 480 | if (attributes != 1 || |
| 481 | ((arg = strdelim(&cp)) != NULL && *arg != '\0')) { |
| 482 | error("'all' cannot be combined with other " |
| 483 | "Match attributes"); |
| 484 | result = -1; |
| 485 | goto out; |
| 486 | } |
| 487 | *condition = cp; |
| 488 | result = 1; |
| 489 | goto out; |
| 490 | } |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 491 | if ((arg = strdelim(&cp)) == NULL || *arg == '\0') { |
| 492 | error("Missing Match criteria for %s", attrib); |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 493 | result = -1; |
| 494 | goto out; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 495 | } |
| 496 | len = strlen(arg); |
| 497 | if (strcasecmp(attrib, "host") == 0) { |
| 498 | if (match_hostname(host, arg, len) != 1) |
| 499 | result = 0; |
| 500 | else |
| 501 | debug("%.200s line %d: matched 'Host %.100s' ", |
| 502 | filename, linenum, host); |
| 503 | } else if (strcasecmp(attrib, "originalhost") == 0) { |
| 504 | if (match_hostname(host_arg, arg, len) != 1) |
| 505 | result = 0; |
| 506 | else |
| 507 | debug("%.200s line %d: matched " |
| 508 | "'OriginalHost %.100s' ", |
| 509 | filename, linenum, host_arg); |
| 510 | } else if (strcasecmp(attrib, "user") == 0) { |
| 511 | if (match_pattern_list(ruser, arg, len, 0) != 1) |
| 512 | result = 0; |
| 513 | else |
| 514 | debug("%.200s line %d: matched 'User %.100s' ", |
| 515 | filename, linenum, ruser); |
| 516 | } else if (strcasecmp(attrib, "localuser") == 0) { |
| 517 | if (match_pattern_list(pw->pw_name, arg, len, 0) != 1) |
| 518 | result = 0; |
| 519 | else |
| 520 | debug("%.200s line %d: matched " |
| 521 | "'LocalUser %.100s' ", |
| 522 | filename, linenum, pw->pw_name); |
Damien Miller | 8a04be7 | 2013-10-23 16:29:40 +1100 | [diff] [blame] | 523 | } else if (strcasecmp(attrib, "exec") == 0) { |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 524 | if (gethostname(thishost, sizeof(thishost)) == -1) |
| 525 | fatal("gethostname: %s", strerror(errno)); |
| 526 | strlcpy(shorthost, thishost, sizeof(shorthost)); |
| 527 | shorthost[strcspn(thishost, ".")] = '\0'; |
| 528 | snprintf(portstr, sizeof(portstr), "%d", port); |
| 529 | |
| 530 | cmd = percent_expand(arg, |
| 531 | "L", shorthost, |
| 532 | "d", pw->pw_dir, |
| 533 | "h", host, |
| 534 | "l", thishost, |
| 535 | "n", host_arg, |
| 536 | "p", portstr, |
| 537 | "r", ruser, |
| 538 | "u", pw->pw_name, |
| 539 | (char *)NULL); |
Damien Miller | 0628780 | 2014-02-24 15:56:45 +1100 | [diff] [blame] | 540 | if (result != 1) { |
| 541 | /* skip execution if prior predicate failed */ |
| 542 | debug("%.200s line %d: skipped exec \"%.100s\"", |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 543 | filename, linenum, cmd); |
Damien Miller | 0628780 | 2014-02-24 15:56:45 +1100 | [diff] [blame] | 544 | } else { |
| 545 | r = execute_in_shell(cmd); |
| 546 | if (r == -1) { |
| 547 | fatal("%.200s line %d: match exec " |
| 548 | "'%.100s' error", filename, |
| 549 | linenum, cmd); |
| 550 | } else if (r == 0) { |
| 551 | debug("%.200s line %d: matched " |
| 552 | "'exec \"%.100s\"'", filename, |
| 553 | linenum, cmd); |
| 554 | } else { |
| 555 | debug("%.200s line %d: no match " |
| 556 | "'exec \"%.100s\"'", filename, |
| 557 | linenum, cmd); |
| 558 | result = 0; |
| 559 | } |
| 560 | } |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 561 | free(cmd); |
| 562 | } else { |
| 563 | error("Unsupported Match attribute %s", attrib); |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 564 | result = -1; |
| 565 | goto out; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 566 | } |
| 567 | } |
Damien Miller | cf31f38 | 2013-10-24 21:02:56 +1100 | [diff] [blame] | 568 | if (attributes == 0) { |
| 569 | error("One or more attributes required for Match"); |
| 570 | result = -1; |
| 571 | goto out; |
| 572 | } |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 573 | debug3("match %sfound", result ? "" : "not "); |
| 574 | *condition = cp; |
Damien Miller | 084bcd2 | 2013-10-23 16:30:51 +1100 | [diff] [blame] | 575 | out: |
| 576 | free(host); |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 577 | return result; |
| 578 | } |
| 579 | |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 580 | /* Check and prepare a domain name: removes trailing '.' and lowercases */ |
| 581 | static void |
| 582 | valid_domain(char *name, const char *filename, int linenum) |
| 583 | { |
| 584 | size_t i, l = strlen(name); |
| 585 | u_char c, last = '\0'; |
| 586 | |
| 587 | if (l == 0) |
| 588 | fatal("%s line %d: empty hostname suffix", filename, linenum); |
| 589 | if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0])) |
| 590 | fatal("%s line %d: hostname suffix \"%.100s\" " |
| 591 | "starts with invalid character", filename, linenum, name); |
| 592 | for (i = 0; i < l; i++) { |
| 593 | c = tolower((u_char)name[i]); |
| 594 | name[i] = (char)c; |
| 595 | if (last == '.' && c == '.') |
| 596 | fatal("%s line %d: hostname suffix \"%.100s\" contains " |
| 597 | "consecutive separators", filename, linenum, name); |
| 598 | if (c != '.' && c != '-' && !isalnum(c) && |
| 599 | c != '_') /* technically invalid, but common */ |
| 600 | fatal("%s line %d: hostname suffix \"%.100s\" contains " |
| 601 | "invalid characters", filename, linenum, name); |
| 602 | last = c; |
| 603 | } |
| 604 | if (name[l - 1] == '.') |
| 605 | name[l - 1] = '\0'; |
| 606 | } |
| 607 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 608 | /* |
Ben Lindstrom | 3704c26 | 2001-04-02 18:20:03 +0000 | [diff] [blame] | 609 | * Returns the number of the token pointed to by cp or oBadOption. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 610 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 611 | static OpCodes |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 612 | parse_token(const char *cp, const char *filename, int linenum, |
| 613 | const char *ignored_unknown) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 614 | { |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 615 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 616 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 617 | for (i = 0; keywords[i].name; i++) |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 618 | if (strcmp(cp, keywords[i].name) == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 619 | return keywords[i].opcode; |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 620 | if (ignored_unknown != NULL && match_pattern_list(cp, ignored_unknown, |
| 621 | strlen(ignored_unknown), 1) == 1) |
| 622 | return oIgnoredUnknownOption; |
Ben Lindstrom | b5cdc66 | 2001-04-16 02:13:26 +0000 | [diff] [blame] | 623 | error("%s: line %d: Bad configuration option: %s", |
| 624 | filename, linenum, cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 625 | return oBadOption; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 626 | } |
| 627 | |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 628 | /* Multistate option parsing */ |
| 629 | struct multistate { |
| 630 | char *key; |
| 631 | int value; |
| 632 | }; |
| 633 | static const struct multistate multistate_flag[] = { |
| 634 | { "true", 1 }, |
| 635 | { "false", 0 }, |
| 636 | { "yes", 1 }, |
| 637 | { "no", 0 }, |
| 638 | { NULL, -1 } |
| 639 | }; |
| 640 | static const struct multistate multistate_yesnoask[] = { |
| 641 | { "true", 1 }, |
| 642 | { "false", 0 }, |
| 643 | { "yes", 1 }, |
| 644 | { "no", 0 }, |
| 645 | { "ask", 2 }, |
| 646 | { NULL, -1 } |
| 647 | }; |
| 648 | static const struct multistate multistate_addressfamily[] = { |
| 649 | { "inet", AF_INET }, |
| 650 | { "inet6", AF_INET6 }, |
| 651 | { "any", AF_UNSPEC }, |
| 652 | { NULL, -1 } |
| 653 | }; |
| 654 | static const struct multistate multistate_controlmaster[] = { |
| 655 | { "true", SSHCTL_MASTER_YES }, |
| 656 | { "yes", SSHCTL_MASTER_YES }, |
| 657 | { "false", SSHCTL_MASTER_NO }, |
| 658 | { "no", SSHCTL_MASTER_NO }, |
| 659 | { "auto", SSHCTL_MASTER_AUTO }, |
| 660 | { "ask", SSHCTL_MASTER_ASK }, |
| 661 | { "autoask", SSHCTL_MASTER_AUTO_ASK }, |
| 662 | { NULL, -1 } |
| 663 | }; |
| 664 | static const struct multistate multistate_tunnel[] = { |
| 665 | { "ethernet", SSH_TUNMODE_ETHERNET }, |
| 666 | { "point-to-point", SSH_TUNMODE_POINTOPOINT }, |
| 667 | { "true", SSH_TUNMODE_DEFAULT }, |
| 668 | { "yes", SSH_TUNMODE_DEFAULT }, |
| 669 | { "false", SSH_TUNMODE_NO }, |
| 670 | { "no", SSH_TUNMODE_NO }, |
| 671 | { NULL, -1 } |
| 672 | }; |
| 673 | static const struct multistate multistate_requesttty[] = { |
| 674 | { "true", REQUEST_TTY_YES }, |
| 675 | { "yes", REQUEST_TTY_YES }, |
| 676 | { "false", REQUEST_TTY_NO }, |
| 677 | { "no", REQUEST_TTY_NO }, |
| 678 | { "force", REQUEST_TTY_FORCE }, |
| 679 | { "auto", REQUEST_TTY_AUTO }, |
| 680 | { NULL, -1 } |
| 681 | }; |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 682 | static const struct multistate multistate_canonicalizehostname[] = { |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 683 | { "true", SSH_CANONICALISE_YES }, |
| 684 | { "false", SSH_CANONICALISE_NO }, |
| 685 | { "yes", SSH_CANONICALISE_YES }, |
| 686 | { "no", SSH_CANONICALISE_NO }, |
| 687 | { "always", SSH_CANONICALISE_ALWAYS }, |
| 688 | { NULL, -1 } |
| 689 | }; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 690 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 691 | /* |
| 692 | * Processes a single option line as used in the configuration files. This |
| 693 | * only sets those values that have not already been set. |
| 694 | */ |
Damien Miller | 61f08ac | 2003-02-24 11:56:27 +1100 | [diff] [blame] | 695 | #define WHITESPACE " \t\r\n" |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 696 | int |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 697 | process_config_line(Options *options, struct passwd *pw, const char *host, |
| 698 | char *line, const char *filename, int linenum, int *activep, int userconfig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 699 | { |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 700 | char *s, **charptr, *endofnumber, *keyword, *arg, *arg2; |
| 701 | char **cpptr, fwdarg[256]; |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 702 | u_int i, *uintptr, max_entries = 0; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 703 | int negated, opcode, *intptr, value, value2, cmdline = 0; |
Darren Tucker | 1e44c5d | 2008-01-01 20:32:26 +1100 | [diff] [blame] | 704 | LogLevel *log_level_ptr; |
Darren Tucker | 9113d0c | 2013-05-16 20:48:14 +1000 | [diff] [blame] | 705 | long long val64; |
Damien Miller | 61f08ac | 2003-02-24 11:56:27 +1100 | [diff] [blame] | 706 | size_t len; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 707 | Forward fwd; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 708 | const struct multistate *multistate_ptr; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 709 | struct allowed_cname *cname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 710 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 711 | if (activep == NULL) { /* We are processing a command line directive */ |
| 712 | cmdline = 1; |
| 713 | activep = &cmdline; |
| 714 | } |
| 715 | |
Damien Miller | c652cac | 2003-05-14 13:40:54 +1000 | [diff] [blame] | 716 | /* Strip trailing whitespace */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 717 | for (len = strlen(line) - 1; len > 0; len--) { |
Damien Miller | c652cac | 2003-05-14 13:40:54 +1000 | [diff] [blame] | 718 | if (strchr(WHITESPACE, line[len]) == NULL) |
| 719 | break; |
| 720 | line[len] = '\0'; |
| 721 | } |
| 722 | |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 723 | s = line; |
| 724 | /* Get the keyword. (Each line is supposed to begin with a keyword). */ |
Damien Miller | 928b236 | 2006-03-26 13:53:32 +1100 | [diff] [blame] | 725 | if ((keyword = strdelim(&s)) == NULL) |
| 726 | return 0; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 727 | /* Ignore leading whitespace. */ |
| 728 | if (*keyword == '\0') |
| 729 | keyword = strdelim(&s); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 730 | if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 731 | return 0; |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 732 | /* Match lowercase keyword */ |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 733 | lowercase(keyword); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 734 | |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 735 | opcode = parse_token(keyword, filename, linenum, |
| 736 | options->ignored_unknown); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 737 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 738 | switch (opcode) { |
| 739 | case oBadOption: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 740 | /* don't panic, but count bad options */ |
| 741 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 742 | /* NOTREACHED */ |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 743 | case oIgnoredUnknownOption: |
| 744 | debug("%s line %d: Ignored unknown option \"%s\"", |
| 745 | filename, linenum, keyword); |
| 746 | return 0; |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 747 | case oConnectTimeout: |
| 748 | intptr = &options->connection_timeout; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 749 | parse_time: |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 750 | arg = strdelim(&s); |
| 751 | if (!arg || *arg == '\0') |
| 752 | fatal("%s line %d: missing time value.", |
| 753 | filename, linenum); |
| 754 | if ((value = convtime(arg)) == -1) |
| 755 | fatal("%s line %d: invalid time value.", |
| 756 | filename, linenum); |
Darren Tucker | a52c5b6 | 2007-02-19 22:09:45 +1100 | [diff] [blame] | 757 | if (*activep && *intptr == -1) |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 758 | *intptr = value; |
| 759 | break; |
| 760 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 761 | case oForwardAgent: |
| 762 | intptr = &options->forward_agent; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 763 | parse_flag: |
| 764 | multistate_ptr = multistate_flag; |
| 765 | parse_multistate: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 766 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 767 | if (!arg || *arg == '\0') |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 768 | fatal("%s line %d: missing argument.", |
| 769 | filename, linenum); |
| 770 | value = -1; |
| 771 | for (i = 0; multistate_ptr[i].key != NULL; i++) { |
| 772 | if (strcasecmp(arg, multistate_ptr[i].key) == 0) { |
| 773 | value = multistate_ptr[i].value; |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | if (value == -1) |
| 778 | fatal("%s line %d: unsupported option \"%s\".", |
| 779 | filename, linenum, arg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 780 | if (*activep && *intptr == -1) |
| 781 | *intptr = value; |
| 782 | break; |
| 783 | |
| 784 | case oForwardX11: |
| 785 | intptr = &options->forward_x11; |
| 786 | goto parse_flag; |
| 787 | |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 788 | case oForwardX11Trusted: |
| 789 | intptr = &options->forward_x11_trusted; |
| 790 | goto parse_flag; |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 791 | |
| 792 | case oForwardX11Timeout: |
| 793 | intptr = &options->forward_x11_timeout; |
| 794 | goto parse_time; |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 795 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 796 | case oGatewayPorts: |
| 797 | intptr = &options->gateway_ports; |
| 798 | goto parse_flag; |
| 799 | |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 800 | case oExitOnForwardFailure: |
| 801 | intptr = &options->exit_on_forward_failure; |
| 802 | goto parse_flag; |
| 803 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 804 | case oUsePrivilegedPort: |
| 805 | intptr = &options->use_privileged_port; |
| 806 | goto parse_flag; |
| 807 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 808 | case oPasswordAuthentication: |
| 809 | intptr = &options->password_authentication; |
| 810 | goto parse_flag; |
| 811 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 812 | case oKbdInteractiveAuthentication: |
| 813 | intptr = &options->kbd_interactive_authentication; |
| 814 | goto parse_flag; |
| 815 | |
| 816 | case oKbdInteractiveDevices: |
| 817 | charptr = &options->kbd_interactive_devices; |
| 818 | goto parse_string; |
| 819 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 820 | case oPubkeyAuthentication: |
| 821 | intptr = &options->pubkey_authentication; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 822 | goto parse_flag; |
| 823 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 824 | case oRSAAuthentication: |
| 825 | intptr = &options->rsa_authentication; |
| 826 | goto parse_flag; |
| 827 | |
| 828 | case oRhostsRSAAuthentication: |
| 829 | intptr = &options->rhosts_rsa_authentication; |
| 830 | goto parse_flag; |
| 831 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 832 | case oHostbasedAuthentication: |
| 833 | intptr = &options->hostbased_authentication; |
| 834 | goto parse_flag; |
| 835 | |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 836 | case oChallengeResponseAuthentication: |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 837 | intptr = &options->challenge_response_authentication; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 838 | goto parse_flag; |
Damien Miller | 2aa0ab4 | 2003-05-15 12:05:28 +1000 | [diff] [blame] | 839 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 840 | case oGssAuthentication: |
| 841 | intptr = &options->gss_authentication; |
| 842 | goto parse_flag; |
| 843 | |
| 844 | case oGssDelegateCreds: |
| 845 | intptr = &options->gss_deleg_creds; |
| 846 | goto parse_flag; |
| 847 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 848 | case oBatchMode: |
| 849 | intptr = &options->batch_mode; |
| 850 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 851 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 852 | case oCheckHostIP: |
| 853 | intptr = &options->check_host_ip; |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 854 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 855 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 856 | case oVerifyHostKeyDNS: |
| 857 | intptr = &options->verify_host_key_dns; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 858 | multistate_ptr = multistate_yesnoask; |
| 859 | goto parse_multistate; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 860 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 861 | case oStrictHostKeyChecking: |
| 862 | intptr = &options->strict_host_key_checking; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 863 | multistate_ptr = multistate_yesnoask; |
| 864 | goto parse_multistate; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 865 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 866 | case oCompression: |
| 867 | intptr = &options->compression; |
| 868 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 869 | |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 870 | case oTCPKeepAlive: |
| 871 | intptr = &options->tcp_keep_alive; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 872 | goto parse_flag; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 873 | |
Ben Lindstrom | 3cecc9a | 2001-10-03 17:39:38 +0000 | [diff] [blame] | 874 | case oNoHostAuthenticationForLocalhost: |
| 875 | intptr = &options->no_host_authentication_for_localhost; |
| 876 | goto parse_flag; |
| 877 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 878 | case oNumberOfPasswordPrompts: |
| 879 | intptr = &options->number_of_password_prompts; |
| 880 | goto parse_int; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 881 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 882 | case oCompressionLevel: |
| 883 | intptr = &options->compression_level; |
| 884 | goto parse_int; |
| 885 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 886 | case oRekeyLimit: |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 887 | arg = strdelim(&s); |
| 888 | if (!arg || *arg == '\0') |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 889 | fatal("%.200s line %d: Missing argument.", filename, |
| 890 | linenum); |
| 891 | if (strcmp(arg, "default") == 0) { |
| 892 | val64 = 0; |
| 893 | } else { |
Darren Tucker | b7ee852 | 2013-05-16 20:33:10 +1000 | [diff] [blame] | 894 | if (scan_scaled(arg, &val64) == -1) |
| 895 | fatal("%.200s line %d: Bad number '%s': %s", |
| 896 | filename, linenum, arg, strerror(errno)); |
| 897 | /* check for too-large or too-small limits */ |
| 898 | if (val64 > UINT_MAX) |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 899 | fatal("%.200s line %d: RekeyLimit too large", |
| 900 | filename, linenum); |
| 901 | if (val64 != 0 && val64 < 16) |
| 902 | fatal("%.200s line %d: RekeyLimit too small", |
| 903 | filename, linenum); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 904 | } |
Damien Miller | 3dff176 | 2008-02-10 22:25:52 +1100 | [diff] [blame] | 905 | if (*activep && options->rekey_limit == -1) |
| 906 | options->rekey_limit = (u_int32_t)val64; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 907 | if (s != NULL) { /* optional rekey interval present */ |
| 908 | if (strcmp(s, "none") == 0) { |
| 909 | (void)strdelim(&s); /* discard */ |
| 910 | break; |
| 911 | } |
| 912 | intptr = &options->rekey_interval; |
| 913 | goto parse_time; |
| 914 | } |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 915 | break; |
| 916 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 917 | case oIdentityFile: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 918 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 919 | if (!arg || *arg == '\0') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 920 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
| 921 | if (*activep) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 922 | intptr = &options->num_identity_files; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 923 | if (*intptr >= SSH_MAX_IDENTITY_FILES) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 924 | fatal("%.200s line %d: Too many identity files specified (max %d).", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 925 | filename, linenum, SSH_MAX_IDENTITY_FILES); |
Darren Tucker | aefa368 | 2013-04-05 11:18:35 +1100 | [diff] [blame] | 926 | add_identity_file(options, NULL, arg, userconfig); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 927 | } |
| 928 | break; |
| 929 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 930 | case oXAuthLocation: |
| 931 | charptr=&options->xauth_location; |
| 932 | goto parse_string; |
| 933 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 934 | case oUser: |
| 935 | charptr = &options->user; |
| 936 | parse_string: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 937 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 938 | if (!arg || *arg == '\0') |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 939 | fatal("%.200s line %d: Missing argument.", |
| 940 | filename, linenum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 941 | if (*activep && *charptr == NULL) |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 942 | *charptr = xstrdup(arg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 943 | break; |
| 944 | |
| 945 | case oGlobalKnownHostsFile: |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 946 | cpptr = (char **)&options->system_hostfiles; |
| 947 | uintptr = &options->num_system_hostfiles; |
| 948 | max_entries = SSH_MAX_HOSTS_FILES; |
| 949 | parse_char_array: |
| 950 | if (*activep && *uintptr == 0) { |
| 951 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 952 | if ((*uintptr) >= max_entries) |
| 953 | fatal("%s line %d: " |
| 954 | "too many authorized keys files.", |
| 955 | filename, linenum); |
| 956 | cpptr[(*uintptr)++] = xstrdup(arg); |
| 957 | } |
| 958 | } |
| 959 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 960 | |
| 961 | case oUserKnownHostsFile: |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 962 | cpptr = (char **)&options->user_hostfiles; |
| 963 | uintptr = &options->num_user_hostfiles; |
| 964 | max_entries = SSH_MAX_HOSTS_FILES; |
| 965 | goto parse_char_array; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 966 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 967 | case oHostName: |
| 968 | charptr = &options->hostname; |
| 969 | goto parse_string; |
| 970 | |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 971 | case oHostKeyAlias: |
| 972 | charptr = &options->host_key_alias; |
| 973 | goto parse_string; |
| 974 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 975 | case oPreferredAuthentications: |
| 976 | charptr = &options->preferred_authentications; |
| 977 | goto parse_string; |
| 978 | |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 979 | case oBindAddress: |
| 980 | charptr = &options->bind_address; |
| 981 | goto parse_string; |
| 982 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 983 | case oPKCS11Provider: |
| 984 | charptr = &options->pkcs11_provider; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 985 | goto parse_string; |
Ben Lindstrom | ae996bf | 2001-08-06 21:27:53 +0000 | [diff] [blame] | 986 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 987 | case oProxyCommand: |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 988 | charptr = &options->proxy_command; |
| 989 | parse_command: |
Darren Tucker | a99c1b7 | 2003-06-28 12:40:12 +1000 | [diff] [blame] | 990 | if (s == NULL) |
| 991 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 61f08ac | 2003-02-24 11:56:27 +1100 | [diff] [blame] | 992 | len = strspn(s, WHITESPACE "="); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 993 | if (*activep && *charptr == NULL) |
Damien Miller | 61f08ac | 2003-02-24 11:56:27 +1100 | [diff] [blame] | 994 | *charptr = xstrdup(s + len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 995 | return 0; |
| 996 | |
| 997 | case oPort: |
| 998 | intptr = &options->port; |
| 999 | parse_int: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1000 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1001 | if (!arg || *arg == '\0') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1002 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1003 | if (arg[0] < '0' || arg[0] > '9') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1004 | fatal("%.200s line %d: Bad number.", filename, linenum); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1005 | |
| 1006 | /* Octal, decimal, or hex format? */ |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1007 | value = strtol(arg, &endofnumber, 0); |
| 1008 | if (arg == endofnumber) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1009 | fatal("%.200s line %d: Bad number.", filename, linenum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1010 | if (*activep && *intptr == -1) |
| 1011 | *intptr = value; |
| 1012 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1013 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1014 | case oConnectionAttempts: |
| 1015 | intptr = &options->connection_attempts; |
| 1016 | goto parse_int; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 1017 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1018 | case oCipher: |
| 1019 | intptr = &options->cipher; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1020 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1021 | if (!arg || *arg == '\0') |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1022 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1023 | value = cipher_number(arg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1024 | if (value == -1) |
| 1025 | fatal("%.200s line %d: Bad cipher '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1026 | filename, linenum, arg ? arg : "<NONE>"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1027 | if (*activep && *intptr == -1) |
| 1028 | *intptr = value; |
| 1029 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1030 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1031 | case oCiphers: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1032 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1033 | if (!arg || *arg == '\0') |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1034 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1035 | if (!ciphers_valid(arg)) |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1036 | fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1037 | filename, linenum, arg ? arg : "<NONE>"); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1038 | if (*activep && options->ciphers == NULL) |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1039 | options->ciphers = xstrdup(arg); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1040 | break; |
| 1041 | |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1042 | case oMacs: |
| 1043 | arg = strdelim(&s); |
| 1044 | if (!arg || *arg == '\0') |
| 1045 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
| 1046 | if (!mac_valid(arg)) |
| 1047 | fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1048 | filename, linenum, arg ? arg : "<NONE>"); |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1049 | if (*activep && options->macs == NULL) |
| 1050 | options->macs = xstrdup(arg); |
| 1051 | break; |
| 1052 | |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 1053 | case oKexAlgorithms: |
| 1054 | arg = strdelim(&s); |
| 1055 | if (!arg || *arg == '\0') |
| 1056 | fatal("%.200s line %d: Missing argument.", |
| 1057 | filename, linenum); |
| 1058 | if (!kex_names_valid(arg)) |
| 1059 | fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.", |
| 1060 | filename, linenum, arg ? arg : "<NONE>"); |
| 1061 | if (*activep && options->kex_algorithms == NULL) |
| 1062 | options->kex_algorithms = xstrdup(arg); |
| 1063 | break; |
| 1064 | |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1065 | case oHostKeyAlgorithms: |
| 1066 | arg = strdelim(&s); |
| 1067 | if (!arg || *arg == '\0') |
| 1068 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
| 1069 | if (!key_names_valid2(arg)) |
| 1070 | fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1071 | filename, linenum, arg ? arg : "<NONE>"); |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1072 | if (*activep && options->hostkeyalgorithms == NULL) |
| 1073 | options->hostkeyalgorithms = xstrdup(arg); |
| 1074 | break; |
| 1075 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1076 | case oProtocol: |
| 1077 | intptr = &options->protocol; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1078 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1079 | if (!arg || *arg == '\0') |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1080 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1081 | value = proto_spec(arg); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1082 | if (value == SSH_PROTO_UNKNOWN) |
| 1083 | fatal("%.200s line %d: Bad protocol spec '%s'.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1084 | filename, linenum, arg ? arg : "<NONE>"); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1085 | if (*activep && *intptr == SSH_PROTO_UNKNOWN) |
| 1086 | *intptr = value; |
| 1087 | break; |
| 1088 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1089 | case oLogLevel: |
Darren Tucker | 1e44c5d | 2008-01-01 20:32:26 +1100 | [diff] [blame] | 1090 | log_level_ptr = &options->log_level; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1091 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1092 | value = log_level_number(arg); |
Damien Miller | fcd9320 | 2002-02-05 12:26:34 +1100 | [diff] [blame] | 1093 | if (value == SYSLOG_LEVEL_NOT_SET) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 1094 | fatal("%.200s line %d: unsupported log level '%s'", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1095 | filename, linenum, arg ? arg : "<NONE>"); |
Darren Tucker | 1e44c5d | 2008-01-01 20:32:26 +1100 | [diff] [blame] | 1096 | if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET) |
| 1097 | *log_level_ptr = (LogLevel) value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1098 | break; |
| 1099 | |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1100 | case oLocalForward: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1101 | case oRemoteForward: |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1102 | case oDynamicForward: |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1103 | arg = strdelim(&s); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1104 | if (arg == NULL || *arg == '\0') |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1105 | fatal("%.200s line %d: Missing port argument.", |
| 1106 | filename, linenum); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1107 | |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1108 | if (opcode == oLocalForward || |
| 1109 | opcode == oRemoteForward) { |
| 1110 | arg2 = strdelim(&s); |
| 1111 | if (arg2 == NULL || *arg2 == '\0') |
| 1112 | fatal("%.200s line %d: Missing target argument.", |
| 1113 | filename, linenum); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1114 | |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1115 | /* construct a string for parse_forward */ |
| 1116 | snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2); |
| 1117 | } else if (opcode == oDynamicForward) { |
| 1118 | strlcpy(fwdarg, arg, sizeof(fwdarg)); |
| 1119 | } |
| 1120 | |
| 1121 | if (parse_forward(&fwd, fwdarg, |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 1122 | opcode == oDynamicForward ? 1 : 0, |
| 1123 | opcode == oRemoteForward ? 1 : 0) == 0) |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1124 | fatal("%.200s line %d: Bad forwarding specification.", |
| 1125 | filename, linenum); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1126 | |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1127 | if (*activep) { |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1128 | if (opcode == oLocalForward || |
| 1129 | opcode == oDynamicForward) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1130 | add_local_forward(options, &fwd); |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1131 | else if (opcode == oRemoteForward) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1132 | add_remote_forward(options, &fwd); |
Ben Lindstrom | 62c25a4 | 2001-09-12 18:01:59 +0000 | [diff] [blame] | 1133 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1134 | break; |
| 1135 | |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 1136 | case oClearAllForwardings: |
| 1137 | intptr = &options->clear_forwardings; |
| 1138 | goto parse_flag; |
| 1139 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1140 | case oHost: |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1141 | if (cmdline) |
| 1142 | fatal("Host directive not supported as a command-line " |
| 1143 | "option"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1144 | *activep = 0; |
Damien Miller | fe92421 | 2011-05-15 08:44:45 +1000 | [diff] [blame] | 1145 | arg2 = NULL; |
| 1146 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1147 | negated = *arg == '!'; |
| 1148 | if (negated) |
| 1149 | arg++; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1150 | if (match_pattern(host, arg)) { |
Damien Miller | fe92421 | 2011-05-15 08:44:45 +1000 | [diff] [blame] | 1151 | if (negated) { |
| 1152 | debug("%.200s line %d: Skipping Host " |
| 1153 | "block because of negated match " |
| 1154 | "for %.100s", filename, linenum, |
| 1155 | arg); |
| 1156 | *activep = 0; |
| 1157 | break; |
| 1158 | } |
| 1159 | if (!*activep) |
| 1160 | arg2 = arg; /* logged below */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1161 | *activep = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1162 | } |
Damien Miller | fe92421 | 2011-05-15 08:44:45 +1000 | [diff] [blame] | 1163 | } |
| 1164 | if (*activep) |
| 1165 | debug("%.200s line %d: Applying options for %.100s", |
| 1166 | filename, linenum, arg2); |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1167 | /* Avoid garbage check below, as strdelim is done. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1168 | return 0; |
| 1169 | |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1170 | case oMatch: |
| 1171 | if (cmdline) |
| 1172 | fatal("Host directive not supported as a command-line " |
| 1173 | "option"); |
| 1174 | value = match_cfg_line(options, &s, pw, host, |
| 1175 | filename, linenum); |
| 1176 | if (value < 0) |
| 1177 | fatal("%.200s line %d: Bad Match condition", filename, |
| 1178 | linenum); |
| 1179 | *activep = value; |
| 1180 | break; |
| 1181 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1182 | case oEscapeChar: |
| 1183 | intptr = &options->escape_char; |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1184 | arg = strdelim(&s); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1185 | if (!arg || *arg == '\0') |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1186 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1187 | if (arg[0] == '^' && arg[2] == 0 && |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1188 | (u_char) arg[1] >= 64 && (u_char) arg[1] < 128) |
| 1189 | value = (u_char) arg[1] & 31; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1190 | else if (strlen(arg) == 1) |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1191 | value = (u_char) arg[0]; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1192 | else if (strcmp(arg, "none") == 0) |
Ben Lindstrom | 2b1f71b | 2001-06-05 20:32:21 +0000 | [diff] [blame] | 1193 | value = SSH_ESCAPECHAR_NONE; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1194 | else { |
| 1195 | fatal("%.200s line %d: Bad escape character.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1196 | filename, linenum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1197 | /* NOTREACHED */ |
| 1198 | value = 0; /* Avoid compiler warning. */ |
| 1199 | } |
| 1200 | if (*activep && *intptr == -1) |
| 1201 | *intptr = value; |
| 1202 | break; |
| 1203 | |
Damien Miller | 20a8f97 | 2003-05-18 20:50:30 +1000 | [diff] [blame] | 1204 | case oAddressFamily: |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 1205 | intptr = &options->address_family; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1206 | multistate_ptr = multistate_addressfamily; |
| 1207 | goto parse_multistate; |
Damien Miller | 20a8f97 | 2003-05-18 20:50:30 +1000 | [diff] [blame] | 1208 | |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 1209 | case oEnableSSHKeysign: |
| 1210 | intptr = &options->enable_ssh_keysign; |
| 1211 | goto parse_flag; |
| 1212 | |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1213 | case oIdentitiesOnly: |
| 1214 | intptr = &options->identities_only; |
| 1215 | goto parse_flag; |
| 1216 | |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1217 | case oServerAliveInterval: |
| 1218 | intptr = &options->server_alive_interval; |
| 1219 | goto parse_time; |
| 1220 | |
| 1221 | case oServerAliveCountMax: |
| 1222 | intptr = &options->server_alive_count_max; |
| 1223 | goto parse_int; |
| 1224 | |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1225 | case oSendEnv: |
| 1226 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1227 | if (strchr(arg, '=') != NULL) |
| 1228 | fatal("%s line %d: Invalid environment name.", |
| 1229 | filename, linenum); |
Damien Miller | f8e7acc | 2005-03-05 11:22:50 +1100 | [diff] [blame] | 1230 | if (!*activep) |
| 1231 | continue; |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1232 | if (options->num_send_env >= MAX_SEND_ENV) |
| 1233 | fatal("%s line %d: too many send env.", |
| 1234 | filename, linenum); |
| 1235 | options->send_env[options->num_send_env++] = |
| 1236 | xstrdup(arg); |
| 1237 | } |
| 1238 | break; |
| 1239 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1240 | case oControlPath: |
| 1241 | charptr = &options->control_path; |
| 1242 | goto parse_string; |
| 1243 | |
| 1244 | case oControlMaster: |
| 1245 | intptr = &options->control_master; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1246 | multistate_ptr = multistate_controlmaster; |
| 1247 | goto parse_multistate; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1248 | |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1249 | case oControlPersist: |
| 1250 | /* no/false/yes/true, or a time spec */ |
| 1251 | intptr = &options->control_persist; |
| 1252 | arg = strdelim(&s); |
| 1253 | if (!arg || *arg == '\0') |
| 1254 | fatal("%.200s line %d: Missing ControlPersist" |
| 1255 | " argument.", filename, linenum); |
| 1256 | value = 0; |
| 1257 | value2 = 0; /* timeout */ |
| 1258 | if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0) |
| 1259 | value = 0; |
| 1260 | else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0) |
| 1261 | value = 1; |
| 1262 | else if ((value2 = convtime(arg)) >= 0) |
| 1263 | value = 1; |
| 1264 | else |
| 1265 | fatal("%.200s line %d: Bad ControlPersist argument.", |
| 1266 | filename, linenum); |
| 1267 | if (*activep && *intptr == -1) { |
| 1268 | *intptr = value; |
| 1269 | options->control_persist_timeout = value2; |
| 1270 | } |
| 1271 | break; |
| 1272 | |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 1273 | case oHashKnownHosts: |
| 1274 | intptr = &options->hash_known_hosts; |
| 1275 | goto parse_flag; |
| 1276 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1277 | case oTunnel: |
| 1278 | intptr = &options->tun_open; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1279 | multistate_ptr = multistate_tunnel; |
| 1280 | goto parse_multistate; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1281 | |
| 1282 | case oTunnelDevice: |
| 1283 | arg = strdelim(&s); |
| 1284 | if (!arg || *arg == '\0') |
| 1285 | fatal("%.200s line %d: Missing argument.", filename, linenum); |
| 1286 | value = a2tun(arg, &value2); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1287 | if (value == SSH_TUNID_ERR) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1288 | fatal("%.200s line %d: Bad tun device.", filename, linenum); |
| 1289 | if (*activep) { |
| 1290 | options->tun_local = value; |
| 1291 | options->tun_remote = value2; |
| 1292 | } |
| 1293 | break; |
| 1294 | |
| 1295 | case oLocalCommand: |
| 1296 | charptr = &options->local_command; |
| 1297 | goto parse_command; |
| 1298 | |
| 1299 | case oPermitLocalCommand: |
| 1300 | intptr = &options->permit_local_command; |
| 1301 | goto parse_flag; |
| 1302 | |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1303 | case oVisualHostKey: |
| 1304 | intptr = &options->visual_host_key; |
| 1305 | goto parse_flag; |
| 1306 | |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1307 | case oIPQoS: |
| 1308 | arg = strdelim(&s); |
| 1309 | if ((value = parse_ipqos(arg)) == -1) |
| 1310 | fatal("%s line %d: Bad IPQoS value: %s", |
| 1311 | filename, linenum, arg); |
| 1312 | arg = strdelim(&s); |
| 1313 | if (arg == NULL) |
| 1314 | value2 = value; |
| 1315 | else if ((value2 = parse_ipqos(arg)) == -1) |
| 1316 | fatal("%s line %d: Bad IPQoS value: %s", |
| 1317 | filename, linenum, arg); |
| 1318 | if (*activep) { |
| 1319 | options->ip_qos_interactive = value; |
| 1320 | options->ip_qos_bulk = value2; |
| 1321 | } |
| 1322 | break; |
| 1323 | |
Darren Tucker | 71e4d54 | 2009-07-06 07:12:27 +1000 | [diff] [blame] | 1324 | case oUseRoaming: |
| 1325 | intptr = &options->use_roaming; |
| 1326 | goto parse_flag; |
| 1327 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1328 | case oRequestTTY: |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1329 | intptr = &options->request_tty; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1330 | multistate_ptr = multistate_requesttty; |
| 1331 | goto parse_multistate; |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1332 | |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 1333 | case oIgnoreUnknown: |
| 1334 | charptr = &options->ignored_unknown; |
| 1335 | goto parse_string; |
| 1336 | |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 1337 | case oProxyUseFdpass: |
| 1338 | intptr = &options->proxy_use_fdpass; |
| 1339 | goto parse_flag; |
| 1340 | |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1341 | case oCanonicalDomains: |
| 1342 | value = options->num_canonical_domains != 0; |
| 1343 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1344 | valid_domain(arg, filename, linenum); |
| 1345 | if (!*activep || value) |
| 1346 | continue; |
| 1347 | if (options->num_canonical_domains >= MAX_CANON_DOMAINS) |
| 1348 | fatal("%s line %d: too many hostname suffixes.", |
| 1349 | filename, linenum); |
| 1350 | options->canonical_domains[ |
| 1351 | options->num_canonical_domains++] = xstrdup(arg); |
| 1352 | } |
| 1353 | break; |
| 1354 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1355 | case oCanonicalizePermittedCNAMEs: |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1356 | value = options->num_permitted_cnames != 0; |
| 1357 | while ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
| 1358 | /* Either '*' for everything or 'list:list' */ |
| 1359 | if (strcmp(arg, "*") == 0) |
| 1360 | arg2 = arg; |
| 1361 | else { |
| 1362 | lowercase(arg); |
| 1363 | if ((arg2 = strchr(arg, ':')) == NULL || |
| 1364 | arg2[1] == '\0') { |
| 1365 | fatal("%s line %d: " |
| 1366 | "Invalid permitted CNAME \"%s\"", |
| 1367 | filename, linenum, arg); |
| 1368 | } |
| 1369 | *arg2 = '\0'; |
| 1370 | arg2++; |
| 1371 | } |
| 1372 | if (!*activep || value) |
| 1373 | continue; |
| 1374 | if (options->num_permitted_cnames >= MAX_CANON_DOMAINS) |
| 1375 | fatal("%s line %d: too many permitted CNAMEs.", |
| 1376 | filename, linenum); |
| 1377 | cname = options->permitted_cnames + |
| 1378 | options->num_permitted_cnames++; |
| 1379 | cname->source_list = xstrdup(arg); |
| 1380 | cname->target_list = xstrdup(arg2); |
| 1381 | } |
| 1382 | break; |
| 1383 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1384 | case oCanonicalizeHostname: |
| 1385 | intptr = &options->canonicalize_hostname; |
| 1386 | multistate_ptr = multistate_canonicalizehostname; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1387 | goto parse_multistate; |
| 1388 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1389 | case oCanonicalizeMaxDots: |
| 1390 | intptr = &options->canonicalize_max_dots; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1391 | goto parse_int; |
| 1392 | |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1393 | case oCanonicalizeFallbackLocal: |
| 1394 | intptr = &options->canonicalize_fallback_local; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1395 | goto parse_flag; |
| 1396 | |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 1397 | case oDeprecated: |
Ben Lindstrom | 2e17b08 | 2002-06-09 20:13:27 +0000 | [diff] [blame] | 1398 | debug("%s line %d: Deprecated option \"%s\"", |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 1399 | filename, linenum, keyword); |
Ben Lindstrom | 2e17b08 | 2002-06-09 20:13:27 +0000 | [diff] [blame] | 1400 | return 0; |
Ben Lindstrom | 4daea86 | 2002-06-09 20:04:02 +0000 | [diff] [blame] | 1401 | |
Damien Miller | f9b3feb | 2003-05-16 11:38:32 +1000 | [diff] [blame] | 1402 | case oUnsupported: |
| 1403 | error("%s line %d: Unsupported option \"%s\"", |
| 1404 | filename, linenum, keyword); |
| 1405 | return 0; |
| 1406 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1407 | default: |
| 1408 | fatal("process_config_line: Unimplemented opcode %d", opcode); |
| 1409 | } |
| 1410 | |
| 1411 | /* Check that there is no garbage at end of line. */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1412 | if ((arg = strdelim(&s)) != NULL && *arg != '\0') { |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1413 | fatal("%.200s line %d: garbage at end of line; \"%.200s\".", |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 1414 | filename, linenum, arg); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 1415 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1416 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1420 | /* |
| 1421 | * Reads the config file and modifies the options accordingly. Options |
| 1422 | * should already be initialized before this call. This never returns if |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1423 | * 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] | 1424 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1425 | |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1426 | int |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1427 | read_config_file(const char *filename, struct passwd *pw, const char *host, |
| 1428 | Options *options, int flags) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1429 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1430 | FILE *f; |
| 1431 | char line[1024]; |
| 1432 | int active, linenum; |
| 1433 | int bad_options = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1434 | |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1435 | if ((f = fopen(filename, "r")) == NULL) |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1436 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1437 | |
Darren Tucker | aefa368 | 2013-04-05 11:18:35 +1100 | [diff] [blame] | 1438 | if (flags & SSHCONF_CHECKPERM) { |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1439 | struct stat sb; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1440 | |
Damien Miller | 3379385 | 2004-06-15 10:27:55 +1000 | [diff] [blame] | 1441 | if (fstat(fileno(f), &sb) == -1) |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1442 | fatal("fstat %s: %s", filename, strerror(errno)); |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1443 | if (((sb.st_uid != 0 && sb.st_uid != getuid()) || |
Damien Miller | 3379385 | 2004-06-15 10:27:55 +1000 | [diff] [blame] | 1444 | (sb.st_mode & 022) != 0)) |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1445 | fatal("Bad owner or permissions on %s", filename); |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 1446 | } |
| 1447 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1448 | debug("Reading configuration data %.200s", filename); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1449 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1450 | /* |
| 1451 | * Mark that we are now processing the options. This flag is turned |
| 1452 | * on/off by Host specifications. |
| 1453 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1454 | active = 1; |
| 1455 | linenum = 0; |
| 1456 | while (fgets(line, sizeof(line), f)) { |
| 1457 | /* Update line number counter. */ |
| 1458 | linenum++; |
Damien Miller | 194fd90 | 2013-10-15 12:13:05 +1100 | [diff] [blame] | 1459 | if (process_config_line(options, pw, host, line, filename, |
| 1460 | linenum, &active, flags & SSHCONF_USERCONF) != 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1461 | bad_options++; |
| 1462 | } |
| 1463 | fclose(f); |
| 1464 | if (bad_options > 0) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 1465 | fatal("%s: terminating, %d bad configuration options", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1466 | filename, bad_options); |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 1467 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1468 | } |
| 1469 | |
Damien Miller | 13f97b2 | 2014-02-24 15:57:55 +1100 | [diff] [blame^] | 1470 | /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */ |
| 1471 | int |
| 1472 | option_clear_or_none(const char *o) |
| 1473 | { |
| 1474 | return o == NULL || strcasecmp(o, "none") == 0; |
| 1475 | } |
| 1476 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1477 | /* |
| 1478 | * Initializes options to special values that indicate that they have not yet |
| 1479 | * been set. Read_config_file will only set options with this value. Options |
| 1480 | * are processed in the following order: command line, user config file, |
| 1481 | * system config file. Last, fill_default_options is called. |
| 1482 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1483 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1484 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1485 | initialize_options(Options * options) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1486 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1487 | memset(options, 'X', sizeof(*options)); |
| 1488 | options->forward_agent = -1; |
| 1489 | options->forward_x11 = -1; |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 1490 | options->forward_x11_trusted = -1; |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1491 | options->forward_x11_timeout = -1; |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 1492 | options->exit_on_forward_failure = -1; |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1493 | options->xauth_location = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1494 | options->gateway_ports = -1; |
| 1495 | options->use_privileged_port = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1496 | options->rsa_authentication = -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1497 | options->pubkey_authentication = -1; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1498 | options->challenge_response_authentication = -1; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1499 | options->gss_authentication = -1; |
| 1500 | options->gss_deleg_creds = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1501 | options->password_authentication = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1502 | options->kbd_interactive_authentication = -1; |
| 1503 | options->kbd_interactive_devices = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1504 | options->rhosts_rsa_authentication = -1; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1505 | options->hostbased_authentication = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1506 | options->batch_mode = -1; |
| 1507 | options->check_host_ip = -1; |
| 1508 | options->strict_host_key_checking = -1; |
| 1509 | options->compression = -1; |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 1510 | options->tcp_keep_alive = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1511 | options->compression_level = -1; |
| 1512 | options->port = -1; |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 1513 | options->address_family = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1514 | options->connection_attempts = -1; |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 1515 | options->connection_timeout = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1516 | options->number_of_password_prompts = -1; |
| 1517 | options->cipher = -1; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1518 | options->ciphers = NULL; |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1519 | options->macs = NULL; |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 1520 | options->kex_algorithms = NULL; |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1521 | options->hostkeyalgorithms = NULL; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1522 | options->protocol = SSH_PROTO_UNKNOWN; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1523 | options->num_identity_files = 0; |
| 1524 | options->hostname = NULL; |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 1525 | options->host_key_alias = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1526 | options->proxy_command = NULL; |
| 1527 | options->user = NULL; |
| 1528 | options->escape_char = -1; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1529 | options->num_system_hostfiles = 0; |
| 1530 | options->num_user_hostfiles = 0; |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 1531 | options->local_forwards = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1532 | options->num_local_forwards = 0; |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 1533 | options->remote_forwards = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1534 | options->num_remote_forwards = 0; |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 1535 | options->clear_forwardings = -1; |
Damien Miller | fcd9320 | 2002-02-05 12:26:34 +1100 | [diff] [blame] | 1536 | options->log_level = SYSLOG_LEVEL_NOT_SET; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1537 | options->preferred_authentications = NULL; |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 1538 | options->bind_address = NULL; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1539 | options->pkcs11_provider = NULL; |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 1540 | options->enable_ssh_keysign = - 1; |
Ben Lindstrom | 3cecc9a | 2001-10-03 17:39:38 +0000 | [diff] [blame] | 1541 | options->no_host_authentication_for_localhost = - 1; |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1542 | options->identities_only = - 1; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1543 | options->rekey_limit = - 1; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1544 | options->rekey_interval = -1; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1545 | options->verify_host_key_dns = -1; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1546 | options->server_alive_interval = -1; |
| 1547 | options->server_alive_count_max = -1; |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1548 | options->num_send_env = 0; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1549 | options->control_path = NULL; |
| 1550 | options->control_master = -1; |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1551 | options->control_persist = -1; |
| 1552 | options->control_persist_timeout = 0; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 1553 | options->hash_known_hosts = -1; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1554 | options->tun_open = -1; |
| 1555 | options->tun_local = -1; |
| 1556 | options->tun_remote = -1; |
| 1557 | options->local_command = NULL; |
| 1558 | options->permit_local_command = -1; |
Darren Tucker | 71e4d54 | 2009-07-06 07:12:27 +1000 | [diff] [blame] | 1559 | options->use_roaming = -1; |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1560 | options->visual_host_key = -1; |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1561 | options->ip_qos_interactive = -1; |
| 1562 | options->ip_qos_bulk = -1; |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1563 | options->request_tty = -1; |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 1564 | options->proxy_use_fdpass = -1; |
Darren Tucker | 0763698 | 2013-05-16 20:30:03 +1000 | [diff] [blame] | 1565 | options->ignored_unknown = NULL; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1566 | options->num_canonical_domains = 0; |
| 1567 | options->num_permitted_cnames = 0; |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1568 | options->canonicalize_max_dots = -1; |
| 1569 | options->canonicalize_fallback_local = -1; |
| 1570 | options->canonicalize_hostname = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1571 | } |
| 1572 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1573 | /* |
Damien Miller | 13f97b2 | 2014-02-24 15:57:55 +1100 | [diff] [blame^] | 1574 | * A petite version of fill_default_options() that just fills the options |
| 1575 | * needed for hostname canonicalization to proceed. |
| 1576 | */ |
| 1577 | void |
| 1578 | fill_default_options_for_canonicalization(Options *options) |
| 1579 | { |
| 1580 | if (options->canonicalize_max_dots == -1) |
| 1581 | options->canonicalize_max_dots = 1; |
| 1582 | if (options->canonicalize_fallback_local == -1) |
| 1583 | options->canonicalize_fallback_local = 1; |
| 1584 | if (options->canonicalize_hostname == -1) |
| 1585 | options->canonicalize_hostname = SSH_CANONICALISE_NO; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1589 | * Called after processing other sources of option data, this fills those |
| 1590 | * options for which no value has been specified with their default values. |
| 1591 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1592 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1593 | fill_default_options(Options * options) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1594 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1595 | if (options->forward_agent == -1) |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1596 | options->forward_agent = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1597 | if (options->forward_x11 == -1) |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 1598 | options->forward_x11 = 0; |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 1599 | if (options->forward_x11_trusted == -1) |
| 1600 | options->forward_x11_trusted = 0; |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1601 | if (options->forward_x11_timeout == -1) |
| 1602 | options->forward_x11_timeout = 1200; |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 1603 | if (options->exit_on_forward_failure == -1) |
| 1604 | options->exit_on_forward_failure = 0; |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1605 | if (options->xauth_location == NULL) |
Ben Lindstrom | 1bf11f6 | 2001-06-09 01:48:01 +0000 | [diff] [blame] | 1606 | options->xauth_location = _PATH_XAUTH; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1607 | if (options->gateway_ports == -1) |
| 1608 | options->gateway_ports = 0; |
| 1609 | if (options->use_privileged_port == -1) |
Ben Lindstrom | cebc858 | 2001-03-08 03:39:10 +0000 | [diff] [blame] | 1610 | options->use_privileged_port = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1611 | if (options->rsa_authentication == -1) |
| 1612 | options->rsa_authentication = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1613 | if (options->pubkey_authentication == -1) |
| 1614 | options->pubkey_authentication = 1; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1615 | if (options->challenge_response_authentication == -1) |
Ben Lindstrom | 0076d75 | 2001-08-06 20:53:26 +0000 | [diff] [blame] | 1616 | options->challenge_response_authentication = 1; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1617 | if (options->gss_authentication == -1) |
Darren Tucker | a044f47 | 2003-10-15 15:52:03 +1000 | [diff] [blame] | 1618 | options->gss_authentication = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1619 | if (options->gss_deleg_creds == -1) |
| 1620 | options->gss_deleg_creds = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1621 | if (options->password_authentication == -1) |
| 1622 | options->password_authentication = 1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1623 | if (options->kbd_interactive_authentication == -1) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 1624 | options->kbd_interactive_authentication = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1625 | if (options->rhosts_rsa_authentication == -1) |
Ben Lindstrom | 2bf8276 | 2002-06-11 15:53:05 +0000 | [diff] [blame] | 1626 | options->rhosts_rsa_authentication = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1627 | if (options->hostbased_authentication == -1) |
| 1628 | options->hostbased_authentication = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1629 | if (options->batch_mode == -1) |
| 1630 | options->batch_mode = 0; |
| 1631 | if (options->check_host_ip == -1) |
| 1632 | options->check_host_ip = 1; |
| 1633 | if (options->strict_host_key_checking == -1) |
| 1634 | options->strict_host_key_checking = 2; /* 2 is default */ |
| 1635 | if (options->compression == -1) |
| 1636 | options->compression = 0; |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 1637 | if (options->tcp_keep_alive == -1) |
| 1638 | options->tcp_keep_alive = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1639 | if (options->compression_level == -1) |
| 1640 | options->compression_level = 6; |
| 1641 | if (options->port == -1) |
| 1642 | options->port = 0; /* Filled in ssh_connect. */ |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 1643 | if (options->address_family == -1) |
| 1644 | options->address_family = AF_UNSPEC; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1645 | if (options->connection_attempts == -1) |
Ben Lindstrom | f9cedb9 | 2001-08-06 21:07:11 +0000 | [diff] [blame] | 1646 | options->connection_attempts = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1647 | if (options->number_of_password_prompts == -1) |
| 1648 | options->number_of_password_prompts = 3; |
| 1649 | /* Selected in ssh_login(). */ |
| 1650 | if (options->cipher == -1) |
| 1651 | options->cipher = SSH_CIPHER_NOT_SET; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1652 | /* options->ciphers, default set in myproposals.h */ |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 1653 | /* options->macs, default set in myproposals.h */ |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 1654 | /* options->kex_algorithms, default set in myproposals.h */ |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1655 | /* options->hostkeyalgorithms, default set in myproposals.h */ |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1656 | if (options->protocol == SSH_PROTO_UNKNOWN) |
Darren Tucker | bad5076 | 2009-10-11 21:51:08 +1100 | [diff] [blame] | 1657 | options->protocol = SSH_PROTO_2; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1658 | if (options->num_identity_files == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1659 | if (options->protocol & SSH_PROTO_1) { |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1660 | add_identity_file(options, "~/", |
| 1661 | _PATH_SSH_CLIENT_IDENTITY, 0); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1662 | } |
| 1663 | if (options->protocol & SSH_PROTO_2) { |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1664 | add_identity_file(options, "~/", |
| 1665 | _PATH_SSH_CLIENT_ID_RSA, 0); |
| 1666 | add_identity_file(options, "~/", |
| 1667 | _PATH_SSH_CLIENT_ID_DSA, 0); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1668 | #ifdef OPENSSL_HAS_ECC |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1669 | add_identity_file(options, "~/", |
| 1670 | _PATH_SSH_CLIENT_ID_ECDSA, 0); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1671 | #endif |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 1672 | add_identity_file(options, "~/", |
| 1673 | _PATH_SSH_CLIENT_ID_ED25519, 0); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1674 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1675 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1676 | if (options->escape_char == -1) |
| 1677 | options->escape_char = '~'; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1678 | if (options->num_system_hostfiles == 0) { |
| 1679 | options->system_hostfiles[options->num_system_hostfiles++] = |
| 1680 | xstrdup(_PATH_SSH_SYSTEM_HOSTFILE); |
| 1681 | options->system_hostfiles[options->num_system_hostfiles++] = |
| 1682 | xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2); |
| 1683 | } |
| 1684 | if (options->num_user_hostfiles == 0) { |
| 1685 | options->user_hostfiles[options->num_user_hostfiles++] = |
| 1686 | xstrdup(_PATH_SSH_USER_HOSTFILE); |
| 1687 | options->user_hostfiles[options->num_user_hostfiles++] = |
| 1688 | xstrdup(_PATH_SSH_USER_HOSTFILE2); |
| 1689 | } |
Damien Miller | fcd9320 | 2002-02-05 12:26:34 +1100 | [diff] [blame] | 1690 | if (options->log_level == SYSLOG_LEVEL_NOT_SET) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 1691 | options->log_level = SYSLOG_LEVEL_INFO; |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 1692 | if (options->clear_forwardings == 1) |
| 1693 | clear_forwardings(options); |
Ben Lindstrom | 3cecc9a | 2001-10-03 17:39:38 +0000 | [diff] [blame] | 1694 | if (options->no_host_authentication_for_localhost == - 1) |
| 1695 | options->no_host_authentication_for_localhost = 0; |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1696 | if (options->identities_only == -1) |
| 1697 | options->identities_only = 0; |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 1698 | if (options->enable_ssh_keysign == -1) |
| 1699 | options->enable_ssh_keysign = 0; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1700 | if (options->rekey_limit == -1) |
| 1701 | options->rekey_limit = 0; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 1702 | if (options->rekey_interval == -1) |
| 1703 | options->rekey_interval = 0; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1704 | if (options->verify_host_key_dns == -1) |
| 1705 | options->verify_host_key_dns = 0; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1706 | if (options->server_alive_interval == -1) |
| 1707 | options->server_alive_interval = 0; |
| 1708 | if (options->server_alive_count_max == -1) |
| 1709 | options->server_alive_count_max = 3; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1710 | if (options->control_master == -1) |
| 1711 | options->control_master = 0; |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1712 | if (options->control_persist == -1) { |
| 1713 | options->control_persist = 0; |
| 1714 | options->control_persist_timeout = 0; |
| 1715 | } |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 1716 | if (options->hash_known_hosts == -1) |
| 1717 | options->hash_known_hosts = 0; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1718 | if (options->tun_open == -1) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1719 | options->tun_open = SSH_TUNMODE_NO; |
| 1720 | if (options->tun_local == -1) |
| 1721 | options->tun_local = SSH_TUNID_ANY; |
| 1722 | if (options->tun_remote == -1) |
| 1723 | options->tun_remote = SSH_TUNID_ANY; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1724 | if (options->permit_local_command == -1) |
| 1725 | options->permit_local_command = 0; |
Darren Tucker | 71e4d54 | 2009-07-06 07:12:27 +1000 | [diff] [blame] | 1726 | if (options->use_roaming == -1) |
| 1727 | options->use_roaming = 1; |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1728 | if (options->visual_host_key == -1) |
| 1729 | options->visual_host_key = 0; |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1730 | if (options->ip_qos_interactive == -1) |
| 1731 | options->ip_qos_interactive = IPTOS_LOWDELAY; |
| 1732 | if (options->ip_qos_bulk == -1) |
| 1733 | options->ip_qos_bulk = IPTOS_THROUGHPUT; |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1734 | if (options->request_tty == -1) |
| 1735 | options->request_tty = REQUEST_TTY_AUTO; |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 1736 | if (options->proxy_use_fdpass == -1) |
| 1737 | options->proxy_use_fdpass = 0; |
Damien Miller | 3850559 | 2013-10-17 11:48:13 +1100 | [diff] [blame] | 1738 | if (options->canonicalize_max_dots == -1) |
| 1739 | options->canonicalize_max_dots = 1; |
| 1740 | if (options->canonicalize_fallback_local == -1) |
| 1741 | options->canonicalize_fallback_local = 1; |
| 1742 | if (options->canonicalize_hostname == -1) |
| 1743 | options->canonicalize_hostname = SSH_CANONICALISE_NO; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1744 | #define CLEAR_ON_NONE(v) \ |
| 1745 | do { \ |
Damien Miller | 13f97b2 | 2014-02-24 15:57:55 +1100 | [diff] [blame^] | 1746 | if (option_clear_or_none(v)) { \ |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1747 | free(v); \ |
| 1748 | v = NULL; \ |
| 1749 | } \ |
| 1750 | } while(0) |
| 1751 | CLEAR_ON_NONE(options->local_command); |
| 1752 | CLEAR_ON_NONE(options->proxy_command); |
| 1753 | CLEAR_ON_NONE(options->control_path); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1754 | /* options->user will be set in the main program if appropriate */ |
| 1755 | /* options->hostname will be set in the main program if appropriate */ |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 1756 | /* options->host_key_alias should not be set by default */ |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1757 | /* options->preferred_authentications will be set in ssh */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1758 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1759 | |
| 1760 | /* |
| 1761 | * parse_forward |
| 1762 | * parses a string containing a port forwarding specification of the form: |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1763 | * dynamicfwd == 0 |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1764 | * [listenhost:]listenport:connecthost:connectport |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1765 | * dynamicfwd == 1 |
| 1766 | * [listenhost:]listenport |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1767 | * returns number of arguments parsed or zero on error |
| 1768 | */ |
| 1769 | int |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 1770 | parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1771 | { |
| 1772 | int i; |
| 1773 | char *p, *cp, *fwdarg[4]; |
| 1774 | |
| 1775 | memset(fwd, '\0', sizeof(*fwd)); |
| 1776 | |
| 1777 | cp = p = xstrdup(fwdspec); |
| 1778 | |
| 1779 | /* skip leading spaces */ |
Damien Miller | fdb2306 | 2013-11-21 13:57:15 +1100 | [diff] [blame] | 1780 | while (isspace((u_char)*cp)) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1781 | cp++; |
| 1782 | |
| 1783 | for (i = 0; i < 4; ++i) |
| 1784 | if ((fwdarg[i] = hpdelim(&cp)) == NULL) |
| 1785 | break; |
| 1786 | |
Damien Miller | f4b3953 | 2008-11-03 19:28:21 +1100 | [diff] [blame] | 1787 | /* Check for trailing garbage */ |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1788 | if (cp != NULL) |
| 1789 | i = 0; /* failure */ |
| 1790 | |
| 1791 | switch (i) { |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1792 | case 1: |
| 1793 | fwd->listen_host = NULL; |
| 1794 | fwd->listen_port = a2port(fwdarg[0]); |
| 1795 | fwd->connect_host = xstrdup("socks"); |
| 1796 | break; |
| 1797 | |
| 1798 | case 2: |
| 1799 | fwd->listen_host = xstrdup(cleanhostname(fwdarg[0])); |
| 1800 | fwd->listen_port = a2port(fwdarg[1]); |
| 1801 | fwd->connect_host = xstrdup("socks"); |
| 1802 | break; |
| 1803 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1804 | case 3: |
| 1805 | fwd->listen_host = NULL; |
| 1806 | fwd->listen_port = a2port(fwdarg[0]); |
| 1807 | fwd->connect_host = xstrdup(cleanhostname(fwdarg[1])); |
| 1808 | fwd->connect_port = a2port(fwdarg[2]); |
| 1809 | break; |
| 1810 | |
| 1811 | case 4: |
| 1812 | fwd->listen_host = xstrdup(cleanhostname(fwdarg[0])); |
| 1813 | fwd->listen_port = a2port(fwdarg[1]); |
| 1814 | fwd->connect_host = xstrdup(cleanhostname(fwdarg[2])); |
| 1815 | fwd->connect_port = a2port(fwdarg[3]); |
| 1816 | break; |
| 1817 | default: |
| 1818 | i = 0; /* failure */ |
| 1819 | } |
| 1820 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1821 | free(p); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1822 | |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1823 | if (dynamicfwd) { |
| 1824 | if (!(i == 1 || i == 2)) |
| 1825 | goto fail_free; |
| 1826 | } else { |
| 1827 | if (!(i == 3 || i == 4)) |
| 1828 | goto fail_free; |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 1829 | if (fwd->connect_port <= 0) |
Damien Miller | a699d95 | 2008-11-03 19:27:34 +1100 | [diff] [blame] | 1830 | goto fail_free; |
| 1831 | } |
| 1832 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 1833 | if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0)) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1834 | goto fail_free; |
| 1835 | |
| 1836 | if (fwd->connect_host != NULL && |
| 1837 | strlen(fwd->connect_host) >= NI_MAXHOST) |
| 1838 | goto fail_free; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 1839 | if (fwd->listen_host != NULL && |
| 1840 | strlen(fwd->listen_host) >= NI_MAXHOST) |
| 1841 | goto fail_free; |
| 1842 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1843 | |
| 1844 | return (i); |
| 1845 | |
| 1846 | fail_free: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1847 | free(fwd->connect_host); |
| 1848 | fwd->connect_host = NULL; |
| 1849 | free(fwd->listen_host); |
| 1850 | fwd->listen_host = NULL; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1851 | return (0); |
| 1852 | } |