/
xattr.cc
3964 lines (3551 loc) · 127 KB
/
xattr.cc
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2008-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2016 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Functions to handle Extended Attributes for bareos.
*
* Extended Attributes are so OS specific we only restore Extended Attributes if
* they were saved using a filed on the same platform.
*
* Currently we support the following OSes:
* Written by Marco van Wieringen, November 2008
* Major overhaul January 2012 + June 2012
* Moved into findlib so it can be used from other programs, May 2012
*/
/**
* @file
* Functions to handle Extended Attributes for bareos.
*
* Extended Attributes are so OS specific we only restore Extended Attributes if
* they were saved using a filed on the same platform.
*
* Currently we support the following OSes:
* - AIX (Extended Attributes)
* - Darwin (Extended Attributes)
* - FreeBSD (Extended Attributes)
* - GNU HURD (Extended Attributes)
* - IRIX (Extended Attributes)
* - Linux (Extended Attributes)
* - NetBSD (Extended Attributes)
* - OpenBSD (Extended Attributes)
* (As it seems either they never implemented xattr or they are removed
* the support as it stated it was in version 3.1 but the current syscall
* tabled shows the extattr_ functions are not implemented. So as such we
* might eventually support xattr on OpenBSD when they implemented them using
* the same interface as FreeBSD and NetBSD.
* - Solaris (Extended Attributes and Extensible Attributes)
* - Tru64 (Extended Attributes)
*/
#include "include/bareos.h"
#include "find.h"
#include "include/jcr.h"
#if !defined(HAVE_XATTR)
/**
* Entry points when compiled without support for XATTRs or on an unsupported platform.
*/
bxattr_exit_code BuildXattrStreams(JobControlRecord *jcr,
xattr_data_t *xattr_data,
FindFilesPacket *ff_pkt)
{
return bxattr_exit_fatal;
}
bxattr_exit_code ParseXattrStreams(JobControlRecord *jcr,
xattr_data_t *xattr_data,
int stream,
char *content,
uint32_t content_length)
{
return bxattr_exit_fatal;
}
#else
/**
* Send a XATTR stream to the SD.
*/
bxattr_exit_code SendXattrStream(JobControlRecord *jcr,
xattr_data_t *xattr_data,
int stream)
{
BareosSocket *sd = jcr->store_bsock;
POOLMEM *msgsave;
#ifdef FD_NO_SEND_TEST
return bxattr_exit_ok;
#endif
/*
* Sanity check
*/
if (xattr_data->u.build->content_length <= 0) {
return bxattr_exit_ok;
}
/*
* Send header
*/
if (!sd->fsend("%ld %d 0", jcr->JobFiles, stream)) {
Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
sd->bstrerror());
return bxattr_exit_fatal;
}
/*
* Send the buffer to the storage deamon
*/
Dmsg1(400, "Backing up XATTR <%s>\n", xattr_data->u.build->content);
msgsave = sd->msg;
sd->msg = xattr_data->u.build->content;
sd->message_length = xattr_data->u.build->content_length;
if (!sd->send()) {
sd->msg = msgsave;
sd->message_length = 0;
Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
sd->bstrerror());
return bxattr_exit_fatal;
}
jcr->JobBytes += sd->message_length;
sd->msg = msgsave;
if (!sd->signal(BNET_EOD)) {
Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
sd->bstrerror());
return bxattr_exit_fatal;
}
Dmsg1(200, "XATTR of file: %s successfully backed up!\n", xattr_data->last_fname);
return bxattr_exit_ok;
}
/**
* First some generic functions for OSes that use the same xattr encoding scheme.
* Currently for all OSes except for Solaris.
*/
void XattrDropInternalTable(alist *xattr_value_list)
{
xattr_t *current_xattr = nullptr;
/*
* Walk the list of xattrs and free allocated memory on traversing.
*/
foreach_alist(current_xattr, xattr_value_list) {
/*
* See if we can shortcut.
*/
if (current_xattr == NULL || current_xattr->magic != XATTR_MAGIC)
break;
free(current_xattr->name);
if (current_xattr->value_length > 0)
free(current_xattr->value);
free(current_xattr);
}
delete xattr_value_list;
}
/**
* The xattr stream for OSX, FreeBSD, Linux and NetBSD is a serialized stream of bytes
* which encodes one or more xattr_t structures.
*
* The Serialized stream consists of the following elements:
* magic - A magic string which makes it easy to detect any binary incompatabilites
* name_length - The length of the following xattr name
* name - The name of the extended attribute
* value_length - The length of the following xattr data
* value - The actual content of the extended attribute
*
* This is repeated 1 or more times.
*
*/
uint32_t SerializeXattrStream(JobControlRecord *jcr,
xattr_data_t *xattr_data,
uint32_t expected_serialize_len,
alist *xattr_value_list)
{
xattr_t *current_xattr = nullptr;
ser_declare;
/*
* Make sure the serialized stream fits in the poolmem buffer.
* We allocate some more to be sure the stream is gonna fit.
*/
xattr_data->u.build->content =
CheckPoolMemorySize(xattr_data->u.build->content,
expected_serialize_len + 10);
SerBegin(xattr_data->u.build->content,
expected_serialize_len + 10);
/*
* Walk the list of xattrs and Serialize the data.
*/
foreach_alist(current_xattr, xattr_value_list) {
/*
* See if we can shortcut.
*/
if (current_xattr == NULL || current_xattr->magic != XATTR_MAGIC)
break;
ser_uint32(current_xattr->magic);
ser_uint32(current_xattr->name_length);
SerBytes(current_xattr->name, current_xattr->name_length);
ser_uint32(current_xattr->value_length);
if (current_xattr->value_length > 0 && current_xattr->value) {
SerBytes(current_xattr->value, current_xattr->value_length);
Dmsg3(100, "Backup xattr named %s, value %*s\n",
current_xattr->name, current_xattr->value_length, current_xattr->value);
} else {
Dmsg1(100, "Backup empty xattr named %s\n", current_xattr->name);
}
}
SerEnd(xattr_data->u.build->content, expected_serialize_len + 10);
xattr_data->u.build->content_length =
SerLength(xattr_data->u.build->content);
return xattr_data->u.build->content_length;
}
bxattr_exit_code UnSerializeXattrStream(JobControlRecord *jcr,
xattr_data_t *xattr_data,
char *content,
uint32_t content_length,
alist *xattr_value_list)
{
unser_declare;
xattr_t *current_xattr;
/**
* Parse the stream and call restore_xattr_on_file for each extended attribute.
*
* Start unserializing the data. We keep on looping while we have not
* unserialized all bytes in the stream.
*/
UnserBegin(content, content_length);
while (UnserLength(content) < content_length) {
/*
* First make sure the magic is present. This way we can easily catch corruption.
* Any missing MAGIC is fatal we do NOT try to continue.
*/
current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
unser_uint32(current_xattr->magic);
if (current_xattr->magic != XATTR_MAGIC) {
Mmsg1(jcr->errmsg,
_("Illegal xattr stream, no XATTR_MAGIC on file \"%s\"\n"),
xattr_data->last_fname);
Dmsg1(100, "Illegal xattr stream, no XATTR_MAGIC on file \"%s\"\n",
xattr_data->last_fname);
free(current_xattr);
return bxattr_exit_error;
}
/*
* Decode the valuepair. First decode the length of the name.
*/
unser_uint32(current_xattr->name_length);
if (current_xattr->name_length == 0) {
Mmsg1(jcr->errmsg,
_("Illegal xattr stream, xattr name length <= 0 on file \"%s\"\n"),
xattr_data->last_fname);
Dmsg1(100, "Illegal xattr stream, xattr name length <= 0 on file \"%s\"\n",
xattr_data->last_fname);
free(current_xattr);
return bxattr_exit_error;
}
/*
* Allocate room for the name and decode its content.
*/
current_xattr->name = (char *)malloc(current_xattr->name_length + 1);
UnserBytes(current_xattr->name, current_xattr->name_length);
/*
* The xattr_name needs to be null terminated.
*/
current_xattr->name[current_xattr->name_length] = '\0';
/*
* Decode the value length.
*/
unser_uint32(current_xattr->value_length);
if (current_xattr->value_length > 0) {
/*
* Allocate room for the value and decode its content.
*/
current_xattr->value = (char *)malloc(current_xattr->value_length);
UnserBytes(current_xattr->value, current_xattr->value_length);
Dmsg3(100, "Restoring xattr named %s, value %*s\n",
current_xattr->name, current_xattr->value_length, current_xattr->value);
} else {
current_xattr->value = NULL;
Dmsg1(100, "Restoring empty xattr named %s\n", current_xattr->name);
}
xattr_value_list->append(current_xattr);
}
UnserEnd(content, content_length);
return bxattr_exit_ok;
}
/**
* This is a supported OS, See what kind of interface we should use.
*/
#if defined(HAVE_AIX_OS)
#if (!defined(HAVE_LISTEA) && !defined(HAVE_LLISTEA)) || \
(!defined(HAVE_GETEA) && !defined(HAVE_LGETEA)) || \
(!defined(HAVE_SETEA) && !defined(HAVE_LSETEA))
#error "Missing full support for the Extended Attributes (EA) functions."
#endif
#ifdef HAVE_SYS_EA_H
#include <sys/ea.h>
#else
#error "Missing sys/ea.h header file"
#endif
/**
* Define the supported XATTR streams for this OS
*/
static int os_default_xattr_streams[1] = {
STREAM_XATTR_AIX
};
/**
* Fallback to the non l-functions when those are not available.
*/
#if defined(HAVE_GETEA) && !defined(HAVE_LGETEA)
#define lgetea getea
#endif
#if defined(HAVE_SETEA) && !defined(HAVE_LSETEA)
#define lsetea setea
#endif
#if defined(HAVE_LISTEA) && !defined(HAVE_LLISTEA)
#define llistea listea
#endif
static bxattr_exit_code aix_build_xattr_streams(JobControlRecord *jcr,
xattr_data_t *xattr_data,
FindFilesPacket *ff_pkt)
{
char *bp;
bool skip_xattr;
char *xattr_list = NULL;
int cnt, xattr_count = 0;
uint32_t name_length;
int32_t xattr_list_len,
xattr_value_len;
uint32_t expected_serialize_len = 0;
xattr_t *current_xattr;
alist *xattr_value_list = NULL;
bxattr_exit_code retval = bxattr_exit_error;
/*
* First get the length of the available list with extended attributes.
*/
xattr_list_len = llistea(xattr_data->last_fname, NULL, 0);
switch (xattr_list_len) {
case -1: {
BErrNo be;
switch (errno) {
case ENOENT:
case EFORMAT:
retval = bxattr_exit_ok;
goto bail_out;
case ENOTSUP:
/*
* If the filesystem reports it doesn't support XATTRs we clear the
* BXATTR_FLAG_SAVE_NATIVE flag so we skip XATTR saves on all other files
* on the same filesystem. The BXATTR_FLAG_SAVE_NATIVE flags gets sets again
* when we change from one filesystem to another.
*/
xattr_data->flags &= ~BXATTR_FLAG_SAVE_NATIVE;
retval = bxattr_exit_ok;
goto bail_out;
default:
Mmsg2(jcr->errmsg,
_("llistea error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "llistea error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
goto bail_out;
}
break;
}
case 0:
retval = bxattr_exit_ok;
goto bail_out;
default:
break;
}
/*
* Allocate room for the extented attribute list.
*/
xattr_list = (char *)malloc(xattr_list_len + 1);
memset(xattr_list, 0, xattr_list_len + 1);
/*
* Get the actual list of extended attributes names for a file.
*/
xattr_list_len = llistea(xattr_data->last_fname, xattr_list, xattr_list_len);
switch (xattr_list_len) {
case -1: {
BErrNo be;
switch (errno) {
case ENOENT:
case EFORMAT:
retval = bxattr_exit_ok;
goto bail_out;
default:
Mmsg2(jcr->errmsg,
_("llistea error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "llistea error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
goto bail_out;
}
break;
}
default:
break;
}
xattr_list[xattr_list_len] = '\0';
/*
* Walk the list of extended attributes names and retrieve the data.
* We already count the bytes needed for serializing the stream later on.
*/
for (bp = xattr_list;
(bp - xattr_list) + 1 < xattr_list_len;
bp = strchr(bp, '\0') + 1) {
skip_xattr = false;
/*
* We want to skip certain xattrs which start with a 0xF8 character on AIX.
*/
if (*bp == 0xF8) {
skip_xattr = true;
}
name_length = strlen(bp);
if (skip_xattr || name_length == 0) {
Dmsg1(100, "Skipping xattr named %s\n", bp);
continue;
}
/*
* First see how long the value is for the extended attribute.
*/
xattr_value_len = lgetea(xattr_data->last_fname, bp, NULL, 0);
switch (xattr_value_len) {
case -1: {
BErrNo be;
switch (errno) {
case ENOENT:
case EFORMAT:
retval = bxattr_exit_ok;
goto bail_out;
default:
Mmsg2(jcr->errmsg,
_("lgetea error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "lgetea error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
goto bail_out;
}
break;
}
default:
break;
}
/*
* Each xattr valuepair starts with a magic so we can parse it easier.
*/
current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
current_xattr->magic = XATTR_MAGIC;
expected_serialize_len += sizeof(current_xattr->magic);
/*
* Allocate space for storing the name.
*/
current_xattr->name_length = name_length;
current_xattr->name = (char *)malloc(current_xattr->name_length);
memcpy(current_xattr->name, bp, current_xattr->name_length);
expected_serialize_len += sizeof(current_xattr->name_length) +
current_xattr->name_length;
switch (xattr_value_len) {
case 0:
current_xattr->value = NULL;
current_xattr->value_length = 0;
expected_serialize_len += sizeof(current_xattr->value_length);
break;
default:
/*
* Allocate space for storing the value.
*/
current_xattr->value = (char *)malloc(xattr_value_len);
memset(current_xattr->value, 0, xattr_value_len);
xattr_value_len = lgetea(xattr_data->last_fname, bp, current_xattr->value, xattr_value_len);
if (xattr_value_len < 0) {
BErrNo be;
switch (errno) {
case ENOENT:
case EFORMAT:
retval = bxattr_exit_ok;
break;
default:
Mmsg2(jcr->errmsg,
_("lgetea error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "lgetea error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
break;
}
/*
* Default failure path out when retrieval of attr fails.
*/
free(current_xattr->value);
free(current_xattr->name);
free(current_xattr);
goto bail_out;
}
/*
* Store the actual length of the value.
*/
current_xattr->value_length = xattr_value_len;
expected_serialize_len += sizeof(current_xattr->value_length) +
current_xattr->value_length;
break;
}
if (xattr_value_list == NULL) {
xattr_value_list = New(alist(10, not_owned_by_alist));
}
xattr_value_list->append(current_xattr);
xattr_count++;
/*
* Protect ourself against things getting out of hand.
*/
if (expected_serialize_len >= MAX_XATTR_STREAM) {
Mmsg2(jcr->errmsg,
_("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
xattr_data->last_fname, MAX_XATTR_STREAM);
goto bail_out;
}
}
free(xattr_list);
xattr_list = (char *)NULL;
/*
* If we found any xattr send them to the SD.
*/
if (xattr_count > 0) {
/*
* Serialize the datastream.
*/
if (SerializeXattrStream(jcr,
xattr_data,
expected_serialize_len,
xattr_value_list) < expected_serialize_len) {
Mmsg1(jcr->errmsg,
_("Failed to Serialize extended attributes on file \"%s\"\n"),
xattr_data->last_fname);
Dmsg1(100, "Failed to Serialize extended attributes on file \"%s\"\n",
xattr_data->last_fname);
goto bail_out;
}
/*
* Send the datastream to the SD.
*/
retval = SendXattrStream(jcr, xattr_data, os_default_xattr_streams[0]);
} else {
retval = bxattr_exit_ok;
}
bail_out:
if (xattr_list != NULL) {
free(xattr_list);
}
if (xattr_value_list != NULL) {
XattrDropInternalTable(xattr_value_list);
}
return retval;
}
static bxattr_exit_code aix_parse_xattr_streams(JobControlRecord *jcr,
xattr_data_t *xattr_data,
int stream,
char *content,
uint32_t content_length)
{
xattr_t *current_xattr;
alist *xattr_value_list;
bxattr_exit_code retval = bxattr_exit_error;
xattr_value_list = New(alist(10, not_owned_by_alist));
if (UnSerializeXattrStream(jcr,
xattr_data,
content,
content_length,
xattr_value_list) != bxattr_exit_ok) {
goto bail_out;
}
foreach_alist(current_xattr, xattr_value_list) {
if (lsetea(xattr_data->last_fname,
current_xattr->name,
current_xattr->value,
current_xattr->value_length, 0) != 0) {
BErrNo be;
switch (errno) {
case ENOENT:
case EFORMAT:
goto bail_out;
case ENOTSUP:
/*
* If the filesystem reports it doesn't support XATTRs we clear
* the BXATTR_FLAG_RESTORE_NATIVE flag so we skip XATTR restores
* on all other files on the same filesystem. The
* BXATTR_FLAG_RESTORE_NATIVE flags gets sets again when we
* change from one filesystem to another.
*/
xattr_data->flags &= ~BXATTR_FLAG_RESTORE_NATIVE;
goto bail_out;
default:
Mmsg2(jcr->errmsg,
_("lsetea error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "lsetea error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
goto bail_out;
}
}
}
retval = bxattr_exit_ok;
bail_out:
XattrDropInternalTable(xattr_value_list);
return retval;
}
/**
* Function pointers to the build and parse function to use for these xattrs.
*/
static bxattr_exit_code (*os_build_xattr_streams)
(JobControlRecord *jcr, xattr_data_t *xattr_data, FindFilesPacket *ff_pkt) =
aix_build_xattr_streams;
static bxattr_exit_code (*os_parse_xattr_streams)
(JobControlRecord *jcr, xattr_data_t *xattr_data,
int stream, char *content, uint32_t content_length) =
aix_parse_xattr_streams;
#elif defined(HAVE_IRIX_OS)
#include <sys/attributes.h>
/**
* Define the supported XATTR streams for this OS
*/
static int os_default_xattr_streams[1] = {
STREAM_XATTR_IRIX
};
static const char *xattr_acl_skiplist[1] = {
NULL
};
static const char *xattr_skiplist[1] = {
NULL
};
struct xattr_naming_space {
const char *name;
int flags;
};
static xattr_naming_space xattr_naming_spaces[] = {
{
"user.",
ATTR_DONTFOLLOW
}, {
"root.",
ATTR_ROOT | ATTR_DONTFOLLOW
}, {
NULL,
0
}
};
static bxattr_exit_code irix_build_xattr_streams(JobControlRecord *jcr,
xattr_data_t *xattr_data,
FindFilesPacket *ff_pkt)
{
char dummy[32];
int cnt, length, xattr_count = 0;
attrlist_cursor_t cursor;
attrlist_t *attrlist;
attrlist_ent_t *attrlist_ent;
xattr_t *current_xattr;
alist *xattr_value_list = NULL;
uint32_t expected_serialize_len = 0;
bxattr_exit_code retval = bxattr_exit_error;
POOLMEM *xattrbuf = GetMemory(ATTR_MAX_VALUELEN);
for (cnt = 0; xattr_naming_spaces[cnt].name != NULL; cnt++) {
memset(&cursor, 0, sizeof(attrlist_cursor_t));
while (1) {
if (attr_list(xattr_data->last_fname, xattrbuf, ATTR_MAX_VALUELEN,
xattr_naming_spaces[cnt].flags, &cursor) != 0) {
BErrNo be;
switch (errno) {
case ENOENT:
retval = bxattr_exit_ok;
goto bail_out;
default:
Mmsg2(jcr->errmsg,
_("attr_list error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "attr_list error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
goto bail_out;
}
}
attrlist = (attrlist_t *)xattrbuf;
/*
* Walk the available attributes.
*/
for (cnt = 0; cnt < attrlist->al_count; cnt++) {
attrlist_ent = ATTR_ENTRY(xattrbuf, cnt);
/*
* First determine if we can retrieve the xattr and how big it really is.
*/
length = sizeof(dummy);
if (attr_get(xattr_data->last_fname, attrlist_ent->a_name, dummy,
&length, xattr_naming_spaces[cnt].flags) != 0) {
BErrNo be;
switch (errno) {
case ENOENT:
case ENOATTR:
retval = bxattr_exit_ok;
goto bail_out;
case E2BIG:
/*
* Size of the xattr is bigger then the 32 bytes dummy which is
* likely. As length now contains its actual length we can allocate
* a properly size buffer for the real retrieval.
*/
break;
default:
Mmsg2(jcr->errmsg,
_("attr_list error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "attr_list error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
goto bail_out;
}
}
/*
* Each xattr valuepair starts with a magic so we can parse it easier.
*/
current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
current_xattr->magic = XATTR_MAGIC;
expected_serialize_len += sizeof(current_xattr->magic);
/*
* Allocate space for storing the name.
* We store the name as <naming_space_name><xattr_name>
*/
current_xattr->name_length = strlen(xattr_naming_spaces[cnt].name) +
strlen(attrlist_ent->a_name) + 1;
current_xattr->name = (char *)malloc(current_xattr->name_length);
Bsnprintf(current_xattr->name, current_xattr->name_length, "%s%s",
xattr_naming_spaces[cnt].name, attrlist_ent->a_name);
expected_serialize_len += sizeof(current_xattr->name_length) +
current_xattr->name_length;
current_xattr->value_length = length;
current_xattr->value = (char *)malloc(current_xattr->value_length);
/*
* Retrieve the actual value of the xattr.
*/
if (attr_get(xattr_data->last_fname, attrlist_ent->a_name, current_xattr->value,
&length, xattr_naming_spaces[cnt].flags) != 0) {
BErrNo be;
switch (errno) {
case ENOENT:
case ENOATTR:
retval = bxattr_exit_ok;
break;
case E2BIG:
/*
* The buffer for the xattr isn't big enough. the value of
* current_xattr->value_length is updated with the actual size
* of the xattr. So we free the old buffer and create a new one
* and try again. Normally this cannot happen as we size the
* buffer using a call to attr_get before but in case of an
* race condition it might happen.
*/
free(current_xattr->value);
current_xattr->value = (char *)malloc(length);
if (attr_get(xattr_data->last_fname, attrlist_ent->a_name, current_xattr->value,
&length, xattr_naming_spaces[cnt].flags) != 0) {
switch (errno) {
case ENOENT:
case ENOATTR:
retval = bxattr_exit_ok;
break;
default:
Mmsg2(jcr->errmsg,
_("attr_list error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror(errno));
Dmsg2(100, "attr_list error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
break;
}
} else {
goto ok_continue;
}
break;
default:
Mmsg2(jcr->errmsg,
_("attr_list error on file \"%s\": ERR=%s\n"),
xattr_data->last_fname, be.bstrerror());
Dmsg2(100, "attr_list error file=%s ERR=%s\n",
xattr_data->last_fname, be.bstrerror());
break;
}
/*
* Default failure path out when retrieval of attr fails.
*/
free(current_xattr->value);
free(current_xattr->name);
free(current_xattr);
goto bail_out;
}
ok_continue:
current_xattr->value_length = length;
expected_serialize_len += sizeof(current_xattr->value_length) +
current_xattr->value_length;
if (xattr_value_list == NULL) {
xattr_value_list = New(alist(10, not_owned_by_alist));
}
xattr_value_list->append(current_xattr);
xattr_count++;
/*
* Protect ourself against things getting out of hand.
*/
if (expected_serialize_len >= MAX_XATTR_STREAM) {
Mmsg2(jcr->errmsg,
_("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
xattr_data->last_fname, MAX_XATTR_STREAM);
goto bail_out;
}
}
/*
* See if there are more attributes available for a next run of attr_list.
*/
if (attrlist->al_more == 0) {
break;
}
}
}
/*
* If we found any xattr send them to the SD.
*/
if (xattr_count > 0) {
/*
* Serialize the datastream.
*/
if (SerializeXattrStream(jcr,
xattr_data,
expected_serialize_len,
xattr_value_list) < expected_serialize_len) {
Mmsg1(jcr->errmsg,
_("Failed to Serialize extended attributes on file \"%s\"\n"),
xattr_data->last_fname);
Dmsg1(100, "Failed to Serialize extended attributes on file \"%s\"\n",
xattr_data->last_fname);
goto bail_out;
}
/*
* Send the datastream to the SD.
*/
retval = SendXattrStream(jcr, xattr_data, os_default_xattr_streams[0]);
} else {
retval = bxattr_exit_ok;
}
bail_out:
FreePoolMemory(xattrbuf);
if (xattr_value_list != NULL) {
XattrDropInternalTable(xattr_value_list);
}
return retval;
}
static bxattr_exit_code irix_parse_xattr_streams(JobControlRecord *jcr,
xattr_data_t *xattr_data,
int stream,
char *content,
uint32_t content_length)
{
char *bp;
int cnt, cmp_size, name_space_index, flags;
xattr_t *current_xattr;
alist *xattr_value_list;
bxattr_exit_code retval = bxattr_exit_error;
xattr_value_list = New(alist(10, not_owned_by_alist));
if (UnSerializeXattrStream(jcr,
xattr_data,
content,
content_length,
xattr_value_list) != bxattr_exit_ok) {
goto bail_out;
}
foreach_alist(current_xattr, xattr_value_list) {
/*
* See to what namingspace this xattr belongs to.
*/
name_space_index = 0;
for (cnt = 0; xattr_naming_spaces[cnt].name != NULL; cnt++) {
cmp_size = strlen(xattr_naming_spaces[cnt].name);
if (bstrncasecmp(current_xattr->name,
xattr_naming_spaces[cnt].name,
cmp_size)) {
name_space_index = cnt;
break;
}
}
/*
* If we got a xattr that doesn't belong to an valid namespace complain.
*/
if (name_space_index == 0) {
Mmsg2(jcr->errmsg,
_("Received illegal xattr named %s on file \"%s\"\n"),
current_xattr->name, xattr_data->last_fname);
Dmsg2(100, "Received illegal xattr named %s on file \"%s\"\n",
current_xattr->name, xattr_data->last_fname);
goto bail_out;
}
/*
* Restore the xattr first try to create the attribute from scratch.
*/
flags = xattr_naming_spaces[name_space_index].flags | ATTR_CREATE;