1/*	$NetBSD: cred.h,v 1.4 2010/03/13 22:31:15 christos 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: src/sys/compat/opensolaris/sys/cred.h,v 1.1 2007/04/06 01:09:06 pjd Exp $
29 */
30
31#ifndef _OPENSOLARIS_SYS_CRED_H_
32#define	_OPENSOLARIS_SYS_CRED_H_
33
34#include <sys/param.h>
35#include <sys/types.h>
36
37#ifdef _KERNEL
38#include <sys/kauth.h>
39
40#define	CRED()		(kauth_cred_get())
41#define KCRED()		(cred0)
42#define	kcred		cred0
43
44extern kauth_cred_t	cred0;
45
46#define	crget(cr)		cr = kauth_cred_get()
47#define	crgetuid(cr)		kauth_cred_getuid(cr)
48#define	crgetgid(cr)		kauth_cred_getgid(cr)
49#define	crgetngroups(cr) 	kauth_cred_ngroups(cr)
50#define	cralloc()		kauth_cred_alloc()
51#define	crfree(cr)		kauth_cred_free(cr)
52#define	crsetugid(cr, u, g)	( \
53	kauth_cred_setuid(cr, u), \
54	kauth_cred_setgid(cr, g), \
55	kauth_cred_seteuid(cr, u), \
56	kauth_cred_setegid(cr, g), \
57	kauth_cred_setsvuid(cr, u), \
58	kauth_cred_setsvgid(cr, g), 0)
59#define	crsetgroups(cr, gc, ga)	\
60    kauth_cred_setgroups(cr, ga, gc, 0, UIO_SYSSPACE)
61#define crgetsid(cr, i) (NULL)
62
63static __inline gid_t *
64crgetgroups(cred_t *cr)
65{
66	static gid_t gids[NGROUPS_MAX];
67
68	memset(gids, 0, NGROUPS_MAX);
69	if (kauth_cred_getgroups(cr, gids, NGROUPS_MAX, UIO_SYSSPACE) != 0)
70		return NULL;
71
72	return gids;
73}
74
75static __inline int
76groupmember(gid_t gid, cred_t *cr)
77{
78  int result;
79
80  kauth_cred_ismember_gid(cr, gid, &result);
81
82  return result;
83}
84
85#endif	/* _KERNEL */
86
87#endif	/* _OPENSOLARIS_SYS_CRED_H_ */
88