]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/acls.c
Log filename
[netatalk.git] / etc / afpd / acls.c
1 /*
2   Copyright (c) 2008, 2009, 2010 Frank Lahm <franklahm@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <grp.h>
24 #include <pwd.h>
25 #include <errno.h>
26 #ifdef HAVE_SOLARIS_ACLS
27 #include <sys/acl.h>
28 #endif
29 #ifdef HAVE_POSIX_ACLS
30 #include <sys/acl.h>
31 #endif
32 #ifdef HAVE_ACL_LIBACL_H
33 #include <acl/libacl.h>
34 #endif
35
36 #include <atalk/errchk.h>
37 #include <atalk/adouble.h>
38 #include <atalk/vfs.h>
39 #include <atalk/afp.h>
40 #include <atalk/util.h>
41 #include <atalk/cnid.h>
42 #include <atalk/logger.h>
43 #include <atalk/uuid.h>
44 #include <atalk/acl.h>
45 #include <atalk/bstrlib.h>
46 #include <atalk/bstradd.h>
47
48 #include "directory.h"
49 #include "desktop.h"
50 #include "volume.h"
51 #include "fork.h"
52 #include "unix.h"
53 #include "acls.h"
54 #include "acl_mappings.h"
55 #include "auth.h"
56
57 /* for map_acl() */
58 #define SOLARIS_2_DARWIN       1
59 #define DARWIN_2_SOLARIS       2
60 #define POSIX_DEFAULT_2_DARWIN 3
61 #define POSIX_ACCESS_2_DARWIN  4
62 #define DARWIN_2_POSIX_DEFAULT 5
63 #define DARWIN_2_POSIX_ACCESS  6
64
65 #define MAP_MASK               31
66 #define IS_DIR                 32
67
68 /********************************************************
69  * Solaris funcs
70  ********************************************************/
71
72 #ifdef HAVE_SOLARIS_ACLS
73
74 /*! 
75  * Compile access rights for a user to one file-system object
76  *
77  * This combines combines all access rights for a user to one fs-object and
78  * returns the result as a Darwin allowed rights ACE.
79  * This must honor trivial ACEs which are a mode_t mapping.
80  *
81  * @param path           (r) path to filesystem object
82  * @param sb             (r) struct stat of path
83  * @param result         (w) resulting Darwin allow ACE
84  *
85  * @returns                  0 or -1 on error
86  */
87 static int solaris_acl_rights(const char *path,
88                               const struct stat *sb,
89                               uint32_t *result)
90 {
91     EC_INIT;
92     int      i, ace_count, checkgroup;
93     ace_t    *aces = NULL;
94     uid_t    who;
95     uint16_t flags, type;
96     uint32_t rights, allowed_rights = 0, denied_rights = 0, darwin_rights;
97
98     /* Get ACL from file/dir */
99     EC_NEG1_LOG(ace_count = get_nfsv4_acl(path, &aces));
100
101     if (ace_count == 0)
102         goto EC_CLEANUP;
103
104     /* Now check requested rights */
105     i = 0;
106     do { /* Loop through ACEs */
107         who = aces[i].a_who;
108         flags = aces[i].a_flags;
109         type = aces[i].a_type;
110         rights = aces[i].a_access_mask;
111
112         if (flags & ACE_INHERIT_ONLY_ACE)
113             continue;
114
115         /* Now the tricky part: decide if ACE effects our user. I'll explain:
116            if its a dedicated (non trivial) ACE for the user
117            OR
118            if its a ACE for a group we're member of
119            OR
120            if its a trivial ACE_OWNER ACE and requested UUID is the owner
121            OR
122            if its a trivial ACE_GROUP ACE and requested UUID is group
123            OR
124            if its a trivial ACE_EVERYONE ACE
125            THEN
126            process ACE */
127         if (((who == uuid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)))
128             ||
129             ((flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) && gmem(who))
130             ||
131             ((flags & ACE_OWNER) && (uuid == sb->st_uid))
132             ||
133             ((flags & ACE_GROUP) && !(uuid == sb->st_uid) && gmem(sb->st_gid))
134             ||
135             (flags & ACE_EVERYONE && !(uuid == sb->st_uid) && !gmem(sb->st_gid))
136             ) {
137             /* Found an applicable ACE */
138             if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
139                 allowed_rights |= rights;
140             else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
141                 /* Only or to denied rights if not previously allowed !! */
142                 denied_rights |= ((!allowed_rights) & rights);
143         }
144     } while (++i < ace_count);
145
146
147     /* Darwin likes to ask for "delete_child" on dir,
148        "write_data" is actually the same, so we add that for dirs */
149     if (S_ISDIR(sb->st_mode) && (allowed_rights & ACE_WRITE_DATA))
150         allowed_rights |= ACE_DELETE_CHILD;
151
152     /* Remove denied from allowed rights */
153     allowed_rights &= ~denied_rights;
154
155     /* map rights */
156     darwin_rights = 0;
157     for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
158         if (allowed_rights & nfsv4_to_darwin_rights[i].from)
159             darwin_rights |= nfsv4_to_darwin_rights[i].to;
160     }
161
162     *result |= darwin_rights;
163
164 EC_CLEANUP:
165     if (aces) free(aces);
166
167     EC_EXIT;
168 }
169
170 /*
171   Maps ACE array from Solaris to Darwin. Darwin ACEs are stored in network byte order.
172   Return numer of mapped ACEs or -1 on error.
173   All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
174 */
175 static int map_aces_solaris_to_darwin(const ace_t *aces,
176                                       darwin_ace_t *darwin_aces,
177                                       int ace_count)
178 {
179     EC_INIT;
180     int i, count = 0;
181     uint32_t flags;
182     uint32_t rights;
183     struct passwd *pwd = NULL;
184     struct group *grp = NULL;
185
186     LOG(log_maxdebug, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
187
188     while(ace_count--) {
189         LOG(log_maxdebug, logtype_afpd, "ACE No. %d", ace_count + 1);
190         /* if its a ACE resulting from nfsv4 mode mapping, discard it */
191         if (aces->a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
192             LOG(log_debug, logtype_afpd, "trivial ACE");
193             aces++;
194             continue;
195         }
196
197         if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) { /* user ace */
198             LOG(log_debug, logtype_afpd, "uid: %d", aces->a_who);
199             EC_NULL_LOG(pwd = getpwuid(aces->a_who));
200             LOG(log_debug, logtype_afpd, "uid: %d -> name: %s", aces->a_who, pwd->pw_name);
201             EC_ZERO_LOG(getuuidfromname(pwd->pw_name,
202                                         UUID_USER,
203                                         darwin_aces->darwin_ace_uuid));
204         } else { /* group ace */
205             LOG(log_debug, logtype_afpd, "gid: %d", aces->a_who);
206             EC_NULL_LOG(grp = getgrgid(aces->a_who));
207             LOG(log_debug, logtype_afpd, "gid: %d -> name: %s", aces->a_who, grp->gr_name);
208             EC_ZERO_LOG(getuuidfromname(grp->gr_name,
209                                         UUID_GROUP,
210                                         darwin_aces->darwin_ace_uuid));
211         }
212
213         /* map flags */
214         if (aces->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE)
215             flags = DARWIN_ACE_FLAGS_PERMIT;
216         else if (aces->a_type == ACE_ACCESS_DENIED_ACE_TYPE)
217             flags = DARWIN_ACE_FLAGS_DENY;
218         else {          /* unsupported type */
219             aces++;
220             continue;
221         }
222         for(i=0; nfsv4_to_darwin_flags[i].from != 0; i++) {
223             if (aces->a_flags & nfsv4_to_darwin_flags[i].from)
224                 flags |= nfsv4_to_darwin_flags[i].to;
225         }
226         darwin_aces->darwin_ace_flags = htonl(flags);
227
228         /* map rights */
229         rights = 0;
230         for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
231             if (aces->a_access_mask & nfsv4_to_darwin_rights[i].from)
232                 rights |= nfsv4_to_darwin_rights[i].to;
233         }
234         darwin_aces->darwin_ace_rights = htonl(rights);
235
236         count++;
237         aces++;
238         darwin_aces++;
239     }
240
241     return count;
242 EC_CLEANUP:
243     EC_EXIT;
244 }
245
246 /*
247   Maps ACE array from Darwin to Solaris. Darwin ACEs are expected in network byte order.
248   Return numer of mapped ACEs or -1 on error.
249   All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
250 */
251 static int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces,
252                                       ace_t *nfsv4_aces,
253                                       int ace_count)
254 {
255     EC_INIT;
256     int i, mapped_aces = 0;
257     uint32_t darwin_ace_flags;
258     uint32_t darwin_ace_rights;
259     uint16_t nfsv4_ace_flags;
260     uint32_t nfsv4_ace_rights;
261     char *name = NULL;
262     uuidtype_t uuidtype;
263     struct passwd *pwd;
264     struct group *grp;
265
266     while(ace_count--) {
267         nfsv4_ace_flags = 0;
268         nfsv4_ace_rights = 0;
269
270         /* uid/gid first */
271         EC_ZERO(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
272         switch (uuidtype) {
273         case UUID_USER:
274             EC_NULL_LOG(pwd = getpwnam(name));
275             nfsv4_aces->a_who = pwd->pw_uid;
276             break;
277         case UUID_GROUP:
278             EC_NULL_LOG(grp = getgrnam(name));
279             nfsv4_aces->a_who = (uid_t)(grp->gr_gid);
280             nfsv4_ace_flags |= ACE_IDENTIFIER_GROUP;
281             break;
282         default:
283             LOG(log_error, logtype_afpd, "map_aces_darwin_to_solaris: unkown uuidtype");
284             EC_FAIL;
285         }
286         free(name);
287         name = NULL;
288
289         /* now type: allow/deny */
290         darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
291         if (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT)
292             nfsv4_aces->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
293         else if (darwin_ace_flags & DARWIN_ACE_FLAGS_DENY)
294             nfsv4_aces->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
295         else { /* unsupported type */
296             darwin_aces++;
297             continue;
298         }
299         /* map flags */
300         for(i=0; darwin_to_nfsv4_flags[i].from != 0; i++) {
301             if (darwin_ace_flags & darwin_to_nfsv4_flags[i].from)
302                 nfsv4_ace_flags |= darwin_to_nfsv4_flags[i].to;
303         }
304
305         /* map rights */
306         darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
307         for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
308             if (darwin_ace_rights & darwin_to_nfsv4_rights[i].from)
309                 nfsv4_ace_rights |= darwin_to_nfsv4_rights[i].to;
310         }
311
312         LOG(log_debug9, logtype_afpd,
313             "map_aces_darwin_to_solaris: ACE flags: Darwin:%08x -> NFSv4:%04x",
314             darwin_ace_flags, nfsv4_ace_flags);
315         LOG(log_debug9, logtype_afpd,
316             "map_aces_darwin_to_solaris: ACE rights: Darwin:%08x -> NFSv4:%08x",
317             darwin_ace_rights, nfsv4_ace_rights);
318
319         nfsv4_aces->a_flags = nfsv4_ace_flags;
320         nfsv4_aces->a_access_mask = nfsv4_ace_rights;
321
322         mapped_aces++;
323         darwin_aces++;
324         nfsv4_aces++;
325     }
326
327     return mapped_aces;
328 EC_CLEANUP:
329     if (name)
330         free(name);
331     EC_EXIT;
332 }
333 #endif /* HAVE_SOLARIS_ACLS */
334
335 /********************************************************
336  * POSIX 1e funcs
337  ********************************************************/
338
339 #ifdef HAVE_POSIX_ACLS
340
341 static uint32_t posix_permset_to_darwin_rights(acl_entry_t e, int is_dir)
342 {
343     EC_INIT;
344     uint32_t rights = 0;
345     acl_permset_t permset;
346
347     EC_ZERO_LOG(acl_get_permset(e, &permset));
348
349 #ifdef HAVE_ACL_GET_PERM_NP
350     if (acl_get_perm_np(permset, ACL_READ))
351 #else
352     if (acl_get_perm(permset, ACL_READ))
353 #endif
354         rights = DARWIN_ACE_READ_DATA
355             | DARWIN_ACE_READ_EXTATTRIBUTES
356             | DARWIN_ACE_READ_ATTRIBUTES
357             | DARWIN_ACE_READ_SECURITY;
358 #ifdef HAVE_ACL_GET_PERM_NP
359     if (acl_get_perm_np(permset, ACL_WRITE)) {
360 #else
361     if (acl_get_perm(permset, ACL_WRITE)) {
362 #endif
363         rights |= DARWIN_ACE_WRITE_DATA
364             | DARWIN_ACE_APPEND_DATA
365             | DARWIN_ACE_WRITE_EXTATTRIBUTES
366             | DARWIN_ACE_WRITE_ATTRIBUTES;
367         if (is_dir)
368             rights |= DARWIN_ACE_DELETE_CHILD;
369     }
370 #ifdef HAVE_ACL_GET_PERM_NP
371     if (acl_get_perm_np(permset, ACL_EXECUTE))
372 #else
373     if (acl_get_perm(permset, ACL_EXECUTE))
374 #endif
375         rights |= DARWIN_ACE_EXECUTE;
376
377 EC_CLEANUP:
378     LOG(log_maxdebug, logtype_afpd, "mapped rights: 0x%08x", rights);
379     return rights;
380 }
381
382 /*! 
383  * Compile access rights for a user to one file-system object
384  *
385  * This combines combines all access rights for a user to one fs-object and
386  * returns the result as a Darwin allowed rights ACE.
387  * This must honor trivial ACEs which are a mode_t mapping.
388  *
389  * @param path           (r) path to filesystem object
390  * @param sb             (r) struct stat of path
391  * @param result         (rw) resulting Darwin allow ACE
392  *
393  * @returns                  0 or -1 on error
394  */
395 static int posix_acl_rights(const char *path,
396                             const struct stat *sb,
397                             uint32_t *result)
398 {
399     EC_INIT;
400     int havemask = 0;
401     int entry_id = ACL_FIRST_ENTRY;
402     uint32_t rights = 0, maskrights = 0;
403     uid_t *uid = NULL;
404     gid_t *gid = NULL;
405     acl_t acl = NULL;
406     acl_entry_t e;
407     acl_tag_t tag;
408
409     EC_NULL_LOGSTR(acl = acl_get_file(path, ACL_TYPE_ACCESS),
410                    "acl_get_file(\"%s\"): %s", fullpathname(path), strerror(errno));
411
412     /* itereate through all ACEs to get the mask */
413     while (!havemask && acl_get_entry(acl, entry_id, &e) == 1) {
414         entry_id = ACL_NEXT_ENTRY;
415         EC_ZERO_LOG(acl_get_tag_type(e, &tag));
416         switch (tag) {
417         case ACL_MASK:
418             maskrights = posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
419             LOG(log_maxdebug, logtype_afpd, "maskrights: 0x%08x", maskrights);
420             havemask = 1;
421             break;
422         default:
423             continue;
424         }
425     }
426
427     /* itereate through all ACEs */
428     entry_id = ACL_FIRST_ENTRY;
429     while (acl_get_entry(acl, entry_id, &e) == 1) {
430         entry_id = ACL_NEXT_ENTRY;
431         EC_ZERO_LOG(acl_get_tag_type(e, &tag));
432         switch (tag) {
433         case ACL_USER:
434             EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
435             if (*uid == uuid) {
436                 LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
437                 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
438             }
439             acl_free(uid);
440             uid = NULL;
441             break;
442         case ACL_USER_OBJ:
443             if (sb->st_uid == uuid) {
444                 LOG(log_maxdebug, logtype_afpd, "ACL_USER_OBJ: %u", sb->st_uid);
445                 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
446             }
447             break;
448         case ACL_GROUP:
449             EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
450             if (gmem(*gid)) {
451                 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
452                 rights |= (posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode)) & maskrights);
453             }
454             acl_free(gid);
455             gid = NULL;
456             break;
457         case ACL_GROUP_OBJ:
458             if (!(sb->st_uid == uuid) && gmem(sb->st_gid)) {
459                 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
460                 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));            
461             }
462             break;
463         case ACL_OTHER:
464             if (!(sb->st_uid == uuid) && !gmem(sb->st_gid)) {
465                 LOG(log_maxdebug, logtype_afpd, "ACL_OTHER");
466                 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
467             }
468             break;
469         default:
470             continue;
471         }
472     } /* while */
473
474     *result |= rights;
475
476 EC_CLEANUP:
477     if (acl) acl_free(acl);
478     if (uid) acl_free(uid);
479     if (gid) acl_free(gid);
480     EC_EXIT;
481 }
482
483 /*!
484  * Add entries of one acl to another acl
485  *
486  * @param aclp   (rw) destination acl where new aces will be added
487  * @param acl    (r)  source acl where aces will be copied from
488  *
489  * @returns 0 on success, -1 on error
490  */
491 static int acl_add_acl(acl_t *aclp, const acl_t acl)
492 {
493     EC_INIT;
494     int id;
495     acl_entry_t se, de;
496
497     for (id = ACL_FIRST_ENTRY; acl_get_entry(acl, id, &se) == 1; id = ACL_NEXT_ENTRY) {
498         EC_ZERO_LOG_ERR(acl_create_entry(aclp, &de), AFPERR_MISC);
499         EC_ZERO_LOG_ERR(acl_copy_entry(de, se), AFPERR_MISC);
500     }
501
502 EC_CLEANUP:
503     EC_EXIT;
504 }
505
506 /*!
507  * Map Darwin ACE rights to POSIX 1e perm
508  *
509  * We can only map few rights:
510  *   DARWIN_ACE_READ_DATA                    -> ACL_READ
511  *   DARWIN_ACE_WRITE_DATA                   -> ACL_WRITE
512  *   DARWIN_ACE_DELETE_CHILD & (is_dir == 1) -> ACL_WRITE
513  *   DARWIN_ACE_EXECUTE                      -> ACL_EXECUTE
514  *
515  * @param entry             (rw) result of the mapping
516  * @param is_dir            (r) 1 for dirs, 0 for files
517  *
518  * @returns mapping result as acl_perm_t, -1 on error
519  */
520 static acl_perm_t map_darwin_right_to_posix_permset(uint32_t darwin_ace_rights, int is_dir)
521 {
522     acl_perm_t perm = 0;
523
524     if (darwin_ace_rights & DARWIN_ACE_READ_DATA)
525         perm |= ACL_READ;
526
527     if (darwin_ace_rights & (DARWIN_ACE_WRITE_DATA | (DARWIN_ACE_DELETE_CHILD & is_dir)))
528         perm |= ACL_WRITE;
529
530     if (darwin_ace_rights & DARWIN_ACE_EXECUTE)
531         perm |= ACL_EXECUTE;
532
533     return perm;
534 }
535
536 /*!
537  * Add a ACL_USER or ACL_GROUP permission to an ACL, extending existing ACEs
538  *
539  * Add a permission of "type" for user or group "id" to an ACL. Scan the ACL
540  * for existing permissions for this type/id, if there is one add the perm,
541  * otherwise create a new ACL entry.
542  * perm can be or'ed ACL_READ, ACL_WRITE and ACL_EXECUTE.  
543  *
544  * @param aclp     (rw) pointer to ACL
545  * @param type     (r)  acl_tag_t of ACL_USER or ACL_GROUP
546  * @param id       (r)  uid_t uid for ACL_USER, or gid casted to uid_t for ACL_GROUP
547  * @param perm     (r)  acl_perm_t permissions to add
548  *
549  * @returns 0 on success, -1 on failure
550  */
551 static int posix_acl_add_perm(acl_t *aclp, acl_tag_t type, uid_t id, acl_perm_t perm)
552 {
553     EC_INIT;
554     uid_t *eid = NULL;
555     acl_entry_t e;
556     acl_tag_t tag;
557     int entry_id = ACL_FIRST_ENTRY;
558     acl_permset_t permset;
559
560     int found = 0;
561     for ( ; (! found) && acl_get_entry(*aclp, entry_id, &e) == 1; entry_id = ACL_NEXT_ENTRY) {
562         EC_ZERO_LOG(acl_get_tag_type(e, &tag));
563         if (tag != ACL_USER && tag != ACL_GROUP)
564             continue;
565         EC_NULL_LOG(eid = (uid_t *)acl_get_qualifier(e));
566         if ((*eid == id) && (type == tag)) {
567             /* found an ACE for this type/id */
568             found = 1;
569             EC_ZERO_LOG(acl_get_permset(e, &permset));
570             EC_ZERO_LOG(acl_add_perm(permset, perm));
571         }
572
573         acl_free(eid);
574         eid = NULL;
575     }
576
577     if ( ! found) {
578         /* there was no existing ACE for this type/id */
579         EC_ZERO_LOG(acl_create_entry(aclp, &e));
580         EC_ZERO_LOG(acl_set_tag_type(e, type));
581         EC_ZERO_LOG(acl_set_qualifier(e, &id));
582         EC_ZERO_LOG(acl_get_permset(e, &permset));
583         EC_ZERO_LOG(acl_clear_perms(permset));
584         EC_ZERO_LOG(acl_add_perm(permset, perm));
585         EC_ZERO_LOG(acl_set_permset(e, permset));
586     }
587     
588 EC_CLEANUP:
589     if (eid) acl_free(eid);
590
591     EC_EXIT;
592 }
593
594 /*!
595  * Map Darwin ACL to POSIX ACL.
596  *
597  * aclp must point to a acl_init'ed acl_t or an acl_t that can eg contain default ACEs.
598  * Mapping pecularities:
599  * - we create a default ace (which inherits to files and dirs) if either
600      DARWIN_ACE_FLAGS_FILE_INHERIT or DARWIN_ACE_FLAGS_DIRECTORY_INHERIT is requested
601  * - we throw away DARWIN_ACE_FLAGS_LIMIT_INHERIT (can't be mapped), thus the ACL will
602  *   not be limited
603  *
604  * @param darwin_aces  (r)  pointer to darwin_aces buffer
605  * @param def_aclp     (rw) directories: pointer to an initialized acl_t with the default acl
606  *                          files: *def_aclp will be NULL
607  * @param acc_aclp     (rw) pointer to an initialized acl_t with the access acl
608  * @param ace_count    (r)  number of ACEs in darwin_aces buffer
609  *
610  * @returns 0 on success storing the result in aclp, -1 on error.
611  */
612 static int map_aces_darwin_to_posix(const darwin_ace_t *darwin_aces,
613                                     acl_t *def_aclp,
614                                     acl_t *acc_aclp,
615                                     int ace_count)
616 {
617     EC_INIT;
618     char *name = NULL;
619     uuidtype_t uuidtype;
620     struct passwd *pwd;
621     struct group *grp;
622     uid_t id;
623     uint32_t darwin_ace_flags, darwin_ace_rights;
624     acl_tag_t tag;
625     acl_perm_t perm;
626
627     for ( ; ace_count != 0; ace_count--, darwin_aces++) {
628         /* type: allow/deny, posix only has allow */
629         darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
630         if ( ! (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT))
631             continue;
632
633         darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
634         perm = map_darwin_right_to_posix_permset(darwin_ace_rights, (*def_aclp != NULL));
635         if (perm == 0)
636             continue;       /* dont add empty perm */
637
638         LOG(log_debug, logtype_afpd, "map_ace: no: %u, flags: %08x, darwin: %08x, posix: %02x",
639             ace_count, darwin_ace_flags, darwin_ace_rights, perm);
640
641          /* uid/gid */
642         EC_ZERO_LOG(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
643         switch (uuidtype) {
644         case UUID_USER:
645             EC_NULL_LOG(pwd = getpwnam(name));
646             tag = ACL_USER;
647             id = pwd->pw_uid;
648             LOG(log_debug, logtype_afpd, "map_ace: name: %s, uid: %u", name, id);
649             break;
650         case UUID_GROUP:
651             EC_NULL_LOG(grp = getgrnam(name));
652             tag = ACL_GROUP;
653             id = (uid_t)(grp->gr_gid);
654             LOG(log_debug, logtype_afpd, "map_ace: name: %s, gid: %u", name, id);
655             break;
656         default:
657             continue;
658         }
659         free(name);
660         name = NULL;
661
662         if (darwin_ace_flags & DARWIN_ACE_INHERIT_CONTROL_FLAGS) {
663             if (*def_aclp == NULL) {
664                 /* ace request inheritane but we haven't got a default acl pointer */
665                 LOG(log_warning, logtype_afpd, "map_acl: unexpected ACE, flags: 0x%04x",
666                     darwin_ace_flags);
667                 EC_FAIL;
668             }
669             /* add it as default ace */
670             EC_ZERO_LOG(posix_acl_add_perm(def_aclp, tag, id, perm));
671
672
673             if (! (darwin_ace_flags & DARWIN_ACE_FLAGS_ONLY_INHERIT))
674                 /* if it not a "inherit only" ace, it must be added as access aces too */
675                 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
676         } else {
677             EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
678         }
679     }
680
681 EC_CLEANUP:
682     if (name)
683         free(name);
684
685     EC_EXIT;
686 }
687
688 /*
689  * Map ACEs from POSIX to Darwin.
690  * type is either POSIX_DEFAULT_2_DARWIN or POSIX_ACCESS_2_DARWIN, cf. acl_get_file.
691  * Return number of mapped ACES, -1 on error.
692  */
693 static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darwin_aces)
694 {
695     EC_INIT;
696     int mapped_aces = 0;
697     int entry_id = ACL_FIRST_ENTRY;
698     acl_entry_t e;
699     acl_tag_t tag;
700     uid_t *uid = NULL;
701     gid_t *gid = NULL;
702     struct passwd *pwd = NULL;
703     struct group *grp = NULL;
704     uint32_t flags;
705     uint32_t rights, maskrights = 0;
706     darwin_ace_t *saved_darwin_aces = darwin_aces;
707
708     LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
709         (type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN ?
710         "POSIX_DEFAULT_2_DARWIN" : "POSIX_ACCESS_2_DARWIN");
711
712     /* itereate through all ACEs */
713     while (acl_get_entry(acl, entry_id, &e) == 1) {
714         entry_id = ACL_NEXT_ENTRY;
715
716         /* get ACE type */
717         EC_ZERO_LOG(acl_get_tag_type(e, &tag));
718
719         /* we return user and group ACE */
720         switch (tag) {
721         case ACL_USER:
722             EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
723             EC_NULL_LOG(pwd = getpwuid(*uid));
724             LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: uid: %d -> name: %s",
725                 *uid, pwd->pw_name);
726             EC_ZERO_LOG(getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid));
727             acl_free(uid);
728             uid = NULL;
729             break;
730
731         case ACL_GROUP:
732             EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
733             EC_NULL_LOG(grp = getgrgid(*gid));
734             LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: gid: %d -> name: %s",
735                 *gid, grp->gr_name);
736             EC_ZERO_LOG(getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid));
737             acl_free(gid);
738             gid = NULL;
739             break;
740
741         case ACL_MASK:
742             maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
743             continue;
744
745         default:
746             continue;
747         }
748
749         /* flags */
750         flags = DARWIN_ACE_FLAGS_PERMIT;
751         if ((type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN)
752             flags |= DARWIN_ACE_FLAGS_FILE_INHERIT
753                 | DARWIN_ACE_FLAGS_DIRECTORY_INHERIT
754                 | DARWIN_ACE_FLAGS_ONLY_INHERIT;
755         darwin_aces->darwin_ace_flags = htonl(flags);
756
757         /* rights */
758         rights = posix_permset_to_darwin_rights(e, type & IS_DIR);
759         darwin_aces->darwin_ace_rights = htonl(rights);
760
761         darwin_aces++;
762         mapped_aces++;
763     } /* while */
764
765     /* Loop through the mapped ACE buffer once again, applying the mask */
766     for (int i = mapped_aces; i > 0; i--) {
767         saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
768         saved_darwin_aces++;
769     }
770
771     EC_STATUS(mapped_aces);
772
773 EC_CLEANUP:
774     if (uid) acl_free(uid);
775     if (gid) acl_free(gid);
776     EC_EXIT;
777 }
778 #endif
779
780 /*
781  * Multiplex ACL mapping (SOLARIS_2_DARWIN, DARWIN_2_SOLARIS, POSIX_2_DARWIN, DARWIN_2_POSIX).
782  * Reads from 'aces' buffer, writes to 'rbuf' buffer.
783  * Caller must provide buffer.
784  * Darwin ACEs are read and written in network byte order.
785  * Needs to know how many ACEs are in the ACL (ace_count) for Solaris ACLs.
786  * Ignores trivial ACEs.
787  * Return no of mapped ACEs or -1 on error.
788  */
789 static int map_acl(int type, void *acl, darwin_ace_t *buf, int ace_count)
790 {
791     int mapped_aces;
792
793     LOG(log_debug9, logtype_afpd, "map_acl: BEGIN");
794
795     switch (type & MAP_MASK) {
796
797 #ifdef HAVE_SOLARIS_ACLS
798     case SOLARIS_2_DARWIN:
799         mapped_aces = map_aces_solaris_to_darwin( acl, buf, ace_count);
800         break;
801
802     case DARWIN_2_SOLARIS:
803         mapped_aces = map_aces_darwin_to_solaris( buf, acl, ace_count);
804         break;
805 #endif /* HAVE_SOLARIS_ACLS */
806
807 #ifdef HAVE_POSIX_ACLS
808     case POSIX_DEFAULT_2_DARWIN:
809         mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
810         break;
811
812     case POSIX_ACCESS_2_DARWIN:
813         mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
814         break;
815
816     case DARWIN_2_POSIX_DEFAULT:
817         break;
818
819     case DARWIN_2_POSIX_ACCESS:
820         break;
821 #endif /* HAVE_POSIX_ACLS */
822
823     default:
824         mapped_aces = -1;
825         break;
826     }
827
828     LOG(log_debug9, logtype_afpd, "map_acl: END");
829     return mapped_aces;
830 }
831
832 /* Get ACL from object omitting trivial ACEs. Map to Darwin ACL style and
833    store Darwin ACL at rbuf. Add length of ACL written to rbuf to *rbuflen.
834    Returns 0 on success, -1 on error. */
835 static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
836 {
837     EC_INIT;
838     int mapped_aces = 0;
839     int dirflag;
840     uint32_t *darwin_ace_count = (u_int32_t *)rbuf;
841 #ifdef HAVE_SOLARIS_ACLS
842     int ace_count = 0;
843     ace_t *aces = NULL;
844 #endif
845 #ifdef HAVE_POSIX_ACLS
846     struct stat st;
847 #endif
848     LOG(log_debug9, logtype_afpd, "get_and_map_acl: BEGIN");
849
850     /* Skip length and flags */
851     rbuf += 4;
852     *rbuf = 0;
853     rbuf += 4;
854
855 #ifdef HAVE_SOLARIS_ACLS
856     EC_NEG1(ace_count = get_nfsv4_acl(name, &aces));
857     EC_NEG1(mapped_aces = map_acl(SOLARIS_2_DARWIN, aces, (darwin_ace_t *)rbuf, ace_count));
858 #endif /* HAVE_SOLARIS_ACLS */
859
860 #ifdef HAVE_POSIX_ACLS
861     acl_t defacl = NULL , accacl = NULL;
862
863     /* stat to check if its a dir */
864     EC_ZERO_LOG(lstat(name, &st));
865
866     /* if its a dir, check for default acl too */
867     dirflag = 0;
868     if (S_ISDIR(st.st_mode)) {
869         dirflag = IS_DIR;
870         EC_NULL_LOG(defacl = acl_get_file(name, ACL_TYPE_DEFAULT));
871         EC_NEG1(mapped_aces = map_acl(POSIX_DEFAULT_2_DARWIN | dirflag,
872                                       defacl,
873                                       (darwin_ace_t *)rbuf,
874                                       0));
875     }
876
877     EC_NULL_LOG(accacl = acl_get_file(name, ACL_TYPE_ACCESS));
878
879     int tmp;
880     EC_NEG1(tmp = map_acl(POSIX_ACCESS_2_DARWIN | dirflag,
881                           accacl,
882                           (darwin_ace_t *)(rbuf + mapped_aces * sizeof(darwin_ace_t)),
883                           0));
884     mapped_aces += tmp;
885 #endif /* HAVE_POSIX_ACLS */
886
887     LOG(log_debug, logtype_afpd, "get_and_map_acl: mapped %d ACEs", mapped_aces);
888
889     *darwin_ace_count = htonl(mapped_aces);
890     *rbuflen += sizeof(darwin_acl_header_t) + (mapped_aces * sizeof(darwin_ace_t));
891
892     EC_STATUS(0);
893
894 EC_CLEANUP:
895 #ifdef HAVE_SOLARIS_ACLS
896     if (aces) free(aces);
897 #endif
898 #ifdef HAVE_POSIX_ACLS
899     if (defacl) acl_free(defacl);
900     if (accacl) acl_free(accacl);
901 #endif /* HAVE_POSIX_ACLS */
902
903     LOG(log_debug9, logtype_afpd, "get_and_map_acl: END");
904
905     EC_EXIT;
906 }
907
908 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
909 static int remove_acl(const struct vol *vol,const char *path, int dir)
910 {
911     int ret = AFP_OK;
912
913 #if (defined HAVE_SOLARIS_ACLS || defined HAVE_POSIX_ACLS)
914     /* Ressource etc. first */
915     if ((ret = vol->vfs->vfs_remove_acl(vol, path, dir)) != AFP_OK)
916         return ret;
917     /* now the data fork or dir */
918     ret = remove_acl_vfs(path);
919 #endif
920     return ret;
921 }
922
923 /*
924   Set ACL. Subtleties:
925   - the client sends a complete list of ACEs, not only new ones. So we dont need to do
926   any combination business (one exception being 'kFileSec_Inherit': see next)
927   - client might request that we add inherited ACEs via 'kFileSec_Inherit'.
928   We will store inherited ACEs first, which is Darwins canonical order.
929   - returns AFPerror code
930 */
931 #ifdef HAVE_SOLARIS_ACLS
932 static int set_acl(const struct vol *vol,
933                    char *name,
934                    int inherit,
935                    darwin_ace_t *daces,
936                    uint32_t ace_count)
937 {
938     EC_INIT;
939     int i, nfsv4_ace_count;
940     int tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
941     ace_t *old_aces, *new_aces = NULL;
942     uint16_t flags;
943
944     LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
945
946     if (inherit)
947         /* inherited + trivial ACEs */
948         flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
949     else
950         /* only trivial ACEs */
951         flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
952
953     /* Get existing ACL and count ACEs which have to be copied */
954     if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
955         return AFPERR_MISC;
956     for ( i=0; i < nfsv4_ace_count; i++) {
957         if (old_aces[i].a_flags & flags)
958             tocopy_aces_count++;
959     }
960
961     /* Now malloc buffer exactly sized to fit all new ACEs */
962     if ((new_aces = malloc((ace_count + tocopy_aces_count) * sizeof(ace_t))) == NULL) {
963         LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
964         EC_STATUS(AFPERR_MISC);
965         goto EC_CLEANUP;
966     }
967
968     /* Start building new ACL */
969
970     /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
971        inherited ACEs first. */
972     if (inherit) {
973         for (i=0; i < nfsv4_ace_count; i++) {
974             if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
975                 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
976                 new_aces_count++;
977             }
978         }
979     }
980     LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
981
982     /* Now the ACEs from the client */
983     if ((ret = (map_acl(DARWIN_2_SOLARIS,
984                         &new_aces[new_aces_count],
985                         daces,
986                         ace_count))) == -1) {
987         EC_STATUS(AFPERR_PARAM);
988         goto EC_CLEANUP;
989     }
990     new_aces_count += ret;
991     LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ret);
992
993     /* Now copy the trivial ACEs */
994     for (i=0; i < nfsv4_ace_count; i++) {
995         if (old_aces[i].a_flags  & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
996             memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
997             new_aces_count++;
998             trivial_ace_count++;
999         }
1000     }
1001     LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
1002
1003     /* Ressourcefork first.
1004        Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
1005        might be strange for ACE_DELETE_CHILD and for inheritance flags. */
1006     if ((ret = (vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1007         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1008         if (errno == (EACCES | EPERM))
1009             EC_STATUS(AFPERR_ACCESS);
1010         else if (errno == ENOENT)
1011             EC_STATUS(AFPERR_NOITEM);
1012         else
1013             EC_STATUS(AFPERR_MISC);
1014         goto EC_CLEANUP;
1015     }
1016     if ((ret = (acl(name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1017         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1018         if (errno == (EACCES | EPERM))
1019             EC_STATUS(AFPERR_ACCESS);
1020         else if (errno == ENOENT)
1021             EC_STATUS(AFPERR_NOITEM);
1022         else
1023             EC_STATUS(AFPERR_MISC);
1024         goto EC_CLEANUP;
1025     }
1026
1027     EC_STATUS(AFP_OK);
1028
1029 EC_CLEANUP:
1030     if (old_aces) free(old_aces);
1031     if (new_aces) free(new_aces);
1032
1033     LOG(log_debug9, logtype_afpd, "set_acl: END");
1034     EC_EXIT;
1035 }
1036 #endif /* HAVE_SOLARIS_ACLS */
1037
1038 #ifdef HAVE_POSIX_ACLS
1039 #ifndef HAVE_ACL_FROM_MODE
1040 static acl_t acl_from_mode(mode_t mode)
1041 {
1042     acl_t acl;
1043     acl_entry_t entry;
1044     acl_permset_t permset;
1045
1046     if (!(acl = acl_init(3)))
1047         return NULL;
1048
1049     if (acl_create_entry(&acl, &entry) != 0)
1050         goto error;
1051     acl_set_tag_type(entry, ACL_USER_OBJ);
1052     acl_get_permset(entry, &permset);
1053     acl_clear_perms(permset);
1054     if (mode & S_IRUSR)
1055         acl_add_perm(permset, ACL_READ);
1056     if (mode & S_IWUSR)
1057         acl_add_perm(permset, ACL_WRITE);
1058     if (mode & S_IXUSR)
1059         acl_add_perm(permset, ACL_EXECUTE);
1060     acl_set_permset(entry, permset);
1061
1062     if (acl_create_entry(&acl, &entry) != 0)
1063         goto error;
1064     acl_set_tag_type(entry, ACL_GROUP_OBJ);
1065     acl_get_permset(entry, &permset);
1066     acl_clear_perms(permset);
1067     if (mode & S_IRGRP)
1068         acl_add_perm(permset, ACL_READ);
1069     if (mode & S_IWGRP)
1070         acl_add_perm(permset, ACL_WRITE);
1071     if (mode & S_IXGRP)
1072         acl_add_perm(permset, ACL_EXECUTE);
1073     acl_set_permset(entry, permset);
1074
1075     if (acl_create_entry(&acl, &entry) != 0)
1076         goto error;
1077     acl_set_tag_type(entry, ACL_OTHER);
1078     acl_get_permset(entry, &permset);
1079     acl_clear_perms(permset);
1080     if (mode & S_IROTH)
1081         acl_add_perm(permset, ACL_READ);
1082     if (mode & S_IWOTH)
1083         acl_add_perm(permset, ACL_WRITE);
1084     if (mode & S_IXOTH)
1085         acl_add_perm(permset, ACL_EXECUTE);
1086     acl_set_permset(entry, permset);
1087
1088     return acl;
1089
1090 error:
1091     acl_free(acl);
1092     return NULL;
1093 }
1094 #endif
1095
1096 static int set_acl(const struct vol *vol,
1097                    const char *name,
1098                    int inherit _U_,
1099                    darwin_ace_t *daces,
1100                    uint32_t ace_count)
1101 {
1102     EC_INIT;
1103     acl_t def_acl = NULL;
1104     acl_t acc_acl = NULL;
1105
1106     LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
1107
1108     struct stat st;
1109     EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
1110
1111     /* seed default ACL with access ACL */
1112     if (S_ISDIR(st.st_mode))
1113         EC_NULL_LOG_ERR(def_acl = acl_get_file(name, ACL_TYPE_ACCESS), AFPERR_MISC);
1114
1115     /* for files def_acl will be NULL */
1116
1117     /* create access acl from mode */
1118     EC_NULL_LOG_ERR(acc_acl = acl_from_mode(st.st_mode), AFPERR_MISC);
1119
1120     /* adds the clients aces */
1121     EC_ZERO_ERR(map_aces_darwin_to_posix(daces, &def_acl, &acc_acl, ace_count), AFPERR_MISC);
1122
1123     /* calcuate ACL mask */
1124     EC_ZERO_LOG_ERR(acl_calc_mask(&acc_acl), AFPERR_MISC);
1125
1126     /* is it ok? */
1127     EC_ZERO_LOG_ERR(acl_valid(acc_acl), AFPERR_MISC);
1128
1129     /* set it */
1130     EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_ACCESS, acc_acl), AFPERR_MISC);
1131     EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_ACCESS, 0, acc_acl), AFPERR_MISC);
1132
1133     if (def_acl) {
1134         EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_DEFAULT, def_acl), AFPERR_MISC);
1135         EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_DEFAULT, 0, def_acl), AFPERR_MISC);
1136     }
1137
1138 EC_CLEANUP:
1139     acl_free(acc_acl);
1140     acl_free(def_acl);
1141
1142     LOG(log_maxdebug, logtype_afpd, "set_acl: END");
1143     EC_EXIT;
1144 }
1145 #endif /* HAVE_POSIX_ACLS */
1146
1147 /*!
1148  * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
1149  *
1150  * Note: this gets called frequently and is a good place for optimizations !
1151  *
1152  * @param vol              (r) volume
1153  * @param dir              (rw) directory
1154  * @param path             (r) path to filesystem object
1155  * @param uuid             (r) UUID of user
1156  * @param requested_rights (r) requested Darwin ACE
1157  *
1158  * @returns                    AFP result code
1159 */
1160 static int check_acl_access(const struct vol *vol,
1161                             struct dir *dir,
1162                             const char *path,
1163                             const uuidp_t uuid,
1164                             uint32_t requested_rights)
1165 {
1166     int            ret;
1167     uint32_t       allowed_rights = 0;
1168     char           *username = NULL;
1169     uuidtype_t     uuidtype;
1170     struct stat    st;
1171     bstring        parent = NULL;
1172     int            is_dir;
1173
1174     LOG(log_maxdebug, logtype_afpd, "check_acl_access(dir: \"%s\", path: \"%s\", curdir: \"%s\", 0x%08x)",
1175         cfrombstr(dir->d_fullpath), path, getcwdpath(), requested_rights);
1176
1177     /* This check is not used anymore, as OS X Server seems to be ignoring too */
1178 #if 0
1179     /* Get uid or gid from UUID */
1180     EC_ZERO_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
1181     switch (uuidtype) {
1182     case UUID_USER:
1183         break;
1184     case UUID_GROUP:
1185         LOG(log_warning, logtype_afpd, "check_acl_access: afp_access not supported for groups");
1186         EC_STATUS(AFPERR_MISC);
1187         goto EC_CLEANUP;
1188     default:
1189         EC_STATUS(AFPERR_MISC);
1190         goto EC_CLEANUP;
1191     }
1192 #endif
1193
1194     EC_ZERO_LOG_ERR(lstat(path, &st), AFPERR_PARAM);
1195
1196     is_dir = !strcmp(path, ".");
1197
1198     if (is_dir && (curdir->d_rights_cache != 0xffffffff)) {
1199         /* its a dir and the cache value is valid */
1200         allowed_rights = curdir->d_rights_cache;
1201         LOG(log_debug, logtype_afpd, "check_access: allowed rights from dircache: 0x%08x", allowed_rights);
1202     } else {
1203 #ifdef HAVE_SOLARIS_ACLS
1204         EC_ZERO_LOG(solaris_acl_rights(path, &st, &allowed_rights));
1205 #endif
1206 #ifdef HAVE_POSIX_ACLS
1207         EC_ZERO_LOG(posix_acl_rights(path, &st, &allowed_rights));
1208 #endif
1209         /*
1210          * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
1211          * directory. As it seems the 10.6 AFP client is puzzled when this right is not
1212          * allowed where a delete would succeed because the parent dir gives write perms.
1213          * So we check the parent dir for write access and set the right accordingly.
1214          * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
1215          * there to do this extra check -- afaict.
1216          */
1217         if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
1218             int i;
1219             uint32_t parent_rights = 0;
1220
1221             if (curdir->d_did == DIRDID_ROOT_PARENT) {
1222                 /* use volume path */
1223                 EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
1224             } else {
1225                 /* build path for parent */
1226                 EC_NULL_LOG_ERR(parent = bstrcpy(curdir->d_fullpath), AFPERR_MISC);
1227                 EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
1228                 EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
1229                 EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
1230                 EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
1231             }
1232
1233             LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
1234             EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
1235
1236 #ifdef HAVE_SOLARIS_ACLS
1237             EC_ZERO_LOG(solaris_acl_rights(cfrombstr(parent), &st, &parent_rights));
1238 #endif
1239 #ifdef HAVE_POSIX_ACLS
1240             EC_ZERO_LOG(posix_acl_rights(path, &st, &parent_rights));
1241 #endif
1242             if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
1243                 allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
1244         }
1245
1246         if (is_dir) {
1247             /* Without DARWIN_ACE_DELETE set OS X 10.6 refuses to rename subdirectories in a
1248              * directory.
1249              */
1250             if (allowed_rights & DARWIN_ACE_ADD_SUBDIRECTORY)
1251                 allowed_rights |= DARWIN_ACE_DELETE;
1252
1253             dir->d_rights_cache = allowed_rights;
1254         }
1255         LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
1256     }
1257
1258     if ((requested_rights & allowed_rights) != requested_rights) {
1259         LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
1260             requested_rights, allowed_rights);
1261         EC_STATUS(AFPERR_ACCESS);
1262     } else {
1263         LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
1264             requested_rights);
1265         EC_STATUS(AFP_OK);
1266     }
1267
1268 EC_CLEANUP:
1269     if (username) free(username);
1270     if (parent) bdestroy(parent);
1271
1272     EC_EXIT;
1273 }
1274
1275 /********************************************************
1276  * Interface
1277  ********************************************************/
1278
1279 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1280 {
1281     int         ret;
1282     struct vol      *vol;
1283     struct dir      *dir;
1284     uint32_t            did, darwin_ace_rights;
1285     uint16_t        vid;
1286     struct path         *s_path;
1287     uuidp_t             uuid;
1288
1289     *rbuflen = 0;
1290     ibuf += 2;
1291
1292     memcpy(&vid, ibuf, sizeof( vid ));
1293     ibuf += sizeof(vid);
1294     if (NULL == ( vol = getvolbyvid( vid ))) {
1295         LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1296         return AFPERR_NOOBJ;
1297     }
1298
1299     memcpy(&did, ibuf, sizeof( did ));
1300     ibuf += sizeof( did );
1301     if (NULL == ( dir = dirlookup( vol, did ))) {
1302         LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1303         return afp_errno;
1304     }
1305
1306     /* Skip bitmap */
1307     ibuf += 2;
1308
1309     /* Store UUID address */
1310     uuid = (uuidp_t)ibuf;
1311     ibuf += UUID_BINSIZE;
1312
1313     /* Store ACE rights */
1314     memcpy(&darwin_ace_rights, ibuf, 4);
1315     darwin_ace_rights = ntohl(darwin_ace_rights);
1316     ibuf += 4;
1317
1318     /* get full path and handle file/dir subtleties in netatalk code*/
1319     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1320         LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1321         return AFPERR_NOOBJ;
1322     }
1323     if (!s_path->st_valid)
1324         of_statdir(vol, s_path);
1325     if ( s_path->st_errno != 0 ) {
1326         LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1327         return AFPERR_NOOBJ;
1328     }
1329
1330     ret = check_acl_access(vol, dir, s_path->u_name, uuid, darwin_ace_rights);
1331
1332     return ret;
1333 }
1334
1335 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1336 {
1337     struct vol      *vol;
1338     struct dir      *dir;
1339     int         ret;
1340     uint32_t           did;
1341     uint16_t        vid, bitmap;
1342     struct path         *s_path;
1343     struct passwd       *pw;
1344     struct group        *gr;
1345
1346     LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1347     *rbuflen = 0;
1348     ibuf += 2;
1349
1350     memcpy(&vid, ibuf, sizeof( vid ));
1351     ibuf += sizeof(vid);
1352     if (NULL == ( vol = getvolbyvid( vid ))) {
1353         LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1354         return AFPERR_NOOBJ;
1355     }
1356
1357     memcpy(&did, ibuf, sizeof( did ));
1358     ibuf += sizeof( did );
1359     if (NULL == ( dir = dirlookup( vol, did ))) {
1360         LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1361         return afp_errno;
1362     }
1363
1364     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1365     memcpy(rbuf, ibuf, sizeof( bitmap ));
1366     bitmap = ntohs( bitmap );
1367     ibuf += sizeof( bitmap );
1368     rbuf += sizeof( bitmap );
1369     *rbuflen += sizeof( bitmap );
1370
1371     /* skip maxreplysize */
1372     ibuf += 4;
1373
1374     /* get full path and handle file/dir subtleties in netatalk code*/
1375     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1376         LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1377         return AFPERR_NOOBJ;
1378     }
1379     if (!s_path->st_valid)
1380         of_statdir(vol, s_path);
1381     if ( s_path->st_errno != 0 ) {
1382         LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1383         return AFPERR_NOOBJ;
1384     }
1385
1386     /* Shall we return owner UUID ? */
1387     if (bitmap & kFileSec_UUID) {
1388         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1389         if (NULL == (pw = getpwuid(s_path->st.st_uid))) {
1390             LOG(log_debug, logtype_afpd, "afp_getacl: local uid: %u", s_path->st.st_uid);
1391             localuuid_from_id(rbuf, UUID_USER, s_path->st.st_uid);
1392         } else {
1393             LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1394             if ((ret = getuuidfromname(pw->pw_name, UUID_USER, rbuf)) != 0)
1395                 return AFPERR_MISC;
1396         }
1397         rbuf += UUID_BINSIZE;
1398         *rbuflen += UUID_BINSIZE;
1399     }
1400
1401     /* Shall we return group UUID ? */
1402     if (bitmap & kFileSec_GRPUUID) {
1403         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1404         if (NULL == (gr = getgrgid(s_path->st.st_gid))) {
1405             LOG(log_debug, logtype_afpd, "afp_getacl: local gid: %u", s_path->st.st_gid);
1406             localuuid_from_id(rbuf, UUID_GROUP, s_path->st.st_gid);
1407         } else {
1408             LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1409             if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, rbuf)) != 0)
1410                 return AFPERR_MISC;
1411         }
1412         rbuf += UUID_BINSIZE;
1413         *rbuflen += UUID_BINSIZE;
1414     }
1415
1416     /* Shall we return ACL ? */
1417     if (bitmap & kFileSec_ACL) {
1418         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1419         if (get_and_map_acl(s_path->u_name, rbuf, rbuflen) != 0) {
1420             LOG(log_error, logtype_afpd, "afp_getacl(\"%s/%s\"): mapping error",
1421                 getcwdpath(), s_path->u_name);
1422             return AFPERR_MISC;
1423         }
1424     }
1425
1426     LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1427     return AFP_OK;
1428 }
1429
1430 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1431 {
1432     struct vol      *vol;
1433     struct dir      *dir;
1434     int         ret;
1435     uint32_t            did;
1436     uint16_t        vid, bitmap;
1437     struct path         *s_path;
1438
1439     LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1440     *rbuflen = 0;
1441     ibuf += 2;
1442
1443     memcpy(&vid, ibuf, sizeof( vid ));
1444     ibuf += sizeof(vid);
1445     if (NULL == ( vol = getvolbyvid( vid ))) {
1446         LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1447         return AFPERR_NOOBJ;
1448     }
1449
1450     memcpy(&did, ibuf, sizeof( did ));
1451     ibuf += sizeof( did );
1452     if (NULL == ( dir = dirlookup( vol, did ))) {
1453         LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1454         return afp_errno;
1455     }
1456
1457     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1458     bitmap = ntohs( bitmap );
1459     ibuf += sizeof( bitmap );
1460
1461     /* get full path and handle file/dir subtleties in netatalk code*/
1462     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1463         LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1464         return AFPERR_NOOBJ;
1465     }
1466     if (!s_path->st_valid)
1467         of_statdir(vol, s_path);
1468     if ( s_path->st_errno != 0 ) {
1469         LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1470         return AFPERR_NOOBJ;
1471     }
1472     LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1473
1474     /* Padding? */
1475     if ((unsigned long)ibuf & 1)
1476         ibuf++;
1477
1478     /* Start processing request */
1479
1480     /* Change owner: dont even try */
1481     if (bitmap & kFileSec_UUID) {
1482         LOG(log_note, logtype_afpd, "afp_setacl: change owner request, discarded");
1483         ret = AFPERR_ACCESS;
1484         ibuf += UUID_BINSIZE;
1485     }
1486
1487     /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1488     if (bitmap & kFileSec_UUID) {
1489         LOG(log_note, logtype_afpd, "afp_setacl: change group request, not supported");
1490         ret = AFPERR_PARAM;
1491         ibuf += UUID_BINSIZE;
1492     }
1493
1494     /* Remove ACL ? */
1495     if (bitmap & kFileSec_REMOVEACL) {
1496         LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1497         if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1498             LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1499     }
1500
1501     /* Change ACL ? */
1502     if (bitmap & kFileSec_ACL) {
1503         LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1504         /*  Get no of ACEs the client put on the wire */
1505         uint32_t ace_count;
1506         memcpy(&ace_count, ibuf, sizeof(uint32_t));
1507         ace_count = htonl(ace_count);
1508         ibuf += 8;      /* skip ACL flags (see acls.h) */
1509
1510         ret = set_acl(vol,
1511                       s_path->u_name,
1512                       (bitmap & kFileSec_Inherit),
1513                       (darwin_ace_t *)ibuf,
1514                       ace_count);
1515         if (ret == 0)
1516             ret = AFP_OK;
1517         else {
1518             LOG(log_warning, logtype_afpd, "afp_setacl(\"%s/%s\"): error",
1519                 getcwdpath(), s_path->u_name);
1520             ret = AFPERR_MISC;
1521         }
1522     }
1523
1524     LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1525     return ret;
1526 }
1527
1528 /********************************************************************
1529  * ACL funcs interfacing with other parts
1530  ********************************************************************/
1531
1532 /*!
1533  * map ACL to user maccess
1534  *
1535  * This is the magic function that makes ACLs usable by calculating
1536  * the access granted by ACEs to the logged in user.
1537  */
1538 int acltoownermode(char *path, struct stat *st, struct maccess *ma)
1539 {
1540     EC_INIT;
1541     uint32_t rights = 0;
1542
1543     if ( ! (AFPobj->options.flags & OPTION_ACL2MACCESS)
1544          || (current_vol == NULL)
1545          || ! (current_vol->v_flags & AFPVOL_ACLS))
1546          return 0;
1547
1548     LOG(log_maxdebug, logtype_afpd, "acltoownermode(\"%s/%s\", 0x%02x)",
1549         getcwdpath(), path, ma->ma_user);
1550
1551 #ifdef HAVE_SOLARIS_ACLS
1552     EC_ZERO_LOG(solaris_acl_rights(path, st, &rights));
1553 #endif
1554 #ifdef HAVE_POSIX_ACLS
1555     EC_ZERO_LOG(posix_acl_rights(path, st, &rights));
1556 #endif
1557
1558     LOG(log_maxdebug, logtype_afpd, "rights: 0x%08x", rights);
1559
1560     if (rights & DARWIN_ACE_READ_DATA)
1561         ma->ma_user |= AR_UREAD;
1562     if (rights & DARWIN_ACE_WRITE_DATA)
1563         ma->ma_user |= AR_UWRITE;
1564     if (rights & (DARWIN_ACE_EXECUTE | DARWIN_ACE_SEARCH))
1565         ma->ma_user |= AR_USEARCH;
1566
1567     LOG(log_maxdebug, logtype_afpd, "resulting user maccess: 0x%02x", ma->ma_user);
1568
1569 EC_CLEANUP:
1570     EC_EXIT;
1571 }
1572
1573 /*!
1574  * Check whether a volume supports ACLs
1575  *
1576  * @param vol  (r) volume
1577  *
1578  * @returns        0 if not, 1 if yes
1579  */
1580 int check_vol_acl_support(const struct vol *vol)
1581 {
1582     int ret = 0;
1583
1584 #ifdef HAVE_SOLARIS_ACLS
1585     ace_t *aces = NULL;
1586     ret = 1;
1587     if (get_nfsv4_acl(vol->v_path, &aces) == -1)
1588         ret = 0;
1589 #endif
1590 #ifdef HAVE_POSIX_ACLS
1591     acl_t acl = NULL;
1592     ret = 1;
1593     if ((acl = acl_get_file(vol->v_path, ACL_TYPE_ACCESS)) == NULL)
1594         ret = 0;
1595 #endif
1596
1597 #ifdef HAVE_SOLARIS_ACLS
1598     if (aces) free(aces);
1599 #endif
1600 #ifdef HAVE_POSIX_ACLS
1601     if (acl) acl_free(acl);
1602 #endif /* HAVE_POSIX_ACLS */
1603
1604     LOG(log_debug, logtype_afpd, "Volume \"%s\" ACL support: %s",
1605         vol->v_path, ret ? "yes" : "no");
1606     return ret;
1607 }