First (incomplete) cut of this module. Based on Volker's original work.
[samba.git] / source3 / modules / vfs_acl_xattr.c
1 /*
2  * Store Windows ACLs in xattrs.
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  * Copyright (C) Jeremy Allison, 2008
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 #include "includes.h"
22 #include "librpc/gen_ndr/xattr.h"
23 #include "librpc/gen_ndr/ndr_xattr.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_VFS
27
28 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
29                                 const struct timespec cts,
30                                 uint32 security_info,
31                                 struct security_descriptor **ppdesc)
32 {
33         TALLOC_CTX *ctx = talloc_tos();
34         struct xattr_NTACL xacl;
35         enum ndr_err_code ndr_err;
36         size_t sd_size;
37         struct timespec ts;
38
39         ndr_err = ndr_pull_struct_blob(pblob, ctx, &xacl,
40                         (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
41
42         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
43                 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
44                         ndr_errstr(ndr_err)));
45                 return ndr_map_error2ntstatus(ndr_err);;
46         }
47
48         if (xacl.version != 2) {
49                 return NT_STATUS_REVISION_MISMATCH;
50         }
51
52         /*
53          * Check that the ctime timestamp is ealier
54          * than the stored timestamp.
55          */
56
57         ts = nt_time_to_unix_timespec(&xacl.info.sd_ts->last_changed);
58
59         if (timespec_compare(&cts, &ts) > 0) {
60                 DEBUG(5, ("parse_acl_blob: stored ACL out of date.\n"));
61                 return NT_STATUS_EA_CORRUPT_ERROR;
62         }
63
64         *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
65                         (security_info & OWNER_SECURITY_INFORMATION)
66                         ? xacl.info.sd_ts->sd->owner_sid : NULL,
67                         (security_info & GROUP_SECURITY_INFORMATION)
68                         ? xacl.info.sd_ts->sd->group_sid : NULL,
69                         (security_info & SACL_SECURITY_INFORMATION)
70                         ? xacl.info.sd_ts->sd->sacl : NULL,
71                         (security_info & DACL_SECURITY_INFORMATION)
72                         ? xacl.info.sd_ts->sd->dacl : NULL,
73                         &sd_size);
74
75         TALLOC_FREE(xacl.info.sd);
76
77         return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
78 }
79
80 static NTSTATUS get_acl_blob(vfs_handle_struct *handle,
81                         TALLOC_CTX *ctx,
82                         files_struct *fsp,
83                         const char *name,
84                         DATA_BLOB *pblob)
85 {
86         size_t size = 1024;
87         uint8_t *val = NULL;
88         uint8_t *tmp;
89         ssize_t sizeret;
90         int saved_errno;
91
92         ZERO_STRUCTP(pblob);
93
94   again:
95
96         tmp = TALLOC_REALLOC_ARRAY(ctx, val, uint8_t, size);
97         if (tmp == NULL) {
98                 TALLOC_FREE(val);
99                 return NT_STATUS_NO_MEMORY;
100         }
101         val = tmp;
102
103         become_root();
104         if (fsp) {
105                 sizeret = SMB_VFS_FGETXATTR(fsp, XATTR_NTACL_NAME, val, size);
106         } else {
107                 sizeret = SMB_VFS_GETXATTR(handle->conn, name,
108                                         XATTR_NTACL_NAME, val, size);
109         }
110         if (sizeret == -1) {
111                 saved_errno = errno;
112         }
113         unbecome_root();
114
115         /* Max ACL size is 65536 bytes. */
116         if (sizeret == -1) {
117                 errno = saved_errno;
118                 if ((errno == ERANGE) && (size != 65536)) {
119                         /* Too small, try again. */
120                         size = 65536;
121                         goto again;
122                 }
123
124                 /* Real error - exit here. */
125                 TALLOC_FREE(val);
126                 return map_nt_error_from_unix(errno);
127         }
128
129         pblob->data = val;
130         pblob->length = sizeret;
131         return NT_STATUS_OK;
132 }
133
134 static int mkdir_acl_xattr(vfs_handle_struct *handle,  const char *path, mode_t mode)
135 {
136         return SMB_VFS_NEXT_MKDIR(handle, path, mode);
137 }
138
139 static int rmdir_acl_xattr(vfs_handle_struct *handle,  const char *path)
140 {
141         return SMB_VFS_NEXT_RMDIR(handle, path);
142 }
143
144 static int open_acl_xattr(vfs_handle_struct *handle,  const char *fname, files_struct *fsp, int flags, mode_t mode)
145 {
146         return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
147 }
148
149 static int unlink_acl_xattr(vfs_handle_struct *handle,  const char *fname)
150 {
151         return SMB_VFS_NEXT_UNLINK(handle, fname);
152 }
153
154 static NTSTATUS get_nt_acl_xattr_internal(vfs_handle_struct *handle,
155                                         files_struct *fsp,
156                                         const char *name,
157                                         uint32 security_info,
158                                         SEC_DESC **ppdesc)
159 {
160         TALLOC_CTX *ctx = talloc_tos();
161         DATA_BLOB blob;
162         SMB_STRUCT_STAT sbuf;
163         NTSTATUS status;
164
165         if (fsp && name == NULL) {
166                 name = fsp->fsp_name;
167         }
168
169         DEBUG(10, ("get_nt_acl_xattr_internal: name=%s\n", name));
170
171         status = get_acl_blob(ctx, handle, fsp, name, &blob);
172         if (!NT_STATUS_IS_OK(status)) {
173                 DEBUG(10, ("get_acl_blob returned %s\n", nt_errstr(status)));
174                 return status;
175         }
176
177         if (fsp && fsp->fh->fd != -1) {
178                 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
179                         return map_nt_error_from_unix(errno);
180                 }
181         } else {
182                 if (SMB_VFS_STAT(handle->conn, name, &sbuf) == -1) {
183                         return map_nt_error_from_unix(errno);
184                 }
185         }
186
187         status = parse_acl_blob(&blob, get_ctimespec(&sbuf),
188                         security_info, ppdesc);
189         if (!NT_STATUS_IS_OK(status)) {
190                 DEBUG(10, ("parse_acl_blob returned %s\n",
191                                 nt_errstr(status)));
192                 return status;
193         }
194
195         TALLOC_FREE(blob.data);
196         return status;
197 }
198
199 static NTSTATUS fget_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
200         uint32 security_info, SEC_DESC **ppdesc)
201 {
202         NTSTATUS status = get_nt_acl_xattr_internal(handle, fsp,
203                                 NULL, security_info, ppdesc);
204         if (NT_STATUS_IS_OK(status)) {
205                 return NT_STATUS_OK;
206         }
207         return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp,
208                         security_info, ppdesc);
209 }
210
211 static NTSTATUS get_nt_acl_xattr(vfs_handle_struct *handle,
212         const char *name, uint32 security_info, SEC_DESC **ppdesc)
213 {
214         NTSTATUS status = get_nt_acl_xattr_internal(handle, NULL,
215                                 name, security_info, ppdesc);
216         if (NT_STATUS_IS_OK(status)) {
217                 return NT_STATUS_OK;
218         }
219         return SMB_VFS_NEXT_GET_NT_ACL(handle, name,
220                         security_info, ppdesc);
221 }
222
223 static NTSTATUS create_acl_blob(SEC_DESC *psd, DATA_BLOB *pblob)
224 {
225         struct xattr_NTACL xacl;
226         enum ndr_err_code ndr_err;
227         TALLOC_CTX *ctx = talloc_tos();
228         struct timespec curr = timespec_current();
229
230         /* Horrid hack as setting an xattr changes the ctime
231          * on Linux. This gives a race of 1 second during
232          * which we would not see a POSIX ACL set.
233          */
234         curr.tv_sec += 1;
235
236         xacl.version = 2;
237         xacl.info.sd_ts->sd = psd;
238         unix_timespec_to_nt_time(&xacl.info.sd_ts->last_changed, curr);
239
240         ndr_err = ndr_push_struct_blob(
241                         pblob, ctx, &xacl,
242                         (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
243
244         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
245                 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
246                         ndr_errstr(ndr_err)));
247                 return ndr_map_error2ntstatus(ndr_err);;
248         }
249
250         return NT_STATUS_OK;
251 }
252
253 static NTSTATUS store_acl_blob(files_struct *fsp,
254                                 DATA_BLOB *pblob)
255 {
256         int ret;
257         int saved_errno;
258
259         become_root();
260         ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
261                         pblob->data, pblob->length, 0);
262         if (ret) {
263                 saved_errno = errno;
264         }
265         unbecome_root();
266         if (ret) {
267                 errno = saved_errno;
268                 DEBUG(5, ("store_acl_blob: fsetxattr failed for file %s "
269                         "with error %s\n",
270                         fsp->fsp_name,
271                         strerror(errno) ));
272                 return map_nt_error_from_unix(errno);
273         }
274         return NT_STATUS_OK;
275 }
276
277 static NTSTATUS fset_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
278         uint32 security_info_sent, SEC_DESC *psd)
279 {
280         NTSTATUS status;
281         DATA_BLOB blob;
282
283         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
284         if (!NT_STATUS_IS_OK(status)) {
285                 return status;
286         }
287
288         create_acl_blob(psd, &blob);
289         store_acl_blob(fsp, &blob);
290
291         return NT_STATUS_OK;
292 }
293
294 /* VFS operations structure */
295
296 static vfs_op_tuple skel_op_tuples[] =
297 {
298         {SMB_VFS_OP(mkdir_acl_xattr), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
299         {SMB_VFS_OP(rmdir_acl_xattr), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
300         {SMB_VFS_OP(open_acl_xattr),  SMB_VFS_OP_OPEN,  SMB_VFS_LAYER_TRANSPARENT},
301         {SMB_VFS_OP(unlink_acl_xattr),SMB_VFS_OP_UNLINK,SMB_VFS_LAYER_TRANSPARENT},
302
303         /* NT File ACL operations */
304
305         {SMB_VFS_OP(fget_nt_acl_xattr),SMB_VFS_OP_FGET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
306         {SMB_VFS_OP(get_nt_acl_xattr), SMB_VFS_OP_GET_NT_ACL, SMB_VFS_LAYER_TRANSPARENT},
307         {SMB_VFS_OP(fset_nt_acl_xattr),SMB_VFS_OP_FSET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
308
309         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
310 };
311
312 NTSTATUS vfs_acl_xattr_init(void)
313 {
314         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr", skel_op_tuples);
315 }