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