kernel: Hide the sysctl.debug sysctl in the SYSCTL_DEBUG kernel option.
[dragonfly.git] / lib / libpuffs / creds.c
1 /*      $NetBSD: creds.c,v 1.15 2009/11/20 14:23:54 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 /*
31  * Interface for dealing with credits.
32  */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36
37 #include <errno.h>
38 #include <stdbool.h>
39 #include <string.h>
40
41 #include "puffs.h"
42 #include "puffs_priv.h"
43
44 #define UUCCRED(a) (a->pkcr_type == PUFFCRED_TYPE_UUC)
45 #define INTCRED(a) (a->pkcr_type == PUFFCRED_TYPE_INTERNAL)
46
47 int
48 puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *ruid)
49 {
50         PUFFS_MAKEKCRED(pkcr, pcr);
51
52         if (!UUCCRED(pkcr)) {
53                 errno = EOPNOTSUPP;
54                 return -1;
55         }
56         *ruid = pkcr->pkcr_uuc.cr_uid;
57
58         return 0;
59 }
60
61 int
62 puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *rgid)
63 {
64         PUFFS_MAKEKCRED(pkcr, pcr);
65
66         if (!UUCCRED(pkcr)) {
67                 errno = EOPNOTSUPP;
68                 return -1;
69         }
70         *rgid = pkcr->pkcr_uuc.cr_gid;
71
72         return 0;
73 }
74
75 int
76 puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *rgids, short *ngids)
77 {
78         PUFFS_MAKEKCRED(pkcr, pcr);
79         size_t ncopy;
80
81         if (!UUCCRED(pkcr)) {
82                 errno = EOPNOTSUPP;
83                 *ngids = 0;
84                 return -1;
85         }
86
87         ncopy = MIN(*ngids, pkcr->pkcr_uuc.cr_ngroups);
88         (void)memcpy(rgids, pkcr->pkcr_uuc.cr_groups, sizeof(gid_t) * ncopy);
89         *ngids = (short)ncopy;
90
91         return 0;
92 }
93
94 bool
95 puffs_cred_isuid(const struct puffs_cred *pcr, uid_t uid)
96 {
97         PUFFS_MAKEKCRED(pkcr, pcr);
98
99         return UUCCRED(pkcr) && pkcr->pkcr_uuc.cr_uid == uid;
100 }
101
102 bool
103 puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t gid)
104 {
105         PUFFS_MAKEKCRED(pkcr, pcr);
106         short i;
107
108         if (!UUCCRED(pkcr))
109                 return false;
110
111         if (pkcr->pkcr_uuc.cr_gid == gid)
112                 return true;
113         for (i = 0; i < pkcr->pkcr_uuc.cr_ngroups; i++)
114                 if (pkcr->pkcr_uuc.cr_groups[i] == gid)
115                         return true;
116
117         return false;
118 }
119
120 bool
121 puffs_cred_isregular(const struct puffs_cred *pcr)
122 {
123         PUFFS_MAKEKCRED(pkcr, pcr);
124
125         return UUCCRED(pkcr);
126 }
127
128 bool
129 puffs_cred_iskernel(const struct puffs_cred *pcr)
130 {
131         PUFFS_MAKEKCRED(pkcr, pcr);
132
133         return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_NOCRED;
134 }
135
136 bool
137 puffs_cred_isfs(const struct puffs_cred *pcr)
138 {
139         PUFFS_MAKEKCRED(pkcr, pcr);
140
141         return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_FSCRED;
142 }
143
144 bool
145 puffs_cred_isjuggernaut(const struct puffs_cred *pcr)
146 {
147
148         return puffs_cred_isuid(pcr, 0) || puffs_cred_iskernel(pcr)
149             || puffs_cred_isfs(pcr);
150 }
151
152 /*
153  * Generic routine for checking file access rights.  Modeled after
154  * vaccess() in the kernel.
155  */
156 int
157 puffs_access(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
158         mode_t acc_mode, const struct puffs_cred *pcr)
159 {
160         mode_t mask;
161
162         /* megapower */
163         if (puffs_cred_iskernel(pcr) || puffs_cred_isfs(pcr))
164                 return 0;
165
166         /* superuser, allow all except exec if *ALL* exec bits are unset */
167         if (puffs_cred_isuid(pcr, 0)) {
168                 if ((acc_mode & PUFFS_VEXEC) && type != VDIR &&
169                     (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
170                         return EACCES;
171                 return 0;
172         }
173
174         mask = 0;
175         /* owner */
176         if (puffs_cred_isuid(pcr, uid)) {
177                 if (acc_mode & PUFFS_VEXEC)
178                         mask |= S_IXUSR;
179                 if (acc_mode & PUFFS_VREAD)
180                         mask |= S_IRUSR;
181                 if (acc_mode & PUFFS_VWRITE)
182                         mask |= S_IWUSR;
183         /* group */
184         } else if (puffs_cred_hasgroup(pcr, gid)) {
185                 if (acc_mode & PUFFS_VEXEC)
186                         mask |= S_IXGRP;
187                 if (acc_mode & PUFFS_VREAD)
188                         mask |= S_IRGRP;
189                 if (acc_mode & PUFFS_VWRITE)
190                         mask |= S_IWGRP;
191         /* other */
192         } else {
193                 if (acc_mode & PUFFS_VEXEC)
194                         mask |= S_IXOTH;
195                 if (acc_mode & PUFFS_VREAD)
196                         mask |= S_IROTH;
197                 if (acc_mode & PUFFS_VWRITE)
198                         mask |= S_IWOTH;
199         }
200
201         if ((file_mode & mask) == mask)
202                 return 0;
203         else
204                 return EACCES;
205 }
206
207 int
208 puffs_access_chown(uid_t owner, gid_t group, uid_t newowner, gid_t newgroup,
209         const struct puffs_cred *pcr)
210 {
211
212         if (newowner == (uid_t)PUFFS_VNOVAL)
213                 newowner = owner;
214         if (newgroup == (gid_t)PUFFS_VNOVAL)
215                 newgroup = group;
216
217         if ((!puffs_cred_isuid(pcr, owner) || newowner != owner ||
218             ((newgroup != group && !puffs_cred_hasgroup(pcr, newgroup))))
219             && !puffs_cred_isjuggernaut(pcr))
220                 return EPERM;
221
222         return 0;
223 }
224
225 int
226 puffs_access_chmod(uid_t owner, gid_t group, enum vtype type, mode_t mode,
227         const struct puffs_cred *pcr)
228 {
229
230         if (!puffs_cred_isuid(pcr, owner) && !puffs_cred_isjuggernaut(pcr))
231                 return EPERM;
232
233         if (!puffs_cred_isjuggernaut(pcr)) {
234                 if (type != VDIR && (mode & S_ISTXT))
235                         return EFTYPE;
236                 if (!puffs_cred_hasgroup(pcr, group) && (mode & S_ISGID))
237                         return EPERM;
238         }
239
240         return 0;
241 }
242
243 int
244 puffs_access_times(uid_t uid, gid_t gid, mode_t mode, int va_utimes_null,
245         const struct puffs_cred *pcr)
246 {
247
248         if (!puffs_cred_isuid(pcr, uid) && !puffs_cred_isjuggernaut(pcr)
249             && (va_utimes_null == 0
250               || puffs_access(VNON, mode, uid, gid, PUFFS_VWRITE, pcr) != 0))
251                 return EPERM;
252
253         return 0;
254 }