]> andersk Git - openssh.git/blame - sshconnect.c
- stevesk@cvs.openbsd.org 2006/02/20 16:36:15
[openssh.git] / sshconnect.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
bcbf86ec 7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
5260325f 13 */
8efc0c15 14
15#include "includes.h"
aa2eae64 16RCSID("$OpenBSD: sshconnect.c,v 1.174 2006/02/10 01:44:27 stevesk Exp $");
17
18#include <sys/types.h>
19#include <sys/wait.h>
a75f5360 20
21#include <paths.h>
8efc0c15 22
42f11eb2 23#include "ssh.h"
8efc0c15 24#include "xmalloc.h"
25#include "rsa.h"
8ce64345 26#include "buffer.h"
8efc0c15 27#include "packet.h"
8efc0c15 28#include "uidswap.h"
29#include "compat.h"
4fe2af09 30#include "key.h"
a306f2dd 31#include "sshconnect.h"
4fe2af09 32#include "hostfile.h"
42f11eb2 33#include "log.h"
34#include "readconf.h"
35#include "atomicio.h"
36#include "misc.h"
21289cd0 37#include "dns.h"
21289cd0 38
a306f2dd 39char *client_version_string = NULL;
40char *server_version_string = NULL;
8ce64345 41
ed82a2a9 42static int matching_host_key_dns = 0;
1432b5c4 43
3fb156df 44/* import */
57112b5a 45extern Options options;
48e671d5 46extern char *__progname;
3fb156df 47extern uid_t original_real_uid;
48extern uid_t original_effective_uid;
6939bbd4 49extern pid_t proxy_command_pid;
57112b5a 50
a704dd54 51#ifndef INET6_ADDRSTRLEN /* for non IPv6 machines */
52#define INET6_ADDRSTRLEN 46
53#endif
54
8a48a7ef 55static int show_other_keys(const char *, Key *);
1a1bc5d5 56static void warn_changed_key(Key *);
8a48a7ef 57
5260325f 58/*
59 * Connect to the given ssh server using a proxy command.
60 */
396c147e 61static int
3fb156df 62ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
8efc0c15 63{
a980cbd7 64 char *command_string, *tmp;
5260325f 65 int pin[2], pout[2];
9da5c3c9 66 pid_t pid;
48e671d5 67 char strport[NI_MAXSERV];
a980cbd7 68 size_t len;
5260325f 69
70 /* Convert the port number into a string. */
48e671d5 71 snprintf(strport, sizeof strport, "%hu", port);
5260325f 72
6939bbd4 73 /*
74 * Build the final command string in the buffer by making the
75 * appropriate substitutions to the given proxy command.
76 *
aff51935 77 * Use "exec" to avoid "sh -c" processes on some platforms
6939bbd4 78 * (e.g. Solaris)
79 */
a980cbd7 80 len = strlen(proxy_command) + 6;
81 tmp = xmalloc(len);
82 strlcpy(tmp, "exec ", len);
83 strlcat(tmp, proxy_command, len);
84 command_string = percent_expand(tmp, "h", host,
85 "p", strport, (char *)NULL);
86 xfree(tmp);
5260325f 87
88 /* Create pipes for communicating with the proxy. */
89 if (pipe(pin) < 0 || pipe(pout) < 0)
90 fatal("Could not create pipes to communicate with the proxy: %.100s",
184eed6a 91 strerror(errno));
5260325f 92
93 debug("Executing proxy command: %.500s", command_string);
94
95 /* Fork and execute the proxy command. */
96 if ((pid = fork()) == 0) {
97 char *argv[10];
98
99 /* Child. Permanently give up superuser privileges. */
3fb156df 100 seteuid(original_real_uid);
101 setuid(original_real_uid);
5260325f 102
103 /* Redirect stdin and stdout. */
104 close(pin[1]);
105 if (pin[0] != 0) {
106 if (dup2(pin[0], 0) < 0)
107 perror("dup2 stdin");
108 close(pin[0]);
109 }
110 close(pout[0]);
111 if (dup2(pout[1], 1) < 0)
112 perror("dup2 stdout");
113 /* Cannot be 1 because pin allocated two descriptors. */
114 close(pout[1]);
115
116 /* Stderr is left as it is so that error messages get
117 printed on the user's terminal. */
30228d7c 118 argv[0] = _PATH_BSHELL;
5260325f 119 argv[1] = "-c";
120 argv[2] = command_string;
121 argv[3] = NULL;
122
123 /* Execute the proxy command. Note that we gave up any
124 extra privileges above. */
328654d8 125 execv(argv[0], argv);
126 perror(argv[0]);
5260325f 127 exit(1);
8efc0c15 128 }
5260325f 129 /* Parent. */
130 if (pid < 0)
131 fatal("fork failed: %.100s", strerror(errno));
6939bbd4 132 else
133 proxy_command_pid = pid; /* save pid to clean up later */
5260325f 134
135 /* Close child side of the descriptors. */
136 close(pin[0]);
137 close(pout[1]);
138
139 /* Free the command name. */
a980cbd7 140 xfree(command_string);
5260325f 141
142 /* Set the connection file descriptors. */
143 packet_set_connection(pout[0], pin[1]);
8efc0c15 144
ce773142 145 /* Indicate OK return */
146 return 0;
5260325f 147}
8efc0c15 148
5260325f 149/*
150 * Creates a (possibly privileged) socket for use as the ssh connection.
151 */
396c147e 152static int
3e131a6d 153ssh_create_socket(int privileged, struct addrinfo *ai)
8efc0c15 154{
3435f5a6 155 int sock, gaierr;
156 struct addrinfo hints, *res;
5260325f 157
aa3378df 158 /*
159 * If we are running as root and want to connect to a privileged
160 * port, bind our own socket to a privileged port.
161 */
5260325f 162 if (privileged) {
163 int p = IPPORT_RESERVED - 1;
3fb156df 164 PRIV_START;
3e131a6d 165 sock = rresvport_af(&p, ai->ai_family);
3fb156df 166 PRIV_END;
5260325f 167 if (sock < 0)
3e131a6d 168 error("rresvport: af=%d %.100s", ai->ai_family,
169 strerror(errno));
c8d54615 170 else
171 debug("Allocated local port %d.", p);
3435f5a6 172 return sock;
173 }
3e131a6d 174 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
3435f5a6 175 if (sock < 0)
176 error("socket: %.100s", strerror(errno));
3435f5a6 177
178 /* Bind the socket to an alternative local IP address */
179 if (options.bind_address == NULL)
180 return sock;
181
182 memset(&hints, 0, sizeof(hints));
3e131a6d 183 hints.ai_family = ai->ai_family;
184 hints.ai_socktype = ai->ai_socktype;
185 hints.ai_protocol = ai->ai_protocol;
3435f5a6 186 hints.ai_flags = AI_PASSIVE;
187 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
188 if (gaierr) {
189 error("getaddrinfo: %s: %s", options.bind_address,
190 gai_strerror(gaierr));
191 close(sock);
192 return -1;
193 }
194 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
195 error("bind: %s: %s", options.bind_address, strerror(errno));
196 close(sock);
197 freeaddrinfo(res);
198 return -1;
5260325f 199 }
3435f5a6 200 freeaddrinfo(res);
5260325f 201 return sock;
8efc0c15 202}
203
09ab3296 204static int
205timeout_connect(int sockfd, const struct sockaddr *serv_addr,
206 socklen_t addrlen, int timeout)
207{
208 fd_set *fdset;
209 struct timeval tv;
210 socklen_t optlen;
da54f5be 211 int fdsetsz, optval, rc, result = -1;
09ab3296 212
213 if (timeout <= 0)
214 return (connect(sockfd, serv_addr, addrlen));
215
2daf1db1 216 set_nonblock(sockfd);
09ab3296 217 rc = connect(sockfd, serv_addr, addrlen);
2daf1db1 218 if (rc == 0) {
219 unset_nonblock(sockfd);
09ab3296 220 return (0);
2daf1db1 221 }
09ab3296 222 if (errno != EINPROGRESS)
223 return (-1);
224
225 fdsetsz = howmany(sockfd + 1, NFDBITS) * sizeof(fd_mask);
226 fdset = (fd_set *)xmalloc(fdsetsz);
227
7caca6d4 228 memset(fdset, 0, fdsetsz);
09ab3296 229 FD_SET(sockfd, fdset);
230 tv.tv_sec = timeout;
231 tv.tv_usec = 0;
232
f8cc7664 233 for (;;) {
09ab3296 234 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
235 if (rc != -1 || errno != EINTR)
236 break;
237 }
238
f8cc7664 239 switch (rc) {
09ab3296 240 case 0:
241 /* Timed out */
242 errno = ETIMEDOUT;
da54f5be 243 break;
09ab3296 244 case -1:
245 /* Select error */
aff51935 246 debug("select: %s", strerror(errno));
da54f5be 247 break;
09ab3296 248 case 1:
249 /* Completed or failed */
250 optval = 0;
251 optlen = sizeof(optval);
aff51935 252 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
ce26c02a 253 &optlen) == -1) {
aff51935 254 debug("getsockopt: %s", strerror(errno));
da54f5be 255 break;
ce26c02a 256 }
09ab3296 257 if (optval != 0) {
258 errno = optval;
da54f5be 259 break;
09ab3296 260 }
da54f5be 261 result = 0;
2daf1db1 262 unset_nonblock(sockfd);
09ab3296 263 break;
264 default:
265 /* Should not occur */
266 fatal("Bogus return (%d) from select()", rc);
267 }
268
da54f5be 269 xfree(fdset);
270 return (result);
09ab3296 271}
272
5260325f 273/*
48e671d5 274 * Opens a TCP/IP connection to the remote server on the given host.
275 * The address of the remote host will be returned in hostaddr.
3fb156df 276 * If port is 0, the default port will be used. If needpriv is true,
5260325f 277 * a privileged port will be allocated to make the connection.
3fb156df 278 * This requires super-user privileges if needpriv is true.
5260325f 279 * Connection_attempts specifies the maximum number of tries (one per
280 * second). If proxy_command is non-NULL, it specifies the command (with %h
281 * and %p substituted for host and port, respectively) to use to contact
282 * the daemon.
283 */
284int
48e671d5 285ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
dab89049 286 u_short port, int family, int connection_attempts,
3fb156df 287 int needpriv, const char *proxy_command)
8efc0c15 288{
b5c334cc 289 int gaierr;
290 int on = 1;
48e671d5 291 int sock = -1, attempt;
48e671d5 292 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
b5c334cc 293 struct addrinfo hints, *ai, *aitop;
5260325f 294
a77673cc 295 debug2("ssh_connect: needpriv %d", needpriv);
5260325f 296
5260325f 297 /* If a proxy command is given, connect using it. */
298 if (proxy_command != NULL)
3fb156df 299 return ssh_proxy_connect(host, port, proxy_command);
5260325f 300
301 /* No proxy command. */
302
48e671d5 303 memset(&hints, 0, sizeof(hints));
dab89049 304 hints.ai_family = family;
48e671d5 305 hints.ai_socktype = SOCK_STREAM;
d6133f43 306 snprintf(strport, sizeof strport, "%u", port);
48e671d5 307 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
308 fatal("%s: %.100s: %s", __progname, host,
309 gai_strerror(gaierr));
5260325f 310
95f1eccc 311 /*
312 * Try to connect several times. On some machines, the first time
313 * will sometimes fail. In general socket code appears to behave
314 * quite magically on many machines.
ce773142 315 */
316 for (attempt = 0; ;) {
5260325f 317 if (attempt > 0)
318 debug("Trying again...");
319
48e671d5 320 /* Loop through addresses for this host, and try each one in
6ae2364d 321 sequence until the connection succeeds. */
48e671d5 322 for (ai = aitop; ai; ai = ai->ai_next) {
323 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
324 continue;
325 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
326 ntop, sizeof(ntop), strport, sizeof(strport),
327 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
328 error("ssh_connect: getnameinfo failed");
329 continue;
330 }
331 debug("Connecting to %.200s [%.100s] port %s.",
332 host, ntop, strport);
333
334 /* Create a socket for connecting. */
3e131a6d 335 sock = ssh_create_socket(needpriv, ai);
48e671d5 336 if (sock < 0)
ce773142 337 /* Any error is already output */
48e671d5 338 continue;
339
09ab3296 340 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
341 options.connection_timeout) >= 0) {
48e671d5 342 /* Successful connection. */
cd332296 343 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
5260325f 344 break;
48e671d5 345 } else {
c750d869 346 debug("connect to address %s port %s: %s",
347 ntop, strport, strerror(errno));
aa3378df 348 /*
349 * Close the failed socket; there appear to
350 * be some problems when reusing a socket for
351 * which connect() has already returned an
352 * error.
353 */
5260325f 354 close(sock);
355 }
8efc0c15 356 }
48e671d5 357 if (ai)
358 break; /* Successful connection. */
8efc0c15 359
ce773142 360 attempt++;
361 if (attempt >= connection_attempts)
362 break;
5260325f 363 /* Sleep a moment before retrying. */
364 sleep(1);
365 }
48e671d5 366
367 freeaddrinfo(aitop);
368
5260325f 369 /* Return failure if we didn't get a successful connection. */
9bd68577 370 if (attempt >= connection_attempts) {
cf039bd1 371 error("ssh: connect to host %s port %s: %s",
9bd68577 372 host, strport, strerror(errno));
cf039bd1 373 return (-1);
9bd68577 374 }
8efc0c15 375
5260325f 376 debug("Connection established.");
8efc0c15 377
fd573618 378 /* Set SO_KEEPALIVE if requested. */
379 if (options.tcp_keep_alive &&
b5c334cc 380 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
381 sizeof(on)) < 0)
382 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
8efc0c15 383
5260325f 384 /* Set the connection. */
385 packet_set_connection(sock, sock);
8efc0c15 386
ce773142 387 return 0;
8efc0c15 388}
389
5260325f 390/*
391 * Waits for the server identification string, and sends our own
392 * identification string.
393 */
396c147e 394static void
5ca51e19 395ssh_exchange_identification(void)
8efc0c15 396{
5260325f 397 char buf[256], remote_version[256]; /* must be same size! */
2ceb8101 398 int remote_major, remote_minor, mismatch;
5260325f 399 int connection_in = packet_get_connection_in();
400 int connection_out = packet_get_connection_out();
2b87da3b 401 int minor1 = PROTOCOL_MINOR_1;
2ceb8101 402 u_int i;
5260325f 403
05624c18 404 /* Read other side's version identification. */
38c295d6 405 for (;;) {
406 for (i = 0; i < sizeof(buf) - 1; i++) {
05624c18 407 size_t len = atomicio(read, connection_in, &buf[i], 1);
408
56964485 409 if (len != 1 && errno == EPIPE)
38c295d6 410 fatal("ssh_exchange_identification: Connection closed by remote host");
05624c18 411 else if (len != 1)
412 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
38c295d6 413 if (buf[i] == '\r') {
414 buf[i] = '\n';
415 buf[i + 1] = 0;
416 continue; /**XXX wait for \n */
417 }
418 if (buf[i] == '\n') {
419 buf[i + 1] = 0;
420 break;
421 }
5260325f 422 }
38c295d6 423 buf[sizeof(buf) - 1] = 0;
424 if (strncmp(buf, "SSH-", 4) == 0)
5260325f 425 break;
38c295d6 426 debug("ssh_exchange_identification: %s", buf);
8efc0c15 427 }
8ce64345 428 server_version_string = xstrdup(buf);
5260325f 429
aa3378df 430 /*
431 * Check that the versions match. In future this might accept
432 * several versions and set appropriate flags to handle them.
433 */
8ce64345 434 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
435 &remote_major, &remote_minor, remote_version) != 3)
5260325f 436 fatal("Bad remote protocol version identification: '%.100s'", buf);
437 debug("Remote protocol version %d.%d, remote software version %.100s",
184eed6a 438 remote_major, remote_minor, remote_version);
5260325f 439
8ce64345 440 compat_datafellows(remote_version);
a8be9f80 441 mismatch = 0;
442
6aacefa7 443 switch (remote_major) {
a8be9f80 444 case 1:
445 if (remote_minor == 99 &&
446 (options.protocol & SSH_PROTO_2) &&
447 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
448 enable_compat20();
449 break;
5260325f 450 }
a8be9f80 451 if (!(options.protocol & SSH_PROTO_1)) {
452 mismatch = 1;
453 break;
454 }
455 if (remote_minor < 3) {
456 fatal("Remote machine has too old SSH software version.");
fa08c86b 457 } else if (remote_minor == 3 || remote_minor == 4) {
a8be9f80 458 /* We speak 1.3, too. */
459 enable_compat13();
fa08c86b 460 minor1 = 3;
a8be9f80 461 if (options.forward_agent) {
bbe88b6d 462 logit("Agent forwarding disabled for protocol 1.3");
a8be9f80 463 options.forward_agent = 0;
464 }
465 }
466 break;
467 case 2:
468 if (options.protocol & SSH_PROTO_2) {
469 enable_compat20();
470 break;
471 }
472 /* FALLTHROUGH */
6ae2364d 473 default:
a8be9f80 474 mismatch = 1;
475 break;
8efc0c15 476 }
a8be9f80 477 if (mismatch)
5260325f 478 fatal("Protocol major versions differ: %d vs. %d",
a8be9f80 479 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
480 remote_major);
5260325f 481 /* Send our own protocol version identification. */
482 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
a8be9f80 483 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
fa08c86b 484 compat20 ? PROTOCOL_MINOR_2 : minor1,
8ce64345 485 SSH_VERSION);
dc54438a 486 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
5260325f 487 fatal("write: %.100s", strerror(errno));
8ce64345 488 client_version_string = xstrdup(buf);
489 chop(client_version_string);
490 chop(server_version_string);
491 debug("Local version string %.100s", client_version_string);
8efc0c15 492}
493
f21032a6 494/* defaults to 'no' */
396c147e 495static int
a2c92c4a 496confirm(const char *prompt)
8efc0c15 497{
616a6b93 498 const char *msg, *again = "Please type 'yes' or 'no': ";
499 char *p;
500 int ret = -1;
5260325f 501
f21032a6 502 if (options.batch_mode)
503 return 0;
616a6b93 504 for (msg = prompt;;msg = again) {
505 p = read_passphrase(msg, RP_ECHO);
506 if (p == NULL ||
507 (p[0] == '\0') || (p[0] == '\n') ||
508 strncasecmp(p, "no", 2) == 0)
509 ret = 0;
f11fe301 510 if (p && strncasecmp(p, "yes", 3) == 0)
616a6b93 511 ret = 1;
512 if (p)
513 xfree(p);
514 if (ret != -1)
515 return ret;
8efc0c15 516 }
8efc0c15 517}
518
5260325f 519/*
f49bc4f7 520 * check whether the supplied host key is valid, return -1 if the key
521 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
5260325f 522 */
396c147e 523static int
a306f2dd 524check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
f49bc4f7 525 int readonly, const char *user_hostfile, const char *system_hostfile)
8efc0c15 526{
4fe2af09 527 Key *file_key;
b6c7b7b7 528 const char *type = key_type(host_key);
95f1eccc 529 char *ip = NULL;
22138a36 530 char hostline[1000], *hostp, *fp;
5260325f 531 HostStatus host_status;
532 HostStatus ip_status;
e79276c2 533 int r, local = 0, host_ip_differ = 0;
20af321f 534 int salen;
48e671d5 535 char ntop[NI_MAXHOST];
616a6b93 536 char msg[1024];
1432b5c4 537 int len, host_line, ip_line;
1e3b8b07 538 const char *host_file = NULL, *ip_file = NULL;
48e671d5 539
540 /*
541 * Force accepting of the host key for loopback/localhost. The
542 * problem is that if the home directory is NFS-mounted to multiple
543 * machines, localhost will refer to a different machine in each of
544 * them, and the user will get bogus HOST_CHANGED warnings. This
545 * essentially disables host authentication for localhost; however,
546 * this is probably not a real problem.
547 */
a306f2dd 548 /** hostaddr == 0! */
48e671d5 549 switch (hostaddr->sa_family) {
550 case AF_INET:
f49bc4f7 551 local = (ntohl(((struct sockaddr_in *)hostaddr)->
4e2e5cfd 552 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
20af321f 553 salen = sizeof(struct sockaddr_in);
48e671d5 554 break;
555 case AF_INET6:
f49bc4f7 556 local = IN6_IS_ADDR_LOOPBACK(
557 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
20af321f 558 salen = sizeof(struct sockaddr_in6);
48e671d5 559 break;
560 default:
561 local = 0;
20af321f 562 salen = sizeof(struct sockaddr_storage);
48e671d5 563 break;
564 }
8bbc048a 565 if (options.no_host_authentication_for_localhost == 1 && local &&
566 options.host_key_alias == NULL) {
7c49df64 567 debug("Forcing accepting of host key for "
568 "loopback/localhost.");
f49bc4f7 569 return 0;
48e671d5 570 }
5260325f 571
57112b5a 572 /*
7c49df64 573 * We don't have the remote ip-address for connections
574 * using a proxy command
575 */
576 if (options.proxy_command == NULL) {
577 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
578 NULL, 0, NI_NUMERICHOST) != 0)
579 fatal("check_host_key: getnameinfo failed");
580 ip = xstrdup(ntop);
581 } else {
582 ip = xstrdup("<no hostip for proxy command>");
583 }
584 /*
585 * Turn off check_host_ip if the connection is to localhost, via proxy
586 * command or if we don't have a hostname to compare with
57112b5a 587 */
7c49df64 588 if (options.check_host_ip &&
589 (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
57112b5a 590 options.check_host_ip = 0;
591
8abcdba4 592 /*
7c49df64 593 * Allow the user to record the key under a different name. This is
8abcdba4 594 * useful for ssh tunneling over forwarded connections or if you run
7c49df64 595 * multiple sshd's on different ports on the same machine.
8abcdba4 596 */
597 if (options.host_key_alias != NULL) {
598 host = options.host_key_alias;
599 debug("using hostkeyalias: %s", host);
600 }
601
95f1eccc 602 /*
603 * Store the host key from the known host file in here so that we can
604 * compare it with the key for the IP address.
605 */
4fe2af09 606 file_key = key_new(host_key->type);
5260325f 607
aa3378df 608 /*
7b9b0103 609 * Check if the host key is present in the user's list of known
aa3378df 610 * hosts or in the systemwide list.
611 */
1e3b8b07 612 host_file = user_hostfile;
f49bc4f7 613 host_status = check_host_in_hostfile(host_file, host, host_key,
184eed6a 614 file_key, &host_line);
1e3b8b07 615 if (host_status == HOST_NEW) {
616 host_file = system_hostfile;
f49bc4f7 617 host_status = check_host_in_hostfile(host_file, host, host_key,
618 file_key, &host_line);
1e3b8b07 619 }
aa3378df 620 /*
621 * Also perform check for the ip address, skip the check if we are
622 * localhost or the hostname was an ip address to begin with
623 */
7c49df64 624 if (options.check_host_ip) {
4fe2af09 625 Key *ip_key = key_new(host_key->type);
5260325f 626
1e3b8b07 627 ip_file = user_hostfile;
f49bc4f7 628 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
629 ip_key, &ip_line);
1e3b8b07 630 if (ip_status == HOST_NEW) {
631 ip_file = system_hostfile;
f49bc4f7 632 ip_status = check_host_in_hostfile(ip_file, ip,
633 host_key, ip_key, &ip_line);
1e3b8b07 634 }
5260325f 635 if (host_status == HOST_CHANGED &&
4fe2af09 636 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
5260325f 637 host_ip_differ = 1;
638
4fe2af09 639 key_free(ip_key);
5260325f 640 } else
641 ip_status = host_status;
642
4fe2af09 643 key_free(file_key);
5260325f 644
645 switch (host_status) {
646 case HOST_OK:
647 /* The host is known and the key matches. */
1d1ffb87 648 debug("Host '%.200s' is known and matches the %s host key.",
649 host, type);
1e3b8b07 650 debug("Found key in %s:%d", host_file, host_line);
7c49df64 651 if (options.check_host_ip && ip_status == HOST_NEW) {
f49bc4f7 652 if (readonly)
bbe88b6d 653 logit("%s host key for IP address "
f49bc4f7 654 "'%.128s' not in list of known hosts.",
7c49df64 655 type, ip);
f49bc4f7 656 else if (!add_host_to_hostfile(user_hostfile, ip,
5c63c2ab 657 host_key, options.hash_known_hosts))
bbe88b6d 658 logit("Failed to add the %s host key for IP "
f49bc4f7 659 "address '%.128s' to the list of known "
660 "hosts (%.30s).", type, ip, user_hostfile);
661 else
bbe88b6d 662 logit("Warning: Permanently added the %s host "
f49bc4f7 663 "key for IP address '%.128s' to the list "
664 "of known hosts.", type, ip);
5260325f 665 }
666 break;
667 case HOST_NEW:
f49bc4f7 668 if (readonly)
669 goto fail;
5260325f 670 /* The host is new. */
671 if (options.strict_host_key_checking == 1) {
f49bc4f7 672 /*
673 * User has requested strict host key checking. We
674 * will not add the host key automatically. The only
675 * alternative left is to abort.
676 */
677 error("No %s host key is known for %.200s and you "
678 "have requested strict checking.", type, host);
679 goto fail;
5260325f 680 } else if (options.strict_host_key_checking == 2) {
1432b5c4 681 char msg1[1024], msg2[1024];
682
683 if (show_other_keys(host, host_key))
684 snprintf(msg1, sizeof(msg1),
4e2e5cfd 685 "\nbut keys of different type are already"
686 " known for this host.");
1432b5c4 687 else
688 snprintf(msg1, sizeof(msg1), ".");
5260325f 689 /* The default */
22138a36 690 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1432b5c4 691 msg2[0] = '\0';
1432b5c4 692 if (options.verify_host_key_dns) {
0161a13d 693 if (matching_host_key_dns)
1432b5c4 694 snprintf(msg2, sizeof(msg2),
695 "Matching host key fingerprint"
696 " found in DNS.\n");
697 else
698 snprintf(msg2, sizeof(msg2),
699 "No matching host key fingerprint"
700 " found in DNS.\n");
701 }
616a6b93 702 snprintf(msg, sizeof(msg),
f49bc4f7 703 "The authenticity of host '%.200s (%s)' can't be "
8a48a7ef 704 "established%s\n"
1432b5c4 705 "%s key fingerprint is %s.\n%s"
f49bc4f7 706 "Are you sure you want to continue connecting "
8a48a7ef 707 "(yes/no)? ",
1432b5c4 708 host, ip, msg1, type, fp, msg2);
22138a36 709 xfree(fp);
616a6b93 710 if (!confirm(msg))
f49bc4f7 711 goto fail;
5260325f 712 }
f49bc4f7 713 /*
714 * If not in strict mode, add the key automatically to the
715 * local known_hosts file.
716 */
e79276c2 717 if (options.check_host_ip && ip_status == HOST_NEW) {
718 snprintf(hostline, sizeof(hostline), "%s,%s",
719 host, ip);
720 hostp = hostline;
721 if (options.hash_known_hosts) {
722 /* Add hash of host and IP separately */
723 r = add_host_to_hostfile(user_hostfile, host,
724 host_key, options.hash_known_hosts) &&
725 add_host_to_hostfile(user_hostfile, ip,
726 host_key, options.hash_known_hosts);
727 } else {
728 /* Add unhashed "host,ip" */
729 r = add_host_to_hostfile(user_hostfile,
730 hostline, host_key,
731 options.hash_known_hosts);
732 }
733 } else {
734 r = add_host_to_hostfile(user_hostfile, host, host_key,
735 options.hash_known_hosts);
736 hostp = host;
737 }
738
739 if (!r)
bbe88b6d 740 logit("Failed to add the host to the list of known "
f49bc4f7 741 "hosts (%.500s).", user_hostfile);
5260325f 742 else
bbe88b6d 743 logit("Warning: Permanently added '%.200s' (%s) to the "
f49bc4f7 744 "list of known hosts.", hostp, type);
5260325f 745 break;
746 case HOST_CHANGED:
747 if (options.check_host_ip && host_ip_differ) {
ca75d7de 748 char *key_msg;
5260325f 749 if (ip_status == HOST_NEW)
ca75d7de 750 key_msg = "is unknown";
5260325f 751 else if (ip_status == HOST_OK)
ca75d7de 752 key_msg = "is unchanged";
5260325f 753 else
ca75d7de 754 key_msg = "has a different value";
5260325f 755 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
756 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
757 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1d1ffb87 758 error("The %s host key for %s has changed,", type, host);
5260325f 759 error("and the key for the according IP address %s", ip);
ca75d7de 760 error("%s. This could either mean that", key_msg);
5260325f 761 error("DNS SPOOFING is happening or the IP address for the host");
1e3b8b07 762 error("and its host key have changed at the same time.");
7c49df64 763 if (ip_status != HOST_NEW)
1e3b8b07 764 error("Offending key for IP in %s:%d", ip_file, ip_line);
5260325f 765 }
766 /* The host key has changed. */
1a1bc5d5 767 warn_changed_key(host_key);
5260325f 768 error("Add correct host key in %.100s to get rid of this message.",
8abcdba4 769 user_hostfile);
1e3b8b07 770 error("Offending key in %s:%d", host_file, host_line);
5260325f 771
aa3378df 772 /*
773 * If strict host key checking is in use, the user will have
774 * to edit the key manually and we can only abort.
775 */
f49bc4f7 776 if (options.strict_host_key_checking) {
777 error("%s host key for %.200s has changed and you have "
778 "requested strict checking.", type, host);
779 goto fail;
780 }
5260325f 781
aa3378df 782 /*
783 * If strict host key checking has not been requested, allow
d453a600 784 * the connection but without MITM-able authentication or
aa3378df 785 * agent forwarding.
786 */
5260325f 787 if (options.password_authentication) {
f49bc4f7 788 error("Password authentication is disabled to avoid "
789 "man-in-the-middle attacks.");
5260325f 790 options.password_authentication = 0;
791 }
d453a600 792 if (options.kbd_interactive_authentication) {
793 error("Keyboard-interactive authentication is disabled"
794 " to avoid man-in-the-middle attacks.");
795 options.kbd_interactive_authentication = 0;
796 options.challenge_response_authentication = 0;
797 }
798 if (options.challenge_response_authentication) {
799 error("Challenge/response authentication is disabled"
800 " to avoid man-in-the-middle attacks.");
801 options.challenge_response_authentication = 0;
802 }
5260325f 803 if (options.forward_agent) {
f49bc4f7 804 error("Agent forwarding is disabled to avoid "
805 "man-in-the-middle attacks.");
5260325f 806 options.forward_agent = 0;
807 }
0b6fbf03 808 if (options.forward_x11) {
f49bc4f7 809 error("X11 forwarding is disabled to avoid "
810 "man-in-the-middle attacks.");
0b6fbf03 811 options.forward_x11 = 0;
812 }
f49bc4f7 813 if (options.num_local_forwards > 0 ||
814 options.num_remote_forwards > 0) {
815 error("Port forwarding is disabled to avoid "
816 "man-in-the-middle attacks.");
817 options.num_local_forwards =
184eed6a 818 options.num_remote_forwards = 0;
0b6fbf03 819 }
aa3378df 820 /*
821 * XXX Should permit the user to change to use the new id.
822 * This could be done by converting the host key to an
823 * identifying sentence, tell that the host identifies itself
824 * by that sentence, and ask the user if he/she whishes to
825 * accept the authentication.
826 */
5260325f 827 break;
8a48a7ef 828 case HOST_FOUND:
829 fatal("internal error");
830 break;
5260325f 831 }
0b6fbf03 832
7c49df64 833 if (options.check_host_ip && host_status != HOST_CHANGED &&
834 ip_status == HOST_CHANGED) {
616a6b93 835 snprintf(msg, sizeof(msg),
836 "Warning: the %s host key for '%.200s' "
837 "differs from the key for the IP address '%.128s'"
838 "\nOffending key for IP in %s:%d",
839 type, host, ip, ip_file, ip_line);
840 if (host_status == HOST_OK) {
841 len = strlen(msg);
842 snprintf(msg + len, sizeof(msg) - len,
843 "\nMatching host key in %s:%d",
7203d6bb 844 host_file, host_line);
616a6b93 845 }
7c49df64 846 if (options.strict_host_key_checking == 1) {
b0545fe6 847 logit("%s", msg);
f49bc4f7 848 error("Exiting, you have requested strict checking.");
849 goto fail;
7c49df64 850 } else if (options.strict_host_key_checking == 2) {
616a6b93 851 strlcat(msg, "\nAre you sure you want "
852 "to continue connecting (yes/no)? ", sizeof(msg));
853 if (!confirm(msg))
f49bc4f7 854 goto fail;
616a6b93 855 } else {
b0545fe6 856 logit("%s", msg);
7c49df64 857 }
858 }
859
0b6fbf03 860 xfree(ip);
f49bc4f7 861 return 0;
862
863fail:
864 xfree(ip);
865 return -1;
866}
867
21289cd0 868/* returns 0 if key verifies or -1 if key does NOT verify */
f49bc4f7 869int
870verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
871{
872 struct stat st;
0161a13d 873 int flags = 0;
f49bc4f7 874
0161a13d 875 if (options.verify_host_key_dns &&
876 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
877
878 if (flags & DNS_VERIFY_FOUND) {
879
880 if (options.verify_host_key_dns == 1 &&
881 flags & DNS_VERIFY_MATCH &&
882 flags & DNS_VERIFY_SECURE)
883 return 0;
884
885 if (flags & DNS_VERIFY_MATCH) {
886 matching_host_key_dns = 1;
887 } else {
888 warn_changed_key(host_key);
889 error("Update the SSHFP RR in DNS with the new "
890 "host key to get rid of this message.");
891 }
21289cd0 892 }
893 }
21289cd0 894
f49bc4f7 895 /* return ok if the key can be found in an old keyfile */
896 if (stat(options.system_hostfile2, &st) == 0 ||
897 stat(options.user_hostfile2, &st) == 0) {
898 if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
899 options.user_hostfile2, options.system_hostfile2) == 0)
900 return 0;
901 }
902 return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
903 options.user_hostfile, options.system_hostfile);
95f1eccc 904}
905
8ce64345 906/*
a306f2dd 907 * Starts a dialog with the server, and authenticates the current user on the
908 * server. This does not need any extra privileges. The basic connection
909 * to the server must already have been established before this is called.
910 * If login fails, this function prints an error and never returns.
911 * This function does not require super-user privileges.
8ce64345 912 */
913void
39c00dc2 914ssh_login(Sensitive *sensitive, const char *orighost,
63bd8c36 915 struct sockaddr *hostaddr, struct passwd *pw)
8ce64345 916{
a306f2dd 917 char *host, *cp;
8ce64345 918 char *server_user, *local_user;
8ce64345 919
8ce64345 920 local_user = xstrdup(pw->pw_name);
921 server_user = options.user ? options.user : local_user;
922
7b2ea3a1 923 /* Convert the user-supplied hostname into all lowercase. */
924 host = xstrdup(orighost);
925 for (cp = host; *cp; cp++)
926 if (isupper(*cp))
927 *cp = tolower(*cp);
928
929 /* Exchange protocol version identification strings with the server. */
930 ssh_exchange_identification();
931
932 /* Put the connection into non-blocking mode. */
933 packet_set_nonblocking();
934
7b2ea3a1 935 /* key exchange */
7b2ea3a1 936 /* authenticate user */
8ce64345 937 if (compat20) {
938 ssh_kex2(host, hostaddr);
39c00dc2 939 ssh_userauth2(local_user, server_user, host, sensitive);
8ce64345 940 } else {
8ce64345 941 ssh_kex(host, hostaddr);
39c00dc2 942 ssh_userauth1(local_user, server_user, host, sensitive);
8ce64345 943 }
7b2ea3a1 944}
0ae4fe1d 945
946void
947ssh_put_password(char *password)
948{
949 int size;
950 char *padded;
951
99c415db 952 if (datafellows & SSH_BUG_PASSWORDPAD) {
449c5ba5 953 packet_put_cstring(password);
99c415db 954 return;
955 }
0ae4fe1d 956 size = roundup(strlen(password) + 1, 32);
957 padded = xmalloc(size);
958 memset(padded, 0, size);
959 strlcpy(padded, password, size);
960 packet_put_string(padded, size);
961 memset(padded, 0, size);
962 xfree(padded);
963}
8a48a7ef 964
965static int
966show_key_from_file(const char *file, const char *host, int keytype)
967{
968 Key *found;
969 char *fp;
970 int line, ret;
971
972 found = key_new(keytype);
973 if ((ret = lookup_key_in_hostfile_by_type(file, host,
974 keytype, found, &line))) {
975 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
bbe88b6d 976 logit("WARNING: %s key found for host %s\n"
567a05bf 977 "in %s:%d\n"
8a48a7ef 978 "%s key fingerprint %s.",
979 key_type(found), host, file, line,
980 key_type(found), fp);
981 xfree(fp);
982 }
983 key_free(found);
984 return (ret);
985}
986
987/* print all known host keys for a given host, but skip keys of given type */
988static int
989show_other_keys(const char *host, Key *key)
990{
991 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
992 int i, found = 0;
993
994 for (i = 0; type[i] != -1; i++) {
995 if (type[i] == key->type)
996 continue;
997 if (type[i] != KEY_RSA1 &&
998 show_key_from_file(options.user_hostfile2, host, type[i])) {
999 found = 1;
1000 continue;
1001 }
1002 if (type[i] != KEY_RSA1 &&
1003 show_key_from_file(options.system_hostfile2, host, type[i])) {
1004 found = 1;
1005 continue;
1006 }
1007 if (show_key_from_file(options.user_hostfile, host, type[i])) {
1008 found = 1;
1009 continue;
1010 }
1011 if (show_key_from_file(options.system_hostfile, host, type[i])) {
1012 found = 1;
1013 continue;
1014 }
1015 debug2("no key of type %d for host %s", type[i], host);
1016 }
1017 return (found);
1018}
1a1bc5d5 1019
1020static void
1021warn_changed_key(Key *host_key)
1022{
1023 char *fp;
f5da7f70 1024 const char *type = key_type(host_key);
1a1bc5d5 1025
1026 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1027
1028 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1029 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1030 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1031 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1032 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1033 error("It is also possible that the %s host key has just been changed.", type);
1034 error("The fingerprint for the %s key sent by the remote host is\n%s.",
1035 type, fp);
1036 error("Please contact your system administrator.");
1037
1038 xfree(fp);
1a1bc5d5 1039}
d20f3c9e 1040
1041/*
1042 * Execute a local command
1043 */
1044int
1045ssh_local_cmd(const char *args)
1046{
1047 char *shell;
1048 pid_t pid;
1049 int status;
1050
1051 if (!options.permit_local_command ||
1052 args == NULL || !*args)
1053 return (1);
1054
1055 if ((shell = getenv("SHELL")) == NULL)
1056 shell = _PATH_BSHELL;
1057
1058 pid = fork();
1059 if (pid == 0) {
1060 debug3("Executing %s -c \"%s\"", shell, args);
1061 execl(shell, shell, "-c", args, (char *)NULL);
1062 error("Couldn't execute %s -c \"%s\": %s",
1063 shell, args, strerror(errno));
1064 _exit(1);
1065 } else if (pid == -1)
1066 fatal("fork failed: %.100s", strerror(errno));
1067 while (waitpid(pid, &status, 0) == -1)
1068 if (errno != EINTR)
1069 fatal("Couldn't wait for child: %s", strerror(errno));
1070
1071 if (!WIFEXITED(status))
1072 return (1);
1073
1074 return (WEXITSTATUS(status));
1075}
This page took 1.191698 seconds and 5 git commands to generate.