Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2008 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
  auth_gss.c

  RPCSEC_GSS client routines.
  
  Copyright (c) 2000 The Regents of the University of Michigan.
  All rights reserved.

  Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
  All rights reserved, all wrongs reversed.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. Neither the name of the University nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  $Id: auth_gss.c,v 1.32 2002/01/15 15:43:00 andros Exp $
*/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/hash.h>
#include <sys/kernel.h>
#include <sys/kobj.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/refcount.h>
#include <sys/sx.h>
#include <sys/ucred.h>

#include <rpc/rpc.h>
#include <rpc/rpcsec_gss.h>

#include <kgssapi/krb5/kcrypto.h>

#include "rpcsec_gss_int.h"

static void	rpc_gss_nextverf(AUTH*);
static bool_t	rpc_gss_marshal(AUTH *, uint32_t, XDR *, struct mbuf *);
static bool_t	rpc_gss_init(AUTH *auth, rpc_gss_options_ret_t *options_ret);
static bool_t	rpc_gss_refresh(AUTH *, void *);
static bool_t	rpc_gss_validate(AUTH *, uint32_t, struct opaque_auth *,
    struct mbuf **);
static void	rpc_gss_destroy(AUTH *);
static void	rpc_gss_destroy_context(AUTH *, bool_t);

static struct auth_ops rpc_gss_ops = {
	rpc_gss_nextverf,
	rpc_gss_marshal,
	rpc_gss_validate,
	rpc_gss_refresh,
	rpc_gss_destroy,
};

enum rpcsec_gss_state {
	RPCSEC_GSS_START,
	RPCSEC_GSS_CONTEXT,
	RPCSEC_GSS_ESTABLISHED,
	RPCSEC_GSS_DESTROYING
};

struct rpc_pending_request {
	uint32_t		pr_xid;		/* XID of rpc */
	uint32_t		pr_seq;		/* matching GSS seq */
	LIST_ENTRY(rpc_pending_request) pr_link;
};
LIST_HEAD(rpc_pending_request_list, rpc_pending_request);

struct rpc_gss_data {
	volatile u_int		gd_refs;	/* number of current users */
	struct mtx		gd_lock;
	uint32_t		gd_hash;
	AUTH			*gd_auth;	/* link back to AUTH */
	struct ucred		*gd_ucred;	/* matching local cred */
	char			*gd_principal;	/* server principal name */
	char			*gd_clntprincipal; /* client principal name */
	rpc_gss_options_req_t	gd_options;	/* GSS context options */
	enum rpcsec_gss_state	gd_state;	/* connection state */
	gss_buffer_desc		gd_verf;	/* save GSS_S_COMPLETE
						 * NULL RPC verfier to
						 * process at end of
						 * context negotiation */
	CLIENT			*gd_clnt;	/* client handle */
	gss_OID			gd_mech;	/* mechanism to use */
	gss_qop_t		gd_qop;		/* quality of protection */
	gss_ctx_id_t		gd_ctx;		/* context id */
	struct rpc_gss_cred	gd_cred;	/* client credentials */
	uint32_t		gd_seq;		/* next sequence number */
	u_int			gd_win;		/* sequence window */
	struct rpc_pending_request_list gd_reqs;
	TAILQ_ENTRY(rpc_gss_data) gd_link;
	TAILQ_ENTRY(rpc_gss_data) gd_alllink;
};
TAILQ_HEAD(rpc_gss_data_list, rpc_gss_data);

#define	AUTH_PRIVATE(auth)	((struct rpc_gss_data *)auth->ah_private)

static struct timeval AUTH_TIMEOUT = { 25, 0 };

#define RPC_GSS_HASH_SIZE	11
#define RPC_GSS_MAX		256
static struct rpc_gss_data_list rpc_gss_cache[RPC_GSS_HASH_SIZE];
static struct rpc_gss_data_list rpc_gss_all;
static struct sx rpc_gss_lock;
static int rpc_gss_count;

static AUTH *rpc_gss_seccreate_int(CLIENT *, struct ucred *, const char *,
    const char *, gss_OID, rpc_gss_service_t, u_int, rpc_gss_options_req_t *,
    rpc_gss_options_ret_t *);

static void
rpc_gss_hashinit(void *dummy)
{
	int i;

	for (i = 0; i < RPC_GSS_HASH_SIZE; i++)
		TAILQ_INIT(&rpc_gss_cache[i]);
	TAILQ_INIT(&rpc_gss_all);
	sx_init(&rpc_gss_lock, "rpc_gss_lock");
}
SYSINIT(rpc_gss_hashinit, SI_SUB_KMEM, SI_ORDER_ANY, rpc_gss_hashinit, NULL);

static uint32_t
rpc_gss_hash(const char *principal, gss_OID mech,
    struct ucred *cred, rpc_gss_service_t service)
{
	uint32_t h;

	h = HASHSTEP(HASHINIT, cred->cr_uid);
	h = hash32_str(principal, h);
	h = hash32_buf(mech->elements, mech->length, h);
	h = HASHSTEP(h, (int) service);

	return (h % RPC_GSS_HASH_SIZE);
}

