ctdb/events: add 47.samba-dcerpcd.script
[Samba.git] / source3 / modules / vfs_posixacl.c
blobfeb819de98259dc176651e86cde45a5089e67c27
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_fd(vfs_handle_struct *handle,
37 files_struct *fsp,
38 SMB_ACL_TYPE_T type,
39 TALLOC_CTX *mem_ctx)
41 struct smb_acl_t *result;
42 acl_t acl = NULL;
43 acl_type_t acl_type;
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;
56 if (!fsp->fsp_flags.is_pathref && (acl_type == ACL_TYPE_ACCESS)) {
57 /* POSIX API only allows ACL_TYPE_ACCESS fetched on fd. */
58 acl = acl_get_fd(fsp_get_io_fd(fsp));
59 } else if (fsp->fsp_flags.have_proc_fds) {
60 int fd = fsp_get_pathref_fd(fsp);
61 struct sys_proc_fd_path_buf buf;
63 acl = acl_get_file(sys_proc_fd_path(fd, &buf), acl_type);
64 } else {
66 * This is no longer a handle based call.
68 acl = acl_get_file(fsp->fsp_name->base_name, acl_type);
70 if (acl == NULL) {
71 return NULL;
74 result = smb_acl_to_internal(acl, mem_ctx);
75 acl_free(acl);
76 return result;
79 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
80 files_struct *fsp,
81 SMB_ACL_TYPE_T type,
82 SMB_ACL_T theacl)
84 int res;
85 acl_t acl = smb_acl_to_posix(theacl);
86 acl_type_t acl_type;
87 int fd = fsp_get_pathref_fd(fsp);
89 if (acl == NULL) {
90 return -1;
93 switch(type) {
94 case SMB_ACL_TYPE_ACCESS:
95 acl_type = ACL_TYPE_ACCESS;
96 break;
97 case SMB_ACL_TYPE_DEFAULT:
98 acl_type = ACL_TYPE_DEFAULT;
99 break;
100 default:
101 acl_free(acl);
102 errno = EINVAL;
103 return -1;
106 if (!fsp->fsp_flags.is_pathref && type == SMB_ACL_TYPE_ACCESS) {
107 res = acl_set_fd(fd, acl);
108 } else if (fsp->fsp_flags.have_proc_fds) {
109 struct sys_proc_fd_path_buf buf;
111 res = acl_set_file(sys_proc_fd_path(fd, &buf), acl_type, acl);
112 } else {
114 * This is no longer a handle based call.
116 res = acl_set_file(fsp->fsp_name->base_name,
117 acl_type,
118 acl);
121 acl_free(acl);
122 return res;
125 int posixacl_sys_acl_delete_def_fd(vfs_handle_struct *handle,
126 files_struct *fsp)
128 if (fsp->fsp_flags.have_proc_fds) {
129 int fd = fsp_get_pathref_fd(fsp);
130 struct sys_proc_fd_path_buf buf;
132 return acl_delete_def_file(sys_proc_fd_path(fd, &buf));
136 * This is no longer a handle based call.
138 return acl_delete_def_file(fsp->fsp_name->base_name);
141 /* private functions */
143 static bool smb_ace_to_internal(acl_entry_t posix_ace,
144 struct smb_acl_entry *ace)
146 acl_tag_t tag;
147 acl_permset_t permset;
149 if (acl_get_tag_type(posix_ace, &tag) != 0) {
150 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
151 return False;
154 switch(tag) {
155 case ACL_USER:
156 ace->a_type = SMB_ACL_USER;
157 break;
158 case ACL_USER_OBJ:
159 ace->a_type = SMB_ACL_USER_OBJ;
160 break;
161 case ACL_GROUP:
162 ace->a_type = SMB_ACL_GROUP;
163 break;
164 case ACL_GROUP_OBJ:
165 ace->a_type = SMB_ACL_GROUP_OBJ;
166 break;
167 case ACL_OTHER:
168 ace->a_type = SMB_ACL_OTHER;
169 break;
170 case ACL_MASK:
171 ace->a_type = SMB_ACL_MASK;
172 break;
173 #ifdef HAVE_ACL_EVERYONE
174 case ACL_EVERYONE:
175 DEBUG(1, ("ACL tag type ACL_EVERYONE. FreeBSD with ZFS? Use 'vfs objects = zfsacl'\n"));
176 return false;
177 #endif
178 default:
179 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
180 return False;
182 switch(ace->a_type) {
183 case SMB_ACL_USER: {
184 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
185 if (puid == NULL) {
186 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
187 return False;
189 ace->info.user.uid = *puid;
190 acl_free(puid);
191 break;
194 case SMB_ACL_GROUP: {
195 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
196 if (pgid == NULL) {
197 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
198 return False;
200 ace->info.group.gid = *pgid;
201 acl_free(pgid);
202 break;
204 default:
205 break;
207 if (acl_get_permset(posix_ace, &permset) != 0) {
208 DEBUG(0, ("smb_acl_get_mode failed\n"));
209 return False;
211 ace->a_perm = 0;
212 #ifdef HAVE_ACL_GET_PERM_NP
213 ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
214 ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
215 ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
216 #else
217 ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
218 ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
219 ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
220 #endif
221 return True;
224 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx)
226 struct smb_acl_t *result = sys_acl_init(mem_ctx);
227 int entry_id = ACL_FIRST_ENTRY;
228 acl_entry_t e;
229 if (result == NULL) {
230 return NULL;
232 while (acl_get_entry(acl, entry_id, &e) == 1) {
234 entry_id = ACL_NEXT_ENTRY;
236 result->acl = talloc_realloc(result, result->acl,
237 struct smb_acl_entry, result->count+1);
238 if (result->acl == NULL) {
239 TALLOC_FREE(result);
240 DEBUG(0, ("talloc_realloc failed\n"));
241 errno = ENOMEM;
242 return NULL;
245 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
246 TALLOC_FREE(result);
247 return NULL;
250 result->count += 1;
252 return result;
255 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
257 int ret;
258 acl_permset_t permset;
260 if ((ret = acl_get_permset(entry, &permset)) != 0) {
261 return ret;
263 if ((ret = acl_clear_perms(permset)) != 0) {
264 return ret;
266 if ((perm & SMB_ACL_READ) &&
267 ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
268 return ret;
270 if ((perm & SMB_ACL_WRITE) &&
271 ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
272 return ret;
274 if ((perm & SMB_ACL_EXECUTE) &&
275 ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
276 return ret;
279 return 0;
282 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
284 acl_t result;
285 int i;
287 result = acl_init(acl->count);
288 if (result == NULL) {
289 DEBUG(10, ("acl_init failed\n"));
290 return NULL;
293 for (i=0; i<acl->count; i++) {
294 const struct smb_acl_entry *entry = &acl->acl[i];
295 acl_entry_t e;
296 acl_tag_t tag;
298 if (acl_create_entry(&result, &e) != 0) {
299 DEBUG(1, ("acl_create_entry failed: %s\n",
300 strerror(errno)));
301 goto fail;
304 switch (entry->a_type) {
305 case SMB_ACL_USER:
306 tag = ACL_USER;
307 break;
308 case SMB_ACL_USER_OBJ:
309 tag = ACL_USER_OBJ;
310 break;
311 case SMB_ACL_GROUP:
312 tag = ACL_GROUP;
313 break;
314 case SMB_ACL_GROUP_OBJ:
315 tag = ACL_GROUP_OBJ;
316 break;
317 case SMB_ACL_OTHER:
318 tag = ACL_OTHER;
319 break;
320 case SMB_ACL_MASK:
321 tag = ACL_MASK;
322 break;
323 default:
324 DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
325 goto fail;
328 if (acl_set_tag_type(e, tag) != 0) {
329 DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
330 tag, strerror(errno)));
331 goto fail;
334 switch (entry->a_type) {
335 case SMB_ACL_USER:
336 if (acl_set_qualifier(e, &entry->info.user.uid) != 0) {
337 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
338 strerror(errno)));
339 goto fail;
341 break;
342 case SMB_ACL_GROUP:
343 if (acl_set_qualifier(e, &entry->info.group.gid) != 0) {
344 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
345 strerror(errno)));
346 goto fail;
348 break;
349 default: /* Shut up, compiler! :-) */
350 break;
353 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
354 goto fail;
358 if (acl_valid(result) != 0) {
359 char *acl_string = sys_acl_to_text(acl, NULL);
360 DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
361 acl_string, strerror(errno)));
362 SAFE_FREE(acl_string);
363 goto fail;
366 return result;
368 fail:
369 if (result != NULL) {
370 acl_free(result);
372 return NULL;
375 /* VFS operations structure */
377 static struct vfs_fn_pointers posixacl_fns = {
378 .sys_acl_get_fd_fn = posixacl_sys_acl_get_fd,
379 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
380 .sys_acl_set_fd_fn = posixacl_sys_acl_set_fd,
381 .sys_acl_delete_def_fd_fn = posixacl_sys_acl_delete_def_fd,
384 static_decl_vfs;
385 NTSTATUS vfs_posixacl_init(TALLOC_CTX *ctx)
387 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",
388 &posixacl_fns);