1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2012 Milan Jurik. All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
  28  *
  29  * $Id: svc_auth_gssapi.c,v 1.19 1994/10/27 12:38:51 jik Exp $
  30  */
  31 
  32 /*
  33  * Server side handling of RPCSEC_GSS flavor.
  34  */
  35 
  36 #include <sys/systm.h>
  37 #include <sys/kstat.h>
  38 #include <sys/cmn_err.h>
  39 #include <sys/debug.h>
  40 #include <sys/types.h>
  41 #include <sys/time.h>
  42 #include <gssapi/gssapi.h>
  43 #include <gssapi/gssapi_ext.h>
  44 #include <rpc/rpc.h>
  45 #include <rpc/rpcsec_defs.h>
  46 #include <sys/sunddi.h>
  47 #include <sys/atomic.h>
  48 
  49 extern bool_t __rpc_gss_make_principal(rpc_gss_principal_t *, gss_buffer_t);
  50 
  51 #ifdef  DEBUG
  52 extern void prom_printf(const char *, ...);
  53 #endif
  54 
  55 #ifdef  _KERNEL
  56 #define memcmp(a, b, l) bcmp((a), (b), (l))
  57 #endif
  58 
  59 
  60 /*
  61  * Sequence window definitions.
  62  */
  63 #define SEQ_ARR_SIZE    4
  64 #define SEQ_WIN         (SEQ_ARR_SIZE*32)
  65 #define SEQ_HI_BIT      0x80000000
  66 #define SEQ_LO_BIT      1
  67 #define DIV_BY_32       5
  68 #define SEQ_MASK        0x1f
  69 #define SEQ_MAX         ((unsigned int)0x80000000)
  70 
  71 
  72 /* cache retransmit data */
  73 typedef struct _retrans_entry {
  74         uint32_t        xid;
  75         rpc_gss_init_res result;
  76 } retrans_entry;
  77 
  78 /*
  79  * Server side RPCSEC_GSS context information.
  80  */
  81 typedef struct _svc_rpc_gss_data {
  82         struct _svc_rpc_gss_data        *next, *prev;
  83         struct _svc_rpc_gss_data        *lru_next, *lru_prev;
  84         bool_t                          established;
  85         gss_ctx_id_t                    context;
  86         gss_buffer_desc                 client_name;
  87         time_t                          expiration;
  88         uint_t                          seq_num;
  89         uint_t                          seq_bits[SEQ_ARR_SIZE];
  90         uint_t                          key;
  91         OM_uint32                       qop;
  92         bool_t                          done_docallback;
  93         bool_t                          locked;
  94         rpc_gss_rawcred_t               raw_cred;
  95         rpc_gss_ucred_t                 u_cred;
  96         time_t                          u_cred_set;
  97         void                            *cookie;
  98         gss_cred_id_t                   deleg;
  99         kmutex_t                        clm;
 100         int                             ref_cnt;
 101         time_t                          last_ref_time;
 102         bool_t                          stale;
 103         retrans_entry                   *retrans_data;
 104 } svc_rpc_gss_data;
 105 
 106 /*
 107  * Data structures used for LRU based context management.
 108  */
 109 
 110 
 111 #define HASH(key) ((key) % svc_rpc_gss_hashmod)
 112 /* Size of hash table for svc_rpc_gss_data structures */
 113 #define GSS_DATA_HASH_SIZE      1024
 114 
 115 /*
 116  * The following two defines specify a time delta that is used in
 117  * sweep_clients. When the last_ref_time of a context is older than
 118  * than the current time minus the delta, i.e, the context has not
 119  * been referenced in the last delta seconds, we will return the
 120  * context back to the cache if the ref_cnt is zero. The first delta
 121  * value will be used when sweep_clients is called from
 122  * svc_data_reclaim, the kmem_cache reclaim call back. We will reclaim
 123  * all entries except those that are currently "active". By active we
 124  * mean those that have been referenced in the last ACTIVE_DELTA
 125  * seconds. If sweep_client is not being called from reclaim, then we
 126  * will reclaim all entries that are "inactive". By inactive we mean
 127  * those entries that have not been accessed in INACTIVE_DELTA
 128  * seconds.  Note we always assume that ACTIVE_DELTA is less than
 129  * INACTIVE_DELTA, so that reaping entries from a reclaim operation
 130  * will necessarily imply reaping all "inactive" entries and then
 131  * some.
 132  */
 133 
 134 /*
 135  * If low on memory reap cache entries that have not been active for
 136  * ACTIVE_DELTA seconds and have a ref_cnt equal to zero.
 137  */
 138 #define ACTIVE_DELTA            30*60           /* 30 minutes */
 139 
 140 /*
 141  * If in sweeping contexts we find contexts with a ref_cnt equal to zero
 142  * and the context has not been referenced in INACTIVE_DELTA seconds, return
 143  * the entry to the cache.
 144  */
 145 #define INACTIVE_DELTA          8*60*60         /* 8 hours */
 146 
 147 int                             svc_rpc_gss_hashmod = GSS_DATA_HASH_SIZE;
 148 static svc_rpc_gss_data         **clients;
 149 static svc_rpc_gss_data         *lru_first, *lru_last;
 150 static time_t                   sweep_interval = 60*60;
 151 static time_t                   last_swept = 0;
 152 static int                      num_gss_contexts = 0;
 153 static time_t                   svc_rpcgss_gid_timeout = 60*60*12;
 154 static kmem_cache_t             *svc_data_handle;
 155 static time_t                   svc_rpc_gss_active_delta = ACTIVE_DELTA;
 156 static time_t                   svc_rpc_gss_inactive_delta = INACTIVE_DELTA;
 157 
 158 /*
 159  * lock used with context/lru variables
 160  */
 161 static kmutex_t                 ctx_mutex;
 162 
 163 /*
 164  * Data structure to contain cache statistics
 165  */
 166 
 167 static struct {
 168         int64_t total_entries_allocated;
 169         int64_t no_reclaims;
 170         int64_t no_returned_by_reclaim;
 171 } svc_rpc_gss_cache_stats;
 172 
 173 
 174 /*
 175  * lock used with server credential variables list
 176  *
 177  * server cred list locking guidelines:
 178  * - Writer's lock holder has exclusive access to the list
 179  */
 180 static krwlock_t                cred_lock;
 181 
 182 /*
 183  * server callback list
 184  */
 185 typedef struct rpc_gss_cblist_s {
 186         struct rpc_gss_cblist_s         *next;
 187         rpc_gss_callback_t      cb;
 188 } rpc_gss_cblist_t;
 189 
 190 static rpc_gss_cblist_t                 *rpc_gss_cblist = NULL;
 191 
 192 /*
 193  * lock used with callback variables
 194  */
 195 static kmutex_t                 cb_mutex;
 196 
 197 /*
 198  * forward declarations
 199  */
 200 static bool_t                   svc_rpc_gss_wrap();
 201 static bool_t                   svc_rpc_gss_unwrap();
 202 static svc_rpc_gss_data         *create_client();
 203 static svc_rpc_gss_data         *get_client();
 204 static svc_rpc_gss_data         *find_client();
 205 static void                     destroy_client();
 206 static void                     sweep_clients(bool_t);
 207 static void                     insert_client();
 208 static bool_t                   check_verf(struct rpc_msg *, gss_ctx_id_t,
 209                                         int *, uid_t);
 210 static bool_t                   set_response_verf();
 211 static void                     retrans_add(svc_rpc_gss_data *, uint32_t,
 212                                         rpc_gss_init_res *);
 213 static void                     retrans_del(svc_rpc_gss_data *);
 214 static bool_t                   transfer_sec_context(svc_rpc_gss_data *);
 215 static void                     common_client_data_free(svc_rpc_gss_data *);
 216 
 217 /*
 218  * server side wrap/unwrap routines
 219  */
 220 struct svc_auth_ops svc_rpc_gss_ops = {
 221         svc_rpc_gss_wrap,
 222         svc_rpc_gss_unwrap,
 223 };
 224 
 225 /* taskq(9F) */
 226 typedef struct svcrpcsec_gss_taskq_arg {
 227         SVCXPRT                 *rq_xprt;
 228         rpc_gss_init_arg        *rpc_call_arg;
 229         struct rpc_msg          *msg;
 230         svc_rpc_gss_data        *client_data;
 231         uint_t                  cr_version;
 232         rpc_gss_service_t       cr_service;
 233 } svcrpcsec_gss_taskq_arg_t;
 234 
 235 /* gssd is single threaded, so 1 thread for the taskq is probably good/ok */
 236 int rpcsec_gss_init_taskq_nthreads = 1;
 237 static ddi_taskq_t *svcrpcsec_gss_init_taskq = NULL;
 238 
 239 extern struct rpc_msg *rpc_msg_dup(struct rpc_msg *);
 240 extern void rpc_msg_free(struct rpc_msg **, int);
 241 
 242 /*
 243  * from svc_clts.c:
 244  * Transport private data.
 245  * Kept in xprt->xp_p2buf.
 246  */
 247 struct udp_data {
 248         mblk_t  *ud_resp;                       /* buffer for response */
 249         mblk_t  *ud_inmp;                       /* mblk chain of request */
 250 };
 251 
 252 /*ARGSUSED*/
 253 static int
 254 svc_gss_data_create(void *buf, void *pdata, int kmflag)
 255 {
 256         svc_rpc_gss_data *client_data = (svc_rpc_gss_data *)buf;
 257 
 258         mutex_init(&client_data->clm, NULL, MUTEX_DEFAULT, NULL);
 259 
 260         return (0);
 261 }
 262 
 263 /*ARGSUSED*/
 264 static void
 265 svc_gss_data_destroy(void *buf, void *pdata)
 266 {
 267         svc_rpc_gss_data *client_data = (svc_rpc_gss_data *)buf;
 268 
 269         mutex_destroy(&client_data->clm);
 270 }
 271 
 272 
 273 /*ARGSUSED*/
 274 static void
 275 svc_gss_data_reclaim(void *pdata)
 276 {
 277         mutex_enter(&ctx_mutex);
 278 
 279         svc_rpc_gss_cache_stats.no_reclaims++;
 280         sweep_clients(TRUE);
 281 
 282         mutex_exit(&ctx_mutex);
 283 }
 284 
 285 /*
 286  *  Init stuff on the server side.
 287  */
 288 void
 289 svc_gss_init()
 290 {
 291         mutex_init(&cb_mutex, NULL, MUTEX_DEFAULT, NULL);
 292         mutex_init(&ctx_mutex, NULL, MUTEX_DEFAULT, NULL);
 293         rw_init(&cred_lock, NULL, RW_DEFAULT, NULL);
 294         clients = (svc_rpc_gss_data **)
 295             kmem_zalloc(svc_rpc_gss_hashmod * sizeof (svc_rpc_gss_data *),
 296             KM_SLEEP);
 297         svc_data_handle = kmem_cache_create("rpc_gss_data_cache",
 298             sizeof (svc_rpc_gss_data), 0,
 299             svc_gss_data_create,
 300             svc_gss_data_destroy,
 301             svc_gss_data_reclaim,
 302             NULL, NULL, 0);
 303 
 304         if (svcrpcsec_gss_init_taskq == NULL) {
 305                 svcrpcsec_gss_init_taskq = ddi_taskq_create(NULL,
 306                     "rpcsec_gss_init_taskq", rpcsec_gss_init_taskq_nthreads,
 307                     TASKQ_DEFAULTPRI, 0);
 308                 if (svcrpcsec_gss_init_taskq == NULL)
 309                         cmn_err(CE_NOTE,
 310                             "svc_gss_init: ddi_taskq_create failed");
 311         }
 312 }
 313 
 314 /*
 315  * Destroy structures allocated in svc_gss_init().
 316  * This routine is called by _init() if mod_install() failed.
 317  */
 318 void
 319 svc_gss_fini()
 320 {
 321         mutex_destroy(&cb_mutex);
 322         mutex_destroy(&ctx_mutex);
 323         rw_destroy(&cred_lock);
 324         kmem_free(clients, svc_rpc_gss_hashmod * sizeof (svc_rpc_gss_data *));
 325         kmem_cache_destroy(svc_data_handle);
 326 }
 327 
 328 /*
 329  * Cleanup routine for destroying context, called after service
 330  * procedure is executed. Actually we just decrement the reference count
 331  * associated with this context. If the reference count is zero and the
 332  * context is marked as stale, we would then destroy the context. Additionally,
 333  * we check if its been longer than sweep_interval since the last sweep_clients
 334  * was run, and if so run sweep_clients to free all stale contexts with zero
 335  * reference counts or contexts that are old. (Haven't been access in
 336  * svc_rpc_inactive_delta seconds).
 337  */
 338 void
 339 rpc_gss_cleanup(SVCXPRT *clone_xprt)
 340 {
 341         svc_rpc_gss_data        *cl;
 342         SVCAUTH                 *svcauth;
 343 
 344         /*
 345          * First check if current context needs to be cleaned up.
 346          * There might be other threads stale this client data
 347          * in between.
 348          */
 349         svcauth = &clone_xprt->xp_auth;
 350         mutex_enter(&ctx_mutex);
 351         if ((cl = (svc_rpc_gss_data *)svcauth->svc_ah_private) != NULL) {
 352                 mutex_enter(&cl->clm);
 353                 ASSERT(cl->ref_cnt > 0);
 354                 if (--cl->ref_cnt == 0 && cl->stale) {
 355                         mutex_exit(&cl->clm);
 356                         destroy_client(cl);
 357                         svcauth->svc_ah_private = NULL;
 358                 } else
 359                         mutex_exit(&cl->clm);
 360         }
 361 
 362         /*
 363          * Check for other expired contexts.
 364          */
 365         if ((gethrestime_sec() - last_swept) > sweep_interval)
 366                 sweep_clients(FALSE);
 367 
 368         mutex_exit(&ctx_mutex);
 369 }
 370 
 371 /*
 372  * Shift the array arr of length arrlen right by nbits bits.
 373  */
 374 static void
 375 shift_bits(arr, arrlen, nbits)
 376         uint_t  *arr;
 377         int     arrlen;
 378         int     nbits;
 379 {
 380         int     i, j;
 381         uint_t  lo, hi;
 382 
 383         /*
 384          * If the number of bits to be shifted exceeds SEQ_WIN, just
 385          * zero out the array.
 386          */
 387         if (nbits < SEQ_WIN) {
 388                 for (i = 0; i < nbits; i++) {
 389                         hi = 0;
 390                         for (j = 0; j < arrlen; j++) {
 391                                 lo = arr[j] & SEQ_LO_BIT;
 392                                 arr[j] >>= 1;
 393                                 if (hi)
 394                                         arr[j] |= SEQ_HI_BIT;
 395                                 hi = lo;
 396                         }
 397                 }
 398         } else {
 399                 for (j = 0; j < arrlen; j++)
 400                         arr[j] = 0;
 401         }
 402 }
 403 
 404 /*
 405  * Check that the received sequence number seq_num is valid.
 406  */
 407 static bool_t
 408 check_seq(cl, seq_num, kill_context)
 409         svc_rpc_gss_data        *cl;
 410         uint_t                  seq_num;
 411         bool_t                  *kill_context;
 412 {
 413         int                     i, j;
 414         uint_t                  bit;
 415 
 416         /*
 417          * If it exceeds the maximum, kill context.
 418          */
 419         if (seq_num >= SEQ_MAX) {
 420                 *kill_context = TRUE;
 421                 RPCGSS_LOG0(4, "check_seq: seq_num not valid\n");
 422                 return (FALSE);
 423         }
 424 
 425         /*
 426          * If greater than the last seen sequence number, just shift
 427          * the sequence window so that it starts at the new sequence
 428          * number and extends downwards by SEQ_WIN.
 429          */
 430         if (seq_num > cl->seq_num) {
 431                 (void) shift_bits(cl->seq_bits, SEQ_ARR_SIZE,
 432                                 (int)(seq_num - cl->seq_num));
 433                 cl->seq_bits[0] |= SEQ_HI_BIT;
 434                 cl->seq_num = seq_num;
 435                 return (TRUE);
 436         }
 437 
 438         /*
 439          * If it is outside the sequence window, return failure.
 440          */
 441         i = cl->seq_num - seq_num;
 442         if (i >= SEQ_WIN) {
 443                 RPCGSS_LOG0(4, "check_seq: seq_num is outside the window\n");
 444                 return (FALSE);
 445         }
 446 
 447         /*
 448          * If within sequence window, set the bit corresponding to it
 449          * if not already seen;  if already seen, return failure.
 450          */
 451         j = SEQ_MASK - (i & SEQ_MASK);
 452         bit = j > 0 ? (1 << j) : 1;
 453         i >>= DIV_BY_32;
 454         if (cl->seq_bits[i] & bit) {
 455                 RPCGSS_LOG0(4, "check_seq: sequence number already seen\n");
 456                 return (FALSE);
 457         }
 458         cl->seq_bits[i] |= bit;
 459         return (TRUE);
 460 }
 461 
 462 /*
 463  * Set server callback.
 464  */
 465 bool_t
 466 rpc_gss_set_callback(cb)
 467         rpc_gss_callback_t      *cb;
 468 {
 469         rpc_gss_cblist_t                *cbl, *tmp;
 470 
 471         if (cb->callback == NULL) {
 472                 RPCGSS_LOG0(1, "rpc_gss_set_callback: no callback to set\n");
 473                 return (FALSE);
 474         }
 475 
 476         /* check if there is already an entry in the rpc_gss_cblist. */
 477         mutex_enter(&cb_mutex);
 478         if (rpc_gss_cblist) {
 479                 for (tmp = rpc_gss_cblist; tmp != NULL; tmp = tmp->next) {
 480                         if ((tmp->cb.callback == cb->callback) &&
 481                             (tmp->cb.version == cb->version) &&
 482                             (tmp->cb.program == cb->program)) {
 483                                 mutex_exit(&cb_mutex);
 484                                 return (TRUE);
 485                         }
 486                 }
 487         }
 488 
 489         /* Not in rpc_gss_cblist.  Create a new entry. */
 490         if ((cbl = (rpc_gss_cblist_t *)kmem_alloc(sizeof (*cbl), KM_SLEEP))
 491             == NULL) {
 492                 mutex_exit(&cb_mutex);
 493                 return (FALSE);
 494         }
 495         cbl->cb = *cb;
 496         cbl->next = rpc_gss_cblist;
 497         rpc_gss_cblist = cbl;
 498         mutex_exit(&cb_mutex);
 499         return (TRUE);
 500 }
 501 
 502 /*
 503  * Locate callback (if specified) and call server.  Release any
 504  * delegated credentials unless passed to server and the server
 505  * accepts the context.  If a callback is not specified, accept
 506  * the incoming context.
 507  */
 508 static bool_t
 509 do_callback(req, client_data)
 510         struct svc_req          *req;
 511         svc_rpc_gss_data        *client_data;
 512 {
 513         rpc_gss_cblist_t                *cbl;
 514         bool_t                  ret = TRUE, found = FALSE;
 515         rpc_gss_lock_t          lock;
 516         OM_uint32               minor;
 517         mutex_enter(&cb_mutex);
 518         for (cbl = rpc_gss_cblist; cbl != NULL; cbl = cbl->next) {
 519                 if (req->rq_prog != cbl->cb.program ||
 520                                         req->rq_vers != cbl->cb.version)
 521                         continue;
 522                 found = TRUE;
 523                 lock.locked = FALSE;
 524                 lock.raw_cred = &client_data->raw_cred;
 525                 ret = (*cbl->cb.callback)(req, client_data->deleg,
 526                         client_data->context, &lock, &client_data->cookie);
 527                 req->rq_xprt->xp_cookie = client_data->cookie;
 528 
 529                 if (ret) {
 530                         client_data->locked = lock.locked;
 531                         client_data->deleg = GSS_C_NO_CREDENTIAL;
 532                 }
 533                 break;
 534         }
 535         if (!found) {
 536                 if (client_data->deleg != GSS_C_NO_CREDENTIAL) {
 537                         (void) kgss_release_cred(&minor, &client_data->deleg,
 538                                         crgetuid(CRED()));
 539                         client_data->deleg = GSS_C_NO_CREDENTIAL;
 540                 }
 541         }
 542         mutex_exit(&cb_mutex);
 543         return (ret);
 544 }
 545 
 546 /*
 547  * Get caller credentials.
 548  */
 549 bool_t
 550 rpc_gss_getcred(req, rcred, ucred, cookie)
 551         struct svc_req          *req;
 552         rpc_gss_rawcred_t       **rcred;
 553         rpc_gss_ucred_t         **ucred;
 554         void                    **cookie;
 555 {
 556         SVCAUTH                 *svcauth;
 557         svc_rpc_gss_data        *client_data;
 558         int                     gssstat, gidlen;
 559 
 560         svcauth = &req->rq_xprt->xp_auth;
 561         client_data = (svc_rpc_gss_data *)svcauth->svc_ah_private;
 562 
 563         mutex_enter(&client_data->clm);
 564 
 565         if (rcred != NULL) {
 566                 svcauth->raw_cred = client_data->raw_cred;
 567                 *rcred = &svcauth->raw_cred;
 568         }
 569         if (ucred != NULL) {
 570                 *ucred = &client_data->u_cred;
 571 
 572                 if (client_data->u_cred_set == 0 ||
 573                     client_data->u_cred_set < gethrestime_sec()) {
 574                     if (client_data->u_cred_set == 0) {
 575                         if ((gssstat = kgsscred_expname_to_unix_cred(
 576                             &client_data->client_name,
 577                             &client_data->u_cred.uid,
 578                             &client_data->u_cred.gid,
 579                             &client_data->u_cred.gidlist,
 580                             &gidlen, crgetuid(CRED()))) != GSS_S_COMPLETE) {
 581                                 RPCGSS_LOG(1, "rpc_gss_getcred: "
 582                                     "kgsscred_expname_to_unix_cred failed %x\n",
 583                                     gssstat);
 584                                 *ucred = NULL;
 585                         } else {
 586                                 client_data->u_cred.gidlen = (short)gidlen;
 587                                 client_data->u_cred_set =
 588                                     gethrestime_sec() + svc_rpcgss_gid_timeout;
 589                         }
 590                     } else if (client_data->u_cred_set < gethrestime_sec()) {
 591                         if ((gssstat = kgss_get_group_info(
 592                             client_data->u_cred.uid,
 593                             &client_data->u_cred.gid,
 594                             &client_data->u_cred.gidlist,
 595                             &gidlen, crgetuid(CRED()))) != GSS_S_COMPLETE) {
 596                                 RPCGSS_LOG(1, "rpc_gss_getcred: "
 597                                     "kgss_get_group_info failed %x\n",
 598                                     gssstat);
 599                                 *ucred = NULL;
 600                         } else {
 601                                 client_data->u_cred.gidlen = (short)gidlen;
 602                                 client_data->u_cred_set =
 603                                     gethrestime_sec() + svc_rpcgss_gid_timeout;
 604                         }
 605                     }
 606                 }
 607         }
 608 
 609         if (cookie != NULL)
 610                 *cookie = client_data->cookie;
 611         req->rq_xprt->xp_cookie = client_data->cookie;
 612 
 613         mutex_exit(&client_data->clm);
 614 
 615         return (TRUE);
 616 }
 617 
 618 /*
 619  * Transfer the context data from the user land to the kernel.
 620  */
 621 bool_t transfer_sec_context(svc_rpc_gss_data *client_data) {
 622 
 623         gss_buffer_desc process_token;
 624         OM_uint32 gssstat, minor;
 625 
 626         /*
 627          * Call kgss_export_sec_context
 628          * if an error is returned log a message
 629          * go to error handling
 630          * Otherwise call kgss_import_sec_context to
 631          * convert the token into a context
 632          */
 633         gssstat  = kgss_export_sec_context(&minor, client_data->context,
 634                                 &process_token);
 635         /*
 636          * if export_sec_context returns an error we delete the
 637          * context just to be safe.
 638          */
 639         if (gssstat == GSS_S_NAME_NOT_MN) {
 640                 RPCGSS_LOG0(4, "svc_rpcsec_gss: export sec context "
 641                                 "Kernel mod unavailable\n");
 642 
 643         } else if (gssstat != GSS_S_COMPLETE) {
 644                 RPCGSS_LOG(1, "svc_rpcsec_gss: export sec context failed  "
 645                                 " gssstat = 0x%x\n", gssstat);
 646                 (void) gss_release_buffer(&minor, &process_token);
 647                 (void) kgss_delete_sec_context(&minor, &client_data->context,
 648                                 NULL);
 649                 return (FALSE);
 650 
 651         } else if (process_token.length == 0) {
 652                 RPCGSS_LOG0(1, "svc_rpcsec_gss:zero length token in response "
 653                                 "for export_sec_context, but "
 654                                 "gsstat == GSS_S_COMPLETE\n");
 655                 (void) kgss_delete_sec_context(&minor, &client_data->context,
 656                                 NULL);
 657                 return (FALSE);
 658 
 659         } else {
 660                 gssstat = kgss_import_sec_context(&minor, &process_token,
 661                                         client_data->context);
 662                 if (gssstat != GSS_S_COMPLETE) {
 663                         RPCGSS_LOG(1, "svc_rpcsec_gss: import sec context "
 664                                 " failed gssstat = 0x%x\n", gssstat);
 665                         (void) kgss_delete_sec_context(&minor,
 666                                 &client_data->context, NULL);
 667                         (void) gss_release_buffer(&minor, &process_token);
 668                         return (FALSE);
 669                 }
 670 
 671                 RPCGSS_LOG0(4, "gss_import_sec_context successful\n");
 672                 (void) gss_release_buffer(&minor, &process_token);
 673         }
 674 
 675         return (TRUE);
 676 }
 677 
 678 /*
 679  * do_gss_accept is called from a taskq and does all the work for a
 680  * RPCSEC_GSS_INIT call (mostly calling kgss_accept_sec_context()).
 681  */
 682 static enum auth_stat
 683 do_gss_accept(
 684         SVCXPRT *xprt,
 685         rpc_gss_init_arg *call_arg,
 686         struct rpc_msg *msg,
 687         svc_rpc_gss_data *client_data,
 688         uint_t cr_version,
 689         rpc_gss_service_t cr_service)
 690 {
 691         rpc_gss_init_res        call_res;
 692         gss_buffer_desc         output_token;
 693         OM_uint32               gssstat, minor, minor_stat, time_rec;
 694         int                     ret_flags, ret;
 695         gss_OID                 mech_type = GSS_C_NULL_OID;
 696         int                     free_mech_type = 1;
 697         struct svc_req          r, *rqst;
 698 
 699         rqst = &r;
 700         rqst->rq_xprt = xprt;
 701 
 702         /*
 703          * Initialize output_token.
 704          */
 705         output_token.length = 0;
 706         output_token.value = NULL;
 707 
 708         bzero((char *)&call_res, sizeof (call_res));
 709 
 710         mutex_enter(&client_data->clm);
 711         if (client_data->stale) {
 712                 ret = RPCSEC_GSS_NOCRED;
 713                 RPCGSS_LOG0(1, "_svcrpcsec_gss: client data stale\n");
 714                 goto error2;
 715         }
 716 
 717         /*
 718          * Any response we send will use ctx_handle, so set it now;
 719          * also set seq_window since this won't change.
 720          */
 721         call_res.ctx_handle.length = sizeof (client_data->key);
 722         call_res.ctx_handle.value = (char *)&client_data->key;
 723         call_res.seq_window = SEQ_WIN;
 724 
 725         gssstat = GSS_S_FAILURE;
 726         minor = 0;
 727         minor_stat = 0;
 728         rw_enter(&cred_lock, RW_READER);
 729 
 730         if (client_data->client_name.length) {
 731                 (void) gss_release_buffer(&minor,
 732                     &client_data->client_name);
 733         }
 734         gssstat = kgss_accept_sec_context(&minor_stat,
 735             &client_data->context,
 736             GSS_C_NO_CREDENTIAL,
 737             call_arg,
 738             GSS_C_NO_CHANNEL_BINDINGS,
 739             &client_data->client_name,
 740             &mech_type,
 741             &output_token,
 742             &ret_flags,
 743             &time_rec,
 744             NULL,               /* don't need a delegated cred back */
 745             crgetuid(CRED()));
 746 
 747         RPCGSS_LOG(4, "gssstat 0x%x \n", gssstat);
 748 
 749         if (gssstat == GSS_S_COMPLETE) {
 750                 /*
 751                  * Set the raw and unix credentials at this
 752                  * point.  This saves a lot of computation
 753                  * later when credentials are retrieved.
 754                  */
 755                 client_data->raw_cred.version = cr_version;
 756                 client_data->raw_cred.service = cr_service;
 757 
 758                 if (client_data->raw_cred.mechanism) {
 759                         kgss_free_oid(client_data->raw_cred.mechanism);
 760                         client_data->raw_cred.mechanism = NULL;
 761                 }
 762                 client_data->raw_cred.mechanism = (rpc_gss_OID) mech_type;
 763                 /*
 764                  * client_data is now responsible for freeing
 765                  * the data of 'mech_type'.
 766                  */
 767                 free_mech_type = 0;
 768 
 769                 if (client_data->raw_cred.client_principal) {
 770                         kmem_free((caddr_t)client_data->\
 771                             raw_cred.client_principal,
 772                             client_data->raw_cred.\
 773                             client_principal->len + sizeof (int));
 774                         client_data->raw_cred.client_principal = NULL;
 775                 }
 776 
 777                 /*
 778                  *  The client_name returned from
 779                  *  kgss_accept_sec_context() is in an
 780                  *  exported flat format.
 781                  */
 782                 if (! __rpc_gss_make_principal(
 783                     &client_data->raw_cred.client_principal,
 784                     &client_data->client_name)) {
 785                         RPCGSS_LOG0(1, "_svcrpcsec_gss: "
 786                             "make principal failed\n");
 787                         gssstat = GSS_S_FAILURE;
 788                         (void) gss_release_buffer(&minor_stat, &output_token);
 789                 }
 790         }
 791 
 792         rw_exit(&cred_lock);
 793 
 794         call_res.gss_major = gssstat;
 795         call_res.gss_minor = minor_stat;
 796 
 797         if (gssstat != GSS_S_COMPLETE &&
 798             gssstat != GSS_S_CONTINUE_NEEDED) {
 799                 call_res.ctx_handle.length = 0;
 800                 call_res.ctx_handle.value = NULL;
 801                 call_res.seq_window = 0;
 802                 rpc_gss_display_status(gssstat, minor_stat, mech_type,
 803                     crgetuid(CRED()),
 804                     "_svc_rpcsec_gss gss_accept_sec_context");
 805                 (void) svc_sendreply(rqst->rq_xprt,
 806                     __xdr_rpc_gss_init_res, (caddr_t)&call_res);
 807                 client_data->stale = TRUE;
 808                 ret = AUTH_OK;
 809                 goto error2;
 810         }
 811 
 812         /*
 813          * If appropriate, set established to TRUE *after* sending
 814          * response (otherwise, the client will receive the final
 815          * token encrypted)
 816          */
 817         if (gssstat == GSS_S_COMPLETE) {
 818                 /*
 819                  * Context is established.  Set expiration time
 820                  * for the context.
 821                  */
 822                 client_data->seq_num = 1;
 823                 if ((time_rec == GSS_C_INDEFINITE) || (time_rec == 0)) {
 824                         client_data->expiration = GSS_C_INDEFINITE;
 825                 } else {
 826                         client_data->expiration =
 827                             time_rec + gethrestime_sec();
 828                 }
 829 
 830                 if (!transfer_sec_context(client_data)) {
 831                         ret = RPCSEC_GSS_FAILED;
 832                         client_data->stale = TRUE;
 833                         RPCGSS_LOG0(1,
 834                             "_svc_rpcsec_gss: transfer sec context failed\n");
 835                         goto error2;
 836                 }
 837 
 838                 client_data->established = TRUE;
 839         }
 840 
 841         /*
 842          * This step succeeded.  Send a response, along with
 843          * a token if there's one.  Don't dispatch.
 844          */
 845 
 846         if (output_token.length != 0)
 847                 GSS_COPY_BUFFER(call_res.token, output_token);
 848 
 849         /*
 850          * If GSS_S_COMPLETE: set response verifier to
 851          * checksum of SEQ_WIN
 852          */
 853         if (gssstat == GSS_S_COMPLETE) {
 854                 if (!set_response_verf(rqst, msg, client_data,
 855                     (uint_t)SEQ_WIN)) {
 856                         ret = RPCSEC_GSS_FAILED;
 857                         client_data->stale = TRUE;
 858                         RPCGSS_LOG0(1,
 859                             "_svc_rpcsec_gss:set response verifier failed\n");
 860                         goto error2;
 861                 }
 862         }
 863 
 864         if (!svc_sendreply(rqst->rq_xprt, __xdr_rpc_gss_init_res,
 865             (caddr_t)&call_res)) {
 866                 ret = RPCSEC_GSS_FAILED;
 867                 client_data->stale = TRUE;
 868                 RPCGSS_LOG0(1, "_svc_rpcsec_gss:send reply failed\n");
 869                 goto error2;
 870         }
 871 
 872         /*
 873          * Cache last response in case it is lost and the client
 874          * retries on an established context.
 875          */
 876         (void) retrans_add(client_data, msg->rm_xid, &call_res);
 877         ASSERT(client_data->ref_cnt > 0);
 878         client_data->ref_cnt--;
 879         mutex_exit(&client_data->clm);
 880 
 881         (void) gss_release_buffer(&minor_stat, &output_token);
 882 
 883         return (AUTH_OK);
 884 
 885 error2:
 886         ASSERT(client_data->ref_cnt > 0);
 887         client_data->ref_cnt--;
 888         mutex_exit(&client_data->clm);
 889         (void) gss_release_buffer(&minor_stat, &output_token);
 890         if (free_mech_type && mech_type)
 891                 kgss_free_oid(mech_type);
 892 
 893         return (ret);
 894 }
 895 
 896 static void
 897 svcrpcsec_gss_taskq_func(void *svcrpcsecgss_taskq_arg)
 898 {
 899         enum auth_stat retval;
 900         svcrpcsec_gss_taskq_arg_t *arg = svcrpcsecgss_taskq_arg;
 901 
 902         retval = do_gss_accept(arg->rq_xprt, arg->rpc_call_arg, arg->msg,
 903             arg->client_data, arg->cr_version, arg->cr_service);
 904         if (retval != AUTH_OK) {
 905                 cmn_err(CE_NOTE,
 906                     "svcrpcsec_gss_taskq_func:  do_gss_accept fail 0x%x",
 907                     retval);
 908         }
 909         rpc_msg_free(&arg->msg, MAX_AUTH_BYTES);
 910         svc_clone_unlink(arg->rq_xprt);
 911         svc_clone_free(arg->rq_xprt);
 912         xdr_free(__xdr_rpc_gss_init_arg, (caddr_t)arg->rpc_call_arg);
 913         kmem_free(arg->rpc_call_arg, sizeof (*arg->rpc_call_arg));
 914 
 915         kmem_free(arg, sizeof (*arg));
 916 }
 917 
 918 static enum auth_stat
 919 rpcsec_gss_init(
 920         struct svc_req          *rqst,
 921         struct rpc_msg          *msg,
 922         rpc_gss_creds           creds,
 923         bool_t                  *no_dispatch,
 924         svc_rpc_gss_data        *c_d) /* client data, can be NULL */
 925 {
 926         svc_rpc_gss_data        *client_data;
 927         int ret;
 928         svcrpcsec_gss_taskq_arg_t *arg;
 929 
 930         if (creds.ctx_handle.length != 0) {
 931                 RPCGSS_LOG0(1, "_svcrpcsec_gss: ctx_handle not null\n");
 932                 ret = AUTH_BADCRED;
 933                 return (ret);
 934         }
 935 
 936         client_data = c_d ? c_d : create_client();
 937         if (client_data == NULL) {
 938                 RPCGSS_LOG0(1,
 939                     "_svcrpcsec_gss: can't create a new cache entry\n");
 940                 ret = AUTH_FAILED;
 941                 return (ret);
 942         }
 943 
 944         mutex_enter(&client_data->clm);
 945         if (client_data->stale) {
 946                 ret = RPCSEC_GSS_NOCRED;
 947                 RPCGSS_LOG0(1, "_svcrpcsec_gss: client data stale\n");
 948                 goto error2;
 949         }
 950 
 951         /*
 952          * kgss_accept_sec_context()/gssd(1M) can be overly time
 953          * consuming so let's queue it and return asap.
 954          *
 955          * taskq func must free arg.
 956          */
 957         arg = kmem_alloc(sizeof (*arg), KM_SLEEP);
 958 
 959         /* taskq func must free rpc_call_arg & deserialized arguments */
 960         arg->rpc_call_arg = kmem_zalloc(sizeof (*arg->rpc_call_arg), KM_SLEEP);
 961 
 962         /* deserialize arguments */
 963         if (!SVC_GETARGS(rqst->rq_xprt, __xdr_rpc_gss_init_arg,
 964             (caddr_t)arg->rpc_call_arg)) {
 965                 ret = RPCSEC_GSS_FAILED;
 966                 client_data->stale = TRUE;
 967                 goto error2;
 968         }
 969 
 970         /* get a xprt clone for taskq thread, taskq func must free it */
 971         arg->rq_xprt = svc_clone_init();
 972         svc_clone_link(rqst->rq_xprt->xp_master, arg->rq_xprt, rqst->rq_xprt);
 973         arg->rq_xprt->xp_xid = rqst->rq_xprt->xp_xid;
 974 
 975 
 976         /* set the appropriate wrap/unwrap routine for RPCSEC_GSS */
 977         arg->rq_xprt->xp_auth.svc_ah_ops = svc_rpc_gss_ops;
 978         arg->rq_xprt->xp_auth.svc_ah_private = (caddr_t)client_data;
 979 
 980         /* get a dup of rpc msg for taskq thread */
 981         arg->msg = rpc_msg_dup(msg);  /* taskq func must free msg dup */
 982 
 983         arg->client_data = client_data;
 984         arg->cr_version = creds.version;
 985         arg->cr_service = creds.service;
 986 
 987         /* We no longer need the xp_xdrin, destroy it all here. */
 988         XDR_DESTROY(&(rqst->rq_xprt->xp_xdrin));
 989 
 990         /* should be ok to hold clm lock as taskq will have new thread(s) */
 991         ret = ddi_taskq_dispatch(svcrpcsec_gss_init_taskq,
 992             svcrpcsec_gss_taskq_func, arg, DDI_SLEEP);
 993         if (ret == DDI_FAILURE) {
 994                 cmn_err(CE_NOTE, "rpcsec_gss_init: taskq dispatch fail");
 995                 ret = RPCSEC_GSS_FAILED;
 996                 rpc_msg_free(&arg->msg, MAX_AUTH_BYTES);
 997                 svc_clone_unlink(arg->rq_xprt);
 998                 svc_clone_free(arg->rq_xprt);
 999                 kmem_free(arg, sizeof (*arg));
1000                 goto error2;
1001         }
1002 
1003         mutex_exit(&client_data->clm);
1004         *no_dispatch = TRUE;
1005         return (AUTH_OK);
1006 
1007 error2:
1008         ASSERT(client_data->ref_cnt > 0);
1009         client_data->ref_cnt--;
1010         mutex_exit(&client_data->clm);
1011         cmn_err(CE_NOTE, "rpcsec_gss_init: error 0x%x", ret);
1012         return (ret);
1013 }
1014 
1015 static enum auth_stat
1016 rpcsec_gss_continue_init(
1017         struct svc_req          *rqst,
1018         struct rpc_msg          *msg,
1019         rpc_gss_creds           creds,
1020         bool_t                  *no_dispatch)
1021 {
1022         int ret;
1023         svc_rpc_gss_data        *client_data;
1024         svc_rpc_gss_parms_t     *gss_parms;
1025         rpc_gss_init_res        *retrans_result;
1026 
1027         if (creds.ctx_handle.length == 0) {
1028                 RPCGSS_LOG0(1, "_svcrpcsec_gss: no ctx_handle\n");
1029                 ret = AUTH_BADCRED;
1030                 return (ret);
1031         }
1032         if ((client_data = get_client(&creds.ctx_handle)) == NULL) {
1033                 ret = RPCSEC_GSS_NOCRED;
1034                 RPCGSS_LOG0(1, "_svcrpcsec_gss: no security context\n");
1035                 return (ret);
1036         }
1037 
1038         mutex_enter(&client_data->clm);
1039         if (client_data->stale) {
1040                 ret = RPCSEC_GSS_NOCRED;
1041                 RPCGSS_LOG0(1, "_svcrpcsec_gss: client data stale\n");
1042                 goto error2;
1043         }
1044 
1045         /*
1046          * If context not established, go thru INIT code but with
1047          * this client handle.
1048          */
1049         if (!client_data->established) {
1050                 mutex_exit(&client_data->clm);
1051                 return (rpcsec_gss_init(rqst, msg, creds, no_dispatch,
1052                     client_data));
1053         }
1054 
1055         /*
1056          * Set the appropriate wrap/unwrap routine for RPCSEC_GSS.
1057          */
1058         rqst->rq_xprt->xp_auth.svc_ah_ops = svc_rpc_gss_ops;
1059         rqst->rq_xprt->xp_auth.svc_ah_private = (caddr_t)client_data;
1060 
1061         /*
1062          * Keep copy of parameters we'll need for response, for the
1063          * sake of reentrancy (we don't want to look in the context
1064          * data because when we are sending a response, another
1065          * request may have come in).
1066          */
1067         gss_parms = &rqst->rq_xprt->xp_auth.svc_gss_parms;
1068         gss_parms->established = client_data->established;
1069         gss_parms->service = creds.service;
1070         gss_parms->qop_rcvd = (uint_t)client_data->qop;
1071         gss_parms->context = (void *)client_data->context;
1072         gss_parms->seq_num = creds.seq_num;
1073 
1074         /*
1075          * This is an established context. Continue to
1076          * satisfy retried continue init requests out of
1077          * the retransmit cache.  Throw away any that don't
1078          * have a matching xid or the cach is empty.
1079          * Delete the retransmit cache once the client sends
1080          * a data request.
1081          */
1082         if (client_data->retrans_data &&
1083             (client_data->retrans_data->xid == msg->rm_xid)) {
1084                 retrans_result = &client_data->retrans_data->result;
1085                 if (set_response_verf(rqst, msg, client_data,
1086                     (uint_t)retrans_result->seq_window)) {
1087                         gss_parms->established = FALSE;
1088                         (void) svc_sendreply(rqst->rq_xprt,
1089                             __xdr_rpc_gss_init_res, (caddr_t)retrans_result);
1090                         *no_dispatch = TRUE;
1091                         ASSERT(client_data->ref_cnt > 0);
1092                         client_data->ref_cnt--;
1093                 }
1094         }
1095         mutex_exit(&client_data->clm);
1096 
1097         return (AUTH_OK);
1098 
1099 error2:
1100         ASSERT(client_data->ref_cnt > 0);
1101         client_data->ref_cnt--;
1102         mutex_exit(&client_data->clm);
1103         return (ret);
1104 }
1105 
1106 static enum auth_stat
1107 rpcsec_gss_data(
1108         struct svc_req          *rqst,
1109         struct rpc_msg          *msg,
1110         rpc_gss_creds           creds,
1111         bool_t                  *no_dispatch)
1112 {
1113         int ret;
1114         svc_rpc_gss_parms_t     *gss_parms;
1115         svc_rpc_gss_data        *client_data;
1116 
1117         switch (creds.service) {
1118         case rpc_gss_svc_none:
1119         case rpc_gss_svc_integrity:
1120         case rpc_gss_svc_privacy:
1121                 break;
1122         default:
1123                 cmn_err(CE_NOTE, "__svcrpcsec_gss: unknown service type=0x%x",
1124                     creds.service);
1125                 RPCGSS_LOG(1, "_svcrpcsec_gss: unknown service type: 0x%x\n",
1126                     creds.service);
1127                 ret = AUTH_BADCRED;
1128                 return (ret);
1129         }
1130 
1131         if (creds.ctx_handle.length == 0) {
1132                 RPCGSS_LOG0(1, "_svcrpcsec_gss: no ctx_handle\n");
1133                 ret = AUTH_BADCRED;
1134                 return (ret);
1135         }
1136         if ((client_data = get_client(&creds.ctx_handle)) == NULL) {
1137                 ret = RPCSEC_GSS_NOCRED;
1138                 RPCGSS_LOG0(1, "_svcrpcsec_gss: no security context\n");
1139                 return (ret);
1140         }
1141 
1142 
1143         mutex_enter(&client_data->clm);
1144         if (!client_data->established) {
1145                 ret = AUTH_FAILED;
1146                 goto error2;
1147         }
1148         if (client_data->stale) {
1149                 ret = RPCSEC_GSS_NOCRED;
1150                 RPCGSS_LOG0(1, "_svcrpcsec_gss: client data stale\n");
1151                 goto error2;
1152         }
1153 
1154         /*
1155          * Once the context is established and there is no more
1156          * retransmission of last continue init request, it is safe
1157          * to delete the retransmit cache entry.
1158          */
1159         if (client_data->retrans_data)
1160                 retrans_del(client_data);
1161 
1162         /*
1163          * Set the appropriate wrap/unwrap routine for RPCSEC_GSS.
1164          */
1165         rqst->rq_xprt->xp_auth.svc_ah_ops = svc_rpc_gss_ops;
1166         rqst->rq_xprt->xp_auth.svc_ah_private = (caddr_t)client_data;
1167 
1168         /*
1169          * Keep copy of parameters we'll need for response, for the
1170          * sake of reentrancy (we don't want to look in the context
1171          * data because when we are sending a response, another
1172          * request may have come in).
1173          */
1174         gss_parms = &rqst->rq_xprt->xp_auth.svc_gss_parms;
1175         gss_parms->established = client_data->established;
1176         gss_parms->service = creds.service;
1177         gss_parms->qop_rcvd = (uint_t)client_data->qop;
1178         gss_parms->context = (void *)client_data->context;
1179         gss_parms->seq_num = creds.seq_num;
1180 
1181         /*
1182          * Context is already established.  Check verifier, and
1183          * note parameters we will need for response in gss_parms.
1184          */
1185         if (!check_verf(msg, client_data->context,
1186             (int *)&gss_parms->qop_rcvd, client_data->u_cred.uid)) {
1187                 ret = RPCSEC_GSS_NOCRED;
1188                 RPCGSS_LOG0(1, "_svcrpcsec_gss: check verf failed\n");
1189                 goto error2;
1190         }
1191 
1192         /*
1193          *  Check and invoke callback if necessary.
1194          */
1195         if (!client_data->done_docallback) {
1196                 client_data->done_docallback = TRUE;
1197                 client_data->qop = gss_parms->qop_rcvd;
1198                 client_data->raw_cred.qop = gss_parms->qop_rcvd;
1199                 client_data->raw_cred.service = creds.service;
1200                 if (!do_callback(rqst, client_data)) {
1201                         ret = AUTH_FAILED;
1202                         RPCGSS_LOG0(1, "_svc_rpcsec_gss:callback failed\n");
1203                         goto error2;
1204                 }
1205         }
1206 
1207         /*
1208          * If the context was locked, make sure that the client
1209          * has not changed QOP.
1210          */
1211         if (client_data->locked && gss_parms->qop_rcvd != client_data->qop) {
1212                 ret = AUTH_BADVERF;
1213                 RPCGSS_LOG0(1, "_svcrpcsec_gss: can not change qop\n");
1214                 goto error2;
1215         }
1216 
1217         /*
1218          * Validate sequence number.
1219          */
1220         if (!check_seq(client_data, creds.seq_num, &client_data->stale)) {
1221                 if (client_data->stale) {
1222                         ret = RPCSEC_GSS_FAILED;
1223                         RPCGSS_LOG0(1,
1224                             "_svc_rpcsec_gss:check seq failed\n");
1225                 } else {
1226                         RPCGSS_LOG0(4, "_svc_rpcsec_gss:check seq "
1227                             "failed on good context. Ignoring "
1228                             "request\n");
1229                         /*
1230                          * Operational error, drop packet silently.
1231                          * The client will recover after timing out,
1232                          * assuming this is a client error and not
1233                          * a relpay attack.  Don't dispatch.
1234                          */
1235                         ret = AUTH_OK;
1236                         *no_dispatch = TRUE;
1237                 }
1238                 goto error2;
1239         }
1240 
1241         /*
1242          * set response verifier
1243          */
1244         if (!set_response_verf(rqst, msg, client_data, creds.seq_num)) {
1245                 ret = RPCSEC_GSS_FAILED;
1246                 client_data->stale = TRUE;
1247                 RPCGSS_LOG0(1,
1248                     "_svc_rpcsec_gss:set response verifier failed\n");
1249                 goto error2;
1250         }
1251 
1252         /*
1253          * If context is locked, make sure that the client
1254          * has not changed the security service.
1255          */
1256         if (client_data->locked &&
1257             client_data->raw_cred.service != creds.service) {
1258                 RPCGSS_LOG0(1, "_svc_rpcsec_gss: "
1259                     "security service changed.\n");
1260                 ret = AUTH_FAILED;
1261                 goto error2;
1262         }
1263 
1264         /*
1265          * Set client credentials to raw credential
1266          * structure in context.  This is okay, since
1267          * this will not change during the lifetime of
1268          * the context (so it's MT safe).
1269          */
1270         rqst->rq_clntcred = (char *)&client_data->raw_cred;
1271 
1272         mutex_exit(&client_data->clm);
1273         return (AUTH_OK);
1274 
1275 error2:
1276         ASSERT(client_data->ref_cnt > 0);
1277         client_data->ref_cnt--;
1278         mutex_exit(&client_data->clm);
1279         return (ret);
1280 }
1281 
1282 /*
1283  * Note we don't have a client yet to use this routine and test it.
1284  */
1285 static enum auth_stat
1286 rpcsec_gss_destroy(
1287         struct svc_req          *rqst,
1288         rpc_gss_creds           creds,
1289         bool_t                  *no_dispatch)
1290 {
1291         svc_rpc_gss_data        *client_data;
1292         int ret;
1293 
1294         if (creds.ctx_handle.length == 0) {
1295                 RPCGSS_LOG0(1, "_svcrpcsec_gss: no ctx_handle\n");
1296                 ret = AUTH_BADCRED;
1297                 return (ret);
1298         }
1299         if ((client_data = get_client(&creds.ctx_handle)) == NULL) {
1300                 ret = RPCSEC_GSS_NOCRED;
1301                 RPCGSS_LOG0(1, "_svcrpcsec_gss: no security context\n");
1302                 return (ret);
1303         }
1304 
1305         mutex_enter(&client_data->clm);
1306         if (!client_data->established) {
1307                 ret = AUTH_FAILED;
1308                 goto error2;
1309         }
1310         if (client_data->stale) {
1311                 ret = RPCSEC_GSS_NOCRED;
1312                 RPCGSS_LOG0(1, "_svcrpcsec_gss: client data stale\n");
1313                 goto error2;
1314         }
1315 
1316         (void) svc_sendreply(rqst->rq_xprt, xdr_void, NULL);
1317         *no_dispatch = TRUE;
1318         ASSERT(client_data->ref_cnt > 0);
1319         client_data->ref_cnt--;
1320         client_data->stale = TRUE;
1321         mutex_exit(&client_data->clm);
1322         return (AUTH_OK);
1323 
1324 error2:
1325         ASSERT(client_data->ref_cnt > 0);
1326         client_data->ref_cnt--;
1327         client_data->stale = TRUE;
1328         mutex_exit(&client_data->clm);
1329         return (ret);
1330 }
1331 
1332 /*
1333  * Server side authentication for RPCSEC_GSS.
1334  */
1335 enum auth_stat
1336 __svcrpcsec_gss(
1337         struct svc_req          *rqst,
1338         struct rpc_msg          *msg,
1339         bool_t                  *no_dispatch)
1340 {
1341         XDR                     xdrs;
1342         rpc_gss_creds           creds;
1343         struct opaque_auth      *cred;
1344         int                     ret;
1345 
1346         *no_dispatch = FALSE;
1347 
1348         /*
1349          * Initialize response verifier to NULL verifier.  If
1350          * necessary, this will be changed later.
1351          */
1352         rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NONE;
1353         rqst->rq_xprt->xp_verf.oa_base = NULL;
1354         rqst->rq_xprt->xp_verf.oa_length = 0;
1355 
1356         /*
1357          * Pull out and check credential and verifier.
1358          */
1359         cred = &msg->rm_call.cb_cred;
1360 
1361         if (cred->oa_length == 0) {
1362                 RPCGSS_LOG0(1, "_svcrpcsec_gss: zero length cred\n");
1363                 return (AUTH_BADCRED);
1364         }
1365 
1366         xdrmem_create(&xdrs, cred->oa_base, cred->oa_length, XDR_DECODE);
1367         bzero((char *)&creds, sizeof (creds));
1368         if (!__xdr_rpc_gss_creds(&xdrs, &creds)) {
1369                 XDR_DESTROY(&xdrs);
1370                 RPCGSS_LOG0(1, "_svcrpcsec_gss: can't decode creds\n");
1371                 ret = AUTH_BADCRED;
1372                 return (AUTH_BADCRED);
1373         }
1374         XDR_DESTROY(&xdrs);
1375 
1376         switch (creds.gss_proc) {
1377         case RPCSEC_GSS_INIT:
1378                 ret = rpcsec_gss_init(rqst, msg, creds, no_dispatch, NULL);
1379                 break;
1380         case RPCSEC_GSS_CONTINUE_INIT:
1381                 ret = rpcsec_gss_continue_init(rqst, msg, creds, no_dispatch);
1382                 break;
1383         case RPCSEC_GSS_DATA:
1384                 ret = rpcsec_gss_data(rqst, msg, creds, no_dispatch);
1385                 break;
1386         case RPCSEC_GSS_DESTROY:
1387                 ret = rpcsec_gss_destroy(rqst, creds, no_dispatch);
1388                 break;
1389         default:
1390                 cmn_err(CE_NOTE, "__svcrpcsec_gss: bad proc=%d",
1391                     creds.gss_proc);
1392                 ret = AUTH_BADCRED;
1393         }
1394 
1395         if (creds.ctx_handle.length != 0)
1396                 xdr_free(__xdr_rpc_gss_creds, (caddr_t)&creds);
1397         return (ret);
1398 }
1399 
1400 /*
1401  * Check verifier.  The verifier is the checksum of the RPC header
1402  * upto and including the credentials field.
1403  */
1404 
1405 /* ARGSUSED */
1406 static bool_t
1407 check_verf(struct rpc_msg *msg, gss_ctx_id_t context, int *qop_state, uid_t uid)
1408 {
1409         int                     *buf, *tmp;
1410         char                    hdr[128];
1411         struct opaque_auth      *oa;
1412         int                     len;
1413         gss_buffer_desc         msg_buf;
1414         gss_buffer_desc         tok_buf;
1415         OM_uint32               gssstat, minor_stat;
1416 
1417         /*
1418          * We have to reconstruct the RPC header from the previously
1419          * parsed information, since we haven't kept the header intact.
1420          */
1421 
1422         oa = &msg->rm_call.cb_cred;
1423         if (oa->oa_length > MAX_AUTH_BYTES)
1424                 return (FALSE);
1425 
1426         /* 8 XDR units from the IXDR macro calls. */
1427         if (sizeof (hdr) < (8 * BYTES_PER_XDR_UNIT +
1428             RNDUP(oa->oa_length)))
1429                 return (FALSE);
1430         buf = (int *)hdr;
1431         IXDR_PUT_U_INT32(buf, msg->rm_xid);
1432         IXDR_PUT_ENUM(buf, msg->rm_direction);
1433         IXDR_PUT_U_INT32(buf, msg->rm_call.cb_rpcvers);
1434         IXDR_PUT_U_INT32(buf, msg->rm_call.cb_prog);
1435         IXDR_PUT_U_INT32(buf, msg->rm_call.cb_vers);
1436         IXDR_PUT_U_INT32(buf, msg->rm_call.cb_proc);
1437         IXDR_PUT_ENUM(buf, oa->oa_flavor);
1438         IXDR_PUT_U_INT32(buf, oa->oa_length);
1439         if (oa->oa_length) {
1440                 len = RNDUP(oa->oa_length);
1441                 tmp = buf;
1442                 buf += len / sizeof (int);
1443                 *(buf - 1) = 0;
1444                 (void) bcopy(oa->oa_base, (caddr_t)tmp, oa->oa_length);
1445         }
1446         len = ((char *)buf) - hdr;
1447         msg_buf.length = len;
1448         msg_buf.value = hdr;
1449         oa = &msg->rm_call.cb_verf;
1450         tok_buf.length = oa->oa_length;
1451         tok_buf.value = oa->oa_base;
1452 
1453         gssstat = kgss_verify(&minor_stat, context, &msg_buf, &tok_buf,
1454             qop_state);
1455         if (gssstat != GSS_S_COMPLETE) {
1456                 RPCGSS_LOG(1, "check_verf: kgss_verify status 0x%x\n", gssstat);
1457 
1458                 RPCGSS_LOG(4, "check_verf: msg_buf length %d\n", len);
1459                 RPCGSS_LOG(4, "check_verf: msg_buf value 0x%x\n", *(int *)hdr);
1460                 RPCGSS_LOG(4, "check_verf: tok_buf length %ld\n",
1461                     tok_buf.length);
1462                 RPCGSS_LOG(4, "check_verf: tok_buf value 0x%p\n",
1463                     (void *)oa->oa_base);
1464                 RPCGSS_LOG(4, "check_verf: context 0x%p\n", (void *)context);
1465 
1466                 return (FALSE);
1467         }
1468         return (TRUE);
1469 }
1470 
1471 
1472 /*
1473  * Set response verifier.  This is the checksum of the given number.
1474  * (e.g. sequence number or sequence window)
1475  */
1476 static bool_t
1477 set_response_verf(rqst, msg, cl, num)
1478         struct svc_req          *rqst;
1479         struct rpc_msg          *msg;
1480         svc_rpc_gss_data        *cl;
1481         uint_t                  num;
1482 {
1483         OM_uint32               minor;
1484         gss_buffer_desc         in_buf, out_buf;
1485         uint_t                  num_net;
1486 
1487         num_net = (uint_t)htonl(num);
1488         in_buf.length = sizeof (num);
1489         in_buf.value = (char *)&num_net;
1490 /* XXX uid ? */
1491 
1492         if ((kgss_sign(&minor, cl->context, cl->qop, &in_buf,
1493                                 &out_buf)) != GSS_S_COMPLETE)
1494                 return (FALSE);
1495 
1496         rqst->rq_xprt->xp_verf.oa_flavor = RPCSEC_GSS;
1497         rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
1498         rqst->rq_xprt->xp_verf.oa_length = out_buf.length;
1499         bcopy(out_buf.value, rqst->rq_xprt->xp_verf.oa_base, out_buf.length);
1500         (void) gss_release_buffer(&minor, &out_buf);
1501         return (TRUE);
1502 }
1503 
1504 /*
1505  * Create client context.
1506  */
1507 static svc_rpc_gss_data *
1508 create_client()
1509 {
1510         svc_rpc_gss_data        *client_data;
1511         static uint_t           key = 1;
1512 
1513         client_data = (svc_rpc_gss_data *) kmem_cache_alloc(svc_data_handle,
1514             KM_SLEEP);
1515         if (client_data == NULL)
1516                 return (NULL);
1517 
1518         /*
1519          * set up client data structure
1520          */
1521         client_data->next = NULL;
1522         client_data->prev = NULL;
1523         client_data->lru_next = NULL;
1524         client_data->lru_prev = NULL;
1525         client_data->client_name.length = 0;
1526         client_data->client_name.value = NULL;
1527         client_data->seq_num = 0;
1528         bzero(client_data->seq_bits, sizeof (client_data->seq_bits));
1529         client_data->key = 0;
1530         client_data->cookie = NULL;
1531         bzero(&client_data->u_cred, sizeof (client_data->u_cred));
1532         client_data->established = FALSE;
1533         client_data->locked = FALSE;
1534         client_data->u_cred_set = 0;
1535         client_data->context = GSS_C_NO_CONTEXT;
1536         client_data->expiration = GSS_C_INDEFINITE;
1537         client_data->deleg = GSS_C_NO_CREDENTIAL;
1538         client_data->ref_cnt = 1;
1539         client_data->last_ref_time = gethrestime_sec();
1540         client_data->qop = GSS_C_QOP_DEFAULT;
1541         client_data->done_docallback = FALSE;
1542         client_data->stale = FALSE;
1543         client_data->retrans_data = NULL;
1544         bzero(&client_data->raw_cred, sizeof (client_data->raw_cred));
1545 
1546         /*
1547          * The client context handle is a 32-bit key (unsigned int).
1548          * The key is incremented until there is no duplicate for it.
1549          */
1550 
1551         svc_rpc_gss_cache_stats.total_entries_allocated++;
1552         mutex_enter(&ctx_mutex);
1553         for (;;) {
1554                 client_data->key = key++;
1555                 if (find_client(client_data->key) == NULL) {
1556                         insert_client(client_data);
1557                         mutex_exit(&ctx_mutex);
1558                         return (client_data);
1559                 }
1560         }
1561         /*NOTREACHED*/
1562 }
1563 
1564 /*
1565  * Insert client context into hash list and LRU list.
1566  */
1567 static void
1568 insert_client(client_data)
1569         svc_rpc_gss_data        *client_data;
1570 {
1571         svc_rpc_gss_data        *cl;
1572         int                     index = HASH(client_data->key);
1573 
1574         ASSERT(mutex_owned(&ctx_mutex));
1575 
1576         client_data->prev = NULL;
1577         cl = clients[index];
1578         if ((client_data->next = cl) != NULL)
1579                 cl->prev = client_data;
1580         clients[index] = client_data;
1581 
1582         client_data->lru_prev = NULL;
1583         if ((client_data->lru_next = lru_first) != NULL)
1584                 lru_first->lru_prev = client_data;
1585         else
1586                 lru_last = client_data;
1587         lru_first = client_data;
1588 
1589         num_gss_contexts++;
1590 }
1591 
1592 /*
1593  * Fetch a client, given the client context handle.  Move it to the
1594  * top of the LRU list since this is the most recently used context.
1595  */
1596 static svc_rpc_gss_data *
1597 get_client(ctx_handle)
1598         gss_buffer_t            ctx_handle;
1599 {
1600         uint_t                  key = *(uint_t *)ctx_handle->value;
1601         svc_rpc_gss_data        *cl;
1602 
1603         mutex_enter(&ctx_mutex);
1604         if ((cl = find_client(key)) != NULL) {
1605                 mutex_enter(&cl->clm);
1606                 if (cl->stale) {
1607                         if (cl->ref_cnt == 0) {
1608                                 mutex_exit(&cl->clm);
1609                                 destroy_client(cl);
1610                         } else {
1611                                 mutex_exit(&cl->clm);
1612                         }
1613                         mutex_exit(&ctx_mutex);
1614                         return (NULL);
1615                 }
1616                 cl->ref_cnt++;
1617                 cl->last_ref_time = gethrestime_sec();
1618                 mutex_exit(&cl->clm);
1619                 if (cl != lru_first) {
1620                         cl->lru_prev->lru_next = cl->lru_next;
1621                         if (cl->lru_next != NULL)
1622                                 cl->lru_next->lru_prev = cl->lru_prev;
1623                         else
1624                                 lru_last = cl->lru_prev;
1625                         cl->lru_prev = NULL;
1626                         cl->lru_next = lru_first;
1627                         lru_first->lru_prev = cl;
1628                         lru_first = cl;
1629                 }
1630         }
1631         mutex_exit(&ctx_mutex);
1632         return (cl);
1633 }
1634 
1635 /*
1636  * Given the client context handle, find the context corresponding to it.
1637  * Don't change its LRU state since it may not be used.
1638  */
1639 static svc_rpc_gss_data *
1640 find_client(key)
1641         uint_t                  key;
1642 {
1643         int                     index = HASH(key);
1644         svc_rpc_gss_data        *cl = NULL;
1645 
1646         ASSERT(mutex_owned(&ctx_mutex));
1647 
1648         for (cl = clients[index]; cl != NULL; cl = cl->next) {
1649                 if (cl->key == key)
1650                         break;
1651         }
1652         return (cl);
1653 }
1654 
1655 /*
1656  * Destroy a client context.
1657  */
1658 static void
1659 destroy_client(client_data)
1660         svc_rpc_gss_data        *client_data;
1661 {
1662         OM_uint32               minor;
1663         int                     index = HASH(client_data->key);
1664 
1665         ASSERT(mutex_owned(&ctx_mutex));
1666 
1667         /*
1668          * remove from hash list
1669          */
1670         if (client_data->prev == NULL)
1671                 clients[index] = client_data->next;
1672         else
1673                 client_data->prev->next = client_data->next;
1674         if (client_data->next != NULL)
1675                 client_data->next->prev = client_data->prev;
1676 
1677         /*
1678          * remove from LRU list
1679          */
1680         if (client_data->lru_prev == NULL)
1681                 lru_first = client_data->lru_next;
1682         else
1683                 client_data->lru_prev->lru_next = client_data->lru_next;
1684         if (client_data->lru_next != NULL)
1685                 client_data->lru_next->lru_prev = client_data->lru_prev;
1686         else
1687                 lru_last = client_data->lru_prev;
1688 
1689         /*
1690          * If there is a GSS context, clean up GSS state.
1691          */
1692         if (client_data->context != GSS_C_NO_CONTEXT) {
1693                 (void) kgss_delete_sec_context(&minor, &client_data->context,
1694                                         NULL);
1695 
1696                 common_client_data_free(client_data);
1697 
1698                 if (client_data->deleg != GSS_C_NO_CREDENTIAL) {
1699                     (void) kgss_release_cred(&minor, &client_data->deleg,
1700                                 crgetuid(CRED()));
1701                 }
1702         }
1703 
1704         if (client_data->u_cred.gidlist != NULL) {
1705             kmem_free((char *)client_data->u_cred.gidlist,
1706                         client_data->u_cred.gidlen * sizeof (gid_t));
1707             client_data->u_cred.gidlist = NULL;
1708         }
1709         if (client_data->retrans_data != NULL)
1710                 retrans_del(client_data);
1711 
1712         kmem_cache_free(svc_data_handle, client_data);
1713         num_gss_contexts--;
1714 }
1715 
1716 /*
1717  * Check for expired and stale client contexts.
1718  */
1719 static void
1720 sweep_clients(bool_t from_reclaim)
1721 {
1722         svc_rpc_gss_data        *cl, *next;
1723         time_t                  last_reference_needed;
1724         time_t                  now = gethrestime_sec();
1725 
1726         ASSERT(mutex_owned(&ctx_mutex));
1727 
1728         last_reference_needed = now - (from_reclaim ?
1729             svc_rpc_gss_active_delta : svc_rpc_gss_inactive_delta);
1730 
1731         cl = lru_last;
1732         while (cl) {
1733                 /*
1734                  * We assume here that any manipulation of the LRU pointers
1735                  * and hash bucket pointers are only done when holding the
1736                  * ctx_mutex.
1737                  */
1738                 next = cl->lru_prev;
1739 
1740                 mutex_enter(&cl->clm);
1741 
1742                 if ((cl->expiration != GSS_C_INDEFINITE &&
1743                     cl->expiration <= now) || cl->stale ||
1744                     cl->last_ref_time <= last_reference_needed) {
1745 
1746                         if ((cl->expiration != GSS_C_INDEFINITE &&
1747                             cl->expiration <= now) || cl->stale ||
1748                             (cl->last_ref_time <= last_reference_needed &&
1749                             cl->ref_cnt == 0)) {
1750 
1751                                 cl->stale = TRUE;
1752 
1753                                 if (cl->ref_cnt == 0) {
1754                                         mutex_exit(&cl->clm);
1755                                         if (from_reclaim)
1756                                                 svc_rpc_gss_cache_stats.
1757                                                     no_returned_by_reclaim++;
1758                                         destroy_client(cl);
1759                                 } else
1760                                         mutex_exit(&cl->clm);
1761                         } else
1762                                 mutex_exit(&cl->clm);
1763                 } else
1764                         mutex_exit(&cl->clm);
1765 
1766                 cl = next;
1767         }
1768 
1769         last_swept = gethrestime_sec();
1770 }
1771 
1772 /*
1773  * Encrypt the serialized arguments from xdr_func applied to xdr_ptr
1774  * and write the result to xdrs.
1775  */
1776 static bool_t
1777 svc_rpc_gss_wrap(auth, out_xdrs, xdr_func, xdr_ptr)
1778         SVCAUTH                 *auth;
1779         XDR                     *out_xdrs;
1780         bool_t                  (*xdr_func)();
1781         caddr_t                 xdr_ptr;
1782 {
1783         svc_rpc_gss_parms_t     *gss_parms = SVCAUTH_GSSPARMS(auth);
1784         bool_t ret;
1785 
1786         /*
1787          * If context is not established, or if neither integrity nor
1788          * privacy service is used, don't wrap - just XDR encode.
1789          * Otherwise, wrap data using service and QOP parameters.
1790          */
1791         if (!gss_parms->established ||
1792                                 gss_parms->service == rpc_gss_svc_none)
1793                 return ((*xdr_func)(out_xdrs, xdr_ptr));
1794 
1795         ret = __rpc_gss_wrap_data(gss_parms->service,
1796                                 (OM_uint32)gss_parms->qop_rcvd,
1797                                 (gss_ctx_id_t)gss_parms->context,
1798                                 gss_parms->seq_num,
1799                                 out_xdrs, xdr_func, xdr_ptr);
1800         return (ret);
1801 }
1802 
1803 /*
1804  * Decrypt the serialized arguments and XDR decode them.
1805  */
1806 static bool_t
1807 svc_rpc_gss_unwrap(auth, in_xdrs, xdr_func, xdr_ptr)
1808         SVCAUTH                 *auth;
1809         XDR                     *in_xdrs;
1810         bool_t                  (*xdr_func)();
1811         caddr_t                 xdr_ptr;
1812 {
1813         svc_rpc_gss_parms_t     *gss_parms = SVCAUTH_GSSPARMS(auth);
1814 
1815         /*
1816          * If context is not established, or if neither integrity nor
1817          * privacy service is used, don't unwrap - just XDR decode.
1818          * Otherwise, unwrap data.
1819          */
1820         if (!gss_parms->established ||
1821                                 gss_parms->service == rpc_gss_svc_none)
1822                 return ((*xdr_func)(in_xdrs, xdr_ptr));
1823 
1824         return (__rpc_gss_unwrap_data(gss_parms->service,
1825                                 (gss_ctx_id_t)gss_parms->context,
1826                                 gss_parms->seq_num,
1827                                 gss_parms->qop_rcvd,
1828                                 in_xdrs, xdr_func, xdr_ptr));
1829 }
1830 
1831 
1832 /* ARGSUSED */
1833 int
1834 rpc_gss_svc_max_data_length(struct svc_req *req, int max_tp_unit_len)
1835 {
1836         return (0);
1837 }
1838 
1839 /*
1840  * Add retransmit entry to the context cache entry for a new xid.
1841  * If there is already an entry, delete it before adding the new one.
1842  */
1843 static void retrans_add(client, xid, result)
1844         svc_rpc_gss_data *client;
1845         uint32_t        xid;
1846         rpc_gss_init_res *result;
1847 {
1848         retrans_entry   *rdata;
1849 
1850         if (client->retrans_data && client->retrans_data->xid == xid)
1851                 return;
1852 
1853         rdata = kmem_zalloc(sizeof (*rdata), KM_SLEEP);
1854 
1855         if (rdata == NULL)
1856                 return;
1857 
1858         rdata->xid = xid;
1859         rdata->result = *result;
1860 
1861         if (result->token.length != 0) {
1862                 GSS_DUP_BUFFER(rdata->result.token, result->token);
1863         }
1864 
1865         if (client->retrans_data)
1866                 retrans_del(client);
1867 
1868         client->retrans_data = rdata;
1869 }
1870 
1871 /*
1872  * Delete the retransmit data from the context cache entry.
1873  */
1874 static void retrans_del(client)
1875         svc_rpc_gss_data *client;
1876 {
1877         retrans_entry *rdata;
1878         OM_uint32 minor_stat;
1879 
1880         if (client->retrans_data == NULL)
1881                 return;
1882 
1883         rdata = client->retrans_data;
1884         if (rdata->result.token.length != 0) {
1885             (void) gss_release_buffer(&minor_stat, &rdata->result.token);
1886         }
1887 
1888         kmem_free((caddr_t)rdata, sizeof (*rdata));
1889         client->retrans_data = NULL;
1890 }
1891 
1892 /*
1893  * This function frees the following fields of svc_rpc_gss_data:
1894  *      client_name, raw_cred.client_principal, raw_cred.mechanism.
1895  */
1896 static void
1897 common_client_data_free(svc_rpc_gss_data *client_data)
1898 {
1899         if (client_data->client_name.length > 0) {
1900                 (void) gss_release_buffer(NULL, &client_data->client_name);
1901         }
1902 
1903         if (client_data->raw_cred.client_principal) {
1904                 kmem_free((caddr_t)client_data->raw_cred.client_principal,
1905                     client_data->raw_cred.client_principal->len +
1906                     sizeof (int));
1907                 client_data->raw_cred.client_principal = NULL;
1908         }
1909 
1910         /*
1911          * In the user GSS-API library, mechanism (mech_type returned
1912          * by gss_accept_sec_context) is static storage, however
1913          * since all the work is done for gss_accept_sec_context under
1914          * gssd, what is returned in the kernel, is a copy from the oid
1915          * obtained under from gssd, so need to free it when destroying
1916          * the client data.
1917          */
1918 
1919         if (client_data->raw_cred.mechanism) {
1920                 kgss_free_oid(client_data->raw_cred.mechanism);
1921                 client_data->raw_cred.mechanism = NULL;
1922         }
1923 }