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