s3: smbd: Remove all references to utility and backend functions supporting sys_acl_d...
[Samba.git] / source3 / lib / sysacls.c
blobe41f88b12855728764d4ba420221f590f5d421b7
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_SOLARIS_UNIXWARE_ACLS)
30 #include "modules/vfs_solarisacl.h"
31 #endif
33 #if defined(HAVE_HPUX_ACLS)
34 #include "modules/vfs_hpuxacl.h"
35 #endif
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_ACLS
41 * Note that while this code implements sufficient functionality
42 * to support the sys_acl_* interfaces it does not provide all
43 * of the semantics of the POSIX ACL interfaces.
45 * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
46 * from a call to sys_acl_get_entry() should not be assumed to be
47 * valid after calling any of the following functions, which may
48 * reorder the entries in the ACL.
50 * sys_acl_valid()
51 * sys_acl_set_file()
52 * sys_acl_set_fd()
55 int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
57 if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
58 errno = EINVAL;
59 return -1;
62 if (entry_p == NULL) {
63 errno = EINVAL;
64 return -1;
67 if (entry_id == SMB_ACL_FIRST_ENTRY) {
68 acl_d->next = 0;
71 if (acl_d->next < 0) {
72 errno = EINVAL;
73 return -1;
76 if (acl_d->next >= acl_d->count) {
77 return 0;
80 *entry_p = &acl_d->acl[acl_d->next++];
82 return 1;
85 int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
87 *type_p = entry_d->a_type;
89 return 0;
92 int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
94 *permset_p = &entry_d->a_perm;
96 return 0;
99 void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
101 if (entry_d->a_type == SMB_ACL_USER) {
102 return &entry_d->info.user.uid;
105 if (entry_d->a_type == SMB_ACL_GROUP) {
106 return &entry_d->info.group.gid;
109 errno = EINVAL;
110 return NULL;
113 int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
115 *permset_d = 0;
117 return 0;
120 int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
122 if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
123 && perm != SMB_ACL_EXECUTE) {
124 errno = EINVAL;
125 return -1;
128 if (permset_d == NULL) {
129 errno = EINVAL;
130 return -1;
133 *permset_d |= perm;
135 return 0;
138 int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
140 return *permset_d & perm;
143 char *sys_acl_to_text(const struct smb_acl_t *acl_d, ssize_t *len_p)
145 int i;
146 int len, maxlen;
147 char *text;
150 * use an initial estimate of 20 bytes per ACL entry
151 * when allocating memory for the text representation
152 * of the ACL
154 len = 0;
155 maxlen = 20 * acl_d->count;
156 if ((text = (char *)SMB_MALLOC(maxlen)) == NULL) {
157 errno = ENOMEM;
158 return NULL;
161 for (i = 0; i < acl_d->count; i++) {
162 struct smb_acl_entry *ap = &acl_d->acl[i];
163 struct group *gr;
164 char tagbuf[12];
165 char idbuf[12];
166 const char *tag;
167 const char *id = "";
168 char perms[4];
169 int nbytes;
171 switch (ap->a_type) {
173 * for debugging purposes it's probably more
174 * useful to dump unknown tag types rather
175 * than just returning an error
177 default:
178 slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
179 ap->a_type);
180 tag = tagbuf;
181 break;
183 case SMB_ACL_USER:
184 id = uidtoname(ap->info.user.uid);
186 FALL_THROUGH;
187 case SMB_ACL_USER_OBJ:
188 tag = "user";
189 break;
191 case SMB_ACL_GROUP:
192 if ((gr = getgrgid(ap->info.group.gid)) == NULL) {
193 slprintf(idbuf, sizeof(idbuf)-1, "%ld",
194 (long)ap->info.group.gid);
195 id = idbuf;
196 } else {
197 id = gr->gr_name;
200 FALL_THROUGH;
201 case SMB_ACL_GROUP_OBJ:
202 tag = "group";
203 break;
205 case SMB_ACL_OTHER:
206 tag = "other";
207 break;
209 case SMB_ACL_MASK:
210 tag = "mask";
211 break;
215 perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
216 perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
217 perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
218 perms[3] = '\0';
220 /* <tag> : <qualifier> : rwx \n \0 */
221 nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
224 * If this entry would overflow the buffer
225 * allocate enough additional memory for this
226 * entry and an estimate of another 20 bytes
227 * for each entry still to be processed
229 if ((len + nbytes) > maxlen) {
230 maxlen += nbytes + 20 * (acl_d->count - i);
231 if ((text = (char *)SMB_REALLOC(text, maxlen)) == NULL) {
232 errno = ENOMEM;
233 return NULL;
238 slprintf(&text[len], nbytes, "%s:%s:%s\n", tag, id, perms);
239 len += (nbytes - 1);
242 if (len_p)
243 *len_p = len;
245 return text;
248 SMB_ACL_T sys_acl_init(TALLOC_CTX *mem_ctx)
250 SMB_ACL_T a;
252 if ((a = talloc(mem_ctx, struct smb_acl_t)) == NULL) {
253 errno = ENOMEM;
254 return NULL;
257 a->count = 0;
258 a->next = -1;
260 a->acl = talloc_array(a, struct smb_acl_entry, 0);
261 if (!a->acl) {
262 TALLOC_FREE(a);
263 errno = ENOMEM;
264 return NULL;
267 return a;
270 int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
272 SMB_ACL_T acl_d;
273 SMB_ACL_ENTRY_T entry_d;
274 struct smb_acl_entry *acl;
276 if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
277 errno = EINVAL;
278 return -1;
281 acl = talloc_realloc(acl_d, acl_d->acl, struct smb_acl_entry, acl_d->count+1);
282 if (!acl) {
283 errno = ENOMEM;
284 return -1;
286 acl_d->acl = acl;
287 entry_d = &acl_d->acl[acl_d->count];
288 entry_d->a_type = SMB_ACL_TAG_INVALID;
289 entry_d->a_perm = 0;
290 *entry_p = entry_d;
292 acl_d->count++;
293 return 0;
296 int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
298 switch (tag_type) {
299 case SMB_ACL_USER:
300 case SMB_ACL_USER_OBJ:
301 case SMB_ACL_GROUP:
302 case SMB_ACL_GROUP_OBJ:
303 case SMB_ACL_OTHER:
304 case SMB_ACL_MASK:
305 entry_d->a_type = tag_type;
306 break;
307 default:
308 errno = EINVAL;
309 return -1;
312 return 0;
315 int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
317 if (entry_d->a_type == SMB_ACL_USER) {
318 entry_d->info.user.uid = *((uid_t *)qual_p);
319 return 0;
321 if (entry_d->a_type == SMB_ACL_GROUP) {
322 entry_d->info.group.gid = *((gid_t *)qual_p);
323 return 0;
326 errno = EINVAL;
327 return -1;
330 int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
332 if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
333 errno = EINVAL;
334 return -1;
337 entry_d->a_perm = *permset_d;
339 return 0;
342 int sys_acl_free_text(char *text)
344 SAFE_FREE(text);
345 return 0;
348 int sys_acl_valid(SMB_ACL_T acl_d)
350 errno = EINVAL;
351 return -1;
355 * acl_get_file, acl_get_fd, acl_set_file, acl_set_fd and
356 * sys_acl_delete_def_fd are to be redirected to the default
357 * statically-bound acl vfs module, but they are replacable.
360 #if defined(HAVE_POSIX_ACLS)
362 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
363 const struct smb_filename *smb_fname,
364 SMB_ACL_TYPE_T type,
365 TALLOC_CTX *mem_ctx)
367 return posixacl_sys_acl_get_file(handle, smb_fname, type, mem_ctx);
370 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp, TALLOC_CTX *mem_ctx)
372 return posixacl_sys_acl_get_fd(handle, fsp, mem_ctx);
375 int sys_acl_set_file(vfs_handle_struct *handle,
376 const struct smb_filename *smb_fname,
377 SMB_ACL_TYPE_T type,
378 SMB_ACL_T acl_d)
380 return posixacl_sys_acl_set_file(handle, smb_fname, 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, SMB_ACL_TYPE_ACCESS, acl_d);
389 int sys_acl_delete_def_fd(vfs_handle_struct *handle,
390 files_struct *fsp)
392 return posixacl_sys_acl_delete_def_fd(handle, fsp);
395 #elif defined(HAVE_AIX_ACLS)
397 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
398 const struct smb_filename *smb_fname,
399 SMB_ACL_TYPE_T type,
400 TALLOC_CTX *mem_ctx)
402 return aixacl_sys_acl_get_file(handle, smb_fname, type, mem_ctx);
405 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
406 TALLOC_CTX *mem_ctx)
408 return aixacl_sys_acl_get_fd(handle, fsp, mem_ctx);
411 int sys_acl_set_file(vfs_handle_struct *handle,
412 const struct smb_filename *smb_fname,
413 SMB_ACL_TYPE_T type,
414 SMB_ACL_T acl_d)
416 return aixacl_sys_acl_set_file(handle, smb_fname, type, acl_d);
419 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
420 SMB_ACL_T acl_d)
422 return aixacl_sys_acl_set_fd(handle, fsp, acl_d);
425 int sys_acl_delete_def_fd(vfs_handle_struct *handle,
426 files_struct *fsp)
428 return aixacl_sys_acl_delete_def_fd(handle, fsp);
430 #elif defined(HAVE_SOLARIS_UNIXWARE_ACLS)
432 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
433 const struct smb_filename *smb_fname,
434 SMB_ACL_TYPE_T type,
435 TALLOC_CTX *mem_ctx)
437 return solarisacl_sys_acl_get_file(handle, smb_fname, type,
438 mem_ctx);
441 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
442 TALLOC_CTX *mem_ctx)
444 return solarisacl_sys_acl_get_fd(handle, fsp,
445 mem_ctx);
448 int sys_acl_set_file(vfs_handle_struct *handle,
449 const struct smb_filename *smb_fname,
450 SMB_ACL_TYPE_T type,
451 SMB_ACL_T acl_d)
453 return solarisacl_sys_acl_set_file(handle, smb_fname, type, acl_d);
456 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
457 SMB_ACL_T acl_d)
459 return solarisacl_sys_acl_set_fd(handle, fsp, acl_d);
462 int sys_acl_delete_def_fd(vfs_handle_struct *handle,
463 files_struct *fsp)
465 return solarisacl_sys_acl_delete_def_fd(handle, fsp);
467 #elif defined(HAVE_HPUX_ACLS)
469 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
470 const struct smb_filename *smb_fname,
471 SMB_ACL_TYPE_T type,
472 TALLOC_CTX *mem_ctx)
474 return hpuxacl_sys_acl_get_file(handle, smb_fname, type, mem_ctx);
477 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
478 TALLOC_CTX *mem_ctx)
480 return hpuxacl_sys_acl_get_fd(handle, fsp, mem_ctx);
483 int sys_acl_set_file(vfs_handle_struct *handle,
484 const struct smb_filename *smb_fname,
485 SMB_ACL_TYPE_T type,
486 SMB_ACL_T acl_d)
488 return hpuxacl_sys_acl_set_file(handle, smb_fname, type, acl_d);
491 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
492 SMB_ACL_T acl_d)
494 return hpuxacl_sys_acl_set_fd(handle, fsp, acl_d);
497 int sys_acl_delete_def_fd(vfs_handle_struct *handle,
498 files_struct *fsp)
500 return hpuxacl_sys_acl_delete_def_fd(handle, fsp);
502 #else /* No ACLs. */
504 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
505 const struct smb_filename *smb_fname,
506 SMB_ACL_TYPE_T type,
507 TALLOC_CTX *mem_ctx)
509 #ifdef ENOTSUP
510 errno = ENOTSUP;
511 #else
512 errno = ENOSYS;
513 #endif
514 return NULL;
517 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
518 TALLOC_CTX *mem_ctx)
520 #ifdef ENOTSUP
521 errno = ENOTSUP;
522 #else
523 errno = ENOSYS;
524 #endif
525 return NULL;
528 int sys_acl_set_file(vfs_handle_struct *handle,
529 const struct smb_filename *smb_fname,
530 SMB_ACL_TYPE_T type,
531 SMB_ACL_T acl_d)
533 #ifdef ENOTSUP
534 errno = ENOTSUP;
535 #else
536 errno = ENOSYS;
537 #endif
538 return -1;
541 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
542 SMB_ACL_T acl_d)
544 #ifdef ENOTSUP
545 errno = ENOTSUP;
546 #else
547 errno = ENOSYS;
548 #endif
549 return -1;
552 int sys_acl_delete_def_fd(vfs_handle_struct *handle,
553 files_struct *fsp)
555 #ifdef ENOTSUP
556 errno = ENOTSUP;
557 #else
558 errno = ENOSYS;
559 #endif
560 return -1;
562 #endif
564 /************************************************************************
565 Deliberately outside the ACL defines. Return 1 if this is a "no acls"
566 errno, 0 if not.
567 ************************************************************************/
569 int no_acl_syscall_error(int err)
571 #if defined(ENOSYS)
572 if (err == ENOSYS) {
573 return 1;
575 #endif
576 #if defined(ENOTSUP)
577 if (err == ENOTSUP) {
578 return 1;
580 #endif
581 return 0;