Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.153
1.1 deraadt 1: /*
1.39 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Code to connect to a remote host, and to perform the client side of the
6: * login (authentication) dialog.
1.78 deraadt 7: *
8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.39 deraadt 13: */
1.1 deraadt 14:
15: #include "includes.h"
1.153 ! jakob 16: RCSID("$OpenBSD: sshconnect.c,v 1.152 2003/11/10 16:23:41 jakob Exp $");
1.1 deraadt 17:
1.66 markus 18: #include <openssl/bn.h>
1.71 markus 19:
1.91 markus 20: #include "ssh.h"
1.1 deraadt 21: #include "xmalloc.h"
22: #include "rsa.h"
1.59 markus 23: #include "buffer.h"
1.1 deraadt 24: #include "packet.h"
25: #include "uidswap.h"
1.21 markus 26: #include "compat.h"
1.58 markus 27: #include "key.h"
1.71 markus 28: #include "sshconnect.h"
1.58 markus 29: #include "hostfile.h"
1.91 markus 30: #include "log.h"
31: #include "readconf.h"
32: #include "atomicio.h"
33: #include "misc.h"
1.119 markus 34: #include "readpass.h"
1.51 markus 35:
1.140 jakob 36: #include "dns.h"
37:
1.70 markus 38: char *client_version_string = NULL;
39: char *server_version_string = NULL;
1.59 markus 40:
1.153 ! jakob 41: int matching_host_key_dns = 0;
1.145 jakob 42:
1.124 markus 43: /* import */
1.43 markus 44: extern Options options;
1.50 markus 45: extern char *__progname;
1.124 markus 46: extern uid_t original_real_uid;
47: extern uid_t original_effective_uid;
1.135 djm 48: extern pid_t proxy_command_pid;
1.91 markus 49:
1.132 markus 50: static int show_other_keys(const char *, Key *);
1.150 jakob 51: static void warn_changed_key(Key *);
1.132 markus 52:
1.39 deraadt 53: /*
54: * Connect to the given ssh server using a proxy command.
55: */
1.109 itojun 56: static int
1.124 markus 57: ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
1.1 deraadt 58: {
1.38 markus 59: Buffer command;
60: const char *cp;
61: char *command_string;
62: int pin[2], pout[2];
1.69 deraadt 63: pid_t pid;
1.49 markus 64: char strport[NI_MAXSERV];
1.38 markus 65:
66: /* Convert the port number into a string. */
1.49 markus 67: snprintf(strport, sizeof strport, "%hu", port);
1.38 markus 68:
1.135 djm 69: /*
70: * Build the final command string in the buffer by making the
71: * appropriate substitutions to the given proxy command.
72: *
73: * Use "exec" to avoid "sh -c" processes on some platforms
74: * (e.g. Solaris)
75: */
1.38 markus 76: buffer_init(&command);
1.135 djm 77: buffer_append(&command, "exec ", 5);
78:
1.38 markus 79: for (cp = proxy_command; *cp; cp++) {
80: if (cp[0] == '%' && cp[1] == '%') {
81: buffer_append(&command, "%", 1);
82: cp++;
83: continue;
84: }
85: if (cp[0] == '%' && cp[1] == 'h') {
86: buffer_append(&command, host, strlen(host));
87: cp++;
88: continue;
89: }
90: if (cp[0] == '%' && cp[1] == 'p') {
1.49 markus 91: buffer_append(&command, strport, strlen(strport));
1.38 markus 92: cp++;
93: continue;
94: }
95: buffer_append(&command, cp, 1);
96: }
97: buffer_append(&command, "\0", 1);
98:
99: /* Get the final command string. */
100: command_string = buffer_ptr(&command);
101:
102: /* Create pipes for communicating with the proxy. */
103: if (pipe(pin) < 0 || pipe(pout) < 0)
104: fatal("Could not create pipes to communicate with the proxy: %.100s",
1.118 deraadt 105: strerror(errno));
1.38 markus 106:
107: debug("Executing proxy command: %.500s", command_string);
108:
109: /* Fork and execute the proxy command. */
110: if ((pid = fork()) == 0) {
111: char *argv[10];
112:
113: /* Child. Permanently give up superuser privileges. */
1.124 markus 114: seteuid(original_real_uid);
115: setuid(original_real_uid);
1.38 markus 116:
117: /* Redirect stdin and stdout. */
118: close(pin[1]);
119: if (pin[0] != 0) {
120: if (dup2(pin[0], 0) < 0)
121: perror("dup2 stdin");
122: close(pin[0]);
123: }
124: close(pout[0]);
125: if (dup2(pout[1], 1) < 0)
126: perror("dup2 stdout");
127: /* Cannot be 1 because pin allocated two descriptors. */
128: close(pout[1]);
129:
130: /* Stderr is left as it is so that error messages get
131: printed on the user's terminal. */
1.89 markus 132: argv[0] = _PATH_BSHELL;
1.38 markus 133: argv[1] = "-c";
134: argv[2] = command_string;
135: argv[3] = NULL;
136:
137: /* Execute the proxy command. Note that we gave up any
138: extra privileges above. */
1.89 markus 139: execv(argv[0], argv);
140: perror(argv[0]);
1.38 markus 141: exit(1);
142: }
143: /* Parent. */
144: if (pid < 0)
145: fatal("fork failed: %.100s", strerror(errno));
1.135 djm 146: else
147: proxy_command_pid = pid; /* save pid to clean up later */
1.38 markus 148:
149: /* Close child side of the descriptors. */
150: close(pin[0]);
151: close(pout[1]);
152:
153: /* Free the command name. */
154: buffer_free(&command);
155:
156: /* Set the connection file descriptors. */
157: packet_set_connection(pout[0], pin[1]);
1.1 deraadt 158:
1.110 markus 159: /* Indicate OK return */
160: return 0;
1.1 deraadt 161: }
162:
1.39 deraadt 163: /*
164: * Creates a (possibly privileged) socket for use as the ssh connection.
165: */
1.109 itojun 166: static int
1.139 markus 167: ssh_create_socket(int privileged, struct addrinfo *ai)
1.1 deraadt 168: {
1.105 markus 169: int sock, gaierr;
170: struct addrinfo hints, *res;
1.1 deraadt 171:
1.40 markus 172: /*
173: * If we are running as root and want to connect to a privileged
174: * port, bind our own socket to a privileged port.
175: */
1.38 markus 176: if (privileged) {
177: int p = IPPORT_RESERVED - 1;
1.124 markus 178: PRIV_START;
1.139 markus 179: sock = rresvport_af(&p, ai->ai_family);
1.124 markus 180: PRIV_END;
1.38 markus 181: if (sock < 0)
1.139 markus 182: error("rresvport: af=%d %.100s", ai->ai_family,
183: strerror(errno));
1.55 markus 184: else
185: debug("Allocated local port %d.", p);
1.105 markus 186: return sock;
187: }
1.139 markus 188: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.105 markus 189: if (sock < 0)
190: error("socket: %.100s", strerror(errno));
191:
192: /* Bind the socket to an alternative local IP address */
193: if (options.bind_address == NULL)
194: return sock;
195:
196: memset(&hints, 0, sizeof(hints));
1.139 markus 197: hints.ai_family = ai->ai_family;
198: hints.ai_socktype = ai->ai_socktype;
199: hints.ai_protocol = ai->ai_protocol;
1.105 markus 200: hints.ai_flags = AI_PASSIVE;
201: gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
202: if (gaierr) {
203: error("getaddrinfo: %s: %s", options.bind_address,
204: gai_strerror(gaierr));
205: close(sock);
206: return -1;
207: }
208: if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
209: error("bind: %s: %s", options.bind_address, strerror(errno));
210: close(sock);
211: freeaddrinfo(res);
212: return -1;
1.38 markus 213: }
1.105 markus 214: freeaddrinfo(res);
1.38 markus 215: return sock;
1.1 deraadt 216: }
217:
1.141 djm 218: static int
219: timeout_connect(int sockfd, const struct sockaddr *serv_addr,
220: socklen_t addrlen, int timeout)
221: {
222: fd_set *fdset;
223: struct timeval tv;
224: socklen_t optlen;
1.142 djm 225: int fdsetsz, optval, rc, result = -1;
1.141 djm 226:
227: if (timeout <= 0)
228: return (connect(sockfd, serv_addr, addrlen));
229:
230: if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
231: return (-1);
232:
233: rc = connect(sockfd, serv_addr, addrlen);
234: if (rc == 0)
235: return (0);
236: if (errno != EINPROGRESS)
237: return (-1);
238:
239: fdsetsz = howmany(sockfd + 1, NFDBITS) * sizeof(fd_mask);
240: fdset = (fd_set *)xmalloc(fdsetsz);
241:
1.147 markus 242: memset(fdset, 0, fdsetsz);
1.141 djm 243: FD_SET(sockfd, fdset);
244: tv.tv_sec = timeout;
245: tv.tv_usec = 0;
246:
247: for(;;) {
248: rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
249: if (rc != -1 || errno != EINTR)
250: break;
251: }
252:
253: switch(rc) {
254: case 0:
255: /* Timed out */
256: errno = ETIMEDOUT;
1.142 djm 257: break;
1.141 djm 258: case -1:
259: /* Select error */
260: debug("select: %s", strerror(errno));
1.142 djm 261: break;
1.141 djm 262: case 1:
263: /* Completed or failed */
264: optval = 0;
265: optlen = sizeof(optval);
266: if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
1.148 markus 267: &optlen) == -1) {
1.141 djm 268: debug("getsockopt: %s", strerror(errno));
1.142 djm 269: break;
1.148 markus 270: }
1.141 djm 271: if (optval != 0) {
272: errno = optval;
1.142 djm 273: break;
1.141 djm 274: }
1.142 djm 275: result = 0;
1.141 djm 276: break;
277: default:
278: /* Should not occur */
279: fatal("Bogus return (%d) from select()", rc);
280: }
281:
1.142 djm 282: xfree(fdset);
283: return (result);
1.141 djm 284: }
285:
1.39 deraadt 286: /*
1.49 markus 287: * Opens a TCP/IP connection to the remote server on the given host.
288: * The address of the remote host will be returned in hostaddr.
1.124 markus 289: * If port is 0, the default port will be used. If needpriv is true,
1.39 deraadt 290: * a privileged port will be allocated to make the connection.
1.124 markus 291: * This requires super-user privileges if needpriv is true.
1.39 deraadt 292: * Connection_attempts specifies the maximum number of tries (one per
293: * second). If proxy_command is non-NULL, it specifies the command (with %h
294: * and %p substituted for host and port, respectively) to use to contact
295: * the daemon.
1.110 markus 296: * Return values:
297: * 0 for OK
298: * ECONNREFUSED if we got a "Connection Refused" by the peer on any address
299: * ECONNABORTED if we failed without a "Connection refused"
300: * Suitable error messages for the connection failure will already have been
301: * printed.
1.39 deraadt 302: */
1.38 markus 303: int
1.49 markus 304: ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
1.115 markus 305: u_short port, int family, int connection_attempts,
1.124 markus 306: int needpriv, const char *proxy_command)
1.1 deraadt 307: {
1.90 markus 308: int gaierr;
309: int on = 1;
1.49 markus 310: int sock = -1, attempt;
1.90 markus 311: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.49 markus 312: struct addrinfo hints, *ai, *aitop;
1.90 markus 313: struct servent *sp;
1.110 markus 314: /*
315: * Did we get only other errors than "Connection refused" (which
316: * should block fallback to rsh and similar), or did we get at least
317: * one "Connection refused"?
318: */
319: int full_failure = 1;
1.38 markus 320:
1.136 markus 321: debug2("ssh_connect: needpriv %d", needpriv);
1.38 markus 322:
323: /* Get default port if port has not been set. */
324: if (port == 0) {
325: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
326: if (sp)
327: port = ntohs(sp->s_port);
328: else
329: port = SSH_DEFAULT_PORT;
330: }
331: /* If a proxy command is given, connect using it. */
332: if (proxy_command != NULL)
1.124 markus 333: return ssh_proxy_connect(host, port, proxy_command);
1.38 markus 334:
335: /* No proxy command. */
336:
1.49 markus 337: memset(&hints, 0, sizeof(hints));
1.115 markus 338: hints.ai_family = family;
1.49 markus 339: hints.ai_socktype = SOCK_STREAM;
1.126 deraadt 340: snprintf(strport, sizeof strport, "%u", port);
1.49 markus 341: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
1.50 markus 342: fatal("%s: %.100s: %s", __progname, host,
343: gai_strerror(gaierr));
1.38 markus 344:
1.46 markus 345: /*
346: * Try to connect several times. On some machines, the first time
347: * will sometimes fail. In general socket code appears to behave
348: * quite magically on many machines.
1.110 markus 349: */
350: for (attempt = 0; ;) {
1.38 markus 351: if (attempt > 0)
352: debug("Trying again...");
353:
1.49 markus 354: /* Loop through addresses for this host, and try each one in
1.68 markus 355: sequence until the connection succeeds. */
1.49 markus 356: for (ai = aitop; ai; ai = ai->ai_next) {
357: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
358: continue;
359: if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
360: ntop, sizeof(ntop), strport, sizeof(strport),
361: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
362: error("ssh_connect: getnameinfo failed");
363: continue;
364: }
365: debug("Connecting to %.200s [%.100s] port %s.",
366: host, ntop, strport);
367:
368: /* Create a socket for connecting. */
1.139 markus 369: sock = ssh_create_socket(needpriv, ai);
1.49 markus 370: if (sock < 0)
1.110 markus 371: /* Any error is already output */
1.49 markus 372: continue;
373:
1.141 djm 374: if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
375: options.connection_timeout) >= 0) {
1.49 markus 376: /* Successful connection. */
1.102 markus 377: memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
1.38 markus 378: break;
1.49 markus 379: } else {
1.110 markus 380: if (errno == ECONNREFUSED)
381: full_failure = 0;
1.131 itojun 382: debug("connect to address %s port %s: %s",
383: ntop, strport, strerror(errno));
1.40 markus 384: /*
385: * Close the failed socket; there appear to
386: * be some problems when reusing a socket for
387: * which connect() has already returned an
388: * error.
389: */
1.38 markus 390: close(sock);
391: }
392: }
1.49 markus 393: if (ai)
394: break; /* Successful connection. */
1.1 deraadt 395:
1.110 markus 396: attempt++;
397: if (attempt >= connection_attempts)
398: break;
1.38 markus 399: /* Sleep a moment before retrying. */
400: sleep(1);
401: }
1.49 markus 402:
403: freeaddrinfo(aitop);
404:
1.38 markus 405: /* Return failure if we didn't get a successful connection. */
1.130 itojun 406: if (attempt >= connection_attempts) {
1.138 itojun 407: logit("ssh: connect to host %s port %s: %s",
1.130 itojun 408: host, strport, strerror(errno));
1.110 markus 409: return full_failure ? ECONNABORTED : ECONNREFUSED;
1.130 itojun 410: }
1.38 markus 411:
412: debug("Connection established.");
1.90 markus 413:
414: /* Set keepalives if requested. */
415: if (options.keepalives &&
416: setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
417: sizeof(on)) < 0)
418: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.38 markus 419:
420: /* Set the connection. */
421: packet_set_connection(sock, sock);
1.1 deraadt 422:
1.110 markus 423: return 0;
1.59 markus 424: }
425:
1.43 markus 426: /*
1.39 deraadt 427: * Waits for the server identification string, and sends our own
428: * identification string.
429: */
1.109 itojun 430: static void
1.95 itojun 431: ssh_exchange_identification(void)
1.1 deraadt 432: {
1.38 markus 433: char buf[256], remote_version[256]; /* must be same size! */
1.64 markus 434: int remote_major, remote_minor, i, mismatch;
1.38 markus 435: int connection_in = packet_get_connection_in();
436: int connection_out = packet_get_connection_out();
1.93 stevesk 437: int minor1 = PROTOCOL_MINOR_1;
1.38 markus 438:
439: /* Read other side\'s version identification. */
1.75 markus 440: for (;;) {
441: for (i = 0; i < sizeof(buf) - 1; i++) {
1.76 markus 442: int len = atomicio(read, connection_in, &buf[i], 1);
1.75 markus 443: if (len < 0)
444: fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
445: if (len != 1)
446: fatal("ssh_exchange_identification: Connection closed by remote host");
447: if (buf[i] == '\r') {
448: buf[i] = '\n';
449: buf[i + 1] = 0;
450: continue; /**XXX wait for \n */
451: }
452: if (buf[i] == '\n') {
453: buf[i + 1] = 0;
454: break;
455: }
1.38 markus 456: }
1.75 markus 457: buf[sizeof(buf) - 1] = 0;
1.76 markus 458: if (strncmp(buf, "SSH-", 4) == 0)
1.38 markus 459: break;
1.75 markus 460: debug("ssh_exchange_identification: %s", buf);
1.38 markus 461: }
1.59 markus 462: server_version_string = xstrdup(buf);
1.38 markus 463:
1.40 markus 464: /*
465: * Check that the versions match. In future this might accept
466: * several versions and set appropriate flags to handle them.
467: */
1.59 markus 468: if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
469: &remote_major, &remote_minor, remote_version) != 3)
1.38 markus 470: fatal("Bad remote protocol version identification: '%.100s'", buf);
471: debug("Remote protocol version %d.%d, remote software version %.100s",
1.118 deraadt 472: remote_major, remote_minor, remote_version);
1.38 markus 473:
1.59 markus 474: compat_datafellows(remote_version);
1.64 markus 475: mismatch = 0;
1.59 markus 476:
1.116 deraadt 477: switch (remote_major) {
1.64 markus 478: case 1:
479: if (remote_minor == 99 &&
480: (options.protocol & SSH_PROTO_2) &&
481: !(options.protocol & SSH_PROTO_1_PREFERRED)) {
482: enable_compat20();
483: break;
484: }
485: if (!(options.protocol & SSH_PROTO_1)) {
486: mismatch = 1;
487: break;
488: }
489: if (remote_minor < 3) {
490: fatal("Remote machine has too old SSH software version.");
1.81 markus 491: } else if (remote_minor == 3 || remote_minor == 4) {
1.64 markus 492: /* We speak 1.3, too. */
493: enable_compat13();
1.81 markus 494: minor1 = 3;
1.64 markus 495: if (options.forward_agent) {
1.138 itojun 496: logit("Agent forwarding disabled for protocol 1.3");
1.64 markus 497: options.forward_agent = 0;
498: }
499: }
500: break;
501: case 2:
502: if (options.protocol & SSH_PROTO_2) {
503: enable_compat20();
504: break;
1.38 markus 505: }
1.64 markus 506: /* FALLTHROUGH */
1.68 markus 507: default:
1.64 markus 508: mismatch = 1;
509: break;
1.38 markus 510: }
1.64 markus 511: if (mismatch)
1.38 markus 512: fatal("Protocol major versions differ: %d vs. %d",
1.64 markus 513: (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
514: remote_major);
1.38 markus 515: /* Send our own protocol version identification. */
516: snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.64 markus 517: compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
1.81 markus 518: compat20 ? PROTOCOL_MINOR_2 : minor1,
1.59 markus 519: SSH_VERSION);
1.146 deraadt 520: if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
1.38 markus 521: fatal("write: %.100s", strerror(errno));
1.59 markus 522: client_version_string = xstrdup(buf);
523: chop(client_version_string);
524: chop(server_version_string);
525: debug("Local version string %.100s", client_version_string);
1.1 deraadt 526: }
527:
1.96 markus 528: /* defaults to 'no' */
1.109 itojun 529: static int
1.112 markus 530: confirm(const char *prompt)
1.1 deraadt 531: {
1.119 markus 532: const char *msg, *again = "Please type 'yes' or 'no': ";
533: char *p;
534: int ret = -1;
1.96 markus 535:
536: if (options.batch_mode)
537: return 0;
1.119 markus 538: for (msg = prompt;;msg = again) {
539: p = read_passphrase(msg, RP_ECHO);
540: if (p == NULL ||
541: (p[0] == '\0') || (p[0] == '\n') ||
542: strncasecmp(p, "no", 2) == 0)
543: ret = 0;
1.127 markus 544: if (p && strncasecmp(p, "yes", 3) == 0)
1.119 markus 545: ret = 1;
546: if (p)
547: xfree(p);
548: if (ret != -1)
549: return ret;
1.1 deraadt 550: }
551: }
552:
1.39 deraadt 553: /*
1.108 markus 554: * check whether the supplied host key is valid, return -1 if the key
555: * is not valid. the user_hostfile will not be updated if 'readonly' is true.
1.39 deraadt 556: */
1.109 itojun 557: static int
1.70 markus 558: check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
1.108 markus 559: int readonly, const char *user_hostfile, const char *system_hostfile)
1.1 deraadt 560: {
1.58 markus 561: Key *file_key;
1.152 jakob 562: const char *type = key_type(host_key);
1.46 markus 563: char *ip = NULL;
1.100 markus 564: char hostline[1000], *hostp, *fp;
1.38 markus 565: HostStatus host_status;
566: HostStatus ip_status;
1.49 markus 567: int local = 0, host_ip_differ = 0;
568: char ntop[NI_MAXHOST];
1.119 markus 569: char msg[1024];
1.145 jakob 570: int len, host_line, ip_line;
1.85 markus 571: const char *host_file = NULL, *ip_file = NULL;
1.49 markus 572:
573: /*
574: * Force accepting of the host key for loopback/localhost. The
575: * problem is that if the home directory is NFS-mounted to multiple
576: * machines, localhost will refer to a different machine in each of
577: * them, and the user will get bogus HOST_CHANGED warnings. This
578: * essentially disables host authentication for localhost; however,
579: * this is probably not a real problem.
580: */
1.70 markus 581: /** hostaddr == 0! */
1.49 markus 582: switch (hostaddr->sa_family) {
583: case AF_INET:
1.108 markus 584: local = (ntohl(((struct sockaddr_in *)hostaddr)->
585: sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.49 markus 586: break;
587: case AF_INET6:
1.108 markus 588: local = IN6_IS_ADDR_LOOPBACK(
589: &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
1.49 markus 590: break;
591: default:
592: local = 0;
593: break;
594: }
1.111 markus 595: if (options.no_host_authentication_for_localhost == 1 && local &&
596: options.host_key_alias == NULL) {
1.88 markus 597: debug("Forcing accepting of host key for "
598: "loopback/localhost.");
1.108 markus 599: return 0;
1.49 markus 600: }
1.42 markus 601:
602: /*
1.88 markus 603: * We don't have the remote ip-address for connections
604: * using a proxy command
1.42 markus 605: */
1.84 markus 606: if (options.proxy_command == NULL) {
607: if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
1.86 markus 608: NULL, 0, NI_NUMERICHOST) != 0)
1.84 markus 609: fatal("check_host_key: getnameinfo failed");
610: ip = xstrdup(ntop);
611: } else {
612: ip = xstrdup("<no hostip for proxy command>");
1.86 markus 613: }
1.88 markus 614: /*
615: * Turn off check_host_ip if the connection is to localhost, via proxy
616: * command or if we don't have a hostname to compare with
617: */
618: if (options.check_host_ip &&
619: (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
620: options.check_host_ip = 0;
1.86 markus 621:
622: /*
623: * Allow the user to record the key under a different name. This is
624: * useful for ssh tunneling over forwarded connections or if you run
625: * multiple sshd's on different ports on the same machine.
626: */
627: if (options.host_key_alias != NULL) {
628: host = options.host_key_alias;
629: debug("using hostkeyalias: %s", host);
1.84 markus 630: }
1.38 markus 631:
1.46 markus 632: /*
633: * Store the host key from the known host file in here so that we can
634: * compare it with the key for the IP address.
635: */
1.58 markus 636: file_key = key_new(host_key->type);
1.38 markus 637:
1.40 markus 638: /*
639: * Check if the host key is present in the user\'s list of known
640: * hosts or in the systemwide list.
641: */
1.85 markus 642: host_file = user_hostfile;
1.108 markus 643: host_status = check_host_in_hostfile(host_file, host, host_key,
1.118 deraadt 644: file_key, &host_line);
1.85 markus 645: if (host_status == HOST_NEW) {
646: host_file = system_hostfile;
1.108 markus 647: host_status = check_host_in_hostfile(host_file, host, host_key,
648: file_key, &host_line);
1.85 markus 649: }
1.40 markus 650: /*
651: * Also perform check for the ip address, skip the check if we are
652: * localhost or the hostname was an ip address to begin with
653: */
1.88 markus 654: if (options.check_host_ip) {
1.58 markus 655: Key *ip_key = key_new(host_key->type);
1.38 markus 656:
1.85 markus 657: ip_file = user_hostfile;
1.108 markus 658: ip_status = check_host_in_hostfile(ip_file, ip, host_key,
659: ip_key, &ip_line);
1.85 markus 660: if (ip_status == HOST_NEW) {
661: ip_file = system_hostfile;
1.108 markus 662: ip_status = check_host_in_hostfile(ip_file, ip,
663: host_key, ip_key, &ip_line);
1.85 markus 664: }
1.38 markus 665: if (host_status == HOST_CHANGED &&
1.58 markus 666: (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38 markus 667: host_ip_differ = 1;
668:
1.58 markus 669: key_free(ip_key);
1.38 markus 670: } else
671: ip_status = host_status;
672:
1.58 markus 673: key_free(file_key);
1.38 markus 674:
675: switch (host_status) {
676: case HOST_OK:
677: /* The host is known and the key matches. */
1.72 markus 678: debug("Host '%.200s' is known and matches the %s host key.",
679: host, type);
1.85 markus 680: debug("Found key in %s:%d", host_file, host_line);
1.88 markus 681: if (options.check_host_ip && ip_status == HOST_NEW) {
1.108 markus 682: if (readonly)
1.138 itojun 683: logit("%s host key for IP address "
1.108 markus 684: "'%.128s' not in list of known hosts.",
685: type, ip);
686: else if (!add_host_to_hostfile(user_hostfile, ip,
1.118 deraadt 687: host_key))
1.138 itojun 688: logit("Failed to add the %s host key for IP "
1.108 markus 689: "address '%.128s' to the list of known "
690: "hosts (%.30s).", type, ip, user_hostfile);
1.88 markus 691: else
1.138 itojun 692: logit("Warning: Permanently added the %s host "
1.108 markus 693: "key for IP address '%.128s' to the list "
694: "of known hosts.", type, ip);
1.38 markus 695: }
696: break;
697: case HOST_NEW:
1.108 markus 698: if (readonly)
699: goto fail;
1.38 markus 700: /* The host is new. */
701: if (options.strict_host_key_checking == 1) {
1.108 markus 702: /*
703: * User has requested strict host key checking. We
704: * will not add the host key automatically. The only
705: * alternative left is to abort.
706: */
707: error("No %s host key is known for %.200s and you "
708: "have requested strict checking.", type, host);
709: goto fail;
1.38 markus 710: } else if (options.strict_host_key_checking == 2) {
1.145 jakob 711: char msg1[1024], msg2[1024];
712:
713: if (show_other_keys(host, host_key))
714: snprintf(msg1, sizeof(msg1),
715: "\nbut keys of different type are already"
716: " known for this host.");
717: else
718: snprintf(msg1, sizeof(msg1), ".");
1.38 markus 719: /* The default */
1.100 markus 720: fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.145 jakob 721: msg2[0] = '\0';
722: if (options.verify_host_key_dns) {
1.153 ! jakob 723: if (matching_host_key_dns)
1.145 jakob 724: snprintf(msg2, sizeof(msg2),
725: "Matching host key fingerprint"
726: " found in DNS.\n");
727: else
728: snprintf(msg2, sizeof(msg2),
729: "No matching host key fingerprint"
730: " found in DNS.\n");
731: }
1.119 markus 732: snprintf(msg, sizeof(msg),
1.108 markus 733: "The authenticity of host '%.200s (%s)' can't be "
1.132 markus 734: "established%s\n"
1.145 jakob 735: "%s key fingerprint is %s.\n%s"
1.108 markus 736: "Are you sure you want to continue connecting "
1.132 markus 737: "(yes/no)? ",
1.145 jakob 738: host, ip, msg1, type, fp, msg2);
1.100 markus 739: xfree(fp);
1.119 markus 740: if (!confirm(msg))
1.108 markus 741: goto fail;
1.38 markus 742: }
1.88 markus 743: if (options.check_host_ip && ip_status == HOST_NEW) {
1.38 markus 744: snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
745: hostp = hostline;
746: } else
747: hostp = host;
748:
1.108 markus 749: /*
750: * If not in strict mode, add the key automatically to the
751: * local known_hosts file.
752: */
1.70 markus 753: if (!add_host_to_hostfile(user_hostfile, hostp, host_key))
1.138 itojun 754: logit("Failed to add the host to the list of known "
1.108 markus 755: "hosts (%.500s).", user_hostfile);
1.38 markus 756: else
1.138 itojun 757: logit("Warning: Permanently added '%.200s' (%s) to the "
1.108 markus 758: "list of known hosts.", hostp, type);
1.38 markus 759: break;
760: case HOST_CHANGED:
761: if (options.check_host_ip && host_ip_differ) {
762: char *msg;
763: if (ip_status == HOST_NEW)
764: msg = "is unknown";
765: else if (ip_status == HOST_OK)
766: msg = "is unchanged";
767: else
768: msg = "has a different value";
769: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
770: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
771: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72 markus 772: error("The %s host key for %s has changed,", type, host);
1.38 markus 773: error("and the key for the according IP address %s", ip);
774: error("%s. This could either mean that", msg);
775: error("DNS SPOOFING is happening or the IP address for the host");
1.85 markus 776: error("and its host key have changed at the same time.");
1.88 markus 777: if (ip_status != HOST_NEW)
1.85 markus 778: error("Offending key for IP in %s:%d", ip_file, ip_line);
1.38 markus 779: }
780: /* The host key has changed. */
1.150 jakob 781: warn_changed_key(host_key);
1.38 markus 782: error("Add correct host key in %.100s to get rid of this message.",
1.87 markus 783: user_hostfile);
1.85 markus 784: error("Offending key in %s:%d", host_file, host_line);
1.38 markus 785:
1.40 markus 786: /*
787: * If strict host key checking is in use, the user will have
788: * to edit the key manually and we can only abort.
789: */
1.108 markus 790: if (options.strict_host_key_checking) {
791: error("%s host key for %.200s has changed and you have "
792: "requested strict checking.", type, host);
793: goto fail;
794: }
1.38 markus 795:
1.40 markus 796: /*
797: * If strict host key checking has not been requested, allow
1.144 djm 798: * the connection but without MITM-able authentication or
1.40 markus 799: * agent forwarding.
800: */
1.38 markus 801: if (options.password_authentication) {
1.108 markus 802: error("Password authentication is disabled to avoid "
803: "man-in-the-middle attacks.");
1.38 markus 804: options.password_authentication = 0;
1.144 djm 805: }
806: if (options.kbd_interactive_authentication) {
807: error("Keyboard-interactive authentication is disabled"
808: " to avoid man-in-the-middle attacks.");
809: options.kbd_interactive_authentication = 0;
810: options.challenge_response_authentication = 0;
811: }
812: if (options.challenge_response_authentication) {
813: error("Challenge/response authentication is disabled"
814: " to avoid man-in-the-middle attacks.");
815: options.challenge_response_authentication = 0;
1.38 markus 816: }
817: if (options.forward_agent) {
1.108 markus 818: error("Agent forwarding is disabled to avoid "
819: "man-in-the-middle attacks.");
1.38 markus 820: options.forward_agent = 0;
1.83 markus 821: }
822: if (options.forward_x11) {
1.108 markus 823: error("X11 forwarding is disabled to avoid "
824: "man-in-the-middle attacks.");
1.83 markus 825: options.forward_x11 = 0;
826: }
1.108 markus 827: if (options.num_local_forwards > 0 ||
828: options.num_remote_forwards > 0) {
829: error("Port forwarding is disabled to avoid "
830: "man-in-the-middle attacks.");
831: options.num_local_forwards =
1.118 deraadt 832: options.num_remote_forwards = 0;
1.38 markus 833: }
1.40 markus 834: /*
835: * XXX Should permit the user to change to use the new id.
836: * This could be done by converting the host key to an
837: * identifying sentence, tell that the host identifies itself
838: * by that sentence, and ask the user if he/she whishes to
839: * accept the authentication.
840: */
1.38 markus 841: break;
1.132 markus 842: case HOST_FOUND:
843: fatal("internal error");
844: break;
1.88 markus 845: }
846:
847: if (options.check_host_ip && host_status != HOST_CHANGED &&
848: ip_status == HOST_CHANGED) {
1.119 markus 849: snprintf(msg, sizeof(msg),
850: "Warning: the %s host key for '%.200s' "
851: "differs from the key for the IP address '%.128s'"
852: "\nOffending key for IP in %s:%d",
853: type, host, ip, ip_file, ip_line);
854: if (host_status == HOST_OK) {
855: len = strlen(msg);
856: snprintf(msg + len, sizeof(msg) - len,
857: "\nMatching host key in %s:%d",
1.125 deraadt 858: host_file, host_line);
1.119 markus 859: }
1.88 markus 860: if (options.strict_host_key_checking == 1) {
1.143 djm 861: logit("%s", msg);
1.108 markus 862: error("Exiting, you have requested strict checking.");
863: goto fail;
1.88 markus 864: } else if (options.strict_host_key_checking == 2) {
1.119 markus 865: strlcat(msg, "\nAre you sure you want "
866: "to continue connecting (yes/no)? ", sizeof(msg));
867: if (!confirm(msg))
1.108 markus 868: goto fail;
1.119 markus 869: } else {
1.143 djm 870: logit("%s", msg);
1.88 markus 871: }
1.38 markus 872: }
1.82 provos 873:
874: xfree(ip);
1.108 markus 875: return 0;
876:
877: fail:
878: xfree(ip);
879: return -1;
880: }
881:
1.140 jakob 882: /* returns 0 if key verifies or -1 if key does NOT verify */
1.108 markus 883: int
884: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
885: {
886: struct stat st;
1.153 ! jakob 887: int flags = 0;
1.140 jakob 888:
1.153 ! jakob 889: if (options.verify_host_key_dns &&
! 890: verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
! 891:
! 892: if (flags & DNS_VERIFY_FOUND) {
! 893:
! 894: if (options.verify_host_key_dns == 1 &&
! 895: flags & DNS_VERIFY_MATCH &&
! 896: flags & DNS_VERIFY_SECURE)
! 897: return 0;
! 898:
! 899: if (flags & DNS_VERIFY_MATCH) {
! 900: matching_host_key_dns = 1;
! 901: } else {
! 902: warn_changed_key(host_key);
! 903: error("Update the SSHFP RR in DNS with the new "
! 904: "host key to get rid of this message.");
! 905: }
1.140 jakob 906: }
907: }
1.108 markus 908:
909: /* return ok if the key can be found in an old keyfile */
910: if (stat(options.system_hostfile2, &st) == 0 ||
911: stat(options.user_hostfile2, &st) == 0) {
912: if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
913: options.user_hostfile2, options.system_hostfile2) == 0)
914: return 0;
915: }
916: return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
917: 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))
941: *cp = tolower(*cp);
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.97 markus 958: }
959:
960: void
961: ssh_put_password(char *password)
962: {
963: int size;
964: char *padded;
965:
1.99 deraadt 966: if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107 markus 967: packet_put_cstring(password);
1.99 deraadt 968: return;
969: }
1.97 markus 970: size = roundup(strlen(password) + 1, 32);
971: padded = xmalloc(size);
972: memset(padded, 0, size);
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.1 deraadt 1053: }