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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* prototypes for static functions first - for clarity */
26 static BOOL smb_ace_to_internal(acl_entry_t posix_ace,
27 struct smb_acl_entry *ace);
28 static struct smb_acl_t *smb_acl_to_internal(acl_t acl);
29 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm);
30 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl);
33 /* public functions - the api */
35 SMB_ACL_T posixacl_sys_acl_get_file(vfs_handle_struct *handle,
39 struct smb_acl_t *result;
44 case SMB_ACL_TYPE_ACCESS:
45 acl_type = ACL_TYPE_ACCESS;
47 case SMB_ACL_TYPE_DEFAULT:
48 acl_type = ACL_TYPE_DEFAULT;
55 acl = acl_get_file(path_p, acl_type);
61 result = smb_acl_to_internal(acl);
66 SMB_ACL_T posixacl_sys_acl_get_fd(vfs_handle_struct *handle,
70 struct smb_acl_t *result;
71 acl_t acl = acl_get_fd(fd);
77 result = smb_acl_to_internal(acl);
82 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
91 DEBUG(10, ("Calling acl_set_file: %s, %d\n", name, type));
94 case SMB_ACL_TYPE_ACCESS:
95 acl_type = ACL_TYPE_ACCESS;
97 case SMB_ACL_TYPE_DEFAULT:
98 acl_type = ACL_TYPE_DEFAULT;
105 if ((acl = smb_acl_to_posix(theacl)) == NULL) {
108 res = acl_set_file(name, acl_type, acl);
110 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
116 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
118 int fd, SMB_ACL_T theacl)
121 acl_t acl = smb_acl_to_posix(theacl);
125 res = acl_set_fd(fd, acl);
130 int posixacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
133 return acl_delete_def_file(path);
137 /* private functions */
139 static BOOL smb_ace_to_internal(acl_entry_t posix_ace,
140 struct smb_acl_entry *ace)
143 acl_permset_t permset;
145 if (acl_get_tag_type(posix_ace, &tag) != 0) {
146 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
152 ace->a_type = SMB_ACL_USER;
155 ace->a_type = SMB_ACL_USER_OBJ;
158 ace->a_type = SMB_ACL_GROUP;
161 ace->a_type = SMB_ACL_GROUP_OBJ;
164 ace->a_type = SMB_ACL_OTHER;
167 ace->a_type = SMB_ACL_MASK;
170 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
173 switch(ace->a_type) {
175 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
177 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
185 case SMB_ACL_GROUP: {
186 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
188 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
198 if (acl_get_permset(posix_ace, &permset) != 0) {
199 DEBUG(0, ("smb_acl_get_mode failed\n"));
203 ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
204 ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
205 ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
209 static struct smb_acl_t *smb_acl_to_internal(acl_t acl)
211 struct smb_acl_t *result = SMB_MALLOC_P(struct smb_acl_t);
212 int entry_id = ACL_FIRST_ENTRY;
214 if (result == NULL) {
217 ZERO_STRUCTP(result);
218 while (acl_get_entry(acl, entry_id, &e) == 1) {
220 entry_id = ACL_NEXT_ENTRY;
222 result = (struct smb_acl_t *)SMB_REALLOC(
223 result, sizeof(struct smb_acl_t) +
224 (sizeof(struct smb_acl_entry) * (result->count+1)));
225 if (result == NULL) {
226 DEBUG(0, ("SMB_REALLOC failed\n"));
231 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
241 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
244 acl_permset_t permset;
246 if ((ret = acl_get_permset(entry, &permset)) != 0) {
249 if ((ret = acl_clear_perms(permset)) != 0) {
252 if ((perm & SMB_ACL_READ) &&
253 ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
256 if ((perm & SMB_ACL_WRITE) &&
257 ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
260 if ((perm & SMB_ACL_EXECUTE) &&
261 ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
264 return acl_set_permset(entry, permset);
267 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
272 result = acl_init(acl->count);
273 if (result == NULL) {
274 DEBUG(10, ("acl_init failed\n"));
278 for (i=0; i<acl->count; i++) {
279 const struct smb_acl_entry *entry = &acl->acl[i];
283 if (acl_create_entry(&result, &e) != 0) {
284 DEBUG(1, ("acl_create_entry failed: %s\n",
289 switch (entry->a_type) {
293 case SMB_ACL_USER_OBJ:
299 case SMB_ACL_GROUP_OBJ:
309 DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
313 if (acl_set_tag_type(e, tag) != 0) {
314 DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
315 tag, strerror(errno)));
319 switch (entry->a_type) {
321 if (acl_set_qualifier(e, &entry->uid) != 0) {
322 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
328 if (acl_set_qualifier(e, &entry->gid) != 0) {
329 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
334 default: /* Shut up, compiler! :-) */
338 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
343 if (acl_valid(result) != 0) {
344 DEBUG(0, ("smb_acl_to_posix: ACL is invalid for set (%s)\n",
352 if (result != NULL) {
358 /* VFS operations structure */
360 static vfs_op_tuple posixacl_op_tuples[] = {
361 /* Disk operations */
362 {SMB_VFS_OP(posixacl_sys_acl_get_file),
363 SMB_VFS_OP_SYS_ACL_GET_FILE,
364 SMB_VFS_LAYER_TRANSPARENT},
366 {SMB_VFS_OP(posixacl_sys_acl_get_fd),
367 SMB_VFS_OP_SYS_ACL_GET_FD,
368 SMB_VFS_LAYER_TRANSPARENT},
370 {SMB_VFS_OP(posixacl_sys_acl_set_file),
371 SMB_VFS_OP_SYS_ACL_SET_FILE,
372 SMB_VFS_LAYER_TRANSPARENT},
374 {SMB_VFS_OP(posixacl_sys_acl_set_fd),
375 SMB_VFS_OP_SYS_ACL_SET_FD,
376 SMB_VFS_LAYER_TRANSPARENT},
378 {SMB_VFS_OP(posixacl_sys_acl_delete_def_file),
379 SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
380 SMB_VFS_LAYER_TRANSPARENT},
387 NTSTATUS vfs_posixacl_init(void)
389 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",