summaryrefslogtreecommitdiff
path: root/source4/ntvfs/posix/pvfs_xattr.c
blob: c930fcbd681bb081ea2817d60e9774b81ab0157d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* 
   Unix SMB/CIFS implementation.

   POSIX NTVFS backend - xattr support

   Copyright (C) Andrew Tridgell 2004

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "vfs_posix.h"
#include "librpc/gen_ndr/ndr_xattr.h"

/*
  pull a xattr as a blob
*/
static NTSTATUS pull_xattr_blob(struct pvfs_state *pvfs,
				TALLOC_CTX *mem_ctx,
				const char *attr_name, 
				const char *fname, 
				int fd, 
				size_t estimated_size,
				DATA_BLOB *blob)
{
	NTSTATUS status;

	if (pvfs->ea_db) {
		return pull_xattr_blob_tdb(pvfs, mem_ctx, attr_name, fname, 
					   fd, estimated_size, blob);
	}

	status = pull_xattr_blob_system(pvfs, mem_ctx, attr_name, fname, 
					fd, estimated_size, blob);

	/* if the filesystem doesn't support them, then tell pvfs not to try again */
	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
		DEBUG(5,("pvfs_xattr: xattr not supported in filesystem\n"));
		pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
		status = NT_STATUS_NOT_FOUND;
	}

	return status;
}

/*
  push a xattr as a blob
*/
static NTSTATUS push_xattr_blob(struct pvfs_state *pvfs,
				const char *attr_name, 
				const char *fname, 
				int fd, 
				const DATA_BLOB *blob)
{
	if (pvfs->ea_db) {
		return push_xattr_blob_tdb(pvfs, attr_name, fname, fd, blob);
	}
	return push_xattr_blob_system(pvfs, attr_name, fname, fd, blob);
}


/*
  delete a xattr
*/
static NTSTATUS delete_xattr(struct pvfs_state *pvfs, const char *attr_name, 
			     const char *fname, int fd)
{
	if (pvfs->ea_db) {
		return delete_xattr_tdb(pvfs, attr_name, fname, fd);
	}
	return delete_xattr_system(pvfs, attr_name, fname, fd);
}

/*
  a hook called on unlink - allows the tdb xattr backend to cleanup
*/
NTSTATUS pvfs_xattr_unlink_hook(struct pvfs_state *pvfs, const char *fname)
{
	if (pvfs->ea_db) {
		return unlink_xattr_tdb(pvfs, fname);
	}
	return unlink_xattr_system(pvfs, fname);
}


/*
  load a NDR structure from a xattr
*/
static NTSTATUS pvfs_xattr_ndr_load(struct pvfs_state *pvfs,
				    TALLOC_CTX *mem_ctx,
				    const char *fname, int fd, const char *attr_name,
				    void *p, ndr_pull_flags_fn_t pull_fn)
{
	NTSTATUS status;
	DATA_BLOB blob;

	status = pull_xattr_blob(pvfs, mem_ctx, attr_name, fname, 
				 fd, XATTR_DOSATTRIB_ESTIMATED_SIZE, &blob);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* pull the blob */
	status = ndr_pull_struct_blob(&blob, mem_ctx, p, pull_fn);

	data_blob_free(&blob);

	return status;
}

/*
  save a NDR structure into a xattr
*/
static NTSTATUS pvfs_xattr_ndr_save(struct pvfs_state *pvfs,
				    const char *fname, int fd, const char *attr_name, 
				    void *p, ndr_push_flags_fn_t push_fn)
{
	TALLOC_CTX *mem_ctx = talloc_new(NULL);
	DATA_BLOB blob;
	NTSTATUS status;

	status = ndr_push_struct_blob(&blob, mem_ctx, p, push_fn);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(mem_ctx);
		return status;
	}

	status = push_xattr_blob(pvfs, attr_name, fname, fd, &blob);
	talloc_free(mem_ctx);

	return status;
}


