1 /* $OpenBSD: sshconnect2.c,v 1.160 2006/08/03 03:34:42 deraadt 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>
29 #include <sys/socket.h>
40 #include "openbsd-compat/sys-queue.h"
51 #include "myproposal.h"
52 #include "sshconnect.h"
63 #include "pathnames.h"
71 extern char *client_version_string
;
72 extern char *server_version_string
;
73 extern Options options
;
79 u_char
*session_id2
= NULL
;
80 u_int session_id2_len
= 0;
83 struct sockaddr
*xxx_hostaddr
;
88 verify_host_key_callback(Key
*hostkey
)
90 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
91 fatal("Host key verification failed.");
96 ssh_kex2(char *host
, struct sockaddr
*hostaddr
)
101 xxx_hostaddr
= hostaddr
;
103 if (options
.ciphers
== (char *)-1) {
104 logit("No valid ciphers for protocol version 2 given, using defaults.");
105 options
.ciphers
= NULL
;
107 if (options
.ciphers
!= NULL
) {
108 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
109 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
111 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
112 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
113 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
114 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
115 if (options
.compression
) {
116 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
117 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
119 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
120 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
122 if (options
.macs
!= NULL
) {
123 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
124 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
126 if (options
.hostkeyalgorithms
!= NULL
)
127 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
128 options
.hostkeyalgorithms
;
130 if (options
.rekey_limit
)
131 packet_set_rekey_limit(options
.rekey_limit
);
133 /* start key exchange */
134 kex
= kex_setup(myproposal
);
135 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
136 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
137 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
138 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
139 kex
->client_version_string
=client_version_string
;
140 kex
->server_version_string
=server_version_string
;
141 kex
->verify_host_key
=&verify_host_key_callback
;
145 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
147 session_id2
= kex
->session_id
;
148 session_id2_len
= kex
->session_id_len
;
151 /* send 1st encrypted/maced/compressed message */
152 packet_start(SSH2_MSG_IGNORE
);
153 packet_put_cstring("markus");
163 typedef struct Authctxt Authctxt
;
164 typedef struct Authmethod Authmethod
;
165 typedef struct identity Identity
;
166 typedef struct idlist Idlist
;
169 TAILQ_ENTRY(identity
) next
;
170 AuthenticationConnection
*ac
; /* set if agent supports key */
171 Key
*key
; /* public/private key */
172 char *filename
; /* comment for agent-only keys */
174 int isprivate
; /* key points to the private key */
176 TAILQ_HEAD(idlist
, identity
);
179 const char *server_user
;
180 const char *local_user
;
188 AuthenticationConnection
*agent
;
190 Sensitive
*sensitive
;
191 /* kbd-interactive */
197 char *name
; /* string to compare against server's list */
198 int (*userauth
)(Authctxt
*authctxt
);
199 int *enabled
; /* flag in option struct that enables method */
200 int *batch_flag
; /* flag in option struct that disables method */
203 void input_userauth_success(int, u_int32_t
, void *);
204 void input_userauth_failure(int, u_int32_t
, void *);
205 void input_userauth_banner(int, u_int32_t
, void *);
206 void input_userauth_error(int, u_int32_t
, void *);
207 void input_userauth_info_req(int, u_int32_t
, void *);
208 void input_userauth_pk_ok(int, u_int32_t
, void *);
209 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
211 int userauth_none(Authctxt
*);
212 int userauth_pubkey(Authctxt
*);
213 int userauth_passwd(Authctxt
*);
214 int userauth_kbdint(Authctxt
*);
215 int userauth_hostbased(Authctxt
*);
216 int userauth_kerberos(Authctxt
*);
219 int userauth_gssapi(Authctxt
*authctxt
);
220 void input_gssapi_response(int type
, u_int32_t
, void *);
221 void input_gssapi_token(int type
, u_int32_t
, void *);
222 void input_gssapi_hash(int type
, u_int32_t
, void *);
223 void input_gssapi_error(int, u_int32_t
, void *);
224 void input_gssapi_errtok(int, u_int32_t
, void *);
227 void userauth(Authctxt
*, char *);
229 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
230 static void pubkey_prepare(Authctxt
*);
231 static void pubkey_cleanup(Authctxt
*);
232 static Key
*load_identity_file(char *);
234 static Authmethod
*authmethod_get(char *authlist
);
235 static Authmethod
*authmethod_lookup(const char *name
);
236 static char *authmethods_get(void);
238 Authmethod authmethods
[] = {
242 &options
.gss_authentication
,
247 &options
.hostbased_authentication
,
251 &options
.pubkey_authentication
,
253 {"keyboard-interactive",
255 &options
.kbd_interactive_authentication
,
256 &options
.batch_mode
},
259 &options
.password_authentication
,
260 &options
.batch_mode
},
265 {NULL
, NULL
, NULL
, NULL
}
269 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
270 Sensitive
*sensitive
)
275 if (options
.challenge_response_authentication
)
276 options
.kbd_interactive_authentication
= 1;
278 packet_start(SSH2_MSG_SERVICE_REQUEST
);
279 packet_put_cstring("ssh-userauth");
281 debug("SSH2_MSG_SERVICE_REQUEST sent");
283 type
= packet_read();
284 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
285 fatal("Server denied authentication request: %d", type
);
286 if (packet_remaining() > 0) {
287 char *reply
= packet_get_string(NULL
);
288 debug2("service_accept: %s", reply
);
291 debug2("buggy server: service_accept w/o service");
294 debug("SSH2_MSG_SERVICE_ACCEPT received");
296 if (options
.preferred_authentications
== NULL
)
297 options
.preferred_authentications
= authmethods_get();
299 /* setup authentication context */
300 memset(&authctxt
, 0, sizeof(authctxt
));
301 pubkey_prepare(&authctxt
);
302 authctxt
.server_user
= server_user
;
303 authctxt
.local_user
= local_user
;
304 authctxt
.host
= host
;
305 authctxt
.service
= "ssh-connection"; /* service name */
306 authctxt
.success
= 0;
307 authctxt
.method
= authmethod_lookup("none");
308 authctxt
.authlist
= NULL
;
309 authctxt
.methoddata
= NULL
;
310 authctxt
.sensitive
= sensitive
;
311 authctxt
.info_req_seen
= 0;
312 if (authctxt
.method
== NULL
)
313 fatal("ssh_userauth2: internal error: cannot send userauth none request");
315 /* initial userauth request */
316 userauth_none(&authctxt
);
318 dispatch_init(&input_userauth_error
);
319 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
320 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
321 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
322 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
324 pubkey_cleanup(&authctxt
);
325 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
327 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
331 userauth(Authctxt
*authctxt
, char *authlist
)
333 if (authctxt
->methoddata
) {
334 xfree(authctxt
->methoddata
);
335 authctxt
->methoddata
= NULL
;
337 if (authlist
== NULL
) {
338 authlist
= authctxt
->authlist
;
340 if (authctxt
->authlist
)
341 xfree(authctxt
->authlist
);
342 authctxt
->authlist
= authlist
;
345 Authmethod
*method
= authmethod_get(authlist
);
347 fatal("Permission denied (%s).", authlist
);
348 authctxt
->method
= method
;
350 /* reset the per method handler */
351 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
352 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
354 /* and try new method */
355 if (method
->userauth(authctxt
) != 0) {
356 debug2("we sent a %s packet, wait for reply", method
->name
);
359 debug2("we did not send a packet, disable method");
360 method
->enabled
= NULL
;
366 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
368 fatal("input_userauth_error: bad message during authentication: "
373 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
377 debug3("input_userauth_banner");
378 msg
= packet_get_string(NULL
);
379 lang
= packet_get_string(NULL
);
380 if (options
.log_level
> SYSLOG_LEVEL_QUIET
)
381 fprintf(stderr
, "%s", msg
);
387 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
389 Authctxt
*authctxt
= ctxt
;
390 if (authctxt
== NULL
)
391 fatal("input_userauth_success: no authentication context");
392 if (authctxt
->authlist
) {
393 xfree(authctxt
->authlist
);
394 authctxt
->authlist
= NULL
;
396 if (authctxt
->methoddata
) {
397 xfree(authctxt
->methoddata
);
398 authctxt
->methoddata
= NULL
;
400 authctxt
->success
= 1; /* break out */
404 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
406 Authctxt
*authctxt
= ctxt
;
407 char *authlist
= NULL
;
410 if (authctxt
== NULL
)
411 fatal("input_userauth_failure: no authentication context");
413 authlist
= packet_get_string(NULL
);
414 partial
= packet_get_char();
418 logit("Authenticated with partial success.");
419 debug("Authentications that can continue: %s", authlist
);
421 userauth(authctxt
, authlist
);
424 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
426 Authctxt
*authctxt
= ctxt
;
430 int pktype
, sent
= 0;
435 if (authctxt
== NULL
)
436 fatal("input_userauth_pk_ok: no authentication context");
437 if (datafellows
& SSH_BUG_PKOK
) {
438 /* this is similar to SSH_BUG_PKAUTH */
439 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
440 pkblob
= packet_get_string(&blen
);
442 buffer_append(&b
, pkblob
, blen
);
443 pkalg
= buffer_get_string(&b
, &alen
);
446 pkalg
= packet_get_string(&alen
);
447 pkblob
= packet_get_string(&blen
);
451 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
453 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
454 debug("unknown pkalg %s", pkalg
);
457 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
458 debug("no key from blob. pkalg %s", pkalg
);
461 if (key
->type
!= pktype
) {
462 error("input_userauth_pk_ok: type mismatch "
463 "for decoded key (received %d, expected %d)",
467 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
468 debug2("input_userauth_pk_ok: fp %s", fp
);
472 * search keys in the reverse order, because last candidate has been
473 * moved to the end of the queue. this also avoids confusion by
476 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
477 if (key_equal(key
, id
->key
)) {
478 sent
= sign_and_send_pubkey(authctxt
, id
);
488 /* try another method if we did not send a packet */
490 userauth(authctxt
, NULL
);
495 userauth_gssapi(Authctxt
*authctxt
)
497 Gssctxt
*gssctxt
= NULL
;
498 static gss_OID_set gss_supported
= NULL
;
499 static u_int mech
= 0;
503 /* Try one GSSAPI method at a time, rather than sending them all at
506 if (gss_supported
== NULL
)
507 gss_indicate_mechs(&min
, &gss_supported
);
509 /* Check to see if the mechanism is usable before we offer it */
510 while (mech
< gss_supported
->count
&& !ok
) {
512 ssh_gssapi_delete_ctx(&gssctxt
);
513 ssh_gssapi_build_ctx(&gssctxt
);
514 ssh_gssapi_set_oid(gssctxt
, &gss_supported
->elements
[mech
]);
516 /* My DER encoding requires length<128 */
517 if (gss_supported
->elements
[mech
].length
< 128 &&
518 !GSS_ERROR(ssh_gssapi_import_name(gssctxt
,
520 ok
= 1; /* Mechanism works */
527 ssh_gssapi_delete_ctx(&gssctxt
);
531 authctxt
->methoddata
=(void *)gssctxt
;
533 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
534 packet_put_cstring(authctxt
->server_user
);
535 packet_put_cstring(authctxt
->service
);
536 packet_put_cstring(authctxt
->method
->name
);
540 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
541 packet_put_char(SSH_GSS_OIDTYPE
);
542 packet_put_char(gss_supported
->elements
[mech
].length
);
543 packet_put_raw(gss_supported
->elements
[mech
].elements
,
544 gss_supported
->elements
[mech
].length
);
548 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
549 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
550 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
551 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
553 mech
++; /* Move along to next candidate */
559 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
561 Authctxt
*authctxt
= ctxt
;
562 Gssctxt
*gssctxt
= authctxt
->methoddata
;
563 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
564 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
565 gss_buffer_desc gssbuf
;
566 OM_uint32 status
, ms
, flags
;
569 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
570 recv_tok
, &send_tok
, &flags
);
572 if (send_tok
.length
> 0) {
573 if (GSS_ERROR(status
))
574 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
576 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
578 packet_put_string(send_tok
.value
, send_tok
.length
);
580 gss_release_buffer(&ms
, &send_tok
);
583 if (status
== GSS_S_COMPLETE
) {
584 /* send either complete or MIC, depending on mechanism */
585 if (!(flags
& GSS_C_INTEG_FLAG
)) {
586 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
589 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
590 authctxt
->service
, "gssapi-with-mic");
592 gssbuf
.value
= buffer_ptr(&b
);
593 gssbuf
.length
= buffer_len(&b
);
595 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
597 if (!GSS_ERROR(status
)) {
598 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
599 packet_put_string(mic
.value
, mic
.length
);
605 gss_release_buffer(&ms
, &mic
);
613 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
615 Authctxt
*authctxt
= ctxt
;
620 if (authctxt
== NULL
)
621 fatal("input_gssapi_response: no authentication context");
622 gssctxt
= authctxt
->methoddata
;
625 oidv
= packet_get_string(&oidlen
);
628 oidv
[0] != SSH_GSS_OIDTYPE
||
629 oidv
[1] != oidlen
- 2) {
631 debug("Badly encoded mechanism OID received");
632 userauth(authctxt
, NULL
);
636 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
637 fatal("Server returned different OID than expected");
643 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
644 /* Start again with next method on list */
645 debug("Trying to start again");
646 userauth(authctxt
, NULL
);
652 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
654 Authctxt
*authctxt
= ctxt
;
655 gss_buffer_desc recv_tok
;
659 if (authctxt
== NULL
)
660 fatal("input_gssapi_response: no authentication context");
662 recv_tok
.value
= packet_get_string(&slen
);
663 recv_tok
.length
= slen
; /* safe typecast */
667 status
= process_gssapi_token(ctxt
, &recv_tok
);
669 xfree(recv_tok
.value
);
671 if (GSS_ERROR(status
)) {
672 /* Start again with the next method in the list */
673 userauth(authctxt
, NULL
);
679 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
681 Authctxt
*authctxt
= ctxt
;
683 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
684 gss_buffer_desc recv_tok
;
685 OM_uint32 status
, ms
;
688 if (authctxt
== NULL
)
689 fatal("input_gssapi_response: no authentication context");
690 gssctxt
= authctxt
->methoddata
;
692 recv_tok
.value
= packet_get_string(&len
);
693 recv_tok
.length
= len
;
697 /* Stick it into GSSAPI and see what it says */
698 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
699 &recv_tok
, &send_tok
, NULL
);
701 xfree(recv_tok
.value
);
702 gss_release_buffer(&ms
, &send_tok
);
704 /* Server will be returning a failed packet after this one */
708 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
714 maj
=packet_get_int();
715 min
=packet_get_int();
716 msg
=packet_get_string(NULL
);
717 lang
=packet_get_string(NULL
);
721 debug("Server GSSAPI Error:\n%s", msg
);
728 userauth_none(Authctxt
*authctxt
)
730 /* initial userauth request */
731 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
732 packet_put_cstring(authctxt
->server_user
);
733 packet_put_cstring(authctxt
->service
);
734 packet_put_cstring(authctxt
->method
->name
);
740 userauth_passwd(Authctxt
*authctxt
)
742 static int attempt
= 0;
746 if (attempt
++ >= options
.number_of_password_prompts
)
750 error("Permission denied, please try again.");
752 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
753 authctxt
->server_user
, authctxt
->host
);
754 password
= read_passphrase(prompt
, 0);
755 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
756 packet_put_cstring(authctxt
->server_user
);
757 packet_put_cstring(authctxt
->service
);
758 packet_put_cstring(authctxt
->method
->name
);
760 packet_put_cstring(password
);
761 memset(password
, 0, strlen(password
));
763 packet_add_padding(64);
766 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
767 &input_userauth_passwd_changereq
);
772 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
775 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
777 Authctxt
*authctxt
= ctxt
;
778 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
781 debug2("input_userauth_passwd_changereq");
783 if (authctxt
== NULL
)
784 fatal("input_userauth_passwd_changereq: "
785 "no authentication context");
787 info
= packet_get_string(NULL
);
788 lang
= packet_get_string(NULL
);
789 if (strlen(info
) > 0)
793 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
794 packet_put_cstring(authctxt
->server_user
);
795 packet_put_cstring(authctxt
->service
);
796 packet_put_cstring(authctxt
->method
->name
);
797 packet_put_char(1); /* additional info */
798 snprintf(prompt
, sizeof(prompt
),
799 "Enter %.30s@%.128s's old password: ",
800 authctxt
->server_user
, authctxt
->host
);
801 password
= read_passphrase(prompt
, 0);
802 packet_put_cstring(password
);
803 memset(password
, 0, strlen(password
));
806 while (password
== NULL
) {
807 snprintf(prompt
, sizeof(prompt
),
808 "Enter %.30s@%.128s's new password: ",
809 authctxt
->server_user
, authctxt
->host
);
810 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
811 if (password
== NULL
) {
815 snprintf(prompt
, sizeof(prompt
),
816 "Retype %.30s@%.128s's new password: ",
817 authctxt
->server_user
, authctxt
->host
);
818 retype
= read_passphrase(prompt
, 0);
819 if (strcmp(password
, retype
) != 0) {
820 memset(password
, 0, strlen(password
));
822 logit("Mismatch; try again, EOF to quit.");
825 memset(retype
, 0, strlen(retype
));
828 packet_put_cstring(password
);
829 memset(password
, 0, strlen(password
));
831 packet_add_padding(64);
834 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
835 &input_userauth_passwd_changereq
);
839 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
840 u_char
*data
, u_int datalen
)
845 /* the agent supports this key */
847 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
850 * we have already loaded the private key or
851 * the private key is stored in external hardware
853 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
854 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
855 /* load the private key from the file */
856 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
858 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
864 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
867 u_char
*blob
, *signature
;
873 debug3("sign_and_send_pubkey");
875 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
876 /* we cannot handle this key */
877 debug3("sign_and_send_pubkey: cannot handle key");
880 /* data to be signed */
882 if (datafellows
& SSH_OLD_SESSIONID
) {
883 buffer_append(&b
, session_id2
, session_id2_len
);
884 skip
= session_id2_len
;
886 buffer_put_string(&b
, session_id2
, session_id2_len
);
887 skip
= buffer_len(&b
);
889 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
890 buffer_put_cstring(&b
, authctxt
->server_user
);
891 buffer_put_cstring(&b
,
892 datafellows
& SSH_BUG_PKSERVICE
?
895 if (datafellows
& SSH_BUG_PKAUTH
) {
896 buffer_put_char(&b
, have_sig
);
898 buffer_put_cstring(&b
, authctxt
->method
->name
);
899 buffer_put_char(&b
, have_sig
);
900 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
902 buffer_put_string(&b
, blob
, bloblen
);
904 /* generate signature */
905 ret
= identity_sign(id
, &signature
, &slen
,
906 buffer_ptr(&b
), buffer_len(&b
));
915 if (datafellows
& SSH_BUG_PKSERVICE
) {
917 buffer_append(&b
, session_id2
, session_id2_len
);
918 skip
= session_id2_len
;
919 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
920 buffer_put_cstring(&b
, authctxt
->server_user
);
921 buffer_put_cstring(&b
, authctxt
->service
);
922 buffer_put_cstring(&b
, authctxt
->method
->name
);
923 buffer_put_char(&b
, have_sig
);
924 if (!(datafellows
& SSH_BUG_PKAUTH
))
925 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
926 buffer_put_string(&b
, blob
, bloblen
);
930 /* append signature */
931 buffer_put_string(&b
, signature
, slen
);
934 /* skip session id and packet type */
935 if (buffer_len(&b
) < skip
+ 1)
936 fatal("userauth_pubkey: internal error");
937 buffer_consume(&b
, skip
+ 1);
939 /* put remaining data from buffer into packet */
940 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
941 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
949 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
952 u_int bloblen
, have_sig
= 0;
954 debug3("send_pubkey_test");
956 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
957 /* we cannot handle this key */
958 debug3("send_pubkey_test: cannot handle key");
961 /* register callback for USERAUTH_PK_OK message */
962 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
964 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
965 packet_put_cstring(authctxt
->server_user
);
966 packet_put_cstring(authctxt
->service
);
967 packet_put_cstring(authctxt
->method
->name
);
968 packet_put_char(have_sig
);
969 if (!(datafellows
& SSH_BUG_PKAUTH
))
970 packet_put_cstring(key_ssh_name(id
->key
));
971 packet_put_string(blob
, bloblen
);
978 load_identity_file(char *filename
)
981 char prompt
[300], *passphrase
;
982 int perm_ok
, quit
, i
;
985 if (stat(filename
, &st
) < 0) {
986 debug3("no such identity: %s", filename
);
989 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
992 if (private == NULL
) {
993 if (options
.batch_mode
)
995 snprintf(prompt
, sizeof prompt
,
996 "Enter passphrase for key '%.100s': ", filename
);
997 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
998 passphrase
= read_passphrase(prompt
, 0);
999 if (strcmp(passphrase
, "") != 0) {
1000 private = key_load_private_type(KEY_UNSPEC
,
1001 filename
, passphrase
, NULL
, NULL
);
1004 debug2("no passphrase given, try next key");
1007 memset(passphrase
, 0, strlen(passphrase
));
1009 if (private != NULL
|| quit
)
1011 debug2("bad passphrase given, try again...");
1018 * try keys in the following order:
1019 * 1. agent keys that are found in the config file
1020 * 2. other agent keys
1021 * 3. keys that are only listed in the config file
1024 pubkey_prepare(Authctxt
*authctxt
)
1027 Idlist agent
, files
, *preferred
;
1029 AuthenticationConnection
*ac
;
1033 TAILQ_INIT(&agent
); /* keys from the agent */
1034 TAILQ_INIT(&files
); /* keys from the config file */
1035 preferred
= &authctxt
->keys
;
1036 TAILQ_INIT(preferred
); /* preferred order of keys */
1038 /* list of keys stored in the filesystem */
1039 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1040 key
= options
.identity_keys
[i
];
1041 if (key
&& key
->type
== KEY_RSA1
)
1043 options
.identity_keys
[i
] = NULL
;
1044 id
= xcalloc(1, sizeof(*id
));
1046 id
->filename
= xstrdup(options
.identity_files
[i
]);
1047 TAILQ_INSERT_TAIL(&files
, id
, next
);
1049 /* list of keys supported by the agent */
1050 if ((ac
= ssh_get_authentication_connection())) {
1051 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1053 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1055 TAILQ_FOREACH(id
, &files
, next
) {
1056 /* agent keys from the config file are preferred */
1057 if (key_equal(key
, id
->key
)) {
1060 TAILQ_REMOVE(&files
, id
, next
);
1061 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1067 if (!found
&& !options
.identities_only
) {
1068 id
= xcalloc(1, sizeof(*id
));
1070 id
->filename
= comment
;
1072 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1075 /* append remaining agent keys */
1076 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1077 TAILQ_REMOVE(&agent
, id
, next
);
1078 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1080 authctxt
->agent
= ac
;
1082 /* append remaining keys from the config file */
1083 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1084 TAILQ_REMOVE(&files
, id
, next
);
1085 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1087 TAILQ_FOREACH(id
, preferred
, next
) {
1088 debug2("key: %s (%p)", id
->filename
, id
->key
);
1093 pubkey_cleanup(Authctxt
*authctxt
)
1097 if (authctxt
->agent
!= NULL
)
1098 ssh_close_authentication_connection(authctxt
->agent
);
1099 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1100 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1101 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1105 xfree(id
->filename
);
1111 userauth_pubkey(Authctxt
*authctxt
)
1116 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1119 /* move key to the end of the queue */
1120 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1121 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1123 * send a test message if we have the public key. for
1124 * encrypted keys we cannot do this and have to load the
1125 * private key instead
1127 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1128 debug("Offering public key: %s", id
->filename
);
1129 sent
= send_pubkey_test(authctxt
, id
);
1130 } else if (id
->key
== NULL
) {
1131 debug("Trying private key: %s", id
->filename
);
1132 id
->key
= load_identity_file(id
->filename
);
1133 if (id
->key
!= NULL
) {
1135 sent
= sign_and_send_pubkey(authctxt
, id
);
1147 * Send userauth request message specifying keyboard-interactive method.
1150 userauth_kbdint(Authctxt
*authctxt
)
1152 static int attempt
= 0;
1154 if (attempt
++ >= options
.number_of_password_prompts
)
1156 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1157 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1158 debug3("userauth_kbdint: disable: no info_req_seen");
1159 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1163 debug2("userauth_kbdint");
1164 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1165 packet_put_cstring(authctxt
->server_user
);
1166 packet_put_cstring(authctxt
->service
);
1167 packet_put_cstring(authctxt
->method
->name
);
1168 packet_put_cstring(""); /* lang */
1169 packet_put_cstring(options
.kbd_interactive_devices
?
1170 options
.kbd_interactive_devices
: "");
1173 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1178 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1181 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1183 Authctxt
*authctxt
= ctxt
;
1184 char *name
, *inst
, *lang
, *prompt
, *response
;
1185 u_int num_prompts
, i
;
1188 debug2("input_userauth_info_req");
1190 if (authctxt
== NULL
)
1191 fatal("input_userauth_info_req: no authentication context");
1193 authctxt
->info_req_seen
= 1;
1195 name
= packet_get_string(NULL
);
1196 inst
= packet_get_string(NULL
);
1197 lang
= packet_get_string(NULL
);
1198 if (strlen(name
) > 0)
1200 if (strlen(inst
) > 0)
1206 num_prompts
= packet_get_int();
1208 * Begin to build info response packet based on prompts requested.
1209 * We commit to providing the correct number of responses, so if
1210 * further on we run into a problem that prevents this, we have to
1211 * be sure and clean this up and send a correct error response.
1213 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1214 packet_put_int(num_prompts
);
1216 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1217 for (i
= 0; i
< num_prompts
; i
++) {
1218 prompt
= packet_get_string(NULL
);
1219 echo
= packet_get_char();
1221 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1223 packet_put_cstring(response
);
1224 memset(response
, 0, strlen(response
));
1228 packet_check_eom(); /* done with parsing incoming message. */
1230 packet_add_padding(64);
1235 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1236 u_char
*data
, u_int datalen
)
1241 int to
[2], from
[2], status
, version
= 2;
1243 debug2("ssh_keysign called");
1245 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1246 error("ssh_keysign: no installed: %s", strerror(errno
));
1249 if (fflush(stdout
) != 0)
1250 error("ssh_keysign: fflush: %s", strerror(errno
));
1252 error("ssh_keysign: pipe: %s", strerror(errno
));
1255 if (pipe(from
) < 0) {
1256 error("ssh_keysign: pipe: %s", strerror(errno
));
1259 if ((pid
= fork()) < 0) {
1260 error("ssh_keysign: fork: %s", strerror(errno
));
1264 permanently_drop_suid(getuid());
1266 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1267 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1269 if (dup2(to
[0], STDIN_FILENO
) < 0)
1270 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1273 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1274 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1281 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1282 buffer_put_string(&b
, data
, datalen
);
1283 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1284 fatal("ssh_keysign: couldn't send request");
1286 if (ssh_msg_recv(from
[0], &b
) < 0) {
1287 error("ssh_keysign: no reply");
1294 while (waitpid(pid
, &status
, 0) < 0)
1298 if (buffer_get_char(&b
) != version
) {
1299 error("ssh_keysign: bad version");
1303 *sigp
= buffer_get_string(&b
, lenp
);
1310 userauth_hostbased(Authctxt
*authctxt
)
1312 Key
*private = NULL
;
1313 Sensitive
*sensitive
= authctxt
->sensitive
;
1315 u_char
*signature
, *blob
;
1316 char *chost
, *pkalg
, *p
;
1317 const char *service
;
1319 int ok
, i
, len
, found
= 0;
1321 /* check for a useful key */
1322 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1323 private = sensitive
->keys
[i
];
1324 if (private && private->type
!= KEY_RSA1
) {
1326 /* we take and free the key */
1327 sensitive
->keys
[i
] = NULL
;
1332 debug("No more client hostkeys for hostbased authentication.");
1335 if (key_to_blob(private, &blob
, &blen
) == 0) {
1339 /* figure out a name for the client host */
1340 p
= get_local_name(packet_get_connection_in());
1342 error("userauth_hostbased: cannot get local ipaddr/name");
1347 len
= strlen(p
) + 2;
1348 xasprintf(&chost
, "%s.", p
);
1349 debug2("userauth_hostbased: chost %s", chost
);
1352 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1354 pkalg
= xstrdup(key_ssh_name(private));
1356 /* construct data */
1357 buffer_put_string(&b
, session_id2
, session_id2_len
);
1358 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1359 buffer_put_cstring(&b
, authctxt
->server_user
);
1360 buffer_put_cstring(&b
, service
);
1361 buffer_put_cstring(&b
, authctxt
->method
->name
);
1362 buffer_put_cstring(&b
, pkalg
);
1363 buffer_put_string(&b
, blob
, blen
);
1364 buffer_put_cstring(&b
, chost
);
1365 buffer_put_cstring(&b
, authctxt
->local_user
);
1369 if (sensitive
->external_keysign
)
1370 ok
= ssh_keysign(private, &signature
, &slen
,
1371 buffer_ptr(&b
), buffer_len(&b
));
1373 ok
= key_sign(private, &signature
, &slen
,
1374 buffer_ptr(&b
), buffer_len(&b
));
1378 error("key_sign failed");
1384 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1385 packet_put_cstring(authctxt
->server_user
);
1386 packet_put_cstring(authctxt
->service
);
1387 packet_put_cstring(authctxt
->method
->name
);
1388 packet_put_cstring(pkalg
);
1389 packet_put_string(blob
, blen
);
1390 packet_put_cstring(chost
);
1391 packet_put_cstring(authctxt
->local_user
);
1392 packet_put_string(signature
, slen
);
1393 memset(signature
, 's', slen
);
1403 /* find auth method */
1406 * given auth method name, if configurable options permit this method fill
1407 * in auth_ident field and return true, otherwise return false.
1410 authmethod_is_enabled(Authmethod
*method
)
1414 /* return false if options indicate this method is disabled */
1415 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1417 /* return false if batch mode is enabled but method needs interactive mode */
1418 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1424 authmethod_lookup(const char *name
)
1426 Authmethod
*method
= NULL
;
1428 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1429 if (strcmp(name
, method
->name
) == 0)
1431 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1435 /* XXX internal state */
1436 static Authmethod
*current
= NULL
;
1437 static char *supported
= NULL
;
1438 static char *preferred
= NULL
;
1441 * Given the authentication method list sent by the server, return the
1442 * next method we should try. If the server initially sends a nil list,
1443 * use a built-in default list.
1446 authmethod_get(char *authlist
)
1451 /* Use a suitable default if we're passed a nil list. */
1452 if (authlist
== NULL
|| strlen(authlist
) == 0)
1453 authlist
= options
.preferred_authentications
;
1455 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1456 debug3("start over, passed a different list %s", authlist
);
1457 if (supported
!= NULL
)
1459 supported
= xstrdup(authlist
);
1460 preferred
= options
.preferred_authentications
;
1461 debug3("preferred %s", preferred
);
1463 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1467 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1468 debug("No more authentication methods to try.");
1473 debug3("authmethod_lookup %s", name
);
1474 debug3("remaining preferred: %s", preferred
);
1475 if ((current
= authmethod_lookup(name
)) != NULL
&&
1476 authmethod_is_enabled(current
)) {
1477 debug3("authmethod_is_enabled %s", name
);
1478 debug("Next authentication method: %s", name
);
1485 authmethods_get(void)
1487 Authmethod
*method
= NULL
;
1492 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1493 if (authmethod_is_enabled(method
)) {
1494 if (buffer_len(&b
) > 0)
1495 buffer_append(&b
, ",", 1);
1496 buffer_append(&b
, method
->name
, strlen(method
->name
));
1499 buffer_append(&b
, "\0", 1);
1500 list
= xstrdup(buffer_ptr(&b
));