]> andersk Git - openssh.git/blame - sshconnect.c
- (djm) OpenBSD CVS Sync
[openssh.git] / sshconnect.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
bcbf86ec 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".
5260325f 13 */
8efc0c15 14
15#include "includes.h"
3e131a6d 16RCSID("$OpenBSD: sshconnect.c,v 1.139 2003/04/14 14:17:50 markus Exp $");
8efc0c15 17
8ce64345 18#include <openssl/bn.h>
a306f2dd 19
42f11eb2 20#include "ssh.h"
8efc0c15 21#include "xmalloc.h"
22#include "rsa.h"
8ce64345 23#include "buffer.h"
8efc0c15 24#include "packet.h"
8efc0c15 25#include "uidswap.h"
26#include "compat.h"
4fe2af09 27#include "key.h"
a306f2dd 28#include "sshconnect.h"
4fe2af09 29#include "hostfile.h"
42f11eb2 30#include "log.h"
31#include "readconf.h"
32#include "atomicio.h"
33#include "misc.h"
616a6b93 34#include "readpass.h"
8efc0c15 35
a306f2dd 36char *client_version_string = NULL;
37char *server_version_string = NULL;
8ce64345 38
3fb156df 39/* import */
57112b5a 40extern Options options;
48e671d5 41extern char *__progname;
3fb156df 42extern uid_t original_real_uid;
43extern uid_t original_effective_uid;
6939bbd4 44extern pid_t proxy_command_pid;
57112b5a 45
a704dd54 46#ifndef INET6_ADDRSTRLEN /* for non IPv6 machines */
47#define INET6_ADDRSTRLEN 46
48#endif
49
8a48a7ef 50static int show_other_keys(const char *, Key *);
51
5260325f 52/*
53 * Connect to the given ssh server using a proxy command.
54 */
396c147e 55static int
3fb156df 56ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
8efc0c15 57{
5260325f 58 Buffer command;
59 const char *cp;
60 char *command_string;
61 int pin[2], pout[2];
9da5c3c9 62 pid_t pid;
48e671d5 63 char strport[NI_MAXSERV];
5260325f 64
65 /* Convert the port number into a string. */
48e671d5 66 snprintf(strport, sizeof strport, "%hu", port);
5260325f 67
6939bbd4 68 /*
69 * Build the final command string in the buffer by making the
70 * appropriate substitutions to the given proxy command.
71 *
72 * Use "exec" to avoid "sh -c" processes on some platforms
73 * (e.g. Solaris)
74 */
5260325f 75 buffer_init(&command);
6939bbd4 76 buffer_append(&command, "exec ", 5);
77
5260325f 78 for (cp = proxy_command; *cp; cp++) {
79 if (cp[0] == '%' && cp[1] == '%') {
80 buffer_append(&command, "%", 1);
81 cp++;
82 continue;
83 }
84 if (cp[0] == '%' && cp[1] == 'h') {
85 buffer_append(&command, host, strlen(host));
86 cp++;
87 continue;
88 }
89 if (cp[0] == '%' && cp[1] == 'p') {
48e671d5 90 buffer_append(&command, strport, strlen(strport));
5260325f 91 cp++;
92 continue;
93 }
94 buffer_append(&command, cp, 1);
8efc0c15 95 }
5260325f 96 buffer_append(&command, "\0", 1);
97
98 /* Get the final command string. */
99 command_string = buffer_ptr(&command);
100
101 /* Create pipes for communicating with the proxy. */
102 if (pipe(pin) < 0 || pipe(pout) < 0)
103 fatal("Could not create pipes to communicate with the proxy: %.100s",
184eed6a 104 strerror(errno));
5260325f 105
106 debug("Executing proxy command: %.500s", command_string);
107
108 /* Fork and execute the proxy command. */
109 if ((pid = fork()) == 0) {
110 char *argv[10];
111
112 /* Child. Permanently give up superuser privileges. */
3fb156df 113 seteuid(original_real_uid);
114 setuid(original_real_uid);
5260325f 115
116 /* Redirect stdin and stdout. */
117 close(pin[1]);
118 if (pin[0] != 0) {
119 if (dup2(pin[0], 0) < 0)
120 perror("dup2 stdin");
121 close(pin[0]);
122 }
123 close(pout[0]);
124 if (dup2(pout[1], 1) < 0)
125 perror("dup2 stdout");
126 /* Cannot be 1 because pin allocated two descriptors. */
127 close(pout[1]);
128
129 /* Stderr is left as it is so that error messages get
130 printed on the user's terminal. */
30228d7c 131 argv[0] = _PATH_BSHELL;
5260325f 132 argv[1] = "-c";
133 argv[2] = command_string;
134 argv[3] = NULL;
135
136 /* Execute the proxy command. Note that we gave up any
137 extra privileges above. */
328654d8 138 execv(argv[0], argv);
139 perror(argv[0]);
5260325f 140 exit(1);
8efc0c15 141 }
5260325f 142 /* Parent. */
143 if (pid < 0)
144 fatal("fork failed: %.100s", strerror(errno));
6939bbd4 145 else
146 proxy_command_pid = pid; /* save pid to clean up later */
5260325f 147
148 /* Close child side of the descriptors. */
149 close(pin[0]);
150 close(pout[1]);
151
152 /* Free the command name. */
153 buffer_free(&command);
154
155 /* Set the connection file descriptors. */
156 packet_set_connection(pout[0], pin[1]);
8efc0c15 157
ce773142 158 /* Indicate OK return */
159 return 0;
5260325f 160}
8efc0c15 161
5260325f 162/*
163 * Creates a (possibly privileged) socket for use as the ssh connection.
164 */
396c147e 165static int
3e131a6d 166ssh_create_socket(int privileged, struct addrinfo *ai)
8efc0c15 167{
3435f5a6 168 int sock, gaierr;
169 struct addrinfo hints, *res;
5260325f 170
aa3378df 171 /*
172 * If we are running as root and want to connect to a privileged
173 * port, bind our own socket to a privileged port.
174 */
5260325f 175 if (privileged) {
176 int p = IPPORT_RESERVED - 1;
3fb156df 177 PRIV_START;
3e131a6d 178 sock = rresvport_af(&p, ai->ai_family);
3fb156df 179 PRIV_END;
5260325f 180 if (sock < 0)
3e131a6d 181 error("rresvport: af=%d %.100s", ai->ai_family,
182 strerror(errno));
c8d54615 183 else
184 debug("Allocated local port %d.", p);
3435f5a6 185 return sock;
186 }
3e131a6d 187 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
3435f5a6 188 if (sock < 0)
189 error("socket: %.100s", strerror(errno));
3435f5a6 190
191 /* Bind the socket to an alternative local IP address */
192 if (options.bind_address == NULL)
193 return sock;
194
195 memset(&hints, 0, sizeof(hints));
3e131a6d 196 hints.ai_family = ai->ai_family;
197 hints.ai_socktype = ai->ai_socktype;
198 hints.ai_protocol = ai->ai_protocol;
3435f5a6 199 hints.ai_flags = AI_PASSIVE;
200 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
201 if (gaierr) {
202 error("getaddrinfo: %s: %s", options.bind_address,
203 gai_strerror(gaierr));
204 close(sock);
205 return -1;
206 }
207 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
208 error("bind: %s: %s", options.bind_address, strerror(errno));
209 close(sock);
210 freeaddrinfo(res);
211 return -1;
5260325f 212 }
3435f5a6 213 freeaddrinfo(res);
5260325f 214 return sock;
8efc0c15 215}
216
5260325f 217/*
48e671d5 218 * Opens a TCP/IP connection to the remote server on the given host.
219 * The address of the remote host will be returned in hostaddr.
3fb156df 220 * If port is 0, the default port will be used. If needpriv is true,
5260325f 221 * a privileged port will be allocated to make the connection.
3fb156df 222 * This requires super-user privileges if needpriv is true.
5260325f 223 * Connection_attempts specifies the maximum number of tries (one per
224 * second). If proxy_command is non-NULL, it specifies the command (with %h
225 * and %p substituted for host and port, respectively) to use to contact
226 * the daemon.
ce773142 227 * Return values:
228 * 0 for OK
229 * ECONNREFUSED if we got a "Connection Refused" by the peer on any address
230 * ECONNABORTED if we failed without a "Connection refused"
231 * Suitable error messages for the connection failure will already have been
232 * printed.
5260325f 233 */
234int
48e671d5 235ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
dab89049 236 u_short port, int family, int connection_attempts,
3fb156df 237 int needpriv, const char *proxy_command)
8efc0c15 238{
b5c334cc 239 int gaierr;
240 int on = 1;
48e671d5 241 int sock = -1, attempt;
48e671d5 242 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
b5c334cc 243 struct addrinfo hints, *ai, *aitop;
b5c334cc 244 struct servent *sp;
ce773142 245 /*
246 * Did we get only other errors than "Connection refused" (which
247 * should block fallback to rsh and similar), or did we get at least
248 * one "Connection refused"?
249 */
250 int full_failure = 1;
5260325f 251
a77673cc 252 debug2("ssh_connect: needpriv %d", needpriv);
5260325f 253
254 /* Get default port if port has not been set. */
255 if (port == 0) {
256 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
257 if (sp)
258 port = ntohs(sp->s_port);
259 else
260 port = SSH_DEFAULT_PORT;
8efc0c15 261 }
5260325f 262 /* If a proxy command is given, connect using it. */
263 if (proxy_command != NULL)
3fb156df 264 return ssh_proxy_connect(host, port, proxy_command);
5260325f 265
266 /* No proxy command. */
267
48e671d5 268 memset(&hints, 0, sizeof(hints));
dab89049 269 hints.ai_family = family;
48e671d5 270 hints.ai_socktype = SOCK_STREAM;
d6133f43 271 snprintf(strport, sizeof strport, "%u", port);
48e671d5 272 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
273 fatal("%s: %.100s: %s", __progname, host,
274 gai_strerror(gaierr));
5260325f 275
95f1eccc 276 /*
277 * Try to connect several times. On some machines, the first time
278 * will sometimes fail. In general socket code appears to behave
279 * quite magically on many machines.
ce773142 280 */
281 for (attempt = 0; ;) {
5260325f 282 if (attempt > 0)
283 debug("Trying again...");
284
48e671d5 285 /* Loop through addresses for this host, and try each one in
6ae2364d 286 sequence until the connection succeeds. */
48e671d5 287 for (ai = aitop; ai; ai = ai->ai_next) {
288 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
289 continue;
290 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
291 ntop, sizeof(ntop), strport, sizeof(strport),
292 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
293 error("ssh_connect: getnameinfo failed");
294 continue;
295 }
296 debug("Connecting to %.200s [%.100s] port %s.",
297 host, ntop, strport);
298
299 /* Create a socket for connecting. */
3e131a6d 300 sock = ssh_create_socket(needpriv, ai);
48e671d5 301 if (sock < 0)
ce773142 302 /* Any error is already output */
48e671d5 303 continue;
304
48e671d5 305 if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) {
306 /* Successful connection. */
cd332296 307 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
5260325f 308 break;
48e671d5 309 } else {
ce773142 310 if (errno == ECONNREFUSED)
311 full_failure = 0;
c750d869 312 debug("connect to address %s port %s: %s",
313 ntop, strport, strerror(errno));
aa3378df 314 /*
315 * Close the failed socket; there appear to
316 * be some problems when reusing a socket for
317 * which connect() has already returned an
318 * error.
319 */
5260325f 320 close(sock);
321 }
8efc0c15 322 }
48e671d5 323 if (ai)
324 break; /* Successful connection. */
8efc0c15 325
ce773142 326 attempt++;
327 if (attempt >= connection_attempts)
328 break;
5260325f 329 /* Sleep a moment before retrying. */
330 sleep(1);
331 }
48e671d5 332
333 freeaddrinfo(aitop);
334
5260325f 335 /* Return failure if we didn't get a successful connection. */
9bd68577 336 if (attempt >= connection_attempts) {
bbe88b6d 337 logit("ssh: connect to host %s port %s: %s",
9bd68577 338 host, strport, strerror(errno));
ce773142 339 return full_failure ? ECONNABORTED : ECONNREFUSED;
9bd68577 340 }
8efc0c15 341
5260325f 342 debug("Connection established.");
8efc0c15 343
b5c334cc 344 /* Set keepalives if requested. */
345 if (options.keepalives &&
346 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
347 sizeof(on)) < 0)
348 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
8efc0c15 349
5260325f 350 /* Set the connection. */
351 packet_set_connection(sock, sock);
8efc0c15 352
ce773142 353 return 0;
8efc0c15 354}
355
5260325f 356/*
357 * Waits for the server identification string, and sends our own
358 * identification string.
359 */
396c147e 360static void
5ca51e19 361ssh_exchange_identification(void)
8efc0c15 362{
5260325f 363 char buf[256], remote_version[256]; /* must be same size! */
a8be9f80 364 int remote_major, remote_minor, i, mismatch;
5260325f 365 int connection_in = packet_get_connection_in();
366 int connection_out = packet_get_connection_out();
2b87da3b 367 int minor1 = PROTOCOL_MINOR_1;
5260325f 368
369 /* Read other side\'s version identification. */
38c295d6 370 for (;;) {
371 for (i = 0; i < sizeof(buf) - 1; i++) {
372 int len = atomicio(read, connection_in, &buf[i], 1);
373 if (len < 0)
374 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
375 if (len != 1)
376 fatal("ssh_exchange_identification: Connection closed by remote host");
377 if (buf[i] == '\r') {
378 buf[i] = '\n';
379 buf[i + 1] = 0;
380 continue; /**XXX wait for \n */
381 }
382 if (buf[i] == '\n') {
383 buf[i + 1] = 0;
384 break;
385 }
5260325f 386 }
38c295d6 387 buf[sizeof(buf) - 1] = 0;
388 if (strncmp(buf, "SSH-", 4) == 0)
5260325f 389 break;
38c295d6 390 debug("ssh_exchange_identification: %s", buf);
8efc0c15 391 }
8ce64345 392 server_version_string = xstrdup(buf);
5260325f 393
aa3378df 394 /*
395 * Check that the versions match. In future this might accept
396 * several versions and set appropriate flags to handle them.
397 */
8ce64345 398 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
399 &remote_major, &remote_minor, remote_version) != 3)
5260325f 400 fatal("Bad remote protocol version identification: '%.100s'", buf);
401 debug("Remote protocol version %d.%d, remote software version %.100s",
184eed6a 402 remote_major, remote_minor, remote_version);
5260325f 403
8ce64345 404 compat_datafellows(remote_version);
a8be9f80 405 mismatch = 0;
406
6aacefa7 407 switch (remote_major) {
a8be9f80 408 case 1:
409 if (remote_minor == 99 &&
410 (options.protocol & SSH_PROTO_2) &&
411 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
412 enable_compat20();
413 break;
5260325f 414 }
a8be9f80 415 if (!(options.protocol & SSH_PROTO_1)) {
416 mismatch = 1;
417 break;
418 }
419 if (remote_minor < 3) {
420 fatal("Remote machine has too old SSH software version.");
fa08c86b 421 } else if (remote_minor == 3 || remote_minor == 4) {
a8be9f80 422 /* We speak 1.3, too. */
423 enable_compat13();
fa08c86b 424 minor1 = 3;
a8be9f80 425 if (options.forward_agent) {
bbe88b6d 426 logit("Agent forwarding disabled for protocol 1.3");
a8be9f80 427 options.forward_agent = 0;
428 }
429 }
430 break;
431 case 2:
432 if (options.protocol & SSH_PROTO_2) {
433 enable_compat20();
434 break;
435 }
436 /* FALLTHROUGH */
6ae2364d 437 default:
a8be9f80 438 mismatch = 1;
439 break;
8efc0c15 440 }
a8be9f80 441 if (mismatch)
5260325f 442 fatal("Protocol major versions differ: %d vs. %d",
a8be9f80 443 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
444 remote_major);
5260325f 445 /* Send our own protocol version identification. */
446 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
a8be9f80 447 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
fa08c86b 448 compat20 ? PROTOCOL_MINOR_2 : minor1,
8ce64345 449 SSH_VERSION);
a408af76 450 if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf))
5260325f 451 fatal("write: %.100s", strerror(errno));
8ce64345 452 client_version_string = xstrdup(buf);
453 chop(client_version_string);
454 chop(server_version_string);
455 debug("Local version string %.100s", client_version_string);
8efc0c15 456}
457
f21032a6 458/* defaults to 'no' */
396c147e 459static int
a2c92c4a 460confirm(const char *prompt)
8efc0c15 461{
616a6b93 462 const char *msg, *again = "Please type 'yes' or 'no': ";
463 char *p;
464 int ret = -1;
5260325f 465
f21032a6 466 if (options.batch_mode)
467 return 0;
616a6b93 468 for (msg = prompt;;msg = again) {
469 p = read_passphrase(msg, RP_ECHO);
470 if (p == NULL ||
471 (p[0] == '\0') || (p[0] == '\n') ||
472 strncasecmp(p, "no", 2) == 0)
473 ret = 0;
f11fe301 474 if (p && strncasecmp(p, "yes", 3) == 0)
616a6b93 475 ret = 1;
476 if (p)
477 xfree(p);
478 if (ret != -1)
479 return ret;
8efc0c15 480 }
8efc0c15 481}
482
5260325f 483/*
f49bc4f7 484 * check whether the supplied host key is valid, return -1 if the key
485 * is not valid. the user_hostfile will not be updated if 'readonly' is true.
5260325f 486 */
396c147e 487static int
a306f2dd 488check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
f49bc4f7 489 int readonly, const char *user_hostfile, const char *system_hostfile)
8efc0c15 490{
4fe2af09 491 Key *file_key;
1d1ffb87 492 char *type = key_type(host_key);
95f1eccc 493 char *ip = NULL;
22138a36 494 char hostline[1000], *hostp, *fp;
5260325f 495 HostStatus host_status;
496 HostStatus ip_status;
48e671d5 497 int local = 0, host_ip_differ = 0;
20af321f 498 int salen;
48e671d5 499 char ntop[NI_MAXHOST];
616a6b93 500 char msg[1024];
8a48a7ef 501 int len, host_line, ip_line, has_keys;
1e3b8b07 502 const char *host_file = NULL, *ip_file = NULL;
48e671d5 503
504 /*
505 * Force accepting of the host key for loopback/localhost. The
506 * problem is that if the home directory is NFS-mounted to multiple
507 * machines, localhost will refer to a different machine in each of
508 * them, and the user will get bogus HOST_CHANGED warnings. This
509 * essentially disables host authentication for localhost; however,
510 * this is probably not a real problem.
511 */
a306f2dd 512 /** hostaddr == 0! */
48e671d5 513 switch (hostaddr->sa_family) {
514 case AF_INET:
f49bc4f7 515 local = (ntohl(((struct sockaddr_in *)hostaddr)->
20a11d35 516 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
20af321f 517 salen = sizeof(struct sockaddr_in);
48e671d5 518 break;
519 case AF_INET6:
f49bc4f7 520 local = IN6_IS_ADDR_LOOPBACK(
521 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
20af321f 522 salen = sizeof(struct sockaddr_in6);
48e671d5 523 break;
524 default:
525 local = 0;
20af321f 526 salen = sizeof(struct sockaddr_storage);
48e671d5 527 break;
528 }
8bbc048a 529 if (options.no_host_authentication_for_localhost == 1 && local &&
530 options.host_key_alias == NULL) {
7c49df64 531 debug("Forcing accepting of host key for "
532 "loopback/localhost.");
f49bc4f7 533 return 0;
48e671d5 534 }
5260325f 535
57112b5a 536 /*
7c49df64 537 * We don't have the remote ip-address for connections
538 * using a proxy command
539 */
540 if (options.proxy_command == NULL) {
541 if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop),
542 NULL, 0, NI_NUMERICHOST) != 0)
543 fatal("check_host_key: getnameinfo failed");
544 ip = xstrdup(ntop);
545 } else {
546 ip = xstrdup("<no hostip for proxy command>");
547 }
548 /*
549 * Turn off check_host_ip if the connection is to localhost, via proxy
550 * command or if we don't have a hostname to compare with
57112b5a 551 */
7c49df64 552 if (options.check_host_ip &&
553 (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
57112b5a 554 options.check_host_ip = 0;
555
8abcdba4 556 /*
7c49df64 557 * Allow the user to record the key under a different name. This is
8abcdba4 558 * useful for ssh tunneling over forwarded connections or if you run
7c49df64 559 * multiple sshd's on different ports on the same machine.
8abcdba4 560 */
561 if (options.host_key_alias != NULL) {
562 host = options.host_key_alias;
563 debug("using hostkeyalias: %s", host);
564 }
565
95f1eccc 566 /*
567 * Store the host key from the known host file in here so that we can
568 * compare it with the key for the IP address.
569 */
4fe2af09 570 file_key = key_new(host_key->type);
5260325f 571
aa3378df 572 /*
573 * Check if the host key is present in the user\'s list of known
574 * hosts or in the systemwide list.
575 */
1e3b8b07 576 host_file = user_hostfile;
f49bc4f7 577 host_status = check_host_in_hostfile(host_file, host, host_key,
184eed6a 578 file_key, &host_line);
1e3b8b07 579 if (host_status == HOST_NEW) {
580 host_file = system_hostfile;
f49bc4f7 581 host_status = check_host_in_hostfile(host_file, host, host_key,
582 file_key, &host_line);
1e3b8b07 583 }
aa3378df 584 /*
585 * Also perform check for the ip address, skip the check if we are
586 * localhost or the hostname was an ip address to begin with
587 */
7c49df64 588 if (options.check_host_ip) {
4fe2af09 589 Key *ip_key = key_new(host_key->type);
5260325f 590
1e3b8b07 591 ip_file = user_hostfile;
f49bc4f7 592 ip_status = check_host_in_hostfile(ip_file, ip, host_key,
593 ip_key, &ip_line);
1e3b8b07 594 if (ip_status == HOST_NEW) {
595 ip_file = system_hostfile;
f49bc4f7 596 ip_status = check_host_in_hostfile(ip_file, ip,
597 host_key, ip_key, &ip_line);
1e3b8b07 598 }
5260325f 599 if (host_status == HOST_CHANGED &&
4fe2af09 600 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
5260325f 601 host_ip_differ = 1;
602
4fe2af09 603 key_free(ip_key);
5260325f 604 } else
605 ip_status = host_status;
606
4fe2af09 607 key_free(file_key);
5260325f 608
609 switch (host_status) {
610 case HOST_OK:
611 /* The host is known and the key matches. */
1d1ffb87 612 debug("Host '%.200s' is known and matches the %s host key.",
613 host, type);
1e3b8b07 614 debug("Found key in %s:%d", host_file, host_line);
7c49df64 615 if (options.check_host_ip && ip_status == HOST_NEW) {
f49bc4f7 616 if (readonly)
bbe88b6d 617 logit("%s host key for IP address "
f49bc4f7 618 "'%.128s' not in list of known hosts.",
7c49df64 619 type, ip);
f49bc4f7 620 else if (!add_host_to_hostfile(user_hostfile, ip,
184eed6a 621 host_key))
bbe88b6d 622 logit("Failed to add the %s host key for IP "
f49bc4f7 623 "address '%.128s' to the list of known "
624 "hosts (%.30s).", type, ip, user_hostfile);
625 else
bbe88b6d 626 logit("Warning: Permanently added the %s host "
f49bc4f7 627 "key for IP address '%.128s' to the list "
628 "of known hosts.", type, ip);
5260325f 629 }
630 break;
631 case HOST_NEW:
f49bc4f7 632 if (readonly)
633 goto fail;
5260325f 634 /* The host is new. */
635 if (options.strict_host_key_checking == 1) {
f49bc4f7 636 /*
637 * User has requested strict host key checking. We
638 * will not add the host key automatically. The only
639 * alternative left is to abort.
640 */
641 error("No %s host key is known for %.200s and you "
642 "have requested strict checking.", type, host);
643 goto fail;
5260325f 644 } else if (options.strict_host_key_checking == 2) {
8a48a7ef 645 has_keys = show_other_keys(host, host_key);
5260325f 646 /* The default */
22138a36 647 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
616a6b93 648 snprintf(msg, sizeof(msg),
f49bc4f7 649 "The authenticity of host '%.200s (%s)' can't be "
8a48a7ef 650 "established%s\n"
1d1ffb87 651 "%s key fingerprint is %s.\n"
f49bc4f7 652 "Are you sure you want to continue connecting "
8a48a7ef 653 "(yes/no)? ",
b77a87e5 654 host, ip,
655 has_keys ? ",\nbut keys of different type are already "
656 "known for this host." : ".",
657 type, fp);
22138a36 658 xfree(fp);
616a6b93 659 if (!confirm(msg))
f49bc4f7 660 goto fail;
5260325f 661 }
7c49df64 662 if (options.check_host_ip && ip_status == HOST_NEW) {
5260325f 663 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
664 hostp = hostline;
665 } else
666 hostp = host;
667
f49bc4f7 668 /*
669 * If not in strict mode, add the key automatically to the
670 * local known_hosts file.
671 */
a306f2dd 672 if (!add_host_to_hostfile(user_hostfile, hostp, host_key))
bbe88b6d 673 logit("Failed to add the host to the list of known "
f49bc4f7 674 "hosts (%.500s).", user_hostfile);
5260325f 675 else
bbe88b6d 676 logit("Warning: Permanently added '%.200s' (%s) to the "
f49bc4f7 677 "list of known hosts.", hostp, type);
5260325f 678 break;
679 case HOST_CHANGED:
680 if (options.check_host_ip && host_ip_differ) {
681 char *msg;
682 if (ip_status == HOST_NEW)
683 msg = "is unknown";
684 else if (ip_status == HOST_OK)
685 msg = "is unchanged";
686 else
687 msg = "has a different value";
688 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
689 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
690 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1d1ffb87 691 error("The %s host key for %s has changed,", type, host);
5260325f 692 error("and the key for the according IP address %s", ip);
693 error("%s. This could either mean that", msg);
694 error("DNS SPOOFING is happening or the IP address for the host");
1e3b8b07 695 error("and its host key have changed at the same time.");
7c49df64 696 if (ip_status != HOST_NEW)
1e3b8b07 697 error("Offending key for IP in %s:%d", ip_file, ip_line);
5260325f 698 }
699 /* The host key has changed. */
22138a36 700 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
5260325f 701 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
ae2f7af7 702 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
5260325f 703 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
704 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
705 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1d1ffb87 706 error("It is also possible that the %s host key has just been changed.", type);
8abcdba4 707 error("The fingerprint for the %s key sent by the remote host is\n%s.",
22138a36 708 type, fp);
5260325f 709 error("Please contact your system administrator.");
710 error("Add correct host key in %.100s to get rid of this message.",
8abcdba4 711 user_hostfile);
1e3b8b07 712 error("Offending key in %s:%d", host_file, host_line);
22138a36 713 xfree(fp);
5260325f 714
aa3378df 715 /*
716 * If strict host key checking is in use, the user will have
717 * to edit the key manually and we can only abort.
718 */
f49bc4f7 719 if (options.strict_host_key_checking) {
720 error("%s host key for %.200s has changed and you have "
721 "requested strict checking.", type, host);
722 goto fail;
723 }
5260325f 724
aa3378df 725 /*
726 * If strict host key checking has not been requested, allow
727 * the connection but without password authentication or
728 * agent forwarding.
729 */
5260325f 730 if (options.password_authentication) {
f49bc4f7 731 error("Password authentication is disabled to avoid "
732 "man-in-the-middle attacks.");
5260325f 733 options.password_authentication = 0;
734 }
735 if (options.forward_agent) {
f49bc4f7 736 error("Agent forwarding is disabled to avoid "
737 "man-in-the-middle attacks.");
5260325f 738 options.forward_agent = 0;
739 }
0b6fbf03 740 if (options.forward_x11) {
f49bc4f7 741 error("X11 forwarding is disabled to avoid "
742 "man-in-the-middle attacks.");
0b6fbf03 743 options.forward_x11 = 0;
744 }
f49bc4f7 745 if (options.num_local_forwards > 0 ||
746 options.num_remote_forwards > 0) {
747 error("Port forwarding is disabled to avoid "
748 "man-in-the-middle attacks.");
749 options.num_local_forwards =
184eed6a 750 options.num_remote_forwards = 0;
0b6fbf03 751 }
aa3378df 752 /*
753 * XXX Should permit the user to change to use the new id.
754 * This could be done by converting the host key to an
755 * identifying sentence, tell that the host identifies itself
756 * by that sentence, and ask the user if he/she whishes to
757 * accept the authentication.
758 */
5260325f 759 break;
8a48a7ef 760 case HOST_FOUND:
761 fatal("internal error");
762 break;
5260325f 763 }
0b6fbf03 764
7c49df64 765 if (options.check_host_ip && host_status != HOST_CHANGED &&
766 ip_status == HOST_CHANGED) {
616a6b93 767 snprintf(msg, sizeof(msg),
768 "Warning: the %s host key for '%.200s' "
769 "differs from the key for the IP address '%.128s'"
770 "\nOffending key for IP in %s:%d",
771 type, host, ip, ip_file, ip_line);
772 if (host_status == HOST_OK) {
773 len = strlen(msg);
774 snprintf(msg + len, sizeof(msg) - len,
775 "\nMatching host key in %s:%d",
7203d6bb 776 host_file, host_line);
616a6b93 777 }
7c49df64 778 if (options.strict_host_key_checking == 1) {
bbe88b6d 779 logit(msg);
f49bc4f7 780 error("Exiting, you have requested strict checking.");
781 goto fail;
7c49df64 782 } else if (options.strict_host_key_checking == 2) {
616a6b93 783 strlcat(msg, "\nAre you sure you want "
784 "to continue connecting (yes/no)? ", sizeof(msg));
785 if (!confirm(msg))
f49bc4f7 786 goto fail;
616a6b93 787 } else {
bbe88b6d 788 logit(msg);
7c49df64 789 }
790 }
791
0b6fbf03 792 xfree(ip);
f49bc4f7 793 return 0;
794
795fail:
796 xfree(ip);
797 return -1;
798}
799
800int
801verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
802{
803 struct stat st;
804
805 /* return ok if the key can be found in an old keyfile */
806 if (stat(options.system_hostfile2, &st) == 0 ||
807 stat(options.user_hostfile2, &st) == 0) {
808 if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
809 options.user_hostfile2, options.system_hostfile2) == 0)
810 return 0;
811 }
812 return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
813 options.user_hostfile, options.system_hostfile);
95f1eccc 814}
815
8ce64345 816/*
a306f2dd 817 * Starts a dialog with the server, and authenticates the current user on the
818 * server. This does not need any extra privileges. The basic connection
819 * to the server must already have been established before this is called.
820 * If login fails, this function prints an error and never returns.
821 * This function does not require super-user privileges.
8ce64345 822 */
823void
39c00dc2 824ssh_login(Sensitive *sensitive, const char *orighost,
63bd8c36 825 struct sockaddr *hostaddr, struct passwd *pw)
8ce64345 826{
a306f2dd 827 char *host, *cp;
8ce64345 828 char *server_user, *local_user;
8ce64345 829
8ce64345 830 local_user = xstrdup(pw->pw_name);
831 server_user = options.user ? options.user : local_user;
832
7b2ea3a1 833 /* Convert the user-supplied hostname into all lowercase. */
834 host = xstrdup(orighost);
835 for (cp = host; *cp; cp++)
836 if (isupper(*cp))
837 *cp = tolower(*cp);
838
839 /* Exchange protocol version identification strings with the server. */
840 ssh_exchange_identification();
841
842 /* Put the connection into non-blocking mode. */
843 packet_set_nonblocking();
844
7b2ea3a1 845 /* key exchange */
7b2ea3a1 846 /* authenticate user */
8ce64345 847 if (compat20) {
848 ssh_kex2(host, hostaddr);
39c00dc2 849 ssh_userauth2(local_user, server_user, host, sensitive);
8ce64345 850 } else {
8ce64345 851 ssh_kex(host, hostaddr);
39c00dc2 852 ssh_userauth1(local_user, server_user, host, sensitive);
8ce64345 853 }
7b2ea3a1 854}
0ae4fe1d 855
856void
857ssh_put_password(char *password)
858{
859 int size;
860 char *padded;
861
99c415db 862 if (datafellows & SSH_BUG_PASSWORDPAD) {
449c5ba5 863 packet_put_cstring(password);
99c415db 864 return;
865 }
0ae4fe1d 866 size = roundup(strlen(password) + 1, 32);
867 padded = xmalloc(size);
868 memset(padded, 0, size);
869 strlcpy(padded, password, size);
870 packet_put_string(padded, size);
871 memset(padded, 0, size);
872 xfree(padded);
873}
8a48a7ef 874
875static int
876show_key_from_file(const char *file, const char *host, int keytype)
877{
878 Key *found;
879 char *fp;
880 int line, ret;
881
882 found = key_new(keytype);
883 if ((ret = lookup_key_in_hostfile_by_type(file, host,
884 keytype, found, &line))) {
885 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
bbe88b6d 886 logit("WARNING: %s key found for host %s\n"
567a05bf 887 "in %s:%d\n"
8a48a7ef 888 "%s key fingerprint %s.",
889 key_type(found), host, file, line,
890 key_type(found), fp);
891 xfree(fp);
892 }
893 key_free(found);
894 return (ret);
895}
896
897/* print all known host keys for a given host, but skip keys of given type */
898static int
899show_other_keys(const char *host, Key *key)
900{
901 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
902 int i, found = 0;
903
904 for (i = 0; type[i] != -1; i++) {
905 if (type[i] == key->type)
906 continue;
907 if (type[i] != KEY_RSA1 &&
908 show_key_from_file(options.user_hostfile2, host, type[i])) {
909 found = 1;
910 continue;
911 }
912 if (type[i] != KEY_RSA1 &&
913 show_key_from_file(options.system_hostfile2, host, type[i])) {
914 found = 1;
915 continue;
916 }
917 if (show_key_from_file(options.user_hostfile, host, type[i])) {
918 found = 1;
919 continue;
920 }
921 if (show_key_from_file(options.system_hostfile, host, type[i])) {
922 found = 1;
923 continue;
924 }
925 debug2("no key of type %d for host %s", type[i], host);
926 }
927 return (found);
928}
This page took 0.343943 seconds and 5 git commands to generate.