]>
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 | |
5 | * Created: Sat Mar 18 22:15:47 1995 ylo | |
6 | * Code to connect to a remote host, and to perform the client side of the | |
7 | * login (authentication) dialog. | |
8 | */ | |
8efc0c15 | 9 | |
10 | #include "includes.h" | |
610cd5c6 | 11 | RCSID("$OpenBSD: sshconnect.c,v 1.57 2000/03/16 20:56:14 markus Exp $"); |
8efc0c15 | 12 | |
5881cd60 | 13 | #ifdef HAVE_OPENSSL |
8efc0c15 | 14 | #include <openssl/bn.h> |
5881cd60 | 15 | #include <openssl/md5.h> |
16 | #endif | |
17 | #ifdef HAVE_SSL | |
18 | #include <ssl/bn.h> | |
19 | #include <ssl/md5.h> | |
20 | #endif | |
21 | ||
8efc0c15 | 22 | #include "xmalloc.h" |
23 | #include "rsa.h" | |
24 | #include "ssh.h" | |
25 | #include "packet.h" | |
26 | #include "authfd.h" | |
27 | #include "cipher.h" | |
28 | #include "mpaux.h" | |
29 | #include "uidswap.h" | |
30 | #include "compat.h" | |
6fa724bc | 31 | #include "readconf.h" |
2bd61362 | 32 | #include "fingerprint.h" |
8efc0c15 | 33 | |
34 | /* Session id for the current session. */ | |
35 | unsigned char session_id[16]; | |
36 | ||
7b2ea3a1 | 37 | /* authentications supported by server */ |
38 | unsigned int supported_authentications; | |
39 | ||
57112b5a | 40 | extern Options options; |
48e671d5 | 41 | extern char *__progname; |
57112b5a | 42 | |
5260325f | 43 | /* |
44 | * Connect to the given ssh server using a proxy command. | |
45 | */ | |
8efc0c15 | 46 | int |
57112b5a | 47 | ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid, |
8efc0c15 | 48 | const char *proxy_command) |
49 | { | |
5260325f | 50 | Buffer command; |
51 | const char *cp; | |
52 | char *command_string; | |
53 | int pin[2], pout[2]; | |
54 | int pid; | |
48e671d5 | 55 | char strport[NI_MAXSERV]; |
5260325f | 56 | |
57 | /* Convert the port number into a string. */ | |
48e671d5 | 58 | snprintf(strport, sizeof strport, "%hu", port); |
5260325f | 59 | |
60 | /* Build the final command string in the buffer by making the | |
61 | appropriate substitutions to the given proxy command. */ | |
62 | buffer_init(&command); | |
63 | for (cp = proxy_command; *cp; cp++) { | |
64 | if (cp[0] == '%' && cp[1] == '%') { | |
65 | buffer_append(&command, "%", 1); | |
66 | cp++; | |
67 | continue; | |
68 | } | |
69 | if (cp[0] == '%' && cp[1] == 'h') { | |
70 | buffer_append(&command, host, strlen(host)); | |
71 | cp++; | |
72 | continue; | |
73 | } | |
74 | if (cp[0] == '%' && cp[1] == 'p') { | |
48e671d5 | 75 | buffer_append(&command, strport, strlen(strport)); |
5260325f | 76 | cp++; |
77 | continue; | |
78 | } | |
79 | buffer_append(&command, cp, 1); | |
8efc0c15 | 80 | } |
5260325f | 81 | buffer_append(&command, "\0", 1); |
82 | ||
83 | /* Get the final command string. */ | |
84 | command_string = buffer_ptr(&command); | |
85 | ||
86 | /* Create pipes for communicating with the proxy. */ | |
87 | if (pipe(pin) < 0 || pipe(pout) < 0) | |
88 | fatal("Could not create pipes to communicate with the proxy: %.100s", | |
89 | strerror(errno)); | |
90 | ||
91 | debug("Executing proxy command: %.500s", command_string); | |
92 | ||
93 | /* Fork and execute the proxy command. */ | |
94 | if ((pid = fork()) == 0) { | |
95 | char *argv[10]; | |
96 | ||
97 | /* Child. Permanently give up superuser privileges. */ | |
98 | permanently_set_uid(original_real_uid); | |
99 | ||
100 | /* Redirect stdin and stdout. */ | |
101 | close(pin[1]); | |
102 | if (pin[0] != 0) { | |
103 | if (dup2(pin[0], 0) < 0) | |
104 | perror("dup2 stdin"); | |
105 | close(pin[0]); | |
106 | } | |
107 | close(pout[0]); | |
108 | if (dup2(pout[1], 1) < 0) | |
109 | perror("dup2 stdout"); | |
110 | /* Cannot be 1 because pin allocated two descriptors. */ | |
111 | close(pout[1]); | |
112 | ||
113 | /* Stderr is left as it is so that error messages get | |
114 | printed on the user's terminal. */ | |
115 | argv[0] = "/bin/sh"; | |
116 | argv[1] = "-c"; | |
117 | argv[2] = command_string; | |
118 | argv[3] = NULL; | |
119 | ||
120 | /* Execute the proxy command. Note that we gave up any | |
121 | extra privileges above. */ | |
122 | execv("/bin/sh", argv); | |
123 | perror("/bin/sh"); | |
124 | exit(1); | |
8efc0c15 | 125 | } |
5260325f | 126 | /* Parent. */ |
127 | if (pid < 0) | |
128 | fatal("fork failed: %.100s", strerror(errno)); | |
129 | ||
130 | /* Close child side of the descriptors. */ | |
131 | close(pin[0]); | |
132 | close(pout[1]); | |
133 | ||
134 | /* Free the command name. */ | |
135 | buffer_free(&command); | |
136 | ||
137 | /* Set the connection file descriptors. */ | |
138 | packet_set_connection(pout[0], pin[1]); | |
8efc0c15 | 139 | |
5260325f | 140 | return 1; |
141 | } | |
8efc0c15 | 142 | |
5260325f | 143 | /* |
144 | * Creates a (possibly privileged) socket for use as the ssh connection. | |
145 | */ | |
146 | int | |
48e671d5 | 147 | ssh_create_socket(uid_t original_real_uid, int privileged, int family) |
8efc0c15 | 148 | { |
5260325f | 149 | int sock; |
150 | ||
aa3378df | 151 | /* |
152 | * If we are running as root and want to connect to a privileged | |
153 | * port, bind our own socket to a privileged port. | |
154 | */ | |
5260325f | 155 | if (privileged) { |
156 | int p = IPPORT_RESERVED - 1; | |
48e671d5 | 157 | sock = rresvport_af(&p, family); |
5260325f | 158 | if (sock < 0) |
c8d54615 | 159 | error("rresvport: af=%d %.100s", family, strerror(errno)); |
160 | else | |
161 | debug("Allocated local port %d.", p); | |
5260325f | 162 | } else { |
95f1eccc | 163 | /* |
164 | * Just create an ordinary socket on arbitrary port. We use | |
165 | * the user's uid to create the socket. | |
166 | */ | |
5260325f | 167 | temporarily_use_uid(original_real_uid); |
48e671d5 | 168 | sock = socket(family, SOCK_STREAM, 0); |
5260325f | 169 | if (sock < 0) |
48e671d5 | 170 | error("socket: %.100s", strerror(errno)); |
5260325f | 171 | restore_uid(); |
172 | } | |
173 | return sock; | |
8efc0c15 | 174 | } |
175 | ||
5260325f | 176 | /* |
48e671d5 | 177 | * Opens a TCP/IP connection to the remote server on the given host. |
178 | * The address of the remote host will be returned in hostaddr. | |
179 | * If port is 0, the default port will be used. If anonymous is zero, | |
5260325f | 180 | * a privileged port will be allocated to make the connection. |
181 | * This requires super-user privileges if anonymous is false. | |
182 | * Connection_attempts specifies the maximum number of tries (one per | |
183 | * second). If proxy_command is non-NULL, it specifies the command (with %h | |
184 | * and %p substituted for host and port, respectively) to use to contact | |
185 | * the daemon. | |
186 | */ | |
187 | int | |
48e671d5 | 188 | ssh_connect(const char *host, struct sockaddr_storage * hostaddr, |
57112b5a | 189 | u_short port, int connection_attempts, |
5260325f | 190 | int anonymous, uid_t original_real_uid, |
191 | const char *proxy_command) | |
8efc0c15 | 192 | { |
48e671d5 | 193 | int sock = -1, attempt; |
5260325f | 194 | struct servent *sp; |
48e671d5 | 195 | struct addrinfo hints, *ai, *aitop; |
196 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; | |
197 | int gaierr; | |
5260325f | 198 | struct linger linger; |
199 | ||
200 | debug("ssh_connect: getuid %d geteuid %d anon %d", | |
201 | (int) getuid(), (int) geteuid(), anonymous); | |
202 | ||
203 | /* Get default port if port has not been set. */ | |
204 | if (port == 0) { | |
205 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); | |
206 | if (sp) | |
207 | port = ntohs(sp->s_port); | |
208 | else | |
209 | port = SSH_DEFAULT_PORT; | |
8efc0c15 | 210 | } |
5260325f | 211 | /* If a proxy command is given, connect using it. */ |
212 | if (proxy_command != NULL) | |
213 | return ssh_proxy_connect(host, port, original_real_uid, proxy_command); | |
214 | ||
215 | /* No proxy command. */ | |
216 | ||
48e671d5 | 217 | memset(&hints, 0, sizeof(hints)); |
218 | hints.ai_family = IPv4or6; | |
219 | hints.ai_socktype = SOCK_STREAM; | |
220 | snprintf(strport, sizeof strport, "%d", port); | |
221 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) | |
222 | fatal("%s: %.100s: %s", __progname, host, | |
223 | gai_strerror(gaierr)); | |
5260325f | 224 | |
95f1eccc | 225 | /* |
226 | * Try to connect several times. On some machines, the first time | |
227 | * will sometimes fail. In general socket code appears to behave | |
228 | * quite magically on many machines. | |
229 | */ | |
5260325f | 230 | for (attempt = 0; attempt < connection_attempts; attempt++) { |
231 | if (attempt > 0) | |
232 | debug("Trying again..."); | |
233 | ||
48e671d5 | 234 | /* Loop through addresses for this host, and try each one in |
235 | sequence until the connection succeeds. */ | |
236 | for (ai = aitop; ai; ai = ai->ai_next) { | |
237 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) | |
238 | continue; | |
239 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, | |
240 | ntop, sizeof(ntop), strport, sizeof(strport), | |
241 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { | |
242 | error("ssh_connect: getnameinfo failed"); | |
243 | continue; | |
244 | } | |
245 | debug("Connecting to %.200s [%.100s] port %s.", | |
246 | host, ntop, strport); | |
247 | ||
248 | /* Create a socket for connecting. */ | |
249 | sock = ssh_create_socket(original_real_uid, | |
250 | !anonymous && geteuid() == 0 && port < IPPORT_RESERVED, | |
251 | ai->ai_family); | |
252 | if (sock < 0) | |
253 | continue; | |
254 | ||
255 | /* Connect to the host. We use the user's uid in the | |
256 | * hope that it will help with tcp_wrappers showing | |
257 | * the remote uid as root. | |
aa3378df | 258 | */ |
5260325f | 259 | temporarily_use_uid(original_real_uid); |
48e671d5 | 260 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) { |
261 | /* Successful connection. */ | |
262 | memcpy(hostaddr, ai->ai_addr, sizeof(*(ai->ai_addr))); | |
5260325f | 263 | restore_uid(); |
264 | break; | |
48e671d5 | 265 | } else { |
5260325f | 266 | debug("connect: %.100s", strerror(errno)); |
267 | restore_uid(); | |
aa3378df | 268 | /* |
269 | * Close the failed socket; there appear to | |
270 | * be some problems when reusing a socket for | |
271 | * which connect() has already returned an | |
272 | * error. | |
273 | */ | |
5260325f | 274 | shutdown(sock, SHUT_RDWR); |
275 | close(sock); | |
276 | } | |
8efc0c15 | 277 | } |
48e671d5 | 278 | if (ai) |
279 | break; /* Successful connection. */ | |
8efc0c15 | 280 | |
5260325f | 281 | /* Sleep a moment before retrying. */ |
282 | sleep(1); | |
283 | } | |
48e671d5 | 284 | |
285 | freeaddrinfo(aitop); | |
286 | ||
5260325f | 287 | /* Return failure if we didn't get a successful connection. */ |
288 | if (attempt >= connection_attempts) | |
289 | return 0; | |
8efc0c15 | 290 | |
5260325f | 291 | debug("Connection established."); |
8efc0c15 | 292 | |
aa3378df | 293 | /* |
294 | * Set socket options. We would like the socket to disappear as soon | |
295 | * as it has been closed for whatever reason. | |
296 | */ | |
297 | /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ | |
5260325f | 298 | linger.l_onoff = 1; |
299 | linger.l_linger = 5; | |
300 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); | |
8efc0c15 | 301 | |
5260325f | 302 | /* Set the connection. */ |
303 | packet_set_connection(sock, sock); | |
8efc0c15 | 304 | |
5260325f | 305 | return 1; |
8efc0c15 | 306 | } |
307 | ||
5260325f | 308 | /* |
309 | * Checks if the user has an authentication agent, and if so, tries to | |
310 | * authenticate using the agent. | |
311 | */ | |
8efc0c15 | 312 | int |
313 | try_agent_authentication() | |
314 | { | |
5260325f | 315 | int status, type; |
316 | char *comment; | |
317 | AuthenticationConnection *auth; | |
318 | unsigned char response[16]; | |
319 | unsigned int i; | |
320 | BIGNUM *e, *n, *challenge; | |
321 | ||
322 | /* Get connection to the agent. */ | |
323 | auth = ssh_get_authentication_connection(); | |
324 | if (!auth) | |
325 | return 0; | |
326 | ||
327 | e = BN_new(); | |
328 | n = BN_new(); | |
329 | challenge = BN_new(); | |
330 | ||
331 | /* Loop through identities served by the agent. */ | |
332 | for (status = ssh_get_first_identity(auth, e, n, &comment); | |
333 | status; | |
334 | status = ssh_get_next_identity(auth, e, n, &comment)) { | |
335 | int plen, clen; | |
336 | ||
337 | /* Try this identity. */ | |
338 | debug("Trying RSA authentication via agent with '%.100s'", comment); | |
339 | xfree(comment); | |
340 | ||
341 | /* Tell the server that we are willing to authenticate using this key. */ | |
342 | packet_start(SSH_CMSG_AUTH_RSA); | |
343 | packet_put_bignum(n); | |
344 | packet_send(); | |
345 | packet_write_wait(); | |
346 | ||
347 | /* Wait for server's response. */ | |
348 | type = packet_read(&plen); | |
349 | ||
350 | /* The server sends failure if it doesn\'t like our key or | |
351 | does not support RSA authentication. */ | |
352 | if (type == SSH_SMSG_FAILURE) { | |
353 | debug("Server refused our key."); | |
354 | continue; | |
355 | } | |
356 | /* Otherwise it should have sent a challenge. */ | |
357 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
358 | packet_disconnect("Protocol error during RSA authentication: %d", | |
359 | type); | |
8efc0c15 | 360 | |
5260325f | 361 | packet_get_bignum(challenge, &clen); |
8efc0c15 | 362 | |
5260325f | 363 | packet_integrity_check(plen, clen, type); |
8efc0c15 | 364 | |
5260325f | 365 | debug("Received RSA challenge from server."); |
366 | ||
367 | /* Ask the agent to decrypt the challenge. */ | |
368 | if (!ssh_decrypt_challenge(auth, e, n, challenge, | |
369 | session_id, 1, response)) { | |
370 | /* The agent failed to authenticate this identifier although it | |
371 | advertised it supports this. Just return a wrong value. */ | |
372 | log("Authentication agent failed to decrypt challenge."); | |
373 | memset(response, 0, sizeof(response)); | |
374 | } | |
375 | debug("Sending response to RSA challenge."); | |
376 | ||
377 | /* Send the decrypted challenge back to the server. */ | |
378 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
379 | for (i = 0; i < 16; i++) | |
380 | packet_put_char(response[i]); | |
381 | packet_send(); | |
382 | packet_write_wait(); | |
383 | ||
384 | /* Wait for response from the server. */ | |
385 | type = packet_read(&plen); | |
386 | ||
387 | /* The server returns success if it accepted the authentication. */ | |
388 | if (type == SSH_SMSG_SUCCESS) { | |
389 | debug("RSA authentication accepted by server."); | |
390 | BN_clear_free(e); | |
391 | BN_clear_free(n); | |
392 | BN_clear_free(challenge); | |
393 | return 1; | |
394 | } | |
395 | /* Otherwise it should return failure. */ | |
396 | if (type != SSH_SMSG_FAILURE) | |
397 | packet_disconnect("Protocol error waiting RSA auth response: %d", | |
398 | type); | |
399 | } | |
400 | ||
401 | BN_clear_free(e); | |
402 | BN_clear_free(n); | |
403 | BN_clear_free(challenge); | |
8efc0c15 | 404 | |
5260325f | 405 | debug("RSA authentication using agent refused."); |
406 | return 0; | |
407 | } | |
8efc0c15 | 408 | |
5260325f | 409 | /* |
410 | * Computes the proper response to a RSA challenge, and sends the response to | |
411 | * the server. | |
412 | */ | |
8efc0c15 | 413 | void |
5260325f | 414 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
8efc0c15 | 415 | { |
5260325f | 416 | unsigned char buf[32], response[16]; |
417 | MD5_CTX md; | |
418 | int i, len; | |
419 | ||
420 | /* Decrypt the challenge using the private key. */ | |
421 | rsa_private_decrypt(challenge, challenge, prv); | |
422 | ||
423 | /* Compute the response. */ | |
424 | /* The response is MD5 of decrypted challenge plus session id. */ | |
425 | len = BN_num_bytes(challenge); | |
426 | if (len <= 0 || len > sizeof(buf)) | |
427 | packet_disconnect("respond_to_rsa_challenge: bad challenge length %d", | |
428 | len); | |
429 | ||
430 | memset(buf, 0, sizeof(buf)); | |
431 | BN_bn2bin(challenge, buf + sizeof(buf) - len); | |
432 | MD5_Init(&md); | |
433 | MD5_Update(&md, buf, 32); | |
434 | MD5_Update(&md, session_id, 16); | |
435 | MD5_Final(response, &md); | |
436 | ||
437 | debug("Sending response to host key RSA challenge."); | |
438 | ||
439 | /* Send the response back to the server. */ | |
440 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
441 | for (i = 0; i < 16; i++) | |
442 | packet_put_char(response[i]); | |
443 | packet_send(); | |
444 | packet_write_wait(); | |
8efc0c15 | 445 | |
5260325f | 446 | memset(buf, 0, sizeof(buf)); |
447 | memset(response, 0, sizeof(response)); | |
448 | memset(&md, 0, sizeof(md)); | |
449 | } | |
8efc0c15 | 450 | |
5260325f | 451 | /* |
452 | * Checks if the user has authentication file, and if so, tries to authenticate | |
453 | * the user using it. | |
454 | */ | |
8efc0c15 | 455 | int |
57112b5a | 456 | try_rsa_authentication(const char *authfile) |
8efc0c15 | 457 | { |
5260325f | 458 | BIGNUM *challenge; |
459 | RSA *private_key; | |
460 | RSA *public_key; | |
461 | char *passphrase, *comment; | |
462 | int type, i; | |
463 | int plen, clen; | |
464 | ||
465 | /* Try to load identification for the authentication key. */ | |
466 | public_key = RSA_new(); | |
467 | if (!load_public_key(authfile, public_key, &comment)) { | |
468 | RSA_free(public_key); | |
57112b5a | 469 | /* Could not load it. Fail. */ |
470 | return 0; | |
8efc0c15 | 471 | } |
5260325f | 472 | debug("Trying RSA authentication with key '%.100s'", comment); |
473 | ||
474 | /* Tell the server that we are willing to authenticate using this key. */ | |
475 | packet_start(SSH_CMSG_AUTH_RSA); | |
476 | packet_put_bignum(public_key->n); | |
477 | packet_send(); | |
478 | packet_write_wait(); | |
479 | ||
480 | /* We no longer need the public key. */ | |
481 | RSA_free(public_key); | |
482 | ||
483 | /* Wait for server's response. */ | |
484 | type = packet_read(&plen); | |
485 | ||
aa3378df | 486 | /* |
487 | * The server responds with failure if it doesn\'t like our key or | |
488 | * doesn\'t support RSA authentication. | |
489 | */ | |
5260325f | 490 | if (type == SSH_SMSG_FAILURE) { |
491 | debug("Server refused our key."); | |
492 | xfree(comment); | |
57112b5a | 493 | return 0; |
8efc0c15 | 494 | } |
5260325f | 495 | /* Otherwise, the server should respond with a challenge. */ |
496 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
497 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
498 | ||
499 | /* Get the challenge from the packet. */ | |
500 | challenge = BN_new(); | |
501 | packet_get_bignum(challenge, &clen); | |
502 | ||
503 | packet_integrity_check(plen, clen, type); | |
504 | ||
505 | debug("Received RSA challenge from server."); | |
506 | ||
507 | private_key = RSA_new(); | |
aa3378df | 508 | /* |
509 | * Load the private key. Try first with empty passphrase; if it | |
510 | * fails, ask for a passphrase. | |
511 | */ | |
5260325f | 512 | if (!load_private_key(authfile, "", private_key, NULL)) { |
513 | char buf[300]; | |
514 | snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ", | |
a408af76 | 515 | comment); |
5260325f | 516 | if (!options.batch_mode) |
517 | passphrase = read_passphrase(buf, 0); | |
518 | else { | |
519 | debug("Will not query passphrase for %.100s in batch mode.", | |
520 | comment); | |
521 | passphrase = xstrdup(""); | |
522 | } | |
8efc0c15 | 523 | |
5260325f | 524 | /* Load the authentication file using the pasphrase. */ |
525 | if (!load_private_key(authfile, passphrase, private_key, NULL)) { | |
526 | memset(passphrase, 0, strlen(passphrase)); | |
527 | xfree(passphrase); | |
528 | error("Bad passphrase."); | |
529 | ||
530 | /* Send a dummy response packet to avoid protocol error. */ | |
531 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
532 | for (i = 0; i < 16; i++) | |
533 | packet_put_char(0); | |
534 | packet_send(); | |
535 | packet_write_wait(); | |
536 | ||
537 | /* Expect the server to reject it... */ | |
538 | packet_read_expect(&plen, SSH_SMSG_FAILURE); | |
539 | xfree(comment); | |
540 | return 0; | |
541 | } | |
542 | /* Destroy the passphrase. */ | |
543 | memset(passphrase, 0, strlen(passphrase)); | |
544 | xfree(passphrase); | |
545 | } | |
546 | /* We no longer need the comment. */ | |
547 | xfree(comment); | |
548 | ||
549 | /* Compute and send a response to the challenge. */ | |
550 | respond_to_rsa_challenge(challenge, private_key); | |
551 | ||
552 | /* Destroy the private key. */ | |
553 | RSA_free(private_key); | |
8efc0c15 | 554 | |
5260325f | 555 | /* We no longer need the challenge. */ |
556 | BN_clear_free(challenge); | |
557 | ||
558 | /* Wait for response from the server. */ | |
559 | type = packet_read(&plen); | |
560 | if (type == SSH_SMSG_SUCCESS) { | |
561 | debug("RSA authentication accepted by server."); | |
562 | return 1; | |
563 | } | |
564 | if (type != SSH_SMSG_FAILURE) | |
565 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
566 | debug("RSA authentication refused."); | |
567 | return 0; | |
568 | } | |
8efc0c15 | 569 | |
5260325f | 570 | /* |
571 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv | |
572 | * authentication and RSA host authentication. | |
573 | */ | |
8efc0c15 | 574 | int |
5260325f | 575 | try_rhosts_rsa_authentication(const char *local_user, RSA * host_key) |
8efc0c15 | 576 | { |
5260325f | 577 | int type; |
578 | BIGNUM *challenge; | |
579 | int plen, clen; | |
580 | ||
581 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); | |
582 | ||
583 | /* Tell the server that we are willing to authenticate using this key. */ | |
584 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); | |
585 | packet_put_string(local_user, strlen(local_user)); | |
586 | packet_put_int(BN_num_bits(host_key->n)); | |
587 | packet_put_bignum(host_key->e); | |
588 | packet_put_bignum(host_key->n); | |
589 | packet_send(); | |
590 | packet_write_wait(); | |
591 | ||
592 | /* Wait for server's response. */ | |
593 | type = packet_read(&plen); | |
594 | ||
595 | /* The server responds with failure if it doesn't admit our | |
596 | .rhosts authentication or doesn't know our host key. */ | |
597 | if (type == SSH_SMSG_FAILURE) { | |
598 | debug("Server refused our rhosts authentication or host key."); | |
599 | return 0; | |
600 | } | |
601 | /* Otherwise, the server should respond with a challenge. */ | |
602 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
603 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
604 | ||
605 | /* Get the challenge from the packet. */ | |
606 | challenge = BN_new(); | |
607 | packet_get_bignum(challenge, &clen); | |
608 | ||
609 | packet_integrity_check(plen, clen, type); | |
610 | ||
611 | debug("Received RSA challenge for host key from server."); | |
612 | ||
613 | /* Compute a response to the challenge. */ | |
614 | respond_to_rsa_challenge(challenge, host_key); | |
615 | ||
616 | /* We no longer need the challenge. */ | |
617 | BN_clear_free(challenge); | |
618 | ||
619 | /* Wait for response from the server. */ | |
620 | type = packet_read(&plen); | |
621 | if (type == SSH_SMSG_SUCCESS) { | |
622 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); | |
623 | return 1; | |
624 | } | |
625 | if (type != SSH_SMSG_FAILURE) | |
626 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
627 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); | |
628 | return 0; | |
8efc0c15 | 629 | } |
630 | ||
631 | #ifdef KRB4 | |
5260325f | 632 | int |
633 | try_kerberos_authentication() | |
8efc0c15 | 634 | { |
5260325f | 635 | KTEXT_ST auth; /* Kerberos data */ |
636 | char *reply; | |
637 | char inst[INST_SZ]; | |
638 | char *realm; | |
639 | CREDENTIALS cred; | |
640 | int r, type, plen; | |
610cd5c6 | 641 | socklen_t slen; |
5260325f | 642 | Key_schedule schedule; |
643 | u_long checksum, cksum; | |
644 | MSG_DAT msg_data; | |
645 | struct sockaddr_in local, foreign; | |
646 | struct stat st; | |
647 | ||
648 | /* Don't do anything if we don't have any tickets. */ | |
649 | if (stat(tkt_string(), &st) < 0) | |
650 | return 0; | |
651 | ||
652 | strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ); | |
653 | ||
654 | realm = (char *) krb_realmofhost(get_canonical_hostname()); | |
655 | if (!realm) { | |
656 | debug("Kerberos V4: no realm for %s", get_canonical_hostname()); | |
657 | return 0; | |
658 | } | |
659 | /* This can really be anything. */ | |
660 | checksum = (u_long) getpid(); | |
661 | ||
662 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); | |
663 | if (r != KSUCCESS) { | |
664 | debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]); | |
665 | return 0; | |
666 | } | |
667 | /* Get session key to decrypt the server's reply with. */ | |
668 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); | |
669 | if (r != KSUCCESS) { | |
670 | debug("get_cred failed: %s", krb_err_txt[r]); | |
671 | return 0; | |
672 | } | |
673 | des_key_sched((des_cblock *) cred.session, schedule); | |
674 | ||
675 | /* Send authentication info to server. */ | |
676 | packet_start(SSH_CMSG_AUTH_KERBEROS); | |
677 | packet_put_string((char *) auth.dat, auth.length); | |
678 | packet_send(); | |
679 | packet_write_wait(); | |
680 | ||
681 | /* Zero the buffer. */ | |
682 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); | |
683 | ||
610cd5c6 | 684 | slen = sizeof(local); |
5260325f | 685 | memset(&local, 0, sizeof(local)); |
686 | if (getsockname(packet_get_connection_in(), | |
610cd5c6 | 687 | (struct sockaddr *) & local, &slen) < 0) |
5260325f | 688 | debug("getsockname failed: %s", strerror(errno)); |
689 | ||
610cd5c6 | 690 | slen = sizeof(foreign); |
5260325f | 691 | memset(&foreign, 0, sizeof(foreign)); |
692 | if (getpeername(packet_get_connection_in(), | |
610cd5c6 | 693 | (struct sockaddr *) & foreign, &slen) < 0) { |
5260325f | 694 | debug("getpeername failed: %s", strerror(errno)); |
695 | fatal_cleanup(); | |
696 | } | |
697 | /* Get server reply. */ | |
698 | type = packet_read(&plen); | |
699 | switch (type) { | |
700 | case SSH_SMSG_FAILURE: | |
701 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
702 | debug("Kerberos V4 authentication failed."); | |
703 | return 0; | |
704 | break; | |
705 | ||
706 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: | |
707 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ | |
708 | debug("Kerberos V4 authentication accepted."); | |
709 | ||
710 | /* Get server's response. */ | |
711 | reply = packet_get_string((unsigned int *) &auth.length); | |
712 | memcpy(auth.dat, reply, auth.length); | |
713 | xfree(reply); | |
714 | ||
715 | packet_integrity_check(plen, 4 + auth.length, type); | |
716 | ||
aa3378df | 717 | /* |
718 | * If his response isn't properly encrypted with the session | |
719 | * key, and the decrypted checksum fails to match, he's | |
720 | * bogus. Bail out. | |
721 | */ | |
5260325f | 722 | r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session, |
723 | &foreign, &local, &msg_data); | |
724 | if (r != KSUCCESS) { | |
725 | debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]); | |
726 | packet_disconnect("Kerberos V4 challenge failed!"); | |
727 | } | |
728 | /* Fetch the (incremented) checksum that we supplied in the request. */ | |
729 | (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum)); | |
730 | cksum = ntohl(cksum); | |
731 | ||
732 | /* If it matches, we're golden. */ | |
733 | if (cksum == checksum + 1) { | |
734 | debug("Kerberos V4 challenge successful."); | |
735 | return 1; | |
736 | } else | |
737 | packet_disconnect("Kerberos V4 challenge failed!"); | |
738 | break; | |
739 | ||
740 | default: | |
741 | packet_disconnect("Protocol error on Kerberos V4 response: %d", type); | |
742 | } | |
743 | return 0; | |
8efc0c15 | 744 | } |
5260325f | 745 | |
8efc0c15 | 746 | #endif /* KRB4 */ |
747 | ||
748 | #ifdef AFS | |
5260325f | 749 | int |
750 | send_kerberos_tgt() | |
8efc0c15 | 751 | { |
5260325f | 752 | CREDENTIALS *creds; |
753 | char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ]; | |
754 | int r, type, plen; | |
610cd5c6 | 755 | char buffer[8192]; |
5260325f | 756 | struct stat st; |
757 | ||
758 | /* Don't do anything if we don't have any tickets. */ | |
759 | if (stat(tkt_string(), &st) < 0) | |
760 | return 0; | |
761 | ||
762 | creds = xmalloc(sizeof(*creds)); | |
763 | ||
764 | if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) { | |
765 | debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]); | |
766 | return 0; | |
767 | } | |
768 | if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) { | |
769 | debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]); | |
770 | return 0; | |
771 | } | |
772 | if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) { | |
773 | debug("Kerberos V4 ticket expired: %s", TKT_FILE); | |
774 | return 0; | |
775 | } | |
610cd5c6 | 776 | creds_to_radix(creds, (unsigned char *)buffer); |
5260325f | 777 | xfree(creds); |
778 | ||
779 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); | |
610cd5c6 | 780 | packet_put_string(buffer, strlen(buffer)); |
5260325f | 781 | packet_send(); |
782 | packet_write_wait(); | |
783 | ||
784 | type = packet_read(&plen); | |
785 | ||
786 | if (type == SSH_SMSG_FAILURE) | |
787 | debug("Kerberos TGT for realm %s rejected.", prealm); | |
788 | else if (type != SSH_SMSG_SUCCESS) | |
789 | packet_disconnect("Protocol error on Kerberos TGT response: %d", type); | |
790 | ||
791 | return 1; | |
8efc0c15 | 792 | } |
793 | ||
5260325f | 794 | void |
795 | send_afs_tokens(void) | |
8efc0c15 | 796 | { |
5260325f | 797 | CREDENTIALS creds; |
798 | struct ViceIoctl parms; | |
799 | struct ClearToken ct; | |
800 | int i, type, len, plen; | |
801 | char buf[2048], *p, *server_cell; | |
610cd5c6 | 802 | char buffer[8192]; |
5260325f | 803 | |
804 | /* Move over ktc_GetToken, here's something leaner. */ | |
805 | for (i = 0; i < 100; i++) { /* just in case */ | |
806 | parms.in = (char *) &i; | |
807 | parms.in_size = sizeof(i); | |
808 | parms.out = buf; | |
809 | parms.out_size = sizeof(buf); | |
810 | if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) | |
811 | break; | |
812 | p = buf; | |
813 | ||
814 | /* Get secret token. */ | |
815 | memcpy(&creds.ticket_st.length, p, sizeof(unsigned int)); | |
816 | if (creds.ticket_st.length > MAX_KTXT_LEN) | |
817 | break; | |
818 | p += sizeof(unsigned int); | |
819 | memcpy(creds.ticket_st.dat, p, creds.ticket_st.length); | |
820 | p += creds.ticket_st.length; | |
821 | ||
822 | /* Get clear token. */ | |
823 | memcpy(&len, p, sizeof(len)); | |
824 | if (len != sizeof(struct ClearToken)) | |
825 | break; | |
826 | p += sizeof(len); | |
827 | memcpy(&ct, p, len); | |
828 | p += len; | |
829 | p += sizeof(len); /* primary flag */ | |
830 | server_cell = p; | |
831 | ||
832 | /* Flesh out our credentials. */ | |
833 | strlcpy(creds.service, "afs", sizeof creds.service); | |
834 | creds.instance[0] = '\0'; | |
835 | strlcpy(creds.realm, server_cell, REALM_SZ); | |
836 | memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ); | |
837 | creds.issue_date = ct.BeginTimestamp; | |
838 | creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp); | |
839 | creds.kvno = ct.AuthHandle; | |
840 | snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId); | |
841 | creds.pinst[0] = '\0'; | |
842 | ||
843 | /* Encode token, ship it off. */ | |
610cd5c6 | 844 | if (!creds_to_radix(&creds, (unsigned char*) buffer)) |
5260325f | 845 | break; |
846 | packet_start(SSH_CMSG_HAVE_AFS_TOKEN); | |
610cd5c6 | 847 | packet_put_string(buffer, strlen(buffer)); |
5260325f | 848 | packet_send(); |
849 | packet_write_wait(); | |
850 | ||
851 | /* Roger, Roger. Clearance, Clarence. What's your vector, | |
852 | Victor? */ | |
853 | type = packet_read(&plen); | |
854 | ||
855 | if (type == SSH_SMSG_FAILURE) | |
856 | debug("AFS token for cell %s rejected.", server_cell); | |
857 | else if (type != SSH_SMSG_SUCCESS) | |
858 | packet_disconnect("Protocol error on AFS token response: %d", type); | |
859 | } | |
8efc0c15 | 860 | } |
8efc0c15 | 861 | |
5260325f | 862 | #endif /* AFS */ |
8efc0c15 | 863 | |
57112b5a | 864 | /* |
865 | * Tries to authenticate with any string-based challenge/response system. | |
866 | * Note that the client code is not tied to s/key or TIS. | |
867 | */ | |
868 | int | |
869 | try_skey_authentication() | |
870 | { | |
610cd5c6 | 871 | int type, i; |
872 | int payload_len; | |
873 | unsigned int clen; | |
57112b5a | 874 | char *challenge, *response; |
875 | ||
876 | debug("Doing skey authentication."); | |
877 | ||
878 | /* request a challenge */ | |
879 | packet_start(SSH_CMSG_AUTH_TIS); | |
880 | packet_send(); | |
881 | packet_write_wait(); | |
882 | ||
883 | type = packet_read(&payload_len); | |
884 | if (type != SSH_SMSG_FAILURE && | |
885 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
886 | packet_disconnect("Protocol error: got %d in response " | |
887 | "to skey-auth", type); | |
888 | } | |
889 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
890 | debug("No challenge for skey authentication."); | |
891 | return 0; | |
892 | } | |
610cd5c6 | 893 | challenge = packet_get_string(&clen); |
894 | packet_integrity_check(payload_len, (4 + clen), type); | |
57112b5a | 895 | if (options.cipher == SSH_CIPHER_NONE) |
896 | log("WARNING: Encryption is disabled! " | |
897 | "Reponse will be transmitted in clear text."); | |
898 | fprintf(stderr, "%s\n", challenge); | |
c8d54615 | 899 | xfree(challenge); |
57112b5a | 900 | fflush(stderr); |
901 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
902 | if (i != 0) | |
903 | error("Permission denied, please try again."); | |
904 | response = read_passphrase("Response: ", 0); | |
905 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); | |
906 | packet_put_string(response, strlen(response)); | |
907 | memset(response, 0, strlen(response)); | |
908 | xfree(response); | |
909 | packet_send(); | |
910 | packet_write_wait(); | |
911 | type = packet_read(&payload_len); | |
912 | if (type == SSH_SMSG_SUCCESS) | |
913 | return 1; | |
914 | if (type != SSH_SMSG_FAILURE) | |
915 | packet_disconnect("Protocol error: got %d in response " | |
916 | "to skey-auth-reponse", type); | |
917 | } | |
918 | /* failure */ | |
919 | return 0; | |
920 | } | |
921 | ||
922 | /* | |
923 | * Tries to authenticate with plain passwd authentication. | |
924 | */ | |
925 | int | |
926 | try_password_authentication(char *prompt) | |
927 | { | |
928 | int type, i, payload_len; | |
929 | char *password; | |
930 | ||
931 | debug("Doing password authentication."); | |
932 | if (options.cipher == SSH_CIPHER_NONE) | |
933 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); | |
934 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
935 | if (i != 0) | |
936 | error("Permission denied, please try again."); | |
937 | password = read_passphrase(prompt, 0); | |
938 | packet_start(SSH_CMSG_AUTH_PASSWORD); | |
939 | packet_put_string(password, strlen(password)); | |
940 | memset(password, 0, strlen(password)); | |
941 | xfree(password); | |
942 | packet_send(); | |
943 | packet_write_wait(); | |
944 | ||
945 | type = packet_read(&payload_len); | |
946 | if (type == SSH_SMSG_SUCCESS) | |
947 | return 1; | |
948 | if (type != SSH_SMSG_FAILURE) | |
949 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); | |
950 | } | |
951 | /* failure */ | |
952 | return 0; | |
953 | } | |
954 | ||
5260325f | 955 | /* |
956 | * Waits for the server identification string, and sends our own | |
957 | * identification string. | |
958 | */ | |
959 | void | |
960 | ssh_exchange_identification() | |
8efc0c15 | 961 | { |
5260325f | 962 | char buf[256], remote_version[256]; /* must be same size! */ |
963 | int remote_major, remote_minor, i; | |
964 | int connection_in = packet_get_connection_in(); | |
965 | int connection_out = packet_get_connection_out(); | |
5260325f | 966 | |
967 | /* Read other side\'s version identification. */ | |
968 | for (i = 0; i < sizeof(buf) - 1; i++) { | |
c8d54615 | 969 | int len = read(connection_in, &buf[i], 1); |
970 | if (len < 0) | |
5260325f | 971 | fatal("ssh_exchange_identification: read: %.100s", strerror(errno)); |
c8d54615 | 972 | if (len != 1) |
973 | fatal("ssh_exchange_identification: Connection closed by remote host"); | |
5260325f | 974 | if (buf[i] == '\r') { |
975 | buf[i] = '\n'; | |
976 | buf[i + 1] = 0; | |
977 | break; | |
978 | } | |
979 | if (buf[i] == '\n') { | |
980 | buf[i + 1] = 0; | |
981 | break; | |
982 | } | |
8efc0c15 | 983 | } |
5260325f | 984 | buf[sizeof(buf) - 1] = 0; |
985 | ||
aa3378df | 986 | /* |
987 | * Check that the versions match. In future this might accept | |
988 | * several versions and set appropriate flags to handle them. | |
989 | */ | |
5260325f | 990 | if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor, |
991 | remote_version) != 3) | |
992 | fatal("Bad remote protocol version identification: '%.100s'", buf); | |
993 | debug("Remote protocol version %d.%d, remote software version %.100s", | |
994 | remote_major, remote_minor, remote_version); | |
995 | ||
996 | /* Check if the remote protocol version is too old. */ | |
997 | if (remote_major == 1 && remote_minor < 3) | |
998 | fatal("Remote machine has too old SSH software version."); | |
999 | ||
1000 | /* We speak 1.3, too. */ | |
1001 | if (remote_major == 1 && remote_minor == 3) { | |
1002 | enable_compat13(); | |
7b2ea3a1 | 1003 | if (options.forward_agent) { |
1004 | log("Agent forwarding disabled for protocol 1.3"); | |
5260325f | 1005 | options.forward_agent = 0; |
1006 | } | |
8efc0c15 | 1007 | } |
8efc0c15 | 1008 | #if 0 |
aa3378df | 1009 | /* |
1010 | * Removed for now, to permit compatibility with latter versions. The | |
1011 | * server will reject our version and disconnect if it doesn't | |
1012 | * support it. | |
1013 | */ | |
5260325f | 1014 | if (remote_major != PROTOCOL_MAJOR) |
1015 | fatal("Protocol major versions differ: %d vs. %d", | |
1016 | PROTOCOL_MAJOR, remote_major); | |
8efc0c15 | 1017 | #endif |
1018 | ||
5260325f | 1019 | /* Send our own protocol version identification. */ |
1020 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", | |
a408af76 | 1021 | PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION); |
1022 | if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf)) | |
5260325f | 1023 | fatal("write: %.100s", strerror(errno)); |
8efc0c15 | 1024 | } |
1025 | ||
1026 | int ssh_cipher_default = SSH_CIPHER_3DES; | |
1027 | ||
5260325f | 1028 | int |
1029 | read_yes_or_no(const char *prompt, int defval) | |
8efc0c15 | 1030 | { |
5260325f | 1031 | char buf[1024]; |
1032 | FILE *f; | |
1033 | int retval = -1; | |
1034 | ||
1035 | if (isatty(0)) | |
1036 | f = stdin; | |
1037 | else | |
1038 | f = fopen("/dev/tty", "rw"); | |
1039 | ||
1040 | if (f == NULL) | |
1041 | return 0; | |
1042 | ||
1043 | fflush(stdout); | |
1044 | ||
1045 | while (1) { | |
1046 | fprintf(stderr, "%s", prompt); | |
1047 | if (fgets(buf, sizeof(buf), f) == NULL) { | |
1048 | /* Print a newline (the prompt probably didn\'t have one). */ | |
1049 | fprintf(stderr, "\n"); | |
1050 | strlcpy(buf, "no", sizeof buf); | |
1051 | } | |
1052 | /* Remove newline from response. */ | |
1053 | if (strchr(buf, '\n')) | |
1054 | *strchr(buf, '\n') = 0; | |
1055 | ||
1056 | if (buf[0] == 0) | |
1057 | retval = defval; | |
1058 | if (strcmp(buf, "yes") == 0) | |
1059 | retval = 1; | |
1060 | if (strcmp(buf, "no") == 0) | |
1061 | retval = 0; | |
1062 | ||
1063 | if (retval != -1) { | |
1064 | if (f != stdin) | |
1065 | fclose(f); | |
1066 | return retval; | |
1067 | } | |
8efc0c15 | 1068 | } |
8efc0c15 | 1069 | } |
1070 | ||
5260325f | 1071 | /* |
95f1eccc | 1072 | * check whether the supplied host key is valid, return only if ok. |
5260325f | 1073 | */ |
95f1eccc | 1074 | |
5260325f | 1075 | void |
48e671d5 | 1076 | check_host_key(char *host, struct sockaddr *hostaddr, RSA *host_key) |
8efc0c15 | 1077 | { |
95f1eccc | 1078 | RSA *file_key; |
1079 | char *ip = NULL; | |
5260325f | 1080 | char hostline[1000], *hostp; |
5260325f | 1081 | HostStatus host_status; |
1082 | HostStatus ip_status; | |
48e671d5 | 1083 | int local = 0, host_ip_differ = 0; |
20af321f | 1084 | int salen; |
48e671d5 | 1085 | char ntop[NI_MAXHOST]; |
1086 | ||
1087 | /* | |
1088 | * Force accepting of the host key for loopback/localhost. The | |
1089 | * problem is that if the home directory is NFS-mounted to multiple | |
1090 | * machines, localhost will refer to a different machine in each of | |
1091 | * them, and the user will get bogus HOST_CHANGED warnings. This | |
1092 | * essentially disables host authentication for localhost; however, | |
1093 | * this is probably not a real problem. | |
1094 | */ | |
1095 | switch (hostaddr->sa_family) { | |
1096 | case AF_INET: | |
1097 | local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET; | |
20af321f | 1098 | salen = sizeof(struct sockaddr_in); |
48e671d5 | 1099 | break; |
1100 | case AF_INET6: | |
1101 | local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr)); | |
20af321f | 1102 | salen = sizeof(struct sockaddr_in6); |
48e671d5 | 1103 | break; |
1104 | default: | |
1105 | local = 0; | |
20af321f | 1106 | salen = sizeof(struct sockaddr_storage); |
48e671d5 | 1107 | break; |
1108 | } | |
1109 | if (local) { | |
1110 | debug("Forcing accepting of host key for loopback/localhost."); | |
1111 | return; | |
1112 | } | |
5260325f | 1113 | |
57112b5a | 1114 | /* |
1115 | * Turn off check_host_ip for proxy connects, since | |
1116 | * we don't have the remote ip-address | |
1117 | */ | |
1118 | if (options.proxy_command != NULL && options.check_host_ip) | |
1119 | options.check_host_ip = 0; | |
1120 | ||
48e671d5 | 1121 | if (options.check_host_ip) { |
20af321f | 1122 | if (getnameinfo(hostaddr, salen, ntop, sizeof(ntop), |
48e671d5 | 1123 | NULL, 0, NI_NUMERICHOST) != 0) |
1124 | fatal("check_host_key: getnameinfo failed"); | |
1125 | ip = xstrdup(ntop); | |
1126 | } | |
5260325f | 1127 | |
95f1eccc | 1128 | /* |
1129 | * Store the host key from the known host file in here so that we can | |
1130 | * compare it with the key for the IP address. | |
1131 | */ | |
5260325f | 1132 | file_key = RSA_new(); |
1133 | file_key->n = BN_new(); | |
1134 | file_key->e = BN_new(); | |
1135 | ||
aa3378df | 1136 | /* |
1137 | * Check if the host key is present in the user\'s list of known | |
1138 | * hosts or in the systemwide list. | |
1139 | */ | |
5260325f | 1140 | host_status = check_host_in_hostfile(options.user_hostfile, host, |
1141 | host_key->e, host_key->n, | |
1142 | file_key->e, file_key->n); | |
1143 | if (host_status == HOST_NEW) | |
1144 | host_status = check_host_in_hostfile(options.system_hostfile, host, | |
1145 | host_key->e, host_key->n, | |
1146 | file_key->e, file_key->n); | |
aa3378df | 1147 | /* |
1148 | * Also perform check for the ip address, skip the check if we are | |
1149 | * localhost or the hostname was an ip address to begin with | |
1150 | */ | |
5260325f | 1151 | if (options.check_host_ip && !local && strcmp(host, ip)) { |
1152 | RSA *ip_key = RSA_new(); | |
1153 | ip_key->n = BN_new(); | |
1154 | ip_key->e = BN_new(); | |
1155 | ip_status = check_host_in_hostfile(options.user_hostfile, ip, | |
1156 | host_key->e, host_key->n, | |
1157 | ip_key->e, ip_key->n); | |
1158 | ||
1159 | if (ip_status == HOST_NEW) | |
1160 | ip_status = check_host_in_hostfile(options.system_hostfile, ip, | |
1161 | host_key->e, host_key->n, | |
1162 | ip_key->e, ip_key->n); | |
1163 | if (host_status == HOST_CHANGED && | |
1164 | (ip_status != HOST_CHANGED || | |
1165 | (BN_cmp(ip_key->e, file_key->e) || BN_cmp(ip_key->n, file_key->n)))) | |
1166 | host_ip_differ = 1; | |
1167 | ||
1168 | RSA_free(ip_key); | |
1169 | } else | |
1170 | ip_status = host_status; | |
1171 | ||
1172 | RSA_free(file_key); | |
1173 | ||
1174 | switch (host_status) { | |
1175 | case HOST_OK: | |
1176 | /* The host is known and the key matches. */ | |
1177 | debug("Host '%.200s' is known and matches the host key.", host); | |
1178 | if (options.check_host_ip) { | |
1179 | if (ip_status == HOST_NEW) { | |
1180 | if (!add_host_to_hostfile(options.user_hostfile, ip, | |
1181 | host_key->e, host_key->n)) | |
1182 | log("Failed to add the host key for IP address '%.30s' to the list of known hosts (%.30s).", | |
1183 | ip, options.user_hostfile); | |
1184 | else | |
1185 | log("Warning: Permanently added host key for IP address '%.30s' to the list of known hosts.", | |
1186 | ip); | |
1187 | } else if (ip_status != HOST_OK) | |
1188 | log("Warning: the host key for '%.200s' differs from the key for the IP address '%.30s'", | |
1189 | host, ip); | |
1190 | } | |
1191 | break; | |
1192 | case HOST_NEW: | |
1193 | /* The host is new. */ | |
1194 | if (options.strict_host_key_checking == 1) { | |
1195 | /* User has requested strict host key checking. We will not add the host key | |
1196 | automatically. The only alternative left is to abort. */ | |
1197 | fatal("No host key is known for %.200s and you have requested strict checking.", host); | |
1198 | } else if (options.strict_host_key_checking == 2) { | |
1199 | /* The default */ | |
1200 | char prompt[1024]; | |
1201 | char *fp = fingerprint(host_key->e, host_key->n); | |
1202 | snprintf(prompt, sizeof(prompt), | |
a408af76 | 1203 | "The authenticity of host '%.200s' can't be established.\n" |
1204 | "Key fingerprint is %d %s.\n" | |
1205 | "Are you sure you want to continue connecting (yes/no)? ", | |
1206 | host, BN_num_bits(host_key->n), fp); | |
5260325f | 1207 | if (!read_yes_or_no(prompt, -1)) |
1208 | fatal("Aborted by user!\n"); | |
1209 | } | |
1210 | if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) { | |
1211 | snprintf(hostline, sizeof(hostline), "%s,%s", host, ip); | |
1212 | hostp = hostline; | |
1213 | } else | |
1214 | hostp = host; | |
1215 | ||
1216 | /* If not in strict mode, add the key automatically to the local known_hosts file. */ | |
1217 | if (!add_host_to_hostfile(options.user_hostfile, hostp, | |
1218 | host_key->e, host_key->n)) | |
1219 | log("Failed to add the host to the list of known hosts (%.500s).", | |
1220 | options.user_hostfile); | |
1221 | else | |
1222 | log("Warning: Permanently added '%.200s' to the list of known hosts.", | |
1223 | hostp); | |
1224 | break; | |
1225 | case HOST_CHANGED: | |
1226 | if (options.check_host_ip && host_ip_differ) { | |
1227 | char *msg; | |
1228 | if (ip_status == HOST_NEW) | |
1229 | msg = "is unknown"; | |
1230 | else if (ip_status == HOST_OK) | |
1231 | msg = "is unchanged"; | |
1232 | else | |
1233 | msg = "has a different value"; | |
1234 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
1235 | error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @"); | |
1236 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
1237 | error("The host key for %s has changed,", host); | |
1238 | error("and the key for the according IP address %s", ip); | |
1239 | error("%s. This could either mean that", msg); | |
1240 | error("DNS SPOOFING is happening or the IP address for the host"); | |
1241 | error("and its host key have changed at the same time"); | |
1242 | } | |
1243 | /* The host key has changed. */ | |
1244 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
ae2f7af7 | 1245 | error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); |
5260325f | 1246 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
1247 | error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); | |
1248 | error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); | |
1249 | error("It is also possible that the host key has just been changed."); | |
1250 | error("Please contact your system administrator."); | |
1251 | error("Add correct host key in %.100s to get rid of this message.", | |
1252 | options.user_hostfile); | |
1253 | ||
aa3378df | 1254 | /* |
1255 | * If strict host key checking is in use, the user will have | |
1256 | * to edit the key manually and we can only abort. | |
1257 | */ | |
5260325f | 1258 | if (options.strict_host_key_checking) |
1259 | fatal("Host key for %.200s has changed and you have requested strict checking.", host); | |
1260 | ||
aa3378df | 1261 | /* |
1262 | * If strict host key checking has not been requested, allow | |
1263 | * the connection but without password authentication or | |
1264 | * agent forwarding. | |
1265 | */ | |
5260325f | 1266 | if (options.password_authentication) { |
1267 | error("Password authentication is disabled to avoid trojan horses."); | |
1268 | options.password_authentication = 0; | |
1269 | } | |
1270 | if (options.forward_agent) { | |
1271 | error("Agent forwarding is disabled to avoid trojan horses."); | |
1272 | options.forward_agent = 0; | |
1273 | } | |
aa3378df | 1274 | /* |
1275 | * XXX Should permit the user to change to use the new id. | |
1276 | * This could be done by converting the host key to an | |
1277 | * identifying sentence, tell that the host identifies itself | |
1278 | * by that sentence, and ask the user if he/she whishes to | |
1279 | * accept the authentication. | |
1280 | */ | |
5260325f | 1281 | break; |
1282 | } | |
5260325f | 1283 | if (options.check_host_ip) |
1284 | xfree(ip); | |
95f1eccc | 1285 | } |
1286 | ||
1287 | /* | |
7b2ea3a1 | 1288 | * SSH1 key exchange |
95f1eccc | 1289 | */ |
1290 | void | |
7b2ea3a1 | 1291 | ssh_kex(char *host, struct sockaddr *hostaddr) |
95f1eccc | 1292 | { |
7b2ea3a1 | 1293 | int i; |
95f1eccc | 1294 | BIGNUM *key; |
1295 | RSA *host_key; | |
1296 | RSA *public_key; | |
1297 | int bits, rbits; | |
1298 | unsigned char session_key[SSH_SESSION_KEY_LENGTH]; | |
7b2ea3a1 | 1299 | unsigned char cookie[8]; |
1300 | unsigned int supported_ciphers; | |
95f1eccc | 1301 | unsigned int server_flags, client_flags; |
1302 | int payload_len, clen, sum_len = 0; | |
1303 | u_int32_t rand = 0; | |
1304 | ||
95f1eccc | 1305 | debug("Waiting for server public key."); |
1306 | ||
1307 | /* Wait for a public key packet from the server. */ | |
1308 | packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY); | |
1309 | ||
7b2ea3a1 | 1310 | /* Get cookie from the packet. */ |
95f1eccc | 1311 | for (i = 0; i < 8; i++) |
7b2ea3a1 | 1312 | cookie[i] = packet_get_char(); |
95f1eccc | 1313 | |
1314 | /* Get the public key. */ | |
1315 | public_key = RSA_new(); | |
1316 | bits = packet_get_int();/* bits */ | |
1317 | public_key->e = BN_new(); | |
1318 | packet_get_bignum(public_key->e, &clen); | |
1319 | sum_len += clen; | |
1320 | public_key->n = BN_new(); | |
1321 | packet_get_bignum(public_key->n, &clen); | |
1322 | sum_len += clen; | |
1323 | ||
1324 | rbits = BN_num_bits(public_key->n); | |
1325 | if (bits != rbits) { | |
1326 | log("Warning: Server lies about size of server public key: " | |
1327 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
1328 | log("Warning: This may be due to an old implementation of ssh."); | |
1329 | } | |
1330 | /* Get the host key. */ | |
1331 | host_key = RSA_new(); | |
1332 | bits = packet_get_int();/* bits */ | |
1333 | host_key->e = BN_new(); | |
1334 | packet_get_bignum(host_key->e, &clen); | |
1335 | sum_len += clen; | |
1336 | host_key->n = BN_new(); | |
1337 | packet_get_bignum(host_key->n, &clen); | |
1338 | sum_len += clen; | |
1339 | ||
1340 | rbits = BN_num_bits(host_key->n); | |
1341 | if (bits != rbits) { | |
1342 | log("Warning: Server lies about size of server host key: " | |
1343 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
1344 | log("Warning: This may be due to an old implementation of ssh."); | |
1345 | } | |
1346 | ||
1347 | /* Get protocol flags. */ | |
1348 | server_flags = packet_get_int(); | |
1349 | packet_set_protocol_flags(server_flags); | |
1350 | ||
1351 | supported_ciphers = packet_get_int(); | |
1352 | supported_authentications = packet_get_int(); | |
1353 | ||
1354 | debug("Received server public key (%d bits) and host key (%d bits).", | |
1355 | BN_num_bits(public_key->n), BN_num_bits(host_key->n)); | |
1356 | ||
1357 | packet_integrity_check(payload_len, | |
1358 | 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4, | |
1359 | SSH_SMSG_PUBLIC_KEY); | |
1360 | ||
1361 | check_host_key(host, hostaddr, host_key); | |
1362 | ||
1363 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; | |
1364 | ||
7b2ea3a1 | 1365 | compute_session_id(session_id, cookie, host_key->n, public_key->n); |
5260325f | 1366 | |
1367 | /* Generate a session key. */ | |
1368 | arc4random_stir(); | |
1369 | ||
aa3378df | 1370 | /* |
1371 | * Generate an encryption key for the session. The key is a 256 bit | |
1372 | * random number, interpreted as a 32-byte key, with the least | |
1373 | * significant 8 bits being the first byte of the key. | |
1374 | */ | |
5260325f | 1375 | for (i = 0; i < 32; i++) { |
1376 | if (i % 4 == 0) | |
1377 | rand = arc4random(); | |
1378 | session_key[i] = rand & 0xff; | |
1379 | rand >>= 8; | |
1380 | } | |
1381 | ||
aa3378df | 1382 | /* |
1383 | * According to the protocol spec, the first byte of the session key | |
1384 | * is the highest byte of the integer. The session key is xored with | |
1385 | * the first 16 bytes of the session id. | |
1386 | */ | |
5260325f | 1387 | key = BN_new(); |
1388 | BN_set_word(key, 0); | |
1389 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
1390 | BN_lshift(key, key, 8); | |
1391 | if (i < 16) | |
1392 | BN_add_word(key, session_key[i] ^ session_id[i]); | |
1393 | else | |
1394 | BN_add_word(key, session_key[i]); | |
1395 | } | |
1396 | ||
aa3378df | 1397 | /* |
1398 | * Encrypt the integer using the public key and host key of the | |
1399 | * server (key with smaller modulus first). | |
1400 | */ | |
5260325f | 1401 | if (BN_cmp(public_key->n, host_key->n) < 0) { |
1402 | /* Public key has smaller modulus. */ | |
1403 | if (BN_num_bits(host_key->n) < | |
1404 | BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) { | |
1405 | fatal("respond_to_rsa_challenge: host_key %d < public_key %d + " | |
1406 | "SSH_KEY_BITS_RESERVED %d", | |
1407 | BN_num_bits(host_key->n), | |
1408 | BN_num_bits(public_key->n), | |
1409 | SSH_KEY_BITS_RESERVED); | |
1410 | } | |
1411 | rsa_public_encrypt(key, key, public_key); | |
1412 | rsa_public_encrypt(key, key, host_key); | |
1413 | } else { | |
1414 | /* Host key has smaller modulus (or they are equal). */ | |
1415 | if (BN_num_bits(public_key->n) < | |
1416 | BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) { | |
1417 | fatal("respond_to_rsa_challenge: public_key %d < host_key %d + " | |
1418 | "SSH_KEY_BITS_RESERVED %d", | |
1419 | BN_num_bits(public_key->n), | |
1420 | BN_num_bits(host_key->n), | |
1421 | SSH_KEY_BITS_RESERVED); | |
1422 | } | |
1423 | rsa_public_encrypt(key, key, host_key); | |
1424 | rsa_public_encrypt(key, key, public_key); | |
1425 | } | |
1426 | ||
7b2ea3a1 | 1427 | /* Destroy the public keys since we no longer need them. */ |
1428 | RSA_free(public_key); | |
1429 | RSA_free(host_key); | |
1430 | ||
5260325f | 1431 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
1432 | if (cipher_mask() & supported_ciphers & (1 << ssh_cipher_default)) | |
1433 | options.cipher = ssh_cipher_default; | |
1434 | else { | |
1435 | debug("Cipher %s not supported, using %.100s instead.", | |
1436 | cipher_name(ssh_cipher_default), | |
1437 | cipher_name(SSH_FALLBACK_CIPHER)); | |
1438 | options.cipher = SSH_FALLBACK_CIPHER; | |
1439 | } | |
1440 | } | |
1441 | /* Check that the selected cipher is supported. */ | |
1442 | if (!(supported_ciphers & (1 << options.cipher))) | |
1443 | fatal("Selected cipher type %.100s not supported by server.", | |
1444 | cipher_name(options.cipher)); | |
1445 | ||
1446 | debug("Encryption type: %.100s", cipher_name(options.cipher)); | |
1447 | ||
1448 | /* Send the encrypted session key to the server. */ | |
1449 | packet_start(SSH_CMSG_SESSION_KEY); | |
1450 | packet_put_char(options.cipher); | |
1451 | ||
7b2ea3a1 | 1452 | /* Send the cookie back to the server. */ |
5260325f | 1453 | for (i = 0; i < 8; i++) |
7b2ea3a1 | 1454 | packet_put_char(cookie[i]); |
5260325f | 1455 | |
7b2ea3a1 | 1456 | /* Send and destroy the encrypted encryption key integer. */ |
5260325f | 1457 | packet_put_bignum(key); |
7b2ea3a1 | 1458 | BN_clear_free(key); |
5260325f | 1459 | |
1460 | /* Send protocol flags. */ | |
95f1eccc | 1461 | packet_put_int(client_flags); |
5260325f | 1462 | |
1463 | /* Send the packet now. */ | |
8efc0c15 | 1464 | packet_send(); |
1465 | packet_write_wait(); | |
5260325f | 1466 | |
5260325f | 1467 | debug("Sent encrypted session key."); |
1468 | ||
1469 | /* Set the encryption key. */ | |
1470 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); | |
1471 | ||
1472 | /* We will no longer need the session key here. Destroy any extra copies. */ | |
1473 | memset(session_key, 0, sizeof(session_key)); | |
1474 | ||
aa3378df | 1475 | /* |
1476 | * Expect a success message from the server. Note that this message | |
1477 | * will be received in encrypted form. | |
1478 | */ | |
5260325f | 1479 | packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); |
1480 | ||
1481 | debug("Received encrypted confirmation."); | |
7b2ea3a1 | 1482 | } |
1483 | ||
1484 | /* | |
1485 | * Authenticate user | |
1486 | */ | |
1487 | void | |
1488 | ssh_userauth(int host_key_valid, RSA *own_host_key, | |
1489 | uid_t original_real_uid, char *host) | |
1490 | { | |
1491 | int i, type; | |
1492 | int payload_len; | |
1493 | struct passwd *pw; | |
1494 | const char *server_user, *local_user; | |
1495 | ||
1496 | /* Get local user name. Use it as server user if no user name was given. */ | |
1497 | pw = getpwuid(original_real_uid); | |
1498 | if (!pw) | |
1499 | fatal("User id %d not found from user database.", original_real_uid); | |
1500 | local_user = xstrdup(pw->pw_name); | |
1501 | server_user = options.user ? options.user : local_user; | |
5260325f | 1502 | |
1503 | /* Send the name of the user to log in as on the server. */ | |
1504 | packet_start(SSH_CMSG_USER); | |
1505 | packet_put_string(server_user, strlen(server_user)); | |
1506 | packet_send(); | |
1507 | packet_write_wait(); | |
1508 | ||
aa3378df | 1509 | /* |
1510 | * The server should respond with success if no authentication is | |
1511 | * needed (the user has no password). Otherwise the server responds | |
1512 | * with failure. | |
1513 | */ | |
8efc0c15 | 1514 | type = packet_read(&payload_len); |
5260325f | 1515 | |
1516 | /* check whether the connection was accepted without authentication. */ | |
8efc0c15 | 1517 | if (type == SSH_SMSG_SUCCESS) |
5260325f | 1518 | return; |
8efc0c15 | 1519 | if (type != SSH_SMSG_FAILURE) |
5260325f | 1520 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", |
1521 | type); | |
1522 | ||
1523 | #ifdef AFS | |
1524 | /* Try Kerberos tgt passing if the server supports it. */ | |
1525 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
1526 | options.kerberos_tgt_passing) { | |
1527 | if (options.cipher == SSH_CIPHER_NONE) | |
1528 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
1529 | (void) send_kerberos_tgt(); | |
1530 | } | |
1531 | /* Try AFS token passing if the server supports it. */ | |
1532 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && | |
1533 | options.afs_token_passing && k_hasafs()) { | |
1534 | if (options.cipher == SSH_CIPHER_NONE) | |
1535 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); | |
1536 | send_afs_tokens(); | |
1537 | } | |
1538 | #endif /* AFS */ | |
8efc0c15 | 1539 | |
5260325f | 1540 | #ifdef KRB4 |
1541 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
1542 | options.kerberos_authentication) { | |
1543 | debug("Trying Kerberos authentication."); | |
1544 | if (try_kerberos_authentication()) { | |
1545 | /* The server should respond with success or failure. */ | |
1546 | type = packet_read(&payload_len); | |
1547 | if (type == SSH_SMSG_SUCCESS) | |
1548 | return; | |
1549 | if (type != SSH_SMSG_FAILURE) | |
1550 | packet_disconnect("Protocol error: got %d in response to Kerberos auth", type); | |
1551 | } | |
1552 | } | |
1553 | #endif /* KRB4 */ | |
1554 | ||
aa3378df | 1555 | /* |
1556 | * Use rhosts authentication if running in privileged socket and we | |
1557 | * do not wish to remain anonymous. | |
1558 | */ | |
5260325f | 1559 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && |
1560 | options.rhosts_authentication) { | |
1561 | debug("Trying rhosts authentication."); | |
1562 | packet_start(SSH_CMSG_AUTH_RHOSTS); | |
1563 | packet_put_string(local_user, strlen(local_user)); | |
1564 | packet_send(); | |
1565 | packet_write_wait(); | |
1566 | ||
1567 | /* The server should respond with success or failure. */ | |
1568 | type = packet_read(&payload_len); | |
1569 | if (type == SSH_SMSG_SUCCESS) | |
1570 | return; | |
1571 | if (type != SSH_SMSG_FAILURE) | |
1572 | packet_disconnect("Protocol error: got %d in response to rhosts auth", | |
1573 | type); | |
1574 | } | |
aa3378df | 1575 | /* |
1576 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host | |
1577 | * authentication. | |
1578 | */ | |
5260325f | 1579 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && |
1580 | options.rhosts_rsa_authentication && host_key_valid) { | |
1581 | if (try_rhosts_rsa_authentication(local_user, own_host_key)) | |
1582 | return; | |
1583 | } | |
1584 | /* Try RSA authentication if the server supports it. */ | |
1585 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && | |
1586 | options.rsa_authentication) { | |
aa3378df | 1587 | /* |
1588 | * Try RSA authentication using the authentication agent. The | |
1589 | * agent is tried first because no passphrase is needed for | |
1590 | * it, whereas identity files may require passphrases. | |
1591 | */ | |
5260325f | 1592 | if (try_agent_authentication()) |
1593 | return; | |
1594 | ||
1595 | /* Try RSA authentication for each identity. */ | |
1596 | for (i = 0; i < options.num_identity_files; i++) | |
57112b5a | 1597 | if (try_rsa_authentication(options.identity_files[i])) |
5260325f | 1598 | return; |
1599 | } | |
1600 | /* Try skey authentication if the server supports it. */ | |
1601 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && | |
1602 | options.skey_authentication && !options.batch_mode) { | |
57112b5a | 1603 | if (try_skey_authentication()) |
1604 | return; | |
5260325f | 1605 | } |
1606 | /* Try password authentication if the server supports it. */ | |
1607 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && | |
1608 | options.password_authentication && !options.batch_mode) { | |
1609 | char prompt[80]; | |
a408af76 | 1610 | |
57112b5a | 1611 | snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ", |
a408af76 | 1612 | server_user, host); |
57112b5a | 1613 | if (try_password_authentication(prompt)) |
1614 | return; | |
5260325f | 1615 | } |
1616 | /* All authentication methods have failed. Exit with an error message. */ | |
1617 | fatal("Permission denied."); | |
1618 | /* NOTREACHED */ | |
8efc0c15 | 1619 | } |
7b2ea3a1 | 1620 | |
1621 | /* | |
1622 | * Starts a dialog with the server, and authenticates the current user on the | |
1623 | * server. This does not need any extra privileges. The basic connection | |
1624 | * to the server must already have been established before this is called. | |
1625 | * If login fails, this function prints an error and never returns. | |
1626 | * This function does not require super-user privileges. | |
1627 | */ | |
1628 | void | |
1629 | ssh_login(int host_key_valid, RSA *own_host_key, const char *orighost, | |
1630 | struct sockaddr *hostaddr, uid_t original_real_uid) | |
1631 | { | |
1632 | char *host, *cp; | |
1633 | ||
1634 | /* Convert the user-supplied hostname into all lowercase. */ | |
1635 | host = xstrdup(orighost); | |
1636 | for (cp = host; *cp; cp++) | |
1637 | if (isupper(*cp)) | |
1638 | *cp = tolower(*cp); | |
1639 | ||
1640 | /* Exchange protocol version identification strings with the server. */ | |
1641 | ssh_exchange_identification(); | |
1642 | ||
1643 | /* Put the connection into non-blocking mode. */ | |
1644 | packet_set_nonblocking(); | |
1645 | ||
1646 | supported_authentications = 0; | |
1647 | /* key exchange */ | |
1648 | ssh_kex(host, hostaddr); | |
1649 | if (supported_authentications == 0) | |
1650 | fatal("supported_authentications == 0."); | |
1651 | /* authenticate user */ | |
1652 | ssh_userauth(host_key_valid, own_host_key, original_real_uid, host); | |
1653 | } |