1 /* $NetBSD: cred.h,v 1.8 2020/10/12 08:21:21 hannken Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/cddl/compat/opensolaris/sys/cred.h 248571 2013-03-21 08:38:03Z mm $
29 */
30
31 #ifndef _OPENSOLARIS_SYS_CRED_H_
32 #define _OPENSOLARIS_SYS_CRED_H_
33
34 #ifdef _KERNEL
35 /* Needed for access to cr_groups. */
36 #define __KAUTH_PRIVATE
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41
42 #ifdef _KERNEL
43 #include <sys/kauth.h>
44
45 #define CRED() (kauth_cred_get())
46 #define kcred cred0
47
48 extern kauth_cred_t cred0;
49
50 #define crgetuid(cr) kauth_cred_geteuid(cr)
51 #define crgetruid(cr) kauth_cred_getuid(cr)
52 #define crgetgid(cr) kauth_cred_getegid(cr)
53 #define crgetrgid(cr) kauth_cred_getgid(cr)
54 #define crgetngroups(cr) kauth_cred_ngroups(cr)
55 #define cralloc() kauth_cred_alloc()
56 #define crhold(cr) kauth_cred_hold(cr)
57 #define crfree(cr) kauth_cred_free(cr)
58 #define crsetugid(cr, u, g) ( \
59 kauth_cred_setuid(cr, u), \
60 kauth_cred_setgid(cr, g), \
61 kauth_cred_seteuid(cr, u), \
62 kauth_cred_setegid(cr, g), \
63 kauth_cred_setsvuid(cr, u), \
64 kauth_cred_setsvgid(cr, g), 0)
65 #define crgetgroups(cr) ((cr)->cr_groups)
66 #define crsetgroups(cr, gc, ga) \
67 kauth_cred_setgroups(cr, ga, gc, 0, UIO_SYSSPACE)
68 #define crgetsid(cr, i) (NULL)
69
70 static __inline int
groupmember(gid_t gid,cred_t * cr)71 groupmember(gid_t gid, cred_t *cr)
72 {
73 int result, error;
74
75 error = kauth_cred_ismember_gid(cr, gid, &result);
76 if (error)
77 return 0;
78 return (kauth_cred_getegid(cr) == gid || result);
79 }
80
81 #endif /* _KERNEL */
82
83 #endif /* _OPENSOLARIS_SYS_CRED_H_ */
84