docs: Fix typos in man vfs_gpfs.
[Samba.git] / source3 / modules / vfs_posixacl.c
blobf009fcc846a15920b294cc51089a512bf8396583
1 /*
2 Unix SMB/Netbios implementation.
3 VFS module to get and set posix acls
4 Copyright (C) Volker Lendecke 2006
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 3 of the License, or
9 (at your option) any later version.
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 the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "modules/vfs_posixacl.h"
25 /* prototypes for static functions first - for clarity */
27 static bool smb_ace_to_internal(acl_entry_t posix_ace,
28 struct smb_acl_entry *ace);
29 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx);
30 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm);
31 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl);
34 /* public functions - the api */
36 SMB_ACL_T posixacl_sys_acl_get_file(vfs_handle_struct *handle,
37 const char *path_p,
38 SMB_ACL_TYPE_T type,
39 TALLOC_CTX *mem_ctx)
41 struct smb_acl_t *result;
42 acl_type_t acl_type;
43 acl_t acl;
45 switch(type) {
46 case SMB_ACL_TYPE_ACCESS:
47 acl_type = ACL_TYPE_ACCESS;
48 break;
49 case SMB_ACL_TYPE_DEFAULT:
50 acl_type = ACL_TYPE_DEFAULT;
51 break;
52 default:
53 errno = EINVAL;
54 return NULL;
57 acl = acl_get_file(path_p, acl_type);
59 if (acl == NULL) {
60 return NULL;
63 result = smb_acl_to_internal(acl, mem_ctx);
64 acl_free(acl);
65 return result;
68 SMB_ACL_T posixacl_sys_acl_get_fd(vfs_handle_struct *handle,
69 files_struct *fsp, TALLOC_CTX *mem_ctx)
71 struct smb_acl_t *result;
72 acl_t acl = acl_get_fd(fsp->fh->fd);
74 if (acl == NULL) {
75 return NULL;
78 result = smb_acl_to_internal(acl, mem_ctx);
79 acl_free(acl);
80 return result;
83 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
84 const char *name,
85 SMB_ACL_TYPE_T type,
86 SMB_ACL_T theacl)
88 int res;
89 acl_type_t acl_type;
90 acl_t acl;
92 DEBUG(10, ("Calling acl_set_file: %s, %d\n", name, type));
94 switch(type) {
95 case SMB_ACL_TYPE_ACCESS:
96 acl_type = ACL_TYPE_ACCESS;
97 break;
98 case SMB_ACL_TYPE_DEFAULT:
99 acl_type = ACL_TYPE_DEFAULT;
100 break;
101 default:
102 errno = EINVAL;
103 return -1;
106 if ((acl = smb_acl_to_posix(theacl)) == NULL) {
107 return -1;
109 res = acl_set_file(name, acl_type, acl);
110 if (res != 0) {
111 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
113 acl_free(acl);
114 return res;
117 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
118 files_struct *fsp,
119 SMB_ACL_T theacl)
121 int res;
122 acl_t acl = smb_acl_to_posix(theacl);
123 if (acl == NULL) {
124 return -1;
126 res = acl_set_fd(fsp->fh->fd, acl);
127 acl_free(acl);
128 return res;
131 int posixacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
132 const char *path)
134 return acl_delete_def_file(path);
138 /* private functions */
140 static bool smb_ace_to_internal(acl_entry_t posix_ace,
141 struct smb_acl_entry *ace)
143 acl_tag_t tag;
144 acl_permset_t permset;
146 if (acl_get_tag_type(posix_ace, &tag) != 0) {
147 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
148 return False;
151 switch(tag) {
152 case ACL_USER:
153 ace->a_type = SMB_ACL_USER;
154 break;
155 case ACL_USER_OBJ:
156 ace->a_type = SMB_ACL_USER_OBJ;
157 break;
158 case ACL_GROUP:
159 ace->a_type = SMB_ACL_GROUP;
160 break;
161 case ACL_GROUP_OBJ:
162 ace->a_type = SMB_ACL_GROUP_OBJ;
163 break;
164 case ACL_OTHER:
165 ace->a_type = SMB_ACL_OTHER;
166 break;
167 case ACL_MASK:
168 ace->a_type = SMB_ACL_MASK;
169 break;
170 #ifdef HAVE_ACL_EVERYONE
171 case ACL_EVERYONE:
172 DEBUG(1, ("ACL tag type ACL_EVERYONE. FreeBSD with ZFS? Use 'vfs objects = zfsacl'\n"));
173 return false;
174 #endif
175 default:
176 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
177 return False;
179 switch(ace->a_type) {
180 case SMB_ACL_USER: {
181 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
182 if (puid == NULL) {
183 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
184 return False;
186 ace->info.user.uid = *puid;
187 acl_free(puid);
188 break;
191 case SMB_ACL_GROUP: {
192 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
193 if (pgid == NULL) {
194 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
195 return False;
197 ace->info.group.gid = *pgid;
198 acl_free(pgid);
199 break;
201 default:
202 break;
204 if (acl_get_permset(posix_ace, &permset) != 0) {
205 DEBUG(0, ("smb_acl_get_mode failed\n"));
206 return False;
208 ace->a_perm = 0;
209 #ifdef HAVE_ACL_GET_PERM_NP
210 ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
211 ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
212 ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
213 #else
214 ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
215 ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
216 ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
217 #endif
218 return True;
221 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx)
223 struct smb_acl_t *result = sys_acl_init(mem_ctx);
224 int entry_id = ACL_FIRST_ENTRY;
225 acl_entry_t e;
226 if (result == NULL) {
227 return NULL;
229 while (acl_get_entry(acl, entry_id, &e) == 1) {
231 entry_id = ACL_NEXT_ENTRY;
233 result->acl = talloc_realloc(result, result->acl,
234 struct smb_acl_entry, result->count+1);
235 if (result->acl == NULL) {
236 TALLOC_FREE(result);
237 DEBUG(0, ("talloc_realloc failed\n"));
238 errno = ENOMEM;
239 return NULL;
242 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
243 TALLOC_FREE(result);
244 return NULL;
247 result->count += 1;
249 return result;
252 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
254 int ret;
255 acl_permset_t permset;
257 if ((ret = acl_get_permset(entry, &permset)) != 0) {
258 return ret;
260 if ((ret = acl_clear_perms(permset)) != 0) {
261 return ret;
263 if ((perm & SMB_ACL_READ) &&
264 ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
265 return ret;
267 if ((perm & SMB_ACL_WRITE) &&
268 ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
269 return ret;
271 if ((perm & SMB_ACL_EXECUTE) &&
272 ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
273 return ret;
275 return acl_set_permset(entry, permset);
278 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
280 acl_t result;
281 int i;
283 result = acl_init(acl->count);
284 if (result == NULL) {
285 DEBUG(10, ("acl_init failed\n"));
286 return NULL;
289 for (i=0; i<acl->count; i++) {
290 const struct smb_acl_entry *entry = &acl->acl[i];
291 acl_entry_t e;
292 acl_tag_t tag;
294 if (acl_create_entry(&result, &e) != 0) {
295 DEBUG(1, ("acl_create_entry failed: %s\n",
296 strerror(errno)));
297 goto fail;
300 switch (entry->a_type) {
301 case SMB_ACL_USER:
302 tag = ACL_USER;
303 break;
304 case SMB_ACL_USER_OBJ:
305 tag = ACL_USER_OBJ;
306 break;
307 case SMB_ACL_GROUP:
308 tag = ACL_GROUP;
309 break;
310 case SMB_ACL_GROUP_OBJ:
311 tag = ACL_GROUP_OBJ;
312 break;
313 case SMB_ACL_OTHER:
314 tag = ACL_OTHER;
315 break;
316 case SMB_ACL_MASK:
317 tag = ACL_MASK;
318 break;
319 default:
320 DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
321 goto fail;
324 if (acl_set_tag_type(e, tag) != 0) {
325 DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
326 tag, strerror(errno)));
327 goto fail;
330 switch (entry->a_type) {
331 case SMB_ACL_USER:
332 if (acl_set_qualifier(e, &entry->info.user.uid) != 0) {
333 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
334 strerror(errno)));
335 goto fail;
337 break;
338 case SMB_ACL_GROUP:
339 if (acl_set_qualifier(e, &entry->info.group.gid) != 0) {
340 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
341 strerror(errno)));
342 goto fail;
344 break;
345 default: /* Shut up, compiler! :-) */
346 break;
349 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
350 goto fail;
354 if (acl_valid(result) != 0) {
355 char *acl_string = sys_acl_to_text(acl, NULL);
356 DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
357 acl_string, strerror(errno)));
358 SAFE_FREE(acl_string);
359 goto fail;
362 return result;
364 fail:
365 if (result != NULL) {
366 acl_free(result);
368 return NULL;
371 /* VFS operations structure */
373 static struct vfs_fn_pointers posixacl_fns = {
374 .sys_acl_get_file_fn = posixacl_sys_acl_get_file,
375 .sys_acl_get_fd_fn = posixacl_sys_acl_get_fd,
376 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
377 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
378 .sys_acl_set_file_fn = posixacl_sys_acl_set_file,
379 .sys_acl_set_fd_fn = posixacl_sys_acl_set_fd,
380 .sys_acl_delete_def_file_fn = posixacl_sys_acl_delete_def_file,
383 NTSTATUS vfs_posixacl_init(void);
384 NTSTATUS vfs_posixacl_init(void)
386 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",
387 &posixacl_fns);