1/*	$NetBSD: creds.c,v 1.14 2007/12/08 19:57:02 pooka Exp $	*/
2
3/*
4 * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
5 *
6 * Development of this software was supported by the Ulla Tuominen Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31#if !defined(lint)
32__RCSID("$NetBSD: creds.c,v 1.14 2007/12/08 19:57:02 pooka Exp $");
33#endif /* !lint */
34
35/*
36 * Interface for dealing with credits.
37 */
38
39#include <sys/types.h>
40#include <sys/param.h>
41
42#include <errno.h>
43#include <puffs.h>
44#include <stdbool.h>
45#include <string.h>
46
47#include "puffs_priv.h"
48
49#define UUCCRED(a) (a->pkcr_type == PUFFCRED_TYPE_UUC)
50#define INTCRED(a) (a->pkcr_type == PUFFCRED_TYPE_INTERNAL)
51
52int
53puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *ruid)
54{
55	PUFFS_MAKEKCRED(pkcr, pcr);
56
57	if (!UUCCRED(pkcr)) {
58		errno = EOPNOTSUPP;
59		return -1;
60	}
61	*ruid = pkcr->pkcr_uuc.cr_uid;
62
63	return 0;
64}
65
66int
67puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *rgid)
68{
69	PUFFS_MAKEKCRED(pkcr, pcr);
70
71	if (!UUCCRED(pkcr)) {
72		errno = EOPNOTSUPP;
73		return -1;
74	}
75	*rgid = pkcr->pkcr_uuc.cr_gid;
76
77	return 0;
78}
79
80int
81puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *rgids, short *ngids)
82{
83	PUFFS_MAKEKCRED(pkcr, pcr);
84	size_t ncopy;
85
86	if (!UUCCRED(pkcr)) {
87		errno = EOPNOTSUPP;
88		*ngids = 0;
89		return -1;
90	}
91
92	ncopy = MIN(*ngids, pkcr->pkcr_uuc.cr_ngroups);
93	(void)memcpy(rgids, pkcr->pkcr_uuc.cr_groups, sizeof(gid_t) * ncopy);
94	*ngids = (short)ncopy;
95
96	return 0;
97}
98
99bool
100puffs_cred_isuid(const struct puffs_cred *pcr, uid_t uid)
101{
102	PUFFS_MAKEKCRED(pkcr, pcr);
103
104	return UUCCRED(pkcr) && pkcr->pkcr_uuc.cr_uid == uid;
105}
106
107bool
108puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t gid)
109{
110	PUFFS_MAKEKCRED(pkcr, pcr);
111	short i;
112
113	if (!UUCCRED(pkcr))
114		return false;
115
116	if (pkcr->pkcr_uuc.cr_gid == gid)
117		return true;
118	for (i = 0; i < pkcr->pkcr_uuc.cr_ngroups; i++)
119		if (pkcr->pkcr_uuc.cr_groups[i] == gid)
120			return true;
121
122	return false;
123}
124
125bool
126puffs_cred_isregular(const struct puffs_cred *pcr)
127{
128	PUFFS_MAKEKCRED(pkcr, pcr);
129
130	return UUCCRED(pkcr);
131}
132
133bool
134puffs_cred_iskernel(const struct puffs_cred *pcr)
135{
136	PUFFS_MAKEKCRED(pkcr, pcr);
137
138	return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_NOCRED;
139}
140
141bool
142puffs_cred_isfs(const struct puffs_cred *pcr)
143{
144	PUFFS_MAKEKCRED(pkcr, pcr);
145
146	return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_FSCRED;
147}
148
149bool
150puffs_cred_isjuggernaut(const struct puffs_cred *pcr)
151{
152
153	return puffs_cred_isuid(pcr, 0) || puffs_cred_iskernel(pcr)
154	    || puffs_cred_isfs(pcr);
155}
156
157/*
158 * Generic routine for checking file access rights.  Modeled after
159 * vaccess() in the kernel.
160 */
161int
162puffs_access(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
163	mode_t acc_mode, const struct puffs_cred *pcr)
164{
165	mode_t mask;
166
167	/* megapower */
168	if (puffs_cred_iskernel(pcr) || puffs_cred_isfs(pcr))
169		return 0;
170
171	/* superuser, allow all except exec if *ALL* exec bits are unset */
172	if (puffs_cred_isuid(pcr, 0)) {
173		if ((acc_mode & PUFFS_VEXEC) && type != VDIR &&
174		    (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
175			return EACCES;
176		return 0;
177	}
178
179	mask = 0;
180	/* owner */
181	if (puffs_cred_isuid(pcr, uid)) {
182		if (acc_mode & PUFFS_VEXEC)
183			mask |= S_IXUSR;
184		if (acc_mode & PUFFS_VREAD)
185			mask |= S_IRUSR;
186		if (acc_mode & PUFFS_VWRITE)
187			mask |= S_IWUSR;
188	/* group */
189	} else if (puffs_cred_hasgroup(pcr, gid)) {
190		if (acc_mode & PUFFS_VEXEC)
191			mask |= S_IXGRP;
192		if (acc_mode & PUFFS_VREAD)
193			mask |= S_IRGRP;
194		if (acc_mode & PUFFS_VWRITE)
195			mask |= S_IWGRP;
196	/* other */
197	} else {
198		if (acc_mode & PUFFS_VEXEC)
199			mask |= S_IXOTH;
200		if (acc_mode & PUFFS_VREAD)
201			mask |= S_IROTH;
202		if (acc_mode & PUFFS_VWRITE)
203			mask |= S_IWOTH;
204	}
205
206	if ((file_mode & mask) == mask)
207		return 0;
208	else
209		return EACCES;
210}
211
212int
213puffs_access_chown(uid_t owner, gid_t group, uid_t newowner, gid_t newgroup,
214	const struct puffs_cred *pcr)
215{
216
217	if (newowner == (uid_t)PUFFS_VNOVAL)
218		newowner = owner;
219	if (newgroup == (gid_t)PUFFS_VNOVAL)
220		newgroup = group;
221
222	if ((!puffs_cred_isuid(pcr, owner) || newowner != owner ||
223	    ((newgroup != group && !puffs_cred_hasgroup(pcr, newgroup))))
224	    && !puffs_cred_isjuggernaut(pcr))
225		return EPERM;
226
227	return 0;
228}
229
230int
231puffs_access_chmod(uid_t owner, gid_t group, enum vtype type, mode_t mode,
232	const struct puffs_cred *pcr)
233{
234
235	if (!puffs_cred_isuid(pcr, owner) && !puffs_cred_isjuggernaut(pcr))
236		return EPERM;
237
238	if (!puffs_cred_isjuggernaut(pcr)) {
239		if (type != VDIR && (mode & S_ISTXT))
240			return EFTYPE;
241		if (!puffs_cred_hasgroup(pcr, group) && (mode & S_ISGID))
242			return EPERM;
243	}
244
245	return 0;
246}
247
248int
249puffs_access_times(uid_t uid, gid_t gid, mode_t mode, int va_utimes_null,
250	const struct puffs_cred *pcr)
251{
252
253	if (!puffs_cred_isuid(pcr, uid) && !puffs_cred_isjuggernaut(pcr)
254	    && (va_utimes_null == 0
255	      || puffs_access(VNON, mode, uid, gid, PUFFS_VWRITE, pcr) != 0))
256		return EPERM;
257
258	return 0;
259}
260