s3-pysmbd: fix DEBUG
[Samba.git] / source3 / modules / vfs_aixacl2.c
blob3f13a6fa87f35678da837e15ef494b2e70f8acc2
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"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_VFS
28 #define AIXACL2_MODULE_NAME "aixacl2"
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);
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;
38 static int32_t aixacl2_getlen(AIXJFS2_ACL_T *acl, acl_type_t *type)
40 int32_t len;
42 if(type->u64 == ACL_NFS4) {
43 len = acl->jfs2_acl[0].aclLength;
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;
53 DEBUG(10,("aixacl2_getlen:%d\n",len));
54 return len;
57 static AIXJFS2_ACL_T *aixjfs2_getacl_alloc(const char *fname, acl_type_t *type)
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;
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;
73 if(type->u64 == ACL_ANY) {
74 ctl_flag = ctl_flag | GET_ACLINFO_ONLY;
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));
82 acl = (AIXJFS2_ACL_T *)TALLOC_SIZE(mem_ctx, len);
83 if (acl == NULL) {
84 errno = ENOMEM;
85 return NULL;
88 ret = aclx_get((char *)fname, ctl_flag, type, acl, &len, &mode);
90 if (ret != 0) {
91 DEBUG(8, ("aclx_get failed with %s\n", strerror(errno)));
92 return NULL;
95 return acl;
98 static bool aixjfs2_get_nfs4_acl(const char *name,
99 SMB4ACL_T **ppacl, bool *pretryPosix)
101 int32_t i;
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;
108 DEBUG(10,("jfs2 get_nt_acl invoked for %s\n", name));
110 memset(&type, 0, sizeof(acl_type_t));
111 type.u64 = ACL_NFS4;
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;
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));
126 *ppacl = smb_create_smb4acl();
127 if (*ppacl==NULL)
128 return False;
130 jfs2_ace = &jfs2_acl->aclEntry[0];
131 for (i=0; i<jfs2_acl->aclEntryN; i++) {
132 SMB_ACE4PROP_T aceprop;
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));
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;
143 /* don't care it's real content is only 16 or 32 bit */
144 aceprop.who.id = jfs2_ace->aceWho.id;
146 if (smb_add_ace4(*ppacl, &aceprop)==NULL)
147 return False;
149 /* iterate to the next jfs2 ace */
150 jfs2_ace = (nfs4_ace_int_t *)(((char *)jfs2_ace) + jfs2_ace->entryLen);
153 DEBUG(10,("jfs2 get_nt_acl finished successfully\n"));
155 return True;
158 static NTSTATUS aixjfs2_fget_nt_acl(vfs_handle_struct *handle,
159 files_struct *fsp, uint32 security_info,
160 struct security_descriptor **ppdesc)
162 SMB4ACL_T *pacl = NULL;
163 bool result;
164 bool retryPosix = False;
166 *ppdesc = NULL;
167 result = aixjfs2_get_nfs4_acl(fsp->fsp_name->base_name, &pacl,
168 &retryPosix);
169 if (retryPosix)
171 DEBUG(10, ("retrying with posix acl...\n"));
172 return posix_fget_nt_acl(fsp, security_info, ppdesc);
174 if (result==False)
175 return NT_STATUS_ACCESS_DENIED;
177 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
180 static NTSTATUS aixjfs2_get_nt_acl(vfs_handle_struct *handle,
181 const char *name,
182 uint32 security_info, struct security_descriptor **ppdesc)
184 SMB4ACL_T *pacl = NULL;
185 bool result;
186 bool retryPosix = False;
188 *ppdesc = NULL;
189 result = aixjfs2_get_nfs4_acl(name, &pacl, &retryPosix);
190 if (retryPosix)
192 DEBUG(10, ("retrying with posix acl...\n"));
193 return posix_get_nt_acl(handle->conn, name, security_info,
194 ppdesc);
196 if (result==False)
197 return NT_STATUS_ACCESS_DENIED;
199 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc,
200 pacl);
203 static SMB_ACL_T aixjfs2_get_posix_acl(const char *path, acl_type_t type)
205 aixc_acl_t *pacl;
206 AIXJFS2_ACL_T *acl;
207 SMB_ACL_T result = NULL;
208 int ret;
210 acl = aixjfs2_getacl_alloc(path, &type);
212 if (acl == NULL) {
213 DEBUG(10, ("aixjfs2_getacl failed for %s with %s\n",
214 path, strerror(errno)));
215 if (errno == 0) {
216 errno = EINVAL;
218 goto done;
221 pacl = &acl->aixc_acl[0];
222 DEBUG(10, ("len: %d, mode: %d\n",
223 pacl->acl_len, pacl->acl_mode));
225 result = aixacl_to_smbacl(pacl);
226 if (result == NULL) {
227 goto done;
230 done:
231 if (errno != 0) {
232 SAFE_FREE(result);
234 return result;
237 SMB_ACL_T aixjfs2_sys_acl_get_file(vfs_handle_struct *handle,
238 const char *path_p,
239 SMB_ACL_TYPE_T type)
241 acl_type_t aixjfs2_type;
243 switch(type) {
244 case SMB_ACL_TYPE_ACCESS:
245 aixjfs2_type.u64 = ACL_AIXC;
246 break;
247 case SMB_ACL_TYPE_DEFAULT:
248 DEBUG(0, ("Got AIX JFS2 unsupported type: %d\n", type));
249 return NULL;
250 default:
251 DEBUG(0, ("Got invalid type: %d\n", type));
252 smb_panic("exiting");
255 return aixjfs2_get_posix_acl(path_p, aixjfs2_type);
258 SMB_ACL_T aixjfs2_sys_acl_get_fd(vfs_handle_struct *handle,
259 files_struct *fsp)
261 acl_type_t aixjfs2_type;
262 aixjfs2_type.u64 = ACL_AIXC;
264 return aixjfs2_get_posix_acl(fsp->fsp_name->base_name, aixjfs2_type);
268 * Test whether we have that aclType support on the given path
270 static int aixjfs2_query_acl_support(
271 char *filepath,
272 uint64_t aclType,
273 acl_type_t *pacl_type_info
276 acl_types_list_t acl_type_list;
277 size_t acl_type_list_len = sizeof(acl_types_list_t);
278 uint32_t i;
280 memset(&acl_type_list, 0, sizeof(acl_type_list));
282 if (aclx_gettypes(filepath, &acl_type_list, &acl_type_list_len)) {
283 DEBUG(2, ("aclx_gettypes failed with error %s for %s\n",
284 strerror(errno), filepath));
285 return -1;
288 for(i=0; i<acl_type_list.num_entries; i++) {
289 if (acl_type_list.entries[i].u64==aclType) {
290 memcpy(pacl_type_info, acl_type_list.entries + i, sizeof(acl_type_t));
291 DEBUG(10, ("found %s ACL support for %s\n",
292 pacl_type_info->acl_type, filepath));
293 return 0;
297 return 1; /* haven't found that ACL type. */
300 static bool aixjfs2_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
302 SMB4ACE_T *smbace;
303 TALLOC_CTX *mem_ctx;
304 nfs4_acl_int_t *jfs2acl;
305 int32_t entryLen;
306 uint32 aclLen, naces;
307 int rc;
308 acl_type_t acltype;
310 DEBUG(10, ("jfs2_process_smbacl invoked on %s\n", fsp_str_dbg(fsp)));
312 /* no need to be freed which is alloced with mem_ctx */
313 mem_ctx = talloc_tos();
315 entryLen = sizeof(nfs4_ace_int_t);
316 if (entryLen & 0x03)
317 entryLen = entryLen + 4 - (entryLen%4);
319 naces = smb_get_naces(smbacl);
320 aclLen = ACL_V4_SIZ + naces * entryLen;
321 jfs2acl = (nfs4_acl_int_t *)TALLOC_SIZE(mem_ctx, aclLen);
322 if (jfs2acl==NULL) {
323 DEBUG(0, ("TALLOC_SIZE failed\n"));
324 errno = ENOMEM;
325 return False;
328 jfs2acl->aclLength = ACL_V4_SIZ;
329 jfs2acl->aclVersion = NFS4_ACL_INT_STRUCT_VERSION;
330 jfs2acl->aclEntryN = 0;
332 for(smbace = smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace))
334 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
335 nfs4_ace_int_t *jfs2_ace = (nfs4_ace_int_t *)(((char *)jfs2acl) + jfs2acl->aclLength);
337 memset(jfs2_ace, 0, entryLen);
338 jfs2_ace->entryLen = entryLen; /* won't store textual "who" */
339 jfs2_ace->aceType = aceprop->aceType; /* only ACCES|DENY supported by jfs2 */
340 jfs2_ace->aceFlags = aceprop->aceFlags;
341 jfs2_ace->aceMask = aceprop->aceMask;
342 jfs2_ace->flags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_ID_SPECIAL : 0;
344 /* don't care it's real content is only 16 or 32 bit */
345 jfs2_ace->aceWho.id = aceprop->who.id;
347 /* iterate to the next jfs2 ace */
348 jfs2acl->aclLength += jfs2_ace->entryLen;
349 jfs2acl->aclEntryN++;
351 SMB_ASSERT(jfs2acl->aclEntryN==naces);
353 /* Don't query it (again) */
354 memset(&acltype, 0, sizeof(acl_type_t));
355 acltype.u64 = ACL_NFS4;
357 /* won't set S_ISUID - the only one JFS2/NFS4 accepts */
358 rc = aclx_put(
359 fsp->fsp_name->base_name,
360 SET_ACL, /* set only the ACL, not mode bits */
361 acltype, /* not a pointer !!! */
362 jfs2acl,
363 jfs2acl->aclLength,
364 0 /* don't set here mode bits */
366 if (rc) {
367 DEBUG(8, ("aclx_put failed with %s\n", strerror(errno)));
368 return False;
371 DEBUG(10, ("jfs2_process_smbacl succeeded.\n"));
372 return True;
375 static NTSTATUS aixjfs2_set_nt_acl_common(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
377 acl_type_t acl_type_info;
378 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
379 int rc;
381 rc = aixjfs2_query_acl_support(
382 fsp->fsp_name,
383 ACL_NFS4,
384 &acl_type_info);
386 if (rc==0)
388 result = smb_set_nt_acl_nfs4(
389 fsp, security_info_sent, psd,
390 aixjfs2_process_smbacl);
391 } else if (rc==1) { /* assume POSIX ACL - by default... */
392 result = set_nt_acl(fsp, security_info_sent, psd);
393 } else
394 result = map_nt_error_from_unix(errno); /* query failed */
396 return result;
399 NTSTATUS aixjfs2_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
401 return aixjfs2_set_nt_acl_common(fsp, security_info_sent, psd);
404 int aixjfs2_sys_acl_set_file(vfs_handle_struct *handle,
405 const char *name,
406 SMB_ACL_TYPE_T type,
407 SMB_ACL_T theacl)
409 struct acl *acl_aixc;
410 acl_type_t acl_type_info;
411 int rc;
413 DEBUG(10, ("aixjfs2_sys_acl_set_file invoked for %s", name));
415 rc = aixjfs2_query_acl_support((char *)name, ACL_AIXC, &acl_type_info);
416 if (rc) {
417 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
418 return -1;
421 acl_aixc = aixacl_smb_to_aixacl(type, theacl);
422 if (!acl_aixc)
423 return -1;
425 rc = aclx_put(
426 (char *)name,
427 SET_ACL, /* set only the ACL, not mode bits */
428 acl_type_info,
429 acl_aixc,
430 acl_aixc->acl_len,
433 if (rc) {
434 DEBUG(2, ("aclx_put failed with %s for %s\n",
435 strerror(errno), name));
436 return -1;
439 return 0;
442 int aixjfs2_sys_acl_set_fd(vfs_handle_struct *handle,
443 files_struct *fsp,
444 SMB_ACL_T theacl)
446 struct acl *acl_aixc;
447 acl_type_t acl_type_info;
448 int rc;
450 DEBUG(10, ("aixjfs2_sys_acl_set_fd invoked for %s", fsp_str_dbg(fsp)));
452 rc = aixjfs2_query_acl_support(fsp->fsp_name->base_name, ACL_AIXC,
453 &acl_type_info);
454 if (rc) {
455 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
456 return -1;
459 acl_aixc = aixacl_smb_to_aixacl(SMB_ACL_TYPE_ACCESS, theacl);
460 if (!acl_aixc)
461 return -1;
463 rc = aclx_fput(
464 fsp->fh->fd,
465 SET_ACL, /* set only the ACL, not mode bits */
466 acl_type_info,
467 acl_aixc,
468 acl_aixc->acl_len,
471 if (rc) {
472 DEBUG(2, ("aclx_fput failed with %s for %s\n",
473 strerror(errno), fsp_str_dbg(fsp)));
474 return -1;
477 return 0;
480 int aixjfs2_sys_acl_delete_def_file(vfs_handle_struct *handle,
481 const char *path)
483 /* Not available under AIXC ACL */
484 /* Don't report here any error otherwise */
485 /* upper layer will break the normal execution */
486 return 0;
489 static struct vfs_fn_pointers vfs_aixacl2_fns = {
490 .fget_nt_acl_fn = aixjfs2_fget_nt_acl,
491 .get_nt_acl_fn = aixjfs2_get_nt_acl,
492 .fset_nt_acl_fn = aixjfs2_fset_nt_acl,
493 .sys_acl_get_file_fn = aixjfs2_sys_acl_get_file,
494 .sys_acl_get_fd_fn = aixjfs2_sys_acl_get_fd,
495 .sys_acl_set_file_fn = aixjfs2_sys_acl_set_file,
496 .sys_acl_set_fd_fn = aixjfs2_sys_acl_set_fd,
497 .sys_acl_delete_def_file_fn = aixjfs2_sys_acl_delete_def_file
500 NTSTATUS vfs_aixacl2_init(void);
501 NTSTATUS vfs_aixacl2_init(void)
503 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, AIXACL2_MODULE_NAME,
504 &vfs_aixacl2_fns);