summaryrefslogtreecommitdiff
path: root/support/sys-xattr.c
blob: fc03f7ee95a97fd728e63f2933e00a6284de2132 (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 * Wrapper functions for <sys/xattr.h> (or <attr/xattr.h>) and <sys/extattr.h>
 *
 * Authors:
 *   Daniel Drake (dsd@gentoo.org)
 *   Jonathan Pryor (jonpryor@vt.edu)
 *
 * Copyright (C) 2005 Daniel Drake
 * Copyright (C) 2006 Jonathan Pryor
 */

#include <config.h>

#if defined(HAVE_SYS_XATTR_H) || defined(HAVE_ATTR_ATTR_H) || defined(HAVE_SYS_EXTATTR_H)

#include <sys/types.h>

/*
 * Where available, we prefer to use the libc implementation of the xattr
 * syscalls. However, we also support using libattr for this on systems where
 * libc does not provide this (e.g. glibc-2.2 and older)
 * (configure-time magic is used to select which library to link to)
 */
#ifdef HAVE_SYS_XATTR_H
// libc
#include <sys/xattr.h>
#define EA_UNIX
#elif HAVE_ATTR_ATTR_H
// libattr
#include <attr/xattr.h>
#define EA_UNIX
#endif /* HAVE_SYS_XATTR_H */

#ifdef HAVE_SYS_EXTATTR_H
#include <sys/extattr.h>
#include <sys/uio.h>
#define EA_BSD
#endif

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include "map.h"
#include "mph.h"

/*
 * Linux provides extended attributes through the <sys/xattr.h> API.
 * Any file or link can have attributes assigned to it (provided that they are
 * supported by the backing filesystem). Each attribute has to be placed in a
 * namespace, of which "user" is the most common. Namespaces are specified as
 * a prefix to the attribute name, proceeded by a '.' (e.g. user.myattribute)
 *
 * FreeBSD provides extended attributes through the <sys/extattr.h> API.
 * Behaviour is very similar to Linux EA's, but the namespace is specified
 * through an enum-style parameter rather than as a prefix to an attribute
 * name. There are also differences in the behaviour of the "list attributes"
 * system calls.
 *
 * This file merges the two implementations into a single API for use by the
 * Mono.Unix.Syscall.*xattr methods. No matter which OS you are on, things
 * should "just work" the same as anywhere else.
 *
 * The API provided here leans more towards the Linux implementation. Attribute
 * namespaces are provided as prefixes to the attribute name (followed by '.').
 * There is no limit to the namespaces accepted by the Linux side of this
 * implementation, but you are obviously limited to the ones available to you
 * on the system.
 * FreeBSD namespaces have to be converted from the textual prefix into their
 * relevant number so that they can be used in the FreeBSD system calls.
 * This means that the only namespaces available are the ones known by in this
 * file (see bsd_extattr_namespaces). However, you can also specify the
 * numericalnamespace index yourself, by using an attribute name such as
 * "5.myattr".
 * (this will obviously fail on Linux, your code will no longer be 'portable')
 *
 * Linux {,l,f}setxattr calls have a flags parameter which allow you to control
 * what should happen if an attribute with the same name does (or doesn't)
 * already exist. The 'flags' parameter is available here, but because FreeBSD
 * does not support this kind of refinement, it will fail on FreeBSD if you
 * specify anything other than XATTR_AUTO (XATTR_AUTO will create the attribute
 * if it doesn't already exist, and overwrite the existing attribute if it
 * already set).
 * 
 * For usage and behaviour information, see the monodoc documentation on the
 * Mono.Unix.Syscall class.
 */

G_BEGIN_DECLS

//
// HELPER FUNCTIONS
//

#ifdef EA_BSD

struct BsdNamespaceInfo {
	const char *name;
	int value;
};

static struct BsdNamespaceInfo bsd_extattr_namespaces[] = {
	{"user"         , EXTATTR_NAMESPACE_USER},
	{"system"       , EXTATTR_NAMESPACE_SYSTEM}
};

static int bsd_check_flags (gint32 flags)
{
	// BSD doesn't support flags, but always provides the same behaviour as
	// XATTR_AUTO. So we enforce that here.
	if (flags != Mono_Posix_XattrFlags_XATTR_AUTO) {
		errno = EINVAL;
		return -1;
	}
	return 0;
}

// On FreeBSD, we need to convert "user.blah" into namespace 1 and attribute
// name "blah", or maybe "6.blah" into namespace 6 attribute "blah"
static int
bsd_handle_nsprefix (const char *name, char **_name, int *namespace)
{
	int i;
	gchar **components = g_strsplit (name, ".", 2);

	// Find namespace number from textual representation
	for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
		if (strcmp (bsd_extattr_namespaces[i].name, components[0]) == 0) {
			*namespace = bsd_extattr_namespaces[i].value;
			break;
		}

	if (*namespace == 0) {
		// Perhaps they specified the namespace number themselves..?
		char *endptr;
		*namespace = (int) strtol (components[0], &endptr, 10);
		if (*endptr != '\0')
			return -1;
	}

	*_name = g_strdup (components[1]);
	g_strfreev (components);
	return 0;
}

static void
init_attrlists (char *attrlists[])
{
	memset (attrlists, 0, G_N_ELEMENTS(bsd_extattr_namespaces) * sizeof(char*));
}

static void
free_attrlists (char *attrlists[])
{
	int i;
	for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
		g_free (attrlists[i]);
}

// Counts the number of attributes in the result of a
// extattr_list_*() call. Note that the format of the data
// is: \3one\3two\6eleven where the leading charaters represent the length
// of the following attribute. (the description in the man-page is wrong)
static unsigned int
count_num_attrs (char *attrs, size_t size)
{
	size_t i = 0;
	unsigned int num_attrs = 0;

	if (!attrs || !size)
		return 0;

	while (i < size) {
		num_attrs++;
		i += attrs[i] + 1;
	}

	return num_attrs;
}

// Convert a BSD-style list buffer (see the description for count_num_attrs)
// into a Linux-style NULL-terminated list including namespace prefix.
static char
*bsd_convert_list (const char *nsprefix, const char *src, size_t size, char *dest)
{
	size_t i = 0;
	if (src == NULL || dest == NULL || size == 0)
		return NULL;

	while (i < size) {
		// Read length
		int attr_len = (int) src[i];
		int prefix_len = strlen (nsprefix);

		// Add namespace prefix
		strncpy (dest, nsprefix, prefix_len);
		dest[prefix_len] = '.';
		dest += prefix_len + 1;

		// Copy attribute
		memcpy(dest, src + ++i, attr_len);

		// NULL-terminate
		i += attr_len;
		dest[attr_len] = '\0';
		dest += attr_len + 1;
	}

	return dest;
}

// Combine all the lists of attributes that we know about into a single
// Linux-style buffer
static ssize_t
bsd_combine_lists (char *attrlists[], char *dest, size_t dest_size_needed, size_t dest_size)
{
	int i;
	if (!dest)
		return dest_size_needed;

	if (dest_size < dest_size_needed) {
		errno = ERANGE;
		return -1;
	}

	for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
		if (attrlists[i])
			dest = bsd_convert_list (bsd_extattr_namespaces[i].name, attrlists[i], strlen (attrlists[i]), dest);

	return dest_size_needed;
}

static mph_ssize_t
bsd_listxattr (const char *path, void *list, mph_size_t size)
{
	size_t full_size = 0;
	int i;
	char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];

	init_attrlists (attrlists);
	for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
		size_t buf_size;
		int num_attrs;

		buf_size = (size_t) extattr_list_file (path, i + 1, NULL, 0);
		if (buf_size == -1)
			continue;

		attrlists[i] = g_malloc0 (buf_size + 1);
		buf_size = (size_t) extattr_list_file (path, i + 1, attrlists[i], buf_size);
		if (buf_size == -1)
			continue;

		num_attrs = count_num_attrs(attrlists[i], buf_size);
		full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
	}

	full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
	free_attrlists (attrlists);
	return full_size;
}

