]> andersk Git - gssapi-openssh.git/blob - openssh/sshconnect2.c
openssh-3.6.1p2-gssapi-20030430.diff from Simon
[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(get_canonical_hostname(1));  
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 #ifdef GSSAPI
152         kex->options.gss_deleg_creds=options.gss_deleg_creds;
153 #endif
154         xxx_kex = kex;
155
156         dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
157
158         session_id2 = kex->session_id;
159         session_id2_len = kex->session_id_len;
160
161 #ifdef DEBUG_KEXDH
162         /* send 1st encrypted/maced/compressed message */
163         packet_start(SSH2_MSG_IGNORE);
164         packet_put_cstring("markus");
165         packet_send();
166         packet_write_wait();
167 #endif
168 }
169
170 /*
171  * Authenticate user
172  */
173
174 typedef struct Authctxt Authctxt;
175 typedef struct Authmethod Authmethod;
176
177 typedef int sign_cb_fn(
178     Authctxt *authctxt, Key *key,
179     u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
180
181 struct Authctxt {
182         const char *server_user;
183         const char *local_user;
184         const char *host;
185         const char *service;
186         Authmethod *method;
187         int success;
188         char *authlist;
189         /* pubkey */
190         Key *last_key;
191         sign_cb_fn *last_key_sign;
192         int last_key_hint;
193         AuthenticationConnection *agent;
194         /* hostbased */
195         Sensitive *sensitive;
196         /* kbd-interactive */
197         int info_req_seen;
198         /* generic */
199         void *methoddata;
200 };
201 struct Authmethod {
202         char    *name;          /* string to compare against server's list */
203         int     (*userauth)(Authctxt *authctxt);
204         int     *enabled;       /* flag in option struct that enables method */
205         int     *batch_flag;    /* flag in option struct that disables method */
206 };
207
208 void    input_userauth_success(int, u_int32_t, void *);
209 void    input_userauth_failure(int, u_int32_t, void *);
210 void    input_userauth_banner(int, u_int32_t, void *);
211 void    input_userauth_error(int, u_int32_t, void *);
212 void    input_userauth_info_req(int, u_int32_t, void *);
213 void    input_userauth_pk_ok(int, u_int32_t, void *);
214 void    input_userauth_passwd_changereq(int, u_int32_t, void *);
215
216 int     userauth_none(Authctxt *);
217 int     userauth_pubkey(Authctxt *);
218 int     userauth_passwd(Authctxt *);
219 int     userauth_kbdint(Authctxt *);
220 int     userauth_hostbased(Authctxt *);
221
222 #ifdef GSSAPI
223 int     userauth_external(Authctxt *authctxt);
224 int     userauth_gssapi(Authctxt *authctxt);
225 void    input_gssapi_response(int type, u_int32_t, void *);
226 void    input_gssapi_token(int type, u_int32_t, void *);
227 void    input_gssapi_hash(int type, u_int32_t, void *);
228 void    input_gssapi_error(int, u_int32_t, void *);
229 void    input_gssapi_errtok(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         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,&input_gssapi_errtok);
569         
570         mech++; /* Move along to next candidate */
571
572         return 1;
573 }
574
575 void
576 input_gssapi_response(int type, u_int32_t plen, void *ctxt) 
577 {
578         Authctxt *authctxt = ctxt;
579         Gssctxt *gssctxt;
580         OM_uint32 status,ms;
581         int oidlen;
582         char *oidv;
583         gss_buffer_desc send_tok;
584         
585         if (authctxt == NULL)
586                 fatal("input_gssapi_response: no authentication context");
587         gssctxt = authctxt->methoddata;
588         
589         /* Setup our OID */
590         oidv=packet_get_string(&oidlen);
591         
592         if (datafellows & SSH_BUG_GSSAPI_BER) {
593                 if (!ssh_gssapi_check_oid(gssctxt,oidv,oidlen)) {
594                         fatal("Server returned different OID than expected");
595                 }
596                 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
597         } else {
598                 if(oidv[0]!=0x06 || oidv[1]!=oidlen-2) {
599                         debug("Badly encoded mechanism OID received");
600                         clear_auth_state(authctxt);
601                         userauth(authctxt,NULL);
602                         return;
603                 }
604                 if (!ssh_gssapi_check_oid(gssctxt,oidv+2,oidlen-2)) {
605                         fatal("Server returned different OID than expected");
606                 }
607                 ssh_gssapi_set_oid_data(gssctxt,oidv+2,oidlen-2);
608         }
609                 
610         packet_check_eom();
611         
612         status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
613                                      GSS_C_NO_BUFFER, &send_tok, 
614                                      NULL);
615         if (GSS_ERROR(status)) {
616                 if (send_tok.length>0) {
617                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
618                         packet_put_string(send_tok.value,send_tok.length);
619                         packet_send();
620                         packet_write_wait();
621                 }
622                 /* Start again with next method on list */
623                 debug("Trying to start again");
624                 clear_auth_state(authctxt);
625                 userauth(authctxt,NULL);
626                 return;
627         }
628
629         /* We must have data to send */                                 
630         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
631         packet_put_string(send_tok.value,send_tok.length);
632         packet_send();
633         packet_write_wait();
634         gss_release_buffer(&ms, &send_tok);
635 }
636
637 void
638 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
639 {
640         Authctxt *authctxt = ctxt;
641         Gssctxt *gssctxt;
642         gss_buffer_desc send_tok,recv_tok;
643         OM_uint32 status;
644         u_int slen;
645         
646         if (authctxt == NULL)
647                 fatal("input_gssapi_response: no authentication context");
648         gssctxt = authctxt->methoddata;
649         
650         recv_tok.value=packet_get_string(&slen);
651         recv_tok.length=slen;   /* safe typecast */
652
653         status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
654                                    &recv_tok, &send_tok, NULL);
655
656         packet_check_eom();
657         
658         if (GSS_ERROR(status)) {
659                 if (send_tok.length>0) {
660                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
661                         packet_put_string(send_tok.value,send_tok.length);
662                         packet_send();
663                         packet_write_wait();
664                 }
665                 /* Start again with the next method in the list */
666                 clear_auth_state(authctxt);
667                 userauth(authctxt,NULL);
668                 return;
669         }
670         
671         if (send_tok.length>0) {
672                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
673                 packet_put_string(send_tok.value,send_tok.length);
674                 packet_send();
675                 packet_write_wait();
676         }
677         
678         if (status == GSS_S_COMPLETE) {
679                 /* If that succeeded, send a exchange complete message */
680                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
681                 packet_send();
682                 packet_write_wait();
683         }
684 }
685
686 void
687 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
688 {
689         Authctxt *authctxt = ctxt;
690         Gssctxt *gssctxt;
691         gss_buffer_desc send_tok,recv_tok;
692         OM_uint32 status;
693         
694         if (authctxt == NULL)
695                 fatal("input_gssapi_response: no authentication context");
696         gssctxt = authctxt->methoddata;
697         
698         recv_tok.value=packet_get_string(&recv_tok.length);
699
700         /* Stick it into GSSAPI and see what it says */
701         status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
702                                    &recv_tok, &send_tok, NULL);
703
704         packet_check_eom();
705         
706         /* We can't send a packet to the server */
707
708         /* The draft says that we should wait for the server to fail 
709          * before starting the next authentication. So, we clear the
710          * state, but don't do anything else */
711         clear_auth_state(authctxt);
712         return;
713 }
714
715 void
716 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
717 {
718         OM_uint32 maj,min;
719         char *msg;
720         char *lang;
721         
722         maj=packet_get_int();
723         min=packet_get_int();
724         msg=packet_get_string(NULL);
725         lang=packet_get_string(NULL);
726
727         packet_check_eom();
728         
729         fprintf(stderr, "Server GSSAPI Error:\n%s\n", msg);
730         xfree(msg);
731         xfree(lang);
732 }
733
734 int
735 userauth_external(Authctxt *authctxt)
736 {
737         static int attempt =0;
738         
739         if (attempt++ >= 1)
740                 return 0;
741                                 
742         debug2("userauth_external");
743         packet_start(SSH2_MSG_USERAUTH_REQUEST);
744         packet_put_cstring(authctxt->server_user);
745         packet_put_cstring(authctxt->service);
746         packet_put_cstring(authctxt->method->name);
747         packet_send();
748         packet_write_wait();
749         return 1;
750 }                                                                                                
751 #endif /* GSSAPI */
752
753 int
754 userauth_none(Authctxt *authctxt)
755 {
756         /* initial userauth request */
757         packet_start(SSH2_MSG_USERAUTH_REQUEST);
758         packet_put_cstring(authctxt->server_user);
759         packet_put_cstring(authctxt->service);
760         packet_put_cstring(authctxt->method->name);
761         packet_send();
762         return 1;
763
764 }
765
766 int
767 userauth_passwd(Authctxt *authctxt)
768 {
769         static int attempt = 0;
770         char prompt[150];
771         char *password;
772
773         if (attempt++ >= options.number_of_password_prompts)
774                 return 0;
775
776         if (attempt != 1)
777                 error("Permission denied, please try again.");
778
779         snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
780             authctxt->server_user, authctxt->host);
781         password = read_passphrase(prompt, 0);
782         packet_start(SSH2_MSG_USERAUTH_REQUEST);
783         packet_put_cstring(authctxt->server_user);
784         packet_put_cstring(authctxt->service);
785         packet_put_cstring(authctxt->method->name);
786         packet_put_char(0);
787         packet_put_cstring(password);
788         memset(password, 0, strlen(password));
789         xfree(password);
790         packet_add_padding(64);
791         packet_send();
792
793         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
794             &input_userauth_passwd_changereq);
795
796         return 1;
797 }
798 /*
799  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
800  */
801 void
802 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
803 {
804         Authctxt *authctxt = ctxt;
805         char *info, *lang, *password = NULL, *retype = NULL;
806         char prompt[150];
807
808         debug2("input_userauth_passwd_changereq");
809
810         if (authctxt == NULL)
811                 fatal("input_userauth_passwd_changereq: "
812                     "no authentication context");
813
814         info = packet_get_string(NULL);
815         lang = packet_get_string(NULL);
816         if (strlen(info) > 0)
817                 log("%s", info);
818         xfree(info);
819         xfree(lang);
820         packet_start(SSH2_MSG_USERAUTH_REQUEST);
821         packet_put_cstring(authctxt->server_user);
822         packet_put_cstring(authctxt->service);
823         packet_put_cstring(authctxt->method->name);
824         packet_put_char(1);                     /* additional info */
825         snprintf(prompt, sizeof(prompt),
826             "Enter %.30s@%.128s's old password: ",
827             authctxt->server_user, authctxt->host);
828         password = read_passphrase(prompt, 0);
829         packet_put_cstring(password);
830         memset(password, 0, strlen(password));
831         xfree(password);
832         password = NULL;
833         while (password == NULL) {
834                 snprintf(prompt, sizeof(prompt),
835                     "Enter %.30s@%.128s's new password: ",
836                     authctxt->server_user, authctxt->host);
837                 password = read_passphrase(prompt, RP_ALLOW_EOF);
838                 if (password == NULL) {
839                         /* bail out */
840                         return;
841                 }
842                 snprintf(prompt, sizeof(prompt),
843                     "Retype %.30s@%.128s's new password: ",
844                     authctxt->server_user, authctxt->host);
845                 retype = read_passphrase(prompt, 0);
846                 if (strcmp(password, retype) != 0) {
847                         memset(password, 0, strlen(password));
848                         xfree(password);
849                         log("Mismatch; try again, EOF to quit.");
850                         password = NULL;
851                 }
852                 memset(retype, 0, strlen(retype));
853                 xfree(retype);
854         }
855         packet_put_cstring(password);
856         memset(password, 0, strlen(password));
857         xfree(password);
858         packet_add_padding(64);
859         packet_send();
860
861         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
862             &input_userauth_passwd_changereq);
863 }
864
865 static void
866 clear_auth_state(Authctxt *authctxt)
867 {
868         /* XXX clear authentication state */
869         dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
870 #ifdef GSSAPI
871         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
872         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
873         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
874 #endif
875         
876         if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
877                 debug3("clear_auth_state: key_free %p", authctxt->last_key);
878                 key_free(authctxt->last_key);
879         }
880         authctxt->last_key = NULL;
881         authctxt->last_key_hint = -2;
882         authctxt->last_key_sign = NULL;
883 }
884
885 static int
886 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
887 {
888         Buffer b;
889         u_char *blob, *signature;
890         u_int bloblen, slen;
891         int skip = 0;
892         int ret = -1;
893         int have_sig = 1;
894
895         debug3("sign_and_send_pubkey");
896
897         if (key_to_blob(k, &blob, &bloblen) == 0) {
898                 /* we cannot handle this key */
899                 debug3("sign_and_send_pubkey: cannot handle key");
900                 return 0;
901         }
902         /* data to be signed */
903         buffer_init(&b);
904         if (datafellows & SSH_OLD_SESSIONID) {
905                 buffer_append(&b, session_id2, session_id2_len);
906                 skip = session_id2_len;
907         } else {
908                 buffer_put_string(&b, session_id2, session_id2_len);
909                 skip = buffer_len(&b);
910         }
911         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
912         buffer_put_cstring(&b, authctxt->server_user);
913         buffer_put_cstring(&b,
914             datafellows & SSH_BUG_PKSERVICE ?
915             "ssh-userauth" :
916             authctxt->service);
917         if (datafellows & SSH_BUG_PKAUTH) {
918                 buffer_put_char(&b, have_sig);
919         } else {
920                 buffer_put_cstring(&b, authctxt->method->name);
921                 buffer_put_char(&b, have_sig);
922                 buffer_put_cstring(&b, key_ssh_name(k));
923         }
924         buffer_put_string(&b, blob, bloblen);
925
926         /* generate signature */
927         ret = (*sign_callback)(authctxt, k, &signature, &slen,
928             buffer_ptr(&b), buffer_len(&b));
929         if (ret == -1) {
930                 xfree(blob);
931                 buffer_free(&b);
932                 return 0;
933         }
934 #ifdef DEBUG_PK
935         buffer_dump(&b);
936 #endif
937         if (datafellows & SSH_BUG_PKSERVICE) {
938                 buffer_clear(&b);
939                 buffer_append(&b, session_id2, session_id2_len);
940                 skip = session_id2_len;
941                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
942                 buffer_put_cstring(&b, authctxt->server_user);
943                 buffer_put_cstring(&b, authctxt->service);
944                 buffer_put_cstring(&b, authctxt->method->name);
945                 buffer_put_char(&b, have_sig);
946                 if (!(datafellows & SSH_BUG_PKAUTH))
947                         buffer_put_cstring(&b, key_ssh_name(k));
948                 buffer_put_string(&b, blob, bloblen);
949         }
950         xfree(blob);
951
952         /* append signature */
953         buffer_put_string(&b, signature, slen);
954         xfree(signature);
955
956         /* skip session id and packet type */
957         if (buffer_len(&b) < skip + 1)
958                 fatal("userauth_pubkey: internal error");
959         buffer_consume(&b, skip + 1);
960
961         /* put remaining data from buffer into packet */
962         packet_start(SSH2_MSG_USERAUTH_REQUEST);
963         packet_put_raw(buffer_ptr(&b), buffer_len(&b));
964         buffer_free(&b);
965         packet_send();
966
967         return 1;
968 }
969
970 static int
971 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
972     int hint)
973 {
974         u_char *blob;
975         u_int bloblen, have_sig = 0;
976
977         debug3("send_pubkey_test");
978
979         if (key_to_blob(k, &blob, &bloblen) == 0) {
980                 /* we cannot handle this key */
981                 debug3("send_pubkey_test: cannot handle key");
982                 return 0;
983         }
984         /* register callback for USERAUTH_PK_OK message */
985         authctxt->last_key_sign = sign_callback;
986         authctxt->last_key_hint = hint;
987         authctxt->last_key = k;
988         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
989
990         packet_start(SSH2_MSG_USERAUTH_REQUEST);
991         packet_put_cstring(authctxt->server_user);
992         packet_put_cstring(authctxt->service);
993         packet_put_cstring(authctxt->method->name);
994         packet_put_char(have_sig);
995         if (!(datafellows & SSH_BUG_PKAUTH))
996                 packet_put_cstring(key_ssh_name(k));
997         packet_put_string(blob, bloblen);
998         xfree(blob);
999         packet_send();
1000         return 1;
1001 }
1002
1003 static Key *
1004 load_identity_file(char *filename)
1005 {
1006         Key *private;
1007         char prompt[300], *passphrase;
1008         int quit, i;
1009         struct stat st;
1010
1011         if (stat(filename, &st) < 0) {
1012                 debug3("no such identity: %s", filename);
1013                 return NULL;
1014         }
1015         private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1016         if (private == NULL) {
1017                 if (options.batch_mode)
1018                         return NULL;
1019                 snprintf(prompt, sizeof prompt,
1020                     "Enter passphrase for key '%.100s': ", filename);
1021                 for (i = 0; i < options.number_of_password_prompts; i++) {
1022                         passphrase = read_passphrase(prompt, 0);
1023                         if (strcmp(passphrase, "") != 0) {
1024                                 private = key_load_private_type(KEY_UNSPEC, filename,
1025                                     passphrase, NULL);
1026                                 quit = 0;
1027                         } else {
1028                                 debug2("no passphrase given, try next key");
1029                                 quit = 1;
1030                         }
1031                         memset(passphrase, 0, strlen(passphrase));
1032                         xfree(passphrase);
1033                         if (private != NULL || quit)
1034                                 break;
1035                         debug2("bad passphrase given, try again...");
1036                 }
1037         }
1038         return private;
1039 }
1040
1041 static int
1042 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1043     u_char *data, u_int datalen)
1044 {
1045         Key *private;
1046         int idx, ret;
1047
1048         idx = authctxt->last_key_hint;
1049         if (idx < 0)
1050                 return -1;
1051
1052         /* private key is stored in external hardware */
1053         if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
1054                 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
1055
1056         private = load_identity_file(options.identity_files[idx]);
1057         if (private == NULL)
1058                 return -1;
1059         ret = key_sign(private, sigp, lenp, data, datalen);
1060         key_free(private);
1061         return ret;
1062 }
1063
1064 static int
1065 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1066     u_char *data, u_int datalen)
1067 {
1068         return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1069 }
1070
1071 static int
1072 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1073     u_char *data, u_int datalen)
1074 {
1075         return key_sign(key, sigp, lenp, data, datalen);
1076 }
1077
1078 static int
1079 userauth_pubkey_agent(Authctxt *authctxt)
1080 {
1081         static int called = 0;
1082         int ret = 0;
1083         char *comment;
1084         Key *k;
1085
1086         if (called == 0) {
1087                 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1088                         debug2("userauth_pubkey_agent: no keys at all");
1089                 called = 1;
1090         }
1091         k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1092         if (k == NULL) {
1093                 debug2("userauth_pubkey_agent: no more keys");
1094         } else {
1095                 debug("Offering agent key: %s", comment);
1096                 xfree(comment);
1097                 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1098                 if (ret == 0)
1099                         key_free(k);
1100         }
1101         if (ret == 0)
1102                 debug2("userauth_pubkey_agent: no message sent");
1103         return ret;
1104 }
1105
1106 int
1107 userauth_pubkey(Authctxt *authctxt)
1108 {
1109         static int idx = 0;
1110         int sent = 0;
1111         Key *key;
1112         char *filename;
1113
1114         if (authctxt->agent != NULL) {
1115                 do {
1116                         sent = userauth_pubkey_agent(authctxt);
1117                 } while (!sent && authctxt->agent->howmany > 0);
1118         }
1119         while (!sent && idx < options.num_identity_files) {
1120                 key = options.identity_keys[idx];
1121                 filename = options.identity_files[idx];
1122                 if (key == NULL) {
1123                         debug("Trying private key: %s", filename);
1124                         key = load_identity_file(filename);
1125                         if (key != NULL) {
1126                                 sent = sign_and_send_pubkey(authctxt, key,
1127                                     key_sign_cb);
1128                                 key_free(key);
1129                         }
1130                 } else if (key->type != KEY_RSA1) {
1131                         debug("Offering public key: %s", filename);
1132                         sent = send_pubkey_test(authctxt, key,
1133                             identity_sign_cb, idx);
1134                 }
1135                 idx++;
1136         }
1137         return sent;
1138 }
1139
1140 /*
1141  * Send userauth request message specifying keyboard-interactive method.
1142  */
1143 int
1144 userauth_kbdint(Authctxt *authctxt)
1145 {
1146         static int attempt = 0;
1147
1148         if (attempt++ >= options.number_of_password_prompts)
1149                 return 0;
1150         /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1151         if (attempt > 1 && !authctxt->info_req_seen) {
1152                 debug3("userauth_kbdint: disable: no info_req_seen");
1153                 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1154                 return 0;
1155         }
1156
1157         debug2("userauth_kbdint");
1158         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1159         packet_put_cstring(authctxt->server_user);
1160         packet_put_cstring(authctxt->service);
1161         packet_put_cstring(authctxt->method->name);
1162         packet_put_cstring("");                                 /* lang */
1163         packet_put_cstring(options.kbd_interactive_devices ?
1164             options.kbd_interactive_devices : "");
1165         packet_send();
1166
1167         dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1168         return 1;
1169 }
1170
1171 /*
1172  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1173  */
1174 void
1175 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1176 {
1177         Authctxt *authctxt = ctxt;
1178         char *name, *inst, *lang, *prompt, *response;
1179         u_int num_prompts, i;
1180         int echo = 0;
1181
1182         debug2("input_userauth_info_req");
1183
1184         if (authctxt == NULL)
1185                 fatal("input_userauth_info_req: no authentication context");
1186
1187         authctxt->info_req_seen = 1;
1188
1189         name = packet_get_string(NULL);
1190         inst = packet_get_string(NULL);
1191         lang = packet_get_string(NULL);
1192         if (strlen(name) > 0)
1193                 log("%s", name);
1194         if (strlen(inst) > 0)
1195                 log("%s", inst);
1196         xfree(name);
1197         xfree(inst);
1198         xfree(lang);
1199
1200         num_prompts = packet_get_int();
1201         /*
1202          * Begin to build info response packet based on prompts requested.
1203          * We commit to providing the correct number of responses, so if
1204          * further on we run into a problem that prevents this, we have to
1205          * be sure and clean this up and send a correct error response.
1206          */
1207         packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1208         packet_put_int(num_prompts);
1209
1210         debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1211         for (i = 0; i < num_prompts; i++) {
1212                 prompt = packet_get_string(NULL);
1213                 echo = packet_get_char();
1214
1215                 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1216
1217                 packet_put_cstring(response);
1218                 memset(response, 0, strlen(response));
1219                 xfree(response);
1220                 xfree(prompt);
1221         }
1222         packet_check_eom(); /* done with parsing incoming message. */
1223
1224         packet_add_padding(64);
1225         packet_send();
1226 }
1227
1228 static int
1229 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1230     u_char *data, u_int datalen)
1231 {
1232         Buffer b;
1233         struct stat st;
1234         pid_t pid;
1235         int to[2], from[2], status, version = 2;
1236
1237         debug2("ssh_keysign called");
1238
1239         if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1240                 error("ssh_keysign: no installed: %s", strerror(errno));
1241                 return -1;
1242         }
1243         if (fflush(stdout) != 0)
1244                 error("ssh_keysign: fflush: %s", strerror(errno));
1245         if (pipe(to) < 0) {
1246                 error("ssh_keysign: pipe: %s", strerror(errno));
1247                 return -1;
1248         }
1249         if (pipe(from) < 0) {
1250                 error("ssh_keysign: pipe: %s", strerror(errno));
1251                 return -1;
1252         }
1253         if ((pid = fork()) < 0) {
1254                 error("ssh_keysign: fork: %s", strerror(errno));
1255                 return -1;
1256         }
1257         if (pid == 0) {
1258                 seteuid(getuid());
1259                 setuid(getuid());
1260                 close(from[0]);
1261                 if (dup2(from[1], STDOUT_FILENO) < 0)
1262                         fatal("ssh_keysign: dup2: %s", strerror(errno));
1263                 close(to[1]);
1264                 if (dup2(to[0], STDIN_FILENO) < 0)
1265                         fatal("ssh_keysign: dup2: %s", strerror(errno));
1266                 close(from[1]);
1267                 close(to[0]);
1268                 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1269                 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1270                     strerror(errno));
1271         }
1272         close(from[1]);
1273         close(to[0]);
1274
1275         buffer_init(&b);
1276         buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1277         buffer_put_string(&b, data, datalen);
1278         ssh_msg_send(to[1], version, &b);
1279
1280         if (ssh_msg_recv(from[0], &b) < 0) {
1281                 error("ssh_keysign: no reply");
1282                 buffer_clear(&b);
1283                 return -1;
1284         }
1285         close(from[0]);
1286         close(to[1]);
1287
1288         while (waitpid(pid, &status, 0) < 0)
1289                 if (errno != EINTR)
1290                         break;
1291
1292         if (buffer_get_char(&b) != version) {
1293                 error("ssh_keysign: bad version");
1294                 buffer_clear(&b);
1295                 return -1;
1296         }
1297         *sigp = buffer_get_string(&b, lenp);
1298         buffer_clear(&b);
1299
1300         return 0;
1301 }
1302
1303 int
1304 userauth_hostbased(Authctxt *authctxt)
1305 {
1306         Key *private = NULL;
1307         Sensitive *sensitive = authctxt->sensitive;
1308         Buffer b;
1309         u_char *signature, *blob;
1310         char *chost, *pkalg, *p;
1311         const char *service;
1312         u_int blen, slen;
1313         int ok, i, len, found = 0;
1314
1315         /* check for a useful key */
1316         for (i = 0; i < sensitive->nkeys; i++) {
1317                 private = sensitive->keys[i];
1318                 if (private && private->type != KEY_RSA1) {
1319                         found = 1;
1320                         /* we take and free the key */
1321                         sensitive->keys[i] = NULL;
1322                         break;
1323                 }
1324         }
1325         if (!found) {
1326                 debug("No more client hostkeys for hostbased authentication.");
1327                 return 0;
1328         }
1329         if (key_to_blob(private, &blob, &blen) == 0) {
1330                 key_free(private);
1331                 return 0;
1332         }
1333         /* figure out a name for the client host */
1334         p = get_local_name(packet_get_connection_in());
1335         if (p == NULL) {
1336                 error("userauth_hostbased: cannot get local ipaddr/name");
1337                 key_free(private);
1338                 return 0;
1339         }
1340         len = strlen(p) + 2;
1341         chost = xmalloc(len);
1342         strlcpy(chost, p, len);
1343         strlcat(chost, ".", len);
1344         debug2("userauth_hostbased: chost %s", chost);
1345         xfree(p);
1346
1347         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1348             authctxt->service;
1349         pkalg = xstrdup(key_ssh_name(private));
1350         buffer_init(&b);
1351         /* construct data */
1352         buffer_put_string(&b, session_id2, session_id2_len);
1353         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1354         buffer_put_cstring(&b, authctxt->server_user);
1355         buffer_put_cstring(&b, service);
1356         buffer_put_cstring(&b, authctxt->method->name);
1357         buffer_put_cstring(&b, pkalg);
1358         buffer_put_string(&b, blob, blen);
1359         buffer_put_cstring(&b, chost);
1360         buffer_put_cstring(&b, authctxt->local_user);
1361 #ifdef DEBUG_PK
1362         buffer_dump(&b);
1363 #endif
1364         if (sensitive->external_keysign)
1365                 ok = ssh_keysign(private, &signature, &slen,
1366                     buffer_ptr(&b), buffer_len(&b));
1367         else
1368                 ok = key_sign(private, &signature, &slen,
1369                     buffer_ptr(&b), buffer_len(&b));
1370         key_free(private);
1371         buffer_free(&b);
1372         if (ok != 0) {
1373                 error("key_sign failed");
1374                 xfree(chost);
1375                 xfree(pkalg);
1376                 return 0;
1377         }
1378         packet_start(SSH2_MSG_USERAUTH_REQUEST);
1379         packet_put_cstring(authctxt->server_user);
1380         packet_put_cstring(authctxt->service);
1381         packet_put_cstring(authctxt->method->name);
1382         packet_put_cstring(pkalg);
1383         packet_put_string(blob, blen);
1384         packet_put_cstring(chost);
1385         packet_put_cstring(authctxt->local_user);
1386         packet_put_string(signature, slen);
1387         memset(signature, 's', slen);
1388         xfree(signature);
1389         xfree(chost);
1390         xfree(pkalg);
1391
1392         packet_send();
1393         return 1;
1394 }
1395
1396 /* find auth method */
1397
1398 /*
1399  * given auth method name, if configurable options permit this method fill
1400  * in auth_ident field and return true, otherwise return false.
1401  */
1402 static int
1403 authmethod_is_enabled(Authmethod *method)
1404 {
1405         if (method == NULL)
1406                 return 0;
1407         /* return false if options indicate this method is disabled */
1408         if  (method->enabled == NULL || *method->enabled == 0)
1409                 return 0;
1410         /* return false if batch mode is enabled but method needs interactive mode */
1411         if  (method->batch_flag != NULL && *method->batch_flag != 0)
1412                 return 0;
1413         return 1;
1414 }
1415
1416 static Authmethod *
1417 authmethod_lookup(const char *name)
1418 {
1419         Authmethod *method = NULL;
1420         if (name != NULL)
1421                 for (method = authmethods; method->name != NULL; method++)
1422                         if (strcmp(name, method->name) == 0)
1423                                 return method;
1424         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1425         return NULL;
1426 }
1427
1428 /* XXX internal state */
1429 static Authmethod *current = NULL;
1430 static char *supported = NULL;
1431 static char *preferred = NULL;
1432
1433 /*
1434  * Given the authentication method list sent by the server, return the
1435  * next method we should try.  If the server initially sends a nil list,
1436  * use a built-in default list.
1437  */
1438 static Authmethod *
1439 authmethod_get(char *authlist)
1440 {
1441         char *name = NULL;
1442         u_int next;
1443
1444         /* Use a suitable default if we're passed a nil list.  */
1445         if (authlist == NULL || strlen(authlist) == 0)
1446                 authlist = options.preferred_authentications;
1447
1448         if (supported == NULL || strcmp(authlist, supported) != 0) {
1449                 debug3("start over, passed a different list %s", authlist);
1450                 if (supported != NULL)
1451                         xfree(supported);
1452                 supported = xstrdup(authlist);
1453                 preferred = options.preferred_authentications;
1454                 debug3("preferred %s", preferred);
1455                 current = NULL;
1456         } else if (current != NULL && authmethod_is_enabled(current))
1457                 return current;
1458
1459         for (;;) {
1460                 if ((name = match_list(preferred, supported, &next)) == NULL) {
1461                         debug("No more authentication methods to try.");
1462                         current = NULL;
1463                         return NULL;
1464                 }
1465                 preferred += next;
1466                 debug3("authmethod_lookup %s", name);
1467                 debug3("remaining preferred: %s", preferred);
1468                 if ((current = authmethod_lookup(name)) != NULL &&
1469                     authmethod_is_enabled(current)) {
1470                         debug3("authmethod_is_enabled %s", name);
1471                         debug("Next authentication method: %s", name);
1472                         return current;
1473                 }
1474         }
1475 }
1476
1477 static char *
1478 authmethods_get(void)
1479 {
1480         Authmethod *method = NULL;
1481         Buffer b;
1482         char *list;
1483
1484         buffer_init(&b);
1485         for (method = authmethods; method->name != NULL; method++) {
1486                 if (authmethod_is_enabled(method)) {
1487                         if (buffer_len(&b) > 0)
1488                                 buffer_append(&b, ",", 1);
1489                         buffer_append(&b, method->name, strlen(method->name));
1490                 }
1491         }
1492         buffer_append(&b, "\0", 1);
1493         list = xstrdup(buffer_ptr(&b));
1494         buffer_free(&b);
1495         return list;
1496 }
This page took 0.168309 seconds and 5 git commands to generate.