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