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