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