Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.187
1.187 ! stevesk 1: /* $OpenBSD: sshconnect.c,v 1.186 2006/07/03 08:54:20 stevesk Exp $ */
1.1 deraadt 2: /*
1.39 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Code to connect to a remote host, and to perform the client side of the
7: * login (authentication) dialog.
1.78 deraadt 8: *
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".
1.39 deraadt 14: */
1.1 deraadt 15:
16: #include "includes.h"
1.174 stevesk 17:
18: #include <sys/types.h>
19: #include <sys/wait.h>
1.175 stevesk 20: #include <sys/stat.h>
1.187 ! stevesk 21: #include <sys/socket.h>
! 22:
! 23: #include <netinet/in.h>
1.172 stevesk 24:
1.176 stevesk 25: #include <ctype.h>
1.172 stevesk 26: #include <paths.h>
1.71 markus 27:
1.91 markus 28: #include "ssh.h"
1.1 deraadt 29: #include "xmalloc.h"
30: #include "rsa.h"
1.59 markus 31: #include "buffer.h"
1.1 deraadt 32: #include "packet.h"
33: #include "uidswap.h"
1.21 markus 34: #include "compat.h"
1.58 markus 35: #include "key.h"
1.71 markus 36: #include "sshconnect.h"
1.58 markus 37: #include "hostfile.h"
1.91 markus 38: #include "log.h"
39: #include "readconf.h"
40: #include "atomicio.h"
41: #include "misc.h"
1.140 jakob 42: #include "dns.h"
1.186 stevesk 43: #include "version.h"
1.140 jakob 44:
1.70 markus 45: char *client_version_string = NULL;
46: char *server_version_string = NULL;
1.59 markus 47:
1.169 stevesk 48: static int matching_host_key_dns = 0;
1.145 jakob 49:
1.124 markus 50: /* import */
1.43 markus 51: extern Options options;
1.50 markus 52: extern char *__progname;
1.124 markus 53: extern uid_t original_real_uid;
54: extern uid_t original_effective_uid;
1.135 djm 55: extern pid_t proxy_command_pid;
1.91 markus 56:
1.132 markus 57: static int show_other_keys(const char *, Key *);
1.150 jakob 58: static void warn_changed_key(Key *);
1.132 markus 59:
1.39 deraadt 60: /*
61: * Connect to the given ssh server using a proxy command.
62: */
1.109 itojun 63: static int
1.124 markus 64: ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
1.1 deraadt 65: {
1.164 djm 66: char *command_string, *tmp;
1.38 markus 67: int pin[2], pout[2];
1.69 deraadt 68: pid_t pid;
1.49 markus 69: char strport[NI_MAXSERV];
1.38 markus 70:
71: /* Convert the port number into a string. */
1.49 markus 72: snprintf(strport, sizeof strport, "%hu", port);
1.38 markus 73:
1.135 djm 74: /*
75: * Build the final command string in the buffer by making the
76: * appropriate substitutions to the given proxy command.
77: *
1.154 djm 78: * Use "exec" to avoid "sh -c" processes on some platforms
1.135 djm 79: * (e.g. Solaris)
80: */
1.179 djm 81: xasprintf(&tmp, "exec %s", proxy_command);
1.164 djm 82: command_string = percent_expand(tmp, "h", host,
83: "p", strport, (char *)NULL);
84: xfree(tmp);
1.38 markus 85:
86: /* Create pipes for communicating with the proxy. */
87: if (pipe(pin) < 0 || pipe(pout) < 0)
88: fatal("Could not create pipes to communicate with the proxy: %.100s",
1.118 deraadt 89: strerror(errno));
1.38 markus 90:
91: debug("Executing proxy command: %.500s", command_string);
92:
93: /* Fork and execute the proxy command. */
94: if ((pid = fork()) == 0) {
95: char *argv[10];
96:
97: /* Child. Permanently give up superuser privileges. */
1.184 markus 98: permanently_drop_suid(original_real_uid);
1.38 markus 99:
100: /* Redirect stdin and stdout. */
101: close(pin[1]);
102: if (pin[0] != 0) {
103: if (dup2(pin[0], 0) < 0)
104: perror("dup2 stdin");
105: close(pin[0]);
106: }
107: close(pout[0]);
108: if (dup2(pout[1], 1) < 0)
109: perror("dup2 stdout");
110: /* Cannot be 1 because pin allocated two descriptors. */
111: close(pout[1]);
112:
113: /* Stderr is left as it is so that error messages get
114: printed on the user's terminal. */
1.89 markus 115: argv[0] = _PATH_BSHELL;
1.38 markus 116: argv[1] = "-c";
117: argv[2] = command_string;
118: argv[3] = NULL;
119:
120: /* Execute the proxy command. Note that we gave up any
121: extra privileges above. */
1.89 markus 122: execv(argv[0], argv);
123: perror(argv[0]);
1.38 markus 124: exit(1);
125: }
126: /* Parent. */
127: if (pid < 0)
128: fatal("fork failed: %.100s", strerror(errno));
1.135 djm 129: else
130: proxy_command_pid = pid; /* save pid to clean up later */
1.38 markus 131:
132: /* Close child side of the descriptors. */
133: close(pin[0]);
134: close(pout[1]);
135:
136: /* Free the command name. */
1.164 djm 137: xfree(command_string);
1.38 markus 138:
139: /* Set the connection file descriptors. */
140: packet_set_connection(pout[0], pin[1]);
1.1 deraadt 141:
1.110 markus 142: /* Indicate OK return */
143: return 0;
1.1 deraadt 144: }
145:
1.39 deraadt 146: /*
147: * Creates a (possibly privileged) socket for use as the ssh connection.
148: */
1.109 itojun 149: static int
1.139 markus 150: ssh_create_socket(int privileged, struct addrinfo *ai)
1.1 deraadt 151: {
1.105 markus 152: int sock, gaierr;
153: struct addrinfo hints, *res;
1.1 deraadt 154:
1.40 markus 155: /*
156: * If we are running as root and want to connect to a privileged
157: * port, bind our own socket to a privileged port.
158: */
1.38 markus 159: if (privileged) {
160: int p = IPPORT_RESERVED - 1;
1.124 markus 161: PRIV_START;
1.139 markus 162: sock = rresvport_af(&p, ai->ai_family);
1.124 markus 163: PRIV_END;
1.38 markus 164: if (sock < 0)
1.139 markus 165: error("rresvport: af=%d %.100s", ai->ai_family,
166: strerror(errno));
1.55 markus 167: else
168: debug("Allocated local port %d.", p);
1.105 markus 169: return sock;
170: }
1.139 markus 171: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.105 markus 172: if (sock < 0)
173: error("socket: %.100s", strerror(errno));
174:
175: /* Bind the socket to an alternative local IP address */
176: if (options.bind_address == NULL)
177: return sock;
178:
179: memset(&hints, 0, sizeof(hints));
1.139 markus 180: hints.ai_family = ai->ai_family;
181: hints.ai_socktype = ai->ai_socktype;
182: hints.ai_protocol = ai->ai_protocol;
1.105 markus 183: hints.ai_flags = AI_PASSIVE;
184: gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
185: if (gaierr) {
186: error("getaddrinfo: %s: %s", options.bind_address,
187: gai_strerror(gaierr));
188: close(sock);
189: return -1;
190: }
191: if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
192: error("bind: %s: %s", options.bind_address, strerror(errno));
193: close(sock);
194: freeaddrinfo(res);
195: return -1;
1.38 markus 196: }
1.105 markus 197: freeaddrinfo(res);
1.38 markus 198: return sock;
1.1 deraadt 199: }
200:
1.141 djm 201: static int
202: timeout_connect(int sockfd, const struct sockaddr *serv_addr,
203: socklen_t addrlen, int timeout)
204: {
205: fd_set *fdset;
206: struct timeval tv;
207: socklen_t optlen;
1.179 djm 208: int optval, rc, result = -1;
1.141 djm 209:
210: if (timeout <= 0)
211: return (connect(sockfd, serv_addr, addrlen));
212:
1.156 djm 213: set_nonblock(sockfd);
1.141 djm 214: rc = connect(sockfd, serv_addr, addrlen);
1.156 djm 215: if (rc == 0) {
216: unset_nonblock(sockfd);
1.141 djm 217: return (0);
1.156 djm 218: }
1.141 djm 219: if (errno != EINPROGRESS)
220: return (-1);
221:
1.179 djm 222: fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
223: sizeof(fd_mask));
1.141 djm 224: FD_SET(sockfd, fdset);
225: tv.tv_sec = timeout;
226: tv.tv_usec = 0;
227:
1.162 deraadt 228: for (;;) {
1.141 djm 229: rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
230: if (rc != -1 || errno != EINTR)
231: break;
232: }
233:
1.162 deraadt 234: switch (rc) {
1.141 djm 235: case 0:
236: /* Timed out */
237: errno = ETIMEDOUT;
1.142 djm 238: break;
1.141 djm 239: case -1:
240: /* Select error */
1.154 djm 241: debug("select: %s", strerror(errno));
1.142 djm 242: break;
1.141 djm 243: case 1:
244: /* Completed or failed */
245: optval = 0;
246: optlen = sizeof(optval);
1.154 djm 247: if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
1.148 markus 248: &optlen) == -1) {
1.154 djm 249: debug("getsockopt: %s", strerror(errno));
1.142 djm 250: break;
1.148 markus 251: }
1.141 djm 252: if (optval != 0) {
253: errno = optval;
1.142 djm 254: break;
1.141 djm 255: }
1.142 djm 256: result = 0;
1.156 djm 257: unset_nonblock(sockfd);
1.141 djm 258: break;
259: default:
260: /* Should not occur */
261: fatal("Bogus return (%d) from select()", rc);
262: }
263:
1.142 djm 264: xfree(fdset);
265: return (result);
1.141 djm 266: }
267:
1.39 deraadt 268: /*
1.49 markus 269: * Opens a TCP/IP connection to the remote server on the given host.
270: * The address of the remote host will be returned in hostaddr.
1.124 markus 271: * If port is 0, the default port will be used. If needpriv is true,
1.39 deraadt 272: * a privileged port will be allocated to make the connection.
1.124 markus 273: * This requires super-user privileges if needpriv is true.
1.39 deraadt 274: * Connection_attempts specifies the maximum number of tries (one per
275: * second). If proxy_command is non-NULL, it specifies the command (with %h
276: * and %p substituted for host and port, respectively) to use to contact
277: * the daemon.
278: */
1.38 markus 279: int
1.49 markus 280: ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
1.115 markus 281: u_short port, int family, int connection_attempts,
1.124 markus 282: int needpriv, const char *proxy_command)
1.1 deraadt 283: {
1.90 markus 284: int gaierr;
285: int on = 1;
1.49 markus 286: int sock = -1, attempt;
1.90 markus 287: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.49 markus 288: struct addrinfo hints, *ai, *aitop;
1.38 markus 289:
1.136 markus 290: debug2("ssh_connect: needpriv %d", needpriv);
1.38 markus 291:
292: /* If a proxy command is given, connect using it. */
293: if (proxy_command != NULL)
1.124 markus 294: return ssh_proxy_connect(host, port, proxy_command);
1.38 markus 295:
296: /* No proxy command. */
297:
1.49 markus 298: memset(&hints, 0, sizeof(hints));
1.115 markus 299: hints.ai_family = family;
1.49 markus 300: hints.ai_socktype = SOCK_STREAM;
1.126 deraadt 301: snprintf(strport, sizeof strport, "%u", port);
1.49 markus 302: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
1.50 markus 303: fatal("%s: %.100s: %s", __progname, host,
304: gai_strerror(gaierr));
1.38 markus 305:
1.181 markus 306: for (attempt = 0; attempt < connection_attempts; attempt++) {
1.38 markus 307: if (attempt > 0)
308: debug("Trying again...");
309:
1.181 markus 310: /*
311: * Loop through addresses for this host, and try each one in
312: * sequence until the connection succeeds.
313: */
1.49 markus 314: for (ai = aitop; ai; ai = ai->ai_next) {
315: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
316: continue;
317: if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
318: ntop, sizeof(ntop), strport, sizeof(strport),
319: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
320: error("ssh_connect: getnameinfo failed");
321: continue;
322: }
323: debug("Connecting to %.200s [%.100s] port %s.",
324: host, ntop, strport);
325:
326: /* Create a socket for connecting. */
1.139 markus 327: sock = ssh_create_socket(needpriv, ai);
1.49 markus 328: if (sock < 0)
1.110 markus 329: /* Any error is already output */
1.49 markus 330: continue;
331:
1.141 djm 332: if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
333: options.connection_timeout) >= 0) {
1.49 markus 334: /* Successful connection. */
1.102 markus 335: memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
1.38 markus 336: break;
1.49 markus 337: } else {
1.131 itojun 338: debug("connect to address %s port %s: %s",
339: ntop, strport, strerror(errno));
1.38 markus 340: close(sock);
1.181 markus 341: sock = -1;
1.38 markus 342: }
343: }
1.181 markus 344: if (sock != -1)
1.49 markus 345: break; /* Successful connection. */
1.1 deraadt 346:
1.38 markus 347: /* Sleep a moment before retrying. */
348: sleep(1);
349: }
1.49 markus 350:
351: freeaddrinfo(aitop);
352:
1.38 markus 353: /* Return failure if we didn't get a successful connection. */
1.181 markus 354: if (sock == -1) {
1.159 markus 355: error("ssh: connect to host %s port %s: %s",
1.130 itojun 356: host, strport, strerror(errno));
1.159 markus 357: return (-1);
1.130 itojun 358: }
1.38 markus 359:
360: debug("Connection established.");
1.90 markus 361:
1.155 markus 362: /* Set SO_KEEPALIVE if requested. */
363: if (options.tcp_keep_alive &&
1.90 markus 364: setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
365: sizeof(on)) < 0)
366: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.38 markus 367:
368: /* Set the connection. */
369: packet_set_connection(sock, sock);
1.1 deraadt 370:
1.110 markus 371: return 0;
1.59 markus 372: }
373:
1.43 markus 374: /*
1.39 deraadt 375: * Waits for the server identification string, and sends our own
376: * identification string.
377: */
1.109 itojun 378: static void
1.95 itojun 379: ssh_exchange_identification(void)
1.1 deraadt 380: {
1.38 markus 381: char buf[256], remote_version[256]; /* must be same size! */
1.165 djm 382: int remote_major, remote_minor, mismatch;
1.38 markus 383: int connection_in = packet_get_connection_in();
384: int connection_out = packet_get_connection_out();
1.93 stevesk 385: int minor1 = PROTOCOL_MINOR_1;
1.185 djm 386: u_int i, n;
1.38 markus 387:
1.163 avsm 388: /* Read other side's version identification. */
1.185 djm 389: for (n = 0;;) {
1.75 markus 390: for (i = 0; i < sizeof(buf) - 1; i++) {
1.163 avsm 391: size_t len = atomicio(read, connection_in, &buf[i], 1);
392:
1.167 djm 393: if (len != 1 && errno == EPIPE)
1.163 avsm 394: fatal("ssh_exchange_identification: Connection closed by remote host");
395: else if (len != 1)
1.75 markus 396: fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
397: if (buf[i] == '\r') {
398: buf[i] = '\n';
399: buf[i + 1] = 0;
400: continue; /**XXX wait for \n */
401: }
402: if (buf[i] == '\n') {
403: buf[i + 1] = 0;
404: break;
405: }
1.185 djm 406: if (++n > 65536)
407: fatal("ssh_exchange_identification: No banner received");
1.38 markus 408: }
1.75 markus 409: buf[sizeof(buf) - 1] = 0;
1.76 markus 410: if (strncmp(buf, "SSH-", 4) == 0)
1.38 markus 411: break;
1.75 markus 412: debug("ssh_exchange_identification: %s", buf);
1.38 markus 413: }
1.59 markus 414: server_version_string = xstrdup(buf);
1.38 markus 415:
1.40 markus 416: /*
417: * Check that the versions match. In future this might accept
418: * several versions and set appropriate flags to handle them.
419: */
1.59 markus 420: if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
421: &remote_major, &remote_minor, remote_version) != 3)
1.38 markus 422: fatal("Bad remote protocol version identification: '%.100s'", buf);
423: debug("Remote protocol version %d.%d, remote software version %.100s",
1.118 deraadt 424: remote_major, remote_minor, remote_version);
1.38 markus 425:
1.59 markus 426: compat_datafellows(remote_version);
1.64 markus 427: mismatch = 0;
1.59 markus 428:
1.116 deraadt 429: switch (remote_major) {
1.64 markus 430: case 1:
431: if (remote_minor == 99 &&
432: (options.protocol & SSH_PROTO_2) &&
433: !(options.protocol & SSH_PROTO_1_PREFERRED)) {
434: enable_compat20();
435: break;
436: }
437: if (!(options.protocol & SSH_PROTO_1)) {
438: mismatch = 1;
439: break;
440: }
441: if (remote_minor < 3) {
442: fatal("Remote machine has too old SSH software version.");
1.81 markus 443: } else if (remote_minor == 3 || remote_minor == 4) {
1.64 markus 444: /* We speak 1.3, too. */
445: enable_compat13();
1.81 markus 446: minor1 = 3;
1.64 markus 447: if (options.forward_agent) {
1.138 itojun 448: logit("Agent forwarding disabled for protocol 1.3");
1.64 markus 449: options.forward_agent = 0;
450: }
451: }
452: break;
453: case 2:
454: if (options.protocol & SSH_PROTO_2) {
455: enable_compat20();
456: break;
1.38 markus 457: }
1.64 markus 458: /* FALLTHROUGH */
1.68 markus 459: default:
1.64 markus 460: mismatch = 1;
461: break;
1.38 markus 462: }
1.64 markus 463: if (mismatch)
1.38 markus 464: fatal("Protocol major versions differ: %d vs. %d",
1.64 markus 465: (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
466: remote_major);
1.38 markus 467: /* Send our own protocol version identification. */
468: snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.64 markus 469: compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
1.81 markus 470: compat20 ? PROTOCOL_MINOR_2 : minor1,
1.59 markus 471: SSH_VERSION);
1.146 deraadt 472: if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
1.38 markus 473: fatal("write: %.100s", strerror(errno));
1.59 markus 474: client_version_string = xstrdup(buf);
475: chop(client_version_string);
476: chop(server_version_string);
477: debug("Local version string %.100s", client_version_string);
1.1 deraadt 478: }
479:
1.96 markus 480: /* defaults to 'no' */
1.109 itojun 481: static int
1.112 markus 482: confirm(const char *prompt)
1.1 deraadt 483: {
1.119 markus 484: const char *msg, *again = "Please type 'yes' or 'no': ";
485: char *p;
486: int ret = -1;
1.96 markus 487:
488: if (options.batch_mode)
489: return 0;
1.119 markus 490: for (msg = prompt;;msg = again) {
491: p = read_passphrase(msg, RP_ECHO);
492: if (p == NULL ||
493: (p[0] == '\0') || (p[0] == '\n') ||
494: strncasecmp(p, "no", 2) == 0)
495: ret = 0;
1.127 markus 496: if (p && strncasecmp(p, "yes", 3) == 0)
1.119 markus 497: ret = 1;
498: if (p)
499: xfree(p);
500: if (ret != -1)
501: return ret;
1.1 deraadt 502: }
503: }
504:
1.39 deraadt 505: /*
1.108 markus 506: * check whether the supplied host key is valid, return -1 if the key
507: * is not valid. the user_hostfile will not be updated if 'readonly' is true.
1.39 deraadt 508: */
1.109 itojun 509: static int
1.70 markus 510: check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
1.108 markus 511: int readonly, const char *user_hostfile, const char *system_hostfile)
1.1 deraadt 512: {
1.58 markus 513: Key *file_key;
1.152 jakob 514: const char *type = key_type(host_key);
1.46 markus 515: char *ip = NULL;
1.100 markus 516: char hostline[1000], *hostp, *fp;
1.38 markus 517: HostStatus host_status;
518: HostStatus ip_status;
1.161 djm 519: int r, local = 0, host_ip_differ = 0;
1.49 markus 520: char ntop[NI_MAXHOST];
1.119 markus 521: char msg[1024];
1.145 jakob 522: int len, host_line, ip_line;
1.85 markus 523: const char *host_file = NULL, *ip_file = NULL;
1.49 markus 524:
525: /*
526: * Force accepting of the host key for loopback/localhost. The
527: * problem is that if the home directory is NFS-mounted to multiple
528: * machines, localhost will refer to a different machine in each of
529: * them, and the user will get bogus HOST_CHANGED warnings. This
530: * essentially disables host authentication for localhost; however,
531: * this is probably not a real problem.
532: */
1.70 markus 533: /** hostaddr == 0! */
1.49 markus 534: switch (hostaddr->sa_family) {
535: case AF_INET:
1.108 markus 536: local = (ntohl(((struct sockaddr_in *)hostaddr)->
1.168 djm 537: sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.49 markus 538: break;
539: case AF_INET6:
1.108 markus 540: local = IN6_IS_ADDR_LOOPBACK(
541: &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
1.49 markus 542: break;
543: default:
544: local = 0;
545: break;
546: }
1.111 markus 547: if (options.no_host_authentication_for_localhost == 1 && local &&
548: options.host_key_alias == NULL) {
1.88 markus 549: debug("Forcing accepting of host key for "
550: "loopback/localhost.");
1.108 markus 551: return 0;
1.49 markus 552: }
1.42 markus 553:
554: /*
1.88 markus 555: * We don't have the remote ip-address for connections
556: * using a proxy command
1.42 markus 557: */
1.84 markus 558: if (options.proxy_command == NULL) {
559: if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
1.86 markus 560: NULL, 0, NI_NUMERICHOST) != 0)
1.84 markus 561: fatal("check_host_key: getnameinfo failed");
562: ip = xstrdup(ntop);
563: } else {
564: ip = xstrdup("<no hostip for proxy command>");
1.86 markus 565: }
1.88 markus 566: /*
567: * Turn off check_host_ip if the connection is to localhost, via proxy
568: * command or if we don't have a hostname to compare with
569: */
570: if (options.check_host_ip &&
571: (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
572: options.check_host_ip = 0;
1.86 markus 573:
574: /*
575: * Allow the user to record the key under a different name. This is
576: * useful for ssh tunneling over forwarded connections or if you run
577: * multiple sshd's on different ports on the same machine.
578: */
579: if (options.host_key_alias != NULL) {
580: host = options.host_key_alias;
581: debug("using hostkeyalias: %s", host);
1.84 markus 582: }
1.38 markus 583:
1.46 markus 584: /*
585: * Store the host key from the known host file in here so that we can
586: * compare it with the key for the IP address.
587: */
1.58 markus 588: file_key = key_new(host_key->type);
1.38 markus 589:
1.40 markus 590: /*
1.170 djm 591: * Check if the host key is present in the user's list of known
1.40 markus 592: * hosts or in the systemwide list.
593: */
1.85 markus 594: host_file = user_hostfile;
1.108 markus 595: host_status = check_host_in_hostfile(host_file, host, host_key,
1.118 deraadt 596: file_key, &host_line);
1.85 markus 597: if (host_status == HOST_NEW) {
598: host_file = system_hostfile;
1.108 markus 599: host_status = check_host_in_hostfile(host_file, host, host_key,
600: file_key, &host_line);
1.85 markus 601: }
1.40 markus 602: /*
603: * Also perform check for the ip address, skip the check if we are
604: * localhost or the hostname was an ip address to begin with
605: */
1.88 markus 606: if (options.check_host_ip) {
1.58 markus 607: Key *ip_key = key_new(host_key->type);
1.38 markus 608:
1.85 markus 609: ip_file = user_hostfile;
1.108 markus 610: ip_status = check_host_in_hostfile(ip_file, ip, host_key,
611: ip_key, &ip_line);
1.85 markus 612: if (ip_status == HOST_NEW) {
613: ip_file = system_hostfile;
1.108 markus 614: ip_status = check_host_in_hostfile(ip_file, ip,
615: host_key, ip_key, &ip_line);
1.85 markus 616: }
1.38 markus 617: if (host_status == HOST_CHANGED &&
1.58 markus 618: (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38 markus 619: host_ip_differ = 1;
620:
1.58 markus 621: key_free(ip_key);
1.38 markus 622: } else
623: ip_status = host_status;
624:
1.58 markus 625: key_free(file_key);
1.38 markus 626:
627: switch (host_status) {
628: case HOST_OK:
629: /* The host is known and the key matches. */
1.72 markus 630: debug("Host '%.200s' is known and matches the %s host key.",
631: host, type);
1.85 markus 632: debug("Found key in %s:%d", host_file, host_line);
1.88 markus 633: if (options.check_host_ip && ip_status == HOST_NEW) {
1.108 markus 634: if (readonly)
1.138 itojun 635: logit("%s host key for IP address "
1.108 markus 636: "'%.128s' not in list of known hosts.",
637: type, ip);
638: else if (!add_host_to_hostfile(user_hostfile, ip,
1.160 djm 639: host_key, options.hash_known_hosts))
1.138 itojun 640: logit("Failed to add the %s host key for IP "
1.108 markus 641: "address '%.128s' to the list of known "
642: "hosts (%.30s).", type, ip, user_hostfile);
1.88 markus 643: else
1.138 itojun 644: logit("Warning: Permanently added the %s host "
1.108 markus 645: "key for IP address '%.128s' to the list "
646: "of known hosts.", type, ip);
1.38 markus 647: }
648: break;
649: case HOST_NEW:
1.108 markus 650: if (readonly)
651: goto fail;
1.38 markus 652: /* The host is new. */
653: if (options.strict_host_key_checking == 1) {
1.108 markus 654: /*
655: * User has requested strict host key checking. We
656: * will not add the host key automatically. The only
657: * alternative left is to abort.
658: */
659: error("No %s host key is known for %.200s and you "
660: "have requested strict checking.", type, host);
661: goto fail;
1.38 markus 662: } else if (options.strict_host_key_checking == 2) {
1.145 jakob 663: char msg1[1024], msg2[1024];
664:
665: if (show_other_keys(host, host_key))
666: snprintf(msg1, sizeof(msg1),
1.168 djm 667: "\nbut keys of different type are already"
668: " known for this host.");
1.145 jakob 669: else
670: snprintf(msg1, sizeof(msg1), ".");
1.38 markus 671: /* The default */
1.100 markus 672: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.145 jakob 673: msg2[0] = '\0';
674: if (options.verify_host_key_dns) {
1.153 jakob 675: if (matching_host_key_dns)
1.145 jakob 676: snprintf(msg2, sizeof(msg2),
677: "Matching host key fingerprint"
678: " found in DNS.\n");
679: else
680: snprintf(msg2, sizeof(msg2),
681: "No matching host key fingerprint"
682: " found in DNS.\n");
683: }
1.119 markus 684: snprintf(msg, sizeof(msg),
1.108 markus 685: "The authenticity of host '%.200s (%s)' can't be "
1.132 markus 686: "established%s\n"
1.145 jakob 687: "%s key fingerprint is %s.\n%s"
1.108 markus 688: "Are you sure you want to continue connecting "
1.132 markus 689: "(yes/no)? ",
1.145 jakob 690: host, ip, msg1, type, fp, msg2);
1.100 markus 691: xfree(fp);
1.119 markus 692: if (!confirm(msg))
1.108 markus 693: goto fail;
1.38 markus 694: }
1.161 djm 695: /*
696: * If not in strict mode, add the key automatically to the
697: * local known_hosts file.
698: */
1.88 markus 699: if (options.check_host_ip && ip_status == HOST_NEW) {
1.161 djm 700: snprintf(hostline, sizeof(hostline), "%s,%s",
701: host, ip);
1.38 markus 702: hostp = hostline;
1.161 djm 703: if (options.hash_known_hosts) {
704: /* Add hash of host and IP separately */
705: r = add_host_to_hostfile(user_hostfile, host,
706: host_key, options.hash_known_hosts) &&
707: add_host_to_hostfile(user_hostfile, ip,
708: host_key, options.hash_known_hosts);
709: } else {
710: /* Add unhashed "host,ip" */
711: r = add_host_to_hostfile(user_hostfile,
712: hostline, host_key,
713: options.hash_known_hosts);
714: }
715: } else {
716: r = add_host_to_hostfile(user_hostfile, host, host_key,
717: options.hash_known_hosts);
1.38 markus 718: hostp = host;
1.161 djm 719: }
1.38 markus 720:
1.161 djm 721: if (!r)
1.138 itojun 722: logit("Failed to add the host to the list of known "
1.108 markus 723: "hosts (%.500s).", user_hostfile);
1.38 markus 724: else
1.138 itojun 725: logit("Warning: Permanently added '%.200s' (%s) to the "
1.108 markus 726: "list of known hosts.", hostp, type);
1.38 markus 727: break;
728: case HOST_CHANGED:
729: if (options.check_host_ip && host_ip_differ) {
1.158 avsm 730: char *key_msg;
1.38 markus 731: if (ip_status == HOST_NEW)
1.158 avsm 732: key_msg = "is unknown";
1.38 markus 733: else if (ip_status == HOST_OK)
1.158 avsm 734: key_msg = "is unchanged";
1.38 markus 735: else
1.158 avsm 736: key_msg = "has a different value";
1.38 markus 737: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
738: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
739: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72 markus 740: error("The %s host key for %s has changed,", type, host);
1.38 markus 741: error("and the key for the according IP address %s", ip);
1.158 avsm 742: error("%s. This could either mean that", key_msg);
1.38 markus 743: error("DNS SPOOFING is happening or the IP address for the host");
1.85 markus 744: error("and its host key have changed at the same time.");
1.88 markus 745: if (ip_status != HOST_NEW)
1.85 markus 746: error("Offending key for IP in %s:%d", ip_file, ip_line);
1.38 markus 747: }
748: /* The host key has changed. */
1.150 jakob 749: warn_changed_key(host_key);
1.38 markus 750: error("Add correct host key in %.100s to get rid of this message.",
1.87 markus 751: user_hostfile);
1.85 markus 752: error("Offending key in %s:%d", host_file, host_line);
1.38 markus 753:
1.40 markus 754: /*
755: * If strict host key checking is in use, the user will have
756: * to edit the key manually and we can only abort.
757: */
1.108 markus 758: if (options.strict_host_key_checking) {
759: error("%s host key for %.200s has changed and you have "
760: "requested strict checking.", type, host);
761: goto fail;
762: }
1.38 markus 763:
1.40 markus 764: /*
765: * If strict host key checking has not been requested, allow
1.144 djm 766: * the connection but without MITM-able authentication or
1.40 markus 767: * agent forwarding.
768: */
1.38 markus 769: if (options.password_authentication) {
1.108 markus 770: error("Password authentication is disabled to avoid "
771: "man-in-the-middle attacks.");
1.38 markus 772: options.password_authentication = 0;
1.144 djm 773: }
774: if (options.kbd_interactive_authentication) {
775: error("Keyboard-interactive authentication is disabled"
776: " to avoid man-in-the-middle attacks.");
777: options.kbd_interactive_authentication = 0;
778: options.challenge_response_authentication = 0;
779: }
780: if (options.challenge_response_authentication) {
781: error("Challenge/response authentication is disabled"
782: " to avoid man-in-the-middle attacks.");
783: options.challenge_response_authentication = 0;
1.38 markus 784: }
785: if (options.forward_agent) {
1.108 markus 786: error("Agent forwarding is disabled to avoid "
787: "man-in-the-middle attacks.");
1.38 markus 788: options.forward_agent = 0;
1.83 markus 789: }
790: if (options.forward_x11) {
1.108 markus 791: error("X11 forwarding is disabled to avoid "
792: "man-in-the-middle attacks.");
1.83 markus 793: options.forward_x11 = 0;
794: }
1.108 markus 795: if (options.num_local_forwards > 0 ||
796: options.num_remote_forwards > 0) {
797: error("Port forwarding is disabled to avoid "
798: "man-in-the-middle attacks.");
799: options.num_local_forwards =
1.118 deraadt 800: options.num_remote_forwards = 0;
1.38 markus 801: }
1.40 markus 802: /*
803: * XXX Should permit the user to change to use the new id.
804: * This could be done by converting the host key to an
805: * identifying sentence, tell that the host identifies itself
806: * by that sentence, and ask the user if he/she whishes to
807: * accept the authentication.
808: */
1.38 markus 809: break;
1.132 markus 810: case HOST_FOUND:
811: fatal("internal error");
812: break;
1.88 markus 813: }
814:
815: if (options.check_host_ip && host_status != HOST_CHANGED &&
816: ip_status == HOST_CHANGED) {
1.119 markus 817: snprintf(msg, sizeof(msg),
818: "Warning: the %s host key for '%.200s' "
819: "differs from the key for the IP address '%.128s'"
820: "\nOffending key for IP in %s:%d",
821: type, host, ip, ip_file, ip_line);
822: if (host_status == HOST_OK) {
823: len = strlen(msg);
824: snprintf(msg + len, sizeof(msg) - len,
825: "\nMatching host key in %s:%d",
1.125 deraadt 826: host_file, host_line);
1.119 markus 827: }
1.88 markus 828: if (options.strict_host_key_checking == 1) {
1.143 djm 829: logit("%s", msg);
1.108 markus 830: error("Exiting, you have requested strict checking.");
831: goto fail;
1.88 markus 832: } else if (options.strict_host_key_checking == 2) {
1.119 markus 833: strlcat(msg, "\nAre you sure you want "
834: "to continue connecting (yes/no)? ", sizeof(msg));
835: if (!confirm(msg))
1.108 markus 836: goto fail;
1.119 markus 837: } else {
1.143 djm 838: logit("%s", msg);
1.88 markus 839: }
1.38 markus 840: }
1.82 provos 841:
842: xfree(ip);
1.108 markus 843: return 0;
844:
845: fail:
846: xfree(ip);
847: return -1;
848: }
849:
1.140 jakob 850: /* returns 0 if key verifies or -1 if key does NOT verify */
1.108 markus 851: int
852: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
853: {
854: struct stat st;
1.153 jakob 855: int flags = 0;
1.140 jakob 856:
1.153 jakob 857: if (options.verify_host_key_dns &&
858: verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
859:
860: if (flags & DNS_VERIFY_FOUND) {
861:
862: if (options.verify_host_key_dns == 1 &&
863: flags & DNS_VERIFY_MATCH &&
864: flags & DNS_VERIFY_SECURE)
865: return 0;
866:
867: if (flags & DNS_VERIFY_MATCH) {
868: matching_host_key_dns = 1;
869: } else {
870: warn_changed_key(host_key);
871: error("Update the SSHFP RR in DNS with the new "
872: "host key to get rid of this message.");
873: }
1.140 jakob 874: }
875: }
1.108 markus 876:
877: /* return ok if the key can be found in an old keyfile */
878: if (stat(options.system_hostfile2, &st) == 0 ||
879: stat(options.user_hostfile2, &st) == 0) {
880: if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
881: options.user_hostfile2, options.system_hostfile2) == 0)
882: return 0;
883: }
884: return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
885: options.user_hostfile, options.system_hostfile);
1.51 markus 886: }
1.70 markus 887:
1.51 markus 888: /*
889: * Starts a dialog with the server, and authenticates the current user on the
890: * server. This does not need any extra privileges. The basic connection
891: * to the server must already have been established before this is called.
892: * If login fails, this function prints an error and never returns.
893: * This function does not require super-user privileges.
894: */
895: void
1.120 markus 896: ssh_login(Sensitive *sensitive, const char *orighost,
1.103 markus 897: struct sockaddr *hostaddr, struct passwd *pw)
1.51 markus 898: {
899: char *host, *cp;
1.70 markus 900: char *server_user, *local_user;
901:
902: local_user = xstrdup(pw->pw_name);
903: server_user = options.user ? options.user : local_user;
1.51 markus 904:
905: /* Convert the user-supplied hostname into all lowercase. */
906: host = xstrdup(orighost);
907: for (cp = host; *cp; cp++)
908: if (isupper(*cp))
1.178 deraadt 909: *cp = (char)tolower(*cp);
1.51 markus 910:
911: /* Exchange protocol version identification strings with the server. */
912: ssh_exchange_identification();
913:
914: /* Put the connection into non-blocking mode. */
915: packet_set_nonblocking();
916:
917: /* key exchange */
918: /* authenticate user */
1.59 markus 919: if (compat20) {
920: ssh_kex2(host, hostaddr);
1.120 markus 921: ssh_userauth2(local_user, server_user, host, sensitive);
1.59 markus 922: } else {
923: ssh_kex(host, hostaddr);
1.120 markus 924: ssh_userauth1(local_user, server_user, host, sensitive);
1.59 markus 925: }
1.182 markus 926: xfree(local_user);
1.97 markus 927: }
928:
929: void
930: ssh_put_password(char *password)
931: {
932: int size;
933: char *padded;
934:
1.99 deraadt 935: if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107 markus 936: packet_put_cstring(password);
1.99 deraadt 937: return;
938: }
1.97 markus 939: size = roundup(strlen(password) + 1, 32);
1.179 djm 940: padded = xcalloc(1, size);
1.97 markus 941: strlcpy(padded, password, size);
942: packet_put_string(padded, size);
943: memset(padded, 0, size);
944: xfree(padded);
1.132 markus 945: }
946:
947: static int
948: show_key_from_file(const char *file, const char *host, int keytype)
949: {
950: Key *found;
951: char *fp;
952: int line, ret;
953:
954: found = key_new(keytype);
955: if ((ret = lookup_key_in_hostfile_by_type(file, host,
956: keytype, found, &line))) {
957: fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1.138 itojun 958: logit("WARNING: %s key found for host %s\n"
1.133 markus 959: "in %s:%d\n"
1.132 markus 960: "%s key fingerprint %s.",
961: key_type(found), host, file, line,
962: key_type(found), fp);
963: xfree(fp);
964: }
965: key_free(found);
966: return (ret);
967: }
968:
969: /* print all known host keys for a given host, but skip keys of given type */
970: static int
971: show_other_keys(const char *host, Key *key)
972: {
973: int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
974: int i, found = 0;
975:
976: for (i = 0; type[i] != -1; i++) {
977: if (type[i] == key->type)
978: continue;
979: if (type[i] != KEY_RSA1 &&
980: show_key_from_file(options.user_hostfile2, host, type[i])) {
981: found = 1;
982: continue;
983: }
984: if (type[i] != KEY_RSA1 &&
985: show_key_from_file(options.system_hostfile2, host, type[i])) {
986: found = 1;
987: continue;
988: }
989: if (show_key_from_file(options.user_hostfile, host, type[i])) {
990: found = 1;
991: continue;
992: }
993: if (show_key_from_file(options.system_hostfile, host, type[i])) {
994: found = 1;
995: continue;
996: }
997: debug2("no key of type %d for host %s", type[i], host);
998: }
999: return (found);
1.150 jakob 1000: }
1001:
1002: static void
1003: warn_changed_key(Key *host_key)
1004: {
1005: char *fp;
1.151 jakob 1006: const char *type = key_type(host_key);
1.150 jakob 1007:
1008: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1009:
1010: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1011: error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1012: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1013: error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1014: error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1015: error("It is also possible that the %s host key has just been changed.", type);
1016: error("The fingerprint for the %s key sent by the remote host is\n%s.",
1017: type, fp);
1018: error("Please contact your system administrator.");
1019:
1020: xfree(fp);
1.171 reyk 1021: }
1022:
1023: /*
1024: * Execute a local command
1025: */
1026: int
1027: ssh_local_cmd(const char *args)
1028: {
1029: char *shell;
1030: pid_t pid;
1031: int status;
1032:
1033: if (!options.permit_local_command ||
1034: args == NULL || !*args)
1035: return (1);
1036:
1037: if ((shell = getenv("SHELL")) == NULL)
1038: shell = _PATH_BSHELL;
1039:
1040: pid = fork();
1041: if (pid == 0) {
1042: debug3("Executing %s -c \"%s\"", shell, args);
1043: execl(shell, shell, "-c", args, (char *)NULL);
1044: error("Couldn't execute %s -c \"%s\": %s",
1045: shell, args, strerror(errno));
1046: _exit(1);
1047: } else if (pid == -1)
1048: fatal("fork failed: %.100s", strerror(errno));
1049: while (waitpid(pid, &status, 0) == -1)
1050: if (errno != EINTR)
1051: fatal("Couldn't wait for child: %s", strerror(errno));
1052:
1053: if (!WIFEXITED(status))
1054: return (1);
1055:
1056: return (WEXITSTATUS(status));
1.1 deraadt 1057: }