summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/ucred.c
blob: 69dfa3c71b27f70a0dfe24558b6fde755453c2b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


#pragma weak _ucred_free = ucred_free
#pragma weak _ucred_get = ucred_get
#pragma weak _ucred_getegid = ucred_getegid
#pragma weak _ucred_geteuid = ucred_geteuid
#pragma weak _ucred_getgroups = ucred_getgroups
#pragma weak _ucred_getpflags = ucred_getpflags
#pragma weak _ucred_getpid = ucred_getpid
#pragma weak _ucred_getzoneid = ucred_getzoneid
#pragma weak _ucred_getprojid = ucred_getprojid
#pragma weak _ucred_getprivset = ucred_getprivset
#pragma weak _ucred_getrgid = ucred_getrgid
#pragma weak _ucred_getruid = ucred_getruid
#pragma weak _ucred_getsgid = ucred_getsgid
#pragma weak _ucred_getsuid = ucred_getsuid
#pragma weak _ucred_getauid = ucred_getauid
#pragma weak _ucred_getasid = ucred_getasid
#pragma weak _ucred_getatid = ucred_getatid
#pragma weak _ucred_getlabel = ucred_getlabel
#pragma weak _ucred_getamask = ucred_getamask
#pragma weak _ucred_size = ucred_size

#include "lint.h"

#define	_STRUCTURED_PROC	1

#include "priv_private.h"
#include <errno.h>
#include <priv.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ucred.h>
#include <limits.h>
#include <fcntl.h>
#include <door.h>
#include <alloca.h>
#include <sys/ucred.h>
#include <sys/procfs.h>
#include <sys/sysmacros.h>
#include <sys/zone.h>
#include <tsol/label.h>

ucred_t *
_ucred_alloc(void)
{
	ucred_t *r;
	size_t sz = ucred_size();

	r = malloc(sz);

	if (r != NULL)
		r->uc_size = (uint32_t)sz;

	return (r);
}

void
ucred_free(ucred_t *uc)
{
	free(uc);
}


ucred_t *
ucred_get(pid_t pid)
{
	ucred_t *uc;

	uc = _ucred_alloc();

	if (uc == NULL)
		return (NULL);

	if (syscall(SYS_ucredsys, UCREDSYS_UCREDGET, pid, uc) != 0) {
		ucred_free(uc);
		return (NULL);
	}

	return (uc);
}

uid_t
ucred_geteuid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const prcred_t *cr = UCCRED(uc);

	if (cr == NULL) {
		errno = EINVAL;
		return ((uid_t)-1);
	}

	return (cr->pr_euid);
}

uid_t
ucred_getruid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const prcred_t *cr = UCCRED(uc);

	if (cr == NULL) {
		errno = EINVAL;
		return ((uid_t)-1);
	}

	return (cr->pr_ruid);
}

uid_t
ucred_getsuid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const prcred_t *cr = UCCRED(uc);

	if (cr == NULL) {
		errno = EINVAL;
		return ((uid_t)-1);
	}

	return (cr->pr_suid);
}

gid_t
ucred_getegid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const prcred_t *cr = UCCRED(uc);

	if (cr == NULL) {
		errno = EINVAL;
		return ((gid_t)-1);
	}

	return (cr->pr_egid);
}

gid_t
ucred_getrgid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const prcred_t *cr = UCCRED(uc);

	if (cr == NULL) {
		errno = EINVAL;
		return ((gid_t)-1);
	}

	return (cr->pr_rgid);
}

gid_t
ucred_getsgid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const prcred_t *cr = UCCRED(uc);

	if (cr == NULL) {
		errno = EINVAL;
		return ((gid_t)-1);
	}

	return (cr->pr_sgid);
}

int
ucred_getgroups(const ucred_t *uc, const gid_t **grps)
{
	/* LINTED: alignment */
	const prcred_t *cr = UCCRED(uc);

	if (cr == NULL) {
		errno = EINVAL;
		return (-1);
	}

	if (cr->pr_ngroups > 0)
		*grps = &cr->pr_groups[0];
	else
		*grps = NULL;

	return (cr->pr_ngroups);
}

