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