]> andersk Git - gssapi-openssh.git/blame - openssh/sshconnect.c
Tweak CPPFLAGS in patch configure.ac (not GPT version) to fix build
[gssapi-openssh.git] / openssh / sshconnect.c
CommitLineData
240debe0 1/* $OpenBSD: sshconnect.c,v 1.200 2006/10/10 10:12:45 markus Exp $ */
3c0ef626 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
8 *
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 */
15
16#include "includes.h"
3c0ef626 17
30460aeb 18#include <sys/types.h>
19#include <sys/wait.h>
20#include <sys/stat.h>
21#include <sys/socket.h>
22#ifdef HAVE_SYS_TIME_H
23# include <sys/time.h>
24#endif
25
26#include <netinet/in.h>
27#include <arpa/inet.h>
28
29#include <ctype.h>
30#include <errno.h>
31#include <netdb.h>
32#ifdef HAVE_PATHS_H
33#include <paths.h>
34#endif
35#include <pwd.h>
36#include <stdarg.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
3c0ef626 41
3c0ef626 42#include "xmalloc.h"
30460aeb 43#include "key.h"
44#include "hostfile.h"
45#include "ssh.h"
3c0ef626 46#include "rsa.h"
47#include "buffer.h"
48#include "packet.h"
49#include "uidswap.h"
50#include "compat.h"
51#include "key.h"
52#include "sshconnect.h"
53#include "hostfile.h"
54#include "log.h"
55#include "readconf.h"
56#include "atomicio.h"
57#include "misc.h"
473db5ab 58#include "dns.h"
30460aeb 59#include "version.h"
473db5ab 60
3c0ef626 61char *client_version_string = NULL;
62char *server_version_string = NULL;
63
08822d99 64static int matching_host_key_dns = 0;
473db5ab 65
66/* import */
3c0ef626 67extern Options options;
68extern char *__progname;
473db5ab 69extern uid_t original_real_uid;
70extern uid_t original_effective_uid;
71extern pid_t proxy_command_pid;
3c0ef626 72
73#ifndef INET6_ADDRSTRLEN /* for non IPv6 machines */
74#define INET6_ADDRSTRLEN 46
75#endif
76
473db5ab 77static int show_other_keys(const char *, Key *);
78static void warn_changed_key(Key *);
3c0ef626 79
80/*
81 * Connect to the given ssh server using a proxy command.
82 */
83static int
473db5ab 84ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
3c0ef626 85{
473db5ab 86 char *command_string, *tmp;
3c0ef626 87 int pin[2], pout[2];
88 pid_t pid;
89 char strport[NI_MAXSERV];
90
91 /* Convert the port number into a string. */
92 snprintf(strport, sizeof strport, "%hu", port);
93
473db5ab 94 /*
95 * Build the final command string in the buffer by making the
96 * appropriate substitutions to the given proxy command.
97 *
98 * Use "exec" to avoid "sh -c" processes on some platforms
99 * (e.g. Solaris)
100 */
30460aeb 101 xasprintf(&tmp, "exec %s", proxy_command);
473db5ab 102 command_string = percent_expand(tmp, "h", host,
103 "p", strport, (char *)NULL);
104 xfree(tmp);
3c0ef626 105
106 /* Create pipes for communicating with the proxy. */
107 if (pipe(pin) < 0 || pipe(pout) < 0)
108 fatal("Could not create pipes to communicate with the proxy: %.100s",
473db5ab 109 strerror(errno));
3c0ef626 110
111 debug("Executing proxy command: %.500s", command_string);
112
113 /* Fork and execute the proxy command. */
114 if ((pid = fork()) == 0) {
115 char *argv[10];
116
117 /* Child. Permanently give up superuser privileges. */
30460aeb 118 permanently_drop_suid(original_real_uid);
3c0ef626 119
120 /* Redirect stdin and stdout. */
121 close(pin[1]);
122 if (pin[0] != 0) {
123 if (dup2(pin[0], 0) < 0)
124 perror("dup2 stdin");
125 close(pin[0]);
126 }
127 close(pout[0]);
128 if (dup2(pout[1], 1) < 0)
129 perror("dup2 stdout");
130 /* Cannot be 1 because pin allocated two descriptors. */
131 close(pout[1]);
132
133 /* Stderr is left as it is so that error messages get
134 printed on the user's terminal. */
135 argv[0] = _PATH_BSHELL;
136 argv[1] = "-c";
137 argv[2] = command_string;
138 argv[3] = NULL;
139
140 /* Execute the proxy command. Note that we gave up any
141 extra privileges above. */
142 execv(argv[0], argv);
143 perror(argv[0]);
144 exit(1);
145 }
146 /* Parent. */
147 if (pid < 0)
148 fatal("fork failed: %.100s", strerror(errno));
473db5ab 149 else
150 proxy_command_pid = pid; /* save pid to clean up later */
3c0ef626 151
152 /* Close child side of the descriptors. */
153 close(pin[0]);
154 close(pout[1]);
155
156 /* Free the command name. */
473db5ab 157 xfree(command_string);
3c0ef626 158
159 /* Set the connection file descriptors. */
160 packet_set_connection(pout[0], pin[1]);
161
162 /* Indicate OK return */
163 return 0;
164}
165
a7213e65 166/*
167 * Set TCP receive buffer if requested.
168 * Note: tuning needs to happen after the socket is
169 * created but before the connection happens
170 * so winscale is negotiated properly -cjr
171 */
172static void
173ssh_set_socket_recvbuf(int sock)
174{
175 void *buf = (void *)&options.tcp_rcv_buf;
176 int sz = sizeof(options.tcp_rcv_buf);
177 int socksize;
178 int socksizelen = sizeof(int);
179
180 debug("setsockopt Attempting to set SO_RCVBUF to %d", options.tcp_rcv_buf);
181 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, buf, sz) >= 0) {
182 getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &socksize, &socksizelen);
183 debug("setsockopt SO_RCVBUF: %.100s %d", strerror(errno), socksize);
184 }
185 else
186 error("Couldn't set socket receive buffer to %d: %.100s",
187 options.tcp_rcv_buf, strerror(errno));
188}
189
190
3c0ef626 191/*
192 * Creates a (possibly privileged) socket for use as the ssh connection.
193 */
194static int
473db5ab 195ssh_create_socket(int privileged, struct addrinfo *ai)
3c0ef626 196{
197 int sock, gaierr;
198 struct addrinfo hints, *res;
199
200 /*
201 * If we are running as root and want to connect to a privileged
202 * port, bind our own socket to a privileged port.
203 */
204 if (privileged) {
205 int p = IPPORT_RESERVED - 1;
473db5ab 206 PRIV_START;
207 sock = rresvport_af(&p, ai->ai_family);
208 PRIV_END;
3c0ef626 209 if (sock < 0)
473db5ab 210 error("rresvport: af=%d %.100s", ai->ai_family,
211 strerror(errno));
3c0ef626 212 else
213 debug("Allocated local port %d.", p);
473db5ab 214
a7213e65 215 if (options.tcp_rcv_buf > 0)
216 ssh_set_socket_recvbuf(sock);
3c0ef626 217 return sock;
218 }
473db5ab 219 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
3c0ef626 220 if (sock < 0)
221 error("socket: %.100s", strerror(errno));
473db5ab 222
6df46d40 223
224 if (options.tcp_rcv_buf > 0)
225 ssh_set_socket_recvbuf(sock);
4834571d 226
227 /* Bind the socket to an alternative local IP address */
3c0ef626 228 if (options.bind_address == NULL)
229 return sock;
230
231 memset(&hints, 0, sizeof(hints));
473db5ab 232 hints.ai_family = ai->ai_family;
233 hints.ai_socktype = ai->ai_socktype;
234 hints.ai_protocol = ai->ai_protocol;
3c0ef626 235 hints.ai_flags = AI_PASSIVE;
236 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
237 if (gaierr) {
238 error("getaddrinfo: %s: %s", options.bind_address,
239 gai_strerror(gaierr));
240 close(sock);
241 return -1;
242 }
243 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
244 error("bind: %s: %s", options.bind_address, strerror(errno));
245 close(sock);
246 freeaddrinfo(res);
247 return -1;
248 }
249 freeaddrinfo(res);
250 return sock;
251}
252
473db5ab 253static int
254timeout_connect(int sockfd, const struct sockaddr *serv_addr,
255 socklen_t addrlen, int timeout)
256{
257 fd_set *fdset;
258 struct timeval tv;
259 socklen_t optlen;
30460aeb 260 int optval, rc, result = -1;
473db5ab 261
262 if (timeout <= 0)
263 return (connect(sockfd, serv_addr, addrlen));
264
265 set_nonblock(sockfd);
266 rc = connect(sockfd, serv_addr, addrlen);
267 if (rc == 0) {
268 unset_nonblock(sockfd);
269 return (0);
270 }
271 if (errno != EINPROGRESS)
272 return (-1);
273
30460aeb 274 fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
275 sizeof(fd_mask));
473db5ab 276 FD_SET(sockfd, fdset);
277 tv.tv_sec = timeout;
278 tv.tv_usec = 0;
279
280 for (;;) {
281 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
282 if (rc != -1 || errno != EINTR)
283 break;
284 }
285
286 switch (rc) {
287 case 0:
288 /* Timed out */
289 errno = ETIMEDOUT;
290 break;
291 case -1:
292 /* Select error */
293 debug("select: %s", strerror(errno));
294 break;
295 case 1:
296 /* Completed or failed */
297 optval = 0;
298 optlen = sizeof(optval);
299 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
300 &optlen) == -1) {
301 debug("getsockopt: %s", strerror(errno));
302 break;
303 }
304 if (optval != 0) {
305 errno = optval;
306 break;
307 }
308 result = 0;
309 unset_nonblock(sockfd);
310 break;
311 default:
312 /* Should not occur */
313 fatal("Bogus return (%d) from select()", rc);
314 }
315
316 xfree(fdset);
317 return (result);
318}
319
3c0ef626 320/*
321 * Opens a TCP/IP connection to the remote server on the given host.
322 * The address of the remote host will be returned in hostaddr.
473db5ab 323 * If port is 0, the default port will be used. If needpriv is true,
3c0ef626 324 * a privileged port will be allocated to make the connection.
473db5ab 325 * This requires super-user privileges if needpriv is true.
3c0ef626 326 * Connection_attempts specifies the maximum number of tries (one per
327 * second). If proxy_command is non-NULL, it specifies the command (with %h
328 * and %p substituted for host and port, respectively) to use to contact
329 * the daemon.
3c0ef626 330 */
331int
332ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
333 u_short port, int family, int connection_attempts,
473db5ab 334 int needpriv, const char *proxy_command)
3c0ef626 335{
336 int gaierr;
337 int on = 1;
338 int sock = -1, attempt;
339 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
340 struct addrinfo hints, *ai, *aitop;
3c0ef626 341
473db5ab 342 debug2("ssh_connect: needpriv %d", needpriv);
3c0ef626 343
3c0ef626 344 /* If a proxy command is given, connect using it. */
345 if (proxy_command != NULL)
473db5ab 346 return ssh_proxy_connect(host, port, proxy_command);
3c0ef626 347
348 /* No proxy command. */
349
350 memset(&hints, 0, sizeof(hints));
351 hints.ai_family = family;
352 hints.ai_socktype = SOCK_STREAM;
473db5ab 353 snprintf(strport, sizeof strport, "%u", port);
3c0ef626 354 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
355 fatal("%s: %.100s: %s", __progname, host,
356 gai_strerror(gaierr));
357
30460aeb 358 for (attempt = 0; attempt < connection_attempts; attempt++) {
240debe0 359 if (attempt > 0) {
360 /* Sleep a moment before retrying. */
361 sleep(1);
3c0ef626 362 debug("Trying again...");
240debe0 363 }
30460aeb 364 /*
365 * Loop through addresses for this host, and try each one in
366 * sequence until the connection succeeds.
367 */
3c0ef626 368 for (ai = aitop; ai; ai = ai->ai_next) {
369 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
370 continue;
371 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
372 ntop, sizeof(ntop), strport, sizeof(strport),
373 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
374 error("ssh_connect: getnameinfo failed");
375 continue;
376 }
377 debug("Connecting to %.200s [%.100s] port %s.",
378 host, ntop, strport);
379
380 /* Create a socket for connecting. */
473db5ab 381 sock = ssh_create_socket(needpriv, ai);
3c0ef626 382 if (sock < 0)
383 /* Any error is already output */
384 continue;
385
473db5ab 386 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
387 options.connection_timeout) >= 0) {
3c0ef626 388 /* Successful connection. */
389 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
3c0ef626 390 break;
391 } else {
473db5ab 392 debug("connect to address %s port %s: %s",
393 ntop, strport, strerror(errno));
3c0ef626 394 close(sock);
30460aeb 395 sock = -1;
3c0ef626 396 }
397 }
30460aeb 398 if (sock != -1)
3c0ef626 399 break; /* Successful connection. */
3c0ef626 400 }
401
402 freeaddrinfo(aitop);
403
404 /* Return failure if we didn't get a successful connection. */
30460aeb 405 if (sock == -1) {
473db5ab 406 error("ssh: connect to host %s port %s: %s",
407 host, strport, strerror(errno));
408 return (-1);
409 }
3c0ef626 410
411 debug("Connection established.");
412
473db5ab 413 /* Set SO_KEEPALIVE if requested. */
414 if (options.tcp_keep_alive &&
3c0ef626 415 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
416 sizeof(on)) < 0)
417 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
418
419 /* Set the connection. */
420 packet_set_connection(sock, sock);
421
422 return 0;
423}
424
425/*
426 * Waits for the server identification string, and sends our own
427 * identification string.
428 */
429static void
430ssh_exchange_identification(void)
431{
432 char buf[256], remote_version[256]; /* must be same size! */
473db5ab 433 int remote_major, remote_minor, mismatch;
3c0ef626 434 int connection_in = packet_get_connection_in();
435 int connection_out = packet_get_connection_out();
436 int minor1 = PROTOCOL_MINOR_1;
30460aeb 437 u_int i, n;
3c0ef626 438
473db5ab 439 /* Read other side's version identification. */
30460aeb 440 for (n = 0;;) {
3c0ef626 441 for (i = 0; i < sizeof(buf) - 1; i++) {
473db5ab 442 size_t len = atomicio(read, connection_in, &buf[i], 1);
443
444 if (len != 1 && errno == EPIPE)
3c0ef626 445 fatal("ssh_exchange_identification: Connection closed by remote host");
473db5ab 446 else if (len != 1)
447 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
3c0ef626 448 if (buf[i] == '\r') {
449 buf[i] = '\n';
450 buf[i + 1] = 0;
451 continue; /**XXX wait for \n */
452 }
453 if (buf[i] == '\n') {
454 buf[i + 1] = 0;
455 break;
456 }
30460aeb 457 if (++n > 65536)
458 fatal("ssh_exchange_identification: No banner received");
3c0ef626 459 }
460 buf[sizeof(buf) - 1] = 0;
461 if (strncmp(buf, "SSH-", 4) == 0)
462 break;
463 debug("ssh_exchange_identification: %s", buf);
464 }
465 server_version_string = xstrdup(buf);
466
467 /*
468 * Check that the versions match. In future this might accept
469 * several versions and set appropriate flags to handle them.
470 */
471 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
472 &remote_major, &remote_minor, remote_version) != 3)
473 fatal("Bad remote protocol version identification: '%.100s'", buf);
474 debug("Remote protocol version %d.%d, remote software version %.100s",
473db5ab 475 remote_major, remote_minor, remote_version);
3c0ef626 476
477 compat_datafellows(remote_version);
478 mismatch = 0;
479
473db5ab 480 switch (remote_major) {
3c0ef626 481 case 1:
482 if (remote_minor == 99 &&
483 (options.protocol & SSH_PROTO_2) &&
484 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
485 enable_compat20();
486 break;
487 }
488 if (!(options.protocol & SSH_PROTO_1)) {
489 mismatch = 1;
490 break;
491 }
492 if (remote_minor < 3) {
493 fatal("Remote machine has too old SSH software version.");
494 } else if (remote_minor == 3 || remote_minor == 4) {
495 /* We speak 1.3, too. */
496 enable_compat13();
497 minor1 = 3;
498 if (options.forward_agent) {
473db5ab 499 logit("Agent forwarding disabled for protocol 1.3");
3c0ef626 500 options.forward_agent = 0;
501 }
502 }
503 break;
504 case 2:
505 if (options.protocol & SSH_PROTO_2) {
506 enable_compat20();
507 break;
508 }
509 /* FALLTHROUGH */
510 default:
511 mismatch = 1;
512 break;
513 }
514 if (mismatch)
515 fatal("Protocol major versions differ: %d vs. %d",
516 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
517 remote_major);
518 /* Send our own protocol version identification. */
519 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
520 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
521 compat20 ? PROTOCOL_MINOR_2 : minor1,
473db5ab 522 SSH_RELEASE);
523 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
3c0ef626 524 fatal("write: %.100s", strerror(errno));
525 client_version_string = xstrdup(buf);
526 chop(client_version_string);
527 chop(server_version_string);
528 debug("Local version string %.100s", client_version_string);
529}
530
531/* defaults to 'no' */
532static int
533confirm(const char *prompt)
534{
473db5ab 535 const char *msg, *again = "Please type 'yes' or 'no': ";
536 char *p;
537 int ret = -1;
3c0ef626 538
539 if (options.batch_mode)
540 return 0;
473db5ab 541 for (msg = prompt;;msg = again) {
542 p = read_passphrase(msg, RP_ECHO);
543 if (p == NULL ||
544 (p[0] == '\0') || (p[0] == '\n') ||
545 strncasecmp(p, "no", 2) == 0)
546 ret = 0;
547 if (p && strncasecmp(p, "yes", 3) == 0)
548 ret = 1;
549 if (p)
550 xfree(p);
551 if (ret != -1)
552 return ret;
3c0ef626 553 }
554}
555
556/*
557 * check whether the supplied host key is valid, return -1 if the key
558 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
559 */
30460aeb 560#define RDRW 0
561#define RDONLY 1
562#define ROQUIET 2
3c0ef626 563static int
30460aeb 564check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
565 Key *host_key, int readonly, const char *user_hostfile,
566 const char *system_hostfile)
3c0ef626 567{
568 Key *file_key;
473db5ab 569 const char *type = key_type(host_key);
30460aeb 570 char *ip = NULL, *host = NULL;
3c0ef626 571 char hostline[1000], *hostp, *fp;
572 HostStatus host_status;
573 HostStatus ip_status;
473db5ab 574 int r, local = 0, host_ip_differ = 0;
3c0ef626 575 int salen;
576 char ntop[NI_MAXHOST];
473db5ab 577 char msg[1024];
578 int len, host_line, ip_line;
3c0ef626 579 const char *host_file = NULL, *ip_file = NULL;
580
581 /*
582 * Force accepting of the host key for loopback/localhost. The
583 * problem is that if the home directory is NFS-mounted to multiple
584 * machines, localhost will refer to a different machine in each of
585 * them, and the user will get bogus HOST_CHANGED warnings. This
586 * essentially disables host authentication for localhost; however,
587 * this is probably not a real problem.
588 */
589 /** hostaddr == 0! */
590 switch (hostaddr->sa_family) {
591 case AF_INET:
592 local = (ntohl(((struct sockaddr_in *)hostaddr)->
593 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
594 salen = sizeof(struct sockaddr_in);
595 break;
596 case AF_INET6:
597 local = IN6_IS_ADDR_LOOPBACK(
598 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
599 salen = sizeof(struct sockaddr_in6);
600 break;
601 default:
602 local = 0;
603 salen = sizeof(struct sockaddr_storage);
604 break;
605 }
606 if (options.no_host_authentication_for_localhost == 1 && local &&
607 options.host_key_alias == NULL) {
608 debug("Forcing accepting of host key for "
609 "loopback/localhost.");
610 return 0;
611 }
612
613 /*
614 * We don't have the remote ip-address for connections
615 * using a proxy command
616 */
617 if (options.proxy_command == NULL) {
618 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
619 NULL, 0, NI_NUMERICHOST) != 0)
620 fatal("check_host_key: getnameinfo failed");
30460aeb 621 ip = put_host_port(ntop, port);
3c0ef626 622 } else {
623 ip = xstrdup("<no hostip for proxy command>");
624 }
625 /*
626 * Turn off check_host_ip if the connection is to localhost, via proxy
627 * command or if we don't have a hostname to compare with
628 */
30460aeb 629 if (options.check_host_ip && (local ||
630 strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
3c0ef626 631 options.check_host_ip = 0;
632
633 /*
30460aeb 634 * Allow the user to record the key under a different name or
635 * differentiate a non-standard port. This is useful for ssh
636 * tunneling over forwarded connections or if you run multiple
637 * sshd's on different ports on the same machine.
3c0ef626 638 */
639 if (options.host_key_alias != NULL) {
30460aeb 640 host = xstrdup(options.host_key_alias);
3c0ef626 641 debug("using hostkeyalias: %s", host);
30460aeb 642 } else {
643 host = put_host_port(hostname, port);
3c0ef626 644 }
645
646 /*
647 * Store the host key from the known host file in here so that we can
648 * compare it with the key for the IP address.
649 */
650 file_key = key_new(host_key->type);
651
652 /*
08822d99 653 * Check if the host key is present in the user's list of known
3c0ef626 654 * hosts or in the systemwide list.
655 */
656 host_file = user_hostfile;
657 host_status = check_host_in_hostfile(host_file, host, host_key,
473db5ab 658 file_key, &host_line);
3c0ef626 659 if (host_status == HOST_NEW) {
660 host_file = system_hostfile;
661 host_status = check_host_in_hostfile(host_file, host, host_key,
662 file_key, &host_line);
663 }
664 /*
665 * Also perform check for the ip address, skip the check if we are
666 * localhost or the hostname was an ip address to begin with
667 */
668 if (options.check_host_ip) {
669 Key *ip_key = key_new(host_key->type);
670
671 ip_file = user_hostfile;
672 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
673 ip_key, &ip_line);
674 if (ip_status == HOST_NEW) {
675 ip_file = system_hostfile;
676 ip_status = check_host_in_hostfile(ip_file, ip,
677 host_key, ip_key, &ip_line);
678 }
679 if (host_status == HOST_CHANGED &&
680 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
681 host_ip_differ = 1;
682
683 key_free(ip_key);
684 } else
685 ip_status = host_status;
686
687 key_free(file_key);
688
689 switch (host_status) {
690 case HOST_OK:
691 /* The host is known and the key matches. */
692 debug("Host '%.200s' is known and matches the %s host key.",
693 host, type);
694 debug("Found key in %s:%d", host_file, host_line);
695 if (options.check_host_ip && ip_status == HOST_NEW) {
696 if (readonly)
473db5ab 697 logit("%s host key for IP address "
3c0ef626 698 "'%.128s' not in list of known hosts.",
699 type, ip);
700 else if (!add_host_to_hostfile(user_hostfile, ip,
473db5ab 701 host_key, options.hash_known_hosts))
702 logit("Failed to add the %s host key for IP "
3c0ef626 703 "address '%.128s' to the list of known "
704 "hosts (%.30s).", type, ip, user_hostfile);
705 else
473db5ab 706 logit("Warning: Permanently added the %s host "
3c0ef626 707 "key for IP address '%.128s' to the list "
708 "of known hosts.", type, ip);
709 }
710 break;
711 case HOST_NEW:
30460aeb 712 if (options.host_key_alias == NULL && port != 0 &&
713 port != SSH_DEFAULT_PORT) {
714 debug("checking without port identifier");
715 if (check_host_key(hostname, hostaddr, 0, host_key, 2,
716 user_hostfile, system_hostfile) == 0) {
717 debug("found matching key w/out port");
718 break;
719 }
720 }
3c0ef626 721 if (readonly)
722 goto fail;
723 /* The host is new. */
724 if (options.strict_host_key_checking == 1) {
725 /*
726 * User has requested strict host key checking. We
727 * will not add the host key automatically. The only
728 * alternative left is to abort.
729 */
730 error("No %s host key is known for %.200s and you "
731 "have requested strict checking.", type, host);
732 goto fail;
733 } else if (options.strict_host_key_checking == 2) {
473db5ab 734 char msg1[1024], msg2[1024];
735
736 if (show_other_keys(host, host_key))
737 snprintf(msg1, sizeof(msg1),
738 "\nbut keys of different type are already"
739 " known for this host.");
740 else
741 snprintf(msg1, sizeof(msg1), ".");
3c0ef626 742 /* The default */
3c0ef626 743 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
473db5ab 744 msg2[0] = '\0';
745 if (options.verify_host_key_dns) {
746 if (matching_host_key_dns)
747 snprintf(msg2, sizeof(msg2),
748 "Matching host key fingerprint"
749 " found in DNS.\n");
750 else
751 snprintf(msg2, sizeof(msg2),
752 "No matching host key fingerprint"
753 " found in DNS.\n");
754 }
755 snprintf(msg, sizeof(msg),
3c0ef626 756 "The authenticity of host '%.200s (%s)' can't be "
473db5ab 757 "established%s\n"
758 "%s key fingerprint is %s.\n%s"
3c0ef626 759 "Are you sure you want to continue connecting "
473db5ab 760 "(yes/no)? ",
761 host, ip, msg1, type, fp, msg2);
3c0ef626 762 xfree(fp);
473db5ab 763 if (!confirm(msg))
3c0ef626 764 goto fail;
3c0ef626 765 }
3c0ef626 766 /*
767 * If not in strict mode, add the key automatically to the
768 * local known_hosts file.
769 */
473db5ab 770 if (options.check_host_ip && ip_status == HOST_NEW) {
771 snprintf(hostline, sizeof(hostline), "%s,%s",
772 host, ip);
773 hostp = hostline;
774 if (options.hash_known_hosts) {
775 /* Add hash of host and IP separately */
776 r = add_host_to_hostfile(user_hostfile, host,
777 host_key, options.hash_known_hosts) &&
778 add_host_to_hostfile(user_hostfile, ip,
779 host_key, options.hash_known_hosts);
780 } else {
781 /* Add unhashed "host,ip" */
782 r = add_host_to_hostfile(user_hostfile,
783 hostline, host_key,
784 options.hash_known_hosts);
785 }
786 } else {
787 r = add_host_to_hostfile(user_hostfile, host, host_key,
788 options.hash_known_hosts);
789 hostp = host;
790 }
791
792 if (!r)
793 logit("Failed to add the host to the list of known "
3c0ef626 794 "hosts (%.500s).", user_hostfile);
795 else
473db5ab 796 logit("Warning: Permanently added '%.200s' (%s) to the "
3c0ef626 797 "list of known hosts.", hostp, type);
798 break;
799 case HOST_CHANGED:
30460aeb 800 if (readonly == ROQUIET)
801 goto fail;
3c0ef626 802 if (options.check_host_ip && host_ip_differ) {
473db5ab 803 char *key_msg;
3c0ef626 804 if (ip_status == HOST_NEW)
473db5ab 805 key_msg = "is unknown";
3c0ef626 806 else if (ip_status == HOST_OK)
473db5ab 807 key_msg = "is unchanged";
3c0ef626 808 else
473db5ab 809 key_msg = "has a different value";
3c0ef626 810 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
811 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
812 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
813 error("The %s host key for %s has changed,", type, host);
814 error("and the key for the according IP address %s", ip);
473db5ab 815 error("%s. This could either mean that", key_msg);
3c0ef626 816 error("DNS SPOOFING is happening or the IP address for the host");
817 error("and its host key have changed at the same time.");
818 if (ip_status != HOST_NEW)
819 error("Offending key for IP in %s:%d", ip_file, ip_line);
820 }
821 /* The host key has changed. */
473db5ab 822 warn_changed_key(host_key);
3c0ef626 823 error("Add correct host key in %.100s to get rid of this message.",
824 user_hostfile);
825 error("Offending key in %s:%d", host_file, host_line);
3c0ef626 826
827 /*
828 * If strict host key checking is in use, the user will have
829 * to edit the key manually and we can only abort.
830 */
831 if (options.strict_host_key_checking) {
832 error("%s host key for %.200s has changed and you have "
833 "requested strict checking.", type, host);
834 goto fail;
835 }
836
837 /*
838 * If strict host key checking has not been requested, allow
473db5ab 839 * the connection but without MITM-able authentication or
30460aeb 840 * forwarding.
3c0ef626 841 */
842 if (options.password_authentication) {
843 error("Password authentication is disabled to avoid "
844 "man-in-the-middle attacks.");
845 options.password_authentication = 0;
846 }
473db5ab 847 if (options.kbd_interactive_authentication) {
848 error("Keyboard-interactive authentication is disabled"
849 " to avoid man-in-the-middle attacks.");
850 options.kbd_interactive_authentication = 0;
851 options.challenge_response_authentication = 0;
852 }
853 if (options.challenge_response_authentication) {
854 error("Challenge/response authentication is disabled"
855 " to avoid man-in-the-middle attacks.");
856 options.challenge_response_authentication = 0;
857 }
3c0ef626 858 if (options.forward_agent) {
859 error("Agent forwarding is disabled to avoid "
860 "man-in-the-middle attacks.");
861 options.forward_agent = 0;
862 }
863 if (options.forward_x11) {
864 error("X11 forwarding is disabled to avoid "
865 "man-in-the-middle attacks.");
866 options.forward_x11 = 0;
867 }
868 if (options.num_local_forwards > 0 ||
869 options.num_remote_forwards > 0) {
870 error("Port forwarding is disabled to avoid "
871 "man-in-the-middle attacks.");
872 options.num_local_forwards =
473db5ab 873 options.num_remote_forwards = 0;
3c0ef626 874 }
30460aeb 875 if (options.tun_open != SSH_TUNMODE_NO) {
876 error("Tunnel forwarding is disabled to avoid "
877 "man-in-the-middle attacks.");
878 options.tun_open = SSH_TUNMODE_NO;
879 }
3c0ef626 880 /*
881 * XXX Should permit the user to change to use the new id.
882 * This could be done by converting the host key to an
883 * identifying sentence, tell that the host identifies itself
884 * by that sentence, and ask the user if he/she whishes to
885 * accept the authentication.
886 */
887 break;
473db5ab 888 case HOST_FOUND:
889 fatal("internal error");
890 break;
3c0ef626 891 }
892
893 if (options.check_host_ip && host_status != HOST_CHANGED &&
894 ip_status == HOST_CHANGED) {
473db5ab 895 snprintf(msg, sizeof(msg),
896 "Warning: the %s host key for '%.200s' "
897 "differs from the key for the IP address '%.128s'"
898 "\nOffending key for IP in %s:%d",
899 type, host, ip, ip_file, ip_line);
900 if (host_status == HOST_OK) {
901 len = strlen(msg);
902 snprintf(msg + len, sizeof(msg) - len,
903 "\nMatching host key in %s:%d",
904 host_file, host_line);
905 }
3c0ef626 906 if (options.strict_host_key_checking == 1) {
473db5ab 907 logit("%s", msg);
3c0ef626 908 error("Exiting, you have requested strict checking.");
909 goto fail;
910 } else if (options.strict_host_key_checking == 2) {
473db5ab 911 strlcat(msg, "\nAre you sure you want "
912 "to continue connecting (yes/no)? ", sizeof(msg));
913 if (!confirm(msg))
3c0ef626 914 goto fail;
473db5ab 915 } else {
916 logit("%s", msg);
3c0ef626 917 }
918 }
919
920 xfree(ip);
30460aeb 921 xfree(host);
3c0ef626 922 return 0;
923
924fail:
925 xfree(ip);
30460aeb 926 xfree(host);
3c0ef626 927 return -1;
928}
929
473db5ab 930/* returns 0 if key verifies or -1 if key does NOT verify */
3c0ef626 931int
932verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
933{
934 struct stat st;
473db5ab 935 int flags = 0;
936
937 if (options.verify_host_key_dns &&
938 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
939
940 if (flags & DNS_VERIFY_FOUND) {
941
942 if (options.verify_host_key_dns == 1 &&
943 flags & DNS_VERIFY_MATCH &&
944 flags & DNS_VERIFY_SECURE)
945 return 0;
946
947 if (flags & DNS_VERIFY_MATCH) {
948 matching_host_key_dns = 1;
949 } else {
950 warn_changed_key(host_key);
951 error("Update the SSHFP RR in DNS with the new "
952 "host key to get rid of this message.");
953 }
954 }
955 }
3c0ef626 956
957 /* return ok if the key can be found in an old keyfile */
958 if (stat(options.system_hostfile2, &st) == 0 ||
959 stat(options.user_hostfile2, &st) == 0) {
30460aeb 960 if (check_host_key(host, hostaddr, options.port, host_key,
961 RDONLY, options.user_hostfile2,
962 options.system_hostfile2) == 0)
3c0ef626 963 return 0;
964 }
30460aeb 965 return check_host_key(host, hostaddr, options.port, host_key,
966 RDRW, options.user_hostfile, options.system_hostfile);
3c0ef626 967}
968
969/*
970 * Starts a dialog with the server, and authenticates the current user on the
971 * server. This does not need any extra privileges. The basic connection
972 * to the server must already have been established before this is called.
973 * If login fails, this function prints an error and never returns.
974 * This function does not require super-user privileges.
975 */
976void
473db5ab 977ssh_login(Sensitive *sensitive, const char *orighost,
3c0ef626 978 struct sockaddr *hostaddr, struct passwd *pw)
979{
980 char *host, *cp;
981 char *server_user, *local_user;
982
983 local_user = xstrdup(pw->pw_name);
984 server_user = options.user ? options.user : local_user;
985
986 /* Convert the user-supplied hostname into all lowercase. */
987 host = xstrdup(orighost);
988 for (cp = host; *cp; cp++)
989 if (isupper(*cp))
30460aeb 990 *cp = (char)tolower(*cp);
3c0ef626 991
992 /* Exchange protocol version identification strings with the server. */
993 ssh_exchange_identification();
994
995 /* Put the connection into non-blocking mode. */
996 packet_set_nonblocking();
997
998 /* key exchange */
999 /* authenticate user */
1000 if (compat20) {
1001 ssh_kex2(host, hostaddr);
473db5ab 1002 ssh_userauth2(local_user, server_user, host, sensitive);
3c0ef626 1003 } else {
1004 ssh_kex(host, hostaddr);
473db5ab 1005 ssh_userauth1(local_user, server_user, host, sensitive);
3c0ef626 1006 }
30460aeb 1007 xfree(local_user);
3c0ef626 1008}
1009
1010void
1011ssh_put_password(char *password)
1012{
1013 int size;
1014 char *padded;
1015
1016 if (datafellows & SSH_BUG_PASSWORDPAD) {
1017 packet_put_cstring(password);
1018 return;
1019 }
1020 size = roundup(strlen(password) + 1, 32);
30460aeb 1021 padded = xcalloc(1, size);
3c0ef626 1022 strlcpy(padded, password, size);
1023 packet_put_string(padded, size);
1024 memset(padded, 0, size);
1025 xfree(padded);
1026}
473db5ab 1027
1028static int
1029show_key_from_file(const char *file, const char *host, int keytype)
1030{
1031 Key *found;
1032 char *fp;
1033 int line, ret;
1034
1035 found = key_new(keytype);
1036 if ((ret = lookup_key_in_hostfile_by_type(file, host,
1037 keytype, found, &line))) {
1038 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1039 logit("WARNING: %s key found for host %s\n"
1040 "in %s:%d\n"
1041 "%s key fingerprint %s.",
1042 key_type(found), host, file, line,
1043 key_type(found), fp);
1044 xfree(fp);
1045 }
1046 key_free(found);
1047 return (ret);
1048}
1049
1050/* print all known host keys for a given host, but skip keys of given type */
1051static int
1052show_other_keys(const char *host, Key *key)
1053{
1054 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
1055 int i, found = 0;
1056
1057 for (i = 0; type[i] != -1; i++) {
1058 if (type[i] == key->type)
1059 continue;
1060 if (type[i] != KEY_RSA1 &&
1061 show_key_from_file(options.user_hostfile2, host, type[i])) {
1062 found = 1;
1063 continue;
1064 }
1065 if (type[i] != KEY_RSA1 &&
1066 show_key_from_file(options.system_hostfile2, host, type[i])) {
1067 found = 1;
1068 continue;
1069 }
1070 if (show_key_from_file(options.user_hostfile, host, type[i])) {
1071 found = 1;
1072 continue;
1073 }
1074 if (show_key_from_file(options.system_hostfile, host, type[i])) {
1075 found = 1;
1076 continue;
1077 }
1078 debug2("no key of type %d for host %s", type[i], host);
1079 }
1080 return (found);
1081}
1082
1083static void
1084warn_changed_key(Key *host_key)
1085{
1086 char *fp;
1087 const char *type = key_type(host_key);
1088
1089 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1090
1091 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1092 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1093 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1094 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1095 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1096 error("It is also possible that the %s host key has just been changed.", type);
1097 error("The fingerprint for the %s key sent by the remote host is\n%s.",
1098 type, fp);
1099 error("Please contact your system administrator.");
1100
1101 xfree(fp);
1102}
08822d99 1103
1104/*
1105 * Execute a local command
1106 */
1107int
1108ssh_local_cmd(const char *args)
1109{
1110 char *shell;
1111 pid_t pid;
1112 int status;
1113
1114 if (!options.permit_local_command ||
1115 args == NULL || !*args)
1116 return (1);
1117
1118 if ((shell = getenv("SHELL")) == NULL)
1119 shell = _PATH_BSHELL;
1120
1121 pid = fork();
1122 if (pid == 0) {
1123 debug3("Executing %s -c \"%s\"", shell, args);
1124 execl(shell, shell, "-c", args, (char *)NULL);
1125 error("Couldn't execute %s -c \"%s\": %s",
1126 shell, args, strerror(errno));
1127 _exit(1);
1128 } else if (pid == -1)
1129 fatal("fork failed: %.100s", strerror(errno));
1130 while (waitpid(pid, &status, 0) == -1)
1131 if (errno != EINTR)
1132 fatal("Couldn't wait for child: %s", strerror(errno));
1133
1134 if (!WIFEXITED(status))
1135 return (1);
1136
1137 return (WEXITSTATUS(status));
1138}
This page took 3.506873 seconds and 5 git commands to generate.