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