Make sure that wbcLookupSid() and wbcLookupRids() use talloc()'d memory.
[Samba/gebeck_regimport.git] / source3 / modules / vfs_posixacl.c
blobfb0c0bc8e906cf176f43ed2cf2028756ccd10d3e
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"
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,
35 const char *path_p,
36 SMB_ACL_TYPE_T type)
38 struct smb_acl_t *result;
39 acl_type_t acl_type;
40 acl_t acl;
42 switch(type) {
43 case SMB_ACL_TYPE_ACCESS:
44 acl_type = ACL_TYPE_ACCESS;
45 break;
46 case SMB_ACL_TYPE_DEFAULT:
47 acl_type = ACL_TYPE_DEFAULT;
48 break;
49 default:
50 errno = EINVAL;
51 return NULL;
54 acl = acl_get_file(path_p, acl_type);
56 if (acl == NULL) {
57 return NULL;
60 result = smb_acl_to_internal(acl);
61 acl_free(acl);
62 return result;
65 SMB_ACL_T posixacl_sys_acl_get_fd(vfs_handle_struct *handle,
66 files_struct *fsp,
67 int fd)
69 struct smb_acl_t *result;
70 acl_t acl = acl_get_fd(fd);
72 if (acl == NULL) {
73 return NULL;
76 result = smb_acl_to_internal(acl);
77 acl_free(acl);
78 return result;
81 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
82 const char *name,
83 SMB_ACL_TYPE_T type,
84 SMB_ACL_T theacl)
86 int res;
87 acl_type_t acl_type;
88 acl_t acl;
90 DEBUG(10, ("Calling acl_set_file: %s, %d\n", name, type));
92 switch(type) {
93 case SMB_ACL_TYPE_ACCESS:
94 acl_type = ACL_TYPE_ACCESS;
95 break;
96 case SMB_ACL_TYPE_DEFAULT:
97 acl_type = ACL_TYPE_DEFAULT;
98 break;
99 default:
100 errno = EINVAL;
101 return -1;
104 if ((acl = smb_acl_to_posix(theacl)) == NULL) {
105 return -1;
107 res = acl_set_file(name, acl_type, acl);
108 if (res != 0) {
109 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
111 acl_free(acl);
112 return res;
115 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
116 files_struct *fsp,
117 int fd, SMB_ACL_T theacl)
119 int res;
120 acl_t acl = smb_acl_to_posix(theacl);
121 if (acl == NULL) {
122 return -1;
124 res = acl_set_fd(fd, acl);
125 acl_free(acl);
126 return res;
129 int posixacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
130 const char *path)
132 return acl_delete_def_file(path);
136 /* private functions */
138 static bool smb_ace_to_internal(acl_entry_t posix_ace,
139 struct smb_acl_entry *ace)
141 acl_tag_t tag;
142 acl_permset_t permset;
144 if (acl_get_tag_type(posix_ace, &tag) != 0) {
145 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
146 return False;
149 switch(tag) {
150 case ACL_USER:
151 ace->a_type = SMB_ACL_USER;
152 break;
153 case ACL_USER_OBJ:
154 ace->a_type = SMB_ACL_USER_OBJ;
155 break;
156 case ACL_GROUP:
157 ace->a_type = SMB_ACL_GROUP;
158 break;
159 case ACL_GROUP_OBJ:
160 ace->a_type = SMB_ACL_GROUP_OBJ;
161 break;
162 case ACL_OTHER:
163 ace->a_type = SMB_ACL_OTHER;
164 break;
165 case ACL_MASK:
166 ace->a_type = SMB_ACL_MASK;
167 break;
168 default:
169 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
170 return False;
172 switch(ace->a_type) {
173 case SMB_ACL_USER: {
174 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
175 if (puid == NULL) {
176 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
177 return False;
179 ace->uid = *puid;
180 acl_free(puid);
181 break;
184 case SMB_ACL_GROUP: {
185 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
186 if (pgid == NULL) {
187 DEBUG(0, ("smb_acl_get_qualifier failed\n"));
188 return False;
190 ace->gid = *pgid;
191 acl_free(pgid);
192 break;
194 default:
195 break;
197 if (acl_get_permset(posix_ace, &permset) != 0) {
198 DEBUG(0, ("smb_acl_get_mode failed\n"));
199 return False;
201 ace->a_perm = 0;
202 #ifdef HAVE_ACL_GET_PERM_NP
203 ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
204 ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
205 ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
206 #else
207 ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
208 ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
209 ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
210 #endif
211 return True;
214 static struct smb_acl_t *smb_acl_to_internal(acl_t acl)
216 struct smb_acl_t *result = SMB_MALLOC_P(struct smb_acl_t);
217 int entry_id = ACL_FIRST_ENTRY;
218 acl_entry_t e;
219 if (result == NULL) {
220 return NULL;
222 ZERO_STRUCTP(result);
223 while (acl_get_entry(acl, entry_id, &e) == 1) {
225 entry_id = ACL_NEXT_ENTRY;
227 result = (struct smb_acl_t *)SMB_REALLOC(
228 result, sizeof(struct smb_acl_t) +
229 (sizeof(struct smb_acl_entry) * (result->count+1)));
230 if (result == NULL) {
231 DEBUG(0, ("SMB_REALLOC failed\n"));
232 errno = ENOMEM;
233 return NULL;
236 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
237 SAFE_FREE(result);
238 return NULL;
241 result->count += 1;
243 return result;
246 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
248 int ret;
249 acl_permset_t permset;
251 if ((ret = acl_get_permset(entry, &permset)) != 0) {
252 return ret;
254 if ((ret = acl_clear_perms(permset)) != 0) {
255 return ret;
257 if ((perm & SMB_ACL_READ) &&
258 ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
259 return ret;
261 if ((perm & SMB_ACL_WRITE) &&
262 ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
263 return ret;
265 if ((perm & SMB_ACL_EXECUTE) &&
266 ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
267 return ret;
269 return acl_set_permset(entry, permset);
272 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
274 acl_t result;
275 int i;
277 result = acl_init(acl->count);
278 if (result == NULL) {
279 DEBUG(10, ("acl_init failed\n"));
280 return NULL;
283 for (i=0; i<acl->count; i++) {
284 const struct smb_acl_entry *entry = &acl->acl[i];
285 acl_entry_t e;
286 acl_tag_t tag;
288 if (acl_create_entry(&result, &e) != 0) {
289 DEBUG(1, ("acl_create_entry failed: %s\n",
290 strerror(errno)));
291 goto fail;
294 switch (entry->a_type) {
295 case SMB_ACL_USER:
296 tag = ACL_USER;
297 break;
298 case SMB_ACL_USER_OBJ:
299 tag = ACL_USER_OBJ;
300 break;
301 case SMB_ACL_GROUP:
302 tag = ACL_GROUP;
303 break;
304 case SMB_ACL_GROUP_OBJ:
305 tag = ACL_GROUP_OBJ;
306 break;
307 case SMB_ACL_OTHER:
308 tag = ACL_OTHER;
309 break;
310 case SMB_ACL_MASK:
311 tag = ACL_MASK;
312 break;
313 default:
314 DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
315 goto fail;
318 if (acl_set_tag_type(e, tag) != 0) {
319 DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
320 tag, strerror(errno)));
321 goto fail;
324 switch (entry->a_type) {
325 case SMB_ACL_USER:
326 if (acl_set_qualifier(e, &entry->uid) != 0) {
327 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
328 strerror(errno)));
329 goto fail;
331 break;
332 case SMB_ACL_GROUP:
333 if (acl_set_qualifier(e, &entry->gid) != 0) {
334 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
335 strerror(errno)));
336 goto fail;
338 break;
339 default: /* Shut up, compiler! :-) */
340 break;
343 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
344 goto fail;
348 if (acl_valid(result) != 0) {
349 DEBUG(0, ("smb_acl_to_posix: ACL is invalid for set (%s)\n",
350 strerror(errno)));
351 goto fail;
354 return result;
356 fail:
357 if (result != NULL) {
358 acl_free(result);
360 return NULL;
363 /* VFS operations structure */
365 static vfs_op_tuple posixacl_op_tuples[] = {
366 /* Disk operations */
367 {SMB_VFS_OP(posixacl_sys_acl_get_file),
368 SMB_VFS_OP_SYS_ACL_GET_FILE,
369 SMB_VFS_LAYER_TRANSPARENT},
371 {SMB_VFS_OP(posixacl_sys_acl_get_fd),
372 SMB_VFS_OP_SYS_ACL_GET_FD,
373 SMB_VFS_LAYER_TRANSPARENT},
375 {SMB_VFS_OP(posixacl_sys_acl_set_file),
376 SMB_VFS_OP_SYS_ACL_SET_FILE,
377 SMB_VFS_LAYER_TRANSPARENT},
379 {SMB_VFS_OP(posixacl_sys_acl_set_fd),
380 SMB_VFS_OP_SYS_ACL_SET_FD,
381 SMB_VFS_LAYER_TRANSPARENT},
383 {SMB_VFS_OP(posixacl_sys_acl_delete_def_file),
384 SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
385 SMB_VFS_LAYER_TRANSPARENT},
387 {SMB_VFS_OP(NULL),
388 SMB_VFS_OP_NOOP,
389 SMB_VFS_LAYER_NOOP}
392 NTSTATUS vfs_posixacl_init(void);
393 NTSTATUS vfs_posixacl_init(void)
395 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",
396 posixacl_op_tuples);