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