]> andersk Git - gssapi-openssh.git/blob - openssh/sshconnect2.c
merged OpenSSH 5.2p1 to trunk
[gssapi-openssh.git] / openssh / sshconnect2.c
1 /* $OpenBSD: sshconnect2.c,v 1.170 2008/11/04 08:22:13 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  * Copyright (c) 2008 Damien Miller.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 #include <sys/stat.h>
33
34 #include <errno.h>
35 #include <netdb.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
43 #include <vis.h>
44 #endif
45
46 #include "openbsd-compat/sys-queue.h"
47
48 #include "xmalloc.h"
49 #include "ssh.h"
50 #include "ssh2.h"
51 #include "buffer.h"
52 #include "packet.h"
53 #include "compat.h"
54 #include "cipher.h"
55 #include "key.h"
56 #include "kex.h"
57 #include "myproposal.h"
58 #include "sshconnect.h"
59 #include "authfile.h"
60 #include "dh.h"
61 #include "authfd.h"
62 #include "log.h"
63 #include "readconf.h"
64 #include "misc.h"
65 #include "match.h"
66 #include "dispatch.h"
67 #include "canohost.h"
68 #include "msg.h"
69 #include "pathnames.h"
70 #include "uidswap.h"
71 #include "jpake.h"
72
73 #ifdef GSSAPI
74 #include "ssh-gss.h"
75 #endif
76
77 /* import */
78 extern char *client_version_string;
79 extern char *server_version_string;
80 extern Options options;
81
82 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
83 /* if it is set then prevent the switch to the null cipher */
84
85 extern int tty_flag;
86
87 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
88 /* if it is set then prevent the switch to the null cipher */
89
90 extern int tty_flag;
91
92 /*
93  * SSH2 key exchange
94  */
95
96 u_char *session_id2 = NULL;
97 u_int session_id2_len = 0;
98
99 char *xxx_host;
100 struct sockaddr *xxx_hostaddr;
101
102 Kex *xxx_kex = NULL;
103
104 static int
105 verify_host_key_callback(Key *hostkey)
106 {
107         if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
108                 fatal("Host key verification failed.");
109         return 0;
110 }
111
112 void
113 ssh_kex2(char *host, struct sockaddr *hostaddr)
114 {
115         Kex *kex;
116
117 #ifdef GSSAPI
118         char *orig = NULL, *gss = NULL;
119         char *gss_host = NULL;
120 #endif
121
122         xxx_host = host;
123         xxx_hostaddr = hostaddr;
124
125 #ifdef GSSAPI
126         if (options.gss_keyex) {
127                 /* Add the GSSAPI mechanisms currently supported on this 
128                  * client to the key exchange algorithm proposal */
129                 orig = myproposal[PROPOSAL_KEX_ALGS];
130
131                 if (options.gss_trust_dns)
132                         gss_host = (char *)get_canonical_hostname(1);
133                 else
134                         gss_host = host;
135
136                 gss = ssh_gssapi_client_mechanisms(gss_host);
137                 if (gss) {
138                         debug("Offering GSSAPI proposal: %s", gss);
139                         xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
140                             "%s,%s", gss, orig);
141                 }
142         }
143 #endif
144
145         if (options.ciphers == (char *)-1) {
146                 logit("No valid ciphers for protocol version 2 given, using defaults.");
147                 options.ciphers = NULL;
148         }
149         if (options.ciphers != NULL) {
150                 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
151                 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
152         }
153         myproposal[PROPOSAL_ENC_ALGS_CTOS] =
154             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
155         myproposal[PROPOSAL_ENC_ALGS_STOC] =
156             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
157         if (options.compression) {
158                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
159                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
160         } else {
161                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
162                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
163         }
164         if (options.macs != NULL) {
165                 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
166                 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
167         }
168         if (options.hostkeyalgorithms != NULL)
169                 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
170                     options.hostkeyalgorithms;
171
172 #ifdef GSSAPI
173         /* If we've got GSSAPI algorithms, then we also support the
174          * 'null' hostkey, as a last resort */
175         if (options.gss_keyex && gss) {
176                 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
177                 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], 
178                     "%s,null", orig);
179         }
180 #endif
181
182         if (options.rekey_limit)
183                 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
184
185         /* start key exchange */
186         kex = kex_setup(myproposal);
187         kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
188         kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
189         kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
190         kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
191 #ifdef GSSAPI
192         kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
193         kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
194         kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
195 #endif
196         kex->client_version_string=client_version_string;
197         kex->server_version_string=server_version_string;
198         kex->verify_host_key=&verify_host_key_callback;
199
200 #ifdef GSSAPI
201         kex->gss_deleg_creds = options.gss_deleg_creds;
202         kex->gss_trust_dns = options.gss_trust_dns;
203         kex->gss_host = gss_host;
204 #endif
205
206         xxx_kex = kex;
207
208         dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
209
210         session_id2 = kex->session_id;
211         session_id2_len = kex->session_id_len;
212
213 #ifdef DEBUG_KEXDH
214         /* send 1st encrypted/maced/compressed message */
215         packet_start(SSH2_MSG_IGNORE);
216         packet_put_cstring("markus");
217         packet_send();
218         packet_write_wait();
219 #endif
220 }
221
222 /*
223  * Authenticate user
224  */
225
226 typedef struct Authctxt Authctxt;
227 typedef struct Authmethod Authmethod;
228 typedef struct identity Identity;
229 typedef struct idlist Idlist;
230
231 struct identity {
232         TAILQ_ENTRY(identity) next;
233         AuthenticationConnection *ac;   /* set if agent supports key */
234         Key     *key;                   /* public/private key */
235         char    *filename;              /* comment for agent-only keys */
236         int     tried;
237         int     isprivate;              /* key points to the private key */
238 };
239 TAILQ_HEAD(idlist, identity);
240
241 struct Authctxt {
242         const char *server_user;
243         const char *local_user;
244         const char *host;
245         const char *service;
246         Authmethod *method;
247         int success;
248         char *authlist;
249         /* pubkey */
250         Idlist keys;
251         AuthenticationConnection *agent;
252         /* hostbased */
253         Sensitive *sensitive;
254         /* kbd-interactive */
255         int info_req_seen;
256         /* generic */
257         void *methoddata;
258 };
259 struct Authmethod {
260         char    *name;          /* string to compare against server's list */
261         int     (*userauth)(Authctxt *authctxt);
262         void    (*cleanup)(Authctxt *authctxt);
263         int     *enabled;       /* flag in option struct that enables method */
264         int     *batch_flag;    /* flag in option struct that disables method */
265 };
266
267 void    input_userauth_success(int, u_int32_t, void *);
268 void    input_userauth_failure(int, u_int32_t, void *);
269 void    input_userauth_banner(int, u_int32_t, void *);
270 void    input_userauth_error(int, u_int32_t, void *);
271 void    input_userauth_info_req(int, u_int32_t, void *);
272 void    input_userauth_pk_ok(int, u_int32_t, void *);
273 void    input_userauth_passwd_changereq(int, u_int32_t, void *);
274 void    input_userauth_jpake_server_step1(int, u_int32_t, void *);
275 void    input_userauth_jpake_server_step2(int, u_int32_t, void *);
276 void    input_userauth_jpake_server_confirm(int, u_int32_t, void *);
277
278 int     userauth_none(Authctxt *);
279 int     userauth_pubkey(Authctxt *);
280 int     userauth_passwd(Authctxt *);
281 int     userauth_kbdint(Authctxt *);
282 int     userauth_hostbased(Authctxt *);
283 int     userauth_jpake(Authctxt *);
284
285 void    userauth_jpake_cleanup(Authctxt *);
286
287 #ifdef GSSAPI
288 int     userauth_external(Authctxt *authctxt);
289 int     userauth_gssapi(Authctxt *authctxt);
290 int     userauth_gssapi_with_mic(Authctxt *authctxt);
291 int     userauth_gssapi_without_mic(Authctxt *authctxt);
292 void    input_gssapi_response(int type, u_int32_t, void *);
293 void    input_gssapi_token(int type, u_int32_t, void *);
294 void    input_gssapi_hash(int type, u_int32_t, void *);
295 void    input_gssapi_error(int, u_int32_t, void *);
296 void    input_gssapi_errtok(int, u_int32_t, void *);
297 int     userauth_gsskeyex(Authctxt *authctxt);
298 #endif
299
300 void    userauth(Authctxt *, char *);
301
302 static int sign_and_send_pubkey(Authctxt *, Identity *);
303 static void pubkey_prepare(Authctxt *);
304 static void pubkey_cleanup(Authctxt *);
305 static Key *load_identity_file(char *);
306
307 static Authmethod *authmethod_get(char *authlist);
308 static Authmethod *authmethod_lookup(const char *name);
309 static char *authmethods_get(void);
310
311 Authmethod authmethods[] = {
312 #ifdef GSSAPI
313         {"gssapi-keyex",
314                 userauth_gsskeyex,
315                 &options.gss_authentication,
316                 NULL},
317         {"external-keyx",
318                 userauth_external,
319                 &options.gss_authentication,
320                 NULL},
321         {"gssapi-with-mic",
322                 userauth_gssapi,
323                 NULL,
324                 &options.gss_authentication,
325                 NULL},
326         {"gssapi",
327                 userauth_gssapi,
328                 &options.gss_authentication,
329                 NULL},
330 #endif
331         {"hostbased",
332                 userauth_hostbased,
333                 NULL,
334                 &options.hostbased_authentication,
335                 NULL},
336         {"publickey",
337                 userauth_pubkey,
338                 NULL,
339                 &options.pubkey_authentication,
340                 NULL},
341 #ifdef JPAKE
342         {"jpake-01@openssh.com",
343                 userauth_jpake,
344                 userauth_jpake_cleanup,
345                 &options.zero_knowledge_password_authentication,
346                 &options.batch_mode},
347 #endif
348         {"keyboard-interactive",
349                 userauth_kbdint,
350                 NULL,
351                 &options.kbd_interactive_authentication,
352                 &options.batch_mode},
353         {"password",
354                 userauth_passwd,
355                 NULL,
356                 &options.password_authentication,
357                 &options.batch_mode},
358         {"none",
359                 userauth_none,
360                 NULL,
361                 NULL,
362                 NULL},
363         {NULL, NULL, NULL, NULL, NULL}
364 };
365
366 void
367 ssh_userauth2(const char *local_user, const char *server_user, char *host,
368     Sensitive *sensitive)
369 {
370         Authctxt authctxt;
371         int type;
372
373         if (options.challenge_response_authentication)
374                 options.kbd_interactive_authentication = 1;
375
376         packet_start(SSH2_MSG_SERVICE_REQUEST);
377         packet_put_cstring("ssh-userauth");
378         packet_send();
379         debug("SSH2_MSG_SERVICE_REQUEST sent");
380         packet_write_wait();
381         type = packet_read();
382         if (type != SSH2_MSG_SERVICE_ACCEPT)
383                 fatal("Server denied authentication request: %d", type);
384         if (packet_remaining() > 0) {
385                 char *reply = packet_get_string(NULL);
386                 debug2("service_accept: %s", reply);
387                 xfree(reply);
388         } else {
389                 debug2("buggy server: service_accept w/o service");
390         }
391         packet_check_eom();
392         debug("SSH2_MSG_SERVICE_ACCEPT received");
393
394         if (options.preferred_authentications == NULL)
395                 options.preferred_authentications = authmethods_get();
396
397         /* setup authentication context */
398         memset(&authctxt, 0, sizeof(authctxt));
399         pubkey_prepare(&authctxt);
400         authctxt.server_user = server_user;
401         authctxt.local_user = local_user;
402         authctxt.host = host;
403         authctxt.service = "ssh-connection";            /* service name */
404         authctxt.success = 0;
405         authctxt.method = authmethod_lookup("none");
406         authctxt.authlist = NULL;
407         authctxt.methoddata = NULL;
408         authctxt.sensitive = sensitive;
409         authctxt.info_req_seen = 0;
410         if (authctxt.method == NULL)
411                 fatal("ssh_userauth2: internal error: cannot send userauth none request");
412
413         /* initial userauth request */
414         userauth_none(&authctxt);
415
416         dispatch_init(&input_userauth_error);
417         dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
418         dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
419         dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
420         dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt);     /* loop until success */
421
422         pubkey_cleanup(&authctxt);
423         dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
424
425         /* if the user wants to use the none cipher do it */
426         /* post authentication and only if the right conditions are met */
427         /* both of the NONE commands must be true and there must be no */
428         /* tty allocated */
429         if ((options.none_switch == 1) && (options.none_enabled == 1)) 
430         {
431                 if (!tty_flag) /* no null on tty sessions */
432                 {
433                         debug("Requesting none rekeying...");
434                         myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
435                         myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
436                         kex_prop2buf(&xxx_kex->my,myproposal);
437                         packet_request_rekeying();
438                         fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
439                 }
440                 else
441                 {
442                         /* requested NONE cipher when in a tty */
443                         debug("Cannot switch to NONE cipher with tty allocated");
444                         fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
445                 }
446         }
447         debug("Authentication succeeded (%s).", authctxt.method->name);
448 }
449
450 void
451 userauth(Authctxt *authctxt, char *authlist)
452 {
453         if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
454                 authctxt->method->cleanup(authctxt);
455
456         if (authctxt->methoddata) {
457                 xfree(authctxt->methoddata);
458                 authctxt->methoddata = NULL;
459         }
460         if (authlist == NULL) {
461                 authlist = authctxt->authlist;
462         } else {
463                 if (authctxt->authlist)
464                         xfree(authctxt->authlist);
465                 authctxt->authlist = authlist;
466         }
467         for (;;) {
468                 Authmethod *method = authmethod_get(authlist);
469                 if (method == NULL)
470                         fatal("Permission denied (%s).", authlist);
471                 authctxt->method = method;
472
473                 /* reset the per method handler */
474                 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
475                     SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
476
477                 /* and try new method */
478                 if (method->userauth(authctxt) != 0) {
479                         debug2("we sent a %s packet, wait for reply", method->name);
480                         break;
481                 } else {
482                         debug2("we did not send a packet, disable method");
483                         method->enabled = NULL;
484                 }
485         }
486 }
487
488 /* ARGSUSED */
489 void
490 input_userauth_error(int type, u_int32_t seq, void *ctxt)
491 {
492         fatal("input_userauth_error: bad message during authentication: "
493             "type %d", type);
494 }
495
496 /* ARGSUSED */
497 void
498 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
499 {
500         char *msg, *raw, *lang;
501         u_int len;
502
503         debug3("input_userauth_banner");
504         raw = packet_get_string(&len);
505         lang = packet_get_string(NULL);
506         if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
507                 if (len > 65536)
508                         len = 65536;
509                 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
510                 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
511                 fprintf(stderr, "%s", msg);
512                 xfree(msg);
513         }
514         xfree(raw);
515         xfree(lang);
516 }
517
518 /* ARGSUSED */
519 void
520 input_userauth_success(int type, u_int32_t seq, void *ctxt)
521 {
522         Authctxt *authctxt = ctxt;
523         if (authctxt == NULL)
524                 fatal("input_userauth_success: no authentication context");
525         if (authctxt->authlist) {
526                 xfree(authctxt->authlist);
527                 authctxt->authlist = NULL;
528         }
529         if (authctxt->methoddata) {
530                 xfree(authctxt->methoddata);
531                 authctxt->methoddata = NULL;
532         }
533         authctxt->success = 1;                  /* break out */
534 }
535
536 /* ARGSUSED */
537 void
538 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
539 {
540         Authctxt *authctxt = ctxt;
541         char *authlist = NULL;
542         int partial;
543
544         if (authctxt == NULL)
545                 fatal("input_userauth_failure: no authentication context");
546
547         authlist = packet_get_string(NULL);
548         partial = packet_get_char();
549         packet_check_eom();
550
551         if (partial != 0)
552                 logit("Authenticated with partial success.");
553         debug("Authentications that can continue: %s", authlist);
554
555         userauth(authctxt, authlist);
556 }
557
558 /* ARGSUSED */
559 void
560 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
561 {
562         Authctxt *authctxt = ctxt;
563         Key *key = NULL;
564         Identity *id = NULL;
565         Buffer b;
566         int pktype, sent = 0;
567         u_int alen, blen;
568         char *pkalg, *fp;
569         u_char *pkblob;
570
571         if (authctxt == NULL)
572                 fatal("input_userauth_pk_ok: no authentication context");
573         if (datafellows & SSH_BUG_PKOK) {
574                 /* this is similar to SSH_BUG_PKAUTH */
575                 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
576                 pkblob = packet_get_string(&blen);
577                 buffer_init(&b);
578                 buffer_append(&b, pkblob, blen);
579                 pkalg = buffer_get_string(&b, &alen);
580                 buffer_free(&b);
581         } else {
582                 pkalg = packet_get_string(&alen);
583                 pkblob = packet_get_string(&blen);
584         }
585         packet_check_eom();
586
587         debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
588
589         if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
590                 debug("unknown pkalg %s", pkalg);
591                 goto done;
592         }
593         if ((key = key_from_blob(pkblob, blen)) == NULL) {
594                 debug("no key from blob. pkalg %s", pkalg);
595                 goto done;
596         }
597         if (key->type != pktype) {
598                 error("input_userauth_pk_ok: type mismatch "
599                     "for decoded key (received %d, expected %d)",
600                     key->type, pktype);
601                 goto done;
602         }
603         fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
604         debug2("input_userauth_pk_ok: fp %s", fp);
605         xfree(fp);
606
607         /*
608          * search keys in the reverse order, because last candidate has been
609          * moved to the end of the queue.  this also avoids confusion by
610          * duplicate keys
611          */
612         TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
613                 if (key_equal(key, id->key)) {
614                         sent = sign_and_send_pubkey(authctxt, id);
615                         break;
616                 }
617         }
618 done:
619         if (key != NULL)
620                 key_free(key);
621         xfree(pkalg);
622         xfree(pkblob);
623
624         /* try another method if we did not send a packet */
625         if (sent == 0)
626                 userauth(authctxt, NULL);
627 }
628
629 #ifdef GSSAPI
630 int
631 userauth_gssapi(Authctxt *authctxt)
632 {
633         Gssctxt *gssctxt = NULL;
634         static gss_OID_set gss_supported = NULL;
635         static u_int mech = 0;
636         OM_uint32 min;
637         int ok = 0;
638         char *gss_host = NULL;
639
640         if (!options.gss_authentication) {
641                 verbose("GSSAPI authentication disabled.");
642                 return 0;
643         }
644
645         if (options.gss_trust_dns)
646                 gss_host = (char *)get_canonical_hostname(1);
647         else
648                 gss_host = (char *)authctxt->host;
649
650         /* Try one GSSAPI method at a time, rather than sending them all at
651          * once. */
652
653         if (gss_supported == NULL)
654                 gss_indicate_mechs(&min, &gss_supported);
655
656         /* Check to see if the mechanism is usable before we offer it */
657         while (mech < gss_supported->count && !ok) {
658                 /* My DER encoding requires length<128 */
659                 if (gss_supported->elements[mech].length < 128 &&
660                     ssh_gssapi_check_mechanism(&gssctxt, 
661                     &gss_supported->elements[mech], gss_host)) {
662                         ok = 1; /* Mechanism works */
663                 } else {
664                         mech++;
665                 }
666         }
667
668         if (!ok)
669                 return 0;
670
671         authctxt->methoddata=(void *)gssctxt;
672
673         packet_start(SSH2_MSG_USERAUTH_REQUEST);
674         packet_put_cstring(authctxt->server_user);
675         packet_put_cstring(authctxt->service);
676         packet_put_cstring(authctxt->method->name);
677
678         packet_put_int(1);
679
680         packet_put_int((gss_supported->elements[mech].length) + 2);
681         packet_put_char(SSH_GSS_OIDTYPE);
682         packet_put_char(gss_supported->elements[mech].length);
683         packet_put_raw(gss_supported->elements[mech].elements,
684             gss_supported->elements[mech].length);
685
686         packet_send();
687
688         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
689         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
690         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
691         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
692
693         mech++; /* Move along to next candidate */
694
695         return 1;
696 }
697
698 static OM_uint32
699 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
700 {
701         Authctxt *authctxt = ctxt;
702         Gssctxt *gssctxt = authctxt->methoddata;
703         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
704         gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
705         gss_buffer_desc gssbuf;
706         OM_uint32 status, ms, flags;
707         Buffer b;
708
709         status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
710             recv_tok, &send_tok, &flags);
711
712         if (send_tok.length > 0) {
713                 if (GSS_ERROR(status))
714                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
715                 else
716                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
717
718                 packet_put_string(send_tok.value, send_tok.length);
719                 packet_send();
720                 gss_release_buffer(&ms, &send_tok);
721         }
722
723         if (status == GSS_S_COMPLETE) {
724                 /* send either complete or MIC, depending on mechanism */
725                 if (strcmp(authctxt->method->name,"gssapi")==0 ||
726                     (!(flags & GSS_C_INTEG_FLAG))) {
727                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
728                         packet_send();
729                 } else {
730                         ssh_gssapi_buildmic(&b, authctxt->server_user,
731                             authctxt->service, "gssapi-with-mic");
732
733                         gssbuf.value = buffer_ptr(&b);
734                         gssbuf.length = buffer_len(&b);
735
736                         status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
737
738                         if (!GSS_ERROR(status)) {
739                                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
740                                 packet_put_string(mic.value, mic.length);
741
742                                 packet_send();
743                         }
744
745                         buffer_free(&b);
746                         gss_release_buffer(&ms, &mic);
747                 }
748         }
749
750         return status;
751 }
752
753 /* ARGSUSED */
754 void
755 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
756 {
757         Authctxt *authctxt = ctxt;
758         Gssctxt *gssctxt;
759         u_int oidlen;
760         u_char *oidv;
761
762         if (authctxt == NULL)
763                 fatal("input_gssapi_response: no authentication context");
764         gssctxt = authctxt->methoddata;
765
766         /* Setup our OID */
767         oidv = packet_get_string(&oidlen);
768
769         if (oidlen <= 2 ||
770             oidv[0] != SSH_GSS_OIDTYPE ||
771             oidv[1] != oidlen - 2) {
772                 xfree(oidv);
773                 debug("Badly encoded mechanism OID received");
774                 userauth(authctxt, NULL);
775                 return;
776         }
777
778         if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
779                 fatal("Server returned different OID than expected");
780
781         packet_check_eom();
782
783         xfree(oidv);
784
785         if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
786                 /* Start again with next method on list */
787                 debug("Trying to start again");
788                 userauth(authctxt, NULL);
789                 return;
790         }
791 }
792
793 /* ARGSUSED */
794 void
795 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
796 {
797         Authctxt *authctxt = ctxt;
798         gss_buffer_desc recv_tok;
799         OM_uint32 status;
800         u_int slen;
801
802         if (authctxt == NULL)
803                 fatal("input_gssapi_response: no authentication context");
804
805         recv_tok.value = packet_get_string(&slen);
806         recv_tok.length = slen; /* safe typecast */
807
808         packet_check_eom();
809
810         status = process_gssapi_token(ctxt, &recv_tok);
811
812         xfree(recv_tok.value);
813
814         if (GSS_ERROR(status)) {
815                 /* Start again with the next method in the list */
816                 userauth(authctxt, NULL);
817                 return;
818         }
819 }
820
821 /* ARGSUSED */
822 void
823 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
824 {
825         Authctxt *authctxt = ctxt;
826         Gssctxt *gssctxt;
827         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
828         gss_buffer_desc recv_tok;
829         OM_uint32 status, ms;
830         u_int len;
831
832         if (authctxt == NULL)
833                 fatal("input_gssapi_response: no authentication context");
834         gssctxt = authctxt->methoddata;
835
836         recv_tok.value = packet_get_string(&len);
837         recv_tok.length = len;
838
839         packet_check_eom();
840
841         /* Stick it into GSSAPI and see what it says */
842         status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
843             &recv_tok, &send_tok, NULL);
844
845         xfree(recv_tok.value);
846         gss_release_buffer(&ms, &send_tok);
847
848         /* Server will be returning a failed packet after this one */
849 }
850
851 /* ARGSUSED */
852 void
853 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
854 {
855         OM_uint32 maj, min;
856         char *msg;
857         char *lang;
858
859         maj=packet_get_int();
860         min=packet_get_int();
861         msg=packet_get_string(NULL);
862         lang=packet_get_string(NULL);
863
864         packet_check_eom();
865
866         debug("Server GSSAPI Error:\n%s", msg);
867         xfree(msg);
868         xfree(lang);
869 }
870
871 #ifdef GSI
872 extern
873 const gss_OID_desc * const              gss_mech_globus_gssapi_openssl;
874 #define is_gsi_oid(oid) \
875   (oid->length == gss_mech_globus_gssapi_openssl->length && \
876    (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
877            oid->length) == 0))
878 #endif
879
880 int
881 userauth_external(Authctxt *authctxt)
882 {
883         static int attempt = 0;
884         
885         if (attempt++ >= 1)
886                 return 0;
887                                 
888         /* The client MUST NOT try this method if initial key exchange
889            was not performed using a GSSAPI-based key exchange
890            method. */
891         if (gss_kex_context == NULL) {
892                 debug2("gsskex not performed, skipping external-keyx");
893                 return 0;
894         }
895
896         debug2("userauth_external");
897         packet_start(SSH2_MSG_USERAUTH_REQUEST);
898 #ifdef GSI
899         if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
900         packet_put_cstring("");
901         } else {
902 #endif
903         packet_put_cstring(authctxt->server_user);
904 #ifdef GSI
905         }
906 #endif
907         packet_put_cstring(authctxt->service);
908         packet_put_cstring(authctxt->method->name);
909         packet_send();
910         packet_write_wait();
911         return 1;
912 }                                                                                                
913 int
914 userauth_gsskeyex(Authctxt *authctxt)
915 {
916         Buffer b;
917         gss_buffer_desc gssbuf;
918         gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
919         OM_uint32 ms;
920
921         static int attempt = 0;
922         if (attempt++ >= 1)
923                 return (0);
924
925         if (gss_kex_context == NULL) {
926                 debug("No valid Key exchange context"); 
927                 return (0);
928         }
929
930 #ifdef GSI
931         if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
932         ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
933         } else {
934 #endif
935         ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
936             "gssapi-keyex");
937 #ifdef GSI
938         }
939 #endif
940
941         gssbuf.value = buffer_ptr(&b);
942         gssbuf.length = buffer_len(&b);
943
944         if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
945                 buffer_free(&b);
946                 return (0);
947         }
948
949         packet_start(SSH2_MSG_USERAUTH_REQUEST);
950 #ifdef GSI
951         if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
952         packet_put_cstring("");
953         } else {
954 #endif
955         packet_put_cstring(authctxt->server_user);
956 #ifdef GSI
957         }
958 #endif
959         packet_put_cstring(authctxt->service);
960         packet_put_cstring(authctxt->method->name);
961         packet_put_string(mic.value, mic.length);
962         packet_send();
963
964         buffer_free(&b);
965         gss_release_buffer(&ms, &mic);
966
967         return (1);
968 }
969
970 #endif /* GSSAPI */
971
972 int
973 userauth_none(Authctxt *authctxt)
974 {
975         /* initial userauth request */
976         packet_start(SSH2_MSG_USERAUTH_REQUEST);
977         packet_put_cstring(authctxt->server_user);
978         packet_put_cstring(authctxt->service);
979         packet_put_cstring(authctxt->method->name);
980         packet_send();
981         return 1;
982 }
983
984 int
985 userauth_passwd(Authctxt *authctxt)
986 {
987         static int attempt = 0;
988         char prompt[150];
989         char *password;
990
991         if (attempt++ >= options.number_of_password_prompts)
992                 return 0;
993
994         if (attempt != 1)
995                 error("Permission denied, please try again.");
996
997         snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
998             authctxt->server_user, authctxt->host);
999         password = read_passphrase(prompt, 0);
1000         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1001         packet_put_cstring(authctxt->server_user);
1002         packet_put_cstring(authctxt->service);
1003         packet_put_cstring(authctxt->method->name);
1004         packet_put_char(0);
1005         packet_put_cstring(password);
1006         memset(password, 0, strlen(password));
1007         xfree(password);
1008         packet_add_padding(64);
1009         packet_send();
1010
1011         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1012             &input_userauth_passwd_changereq);
1013
1014         return 1;
1015 }
1016
1017 /*
1018  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1019  */
1020 /* ARGSUSED */
1021 void
1022 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
1023 {
1024         Authctxt *authctxt = ctxt;
1025         char *info, *lang, *password = NULL, *retype = NULL;
1026         char prompt[150];
1027
1028         debug2("input_userauth_passwd_changereq");
1029
1030         if (authctxt == NULL)
1031                 fatal("input_userauth_passwd_changereq: "
1032                     "no authentication context");
1033
1034         info = packet_get_string(NULL);
1035         lang = packet_get_string(NULL);
1036         if (strlen(info) > 0)
1037                 logit("%s", info);
1038         xfree(info);
1039         xfree(lang);
1040         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1041         packet_put_cstring(authctxt->server_user);
1042         packet_put_cstring(authctxt->service);
1043         packet_put_cstring(authctxt->method->name);
1044         packet_put_char(1);                     /* additional info */
1045         snprintf(prompt, sizeof(prompt),
1046             "Enter %.30s@%.128s's old password: ",
1047             authctxt->server_user, authctxt->host);
1048         password = read_passphrase(prompt, 0);
1049         packet_put_cstring(password);
1050         memset(password, 0, strlen(password));
1051         xfree(password);
1052         password = NULL;
1053         while (password == NULL) {
1054                 snprintf(prompt, sizeof(prompt),
1055                     "Enter %.30s@%.128s's new password: ",
1056                     authctxt->server_user, authctxt->host);
1057                 password = read_passphrase(prompt, RP_ALLOW_EOF);
1058                 if (password == NULL) {
1059                         /* bail out */
1060                         return;
1061                 }
1062                 snprintf(prompt, sizeof(prompt),
1063                     "Retype %.30s@%.128s's new password: ",
1064                     authctxt->server_user, authctxt->host);
1065                 retype = read_passphrase(prompt, 0);
1066                 if (strcmp(password, retype) != 0) {
1067                         memset(password, 0, strlen(password));
1068                         xfree(password);
1069                         logit("Mismatch; try again, EOF to quit.");
1070                         password = NULL;
1071                 }
1072                 memset(retype, 0, strlen(retype));
1073                 xfree(retype);
1074         }
1075         packet_put_cstring(password);
1076         memset(password, 0, strlen(password));
1077         xfree(password);
1078         packet_add_padding(64);
1079         packet_send();
1080
1081         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1082             &input_userauth_passwd_changereq);
1083 }
1084
1085 #ifdef JPAKE
1086 static char *
1087 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1088 {
1089         /* OpenBSD crypt(3) handles all of these */
1090         if (strcmp(crypt_scheme, "crypt") == 0 ||
1091             strcmp(crypt_scheme, "bcrypt") == 0 ||
1092             strcmp(crypt_scheme, "md5crypt") == 0 ||
1093             strcmp(crypt_scheme, "crypt-extended") == 0)
1094                 return xstrdup(crypt(password, salt));
1095         error("%s: unsupported password encryption scheme \"%.100s\"",
1096             __func__, crypt_scheme);
1097         return NULL;
1098 }
1099
1100 static BIGNUM *
1101 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1102     const char *salt)
1103 {
1104         char prompt[256], *password, *crypted;
1105         u_char *secret;
1106         u_int secret_len;
1107         BIGNUM *ret;
1108
1109         snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1110             authctxt->server_user, authctxt->host);
1111         password = read_passphrase(prompt, 0);
1112
1113         if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1114                 logit("Disabling %s authentication", authctxt->method->name);
1115                 authctxt->method->enabled = NULL;
1116                 /* Continue with an empty password to fail gracefully */
1117                 crypted = xstrdup("");
1118         }
1119
1120 #ifdef JPAKE_DEBUG
1121         debug3("%s: salt = %s", __func__, salt);
1122         debug3("%s: scheme = %s", __func__, crypt_scheme);
1123         debug3("%s: crypted = %s", __func__, crypted);
1124 #endif
1125
1126         if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1127             &secret, &secret_len) != 0)
1128                 fatal("%s: hash_buffer", __func__);
1129
1130         bzero(password, strlen(password));
1131         bzero(crypted, strlen(crypted));
1132         xfree(password);
1133         xfree(crypted);
1134
1135         if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1136                 fatal("%s: BN_bin2bn (secret)", __func__);
1137         bzero(secret, secret_len);
1138         xfree(secret);
1139
1140         return ret;
1141 }
1142
1143 /* ARGSUSED */
1144 void
1145 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1146 {
1147         Authctxt *authctxt = ctxt;
1148         struct jpake_ctx *pctx = authctxt->methoddata;
1149         u_char *x3_proof, *x4_proof, *x2_s_proof;
1150         u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1151         char *crypt_scheme, *salt;
1152
1153         /* Disable this message */
1154         dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1155
1156         if ((pctx->g_x3 = BN_new()) == NULL ||
1157             (pctx->g_x4 = BN_new()) == NULL)
1158                 fatal("%s: BN_new", __func__);
1159
1160         /* Fetch step 1 values */
1161         crypt_scheme = packet_get_string(NULL);
1162         salt = packet_get_string(NULL);
1163         pctx->server_id = packet_get_string(&pctx->server_id_len);
1164         packet_get_bignum2(pctx->g_x3);
1165         packet_get_bignum2(pctx->g_x4);
1166         x3_proof = packet_get_string(&x3_proof_len);
1167         x4_proof = packet_get_string(&x4_proof_len);
1168         packet_check_eom();
1169
1170         JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1171
1172         /* Obtain password and derive secret */
1173         pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1174         bzero(crypt_scheme, strlen(crypt_scheme));
1175         bzero(salt, strlen(salt));
1176         xfree(crypt_scheme);
1177         xfree(salt);
1178         JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1179
1180         /* Calculate step 2 values */
1181         jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1182             pctx->g_x3, pctx->g_x4, pctx->x2,
1183             pctx->server_id, pctx->server_id_len,
1184             pctx->client_id, pctx->client_id_len,
1185             x3_proof, x3_proof_len,
1186             x4_proof, x4_proof_len,
1187             &pctx->a,
1188             &x2_s_proof, &x2_s_proof_len);
1189
1190         bzero(x3_proof, x3_proof_len);
1191         bzero(x4_proof, x4_proof_len);
1192         xfree(x3_proof);
1193         xfree(x4_proof);
1194
1195         JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1196
1197         /* Send values for step 2 */
1198         packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1199         packet_put_bignum2(pctx->a);
1200         packet_put_string(x2_s_proof, x2_s_proof_len);
1201         packet_send();
1202
1203         bzero(x2_s_proof, x2_s_proof_len);
1204         xfree(x2_s_proof);
1205
1206         /* Expect step 2 packet from peer */
1207         dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1208             input_userauth_jpake_server_step2);
1209 }
1210
1211 /* ARGSUSED */
1212 void
1213 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1214 {
1215         Authctxt *authctxt = ctxt;
1216         struct jpake_ctx *pctx = authctxt->methoddata;
1217         u_char *x4_s_proof;
1218         u_int x4_s_proof_len;
1219
1220         /* Disable this message */
1221         dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1222
1223         if ((pctx->b = BN_new()) == NULL)
1224                 fatal("%s: BN_new", __func__);
1225
1226         /* Fetch step 2 values */
1227         packet_get_bignum2(pctx->b);
1228         x4_s_proof = packet_get_string(&x4_s_proof_len);
1229         packet_check_eom();
1230
1231         JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1232
1233         /* Derive shared key and calculate confirmation hash */
1234         jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1235             pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1236             pctx->client_id, pctx->client_id_len,
1237             pctx->server_id, pctx->server_id_len,
1238             session_id2, session_id2_len,
1239             x4_s_proof, x4_s_proof_len,
1240             &pctx->k,
1241             &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1242
1243         bzero(x4_s_proof, x4_s_proof_len);
1244         xfree(x4_s_proof);
1245
1246         JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1247
1248         /* Send key confirmation proof */
1249         packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1250         packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1251         packet_send();
1252
1253         /* Expect confirmation from peer */
1254         dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1255             input_userauth_jpake_server_confirm);
1256 }
1257
1258 /* ARGSUSED */
1259 void
1260 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1261 {
1262         Authctxt *authctxt = ctxt;
1263         struct jpake_ctx *pctx = authctxt->methoddata;
1264
1265         /* Disable this message */
1266         dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1267
1268         pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1269         packet_check_eom();
1270
1271         JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1272
1273         /* Verify expected confirmation hash */
1274         if (jpake_check_confirm(pctx->k,
1275             pctx->server_id, pctx->server_id_len,
1276             session_id2, session_id2_len,
1277             pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1278                 debug("%s: %s success", __func__, authctxt->method->name);
1279         else {
1280                 debug("%s: confirmation mismatch", __func__);
1281                 /* XXX stash this so if auth succeeds then we can warn/kill */
1282         }
1283
1284         userauth_jpake_cleanup(authctxt);
1285 }
1286 #endif /* JPAKE */
1287
1288 static int
1289 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1290     u_char *data, u_int datalen)
1291 {
1292         Key *prv;
1293         int ret;
1294
1295         /* the agent supports this key */
1296         if (id->ac)
1297                 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1298                     data, datalen));
1299         /*
1300          * we have already loaded the private key or
1301          * the private key is stored in external hardware
1302          */
1303         if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1304                 return (key_sign(id->key, sigp, lenp, data, datalen));
1305         /* load the private key from the file */
1306         if ((prv = load_identity_file(id->filename)) == NULL)
1307                 return (-1);
1308         ret = key_sign(prv, sigp, lenp, data, datalen);
1309         key_free(prv);
1310         return (ret);
1311 }
1312
1313 static int
1314 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1315 {
1316         Buffer b;
1317         u_char *blob, *signature;
1318         u_int bloblen, slen;
1319         u_int skip = 0;
1320         int ret = -1;
1321         int have_sig = 1;
1322
1323         debug3("sign_and_send_pubkey");
1324
1325         if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1326                 /* we cannot handle this key */
1327                 debug3("sign_and_send_pubkey: cannot handle key");
1328                 return 0;
1329         }
1330         /* data to be signed */
1331         buffer_init(&b);
1332         if (datafellows & SSH_OLD_SESSIONID) {
1333                 buffer_append(&b, session_id2, session_id2_len);
1334                 skip = session_id2_len;
1335         } else {
1336                 buffer_put_string(&b, session_id2, session_id2_len);
1337                 skip = buffer_len(&b);
1338         }
1339         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1340         buffer_put_cstring(&b, authctxt->server_user);
1341         buffer_put_cstring(&b,
1342             datafellows & SSH_BUG_PKSERVICE ?
1343             "ssh-userauth" :
1344             authctxt->service);
1345         if (datafellows & SSH_BUG_PKAUTH) {
1346                 buffer_put_char(&b, have_sig);
1347         } else {
1348                 buffer_put_cstring(&b, authctxt->method->name);
1349                 buffer_put_char(&b, have_sig);
1350                 buffer_put_cstring(&b, key_ssh_name(id->key));
1351         }
1352         buffer_put_string(&b, blob, bloblen);
1353
1354         /* generate signature */
1355         ret = identity_sign(id, &signature, &slen,
1356             buffer_ptr(&b), buffer_len(&b));
1357         if (ret == -1) {
1358                 xfree(blob);
1359                 buffer_free(&b);
1360                 return 0;
1361         }
1362 #ifdef DEBUG_PK
1363         buffer_dump(&b);
1364 #endif
1365         if (datafellows & SSH_BUG_PKSERVICE) {
1366                 buffer_clear(&b);
1367                 buffer_append(&b, session_id2, session_id2_len);
1368                 skip = session_id2_len;
1369                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1370                 buffer_put_cstring(&b, authctxt->server_user);
1371                 buffer_put_cstring(&b, authctxt->service);
1372                 buffer_put_cstring(&b, authctxt->method->name);
1373                 buffer_put_char(&b, have_sig);
1374                 if (!(datafellows & SSH_BUG_PKAUTH))
1375                         buffer_put_cstring(&b, key_ssh_name(id->key));
1376                 buffer_put_string(&b, blob, bloblen);
1377         }
1378         xfree(blob);
1379
1380         /* append signature */
1381         buffer_put_string(&b, signature, slen);
1382         xfree(signature);
1383
1384         /* skip session id and packet type */
1385         if (buffer_len(&b) < skip + 1)
1386                 fatal("userauth_pubkey: internal error");
1387         buffer_consume(&b, skip + 1);
1388
1389         /* put remaining data from buffer into packet */
1390         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1391         packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1392         buffer_free(&b);
1393         packet_send();
1394
1395         return 1;
1396 }
1397
1398 static int
1399 send_pubkey_test(Authctxt *authctxt, Identity *id)
1400 {
1401         u_char *blob;
1402         u_int bloblen, have_sig = 0;
1403
1404         debug3("send_pubkey_test");
1405
1406         if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1407                 /* we cannot handle this key */
1408                 debug3("send_pubkey_test: cannot handle key");
1409                 return 0;
1410         }
1411         /* register callback for USERAUTH_PK_OK message */
1412         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1413
1414         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1415         packet_put_cstring(authctxt->server_user);
1416         packet_put_cstring(authctxt->service);
1417         packet_put_cstring(authctxt->method->name);
1418         packet_put_char(have_sig);
1419         if (!(datafellows & SSH_BUG_PKAUTH))
1420                 packet_put_cstring(key_ssh_name(id->key));
1421         packet_put_string(blob, bloblen);
1422         xfree(blob);
1423         packet_send();
1424         return 1;
1425 }
1426
1427 static Key *
1428 load_identity_file(char *filename)
1429 {
1430         Key *private;
1431         char prompt[300], *passphrase;
1432         int perm_ok, quit, i;
1433         struct stat st;
1434
1435         if (stat(filename, &st) < 0) {
1436                 debug3("no such identity: %s", filename);
1437                 return NULL;
1438         }
1439         private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1440         if (!perm_ok)
1441                 return NULL;
1442         if (private == NULL) {
1443                 if (options.batch_mode)
1444                         return NULL;
1445                 snprintf(prompt, sizeof prompt,
1446                     "Enter passphrase for key '%.100s': ", filename);
1447                 for (i = 0; i < options.number_of_password_prompts; i++) {
1448                         passphrase = read_passphrase(prompt, 0);
1449                         if (strcmp(passphrase, "") != 0) {
1450                                 private = key_load_private_type(KEY_UNSPEC,
1451                                     filename, passphrase, NULL, NULL);
1452                                 quit = 0;
1453                         } else {
1454                                 debug2("no passphrase given, try next key");
1455                                 quit = 1;
1456                         }
1457                         memset(passphrase, 0, strlen(passphrase));
1458                         xfree(passphrase);
1459                         if (private != NULL || quit)
1460                                 break;
1461                         debug2("bad passphrase given, try again...");
1462                 }
1463         }
1464         return private;
1465 }
1466
1467 /*
1468  * try keys in the following order:
1469  *      1. agent keys that are found in the config file
1470  *      2. other agent keys
1471  *      3. keys that are only listed in the config file
1472  */
1473 static void
1474 pubkey_prepare(Authctxt *authctxt)
1475 {
1476         Identity *id;
1477         Idlist agent, files, *preferred;
1478         Key *key;
1479         AuthenticationConnection *ac;
1480         char *comment;
1481         int i, found;
1482
1483         TAILQ_INIT(&agent);     /* keys from the agent */
1484         TAILQ_INIT(&files);     /* keys from the config file */
1485         preferred = &authctxt->keys;
1486         TAILQ_INIT(preferred);  /* preferred order of keys */
1487
1488         /* list of keys stored in the filesystem */
1489         for (i = 0; i < options.num_identity_files; i++) {
1490                 key = options.identity_keys[i];
1491                 if (key && key->type == KEY_RSA1)
1492                         continue;
1493                 options.identity_keys[i] = NULL;
1494                 id = xcalloc(1, sizeof(*id));
1495                 id->key = key;
1496                 id->filename = xstrdup(options.identity_files[i]);
1497                 TAILQ_INSERT_TAIL(&files, id, next);
1498         }
1499         /* list of keys supported by the agent */
1500         if ((ac = ssh_get_authentication_connection())) {
1501                 for (key = ssh_get_first_identity(ac, &comment, 2);
1502                     key != NULL;
1503                     key = ssh_get_next_identity(ac, &comment, 2)) {
1504                         found = 0;
1505                         TAILQ_FOREACH(id, &files, next) {
1506                                 /* agent keys from the config file are preferred */
1507                                 if (key_equal(key, id->key)) {
1508                                         key_free(key);
1509                                         xfree(comment);
1510                                         TAILQ_REMOVE(&files, id, next);
1511                                         TAILQ_INSERT_TAIL(preferred, id, next);
1512                                         id->ac = ac;
1513                                         found = 1;
1514                                         break;
1515                                 }
1516                         }
1517                         if (!found && !options.identities_only) {
1518                                 id = xcalloc(1, sizeof(*id));
1519                                 id->key = key;
1520                                 id->filename = comment;
1521                                 id->ac = ac;
1522                                 TAILQ_INSERT_TAIL(&agent, id, next);
1523                         }
1524                 }
1525                 /* append remaining agent keys */
1526                 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1527                         TAILQ_REMOVE(&agent, id, next);
1528                         TAILQ_INSERT_TAIL(preferred, id, next);
1529                 }
1530                 authctxt->agent = ac;
1531         }
1532         /* append remaining keys from the config file */
1533         for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1534                 TAILQ_REMOVE(&files, id, next);
1535                 TAILQ_INSERT_TAIL(preferred, id, next);
1536         }
1537         TAILQ_FOREACH(id, preferred, next) {
1538                 debug2("key: %s (%p)", id->filename, id->key);
1539         }
1540 }
1541
1542 static void
1543 pubkey_cleanup(Authctxt *authctxt)
1544 {
1545         Identity *id;
1546
1547         if (authctxt->agent != NULL)
1548                 ssh_close_authentication_connection(authctxt->agent);
1549         for (id = TAILQ_FIRST(&authctxt->keys); id;
1550             id = TAILQ_FIRST(&authctxt->keys)) {
1551                 TAILQ_REMOVE(&authctxt->keys, id, next);
1552                 if (id->key)
1553                         key_free(id->key);
1554                 if (id->filename)
1555                         xfree(id->filename);
1556                 xfree(id);
1557         }
1558 }
1559
1560 int
1561 userauth_pubkey(Authctxt *authctxt)
1562 {
1563         Identity *id;
1564         int sent = 0;
1565
1566         while ((id = TAILQ_FIRST(&authctxt->keys))) {
1567                 if (id->tried++)
1568                         return (0);
1569                 /* move key to the end of the queue */
1570                 TAILQ_REMOVE(&authctxt->keys, id, next);
1571                 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1572                 /*
1573                  * send a test message if we have the public key. for
1574                  * encrypted keys we cannot do this and have to load the
1575                  * private key instead
1576                  */
1577                 if (id->key && id->key->type != KEY_RSA1) {
1578                         debug("Offering public key: %s", id->filename);
1579                         sent = send_pubkey_test(authctxt, id);
1580                 } else if (id->key == NULL) {
1581                         debug("Trying private key: %s", id->filename);
1582                         id->key = load_identity_file(id->filename);
1583                         if (id->key != NULL) {
1584                                 id->isprivate = 1;
1585                                 sent = sign_and_send_pubkey(authctxt, id);
1586                                 key_free(id->key);
1587                                 id->key = NULL;
1588                         }
1589                 }
1590                 if (sent)
1591                         return (sent);
1592         }
1593         return (0);
1594 }
1595
1596 /*
1597  * Send userauth request message specifying keyboard-interactive method.
1598  */
1599 int
1600 userauth_kbdint(Authctxt *authctxt)
1601 {
1602         static int attempt = 0;
1603
1604         if (attempt++ >= options.number_of_password_prompts)
1605                 return 0;
1606         /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1607         if (attempt > 1 && !authctxt->info_req_seen) {
1608                 debug3("userauth_kbdint: disable: no info_req_seen");
1609                 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1610                 return 0;
1611         }
1612
1613         debug2("userauth_kbdint");
1614         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1615         packet_put_cstring(authctxt->server_user);
1616         packet_put_cstring(authctxt->service);
1617         packet_put_cstring(authctxt->method->name);
1618         packet_put_cstring("");                                 /* lang */
1619         packet_put_cstring(options.kbd_interactive_devices ?
1620             options.kbd_interactive_devices : "");
1621         packet_send();
1622
1623         dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1624         return 1;
1625 }
1626
1627 /*
1628  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1629  */
1630 void
1631 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1632 {
1633         Authctxt *authctxt = ctxt;
1634         char *name, *inst, *lang, *prompt, *response;
1635         u_int num_prompts, i;
1636         int echo = 0;
1637
1638         debug2("input_userauth_info_req");
1639
1640         if (authctxt == NULL)
1641                 fatal("input_userauth_info_req: no authentication context");
1642
1643         authctxt->info_req_seen = 1;
1644
1645         name = packet_get_string(NULL);
1646         inst = packet_get_string(NULL);
1647         lang = packet_get_string(NULL);
1648         if (strlen(name) > 0)
1649                 logit("%s", name);
1650         if (strlen(inst) > 0)
1651                 logit("%s", inst);
1652         xfree(name);
1653         xfree(inst);
1654         xfree(lang);
1655
1656         num_prompts = packet_get_int();
1657         /*
1658          * Begin to build info response packet based on prompts requested.
1659          * We commit to providing the correct number of responses, so if
1660          * further on we run into a problem that prevents this, we have to
1661          * be sure and clean this up and send a correct error response.
1662          */
1663         packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1664         packet_put_int(num_prompts);
1665
1666         debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1667         for (i = 0; i < num_prompts; i++) {
1668                 prompt = packet_get_string(NULL);
1669                 echo = packet_get_char();
1670
1671                 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1672
1673                 packet_put_cstring(response);
1674                 memset(response, 0, strlen(response));
1675                 xfree(response);
1676                 xfree(prompt);
1677         }
1678         packet_check_eom(); /* done with parsing incoming message. */
1679
1680         packet_add_padding(64);
1681         packet_send();
1682 }
1683
1684 static int
1685 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1686     u_char *data, u_int datalen)
1687 {
1688         Buffer b;
1689         struct stat st;
1690         pid_t pid;
1691         int to[2], from[2], status, version = 2;
1692
1693         debug2("ssh_keysign called");
1694
1695         if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1696                 error("ssh_keysign: no installed: %s", strerror(errno));
1697                 return -1;
1698         }
1699         if (fflush(stdout) != 0)
1700                 error("ssh_keysign: fflush: %s", strerror(errno));
1701         if (pipe(to) < 0) {
1702                 error("ssh_keysign: pipe: %s", strerror(errno));
1703                 return -1;
1704         }
1705         if (pipe(from) < 0) {
1706                 error("ssh_keysign: pipe: %s", strerror(errno));
1707                 return -1;
1708         }
1709         if ((pid = fork()) < 0) {
1710                 error("ssh_keysign: fork: %s", strerror(errno));
1711                 return -1;
1712         }
1713         if (pid == 0) {
1714                 permanently_drop_suid(getuid());
1715                 close(from[0]);
1716                 if (dup2(from[1], STDOUT_FILENO) < 0)
1717                         fatal("ssh_keysign: dup2: %s", strerror(errno));
1718                 close(to[1]);
1719                 if (dup2(to[0], STDIN_FILENO) < 0)
1720                         fatal("ssh_keysign: dup2: %s", strerror(errno));
1721                 close(from[1]);
1722                 close(to[0]);
1723                 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1724                 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1725                     strerror(errno));
1726         }
1727         close(from[1]);
1728         close(to[0]);
1729
1730         buffer_init(&b);
1731         buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1732         buffer_put_string(&b, data, datalen);
1733         if (ssh_msg_send(to[1], version, &b) == -1)
1734                 fatal("ssh_keysign: couldn't send request");
1735
1736         if (ssh_msg_recv(from[0], &b) < 0) {
1737                 error("ssh_keysign: no reply");
1738                 buffer_free(&b);
1739                 return -1;
1740         }
1741         close(from[0]);
1742         close(to[1]);
1743
1744         while (waitpid(pid, &status, 0) < 0)
1745                 if (errno != EINTR)
1746                         break;
1747
1748         if (buffer_get_char(&b) != version) {
1749                 error("ssh_keysign: bad version");
1750                 buffer_free(&b);
1751                 return -1;
1752         }
1753         *sigp = buffer_get_string(&b, lenp);
1754         buffer_free(&b);
1755
1756         return 0;
1757 }
1758
1759 int
1760 userauth_hostbased(Authctxt *authctxt)
1761 {
1762         Key *private = NULL;
1763         Sensitive *sensitive = authctxt->sensitive;
1764         Buffer b;
1765         u_char *signature, *blob;
1766         char *chost, *pkalg, *p, myname[NI_MAXHOST];
1767         const char *service;
1768         u_int blen, slen;
1769         int ok, i, len, found = 0;
1770
1771         /* check for a useful key */
1772         for (i = 0; i < sensitive->nkeys; i++) {
1773                 private = sensitive->keys[i];
1774                 if (private && private->type != KEY_RSA1) {
1775                         found = 1;
1776                         /* we take and free the key */
1777                         sensitive->keys[i] = NULL;
1778                         break;
1779                 }
1780         }
1781         if (!found) {
1782                 debug("No more client hostkeys for hostbased authentication.");
1783                 return 0;
1784         }
1785         if (key_to_blob(private, &blob, &blen) == 0) {
1786                 key_free(private);
1787                 return 0;
1788         }
1789         /* figure out a name for the client host */
1790         p = NULL;
1791         if (packet_connection_is_on_socket())
1792                 p = get_local_name(packet_get_connection_in());
1793         if (p == NULL) {
1794                 if (gethostname(myname, sizeof(myname)) == -1) {
1795                         verbose("userauth_hostbased: gethostname: %s", 
1796                             strerror(errno));
1797                 } else
1798                         p = xstrdup(myname);
1799         }
1800         if (p == NULL) {
1801                 error("userauth_hostbased: cannot get local ipaddr/name");
1802                 key_free(private);
1803                 xfree(blob);
1804                 return 0;
1805         }
1806         len = strlen(p) + 2;
1807         xasprintf(&chost, "%s.", p);
1808         debug2("userauth_hostbased: chost %s", chost);
1809         xfree(p);
1810
1811         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1812             authctxt->service;
1813         pkalg = xstrdup(key_ssh_name(private));
1814         buffer_init(&b);
1815         /* construct data */
1816         buffer_put_string(&b, session_id2, session_id2_len);
1817         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1818         buffer_put_cstring(&b, authctxt->server_user);
1819         buffer_put_cstring(&b, service);
1820         buffer_put_cstring(&b, authctxt->method->name);
1821         buffer_put_cstring(&b, pkalg);
1822         buffer_put_string(&b, blob, blen);
1823         buffer_put_cstring(&b, chost);
1824         buffer_put_cstring(&b, authctxt->local_user);
1825 #ifdef DEBUG_PK
1826         buffer_dump(&b);
1827 #endif
1828         if (sensitive->external_keysign)
1829                 ok = ssh_keysign(private, &signature, &slen,
1830                     buffer_ptr(&b), buffer_len(&b));
1831         else
1832                 ok = key_sign(private, &signature, &slen,
1833                     buffer_ptr(&b), buffer_len(&b));
1834         key_free(private);
1835         buffer_free(&b);
1836         if (ok != 0) {
1837                 error("key_sign failed");
1838                 xfree(chost);
1839                 xfree(pkalg);
1840                 xfree(blob);
1841                 return 0;
1842         }
1843         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1844         packet_put_cstring(authctxt->server_user);
1845         packet_put_cstring(authctxt->service);
1846         packet_put_cstring(authctxt->method->name);
1847         packet_put_cstring(pkalg);
1848         packet_put_string(blob, blen);
1849         packet_put_cstring(chost);
1850         packet_put_cstring(authctxt->local_user);
1851         packet_put_string(signature, slen);
1852         memset(signature, 's', slen);
1853         xfree(signature);
1854         xfree(chost);
1855         xfree(pkalg);
1856         xfree(blob);
1857
1858         packet_send();
1859         return 1;
1860 }
1861
1862 #ifdef JPAKE
1863 int
1864 userauth_jpake(Authctxt *authctxt)
1865 {
1866         struct jpake_ctx *pctx;
1867         u_char *x1_proof, *x2_proof;
1868         u_int x1_proof_len, x2_proof_len;
1869         static int attempt = 0; /* XXX share with userauth_password's? */
1870
1871         if (attempt++ >= options.number_of_password_prompts)
1872                 return 0;
1873         if (attempt != 1)
1874                 error("Permission denied, please try again.");
1875
1876         if (authctxt->methoddata != NULL)
1877                 fatal("%s: authctxt->methoddata already set (%p)",
1878                     __func__, authctxt->methoddata);
1879
1880         authctxt->methoddata = pctx = jpake_new();
1881
1882         /*
1883          * Send request immediately, to get the protocol going while
1884          * we do the initial computations.
1885          */
1886         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1887         packet_put_cstring(authctxt->server_user);
1888         packet_put_cstring(authctxt->service);
1889         packet_put_cstring(authctxt->method->name);
1890         packet_send();
1891         packet_write_wait();
1892
1893         jpake_step1(pctx->grp,
1894             &pctx->client_id, &pctx->client_id_len,
1895             &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1896             &x1_proof, &x1_proof_len,
1897             &x2_proof, &x2_proof_len);
1898
1899         JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1900
1901         packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1902         packet_put_string(pctx->client_id, pctx->client_id_len);
1903         packet_put_bignum2(pctx->g_x1);
1904         packet_put_bignum2(pctx->g_x2);
1905         packet_put_string(x1_proof, x1_proof_len);
1906         packet_put_string(x2_proof, x2_proof_len);
1907         packet_send();
1908
1909         bzero(x1_proof, x1_proof_len);
1910         bzero(x2_proof, x2_proof_len);
1911         xfree(x1_proof);
1912         xfree(x2_proof);
1913
1914         /* Expect step 1 packet from peer */
1915         dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1916             input_userauth_jpake_server_step1);
1917
1918         return 1;
1919 }
1920
1921 void
1922 userauth_jpake_cleanup(Authctxt *authctxt)
1923 {
1924         debug3("%s: clean up", __func__);
1925         if (authctxt->methoddata != NULL) {
1926                 jpake_free(authctxt->methoddata);
1927                 authctxt->methoddata = NULL;
1928         }
1929 }
1930 #endif /* JPAKE */
1931
1932 /* find auth method */
1933
1934 /*
1935  * given auth method name, if configurable options permit this method fill
1936  * in auth_ident field and return true, otherwise return false.
1937  */
1938 static int
1939 authmethod_is_enabled(Authmethod *method)
1940 {
1941         if (method == NULL)
1942                 return 0;
1943         /* return false if options indicate this method is disabled */
1944         if  (method->enabled == NULL || *method->enabled == 0)
1945                 return 0;
1946         /* return false if batch mode is enabled but method needs interactive mode */
1947         if  (method->batch_flag != NULL && *method->batch_flag != 0)
1948                 return 0;
1949         return 1;
1950 }
1951
1952 static Authmethod *
1953 authmethod_lookup(const char *name)
1954 {
1955         Authmethod *method = NULL;
1956         if (name != NULL)
1957                 for (method = authmethods; method->name != NULL; method++)
1958                         if (strcmp(name, method->name) == 0)
1959                                 return method;
1960         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1961         return NULL;
1962 }
1963
1964 /* XXX internal state */
1965 static Authmethod *current = NULL;
1966 static char *supported = NULL;
1967 static char *preferred = NULL;
1968
1969 /*
1970  * Given the authentication method list sent by the server, return the
1971  * next method we should try.  If the server initially sends a nil list,
1972  * use a built-in default list.
1973  */
1974 static Authmethod *
1975 authmethod_get(char *authlist)
1976 {
1977         char *name = NULL;
1978         u_int next;
1979
1980         /* Use a suitable default if we're passed a nil list.  */
1981         if (authlist == NULL || strlen(authlist) == 0)
1982                 authlist = options.preferred_authentications;
1983
1984         if (supported == NULL || strcmp(authlist, supported) != 0) {
1985                 debug3("start over, passed a different list %s", authlist);
1986                 if (supported != NULL)
1987                         xfree(supported);
1988                 supported = xstrdup(authlist);
1989                 preferred = options.preferred_authentications;
1990                 debug3("preferred %s", preferred);
1991                 current = NULL;
1992         } else if (current != NULL && authmethod_is_enabled(current))
1993                 return current;
1994
1995         for (;;) {
1996                 if ((name = match_list(preferred, supported, &next)) == NULL) {
1997                         debug("No more authentication methods to try.");
1998                         current = NULL;
1999                         return NULL;
2000                 }
2001                 preferred += next;
2002                 debug3("authmethod_lookup %s", name);
2003                 debug3("remaining preferred: %s", preferred);
2004                 if ((current = authmethod_lookup(name)) != NULL &&
2005                     authmethod_is_enabled(current)) {
2006                         debug3("authmethod_is_enabled %s", name);
2007                         debug("Next authentication method: %s", name);
2008                         return current;
2009                 }
2010         }
2011 }
2012
2013 static char *
2014 authmethods_get(void)
2015 {
2016         Authmethod *method = NULL;
2017         Buffer b;
2018         char *list;
2019
2020         buffer_init(&b);
2021         for (method = authmethods; method->name != NULL; method++) {
2022                 if (authmethod_is_enabled(method)) {
2023                         if (buffer_len(&b) > 0)
2024                                 buffer_append(&b, ",", 1);
2025                         buffer_append(&b, method->name, strlen(method->name));
2026                 }
2027         }
2028         buffer_append(&b, "\0", 1);
2029         list = xstrdup(buffer_ptr(&b));
2030         buffer_free(&b);
2031         return list;
2032 }
2033
This page took 0.301878 seconds and 5 git commands to generate.