-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathacl_tools_posix.c
281 lines (249 loc) · 7.49 KB
/
acl_tools_posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Copyright 2021 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <platform.h>
#include <cf3.defs.h>
#include <acl_tools.h>
#ifdef HAVE_ACL_H
# include <acl.h>
#endif
#ifdef HAVE_SYS_ACL_H
# include <sys/acl.h>
#endif
#ifdef HAVE_ACL_LIBACL_H
# include <acl/libacl.h>
#endif
#ifdef HAVE_LIBACL
#include <pwd.h> /* getpwnam */
bool CopyACLs(const char *src, const char *dst, bool *change)
{
acl_t acls;
struct stat statbuf;
int ret;
acls = acl_get_file(src, ACL_TYPE_ACCESS);
if (!acls)
{
if (errno == ENOTSUP)
{
if (change != NULL)
{
*change = false;
}
return true;
}
else
{
Log(LOG_LEVEL_ERR, "Can't copy ACLs from '%s'. (acl_get_file: %s)", src, GetErrorStr());
return false;
}
}
ret = acl_set_file(dst, ACL_TYPE_ACCESS, acls);
acl_free(acls);
if (ret != 0)
{
if (errno == ENOTSUP)
{
if (change != NULL)
{
*change = false;
}
return true;
}
else
{
Log(LOG_LEVEL_ERR, "Can't copy ACLs to '%s'. (acl_set_file: %s)", dst, GetErrorStr());
return false;
}
}
if (stat(src, &statbuf) != 0)
{
Log(LOG_LEVEL_ERR, "Can't copy ACLs from '%s'. (stat: %s)", src, GetErrorStr());
return false;
}
if (!S_ISDIR(statbuf.st_mode))
{
if (change != NULL)
{
*change = false;
}
return true;
}
// For directory, copy default ACL too.
acls = acl_get_file(src, ACL_TYPE_DEFAULT);
if (!acls)
{
Log(LOG_LEVEL_ERR, "Can't copy ACLs from '%s'. (acl_get_file: %s)", src, GetErrorStr());
return false;
}
ret = acl_set_file(dst, ACL_TYPE_DEFAULT, acls);
acl_free(acls);
if (ret != 0)
{
Log(LOG_LEVEL_ERR, "Can't copy ACLs to '%s'. (acl_set_file: %s)", dst, GetErrorStr());
return false;
}
if (change != NULL)
{
*change = true;
}
return true;
}
bool AllowAccessForUsers(const char *path, StringSet *users, bool allow_writes, bool allow_execute)
{
assert(path != NULL);
assert(users != NULL);
acl_t acl = acl_get_file(path, ACL_TYPE_ACCESS);
if (acl == NULL)
{
Log(LOG_LEVEL_ERR, "Failed to get ACLs for '%s': %s", path, GetErrorStr());
return false;
}
acl_entry_t entry;
int ret = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
while (ret > 0)
{
acl_tag_t entry_tag;
if (acl_get_tag_type(entry, &entry_tag) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to get ACL entry type: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (entry_tag == ACL_USER)
{
if (acl_delete_entry(acl, entry) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to remove user ACL entry: %s", GetErrorStr());
acl_free(acl);
return false;
}
}
ret = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
}
if (ret == -1)
{
Log(LOG_LEVEL_ERR, "Failed to get ACL entries: %s", GetErrorStr());
acl_free(acl);
return false;
}
StringSetIterator iter = StringSetIteratorInit(users);
const char *user = NULL;
while ((user = StringSetIteratorNext(&iter)) != NULL)
{
struct passwd *pw = getpwnam(user);
if (pw == NULL)
{
Log(LOG_LEVEL_ERR, "Failed to get UID for user '%s': %s",
user, GetErrorStr());
acl_free(acl);
return false;
}
uid_t uid = pw->pw_uid;
if (acl_create_entry(&acl, &entry) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to create a new ACL entry: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (acl_set_tag_type(entry, ACL_USER) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to set ACL entry type: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (acl_set_qualifier(entry, &uid) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to set ACL entry qualifier: %s", GetErrorStr());
acl_free(acl);
return false;
}
acl_permset_t permset;
if (acl_get_permset(entry, &permset) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to get permset: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (acl_clear_perms(permset) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to clear permset: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (acl_add_perm(permset, ACL_READ) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to add read permission to set: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (allow_writes && (acl_add_perm(permset, ACL_WRITE) == -1))
{
Log(LOG_LEVEL_ERR, "Failed to add write permission to set: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (allow_execute && (acl_add_perm(permset, ACL_EXECUTE) == -1))
{
Log(LOG_LEVEL_ERR, "Failed to add execute permission to set: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (acl_set_permset(entry, permset) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to set permset: %s", GetErrorStr());
acl_free(acl);
return false;
}
}
if (acl_calc_mask(&acl) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to recalculate mask: %s", GetErrorStr());
acl_free(acl);
return false;
}
if (acl_valid(acl) != 0)
{
Log(LOG_LEVEL_ERR, "Ended up with an invalid ACL");
acl_free(acl);
return false;
}
if (acl_set_file(path, ACL_TYPE_ACCESS, acl) == -1)
{
Log(LOG_LEVEL_ERR, "Failed to set ACL: %s", GetErrorStr());
acl_free(acl);
return false;
}
acl_free(acl);
return true;
}
#elif !defined(__MINGW32__) /* !HAVE_LIBACL */
bool CopyACLs(ARG_UNUSED const char *src, ARG_UNUSED const char *dst, bool *change)
{
if (change != NULL)
{
*change = false;
}
return true;
}
bool AllowAccessForUsers(ARG_UNUSED const char *path, ARG_UNUSED StringSet *users,
ARG_UNUSED bool allow_writes, ARG_UNUSED bool allow_execute)
{
Log(LOG_LEVEL_ERR, "ACL manipulation not supported");
return false;
}
#endif