Whamcloud - gitweb
This commit was generated by cvs2svn to compensate for changes in r46154,
[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-2003 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 "inode.h"
54 #include "sysio-symbols.h"
55
56 /*
57  * Check given access type on given inode.
58  */
59 static int
60 _sysio_check_permission(struct inode *ino,
61                         uid_t uid, gid_t gid,
62                         gid_t gids[], size_t ngids,
63                         int amode)
64 {
65         mode_t  mask;
66         struct intnl_stat *stat;
67
68         /*
69          * Check amode.
70          */
71         if ((amode & (R_OK|W_OK|X_OK)) != amode)
72                 return -EINVAL;
73
74         if (!amode)
75                 return 0;
76
77         mask = 0;
78         if (amode & R_OK)
79                 mask |= S_IRUSR;
80         if (amode & W_OK)
81                 mask |= S_IWUSR;
82         if (amode & X_OK)
83                 mask |= S_IXUSR;
84
85         stat = &ino->i_stbuf;
86         if (stat->st_uid == uid && (stat->st_mode & mask) == mask) 
87                 return 0;
88
89         mask >>= 3;
90         if (stat->st_gid == gid && (stat->st_mode & mask) == mask)
91                 return 0;
92
93         while (ngids) {
94                 ngids--;
95                 if (stat->st_gid == *gids++ && (stat->st_mode & mask) == mask)
96                         return 0;
97         }
98
99         mask >>= 3;
100         if ((stat->st_mode & mask) == mask)
101                 return 0;
102
103         return -EACCES;
104 }
105
106 /*
107  * Determine if a given access is permitted to a give file.
108  */
109 int
110 _sysio_permitted(struct inode *ino, int amode)
111 {
112         int     err;
113         gid_t   *gids;
114         int     n;
115         void    *p;
116
117         err = 0;
118         gids = NULL;
119         for (;;) {
120                 n = getgroups(0, NULL);
121                 if (!n)
122                         break;
123                 p = realloc(gids, n * sizeof(gid_t));
124                 if (!p && gids) {
125                         err = -ENOMEM;
126                         break;
127                 }
128                 gids = p;
129                 err = getgroups(n, gids);
130                 if (err < 0) {
131                         if (errno == EINVAL)
132                                 continue;
133                         err = -errno;
134                         break;
135                 }
136                 err =
137                     _sysio_check_permission(ino,
138                                             geteuid(), getegid(),
139                                             gids, (size_t )n,
140                                             amode);
141                 break;
142         }
143         if (!gids)
144                 return err;
145         free(gids);
146         return err;
147 }
148
149 int
150 SYSIO_INTERFACE_NAME(access)(const char *path, int amode)
151 {
152         struct intent intent;
153         int     err;
154         struct pnode *pno;
155
156         SYSIO_INTERFACE_DISPLAY_BLOCK;
157
158         SYSIO_INTERFACE_ENTER;
159
160         INTENT_INIT(&intent, INT_GETATTR, NULL, NULL);
161         err = _sysio_namei(_sysio_cwd, path, 0, &intent, &pno);
162         if (err)
163                 SYSIO_INTERFACE_RETURN(-1, err);
164         err =
165             _sysio_check_permission(pno->p_base->pb_ino,
166                                     getuid(), getgid(),
167                                     NULL, 0,
168                                     amode);
169         P_RELE(pno);
170         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
171 }
172
173 #ifdef REDSTORM
174 #undef __access
175 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(access),
176                      PREPEND(__, SYSIO_INTERFACE_NAME(access)))
177 #endif