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 if (options.tcp_rcv_buf > 0)
233 ssh_set_socket_recvbuf(sock);
235 /* Bind the socket to an alternative local IP address */
236 if (options.bind_address == NULL)
239 memset(&hints, 0, sizeof(hints));
240 hints.ai_family = ai->ai_family;
241 hints.ai_socktype = ai->ai_socktype;
242 hints.ai_protocol = ai->ai_protocol;
243 hints.ai_flags = AI_PASSIVE;
244 gaierr = getaddrinfo(options.bind_address, NULL, &hints, &res);
246 error("getaddrinfo: %s: %s", options.bind_address,
247 ssh_gai_strerror(gaierr));
251 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
252 error("bind: %s: %s", options.bind_address, strerror(errno));
262 timeout_connect(int sockfd, const struct sockaddr *serv_addr,
263 socklen_t addrlen, int *timeoutp)
266 struct timeval tv, t_start;
268 int optval, rc, result = -1;
270 gettimeofday(&t_start, NULL);
272 if (*timeoutp <= 0) {
273 result = connect(sockfd, serv_addr, addrlen);
277 set_nonblock(sockfd);
278 rc = connect(sockfd, serv_addr, addrlen);
280 unset_nonblock(sockfd);
284 if (errno != EINPROGRESS) {
289 fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
291 FD_SET(sockfd, fdset);
292 ms_to_timeval(&tv, *timeoutp);
295 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
296 if (rc != -1 || errno != EINTR)
307 debug("select: %s", strerror(errno));
310 /* Completed or failed */
312 optlen = sizeof(optval);
313 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
315 debug("getsockopt: %s", strerror(errno));
323 unset_nonblock(sockfd);
326 /* Should not occur */
327 fatal("Bogus return (%d) from select()", rc);
333 if (result == 0 && *timeoutp > 0) {
334 ms_subtract_diff(&t_start, timeoutp);
335 if (*timeoutp <= 0) {
345 * Opens a TCP/IP connection to the remote server on the given host.
346 * The address of the remote host will be returned in hostaddr.
347 * If port is 0, the default port will be used. If needpriv is true,
348 * a privileged port will be allocated to make the connection.
349 * This requires super-user privileges if needpriv is true.
350 * Connection_attempts specifies the maximum number of tries (one per
351 * second). If proxy_command is non-NULL, it specifies the command (with %h
352 * and %p substituted for host and port, respectively) to use to contact
356 ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
357 u_short port, int family, int connection_attempts, int *timeout_ms,
358 int want_keepalive, int needpriv, const char *proxy_command)
362 int sock = -1, attempt;
363 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
364 struct addrinfo hints, *ai, *aitop;
366 debug2("ssh_connect: needpriv %d", needpriv);
368 /* If a proxy command is given, connect using it. */
369 if (proxy_command != NULL)
370 return ssh_proxy_connect(host, port, proxy_command);
372 /* No proxy command. */
374 memset(&hints, 0, sizeof(hints));
375 hints.ai_family = family;
376 hints.ai_socktype = SOCK_STREAM;
377 snprintf(strport, sizeof strport, "%u", port);
378 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
379 fatal("%s: Could not resolve hostname %.100s: %s", __progname,
380 host, ssh_gai_strerror(gaierr));
382 for (attempt = 0; attempt < connection_attempts; attempt++) {
384 /* Sleep a moment before retrying. */
386 debug("Trying again...");
389 * Loop through addresses for this host, and try each one in
390 * sequence until the connection succeeds.
392 for (ai = aitop; ai; ai = ai->ai_next) {
393 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
395 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
396 ntop, sizeof(ntop), strport, sizeof(strport),
397 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
398 error("ssh_connect: getnameinfo failed");
401 debug("Connecting to %.200s [%.100s] port %s.",
402 host, ntop, strport);
404 /* Create a socket for connecting. */
405 sock = ssh_create_socket(needpriv, ai);
407 /* Any error is already output */
410 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
412 /* Successful connection. */
413 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
416 debug("connect to address %s port %s: %s",
417 ntop, strport, strerror(errno));
423 break; /* Successful connection. */
428 /* Return failure if we didn't get a successful connection. */
430 error("ssh: connect to host %s port %s: %s",
431 host, strport, strerror(errno));
435 debug("Connection established.");
437 /* Set SO_KEEPALIVE if requested. */
438 if (want_keepalive &&
439 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
441 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
443 /* Set the connection. */
444 packet_set_connection(sock, sock);
445 packet_set_timeout(options.server_alive_interval,
446 options.server_alive_count_max);
452 * Waits for the server identification string, and sends our own
453 * identification string.
456 ssh_exchange_identification(int timeout_ms)
458 char buf[256], remote_version[256]; /* must be same size! */
459 int remote_major, remote_minor, mismatch;
460 int connection_in = packet_get_connection_in();
461 int connection_out = packet_get_connection_out();
462 int minor1 = PROTOCOL_MINOR_1;
465 int fdsetsz, remaining, rc;
466 struct timeval t_start, t_remaining;
469 fdsetsz = howmany(connection_in + 1, NFDBITS) * sizeof(fd_mask);
470 fdset = xcalloc(1, fdsetsz);
472 /* Read other side's version identification. */
473 remaining = timeout_ms;
475 for (i = 0; i < sizeof(buf) - 1; i++) {
476 if (timeout_ms > 0) {
477 gettimeofday(&t_start, NULL);
478 ms_to_timeval(&t_remaining, remaining);
479 FD_SET(connection_in, fdset);
480 rc = select(connection_in + 1, fdset, NULL,
481 fdset, &t_remaining);
482 ms_subtract_diff(&t_start, &remaining);
483 if (rc == 0 || remaining <= 0)
484 fatal("Connection timed out during "
489 fatal("ssh_exchange_identification: "
490 "select: %s", strerror(errno));
494 len = atomicio(read, connection_in, &buf[i], 1);
496 if (len != 1 && errno == EPIPE)
497 fatal("ssh_exchange_identification: "
498 "Connection closed by remote host");
500 fatal("ssh_exchange_identification: "
501 "read: %.100s", strerror(errno));
502 if (buf[i] == '\r') {
505 continue; /**XXX wait for \n */
507 if (buf[i] == '\n') {
512 fatal("ssh_exchange_identification: "
513 "No banner received");
515 buf[sizeof(buf) - 1] = 0;
516 if (strncmp(buf, "SSH-", 4) == 0)
518 debug("ssh_exchange_identification: %s", buf);
520 server_version_string = xstrdup(buf);
524 * Check that the versions match. In future this might accept
525 * several versions and set appropriate flags to handle them.
527 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
528 &remote_major, &remote_minor, remote_version) != 3)
529 fatal("Bad remote protocol version identification: '%.100s'", buf);
530 debug("Remote protocol version %d.%d, remote software version %.100s",
531 remote_major, remote_minor, remote_version);
533 compat_datafellows(remote_version);
536 switch (remote_major) {
538 if (remote_minor == 99 &&
539 (options.protocol & SSH_PROTO_2) &&
540 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
544 if (!(options.protocol & SSH_PROTO_1)) {
548 if (remote_minor < 3) {
549 fatal("Remote machine has too old SSH software version.");
550 } else if (remote_minor == 3 || remote_minor == 4) {
551 /* We speak 1.3, too. */
554 if (options.forward_agent) {
555 logit("Agent forwarding disabled for protocol 1.3");
556 options.forward_agent = 0;
561 if (options.protocol & SSH_PROTO_2) {
571 fatal("Protocol major versions differ: %d vs. %d",
572 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
574 /* Send our own protocol version identification. */
575 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s%s",
576 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
577 compat20 ? PROTOCOL_MINOR_2 : minor1,
578 SSH_RELEASE, compat20 ? "\r\n" : "\n");
579 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
580 fatal("write: %.100s", strerror(errno));
581 client_version_string = xstrdup(buf);
582 chop(client_version_string);
583 chop(server_version_string);
584 debug("Local version string %.100s", client_version_string);
587 /* defaults to 'no' */
589 confirm(const char *prompt)
591 const char *msg, *again = "Please type 'yes' or 'no': ";
595 if (options.batch_mode)
597 for (msg = prompt;;msg = again) {
598 p = read_passphrase(msg, RP_ECHO);
600 (p[0] == '\0') || (p[0] == '\n') ||
601 strncasecmp(p, "no", 2) == 0)
603 if (p && strncasecmp(p, "yes", 3) == 0)
613 * check whether the supplied host key is valid, return -1 if the key
614 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
620 check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
621 Key *host_key, int readonly, const char *user_hostfile,
622 const char *system_hostfile)
625 const char *type = key_type(host_key);
626 char *ip = NULL, *host = NULL;
627 char hostline[1000], *hostp, *fp, *ra;
628 HostStatus host_status;
629 HostStatus ip_status;
630 int r, local = 0, host_ip_differ = 0;
632 char ntop[NI_MAXHOST];
634 int len, host_line, ip_line, cancelled_forwarding = 0;
635 const char *host_file = NULL, *ip_file = NULL;
638 * Force accepting of the host key for loopback/localhost. The
639 * problem is that if the home directory is NFS-mounted to multiple
640 * machines, localhost will refer to a different machine in each of
641 * them, and the user will get bogus HOST_CHANGED warnings. This
642 * essentially disables host authentication for localhost; however,
643 * this is probably not a real problem.
645 /** hostaddr == 0! */
646 switch (hostaddr->sa_family) {
648 local = (ntohl(((struct sockaddr_in *)hostaddr)->
649 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
650 salen = sizeof(struct sockaddr_in);
653 local = IN6_IS_ADDR_LOOPBACK(
654 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
655 salen = sizeof(struct sockaddr_in6);
659 salen = sizeof(struct sockaddr_storage);
662 if (options.no_host_authentication_for_localhost == 1 && local &&
663 options.host_key_alias == NULL) {
664 debug("Forcing accepting of host key for "
665 "loopback/localhost.");
670 * We don't have the remote ip-address for connections
671 * using a proxy command
673 if (options.proxy_command == NULL) {
674 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
675 NULL, 0, NI_NUMERICHOST) != 0)
676 fatal("check_host_key: getnameinfo failed");
677 ip = put_host_port(ntop, port);
679 ip = xstrdup("<no hostip for proxy command>");
683 * Turn off check_host_ip if the connection is to localhost, via proxy
684 * command or if we don't have a hostname to compare with
686 if (options.check_host_ip && (local ||
687 strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
688 options.check_host_ip = 0;
691 * Allow the user to record the key under a different name or
692 * differentiate a non-standard port. This is useful for ssh
693 * tunneling over forwarded connections or if you run multiple
694 * sshd's on different ports on the same machine.
696 if (options.host_key_alias != NULL) {
697 host = xstrdup(options.host_key_alias);
698 debug("using hostkeyalias: %s", host);
700 host = put_host_port(hostname, port);
704 * Store the host key from the known host file in here so that we can
705 * compare it with the key for the IP address.
707 file_key = key_new(host_key->type);
710 * Check if the host key is present in the user's list of known
711 * hosts or in the systemwide list.
713 host_file = user_hostfile;
714 host_status = check_host_in_hostfile(host_file, host, host_key,
715 file_key, &host_line);
716 if (host_status == HOST_NEW) {
717 host_file = system_hostfile;
718 host_status = check_host_in_hostfile(host_file, host, host_key,
719 file_key, &host_line);
722 * Also perform check for the ip address, skip the check if we are
723 * localhost or the hostname was an ip address to begin with
725 if (options.check_host_ip) {
726 Key *ip_key = key_new(host_key->type);
728 ip_file = user_hostfile;
729 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
731 if (ip_status == HOST_NEW) {
732 ip_file = system_hostfile;
733 ip_status = check_host_in_hostfile(ip_file, ip,
734 host_key, ip_key, &ip_line);
736 if (host_status == HOST_CHANGED &&
737 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
742 ip_status = host_status;
746 switch (host_status) {
748 /* The host is known and the key matches. */
749 debug("Host '%.200s' is known and matches the %s host key.",
751 debug("Found key in %s:%d", host_file, host_line);
752 if (options.check_host_ip && ip_status == HOST_NEW) {
754 logit("%s host key for IP address "
755 "'%.128s' not in list of known hosts.",
757 else if (!add_host_to_hostfile(user_hostfile, ip,
758 host_key, options.hash_known_hosts))
759 logit("Failed to add the %s host key for IP "
760 "address '%.128s' to the list of known "
761 "hosts (%.30s).", type, ip, user_hostfile);
763 logit("Warning: Permanently added the %s host "
764 "key for IP address '%.128s' to the list "
765 "of known hosts.", type, ip);
766 } else if (options.visual_host_key) {
767 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
768 ra = key_fingerprint(host_key, SSH_FP_MD5,
770 logit("Host key fingerprint is %s\n%s\n", fp, ra);
776 if (options.host_key_alias == NULL && port != 0 &&
777 port != SSH_DEFAULT_PORT) {
778 debug("checking without port identifier");
779 if (check_host_key(hostname, hostaddr, 0, host_key, 2,
780 user_hostfile, system_hostfile) == 0) {
781 debug("found matching key w/out port");
787 /* The host is new. */
788 if (options.strict_host_key_checking == 1) {
790 * User has requested strict host key checking. We
791 * will not add the host key automatically. The only
792 * alternative left is to abort.
794 error("No %s host key is known for %.200s and you "
795 "have requested strict checking.", type, host);
797 } else if (options.strict_host_key_checking == 2) {
798 char msg1[1024], msg2[1024];
800 if (show_other_keys(host, host_key))
801 snprintf(msg1, sizeof(msg1),
802 "\nbut keys of different type are already"
803 " known for this host.");
805 snprintf(msg1, sizeof(msg1), ".");
807 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
808 ra = key_fingerprint(host_key, SSH_FP_MD5,
811 if (options.verify_host_key_dns) {
812 if (matching_host_key_dns)
813 snprintf(msg2, sizeof(msg2),
814 "Matching host key fingerprint"
817 snprintf(msg2, sizeof(msg2),
818 "No matching host key fingerprint"
821 snprintf(msg, sizeof(msg),
822 "The authenticity of host '%.200s (%s)' can't be "
824 "%s key fingerprint is %s.%s%s\n%s"
825 "Are you sure you want to continue connecting "
827 host, ip, msg1, type, fp,
828 options.visual_host_key ? "\n" : "",
829 options.visual_host_key ? ra : "",
837 * If not in strict mode, add the key automatically to the
838 * local known_hosts file.
840 if (options.check_host_ip && ip_status == HOST_NEW) {
841 snprintf(hostline, sizeof(hostline), "%s,%s",
844 if (options.hash_known_hosts) {
845 /* Add hash of host and IP separately */
846 r = add_host_to_hostfile(user_hostfile, host,
847 host_key, options.hash_known_hosts) &&
848 add_host_to_hostfile(user_hostfile, ip,
849 host_key, options.hash_known_hosts);
851 /* Add unhashed "host,ip" */
852 r = add_host_to_hostfile(user_hostfile,
854 options.hash_known_hosts);
857 r = add_host_to_hostfile(user_hostfile, host, host_key,
858 options.hash_known_hosts);
863 logit("Failed to add the host to the list of known "
864 "hosts (%.500s).", user_hostfile);
866 logit("Warning: Permanently added '%.200s' (%s) to the "
867 "list of known hosts.", hostp, type);
870 if (readonly == ROQUIET)
872 if (options.check_host_ip && host_ip_differ) {
874 if (ip_status == HOST_NEW)
875 key_msg = "is unknown";
876 else if (ip_status == HOST_OK)
877 key_msg = "is unchanged";
879 key_msg = "has a different value";
880 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
881 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
882 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
883 error("The %s host key for %s has changed,", type, host);
884 error("and the key for the corresponding IP address %s", ip);
885 error("%s. This could either mean that", key_msg);
886 error("DNS SPOOFING is happening or the IP address for the host");
887 error("and its host key have changed at the same time.");
888 if (ip_status != HOST_NEW)
889 error("Offending key for IP in %s:%d", ip_file, ip_line);
891 /* The host key has changed. */
892 warn_changed_key(host_key);
893 error("Add correct host key in %.100s to get rid of this message.",
895 error("Offending key in %s:%d", host_file, host_line);
898 * If strict host key checking is in use, the user will have
899 * to edit the key manually and we can only abort.
901 if (options.strict_host_key_checking) {
902 error("%s host key for %.200s has changed and you have "
903 "requested strict checking.", type, host);
908 * If strict host key checking has not been requested, allow
909 * the connection but without MITM-able authentication or
912 if (options.password_authentication) {
913 error("Password authentication is disabled to avoid "
914 "man-in-the-middle attacks.");
915 options.password_authentication = 0;
916 cancelled_forwarding = 1;
918 if (options.kbd_interactive_authentication) {
919 error("Keyboard-interactive authentication is disabled"
920 " to avoid man-in-the-middle attacks.");
921 options.kbd_interactive_authentication = 0;
922 options.challenge_response_authentication = 0;
923 cancelled_forwarding = 1;
925 if (options.challenge_response_authentication) {
926 error("Challenge/response authentication is disabled"
927 " to avoid man-in-the-middle attacks.");
928 options.challenge_response_authentication = 0;
929 cancelled_forwarding = 1;
931 if (options.forward_agent) {
932 error("Agent forwarding is disabled to avoid "
933 "man-in-the-middle attacks.");
934 options.forward_agent = 0;
935 cancelled_forwarding = 1;
937 if (options.forward_x11) {
938 error("X11 forwarding is disabled to avoid "
939 "man-in-the-middle attacks.");
940 options.forward_x11 = 0;
941 cancelled_forwarding = 1;
943 if (options.num_local_forwards > 0 ||
944 options.num_remote_forwards > 0) {
945 error("Port forwarding is disabled to avoid "
946 "man-in-the-middle attacks.");
947 options.num_local_forwards =
948 options.num_remote_forwards = 0;
949 cancelled_forwarding = 1;
951 if (options.tun_open != SSH_TUNMODE_NO) {
952 error("Tunnel forwarding is disabled to avoid "
953 "man-in-the-middle attacks.");
954 options.tun_open = SSH_TUNMODE_NO;
955 cancelled_forwarding = 1;
957 if (options.exit_on_forward_failure && cancelled_forwarding)
958 fatal("Error: forwarding disabled due to host key "
962 * XXX Should permit the user to change to use the new id.
963 * This could be done by converting the host key to an
964 * identifying sentence, tell that the host identifies itself
965 * by that sentence, and ask the user if he/she whishes to
966 * accept the authentication.
970 fatal("internal error");
974 if (options.check_host_ip && host_status != HOST_CHANGED &&
975 ip_status == HOST_CHANGED) {
976 snprintf(msg, sizeof(msg),
977 "Warning: the %s host key for '%.200s' "
978 "differs from the key for the IP address '%.128s'"
979 "\nOffending key for IP in %s:%d",
980 type, host, ip, ip_file, ip_line);
981 if (host_status == HOST_OK) {
983 snprintf(msg + len, sizeof(msg) - len,
984 "\nMatching host key in %s:%d",
985 host_file, host_line);
987 if (options.strict_host_key_checking == 1) {
989 error("Exiting, you have requested strict checking.");
991 } else if (options.strict_host_key_checking == 2) {
992 strlcat(msg, "\nAre you sure you want "
993 "to continue connecting (yes/no)? ", sizeof(msg));
1011 /* returns 0 if key verifies or -1 if key does NOT verify */
1013 verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
1018 if (options.verify_host_key_dns &&
1019 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
1021 if (flags & DNS_VERIFY_FOUND) {
1023 if (options.verify_host_key_dns == 1 &&
1024 flags & DNS_VERIFY_MATCH &&
1025 flags & DNS_VERIFY_SECURE)
1028 if (flags & DNS_VERIFY_MATCH) {
1029 matching_host_key_dns = 1;
1031 warn_changed_key(host_key);
1032 error("Update the SSHFP RR in DNS with the new "
1033 "host key to get rid of this message.");
1038 /* return ok if the key can be found in an old keyfile */
1039 if (stat(options.system_hostfile2, &st) == 0 ||
1040 stat(options.user_hostfile2, &st) == 0) {
1041 if (check_host_key(host, hostaddr, options.port, host_key,
1042 RDONLY, options.user_hostfile2,
1043 options.system_hostfile2) == 0)
1046 return check_host_key(host, hostaddr, options.port, host_key,
1047 RDRW, options.user_hostfile, options.system_hostfile);
1051 * Starts a dialog with the server, and authenticates the current user on the
1052 * server. This does not need any extra privileges. The basic connection
1053 * to the server must already have been established before this is called.
1054 * If login fails, this function prints an error and never returns.
1055 * This function does not require super-user privileges.
1058 ssh_login(Sensitive *sensitive, const char *orighost,
1059 struct sockaddr *hostaddr, struct passwd *pw, int timeout_ms)
1062 char *server_user, *local_user;
1064 local_user = xstrdup(pw->pw_name);
1065 server_user = options.user ? options.user : local_user;
1067 /* Convert the user-supplied hostname into all lowercase. */
1068 host = xstrdup(orighost);
1069 for (cp = host; *cp; cp++)
1071 *cp = (char)tolower(*cp);
1073 /* Exchange protocol version identification strings with the server. */
1074 ssh_exchange_identification(timeout_ms);
1076 /* Put the connection into non-blocking mode. */
1077 packet_set_nonblocking();
1080 /* authenticate user */
1082 ssh_kex2(host, hostaddr);
1083 ssh_userauth2(local_user, server_user, host, sensitive);
1085 ssh_kex(host, hostaddr);
1086 ssh_userauth1(local_user, server_user, host, sensitive);
1092 ssh_put_password(char *password)
1097 if (datafellows & SSH_BUG_PASSWORDPAD) {
1098 packet_put_cstring(password);
1101 size = roundup(strlen(password) + 1, 32);
1102 padded = xcalloc(1, size);
1103 strlcpy(padded, password, size);
1104 packet_put_string(padded, size);
1105 memset(padded, 0, size);
1110 show_key_from_file(const char *file, const char *host, int keytype)
1116 found = key_new(keytype);
1117 if ((ret = lookup_key_in_hostfile_by_type(file, host,
1118 keytype, found, &line))) {
1119 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1120 ra = key_fingerprint(found, SSH_FP_MD5, SSH_FP_RANDOMART);
1121 logit("WARNING: %s key found for host %s\n"
1123 "%s key fingerprint %s.\n%s\n",
1124 key_type(found), host, file, line,
1125 key_type(found), fp, ra);
1133 /* print all known host keys for a given host, but skip keys of given type */
1135 show_other_keys(const char *host, Key *key)
1137 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
1140 for (i = 0; type[i] != -1; i++) {
1141 if (type[i] == key->type)
1143 if (type[i] != KEY_RSA1 &&
1144 show_key_from_file(options.user_hostfile2, host, type[i])) {
1148 if (type[i] != KEY_RSA1 &&
1149 show_key_from_file(options.system_hostfile2, host, type[i])) {
1153 if (show_key_from_file(options.user_hostfile, host, type[i])) {
1157 if (show_key_from_file(options.system_hostfile, host, type[i])) {
1161 debug2("no key of type %d for host %s", type[i], host);
1167 warn_changed_key(Key *host_key)
1170 const char *type = key_type(host_key);
1172 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1174 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1175 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1176 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1177 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1178 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1179 error("It is also possible that the %s host key has just been changed.", type);
1180 error("The fingerprint for the %s key sent by the remote host is\n%s.",
1182 error("Please contact your system administrator.");
1188 * Execute a local command
1191 ssh_local_cmd(const char *args)
1197 if (!options.permit_local_command ||
1198 args == NULL || !*args)
1201 if ((shell = getenv("SHELL")) == NULL)
1202 shell = _PATH_BSHELL;
1206 debug3("Executing %s -c \"%s\"", shell, args);
1207 execl(shell, shell, "-c", args, (char *)NULL);
1208 error("Couldn't execute %s -c \"%s\": %s",
1209 shell, args, strerror(errno));
1211 } else if (pid == -1)
1212 fatal("fork failed: %.100s", strerror(errno));
1213 while (waitpid(pid, &status, 0) == -1)
1215 fatal("Couldn't wait for child: %s", strerror(errno));
1217 if (!WIFEXITED(status))
1220 return (WEXITSTATUS(status));