1 /* $OpenBSD: sshconnect.c,v 1.199 2006/08/03 03:34:42 deraadt 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 strport[NI_MAXSERV];
91 /* Convert the port number into a string. */
92 snprintf(strport, sizeof strport, "%hu", port);
95 * Build the final command string in the buffer by making the
96 * appropriate substitutions to the given proxy command.
98 * Use "exec" to avoid "sh -c" processes on some platforms
101 xasprintf(&tmp, "exec %s", proxy_command);
102 command_string = percent_expand(tmp, "h", host,
103 "p", strport, (char *)NULL);
106 /* Create pipes for communicating with the proxy. */
107 if (pipe(pin) < 0 || pipe(pout) < 0)
108 fatal("Could not create pipes to communicate with the proxy: %.100s",
111 debug("Executing proxy command: %.500s", command_string);
113 /* Fork and execute the proxy command. */
114 if ((pid = fork()) == 0) {
117 /* Child. Permanently give up superuser privileges. */
118 permanently_drop_suid(original_real_uid);
120 /* Redirect stdin and stdout. */
123 if (dup2(pin[0], 0) < 0)
124 perror("dup2 stdin");
128 if (dup2(pout[1], 1) < 0)
129 perror("dup2 stdout");
130 /* Cannot be 1 because pin allocated two descriptors. */
133 /* Stderr is left as it is so that error messages get
134 printed on the user's terminal. */
135 argv[0] = _PATH_BSHELL;
137 argv[2] = command_string;
140 /* Execute the proxy command. Note that we gave up any
141 extra privileges above. */
142 execv(argv[0], argv);
148 fatal("fork failed: %.100s", strerror(errno));
150 proxy_command_pid = pid; /* save pid to clean up later */
152 /* Close child side of the descriptors. */
156 /* Free the command name. */
157 xfree(command_string);
159 /* Set the connection file descriptors. */
160 packet_set_connection(pout[0], pin[1]);
162 /* Indicate OK return */
167 * Set TCP receive buffer if requested.
168 * Note: tuning needs to happen after the socket is
169 * created but before the connection happens
170 * so winscale is negotiated properly -cjr
173 ssh_set_socket_recvbuf(int sock)
175 void *buf = (void *)&options.tcp_rcv_buf;
176 int sz = sizeof(options.tcp_rcv_buf);
178 int socksizelen = sizeof(int);
180 debug("setsockopt Attempting to set SO_RCVBUF to %d", options.tcp_rcv_buf);
181 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, buf, sz) >= 0) {
182 getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &socksize, &socksizelen);
183 debug("setsockopt SO_RCVBUF: %.100s %d", strerror(errno), socksize);
186 error("Couldn't set socket receive buffer to %d: %.100s",
187 options.tcp_rcv_buf, strerror(errno));
192 * Creates a (possibly privileged) socket for use as the ssh connection.
195 ssh_create_socket(int privileged, struct addrinfo *ai)
198 struct addrinfo hints, *res;
201 * If we are running as root and want to connect to a privileged
202 * port, bind our own socket to a privileged port.
205 int p = IPPORT_RESERVED - 1;
207 sock = rresvport_af(&p, ai->ai_family);
210 error("rresvport: af=%d %.100s", ai->ai_family,
213 debug("Allocated local port %d.", p);
215 if (options.tcp_rcv_buf > 0)
216 ssh_set_socket_recvbuf(sock);
219 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
221 error("socket: %.100s", strerror(errno));
223 if (options.tcp_rcv_buf > 0)
224 ssh_set_socket_recvbuf(sock);
226 /* Bind the socket to an alternative local IP address */
227 if (options.bind_address == NULL)
230 memset(&hints, 0, sizeof(hints));
231 hints.ai_family = ai->ai_family;
232 hints.ai_socktype = ai->ai_socktype;
233 hints.ai_protocol = ai->ai_protocol;
234 hints.ai_flags = AI_PASSIVE;
235 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
237 error("getaddrinfo: %s: %s", options.bind_address,
238 gai_strerror(gaierr));
242 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
243 error("bind: %s: %s", options.bind_address, strerror(errno));
253 timeout_connect(int sockfd, const struct sockaddr *serv_addr,
254 socklen_t addrlen, int timeout)
259 int optval, rc, result = -1;
262 return (connect(sockfd, serv_addr, addrlen));
264 set_nonblock(sockfd);
265 rc = connect(sockfd, serv_addr, addrlen);
267 unset_nonblock(sockfd);
270 if (errno != EINPROGRESS)
273 fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
275 FD_SET(sockfd, fdset);
280 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
281 if (rc != -1 || errno != EINTR)
292 debug("select: %s", strerror(errno));
295 /* Completed or failed */
297 optlen = sizeof(optval);
298 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
300 debug("getsockopt: %s", strerror(errno));
308 unset_nonblock(sockfd);
311 /* Should not occur */
312 fatal("Bogus return (%d) from select()", rc);
320 * Opens a TCP/IP connection to the remote server on the given host.
321 * The address of the remote host will be returned in hostaddr.
322 * If port is 0, the default port will be used. If needpriv is true,
323 * a privileged port will be allocated to make the connection.
324 * This requires super-user privileges if needpriv is true.
325 * Connection_attempts specifies the maximum number of tries (one per
326 * second). If proxy_command is non-NULL, it specifies the command (with %h
327 * and %p substituted for host and port, respectively) to use to contact
331 ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
332 u_short port, int family, int connection_attempts,
333 int needpriv, const char *proxy_command)
337 int sock = -1, attempt;
338 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
339 struct addrinfo hints, *ai, *aitop;
341 debug2("ssh_connect: needpriv %d", needpriv);
343 /* If a proxy command is given, connect using it. */
344 if (proxy_command != NULL)
345 return ssh_proxy_connect(host, port, proxy_command);
347 /* No proxy command. */
349 memset(&hints, 0, sizeof(hints));
350 hints.ai_family = family;
351 hints.ai_socktype = SOCK_STREAM;
352 snprintf(strport, sizeof strport, "%u", port);
353 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
354 fatal("%s: %.100s: %s", __progname, host,
355 gai_strerror(gaierr));
357 for (attempt = 0; attempt < connection_attempts; attempt++) {
359 debug("Trying again...");
362 * Loop through addresses for this host, and try each one in
363 * sequence until the connection succeeds.
365 for (ai = aitop; ai; ai = ai->ai_next) {
366 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
368 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
369 ntop, sizeof(ntop), strport, sizeof(strport),
370 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
371 error("ssh_connect: getnameinfo failed");
374 debug("Connecting to %.200s [%.100s] port %s.",
375 host, ntop, strport);
377 /* Create a socket for connecting. */
378 sock = ssh_create_socket(needpriv, ai);
380 /* Any error is already output */
383 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
384 options.connection_timeout) >= 0) {
385 /* Successful connection. */
386 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
389 debug("connect to address %s port %s: %s",
390 ntop, strport, strerror(errno));
396 break; /* Successful connection. */
398 /* Sleep a moment before retrying. */
404 /* Return failure if we didn't get a successful connection. */
406 error("ssh: connect to host %s port %s: %s",
407 host, strport, strerror(errno));
411 debug("Connection established.");
413 /* Set SO_KEEPALIVE if requested. */
414 if (options.tcp_keep_alive &&
415 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
417 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
419 /* Set the connection. */
420 packet_set_connection(sock, sock);
426 * Waits for the server identification string, and sends our own
427 * identification string.
430 ssh_exchange_identification(void)
432 char buf[256], remote_version[256]; /* must be same size! */
433 int remote_major, remote_minor, mismatch;
434 int connection_in = packet_get_connection_in();
435 int connection_out = packet_get_connection_out();
436 int minor1 = PROTOCOL_MINOR_1;
439 /* Read other side's version identification. */
441 for (i = 0; i < sizeof(buf) - 1; i++) {
442 size_t len = atomicio(read, connection_in, &buf[i], 1);
444 if (len != 1 && errno == EPIPE)
445 fatal("ssh_exchange_identification: Connection closed by remote host");
447 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
448 if (buf[i] == '\r') {
451 continue; /**XXX wait for \n */
453 if (buf[i] == '\n') {
458 fatal("ssh_exchange_identification: No banner received");
460 buf[sizeof(buf) - 1] = 0;
461 if (strncmp(buf, "SSH-", 4) == 0)
463 debug("ssh_exchange_identification: %s", buf);
465 server_version_string = xstrdup(buf);
468 * Check that the versions match. In future this might accept
469 * several versions and set appropriate flags to handle them.
471 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
472 &remote_major, &remote_minor, remote_version) != 3)
473 fatal("Bad remote protocol version identification: '%.100s'", buf);
474 debug("Remote protocol version %d.%d, remote software version %.100s",
475 remote_major, remote_minor, remote_version);
477 compat_datafellows(remote_version);
480 switch (remote_major) {
482 if (remote_minor == 99 &&
483 (options.protocol & SSH_PROTO_2) &&
484 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
488 if (!(options.protocol & SSH_PROTO_1)) {
492 if (remote_minor < 3) {
493 fatal("Remote machine has too old SSH software version.");
494 } else if (remote_minor == 3 || remote_minor == 4) {
495 /* We speak 1.3, too. */
498 if (options.forward_agent) {
499 logit("Agent forwarding disabled for protocol 1.3");
500 options.forward_agent = 0;
505 if (options.protocol & SSH_PROTO_2) {
515 fatal("Protocol major versions differ: %d vs. %d",
516 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
518 /* Send our own protocol version identification. */
519 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
520 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
521 compat20 ? PROTOCOL_MINOR_2 : minor1,
523 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
524 fatal("write: %.100s", strerror(errno));
525 client_version_string = xstrdup(buf);
526 chop(client_version_string);
527 chop(server_version_string);
528 debug("Local version string %.100s", client_version_string);
531 /* defaults to 'no' */
533 confirm(const char *prompt)
535 const char *msg, *again = "Please type 'yes' or 'no': ";
539 if (options.batch_mode)
541 for (msg = prompt;;msg = again) {
542 p = read_passphrase(msg, RP_ECHO);
544 (p[0] == '\0') || (p[0] == '\n') ||
545 strncasecmp(p, "no", 2) == 0)
547 if (p && strncasecmp(p, "yes", 3) == 0)
557 * check whether the supplied host key is valid, return -1 if the key
558 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
564 check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
565 Key *host_key, int readonly, const char *user_hostfile,
566 const char *system_hostfile)
569 const char *type = key_type(host_key);
570 char *ip = NULL, *host = NULL;
571 char hostline[1000], *hostp, *fp;
572 HostStatus host_status;
573 HostStatus ip_status;
574 int r, local = 0, host_ip_differ = 0;
576 char ntop[NI_MAXHOST];
578 int len, host_line, ip_line;
579 const char *host_file = NULL, *ip_file = NULL;
582 * Force accepting of the host key for loopback/localhost. The
583 * problem is that if the home directory is NFS-mounted to multiple
584 * machines, localhost will refer to a different machine in each of
585 * them, and the user will get bogus HOST_CHANGED warnings. This
586 * essentially disables host authentication for localhost; however,
587 * this is probably not a real problem.
589 /** hostaddr == 0! */
590 switch (hostaddr->sa_family) {
592 local = (ntohl(((struct sockaddr_in *)hostaddr)->
593 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
594 salen = sizeof(struct sockaddr_in);
597 local = IN6_IS_ADDR_LOOPBACK(
598 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
599 salen = sizeof(struct sockaddr_in6);
603 salen = sizeof(struct sockaddr_storage);
606 if (options.no_host_authentication_for_localhost == 1 && local &&
607 options.host_key_alias == NULL) {
608 debug("Forcing accepting of host key for "
609 "loopback/localhost.");
614 * We don't have the remote ip-address for connections
615 * using a proxy command
617 if (options.proxy_command == NULL) {
618 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
619 NULL, 0, NI_NUMERICHOST) != 0)
620 fatal("check_host_key: getnameinfo failed");
621 ip = put_host_port(ntop, port);
623 ip = xstrdup("<no hostip for proxy command>");
626 * Turn off check_host_ip if the connection is to localhost, via proxy
627 * command or if we don't have a hostname to compare with
629 if (options.check_host_ip && (local ||
630 strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
631 options.check_host_ip = 0;
634 * Allow the user to record the key under a different name or
635 * differentiate a non-standard port. This is useful for ssh
636 * tunneling over forwarded connections or if you run multiple
637 * sshd's on different ports on the same machine.
639 if (options.host_key_alias != NULL) {
640 host = xstrdup(options.host_key_alias);
641 debug("using hostkeyalias: %s", host);
643 host = put_host_port(hostname, port);
647 * Store the host key from the known host file in here so that we can
648 * compare it with the key for the IP address.
650 file_key = key_new(host_key->type);
653 * Check if the host key is present in the user's list of known
654 * hosts or in the systemwide list.
656 host_file = user_hostfile;
657 host_status = check_host_in_hostfile(host_file, host, host_key,
658 file_key, &host_line);
659 if (host_status == HOST_NEW) {
660 host_file = system_hostfile;
661 host_status = check_host_in_hostfile(host_file, host, host_key,
662 file_key, &host_line);
665 * Also perform check for the ip address, skip the check if we are
666 * localhost or the hostname was an ip address to begin with
668 if (options.check_host_ip) {
669 Key *ip_key = key_new(host_key->type);
671 ip_file = user_hostfile;
672 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
674 if (ip_status == HOST_NEW) {
675 ip_file = system_hostfile;
676 ip_status = check_host_in_hostfile(ip_file, ip,
677 host_key, ip_key, &ip_line);
679 if (host_status == HOST_CHANGED &&
680 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
685 ip_status = host_status;
689 switch (host_status) {
691 /* The host is known and the key matches. */
692 debug("Host '%.200s' is known and matches the %s host key.",
694 debug("Found key in %s:%d", host_file, host_line);
695 if (options.check_host_ip && ip_status == HOST_NEW) {
697 logit("%s host key for IP address "
698 "'%.128s' not in list of known hosts.",
700 else if (!add_host_to_hostfile(user_hostfile, ip,
701 host_key, options.hash_known_hosts))
702 logit("Failed to add the %s host key for IP "
703 "address '%.128s' to the list of known "
704 "hosts (%.30s).", type, ip, user_hostfile);
706 logit("Warning: Permanently added the %s host "
707 "key for IP address '%.128s' to the list "
708 "of known hosts.", type, ip);
712 if (options.host_key_alias == NULL && port != 0 &&
713 port != SSH_DEFAULT_PORT) {
714 debug("checking without port identifier");
715 if (check_host_key(hostname, hostaddr, 0, host_key, 2,
716 user_hostfile, system_hostfile) == 0) {
717 debug("found matching key w/out port");
723 /* The host is new. */
724 if (options.strict_host_key_checking == 1) {
726 * User has requested strict host key checking. We
727 * will not add the host key automatically. The only
728 * alternative left is to abort.
730 error("No %s host key is known for %.200s and you "
731 "have requested strict checking.", type, host);
733 } else if (options.strict_host_key_checking == 2) {
734 char msg1[1024], msg2[1024];
736 if (show_other_keys(host, host_key))
737 snprintf(msg1, sizeof(msg1),
738 "\nbut keys of different type are already"
739 " known for this host.");
741 snprintf(msg1, sizeof(msg1), ".");
743 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
745 if (options.verify_host_key_dns) {
746 if (matching_host_key_dns)
747 snprintf(msg2, sizeof(msg2),
748 "Matching host key fingerprint"
751 snprintf(msg2, sizeof(msg2),
752 "No matching host key fingerprint"
755 snprintf(msg, sizeof(msg),
756 "The authenticity of host '%.200s (%s)' can't be "
758 "%s key fingerprint is %s.\n%s"
759 "Are you sure you want to continue connecting "
761 host, ip, msg1, type, fp, msg2);
767 * If not in strict mode, add the key automatically to the
768 * local known_hosts file.
770 if (options.check_host_ip && ip_status == HOST_NEW) {
771 snprintf(hostline, sizeof(hostline), "%s,%s",
774 if (options.hash_known_hosts) {
775 /* Add hash of host and IP separately */
776 r = add_host_to_hostfile(user_hostfile, host,
777 host_key, options.hash_known_hosts) &&
778 add_host_to_hostfile(user_hostfile, ip,
779 host_key, options.hash_known_hosts);
781 /* Add unhashed "host,ip" */
782 r = add_host_to_hostfile(user_hostfile,
784 options.hash_known_hosts);
787 r = add_host_to_hostfile(user_hostfile, host, host_key,
788 options.hash_known_hosts);
793 logit("Failed to add the host to the list of known "
794 "hosts (%.500s).", user_hostfile);
796 logit("Warning: Permanently added '%.200s' (%s) to the "
797 "list of known hosts.", hostp, type);
800 if (readonly == ROQUIET)
802 if (options.check_host_ip && host_ip_differ) {
804 if (ip_status == HOST_NEW)
805 key_msg = "is unknown";
806 else if (ip_status == HOST_OK)
807 key_msg = "is unchanged";
809 key_msg = "has a different value";
810 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
811 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
812 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
813 error("The %s host key for %s has changed,", type, host);
814 error("and the key for the according IP address %s", ip);
815 error("%s. This could either mean that", key_msg);
816 error("DNS SPOOFING is happening or the IP address for the host");
817 error("and its host key have changed at the same time.");
818 if (ip_status != HOST_NEW)
819 error("Offending key for IP in %s:%d", ip_file, ip_line);
821 /* The host key has changed. */
822 warn_changed_key(host_key);
823 error("Add correct host key in %.100s to get rid of this message.",
825 error("Offending key in %s:%d", host_file, host_line);
828 * If strict host key checking is in use, the user will have
829 * to edit the key manually and we can only abort.
831 if (options.strict_host_key_checking) {
832 error("%s host key for %.200s has changed and you have "
833 "requested strict checking.", type, host);
838 * If strict host key checking has not been requested, allow
839 * the connection but without MITM-able authentication or
842 if (options.password_authentication) {
843 error("Password authentication is disabled to avoid "
844 "man-in-the-middle attacks.");
845 options.password_authentication = 0;
847 if (options.kbd_interactive_authentication) {
848 error("Keyboard-interactive authentication is disabled"
849 " to avoid man-in-the-middle attacks.");
850 options.kbd_interactive_authentication = 0;
851 options.challenge_response_authentication = 0;
853 if (options.challenge_response_authentication) {
854 error("Challenge/response authentication is disabled"
855 " to avoid man-in-the-middle attacks.");
856 options.challenge_response_authentication = 0;
858 if (options.forward_agent) {
859 error("Agent forwarding is disabled to avoid "
860 "man-in-the-middle attacks.");
861 options.forward_agent = 0;
863 if (options.forward_x11) {
864 error("X11 forwarding is disabled to avoid "
865 "man-in-the-middle attacks.");
866 options.forward_x11 = 0;
868 if (options.num_local_forwards > 0 ||
869 options.num_remote_forwards > 0) {
870 error("Port forwarding is disabled to avoid "
871 "man-in-the-middle attacks.");
872 options.num_local_forwards =
873 options.num_remote_forwards = 0;
875 if (options.tun_open != SSH_TUNMODE_NO) {
876 error("Tunnel forwarding is disabled to avoid "
877 "man-in-the-middle attacks.");
878 options.tun_open = SSH_TUNMODE_NO;
881 * XXX Should permit the user to change to use the new id.
882 * This could be done by converting the host key to an
883 * identifying sentence, tell that the host identifies itself
884 * by that sentence, and ask the user if he/she whishes to
885 * accept the authentication.
889 fatal("internal error");
893 if (options.check_host_ip && host_status != HOST_CHANGED &&
894 ip_status == HOST_CHANGED) {
895 snprintf(msg, sizeof(msg),
896 "Warning: the %s host key for '%.200s' "
897 "differs from the key for the IP address '%.128s'"
898 "\nOffending key for IP in %s:%d",
899 type, host, ip, ip_file, ip_line);
900 if (host_status == HOST_OK) {
902 snprintf(msg + len, sizeof(msg) - len,
903 "\nMatching host key in %s:%d",
904 host_file, host_line);
906 if (options.strict_host_key_checking == 1) {
908 error("Exiting, you have requested strict checking.");
910 } else if (options.strict_host_key_checking == 2) {
911 strlcat(msg, "\nAre you sure you want "
912 "to continue connecting (yes/no)? ", sizeof(msg));
930 /* returns 0 if key verifies or -1 if key does NOT verify */
932 verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
937 if (options.verify_host_key_dns &&
938 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
940 if (flags & DNS_VERIFY_FOUND) {
942 if (options.verify_host_key_dns == 1 &&
943 flags & DNS_VERIFY_MATCH &&
944 flags & DNS_VERIFY_SECURE)
947 if (flags & DNS_VERIFY_MATCH) {
948 matching_host_key_dns = 1;
950 warn_changed_key(host_key);
951 error("Update the SSHFP RR in DNS with the new "
952 "host key to get rid of this message.");
957 /* return ok if the key can be found in an old keyfile */
958 if (stat(options.system_hostfile2, &st) == 0 ||
959 stat(options.user_hostfile2, &st) == 0) {
960 if (check_host_key(host, hostaddr, options.port, host_key,
961 RDONLY, options.user_hostfile2,
962 options.system_hostfile2) == 0)
965 return check_host_key(host, hostaddr, options.port, host_key,
966 RDRW, options.user_hostfile, options.system_hostfile);
970 * Starts a dialog with the server, and authenticates the current user on the
971 * server. This does not need any extra privileges. The basic connection
972 * to the server must already have been established before this is called.
973 * If login fails, this function prints an error and never returns.
974 * This function does not require super-user privileges.
977 ssh_login(Sensitive *sensitive, const char *orighost,
978 struct sockaddr *hostaddr, struct passwd *pw)
981 char *server_user, *local_user;
983 local_user = xstrdup(pw->pw_name);
984 server_user = options.user ? options.user : local_user;
986 /* Convert the user-supplied hostname into all lowercase. */
987 host = xstrdup(orighost);
988 for (cp = host; *cp; cp++)
990 *cp = (char)tolower(*cp);
992 /* Exchange protocol version identification strings with the server. */
993 ssh_exchange_identification();
995 /* Put the connection into non-blocking mode. */
996 packet_set_nonblocking();
999 /* authenticate user */
1001 ssh_kex2(host, hostaddr);
1002 ssh_userauth2(local_user, server_user, host, sensitive);
1004 ssh_kex(host, hostaddr);
1005 ssh_userauth1(local_user, server_user, host, sensitive);
1011 ssh_put_password(char *password)
1016 if (datafellows & SSH_BUG_PASSWORDPAD) {
1017 packet_put_cstring(password);
1020 size = roundup(strlen(password) + 1, 32);
1021 padded = xcalloc(1, size);
1022 strlcpy(padded, password, size);
1023 packet_put_string(padded, size);
1024 memset(padded, 0, size);
1029 show_key_from_file(const char *file, const char *host, int keytype)
1035 found = key_new(keytype);
1036 if ((ret = lookup_key_in_hostfile_by_type(file, host,
1037 keytype, found, &line))) {
1038 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1039 logit("WARNING: %s key found for host %s\n"
1041 "%s key fingerprint %s.",
1042 key_type(found), host, file, line,
1043 key_type(found), fp);
1050 /* print all known host keys for a given host, but skip keys of given type */
1052 show_other_keys(const char *host, Key *key)
1054 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
1057 for (i = 0; type[i] != -1; i++) {
1058 if (type[i] == key->type)
1060 if (type[i] != KEY_RSA1 &&
1061 show_key_from_file(options.user_hostfile2, host, type[i])) {
1065 if (type[i] != KEY_RSA1 &&
1066 show_key_from_file(options.system_hostfile2, host, type[i])) {
1070 if (show_key_from_file(options.user_hostfile, host, type[i])) {
1074 if (show_key_from_file(options.system_hostfile, host, type[i])) {
1078 debug2("no key of type %d for host %s", type[i], host);
1084 warn_changed_key(Key *host_key)
1087 const char *type = key_type(host_key);
1089 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1091 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1092 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1093 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1094 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1095 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1096 error("It is also possible that the %s host key has just been changed.", type);
1097 error("The fingerprint for the %s key sent by the remote host is\n%s.",
1099 error("Please contact your system administrator.");
1105 * Execute a local command
1108 ssh_local_cmd(const char *args)
1114 if (!options.permit_local_command ||
1115 args == NULL || !*args)
1118 if ((shell = getenv("SHELL")) == NULL)
1119 shell = _PATH_BSHELL;
1123 debug3("Executing %s -c \"%s\"", shell, args);
1124 execl(shell, shell, "-c", args, (char *)NULL);
1125 error("Couldn't execute %s -c \"%s\": %s",
1126 shell, args, strerror(errno));
1128 } else if (pid == -1)
1129 fatal("fork failed: %.100s", strerror(errno));
1130 while (waitpid(pid, &status, 0) == -1)
1132 fatal("Couldn't wait for child: %s", strerror(errno));
1134 if (!WIFEXITED(status))
1137 return (WEXITSTATUS(status));