/*
 * Simplified interface to create a security association for the
 * current thread's * ucred.
 */
AUTH *
rpc_gss_secfind(CLIENT *clnt, struct ucred *cred, const char *principal,
    gss_OID mech_oid, rpc_gss_service_t service)
{
	uint32_t		h, th;
	AUTH			*auth;
	struct rpc_gss_data	*gd, *tgd;
	rpc_gss_options_ret_t	options;

	if (rpc_gss_count > RPC_GSS_MAX) {
		while (rpc_gss_count > RPC_GSS_MAX) {
			sx_xlock(&rpc_gss_lock);
			tgd = TAILQ_FIRST(&rpc_gss_all);
			th = tgd->gd_hash;
			TAILQ_REMOVE(&rpc_gss_cache[th], tgd, gd_link);
			TAILQ_REMOVE(&rpc_gss_all, tgd, gd_alllink);
			rpc_gss_count--;
			sx_xunlock(&rpc_gss_lock);
			AUTH_DESTROY(tgd->gd_auth);
		}
	}

	/*
	 * See if we already have an AUTH which matches.
	 */
	h = rpc_gss_hash(principal, mech_oid, cred, service);

again:
	sx_slock(&rpc_gss_lock);
	TAILQ_FOREACH(gd, &rpc_gss_cache[h], gd_link) {
		if (gd->gd_ucred->cr_uid == cred->cr_uid
		    && !strcmp(gd->gd_principal, principal)
		    && gd->gd_mech == mech_oid
		    && gd->gd_cred.gc_svc == service) {
			refcount_acquire(&gd->gd_refs);
			if (sx_try_upgrade(&rpc_gss_lock)) {
				/*
				 * Keep rpc_gss_all LRU sorted.
				 */
				TAILQ_REMOVE(&rpc_gss_all, gd, gd_alllink);
				TAILQ_INSERT_TAIL(&rpc_gss_all, gd,
				    gd_alllink);
				sx_xunlock(&rpc_gss_lock);
			} else {
				sx_sunlock(&rpc_gss_lock);
			}

			/*
			 * If the state != ESTABLISHED, try and initialize
			 * the authenticator again. This will happen if the
			 * user's credentials have expired. It may succeed now,
			 * if they have done a kinit or similar.
			 */
			if (gd->gd_state != RPCSEC_GSS_ESTABLISHED) {
				memset(&options, 0, sizeof (options));
				(void) rpc_gss_init(gd->gd_auth, &options);
			}
			return (gd->gd_auth);
		}
	}
	sx_sunlock(&rpc_gss_lock);

	/*
	 * We missed in the cache - create a new association.
	 */
	auth = rpc_gss_seccreate_int(clnt, cred, NULL, principal, mech_oid,
	    service, GSS_C_QOP_DEFAULT, NULL, NULL);
	if (!auth)
		return (NULL);

	gd = AUTH_PRIVATE(auth);
	gd->gd_hash = h;
	
	sx_xlock(&rpc_gss_lock);
	TAILQ_FOREACH(tgd, &rpc_gss_cache[h], gd_link) {
		if (tgd->gd_ucred->cr_uid == cred->cr_uid
		    && !strcmp(tgd->gd_principal, principal)
		    && tgd->gd_mech == mech_oid
		    && tgd->gd_cred.gc_svc == service) {
			/*
			 * We lost a race to create the AUTH that
			 * matches this cred.
			 */
			sx_xunlock(&rpc_gss_lock);
			AUTH_DESTROY(auth);
			goto again;
		}
	}

	rpc_gss_count++;
	TAILQ_INSERT_TAIL(&rpc_gss_cache[h], gd, gd_link);
	TAILQ_INSERT_TAIL(&rpc_gss_all, gd, gd_alllink);
	refcount_acquire(&gd->gd_refs);	/* one for the cache, one for user */
	sx_xunlock(&rpc_gss_lock);

	return (auth);
}

void
rpc_gss_secpurge(CLIENT *clnt)
{
	uint32_t		h;
	struct rpc_gss_data	*gd, *tgd;

	TAILQ_FOREACH_SAFE(gd, &rpc_gss_all, gd_alllink, tgd) {
		if (gd->gd_clnt == clnt) {
			sx_xlock(&rpc_gss_lock);
			h = gd->gd_hash;
			TAILQ_REMOVE(&rpc_gss_cache[h], gd, gd_link);
			TAILQ_REMOVE(&rpc_gss_all, gd, gd_alllink);
			rpc_gss_count--;
			sx_xunlock(&rpc_gss_lock);
			AUTH_DESTROY(gd->gd_auth);
		}
	}
}

AUTH *
rpc_gss_seccreate(CLIENT *clnt, struct ucred *cred, const char *clnt_principal,
    const char *principal, const char *mechanism, rpc_gss_service_t service,
    const char *qop, rpc_gss_options_req_t *options_req,
    rpc_gss_options_ret_t *options_ret)
{
	gss_OID			oid;
	u_int			qop_num;

	/*
	 * Bail out now if we don't know this mechanism.
	 */
	if (!rpc_gss_mech_to_oid(mechanism, &oid))
		return (NULL);

	if (qop) {
		if (!rpc_gss_qop_to_num(qop, mechanism, &qop_num))
			return (NULL);
	} else {
		qop_num = GSS_C_QOP_DEFAULT;
	}

	return (rpc_gss_seccreate_int(clnt, cred, clnt_principal, principal,
		oid, service, qop_num, options_req, options_ret));
}

