]> andersk Git - gssapi-openssh.git/blame - openssh/sshconnect.c
openssh-4.4p1-hpn12v13.diff
[gssapi-openssh.git] / openssh / sshconnect.c
CommitLineData
9108f8d9 1/* $OpenBSD: sshconnect.c,v 1.199 2006/08/03 03:34:42 deraadt 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
9108f8d9 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"
9108f8d9 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"
0fff78ff 58#include "dns.h"
9108f8d9 59#include "version.h"
0fff78ff 60
3c0ef626 61char *client_version_string = NULL;
62char *server_version_string = NULL;
63
2c06c99b 64static int matching_host_key_dns = 0;
0fff78ff 65
f5799ae1 66/* import */
3c0ef626 67extern Options options;
68extern char *__progname;
f5799ae1 69extern uid_t original_real_uid;
70extern uid_t original_effective_uid;
41b2f314 71extern pid_t proxy_command_pid;
3c0ef626 72
73#ifndef INET6_ADDRSTRLEN /* for non IPv6 machines */
74#define INET6_ADDRSTRLEN 46
75#endif
76
41b2f314 77static int show_other_keys(const char *, Key *);
cdd66111 78static void warn_changed_key(Key *);
3c0ef626 79
80/*
81 * Connect to the given ssh server using a proxy command.
82 */
83static int
f5799ae1 84ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
3c0ef626 85{
665a873d 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
41b2f314 94 /*
95 * Build the final command string in the buffer by making the
96 * appropriate substitutions to the given proxy command.
97 *
cdd66111 98 * Use "exec" to avoid "sh -c" processes on some platforms
41b2f314 99 * (e.g. Solaris)
100 */
9108f8d9 101 xasprintf(&tmp, "exec %s", proxy_command);
665a873d 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",
e9a17296 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. */
9108f8d9 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));
41b2f314 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. */
665a873d 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
bed7ee6f 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
0fff78ff 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;
f5799ae1 206 PRIV_START;
0fff78ff 207 sock = rresvport_af(&p, ai->ai_family);
f5799ae1 208 PRIV_END;
3c0ef626 209 if (sock < 0)
0fff78ff 210 error("rresvport: af=%d %.100s", ai->ai_family,
211 strerror(errno));
3c0ef626 212 else
213 debug("Allocated local port %d.", p);
bed7ee6f 214
215 if (options.tcp_rcv_buf > 0)
216 ssh_set_socket_recvbuf(sock);
3c0ef626 217 return sock;
218 }
0fff78ff 219 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
3c0ef626 220 if (sock < 0)
221 error("socket: %.100s", strerror(errno));
bed7ee6f 222
223 if (options.tcp_rcv_buf > 0)
224 ssh_set_socket_recvbuf(sock);
225
226 /* Bind the socket to an alternative local IP address */
3c0ef626 227 if (options.bind_address == NULL)
228 return sock;
229
230 memset(&hints, 0, sizeof(hints));
0fff78ff 231 hints.ai_family = ai->ai_family;
232 hints.ai_socktype = ai->ai_socktype;
233 hints.ai_protocol = ai->ai_protocol;
3c0ef626 234 hints.ai_flags = AI_PASSIVE;
235 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
236 if (gaierr) {
237 error("getaddrinfo: %s: %s", options.bind_address,
238 gai_strerror(gaierr));
239 close(sock);
240 return -1;
241 }
242 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
243 error("bind: %s: %s", options.bind_address, strerror(errno));
244 close(sock);
245 freeaddrinfo(res);
246 return -1;
247 }
248 freeaddrinfo(res);
249 return sock;
250}
251
0fff78ff 252static int
253timeout_connect(int sockfd, const struct sockaddr *serv_addr,
254 socklen_t addrlen, int timeout)
255{
256 fd_set *fdset;
257 struct timeval tv;
258 socklen_t optlen;
9108f8d9 259 int optval, rc, result = -1;
0fff78ff 260
261 if (timeout <= 0)
262 return (connect(sockfd, serv_addr, addrlen));
263
cdd66111 264 set_nonblock(sockfd);
0fff78ff 265 rc = connect(sockfd, serv_addr, addrlen);
cdd66111 266 if (rc == 0) {
267 unset_nonblock(sockfd);
0fff78ff 268 return (0);
cdd66111 269 }
0fff78ff 270 if (errno != EINPROGRESS)
271 return (-1);
272
9108f8d9 273 fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
274 sizeof(fd_mask));
0fff78ff 275 FD_SET(sockfd, fdset);
276 tv.tv_sec = timeout;
277 tv.tv_usec = 0;
278
dec6d9fe 279 for (;;) {
0fff78ff 280 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
281 if (rc != -1 || errno != EINTR)
282 break;
283 }
284
dec6d9fe 285 switch (rc) {
0fff78ff 286 case 0:
287 /* Timed out */
288 errno = ETIMEDOUT;
289 break;
290 case -1:
291 /* Select error */
cdd66111 292 debug("select: %s", strerror(errno));
0fff78ff 293 break;
294 case 1:
295 /* Completed or failed */
296 optval = 0;
297 optlen = sizeof(optval);
cdd66111 298 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
acc3d05e 299 &optlen) == -1) {
cdd66111 300 debug("getsockopt: %s", strerror(errno));
0fff78ff 301 break;
acc3d05e 302 }
0fff78ff 303 if (optval != 0) {
304 errno = optval;
305 break;
306 }
307 result = 0;
cdd66111 308 unset_nonblock(sockfd);
0fff78ff 309 break;
310 default:
311 /* Should not occur */
312 fatal("Bogus return (%d) from select()", rc);
313 }
314
315 xfree(fdset);
316 return (result);
317}
318
3c0ef626 319/*
320 * Opens a TCP/IP connection to the remote server on the given host.
321 * The address of the remote host will be returned in hostaddr.
f5799ae1 322 * If port is 0, the default port will be used. If needpriv is true,
3c0ef626 323 * a privileged port will be allocated to make the connection.
f5799ae1 324 * This requires super-user privileges if needpriv is true.
3c0ef626 325 * Connection_attempts specifies the maximum number of tries (one per
326 * second). If proxy_command is non-NULL, it specifies the command (with %h
327 * and %p substituted for host and port, respectively) to use to contact
328 * the daemon.
3c0ef626 329 */
330int
331ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
332 u_short port, int family, int connection_attempts,
f5799ae1 333 int needpriv, const char *proxy_command)
3c0ef626 334{
335 int gaierr;
336 int on = 1;
337 int sock = -1, attempt;
338 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
339 struct addrinfo hints, *ai, *aitop;
3c0ef626 340
6a9b3198 341 debug2("ssh_connect: needpriv %d", needpriv);
3c0ef626 342
3c0ef626 343 /* If a proxy command is given, connect using it. */
344 if (proxy_command != NULL)
f5799ae1 345 return ssh_proxy_connect(host, port, proxy_command);
3c0ef626 346
347 /* No proxy command. */
348
349 memset(&hints, 0, sizeof(hints));
350 hints.ai_family = family;
351 hints.ai_socktype = SOCK_STREAM;
680cee3b 352 snprintf(strport, sizeof strport, "%u", port);
3c0ef626 353 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
354 fatal("%s: %.100s: %s", __progname, host,
355 gai_strerror(gaierr));
356
9108f8d9 357 for (attempt = 0; attempt < connection_attempts; attempt++) {
3c0ef626 358 if (attempt > 0)
359 debug("Trying again...");
360
9108f8d9 361 /*
362 * Loop through addresses for this host, and try each one in
363 * sequence until the connection succeeds.
364 */
3c0ef626 365 for (ai = aitop; ai; ai = ai->ai_next) {
366 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
367 continue;
368 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
369 ntop, sizeof(ntop), strport, sizeof(strport),
370 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
371 error("ssh_connect: getnameinfo failed");
372 continue;
373 }
374 debug("Connecting to %.200s [%.100s] port %s.",
375 host, ntop, strport);
376
377 /* Create a socket for connecting. */
0fff78ff 378 sock = ssh_create_socket(needpriv, ai);
3c0ef626 379 if (sock < 0)
380 /* Any error is already output */
381 continue;
382
0fff78ff 383 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
384 options.connection_timeout) >= 0) {
3c0ef626 385 /* Successful connection. */
386 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
3c0ef626 387 break;
388 } else {
41b2f314 389 debug("connect to address %s port %s: %s",
390 ntop, strport, strerror(errno));
3c0ef626 391 close(sock);
9108f8d9 392 sock = -1;
3c0ef626 393 }
394 }
9108f8d9 395 if (sock != -1)
3c0ef626 396 break; /* Successful connection. */
397
3c0ef626 398 /* Sleep a moment before retrying. */
399 sleep(1);
400 }
401
402 freeaddrinfo(aitop);
403
404 /* Return failure if we didn't get a successful connection. */
9108f8d9 405 if (sock == -1) {
996d5e62 406 error("ssh: connect to host %s port %s: %s",
41b2f314 407 host, strport, strerror(errno));
996d5e62 408 return (-1);
41b2f314 409 }
3c0ef626 410
411 debug("Connection established.");
412
cdd66111 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! */
665a873d 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;
9108f8d9 437 u_int i, n;
3c0ef626 438
665a873d 439 /* Read other side's version identification. */
9108f8d9 440 for (n = 0;;) {
3c0ef626 441 for (i = 0; i < sizeof(buf) - 1; i++) {
665a873d 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");
665a873d 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 }
9108f8d9 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",
e9a17296 475 remote_major, remote_minor, remote_version);
3c0ef626 476
477 compat_datafellows(remote_version);
478 mismatch = 0;
479
e9a17296 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) {
0fff78ff 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,
bed7ee6f 522 SSH_RELEASE);
0fff78ff 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{
e9a17296 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;
e9a17296 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;
41b2f314 547 if (p && strncasecmp(p, "yes", 3) == 0)
e9a17296 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 */
9108f8d9 560#define RDRW 0
561#define RDONLY 1
562#define ROQUIET 2
3c0ef626 563static int
9108f8d9 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;
cdd66111 569 const char *type = key_type(host_key);
9108f8d9 570 char *ip = NULL, *host = NULL;
3c0ef626 571 char hostline[1000], *hostp, *fp;
572 HostStatus host_status;
573 HostStatus ip_status;
996d5e62 574 int r, local = 0, host_ip_differ = 0;
3c0ef626 575 int salen;
576 char ntop[NI_MAXHOST];
e9a17296 577 char msg[1024];
0fff78ff 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)->
665a873d 593 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
3c0ef626 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");
9108f8d9 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 */
9108f8d9 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 /*
9108f8d9 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) {
9108f8d9 640 host = xstrdup(options.host_key_alias);
3c0ef626 641 debug("using hostkeyalias: %s", host);
9108f8d9 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 /*
2c06c99b 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,
e9a17296 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)
0fff78ff 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,
996d5e62 701 host_key, options.hash_known_hosts))
0fff78ff 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
0fff78ff 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:
9108f8d9 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) {
0fff78ff 734 char msg1[1024], msg2[1024];
735
736 if (show_other_keys(host, host_key))
737 snprintf(msg1, sizeof(msg1),
665a873d 738 "\nbut keys of different type are already"
739 " known for this host.");
0fff78ff 740 else
741 snprintf(msg1, sizeof(msg1), ".");
3c0ef626 742 /* The default */
3c0ef626 743 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
0fff78ff 744 msg2[0] = '\0';
0fff78ff 745 if (options.verify_host_key_dns) {
cdd66111 746 if (matching_host_key_dns)
0fff78ff 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 }
e9a17296 755 snprintf(msg, sizeof(msg),
3c0ef626 756 "The authenticity of host '%.200s (%s)' can't be "
41b2f314 757 "established%s\n"
0fff78ff 758 "%s key fingerprint is %s.\n%s"
3c0ef626 759 "Are you sure you want to continue connecting "
41b2f314 760 "(yes/no)? ",
0fff78ff 761 host, ip, msg1, type, fp, msg2);
3c0ef626 762 xfree(fp);
e9a17296 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 */
996d5e62 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)
0fff78ff 793 logit("Failed to add the host to the list of known "
3c0ef626 794 "hosts (%.500s).", user_hostfile);
795 else
0fff78ff 796 logit("Warning: Permanently added '%.200s' (%s) to the "
3c0ef626 797 "list of known hosts.", hostp, type);
798 break;
799 case HOST_CHANGED:
9108f8d9 800 if (readonly == ROQUIET)
801 goto fail;
3c0ef626 802 if (options.check_host_ip && host_ip_differ) {
c9f39d2c 803 char *key_msg;
3c0ef626 804 if (ip_status == HOST_NEW)
c9f39d2c 805 key_msg = "is unknown";
3c0ef626 806 else if (ip_status == HOST_OK)
c9f39d2c 807 key_msg = "is unchanged";
3c0ef626 808 else
c9f39d2c 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);
c9f39d2c 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. */
cdd66111 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
0fff78ff 839 * the connection but without MITM-able authentication or
9108f8d9 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 }
0fff78ff 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 =
e9a17296 873 options.num_remote_forwards = 0;
3c0ef626 874 }
9108f8d9 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;
41b2f314 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) {
e9a17296 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",
f5799ae1 904 host_file, host_line);
e9a17296 905 }
3c0ef626 906 if (options.strict_host_key_checking == 1) {
0fff78ff 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) {
e9a17296 911 strlcat(msg, "\nAre you sure you want "
912 "to continue connecting (yes/no)? ", sizeof(msg));
913 if (!confirm(msg))
3c0ef626 914 goto fail;
e9a17296 915 } else {
0fff78ff 916 logit("%s", msg);
3c0ef626 917 }
918 }
919
920 xfree(ip);
9108f8d9 921 xfree(host);
3c0ef626 922 return 0;
923
924fail:
925 xfree(ip);
9108f8d9 926 xfree(host);
3c0ef626 927 return -1;
928}
929
0fff78ff 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;
cdd66111 935 int flags = 0;
12408a1b 936
cdd66111 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 }
0fff78ff 954 }
955 }
0fff78ff 956
3c0ef626 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) {
9108f8d9 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 }
9108f8d9 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
f5799ae1 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))
9108f8d9 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);
f5799ae1 1002 ssh_userauth2(local_user, server_user, host, sensitive);
3c0ef626 1003 } else {
1004 ssh_kex(host, hostaddr);
f5799ae1 1005 ssh_userauth1(local_user, server_user, host, sensitive);
3c0ef626 1006 }
9108f8d9 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);
9108f8d9 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}
41b2f314 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);
0fff78ff 1039 logit("WARNING: %s key found for host %s\n"
41b2f314 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}
cdd66111 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}
2c06c99b 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 0.243311 seconds and 5 git commands to generate.