1 /* $OpenBSD: sshconnect.c,v 1.195 2006/07/25 02:59:21 stevesk 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>
46 #include "sshconnect.h"
55 char *client_version_string = NULL;
56 char *server_version_string = NULL;
58 static int matching_host_key_dns = 0;
61 extern Options options;
62 extern char *__progname;
63 extern uid_t original_real_uid;
64 extern uid_t original_effective_uid;
65 extern pid_t proxy_command_pid;
67 #ifndef INET6_ADDRSTRLEN /* for non IPv6 machines */
68 #define INET6_ADDRSTRLEN 46
71 static int show_other_keys(const char *, Key *);
72 static void warn_changed_key(Key *);
75 * Connect to the given ssh server using a proxy command.
78 ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
80 char *command_string, *tmp;
83 char strport[NI_MAXSERV];
85 /* Convert the port number into a string. */
86 snprintf(strport, sizeof strport, "%hu", port);
89 * Build the final command string in the buffer by making the
90 * appropriate substitutions to the given proxy command.
92 * Use "exec" to avoid "sh -c" processes on some platforms
95 xasprintf(&tmp, "exec %s", proxy_command);
96 command_string = percent_expand(tmp, "h", host,
97 "p", strport, (char *)NULL);
100 /* Create pipes for communicating with the proxy. */
101 if (pipe(pin) < 0 || pipe(pout) < 0)
102 fatal("Could not create pipes to communicate with the proxy: %.100s",
105 debug("Executing proxy command: %.500s", command_string);
107 /* Fork and execute the proxy command. */
108 if ((pid = fork()) == 0) {
111 /* Child. Permanently give up superuser privileges. */
112 permanently_drop_suid(original_real_uid);
114 /* Redirect stdin and stdout. */
117 if (dup2(pin[0], 0) < 0)
118 perror("dup2 stdin");
122 if (dup2(pout[1], 1) < 0)
123 perror("dup2 stdout");
124 /* Cannot be 1 because pin allocated two descriptors. */
127 /* Stderr is left as it is so that error messages get
128 printed on the user's terminal. */
129 argv[0] = _PATH_BSHELL;
131 argv[2] = command_string;
134 /* Execute the proxy command. Note that we gave up any
135 extra privileges above. */
136 execv(argv[0], argv);
142 fatal("fork failed: %.100s", strerror(errno));
144 proxy_command_pid = pid; /* save pid to clean up later */
146 /* Close child side of the descriptors. */
150 /* Free the command name. */
151 xfree(command_string);
153 /* Set the connection file descriptors. */
154 packet_set_connection(pout[0], pin[1]);
156 /* Indicate OK return */
161 * Creates a (possibly privileged) socket for use as the ssh connection.
164 ssh_create_socket(int privileged, struct addrinfo *ai)
167 struct addrinfo hints, *res;
170 * If we are running as root and want to connect to a privileged
171 * port, bind our own socket to a privileged port.
174 int p = IPPORT_RESERVED - 1;
176 sock = rresvport_af(&p, ai->ai_family);
179 error("rresvport: af=%d %.100s", ai->ai_family,
182 debug("Allocated local port %d.", p);
185 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
187 error("socket: %.100s", strerror(errno));
189 /* Bind the socket to an alternative local IP address */
190 if (options.bind_address == NULL)
193 memset(&hints, 0, sizeof(hints));
194 hints.ai_family = ai->ai_family;
195 hints.ai_socktype = ai->ai_socktype;
196 hints.ai_protocol = ai->ai_protocol;
197 hints.ai_flags = AI_PASSIVE;
198 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
200 error("getaddrinfo: %s: %s", options.bind_address,
201 gai_strerror(gaierr));
205 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
206 error("bind: %s: %s", options.bind_address, strerror(errno));
216 timeout_connect(int sockfd, const struct sockaddr *serv_addr,
217 socklen_t addrlen, int timeout)
222 int optval, rc, result = -1;
225 return (connect(sockfd, serv_addr, addrlen));
227 set_nonblock(sockfd);
228 rc = connect(sockfd, serv_addr, addrlen);
230 unset_nonblock(sockfd);
233 if (errno != EINPROGRESS)
236 fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
238 FD_SET(sockfd, fdset);
243 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
244 if (rc != -1 || errno != EINTR)
255 debug("select: %s", strerror(errno));
258 /* Completed or failed */
260 optlen = sizeof(optval);
261 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
263 debug("getsockopt: %s", strerror(errno));
271 unset_nonblock(sockfd);
274 /* Should not occur */
275 fatal("Bogus return (%d) from select()", rc);
283 * Opens a TCP/IP connection to the remote server on the given host.
284 * The address of the remote host will be returned in hostaddr.
285 * If port is 0, the default port will be used. If needpriv is true,
286 * a privileged port will be allocated to make the connection.
287 * This requires super-user privileges if needpriv is true.
288 * Connection_attempts specifies the maximum number of tries (one per
289 * second). If proxy_command is non-NULL, it specifies the command (with %h
290 * and %p substituted for host and port, respectively) to use to contact
294 ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
295 u_short port, int family, int connection_attempts,
296 int needpriv, const char *proxy_command)
300 int sock = -1, attempt;
301 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
302 struct addrinfo hints, *ai, *aitop;
304 debug2("ssh_connect: needpriv %d", needpriv);
306 /* If a proxy command is given, connect using it. */
307 if (proxy_command != NULL)
308 return ssh_proxy_connect(host, port, proxy_command);
310 /* No proxy command. */
312 memset(&hints, 0, sizeof(hints));
313 hints.ai_family = family;
314 hints.ai_socktype = SOCK_STREAM;
315 snprintf(strport, sizeof strport, "%u", port);
316 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
317 fatal("%s: %.100s: %s", __progname, host,
318 gai_strerror(gaierr));
320 for (attempt = 0; attempt < connection_attempts; attempt++) {
322 debug("Trying again...");
325 * Loop through addresses for this host, and try each one in
326 * sequence until the connection succeeds.
328 for (ai = aitop; ai; ai = ai->ai_next) {
329 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
331 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
332 ntop, sizeof(ntop), strport, sizeof(strport),
333 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
334 error("ssh_connect: getnameinfo failed");
337 debug("Connecting to %.200s [%.100s] port %s.",
338 host, ntop, strport);
340 /* Create a socket for connecting. */
341 sock = ssh_create_socket(needpriv, ai);
343 /* Any error is already output */
346 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
347 options.connection_timeout) >= 0) {
348 /* Successful connection. */
349 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
352 debug("connect to address %s port %s: %s",
353 ntop, strport, strerror(errno));
359 break; /* Successful connection. */
361 /* Sleep a moment before retrying. */
367 /* Return failure if we didn't get a successful connection. */
369 error("ssh: connect to host %s port %s: %s",
370 host, strport, strerror(errno));
374 debug("Connection established.");
376 /* Set SO_KEEPALIVE if requested. */
377 if (options.tcp_keep_alive &&
378 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
380 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
382 /* Set the connection. */
383 packet_set_connection(sock, sock);
389 * Waits for the server identification string, and sends our own
390 * identification string.
393 ssh_exchange_identification(void)
395 char buf[256], remote_version[256]; /* must be same size! */
396 int remote_major, remote_minor, mismatch;
397 int connection_in = packet_get_connection_in();
398 int connection_out = packet_get_connection_out();
399 int minor1 = PROTOCOL_MINOR_1;
402 /* Read other side's version identification. */
404 for (i = 0; i < sizeof(buf) - 1; i++) {
405 size_t len = atomicio(read, connection_in, &buf[i], 1);
407 if (len != 1 && errno == EPIPE)
408 fatal("ssh_exchange_identification: Connection closed by remote host");
410 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
411 if (buf[i] == '\r') {
414 continue; /**XXX wait for \n */
416 if (buf[i] == '\n') {
421 fatal("ssh_exchange_identification: No banner received");
423 buf[sizeof(buf) - 1] = 0;
424 if (strncmp(buf, "SSH-", 4) == 0)
426 debug("ssh_exchange_identification: %s", buf);
428 server_version_string = xstrdup(buf);
431 * Check that the versions match. In future this might accept
432 * several versions and set appropriate flags to handle them.
434 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
435 &remote_major, &remote_minor, remote_version) != 3)
436 fatal("Bad remote protocol version identification: '%.100s'", buf);
437 debug("Remote protocol version %d.%d, remote software version %.100s",
438 remote_major, remote_minor, remote_version);
440 compat_datafellows(remote_version);
443 switch (remote_major) {
445 if (remote_minor == 99 &&
446 (options.protocol & SSH_PROTO_2) &&
447 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
451 if (!(options.protocol & SSH_PROTO_1)) {
455 if (remote_minor < 3) {
456 fatal("Remote machine has too old SSH software version.");
457 } else if (remote_minor == 3 || remote_minor == 4) {
458 /* We speak 1.3, too. */
461 if (options.forward_agent) {
462 logit("Agent forwarding disabled for protocol 1.3");
463 options.forward_agent = 0;
468 if (options.protocol & SSH_PROTO_2) {
478 fatal("Protocol major versions differ: %d vs. %d",
479 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
481 /* Send our own protocol version identification. */
482 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
483 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
484 compat20 ? PROTOCOL_MINOR_2 : minor1,
486 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
487 fatal("write: %.100s", strerror(errno));
488 client_version_string = xstrdup(buf);
489 chop(client_version_string);
490 chop(server_version_string);
491 debug("Local version string %.100s", client_version_string);
494 /* defaults to 'no' */
496 confirm(const char *prompt)
498 const char *msg, *again = "Please type 'yes' or 'no': ";
502 if (options.batch_mode)
504 for (msg = prompt;;msg = again) {
505 p = read_passphrase(msg, RP_ECHO);
507 (p[0] == '\0') || (p[0] == '\n') ||
508 strncasecmp(p, "no", 2) == 0)
510 if (p && strncasecmp(p, "yes", 3) == 0)
520 * check whether the supplied host key is valid, return -1 if the key
521 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
524 check_host_key(char *hostname, struct sockaddr *hostaddr, Key *host_key,
525 int readonly, const char *user_hostfile, const char *system_hostfile)
528 const char *type = key_type(host_key);
529 char *ip = NULL, *host = NULL;
530 char hostline[1000], *hostp, *fp;
531 HostStatus host_status;
532 HostStatus ip_status;
533 int r, local = 0, host_ip_differ = 0;
535 char ntop[NI_MAXHOST];
537 int len, host_line, ip_line;
538 const char *host_file = NULL, *ip_file = NULL;
541 * Force accepting of the host key for loopback/localhost. The
542 * problem is that if the home directory is NFS-mounted to multiple
543 * machines, localhost will refer to a different machine in each of
544 * them, and the user will get bogus HOST_CHANGED warnings. This
545 * essentially disables host authentication for localhost; however,
546 * this is probably not a real problem.
548 /** hostaddr == 0! */
549 switch (hostaddr->sa_family) {
551 local = (ntohl(((struct sockaddr_in *)hostaddr)->
552 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
553 salen = sizeof(struct sockaddr_in);
556 local = IN6_IS_ADDR_LOOPBACK(
557 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
558 salen = sizeof(struct sockaddr_in6);
562 salen = sizeof(struct sockaddr_storage);
565 if (options.no_host_authentication_for_localhost == 1 && local &&
566 options.host_key_alias == NULL) {
567 debug("Forcing accepting of host key for "
568 "loopback/localhost.");
573 * We don't have the remote ip-address for connections
574 * using a proxy command
576 if (options.proxy_command == NULL) {
577 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
578 NULL, 0, NI_NUMERICHOST) != 0)
579 fatal("check_host_key: getnameinfo failed");
580 ip = put_host_port(ntop, options.port);
582 ip = xstrdup("<no hostip for proxy command>");
585 * Turn off check_host_ip if the connection is to localhost, via proxy
586 * command or if we don't have a hostname to compare with
588 if (options.check_host_ip && (local ||
589 strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
590 options.check_host_ip = 0;
593 * Allow the user to record the key under a different name or
594 * differentiate a non-standard port. This is useful for ssh
595 * tunneling over forwarded connections or if you run multiple
596 * sshd's on different ports on the same machine.
598 if (options.host_key_alias != NULL) {
599 host = xstrdup(options.host_key_alias);
600 debug("using hostkeyalias: %s", host);
602 host = put_host_port(hostname, options.port);
606 * Store the host key from the known host file in here so that we can
607 * compare it with the key for the IP address.
609 file_key = key_new(host_key->type);
612 * Check if the host key is present in the user's list of known
613 * hosts or in the systemwide list.
615 host_file = user_hostfile;
616 host_status = check_host_in_hostfile(host_file, host, host_key,
617 file_key, &host_line);
618 if (host_status == HOST_NEW) {
619 host_file = system_hostfile;
620 host_status = check_host_in_hostfile(host_file, host, host_key,
621 file_key, &host_line);
624 * Also perform check for the ip address, skip the check if we are
625 * localhost or the hostname was an ip address to begin with
627 if (options.check_host_ip) {
628 Key *ip_key = key_new(host_key->type);
630 ip_file = user_hostfile;
631 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
633 if (ip_status == HOST_NEW) {
634 ip_file = system_hostfile;
635 ip_status = check_host_in_hostfile(ip_file, ip,
636 host_key, ip_key, &ip_line);
638 if (host_status == HOST_CHANGED &&
639 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
644 ip_status = host_status;
648 switch (host_status) {
650 /* The host is known and the key matches. */
651 debug("Host '%.200s' is known and matches the %s host key.",
653 debug("Found key in %s:%d", host_file, host_line);
654 if (options.check_host_ip && ip_status == HOST_NEW) {
656 logit("%s host key for IP address "
657 "'%.128s' not in list of known hosts.",
659 else if (!add_host_to_hostfile(user_hostfile, ip,
660 host_key, options.hash_known_hosts))
661 logit("Failed to add the %s host key for IP "
662 "address '%.128s' to the list of known "
663 "hosts (%.30s).", type, ip, user_hostfile);
665 logit("Warning: Permanently added the %s host "
666 "key for IP address '%.128s' to the list "
667 "of known hosts.", type, ip);
673 /* The host is new. */
674 if (options.strict_host_key_checking == 1) {
676 * User has requested strict host key checking. We
677 * will not add the host key automatically. The only
678 * alternative left is to abort.
680 error("No %s host key is known for %.200s and you "
681 "have requested strict checking.", type, host);
683 } else if (options.strict_host_key_checking == 2) {
684 char msg1[1024], msg2[1024];
686 if (show_other_keys(host, host_key))
687 snprintf(msg1, sizeof(msg1),
688 "\nbut keys of different type are already"
689 " known for this host.");
691 snprintf(msg1, sizeof(msg1), ".");
693 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
695 if (options.verify_host_key_dns) {
696 if (matching_host_key_dns)
697 snprintf(msg2, sizeof(msg2),
698 "Matching host key fingerprint"
701 snprintf(msg2, sizeof(msg2),
702 "No matching host key fingerprint"
705 snprintf(msg, sizeof(msg),
706 "The authenticity of host '%.200s (%s)' can't be "
708 "%s key fingerprint is %s.\n%s"
709 "Are you sure you want to continue connecting "
711 host, ip, msg1, type, fp, msg2);
717 * If not in strict mode, add the key automatically to the
718 * local known_hosts file.
720 if (options.check_host_ip && ip_status == HOST_NEW) {
721 snprintf(hostline, sizeof(hostline), "%s,%s",
724 if (options.hash_known_hosts) {
725 /* Add hash of host and IP separately */
726 r = add_host_to_hostfile(user_hostfile, host,
727 host_key, options.hash_known_hosts) &&
728 add_host_to_hostfile(user_hostfile, ip,
729 host_key, options.hash_known_hosts);
731 /* Add unhashed "host,ip" */
732 r = add_host_to_hostfile(user_hostfile,
734 options.hash_known_hosts);
737 r = add_host_to_hostfile(user_hostfile, host, host_key,
738 options.hash_known_hosts);
743 logit("Failed to add the host to the list of known "
744 "hosts (%.500s).", user_hostfile);
746 logit("Warning: Permanently added '%.200s' (%s) to the "
747 "list of known hosts.", hostp, type);
750 if (options.check_host_ip && host_ip_differ) {
752 if (ip_status == HOST_NEW)
753 key_msg = "is unknown";
754 else if (ip_status == HOST_OK)
755 key_msg = "is unchanged";
757 key_msg = "has a different value";
758 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
759 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
760 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
761 error("The %s host key for %s has changed,", type, host);
762 error("and the key for the according IP address %s", ip);
763 error("%s. This could either mean that", key_msg);
764 error("DNS SPOOFING is happening or the IP address for the host");
765 error("and its host key have changed at the same time.");
766 if (ip_status != HOST_NEW)
767 error("Offending key for IP in %s:%d", ip_file, ip_line);
769 /* The host key has changed. */
770 warn_changed_key(host_key);
771 error("Add correct host key in %.100s to get rid of this message.",
773 error("Offending key in %s:%d", host_file, host_line);
776 * If strict host key checking is in use, the user will have
777 * to edit the key manually and we can only abort.
779 if (options.strict_host_key_checking) {
780 error("%s host key for %.200s has changed and you have "
781 "requested strict checking.", type, host);
786 * If strict host key checking has not been requested, allow
787 * the connection but without MITM-able authentication or
790 if (options.password_authentication) {
791 error("Password authentication is disabled to avoid "
792 "man-in-the-middle attacks.");
793 options.password_authentication = 0;
795 if (options.kbd_interactive_authentication) {
796 error("Keyboard-interactive authentication is disabled"
797 " to avoid man-in-the-middle attacks.");
798 options.kbd_interactive_authentication = 0;
799 options.challenge_response_authentication = 0;
801 if (options.challenge_response_authentication) {
802 error("Challenge/response authentication is disabled"
803 " to avoid man-in-the-middle attacks.");
804 options.challenge_response_authentication = 0;
806 if (options.forward_agent) {
807 error("Agent forwarding is disabled to avoid "
808 "man-in-the-middle attacks.");
809 options.forward_agent = 0;
811 if (options.forward_x11) {
812 error("X11 forwarding is disabled to avoid "
813 "man-in-the-middle attacks.");
814 options.forward_x11 = 0;
816 if (options.num_local_forwards > 0 ||
817 options.num_remote_forwards > 0) {
818 error("Port forwarding is disabled to avoid "
819 "man-in-the-middle attacks.");
820 options.num_local_forwards =
821 options.num_remote_forwards = 0;
823 if (options.tun_open != SSH_TUNMODE_NO) {
824 error("Tunnel forwarding is disabled to avoid "
825 "man-in-the-middle attacks.");
826 options.tun_open = SSH_TUNMODE_NO;
829 * XXX Should permit the user to change to use the new id.
830 * This could be done by converting the host key to an
831 * identifying sentence, tell that the host identifies itself
832 * by that sentence, and ask the user if he/she whishes to
833 * accept the authentication.
837 fatal("internal error");
841 if (options.check_host_ip && host_status != HOST_CHANGED &&
842 ip_status == HOST_CHANGED) {
843 snprintf(msg, sizeof(msg),
844 "Warning: the %s host key for '%.200s' "
845 "differs from the key for the IP address '%.128s'"
846 "\nOffending key for IP in %s:%d",
847 type, host, ip, ip_file, ip_line);
848 if (host_status == HOST_OK) {
850 snprintf(msg + len, sizeof(msg) - len,
851 "\nMatching host key in %s:%d",
852 host_file, host_line);
854 if (options.strict_host_key_checking == 1) {
856 error("Exiting, you have requested strict checking.");
858 } else if (options.strict_host_key_checking == 2) {
859 strlcat(msg, "\nAre you sure you want "
860 "to continue connecting (yes/no)? ", sizeof(msg));
878 /* returns 0 if key verifies or -1 if key does NOT verify */
880 verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
885 if (options.verify_host_key_dns &&
886 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
888 if (flags & DNS_VERIFY_FOUND) {
890 if (options.verify_host_key_dns == 1 &&
891 flags & DNS_VERIFY_MATCH &&
892 flags & DNS_VERIFY_SECURE)
895 if (flags & DNS_VERIFY_MATCH) {
896 matching_host_key_dns = 1;
898 warn_changed_key(host_key);
899 error("Update the SSHFP RR in DNS with the new "
900 "host key to get rid of this message.");
905 /* return ok if the key can be found in an old keyfile */
906 if (stat(options.system_hostfile2, &st) == 0 ||
907 stat(options.user_hostfile2, &st) == 0) {
908 if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
909 options.user_hostfile2, options.system_hostfile2) == 0)
912 return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
913 options.user_hostfile, options.system_hostfile);
917 * Starts a dialog with the server, and authenticates the current user on the
918 * server. This does not need any extra privileges. The basic connection
919 * to the server must already have been established before this is called.
920 * If login fails, this function prints an error and never returns.
921 * This function does not require super-user privileges.
924 ssh_login(Sensitive *sensitive, const char *orighost,
925 struct sockaddr *hostaddr, struct passwd *pw)
928 char *server_user, *local_user;
930 local_user = xstrdup(pw->pw_name);
931 server_user = options.user ? options.user : local_user;
933 /* Convert the user-supplied hostname into all lowercase. */
934 host = xstrdup(orighost);
935 for (cp = host; *cp; cp++)
937 *cp = (char)tolower(*cp);
939 /* Exchange protocol version identification strings with the server. */
940 ssh_exchange_identification();
942 /* Put the connection into non-blocking mode. */
943 packet_set_nonblocking();
946 /* authenticate user */
948 ssh_kex2(host, hostaddr);
949 ssh_userauth2(local_user, server_user, host, sensitive);
951 ssh_kex(host, hostaddr);
952 ssh_userauth1(local_user, server_user, host, sensitive);
958 ssh_put_password(char *password)
963 if (datafellows & SSH_BUG_PASSWORDPAD) {
964 packet_put_cstring(password);
967 size = roundup(strlen(password) + 1, 32);
968 padded = xcalloc(1, size);
969 strlcpy(padded, password, size);
970 packet_put_string(padded, size);
971 memset(padded, 0, size);
976 show_key_from_file(const char *file, const char *host, int keytype)
982 found = key_new(keytype);
983 if ((ret = lookup_key_in_hostfile_by_type(file, host,
984 keytype, found, &line))) {
985 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
986 logit("WARNING: %s key found for host %s\n"
988 "%s key fingerprint %s.",
989 key_type(found), host, file, line,
990 key_type(found), fp);
997 /* print all known host keys for a given host, but skip keys of given type */
999 show_other_keys(const char *host, Key *key)
1001 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
1004 for (i = 0; type[i] != -1; i++) {
1005 if (type[i] == key->type)
1007 if (type[i] != KEY_RSA1 &&
1008 show_key_from_file(options.user_hostfile2, host, type[i])) {
1012 if (type[i] != KEY_RSA1 &&
1013 show_key_from_file(options.system_hostfile2, host, type[i])) {
1017 if (show_key_from_file(options.user_hostfile, host, type[i])) {
1021 if (show_key_from_file(options.system_hostfile, host, type[i])) {
1025 debug2("no key of type %d for host %s", type[i], host);
1031 warn_changed_key(Key *host_key)
1034 const char *type = key_type(host_key);
1036 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1038 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1039 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1040 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1041 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1042 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1043 error("It is also possible that the %s host key has just been changed.", type);
1044 error("The fingerprint for the %s key sent by the remote host is\n%s.",
1046 error("Please contact your system administrator.");
1052 * Execute a local command
1055 ssh_local_cmd(const char *args)
1061 if (!options.permit_local_command ||
1062 args == NULL || !*args)
1065 if ((shell = getenv("SHELL")) == NULL)
1066 shell = _PATH_BSHELL;
1070 debug3("Executing %s -c \"%s\"", shell, args);
1071 execl(shell, shell, "-c", args, (char *)NULL);
1072 error("Couldn't execute %s -c \"%s\": %s",
1073 shell, args, strerror(errno));
1075 } else if (pid == -1)
1076 fatal("fork failed: %.100s", strerror(errno));
1077 while (waitpid(pid, &status, 0) == -1)
1079 fatal("Couldn't wait for child: %s", strerror(errno));
1081 if (!WIFEXITED(status))
1084 return (WEXITSTATUS(status));