void
rpc_gss_refresh_auth(AUTH *auth)
{
	struct rpc_gss_data	*gd;
	rpc_gss_options_ret_t	options;

	gd = AUTH_PRIVATE(auth);
	/*
	 * If the state != ESTABLISHED, try and initialize
	 * the authenticator again. This will happen if the
	 * user's credentials have expired. It may succeed now,
	 * if they have done a kinit or similar.
	 */
	if (gd->gd_state != RPCSEC_GSS_ESTABLISHED) {
		memset(&options, 0, sizeof (options));
		(void) rpc_gss_init(auth, &options);
	}
}

static AUTH *
rpc_gss_seccreate_int(CLIENT *clnt, struct ucred *cred,
    const char *clnt_principal, const char *principal, gss_OID mech_oid,
    rpc_gss_service_t service, u_int qop_num,
    rpc_gss_options_req_t *options_req, rpc_gss_options_ret_t *options_ret)
{
	AUTH			*auth;
	rpc_gss_options_ret_t	options;
	struct rpc_gss_data	*gd;

	/*
	 * If the caller doesn't want the options, point at local
	 * storage to simplify the code below.
	 */
	if (!options_ret)
		options_ret = &options;

	/*
	 * Default service is integrity.
	 */
	if (service == rpc_gss_svc_default)
		service = rpc_gss_svc_integrity;

	memset(options_ret, 0, sizeof(*options_ret));

	rpc_gss_log_debug("in rpc_gss_seccreate()");
	
	memset(&rpc_createerr, 0, sizeof(rpc_createerr));
	
	auth = mem_alloc(sizeof(*auth));
	if (auth == NULL) {
		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
		rpc_createerr.cf_error.re_errno = ENOMEM;
		return (NULL);
	}
	gd = mem_alloc(sizeof(*gd));
	if (gd == NULL) {
		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
		rpc_createerr.cf_error.re_errno = ENOMEM;
		mem_free(auth, sizeof(*auth));
		return (NULL);
	}

	auth->ah_ops = &rpc_gss_ops;
	auth->ah_private = (caddr_t) gd;
	auth->ah_cred.oa_flavor = RPCSEC_GSS;
	
	refcount_init(&gd->gd_refs, 1);
	mtx_init(&gd->gd_lock, "gd->gd_lock", NULL, MTX_DEF);
	gd->gd_auth = auth;
	gd->gd_ucred = crdup(cred);
	gd->gd_principal = strdup(principal, M_RPC);
	if (clnt_principal != NULL)
		gd->gd_clntprincipal = strdup(clnt_principal, M_RPC);
	else
		gd->gd_clntprincipal = NULL;


	if (options_req) {
		gd->gd_options = *options_req;
	} else {
		gd->gd_options.req_flags = GSS_C_MUTUAL_FLAG;
		gd->gd_options.time_req = 0;
		gd->gd_options.my_cred = GSS_C_NO_CREDENTIAL;
		gd->gd_options.input_channel_bindings = NULL;
	}
	CLNT_ACQUIRE(clnt);
	gd->gd_clnt = clnt;
	gd->gd_ctx = GSS_C_NO_CONTEXT;
	gd->gd_mech = mech_oid;
	gd->gd_qop = qop_num;

	gd->gd_cred.gc_version = RPCSEC_GSS_VERSION;
	gd->gd_cred.gc_proc = RPCSEC_GSS_INIT;
	gd->gd_cred.gc_seq = 0;
	gd->gd_cred.gc_svc = service;
	LIST_INIT(&gd->gd_reqs);
	
	if (!rpc_gss_init(auth, options_ret)) {
		goto bad;
	}
	
	return (auth);

 bad:
	AUTH_DESTROY(auth);
	return (NULL);
}

bool_t
rpc_gss_set_defaults(AUTH *auth, rpc_gss_service_t service, const char *qop)
{
	struct rpc_gss_data	*gd;
	u_int			qop_num;
	const char		*mechanism;

	gd = AUTH_PRIVATE(auth);
	if (!rpc_gss_oid_to_mech(gd->gd_mech, &mechanism)) {
		return (FALSE);
	}

	if (qop) {
		if (!rpc_gss_qop_to_num(qop, mechanism, &qop_num)) {
			return (FALSE);
		}
	} else {
		qop_num = GSS_C_QOP_DEFAULT;
	}

	gd->gd_cred.gc_svc = service;
	gd->gd_qop = qop_num;
	return (TRUE);
}

