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