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