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