const priv_set_t *
ucred_getprivset(const ucred_t *uc, priv_ptype_t set)
{
	/* LINTED: alignment */
	const prpriv_t *pr = UCPRIV(uc);
	int pset = priv_getsetbyname(set);
	priv_data_t *d;

	if (pr == NULL || pset == -1) {
		errno = EINVAL;
		return (NULL);
	}

	LOADPRIVDATA(d);

	return ((const priv_set_t *)
	    &pr->pr_sets[d->pd_pinfo->priv_setsize * pset]);
}

pid_t
ucred_getpid(const ucred_t *uc)
{

	if (uc->uc_pid == -1)
		errno = EINVAL;

	return (uc->uc_pid);
}

projid_t
ucred_getprojid(const ucred_t *uc)
{

	if (uc->uc_projid == -1)
		errno = EINVAL;

	return (uc->uc_projid);
}

zoneid_t
ucred_getzoneid(const ucred_t *uc)
{

	if (uc->uc_zoneid < MIN_ZONEID || uc->uc_zoneid > MAX_ZONEID) {
		errno = EINVAL;
		return (-1);
	}

	return (uc->uc_zoneid);
}

bslabel_t *
ucred_getlabel(const ucred_t *uc)
{
	/* LINTED: alignment */
	bslabel_t *slabel = UCLABEL(uc);

	if (!is_system_labeled() || slabel == NULL) {
		errno = EINVAL;
		return (NULL);
	}

	return (slabel);
}

/*
 * For now, assume single bit flags.
 */
uint_t
ucred_getpflags(const ucred_t *uc, uint_t flag)
{
	/* LINTED: alignment */
	prpriv_t *pr = UCPRIV(uc);
	char *x, *end;

	if (pr == NULL) {
		errno = EINVAL;
		return ((uint_t)-1);
	}

	end = (char *)pr + PRIV_PRPRIV_SIZE(pr);
	x = end - pr->pr_infosize;

	while (x < end) {
		/* LINTED: alignment */
		priv_info_t *pi = (priv_info_t *)x;
		priv_info_uint_t *pii;

		switch (pi->priv_info_type) {
		case PRIV_INFO_FLAGS:
			/* LINTED: alignment */
			pii = (priv_info_uint_t *)x;
			return ((pii->val & flag) ? 1 : 0);
		}
		/* Forward progress */
		if (pi->priv_info_size < sizeof (priv_info_t))
			break;
		x += pi->priv_info_size;
	}

	errno = EINVAL;
	return ((uint_t)-1);
}

au_id_t
ucred_getauid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const auditinfo64_addr_t *ainfo = UCAUD(uc);

	if (ainfo == NULL)
		return (AU_NOAUDITID);

	return (ainfo->ai_auid);
}

au_asid_t
ucred_getasid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const auditinfo64_addr_t *ainfo = UCAUD(uc);

	if (ainfo == NULL)
		return ((au_asid_t)-1);

	return (ainfo->ai_asid);
}

const au_tid64_addr_t *
ucred_getatid(const ucred_t *uc)
{
	/* LINTED: alignment */
	const auditinfo64_addr_t *ainfo = UCAUD(uc);

	if (ainfo == NULL) {
		errno = EINVAL;
		return (NULL);
	}

	return (&ainfo->ai_termid);
}

const au_mask_t *
ucred_getamask(const ucred_t *uc)
{
	/* LINTED: alignment */
	const auditinfo64_addr_t *ainfo = UCAUD(uc);

	if (ainfo == NULL) {
		errno = EINVAL;
		return (NULL);
	}

	return (&ainfo->ai_mask);
}

size_t
ucred_size(void)
{
	priv_data_t *d;

	LOADPRIVDATA(d);

	return (d->pd_ucredsize);
}