dbwrap: Use dbwrap_parse_record in dbwrap_fetch_uint32_bystring
[Samba/gbeck.git] / source3 / modules / vfs_aixacl2.c
blob65625d1853eb798553e02cb8b57b47e9a9f71508
1 /*
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/>.
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"
26 #undef DBGC_CLASS
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];
34 }AIXJFS2_ACL_T;
36 static int32_t aixacl2_getlen(AIXJFS2_ACL_T *acl, acl_type_t *type)
38 int32_t len;
40 if(type->u64 == ACL_NFS4) {
41 len = acl->jfs2_acl[0].aclLength;
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;
51 DEBUG(10,("aixacl2_getlen:%d\n",len));
52 return len;
55 static AIXJFS2_ACL_T *aixjfs2_getacl_alloc(const char *fname, acl_type_t *type)
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;
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;
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);
81 if (acl == NULL) {
82 errno = ENOMEM;
83 return NULL;
86 ret = aclx_get((char *)fname, ctl_flag, type, acl, &len, &mode);
88 if (ret != 0) {
89 DEBUG(8, ("aclx_get failed with %s\n", strerror(errno)));
90 return NULL;
93 return acl;
96 static bool aixjfs2_get_nfs4_acl(const char *name,
97 SMB4ACL_T **ppacl, bool *pretryPosix)
99 int32_t i;
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;
106 DEBUG(10,("jfs2 get_nt_acl invoked for %s\n", name));
108 memset(&type, 0, sizeof(acl_type_t));
109 type.u64 = ACL_NFS4;
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;
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();
125 if (*ppacl==NULL)
126 return False;
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)
145 return False;
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"));
153 return True;
156 static NTSTATUS aixjfs2_fget_nt_acl(vfs_handle_struct *handle,
157 files_struct *fsp, uint32 security_info,
158 TALLOC_CTX *mem_ctx,
159 struct security_descriptor **ppdesc)
161 SMB4ACL_T *pacl = NULL;
162 bool result;
163 bool retryPosix = False;
165 *ppdesc = NULL;
166 result = aixjfs2_get_nfs4_acl(fsp->fsp_name->base_name, &pacl,
167 &retryPosix);
168 if (retryPosix)
170 DEBUG(10, ("retrying with posix acl...\n"));
171 return posix_fget_nt_acl(fsp, security_info,
172 mem_ctx, ppdesc);
174 if (result==False)
175 return NT_STATUS_ACCESS_DENIED;
177 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc,
178 mem_ctx, pacl);
181 static NTSTATUS aixjfs2_get_nt_acl(vfs_handle_struct *handle,
182 const char *name,
183 uint32 security_info,
184 TALLOC_CTX *mem_ctx,
185 struct security_descriptor **ppdesc)
187 SMB4ACL_T *pacl = NULL;
188 bool result;
189 bool retryPosix = False;
191 *ppdesc = NULL;
192 result = aixjfs2_get_nfs4_acl(name, &pacl, &retryPosix);
193 if (retryPosix)
195 DEBUG(10, ("retrying with posix acl...\n"));
196 return posix_get_nt_acl(handle->conn, name, security_info,
197 mem_ctx, ppdesc);
199 if (result==False)
200 return NT_STATUS_ACCESS_DENIED;
202 return smb_get_nt_acl_nfs4(handle->conn, name, security_info,
203 mem_ctx, ppdesc,
204 pacl);
207 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)
209 SMB4ACL_T *pacl = NULL;
210 bool result;
211 bool retryPosix = False;
213 *ppdesc = NULL;
214 result = aixjfs2_get_nfs4_acl(path_p, &pacl, &retryPosix);
215 if (retryPosix)
217 return posix_sys_acl_blob_get_file(handle, path_p, mem_ctx,
218 blob_description, blob);
220 /* Now way to linarlise NFS4 ACLs at the moment, but the NT ACL is pretty close in this case */
221 errno = ENOSYS;
222 return -1;
225 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)
227 SMB4ACL_T *pacl = NULL;
228 bool result;
229 bool retryPosix = False;
231 result = aixjfs2_get_nfs4_acl(fsp->fsp_name->base_name, &pacl,
232 &retryPosix);
233 if (retryPosix)
235 return posix_sys_acl_blob_get_fd(handle, fsp, mem_ctx, blob_description, blob);
238 /* Now way to linarlise NFS4 ACLs at the moment, but the NT ACL is pretty close in this case */
239 errno = ENOSYS;
240 return -1;
243 static SMB_ACL_T aixjfs2_get_posix_acl(const char *path, acl_type_t type, TALLOC_CTX *mem_ctx)
245 aixc_acl_t *pacl;
246 AIXJFS2_ACL_T *acl;
247 SMB_ACL_T result = NULL;
248 int ret;
250 acl = aixjfs2_getacl_alloc(path, &type);
252 if (acl == NULL) {
253 DEBUG(10, ("aixjfs2_getacl failed for %s with %s\n",
254 path, strerror(errno)));
255 if (errno == 0) {
256 errno = EINVAL;
258 goto done;
261 pacl = &acl->aixc_acl[0];
262 DEBUG(10, ("len: %d, mode: %d\n",
263 pacl->acl_len, pacl->acl_mode));
265 result = aixacl_to_smbacl(pacl, mem_ctx);
266 if (result == NULL) {
267 goto done;
270 done:
271 if (errno != 0) {
272 TALLOC_FREE(result);
274 return result;
277 SMB_ACL_T aixjfs2_sys_acl_get_file(vfs_handle_struct *handle,
278 const char *path_p,
279 SMB_ACL_TYPE_T type,
280 TALLOC_CTX *mem_ctx)
282 acl_type_t aixjfs2_type;
284 switch(type) {
285 case SMB_ACL_TYPE_ACCESS:
286 aixjfs2_type.u64 = ACL_AIXC;
287 break;
288 case SMB_ACL_TYPE_DEFAULT:
289 DEBUG(0, ("Got AIX JFS2 unsupported type: %d\n", type));
290 return NULL;
291 default:
292 DEBUG(0, ("Got invalid type: %d\n", type));
293 smb_panic("exiting");
296 return aixjfs2_get_posix_acl(path_p, aixjfs2_type, mem_ctx);
299 SMB_ACL_T aixjfs2_sys_acl_get_fd(vfs_handle_struct *handle,
300 files_struct *fsp)
302 acl_type_t aixjfs2_type;
303 aixjfs2_type.u64 = ACL_AIXC;
305 return aixjfs2_get_posix_acl(fsp->fsp_name->base_name, aixjfs2_type);
309 * Test whether we have that aclType support on the given path
311 static int aixjfs2_query_acl_support(
312 char *filepath,
313 uint64_t aclType,
314 acl_type_t *pacl_type_info
317 acl_types_list_t acl_type_list;
318 size_t acl_type_list_len = sizeof(acl_types_list_t);
319 uint32_t i;
321 memset(&acl_type_list, 0, sizeof(acl_type_list));
323 if (aclx_gettypes(filepath, &acl_type_list, &acl_type_list_len)) {
324 DEBUG(2, ("aclx_gettypes failed with error %s for %s\n",
325 strerror(errno), filepath));
326 return -1;
329 for(i=0; i<acl_type_list.num_entries; i++) {
330 if (acl_type_list.entries[i].u64==aclType) {
331 memcpy(pacl_type_info, acl_type_list.entries + i, sizeof(acl_type_t));
332 DEBUG(10, ("found %s ACL support for %s\n",
333 pacl_type_info->acl_type, filepath));
334 return 0;
338 return 1; /* haven't found that ACL type. */
341 static bool aixjfs2_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
343 SMB4ACE_T *smbace;
344 TALLOC_CTX *mem_ctx;
345 nfs4_acl_int_t *jfs2acl;
346 int32_t entryLen;
347 uint32 aclLen, naces;
348 int rc;
349 acl_type_t acltype;
351 DEBUG(10, ("jfs2_process_smbacl invoked on %s\n", fsp_str_dbg(fsp)));
353 /* no need to be freed which is alloced with mem_ctx */
354 mem_ctx = talloc_tos();
356 entryLen = sizeof(nfs4_ace_int_t);
357 if (entryLen & 0x03)
358 entryLen = entryLen + 4 - (entryLen%4);
360 naces = smb_get_naces(smbacl);
361 aclLen = ACL_V4_SIZ + naces * entryLen;
362 jfs2acl = (nfs4_acl_int_t *)TALLOC_SIZE(mem_ctx, aclLen);
363 if (jfs2acl==NULL) {
364 DEBUG(0, ("TALLOC_SIZE failed\n"));
365 errno = ENOMEM;
366 return False;
369 jfs2acl->aclLength = ACL_V4_SIZ;
370 jfs2acl->aclVersion = NFS4_ACL_INT_STRUCT_VERSION;
371 jfs2acl->aclEntryN = 0;
373 for(smbace = smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace))
375 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
376 nfs4_ace_int_t *jfs2_ace = (nfs4_ace_int_t *)(((char *)jfs2acl) + jfs2acl->aclLength);
378 memset(jfs2_ace, 0, entryLen);
379 jfs2_ace->entryLen = entryLen; /* won't store textual "who" */
380 jfs2_ace->aceType = aceprop->aceType; /* only ACCES|DENY supported by jfs2 */
381 jfs2_ace->aceFlags = aceprop->aceFlags;
382 jfs2_ace->aceMask = aceprop->aceMask;
383 jfs2_ace->flags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_ID_SPECIAL : 0;
385 /* don't care it's real content is only 16 or 32 bit */
386 jfs2_ace->aceWho.id = aceprop->who.id;
388 /* iterate to the next jfs2 ace */
389 jfs2acl->aclLength += jfs2_ace->entryLen;
390 jfs2acl->aclEntryN++;
392 SMB_ASSERT(jfs2acl->aclEntryN==naces);
394 /* Don't query it (again) */
395 memset(&acltype, 0, sizeof(acl_type_t));
396 acltype.u64 = ACL_NFS4;
398 /* won't set S_ISUID - the only one JFS2/NFS4 accepts */
399 rc = aclx_put(
400 fsp->fsp_name->base_name,
401 SET_ACL, /* set only the ACL, not mode bits */
402 acltype, /* not a pointer !!! */
403 jfs2acl,
404 jfs2acl->aclLength,
405 0 /* don't set here mode bits */
407 if (rc) {
408 DEBUG(8, ("aclx_put failed with %s\n", strerror(errno)));
409 return False;
412 DEBUG(10, ("jfs2_process_smbacl succeeded.\n"));
413 return True;
416 static NTSTATUS aixjfs2_set_nt_acl_common(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
418 acl_type_t acl_type_info;
419 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
420 int rc;
422 rc = aixjfs2_query_acl_support(
423 fsp->fsp_name,
424 ACL_NFS4,
425 &acl_type_info);
427 if (rc==0)
429 result = smb_set_nt_acl_nfs4(
430 fsp, security_info_sent, psd,
431 aixjfs2_process_smbacl);
432 } else if (rc==1) { /* assume POSIX ACL - by default... */
433 result = set_nt_acl(fsp, security_info_sent, psd);
434 } else
435 result = map_nt_error_from_unix(errno); /* query failed */
437 return result;
440 NTSTATUS aixjfs2_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
442 return aixjfs2_set_nt_acl_common(fsp, security_info_sent, psd);
445 int aixjfs2_sys_acl_set_file(vfs_handle_struct *handle,
446 const char *name,
447 SMB_ACL_TYPE_T type,
448 SMB_ACL_T theacl)
450 struct acl *acl_aixc;
451 acl_type_t acl_type_info;
452 int rc;
454 DEBUG(10, ("aixjfs2_sys_acl_set_file invoked for %s", name));
456 rc = aixjfs2_query_acl_support((char *)name, ACL_AIXC, &acl_type_info);
457 if (rc) {
458 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
459 return -1;
462 acl_aixc = aixacl_smb_to_aixacl(type, theacl);
463 if (!acl_aixc)
464 return -1;
466 rc = aclx_put(
467 (char *)name,
468 SET_ACL, /* set only the ACL, not mode bits */
469 acl_type_info,
470 acl_aixc,
471 acl_aixc->acl_len,
474 if (rc) {
475 DEBUG(2, ("aclx_put failed with %s for %s\n",
476 strerror(errno), name));
477 return -1;
480 return 0;
483 int aixjfs2_sys_acl_set_fd(vfs_handle_struct *handle,
484 files_struct *fsp,
485 SMB_ACL_T theacl)
487 struct acl *acl_aixc;
488 acl_type_t acl_type_info;
489 int rc;
491 DEBUG(10, ("aixjfs2_sys_acl_set_fd invoked for %s", fsp_str_dbg(fsp)));
493 rc = aixjfs2_query_acl_support(fsp->fsp_name->base_name, ACL_AIXC,
494 &acl_type_info);
495 if (rc) {
496 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
497 return -1;
500 acl_aixc = aixacl_smb_to_aixacl(SMB_ACL_TYPE_ACCESS, theacl);
501 if (!acl_aixc)
502 return -1;
504 rc = aclx_fput(
505 fsp->fh->fd,
506 SET_ACL, /* set only the ACL, not mode bits */
507 acl_type_info,
508 acl_aixc,
509 acl_aixc->acl_len,
512 if (rc) {
513 DEBUG(2, ("aclx_fput failed with %s for %s\n",
514 strerror(errno), fsp_str_dbg(fsp)));
515 return -1;
518 return 0;
521 int aixjfs2_sys_acl_delete_def_file(vfs_handle_struct *handle,
522 const char *path)
524 /* Not available under AIXC ACL */
525 /* Don't report here any error otherwise */
526 /* upper layer will break the normal execution */
527 return 0;
530 static struct vfs_fn_pointers vfs_aixacl2_fns = {
531 .fget_nt_acl_fn = aixjfs2_fget_nt_acl,
532 .get_nt_acl_fn = aixjfs2_get_nt_acl,
533 .fset_nt_acl_fn = aixjfs2_fset_nt_acl,
534 .sys_acl_get_file_fn = aixjfs2_sys_acl_get_file,
535 .sys_acl_get_fd_fn = aixjfs2_sys_acl_get_fd,
536 .sys_acl_blob_get_file_fn = aixjfs2_sys_acl_blob_get_file,
537 .sys_acl_blob_get_fd_fn = aixjfs2_sys_acl_blob_get_fd,
538 .sys_acl_set_file_fn = aixjfs2_sys_acl_set_file,
539 .sys_acl_set_fd_fn = aixjfs2_sys_acl_set_fd,
540 .sys_acl_delete_def_file_fn = aixjfs2_sys_acl_delete_def_file
543 NTSTATUS vfs_aixacl2_init(void);
544 NTSTATUS vfs_aixacl2_init(void)
546 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, AIXACL2_MODULE_NAME,
547 &vfs_aixacl2_fns);