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.79 2000/09/17 15:52:51 markus Exp $");
19 #include <openssl/bn.h>
20 #include <openssl/dsa.h>
21 #include <openssl/rsa.h>
32 #include "sshconnect.h"
35 char *client_version_string = NULL;
36 char *server_version_string = NULL;
38 extern Options options;
39 extern char *__progname;
42 * Connect to the given ssh server using a proxy command.
45 ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid,
46 const char *proxy_command)
53 char strport[NI_MAXSERV];
55 /* Convert the port number into a string. */
56 snprintf(strport, sizeof strport, "%hu", port);
58 /* Build the final command string in the buffer by making the
59 appropriate substitutions to the given proxy command. */
60 buffer_init(&command);
61 for (cp = proxy_command; *cp; cp++) {
62 if (cp[0] == '%' && cp[1] == '%') {
63 buffer_append(&command, "%", 1);
67 if (cp[0] == '%' && cp[1] == 'h') {
68 buffer_append(&command, host, strlen(host));
72 if (cp[0] == '%' && cp[1] == 'p') {
73 buffer_append(&command, strport, strlen(strport));
77 buffer_append(&command, cp, 1);
79 buffer_append(&command, "\0", 1);
81 /* Get the final command string. */
82 command_string = buffer_ptr(&command);
84 /* Create pipes for communicating with the proxy. */
85 if (pipe(pin) < 0 || pipe(pout) < 0)
86 fatal("Could not create pipes to communicate with the proxy: %.100s",
89 debug("Executing proxy command: %.500s", command_string);
91 /* Fork and execute the proxy command. */
92 if ((pid = fork()) == 0) {
95 /* Child. Permanently give up superuser privileges. */
96 permanently_set_uid(original_real_uid);
98 /* Redirect stdin and stdout. */
101 if (dup2(pin[0], 0) < 0)
102 perror("dup2 stdin");
106 if (dup2(pout[1], 1) < 0)
107 perror("dup2 stdout");
108 /* Cannot be 1 because pin allocated two descriptors. */
111 /* Stderr is left as it is so that error messages get
112 printed on the user's terminal. */
115 argv[2] = command_string;
118 /* Execute the proxy command. Note that we gave up any
119 extra privileges above. */
120 execv("/bin/sh", argv);
126 fatal("fork failed: %.100s", strerror(errno));
128 /* Close child side of the descriptors. */
132 /* Free the command name. */
133 buffer_free(&command);
135 /* Set the connection file descriptors. */
136 packet_set_connection(pout[0], pin[1]);
142 * Creates a (possibly privileged) socket for use as the ssh connection.
145 ssh_create_socket(uid_t original_real_uid, int privileged, int family)
150 * If we are running as root and want to connect to a privileged
151 * port, bind our own socket to a privileged port.
154 int p = IPPORT_RESERVED - 1;
155 sock = rresvport_af(&p, family);
157 error("rresvport: af=%d %.100s", family, strerror(errno));
159 debug("Allocated local port %d.", p);
162 * Just create an ordinary socket on arbitrary port. We use
163 * the user's uid to create the socket.
165 temporarily_use_uid(original_real_uid);
166 sock = socket(family, SOCK_STREAM, 0);
168 error("socket: %.100s", strerror(errno));
175 * Opens a TCP/IP connection to the remote server on the given host.
176 * The canonical host name used to connect will be returned in *host.
177 * The address of the remote host will be returned in hostaddr.
178 * If port is 0, the default port will be used. If anonymous is zero,
179 * a privileged port will be allocated to make the connection.
180 * This requires super-user privileges if anonymous is false.
181 * Connection_attempts specifies the maximum number of tries (one per
182 * second). If proxy_command is non-NULL, it specifies the command (with %h
183 * and %p substituted for host and port, respectively) to use to contact
187 ssh_connect(char **host, struct sockaddr_storage * hostaddr,
188 u_short port, int connection_attempts,
189 int anonymous, uid_t original_real_uid,
190 const char *proxy_command)
192 int sock = -1, attempt;
194 struct addrinfo hints, *ai, *aitop;
195 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
197 struct linger linger;
199 debug("ssh_connect: getuid %u geteuid %u anon %d",
200 (u_int) getuid(), (u_int) geteuid(), anonymous);
202 /* Get default port if port has not been set. */
204 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
206 port = ntohs(sp->s_port);
208 port = SSH_DEFAULT_PORT;
210 /* If a proxy command is given, connect using it. */
211 if (proxy_command != NULL)
212 return ssh_proxy_connect(*host, port, original_real_uid, proxy_command);
214 /* No proxy command. */
216 memset(&hints, 0, sizeof(hints));
217 hints.ai_family = IPv4or6;
218 hints.ai_socktype = SOCK_STREAM;
219 hints.ai_flags = AI_CANONNAME;
220 snprintf(strport, sizeof strport, "%d", port);
221 if ((gaierr = getaddrinfo(*host, strport, &hints, &aitop)) != 0)
222 fatal("%s: %.100s: %s", __progname, *host,
223 gai_strerror(gaierr));
226 * Try to connect several times. On some machines, the first time
227 * will sometimes fail. In general socket code appears to behave
228 * quite magically on many machines.
230 for (attempt = 0; attempt < connection_attempts; attempt++) {
232 debug("Trying again...");
234 /* Loop through addresses for this host, and try each one in
235 sequence until the connection succeeds. */
236 for (ai = aitop; ai; ai = ai->ai_next) {
237 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
239 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
240 ntop, sizeof(ntop), strport, sizeof(strport),
241 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
242 error("ssh_connect: getnameinfo failed");
245 debug("Connecting to %.200s [%.100s] port %s.",
246 ai->ai_canonname, ntop, strport);
248 /* Create a socket for connecting. */
249 sock = ssh_create_socket(original_real_uid,
250 !anonymous && geteuid() == 0 && port < IPPORT_RESERVED,
255 /* Connect to the host. We use the user's uid in the
256 * hope that it will help with tcp_wrappers showing
257 * the remote uid as root.
259 temporarily_use_uid(original_real_uid);
260 if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) {
261 /* Successful connection. */
262 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
266 debug("connect: %.100s", strerror(errno));
269 * Close the failed socket; there appear to
270 * be some problems when reusing a socket for
271 * which connect() has already returned an
274 shutdown(sock, SHUT_RDWR);
280 if (ai->ai_canonname != NULL)
281 *host = xstrdup(ai->ai_canonname);
283 break; /* Successful connection. */
286 /* Sleep a moment before retrying. */
292 /* Return failure if we didn't get a successful connection. */
293 if (attempt >= connection_attempts)
296 debug("Connection established.");
299 * Set socket options. We would like the socket to disappear as soon
300 * as it has been closed for whatever reason.
302 /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
305 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
307 /* Set the connection. */
308 packet_set_connection(sock, sock);
314 * Waits for the server identification string, and sends our own
315 * identification string.
318 ssh_exchange_identification()
320 char buf[256], remote_version[256]; /* must be same size! */
321 int remote_major, remote_minor, i, mismatch;
322 int connection_in = packet_get_connection_in();
323 int connection_out = packet_get_connection_out();
325 /* Read other side\'s version identification. */
327 for (i = 0; i < sizeof(buf) - 1; i++) {
328 int len = atomicio(read, connection_in, &buf[i], 1);
330 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
332 fatal("ssh_exchange_identification: Connection closed by remote host");
333 if (buf[i] == '\r') {
336 continue; /**XXX wait for \n */
338 if (buf[i] == '\n') {
343 buf[sizeof(buf) - 1] = 0;
344 if (strncmp(buf, "SSH-", 4) == 0)
346 debug("ssh_exchange_identification: %s", buf);
348 server_version_string = xstrdup(buf);
351 * Check that the versions match. In future this might accept
352 * several versions and set appropriate flags to handle them.
354 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
355 &remote_major, &remote_minor, remote_version) != 3)
356 fatal("Bad remote protocol version identification: '%.100s'", buf);
357 debug("Remote protocol version %d.%d, remote software version %.100s",
358 remote_major, remote_minor, remote_version);
360 compat_datafellows(remote_version);
363 switch(remote_major) {
365 if (remote_minor == 99 &&
366 (options.protocol & SSH_PROTO_2) &&
367 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
371 if (!(options.protocol & SSH_PROTO_1)) {
375 if (remote_minor < 3) {
376 fatal("Remote machine has too old SSH software version.");
377 } else if (remote_minor == 3) {
378 /* We speak 1.3, too. */
380 if (options.forward_agent) {
381 log("Agent forwarding disabled for protocol 1.3");
382 options.forward_agent = 0;
387 if (options.protocol & SSH_PROTO_2) {
397 fatal("Protocol major versions differ: %d vs. %d",
398 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
401 packet_set_ssh2_format();
402 /* Send our own protocol version identification. */
403 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
404 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
405 compat20 ? PROTOCOL_MINOR_2 : PROTOCOL_MINOR_1,
407 if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf))
408 fatal("write: %.100s", strerror(errno));
409 client_version_string = xstrdup(buf);
410 chop(client_version_string);
411 chop(server_version_string);
412 debug("Local version string %.100s", client_version_string);
416 read_yes_or_no(const char *prompt, int defval)
425 f = fopen("/dev/tty", "rw");
433 fprintf(stderr, "%s", prompt);
434 if (fgets(buf, sizeof(buf), f) == NULL) {
435 /* Print a newline (the prompt probably didn\'t have one). */
436 fprintf(stderr, "\n");
437 strlcpy(buf, "no", sizeof buf);
439 /* Remove newline from response. */
440 if (strchr(buf, '\n'))
441 *strchr(buf, '\n') = 0;
445 if (strcmp(buf, "yes") == 0)
447 else if (strcmp(buf, "no") == 0)
450 fprintf(stderr, "Please type 'yes' or 'no'.\n");
461 * check whether the supplied host key is valid, return only if ok.
465 check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
466 const char *user_hostfile, const char *system_hostfile)
469 char *type = key_type(host_key);
471 char hostline[1000], *hostp;
472 HostStatus host_status;
473 HostStatus ip_status;
474 int local = 0, host_ip_differ = 0;
475 char ntop[NI_MAXHOST];
478 * Force accepting of the host key for loopback/localhost. The
479 * problem is that if the home directory is NFS-mounted to multiple
480 * machines, localhost will refer to a different machine in each of
481 * them, and the user will get bogus HOST_CHANGED warnings. This
482 * essentially disables host authentication for localhost; however,
483 * this is probably not a real problem.
485 /** hostaddr == 0! */
486 switch (hostaddr->sa_family) {
488 local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
491 local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
498 debug("Forcing accepting of host key for loopback/localhost.");
503 * Turn off check_host_ip for proxy connects, since
504 * we don't have the remote ip-address
506 if (options.proxy_command != NULL && options.check_host_ip)
507 options.check_host_ip = 0;
509 if (options.check_host_ip) {
510 if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
511 NULL, 0, NI_NUMERICHOST) != 0)
512 fatal("check_host_key: getnameinfo failed");
517 * Store the host key from the known host file in here so that we can
518 * compare it with the key for the IP address.
520 file_key = key_new(host_key->type);
523 * Check if the host key is present in the user\'s list of known
524 * hosts or in the systemwide list.
526 host_status = check_host_in_hostfile(user_hostfile, host, host_key, file_key);
527 if (host_status == HOST_NEW)
528 host_status = check_host_in_hostfile(system_hostfile, host, host_key, file_key);
530 * Also perform check for the ip address, skip the check if we are
531 * localhost or the hostname was an ip address to begin with
533 if (options.check_host_ip && !local && strcmp(host, ip)) {
534 Key *ip_key = key_new(host_key->type);
535 ip_status = check_host_in_hostfile(user_hostfile, ip, host_key, ip_key);
537 if (ip_status == HOST_NEW)
538 ip_status = check_host_in_hostfile(system_hostfile, ip, host_key, ip_key);
539 if (host_status == HOST_CHANGED &&
540 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
545 ip_status = host_status;
549 switch (host_status) {
551 /* The host is known and the key matches. */
552 debug("Host '%.200s' is known and matches the %s host key.",
554 if (options.check_host_ip) {
555 if (ip_status == HOST_NEW) {
556 if (!add_host_to_hostfile(user_hostfile, ip, host_key))
557 log("Failed to add the %s host key for IP address '%.30s' to the list of known hosts (%.30s).",
558 type, ip, user_hostfile);
560 log("Warning: Permanently added the %s host key for IP address '%.30s' to the list of known hosts.",
562 } else if (ip_status != HOST_OK)
563 log("Warning: the %s host key for '%.200s' differs from the key for the IP address '%.30s'",
568 /* The host is new. */
569 if (options.strict_host_key_checking == 1) {
570 /* User has requested strict host key checking. We will not add the host key
571 automatically. The only alternative left is to abort. */
572 fatal("No %s host key is known for %.200s and you have requested strict checking.", type, host);
573 } else if (options.strict_host_key_checking == 2) {
576 char *fp = key_fingerprint(host_key);
577 snprintf(prompt, sizeof(prompt),
578 "The authenticity of host '%.200s' can't be established.\n"
579 "%s key fingerprint is %s.\n"
580 "Are you sure you want to continue connecting (yes/no)? ",
582 if (!read_yes_or_no(prompt, -1))
583 fatal("Aborted by user!\n");
585 if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) {
586 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
591 /* If not in strict mode, add the key automatically to the local known_hosts file. */
592 if (!add_host_to_hostfile(user_hostfile, hostp, host_key))
593 log("Failed to add the host to the list of known hosts (%.500s).",
596 log("Warning: Permanently added '%.200s' (%s) to the list of known hosts.",
600 if (options.check_host_ip && host_ip_differ) {
602 if (ip_status == HOST_NEW)
604 else if (ip_status == HOST_OK)
605 msg = "is unchanged";
607 msg = "has a different value";
608 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
609 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
610 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
611 error("The %s host key for %s has changed,", type, host);
612 error("and the key for the according IP address %s", ip);
613 error("%s. This could either mean that", msg);
614 error("DNS SPOOFING is happening or the IP address for the host");
615 error("and its host key have changed at the same time");
617 /* The host key has changed. */
618 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
619 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
620 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
621 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
622 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
623 error("It is also possible that the %s host key has just been changed.", type);
624 error("Please contact your system administrator.");
625 error("Add correct host key in %.100s to get rid of this message.",
629 * If strict host key checking is in use, the user will have
630 * to edit the key manually and we can only abort.
632 if (options.strict_host_key_checking)
633 fatal("%s host key for %.200s has changed and you have requested strict checking.", type, host);
636 * If strict host key checking has not been requested, allow
637 * the connection but without password authentication or
640 if (options.password_authentication) {
641 error("Password authentication is disabled to avoid trojan horses.");
642 options.password_authentication = 0;
644 if (options.forward_agent) {
645 error("Agent forwarding is disabled to avoid trojan horses.");
646 options.forward_agent = 0;
649 * XXX Should permit the user to change to use the new id.
650 * This could be done by converting the host key to an
651 * identifying sentence, tell that the host identifies itself
652 * by that sentence, and ask the user if he/she whishes to
653 * accept the authentication.
657 if (options.check_host_ip)
663 try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
665 krb5_error_code problem;
668 krb5_ccache ccache = NULL;
669 const char *remotehost;
671 int type, payload_len;
672 krb5_ap_rep_enc_part *reply = NULL;
675 memset(&ap, 0, sizeof(ap));
677 problem = krb5_init_context(context);
683 tkfile = krb5_cc_default_name(*context);
684 if (strncmp(tkfile, "FILE:", 5) == 0)
687 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
688 debug("Kerberos V5: could not get default ccache (permission denied).");
693 problem = krb5_cc_default(*context, &ccache);
699 remotehost = get_canonical_hostname();
701 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
702 "host", remotehost, NULL, ccache, &ap);
708 packet_start(SSH_CMSG_AUTH_KERBEROS);
709 packet_put_string((char *) ap.data, ap.length);
716 type = packet_read(&payload_len);
718 case SSH_SMSG_FAILURE:
719 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
720 debug("Kerberos V5 authentication failed.");
724 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
725 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
726 debug("Kerberos V5 authentication accepted.");
728 /* Get server's response. */
729 ap.data = packet_get_string((unsigned int *) &ap.length);
731 packet_integrity_check(payload_len, 4 + ap.length, type);
732 /* XXX je to dobre? */
734 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
742 packet_disconnect("Protocol error on Kerberos V5 response: %d", type);
750 krb5_cc_close(*context, ccache);
752 krb5_free_ap_rep_enc_part(*context, reply);
761 send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
764 int type, payload_len;
765 krb5_error_code problem;
767 krb5_ccache ccache = NULL;
769 krb5_kdc_flags flags;
770 const char* remotehost = get_canonical_hostname();
772 memset(&creds, 0, sizeof(creds));
773 memset(&outbuf, 0, sizeof(outbuf));
775 fd = packet_get_connection_in();
776 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
782 tkfile = krb5_cc_default_name(context);
783 if (strncmp(tkfile, "FILE:", 5) == 0)
786 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
787 debug("Kerberos V5: could not get default ccache (permission denied).");
792 problem = krb5_cc_default(context, &ccache);
797 problem = krb5_cc_get_principal(context, ccache, &creds.client);
802 problem = krb5_build_principal(context, &creds.server,
803 strlen(creds.client->realm),
812 creds.times.endtime = 0;
815 flags.b.forwarded = 1;
816 flags.b.forwardable = krb5_config_get_bool(context, NULL,
817 "libdefaults", "forwardable", NULL);
819 problem = krb5_get_forwarded_creds (context,
830 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
831 packet_put_string((char *)outbuf.data, outbuf.length);
835 type = packet_read(&payload_len);
837 case SSH_SMSG_SUCCESS:
839 case SSH_SMSG_FAILURE:
847 krb5_free_principal(context, creds.client);
849 krb5_free_principal(context, creds.server);
851 krb5_cc_close(context, ccache);
860 * Starts a dialog with the server, and authenticates the current user on the
861 * server. This does not need any extra privileges. The basic connection
862 * to the server must already have been established before this is called.
863 * If login fails, this function prints an error and never returns.
864 * This function does not require super-user privileges.
867 ssh_login(int host_key_valid, RSA *own_host_key, const char *orighost,
868 struct sockaddr *hostaddr, uid_t original_real_uid)
872 char *server_user, *local_user;
874 /* Get local user name. Use it as server user if no user name was given. */
875 pw = getpwuid(original_real_uid);
877 fatal("User id %u not found from user database.", original_real_uid);
878 local_user = xstrdup(pw->pw_name);
879 server_user = options.user ? options.user : local_user;
881 /* Convert the user-supplied hostname into all lowercase. */
882 host = xstrdup(orighost);
883 for (cp = host; *cp; cp++)
887 /* Exchange protocol version identification strings with the server. */
888 ssh_exchange_identification();
890 /* Put the connection into non-blocking mode. */
891 packet_set_nonblocking();
894 /* authenticate user */
896 ssh_kex2(host, hostaddr);
897 ssh_userauth2(server_user, host);
899 ssh_kex(host, hostaddr);
900 ssh_userauth(local_user, server_user, host, host_key_valid, own_host_key);
905 ssh_put_password(char *password)
910 size = roundup(strlen(password) + 1, 32);
911 padded = xmalloc(size);
912 memset(padded, 0, size);
913 strlcpy(padded, password, size);
914 packet_put_string(padded, size);
915 memset(padded, 0, size);