/*
  fill in file attributes from extended attributes
*/
NTSTATUS pvfs_dosattrib_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
{
	NTSTATUS status;
	struct xattr_DosAttrib attrib;
	TALLOC_CTX *mem_ctx = talloc_new(name);
	struct xattr_DosInfo1 *info1;
	struct xattr_DosInfo2 *info2;

	if (name->stream_name != NULL) {
		name->stream_exists = False;
	} else {
		name->stream_exists = True;
	}

	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_OK;
	}

	status = pvfs_xattr_ndr_load(pvfs, mem_ctx, name->full_name, 
				     fd, XATTR_DOSATTRIB_NAME,
				     &attrib, 
				     (ndr_pull_flags_fn_t)ndr_pull_xattr_DosAttrib);

	/* not having a DosAttrib is not an error */
	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		talloc_free(mem_ctx);
		return pvfs_stream_info(pvfs, name, fd);
	}

	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(mem_ctx);
		return status;
	}

	switch (attrib.version) {
	case 1:
		info1 = &attrib.info.info1;
		name->dos.attrib = pvfs_attrib_normalise(info1->attrib);
		name->dos.ea_size = info1->ea_size;
		if (name->st.st_size == info1->size) {
			name->dos.alloc_size = 
				pvfs_round_alloc_size(pvfs, info1->alloc_size);
		}
		if (info1->create_time != 0) {
			name->dos.create_time = info1->create_time;
		}
		if (info1->change_time != 0) {
			name->dos.change_time = info1->change_time;
		}
		name->dos.flags = 0;
		break;

	case 2:
		info2 = &attrib.info.info2;
		name->dos.attrib = pvfs_attrib_normalise(info2->attrib);
		name->dos.ea_size = info2->ea_size;
		if (name->st.st_size == info2->size) {
			name->dos.alloc_size = 
				pvfs_round_alloc_size(pvfs, info2->alloc_size);
		}
		if (info2->create_time != 0) {
			name->dos.create_time = info2->create_time;
		}
		if (info2->change_time != 0) {
			name->dos.change_time = info2->change_time;
		}
		name->dos.flags = info2->flags;
		if (name->dos.flags & XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME) {
			name->dos.write_time = info2->write_time;
		}
		break;

	default:
		DEBUG(0,("ERROR: Unsupported xattr DosAttrib version %d on '%s'\n",
			 attrib.version, name->full_name));
		talloc_free(mem_ctx);
		return NT_STATUS_INVALID_LEVEL;
	}
	talloc_free(mem_ctx);
	
	status = pvfs_stream_info(pvfs, name, fd);

	return status;
}


/*
  save the file attribute into the xattr
*/
NTSTATUS pvfs_dosattrib_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
{
	struct xattr_DosAttrib attrib;
	struct xattr_DosInfo2 *info2;

	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_OK;
	}

	attrib.version = 2;
	info2 = &attrib.info.info2;

	name->dos.attrib = pvfs_attrib_normalise(name->dos.attrib);

	info2->attrib      = name->dos.attrib;
	info2->ea_size     = name->dos.ea_size;
	info2->size        = name->st.st_size;
	info2->alloc_size  = name->dos.alloc_size;
	info2->create_time = name->dos.create_time;
	info2->change_time = name->dos.change_time;
	info2->write_time  = name->dos.write_time;
	info2->flags       = name->dos.flags;
	info2->name        = "";

	return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, 
				   XATTR_DOSATTRIB_NAME, &attrib, 
				   (ndr_push_flags_fn_t)ndr_push_xattr_DosAttrib);
}


/*
  load the set of DOS EAs
*/
NTSTATUS pvfs_doseas_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
			  struct xattr_DosEAs *eas)
{
	NTSTATUS status;
	ZERO_STRUCTP(eas);
	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_OK;
	}
	status = pvfs_xattr_ndr_load(pvfs, eas, name->full_name, fd, XATTR_DOSEAS_NAME,
				     eas, (ndr_pull_flags_fn_t)ndr_pull_xattr_DosEAs);
	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		return NT_STATUS_OK;
	}
	return status;
}

