xref: /illumos-gate/usr/src/uts/common/sys/sid.h (revision f37b3cbb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright 2020 Tintri by DDN, Inc. All rights reserved.
27  */
28 
29 #ifndef _SYS_SID_H
30 #define	_SYS_SID_H
31 
32 #include <sys/types.h>
33 #include <sys/avl.h>
34 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
35 #include <sys/zone.h>
36 #endif
37 
38 /*
39  * Kernel SID data structure and functions.
40  */
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /* sidsys subcodes */
46 #define	SIDSYS_ALLOC_IDS	0
47 /* Flags for ALLOC_IDS */
48 #define	SID_EXTEND_RANGE	0
49 #define	SID_NEW_RANGE		1
50 
51 #define	SIDSYS_IDMAP_REG	1
52 #define	SIDSYS_IDMAP_UNREG	2
53 #define	SIDSYS_IDMAP_FLUSH_KCACHE 3
54 
55 #define	SIDSYS_SID2ID	0
56 #define	SIDSYS_ID2SID	1
57 
58 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
59 #define	KSIDLIST_MEM(n)	(sizeof (ksidlist_t) + ((n) - 1) * sizeof (ksid_t))
60 
61 /* Domains are stored in AVL trees so we can share them among SIDs */
62 typedef struct ksiddomain {
63 	uint_t		kd_ref;
64 	uint_t		kd_len;
65 	char		*kd_name;	/* Domain part of SID */
66 	avl_node_t	kd_link;
67 } ksiddomain_t;
68 
69 typedef struct ksid {
70 	uid_t		ks_id;		/* Cache of (ephemeral) uid */
71 	uint32_t	ks_rid;		/* Rid part of the name */
72 	uint32_t	ks_attr;	/* Attribute */
73 	ksiddomain_t	*ks_domain;	/* Domain descsriptor */
74 } ksid_t;
75 
76 typedef enum ksid_index {
77 	KSID_USER,
78 	KSID_GROUP,
79 	KSID_OWNER,
80 	KSID_COUNT			/* Must be last */
81 } ksid_index_t;
82 
83 /*
84  * As no memory may be allocated for credentials while holding p_crlock,
85  * all sub data structures need to be ref counted.
86  */
87 
88 typedef struct ksidlist {
89 	uint_t		ksl_ref;
90 	uint_t		ksl_nsid;
91 	uint_t		ksl_neid;	/* Number of ids which are ephemeral */
92 	ksid_t		**ksl_sorted;	/* ksl_sids sorted by ID */
93 	ksid_t		ksl_sids[1];	/* Allocate ksl_nsid times */
94 } ksidlist_t;
95 
96 typedef struct credsid {
97 	uint_t		kr_ref;			/* Reference count */
98 	ksid_t		kr_sidx[KSID_COUNT];	/* User, group, default owner */
99 	ksidlist_t	*kr_sidlist;		/* List of SIDS */
100 } credsid_t;
101 
102 const char *ksid_getdomain(ksid_t *);
103 uint_t ksid_getrid(ksid_t *);
104 uid_t ksid_getid(ksid_t *);
105 
106 int ksid_lookupbyuid(zone_t *, uid_t, ksid_t *);
107 int ksid_lookupbygid(zone_t *, gid_t, ksid_t *);
108 void ksid_rele(ksid_t *);
109 
110 credsid_t *kcrsid_alloc(void);
111 
112 credsid_t *kcrsid_setsid(credsid_t *, ksid_t *, ksid_index_t);
113 credsid_t *kcrsid_setsidlist(credsid_t *, ksidlist_t *);
114 
115 void kcrsid_rele(credsid_t *);
116 void kcrsid_hold(credsid_t *);
117 void kcrsidcopy_to(const credsid_t *okcr, credsid_t *nkcr);
118 
119 void ksiddomain_rele(ksiddomain_t *);
120 void ksiddomain_hold(ksiddomain_t *);
121 void ksidlist_rele(ksidlist_t *);
122 void ksidlist_hold(ksidlist_t *);
123 boolean_t ksidlist_has_sid(ksidlist_t *, const char *, uint32_t);
124 boolean_t ksidlist_has_pid(ksidlist_t *, uint32_t);
125 
126 ksiddomain_t *ksid_lookupdomain(const char *);
127 
128 ksidlist_t *kcrsid_gidstosids(zone_t *, int, gid_t *);
129 
130 #else /* _KERNEL */
131 
132 int allocids(int, int, uid_t *, int, gid_t *);
133 int __idmap_reg(int);
134 int __idmap_unreg(int);
135 int __idmap_flush_kcache(void);
136 
137 #endif /* _KERNEL */
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif /* _SYS_SID_H */
144