Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.242
1.242 ! djm 1: /* $OpenBSD: sshconnect.c,v 1.241 2013/10/16 02:31:46 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:
1.174 stevesk 16: #include <sys/types.h>
17: #include <sys/wait.h>
1.175 stevesk 18: #include <sys/stat.h>
1.187 stevesk 19: #include <sys/socket.h>
1.195 stevesk 20: #include <sys/time.h>
1.187 stevesk 21:
22: #include <netinet/in.h>
1.172 stevesk 23:
1.176 stevesk 24: #include <ctype.h>
1.190 stevesk 25: #include <errno.h>
1.216 dtucker 26: #include <fcntl.h>
1.191 stevesk 27: #include <netdb.h>
1.172 stevesk 28: #include <paths.h>
1.199 deraadt 29: #include <signal.h>
1.188 stevesk 30: #include <pwd.h>
1.198 stevesk 31: #include <stdio.h>
1.196 stevesk 32: #include <stdlib.h>
1.193 stevesk 33: #include <string.h>
1.192 stevesk 34: #include <unistd.h>
1.71 markus 35:
1.199 deraadt 36: #include "xmalloc.h"
1.91 markus 37: #include "ssh.h"
1.1 deraadt 38: #include "rsa.h"
1.59 markus 39: #include "buffer.h"
1.1 deraadt 40: #include "packet.h"
41: #include "uidswap.h"
1.21 markus 42: #include "compat.h"
1.58 markus 43: #include "key.h"
1.71 markus 44: #include "sshconnect.h"
1.58 markus 45: #include "hostfile.h"
1.91 markus 46: #include "log.h"
47: #include "readconf.h"
48: #include "atomicio.h"
49: #include "misc.h"
1.140 jakob 50: #include "dns.h"
1.214 andreas 51: #include "roaming.h"
1.239 djm 52: #include "monitor_fdpass.h"
1.219 djm 53: #include "ssh2.h"
1.186 stevesk 54: #include "version.h"
1.140 jakob 55:
1.70 markus 56: char *client_version_string = NULL;
57: char *server_version_string = NULL;
1.59 markus 58:
1.169 stevesk 59: static int matching_host_key_dns = 0;
1.145 jakob 60:
1.227 djm 61: static pid_t proxy_command_pid = 0;
62:
1.124 markus 63: /* import */
1.43 markus 64: extern Options options;
1.50 markus 65: extern char *__progname;
1.124 markus 66: extern uid_t original_real_uid;
67: extern uid_t original_effective_uid;
1.91 markus 68:
1.229 djm 69: static int show_other_keys(struct hostkeys *, Key *);
1.150 jakob 70: static void warn_changed_key(Key *);
1.132 markus 71:
1.239 djm 72: /* Expand a proxy command */
73: static char *
74: expand_proxy_command(const char *proxy_command, const char *user,
75: const char *host, int port)
76: {
77: char *tmp, *ret, strport[NI_MAXSERV];
78:
1.241 djm 79: snprintf(strport, sizeof strport, "%d", port);
1.239 djm 80: xasprintf(&tmp, "exec %s", proxy_command);
81: ret = percent_expand(tmp, "h", host, "p", strport,
82: "r", options.user, (char *)NULL);
83: free(tmp);
84: return ret;
85: }
86:
87: /*
88: * Connect to the given ssh server using a proxy command that passes a
89: * a connected fd back to us.
90: */
91: static int
92: ssh_proxy_fdpass_connect(const char *host, u_short port,
93: const char *proxy_command)
94: {
95: char *command_string;
96: int sp[2], sock;
97: pid_t pid;
98: char *shell;
99:
100: if ((shell = getenv("SHELL")) == NULL)
101: shell = _PATH_BSHELL;
102:
103: if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0)
104: fatal("Could not create socketpair to communicate with "
105: "proxy dialer: %.100s", strerror(errno));
106:
107: command_string = expand_proxy_command(proxy_command, options.user,
108: host, port);
109: debug("Executing proxy dialer command: %.500s", command_string);
110:
111: /* Fork and execute the proxy command. */
112: if ((pid = fork()) == 0) {
113: char *argv[10];
114:
115: /* Child. Permanently give up superuser privileges. */
116: permanently_drop_suid(original_real_uid);
117:
118: close(sp[1]);
119: /* Redirect stdin and stdout. */
120: if (sp[0] != 0) {
121: if (dup2(sp[0], 0) < 0)
122: perror("dup2 stdin");
123: }
124: if (sp[0] != 1) {
125: if (dup2(sp[0], 1) < 0)
126: perror("dup2 stdout");
127: }
128: if (sp[0] >= 2)
129: close(sp[0]);
130:
131: /*
132: * Stderr is left as it is so that error messages get
133: * printed on the user's terminal.
134: */
135: argv[0] = shell;
136: argv[1] = "-c";
137: argv[2] = command_string;
138: argv[3] = NULL;
139:
140: /*
141: * Execute the proxy command.
142: * Note that we gave up any extra privileges above.
143: */
144: execv(argv[0], argv);
145: perror(argv[0]);
146: exit(1);
147: }
148: /* Parent. */
149: if (pid < 0)
150: fatal("fork failed: %.100s", strerror(errno));
151: close(sp[0]);
152: free(command_string);
153:
154: if ((sock = mm_receive_fd(sp[1])) == -1)
155: fatal("proxy dialer did not pass back a connection");
156:
157: while (waitpid(pid, NULL, 0) == -1)
158: if (errno != EINTR)
159: fatal("Couldn't wait for child: %s", strerror(errno));
160:
161: /* Set the connection file descriptors. */
162: packet_set_connection(sock, sock);
163:
164: return 0;
165: }
166:
1.39 deraadt 167: /*
168: * Connect to the given ssh server using a proxy command.
169: */
1.109 itojun 170: static int
1.124 markus 171: ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
1.1 deraadt 172: {
1.239 djm 173: char *command_string;
1.38 markus 174: int pin[2], pout[2];
1.69 deraadt 175: pid_t pid;
1.239 djm 176: char *shell;
1.237 markus 177:
1.226 djm 178: if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
1.201 djm 179: shell = _PATH_BSHELL;
1.38 markus 180:
181: /* Create pipes for communicating with the proxy. */
182: if (pipe(pin) < 0 || pipe(pout) < 0)
183: fatal("Could not create pipes to communicate with the proxy: %.100s",
1.118 deraadt 184: strerror(errno));
1.38 markus 185:
1.239 djm 186: command_string = expand_proxy_command(proxy_command, options.user,
187: host, port);
1.38 markus 188: debug("Executing proxy command: %.500s", command_string);
189:
190: /* Fork and execute the proxy command. */
191: if ((pid = fork()) == 0) {
192: char *argv[10];
193:
194: /* Child. Permanently give up superuser privileges. */
1.184 markus 195: permanently_drop_suid(original_real_uid);
1.38 markus 196:
197: /* Redirect stdin and stdout. */
198: close(pin[1]);
199: if (pin[0] != 0) {
200: if (dup2(pin[0], 0) < 0)
201: perror("dup2 stdin");
202: close(pin[0]);
203: }
204: close(pout[0]);
205: if (dup2(pout[1], 1) < 0)
206: perror("dup2 stdout");
207: /* Cannot be 1 because pin allocated two descriptors. */
208: close(pout[1]);
209:
210: /* Stderr is left as it is so that error messages get
211: printed on the user's terminal. */
1.201 djm 212: argv[0] = shell;
1.38 markus 213: argv[1] = "-c";
214: argv[2] = command_string;
215: argv[3] = NULL;
216:
217: /* Execute the proxy command. Note that we gave up any
218: extra privileges above. */
1.232 djm 219: signal(SIGPIPE, SIG_DFL);
1.89 markus 220: execv(argv[0], argv);
221: perror(argv[0]);
1.38 markus 222: exit(1);
223: }
224: /* Parent. */
225: if (pid < 0)
226: fatal("fork failed: %.100s", strerror(errno));
1.135 djm 227: else
228: proxy_command_pid = pid; /* save pid to clean up later */
1.38 markus 229:
230: /* Close child side of the descriptors. */
231: close(pin[0]);
232: close(pout[1]);
233:
234: /* Free the command name. */
1.238 djm 235: free(command_string);
1.38 markus 236:
237: /* Set the connection file descriptors. */
238: packet_set_connection(pout[0], pin[1]);
1.1 deraadt 239:
1.110 markus 240: /* Indicate OK return */
241: return 0;
1.227 djm 242: }
243:
244: void
245: ssh_kill_proxy_command(void)
246: {
247: /*
248: * Send SIGHUP to proxy command if used. We don't wait() in
249: * case it hangs and instead rely on init to reap the child
250: */
251: if (proxy_command_pid > 1)
1.228 djm 252: kill(proxy_command_pid, SIGHUP);
1.1 deraadt 253: }
254:
1.39 deraadt 255: /*
256: * Creates a (possibly privileged) socket for use as the ssh connection.
257: */
1.109 itojun 258: static int
1.139 markus 259: ssh_create_socket(int privileged, struct addrinfo *ai)
1.1 deraadt 260: {
1.240 djm 261: int sock, r, gaierr;
1.105 markus 262: struct addrinfo hints, *res;
1.1 deraadt 263:
1.217 dtucker 264: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.216 dtucker 265: if (sock < 0) {
1.240 djm 266: error("socket: %s", strerror(errno));
1.216 dtucker 267: return -1;
268: }
269: fcntl(sock, F_SETFD, FD_CLOEXEC);
1.105 markus 270:
271: /* Bind the socket to an alternative local IP address */
1.240 djm 272: if (options.bind_address == NULL && !privileged)
1.105 markus 273: return sock;
274:
275: memset(&hints, 0, sizeof(hints));
1.139 markus 276: hints.ai_family = ai->ai_family;
277: hints.ai_socktype = ai->ai_socktype;
278: hints.ai_protocol = ai->ai_protocol;
1.105 markus 279: hints.ai_flags = AI_PASSIVE;
1.205 dtucker 280: gaierr = getaddrinfo(options.bind_address, NULL, &hints, &res);
1.105 markus 281: if (gaierr) {
282: error("getaddrinfo: %s: %s", options.bind_address,
1.203 dtucker 283: ssh_gai_strerror(gaierr));
1.105 markus 284: close(sock);
285: return -1;
286: }
1.240 djm 287: /*
288: * If we are running as root and want to connect to a privileged
289: * port, bind our own socket to a privileged port.
290: */
291: if (privileged) {
292: PRIV_START;
293: r = bindresvport_sa(sock, res->ai_addr);
294: PRIV_END;
295: if (r < 0) {
296: error("bindresvport_sa: af=%d %s", ai->ai_family,
297: strerror(errno));
298: goto fail;
299: }
300: } else {
301: if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
302: error("bind: %s: %s", options.bind_address,
303: strerror(errno));
304: fail:
305: close(sock);
306: freeaddrinfo(res);
307: return -1;
308: }
1.38 markus 309: }
1.105 markus 310: freeaddrinfo(res);
1.38 markus 311: return sock;
1.1 deraadt 312: }
313:
1.141 djm 314: static int
315: timeout_connect(int sockfd, const struct sockaddr *serv_addr,
1.202 djm 316: socklen_t addrlen, int *timeoutp)
1.141 djm 317: {
318: fd_set *fdset;
1.202 djm 319: struct timeval tv, t_start;
1.141 djm 320: socklen_t optlen;
1.179 djm 321: int optval, rc, result = -1;
1.141 djm 322:
1.202 djm 323: gettimeofday(&t_start, NULL);
324:
325: if (*timeoutp <= 0) {
326: result = connect(sockfd, serv_addr, addrlen);
327: goto done;
328: }
1.141 djm 329:
1.156 djm 330: set_nonblock(sockfd);
1.141 djm 331: rc = connect(sockfd, serv_addr, addrlen);
1.156 djm 332: if (rc == 0) {
333: unset_nonblock(sockfd);
1.202 djm 334: result = 0;
335: goto done;
336: }
337: if (errno != EINPROGRESS) {
338: result = -1;
339: goto done;
1.156 djm 340: }
1.141 djm 341:
1.179 djm 342: fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
343: sizeof(fd_mask));
1.141 djm 344: FD_SET(sockfd, fdset);
1.202 djm 345: ms_to_timeval(&tv, *timeoutp);
1.141 djm 346:
1.162 deraadt 347: for (;;) {
1.141 djm 348: rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
349: if (rc != -1 || errno != EINTR)
350: break;
351: }
352:
1.162 deraadt 353: switch (rc) {
1.141 djm 354: case 0:
355: /* Timed out */
356: errno = ETIMEDOUT;
1.142 djm 357: break;
1.141 djm 358: case -1:
359: /* Select error */
1.154 djm 360: debug("select: %s", strerror(errno));
1.142 djm 361: break;
1.141 djm 362: case 1:
363: /* Completed or failed */
364: optval = 0;
365: optlen = sizeof(optval);
1.154 djm 366: if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
1.148 markus 367: &optlen) == -1) {
1.154 djm 368: debug("getsockopt: %s", strerror(errno));
1.142 djm 369: break;
1.148 markus 370: }
1.141 djm 371: if (optval != 0) {
372: errno = optval;
1.142 djm 373: break;
1.141 djm 374: }
1.142 djm 375: result = 0;
1.156 djm 376: unset_nonblock(sockfd);
1.141 djm 377: break;
378: default:
379: /* Should not occur */
380: fatal("Bogus return (%d) from select()", rc);
381: }
382:
1.238 djm 383: free(fdset);
1.202 djm 384:
385: done:
386: if (result == 0 && *timeoutp > 0) {
387: ms_subtract_diff(&t_start, timeoutp);
388: if (*timeoutp <= 0) {
389: errno = ETIMEDOUT;
390: result = -1;
391: }
392: }
393:
1.142 djm 394: return (result);
1.141 djm 395: }
396:
1.39 deraadt 397: /*
1.49 markus 398: * Opens a TCP/IP connection to the remote server on the given host.
399: * The address of the remote host will be returned in hostaddr.
1.124 markus 400: * If port is 0, the default port will be used. If needpriv is true,
1.39 deraadt 401: * a privileged port will be allocated to make the connection.
1.124 markus 402: * This requires super-user privileges if needpriv is true.
1.39 deraadt 403: * Connection_attempts specifies the maximum number of tries (one per
404: * second). If proxy_command is non-NULL, it specifies the command (with %h
405: * and %p substituted for host and port, respectively) to use to contact
406: * the daemon.
407: */
1.241 djm 408: static int
409: ssh_connect_direct(const char *host, struct addrinfo *aitop,
410: struct sockaddr_storage *hostaddr, u_short port, int family,
411: int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv)
1.1 deraadt 412: {
1.90 markus 413: int on = 1;
1.49 markus 414: int sock = -1, attempt;
1.90 markus 415: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.241 djm 416: struct addrinfo *ai;
1.38 markus 417:
1.136 markus 418: debug2("ssh_connect: needpriv %d", needpriv);
1.38 markus 419:
1.181 markus 420: for (attempt = 0; attempt < connection_attempts; attempt++) {
1.200 markus 421: if (attempt > 0) {
422: /* Sleep a moment before retrying. */
423: sleep(1);
1.38 markus 424: debug("Trying again...");
1.200 markus 425: }
1.181 markus 426: /*
427: * Loop through addresses for this host, and try each one in
428: * sequence until the connection succeeds.
429: */
1.49 markus 430: for (ai = aitop; ai; ai = ai->ai_next) {
1.241 djm 431: if (ai->ai_family != AF_INET &&
432: ai->ai_family != AF_INET6)
1.49 markus 433: continue;
434: if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
435: ntop, sizeof(ntop), strport, sizeof(strport),
436: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
437: error("ssh_connect: getnameinfo failed");
438: continue;
439: }
440: debug("Connecting to %.200s [%.100s] port %s.",
441: host, ntop, strport);
442:
443: /* Create a socket for connecting. */
1.139 markus 444: sock = ssh_create_socket(needpriv, ai);
1.49 markus 445: if (sock < 0)
1.110 markus 446: /* Any error is already output */
1.49 markus 447: continue;
448:
1.141 djm 449: if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
1.202 djm 450: timeout_ms) >= 0) {
1.49 markus 451: /* Successful connection. */
1.102 markus 452: memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
1.38 markus 453: break;
1.49 markus 454: } else {
1.131 itojun 455: debug("connect to address %s port %s: %s",
456: ntop, strport, strerror(errno));
1.38 markus 457: close(sock);
1.181 markus 458: sock = -1;
1.38 markus 459: }
460: }
1.181 markus 461: if (sock != -1)
1.49 markus 462: break; /* Successful connection. */
1.38 markus 463: }
1.49 markus 464:
1.38 markus 465: /* Return failure if we didn't get a successful connection. */
1.181 markus 466: if (sock == -1) {
1.159 markus 467: error("ssh: connect to host %s port %s: %s",
1.130 itojun 468: host, strport, strerror(errno));
1.159 markus 469: return (-1);
1.130 itojun 470: }
1.38 markus 471:
472: debug("Connection established.");
1.90 markus 473:
1.155 markus 474: /* Set SO_KEEPALIVE if requested. */
1.202 djm 475: if (want_keepalive &&
1.90 markus 476: setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
477: sizeof(on)) < 0)
478: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.38 markus 479:
480: /* Set the connection. */
481: packet_set_connection(sock, sock);
1.1 deraadt 482:
1.110 markus 483: return 0;
1.59 markus 484: }
485:
1.241 djm 486: int
487: ssh_connect(const char *host, struct addrinfo *addrs,
488: struct sockaddr_storage *hostaddr, u_short port, int family,
489: int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv)
490: {
491: if (options.proxy_command == NULL) {
492: return ssh_connect_direct(host, addrs, hostaddr, port, family,
493: connection_attempts, timeout_ms, want_keepalive, needpriv);
494: } else if (strcmp(options.proxy_command, "-") == 0) {
495: packet_set_connection(STDIN_FILENO, STDOUT_FILENO);
496: return 0; /* Always succeeds */
497: } else if (options.proxy_use_fdpass) {
498: return ssh_proxy_fdpass_connect(host, port,
499: options.proxy_command);
500: }
501: return ssh_proxy_connect(host, port, options.proxy_command);
502: }
503:
1.235 djm 504: static void
505: send_client_banner(int connection_out, int minor1)
506: {
507: /* Send our own protocol version identification. */
508: if (compat20) {
509: xasprintf(&client_version_string, "SSH-%d.%d-%.100s\r\n",
510: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION);
511: } else {
512: xasprintf(&client_version_string, "SSH-%d.%d-%.100s\n",
513: PROTOCOL_MAJOR_1, minor1, SSH_VERSION);
514: }
515: if (roaming_atomicio(vwrite, connection_out, client_version_string,
516: strlen(client_version_string)) != strlen(client_version_string))
517: fatal("write: %.100s", strerror(errno));
518: chop(client_version_string);
519: debug("Local version string %.100s", client_version_string);
520: }
521:
1.43 markus 522: /*
1.39 deraadt 523: * Waits for the server identification string, and sends our own
524: * identification string.
525: */
1.213 andreas 526: void
1.202 djm 527: ssh_exchange_identification(int timeout_ms)
1.1 deraadt 528: {
1.38 markus 529: char buf[256], remote_version[256]; /* must be same size! */
1.165 djm 530: int remote_major, remote_minor, mismatch;
1.38 markus 531: int connection_in = packet_get_connection_in();
532: int connection_out = packet_get_connection_out();
1.235 djm 533: int minor1 = PROTOCOL_MINOR_1, client_banner_sent = 0;
1.185 djm 534: u_int i, n;
1.202 djm 535: size_t len;
536: int fdsetsz, remaining, rc;
537: struct timeval t_start, t_remaining;
538: fd_set *fdset;
539:
540: fdsetsz = howmany(connection_in + 1, NFDBITS) * sizeof(fd_mask);
541: fdset = xcalloc(1, fdsetsz);
1.38 markus 542:
1.235 djm 543: /*
544: * If we are SSH2-only then we can send the banner immediately and
545: * save a round-trip.
546: */
547: if (options.protocol == SSH_PROTO_2) {
548: enable_compat20();
549: send_client_banner(connection_out, 0);
550: client_banner_sent = 1;
551: }
552:
1.163 avsm 553: /* Read other side's version identification. */
1.202 djm 554: remaining = timeout_ms;
1.185 djm 555: for (n = 0;;) {
1.75 markus 556: for (i = 0; i < sizeof(buf) - 1; i++) {
1.202 djm 557: if (timeout_ms > 0) {
558: gettimeofday(&t_start, NULL);
559: ms_to_timeval(&t_remaining, remaining);
560: FD_SET(connection_in, fdset);
561: rc = select(connection_in + 1, fdset, NULL,
562: fdset, &t_remaining);
563: ms_subtract_diff(&t_start, &remaining);
564: if (rc == 0 || remaining <= 0)
565: fatal("Connection timed out during "
566: "banner exchange");
567: if (rc == -1) {
568: if (errno == EINTR)
569: continue;
570: fatal("ssh_exchange_identification: "
571: "select: %s", strerror(errno));
572: }
573: }
574:
1.214 andreas 575: len = roaming_atomicio(read, connection_in, &buf[i], 1);
1.163 avsm 576:
1.167 djm 577: if (len != 1 && errno == EPIPE)
1.202 djm 578: fatal("ssh_exchange_identification: "
579: "Connection closed by remote host");
1.163 avsm 580: else if (len != 1)
1.202 djm 581: fatal("ssh_exchange_identification: "
582: "read: %.100s", strerror(errno));
1.75 markus 583: if (buf[i] == '\r') {
584: buf[i] = '\n';
585: buf[i + 1] = 0;
586: continue; /**XXX wait for \n */
587: }
588: if (buf[i] == '\n') {
589: buf[i + 1] = 0;
590: break;
591: }
1.185 djm 592: if (++n > 65536)
1.202 djm 593: fatal("ssh_exchange_identification: "
594: "No banner received");
1.38 markus 595: }
1.75 markus 596: buf[sizeof(buf) - 1] = 0;
1.76 markus 597: if (strncmp(buf, "SSH-", 4) == 0)
1.38 markus 598: break;
1.75 markus 599: debug("ssh_exchange_identification: %s", buf);
1.38 markus 600: }
1.59 markus 601: server_version_string = xstrdup(buf);
1.238 djm 602: free(fdset);
1.38 markus 603:
1.40 markus 604: /*
605: * Check that the versions match. In future this might accept
606: * several versions and set appropriate flags to handle them.
607: */
1.59 markus 608: if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
609: &remote_major, &remote_minor, remote_version) != 3)
1.38 markus 610: fatal("Bad remote protocol version identification: '%.100s'", buf);
611: debug("Remote protocol version %d.%d, remote software version %.100s",
1.118 deraadt 612: remote_major, remote_minor, remote_version);
1.38 markus 613:
1.59 markus 614: compat_datafellows(remote_version);
1.64 markus 615: mismatch = 0;
1.59 markus 616:
1.116 deraadt 617: switch (remote_major) {
1.64 markus 618: case 1:
619: if (remote_minor == 99 &&
620: (options.protocol & SSH_PROTO_2) &&
621: !(options.protocol & SSH_PROTO_1_PREFERRED)) {
622: enable_compat20();
623: break;
624: }
625: if (!(options.protocol & SSH_PROTO_1)) {
626: mismatch = 1;
627: break;
628: }
629: if (remote_minor < 3) {
630: fatal("Remote machine has too old SSH software version.");
1.81 markus 631: } else if (remote_minor == 3 || remote_minor == 4) {
1.64 markus 632: /* We speak 1.3, too. */
633: enable_compat13();
1.81 markus 634: minor1 = 3;
1.64 markus 635: if (options.forward_agent) {
1.138 itojun 636: logit("Agent forwarding disabled for protocol 1.3");
1.64 markus 637: options.forward_agent = 0;
638: }
639: }
640: break;
641: case 2:
642: if (options.protocol & SSH_PROTO_2) {
643: enable_compat20();
644: break;
1.38 markus 645: }
1.64 markus 646: /* FALLTHROUGH */
1.68 markus 647: default:
1.64 markus 648: mismatch = 1;
649: break;
1.38 markus 650: }
1.64 markus 651: if (mismatch)
1.38 markus 652: fatal("Protocol major versions differ: %d vs. %d",
1.64 markus 653: (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
654: remote_major);
1.235 djm 655: if (!client_banner_sent)
656: send_client_banner(connection_out, minor1);
1.59 markus 657: chop(server_version_string);
1.1 deraadt 658: }
659:
1.96 markus 660: /* defaults to 'no' */
1.109 itojun 661: static int
1.112 markus 662: confirm(const char *prompt)
1.1 deraadt 663: {
1.119 markus 664: const char *msg, *again = "Please type 'yes' or 'no': ";
665: char *p;
666: int ret = -1;
1.96 markus 667:
668: if (options.batch_mode)
669: return 0;
1.119 markus 670: for (msg = prompt;;msg = again) {
671: p = read_passphrase(msg, RP_ECHO);
672: if (p == NULL ||
673: (p[0] == '\0') || (p[0] == '\n') ||
674: strncasecmp(p, "no", 2) == 0)
675: ret = 0;
1.127 markus 676: if (p && strncasecmp(p, "yes", 3) == 0)
1.119 markus 677: ret = 1;
1.238 djm 678: free(p);
1.119 markus 679: if (ret != -1)
680: return ret;
1.1 deraadt 681: }
682: }
683:
1.219 djm 684: static int
685: check_host_cert(const char *host, const Key *host_key)
686: {
687: const char *reason;
688:
689: if (key_cert_check_authority(host_key, 1, 0, host, &reason) != 0) {
690: error("%s", reason);
691: return 0;
692: }
1.223 djm 693: if (buffer_len(&host_key->cert->critical) != 0) {
694: error("Certificate for %s contains unsupported "
695: "critical options(s)", host);
1.219 djm 696: return 0;
697: }
698: return 1;
699: }
700:
1.229 djm 701: static int
702: sockaddr_is_local(struct sockaddr *hostaddr)
703: {
704: switch (hostaddr->sa_family) {
705: case AF_INET:
706: return (ntohl(((struct sockaddr_in *)hostaddr)->
707: sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
708: case AF_INET6:
709: return IN6_IS_ADDR_LOOPBACK(
710: &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
711: default:
712: return 0;
713: }
714: }
715:
716: /*
717: * Prepare the hostname and ip address strings that are used to lookup
718: * host keys in known_hosts files. These may have a port number appended.
719: */
720: void
721: get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr,
722: u_short port, char **hostfile_hostname, char **hostfile_ipaddr)
723: {
724: char ntop[NI_MAXHOST];
725:
726: /*
727: * We don't have the remote ip-address for connections
728: * using a proxy command
729: */
730: if (hostfile_ipaddr != NULL) {
731: if (options.proxy_command == NULL) {
732: if (getnameinfo(hostaddr, hostaddr->sa_len,
733: ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST) != 0)
734: fatal("check_host_key: getnameinfo failed");
735: *hostfile_ipaddr = put_host_port(ntop, port);
736: } else {
737: *hostfile_ipaddr = xstrdup("<no hostip for proxy "
738: "command>");
739: }
740: }
741:
742: /*
743: * Allow the user to record the key under a different name or
744: * differentiate a non-standard port. This is useful for ssh
745: * tunneling over forwarded connections or if you run multiple
746: * sshd's on different ports on the same machine.
747: */
748: if (hostfile_hostname != NULL) {
749: if (options.host_key_alias != NULL) {
750: *hostfile_hostname = xstrdup(options.host_key_alias);
751: debug("using hostkeyalias: %s", *hostfile_hostname);
752: } else {
753: *hostfile_hostname = put_host_port(hostname, port);
754: }
755: }
756: }
757:
1.39 deraadt 758: /*
1.108 markus 759: * check whether the supplied host key is valid, return -1 if the key
1.234 djm 760: * is not valid. user_hostfile[0] will not be updated if 'readonly' is true.
1.39 deraadt 761: */
1.197 dtucker 762: #define RDRW 0
763: #define RDONLY 1
764: #define ROQUIET 2
1.109 itojun 765: static int
1.197 dtucker 766: check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
1.234 djm 767: Key *host_key, int readonly,
768: char **user_hostfiles, u_int num_user_hostfiles,
769: char **system_hostfiles, u_int num_system_hostfiles)
1.1 deraadt 770: {
1.234 djm 771: HostStatus host_status;
772: HostStatus ip_status;
1.229 djm 773: Key *raw_key = NULL;
1.189 dtucker 774: char *ip = NULL, *host = NULL;
1.204 grunk 775: char hostline[1000], *hostp, *fp, *ra;
1.119 markus 776: char msg[1024];
1.234 djm 777: const char *type;
778: const struct hostkey_entry *host_found, *ip_found;
1.229 djm 779: int len, cancelled_forwarding = 0;
1.234 djm 780: int local = sockaddr_is_local(hostaddr);
781: int r, want_cert = key_is_cert(host_key), host_ip_differ = 0;
1.229 djm 782: struct hostkeys *host_hostkeys, *ip_hostkeys;
1.234 djm 783: u_int i;
1.49 markus 784:
785: /*
786: * Force accepting of the host key for loopback/localhost. The
787: * problem is that if the home directory is NFS-mounted to multiple
788: * machines, localhost will refer to a different machine in each of
789: * them, and the user will get bogus HOST_CHANGED warnings. This
790: * essentially disables host authentication for localhost; however,
791: * this is probably not a real problem.
792: */
1.111 markus 793: if (options.no_host_authentication_for_localhost == 1 && local &&
794: options.host_key_alias == NULL) {
1.88 markus 795: debug("Forcing accepting of host key for "
796: "loopback/localhost.");
1.108 markus 797: return 0;
1.49 markus 798: }
1.42 markus 799:
800: /*
1.229 djm 801: * Prepare the hostname and address strings used for hostkey lookup.
802: * In some cases, these will have a port number appended.
1.42 markus 803: */
1.229 djm 804: get_hostfile_hostname_ipaddr(hostname, hostaddr, port, &host, &ip);
1.206 grunk 805:
806: /*
1.88 markus 807: * Turn off check_host_ip if the connection is to localhost, via proxy
808: * command or if we don't have a hostname to compare with
809: */
1.189 dtucker 810: if (options.check_host_ip && (local ||
811: strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
1.88 markus 812: options.check_host_ip = 0;
1.86 markus 813:
1.229 djm 814: host_hostkeys = init_hostkeys();
1.234 djm 815: for (i = 0; i < num_user_hostfiles; i++)
816: load_hostkeys(host_hostkeys, host, user_hostfiles[i]);
817: for (i = 0; i < num_system_hostfiles; i++)
818: load_hostkeys(host_hostkeys, host, system_hostfiles[i]);
1.229 djm 819:
820: ip_hostkeys = NULL;
821: if (!want_cert && options.check_host_ip) {
822: ip_hostkeys = init_hostkeys();
1.234 djm 823: for (i = 0; i < num_user_hostfiles; i++)
824: load_hostkeys(ip_hostkeys, ip, user_hostfiles[i]);
825: for (i = 0; i < num_system_hostfiles; i++)
826: load_hostkeys(ip_hostkeys, ip, system_hostfiles[i]);
1.84 markus 827: }
1.38 markus 828:
1.219 djm 829: retry:
1.229 djm 830: /* Reload these as they may have changed on cert->key downgrade */
1.219 djm 831: want_cert = key_is_cert(host_key);
832: type = key_type(host_key);
833:
1.46 markus 834: /*
1.170 djm 835: * Check if the host key is present in the user's list of known
1.40 markus 836: * hosts or in the systemwide list.
837: */
1.229 djm 838: host_status = check_key_in_hostkeys(host_hostkeys, host_key,
839: &host_found);
840:
1.40 markus 841: /*
842: * Also perform check for the ip address, skip the check if we are
1.219 djm 843: * localhost, looking for a certificate, or the hostname was an ip
844: * address to begin with.
1.40 markus 845: */
1.229 djm 846: if (!want_cert && ip_hostkeys != NULL) {
847: ip_status = check_key_in_hostkeys(ip_hostkeys, host_key,
848: &ip_found);
1.38 markus 849: if (host_status == HOST_CHANGED &&
1.229 djm 850: (ip_status != HOST_CHANGED ||
851: (ip_found != NULL &&
852: !key_equal(ip_found->key, host_found->key))))
1.38 markus 853: host_ip_differ = 1;
854: } else
855: ip_status = host_status;
856:
857: switch (host_status) {
858: case HOST_OK:
859: /* The host is known and the key matches. */
1.219 djm 860: debug("Host '%.200s' is known and matches the %s host %s.",
861: host, type, want_cert ? "certificate" : "key");
1.229 djm 862: debug("Found %s in %s:%lu", want_cert ? "CA key" : "key",
863: host_found->file, host_found->line);
1.219 djm 864: if (want_cert && !check_host_cert(hostname, host_key))
865: goto fail;
1.88 markus 866: if (options.check_host_ip && ip_status == HOST_NEW) {
1.219 djm 867: if (readonly || want_cert)
1.138 itojun 868: logit("%s host key for IP address "
1.108 markus 869: "'%.128s' not in list of known hosts.",
870: type, ip);
1.234 djm 871: else if (!add_host_to_hostfile(user_hostfiles[0], ip,
1.160 djm 872: host_key, options.hash_known_hosts))
1.138 itojun 873: logit("Failed to add the %s host key for IP "
1.108 markus 874: "address '%.128s' to the list of known "
1.234 djm 875: "hosts (%.30s).", type, ip,
876: user_hostfiles[0]);
1.88 markus 877: else
1.138 itojun 878: logit("Warning: Permanently added the %s host "
1.108 markus 879: "key for IP address '%.128s' to the list "
880: "of known hosts.", type, ip);
1.209 grunk 881: } else if (options.visual_host_key) {
1.204 grunk 882: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
883: ra = key_fingerprint(host_key, SSH_FP_MD5,
884: SSH_FP_RANDOMART);
885: logit("Host key fingerprint is %s\n%s\n", fp, ra);
1.238 djm 886: free(ra);
887: free(fp);
1.38 markus 888: }
889: break;
890: case HOST_NEW:
1.197 dtucker 891: if (options.host_key_alias == NULL && port != 0 &&
892: port != SSH_DEFAULT_PORT) {
893: debug("checking without port identifier");
1.212 stevesk 894: if (check_host_key(hostname, hostaddr, 0, host_key,
1.234 djm 895: ROQUIET, user_hostfiles, num_user_hostfiles,
896: system_hostfiles, num_system_hostfiles) == 0) {
1.197 dtucker 897: debug("found matching key w/out port");
898: break;
899: }
900: }
1.219 djm 901: if (readonly || want_cert)
1.108 markus 902: goto fail;
1.38 markus 903: /* The host is new. */
904: if (options.strict_host_key_checking == 1) {
1.108 markus 905: /*
906: * User has requested strict host key checking. We
907: * will not add the host key automatically. The only
908: * alternative left is to abort.
909: */
910: error("No %s host key is known for %.200s and you "
911: "have requested strict checking.", type, host);
912: goto fail;
1.38 markus 913: } else if (options.strict_host_key_checking == 2) {
1.145 jakob 914: char msg1[1024], msg2[1024];
915:
1.229 djm 916: if (show_other_keys(host_hostkeys, host_key))
1.145 jakob 917: snprintf(msg1, sizeof(msg1),
1.168 djm 918: "\nbut keys of different type are already"
919: " known for this host.");
1.145 jakob 920: else
921: snprintf(msg1, sizeof(msg1), ".");
1.38 markus 922: /* The default */
1.100 markus 923: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.204 grunk 924: ra = key_fingerprint(host_key, SSH_FP_MD5,
925: SSH_FP_RANDOMART);
1.145 jakob 926: msg2[0] = '\0';
927: if (options.verify_host_key_dns) {
1.153 jakob 928: if (matching_host_key_dns)
1.145 jakob 929: snprintf(msg2, sizeof(msg2),
930: "Matching host key fingerprint"
931: " found in DNS.\n");
932: else
933: snprintf(msg2, sizeof(msg2),
934: "No matching host key fingerprint"
935: " found in DNS.\n");
936: }
1.119 markus 937: snprintf(msg, sizeof(msg),
1.108 markus 938: "The authenticity of host '%.200s (%s)' can't be "
1.132 markus 939: "established%s\n"
1.209 grunk 940: "%s key fingerprint is %s.%s%s\n%s"
1.108 markus 941: "Are you sure you want to continue connecting "
1.132 markus 942: "(yes/no)? ",
1.209 grunk 943: host, ip, msg1, type, fp,
944: options.visual_host_key ? "\n" : "",
945: options.visual_host_key ? ra : "",
946: msg2);
1.238 djm 947: free(ra);
948: free(fp);
1.119 markus 949: if (!confirm(msg))
1.108 markus 950: goto fail;
1.38 markus 951: }
1.161 djm 952: /*
953: * If not in strict mode, add the key automatically to the
954: * local known_hosts file.
955: */
1.88 markus 956: if (options.check_host_ip && ip_status == HOST_NEW) {
1.229 djm 957: snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
1.38 markus 958: hostp = hostline;
1.161 djm 959: if (options.hash_known_hosts) {
960: /* Add hash of host and IP separately */
1.234 djm 961: r = add_host_to_hostfile(user_hostfiles[0],
962: host, host_key, options.hash_known_hosts) &&
963: add_host_to_hostfile(user_hostfiles[0], ip,
1.161 djm 964: host_key, options.hash_known_hosts);
965: } else {
966: /* Add unhashed "host,ip" */
1.234 djm 967: r = add_host_to_hostfile(user_hostfiles[0],
1.161 djm 968: hostline, host_key,
969: options.hash_known_hosts);
970: }
971: } else {
1.234 djm 972: r = add_host_to_hostfile(user_hostfiles[0], host,
973: host_key, options.hash_known_hosts);
1.38 markus 974: hostp = host;
1.161 djm 975: }
1.38 markus 976:
1.161 djm 977: if (!r)
1.138 itojun 978: logit("Failed to add the host to the list of known "
1.234 djm 979: "hosts (%.500s).", user_hostfiles[0]);
1.38 markus 980: else
1.138 itojun 981: logit("Warning: Permanently added '%.200s' (%s) to the "
1.108 markus 982: "list of known hosts.", hostp, type);
1.38 markus 983: break;
1.220 djm 984: case HOST_REVOKED:
985: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
986: error("@ WARNING: REVOKED HOST KEY DETECTED! @");
987: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
988: error("The %s host key for %s is marked as revoked.", type, host);
989: error("This could mean that a stolen key is being used to");
990: error("impersonate this host.");
991:
992: /*
993: * If strict host key checking is in use, the user will have
994: * to edit the key manually and we can only abort.
995: */
996: if (options.strict_host_key_checking) {
997: error("%s host key for %.200s was revoked and you have "
998: "requested strict checking.", type, host);
999: goto fail;
1000: }
1001: goto continue_unsafe;
1002:
1.38 markus 1003: case HOST_CHANGED:
1.219 djm 1004: if (want_cert) {
1005: /*
1006: * This is only a debug() since it is valid to have
1007: * CAs with wildcard DNS matches that don't match
1008: * all hosts that one might visit.
1009: */
1010: debug("Host certificate authority does not "
1.229 djm 1011: "match %s in %s:%lu", CA_MARKER,
1012: host_found->file, host_found->line);
1.219 djm 1013: goto fail;
1014: }
1.197 dtucker 1015: if (readonly == ROQUIET)
1016: goto fail;
1.38 markus 1017: if (options.check_host_ip && host_ip_differ) {
1.158 avsm 1018: char *key_msg;
1.38 markus 1019: if (ip_status == HOST_NEW)
1.158 avsm 1020: key_msg = "is unknown";
1.38 markus 1021: else if (ip_status == HOST_OK)
1.158 avsm 1022: key_msg = "is unchanged";
1.38 markus 1023: else
1.158 avsm 1024: key_msg = "has a different value";
1.38 markus 1025: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1026: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1027: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72 markus 1028: error("The %s host key for %s has changed,", type, host);
1.208 ian 1029: error("and the key for the corresponding IP address %s", ip);
1.158 avsm 1030: error("%s. This could either mean that", key_msg);
1.38 markus 1031: error("DNS SPOOFING is happening or the IP address for the host");
1.85 markus 1032: error("and its host key have changed at the same time.");
1.88 markus 1033: if (ip_status != HOST_NEW)
1.229 djm 1034: error("Offending key for IP in %s:%lu",
1035: ip_found->file, ip_found->line);
1.38 markus 1036: }
1037: /* The host key has changed. */
1.150 jakob 1038: warn_changed_key(host_key);
1.38 markus 1039: error("Add correct host key in %.100s to get rid of this message.",
1.234 djm 1040: user_hostfiles[0]);
1.229 djm 1041: error("Offending %s key in %s:%lu", key_type(host_found->key),
1042: host_found->file, host_found->line);
1.38 markus 1043:
1.40 markus 1044: /*
1045: * If strict host key checking is in use, the user will have
1046: * to edit the key manually and we can only abort.
1047: */
1.108 markus 1048: if (options.strict_host_key_checking) {
1049: error("%s host key for %.200s has changed and you have "
1050: "requested strict checking.", type, host);
1051: goto fail;
1052: }
1.38 markus 1053:
1.220 djm 1054: continue_unsafe:
1.40 markus 1055: /*
1056: * If strict host key checking has not been requested, allow
1.144 djm 1057: * the connection but without MITM-able authentication or
1.194 stevesk 1058: * forwarding.
1.40 markus 1059: */
1.38 markus 1060: if (options.password_authentication) {
1.108 markus 1061: error("Password authentication is disabled to avoid "
1062: "man-in-the-middle attacks.");
1.38 markus 1063: options.password_authentication = 0;
1.210 dtucker 1064: cancelled_forwarding = 1;
1.144 djm 1065: }
1066: if (options.kbd_interactive_authentication) {
1067: error("Keyboard-interactive authentication is disabled"
1068: " to avoid man-in-the-middle attacks.");
1069: options.kbd_interactive_authentication = 0;
1070: options.challenge_response_authentication = 0;
1.210 dtucker 1071: cancelled_forwarding = 1;
1.144 djm 1072: }
1073: if (options.challenge_response_authentication) {
1074: error("Challenge/response authentication is disabled"
1075: " to avoid man-in-the-middle attacks.");
1076: options.challenge_response_authentication = 0;
1.210 dtucker 1077: cancelled_forwarding = 1;
1.38 markus 1078: }
1079: if (options.forward_agent) {
1.108 markus 1080: error("Agent forwarding is disabled to avoid "
1081: "man-in-the-middle attacks.");
1.38 markus 1082: options.forward_agent = 0;
1.210 dtucker 1083: cancelled_forwarding = 1;
1.83 markus 1084: }
1085: if (options.forward_x11) {
1.108 markus 1086: error("X11 forwarding is disabled to avoid "
1087: "man-in-the-middle attacks.");
1.83 markus 1088: options.forward_x11 = 0;
1.210 dtucker 1089: cancelled_forwarding = 1;
1.83 markus 1090: }
1.108 markus 1091: if (options.num_local_forwards > 0 ||
1092: options.num_remote_forwards > 0) {
1093: error("Port forwarding is disabled to avoid "
1094: "man-in-the-middle attacks.");
1095: options.num_local_forwards =
1.118 deraadt 1096: options.num_remote_forwards = 0;
1.210 dtucker 1097: cancelled_forwarding = 1;
1.194 stevesk 1098: }
1099: if (options.tun_open != SSH_TUNMODE_NO) {
1100: error("Tunnel forwarding is disabled to avoid "
1101: "man-in-the-middle attacks.");
1102: options.tun_open = SSH_TUNMODE_NO;
1.210 dtucker 1103: cancelled_forwarding = 1;
1.38 markus 1104: }
1.210 dtucker 1105: if (options.exit_on_forward_failure && cancelled_forwarding)
1106: fatal("Error: forwarding disabled due to host key "
1107: "check failure");
1108:
1.40 markus 1109: /*
1110: * XXX Should permit the user to change to use the new id.
1111: * This could be done by converting the host key to an
1112: * identifying sentence, tell that the host identifies itself
1.218 dtucker 1113: * by that sentence, and ask the user if he/she wishes to
1.40 markus 1114: * accept the authentication.
1115: */
1.38 markus 1116: break;
1.132 markus 1117: case HOST_FOUND:
1118: fatal("internal error");
1119: break;
1.88 markus 1120: }
1121:
1122: if (options.check_host_ip && host_status != HOST_CHANGED &&
1123: ip_status == HOST_CHANGED) {
1.119 markus 1124: snprintf(msg, sizeof(msg),
1125: "Warning: the %s host key for '%.200s' "
1126: "differs from the key for the IP address '%.128s'"
1.229 djm 1127: "\nOffending key for IP in %s:%lu",
1128: type, host, ip, ip_found->file, ip_found->line);
1.119 markus 1129: if (host_status == HOST_OK) {
1130: len = strlen(msg);
1131: snprintf(msg + len, sizeof(msg) - len,
1.229 djm 1132: "\nMatching host key in %s:%lu",
1133: host_found->file, host_found->line);
1.119 markus 1134: }
1.88 markus 1135: if (options.strict_host_key_checking == 1) {
1.143 djm 1136: logit("%s", msg);
1.108 markus 1137: error("Exiting, you have requested strict checking.");
1138: goto fail;
1.88 markus 1139: } else if (options.strict_host_key_checking == 2) {
1.119 markus 1140: strlcat(msg, "\nAre you sure you want "
1141: "to continue connecting (yes/no)? ", sizeof(msg));
1142: if (!confirm(msg))
1.108 markus 1143: goto fail;
1.119 markus 1144: } else {
1.143 djm 1145: logit("%s", msg);
1.88 markus 1146: }
1.38 markus 1147: }
1.82 provos 1148:
1.238 djm 1149: free(ip);
1150: free(host);
1.229 djm 1151: if (host_hostkeys != NULL)
1152: free_hostkeys(host_hostkeys);
1153: if (ip_hostkeys != NULL)
1154: free_hostkeys(ip_hostkeys);
1.108 markus 1155: return 0;
1156:
1157: fail:
1.220 djm 1158: if (want_cert && host_status != HOST_REVOKED) {
1.219 djm 1159: /*
1160: * No matching certificate. Downgrade cert to raw key and
1161: * search normally.
1162: */
1163: debug("No matching CA found. Retry with plain key");
1164: raw_key = key_from_private(host_key);
1165: if (key_drop_cert(raw_key) != 0)
1166: fatal("Couldn't drop certificate");
1167: host_key = raw_key;
1168: goto retry;
1169: }
1170: if (raw_key != NULL)
1171: key_free(raw_key);
1.238 djm 1172: free(ip);
1173: free(host);
1.229 djm 1174: if (host_hostkeys != NULL)
1175: free_hostkeys(host_hostkeys);
1176: if (ip_hostkeys != NULL)
1177: free_hostkeys(ip_hostkeys);
1.108 markus 1178: return -1;
1179: }
1180:
1.140 jakob 1181: /* returns 0 if key verifies or -1 if key does NOT verify */
1.108 markus 1182: int
1183: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
1184: {
1.153 jakob 1185: int flags = 0;
1.229 djm 1186: char *fp;
1187:
1188: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1189: debug("Server host key: %s %s", key_type(host_key), fp);
1.238 djm 1190: free(fp);
1.140 jakob 1191:
1.219 djm 1192: /* XXX certs are not yet supported for DNS */
1193: if (!key_is_cert(host_key) && options.verify_host_key_dns &&
1.153 jakob 1194: verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
1195: if (flags & DNS_VERIFY_FOUND) {
1196:
1197: if (options.verify_host_key_dns == 1 &&
1198: flags & DNS_VERIFY_MATCH &&
1199: flags & DNS_VERIFY_SECURE)
1200: return 0;
1201:
1202: if (flags & DNS_VERIFY_MATCH) {
1203: matching_host_key_dns = 1;
1204: } else {
1205: warn_changed_key(host_key);
1206: error("Update the SSHFP RR in DNS with the new "
1207: "host key to get rid of this message.");
1208: }
1.140 jakob 1209: }
1210: }
1.108 markus 1211:
1.234 djm 1212: return check_host_key(host, hostaddr, options.port, host_key, RDRW,
1213: options.user_hostfiles, options.num_user_hostfiles,
1214: options.system_hostfiles, options.num_system_hostfiles);
1.51 markus 1215: }
1.70 markus 1216:
1.51 markus 1217: /*
1218: * Starts a dialog with the server, and authenticates the current user on the
1219: * server. This does not need any extra privileges. The basic connection
1220: * to the server must already have been established before this is called.
1221: * If login fails, this function prints an error and never returns.
1222: * This function does not require super-user privileges.
1223: */
1224: void
1.120 markus 1225: ssh_login(Sensitive *sensitive, const char *orighost,
1.229 djm 1226: struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms)
1.51 markus 1227: {
1.241 djm 1228: char *host;
1.70 markus 1229: char *server_user, *local_user;
1230:
1231: local_user = xstrdup(pw->pw_name);
1232: server_user = options.user ? options.user : local_user;
1.51 markus 1233:
1234: /* Convert the user-supplied hostname into all lowercase. */
1235: host = xstrdup(orighost);
1.241 djm 1236: lowercase(host);
1.51 markus 1237:
1238: /* Exchange protocol version identification strings with the server. */
1.202 djm 1239: ssh_exchange_identification(timeout_ms);
1.51 markus 1240:
1241: /* Put the connection into non-blocking mode. */
1242: packet_set_nonblocking();
1243:
1244: /* key exchange */
1245: /* authenticate user */
1.59 markus 1246: if (compat20) {
1.229 djm 1247: ssh_kex2(host, hostaddr, port);
1.120 markus 1248: ssh_userauth2(local_user, server_user, host, sensitive);
1.59 markus 1249: } else {
1250: ssh_kex(host, hostaddr);
1.120 markus 1251: ssh_userauth1(local_user, server_user, host, sensitive);
1.59 markus 1252: }
1.238 djm 1253: free(local_user);
1.97 markus 1254: }
1255:
1256: void
1257: ssh_put_password(char *password)
1258: {
1259: int size;
1260: char *padded;
1261:
1.99 deraadt 1262: if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107 markus 1263: packet_put_cstring(password);
1.99 deraadt 1264: return;
1265: }
1.97 markus 1266: size = roundup(strlen(password) + 1, 32);
1.179 djm 1267: padded = xcalloc(1, size);
1.97 markus 1268: strlcpy(padded, password, size);
1269: packet_put_string(padded, size);
1270: memset(padded, 0, size);
1.238 djm 1271: free(padded);
1.132 markus 1272: }
1273:
1274: /* print all known host keys for a given host, but skip keys of given type */
1275: static int
1.229 djm 1276: show_other_keys(struct hostkeys *hostkeys, Key *key)
1.132 markus 1277: {
1.242 ! djm 1278: int type[] = {
! 1279: KEY_RSA1,
! 1280: KEY_RSA,
! 1281: KEY_DSA,
! 1282: KEY_ECDSA,
! 1283: KEY_ED25519,
! 1284: -1
! 1285: };
1.229 djm 1286: int i, ret = 0;
1287: char *fp, *ra;
1288: const struct hostkey_entry *found;
1.132 markus 1289:
1290: for (i = 0; type[i] != -1; i++) {
1291: if (type[i] == key->type)
1292: continue;
1.229 djm 1293: if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i], &found))
1.132 markus 1294: continue;
1.229 djm 1295: fp = key_fingerprint(found->key, SSH_FP_MD5, SSH_FP_HEX);
1296: ra = key_fingerprint(found->key, SSH_FP_MD5, SSH_FP_RANDOMART);
1297: logit("WARNING: %s key found for host %s\n"
1298: "in %s:%lu\n"
1299: "%s key fingerprint %s.",
1300: key_type(found->key),
1301: found->host, found->file, found->line,
1302: key_type(found->key), fp);
1303: if (options.visual_host_key)
1304: logit("%s", ra);
1.238 djm 1305: free(ra);
1306: free(fp);
1.229 djm 1307: ret = 1;
1.132 markus 1308: }
1.229 djm 1309: return ret;
1.150 jakob 1310: }
1311:
1312: static void
1313: warn_changed_key(Key *host_key)
1314: {
1315: char *fp;
1316:
1317: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1318:
1319: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1320: error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1321: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1322: error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1323: error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1.230 markus 1324: error("It is also possible that a host key has just been changed.");
1.150 jakob 1325: error("The fingerprint for the %s key sent by the remote host is\n%s.",
1.230 markus 1326: key_type(host_key), fp);
1.150 jakob 1327: error("Please contact your system administrator.");
1328:
1.238 djm 1329: free(fp);
1.171 reyk 1330: }
1331:
1332: /*
1333: * Execute a local command
1334: */
1335: int
1336: ssh_local_cmd(const char *args)
1337: {
1338: char *shell;
1339: pid_t pid;
1340: int status;
1.231 djm 1341: void (*osighand)(int);
1.171 reyk 1342:
1343: if (!options.permit_local_command ||
1344: args == NULL || !*args)
1345: return (1);
1346:
1.226 djm 1347: if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
1.171 reyk 1348: shell = _PATH_BSHELL;
1349:
1.231 djm 1350: osighand = signal(SIGCHLD, SIG_DFL);
1.171 reyk 1351: pid = fork();
1352: if (pid == 0) {
1.232 djm 1353: signal(SIGPIPE, SIG_DFL);
1.171 reyk 1354: debug3("Executing %s -c \"%s\"", shell, args);
1355: execl(shell, shell, "-c", args, (char *)NULL);
1356: error("Couldn't execute %s -c \"%s\": %s",
1357: shell, args, strerror(errno));
1358: _exit(1);
1359: } else if (pid == -1)
1360: fatal("fork failed: %.100s", strerror(errno));
1361: while (waitpid(pid, &status, 0) == -1)
1362: if (errno != EINTR)
1363: fatal("Couldn't wait for child: %s", strerror(errno));
1.231 djm 1364: signal(SIGCHLD, osighand);
1.171 reyk 1365:
1366: if (!WIFEXITED(status))
1367: return (1);
1368:
1369: return (WEXITSTATUS(status));
1.1 deraadt 1370: }