kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / usr / src / lib / libc / port / gen / ucred.c
blob85b998e8c403929864b226e7b68f100b7f219895
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #pragma weak _ucred_free = ucred_free
29 #pragma weak _ucred_get = ucred_get
30 #pragma weak _ucred_getegid = ucred_getegid
31 #pragma weak _ucred_geteuid = ucred_geteuid
32 #pragma weak _ucred_getgroups = ucred_getgroups
33 #pragma weak _ucred_getpflags = ucred_getpflags
34 #pragma weak _ucred_getpid = ucred_getpid
35 #pragma weak _ucred_getzoneid = ucred_getzoneid
36 #pragma weak _ucred_getprojid = ucred_getprojid
37 #pragma weak _ucred_getprivset = ucred_getprivset
38 #pragma weak _ucred_getrgid = ucred_getrgid
39 #pragma weak _ucred_getruid = ucred_getruid
40 #pragma weak _ucred_getsgid = ucred_getsgid
41 #pragma weak _ucred_getsuid = ucred_getsuid
42 #pragma weak _ucred_getauid = ucred_getauid
43 #pragma weak _ucred_getasid = ucred_getasid
44 #pragma weak _ucred_getatid = ucred_getatid
45 #pragma weak _ucred_getamask = ucred_getamask
46 #pragma weak _ucred_size = ucred_size
48 #include "lint.h"
50 #define _STRUCTURED_PROC 1
52 #include "priv_private.h"
53 #include <errno.h>
54 #include <priv.h>
55 #include <stdarg.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <unistd.h>
59 #include <ucred.h>
60 #include <limits.h>
61 #include <fcntl.h>
62 #include <door.h>
63 #include <alloca.h>
64 #include <sys/ucred.h>
65 #include <sys/procfs.h>
66 #include <sys/sysmacros.h>
67 #include <sys/zone.h>
69 ucred_t *
70 _ucred_alloc(void)
72 ucred_t *r;
73 size_t sz = ucred_size();
75 r = malloc(sz);
77 if (r != NULL)
78 r->uc_size = (uint32_t)sz;
80 return (r);
83 void
84 ucred_free(ucred_t *uc)
86 free(uc);
90 ucred_t *
91 ucred_get(pid_t pid)
93 ucred_t *uc;
95 uc = _ucred_alloc();
97 if (uc == NULL)
98 return (NULL);
100 if (syscall(SYS_ucredsys, UCREDSYS_UCREDGET, pid, uc) != 0) {
101 ucred_free(uc);
102 return (NULL);
105 return (uc);
108 uid_t
109 ucred_geteuid(const ucred_t *uc)
111 /* LINTED: alignment */
112 const prcred_t *cr = UCCRED(uc);
114 if (cr == NULL) {
115 errno = EINVAL;
116 return ((uid_t)-1);
119 return (cr->pr_euid);
122 uid_t
123 ucred_getruid(const ucred_t *uc)
125 /* LINTED: alignment */
126 const prcred_t *cr = UCCRED(uc);
128 if (cr == NULL) {
129 errno = EINVAL;
130 return ((uid_t)-1);
133 return (cr->pr_ruid);
136 uid_t
137 ucred_getsuid(const ucred_t *uc)
139 /* LINTED: alignment */
140 const prcred_t *cr = UCCRED(uc);
142 if (cr == NULL) {
143 errno = EINVAL;
144 return ((uid_t)-1);
147 return (cr->pr_suid);
150 gid_t
151 ucred_getegid(const ucred_t *uc)
153 /* LINTED: alignment */
154 const prcred_t *cr = UCCRED(uc);
156 if (cr == NULL) {
157 errno = EINVAL;
158 return ((gid_t)-1);
161 return (cr->pr_egid);
164 gid_t
165 ucred_getrgid(const ucred_t *uc)
167 /* LINTED: alignment */
168 const prcred_t *cr = UCCRED(uc);
170 if (cr == NULL) {
171 errno = EINVAL;
172 return ((gid_t)-1);
175 return (cr->pr_rgid);
178 gid_t
179 ucred_getsgid(const ucred_t *uc)
181 /* LINTED: alignment */
182 const prcred_t *cr = UCCRED(uc);
184 if (cr == NULL) {
185 errno = EINVAL;
186 return ((gid_t)-1);
189 return (cr->pr_sgid);
193 ucred_getgroups(const ucred_t *uc, const gid_t **grps)
195 /* LINTED: alignment */
196 const prcred_t *cr = UCCRED(uc);
198 if (cr == NULL) {
199 errno = EINVAL;
200 return (-1);
203 if (cr->pr_ngroups > 0)
204 *grps = &cr->pr_groups[0];
205 else
206 *grps = NULL;
208 return (cr->pr_ngroups);
211 const priv_set_t *
212 ucred_getprivset(const ucred_t *uc, priv_ptype_t set)
214 /* LINTED: alignment */
215 const prpriv_t *pr = UCPRIV(uc);
216 int pset = priv_getsetbyname(set);
217 priv_data_t *d;
219 if (pr == NULL || pset == -1) {
220 errno = EINVAL;
221 return (NULL);
224 LOADPRIVDATA(d);
226 return ((const priv_set_t *)
227 &pr->pr_sets[d->pd_pinfo->priv_setsize * pset]);
230 pid_t
231 ucred_getpid(const ucred_t *uc)
234 if (uc->uc_pid == -1)
235 errno = EINVAL;
237 return (uc->uc_pid);
240 projid_t
241 ucred_getprojid(const ucred_t *uc)
244 if (uc->uc_projid == -1)
245 errno = EINVAL;
247 return (uc->uc_projid);
250 zoneid_t
251 ucred_getzoneid(const ucred_t *uc)
254 if (uc->uc_zoneid < MIN_ZONEID || uc->uc_zoneid > MAX_ZONEID) {
255 errno = EINVAL;
256 return (-1);
259 return (uc->uc_zoneid);
263 * For now, assume single bit flags.
265 uint_t
266 ucred_getpflags(const ucred_t *uc, uint_t flag)
268 /* LINTED: alignment */
269 prpriv_t *pr = UCPRIV(uc);
270 char *x, *end;
272 if (pr == NULL) {
273 errno = EINVAL;
274 return ((uint_t)-1);
277 end = (char *)pr + PRIV_PRPRIV_SIZE(pr);
278 x = end - pr->pr_infosize;
280 while (x < end) {
281 /* LINTED: alignment */
282 priv_info_t *pi = (priv_info_t *)x;
283 priv_info_uint_t *pii;
285 switch (pi->priv_info_type) {
286 case PRIV_INFO_FLAGS:
287 /* LINTED: alignment */
288 pii = (priv_info_uint_t *)x;
289 return ((pii->val & flag) ? 1 : 0);
291 /* Forward progress */
292 if (pi->priv_info_size < sizeof (priv_info_t))
293 break;
294 x += pi->priv_info_size;
297 errno = EINVAL;
298 return ((uint_t)-1);
301 au_id_t
302 ucred_getauid(const ucred_t *uc)
304 /* LINTED: alignment */
305 const auditinfo64_addr_t *ainfo = UCAUD(uc);
307 if (ainfo == NULL)
308 return (AU_NOAUDITID);
310 return (ainfo->ai_auid);
313 au_asid_t
314 ucred_getasid(const ucred_t *uc)
316 /* LINTED: alignment */
317 const auditinfo64_addr_t *ainfo = UCAUD(uc);
319 if (ainfo == NULL)
320 return ((au_asid_t)-1);
322 return (ainfo->ai_asid);
325 const au_tid64_addr_t *
326 ucred_getatid(const ucred_t *uc)
328 /* LINTED: alignment */
329 const auditinfo64_addr_t *ainfo = UCAUD(uc);
331 if (ainfo == NULL) {
332 errno = EINVAL;
333 return (NULL);
336 return (&ainfo->ai_termid);
339 const au_mask_t *
340 ucred_getamask(const ucred_t *uc)
342 /* LINTED: alignment */
343 const auditinfo64_addr_t *ainfo = UCAUD(uc);
345 if (ainfo == NULL) {
346 errno = EINVAL;
347 return (NULL);
350 return (&ainfo->ai_mask);
353 size_t
354 ucred_size(void)
356 priv_data_t *d;
358 LOADPRIVDATA(d);
360 return (d->pd_ucredsize);