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