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/>.
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 struct smb_filename *smb_fname,
41 struct smb_acl_t *result;
46 case SMB_ACL_TYPE_ACCESS:
47 acl_type = ACL_TYPE_ACCESS;
49 case SMB_ACL_TYPE_DEFAULT:
50 acl_type = ACL_TYPE_DEFAULT;
57 acl = acl_get_file(smb_fname->base_name, acl_type);
63 result = smb_acl_to_internal(acl, mem_ctx);
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;
74 if (!fsp->fsp_flags.is_pathref) {
75 acl = acl_get_fd(fsp_get_io_fd(fsp));
76 } else if (fsp->fsp_flags.have_proc_fds) {
77 int fd = fsp_get_pathref_fd(fsp);
78 const char *proc_fd_path = NULL;
81 proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
82 if (proc_fd_path == NULL) {
86 acl = acl_get_file(proc_fd_path, ACL_TYPE_ACCESS);
89 * This is no longer a handle based call.
91 acl = acl_get_file(fsp->fsp_name->base_name, ACL_TYPE_ACCESS);
97 result = smb_acl_to_internal(acl, mem_ctx);
102 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
103 const struct smb_filename *smb_fname,
111 DEBUG(10, ("Calling acl_set_file: %s, %d\n",
112 smb_fname->base_name,
116 case SMB_ACL_TYPE_ACCESS:
117 acl_type = ACL_TYPE_ACCESS;
119 case SMB_ACL_TYPE_DEFAULT:
120 acl_type = ACL_TYPE_DEFAULT;
127 if ((acl = smb_acl_to_posix(theacl)) == NULL) {
130 res = acl_set_file(smb_fname->base_name, acl_type, acl);
132 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
138 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
144 acl_t acl = smb_acl_to_posix(theacl);
145 int fd = fsp_get_pathref_fd(fsp);
151 if (!fsp->fsp_flags.is_pathref && type == SMB_ACL_TYPE_ACCESS) {
152 res = acl_set_fd(fd, acl);
153 } else if (fsp->fsp_flags.have_proc_fds) {
154 const char *proc_fd_path = NULL;
157 proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
158 if (proc_fd_path == NULL) {
161 res = acl_set_file(proc_fd_path, type, acl);
164 * This is no longer a handle based call.
166 res = acl_set_file(fsp->fsp_name->base_name,
175 int posixacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
176 const struct smb_filename *smb_fname)
178 return acl_delete_def_file(smb_fname->base_name);
181 int posixacl_sys_acl_delete_def_fd(vfs_handle_struct *handle,
184 if (fsp->fsp_flags.have_proc_fds) {
185 int fd = fsp_get_pathref_fd(fsp);
186 const char *proc_fd_path = NULL;
189 proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
190 if (proc_fd_path == NULL) {
193 return acl_delete_def_file(proc_fd_path);
197 * This is no longer a handle based call.
199 return acl_delete_def_file(fsp->fsp_name->base_name);
202 /* private functions */
204 static bool smb_ace_to_internal(acl_entry_t posix_ace,
205 struct smb_acl_entry *ace)
208 acl_permset_t permset;
210 if (acl_get_tag_type(posix_ace, &tag) != 0) {
211 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
217 ace->a_type = SMB_ACL_USER;
220 ace->a_type = SMB_ACL_USER_OBJ;
223 ace->a_type = SMB_ACL_GROUP;
226 ace->a_type = SMB_ACL_GROUP_OBJ;
229 ace->a_type = SMB_ACL_OTHER;
232 ace->a_type = SMB_ACL_MASK;
234 #ifdef HAVE_ACL_EVERYONE
236 DEBUG(1, ("ACL tag type ACL_EVERYONE. FreeBSD with ZFS? Use 'vfs objects = zfsacl'\n"));
240 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
243 switch(ace->a_type) {
245 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
247 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
250 ace->info.user.uid = *puid;
255 case SMB_ACL_GROUP: {
256 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
258 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
261 ace->info.group.gid = *pgid;
268 if (acl_get_permset(posix_ace, &permset) != 0) {
269 DEBUG(0, ("smb_acl_get_mode failed\n"));
273 #ifdef HAVE_ACL_GET_PERM_NP
274 ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
275 ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
276 ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
278 ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
279 ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
280 ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
285 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx)
287 struct smb_acl_t *result = sys_acl_init(mem_ctx);
288 int entry_id = ACL_FIRST_ENTRY;
290 if (result == NULL) {
293 while (acl_get_entry(acl, entry_id, &e) == 1) {
295 entry_id = ACL_NEXT_ENTRY;
297 result->acl = talloc_realloc(result, result->acl,
298 struct smb_acl_entry, result->count+1);
299 if (result->acl == NULL) {
301 DEBUG(0, ("talloc_realloc failed\n"));
306 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
316 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
319 acl_permset_t permset;
321 if ((ret = acl_get_permset(entry, &permset)) != 0) {
324 if ((ret = acl_clear_perms(permset)) != 0) {
327 if ((perm & SMB_ACL_READ) &&
328 ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
331 if ((perm & SMB_ACL_WRITE) &&
332 ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
335 if ((perm & SMB_ACL_EXECUTE) &&
336 ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
343 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
348 result = acl_init(acl->count);
349 if (result == NULL) {
350 DEBUG(10, ("acl_init failed\n"));
354 for (i=0; i<acl->count; i++) {
355 const struct smb_acl_entry *entry = &acl->acl[i];
359 if (acl_create_entry(&result, &e) != 0) {
360 DEBUG(1, ("acl_create_entry failed: %s\n",
365 switch (entry->a_type) {
369 case SMB_ACL_USER_OBJ:
375 case SMB_ACL_GROUP_OBJ:
385 DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
389 if (acl_set_tag_type(e, tag) != 0) {
390 DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
391 tag, strerror(errno)));
395 switch (entry->a_type) {
397 if (acl_set_qualifier(e, &entry->info.user.uid) != 0) {
398 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
404 if (acl_set_qualifier(e, &entry->info.group.gid) != 0) {
405 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
410 default: /* Shut up, compiler! :-) */
414 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
419 if (acl_valid(result) != 0) {
420 char *acl_string = sys_acl_to_text(acl, NULL);
421 DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
422 acl_string, strerror(errno)));
423 SAFE_FREE(acl_string);
430 if (result != NULL) {
436 /* VFS operations structure */
438 static struct vfs_fn_pointers posixacl_fns = {
439 .sys_acl_get_file_fn = posixacl_sys_acl_get_file,
440 .sys_acl_get_fd_fn = posixacl_sys_acl_get_fd,
441 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
442 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
443 .sys_acl_set_fd_fn = posixacl_sys_acl_set_fd,
444 .sys_acl_delete_def_file_fn = posixacl_sys_acl_delete_def_file,
445 .sys_acl_delete_def_fd_fn = posixacl_sys_acl_delete_def_fd,
449 NTSTATUS vfs_posixacl_init(TALLOC_CTX *ctx)
451 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",