]>
Commit | Line | Data |
---|---|---|
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" | |
bcbf86ec | 16 | RCSID("$OpenBSD: sshconnect.c,v 1.78 2000/09/07 20:27:54 deraadt Exp $"); |
8efc0c15 | 17 | |
8ce64345 | 18 | #include <openssl/bn.h> |
a306f2dd | 19 | #include <openssl/dsa.h> |
20 | #include <openssl/rsa.h> | |
21 | ||
8efc0c15 | 22 | #include "xmalloc.h" |
23 | #include "rsa.h" | |
24 | #include "ssh.h" | |
8ce64345 | 25 | #include "buffer.h" |
8efc0c15 | 26 | #include "packet.h" |
8efc0c15 | 27 | #include "uidswap.h" |
28 | #include "compat.h" | |
6fa724bc | 29 | #include "readconf.h" |
4fe2af09 | 30 | #include "key.h" |
a306f2dd | 31 | #include "sshconnect.h" |
4fe2af09 | 32 | #include "hostfile.h" |
8efc0c15 | 33 | |
a306f2dd | 34 | char *client_version_string = NULL; |
35 | char *server_version_string = NULL; | |
8ce64345 | 36 | |
57112b5a | 37 | extern Options options; |
3fd95d9a | 38 | #ifdef HAVE___PROGNAME |
48e671d5 | 39 | extern char *__progname; |
3fd95d9a | 40 | #else /* HAVE___PROGNAME */ |
41 | static const char *__progname = "ssh"; | |
42 | #endif /* HAVE___PROGNAME */ | |
57112b5a | 43 | |
5260325f | 44 | /* |
45 | * Connect to the given ssh server using a proxy command. | |
46 | */ | |
8efc0c15 | 47 | int |
57112b5a | 48 | ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid, |
8efc0c15 | 49 | const char *proxy_command) |
50 | { | |
5260325f | 51 | Buffer command; |
52 | const char *cp; | |
53 | char *command_string; | |
54 | int pin[2], pout[2]; | |
9da5c3c9 | 55 | pid_t pid; |
48e671d5 | 56 | char strport[NI_MAXSERV]; |
5260325f | 57 | |
58 | /* Convert the port number into a string. */ | |
48e671d5 | 59 | snprintf(strport, sizeof strport, "%hu", port); |
5260325f | 60 | |
61 | /* Build the final command string in the buffer by making the | |
62 | appropriate substitutions to the given proxy command. */ | |
63 | buffer_init(&command); | |
64 | for (cp = proxy_command; *cp; cp++) { | |
65 | if (cp[0] == '%' && cp[1] == '%') { | |
66 | buffer_append(&command, "%", 1); | |
67 | cp++; | |
68 | continue; | |
69 | } | |
70 | if (cp[0] == '%' && cp[1] == 'h') { | |
71 | buffer_append(&command, host, strlen(host)); | |
72 | cp++; | |
73 | continue; | |
74 | } | |
75 | if (cp[0] == '%' && cp[1] == 'p') { | |
48e671d5 | 76 | buffer_append(&command, strport, strlen(strport)); |
5260325f | 77 | cp++; |
78 | continue; | |
79 | } | |
80 | buffer_append(&command, cp, 1); | |
8efc0c15 | 81 | } |
5260325f | 82 | buffer_append(&command, "\0", 1); |
83 | ||
84 | /* Get the final command string. */ | |
85 | command_string = buffer_ptr(&command); | |
86 | ||
87 | /* Create pipes for communicating with the proxy. */ | |
88 | if (pipe(pin) < 0 || pipe(pout) < 0) | |
89 | fatal("Could not create pipes to communicate with the proxy: %.100s", | |
90 | strerror(errno)); | |
91 | ||
92 | debug("Executing proxy command: %.500s", command_string); | |
93 | ||
94 | /* Fork and execute the proxy command. */ | |
95 | if ((pid = fork()) == 0) { | |
96 | char *argv[10]; | |
97 | ||
98 | /* Child. Permanently give up superuser privileges. */ | |
99 | permanently_set_uid(original_real_uid); | |
100 | ||
101 | /* Redirect stdin and stdout. */ | |
102 | close(pin[1]); | |
103 | if (pin[0] != 0) { | |
104 | if (dup2(pin[0], 0) < 0) | |
105 | perror("dup2 stdin"); | |
106 | close(pin[0]); | |
107 | } | |
108 | close(pout[0]); | |
109 | if (dup2(pout[1], 1) < 0) | |
110 | perror("dup2 stdout"); | |
111 | /* Cannot be 1 because pin allocated two descriptors. */ | |
112 | close(pout[1]); | |
113 | ||
114 | /* Stderr is left as it is so that error messages get | |
115 | printed on the user's terminal. */ | |
30228d7c | 116 | argv[0] = _PATH_BSHELL; |
5260325f | 117 | argv[1] = "-c"; |
118 | argv[2] = command_string; | |
119 | argv[3] = NULL; | |
120 | ||
121 | /* Execute the proxy command. Note that we gave up any | |
122 | extra privileges above. */ | |
30228d7c | 123 | execv(_PATH_BSHELL, argv); |
124 | perror(_PATH_BSHELL); | |
5260325f | 125 | exit(1); |
8efc0c15 | 126 | } |
5260325f | 127 | /* Parent. */ |
128 | if (pid < 0) | |
129 | fatal("fork failed: %.100s", strerror(errno)); | |
130 | ||
131 | /* Close child side of the descriptors. */ | |
132 | close(pin[0]); | |
133 | close(pout[1]); | |
134 | ||
135 | /* Free the command name. */ | |
136 | buffer_free(&command); | |
137 | ||
138 | /* Set the connection file descriptors. */ | |
139 | packet_set_connection(pout[0], pin[1]); | |
8efc0c15 | 140 | |
5260325f | 141 | return 1; |
142 | } | |
8efc0c15 | 143 | |
5260325f | 144 | /* |
145 | * Creates a (possibly privileged) socket for use as the ssh connection. | |
146 | */ | |
147 | int | |
48e671d5 | 148 | ssh_create_socket(uid_t original_real_uid, int privileged, int family) |
8efc0c15 | 149 | { |
5260325f | 150 | int sock; |
151 | ||
aa3378df | 152 | /* |
153 | * If we are running as root and want to connect to a privileged | |
154 | * port, bind our own socket to a privileged port. | |
155 | */ | |
5260325f | 156 | if (privileged) { |
157 | int p = IPPORT_RESERVED - 1; | |
48e671d5 | 158 | sock = rresvport_af(&p, family); |
5260325f | 159 | if (sock < 0) |
c8d54615 | 160 | error("rresvport: af=%d %.100s", family, strerror(errno)); |
161 | else | |
162 | debug("Allocated local port %d.", p); | |
5260325f | 163 | } else { |
95f1eccc | 164 | /* |
165 | * Just create an ordinary socket on arbitrary port. We use | |
166 | * the user's uid to create the socket. | |
167 | */ | |
5260325f | 168 | temporarily_use_uid(original_real_uid); |
48e671d5 | 169 | sock = socket(family, SOCK_STREAM, 0); |
5260325f | 170 | if (sock < 0) |
48e671d5 | 171 | error("socket: %.100s", strerror(errno)); |
5260325f | 172 | restore_uid(); |
173 | } | |
174 | return sock; | |
8efc0c15 | 175 | } |
176 | ||
5260325f | 177 | /* |
48e671d5 | 178 | * Opens a TCP/IP connection to the remote server on the given host. |
179 | * The address of the remote host will be returned in hostaddr. | |
180 | * If port is 0, the default port will be used. If anonymous is zero, | |
5260325f | 181 | * a privileged port will be allocated to make the connection. |
182 | * This requires super-user privileges if anonymous is false. | |
183 | * Connection_attempts specifies the maximum number of tries (one per | |
184 | * second). If proxy_command is non-NULL, it specifies the command (with %h | |
185 | * and %p substituted for host and port, respectively) to use to contact | |
186 | * the daemon. | |
187 | */ | |
188 | int | |
48e671d5 | 189 | ssh_connect(const char *host, struct sockaddr_storage * hostaddr, |
57112b5a | 190 | u_short port, int connection_attempts, |
5260325f | 191 | int anonymous, uid_t original_real_uid, |
192 | const char *proxy_command) | |
8efc0c15 | 193 | { |
48e671d5 | 194 | int sock = -1, attempt; |
5260325f | 195 | struct servent *sp; |
48e671d5 | 196 | struct addrinfo hints, *ai, *aitop; |
197 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; | |
198 | int gaierr; | |
5260325f | 199 | struct linger linger; |
200 | ||
14a9a859 | 201 | debug("ssh_connect: getuid %u geteuid %u anon %d", |
202 | (u_int) getuid(), (u_int) geteuid(), anonymous); | |
5260325f | 203 | |
204 | /* Get default port if port has not been set. */ | |
205 | if (port == 0) { | |
206 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); | |
207 | if (sp) | |
208 | port = ntohs(sp->s_port); | |
209 | else | |
210 | port = SSH_DEFAULT_PORT; | |
8efc0c15 | 211 | } |
5260325f | 212 | /* If a proxy command is given, connect using it. */ |
213 | if (proxy_command != NULL) | |
214 | return ssh_proxy_connect(host, port, original_real_uid, proxy_command); | |
215 | ||
216 | /* No proxy command. */ | |
217 | ||
48e671d5 | 218 | memset(&hints, 0, sizeof(hints)); |
219 | hints.ai_family = IPv4or6; | |
220 | hints.ai_socktype = SOCK_STREAM; | |
221 | snprintf(strport, sizeof strport, "%d", port); | |
222 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) | |
223 | fatal("%s: %.100s: %s", __progname, host, | |
224 | gai_strerror(gaierr)); | |
5260325f | 225 | |
95f1eccc | 226 | /* |
227 | * Try to connect several times. On some machines, the first time | |
228 | * will sometimes fail. In general socket code appears to behave | |
229 | * quite magically on many machines. | |
230 | */ | |
5260325f | 231 | for (attempt = 0; attempt < connection_attempts; attempt++) { |
232 | if (attempt > 0) | |
233 | debug("Trying again..."); | |
234 | ||
48e671d5 | 235 | /* Loop through addresses for this host, and try each one in |
6ae2364d | 236 | sequence until the connection succeeds. */ |
48e671d5 | 237 | for (ai = aitop; ai; ai = ai->ai_next) { |
238 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) | |
239 | continue; | |
240 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, | |
241 | ntop, sizeof(ntop), strport, sizeof(strport), | |
242 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { | |
243 | error("ssh_connect: getnameinfo failed"); | |
244 | continue; | |
245 | } | |
246 | debug("Connecting to %.200s [%.100s] port %s.", | |
247 | host, ntop, strport); | |
248 | ||
249 | /* Create a socket for connecting. */ | |
6ae2364d | 250 | sock = ssh_create_socket(original_real_uid, |
3c62e7eb | 251 | #ifdef HAVE_CYGWIN |
252 | !anonymous && port < IPPORT_RESERVED, | |
253 | #else | |
48e671d5 | 254 | !anonymous && geteuid() == 0 && port < IPPORT_RESERVED, |
3c62e7eb | 255 | #endif |
48e671d5 | 256 | ai->ai_family); |
257 | if (sock < 0) | |
258 | continue; | |
259 | ||
260 | /* Connect to the host. We use the user's uid in the | |
261 | * hope that it will help with tcp_wrappers showing | |
262 | * the remote uid as root. | |
aa3378df | 263 | */ |
5260325f | 264 | temporarily_use_uid(original_real_uid); |
48e671d5 | 265 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) { |
266 | /* Successful connection. */ | |
3d1a1654 | 267 | memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen); |
5260325f | 268 | restore_uid(); |
269 | break; | |
48e671d5 | 270 | } else { |
5260325f | 271 | debug("connect: %.100s", strerror(errno)); |
272 | restore_uid(); | |
aa3378df | 273 | /* |
274 | * Close the failed socket; there appear to | |
275 | * be some problems when reusing a socket for | |
276 | * which connect() has already returned an | |
277 | * error. | |
278 | */ | |
5260325f | 279 | shutdown(sock, SHUT_RDWR); |
280 | close(sock); | |
281 | } | |
8efc0c15 | 282 | } |
48e671d5 | 283 | if (ai) |
284 | break; /* Successful connection. */ | |
8efc0c15 | 285 | |
5260325f | 286 | /* Sleep a moment before retrying. */ |
287 | sleep(1); | |
288 | } | |
48e671d5 | 289 | |
290 | freeaddrinfo(aitop); | |
291 | ||
5260325f | 292 | /* Return failure if we didn't get a successful connection. */ |
293 | if (attempt >= connection_attempts) | |
294 | return 0; | |
8efc0c15 | 295 | |
5260325f | 296 | debug("Connection established."); |
8efc0c15 | 297 | |
aa3378df | 298 | /* |
299 | * Set socket options. We would like the socket to disappear as soon | |
300 | * as it has been closed for whatever reason. | |
301 | */ | |
302 | /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ | |
5260325f | 303 | linger.l_onoff = 1; |
304 | linger.l_linger = 5; | |
305 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); | |
8efc0c15 | 306 | |
5260325f | 307 | /* Set the connection. */ |
308 | packet_set_connection(sock, sock); | |
8efc0c15 | 309 | |
5260325f | 310 | return 1; |
8efc0c15 | 311 | } |
312 | ||
5260325f | 313 | /* |
314 | * Waits for the server identification string, and sends our own | |
315 | * identification string. | |
316 | */ | |
317 | void | |
318 | ssh_exchange_identification() | |
8efc0c15 | 319 | { |
5260325f | 320 | char buf[256], remote_version[256]; /* must be same size! */ |
a8be9f80 | 321 | int remote_major, remote_minor, i, mismatch; |
5260325f | 322 | int connection_in = packet_get_connection_in(); |
323 | int connection_out = packet_get_connection_out(); | |
5260325f | 324 | |
325 | /* Read other side\'s version identification. */ | |
38c295d6 | 326 | for (;;) { |
327 | for (i = 0; i < sizeof(buf) - 1; i++) { | |
328 | int len = atomicio(read, connection_in, &buf[i], 1); | |
329 | if (len < 0) | |
330 | fatal("ssh_exchange_identification: read: %.100s", strerror(errno)); | |
331 | if (len != 1) | |
332 | fatal("ssh_exchange_identification: Connection closed by remote host"); | |
333 | if (buf[i] == '\r') { | |
334 | buf[i] = '\n'; | |
335 | buf[i + 1] = 0; | |
336 | continue; /**XXX wait for \n */ | |
337 | } | |
338 | if (buf[i] == '\n') { | |
339 | buf[i + 1] = 0; | |
340 | break; | |
341 | } | |
5260325f | 342 | } |
38c295d6 | 343 | buf[sizeof(buf) - 1] = 0; |
344 | if (strncmp(buf, "SSH-", 4) == 0) | |
5260325f | 345 | break; |
38c295d6 | 346 | debug("ssh_exchange_identification: %s", buf); |
8efc0c15 | 347 | } |
8ce64345 | 348 | server_version_string = xstrdup(buf); |
5260325f | 349 | |
aa3378df | 350 | /* |
351 | * Check that the versions match. In future this might accept | |
352 | * several versions and set appropriate flags to handle them. | |
353 | */ | |
8ce64345 | 354 | if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n", |
355 | &remote_major, &remote_minor, remote_version) != 3) | |
5260325f | 356 | fatal("Bad remote protocol version identification: '%.100s'", buf); |
357 | debug("Remote protocol version %d.%d, remote software version %.100s", | |
358 | remote_major, remote_minor, remote_version); | |
359 | ||
8ce64345 | 360 | compat_datafellows(remote_version); |
a8be9f80 | 361 | mismatch = 0; |
362 | ||
363 | switch(remote_major) { | |
364 | case 1: | |
365 | if (remote_minor == 99 && | |
366 | (options.protocol & SSH_PROTO_2) && | |
367 | !(options.protocol & SSH_PROTO_1_PREFERRED)) { | |
368 | enable_compat20(); | |
369 | break; | |
5260325f | 370 | } |
a8be9f80 | 371 | if (!(options.protocol & SSH_PROTO_1)) { |
372 | mismatch = 1; | |
373 | break; | |
374 | } | |
375 | if (remote_minor < 3) { | |
376 | fatal("Remote machine has too old SSH software version."); | |
377 | } else if (remote_minor == 3) { | |
378 | /* We speak 1.3, too. */ | |
379 | enable_compat13(); | |
380 | if (options.forward_agent) { | |
381 | log("Agent forwarding disabled for protocol 1.3"); | |
382 | options.forward_agent = 0; | |
383 | } | |
384 | } | |
385 | break; | |
386 | case 2: | |
387 | if (options.protocol & SSH_PROTO_2) { | |
388 | enable_compat20(); | |
389 | break; | |
390 | } | |
391 | /* FALLTHROUGH */ | |
6ae2364d | 392 | default: |
a8be9f80 | 393 | mismatch = 1; |
394 | break; | |
8efc0c15 | 395 | } |
a8be9f80 | 396 | if (mismatch) |
5260325f | 397 | fatal("Protocol major versions differ: %d vs. %d", |
a8be9f80 | 398 | (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, |
399 | remote_major); | |
a306f2dd | 400 | if (compat20) |
401 | packet_set_ssh2_format(); | |
5260325f | 402 | /* Send our own protocol version identification. */ |
403 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", | |
a8be9f80 | 404 | compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, |
405 | compat20 ? PROTOCOL_MINOR_2 : PROTOCOL_MINOR_1, | |
8ce64345 | 406 | SSH_VERSION); |
a408af76 | 407 | if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf)) |
5260325f | 408 | fatal("write: %.100s", strerror(errno)); |
8ce64345 | 409 | client_version_string = xstrdup(buf); |
410 | chop(client_version_string); | |
411 | chop(server_version_string); | |
412 | debug("Local version string %.100s", client_version_string); | |
8efc0c15 | 413 | } |
414 | ||
5260325f | 415 | int |
416 | read_yes_or_no(const char *prompt, int defval) | |
8efc0c15 | 417 | { |
5260325f | 418 | char buf[1024]; |
419 | FILE *f; | |
420 | int retval = -1; | |
421 | ||
422 | if (isatty(0)) | |
423 | f = stdin; | |
424 | else | |
425 | f = fopen("/dev/tty", "rw"); | |
426 | ||
427 | if (f == NULL) | |
428 | return 0; | |
429 | ||
430 | fflush(stdout); | |
431 | ||
432 | while (1) { | |
433 | fprintf(stderr, "%s", prompt); | |
434 | if (fgets(buf, sizeof(buf), f) == NULL) { | |
435 | /* Print a newline (the prompt probably didn\'t have one). */ | |
436 | fprintf(stderr, "\n"); | |
437 | strlcpy(buf, "no", sizeof buf); | |
438 | } | |
439 | /* Remove newline from response. */ | |
440 | if (strchr(buf, '\n')) | |
441 | *strchr(buf, '\n') = 0; | |
442 | ||
443 | if (buf[0] == 0) | |
444 | retval = defval; | |
445 | if (strcmp(buf, "yes") == 0) | |
446 | retval = 1; | |
447 | if (strcmp(buf, "no") == 0) | |
448 | retval = 0; | |
449 | ||
450 | if (retval != -1) { | |
451 | if (f != stdin) | |
452 | fclose(f); | |
453 | return retval; | |
454 | } | |
8efc0c15 | 455 | } |
8efc0c15 | 456 | } |
457 | ||
5260325f | 458 | /* |
95f1eccc | 459 | * check whether the supplied host key is valid, return only if ok. |
5260325f | 460 | */ |
95f1eccc | 461 | |
5260325f | 462 | void |
a306f2dd | 463 | check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key, |
464 | const char *user_hostfile, const char *system_hostfile) | |
8efc0c15 | 465 | { |
4fe2af09 | 466 | Key *file_key; |
1d1ffb87 | 467 | char *type = key_type(host_key); |
95f1eccc | 468 | char *ip = NULL; |
5260325f | 469 | char hostline[1000], *hostp; |
5260325f | 470 | HostStatus host_status; |
471 | HostStatus ip_status; | |
48e671d5 | 472 | int local = 0, host_ip_differ = 0; |
20af321f | 473 | int salen; |
48e671d5 | 474 | char ntop[NI_MAXHOST]; |
475 | ||
476 | /* | |
477 | * Force accepting of the host key for loopback/localhost. The | |
478 | * problem is that if the home directory is NFS-mounted to multiple | |
479 | * machines, localhost will refer to a different machine in each of | |
480 | * them, and the user will get bogus HOST_CHANGED warnings. This | |
481 | * essentially disables host authentication for localhost; however, | |
482 | * this is probably not a real problem. | |
483 | */ | |
a306f2dd | 484 | /** hostaddr == 0! */ |
48e671d5 | 485 | switch (hostaddr->sa_family) { |
486 | case AF_INET: | |
487 | local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET; | |
20af321f | 488 | salen = sizeof(struct sockaddr_in); |
48e671d5 | 489 | break; |
490 | case AF_INET6: | |
491 | local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr)); | |
20af321f | 492 | salen = sizeof(struct sockaddr_in6); |
48e671d5 | 493 | break; |
494 | default: | |
495 | local = 0; | |
20af321f | 496 | salen = sizeof(struct sockaddr_storage); |
48e671d5 | 497 | break; |
498 | } | |
499 | if (local) { | |
500 | debug("Forcing accepting of host key for loopback/localhost."); | |
501 | return; | |
502 | } | |
5260325f | 503 | |
57112b5a | 504 | /* |
505 | * Turn off check_host_ip for proxy connects, since | |
506 | * we don't have the remote ip-address | |
507 | */ | |
508 | if (options.proxy_command != NULL && options.check_host_ip) | |
509 | options.check_host_ip = 0; | |
510 | ||
48e671d5 | 511 | if (options.check_host_ip) { |
20af321f | 512 | if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop), |
48e671d5 | 513 | NULL, 0, NI_NUMERICHOST) != 0) |
514 | fatal("check_host_key: getnameinfo failed"); | |
515 | ip = xstrdup(ntop); | |
516 | } | |
5260325f | 517 | |
95f1eccc | 518 | /* |
519 | * Store the host key from the known host file in here so that we can | |
520 | * compare it with the key for the IP address. | |
521 | */ | |
4fe2af09 | 522 | file_key = key_new(host_key->type); |
5260325f | 523 | |
aa3378df | 524 | /* |
525 | * Check if the host key is present in the user\'s list of known | |
526 | * hosts or in the systemwide list. | |
527 | */ | |
a306f2dd | 528 | host_status = check_host_in_hostfile(user_hostfile, host, host_key, file_key); |
5260325f | 529 | if (host_status == HOST_NEW) |
a306f2dd | 530 | host_status = check_host_in_hostfile(system_hostfile, host, host_key, file_key); |
aa3378df | 531 | /* |
532 | * Also perform check for the ip address, skip the check if we are | |
533 | * localhost or the hostname was an ip address to begin with | |
534 | */ | |
5260325f | 535 | if (options.check_host_ip && !local && strcmp(host, ip)) { |
4fe2af09 | 536 | Key *ip_key = key_new(host_key->type); |
a306f2dd | 537 | ip_status = check_host_in_hostfile(user_hostfile, ip, host_key, ip_key); |
5260325f | 538 | |
539 | if (ip_status == HOST_NEW) | |
a306f2dd | 540 | ip_status = check_host_in_hostfile(system_hostfile, ip, host_key, ip_key); |
5260325f | 541 | if (host_status == HOST_CHANGED && |
4fe2af09 | 542 | (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key))) |
5260325f | 543 | host_ip_differ = 1; |
544 | ||
4fe2af09 | 545 | key_free(ip_key); |
5260325f | 546 | } else |
547 | ip_status = host_status; | |
548 | ||
4fe2af09 | 549 | key_free(file_key); |
5260325f | 550 | |
551 | switch (host_status) { | |
552 | case HOST_OK: | |
553 | /* The host is known and the key matches. */ | |
1d1ffb87 | 554 | debug("Host '%.200s' is known and matches the %s host key.", |
555 | host, type); | |
5260325f | 556 | if (options.check_host_ip) { |
557 | if (ip_status == HOST_NEW) { | |
a306f2dd | 558 | if (!add_host_to_hostfile(user_hostfile, ip, host_key)) |
1d1ffb87 | 559 | log("Failed to add the %s host key for IP address '%.30s' to the list of known hosts (%.30s).", |
560 | type, ip, user_hostfile); | |
5260325f | 561 | else |
1d1ffb87 | 562 | log("Warning: Permanently added the %s host key for IP address '%.30s' to the list of known hosts.", |
563 | type, ip); | |
5260325f | 564 | } else if (ip_status != HOST_OK) |
1d1ffb87 | 565 | log("Warning: the %s host key for '%.200s' differs from the key for the IP address '%.30s'", |
566 | type, host, ip); | |
5260325f | 567 | } |
568 | break; | |
569 | case HOST_NEW: | |
570 | /* The host is new. */ | |
571 | if (options.strict_host_key_checking == 1) { | |
572 | /* User has requested strict host key checking. We will not add the host key | |
573 | automatically. The only alternative left is to abort. */ | |
1d1ffb87 | 574 | fatal("No %s host key is known for %.200s and you have requested strict checking.", type, host); |
5260325f | 575 | } else if (options.strict_host_key_checking == 2) { |
576 | /* The default */ | |
577 | char prompt[1024]; | |
4fe2af09 | 578 | char *fp = key_fingerprint(host_key); |
5260325f | 579 | snprintf(prompt, sizeof(prompt), |
a408af76 | 580 | "The authenticity of host '%.200s' can't be established.\n" |
1d1ffb87 | 581 | "%s key fingerprint is %s.\n" |
a408af76 | 582 | "Are you sure you want to continue connecting (yes/no)? ", |
1d1ffb87 | 583 | host, type, fp); |
5260325f | 584 | if (!read_yes_or_no(prompt, -1)) |
585 | fatal("Aborted by user!\n"); | |
586 | } | |
587 | if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) { | |
588 | snprintf(hostline, sizeof(hostline), "%s,%s", host, ip); | |
589 | hostp = hostline; | |
590 | } else | |
591 | hostp = host; | |
592 | ||
593 | /* If not in strict mode, add the key automatically to the local known_hosts file. */ | |
a306f2dd | 594 | if (!add_host_to_hostfile(user_hostfile, hostp, host_key)) |
5260325f | 595 | log("Failed to add the host to the list of known hosts (%.500s).", |
a306f2dd | 596 | user_hostfile); |
5260325f | 597 | else |
1d1ffb87 | 598 | log("Warning: Permanently added '%.200s' (%s) to the list of known hosts.", |
599 | hostp, type); | |
5260325f | 600 | break; |
601 | case HOST_CHANGED: | |
602 | if (options.check_host_ip && host_ip_differ) { | |
603 | char *msg; | |
604 | if (ip_status == HOST_NEW) | |
605 | msg = "is unknown"; | |
606 | else if (ip_status == HOST_OK) | |
607 | msg = "is unchanged"; | |
608 | else | |
609 | msg = "has a different value"; | |
610 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
611 | error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @"); | |
612 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
1d1ffb87 | 613 | error("The %s host key for %s has changed,", type, host); |
5260325f | 614 | error("and the key for the according IP address %s", ip); |
615 | error("%s. This could either mean that", msg); | |
616 | error("DNS SPOOFING is happening or the IP address for the host"); | |
617 | error("and its host key have changed at the same time"); | |
618 | } | |
619 | /* The host key has changed. */ | |
620 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
ae2f7af7 | 621 | error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); |
5260325f | 622 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
623 | error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); | |
624 | error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); | |
1d1ffb87 | 625 | error("It is also possible that the %s host key has just been changed.", type); |
5260325f | 626 | error("Please contact your system administrator."); |
627 | error("Add correct host key in %.100s to get rid of this message.", | |
a306f2dd | 628 | user_hostfile); |
5260325f | 629 | |
aa3378df | 630 | /* |
631 | * If strict host key checking is in use, the user will have | |
632 | * to edit the key manually and we can only abort. | |
633 | */ | |
5260325f | 634 | if (options.strict_host_key_checking) |
1d1ffb87 | 635 | fatal("%s host key for %.200s has changed and you have requested strict checking.", type, host); |
5260325f | 636 | |
aa3378df | 637 | /* |
638 | * If strict host key checking has not been requested, allow | |
639 | * the connection but without password authentication or | |
640 | * agent forwarding. | |
641 | */ | |
5260325f | 642 | if (options.password_authentication) { |
643 | error("Password authentication is disabled to avoid trojan horses."); | |
644 | options.password_authentication = 0; | |
645 | } | |
646 | if (options.forward_agent) { | |
647 | error("Agent forwarding is disabled to avoid trojan horses."); | |
648 | options.forward_agent = 0; | |
649 | } | |
aa3378df | 650 | /* |
651 | * XXX Should permit the user to change to use the new id. | |
652 | * This could be done by converting the host key to an | |
653 | * identifying sentence, tell that the host identifies itself | |
654 | * by that sentence, and ask the user if he/she whishes to | |
655 | * accept the authentication. | |
656 | */ | |
5260325f | 657 | break; |
658 | } | |
5260325f | 659 | if (options.check_host_ip) |
660 | xfree(ip); | |
95f1eccc | 661 | } |
662 | ||
8ce64345 | 663 | /* |
a306f2dd | 664 | * Starts a dialog with the server, and authenticates the current user on the |
665 | * server. This does not need any extra privileges. The basic connection | |
666 | * to the server must already have been established before this is called. | |
667 | * If login fails, this function prints an error and never returns. | |
668 | * This function does not require super-user privileges. | |
8ce64345 | 669 | */ |
670 | void | |
a306f2dd | 671 | ssh_login(int host_key_valid, RSA *own_host_key, const char *orighost, |
672 | struct sockaddr *hostaddr, uid_t original_real_uid) | |
8ce64345 | 673 | { |
8ce64345 | 674 | struct passwd *pw; |
a306f2dd | 675 | char *host, *cp; |
8ce64345 | 676 | char *server_user, *local_user; |
8ce64345 | 677 | |
8ce64345 | 678 | /* Get local user name. Use it as server user if no user name was given. */ |
679 | pw = getpwuid(original_real_uid); | |
680 | if (!pw) | |
14a9a859 | 681 | fatal("User id %u not found from user database.", original_real_uid); |
8ce64345 | 682 | local_user = xstrdup(pw->pw_name); |
683 | server_user = options.user ? options.user : local_user; | |
684 | ||
7b2ea3a1 | 685 | /* Convert the user-supplied hostname into all lowercase. */ |
686 | host = xstrdup(orighost); | |
687 | for (cp = host; *cp; cp++) | |
688 | if (isupper(*cp)) | |
689 | *cp = tolower(*cp); | |
690 | ||
691 | /* Exchange protocol version identification strings with the server. */ | |
692 | ssh_exchange_identification(); | |
693 | ||
694 | /* Put the connection into non-blocking mode. */ | |
695 | packet_set_nonblocking(); | |
696 | ||
7b2ea3a1 | 697 | /* key exchange */ |
7b2ea3a1 | 698 | /* authenticate user */ |
8ce64345 | 699 | if (compat20) { |
700 | ssh_kex2(host, hostaddr); | |
a306f2dd | 701 | ssh_userauth2(server_user, host); |
8ce64345 | 702 | } else { |
8ce64345 | 703 | ssh_kex(host, hostaddr); |
a306f2dd | 704 | ssh_userauth(local_user, server_user, host, host_key_valid, own_host_key); |
8ce64345 | 705 | } |
7b2ea3a1 | 706 | } |