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