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