]> andersk Git - gssapi-openssh.git/blob - openssh/sshconnect2.c
merged OPENSSH_3_5P1_GSI_20030317 from trunk to GPT branch:
[gssapi-openssh.git] / openssh / sshconnect2.c
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 #include "includes.h"
26 RCSID("$OpenBSD: sshconnect2.c,v 1.107 2002/07/01 19:48:46 markus Exp $");
27
28 #include "ssh.h"
29 #include "ssh2.h"
30 #include "xmalloc.h"
31 #include "buffer.h"
32 #include "packet.h"
33 #include "compat.h"
34 #include "bufaux.h"
35 #include "cipher.h"
36 #include "kex.h"
37 #include "myproposal.h"
38 #include "sshconnect.h"
39 #include "authfile.h"
40 #include "dh.h"
41 #include "authfd.h"
42 #include "log.h"
43 #include "readconf.h"
44 #include "readpass.h"
45 #include "match.h"
46 #include "dispatch.h"
47 #include "canohost.h"
48 #include "msg.h"
49 #include "pathnames.h"
50
51 #ifdef GSSAPI
52 #include "ssh-gss.h"
53 #endif
54
55 /* import */
56 extern char *client_version_string;
57 extern char *server_version_string;
58 extern Options options;
59
60 /*
61  * SSH2 key exchange
62  */
63
64 u_char *session_id2 = NULL;
65 int session_id2_len = 0;
66
67 char *xxx_host;
68 struct sockaddr *xxx_hostaddr;
69
70 Kex *xxx_kex = NULL;
71
72 static int
73 verify_host_key_callback(Key *hostkey)
74 {
75         if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
76                 fatal("Host key verification failed.");
77         return 0;
78 }
79
80 void
81 ssh_kex2(char *host, struct sockaddr *hostaddr)
82 {
83         Kex *kex;
84 #ifdef GSSAPI
85         char *orig, *gss;
86         int len;
87 #endif
88
89         xxx_host = host;
90         xxx_hostaddr = hostaddr;
91
92 #ifdef GSSAPI
93         if (options.gss_keyex) {
94         char *canonhost;
95         /* Add the GSSAPI mechanisms currently supported on this client to
96          * the key exchange algorithm proposal */
97         orig = myproposal[PROPOSAL_KEX_ALGS];
98         canonhost = xstrdup(get_canonical_hostname(1));
99         resolve_localhost(&canonhost);
100         gss = ssh_gssapi_mechanisms(0,canonhost);
101         xfree(canonhost);
102         canonhost=NULL;
103         if (gss) {
104            len = strlen(orig)+strlen(gss)+2;
105            myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
106            snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
107         }
108         }
109 #endif
110
111         if (options.ciphers == (char *)-1) {
112                 log("No valid ciphers for protocol version 2 given, using defaults.");
113                 options.ciphers = NULL;
114         }
115         if (options.ciphers != NULL) {
116                 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
117                 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
118         }
119         myproposal[PROPOSAL_ENC_ALGS_CTOS] =
120             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
121         myproposal[PROPOSAL_ENC_ALGS_STOC] =
122             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
123         if (options.compression) {
124                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
125                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
126         } else {
127                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
128                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
129         }
130         if (options.macs != NULL) {
131                 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
132                 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
133         }
134         if (options.hostkeyalgorithms != NULL)
135                 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
136                     options.hostkeyalgorithms;
137
138 #ifdef GSSAPI
139         /* If we've got GSSAPI algorithms, then we also support the
140          * 'null' hostkey, as a last resort */
141         if (options.gss_keyex && gss) {
142                 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
143                 len = strlen(orig)+sizeof(",null");
144                 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
145                 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);  
146         }
147 #endif
148
149         /* start key exchange */
150         kex = kex_setup(myproposal);
151         kex->client_version_string=client_version_string;
152         kex->server_version_string=server_version_string;
153         kex->verify_host_key=&verify_host_key_callback;
154         kex->host=host;
155
156 #ifdef GSSAPI
157         kex->options.gss_deleg_creds=options.gss_deleg_creds;
158 #endif
159
160         xxx_kex = kex;
161
162         dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
163
164         session_id2 = kex->session_id;
165         session_id2_len = kex->session_id_len;
166
167 #ifdef DEBUG_KEXDH
168         /* send 1st encrypted/maced/compressed message */
169         packet_start(SSH2_MSG_IGNORE);
170         packet_put_cstring("markus");
171         packet_send();
172         packet_write_wait();
173 #endif
174         debug("done: ssh_kex2.");
175 }
176
177 /*
178  * Authenticate user
179  */
180
181 typedef struct Authctxt Authctxt;
182 typedef struct Authmethod Authmethod;
183
184 typedef int sign_cb_fn(
185     Authctxt *authctxt, Key *key,
186     u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
187
188 struct Authctxt {
189         const char *server_user;
190         const char *local_user;
191         const char *host;
192         const char *service;
193         Authmethod *method;
194         int success;
195         char *authlist;
196         /* pubkey */
197         Key *last_key;
198         sign_cb_fn *last_key_sign;
199         int last_key_hint;
200         AuthenticationConnection *agent;
201         /* hostbased */
202         Sensitive *sensitive;
203         /* kbd-interactive */
204         int info_req_seen;
205         /* generic */
206         void *methoddata;
207 };
208 struct Authmethod {
209         char    *name;          /* string to compare against server's list */
210         int     (*userauth)(Authctxt *authctxt);
211         int     *enabled;       /* flag in option struct that enables method */
212         int     *batch_flag;    /* flag in option struct that disables method */
213 };
214
215 void    input_userauth_success(int, u_int32_t, void *);
216 void    input_userauth_failure(int, u_int32_t, void *);
217 void    input_userauth_banner(int, u_int32_t, void *);
218 void    input_userauth_error(int, u_int32_t, void *);
219 void    input_userauth_info_req(int, u_int32_t, void *);
220 void    input_userauth_pk_ok(int, u_int32_t, void *);
221 void    input_userauth_passwd_changereq(int, u_int32_t, void *);
222
223 int     userauth_none(Authctxt *);
224 int     userauth_pubkey(Authctxt *);
225 int     userauth_passwd(Authctxt *);
226 int     userauth_kbdint(Authctxt *);
227 int     userauth_hostbased(Authctxt *);
228
229 #ifdef GSSAPI
230 int     userauth_external(Authctxt *authctxt);
231 int     userauth_gssapi(Authctxt *authctxt);
232 void    input_gssapi_response(int type, u_int32_t plen, void *ctxt);
233 void    input_gssapi_token(int type, u_int32_t plen, void *ctxt);
234 void    input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
235 #endif
236
237 void    userauth(Authctxt *, char *);
238
239 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
240 static void clear_auth_state(Authctxt *);
241
242 static Authmethod *authmethod_get(char *authlist);
243 static Authmethod *authmethod_lookup(const char *name);
244 static char *authmethods_get(void);
245
246 Authmethod authmethods[] = {
247 #ifdef GSSAPI
248         {"external-keyx",
249                 userauth_external,
250                 &options.gss_authentication,
251                 NULL},
252         {"gssapi",
253                 userauth_gssapi,
254                 &options.gss_authentication,
255                 NULL},
256 #endif
257         {"hostbased",
258                 userauth_hostbased,
259                 &options.hostbased_authentication,
260                 NULL},
261         {"publickey",
262                 userauth_pubkey,
263                 &options.pubkey_authentication,
264                 NULL},
265         {"keyboard-interactive",
266                 userauth_kbdint,
267                 &options.kbd_interactive_authentication,
268                 &options.batch_mode},
269         {"password",
270                 userauth_passwd,
271                 &options.password_authentication,
272                 &options.batch_mode},
273         {"none",
274                 userauth_none,
275                 NULL,
276                 NULL},
277         {NULL, NULL, NULL, NULL}
278 };
279
280 void
281 ssh_userauth2(const char *local_user, const char *server_user, char *host,
282     Sensitive *sensitive)
283 {
284         Authctxt authctxt;
285         int type;
286
287         if (options.challenge_response_authentication)
288                 options.kbd_interactive_authentication = 1;
289
290         debug("send SSH2_MSG_SERVICE_REQUEST");
291         packet_start(SSH2_MSG_SERVICE_REQUEST);
292         packet_put_cstring("ssh-userauth");
293         packet_send();
294         packet_write_wait();
295         type = packet_read();
296         if (type != SSH2_MSG_SERVICE_ACCEPT) {
297                 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
298         }
299         if (packet_remaining() > 0) {
300                 char *reply = packet_get_string(NULL);
301                 debug("service_accept: %s", reply);
302                 xfree(reply);
303         } else {
304                 debug("buggy server: service_accept w/o service");
305         }
306         packet_check_eom();
307         debug("got SSH2_MSG_SERVICE_ACCEPT");
308
309         if (options.preferred_authentications == NULL)
310                 options.preferred_authentications = authmethods_get();
311
312         /* setup authentication context */
313         memset(&authctxt, 0, sizeof(authctxt));
314         authctxt.agent = ssh_get_authentication_connection();
315         authctxt.server_user = server_user;
316         authctxt.local_user = local_user;
317         authctxt.host = host;
318         authctxt.service = "ssh-connection";            /* service name */
319         authctxt.success = 0;
320         authctxt.method = authmethod_lookup("none");
321         authctxt.authlist = NULL;
322         authctxt.methoddata = NULL;
323         authctxt.sensitive = sensitive;
324         authctxt.info_req_seen = 0;
325         if (authctxt.method == NULL)
326                 fatal("ssh_userauth2: internal error: cannot send userauth none request");
327
328         /* initial userauth request */
329         userauth_none(&authctxt);
330
331         dispatch_init(&input_userauth_error);
332         dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
333         dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
334         dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
335         dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt);     /* loop until success */
336
337         if (authctxt.agent != NULL)
338                 ssh_close_authentication_connection(authctxt.agent);
339
340         debug("ssh-userauth2 successful: method %s", authctxt.method->name);
341 }
342 void
343 userauth(Authctxt *authctxt, char *authlist)
344 {
345         if (authctxt->methoddata!=NULL) {
346                 xfree(authctxt->methoddata);
347                 authctxt->methoddata=NULL;
348         }
349            
350         if (authlist == NULL) {
351                 authlist = authctxt->authlist;
352         } else {
353                 if (authctxt->authlist)
354                         xfree(authctxt->authlist);
355                 authctxt->authlist = authlist;
356         }
357         for (;;) {
358                 Authmethod *method = authmethod_get(authlist);
359                 if (method == NULL)
360                         fatal("Permission denied (%s).", authlist);
361                 authctxt->method = method;
362                 if (method->userauth(authctxt) != 0) {
363                         debug2("we sent a %s packet, wait for reply", method->name);
364                         break;
365                 } else {
366                         debug2("we did not send a packet, disable method");
367                         method->enabled = NULL;
368                 }
369         }
370 }
371
372 void
373 input_userauth_error(int type, u_int32_t seq, void *ctxt)
374 {
375         fatal("input_userauth_error: bad message during authentication: "
376            "type %d", type);
377 }
378
379 void
380 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
381 {
382         char *msg, *lang;
383         debug3("input_userauth_banner");
384         msg = packet_get_string(NULL);
385         lang = packet_get_string(NULL);
386         fprintf(stderr, "%s", msg);
387         xfree(msg);
388         xfree(lang);
389 }
390
391 void
392 input_userauth_success(int type, u_int32_t seq, void *ctxt)
393 {
394         Authctxt *authctxt = ctxt;
395         if (authctxt == NULL)
396                 fatal("input_userauth_success: no authentication context");
397         if (authctxt->authlist)
398                 xfree(authctxt->authlist);
399         if (authctxt->methoddata)
400                 xfree(authctxt->methoddata);
401         clear_auth_state(authctxt);
402         authctxt->success = 1;                  /* break out */
403 }
404
405 void
406 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
407 {
408         Authctxt *authctxt = ctxt;
409         char *authlist = NULL;
410         int partial;
411
412         if (authctxt == NULL)
413                 fatal("input_userauth_failure: no authentication context");
414
415         authlist = packet_get_string(NULL);
416         partial = packet_get_char();
417         packet_check_eom();
418
419         if (partial != 0)
420                 log("Authenticated with partial success.");
421         debug("authentications that can continue: %s", authlist);
422
423         clear_auth_state(authctxt);
424         userauth(authctxt, authlist);
425 }
426 void
427 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
428 {
429         Authctxt *authctxt = ctxt;
430         Key *key = NULL;
431         Buffer b;
432         int pktype, sent = 0;
433         u_int alen, blen;
434         char *pkalg, *fp;
435         u_char *pkblob;
436
437         if (authctxt == NULL)
438                 fatal("input_userauth_pk_ok: no authentication context");
439         if (datafellows & SSH_BUG_PKOK) {
440                 /* this is similar to SSH_BUG_PKAUTH */
441                 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
442                 pkblob = packet_get_string(&blen);
443                 buffer_init(&b);
444                 buffer_append(&b, pkblob, blen);
445                 pkalg = buffer_get_string(&b, &alen);
446                 buffer_free(&b);
447         } else {
448                 pkalg = packet_get_string(&alen);
449                 pkblob = packet_get_string(&blen);
450         }
451         packet_check_eom();
452
453         debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
454             pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
455
456         do {
457                 if (authctxt->last_key == NULL ||
458                     authctxt->last_key_sign == NULL) {
459                         debug("no last key or no sign cb");
460                         break;
461                 }
462                 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
463                         debug("unknown pkalg %s", pkalg);
464                         break;
465                 }
466                 if ((key = key_from_blob(pkblob, blen)) == NULL) {
467                         debug("no key from blob. pkalg %s", pkalg);
468                         break;
469                 }
470                 if (key->type != pktype) {
471                         error("input_userauth_pk_ok: type mismatch "
472                             "for decoded key (received %d, expected %d)",
473                             key->type, pktype);
474                         break;
475                 }
476                 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
477                 debug2("input_userauth_pk_ok: fp %s", fp);
478                 xfree(fp);
479                 if (!key_equal(key, authctxt->last_key)) {
480                         debug("key != last_key");
481                         break;
482                 }
483                 sent = sign_and_send_pubkey(authctxt, key,
484                    authctxt->last_key_sign);
485         } while (0);
486
487         if (key != NULL)
488                 key_free(key);
489         xfree(pkalg);
490         xfree(pkblob);
491
492         /* unregister */
493         clear_auth_state(authctxt);
494         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
495
496         /* try another method if we did not send a packet */
497         if (sent == 0)
498                 userauth(authctxt, NULL);
499
500 }
501
502 #ifdef GSSAPI
503 int 
504 userauth_gssapi(Authctxt *authctxt)
505 {
506         Gssctxt *gssctxt;
507         static int mech=0;
508
509         if (datafellows & SSH_OLD_GSSAPI) return 0;
510         
511         /* Try each mechanism in turn.  Give up if we've tried all
512            supported mechanisms.
513          */
514         if (mech==GSS_LAST_ENTRY) return 0;
515         
516         /* Initialise as much of our context as we can, so failures can be
517          * trapped before sending any packets.
518          */
519         ssh_gssapi_build_ctx(&gssctxt);
520
521         if (ssh_gssapi_import_name(gssctxt,get_canonical_hostname(1))) {
522                 return(0);
523         }
524         
525         authctxt->methoddata=(void *)gssctxt;
526                 
527         packet_start(SSH2_MSG_USERAUTH_REQUEST);
528 #ifdef GSI
529         if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
530             packet_put_cstring("");
531         } else {
532 #endif
533             packet_put_cstring(authctxt->server_user);
534 #ifdef GSI
535         }
536 #endif
537         packet_put_cstring(authctxt->service);
538         packet_put_cstring(authctxt->method->name);
539
540         /* FIXME: This assumes that our current GSSAPI implementation
541          * supports all of the mechanisms listed in supported_mechs.
542          * This may not be the case - we should use something along
543          * the lines of the code in gss_genr to remove the ones that
544          * aren't supported */
545
546         /* Try one GSSAPI mechanism at a time. */
547         packet_put_int(1);
548         packet_put_string(supported_mechs[mech].oid.elements,
549                           supported_mechs[mech].oid.length);
550         packet_send();
551         packet_write_wait();
552
553         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
554         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
555         
556         mech++;                 /* Move to next mechanism for next time. */
557
558         return 1;
559 }
560
561 void
562 input_gssapi_response(int type, u_int32_t plen, void *ctxt) 
563 {
564         Authctxt *authctxt = ctxt;
565         Gssctxt *gssctxt;
566         OM_uint32 status,ms;
567         int oidlen;
568         char *oidv;
569         gss_buffer_desc send_tok;
570         
571         if (authctxt == NULL)
572                 fatal("input_gssapi_response: no authentication context");
573         gssctxt = authctxt->methoddata;
574         
575         /* Setup our OID */
576         oidv=packet_get_string(&oidlen);
577         ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
578         
579         packet_check_eom();
580         
581         status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
582                                      GSS_C_NO_BUFFER, &send_tok, 
583                                      NULL);
584         if (GSS_ERROR(status)) {
585                 /* Start again with next method on list */
586                 debug("Trying to start again");
587                 userauth(authctxt,NULL);
588                 return;
589         }
590
591         /* We must have data to send */                                 
592         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
593         packet_put_string(send_tok.value,send_tok.length);
594         packet_send();
595         packet_write_wait();
596         gss_release_buffer(&ms, &send_tok);
597 }
598
599 void
600 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
601 {
602         Authctxt *authctxt = ctxt;
603         Gssctxt *gssctxt;
604         gss_buffer_desc send_tok,recv_tok;
605         OM_uint32 status;
606         u_int slen;
607         
608         if (authctxt == NULL)
609                 fatal("input_gssapi_token: no authentication context");
610         gssctxt = authctxt->methoddata;
611         
612         recv_tok.value=packet_get_string(&slen);
613         recv_tok.length=slen;   /* safe typecast */
614
615         status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
616                                    &recv_tok, &send_tok, NULL);
617
618         packet_check_eom();
619         
620         if (GSS_ERROR(status)) {
621                 /* Start again with the next method in the list */
622                 userauth(authctxt,NULL);
623                 return;
624         }
625         
626         if (send_tok.length>0) {
627                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
628                 packet_put_string(send_tok.value,send_tok.length);
629                 packet_send();
630                 packet_write_wait();
631         }
632         
633         if (status == GSS_S_COMPLETE) {
634                 /* If that succeeded, send a exchange complete message */
635                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
636                 packet_send();
637                 packet_write_wait();
638         }
639 }
640
641 int
642 userauth_external(Authctxt *authctxt)
643 {
644         static int attempt =0;
645         
646         if (attempt++ >= 1)
647                 return 0;
648                                 
649         debug2("userauth_external");
650         packet_start(SSH2_MSG_USERAUTH_REQUEST);
651 #ifdef GSI
652         if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
653             packet_put_cstring("");
654         } else {
655 #endif
656             packet_put_cstring(authctxt->server_user);
657 #ifdef GSI
658         }
659 #endif
660         packet_put_cstring(authctxt->service);
661         packet_put_cstring(authctxt->method->name);
662         packet_send();
663         packet_write_wait();
664         return 1;
665 }                                                                                                
666 #endif /* GSSAPI */
667
668 int
669 userauth_none(Authctxt *authctxt)
670 {
671         /* initial userauth request */
672         packet_start(SSH2_MSG_USERAUTH_REQUEST);
673         packet_put_cstring(authctxt->server_user);
674         packet_put_cstring(authctxt->service);
675         packet_put_cstring(authctxt->method->name);
676         packet_send();
677         return 1;
678
679 }
680
681 int
682 userauth_passwd(Authctxt *authctxt)
683 {
684         static int attempt = 0;
685         char prompt[150];
686         char *password;
687
688         if (attempt++ >= options.number_of_password_prompts)
689                 return 0;
690
691         if (attempt != 1)
692                 error("Permission denied, please try again.");
693
694         snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
695             authctxt->server_user, authctxt->host);
696         password = read_passphrase(prompt, 0);
697         packet_start(SSH2_MSG_USERAUTH_REQUEST);
698         packet_put_cstring(authctxt->server_user);
699         packet_put_cstring(authctxt->service);
700         packet_put_cstring(authctxt->method->name);
701         packet_put_char(0);
702         packet_put_cstring(password);
703         memset(password, 0, strlen(password));
704         xfree(password);
705         packet_add_padding(64);
706         packet_send();
707
708         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
709             &input_userauth_passwd_changereq);
710
711         return 1;
712 }
713 /*
714  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
715  */
716 void
717 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
718 {
719         Authctxt *authctxt = ctxt;
720         char *info, *lang, *password = NULL, *retype = NULL;
721         char prompt[150];
722
723         debug2("input_userauth_passwd_changereq");
724
725         if (authctxt == NULL)
726                 fatal("input_userauth_passwd_changereq: "
727                     "no authentication context");
728
729         info = packet_get_string(NULL);
730         lang = packet_get_string(NULL);
731         if (strlen(info) > 0)
732                 log("%s", info);
733         xfree(info);
734         xfree(lang);
735         packet_start(SSH2_MSG_USERAUTH_REQUEST);
736         packet_put_cstring(authctxt->server_user);
737         packet_put_cstring(authctxt->service);
738         packet_put_cstring(authctxt->method->name);
739         packet_put_char(1);                     /* additional info */
740         snprintf(prompt, sizeof(prompt),
741             "Enter %.30s@%.128s's old password: ",
742             authctxt->server_user, authctxt->host);
743         password = read_passphrase(prompt, 0);
744         packet_put_cstring(password);
745         memset(password, 0, strlen(password));
746         xfree(password);
747         password = NULL;
748         while (password == NULL) {
749                 snprintf(prompt, sizeof(prompt),
750                     "Enter %.30s@%.128s's new password: ",
751                     authctxt->server_user, authctxt->host);
752                 password = read_passphrase(prompt, RP_ALLOW_EOF);
753                 if (password == NULL) {
754                         /* bail out */
755                         return;
756                 }
757                 snprintf(prompt, sizeof(prompt),
758                     "Retype %.30s@%.128s's new password: ",
759                     authctxt->server_user, authctxt->host);
760                 retype = read_passphrase(prompt, 0);
761                 if (strcmp(password, retype) != 0) {
762                         memset(password, 0, strlen(password));
763                         xfree(password);
764                         log("Mismatch; try again, EOF to quit.");
765                         password = NULL;
766                 }
767                 memset(retype, 0, strlen(retype));
768                 xfree(retype);
769         }
770         packet_put_cstring(password);
771         memset(password, 0, strlen(password));
772         xfree(password);
773         packet_add_padding(64);
774         packet_send();
775
776         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
777             &input_userauth_passwd_changereq);
778 }
779
780 static void
781 clear_auth_state(Authctxt *authctxt)
782 {
783         /* XXX clear authentication state */
784         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
785
786         if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
787                 debug3("clear_auth_state: key_free %p", authctxt->last_key);
788                 key_free(authctxt->last_key);
789         }
790         authctxt->last_key = NULL;
791         authctxt->last_key_hint = -2;
792         authctxt->last_key_sign = NULL;
793 }
794
795 static int
796 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
797 {
798         Buffer b;
799         u_char *blob, *signature;
800         u_int bloblen, slen;
801         int skip = 0;
802         int ret = -1;
803         int have_sig = 1;
804
805         debug3("sign_and_send_pubkey");
806
807         if (key_to_blob(k, &blob, &bloblen) == 0) {
808                 /* we cannot handle this key */
809                 debug3("sign_and_send_pubkey: cannot handle key");
810                 return 0;
811         }
812         /* data to be signed */
813         buffer_init(&b);
814         if (datafellows & SSH_OLD_SESSIONID) {
815                 buffer_append(&b, session_id2, session_id2_len);
816                 skip = session_id2_len;
817         } else {
818                 buffer_put_string(&b, session_id2, session_id2_len);
819                 skip = buffer_len(&b);
820         }
821         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
822         buffer_put_cstring(&b, authctxt->server_user);
823         buffer_put_cstring(&b,
824             datafellows & SSH_BUG_PKSERVICE ?
825             "ssh-userauth" :
826             authctxt->service);
827         if (datafellows & SSH_BUG_PKAUTH) {
828                 buffer_put_char(&b, have_sig);
829         } else {
830                 buffer_put_cstring(&b, authctxt->method->name);
831                 buffer_put_char(&b, have_sig);
832                 buffer_put_cstring(&b, key_ssh_name(k));
833         }
834         buffer_put_string(&b, blob, bloblen);
835
836         /* generate signature */
837         ret = (*sign_callback)(authctxt, k, &signature, &slen,
838             buffer_ptr(&b), buffer_len(&b));
839         if (ret == -1) {
840                 xfree(blob);
841                 buffer_free(&b);
842                 return 0;
843         }
844 #ifdef DEBUG_PK
845         buffer_dump(&b);
846 #endif
847         if (datafellows & SSH_BUG_PKSERVICE) {
848                 buffer_clear(&b);
849                 buffer_append(&b, session_id2, session_id2_len);
850                 skip = session_id2_len;
851                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
852                 buffer_put_cstring(&b, authctxt->server_user);
853                 buffer_put_cstring(&b, authctxt->service);
854                 buffer_put_cstring(&b, authctxt->method->name);
855                 buffer_put_char(&b, have_sig);
856                 if (!(datafellows & SSH_BUG_PKAUTH))
857                         buffer_put_cstring(&b, key_ssh_name(k));
858                 buffer_put_string(&b, blob, bloblen);
859         }
860         xfree(blob);
861
862         /* append signature */
863         buffer_put_string(&b, signature, slen);
864         xfree(signature);
865
866         /* skip session id and packet type */
867         if (buffer_len(&b) < skip + 1)
868                 fatal("userauth_pubkey: internal error");
869         buffer_consume(&b, skip + 1);
870
871         /* put remaining data from buffer into packet */
872         packet_start(SSH2_MSG_USERAUTH_REQUEST);
873         packet_put_raw(buffer_ptr(&b), buffer_len(&b));
874         buffer_free(&b);
875         packet_send();
876
877         return 1;
878 }
879
880 static int
881 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
882     int hint)
883 {
884         u_char *blob;
885         u_int bloblen, have_sig = 0;
886
887         debug3("send_pubkey_test");
888
889         if (key_to_blob(k, &blob, &bloblen) == 0) {
890                 /* we cannot handle this key */
891                 debug3("send_pubkey_test: cannot handle key");
892                 return 0;
893         }
894         /* register callback for USERAUTH_PK_OK message */
895         authctxt->last_key_sign = sign_callback;
896         authctxt->last_key_hint = hint;
897         authctxt->last_key = k;
898         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
899
900         packet_start(SSH2_MSG_USERAUTH_REQUEST);
901         packet_put_cstring(authctxt->server_user);
902         packet_put_cstring(authctxt->service);
903         packet_put_cstring(authctxt->method->name);
904         packet_put_char(have_sig);
905         if (!(datafellows & SSH_BUG_PKAUTH))
906                 packet_put_cstring(key_ssh_name(k));
907         packet_put_string(blob, bloblen);
908         xfree(blob);
909         packet_send();
910         return 1;
911 }
912
913 static Key *
914 load_identity_file(char *filename)
915 {
916         Key *private;
917         char prompt[300], *passphrase;
918         int quit, i;
919         struct stat st;
920
921         if (stat(filename, &st) < 0) {
922                 debug3("no such identity: %s", filename);
923                 return NULL;
924         }
925         private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
926         if (private == NULL) {
927                 if (options.batch_mode)
928                         return NULL;
929                 snprintf(prompt, sizeof prompt,
930                     "Enter passphrase for key '%.100s': ", filename);
931                 for (i = 0; i < options.number_of_password_prompts; i++) {
932                         passphrase = read_passphrase(prompt, 0);
933                         if (strcmp(passphrase, "") != 0) {
934                                 private = key_load_private_type(KEY_UNSPEC, filename,
935                                     passphrase, NULL);
936                                 quit = 0;
937                         } else {
938                                 debug2("no passphrase given, try next key");
939                                 quit = 1;
940                         }
941                         memset(passphrase, 0, strlen(passphrase));
942                         xfree(passphrase);
943                         if (private != NULL || quit)
944                                 break;
945                         debug2("bad passphrase given, try again...");
946                 }
947         }
948         return private;
949 }
950
951 static int
952 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
953     u_char *data, u_int datalen)
954 {
955         Key *private;
956         int idx, ret;
957
958         idx = authctxt->last_key_hint;
959         if (idx < 0)
960                 return -1;
961
962         /* private key is stored in external hardware */
963         if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
964                 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
965
966         private = load_identity_file(options.identity_files[idx]);
967         if (private == NULL)
968                 return -1;
969         ret = key_sign(private, sigp, lenp, data, datalen);
970         key_free(private);
971         return ret;
972 }
973
974 static int
975 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
976     u_char *data, u_int datalen)
977 {
978         return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
979 }
980
981 static int
982 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
983     u_char *data, u_int datalen)
984 {
985         return key_sign(key, sigp, lenp, data, datalen);
986 }
987
988 static int
989 userauth_pubkey_agent(Authctxt *authctxt)
990 {
991         static int called = 0;
992         int ret = 0;
993         char *comment;
994         Key *k;
995
996         if (called == 0) {
997                 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
998                         debug2("userauth_pubkey_agent: no keys at all");
999                 called = 1;
1000         }
1001         k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1002         if (k == NULL) {
1003                 debug2("userauth_pubkey_agent: no more keys");
1004         } else {
1005                 debug("userauth_pubkey_agent: testing agent key %s", comment);
1006                 xfree(comment);
1007                 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1008                 if (ret == 0)
1009                         key_free(k);
1010         }
1011         if (ret == 0)
1012                 debug2("userauth_pubkey_agent: no message sent");
1013         return ret;
1014 }
1015
1016 int
1017 userauth_pubkey(Authctxt *authctxt)
1018 {
1019         static int idx = 0;
1020         int sent = 0;
1021         Key *key;
1022         char *filename;
1023
1024         if (authctxt->agent != NULL) {
1025                 do {
1026                         sent = userauth_pubkey_agent(authctxt);
1027                 } while (!sent && authctxt->agent->howmany > 0);
1028         }
1029         while (!sent && idx < options.num_identity_files) {
1030                 key = options.identity_keys[idx];
1031                 filename = options.identity_files[idx];
1032                 if (key == NULL) {
1033                         debug("try privkey: %s", filename);
1034                         key = load_identity_file(filename);
1035                         if (key != NULL) {
1036                                 sent = sign_and_send_pubkey(authctxt, key,
1037                                     key_sign_cb);
1038                                 key_free(key);
1039                         }
1040                 } else if (key->type != KEY_RSA1) {
1041                         debug("try pubkey: %s", filename);
1042                         sent = send_pubkey_test(authctxt, key,
1043                             identity_sign_cb, idx);
1044                 }
1045                 idx++;
1046         }
1047         return sent;
1048 }
1049
1050 /*
1051  * Send userauth request message specifying keyboard-interactive method.
1052  */
1053 int
1054 userauth_kbdint(Authctxt *authctxt)
1055 {
1056         static int attempt = 0;
1057
1058         if (attempt++ >= options.number_of_password_prompts)
1059                 return 0;
1060         /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1061         if (attempt > 1 && !authctxt->info_req_seen) {
1062                 debug3("userauth_kbdint: disable: no info_req_seen");
1063                 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1064                 return 0;
1065         }
1066
1067         debug2("userauth_kbdint");
1068         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1069         packet_put_cstring(authctxt->server_user);
1070         packet_put_cstring(authctxt->service);
1071         packet_put_cstring(authctxt->method->name);
1072         packet_put_cstring("");                                 /* lang */
1073         packet_put_cstring(options.kbd_interactive_devices ?
1074             options.kbd_interactive_devices : "");
1075         packet_send();
1076
1077         dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1078         return 1;
1079 }
1080
1081 /*
1082  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1083  */
1084 void
1085 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1086 {
1087         Authctxt *authctxt = ctxt;
1088         char *name, *inst, *lang, *prompt, *response;
1089         u_int num_prompts, i;
1090         int echo = 0;
1091
1092         debug2("input_userauth_info_req");
1093
1094         if (authctxt == NULL)
1095                 fatal("input_userauth_info_req: no authentication context");
1096
1097         authctxt->info_req_seen = 1;
1098
1099         name = packet_get_string(NULL);
1100         inst = packet_get_string(NULL);
1101         lang = packet_get_string(NULL);
1102         if (strlen(name) > 0)
1103                 log("%s", name);
1104         if (strlen(inst) > 0)
1105                 log("%s", inst);
1106         xfree(name);
1107         xfree(inst);
1108         xfree(lang);
1109
1110         num_prompts = packet_get_int();
1111         /*
1112          * Begin to build info response packet based on prompts requested.
1113          * We commit to providing the correct number of responses, so if
1114          * further on we run into a problem that prevents this, we have to
1115          * be sure and clean this up and send a correct error response.
1116          */
1117         packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1118         packet_put_int(num_prompts);
1119
1120         debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1121         for (i = 0; i < num_prompts; i++) {
1122                 prompt = packet_get_string(NULL);
1123                 echo = packet_get_char();
1124
1125                 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1126
1127                 packet_put_cstring(response);
1128                 memset(response, 0, strlen(response));
1129                 xfree(response);
1130                 xfree(prompt);
1131         }
1132         packet_check_eom(); /* done with parsing incoming message. */
1133
1134         packet_add_padding(64);
1135         packet_send();
1136 }
1137
1138 static int
1139 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1140     u_char *data, u_int datalen)
1141 {
1142         Buffer b;
1143         struct stat st;
1144         pid_t pid;
1145         int to[2], from[2], status, version = 2;
1146
1147         debug("ssh_keysign called");
1148
1149         if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1150                 error("ssh_keysign: no installed: %s", strerror(errno));
1151                 return -1;
1152         }
1153         if (fflush(stdout) != 0)
1154                 error("ssh_keysign: fflush: %s", strerror(errno));
1155         if (pipe(to) < 0) {
1156                 error("ssh_keysign: pipe: %s", strerror(errno));
1157                 return -1;
1158         }
1159         if (pipe(from) < 0) {
1160                 error("ssh_keysign: pipe: %s", strerror(errno));
1161                 return -1;
1162         }
1163         if ((pid = fork()) < 0) {
1164                 error("ssh_keysign: fork: %s", strerror(errno));
1165                 return -1;
1166         }
1167         if (pid == 0) {
1168                 seteuid(getuid());
1169                 setuid(getuid());
1170                 close(from[0]);
1171                 if (dup2(from[1], STDOUT_FILENO) < 0)
1172                         fatal("ssh_keysign: dup2: %s", strerror(errno));
1173                 close(to[1]);
1174                 if (dup2(to[0], STDIN_FILENO) < 0)
1175                         fatal("ssh_keysign: dup2: %s", strerror(errno));
1176                 close(from[1]);
1177                 close(to[0]);
1178                 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1179                 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1180                     strerror(errno));
1181         }
1182         close(from[1]);
1183         close(to[0]);
1184
1185         buffer_init(&b);
1186         buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1187         buffer_put_string(&b, data, datalen);
1188         ssh_msg_send(to[1], version, &b);
1189
1190         if (ssh_msg_recv(from[0], &b) < 0) {
1191                 error("ssh_keysign: no reply");
1192                 buffer_clear(&b);
1193                 return -1;
1194         }
1195         close(from[0]);
1196         close(to[1]);
1197
1198         while (waitpid(pid, &status, 0) < 0)
1199                 if (errno != EINTR)
1200                         break;
1201
1202         if (buffer_get_char(&b) != version) {
1203                 error("ssh_keysign: bad version");
1204                 buffer_clear(&b);
1205                 return -1;
1206         }
1207         *sigp = buffer_get_string(&b, lenp);
1208         buffer_clear(&b);
1209
1210         return 0;
1211 }
1212
1213 int
1214 userauth_hostbased(Authctxt *authctxt)
1215 {
1216         Key *private = NULL;
1217         Sensitive *sensitive = authctxt->sensitive;
1218         Buffer b;
1219         u_char *signature, *blob;
1220         char *chost, *pkalg, *p;
1221         const char *service;
1222         u_int blen, slen;
1223         int ok, i, len, found = 0;
1224
1225         /* check for a useful key */
1226         for (i = 0; i < sensitive->nkeys; i++) {
1227                 private = sensitive->keys[i];
1228                 if (private && private->type != KEY_RSA1) {
1229                         found = 1;
1230                         /* we take and free the key */
1231                         sensitive->keys[i] = NULL;
1232                         break;
1233                 }
1234         }
1235         if (!found) {
1236                 debug("userauth_hostbased: no more client hostkeys");
1237                 return 0;
1238         }
1239         if (key_to_blob(private, &blob, &blen) == 0) {
1240                 key_free(private);
1241                 return 0;
1242         }
1243         /* figure out a name for the client host */
1244         p = get_local_name(packet_get_connection_in());
1245         if (p == NULL) {
1246                 error("userauth_hostbased: cannot get local ipaddr/name");
1247                 key_free(private);
1248                 return 0;
1249         }
1250         len = strlen(p) + 2;
1251         chost = xmalloc(len);
1252         strlcpy(chost, p, len);
1253         strlcat(chost, ".", len);
1254         debug2("userauth_hostbased: chost %s", chost);
1255
1256         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1257             authctxt->service;
1258         pkalg = xstrdup(key_ssh_name(private));
1259         buffer_init(&b);
1260         /* construct data */
1261         buffer_put_string(&b, session_id2, session_id2_len);
1262         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1263         buffer_put_cstring(&b, authctxt->server_user);
1264         buffer_put_cstring(&b, service);
1265         buffer_put_cstring(&b, authctxt->method->name);
1266         buffer_put_cstring(&b, pkalg);
1267         buffer_put_string(&b, blob, blen);
1268         buffer_put_cstring(&b, chost);
1269         buffer_put_cstring(&b, authctxt->local_user);
1270 #ifdef DEBUG_PK
1271         buffer_dump(&b);
1272 #endif
1273         if (sensitive->external_keysign)
1274                 ok = ssh_keysign(private, &signature, &slen,
1275                     buffer_ptr(&b), buffer_len(&b));
1276         else
1277                 ok = key_sign(private, &signature, &slen,
1278                     buffer_ptr(&b), buffer_len(&b));
1279         key_free(private);
1280         buffer_free(&b);
1281         if (ok != 0) {
1282                 error("key_sign failed");
1283                 xfree(chost);
1284                 xfree(pkalg);
1285                 return 0;
1286         }
1287         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1288         packet_put_cstring(authctxt->server_user);
1289         packet_put_cstring(authctxt->service);
1290         packet_put_cstring(authctxt->method->name);
1291         packet_put_cstring(pkalg);
1292         packet_put_string(blob, blen);
1293         packet_put_cstring(chost);
1294         packet_put_cstring(authctxt->local_user);
1295         packet_put_string(signature, slen);
1296         memset(signature, 's', slen);
1297         xfree(signature);
1298         xfree(chost);
1299         xfree(pkalg);
1300
1301         packet_send();
1302         return 1;
1303 }
1304
1305 /* find auth method */
1306
1307 /*
1308  * given auth method name, if configurable options permit this method fill
1309  * in auth_ident field and return true, otherwise return false.
1310  */
1311 static int
1312 authmethod_is_enabled(Authmethod *method)
1313 {
1314         if (method == NULL)
1315                 return 0;
1316         /* return false if options indicate this method is disabled */
1317         if  (method->enabled == NULL || *method->enabled == 0)
1318                 return 0;
1319         /* return false if batch mode is enabled but method needs interactive mode */
1320         if  (method->batch_flag != NULL && *method->batch_flag != 0)
1321                 return 0;
1322         return 1;
1323 }
1324
1325 static Authmethod *
1326 authmethod_lookup(const char *name)
1327 {
1328         Authmethod *method = NULL;
1329         if (name != NULL)
1330                 for (method = authmethods; method->name != NULL; method++)
1331                         if (strcmp(name, method->name) == 0)
1332                                 return method;
1333         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1334         return NULL;
1335 }
1336
1337 /* XXX internal state */
1338 static Authmethod *current = NULL;
1339 static char *supported = NULL;
1340 static char *preferred = NULL;
1341
1342 /*
1343  * Given the authentication method list sent by the server, return the
1344  * next method we should try.  If the server initially sends a nil list,
1345  * use a built-in default list.
1346  */
1347 static Authmethod *
1348 authmethod_get(char *authlist)
1349 {
1350
1351         char *name = NULL;
1352         u_int next;
1353
1354         /* Use a suitable default if we're passed a nil list.  */
1355         if (authlist == NULL || strlen(authlist) == 0)
1356                 authlist = options.preferred_authentications;
1357
1358         if (supported == NULL || strcmp(authlist, supported) != 0) {
1359                 debug3("start over, passed a different list %s", authlist);
1360                 if (supported != NULL)
1361                         xfree(supported);
1362                 supported = xstrdup(authlist);
1363                 preferred = options.preferred_authentications;
1364                 debug3("preferred %s", preferred);
1365                 current = NULL;
1366         } else if (current != NULL && authmethod_is_enabled(current))
1367                 return current;
1368
1369         for (;;) {
1370                 if ((name = match_list(preferred, supported, &next)) == NULL) {
1371                         debug("no more auth methods to try");
1372                         current = NULL;
1373                         return NULL;
1374                 }
1375                 preferred += next;
1376                 debug3("authmethod_lookup %s", name);
1377                 debug3("remaining preferred: %s", preferred);
1378                 if ((current = authmethod_lookup(name)) != NULL &&
1379                     authmethod_is_enabled(current)) {
1380                         debug3("authmethod_is_enabled %s", name);
1381                         debug("next auth method to try is %s", name);
1382                         return current;
1383                 }
1384         }
1385 }
1386
1387 static char *
1388 authmethods_get(void)
1389 {
1390         Authmethod *method = NULL;
1391         Buffer b;
1392         char *list;
1393
1394         buffer_init(&b);
1395         for (method = authmethods; method->name != NULL; method++) {
1396                 if (authmethod_is_enabled(method)) {
1397                         if (buffer_len(&b) > 0)
1398                                 buffer_append(&b, ",", 1);
1399                         buffer_append(&b, method->name, strlen(method->name));
1400                 }
1401         }
1402         buffer_append(&b, "\0", 1);
1403         list = xstrdup(buffer_ptr(&b));
1404         buffer_free(&b);
1405         return list;
1406 }
This page took 0.157415 seconds and 5 git commands to generate.