2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
16 RCSID("$OpenBSD: sshconnect1.c,v 1.54 2003/07/22 13:35:22 markus Exp $");
18 #include <openssl/bn.h>
19 #include <openssl/md5.h>
24 #define krb5_get_err_text(context,code) error_message(code)
40 #include "sshconnect.h"
47 /* Session id for the current session. */
48 u_char session_id[16];
49 u_int supported_authentications = 0;
51 extern Options options;
52 extern char *__progname;
55 * Checks if the user has an authentication agent, and if so, tries to
56 * authenticate using the agent.
59 try_agent_authentication(void)
63 AuthenticationConnection *auth;
69 /* Get connection to the agent. */
70 auth = ssh_get_authentication_connection();
74 if ((challenge = BN_new()) == NULL)
75 fatal("try_agent_authentication: BN_new failed");
76 /* Loop through identities served by the agent. */
77 for (key = ssh_get_first_identity(auth, &comment, 1);
79 key = ssh_get_next_identity(auth, &comment, 1)) {
81 /* Try this identity. */
82 debug("Trying RSA authentication via agent with '%.100s'", comment);
85 /* Tell the server that we are willing to authenticate using this key. */
86 packet_start(SSH_CMSG_AUTH_RSA);
87 packet_put_bignum(key->rsa->n);
91 /* Wait for server's response. */
94 /* The server sends failure if it doesn\'t like our key or
95 does not support RSA authentication. */
96 if (type == SSH_SMSG_FAILURE) {
97 debug("Server refused our key.");
101 /* Otherwise it should have sent a challenge. */
102 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
103 packet_disconnect("Protocol error during RSA authentication: %d",
106 packet_get_bignum(challenge);
109 debug("Received RSA challenge from server.");
111 /* Ask the agent to decrypt the challenge. */
112 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
114 * The agent failed to authenticate this identifier
115 * although it advertised it supports this. Just
116 * return a wrong value.
118 logit("Authentication agent failed to decrypt challenge.");
119 memset(response, 0, sizeof(response));
122 debug("Sending response to RSA challenge.");
124 /* Send the decrypted challenge back to the server. */
125 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
126 for (i = 0; i < 16; i++)
127 packet_put_char(response[i]);
131 /* Wait for response from the server. */
132 type = packet_read();
134 /* The server returns success if it accepted the authentication. */
135 if (type == SSH_SMSG_SUCCESS) {
136 ssh_close_authentication_connection(auth);
137 BN_clear_free(challenge);
138 debug("RSA authentication accepted by server.");
141 /* Otherwise it should return failure. */
142 if (type != SSH_SMSG_FAILURE)
143 packet_disconnect("Protocol error waiting RSA auth response: %d",
146 ssh_close_authentication_connection(auth);
147 BN_clear_free(challenge);
148 debug("RSA authentication using agent refused.");
153 * Computes the proper response to a RSA challenge, and sends the response to
157 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
159 u_char buf[32], response[16];
163 /* Decrypt the challenge using the private key. */
164 /* XXX think about Bleichenbacher, too */
165 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
167 "respond_to_rsa_challenge: rsa_private_decrypt failed");
169 /* Compute the response. */
170 /* The response is MD5 of decrypted challenge plus session id. */
171 len = BN_num_bytes(challenge);
172 if (len <= 0 || len > sizeof(buf))
174 "respond_to_rsa_challenge: bad challenge length %d", len);
176 memset(buf, 0, sizeof(buf));
177 BN_bn2bin(challenge, buf + sizeof(buf) - len);
179 MD5_Update(&md, buf, 32);
180 MD5_Update(&md, session_id, 16);
181 MD5_Final(response, &md);
183 debug("Sending response to host key RSA challenge.");
185 /* Send the response back to the server. */
186 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
187 for (i = 0; i < 16; i++)
188 packet_put_char(response[i]);
192 memset(buf, 0, sizeof(buf));
193 memset(response, 0, sizeof(response));
194 memset(&md, 0, sizeof(md));
198 * Checks if the user has authentication file, and if so, tries to authenticate
202 try_rsa_authentication(int idx)
205 Key *public, *private;
206 char buf[300], *passphrase, *comment, *authfile;
209 public = options.identity_keys[idx];
210 authfile = options.identity_files[idx];
211 comment = xstrdup(authfile);
213 debug("Trying RSA authentication with key '%.100s'", comment);
215 /* Tell the server that we are willing to authenticate using this key. */
216 packet_start(SSH_CMSG_AUTH_RSA);
217 packet_put_bignum(public->rsa->n);
221 /* Wait for server's response. */
222 type = packet_read();
225 * The server responds with failure if it doesn\'t like our key or
226 * doesn\'t support RSA authentication.
228 if (type == SSH_SMSG_FAILURE) {
229 debug("Server refused our key.");
233 /* Otherwise, the server should respond with a challenge. */
234 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
235 packet_disconnect("Protocol error during RSA authentication: %d", type);
237 /* Get the challenge from the packet. */
238 if ((challenge = BN_new()) == NULL)
239 fatal("try_rsa_authentication: BN_new failed");
240 packet_get_bignum(challenge);
243 debug("Received RSA challenge from server.");
246 * If the key is not stored in external hardware, we have to
247 * load the private key. Try first with empty passphrase; if it
248 * fails, ask for a passphrase.
250 if (public->flags & KEY_FLAG_EXT)
253 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
254 if (private == NULL && !options.batch_mode) {
255 snprintf(buf, sizeof(buf),
256 "Enter passphrase for RSA key '%.100s': ", comment);
257 for (i = 0; i < options.number_of_password_prompts; i++) {
258 passphrase = read_passphrase(buf, 0);
259 if (strcmp(passphrase, "") != 0) {
260 private = key_load_private_type(KEY_RSA1,
261 authfile, passphrase, NULL);
264 debug2("no passphrase given, try next key");
267 memset(passphrase, 0, strlen(passphrase));
269 if (private != NULL || quit)
271 debug2("bad passphrase given, try again...");
274 /* We no longer need the comment. */
277 if (private == NULL) {
278 if (!options.batch_mode)
279 error("Bad passphrase.");
281 /* Send a dummy response packet to avoid protocol error. */
282 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
283 for (i = 0; i < 16; i++)
288 /* Expect the server to reject it... */
289 packet_read_expect(SSH_SMSG_FAILURE);
290 BN_clear_free(challenge);
294 /* Compute and send a response to the challenge. */
295 respond_to_rsa_challenge(challenge, private->rsa);
297 /* Destroy the private key unless it in external hardware. */
298 if (!(private->flags & KEY_FLAG_EXT))
301 /* We no longer need the challenge. */
302 BN_clear_free(challenge);
304 /* Wait for response from the server. */
305 type = packet_read();
306 if (type == SSH_SMSG_SUCCESS) {
307 debug("RSA authentication accepted by server.");
310 if (type != SSH_SMSG_FAILURE)
311 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
312 debug("RSA authentication refused.");
317 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
318 * authentication and RSA host authentication.
321 try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
326 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
328 /* Tell the server that we are willing to authenticate using this key. */
329 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
330 packet_put_cstring(local_user);
331 packet_put_int(BN_num_bits(host_key->rsa->n));
332 packet_put_bignum(host_key->rsa->e);
333 packet_put_bignum(host_key->rsa->n);
337 /* Wait for server's response. */
338 type = packet_read();
340 /* The server responds with failure if it doesn't admit our
341 .rhosts authentication or doesn't know our host key. */
342 if (type == SSH_SMSG_FAILURE) {
343 debug("Server refused our rhosts authentication or host key.");
346 /* Otherwise, the server should respond with a challenge. */
347 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
348 packet_disconnect("Protocol error during RSA authentication: %d", type);
350 /* Get the challenge from the packet. */
351 if ((challenge = BN_new()) == NULL)
352 fatal("try_rhosts_rsa_authentication: BN_new failed");
353 packet_get_bignum(challenge);
356 debug("Received RSA challenge for host key from server.");
358 /* Compute a response to the challenge. */
359 respond_to_rsa_challenge(challenge, host_key->rsa);
361 /* We no longer need the challenge. */
362 BN_clear_free(challenge);
364 /* Wait for response from the server. */
365 type = packet_read();
366 if (type == SSH_SMSG_SUCCESS) {
367 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
370 if (type != SSH_SMSG_FAILURE)
371 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
372 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
378 try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
380 krb5_error_code problem;
383 krb5_ccache ccache = NULL;
384 const char *remotehost;
387 krb5_ap_rep_enc_part *reply = NULL;
390 memset(&ap, 0, sizeof(ap));
392 problem = krb5_init_context(context);
394 debug("Kerberos v5: krb5_init_context failed");
399 problem = krb5_auth_con_init(*context, auth_context);
401 debug("Kerberos v5: krb5_auth_con_init failed");
407 problem = krb5_auth_con_setflags(*context, *auth_context,
408 KRB5_AUTH_CONTEXT_RET_TIME);
410 debug("Keberos v5: krb5_auth_con_setflags failed");
416 tkfile = krb5_cc_default_name(*context);
417 if (strncmp(tkfile, "FILE:", 5) == 0)
420 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
421 debug("Kerberos v5: could not get default ccache (permission denied).");
426 problem = krb5_cc_default(*context, &ccache);
428 debug("Kerberos v5: krb5_cc_default failed: %s",
429 krb5_get_err_text(*context, problem));
434 remotehost = get_canonical_hostname(1);
436 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
437 "host", remotehost, NULL, ccache, &ap);
439 debug("Kerberos v5: krb5_mk_req failed: %s",
440 krb5_get_err_text(*context, problem));
445 packet_start(SSH_CMSG_AUTH_KERBEROS);
446 packet_put_string((char *) ap.data, ap.length);
453 type = packet_read();
455 case SSH_SMSG_FAILURE:
456 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
457 debug("Kerberos v5 authentication failed.");
461 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
462 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
463 debug("Kerberos v5 authentication accepted.");
465 /* Get server's response. */
466 ap.data = packet_get_string((unsigned int *) &ap.length);
468 /* XXX je to dobre? */
470 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
478 packet_disconnect("Protocol error on Kerberos v5 response: %d",
487 krb5_cc_close(*context, ccache);
489 krb5_free_ap_rep_enc_part(*context, reply);
494 krb5_free_data_contents(*context, &ap);
501 send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
504 krb5_error_code problem;
506 krb5_ccache ccache = NULL;
509 krb5_kdc_flags flags;
513 const char *remotehost;
515 memset(&creds, 0, sizeof(creds));
516 memset(&outbuf, 0, sizeof(outbuf));
518 fd = packet_get_connection_in();
521 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
523 problem = krb5_auth_con_genaddrs(context, auth_context, fd,
524 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
525 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
530 problem = krb5_cc_default(context, &ccache);
534 problem = krb5_cc_get_principal(context, ccache, &creds.client);
538 remotehost = get_canonical_hostname(1);
541 problem = krb5_build_principal(context, &creds.server,
542 strlen(creds.client->realm), creds.client->realm,
543 "krbtgt", creds.client->realm, NULL);
545 problem = krb5_build_principal(context, &creds.server,
546 creds.client->realm.length, creds.client->realm.data,
547 "host", remotehost, NULL);
552 creds.times.endtime = 0;
556 flags.b.forwarded = 1;
557 flags.b.forwardable = krb5_config_get_bool(context, NULL,
558 "libdefaults", "forwardable", NULL);
559 problem = krb5_get_forwarded_creds(context, auth_context,
560 ccache, flags.i, remotehost, &creds, &outbuf);
563 problem = krb5_fwd_tgt_creds(context, auth_context, remotehost,
564 creds.client, creds.server, ccache, forwardable, &outbuf);
570 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
571 packet_put_string((char *)outbuf.data, outbuf.length);
575 type = packet_read();
577 if (type == SSH_SMSG_SUCCESS) {
580 krb5_unparse_name(context, creds.client, &pname);
581 debug("Kerberos v5 TGT forwarded (%s).", pname);
584 debug("Kerberos v5 TGT forwarding failed.");
590 debug("Kerberos v5 TGT forwarding failed: %s",
591 krb5_get_err_text(context, problem));
593 krb5_free_principal(context, creds.client);
595 krb5_free_principal(context, creds.server);
597 krb5_cc_close(context, ccache);
604 * Tries to authenticate with any string-based challenge/response system.
605 * Note that the client code is not tied to s/key or TIS.
608 try_challenge_response_authentication(void)
613 char *challenge, *response;
615 debug("Doing challenge response authentication.");
617 for (i = 0; i < options.number_of_password_prompts; i++) {
618 /* request a challenge */
619 packet_start(SSH_CMSG_AUTH_TIS);
623 type = packet_read();
624 if (type != SSH_SMSG_FAILURE &&
625 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
626 packet_disconnect("Protocol error: got %d in response "
627 "to SSH_CMSG_AUTH_TIS", type);
629 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
630 debug("No challenge.");
633 challenge = packet_get_string(&clen);
635 snprintf(prompt, sizeof prompt, "%s%s", challenge,
636 strchr(challenge, '\n') ? "" : "\nResponse: ");
639 error("Permission denied, please try again.");
640 if (options.cipher == SSH_CIPHER_NONE)
641 logit("WARNING: Encryption is disabled! "
642 "Response will be transmitted in clear text.");
643 response = read_passphrase(prompt, 0);
644 if (strcmp(response, "") == 0) {
648 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
649 ssh_put_password(response);
650 memset(response, 0, strlen(response));
654 type = packet_read();
655 if (type == SSH_SMSG_SUCCESS)
657 if (type != SSH_SMSG_FAILURE)
658 packet_disconnect("Protocol error: got %d in response "
659 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
666 * Tries to authenticate with plain passwd authentication.
669 try_password_authentication(char *prompt)
674 debug("Doing password authentication.");
675 if (options.cipher == SSH_CIPHER_NONE)
676 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
677 for (i = 0; i < options.number_of_password_prompts; i++) {
679 error("Permission denied, please try again.");
680 password = read_passphrase(prompt, 0);
681 packet_start(SSH_CMSG_AUTH_PASSWORD);
682 ssh_put_password(password);
683 memset(password, 0, strlen(password));
688 type = packet_read();
689 if (type == SSH_SMSG_SUCCESS)
691 if (type != SSH_SMSG_FAILURE)
692 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
702 ssh_kex(char *host, struct sockaddr *hostaddr)
706 Key *host_key, *server_key;
708 int ssh_cipher_default = SSH_CIPHER_3DES;
709 u_char session_key[SSH_SESSION_KEY_LENGTH];
711 u_int supported_ciphers;
712 u_int server_flags, client_flags;
715 debug("Waiting for server public key.");
717 /* Wait for a public key packet from the server. */
718 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
720 /* Get cookie from the packet. */
721 for (i = 0; i < 8; i++)
722 cookie[i] = packet_get_char();
724 /* Get the public key. */
725 server_key = key_new(KEY_RSA1);
726 bits = packet_get_int();
727 packet_get_bignum(server_key->rsa->e);
728 packet_get_bignum(server_key->rsa->n);
730 rbits = BN_num_bits(server_key->rsa->n);
732 logit("Warning: Server lies about size of server public key: "
733 "actual size is %d bits vs. announced %d.", rbits, bits);
734 logit("Warning: This may be due to an old implementation of ssh.");
736 /* Get the host key. */
737 host_key = key_new(KEY_RSA1);
738 bits = packet_get_int();
739 packet_get_bignum(host_key->rsa->e);
740 packet_get_bignum(host_key->rsa->n);
742 rbits = BN_num_bits(host_key->rsa->n);
744 logit("Warning: Server lies about size of server host key: "
745 "actual size is %d bits vs. announced %d.", rbits, bits);
746 logit("Warning: This may be due to an old implementation of ssh.");
749 /* Get protocol flags. */
750 server_flags = packet_get_int();
751 packet_set_protocol_flags(server_flags);
753 supported_ciphers = packet_get_int();
754 supported_authentications = packet_get_int();
757 debug("Received server public key (%d bits) and host key (%d bits).",
758 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
760 if (verify_host_key(host, hostaddr, host_key) == -1)
761 fatal("Host key verification failed.");
763 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
765 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
767 /* Generate a session key. */
771 * Generate an encryption key for the session. The key is a 256 bit
772 * random number, interpreted as a 32-byte key, with the least
773 * significant 8 bits being the first byte of the key.
775 for (i = 0; i < 32; i++) {
778 session_key[i] = rand & 0xff;
783 * According to the protocol spec, the first byte of the session key
784 * is the highest byte of the integer. The session key is xored with
785 * the first 16 bytes of the session id.
787 if ((key = BN_new()) == NULL)
788 fatal("respond_to_rsa_challenge: BN_new failed");
790 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
791 BN_lshift(key, key, 8);
793 BN_add_word(key, session_key[i] ^ session_id[i]);
795 BN_add_word(key, session_key[i]);
799 * Encrypt the integer using the public key and host key of the
800 * server (key with smaller modulus first).
802 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
803 /* Public key has smaller modulus. */
804 if (BN_num_bits(host_key->rsa->n) <
805 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
806 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
807 "SSH_KEY_BITS_RESERVED %d",
808 BN_num_bits(host_key->rsa->n),
809 BN_num_bits(server_key->rsa->n),
810 SSH_KEY_BITS_RESERVED);
812 rsa_public_encrypt(key, key, server_key->rsa);
813 rsa_public_encrypt(key, key, host_key->rsa);
815 /* Host key has smaller modulus (or they are equal). */
816 if (BN_num_bits(server_key->rsa->n) <
817 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
818 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
819 "SSH_KEY_BITS_RESERVED %d",
820 BN_num_bits(server_key->rsa->n),
821 BN_num_bits(host_key->rsa->n),
822 SSH_KEY_BITS_RESERVED);
824 rsa_public_encrypt(key, key, host_key->rsa);
825 rsa_public_encrypt(key, key, server_key->rsa);
828 /* Destroy the public keys since we no longer need them. */
829 key_free(server_key);
832 if (options.cipher == SSH_CIPHER_NOT_SET) {
833 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
834 options.cipher = ssh_cipher_default;
835 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
836 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
837 logit("No valid SSH1 cipher, using %.100s instead.",
838 cipher_name(ssh_cipher_default));
839 options.cipher = ssh_cipher_default;
841 /* Check that the selected cipher is supported. */
842 if (!(supported_ciphers & (1 << options.cipher)))
843 fatal("Selected cipher type %.100s not supported by server.",
844 cipher_name(options.cipher));
846 debug("Encryption type: %.100s", cipher_name(options.cipher));
848 /* Send the encrypted session key to the server. */
849 packet_start(SSH_CMSG_SESSION_KEY);
850 packet_put_char(options.cipher);
852 /* Send the cookie back to the server. */
853 for (i = 0; i < 8; i++)
854 packet_put_char(cookie[i]);
856 /* Send and destroy the encrypted encryption key integer. */
857 packet_put_bignum(key);
860 /* Send protocol flags. */
861 packet_put_int(client_flags);
863 /* Send the packet now. */
867 debug("Sent encrypted session key.");
869 /* Set the encryption key. */
870 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
872 /* We will no longer need the session key here. Destroy any extra copies. */
873 memset(session_key, 0, sizeof(session_key));
876 * Expect a success message from the server. Note that this message
877 * will be received in encrypted form.
879 packet_read_expect(SSH_SMSG_SUCCESS);
881 debug("Received encrypted confirmation.");
888 ssh_userauth1(const char *local_user, const char *server_user, char *host,
889 Sensitive *sensitive)
892 krb5_context context = NULL;
893 krb5_auth_context auth_context = NULL;
897 if (supported_authentications == 0)
898 fatal("ssh_userauth1: server supports no auth methods");
900 /* Send the name of the user to log in as on the server. */
901 packet_start(SSH_CMSG_USER);
902 packet_put_cstring(server_user);
907 * The server should respond with success if no authentication is
908 * needed (the user has no password). Otherwise the server responds
911 type = packet_read();
913 /* check whether the connection was accepted without authentication. */
914 if (type == SSH_SMSG_SUCCESS)
916 if (type != SSH_SMSG_FAILURE)
917 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
920 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
921 options.kerberos_authentication) {
922 debug("Trying Kerberos v5 authentication.");
924 if (try_krb5_authentication(&context, &auth_context)) {
925 type = packet_read();
926 if (type == SSH_SMSG_SUCCESS)
928 if (type != SSH_SMSG_FAILURE)
929 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
935 * Use rhosts authentication if running in privileged socket and we
936 * do not wish to remain anonymous.
938 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
939 options.rhosts_authentication) {
940 debug("Trying rhosts authentication.");
941 packet_start(SSH_CMSG_AUTH_RHOSTS);
942 packet_put_cstring(local_user);
946 /* The server should respond with success or failure. */
947 type = packet_read();
948 if (type == SSH_SMSG_SUCCESS)
950 if (type != SSH_SMSG_FAILURE)
951 packet_disconnect("Protocol error: got %d in response to rhosts auth",
955 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
958 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
959 options.rhosts_rsa_authentication) {
960 for (i = 0; i < sensitive->nkeys; i++) {
961 if (sensitive->keys[i] != NULL &&
962 sensitive->keys[i]->type == KEY_RSA1 &&
963 try_rhosts_rsa_authentication(local_user,
968 /* Try RSA authentication if the server supports it. */
969 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
970 options.rsa_authentication) {
972 * Try RSA authentication using the authentication agent. The
973 * agent is tried first because no passphrase is needed for
974 * it, whereas identity files may require passphrases.
976 if (try_agent_authentication())
979 /* Try RSA authentication for each identity. */
980 for (i = 0; i < options.num_identity_files; i++)
981 if (options.identity_keys[i] != NULL &&
982 options.identity_keys[i]->type == KEY_RSA1 &&
983 try_rsa_authentication(i))
986 /* Try challenge response authentication if the server supports it. */
987 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
988 options.challenge_response_authentication && !options.batch_mode) {
989 if (try_challenge_response_authentication())
992 /* Try password authentication if the server supports it. */
993 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
994 options.password_authentication && !options.batch_mode) {
997 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
999 if (try_password_authentication(prompt))
1002 /* All authentication methods have failed. Exit with an error message. */
1003 fatal("Permission denied.");
1008 /* Try Kerberos v5 TGT passing. */
1009 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1010 options.kerberos_tgt_passing && context && auth_context) {
1011 if (options.cipher == SSH_CIPHER_NONE)
1012 logit("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1013 send_krb5_tgt(context, auth_context);
1016 krb5_auth_con_free(context, auth_context);
1018 krb5_free_context(context);
1020 return; /* need statement after label */