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