s3: VFS: posixacl: Remove posixacl_sys_acl_get_file().
[samba.git] / source3 / modules / vfs_posixacl.c
1 /*
2    Unix SMB/Netbios implementation.
3    VFS module to get and set posix acls
4    Copyright (C) Volker Lendecke 2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "modules/vfs_posixacl.h"
24
25 /* prototypes for static functions first - for clarity */
26
27 static bool smb_ace_to_internal(acl_entry_t posix_ace,
28                                 struct smb_acl_entry *ace);
29 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx);
30 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm);
31 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl);
32
33
34 /* public functions - the api */
35
36 SMB_ACL_T posixacl_sys_acl_get_fd(vfs_handle_struct *handle,
37                                   files_struct *fsp,
38                                   SMB_ACL_TYPE_T type,
39                                   TALLOC_CTX *mem_ctx)
40 {
41         struct smb_acl_t *result;
42         acl_t acl = NULL;
43         acl_type_t acl_type;
44
45         switch(type) {
46         case SMB_ACL_TYPE_ACCESS:
47                 acl_type = ACL_TYPE_ACCESS;
48                 break;
49         case SMB_ACL_TYPE_DEFAULT:
50                 acl_type = ACL_TYPE_DEFAULT;
51                 break;
52         default:
53                 errno = EINVAL;
54                 return NULL;
55         }
56         if (!fsp->fsp_flags.is_pathref && (acl_type == ACL_TYPE_ACCESS)) {
57                 /* POSIX API only allows ACL_TYPE_ACCESS fetched on fd. */
58                 acl = acl_get_fd(fsp_get_io_fd(fsp));
59         } else if (fsp->fsp_flags.have_proc_fds) {
60                 int fd = fsp_get_pathref_fd(fsp);
61                 const char *proc_fd_path = NULL;
62                 char buf[PATH_MAX];
63
64                 proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
65                 if (proc_fd_path == NULL) {
66                         return NULL;
67                 }
68
69                 acl = acl_get_file(proc_fd_path, acl_type);
70         } else {
71                 /*
72                  * This is no longer a handle based call.
73                  */
74                 acl = acl_get_file(fsp->fsp_name->base_name, acl_type);
75         }
76         if (acl == NULL) {
77                 return NULL;
78         }
79
80         result = smb_acl_to_internal(acl, mem_ctx);
81         acl_free(acl);
82         return result;
83 }
84
85 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
86                               const struct smb_filename *smb_fname,
87                               SMB_ACL_TYPE_T type,
88                               SMB_ACL_T theacl)
89 {
90         int res;
91         acl_type_t acl_type;
92         acl_t acl;
93
94         DEBUG(10, ("Calling acl_set_file: %s, %d\n",
95                         smb_fname->base_name,
96                         type));
97
98         switch(type) {
99         case SMB_ACL_TYPE_ACCESS:
100                 acl_type = ACL_TYPE_ACCESS;
101                 break;
102         case SMB_ACL_TYPE_DEFAULT:
103                 acl_type = ACL_TYPE_DEFAULT;
104                 break;
105         default:
106                 errno = EINVAL;
107                 return -1;
108         }
109
110         if ((acl = smb_acl_to_posix(theacl)) == NULL) {
111                 return -1;
112         }
113         res = acl_set_file(smb_fname->base_name, acl_type, acl);
114         if (res != 0) {
115                 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
116         }
117         acl_free(acl);
118         return res;
119 }
120
121 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
122                             files_struct *fsp,
123                             SMB_ACL_TYPE_T type,
124                             SMB_ACL_T theacl)
125 {
126         int res;
127         acl_t acl = smb_acl_to_posix(theacl);
128         acl_type_t acl_type;
129         int fd = fsp_get_pathref_fd(fsp);
130
131         if (acl == NULL) {
132                 return -1;
133         }
134
135         switch(type) {
136         case SMB_ACL_TYPE_ACCESS:
137                 acl_type = ACL_TYPE_ACCESS;
138                 break;
139         case SMB_ACL_TYPE_DEFAULT:
140                 acl_type = ACL_TYPE_DEFAULT;
141                 break;
142         default:
143                 acl_free(acl);
144                 errno = EINVAL;
145                 return -1;
146         }
147
148         if (!fsp->fsp_flags.is_pathref && type == SMB_ACL_TYPE_ACCESS) {
149                 res = acl_set_fd(fd, acl);
150         } else if (fsp->fsp_flags.have_proc_fds) {
151                 const char *proc_fd_path = NULL;
152                 char buf[PATH_MAX];
153
154                 proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
155                 if (proc_fd_path == NULL) {
156                         acl_free(acl);
157                         return -1;
158                 }
159                 res = acl_set_file(proc_fd_path, acl_type, acl);
160         } else {
161                 /*
162                  * This is no longer a handle based call.
163                  */
164                 res = acl_set_file(fsp->fsp_name->base_name,
165                                    acl_type,
166                                    acl);
167         }
168
169         acl_free(acl);
170         return res;
171 }
172
173 int posixacl_sys_acl_delete_def_fd(vfs_handle_struct *handle,
174                                 files_struct *fsp)
175 {
176         if (fsp->fsp_flags.have_proc_fds) {
177                 int fd = fsp_get_pathref_fd(fsp);
178                 const char *proc_fd_path = NULL;
179                 char buf[PATH_MAX];
180
181                 proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
182                 if (proc_fd_path == NULL) {
183                         return -1;
184                 }
185                 return acl_delete_def_file(proc_fd_path);
186         }
187
188         /*
189          * This is no longer a handle based call.
190          */
191         return acl_delete_def_file(fsp->fsp_name->base_name);
192 }
193
194 /* private functions */
195
196 static bool smb_ace_to_internal(acl_entry_t posix_ace,
197                                 struct smb_acl_entry *ace)
198 {
199         acl_tag_t tag;
200         acl_permset_t permset;
201
202         if (acl_get_tag_type(posix_ace, &tag) != 0) {
203                 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
204                 return False;
205         }
206
207         switch(tag) {
208         case ACL_USER:
209                 ace->a_type = SMB_ACL_USER;
210                 break;
211         case ACL_USER_OBJ:
212                 ace->a_type = SMB_ACL_USER_OBJ;
213                 break;
214         case ACL_GROUP:
215                 ace->a_type = SMB_ACL_GROUP;
216                 break;
217         case ACL_GROUP_OBJ:
218                 ace->a_type = SMB_ACL_GROUP_OBJ;
219                 break;
220         case ACL_OTHER:
221                 ace->a_type = SMB_ACL_OTHER;
222                 break;
223         case ACL_MASK:
224                 ace->a_type = SMB_ACL_MASK;
225                 break;
226 #ifdef HAVE_ACL_EVERYONE
227         case ACL_EVERYONE:
228                 DEBUG(1, ("ACL tag type ACL_EVERYONE. FreeBSD with ZFS? Use 'vfs objects = zfsacl'\n"));
229                 return false;
230 #endif
231         default:
232                 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
233                 return False;
234         }
235         switch(ace->a_type) {
236         case SMB_ACL_USER: {
237                 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
238                 if (puid == NULL) {
239                         DEBUG(0, ("smb_acl_get_qualifier failed\n"));
240                         return False;
241                 }
242                 ace->info.user.uid = *puid;
243                 acl_free(puid);
244                 break;
245         }
246
247         case SMB_ACL_GROUP: {
248                 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
249                 if (pgid == NULL) {
250                         DEBUG(0, ("smb_acl_get_qualifier failed\n"));
251                         return False;
252                 }
253                 ace->info.group.gid = *pgid;
254                 acl_free(pgid);
255                 break;
256         }
257         default:
258                 break;
259         }
260         if (acl_get_permset(posix_ace, &permset) != 0) {
261                 DEBUG(0, ("smb_acl_get_mode failed\n"));
262                 return False;
263         }
264         ace->a_perm = 0;
265 #ifdef HAVE_ACL_GET_PERM_NP
266         ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
267         ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
268         ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
269 #else
270         ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
271         ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
272         ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
273 #endif
274         return True;
275 }
276
277 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx)
278 {
279         struct smb_acl_t *result = sys_acl_init(mem_ctx);
280         int entry_id = ACL_FIRST_ENTRY;
281         acl_entry_t e;
282         if (result == NULL) {
283                 return NULL;
284         }
285         while (acl_get_entry(acl, entry_id, &e) == 1) {
286
287                 entry_id = ACL_NEXT_ENTRY;
288
289                 result->acl = talloc_realloc(result, result->acl,
290                                              struct smb_acl_entry, result->count+1);
291                 if (result->acl == NULL) {
292                         TALLOC_FREE(result);
293                         DEBUG(0, ("talloc_realloc failed\n"));
294                         errno = ENOMEM;
295                         return NULL;
296                 }
297
298                 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
299                         TALLOC_FREE(result);
300                         return NULL;
301                 }
302
303                 result->count += 1;
304         }
305         return result;
306 }
307
308 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
309 {
310         int ret;
311         acl_permset_t permset;
312
313         if ((ret = acl_get_permset(entry, &permset)) != 0) {
314                 return ret;
315         }
316         if ((ret = acl_clear_perms(permset)) != 0) {
317                 return ret;
318         }
319         if ((perm & SMB_ACL_READ) &&
320             ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
321                 return ret;
322         }
323         if ((perm & SMB_ACL_WRITE) &&
324             ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
325                 return ret;
326         }
327         if ((perm & SMB_ACL_EXECUTE) &&
328             ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
329                 return ret;
330         }
331
332         return 0;
333 }
334
335 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
336 {
337         acl_t result;
338         int i;
339
340         result = acl_init(acl->count);
341         if (result == NULL) {
342                 DEBUG(10, ("acl_init failed\n"));
343                 return NULL;
344         }
345
346         for (i=0; i<acl->count; i++) {
347                 const struct smb_acl_entry *entry = &acl->acl[i];
348                 acl_entry_t e;
349                 acl_tag_t tag;
350
351                 if (acl_create_entry(&result, &e) != 0) {
352                         DEBUG(1, ("acl_create_entry failed: %s\n",
353                                   strerror(errno)));
354                         goto fail;
355                 }
356
357                 switch (entry->a_type) {
358                 case SMB_ACL_USER:
359                         tag = ACL_USER;
360                         break;
361                 case SMB_ACL_USER_OBJ:
362                         tag = ACL_USER_OBJ;
363                         break;
364                 case SMB_ACL_GROUP:
365                         tag = ACL_GROUP;
366                         break;
367                 case SMB_ACL_GROUP_OBJ:
368                         tag = ACL_GROUP_OBJ;
369                         break;
370                 case SMB_ACL_OTHER:
371                         tag = ACL_OTHER;
372                         break;
373                 case SMB_ACL_MASK:
374                         tag = ACL_MASK;
375                         break;
376                 default:
377                         DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
378                         goto fail;
379                 }
380
381                 if (acl_set_tag_type(e, tag) != 0) {
382                         DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
383                                    tag, strerror(errno)));
384                         goto fail;
385                 }
386
387                 switch (entry->a_type) {
388                 case SMB_ACL_USER:
389                         if (acl_set_qualifier(e, &entry->info.user.uid) != 0) {
390                                 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
391                                           strerror(errno)));
392                                 goto fail;
393                         }
394                         break;
395                 case SMB_ACL_GROUP:
396                         if (acl_set_qualifier(e, &entry->info.group.gid) != 0) {
397                                 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
398                                           strerror(errno)));
399                                 goto fail;
400                         }
401                         break;
402                 default:        /* Shut up, compiler! :-) */
403                         break;
404                 }
405
406                 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
407                         goto fail;
408                 }
409         }
410
411         if (acl_valid(result) != 0) {
412                 char *acl_string = sys_acl_to_text(acl, NULL);
413                 DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
414                           acl_string, strerror(errno)));
415                 SAFE_FREE(acl_string);
416                 goto fail;
417         }
418
419         return result;
420
421  fail:
422         if (result != NULL) {
423                 acl_free(result);
424         }
425         return NULL;
426 }
427
428 /* VFS operations structure */
429
430 static struct vfs_fn_pointers posixacl_fns = {
431         .sys_acl_get_fd_fn = posixacl_sys_acl_get_fd,
432         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
433         .sys_acl_set_fd_fn = posixacl_sys_acl_set_fd,
434         .sys_acl_delete_def_fd_fn = posixacl_sys_acl_delete_def_fd,
435 };
436
437 static_decl_vfs;
438 NTSTATUS vfs_posixacl_init(TALLOC_CTX *ctx)
439 {
440         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",
441                                 &posixacl_fns);
442 }