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