/*
  save the set of DOS EAs
*/
NTSTATUS pvfs_doseas_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
			  struct xattr_DosEAs *eas)
{
	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_OK;
	}
	return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, XATTR_DOSEAS_NAME, eas, 
				   (ndr_push_flags_fn_t)ndr_push_xattr_DosEAs);
}


/*
  load the set of streams from extended attributes
*/
NTSTATUS pvfs_streams_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
			   struct xattr_DosStreams *streams)
{
	NTSTATUS status;
	ZERO_STRUCTP(streams);
	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_OK;
	}
	status = pvfs_xattr_ndr_load(pvfs, streams, name->full_name, fd, 
				     XATTR_DOSSTREAMS_NAME,
				     streams, 
				     (ndr_pull_flags_fn_t)ndr_pull_xattr_DosStreams);
	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		return NT_STATUS_OK;
	}
	return status;
}

/*
  save the set of streams into filesystem xattr
*/
NTSTATUS pvfs_streams_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
			   struct xattr_DosStreams *streams)
{
	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_OK;
	}
	return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, 
				   XATTR_DOSSTREAMS_NAME, 
				   streams, 
				   (ndr_push_flags_fn_t)ndr_push_xattr_DosStreams);
}


/*
  load the current ACL from extended attributes
*/
NTSTATUS pvfs_acl_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
		       struct xattr_NTACL *acl)
{
	NTSTATUS status;
	ZERO_STRUCTP(acl);
	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_NOT_FOUND;
	}
	status = pvfs_xattr_ndr_load(pvfs, acl, name->full_name, fd, 
				     XATTR_NTACL_NAME,
				     acl, 
				     (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
	return status;
}

/*
  save the acl for a file into filesystem xattr
*/
NTSTATUS pvfs_acl_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
		       struct xattr_NTACL *acl)
{
	NTSTATUS status;
	void *privs;

	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_OK;
	}

	/* this xattr is in the "system" namespace, so we need
	   admin privileges to set it */
	privs = root_privileges();
	status = pvfs_xattr_ndr_save(pvfs, name->full_name, fd, 
				     XATTR_NTACL_NAME, 
				     acl, 
				     (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
	talloc_free(privs);
	return status;
}

/*
  create a zero length xattr with the given name
*/
NTSTATUS pvfs_xattr_create(struct pvfs_state *pvfs, 
			   const char *fname, int fd,
			   const char *attr_prefix,
			   const char *attr_name)
{
	NTSTATUS status;
	DATA_BLOB blob = data_blob(NULL, 0);
	char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
	if (aname == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	status = push_xattr_blob(pvfs, aname, fname, fd, &blob);
	talloc_free(aname);
	return status;
}


/*
  delete a xattr with the given name
*/
NTSTATUS pvfs_xattr_delete(struct pvfs_state *pvfs, 
			   const char *fname, int fd,
			   const char *attr_prefix,
			   const char *attr_name)
{
	NTSTATUS status;
	char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
	if (aname == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	status = delete_xattr(pvfs, aname, fname, fd);
	talloc_free(aname);
	return status;
}

/*
  load a xattr with the given name
*/
NTSTATUS pvfs_xattr_load(struct pvfs_state *pvfs, 
			 TALLOC_CTX *mem_ctx,
			 const char *fname, int fd,
			 const char *attr_prefix,
			 const char *attr_name,
			 size_t estimated_size,
			 DATA_BLOB *blob)
{
	NTSTATUS status;
	char *aname = talloc_asprintf(mem_ctx, "%s%s", attr_prefix, attr_name);
	if (aname == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	status = pull_xattr_blob(pvfs, mem_ctx, aname, fname, fd, estimated_size, blob);
	talloc_free(aname);
	return status;
}

/*
  save a xattr with the given name
*/
NTSTATUS pvfs_xattr_save(struct pvfs_state *pvfs, 
			 const char *fname, int fd,
			 const char *attr_prefix,
			 const char *attr_name,
			 const DATA_BLOB *blob)
{
	NTSTATUS status;
	char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
	if (aname == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	status = push_xattr_blob(pvfs, aname, fname, fd, blob);
	talloc_free(aname);
	return status;
}