static void
rpc_gss_purge_xid(struct rpc_gss_data *gd, uint32_t xid)
{
	struct rpc_pending_request *pr, *npr;
	struct rpc_pending_request_list reqs;

	LIST_INIT(&reqs);
	mtx_lock(&gd->gd_lock);
	LIST_FOREACH_SAFE(pr, &gd->gd_reqs, pr_link, npr) {
		if (pr->pr_xid == xid) {
			LIST_REMOVE(pr, pr_link);
			LIST_INSERT_HEAD(&reqs, pr, pr_link);
		}
	}

	mtx_unlock(&gd->gd_lock);

	LIST_FOREACH_SAFE(pr, &reqs, pr_link, npr) {
		mem_free(pr, sizeof(*pr));
	}
}

static uint32_t
rpc_gss_alloc_seq(struct rpc_gss_data *gd)
{
	uint32_t seq;

	mtx_lock(&gd->gd_lock);
	seq = gd->gd_seq;
	gd->gd_seq++;
	mtx_unlock(&gd->gd_lock);

	return (seq);
}

static void
rpc_gss_nextverf(__unused AUTH *auth)
{

	/* not used */
}

static bool_t
rpc_gss_marshal(AUTH *auth, uint32_t xid, XDR *xdrs, struct mbuf *args)
{
	struct rpc_gss_data	*gd;
	struct rpc_pending_request *pr;
	uint32_t		 seq;
	XDR			 tmpxdrs;
	struct rpc_gss_cred	 gsscred;
	char			 credbuf[MAX_AUTH_BYTES];
	struct opaque_auth	 creds, verf;
	gss_buffer_desc		 rpcbuf, checksum;
	OM_uint32		 maj_stat, min_stat;
	bool_t			 xdr_stat;

	rpc_gss_log_debug("in rpc_gss_marshal()");

	gd = AUTH_PRIVATE(auth);
	
	gsscred = gd->gd_cred;
	seq = rpc_gss_alloc_seq(gd);
	gsscred.gc_seq = seq;

	xdrmem_create(&tmpxdrs, credbuf, sizeof(credbuf), XDR_ENCODE);
	if (!xdr_rpc_gss_cred(&tmpxdrs, &gsscred)) {
		XDR_DESTROY(&tmpxdrs);
		_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOMEM);
		return (FALSE);
	}
	creds.oa_flavor = RPCSEC_GSS;
	creds.oa_base = credbuf;
	creds.oa_length = XDR_GETPOS(&tmpxdrs);
	XDR_DESTROY(&tmpxdrs);

	xdr_opaque_auth(xdrs, &creds);

	if (gd->gd_cred.gc_proc == RPCSEC_GSS_INIT ||
	    gd->gd_cred.gc_proc == RPCSEC_GSS_CONTINUE_INIT) {
		if (!xdr_opaque_auth(xdrs, &_null_auth)) {
			_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOMEM);
			return (FALSE);
		}
		xdrmbuf_append(xdrs, args);
		return (TRUE);
	} else {
		/*
		 * Keep track of this XID + seq pair so that we can do
		 * the matching gss_verify_mic in AUTH_VALIDATE.
		 */
		pr = mem_alloc(sizeof(struct rpc_pending_request));
		mtx_lock(&gd->gd_lock);
		pr->pr_xid = xid;
		pr->pr_seq = seq;
		LIST_INSERT_HEAD(&gd->gd_reqs, pr, pr_link);
		mtx_unlock(&gd->gd_lock);

		/*
		 * Checksum serialized RPC header, up to and including
		 * credential. For the in-kernel environment, we
		 * assume that our XDR stream is on a contiguous
		 * memory buffer (e.g. an mbuf).
		 */
		rpcbuf.length = XDR_GETPOS(xdrs);
		XDR_SETPOS(xdrs, 0);
		rpcbuf.value = XDR_INLINE(xdrs, rpcbuf.length);

		maj_stat = gss_get_mic(&min_stat, gd->gd_ctx, gd->gd_qop,
		    &rpcbuf, &checksum);

		if (maj_stat != GSS_S_COMPLETE) {
			rpc_gss_log_status("gss_get_mic", gd->gd_mech,
			    maj_stat, min_stat);
			if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
				rpc_gss_destroy_context(auth, TRUE);
			}
			_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, EPERM);
			return (FALSE);
		}

		verf.oa_flavor = RPCSEC_GSS;
		verf.oa_base = checksum.value;
		verf.oa_length = checksum.length;

		xdr_stat = xdr_opaque_auth(xdrs, &verf);
		gss_release_buffer(&min_stat, &checksum);
		if (!xdr_stat) {
			_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOMEM);
			return (FALSE);
		}
		if (gd->gd_state != RPCSEC_GSS_ESTABLISHED ||
		    gd->gd_cred.gc_svc == rpc_gss_svc_none) {
			xdrmbuf_append(xdrs, args);
			return (TRUE);
		} else {
			if (!xdr_rpc_gss_wrap_data(&args,
				gd->gd_ctx, gd->gd_qop, gd->gd_cred.gc_svc,
				seq))
				return (FALSE);
			xdrmbuf_append(xdrs, args);
			return (TRUE);
		}
	}

	return (TRUE);
}

