0bf3c37edfa517dd08680dccbf79a492ec4c67b8
[amitay/samba.git] / source3 / lib / sysacls.c
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
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "system/passwd.h"
24
25 #if defined(HAVE_POSIX_ACLS)
26 #include "modules/vfs_posixacl.h"
27 #endif
28
29 #if defined(HAVE_TRU64_ACLS)
30 #include "modules/vfs_tru64acl.h"
31 #endif
32
33 #if defined(HAVE_SOLARIS_UNIXWARE_ACLS)
34 #include "modules/vfs_solarisacl.h"
35 #endif
36
37 #if defined(HAVE_HPUX_ACLS)
38 #include "modules/vfs_hpuxacl.h"
39 #endif
40
41 #undef  DBGC_CLASS
42 #define DBGC_CLASS DBGC_ACLS
43
44 /*
45  * Note that while this code implements sufficient functionality
46  * to support the sys_acl_* interfaces it does not provide all
47  * of the semantics of the POSIX ACL interfaces.
48  *
49  * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
50  * from a call to sys_acl_get_entry() should not be assumed to be
51  * valid after calling any of the following functions, which may
52  * reorder the entries in the ACL.
53  *
54  *      sys_acl_valid()
55  *      sys_acl_set_file()
56  *      sys_acl_set_fd()
57  */
58
59 int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
60 {
61         if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
62                 errno = EINVAL;
63                 return -1;
64         }
65
66         if (entry_p == NULL) {
67                 errno = EINVAL;
68                 return -1;
69         }
70
71         if (entry_id == SMB_ACL_FIRST_ENTRY) {
72                 acl_d->next = 0;
73         }
74
75         if (acl_d->next < 0) {
76                 errno = EINVAL;
77                 return -1;
78         }
79
80         if (acl_d->next >= acl_d->count) {
81                 return 0;
82         }
83
84         *entry_p = &acl_d->acl[acl_d->next++];
85
86         return 1;
87 }
88
89 int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
90 {
91         *type_p = entry_d->a_type;
92
93         return 0;
94 }
95
96 int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
97 {
98         *permset_p = &entry_d->a_perm;
99
100         return 0;
101 }
102
103 void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
104 {
105         if (entry_d->a_type == SMB_ACL_USER) {
106                 return &entry_d->info.user.uid;
107         }
108
109         if (entry_d->a_type == SMB_ACL_GROUP) {
110                 return &entry_d->info.group.gid;
111         }
112
113         errno = EINVAL;
114         return NULL;
115 }
116
117 int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
118 {
119         *permset_d = 0;
120
121         return 0;
122 }
123
124 int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
125 {
126         if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
127             && perm != SMB_ACL_EXECUTE) {
128                 errno = EINVAL;
129                 return -1;
130         }
131
132         if (permset_d == NULL) {
133                 errno = EINVAL;
134                 return -1;
135         }
136
137         *permset_d |= perm;
138
139         return 0;
140 }
141
142 int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
143 {
144         return *permset_d & perm;
145 }
146
147 char *sys_acl_to_text(const struct smb_acl_t *acl_d, ssize_t *len_p)
148 {
149         int     i;
150         int     len, maxlen;
151         char    *text;
152
153         /*
154          * use an initial estimate of 20 bytes per ACL entry
155          * when allocating memory for the text representation
156          * of the ACL
157          */
158         len     = 0;
159         maxlen  = 20 * acl_d->count;
160         if ((text = (char *)SMB_MALLOC(maxlen)) == NULL) {
161                 errno = ENOMEM;
162                 return NULL;
163         }
164
165         for (i = 0; i < acl_d->count; i++) {
166                 struct smb_acl_entry *ap = &acl_d->acl[i];
167                 struct group    *gr;
168                 char            tagbuf[12];
169                 char            idbuf[12];
170                 const char      *tag;
171                 const char      *id     = "";
172                 char            perms[4];
173                 int             nbytes;
174
175                 switch (ap->a_type) {
176                         /*
177                          * for debugging purposes it's probably more
178                          * useful to dump unknown tag types rather
179                          * than just returning an error
180                          */
181                         default:
182                                 slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
183                                          ap->a_type);
184                                 tag = tagbuf;
185                                 break;
186
187                         case SMB_ACL_USER:
188                                 id = uidtoname(ap->info.user.uid);
189                                 /* FALL TROUGH */
190                         case SMB_ACL_USER_OBJ:
191                                 tag = "user";
192                                 break;
193
194                         case SMB_ACL_GROUP:
195                                 if ((gr = getgrgid(ap->info.group.gid)) == NULL) {
196                                         slprintf(idbuf, sizeof(idbuf)-1, "%ld",
197                                                 (long)ap->info.group.gid);
198                                         id = idbuf;
199                                 } else {
200                                         id = gr->gr_name;
201                                 }
202                                 /* FALL TROUGH */
203                         case SMB_ACL_GROUP_OBJ:
204                                 tag = "group";
205                                 break;
206
207                         case SMB_ACL_OTHER:
208                                 tag = "other";
209                                 break;
210
211                         case SMB_ACL_MASK:
212                                 tag = "mask";
213                                 break;
214
215                 }
216
217                 perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
218                 perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
219                 perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
220                 perms[3] = '\0';
221
222                 /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
223                 nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
224
225                 /*
226                  * If this entry would overflow the buffer
227                  * allocate enough additional memory for this
228                  * entry and an estimate of another 20 bytes
229                  * for each entry still to be processed
230                  */
231                 if ((len + nbytes) > maxlen) {
232                         maxlen += nbytes + 20 * (acl_d->count - i);
233                         if ((text = (char *)SMB_REALLOC(text, maxlen)) == NULL) {
234                                 errno = ENOMEM;
235                                 return NULL;
236                         }
237                 }
238
239
240                 slprintf(&text[len], nbytes, "%s:%s:%s\n", tag, id, perms);
241                 len += (nbytes - 1);
242         }
243
244         if (len_p)
245                 *len_p = len;
246
247         return text;
248 }
249
250 SMB_ACL_T sys_acl_init(TALLOC_CTX *mem_ctx)
251 {
252         SMB_ACL_T       a;
253
254         if ((a = talloc(mem_ctx, struct smb_acl_t)) == NULL) {
255                 errno = ENOMEM;
256                 return NULL;
257         }
258
259         a->count = 0;
260         a->next = -1;
261
262         a->acl = talloc_array(a, struct smb_acl_entry, 0);
263         if (!a->acl) {
264                 TALLOC_FREE(a);
265                 errno = ENOMEM;
266                 return NULL;
267         }
268
269         return a;
270 }
271
272 int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
273 {
274         SMB_ACL_T       acl_d;
275         SMB_ACL_ENTRY_T entry_d;
276         struct smb_acl_entry *acl;
277
278         if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
279                 errno = EINVAL;
280                 return -1;
281         }
282
283         acl = talloc_realloc(acl_d, acl_d->acl, struct smb_acl_entry, acl_d->count+1);
284         if (!acl) {
285                 errno = ENOMEM;
286                 return -1;
287         }
288         acl_d->acl = acl;
289         entry_d         = &acl_d->acl[acl_d->count];
290         entry_d->a_type = SMB_ACL_TAG_INVALID;
291         entry_d->a_perm = 0;
292         *entry_p        = entry_d;
293
294         acl_d->count++;
295         return 0;
296 }
297
298 int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
299 {
300         switch (tag_type) {
301                 case SMB_ACL_USER:
302                 case SMB_ACL_USER_OBJ:
303                 case SMB_ACL_GROUP:
304                 case SMB_ACL_GROUP_OBJ:
305                 case SMB_ACL_OTHER:
306                 case SMB_ACL_MASK:
307                         entry_d->a_type = tag_type;
308                         break;
309                 default:
310                         errno = EINVAL;
311                         return -1;
312                 }
313
314         return 0;
315 }
316
317 int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
318 {
319         if (entry_d->a_type == SMB_ACL_USER) {
320                 entry_d->info.user.uid = *((uid_t *)qual_p);
321                 return 0;
322         }
323         if (entry_d->a_type == SMB_ACL_GROUP) {
324                 entry_d->info.group.gid = *((gid_t *)qual_p);
325                 return 0;
326         }
327
328         errno = EINVAL;
329         return -1;
330 }
331
332 int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
333 {
334         if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
335                 errno = EINVAL;
336                 return -1;
337         }
338
339         entry_d->a_perm = *permset_d;
340
341         return 0;
342 }
343
344 int sys_acl_free_text(char *text)
345 {
346         SAFE_FREE(text);
347         return 0;
348 }
349
350 int sys_acl_valid(SMB_ACL_T acl_d)
351 {
352         errno = EINVAL;
353         return -1;
354 }
355
356 /*
357  * acl_get_file, acl_get_fd, acl_set_file, acl_set_fd and
358  * sys_acl_delete_def_file are to be redirected to the default
359  * statically-bound acl vfs module, but they are replacable.
360  */
361
362 #if defined(HAVE_POSIX_ACLS)
363
364 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
365                         const struct smb_filename *smb_fname,
366                         SMB_ACL_TYPE_T type,
367                         TALLOC_CTX *mem_ctx)
368 {
369         return posixacl_sys_acl_get_file(handle, smb_fname, type, mem_ctx);
370 }
371
372 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp, TALLOC_CTX *mem_ctx)
373 {
374         return posixacl_sys_acl_get_fd(handle, fsp, mem_ctx);
375 }
376
377 int sys_acl_set_file(vfs_handle_struct *handle,
378                         const struct smb_filename *smb_fname,
379                         SMB_ACL_TYPE_T type,
380                         SMB_ACL_T acl_d)
381 {
382         return posixacl_sys_acl_set_file(handle, smb_fname, type, acl_d);
383 }
384
385 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
386                    SMB_ACL_T acl_d)
387 {
388         return posixacl_sys_acl_set_fd(handle, fsp, acl_d);
389 }
390
391 int sys_acl_delete_def_file(vfs_handle_struct *handle,
392                         const struct smb_filename *smb_fname)
393 {
394         return posixacl_sys_acl_delete_def_file(handle, smb_fname);
395 }
396
397 #elif defined(HAVE_AIX_ACLS)
398
399 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
400                         const struct smb_filename *smb_fname,
401                         SMB_ACL_TYPE_T type,
402                         TALLOC_CTX *mem_ctx)
403 {
404         return aixacl_sys_acl_get_file(handle, smb_fname, type, mem_ctx);
405 }
406
407 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
408                            TALLOC_CTX *mem_ctx)
409 {
410         return aixacl_sys_acl_get_fd(handle, fsp, mem_ctx);
411 }
412
413 int sys_acl_set_file(vfs_handle_struct *handle,
414                         const struct smb_filename *smb_fname,
415                         SMB_ACL_TYPE_T type,
416                         SMB_ACL_T acl_d)
417 {
418         return aixacl_sys_acl_set_file(handle, smb_fname, type, acl_d);
419 }
420
421 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
422                    SMB_ACL_T acl_d)
423 {
424         return aixacl_sys_acl_set_fd(handle, fsp, acl_d);
425 }
426
427 int sys_acl_delete_def_file(vfs_handle_struct *handle,
428                                 const struct smb_filename *smb_fname)
429 {
430         return aixacl_sys_acl_delete_def_file(handle, smb_fname);
431 }
432
433 #elif defined(HAVE_TRU64_ACLS)
434
435 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
436                         const struct smb_filename *smb_fname,
437                         SMB_ACL_TYPE_T type,
438                         TALLOC_CTX *mem_ctx)
439 {
440         return tru64acl_sys_acl_get_file(handle, smb_fname, type,
441                                          mem_ctx);
442 }
443
444 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
445                          TALLOC_CTX *mem_ctx)
446 {
447         return tru64acl_sys_acl_get_fd(handle, fsp, mem_ctx);
448 }
449
450 int sys_acl_set_file(vfs_handle_struct *handle,
451                         const struct smb_filename *smb_fname,
452                         SMB_ACL_TYPE_T type,
453                         SMB_ACL_T acl_d)
454 {
455         return tru64acl_sys_acl_set_file(handle, smb_fname, type, acl_d);
456 }
457
458 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
459                    SMB_ACL_T acl_d)
460 {
461         return tru64acl_sys_acl_set_fd(handle, fsp, acl_d);
462 }
463
464 int sys_acl_delete_def_file(vfs_handle_struct *handle,
465                                 const struct smb_filename *smb_fname)
466 {
467         return tru64acl_sys_acl_delete_def_file(handle, smb_fname);
468 }
469
470 #elif defined(HAVE_SOLARIS_UNIXWARE_ACLS)
471
472 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
473                         const struct smb_filename *smb_fname,
474                         SMB_ACL_TYPE_T type,
475                         TALLOC_CTX *mem_ctx)
476 {
477         return solarisacl_sys_acl_get_file(handle, smb_fname, type,
478                                            mem_ctx);
479 }
480
481 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
482                          TALLOC_CTX *mem_ctx)
483 {
484         return solarisacl_sys_acl_get_fd(handle, fsp,
485                                          mem_ctx);
486 }
487
488 int sys_acl_set_file(vfs_handle_struct *handle,
489                         const struct smb_filename *smb_fname,
490                         SMB_ACL_TYPE_T type,
491                         SMB_ACL_T acl_d)
492 {
493         return solarisacl_sys_acl_set_file(handle, smb_fname, type, acl_d);
494 }
495
496 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
497                    SMB_ACL_T acl_d)
498 {
499         return solarisacl_sys_acl_set_fd(handle, fsp, acl_d);
500 }
501
502 int sys_acl_delete_def_file(vfs_handle_struct *handle,
503                                 const struct smb_filename *smb_fname)
504 {
505         return solarisacl_sys_acl_delete_def_file(handle, smb_fname);
506 }
507
508 #elif defined(HAVE_HPUX_ACLS)
509
510 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
511                         const struct smb_filename *smb_fname,
512                         SMB_ACL_TYPE_T type,
513                         TALLOC_CTX *mem_ctx)
514 {
515         return hpuxacl_sys_acl_get_file(handle, smb_fname, type, mem_ctx);
516 }
517
518 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
519                            TALLOC_CTX *mem_ctx)
520 {
521         return hpuxacl_sys_acl_get_fd(handle, fsp, mem_ctx);
522 }
523
524 int sys_acl_set_file(vfs_handle_struct *handle,
525                         const struct smb_filename *smb_fname,
526                         SMB_ACL_TYPE_T type,
527                         SMB_ACL_T acl_d)
528 {
529         return hpuxacl_sys_acl_set_file(handle, smb_fname, type, acl_d);
530 }
531
532 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
533                    SMB_ACL_T acl_d)
534 {
535         return hpuxacl_sys_acl_set_fd(handle, fsp, acl_d);
536 }
537
538 int sys_acl_delete_def_file(vfs_handle_struct *handle,
539                                 const struct smb_filename *smb_fname)
540 {
541         return hpuxacl_sys_acl_delete_def_file(handle, smb_fname);
542 }
543
544 #else /* No ACLs. */
545
546 SMB_ACL_T sys_acl_get_file(vfs_handle_struct *handle,
547                         const struct smb_filename *smb_fname,
548                         SMB_ACL_TYPE_T type,
549                         TALLOC_CTX *mem_ctx)
550 {
551 #ifdef ENOTSUP
552         errno = ENOTSUP;
553 #else
554         errno = ENOSYS;
555 #endif
556         return NULL;
557 }
558
559 SMB_ACL_T sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp,
560                          TALLOC_CTX *mem_ctx)
561 {
562 #ifdef ENOTSUP
563         errno = ENOTSUP;
564 #else
565         errno = ENOSYS;
566 #endif
567         return NULL;
568 }
569
570 int sys_acl_set_file(vfs_handle_struct *handle,
571                         const struct smb_filename *smb_fname,
572                         SMB_ACL_TYPE_T type,
573                         SMB_ACL_T acl_d)
574 {
575 #ifdef ENOTSUP
576         errno = ENOTSUP;
577 #else
578         errno = ENOSYS;
579 #endif
580         return -1;
581 }
582
583 int sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
584                    SMB_ACL_T acl_d)
585 {
586 #ifdef ENOTSUP
587         errno = ENOTSUP;
588 #else
589         errno = ENOSYS;
590 #endif
591         return -1;
592 }
593
594 int sys_acl_delete_def_file(vfs_handle_struct *handle,
595                                 const struct smb_filename *smb_fname)
596 {
597 #ifdef ENOTSUP
598         errno = ENOTSUP;
599 #else
600         errno = ENOSYS;
601 #endif
602         return -1;
603 }
604
605 #endif
606
607 /************************************************************************
608  Deliberately outside the ACL defines. Return 1 if this is a "no acls"
609  errno, 0 if not.
610 ************************************************************************/
611
612 int no_acl_syscall_error(int err)
613 {
614 #if defined(ENOSYS)
615         if (err == ENOSYS) {
616                 return 1;
617         }
618 #endif
619 #if defined(ENOTSUP)
620         if (err == ENOTSUP) {
621                 return 1;
622         }
623 #endif
624         return 0;
625 }