Path: blob/main/sys/rpc/rpcsec_gss/svc_rpcsec_gss.c
39564 views
/*-1* SPDX-License-Identifier: BSD-3-Clause2* Copyright (c) 1990 The Regents of the University of California.3*4* Copyright (c) 2008 Doug Rabson5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/28/*29svc_rpcsec_gss.c3031Copyright (c) 2000 The Regents of the University of Michigan.32All rights reserved.3334Copyright (c) 2000 Dug Song <[email protected]>.35All rights reserved, all wrongs reversed.3637Redistribution and use in source and binary forms, with or without38modification, are permitted provided that the following conditions39are met:40411. Redistributions of source code must retain the above copyright42notice, this list of conditions and the following disclaimer.432. Redistributions in binary form must reproduce the above copyright44notice, this list of conditions and the following disclaimer in the45documentation and/or other materials provided with the distribution.463. Neither the name of the University nor the names of its47contributors may be used to endorse or promote products derived48from this software without specific prior written permission.4950THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED51WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF52MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE53DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE54FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR55CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF56SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR57BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF58LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING59NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS60SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.6162$Id: svc_auth_gss.c,v 1.27 2002/01/15 15:43:00 andros Exp $63*/6465#include <sys/param.h>66#include <sys/systm.h>67#include <sys/jail.h>68#include <sys/kernel.h>69#include <sys/kobj.h>70#include <sys/lock.h>71#include <sys/malloc.h>72#include <sys/mbuf.h>73#include <sys/mutex.h>74#include <sys/proc.h>75#include <sys/sx.h>76#include <sys/ucred.h>7778#include <rpc/rpc.h>79#include <rpc/rpcsec_gss.h>8081#include "rpcsec_gss_int.h"8283static bool_t svc_rpc_gss_wrap(SVCAUTH *, struct mbuf **);84static bool_t svc_rpc_gss_unwrap(SVCAUTH *, struct mbuf **);85static void svc_rpc_gss_release(SVCAUTH *);86static enum auth_stat svc_rpc_gss(struct svc_req *, struct rpc_msg *);87static int rpc_gss_svc_getcred(struct svc_req *, struct ucred **, int *);8889static const struct svc_auth_ops svc_auth_gss_ops = {90.svc_ah_wrap = svc_rpc_gss_wrap,91.svc_ah_unwrap = svc_rpc_gss_unwrap,92.svc_ah_release = svc_rpc_gss_release,93};9495struct sx svc_rpc_gss_lock;9697struct svc_rpc_gss_callback {98SLIST_ENTRY(svc_rpc_gss_callback) cb_link;99rpc_gss_callback_t cb_callback;100};101SLIST_HEAD(svc_rpc_gss_callback_list, svc_rpc_gss_callback);102KGSS_VNET_DEFINE_STATIC(struct svc_rpc_gss_callback_list,103svc_rpc_gss_callbacks) = SLIST_HEAD_INITIALIZER(svc_rpc_gss_callbacks);104105struct svc_rpc_gss_svc_name {106SLIST_ENTRY(svc_rpc_gss_svc_name) sn_link;107char *sn_principal;108gss_OID sn_mech;109u_int sn_req_time;110gss_cred_id_t sn_cred;111u_int sn_program;112u_int sn_version;113};114SLIST_HEAD(svc_rpc_gss_svc_name_list, svc_rpc_gss_svc_name);115KGSS_VNET_DEFINE_STATIC(struct svc_rpc_gss_svc_name_list,116svc_rpc_gss_svc_names) = SLIST_HEAD_INITIALIZER(svc_rpc_gss_svc_names);117118enum svc_rpc_gss_client_state {119CLIENT_NEW, /* still authenticating */120CLIENT_ESTABLISHED, /* context established */121CLIENT_STALE /* garbage to collect */122};123124#define SVC_RPC_GSS_SEQWINDOW 128125126struct svc_rpc_gss_clientid {127unsigned long ci_hostid;128uint32_t ci_boottime;129uint32_t ci_id;130};131132struct svc_rpc_gss_client {133TAILQ_ENTRY(svc_rpc_gss_client) cl_link;134TAILQ_ENTRY(svc_rpc_gss_client) cl_alllink;135volatile u_int cl_refs;136struct sx cl_lock;137struct svc_rpc_gss_clientid cl_id;138time_t cl_expiration; /* when to gc */139enum svc_rpc_gss_client_state cl_state; /* client state */140bool_t cl_locked; /* fixed service+qop */141gss_ctx_id_t cl_ctx; /* context id */142gss_cred_id_t cl_creds; /* delegated creds */143gss_name_t cl_cname; /* client name */144struct svc_rpc_gss_svc_name *cl_sname; /* server name used */145rpc_gss_rawcred_t cl_rawcred; /* raw credentials */146rpc_gss_ucred_t cl_ucred; /* unix-style credentials */147struct ucred *cl_cred; /* kernel-style credentials */148int cl_rpcflavor; /* RPC pseudo sec flavor */149bool_t cl_done_callback; /* TRUE after call */150void *cl_cookie; /* user cookie from callback */151gid_t cl_gid_storage[NGROUPS];152gss_OID cl_mech; /* mechanism */153gss_qop_t cl_qop; /* quality of protection */154uint32_t cl_seqlast; /* sequence window origin */155uint32_t cl_seqmask[SVC_RPC_GSS_SEQWINDOW/32]; /* bitmask of seqnums */156};157TAILQ_HEAD(svc_rpc_gss_client_list, svc_rpc_gss_client);158159/*160* This structure holds enough information to unwrap arguments or wrap161* results for a given request. We use the rq_clntcred area for this162* (which is a per-request buffer).163*/164struct svc_rpc_gss_cookedcred {165struct svc_rpc_gss_client *cc_client;166rpc_gss_service_t cc_service;167uint32_t cc_seq;168};169170#define CLIENT_HASH_SIZE 256171#define CLIENT_MAX 1024172u_int svc_rpc_gss_client_max = CLIENT_MAX;173u_int svc_rpc_gss_client_hash_size = CLIENT_HASH_SIZE;174175SYSCTL_DECL(_kern_rpc);176SYSCTL_NODE(_kern_rpc, OID_AUTO, gss, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,177"GSS");178179SYSCTL_UINT(_kern_rpc_gss, OID_AUTO, client_max, CTLFLAG_RW,180&svc_rpc_gss_client_max, 0,181"Max number of rpc-gss clients");182183SYSCTL_UINT(_kern_rpc_gss, OID_AUTO, client_hash, CTLFLAG_RDTUN,184&svc_rpc_gss_client_hash_size, 0,185"Size of rpc-gss client hash table");186187static u_int svc_rpc_gss_lifetime_max = 0;188SYSCTL_UINT(_kern_rpc_gss, OID_AUTO, lifetime_max, CTLFLAG_RW,189&svc_rpc_gss_lifetime_max, 0,190"Maximum lifetime (seconds) of rpc-gss clients");191192static u_int svc_rpc_gss_client_count;193SYSCTL_UINT(_kern_rpc_gss, OID_AUTO, client_count, CTLFLAG_RD,194&svc_rpc_gss_client_count, 0,195"Number of rpc-gss clients");196197KGSS_VNET_DEFINE(struct svc_rpc_gss_client_list *, svc_rpc_gss_client_hash);198KGSS_VNET_DEFINE(struct svc_rpc_gss_client_list, svc_rpc_gss_clients);199KGSS_VNET_DEFINE_STATIC(uint32_t, svc_rpc_gss_next_clientid) = 1;200201static void202svc_rpc_gss_init(void *unused __unused)203{204205svc_auth_reg(RPCSEC_GSS, svc_rpc_gss, rpc_gss_svc_getcred);206sx_init(&svc_rpc_gss_lock, "gsslock");207}208SYSINIT(svc_rpc_gss_init, SI_SUB_VFS, SI_ORDER_ANY,209svc_rpc_gss_init, NULL);210211static void212svc_rpc_gss_cleanup(void *unused __unused)213{214215sx_destroy(&svc_rpc_gss_lock);216}217SYSUNINIT(svc_rpc_gss_cleanup, SI_SUB_VFS, SI_ORDER_ANY,218svc_rpc_gss_cleanup, NULL);219220static void221svc_rpc_gss_vnetinit(void *unused __unused)222{223int i;224225KGSS_VNET(svc_rpc_gss_client_hash) = mem_alloc(226sizeof(struct svc_rpc_gss_client_list) *227svc_rpc_gss_client_hash_size);228for (i = 0; i < svc_rpc_gss_client_hash_size; i++)229TAILQ_INIT(&KGSS_VNET(svc_rpc_gss_client_hash)[i]);230TAILQ_INIT(&KGSS_VNET(svc_rpc_gss_clients));231}232VNET_SYSINIT(svc_rpc_gss_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_ANY,233svc_rpc_gss_vnetinit, NULL);234235static void236svc_rpc_gss_vnet_cleanup(void *unused __unused)237{238239mem_free(KGSS_VNET(svc_rpc_gss_client_hash),240sizeof(struct svc_rpc_gss_client_list) *241svc_rpc_gss_client_hash_size);242}243VNET_SYSUNINIT(svc_rpc_gss_vnet_cleanup, SI_SUB_VNET_DONE, SI_ORDER_ANY,244svc_rpc_gss_vnet_cleanup, NULL);245246bool_t247rpc_gss_set_callback(rpc_gss_callback_t *cb)248{249struct svc_rpc_gss_callback *scb;250251scb = mem_alloc(sizeof(struct svc_rpc_gss_callback));252if (!scb) {253_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOMEM);254return (FALSE);255}256scb->cb_callback = *cb;257sx_xlock(&svc_rpc_gss_lock);258SLIST_INSERT_HEAD(&KGSS_VNET(svc_rpc_gss_callbacks), scb, cb_link);259sx_xunlock(&svc_rpc_gss_lock);260261return (TRUE);262}263264void265rpc_gss_clear_callback(rpc_gss_callback_t *cb)266{267struct svc_rpc_gss_callback *scb;268269sx_xlock(&svc_rpc_gss_lock);270SLIST_FOREACH(scb, &KGSS_VNET(svc_rpc_gss_callbacks), cb_link) {271if (scb->cb_callback.program == cb->program272&& scb->cb_callback.version == cb->version273&& scb->cb_callback.callback == cb->callback) {274SLIST_REMOVE(&KGSS_VNET(svc_rpc_gss_callbacks), scb,275svc_rpc_gss_callback, cb_link);276sx_xunlock(&svc_rpc_gss_lock);277mem_free(scb, sizeof(*scb));278return;279}280}281sx_xunlock(&svc_rpc_gss_lock);282}283284static bool_t285rpc_gss_acquire_svc_cred(struct svc_rpc_gss_svc_name *sname)286{287OM_uint32 maj_stat, min_stat;288gss_buffer_desc namebuf;289gss_name_t name;290gss_OID_set_desc oid_set;291292oid_set.count = 1;293oid_set.elements = sname->sn_mech;294295namebuf.value = (void *) sname->sn_principal;296namebuf.length = strlen(sname->sn_principal);297298maj_stat = gss_import_name(&min_stat, &namebuf,299GSS_C_NT_HOSTBASED_SERVICE, &name);300if (maj_stat != GSS_S_COMPLETE)301return (FALSE);302303if (sname->sn_cred != GSS_C_NO_CREDENTIAL)304gss_release_cred(&min_stat, &sname->sn_cred);305306maj_stat = gss_acquire_cred(&min_stat, name,307sname->sn_req_time, &oid_set, GSS_C_ACCEPT, &sname->sn_cred,308NULL, NULL);309if (maj_stat != GSS_S_COMPLETE) {310gss_release_name(&min_stat, &name);311return (FALSE);312}313gss_release_name(&min_stat, &name);314315return (TRUE);316}317318bool_t319rpc_gss_set_svc_name(const char *principal, const char *mechanism,320u_int req_time, u_int program, u_int version)321{322struct svc_rpc_gss_svc_name *sname;323gss_OID mech_oid;324325if (!rpc_gss_mech_to_oid(mechanism, &mech_oid))326return (FALSE);327328sname = mem_alloc(sizeof(*sname));329if (!sname)330return (FALSE);331sname->sn_principal = strdup(principal, M_RPC);332sname->sn_mech = mech_oid;333sname->sn_req_time = req_time;334sname->sn_cred = GSS_C_NO_CREDENTIAL;335sname->sn_program = program;336sname->sn_version = version;337338if (!rpc_gss_acquire_svc_cred(sname)) {339free(sname->sn_principal, M_RPC);340mem_free(sname, sizeof(*sname));341return (FALSE);342}343344sx_xlock(&svc_rpc_gss_lock);345SLIST_INSERT_HEAD(&KGSS_VNET(svc_rpc_gss_svc_names), sname, sn_link);346sx_xunlock(&svc_rpc_gss_lock);347348return (TRUE);349}350351void352rpc_gss_clear_svc_name(u_int program, u_int version)353{354OM_uint32 min_stat;355struct svc_rpc_gss_svc_name *sname;356357sx_xlock(&svc_rpc_gss_lock);358SLIST_FOREACH(sname, &KGSS_VNET(svc_rpc_gss_svc_names), sn_link) {359if (sname->sn_program == program360&& sname->sn_version == version) {361SLIST_REMOVE(&KGSS_VNET(svc_rpc_gss_svc_names), sname,362svc_rpc_gss_svc_name, sn_link);363sx_xunlock(&svc_rpc_gss_lock);364gss_release_cred(&min_stat, &sname->sn_cred);365free(sname->sn_principal, M_RPC);366mem_free(sname, sizeof(*sname));367return;368}369}370sx_xunlock(&svc_rpc_gss_lock);371}372373bool_t374rpc_gss_get_principal_name(rpc_gss_principal_t *principal,375const char *mech, const char *name, const char *node, const char *domain)376{377OM_uint32 maj_stat, min_stat;378gss_OID mech_oid;379size_t namelen;380gss_buffer_desc buf;381gss_name_t gss_name, gss_mech_name;382rpc_gss_principal_t result;383384if (!rpc_gss_mech_to_oid(mech, &mech_oid))385return (FALSE);386387/*388* Construct a gss_buffer containing the full name formatted389* as "name/node@domain" where node and domain are optional.390*/391namelen = strlen(name) + 1;392if (node) {393namelen += strlen(node) + 1;394}395if (domain) {396namelen += strlen(domain) + 1;397}398399buf.value = mem_alloc(namelen);400buf.length = namelen;401strcpy((char *) buf.value, name);402if (node) {403strcat((char *) buf.value, "/");404strcat((char *) buf.value, node);405}406if (domain) {407strcat((char *) buf.value, "@");408strcat((char *) buf.value, domain);409}410411/*412* Convert that to a gss_name_t and then convert that to a413* mechanism name in the selected mechanism.414*/415maj_stat = gss_import_name(&min_stat, &buf,416GSS_C_NT_USER_NAME, &gss_name);417mem_free(buf.value, buf.length);418if (maj_stat != GSS_S_COMPLETE) {419rpc_gss_log_status("gss_import_name", mech_oid, maj_stat, min_stat);420return (FALSE);421}422maj_stat = gss_canonicalize_name(&min_stat, gss_name, mech_oid,423&gss_mech_name);424if (maj_stat != GSS_S_COMPLETE) {425rpc_gss_log_status("gss_canonicalize_name", mech_oid, maj_stat,426min_stat);427gss_release_name(&min_stat, &gss_name);428return (FALSE);429}430gss_release_name(&min_stat, &gss_name);431432/*433* Export the mechanism name and use that to construct the434* rpc_gss_principal_t result.435*/436maj_stat = gss_export_name(&min_stat, gss_mech_name, &buf);437if (maj_stat != GSS_S_COMPLETE) {438rpc_gss_log_status("gss_export_name", mech_oid, maj_stat, min_stat);439gss_release_name(&min_stat, &gss_mech_name);440return (FALSE);441}442gss_release_name(&min_stat, &gss_mech_name);443444result = mem_alloc(sizeof(int) + buf.length);445if (!result) {446gss_release_buffer(&min_stat, &buf);447return (FALSE);448}449result->len = buf.length;450memcpy(result->name, buf.value, buf.length);451gss_release_buffer(&min_stat, &buf);452453*principal = result;454return (TRUE);455}456457/*458* Note that the ip_addr and srv_principal pointers can point to the same459* buffer, so long as ip_addr is at least strlen(srv_name) + 1 > srv_principal.460*/461bool_t462rpc_gss_ip_to_srv_principal(char *ip_addr, const char *srv_name,463char *srv_principal)464{465OM_uint32 maj_stat, min_stat;466size_t len;467468/*469* First fill in the service name and '@'.470*/471len = strlen(srv_name);472if (len > NI_MAXSERV)473return (FALSE);474memcpy(srv_principal, srv_name, len);475srv_principal[len] = '@';476477/*478* Do reverse DNS to get the DNS name for the ip_addr.479*/480maj_stat = gss_ip_to_dns(&min_stat, ip_addr, &srv_principal[len + 1]);481if (maj_stat != GSS_S_COMPLETE) {482rpc_gss_log_status("gss_ip_to_dns", NULL, maj_stat, min_stat);483return (FALSE);484}485return (TRUE);486}487488bool_t489rpc_gss_getcred(struct svc_req *req, rpc_gss_rawcred_t **rcred,490rpc_gss_ucred_t **ucred, void **cookie)491{492struct svc_rpc_gss_cookedcred *cc;493struct svc_rpc_gss_client *client;494495if (req->rq_cred.oa_flavor != RPCSEC_GSS)496return (FALSE);497498cc = req->rq_clntcred;499client = cc->cc_client;500if (rcred)501*rcred = &client->cl_rawcred;502if (ucred)503*ucred = &client->cl_ucred;504if (cookie)505*cookie = client->cl_cookie;506return (TRUE);507}508509/*510* This simpler interface is used by svc_getcred to copy the cred data511* into a kernel cred structure.512*/513static int514rpc_gss_svc_getcred(struct svc_req *req, struct ucred **crp, int *flavorp)515{516struct ucred *cr;517struct svc_rpc_gss_cookedcred *cc;518struct svc_rpc_gss_client *client;519rpc_gss_ucred_t *uc;520521if (req->rq_cred.oa_flavor != RPCSEC_GSS)522return (FALSE);523524cc = req->rq_clntcred;525client = cc->cc_client;526527if (flavorp)528*flavorp = client->cl_rpcflavor;529530if (client->cl_cred) {531*crp = crhold(client->cl_cred);532return (TRUE);533}534535uc = &client->cl_ucred;536cr = client->cl_cred = crget();537cr->cr_uid = cr->cr_ruid = cr->cr_svuid = uc->uid;538cr->cr_rgid = cr->cr_svgid = uc->gid;539crsetgroups_and_egid(cr, uc->gidlen, uc->gidlist, uc->gid);540cr->cr_prison = curthread->td_ucred->cr_prison;541prison_hold(cr->cr_prison);542*crp = crhold(cr);543544return (TRUE);545}546547int548rpc_gss_svc_max_data_length(struct svc_req *req, int max_tp_unit_len)549{550struct svc_rpc_gss_cookedcred *cc = req->rq_clntcred;551struct svc_rpc_gss_client *client = cc->cc_client;552int want_conf;553OM_uint32 max;554OM_uint32 maj_stat, min_stat;555int result;556557switch (client->cl_rawcred.service) {558case rpc_gss_svc_none:559return (max_tp_unit_len);560break;561562case rpc_gss_svc_default:563case rpc_gss_svc_integrity:564want_conf = FALSE;565break;566567case rpc_gss_svc_privacy:568want_conf = TRUE;569break;570571default:572return (0);573}574575maj_stat = gss_wrap_size_limit(&min_stat, client->cl_ctx, want_conf,576client->cl_qop, max_tp_unit_len, &max);577578if (maj_stat == GSS_S_COMPLETE) {579result = (int) max;580if (result < 0)581result = 0;582return (result);583} else {584rpc_gss_log_status("gss_wrap_size_limit", client->cl_mech,585maj_stat, min_stat);586return (0);587}588}589590static struct svc_rpc_gss_client *591svc_rpc_gss_find_client(struct svc_rpc_gss_clientid *id)592{593struct svc_rpc_gss_client *client;594struct svc_rpc_gss_client_list *list;595struct timeval boottime;596unsigned long hostid;597598rpc_gss_log_debug("in svc_rpc_gss_find_client(%d)", id->ci_id);599600getcredhostid(curthread->td_ucred, &hostid);601getboottime(&boottime);602if (id->ci_hostid != hostid || id->ci_boottime != boottime.tv_sec)603return (NULL);604605list = &KGSS_VNET(svc_rpc_gss_client_hash)606[id->ci_id % svc_rpc_gss_client_hash_size];607sx_xlock(&svc_rpc_gss_lock);608TAILQ_FOREACH(client, list, cl_link) {609if (client->cl_id.ci_id == id->ci_id) {610/*611* Move this client to the front of the LRU612* list.613*/614TAILQ_REMOVE(&KGSS_VNET(svc_rpc_gss_clients), client,615cl_alllink);616TAILQ_INSERT_HEAD(&KGSS_VNET(svc_rpc_gss_clients),617client, cl_alllink);618refcount_acquire(&client->cl_refs);619break;620}621}622sx_xunlock(&svc_rpc_gss_lock);623624return (client);625}626627static struct svc_rpc_gss_client *628svc_rpc_gss_create_client(void)629{630struct svc_rpc_gss_client *client;631struct svc_rpc_gss_client_list *list;632struct timeval boottime;633unsigned long hostid;634635rpc_gss_log_debug("in svc_rpc_gss_create_client()");636637client = mem_alloc(sizeof(struct svc_rpc_gss_client));638memset(client, 0, sizeof(struct svc_rpc_gss_client));639640/*641* Set the initial value of cl_refs to two. One for the caller642* and the other to hold onto the client structure until it expires.643*/644refcount_init(&client->cl_refs, 2);645sx_init(&client->cl_lock, "GSS-client");646getcredhostid(curthread->td_ucred, &hostid);647client->cl_id.ci_hostid = hostid;648getboottime(&boottime);649client->cl_id.ci_boottime = boottime.tv_sec;650client->cl_id.ci_id = KGSS_VNET(svc_rpc_gss_next_clientid)++;651652/*653* Start the client off with a short expiration time. We will654* try to get a saner value from the client creds later.655*/656client->cl_state = CLIENT_NEW;657client->cl_locked = FALSE;658client->cl_expiration = time_uptime + 5*60;659660list = &KGSS_VNET(svc_rpc_gss_client_hash)661[client->cl_id.ci_id % svc_rpc_gss_client_hash_size];662sx_xlock(&svc_rpc_gss_lock);663TAILQ_INSERT_HEAD(list, client, cl_link);664TAILQ_INSERT_HEAD(&KGSS_VNET(svc_rpc_gss_clients), client, cl_alllink);665svc_rpc_gss_client_count++;666sx_xunlock(&svc_rpc_gss_lock);667return (client);668}669670static void671svc_rpc_gss_destroy_client(struct svc_rpc_gss_client *client)672{673OM_uint32 min_stat;674675rpc_gss_log_debug("in svc_rpc_gss_destroy_client()");676677if (client->cl_ctx)678gss_delete_sec_context(&min_stat,679&client->cl_ctx, GSS_C_NO_BUFFER);680681if (client->cl_cname)682gss_release_name(&min_stat, &client->cl_cname);683684if (client->cl_rawcred.client_principal)685mem_free(client->cl_rawcred.client_principal,686sizeof(*client->cl_rawcred.client_principal)687+ client->cl_rawcred.client_principal->len);688689if (client->cl_cred)690crfree(client->cl_cred);691692sx_destroy(&client->cl_lock);693mem_free(client, sizeof(*client));694}695696/*697* Drop a reference to a client and free it if that was the last reference.698*/699static void700svc_rpc_gss_release_client(struct svc_rpc_gss_client *client)701{702703if (!refcount_release(&client->cl_refs))704return;705svc_rpc_gss_destroy_client(client);706}707708/*709* Remove a client from our global lists.710* Must be called with svc_rpc_gss_lock held.711*/712static void713svc_rpc_gss_forget_client_locked(struct svc_rpc_gss_client *client)714{715struct svc_rpc_gss_client_list *list;716717sx_assert(&svc_rpc_gss_lock, SX_XLOCKED);718list = &KGSS_VNET(svc_rpc_gss_client_hash)719[client->cl_id.ci_id % svc_rpc_gss_client_hash_size];720TAILQ_REMOVE(list, client, cl_link);721TAILQ_REMOVE(&KGSS_VNET(svc_rpc_gss_clients), client, cl_alllink);722svc_rpc_gss_client_count--;723}724725/*726* Remove a client from our global lists and free it if we can.727*/728static void729svc_rpc_gss_forget_client(struct svc_rpc_gss_client *client)730{731struct svc_rpc_gss_client_list *list;732struct svc_rpc_gss_client *tclient;733734list = &KGSS_VNET(svc_rpc_gss_client_hash)735[client->cl_id.ci_id % svc_rpc_gss_client_hash_size];736sx_xlock(&svc_rpc_gss_lock);737TAILQ_FOREACH(tclient, list, cl_link) {738/*739* Make sure this client has not already been removed740* from the lists by svc_rpc_gss_forget_client() or741* svc_rpc_gss_forget_client_locked().742*/743if (client == tclient) {744svc_rpc_gss_forget_client_locked(client);745sx_xunlock(&svc_rpc_gss_lock);746svc_rpc_gss_release_client(client);747return;748}749}750sx_xunlock(&svc_rpc_gss_lock);751}752753static void754svc_rpc_gss_timeout_clients(void)755{756struct svc_rpc_gss_client *client;757time_t now = time_uptime;758759rpc_gss_log_debug("in svc_rpc_gss_timeout_clients()");760761/*762* First enforce the max client limit. We keep763* svc_rpc_gss_clients in LRU order.764*/765sx_xlock(&svc_rpc_gss_lock);766client = TAILQ_LAST(&KGSS_VNET(svc_rpc_gss_clients),767svc_rpc_gss_client_list);768while (svc_rpc_gss_client_count > svc_rpc_gss_client_max && client != NULL) {769svc_rpc_gss_forget_client_locked(client);770sx_xunlock(&svc_rpc_gss_lock);771svc_rpc_gss_release_client(client);772sx_xlock(&svc_rpc_gss_lock);773client = TAILQ_LAST(&KGSS_VNET(svc_rpc_gss_clients),774svc_rpc_gss_client_list);775}776again:777TAILQ_FOREACH(client, &KGSS_VNET(svc_rpc_gss_clients), cl_alllink) {778if (client->cl_state == CLIENT_STALE779|| now > client->cl_expiration) {780svc_rpc_gss_forget_client_locked(client);781sx_xunlock(&svc_rpc_gss_lock);782rpc_gss_log_debug("expiring client %p", client);783svc_rpc_gss_release_client(client);784sx_xlock(&svc_rpc_gss_lock);785goto again;786}787}788sx_xunlock(&svc_rpc_gss_lock);789}790791#ifdef DEBUG792/*793* OID<->string routines. These are uuuuugly.794*/795static OM_uint32796gss_oid_to_str(OM_uint32 *minor_status, gss_OID oid, gss_buffer_t oid_str)797{798char numstr[128];799unsigned long number;800int numshift;801size_t string_length;802size_t i;803unsigned char *cp;804char *bp;805806/* Decoded according to krb5/gssapi_krb5.c */807808/* First determine the size of the string */809string_length = 0;810number = 0;811numshift = 0;812cp = (unsigned char *) oid->elements;813number = (unsigned long) cp[0];814sprintf(numstr, "%ld ", number/40);815string_length += strlen(numstr);816sprintf(numstr, "%ld ", number%40);817string_length += strlen(numstr);818for (i=1; i<oid->length; i++) {819if ( (size_t) (numshift+7) < (sizeof(unsigned long)*8)) {820number = (number << 7) | (cp[i] & 0x7f);821numshift += 7;822}823else {824*minor_status = 0;825return(GSS_S_FAILURE);826}827if ((cp[i] & 0x80) == 0) {828sprintf(numstr, "%ld ", number);829string_length += strlen(numstr);830number = 0;831numshift = 0;832}833}834/*835* If we get here, we've calculated the length of "n n n ... n ". Add 4836* here for "{ " and "}\0".837*/838string_length += 4;839if ((bp = malloc(string_length, M_GSSAPI, M_WAITOK | M_ZERO))) {840strcpy(bp, "{ ");841number = (unsigned long) cp[0];842sprintf(numstr, "%ld ", number/40);843strcat(bp, numstr);844sprintf(numstr, "%ld ", number%40);845strcat(bp, numstr);846number = 0;847cp = (unsigned char *) oid->elements;848for (i=1; i<oid->length; i++) {849number = (number << 7) | (cp[i] & 0x7f);850if ((cp[i] & 0x80) == 0) {851sprintf(numstr, "%ld ", number);852strcat(bp, numstr);853number = 0;854}855}856strcat(bp, "}");857oid_str->length = strlen(bp)+1;858oid_str->value = (void *) bp;859*minor_status = 0;860return(GSS_S_COMPLETE);861}862*minor_status = 0;863return(GSS_S_FAILURE);864}865#endif866867static void868svc_rpc_gss_build_ucred(struct svc_rpc_gss_client *client,869const gss_name_t name)870{871OM_uint32 maj_stat, min_stat;872rpc_gss_ucred_t *uc = &client->cl_ucred;873int numgroups;874875uc->uid = 65534;876uc->gid = 65534;877uc->gidlist = client->cl_gid_storage;878879numgroups = NGROUPS;880maj_stat = gss_pname_to_unix_cred(&min_stat, name, client->cl_mech,881&uc->uid, &uc->gid, &numgroups, &uc->gidlist[0]);882if (GSS_ERROR(maj_stat))883uc->gidlen = 0;884else885uc->gidlen = numgroups;886}887888static void889svc_rpc_gss_set_flavor(struct svc_rpc_gss_client *client)890{891static gss_OID_desc krb5_mech_oid =892{9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };893894/*895* Attempt to translate mech type and service into a896* 'pseudo flavor'. Hardwire in krb5 support for now.897*/898if (kgss_oid_equal(client->cl_mech, &krb5_mech_oid)) {899switch (client->cl_rawcred.service) {900case rpc_gss_svc_default:901case rpc_gss_svc_none:902client->cl_rpcflavor = RPCSEC_GSS_KRB5;903break;904case rpc_gss_svc_integrity:905client->cl_rpcflavor = RPCSEC_GSS_KRB5I;906break;907case rpc_gss_svc_privacy:908client->cl_rpcflavor = RPCSEC_GSS_KRB5P;909break;910}911} else {912client->cl_rpcflavor = RPCSEC_GSS;913}914}915916static bool_t917svc_rpc_gss_accept_sec_context(struct svc_rpc_gss_client *client,918struct svc_req *rqst,919struct rpc_gss_init_res *gr,920struct rpc_gss_cred *gc)921{922gss_buffer_desc recv_tok;923gss_OID mech;924OM_uint32 maj_stat = 0, min_stat = 0, ret_flags;925OM_uint32 cred_lifetime;926struct svc_rpc_gss_svc_name *sname;927gss_buffer_desc export_name;928rpc_gss_ucred_t *uc = &client->cl_ucred;929int numgroups;930static enum krb_imp my_krb_imp = KRBIMP_UNKNOWN;931932rpc_gss_log_debug("in svc_rpc_gss_accept_context()");933934if (my_krb_imp == KRBIMP_UNKNOWN) {935maj_stat = gss_supports_lucid(&min_stat, NULL);936if (maj_stat == GSS_S_COMPLETE)937my_krb_imp = KRBIMP_MIT;938else939my_krb_imp = KRBIMP_HEIMDALV1;940min_stat = 0;941}942943if (my_krb_imp == KRBIMP_MIT) {944uc->uid = 65534;945uc->gid = 65534;946uc->gidlist = client->cl_gid_storage;947numgroups = NGROUPS;948}949950/* Deserialize arguments. */951memset(&recv_tok, 0, sizeof(recv_tok));952953if (!svc_getargs(rqst,954(xdrproc_t) xdr_gss_buffer_desc,955(caddr_t) &recv_tok)) {956client->cl_state = CLIENT_STALE;957return (FALSE);958}959960/*961* First time round, try all the server names we have until962* one matches. Afterwards, stick with that one.963*/964sx_xlock(&svc_rpc_gss_lock);965if (!client->cl_sname) {966SLIST_FOREACH(sname, &KGSS_VNET(svc_rpc_gss_svc_names),967sn_link) {968if (sname->sn_program == rqst->rq_prog969&& sname->sn_version == rqst->rq_vers) {970retry:971if (my_krb_imp == KRBIMP_MIT)972gr->gr_major =973gss_accept_sec_context_lucid_v1(974&gr->gr_minor,975&client->cl_ctx,976sname->sn_cred,977&recv_tok,978GSS_C_NO_CHANNEL_BINDINGS,979&client->cl_cname,980&mech,981&gr->gr_token,982&ret_flags,983&cred_lifetime,984&client->cl_creds,985&export_name,986&uc->uid,987&uc->gid,988&numgroups,989&uc->gidlist[0]);990else991gr->gr_major = gss_accept_sec_context(992&gr->gr_minor,993&client->cl_ctx,994sname->sn_cred,995&recv_tok,996GSS_C_NO_CHANNEL_BINDINGS,997&client->cl_cname,998&mech,999&gr->gr_token,1000&ret_flags,1001&cred_lifetime,1002&client->cl_creds);1003if (gr->gr_major ==1004GSS_S_CREDENTIALS_EXPIRED) {1005/*1006* Either our creds really did1007* expire or gssd was1008* restarted.1009*/1010if (rpc_gss_acquire_svc_cred(sname))1011goto retry;1012}1013client->cl_sname = sname;1014break;1015}1016}1017if (!sname) {1018xdr_free((xdrproc_t) xdr_gss_buffer_desc,1019(char *) &recv_tok);1020sx_xunlock(&svc_rpc_gss_lock);1021return (FALSE);1022}1023} else {1024if (my_krb_imp == KRBIMP_MIT)1025gr->gr_major = gss_accept_sec_context_lucid_v1(1026&gr->gr_minor,1027&client->cl_ctx,1028client->cl_sname->sn_cred,1029&recv_tok,1030GSS_C_NO_CHANNEL_BINDINGS,1031&client->cl_cname,1032&mech,1033&gr->gr_token,1034&ret_flags,1035&cred_lifetime,1036NULL,1037&export_name,1038&uc->uid,1039&uc->gid,1040&numgroups,1041&uc->gidlist[0]);1042else1043gr->gr_major = gss_accept_sec_context(1044&gr->gr_minor,1045&client->cl_ctx,1046client->cl_sname->sn_cred,1047&recv_tok,1048GSS_C_NO_CHANNEL_BINDINGS,1049&client->cl_cname,1050&mech,1051&gr->gr_token,1052&ret_flags,1053&cred_lifetime,1054NULL);1055}1056sx_xunlock(&svc_rpc_gss_lock);10571058xdr_free((xdrproc_t) xdr_gss_buffer_desc, (char *) &recv_tok);10591060/*1061* If we get an error from gss_accept_sec_context, send the1062* reply anyway so that the client gets a chance to see what1063* is wrong.1064*/1065if (gr->gr_major != GSS_S_COMPLETE &&1066gr->gr_major != GSS_S_CONTINUE_NEEDED) {1067rpc_gss_log_status("accept_sec_context", client->cl_mech,1068gr->gr_major, gr->gr_minor);1069client->cl_state = CLIENT_STALE;1070if (my_krb_imp == KRBIMP_MIT)1071uc->gidlen = 0;1072return (TRUE);1073}1074if (my_krb_imp == KRBIMP_MIT)1075uc->gidlen = numgroups;10761077gr->gr_handle.value = &client->cl_id;1078gr->gr_handle.length = sizeof(client->cl_id);1079gr->gr_win = SVC_RPC_GSS_SEQWINDOW;10801081/* Save client info. */1082client->cl_mech = mech;1083client->cl_qop = GSS_C_QOP_DEFAULT;1084client->cl_done_callback = FALSE;10851086if (gr->gr_major == GSS_S_COMPLETE) {1087/*1088* Change client expiration time to be near when the1089* client creds expire (or 24 hours if we can't figure1090* that out).1091*/1092if (cred_lifetime == GSS_C_INDEFINITE)1093cred_lifetime = 24*60*60;10941095/*1096* Cap cred_lifetime if sysctl kern.rpc.gss.lifetime_max is set.1097*/1098if (svc_rpc_gss_lifetime_max > 0 && cred_lifetime >1099svc_rpc_gss_lifetime_max)1100cred_lifetime = svc_rpc_gss_lifetime_max;11011102client->cl_expiration = time_uptime + cred_lifetime;11031104/*1105* Fill in cred details in the rawcred structure.1106*/1107client->cl_rawcred.version = RPCSEC_GSS_VERSION;1108rpc_gss_oid_to_mech(mech, &client->cl_rawcred.mechanism);1109maj_stat = GSS_S_COMPLETE;1110if (my_krb_imp != KRBIMP_MIT)1111maj_stat = gss_export_name(&min_stat, client->cl_cname,1112&export_name);1113if (maj_stat != GSS_S_COMPLETE) {1114rpc_gss_log_status("gss_export_name", client->cl_mech,1115maj_stat, min_stat);1116return (FALSE);1117}1118client->cl_rawcred.client_principal =1119mem_alloc(sizeof(*client->cl_rawcred.client_principal)1120+ export_name.length);1121client->cl_rawcred.client_principal->len = export_name.length;1122memcpy(client->cl_rawcred.client_principal->name,1123export_name.value, export_name.length);1124gss_release_buffer(&min_stat, &export_name);1125client->cl_rawcred.svc_principal =1126client->cl_sname->sn_principal;1127client->cl_rawcred.service = gc->gc_svc;11281129/*1130* Use gss_pname_to_uid to map to unix creds. For1131* kerberos5, this uses krb5_aname_to_localname.1132*/1133if (my_krb_imp != KRBIMP_MIT)1134svc_rpc_gss_build_ucred(client, client->cl_cname);1135svc_rpc_gss_set_flavor(client);1136gss_release_name(&min_stat, &client->cl_cname);11371138#ifdef DEBUG1139{1140gss_buffer_desc mechname;11411142gss_oid_to_str(&min_stat, mech, &mechname);11431144rpc_gss_log_debug("accepted context for %s with "1145"<mech %.*s, qop %d, svc %d>",1146client->cl_rawcred.client_principal->name,1147mechname.length, (char *)mechname.value,1148client->cl_qop, client->cl_rawcred.service);11491150gss_release_buffer(&min_stat, &mechname);1151}1152#endif /* DEBUG */1153}1154return (TRUE);1155}11561157static bool_t1158svc_rpc_gss_validate(struct svc_rpc_gss_client *client, struct rpc_msg *msg,1159gss_qop_t *qop, rpc_gss_proc_t gcproc)1160{1161struct opaque_auth *oa;1162gss_buffer_desc rpcbuf, checksum;1163OM_uint32 maj_stat, min_stat;1164gss_qop_t qop_state;1165int32_t rpchdr[128 / sizeof(int32_t)];1166int32_t *buf;11671168rpc_gss_log_debug("in svc_rpc_gss_validate()");11691170memset(rpchdr, 0, sizeof(rpchdr));11711172/* Reconstruct RPC header for signing (from xdr_callmsg). */1173buf = rpchdr;1174IXDR_PUT_LONG(buf, msg->rm_xid);1175IXDR_PUT_ENUM(buf, msg->rm_direction);1176IXDR_PUT_LONG(buf, msg->rm_call.cb_rpcvers);1177IXDR_PUT_LONG(buf, msg->rm_call.cb_prog);1178IXDR_PUT_LONG(buf, msg->rm_call.cb_vers);1179IXDR_PUT_LONG(buf, msg->rm_call.cb_proc);1180oa = &msg->rm_call.cb_cred;1181IXDR_PUT_ENUM(buf, oa->oa_flavor);1182IXDR_PUT_LONG(buf, oa->oa_length);1183if (oa->oa_length) {1184memcpy((caddr_t)buf, oa->oa_base, oa->oa_length);1185buf += RNDUP(oa->oa_length) / sizeof(int32_t);1186}1187rpcbuf.value = rpchdr;1188rpcbuf.length = (u_char *)buf - (u_char *)rpchdr;11891190checksum.value = msg->rm_call.cb_verf.oa_base;1191checksum.length = msg->rm_call.cb_verf.oa_length;11921193maj_stat = gss_verify_mic(&min_stat, client->cl_ctx, &rpcbuf, &checksum,1194&qop_state);11951196if (maj_stat != GSS_S_COMPLETE) {1197rpc_gss_log_status("gss_verify_mic", client->cl_mech,1198maj_stat, min_stat);1199/*1200* A bug in some versions of the Linux client generates a1201* Destroy operation with a bogus encrypted checksum. Deleting1202* the credential handle for that case causes the mount to fail.1203* Since the checksum is bogus (gss_verify_mic() failed), it1204* doesn't make sense to destroy the handle and not doing so1205* fixes the Linux mount.1206*/1207if (gcproc != RPCSEC_GSS_DESTROY)1208client->cl_state = CLIENT_STALE;1209return (FALSE);1210}12111212*qop = qop_state;1213return (TRUE);1214}12151216static bool_t1217svc_rpc_gss_nextverf(struct svc_rpc_gss_client *client,1218struct svc_req *rqst, u_int seq)1219{1220gss_buffer_desc signbuf;1221gss_buffer_desc mic;1222OM_uint32 maj_stat, min_stat;1223uint32_t nseq;12241225rpc_gss_log_debug("in svc_rpc_gss_nextverf()");12261227nseq = htonl(seq);1228signbuf.value = &nseq;1229signbuf.length = sizeof(nseq);12301231maj_stat = gss_get_mic(&min_stat, client->cl_ctx, client->cl_qop,1232&signbuf, &mic);12331234if (maj_stat != GSS_S_COMPLETE) {1235rpc_gss_log_status("gss_get_mic", client->cl_mech, maj_stat, min_stat);1236client->cl_state = CLIENT_STALE;1237return (FALSE);1238}12391240KASSERT(mic.length <= MAX_AUTH_BYTES,1241("MIC too large for RPCSEC_GSS"));12421243rqst->rq_verf.oa_flavor = RPCSEC_GSS;1244rqst->rq_verf.oa_length = mic.length;1245bcopy(mic.value, rqst->rq_verf.oa_base, mic.length);12461247gss_release_buffer(&min_stat, &mic);12481249return (TRUE);1250}12511252static bool_t1253svc_rpc_gss_callback(struct svc_rpc_gss_client *client, struct svc_req *rqst)1254{1255struct svc_rpc_gss_callback *scb;1256rpc_gss_lock_t lock;1257void *cookie;1258bool_t cb_res;1259bool_t result;12601261/*1262* See if we have a callback for this guy.1263*/1264result = TRUE;1265SLIST_FOREACH(scb, &KGSS_VNET(svc_rpc_gss_callbacks), cb_link) {1266if (scb->cb_callback.program == rqst->rq_prog1267&& scb->cb_callback.version == rqst->rq_vers) {1268/*1269* This one matches. Call the callback and see1270* if it wants to veto or something.1271*/1272lock.locked = FALSE;1273lock.raw_cred = &client->cl_rawcred;1274cb_res = scb->cb_callback.callback(rqst,1275client->cl_creds,1276client->cl_ctx,1277&lock,1278&cookie);12791280if (!cb_res) {1281client->cl_state = CLIENT_STALE;1282result = FALSE;1283break;1284}12851286/*1287* The callback accepted the connection - it1288* is responsible for freeing client->cl_creds1289* now.1290*/1291client->cl_creds = GSS_C_NO_CREDENTIAL;1292client->cl_locked = lock.locked;1293client->cl_cookie = cookie;1294return (TRUE);1295}1296}12971298/*1299* Either no callback exists for this program/version or one1300* of the callbacks rejected the connection. We just need to1301* clean up the delegated client creds, if any.1302*/1303if (client->cl_creds) {1304OM_uint32 min_ver;1305gss_release_cred(&min_ver, &client->cl_creds);1306}1307return (result);1308}13091310static bool_t1311svc_rpc_gss_check_replay(struct svc_rpc_gss_client *client, uint32_t seq)1312{1313uint32_t offset;1314int word, bit;1315bool_t result;13161317sx_xlock(&client->cl_lock);1318if (seq <= client->cl_seqlast) {1319/*1320* The request sequence number is less than1321* the largest we have seen so far. If it is1322* outside the window or if we have seen a1323* request with this sequence before, silently1324* discard it.1325*/1326offset = client->cl_seqlast - seq;1327if (offset >= SVC_RPC_GSS_SEQWINDOW) {1328result = FALSE;1329goto out;1330}1331word = offset / 32;1332bit = offset % 32;1333if (client->cl_seqmask[word] & (1 << bit)) {1334result = FALSE;1335goto out;1336}1337}13381339result = TRUE;1340out:1341sx_xunlock(&client->cl_lock);1342return (result);1343}13441345static void1346svc_rpc_gss_update_seq(struct svc_rpc_gss_client *client, uint32_t seq)1347{1348int offset, i, word, bit;1349uint32_t carry, newcarry;13501351sx_xlock(&client->cl_lock);1352if (seq > client->cl_seqlast) {1353/*1354* This request has a sequence number greater1355* than any we have seen so far. Advance the1356* seq window and set bit zero of the window1357* (which corresponds to the new sequence1358* number)1359*/1360offset = seq - client->cl_seqlast;1361while (offset > 32) {1362for (i = (SVC_RPC_GSS_SEQWINDOW / 32) - 1;1363i > 0; i--) {1364client->cl_seqmask[i] = client->cl_seqmask[i-1];1365}1366client->cl_seqmask[0] = 0;1367offset -= 32;1368}1369carry = 0;1370for (i = 0; i < SVC_RPC_GSS_SEQWINDOW / 32; i++) {1371newcarry = client->cl_seqmask[i] >> (32 - offset);1372client->cl_seqmask[i] =1373(client->cl_seqmask[i] << offset) | carry;1374carry = newcarry;1375}1376client->cl_seqmask[0] |= 1;1377client->cl_seqlast = seq;1378} else {1379offset = client->cl_seqlast - seq;1380word = offset / 32;1381bit = offset % 32;1382client->cl_seqmask[word] |= (1 << bit);1383}1384sx_xunlock(&client->cl_lock);1385}13861387enum auth_stat1388svc_rpc_gss(struct svc_req *rqst, struct rpc_msg *msg)13891390{1391OM_uint32 min_stat;1392XDR xdrs;1393struct svc_rpc_gss_cookedcred *cc;1394struct svc_rpc_gss_client *client;1395struct rpc_gss_cred gc;1396struct rpc_gss_init_res gr;1397gss_qop_t qop;1398int call_stat;1399enum auth_stat result;14001401KGSS_CURVNET_SET_QUIET(KGSS_TD_TO_VNET(curthread));1402rpc_gss_log_debug("in svc_rpc_gss()");14031404/* Garbage collect old clients. */1405svc_rpc_gss_timeout_clients();14061407/* Initialize reply. */1408rqst->rq_verf = _null_auth;14091410/* Deserialize client credentials. */1411if (rqst->rq_cred.oa_length <= 0) {1412KGSS_CURVNET_RESTORE();1413return (AUTH_BADCRED);1414}14151416memset(&gc, 0, sizeof(gc));14171418xdrmem_create(&xdrs, rqst->rq_cred.oa_base,1419rqst->rq_cred.oa_length, XDR_DECODE);14201421if (!xdr_rpc_gss_cred(&xdrs, &gc)) {1422XDR_DESTROY(&xdrs);1423KGSS_CURVNET_RESTORE();1424return (AUTH_BADCRED);1425}1426XDR_DESTROY(&xdrs);14271428client = NULL;14291430/* Check version. */1431if (gc.gc_version != RPCSEC_GSS_VERSION) {1432result = AUTH_BADCRED;1433goto out;1434}14351436/* Check the proc and find the client (or create it) */1437if (gc.gc_proc == RPCSEC_GSS_INIT) {1438if (gc.gc_handle.length != 0) {1439result = AUTH_BADCRED;1440goto out;1441}1442client = svc_rpc_gss_create_client();1443} else {1444struct svc_rpc_gss_clientid *p;1445if (gc.gc_handle.length != sizeof(*p)) {1446result = AUTH_BADCRED;1447goto out;1448}1449p = gc.gc_handle.value;1450client = svc_rpc_gss_find_client(p);1451if (!client) {1452/*1453* Can't find the client - we may have1454* destroyed it - tell the other side to1455* re-authenticate.1456*/1457result = RPCSEC_GSS_CREDPROBLEM;1458goto out;1459}1460}1461cc = rqst->rq_clntcred;1462cc->cc_client = client;1463cc->cc_service = gc.gc_svc;1464cc->cc_seq = gc.gc_seq;14651466/*1467* The service and sequence number must be ignored for1468* RPCSEC_GSS_INIT and RPCSEC_GSS_CONTINUE_INIT.1469*/1470if (gc.gc_proc != RPCSEC_GSS_INIT1471&& gc.gc_proc != RPCSEC_GSS_CONTINUE_INIT) {1472/*1473* Check for sequence number overflow.1474*/1475if (gc.gc_seq >= MAXSEQ) {1476result = RPCSEC_GSS_CTXPROBLEM;1477goto out;1478}14791480/*1481* Check for valid service.1482*/1483if (gc.gc_svc != rpc_gss_svc_none &&1484gc.gc_svc != rpc_gss_svc_integrity &&1485gc.gc_svc != rpc_gss_svc_privacy) {1486result = AUTH_BADCRED;1487goto out;1488}1489}14901491/* Handle RPCSEC_GSS control procedure. */1492switch (gc.gc_proc) {14931494case RPCSEC_GSS_INIT:1495case RPCSEC_GSS_CONTINUE_INIT:1496if (rqst->rq_proc != NULLPROC) {1497result = AUTH_REJECTEDCRED;1498break;1499}15001501memset(&gr, 0, sizeof(gr));1502if (!svc_rpc_gss_accept_sec_context(client, rqst, &gr, &gc)) {1503result = AUTH_REJECTEDCRED;1504break;1505}15061507if (gr.gr_major == GSS_S_COMPLETE) {1508/*1509* We borrow the space for the call verf to1510* pack our reply verf.1511*/1512rqst->rq_verf = msg->rm_call.cb_verf;1513if (!svc_rpc_gss_nextverf(client, rqst, gr.gr_win)) {1514result = AUTH_REJECTEDCRED;1515break;1516}1517} else {1518rqst->rq_verf = _null_auth;1519}15201521call_stat = svc_sendreply(rqst,1522(xdrproc_t) xdr_rpc_gss_init_res,1523(caddr_t) &gr);15241525gss_release_buffer(&min_stat, &gr.gr_token);15261527if (!call_stat) {1528result = AUTH_FAILED;1529break;1530}15311532if (gr.gr_major == GSS_S_COMPLETE)1533client->cl_state = CLIENT_ESTABLISHED;15341535result = RPCSEC_GSS_NODISPATCH;1536break;15371538case RPCSEC_GSS_DATA:1539case RPCSEC_GSS_DESTROY:1540if (!svc_rpc_gss_check_replay(client, gc.gc_seq)) {1541result = RPCSEC_GSS_NODISPATCH;1542break;1543}15441545if (!svc_rpc_gss_validate(client, msg, &qop, gc.gc_proc)) {1546result = RPCSEC_GSS_CREDPROBLEM;1547break;1548}15491550/*1551* We borrow the space for the call verf to pack our1552* reply verf.1553*/1554rqst->rq_verf = msg->rm_call.cb_verf;1555if (!svc_rpc_gss_nextverf(client, rqst, gc.gc_seq)) {1556result = RPCSEC_GSS_CTXPROBLEM;1557break;1558}15591560svc_rpc_gss_update_seq(client, gc.gc_seq);15611562/*1563* Change the SVCAUTH ops on the request to point at1564* our own code so that we can unwrap the arguments1565* and wrap the result. The caller will re-set this on1566* every request to point to a set of null wrap/unwrap1567* methods. Acquire an extra reference to the client1568* which will be released by svc_rpc_gss_release()1569* after the request has finished processing.1570*/1571refcount_acquire(&client->cl_refs);1572rqst->rq_auth.svc_ah_ops = &svc_auth_gss_ops;1573rqst->rq_auth.svc_ah_private = cc;15741575if (gc.gc_proc == RPCSEC_GSS_DATA) {1576/*1577* We might be ready to do a callback to the server to1578* see if it wants to accept/reject the connection.1579*/1580sx_xlock(&client->cl_lock);1581if (!client->cl_done_callback) {1582client->cl_done_callback = TRUE;1583client->cl_qop = qop;1584client->cl_rawcred.qop = _rpc_gss_num_to_qop(1585client->cl_rawcred.mechanism, qop);1586if (!svc_rpc_gss_callback(client, rqst)) {1587result = AUTH_REJECTEDCRED;1588sx_xunlock(&client->cl_lock);1589break;1590}1591}1592sx_xunlock(&client->cl_lock);15931594/*1595* If the server has locked this client to a1596* particular service+qop pair, enforce that1597* restriction now.1598*/1599if (client->cl_locked) {1600if (client->cl_rawcred.service != gc.gc_svc) {1601result = AUTH_FAILED;1602break;1603} else if (client->cl_qop != qop) {1604result = AUTH_BADVERF;1605break;1606}1607}16081609/*1610* If the qop changed, look up the new qop1611* name for rawcred.1612*/1613if (client->cl_qop != qop) {1614client->cl_qop = qop;1615client->cl_rawcred.qop = _rpc_gss_num_to_qop(1616client->cl_rawcred.mechanism, qop);1617}16181619/*1620* Make sure we use the right service value1621* for unwrap/wrap.1622*/1623if (client->cl_rawcred.service != gc.gc_svc) {1624client->cl_rawcred.service = gc.gc_svc;1625svc_rpc_gss_set_flavor(client);1626}16271628result = AUTH_OK;1629} else {1630if (rqst->rq_proc != NULLPROC) {1631result = AUTH_REJECTEDCRED;1632break;1633}16341635call_stat = svc_sendreply(rqst,1636(xdrproc_t) xdr_void, (caddr_t) NULL);16371638if (!call_stat) {1639result = AUTH_FAILED;1640break;1641}16421643svc_rpc_gss_forget_client(client);16441645result = RPCSEC_GSS_NODISPATCH;1646break;1647}1648break;16491650default:1651result = AUTH_BADCRED;1652break;1653}1654out:1655if (client)1656svc_rpc_gss_release_client(client);16571658xdr_free((xdrproc_t) xdr_rpc_gss_cred, (char *) &gc);1659KGSS_CURVNET_RESTORE();1660return (result);1661}16621663static bool_t1664svc_rpc_gss_wrap(SVCAUTH *auth, struct mbuf **mp)1665{1666struct svc_rpc_gss_cookedcred *cc;1667struct svc_rpc_gss_client *client;16681669rpc_gss_log_debug("in svc_rpc_gss_wrap()");16701671cc = (struct svc_rpc_gss_cookedcred *) auth->svc_ah_private;1672client = cc->cc_client;1673if (client->cl_state != CLIENT_ESTABLISHED1674|| cc->cc_service == rpc_gss_svc_none || *mp == NULL) {1675return (TRUE);1676}16771678return (xdr_rpc_gss_wrap_data(mp,1679client->cl_ctx, client->cl_qop,1680cc->cc_service, cc->cc_seq));1681}16821683static bool_t1684svc_rpc_gss_unwrap(SVCAUTH *auth, struct mbuf **mp)1685{1686struct svc_rpc_gss_cookedcred *cc;1687struct svc_rpc_gss_client *client;16881689rpc_gss_log_debug("in svc_rpc_gss_unwrap()");16901691cc = (struct svc_rpc_gss_cookedcred *) auth->svc_ah_private;1692client = cc->cc_client;1693if (client->cl_state != CLIENT_ESTABLISHED1694|| cc->cc_service == rpc_gss_svc_none) {1695return (TRUE);1696}16971698return (xdr_rpc_gss_unwrap_data(mp,1699client->cl_ctx, client->cl_qop,1700cc->cc_service, cc->cc_seq));1701}17021703static void1704svc_rpc_gss_release(SVCAUTH *auth)1705{1706struct svc_rpc_gss_cookedcred *cc;1707struct svc_rpc_gss_client *client;17081709rpc_gss_log_debug("in svc_rpc_gss_release()");17101711cc = (struct svc_rpc_gss_cookedcred *) auth->svc_ah_private;1712client = cc->cc_client;1713svc_rpc_gss_release_client(client);1714}171517161717