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