1 /* $OpenBSD: sshconnect.c,v 1.211 2008/07/01 07:24:22 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
18 #include <sys/types.h>
21 #include <sys/socket.h>
22 #ifdef HAVE_SYS_TIME_H
23 # include <sys/time.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
52 #include "sshconnect.h"
61 char *client_version_string = NULL;
62 char *server_version_string = NULL;
64 static int matching_host_key_dns = 0;
67 extern Options options;
68 extern char *__progname;
69 extern uid_t original_real_uid;
70 extern uid_t original_effective_uid;
71 extern pid_t proxy_command_pid;
73 #ifndef INET6_ADDRSTRLEN /* for non IPv6 machines */
74 #define INET6_ADDRSTRLEN 46
77 static int show_other_keys(const char *, Key *);
78 static void warn_changed_key(Key *);
81 * Connect to the given ssh server using a proxy command.
84 ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
86 char *command_string, *tmp;
89 char *shell, strport[NI_MAXSERV];
91 if ((shell = getenv("SHELL")) == NULL)
94 /* Convert the port number into a string. */
95 snprintf(strport, sizeof strport, "%hu", port);
98 * Build the final command string in the buffer by making the
99 * appropriate substitutions to the given proxy command.
101 * Use "exec" to avoid "sh -c" processes on some platforms
104 xasprintf(&tmp, "exec %s", proxy_command);
105 command_string = percent_expand(tmp, "h", host,
106 "p", strport, (char *)NULL);
109 /* Create pipes for communicating with the proxy. */
110 if (pipe(pin) < 0 || pipe(pout) < 0)
111 fatal("Could not create pipes to communicate with the proxy: %.100s",
114 debug("Executing proxy command: %.500s", command_string);
116 /* Fork and execute the proxy command. */
117 if ((pid = fork()) == 0) {
120 /* Child. Permanently give up superuser privileges. */
121 permanently_drop_suid(original_real_uid);
123 /* Redirect stdin and stdout. */
126 if (dup2(pin[0], 0) < 0)
127 perror("dup2 stdin");
131 if (dup2(pout[1], 1) < 0)
132 perror("dup2 stdout");
133 /* Cannot be 1 because pin allocated two descriptors. */
136 /* Stderr is left as it is so that error messages get
137 printed on the user's terminal. */
140 argv[2] = command_string;
143 /* Execute the proxy command. Note that we gave up any
144 extra privileges above. */
145 execv(argv[0], argv);
151 fatal("fork failed: %.100s", strerror(errno));
153 proxy_command_pid = pid; /* save pid to clean up later */
155 /* Close child side of the descriptors. */
159 /* Free the command name. */
160 xfree(command_string);
162 /* Set the connection file descriptors. */
163 packet_set_connection(pout[0], pin[1]);
164 packet_set_timeout(options.server_alive_interval,
165 options.server_alive_count_max);
167 /* Indicate OK return */
172 * Set TCP receive buffer if requested.
173 * Note: tuning needs to happen after the socket is
174 * created but before the connection happens
175 * so winscale is negotiated properly -cjr
178 ssh_set_socket_recvbuf(int sock)
180 void *buf = (void *)&options.tcp_rcv_buf;
181 int sz = sizeof(options.tcp_rcv_buf);
183 int socksizelen = sizeof(int);
185 debug("setsockopt Attempting to set SO_RCVBUF to %d", options.tcp_rcv_buf);
186 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, buf, sz) >= 0) {
187 getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &socksize, &socksizelen);
188 debug("setsockopt SO_RCVBUF: %.100s %d", strerror(errno), socksize);
191 error("Couldn't set socket receive buffer to %d: %.100s",
192 options.tcp_rcv_buf, strerror(errno));
197 * Creates a (possibly privileged) socket for use as the ssh connection.
200 ssh_create_socket(int privileged, struct addrinfo *ai)
203 struct addrinfo hints, *res;
206 * If we are running as root and want to connect to a privileged
207 * port, bind our own socket to a privileged port.
210 int p = IPPORT_RESERVED - 1;
212 sock = rresvport_af(&p, ai->ai_family);
215 error("rresvport: af=%d %.100s", ai->ai_family,
218 debug("Allocated local port %d.", p);
220 if (options.tcp_rcv_buf > 0)
221 ssh_set_socket_recvbuf(sock);
224 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
226 error("socket: %.100s", strerror(errno));
229 if (options.tcp_rcv_buf > 0)
230 ssh_set_socket_recvbuf(sock);
232 /* Bind the socket to an alternative local IP address */
233 if (options.bind_address == NULL)
236 memset(&hints, 0, sizeof(hints));
237 hints.ai_family = ai->ai_family;
238 hints.ai_socktype = ai->ai_socktype;
239 hints.ai_protocol = ai->ai_protocol;
240 hints.ai_flags = AI_PASSIVE;
241 gaierr = getaddrinfo(options.bind_address, NULL, &hints, &res);
243 error("getaddrinfo: %s: %s", options.bind_address,
244 ssh_gai_strerror(gaierr));
248 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
249 error("bind: %s: %s", options.bind_address, strerror(errno));
259 timeout_connect(int sockfd, const struct sockaddr *serv_addr,
260 socklen_t addrlen, int *timeoutp)
263 struct timeval tv, t_start;
265 int optval, rc, result = -1;
267 gettimeofday(&t_start, NULL);
269 if (*timeoutp <= 0) {
270 result = connect(sockfd, serv_addr, addrlen);
274 set_nonblock(sockfd);
275 rc = connect(sockfd, serv_addr, addrlen);
277 unset_nonblock(sockfd);
281 if (errno != EINPROGRESS) {
286 fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
288 FD_SET(sockfd, fdset);
289 ms_to_timeval(&tv, *timeoutp);
292 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
293 if (rc != -1 || errno != EINTR)
304 debug("select: %s", strerror(errno));
307 /* Completed or failed */
309 optlen = sizeof(optval);
310 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
312 debug("getsockopt: %s", strerror(errno));
320 unset_nonblock(sockfd);
323 /* Should not occur */
324 fatal("Bogus return (%d) from select()", rc);
330 if (result == 0 && *timeoutp > 0) {
331 ms_subtract_diff(&t_start, timeoutp);
332 if (*timeoutp <= 0) {
342 * Opens a TCP/IP connection to the remote server on the given host.
343 * The address of the remote host will be returned in hostaddr.
344 * If port is 0, the default port will be used. If needpriv is true,
345 * a privileged port will be allocated to make the connection.
346 * This requires super-user privileges if needpriv is true.
347 * Connection_attempts specifies the maximum number of tries (one per
348 * second). If proxy_command is non-NULL, it specifies the command (with %h
349 * and %p substituted for host and port, respectively) to use to contact
353 ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
354 u_short port, int family, int connection_attempts, int *timeout_ms,
355 int want_keepalive, int needpriv, const char *proxy_command)
359 int sock = -1, attempt;
360 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
361 struct addrinfo hints, *ai, *aitop;
363 debug2("ssh_connect: needpriv %d", needpriv);
365 /* If a proxy command is given, connect using it. */
366 if (proxy_command != NULL)
367 return ssh_proxy_connect(host, port, proxy_command);
369 /* No proxy command. */
371 memset(&hints, 0, sizeof(hints));
372 hints.ai_family = family;
373 hints.ai_socktype = SOCK_STREAM;
374 snprintf(strport, sizeof strport, "%u", port);
375 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
376 fatal("%s: Could not resolve hostname %.100s: %s", __progname,
377 host, ssh_gai_strerror(gaierr));
379 for (attempt = 0; attempt < connection_attempts; attempt++) {
381 /* Sleep a moment before retrying. */
383 debug("Trying again...");
386 * Loop through addresses for this host, and try each one in
387 * sequence until the connection succeeds.
389 for (ai = aitop; ai; ai = ai->ai_next) {
390 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
392 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
393 ntop, sizeof(ntop), strport, sizeof(strport),
394 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
395 error("ssh_connect: getnameinfo failed");
398 debug("Connecting to %.200s [%.100s] port %s.",
399 host, ntop, strport);
401 /* Create a socket for connecting. */
402 sock = ssh_create_socket(needpriv, ai);
404 /* Any error is already output */
407 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
409 /* Successful connection. */
410 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
413 debug("connect to address %s port %s: %s",
414 ntop, strport, strerror(errno));
420 break; /* Successful connection. */
425 /* Return failure if we didn't get a successful connection. */
427 error("ssh: connect to host %s port %s: %s",
428 host, strport, strerror(errno));
432 debug("Connection established.");
434 /* Set SO_KEEPALIVE if requested. */
435 if (want_keepalive &&
436 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
438 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
440 /* Set the connection. */
441 packet_set_connection(sock, sock);
442 packet_set_timeout(options.server_alive_interval,
443 options.server_alive_count_max);
449 * Waits for the server identification string, and sends our own
450 * identification string.
453 ssh_exchange_identification(int timeout_ms)
455 char buf[256], remote_version[256]; /* must be same size! */
456 int remote_major, remote_minor, mismatch;
457 int connection_in = packet_get_connection_in();
458 int connection_out = packet_get_connection_out();
459 int minor1 = PROTOCOL_MINOR_1;
462 int fdsetsz, remaining, rc;
463 struct timeval t_start, t_remaining;
466 fdsetsz = howmany(connection_in + 1, NFDBITS) * sizeof(fd_mask);
467 fdset = xcalloc(1, fdsetsz);
469 /* Read other side's version identification. */
470 remaining = timeout_ms;
472 for (i = 0; i < sizeof(buf) - 1; i++) {
473 if (timeout_ms > 0) {
474 gettimeofday(&t_start, NULL);
475 ms_to_timeval(&t_remaining, remaining);
476 FD_SET(connection_in, fdset);
477 rc = select(connection_in + 1, fdset, NULL,
478 fdset, &t_remaining);
479 ms_subtract_diff(&t_start, &remaining);
480 if (rc == 0 || remaining <= 0)
481 fatal("Connection timed out during "
486 fatal("ssh_exchange_identification: "
487 "select: %s", strerror(errno));
491 len = atomicio(read, connection_in, &buf[i], 1);
493 if (len != 1 && errno == EPIPE)
494 fatal("ssh_exchange_identification: "
495 "Connection closed by remote host");
497 fatal("ssh_exchange_identification: "
498 "read: %.100s", strerror(errno));
499 if (buf[i] == '\r') {
502 continue; /**XXX wait for \n */
504 if (buf[i] == '\n') {
509 fatal("ssh_exchange_identification: "
510 "No banner received");
512 buf[sizeof(buf) - 1] = 0;
513 if (strncmp(buf, "SSH-", 4) == 0)
515 debug("ssh_exchange_identification: %s", buf);
517 server_version_string = xstrdup(buf);
521 * Check that the versions match. In future this might accept
522 * several versions and set appropriate flags to handle them.
524 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
525 &remote_major, &remote_minor, remote_version) != 3)
526 fatal("Bad remote protocol version identification: '%.100s'", buf);
527 debug("Remote protocol version %d.%d, remote software version %.100s",
528 remote_major, remote_minor, remote_version);
530 compat_datafellows(remote_version);
533 switch (remote_major) {
535 if (remote_minor == 99 &&
536 (options.protocol & SSH_PROTO_2) &&
537 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
541 if (!(options.protocol & SSH_PROTO_1)) {
545 if (remote_minor < 3) {
546 fatal("Remote machine has too old SSH software version.");
547 } else if (remote_minor == 3 || remote_minor == 4) {
548 /* We speak 1.3, too. */
551 if (options.forward_agent) {
552 logit("Agent forwarding disabled for protocol 1.3");
553 options.forward_agent = 0;
558 if (options.protocol & SSH_PROTO_2) {
568 fatal("Protocol major versions differ: %d vs. %d",
569 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
571 /* Send our own protocol version identification. */
572 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s%s",
573 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
574 compat20 ? PROTOCOL_MINOR_2 : minor1,
575 SSH_RELEASE, compat20 ? "\r\n" : "\n");
576 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
577 fatal("write: %.100s", strerror(errno));
578 client_version_string = xstrdup(buf);
579 chop(client_version_string);
580 chop(server_version_string);
581 debug("Local version string %.100s", client_version_string);
584 /* defaults to 'no' */
586 confirm(const char *prompt)
588 const char *msg, *again = "Please type 'yes' or 'no': ";
592 if (options.batch_mode)
594 for (msg = prompt;;msg = again) {
595 p = read_passphrase(msg, RP_ECHO);
597 (p[0] == '\0') || (p[0] == '\n') ||
598 strncasecmp(p, "no", 2) == 0)
600 if (p && strncasecmp(p, "yes", 3) == 0)
610 * check whether the supplied host key is valid, return -1 if the key
611 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
617 check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
618 Key *host_key, int readonly, const char *user_hostfile,
619 const char *system_hostfile)
622 const char *type = key_type(host_key);
623 char *ip = NULL, *host = NULL;
624 char hostline[1000], *hostp, *fp, *ra;
625 HostStatus host_status;
626 HostStatus ip_status;
627 int r, local = 0, host_ip_differ = 0;
629 char ntop[NI_MAXHOST];
631 int len, host_line, ip_line, cancelled_forwarding = 0;
632 const char *host_file = NULL, *ip_file = NULL;
635 * Force accepting of the host key for loopback/localhost. The
636 * problem is that if the home directory is NFS-mounted to multiple
637 * machines, localhost will refer to a different machine in each of
638 * them, and the user will get bogus HOST_CHANGED warnings. This
639 * essentially disables host authentication for localhost; however,
640 * this is probably not a real problem.
642 /** hostaddr == 0! */
643 switch (hostaddr->sa_family) {
645 local = (ntohl(((struct sockaddr_in *)hostaddr)->
646 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
647 salen = sizeof(struct sockaddr_in);
650 local = IN6_IS_ADDR_LOOPBACK(
651 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
652 salen = sizeof(struct sockaddr_in6);
656 salen = sizeof(struct sockaddr_storage);
659 if (options.no_host_authentication_for_localhost == 1 && local &&
660 options.host_key_alias == NULL) {
661 debug("Forcing accepting of host key for "
662 "loopback/localhost.");
667 * We don't have the remote ip-address for connections
668 * using a proxy command
670 if (options.proxy_command == NULL) {
671 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
672 NULL, 0, NI_NUMERICHOST) != 0)
673 fatal("check_host_key: getnameinfo failed");
674 ip = put_host_port(ntop, port);
676 ip = xstrdup("<no hostip for proxy command>");
680 * Turn off check_host_ip if the connection is to localhost, via proxy
681 * command or if we don't have a hostname to compare with
683 if (options.check_host_ip && (local ||
684 strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
685 options.check_host_ip = 0;
688 * Allow the user to record the key under a different name or
689 * differentiate a non-standard port. This is useful for ssh
690 * tunneling over forwarded connections or if you run multiple
691 * sshd's on different ports on the same machine.
693 if (options.host_key_alias != NULL) {
694 host = xstrdup(options.host_key_alias);
695 debug("using hostkeyalias: %s", host);
697 host = put_host_port(hostname, port);
701 * Store the host key from the known host file in here so that we can
702 * compare it with the key for the IP address.
704 file_key = key_new(host_key->type);
707 * Check if the host key is present in the user's list of known
708 * hosts or in the systemwide list.
710 host_file = user_hostfile;
711 host_status = check_host_in_hostfile(host_file, host, host_key,
712 file_key, &host_line);
713 if (host_status == HOST_NEW) {
714 host_file = system_hostfile;
715 host_status = check_host_in_hostfile(host_file, host, host_key,
716 file_key, &host_line);
719 * Also perform check for the ip address, skip the check if we are
720 * localhost or the hostname was an ip address to begin with
722 if (options.check_host_ip) {
723 Key *ip_key = key_new(host_key->type);
725 ip_file = user_hostfile;
726 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
728 if (ip_status == HOST_NEW) {
729 ip_file = system_hostfile;
730 ip_status = check_host_in_hostfile(ip_file, ip,
731 host_key, ip_key, &ip_line);
733 if (host_status == HOST_CHANGED &&
734 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
739 ip_status = host_status;
743 switch (host_status) {
745 /* The host is known and the key matches. */
746 debug("Host '%.200s' is known and matches the %s host key.",
748 debug("Found key in %s:%d", host_file, host_line);
749 if (options.check_host_ip && ip_status == HOST_NEW) {
751 logit("%s host key for IP address "
752 "'%.128s' not in list of known hosts.",
754 else if (!add_host_to_hostfile(user_hostfile, ip,
755 host_key, options.hash_known_hosts))
756 logit("Failed to add the %s host key for IP "
757 "address '%.128s' to the list of known "
758 "hosts (%.30s).", type, ip, user_hostfile);
760 logit("Warning: Permanently added the %s host "
761 "key for IP address '%.128s' to the list "
762 "of known hosts.", type, ip);
763 } else if (options.visual_host_key) {
764 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
765 ra = key_fingerprint(host_key, SSH_FP_MD5,
767 logit("Host key fingerprint is %s\n%s\n", fp, ra);
773 if (options.host_key_alias == NULL && port != 0 &&
774 port != SSH_DEFAULT_PORT) {
775 debug("checking without port identifier");
776 if (check_host_key(hostname, hostaddr, 0, host_key, 2,
777 user_hostfile, system_hostfile) == 0) {
778 debug("found matching key w/out port");
784 /* The host is new. */
785 if (options.strict_host_key_checking == 1) {
787 * User has requested strict host key checking. We
788 * will not add the host key automatically. The only
789 * alternative left is to abort.
791 error("No %s host key is known for %.200s and you "
792 "have requested strict checking.", type, host);
794 } else if (options.strict_host_key_checking == 2) {
795 char msg1[1024], msg2[1024];
797 if (show_other_keys(host, host_key))
798 snprintf(msg1, sizeof(msg1),
799 "\nbut keys of different type are already"
800 " known for this host.");
802 snprintf(msg1, sizeof(msg1), ".");
804 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
805 ra = key_fingerprint(host_key, SSH_FP_MD5,
808 if (options.verify_host_key_dns) {
809 if (matching_host_key_dns)
810 snprintf(msg2, sizeof(msg2),
811 "Matching host key fingerprint"
814 snprintf(msg2, sizeof(msg2),
815 "No matching host key fingerprint"
818 snprintf(msg, sizeof(msg),
819 "The authenticity of host '%.200s (%s)' can't be "
821 "%s key fingerprint is %s.%s%s\n%s"
822 "Are you sure you want to continue connecting "
824 host, ip, msg1, type, fp,
825 options.visual_host_key ? "\n" : "",
826 options.visual_host_key ? ra : "",
834 * If not in strict mode, add the key automatically to the
835 * local known_hosts file.
837 if (options.check_host_ip && ip_status == HOST_NEW) {
838 snprintf(hostline, sizeof(hostline), "%s,%s",
841 if (options.hash_known_hosts) {
842 /* Add hash of host and IP separately */
843 r = add_host_to_hostfile(user_hostfile, host,
844 host_key, options.hash_known_hosts) &&
845 add_host_to_hostfile(user_hostfile, ip,
846 host_key, options.hash_known_hosts);
848 /* Add unhashed "host,ip" */
849 r = add_host_to_hostfile(user_hostfile,
851 options.hash_known_hosts);
854 r = add_host_to_hostfile(user_hostfile, host, host_key,
855 options.hash_known_hosts);
860 logit("Failed to add the host to the list of known "
861 "hosts (%.500s).", user_hostfile);
863 logit("Warning: Permanently added '%.200s' (%s) to the "
864 "list of known hosts.", hostp, type);
867 if (readonly == ROQUIET)
869 if (options.check_host_ip && host_ip_differ) {
871 if (ip_status == HOST_NEW)
872 key_msg = "is unknown";
873 else if (ip_status == HOST_OK)
874 key_msg = "is unchanged";
876 key_msg = "has a different value";
877 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
878 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
879 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
880 error("The %s host key for %s has changed,", type, host);
881 error("and the key for the corresponding IP address %s", ip);
882 error("%s. This could either mean that", key_msg);
883 error("DNS SPOOFING is happening or the IP address for the host");
884 error("and its host key have changed at the same time.");
885 if (ip_status != HOST_NEW)
886 error("Offending key for IP in %s:%d", ip_file, ip_line);
888 /* The host key has changed. */
889 warn_changed_key(host_key);
890 error("Add correct host key in %.100s to get rid of this message.",
892 error("Offending key in %s:%d", host_file, host_line);
895 * If strict host key checking is in use, the user will have
896 * to edit the key manually and we can only abort.
898 if (options.strict_host_key_checking) {
899 error("%s host key for %.200s has changed and you have "
900 "requested strict checking.", type, host);
905 * If strict host key checking has not been requested, allow
906 * the connection but without MITM-able authentication or
909 if (options.password_authentication) {
910 error("Password authentication is disabled to avoid "
911 "man-in-the-middle attacks.");
912 options.password_authentication = 0;
913 cancelled_forwarding = 1;
915 if (options.kbd_interactive_authentication) {
916 error("Keyboard-interactive authentication is disabled"
917 " to avoid man-in-the-middle attacks.");
918 options.kbd_interactive_authentication = 0;
919 options.challenge_response_authentication = 0;
920 cancelled_forwarding = 1;
922 if (options.challenge_response_authentication) {
923 error("Challenge/response authentication is disabled"
924 " to avoid man-in-the-middle attacks.");
925 options.challenge_response_authentication = 0;
926 cancelled_forwarding = 1;
928 if (options.forward_agent) {
929 error("Agent forwarding is disabled to avoid "
930 "man-in-the-middle attacks.");
931 options.forward_agent = 0;
932 cancelled_forwarding = 1;
934 if (options.forward_x11) {
935 error("X11 forwarding is disabled to avoid "
936 "man-in-the-middle attacks.");
937 options.forward_x11 = 0;
938 cancelled_forwarding = 1;
940 if (options.num_local_forwards > 0 ||
941 options.num_remote_forwards > 0) {
942 error("Port forwarding is disabled to avoid "
943 "man-in-the-middle attacks.");
944 options.num_local_forwards =
945 options.num_remote_forwards = 0;
946 cancelled_forwarding = 1;
948 if (options.tun_open != SSH_TUNMODE_NO) {
949 error("Tunnel forwarding is disabled to avoid "
950 "man-in-the-middle attacks.");
951 options.tun_open = SSH_TUNMODE_NO;
952 cancelled_forwarding = 1;
954 if (options.exit_on_forward_failure && cancelled_forwarding)
955 fatal("Error: forwarding disabled due to host key "
959 * XXX Should permit the user to change to use the new id.
960 * This could be done by converting the host key to an
961 * identifying sentence, tell that the host identifies itself
962 * by that sentence, and ask the user if he/she whishes to
963 * accept the authentication.
967 fatal("internal error");
971 if (options.check_host_ip && host_status != HOST_CHANGED &&
972 ip_status == HOST_CHANGED) {
973 snprintf(msg, sizeof(msg),
974 "Warning: the %s host key for '%.200s' "
975 "differs from the key for the IP address '%.128s'"
976 "\nOffending key for IP in %s:%d",
977 type, host, ip, ip_file, ip_line);
978 if (host_status == HOST_OK) {
980 snprintf(msg + len, sizeof(msg) - len,
981 "\nMatching host key in %s:%d",
982 host_file, host_line);
984 if (options.strict_host_key_checking == 1) {
986 error("Exiting, you have requested strict checking.");
988 } else if (options.strict_host_key_checking == 2) {
989 strlcat(msg, "\nAre you sure you want "
990 "to continue connecting (yes/no)? ", sizeof(msg));
1008 /* returns 0 if key verifies or -1 if key does NOT verify */
1010 verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
1015 if (options.verify_host_key_dns &&
1016 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
1018 if (flags & DNS_VERIFY_FOUND) {
1020 if (options.verify_host_key_dns == 1 &&
1021 flags & DNS_VERIFY_MATCH &&
1022 flags & DNS_VERIFY_SECURE)
1025 if (flags & DNS_VERIFY_MATCH) {
1026 matching_host_key_dns = 1;
1028 warn_changed_key(host_key);
1029 error("Update the SSHFP RR in DNS with the new "
1030 "host key to get rid of this message.");
1035 /* return ok if the key can be found in an old keyfile */
1036 if (stat(options.system_hostfile2, &st) == 0 ||
1037 stat(options.user_hostfile2, &st) == 0) {
1038 if (check_host_key(host, hostaddr, options.port, host_key,
1039 RDONLY, options.user_hostfile2,
1040 options.system_hostfile2) == 0)
1043 return check_host_key(host, hostaddr, options.port, host_key,
1044 RDRW, options.user_hostfile, options.system_hostfile);
1048 * Starts a dialog with the server, and authenticates the current user on the
1049 * server. This does not need any extra privileges. The basic connection
1050 * to the server must already have been established before this is called.
1051 * If login fails, this function prints an error and never returns.
1052 * This function does not require super-user privileges.
1055 ssh_login(Sensitive *sensitive, const char *orighost,
1056 struct sockaddr *hostaddr, struct passwd *pw, int timeout_ms)
1059 char *server_user, *local_user;
1061 local_user = xstrdup(pw->pw_name);
1062 server_user = options.user ? options.user : local_user;
1064 /* Convert the user-supplied hostname into all lowercase. */
1065 host = xstrdup(orighost);
1066 for (cp = host; *cp; cp++)
1068 *cp = (char)tolower(*cp);
1070 /* Exchange protocol version identification strings with the server. */
1071 ssh_exchange_identification(timeout_ms);
1073 /* Put the connection into non-blocking mode. */
1074 packet_set_nonblocking();
1077 /* authenticate user */
1079 ssh_kex2(host, hostaddr);
1080 ssh_userauth2(local_user, server_user, host, sensitive);
1082 ssh_kex(host, hostaddr);
1083 ssh_userauth1(local_user, server_user, host, sensitive);
1089 ssh_put_password(char *password)
1094 if (datafellows & SSH_BUG_PASSWORDPAD) {
1095 packet_put_cstring(password);
1098 size = roundup(strlen(password) + 1, 32);
1099 padded = xcalloc(1, size);
1100 strlcpy(padded, password, size);
1101 packet_put_string(padded, size);
1102 memset(padded, 0, size);
1107 show_key_from_file(const char *file, const char *host, int keytype)
1113 found = key_new(keytype);
1114 if ((ret = lookup_key_in_hostfile_by_type(file, host,
1115 keytype, found, &line))) {
1116 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1117 ra = key_fingerprint(found, SSH_FP_MD5, SSH_FP_RANDOMART);
1118 logit("WARNING: %s key found for host %s\n"
1120 "%s key fingerprint %s.\n%s\n",
1121 key_type(found), host, file, line,
1122 key_type(found), fp, ra);
1130 /* print all known host keys for a given host, but skip keys of given type */
1132 show_other_keys(const char *host, Key *key)
1134 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
1137 for (i = 0; type[i] != -1; i++) {
1138 if (type[i] == key->type)
1140 if (type[i] != KEY_RSA1 &&
1141 show_key_from_file(options.user_hostfile2, host, type[i])) {
1145 if (type[i] != KEY_RSA1 &&
1146 show_key_from_file(options.system_hostfile2, host, type[i])) {
1150 if (show_key_from_file(options.user_hostfile, host, type[i])) {
1154 if (show_key_from_file(options.system_hostfile, host, type[i])) {
1158 debug2("no key of type %d for host %s", type[i], host);
1164 warn_changed_key(Key *host_key)
1167 const char *type = key_type(host_key);
1169 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1171 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1172 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1173 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1174 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1175 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1176 error("It is also possible that the %s host key has just been changed.", type);
1177 error("The fingerprint for the %s key sent by the remote host is\n%s.",
1179 error("Please contact your system administrator.");
1185 * Execute a local command
1188 ssh_local_cmd(const char *args)
1194 if (!options.permit_local_command ||
1195 args == NULL || !*args)
1198 if ((shell = getenv("SHELL")) == NULL)
1199 shell = _PATH_BSHELL;
1203 debug3("Executing %s -c \"%s\"", shell, args);
1204 execl(shell, shell, "-c", args, (char *)NULL);
1205 error("Couldn't execute %s -c \"%s\": %s",
1206 shell, args, strerror(errno));
1208 } else if (pid == -1)
1209 fatal("fork failed: %.100s", strerror(errno));
1210 while (waitpid(pid, &status, 0) == -1)
1212 fatal("Couldn't wait for child: %s", strerror(errno));
1214 if (!WIFEXITED(status))
1217 return (WEXITSTATUS(status));