smbd: Add mem_ctx to {f,}get_nt_acl VFS call
[samba.git] / source3 / modules / vfs_aixacl2.c
1 /*
2  * Convert JFS2 NFS4/AIXC acls to NT acls and vice versa.
3  *
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 "nfs4_acls.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_VFS
27
28 #define AIXACL2_MODULE_NAME "aixacl2"
29
30 extern SMB_ACL_T aixacl_to_smbacl( struct acl *file_acl);
31 extern struct acl *aixacl_smb_to_aixacl(SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
32
33 typedef union aixjfs2_acl_t {
34         nfs4_acl_int_t jfs2_acl[1];
35         aixc_acl_t aixc_acl[1];
36 }AIXJFS2_ACL_T;
37
38 static int32_t aixacl2_getlen(AIXJFS2_ACL_T *acl, acl_type_t *type)
39 {
40         int32_t len;
41  
42                 if(type->u64 == ACL_NFS4) {
43                         len = acl->jfs2_acl[0].aclLength;
44                 }       
45                 else {
46                         if(type->u64 == ACL_AIXC) {
47                                 len = acl->aixc_acl[0].acl_len;
48                         } else {
49                                 DEBUG(0,("aixacl2_getlen:unknown type:%d\n",type->u64));
50                                 return False;
51                         }       
52                 }               
53                 DEBUG(10,("aixacl2_getlen:%d\n",len));
54         return len;
55 }
56
57 static AIXJFS2_ACL_T *aixjfs2_getacl_alloc(const char *fname, acl_type_t *type)
58 {
59         AIXJFS2_ACL_T *acl;
60         size_t len = 200;
61         mode_t mode;
62         int ret;
63         uint64_t ctl_flag=0;
64         TALLOC_CTX      *mem_ctx;
65
66         mem_ctx = talloc_tos();
67         acl = (AIXJFS2_ACL_T *)TALLOC_SIZE(mem_ctx, len);
68         if (acl == NULL) {
69                 errno = ENOMEM;
70                 return NULL;
71         }
72
73         if(type->u64 == ACL_ANY) {
74                 ctl_flag = ctl_flag | GET_ACLINFO_ONLY;
75         }
76
77         ret = aclx_get((char *)fname, ctl_flag, type, acl, &len, &mode);
78         if ((ret != 0) && (errno == ENOSPC)) {
79                 len = aixacl2_getlen(acl, type) + sizeof(AIXJFS2_ACL_T);
80                 DEBUG(10,("aixjfs2_getacl_alloc - acl_len:%d\n",len));
81
82                 acl = (AIXJFS2_ACL_T *)TALLOC_SIZE(mem_ctx, len);
83                 if (acl == NULL) {
84                         errno = ENOMEM;
85                         return NULL;
86                 }
87
88                 ret = aclx_get((char *)fname, ctl_flag, type, acl, &len, &mode);
89         }
90         if (ret != 0) {
91                 DEBUG(8, ("aclx_get failed with %s\n", strerror(errno)));
92                 return NULL;
93         }
94
95         return acl;
96 }
97
98 static bool aixjfs2_get_nfs4_acl(const char *name,
99         SMB4ACL_T **ppacl, bool *pretryPosix)
100 {
101         int32_t i;
102         
103         AIXJFS2_ACL_T *pacl = NULL;
104         nfs4_acl_int_t *jfs2_acl = NULL;
105         nfs4_ace_int_t *jfs2_ace = NULL;
106         acl_type_t type;
107
108         DEBUG(10,("jfs2 get_nt_acl invoked for %s\n", name));
109
110         memset(&type, 0, sizeof(acl_type_t));
111         type.u64 = ACL_NFS4;
112
113         pacl = aixjfs2_getacl_alloc(name, &type);
114         if (pacl == NULL) {
115                 DEBUG(9, ("aixjfs2_getacl_alloc failed for %s with %s\n",
116                                 name, strerror(errno)));
117                 if (errno==ENOSYS)
118                         *pretryPosix = True;
119                 return False;
120         }
121
122         jfs2_acl = &pacl->jfs2_acl[0];
123         DEBUG(10, ("len: %d, version: %d, nace: %d, type: 0x%x\n",
124                         jfs2_acl->aclLength, jfs2_acl->aclVersion, jfs2_acl->aclEntryN, type.u64));
125
126         *ppacl = smb_create_smb4acl();
127         if (*ppacl==NULL)
128                 return False;
129
130         jfs2_ace = &jfs2_acl->aclEntry[0];
131         for (i=0; i<jfs2_acl->aclEntryN; i++) {
132                 SMB_ACE4PROP_T aceprop;
133
134                 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
135                                 "who: %d, aclLen: %d\n", jfs2_ace->aceType, jfs2_ace->flags,
136                                 jfs2_ace->aceFlags, jfs2_ace->aceMask, jfs2_ace->aceWho.id, jfs2_ace->entryLen));
137
138                 aceprop.aceType = jfs2_ace->aceType;
139                 aceprop.aceFlags = jfs2_ace->aceFlags;
140                 aceprop.aceMask = jfs2_ace->aceMask;
141                 aceprop.flags = (jfs2_ace->flags&ACE4_ID_SPECIAL) ? SMB_ACE4_ID_SPECIAL : 0;
142
143                 /* don't care it's real content is only 16 or 32 bit */
144                 aceprop.who.id = jfs2_ace->aceWho.id;
145
146                 if (smb_add_ace4(*ppacl, &aceprop)==NULL)
147                         return False;
148
149                 /* iterate to the next jfs2 ace */
150                 jfs2_ace = (nfs4_ace_int_t *)(((char *)jfs2_ace) + jfs2_ace->entryLen);
151         }
152
153         DEBUG(10,("jfs2 get_nt_acl finished successfully\n"));
154
155         return True;
156 }
157
158 static NTSTATUS aixjfs2_fget_nt_acl(vfs_handle_struct *handle,
159         files_struct *fsp, uint32 security_info,
160         TALLOC_CTX *mem_ctx,
161         struct security_descriptor **ppdesc)
162 {
163         SMB4ACL_T *pacl = NULL;
164         bool    result;
165         bool    retryPosix = False;
166
167         *ppdesc = NULL;
168         result = aixjfs2_get_nfs4_acl(fsp->fsp_name->base_name, &pacl,
169                                       &retryPosix);
170         if (retryPosix)
171         {
172                 DEBUG(10, ("retrying with posix acl...\n"));
173                 return posix_fget_nt_acl(fsp, security_info,
174                                          mem_ctx, ppdesc);
175         }
176         if (result==False)
177                 return NT_STATUS_ACCESS_DENIED;
178
179         return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc,
180                                     mem_ctx, pacl);
181 }
182
183 static NTSTATUS aixjfs2_get_nt_acl(vfs_handle_struct *handle,
184         const char *name,
185         uint32 security_info,
186         TALLOC_CTX *mem_ctx,
187         struct security_descriptor **ppdesc)
188 {
189         SMB4ACL_T *pacl = NULL;
190         bool    result;
191         bool    retryPosix = False;
192
193         *ppdesc = NULL;
194         result = aixjfs2_get_nfs4_acl(name, &pacl, &retryPosix);
195         if (retryPosix)
196         {
197                 DEBUG(10, ("retrying with posix acl...\n"));
198                 return posix_get_nt_acl(handle->conn, name, security_info,
199                                         mem_ctx, ppdesc);
200         }
201         if (result==False)
202                 return NT_STATUS_ACCESS_DENIED;
203
204         return smb_get_nt_acl_nfs4(handle->conn, name, security_info,
205                                    mem_ctx, ppdesc,
206                                    pacl);
207 }
208
209 static SMB_ACL_T aixjfs2_get_posix_acl(const char *path, acl_type_t type, TALLOC_CTX *mem_ctx)
210 {
211         aixc_acl_t *pacl;
212         AIXJFS2_ACL_T *acl;
213         SMB_ACL_T result = NULL;
214         int ret;
215
216         acl = aixjfs2_getacl_alloc(path, &type);
217
218         if (acl == NULL) {
219                 DEBUG(10, ("aixjfs2_getacl failed for %s with %s\n",
220                            path, strerror(errno)));
221                 if (errno == 0) {
222                         errno = EINVAL;
223                 }
224                 goto done;
225         }
226
227         pacl = &acl->aixc_acl[0];
228         DEBUG(10, ("len: %d, mode: %d\n",
229                    pacl->acl_len, pacl->acl_mode));
230
231         result = aixacl_to_smbacl(pacl, mem_ctx);
232         if (result == NULL) {
233                 goto done;
234         }
235
236  done:
237         if (errno != 0) {
238                 TALLOC_FREE(result);
239         }
240         return result;
241 }
242
243 SMB_ACL_T aixjfs2_sys_acl_get_file(vfs_handle_struct *handle,
244                                     const char *path_p,
245                                    SMB_ACL_TYPE_T type,
246                                    TALLOC_CTX *mem_ctx)
247 {
248         acl_type_t aixjfs2_type;
249
250         switch(type) {
251         case SMB_ACL_TYPE_ACCESS:
252                 aixjfs2_type.u64 = ACL_AIXC;
253                 break;
254         case SMB_ACL_TYPE_DEFAULT:
255                 DEBUG(0, ("Got AIX JFS2 unsupported type: %d\n", type));
256                 return NULL;
257         default:
258                 DEBUG(0, ("Got invalid type: %d\n", type));
259                 smb_panic("exiting");
260         }
261
262         return aixjfs2_get_posix_acl(path_p, aixjfs2_type, mem_ctx);
263 }
264
265 SMB_ACL_T aixjfs2_sys_acl_get_fd(vfs_handle_struct *handle,
266                                   files_struct *fsp)
267 {
268         acl_type_t aixjfs2_type;
269         aixjfs2_type.u64 = ACL_AIXC;
270
271         return aixjfs2_get_posix_acl(fsp->fsp_name->base_name, aixjfs2_type);
272 }
273
274 /*
275  * Test whether we have that aclType support on the given path
276  */
277 static int aixjfs2_query_acl_support(
278         char *filepath,
279         uint64_t aclType,
280         acl_type_t *pacl_type_info
281 )
282 {
283         acl_types_list_t        acl_type_list;
284         size_t  acl_type_list_len = sizeof(acl_types_list_t);
285         uint32_t        i;
286
287         memset(&acl_type_list, 0, sizeof(acl_type_list));
288
289         if (aclx_gettypes(filepath, &acl_type_list, &acl_type_list_len)) {
290                 DEBUG(2, ("aclx_gettypes failed with error %s for %s\n",
291                         strerror(errno), filepath));
292                 return -1;
293         }
294
295         for(i=0; i<acl_type_list.num_entries; i++) {
296                 if (acl_type_list.entries[i].u64==aclType) {
297                         memcpy(pacl_type_info, acl_type_list.entries + i, sizeof(acl_type_t));
298                         DEBUG(10, ("found %s ACL support for %s\n",
299                                 pacl_type_info->acl_type, filepath));
300                         return 0;
301                 }
302         }
303
304         return 1; /* haven't found that ACL type. */
305 }
306
307 static bool aixjfs2_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
308 {
309         SMB4ACE_T       *smbace;
310         TALLOC_CTX      *mem_ctx;
311         nfs4_acl_int_t  *jfs2acl;
312         int32_t entryLen;
313         uint32  aclLen, naces;
314         int     rc;
315         acl_type_t      acltype;
316
317         DEBUG(10, ("jfs2_process_smbacl invoked on %s\n", fsp_str_dbg(fsp)));
318
319         /* no need to be freed which is alloced with mem_ctx */
320         mem_ctx = talloc_tos();
321
322         entryLen = sizeof(nfs4_ace_int_t);
323         if (entryLen & 0x03)
324                 entryLen = entryLen + 4 - (entryLen%4);
325
326         naces = smb_get_naces(smbacl);
327         aclLen = ACL_V4_SIZ + naces * entryLen;
328         jfs2acl = (nfs4_acl_int_t *)TALLOC_SIZE(mem_ctx, aclLen);
329         if (jfs2acl==NULL) {
330                 DEBUG(0, ("TALLOC_SIZE failed\n"));
331                 errno = ENOMEM;
332                 return False;
333         }
334
335         jfs2acl->aclLength = ACL_V4_SIZ;
336         jfs2acl->aclVersion = NFS4_ACL_INT_STRUCT_VERSION;
337         jfs2acl->aclEntryN = 0;
338
339         for(smbace = smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace))
340         {
341                 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
342                 nfs4_ace_int_t *jfs2_ace = (nfs4_ace_int_t *)(((char *)jfs2acl) + jfs2acl->aclLength);
343
344                 memset(jfs2_ace, 0, entryLen);
345                 jfs2_ace->entryLen = entryLen; /* won't store textual "who" */
346                 jfs2_ace->aceType = aceprop->aceType; /* only ACCES|DENY supported by jfs2 */
347                 jfs2_ace->aceFlags = aceprop->aceFlags;
348                 jfs2_ace->aceMask = aceprop->aceMask;
349                 jfs2_ace->flags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_ID_SPECIAL : 0;
350
351                 /* don't care it's real content is only 16 or 32 bit */
352                 jfs2_ace->aceWho.id = aceprop->who.id;
353
354                 /* iterate to the next jfs2 ace */
355                 jfs2acl->aclLength += jfs2_ace->entryLen;
356                 jfs2acl->aclEntryN++;
357         }
358         SMB_ASSERT(jfs2acl->aclEntryN==naces);
359
360         /* Don't query it (again) */
361         memset(&acltype, 0, sizeof(acl_type_t));
362         acltype.u64 = ACL_NFS4;
363
364         /* won't set S_ISUID - the only one JFS2/NFS4 accepts */
365         rc = aclx_put(
366                 fsp->fsp_name->base_name,
367                 SET_ACL, /* set only the ACL, not mode bits */
368                 acltype, /* not a pointer !!! */
369                 jfs2acl,
370                 jfs2acl->aclLength,
371                 0 /* don't set here mode bits */
372         );
373         if (rc) {
374                 DEBUG(8, ("aclx_put failed with %s\n", strerror(errno)));
375                 return False;
376         }
377
378         DEBUG(10, ("jfs2_process_smbacl succeeded.\n"));
379         return True;
380 }
381
382 static NTSTATUS aixjfs2_set_nt_acl_common(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
383 {
384         acl_type_t      acl_type_info;
385         NTSTATUS        result = NT_STATUS_ACCESS_DENIED;
386         int     rc;
387
388         rc = aixjfs2_query_acl_support(
389                 fsp->fsp_name,
390                 ACL_NFS4,
391                 &acl_type_info);
392
393         if (rc==0)
394         {
395                 result = smb_set_nt_acl_nfs4(
396                         fsp, security_info_sent, psd,
397                         aixjfs2_process_smbacl);
398         } else if (rc==1) { /* assume POSIX ACL - by default... */
399                 result = set_nt_acl(fsp, security_info_sent, psd);
400         } else
401                 result = map_nt_error_from_unix(errno); /* query failed */
402         
403         return result;
404 }
405
406 NTSTATUS aixjfs2_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
407 {
408         return aixjfs2_set_nt_acl_common(fsp, security_info_sent, psd);
409 }
410
411 int aixjfs2_sys_acl_set_file(vfs_handle_struct *handle,
412                               const char *name,
413                               SMB_ACL_TYPE_T type,
414                               SMB_ACL_T theacl)
415 {
416         struct acl      *acl_aixc;
417         acl_type_t      acl_type_info;
418         int     rc;
419
420         DEBUG(10, ("aixjfs2_sys_acl_set_file invoked for %s", name));
421
422         rc = aixjfs2_query_acl_support((char *)name, ACL_AIXC, &acl_type_info);
423         if (rc) {
424                 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
425                 return -1;
426         }
427
428         acl_aixc = aixacl_smb_to_aixacl(type, theacl);
429         if (!acl_aixc)
430                 return -1;
431
432         rc = aclx_put(
433                 (char *)name,
434                 SET_ACL, /* set only the ACL, not mode bits */
435                 acl_type_info,
436                 acl_aixc,
437                 acl_aixc->acl_len,
438                 0
439         );
440         if (rc) {
441                 DEBUG(2, ("aclx_put failed with %s for %s\n",
442                         strerror(errno), name));
443                 return -1;
444         }
445
446         return 0;
447 }
448
449 int aixjfs2_sys_acl_set_fd(vfs_handle_struct *handle,
450                             files_struct *fsp,
451                             SMB_ACL_T theacl)
452 {
453         struct acl      *acl_aixc;
454         acl_type_t      acl_type_info;
455         int     rc;
456
457         DEBUG(10, ("aixjfs2_sys_acl_set_fd invoked for %s", fsp_str_dbg(fsp)));
458
459         rc = aixjfs2_query_acl_support(fsp->fsp_name->base_name, ACL_AIXC,
460                                        &acl_type_info);
461         if (rc) {
462                 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
463                 return -1;
464         }
465
466         acl_aixc = aixacl_smb_to_aixacl(SMB_ACL_TYPE_ACCESS, theacl);
467         if (!acl_aixc)
468                 return -1;
469
470         rc = aclx_fput(
471                 fsp->fh->fd,
472                 SET_ACL, /* set only the ACL, not mode bits */
473                 acl_type_info,
474                 acl_aixc,
475                 acl_aixc->acl_len,
476                 0
477         );
478         if (rc) {
479                 DEBUG(2, ("aclx_fput failed with %s for %s\n",
480                         strerror(errno), fsp_str_dbg(fsp)));
481                 return -1;
482         }
483
484         return 0;
485 }
486
487 int aixjfs2_sys_acl_delete_def_file(vfs_handle_struct *handle,
488                                      const char *path)
489 {
490         /* Not available under AIXC ACL */
491         /* Don't report here any error otherwise */
492         /* upper layer will break the normal execution */
493         return 0;
494 }
495
496 static struct vfs_fn_pointers vfs_aixacl2_fns = {
497         .fget_nt_acl_fn = aixjfs2_fget_nt_acl,
498         .get_nt_acl_fn = aixjfs2_get_nt_acl,
499         .fset_nt_acl_fn = aixjfs2_fset_nt_acl,
500         .sys_acl_get_file_fn = aixjfs2_sys_acl_get_file,
501         .sys_acl_get_fd_fn = aixjfs2_sys_acl_get_fd,
502         .sys_acl_set_file_fn = aixjfs2_sys_acl_set_file,
503         .sys_acl_set_fd_fn = aixjfs2_sys_acl_set_fd,
504         .sys_acl_delete_def_file_fn = aixjfs2_sys_acl_delete_def_file
505 };
506
507 NTSTATUS vfs_aixacl2_init(void);
508 NTSTATUS vfs_aixacl2_init(void)
509 {
510         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, AIXACL2_MODULE_NAME,
511                                 &vfs_aixacl2_fns);
512 }