1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3eda14cbcSMatt Macy  *  Copyright (C) 2007 The Regents of the University of California.
4eda14cbcSMatt Macy  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5eda14cbcSMatt Macy  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6eda14cbcSMatt Macy  *  UCRL-CODE-235197
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  *  This file is part of the SPL, Solaris Porting Layer.
9eda14cbcSMatt Macy  *
10eda14cbcSMatt Macy  *  The SPL is free software; you can redistribute it and/or modify it
11eda14cbcSMatt Macy  *  under the terms of the GNU General Public License as published by the
12eda14cbcSMatt Macy  *  Free Software Foundation; either version 2 of the License, or (at your
13eda14cbcSMatt Macy  *  option) any later version.
14eda14cbcSMatt Macy  *
15eda14cbcSMatt Macy  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16eda14cbcSMatt Macy  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17eda14cbcSMatt Macy  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18eda14cbcSMatt Macy  *  for more details.
19eda14cbcSMatt Macy  *
20eda14cbcSMatt Macy  *  You should have received a copy of the GNU General Public License along
21eda14cbcSMatt Macy  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22eda14cbcSMatt Macy  *
23eda14cbcSMatt Macy  *  Solaris Porting Layer (SPL) Credential Implementation.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy #include <sys/cred.h>
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy static int
cr_groups_search(const struct group_info * group_info,kgid_t grp)29eda14cbcSMatt Macy cr_groups_search(const struct group_info *group_info, kgid_t grp)
30eda14cbcSMatt Macy {
31eda14cbcSMatt Macy 	unsigned int left, right, mid;
32eda14cbcSMatt Macy 	int cmp;
33eda14cbcSMatt Macy 
34eda14cbcSMatt Macy 	if (!group_info)
35eda14cbcSMatt Macy 		return (0);
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy 	left = 0;
38eda14cbcSMatt Macy 	right = group_info->ngroups;
39eda14cbcSMatt Macy 	while (left < right) {
40eda14cbcSMatt Macy 		mid = (left + right) / 2;
41eda14cbcSMatt Macy 		cmp = KGID_TO_SGID(grp) -
42eda14cbcSMatt Macy 		    KGID_TO_SGID(GROUP_AT(group_info, mid));
43eda14cbcSMatt Macy 
44eda14cbcSMatt Macy 		if (cmp > 0)
45eda14cbcSMatt Macy 			left = mid + 1;
46eda14cbcSMatt Macy 		else if (cmp < 0)
47eda14cbcSMatt Macy 			right = mid;
48eda14cbcSMatt Macy 		else
49eda14cbcSMatt Macy 			return (1);
50eda14cbcSMatt Macy 	}
51eda14cbcSMatt Macy 	return (0);
52eda14cbcSMatt Macy }
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy /* Hold a reference on the credential */
55eda14cbcSMatt Macy void
crhold(cred_t * cr)56eda14cbcSMatt Macy crhold(cred_t *cr)
57eda14cbcSMatt Macy {
58eda14cbcSMatt Macy 	(void) get_cred((const cred_t *)cr);
59eda14cbcSMatt Macy }
60eda14cbcSMatt Macy 
61eda14cbcSMatt Macy /* Free a reference on the credential */
62eda14cbcSMatt Macy void
crfree(cred_t * cr)63eda14cbcSMatt Macy crfree(cred_t *cr)
64eda14cbcSMatt Macy {
65eda14cbcSMatt Macy 	put_cred((const cred_t *)cr);
66eda14cbcSMatt Macy }
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy /* Return the number of supplemental groups */
69eda14cbcSMatt Macy int
crgetngroups(const cred_t * cr)70eda14cbcSMatt Macy crgetngroups(const cred_t *cr)
71eda14cbcSMatt Macy {
72eda14cbcSMatt Macy 	struct group_info *gi;
73eda14cbcSMatt Macy 	int rc;
74eda14cbcSMatt Macy 
75eda14cbcSMatt Macy 	gi = cr->group_info;
76eda14cbcSMatt Macy 	rc = gi->ngroups;
77eda14cbcSMatt Macy #ifndef HAVE_GROUP_INFO_GID
78eda14cbcSMatt Macy 	/*
79eda14cbcSMatt Macy 	 * For Linux <= 4.8,
80eda14cbcSMatt Macy 	 * crgetgroups will only returns gi->blocks[0], which contains only
81eda14cbcSMatt Macy 	 * the first NGROUPS_PER_BLOCK groups.
82eda14cbcSMatt Macy 	 */
83eda14cbcSMatt Macy 	if (rc > NGROUPS_PER_BLOCK) {
84eda14cbcSMatt Macy 		WARN_ON_ONCE(1);
85eda14cbcSMatt Macy 		rc = NGROUPS_PER_BLOCK;
86eda14cbcSMatt Macy 	}
87eda14cbcSMatt Macy #endif
88eda14cbcSMatt Macy 	return (rc);
89eda14cbcSMatt Macy }
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy /*
92eda14cbcSMatt Macy  * Return an array of supplemental gids.  The returned address is safe
93eda14cbcSMatt Macy  * to use as long as the caller has taken a reference with crhold().
94eda14cbcSMatt Macy  *
95eda14cbcSMatt Macy  * Linux 4.9 API change, group_info changed from 2d array via ->blocks to 1d
96eda14cbcSMatt Macy  * array via ->gid.
97eda14cbcSMatt Macy  */
98eda14cbcSMatt Macy gid_t *
crgetgroups(const cred_t * cr)99eda14cbcSMatt Macy crgetgroups(const cred_t *cr)
100eda14cbcSMatt Macy {
101eda14cbcSMatt Macy 	struct group_info *gi;
102eda14cbcSMatt Macy 	gid_t *gids = NULL;
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 	gi = cr->group_info;
105eda14cbcSMatt Macy #ifdef HAVE_GROUP_INFO_GID
106eda14cbcSMatt Macy 	gids = KGIDP_TO_SGIDP(gi->gid);
107eda14cbcSMatt Macy #else
108eda14cbcSMatt Macy 	if (gi->nblocks > 0)
109eda14cbcSMatt Macy 		gids = KGIDP_TO_SGIDP(gi->blocks[0]);
110eda14cbcSMatt Macy #endif
111eda14cbcSMatt Macy 	return (gids);
112eda14cbcSMatt Macy }
113eda14cbcSMatt Macy 
114eda14cbcSMatt Macy /* Check if the passed gid is available in supplied credential. */
115eda14cbcSMatt Macy int
groupmember(gid_t gid,const cred_t * cr)116eda14cbcSMatt Macy groupmember(gid_t gid, const cred_t *cr)
117eda14cbcSMatt Macy {
118eda14cbcSMatt Macy 	struct group_info *gi;
119eda14cbcSMatt Macy 	int rc;
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy 	gi = cr->group_info;
122eda14cbcSMatt Macy 	rc = cr_groups_search(gi, SGID_TO_KGID(gid));
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy 	return (rc);
125eda14cbcSMatt Macy }
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy /* Return the effective user id */
128eda14cbcSMatt Macy uid_t
crgetuid(const cred_t * cr)129eda14cbcSMatt Macy crgetuid(const cred_t *cr)
130eda14cbcSMatt Macy {
131da5137abSMartin Matuska 	return (KUID_TO_SUID(cr->fsuid));
132eda14cbcSMatt Macy }
133eda14cbcSMatt Macy 
134eda14cbcSMatt Macy /* Return the real user id */
135eda14cbcSMatt Macy uid_t
crgetruid(const cred_t * cr)136eda14cbcSMatt Macy crgetruid(const cred_t *cr)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy 	return (KUID_TO_SUID(cr->uid));
139eda14cbcSMatt Macy }
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy /* Return the effective group id */
142eda14cbcSMatt Macy gid_t
crgetgid(const cred_t * cr)143eda14cbcSMatt Macy crgetgid(const cred_t *cr)
144eda14cbcSMatt Macy {
145eda14cbcSMatt Macy 	return (KGID_TO_SGID(cr->fsgid));
146eda14cbcSMatt Macy }
147eda14cbcSMatt Macy 
148*d411c1d6SMartin Matuska /* Return the initial user ns or nop_mnt_idmap */
149*d411c1d6SMartin Matuska zidmap_t *
zfs_get_init_idmap(void)150*d411c1d6SMartin Matuska zfs_get_init_idmap(void)
151*d411c1d6SMartin Matuska {
152*d411c1d6SMartin Matuska #ifdef HAVE_IOPS_CREATE_IDMAP
153*d411c1d6SMartin Matuska 	return ((zidmap_t *)&nop_mnt_idmap);
154*d411c1d6SMartin Matuska #else
155*d411c1d6SMartin Matuska 	return ((zidmap_t *)&init_user_ns);
156*d411c1d6SMartin Matuska #endif
157*d411c1d6SMartin Matuska }
158*d411c1d6SMartin Matuska 
159*d411c1d6SMartin Matuska EXPORT_SYMBOL(zfs_get_init_idmap);
160eda14cbcSMatt Macy EXPORT_SYMBOL(crhold);
161eda14cbcSMatt Macy EXPORT_SYMBOL(crfree);
162eda14cbcSMatt Macy EXPORT_SYMBOL(crgetuid);
163eda14cbcSMatt Macy EXPORT_SYMBOL(crgetruid);
164eda14cbcSMatt Macy EXPORT_SYMBOL(crgetgid);
165eda14cbcSMatt Macy EXPORT_SYMBOL(crgetngroups);
166eda14cbcSMatt Macy EXPORT_SYMBOL(crgetgroups);
167eda14cbcSMatt Macy EXPORT_SYMBOL(groupmember);
168