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