Path: blob/main/sys/contrib/openzfs/module/os/linux/spl/spl-cred.c
48775 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.3* Copyright (C) 2007 The Regents of the University of California.4* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).5* Written by Brian Behlendorf <[email protected]>.6* UCRL-CODE-2351977*8* This file is part of the SPL, Solaris Porting Layer.9*10* The SPL is free software; you can redistribute it and/or modify it11* under the terms of the GNU General Public License as published by the12* Free Software Foundation; either version 2 of the License, or (at your13* option) any later version.14*15* The SPL is distributed in the hope that it will be useful, but WITHOUT16* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or17* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License18* for more details.19*20* You should have received a copy of the GNU General Public License along21* with the SPL. If not, see <http://www.gnu.org/licenses/>.22*23* Solaris Porting Layer (SPL) Credential Implementation.24*/2526#include <sys/cred.h>2728static int29cr_groups_search(const struct group_info *group_info, kgid_t grp)30{31unsigned int left, right, mid;32int cmp;3334if (!group_info)35return (0);3637left = 0;38right = group_info->ngroups;39while (left < right) {40mid = (left + right) / 2;41cmp = KGID_TO_SGID(grp) -42KGID_TO_SGID(GROUP_AT(group_info, mid));4344if (cmp > 0)45left = mid + 1;46else if (cmp < 0)47right = mid;48else49return (1);50}51return (0);52}5354/* Hold a reference on the credential */55void56crhold(cred_t *cr)57{58(void) get_cred((const cred_t *)cr);59}6061/* Free a reference on the credential */62void63crfree(cred_t *cr)64{65put_cred((const cred_t *)cr);66}6768/* Return the number of supplemental groups */69int70crgetngroups(const cred_t *cr)71{72struct group_info *gi;73int rc;7475gi = cr->group_info;76rc = gi->ngroups;7778return (rc);79}8081/*82* Return an array of supplemental gids. The returned address is safe83* to use as long as the caller has taken a reference with crhold().84*/85gid_t *86crgetgroups(const cred_t *cr)87{88struct group_info *gi;89gid_t *gids = NULL;9091gi = cr->group_info;92gids = KGIDP_TO_SGIDP(gi->gid);9394return (gids);95}9697/* Check if the passed gid is available in supplied credential. */98int99groupmember(gid_t gid, const cred_t *cr)100{101struct group_info *gi;102int rc;103104gi = cr->group_info;105rc = cr_groups_search(gi, SGID_TO_KGID(gid));106107return (rc);108}109110/* Return the effective user id */111uid_t112crgetuid(const cred_t *cr)113{114return (KUID_TO_SUID(cr->fsuid));115}116117/* Return the real user id */118uid_t119crgetruid(const cred_t *cr)120{121return (KUID_TO_SUID(cr->uid));122}123124/* Return the effective group id */125gid_t126crgetgid(const cred_t *cr)127{128return (KGID_TO_SGID(cr->fsgid));129}130131/* Return the initial user ns or nop_mnt_idmap */132zidmap_t *133zfs_get_init_idmap(void)134{135#ifdef HAVE_IOPS_CREATE_IDMAP136return ((zidmap_t *)&nop_mnt_idmap);137#else138return ((zidmap_t *)&init_user_ns);139#endif140}141142EXPORT_SYMBOL(zfs_get_init_idmap);143EXPORT_SYMBOL(crhold);144EXPORT_SYMBOL(crfree);145EXPORT_SYMBOL(crgetuid);146EXPORT_SYMBOL(crgetruid);147EXPORT_SYMBOL(crgetgid);148EXPORT_SYMBOL(crgetngroups);149EXPORT_SYMBOL(crgetgroups);150EXPORT_SYMBOL(groupmember);151152153