ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jfs / acl.c
1 /*
2  *   Copyright (c) International Business Machines  Corp., 2002
3  *   Copyright (c) Andreas Gruenbacher, 2001
4  *   Copyright (c) Linus Torvalds, 1991, 1992
5  *
6  *   This program is free software;  you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or 
9  *   (at your option) any later version.
10  * 
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  *   the GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program;  if not, write to the Free Software 
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include <linux/sched.h>
22 #include <linux/fs.h>
23 #include "jfs_incore.h"
24 #include "jfs_xattr.h"
25 #include "jfs_acl.h"
26
27 static struct posix_acl *jfs_get_acl(struct inode *inode, int type)
28 {
29         struct posix_acl *acl;
30         char *ea_name;
31         struct jfs_inode_info *ji = JFS_IP(inode);
32         struct posix_acl **p_acl;
33         int size;
34         char *value = NULL;
35
36         switch(type) {
37                 case ACL_TYPE_ACCESS:
38                         ea_name = XATTR_NAME_ACL_ACCESS;
39                         p_acl = &ji->i_acl;
40                         break;
41                 case ACL_TYPE_DEFAULT:
42                         ea_name = XATTR_NAME_ACL_DEFAULT;
43                         p_acl = &ji->i_default_acl;
44                         break;
45                 default:
46                         return ERR_PTR(-EINVAL);
47         }
48
49         if (*p_acl != JFS_ACL_NOT_CACHED)
50                 return posix_acl_dup(*p_acl);
51
52         size = __jfs_getxattr(inode, ea_name, NULL, 0);
53
54         if (size > 0) {
55                 value = kmalloc(size, GFP_KERNEL);
56                 if (!value)
57                         return ERR_PTR(-ENOMEM);
58                 size = __jfs_getxattr(inode, ea_name, value, size);
59         }
60
61         if (size < 0) {
62                 if (size == -ENODATA) {
63                         *p_acl = NULL;
64                         acl = NULL;
65                 } else
66                         acl = ERR_PTR(size);
67         } else {
68                 acl = posix_acl_from_xattr(value, size);
69                 if (!IS_ERR(acl))
70                         *p_acl = posix_acl_dup(acl);
71         }
72         if (value)
73                 kfree(value);
74         return acl;
75 }
76
77 static int jfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
78 {
79         char *ea_name;
80         struct jfs_inode_info *ji = JFS_IP(inode);
81         struct posix_acl **p_acl;
82         int rc;
83         int size = 0;
84         char *value = NULL;
85
86         if (S_ISLNK(inode->i_mode))
87                 return -EOPNOTSUPP;
88
89         switch(type) {
90                 case ACL_TYPE_ACCESS:
91                         ea_name = XATTR_NAME_ACL_ACCESS;
92                         p_acl = &ji->i_acl;
93                         break;
94                 case ACL_TYPE_DEFAULT:
95                         ea_name = XATTR_NAME_ACL_DEFAULT;
96                         p_acl = &ji->i_default_acl;
97                         if (!S_ISDIR(inode->i_mode))
98                                 return acl ? -EACCES : 0;
99                         break;
100                 default:
101                         return -EINVAL;
102         }
103         if (acl) {
104                 size = xattr_acl_size(acl->a_count);
105                 value = kmalloc(size, GFP_KERNEL);
106                 if (!value)
107                         return -ENOMEM;
108                 rc = posix_acl_to_xattr(acl, value, size);
109                 if (rc < 0)
110                         goto out;
111         }
112         rc = __jfs_setxattr(inode, ea_name, value, size, 0);
113 out:
114         if (value)
115                 kfree(value);
116
117         if (!rc) {
118                 if (*p_acl && (*p_acl != JFS_ACL_NOT_CACHED))
119                         posix_acl_release(*p_acl);
120                 *p_acl = posix_acl_dup(acl);
121         }
122         return rc;
123 }
124
125 /*
126  *      jfs_permission()
127  *
128  * modified vfs_permission to check posix acl
129  */
130 int jfs_permission(struct inode * inode, int mask, struct nameidata *nd)
131 {
132         umode_t mode = inode->i_mode;
133         struct jfs_inode_info *ji = JFS_IP(inode);
134
135         if (mask & MAY_WRITE) {
136                 /*
137                  * Nobody gets write access to a read-only fs.
138                  */
139                 if (IS_RDONLY(inode) &&
140                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
141                         return -EROFS;
142
143                 /*
144                  * Nobody gets write access to an immutable file.
145                  */
146                 if (IS_IMMUTABLE(inode))
147                         return -EACCES;
148         }
149
150         if (current->fsuid == inode->i_uid) {
151                 mode >>= 6;
152                 goto check_mode;
153         }
154         /*
155          * ACL can't contain additional permissions if the ACL_MASK entry
156          * is zero.
157          */
158         if (!(mode & S_IRWXG))
159                 goto check_groups;
160
161         if (ji->i_acl == JFS_ACL_NOT_CACHED) {
162                 struct posix_acl *acl;
163
164                 acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
165
166                 if (IS_ERR(acl))
167                         return PTR_ERR(acl);
168                 posix_acl_release(acl);
169         }
170
171         if (ji->i_acl) {
172                 int rc = posix_acl_permission(inode, ji->i_acl, mask);
173                 if (rc == -EACCES)
174                         goto check_capabilities;
175                 return rc;
176         }
177
178 check_groups:
179         if (in_group_p(inode->i_gid))
180                 mode >>= 3;
181
182 check_mode:
183         /*
184          * If the DACs are ok we don't need any capability check.
185          */
186         if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
187                 return 0;
188
189 check_capabilities:
190         /*
191          * Read/write DACs are always overridable.
192          * Executable DACs are overridable if at least one exec bit is set.
193          */
194         if (!(mask & MAY_EXEC) ||
195             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
196                 if (capable(CAP_DAC_OVERRIDE))
197                         return 0;
198
199         /*
200          * Searching includes executable on directories, else just read.
201          */
202         if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
203                 if (capable(CAP_DAC_READ_SEARCH))
204                         return 0;
205
206         return -EACCES;
207 }
208
209 int jfs_init_acl(struct inode *inode, struct inode *dir)
210 {
211         struct posix_acl *acl = NULL;
212         struct posix_acl *clone;
213         mode_t mode;
214         int rc = 0;
215
216         if (S_ISLNK(inode->i_mode))
217                 return 0;
218
219         acl = jfs_get_acl(dir, ACL_TYPE_DEFAULT);
220         if (IS_ERR(acl))
221                 return PTR_ERR(acl);
222
223         if (acl) {
224                 if (S_ISDIR(inode->i_mode)) {
225                         rc = jfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
226                         if (rc)
227                                 goto cleanup;
228                 }
229                 clone = posix_acl_clone(acl, GFP_KERNEL);
230                 if (!clone) {
231                         rc = -ENOMEM;
232                         goto cleanup;
233                 }
234                 mode = inode->i_mode;
235                 rc = posix_acl_create_masq(clone, &mode);
236                 if (rc >= 0) {
237                         inode->i_mode = mode;
238                         if (rc > 0)
239                                 rc = jfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
240                 }
241                 posix_acl_release(clone);
242 cleanup:
243                 posix_acl_release(acl);
244         } else
245                 inode->i_mode &= ~current->fs->umask;
246
247         return rc;
248 }
249
250 static int jfs_acl_chmod(struct inode *inode)
251 {
252         struct posix_acl *acl, *clone;
253         int rc;
254
255         if (S_ISLNK(inode->i_mode))
256                 return -EOPNOTSUPP;
257
258         acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
259         if (IS_ERR(acl) || !acl)
260                 return PTR_ERR(acl);
261
262         clone = posix_acl_clone(acl, GFP_KERNEL);
263         posix_acl_release(acl);
264         if (!clone)
265                 return -ENOMEM;
266
267         rc = posix_acl_chmod_masq(clone, inode->i_mode);
268         if (!rc)
269                 rc = jfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
270
271         posix_acl_release(clone);
272         return rc;
273 }
274
275 int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
276 {
277         struct inode *inode = dentry->d_inode;
278         int rc;
279
280         rc = inode_change_ok(inode, iattr);
281         if (rc)
282                 return rc;
283
284         inode_setattr(inode, iattr);
285
286         if (iattr->ia_valid & ATTR_MODE)
287                 rc = jfs_acl_chmod(inode);
288
289         return rc;
290 }