static bool_t
rpc_gss_validate(AUTH *auth, uint32_t xid, struct opaque_auth *verf,
    struct mbuf **resultsp)
{
	struct rpc_gss_data	*gd;
	struct rpc_pending_request *pr, *npr;
	struct rpc_pending_request_list reqs;
	gss_qop_t		qop_state;
	uint32_t		num, seq;
	gss_buffer_desc		signbuf, checksum;
	OM_uint32		maj_stat, min_stat;

	rpc_gss_log_debug("in rpc_gss_validate()");
	
	gd = AUTH_PRIVATE(auth);

	/*
	 * The client will call us with a NULL verf when it gives up
	 * on an XID.
	 */
	if (!verf) {
		rpc_gss_purge_xid(gd, xid);
		return (TRUE);
	}

	if (gd->gd_state == RPCSEC_GSS_CONTEXT) {
		/*
		 * Save the on the wire verifier to validate last INIT
		 * phase packet after decode if the major status is
		 * GSS_S_COMPLETE.
		 */
		if (gd->gd_verf.value)
			xdr_free((xdrproc_t) xdr_gss_buffer_desc,
			    (char *) &gd->gd_verf);
		gd->gd_verf.value = mem_alloc(verf->oa_length);
		if (gd->gd_verf.value == NULL) {
			printf("gss_validate: out of memory\n");
			_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOMEM);
			m_freem(*resultsp);
			*resultsp = NULL;
			return (FALSE);
		}
		memcpy(gd->gd_verf.value, verf->oa_base, verf->oa_length);
		gd->gd_verf.length = verf->oa_length;

		return (TRUE);
	}

	/*
	 * We need to check the verifier against all the requests
	 * we've send for this XID - for unreliable protocols, we
	 * retransmit with the same XID but different sequence
	 * number. We temporarily take this set of requests out of the
	 * list so that we can work through the list without having to
	 * hold the lock.
	 */
	mtx_lock(&gd->gd_lock);
	LIST_INIT(&reqs);
	LIST_FOREACH_SAFE(pr, &gd->gd_reqs, pr_link, npr) {
		if (pr->pr_xid == xid) {
			LIST_REMOVE(pr, pr_link);
			LIST_INSERT_HEAD(&reqs, pr, pr_link);
		}
	}
	mtx_unlock(&gd->gd_lock);
	LIST_FOREACH(pr, &reqs, pr_link) {
		if (pr->pr_xid == xid) {
			seq = pr->pr_seq;
			num = htonl(seq);
			signbuf.value = &num;
			signbuf.length = sizeof(num);
	
			checksum.value = verf->oa_base;
			checksum.length = verf->oa_length;
	
			maj_stat = gss_verify_mic(&min_stat, gd->gd_ctx,
			    &signbuf, &checksum, &qop_state);
			if (maj_stat != GSS_S_COMPLETE
			    || qop_state != gd->gd_qop) {
				continue;
			}
			if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
				rpc_gss_destroy_context(auth, TRUE);
				break;
			}
			//rpc_gss_purge_reqs(gd, seq);
			LIST_FOREACH_SAFE(pr, &reqs, pr_link, npr)
				mem_free(pr, sizeof(*pr));

			if (gd->gd_cred.gc_svc == rpc_gss_svc_none) {
				return (TRUE);
			} else {
				if (!xdr_rpc_gss_unwrap_data(resultsp,
					gd->gd_ctx, gd->gd_qop,
					gd->gd_cred.gc_svc, seq)) {
					return (FALSE);
				}
			}
			return (TRUE);
		}
	}

	/*
	 * We didn't match - put back any entries for this XID so that
	 * a future call to validate can retry.
	 */
	mtx_lock(&gd->gd_lock);
	LIST_FOREACH_SAFE(pr, &reqs, pr_link, npr) {
		LIST_REMOVE(pr, pr_link);
		LIST_INSERT_HEAD(&gd->gd_reqs, pr, pr_link);
	}
	mtx_unlock(&gd->gd_lock);

	/*
	 * Nothing matches - give up.
	 */
	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, EPERM);
	m_freem(*resultsp);
	*resultsp = NULL;
	return (FALSE);
}

