Whamcloud - gitweb
80e8fcd9ed005ea528c5cad7a339ad55ea212153
[fs/lustre-release.git] / libsysio / src / access.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2006 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <assert.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/queue.h>
51
52 #include "sysio.h"
53 #include "mount.h"
54 #include "fs.h"
55 #include "inode.h"
56 #include "sysio-symbols.h"
57
58 /*
59  * Use a persistent buffer for gids. No, not a cache. We just want to
60  * avoid calling malloc over, and over, and...
61  */
62 static gid_t *gids = NULL;
63 static int gidslen = 0;
64
65 /*
66  * Check given access type on given inode.
67  */
68 int
69 _sysio_check_permission(struct pnode *pno, struct creds *crp, int amode)
70 {
71         mode_t  mask;
72         struct inode *ino;
73         int     err;
74         struct intnl_stat *stat;
75         gid_t   *gids;
76         int     ngids;
77         int     group_matched;
78
79         /*
80          * Check amode.
81          */
82         if ((amode & (R_OK|W_OK|X_OK)) != amode)
83                 return -EINVAL;
84
85         if (!amode)
86                 return 0;
87
88         mask = 0;
89         if (amode & R_OK)
90                 mask |= S_IRUSR;
91         if (amode & W_OK)
92                 mask |= S_IWUSR;
93         if (amode & X_OK)
94                 mask |= S_IXUSR;
95
96         ino = pno->p_base->pb_ino;
97         assert(ino);
98
99         err = -EACCES;                                  /* assume error */
100         stat = &ino->i_stbuf;
101         do {
102                 /*
103                  * Owner?
104                  */
105                 if (stat->st_uid == crp->creds_uid) {
106                         if ((stat->st_mode & mask) == mask)
107                                 err = 0;
108                         break;
109                 }
110
111                 /*
112                  * Group?
113                  */
114                 mask >>= 3;
115                 group_matched = 0;
116                 gids = crp->creds_gids;
117                 ngids = crp->creds_ngids;
118                 while (ngids) {
119                         ngids--;
120                         if (stat->st_gid == *gids++) {
121                                 group_matched = 1;
122                                 if ((stat->st_mode & mask) == mask)
123                                         err = 0;
124                         }
125                 }
126                 if (group_matched)
127                         break;
128
129                 /*
130                  * Other?
131                  */
132                 mask >>= 3;
133                 if ((stat->st_mode & mask) == mask)
134                         err = 0;
135         } while (0);
136         if (err)
137                 return err;
138
139         /*
140          * Check for RO access to the file due to mount
141          * options.
142          */
143         if (amode & W_OK && IS_RDONLY(pno))
144                 return -EROFS;
145
146         return 0;
147 }
148
149 /*
150  * Cache groups.
151  */
152 static int
153 _sysio_ldgroups(gid_t gid0, gid_t **gidsp, int *gidslenp)
154 {
155         int     n, i;
156         void    *p;
157
158         n = *gidslenp;
159         if (n < 8) {
160                 *gidsp = NULL;
161                 n = 8;
162         }
163         for (;;) {
164                 /*
165                  * This is far more expensive than I would like. Each time
166                  * called it has to go to some length to acquire the
167                  * current uid and groups membership. We can't just cache
168                  * the result, either. The caller could have altered something
169                  * asynchronously. Wish we had easy access to this info.
170                  */
171                 if (n > *gidslenp) {
172                         p = realloc(*gidsp, (size_t )n * sizeof(gid_t));
173                         if (!p)
174                                 return -errno;
175                         *gidsp = p;
176                         *gidslenp = n;
177                 }
178                 (*gidsp)[0] = gid0;
179                 i = getgroups(n - 1, *gidsp + 1);
180                 if (i < 0) {
181                         if (errno != EINVAL)
182                                 return -errno;
183                         if (INT_MAX / 2 < n)
184                                 return -EINVAL;
185                         n *= 2;
186                         continue;
187                 }
188                 break;
189         }
190         return i;
191 }
192
193 /*
194  * Get current credentials.
195  */
196 static int
197 _sysio_ldcreds(uid_t uid, gid_t gid, struct creds *crp)
198 {
199         int     n;
200
201         n = _sysio_ldgroups(gid, &gids, &gidslen);
202         if (n < 0)
203                 return n;
204         crp->creds_uid = uid;
205         crp->creds_gids = gids;
206         crp->creds_ngids = n;
207
208         return 0;
209 }
210
211 static int
212 _sysio_getcreds(struct creds *crp)
213 {
214
215         return _sysio_ldcreds(getuid(), getgid(), crp);
216 }
217
218 /*
219  * Determine if a given access is permitted to a given file.
220  */
221 int
222 _sysio_permitted(struct pnode *pno, int amode)
223 {
224         struct creds cr;
225         int     err;
226
227         err = _sysio_ldcreds(geteuid(), getegid(), &cr);
228         if (err < 0)
229                 return err;
230         err = _sysio_check_permission(pno, &cr, amode);
231         return err;
232 }
233
234 #ifdef ZERO_SUM_MEMORY
235 /*
236  * Clean up persistent resource on shutdown.
237  */
238 void
239 _sysio_access_shutdown()
240 {
241
242         if (gids)
243                 free(gids);
244         gids = NULL;
245         gidslen = 0;
246 }
247 #endif
248
249 int
250 SYSIO_INTERFACE_NAME(access)(const char *path, int amode)
251 {
252         struct intent intent;
253         int     err;
254         struct pnode *pno;
255         struct creds cr;
256
257         SYSIO_INTERFACE_DISPLAY_BLOCK;
258
259         SYSIO_INTERFACE_ENTER;
260
261         INTENT_INIT(&intent, INT_GETATTR, NULL, NULL);
262         err = _sysio_namei(_sysio_cwd, path, 0, &intent, &pno);
263         if (err)
264                 SYSIO_INTERFACE_RETURN(-1, err);
265         err = _sysio_ldcreds(geteuid(), getegid(), &cr);
266         if (err < 0)
267                 goto out;
268         err =
269             _sysio_check_permission(pno, &cr, amode);
270 out:
271         P_RELE(pno);
272         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
273 }
274
275 #ifdef REDSTORM
276 #undef __access
277 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(access),
278                      PREPEND(__, SYSIO_INTERFACE_NAME(access)))
279 #endif