static mph_ssize_t
bsd_llistxattr (const char *path, void *list, mph_size_t size)
{
	size_t full_size = 0;
	int i;
	char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];

	init_attrlists (attrlists);
	for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
		size_t buf_size;
		int num_attrs;

		buf_size = (size_t) extattr_list_link (path, i + 1, NULL, 0);
		if (buf_size == -1)
			continue;

		attrlists[i] = g_malloc0 (buf_size + 1);
		buf_size = (size_t) extattr_list_link (path, i + 1, attrlists[i], buf_size);
		if (buf_size == -1)
			continue;

		num_attrs = count_num_attrs(attrlists[i], buf_size);
		full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
	}

	full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
	free_attrlists (attrlists);
	return full_size;
}

static mph_ssize_t
bsd_flistxattr (int fd, void *list, mph_size_t size)
{
	size_t full_size = 0;
	int i;
	char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];

	init_attrlists (attrlists);
	for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
		size_t buf_size;
		int num_attrs;

		buf_size = (size_t) extattr_list_fd (fd, i + 1, NULL, 0);
		if (buf_size == -1)
			continue;

		attrlists[i] = g_malloc0 (buf_size + 1);
		buf_size = (size_t) extattr_list_fd (fd, i + 1, attrlists[i], buf_size);
		if (buf_size == -1)
			continue;

		num_attrs = count_num_attrs(attrlists[i], buf_size);
		full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
	}

	full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
	free_attrlists (attrlists);
	return full_size;
}

