Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.186
1.186 ! stevesk 1: /* $OpenBSD: sshconnect.c,v 1.185 2006/06/14 10:50:42 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"
1.186 ! stevesk 40: #include "version.h"
1.140 jakob 41:
1.70 markus 42: char *client_version_string = NULL;
43: char *server_version_string = NULL;
1.59 markus 44:
1.169 stevesk 45: static int matching_host_key_dns = 0;
1.145 jakob 46:
1.124 markus 47: /* import */
1.43 markus 48: extern Options options;
1.50 markus 49: extern char *__progname;
1.124 markus 50: extern uid_t original_real_uid;
51: extern uid_t original_effective_uid;
1.135 djm 52: extern pid_t proxy_command_pid;
1.91 markus 53:
1.132 markus 54: static int show_other_keys(const char *, Key *);
1.150 jakob 55: static void warn_changed_key(Key *);
1.132 markus 56:
1.39 deraadt 57: /*
58: * Connect to the given ssh server using a proxy command.
59: */
1.109 itojun 60: static int
1.124 markus 61: ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
1.1 deraadt 62: {
1.164 djm 63: char *command_string, *tmp;
1.38 markus 64: int pin[2], pout[2];
1.69 deraadt 65: pid_t pid;
1.49 markus 66: char strport[NI_MAXSERV];
1.38 markus 67:
68: /* Convert the port number into a string. */
1.49 markus 69: snprintf(strport, sizeof strport, "%hu", port);
1.38 markus 70:
1.135 djm 71: /*
72: * Build the final command string in the buffer by making the
73: * appropriate substitutions to the given proxy command.
74: *
1.154 djm 75: * Use "exec" to avoid "sh -c" processes on some platforms
1.135 djm 76: * (e.g. Solaris)
77: */
1.179 djm 78: xasprintf(&tmp, "exec %s", proxy_command);
1.164 djm 79: command_string = percent_expand(tmp, "h", host,
80: "p", strport, (char *)NULL);
81: xfree(tmp);
1.38 markus 82:
83: /* Create pipes for communicating with the proxy. */
84: if (pipe(pin) < 0 || pipe(pout) < 0)
85: fatal("Could not create pipes to communicate with the proxy: %.100s",
1.118 deraadt 86: strerror(errno));
1.38 markus 87:
88: debug("Executing proxy command: %.500s", command_string);
89:
90: /* Fork and execute the proxy command. */
91: if ((pid = fork()) == 0) {
92: char *argv[10];
93:
94: /* Child. Permanently give up superuser privileges. */
1.184 markus 95: permanently_drop_suid(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.185 djm 383: u_int i, n;
1.38 markus 384:
1.163 avsm 385: /* Read other side's version identification. */
1.185 djm 386: for (n = 0;;) {
1.75 markus 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.185 djm 403: if (++n > 65536)
404: fatal("ssh_exchange_identification: No banner received");
1.38 markus 405: }
1.75 markus 406: buf[sizeof(buf) - 1] = 0;
1.76 markus 407: if (strncmp(buf, "SSH-", 4) == 0)
1.38 markus 408: break;
1.75 markus 409: debug("ssh_exchange_identification: %s", buf);
1.38 markus 410: }
1.59 markus 411: server_version_string = xstrdup(buf);
1.38 markus 412:
1.40 markus 413: /*
414: * Check that the versions match. In future this might accept
415: * several versions and set appropriate flags to handle them.
416: */
1.59 markus 417: if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
418: &remote_major, &remote_minor, remote_version) != 3)
1.38 markus 419: fatal("Bad remote protocol version identification: '%.100s'", buf);
420: debug("Remote protocol version %d.%d, remote software version %.100s",
1.118 deraadt 421: remote_major, remote_minor, remote_version);
1.38 markus 422:
1.59 markus 423: compat_datafellows(remote_version);
1.64 markus 424: mismatch = 0;
1.59 markus 425:
1.116 deraadt 426: switch (remote_major) {
1.64 markus 427: case 1:
428: if (remote_minor == 99 &&
429: (options.protocol & SSH_PROTO_2) &&
430: !(options.protocol & SSH_PROTO_1_PREFERRED)) {
431: enable_compat20();
432: break;
433: }
434: if (!(options.protocol & SSH_PROTO_1)) {
435: mismatch = 1;
436: break;
437: }
438: if (remote_minor < 3) {
439: fatal("Remote machine has too old SSH software version.");
1.81 markus 440: } else if (remote_minor == 3 || remote_minor == 4) {
1.64 markus 441: /* We speak 1.3, too. */
442: enable_compat13();
1.81 markus 443: minor1 = 3;
1.64 markus 444: if (options.forward_agent) {
1.138 itojun 445: logit("Agent forwarding disabled for protocol 1.3");
1.64 markus 446: options.forward_agent = 0;
447: }
448: }
449: break;
450: case 2:
451: if (options.protocol & SSH_PROTO_2) {
452: enable_compat20();
453: break;
1.38 markus 454: }
1.64 markus 455: /* FALLTHROUGH */
1.68 markus 456: default:
1.64 markus 457: mismatch = 1;
458: break;
1.38 markus 459: }
1.64 markus 460: if (mismatch)
1.38 markus 461: fatal("Protocol major versions differ: %d vs. %d",
1.64 markus 462: (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
463: remote_major);
1.38 markus 464: /* Send our own protocol version identification. */
465: snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.64 markus 466: compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
1.81 markus 467: compat20 ? PROTOCOL_MINOR_2 : minor1,
1.59 markus 468: SSH_VERSION);
1.146 deraadt 469: if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
1.38 markus 470: fatal("write: %.100s", strerror(errno));
1.59 markus 471: client_version_string = xstrdup(buf);
472: chop(client_version_string);
473: chop(server_version_string);
474: debug("Local version string %.100s", client_version_string);
1.1 deraadt 475: }
476:
1.96 markus 477: /* defaults to 'no' */
1.109 itojun 478: static int
1.112 markus 479: confirm(const char *prompt)
1.1 deraadt 480: {
1.119 markus 481: const char *msg, *again = "Please type 'yes' or 'no': ";
482: char *p;
483: int ret = -1;
1.96 markus 484:
485: if (options.batch_mode)
486: return 0;
1.119 markus 487: for (msg = prompt;;msg = again) {
488: p = read_passphrase(msg, RP_ECHO);
489: if (p == NULL ||
490: (p[0] == '\0') || (p[0] == '\n') ||
491: strncasecmp(p, "no", 2) == 0)
492: ret = 0;
1.127 markus 493: if (p && strncasecmp(p, "yes", 3) == 0)
1.119 markus 494: ret = 1;
495: if (p)
496: xfree(p);
497: if (ret != -1)
498: return ret;
1.1 deraadt 499: }
500: }
501:
1.39 deraadt 502: /*
1.108 markus 503: * check whether the supplied host key is valid, return -1 if the key
504: * is not valid. the user_hostfile will not be updated if 'readonly' is true.
1.39 deraadt 505: */
1.109 itojun 506: static int
1.70 markus 507: check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
1.108 markus 508: int readonly, const char *user_hostfile, const char *system_hostfile)
1.1 deraadt 509: {
1.58 markus 510: Key *file_key;
1.152 jakob 511: const char *type = key_type(host_key);
1.46 markus 512: char *ip = NULL;
1.100 markus 513: char hostline[1000], *hostp, *fp;
1.38 markus 514: HostStatus host_status;
515: HostStatus ip_status;
1.161 djm 516: int r, local = 0, host_ip_differ = 0;
1.49 markus 517: char ntop[NI_MAXHOST];
1.119 markus 518: char msg[1024];
1.145 jakob 519: int len, host_line, ip_line;
1.85 markus 520: const char *host_file = NULL, *ip_file = NULL;
1.49 markus 521:
522: /*
523: * Force accepting of the host key for loopback/localhost. The
524: * problem is that if the home directory is NFS-mounted to multiple
525: * machines, localhost will refer to a different machine in each of
526: * them, and the user will get bogus HOST_CHANGED warnings. This
527: * essentially disables host authentication for localhost; however,
528: * this is probably not a real problem.
529: */
1.70 markus 530: /** hostaddr == 0! */
1.49 markus 531: switch (hostaddr->sa_family) {
532: case AF_INET:
1.108 markus 533: local = (ntohl(((struct sockaddr_in *)hostaddr)->
1.168 djm 534: sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.49 markus 535: break;
536: case AF_INET6:
1.108 markus 537: local = IN6_IS_ADDR_LOOPBACK(
538: &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
1.49 markus 539: break;
540: default:
541: local = 0;
542: break;
543: }
1.111 markus 544: if (options.no_host_authentication_for_localhost == 1 && local &&
545: options.host_key_alias == NULL) {
1.88 markus 546: debug("Forcing accepting of host key for "
547: "loopback/localhost.");
1.108 markus 548: return 0;
1.49 markus 549: }
1.42 markus 550:
551: /*
1.88 markus 552: * We don't have the remote ip-address for connections
553: * using a proxy command
1.42 markus 554: */
1.84 markus 555: if (options.proxy_command == NULL) {
556: if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
1.86 markus 557: NULL, 0, NI_NUMERICHOST) != 0)
1.84 markus 558: fatal("check_host_key: getnameinfo failed");
559: ip = xstrdup(ntop);
560: } else {
561: ip = xstrdup("<no hostip for proxy command>");
1.86 markus 562: }
1.88 markus 563: /*
564: * Turn off check_host_ip if the connection is to localhost, via proxy
565: * command or if we don't have a hostname to compare with
566: */
567: if (options.check_host_ip &&
568: (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
569: options.check_host_ip = 0;
1.86 markus 570:
571: /*
572: * Allow the user to record the key under a different name. This is
573: * useful for ssh tunneling over forwarded connections or if you run
574: * multiple sshd's on different ports on the same machine.
575: */
576: if (options.host_key_alias != NULL) {
577: host = options.host_key_alias;
578: debug("using hostkeyalias: %s", host);
1.84 markus 579: }
1.38 markus 580:
1.46 markus 581: /*
582: * Store the host key from the known host file in here so that we can
583: * compare it with the key for the IP address.
584: */
1.58 markus 585: file_key = key_new(host_key->type);
1.38 markus 586:
1.40 markus 587: /*
1.170 djm 588: * Check if the host key is present in the user's list of known
1.40 markus 589: * hosts or in the systemwide list.
590: */
1.85 markus 591: host_file = user_hostfile;
1.108 markus 592: host_status = check_host_in_hostfile(host_file, host, host_key,
1.118 deraadt 593: file_key, &host_line);
1.85 markus 594: if (host_status == HOST_NEW) {
595: host_file = system_hostfile;
1.108 markus 596: host_status = check_host_in_hostfile(host_file, host, host_key,
597: file_key, &host_line);
1.85 markus 598: }
1.40 markus 599: /*
600: * Also perform check for the ip address, skip the check if we are
601: * localhost or the hostname was an ip address to begin with
602: */
1.88 markus 603: if (options.check_host_ip) {
1.58 markus 604: Key *ip_key = key_new(host_key->type);
1.38 markus 605:
1.85 markus 606: ip_file = user_hostfile;
1.108 markus 607: ip_status = check_host_in_hostfile(ip_file, ip, host_key,
608: ip_key, &ip_line);
1.85 markus 609: if (ip_status == HOST_NEW) {
610: ip_file = system_hostfile;
1.108 markus 611: ip_status = check_host_in_hostfile(ip_file, ip,
612: host_key, ip_key, &ip_line);
1.85 markus 613: }
1.38 markus 614: if (host_status == HOST_CHANGED &&
1.58 markus 615: (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38 markus 616: host_ip_differ = 1;
617:
1.58 markus 618: key_free(ip_key);
1.38 markus 619: } else
620: ip_status = host_status;
621:
1.58 markus 622: key_free(file_key);
1.38 markus 623:
624: switch (host_status) {
625: case HOST_OK:
626: /* The host is known and the key matches. */
1.72 markus 627: debug("Host '%.200s' is known and matches the %s host key.",
628: host, type);
1.85 markus 629: debug("Found key in %s:%d", host_file, host_line);
1.88 markus 630: if (options.check_host_ip && ip_status == HOST_NEW) {
1.108 markus 631: if (readonly)
1.138 itojun 632: logit("%s host key for IP address "
1.108 markus 633: "'%.128s' not in list of known hosts.",
634: type, ip);
635: else if (!add_host_to_hostfile(user_hostfile, ip,
1.160 djm 636: host_key, options.hash_known_hosts))
1.138 itojun 637: logit("Failed to add the %s host key for IP "
1.108 markus 638: "address '%.128s' to the list of known "
639: "hosts (%.30s).", type, ip, user_hostfile);
1.88 markus 640: else
1.138 itojun 641: logit("Warning: Permanently added the %s host "
1.108 markus 642: "key for IP address '%.128s' to the list "
643: "of known hosts.", type, ip);
1.38 markus 644: }
645: break;
646: case HOST_NEW:
1.108 markus 647: if (readonly)
648: goto fail;
1.38 markus 649: /* The host is new. */
650: if (options.strict_host_key_checking == 1) {
1.108 markus 651: /*
652: * User has requested strict host key checking. We
653: * will not add the host key automatically. The only
654: * alternative left is to abort.
655: */
656: error("No %s host key is known for %.200s and you "
657: "have requested strict checking.", type, host);
658: goto fail;
1.38 markus 659: } else if (options.strict_host_key_checking == 2) {
1.145 jakob 660: char msg1[1024], msg2[1024];
661:
662: if (show_other_keys(host, host_key))
663: snprintf(msg1, sizeof(msg1),
1.168 djm 664: "\nbut keys of different type are already"
665: " known for this host.");
1.145 jakob 666: else
667: snprintf(msg1, sizeof(msg1), ".");
1.38 markus 668: /* The default */
1.100 markus 669: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.145 jakob 670: msg2[0] = '\0';
671: if (options.verify_host_key_dns) {
1.153 jakob 672: if (matching_host_key_dns)
1.145 jakob 673: snprintf(msg2, sizeof(msg2),
674: "Matching host key fingerprint"
675: " found in DNS.\n");
676: else
677: snprintf(msg2, sizeof(msg2),
678: "No matching host key fingerprint"
679: " found in DNS.\n");
680: }
1.119 markus 681: snprintf(msg, sizeof(msg),
1.108 markus 682: "The authenticity of host '%.200s (%s)' can't be "
1.132 markus 683: "established%s\n"
1.145 jakob 684: "%s key fingerprint is %s.\n%s"
1.108 markus 685: "Are you sure you want to continue connecting "
1.132 markus 686: "(yes/no)? ",
1.145 jakob 687: host, ip, msg1, type, fp, msg2);
1.100 markus 688: xfree(fp);
1.119 markus 689: if (!confirm(msg))
1.108 markus 690: goto fail;
1.38 markus 691: }
1.161 djm 692: /*
693: * If not in strict mode, add the key automatically to the
694: * local known_hosts file.
695: */
1.88 markus 696: if (options.check_host_ip && ip_status == HOST_NEW) {
1.161 djm 697: snprintf(hostline, sizeof(hostline), "%s,%s",
698: host, ip);
1.38 markus 699: hostp = hostline;
1.161 djm 700: if (options.hash_known_hosts) {
701: /* Add hash of host and IP separately */
702: r = add_host_to_hostfile(user_hostfile, host,
703: host_key, options.hash_known_hosts) &&
704: add_host_to_hostfile(user_hostfile, ip,
705: host_key, options.hash_known_hosts);
706: } else {
707: /* Add unhashed "host,ip" */
708: r = add_host_to_hostfile(user_hostfile,
709: hostline, host_key,
710: options.hash_known_hosts);
711: }
712: } else {
713: r = add_host_to_hostfile(user_hostfile, host, host_key,
714: options.hash_known_hosts);
1.38 markus 715: hostp = host;
1.161 djm 716: }
1.38 markus 717:
1.161 djm 718: if (!r)
1.138 itojun 719: logit("Failed to add the host to the list of known "
1.108 markus 720: "hosts (%.500s).", user_hostfile);
1.38 markus 721: else
1.138 itojun 722: logit("Warning: Permanently added '%.200s' (%s) to the "
1.108 markus 723: "list of known hosts.", hostp, type);
1.38 markus 724: break;
725: case HOST_CHANGED:
726: if (options.check_host_ip && host_ip_differ) {
1.158 avsm 727: char *key_msg;
1.38 markus 728: if (ip_status == HOST_NEW)
1.158 avsm 729: key_msg = "is unknown";
1.38 markus 730: else if (ip_status == HOST_OK)
1.158 avsm 731: key_msg = "is unchanged";
1.38 markus 732: else
1.158 avsm 733: key_msg = "has a different value";
1.38 markus 734: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
735: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
736: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72 markus 737: error("The %s host key for %s has changed,", type, host);
1.38 markus 738: error("and the key for the according IP address %s", ip);
1.158 avsm 739: error("%s. This could either mean that", key_msg);
1.38 markus 740: error("DNS SPOOFING is happening or the IP address for the host");
1.85 markus 741: error("and its host key have changed at the same time.");
1.88 markus 742: if (ip_status != HOST_NEW)
1.85 markus 743: error("Offending key for IP in %s:%d", ip_file, ip_line);
1.38 markus 744: }
745: /* The host key has changed. */
1.150 jakob 746: warn_changed_key(host_key);
1.38 markus 747: error("Add correct host key in %.100s to get rid of this message.",
1.87 markus 748: user_hostfile);
1.85 markus 749: error("Offending key in %s:%d", host_file, host_line);
1.38 markus 750:
1.40 markus 751: /*
752: * If strict host key checking is in use, the user will have
753: * to edit the key manually and we can only abort.
754: */
1.108 markus 755: if (options.strict_host_key_checking) {
756: error("%s host key for %.200s has changed and you have "
757: "requested strict checking.", type, host);
758: goto fail;
759: }
1.38 markus 760:
1.40 markus 761: /*
762: * If strict host key checking has not been requested, allow
1.144 djm 763: * the connection but without MITM-able authentication or
1.40 markus 764: * agent forwarding.
765: */
1.38 markus 766: if (options.password_authentication) {
1.108 markus 767: error("Password authentication is disabled to avoid "
768: "man-in-the-middle attacks.");
1.38 markus 769: options.password_authentication = 0;
1.144 djm 770: }
771: if (options.kbd_interactive_authentication) {
772: error("Keyboard-interactive authentication is disabled"
773: " to avoid man-in-the-middle attacks.");
774: options.kbd_interactive_authentication = 0;
775: options.challenge_response_authentication = 0;
776: }
777: if (options.challenge_response_authentication) {
778: error("Challenge/response authentication is disabled"
779: " to avoid man-in-the-middle attacks.");
780: options.challenge_response_authentication = 0;
1.38 markus 781: }
782: if (options.forward_agent) {
1.108 markus 783: error("Agent forwarding is disabled to avoid "
784: "man-in-the-middle attacks.");
1.38 markus 785: options.forward_agent = 0;
1.83 markus 786: }
787: if (options.forward_x11) {
1.108 markus 788: error("X11 forwarding is disabled to avoid "
789: "man-in-the-middle attacks.");
1.83 markus 790: options.forward_x11 = 0;
791: }
1.108 markus 792: if (options.num_local_forwards > 0 ||
793: options.num_remote_forwards > 0) {
794: error("Port forwarding is disabled to avoid "
795: "man-in-the-middle attacks.");
796: options.num_local_forwards =
1.118 deraadt 797: options.num_remote_forwards = 0;
1.38 markus 798: }
1.40 markus 799: /*
800: * XXX Should permit the user to change to use the new id.
801: * This could be done by converting the host key to an
802: * identifying sentence, tell that the host identifies itself
803: * by that sentence, and ask the user if he/she whishes to
804: * accept the authentication.
805: */
1.38 markus 806: break;
1.132 markus 807: case HOST_FOUND:
808: fatal("internal error");
809: break;
1.88 markus 810: }
811:
812: if (options.check_host_ip && host_status != HOST_CHANGED &&
813: ip_status == HOST_CHANGED) {
1.119 markus 814: snprintf(msg, sizeof(msg),
815: "Warning: the %s host key for '%.200s' "
816: "differs from the key for the IP address '%.128s'"
817: "\nOffending key for IP in %s:%d",
818: type, host, ip, ip_file, ip_line);
819: if (host_status == HOST_OK) {
820: len = strlen(msg);
821: snprintf(msg + len, sizeof(msg) - len,
822: "\nMatching host key in %s:%d",
1.125 deraadt 823: host_file, host_line);
1.119 markus 824: }
1.88 markus 825: if (options.strict_host_key_checking == 1) {
1.143 djm 826: logit("%s", msg);
1.108 markus 827: error("Exiting, you have requested strict checking.");
828: goto fail;
1.88 markus 829: } else if (options.strict_host_key_checking == 2) {
1.119 markus 830: strlcat(msg, "\nAre you sure you want "
831: "to continue connecting (yes/no)? ", sizeof(msg));
832: if (!confirm(msg))
1.108 markus 833: goto fail;
1.119 markus 834: } else {
1.143 djm 835: logit("%s", msg);
1.88 markus 836: }
1.38 markus 837: }
1.82 provos 838:
839: xfree(ip);
1.108 markus 840: return 0;
841:
842: fail:
843: xfree(ip);
844: return -1;
845: }
846:
1.140 jakob 847: /* returns 0 if key verifies or -1 if key does NOT verify */
1.108 markus 848: int
849: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
850: {
851: struct stat st;
1.153 jakob 852: int flags = 0;
1.140 jakob 853:
1.153 jakob 854: if (options.verify_host_key_dns &&
855: verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
856:
857: if (flags & DNS_VERIFY_FOUND) {
858:
859: if (options.verify_host_key_dns == 1 &&
860: flags & DNS_VERIFY_MATCH &&
861: flags & DNS_VERIFY_SECURE)
862: return 0;
863:
864: if (flags & DNS_VERIFY_MATCH) {
865: matching_host_key_dns = 1;
866: } else {
867: warn_changed_key(host_key);
868: error("Update the SSHFP RR in DNS with the new "
869: "host key to get rid of this message.");
870: }
1.140 jakob 871: }
872: }
1.108 markus 873:
874: /* return ok if the key can be found in an old keyfile */
875: if (stat(options.system_hostfile2, &st) == 0 ||
876: stat(options.user_hostfile2, &st) == 0) {
877: if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
878: options.user_hostfile2, options.system_hostfile2) == 0)
879: return 0;
880: }
881: return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
882: options.user_hostfile, options.system_hostfile);
1.51 markus 883: }
1.70 markus 884:
1.51 markus 885: /*
886: * Starts a dialog with the server, and authenticates the current user on the
887: * server. This does not need any extra privileges. The basic connection
888: * to the server must already have been established before this is called.
889: * If login fails, this function prints an error and never returns.
890: * This function does not require super-user privileges.
891: */
892: void
1.120 markus 893: ssh_login(Sensitive *sensitive, const char *orighost,
1.103 markus 894: struct sockaddr *hostaddr, struct passwd *pw)
1.51 markus 895: {
896: char *host, *cp;
1.70 markus 897: char *server_user, *local_user;
898:
899: local_user = xstrdup(pw->pw_name);
900: server_user = options.user ? options.user : local_user;
1.51 markus 901:
902: /* Convert the user-supplied hostname into all lowercase. */
903: host = xstrdup(orighost);
904: for (cp = host; *cp; cp++)
905: if (isupper(*cp))
1.178 deraadt 906: *cp = (char)tolower(*cp);
1.51 markus 907:
908: /* Exchange protocol version identification strings with the server. */
909: ssh_exchange_identification();
910:
911: /* Put the connection into non-blocking mode. */
912: packet_set_nonblocking();
913:
914: /* key exchange */
915: /* authenticate user */
1.59 markus 916: if (compat20) {
917: ssh_kex2(host, hostaddr);
1.120 markus 918: ssh_userauth2(local_user, server_user, host, sensitive);
1.59 markus 919: } else {
920: ssh_kex(host, hostaddr);
1.120 markus 921: ssh_userauth1(local_user, server_user, host, sensitive);
1.59 markus 922: }
1.182 markus 923: xfree(local_user);
1.97 markus 924: }
925:
926: void
927: ssh_put_password(char *password)
928: {
929: int size;
930: char *padded;
931:
1.99 deraadt 932: if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107 markus 933: packet_put_cstring(password);
1.99 deraadt 934: return;
935: }
1.97 markus 936: size = roundup(strlen(password) + 1, 32);
1.179 djm 937: padded = xcalloc(1, size);
1.97 markus 938: strlcpy(padded, password, size);
939: packet_put_string(padded, size);
940: memset(padded, 0, size);
941: xfree(padded);
1.132 markus 942: }
943:
944: static int
945: show_key_from_file(const char *file, const char *host, int keytype)
946: {
947: Key *found;
948: char *fp;
949: int line, ret;
950:
951: found = key_new(keytype);
952: if ((ret = lookup_key_in_hostfile_by_type(file, host,
953: keytype, found, &line))) {
954: fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1.138 itojun 955: logit("WARNING: %s key found for host %s\n"
1.133 markus 956: "in %s:%d\n"
1.132 markus 957: "%s key fingerprint %s.",
958: key_type(found), host, file, line,
959: key_type(found), fp);
960: xfree(fp);
961: }
962: key_free(found);
963: return (ret);
964: }
965:
966: /* print all known host keys for a given host, but skip keys of given type */
967: static int
968: show_other_keys(const char *host, Key *key)
969: {
970: int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
971: int i, found = 0;
972:
973: for (i = 0; type[i] != -1; i++) {
974: if (type[i] == key->type)
975: continue;
976: if (type[i] != KEY_RSA1 &&
977: show_key_from_file(options.user_hostfile2, host, type[i])) {
978: found = 1;
979: continue;
980: }
981: if (type[i] != KEY_RSA1 &&
982: show_key_from_file(options.system_hostfile2, host, type[i])) {
983: found = 1;
984: continue;
985: }
986: if (show_key_from_file(options.user_hostfile, host, type[i])) {
987: found = 1;
988: continue;
989: }
990: if (show_key_from_file(options.system_hostfile, host, type[i])) {
991: found = 1;
992: continue;
993: }
994: debug2("no key of type %d for host %s", type[i], host);
995: }
996: return (found);
1.150 jakob 997: }
998:
999: static void
1000: warn_changed_key(Key *host_key)
1001: {
1002: char *fp;
1.151 jakob 1003: const char *type = key_type(host_key);
1.150 jakob 1004:
1005: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1006:
1007: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1008: error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1009: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1010: error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1011: error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1012: error("It is also possible that the %s host key has just been changed.", type);
1013: error("The fingerprint for the %s key sent by the remote host is\n%s.",
1014: type, fp);
1015: error("Please contact your system administrator.");
1016:
1017: xfree(fp);
1.171 reyk 1018: }
1019:
1020: /*
1021: * Execute a local command
1022: */
1023: int
1024: ssh_local_cmd(const char *args)
1025: {
1026: char *shell;
1027: pid_t pid;
1028: int status;
1029:
1030: if (!options.permit_local_command ||
1031: args == NULL || !*args)
1032: return (1);
1033:
1034: if ((shell = getenv("SHELL")) == NULL)
1035: shell = _PATH_BSHELL;
1036:
1037: pid = fork();
1038: if (pid == 0) {
1039: debug3("Executing %s -c \"%s\"", shell, args);
1040: execl(shell, shell, "-c", args, (char *)NULL);
1041: error("Couldn't execute %s -c \"%s\": %s",
1042: shell, args, strerror(errno));
1043: _exit(1);
1044: } else if (pid == -1)
1045: fatal("fork failed: %.100s", strerror(errno));
1046: while (waitpid(pid, &status, 0) == -1)
1047: if (errno != EINTR)
1048: fatal("Couldn't wait for child: %s", strerror(errno));
1049:
1050: if (!WIFEXITED(status))
1051: return (1);
1052:
1053: return (WEXITSTATUS(status));
1.1 deraadt 1054: }