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 <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
10 * The SPL is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * The SPL is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 * Solaris Porting Layer (SPL) Credential Implementation.
29 cr_groups_search(const struct group_info
*group_info
, kgid_t grp
)
31 unsigned int left
, right
, mid
;
38 right
= group_info
->ngroups
;
39 while (left
< right
) {
40 mid
= (left
+ right
) / 2;
41 cmp
= KGID_TO_SGID(grp
) -
42 KGID_TO_SGID(GROUP_AT(group_info
, mid
));
54 /* Hold a reference on the credential */
58 (void) get_cred((const cred_t
*)cr
);
61 /* Free a reference on the credential */
65 put_cred((const cred_t
*)cr
);
68 /* Return the number of supplemental groups */
70 crgetngroups(const cred_t
*cr
)
72 struct group_info
*gi
;
77 #ifndef HAVE_GROUP_INFO_GID
80 * crgetgroups will only returns gi->blocks[0], which contains only
81 * the first NGROUPS_PER_BLOCK groups.
83 if (rc
> NGROUPS_PER_BLOCK
) {
85 rc
= NGROUPS_PER_BLOCK
;
92 * Return an array of supplemental gids. The returned address is safe
93 * to use as long as the caller has taken a reference with crhold().
95 * Linux 4.9 API change, group_info changed from 2d array via ->blocks to 1d
99 crgetgroups(const cred_t
*cr
)
101 struct group_info
*gi
;
105 #ifdef HAVE_GROUP_INFO_GID
106 gids
= KGIDP_TO_SGIDP(gi
->gid
);
109 gids
= KGIDP_TO_SGIDP(gi
->blocks
[0]);
114 /* Check if the passed gid is available in supplied credential. */
116 groupmember(gid_t gid
, const cred_t
*cr
)
118 struct group_info
*gi
;
122 rc
= cr_groups_search(gi
, SGID_TO_KGID(gid
));
127 /* Return the effective user id */
129 crgetuid(const cred_t
*cr
)
131 return (KUID_TO_SUID(cr
->fsuid
));
134 /* Return the real user id */
136 crgetruid(const cred_t
*cr
)
138 return (KUID_TO_SUID(cr
->uid
));
141 /* Return the effective group id */
143 crgetgid(const cred_t
*cr
)
145 return (KGID_TO_SGID(cr
->fsgid
));
148 /* Return the initial user ns or nop_mnt_idmap */
150 zfs_get_init_idmap(void)
152 #ifdef HAVE_IOPS_CREATE_IDMAP
153 return ((zidmap_t
*)&nop_mnt_idmap
);
155 return ((zidmap_t
*)&init_user_ns
);
159 EXPORT_SYMBOL(zfs_get_init_idmap
);
160 EXPORT_SYMBOL(crhold
);
161 EXPORT_SYMBOL(crfree
);
162 EXPORT_SYMBOL(crgetuid
);
163 EXPORT_SYMBOL(crgetruid
);
164 EXPORT_SYMBOL(crgetgid
);
165 EXPORT_SYMBOL(crgetngroups
);
166 EXPORT_SYMBOL(crgetgroups
);
167 EXPORT_SYMBOL(groupmember
);