2 * Convert JFS2 NFS4/AIXC acls to NT acls and vice versa.
4 * Copyright (C) Volker Lendecke, 2006
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.
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.
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/>.
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "nfs4_acls.h"
24 #include "vfs_aixacl_util.h"
27 #define DBGC_CLASS DBGC_VFS
29 #define AIXACL2_MODULE_NAME "aixacl2"
31 typedef union aixjfs2_acl_t
{
32 nfs4_acl_int_t jfs2_acl
[1];
33 aixc_acl_t aixc_acl
[1];
36 static int32_t aixacl2_getlen(AIXJFS2_ACL_T
*acl
, acl_type_t
*type
)
40 if(type
->u64
== ACL_NFS4
) {
41 len
= acl
->jfs2_acl
[0].aclLength
;
44 if(type
->u64
== ACL_AIXC
) {
45 len
= acl
->aixc_acl
[0].acl_len
;
47 DEBUG(0,("aixacl2_getlen:unknown type:%d\n",type
->u64
));
51 DEBUG(10,("aixacl2_getlen:%d\n",len
));
55 static AIXJFS2_ACL_T
*aixjfs2_getacl_alloc(const char *fname
, acl_type_t
*type
)
64 mem_ctx
= talloc_tos();
65 acl
= (AIXJFS2_ACL_T
*)TALLOC_SIZE(mem_ctx
, len
);
71 if(type
->u64
== ACL_ANY
) {
72 ctl_flag
= ctl_flag
| GET_ACLINFO_ONLY
;
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
));
80 acl
= (AIXJFS2_ACL_T
*)TALLOC_SIZE(mem_ctx
, len
);
86 ret
= aclx_get((char *)fname
, ctl_flag
, type
, acl
, &len
, &mode
);
89 DEBUG(8, ("aclx_get failed with %s\n", strerror(errno
)));
96 static bool aixjfs2_get_nfs4_acl(TALLOC_CTX
*mem_ctx
, const char *name
,
97 struct SMB4ACL_T
**ppacl
, bool *pretryPosix
)
101 AIXJFS2_ACL_T
*pacl
= NULL
;
102 nfs4_acl_int_t
*jfs2_acl
= NULL
;
103 nfs4_ace_int_t
*jfs2_ace
= NULL
;
106 DEBUG(10,("jfs2 get_nt_acl invoked for %s\n", name
));
108 memset(&type
, 0, sizeof(acl_type_t
));
111 pacl
= aixjfs2_getacl_alloc(name
, &type
);
113 DEBUG(9, ("aixjfs2_getacl_alloc failed for %s with %s\n",
114 name
, strerror(errno
)));
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
));
124 *ppacl
= smb_create_smb4acl(mem_ctx
);
128 jfs2_ace
= &jfs2_acl
->aclEntry
[0];
129 for (i
=0; i
<jfs2_acl
->aclEntryN
; i
++) {
130 SMB_ACE4PROP_T aceprop
;
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
));
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;
141 /* don't care it's real content is only 16 or 32 bit */
142 aceprop
.who
.id
= jfs2_ace
->aceWho
.id
;
144 if (smb_add_ace4(*ppacl
, &aceprop
)==NULL
)
147 /* iterate to the next jfs2 ace */
148 jfs2_ace
= (nfs4_ace_int_t
*)(((char *)jfs2_ace
) + jfs2_ace
->entryLen
);
151 DEBUG(10,("jfs2 get_nt_acl finished successfully\n"));
156 static NTSTATUS
aixjfs2_fget_nt_acl(vfs_handle_struct
*handle
,
157 files_struct
*fsp
, uint32_t security_info
,
159 struct security_descriptor
**ppdesc
)
162 struct SMB4ACL_T
*pacl
= NULL
;
164 bool retryPosix
= False
;
165 TALLOC_CTX
*frame
= talloc_stackframe();
168 result
= aixjfs2_get_nfs4_acl(frame
, fsp
->fsp_name
->base_name
, &pacl
,
173 DEBUG(10, ("retrying with posix acl...\n"));
174 return posix_fget_nt_acl(fsp
, security_info
,
179 return NT_STATUS_ACCESS_DENIED
;
182 status
= smb_fget_nt_acl_nfs4(fsp
, NULL
, security_info
, ppdesc
,
188 static NTSTATUS
aixjfs2_get_nt_acl(vfs_handle_struct
*handle
,
189 const struct smb_filename
*smb_fname
,
190 uint32_t security_info
,
192 struct security_descriptor
**ppdesc
)
194 struct SMB4ACL_T
*pacl
= NULL
;
196 bool retryPosix
= False
;
199 result
= aixjfs2_get_nfs4_acl(mem_ctx
,
200 smb_fname
->base_name
,
205 DEBUG(10, ("retrying with posix acl...\n"));
206 return posix_get_nt_acl(handle
->conn
,
213 return NT_STATUS_ACCESS_DENIED
;
215 return smb_get_nt_acl_nfs4(handle
->conn
,
224 static int aixjfs2_sys_acl_blob_get_file(vfs_handle_struct
*handle
,
225 const struct smb_filename
*smb_fname
,
227 char **blob_description
,
230 struct SMB4ACL_T
*pacl
= NULL
;
231 const char *path_p
= smb_fname
->base_name
;
233 bool retryPosix
= False
;
235 result
= aixjfs2_get_nfs4_acl(mem_ctx
, path_p
, &pacl
, &retryPosix
);
238 return posix_sys_acl_blob_get_file(handle
, smb_fname
, mem_ctx
,
239 blob_description
, blob
);
241 /* Now way to linarlise NFS4 ACLs at the moment, but the NT ACL is pretty close in this case */
246 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
)
248 struct SMB4ACL_T
*pacl
= NULL
;
250 bool retryPosix
= False
;
252 result
= aixjfs2_get_nfs4_acl(mem_ctx
, fsp
->fsp_name
->base_name
, &pacl
,
256 return posix_sys_acl_blob_get_fd(handle
, fsp
, mem_ctx
, blob_description
, blob
);
259 /* Now way to linarlise NFS4 ACLs at the moment, but the NT ACL is pretty close in this case */
264 static SMB_ACL_T
aixjfs2_get_posix_acl(const char *path
, acl_type_t type
, TALLOC_CTX
*mem_ctx
)
268 SMB_ACL_T result
= NULL
;
271 acl
= aixjfs2_getacl_alloc(path
, &type
);
274 DEBUG(10, ("aixjfs2_getacl failed for %s with %s\n",
275 path
, strerror(errno
)));
282 pacl
= &acl
->aixc_acl
[0];
283 DEBUG(10, ("len: %d, mode: %d\n",
284 pacl
->acl_len
, pacl
->acl_mode
));
286 result
= aixacl_to_smbacl(pacl
, mem_ctx
);
287 if (result
== NULL
) {
298 SMB_ACL_T
aixjfs2_sys_acl_get_file(vfs_handle_struct
*handle
,
299 const struct smb_filename
*smb_fname
,
303 acl_type_t aixjfs2_type
;
306 case SMB_ACL_TYPE_ACCESS
:
307 aixjfs2_type
.u64
= ACL_AIXC
;
309 case SMB_ACL_TYPE_DEFAULT
:
310 DEBUG(0, ("Got AIX JFS2 unsupported type: %d\n", type
));
313 DEBUG(0, ("Got invalid type: %d\n", type
));
314 smb_panic("exiting");
317 return aixjfs2_get_posix_acl(smb_fname
->base_name
,
318 aixjfs2_type
, mem_ctx
);
321 SMB_ACL_T
aixjfs2_sys_acl_get_fd(vfs_handle_struct
*handle
,
322 files_struct
*fsp
, TALLOC_CTX
*mem_ctx
)
324 acl_type_t aixjfs2_type
;
325 aixjfs2_type
.u64
= ACL_AIXC
;
327 return aixjfs2_get_posix_acl(fsp
->fsp_name
->base_name
,
328 aixjfs2_type
, mem_ctx
);
332 * Test whether we have that aclType support on the given path
334 static int aixjfs2_query_acl_support(
337 acl_type_t
*pacl_type_info
340 acl_types_list_t acl_type_list
;
341 size_t acl_type_list_len
= sizeof(acl_types_list_t
);
344 memset(&acl_type_list
, 0, sizeof(acl_type_list
));
346 if (aclx_gettypes(filepath
, &acl_type_list
, &acl_type_list_len
)) {
347 DEBUG(2, ("aclx_gettypes failed with error %s for %s\n",
348 strerror(errno
), filepath
));
352 for(i
=0; i
<acl_type_list
.num_entries
; i
++) {
353 if (acl_type_list
.entries
[i
].u64
==aclType
) {
354 memcpy(pacl_type_info
, acl_type_list
.entries
+ i
, sizeof(acl_type_t
));
355 DEBUG(10, ("found %s ACL support for %s\n",
356 pacl_type_info
->acl_type
, filepath
));
361 return 1; /* haven't found that ACL type. */
364 static bool aixjfs2_process_smbacl(vfs_handle_struct
*handle
,
366 struct SMB4ACL_T
*smbacl
)
368 struct SMB4ACE_T
*smbace
;
370 nfs4_acl_int_t
*jfs2acl
;
372 uint32_t aclLen
, naces
;
376 DEBUG(10, ("jfs2_process_smbacl invoked on %s\n", fsp_str_dbg(fsp
)));
378 /* no need to be freed which is alloced with mem_ctx */
379 mem_ctx
= talloc_tos();
381 entryLen
= sizeof(nfs4_ace_int_t
);
383 entryLen
= entryLen
+ 4 - (entryLen
%4);
385 naces
= smb_get_naces(smbacl
);
386 aclLen
= ACL_V4_SIZ
+ naces
* entryLen
;
387 jfs2acl
= (nfs4_acl_int_t
*)TALLOC_SIZE(mem_ctx
, aclLen
);
389 DEBUG(0, ("TALLOC_SIZE failed\n"));
394 jfs2acl
->aclLength
= ACL_V4_SIZ
;
395 jfs2acl
->aclVersion
= NFS4_ACL_INT_STRUCT_VERSION
;
396 jfs2acl
->aclEntryN
= 0;
398 for(smbace
= smb_first_ace4(smbacl
); smbace
!=NULL
; smbace
= smb_next_ace4(smbace
))
400 SMB_ACE4PROP_T
*aceprop
= smb_get_ace4(smbace
);
401 nfs4_ace_int_t
*jfs2_ace
= (nfs4_ace_int_t
*)(((char *)jfs2acl
) + jfs2acl
->aclLength
);
403 memset(jfs2_ace
, 0, entryLen
);
404 jfs2_ace
->entryLen
= entryLen
; /* won't store textual "who" */
405 jfs2_ace
->aceType
= aceprop
->aceType
; /* only ACCES|DENY supported by jfs2 */
406 jfs2_ace
->aceFlags
= aceprop
->aceFlags
;
407 jfs2_ace
->aceMask
= aceprop
->aceMask
;
408 jfs2_ace
->flags
= (aceprop
->flags
&SMB_ACE4_ID_SPECIAL
) ? ACE4_ID_SPECIAL
: 0;
410 /* don't care it's real content is only 16 or 32 bit */
411 jfs2_ace
->aceWho
.id
= aceprop
->who
.id
;
413 /* iterate to the next jfs2 ace */
414 jfs2acl
->aclLength
+= jfs2_ace
->entryLen
;
415 jfs2acl
->aclEntryN
++;
417 SMB_ASSERT(jfs2acl
->aclEntryN
==naces
);
419 /* Don't query it (again) */
420 memset(&acltype
, 0, sizeof(acl_type_t
));
421 acltype
.u64
= ACL_NFS4
;
423 /* won't set S_ISUID - the only one JFS2/NFS4 accepts */
425 fsp
->fsp_name
->base_name
,
426 SET_ACL
, /* set only the ACL, not mode bits */
427 acltype
, /* not a pointer !!! */
430 0 /* don't set here mode bits */
433 DEBUG(8, ("aclx_put failed with %s\n", strerror(errno
)));
437 DEBUG(10, ("jfs2_process_smbacl succeeded.\n"));
441 static NTSTATUS
aixjfs2_set_nt_acl_common(vfs_handle_struct
*handle
, files_struct
*fsp
, uint32_t security_info_sent
, const struct security_descriptor
*psd
)
443 acl_type_t acl_type_info
;
444 NTSTATUS result
= NT_STATUS_ACCESS_DENIED
;
447 rc
= aixjfs2_query_acl_support(
448 fsp
->fsp_name
->base_name
,
454 result
= smb_set_nt_acl_nfs4(handle
,
455 fsp
, NULL
, security_info_sent
, psd
,
456 aixjfs2_process_smbacl
);
457 } else if (rc
==1) { /* assume POSIX ACL - by default... */
458 result
= set_nt_acl(fsp
, security_info_sent
, psd
);
460 result
= map_nt_error_from_unix(errno
); /* query failed */
465 NTSTATUS
aixjfs2_fset_nt_acl(vfs_handle_struct
*handle
, files_struct
*fsp
, uint32_t security_info_sent
, const struct security_descriptor
*psd
)
467 return aixjfs2_set_nt_acl_common(handle
, fsp
, security_info_sent
, psd
);
470 int aixjfs2_sys_acl_set_file(vfs_handle_struct
*handle
,
471 const struct smb_filename
*smb_fname
,
475 struct acl
*acl_aixc
;
476 acl_type_t acl_type_info
;
479 DEBUG(10, ("aixjfs2_sys_acl_set_file invoked for %s",
480 smb_fname
->base_name
));
482 rc
= aixjfs2_query_acl_support((char *)smb_fname
->base_name
,
483 ACL_AIXC
, &acl_type_info
);
485 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
489 acl_aixc
= aixacl_smb_to_aixacl(type
, theacl
);
494 (char *)smb_fname
->base_name
,
495 SET_ACL
, /* set only the ACL, not mode bits */
502 DEBUG(2, ("aclx_put failed with %s for %s\n",
503 strerror(errno
), smb_fname
->base_name
));
510 int aixjfs2_sys_acl_set_fd(vfs_handle_struct
*handle
,
514 struct acl
*acl_aixc
;
515 acl_type_t acl_type_info
;
518 DEBUG(10, ("aixjfs2_sys_acl_set_fd invoked for %s", fsp_str_dbg(fsp
)));
520 rc
= aixjfs2_query_acl_support(fsp
->fsp_name
->base_name
, ACL_AIXC
,
523 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
527 acl_aixc
= aixacl_smb_to_aixacl(SMB_ACL_TYPE_ACCESS
, theacl
);
533 SET_ACL
, /* set only the ACL, not mode bits */
540 DEBUG(2, ("aclx_fput failed with %s for %s\n",
541 strerror(errno
), fsp_str_dbg(fsp
)));
548 int aixjfs2_sys_acl_delete_def_file(vfs_handle_struct
*handle
,
549 const struct smb_filename
*smb_fname
)
551 /* Not available under AIXC ACL */
552 /* Don't report here any error otherwise */
553 /* upper layer will break the normal execution */
557 static struct vfs_fn_pointers vfs_aixacl2_fns
= {
558 .fget_nt_acl_fn
= aixjfs2_fget_nt_acl
,
559 .get_nt_acl_fn
= aixjfs2_get_nt_acl
,
560 .fset_nt_acl_fn
= aixjfs2_fset_nt_acl
,
561 .sys_acl_get_file_fn
= aixjfs2_sys_acl_get_file
,
562 .sys_acl_get_fd_fn
= aixjfs2_sys_acl_get_fd
,
563 .sys_acl_blob_get_file_fn
= aixjfs2_sys_acl_blob_get_file
,
564 .sys_acl_blob_get_fd_fn
= aixjfs2_sys_acl_blob_get_fd
,
565 .sys_acl_set_file_fn
= aixjfs2_sys_acl_set_file
,
566 .sys_acl_set_fd_fn
= aixjfs2_sys_acl_set_fd
,
567 .sys_acl_delete_def_file_fn
= aixjfs2_sys_acl_delete_def_file
571 NTSTATUS
vfs_aixacl2_init(TALLOC_CTX
*ctx
)
573 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, AIXACL2_MODULE_NAME
,