]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/acl.c
more support for sigint in dbd -re
[netatalk.git] / libatalk / vfs / acl.c
1 /*
2   Copyright (c) 2009 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 <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/acl.h>
26
27 #include <atalk/afp.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
30
31 /* Get ACL. Allocates storage as needed. Caller must free.
32  * Returns no of ACEs or -1 on error.  */
33 int get_nfsv4_acl(const char *name, ace_t **retAces)
34 {
35     int ace_count = -1;
36     ace_t *aces;
37     struct stat st;
38
39     *retAces = NULL;
40     /* Only call acl() for regular files and directories, otherwise just return 0 */
41     if (lstat(name, &st) != 0)
42         return -1;
43     if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
44         return 0;
45     if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
46         return 0;
47
48     if (ace_count == -1) {
49         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
50             getcwdpath(), name, ace_count, strerror(errno));
51         return -1;
52     }
53
54     aces = malloc(ace_count * sizeof(ace_t));
55     if (aces == NULL) {
56         LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
57         return -1;
58     }
59
60     if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
61         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
62         free(aces);
63         return -1;
64     }
65
66     LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
67     *retAces = aces;
68
69     return ace_count;
70 }
71
72 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
73 int remove_acl(const char *name)
74 {
75     int ret,i, ace_count, trivial_aces, new_aces_count;
76     ace_t *old_aces = NULL;
77     ace_t *new_aces = NULL;
78
79     LOG(log_debug9, logtype_afpd, "remove_acl: BEGIN");
80
81     /* Get existing ACL and count trivial ACEs */
82     if ((ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
83         return AFPERR_MISC;
84     trivial_aces = 0;
85     for ( i=0; i < ace_count; i++) {
86         if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))
87             trivial_aces++;
88     }
89
90     /* malloc buffer for new ACL */
91     if ((new_aces = malloc(trivial_aces * sizeof(ace_t))) == NULL) {
92         LOG(log_error, logtype_afpd, "remove_acl: malloc %s", strerror(errno));
93         ret = AFPERR_MISC;
94         goto exit;
95     }
96
97     /* Now copy the trivial ACEs */
98     new_aces_count = 0;
99     for (i=0; i < ace_count; i++) {
100         if (old_aces[i].a_flags  & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
101             memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
102             new_aces_count++;
103         }
104     }
105
106     if ( (acl(name, ACE_SETACL, trivial_aces, new_aces)) == 0)
107         ret = AFP_OK;
108     else {
109         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
110         if (errno == (EACCES | EPERM))
111             ret = AFPERR_ACCESS;
112         else if (errno == ENOENT)
113             ret = AFPERR_NOITEM;
114         else
115             ret = AFPERR_MISC;
116     }
117
118 exit:
119     free(old_aces);
120     free(new_aces);
121
122     LOG(log_debug9, logtype_afpd, "remove_acl: END");
123     return ret;
124 }