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