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