Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
| 5 | * Created: Sat Mar 18 22:15:47 1995 ylo |
| 6 | * Code to connect to a remote host, and to perform the client side of the |
| 7 | * login (authentication) dialog. |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 8 | * |
| 9 | * SSH2 support added by Markus Friedl. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 10 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 11 | |
| 12 | #include "includes.h" |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 13 | RCSID("$OpenBSD: sshconnect.c,v 1.69 2000/04/19 07:05:50 deraadt Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 14 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 15 | #include <openssl/bn.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | #include "xmalloc.h" |
| 17 | #include "rsa.h" |
| 18 | #include "ssh.h" |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 19 | #include "buffer.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | #include "packet.h" |
| 21 | #include "authfd.h" |
| 22 | #include "cipher.h" |
| 23 | #include "mpaux.h" |
| 24 | #include "uidswap.h" |
| 25 | #include "compat.h" |
Damien Miller | 6d7b2cd | 1999-11-12 15:19:27 +1100 | [diff] [blame] | 26 | #include "readconf.h" |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 27 | |
| 28 | #include "bufaux.h" |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 29 | #include <openssl/rsa.h> |
| 30 | #include <openssl/dsa.h> |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 31 | |
| 32 | #include "ssh2.h" |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 33 | #include <openssl/md5.h> |
| 34 | #include <openssl/dh.h> |
| 35 | #include <openssl/hmac.h> |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 36 | #include "kex.h" |
| 37 | #include "myproposal.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 38 | #include "key.h" |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 39 | #include "dsa.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 40 | #include "hostfile.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | /* Session id for the current session. */ |
| 43 | unsigned char session_id[16]; |
| 44 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 45 | /* authentications supported by server */ |
| 46 | unsigned int supported_authentications; |
| 47 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 48 | static char *client_version_string = NULL; |
| 49 | static char *server_version_string = NULL; |
| 50 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 51 | extern Options options; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 52 | extern char *__progname; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 53 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 54 | /* |
| 55 | * Connect to the given ssh server using a proxy command. |
| 56 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | int |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 58 | ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid, |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | const char *proxy_command) |
| 60 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 61 | Buffer command; |
| 62 | const char *cp; |
| 63 | char *command_string; |
| 64 | int pin[2], pout[2]; |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 65 | pid_t pid; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 66 | char strport[NI_MAXSERV]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 67 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 68 | /* Convert the port number into a string. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 69 | snprintf(strport, sizeof strport, "%hu", port); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | /* Build the final command string in the buffer by making the |
| 72 | appropriate substitutions to the given proxy command. */ |
| 73 | buffer_init(&command); |
| 74 | for (cp = proxy_command; *cp; cp++) { |
| 75 | if (cp[0] == '%' && cp[1] == '%') { |
| 76 | buffer_append(&command, "%", 1); |
| 77 | cp++; |
| 78 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | if (cp[0] == '%' && cp[1] == 'h') { |
| 81 | buffer_append(&command, host, strlen(host)); |
| 82 | cp++; |
| 83 | continue; |
| 84 | } |
| 85 | if (cp[0] == '%' && cp[1] == 'p') { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 86 | buffer_append(&command, strport, strlen(strport)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 87 | cp++; |
| 88 | continue; |
| 89 | } |
| 90 | buffer_append(&command, cp, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 91 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | buffer_append(&command, "\0", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | /* Get the final command string. */ |
| 95 | command_string = buffer_ptr(&command); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 96 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | /* Create pipes for communicating with the proxy. */ |
| 98 | if (pipe(pin) < 0 || pipe(pout) < 0) |
| 99 | fatal("Could not create pipes to communicate with the proxy: %.100s", |
| 100 | strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 102 | debug("Executing proxy command: %.500s", command_string); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 104 | /* Fork and execute the proxy command. */ |
| 105 | if ((pid = fork()) == 0) { |
| 106 | char *argv[10]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 108 | /* Child. Permanently give up superuser privileges. */ |
| 109 | permanently_set_uid(original_real_uid); |
| 110 | |
| 111 | /* Redirect stdin and stdout. */ |
| 112 | close(pin[1]); |
| 113 | if (pin[0] != 0) { |
| 114 | if (dup2(pin[0], 0) < 0) |
| 115 | perror("dup2 stdin"); |
| 116 | close(pin[0]); |
| 117 | } |
| 118 | close(pout[0]); |
| 119 | if (dup2(pout[1], 1) < 0) |
| 120 | perror("dup2 stdout"); |
| 121 | /* Cannot be 1 because pin allocated two descriptors. */ |
| 122 | close(pout[1]); |
| 123 | |
| 124 | /* Stderr is left as it is so that error messages get |
| 125 | printed on the user's terminal. */ |
| 126 | argv[0] = "/bin/sh"; |
| 127 | argv[1] = "-c"; |
| 128 | argv[2] = command_string; |
| 129 | argv[3] = NULL; |
| 130 | |
| 131 | /* Execute the proxy command. Note that we gave up any |
| 132 | extra privileges above. */ |
| 133 | execv("/bin/sh", argv); |
| 134 | perror("/bin/sh"); |
| 135 | exit(1); |
| 136 | } |
| 137 | /* Parent. */ |
| 138 | if (pid < 0) |
| 139 | fatal("fork failed: %.100s", strerror(errno)); |
| 140 | |
| 141 | /* Close child side of the descriptors. */ |
| 142 | close(pin[0]); |
| 143 | close(pout[1]); |
| 144 | |
| 145 | /* Free the command name. */ |
| 146 | buffer_free(&command); |
| 147 | |
| 148 | /* Set the connection file descriptors. */ |
| 149 | packet_set_connection(pout[0], pin[1]); |
| 150 | |
| 151 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 152 | } |
| 153 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | /* |
| 155 | * Creates a (possibly privileged) socket for use as the ssh connection. |
| 156 | */ |
| 157 | int |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 158 | ssh_create_socket(uid_t original_real_uid, int privileged, int family) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 159 | { |
| 160 | int sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 162 | /* |
| 163 | * If we are running as root and want to connect to a privileged |
| 164 | * port, bind our own socket to a privileged port. |
| 165 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 166 | if (privileged) { |
| 167 | int p = IPPORT_RESERVED - 1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 168 | sock = rresvport_af(&p, family); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | if (sock < 0) |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 170 | error("rresvport: af=%d %.100s", family, strerror(errno)); |
| 171 | else |
| 172 | debug("Allocated local port %d.", p); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 173 | } else { |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 174 | /* |
| 175 | * Just create an ordinary socket on arbitrary port. We use |
| 176 | * the user's uid to create the socket. |
| 177 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 178 | temporarily_use_uid(original_real_uid); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 179 | sock = socket(family, SOCK_STREAM, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 180 | if (sock < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 181 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | restore_uid(); |
| 183 | } |
| 184 | return sock; |
| 185 | } |
| 186 | |
| 187 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 188 | * Opens a TCP/IP connection to the remote server on the given host. |
| 189 | * The address of the remote host will be returned in hostaddr. |
| 190 | * If port is 0, the default port will be used. If anonymous is zero, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 191 | * a privileged port will be allocated to make the connection. |
| 192 | * This requires super-user privileges if anonymous is false. |
| 193 | * Connection_attempts specifies the maximum number of tries (one per |
| 194 | * second). If proxy_command is non-NULL, it specifies the command (with %h |
| 195 | * and %p substituted for host and port, respectively) to use to contact |
| 196 | * the daemon. |
| 197 | */ |
| 198 | int |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 199 | ssh_connect(const char *host, struct sockaddr_storage * hostaddr, |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 200 | u_short port, int connection_attempts, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | int anonymous, uid_t original_real_uid, |
| 202 | const char *proxy_command) |
| 203 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 204 | int sock = -1, attempt; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 205 | struct servent *sp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 206 | struct addrinfo hints, *ai, *aitop; |
| 207 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
| 208 | int gaierr; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | struct linger linger; |
| 210 | |
| 211 | debug("ssh_connect: getuid %d geteuid %d anon %d", |
| 212 | (int) getuid(), (int) geteuid(), anonymous); |
| 213 | |
| 214 | /* Get default port if port has not been set. */ |
| 215 | if (port == 0) { |
| 216 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); |
| 217 | if (sp) |
| 218 | port = ntohs(sp->s_port); |
| 219 | else |
| 220 | port = SSH_DEFAULT_PORT; |
| 221 | } |
| 222 | /* If a proxy command is given, connect using it. */ |
| 223 | if (proxy_command != NULL) |
| 224 | return ssh_proxy_connect(host, port, original_real_uid, proxy_command); |
| 225 | |
| 226 | /* No proxy command. */ |
| 227 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 228 | memset(&hints, 0, sizeof(hints)); |
| 229 | hints.ai_family = IPv4or6; |
| 230 | hints.ai_socktype = SOCK_STREAM; |
| 231 | snprintf(strport, sizeof strport, "%d", port); |
| 232 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) |
| 233 | fatal("%s: %.100s: %s", __progname, host, |
| 234 | gai_strerror(gaierr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 235 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 236 | /* |
| 237 | * Try to connect several times. On some machines, the first time |
| 238 | * will sometimes fail. In general socket code appears to behave |
| 239 | * quite magically on many machines. |
| 240 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 241 | for (attempt = 0; attempt < connection_attempts; attempt++) { |
| 242 | if (attempt > 0) |
| 243 | debug("Trying again..."); |
| 244 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 245 | /* Loop through addresses for this host, and try each one in |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 246 | sequence until the connection succeeds. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 247 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 248 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 249 | continue; |
| 250 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, |
| 251 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 252 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 253 | error("ssh_connect: getnameinfo failed"); |
| 254 | continue; |
| 255 | } |
| 256 | debug("Connecting to %.200s [%.100s] port %s.", |
| 257 | host, ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 258 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 259 | /* Create a socket for connecting. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 260 | sock = ssh_create_socket(original_real_uid, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 261 | !anonymous && geteuid() == 0 && port < IPPORT_RESERVED, |
| 262 | ai->ai_family); |
| 263 | if (sock < 0) |
| 264 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 265 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 266 | /* Connect to the host. We use the user's uid in the |
| 267 | * hope that it will help with tcp_wrappers showing |
| 268 | * the remote uid as root. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 269 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 270 | temporarily_use_uid(original_real_uid); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 271 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) { |
| 272 | /* Successful connection. */ |
| 273 | memcpy(hostaddr, ai->ai_addr, sizeof(*(ai->ai_addr))); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 274 | restore_uid(); |
| 275 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 276 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 277 | debug("connect: %.100s", strerror(errno)); |
| 278 | restore_uid(); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 279 | /* |
| 280 | * Close the failed socket; there appear to |
| 281 | * be some problems when reusing a socket for |
| 282 | * which connect() has already returned an |
| 283 | * error. |
| 284 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 285 | shutdown(sock, SHUT_RDWR); |
| 286 | close(sock); |
| 287 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 288 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 289 | if (ai) |
| 290 | break; /* Successful connection. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 291 | |
| 292 | /* Sleep a moment before retrying. */ |
| 293 | sleep(1); |
| 294 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 295 | |
| 296 | freeaddrinfo(aitop); |
| 297 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 298 | /* Return failure if we didn't get a successful connection. */ |
| 299 | if (attempt >= connection_attempts) |
| 300 | return 0; |
| 301 | |
| 302 | debug("Connection established."); |
| 303 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 304 | /* |
| 305 | * Set socket options. We would like the socket to disappear as soon |
| 306 | * as it has been closed for whatever reason. |
| 307 | */ |
| 308 | /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 309 | linger.l_onoff = 1; |
| 310 | linger.l_linger = 5; |
| 311 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); |
| 312 | |
| 313 | /* Set the connection. */ |
| 314 | packet_set_connection(sock, sock); |
| 315 | |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Checks if the user has an authentication agent, and if so, tries to |
| 321 | * authenticate using the agent. |
| 322 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 323 | int |
| 324 | try_agent_authentication() |
| 325 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 326 | int status, type; |
| 327 | char *comment; |
| 328 | AuthenticationConnection *auth; |
| 329 | unsigned char response[16]; |
| 330 | unsigned int i; |
| 331 | BIGNUM *e, *n, *challenge; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 332 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | /* Get connection to the agent. */ |
| 334 | auth = ssh_get_authentication_connection(); |
| 335 | if (!auth) |
| 336 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 337 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 338 | e = BN_new(); |
| 339 | n = BN_new(); |
| 340 | challenge = BN_new(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 341 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 342 | /* Loop through identities served by the agent. */ |
| 343 | for (status = ssh_get_first_identity(auth, e, n, &comment); |
| 344 | status; |
| 345 | status = ssh_get_next_identity(auth, e, n, &comment)) { |
| 346 | int plen, clen; |
| 347 | |
| 348 | /* Try this identity. */ |
| 349 | debug("Trying RSA authentication via agent with '%.100s'", comment); |
| 350 | xfree(comment); |
| 351 | |
| 352 | /* Tell the server that we are willing to authenticate using this key. */ |
| 353 | packet_start(SSH_CMSG_AUTH_RSA); |
| 354 | packet_put_bignum(n); |
| 355 | packet_send(); |
| 356 | packet_write_wait(); |
| 357 | |
| 358 | /* Wait for server's response. */ |
| 359 | type = packet_read(&plen); |
| 360 | |
| 361 | /* The server sends failure if it doesn\'t like our key or |
| 362 | does not support RSA authentication. */ |
| 363 | if (type == SSH_SMSG_FAILURE) { |
| 364 | debug("Server refused our key."); |
| 365 | continue; |
| 366 | } |
| 367 | /* Otherwise it should have sent a challenge. */ |
| 368 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 369 | packet_disconnect("Protocol error during RSA authentication: %d", |
| 370 | type); |
| 371 | |
| 372 | packet_get_bignum(challenge, &clen); |
| 373 | |
| 374 | packet_integrity_check(plen, clen, type); |
| 375 | |
| 376 | debug("Received RSA challenge from server."); |
| 377 | |
| 378 | /* Ask the agent to decrypt the challenge. */ |
| 379 | if (!ssh_decrypt_challenge(auth, e, n, challenge, |
| 380 | session_id, 1, response)) { |
| 381 | /* The agent failed to authenticate this identifier although it |
| 382 | advertised it supports this. Just return a wrong value. */ |
| 383 | log("Authentication agent failed to decrypt challenge."); |
| 384 | memset(response, 0, sizeof(response)); |
| 385 | } |
| 386 | debug("Sending response to RSA challenge."); |
| 387 | |
| 388 | /* Send the decrypted challenge back to the server. */ |
| 389 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 390 | for (i = 0; i < 16; i++) |
| 391 | packet_put_char(response[i]); |
| 392 | packet_send(); |
| 393 | packet_write_wait(); |
| 394 | |
| 395 | /* Wait for response from the server. */ |
| 396 | type = packet_read(&plen); |
| 397 | |
| 398 | /* The server returns success if it accepted the authentication. */ |
| 399 | if (type == SSH_SMSG_SUCCESS) { |
| 400 | debug("RSA authentication accepted by server."); |
| 401 | BN_clear_free(e); |
| 402 | BN_clear_free(n); |
| 403 | BN_clear_free(challenge); |
| 404 | return 1; |
| 405 | } |
| 406 | /* Otherwise it should return failure. */ |
| 407 | if (type != SSH_SMSG_FAILURE) |
| 408 | packet_disconnect("Protocol error waiting RSA auth response: %d", |
| 409 | type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 410 | } |
| 411 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 412 | BN_clear_free(e); |
| 413 | BN_clear_free(n); |
| 414 | BN_clear_free(challenge); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 415 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 416 | debug("RSA authentication using agent refused."); |
| 417 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 418 | } |
| 419 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 420 | /* |
| 421 | * Computes the proper response to a RSA challenge, and sends the response to |
| 422 | * the server. |
| 423 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 425 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 426 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 427 | unsigned char buf[32], response[16]; |
| 428 | MD5_CTX md; |
| 429 | int i, len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 430 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 431 | /* Decrypt the challenge using the private key. */ |
| 432 | rsa_private_decrypt(challenge, challenge, prv); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 433 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 434 | /* Compute the response. */ |
| 435 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 436 | len = BN_num_bytes(challenge); |
| 437 | if (len <= 0 || len > sizeof(buf)) |
| 438 | packet_disconnect("respond_to_rsa_challenge: bad challenge length %d", |
| 439 | len); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 440 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 441 | memset(buf, 0, sizeof(buf)); |
| 442 | BN_bn2bin(challenge, buf + sizeof(buf) - len); |
| 443 | MD5_Init(&md); |
| 444 | MD5_Update(&md, buf, 32); |
| 445 | MD5_Update(&md, session_id, 16); |
| 446 | MD5_Final(response, &md); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 447 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 448 | debug("Sending response to host key RSA challenge."); |
| 449 | |
| 450 | /* Send the response back to the server. */ |
| 451 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 452 | for (i = 0; i < 16; i++) |
| 453 | packet_put_char(response[i]); |
| 454 | packet_send(); |
| 455 | packet_write_wait(); |
| 456 | |
| 457 | memset(buf, 0, sizeof(buf)); |
| 458 | memset(response, 0, sizeof(response)); |
| 459 | memset(&md, 0, sizeof(md)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 460 | } |
| 461 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 462 | /* |
| 463 | * Checks if the user has authentication file, and if so, tries to authenticate |
| 464 | * the user using it. |
| 465 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 466 | int |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 467 | try_rsa_authentication(const char *authfile) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 468 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 469 | BIGNUM *challenge; |
| 470 | RSA *private_key; |
| 471 | RSA *public_key; |
| 472 | char *passphrase, *comment; |
| 473 | int type, i; |
| 474 | int plen, clen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 475 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 476 | /* Try to load identification for the authentication key. */ |
| 477 | public_key = RSA_new(); |
| 478 | if (!load_public_key(authfile, public_key, &comment)) { |
| 479 | RSA_free(public_key); |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 480 | /* Could not load it. Fail. */ |
| 481 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 482 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 483 | debug("Trying RSA authentication with key '%.100s'", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 484 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 485 | /* Tell the server that we are willing to authenticate using this key. */ |
| 486 | packet_start(SSH_CMSG_AUTH_RSA); |
| 487 | packet_put_bignum(public_key->n); |
| 488 | packet_send(); |
| 489 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 490 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 491 | /* We no longer need the public key. */ |
| 492 | RSA_free(public_key); |
| 493 | |
| 494 | /* Wait for server's response. */ |
| 495 | type = packet_read(&plen); |
| 496 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 497 | /* |
| 498 | * The server responds with failure if it doesn\'t like our key or |
| 499 | * doesn\'t support RSA authentication. |
| 500 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 501 | if (type == SSH_SMSG_FAILURE) { |
| 502 | debug("Server refused our key."); |
| 503 | xfree(comment); |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 504 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 505 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 506 | /* Otherwise, the server should respond with a challenge. */ |
| 507 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 508 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 509 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 510 | /* Get the challenge from the packet. */ |
| 511 | challenge = BN_new(); |
| 512 | packet_get_bignum(challenge, &clen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 513 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 514 | packet_integrity_check(plen, clen, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 515 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 516 | debug("Received RSA challenge from server."); |
| 517 | |
| 518 | private_key = RSA_new(); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 519 | /* |
| 520 | * Load the private key. Try first with empty passphrase; if it |
| 521 | * fails, ask for a passphrase. |
| 522 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 523 | if (!load_private_key(authfile, "", private_key, NULL)) { |
| 524 | char buf[300]; |
| 525 | snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ", |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 526 | comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 527 | if (!options.batch_mode) |
| 528 | passphrase = read_passphrase(buf, 0); |
| 529 | else { |
| 530 | debug("Will not query passphrase for %.100s in batch mode.", |
| 531 | comment); |
| 532 | passphrase = xstrdup(""); |
| 533 | } |
| 534 | |
| 535 | /* Load the authentication file using the pasphrase. */ |
| 536 | if (!load_private_key(authfile, passphrase, private_key, NULL)) { |
| 537 | memset(passphrase, 0, strlen(passphrase)); |
| 538 | xfree(passphrase); |
| 539 | error("Bad passphrase."); |
| 540 | |
| 541 | /* Send a dummy response packet to avoid protocol error. */ |
| 542 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 543 | for (i = 0; i < 16; i++) |
| 544 | packet_put_char(0); |
| 545 | packet_send(); |
| 546 | packet_write_wait(); |
| 547 | |
| 548 | /* Expect the server to reject it... */ |
| 549 | packet_read_expect(&plen, SSH_SMSG_FAILURE); |
| 550 | xfree(comment); |
| 551 | return 0; |
| 552 | } |
| 553 | /* Destroy the passphrase. */ |
| 554 | memset(passphrase, 0, strlen(passphrase)); |
| 555 | xfree(passphrase); |
| 556 | } |
| 557 | /* We no longer need the comment. */ |
| 558 | xfree(comment); |
| 559 | |
| 560 | /* Compute and send a response to the challenge. */ |
| 561 | respond_to_rsa_challenge(challenge, private_key); |
| 562 | |
| 563 | /* Destroy the private key. */ |
| 564 | RSA_free(private_key); |
| 565 | |
| 566 | /* We no longer need the challenge. */ |
| 567 | BN_clear_free(challenge); |
| 568 | |
| 569 | /* Wait for response from the server. */ |
| 570 | type = packet_read(&plen); |
| 571 | if (type == SSH_SMSG_SUCCESS) { |
| 572 | debug("RSA authentication accepted by server."); |
| 573 | return 1; |
| 574 | } |
| 575 | if (type != SSH_SMSG_FAILURE) |
| 576 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 577 | debug("RSA authentication refused."); |
| 578 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 579 | } |
| 580 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 581 | /* |
| 582 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv |
| 583 | * authentication and RSA host authentication. |
| 584 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 585 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 586 | try_rhosts_rsa_authentication(const char *local_user, RSA * host_key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 587 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 588 | int type; |
| 589 | BIGNUM *challenge; |
| 590 | int plen, clen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 591 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 592 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 593 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 594 | /* Tell the server that we are willing to authenticate using this key. */ |
| 595 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); |
| 596 | packet_put_string(local_user, strlen(local_user)); |
| 597 | packet_put_int(BN_num_bits(host_key->n)); |
| 598 | packet_put_bignum(host_key->e); |
| 599 | packet_put_bignum(host_key->n); |
| 600 | packet_send(); |
| 601 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 602 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 603 | /* Wait for server's response. */ |
| 604 | type = packet_read(&plen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 605 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 606 | /* The server responds with failure if it doesn't admit our |
| 607 | .rhosts authentication or doesn't know our host key. */ |
| 608 | if (type == SSH_SMSG_FAILURE) { |
| 609 | debug("Server refused our rhosts authentication or host key."); |
| 610 | return 0; |
| 611 | } |
| 612 | /* Otherwise, the server should respond with a challenge. */ |
| 613 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 614 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 615 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 616 | /* Get the challenge from the packet. */ |
| 617 | challenge = BN_new(); |
| 618 | packet_get_bignum(challenge, &clen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 620 | packet_integrity_check(plen, clen, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 621 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 622 | debug("Received RSA challenge for host key from server."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 623 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 624 | /* Compute a response to the challenge. */ |
| 625 | respond_to_rsa_challenge(challenge, host_key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 626 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 627 | /* We no longer need the challenge. */ |
| 628 | BN_clear_free(challenge); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 629 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 630 | /* Wait for response from the server. */ |
| 631 | type = packet_read(&plen); |
| 632 | if (type == SSH_SMSG_SUCCESS) { |
| 633 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); |
| 634 | return 1; |
| 635 | } |
| 636 | if (type != SSH_SMSG_FAILURE) |
| 637 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 638 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); |
| 639 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | #ifdef KRB4 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 643 | int |
| 644 | try_kerberos_authentication() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 645 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 646 | KTEXT_ST auth; /* Kerberos data */ |
| 647 | char *reply; |
| 648 | char inst[INST_SZ]; |
| 649 | char *realm; |
| 650 | CREDENTIALS cred; |
| 651 | int r, type, plen; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 652 | socklen_t slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 653 | Key_schedule schedule; |
| 654 | u_long checksum, cksum; |
| 655 | MSG_DAT msg_data; |
| 656 | struct sockaddr_in local, foreign; |
| 657 | struct stat st; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 658 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 659 | /* Don't do anything if we don't have any tickets. */ |
| 660 | if (stat(tkt_string(), &st) < 0) |
| 661 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 662 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 663 | strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ); |
| 664 | |
| 665 | realm = (char *) krb_realmofhost(get_canonical_hostname()); |
| 666 | if (!realm) { |
| 667 | debug("Kerberos V4: no realm for %s", get_canonical_hostname()); |
| 668 | return 0; |
| 669 | } |
| 670 | /* This can really be anything. */ |
| 671 | checksum = (u_long) getpid(); |
| 672 | |
| 673 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); |
| 674 | if (r != KSUCCESS) { |
| 675 | debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]); |
| 676 | return 0; |
| 677 | } |
| 678 | /* Get session key to decrypt the server's reply with. */ |
| 679 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); |
| 680 | if (r != KSUCCESS) { |
| 681 | debug("get_cred failed: %s", krb_err_txt[r]); |
| 682 | return 0; |
| 683 | } |
| 684 | des_key_sched((des_cblock *) cred.session, schedule); |
| 685 | |
| 686 | /* Send authentication info to server. */ |
| 687 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
| 688 | packet_put_string((char *) auth.dat, auth.length); |
| 689 | packet_send(); |
| 690 | packet_write_wait(); |
| 691 | |
| 692 | /* Zero the buffer. */ |
| 693 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); |
| 694 | |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 695 | slen = sizeof(local); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 696 | memset(&local, 0, sizeof(local)); |
| 697 | if (getsockname(packet_get_connection_in(), |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 698 | (struct sockaddr *) & local, &slen) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 699 | debug("getsockname failed: %s", strerror(errno)); |
| 700 | |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 701 | slen = sizeof(foreign); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 702 | memset(&foreign, 0, sizeof(foreign)); |
| 703 | if (getpeername(packet_get_connection_in(), |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 704 | (struct sockaddr *) & foreign, &slen) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 705 | debug("getpeername failed: %s", strerror(errno)); |
| 706 | fatal_cleanup(); |
| 707 | } |
| 708 | /* Get server reply. */ |
| 709 | type = packet_read(&plen); |
| 710 | switch (type) { |
| 711 | case SSH_SMSG_FAILURE: |
| 712 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ |
| 713 | debug("Kerberos V4 authentication failed."); |
| 714 | return 0; |
| 715 | break; |
| 716 | |
| 717 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
| 718 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
| 719 | debug("Kerberos V4 authentication accepted."); |
| 720 | |
| 721 | /* Get server's response. */ |
| 722 | reply = packet_get_string((unsigned int *) &auth.length); |
| 723 | memcpy(auth.dat, reply, auth.length); |
| 724 | xfree(reply); |
| 725 | |
| 726 | packet_integrity_check(plen, 4 + auth.length, type); |
| 727 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 728 | /* |
| 729 | * If his response isn't properly encrypted with the session |
| 730 | * key, and the decrypted checksum fails to match, he's |
| 731 | * bogus. Bail out. |
| 732 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 733 | r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session, |
| 734 | &foreign, &local, &msg_data); |
| 735 | if (r != KSUCCESS) { |
| 736 | debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]); |
| 737 | packet_disconnect("Kerberos V4 challenge failed!"); |
| 738 | } |
| 739 | /* Fetch the (incremented) checksum that we supplied in the request. */ |
| 740 | (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum)); |
| 741 | cksum = ntohl(cksum); |
| 742 | |
| 743 | /* If it matches, we're golden. */ |
| 744 | if (cksum == checksum + 1) { |
| 745 | debug("Kerberos V4 challenge successful."); |
| 746 | return 1; |
| 747 | } else |
| 748 | packet_disconnect("Kerberos V4 challenge failed!"); |
| 749 | break; |
| 750 | |
| 751 | default: |
| 752 | packet_disconnect("Protocol error on Kerberos V4 response: %d", type); |
| 753 | } |
| 754 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 755 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 756 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 757 | #endif /* KRB4 */ |
| 758 | |
| 759 | #ifdef AFS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 760 | int |
| 761 | send_kerberos_tgt() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 762 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 763 | CREDENTIALS *creds; |
| 764 | char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ]; |
| 765 | int r, type, plen; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 766 | char buffer[8192]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 767 | struct stat st; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 768 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 769 | /* Don't do anything if we don't have any tickets. */ |
| 770 | if (stat(tkt_string(), &st) < 0) |
| 771 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 772 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 773 | creds = xmalloc(sizeof(*creds)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 774 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 775 | if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) { |
| 776 | debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]); |
| 777 | return 0; |
| 778 | } |
| 779 | if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) { |
| 780 | debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]); |
| 781 | return 0; |
| 782 | } |
| 783 | if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) { |
| 784 | debug("Kerberos V4 ticket expired: %s", TKT_FILE); |
| 785 | return 0; |
| 786 | } |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 787 | creds_to_radix(creds, (unsigned char *)buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 788 | xfree(creds); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 789 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 790 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 791 | packet_put_string(buffer, strlen(buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 792 | packet_send(); |
| 793 | packet_write_wait(); |
| 794 | |
| 795 | type = packet_read(&plen); |
| 796 | |
| 797 | if (type == SSH_SMSG_FAILURE) |
| 798 | debug("Kerberos TGT for realm %s rejected.", prealm); |
| 799 | else if (type != SSH_SMSG_SUCCESS) |
| 800 | packet_disconnect("Protocol error on Kerberos TGT response: %d", type); |
| 801 | |
| 802 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 803 | } |
| 804 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 805 | void |
| 806 | send_afs_tokens(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 807 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 808 | CREDENTIALS creds; |
| 809 | struct ViceIoctl parms; |
| 810 | struct ClearToken ct; |
| 811 | int i, type, len, plen; |
| 812 | char buf[2048], *p, *server_cell; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 813 | char buffer[8192]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 814 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 815 | /* Move over ktc_GetToken, here's something leaner. */ |
| 816 | for (i = 0; i < 100; i++) { /* just in case */ |
| 817 | parms.in = (char *) &i; |
| 818 | parms.in_size = sizeof(i); |
| 819 | parms.out = buf; |
| 820 | parms.out_size = sizeof(buf); |
| 821 | if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) |
| 822 | break; |
| 823 | p = buf; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 824 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 825 | /* Get secret token. */ |
| 826 | memcpy(&creds.ticket_st.length, p, sizeof(unsigned int)); |
| 827 | if (creds.ticket_st.length > MAX_KTXT_LEN) |
| 828 | break; |
| 829 | p += sizeof(unsigned int); |
| 830 | memcpy(creds.ticket_st.dat, p, creds.ticket_st.length); |
| 831 | p += creds.ticket_st.length; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 832 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 833 | /* Get clear token. */ |
| 834 | memcpy(&len, p, sizeof(len)); |
| 835 | if (len != sizeof(struct ClearToken)) |
| 836 | break; |
| 837 | p += sizeof(len); |
| 838 | memcpy(&ct, p, len); |
| 839 | p += len; |
| 840 | p += sizeof(len); /* primary flag */ |
| 841 | server_cell = p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 842 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 843 | /* Flesh out our credentials. */ |
| 844 | strlcpy(creds.service, "afs", sizeof creds.service); |
| 845 | creds.instance[0] = '\0'; |
| 846 | strlcpy(creds.realm, server_cell, REALM_SZ); |
| 847 | memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ); |
| 848 | creds.issue_date = ct.BeginTimestamp; |
| 849 | creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp); |
| 850 | creds.kvno = ct.AuthHandle; |
| 851 | snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId); |
| 852 | creds.pinst[0] = '\0'; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 853 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 854 | /* Encode token, ship it off. */ |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 855 | if (!creds_to_radix(&creds, (unsigned char*) buffer)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 856 | break; |
| 857 | packet_start(SSH_CMSG_HAVE_AFS_TOKEN); |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 858 | packet_put_string(buffer, strlen(buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 859 | packet_send(); |
| 860 | packet_write_wait(); |
| 861 | |
| 862 | /* Roger, Roger. Clearance, Clarence. What's your vector, |
| 863 | Victor? */ |
| 864 | type = packet_read(&plen); |
| 865 | |
| 866 | if (type == SSH_SMSG_FAILURE) |
| 867 | debug("AFS token for cell %s rejected.", server_cell); |
| 868 | else if (type != SSH_SMSG_SUCCESS) |
| 869 | packet_disconnect("Protocol error on AFS token response: %d", type); |
| 870 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 871 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 872 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 873 | #endif /* AFS */ |
| 874 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 875 | /* |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 876 | * Tries to authenticate with any string-based challenge/response system. |
| 877 | * Note that the client code is not tied to s/key or TIS. |
| 878 | */ |
| 879 | int |
| 880 | try_skey_authentication() |
| 881 | { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 882 | int type, i; |
| 883 | int payload_len; |
| 884 | unsigned int clen; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 885 | char *challenge, *response; |
| 886 | |
| 887 | debug("Doing skey authentication."); |
| 888 | |
| 889 | /* request a challenge */ |
| 890 | packet_start(SSH_CMSG_AUTH_TIS); |
| 891 | packet_send(); |
| 892 | packet_write_wait(); |
| 893 | |
| 894 | type = packet_read(&payload_len); |
| 895 | if (type != SSH_SMSG_FAILURE && |
| 896 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
| 897 | packet_disconnect("Protocol error: got %d in response " |
| 898 | "to skey-auth", type); |
| 899 | } |
| 900 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
| 901 | debug("No challenge for skey authentication."); |
| 902 | return 0; |
| 903 | } |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 904 | challenge = packet_get_string(&clen); |
| 905 | packet_integrity_check(payload_len, (4 + clen), type); |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 906 | if (options.cipher == SSH_CIPHER_NONE) |
| 907 | log("WARNING: Encryption is disabled! " |
| 908 | "Reponse will be transmitted in clear text."); |
| 909 | fprintf(stderr, "%s\n", challenge); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 910 | xfree(challenge); |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 911 | fflush(stderr); |
| 912 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 913 | if (i != 0) |
| 914 | error("Permission denied, please try again."); |
| 915 | response = read_passphrase("Response: ", 0); |
| 916 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
| 917 | packet_put_string(response, strlen(response)); |
| 918 | memset(response, 0, strlen(response)); |
| 919 | xfree(response); |
| 920 | packet_send(); |
| 921 | packet_write_wait(); |
| 922 | type = packet_read(&payload_len); |
| 923 | if (type == SSH_SMSG_SUCCESS) |
| 924 | return 1; |
| 925 | if (type != SSH_SMSG_FAILURE) |
| 926 | packet_disconnect("Protocol error: got %d in response " |
| 927 | "to skey-auth-reponse", type); |
| 928 | } |
| 929 | /* failure */ |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * Tries to authenticate with plain passwd authentication. |
| 935 | */ |
| 936 | int |
| 937 | try_password_authentication(char *prompt) |
| 938 | { |
| 939 | int type, i, payload_len; |
| 940 | char *password; |
| 941 | |
| 942 | debug("Doing password authentication."); |
| 943 | if (options.cipher == SSH_CIPHER_NONE) |
| 944 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
| 945 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 946 | if (i != 0) |
| 947 | error("Permission denied, please try again."); |
| 948 | password = read_passphrase(prompt, 0); |
| 949 | packet_start(SSH_CMSG_AUTH_PASSWORD); |
| 950 | packet_put_string(password, strlen(password)); |
| 951 | memset(password, 0, strlen(password)); |
| 952 | xfree(password); |
| 953 | packet_send(); |
| 954 | packet_write_wait(); |
| 955 | |
| 956 | type = packet_read(&payload_len); |
| 957 | if (type == SSH_SMSG_SUCCESS) |
| 958 | return 1; |
| 959 | if (type != SSH_SMSG_FAILURE) |
| 960 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); |
| 961 | } |
| 962 | /* failure */ |
| 963 | return 0; |
| 964 | } |
| 965 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 966 | char * |
| 967 | chop(char *s) |
| 968 | { |
| 969 | char *t = s; |
| 970 | while (*t) { |
| 971 | if(*t == '\n' || *t == '\r') { |
| 972 | *t = '\0'; |
| 973 | return s; |
| 974 | } |
| 975 | t++; |
| 976 | } |
| 977 | return s; |
| 978 | |
| 979 | } |
| 980 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 981 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 982 | * Waits for the server identification string, and sends our own |
| 983 | * identification string. |
| 984 | */ |
| 985 | void |
| 986 | ssh_exchange_identification() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 987 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 988 | char buf[256], remote_version[256]; /* must be same size! */ |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 989 | int remote_major, remote_minor, i, mismatch; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 990 | int connection_in = packet_get_connection_in(); |
| 991 | int connection_out = packet_get_connection_out(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 992 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 993 | /* Read other side\'s version identification. */ |
| 994 | for (i = 0; i < sizeof(buf) - 1; i++) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 995 | int len = read(connection_in, &buf[i], 1); |
| 996 | if (len < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 997 | fatal("ssh_exchange_identification: read: %.100s", strerror(errno)); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 998 | if (len != 1) |
| 999 | fatal("ssh_exchange_identification: Connection closed by remote host"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1000 | if (buf[i] == '\r') { |
| 1001 | buf[i] = '\n'; |
| 1002 | buf[i + 1] = 0; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1003 | continue; /**XXX wait for \n */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1004 | } |
| 1005 | if (buf[i] == '\n') { |
| 1006 | buf[i + 1] = 0; |
| 1007 | break; |
| 1008 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1009 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1010 | buf[sizeof(buf) - 1] = 0; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1011 | server_version_string = xstrdup(buf); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1012 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1013 | /* |
| 1014 | * Check that the versions match. In future this might accept |
| 1015 | * several versions and set appropriate flags to handle them. |
| 1016 | */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1017 | if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n", |
| 1018 | &remote_major, &remote_minor, remote_version) != 3) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1019 | fatal("Bad remote protocol version identification: '%.100s'", buf); |
| 1020 | debug("Remote protocol version %d.%d, remote software version %.100s", |
| 1021 | remote_major, remote_minor, remote_version); |
| 1022 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1023 | compat_datafellows(remote_version); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1024 | mismatch = 0; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1025 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1026 | switch(remote_major) { |
| 1027 | case 1: |
| 1028 | if (remote_minor == 99 && |
| 1029 | (options.protocol & SSH_PROTO_2) && |
| 1030 | !(options.protocol & SSH_PROTO_1_PREFERRED)) { |
| 1031 | enable_compat20(); |
| 1032 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1033 | } |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1034 | if (!(options.protocol & SSH_PROTO_1)) { |
| 1035 | mismatch = 1; |
| 1036 | break; |
| 1037 | } |
| 1038 | if (remote_minor < 3) { |
| 1039 | fatal("Remote machine has too old SSH software version."); |
| 1040 | } else if (remote_minor == 3) { |
| 1041 | /* We speak 1.3, too. */ |
| 1042 | enable_compat13(); |
| 1043 | if (options.forward_agent) { |
| 1044 | log("Agent forwarding disabled for protocol 1.3"); |
| 1045 | options.forward_agent = 0; |
| 1046 | } |
| 1047 | } |
| 1048 | break; |
| 1049 | case 2: |
| 1050 | if (options.protocol & SSH_PROTO_2) { |
| 1051 | enable_compat20(); |
| 1052 | break; |
| 1053 | } |
| 1054 | /* FALLTHROUGH */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1055 | default: |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1056 | mismatch = 1; |
| 1057 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1058 | } |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1059 | if (mismatch) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1060 | fatal("Protocol major versions differ: %d vs. %d", |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1061 | (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, |
| 1062 | remote_major); |
| 1063 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1064 | /* Send our own protocol version identification. */ |
| 1065 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1066 | compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, |
| 1067 | compat20 ? PROTOCOL_MINOR_2 : PROTOCOL_MINOR_1, |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1068 | SSH_VERSION); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1069 | if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1070 | fatal("write: %.100s", strerror(errno)); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1071 | client_version_string = xstrdup(buf); |
| 1072 | chop(client_version_string); |
| 1073 | chop(server_version_string); |
| 1074 | debug("Local version string %.100s", client_version_string); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1075 | } |
| 1076 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1077 | int |
| 1078 | read_yes_or_no(const char *prompt, int defval) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1079 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1080 | char buf[1024]; |
| 1081 | FILE *f; |
| 1082 | int retval = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1083 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1084 | if (isatty(0)) |
| 1085 | f = stdin; |
| 1086 | else |
| 1087 | f = fopen("/dev/tty", "rw"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1088 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1089 | if (f == NULL) |
| 1090 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1091 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1092 | fflush(stdout); |
| 1093 | |
| 1094 | while (1) { |
| 1095 | fprintf(stderr, "%s", prompt); |
| 1096 | if (fgets(buf, sizeof(buf), f) == NULL) { |
| 1097 | /* Print a newline (the prompt probably didn\'t have one). */ |
| 1098 | fprintf(stderr, "\n"); |
| 1099 | strlcpy(buf, "no", sizeof buf); |
| 1100 | } |
| 1101 | /* Remove newline from response. */ |
| 1102 | if (strchr(buf, '\n')) |
| 1103 | *strchr(buf, '\n') = 0; |
| 1104 | |
| 1105 | if (buf[0] == 0) |
| 1106 | retval = defval; |
| 1107 | if (strcmp(buf, "yes") == 0) |
| 1108 | retval = 1; |
| 1109 | if (strcmp(buf, "no") == 0) |
| 1110 | retval = 0; |
| 1111 | |
| 1112 | if (retval != -1) { |
| 1113 | if (f != stdin) |
| 1114 | fclose(f); |
| 1115 | return retval; |
| 1116 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1117 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1118 | } |
| 1119 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1120 | /* |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1121 | * check whether the supplied host key is valid, return only if ok. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1122 | */ |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1123 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1124 | void |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1125 | check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1126 | { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1127 | Key *file_key; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1128 | char *ip = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1129 | char hostline[1000], *hostp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1130 | HostStatus host_status; |
| 1131 | HostStatus ip_status; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1132 | int local = 0, host_ip_differ = 0; |
Damien Miller | eaf9994 | 2000-01-19 13:45:07 +1100 | [diff] [blame] | 1133 | int salen; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1134 | char ntop[NI_MAXHOST]; |
| 1135 | |
| 1136 | /* |
| 1137 | * Force accepting of the host key for loopback/localhost. The |
| 1138 | * problem is that if the home directory is NFS-mounted to multiple |
| 1139 | * machines, localhost will refer to a different machine in each of |
| 1140 | * them, and the user will get bogus HOST_CHANGED warnings. This |
| 1141 | * essentially disables host authentication for localhost; however, |
| 1142 | * this is probably not a real problem. |
| 1143 | */ |
| 1144 | switch (hostaddr->sa_family) { |
| 1145 | case AF_INET: |
| 1146 | local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET; |
Damien Miller | eaf9994 | 2000-01-19 13:45:07 +1100 | [diff] [blame] | 1147 | salen = sizeof(struct sockaddr_in); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1148 | break; |
| 1149 | case AF_INET6: |
| 1150 | local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr)); |
Damien Miller | eaf9994 | 2000-01-19 13:45:07 +1100 | [diff] [blame] | 1151 | salen = sizeof(struct sockaddr_in6); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1152 | break; |
| 1153 | default: |
| 1154 | local = 0; |
Damien Miller | eaf9994 | 2000-01-19 13:45:07 +1100 | [diff] [blame] | 1155 | salen = sizeof(struct sockaddr_storage); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | if (local) { |
| 1159 | debug("Forcing accepting of host key for loopback/localhost."); |
| 1160 | return; |
| 1161 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1162 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1163 | /* |
| 1164 | * Turn off check_host_ip for proxy connects, since |
| 1165 | * we don't have the remote ip-address |
| 1166 | */ |
| 1167 | if (options.proxy_command != NULL && options.check_host_ip) |
| 1168 | options.check_host_ip = 0; |
| 1169 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1170 | if (options.check_host_ip) { |
Damien Miller | eaf9994 | 2000-01-19 13:45:07 +1100 | [diff] [blame] | 1171 | if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop), |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1172 | NULL, 0, NI_NUMERICHOST) != 0) |
| 1173 | fatal("check_host_key: getnameinfo failed"); |
| 1174 | ip = xstrdup(ntop); |
| 1175 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1176 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1177 | /* |
| 1178 | * Store the host key from the known host file in here so that we can |
| 1179 | * compare it with the key for the IP address. |
| 1180 | */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1181 | file_key = key_new(host_key->type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1182 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1183 | /* |
| 1184 | * Check if the host key is present in the user\'s list of known |
| 1185 | * hosts or in the systemwide list. |
| 1186 | */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1187 | host_status = check_host_in_hostfile(options.user_hostfile, host, host_key, file_key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1188 | if (host_status == HOST_NEW) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1189 | host_status = check_host_in_hostfile(options.system_hostfile, host, host_key, file_key); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1190 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1191 | * Also perform check for the ip address, skip the check if we are |
| 1192 | * localhost or the hostname was an ip address to begin with |
| 1193 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1194 | if (options.check_host_ip && !local && strcmp(host, ip)) { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1195 | Key *ip_key = key_new(host_key->type); |
| 1196 | ip_status = check_host_in_hostfile(options.user_hostfile, ip, host_key, ip_key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1197 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1198 | if (ip_status == HOST_NEW) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1199 | ip_status = check_host_in_hostfile(options.system_hostfile, ip, host_key, ip_key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1200 | if (host_status == HOST_CHANGED && |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1201 | (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key))) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1202 | host_ip_differ = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1203 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1204 | key_free(ip_key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1205 | } else |
| 1206 | ip_status = host_status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1207 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1208 | key_free(file_key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1209 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1210 | switch (host_status) { |
| 1211 | case HOST_OK: |
| 1212 | /* The host is known and the key matches. */ |
| 1213 | debug("Host '%.200s' is known and matches the host key.", host); |
| 1214 | if (options.check_host_ip) { |
| 1215 | if (ip_status == HOST_NEW) { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1216 | if (!add_host_to_hostfile(options.user_hostfile, ip, host_key)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1217 | log("Failed to add the host key for IP address '%.30s' to the list of known hosts (%.30s).", |
| 1218 | ip, options.user_hostfile); |
| 1219 | else |
| 1220 | log("Warning: Permanently added host key for IP address '%.30s' to the list of known hosts.", |
| 1221 | ip); |
| 1222 | } else if (ip_status != HOST_OK) |
| 1223 | log("Warning: the host key for '%.200s' differs from the key for the IP address '%.30s'", |
| 1224 | host, ip); |
| 1225 | } |
| 1226 | break; |
| 1227 | case HOST_NEW: |
| 1228 | /* The host is new. */ |
| 1229 | if (options.strict_host_key_checking == 1) { |
| 1230 | /* User has requested strict host key checking. We will not add the host key |
| 1231 | automatically. The only alternative left is to abort. */ |
| 1232 | fatal("No host key is known for %.200s and you have requested strict checking.", host); |
| 1233 | } else if (options.strict_host_key_checking == 2) { |
| 1234 | /* The default */ |
| 1235 | char prompt[1024]; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1236 | char *fp = key_fingerprint(host_key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1237 | snprintf(prompt, sizeof(prompt), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1238 | "The authenticity of host '%.200s' can't be established.\n" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1239 | "Key fingerprint is %s.\n" |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1240 | "Are you sure you want to continue connecting (yes/no)? ", |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1241 | host, fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1242 | if (!read_yes_or_no(prompt, -1)) |
| 1243 | fatal("Aborted by user!\n"); |
| 1244 | } |
| 1245 | if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) { |
| 1246 | snprintf(hostline, sizeof(hostline), "%s,%s", host, ip); |
| 1247 | hostp = hostline; |
| 1248 | } else |
| 1249 | hostp = host; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1250 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1251 | /* If not in strict mode, add the key automatically to the local known_hosts file. */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1252 | if (!add_host_to_hostfile(options.user_hostfile, hostp, host_key)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1253 | log("Failed to add the host to the list of known hosts (%.500s).", |
| 1254 | options.user_hostfile); |
| 1255 | else |
| 1256 | log("Warning: Permanently added '%.200s' to the list of known hosts.", |
| 1257 | hostp); |
| 1258 | break; |
| 1259 | case HOST_CHANGED: |
| 1260 | if (options.check_host_ip && host_ip_differ) { |
| 1261 | char *msg; |
| 1262 | if (ip_status == HOST_NEW) |
| 1263 | msg = "is unknown"; |
| 1264 | else if (ip_status == HOST_OK) |
| 1265 | msg = "is unchanged"; |
| 1266 | else |
| 1267 | msg = "has a different value"; |
| 1268 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1269 | error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @"); |
| 1270 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1271 | error("The host key for %s has changed,", host); |
| 1272 | error("and the key for the according IP address %s", ip); |
| 1273 | error("%s. This could either mean that", msg); |
| 1274 | error("DNS SPOOFING is happening or the IP address for the host"); |
| 1275 | error("and its host key have changed at the same time"); |
| 1276 | } |
| 1277 | /* The host key has changed. */ |
| 1278 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
Damien Miller | f039bad | 1999-12-21 20:57:20 +1100 | [diff] [blame] | 1279 | error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1280 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1281 | error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); |
| 1282 | error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); |
| 1283 | error("It is also possible that the host key has just been changed."); |
| 1284 | error("Please contact your system administrator."); |
| 1285 | error("Add correct host key in %.100s to get rid of this message.", |
| 1286 | options.user_hostfile); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1287 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1288 | /* |
| 1289 | * If strict host key checking is in use, the user will have |
| 1290 | * to edit the key manually and we can only abort. |
| 1291 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1292 | if (options.strict_host_key_checking) |
| 1293 | fatal("Host key for %.200s has changed and you have requested strict checking.", host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1294 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1295 | /* |
| 1296 | * If strict host key checking has not been requested, allow |
| 1297 | * the connection but without password authentication or |
| 1298 | * agent forwarding. |
| 1299 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1300 | if (options.password_authentication) { |
| 1301 | error("Password authentication is disabled to avoid trojan horses."); |
| 1302 | options.password_authentication = 0; |
| 1303 | } |
| 1304 | if (options.forward_agent) { |
| 1305 | error("Agent forwarding is disabled to avoid trojan horses."); |
| 1306 | options.forward_agent = 0; |
| 1307 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1308 | /* |
| 1309 | * XXX Should permit the user to change to use the new id. |
| 1310 | * This could be done by converting the host key to an |
| 1311 | * identifying sentence, tell that the host identifies itself |
| 1312 | * by that sentence, and ask the user if he/she whishes to |
| 1313 | * accept the authentication. |
| 1314 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1315 | break; |
| 1316 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1317 | if (options.check_host_ip) |
| 1318 | xfree(ip); |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1319 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1320 | void |
| 1321 | check_rsa_host_key(char *host, struct sockaddr *hostaddr, RSA *host_key) |
| 1322 | { |
| 1323 | Key k; |
| 1324 | k.type = KEY_RSA; |
| 1325 | k.rsa = host_key; |
| 1326 | check_host_key(host, hostaddr, &k); |
| 1327 | } |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1328 | |
| 1329 | /* |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1330 | * SSH2 key exchange |
| 1331 | */ |
| 1332 | void |
| 1333 | ssh_kex2(char *host, struct sockaddr *hostaddr) |
| 1334 | { |
| 1335 | Kex *kex; |
| 1336 | char *cprop[PROPOSAL_MAX]; |
| 1337 | char *sprop[PROPOSAL_MAX]; |
| 1338 | Buffer *client_kexinit; |
| 1339 | Buffer *server_kexinit; |
| 1340 | int payload_len, dlen; |
| 1341 | unsigned int klen, kout; |
| 1342 | char *ptr; |
| 1343 | char *signature = NULL; |
| 1344 | unsigned int slen; |
| 1345 | char *server_host_key_blob = NULL; |
| 1346 | Key *server_host_key; |
| 1347 | unsigned int sbloblen; |
| 1348 | DH *dh; |
| 1349 | BIGNUM *dh_server_pub = 0; |
| 1350 | BIGNUM *shared_secret = 0; |
| 1351 | int i; |
| 1352 | unsigned char *kbuf; |
| 1353 | unsigned char *hash; |
| 1354 | |
| 1355 | /* KEXINIT */ |
| 1356 | |
| 1357 | debug("Sending KEX init."); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1358 | if (options.ciphers != NULL) { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1359 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1360 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
| 1361 | } else if ( |
| 1362 | options.cipher == SSH_CIPHER_ARCFOUR || |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1363 | options.cipher == SSH_CIPHER_3DES_CBC || |
| 1364 | options.cipher == SSH_CIPHER_CAST128_CBC || |
| 1365 | options.cipher == SSH_CIPHER_BLOWFISH_CBC) { |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1366 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1367 | myproposal[PROPOSAL_ENC_ALGS_STOC] = cipher_name(options.cipher); |
| 1368 | } |
| 1369 | if (options.compression) { |
| 1370 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib"; |
| 1371 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib"; |
| 1372 | } else { |
| 1373 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none"; |
| 1374 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; |
| 1375 | } |
| 1376 | for (i = 0; i < PROPOSAL_MAX; i++) |
| 1377 | cprop[i] = xstrdup(myproposal[i]); |
| 1378 | |
| 1379 | client_kexinit = kex_init(cprop); |
| 1380 | packet_start(SSH2_MSG_KEXINIT); |
| 1381 | packet_put_raw(buffer_ptr(client_kexinit), buffer_len(client_kexinit)); |
| 1382 | packet_send(); |
| 1383 | packet_write_wait(); |
| 1384 | |
| 1385 | debug("done"); |
| 1386 | |
| 1387 | packet_read_expect(&payload_len, SSH2_MSG_KEXINIT); |
| 1388 | |
| 1389 | /* save payload for session_id */ |
| 1390 | server_kexinit = xmalloc(sizeof(*server_kexinit)); |
| 1391 | buffer_init(server_kexinit); |
| 1392 | ptr = packet_get_raw(&payload_len); |
| 1393 | buffer_append(server_kexinit, ptr, payload_len); |
| 1394 | |
| 1395 | /* skip cookie */ |
| 1396 | for (i = 0; i < 16; i++) |
| 1397 | (void) packet_get_char(); |
| 1398 | /* kex init proposal strings */ |
| 1399 | for (i = 0; i < PROPOSAL_MAX; i++) { |
| 1400 | sprop[i] = packet_get_string(NULL); |
| 1401 | debug("got kexinit string: %s", sprop[i]); |
| 1402 | } |
| 1403 | i = (int) packet_get_char(); |
| 1404 | debug("first kex follow == %d", i); |
| 1405 | i = packet_get_int(); |
| 1406 | debug("reserved == %d", i); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1407 | packet_done(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1408 | |
| 1409 | debug("done read kexinit"); |
| 1410 | kex = kex_choose_conf(cprop, sprop, 0); |
| 1411 | |
| 1412 | /* KEXDH */ |
| 1413 | |
| 1414 | debug("Sending SSH2_MSG_KEXDH_INIT."); |
| 1415 | |
| 1416 | /* generate and send 'e', client DH public key */ |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1417 | dh = dh_new_group1(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1418 | packet_start(SSH2_MSG_KEXDH_INIT); |
| 1419 | packet_put_bignum2(dh->pub_key); |
| 1420 | packet_send(); |
| 1421 | packet_write_wait(); |
| 1422 | |
| 1423 | #ifdef DEBUG_KEXDH |
| 1424 | fprintf(stderr, "\np= "); |
| 1425 | bignum_print(dh->p); |
| 1426 | fprintf(stderr, "\ng= "); |
| 1427 | bignum_print(dh->g); |
| 1428 | fprintf(stderr, "\npub= "); |
| 1429 | bignum_print(dh->pub_key); |
| 1430 | fprintf(stderr, "\n"); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1431 | DHparams_print_fp(stderr, dh); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1432 | #endif |
| 1433 | |
| 1434 | debug("Wait SSH2_MSG_KEXDH_REPLY."); |
| 1435 | |
| 1436 | packet_read_expect(&payload_len, SSH2_MSG_KEXDH_REPLY); |
| 1437 | |
| 1438 | debug("Got SSH2_MSG_KEXDH_REPLY."); |
| 1439 | |
| 1440 | /* key, cert */ |
| 1441 | server_host_key_blob = packet_get_string(&sbloblen); |
| 1442 | server_host_key = dsa_serverkey_from_blob(server_host_key_blob, sbloblen); |
| 1443 | if (server_host_key == NULL) |
| 1444 | fatal("cannot decode server_host_key_blob"); |
| 1445 | |
| 1446 | check_host_key(host, hostaddr, server_host_key); |
| 1447 | |
| 1448 | /* DH paramter f, server public DH key */ |
| 1449 | dh_server_pub = BN_new(); |
| 1450 | if (dh_server_pub == NULL) |
| 1451 | fatal("dh_server_pub == NULL"); |
| 1452 | packet_get_bignum2(dh_server_pub, &dlen); |
| 1453 | |
| 1454 | #ifdef DEBUG_KEXDH |
| 1455 | fprintf(stderr, "\ndh_server_pub= "); |
| 1456 | bignum_print(dh_server_pub); |
| 1457 | fprintf(stderr, "\n"); |
| 1458 | debug("bits %d", BN_num_bits(dh_server_pub)); |
| 1459 | #endif |
| 1460 | |
| 1461 | /* signed H */ |
| 1462 | signature = packet_get_string(&slen); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1463 | packet_done(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1464 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1465 | if (!dh_pub_is_valid(dh, dh_server_pub)) |
| 1466 | packet_disconnect("bad server public DH value"); |
| 1467 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1468 | klen = DH_size(dh); |
| 1469 | kbuf = xmalloc(klen); |
| 1470 | kout = DH_compute_key(kbuf, dh_server_pub, dh); |
| 1471 | #ifdef DEBUG_KEXDH |
| 1472 | debug("shared secret: len %d/%d", klen, kout); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1473 | fprintf(stderr, "shared secret == "); |
| 1474 | for (i = 0; i< kout; i++) |
| 1475 | fprintf(stderr, "%02x", (kbuf[i])&0xff); |
| 1476 | fprintf(stderr, "\n"); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1477 | #endif |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1478 | shared_secret = BN_new(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1479 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1480 | BN_bin2bn(kbuf, kout, shared_secret); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1481 | memset(kbuf, 0, klen); |
| 1482 | xfree(kbuf); |
| 1483 | |
| 1484 | /* calc and verify H */ |
| 1485 | hash = kex_hash( |
| 1486 | client_version_string, |
| 1487 | server_version_string, |
| 1488 | buffer_ptr(client_kexinit), buffer_len(client_kexinit), |
| 1489 | buffer_ptr(server_kexinit), buffer_len(server_kexinit), |
| 1490 | server_host_key_blob, sbloblen, |
| 1491 | dh->pub_key, |
| 1492 | dh_server_pub, |
| 1493 | shared_secret |
| 1494 | ); |
| 1495 | buffer_free(client_kexinit); |
| 1496 | buffer_free(server_kexinit); |
| 1497 | xfree(client_kexinit); |
| 1498 | xfree(server_kexinit); |
| 1499 | #ifdef DEBUG_KEXDH |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1500 | fprintf(stderr, "hash == "); |
| 1501 | for (i = 0; i< 20; i++) |
| 1502 | fprintf(stderr, "%02x", (hash[i])&0xff); |
| 1503 | fprintf(stderr, "\n"); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1504 | #endif |
| 1505 | dsa_verify(server_host_key, (unsigned char *)signature, slen, hash, 20); |
| 1506 | key_free(server_host_key); |
| 1507 | |
| 1508 | kex_derive_keys(kex, hash, shared_secret); |
| 1509 | packet_set_kex(kex); |
| 1510 | |
| 1511 | /* have keys, free DH */ |
| 1512 | DH_free(dh); |
| 1513 | |
| 1514 | debug("Wait SSH2_MSG_NEWKEYS."); |
| 1515 | packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1516 | packet_done(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1517 | debug("GOT SSH2_MSG_NEWKEYS."); |
| 1518 | |
| 1519 | debug("send SSH2_MSG_NEWKEYS."); |
| 1520 | packet_start(SSH2_MSG_NEWKEYS); |
| 1521 | packet_send(); |
| 1522 | packet_write_wait(); |
| 1523 | debug("done: send SSH2_MSG_NEWKEYS."); |
| 1524 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1525 | #ifdef DEBUG_KEXDH |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1526 | /* send 1st encrypted/maced/compressed message */ |
| 1527 | packet_start(SSH2_MSG_IGNORE); |
| 1528 | packet_put_cstring("markus"); |
| 1529 | packet_send(); |
| 1530 | packet_write_wait(); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1531 | #endif |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1532 | debug("done: KEX2."); |
| 1533 | } |
| 1534 | /* |
| 1535 | * Authenticate user |
| 1536 | */ |
| 1537 | void |
| 1538 | ssh_userauth2(int host_key_valid, RSA *own_host_key, |
| 1539 | uid_t original_real_uid, char *host) |
| 1540 | { |
| 1541 | int type; |
| 1542 | int plen; |
| 1543 | unsigned int dlen; |
| 1544 | int partial; |
| 1545 | struct passwd *pw; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1546 | char prompt[80]; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1547 | char *server_user, *local_user; |
| 1548 | char *auths; |
| 1549 | char *password; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1550 | char *service = "ssh-connection"; /* service name */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1551 | |
| 1552 | debug("send SSH2_MSG_SERVICE_REQUEST"); |
| 1553 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
| 1554 | packet_put_cstring("ssh-userauth"); |
| 1555 | packet_send(); |
| 1556 | packet_write_wait(); |
| 1557 | |
| 1558 | type = packet_read(&plen); |
| 1559 | if (type != SSH2_MSG_SERVICE_ACCEPT) { |
| 1560 | fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type); |
| 1561 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1562 | if (packet_remaining() > 0) { |
| 1563 | char *reply = packet_get_string(&plen); |
| 1564 | debug("service_accept: %s", reply); |
| 1565 | xfree(reply); |
| 1566 | } else { |
| 1567 | /* payload empty for ssh-2.0.13 ?? */ |
| 1568 | log("buggy server: service_accept w/o service"); |
| 1569 | } |
| 1570 | packet_done(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1571 | debug("got SSH2_MSG_SERVICE_ACCEPT"); |
| 1572 | |
| 1573 | /*XX COMMONCODE: */ |
| 1574 | /* Get local user name. Use it as server user if no user name was given. */ |
| 1575 | pw = getpwuid(original_real_uid); |
| 1576 | if (!pw) |
| 1577 | fatal("User id %d not found from user database.", original_real_uid); |
| 1578 | local_user = xstrdup(pw->pw_name); |
| 1579 | server_user = options.user ? options.user : local_user; |
| 1580 | |
| 1581 | /* INITIAL request for auth */ |
| 1582 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1583 | packet_put_cstring(server_user); |
| 1584 | packet_put_cstring(service); |
| 1585 | packet_put_cstring("none"); |
| 1586 | packet_send(); |
| 1587 | packet_write_wait(); |
| 1588 | |
| 1589 | for (;;) { |
| 1590 | type = packet_read(&plen); |
| 1591 | if (type == SSH2_MSG_USERAUTH_SUCCESS) |
| 1592 | break; |
| 1593 | if (type != SSH2_MSG_USERAUTH_FAILURE) |
| 1594 | fatal("access denied: %d", type); |
| 1595 | /* SSH2_MSG_USERAUTH_FAILURE means: try again */ |
| 1596 | auths = packet_get_string(&dlen); |
| 1597 | debug("authentications that can continue: %s", auths); |
| 1598 | partial = packet_get_char(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1599 | packet_done(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1600 | if (partial) |
| 1601 | debug("partial success"); |
| 1602 | if (strstr(auths, "password") == NULL) |
| 1603 | fatal("passwd auth not supported: %s", auths); |
| 1604 | xfree(auths); |
| 1605 | /* try passwd */ |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 1606 | snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ", |
| 1607 | server_user, host); |
| 1608 | password = read_passphrase(prompt, 0); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1609 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1610 | packet_put_cstring(server_user); |
| 1611 | packet_put_cstring(service); |
| 1612 | packet_put_cstring("password"); |
| 1613 | packet_put_char(0); |
| 1614 | packet_put_cstring(password); |
| 1615 | memset(password, 0, strlen(password)); |
| 1616 | xfree(password); |
| 1617 | packet_send(); |
| 1618 | packet_write_wait(); |
| 1619 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1620 | packet_done(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1621 | debug("ssh-userauth2 successfull"); |
| 1622 | } |
| 1623 | |
| 1624 | /* |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1625 | * SSH1 key exchange |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1626 | */ |
| 1627 | void |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1628 | ssh_kex(char *host, struct sockaddr *hostaddr) |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1629 | { |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1630 | int i; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1631 | BIGNUM *key; |
| 1632 | RSA *host_key; |
| 1633 | RSA *public_key; |
| 1634 | int bits, rbits; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1635 | int ssh_cipher_default = SSH_CIPHER_3DES; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1636 | unsigned char session_key[SSH_SESSION_KEY_LENGTH]; |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1637 | unsigned char cookie[8]; |
| 1638 | unsigned int supported_ciphers; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1639 | unsigned int server_flags, client_flags; |
| 1640 | int payload_len, clen, sum_len = 0; |
| 1641 | u_int32_t rand = 0; |
| 1642 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1643 | debug("Waiting for server public key."); |
| 1644 | |
| 1645 | /* Wait for a public key packet from the server. */ |
| 1646 | packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY); |
| 1647 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1648 | /* Get cookie from the packet. */ |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1649 | for (i = 0; i < 8; i++) |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1650 | cookie[i] = packet_get_char(); |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1651 | |
| 1652 | /* Get the public key. */ |
| 1653 | public_key = RSA_new(); |
| 1654 | bits = packet_get_int();/* bits */ |
| 1655 | public_key->e = BN_new(); |
| 1656 | packet_get_bignum(public_key->e, &clen); |
| 1657 | sum_len += clen; |
| 1658 | public_key->n = BN_new(); |
| 1659 | packet_get_bignum(public_key->n, &clen); |
| 1660 | sum_len += clen; |
| 1661 | |
| 1662 | rbits = BN_num_bits(public_key->n); |
| 1663 | if (bits != rbits) { |
| 1664 | log("Warning: Server lies about size of server public key: " |
| 1665 | "actual size is %d bits vs. announced %d.", rbits, bits); |
| 1666 | log("Warning: This may be due to an old implementation of ssh."); |
| 1667 | } |
| 1668 | /* Get the host key. */ |
| 1669 | host_key = RSA_new(); |
| 1670 | bits = packet_get_int();/* bits */ |
| 1671 | host_key->e = BN_new(); |
| 1672 | packet_get_bignum(host_key->e, &clen); |
| 1673 | sum_len += clen; |
| 1674 | host_key->n = BN_new(); |
| 1675 | packet_get_bignum(host_key->n, &clen); |
| 1676 | sum_len += clen; |
| 1677 | |
| 1678 | rbits = BN_num_bits(host_key->n); |
| 1679 | if (bits != rbits) { |
| 1680 | log("Warning: Server lies about size of server host key: " |
| 1681 | "actual size is %d bits vs. announced %d.", rbits, bits); |
| 1682 | log("Warning: This may be due to an old implementation of ssh."); |
| 1683 | } |
| 1684 | |
| 1685 | /* Get protocol flags. */ |
| 1686 | server_flags = packet_get_int(); |
| 1687 | packet_set_protocol_flags(server_flags); |
| 1688 | |
| 1689 | supported_ciphers = packet_get_int(); |
| 1690 | supported_authentications = packet_get_int(); |
| 1691 | |
| 1692 | debug("Received server public key (%d bits) and host key (%d bits).", |
| 1693 | BN_num_bits(public_key->n), BN_num_bits(host_key->n)); |
| 1694 | |
| 1695 | packet_integrity_check(payload_len, |
| 1696 | 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4, |
| 1697 | SSH_SMSG_PUBLIC_KEY); |
| 1698 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1699 | check_rsa_host_key(host, hostaddr, host_key); |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1700 | |
| 1701 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; |
| 1702 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1703 | compute_session_id(session_id, cookie, host_key->n, public_key->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1704 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1705 | /* Generate a session key. */ |
| 1706 | arc4random_stir(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1707 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1708 | /* |
| 1709 | * Generate an encryption key for the session. The key is a 256 bit |
| 1710 | * random number, interpreted as a 32-byte key, with the least |
| 1711 | * significant 8 bits being the first byte of the key. |
| 1712 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1713 | for (i = 0; i < 32; i++) { |
| 1714 | if (i % 4 == 0) |
| 1715 | rand = arc4random(); |
| 1716 | session_key[i] = rand & 0xff; |
| 1717 | rand >>= 8; |
| 1718 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1719 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1720 | /* |
| 1721 | * According to the protocol spec, the first byte of the session key |
| 1722 | * is the highest byte of the integer. The session key is xored with |
| 1723 | * the first 16 bytes of the session id. |
| 1724 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1725 | key = BN_new(); |
| 1726 | BN_set_word(key, 0); |
| 1727 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { |
| 1728 | BN_lshift(key, key, 8); |
| 1729 | if (i < 16) |
| 1730 | BN_add_word(key, session_key[i] ^ session_id[i]); |
| 1731 | else |
| 1732 | BN_add_word(key, session_key[i]); |
| 1733 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1734 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1735 | /* |
| 1736 | * Encrypt the integer using the public key and host key of the |
| 1737 | * server (key with smaller modulus first). |
| 1738 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1739 | if (BN_cmp(public_key->n, host_key->n) < 0) { |
| 1740 | /* Public key has smaller modulus. */ |
| 1741 | if (BN_num_bits(host_key->n) < |
| 1742 | BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) { |
| 1743 | fatal("respond_to_rsa_challenge: host_key %d < public_key %d + " |
| 1744 | "SSH_KEY_BITS_RESERVED %d", |
| 1745 | BN_num_bits(host_key->n), |
| 1746 | BN_num_bits(public_key->n), |
| 1747 | SSH_KEY_BITS_RESERVED); |
| 1748 | } |
| 1749 | rsa_public_encrypt(key, key, public_key); |
| 1750 | rsa_public_encrypt(key, key, host_key); |
| 1751 | } else { |
| 1752 | /* Host key has smaller modulus (or they are equal). */ |
| 1753 | if (BN_num_bits(public_key->n) < |
| 1754 | BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) { |
| 1755 | fatal("respond_to_rsa_challenge: public_key %d < host_key %d + " |
| 1756 | "SSH_KEY_BITS_RESERVED %d", |
| 1757 | BN_num_bits(public_key->n), |
| 1758 | BN_num_bits(host_key->n), |
| 1759 | SSH_KEY_BITS_RESERVED); |
| 1760 | } |
| 1761 | rsa_public_encrypt(key, key, host_key); |
| 1762 | rsa_public_encrypt(key, key, public_key); |
| 1763 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1764 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1765 | /* Destroy the public keys since we no longer need them. */ |
| 1766 | RSA_free(public_key); |
| 1767 | RSA_free(host_key); |
| 1768 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1769 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1770 | if (cipher_mask1() & supported_ciphers & (1 << ssh_cipher_default)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1771 | options.cipher = ssh_cipher_default; |
| 1772 | else { |
| 1773 | debug("Cipher %s not supported, using %.100s instead.", |
| 1774 | cipher_name(ssh_cipher_default), |
| 1775 | cipher_name(SSH_FALLBACK_CIPHER)); |
| 1776 | options.cipher = SSH_FALLBACK_CIPHER; |
| 1777 | } |
| 1778 | } |
| 1779 | /* Check that the selected cipher is supported. */ |
| 1780 | if (!(supported_ciphers & (1 << options.cipher))) |
| 1781 | fatal("Selected cipher type %.100s not supported by server.", |
| 1782 | cipher_name(options.cipher)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1783 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1784 | debug("Encryption type: %.100s", cipher_name(options.cipher)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1785 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1786 | /* Send the encrypted session key to the server. */ |
| 1787 | packet_start(SSH_CMSG_SESSION_KEY); |
| 1788 | packet_put_char(options.cipher); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1789 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1790 | /* Send the cookie back to the server. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1791 | for (i = 0; i < 8; i++) |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1792 | packet_put_char(cookie[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1793 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1794 | /* Send and destroy the encrypted encryption key integer. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1795 | packet_put_bignum(key); |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1796 | BN_clear_free(key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1797 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1798 | /* Send protocol flags. */ |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1799 | packet_put_int(client_flags); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1800 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1801 | /* Send the packet now. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1802 | packet_send(); |
| 1803 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1804 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1805 | debug("Sent encrypted session key."); |
| 1806 | |
| 1807 | /* Set the encryption key. */ |
| 1808 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); |
| 1809 | |
| 1810 | /* We will no longer need the session key here. Destroy any extra copies. */ |
| 1811 | memset(session_key, 0, sizeof(session_key)); |
| 1812 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1813 | /* |
| 1814 | * Expect a success message from the server. Note that this message |
| 1815 | * will be received in encrypted form. |
| 1816 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1817 | packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); |
| 1818 | |
| 1819 | debug("Received encrypted confirmation."); |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * Authenticate user |
| 1824 | */ |
| 1825 | void |
| 1826 | ssh_userauth(int host_key_valid, RSA *own_host_key, |
| 1827 | uid_t original_real_uid, char *host) |
| 1828 | { |
| 1829 | int i, type; |
| 1830 | int payload_len; |
| 1831 | struct passwd *pw; |
| 1832 | const char *server_user, *local_user; |
| 1833 | |
| 1834 | /* Get local user name. Use it as server user if no user name was given. */ |
| 1835 | pw = getpwuid(original_real_uid); |
| 1836 | if (!pw) |
| 1837 | fatal("User id %d not found from user database.", original_real_uid); |
| 1838 | local_user = xstrdup(pw->pw_name); |
| 1839 | server_user = options.user ? options.user : local_user; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1840 | |
| 1841 | /* Send the name of the user to log in as on the server. */ |
| 1842 | packet_start(SSH_CMSG_USER); |
| 1843 | packet_put_string(server_user, strlen(server_user)); |
| 1844 | packet_send(); |
| 1845 | packet_write_wait(); |
| 1846 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1847 | /* |
| 1848 | * The server should respond with success if no authentication is |
| 1849 | * needed (the user has no password). Otherwise the server responds |
| 1850 | * with failure. |
| 1851 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1852 | type = packet_read(&payload_len); |
| 1853 | |
| 1854 | /* check whether the connection was accepted without authentication. */ |
| 1855 | if (type == SSH_SMSG_SUCCESS) |
| 1856 | return; |
| 1857 | if (type != SSH_SMSG_FAILURE) |
| 1858 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", |
| 1859 | type); |
| 1860 | |
| 1861 | #ifdef AFS |
| 1862 | /* Try Kerberos tgt passing if the server supports it. */ |
| 1863 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && |
| 1864 | options.kerberos_tgt_passing) { |
| 1865 | if (options.cipher == SSH_CIPHER_NONE) |
| 1866 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); |
| 1867 | (void) send_kerberos_tgt(); |
| 1868 | } |
| 1869 | /* Try AFS token passing if the server supports it. */ |
| 1870 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && |
| 1871 | options.afs_token_passing && k_hasafs()) { |
| 1872 | if (options.cipher == SSH_CIPHER_NONE) |
| 1873 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); |
| 1874 | send_afs_tokens(); |
| 1875 | } |
| 1876 | #endif /* AFS */ |
| 1877 | |
| 1878 | #ifdef KRB4 |
| 1879 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && |
| 1880 | options.kerberos_authentication) { |
| 1881 | debug("Trying Kerberos authentication."); |
| 1882 | if (try_kerberos_authentication()) { |
| 1883 | /* The server should respond with success or failure. */ |
| 1884 | type = packet_read(&payload_len); |
| 1885 | if (type == SSH_SMSG_SUCCESS) |
| 1886 | return; |
| 1887 | if (type != SSH_SMSG_FAILURE) |
| 1888 | packet_disconnect("Protocol error: got %d in response to Kerberos auth", type); |
| 1889 | } |
| 1890 | } |
| 1891 | #endif /* KRB4 */ |
| 1892 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1893 | /* |
| 1894 | * Use rhosts authentication if running in privileged socket and we |
| 1895 | * do not wish to remain anonymous. |
| 1896 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1897 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && |
| 1898 | options.rhosts_authentication) { |
| 1899 | debug("Trying rhosts authentication."); |
| 1900 | packet_start(SSH_CMSG_AUTH_RHOSTS); |
| 1901 | packet_put_string(local_user, strlen(local_user)); |
| 1902 | packet_send(); |
| 1903 | packet_write_wait(); |
| 1904 | |
| 1905 | /* The server should respond with success or failure. */ |
| 1906 | type = packet_read(&payload_len); |
| 1907 | if (type == SSH_SMSG_SUCCESS) |
| 1908 | return; |
| 1909 | if (type != SSH_SMSG_FAILURE) |
| 1910 | packet_disconnect("Protocol error: got %d in response to rhosts auth", |
| 1911 | type); |
| 1912 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1913 | /* |
| 1914 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host |
| 1915 | * authentication. |
| 1916 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1917 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && |
| 1918 | options.rhosts_rsa_authentication && host_key_valid) { |
| 1919 | if (try_rhosts_rsa_authentication(local_user, own_host_key)) |
| 1920 | return; |
| 1921 | } |
| 1922 | /* Try RSA authentication if the server supports it. */ |
| 1923 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && |
| 1924 | options.rsa_authentication) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1925 | /* |
| 1926 | * Try RSA authentication using the authentication agent. The |
| 1927 | * agent is tried first because no passphrase is needed for |
| 1928 | * it, whereas identity files may require passphrases. |
| 1929 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1930 | if (try_agent_authentication()) |
| 1931 | return; |
| 1932 | |
| 1933 | /* Try RSA authentication for each identity. */ |
| 1934 | for (i = 0; i < options.num_identity_files; i++) |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1935 | if (try_rsa_authentication(options.identity_files[i])) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1936 | return; |
| 1937 | } |
| 1938 | /* Try skey authentication if the server supports it. */ |
| 1939 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
| 1940 | options.skey_authentication && !options.batch_mode) { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1941 | if (try_skey_authentication()) |
| 1942 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1943 | } |
| 1944 | /* Try password authentication if the server supports it. */ |
| 1945 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && |
| 1946 | options.password_authentication && !options.batch_mode) { |
| 1947 | char prompt[80]; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1948 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1949 | snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ", |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1950 | server_user, host); |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1951 | if (try_password_authentication(prompt)) |
| 1952 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1953 | } |
| 1954 | /* All authentication methods have failed. Exit with an error message. */ |
| 1955 | fatal("Permission denied."); |
| 1956 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1957 | } |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1958 | /* |
| 1959 | * Starts a dialog with the server, and authenticates the current user on the |
| 1960 | * server. This does not need any extra privileges. The basic connection |
| 1961 | * to the server must already have been established before this is called. |
| 1962 | * If login fails, this function prints an error and never returns. |
| 1963 | * This function does not require super-user privileges. |
| 1964 | */ |
| 1965 | void |
| 1966 | ssh_login(int host_key_valid, RSA *own_host_key, const char *orighost, |
| 1967 | struct sockaddr *hostaddr, uid_t original_real_uid) |
| 1968 | { |
| 1969 | char *host, *cp; |
| 1970 | |
| 1971 | /* Convert the user-supplied hostname into all lowercase. */ |
| 1972 | host = xstrdup(orighost); |
| 1973 | for (cp = host; *cp; cp++) |
| 1974 | if (isupper(*cp)) |
| 1975 | *cp = tolower(*cp); |
| 1976 | |
| 1977 | /* Exchange protocol version identification strings with the server. */ |
| 1978 | ssh_exchange_identification(); |
| 1979 | |
| 1980 | /* Put the connection into non-blocking mode. */ |
| 1981 | packet_set_nonblocking(); |
| 1982 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1983 | /* key exchange */ |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1984 | /* authenticate user */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1985 | if (compat20) { |
| 1986 | ssh_kex2(host, hostaddr); |
| 1987 | ssh_userauth2(host_key_valid, own_host_key, original_real_uid, host); |
| 1988 | } else { |
| 1989 | supported_authentications = 0; |
| 1990 | ssh_kex(host, hostaddr); |
| 1991 | if (supported_authentications == 0) |
| 1992 | fatal("supported_authentications == 0."); |
| 1993 | ssh_userauth(host_key_valid, own_host_key, original_real_uid, host); |
| 1994 | } |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1995 | } |