10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*3864Sraf * Common Development and Distribution License (the "License"). 6*3864Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21*3864Sraf 220Sstevel@tonic-gate /* 23*3864Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24*3864Sraf * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved. 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * $Header: 340Sstevel@tonic-gate * /afs/gza.com/product/secure/rel-eng/src/1.1/rpc/RCS/auth_gssapi.c,v 350Sstevel@tonic-gate * 1.14 1995/03/22 22:07:55 jik Exp $ 360Sstevel@tonic-gate */ 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <stdio.h> 390Sstevel@tonic-gate #include <stdlib.h> 400Sstevel@tonic-gate #include <strings.h> 410Sstevel@tonic-gate #include <errno.h> 420Sstevel@tonic-gate #include <pthread.h> 430Sstevel@tonic-gate #include <thread.h> 440Sstevel@tonic-gate #include <syslog.h> 450Sstevel@tonic-gate #include <gssapi/gssapi.h> 460Sstevel@tonic-gate #include <rpc/rpc.h> 470Sstevel@tonic-gate #include <rpc/rpcsec_defs.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate static void rpc_gss_nextverf(); 500Sstevel@tonic-gate static bool_t rpc_gss_marshall(); 510Sstevel@tonic-gate static bool_t rpc_gss_validate(); 520Sstevel@tonic-gate static bool_t rpc_gss_refresh(); 530Sstevel@tonic-gate static void rpc_gss_destroy(); 540Sstevel@tonic-gate static void rpc_gss_destroy_pvt(); 550Sstevel@tonic-gate static bool_t rpc_gss_seccreate_pvt(); 560Sstevel@tonic-gate static bool_t validate_seqwin(); 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * Globals that should have header files but don't. 600Sstevel@tonic-gate */ 610Sstevel@tonic-gate extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *); 620Sstevel@tonic-gate extern int _thr_main(void); 63*3864Sraf extern void *_pthread_getspecific(pthread_key_t key); 640Sstevel@tonic-gate typedef void (*PFrV) (void *); 65*3864Sraf extern int _thr_keycreate_once(thread_key_t *pkey, PFrV destructor); 660Sstevel@tonic-gate extern int _thr_setspecific(unsigned int key, void *value); 670Sstevel@tonic-gate 680Sstevel@tonic-gate 690Sstevel@tonic-gate static struct auth_ops rpc_gss_ops = { 700Sstevel@tonic-gate rpc_gss_nextverf, 710Sstevel@tonic-gate rpc_gss_marshall, 720Sstevel@tonic-gate rpc_gss_validate, 730Sstevel@tonic-gate rpc_gss_refresh, 740Sstevel@tonic-gate rpc_gss_destroy 750Sstevel@tonic-gate }; 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * Private data for RPCSEC_GSS. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate typedef struct _rpc_gss_data { 810Sstevel@tonic-gate bool_t established; /* TRUE when established */ 820Sstevel@tonic-gate CLIENT *clnt; /* associated client handle */ 830Sstevel@tonic-gate uint_t version; /* RPCSEC version */ 840Sstevel@tonic-gate gss_ctx_id_t context; /* GSS context id */ 850Sstevel@tonic-gate gss_buffer_desc ctx_handle; /* RPCSEC context handle */ 860Sstevel@tonic-gate uint_t seq_num; /* last sequence number rcvd */ 870Sstevel@tonic-gate gss_cred_id_t my_cred; /* GSS credentials */ 880Sstevel@tonic-gate OM_uint32 qop; /* requested QOP */ 890Sstevel@tonic-gate rpc_gss_service_t service; /* requested service */ 900Sstevel@tonic-gate uint_t gss_proc; /* GSS control procedure */ 910Sstevel@tonic-gate gss_name_t target_name; /* target server */ 920Sstevel@tonic-gate int req_flags; /* GSS request bits */ 930Sstevel@tonic-gate gss_OID mech_type; /* GSS mechanism */ 940Sstevel@tonic-gate OM_uint32 time_req; /* requested cred lifetime */ 950Sstevel@tonic-gate bool_t invalid; /* can't use this any more */ 960Sstevel@tonic-gate OM_uint32 seq_window; /* server sequence window */ 970Sstevel@tonic-gate struct opaque_auth *verifier; /* rpc reply verifier saved for */ 980Sstevel@tonic-gate /* validating the sequence window */ 990Sstevel@tonic-gate gss_channel_bindings_t icb; 1000Sstevel@tonic-gate } rpc_gss_data; 1010Sstevel@tonic-gate #define AUTH_PRIVATE(auth) ((rpc_gss_data *)auth->ah_private) 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * Create a context. 1050Sstevel@tonic-gate */ 1060Sstevel@tonic-gate AUTH * 1070Sstevel@tonic-gate __rpc_gss_seccreate(clnt, server_name, mech, service, qop, options_req, 1080Sstevel@tonic-gate options_ret) 1090Sstevel@tonic-gate CLIENT *clnt; /* associated client handle */ 1100Sstevel@tonic-gate char *server_name; /* target server */ 1110Sstevel@tonic-gate char *mech; /* security mechanism */ 1120Sstevel@tonic-gate rpc_gss_service_t service; /* security service */ 1130Sstevel@tonic-gate char *qop; /* requested QOP */ 1140Sstevel@tonic-gate rpc_gss_options_req_t *options_req; /* requested options */ 1150Sstevel@tonic-gate rpc_gss_options_ret_t *options_ret; /* returned options */ 1160Sstevel@tonic-gate { 1170Sstevel@tonic-gate OM_uint32 gssstat; 1180Sstevel@tonic-gate OM_uint32 minor_stat; 1190Sstevel@tonic-gate gss_name_t target_name; 1200Sstevel@tonic-gate gss_OID mech_type; 1210Sstevel@tonic-gate OM_uint32 ret_flags; 1220Sstevel@tonic-gate OM_uint32 time_rec; 1230Sstevel@tonic-gate gss_buffer_desc input_name; 1240Sstevel@tonic-gate AUTH *auth = NULL; 1250Sstevel@tonic-gate rpc_gss_data *ap = NULL; 1260Sstevel@tonic-gate OM_uint32 qop_num; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * convert ascii strings to GSS values 1300Sstevel@tonic-gate */ 1310Sstevel@tonic-gate if (!__rpc_gss_qop_to_num(qop, mech, &qop_num)) { 1320Sstevel@tonic-gate return (NULL); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate if (!__rpc_gss_mech_to_oid(mech, &mech_type)) { 1360Sstevel@tonic-gate return (NULL); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * convert name to GSS internal type 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate input_name.value = server_name; 1430Sstevel@tonic-gate input_name.length = strlen(server_name); 1440Sstevel@tonic-gate gssstat = gss_import_name(&minor_stat, &input_name, 1450Sstevel@tonic-gate (gss_OID)GSS_C_NT_HOSTBASED_SERVICE, 1460Sstevel@tonic-gate &target_name); 1470Sstevel@tonic-gate if (gssstat != GSS_S_COMPLETE) { 1480Sstevel@tonic-gate rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR; 1490Sstevel@tonic-gate rpc_gss_err.system_error = ENOMEM; 1500Sstevel@tonic-gate return (NULL); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * Create AUTH handle. Save the necessary interface information 1550Sstevel@tonic-gate * so that the client can refresh the handle later if needed. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate if ((auth = (AUTH *) malloc(sizeof (*auth))) != NULL) 1580Sstevel@tonic-gate ap = (rpc_gss_data *) malloc(sizeof (*ap)); 1590Sstevel@tonic-gate if (auth == NULL || ap == NULL) { 1600Sstevel@tonic-gate rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR; 1610Sstevel@tonic-gate rpc_gss_err.system_error = ENOMEM; 1620Sstevel@tonic-gate if (auth != NULL) 1630Sstevel@tonic-gate free((char *)auth); 1640Sstevel@tonic-gate (void) gss_release_name(&minor_stat, &target_name); 1650Sstevel@tonic-gate return (NULL); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate memset((char *)ap, 0, sizeof (*ap)); 1690Sstevel@tonic-gate ap->clnt = clnt; 1700Sstevel@tonic-gate ap->version = RPCSEC_GSS_VERSION; 1710Sstevel@tonic-gate if (options_req != NULL) { 1720Sstevel@tonic-gate ap->my_cred = options_req->my_cred; 1730Sstevel@tonic-gate ap->req_flags = options_req->req_flags; 1740Sstevel@tonic-gate ap->time_req = options_req->time_req; 1750Sstevel@tonic-gate ap->icb = options_req->input_channel_bindings; 1760Sstevel@tonic-gate } else { 1770Sstevel@tonic-gate ap->my_cred = GSS_C_NO_CREDENTIAL; 1780Sstevel@tonic-gate ap->req_flags = GSS_C_MUTUAL_FLAG; 1790Sstevel@tonic-gate ap->time_req = 0; 1800Sstevel@tonic-gate ap->icb = NULL; 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate if ((ap->service = service) == rpc_gss_svc_default) 1830Sstevel@tonic-gate ap->service = rpc_gss_svc_integrity; 1840Sstevel@tonic-gate ap->qop = qop_num; 1850Sstevel@tonic-gate ap->target_name = target_name; 1860Sstevel@tonic-gate ap->mech_type = mech_type; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate /* 1890Sstevel@tonic-gate * Now invoke the real interface that sets up the context from 1900Sstevel@tonic-gate * the information stashed away in the private data. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate if (!rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth, ap, 1930Sstevel@tonic-gate &mech_type, &ret_flags, &time_rec)) { 1940Sstevel@tonic-gate if (ap->target_name) 1950Sstevel@tonic-gate (void) gss_release_name(&minor_stat, &ap->target_name); 1960Sstevel@tonic-gate free((char *)ap); 1970Sstevel@tonic-gate free((char *)auth); 1980Sstevel@tonic-gate return (NULL); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate /* 2020Sstevel@tonic-gate * Make sure that the requested service is supported. In all 2030Sstevel@tonic-gate * cases, integrity service must be available. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate if ((ap->service == rpc_gss_svc_privacy && 2060Sstevel@tonic-gate !(ret_flags & GSS_C_CONF_FLAG)) || 2070Sstevel@tonic-gate !(ret_flags & GSS_C_INTEG_FLAG)) { 2080Sstevel@tonic-gate rpc_gss_destroy(auth); 2090Sstevel@tonic-gate rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR; 2100Sstevel@tonic-gate rpc_gss_err.system_error = EPROTONOSUPPORT; 2110Sstevel@tonic-gate return (NULL); 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * return option values if requested 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate if (options_ret != NULL) { 2180Sstevel@tonic-gate char *s; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate options_ret->major_status = gssstat; 2210Sstevel@tonic-gate options_ret->minor_status = minor_stat; 2220Sstevel@tonic-gate options_ret->rpcsec_version = ap->version; 2230Sstevel@tonic-gate options_ret->ret_flags = ret_flags; 2240Sstevel@tonic-gate options_ret->time_ret = time_rec; 2250Sstevel@tonic-gate options_ret->gss_context = ap->context; 2260Sstevel@tonic-gate if ((s = __rpc_gss_oid_to_mech(mech_type)) != NULL) 2270Sstevel@tonic-gate strcpy(options_ret->actual_mechanism, s); 2280Sstevel@tonic-gate else 2290Sstevel@tonic-gate options_ret->actual_mechanism[0] = '\0'; 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate return (auth); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * Private interface to create a context. This is the interface 2360Sstevel@tonic-gate * that's invoked when the context has to be refreshed. 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate static bool_t 2390Sstevel@tonic-gate rpc_gss_seccreate_pvt(gssstat, minor_stat, auth, ap, actual_mech_type, 2400Sstevel@tonic-gate ret_flags, time_rec) 2410Sstevel@tonic-gate OM_uint32 *gssstat; 2420Sstevel@tonic-gate OM_uint32 *minor_stat; 2430Sstevel@tonic-gate AUTH *auth; 2440Sstevel@tonic-gate rpc_gss_data *ap; 2450Sstevel@tonic-gate gss_OID *actual_mech_type; 2460Sstevel@tonic-gate OM_uint32 *ret_flags; 2470Sstevel@tonic-gate OM_uint32 *time_rec; 2480Sstevel@tonic-gate { 2490Sstevel@tonic-gate CLIENT *clnt = ap->clnt; 2500Sstevel@tonic-gate AUTH *save_auth; 2510Sstevel@tonic-gate enum clnt_stat callstat; 2520Sstevel@tonic-gate rpc_gss_init_arg call_arg; 2530Sstevel@tonic-gate rpc_gss_init_res call_res; 2540Sstevel@tonic-gate gss_buffer_desc *input_token_p, input_token; 2550Sstevel@tonic-gate bool_t free_results = FALSE; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* 2580Sstevel@tonic-gate * initialize error 2590Sstevel@tonic-gate */ 2600Sstevel@tonic-gate memset(&rpc_createerr, 0, sizeof (rpc_createerr)); 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * (re)initialize AUTH handle and private data. 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate memset((char *)auth, 0, sizeof (*auth)); 2660Sstevel@tonic-gate auth->ah_ops = &rpc_gss_ops; 2670Sstevel@tonic-gate auth->ah_private = (caddr_t)ap; 2680Sstevel@tonic-gate auth->ah_cred.oa_flavor = RPCSEC_GSS; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate ap->established = FALSE; 2710Sstevel@tonic-gate ap->ctx_handle.length = 0; 2720Sstevel@tonic-gate ap->ctx_handle.value = NULL; 2730Sstevel@tonic-gate ap->context = GSS_C_NO_CONTEXT; 2740Sstevel@tonic-gate ap->seq_num = 0; 2750Sstevel@tonic-gate ap->gss_proc = RPCSEC_GSS_INIT; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * should not change clnt->cl_auth at this time, so save 2790Sstevel@tonic-gate * old handle 2800Sstevel@tonic-gate */ 2810Sstevel@tonic-gate save_auth = clnt->cl_auth; 2820Sstevel@tonic-gate clnt->cl_auth = auth; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate /* 2850Sstevel@tonic-gate * set state for starting context setup 2860Sstevel@tonic-gate */ 2870Sstevel@tonic-gate input_token_p = GSS_C_NO_BUFFER; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate next_token: 2900Sstevel@tonic-gate *gssstat = gss_init_sec_context(minor_stat, 2910Sstevel@tonic-gate ap->my_cred, 2920Sstevel@tonic-gate &ap->context, 2930Sstevel@tonic-gate ap->target_name, 2940Sstevel@tonic-gate ap->mech_type, 2950Sstevel@tonic-gate ap->req_flags, 2960Sstevel@tonic-gate ap->time_req, 2970Sstevel@tonic-gate NULL, 2980Sstevel@tonic-gate input_token_p, 2990Sstevel@tonic-gate actual_mech_type, 3000Sstevel@tonic-gate &call_arg, 3010Sstevel@tonic-gate ret_flags, 3020Sstevel@tonic-gate time_rec); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate if (input_token_p != GSS_C_NO_BUFFER) { 3050Sstevel@tonic-gate OM_uint32 minor_stat2; 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat2, input_token_p); 3080Sstevel@tonic-gate input_token_p = GSS_C_NO_BUFFER; 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate if (*gssstat != GSS_S_COMPLETE && *gssstat != GSS_S_CONTINUE_NEEDED) { 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate goto cleanup; 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate /* 3170Sstevel@tonic-gate * if we got a token, pass it on 3180Sstevel@tonic-gate */ 3190Sstevel@tonic-gate if (call_arg.length != 0) { 3200Sstevel@tonic-gate struct timeval timeout = {30, 0}; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate memset((char *)&call_res, 0, sizeof (call_res)); 3230Sstevel@tonic-gate callstat = clnt_call(clnt, NULLPROC, 3240Sstevel@tonic-gate __xdr_rpc_gss_init_arg, (caddr_t)&call_arg, 3250Sstevel@tonic-gate __xdr_rpc_gss_init_res, (caddr_t)&call_res, 3260Sstevel@tonic-gate timeout); 3270Sstevel@tonic-gate (void) gss_release_buffer(minor_stat, &call_arg); 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate if (callstat != RPC_SUCCESS) { 3300Sstevel@tonic-gate goto cleanup; 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate /* 3330Sstevel@tonic-gate * we have results - note that these need to be freed 3340Sstevel@tonic-gate */ 3350Sstevel@tonic-gate free_results = TRUE; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate if (call_res.gss_major != GSS_S_COMPLETE && 3380Sstevel@tonic-gate call_res.gss_major != GSS_S_CONTINUE_NEEDED) 3390Sstevel@tonic-gate goto cleanup; 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate ap->gss_proc = RPCSEC_GSS_CONTINUE_INIT; 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate /* 3440Sstevel@tonic-gate * check for ctx_handle 3450Sstevel@tonic-gate */ 3460Sstevel@tonic-gate if (ap->ctx_handle.length == 0) { 3470Sstevel@tonic-gate if (call_res.ctx_handle.length == 0) 3480Sstevel@tonic-gate goto cleanup; 3490Sstevel@tonic-gate GSS_DUP_BUFFER(ap->ctx_handle, 3500Sstevel@tonic-gate call_res.ctx_handle); 3510Sstevel@tonic-gate } else if (!GSS_BUFFERS_EQUAL(ap->ctx_handle, 3520Sstevel@tonic-gate call_res.ctx_handle)) 3530Sstevel@tonic-gate goto cleanup; 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * check for token 3570Sstevel@tonic-gate */ 3580Sstevel@tonic-gate if (call_res.token.length != 0) { 3590Sstevel@tonic-gate if (*gssstat == GSS_S_COMPLETE) 3600Sstevel@tonic-gate goto cleanup; 3610Sstevel@tonic-gate GSS_DUP_BUFFER(input_token, call_res.token); 3620Sstevel@tonic-gate input_token_p = &input_token; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate } else if (*gssstat != GSS_S_COMPLETE) 3650Sstevel@tonic-gate goto cleanup; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate /* save the sequence window value; validate later */ 3680Sstevel@tonic-gate ap->seq_window = call_res.seq_window; 3690Sstevel@tonic-gate xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res); 3700Sstevel@tonic-gate free_results = FALSE; 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate /* 3740Sstevel@tonic-gate * results were okay.. continue if necessary 3750Sstevel@tonic-gate */ 3760Sstevel@tonic-gate if (*gssstat == GSS_S_CONTINUE_NEEDED) 3770Sstevel@tonic-gate goto next_token; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate /* 3800Sstevel@tonic-gate * Validate the sequence window - RFC 2203 section 5.2.3.1 3810Sstevel@tonic-gate */ 3820Sstevel@tonic-gate if (!validate_seqwin(ap)) { 3830Sstevel@tonic-gate goto cleanup; 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* 3870Sstevel@tonic-gate * Done! Security context creation is successful. 3880Sstevel@tonic-gate * Ready for exchanging data. 3890Sstevel@tonic-gate */ 3900Sstevel@tonic-gate ap->established = TRUE; 3910Sstevel@tonic-gate ap->seq_num = 1; 3920Sstevel@tonic-gate ap->gss_proc = RPCSEC_GSS_DATA; 3930Sstevel@tonic-gate ap->invalid = FALSE; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate clnt->cl_auth = save_auth; /* restore cl_auth */ 3960Sstevel@tonic-gate return (TRUE); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate cleanup: 3990Sstevel@tonic-gate if (ap->context != GSS_C_NO_CONTEXT) 4000Sstevel@tonic-gate rpc_gss_destroy_pvt(auth); 4010Sstevel@tonic-gate if (free_results) 4020Sstevel@tonic-gate xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res); 4030Sstevel@tonic-gate clnt->cl_auth = save_auth; /* restore cl_auth */ 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* 4060Sstevel@tonic-gate * if (rpc_createerr.cf_stat == 0) 4070Sstevel@tonic-gate * rpc_createerr.cf_stat = RPC_AUTHERROR; 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate if (rpc_createerr.cf_stat == 0) { 4100Sstevel@tonic-gate rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR; 4110Sstevel@tonic-gate rpc_gss_err.system_error = RPC_AUTHERROR; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate return (FALSE); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate /* 4180Sstevel@tonic-gate * Set service defaults. 4190Sstevel@tonic-gate */ 4200Sstevel@tonic-gate bool_t 4210Sstevel@tonic-gate __rpc_gss_set_defaults(auth, service, qop) 4220Sstevel@tonic-gate AUTH *auth; 4230Sstevel@tonic-gate rpc_gss_service_t service; 4240Sstevel@tonic-gate char *qop; 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate /*LINTED*/ 4270Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 4280Sstevel@tonic-gate char *mech; 4290Sstevel@tonic-gate OM_uint32 qop_num; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate switch (service) { 4320Sstevel@tonic-gate case rpc_gss_svc_integrity: 4330Sstevel@tonic-gate case rpc_gss_svc_privacy: 4340Sstevel@tonic-gate case rpc_gss_svc_none: 4350Sstevel@tonic-gate break; 4360Sstevel@tonic-gate case rpc_gss_svc_default: 4370Sstevel@tonic-gate service = rpc_gss_svc_integrity; 4380Sstevel@tonic-gate break; 4390Sstevel@tonic-gate default: 4400Sstevel@tonic-gate return (FALSE); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate if ((mech = __rpc_gss_oid_to_mech(ap->mech_type)) == NULL) 4440Sstevel@tonic-gate return (FALSE); 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate if (!__rpc_gss_qop_to_num(qop, mech, &qop_num)) 4470Sstevel@tonic-gate return (FALSE); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate ap->qop = qop_num; 4500Sstevel@tonic-gate ap->service = service; 4510Sstevel@tonic-gate return (TRUE); 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate /* 4550Sstevel@tonic-gate * Marshall credentials. 4560Sstevel@tonic-gate */ 4570Sstevel@tonic-gate static bool_t 4580Sstevel@tonic-gate marshall_creds(ap, xdrs) 4590Sstevel@tonic-gate rpc_gss_data *ap; 4600Sstevel@tonic-gate XDR *xdrs; 4610Sstevel@tonic-gate { 4620Sstevel@tonic-gate rpc_gss_creds ag_creds; 4630Sstevel@tonic-gate char cred_buf[MAX_AUTH_BYTES]; 4640Sstevel@tonic-gate struct opaque_auth creds; 4650Sstevel@tonic-gate XDR cred_xdrs; 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate ag_creds.version = ap->version; 4680Sstevel@tonic-gate ag_creds.gss_proc = ap->gss_proc; 4690Sstevel@tonic-gate ag_creds.seq_num = ap->seq_num; 4700Sstevel@tonic-gate ag_creds.service = ap->service; 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * If context has not been set up yet, use NULL handle. 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate if (ap->ctx_handle.length > 0) 4760Sstevel@tonic-gate ag_creds.ctx_handle = ap->ctx_handle; 4770Sstevel@tonic-gate else { 4780Sstevel@tonic-gate ag_creds.ctx_handle.length = 0; 4790Sstevel@tonic-gate ag_creds.ctx_handle.value = NULL; 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate xdrmem_create(&cred_xdrs, (caddr_t)cred_buf, MAX_AUTH_BYTES, 4830Sstevel@tonic-gate XDR_ENCODE); 4840Sstevel@tonic-gate if (!__xdr_rpc_gss_creds(&cred_xdrs, &ag_creds)) { 4850Sstevel@tonic-gate XDR_DESTROY(&cred_xdrs); 4860Sstevel@tonic-gate return (FALSE); 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate creds.oa_flavor = RPCSEC_GSS; 4900Sstevel@tonic-gate creds.oa_base = cred_buf; 4910Sstevel@tonic-gate creds.oa_length = xdr_getpos(&cred_xdrs); 4920Sstevel@tonic-gate XDR_DESTROY(&cred_xdrs); 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate if (!xdr_opaque_auth(xdrs, &creds)) 4950Sstevel@tonic-gate return (FALSE); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate return (TRUE); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * Marshall verifier. The verifier is the checksum of the RPC header 5020Sstevel@tonic-gate * up to and including the credential field. The XDR handle that's 5030Sstevel@tonic-gate * passed in has the header up to and including the credential field 5040Sstevel@tonic-gate * encoded. A pointer to the transmit buffer is also passed in. 5050Sstevel@tonic-gate */ 5060Sstevel@tonic-gate static bool_t 5070Sstevel@tonic-gate marshall_verf(ap, xdrs, buf) 5080Sstevel@tonic-gate rpc_gss_data *ap; 5090Sstevel@tonic-gate XDR *xdrs; /* send XDR */ 5100Sstevel@tonic-gate char *buf; /* pointer of send buffer */ 5110Sstevel@tonic-gate { 5120Sstevel@tonic-gate struct opaque_auth verf; 5130Sstevel@tonic-gate OM_uint32 major, minor; 5140Sstevel@tonic-gate gss_buffer_desc in_buf, out_buf; 5150Sstevel@tonic-gate bool_t ret = FALSE; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate /* 5180Sstevel@tonic-gate * If context is not established yet, use NULL verifier. 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate if (!ap->established) { 5210Sstevel@tonic-gate verf.oa_flavor = AUTH_NONE; 5220Sstevel@tonic-gate verf.oa_base = NULL; 5230Sstevel@tonic-gate verf.oa_length = 0; 5240Sstevel@tonic-gate return (xdr_opaque_auth(xdrs, &verf)); 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate verf.oa_flavor = RPCSEC_GSS; 5280Sstevel@tonic-gate in_buf.length = xdr_getpos(xdrs); 5290Sstevel@tonic-gate in_buf.value = buf; 5300Sstevel@tonic-gate if ((major = gss_sign(&minor, ap->context, ap->qop, &in_buf, 5310Sstevel@tonic-gate &out_buf)) != GSS_S_COMPLETE) { 5320Sstevel@tonic-gate if (major == GSS_S_CONTEXT_EXPIRED) { 5330Sstevel@tonic-gate ap->invalid = TRUE; 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate return (FALSE); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate verf.oa_base = out_buf.value; 5380Sstevel@tonic-gate verf.oa_length = out_buf.length; 5390Sstevel@tonic-gate ret = xdr_opaque_auth(xdrs, &verf); 5400Sstevel@tonic-gate (void) gss_release_buffer(&minor, &out_buf); 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate return (ret); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate /* 5460Sstevel@tonic-gate * Function: rpc_gss_nextverf. Not used. 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate static void 5490Sstevel@tonic-gate rpc_gss_nextverf() 5500Sstevel@tonic-gate { 5510Sstevel@tonic-gate } 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * Function: rpc_gss_marshall - not used. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate static bool_t 5570Sstevel@tonic-gate rpc_gss_marshall(auth, xdrs) 5580Sstevel@tonic-gate AUTH *auth; 5590Sstevel@tonic-gate XDR *xdrs; 5600Sstevel@tonic-gate { 5610Sstevel@tonic-gate if (!xdr_opaque_auth(xdrs, &auth->ah_cred) || 5620Sstevel@tonic-gate !xdr_opaque_auth(xdrs, &auth->ah_verf)) 5630Sstevel@tonic-gate return (FALSE); 5640Sstevel@tonic-gate return (TRUE); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * Validate sequence window upon a successful RPCSEC_GSS INIT session. 5690Sstevel@tonic-gate * The sequence window sent back by the server should be verifiable by 5700Sstevel@tonic-gate * the verifier which is a checksum of the sequence window. 5710Sstevel@tonic-gate */ 5720Sstevel@tonic-gate static bool_t 5730Sstevel@tonic-gate validate_seqwin(rpc_gss_data *ap) 5740Sstevel@tonic-gate { 5750Sstevel@tonic-gate uint_t seq_win_net; 5760Sstevel@tonic-gate OM_uint32 major = 0, minor = 0; 5770Sstevel@tonic-gate gss_buffer_desc msg_buf, tok_buf; 5780Sstevel@tonic-gate int qop_state = 0; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate seq_win_net = (uint_t)htonl(ap->seq_window); 5810Sstevel@tonic-gate msg_buf.length = sizeof (seq_win_net); 5820Sstevel@tonic-gate msg_buf.value = (char *)&seq_win_net; 5830Sstevel@tonic-gate tok_buf.length = ap->verifier->oa_length; 5840Sstevel@tonic-gate tok_buf.value = ap->verifier->oa_base; 5850Sstevel@tonic-gate major = gss_verify(&minor, ap->context, &msg_buf, &tok_buf, &qop_state); 5860Sstevel@tonic-gate if (major != GSS_S_COMPLETE) 5870Sstevel@tonic-gate return (FALSE); 5880Sstevel@tonic-gate return (TRUE); 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate /* 5920Sstevel@tonic-gate * Validate RPC response verifier from server. The response verifier 5930Sstevel@tonic-gate * is the checksum of the request sequence number. 5940Sstevel@tonic-gate */ 5950Sstevel@tonic-gate static bool_t 5960Sstevel@tonic-gate rpc_gss_validate(auth, verf) 5970Sstevel@tonic-gate AUTH *auth; 5980Sstevel@tonic-gate struct opaque_auth *verf; 5990Sstevel@tonic-gate { 6000Sstevel@tonic-gate /*LINTED*/ 6010Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 6020Sstevel@tonic-gate uint_t seq_num_net; 6030Sstevel@tonic-gate OM_uint32 major, minor; 6040Sstevel@tonic-gate gss_buffer_desc msg_buf, tok_buf; 6050Sstevel@tonic-gate int qop_state; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate /* 6080Sstevel@tonic-gate * If context is not established yet, save the verifier for 6090Sstevel@tonic-gate * validating the sequence window later at the end of context 6100Sstevel@tonic-gate * creation session. 6110Sstevel@tonic-gate */ 6120Sstevel@tonic-gate if (!ap->established) { 6130Sstevel@tonic-gate if (ap->verifier == NULL) { 6140Sstevel@tonic-gate ap->verifier = malloc(sizeof (struct opaque_auth)); 6150Sstevel@tonic-gate memset(ap->verifier, 0, sizeof (struct opaque_auth)); 6160Sstevel@tonic-gate if (verf->oa_length > 0) 6170Sstevel@tonic-gate ap->verifier->oa_base = malloc(verf->oa_length); 6180Sstevel@tonic-gate } else { 6190Sstevel@tonic-gate if (ap->verifier->oa_length > 0) 6200Sstevel@tonic-gate free(ap->verifier->oa_base); 6210Sstevel@tonic-gate if (verf->oa_length > 0) 6220Sstevel@tonic-gate ap->verifier->oa_base = malloc(verf->oa_length); 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate ap->verifier->oa_length = verf->oa_length; 6250Sstevel@tonic-gate bcopy(verf->oa_base, ap->verifier->oa_base, verf->oa_length); 6260Sstevel@tonic-gate return (TRUE); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate seq_num_net = (uint_t)htonl(ap->seq_num); 6300Sstevel@tonic-gate msg_buf.length = sizeof (seq_num_net); 6310Sstevel@tonic-gate msg_buf.value = (char *)&seq_num_net; 6320Sstevel@tonic-gate tok_buf.length = verf->oa_length; 6330Sstevel@tonic-gate tok_buf.value = verf->oa_base; 6340Sstevel@tonic-gate major = gss_verify(&minor, ap->context, &msg_buf, &tok_buf, &qop_state); 6350Sstevel@tonic-gate if (major != GSS_S_COMPLETE) 6360Sstevel@tonic-gate return (FALSE); 6370Sstevel@tonic-gate return (TRUE); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * Refresh client context. This is necessary sometimes because the 6420Sstevel@tonic-gate * server will ocassionally destroy contexts based on LRU method, or 6430Sstevel@tonic-gate * because of expired credentials. 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate static bool_t 6460Sstevel@tonic-gate rpc_gss_refresh(auth, msg) 6470Sstevel@tonic-gate AUTH *auth; 6480Sstevel@tonic-gate struct rpc_msg *msg; 6490Sstevel@tonic-gate { 6500Sstevel@tonic-gate /*LINTED*/ 6510Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 6520Sstevel@tonic-gate OM_uint32 gssstat, minor_stat; 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate /* 6550Sstevel@tonic-gate * The context needs to be recreated only when the error status 6560Sstevel@tonic-gate * returned from the server is one of the following: 6570Sstevel@tonic-gate * RPCSEC_GSS_NOCRED and RPCSEC_GSS_FAILED 6580Sstevel@tonic-gate * The existing context should not be destroyed unless the above 6590Sstevel@tonic-gate * error status codes are received or if the context has not 6600Sstevel@tonic-gate * been set up. 6610Sstevel@tonic-gate */ 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate if (msg->rjcted_rply.rj_why == RPCSEC_GSS_NOCRED || 6640Sstevel@tonic-gate msg->rjcted_rply.rj_why == RPCSEC_GSS_FAILED || 6650Sstevel@tonic-gate !ap->established) { 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * Destroy the context if necessary. Use the same memory 6680Sstevel@tonic-gate * for the new context since we've already passed a pointer 6690Sstevel@tonic-gate * to it to the user. 6700Sstevel@tonic-gate */ 6710Sstevel@tonic-gate if (ap->context != GSS_C_NO_CONTEXT) { 6720Sstevel@tonic-gate (void) gss_delete_sec_context(&minor_stat, &ap->context, 6730Sstevel@tonic-gate NULL); 6740Sstevel@tonic-gate ap->context = GSS_C_NO_CONTEXT; 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate if (ap->ctx_handle.length != 0) { 6770Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat, 6780Sstevel@tonic-gate &ap->ctx_handle); 6790Sstevel@tonic-gate ap->ctx_handle.length = 0; 6800Sstevel@tonic-gate ap->ctx_handle.value = NULL; 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * If the context was not already established, don't try to 6850Sstevel@tonic-gate * recreate it. 6860Sstevel@tonic-gate */ 6870Sstevel@tonic-gate if (!ap->established) { 6880Sstevel@tonic-gate ap->invalid = TRUE; 6890Sstevel@tonic-gate return (FALSE); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate /* 6930Sstevel@tonic-gate * Recreate context. 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate if (rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth, ap, 6960Sstevel@tonic-gate (gss_OID *)0, (OM_uint32 *)0, (OM_uint32 *)0)) 6970Sstevel@tonic-gate return (TRUE); 6980Sstevel@tonic-gate else { 6990Sstevel@tonic-gate ap->invalid = TRUE; 7000Sstevel@tonic-gate return (FALSE); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate return (FALSE); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate /* 7070Sstevel@tonic-gate * Destroy a context. 7080Sstevel@tonic-gate */ 7090Sstevel@tonic-gate static void 7100Sstevel@tonic-gate rpc_gss_destroy(auth) 7110Sstevel@tonic-gate AUTH *auth; 7120Sstevel@tonic-gate { 7130Sstevel@tonic-gate /*LINTED*/ 7140Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate rpc_gss_destroy_pvt(auth); 7170Sstevel@tonic-gate free((char *)ap); 7180Sstevel@tonic-gate free(auth); 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* 7220Sstevel@tonic-gate * Private interface to destroy a context without freeing up 7230Sstevel@tonic-gate * the memory used by it. We need to do this when a refresh 7240Sstevel@tonic-gate * fails, for example, so the user will still have a handle. 7250Sstevel@tonic-gate */ 7260Sstevel@tonic-gate static void 7270Sstevel@tonic-gate rpc_gss_destroy_pvt(auth) 7280Sstevel@tonic-gate AUTH *auth; 7290Sstevel@tonic-gate { 7300Sstevel@tonic-gate struct timeval timeout; 7310Sstevel@tonic-gate OM_uint32 minor_stat; 7320Sstevel@tonic-gate /*LINTED*/ 7330Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * If we have a server context id, inform server that we are 7370Sstevel@tonic-gate * destroying the context. 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate if (ap->ctx_handle.length != 0) { 7400Sstevel@tonic-gate ap->gss_proc = RPCSEC_GSS_DESTROY; 7410Sstevel@tonic-gate timeout.tv_sec = 1; 7420Sstevel@tonic-gate timeout.tv_usec = 0; 7430Sstevel@tonic-gate (void) clnt_call(ap->clnt, NULLPROC, xdr_void, NULL, 7440Sstevel@tonic-gate xdr_void, NULL, timeout); 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate (void) gss_release_buffer(&minor_stat, &ap->ctx_handle); 7470Sstevel@tonic-gate ap->ctx_handle.length = 0; 7480Sstevel@tonic-gate ap->ctx_handle.value = NULL; 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate /* 7520Sstevel@tonic-gate * Destroy local GSS context. 7530Sstevel@tonic-gate */ 7540Sstevel@tonic-gate if (ap->context != GSS_C_NO_CONTEXT) { 7550Sstevel@tonic-gate (void) gss_delete_sec_context(&minor_stat, &ap->context, NULL); 7560Sstevel@tonic-gate ap->context = GSS_C_NO_CONTEXT; 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /* 7600Sstevel@tonic-gate * Looks like we need to release default credentials if we use it. 7610Sstevel@tonic-gate * Non-default creds need to be released by user. 7620Sstevel@tonic-gate */ 7630Sstevel@tonic-gate if (ap->my_cred == GSS_C_NO_CREDENTIAL) 7640Sstevel@tonic-gate (void) gss_release_cred(&minor_stat, &ap->my_cred); 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate /* 7670Sstevel@tonic-gate * Release any internal name structures. 7680Sstevel@tonic-gate */ 7690Sstevel@tonic-gate if (ap->target_name != NULL) { 7700Sstevel@tonic-gate (void) gss_release_name(&minor_stat, &ap->target_name); 7710Sstevel@tonic-gate ap->target_name = NULL; 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* 7750Sstevel@tonic-gate * Free the verifier saved for sequence window checking. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate if (ap->verifier != NULL) { 7780Sstevel@tonic-gate if (ap->verifier->oa_length > 0) 7790Sstevel@tonic-gate free(ap->verifier->oa_base); 7800Sstevel@tonic-gate free(ap->verifier); 7810Sstevel@tonic-gate ap->verifier = NULL; 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* 7860Sstevel@tonic-gate * Wrap client side data. The encoded header is passed in through 7870Sstevel@tonic-gate * buf and buflen. The header is up to but not including the 7880Sstevel@tonic-gate * credential field. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate bool_t 7910Sstevel@tonic-gate __rpc_gss_wrap(auth, buf, buflen, out_xdrs, xdr_func, xdr_ptr) 7920Sstevel@tonic-gate AUTH *auth; 7930Sstevel@tonic-gate char *buf; /* encoded header */ 7940Sstevel@tonic-gate uint_t buflen; /* encoded header length */ 7950Sstevel@tonic-gate XDR *out_xdrs; 7960Sstevel@tonic-gate bool_t (*xdr_func)(); 7970Sstevel@tonic-gate caddr_t xdr_ptr; 7980Sstevel@tonic-gate { 7990Sstevel@tonic-gate /*LINTED*/ 8000Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 8010Sstevel@tonic-gate XDR xdrs; 8020Sstevel@tonic-gate char tmp_buf[512]; 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate /* 8060Sstevel@tonic-gate * Reject an invalid context. 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate if (ap->invalid) 8090Sstevel@tonic-gate return (FALSE); 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate /* 8120Sstevel@tonic-gate * If context is established, bump up sequence number. 8130Sstevel@tonic-gate */ 8140Sstevel@tonic-gate if (ap->established) 8150Sstevel@tonic-gate ap->seq_num++; 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate /* 8180Sstevel@tonic-gate * Create the header in a temporary XDR context and buffer 8190Sstevel@tonic-gate * before putting it out. 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate xdrmem_create(&xdrs, tmp_buf, sizeof (tmp_buf), XDR_ENCODE); 8220Sstevel@tonic-gate if (!XDR_PUTBYTES(&xdrs, buf, buflen)) 8230Sstevel@tonic-gate return (FALSE); 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate /* 8260Sstevel@tonic-gate * create cred field 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate if (!marshall_creds(ap, &xdrs)) 8290Sstevel@tonic-gate return (FALSE); 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate /* 8320Sstevel@tonic-gate * create verifier 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate if (!marshall_verf(ap, &xdrs, tmp_buf)) 8350Sstevel@tonic-gate return (FALSE); 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate /* 8380Sstevel@tonic-gate * write out header and destroy temp structures 8390Sstevel@tonic-gate */ 8400Sstevel@tonic-gate if (!XDR_PUTBYTES(out_xdrs, tmp_buf, XDR_GETPOS(&xdrs))) 8410Sstevel@tonic-gate return (FALSE); 8420Sstevel@tonic-gate XDR_DESTROY(&xdrs); 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* 8450Sstevel@tonic-gate * If context is not established, or if neither integrity 8460Sstevel@tonic-gate * nor privacy is used, just XDR encode data. 8470Sstevel@tonic-gate */ 8480Sstevel@tonic-gate if (!ap->established || ap->service == rpc_gss_svc_none) 8490Sstevel@tonic-gate return ((*xdr_func)(out_xdrs, xdr_ptr)); 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate return (__rpc_gss_wrap_data(ap->service, ap->qop, ap->context, 8520Sstevel@tonic-gate ap->seq_num, out_xdrs, xdr_func, xdr_ptr)); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate /* 8560Sstevel@tonic-gate * Unwrap received data. 8570Sstevel@tonic-gate */ 8580Sstevel@tonic-gate bool_t 8590Sstevel@tonic-gate __rpc_gss_unwrap(auth, in_xdrs, xdr_func, xdr_ptr) 8600Sstevel@tonic-gate AUTH *auth; 8610Sstevel@tonic-gate XDR *in_xdrs; 8620Sstevel@tonic-gate bool_t (*xdr_func)(); 8630Sstevel@tonic-gate caddr_t xdr_ptr; 8640Sstevel@tonic-gate { 8650Sstevel@tonic-gate /*LINTED*/ 8660Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /* 8690Sstevel@tonic-gate * If context is not established, of if neither integrity 8700Sstevel@tonic-gate * nor privacy is used, just XDR encode data. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate if (!ap->established || ap->service == rpc_gss_svc_none) 8730Sstevel@tonic-gate return ((*xdr_func)(in_xdrs, xdr_ptr)); 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate return (__rpc_gss_unwrap_data(ap->service, 8760Sstevel@tonic-gate ap->context, 8770Sstevel@tonic-gate ap->seq_num, 8780Sstevel@tonic-gate ap->qop, 8790Sstevel@tonic-gate in_xdrs, xdr_func, xdr_ptr)); 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate int 8830Sstevel@tonic-gate __rpc_gss_max_data_length(auth, max_tp_unit_len) 8840Sstevel@tonic-gate AUTH *auth; 8850Sstevel@tonic-gate int max_tp_unit_len; 8860Sstevel@tonic-gate { 8870Sstevel@tonic-gate /*LINTED*/ 8880Sstevel@tonic-gate rpc_gss_data *ap = AUTH_PRIVATE(auth); 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate if (!ap->established || max_tp_unit_len <= 0) 8910Sstevel@tonic-gate return (0); 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate return (__find_max_data_length(ap->service, 8940Sstevel@tonic-gate ap->context, 8950Sstevel@tonic-gate ap->qop, 8960Sstevel@tonic-gate max_tp_unit_len)); 8970Sstevel@tonic-gate } 8980Sstevel@tonic-gate 899*3864Sraf void 900*3864Sraf __rpc_gss_get_error(rpc_gss_error_t *error) 901*3864Sraf { 902*3864Sraf *error = rpc_gss_err; 903*3864Sraf } 904*3864Sraf 9050Sstevel@tonic-gate #undef rpc_gss_err 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate rpc_gss_error_t rpc_gss_err; 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate rpc_gss_error_t * 9100Sstevel@tonic-gate __rpc_gss_err() 9110Sstevel@tonic-gate { 912*3864Sraf static thread_key_t rpc_gss_err_key = THR_ONCE_KEY; 913*3864Sraf rpc_gss_error_t *tsd; 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (_thr_main()) 9160Sstevel@tonic-gate return (&rpc_gss_err); 917*3864Sraf if (_thr_keycreate_once(&rpc_gss_err_key, free) != 0) 918*3864Sraf return (&rpc_gss_err); 919*3864Sraf tsd = _pthread_getspecific(rpc_gss_err_key); 920*3864Sraf if (tsd == NULL) { 921*3864Sraf tsd = (rpc_gss_error_t *)calloc(1, sizeof (rpc_gss_error_t)); 922*3864Sraf if (_thr_setspecific(rpc_gss_err_key, tsd) != 0) { 9230Sstevel@tonic-gate if (tsd) 9240Sstevel@tonic-gate free(tsd); 9250Sstevel@tonic-gate return (&rpc_gss_err); 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate return (tsd); 9290Sstevel@tonic-gate } 930