2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - xattr support
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "vfs_posix.h"
27 pull a xattr as a blob
29 static NTSTATUS pull_xattr_blob(struct pvfs_state *pvfs,
31 const char *attr_name,
34 size_t estimated_size,
40 return pull_xattr_blob_tdb(pvfs, mem_ctx, attr_name, fname,
41 fd, estimated_size, blob);
44 status = pull_xattr_blob_system(pvfs, mem_ctx, attr_name, fname,
45 fd, estimated_size, blob);
47 /* if the filesystem doesn't support them, then tell pvfs not to try again */
48 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
49 DEBUG(5,("pvfs_xattr: xattr not supported in filesystem\n"));
50 pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
51 status = NT_STATUS_NOT_FOUND;
58 push a xattr as a blob
60 static NTSTATUS push_xattr_blob(struct pvfs_state *pvfs,
61 const char *attr_name,
64 const DATA_BLOB *blob)
67 return push_xattr_blob_tdb(pvfs, attr_name, fname, fd, blob);
69 return push_xattr_blob_system(pvfs, attr_name, fname, fd, blob);
76 static NTSTATUS delete_xattr(struct pvfs_state *pvfs, const char *attr_name,
77 const char *fname, int fd)
80 return delete_xattr_tdb(pvfs, attr_name, fname, fd);
82 return delete_xattr_system(pvfs, attr_name, fname, fd);
86 a hook called on unlink - allows the tdb xattr backend to cleanup
88 NTSTATUS pvfs_xattr_unlink_hook(struct pvfs_state *pvfs, const char *fname)
91 return unlink_xattr_tdb(pvfs, fname);
93 return unlink_xattr_system(pvfs, fname);
98 load a NDR structure from a xattr
100 static NTSTATUS pvfs_xattr_ndr_load(struct pvfs_state *pvfs,
102 const char *fname, int fd, const char *attr_name,
103 void *p, ndr_pull_flags_fn_t pull_fn)
108 status = pull_xattr_blob(pvfs, mem_ctx, attr_name, fname,
109 fd, XATTR_DOSATTRIB_ESTIMATED_SIZE, &blob);
110 if (!NT_STATUS_IS_OK(status)) {
115 status = ndr_pull_struct_blob(&blob, mem_ctx, p, pull_fn);
117 data_blob_free(&blob);
123 save a NDR structure into a xattr
125 static NTSTATUS pvfs_xattr_ndr_save(struct pvfs_state *pvfs,
126 const char *fname, int fd, const char *attr_name,
127 void *p, ndr_push_flags_fn_t push_fn)
129 TALLOC_CTX *mem_ctx = talloc_new(NULL);
133 status = ndr_push_struct_blob(&blob, mem_ctx, p, push_fn);
134 if (!NT_STATUS_IS_OK(status)) {
135 talloc_free(mem_ctx);
139 status = push_xattr_blob(pvfs, attr_name, fname, fd, &blob);
140 talloc_free(mem_ctx);
147 fill in file attributes from extended attributes
149 NTSTATUS pvfs_dosattrib_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
152 struct xattr_DosAttrib attrib;
153 TALLOC_CTX *mem_ctx = talloc_new(name);
154 struct xattr_DosInfo1 *info1;
155 struct xattr_DosInfo2 *info2;
157 if (name->stream_name != NULL) {
158 name->stream_exists = False;
160 name->stream_exists = True;
163 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
167 status = pvfs_xattr_ndr_load(pvfs, mem_ctx, name->full_name,
168 fd, XATTR_DOSATTRIB_NAME,
170 (ndr_pull_flags_fn_t)ndr_pull_xattr_DosAttrib);
172 /* not having a DosAttrib is not an error */
173 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
174 talloc_free(mem_ctx);
175 return pvfs_stream_info(pvfs, name, fd);
178 if (!NT_STATUS_IS_OK(status)) {
179 talloc_free(mem_ctx);
183 switch (attrib.version) {
185 info1 = &attrib.info.info1;
186 name->dos.attrib = pvfs_attrib_normalise(info1->attrib,
188 name->dos.ea_size = info1->ea_size;
189 if (name->st.st_size == info1->size) {
190 name->dos.alloc_size =
191 pvfs_round_alloc_size(pvfs, info1->alloc_size);
193 if (!null_nttime(info1->create_time)) {
194 name->dos.create_time = info1->create_time;
196 if (!null_nttime(info1->change_time)) {
197 name->dos.change_time = info1->change_time;
203 info2 = &attrib.info.info2;
204 name->dos.attrib = pvfs_attrib_normalise(info2->attrib,
206 name->dos.ea_size = info2->ea_size;
207 if (name->st.st_size == info2->size) {
208 name->dos.alloc_size =
209 pvfs_round_alloc_size(pvfs, info2->alloc_size);
211 if (!null_nttime(info2->create_time)) {
212 name->dos.create_time = info2->create_time;
214 if (!null_nttime(info2->change_time)) {
215 name->dos.change_time = info2->change_time;
217 name->dos.flags = info2->flags;
218 if (name->dos.flags & XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME) {
219 name->dos.write_time = info2->write_time;
224 DEBUG(0,("ERROR: Unsupported xattr DosAttrib version %d on '%s'\n",
225 attrib.version, name->full_name));
226 talloc_free(mem_ctx);
227 return NT_STATUS_INVALID_LEVEL;
229 talloc_free(mem_ctx);
231 status = pvfs_stream_info(pvfs, name, fd);
238 save the file attribute into the xattr
240 NTSTATUS pvfs_dosattrib_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
242 struct xattr_DosAttrib attrib;
243 struct xattr_DosInfo2 *info2;
245 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
250 info2 = &attrib.info.info2;
252 name->dos.attrib = pvfs_attrib_normalise(name->dos.attrib, name->st.st_mode);
254 info2->attrib = name->dos.attrib;
255 info2->ea_size = name->dos.ea_size;
256 info2->size = name->st.st_size;
257 info2->alloc_size = name->dos.alloc_size;
258 info2->create_time = name->dos.create_time;
259 info2->change_time = name->dos.change_time;
260 info2->write_time = name->dos.write_time;
261 info2->flags = name->dos.flags;
264 return pvfs_xattr_ndr_save(pvfs, name->full_name, fd,
265 XATTR_DOSATTRIB_NAME, &attrib,
266 (ndr_push_flags_fn_t)ndr_push_xattr_DosAttrib);
271 load the set of DOS EAs
273 NTSTATUS pvfs_doseas_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
274 struct xattr_DosEAs *eas)
278 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
281 status = pvfs_xattr_ndr_load(pvfs, eas, name->full_name, fd, XATTR_DOSEAS_NAME,
282 eas, (ndr_pull_flags_fn_t)ndr_pull_xattr_DosEAs);
283 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
290 save the set of DOS EAs
292 NTSTATUS pvfs_doseas_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
293 struct xattr_DosEAs *eas)
295 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
298 return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, XATTR_DOSEAS_NAME, eas,
299 (ndr_push_flags_fn_t)ndr_push_xattr_DosEAs);
304 load the set of streams from extended attributes
306 NTSTATUS pvfs_streams_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
307 struct xattr_DosStreams *streams)
310 ZERO_STRUCTP(streams);
311 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
314 status = pvfs_xattr_ndr_load(pvfs, streams, name->full_name, fd,
315 XATTR_DOSSTREAMS_NAME,
317 (ndr_pull_flags_fn_t)ndr_pull_xattr_DosStreams);
318 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
325 save the set of streams into filesystem xattr
327 NTSTATUS pvfs_streams_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
328 struct xattr_DosStreams *streams)
330 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
333 return pvfs_xattr_ndr_save(pvfs, name->full_name, fd,
334 XATTR_DOSSTREAMS_NAME,
336 (ndr_push_flags_fn_t)ndr_push_xattr_DosStreams);
341 load the current ACL from extended attributes
343 NTSTATUS pvfs_acl_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
344 struct xattr_NTACL *acl)
348 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
349 return NT_STATUS_NOT_FOUND;
351 status = pvfs_xattr_ndr_load(pvfs, acl, name->full_name, fd,
354 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
359 save the acl for a file into filesystem xattr
361 NTSTATUS pvfs_acl_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
362 struct xattr_NTACL *acl)
367 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
371 /* this xattr is in the "system" namespace, so we need
372 admin privileges to set it */
373 privs = root_privileges();
374 status = pvfs_xattr_ndr_save(pvfs, name->full_name, fd,
377 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
383 create a zero length xattr with the given name
385 NTSTATUS pvfs_xattr_create(struct pvfs_state *pvfs,
386 const char *fname, int fd,
387 const char *attr_prefix,
388 const char *attr_name)
391 DATA_BLOB blob = data_blob(NULL, 0);
392 char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
394 return NT_STATUS_NO_MEMORY;
396 status = push_xattr_blob(pvfs, aname, fname, fd, &blob);
403 delete a xattr with the given name
405 NTSTATUS pvfs_xattr_delete(struct pvfs_state *pvfs,
406 const char *fname, int fd,
407 const char *attr_prefix,
408 const char *attr_name)
411 char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
413 return NT_STATUS_NO_MEMORY;
415 status = delete_xattr(pvfs, aname, fname, fd);
421 load a xattr with the given name
423 NTSTATUS pvfs_xattr_load(struct pvfs_state *pvfs,
425 const char *fname, int fd,
426 const char *attr_prefix,
427 const char *attr_name,
428 size_t estimated_size,
432 char *aname = talloc_asprintf(mem_ctx, "%s%s", attr_prefix, attr_name);
434 return NT_STATUS_NO_MEMORY;
436 status = pull_xattr_blob(pvfs, mem_ctx, aname, fname, fd, estimated_size, blob);
442 save a xattr with the given name
444 NTSTATUS pvfs_xattr_save(struct pvfs_state *pvfs,
445 const char *fname, int fd,
446 const char *attr_prefix,
447 const char *attr_name,
448 const DATA_BLOB *blob)
451 char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
453 return NT_STATUS_NO_MEMORY;
455 status = push_xattr_blob(pvfs, aname, fname, fd, blob);
462 probe for system support for xattrs
464 void pvfs_xattr_probe(struct pvfs_state *pvfs)
466 TALLOC_CTX *tmp_ctx = talloc_new(pvfs);
468 pull_xattr_blob(pvfs, tmp_ctx, "user.XattrProbe", pvfs->base_directory,
470 pull_xattr_blob(pvfs, tmp_ctx, "security.XattrProbe", pvfs->base_directory,
472 talloc_free(tmp_ctx);