diff options
Diffstat (limited to 'sshconnect2.c')
-rw-r--r-- | sshconnect2.c | 573 |
1 files changed, 361 insertions, 212 deletions
diff --git a/sshconnect2.c b/sshconnect2.c index 68f7f4fdd..ba56f6433 100644 --- a/sshconnect2.c +++ b/sshconnect2.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sshconnect2.c,v 1.210 2014/07/15 15:54:14 millert Exp $ */ | 1 | /* $OpenBSD: sshconnect2.c,v 1.223 2015/01/30 11:43:14 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
4 | * Copyright (c) 2008 Damien Miller. All rights reserved. | 4 | * Copyright (c) 2008 Damien Miller. All rights reserved. |
@@ -70,6 +70,7 @@ | |||
70 | #include "pathnames.h" | 70 | #include "pathnames.h" |
71 | #include "uidswap.h" | 71 | #include "uidswap.h" |
72 | #include "hostfile.h" | 72 | #include "hostfile.h" |
73 | #include "ssherr.h" | ||
73 | 74 | ||
74 | #ifdef GSSAPI | 75 | #ifdef GSSAPI |
75 | #include "ssh-gss.h" | 76 | #include "ssh-gss.h" |
@@ -90,10 +91,8 @@ u_int session_id2_len = 0; | |||
90 | char *xxx_host; | 91 | char *xxx_host; |
91 | struct sockaddr *xxx_hostaddr; | 92 | struct sockaddr *xxx_hostaddr; |
92 | 93 | ||
93 | Kex *xxx_kex = NULL; | ||
94 | |||
95 | static int | 94 | static int |
96 | verify_host_key_callback(Key *hostkey) | 95 | verify_host_key_callback(Key *hostkey, struct ssh *ssh) |
97 | { | 96 | { |
98 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) | 97 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
99 | fatal("Host key verification failed."); | 98 | fatal("Host key verification failed."); |
@@ -131,16 +130,17 @@ order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port) | |||
131 | } while (0) | 130 | } while (0) |
132 | 131 | ||
133 | while ((alg = strsep(&avail, ",")) && *alg != '\0') { | 132 | while ((alg = strsep(&avail, ",")) && *alg != '\0') { |
134 | if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC) | 133 | if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC) |
135 | fatal("%s: unknown alg %s", __func__, alg); | 134 | fatal("%s: unknown alg %s", __func__, alg); |
136 | if (lookup_key_in_hostkeys_by_type(hostkeys, | 135 | if (lookup_key_in_hostkeys_by_type(hostkeys, |
137 | key_type_plain(ktype), NULL)) | 136 | sshkey_type_plain(ktype), NULL)) |
138 | ALG_APPEND(first, alg); | 137 | ALG_APPEND(first, alg); |
139 | else | 138 | else |
140 | ALG_APPEND(last, alg); | 139 | ALG_APPEND(last, alg); |
141 | } | 140 | } |
142 | #undef ALG_APPEND | 141 | #undef ALG_APPEND |
143 | xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last); | 142 | xasprintf(&ret, "%s%s%s", first, |
143 | (*first == '\0' || *last == '\0') ? "" : ",", last); | ||
144 | if (*first != '\0') | 144 | if (*first != '\0') |
145 | debug3("%s: prefer hostkeyalgs: %s", __func__, first); | 145 | debug3("%s: prefer hostkeyalgs: %s", __func__, first); |
146 | 146 | ||
@@ -157,7 +157,8 @@ void | |||
157 | ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) | 157 | ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) |
158 | { | 158 | { |
159 | char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; | 159 | char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; |
160 | Kex *kex; | 160 | struct kex *kex; |
161 | int r; | ||
161 | 162 | ||
162 | xxx_host = host; | 163 | xxx_host = host; |
163 | xxx_hostaddr = hostaddr; | 164 | xxx_hostaddr = hostaddr; |
@@ -204,22 +205,24 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) | |||
204 | (time_t)options.rekey_interval); | 205 | (time_t)options.rekey_interval); |
205 | 206 | ||
206 | /* start key exchange */ | 207 | /* start key exchange */ |
207 | kex = kex_setup(myproposal); | 208 | if ((r = kex_setup(active_state, myproposal)) != 0) |
209 | fatal("kex_setup: %s", ssh_err(r)); | ||
210 | kex = active_state->kex; | ||
208 | #ifdef WITH_OPENSSL | 211 | #ifdef WITH_OPENSSL |
209 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; | 212 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
210 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; | 213 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
211 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; | 214 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
212 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; | 215 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; |
216 | # ifdef OPENSSL_HAS_ECC | ||
213 | kex->kex[KEX_ECDH_SHA2] = kexecdh_client; | 217 | kex->kex[KEX_ECDH_SHA2] = kexecdh_client; |
218 | # endif | ||
214 | #endif | 219 | #endif |
215 | kex->kex[KEX_C25519_SHA256] = kexc25519_client; | 220 | kex->kex[KEX_C25519_SHA256] = kexc25519_client; |
216 | kex->client_version_string=client_version_string; | 221 | kex->client_version_string=client_version_string; |
217 | kex->server_version_string=server_version_string; | 222 | kex->server_version_string=server_version_string; |
218 | kex->verify_host_key=&verify_host_key_callback; | 223 | kex->verify_host_key=&verify_host_key_callback; |
219 | 224 | ||
220 | xxx_kex = kex; | 225 | dispatch_run(DISPATCH_BLOCK, &kex->done, active_state); |
221 | |||
222 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); | ||
223 | 226 | ||
224 | if (options.use_roaming && !kex->roaming) { | 227 | if (options.use_roaming && !kex->roaming) { |
225 | debug("Roaming not allowed by server"); | 228 | debug("Roaming not allowed by server"); |
@@ -242,15 +245,15 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) | |||
242 | * Authenticate user | 245 | * Authenticate user |
243 | */ | 246 | */ |
244 | 247 | ||
245 | typedef struct Authctxt Authctxt; | 248 | typedef struct cauthctxt Authctxt; |
246 | typedef struct Authmethod Authmethod; | 249 | typedef struct cauthmethod Authmethod; |
247 | typedef struct identity Identity; | 250 | typedef struct identity Identity; |
248 | typedef struct idlist Idlist; | 251 | typedef struct idlist Idlist; |
249 | 252 | ||
250 | struct identity { | 253 | struct identity { |
251 | TAILQ_ENTRY(identity) next; | 254 | TAILQ_ENTRY(identity) next; |
252 | AuthenticationConnection *ac; /* set if agent supports key */ | 255 | int agent_fd; /* >=0 if agent supports key */ |
253 | Key *key; /* public/private key */ | 256 | struct sshkey *key; /* public/private key */ |
254 | char *filename; /* comment for agent-only keys */ | 257 | char *filename; /* comment for agent-only keys */ |
255 | int tried; | 258 | int tried; |
256 | int isprivate; /* key points to the private key */ | 259 | int isprivate; /* key points to the private key */ |
@@ -258,25 +261,29 @@ struct identity { | |||
258 | }; | 261 | }; |
259 | TAILQ_HEAD(idlist, identity); | 262 | TAILQ_HEAD(idlist, identity); |
260 | 263 | ||
261 | struct Authctxt { | 264 | struct cauthctxt { |
262 | const char *server_user; | 265 | const char *server_user; |
263 | const char *local_user; | 266 | const char *local_user; |
264 | const char *host; | 267 | const char *host; |
265 | const char *service; | 268 | const char *service; |
266 | Authmethod *method; | 269 | struct cauthmethod *method; |
267 | sig_atomic_t success; | 270 | sig_atomic_t success; |
268 | char *authlist; | 271 | char *authlist; |
272 | int attempt; | ||
269 | /* pubkey */ | 273 | /* pubkey */ |
270 | Idlist keys; | 274 | struct idlist keys; |
271 | AuthenticationConnection *agent; | 275 | int agent_fd; |
272 | /* hostbased */ | 276 | /* hostbased */ |
273 | Sensitive *sensitive; | 277 | Sensitive *sensitive; |
278 | char *oktypes, *ktypes; | ||
279 | const char *active_ktype; | ||
274 | /* kbd-interactive */ | 280 | /* kbd-interactive */ |
275 | int info_req_seen; | 281 | int info_req_seen; |
276 | /* generic */ | 282 | /* generic */ |
277 | void *methoddata; | 283 | void *methoddata; |
278 | }; | 284 | }; |
279 | struct Authmethod { | 285 | |
286 | struct cauthmethod { | ||
280 | char *name; /* string to compare against server's list */ | 287 | char *name; /* string to compare against server's list */ |
281 | int (*userauth)(Authctxt *authctxt); | 288 | int (*userauth)(Authctxt *authctxt); |
282 | void (*cleanup)(Authctxt *authctxt); | 289 | void (*cleanup)(Authctxt *authctxt); |
@@ -284,14 +291,14 @@ struct Authmethod { | |||
284 | int *batch_flag; /* flag in option struct that disables method */ | 291 | int *batch_flag; /* flag in option struct that disables method */ |
285 | }; | 292 | }; |
286 | 293 | ||
287 | void input_userauth_success(int, u_int32_t, void *); | 294 | int input_userauth_success(int, u_int32_t, void *); |
288 | void input_userauth_success_unexpected(int, u_int32_t, void *); | 295 | int input_userauth_success_unexpected(int, u_int32_t, void *); |
289 | void input_userauth_failure(int, u_int32_t, void *); | 296 | int input_userauth_failure(int, u_int32_t, void *); |
290 | void input_userauth_banner(int, u_int32_t, void *); | 297 | int input_userauth_banner(int, u_int32_t, void *); |
291 | void input_userauth_error(int, u_int32_t, void *); | 298 | int input_userauth_error(int, u_int32_t, void *); |
292 | void input_userauth_info_req(int, u_int32_t, void *); | 299 | int input_userauth_info_req(int, u_int32_t, void *); |
293 | void input_userauth_pk_ok(int, u_int32_t, void *); | 300 | int input_userauth_pk_ok(int, u_int32_t, void *); |
294 | void input_userauth_passwd_changereq(int, u_int32_t, void *); | 301 | int input_userauth_passwd_changereq(int, u_int32_t, void *); |
295 | 302 | ||
296 | int userauth_none(Authctxt *); | 303 | int userauth_none(Authctxt *); |
297 | int userauth_pubkey(Authctxt *); | 304 | int userauth_pubkey(Authctxt *); |
@@ -301,11 +308,11 @@ int userauth_hostbased(Authctxt *); | |||
301 | 308 | ||
302 | #ifdef GSSAPI | 309 | #ifdef GSSAPI |
303 | int userauth_gssapi(Authctxt *authctxt); | 310 | int userauth_gssapi(Authctxt *authctxt); |
304 | void input_gssapi_response(int type, u_int32_t, void *); | 311 | int input_gssapi_response(int type, u_int32_t, void *); |
305 | void input_gssapi_token(int type, u_int32_t, void *); | 312 | int input_gssapi_token(int type, u_int32_t, void *); |
306 | void input_gssapi_hash(int type, u_int32_t, void *); | 313 | int input_gssapi_hash(int type, u_int32_t, void *); |
307 | void input_gssapi_error(int, u_int32_t, void *); | 314 | int input_gssapi_error(int, u_int32_t, void *); |
308 | void input_gssapi_errtok(int, u_int32_t, void *); | 315 | int input_gssapi_errtok(int, u_int32_t, void *); |
309 | #endif | 316 | #endif |
310 | 317 | ||
311 | void userauth(Authctxt *, char *); | 318 | void userauth(Authctxt *, char *); |
@@ -398,7 +405,9 @@ ssh_userauth2(const char *local_user, const char *server_user, char *host, | |||
398 | authctxt.authlist = NULL; | 405 | authctxt.authlist = NULL; |
399 | authctxt.methoddata = NULL; | 406 | authctxt.methoddata = NULL; |
400 | authctxt.sensitive = sensitive; | 407 | authctxt.sensitive = sensitive; |
408 | authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL; | ||
401 | authctxt.info_req_seen = 0; | 409 | authctxt.info_req_seen = 0; |
410 | authctxt.agent_fd = -1; | ||
402 | if (authctxt.method == NULL) | 411 | if (authctxt.method == NULL) |
403 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | 412 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); |
404 | 413 | ||
@@ -453,15 +462,16 @@ userauth(Authctxt *authctxt, char *authlist) | |||
453 | } | 462 | } |
454 | 463 | ||
455 | /* ARGSUSED */ | 464 | /* ARGSUSED */ |
456 | void | 465 | int |
457 | input_userauth_error(int type, u_int32_t seq, void *ctxt) | 466 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
458 | { | 467 | { |
459 | fatal("input_userauth_error: bad message during authentication: " | 468 | fatal("input_userauth_error: bad message during authentication: " |
460 | "type %d", type); | 469 | "type %d", type); |
470 | return 0; | ||
461 | } | 471 | } |
462 | 472 | ||
463 | /* ARGSUSED */ | 473 | /* ARGSUSED */ |
464 | void | 474 | int |
465 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) | 475 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
466 | { | 476 | { |
467 | char *msg, *raw, *lang; | 477 | char *msg, *raw, *lang; |
@@ -480,10 +490,11 @@ input_userauth_banner(int type, u_int32_t seq, void *ctxt) | |||
480 | } | 490 | } |
481 | free(raw); | 491 | free(raw); |
482 | free(lang); | 492 | free(lang); |
493 | return 0; | ||
483 | } | 494 | } |
484 | 495 | ||
485 | /* ARGSUSED */ | 496 | /* ARGSUSED */ |
486 | void | 497 | int |
487 | input_userauth_success(int type, u_int32_t seq, void *ctxt) | 498 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
488 | { | 499 | { |
489 | Authctxt *authctxt = ctxt; | 500 | Authctxt *authctxt = ctxt; |
@@ -497,9 +508,10 @@ input_userauth_success(int type, u_int32_t seq, void *ctxt) | |||
497 | free(authctxt->methoddata); | 508 | free(authctxt->methoddata); |
498 | authctxt->methoddata = NULL; | 509 | authctxt->methoddata = NULL; |
499 | authctxt->success = 1; /* break out */ | 510 | authctxt->success = 1; /* break out */ |
511 | return 0; | ||
500 | } | 512 | } |
501 | 513 | ||
502 | void | 514 | int |
503 | input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) | 515 | input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) |
504 | { | 516 | { |
505 | Authctxt *authctxt = ctxt; | 517 | Authctxt *authctxt = ctxt; |
@@ -509,10 +521,11 @@ input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) | |||
509 | 521 | ||
510 | fatal("Unexpected authentication success during %s.", | 522 | fatal("Unexpected authentication success during %s.", |
511 | authctxt->method->name); | 523 | authctxt->method->name); |
524 | return 0; | ||
512 | } | 525 | } |
513 | 526 | ||
514 | /* ARGSUSED */ | 527 | /* ARGSUSED */ |
515 | void | 528 | int |
516 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) | 529 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
517 | { | 530 | { |
518 | Authctxt *authctxt = ctxt; | 531 | Authctxt *authctxt = ctxt; |
@@ -535,10 +548,11 @@ input_userauth_failure(int type, u_int32_t seq, void *ctxt) | |||
535 | debug("Authentications that can continue: %s", authlist); | 548 | debug("Authentications that can continue: %s", authlist); |
536 | 549 | ||
537 | userauth(authctxt, authlist); | 550 | userauth(authctxt, authlist); |
551 | return 0; | ||
538 | } | 552 | } |
539 | 553 | ||
540 | /* ARGSUSED */ | 554 | /* ARGSUSED */ |
541 | void | 555 | int |
542 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) | 556 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
543 | { | 557 | { |
544 | Authctxt *authctxt = ctxt; | 558 | Authctxt *authctxt = ctxt; |
@@ -582,7 +596,9 @@ input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) | |||
582 | key->type, pktype); | 596 | key->type, pktype); |
583 | goto done; | 597 | goto done; |
584 | } | 598 | } |
585 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); | 599 | if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, |
600 | SSH_FP_DEFAULT)) == NULL) | ||
601 | goto done; | ||
586 | debug2("input_userauth_pk_ok: fp %s", fp); | 602 | debug2("input_userauth_pk_ok: fp %s", fp); |
587 | free(fp); | 603 | free(fp); |
588 | 604 | ||
@@ -606,6 +622,7 @@ done: | |||
606 | /* try another method if we did not send a packet */ | 622 | /* try another method if we did not send a packet */ |
607 | if (sent == 0) | 623 | if (sent == 0) |
608 | userauth(authctxt, NULL); | 624 | userauth(authctxt, NULL); |
625 | return 0; | ||
609 | } | 626 | } |
610 | 627 | ||
611 | #ifdef GSSAPI | 628 | #ifdef GSSAPI |
@@ -721,7 +738,7 @@ process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) | |||
721 | } | 738 | } |
722 | 739 | ||
723 | /* ARGSUSED */ | 740 | /* ARGSUSED */ |
724 | void | 741 | int |
725 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) | 742 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) |
726 | { | 743 | { |
727 | Authctxt *authctxt = ctxt; | 744 | Authctxt *authctxt = ctxt; |
@@ -742,7 +759,7 @@ input_gssapi_response(int type, u_int32_t plen, void *ctxt) | |||
742 | free(oidv); | 759 | free(oidv); |
743 | debug("Badly encoded mechanism OID received"); | 760 | debug("Badly encoded mechanism OID received"); |
744 | userauth(authctxt, NULL); | 761 | userauth(authctxt, NULL); |
745 | return; | 762 | return 0; |
746 | } | 763 | } |
747 | 764 | ||
748 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) | 765 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) |
@@ -756,12 +773,13 @@ input_gssapi_response(int type, u_int32_t plen, void *ctxt) | |||
756 | /* Start again with next method on list */ | 773 | /* Start again with next method on list */ |
757 | debug("Trying to start again"); | 774 | debug("Trying to start again"); |
758 | userauth(authctxt, NULL); | 775 | userauth(authctxt, NULL); |
759 | return; | 776 | return 0; |
760 | } | 777 | } |
778 | return 0; | ||
761 | } | 779 | } |
762 | 780 | ||
763 | /* ARGSUSED */ | 781 | /* ARGSUSED */ |
764 | void | 782 | int |
765 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) | 783 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) |
766 | { | 784 | { |
767 | Authctxt *authctxt = ctxt; | 785 | Authctxt *authctxt = ctxt; |
@@ -784,12 +802,13 @@ input_gssapi_token(int type, u_int32_t plen, void *ctxt) | |||
784 | if (GSS_ERROR(status)) { | 802 | if (GSS_ERROR(status)) { |
785 | /* Start again with the next method in the list */ | 803 | /* Start again with the next method in the list */ |
786 | userauth(authctxt, NULL); | 804 | userauth(authctxt, NULL); |
787 | return; | 805 | return 0; |
788 | } | 806 | } |
807 | return 0; | ||
789 | } | 808 | } |
790 | 809 | ||
791 | /* ARGSUSED */ | 810 | /* ARGSUSED */ |
792 | void | 811 | int |
793 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) | 812 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) |
794 | { | 813 | { |
795 | Authctxt *authctxt = ctxt; | 814 | Authctxt *authctxt = ctxt; |
@@ -816,10 +835,11 @@ input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) | |||
816 | gss_release_buffer(&ms, &send_tok); | 835 | gss_release_buffer(&ms, &send_tok); |
817 | 836 | ||
818 | /* Server will be returning a failed packet after this one */ | 837 | /* Server will be returning a failed packet after this one */ |
838 | return 0; | ||
819 | } | 839 | } |
820 | 840 | ||
821 | /* ARGSUSED */ | 841 | /* ARGSUSED */ |
822 | void | 842 | int |
823 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) | 843 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) |
824 | { | 844 | { |
825 | char *msg; | 845 | char *msg; |
@@ -835,6 +855,7 @@ input_gssapi_error(int type, u_int32_t plen, void *ctxt) | |||
835 | debug("Server GSSAPI Error:\n%s", msg); | 855 | debug("Server GSSAPI Error:\n%s", msg); |
836 | free(msg); | 856 | free(msg); |
837 | free(lang); | 857 | free(lang); |
858 | return 0; | ||
838 | } | 859 | } |
839 | #endif /* GSSAPI */ | 860 | #endif /* GSSAPI */ |
840 | 861 | ||
@@ -889,7 +910,7 @@ userauth_passwd(Authctxt *authctxt) | |||
889 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST | 910 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST |
890 | */ | 911 | */ |
891 | /* ARGSUSED */ | 912 | /* ARGSUSED */ |
892 | void | 913 | int |
893 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) | 914 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) |
894 | { | 915 | { |
895 | Authctxt *authctxt = ctxt; | 916 | Authctxt *authctxt = ctxt; |
@@ -930,7 +951,7 @@ input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) | |||
930 | password = read_passphrase(prompt, RP_ALLOW_EOF); | 951 | password = read_passphrase(prompt, RP_ALLOW_EOF); |
931 | if (password == NULL) { | 952 | if (password == NULL) { |
932 | /* bail out */ | 953 | /* bail out */ |
933 | return; | 954 | return 0; |
934 | } | 955 | } |
935 | snprintf(prompt, sizeof(prompt), | 956 | snprintf(prompt, sizeof(prompt), |
936 | "Retype %.30s@%.128s's new password: ", | 957 | "Retype %.30s@%.128s's new password: ", |
@@ -953,30 +974,33 @@ input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) | |||
953 | 974 | ||
954 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | 975 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
955 | &input_userauth_passwd_changereq); | 976 | &input_userauth_passwd_changereq); |
977 | return 0; | ||
956 | } | 978 | } |
957 | 979 | ||
958 | static int | 980 | static int |
959 | identity_sign(Identity *id, u_char **sigp, u_int *lenp, | 981 | identity_sign(struct identity *id, u_char **sigp, size_t *lenp, |
960 | u_char *data, u_int datalen) | 982 | const u_char *data, size_t datalen, u_int compat) |
961 | { | 983 | { |
962 | Key *prv; | 984 | Key *prv; |
963 | int ret; | 985 | int ret; |
964 | 986 | ||
965 | /* the agent supports this key */ | 987 | /* the agent supports this key */ |
966 | if (id->ac) | 988 | if (id->agent_fd) |
967 | return (ssh_agent_sign(id->ac, id->key, sigp, lenp, | 989 | return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp, |
968 | data, datalen)); | 990 | data, datalen, compat); |
991 | |||
969 | /* | 992 | /* |
970 | * we have already loaded the private key or | 993 | * we have already loaded the private key or |
971 | * the private key is stored in external hardware | 994 | * the private key is stored in external hardware |
972 | */ | 995 | */ |
973 | if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT)) | 996 | if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT)) |
974 | return (key_sign(id->key, sigp, lenp, data, datalen)); | 997 | return (sshkey_sign(id->key, sigp, lenp, data, datalen, |
998 | compat)); | ||
975 | /* load the private key from the file */ | 999 | /* load the private key from the file */ |
976 | if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL) | 1000 | if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL) |
977 | return (-1); | 1001 | return (-1); /* XXX return decent error code */ |
978 | ret = key_sign(prv, sigp, lenp, data, datalen); | 1002 | ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat); |
979 | key_free(prv); | 1003 | sshkey_free(prv); |
980 | return (ret); | 1004 | return (ret); |
981 | } | 1005 | } |
982 | 1006 | ||
@@ -985,13 +1009,16 @@ sign_and_send_pubkey(Authctxt *authctxt, Identity *id) | |||
985 | { | 1009 | { |
986 | Buffer b; | 1010 | Buffer b; |
987 | u_char *blob, *signature; | 1011 | u_char *blob, *signature; |
988 | u_int bloblen, slen; | 1012 | u_int bloblen; |
1013 | size_t slen; | ||
989 | u_int skip = 0; | 1014 | u_int skip = 0; |
990 | int ret = -1; | 1015 | int ret = -1; |
991 | int have_sig = 1; | 1016 | int have_sig = 1; |
992 | char *fp; | 1017 | char *fp; |
993 | 1018 | ||
994 | fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); | 1019 | if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash, |
1020 | SSH_FP_DEFAULT)) == NULL) | ||
1021 | return 0; | ||
995 | debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp); | 1022 | debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp); |
996 | free(fp); | 1023 | free(fp); |
997 | 1024 | ||
@@ -1026,8 +1053,8 @@ sign_and_send_pubkey(Authctxt *authctxt, Identity *id) | |||
1026 | 1053 | ||
1027 | /* generate signature */ | 1054 | /* generate signature */ |
1028 | ret = identity_sign(id, &signature, &slen, | 1055 | ret = identity_sign(id, &signature, &slen, |
1029 | buffer_ptr(&b), buffer_len(&b)); | 1056 | buffer_ptr(&b), buffer_len(&b), datafellows); |
1030 | if (ret == -1) { | 1057 | if (ret != 0) { |
1031 | free(blob); | 1058 | free(blob); |
1032 | buffer_free(&b); | 1059 | buffer_free(&b); |
1033 | return 0; | 1060 | return 0; |
@@ -1102,7 +1129,7 @@ load_identity_file(char *filename, int userprovided) | |||
1102 | { | 1129 | { |
1103 | Key *private; | 1130 | Key *private; |
1104 | char prompt[300], *passphrase; | 1131 | char prompt[300], *passphrase; |
1105 | int perm_ok = 0, quit, i; | 1132 | int r, perm_ok = 0, quit = 0, i; |
1106 | struct stat st; | 1133 | struct stat st; |
1107 | 1134 | ||
1108 | if (stat(filename, &st) < 0) { | 1135 | if (stat(filename, &st) < 0) { |
@@ -1110,33 +1137,50 @@ load_identity_file(char *filename, int userprovided) | |||
1110 | filename, strerror(errno)); | 1137 | filename, strerror(errno)); |
1111 | return NULL; | 1138 | return NULL; |
1112 | } | 1139 | } |
1113 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok); | 1140 | snprintf(prompt, sizeof prompt, |
1114 | if (!perm_ok) { | 1141 | "Enter passphrase for key '%.100s': ", filename); |
1115 | if (private != NULL) | 1142 | for (i = 0; i <= options.number_of_password_prompts; i++) { |
1116 | key_free(private); | 1143 | if (i == 0) |
1117 | return NULL; | 1144 | passphrase = ""; |
1118 | } | 1145 | else { |
1119 | if (private == NULL) { | ||
1120 | if (options.batch_mode) | ||
1121 | return NULL; | ||
1122 | snprintf(prompt, sizeof prompt, | ||
1123 | "Enter passphrase for key '%.100s': ", filename); | ||
1124 | for (i = 0; i < options.number_of_password_prompts; i++) { | ||
1125 | passphrase = read_passphrase(prompt, 0); | 1146 | passphrase = read_passphrase(prompt, 0); |
1126 | if (strcmp(passphrase, "") != 0) { | 1147 | if (*passphrase == '\0') { |
1127 | private = key_load_private_type(KEY_UNSPEC, | ||
1128 | filename, passphrase, NULL, NULL); | ||
1129 | quit = 0; | ||
1130 | } else { | ||
1131 | debug2("no passphrase given, try next key"); | 1148 | debug2("no passphrase given, try next key"); |
1149 | free(passphrase); | ||
1150 | break; | ||
1151 | } | ||
1152 | } | ||
1153 | switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename, | ||
1154 | passphrase, &private, NULL, &perm_ok))) { | ||
1155 | case 0: | ||
1156 | break; | ||
1157 | case SSH_ERR_KEY_WRONG_PASSPHRASE: | ||
1158 | if (options.batch_mode) { | ||
1159 | quit = 1; | ||
1160 | break; | ||
1161 | } | ||
1162 | if (i != 0) | ||
1163 | debug2("bad passphrase given, try again..."); | ||
1164 | break; | ||
1165 | case SSH_ERR_SYSTEM_ERROR: | ||
1166 | if (errno == ENOENT) { | ||
1167 | debug2("Load key \"%s\": %s", | ||
1168 | filename, ssh_err(r)); | ||
1132 | quit = 1; | 1169 | quit = 1; |
1170 | break; | ||
1133 | } | 1171 | } |
1172 | /* FALLTHROUGH */ | ||
1173 | default: | ||
1174 | error("Load key \"%s\": %s", filename, ssh_err(r)); | ||
1175 | quit = 1; | ||
1176 | break; | ||
1177 | } | ||
1178 | if (i > 0) { | ||
1134 | explicit_bzero(passphrase, strlen(passphrase)); | 1179 | explicit_bzero(passphrase, strlen(passphrase)); |
1135 | free(passphrase); | 1180 | free(passphrase); |
1136 | if (private != NULL || quit) | ||
1137 | break; | ||
1138 | debug2("bad passphrase given, try again..."); | ||
1139 | } | 1181 | } |
1182 | if (private != NULL || quit) | ||
1183 | break; | ||
1140 | } | 1184 | } |
1141 | return private; | 1185 | return private; |
1142 | } | 1186 | } |
@@ -1150,12 +1194,12 @@ load_identity_file(char *filename, int userprovided) | |||
1150 | static void | 1194 | static void |
1151 | pubkey_prepare(Authctxt *authctxt) | 1195 | pubkey_prepare(Authctxt *authctxt) |
1152 | { | 1196 | { |
1153 | Identity *id, *id2, *tmp; | 1197 | struct identity *id, *id2, *tmp; |
1154 | Idlist agent, files, *preferred; | 1198 | struct idlist agent, files, *preferred; |
1155 | Key *key; | 1199 | struct sshkey *key; |
1156 | AuthenticationConnection *ac; | 1200 | int agent_fd, i, r, found; |
1157 | char *comment; | 1201 | size_t j; |
1158 | int i, found; | 1202 | struct ssh_identitylist *idlist; |
1159 | 1203 | ||
1160 | TAILQ_INIT(&agent); /* keys from the agent */ | 1204 | TAILQ_INIT(&agent); /* keys from the agent */ |
1161 | TAILQ_INIT(&files); /* keys from the config file */ | 1205 | TAILQ_INIT(&files); /* keys from the config file */ |
@@ -1185,7 +1229,7 @@ pubkey_prepare(Authctxt *authctxt) | |||
1185 | if (id2->key == NULL || | 1229 | if (id2->key == NULL || |
1186 | (id2->key->flags & SSHKEY_FLAG_EXT) == 0) | 1230 | (id2->key->flags & SSHKEY_FLAG_EXT) == 0) |
1187 | continue; | 1231 | continue; |
1188 | if (key_equal(id->key, id2->key)) { | 1232 | if (sshkey_equal(id->key, id2->key)) { |
1189 | TAILQ_REMOVE(&files, id, next); | 1233 | TAILQ_REMOVE(&files, id, next); |
1190 | TAILQ_INSERT_TAIL(preferred, id, next); | 1234 | TAILQ_INSERT_TAIL(preferred, id, next); |
1191 | found = 1; | 1235 | found = 1; |
@@ -1200,37 +1244,48 @@ pubkey_prepare(Authctxt *authctxt) | |||
1200 | } | 1244 | } |
1201 | } | 1245 | } |
1202 | /* list of keys supported by the agent */ | 1246 | /* list of keys supported by the agent */ |
1203 | if ((ac = ssh_get_authentication_connection())) { | 1247 | if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) { |
1204 | for (key = ssh_get_first_identity(ac, &comment, 2); | 1248 | if (r != SSH_ERR_AGENT_NOT_PRESENT) |
1205 | key != NULL; | 1249 | debug("%s: ssh_get_authentication_socket: %s", |
1206 | key = ssh_get_next_identity(ac, &comment, 2)) { | 1250 | __func__, ssh_err(r)); |
1251 | } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) { | ||
1252 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) | ||
1253 | debug("%s: ssh_fetch_identitylist: %s", | ||
1254 | __func__, ssh_err(r)); | ||
1255 | } else { | ||
1256 | for (j = 0; j < idlist->nkeys; j++) { | ||
1207 | found = 0; | 1257 | found = 0; |
1208 | TAILQ_FOREACH(id, &files, next) { | 1258 | TAILQ_FOREACH(id, &files, next) { |
1209 | /* agent keys from the config file are preferred */ | 1259 | /* |
1210 | if (key_equal(key, id->key)) { | 1260 | * agent keys from the config file are |
1211 | key_free(key); | 1261 | * preferred |
1212 | free(comment); | 1262 | */ |
1263 | if (sshkey_equal(idlist->keys[j], id->key)) { | ||
1213 | TAILQ_REMOVE(&files, id, next); | 1264 | TAILQ_REMOVE(&files, id, next); |
1214 | TAILQ_INSERT_TAIL(preferred, id, next); | 1265 | TAILQ_INSERT_TAIL(preferred, id, next); |
1215 | id->ac = ac; | 1266 | id->agent_fd = agent_fd; |
1216 | found = 1; | 1267 | found = 1; |
1217 | break; | 1268 | break; |
1218 | } | 1269 | } |
1219 | } | 1270 | } |
1220 | if (!found && !options.identities_only) { | 1271 | if (!found && !options.identities_only) { |
1221 | id = xcalloc(1, sizeof(*id)); | 1272 | id = xcalloc(1, sizeof(*id)); |
1222 | id->key = key; | 1273 | /* XXX "steals" key/comment from idlist */ |
1223 | id->filename = comment; | 1274 | id->key = idlist->keys[j]; |
1224 | id->ac = ac; | 1275 | id->filename = idlist->comments[j]; |
1276 | idlist->keys[j] = NULL; | ||
1277 | idlist->comments[j] = NULL; | ||
1278 | id->agent_fd = agent_fd; | ||
1225 | TAILQ_INSERT_TAIL(&agent, id, next); | 1279 | TAILQ_INSERT_TAIL(&agent, id, next); |
1226 | } | 1280 | } |
1227 | } | 1281 | } |
1282 | ssh_free_identitylist(idlist); | ||
1228 | /* append remaining agent keys */ | 1283 | /* append remaining agent keys */ |
1229 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { | 1284 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { |
1230 | TAILQ_REMOVE(&agent, id, next); | 1285 | TAILQ_REMOVE(&agent, id, next); |
1231 | TAILQ_INSERT_TAIL(preferred, id, next); | 1286 | TAILQ_INSERT_TAIL(preferred, id, next); |
1232 | } | 1287 | } |
1233 | authctxt->agent = ac; | 1288 | authctxt->agent_fd = agent_fd; |
1234 | } | 1289 | } |
1235 | /* append remaining keys from the config file */ | 1290 | /* append remaining keys from the config file */ |
1236 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { | 1291 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { |
@@ -1248,13 +1303,13 @@ pubkey_cleanup(Authctxt *authctxt) | |||
1248 | { | 1303 | { |
1249 | Identity *id; | 1304 | Identity *id; |
1250 | 1305 | ||
1251 | if (authctxt->agent != NULL) | 1306 | if (authctxt->agent_fd != -1) |
1252 | ssh_close_authentication_connection(authctxt->agent); | 1307 | ssh_close_authentication_socket(authctxt->agent_fd); |
1253 | for (id = TAILQ_FIRST(&authctxt->keys); id; | 1308 | for (id = TAILQ_FIRST(&authctxt->keys); id; |
1254 | id = TAILQ_FIRST(&authctxt->keys)) { | 1309 | id = TAILQ_FIRST(&authctxt->keys)) { |
1255 | TAILQ_REMOVE(&authctxt->keys, id, next); | 1310 | TAILQ_REMOVE(&authctxt->keys, id, next); |
1256 | if (id->key) | 1311 | if (id->key) |
1257 | key_free(id->key); | 1312 | sshkey_free(id->key); |
1258 | free(id->filename); | 1313 | free(id->filename); |
1259 | free(id); | 1314 | free(id); |
1260 | } | 1315 | } |
@@ -1346,7 +1401,7 @@ userauth_kbdint(Authctxt *authctxt) | |||
1346 | /* | 1401 | /* |
1347 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE | 1402 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
1348 | */ | 1403 | */ |
1349 | void | 1404 | int |
1350 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) | 1405 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
1351 | { | 1406 | { |
1352 | Authctxt *authctxt = ctxt; | 1407 | Authctxt *authctxt = ctxt; |
@@ -1398,81 +1453,120 @@ input_userauth_info_req(int type, u_int32_t seq, void *ctxt) | |||
1398 | 1453 | ||
1399 | packet_add_padding(64); | 1454 | packet_add_padding(64); |
1400 | packet_send(); | 1455 | packet_send(); |
1456 | return 0; | ||
1401 | } | 1457 | } |
1402 | 1458 | ||
1403 | static int | 1459 | static int |
1404 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, | 1460 | ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp, |
1405 | u_char *data, u_int datalen) | 1461 | const u_char *data, size_t datalen) |
1406 | { | 1462 | { |
1407 | Buffer b; | 1463 | struct sshbuf *b; |
1408 | struct stat st; | 1464 | struct stat st; |
1409 | pid_t pid; | 1465 | pid_t pid; |
1410 | int to[2], from[2], status, version = 2; | 1466 | int i, r, to[2], from[2], status, sock = packet_get_connection_in(); |
1467 | u_char rversion = 0, version = 2; | ||
1468 | void (*osigchld)(int); | ||
1411 | 1469 | ||
1412 | debug2("ssh_keysign called"); | 1470 | *sigp = NULL; |
1471 | *lenp = 0; | ||
1413 | 1472 | ||
1414 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { | 1473 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
1415 | error("ssh_keysign: not installed: %s", strerror(errno)); | 1474 | error("%s: not installed: %s", __func__, strerror(errno)); |
1475 | return -1; | ||
1476 | } | ||
1477 | if (fflush(stdout) != 0) { | ||
1478 | error("%s: fflush: %s", __func__, strerror(errno)); | ||
1416 | return -1; | 1479 | return -1; |
1417 | } | 1480 | } |
1418 | if (fflush(stdout) != 0) | ||
1419 | error("ssh_keysign: fflush: %s", strerror(errno)); | ||
1420 | if (pipe(to) < 0) { | 1481 | if (pipe(to) < 0) { |
1421 | error("ssh_keysign: pipe: %s", strerror(errno)); | 1482 | error("%s: pipe: %s", __func__, strerror(errno)); |
1422 | return -1; | 1483 | return -1; |
1423 | } | 1484 | } |
1424 | if (pipe(from) < 0) { | 1485 | if (pipe(from) < 0) { |
1425 | error("ssh_keysign: pipe: %s", strerror(errno)); | 1486 | error("%s: pipe: %s", __func__, strerror(errno)); |
1426 | return -1; | 1487 | return -1; |
1427 | } | 1488 | } |
1428 | if ((pid = fork()) < 0) { | 1489 | if ((pid = fork()) < 0) { |
1429 | error("ssh_keysign: fork: %s", strerror(errno)); | 1490 | error("%s: fork: %s", __func__, strerror(errno)); |
1430 | return -1; | 1491 | return -1; |
1431 | } | 1492 | } |
1493 | osigchld = signal(SIGCHLD, SIG_DFL); | ||
1432 | if (pid == 0) { | 1494 | if (pid == 0) { |
1433 | /* keep the socket on exec */ | 1495 | /* keep the socket on exec */ |
1434 | fcntl(packet_get_connection_in(), F_SETFD, 0); | 1496 | fcntl(sock, F_SETFD, 0); |
1435 | permanently_drop_suid(getuid()); | 1497 | permanently_drop_suid(getuid()); |
1436 | close(from[0]); | 1498 | close(from[0]); |
1437 | if (dup2(from[1], STDOUT_FILENO) < 0) | 1499 | if (dup2(from[1], STDOUT_FILENO) < 0) |
1438 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | 1500 | fatal("%s: dup2: %s", __func__, strerror(errno)); |
1439 | close(to[1]); | 1501 | close(to[1]); |
1440 | if (dup2(to[0], STDIN_FILENO) < 0) | 1502 | if (dup2(to[0], STDIN_FILENO) < 0) |
1441 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | 1503 | fatal("%s: dup2: %s", __func__, strerror(errno)); |
1442 | close(from[1]); | 1504 | close(from[1]); |
1443 | close(to[0]); | 1505 | close(to[0]); |
1506 | /* Close everything but stdio and the socket */ | ||
1507 | for (i = STDERR_FILENO + 1; i < sock; i++) | ||
1508 | close(i); | ||
1509 | closefrom(sock + 1); | ||
1510 | debug3("%s: [child] pid=%ld, exec %s", | ||
1511 | __func__, (long)getpid(), _PATH_SSH_KEY_SIGN); | ||
1444 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); | 1512 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); |
1445 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, | 1513 | fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN, |
1446 | strerror(errno)); | 1514 | strerror(errno)); |
1447 | } | 1515 | } |
1448 | close(from[1]); | 1516 | close(from[1]); |
1449 | close(to[0]); | 1517 | close(to[0]); |
1450 | 1518 | ||
1451 | buffer_init(&b); | 1519 | if ((b = sshbuf_new()) == NULL) |
1452 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ | 1520 | fatal("%s: sshbuf_new failed", __func__); |
1453 | buffer_put_string(&b, data, datalen); | 1521 | /* send # of sock, data to be signed */ |
1454 | if (ssh_msg_send(to[1], version, &b) == -1) | 1522 | if ((r = sshbuf_put_u32(b, sock) != 0) || |
1455 | fatal("ssh_keysign: couldn't send request"); | 1523 | (r = sshbuf_put_string(b, data, datalen)) != 0) |
1456 | 1524 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); | |
1457 | if (ssh_msg_recv(from[0], &b) < 0) { | 1525 | if (ssh_msg_send(to[1], version, b) == -1) |
1458 | error("ssh_keysign: no reply"); | 1526 | fatal("%s: couldn't send request", __func__); |
1459 | buffer_free(&b); | 1527 | sshbuf_reset(b); |
1460 | return -1; | 1528 | r = ssh_msg_recv(from[0], b); |
1461 | } | ||
1462 | close(from[0]); | 1529 | close(from[0]); |
1463 | close(to[1]); | 1530 | close(to[1]); |
1531 | if (r < 0) { | ||
1532 | error("%s: no reply", __func__); | ||
1533 | goto fail; | ||
1534 | } | ||
1464 | 1535 | ||
1465 | while (waitpid(pid, &status, 0) < 0) | 1536 | errno = 0; |
1466 | if (errno != EINTR) | 1537 | while (waitpid(pid, &status, 0) < 0) { |
1467 | break; | 1538 | if (errno != EINTR) { |
1468 | 1539 | error("%s: waitpid %ld: %s", | |
1469 | if (buffer_get_char(&b) != version) { | 1540 | __func__, (long)pid, strerror(errno)); |
1470 | error("ssh_keysign: bad version"); | 1541 | goto fail; |
1471 | buffer_free(&b); | 1542 | } |
1543 | } | ||
1544 | if (!WIFEXITED(status)) { | ||
1545 | error("%s: exited abnormally", __func__); | ||
1546 | goto fail; | ||
1547 | } | ||
1548 | if (WEXITSTATUS(status) != 0) { | ||
1549 | error("%s: exited with status %d", | ||
1550 | __func__, WEXITSTATUS(status)); | ||
1551 | goto fail; | ||
1552 | } | ||
1553 | if ((r = sshbuf_get_u8(b, &rversion)) != 0) { | ||
1554 | error("%s: buffer error: %s", __func__, ssh_err(r)); | ||
1555 | goto fail; | ||
1556 | } | ||
1557 | if (rversion != version) { | ||
1558 | error("%s: bad version", __func__); | ||
1559 | goto fail; | ||
1560 | } | ||
1561 | if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) { | ||
1562 | error("%s: buffer error: %s", __func__, ssh_err(r)); | ||
1563 | fail: | ||
1564 | signal(SIGCHLD, osigchld); | ||
1565 | sshbuf_free(b); | ||
1472 | return -1; | 1566 | return -1; |
1473 | } | 1567 | } |
1474 | *sigp = buffer_get_string(&b, lenp); | 1568 | signal(SIGCHLD, osigchld); |
1475 | buffer_free(&b); | 1569 | sshbuf_free(b); |
1476 | 1570 | ||
1477 | return 0; | 1571 | return 0; |
1478 | } | 1572 | } |
@@ -1480,94 +1574,149 @@ ssh_keysign(Key *key, u_char **sigp, u_int *lenp, | |||
1480 | int | 1574 | int |
1481 | userauth_hostbased(Authctxt *authctxt) | 1575 | userauth_hostbased(Authctxt *authctxt) |
1482 | { | 1576 | { |
1483 | Key *private = NULL; | 1577 | struct ssh *ssh = active_state; |
1484 | Sensitive *sensitive = authctxt->sensitive; | 1578 | struct sshkey *private = NULL; |
1485 | Buffer b; | 1579 | struct sshbuf *b = NULL; |
1486 | u_char *signature, *blob; | ||
1487 | char *chost, *pkalg, *p; | ||
1488 | const char *service; | 1580 | const char *service; |
1489 | u_int blen, slen; | 1581 | u_char *sig = NULL, *keyblob = NULL; |
1490 | int ok, i, found = 0; | 1582 | char *fp = NULL, *chost = NULL, *lname = NULL; |
1491 | 1583 | size_t siglen = 0, keylen = 0; | |
1492 | /* check for a useful key */ | 1584 | int i, r, success = 0; |
1493 | for (i = 0; i < sensitive->nkeys; i++) { | 1585 | |
1494 | private = sensitive->keys[i]; | 1586 | if (authctxt->ktypes == NULL) { |
1495 | if (private && private->type != KEY_RSA1) { | 1587 | authctxt->oktypes = xstrdup(options.hostbased_key_types); |
1496 | found = 1; | 1588 | authctxt->ktypes = authctxt->oktypes; |
1589 | } | ||
1590 | |||
1591 | /* | ||
1592 | * Work through each listed type pattern in HostbasedKeyTypes, | ||
1593 | * trying each hostkey that matches the type in turn. | ||
1594 | */ | ||
1595 | for (;;) { | ||
1596 | if (authctxt->active_ktype == NULL) | ||
1597 | authctxt->active_ktype = strsep(&authctxt->ktypes, ","); | ||
1598 | if (authctxt->active_ktype == NULL || | ||
1599 | *authctxt->active_ktype == '\0') | ||
1600 | break; | ||
1601 | debug3("%s: trying key type %s", __func__, | ||
1602 | authctxt->active_ktype); | ||
1603 | |||
1604 | /* check for a useful key */ | ||
1605 | private = NULL; | ||
1606 | for (i = 0; i < authctxt->sensitive->nkeys; i++) { | ||
1607 | if (authctxt->sensitive->keys[i] == NULL || | ||
1608 | authctxt->sensitive->keys[i]->type == KEY_RSA1 || | ||
1609 | authctxt->sensitive->keys[i]->type == KEY_UNSPEC) | ||
1610 | continue; | ||
1611 | if (match_pattern_list( | ||
1612 | sshkey_ssh_name(authctxt->sensitive->keys[i]), | ||
1613 | authctxt->active_ktype, | ||
1614 | strlen(authctxt->active_ktype), 0) != 1) | ||
1615 | continue; | ||
1497 | /* we take and free the key */ | 1616 | /* we take and free the key */ |
1498 | sensitive->keys[i] = NULL; | 1617 | private = authctxt->sensitive->keys[i]; |
1618 | authctxt->sensitive->keys[i] = NULL; | ||
1499 | break; | 1619 | break; |
1500 | } | 1620 | } |
1621 | /* Found one */ | ||
1622 | if (private != NULL) | ||
1623 | break; | ||
1624 | /* No more keys of this type; advance */ | ||
1625 | authctxt->active_ktype = NULL; | ||
1501 | } | 1626 | } |
1502 | if (!found) { | 1627 | if (private == NULL) { |
1628 | free(authctxt->oktypes); | ||
1629 | authctxt->oktypes = authctxt->ktypes = NULL; | ||
1630 | authctxt->active_ktype = NULL; | ||
1503 | debug("No more client hostkeys for hostbased authentication."); | 1631 | debug("No more client hostkeys for hostbased authentication."); |
1504 | return 0; | 1632 | goto out; |
1505 | } | 1633 | } |
1506 | if (key_to_blob(private, &blob, &blen) == 0) { | 1634 | |
1507 | key_free(private); | 1635 | if ((fp = sshkey_fingerprint(private, options.fingerprint_hash, |
1508 | return 0; | 1636 | SSH_FP_DEFAULT)) == NULL) { |
1637 | error("%s: sshkey_fingerprint failed", __func__); | ||
1638 | goto out; | ||
1509 | } | 1639 | } |
1640 | debug("%s: trying hostkey %s %s", | ||
1641 | __func__, sshkey_ssh_name(private), fp); | ||
1642 | |||
1510 | /* figure out a name for the client host */ | 1643 | /* figure out a name for the client host */ |
1511 | p = get_local_name(packet_get_connection_in()); | 1644 | if ((lname = get_local_name(packet_get_connection_in())) == NULL) { |
1512 | if (p == NULL) { | 1645 | error("%s: cannot get local ipaddr/name", __func__); |
1513 | error("userauth_hostbased: cannot get local ipaddr/name"); | 1646 | goto out; |
1514 | key_free(private); | ||
1515 | free(blob); | ||
1516 | return 0; | ||
1517 | } | 1647 | } |
1518 | xasprintf(&chost, "%s.", p); | 1648 | |
1519 | debug2("userauth_hostbased: chost %s", chost); | 1649 | /* XXX sshbuf_put_stringf? */ |
1520 | free(p); | 1650 | xasprintf(&chost, "%s.", lname); |
1651 | debug2("%s: chost %s", __func__, chost); | ||
1521 | 1652 | ||
1522 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | 1653 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
1523 | authctxt->service; | 1654 | authctxt->service; |
1524 | pkalg = xstrdup(key_ssh_name(private)); | 1655 | |
1525 | buffer_init(&b); | ||
1526 | /* construct data */ | 1656 | /* construct data */ |
1527 | buffer_put_string(&b, session_id2, session_id2_len); | 1657 | if ((b = sshbuf_new()) == NULL) { |
1528 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | 1658 | error("%s: sshbuf_new failed", __func__); |
1529 | buffer_put_cstring(&b, authctxt->server_user); | 1659 | goto out; |
1530 | buffer_put_cstring(&b, service); | 1660 | } |
1531 | buffer_put_cstring(&b, authctxt->method->name); | 1661 | if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) { |
1532 | buffer_put_cstring(&b, pkalg); | 1662 | error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); |
1533 | buffer_put_string(&b, blob, blen); | 1663 | goto out; |
1534 | buffer_put_cstring(&b, chost); | 1664 | } |
1535 | buffer_put_cstring(&b, authctxt->local_user); | 1665 | if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 || |
1666 | (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || | ||
1667 | (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || | ||
1668 | (r = sshbuf_put_cstring(b, service)) != 0 || | ||
1669 | (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || | ||
1670 | (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 || | ||
1671 | (r = sshbuf_put_string(b, keyblob, keylen)) != 0 || | ||
1672 | (r = sshbuf_put_cstring(b, chost)) != 0 || | ||
1673 | (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) { | ||
1674 | error("%s: buffer error: %s", __func__, ssh_err(r)); | ||
1675 | goto out; | ||
1676 | } | ||
1677 | |||
1536 | #ifdef DEBUG_PK | 1678 | #ifdef DEBUG_PK |
1537 | buffer_dump(&b); | 1679 | sshbuf_dump(b, stderr); |
1538 | #endif | 1680 | #endif |
1539 | if (sensitive->external_keysign) | 1681 | if (authctxt->sensitive->external_keysign) |
1540 | ok = ssh_keysign(private, &signature, &slen, | 1682 | r = ssh_keysign(private, &sig, &siglen, |
1541 | buffer_ptr(&b), buffer_len(&b)); | 1683 | sshbuf_ptr(b), sshbuf_len(b)); |
1542 | else | 1684 | else if ((r = sshkey_sign(private, &sig, &siglen, |
1543 | ok = key_sign(private, &signature, &slen, | 1685 | sshbuf_ptr(b), sshbuf_len(b), datafellows)) != 0) |
1544 | buffer_ptr(&b), buffer_len(&b)); | 1686 | debug("%s: sshkey_sign: %s", __func__, ssh_err(r)); |
1545 | key_free(private); | 1687 | if (r != 0) { |
1546 | buffer_free(&b); | 1688 | error("sign using hostkey %s %s failed", |
1547 | if (ok != 0) { | 1689 | sshkey_ssh_name(private), fp); |
1548 | error("key_sign failed"); | 1690 | goto out; |
1549 | free(chost); | ||
1550 | free(pkalg); | ||
1551 | free(blob); | ||
1552 | return 0; | ||
1553 | } | 1691 | } |
1554 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | 1692 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
1555 | packet_put_cstring(authctxt->server_user); | 1693 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
1556 | packet_put_cstring(authctxt->service); | 1694 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
1557 | packet_put_cstring(authctxt->method->name); | 1695 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
1558 | packet_put_cstring(pkalg); | 1696 | (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 || |
1559 | packet_put_string(blob, blen); | 1697 | (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 || |
1560 | packet_put_cstring(chost); | 1698 | (r = sshpkt_put_cstring(ssh, chost)) != 0 || |
1561 | packet_put_cstring(authctxt->local_user); | 1699 | (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 || |
1562 | packet_put_string(signature, slen); | 1700 | (r = sshpkt_put_string(ssh, sig, siglen)) != 0 || |
1563 | explicit_bzero(signature, slen); | 1701 | (r = sshpkt_send(ssh)) != 0) { |
1564 | free(signature); | 1702 | error("%s: packet error: %s", __func__, ssh_err(r)); |
1703 | goto out; | ||
1704 | } | ||
1705 | success = 1; | ||
1706 | |||
1707 | out: | ||
1708 | if (sig != NULL) { | ||
1709 | explicit_bzero(sig, siglen); | ||
1710 | free(sig); | ||
1711 | } | ||
1712 | free(keyblob); | ||
1713 | free(lname); | ||
1714 | free(fp); | ||
1565 | free(chost); | 1715 | free(chost); |
1566 | free(pkalg); | 1716 | sshkey_free(private); |
1567 | free(blob); | 1717 | sshbuf_free(b); |
1568 | 1718 | ||
1569 | packet_send(); | 1719 | return success; |
1570 | return 1; | ||
1571 | } | 1720 | } |
1572 | 1721 | ||
1573 | /* find auth method */ | 1722 | /* find auth method */ |