r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[ira/wip.git] / source3 / modules / vfs_zfsacl.c
1 /*
2  * Convert ZFS/NFSv4 acls to NT acls and vice versa.
3  *
4  * Copyright (C) Jiri Sasek, 2007
5  * based on the foobar.c module which is copyrighted by Volker Lendecke
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include "includes.h"
23 #include "nfs4_acls.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_VFS
27
28 #define ZFSACL_MODULE_NAME "zfsacl"
29
30 /* zfs_get_nt_acl()
31  * read the local file's acls and return it in NT form
32  * using the NFSv4 format conversion
33  */
34 static size_t zfs_get_nt_acl(struct files_struct *fsp, uint32 security_info,
35                              struct security_descriptor **ppdesc)
36 {
37         int naces, i;
38         ace_t *acebuf;
39         SMB4ACL_T *pacl;
40         TALLOC_CTX      *mem_ctx;
41
42         /* read the number of file aces */
43         if((naces = acl(fsp->fsp_name, ACE_GETACLCNT, 0, NULL)) == -1) {
44                 if(errno == ENOSYS) {
45                         DEBUG(9, ("acl(ACE_GETACLCNT, %s): Operation is not supported on the filesystem where the file reside"));
46                 } else {
47                         DEBUG(9, ("acl(ACE_GETACLCNT, %s): %s ", fsp->fsp_name,
48                                         strerror(errno)));
49                 }
50                 return 0;
51         }
52         /* allocate the field of ZFS aces */
53         mem_ctx = main_loop_talloc_get();
54         acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
55         if(acebuf == NULL) {
56                 errno = ENOMEM;
57                 return 0;
58         }
59         /* read the aces into the field */
60         if(acl(fsp->fsp_name, ACE_GETACL, naces, acebuf) < 0) {
61                 DEBUG(9, ("acl(ACE_GETACL, %s): %s ", fsp->fsp_name,
62                                 strerror(errno)));
63                 return 0;
64         }
65         /* create SMB4ACL data */
66         if((pacl = smb_create_smb4acl()) == NULL) return 0;
67         for(i=0; i<naces; i++) {
68                 SMB_ACE4PROP_T aceprop;
69
70                 aceprop.aceType  = (uint32) acebuf[i].a_type;
71                 aceprop.aceFlags = (uint32) acebuf[i].a_flags;
72                 aceprop.aceMask  = (uint32) acebuf[i].a_access_mask;
73                 aceprop.who.id   = (uint32) acebuf[i].a_who;
74                 aceprop.flags    = 0;
75                 if(smb_add_ace4(pacl, &aceprop) == NULL) return 0;
76         }
77
78         return smb_get_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
79 }
80
81 /* call-back function processing the NT acl -> ZFS acl using NFSv4 conv. */
82 static BOOL zfs_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
83 {
84         int naces = smb_get_naces(smbacl), i;
85         ace_t *acebuf;
86         SMB4ACE_T *smbace;
87         TALLOC_CTX      *mem_ctx;
88
89         /* allocate the field of ZFS aces */
90         mem_ctx = main_loop_talloc_get();
91         acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
92         if(acebuf == NULL) {
93                 errno = ENOMEM;
94                 return False;
95         }
96         /* handle all aces */
97         for(smbace = smb_first_ace4(smbacl), i = 0;
98                         smbace!=NULL;
99                         smbace = smb_next_ace4(smbace), i++) {
100                 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
101
102                 acebuf[i].a_type        = aceprop->aceType;
103                 acebuf[i].a_flags       = aceprop->aceFlags;
104                 acebuf[i].a_access_mask = aceprop->aceMask;
105                 acebuf[i].a_who         = aceprop->who.id;
106         }
107         SMB_ASSERT(i == naces);
108
109         /* store acl */
110         if(acl(fsp->fsp_name, ACE_SETACL, naces, acebuf)) {
111                 if(errno == ENOSYS) {
112                         DEBUG(9, ("acl(ACE_SETACL, %s): Operation is not supported on the filesystem where the file reside"));
113                 } else {
114                         DEBUG(9, ("acl(ACE_SETACL, %s): %s ", fsp->fsp_name,
115                                         strerror(errno)));
116                 }
117                 return 0;
118         }
119
120         return True;
121 }
122
123 /* zfs_set_nt_acl()
124  * set the local file's acls obtaining it in NT form
125  * using the NFSv4 format conversion
126  */
127 static NTSTATUS zfs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
128                            uint32 security_info_sent,
129                            struct security_descriptor *psd)
130 {
131         return smb_set_nt_acl_nfs4(fsp, security_info_sent, psd,
132                         zfs_process_smbacl);
133 }
134
135 static size_t zfsacl_fget_nt_acl(struct vfs_handle_struct *handle,
136                                  struct files_struct *fsp,
137                                  int fd,  uint32 security_info,
138                                  struct security_descriptor **ppdesc)
139 {
140         return zfs_get_nt_acl(fsp, security_info, ppdesc);
141 }
142
143 static size_t zfsacl_get_nt_acl(struct vfs_handle_struct *handle,
144                                 struct files_struct *fsp,
145                                 const char *name,  uint32 security_info,
146                                 struct security_descriptor **ppdesc)
147 {
148         return zfs_get_nt_acl(fsp, security_info, ppdesc);
149 }
150
151 static NTSTATUS zfsacl_fset_nt_acl(vfs_handle_struct *handle,
152                          files_struct *fsp,
153                          int fd, uint32 security_info_sent,
154                          SEC_DESC *psd)
155 {
156         return zfs_set_nt_acl(handle, fsp, security_info_sent, psd);
157 }
158
159 static NTSTATUS zfsacl_set_nt_acl(vfs_handle_struct *handle,
160                        files_struct *fsp,
161                        const char *name, uint32 security_info_sent,
162                        SEC_DESC *psd)
163 {
164         return zfs_set_nt_acl(handle, fsp, security_info_sent, psd);
165 }
166
167 /* VFS operations structure */
168
169 static vfs_op_tuple zfsacl_ops[] = {    
170         {SMB_VFS_OP(zfsacl_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
171          SMB_VFS_LAYER_OPAQUE},
172         {SMB_VFS_OP(zfsacl_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
173          SMB_VFS_LAYER_OPAQUE},
174         {SMB_VFS_OP(zfsacl_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
175          SMB_VFS_LAYER_OPAQUE},
176         {SMB_VFS_OP(zfsacl_set_nt_acl), SMB_VFS_OP_SET_NT_ACL,
177          SMB_VFS_LAYER_OPAQUE},
178         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
179 };
180
181 NTSTATUS vfs_zfsacl_init(void);
182 NTSTATUS vfs_zfsacl_init(void)
183 {
184         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "zfsacl",
185                                 zfsacl_ops);
186 }