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