Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.244
1.244 ! djm 1: /* $OpenBSD: sshconnect.c,v 1.243 2013/12/30 23:52:27 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.244 ! djm 655: if ((datafellows & SSH_BUG_DERIVEKEY) != 0)
! 656: fatal("Server version \"%.100s\" uses unsafe key agreement; "
! 657: "refusing connection", remote_version);
1.243 djm 658: if ((datafellows & SSH_BUG_RSASIGMD5) != 0)
659: logit("Server version \"%.100s\" uses unsafe RSA signature "
660: "scheme; disabling use of RSA keys", remote_version);
1.235 djm 661: if (!client_banner_sent)
662: send_client_banner(connection_out, minor1);
1.59 markus 663: chop(server_version_string);
1.1 deraadt 664: }
665:
1.96 markus 666: /* defaults to 'no' */
1.109 itojun 667: static int
1.112 markus 668: confirm(const char *prompt)
1.1 deraadt 669: {
1.119 markus 670: const char *msg, *again = "Please type 'yes' or 'no': ";
671: char *p;
672: int ret = -1;
1.96 markus 673:
674: if (options.batch_mode)
675: return 0;
1.119 markus 676: for (msg = prompt;;msg = again) {
677: p = read_passphrase(msg, RP_ECHO);
678: if (p == NULL ||
679: (p[0] == '\0') || (p[0] == '\n') ||
680: strncasecmp(p, "no", 2) == 0)
681: ret = 0;
1.127 markus 682: if (p && strncasecmp(p, "yes", 3) == 0)
1.119 markus 683: ret = 1;
1.238 djm 684: free(p);
1.119 markus 685: if (ret != -1)
686: return ret;
1.1 deraadt 687: }
688: }
689:
1.219 djm 690: static int
691: check_host_cert(const char *host, const Key *host_key)
692: {
693: const char *reason;
694:
695: if (key_cert_check_authority(host_key, 1, 0, host, &reason) != 0) {
696: error("%s", reason);
697: return 0;
698: }
1.223 djm 699: if (buffer_len(&host_key->cert->critical) != 0) {
700: error("Certificate for %s contains unsupported "
701: "critical options(s)", host);
1.219 djm 702: return 0;
703: }
704: return 1;
705: }
706:
1.229 djm 707: static int
708: sockaddr_is_local(struct sockaddr *hostaddr)
709: {
710: switch (hostaddr->sa_family) {
711: case AF_INET:
712: return (ntohl(((struct sockaddr_in *)hostaddr)->
713: sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
714: case AF_INET6:
715: return IN6_IS_ADDR_LOOPBACK(
716: &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
717: default:
718: return 0;
719: }
720: }
721:
722: /*
723: * Prepare the hostname and ip address strings that are used to lookup
724: * host keys in known_hosts files. These may have a port number appended.
725: */
726: void
727: get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr,
728: u_short port, char **hostfile_hostname, char **hostfile_ipaddr)
729: {
730: char ntop[NI_MAXHOST];
731:
732: /*
733: * We don't have the remote ip-address for connections
734: * using a proxy command
735: */
736: if (hostfile_ipaddr != NULL) {
737: if (options.proxy_command == NULL) {
738: if (getnameinfo(hostaddr, hostaddr->sa_len,
739: ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST) != 0)
740: fatal("check_host_key: getnameinfo failed");
741: *hostfile_ipaddr = put_host_port(ntop, port);
742: } else {
743: *hostfile_ipaddr = xstrdup("<no hostip for proxy "
744: "command>");
745: }
746: }
747:
748: /*
749: * Allow the user to record the key under a different name or
750: * differentiate a non-standard port. This is useful for ssh
751: * tunneling over forwarded connections or if you run multiple
752: * sshd's on different ports on the same machine.
753: */
754: if (hostfile_hostname != NULL) {
755: if (options.host_key_alias != NULL) {
756: *hostfile_hostname = xstrdup(options.host_key_alias);
757: debug("using hostkeyalias: %s", *hostfile_hostname);
758: } else {
759: *hostfile_hostname = put_host_port(hostname, port);
760: }
761: }
762: }
763:
1.39 deraadt 764: /*
1.108 markus 765: * check whether the supplied host key is valid, return -1 if the key
1.234 djm 766: * is not valid. user_hostfile[0] will not be updated if 'readonly' is true.
1.39 deraadt 767: */
1.197 dtucker 768: #define RDRW 0
769: #define RDONLY 1
770: #define ROQUIET 2
1.109 itojun 771: static int
1.197 dtucker 772: check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
1.234 djm 773: Key *host_key, int readonly,
774: char **user_hostfiles, u_int num_user_hostfiles,
775: char **system_hostfiles, u_int num_system_hostfiles)
1.1 deraadt 776: {
1.234 djm 777: HostStatus host_status;
778: HostStatus ip_status;
1.229 djm 779: Key *raw_key = NULL;
1.189 dtucker 780: char *ip = NULL, *host = NULL;
1.204 grunk 781: char hostline[1000], *hostp, *fp, *ra;
1.119 markus 782: char msg[1024];
1.234 djm 783: const char *type;
784: const struct hostkey_entry *host_found, *ip_found;
1.229 djm 785: int len, cancelled_forwarding = 0;
1.234 djm 786: int local = sockaddr_is_local(hostaddr);
787: int r, want_cert = key_is_cert(host_key), host_ip_differ = 0;
1.229 djm 788: struct hostkeys *host_hostkeys, *ip_hostkeys;
1.234 djm 789: u_int i;
1.49 markus 790:
791: /*
792: * Force accepting of the host key for loopback/localhost. The
793: * problem is that if the home directory is NFS-mounted to multiple
794: * machines, localhost will refer to a different machine in each of
795: * them, and the user will get bogus HOST_CHANGED warnings. This
796: * essentially disables host authentication for localhost; however,
797: * this is probably not a real problem.
798: */
1.111 markus 799: if (options.no_host_authentication_for_localhost == 1 && local &&
800: options.host_key_alias == NULL) {
1.88 markus 801: debug("Forcing accepting of host key for "
802: "loopback/localhost.");
1.108 markus 803: return 0;
1.49 markus 804: }
1.42 markus 805:
806: /*
1.229 djm 807: * Prepare the hostname and address strings used for hostkey lookup.
808: * In some cases, these will have a port number appended.
1.42 markus 809: */
1.229 djm 810: get_hostfile_hostname_ipaddr(hostname, hostaddr, port, &host, &ip);
1.206 grunk 811:
812: /*
1.88 markus 813: * Turn off check_host_ip if the connection is to localhost, via proxy
814: * command or if we don't have a hostname to compare with
815: */
1.189 dtucker 816: if (options.check_host_ip && (local ||
817: strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
1.88 markus 818: options.check_host_ip = 0;
1.86 markus 819:
1.229 djm 820: host_hostkeys = init_hostkeys();
1.234 djm 821: for (i = 0; i < num_user_hostfiles; i++)
822: load_hostkeys(host_hostkeys, host, user_hostfiles[i]);
823: for (i = 0; i < num_system_hostfiles; i++)
824: load_hostkeys(host_hostkeys, host, system_hostfiles[i]);
1.229 djm 825:
826: ip_hostkeys = NULL;
827: if (!want_cert && options.check_host_ip) {
828: ip_hostkeys = init_hostkeys();
1.234 djm 829: for (i = 0; i < num_user_hostfiles; i++)
830: load_hostkeys(ip_hostkeys, ip, user_hostfiles[i]);
831: for (i = 0; i < num_system_hostfiles; i++)
832: load_hostkeys(ip_hostkeys, ip, system_hostfiles[i]);
1.84 markus 833: }
1.38 markus 834:
1.219 djm 835: retry:
1.229 djm 836: /* Reload these as they may have changed on cert->key downgrade */
1.219 djm 837: want_cert = key_is_cert(host_key);
838: type = key_type(host_key);
839:
1.46 markus 840: /*
1.170 djm 841: * Check if the host key is present in the user's list of known
1.40 markus 842: * hosts or in the systemwide list.
843: */
1.229 djm 844: host_status = check_key_in_hostkeys(host_hostkeys, host_key,
845: &host_found);
846:
1.40 markus 847: /*
848: * Also perform check for the ip address, skip the check if we are
1.219 djm 849: * localhost, looking for a certificate, or the hostname was an ip
850: * address to begin with.
1.40 markus 851: */
1.229 djm 852: if (!want_cert && ip_hostkeys != NULL) {
853: ip_status = check_key_in_hostkeys(ip_hostkeys, host_key,
854: &ip_found);
1.38 markus 855: if (host_status == HOST_CHANGED &&
1.229 djm 856: (ip_status != HOST_CHANGED ||
857: (ip_found != NULL &&
858: !key_equal(ip_found->key, host_found->key))))
1.38 markus 859: host_ip_differ = 1;
860: } else
861: ip_status = host_status;
862:
863: switch (host_status) {
864: case HOST_OK:
865: /* The host is known and the key matches. */
1.219 djm 866: debug("Host '%.200s' is known and matches the %s host %s.",
867: host, type, want_cert ? "certificate" : "key");
1.229 djm 868: debug("Found %s in %s:%lu", want_cert ? "CA key" : "key",
869: host_found->file, host_found->line);
1.219 djm 870: if (want_cert && !check_host_cert(hostname, host_key))
871: goto fail;
1.88 markus 872: if (options.check_host_ip && ip_status == HOST_NEW) {
1.219 djm 873: if (readonly || want_cert)
1.138 itojun 874: logit("%s host key for IP address "
1.108 markus 875: "'%.128s' not in list of known hosts.",
876: type, ip);
1.234 djm 877: else if (!add_host_to_hostfile(user_hostfiles[0], ip,
1.160 djm 878: host_key, options.hash_known_hosts))
1.138 itojun 879: logit("Failed to add the %s host key for IP "
1.108 markus 880: "address '%.128s' to the list of known "
1.234 djm 881: "hosts (%.30s).", type, ip,
882: user_hostfiles[0]);
1.88 markus 883: else
1.138 itojun 884: logit("Warning: Permanently added the %s host "
1.108 markus 885: "key for IP address '%.128s' to the list "
886: "of known hosts.", type, ip);
1.209 grunk 887: } else if (options.visual_host_key) {
1.204 grunk 888: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
889: ra = key_fingerprint(host_key, SSH_FP_MD5,
890: SSH_FP_RANDOMART);
891: logit("Host key fingerprint is %s\n%s\n", fp, ra);
1.238 djm 892: free(ra);
893: free(fp);
1.38 markus 894: }
895: break;
896: case HOST_NEW:
1.197 dtucker 897: if (options.host_key_alias == NULL && port != 0 &&
898: port != SSH_DEFAULT_PORT) {
899: debug("checking without port identifier");
1.212 stevesk 900: if (check_host_key(hostname, hostaddr, 0, host_key,
1.234 djm 901: ROQUIET, user_hostfiles, num_user_hostfiles,
902: system_hostfiles, num_system_hostfiles) == 0) {
1.197 dtucker 903: debug("found matching key w/out port");
904: break;
905: }
906: }
1.219 djm 907: if (readonly || want_cert)
1.108 markus 908: goto fail;
1.38 markus 909: /* The host is new. */
910: if (options.strict_host_key_checking == 1) {
1.108 markus 911: /*
912: * User has requested strict host key checking. We
913: * will not add the host key automatically. The only
914: * alternative left is to abort.
915: */
916: error("No %s host key is known for %.200s and you "
917: "have requested strict checking.", type, host);
918: goto fail;
1.38 markus 919: } else if (options.strict_host_key_checking == 2) {
1.145 jakob 920: char msg1[1024], msg2[1024];
921:
1.229 djm 922: if (show_other_keys(host_hostkeys, host_key))
1.145 jakob 923: snprintf(msg1, sizeof(msg1),
1.168 djm 924: "\nbut keys of different type are already"
925: " known for this host.");
1.145 jakob 926: else
927: snprintf(msg1, sizeof(msg1), ".");
1.38 markus 928: /* The default */
1.100 markus 929: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.204 grunk 930: ra = key_fingerprint(host_key, SSH_FP_MD5,
931: SSH_FP_RANDOMART);
1.145 jakob 932: msg2[0] = '\0';
933: if (options.verify_host_key_dns) {
1.153 jakob 934: if (matching_host_key_dns)
1.145 jakob 935: snprintf(msg2, sizeof(msg2),
936: "Matching host key fingerprint"
937: " found in DNS.\n");
938: else
939: snprintf(msg2, sizeof(msg2),
940: "No matching host key fingerprint"
941: " found in DNS.\n");
942: }
1.119 markus 943: snprintf(msg, sizeof(msg),
1.108 markus 944: "The authenticity of host '%.200s (%s)' can't be "
1.132 markus 945: "established%s\n"
1.209 grunk 946: "%s key fingerprint is %s.%s%s\n%s"
1.108 markus 947: "Are you sure you want to continue connecting "
1.132 markus 948: "(yes/no)? ",
1.209 grunk 949: host, ip, msg1, type, fp,
950: options.visual_host_key ? "\n" : "",
951: options.visual_host_key ? ra : "",
952: msg2);
1.238 djm 953: free(ra);
954: free(fp);
1.119 markus 955: if (!confirm(msg))
1.108 markus 956: goto fail;
1.38 markus 957: }
1.161 djm 958: /*
959: * If not in strict mode, add the key automatically to the
960: * local known_hosts file.
961: */
1.88 markus 962: if (options.check_host_ip && ip_status == HOST_NEW) {
1.229 djm 963: snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
1.38 markus 964: hostp = hostline;
1.161 djm 965: if (options.hash_known_hosts) {
966: /* Add hash of host and IP separately */
1.234 djm 967: r = add_host_to_hostfile(user_hostfiles[0],
968: host, host_key, options.hash_known_hosts) &&
969: add_host_to_hostfile(user_hostfiles[0], ip,
1.161 djm 970: host_key, options.hash_known_hosts);
971: } else {
972: /* Add unhashed "host,ip" */
1.234 djm 973: r = add_host_to_hostfile(user_hostfiles[0],
1.161 djm 974: hostline, host_key,
975: options.hash_known_hosts);
976: }
977: } else {
1.234 djm 978: r = add_host_to_hostfile(user_hostfiles[0], host,
979: host_key, options.hash_known_hosts);
1.38 markus 980: hostp = host;
1.161 djm 981: }
1.38 markus 982:
1.161 djm 983: if (!r)
1.138 itojun 984: logit("Failed to add the host to the list of known "
1.234 djm 985: "hosts (%.500s).", user_hostfiles[0]);
1.38 markus 986: else
1.138 itojun 987: logit("Warning: Permanently added '%.200s' (%s) to the "
1.108 markus 988: "list of known hosts.", hostp, type);
1.38 markus 989: break;
1.220 djm 990: case HOST_REVOKED:
991: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
992: error("@ WARNING: REVOKED HOST KEY DETECTED! @");
993: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
994: error("The %s host key for %s is marked as revoked.", type, host);
995: error("This could mean that a stolen key is being used to");
996: error("impersonate this host.");
997:
998: /*
999: * If strict host key checking is in use, the user will have
1000: * to edit the key manually and we can only abort.
1001: */
1002: if (options.strict_host_key_checking) {
1003: error("%s host key for %.200s was revoked and you have "
1004: "requested strict checking.", type, host);
1005: goto fail;
1006: }
1007: goto continue_unsafe;
1008:
1.38 markus 1009: case HOST_CHANGED:
1.219 djm 1010: if (want_cert) {
1011: /*
1012: * This is only a debug() since it is valid to have
1013: * CAs with wildcard DNS matches that don't match
1014: * all hosts that one might visit.
1015: */
1016: debug("Host certificate authority does not "
1.229 djm 1017: "match %s in %s:%lu", CA_MARKER,
1018: host_found->file, host_found->line);
1.219 djm 1019: goto fail;
1020: }
1.197 dtucker 1021: if (readonly == ROQUIET)
1022: goto fail;
1.38 markus 1023: if (options.check_host_ip && host_ip_differ) {
1.158 avsm 1024: char *key_msg;
1.38 markus 1025: if (ip_status == HOST_NEW)
1.158 avsm 1026: key_msg = "is unknown";
1.38 markus 1027: else if (ip_status == HOST_OK)
1.158 avsm 1028: key_msg = "is unchanged";
1.38 markus 1029: else
1.158 avsm 1030: key_msg = "has a different value";
1.38 markus 1031: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1032: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1033: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72 markus 1034: error("The %s host key for %s has changed,", type, host);
1.208 ian 1035: error("and the key for the corresponding IP address %s", ip);
1.158 avsm 1036: error("%s. This could either mean that", key_msg);
1.38 markus 1037: error("DNS SPOOFING is happening or the IP address for the host");
1.85 markus 1038: error("and its host key have changed at the same time.");
1.88 markus 1039: if (ip_status != HOST_NEW)
1.229 djm 1040: error("Offending key for IP in %s:%lu",
1041: ip_found->file, ip_found->line);
1.38 markus 1042: }
1043: /* The host key has changed. */
1.150 jakob 1044: warn_changed_key(host_key);
1.38 markus 1045: error("Add correct host key in %.100s to get rid of this message.",
1.234 djm 1046: user_hostfiles[0]);
1.229 djm 1047: error("Offending %s key in %s:%lu", key_type(host_found->key),
1048: host_found->file, host_found->line);
1.38 markus 1049:
1.40 markus 1050: /*
1051: * If strict host key checking is in use, the user will have
1052: * to edit the key manually and we can only abort.
1053: */
1.108 markus 1054: if (options.strict_host_key_checking) {
1055: error("%s host key for %.200s has changed and you have "
1056: "requested strict checking.", type, host);
1057: goto fail;
1058: }
1.38 markus 1059:
1.220 djm 1060: continue_unsafe:
1.40 markus 1061: /*
1062: * If strict host key checking has not been requested, allow
1.144 djm 1063: * the connection but without MITM-able authentication or
1.194 stevesk 1064: * forwarding.
1.40 markus 1065: */
1.38 markus 1066: if (options.password_authentication) {
1.108 markus 1067: error("Password authentication is disabled to avoid "
1068: "man-in-the-middle attacks.");
1.38 markus 1069: options.password_authentication = 0;
1.210 dtucker 1070: cancelled_forwarding = 1;
1.144 djm 1071: }
1072: if (options.kbd_interactive_authentication) {
1073: error("Keyboard-interactive authentication is disabled"
1074: " to avoid man-in-the-middle attacks.");
1075: options.kbd_interactive_authentication = 0;
1076: options.challenge_response_authentication = 0;
1.210 dtucker 1077: cancelled_forwarding = 1;
1.144 djm 1078: }
1079: if (options.challenge_response_authentication) {
1080: error("Challenge/response authentication is disabled"
1081: " to avoid man-in-the-middle attacks.");
1082: options.challenge_response_authentication = 0;
1.210 dtucker 1083: cancelled_forwarding = 1;
1.38 markus 1084: }
1085: if (options.forward_agent) {
1.108 markus 1086: error("Agent forwarding is disabled to avoid "
1087: "man-in-the-middle attacks.");
1.38 markus 1088: options.forward_agent = 0;
1.210 dtucker 1089: cancelled_forwarding = 1;
1.83 markus 1090: }
1091: if (options.forward_x11) {
1.108 markus 1092: error("X11 forwarding is disabled to avoid "
1093: "man-in-the-middle attacks.");
1.83 markus 1094: options.forward_x11 = 0;
1.210 dtucker 1095: cancelled_forwarding = 1;
1.83 markus 1096: }
1.108 markus 1097: if (options.num_local_forwards > 0 ||
1098: options.num_remote_forwards > 0) {
1099: error("Port forwarding is disabled to avoid "
1100: "man-in-the-middle attacks.");
1101: options.num_local_forwards =
1.118 deraadt 1102: options.num_remote_forwards = 0;
1.210 dtucker 1103: cancelled_forwarding = 1;
1.194 stevesk 1104: }
1105: if (options.tun_open != SSH_TUNMODE_NO) {
1106: error("Tunnel forwarding is disabled to avoid "
1107: "man-in-the-middle attacks.");
1108: options.tun_open = SSH_TUNMODE_NO;
1.210 dtucker 1109: cancelled_forwarding = 1;
1.38 markus 1110: }
1.210 dtucker 1111: if (options.exit_on_forward_failure && cancelled_forwarding)
1112: fatal("Error: forwarding disabled due to host key "
1113: "check failure");
1114:
1.40 markus 1115: /*
1116: * XXX Should permit the user to change to use the new id.
1117: * This could be done by converting the host key to an
1118: * identifying sentence, tell that the host identifies itself
1.218 dtucker 1119: * by that sentence, and ask the user if he/she wishes to
1.40 markus 1120: * accept the authentication.
1121: */
1.38 markus 1122: break;
1.132 markus 1123: case HOST_FOUND:
1124: fatal("internal error");
1125: break;
1.88 markus 1126: }
1127:
1128: if (options.check_host_ip && host_status != HOST_CHANGED &&
1129: ip_status == HOST_CHANGED) {
1.119 markus 1130: snprintf(msg, sizeof(msg),
1131: "Warning: the %s host key for '%.200s' "
1132: "differs from the key for the IP address '%.128s'"
1.229 djm 1133: "\nOffending key for IP in %s:%lu",
1134: type, host, ip, ip_found->file, ip_found->line);
1.119 markus 1135: if (host_status == HOST_OK) {
1136: len = strlen(msg);
1137: snprintf(msg + len, sizeof(msg) - len,
1.229 djm 1138: "\nMatching host key in %s:%lu",
1139: host_found->file, host_found->line);
1.119 markus 1140: }
1.88 markus 1141: if (options.strict_host_key_checking == 1) {
1.143 djm 1142: logit("%s", msg);
1.108 markus 1143: error("Exiting, you have requested strict checking.");
1144: goto fail;
1.88 markus 1145: } else if (options.strict_host_key_checking == 2) {
1.119 markus 1146: strlcat(msg, "\nAre you sure you want "
1147: "to continue connecting (yes/no)? ", sizeof(msg));
1148: if (!confirm(msg))
1.108 markus 1149: goto fail;
1.119 markus 1150: } else {
1.143 djm 1151: logit("%s", msg);
1.88 markus 1152: }
1.38 markus 1153: }
1.82 provos 1154:
1.238 djm 1155: free(ip);
1156: free(host);
1.229 djm 1157: if (host_hostkeys != NULL)
1158: free_hostkeys(host_hostkeys);
1159: if (ip_hostkeys != NULL)
1160: free_hostkeys(ip_hostkeys);
1.108 markus 1161: return 0;
1162:
1163: fail:
1.220 djm 1164: if (want_cert && host_status != HOST_REVOKED) {
1.219 djm 1165: /*
1166: * No matching certificate. Downgrade cert to raw key and
1167: * search normally.
1168: */
1169: debug("No matching CA found. Retry with plain key");
1170: raw_key = key_from_private(host_key);
1171: if (key_drop_cert(raw_key) != 0)
1172: fatal("Couldn't drop certificate");
1173: host_key = raw_key;
1174: goto retry;
1175: }
1176: if (raw_key != NULL)
1177: key_free(raw_key);
1.238 djm 1178: free(ip);
1179: free(host);
1.229 djm 1180: if (host_hostkeys != NULL)
1181: free_hostkeys(host_hostkeys);
1182: if (ip_hostkeys != NULL)
1183: free_hostkeys(ip_hostkeys);
1.108 markus 1184: return -1;
1185: }
1186:
1.140 jakob 1187: /* returns 0 if key verifies or -1 if key does NOT verify */
1.108 markus 1188: int
1189: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
1190: {
1.153 jakob 1191: int flags = 0;
1.229 djm 1192: char *fp;
1193:
1194: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1195: debug("Server host key: %s %s", key_type(host_key), fp);
1.238 djm 1196: free(fp);
1.140 jakob 1197:
1.219 djm 1198: /* XXX certs are not yet supported for DNS */
1199: if (!key_is_cert(host_key) && options.verify_host_key_dns &&
1.153 jakob 1200: verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
1201: if (flags & DNS_VERIFY_FOUND) {
1202:
1203: if (options.verify_host_key_dns == 1 &&
1204: flags & DNS_VERIFY_MATCH &&
1205: flags & DNS_VERIFY_SECURE)
1206: return 0;
1207:
1208: if (flags & DNS_VERIFY_MATCH) {
1209: matching_host_key_dns = 1;
1210: } else {
1211: warn_changed_key(host_key);
1212: error("Update the SSHFP RR in DNS with the new "
1213: "host key to get rid of this message.");
1214: }
1.140 jakob 1215: }
1216: }
1.108 markus 1217:
1.234 djm 1218: return check_host_key(host, hostaddr, options.port, host_key, RDRW,
1219: options.user_hostfiles, options.num_user_hostfiles,
1220: options.system_hostfiles, options.num_system_hostfiles);
1.51 markus 1221: }
1.70 markus 1222:
1.51 markus 1223: /*
1224: * Starts a dialog with the server, and authenticates the current user on the
1225: * server. This does not need any extra privileges. The basic connection
1226: * to the server must already have been established before this is called.
1227: * If login fails, this function prints an error and never returns.
1228: * This function does not require super-user privileges.
1229: */
1230: void
1.120 markus 1231: ssh_login(Sensitive *sensitive, const char *orighost,
1.229 djm 1232: struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms)
1.51 markus 1233: {
1.241 djm 1234: char *host;
1.70 markus 1235: char *server_user, *local_user;
1236:
1237: local_user = xstrdup(pw->pw_name);
1238: server_user = options.user ? options.user : local_user;
1.51 markus 1239:
1240: /* Convert the user-supplied hostname into all lowercase. */
1241: host = xstrdup(orighost);
1.241 djm 1242: lowercase(host);
1.51 markus 1243:
1244: /* Exchange protocol version identification strings with the server. */
1.202 djm 1245: ssh_exchange_identification(timeout_ms);
1.51 markus 1246:
1247: /* Put the connection into non-blocking mode. */
1248: packet_set_nonblocking();
1249:
1250: /* key exchange */
1251: /* authenticate user */
1.59 markus 1252: if (compat20) {
1.229 djm 1253: ssh_kex2(host, hostaddr, port);
1.120 markus 1254: ssh_userauth2(local_user, server_user, host, sensitive);
1.59 markus 1255: } else {
1256: ssh_kex(host, hostaddr);
1.120 markus 1257: ssh_userauth1(local_user, server_user, host, sensitive);
1.59 markus 1258: }
1.238 djm 1259: free(local_user);
1.97 markus 1260: }
1261:
1262: void
1263: ssh_put_password(char *password)
1264: {
1265: int size;
1266: char *padded;
1267:
1.99 deraadt 1268: if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107 markus 1269: packet_put_cstring(password);
1.99 deraadt 1270: return;
1271: }
1.97 markus 1272: size = roundup(strlen(password) + 1, 32);
1.179 djm 1273: padded = xcalloc(1, size);
1.97 markus 1274: strlcpy(padded, password, size);
1275: packet_put_string(padded, size);
1276: memset(padded, 0, size);
1.238 djm 1277: free(padded);
1.132 markus 1278: }
1279:
1280: /* print all known host keys for a given host, but skip keys of given type */
1281: static int
1.229 djm 1282: show_other_keys(struct hostkeys *hostkeys, Key *key)
1.132 markus 1283: {
1.242 djm 1284: int type[] = {
1285: KEY_RSA1,
1286: KEY_RSA,
1287: KEY_DSA,
1288: KEY_ECDSA,
1289: KEY_ED25519,
1290: -1
1291: };
1.229 djm 1292: int i, ret = 0;
1293: char *fp, *ra;
1294: const struct hostkey_entry *found;
1.132 markus 1295:
1296: for (i = 0; type[i] != -1; i++) {
1297: if (type[i] == key->type)
1298: continue;
1.229 djm 1299: if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i], &found))
1.132 markus 1300: continue;
1.229 djm 1301: fp = key_fingerprint(found->key, SSH_FP_MD5, SSH_FP_HEX);
1302: ra = key_fingerprint(found->key, SSH_FP_MD5, SSH_FP_RANDOMART);
1303: logit("WARNING: %s key found for host %s\n"
1304: "in %s:%lu\n"
1305: "%s key fingerprint %s.",
1306: key_type(found->key),
1307: found->host, found->file, found->line,
1308: key_type(found->key), fp);
1309: if (options.visual_host_key)
1310: logit("%s", ra);
1.238 djm 1311: free(ra);
1312: free(fp);
1.229 djm 1313: ret = 1;
1.132 markus 1314: }
1.229 djm 1315: return ret;
1.150 jakob 1316: }
1317:
1318: static void
1319: warn_changed_key(Key *host_key)
1320: {
1321: char *fp;
1322:
1323: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1324:
1325: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1326: error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1327: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1328: error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1329: error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1.230 markus 1330: error("It is also possible that a host key has just been changed.");
1.150 jakob 1331: error("The fingerprint for the %s key sent by the remote host is\n%s.",
1.230 markus 1332: key_type(host_key), fp);
1.150 jakob 1333: error("Please contact your system administrator.");
1334:
1.238 djm 1335: free(fp);
1.171 reyk 1336: }
1337:
1338: /*
1339: * Execute a local command
1340: */
1341: int
1342: ssh_local_cmd(const char *args)
1343: {
1344: char *shell;
1345: pid_t pid;
1346: int status;
1.231 djm 1347: void (*osighand)(int);
1.171 reyk 1348:
1349: if (!options.permit_local_command ||
1350: args == NULL || !*args)
1351: return (1);
1352:
1.226 djm 1353: if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
1.171 reyk 1354: shell = _PATH_BSHELL;
1355:
1.231 djm 1356: osighand = signal(SIGCHLD, SIG_DFL);
1.171 reyk 1357: pid = fork();
1358: if (pid == 0) {
1.232 djm 1359: signal(SIGPIPE, SIG_DFL);
1.171 reyk 1360: debug3("Executing %s -c \"%s\"", shell, args);
1361: execl(shell, shell, "-c", args, (char *)NULL);
1362: error("Couldn't execute %s -c \"%s\": %s",
1363: shell, args, strerror(errno));
1364: _exit(1);
1365: } else if (pid == -1)
1366: fatal("fork failed: %.100s", strerror(errno));
1367: while (waitpid(pid, &status, 0) == -1)
1368: if (errno != EINTR)
1369: fatal("Couldn't wait for child: %s", strerror(errno));
1.231 djm 1370: signal(SIGCHLD, osighand);
1.171 reyk 1371:
1372: if (!WIFEXITED(status))
1373: return (1);
1374:
1375: return (WEXITSTATUS(status));
1.1 deraadt 1376: }