]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
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 | * Code to connect to a remote host, and to perform the client side of the | |
6 | * login (authentication) dialog. | |
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". | |
13 | */ | |
14 | ||
15 | #include "includes.h" | |
1e608e42 | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.48 2002/02/11 16:15:46 markus Exp $"); |
3c0ef626 | 17 | |
18 | #include <openssl/bn.h> | |
1e608e42 | 19 | #include <openssl/md5.h> |
3c0ef626 | 20 | |
21 | #ifdef KRB4 | |
22 | #include <krb.h> | |
23 | #endif | |
24 | #ifdef KRB5 | |
25 | #include <krb5.h> | |
63119dd9 | 26 | #ifndef HEIMDAL |
27 | #define krb5_get_err_text(context,code) error_message(code) | |
28 | #endif /* !HEIMDAL */ | |
3c0ef626 | 29 | #endif |
30 | #ifdef AFS | |
31 | #include <kafs.h> | |
32 | #include "radix.h" | |
33 | #endif | |
34 | ||
35 | #include "ssh.h" | |
36 | #include "ssh1.h" | |
37 | #include "xmalloc.h" | |
38 | #include "rsa.h" | |
39 | #include "buffer.h" | |
40 | #include "packet.h" | |
41 | #include "mpaux.h" | |
42 | #include "uidswap.h" | |
43 | #include "log.h" | |
44 | #include "readconf.h" | |
45 | #include "key.h" | |
46 | #include "authfd.h" | |
47 | #include "sshconnect.h" | |
48 | #include "authfile.h" | |
49 | #include "readpass.h" | |
50 | #include "cipher.h" | |
51 | #include "canohost.h" | |
52 | #include "auth.h" | |
53 | ||
0b582e46 | 54 | #ifdef GSSAPI |
3f726215 | 55 | #include "ssh-gss.h" |
0b582e46 | 56 | #include "bufaux.h" |
1aad58f1 | 57 | |
0b582e46 | 58 | /* |
59 | * MD5 hash of host and session keys for verification. This is filled | |
60 | * in in ssh_login() and then checked in try_gssapi_authentication(). | |
61 | */ | |
62 | unsigned char ssh_key_digest[16]; | |
63 | #endif /* GSSAPI */ | |
0b582e46 | 64 | |
3c0ef626 | 65 | /* Session id for the current session. */ |
66 | u_char session_id[16]; | |
67 | u_int supported_authentications = 0; | |
68 | ||
69 | extern Options options; | |
70 | extern char *__progname; | |
71 | ||
72 | /* | |
73 | * Checks if the user has an authentication agent, and if so, tries to | |
74 | * authenticate using the agent. | |
75 | */ | |
76 | static int | |
77 | try_agent_authentication(void) | |
78 | { | |
79 | int type; | |
80 | char *comment; | |
81 | AuthenticationConnection *auth; | |
82 | u_char response[16]; | |
83 | u_int i; | |
3c0ef626 | 84 | Key *key; |
85 | BIGNUM *challenge; | |
86 | ||
87 | /* Get connection to the agent. */ | |
88 | auth = ssh_get_authentication_connection(); | |
89 | if (!auth) | |
90 | return 0; | |
91 | ||
1e608e42 | 92 | if ((challenge = BN_new()) == NULL) |
93 | fatal("try_agent_authentication: BN_new failed"); | |
3c0ef626 | 94 | /* Loop through identities served by the agent. */ |
95 | for (key = ssh_get_first_identity(auth, &comment, 1); | |
1e608e42 | 96 | key != NULL; |
97 | key = ssh_get_next_identity(auth, &comment, 1)) { | |
3c0ef626 | 98 | |
99 | /* Try this identity. */ | |
100 | debug("Trying RSA authentication via agent with '%.100s'", comment); | |
101 | xfree(comment); | |
102 | ||
103 | /* Tell the server that we are willing to authenticate using this key. */ | |
104 | packet_start(SSH_CMSG_AUTH_RSA); | |
105 | packet_put_bignum(key->rsa->n); | |
106 | packet_send(); | |
107 | packet_write_wait(); | |
108 | ||
109 | /* Wait for server's response. */ | |
1e608e42 | 110 | type = packet_read(); |
3c0ef626 | 111 | |
112 | /* The server sends failure if it doesn\'t like our key or | |
113 | does not support RSA authentication. */ | |
114 | if (type == SSH_SMSG_FAILURE) { | |
115 | debug("Server refused our key."); | |
116 | key_free(key); | |
117 | continue; | |
118 | } | |
119 | /* Otherwise it should have sent a challenge. */ | |
120 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
121 | packet_disconnect("Protocol error during RSA authentication: %d", | |
122 | type); | |
123 | ||
1e608e42 | 124 | packet_get_bignum(challenge); |
125 | packet_check_eom(); | |
3c0ef626 | 126 | |
127 | debug("Received RSA challenge from server."); | |
128 | ||
129 | /* Ask the agent to decrypt the challenge. */ | |
130 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { | |
131 | /* | |
132 | * The agent failed to authenticate this identifier | |
133 | * although it advertised it supports this. Just | |
134 | * return a wrong value. | |
135 | */ | |
136 | log("Authentication agent failed to decrypt challenge."); | |
137 | memset(response, 0, sizeof(response)); | |
138 | } | |
139 | key_free(key); | |
140 | debug("Sending response to RSA challenge."); | |
141 | ||
142 | /* Send the decrypted challenge back to the server. */ | |
143 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
144 | for (i = 0; i < 16; i++) | |
145 | packet_put_char(response[i]); | |
146 | packet_send(); | |
147 | packet_write_wait(); | |
148 | ||
149 | /* Wait for response from the server. */ | |
1e608e42 | 150 | type = packet_read(); |
3c0ef626 | 151 | |
152 | /* The server returns success if it accepted the authentication. */ | |
153 | if (type == SSH_SMSG_SUCCESS) { | |
154 | ssh_close_authentication_connection(auth); | |
155 | BN_clear_free(challenge); | |
156 | debug("RSA authentication accepted by server."); | |
157 | return 1; | |
158 | } | |
159 | /* Otherwise it should return failure. */ | |
160 | if (type != SSH_SMSG_FAILURE) | |
161 | packet_disconnect("Protocol error waiting RSA auth response: %d", | |
162 | type); | |
163 | } | |
164 | ssh_close_authentication_connection(auth); | |
165 | BN_clear_free(challenge); | |
166 | debug("RSA authentication using agent refused."); | |
167 | return 0; | |
168 | } | |
169 | ||
170 | /* | |
171 | * Computes the proper response to a RSA challenge, and sends the response to | |
172 | * the server. | |
173 | */ | |
174 | static void | |
175 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) | |
176 | { | |
177 | u_char buf[32], response[16]; | |
178 | MD5_CTX md; | |
179 | int i, len; | |
180 | ||
181 | /* Decrypt the challenge using the private key. */ | |
182 | /* XXX think about Bleichenbacher, too */ | |
183 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) | |
184 | packet_disconnect( | |
185 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); | |
186 | ||
187 | /* Compute the response. */ | |
188 | /* The response is MD5 of decrypted challenge plus session id. */ | |
189 | len = BN_num_bytes(challenge); | |
190 | if (len <= 0 || len > sizeof(buf)) | |
191 | packet_disconnect( | |
192 | "respond_to_rsa_challenge: bad challenge length %d", len); | |
193 | ||
194 | memset(buf, 0, sizeof(buf)); | |
195 | BN_bn2bin(challenge, buf + sizeof(buf) - len); | |
196 | MD5_Init(&md); | |
197 | MD5_Update(&md, buf, 32); | |
198 | MD5_Update(&md, session_id, 16); | |
199 | MD5_Final(response, &md); | |
200 | ||
201 | debug("Sending response to host key RSA challenge."); | |
202 | ||
203 | /* Send the response back to the server. */ | |
204 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
205 | for (i = 0; i < 16; i++) | |
206 | packet_put_char(response[i]); | |
207 | packet_send(); | |
208 | packet_write_wait(); | |
209 | ||
210 | memset(buf, 0, sizeof(buf)); | |
211 | memset(response, 0, sizeof(response)); | |
212 | memset(&md, 0, sizeof(md)); | |
213 | } | |
214 | ||
215 | /* | |
216 | * Checks if the user has authentication file, and if so, tries to authenticate | |
217 | * the user using it. | |
218 | */ | |
219 | static int | |
220 | try_rsa_authentication(int idx) | |
221 | { | |
222 | BIGNUM *challenge; | |
223 | Key *public, *private; | |
224 | char buf[300], *passphrase, *comment, *authfile; | |
1e608e42 | 225 | int i, type, quit; |
3c0ef626 | 226 | |
227 | public = options.identity_keys[idx]; | |
228 | authfile = options.identity_files[idx]; | |
229 | comment = xstrdup(authfile); | |
230 | ||
231 | debug("Trying RSA authentication with key '%.100s'", comment); | |
232 | ||
233 | /* Tell the server that we are willing to authenticate using this key. */ | |
234 | packet_start(SSH_CMSG_AUTH_RSA); | |
235 | packet_put_bignum(public->rsa->n); | |
236 | packet_send(); | |
237 | packet_write_wait(); | |
238 | ||
239 | /* Wait for server's response. */ | |
1e608e42 | 240 | type = packet_read(); |
3c0ef626 | 241 | |
242 | /* | |
243 | * The server responds with failure if it doesn\'t like our key or | |
244 | * doesn\'t support RSA authentication. | |
245 | */ | |
246 | if (type == SSH_SMSG_FAILURE) { | |
247 | debug("Server refused our key."); | |
248 | xfree(comment); | |
249 | return 0; | |
250 | } | |
251 | /* Otherwise, the server should respond with a challenge. */ | |
252 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
253 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
254 | ||
255 | /* Get the challenge from the packet. */ | |
1e608e42 | 256 | if ((challenge = BN_new()) == NULL) |
257 | fatal("try_rsa_authentication: BN_new failed"); | |
258 | packet_get_bignum(challenge); | |
259 | packet_check_eom(); | |
3c0ef626 | 260 | |
261 | debug("Received RSA challenge from server."); | |
262 | ||
263 | /* | |
264 | * If the key is not stored in external hardware, we have to | |
265 | * load the private key. Try first with empty passphrase; if it | |
266 | * fails, ask for a passphrase. | |
267 | */ | |
268 | if (public->flags && KEY_FLAG_EXT) | |
269 | private = public; | |
270 | else | |
271 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL); | |
272 | if (private == NULL && !options.batch_mode) { | |
273 | snprintf(buf, sizeof(buf), | |
274 | "Enter passphrase for RSA key '%.100s': ", comment); | |
275 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
276 | passphrase = read_passphrase(buf, 0); | |
277 | if (strcmp(passphrase, "") != 0) { | |
278 | private = key_load_private_type(KEY_RSA1, | |
279 | authfile, passphrase, NULL); | |
280 | quit = 0; | |
281 | } else { | |
282 | debug2("no passphrase given, try next key"); | |
283 | quit = 1; | |
284 | } | |
285 | memset(passphrase, 0, strlen(passphrase)); | |
286 | xfree(passphrase); | |
287 | if (private != NULL || quit) | |
288 | break; | |
289 | debug2("bad passphrase given, try again..."); | |
290 | } | |
291 | } | |
292 | /* We no longer need the comment. */ | |
293 | xfree(comment); | |
294 | ||
295 | if (private == NULL) { | |
296 | if (!options.batch_mode) | |
297 | error("Bad passphrase."); | |
298 | ||
299 | /* Send a dummy response packet to avoid protocol error. */ | |
300 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
301 | for (i = 0; i < 16; i++) | |
302 | packet_put_char(0); | |
303 | packet_send(); | |
304 | packet_write_wait(); | |
305 | ||
306 | /* Expect the server to reject it... */ | |
1e608e42 | 307 | packet_read_expect(SSH_SMSG_FAILURE); |
3c0ef626 | 308 | BN_clear_free(challenge); |
309 | return 0; | |
310 | } | |
311 | ||
312 | /* Compute and send a response to the challenge. */ | |
313 | respond_to_rsa_challenge(challenge, private->rsa); | |
314 | ||
315 | /* Destroy the private key unless it in external hardware. */ | |
316 | if (!(private->flags & KEY_FLAG_EXT)) | |
317 | key_free(private); | |
318 | ||
319 | /* We no longer need the challenge. */ | |
320 | BN_clear_free(challenge); | |
321 | ||
322 | /* Wait for response from the server. */ | |
1e608e42 | 323 | type = packet_read(); |
3c0ef626 | 324 | if (type == SSH_SMSG_SUCCESS) { |
325 | debug("RSA authentication accepted by server."); | |
326 | return 1; | |
327 | } | |
328 | if (type != SSH_SMSG_FAILURE) | |
329 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
330 | debug("RSA authentication refused."); | |
331 | return 0; | |
332 | } | |
333 | ||
334 | /* | |
335 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv | |
336 | * authentication and RSA host authentication. | |
337 | */ | |
338 | static int | |
339 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) | |
340 | { | |
341 | int type; | |
342 | BIGNUM *challenge; | |
3c0ef626 | 343 | |
344 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); | |
345 | ||
346 | /* Tell the server that we are willing to authenticate using this key. */ | |
347 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); | |
348 | packet_put_cstring(local_user); | |
349 | packet_put_int(BN_num_bits(host_key->rsa->n)); | |
350 | packet_put_bignum(host_key->rsa->e); | |
351 | packet_put_bignum(host_key->rsa->n); | |
352 | packet_send(); | |
353 | packet_write_wait(); | |
354 | ||
355 | /* Wait for server's response. */ | |
1e608e42 | 356 | type = packet_read(); |
3c0ef626 | 357 | |
358 | /* The server responds with failure if it doesn't admit our | |
359 | .rhosts authentication or doesn't know our host key. */ | |
360 | if (type == SSH_SMSG_FAILURE) { | |
361 | debug("Server refused our rhosts authentication or host key."); | |
362 | return 0; | |
363 | } | |
364 | /* Otherwise, the server should respond with a challenge. */ | |
365 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
366 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
367 | ||
368 | /* Get the challenge from the packet. */ | |
1e608e42 | 369 | if ((challenge = BN_new()) == NULL) |
370 | fatal("try_rhosts_rsa_authentication: BN_new failed"); | |
371 | packet_get_bignum(challenge); | |
372 | packet_check_eom(); | |
3c0ef626 | 373 | |
374 | debug("Received RSA challenge for host key from server."); | |
375 | ||
376 | /* Compute a response to the challenge. */ | |
377 | respond_to_rsa_challenge(challenge, host_key->rsa); | |
378 | ||
379 | /* We no longer need the challenge. */ | |
380 | BN_clear_free(challenge); | |
381 | ||
382 | /* Wait for response from the server. */ | |
1e608e42 | 383 | type = packet_read(); |
3c0ef626 | 384 | if (type == SSH_SMSG_SUCCESS) { |
385 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); | |
386 | return 1; | |
387 | } | |
388 | if (type != SSH_SMSG_FAILURE) | |
389 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
390 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); | |
391 | return 0; | |
392 | } | |
393 | ||
394 | #ifdef KRB4 | |
395 | static int | |
396 | try_krb4_authentication(void) | |
397 | { | |
398 | KTEXT_ST auth; /* Kerberos data */ | |
399 | char *reply; | |
400 | char inst[INST_SZ]; | |
401 | char *realm; | |
402 | CREDENTIALS cred; | |
1e608e42 | 403 | int r, type; |
3c0ef626 | 404 | socklen_t slen; |
405 | Key_schedule schedule; | |
406 | u_long checksum, cksum; | |
407 | MSG_DAT msg_data; | |
408 | struct sockaddr_in local, foreign; | |
409 | struct stat st; | |
410 | ||
411 | /* Don't do anything if we don't have any tickets. */ | |
412 | if (stat(tkt_string(), &st) < 0) | |
413 | return 0; | |
1e608e42 | 414 | |
3c0ef626 | 415 | strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)), |
416 | INST_SZ); | |
1e608e42 | 417 | |
3c0ef626 | 418 | realm = (char *)krb_realmofhost(get_canonical_hostname(1)); |
419 | if (!realm) { | |
420 | debug("Kerberos v4: no realm for %s", get_canonical_hostname(1)); | |
421 | return 0; | |
422 | } | |
423 | /* This can really be anything. */ | |
424 | checksum = (u_long)getpid(); | |
1e608e42 | 425 | |
3c0ef626 | 426 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); |
427 | if (r != KSUCCESS) { | |
428 | debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]); | |
429 | return 0; | |
430 | } | |
431 | /* Get session key to decrypt the server's reply with. */ | |
432 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); | |
433 | if (r != KSUCCESS) { | |
434 | debug("get_cred failed: %s", krb_err_txt[r]); | |
435 | return 0; | |
436 | } | |
437 | des_key_sched((des_cblock *) cred.session, schedule); | |
1e608e42 | 438 | |
3c0ef626 | 439 | /* Send authentication info to server. */ |
440 | packet_start(SSH_CMSG_AUTH_KERBEROS); | |
441 | packet_put_string((char *) auth.dat, auth.length); | |
442 | packet_send(); | |
443 | packet_write_wait(); | |
1e608e42 | 444 | |
3c0ef626 | 445 | /* Zero the buffer. */ |
446 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); | |
1e608e42 | 447 | |
3c0ef626 | 448 | slen = sizeof(local); |
449 | memset(&local, 0, sizeof(local)); | |
450 | if (getsockname(packet_get_connection_in(), | |
451 | (struct sockaddr *)&local, &slen) < 0) | |
452 | debug("getsockname failed: %s", strerror(errno)); | |
1e608e42 | 453 | |
3c0ef626 | 454 | slen = sizeof(foreign); |
455 | memset(&foreign, 0, sizeof(foreign)); | |
456 | if (getpeername(packet_get_connection_in(), | |
457 | (struct sockaddr *)&foreign, &slen) < 0) { | |
458 | debug("getpeername failed: %s", strerror(errno)); | |
459 | fatal_cleanup(); | |
460 | } | |
461 | /* Get server reply. */ | |
1e608e42 | 462 | type = packet_read(); |
3c0ef626 | 463 | switch (type) { |
464 | case SSH_SMSG_FAILURE: | |
465 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
466 | debug("Kerberos v4 authentication failed."); | |
467 | return 0; | |
468 | break; | |
1e608e42 | 469 | |
3c0ef626 | 470 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
471 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ | |
472 | debug("Kerberos v4 authentication accepted."); | |
1e608e42 | 473 | |
3c0ef626 | 474 | /* Get server's response. */ |
475 | reply = packet_get_string((u_int *) &auth.length); | |
476 | memcpy(auth.dat, reply, auth.length); | |
477 | xfree(reply); | |
1e608e42 | 478 | |
479 | packet_check_eom(); | |
480 | ||
3c0ef626 | 481 | /* |
482 | * If his response isn't properly encrypted with the session | |
483 | * key, and the decrypted checksum fails to match, he's | |
484 | * bogus. Bail out. | |
485 | */ | |
486 | r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session, | |
487 | &foreign, &local, &msg_data); | |
488 | if (r != KSUCCESS) { | |
489 | debug("Kerberos v4 krb_rd_priv failed: %s", | |
490 | krb_err_txt[r]); | |
491 | packet_disconnect("Kerberos v4 challenge failed!"); | |
492 | } | |
493 | /* Fetch the (incremented) checksum that we supplied in the request. */ | |
494 | memcpy((char *)&cksum, (char *)msg_data.app_data, | |
495 | sizeof(cksum)); | |
496 | cksum = ntohl(cksum); | |
1e608e42 | 497 | |
3c0ef626 | 498 | /* If it matches, we're golden. */ |
499 | if (cksum == checksum + 1) { | |
500 | debug("Kerberos v4 challenge successful."); | |
501 | return 1; | |
502 | } else | |
503 | packet_disconnect("Kerberos v4 challenge failed!"); | |
504 | break; | |
1e608e42 | 505 | |
3c0ef626 | 506 | default: |
507 | packet_disconnect("Protocol error on Kerberos v4 response: %d", type); | |
508 | } | |
509 | return 0; | |
510 | } | |
511 | ||
512 | #endif /* KRB4 */ | |
513 | ||
514 | #ifdef KRB5 | |
515 | static int | |
516 | try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context) | |
517 | { | |
518 | krb5_error_code problem; | |
519 | const char *tkfile; | |
520 | struct stat buf; | |
521 | krb5_ccache ccache = NULL; | |
522 | const char *remotehost; | |
523 | krb5_data ap; | |
1e608e42 | 524 | int type; |
3c0ef626 | 525 | krb5_ap_rep_enc_part *reply = NULL; |
526 | int ret; | |
1e608e42 | 527 | |
3c0ef626 | 528 | memset(&ap, 0, sizeof(ap)); |
1e608e42 | 529 | |
3c0ef626 | 530 | problem = krb5_init_context(context); |
531 | if (problem) { | |
532 | debug("Kerberos v5: krb5_init_context failed"); | |
533 | ret = 0; | |
534 | goto out; | |
535 | } | |
536 | ||
63119dd9 | 537 | problem = krb5_auth_con_init(*context, auth_context); |
538 | if (problem) { | |
539 | debug("Kerberos v5: krb5_auth_con_init failed"); | |
540 | ret = 0; | |
541 | goto out; | |
542 | } | |
543 | ||
1e608e42 | 544 | #ifndef HEIMDAL |
63119dd9 | 545 | problem = krb5_auth_con_setflags(*context, *auth_context, |
546 | KRB5_AUTH_CONTEXT_RET_TIME); | |
547 | if (problem) { | |
1e608e42 | 548 | debug("Keberos v5: krb5_auth_con_setflags failed"); |
63119dd9 | 549 | ret = 0; |
550 | goto out; | |
1e608e42 | 551 | } |
63119dd9 | 552 | #endif |
553 | ||
3c0ef626 | 554 | tkfile = krb5_cc_default_name(*context); |
555 | if (strncmp(tkfile, "FILE:", 5) == 0) | |
556 | tkfile += 5; | |
1e608e42 | 557 | |
3c0ef626 | 558 | if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { |
559 | debug("Kerberos v5: could not get default ccache (permission denied)."); | |
560 | ret = 0; | |
561 | goto out; | |
562 | } | |
1e608e42 | 563 | |
3c0ef626 | 564 | problem = krb5_cc_default(*context, &ccache); |
565 | if (problem) { | |
566 | debug("Kerberos v5: krb5_cc_default failed: %s", | |
567 | krb5_get_err_text(*context, problem)); | |
568 | ret = 0; | |
569 | goto out; | |
570 | } | |
1e608e42 | 571 | |
3c0ef626 | 572 | remotehost = get_canonical_hostname(1); |
1e608e42 | 573 | |
3c0ef626 | 574 | problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED, |
575 | "host", remotehost, NULL, ccache, &ap); | |
576 | if (problem) { | |
577 | debug("Kerberos v5: krb5_mk_req failed: %s", | |
578 | krb5_get_err_text(*context, problem)); | |
579 | ret = 0; | |
580 | goto out; | |
581 | } | |
1e608e42 | 582 | |
3c0ef626 | 583 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
584 | packet_put_string((char *) ap.data, ap.length); | |
585 | packet_send(); | |
586 | packet_write_wait(); | |
1e608e42 | 587 | |
3c0ef626 | 588 | xfree(ap.data); |
589 | ap.length = 0; | |
1e608e42 | 590 | |
591 | type = packet_read(); | |
3c0ef626 | 592 | switch (type) { |
1e608e42 | 593 | case SSH_SMSG_FAILURE: |
594 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
595 | debug("Kerberos v5 authentication failed."); | |
596 | ret = 0; | |
597 | break; | |
598 | ||
3c0ef626 | 599 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
1e608e42 | 600 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
601 | debug("Kerberos v5 authentication accepted."); | |
602 | ||
603 | /* Get server's response. */ | |
604 | ap.data = packet_get_string((unsigned int *) &ap.length); | |
605 | packet_check_eom(); | |
606 | /* XXX je to dobre? */ | |
607 | ||
608 | problem = krb5_rd_rep(*context, *auth_context, &ap, &reply); | |
609 | if (problem) { | |
3c0ef626 | 610 | ret = 0; |
611 | } | |
612 | ret = 1; | |
613 | break; | |
1e608e42 | 614 | |
3c0ef626 | 615 | default: |
616 | packet_disconnect("Protocol error on Kerberos v5 response: %d", | |
617 | type); | |
618 | ret = 0; | |
619 | break; | |
1e608e42 | 620 | |
3c0ef626 | 621 | } |
1e608e42 | 622 | |
3c0ef626 | 623 | out: |
624 | if (ccache != NULL) | |
625 | krb5_cc_close(*context, ccache); | |
626 | if (reply != NULL) | |
627 | krb5_free_ap_rep_enc_part(*context, reply); | |
628 | if (ap.length > 0) | |
63119dd9 | 629 | #ifdef HEIMDAL |
3c0ef626 | 630 | krb5_data_free(&ap); |
63119dd9 | 631 | #else |
1e608e42 | 632 | krb5_free_data_contents(*context, &ap); |
633 | #endif | |
634 | ||
3c0ef626 | 635 | return (ret); |
636 | } | |
637 | ||
638 | static void | |
639 | send_krb5_tgt(krb5_context context, krb5_auth_context auth_context) | |
640 | { | |
1e608e42 | 641 | int fd, type; |
3c0ef626 | 642 | krb5_error_code problem; |
643 | krb5_data outbuf; | |
644 | krb5_ccache ccache = NULL; | |
645 | krb5_creds creds; | |
63119dd9 | 646 | #ifdef HEIMDAL |
3c0ef626 | 647 | krb5_kdc_flags flags; |
63119dd9 | 648 | #else |
649 | int forwardable; | |
650 | #endif | |
3c0ef626 | 651 | const char *remotehost; |
1e608e42 | 652 | |
3c0ef626 | 653 | memset(&creds, 0, sizeof(creds)); |
654 | memset(&outbuf, 0, sizeof(outbuf)); | |
1e608e42 | 655 | |
3c0ef626 | 656 | fd = packet_get_connection_in(); |
1e608e42 | 657 | |
658 | #ifdef HEIMDAL | |
3c0ef626 | 659 | problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd); |
63119dd9 | 660 | #else |
1e608e42 | 661 | problem = krb5_auth_con_genaddrs(context, auth_context, fd, |
63119dd9 | 662 | KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR | |
663 | KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR); | |
63119dd9 | 664 | #endif |
1e608e42 | 665 | if (problem) |
666 | goto out; | |
667 | ||
3c0ef626 | 668 | problem = krb5_cc_default(context, &ccache); |
669 | if (problem) | |
670 | goto out; | |
1e608e42 | 671 | |
3c0ef626 | 672 | problem = krb5_cc_get_principal(context, ccache, &creds.client); |
673 | if (problem) | |
674 | goto out; | |
1e608e42 | 675 | |
676 | remotehost = get_canonical_hostname(1); | |
3c0ef626 | 677 | |
63119dd9 | 678 | #ifdef HEIMDAL |
3c0ef626 | 679 | problem = krb5_build_principal(context, &creds.server, |
680 | strlen(creds.client->realm), creds.client->realm, | |
681 | "krbtgt", creds.client->realm, NULL); | |
63119dd9 | 682 | #else |
683 | problem = krb5_build_principal(context, &creds.server, | |
684 | creds.client->realm.length, creds.client->realm.data, | |
1e608e42 | 685 | "host", remotehost, NULL); |
63119dd9 | 686 | #endif |
3c0ef626 | 687 | if (problem) |
688 | goto out; | |
1e608e42 | 689 | |
3c0ef626 | 690 | creds.times.endtime = 0; |
1e608e42 | 691 | |
692 | #ifdef HEIMDAL | |
3c0ef626 | 693 | flags.i = 0; |
694 | flags.b.forwarded = 1; | |
695 | flags.b.forwardable = krb5_config_get_bool(context, NULL, | |
696 | "libdefaults", "forwardable", NULL); | |
3c0ef626 | 697 | problem = krb5_get_forwarded_creds(context, auth_context, |
698 | ccache, flags.i, remotehost, &creds, &outbuf); | |
63119dd9 | 699 | #else |
700 | forwardable = 1; | |
1e608e42 | 701 | problem = krb5_fwd_tgt_creds(context, auth_context, remotehost, |
702 | creds.client, creds.server, ccache, forwardable, &outbuf); | |
703 | #endif | |
63119dd9 | 704 | |
1e608e42 | 705 | if (problem) |
706 | goto out; | |
63119dd9 | 707 | |
3c0ef626 | 708 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
709 | packet_put_string((char *)outbuf.data, outbuf.length); | |
710 | packet_send(); | |
711 | packet_write_wait(); | |
1e608e42 | 712 | |
713 | type = packet_read(); | |
714 | ||
3c0ef626 | 715 | if (type == SSH_SMSG_SUCCESS) { |
716 | char *pname; | |
1e608e42 | 717 | |
3c0ef626 | 718 | krb5_unparse_name(context, creds.client, &pname); |
719 | debug("Kerberos v5 TGT forwarded (%s).", pname); | |
720 | xfree(pname); | |
721 | } else | |
722 | debug("Kerberos v5 TGT forwarding failed."); | |
1e608e42 | 723 | |
3c0ef626 | 724 | return; |
1e608e42 | 725 | |
3c0ef626 | 726 | out: |
727 | if (problem) | |
728 | debug("Kerberos v5 TGT forwarding failed: %s", | |
729 | krb5_get_err_text(context, problem)); | |
730 | if (creds.client) | |
731 | krb5_free_principal(context, creds.client); | |
732 | if (creds.server) | |
733 | krb5_free_principal(context, creds.server); | |
734 | if (ccache) | |
735 | krb5_cc_close(context, ccache); | |
736 | if (outbuf.data) | |
737 | xfree(outbuf.data); | |
738 | } | |
739 | #endif /* KRB5 */ | |
740 | ||
741 | #ifdef AFS | |
742 | static void | |
743 | send_krb4_tgt(void) | |
744 | { | |
745 | CREDENTIALS *creds; | |
746 | struct stat st; | |
747 | char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ]; | |
1e608e42 | 748 | int problem, type; |
749 | ||
3c0ef626 | 750 | /* Don't do anything if we don't have any tickets. */ |
751 | if (stat(tkt_string(), &st) < 0) | |
752 | return; | |
1e608e42 | 753 | |
3c0ef626 | 754 | creds = xmalloc(sizeof(*creds)); |
1e608e42 | 755 | |
3c0ef626 | 756 | problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm); |
757 | if (problem) | |
758 | goto out; | |
1e608e42 | 759 | |
3c0ef626 | 760 | problem = krb_get_cred("krbtgt", prealm, prealm, creds); |
761 | if (problem) | |
762 | goto out; | |
1e608e42 | 763 | |
3c0ef626 | 764 | if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) { |
765 | problem = RD_AP_EXP; | |
766 | goto out; | |
767 | } | |
768 | creds_to_radix(creds, (u_char *)buffer, sizeof(buffer)); | |
1e608e42 | 769 | |
3c0ef626 | 770 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
771 | packet_put_cstring(buffer); | |
772 | packet_send(); | |
773 | packet_write_wait(); | |
1e608e42 | 774 | |
775 | type = packet_read(); | |
776 | ||
3c0ef626 | 777 | if (type == SSH_SMSG_SUCCESS) |
778 | debug("Kerberos v4 TGT forwarded (%s%s%s@%s).", | |
779 | creds->pname, creds->pinst[0] ? "." : "", | |
780 | creds->pinst, creds->realm); | |
781 | else | |
782 | debug("Kerberos v4 TGT rejected."); | |
1e608e42 | 783 | |
3c0ef626 | 784 | xfree(creds); |
785 | return; | |
1e608e42 | 786 | |
3c0ef626 | 787 | out: |
788 | debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]); | |
789 | xfree(creds); | |
790 | } | |
791 | ||
792 | static void | |
793 | send_afs_tokens(void) | |
794 | { | |
795 | CREDENTIALS creds; | |
796 | struct ViceIoctl parms; | |
797 | struct ClearToken ct; | |
798 | int i, type, len; | |
799 | char buf[2048], *p, *server_cell; | |
800 | char buffer[8192]; | |
1e608e42 | 801 | |
3c0ef626 | 802 | /* Move over ktc_GetToken, here's something leaner. */ |
803 | for (i = 0; i < 100; i++) { /* just in case */ | |
804 | parms.in = (char *) &i; | |
805 | parms.in_size = sizeof(i); | |
806 | parms.out = buf; | |
807 | parms.out_size = sizeof(buf); | |
808 | if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) | |
809 | break; | |
810 | p = buf; | |
1e608e42 | 811 | |
3c0ef626 | 812 | /* Get secret token. */ |
813 | memcpy(&creds.ticket_st.length, p, sizeof(u_int)); | |
814 | if (creds.ticket_st.length > MAX_KTXT_LEN) | |
815 | break; | |
816 | p += sizeof(u_int); | |
817 | memcpy(creds.ticket_st.dat, p, creds.ticket_st.length); | |
818 | p += creds.ticket_st.length; | |
1e608e42 | 819 | |
3c0ef626 | 820 | /* Get clear token. */ |
821 | memcpy(&len, p, sizeof(len)); | |
822 | if (len != sizeof(struct ClearToken)) | |
823 | break; | |
824 | p += sizeof(len); | |
825 | memcpy(&ct, p, len); | |
826 | p += len; | |
827 | p += sizeof(len); /* primary flag */ | |
828 | server_cell = p; | |
1e608e42 | 829 | |
3c0ef626 | 830 | /* Flesh out our credentials. */ |
831 | strlcpy(creds.service, "afs", sizeof(creds.service)); | |
832 | creds.instance[0] = '\0'; | |
833 | strlcpy(creds.realm, server_cell, REALM_SZ); | |
834 | memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ); | |
835 | creds.issue_date = ct.BeginTimestamp; | |
836 | creds.lifetime = krb_time_to_life(creds.issue_date, | |
837 | ct.EndTimestamp); | |
838 | creds.kvno = ct.AuthHandle; | |
839 | snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId); | |
840 | creds.pinst[0] = '\0'; | |
1e608e42 | 841 | |
3c0ef626 | 842 | /* Encode token, ship it off. */ |
843 | if (creds_to_radix(&creds, (u_char *)buffer, | |
844 | sizeof(buffer)) <= 0) | |
845 | break; | |
846 | packet_start(SSH_CMSG_HAVE_AFS_TOKEN); | |
847 | packet_put_cstring(buffer); | |
848 | packet_send(); | |
849 | packet_write_wait(); | |
850 | ||
851 | /* Roger, Roger. Clearance, Clarence. What's your vector, | |
852 | Victor? */ | |
1e608e42 | 853 | type = packet_read(); |
854 | ||
3c0ef626 | 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 | } | |
860 | } | |
861 | ||
862 | #endif /* AFS */ | |
863 | ||
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 | static int | |
869 | try_challenge_response_authentication(void) | |
870 | { | |
871 | int type, i; | |
3c0ef626 | 872 | u_int clen; |
873 | char prompt[1024]; | |
874 | char *challenge, *response; | |
875 | ||
876 | debug("Doing challenge response authentication."); | |
877 | ||
878 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
879 | /* request a challenge */ | |
880 | packet_start(SSH_CMSG_AUTH_TIS); | |
881 | packet_send(); | |
882 | packet_write_wait(); | |
883 | ||
1e608e42 | 884 | type = packet_read(); |
3c0ef626 | 885 | if (type != SSH_SMSG_FAILURE && |
886 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
887 | packet_disconnect("Protocol error: got %d in response " | |
888 | "to SSH_CMSG_AUTH_TIS", type); | |
889 | } | |
890 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
891 | debug("No challenge."); | |
892 | return 0; | |
893 | } | |
894 | challenge = packet_get_string(&clen); | |
1e608e42 | 895 | packet_check_eom(); |
3c0ef626 | 896 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
1e608e42 | 897 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
3c0ef626 | 898 | xfree(challenge); |
899 | if (i != 0) | |
900 | error("Permission denied, please try again."); | |
901 | if (options.cipher == SSH_CIPHER_NONE) | |
902 | log("WARNING: Encryption is disabled! " | |
903 | "Reponse will be transmitted in clear text."); | |
904 | response = read_passphrase(prompt, 0); | |
905 | if (strcmp(response, "") == 0) { | |
906 | xfree(response); | |
907 | break; | |
908 | } | |
909 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); | |
910 | ssh_put_password(response); | |
911 | memset(response, 0, strlen(response)); | |
912 | xfree(response); | |
913 | packet_send(); | |
914 | packet_write_wait(); | |
1e608e42 | 915 | type = packet_read(); |
3c0ef626 | 916 | if (type == SSH_SMSG_SUCCESS) |
917 | return 1; | |
918 | if (type != SSH_SMSG_FAILURE) | |
919 | packet_disconnect("Protocol error: got %d in response " | |
920 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); | |
921 | } | |
922 | /* failure */ | |
923 | return 0; | |
924 | } | |
925 | ||
926 | /* | |
927 | * Tries to authenticate with plain passwd authentication. | |
928 | */ | |
929 | static int | |
930 | try_password_authentication(char *prompt) | |
931 | { | |
1e608e42 | 932 | int type, i; |
3c0ef626 | 933 | char *password; |
934 | ||
935 | debug("Doing password authentication."); | |
936 | if (options.cipher == SSH_CIPHER_NONE) | |
937 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); | |
938 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
939 | if (i != 0) | |
940 | error("Permission denied, please try again."); | |
941 | password = read_passphrase(prompt, 0); | |
942 | packet_start(SSH_CMSG_AUTH_PASSWORD); | |
943 | ssh_put_password(password); | |
944 | memset(password, 0, strlen(password)); | |
945 | xfree(password); | |
946 | packet_send(); | |
947 | packet_write_wait(); | |
948 | ||
1e608e42 | 949 | type = packet_read(); |
3c0ef626 | 950 | if (type == SSH_SMSG_SUCCESS) |
951 | return 1; | |
952 | if (type != SSH_SMSG_FAILURE) | |
953 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); | |
954 | } | |
955 | /* failure */ | |
956 | return 0; | |
957 | } | |
958 | ||
0b582e46 | 959 | #ifdef GSSAPI |
960 | /* | |
961 | * This code stolen from the gss-client.c sample program from MIT's | |
962 | * kerberos 5 distribution. | |
963 | */ | |
964 | ||
965 | gss_cred_id_t gss_cred = GSS_C_NO_CREDENTIAL; | |
966 | ||
d325eeb3 | 967 | void packet_get_all(void) |
968 | { | |
969 | buffer_clear(&incoming_packet); | |
970 | } | |
971 | ||
0b582e46 | 972 | static void display_status_1(m, code, type) |
973 | char *m; | |
974 | OM_uint32 code; | |
975 | int type; | |
976 | { | |
977 | OM_uint32 maj_stat, min_stat; | |
978 | gss_buffer_desc msg; | |
979 | OM_uint32 msg_ctx; | |
980 | ||
981 | msg_ctx = 0; | |
982 | while (1) { | |
983 | maj_stat = gss_display_status(&min_stat, code, | |
984 | type, GSS_C_NULL_OID, | |
985 | &msg_ctx, &msg); | |
986 | debug("GSS-API error %s: %s", m, (char *)msg.value); | |
987 | (void) gss_release_buffer(&min_stat, &msg); | |
988 | ||
989 | if (!msg_ctx) | |
990 | break; | |
991 | } | |
992 | } | |
993 | ||
994 | static void display_gssapi_status(msg, maj_stat, min_stat) | |
995 | char *msg; | |
996 | OM_uint32 maj_stat; | |
997 | OM_uint32 min_stat; | |
998 | { | |
999 | display_status_1(msg, maj_stat, GSS_C_GSS_CODE); | |
1000 | display_status_1(msg, min_stat, GSS_C_MECH_CODE); | |
1001 | } | |
1002 | ||
1003 | #ifdef GSI | |
1004 | int get_gssapi_cred() | |
1005 | { | |
1006 | OM_uint32 maj_stat; | |
1007 | OM_uint32 min_stat; | |
1008 | ||
1009 | ||
1010 | debug("calling gss_acquire_cred"); | |
1011 | maj_stat = gss_acquire_cred(&min_stat, | |
1012 | GSS_C_NO_NAME, | |
1013 | GSS_C_INDEFINITE, | |
1014 | GSS_C_NO_OID_SET, | |
1015 | GSS_C_INITIATE, | |
1016 | &gss_cred, | |
1017 | NULL, | |
1018 | NULL); | |
1019 | ||
1020 | if (maj_stat != GSS_S_COMPLETE) { | |
1021 | display_gssapi_status("Failuring acquiring GSSAPI credentials", | |
1022 | maj_stat, min_stat); | |
1023 | gss_cred = GSS_C_NO_CREDENTIAL; /* should not be needed */ | |
1024 | return 0; | |
1025 | } | |
1026 | ||
1027 | return 1; /* Success */ | |
1028 | } | |
1029 | ||
1030 | char * get_gss_our_name() | |
1031 | { | |
1032 | OM_uint32 maj_stat; | |
1033 | OM_uint32 min_stat; | |
1034 | gss_name_t pname = GSS_C_NO_NAME; | |
1035 | gss_buffer_desc tmpname; | |
1036 | gss_buffer_t tmpnamed = &tmpname; | |
1037 | char *retname; | |
1038 | ||
1039 | debug("calling gss_inquire_cred"); | |
1040 | maj_stat = gss_inquire_cred(&min_stat, | |
1041 | gss_cred, | |
1042 | &pname, | |
1043 | NULL, | |
1044 | NULL, | |
1045 | NULL); | |
1046 | if (maj_stat != GSS_S_COMPLETE) { | |
1047 | return NULL; | |
1048 | } | |
1049 | ||
1050 | maj_stat = gss_export_name(&min_stat, | |
1051 | pname, | |
1052 | tmpnamed); | |
1053 | if (maj_stat != GSS_S_COMPLETE) { | |
1054 | return NULL; | |
1055 | } | |
1056 | debug("gss_export_name finsished"); | |
1057 | retname = (char *)malloc(tmpname.length + 1); | |
1058 | if (!retname) { | |
1059 | return NULL; | |
1060 | } | |
1061 | memcpy(retname, tmpname.value, tmpname.length); | |
1062 | retname[tmpname.length] = '\0'; | |
1063 | ||
1064 | gss_release_name(&min_stat, &pname); | |
1065 | gss_release_buffer(&min_stat, tmpnamed); | |
1066 | ||
1067 | return retname; | |
1068 | } | |
1069 | #endif /* GSI */ | |
1070 | ||
1071 | int try_gssapi_authentication(char *host, Options *options) | |
1072 | { | |
1073 | char *service_name = NULL; | |
1074 | gss_buffer_desc name_tok; | |
1075 | gss_buffer_desc send_tok; | |
1076 | gss_buffer_desc recv_tok; | |
1077 | gss_buffer_desc *token_ptr; | |
1078 | gss_name_t target_name = NULL; | |
1079 | gss_ctx_id_t gss_context; | |
1080 | gss_OID_desc mech_oid; | |
1081 | gss_OID name_type; | |
1082 | gss_OID_set my_mechs; | |
1083 | int my_mech_num; | |
1084 | OM_uint32 maj_stat; | |
1085 | OM_uint32 min_stat; | |
1086 | int ret_stat = 0; /* 1 == success */ | |
1087 | OM_uint32 req_flags = 0; | |
1088 | OM_uint32 ret_flags; | |
1089 | int type; | |
1090 | char *gssapi_auth_type = NULL; | |
1091 | struct hostent *hostinfo; | |
0b582e46 | 1092 | |
1093 | ||
1094 | /* | |
1095 | * host is not guarenteed to be a FQDN, so we need to make sure it is. | |
1096 | */ | |
1097 | hostinfo = gethostbyname(host); | |
1098 | ||
1099 | if ((hostinfo == NULL) || (hostinfo->h_name == NULL)) { | |
1100 | debug("GSSAPI authentication: Unable to get FQDN for \"%s\"", host); | |
1101 | goto cleanup; | |
1102 | } | |
1103 | ||
1104 | /* | |
1105 | * Default flags | |
1106 | */ | |
1107 | req_flags |= GSS_C_REPLAY_FLAG; | |
1108 | ||
1109 | /* Do mutual authentication */ | |
1110 | req_flags |= GSS_C_MUTUAL_FLAG; | |
1111 | ||
3f726215 | 1112 | #ifdef KRB5 |
0b582e46 | 1113 | |
1114 | gssapi_auth_type = "GSSAPI/Kerberos 5"; | |
1115 | ||
3f726215 | 1116 | #endif |
0b582e46 | 1117 | |
1118 | #ifdef GSI | |
1119 | ||
1120 | gssapi_auth_type = "GSSAPI/GLOBUS"; | |
1121 | ||
1122 | #endif /* GSI */ | |
1123 | ||
1124 | if (gssapi_auth_type == NULL) { | |
1125 | debug("No GSSAPI type defined during compile"); | |
1126 | goto cleanup; | |
1127 | } | |
1128 | ||
1129 | debug("Attempting %s authentication", gssapi_auth_type); | |
1130 | ||
1aad58f1 | 1131 | service_name = (char *) malloc(strlen("host") + |
0b582e46 | 1132 | strlen(hostinfo->h_name) + |
1133 | 2 /* 1 for '@', 1 for NUL */); | |
1134 | ||
1135 | if (service_name == NULL) { | |
1136 | debug("malloc() failed"); | |
1137 | goto cleanup; | |
1138 | } | |
1139 | ||
1140 | ||
1aad58f1 | 1141 | sprintf(service_name, "host@%s", hostinfo->h_name); |
0b582e46 | 1142 | |
1143 | name_type = GSS_C_NT_HOSTBASED_SERVICE; | |
1144 | ||
1145 | debug("Service name is %s", service_name); | |
1146 | ||
1147 | /* Forward credentials? */ | |
1148 | ||
3f726215 | 1149 | #ifdef KRB5 |
0b582e46 | 1150 | if (options->kerberos_tgt_passing) { |
1151 | debug("Forwarding Kerberos credentials"); | |
1152 | req_flags |= GSS_C_DELEG_FLAG; | |
1153 | } | |
3f726215 | 1154 | #endif /* KRB5 */ |
0b582e46 | 1155 | |
d0fc8d74 | 1156 | #ifdef GSSAPI |
1157 | if(options->gss_deleg_creds) { | |
0b582e46 | 1158 | debug("Forwarding X509 proxy certificate"); |
1159 | req_flags |= GSS_C_DELEG_FLAG; | |
1160 | } | |
1161 | #ifdef GSS_C_GLOBUS_LIMITED_DELEG_PROXY_FLAG | |
d0fc8d74 | 1162 | /* Forward limited credentials, overrides gss_deleg_creds */ |
1163 | if(options->gss_globus_deleg_limited_proxy) { | |
0b582e46 | 1164 | debug("Forwarding limited X509 proxy certificate"); |
1165 | req_flags |= (GSS_C_DELEG_FLAG | GSS_C_GLOBUS_LIMITED_DELEG_PROXY_FLAG); | |
1166 | } | |
1167 | #endif /* GSS_C_GLOBUS_LIMITED_DELEG_PROXY_FLAG */ | |
1168 | ||
d0fc8d74 | 1169 | #endif /* GSSAPI */ |
0b582e46 | 1170 | |
1171 | debug("req_flags = %lu", req_flags); | |
1172 | ||
1173 | name_tok.value = service_name; | |
1174 | name_tok.length = strlen(service_name) + 1; | |
1175 | maj_stat = gss_import_name(&min_stat, &name_tok, | |
1176 | name_type, &target_name); | |
1177 | ||
1178 | free(service_name); | |
1179 | service_name = NULL; | |
1180 | ||
1181 | if (maj_stat != GSS_S_COMPLETE) { | |
1182 | display_gssapi_status("importing service name", maj_stat, min_stat); | |
1183 | goto cleanup; | |
1184 | } | |
1185 | ||
1186 | maj_stat = gss_indicate_mechs(&min_stat, &my_mechs); | |
1187 | ||
1188 | if (maj_stat != GSS_S_COMPLETE) { | |
1189 | display_gssapi_status("indicating mechs", maj_stat, min_stat); | |
1190 | goto cleanup; | |
1191 | } | |
1192 | ||
1193 | /* | |
1194 | * Send over a packet to the daemon, letting it know we're doing | |
1195 | * GSSAPI and our mech_oid(s). | |
1196 | */ | |
1197 | debug("Sending mech oid to server"); | |
1198 | packet_start(SSH_CMSG_AUTH_GSSAPI); | |
1199 | packet_put_int(my_mechs->count); /* Number of mechs we're sending */ | |
1200 | for (my_mech_num = 0; my_mech_num < my_mechs->count; my_mech_num++) | |
1201 | packet_put_string(my_mechs->elements[my_mech_num].elements, | |
1202 | my_mechs->elements[my_mech_num].length); | |
1203 | packet_send(); | |
1204 | packet_write_wait(); | |
1205 | ||
1206 | /* | |
1207 | * Get reply from the daemon to see if our mech was acceptable | |
1208 | */ | |
1e608e42 | 1209 | type = packet_read(); |
0b582e46 | 1210 | |
1211 | switch (type) { | |
1212 | case SSH_SMSG_AUTH_GSSAPI_RESPONSE: | |
1213 | debug("Server accepted mechanism"); | |
1214 | /* Successful negotiation */ | |
1215 | break; | |
1216 | ||
1217 | case SSH_MSG_AUTH_GSSAPI_ABORT: | |
1218 | debug("Unable to negotiate GSSAPI mechanism type with server"); | |
1219 | packet_get_all(); | |
1220 | goto cleanup; | |
1221 | ||
1222 | default: | |
1223 | packet_disconnect("Protocol error during GSSAPI authentication:" | |
1224 | " packet type %d received", | |
1225 | type); | |
1226 | /* Does not return */ | |
1227 | } | |
1228 | ||
1229 | /* Read the mechanism the server returned */ | |
1230 | mech_oid.elements = packet_get_string((unsigned int *) &(mech_oid.length)); | |
1231 | packet_get_all(); | |
1232 | ||
1233 | /* | |
1234 | * Perform the context-establishement loop. | |
1235 | * | |
1236 | * On each pass through the loop, token_ptr points to the token | |
1237 | * to send to the server (or GSS_C_NO_BUFFER on the first pass). | |
1238 | * Every generated token is stored in send_tok which is then | |
1239 | * transmitted to the server; every received token is stored in | |
1240 | * recv_tok, which token_ptr is then set to, to be processed by | |
1241 | * the next call to gss_init_sec_context. | |
1242 | * | |
1243 | * GSS-API guarantees that send_tok's length will be non-zero | |
1244 | * if and only if the server is expecting another token from us, | |
1245 | * and that gss_init_sec_context returns GSS_S_CONTINUE_NEEDED if | |
1246 | * and only if the server has another token to send us. | |
1247 | */ | |
1248 | ||
1249 | token_ptr = GSS_C_NO_BUFFER; | |
1250 | gss_context = GSS_C_NO_CONTEXT; | |
1251 | ||
1252 | do { | |
1253 | maj_stat = | |
1254 | gss_init_sec_context(&min_stat, | |
1255 | gss_cred, | |
1256 | &gss_context, | |
1257 | target_name, | |
1258 | &mech_oid, | |
1259 | req_flags, | |
1260 | 0, | |
1261 | NULL, /* no channel bindings */ | |
1262 | token_ptr, | |
1263 | NULL, /* ignore mech type */ | |
1264 | &send_tok, | |
1265 | &ret_flags, | |
1266 | NULL); /* ignore time_rec */ | |
1267 | ||
1268 | if (token_ptr != GSS_C_NO_BUFFER) | |
1269 | (void) gss_release_buffer(&min_stat, &recv_tok); | |
1270 | ||
1271 | if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) { | |
1272 | display_gssapi_status("initializing context", maj_stat, min_stat); | |
1273 | ||
1274 | /* Send an abort message */ | |
1275 | packet_start(SSH_MSG_AUTH_GSSAPI_ABORT); | |
1276 | packet_send(); | |
1277 | packet_write_wait(); | |
1278 | ||
1279 | goto cleanup; | |
1280 | } | |
1281 | ||
1282 | if (send_tok.length != 0) { | |
1283 | debug("Sending authenticaton token..."); | |
1284 | packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN); | |
1285 | packet_put_string((char *) send_tok.value, send_tok.length); | |
1286 | packet_send(); | |
1287 | packet_write_wait(); | |
1288 | ||
1289 | (void) gss_release_buffer(&min_stat, &send_tok); | |
1290 | } | |
1291 | ||
1292 | if (maj_stat == GSS_S_CONTINUE_NEEDED) { | |
1293 | ||
1294 | debug("Continue needed. Reading response..."); | |
1295 | ||
1e608e42 | 1296 | type = packet_read(); |
0b582e46 | 1297 | |
1298 | switch(type) { | |
1299 | ||
1300 | case SSH_MSG_AUTH_GSSAPI_TOKEN: | |
1301 | /* This is what we expected */ | |
1302 | break; | |
1303 | ||
1304 | case SSH_MSG_AUTH_GSSAPI_ABORT: | |
1305 | debug("Server aborted GSSAPI authentication."); | |
1306 | packet_get_all(); | |
1307 | goto cleanup; | |
1308 | ||
1309 | default: | |
1310 | packet_disconnect("Protocol error during GSSAPI authentication:" | |
1311 | " packet type %d received", | |
1312 | type); | |
1313 | /* Does not return */ | |
1314 | } | |
1315 | ||
1316 | recv_tok.value = packet_get_string((unsigned int *) &recv_tok.length); | |
1317 | packet_get_all(); | |
1318 | token_ptr = &recv_tok; | |
1319 | } | |
1320 | } while (maj_stat == GSS_S_CONTINUE_NEEDED); | |
1321 | ||
1322 | /* Success */ | |
1323 | ret_stat = 1; | |
1324 | ||
1325 | debug("%s authentication successful", gssapi_auth_type); | |
1326 | ||
1327 | /* | |
1328 | * Read hash of host and server keys and make sure it | |
1329 | * matches what we got earlier. | |
1330 | */ | |
1331 | debug("Reading hash of server and host keys..."); | |
1e608e42 | 1332 | type = packet_read(); |
0b582e46 | 1333 | |
1334 | if (type == SSH_MSG_AUTH_GSSAPI_ABORT) { | |
1335 | debug("Server aborted GSSAPI authentication."); | |
1336 | packet_get_all(); | |
1337 | ret_stat = 0; | |
1338 | goto cleanup; | |
1339 | ||
1340 | } else if (type == SSH_SMSG_AUTH_GSSAPI_HASH) { | |
1341 | gss_buffer_desc wrapped_buf; | |
1342 | gss_buffer_desc unwrapped_buf; | |
1343 | int conf_state; | |
1344 | gss_qop_t qop_state; | |
1345 | ||
1346 | ||
1347 | wrapped_buf.value = packet_get_string(&(wrapped_buf.length)); | |
1348 | packet_get_all(); | |
1349 | ||
1350 | maj_stat = gss_unwrap(&min_stat, | |
1351 | gss_context, | |
1352 | &wrapped_buf, | |
1353 | &unwrapped_buf, | |
1354 | &conf_state, | |
1355 | &qop_state); | |
1356 | ||
1357 | if (maj_stat != GSS_S_COMPLETE) { | |
1358 | display_gssapi_status("unwraping SSHD key hash", | |
1359 | maj_stat, min_stat); | |
1360 | packet_disconnect("Verification of SSHD keys through GSSAPI-secured channel failed: " | |
1361 | "Unwrapping of hash failed."); | |
1362 | } | |
1363 | ||
1364 | if (unwrapped_buf.length != sizeof(ssh_key_digest)) { | |
1365 | packet_disconnect("Verification of SSHD keys through GSSAPI-secured channel failed: " | |
1366 | "Size of key hashes do not match (%d != %d)!", | |
1367 | unwrapped_buf.length, sizeof(ssh_key_digest)); | |
1368 | } | |
1369 | ||
1370 | if (memcmp(ssh_key_digest, unwrapped_buf.value, sizeof(ssh_key_digest)) != 0) { | |
1371 | packet_disconnect("Verification of SSHD keys through GSSAPI-secured channel failed: " | |
1372 | "Hashes don't match!"); | |
1373 | } | |
1374 | ||
1375 | debug("Verified SSHD keys through GSSAPI-secured channel."); | |
1376 | ||
1377 | gss_release_buffer(&min_stat, &unwrapped_buf); | |
1378 | ||
1379 | } else { | |
1380 | packet_disconnect("Protocol error during GSSAPI authentication:" | |
1381 | "packet type %d received", type); | |
1382 | /* Does not return */ | |
1383 | } | |
1384 | ||
1385 | ||
1386 | cleanup: | |
1387 | if (target_name != NULL) | |
1388 | (void) gss_release_name(&min_stat, &target_name); | |
1389 | ||
1390 | return ret_stat; | |
1391 | } | |
1392 | ||
1393 | #endif /* GSSAPI */ | |
1394 | ||
0b582e46 | 1395 | |
3c0ef626 | 1396 | /* |
1397 | * SSH1 key exchange | |
1398 | */ | |
1399 | void | |
1400 | ssh_kex(char *host, struct sockaddr *hostaddr) | |
1401 | { | |
1402 | int i; | |
1403 | BIGNUM *key; | |
1e608e42 | 1404 | Key *host_key, *server_key; |
3c0ef626 | 1405 | int bits, rbits; |
1406 | int ssh_cipher_default = SSH_CIPHER_3DES; | |
1407 | u_char session_key[SSH_SESSION_KEY_LENGTH]; | |
1408 | u_char cookie[8]; | |
1409 | u_int supported_ciphers; | |
1410 | u_int server_flags, client_flags; | |
3c0ef626 | 1411 | u_int32_t rand = 0; |
1412 | ||
1413 | debug("Waiting for server public key."); | |
1414 | ||
1415 | /* Wait for a public key packet from the server. */ | |
1e608e42 | 1416 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
3c0ef626 | 1417 | |
1418 | /* Get cookie from the packet. */ | |
1419 | for (i = 0; i < 8; i++) | |
1420 | cookie[i] = packet_get_char(); | |
1421 | ||
1422 | /* Get the public key. */ | |
1e608e42 | 1423 | server_key = key_new(KEY_RSA1); |
1424 | bits = packet_get_int(); | |
1425 | packet_get_bignum(server_key->rsa->e); | |
1426 | packet_get_bignum(server_key->rsa->n); | |
1427 | ||
1428 | rbits = BN_num_bits(server_key->rsa->n); | |
3c0ef626 | 1429 | if (bits != rbits) { |
1430 | log("Warning: Server lies about size of server public key: " | |
1431 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
1432 | log("Warning: This may be due to an old implementation of ssh."); | |
1433 | } | |
1434 | /* Get the host key. */ | |
1e608e42 | 1435 | host_key = key_new(KEY_RSA1); |
1436 | bits = packet_get_int(); | |
1437 | packet_get_bignum(host_key->rsa->e); | |
1438 | packet_get_bignum(host_key->rsa->n); | |
1439 | ||
1440 | rbits = BN_num_bits(host_key->rsa->n); | |
3c0ef626 | 1441 | if (bits != rbits) { |
1442 | log("Warning: Server lies about size of server host key: " | |
1443 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
1444 | log("Warning: This may be due to an old implementation of ssh."); | |
1445 | } | |
1446 | ||
0b582e46 | 1447 | #ifdef GSSAPI |
1448 | { | |
1449 | MD5_CTX md5context; | |
1450 | Buffer buf; | |
1451 | unsigned char *data; | |
1452 | unsigned int data_len; | |
1453 | ||
1454 | /* | |
1455 | * Hash the server and host keys. Later we will check them against | |
1456 | * a hash sent over a secure channel to make sure they are legit. | |
1457 | */ | |
1458 | debug("Calculating MD5 hash of server and host keys..."); | |
1459 | ||
1460 | /* Write all the keys to a temporary buffer */ | |
1461 | buffer_init(&buf); | |
1462 | ||
1463 | /* Server key */ | |
1e608e42 | 1464 | buffer_put_bignum(&buf, server_key->rsa->e); |
1465 | buffer_put_bignum(&buf, server_key->rsa->n); | |
0b582e46 | 1466 | |
1467 | /* Host key */ | |
1e608e42 | 1468 | buffer_put_bignum(&buf, host_key->rsa->e); |
1469 | buffer_put_bignum(&buf, host_key->rsa->n); | |
0b582e46 | 1470 | |
1471 | /* Get the resulting data */ | |
1472 | data = (unsigned char *) buffer_ptr(&buf); | |
1473 | data_len = buffer_len(&buf); | |
1474 | ||
1475 | /* And hash it */ | |
1476 | MD5_Init(&md5context); | |
1477 | MD5_Update(&md5context, data, data_len); | |
1478 | MD5_Final(ssh_key_digest, &md5context); | |
1479 | ||
1480 | /* Clean up */ | |
1481 | buffer_clear(&buf); | |
1482 | buffer_free(&buf); | |
1483 | } | |
1484 | #endif /* GSSAPI */ | |
0b582e46 | 1485 | |
3c0ef626 | 1486 | /* Get protocol flags. */ |
1487 | server_flags = packet_get_int(); | |
1488 | packet_set_protocol_flags(server_flags); | |
1489 | ||
1490 | supported_ciphers = packet_get_int(); | |
1491 | supported_authentications = packet_get_int(); | |
1e608e42 | 1492 | packet_check_eom(); |
3c0ef626 | 1493 | |
1494 | debug("Received server public key (%d bits) and host key (%d bits).", | |
1e608e42 | 1495 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
1496 | ||
1497 | if (verify_host_key(host, hostaddr, host_key) == -1) | |
3c0ef626 | 1498 | fatal("Host key verification failed."); |
1499 | ||
1500 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; | |
1501 | ||
1e608e42 | 1502 | compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n); |
3c0ef626 | 1503 | |
1504 | /* Generate a session key. */ | |
1505 | arc4random_stir(); | |
1506 | ||
1507 | /* | |
1508 | * Generate an encryption key for the session. The key is a 256 bit | |
1509 | * random number, interpreted as a 32-byte key, with the least | |
1510 | * significant 8 bits being the first byte of the key. | |
1511 | */ | |
1512 | for (i = 0; i < 32; i++) { | |
1513 | if (i % 4 == 0) | |
1514 | rand = arc4random(); | |
1515 | session_key[i] = rand & 0xff; | |
1516 | rand >>= 8; | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * According to the protocol spec, the first byte of the session key | |
1521 | * is the highest byte of the integer. The session key is xored with | |
1522 | * the first 16 bytes of the session id. | |
1523 | */ | |
1e608e42 | 1524 | if ((key = BN_new()) == NULL) |
1525 | fatal("respond_to_rsa_challenge: BN_new failed"); | |
3c0ef626 | 1526 | BN_set_word(key, 0); |
1527 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
1528 | BN_lshift(key, key, 8); | |
1529 | if (i < 16) | |
1530 | BN_add_word(key, session_key[i] ^ session_id[i]); | |
1531 | else | |
1532 | BN_add_word(key, session_key[i]); | |
1533 | } | |
1534 | ||
1535 | /* | |
1536 | * Encrypt the integer using the public key and host key of the | |
1537 | * server (key with smaller modulus first). | |
1538 | */ | |
1e608e42 | 1539 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
3c0ef626 | 1540 | /* Public key has smaller modulus. */ |
1e608e42 | 1541 | if (BN_num_bits(host_key->rsa->n) < |
1542 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1543 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " | |
1544 | "SSH_KEY_BITS_RESERVED %d", | |
1545 | BN_num_bits(host_key->rsa->n), | |
1546 | BN_num_bits(server_key->rsa->n), | |
1547 | SSH_KEY_BITS_RESERVED); | |
3c0ef626 | 1548 | } |
1e608e42 | 1549 | rsa_public_encrypt(key, key, server_key->rsa); |
1550 | rsa_public_encrypt(key, key, host_key->rsa); | |
3c0ef626 | 1551 | } else { |
1552 | /* Host key has smaller modulus (or they are equal). */ | |
1e608e42 | 1553 | if (BN_num_bits(server_key->rsa->n) < |
1554 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1555 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " | |
1556 | "SSH_KEY_BITS_RESERVED %d", | |
1557 | BN_num_bits(server_key->rsa->n), | |
1558 | BN_num_bits(host_key->rsa->n), | |
1559 | SSH_KEY_BITS_RESERVED); | |
3c0ef626 | 1560 | } |
1e608e42 | 1561 | rsa_public_encrypt(key, key, host_key->rsa); |
1562 | rsa_public_encrypt(key, key, server_key->rsa); | |
3c0ef626 | 1563 | } |
1564 | ||
1565 | /* Destroy the public keys since we no longer need them. */ | |
1e608e42 | 1566 | key_free(server_key); |
1567 | key_free(host_key); | |
3c0ef626 | 1568 | |
1569 | if (options.cipher == SSH_CIPHER_NOT_SET) { | |
1570 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) | |
1571 | options.cipher = ssh_cipher_default; | |
1572 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || | |
1573 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { | |
1574 | log("No valid SSH1 cipher, using %.100s instead.", | |
1575 | cipher_name(ssh_cipher_default)); | |
1576 | options.cipher = ssh_cipher_default; | |
1577 | } | |
1578 | /* Check that the selected cipher is supported. */ | |
1579 | if (!(supported_ciphers & (1 << options.cipher))) | |
1580 | fatal("Selected cipher type %.100s not supported by server.", | |
1e608e42 | 1581 | cipher_name(options.cipher)); |
3c0ef626 | 1582 | |
1583 | debug("Encryption type: %.100s", cipher_name(options.cipher)); | |
1584 | ||
1585 | /* Send the encrypted session key to the server. */ | |
1586 | packet_start(SSH_CMSG_SESSION_KEY); | |
1587 | packet_put_char(options.cipher); | |
1588 | ||
1589 | /* Send the cookie back to the server. */ | |
1590 | for (i = 0; i < 8; i++) | |
1591 | packet_put_char(cookie[i]); | |
1592 | ||
1593 | /* Send and destroy the encrypted encryption key integer. */ | |
1594 | packet_put_bignum(key); | |
1595 | BN_clear_free(key); | |
1596 | ||
1597 | /* Send protocol flags. */ | |
1598 | packet_put_int(client_flags); | |
1599 | ||
1600 | /* Send the packet now. */ | |
1601 | packet_send(); | |
1602 | packet_write_wait(); | |
1603 | ||
1604 | debug("Sent encrypted session key."); | |
1605 | ||
1606 | /* Set the encryption key. */ | |
1607 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); | |
1608 | ||
1609 | /* We will no longer need the session key here. Destroy any extra copies. */ | |
1610 | memset(session_key, 0, sizeof(session_key)); | |
1611 | ||
1612 | /* | |
1613 | * Expect a success message from the server. Note that this message | |
1614 | * will be received in encrypted form. | |
1615 | */ | |
1e608e42 | 1616 | packet_read_expect(SSH_SMSG_SUCCESS); |
3c0ef626 | 1617 | |
1618 | debug("Received encrypted confirmation."); | |
1619 | } | |
1620 | ||
1621 | /* | |
1622 | * Authenticate user | |
1623 | */ | |
1624 | void | |
1625 | ssh_userauth1(const char *local_user, const char *server_user, char *host, | |
1626 | Key **keys, int nkeys) | |
1627 | { | |
0b582e46 | 1628 | #ifdef GSSAPI |
1629 | #ifdef GSI | |
1630 | const char *save_server_user = NULL; | |
1631 | #endif /* GSI */ | |
1632 | #endif /* GSSAPI */ | |
0b582e46 | 1633 | |
3c0ef626 | 1634 | #ifdef KRB5 |
1635 | krb5_context context = NULL; | |
1636 | krb5_auth_context auth_context = NULL; | |
1637 | #endif | |
1638 | int i, type; | |
1e608e42 | 1639 | |
3c0ef626 | 1640 | if (supported_authentications == 0) |
1641 | fatal("ssh_userauth1: server supports no auth methods"); | |
1642 | ||
0b582e46 | 1643 | #ifdef GSSAPI |
1644 | #ifdef GSI | |
1645 | /* if no user given, tack on the subject name after the server_user. | |
1646 | * This will allow us to run gridmap early to get real user | |
1647 | * This name will start with /C= | |
1648 | */ | |
1649 | if ((supported_authentications & (1 << SSH_AUTH_GSSAPI)) && | |
1650 | options.gss_authentication) { | |
1651 | if (get_gssapi_cred()) { | |
1652 | char * retname; | |
1653 | char * newname; | |
1654 | ||
1655 | ||
1656 | save_server_user = server_user; | |
1657 | ||
1658 | retname = get_gss_our_name(); | |
1659 | ||
1660 | if (retname) { | |
1661 | debug("passing gssapi name '%s'", retname); | |
1662 | if (server_user) { | |
1663 | newname = (char *) malloc(strlen(retname) + strlen(server_user) + 4); | |
1664 | if (newname) { | |
1665 | strcpy(newname, server_user); | |
1666 | if(options.user == NULL) | |
1667 | { | |
1668 | strcat(newname,":i:"); | |
1669 | } | |
1670 | else | |
1671 | { | |
1672 | strcat(newname,":x:"); | |
1673 | } | |
1674 | strcat(newname, retname); | |
1675 | server_user = newname; | |
1676 | free(retname); | |
1677 | } | |
1678 | } | |
1679 | } | |
1680 | } else { | |
1681 | /* | |
1682 | * If we couldn't successfully get our GSSAPI credentials then | |
1683 | * turn off gssapi authentication | |
1684 | */ | |
1685 | options.gss_authentication = 0; | |
1686 | } | |
1687 | debug("server_user %s", server_user); | |
1688 | } | |
1689 | #endif /* GSI */ | |
1690 | #endif /* GSSAPI */ | |
0b582e46 | 1691 | |
3c0ef626 | 1692 | /* Send the name of the user to log in as on the server. */ |
1693 | packet_start(SSH_CMSG_USER); | |
1694 | packet_put_cstring(server_user); | |
1695 | packet_send(); | |
1696 | packet_write_wait(); | |
1697 | ||
0b582e46 | 1698 | #if defined(GSI) |
1699 | if(save_server_user) | |
1700 | { | |
1701 | server_user = save_server_user; | |
1702 | } | |
1703 | #endif | |
3c0ef626 | 1704 | /* |
1705 | * The server should respond with success if no authentication is | |
1706 | * needed (the user has no password). Otherwise the server responds | |
1707 | * with failure. | |
1708 | */ | |
1e608e42 | 1709 | type = packet_read(); |
3c0ef626 | 1710 | |
1711 | /* check whether the connection was accepted without authentication. */ | |
1712 | if (type == SSH_SMSG_SUCCESS) | |
1713 | goto success; | |
1714 | if (type != SSH_SMSG_FAILURE) | |
1715 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); | |
0b582e46 | 1716 | |
0b582e46 | 1717 | #ifdef GSSAPI |
1718 | /* Try GSSAPI authentication */ | |
1719 | if ((supported_authentications & (1 << SSH_AUTH_GSSAPI)) && | |
1720 | options.gss_authentication) | |
1721 | { | |
1aad58f1 | 1722 | debug("Trying GSSAPI authentication..."); |
0b582e46 | 1723 | try_gssapi_authentication(host, &options); |
1724 | ||
1725 | /* | |
1726 | * XXX Hmmm. Kerberos authentication only reads a packet if it thinks | |
1727 | * the authentication went OK, but the server seems to always send | |
1728 | * a packet back. So I'm not sure if I'm missing something or | |
1729 | * the Kerberos code is broken. - vwelch 1/27/99 | |
1730 | */ | |
1731 | ||
1e608e42 | 1732 | type = packet_read(); |
0b582e46 | 1733 | if (type == SSH_SMSG_SUCCESS) |
1734 | return; /* Successful connection. */ | |
1735 | if (type != SSH_SMSG_FAILURE) | |
1736 | packet_disconnect("Protocol error: got %d in response to Kerberos auth", type); | |
1737 | ||
1738 | debug("GSSAPI authentication failed"); | |
1739 | } | |
1740 | #endif /* GSSAPI */ | |
3c0ef626 | 1741 | |
1742 | #ifdef KRB5 | |
1743 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
1e608e42 | 1744 | options.kerberos_authentication) { |
3c0ef626 | 1745 | debug("Trying Kerberos v5 authentication."); |
1e608e42 | 1746 | |
3c0ef626 | 1747 | if (try_krb5_authentication(&context, &auth_context)) { |
1e608e42 | 1748 | type = packet_read(); |
3c0ef626 | 1749 | if (type == SSH_SMSG_SUCCESS) |
1750 | goto success; | |
1751 | if (type != SSH_SMSG_FAILURE) | |
1752 | packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type); | |
1753 | } | |
1754 | } | |
1755 | #endif /* KRB5 */ | |
1e608e42 | 1756 | |
3c0ef626 | 1757 | #ifdef KRB4 |
1758 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
1759 | options.kerberos_authentication) { | |
1760 | debug("Trying Kerberos v4 authentication."); | |
1e608e42 | 1761 | |
3c0ef626 | 1762 | if (try_krb4_authentication()) { |
1e608e42 | 1763 | type = packet_read(); |
3c0ef626 | 1764 | if (type == SSH_SMSG_SUCCESS) |
1765 | goto success; | |
1766 | if (type != SSH_SMSG_FAILURE) | |
1767 | packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type); | |
1768 | } | |
1769 | } | |
1770 | #endif /* KRB4 */ | |
1e608e42 | 1771 | |
3c0ef626 | 1772 | /* |
1773 | * Use rhosts authentication if running in privileged socket and we | |
1774 | * do not wish to remain anonymous. | |
1775 | */ | |
1776 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && | |
1777 | options.rhosts_authentication) { | |
1778 | debug("Trying rhosts authentication."); | |
1779 | packet_start(SSH_CMSG_AUTH_RHOSTS); | |
1780 | packet_put_cstring(local_user); | |
1781 | packet_send(); | |
1782 | packet_write_wait(); | |
1783 | ||
1784 | /* The server should respond with success or failure. */ | |
1e608e42 | 1785 | type = packet_read(); |
3c0ef626 | 1786 | if (type == SSH_SMSG_SUCCESS) |
1787 | goto success; | |
1788 | if (type != SSH_SMSG_FAILURE) | |
1789 | packet_disconnect("Protocol error: got %d in response to rhosts auth", | |
1790 | type); | |
1791 | } | |
1792 | /* | |
1793 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host | |
1794 | * authentication. | |
1795 | */ | |
1796 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && | |
1797 | options.rhosts_rsa_authentication) { | |
1798 | for (i = 0; i < nkeys; i++) { | |
1799 | if (keys[i] != NULL && keys[i]->type == KEY_RSA1 && | |
1800 | try_rhosts_rsa_authentication(local_user, keys[i])) | |
1801 | goto success; | |
1802 | } | |
1803 | } | |
1804 | /* Try RSA authentication if the server supports it. */ | |
1805 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && | |
1806 | options.rsa_authentication) { | |
1807 | /* | |
1808 | * Try RSA authentication using the authentication agent. The | |
1809 | * agent is tried first because no passphrase is needed for | |
1810 | * it, whereas identity files may require passphrases. | |
1811 | */ | |
1812 | if (try_agent_authentication()) | |
1813 | goto success; | |
1814 | ||
1815 | /* Try RSA authentication for each identity. */ | |
1816 | for (i = 0; i < options.num_identity_files; i++) | |
1817 | if (options.identity_keys[i] != NULL && | |
1818 | options.identity_keys[i]->type == KEY_RSA1 && | |
1819 | try_rsa_authentication(i)) | |
1820 | goto success; | |
1821 | } | |
1822 | /* Try challenge response authentication if the server supports it. */ | |
1823 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && | |
1824 | options.challenge_response_authentication && !options.batch_mode) { | |
1825 | if (try_challenge_response_authentication()) | |
1826 | goto success; | |
1827 | } | |
1828 | /* Try password authentication if the server supports it. */ | |
1829 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && | |
1830 | options.password_authentication && !options.batch_mode) { | |
1831 | char prompt[80]; | |
1832 | ||
1833 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", | |
1834 | server_user, host); | |
1835 | if (try_password_authentication(prompt)) | |
1836 | goto success; | |
1837 | } | |
1838 | /* All authentication methods have failed. Exit with an error message. */ | |
1839 | fatal("Permission denied."); | |
1840 | /* NOTREACHED */ | |
1841 | ||
1842 | success: | |
1843 | #ifdef KRB5 | |
1844 | /* Try Kerberos v5 TGT passing. */ | |
1845 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
1846 | options.kerberos_tgt_passing && context && auth_context) { | |
1847 | if (options.cipher == SSH_CIPHER_NONE) | |
1848 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
1849 | send_krb5_tgt(context, auth_context); | |
1850 | } | |
1851 | if (auth_context) | |
1852 | krb5_auth_con_free(context, auth_context); | |
1853 | if (context) | |
1854 | krb5_free_context(context); | |
1855 | #endif | |
1e608e42 | 1856 | |
3c0ef626 | 1857 | #ifdef AFS |
1858 | /* Try Kerberos v4 TGT passing if the server supports it. */ | |
1859 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
1860 | options.kerberos_tgt_passing) { | |
1861 | if (options.cipher == SSH_CIPHER_NONE) | |
1862 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
1863 | send_krb4_tgt(); | |
1864 | } | |
1865 | /* Try AFS token passing if the server supports it. */ | |
1866 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && | |
1867 | options.afs_token_passing && k_hasafs()) { | |
1868 | if (options.cipher == SSH_CIPHER_NONE) | |
1869 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); | |
1870 | send_afs_tokens(); | |
1871 | } | |
1872 | #endif /* AFS */ | |
1873 | ||
1874 | return; /* need statement after label */ | |
1875 | } |