Fix some merge omissions for GPFS
[kai/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 "nfs4_acls.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
25
26 #define AIXACL2_MODULE_NAME "aixacl2"
27
28 extern int try_chown(connection_struct *conn, const char *fname, uid_t uid, gid_t gid);
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         SEC_DESC **ppdesc)
161 {
162         SMB4ACL_T *pacl = NULL;
163         bool    result;
164         bool    retryPosix = False;
165
166         *ppdesc = NULL;
167         result = aixjfs2_get_nfs4_acl(fsp->fsp_name, &pacl, &retryPosix);
168         if (retryPosix)
169         {
170                 DEBUG(10, ("retrying with posix acl...\n"));
171                 return posix_fget_nt_acl(fsp, security_info, ppdesc);
172         }
173         if (result==False)
174                 return NT_STATUS_ACCESS_DENIED;
175
176         return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
177 }
178
179 static NTSTATUS aixjfs2_get_nt_acl(vfs_handle_struct *handle,
180         const char *name,
181         uint32 security_info, SEC_DESC **ppdesc)
182 {
183         SMB4ACL_T *pacl = NULL;
184         bool    result;
185         bool    retryPosix = False;
186
187         *ppdesc = NULL;
188         result = aixjfs2_get_nfs4_acl(name, &pacl, &retryPosix);
189         if (retryPosix)
190         {
191                 DEBUG(10, ("retrying with posix acl...\n"));
192                 return posix_get_nt_acl(handle->conn, name, security_info,
193                                         ppdesc);
194         }
195         if (result==False)
196                 return NT_STATUS_ACCESS_DENIED;
197
198         return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc,
199                                    pacl);
200 }
201
202 static SMB_ACL_T aixjfs2_get_posix_acl(const char *path, acl_type_t type)
203 {
204         aixc_acl_t *pacl;
205         AIXJFS2_ACL_T *acl;
206         SMB_ACL_T result = NULL;
207         int ret;
208
209         acl = aixjfs2_getacl_alloc(path, &type);
210
211         if (acl == NULL) {
212                 DEBUG(10, ("aixjfs2_getacl failed for %s with %s\n",
213                            path, strerror(errno)));
214                 if (errno == 0) {
215                         errno = EINVAL;
216                 }
217                 goto done;
218         }
219
220         pacl = &acl->aixc_acl[0];
221         DEBUG(10, ("len: %d, mode: %d\n",
222                    pacl->acl_len, pacl->acl_mode));
223
224         result = aixacl_to_smbacl(pacl);
225         if (result == NULL) {
226                 goto done;
227         }
228
229  done:
230         if (errno != 0) {
231                 SAFE_FREE(result);
232         }
233         return result;
234 }
235
236 SMB_ACL_T aixjfs2_sys_acl_get_file(vfs_handle_struct *handle,
237                                     const char *path_p,
238                                     SMB_ACL_TYPE_T type)
239 {
240         acl_type_t aixjfs2_type;
241
242         switch(type) {
243         case SMB_ACL_TYPE_ACCESS:
244                 aixjfs2_type.u64 = ACL_AIXC;
245                 break;
246         case SMB_ACL_TYPE_DEFAULT:
247                 DEBUG(0, ("Got AIX JFS2 unsupported type: %d\n", type));
248                 return NULL;
249         default:
250                 DEBUG(0, ("Got invalid type: %d\n", type));
251                 smb_panic("exiting");
252         }
253
254         return aixjfs2_get_posix_acl(path_p, aixjfs2_type);
255 }
256
257 SMB_ACL_T aixjfs2_sys_acl_get_fd(vfs_handle_struct *handle,
258                                   files_struct *fsp)
259 {
260         acl_type_t aixjfs2_type;
261         aixjfs2_type.u64 = ACL_AIXC;
262
263         return aixjfs2_get_posix_acl(fsp->fsp_name, aixjfs2_type);
264 }
265
266 /*
267  * Test whether we have that aclType support on the given path
268  */
269 static int aixjfs2_query_acl_support(
270         char *filepath,
271         uint64_t aclType,
272         acl_type_t *pacl_type_info
273 )
274 {
275         acl_types_list_t        acl_type_list;
276         size_t  acl_type_list_len = sizeof(acl_types_list_t);
277         uint32_t        i;
278
279         memset(&acl_type_list, 0, sizeof(acl_type_list));
280
281         if (aclx_gettypes(filepath, &acl_type_list, &acl_type_list_len)) {
282                 DEBUG(2, ("aclx_gettypes failed with error %s for %s\n",
283                         strerror(errno), filepath));
284                 return -1;
285         }
286
287         for(i=0; i<acl_type_list.num_entries; i++) {
288                 if (acl_type_list.entries[i].u64==aclType) {
289                         memcpy(pacl_type_info, acl_type_list.entries + i, sizeof(acl_type_t));
290                         DEBUG(10, ("found %s ACL support for %s\n",
291                                 pacl_type_info->acl_type, filepath));
292                         return 0;
293                 }
294         }
295
296         return 1; /* haven't found that ACL type. */
297 }
298
299 static bool aixjfs2_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
300 {
301         SMB4ACE_T       *smbace;
302         TALLOC_CTX      *mem_ctx;
303         nfs4_acl_int_t  *jfs2acl;
304         int32_t entryLen;
305         uint32  aclLen, naces;
306         int     rc;
307         acl_type_t      acltype;
308
309         DEBUG(10, ("jfs2_process_smbacl invoked on %s\n", fsp->fsp_name));
310
311         /* no need to be freed which is alloced with mem_ctx */
312         mem_ctx = talloc_tos();
313
314         entryLen = sizeof(nfs4_ace_int_t);
315         if (entryLen & 0x03)
316                 entryLen = entryLen + 4 - (entryLen%4);
317
318         naces = smb_get_naces(smbacl);
319         aclLen = ACL_V4_SIZ + naces * entryLen;
320         jfs2acl = (nfs4_acl_int_t *)TALLOC_SIZE(mem_ctx, aclLen);
321         if (jfs2acl==NULL) {
322                 DEBUG(0, ("TALLOC_SIZE failed\n"));
323                 errno = ENOMEM;
324                 return False;
325         }
326
327         jfs2acl->aclLength = ACL_V4_SIZ;
328         jfs2acl->aclVersion = NFS4_ACL_INT_STRUCT_VERSION;
329         jfs2acl->aclEntryN = 0;
330
331         for(smbace = smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace))
332         {
333                 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
334                 nfs4_ace_int_t *jfs2_ace = (nfs4_ace_int_t *)(((char *)jfs2acl) + jfs2acl->aclLength);
335
336                 memset(jfs2_ace, 0, entryLen);
337                 jfs2_ace->entryLen = entryLen; /* won't store textual "who" */
338                 jfs2_ace->aceType = aceprop->aceType; /* only ACCES|DENY supported by jfs2 */
339                 jfs2_ace->aceFlags = aceprop->aceFlags;
340                 jfs2_ace->aceMask = aceprop->aceMask;
341                 jfs2_ace->flags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_ID_SPECIAL : 0;
342
343                 /* don't care it's real content is only 16 or 32 bit */
344                 jfs2_ace->aceWho.id = aceprop->who.id;
345
346                 /* iterate to the next jfs2 ace */
347                 jfs2acl->aclLength += jfs2_ace->entryLen;
348                 jfs2acl->aclEntryN++;
349         }
350         SMB_ASSERT(jfs2acl->aclEntryN==naces);
351
352         /* Don't query it (again) */
353         memset(&acltype, 0, sizeof(acl_type_t));
354         acltype.u64 = ACL_NFS4;
355
356         /* won't set S_ISUID - the only one JFS2/NFS4 accepts */
357         rc = aclx_put(
358                 fsp->fsp_name,
359                 SET_ACL, /* set only the ACL, not mode bits */
360                 acltype, /* not a pointer !!! */
361                 jfs2acl,
362                 jfs2acl->aclLength,
363                 0 /* don't set here mode bits */
364         );
365         if (rc) {
366                 DEBUG(8, ("aclx_put failed with %s\n", strerror(errno)));
367                 return False;
368         }
369
370         DEBUG(10, ("jfs2_process_smbacl succeeded.\n"));
371         return True;
372 }
373
374 static NTSTATUS aixjfs2_set_nt_acl_common(files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
375 {
376         acl_type_t      acl_type_info;
377         NTSTATUS        result = NT_STATUS_ACCESS_DENIED;
378         int     rc;
379
380         rc = aixjfs2_query_acl_support(
381                 fsp->fsp_name,
382                 ACL_NFS4,
383                 &acl_type_info);
384
385         if (rc==0)
386         {
387                 result = smb_set_nt_acl_nfs4(
388                         fsp, security_info_sent, psd,
389                         aixjfs2_process_smbacl);
390         } else if (rc==1) { /* assume POSIX ACL - by default... */
391                 result = set_nt_acl(fsp, security_info_sent, psd);
392         } else
393                 result = map_nt_error_from_unix(errno); /* query failed */
394         
395         return result;
396 }
397
398 NTSTATUS aixjfs2_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
399 {
400         return aixjfs2_set_nt_acl_common(fsp, security_info_sent, psd);
401 }
402
403 int aixjfs2_sys_acl_set_file(vfs_handle_struct *handle,
404                               const char *name,
405                               SMB_ACL_TYPE_T type,
406                               SMB_ACL_T theacl)
407 {
408         struct acl      *acl_aixc;
409         acl_type_t      acl_type_info;
410         int     rc;
411
412         DEBUG(10, ("aixjfs2_sys_acl_set_file invoked for %s", name));
413
414         rc = aixjfs2_query_acl_support((char *)name, ACL_AIXC, &acl_type_info);
415         if (rc) {
416                 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
417                 return -1;
418         }
419
420         acl_aixc = aixacl_smb_to_aixacl(type, theacl);
421         if (!acl_aixc)
422                 return -1;
423
424         rc = aclx_put(
425                 (char *)name,
426                 SET_ACL, /* set only the ACL, not mode bits */
427                 acl_type_info,
428                 acl_aixc,
429                 acl_aixc->acl_len,
430                 0
431         );
432         if (rc) {
433                 DEBUG(2, ("aclx_put failed with %s for %s\n",
434                         strerror(errno), name));
435                 return -1;
436         }
437
438         return 0;
439 }
440
441 int aixjfs2_sys_acl_set_fd(vfs_handle_struct *handle,
442                             files_struct *fsp,
443                             SMB_ACL_T theacl)
444 {
445         struct acl      *acl_aixc;
446         acl_type_t      acl_type_info;
447         int     rc;
448
449         DEBUG(10, ("aixjfs2_sys_acl_set_fd invoked for %s", fsp->fsp_name));
450
451         rc = aixjfs2_query_acl_support(fsp->fsp_name, ACL_AIXC, &acl_type_info);
452         if (rc) {
453                 DEBUG(8, ("jfs2_set_nt_acl: AIXC support not found\n"));
454                 return -1;
455         }
456
457         acl_aixc = aixacl_smb_to_aixacl(SMB_ACL_TYPE_ACCESS, theacl);
458         if (!acl_aixc)
459                 return -1;
460
461         rc = aclx_fput(
462                 fsp->fh->fd,
463                 SET_ACL, /* set only the ACL, not mode bits */
464                 acl_type_info,
465                 acl_aixc,
466                 acl_aixc->acl_len,
467                 0
468         );
469         if (rc) {
470                 DEBUG(2, ("aclx_fput failed with %s for %s\n",
471                         strerror(errno), fsp->fsp_name));
472                 return -1;
473         }
474
475         return 0;
476 }
477
478 int aixjfs2_sys_acl_delete_def_file(vfs_handle_struct *handle,
479                                      const char *path)
480 {
481         /* Not available under AIXC ACL */
482         /* Don't report here any error otherwise */
483         /* upper layer will break the normal execution */
484         return 0;
485 }
486
487
488 /* VFS operations structure */
489
490 static vfs_op_tuple aixjfs2_ops[] =
491 {
492         {SMB_VFS_OP(aixjfs2_fget_nt_acl),
493         SMB_VFS_OP_FGET_NT_ACL,
494         SMB_VFS_LAYER_TRANSPARENT},
495
496         {SMB_VFS_OP(aixjfs2_get_nt_acl),
497         SMB_VFS_OP_GET_NT_ACL,
498         SMB_VFS_LAYER_TRANSPARENT},
499
500         {SMB_VFS_OP(aixjfs2_fset_nt_acl),
501         SMB_VFS_OP_FSET_NT_ACL,
502         SMB_VFS_LAYER_TRANSPARENT},
503
504         {SMB_VFS_OP(aixjfs2_sys_acl_get_file),
505         SMB_VFS_OP_SYS_ACL_GET_FILE,
506         SMB_VFS_LAYER_TRANSPARENT},
507
508         {SMB_VFS_OP(aixjfs2_sys_acl_get_fd),
509         SMB_VFS_OP_SYS_ACL_GET_FD,
510         SMB_VFS_LAYER_TRANSPARENT},
511
512         {SMB_VFS_OP(aixjfs2_sys_acl_set_file),
513         SMB_VFS_OP_SYS_ACL_SET_FILE,
514         SMB_VFS_LAYER_TRANSPARENT},
515
516         {SMB_VFS_OP(aixjfs2_sys_acl_set_fd),
517         SMB_VFS_OP_SYS_ACL_SET_FD,
518         SMB_VFS_LAYER_TRANSPARENT},
519
520         {SMB_VFS_OP(aixjfs2_sys_acl_delete_def_file),
521         SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
522         SMB_VFS_LAYER_TRANSPARENT},
523
524         {SMB_VFS_OP(NULL),
525         SMB_VFS_OP_NOOP,
526         SMB_VFS_LAYER_NOOP}
527 };
528
529 NTSTATUS vfs_aixacl2_init(void);
530 NTSTATUS vfs_aixacl2_init(void)
531 {
532         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, AIXACL2_MODULE_NAME,
533                                 aixjfs2_ops);
534 }