#endif /* EA_BSD */

//
// THE PROVIDED API
//

gint32
Mono_Posix_Syscall_setxattr (const char *path, const char *name, unsigned char *value, mph_size_t size, gint32 flags)
{
	gint32 ret;
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
	{
		int _flags;
		if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
			return -1;
#if __APPLE__
		ret = setxattr (path, name, value, (size_t) size, 0, _flags);
#else /* __APPLE__ */
		ret = setxattr (path, name, value, (size_t) size, _flags);
#endif /* __APPLE__ */
	}
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_check_flags (flags) == -1)
			return -1;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_set_file (path, namespace, _name, value, (size_t) size);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}

#if !__APPLE__
gint32
Mono_Posix_Syscall_lsetxattr (const char *path, const char *name, unsigned char *value, mph_size_t size, gint32 flags)
{
	gint32 ret;
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
	{
		int _flags;
		if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
			return -1;
		ret = lsetxattr (path, name, value, size, _flags);
	}
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_check_flags (flags) == -1)
			return -1;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_set_link (path, namespace, _name, value, (size_t) size);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}
#endif /* !__APPLE__ */

gint32
Mono_Posix_Syscall_fsetxattr (int fd, const char *name, unsigned char *value, mph_size_t size, gint32 flags)
{
	gint32 ret;
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
	{
		int _flags;
		if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
			return -1;
#if __APPLE__
		ret = fsetxattr (fd, name, value, (size_t) size, 0, _flags);
#else /* __APPLE__ */
		ret = fsetxattr (fd, name, value, (size_t) size, _flags);
#endif /* __APPLE__ */
	}
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_check_flags (flags) == -1)
			return -1;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_set_fd (fd, namespace, _name, value, (size_t) size);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}

mph_ssize_t
Mono_Posix_Syscall_getxattr (const char *path, const char *name, unsigned char *value, mph_size_t size)
{
	mph_ssize_t ret;
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
#if __APPLE__
	ret = getxattr (path, name, value, (size_t) size, 0, 0);
#else /* __APPLE__ */
	ret = getxattr (path, name, value, (size_t) size);
#endif /* __APPLE__ */
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_get_file (path, namespace, _name, value, (size_t) size);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}

