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