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,
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(path_p, 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;
72 acl_t acl = acl_get_fd(fsp->fh->fd);
78 result = smb_acl_to_internal(acl, mem_ctx);
83 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
92 DEBUG(10, ("Calling acl_set_file: %s, %d\n", name, type));
95 case SMB_ACL_TYPE_ACCESS:
96 acl_type = ACL_TYPE_ACCESS;
98 case SMB_ACL_TYPE_DEFAULT:
99 acl_type = ACL_TYPE_DEFAULT;
106 if ((acl = smb_acl_to_posix(theacl)) == NULL) {
109 res = acl_set_file(name, acl_type, acl);
111 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
117 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
122 acl_t acl = smb_acl_to_posix(theacl);
126 res = acl_set_fd(fsp->fh->fd, acl);
131 int posixacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
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)
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"));
153 ace->a_type = SMB_ACL_USER;
156 ace->a_type = SMB_ACL_USER_OBJ;
159 ace->a_type = SMB_ACL_GROUP;
162 ace->a_type = SMB_ACL_GROUP_OBJ;
165 ace->a_type = SMB_ACL_OTHER;
168 ace->a_type = SMB_ACL_MASK;
171 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
174 switch(ace->a_type) {
176 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
178 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
181 ace->info.user.uid = *puid;
186 case SMB_ACL_GROUP: {
187 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
189 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
192 ace->info.group.gid = *pgid;
199 if (acl_get_permset(posix_ace, &permset) != 0) {
200 DEBUG(0, ("smb_acl_get_mode failed\n"));
204 #ifdef HAVE_ACL_GET_PERM_NP
205 ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
206 ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
207 ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
209 ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
210 ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
211 ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
216 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx)
218 struct smb_acl_t *result = sys_acl_init(mem_ctx);
219 int entry_id = ACL_FIRST_ENTRY;
221 if (result == NULL) {
224 while (acl_get_entry(acl, entry_id, &e) == 1) {
226 entry_id = ACL_NEXT_ENTRY;
228 result->acl = talloc_realloc(result, result->acl,
229 struct smb_acl_entry, result->count+1);
230 if (result->acl == NULL) {
232 DEBUG(0, ("talloc_realloc failed\n"));
237 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
247 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
250 acl_permset_t permset;
252 if ((ret = acl_get_permset(entry, &permset)) != 0) {
255 if ((ret = acl_clear_perms(permset)) != 0) {
258 if ((perm & SMB_ACL_READ) &&
259 ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
262 if ((perm & SMB_ACL_WRITE) &&
263 ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
266 if ((perm & SMB_ACL_EXECUTE) &&
267 ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
270 return acl_set_permset(entry, permset);
273 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
278 result = acl_init(acl->count);
279 if (result == NULL) {
280 DEBUG(10, ("acl_init failed\n"));
284 for (i=0; i<acl->count; i++) {
285 const struct smb_acl_entry *entry = &acl->acl[i];
289 if (acl_create_entry(&result, &e) != 0) {
290 DEBUG(1, ("acl_create_entry failed: %s\n",
295 switch (entry->a_type) {
299 case SMB_ACL_USER_OBJ:
305 case SMB_ACL_GROUP_OBJ:
315 DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
319 if (acl_set_tag_type(e, tag) != 0) {
320 DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
321 tag, strerror(errno)));
325 switch (entry->a_type) {
327 if (acl_set_qualifier(e, &entry->info.user.uid) != 0) {
328 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
334 if (acl_set_qualifier(e, &entry->info.group.gid) != 0) {
335 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
340 default: /* Shut up, compiler! :-) */
344 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
349 if (acl_valid(result) != 0) {
350 char *acl_string = sys_acl_to_text(acl, NULL);
351 DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
352 acl_string, strerror(errno)));
353 SAFE_FREE(acl_string);
360 if (result != NULL) {
366 /* VFS operations structure */
368 static struct vfs_fn_pointers posixacl_fns = {
369 .sys_acl_get_file_fn = posixacl_sys_acl_get_file,
370 .sys_acl_get_fd_fn = posixacl_sys_acl_get_fd,
371 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
372 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
373 .sys_acl_set_file_fn = posixacl_sys_acl_set_file,
374 .sys_acl_set_fd_fn = posixacl_sys_acl_set_fd,
375 .sys_acl_delete_def_file_fn = posixacl_sys_acl_delete_def_file,
378 NTSTATUS vfs_posixacl_init(void);
379 NTSTATUS vfs_posixacl_init(void)
381 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",