2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: sshconnect2.c,v 1.94 2002/01/25 21:00:24 markus Exp $");
37 #include "myproposal.h"
38 #include "sshconnect.h"
50 extern char *client_version_string;
51 extern char *server_version_string;
52 extern Options options;
58 u_char *session_id2 = NULL;
59 int session_id2_len = 0;
62 struct sockaddr *xxx_hostaddr;
67 verify_host_key_callback(Key *hostkey)
69 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
70 fatal("Host key verification failed.");
75 ssh_kex2(char *host, struct sockaddr *hostaddr)
80 xxx_hostaddr = hostaddr;
82 if (options.ciphers == (char *)-1) {
83 log("No valid ciphers for protocol version 2 given, using defaults.");
84 options.ciphers = NULL;
86 if (options.ciphers != NULL) {
87 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
88 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
90 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
91 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
92 myproposal[PROPOSAL_ENC_ALGS_STOC] =
93 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
94 if (options.compression) {
95 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
96 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
98 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
99 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
101 if (options.macs != NULL) {
102 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
103 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
105 if (options.hostkeyalgorithms != NULL)
106 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
107 options.hostkeyalgorithms;
109 /* start key exchange */
110 kex = kex_setup(myproposal);
111 kex->client_version_string=client_version_string;
112 kex->server_version_string=server_version_string;
113 kex->verify_host_key=&verify_host_key_callback;
117 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
119 session_id2 = kex->session_id;
120 session_id2_len = kex->session_id_len;
123 /* send 1st encrypted/maced/compressed message */
124 packet_start(SSH2_MSG_IGNORE);
125 packet_put_cstring("markus");
129 debug("done: ssh_kex2.");
136 typedef struct Authctxt Authctxt;
137 typedef struct Authmethod Authmethod;
139 typedef int sign_cb_fn(
140 Authctxt *authctxt, Key *key,
141 u_char **sigp, int *lenp, u_char *data, int datalen);
144 const char *server_user;
145 const char *local_user;
153 sign_cb_fn *last_key_sign;
155 AuthenticationConnection *agent;
159 /* kbd-interactive */
163 char *name; /* string to compare against server's list */
164 int (*userauth)(Authctxt *authctxt);
165 int *enabled; /* flag in option struct that enables method */
166 int *batch_flag; /* flag in option struct that disables method */
169 void input_userauth_success(int, u_int32_t, void *);
170 void input_userauth_failure(int, u_int32_t, void *);
171 void input_userauth_banner(int, u_int32_t, void *);
172 void input_userauth_error(int, u_int32_t, void *);
173 void input_userauth_info_req(int, u_int32_t, void *);
174 void input_userauth_pk_ok(int, u_int32_t, void *);
176 int userauth_none(Authctxt *);
177 int userauth_pubkey(Authctxt *);
178 int userauth_passwd(Authctxt *);
179 int userauth_kbdint(Authctxt *);
180 int userauth_hostbased(Authctxt *);
182 void userauth(Authctxt *, char *);
184 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
185 static void clear_auth_state(Authctxt *);
187 static Authmethod *authmethod_get(char *authlist);
188 static Authmethod *authmethod_lookup(const char *name);
189 static char *authmethods_get(void);
191 Authmethod authmethods[] = {
194 &options.hostbased_authentication,
198 &options.pubkey_authentication,
200 {"keyboard-interactive",
202 &options.kbd_interactive_authentication,
203 &options.batch_mode},
206 &options.password_authentication,
207 &options.batch_mode},
212 {NULL, NULL, NULL, NULL}
216 ssh_userauth2(const char *local_user, const char *server_user, char *host,
217 Key **keys, int nkeys)
222 if (options.challenge_response_authentication)
223 options.kbd_interactive_authentication = 1;
225 debug("send SSH2_MSG_SERVICE_REQUEST");
226 packet_start(SSH2_MSG_SERVICE_REQUEST);
227 packet_put_cstring("ssh-userauth");
230 type = packet_read();
231 if (type != SSH2_MSG_SERVICE_ACCEPT) {
232 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
234 if (packet_remaining() > 0) {
235 char *reply = packet_get_string(NULL);
236 debug("service_accept: %s", reply);
239 debug("buggy server: service_accept w/o service");
242 debug("got SSH2_MSG_SERVICE_ACCEPT");
244 if (options.preferred_authentications == NULL)
245 options.preferred_authentications = authmethods_get();
247 /* setup authentication context */
248 memset(&authctxt, 0, sizeof(authctxt));
249 authctxt.agent = ssh_get_authentication_connection();
250 authctxt.server_user = server_user;
251 authctxt.local_user = local_user;
252 authctxt.host = host;
253 authctxt.service = "ssh-connection"; /* service name */
254 authctxt.success = 0;
255 authctxt.method = authmethod_lookup("none");
256 authctxt.authlist = NULL;
257 authctxt.keys = keys;
258 authctxt.nkeys = nkeys;
259 authctxt.info_req_seen = 0;
260 if (authctxt.method == NULL)
261 fatal("ssh_userauth2: internal error: cannot send userauth none request");
263 /* initial userauth request */
264 userauth_none(&authctxt);
266 dispatch_init(&input_userauth_error);
267 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
268 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
269 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
270 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
272 if (authctxt.agent != NULL)
273 ssh_close_authentication_connection(authctxt.agent);
275 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
278 userauth(Authctxt *authctxt, char *authlist)
280 if (authlist == NULL) {
281 authlist = authctxt->authlist;
283 if (authctxt->authlist)
284 xfree(authctxt->authlist);
285 authctxt->authlist = authlist;
288 Authmethod *method = authmethod_get(authlist);
290 fatal("Permission denied (%s).", authlist);
291 authctxt->method = method;
292 if (method->userauth(authctxt) != 0) {
293 debug2("we sent a %s packet, wait for reply", method->name);
296 debug2("we did not send a packet, disable method");
297 method->enabled = NULL;
302 input_userauth_error(int type, u_int32_t seq, void *ctxt)
304 fatal("input_userauth_error: bad message during authentication: "
308 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
311 debug3("input_userauth_banner");
312 msg = packet_get_string(NULL);
313 lang = packet_get_string(NULL);
314 fprintf(stderr, "%s", msg);
319 input_userauth_success(int type, u_int32_t seq, void *ctxt)
321 Authctxt *authctxt = ctxt;
322 if (authctxt == NULL)
323 fatal("input_userauth_success: no authentication context");
324 if (authctxt->authlist)
325 xfree(authctxt->authlist);
326 clear_auth_state(authctxt);
327 authctxt->success = 1; /* break out */
330 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
332 Authctxt *authctxt = ctxt;
333 char *authlist = NULL;
336 if (authctxt == NULL)
337 fatal("input_userauth_failure: no authentication context");
339 authlist = packet_get_string(NULL);
340 partial = packet_get_char();
344 log("Authenticated with partial success.");
345 debug("authentications that can continue: %s", authlist);
347 clear_auth_state(authctxt);
348 userauth(authctxt, authlist);
351 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
353 Authctxt *authctxt = ctxt;
356 int alen, blen, sent = 0;
357 char *pkalg, *pkblob, *fp;
359 if (authctxt == NULL)
360 fatal("input_userauth_pk_ok: no authentication context");
361 if (datafellows & SSH_BUG_PKOK) {
362 /* this is similar to SSH_BUG_PKAUTH */
363 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
364 pkblob = packet_get_string(&blen);
366 buffer_append(&b, pkblob, blen);
367 pkalg = buffer_get_string(&b, &alen);
370 pkalg = packet_get_string(&alen);
371 pkblob = packet_get_string(&blen);
375 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
376 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
379 if (authctxt->last_key == NULL ||
380 authctxt->last_key_sign == NULL) {
381 debug("no last key or no sign cb");
384 if (key_type_from_name(pkalg) == KEY_UNSPEC) {
385 debug("unknown pkalg %s", pkalg);
388 if ((key = key_from_blob(pkblob, blen)) == NULL) {
389 debug("no key from blob. pkalg %s", pkalg);
392 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
393 debug2("input_userauth_pk_ok: fp %s", fp);
395 if (!key_equal(key, authctxt->last_key)) {
396 debug("key != last_key");
399 sent = sign_and_send_pubkey(authctxt, key,
400 authctxt->last_key_sign);
409 clear_auth_state(authctxt);
410 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
412 /* try another method if we did not send a packet*/
414 userauth(authctxt, NULL);
419 userauth_none(Authctxt *authctxt)
421 /* initial userauth request */
422 packet_start(SSH2_MSG_USERAUTH_REQUEST);
423 packet_put_cstring(authctxt->server_user);
424 packet_put_cstring(authctxt->service);
425 packet_put_cstring(authctxt->method->name);
431 userauth_passwd(Authctxt *authctxt)
433 static int attempt = 0;
437 if (attempt++ >= options.number_of_password_prompts)
441 error("Permission denied, please try again.");
443 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
444 authctxt->server_user, authctxt->host);
445 password = read_passphrase(prompt, 0);
446 packet_start(SSH2_MSG_USERAUTH_REQUEST);
447 packet_put_cstring(authctxt->server_user);
448 packet_put_cstring(authctxt->service);
449 packet_put_cstring(authctxt->method->name);
451 packet_put_cstring(password);
452 memset(password, 0, strlen(password));
454 packet_add_padding(64);
460 clear_auth_state(Authctxt *authctxt)
462 /* XXX clear authentication state */
463 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
464 debug3("clear_auth_state: key_free %p", authctxt->last_key);
465 key_free(authctxt->last_key);
467 authctxt->last_key = NULL;
468 authctxt->last_key_hint = -2;
469 authctxt->last_key_sign = NULL;
473 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
476 u_char *blob, *signature;
482 debug3("sign_and_send_pubkey");
484 if (key_to_blob(k, &blob, &bloblen) == 0) {
485 /* we cannot handle this key */
486 debug3("sign_and_send_pubkey: cannot handle key");
489 /* data to be signed */
491 if (datafellows & SSH_OLD_SESSIONID) {
492 buffer_append(&b, session_id2, session_id2_len);
493 skip = session_id2_len;
495 buffer_put_string(&b, session_id2, session_id2_len);
496 skip = buffer_len(&b);
498 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
499 buffer_put_cstring(&b, authctxt->server_user);
500 buffer_put_cstring(&b,
501 datafellows & SSH_BUG_PKSERVICE ?
504 if (datafellows & SSH_BUG_PKAUTH) {
505 buffer_put_char(&b, have_sig);
507 buffer_put_cstring(&b, authctxt->method->name);
508 buffer_put_char(&b, have_sig);
509 buffer_put_cstring(&b, key_ssh_name(k));
511 buffer_put_string(&b, blob, bloblen);
513 /* generate signature */
514 ret = (*sign_callback)(authctxt, k, &signature, &slen,
515 buffer_ptr(&b), buffer_len(&b));
524 if (datafellows & SSH_BUG_PKSERVICE) {
526 buffer_append(&b, session_id2, session_id2_len);
527 skip = session_id2_len;
528 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
529 buffer_put_cstring(&b, authctxt->server_user);
530 buffer_put_cstring(&b, authctxt->service);
531 buffer_put_cstring(&b, authctxt->method->name);
532 buffer_put_char(&b, have_sig);
533 if (!(datafellows & SSH_BUG_PKAUTH))
534 buffer_put_cstring(&b, key_ssh_name(k));
535 buffer_put_string(&b, blob, bloblen);
539 /* append signature */
540 buffer_put_string(&b, signature, slen);
543 /* skip session id and packet type */
544 if (buffer_len(&b) < skip + 1)
545 fatal("userauth_pubkey: internal error");
546 buffer_consume(&b, skip + 1);
548 /* put remaining data from buffer into packet */
549 packet_start(SSH2_MSG_USERAUTH_REQUEST);
550 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
558 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
562 int bloblen, have_sig = 0;
564 debug3("send_pubkey_test");
566 if (key_to_blob(k, &blob, &bloblen) == 0) {
567 /* we cannot handle this key */
568 debug3("send_pubkey_test: cannot handle key");
571 /* register callback for USERAUTH_PK_OK message */
572 authctxt->last_key_sign = sign_callback;
573 authctxt->last_key_hint = hint;
574 authctxt->last_key = k;
575 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
577 packet_start(SSH2_MSG_USERAUTH_REQUEST);
578 packet_put_cstring(authctxt->server_user);
579 packet_put_cstring(authctxt->service);
580 packet_put_cstring(authctxt->method->name);
581 packet_put_char(have_sig);
582 if (!(datafellows & SSH_BUG_PKAUTH))
583 packet_put_cstring(key_ssh_name(k));
584 packet_put_string(blob, bloblen);
591 load_identity_file(char *filename)
594 char prompt[300], *passphrase;
598 if (stat(filename, &st) < 0) {
599 debug3("no such identity: %s", filename);
602 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
603 if (private == NULL) {
604 if (options.batch_mode)
606 snprintf(prompt, sizeof prompt,
607 "Enter passphrase for key '%.100s': ", filename);
608 for (i = 0; i < options.number_of_password_prompts; i++) {
609 passphrase = read_passphrase(prompt, 0);
610 if (strcmp(passphrase, "") != 0) {
611 private = key_load_private_type(KEY_UNSPEC, filename,
615 debug2("no passphrase given, try next key");
618 memset(passphrase, 0, strlen(passphrase));
620 if (private != NULL || quit)
622 debug2("bad passphrase given, try again...");
629 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
630 u_char *data, int datalen)
635 idx = authctxt->last_key_hint;
639 /* private key is stored in external hardware */
640 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
641 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
643 private = load_identity_file(options.identity_files[idx]);
646 ret = key_sign(private, sigp, lenp, data, datalen);
652 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
653 u_char *data, int datalen)
655 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
659 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
660 u_char *data, int datalen)
662 return key_sign(key, sigp, lenp, data, datalen);
666 userauth_pubkey_agent(Authctxt *authctxt)
668 static int called = 0;
674 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
675 debug2("userauth_pubkey_agent: no keys at all");
678 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
680 debug2("userauth_pubkey_agent: no more keys");
682 debug("userauth_pubkey_agent: testing agent key %s", comment);
684 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
689 debug2("userauth_pubkey_agent: no message sent");
694 userauth_pubkey(Authctxt *authctxt)
701 if (authctxt->agent != NULL) {
703 sent = userauth_pubkey_agent(authctxt);
704 } while (!sent && authctxt->agent->howmany > 0);
706 while (!sent && idx < options.num_identity_files) {
707 key = options.identity_keys[idx];
708 filename = options.identity_files[idx];
710 debug("try privkey: %s", filename);
711 key = load_identity_file(filename);
713 sent = sign_and_send_pubkey(authctxt, key,
717 } else if (key->type != KEY_RSA1) {
718 debug("try pubkey: %s", filename);
719 sent = send_pubkey_test(authctxt, key,
720 identity_sign_cb, idx);
728 * Send userauth request message specifying keyboard-interactive method.
731 userauth_kbdint(Authctxt *authctxt)
733 static int attempt = 0;
735 if (attempt++ >= options.number_of_password_prompts)
737 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
738 if (attempt > 1 && !authctxt->info_req_seen) {
739 debug3("userauth_kbdint: disable: no info_req_seen");
740 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
744 debug2("userauth_kbdint");
745 packet_start(SSH2_MSG_USERAUTH_REQUEST);
746 packet_put_cstring(authctxt->server_user);
747 packet_put_cstring(authctxt->service);
748 packet_put_cstring(authctxt->method->name);
749 packet_put_cstring(""); /* lang */
750 packet_put_cstring(options.kbd_interactive_devices ?
751 options.kbd_interactive_devices : "");
754 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
759 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
762 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
764 Authctxt *authctxt = ctxt;
765 char *name, *inst, *lang, *prompt, *response;
766 u_int num_prompts, i;
769 debug2("input_userauth_info_req");
771 if (authctxt == NULL)
772 fatal("input_userauth_info_req: no authentication context");
774 authctxt->info_req_seen = 1;
776 name = packet_get_string(NULL);
777 inst = packet_get_string(NULL);
778 lang = packet_get_string(NULL);
779 if (strlen(name) > 0)
781 if (strlen(inst) > 0)
787 num_prompts = packet_get_int();
789 * Begin to build info response packet based on prompts requested.
790 * We commit to providing the correct number of responses, so if
791 * further on we run into a problem that prevents this, we have to
792 * be sure and clean this up and send a correct error response.
794 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
795 packet_put_int(num_prompts);
797 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
798 for (i = 0; i < num_prompts; i++) {
799 prompt = packet_get_string(NULL);
800 echo = packet_get_char();
802 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
804 packet_put_cstring(response);
805 memset(response, 0, strlen(response));
809 packet_check_eom(); /* done with parsing incoming message. */
811 packet_add_padding(64);
816 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
817 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
820 userauth_hostbased(Authctxt *authctxt)
824 u_char *signature, *blob;
825 char *chost, *pkalg, *p;
828 int ok, i, len, found = 0;
830 /* check for a useful key */
831 for (i = 0; i < authctxt->nkeys; i++) {
832 private = authctxt->keys[i];
833 if (private && private->type != KEY_RSA1) {
835 /* we take and free the key */
836 authctxt->keys[i] = NULL;
841 debug("userauth_hostbased: no more client hostkeys");
844 if (key_to_blob(private, &blob, &blen) == 0) {
848 /* figure out a name for the client host */
849 p = get_local_name(packet_get_connection_in());
851 error("userauth_hostbased: cannot get local ipaddr/name");
856 chost = xmalloc(len);
857 strlcpy(chost, p, len);
858 strlcat(chost, ".", len);
859 debug2("userauth_hostbased: chost %s", chost);
861 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
863 pkalg = xstrdup(key_ssh_name(private));
866 buffer_put_string(&b, session_id2, session_id2_len);
867 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
868 buffer_put_cstring(&b, authctxt->server_user);
869 buffer_put_cstring(&b, service);
870 buffer_put_cstring(&b, authctxt->method->name);
871 buffer_put_cstring(&b, pkalg);
872 buffer_put_string(&b, blob, blen);
873 buffer_put_cstring(&b, chost);
874 buffer_put_cstring(&b, authctxt->local_user);
878 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
882 error("key_sign failed");
887 packet_start(SSH2_MSG_USERAUTH_REQUEST);
888 packet_put_cstring(authctxt->server_user);
889 packet_put_cstring(authctxt->service);
890 packet_put_cstring(authctxt->method->name);
891 packet_put_cstring(pkalg);
892 packet_put_string(blob, blen);
893 packet_put_cstring(chost);
894 packet_put_cstring(authctxt->local_user);
895 packet_put_string(signature, slen);
896 memset(signature, 's', slen);
905 /* find auth method */
908 * given auth method name, if configurable options permit this method fill
909 * in auth_ident field and return true, otherwise return false.
912 authmethod_is_enabled(Authmethod *method)
916 /* return false if options indicate this method is disabled */
917 if (method->enabled == NULL || *method->enabled == 0)
919 /* return false if batch mode is enabled but method needs interactive mode */
920 if (method->batch_flag != NULL && *method->batch_flag != 0)
926 authmethod_lookup(const char *name)
928 Authmethod *method = NULL;
930 for (method = authmethods; method->name != NULL; method++)
931 if (strcmp(name, method->name) == 0)
933 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
937 /* XXX internal state */
938 static Authmethod *current = NULL;
939 static char *supported = NULL;
940 static char *preferred = NULL;
942 * Given the authentication method list sent by the server, return the
943 * next method we should try. If the server initially sends a nil list,
944 * use a built-in default list.
947 authmethod_get(char *authlist)
953 /* Use a suitable default if we're passed a nil list. */
954 if (authlist == NULL || strlen(authlist) == 0)
955 authlist = options.preferred_authentications;
957 if (supported == NULL || strcmp(authlist, supported) != 0) {
958 debug3("start over, passed a different list %s", authlist);
959 if (supported != NULL)
961 supported = xstrdup(authlist);
962 preferred = options.preferred_authentications;
963 debug3("preferred %s", preferred);
965 } else if (current != NULL && authmethod_is_enabled(current))
969 if ((name = match_list(preferred, supported, &next)) == NULL) {
970 debug("no more auth methods to try");
975 debug3("authmethod_lookup %s", name);
976 debug3("remaining preferred: %s", preferred);
977 if ((current = authmethod_lookup(name)) != NULL &&
978 authmethod_is_enabled(current)) {
979 debug3("authmethod_is_enabled %s", name);
980 debug("next auth method to try is %s", name);
987 authmethods_get(void)
989 Authmethod *method = NULL;
994 for (method = authmethods; method->name != NULL; method++) {
995 if (authmethod_is_enabled(method)) {
996 if (buffer_len(&b) > 0)
997 buffer_append(&b, ",", 1);
998 buffer_append(&b, method->name, strlen(method->name));
1001 buffer_append(&b, "\0", 1);
1002 list = xstrdup(buffer_ptr(&b));