static bool_t
rpc_gss_init(AUTH *auth, rpc_gss_options_ret_t *options_ret)
{
	struct thread		*td = curthread;
	struct ucred		*crsave;
	struct rpc_gss_data	*gd;
	struct rpc_gss_init_res	 gr;
	gss_buffer_desc		principal_desc;
	gss_buffer_desc		*recv_tokenp, recv_token, send_token;
	gss_name_t		name;
	OM_uint32		 maj_stat, min_stat, call_stat;
	const char		*mech;
	struct rpc_callextra	 ext;
	gss_OID			mech_oid;
	gss_OID_set		mechlist;

	rpc_gss_log_debug("in rpc_gss_refresh()");
	
	gd = AUTH_PRIVATE(auth);
	
	mtx_lock(&gd->gd_lock);
	/*
	 * If the context isn't in START state, someone else is
	 * refreshing - we wait till they are done. If they fail, they
	 * will put the state back to START and we can try (most
	 * likely to also fail).
	 */
	while (gd->gd_state != RPCSEC_GSS_START
	    && gd->gd_state != RPCSEC_GSS_ESTABLISHED) {
		msleep(gd, &gd->gd_lock, 0, "gssstate", 0);
	}
	if (gd->gd_state == RPCSEC_GSS_ESTABLISHED) {
		mtx_unlock(&gd->gd_lock);
		return (TRUE);
	}
	gd->gd_state = RPCSEC_GSS_CONTEXT;
	mtx_unlock(&gd->gd_lock);

	gd->gd_cred.gc_proc = RPCSEC_GSS_INIT;
	gd->gd_cred.gc_seq = 0;

	/*
	 * For KerberosV, if there is a client principal name, that implies
	 * that this is a host based initiator credential in the default
	 * keytab file. For this case, it is necessary to do a
	 * gss_acquire_cred(). When this is done, the gssd daemon will
	 * do the equivalent of "kinit -k" to put a TGT for the name in
	 * the credential cache file for the gssd daemon.
	 */
	if (gd->gd_clntprincipal != NULL &&
	    rpc_gss_mech_to_oid("kerberosv5", &mech_oid) &&
	    gd->gd_mech == mech_oid) {
		/* Get rid of any old credential. */
		if (gd->gd_options.my_cred != GSS_C_NO_CREDENTIAL) {
			gss_release_cred(&min_stat, &gd->gd_options.my_cred);
			gd->gd_options.my_cred = GSS_C_NO_CREDENTIAL;
		}
	
		/*
		 * The mechanism must be set to KerberosV for acquisition
		 * of credentials to work reliably.
		 */
		maj_stat = gss_create_empty_oid_set(&min_stat, &mechlist);
		if (maj_stat != GSS_S_COMPLETE) {
			options_ret->major_status = maj_stat;
			options_ret->minor_status = min_stat;
			goto out;
		}
		maj_stat = gss_add_oid_set_member(&min_stat, gd->gd_mech,
		    &mechlist);
		if (maj_stat != GSS_S_COMPLETE) {
			options_ret->major_status = maj_stat;
			options_ret->minor_status = min_stat;
			gss_release_oid_set(&min_stat, &mechlist);
			goto out;
		}
	
		principal_desc.value = (void *)gd->gd_clntprincipal;
		principal_desc.length = strlen(gd->gd_clntprincipal);
		maj_stat = gss_import_name(&min_stat, &principal_desc,
		    GSS_C_NT_HOSTBASED_SERVICE, &name);
		if (maj_stat != GSS_S_COMPLETE) {
			options_ret->major_status = maj_stat;
			options_ret->minor_status = min_stat;
			gss_release_oid_set(&min_stat, &mechlist);
			goto out;
		}
		/* Acquire the credentials. */
		maj_stat = gss_acquire_cred(&min_stat, name, 0,
		    mechlist, GSS_C_INITIATE,
		    &gd->gd_options.my_cred, NULL, NULL);
		gss_release_name(&min_stat, &name);
		gss_release_oid_set(&min_stat, &mechlist);
		if (maj_stat != GSS_S_COMPLETE) {
			options_ret->major_status = maj_stat;
			options_ret->minor_status = min_stat;
			goto out;
		}
	}

	principal_desc.value = (void *)gd->gd_principal;
	principal_desc.length = strlen(gd->gd_principal);
	maj_stat = gss_import_name(&min_stat, &principal_desc,
	    GSS_C_NT_HOSTBASED_SERVICE, &name);
	if (maj_stat != GSS_S_COMPLETE) {
		options_ret->major_status = maj_stat;
		options_ret->minor_status = min_stat;
		goto out;
	}

	/* GSS context establishment loop. */
	memset(&recv_token, 0, sizeof(recv_token));
	memset(&gr, 0, sizeof(gr));
	memset(options_ret, 0, sizeof(*options_ret));
	options_ret->major_status = GSS_S_FAILURE;
	recv_tokenp = GSS_C_NO_BUFFER;
	
	for (;;) {
		crsave = td->td_ucred;
		td->td_ucred = gd->gd_ucred;
		maj_stat = gss_init_sec_context(&min_stat,
		    gd->gd_options.my_cred,
		    &gd->gd_ctx,
		    name,
		    gd->gd_mech,
		    gd->gd_options.req_flags,
		    gd->gd_options.time_req,
		    gd->gd_options.input_channel_bindings,
		    recv_tokenp,
		    &gd->gd_mech,	/* used mech */
		    &send_token,
		    &options_ret->ret_flags,
		    &options_ret->time_req);
		td->td_ucred = crsave;
		
		/*
		 * Free the token which we got from the server (if
		 * any).  Remember that this was allocated by XDR, not
		 * GSS-API.
		 */
		if (recv_tokenp != GSS_C_NO_BUFFER) {
			xdr_free((xdrproc_t) xdr_gss_buffer_desc,
			    (char *) &recv_token);
			recv_tokenp = GSS_C_NO_BUFFER;
		}
		if (gd->gd_mech && rpc_gss_oid_to_mech(gd->gd_mech, &mech)) {
			strlcpy(options_ret->actual_mechanism,
			    mech,
			    sizeof(options_ret->actual_mechanism));
		}
		if (maj_stat != GSS_S_COMPLETE &&
		    maj_stat != GSS_S_CONTINUE_NEEDED) {
			rpc_gss_log_status("gss_init_sec_context", gd->gd_mech,
			    maj_stat, min_stat);
			options_ret->major_status = maj_stat;
			options_ret->minor_status = min_stat;
			break;
		}
		if (send_token.length != 0) {
			memset(&gr, 0, sizeof(gr));
			
			bzero(&ext, sizeof(ext));
			ext.rc_auth = auth;
			call_stat = CLNT_CALL_EXT(gd->gd_clnt, &ext, NULLPROC,
			    (xdrproc_t)xdr_gss_buffer_desc,
			    &send_token,
			    (xdrproc_t)xdr_rpc_gss_init_res,
			    (caddr_t)&gr, AUTH_TIMEOUT);
			
			gss_release_buffer(&min_stat, &send_token);
			
			if (call_stat != RPC_SUCCESS)
				break;

			if (gr.gr_major != GSS_S_COMPLETE &&
			    gr.gr_major != GSS_S_CONTINUE_NEEDED) {
				rpc_gss_log_status("server reply", gd->gd_mech,
				    gr.gr_major, gr.gr_minor);
				options_ret->major_status = gr.gr_major;
				options_ret->minor_status = gr.gr_minor;
				break;
			}
			
			/*
			 * Save the server's gr_handle value, freeing
			 * what we have already (remember that this
			 * was allocated by XDR, not GSS-API).
			 */
			if (gr.gr_handle.length != 0) {
				xdr_free((xdrproc_t) xdr_gss_buffer_desc,
				    (char *) &gd->gd_cred.gc_handle);
				gd->gd_cred.gc_handle = gr.gr_handle;
			}

			/*
			 * Save the server's token as well.
			 */
			if (gr.gr_token.length != 0) {
				recv_token = gr.gr_token;
				recv_tokenp = &recv_token;
			}

			/*
			 * Since we have copied out all the bits of gr
			 * which XDR allocated for us, we don't need
			 * to free it.
			 */
			gd->gd_cred.gc_proc = RPCSEC_GSS_CONTINUE_INIT;
		}

		if (maj_stat == GSS_S_COMPLETE) {
			gss_buffer_desc   bufin;
			u_int seq, qop_state = 0;

			/* 
			 * gss header verifier,
			 * usually checked in gss_validate
			 */
			seq = htonl(gr.gr_win);
			bufin.value = (unsigned char *)&seq;
			bufin.length = sizeof(seq);

			maj_stat = gss_verify_mic(&min_stat, gd->gd_ctx,
			    &bufin, &gd->gd_verf, &qop_state);

			if (maj_stat != GSS_S_COMPLETE ||
			    qop_state != gd->gd_qop) {
				rpc_gss_log_status("gss_verify_mic", gd->gd_mech,
				    maj_stat, min_stat);
				if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
					rpc_gss_destroy_context(auth, TRUE);
				}
				_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR,
				    EPERM);
				options_ret->major_status = maj_stat;
				options_ret->minor_status = min_stat;
				break;
			}

			options_ret->major_status = GSS_S_COMPLETE;
			options_ret->minor_status = 0;
			options_ret->rpcsec_version = gd->gd_cred.gc_version;
			options_ret->gss_context = gd->gd_ctx;

			gd->gd_cred.gc_proc = RPCSEC_GSS_DATA;
			gd->gd_seq = 1;
			gd->gd_win = gr.gr_win;
			break;
		}
	}

	gss_release_name(&min_stat, &name);
	xdr_free((xdrproc_t) xdr_gss_buffer_desc,
	    (char *) &gd->gd_verf);

