1 /* $OpenBSD: sshconnect2.c,v 1.157 2006/07/20 15:26:15 stevesk Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
35 #include "openbsd-compat/sys-queue.h"
46 #include "myproposal.h"
47 #include "sshconnect.h"
58 #include "pathnames.h"
66 extern char *client_version_string
;
67 extern char *server_version_string
;
68 extern Options options
;
74 u_char
*session_id2
= NULL
;
75 u_int session_id2_len
= 0;
78 struct sockaddr
*xxx_hostaddr
;
83 verify_host_key_callback(Key
*hostkey
)
85 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
86 fatal("Host key verification failed.");
91 ssh_kex2(char *host
, struct sockaddr
*hostaddr
)
96 xxx_hostaddr
= hostaddr
;
98 if (options
.ciphers
== (char *)-1) {
99 logit("No valid ciphers for protocol version 2 given, using defaults.");
100 options
.ciphers
= NULL
;
102 if (options
.ciphers
!= NULL
) {
103 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
104 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
106 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
107 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
108 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
109 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
110 if (options
.compression
) {
111 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
112 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
114 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
115 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
117 if (options
.macs
!= NULL
) {
118 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
119 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
121 if (options
.hostkeyalgorithms
!= NULL
)
122 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
123 options
.hostkeyalgorithms
;
125 if (options
.rekey_limit
)
126 packet_set_rekey_limit(options
.rekey_limit
);
128 /* start key exchange */
129 kex
= kex_setup(myproposal
);
130 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
131 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
132 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
133 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
134 kex
->client_version_string
=client_version_string
;
135 kex
->server_version_string
=server_version_string
;
136 kex
->verify_host_key
=&verify_host_key_callback
;
140 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
142 session_id2
= kex
->session_id
;
143 session_id2_len
= kex
->session_id_len
;
146 /* send 1st encrypted/maced/compressed message */
147 packet_start(SSH2_MSG_IGNORE
);
148 packet_put_cstring("markus");
158 typedef struct Authctxt Authctxt
;
159 typedef struct Authmethod Authmethod
;
160 typedef struct identity Identity
;
161 typedef struct idlist Idlist
;
164 TAILQ_ENTRY(identity
) next
;
165 AuthenticationConnection
*ac
; /* set if agent supports key */
166 Key
*key
; /* public/private key */
167 char *filename
; /* comment for agent-only keys */
169 int isprivate
; /* key points to the private key */
171 TAILQ_HEAD(idlist
, identity
);
174 const char *server_user
;
175 const char *local_user
;
183 AuthenticationConnection
*agent
;
185 Sensitive
*sensitive
;
186 /* kbd-interactive */
192 char *name
; /* string to compare against server's list */
193 int (*userauth
)(Authctxt
*authctxt
);
194 int *enabled
; /* flag in option struct that enables method */
195 int *batch_flag
; /* flag in option struct that disables method */
198 void input_userauth_success(int, u_int32_t
, void *);
199 void input_userauth_failure(int, u_int32_t
, void *);
200 void input_userauth_banner(int, u_int32_t
, void *);
201 void input_userauth_error(int, u_int32_t
, void *);
202 void input_userauth_info_req(int, u_int32_t
, void *);
203 void input_userauth_pk_ok(int, u_int32_t
, void *);
204 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
206 int userauth_none(Authctxt
*);
207 int userauth_pubkey(Authctxt
*);
208 int userauth_passwd(Authctxt
*);
209 int userauth_kbdint(Authctxt
*);
210 int userauth_hostbased(Authctxt
*);
211 int userauth_kerberos(Authctxt
*);
214 int userauth_gssapi(Authctxt
*authctxt
);
215 void input_gssapi_response(int type
, u_int32_t
, void *);
216 void input_gssapi_token(int type
, u_int32_t
, void *);
217 void input_gssapi_hash(int type
, u_int32_t
, void *);
218 void input_gssapi_error(int, u_int32_t
, void *);
219 void input_gssapi_errtok(int, u_int32_t
, void *);
222 void userauth(Authctxt
*, char *);
224 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
225 static void pubkey_prepare(Authctxt
*);
226 static void pubkey_cleanup(Authctxt
*);
227 static Key
*load_identity_file(char *);
229 static Authmethod
*authmethod_get(char *authlist
);
230 static Authmethod
*authmethod_lookup(const char *name
);
231 static char *authmethods_get(void);
233 Authmethod authmethods
[] = {
237 &options
.gss_authentication
,
242 &options
.hostbased_authentication
,
246 &options
.pubkey_authentication
,
248 {"keyboard-interactive",
250 &options
.kbd_interactive_authentication
,
251 &options
.batch_mode
},
254 &options
.password_authentication
,
255 &options
.batch_mode
},
260 {NULL
, NULL
, NULL
, NULL
}
264 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
265 Sensitive
*sensitive
)
270 if (options
.challenge_response_authentication
)
271 options
.kbd_interactive_authentication
= 1;
273 packet_start(SSH2_MSG_SERVICE_REQUEST
);
274 packet_put_cstring("ssh-userauth");
276 debug("SSH2_MSG_SERVICE_REQUEST sent");
278 type
= packet_read();
279 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
280 fatal("Server denied authentication request: %d", type
);
281 if (packet_remaining() > 0) {
282 char *reply
= packet_get_string(NULL
);
283 debug2("service_accept: %s", reply
);
286 debug2("buggy server: service_accept w/o service");
289 debug("SSH2_MSG_SERVICE_ACCEPT received");
291 if (options
.preferred_authentications
== NULL
)
292 options
.preferred_authentications
= authmethods_get();
294 /* setup authentication context */
295 memset(&authctxt
, 0, sizeof(authctxt
));
296 pubkey_prepare(&authctxt
);
297 authctxt
.server_user
= server_user
;
298 authctxt
.local_user
= local_user
;
299 authctxt
.host
= host
;
300 authctxt
.service
= "ssh-connection"; /* service name */
301 authctxt
.success
= 0;
302 authctxt
.method
= authmethod_lookup("none");
303 authctxt
.authlist
= NULL
;
304 authctxt
.methoddata
= NULL
;
305 authctxt
.sensitive
= sensitive
;
306 authctxt
.info_req_seen
= 0;
307 if (authctxt
.method
== NULL
)
308 fatal("ssh_userauth2: internal error: cannot send userauth none request");
310 /* initial userauth request */
311 userauth_none(&authctxt
);
313 dispatch_init(&input_userauth_error
);
314 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
315 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
316 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
317 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
319 pubkey_cleanup(&authctxt
);
320 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
322 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
326 userauth(Authctxt
*authctxt
, char *authlist
)
328 if (authctxt
->methoddata
) {
329 xfree(authctxt
->methoddata
);
330 authctxt
->methoddata
= NULL
;
332 if (authlist
== NULL
) {
333 authlist
= authctxt
->authlist
;
335 if (authctxt
->authlist
)
336 xfree(authctxt
->authlist
);
337 authctxt
->authlist
= authlist
;
340 Authmethod
*method
= authmethod_get(authlist
);
342 fatal("Permission denied (%s).", authlist
);
343 authctxt
->method
= method
;
345 /* reset the per method handler */
346 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
347 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
349 /* and try new method */
350 if (method
->userauth(authctxt
) != 0) {
351 debug2("we sent a %s packet, wait for reply", method
->name
);
354 debug2("we did not send a packet, disable method");
355 method
->enabled
= NULL
;
361 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
363 fatal("input_userauth_error: bad message during authentication: "
368 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
372 debug3("input_userauth_banner");
373 msg
= packet_get_string(NULL
);
374 lang
= packet_get_string(NULL
);
375 if (options
.log_level
> SYSLOG_LEVEL_QUIET
)
376 fprintf(stderr
, "%s", msg
);
382 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
384 Authctxt
*authctxt
= ctxt
;
385 if (authctxt
== NULL
)
386 fatal("input_userauth_success: no authentication context");
387 if (authctxt
->authlist
) {
388 xfree(authctxt
->authlist
);
389 authctxt
->authlist
= NULL
;
391 if (authctxt
->methoddata
) {
392 xfree(authctxt
->methoddata
);
393 authctxt
->methoddata
= NULL
;
395 authctxt
->success
= 1; /* break out */
399 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
401 Authctxt
*authctxt
= ctxt
;
402 char *authlist
= NULL
;
405 if (authctxt
== NULL
)
406 fatal("input_userauth_failure: no authentication context");
408 authlist
= packet_get_string(NULL
);
409 partial
= packet_get_char();
413 logit("Authenticated with partial success.");
414 debug("Authentications that can continue: %s", authlist
);
416 userauth(authctxt
, authlist
);
419 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
421 Authctxt
*authctxt
= ctxt
;
425 int pktype
, sent
= 0;
430 if (authctxt
== NULL
)
431 fatal("input_userauth_pk_ok: no authentication context");
432 if (datafellows
& SSH_BUG_PKOK
) {
433 /* this is similar to SSH_BUG_PKAUTH */
434 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
435 pkblob
= packet_get_string(&blen
);
437 buffer_append(&b
, pkblob
, blen
);
438 pkalg
= buffer_get_string(&b
, &alen
);
441 pkalg
= packet_get_string(&alen
);
442 pkblob
= packet_get_string(&blen
);
446 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
448 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
449 debug("unknown pkalg %s", pkalg
);
452 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
453 debug("no key from blob. pkalg %s", pkalg
);
456 if (key
->type
!= pktype
) {
457 error("input_userauth_pk_ok: type mismatch "
458 "for decoded key (received %d, expected %d)",
462 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
463 debug2("input_userauth_pk_ok: fp %s", fp
);
467 * search keys in the reverse order, because last candidate has been
468 * moved to the end of the queue. this also avoids confusion by
471 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
472 if (key_equal(key
, id
->key
)) {
473 sent
= sign_and_send_pubkey(authctxt
, id
);
483 /* try another method if we did not send a packet */
485 userauth(authctxt
, NULL
);
490 userauth_gssapi(Authctxt
*authctxt
)
492 Gssctxt
*gssctxt
= NULL
;
493 static gss_OID_set gss_supported
= NULL
;
494 static u_int mech
= 0;
498 /* Try one GSSAPI method at a time, rather than sending them all at
501 if (gss_supported
== NULL
)
502 gss_indicate_mechs(&min
, &gss_supported
);
504 /* Check to see if the mechanism is usable before we offer it */
505 while (mech
< gss_supported
->count
&& !ok
) {
507 ssh_gssapi_delete_ctx(&gssctxt
);
508 ssh_gssapi_build_ctx(&gssctxt
);
509 ssh_gssapi_set_oid(gssctxt
, &gss_supported
->elements
[mech
]);
511 /* My DER encoding requires length<128 */
512 if (gss_supported
->elements
[mech
].length
< 128 &&
513 !GSS_ERROR(ssh_gssapi_import_name(gssctxt
,
515 ok
= 1; /* Mechanism works */
522 ssh_gssapi_delete_ctx(&gssctxt
);
526 authctxt
->methoddata
=(void *)gssctxt
;
528 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
529 packet_put_cstring(authctxt
->server_user
);
530 packet_put_cstring(authctxt
->service
);
531 packet_put_cstring(authctxt
->method
->name
);
535 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
536 packet_put_char(SSH_GSS_OIDTYPE
);
537 packet_put_char(gss_supported
->elements
[mech
].length
);
538 packet_put_raw(gss_supported
->elements
[mech
].elements
,
539 gss_supported
->elements
[mech
].length
);
543 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
544 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
545 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
546 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
548 mech
++; /* Move along to next candidate */
554 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
556 Authctxt
*authctxt
= ctxt
;
557 Gssctxt
*gssctxt
= authctxt
->methoddata
;
558 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
559 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
560 gss_buffer_desc gssbuf
;
561 OM_uint32 status
, ms
, flags
;
564 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
565 recv_tok
, &send_tok
, &flags
);
567 if (send_tok
.length
> 0) {
568 if (GSS_ERROR(status
))
569 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
571 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
573 packet_put_string(send_tok
.value
, send_tok
.length
);
575 gss_release_buffer(&ms
, &send_tok
);
578 if (status
== GSS_S_COMPLETE
) {
579 /* send either complete or MIC, depending on mechanism */
580 if (!(flags
& GSS_C_INTEG_FLAG
)) {
581 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
584 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
585 authctxt
->service
, "gssapi-with-mic");
587 gssbuf
.value
= buffer_ptr(&b
);
588 gssbuf
.length
= buffer_len(&b
);
590 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
592 if (!GSS_ERROR(status
)) {
593 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
594 packet_put_string(mic
.value
, mic
.length
);
600 gss_release_buffer(&ms
, &mic
);
608 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
610 Authctxt
*authctxt
= ctxt
;
615 if (authctxt
== NULL
)
616 fatal("input_gssapi_response: no authentication context");
617 gssctxt
= authctxt
->methoddata
;
620 oidv
= packet_get_string(&oidlen
);
623 oidv
[0] != SSH_GSS_OIDTYPE
||
624 oidv
[1] != oidlen
- 2) {
626 debug("Badly encoded mechanism OID received");
627 userauth(authctxt
, NULL
);
631 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
632 fatal("Server returned different OID than expected");
638 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
639 /* Start again with next method on list */
640 debug("Trying to start again");
641 userauth(authctxt
, NULL
);
647 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
649 Authctxt
*authctxt
= ctxt
;
650 gss_buffer_desc recv_tok
;
654 if (authctxt
== NULL
)
655 fatal("input_gssapi_response: no authentication context");
657 recv_tok
.value
= packet_get_string(&slen
);
658 recv_tok
.length
= slen
; /* safe typecast */
662 status
= process_gssapi_token(ctxt
, &recv_tok
);
664 xfree(recv_tok
.value
);
666 if (GSS_ERROR(status
)) {
667 /* Start again with the next method in the list */
668 userauth(authctxt
, NULL
);
674 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
676 Authctxt
*authctxt
= ctxt
;
678 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
679 gss_buffer_desc recv_tok
;
680 OM_uint32 status
, ms
;
683 if (authctxt
== NULL
)
684 fatal("input_gssapi_response: no authentication context");
685 gssctxt
= authctxt
->methoddata
;
687 recv_tok
.value
= packet_get_string(&len
);
688 recv_tok
.length
= len
;
692 /* Stick it into GSSAPI and see what it says */
693 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
694 &recv_tok
, &send_tok
, NULL
);
696 xfree(recv_tok
.value
);
697 gss_release_buffer(&ms
, &send_tok
);
699 /* Server will be returning a failed packet after this one */
703 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
709 maj
=packet_get_int();
710 min
=packet_get_int();
711 msg
=packet_get_string(NULL
);
712 lang
=packet_get_string(NULL
);
716 debug("Server GSSAPI Error:\n%s", msg
);
723 userauth_none(Authctxt
*authctxt
)
725 /* initial userauth request */
726 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
727 packet_put_cstring(authctxt
->server_user
);
728 packet_put_cstring(authctxt
->service
);
729 packet_put_cstring(authctxt
->method
->name
);
735 userauth_passwd(Authctxt
*authctxt
)
737 static int attempt
= 0;
741 if (attempt
++ >= options
.number_of_password_prompts
)
745 error("Permission denied, please try again.");
747 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
748 authctxt
->server_user
, authctxt
->host
);
749 password
= read_passphrase(prompt
, 0);
750 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
751 packet_put_cstring(authctxt
->server_user
);
752 packet_put_cstring(authctxt
->service
);
753 packet_put_cstring(authctxt
->method
->name
);
755 packet_put_cstring(password
);
756 memset(password
, 0, strlen(password
));
758 packet_add_padding(64);
761 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
762 &input_userauth_passwd_changereq
);
767 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
770 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
772 Authctxt
*authctxt
= ctxt
;
773 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
776 debug2("input_userauth_passwd_changereq");
778 if (authctxt
== NULL
)
779 fatal("input_userauth_passwd_changereq: "
780 "no authentication context");
782 info
= packet_get_string(NULL
);
783 lang
= packet_get_string(NULL
);
784 if (strlen(info
) > 0)
788 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
789 packet_put_cstring(authctxt
->server_user
);
790 packet_put_cstring(authctxt
->service
);
791 packet_put_cstring(authctxt
->method
->name
);
792 packet_put_char(1); /* additional info */
793 snprintf(prompt
, sizeof(prompt
),
794 "Enter %.30s@%.128s's old password: ",
795 authctxt
->server_user
, authctxt
->host
);
796 password
= read_passphrase(prompt
, 0);
797 packet_put_cstring(password
);
798 memset(password
, 0, strlen(password
));
801 while (password
== NULL
) {
802 snprintf(prompt
, sizeof(prompt
),
803 "Enter %.30s@%.128s's new password: ",
804 authctxt
->server_user
, authctxt
->host
);
805 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
806 if (password
== NULL
) {
810 snprintf(prompt
, sizeof(prompt
),
811 "Retype %.30s@%.128s's new password: ",
812 authctxt
->server_user
, authctxt
->host
);
813 retype
= read_passphrase(prompt
, 0);
814 if (strcmp(password
, retype
) != 0) {
815 memset(password
, 0, strlen(password
));
817 logit("Mismatch; try again, EOF to quit.");
820 memset(retype
, 0, strlen(retype
));
823 packet_put_cstring(password
);
824 memset(password
, 0, strlen(password
));
826 packet_add_padding(64);
829 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
830 &input_userauth_passwd_changereq
);
834 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
835 u_char
*data
, u_int datalen
)
840 /* the agent supports this key */
842 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
845 * we have already loaded the private key or
846 * the private key is stored in external hardware
848 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
849 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
850 /* load the private key from the file */
851 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
853 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
859 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
862 u_char
*blob
, *signature
;
868 debug3("sign_and_send_pubkey");
870 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
871 /* we cannot handle this key */
872 debug3("sign_and_send_pubkey: cannot handle key");
875 /* data to be signed */
877 if (datafellows
& SSH_OLD_SESSIONID
) {
878 buffer_append(&b
, session_id2
, session_id2_len
);
879 skip
= session_id2_len
;
881 buffer_put_string(&b
, session_id2
, session_id2_len
);
882 skip
= buffer_len(&b
);
884 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
885 buffer_put_cstring(&b
, authctxt
->server_user
);
886 buffer_put_cstring(&b
,
887 datafellows
& SSH_BUG_PKSERVICE
?
890 if (datafellows
& SSH_BUG_PKAUTH
) {
891 buffer_put_char(&b
, have_sig
);
893 buffer_put_cstring(&b
, authctxt
->method
->name
);
894 buffer_put_char(&b
, have_sig
);
895 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
897 buffer_put_string(&b
, blob
, bloblen
);
899 /* generate signature */
900 ret
= identity_sign(id
, &signature
, &slen
,
901 buffer_ptr(&b
), buffer_len(&b
));
910 if (datafellows
& SSH_BUG_PKSERVICE
) {
912 buffer_append(&b
, session_id2
, session_id2_len
);
913 skip
= session_id2_len
;
914 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
915 buffer_put_cstring(&b
, authctxt
->server_user
);
916 buffer_put_cstring(&b
, authctxt
->service
);
917 buffer_put_cstring(&b
, authctxt
->method
->name
);
918 buffer_put_char(&b
, have_sig
);
919 if (!(datafellows
& SSH_BUG_PKAUTH
))
920 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
921 buffer_put_string(&b
, blob
, bloblen
);
925 /* append signature */
926 buffer_put_string(&b
, signature
, slen
);
929 /* skip session id and packet type */
930 if (buffer_len(&b
) < skip
+ 1)
931 fatal("userauth_pubkey: internal error");
932 buffer_consume(&b
, skip
+ 1);
934 /* put remaining data from buffer into packet */
935 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
936 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
944 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
947 u_int bloblen
, have_sig
= 0;
949 debug3("send_pubkey_test");
951 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
952 /* we cannot handle this key */
953 debug3("send_pubkey_test: cannot handle key");
956 /* register callback for USERAUTH_PK_OK message */
957 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
959 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
960 packet_put_cstring(authctxt
->server_user
);
961 packet_put_cstring(authctxt
->service
);
962 packet_put_cstring(authctxt
->method
->name
);
963 packet_put_char(have_sig
);
964 if (!(datafellows
& SSH_BUG_PKAUTH
))
965 packet_put_cstring(key_ssh_name(id
->key
));
966 packet_put_string(blob
, bloblen
);
973 load_identity_file(char *filename
)
976 char prompt
[300], *passphrase
;
977 int perm_ok
, quit
, i
;
980 if (stat(filename
, &st
) < 0) {
981 debug3("no such identity: %s", filename
);
984 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
987 if (private == NULL
) {
988 if (options
.batch_mode
)
990 snprintf(prompt
, sizeof prompt
,
991 "Enter passphrase for key '%.100s': ", filename
);
992 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
993 passphrase
= read_passphrase(prompt
, 0);
994 if (strcmp(passphrase
, "") != 0) {
995 private = key_load_private_type(KEY_UNSPEC
,
996 filename
, passphrase
, NULL
, NULL
);
999 debug2("no passphrase given, try next key");
1002 memset(passphrase
, 0, strlen(passphrase
));
1004 if (private != NULL
|| quit
)
1006 debug2("bad passphrase given, try again...");
1013 * try keys in the following order:
1014 * 1. agent keys that are found in the config file
1015 * 2. other agent keys
1016 * 3. keys that are only listed in the config file
1019 pubkey_prepare(Authctxt
*authctxt
)
1022 Idlist agent
, files
, *preferred
;
1024 AuthenticationConnection
*ac
;
1028 TAILQ_INIT(&agent
); /* keys from the agent */
1029 TAILQ_INIT(&files
); /* keys from the config file */
1030 preferred
= &authctxt
->keys
;
1031 TAILQ_INIT(preferred
); /* preferred order of keys */
1033 /* list of keys stored in the filesystem */
1034 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1035 key
= options
.identity_keys
[i
];
1036 if (key
&& key
->type
== KEY_RSA1
)
1038 options
.identity_keys
[i
] = NULL
;
1039 id
= xcalloc(1, sizeof(*id
));
1041 id
->filename
= xstrdup(options
.identity_files
[i
]);
1042 TAILQ_INSERT_TAIL(&files
, id
, next
);
1044 /* list of keys supported by the agent */
1045 if ((ac
= ssh_get_authentication_connection())) {
1046 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1048 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1050 TAILQ_FOREACH(id
, &files
, next
) {
1051 /* agent keys from the config file are preferred */
1052 if (key_equal(key
, id
->key
)) {
1055 TAILQ_REMOVE(&files
, id
, next
);
1056 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1062 if (!found
&& !options
.identities_only
) {
1063 id
= xcalloc(1, sizeof(*id
));
1065 id
->filename
= comment
;
1067 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1070 /* append remaining agent keys */
1071 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1072 TAILQ_REMOVE(&agent
, id
, next
);
1073 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1075 authctxt
->agent
= ac
;
1077 /* append remaining keys from the config file */
1078 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1079 TAILQ_REMOVE(&files
, id
, next
);
1080 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1082 TAILQ_FOREACH(id
, preferred
, next
) {
1083 debug2("key: %s (%p)", id
->filename
, id
->key
);
1088 pubkey_cleanup(Authctxt
*authctxt
)
1092 if (authctxt
->agent
!= NULL
)
1093 ssh_close_authentication_connection(authctxt
->agent
);
1094 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1095 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1096 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1100 xfree(id
->filename
);
1106 userauth_pubkey(Authctxt
*authctxt
)
1111 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1114 /* move key to the end of the queue */
1115 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1116 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1118 * send a test message if we have the public key. for
1119 * encrypted keys we cannot do this and have to load the
1120 * private key instead
1122 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1123 debug("Offering public key: %s", id
->filename
);
1124 sent
= send_pubkey_test(authctxt
, id
);
1125 } else if (id
->key
== NULL
) {
1126 debug("Trying private key: %s", id
->filename
);
1127 id
->key
= load_identity_file(id
->filename
);
1128 if (id
->key
!= NULL
) {
1130 sent
= sign_and_send_pubkey(authctxt
, id
);
1142 * Send userauth request message specifying keyboard-interactive method.
1145 userauth_kbdint(Authctxt
*authctxt
)
1147 static int attempt
= 0;
1149 if (attempt
++ >= options
.number_of_password_prompts
)
1151 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1152 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1153 debug3("userauth_kbdint: disable: no info_req_seen");
1154 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1158 debug2("userauth_kbdint");
1159 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1160 packet_put_cstring(authctxt
->server_user
);
1161 packet_put_cstring(authctxt
->service
);
1162 packet_put_cstring(authctxt
->method
->name
);
1163 packet_put_cstring(""); /* lang */
1164 packet_put_cstring(options
.kbd_interactive_devices
?
1165 options
.kbd_interactive_devices
: "");
1168 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1173 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1176 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1178 Authctxt
*authctxt
= ctxt
;
1179 char *name
, *inst
, *lang
, *prompt
, *response
;
1180 u_int num_prompts
, i
;
1183 debug2("input_userauth_info_req");
1185 if (authctxt
== NULL
)
1186 fatal("input_userauth_info_req: no authentication context");
1188 authctxt
->info_req_seen
= 1;
1190 name
= packet_get_string(NULL
);
1191 inst
= packet_get_string(NULL
);
1192 lang
= packet_get_string(NULL
);
1193 if (strlen(name
) > 0)
1195 if (strlen(inst
) > 0)
1201 num_prompts
= packet_get_int();
1203 * Begin to build info response packet based on prompts requested.
1204 * We commit to providing the correct number of responses, so if
1205 * further on we run into a problem that prevents this, we have to
1206 * be sure and clean this up and send a correct error response.
1208 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1209 packet_put_int(num_prompts
);
1211 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1212 for (i
= 0; i
< num_prompts
; i
++) {
1213 prompt
= packet_get_string(NULL
);
1214 echo
= packet_get_char();
1216 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1218 packet_put_cstring(response
);
1219 memset(response
, 0, strlen(response
));
1223 packet_check_eom(); /* done with parsing incoming message. */
1225 packet_add_padding(64);
1230 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1231 u_char
*data
, u_int datalen
)
1236 int to
[2], from
[2], status
, version
= 2;
1238 debug2("ssh_keysign called");
1240 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1241 error("ssh_keysign: no installed: %s", strerror(errno
));
1244 if (fflush(stdout
) != 0)
1245 error("ssh_keysign: fflush: %s", strerror(errno
));
1247 error("ssh_keysign: pipe: %s", strerror(errno
));
1250 if (pipe(from
) < 0) {
1251 error("ssh_keysign: pipe: %s", strerror(errno
));
1254 if ((pid
= fork()) < 0) {
1255 error("ssh_keysign: fork: %s", strerror(errno
));
1259 permanently_drop_suid(getuid());
1261 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1262 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1264 if (dup2(to
[0], STDIN_FILENO
) < 0)
1265 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1268 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1269 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1276 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1277 buffer_put_string(&b
, data
, datalen
);
1278 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1279 fatal("ssh_keysign: couldn't send request");
1281 if (ssh_msg_recv(from
[0], &b
) < 0) {
1282 error("ssh_keysign: no reply");
1289 while (waitpid(pid
, &status
, 0) < 0)
1293 if (buffer_get_char(&b
) != version
) {
1294 error("ssh_keysign: bad version");
1298 *sigp
= buffer_get_string(&b
, lenp
);
1305 userauth_hostbased(Authctxt
*authctxt
)
1307 Key
*private = NULL
;
1308 Sensitive
*sensitive
= authctxt
->sensitive
;
1310 u_char
*signature
, *blob
;
1311 char *chost
, *pkalg
, *p
;
1312 const char *service
;
1314 int ok
, i
, len
, found
= 0;
1316 /* check for a useful key */
1317 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1318 private = sensitive
->keys
[i
];
1319 if (private && private->type
!= KEY_RSA1
) {
1321 /* we take and free the key */
1322 sensitive
->keys
[i
] = NULL
;
1327 debug("No more client hostkeys for hostbased authentication.");
1330 if (key_to_blob(private, &blob
, &blen
) == 0) {
1334 /* figure out a name for the client host */
1335 p
= get_local_name(packet_get_connection_in());
1337 error("userauth_hostbased: cannot get local ipaddr/name");
1342 len
= strlen(p
) + 2;
1343 xasprintf(&chost
, "%s.", p
);
1344 debug2("userauth_hostbased: chost %s", chost
);
1347 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1349 pkalg
= xstrdup(key_ssh_name(private));
1351 /* construct data */
1352 buffer_put_string(&b
, session_id2
, session_id2_len
);
1353 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1354 buffer_put_cstring(&b
, authctxt
->server_user
);
1355 buffer_put_cstring(&b
, service
);
1356 buffer_put_cstring(&b
, authctxt
->method
->name
);
1357 buffer_put_cstring(&b
, pkalg
);
1358 buffer_put_string(&b
, blob
, blen
);
1359 buffer_put_cstring(&b
, chost
);
1360 buffer_put_cstring(&b
, authctxt
->local_user
);
1364 if (sensitive
->external_keysign
)
1365 ok
= ssh_keysign(private, &signature
, &slen
,
1366 buffer_ptr(&b
), buffer_len(&b
));
1368 ok
= key_sign(private, &signature
, &slen
,
1369 buffer_ptr(&b
), buffer_len(&b
));
1373 error("key_sign failed");
1379 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1380 packet_put_cstring(authctxt
->server_user
);
1381 packet_put_cstring(authctxt
->service
);
1382 packet_put_cstring(authctxt
->method
->name
);
1383 packet_put_cstring(pkalg
);
1384 packet_put_string(blob
, blen
);
1385 packet_put_cstring(chost
);
1386 packet_put_cstring(authctxt
->local_user
);
1387 packet_put_string(signature
, slen
);
1388 memset(signature
, 's', slen
);
1398 /* find auth method */
1401 * given auth method name, if configurable options permit this method fill
1402 * in auth_ident field and return true, otherwise return false.
1405 authmethod_is_enabled(Authmethod
*method
)
1409 /* return false if options indicate this method is disabled */
1410 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1412 /* return false if batch mode is enabled but method needs interactive mode */
1413 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1419 authmethod_lookup(const char *name
)
1421 Authmethod
*method
= NULL
;
1423 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1424 if (strcmp(name
, method
->name
) == 0)
1426 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1430 /* XXX internal state */
1431 static Authmethod
*current
= NULL
;
1432 static char *supported
= NULL
;
1433 static char *preferred
= NULL
;
1436 * Given the authentication method list sent by the server, return the
1437 * next method we should try. If the server initially sends a nil list,
1438 * use a built-in default list.
1441 authmethod_get(char *authlist
)
1446 /* Use a suitable default if we're passed a nil list. */
1447 if (authlist
== NULL
|| strlen(authlist
) == 0)
1448 authlist
= options
.preferred_authentications
;
1450 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1451 debug3("start over, passed a different list %s", authlist
);
1452 if (supported
!= NULL
)
1454 supported
= xstrdup(authlist
);
1455 preferred
= options
.preferred_authentications
;
1456 debug3("preferred %s", preferred
);
1458 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1462 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1463 debug("No more authentication methods to try.");
1468 debug3("authmethod_lookup %s", name
);
1469 debug3("remaining preferred: %s", preferred
);
1470 if ((current
= authmethod_lookup(name
)) != NULL
&&
1471 authmethod_is_enabled(current
)) {
1472 debug3("authmethod_is_enabled %s", name
);
1473 debug("Next authentication method: %s", name
);
1480 authmethods_get(void)
1482 Authmethod
*method
= NULL
;
1487 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1488 if (authmethod_is_enabled(method
)) {
1489 if (buffer_len(&b
) > 0)
1490 buffer_append(&b
, ",", 1);
1491 buffer_append(&b
, method
->name
, strlen(method
->name
));
1494 buffer_append(&b
, "\0", 1);
1495 list
= xstrdup(buffer_ptr(&b
));