#if !__APPLE__
mph_ssize_t
Mono_Posix_Syscall_lgetxattr (const char *path, const char *name, unsigned char *value, mph_size_t size)
{
	mph_ssize_t ret;
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
	ret = lgetxattr (path, name, value, (size_t) size);
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_get_link (path, namespace, _name, value, (size_t) size);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}
#endif /* !__APPLE__ */

mph_ssize_t
Mono_Posix_Syscall_fgetxattr (int fd, const char *name, unsigned char *value, mph_size_t size)
{
	mph_ssize_t ret;
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
#if __APPLE__
	ret = fgetxattr (fd, name, value, (size_t) size, 0, 0);
#else /* __APPLE__ */
	ret = fgetxattr (fd, name, value, (size_t) size);
#endif /* __APPLE__ */
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_get_fd (fd, namespace, _name, value, (size_t) size);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}

mph_ssize_t
Mono_Posix_Syscall_listxattr (const char *path, unsigned char *list, mph_size_t size)
{
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
#if __APPLE__
	return listxattr (path, (char*) list, (size_t) size, 0);
#else /* __APPLE__ */
	return listxattr (path, (char*) list, (size_t) size);
#endif /* __APPLE__ */
#else /* EA_UNIX */
	return bsd_listxattr (path, list, size);
#endif /* EA_UNIX */
}

#if !__APPLE__
mph_ssize_t
Mono_Posix_Syscall_llistxattr (const char *path, unsigned char *list, mph_size_t size)
{
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
	return llistxattr (path, (char*) list, (size_t) size);
#else /* EA_UNIX */
	return bsd_llistxattr (path, list, size);
#endif /* EA_UNIX */
}
#endif /* !__APPLE__ */

mph_ssize_t
Mono_Posix_Syscall_flistxattr (int fd, unsigned char *list, mph_size_t size)
{
	mph_return_if_size_t_overflow (size);

#ifdef EA_UNIX
#if __APPLE__
	return flistxattr (fd, (char*) list, (size_t) size, 0);
#else /* __APPLE__ */
	return flistxattr (fd, (char*) list, (size_t) size);
#endif /* __APPLE__ */
#else /* EA_UNIX */
	return bsd_flistxattr (fd, list, size);
#endif /* EA_UNIX */
}

gint32
Mono_Posix_Syscall_removexattr (const char *path, const char *name)
{
	gint32 ret;

#ifdef EA_UNIX
#if __APPLE__
	ret = removexattr (path, name, 0);
#else /* __APPLE__ */
	ret = removexattr (path, name);
#endif /* __APPLE__ */
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_delete_file (path, namespace, _name);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}

#if !__APPLE__
gint32
Mono_Posix_Syscall_lremovexattr (const char *path, const char *name)
{
	gint32 ret;

#ifdef EA_UNIX
	ret = lremovexattr (path, name);
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_delete_link (path, namespace, _name);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}
#endif /* !__APPLE__ */

gint32
Mono_Posix_Syscall_fremovexattr (int fd, const char *name)
{
	gint32 ret;

#ifdef EA_UNIX
#if __APPLE__
	ret = fremovexattr (fd, name, 0);
#else /* __APPLE__ */
	ret = fremovexattr (fd, name);
#endif /* __APPLE__ */
#else /* EA_UNIX */
	{
		char *_name;
		int namespace;
		if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
			return -1;
		ret = extattr_delete_fd (fd, namespace, _name);
		g_free (_name);
	}
#endif /* EA_UNIX */

	return ret;
}

G_END_DECLS

#endif /* HAVE_SYS_XATTR_H || HAVE_ATTR_ATTR_H || HAVE_SYS_EXTATTR_H */

/*
 * vim: noexpandtab
 */