out:
	/* End context negotiation loop. */
	if (gd->gd_cred.gc_proc != RPCSEC_GSS_DATA) {
		rpc_createerr.cf_stat = RPC_AUTHERROR;
		_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, EPERM);
		if (gd->gd_ctx) {
			gss_delete_sec_context(&min_stat, &gd->gd_ctx,
				GSS_C_NO_BUFFER);
		}
		mtx_lock(&gd->gd_lock);
		gd->gd_state = RPCSEC_GSS_START;
		wakeup(gd);
		mtx_unlock(&gd->gd_lock);
		return (FALSE);
	}
	
	mtx_lock(&gd->gd_lock);
	gd->gd_state = RPCSEC_GSS_ESTABLISHED;
	wakeup(gd);
	mtx_unlock(&gd->gd_lock);

	return (TRUE);
}

static bool_t
rpc_gss_refresh(AUTH *auth, void *msg)
{
	struct rpc_msg *reply = (struct rpc_msg *) msg;
	rpc_gss_options_ret_t options;
	struct rpc_gss_data *gd;

	gd = AUTH_PRIVATE(auth);
	
	/*
	 * If the context is in DESTROYING state, then just return, since
	 * there is no point in refreshing the credentials.
	 */
	mtx_lock(&gd->gd_lock);
	if (gd->gd_state == RPCSEC_GSS_DESTROYING) {
		mtx_unlock(&gd->gd_lock);
		return (FALSE);
	}
	mtx_unlock(&gd->gd_lock);

	/*
	 * If the error was RPCSEC_GSS_CREDPROBLEM of
	 * RPCSEC_GSS_CTXPROBLEM we start again from scratch. All
	 * other errors are fatal.
	 */
	if (reply->rm_reply.rp_stat == MSG_DENIED
	    && reply->rm_reply.rp_rjct.rj_stat == AUTH_ERROR
	    && (reply->rm_reply.rp_rjct.rj_why == RPCSEC_GSS_CREDPROBLEM
		|| reply->rm_reply.rp_rjct.rj_why == RPCSEC_GSS_CTXPROBLEM)) {
		rpc_gss_destroy_context(auth, FALSE);
		memset(&options, 0, sizeof(options));
		return (rpc_gss_init(auth, &options));
	}

	return (FALSE);
}

