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