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