static void
rpc_gss_destroy_context(AUTH *auth, bool_t send_destroy)
{
	struct rpc_gss_data	*gd;
	struct rpc_pending_request *pr;
	OM_uint32		 min_stat;
	struct rpc_callextra	 ext;

	rpc_gss_log_debug("in rpc_gss_destroy_context()");
	
	gd = AUTH_PRIVATE(auth);
	
	mtx_lock(&gd->gd_lock);
	/*
	 * If the context isn't in ESTABISHED state, someone else is
	 * destroying/refreshing - we wait till they are done.
	 */
	if (gd->gd_state != RPCSEC_GSS_ESTABLISHED) {
		while (gd->gd_state != RPCSEC_GSS_START
		    && gd->gd_state != RPCSEC_GSS_ESTABLISHED)
			msleep(gd, &gd->gd_lock, 0, "gssstate", 0);
		mtx_unlock(&gd->gd_lock);
		return;
	}
	gd->gd_state = RPCSEC_GSS_DESTROYING;
	mtx_unlock(&gd->gd_lock);

	if (send_destroy) {
		gd->gd_cred.gc_proc = RPCSEC_GSS_DESTROY;
		bzero(&ext, sizeof(ext));
		ext.rc_auth = auth;
		CLNT_CALL_EXT(gd->gd_clnt, &ext, NULLPROC,
		    (xdrproc_t)xdr_void, NULL,
		    (xdrproc_t)xdr_void, NULL, AUTH_TIMEOUT);
	}

	while ((pr = LIST_FIRST(&gd->gd_reqs)) != NULL) {
		LIST_REMOVE(pr, pr_link);
		mem_free(pr, sizeof(*pr));
	}

	/*
	 * Free the context token. Remember that this was
	 * allocated by XDR, not GSS-API.
	 */
	xdr_free((xdrproc_t) xdr_gss_buffer_desc,
	    (char *) &gd->gd_cred.gc_handle);
	gd->gd_cred.gc_handle.length = 0;

	if (gd->gd_ctx != GSS_C_NO_CONTEXT)
		gss_delete_sec_context(&min_stat, &gd->gd_ctx, NULL);

	mtx_lock(&gd->gd_lock);
	gd->gd_state = RPCSEC_GSS_START;
	wakeup(gd);
	mtx_unlock(&gd->gd_lock);
}

static void
rpc_gss_destroy(AUTH *auth)
{
	struct rpc_gss_data	*gd;
	
	rpc_gss_log_debug("in rpc_gss_destroy()");
	
	gd = AUTH_PRIVATE(auth);
	
	if (!refcount_release(&gd->gd_refs))
		return;

	rpc_gss_destroy_context(auth, TRUE);
	
	CLNT_RELEASE(gd->gd_clnt);
	crfree(gd->gd_ucred);
	free(gd->gd_principal, M_RPC);
	if (gd->gd_clntprincipal != NULL)
		free(gd->gd_clntprincipal, M_RPC);
	if (gd->gd_verf.value)
		xdr_free((xdrproc_t) xdr_gss_buffer_desc,
		    (char *) &gd->gd_verf);
	mtx_destroy(&gd->gd_lock);

	mem_free(gd, sizeof(*gd));
	mem_free(auth, sizeof(*auth));
}

int
rpc_gss_max_data_length(AUTH *auth, int max_tp_unit_len)
{
	struct rpc_gss_data	*gd;
	int			want_conf;
	OM_uint32		max;
	OM_uint32		maj_stat, min_stat;
	int			result;

	gd = AUTH_PRIVATE(auth);

	switch (gd->gd_cred.gc_svc) {
	case rpc_gss_svc_none:
		return (max_tp_unit_len);
		break;

	case rpc_gss_svc_default:
	case rpc_gss_svc_integrity:
		want_conf = FALSE;
		break;

	case rpc_gss_svc_privacy:
		want_conf = TRUE;
		break;

	default:
		return (0);
	}

	maj_stat = gss_wrap_size_limit(&min_stat, gd->gd_ctx, want_conf,
	    gd->gd_qop, max_tp_unit_len, &max);

	if (maj_stat == GSS_S_COMPLETE) {
		result = (int) max;
		if (result < 0)
			result = 0;
		return (result);
	} else {
		rpc_gss_log_status("gss_wrap_size_limit", gd->gd_mech,
		    maj_stat, min_stat);
		return (0);
	}
}