Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.98
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.98 ! millert 16: RCSID("$OpenBSD: sshconnect.c,v 1.97 2001/02/15 23:19: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
1.95 itojun 321: ssh_exchange_identification(void)
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.93 stevesk 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.96 markus 420: /* defaults to 'no' */
1.38 markus 421: int
422: read_yes_or_no(const char *prompt, int defval)
1.1 deraadt 423: {
1.38 markus 424: char buf[1024];
425: FILE *f;
426: int retval = -1;
1.96 markus 427:
428: if (options.batch_mode)
429: return 0;
1.38 markus 430:
1.88 markus 431: if (isatty(STDIN_FILENO))
1.38 markus 432: f = stdin;
433: else
1.95 itojun 434: f = fopen(_PATH_TTY, "rw");
1.38 markus 435:
436: if (f == NULL)
437: return 0;
438:
439: fflush(stdout);
440:
441: while (1) {
442: fprintf(stderr, "%s", prompt);
443: if (fgets(buf, sizeof(buf), f) == NULL) {
444: /* Print a newline (the prompt probably didn\'t have one). */
445: fprintf(stderr, "\n");
446: strlcpy(buf, "no", sizeof buf);
447: }
448: /* Remove newline from response. */
449: if (strchr(buf, '\n'))
450: *strchr(buf, '\n') = 0;
451:
452: if (buf[0] == 0)
453: retval = defval;
454: if (strcmp(buf, "yes") == 0)
455: retval = 1;
1.79 markus 456: else if (strcmp(buf, "no") == 0)
1.38 markus 457: retval = 0;
1.79 markus 458: else
459: fprintf(stderr, "Please type 'yes' or 'no'.\n");
1.38 markus 460:
461: if (retval != -1) {
462: if (f != stdin)
463: fclose(f);
464: return retval;
465: }
1.1 deraadt 466: }
467: }
468:
1.39 deraadt 469: /*
1.46 markus 470: * check whether the supplied host key is valid, return only if ok.
1.39 deraadt 471: */
1.46 markus 472:
1.38 markus 473: void
1.70 markus 474: check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
475: const char *user_hostfile, const char *system_hostfile)
1.1 deraadt 476: {
1.58 markus 477: Key *file_key;
1.72 markus 478: char *type = key_type(host_key);
1.46 markus 479: char *ip = NULL;
1.38 markus 480: char hostline[1000], *hostp;
481: HostStatus host_status;
482: HostStatus ip_status;
1.49 markus 483: int local = 0, host_ip_differ = 0;
484: char ntop[NI_MAXHOST];
1.88 markus 485: int host_line, ip_line;
1.85 markus 486: const char *host_file = NULL, *ip_file = NULL;
1.49 markus 487:
488: /*
489: * Force accepting of the host key for loopback/localhost. The
490: * problem is that if the home directory is NFS-mounted to multiple
491: * machines, localhost will refer to a different machine in each of
492: * them, and the user will get bogus HOST_CHANGED warnings. This
493: * essentially disables host authentication for localhost; however,
494: * this is probably not a real problem.
495: */
1.70 markus 496: /** hostaddr == 0! */
1.49 markus 497: switch (hostaddr->sa_family) {
498: case AF_INET:
499: local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
500: break;
501: case AF_INET6:
502: local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
503: break;
504: default:
505: local = 0;
506: break;
507: }
1.88 markus 508: if (local && options.host_key_alias == NULL) {
509: debug("Forcing accepting of host key for "
510: "loopback/localhost.");
511: return;
1.49 markus 512: }
1.42 markus 513:
514: /*
1.88 markus 515: * We don't have the remote ip-address for connections
516: * using a proxy command
1.42 markus 517: */
1.84 markus 518: if (options.proxy_command == NULL) {
519: if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
1.86 markus 520: NULL, 0, NI_NUMERICHOST) != 0)
1.84 markus 521: fatal("check_host_key: getnameinfo failed");
522: ip = xstrdup(ntop);
523: } else {
524: ip = xstrdup("<no hostip for proxy command>");
1.86 markus 525: }
1.88 markus 526: /*
527: * Turn off check_host_ip if the connection is to localhost, via proxy
528: * command or if we don't have a hostname to compare with
529: */
530: if (options.check_host_ip &&
531: (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
532: options.check_host_ip = 0;
1.86 markus 533:
534: /*
535: * Allow the user to record the key under a different name. This is
536: * useful for ssh tunneling over forwarded connections or if you run
537: * multiple sshd's on different ports on the same machine.
538: */
539: if (options.host_key_alias != NULL) {
540: host = options.host_key_alias;
541: debug("using hostkeyalias: %s", host);
1.84 markus 542: }
1.38 markus 543:
1.46 markus 544: /*
545: * Store the host key from the known host file in here so that we can
546: * compare it with the key for the IP address.
547: */
1.58 markus 548: file_key = key_new(host_key->type);
1.38 markus 549:
1.40 markus 550: /*
551: * Check if the host key is present in the user\'s list of known
552: * hosts or in the systemwide list.
553: */
1.85 markus 554: host_file = user_hostfile;
555: host_status = check_host_in_hostfile(host_file, host, host_key, file_key, &host_line);
556: if (host_status == HOST_NEW) {
557: host_file = system_hostfile;
558: host_status = check_host_in_hostfile(host_file, host, host_key, file_key, &host_line);
559: }
1.40 markus 560: /*
561: * Also perform check for the ip address, skip the check if we are
562: * localhost or the hostname was an ip address to begin with
563: */
1.88 markus 564: if (options.check_host_ip) {
1.58 markus 565: Key *ip_key = key_new(host_key->type);
1.38 markus 566:
1.85 markus 567: ip_file = user_hostfile;
568: ip_status = check_host_in_hostfile(ip_file, ip, host_key, ip_key, &ip_line);
569: if (ip_status == HOST_NEW) {
570: ip_file = system_hostfile;
571: ip_status = check_host_in_hostfile(ip_file, ip, host_key, ip_key, &ip_line);
572: }
1.38 markus 573: if (host_status == HOST_CHANGED &&
1.58 markus 574: (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38 markus 575: host_ip_differ = 1;
576:
1.58 markus 577: key_free(ip_key);
1.38 markus 578: } else
579: ip_status = host_status;
580:
1.58 markus 581: key_free(file_key);
1.38 markus 582:
583: switch (host_status) {
584: case HOST_OK:
585: /* The host is known and the key matches. */
1.72 markus 586: debug("Host '%.200s' is known and matches the %s host key.",
587: host, type);
1.85 markus 588: debug("Found key in %s:%d", host_file, host_line);
1.88 markus 589: if (options.check_host_ip && ip_status == HOST_NEW) {
590: if (!add_host_to_hostfile(user_hostfile, ip, host_key))
1.94 itojun 591: log("Failed to add the %s host key for IP address '%.128s' to the list of known hosts (%.30s).",
1.88 markus 592: type, ip, user_hostfile);
593: else
1.94 itojun 594: log("Warning: Permanently added the %s host key for IP address '%.128s' to the list of known hosts.",
1.88 markus 595: type, ip);
1.38 markus 596: }
597: break;
598: case HOST_NEW:
599: /* The host is new. */
600: if (options.strict_host_key_checking == 1) {
601: /* User has requested strict host key checking. We will not add the host key
602: automatically. The only alternative left is to abort. */
1.72 markus 603: fatal("No %s host key is known for %.200s and you have requested strict checking.", type, host);
1.38 markus 604: } else if (options.strict_host_key_checking == 2) {
605: /* The default */
606: char prompt[1024];
607: snprintf(prompt, sizeof(prompt),
1.82 provos 608: "The authenticity of host '%.200s (%s)' can't be established.\n"
1.72 markus 609: "%s key fingerprint is %s.\n"
1.45 deraadt 610: "Are you sure you want to continue connecting (yes/no)? ",
1.87 markus 611: host, ip, type, key_fingerprint(host_key));
1.38 markus 612: if (!read_yes_or_no(prompt, -1))
1.98 ! millert 613: fatal("Aborted by user!");
1.38 markus 614: }
1.88 markus 615: if (options.check_host_ip && ip_status == HOST_NEW) {
1.38 markus 616: snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
617: hostp = hostline;
618: } else
619: hostp = host;
620:
621: /* If not in strict mode, add the key automatically to the local known_hosts file. */
1.70 markus 622: if (!add_host_to_hostfile(user_hostfile, hostp, host_key))
1.38 markus 623: log("Failed to add the host to the list of known hosts (%.500s).",
1.70 markus 624: user_hostfile);
1.38 markus 625: else
1.72 markus 626: log("Warning: Permanently added '%.200s' (%s) to the list of known hosts.",
627: hostp, type);
1.38 markus 628: break;
629: case HOST_CHANGED:
630: if (options.check_host_ip && host_ip_differ) {
631: char *msg;
632: if (ip_status == HOST_NEW)
633: msg = "is unknown";
634: else if (ip_status == HOST_OK)
635: msg = "is unchanged";
636: else
637: msg = "has a different value";
638: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
639: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
640: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72 markus 641: error("The %s host key for %s has changed,", type, host);
1.38 markus 642: error("and the key for the according IP address %s", ip);
643: error("%s. This could either mean that", msg);
644: error("DNS SPOOFING is happening or the IP address for the host");
1.85 markus 645: error("and its host key have changed at the same time.");
1.88 markus 646: if (ip_status != HOST_NEW)
1.85 markus 647: error("Offending key for IP in %s:%d", ip_file, ip_line);
1.38 markus 648: }
649: /* The host key has changed. */
650: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.47 markus 651: error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1.38 markus 652: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
653: error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
654: error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1.72 markus 655: error("It is also possible that the %s host key has just been changed.", type);
1.87 markus 656: error("The fingerprint for the %s key sent by the remote host is\n%s.",
657: type, key_fingerprint(host_key));
1.38 markus 658: error("Please contact your system administrator.");
659: error("Add correct host key in %.100s to get rid of this message.",
1.87 markus 660: user_hostfile);
1.85 markus 661: error("Offending key in %s:%d", host_file, host_line);
1.38 markus 662:
1.40 markus 663: /*
664: * If strict host key checking is in use, the user will have
665: * to edit the key manually and we can only abort.
666: */
1.38 markus 667: if (options.strict_host_key_checking)
1.72 markus 668: fatal("%s host key for %.200s has changed and you have requested strict checking.", type, host);
1.38 markus 669:
1.40 markus 670: /*
671: * If strict host key checking has not been requested, allow
672: * the connection but without password authentication or
673: * agent forwarding.
674: */
1.38 markus 675: if (options.password_authentication) {
676: error("Password authentication is disabled to avoid trojan horses.");
677: options.password_authentication = 0;
678: }
679: if (options.forward_agent) {
680: error("Agent forwarding is disabled to avoid trojan horses.");
681: options.forward_agent = 0;
1.83 markus 682: }
683: if (options.forward_x11) {
684: error("X11 forwarding is disabled to avoid trojan horses.");
685: options.forward_x11 = 0;
686: }
1.93 stevesk 687: if (options.num_local_forwards > 0 || options.num_remote_forwards > 0) {
1.83 markus 688: error("Port forwarding is disabled to avoid trojan horses.");
689: options.num_local_forwards = options.num_remote_forwards = 0;
1.38 markus 690: }
1.40 markus 691: /*
692: * XXX Should permit the user to change to use the new id.
693: * This could be done by converting the host key to an
694: * identifying sentence, tell that the host identifies itself
695: * by that sentence, and ask the user if he/she whishes to
696: * accept the authentication.
697: */
1.38 markus 698: break;
1.88 markus 699: }
700:
701: if (options.check_host_ip && host_status != HOST_CHANGED &&
702: ip_status == HOST_CHANGED) {
703: log("Warning: the %s host key for '%.200s' "
1.94 itojun 704: "differs from the key for the IP address '%.128s'",
1.88 markus 705: type, host, ip);
706: if (host_status == HOST_OK)
707: log("Matching host key in %s:%d", host_file, host_line);
708: log("Offending key for IP in %s:%d", ip_file, ip_line);
709: if (options.strict_host_key_checking == 1) {
710: fatal("Exiting, you have requested strict checking.");
711: } else if (options.strict_host_key_checking == 2) {
1.92 djm 712: if (!read_yes_or_no("Are you sure you want " \
713: "to continue connecting (yes/no)? ", -1))
1.98 ! millert 714: fatal("Aborted by user!");
1.88 markus 715: }
1.38 markus 716: }
1.82 provos 717:
718: xfree(ip);
1.51 markus 719: }
1.70 markus 720:
1.51 markus 721: /*
722: * Starts a dialog with the server, and authenticates the current user on the
723: * server. This does not need any extra privileges. The basic connection
724: * to the server must already have been established before this is called.
725: * If login fails, this function prints an error and never returns.
726: * This function does not require super-user privileges.
727: */
728: void
729: ssh_login(int host_key_valid, RSA *own_host_key, const char *orighost,
730: struct sockaddr *hostaddr, uid_t original_real_uid)
731: {
1.70 markus 732: struct passwd *pw;
1.51 markus 733: char *host, *cp;
1.70 markus 734: char *server_user, *local_user;
735:
736: /* Get local user name. Use it as server user if no user name was given. */
737: pw = getpwuid(original_real_uid);
738: if (!pw)
1.77 deraadt 739: fatal("User id %u not found from user database.", original_real_uid);
1.70 markus 740: local_user = xstrdup(pw->pw_name);
741: server_user = options.user ? options.user : local_user;
1.51 markus 742:
743: /* Convert the user-supplied hostname into all lowercase. */
744: host = xstrdup(orighost);
745: for (cp = host; *cp; cp++)
746: if (isupper(*cp))
747: *cp = tolower(*cp);
748:
749: /* Exchange protocol version identification strings with the server. */
750: ssh_exchange_identification();
751:
752: /* Put the connection into non-blocking mode. */
753: packet_set_nonblocking();
754:
755: /* key exchange */
756: /* authenticate user */
1.59 markus 757: if (compat20) {
758: ssh_kex2(host, hostaddr);
1.70 markus 759: ssh_userauth2(server_user, host);
1.59 markus 760: } else {
761: ssh_kex(host, hostaddr);
1.70 markus 762: ssh_userauth(local_user, server_user, host, host_key_valid, own_host_key);
1.59 markus 763: }
1.97 markus 764: }
765:
766: void
767: ssh_put_password(char *password)
768: {
769: int size;
770: char *padded;
771:
772: size = roundup(strlen(password) + 1, 32);
773: padded = xmalloc(size);
774: memset(padded, 0, size);
775: strlcpy(padded, password, size);
776: packet_put_string(padded, size);
777: memset(padded, 0, size);
778: xfree(padded);
1.1 deraadt 779: }