17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5158643e0Sps57422 * Common Development and Distribution License (the "License").
6158643e0Sps57422 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21158643e0Sps57422
227c478bd9Sstevel@tonic-gate /*
23dda0f8f7Sgtb * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24158643e0Sps57422 * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * $Id: svc_auth_gssapi.c,v 1.19 1994/10/27 12:38:51 jik Exp $
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate * Server side handling of RPCSEC_GSS flavor.
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
437c478bd9Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
447c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
457c478bd9Sstevel@tonic-gate #include <rpc/rpcsec_defs.h>
467c478bd9Sstevel@tonic-gate #include <sys/file.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <pwd.h>
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <syslog.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate * Sequence window definitions.
547c478bd9Sstevel@tonic-gate */
557c478bd9Sstevel@tonic-gate #define SEQ_ARR_SIZE 4
567c478bd9Sstevel@tonic-gate #define SEQ_WIN (SEQ_ARR_SIZE*32)
577c478bd9Sstevel@tonic-gate #define SEQ_HI_BIT 0x80000000
587c478bd9Sstevel@tonic-gate #define SEQ_LO_BIT 1
597c478bd9Sstevel@tonic-gate #define DIV_BY_32 5
607c478bd9Sstevel@tonic-gate #define SEQ_MASK 0x1f
617c478bd9Sstevel@tonic-gate #define SEQ_MAX 0x80000000
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /* cache retransmit data */
657c478bd9Sstevel@tonic-gate typedef struct _retrans_entry {
667c478bd9Sstevel@tonic-gate uint32_t xid;
677c478bd9Sstevel@tonic-gate rpc_gss_init_res result;
687c478bd9Sstevel@tonic-gate struct _retrans_entry *next, *prev;
697c478bd9Sstevel@tonic-gate } retrans_entry;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * Server side RPCSEC_GSS context information.
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate typedef struct _svc_rpc_gss_data {
757c478bd9Sstevel@tonic-gate struct _svc_rpc_gss_data *next, *prev;
767c478bd9Sstevel@tonic-gate struct _svc_rpc_gss_data *lru_next, *lru_prev;
777c478bd9Sstevel@tonic-gate bool_t established;
787c478bd9Sstevel@tonic-gate gss_ctx_id_t context;
797c478bd9Sstevel@tonic-gate gss_name_t client_name;
807c478bd9Sstevel@tonic-gate gss_cred_id_t server_creds;
817c478bd9Sstevel@tonic-gate uint_t expiration;
827c478bd9Sstevel@tonic-gate uint_t seq_num;
837c478bd9Sstevel@tonic-gate uint_t seq_bits[SEQ_ARR_SIZE];
847c478bd9Sstevel@tonic-gate uint_t key;
857c478bd9Sstevel@tonic-gate OM_uint32 qop;
867c478bd9Sstevel@tonic-gate bool_t done_docallback;
877c478bd9Sstevel@tonic-gate bool_t locked;
887c478bd9Sstevel@tonic-gate rpc_gss_rawcred_t raw_cred;
897c478bd9Sstevel@tonic-gate rpc_gss_ucred_t u_cred;
907c478bd9Sstevel@tonic-gate bool_t u_cred_set;
917c478bd9Sstevel@tonic-gate void *cookie;
927c478bd9Sstevel@tonic-gate gss_cred_id_t deleg;
937c478bd9Sstevel@tonic-gate mutex_t clm;
947c478bd9Sstevel@tonic-gate int ref_cnt;
957c478bd9Sstevel@tonic-gate bool_t stale;
967c478bd9Sstevel@tonic-gate time_t time_secs_set;
977c478bd9Sstevel@tonic-gate retrans_entry *retrans_data;
987c478bd9Sstevel@tonic-gate } svc_rpc_gss_data;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * Data structures used for LRU based context management.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate #define HASHMOD 256
1047c478bd9Sstevel@tonic-gate #define HASHMASK 255
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *clients[HASHMOD];
1077c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *lru_first, *lru_last;
1087c478bd9Sstevel@tonic-gate static int num_gss_contexts = 0;
1097c478bd9Sstevel@tonic-gate static int max_gss_contexts = 128;
1107c478bd9Sstevel@tonic-gate static int sweep_interval = 10;
1117c478bd9Sstevel@tonic-gate static int last_swept = 0;
1127c478bd9Sstevel@tonic-gate static uint_t max_lifetime = GSS_C_INDEFINITE;
113dda0f8f7Sgtb static int init_lifetime = 0;
1147c478bd9Sstevel@tonic-gate static uint_t gid_timeout = 43200; /* 43200 secs = 12 hours */
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * lock used with context/lru variables
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate static mutex_t ctx_mutex = DEFAULTMUTEX;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * server credential management data and structures
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate typedef struct svc_creds_list_s {
1257c478bd9Sstevel@tonic-gate struct svc_creds_list_s *next;
1267c478bd9Sstevel@tonic-gate gss_cred_id_t cred;
1277c478bd9Sstevel@tonic-gate gss_name_t name;
1287c478bd9Sstevel@tonic-gate rpcprog_t program;
1297c478bd9Sstevel@tonic-gate rpcvers_t version;
1307c478bd9Sstevel@tonic-gate gss_OID_set oid_set;
1317c478bd9Sstevel@tonic-gate OM_uint32 req_time;
1327c478bd9Sstevel@tonic-gate char *server_name;
1337c478bd9Sstevel@tonic-gate mutex_t refresh_mutex;
1347c478bd9Sstevel@tonic-gate } svc_creds_list_t;
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate static svc_creds_list_t *svc_creds_list;
1387c478bd9Sstevel@tonic-gate static int svc_creds_count = 0;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * lock used with server credential variables list
1427c478bd9Sstevel@tonic-gate *
1437c478bd9Sstevel@tonic-gate * server cred list locking guidelines:
1447c478bd9Sstevel@tonic-gate * - Writer's lock holder has exclusive access to the list
1457c478bd9Sstevel@tonic-gate * - Reader's lock holder(s) must also lock (refresh_mutex) each node
1467c478bd9Sstevel@tonic-gate * before accessing that node's elements (ie. cred)
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate static rwlock_t cred_lock = DEFAULTRWLOCK;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * server callback list
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate typedef struct cblist_s {
1547c478bd9Sstevel@tonic-gate struct cblist_s *next;
1557c478bd9Sstevel@tonic-gate rpc_gss_callback_t cb;
1567c478bd9Sstevel@tonic-gate } cblist_t;
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate cblist_t *cblist = NULL;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * lock used with callback variables
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate static mutex_t cb_mutex = DEFAULTMUTEX;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * forward declarations
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate static bool_t svc_rpc_gss_wrap();
1697c478bd9Sstevel@tonic-gate static bool_t svc_rpc_gss_unwrap();
1707c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *create_client();
1717c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *get_client();
1727c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *find_client();
1737c478bd9Sstevel@tonic-gate static void destroy_client();
1747c478bd9Sstevel@tonic-gate static void sweep_clients();
1757c478bd9Sstevel@tonic-gate static void drop_lru_client();
1767c478bd9Sstevel@tonic-gate static void insert_client();
1777c478bd9Sstevel@tonic-gate static bool_t check_verf();
1787c478bd9Sstevel@tonic-gate static bool_t rpc_gss_refresh_svc_cred();
1797c478bd9Sstevel@tonic-gate static bool_t set_response_verf();
1807c478bd9Sstevel@tonic-gate static void retrans_add(svc_rpc_gss_data *, uint32_t,
1817c478bd9Sstevel@tonic-gate rpc_gss_init_res *);
1827c478bd9Sstevel@tonic-gate static void retrans_del(struct _svc_rpc_gss_data *);
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate * server side wrap/unwrap routines
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate struct svc_auth_ops svc_rpc_gss_ops = {
1897c478bd9Sstevel@tonic-gate svc_rpc_gss_wrap,
1907c478bd9Sstevel@tonic-gate svc_rpc_gss_unwrap,
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * Fetch server side authentication structure.
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate extern SVCAUTH *__svc_get_svcauth();
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * Cleanup routine for destroying context, called after service
2007c478bd9Sstevel@tonic-gate * procedure is executed, for MT safeness.
2017c478bd9Sstevel@tonic-gate */
2027c478bd9Sstevel@tonic-gate extern void *__svc_set_proc_cleanup_cb();
2037c478bd9Sstevel@tonic-gate static void (*old_cleanup_cb)() = NULL;
2047c478bd9Sstevel@tonic-gate static bool_t cleanup_cb_set = FALSE;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate static void
ctx_cleanup(SVCXPRT * xprt)207*b86a7875SToomas Soome ctx_cleanup(SVCXPRT *xprt)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate svc_rpc_gss_data *cl;
2107c478bd9Sstevel@tonic-gate SVCAUTH *svcauth;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate if (old_cleanup_cb != NULL)
2137c478bd9Sstevel@tonic-gate (*old_cleanup_cb)(xprt);
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate * First check if current context needs to be cleaned up.
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate svcauth = __svc_get_svcauth(xprt);
2197c478bd9Sstevel@tonic-gate /*LINTED*/
2207c478bd9Sstevel@tonic-gate if ((cl = (svc_rpc_gss_data *)svcauth->svc_ah_private) != NULL) {
2217c478bd9Sstevel@tonic-gate mutex_lock(&cl->clm);
2227c478bd9Sstevel@tonic-gate if (--cl->ref_cnt == 0 && cl->stale) {
2237c478bd9Sstevel@tonic-gate mutex_unlock(&cl->clm);
2247c478bd9Sstevel@tonic-gate mutex_lock(&ctx_mutex);
2257c478bd9Sstevel@tonic-gate destroy_client(cl);
2267c478bd9Sstevel@tonic-gate mutex_unlock(&ctx_mutex);
2277c478bd9Sstevel@tonic-gate } else
2287c478bd9Sstevel@tonic-gate mutex_unlock(&cl->clm);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * Check for other expired contexts.
2337c478bd9Sstevel@tonic-gate */
2347c478bd9Sstevel@tonic-gate if ((time(0) - last_swept) > sweep_interval) {
2357c478bd9Sstevel@tonic-gate mutex_lock(&ctx_mutex);
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * Check again, in case some other thread got in.
2387c478bd9Sstevel@tonic-gate */
2397c478bd9Sstevel@tonic-gate if ((time(0) - last_swept) > sweep_interval)
2407c478bd9Sstevel@tonic-gate sweep_clients();
2417c478bd9Sstevel@tonic-gate mutex_unlock(&ctx_mutex);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate * Set server parameters.
2477c478bd9Sstevel@tonic-gate */
2487c478bd9Sstevel@tonic-gate void
__rpc_gss_set_server_parms(int init_cred_lifetime,int max_cred_lifetime,int cache_size)249*b86a7875SToomas Soome __rpc_gss_set_server_parms(int init_cred_lifetime, int max_cred_lifetime,
250*b86a7875SToomas Soome int cache_size)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * Ignore parameters unless greater than zero.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate mutex_lock(&ctx_mutex);
2567c478bd9Sstevel@tonic-gate if (cache_size > 0)
2577c478bd9Sstevel@tonic-gate max_gss_contexts = cache_size;
2587c478bd9Sstevel@tonic-gate if (max_cred_lifetime > 0)
2597c478bd9Sstevel@tonic-gate max_lifetime = (uint_t)max_cred_lifetime;
2607c478bd9Sstevel@tonic-gate if (init_cred_lifetime > 0)
2617c478bd9Sstevel@tonic-gate init_lifetime = init_cred_lifetime;
2627c478bd9Sstevel@tonic-gate mutex_unlock(&ctx_mutex);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * Shift the array arr of length arrlen right by nbits bits.
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate static void
shift_bits(uint_t * arr,int arrlen,int nbits)269*b86a7875SToomas Soome shift_bits(uint_t *arr, int arrlen, int nbits)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate int i, j;
2727c478bd9Sstevel@tonic-gate uint_t lo, hi;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * If the number of bits to be shifted exceeds SEQ_WIN, just
2767c478bd9Sstevel@tonic-gate * zero out the array.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate if (nbits < SEQ_WIN) {
2797c478bd9Sstevel@tonic-gate for (i = 0; i < nbits; i++) {
2807c478bd9Sstevel@tonic-gate hi = 0;
2817c478bd9Sstevel@tonic-gate for (j = 0; j < arrlen; j++) {
2827c478bd9Sstevel@tonic-gate lo = arr[j] & SEQ_LO_BIT;
2837c478bd9Sstevel@tonic-gate arr[j] >>= 1;
2847c478bd9Sstevel@tonic-gate if (hi)
2857c478bd9Sstevel@tonic-gate arr[j] |= SEQ_HI_BIT;
2867c478bd9Sstevel@tonic-gate hi = lo;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate } else {
2907c478bd9Sstevel@tonic-gate for (j = 0; j < arrlen; j++)
2917c478bd9Sstevel@tonic-gate arr[j] = 0;
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate * Check that the received sequence number seq_num is valid.
2977c478bd9Sstevel@tonic-gate */
2987c478bd9Sstevel@tonic-gate static bool_t
check_seq(svc_rpc_gss_data * cl,uint_t seq_num,bool_t * kill_context)299*b86a7875SToomas Soome check_seq(svc_rpc_gss_data *cl, uint_t seq_num, bool_t *kill_context)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate int i, j;
3027c478bd9Sstevel@tonic-gate uint_t bit;
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * If it exceeds the maximum, kill context.
3067c478bd9Sstevel@tonic-gate */
3077c478bd9Sstevel@tonic-gate if (seq_num >= SEQ_MAX) {
3087c478bd9Sstevel@tonic-gate *kill_context = TRUE;
3097c478bd9Sstevel@tonic-gate return (FALSE);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * If greater than the last seen sequence number, just shift
3147c478bd9Sstevel@tonic-gate * the sequence window so that it starts at the new sequence
3157c478bd9Sstevel@tonic-gate * number and extends downwards by SEQ_WIN.
3167c478bd9Sstevel@tonic-gate */
3177c478bd9Sstevel@tonic-gate if (seq_num > cl->seq_num) {
3187c478bd9Sstevel@tonic-gate shift_bits(cl->seq_bits, SEQ_ARR_SIZE, seq_num - cl->seq_num);
3197c478bd9Sstevel@tonic-gate cl->seq_bits[0] |= SEQ_HI_BIT;
3207c478bd9Sstevel@tonic-gate cl->seq_num = seq_num;
3217c478bd9Sstevel@tonic-gate return (TRUE);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate * If it is outside the sequence window, return failure.
3267c478bd9Sstevel@tonic-gate */
3277c478bd9Sstevel@tonic-gate i = cl->seq_num - seq_num;
3287c478bd9Sstevel@tonic-gate if (i >= SEQ_WIN)
3297c478bd9Sstevel@tonic-gate return (FALSE);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate * If within sequence window, set the bit corresponding to it
3337c478bd9Sstevel@tonic-gate * if not already seen; if already seen, return failure.
3347c478bd9Sstevel@tonic-gate */
3357c478bd9Sstevel@tonic-gate j = SEQ_MASK - (i & SEQ_MASK);
3367c478bd9Sstevel@tonic-gate bit = j > 0 ? (1 << j) : 1;
3377c478bd9Sstevel@tonic-gate i >>= DIV_BY_32;
3387c478bd9Sstevel@tonic-gate if (cl->seq_bits[i] & bit)
3397c478bd9Sstevel@tonic-gate return (FALSE);
3407c478bd9Sstevel@tonic-gate cl->seq_bits[i] |= bit;
3417c478bd9Sstevel@tonic-gate return (TRUE);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate * Convert a name in gss exported type to rpc_gss_principal_t type.
3467c478bd9Sstevel@tonic-gate */
3477c478bd9Sstevel@tonic-gate static bool_t
__rpc_gss_make_principal(rpc_gss_principal_t * principal,gss_buffer_desc * name)348*b86a7875SToomas Soome __rpc_gss_make_principal(rpc_gss_principal_t *principal, gss_buffer_desc *name)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate int plen;
3517c478bd9Sstevel@tonic-gate char *s;
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate plen = RNDUP(name->length) + sizeof (int);
3547c478bd9Sstevel@tonic-gate (*principal) = (rpc_gss_principal_t)malloc(plen);
3557c478bd9Sstevel@tonic-gate if ((*principal) == NULL)
3567c478bd9Sstevel@tonic-gate return (FALSE);
3577c478bd9Sstevel@tonic-gate bzero((caddr_t)(*principal), plen);
3587c478bd9Sstevel@tonic-gate (*principal)->len = RNDUP(name->length);
3597c478bd9Sstevel@tonic-gate s = (*principal)->name;
3607c478bd9Sstevel@tonic-gate memcpy(s, name->value, name->length);
3617c478bd9Sstevel@tonic-gate return (TRUE);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * Convert a name in internal form to the exported type.
3667c478bd9Sstevel@tonic-gate */
3677c478bd9Sstevel@tonic-gate static bool_t
set_client_principal(gss_name_t g_name,rpc_gss_principal_t * r_name)368*b86a7875SToomas Soome set_client_principal(gss_name_t g_name, rpc_gss_principal_t *r_name)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate gss_buffer_desc name;
3717c478bd9Sstevel@tonic-gate OM_uint32 major, minor;
3727c478bd9Sstevel@tonic-gate bool_t ret = FALSE;
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate major = gss_export_name(&minor, g_name, &name);
3757c478bd9Sstevel@tonic-gate if (major != GSS_S_COMPLETE)
3767c478bd9Sstevel@tonic-gate return (FALSE);
3777c478bd9Sstevel@tonic-gate ret = __rpc_gss_make_principal(r_name, &name);
3787c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&minor, &name);
3797c478bd9Sstevel@tonic-gate return (ret);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * Set server callback.
3847c478bd9Sstevel@tonic-gate */
3857c478bd9Sstevel@tonic-gate bool_t
__rpc_gss_set_callback(rpc_gss_callback_t * cb)386*b86a7875SToomas Soome __rpc_gss_set_callback(rpc_gss_callback_t *cb)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate cblist_t *cbl;
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate if (cb->callback == NULL)
3917c478bd9Sstevel@tonic-gate return (FALSE);
3927c478bd9Sstevel@tonic-gate if ((cbl = (cblist_t *)malloc(sizeof (*cbl))) == NULL)
3937c478bd9Sstevel@tonic-gate return (FALSE);
3947c478bd9Sstevel@tonic-gate cbl->cb = *cb;
3957c478bd9Sstevel@tonic-gate mutex_lock(&cb_mutex);
3967c478bd9Sstevel@tonic-gate cbl->next = cblist;
3977c478bd9Sstevel@tonic-gate cblist = cbl;
3987c478bd9Sstevel@tonic-gate mutex_unlock(&cb_mutex);
3997c478bd9Sstevel@tonic-gate return (TRUE);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate * Locate callback (if specified) and call server. Release any
4047c478bd9Sstevel@tonic-gate * delegated credentials unless passed to server and the server
4057c478bd9Sstevel@tonic-gate * accepts the context. If a callback is not specified, accept
4067c478bd9Sstevel@tonic-gate * the incoming context.
4077c478bd9Sstevel@tonic-gate */
4087c478bd9Sstevel@tonic-gate static bool_t
do_callback(struct svc_req * req,svc_rpc_gss_data * client_data)409*b86a7875SToomas Soome do_callback(struct svc_req *req, svc_rpc_gss_data *client_data)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate cblist_t *cbl;
4127c478bd9Sstevel@tonic-gate bool_t ret = TRUE, found = FALSE;
4137c478bd9Sstevel@tonic-gate rpc_gss_lock_t lock;
4147c478bd9Sstevel@tonic-gate OM_uint32 minor;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate mutex_lock(&cb_mutex);
4177c478bd9Sstevel@tonic-gate for (cbl = cblist; cbl != NULL; cbl = cbl->next) {
4187c478bd9Sstevel@tonic-gate if (req->rq_prog != cbl->cb.program ||
4197c478bd9Sstevel@tonic-gate req->rq_vers != cbl->cb.version)
4207c478bd9Sstevel@tonic-gate continue;
4217c478bd9Sstevel@tonic-gate found = TRUE;
4227c478bd9Sstevel@tonic-gate lock.locked = FALSE;
4237c478bd9Sstevel@tonic-gate lock.raw_cred = &client_data->raw_cred;
4247c478bd9Sstevel@tonic-gate ret = (*cbl->cb.callback)(req, client_data->deleg,
4257c478bd9Sstevel@tonic-gate client_data->context, &lock, &client_data->cookie);
4267c478bd9Sstevel@tonic-gate if (ret) {
4277c478bd9Sstevel@tonic-gate client_data->locked = lock.locked;
4287c478bd9Sstevel@tonic-gate client_data->deleg = GSS_C_NO_CREDENTIAL;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate break;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate if (!found) {
4337c478bd9Sstevel@tonic-gate if (client_data->deleg != GSS_C_NO_CREDENTIAL) {
4347c478bd9Sstevel@tonic-gate (void) gss_release_cred(&minor, &client_data->deleg);
4357c478bd9Sstevel@tonic-gate client_data->deleg = GSS_C_NO_CREDENTIAL;
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate mutex_unlock(&cb_mutex);
4397c478bd9Sstevel@tonic-gate return (ret);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate * Return caller credentials.
4447c478bd9Sstevel@tonic-gate */
4457c478bd9Sstevel@tonic-gate bool_t
__rpc_gss_getcred(struct svc_req * req,rpc_gss_rawcred_t ** rcred,rpc_gss_ucred_t ** ucred,void ** cookie)446*b86a7875SToomas Soome __rpc_gss_getcred(struct svc_req *req, rpc_gss_rawcred_t **rcred,
447*b86a7875SToomas Soome rpc_gss_ucred_t **ucred, void **cookie)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate SVCAUTH *svcauth;
4507c478bd9Sstevel@tonic-gate svc_rpc_gss_data *client_data;
4517c478bd9Sstevel@tonic-gate svc_rpc_gss_parms_t *gss_parms;
4527c478bd9Sstevel@tonic-gate gss_OID oid;
4537c478bd9Sstevel@tonic-gate OM_uint32 status;
4547c478bd9Sstevel@tonic-gate int len = 0;
4557c478bd9Sstevel@tonic-gate struct timeval now;
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate svcauth = __svc_get_svcauth(req->rq_xprt);
4587c478bd9Sstevel@tonic-gate /*LINTED*/
4597c478bd9Sstevel@tonic-gate client_data = (svc_rpc_gss_data *)svcauth->svc_ah_private;
4607c478bd9Sstevel@tonic-gate gss_parms = &svcauth->svc_gss_parms;
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate mutex_lock(&client_data->clm);
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if (rcred != NULL) {
4657c478bd9Sstevel@tonic-gate svcauth->raw_cred = client_data->raw_cred;
4667c478bd9Sstevel@tonic-gate svcauth->raw_cred.service = gss_parms->service;
4677c478bd9Sstevel@tonic-gate svcauth->raw_cred.qop = __rpc_gss_num_to_qop(
4687c478bd9Sstevel@tonic-gate svcauth->raw_cred.mechanism, gss_parms->qop_rcvd);
4697c478bd9Sstevel@tonic-gate *rcred = &svcauth->raw_cred;
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate if (ucred != NULL) {
4727c478bd9Sstevel@tonic-gate if (!client_data->u_cred_set) {
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate * Double check making sure ucred is not set
4757c478bd9Sstevel@tonic-gate * after acquiring the lock.
4767c478bd9Sstevel@tonic-gate */
4777c478bd9Sstevel@tonic-gate if (!client_data->u_cred_set) {
4787c478bd9Sstevel@tonic-gate if (!__rpc_gss_mech_to_oid(
4797c478bd9Sstevel@tonic-gate (*rcred)->mechanism, &oid)) {
4807c478bd9Sstevel@tonic-gate fprintf(stderr, dgettext(TEXT_DOMAIN,
481*b86a7875SToomas Soome "mech_to_oid failed in "
482*b86a7875SToomas Soome "getcred.\n"));
4837c478bd9Sstevel@tonic-gate *ucred = NULL;
4847c478bd9Sstevel@tonic-gate } else {
4857c478bd9Sstevel@tonic-gate status = gsscred_name_to_unix_cred(
4867c478bd9Sstevel@tonic-gate client_data->client_name, oid,
4877c478bd9Sstevel@tonic-gate &client_data->u_cred.uid,
4887c478bd9Sstevel@tonic-gate &client_data->u_cred.gid,
4897c478bd9Sstevel@tonic-gate &client_data->u_cred.gidlist,
4907c478bd9Sstevel@tonic-gate &len);
4917c478bd9Sstevel@tonic-gate if (status == GSS_S_COMPLETE) {
4927c478bd9Sstevel@tonic-gate client_data->u_cred_set = TRUE;
4937c478bd9Sstevel@tonic-gate client_data->u_cred.gidlen =
4947c478bd9Sstevel@tonic-gate (short)len;
495*b86a7875SToomas Soome gettimeofday(&now, NULL);
4967c478bd9Sstevel@tonic-gate client_data->time_secs_set =
4977c478bd9Sstevel@tonic-gate now.tv_sec;
4987c478bd9Sstevel@tonic-gate *ucred = &client_data->u_cred;
4997c478bd9Sstevel@tonic-gate } else
5007c478bd9Sstevel@tonic-gate *ucred = NULL;
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate } else {
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate * gid's already set;
5067c478bd9Sstevel@tonic-gate * check if they have expired.
5077c478bd9Sstevel@tonic-gate */
508*b86a7875SToomas Soome gettimeofday(&now, NULL);
5097c478bd9Sstevel@tonic-gate if ((now.tv_sec - client_data->time_secs_set)
5107c478bd9Sstevel@tonic-gate > gid_timeout) {
5117c478bd9Sstevel@tonic-gate /* Refresh gid's */
5127c478bd9Sstevel@tonic-gate status = gss_get_group_info(
5137c478bd9Sstevel@tonic-gate client_data->u_cred.uid,
5147c478bd9Sstevel@tonic-gate &client_data->u_cred.gid,
5157c478bd9Sstevel@tonic-gate &client_data->u_cred.gidlist,
5167c478bd9Sstevel@tonic-gate &len);
5177c478bd9Sstevel@tonic-gate if (status == GSS_S_COMPLETE) {
5187c478bd9Sstevel@tonic-gate client_data->u_cred.gidlen =
5197c478bd9Sstevel@tonic-gate (short)len;
520*b86a7875SToomas Soome gettimeofday(&now, NULL);
5217c478bd9Sstevel@tonic-gate client_data->time_secs_set = now.tv_sec;
5227c478bd9Sstevel@tonic-gate *ucred = &client_data->u_cred;
5237c478bd9Sstevel@tonic-gate } else {
5247c478bd9Sstevel@tonic-gate client_data->u_cred_set = FALSE;
5257c478bd9Sstevel@tonic-gate *ucred = NULL;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate else
5297c478bd9Sstevel@tonic-gate *ucred = &client_data->u_cred;
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate if (cookie != NULL)
5337c478bd9Sstevel@tonic-gate *cookie = client_data->cookie;
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate mutex_unlock(&client_data->clm);
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate return (TRUE);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate * Server side authentication for RPCSEC_GSS.
5427c478bd9Sstevel@tonic-gate */
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate enum auth_stat
__svcrpcsec_gss(struct svc_req * rqst,struct rpc_msg * msg,bool_t * no_dispatch)545*b86a7875SToomas Soome __svcrpcsec_gss(struct svc_req *rqst, struct rpc_msg *msg, bool_t *no_dispatch)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate XDR xdrs;
5487c478bd9Sstevel@tonic-gate rpc_gss_creds creds;
5497c478bd9Sstevel@tonic-gate rpc_gss_init_arg call_arg;
5507c478bd9Sstevel@tonic-gate rpc_gss_init_res call_res, *retrans_result;
5517c478bd9Sstevel@tonic-gate gss_buffer_desc output_token;
5527c478bd9Sstevel@tonic-gate OM_uint32 gssstat, minor_stat, time_rec, ret_flags;
5537c478bd9Sstevel@tonic-gate struct opaque_auth *cred;
5547c478bd9Sstevel@tonic-gate svc_rpc_gss_data *client_data;
5557c478bd9Sstevel@tonic-gate int ret;
5567c478bd9Sstevel@tonic-gate svc_creds_list_t *sc;
5577c478bd9Sstevel@tonic-gate SVCAUTH *svcauth;
5587c478bd9Sstevel@tonic-gate svc_rpc_gss_parms_t *gss_parms;
5597c478bd9Sstevel@tonic-gate gss_OID mech_type = GSS_C_NULL_OID;
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate * Initialize response verifier to NULL verifier. If
5637c478bd9Sstevel@tonic-gate * necessary, this will be changed later.
5647c478bd9Sstevel@tonic-gate */
5657c478bd9Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NONE;
5667c478bd9Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_base = NULL;
5677c478bd9Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_length = 0;
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate * Need to null out results to start with.
5707c478bd9Sstevel@tonic-gate */
5717c478bd9Sstevel@tonic-gate memset((char *)&call_res, 0, sizeof (call_res));
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate /*
5747c478bd9Sstevel@tonic-gate * Pull out and check credential and verifier.
5757c478bd9Sstevel@tonic-gate */
5767c478bd9Sstevel@tonic-gate cred = &msg->rm_call.cb_cred;
5777c478bd9Sstevel@tonic-gate if (cred->oa_length == 0) {
578158643e0Sps57422 return (AUTH_BADCRED);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate xdrmem_create(&xdrs, cred->oa_base, cred->oa_length, XDR_DECODE);
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate memset((char *)&creds, 0, sizeof (creds));
5847c478bd9Sstevel@tonic-gate if (!__xdr_rpc_gss_creds(&xdrs, &creds)) {
5857c478bd9Sstevel@tonic-gate XDR_DESTROY(&xdrs);
5867c478bd9Sstevel@tonic-gate ret = AUTH_BADCRED;
5877c478bd9Sstevel@tonic-gate goto error;
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate XDR_DESTROY(&xdrs);
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate * If this is a control message and proc is GSSAPI_INIT, then
5937c478bd9Sstevel@tonic-gate * create a client handle for this client. Otherwise, look up
5947c478bd9Sstevel@tonic-gate * the existing handle.
5957c478bd9Sstevel@tonic-gate */
5967c478bd9Sstevel@tonic-gate if (creds.gss_proc == RPCSEC_GSS_INIT) {
5977c478bd9Sstevel@tonic-gate if (creds.ctx_handle.length != 0) {
5987c478bd9Sstevel@tonic-gate ret = AUTH_BADCRED;
5997c478bd9Sstevel@tonic-gate goto error;
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate if ((client_data = create_client()) == NULL) {
6027c478bd9Sstevel@tonic-gate ret = AUTH_FAILED;
6037c478bd9Sstevel@tonic-gate goto error;
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate } else {
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate * Only verify values for service parameter when proc
6087c478bd9Sstevel@tonic-gate * not RPCSEC_GSS_INIT or RPCSEC_GSS_CONTINUE_INIT.
6097c478bd9Sstevel@tonic-gate * RFC2203 says contents for sequence and service args
6107c478bd9Sstevel@tonic-gate * are undefined for creation procs.
6117c478bd9Sstevel@tonic-gate *
6127c478bd9Sstevel@tonic-gate * Note: only need to check for *CONTINUE_INIT here because
6137c478bd9Sstevel@tonic-gate * if() clause already checked for RPCSEC_GSS_INIT
6147c478bd9Sstevel@tonic-gate */
6157c478bd9Sstevel@tonic-gate if (creds.gss_proc != RPCSEC_GSS_CONTINUE_INIT) {
6167c478bd9Sstevel@tonic-gate switch (creds.service) {
6177c478bd9Sstevel@tonic-gate case rpc_gss_svc_none:
6187c478bd9Sstevel@tonic-gate case rpc_gss_svc_integrity:
6197c478bd9Sstevel@tonic-gate case rpc_gss_svc_privacy:
6207c478bd9Sstevel@tonic-gate break;
6217c478bd9Sstevel@tonic-gate default:
6227c478bd9Sstevel@tonic-gate ret = AUTH_BADCRED;
6237c478bd9Sstevel@tonic-gate goto error;
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate if (creds.ctx_handle.length == 0) {
6277c478bd9Sstevel@tonic-gate ret = AUTH_BADCRED;
6287c478bd9Sstevel@tonic-gate goto error;
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate if ((client_data = get_client(&creds.ctx_handle)) == NULL) {
6317c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_NOCRED;
6327c478bd9Sstevel@tonic-gate goto error;
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate * lock the client data until it's safe; if it's already stale,
6387c478bd9Sstevel@tonic-gate * no more processing is possible
6397c478bd9Sstevel@tonic-gate */
6407c478bd9Sstevel@tonic-gate mutex_lock(&client_data->clm);
6417c478bd9Sstevel@tonic-gate if (client_data->stale) {
6427c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_NOCRED;
6437c478bd9Sstevel@tonic-gate goto error2;
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate * Any response we send will use ctx_handle, so set it now;
6487c478bd9Sstevel@tonic-gate * also set seq_window since this won't change.
6497c478bd9Sstevel@tonic-gate */
6507c478bd9Sstevel@tonic-gate call_res.ctx_handle.length = sizeof (client_data->key);
6517c478bd9Sstevel@tonic-gate call_res.ctx_handle.value = (char *)&client_data->key;
6527c478bd9Sstevel@tonic-gate call_res.seq_window = SEQ_WIN;
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate * Set the appropriate wrap/unwrap routine for RPCSEC_GSS.
6567c478bd9Sstevel@tonic-gate */
6577c478bd9Sstevel@tonic-gate svcauth = __svc_get_svcauth(rqst->rq_xprt);
6587c478bd9Sstevel@tonic-gate svcauth->svc_ah_ops = svc_rpc_gss_ops;
6597c478bd9Sstevel@tonic-gate svcauth->svc_ah_private = (caddr_t)client_data;
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Keep copy of parameters we'll need for response, for the
6637c478bd9Sstevel@tonic-gate * sake of reentrancy (we don't want to look in the context
6647c478bd9Sstevel@tonic-gate * data because when we are sending a response, another
6657c478bd9Sstevel@tonic-gate * request may have come in.
6667c478bd9Sstevel@tonic-gate */
6677c478bd9Sstevel@tonic-gate gss_parms = &svcauth->svc_gss_parms;
6687c478bd9Sstevel@tonic-gate gss_parms->established = client_data->established;
6697c478bd9Sstevel@tonic-gate gss_parms->service = creds.service;
6707c478bd9Sstevel@tonic-gate gss_parms->qop_rcvd = (uint_t)client_data->qop;
6717c478bd9Sstevel@tonic-gate gss_parms->context = (void *)client_data->context;
6727c478bd9Sstevel@tonic-gate gss_parms->seq_num = creds.seq_num;
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate if (!client_data->established) {
6757c478bd9Sstevel@tonic-gate if (creds.gss_proc == RPCSEC_GSS_DATA) {
6767c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_FAILED;
6777c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
6787c478bd9Sstevel@tonic-gate goto error2;
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate * If the context is not established, then only GSSAPI_INIT
6837c478bd9Sstevel@tonic-gate * and _CONTINUE requests are valid.
6847c478bd9Sstevel@tonic-gate */
6857c478bd9Sstevel@tonic-gate if (creds.gss_proc != RPCSEC_GSS_INIT && creds.gss_proc !=
6867c478bd9Sstevel@tonic-gate RPCSEC_GSS_CONTINUE_INIT) {
6877c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_FAILED;
6887c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
6897c478bd9Sstevel@tonic-gate goto error2;
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate * call is for us, deserialize arguments
6947c478bd9Sstevel@tonic-gate */
6957c478bd9Sstevel@tonic-gate memset(&call_arg, 0, sizeof (call_arg));
6967c478bd9Sstevel@tonic-gate if (!svc_getargs(rqst->rq_xprt, __xdr_rpc_gss_init_arg,
6977c478bd9Sstevel@tonic-gate (caddr_t)&call_arg)) {
6987c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_FAILED;
6997c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
7007c478bd9Sstevel@tonic-gate goto error2;
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate gssstat = GSS_S_FAILURE;
7047c478bd9Sstevel@tonic-gate minor_stat = 0;
7057c478bd9Sstevel@tonic-gate rw_rdlock(&cred_lock);
7067c478bd9Sstevel@tonic-gate /*
7077c478bd9Sstevel@tonic-gate * set next sc to point to the server cred
7087c478bd9Sstevel@tonic-gate * if the client_data contains server_creds
7097c478bd9Sstevel@tonic-gate */
7107c478bd9Sstevel@tonic-gate for (sc = svc_creds_list; sc != NULL; sc = sc->next) {
7117c478bd9Sstevel@tonic-gate if (rqst->rq_prog != sc->program ||
7127c478bd9Sstevel@tonic-gate rqst->rq_vers != sc->version)
7137c478bd9Sstevel@tonic-gate continue;
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate mutex_lock(&sc->refresh_mutex);
7167c478bd9Sstevel@tonic-gate gssstat = gss_accept_sec_context(&minor_stat,
7177c478bd9Sstevel@tonic-gate &client_data->context,
7187c478bd9Sstevel@tonic-gate sc->cred,
7197c478bd9Sstevel@tonic-gate &call_arg,
7207c478bd9Sstevel@tonic-gate GSS_C_NO_CHANNEL_BINDINGS,
7217c478bd9Sstevel@tonic-gate &client_data->client_name,
7227c478bd9Sstevel@tonic-gate &mech_type,
7237c478bd9Sstevel@tonic-gate &output_token,
7247c478bd9Sstevel@tonic-gate &ret_flags,
7257c478bd9Sstevel@tonic-gate &time_rec,
7267c478bd9Sstevel@tonic-gate NULL);
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate if (gssstat == GSS_S_CREDENTIALS_EXPIRED) {
7297c478bd9Sstevel@tonic-gate if (rpc_gss_refresh_svc_cred(sc)) {
7307c478bd9Sstevel@tonic-gate gssstat = gss_accept_sec_context(
7317c478bd9Sstevel@tonic-gate &minor_stat,
7327c478bd9Sstevel@tonic-gate &client_data->context,
7337c478bd9Sstevel@tonic-gate sc->cred,
7347c478bd9Sstevel@tonic-gate &call_arg,
7357c478bd9Sstevel@tonic-gate GSS_C_NO_CHANNEL_BINDINGS,
7367c478bd9Sstevel@tonic-gate &client_data->client_name,
7377c478bd9Sstevel@tonic-gate &mech_type,
7387c478bd9Sstevel@tonic-gate &output_token,
7397c478bd9Sstevel@tonic-gate &ret_flags,
7407c478bd9Sstevel@tonic-gate &time_rec,
7417c478bd9Sstevel@tonic-gate NULL);
7427c478bd9Sstevel@tonic-gate mutex_unlock(&sc->refresh_mutex);
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate } else {
7457c478bd9Sstevel@tonic-gate mutex_unlock(&sc->refresh_mutex);
7467c478bd9Sstevel@tonic-gate gssstat = GSS_S_NO_CRED;
7477c478bd9Sstevel@tonic-gate break;
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate } else
7517c478bd9Sstevel@tonic-gate mutex_unlock(&sc->refresh_mutex);
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate if (gssstat == GSS_S_COMPLETE) {
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate * Server_creds was right - set it. Also
7567c478bd9Sstevel@tonic-gate * set the raw and unix credentials at this
7577c478bd9Sstevel@tonic-gate * point. This saves a lot of computation
7587c478bd9Sstevel@tonic-gate * later when credentials are retrieved.
7597c478bd9Sstevel@tonic-gate */
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate * XXX server_creds will prob be stale
7627c478bd9Sstevel@tonic-gate * after rpc_gss_refresh_svc_cred(), but
7637c478bd9Sstevel@tonic-gate * it appears not to ever be referenced
7647c478bd9Sstevel@tonic-gate * anyways.
7657c478bd9Sstevel@tonic-gate */
7667c478bd9Sstevel@tonic-gate mutex_lock(&sc->refresh_mutex);
7677c478bd9Sstevel@tonic-gate client_data->server_creds = sc->cred;
7687c478bd9Sstevel@tonic-gate client_data->raw_cred.version = creds.version;
7697c478bd9Sstevel@tonic-gate client_data->raw_cred.service = creds.service;
7707c478bd9Sstevel@tonic-gate client_data->raw_cred.svc_principal =
7717c478bd9Sstevel@tonic-gate sc->server_name;
7727c478bd9Sstevel@tonic-gate mutex_unlock(&sc->refresh_mutex);
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate if ((client_data->raw_cred.mechanism
7757c478bd9Sstevel@tonic-gate = __rpc_gss_oid_to_mech(mech_type))
7767c478bd9Sstevel@tonic-gate == NULL) {
7777c478bd9Sstevel@tonic-gate gssstat = GSS_S_FAILURE;
7787c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat,
7797c478bd9Sstevel@tonic-gate &output_token);
7807c478bd9Sstevel@tonic-gate } else if (!set_client_principal(client_data->
7817c478bd9Sstevel@tonic-gate client_name, &client_data->
7827c478bd9Sstevel@tonic-gate raw_cred.client_principal)) {
7837c478bd9Sstevel@tonic-gate gssstat = GSS_S_FAILURE;
7847c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat,
7857c478bd9Sstevel@tonic-gate &output_token);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate break;
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate if (gssstat == GSS_S_CONTINUE_NEEDED) {
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate * XXX server_creds will prob be stale
7937c478bd9Sstevel@tonic-gate * after rpc_gss_refresh_svc_cred(), but
7947c478bd9Sstevel@tonic-gate * it appears not to ever be referenced
7957c478bd9Sstevel@tonic-gate * anyways.
7967c478bd9Sstevel@tonic-gate */
7977c478bd9Sstevel@tonic-gate mutex_lock(&sc->refresh_mutex);
7987c478bd9Sstevel@tonic-gate client_data->server_creds = sc->cred;
7997c478bd9Sstevel@tonic-gate mutex_unlock(&sc->refresh_mutex);
8007c478bd9Sstevel@tonic-gate break;
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate call_res.gss_major = gssstat;
8077c478bd9Sstevel@tonic-gate call_res.gss_minor = minor_stat;
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate xdr_free(__xdr_rpc_gss_init_arg, (caddr_t)&call_arg);
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate if (gssstat != GSS_S_COMPLETE &&
8127c478bd9Sstevel@tonic-gate gssstat != GSS_S_CONTINUE_NEEDED) {
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate * We have a failure - send response and delete
8157c478bd9Sstevel@tonic-gate * the context. Don't dispatch. Set ctx_handle
8167c478bd9Sstevel@tonic-gate * to NULL and seq_window to 0.
8177c478bd9Sstevel@tonic-gate */
8187c478bd9Sstevel@tonic-gate call_res.ctx_handle.length = 0;
8197c478bd9Sstevel@tonic-gate call_res.ctx_handle.value = NULL;
8207c478bd9Sstevel@tonic-gate call_res.seq_window = 0;
8217c478bd9Sstevel@tonic-gate
8227c478bd9Sstevel@tonic-gate svc_sendreply(rqst->rq_xprt, __xdr_rpc_gss_init_res,
8237c478bd9Sstevel@tonic-gate (caddr_t)&call_res);
8247c478bd9Sstevel@tonic-gate *no_dispatch = TRUE;
8257c478bd9Sstevel@tonic-gate ret = AUTH_OK;
8267c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
8277c478bd9Sstevel@tonic-gate goto error2;
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate /*
8317c478bd9Sstevel@tonic-gate * This step succeeded. Send a response, along with
8327c478bd9Sstevel@tonic-gate * a token if there's one. Don't dispatch.
8337c478bd9Sstevel@tonic-gate */
8347c478bd9Sstevel@tonic-gate if (output_token.length != 0) {
8357c478bd9Sstevel@tonic-gate GSS_COPY_BUFFER(call_res.token, output_token);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate * set response verifier: checksum of SEQ_WIN
8407c478bd9Sstevel@tonic-gate */
8417c478bd9Sstevel@tonic-gate if (gssstat == GSS_S_COMPLETE) {
8427c478bd9Sstevel@tonic-gate if (!set_response_verf(rqst, msg, client_data,
8437c478bd9Sstevel@tonic-gate (uint_t)SEQ_WIN)) {
8447c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_FAILED;
8457c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
8467c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat,
8477c478bd9Sstevel@tonic-gate &output_token);
8487c478bd9Sstevel@tonic-gate goto error2;
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate
8527c478bd9Sstevel@tonic-gate svc_sendreply(rqst->rq_xprt, __xdr_rpc_gss_init_res,
8537c478bd9Sstevel@tonic-gate (caddr_t)&call_res);
8547c478bd9Sstevel@tonic-gate /*
8557c478bd9Sstevel@tonic-gate * Cache last response in case it is lost and the client
8567c478bd9Sstevel@tonic-gate * retries on an established context.
8577c478bd9Sstevel@tonic-gate */
8587c478bd9Sstevel@tonic-gate (void) retrans_add(client_data, msg->rm_xid, &call_res);
8597c478bd9Sstevel@tonic-gate *no_dispatch = TRUE;
8607c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat, &output_token);
8617c478bd9Sstevel@tonic-gate
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate * If appropriate, set established to TRUE *after* sending
8647c478bd9Sstevel@tonic-gate * response (otherwise, the client will receive the final
8657c478bd9Sstevel@tonic-gate * token encrypted)
8667c478bd9Sstevel@tonic-gate */
8677c478bd9Sstevel@tonic-gate if (gssstat == GSS_S_COMPLETE) {
8687c478bd9Sstevel@tonic-gate /*
8697c478bd9Sstevel@tonic-gate * Context is established. Set expiry time for
8707c478bd9Sstevel@tonic-gate * context (the minimum of time_rec and max_lifetime).
8717c478bd9Sstevel@tonic-gate */
8727c478bd9Sstevel@tonic-gate client_data->seq_num = 1;
8737c478bd9Sstevel@tonic-gate if (time_rec == GSS_C_INDEFINITE) {
874*b86a7875SToomas Soome if (max_lifetime != GSS_C_INDEFINITE) {
8757c478bd9Sstevel@tonic-gate client_data->expiration =
8767c478bd9Sstevel@tonic-gate max_lifetime + time(0);
877*b86a7875SToomas Soome } else {
8787c478bd9Sstevel@tonic-gate client_data->expiration =
8797c478bd9Sstevel@tonic-gate GSS_C_INDEFINITE;
880*b86a7875SToomas Soome }
8817c478bd9Sstevel@tonic-gate } else if (max_lifetime == GSS_C_INDEFINITE ||
882*b86a7875SToomas Soome max_lifetime > time_rec) {
8837c478bd9Sstevel@tonic-gate client_data->expiration = time_rec + time(0);
884*b86a7875SToomas Soome } else {
8857c478bd9Sstevel@tonic-gate client_data->expiration = max_lifetime +
8867c478bd9Sstevel@tonic-gate time(0);
887*b86a7875SToomas Soome }
8887c478bd9Sstevel@tonic-gate client_data->established = TRUE;
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate
8917c478bd9Sstevel@tonic-gate } else {
8927c478bd9Sstevel@tonic-gate if ((creds.gss_proc != RPCSEC_GSS_DATA) &&
8937c478bd9Sstevel@tonic-gate (creds.gss_proc != RPCSEC_GSS_DESTROY)) {
8947c478bd9Sstevel@tonic-gate
8957c478bd9Sstevel@tonic-gate switch (creds.gss_proc) {
8967c478bd9Sstevel@tonic-gate
8977c478bd9Sstevel@tonic-gate case RPCSEC_GSS_CONTINUE_INIT:
8987c478bd9Sstevel@tonic-gate /*
8997c478bd9Sstevel@tonic-gate * This is an established context. Continue to
9007c478bd9Sstevel@tonic-gate * satisfy retried continue init requests out of
901*b86a7875SToomas Soome * the retransmit cache. Throw away any that
902*b86a7875SToomas Soome * don't have a matching xid or the cach is
903*b86a7875SToomas Soome * empty. Delete the retransmit cache once the
904*b86a7875SToomas Soome * client sends a data request.
9057c478bd9Sstevel@tonic-gate */
9067c478bd9Sstevel@tonic-gate if (client_data->retrans_data &&
907*b86a7875SToomas Soome (client_data->retrans_data->xid ==
908*b86a7875SToomas Soome msg->rm_xid)) {
9097c478bd9Sstevel@tonic-gate
910*b86a7875SToomas Soome retrans_result =
911*b86a7875SToomas Soome &client_data->retrans_data->result;
912*b86a7875SToomas Soome if (set_response_verf(rqst, msg,
913*b86a7875SToomas Soome client_data, (uint_t)
914*b86a7875SToomas Soome retrans_result->seq_window)) {
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate gss_parms->established = FALSE;
9177c478bd9Sstevel@tonic-gate svc_sendreply(rqst->rq_xprt,
9187c478bd9Sstevel@tonic-gate __xdr_rpc_gss_init_res,
9197c478bd9Sstevel@tonic-gate (caddr_t)retrans_result);
9207c478bd9Sstevel@tonic-gate *no_dispatch = TRUE;
9217c478bd9Sstevel@tonic-gate goto success;
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate }
924*b86a7875SToomas Soome /* FALLTHROUGH */
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate default:
927*b86a7875SToomas Soome syslog(LOG_ERR, "_svcrpcsec_gss: non-data "
928*b86a7875SToomas Soome "request on an established context");
9297c478bd9Sstevel@tonic-gate ret = AUTH_FAILED;
9307c478bd9Sstevel@tonic-gate goto error2;
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate
9347c478bd9Sstevel@tonic-gate /*
9357c478bd9Sstevel@tonic-gate * Once the context is established and there is no more
9367c478bd9Sstevel@tonic-gate * retransmission of last continue init request, it is safe
9377c478bd9Sstevel@tonic-gate * to delete the retransmit cache entry.
9387c478bd9Sstevel@tonic-gate */
9397c478bd9Sstevel@tonic-gate if (client_data->retrans_data)
9407c478bd9Sstevel@tonic-gate retrans_del(client_data);
9417c478bd9Sstevel@tonic-gate
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate * Context is already established. Check verifier, and
9447c478bd9Sstevel@tonic-gate * note parameters we will need for response in gss_parms.
9457c478bd9Sstevel@tonic-gate */
9467c478bd9Sstevel@tonic-gate if (!check_verf(msg, client_data->context,
9477c478bd9Sstevel@tonic-gate &gss_parms->qop_rcvd)) {
9487c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_NOCRED;
9497c478bd9Sstevel@tonic-gate goto error2;
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate * Check and invoke callback if necessary.
9537c478bd9Sstevel@tonic-gate */
9547c478bd9Sstevel@tonic-gate if (!client_data->done_docallback) {
9557c478bd9Sstevel@tonic-gate client_data->done_docallback = TRUE;
9567c478bd9Sstevel@tonic-gate client_data->qop = gss_parms->qop_rcvd;
9577c478bd9Sstevel@tonic-gate client_data->raw_cred.qop = __rpc_gss_num_to_qop(
9587c478bd9Sstevel@tonic-gate client_data->raw_cred.mechanism,
9597c478bd9Sstevel@tonic-gate gss_parms->qop_rcvd);
9607c478bd9Sstevel@tonic-gate client_data->raw_cred.service = creds.service;
9617c478bd9Sstevel@tonic-gate if (!do_callback(rqst, client_data)) {
9627c478bd9Sstevel@tonic-gate ret = AUTH_FAILED;
9637c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
9647c478bd9Sstevel@tonic-gate goto error2;
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate * If the context was locked, make sure that the client
9707c478bd9Sstevel@tonic-gate * has not changed QOP.
9717c478bd9Sstevel@tonic-gate */
9727c478bd9Sstevel@tonic-gate if (client_data->locked &&
9737c478bd9Sstevel@tonic-gate gss_parms->qop_rcvd != client_data->qop) {
9747c478bd9Sstevel@tonic-gate ret = AUTH_BADVERF;
9757c478bd9Sstevel@tonic-gate goto error2;
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate
9787c478bd9Sstevel@tonic-gate /*
9797c478bd9Sstevel@tonic-gate * Validate sequence number.
9807c478bd9Sstevel@tonic-gate */
9817c478bd9Sstevel@tonic-gate if (!check_seq(client_data, creds.seq_num,
9827c478bd9Sstevel@tonic-gate &client_data->stale)) {
9837c478bd9Sstevel@tonic-gate if (client_data->stale)
9847c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_FAILED;
9857c478bd9Sstevel@tonic-gate else {
9867c478bd9Sstevel@tonic-gate /*
9877c478bd9Sstevel@tonic-gate * Operational error, drop packet silently.
9887c478bd9Sstevel@tonic-gate * The client will recover after timing out,
9897c478bd9Sstevel@tonic-gate * assuming this is a client error and not
9907c478bd9Sstevel@tonic-gate * a relpay attack. Don't dispatch.
9917c478bd9Sstevel@tonic-gate */
9927c478bd9Sstevel@tonic-gate ret = AUTH_OK;
9937c478bd9Sstevel@tonic-gate *no_dispatch = TRUE;
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate goto error2;
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate
9987c478bd9Sstevel@tonic-gate /*
9997c478bd9Sstevel@tonic-gate * set response verifier
10007c478bd9Sstevel@tonic-gate */
10017c478bd9Sstevel@tonic-gate if (!set_response_verf(rqst, msg, client_data, creds.seq_num)) {
10027c478bd9Sstevel@tonic-gate ret = RPCSEC_GSS_FAILED;
10037c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
10047c478bd9Sstevel@tonic-gate goto error2;
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate * If this is a control message RPCSEC_GSS_DESTROY, process
10097c478bd9Sstevel@tonic-gate * the call; otherwise, return AUTH_OK so it will be
10107c478bd9Sstevel@tonic-gate * dispatched to the application server.
10117c478bd9Sstevel@tonic-gate */
10127c478bd9Sstevel@tonic-gate if (creds.gss_proc == RPCSEC_GSS_DESTROY) {
10137c478bd9Sstevel@tonic-gate svc_sendreply(rqst->rq_xprt, xdr_void, NULL);
10147c478bd9Sstevel@tonic-gate *no_dispatch = TRUE;
10157c478bd9Sstevel@tonic-gate client_data->stale = TRUE;
10167c478bd9Sstevel@tonic-gate
10177c478bd9Sstevel@tonic-gate } else {
10187c478bd9Sstevel@tonic-gate /*
10197c478bd9Sstevel@tonic-gate * This should be an RPCSEC_GSS_DATA request.
10207c478bd9Sstevel@tonic-gate * If context is locked, make sure that the client
10217c478bd9Sstevel@tonic-gate * has not changed the security service.
10227c478bd9Sstevel@tonic-gate */
10237c478bd9Sstevel@tonic-gate if (client_data->locked &&
10247c478bd9Sstevel@tonic-gate client_data->raw_cred.service != creds.service) {
10257c478bd9Sstevel@tonic-gate ret = AUTH_FAILED;
10267c478bd9Sstevel@tonic-gate goto error2;
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate /*
10307c478bd9Sstevel@tonic-gate * Set client credentials to raw credential
10317c478bd9Sstevel@tonic-gate * structure in context. This is okay, since
10327c478bd9Sstevel@tonic-gate * this will not change during the lifetime of
10337c478bd9Sstevel@tonic-gate * the context (so it's MT safe).
10347c478bd9Sstevel@tonic-gate */
10357c478bd9Sstevel@tonic-gate rqst->rq_clntcred = (char *)&client_data->raw_cred;
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate
10397c478bd9Sstevel@tonic-gate success:
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate * Success.
10427c478bd9Sstevel@tonic-gate */
10437c478bd9Sstevel@tonic-gate if (creds.ctx_handle.length != 0)
10447c478bd9Sstevel@tonic-gate xdr_free(__xdr_rpc_gss_creds, (caddr_t)&creds);
10457c478bd9Sstevel@tonic-gate mutex_unlock(&client_data->clm);
10467c478bd9Sstevel@tonic-gate return (AUTH_OK);
10477c478bd9Sstevel@tonic-gate error2:
10487c478bd9Sstevel@tonic-gate mutex_unlock(&client_data->clm);
10497c478bd9Sstevel@tonic-gate error:
10507c478bd9Sstevel@tonic-gate /*
10517c478bd9Sstevel@tonic-gate * Failure.
10527c478bd9Sstevel@tonic-gate */
10537c478bd9Sstevel@tonic-gate if (creds.ctx_handle.length != 0)
10547c478bd9Sstevel@tonic-gate xdr_free(__xdr_rpc_gss_creds, (caddr_t)&creds);
10557c478bd9Sstevel@tonic-gate return (ret);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate
10587c478bd9Sstevel@tonic-gate /*
10597c478bd9Sstevel@tonic-gate * Check verifier. The verifier is the checksum of the RPC header
10607c478bd9Sstevel@tonic-gate * upto and including the credentials field.
10617c478bd9Sstevel@tonic-gate */
10627c478bd9Sstevel@tonic-gate static bool_t
check_verf(struct rpc_msg * msg,gss_ctx_id_t context,int * qop_state)1063*b86a7875SToomas Soome check_verf(struct rpc_msg *msg, gss_ctx_id_t context, int *qop_state)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate int *buf, *tmp;
10667c478bd9Sstevel@tonic-gate int hdr[32];
10677c478bd9Sstevel@tonic-gate struct opaque_auth *oa;
10687c478bd9Sstevel@tonic-gate int len;
10697c478bd9Sstevel@tonic-gate gss_buffer_desc msg_buf;
10707c478bd9Sstevel@tonic-gate gss_buffer_desc tok_buf;
10717c478bd9Sstevel@tonic-gate OM_uint32 gssstat, minor_stat;
10727c478bd9Sstevel@tonic-gate
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate * We have to reconstruct the RPC header from the previously
10757c478bd9Sstevel@tonic-gate * parsed information, since we haven't kept the header intact.
10767c478bd9Sstevel@tonic-gate */
1077a38dd497Spk193450
1078a38dd497Spk193450 oa = &msg->rm_call.cb_cred;
1079a38dd497Spk193450 if (oa->oa_length > MAX_AUTH_BYTES)
1080a38dd497Spk193450 return (FALSE);
1081a38dd497Spk193450
1082a38dd497Spk193450 /* 8 XDR units from the IXDR macro calls. */
1083*b86a7875SToomas Soome if (sizeof (hdr) < (8 * BYTES_PER_XDR_UNIT + RNDUP(oa->oa_length)))
1084a38dd497Spk193450 return (FALSE);
10857c478bd9Sstevel@tonic-gate buf = hdr;
1086a38dd497Spk193450
10877c478bd9Sstevel@tonic-gate IXDR_PUT_U_INT32(buf, msg->rm_xid);
10887c478bd9Sstevel@tonic-gate IXDR_PUT_ENUM(buf, msg->rm_direction);
10897c478bd9Sstevel@tonic-gate IXDR_PUT_U_INT32(buf, msg->rm_call.cb_rpcvers);
10907c478bd9Sstevel@tonic-gate IXDR_PUT_U_INT32(buf, msg->rm_call.cb_prog);
10917c478bd9Sstevel@tonic-gate IXDR_PUT_U_INT32(buf, msg->rm_call.cb_vers);
10927c478bd9Sstevel@tonic-gate IXDR_PUT_U_INT32(buf, msg->rm_call.cb_proc);
10937c478bd9Sstevel@tonic-gate IXDR_PUT_ENUM(buf, oa->oa_flavor);
10947c478bd9Sstevel@tonic-gate IXDR_PUT_U_INT32(buf, oa->oa_length);
10957c478bd9Sstevel@tonic-gate if (oa->oa_length) {
10967c478bd9Sstevel@tonic-gate len = RNDUP(oa->oa_length);
10977c478bd9Sstevel@tonic-gate tmp = buf;
10987c478bd9Sstevel@tonic-gate buf += len / sizeof (int);
10997c478bd9Sstevel@tonic-gate *(buf - 1) = 0;
11007c478bd9Sstevel@tonic-gate (void) memcpy((caddr_t)tmp, oa->oa_base, oa->oa_length);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate len = ((char *)buf) - (char *)hdr;
11037c478bd9Sstevel@tonic-gate msg_buf.length = len;
11047c478bd9Sstevel@tonic-gate msg_buf.value = (char *)hdr;
11057c478bd9Sstevel@tonic-gate oa = &msg->rm_call.cb_verf;
11067c478bd9Sstevel@tonic-gate tok_buf.length = oa->oa_length;
11077c478bd9Sstevel@tonic-gate tok_buf.value = oa->oa_base;
11087c478bd9Sstevel@tonic-gate
11097c478bd9Sstevel@tonic-gate gssstat = gss_verify(&minor_stat, context, &msg_buf, &tok_buf,
11107c478bd9Sstevel@tonic-gate qop_state);
11117c478bd9Sstevel@tonic-gate if (gssstat != GSS_S_COMPLETE)
11127c478bd9Sstevel@tonic-gate return (FALSE);
11137c478bd9Sstevel@tonic-gate return (TRUE);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate /*
11177c478bd9Sstevel@tonic-gate * Set response verifier. This is the checksum of the given number.
11187c478bd9Sstevel@tonic-gate * (e.g. sequence number or sequence window)
11197c478bd9Sstevel@tonic-gate */
11207c478bd9Sstevel@tonic-gate static bool_t
set_response_verf(struct svc_req * rqst,struct rpc_msg * msg,svc_rpc_gss_data * cl,uint_t num)1121*b86a7875SToomas Soome set_response_verf(struct svc_req *rqst, struct rpc_msg *msg,
1122*b86a7875SToomas Soome svc_rpc_gss_data *cl, uint_t num)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate OM_uint32 minor;
11257c478bd9Sstevel@tonic-gate gss_buffer_desc in_buf, out_buf;
11267c478bd9Sstevel@tonic-gate uint_t num_net;
11277c478bd9Sstevel@tonic-gate
11287c478bd9Sstevel@tonic-gate num_net = (uint_t)htonl(num);
11297c478bd9Sstevel@tonic-gate in_buf.length = sizeof (num);
11307c478bd9Sstevel@tonic-gate in_buf.value = (char *)&num_net;
11317c478bd9Sstevel@tonic-gate if (gss_sign(&minor, cl->context, cl->qop, &in_buf,
11327c478bd9Sstevel@tonic-gate &out_buf) != GSS_S_COMPLETE)
11337c478bd9Sstevel@tonic-gate return (FALSE);
11347c478bd9Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_flavor = RPCSEC_GSS;
11357c478bd9Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
11367c478bd9Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_length = out_buf.length;
11377c478bd9Sstevel@tonic-gate memcpy(rqst->rq_xprt->xp_verf.oa_base, out_buf.value,
11387c478bd9Sstevel@tonic-gate out_buf.length);
11397c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&minor, &out_buf);
11407c478bd9Sstevel@tonic-gate return (TRUE);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate /*
11447c478bd9Sstevel@tonic-gate * Create client context.
11457c478bd9Sstevel@tonic-gate */
11467c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *
create_client(void)1147*b86a7875SToomas Soome create_client(void)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate svc_rpc_gss_data *client_data;
11507c478bd9Sstevel@tonic-gate static uint_t key = 1;
11517c478bd9Sstevel@tonic-gate
11527c478bd9Sstevel@tonic-gate client_data = (svc_rpc_gss_data *) malloc(sizeof (*client_data));
11537c478bd9Sstevel@tonic-gate if (client_data == NULL)
11547c478bd9Sstevel@tonic-gate return (NULL);
11557c478bd9Sstevel@tonic-gate memset((char *)client_data, 0, sizeof (*client_data));
11567c478bd9Sstevel@tonic-gate
11577c478bd9Sstevel@tonic-gate /*
11587c478bd9Sstevel@tonic-gate * set up client data structure
11597c478bd9Sstevel@tonic-gate */
11607c478bd9Sstevel@tonic-gate client_data->established = FALSE;
11617c478bd9Sstevel@tonic-gate client_data->locked = FALSE;
11627c478bd9Sstevel@tonic-gate client_data->u_cred_set = FALSE;
11637c478bd9Sstevel@tonic-gate client_data->context = GSS_C_NO_CONTEXT;
11647c478bd9Sstevel@tonic-gate client_data->expiration = init_lifetime + time(0);
11657c478bd9Sstevel@tonic-gate client_data->ref_cnt = 1;
11667c478bd9Sstevel@tonic-gate client_data->qop = GSS_C_QOP_DEFAULT;
11677c478bd9Sstevel@tonic-gate client_data->done_docallback = FALSE;
11687c478bd9Sstevel@tonic-gate client_data->stale = FALSE;
11697c478bd9Sstevel@tonic-gate client_data->time_secs_set = 0;
11707c478bd9Sstevel@tonic-gate client_data->retrans_data = NULL;
11717c478bd9Sstevel@tonic-gate mutex_init(&client_data->clm, USYNC_THREAD, NULL);
11727c478bd9Sstevel@tonic-gate /*
11737c478bd9Sstevel@tonic-gate * Check totals. If we've hit the limit, we destroy a context
11747c478bd9Sstevel@tonic-gate * based on LRU method.
11757c478bd9Sstevel@tonic-gate */
11767c478bd9Sstevel@tonic-gate mutex_lock(&ctx_mutex);
11777c478bd9Sstevel@tonic-gate if (num_gss_contexts >= max_gss_contexts) {
11787c478bd9Sstevel@tonic-gate /*
11797c478bd9Sstevel@tonic-gate * now try on LRU basis
11807c478bd9Sstevel@tonic-gate */
11817c478bd9Sstevel@tonic-gate drop_lru_client();
11827c478bd9Sstevel@tonic-gate if (num_gss_contexts >= max_gss_contexts) {
11837c478bd9Sstevel@tonic-gate mutex_unlock(&ctx_mutex);
11847c478bd9Sstevel@tonic-gate free((char *)client_data);
11857c478bd9Sstevel@tonic-gate return (NULL);
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate }
11887c478bd9Sstevel@tonic-gate
11897c478bd9Sstevel@tonic-gate /*
11907c478bd9Sstevel@tonic-gate * The client context handle is a 32-bit key (unsigned int).
11917c478bd9Sstevel@tonic-gate * The key is incremented until there is no duplicate for it.
11927c478bd9Sstevel@tonic-gate */
11937c478bd9Sstevel@tonic-gate for (;;) {
11947c478bd9Sstevel@tonic-gate client_data->key = key++;
11957c478bd9Sstevel@tonic-gate if (find_client(client_data->key) == NULL) {
11967c478bd9Sstevel@tonic-gate insert_client(client_data);
11977c478bd9Sstevel@tonic-gate /*
11987c478bd9Sstevel@tonic-gate * Set cleanup callback if we haven't.
11997c478bd9Sstevel@tonic-gate */
12007c478bd9Sstevel@tonic-gate if (!cleanup_cb_set) {
12017c478bd9Sstevel@tonic-gate old_cleanup_cb =
12027c478bd9Sstevel@tonic-gate (void (*)()) __svc_set_proc_cleanup_cb(
12037c478bd9Sstevel@tonic-gate (void *)ctx_cleanup);
12047c478bd9Sstevel@tonic-gate cleanup_cb_set = TRUE;
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate mutex_unlock(&ctx_mutex);
12077c478bd9Sstevel@tonic-gate return (client_data);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate /*NOTREACHED*/
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate * Insert client context into hash list and LRU list.
12157c478bd9Sstevel@tonic-gate */
12167c478bd9Sstevel@tonic-gate static void
insert_client(svc_rpc_gss_data * client_data)1217*b86a7875SToomas Soome insert_client(svc_rpc_gss_data *client_data)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate svc_rpc_gss_data *cl;
12207c478bd9Sstevel@tonic-gate int index = (client_data->key & HASHMASK);
12217c478bd9Sstevel@tonic-gate
12227c478bd9Sstevel@tonic-gate client_data->prev = NULL;
12237c478bd9Sstevel@tonic-gate cl = clients[index];
12247c478bd9Sstevel@tonic-gate if ((client_data->next = cl) != NULL)
12257c478bd9Sstevel@tonic-gate cl->prev = client_data;
12267c478bd9Sstevel@tonic-gate clients[index] = client_data;
12277c478bd9Sstevel@tonic-gate
12287c478bd9Sstevel@tonic-gate client_data->lru_prev = NULL;
12297c478bd9Sstevel@tonic-gate if ((client_data->lru_next = lru_first) != NULL)
12307c478bd9Sstevel@tonic-gate lru_first->lru_prev = client_data;
12317c478bd9Sstevel@tonic-gate else
12327c478bd9Sstevel@tonic-gate lru_last = client_data;
12337c478bd9Sstevel@tonic-gate lru_first = client_data;
12347c478bd9Sstevel@tonic-gate
12357c478bd9Sstevel@tonic-gate num_gss_contexts++;
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate
12387c478bd9Sstevel@tonic-gate /*
12397c478bd9Sstevel@tonic-gate * Fetch a client, given the client context handle. Move it to the
12407c478bd9Sstevel@tonic-gate * top of the LRU list since this is the most recently used context.
12417c478bd9Sstevel@tonic-gate */
12427c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *
get_client(gss_buffer_t ctx_handle)1243*b86a7875SToomas Soome get_client(gss_buffer_t ctx_handle)
12447c478bd9Sstevel@tonic-gate {
12457c478bd9Sstevel@tonic-gate uint_t key = *(uint_t *)ctx_handle->value;
12467c478bd9Sstevel@tonic-gate svc_rpc_gss_data *cl;
12477c478bd9Sstevel@tonic-gate
12487c478bd9Sstevel@tonic-gate mutex_lock(&ctx_mutex);
12497c478bd9Sstevel@tonic-gate if ((cl = find_client(key)) != NULL) {
12507c478bd9Sstevel@tonic-gate mutex_lock(&cl->clm);
12517c478bd9Sstevel@tonic-gate if (cl->stale) {
12527c478bd9Sstevel@tonic-gate mutex_unlock(&cl->clm);
12537c478bd9Sstevel@tonic-gate mutex_unlock(&ctx_mutex);
12547c478bd9Sstevel@tonic-gate return (NULL);
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate cl->ref_cnt++;
12577c478bd9Sstevel@tonic-gate mutex_unlock(&cl->clm);
12587c478bd9Sstevel@tonic-gate if (cl != lru_first) {
12597c478bd9Sstevel@tonic-gate cl->lru_prev->lru_next = cl->lru_next;
12607c478bd9Sstevel@tonic-gate if (cl->lru_next != NULL)
12617c478bd9Sstevel@tonic-gate cl->lru_next->lru_prev = cl->lru_prev;
12627c478bd9Sstevel@tonic-gate else
12637c478bd9Sstevel@tonic-gate lru_last = cl->lru_prev;
12647c478bd9Sstevel@tonic-gate cl->lru_prev = NULL;
12657c478bd9Sstevel@tonic-gate cl->lru_next = lru_first;
12667c478bd9Sstevel@tonic-gate lru_first->lru_prev = cl;
12677c478bd9Sstevel@tonic-gate lru_first = cl;
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate mutex_unlock(&ctx_mutex);
12717c478bd9Sstevel@tonic-gate return (cl);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate * Given the client context handle, find the context corresponding to it.
12767c478bd9Sstevel@tonic-gate * Don't change its LRU state since it may not be used.
12777c478bd9Sstevel@tonic-gate */
12787c478bd9Sstevel@tonic-gate static svc_rpc_gss_data *
find_client(uint_t key)1279*b86a7875SToomas Soome find_client(uint_t key)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate int index = (key & HASHMASK);
12827c478bd9Sstevel@tonic-gate svc_rpc_gss_data *cl;
12837c478bd9Sstevel@tonic-gate
12847c478bd9Sstevel@tonic-gate for (cl = clients[index]; cl != NULL; cl = cl->next) {
12857c478bd9Sstevel@tonic-gate if (cl->key == key)
12867c478bd9Sstevel@tonic-gate break;
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate return (cl);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate * Destroy a client context.
12937c478bd9Sstevel@tonic-gate */
12947c478bd9Sstevel@tonic-gate static void
destroy_client(svc_rpc_gss_data * client_data)1295*b86a7875SToomas Soome destroy_client(svc_rpc_gss_data *client_data)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate OM_uint32 minor;
12987c478bd9Sstevel@tonic-gate int index = (client_data->key & HASHMASK);
12997c478bd9Sstevel@tonic-gate
13007c478bd9Sstevel@tonic-gate /*
13017c478bd9Sstevel@tonic-gate * remove from hash list
13027c478bd9Sstevel@tonic-gate */
13037c478bd9Sstevel@tonic-gate if (client_data->prev == NULL)
13047c478bd9Sstevel@tonic-gate clients[index] = client_data->next;
13057c478bd9Sstevel@tonic-gate else
13067c478bd9Sstevel@tonic-gate client_data->prev->next = client_data->next;
13077c478bd9Sstevel@tonic-gate if (client_data->next != NULL)
13087c478bd9Sstevel@tonic-gate client_data->next->prev = client_data->prev;
13097c478bd9Sstevel@tonic-gate
13107c478bd9Sstevel@tonic-gate /*
13117c478bd9Sstevel@tonic-gate * remove from LRU list
13127c478bd9Sstevel@tonic-gate */
13137c478bd9Sstevel@tonic-gate if (client_data->lru_prev == NULL)
13147c478bd9Sstevel@tonic-gate lru_first = client_data->lru_next;
13157c478bd9Sstevel@tonic-gate else
13167c478bd9Sstevel@tonic-gate client_data->lru_prev->lru_next = client_data->lru_next;
13177c478bd9Sstevel@tonic-gate if (client_data->lru_next != NULL)
13187c478bd9Sstevel@tonic-gate client_data->lru_next->lru_prev = client_data->lru_prev;
13197c478bd9Sstevel@tonic-gate else
13207c478bd9Sstevel@tonic-gate lru_last = client_data->lru_prev;
13217c478bd9Sstevel@tonic-gate
13227c478bd9Sstevel@tonic-gate /*
13237c478bd9Sstevel@tonic-gate * If there is a GSS context, clean up GSS state.
13247c478bd9Sstevel@tonic-gate */
13257c478bd9Sstevel@tonic-gate if (client_data->context != GSS_C_NO_CONTEXT) {
13267c478bd9Sstevel@tonic-gate (void) gss_delete_sec_context(&minor, &client_data->context,
13277c478bd9Sstevel@tonic-gate NULL);
1328*b86a7875SToomas Soome if (client_data->client_name) {
1329*b86a7875SToomas Soome (void) gss_release_name(&minor,
1330*b86a7875SToomas Soome &client_data->client_name);
1331*b86a7875SToomas Soome }
1332*b86a7875SToomas Soome free(client_data->raw_cred.client_principal);
1333*b86a7875SToomas Soome free(client_data->u_cred.gidlist);
13347c478bd9Sstevel@tonic-gate if (client_data->deleg != GSS_C_NO_CREDENTIAL)
13357c478bd9Sstevel@tonic-gate (void) gss_release_cred(&minor, &client_data->deleg);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate
13387c478bd9Sstevel@tonic-gate if (client_data->retrans_data != NULL)
13397c478bd9Sstevel@tonic-gate retrans_del(client_data);
13407c478bd9Sstevel@tonic-gate
13417c478bd9Sstevel@tonic-gate free(client_data);
13427c478bd9Sstevel@tonic-gate num_gss_contexts--;
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate
13457c478bd9Sstevel@tonic-gate /*
13467c478bd9Sstevel@tonic-gate * Check for expired client contexts.
13477c478bd9Sstevel@tonic-gate */
13487c478bd9Sstevel@tonic-gate static void
sweep_clients(void)1349*b86a7875SToomas Soome sweep_clients(void)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate svc_rpc_gss_data *cl, *next;
13527c478bd9Sstevel@tonic-gate int index;
13537c478bd9Sstevel@tonic-gate
13547c478bd9Sstevel@tonic-gate for (index = 0; index < HASHMOD; index++) {
13557c478bd9Sstevel@tonic-gate cl = clients[index];
13567c478bd9Sstevel@tonic-gate while (cl) {
13577c478bd9Sstevel@tonic-gate next = cl->next;
13587c478bd9Sstevel@tonic-gate mutex_lock(&cl->clm);
13597c478bd9Sstevel@tonic-gate if ((cl->expiration != GSS_C_INDEFINITE &&
13607c478bd9Sstevel@tonic-gate cl->expiration <= time(0)) || cl->stale) {
13617c478bd9Sstevel@tonic-gate cl->stale = TRUE;
13627c478bd9Sstevel@tonic-gate if (cl->ref_cnt == 0) {
13637c478bd9Sstevel@tonic-gate mutex_unlock(&cl->clm);
13647c478bd9Sstevel@tonic-gate destroy_client(cl);
13657c478bd9Sstevel@tonic-gate } else
13667c478bd9Sstevel@tonic-gate mutex_unlock(&cl->clm);
13677c478bd9Sstevel@tonic-gate } else
13687c478bd9Sstevel@tonic-gate mutex_unlock(&cl->clm);
13697c478bd9Sstevel@tonic-gate cl = next;
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate last_swept = time(0);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate
13757c478bd9Sstevel@tonic-gate /*
13767c478bd9Sstevel@tonic-gate * Drop the least recently used client context, if possible.
13777c478bd9Sstevel@tonic-gate */
13787c478bd9Sstevel@tonic-gate static void
drop_lru_client(void)1379*b86a7875SToomas Soome drop_lru_client(void)
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate mutex_lock(&lru_last->clm);
13827c478bd9Sstevel@tonic-gate lru_last->stale = TRUE;
13837c478bd9Sstevel@tonic-gate mutex_unlock(&lru_last->clm);
13847c478bd9Sstevel@tonic-gate if (lru_last->ref_cnt == 0)
13857c478bd9Sstevel@tonic-gate destroy_client(lru_last);
13867c478bd9Sstevel@tonic-gate else
13877c478bd9Sstevel@tonic-gate sweep_clients();
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate
13907c478bd9Sstevel@tonic-gate /*
13917c478bd9Sstevel@tonic-gate * find service credentials
13927c478bd9Sstevel@tonic-gate * return cred if found,
13937c478bd9Sstevel@tonic-gate * other wise, NULL
13947c478bd9Sstevel@tonic-gate */
13957c478bd9Sstevel@tonic-gate svc_creds_list_t *
find_svc_cred(char * service_name,uint_t program,uint_t version)1396*b86a7875SToomas Soome find_svc_cred(char *service_name, uint_t program, uint_t version)
1397*b86a7875SToomas Soome {
13987c478bd9Sstevel@tonic-gate
13997c478bd9Sstevel@tonic-gate svc_creds_list_t *sc;
14007c478bd9Sstevel@tonic-gate
14017c478bd9Sstevel@tonic-gate if (!svc_creds_list)
14027c478bd9Sstevel@tonic-gate return (NULL);
14037c478bd9Sstevel@tonic-gate
14047c478bd9Sstevel@tonic-gate for (sc = svc_creds_list; sc != NULL; sc = sc->next) {
14057c478bd9Sstevel@tonic-gate if (program != sc->program || version != sc->version)
14067c478bd9Sstevel@tonic-gate continue;
14077c478bd9Sstevel@tonic-gate
14087c478bd9Sstevel@tonic-gate if (strcmp(service_name, sc->server_name) != 0)
14097c478bd9Sstevel@tonic-gate continue;
14107c478bd9Sstevel@tonic-gate return (sc);
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate return (NULL);
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate
14157c478bd9Sstevel@tonic-gate /*
14167c478bd9Sstevel@tonic-gate * Set the server principal name.
14177c478bd9Sstevel@tonic-gate */
14187c478bd9Sstevel@tonic-gate bool_t
__rpc_gss_set_svc_name(char * server_name,char * mech,OM_uint32 req_time,uint_t program,uint_t version)1419*b86a7875SToomas Soome __rpc_gss_set_svc_name(char *server_name, char *mech, OM_uint32 req_time,
1420*b86a7875SToomas Soome uint_t program, uint_t version)
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate gss_name_t name;
14237c478bd9Sstevel@tonic-gate svc_creds_list_t *svc_cred;
14247c478bd9Sstevel@tonic-gate gss_OID mechanism;
14257c478bd9Sstevel@tonic-gate gss_OID_set_desc oid_set_desc;
14267c478bd9Sstevel@tonic-gate gss_OID_set oid_set;
14277c478bd9Sstevel@tonic-gate OM_uint32 ret_time;
14287c478bd9Sstevel@tonic-gate OM_uint32 major, minor;
14297c478bd9Sstevel@tonic-gate gss_buffer_desc name_buf;
14307c478bd9Sstevel@tonic-gate
14317c478bd9Sstevel@tonic-gate if (!__rpc_gss_mech_to_oid(mech, &mechanism)) {
14327c478bd9Sstevel@tonic-gate return (FALSE);
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate
14357c478bd9Sstevel@tonic-gate name_buf.value = server_name;
14367c478bd9Sstevel@tonic-gate name_buf.length = strlen(server_name);
14377c478bd9Sstevel@tonic-gate major = gss_import_name(&minor, &name_buf,
14387c478bd9Sstevel@tonic-gate (gss_OID) GSS_C_NT_HOSTBASED_SERVICE, &name);
14397c478bd9Sstevel@tonic-gate if (major != GSS_S_COMPLETE) {
14407c478bd9Sstevel@tonic-gate return (FALSE);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate
14437c478bd9Sstevel@tonic-gate /* Check if there is already an entry in the svc_creds_list. */
14447c478bd9Sstevel@tonic-gate rw_wrlock(&cred_lock);
14457c478bd9Sstevel@tonic-gate if (svc_cred = find_svc_cred(server_name, program, version)) {
14467c478bd9Sstevel@tonic-gate
14477c478bd9Sstevel@tonic-gate major = gss_add_cred(&minor, svc_cred->cred, name,
1448*b86a7875SToomas Soome mechanism, GSS_C_ACCEPT, 0, req_time, NULL,
1449*b86a7875SToomas Soome &oid_set, NULL, &ret_time);
14507c478bd9Sstevel@tonic-gate (void) gss_release_name(&minor, &name);
14517c478bd9Sstevel@tonic-gate if (major == GSS_S_COMPLETE) {
14527c478bd9Sstevel@tonic-gate /*
14537c478bd9Sstevel@tonic-gate * Successfully added the mech to the cred handle
14547c478bd9Sstevel@tonic-gate * free the existing oid_set in svc_cred
14557c478bd9Sstevel@tonic-gate */
14567c478bd9Sstevel@tonic-gate gss_release_oid_set(&minor, &svc_cred->oid_set);
14577c478bd9Sstevel@tonic-gate svc_cred->oid_set = oid_set;
14587c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
14597c478bd9Sstevel@tonic-gate return (TRUE);
14607c478bd9Sstevel@tonic-gate } else if (major == GSS_S_DUPLICATE_ELEMENT) {
14617c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
14627c478bd9Sstevel@tonic-gate return (TRUE);
14637c478bd9Sstevel@tonic-gate } else if (major == GSS_S_CREDENTIALS_EXPIRED) {
14647c478bd9Sstevel@tonic-gate if (rpc_gss_refresh_svc_cred(svc_cred)) {
14657c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
14667c478bd9Sstevel@tonic-gate return (TRUE);
14677c478bd9Sstevel@tonic-gate } else {
14687c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
14697c478bd9Sstevel@tonic-gate return (FALSE);
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate } else {
14727c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
14737c478bd9Sstevel@tonic-gate return (FALSE);
14747c478bd9Sstevel@tonic-gate }
14757c478bd9Sstevel@tonic-gate } else {
14767c478bd9Sstevel@tonic-gate svc_cred = (svc_creds_list_t *)malloc(sizeof (*svc_cred));
14777c478bd9Sstevel@tonic-gate if (svc_cred == NULL) {
14787c478bd9Sstevel@tonic-gate (void) gss_release_name(&minor, &name);
14797c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
14807c478bd9Sstevel@tonic-gate return (FALSE);
14817c478bd9Sstevel@tonic-gate }
14827c478bd9Sstevel@tonic-gate oid_set_desc.count = 1;
14837c478bd9Sstevel@tonic-gate oid_set_desc.elements = mechanism;
1484*b86a7875SToomas Soome major = gss_acquire_cred(&minor, name, req_time, &oid_set_desc,
1485*b86a7875SToomas Soome GSS_C_ACCEPT, &svc_cred->cred, &oid_set, &ret_time);
14867c478bd9Sstevel@tonic-gate
14877c478bd9Sstevel@tonic-gate if (major != GSS_S_COMPLETE) {
14887c478bd9Sstevel@tonic-gate (void) gss_release_name(&minor, &name);
14897c478bd9Sstevel@tonic-gate free(svc_cred);
14907c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
14917c478bd9Sstevel@tonic-gate return (FALSE);
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate
14947c478bd9Sstevel@tonic-gate svc_cred->name = name;
14957c478bd9Sstevel@tonic-gate svc_cred->program = program;
14967c478bd9Sstevel@tonic-gate svc_cred->version = version;
14977c478bd9Sstevel@tonic-gate svc_cred->req_time = req_time;
14987c478bd9Sstevel@tonic-gate svc_cred->oid_set = oid_set;
14997c478bd9Sstevel@tonic-gate svc_cred->server_name = strdup(server_name);
15007c478bd9Sstevel@tonic-gate if (svc_cred->server_name == NULL) {
15017c478bd9Sstevel@tonic-gate (void) gss_release_name(&minor, &name);
15027c478bd9Sstevel@tonic-gate free((char *)svc_cred);
15037c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
15047c478bd9Sstevel@tonic-gate return (FALSE);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate mutex_init(&svc_cred->refresh_mutex, USYNC_THREAD, NULL);
15077c478bd9Sstevel@tonic-gate
15087c478bd9Sstevel@tonic-gate svc_cred->next = svc_creds_list;
15097c478bd9Sstevel@tonic-gate svc_creds_list = svc_cred;
15107c478bd9Sstevel@tonic-gate svc_creds_count++;
15117c478bd9Sstevel@tonic-gate rw_unlock(&cred_lock);
15127c478bd9Sstevel@tonic-gate
15137c478bd9Sstevel@tonic-gate return (TRUE);
15147c478bd9Sstevel@tonic-gate }
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate /*
15177c478bd9Sstevel@tonic-gate * Refresh server credentials.
15187c478bd9Sstevel@tonic-gate */
15197c478bd9Sstevel@tonic-gate static bool_t
rpc_gss_refresh_svc_cred(svc_creds_list_t * svc_cred)1520*b86a7875SToomas Soome rpc_gss_refresh_svc_cred(svc_creds_list_t *svc_cred)
15217c478bd9Sstevel@tonic-gate {
15227c478bd9Sstevel@tonic-gate OM_uint32 major, minor;
15237c478bd9Sstevel@tonic-gate gss_OID_set oid_set;
15247c478bd9Sstevel@tonic-gate OM_uint32 ret_time;
15257c478bd9Sstevel@tonic-gate
15267c478bd9Sstevel@tonic-gate (void) gss_release_cred(&minor, &svc_cred->cred);
15277c478bd9Sstevel@tonic-gate svc_cred->cred = GSS_C_NO_CREDENTIAL;
15287c478bd9Sstevel@tonic-gate major = gss_acquire_cred(&minor, svc_cred->name, svc_cred->req_time,
15297c478bd9Sstevel@tonic-gate svc_cred->oid_set, GSS_C_ACCEPT, &svc_cred->cred, &oid_set,
15307c478bd9Sstevel@tonic-gate &ret_time);
15317c478bd9Sstevel@tonic-gate if (major != GSS_S_COMPLETE) {
15327c478bd9Sstevel@tonic-gate return (FALSE);
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate gss_release_oid_set(&minor, &svc_cred->oid_set);
15357c478bd9Sstevel@tonic-gate svc_cred->oid_set = oid_set;
15367c478bd9Sstevel@tonic-gate return (TRUE);
15377c478bd9Sstevel@tonic-gate }
15387c478bd9Sstevel@tonic-gate
15397c478bd9Sstevel@tonic-gate /*
15407c478bd9Sstevel@tonic-gate * Encrypt the serialized arguments from xdr_func applied to xdr_ptr
15417c478bd9Sstevel@tonic-gate * and write the result to xdrs.
15427c478bd9Sstevel@tonic-gate */
15437c478bd9Sstevel@tonic-gate static bool_t
svc_rpc_gss_wrap(SVCAUTH * auth,XDR * out_xdrs,bool_t (* xdr_func)(),caddr_t xdr_ptr)1544*b86a7875SToomas Soome svc_rpc_gss_wrap(SVCAUTH *auth, XDR *out_xdrs, bool_t (*xdr_func)(),
1545*b86a7875SToomas Soome caddr_t xdr_ptr)
15467c478bd9Sstevel@tonic-gate {
15477c478bd9Sstevel@tonic-gate svc_rpc_gss_parms_t *gss_parms = &auth->svc_gss_parms;
15487c478bd9Sstevel@tonic-gate
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate * If context is not established, or if neither integrity nor
15517c478bd9Sstevel@tonic-gate * privacy service is used, don't wrap - just XDR encode.
15527c478bd9Sstevel@tonic-gate * Otherwise, wrap data using service and QOP parameters.
15537c478bd9Sstevel@tonic-gate */
1554*b86a7875SToomas Soome if (!gss_parms->established || gss_parms->service == rpc_gss_svc_none)
15557c478bd9Sstevel@tonic-gate return ((*xdr_func)(out_xdrs, xdr_ptr));
15567c478bd9Sstevel@tonic-gate
15577c478bd9Sstevel@tonic-gate return (__rpc_gss_wrap_data(gss_parms->service,
15587c478bd9Sstevel@tonic-gate (OM_uint32)gss_parms->qop_rcvd,
15597c478bd9Sstevel@tonic-gate (gss_ctx_id_t)gss_parms->context,
15607c478bd9Sstevel@tonic-gate gss_parms->seq_num,
15617c478bd9Sstevel@tonic-gate out_xdrs, xdr_func, xdr_ptr));
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate
15647c478bd9Sstevel@tonic-gate /*
15657c478bd9Sstevel@tonic-gate * Decrypt the serialized arguments and XDR decode them.
15667c478bd9Sstevel@tonic-gate */
15677c478bd9Sstevel@tonic-gate static bool_t
svc_rpc_gss_unwrap(SVCAUTH * auth,XDR * in_xdrs,bool_t (* xdr_func)(),caddr_t xdr_ptr)1568*b86a7875SToomas Soome svc_rpc_gss_unwrap(SVCAUTH *auth, XDR *in_xdrs, bool_t (*xdr_func)(),
1569*b86a7875SToomas Soome caddr_t xdr_ptr)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate svc_rpc_gss_parms_t *gss_parms = &auth->svc_gss_parms;
15727c478bd9Sstevel@tonic-gate
15737c478bd9Sstevel@tonic-gate /*
15747c478bd9Sstevel@tonic-gate * If context is not established, or if neither integrity nor
15757c478bd9Sstevel@tonic-gate * privacy service is used, don't unwrap - just XDR decode.
15767c478bd9Sstevel@tonic-gate * Otherwise, unwrap data.
15777c478bd9Sstevel@tonic-gate */
1578*b86a7875SToomas Soome if (!gss_parms->established || gss_parms->service == rpc_gss_svc_none)
15797c478bd9Sstevel@tonic-gate return ((*xdr_func)(in_xdrs, xdr_ptr));
15807c478bd9Sstevel@tonic-gate
15817c478bd9Sstevel@tonic-gate return (__rpc_gss_unwrap_data(gss_parms->service,
15827c478bd9Sstevel@tonic-gate (gss_ctx_id_t)gss_parms->context,
15837c478bd9Sstevel@tonic-gate gss_parms->seq_num,
15847c478bd9Sstevel@tonic-gate gss_parms->qop_rcvd,
15857c478bd9Sstevel@tonic-gate in_xdrs, xdr_func, xdr_ptr));
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate
15887c478bd9Sstevel@tonic-gate int
__rpc_gss_svc_max_data_length(struct svc_req * req,int max_tp_unit_len)1589*b86a7875SToomas Soome __rpc_gss_svc_max_data_length(struct svc_req *req, int max_tp_unit_len)
15907c478bd9Sstevel@tonic-gate {
15917c478bd9Sstevel@tonic-gate SVCAUTH *svcauth;
15927c478bd9Sstevel@tonic-gate svc_rpc_gss_parms_t *gss_parms;
15937c478bd9Sstevel@tonic-gate
15947c478bd9Sstevel@tonic-gate svcauth = __svc_get_svcauth(req->rq_xprt);
15957c478bd9Sstevel@tonic-gate gss_parms = &svcauth->svc_gss_parms;
15967c478bd9Sstevel@tonic-gate
15977c478bd9Sstevel@tonic-gate if (!gss_parms->established || max_tp_unit_len <= 0)
15987c478bd9Sstevel@tonic-gate return (0);
15997c478bd9Sstevel@tonic-gate
16007c478bd9Sstevel@tonic-gate return (__find_max_data_length(gss_parms->service,
16017c478bd9Sstevel@tonic-gate (gss_ctx_id_t)gss_parms->context,
16027c478bd9Sstevel@tonic-gate gss_parms->qop_rcvd, max_tp_unit_len));
16037c478bd9Sstevel@tonic-gate }
16047c478bd9Sstevel@tonic-gate
16057c478bd9Sstevel@tonic-gate /*
16067c478bd9Sstevel@tonic-gate * Add retransmit entry to the context cache entry for a new xid.
16077c478bd9Sstevel@tonic-gate * If there is already an entry, delete it before adding the new one.
16087c478bd9Sstevel@tonic-gate */
retrans_add(svc_rpc_gss_data * client,uint32_t xid,rpc_gss_init_res * result)1609*b86a7875SToomas Soome static void retrans_add(svc_rpc_gss_data *client, uint32_t xid,
1610*b86a7875SToomas Soome rpc_gss_init_res *result)
16117c478bd9Sstevel@tonic-gate {
16127c478bd9Sstevel@tonic-gate retrans_entry *rdata;
16137c478bd9Sstevel@tonic-gate
16147c478bd9Sstevel@tonic-gate if (client->retrans_data && client->retrans_data->xid == xid)
16157c478bd9Sstevel@tonic-gate return;
16167c478bd9Sstevel@tonic-gate
16177c478bd9Sstevel@tonic-gate rdata = (retrans_entry *) malloc(sizeof (*rdata));
16187c478bd9Sstevel@tonic-gate if (rdata == NULL)
16197c478bd9Sstevel@tonic-gate return;
16207c478bd9Sstevel@tonic-gate
16217c478bd9Sstevel@tonic-gate rdata->xid = xid;
16227c478bd9Sstevel@tonic-gate rdata->result = *result;
16237c478bd9Sstevel@tonic-gate
16247c478bd9Sstevel@tonic-gate if (result->token.length != 0) {
16257c478bd9Sstevel@tonic-gate GSS_DUP_BUFFER(rdata->result.token, result->token);
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate
16287c478bd9Sstevel@tonic-gate if (client->retrans_data)
16297c478bd9Sstevel@tonic-gate retrans_del(client);
16307c478bd9Sstevel@tonic-gate
16317c478bd9Sstevel@tonic-gate client->retrans_data = rdata;
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate
16347c478bd9Sstevel@tonic-gate /*
16357c478bd9Sstevel@tonic-gate * Delete the retransmit data from the context cache entry.
16367c478bd9Sstevel@tonic-gate */
retrans_del(svc_rpc_gss_data * client)1637*b86a7875SToomas Soome static void retrans_del(svc_rpc_gss_data *client)
16387c478bd9Sstevel@tonic-gate {
16397c478bd9Sstevel@tonic-gate retrans_entry *rdata;
16407c478bd9Sstevel@tonic-gate OM_uint32 minor_stat;
16417c478bd9Sstevel@tonic-gate
16427c478bd9Sstevel@tonic-gate if (client->retrans_data == NULL)
16437c478bd9Sstevel@tonic-gate return;
16447c478bd9Sstevel@tonic-gate
16457c478bd9Sstevel@tonic-gate rdata = client->retrans_data;
16467c478bd9Sstevel@tonic-gate if (rdata->result.token.length != 0) {
16477c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat, &rdata->result.token);
16487c478bd9Sstevel@tonic-gate }
16497c478bd9Sstevel@tonic-gate
16507c478bd9Sstevel@tonic-gate free((caddr_t)rdata);
16517c478bd9Sstevel@tonic-gate client->retrans_data = NULL;
16527c478bd9Sstevel@tonic-gate }
1653