2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
16 RCSID("$OpenBSD: sshconnect.c,v 1.171 2005/12/06 22:38:27 reyk Exp $");
18 #include <openssl/bn.h>
28 #include "sshconnect.h"
36 char *client_version_string = NULL;
37 char *server_version_string = NULL;
39 static int matching_host_key_dns = 0;
42 extern Options options;
43 extern char *__progname;
44 extern uid_t original_real_uid;
45 extern uid_t original_effective_uid;
46 extern pid_t proxy_command_pid;
48 #ifndef INET6_ADDRSTRLEN /* for non IPv6 machines */
49 #define INET6_ADDRSTRLEN 46
52 static int show_other_keys(const char *, Key *);
53 static void warn_changed_key(Key *);
56 * Connect to the given ssh server using a proxy command.
59 ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
61 char *command_string, *tmp;
64 char strport[NI_MAXSERV];
67 /* Convert the port number into a string. */
68 snprintf(strport, sizeof strport, "%hu", port);
71 * Build the final command string in the buffer by making the
72 * appropriate substitutions to the given proxy command.
74 * Use "exec" to avoid "sh -c" processes on some platforms
77 len = strlen(proxy_command) + 6;
79 strlcpy(tmp, "exec ", len);
80 strlcat(tmp, proxy_command, len);
81 command_string = percent_expand(tmp, "h", host,
82 "p", strport, (char *)NULL);
85 /* Create pipes for communicating with the proxy. */
86 if (pipe(pin) < 0 || pipe(pout) < 0)
87 fatal("Could not create pipes to communicate with the proxy: %.100s",
90 debug("Executing proxy command: %.500s", command_string);
92 /* Fork and execute the proxy command. */
93 if ((pid = fork()) == 0) {
96 /* Child. Permanently give up superuser privileges. */
97 seteuid(original_real_uid);
98 setuid(original_real_uid);
100 /* Redirect stdin and stdout. */
103 if (dup2(pin[0], 0) < 0)
104 perror("dup2 stdin");
108 if (dup2(pout[1], 1) < 0)
109 perror("dup2 stdout");
110 /* Cannot be 1 because pin allocated two descriptors. */
113 /* Stderr is left as it is so that error messages get
114 printed on the user's terminal. */
115 argv[0] = _PATH_BSHELL;
117 argv[2] = command_string;
120 /* Execute the proxy command. Note that we gave up any
121 extra privileges above. */
122 execv(argv[0], argv);
128 fatal("fork failed: %.100s", strerror(errno));
130 proxy_command_pid = pid; /* save pid to clean up later */
132 /* Close child side of the descriptors. */
136 /* Free the command name. */
137 xfree(command_string);
139 /* Set the connection file descriptors. */
140 packet_set_connection(pout[0], pin[1]);
142 /* Indicate OK return */
147 * Creates a (possibly privileged) socket for use as the ssh connection.
150 ssh_create_socket(int privileged, struct addrinfo *ai)
153 struct addrinfo hints, *res;
156 * If we are running as root and want to connect to a privileged
157 * port, bind our own socket to a privileged port.
160 int p = IPPORT_RESERVED - 1;
162 sock = rresvport_af(&p, ai->ai_family);
165 error("rresvport: af=%d %.100s", ai->ai_family,
168 debug("Allocated local port %d.", p);
171 /* tuning needs to happen after the socket is */
172 /* created but before the connection happens */
173 /* so winscale is negotiated properly -cjr */
175 /* Set tcp receive buffer if requested */
176 if (options.tcp_rcv_buf)
178 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
179 (void *)&options.tcp_rcv_buf,
180 sizeof(options.tcp_rcv_buf)) >= 0)
182 debug("setsockopt SO_RCVBUF: %.100s", strerror(errno));
186 /* coudln't set the socket size to use spec. */
187 /* should default to system param and continue */
188 /* warn the user though - cjr */
189 error("Couldn't set socket receive buffer as requested. Continuing anyway.");
194 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
196 error("socket: %.100s", strerror(errno));
198 /* tuning needs to happen after the socket is */
199 /* created but before the connection happens */
200 /* so winscale is negotiated properly -cjr */
202 /* Set tcp receive buffer if requested */
203 if (options.tcp_rcv_buf)
205 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
206 (void *)&options.tcp_rcv_buf,
207 sizeof(options.tcp_rcv_buf)) >= 0)
209 debug("setsockopt SO_RCVBUF: %.100s", strerror(errno));
213 /* coudln't set the socket size to use spec. */
214 /* should default to system param and continue */
215 /* warn the user though - cjr */
216 error("Couldn't set socket receive buffer as requested. Continuing anyway.");
220 /* Bind the socket to an alternative local IP address */
221 if (options.bind_address == NULL)
224 memset(&hints, 0, sizeof(hints));
225 hints.ai_family = ai->ai_family;
226 hints.ai_socktype = ai->ai_socktype;
227 hints.ai_protocol = ai->ai_protocol;
228 hints.ai_flags = AI_PASSIVE;
229 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
231 error("getaddrinfo: %s: %s", options.bind_address,
232 gai_strerror(gaierr));
236 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
237 error("bind: %s: %s", options.bind_address, strerror(errno));
247 timeout_connect(int sockfd, const struct sockaddr *serv_addr,
248 socklen_t addrlen, int timeout)
253 int fdsetsz, optval, rc, result = -1;
256 return (connect(sockfd, serv_addr, addrlen));
258 set_nonblock(sockfd);
259 rc = connect(sockfd, serv_addr, addrlen);
261 unset_nonblock(sockfd);
264 if (errno != EINPROGRESS)
267 fdsetsz = howmany(sockfd + 1, NFDBITS) * sizeof(fd_mask);
268 fdset = (fd_set *)xmalloc(fdsetsz);
270 memset(fdset, 0, fdsetsz);
271 FD_SET(sockfd, fdset);
276 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
277 if (rc != -1 || errno != EINTR)
288 debug("select: %s", strerror(errno));
291 /* Completed or failed */
293 optlen = sizeof(optval);
294 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
296 debug("getsockopt: %s", strerror(errno));
304 unset_nonblock(sockfd);
307 /* Should not occur */
308 fatal("Bogus return (%d) from select()", rc);
316 * Opens a TCP/IP connection to the remote server on the given host.
317 * The address of the remote host will be returned in hostaddr.
318 * If port is 0, the default port will be used. If needpriv is true,
319 * a privileged port will be allocated to make the connection.
320 * This requires super-user privileges if needpriv is true.
321 * Connection_attempts specifies the maximum number of tries (one per
322 * second). If proxy_command is non-NULL, it specifies the command (with %h
323 * and %p substituted for host and port, respectively) to use to contact
327 ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
328 u_short port, int family, int connection_attempts,
329 int needpriv, const char *proxy_command)
333 int sock = -1, attempt;
334 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
335 struct addrinfo hints, *ai, *aitop;
337 debug2("ssh_connect: needpriv %d", needpriv);
339 /* If a proxy command is given, connect using it. */
340 if (proxy_command != NULL)
341 return ssh_proxy_connect(host, port, proxy_command);
343 /* No proxy command. */
345 memset(&hints, 0, sizeof(hints));
346 hints.ai_family = family;
347 hints.ai_socktype = SOCK_STREAM;
348 snprintf(strport, sizeof strport, "%u", port);
349 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
350 fatal("%s: %.100s: %s", __progname, host,
351 gai_strerror(gaierr));
354 * Try to connect several times. On some machines, the first time
355 * will sometimes fail. In general socket code appears to behave
356 * quite magically on many machines.
358 for (attempt = 0; ;) {
360 debug("Trying again...");
362 /* Loop through addresses for this host, and try each one in
363 sequence until the connection succeeds. */
364 for (ai = aitop; ai; ai = ai->ai_next) {
365 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
367 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
368 ntop, sizeof(ntop), strport, sizeof(strport),
369 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
370 error("ssh_connect: getnameinfo failed");
373 debug("Connecting to %.200s [%.100s] port %s.",
374 host, ntop, strport);
376 /* Create a socket for connecting. */
377 sock = ssh_create_socket(needpriv, ai);
379 /* Any error is already output */
382 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
383 options.connection_timeout) >= 0) {
384 /* Successful connection. */
385 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
388 debug("connect to address %s port %s: %s",
389 ntop, strport, strerror(errno));
391 * Close the failed socket; there appear to
392 * be some problems when reusing a socket for
393 * which connect() has already returned an
400 break; /* Successful connection. */
403 if (attempt >= connection_attempts)
405 /* Sleep a moment before retrying. */
411 /* Return failure if we didn't get a successful connection. */
412 if (attempt >= connection_attempts) {
413 error("ssh: connect to host %s port %s: %s",
414 host, strport, strerror(errno));
418 debug("Connection established.");
420 /* Set SO_KEEPALIVE if requested. */
421 if (options.tcp_keep_alive &&
422 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
424 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
426 /* Set the connection. */
427 packet_set_connection(sock, sock);
433 * Waits for the server identification string, and sends our own
434 * identification string.
437 ssh_exchange_identification(void)
439 char buf[256], remote_version[256]; /* must be same size! */
440 int remote_major, remote_minor, mismatch;
441 int connection_in = packet_get_connection_in();
442 int connection_out = packet_get_connection_out();
443 int minor1 = PROTOCOL_MINOR_1;
446 /* Read other side's version identification. */
448 for (i = 0; i < sizeof(buf) - 1; i++) {
449 size_t len = atomicio(read, connection_in, &buf[i], 1);
451 if (len != 1 && errno == EPIPE)
452 fatal("ssh_exchange_identification: Connection closed by remote host");
454 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
455 if (buf[i] == '\r') {
458 continue; /**XXX wait for \n */
460 if (buf[i] == '\n') {
465 buf[sizeof(buf) - 1] = 0;
466 if (strncmp(buf, "SSH-", 4) == 0)
468 debug("ssh_exchange_identification: %s", buf);
470 server_version_string = xstrdup(buf);
473 * Check that the versions match. In future this might accept
474 * several versions and set appropriate flags to handle them.
476 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
477 &remote_major, &remote_minor, remote_version) != 3)
478 fatal("Bad remote protocol version identification: '%.100s'", buf);
479 debug("Remote protocol version %d.%d, remote software version %.100s",
480 remote_major, remote_minor, remote_version);
482 compat_datafellows(remote_version);
485 switch (remote_major) {
487 if (remote_minor == 99 &&
488 (options.protocol & SSH_PROTO_2) &&
489 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
493 if (!(options.protocol & SSH_PROTO_1)) {
497 if (remote_minor < 3) {
498 fatal("Remote machine has too old SSH software version.");
499 } else if (remote_minor == 3 || remote_minor == 4) {
500 /* We speak 1.3, too. */
503 if (options.forward_agent) {
504 logit("Agent forwarding disabled for protocol 1.3");
505 options.forward_agent = 0;
510 if (options.protocol & SSH_PROTO_2) {
520 fatal("Protocol major versions differ: %d vs. %d",
521 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
523 /* Send our own protocol version identification. */
524 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
525 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
526 compat20 ? PROTOCOL_MINOR_2 : minor1,
528 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
529 fatal("write: %.100s", strerror(errno));
530 client_version_string = xstrdup(buf);
531 chop(client_version_string);
532 chop(server_version_string);
533 debug("Local version string %.100s", client_version_string);
536 /* defaults to 'no' */
538 confirm(const char *prompt)
540 const char *msg, *again = "Please type 'yes' or 'no': ";
544 if (options.batch_mode)
546 for (msg = prompt;;msg = again) {
547 p = read_passphrase(msg, RP_ECHO);
549 (p[0] == '\0') || (p[0] == '\n') ||
550 strncasecmp(p, "no", 2) == 0)
552 if (p && strncasecmp(p, "yes", 3) == 0)
562 * check whether the supplied host key is valid, return -1 if the key
563 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
566 check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
567 int readonly, const char *user_hostfile, const char *system_hostfile)
570 const char *type = key_type(host_key);
572 char hostline[1000], *hostp, *fp;
573 HostStatus host_status;
574 HostStatus ip_status;
575 int r, local = 0, host_ip_differ = 0;
577 char ntop[NI_MAXHOST];
579 int len, host_line, ip_line;
580 const char *host_file = NULL, *ip_file = NULL;
583 * Force accepting of the host key for loopback/localhost. The
584 * problem is that if the home directory is NFS-mounted to multiple
585 * machines, localhost will refer to a different machine in each of
586 * them, and the user will get bogus HOST_CHANGED warnings. This
587 * essentially disables host authentication for localhost; however,
588 * this is probably not a real problem.
590 /** hostaddr == 0! */
591 switch (hostaddr->sa_family) {
593 local = (ntohl(((struct sockaddr_in *)hostaddr)->
594 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
595 salen = sizeof(struct sockaddr_in);
598 local = IN6_IS_ADDR_LOOPBACK(
599 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
600 salen = sizeof(struct sockaddr_in6);
604 salen = sizeof(struct sockaddr_storage);
607 if (options.no_host_authentication_for_localhost == 1 && local &&
608 options.host_key_alias == NULL) {
609 debug("Forcing accepting of host key for "
610 "loopback/localhost.");
615 * We don't have the remote ip-address for connections
616 * using a proxy command
618 if (options.proxy_command == NULL) {
619 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
620 NULL, 0, NI_NUMERICHOST) != 0)
621 fatal("check_host_key: getnameinfo failed");
624 ip = xstrdup("<no hostip for proxy command>");
627 * Turn off check_host_ip if the connection is to localhost, via proxy
628 * command or if we don't have a hostname to compare with
630 if (options.check_host_ip &&
631 (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
632 options.check_host_ip = 0;
635 * Allow the user to record the key under a different name. This is
636 * useful for ssh tunneling over forwarded connections or if you run
637 * multiple sshd's on different ports on the same machine.
639 if (options.host_key_alias != NULL) {
640 host = options.host_key_alias;
641 debug("using hostkeyalias: %s", host);
645 * Store the host key from the known host file in here so that we can
646 * compare it with the key for the IP address.
648 file_key = key_new(host_key->type);
651 * Check if the host key is present in the user's list of known
652 * hosts or in the systemwide list.
654 host_file = user_hostfile;
655 host_status = check_host_in_hostfile(host_file, host, host_key,
656 file_key, &host_line);
657 if (host_status == HOST_NEW) {
658 host_file = system_hostfile;
659 host_status = check_host_in_hostfile(host_file, host, host_key,
660 file_key, &host_line);
663 * Also perform check for the ip address, skip the check if we are
664 * localhost or the hostname was an ip address to begin with
666 if (options.check_host_ip) {
667 Key *ip_key = key_new(host_key->type);
669 ip_file = user_hostfile;
670 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
672 if (ip_status == HOST_NEW) {
673 ip_file = system_hostfile;
674 ip_status = check_host_in_hostfile(ip_file, ip,
675 host_key, ip_key, &ip_line);
677 if (host_status == HOST_CHANGED &&
678 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
683 ip_status = host_status;
687 switch (host_status) {
689 /* The host is known and the key matches. */
690 debug("Host '%.200s' is known and matches the %s host key.",
692 debug("Found key in %s:%d", host_file, host_line);
693 if (options.check_host_ip && ip_status == HOST_NEW) {
695 logit("%s host key for IP address "
696 "'%.128s' not in list of known hosts.",
698 else if (!add_host_to_hostfile(user_hostfile, ip,
699 host_key, options.hash_known_hosts))
700 logit("Failed to add the %s host key for IP "
701 "address '%.128s' to the list of known "
702 "hosts (%.30s).", type, ip, user_hostfile);
704 logit("Warning: Permanently added the %s host "
705 "key for IP address '%.128s' to the list "
706 "of known hosts.", type, ip);
712 /* The host is new. */
713 if (options.strict_host_key_checking == 1) {
715 * User has requested strict host key checking. We
716 * will not add the host key automatically. The only
717 * alternative left is to abort.
719 error("No %s host key is known for %.200s and you "
720 "have requested strict checking.", type, host);
722 } else if (options.strict_host_key_checking == 2) {
723 char msg1[1024], msg2[1024];
725 if (show_other_keys(host, host_key))
726 snprintf(msg1, sizeof(msg1),
727 "\nbut keys of different type are already"
728 " known for this host.");
730 snprintf(msg1, sizeof(msg1), ".");
732 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
734 if (options.verify_host_key_dns) {
735 if (matching_host_key_dns)
736 snprintf(msg2, sizeof(msg2),
737 "Matching host key fingerprint"
740 snprintf(msg2, sizeof(msg2),
741 "No matching host key fingerprint"
744 snprintf(msg, sizeof(msg),
745 "The authenticity of host '%.200s (%s)' can't be "
747 "%s key fingerprint is %s.\n%s"
748 "Are you sure you want to continue connecting "
750 host, ip, msg1, type, fp, msg2);
756 * If not in strict mode, add the key automatically to the
757 * local known_hosts file.
759 if (options.check_host_ip && ip_status == HOST_NEW) {
760 snprintf(hostline, sizeof(hostline), "%s,%s",
763 if (options.hash_known_hosts) {
764 /* Add hash of host and IP separately */
765 r = add_host_to_hostfile(user_hostfile, host,
766 host_key, options.hash_known_hosts) &&
767 add_host_to_hostfile(user_hostfile, ip,
768 host_key, options.hash_known_hosts);
770 /* Add unhashed "host,ip" */
771 r = add_host_to_hostfile(user_hostfile,
773 options.hash_known_hosts);
776 r = add_host_to_hostfile(user_hostfile, host, host_key,
777 options.hash_known_hosts);
782 logit("Failed to add the host to the list of known "
783 "hosts (%.500s).", user_hostfile);
785 logit("Warning: Permanently added '%.200s' (%s) to the "
786 "list of known hosts.", hostp, type);
789 if (options.check_host_ip && host_ip_differ) {
791 if (ip_status == HOST_NEW)
792 key_msg = "is unknown";
793 else if (ip_status == HOST_OK)
794 key_msg = "is unchanged";
796 key_msg = "has a different value";
797 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
798 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
799 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
800 error("The %s host key for %s has changed,", type, host);
801 error("and the key for the according IP address %s", ip);
802 error("%s. This could either mean that", key_msg);
803 error("DNS SPOOFING is happening or the IP address for the host");
804 error("and its host key have changed at the same time.");
805 if (ip_status != HOST_NEW)
806 error("Offending key for IP in %s:%d", ip_file, ip_line);
808 /* The host key has changed. */
809 warn_changed_key(host_key);
810 error("Add correct host key in %.100s to get rid of this message.",
812 error("Offending key in %s:%d", host_file, host_line);
815 * If strict host key checking is in use, the user will have
816 * to edit the key manually and we can only abort.
818 if (options.strict_host_key_checking) {
819 error("%s host key for %.200s has changed and you have "
820 "requested strict checking.", type, host);
825 * If strict host key checking has not been requested, allow
826 * the connection but without MITM-able authentication or
829 if (options.password_authentication) {
830 error("Password authentication is disabled to avoid "
831 "man-in-the-middle attacks.");
832 options.password_authentication = 0;
834 if (options.kbd_interactive_authentication) {
835 error("Keyboard-interactive authentication is disabled"
836 " to avoid man-in-the-middle attacks.");
837 options.kbd_interactive_authentication = 0;
838 options.challenge_response_authentication = 0;
840 if (options.challenge_response_authentication) {
841 error("Challenge/response authentication is disabled"
842 " to avoid man-in-the-middle attacks.");
843 options.challenge_response_authentication = 0;
845 if (options.forward_agent) {
846 error("Agent forwarding is disabled to avoid "
847 "man-in-the-middle attacks.");
848 options.forward_agent = 0;
850 if (options.forward_x11) {
851 error("X11 forwarding is disabled to avoid "
852 "man-in-the-middle attacks.");
853 options.forward_x11 = 0;
855 if (options.num_local_forwards > 0 ||
856 options.num_remote_forwards > 0) {
857 error("Port forwarding is disabled to avoid "
858 "man-in-the-middle attacks.");
859 options.num_local_forwards =
860 options.num_remote_forwards = 0;
863 * XXX Should permit the user to change to use the new id.
864 * This could be done by converting the host key to an
865 * identifying sentence, tell that the host identifies itself
866 * by that sentence, and ask the user if he/she whishes to
867 * accept the authentication.
871 fatal("internal error");
875 if (options.check_host_ip && host_status != HOST_CHANGED &&
876 ip_status == HOST_CHANGED) {
877 snprintf(msg, sizeof(msg),
878 "Warning: the %s host key for '%.200s' "
879 "differs from the key for the IP address '%.128s'"
880 "\nOffending key for IP in %s:%d",
881 type, host, ip, ip_file, ip_line);
882 if (host_status == HOST_OK) {
884 snprintf(msg + len, sizeof(msg) - len,
885 "\nMatching host key in %s:%d",
886 host_file, host_line);
888 if (options.strict_host_key_checking == 1) {
890 error("Exiting, you have requested strict checking.");
892 } else if (options.strict_host_key_checking == 2) {
893 strlcat(msg, "\nAre you sure you want "
894 "to continue connecting (yes/no)? ", sizeof(msg));
910 /* returns 0 if key verifies or -1 if key does NOT verify */
912 verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
917 if (options.verify_host_key_dns &&
918 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
920 if (flags & DNS_VERIFY_FOUND) {
922 if (options.verify_host_key_dns == 1 &&
923 flags & DNS_VERIFY_MATCH &&
924 flags & DNS_VERIFY_SECURE)
927 if (flags & DNS_VERIFY_MATCH) {
928 matching_host_key_dns = 1;
930 warn_changed_key(host_key);
931 error("Update the SSHFP RR in DNS with the new "
932 "host key to get rid of this message.");
937 /* return ok if the key can be found in an old keyfile */
938 if (stat(options.system_hostfile2, &st) == 0 ||
939 stat(options.user_hostfile2, &st) == 0) {
940 if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
941 options.user_hostfile2, options.system_hostfile2) == 0)
944 return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
945 options.user_hostfile, options.system_hostfile);
949 * Starts a dialog with the server, and authenticates the current user on the
950 * server. This does not need any extra privileges. The basic connection
951 * to the server must already have been established before this is called.
952 * If login fails, this function prints an error and never returns.
953 * This function does not require super-user privileges.
956 ssh_login(Sensitive *sensitive, const char *orighost,
957 struct sockaddr *hostaddr, struct passwd *pw)
960 char *server_user, *local_user;
962 local_user = xstrdup(pw->pw_name);
963 server_user = options.user ? options.user : local_user;
965 /* Convert the user-supplied hostname into all lowercase. */
966 host = xstrdup(orighost);
967 for (cp = host; *cp; cp++)
971 /* Exchange protocol version identification strings with the server. */
972 ssh_exchange_identification();
974 /* Put the connection into non-blocking mode. */
975 packet_set_nonblocking();
978 /* authenticate user */
980 ssh_kex2(host, hostaddr);
981 ssh_userauth2(local_user, server_user, host, sensitive);
983 ssh_kex(host, hostaddr);
984 ssh_userauth1(local_user, server_user, host, sensitive);
989 ssh_put_password(char *password)
994 if (datafellows & SSH_BUG_PASSWORDPAD) {
995 packet_put_cstring(password);
998 size = roundup(strlen(password) + 1, 32);
999 padded = xmalloc(size);
1000 memset(padded, 0, size);
1001 strlcpy(padded, password, size);
1002 packet_put_string(padded, size);
1003 memset(padded, 0, size);
1008 show_key_from_file(const char *file, const char *host, int keytype)
1014 found = key_new(keytype);
1015 if ((ret = lookup_key_in_hostfile_by_type(file, host,
1016 keytype, found, &line))) {
1017 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1018 logit("WARNING: %s key found for host %s\n"
1020 "%s key fingerprint %s.",
1021 key_type(found), host, file, line,
1022 key_type(found), fp);
1029 /* print all known host keys for a given host, but skip keys of given type */
1031 show_other_keys(const char *host, Key *key)
1033 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
1036 for (i = 0; type[i] != -1; i++) {
1037 if (type[i] == key->type)
1039 if (type[i] != KEY_RSA1 &&
1040 show_key_from_file(options.user_hostfile2, host, type[i])) {
1044 if (type[i] != KEY_RSA1 &&
1045 show_key_from_file(options.system_hostfile2, host, type[i])) {
1049 if (show_key_from_file(options.user_hostfile, host, type[i])) {
1053 if (show_key_from_file(options.system_hostfile, host, type[i])) {
1057 debug2("no key of type %d for host %s", type[i], host);
1063 warn_changed_key(Key *host_key)
1066 const char *type = key_type(host_key);
1068 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1070 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1071 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1072 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1073 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1074 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1075 error("It is also possible that the %s host key has just been changed.", type);
1076 error("The fingerprint for the %s key sent by the remote host is\n%s.",
1078 error("Please contact your system administrator.");
1084 * Execute a local command
1087 ssh_local_cmd(const char *args)
1093 if (!options.permit_local_command ||
1094 args == NULL || !*args)
1097 if ((shell = getenv("SHELL")) == NULL)
1098 shell = _PATH_BSHELL;
1102 debug3("Executing %s -c \"%s\"", shell, args);
1103 execl(shell, shell, "-c", args, (char *)NULL);
1104 error("Couldn't execute %s -c \"%s\": %s",
1105 shell, args, strerror(errno));
1107 } else if (pid == -1)
1108 fatal("fork failed: %.100s", strerror(errno));
1109 while (waitpid(pid, &status, 0) == -1)
1111 fatal("Couldn't wait for child: %s", strerror(errno));
1113 if (!WIFEXITED(status))
1116 return (WEXITSTATUS(status));