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