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/>.
23 /* prototypes for static functions first - for clarity */
25 static bool smb_ace_to_internal(acl_entry_t posix_ace
,
26 struct smb_acl_entry
*ace
);
27 static struct smb_acl_t
*smb_acl_to_internal(acl_t acl
);
28 static int smb_acl_set_mode(acl_entry_t entry
, SMB_ACL_PERM_T perm
);
29 static acl_t
smb_acl_to_posix(const struct smb_acl_t
*acl
);
32 /* public functions - the api */
34 SMB_ACL_T
posixacl_sys_acl_get_file(vfs_handle_struct
*handle
,
38 struct smb_acl_t
*result
;
43 case SMB_ACL_TYPE_ACCESS
:
44 acl_type
= ACL_TYPE_ACCESS
;
46 case SMB_ACL_TYPE_DEFAULT
:
47 acl_type
= ACL_TYPE_DEFAULT
;
54 acl
= acl_get_file(path_p
, acl_type
);
60 result
= smb_acl_to_internal(acl
);
65 SMB_ACL_T
posixacl_sys_acl_get_fd(vfs_handle_struct
*handle
,
68 struct smb_acl_t
*result
;
69 acl_t acl
= acl_get_fd(fsp
->fh
->fd
);
75 result
= smb_acl_to_internal(acl
);
80 int posixacl_sys_acl_set_file(vfs_handle_struct
*handle
,
89 DEBUG(10, ("Calling acl_set_file: %s, %d\n", name
, type
));
92 case SMB_ACL_TYPE_ACCESS
:
93 acl_type
= ACL_TYPE_ACCESS
;
95 case SMB_ACL_TYPE_DEFAULT
:
96 acl_type
= ACL_TYPE_DEFAULT
;
103 if ((acl
= smb_acl_to_posix(theacl
)) == NULL
) {
106 res
= acl_set_file(name
, acl_type
, acl
);
108 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno
)));
114 int posixacl_sys_acl_set_fd(vfs_handle_struct
*handle
,
119 acl_t acl
= smb_acl_to_posix(theacl
);
123 res
= acl_set_fd(fsp
->fh
->fd
, acl
);
128 int posixacl_sys_acl_delete_def_file(vfs_handle_struct
*handle
,
131 return acl_delete_def_file(path
);
135 /* private functions */
137 static bool smb_ace_to_internal(acl_entry_t posix_ace
,
138 struct smb_acl_entry
*ace
)
141 acl_permset_t permset
;
143 if (acl_get_tag_type(posix_ace
, &tag
) != 0) {
144 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
150 ace
->a_type
= SMB_ACL_USER
;
153 ace
->a_type
= SMB_ACL_USER_OBJ
;
156 ace
->a_type
= SMB_ACL_GROUP
;
159 ace
->a_type
= SMB_ACL_GROUP_OBJ
;
162 ace
->a_type
= SMB_ACL_OTHER
;
165 ace
->a_type
= SMB_ACL_MASK
;
168 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag
));
171 switch(ace
->a_type
) {
173 uid_t
*puid
= (uid_t
*)acl_get_qualifier(posix_ace
);
175 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
183 case SMB_ACL_GROUP
: {
184 gid_t
*pgid
= (uid_t
*)acl_get_qualifier(posix_ace
);
186 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
196 if (acl_get_permset(posix_ace
, &permset
) != 0) {
197 DEBUG(0, ("smb_acl_get_mode failed\n"));
201 #ifdef HAVE_ACL_GET_PERM_NP
202 ace
->a_perm
|= (acl_get_perm_np(permset
, ACL_READ
) ? SMB_ACL_READ
: 0);
203 ace
->a_perm
|= (acl_get_perm_np(permset
, ACL_WRITE
) ? SMB_ACL_WRITE
: 0);
204 ace
->a_perm
|= (acl_get_perm_np(permset
, ACL_EXECUTE
) ? SMB_ACL_EXECUTE
: 0);
206 ace
->a_perm
|= (acl_get_perm(permset
, ACL_READ
) ? SMB_ACL_READ
: 0);
207 ace
->a_perm
|= (acl_get_perm(permset
, ACL_WRITE
) ? SMB_ACL_WRITE
: 0);
208 ace
->a_perm
|= (acl_get_perm(permset
, ACL_EXECUTE
) ? SMB_ACL_EXECUTE
: 0);
213 static struct smb_acl_t
*smb_acl_to_internal(acl_t acl
)
215 struct smb_acl_t
*result
= SMB_MALLOC_P(struct smb_acl_t
);
216 int entry_id
= ACL_FIRST_ENTRY
;
218 if (result
== NULL
) {
221 ZERO_STRUCTP(result
);
222 while (acl_get_entry(acl
, entry_id
, &e
) == 1) {
224 entry_id
= ACL_NEXT_ENTRY
;
226 result
= (struct smb_acl_t
*)SMB_REALLOC(
227 result
, sizeof(struct smb_acl_t
) +
228 (sizeof(struct smb_acl_entry
) * (result
->count
+1)));
229 if (result
== NULL
) {
230 DEBUG(0, ("SMB_REALLOC failed\n"));
235 if (!smb_ace_to_internal(e
, &result
->acl
[result
->count
])) {
245 static int smb_acl_set_mode(acl_entry_t entry
, SMB_ACL_PERM_T perm
)
248 acl_permset_t permset
;
250 if ((ret
= acl_get_permset(entry
, &permset
)) != 0) {
253 if ((ret
= acl_clear_perms(permset
)) != 0) {
256 if ((perm
& SMB_ACL_READ
) &&
257 ((ret
= acl_add_perm(permset
, ACL_READ
)) != 0)) {
260 if ((perm
& SMB_ACL_WRITE
) &&
261 ((ret
= acl_add_perm(permset
, ACL_WRITE
)) != 0)) {
264 if ((perm
& SMB_ACL_EXECUTE
) &&
265 ((ret
= acl_add_perm(permset
, ACL_EXECUTE
)) != 0)) {
268 return acl_set_permset(entry
, permset
);
271 static acl_t
smb_acl_to_posix(const struct smb_acl_t
*acl
)
276 result
= acl_init(acl
->count
);
277 if (result
== NULL
) {
278 DEBUG(10, ("acl_init failed\n"));
282 for (i
=0; i
<acl
->count
; i
++) {
283 const struct smb_acl_entry
*entry
= &acl
->acl
[i
];
287 if (acl_create_entry(&result
, &e
) != 0) {
288 DEBUG(1, ("acl_create_entry failed: %s\n",
293 switch (entry
->a_type
) {
297 case SMB_ACL_USER_OBJ
:
303 case SMB_ACL_GROUP_OBJ
:
313 DEBUG(1, ("Unknown tag value %d\n", entry
->a_type
));
317 if (acl_set_tag_type(e
, tag
) != 0) {
318 DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
319 tag
, strerror(errno
)));
323 switch (entry
->a_type
) {
325 if (acl_set_qualifier(e
, &entry
->uid
) != 0) {
326 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
332 if (acl_set_qualifier(e
, &entry
->gid
) != 0) {
333 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
338 default: /* Shut up, compiler! :-) */
342 if (smb_acl_set_mode(e
, entry
->a_perm
) != 0) {
347 if (acl_valid(result
) != 0) {
348 DEBUG(0, ("smb_acl_to_posix: ACL is invalid for set (%s)\n",
356 if (result
!= NULL
) {
362 /* VFS operations structure */
364 static vfs_op_tuple posixacl_op_tuples
[] = {
365 /* Disk operations */
366 {SMB_VFS_OP(posixacl_sys_acl_get_file
),
367 SMB_VFS_OP_SYS_ACL_GET_FILE
,
368 SMB_VFS_LAYER_TRANSPARENT
},
370 {SMB_VFS_OP(posixacl_sys_acl_get_fd
),
371 SMB_VFS_OP_SYS_ACL_GET_FD
,
372 SMB_VFS_LAYER_TRANSPARENT
},
374 {SMB_VFS_OP(posixacl_sys_acl_set_file
),
375 SMB_VFS_OP_SYS_ACL_SET_FILE
,
376 SMB_VFS_LAYER_TRANSPARENT
},
378 {SMB_VFS_OP(posixacl_sys_acl_set_fd
),
379 SMB_VFS_OP_SYS_ACL_SET_FD
,
380 SMB_VFS_LAYER_TRANSPARENT
},
382 {SMB_VFS_OP(posixacl_sys_acl_delete_def_file
),
383 SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE
,
384 SMB_VFS_LAYER_TRANSPARENT
},
391 NTSTATUS
vfs_posixacl_init(void);
392 NTSTATUS
vfs_posixacl_init(void)
394 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "posixacl",