2017-10-04 16:33:43 +00:00
|
|
|
/*
|
2010-05-17 22:18:00 +00:00
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2009-07-28 00:18:59 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2009-07-28 00:18:59 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2009-07-28 00:18:59 +00:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
2018-02-07 19:49:38 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* Solaris Porting Layer (SPL) Credential Implementation.
|
2017-10-04 16:33:43 +00:00
|
|
|
*/
|
2009-07-28 00:18:59 +00:00
|
|
|
|
|
|
|
#include <sys/cred.h>
|
|
|
|
|
|
|
|
static int
|
2013-07-13 20:34:56 +00:00
|
|
|
cr_groups_search(const struct group_info *group_info, kgid_t grp)
|
2009-07-28 00:18:59 +00:00
|
|
|
{
|
2014-04-07 21:31:21 +00:00
|
|
|
unsigned int left, right, mid;
|
|
|
|
int cmp;
|
2009-07-28 00:18:59 +00:00
|
|
|
|
|
|
|
if (!group_info)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (0);
|
2009-07-28 00:18:59 +00:00
|
|
|
|
|
|
|
left = 0;
|
|
|
|
right = group_info->ngroups;
|
|
|
|
while (left < right) {
|
2014-04-07 21:31:21 +00:00
|
|
|
mid = (left + right) / 2;
|
|
|
|
cmp = KGID_TO_SGID(grp) -
|
|
|
|
KGID_TO_SGID(GROUP_AT(group_info, mid));
|
|
|
|
|
2009-07-28 00:18:59 +00:00
|
|
|
if (cmp > 0)
|
|
|
|
left = mid + 1;
|
|
|
|
else if (cmp < 0)
|
|
|
|
right = mid;
|
|
|
|
else
|
2018-02-07 19:49:38 +00:00
|
|
|
return (1);
|
2009-07-28 00:18:59 +00:00
|
|
|
}
|
2018-02-07 19:49:38 +00:00
|
|
|
return (0);
|
2009-07-28 00:18:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 22:52:30 +00:00
|
|
|
/* Hold a reference on the credential */
|
2009-07-28 00:18:59 +00:00
|
|
|
void
|
|
|
|
crhold(cred_t *cr)
|
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
(void) get_cred((const cred_t *)cr);
|
2009-07-28 00:18:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 22:52:30 +00:00
|
|
|
/* Free a reference on the credential */
|
2009-07-28 00:18:59 +00:00
|
|
|
void
|
|
|
|
crfree(cred_t *cr)
|
|
|
|
{
|
|
|
|
put_cred((const cred_t *)cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of supplemental groups */
|
|
|
|
int
|
|
|
|
crgetngroups(const cred_t *cr)
|
|
|
|
{
|
|
|
|
struct group_info *gi;
|
|
|
|
int rc;
|
|
|
|
|
2016-10-18 22:52:30 +00:00
|
|
|
gi = cr->group_info;
|
2009-07-28 00:18:59 +00:00
|
|
|
rc = gi->ngroups;
|
2016-10-19 00:30:41 +00:00
|
|
|
#ifndef HAVE_GROUP_INFO_GID
|
2016-10-18 22:52:30 +00:00
|
|
|
/*
|
2016-10-19 00:30:41 +00:00
|
|
|
* For Linux <= 4.8,
|
2016-10-18 22:52:30 +00:00
|
|
|
* crgetgroups will only returns gi->blocks[0], which contains only
|
|
|
|
* the first NGROUPS_PER_BLOCK groups.
|
|
|
|
*/
|
|
|
|
if (rc > NGROUPS_PER_BLOCK) {
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
rc = NGROUPS_PER_BLOCK;
|
|
|
|
}
|
2016-10-19 00:30:41 +00:00
|
|
|
#endif
|
2018-02-07 19:49:38 +00:00
|
|
|
return (rc);
|
2009-07-28 00:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an array of supplemental gids. The returned address is safe
|
|
|
|
* to use as long as the caller has taken a reference with crhold().
|
2016-10-19 00:30:41 +00:00
|
|
|
*
|
|
|
|
* Linux 4.9 API change, group_info changed from 2d array via ->blocks to 1d
|
|
|
|
* array via ->gid.
|
2009-07-28 00:18:59 +00:00
|
|
|
*/
|
|
|
|
gid_t *
|
|
|
|
crgetgroups(const cred_t *cr)
|
|
|
|
{
|
|
|
|
struct group_info *gi;
|
2016-10-18 22:52:30 +00:00
|
|
|
gid_t *gids = NULL;
|
2009-07-28 00:18:59 +00:00
|
|
|
|
2016-10-18 22:52:30 +00:00
|
|
|
gi = cr->group_info;
|
2016-10-19 00:30:41 +00:00
|
|
|
#ifdef HAVE_GROUP_INFO_GID
|
|
|
|
gids = KGIDP_TO_SGIDP(gi->gid);
|
|
|
|
#else
|
2016-10-18 22:52:30 +00:00
|
|
|
if (gi->nblocks > 0)
|
|
|
|
gids = KGIDP_TO_SGIDP(gi->blocks[0]);
|
2016-10-19 00:30:41 +00:00
|
|
|
#endif
|
2018-02-07 19:49:38 +00:00
|
|
|
return (gids);
|
2009-07-28 00:18:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 21:31:21 +00:00
|
|
|
/* Check if the passed gid is available in supplied credential. */
|
2009-07-28 00:18:59 +00:00
|
|
|
int
|
|
|
|
groupmember(gid_t gid, const cred_t *cr)
|
|
|
|
{
|
|
|
|
struct group_info *gi;
|
|
|
|
int rc;
|
|
|
|
|
2016-10-18 22:52:30 +00:00
|
|
|
gi = cr->group_info;
|
2014-04-07 21:31:21 +00:00
|
|
|
rc = cr_groups_search(gi, SGID_TO_KGID(gid));
|
2009-07-28 00:18:59 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
return (rc);
|
2009-07-28 00:18:59 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 18:18:15 +00:00
|
|
|
/* Return the effective user id */
|
|
|
|
uid_t
|
|
|
|
crgetuid(const cred_t *cr)
|
|
|
|
{
|
2022-03-21 04:21:18 +00:00
|
|
|
return (KUID_TO_SUID(cr->fsuid));
|
2011-03-22 18:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the real user id */
|
|
|
|
uid_t
|
|
|
|
crgetruid(const cred_t *cr)
|
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
return (KUID_TO_SUID(cr->uid));
|
2011-03-22 18:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the effective group id */
|
|
|
|
gid_t
|
|
|
|
crgetgid(const cred_t *cr)
|
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
return (KGID_TO_SGID(cr->fsgid));
|
2011-03-22 18:18:15 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 00:18:59 +00:00
|
|
|
EXPORT_SYMBOL(crhold);
|
|
|
|
EXPORT_SYMBOL(crfree);
|
|
|
|
EXPORT_SYMBOL(crgetuid);
|
|
|
|
EXPORT_SYMBOL(crgetruid);
|
|
|
|
EXPORT_SYMBOL(crgetgid);
|
|
|
|
EXPORT_SYMBOL(crgetngroups);
|
|
|
|
EXPORT_SYMBOL(crgetgroups);
|
|
|
|
EXPORT_SYMBOL(groupmember);
|