creds.c revision 1.12.4.1 1 /* $NetBSD: creds.c,v 1.12.4.1 2007/11/06 23:11:50 matt 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.12.4.1 2007/11/06 23:11:50 matt 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
52 int
53 puffs_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
66 int
67 puffs_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
80 int
81 puffs_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 return -1;
89 }
90
91 ncopy = MIN(*ngids, NGROUPS);
92 (void)memcpy(rgids, pkcr->pkcr_uuc.cr_groups, sizeof(gid_t) * ncopy);
93 *ngids = (short)ncopy;
94
95 return 0;
96 }
97
98 bool
99 puffs_cred_isuid(const struct puffs_cred *pcr, uid_t uid)
100 {
101 PUFFS_MAKEKCRED(pkcr, pcr);
102
103 return UUCCRED(pkcr) && pkcr->pkcr_uuc.cr_uid == uid;
104 }
105
106 bool
107 puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t gid)
108 {
109 PUFFS_MAKEKCRED(pkcr, pcr);
110 short i;
111
112 if (!UUCCRED(pkcr))
113 return false;
114
115 if (pkcr->pkcr_uuc.cr_gid == gid)
116 return true;
117 for (i = 0; i < pkcr->pkcr_uuc.cr_ngroups; i++)
118 if (pkcr->pkcr_uuc.cr_groups[i] == gid)
119 return true;
120
121 return false;
122 }
123
124 bool
125 puffs_cred_isregular(const struct puffs_cred *pcr)
126 {
127 PUFFS_MAKEKCRED(pkcr, pcr);
128
129 return UUCCRED(pkcr);
130 }
131
132 bool
133 puffs_cred_iskernel(const struct puffs_cred *pcr)
134 {
135 PUFFS_MAKEKCRED(pkcr, pcr);
136
137 return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_NOCRED;
138 }
139
140 bool
141 puffs_cred_isfs(const struct puffs_cred *pcr)
142 {
143 PUFFS_MAKEKCRED(pkcr, pcr);
144
145 return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_FSCRED;
146 }
147
148 bool
149 puffs_cred_isjuggernaut(const struct puffs_cred *pcr)
150 {
151
152 return puffs_cred_isuid(pcr, 0) || puffs_cred_iskernel(pcr)
153 || puffs_cred_isfs(pcr);
154 }
155
156 int
157 puffs_cid_getpid(const struct puffs_cid *pcid, pid_t *pid)
158 {
159 PUFFS_MAKEKCID(pkcid, pcid);
160
161 if (pkcid->pkcid_type == PUFFCID_TYPE_REAL) {
162 *pid = pkcid->pkcid_pid;
163 return 0;
164 } else {
165 errno = ESRCH;
166 return -1;
167 }
168 }
169
170 int
171 puffs_cid_getlwpid(const struct puffs_cid *pcid, lwpid_t *lid)
172 {
173 PUFFS_MAKEKCID(pkcid, pcid);
174
175 if (pkcid->pkcid_type == PUFFCID_TYPE_REAL) {
176 *lid = pkcid->pkcid_lwpid;
177 return 0;
178 } else {
179 errno = ESRCH;
180 return -1;
181 }
182 }
183
184 bool
185 puffs_cid_isequal(const struct puffs_cid *pc1, const struct puffs_cid *pc2)
186 {
187 PUFFS_MAKEKCID(pkc1, pc1);
188 PUFFS_MAKEKCID(pkc2, pc2);
189
190 if (pkc1->pkcid_type != PUFFCID_TYPE_REAL
191 || pkc2->pkcid_type != PUFFCID_TYPE_REAL)
192 return false;
193
194 if (pkc1->pkcid_lwpid == pkc1->pkcid_lwpid
195 && pkc2->pkcid_pid == pkc2->pkcid_pid)
196 return true;
197
198 return false;
199 }
200
201 /*
202 * Generic routine for checking file access rights. Modeled after
203 * vaccess() in the kernel.
204 */
205 int
206 puffs_access(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
207 mode_t acc_mode, const struct puffs_cred *pcr)
208 {
209 mode_t mask;
210
211 /* megapower */
212 if (puffs_cred_iskernel(pcr) || puffs_cred_isfs(pcr))
213 return 0;
214
215 /* superuser, allow all except exec if *ALL* exec bits are unset */
216 if (puffs_cred_isuid(pcr, 0)) {
217 if ((acc_mode & PUFFS_VEXEC) && type != VDIR &&
218 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
219 return EACCES;
220 return 0;
221 }
222
223 mask = 0;
224 /* owner */
225 if (puffs_cred_isuid(pcr, uid)) {
226 if (acc_mode & PUFFS_VEXEC)
227 mask |= S_IXUSR;
228 if (acc_mode & PUFFS_VREAD)
229 mask |= S_IRUSR;
230 if (acc_mode & PUFFS_VWRITE)
231 mask |= S_IWUSR;
232 /* group */
233 } else if (puffs_cred_hasgroup(pcr, gid)) {
234 if (acc_mode & PUFFS_VEXEC)
235 mask |= S_IXGRP;
236 if (acc_mode & PUFFS_VREAD)
237 mask |= S_IRGRP;
238 if (acc_mode & PUFFS_VWRITE)
239 mask |= S_IWGRP;
240 /* other */
241 } else {
242 if (acc_mode & PUFFS_VEXEC)
243 mask |= S_IXOTH;
244 if (acc_mode & PUFFS_VREAD)
245 mask |= S_IROTH;
246 if (acc_mode & PUFFS_VWRITE)
247 mask |= S_IWOTH;
248 }
249
250 if ((file_mode & mask) == mask)
251 return 0;
252 else
253 return EACCES;
254 }
255
256 int
257 puffs_access_chown(uid_t owner, gid_t group, uid_t newowner, gid_t newgroup,
258 const struct puffs_cred *pcr)
259 {
260
261 if (newowner == (uid_t)PUFFS_VNOVAL)
262 newowner = owner;
263 if (newgroup == (gid_t)PUFFS_VNOVAL)
264 newgroup = group;
265
266 if ((!puffs_cred_isuid(pcr, owner) || newowner != owner ||
267 ((newgroup != group && !puffs_cred_hasgroup(pcr, newgroup))))
268 && !puffs_cred_isjuggernaut(pcr))
269 return EPERM;
270
271 return 0;
272 }
273
274 int
275 puffs_access_chmod(uid_t owner, gid_t group, enum vtype type, mode_t mode,
276 const struct puffs_cred *pcr)
277 {
278
279 if (!puffs_cred_isuid(pcr, owner) && !puffs_cred_isjuggernaut(pcr))
280 return EPERM;
281
282 if (!puffs_cred_isjuggernaut(pcr)) {
283 if (type != VDIR && (mode & S_ISTXT))
284 return EFTYPE;
285 if (!puffs_cred_hasgroup(pcr, group) && (mode & S_ISGID))
286 return EPERM;
287 }
288
289 return 0;
290 }
291
292 int
293 puffs_access_times(uid_t uid, gid_t gid, mode_t mode, int va_utimes_null,
294 const struct puffs_cred *pcr)
295 {
296
297 if (!puffs_cred_isuid(pcr, uid) && !puffs_cred_isjuggernaut(pcr)
298 && (va_utimes_null == 0
299 || puffs_access(VNON, mode, uid, gid, PUFFS_VWRITE, pcr) != 0))
300 return EPERM;
301
302 return 0;
303 }
304