shadow_copy2: add comment block explaining shadow_copy2_convert()
[Samba.git] / source3 / lib / sysacls.c
blobd77b2a6822bcd08c1b92b90131adfa9d2040bcfc
1 /*
2 Unix SMB/CIFS implementation.
3 Samba system utilities for ACL support.
4 Copyright (C) Jeremy Allison 2000.
5 Copyright (C) Volker Lendecke 2006
6 Copyright (C) Michael Adam 2006,2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/passwd.h"
25 #if defined(HAVE_POSIX_ACLS)
26 #include "modules/vfs_posixacl.h"
27 #endif
29 #if defined(HAVE_TRU64_ACLS)
30 #include "modules/vfs_tru64acl.h"
31 #endif
33 #if defined(HAVE_SOLARIS_ACLS) || defined(HAVE_UNIXWARE_ACLS)
34 #include "modules/vfs_solarisacl.h"
35 #endif
37 #if defined(HAVE_HPUX_ACLS)
38 #include "modules/vfs_hpuxacl.h"
39 #endif
41 #if defined(HAVE_IRIX_ACLS)
42 #include "modules/vfs_irixacl.h"
43 #endif
45 #undef DBGC_CLASS
46 #define DBGC_CLASS DBGC_ACLS
49 * Note that while this code implements sufficient functionality
50 * to support the sys_acl_* interfaces it does not provide all
51 * of the semantics of the POSIX ACL interfaces.
53 * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
54 * from a call to sys_acl_get_entry() should not be assumed to be
55 * valid after calling any of the following functions, which may
56 * reorder the entries in the ACL.
58 * sys_acl_valid()
59 * sys_acl_set_file()
60 * sys_acl_set_fd()
63 int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
65 if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
66 errno = EINVAL;
67 return -1;
70 if (entry_p == NULL) {
71 errno = EINVAL;
72 return -1;
75 if (entry_id == SMB_ACL_FIRST_ENTRY) {
76 acl_d->next = 0;
79 if (acl_d->next < 0) {
80 errno = EINVAL;
81 return -1;
84 if (acl_d->next >= acl_d->count) {
85 return 0;
88 *entry_p = &acl_d->acl[acl_d->next++];
90 return 1;
93 int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
95 *type_p = entry_d->a_type;
97 return 0;
100 int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
102 *permset_p = &entry_d->a_perm;
104 return 0;
107 void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
109 if (entry_d->a_type == SMB_ACL_USER) {
110 return &entry_d->info.user.uid;
113 if (entry_d->a_type == SMB_ACL_GROUP) {
114 return &entry_d->info.group.gid;
117 errno = EINVAL;
118 return NULL;
121 int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
123 *permset_d = 0;
125 return 0;
128 int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
130 if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
131 && perm != SMB_ACL_EXECUTE) {
132 errno = EINVAL;
133 return -1;
136 if (permset_d == NULL) {
137 errno = EINVAL;
138 return -1;
141 *permset_d |= perm;
143 return 0;
146 int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
148 return *permset_d & perm;
151 char *sys_acl_to_text(const struct smb_acl_t *acl_d, ssize_t *len_p)
153 int i;
154 int len, maxlen;
155 char *text;
158 * use an initial estimate of 20 bytes per ACL entry
159 * when allocating memory for the text representation
160 * of the ACL
162 len = 0;
163 maxlen = 20 * acl_d->count;
164 if ((text = (char *)SMB_MALLOC(maxlen)) == NULL) {
165 errno = ENOMEM;
166 return NULL;
169 for (i = 0; i < acl_d->count; i++) {
170 struct smb_acl_entry *ap = &acl_d->acl[i];
171 struct group *gr;
172 char tagbuf[12];
173 char idbuf[12];
174 const char *tag;
175 const char *id = "";
176 char perms[4];
177 int nbytes;
179 switch (ap->a_type) {
181 * for debugging purposes it's probably more
182 * useful to dump unknown tag types rather
183 * than just returning an error
185 default:
186 slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
187 ap->a_type);
188 tag = tagbuf;
189 break;
191 case SMB_ACL_USER:
192 id = uidtoname(ap->info.user.uid);
193 case SMB_ACL_USER_OBJ:
194 tag = "user";
195 break;
197 case SMB_ACL_GROUP:
198 if ((gr = getgrgid(ap->info.group.gid)) == NULL) {
199 slprintf(idbuf, sizeof(idbuf)-1, "%ld",
200 (long)ap->info.group.gid);
201 id = idbuf;
202 } else {
203 id = gr->gr_name;
205 case SMB_ACL_GROUP_OBJ:
206 tag = "group";
207 break;
209 case SMB_ACL_OTHER:
210 tag = "other";
211 break;
213 case SMB_ACL_MASK:
214 tag = "mask";
215 break;
219 perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
220 perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
221 perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
222 perms[3] = '\0';
224 /* <tag> : <qualifier> : rwx \n \0 */
225 nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
228 * If this entry would overflow the buffer
229 * allocate enough additional memory for this
230 * entry and an estimate of another 20 bytes
231 * for each entry still to be processed
233 if ((len + nbytes) > maxlen) {
234 maxlen += nbytes + 20 * (acl_d->count - i);
235 if ((text = (char *)SMB_REALLOC(text, maxlen)) == NULL) {
236 errno = ENOMEM;
237 return NULL;
242 slprintf(&text[len], nbytes, "%s:%s:%s\n", tag, id, perms);
243 len += (nbytes - 1);
246 if (len_p)
247 *len_p = len;
249 return text;
252 SMB_ACL_T sys_acl_init(TALLOC_CTX *mem_ctx)
254 SMB_ACL_T a;
256 if ((a = talloc(mem_ctx, struct smb_acl_t)) == NULL) {
257 errno = ENOMEM;
258 return NULL;
261 a->count = 0;
262 a->next = -1;
264 a->acl = talloc_array(a, struct smb_acl_entry, 0);
265 if (!a->acl) {
266 TALLOC_FREE(a);
267 errno = ENOMEM;
268 return NULL;
271 return a;
274 int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
276 SMB_ACL_T acl_d;
277 SMB_ACL_ENTRY_T entry_d;
278 struct smb_acl_entry *acl;
280 if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
281 errno = EINVAL;
282 return -1;
285 acl = talloc_realloc(acl_d, acl_d->acl, struct smb_acl_entry, acl_d->count+1);
286 if (!acl) {
287 errno = ENOMEM;
288 return -1;
290 acl_d->acl = acl;
291 entry_d = &acl_d->acl[acl_d->count];
292 entry_d->a_type = SMB_ACL_TAG_INVALID;
293 entry_d->a_perm = 0;
294 *entry_p = entry_d;
296 acl_d->count++;
297 return 0;
300 int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
302 switch (tag_type) {
303 case SMB_ACL_USER:
304 case SMB_ACL_USER_OBJ:
305 case SMB_ACL_GROUP:
306 case SMB_ACL_GROUP_OBJ:
307 case SMB_ACL_OTHER:
308 case SMB_ACL_MASK:
309 entry_d->a_type = tag_type;
310 break;
311 default:
312 errno = EINVAL;
313 return -1;
316 return 0;
319 int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
321 if (entry_d->a_type == SMB_ACL_USER) {
322 entry_d->info.user.uid = *((uid_t *)qual_p);
323 return 0;
325 if (entry_d->a_type == SMB_ACL_GROUP) {
326 entry_d->info.group.gid = *((gid_t *)qual_p);
327 return 0;
330 errno = EINVAL;
331 return -1;
334 int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
336 if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
337 errno = EINVAL;
338 return -1;
341 entry_d->a_perm = *permset_d;
343 return 0;
346 int sys_acl_free_text(char *text)
348 SAFE_FREE(text);
349 return 0;
352 int sys_acl_valid(SMB_ACL_T acl_d)
354 errno = EINVAL;
355 return -1;
359 * acl_get_file, acl_get_fd, acl_set_file, acl_set_fd and
360 * sys_acl_delete_def_file are to be redirected to the default
361 * statically-bound acl vfs module, but they are replacable.
364 #if defined(HAVE_POSIX_ACLS)
366 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
367 const char *path_p, SMB_ACL_TYPE_T type, TALLOC_CTX *mem_ctx)
369 return posixacl_sys_acl_get_file(handle, path_p, type, mem_ctx);
372 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp, TALLOC_CTX *mem_ctx)
374 return posixacl_sys_acl_get_fd(handle, fsp, mem_ctx);
377 int sys_acl_set_file(vfs_handle_struct *handle,
378 const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
380 return posixacl_sys_acl_set_file(handle, name, type, acl_d);
383 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
384 SMB_ACL_T acl_d)
386 return posixacl_sys_acl_set_fd(handle, fsp, acl_d);
389 int sys_acl_delete_def_file(vfs_handle_struct *handle,
390 const char *path)
392 return posixacl_sys_acl_delete_def_file(handle, path);
395 #elif defined(HAVE_AIX_ACLS)
397 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
398 const char *path_p, SMB_ACL_TYPE_T type,
399 TALLOC_CTX *mem_ctx)
401 return aixacl_sys_acl_get_file(handle, path_p, type, mem_ctx);
404 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
405 TALLOC_CTX *mem_ctx)
407 return aixacl_sys_acl_get_fd(handle, fsp, mem_ctx);
410 int sys_acl_set_file(vfs_handle_struct *handle,
411 const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
413 return aixacl_sys_acl_set_file(handle, name, type, acl_d);
416 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
417 SMB_ACL_T acl_d)
419 return aixacl_sys_acl_set_fd(handle, fsp, acl_d);
422 int sys_acl_delete_def_file(vfs_handle_struct *handle,
423 const char *path)
425 return aixacl_sys_acl_delete_def_file(handle, path);
428 #elif defined(HAVE_TRU64_ACLS)
430 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
431 const char *path_p, SMB_ACL_TYPE_T type,
432 TALLOC_CTX *mem_ctx)
434 return tru64acl_sys_acl_get_file(handle, path_p, type,
435 mem_ctx);
438 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
439 TALLOC_CTX *mem_ctx)
441 return tru64acl_sys_acl_get_fd(handle, fsp, mem_ctx);
444 int sys_acl_set_file(vfs_handle_struct *handle,
445 const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
447 return tru64acl_sys_acl_set_file(handle, name, type, acl_d);
450 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
451 SMB_ACL_T acl_d)
453 return tru64acl_sys_acl_set_fd(handle, fsp, acl_d);
456 int sys_acl_delete_def_file(vfs_handle_struct *handle,
457 const char *path)
459 return tru64acl_sys_acl_delete_def_file(handle, path);
462 #elif defined(HAVE_SOLARIS_ACLS) || defined(HAVE_UNIXWARE_ACLS)
464 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
465 const char *path_p, SMB_ACL_TYPE_T type,
466 TALLOC_CTX *mem_ctx)
468 return solarisacl_sys_acl_get_file(handle, path_p, type,
469 mem_ctx);
472 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
473 TALLOC_CTX *mem_ctx)
475 return solarisacl_sys_acl_get_fd(handle, fsp,
476 mem_ctx);
479 int sys_acl_set_file(vfs_handle_struct *handle,
480 const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
482 return solarisacl_sys_acl_set_file(handle, name, type, acl_d);
485 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
486 SMB_ACL_T acl_d)
488 return solarisacl_sys_acl_set_fd(handle, fsp, acl_d);
491 int sys_acl_delete_def_file(vfs_handle_struct *handle,
492 const char *path)
494 return solarisacl_sys_acl_delete_def_file(handle, path);
497 #elif defined(HAVE_HPUX_ACLS)
499 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
500 const char *path_p, SMB_ACL_TYPE_T type,
501 TALLOC_CTX *mem_ctx)
503 return hpuxacl_sys_acl_get_file(handle, path_p, type, mem_ctx);
506 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
507 TALLOC_CTX *mem_ctx)
509 return hpuxacl_sys_acl_get_fd(handle, fsp, mem_ctx);
512 int sys_acl_set_file(vfs_handle_struct *handle,
513 const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
515 return hpuxacl_sys_acl_set_file(handle, name, type, acl_d);
518 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
519 SMB_ACL_T acl_d)
521 return hpuxacl_sys_acl_set_fd(handle, fsp, acl_d);
524 int sys_acl_delete_def_file(vfs_handle_struct *handle,
525 const char *path)
527 return hpuxacl_sys_acl_delete_def_file(handle, path);
530 #elif defined(HAVE_IRIX_ACLS)
532 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
533 const char *path_p, SMB_ACL_TYPE_T type,
534 TALLOC_CTX *mem_ctx)
536 return irixacl_sys_acl_get_file(handle, path_p, type, mem_ctx);
539 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
540 TALLOC_CTX *mem_ctx)
542 return irixacl_sys_acl_get_fd(handle, fsp, mem_ctx);
545 int sys_acl_set_file(vfs_handle_struct *handle,
546 const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
548 return irixacl_sys_acl_set_file(handle, name, type, acl_d);
551 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
552 SMB_ACL_T acl_d)
554 return irixacl_sys_acl_set_fd(handle, fsp, acl_d);
557 int sys_acl_delete_def_file(vfs_handle_struct *handle,
558 const char *path)
560 return irixacl_sys_acl_delete_def_file(handle, path);
563 #else /* No ACLs. */
565 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
566 const char *path_p, SMB_ACL_TYPE_T type,
567 TALLOC_CTX *mem_ctx)
569 #ifdef ENOTSUP
570 errno = ENOTSUP;
571 #else
572 errno = ENOSYS;
573 #endif
574 return NULL;
577 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
578 TALLOC_CTX *mem_ctx)
580 #ifdef ENOTSUP
581 errno = ENOTSUP;
582 #else
583 errno = ENOSYS;
584 #endif
585 return NULL;
588 int sys_acl_set_file(vfs_handle_struct *handle,
589 const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
591 #ifdef ENOTSUP
592 errno = ENOTSUP;
593 #else
594 errno = ENOSYS;
595 #endif
596 return -1;
599 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
600 SMB_ACL_T acl_d)
602 #ifdef ENOTSUP
603 errno = ENOTSUP;
604 #else
605 errno = ENOSYS;
606 #endif
607 return -1;
610 int sys_acl_delete_def_file(vfs_handle_struct *handle,
611 const char *path)
613 #ifdef ENOTSUP
614 errno = ENOTSUP;
615 #else
616 errno = ENOSYS;
617 #endif
618 return -1;
621 #endif
623 /************************************************************************
624 Deliberately outside the ACL defines. Return 1 if this is a "no acls"
625 errno, 0 if not.
626 ************************************************************************/
628 int no_acl_syscall_error(int err)
630 #if defined(ENOSYS)
631 if (err == ENOSYS) {
632 return 1;
634 #endif
635 #if defined(ENOTSUP)
636 if (err == ENOTSUP) {
637 return 1;
639 #endif
640 return 0;