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