+ ser_uint32(current_xattr->magic);
+ ser_uint32(current_xattr->name_length);
+ ser_bytes(current_xattr->name, current_xattr->name_length);
+
+ ser_uint32(current_xattr->value_length);
+ if (current_xattr->value_length > 0 && current_xattr->value) {
+ ser_bytes(current_xattr->value, current_xattr->value_length);
+
+ Dmsg3(100, "Backup xattr named %s, value %*s\n",
+ current_xattr->name, current_xattr->value, current_xattr->value);
+ } else {
+ Dmsg1(100, "Backup empty xattr named %s\n", current_xattr->name);
+ }
+ }
+
+ ser_end(jcr->xattr_data->content, expected_serialize_len + 10);
+ jcr->xattr_data->content_length = ser_length(jcr->xattr_data->content);
+
+ return jcr->xattr_data->content_length;
+}
+
+static bxattr_exit_code unserialize_xattr_stream(JCR *jcr, alist *xattr_value_list)
+{
+ unser_declare;
+ xattr_t *current_xattr;
+ bxattr_exit_code retval = bxattr_exit_ok;
+
+ /**
+ * 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.
+ */
+ unser_begin(jcr->xattr_data->content, jcr->xattr_data->content_length);
+ while (unser_length(jcr->xattr_data->content) < jcr->xattr_data->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"),
+ jcr->last_fname);
+ Dmsg1(100, "Illegal xattr stream, no XATTR_MAGIC on file \"%s\"\n",
+ jcr->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"),
+ jcr->last_fname);
+ Dmsg1(100, "Illegal xattr stream, xattr name length <= 0 on file \"%s\"\n",
+ jcr->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);
+ unser_bytes(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);
+ unser_bytes(current_xattr->value, current_xattr->value_length);
+
+ Dmsg3(100, "Restoring xattr named %s, value %*s\n",
+ current_xattr->name, current_xattr->value, 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);
+ }
+
+ unser_end(jcr->xattr_data->content, jcr->xattr_data->content_length);
+ return retval;
+}
+#endif
+
+/*
+ * 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_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
+{
+ bool skip_xattr;
+ char *xattr_list, *bp;
+ 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 = NULL;
+ alist *xattr_value_list = NULL;
+ bxattr_exit_code retval = bxattr_exit_error;
+ berrno be;
+
+ /*
+ * First get the length of the available list with extended attributes.
+ */
+ xattr_list_len = llistea(jcr->last_fname, NULL, 0);
+ switch (xattr_list_len) {
+ case -1:
+ switch (errno) {
+ case ENOENT:
+ case EFORMAT:
+ case ENOTSUP:
+ return bxattr_exit_ok;
+ default:
+ Mmsg2(jcr->errmsg, _("llistea error on file \"%s\": ERR=%s\n"),
+ jcr->last_fname, be.bstrerror());
+ Dmsg2(100, "llistea error file=%s ERR=%s\n",
+ jcr->last_fname, be.bstrerror());
+ return bxattr_exit_error;
+ }
+ break;
+ case 0:
+ return bxattr_exit_ok;
+ default:
+ break;
+ }
+
+ /*
+ * Allocate room for the extented attribute list.
+ */
+ xattr_list = (char *)malloc(xattr_list_len + 1);
+ memset((caddr_t)xattr_list, 0, xattr_list_len + 1);
+
+ /*
+ * Get the actual list of extended attributes names for a file.
+ */
+ xattr_list_len = llistea(jcr->last_fname, xattr_list, xattr_list_len);
+ switch (xattr_list_len) {
+ case -1:
+ switch (errno) {
+ case ENOENT:
+ case EFORMAT:
+ case ENOTSUP:
+ retval = bxattr_exit_ok;
+ goto bail_out;
+ default:
+ Mmsg2(jcr->errmsg, _("llistea error on file \"%s\": ERR=%s\n"),
+ jcr->last_fname, be.bstrerror());
+ Dmsg2(100, "llistea error file=%s ERR=%s\n",
+ jcr->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.
+ */
+ bp = xattr_list;
+ while ((bp - xattr_list) + 1 < xattr_list_len) {
+ 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);
+ bp = strchr(bp, '\0') + 1;
+ continue;
+ }
+
+ /*
+ * 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((caddr_t)current_xattr->name, (caddr_t)bp, current_xattr->name_length);
+
+ expected_serialize_len += sizeof(current_xattr->name_length) + current_xattr->name_length;
+
+ /*
+ * First see how long the value is for the extended attribute.
+ */
+ xattr_value_len = lgetea(jcr->last_fname, bp, NULL, 0);
+ switch (xattr_value_len) {
+ case -1:
+ switch (errno) {
+ case ENOENT:
+ case EFORMAT:
+ case ENOTSUP:
+ retval = bxattr_exit_ok;
+ goto bail_out;
+ default:
+ Mmsg2(jcr->errmsg, _("lgetea error on file \"%s\": ERR=%s\n"),
+ jcr->last_fname, be.bstrerror());
+ Dmsg2(100, "lgetea error file=%s ERR=%s\n",
+ jcr->last_fname, be.bstrerror());
+ goto bail_out;
+ }
+ break;
+ 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((caddr_t)current_xattr->value, 0, xattr_value_len);
+
+ xattr_value_len = lgetea(jcr->last_fname, bp, current_xattr->value, xattr_value_len);
+ if (xattr_value_len < 0) {
+ switch (errno) {
+ case ENOENT:
+ case EFORMAT:
+ case ENOTSUP:
+ retval = bxattr_exit_ok;
+ goto bail_out;
+ default:
+ Mmsg2(jcr->errmsg, _("lgetea error on file \"%s\": ERR=%s\n"),
+ jcr->last_fname, be.bstrerror());
+ Dmsg2(100, "lgetea error file=%s ERR=%s\n",
+ jcr->last_fname, be.bstrerror());
+ 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;
+
+ /*
+ * 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"),
+ jcr->last_fname, MAX_XATTR_STREAM);
+ goto bail_out;
+ }
+ }
+
+ if (xattr_value_list == NULL) {
+ xattr_value_list = New(alist(10, not_owned_by_alist));
+ }
+
+ xattr_value_list->append(current_xattr);
+ current_xattr = NULL;
+ xattr_count++;
+ bp = strchr(bp, '\0') + 1;
+ break;
+ }
+
+ 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 (serialize_xattr_stream(jcr, expected_serialize_len, xattr_value_list) < expected_serialize_len) {
+ Mmsg1(jcr->errmsg, _("Failed to serialize extended attributes on file \"%s\"\n"),
+ jcr->last_fname);
+ Dmsg1(100, "Failed to serialize extended attributes on file \"%s\"\n",
+ jcr->last_fname);
+ goto bail_out;
+ }
+
+ /*
+ * Send the datastream to the SD.
+ */
+ retval = send_xattr_stream(jcr, os_default_xattr_streams[0]);
+ } else {
+ retval = bxattr_exit_ok;
+ }
+
+bail_out:
+ if (current_xattr != NULL) {
+ if (current_xattr->value != NULL) {
+ free(current_xattr->value);
+ }
+ if (current_xattr->name != NULL) {
+ free(current_xattr->name);
+ }
+ free(current_xattr);
+ }
+ if (xattr_list != NULL) {
+ free(xattr_list);
+ }
+ if (xattr_value_list != NULL) {
+ xattr_drop_internal_table(xattr_value_list);
+ }
+ return retval;
+}
+
+static bxattr_exit_code aix_xattr_parse_streams(JCR *jcr, int stream)
+{
+ xattr_t *current_xattr;
+ alist *xattr_value_list;
+ berrno be;
+
+ xattr_value_list = New(alist(10, not_owned_by_alist));
+
+ if (unserialize_xattr_stream(jcr, xattr_value_list) != bxattr_exit_ok) {
+ xattr_drop_internal_table(xattr_value_list);
+ return bxattr_exit_error;
+ }
+
+ foreach_alist(current_xattr, xattr_value_list) {
+ if (lsetea(jcr->last_fname, current_xattr->name, current_xattr->value, current_xattr->value_length, 0) != 0) {
+ switch (errno) {
+ case ENOENT:
+ case EFORMAT:
+ case ENOTSUP:
+ goto bail_out;
+ default:
+ Mmsg2(jcr->errmsg, _("lsetea error on file \"%s\": ERR=%s\n"),
+ jcr->last_fname, be.bstrerror());
+ Dmsg2(100, "lsetea error file=%s ERR=%s\n",
+ jcr->last_fname, be.bstrerror());
+ goto bail_out;
+ }
+ }
+ }
+
+ xattr_drop_internal_table(xattr_value_list);
+ return bxattr_exit_ok;
+
+bail_out:
+ xattr_drop_internal_table(xattr_value_list);
+ return bxattr_exit_error;
+}
+
+/*
+ * Function pointers to the build and parse function to use for these xattrs.
+ */
+static bxattr_exit_code (*os_build_xattr_streams)(JCR *jcr, FF_PKT *ff_pkt) = aix_xattr_build_streams;
+static bxattr_exit_code (*os_parse_xattr_streams)(JCR *jcr, int stream) = aix_xattr_parse_streams;
+
+#elif defined(HAVE_IRIX_OS)
+
+/*
+ * 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_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
+{
+ int cnt, xattr_count = 0;
+ attrlist_cursor_t cursor;
+ attrlist_t *attrlist;
+ attrlist_ent_t *attrlist_ent;
+ xattr_t *current_xattr = NULL;
+ alist *xattr_value_list = NULL;
+ uint32_t expected_serialize_len = 0;
+ bxattr_exit_code retval = bxattr_exit_error;
+ POOLMEM *xattrbuf = get_memory(ATTR_MAX_VALUELEN);
+ berrno be;
+
+ for (cnt = 0; xattr_naming_spaces[cnt].name != NULL; cnt++) {
+ memset(cursor, 0, sizeof(attrlist_cursor_t));
+ while (1) {
+ if (attr_list(jcr->last_fname, xattrbuf, ATTR_MAX_VALUELEN,
+ xattr_naming_spaces[cnt].flags, &cursor) != 0) {
+ switch (errno) {
+ case ENOENT:
+ retval = bxattr_exit_ok;
+ goto bail_out;
+ default:
+ Mmsg2(jcr->errmsg, _("attr_list error on file \"%s\": ERR=%s\n"),
+ jcr->last_fname, be.bstrerror());
+ Dmsg2(100, "attr_list error file=%s ERR=%s\n",
+ jcr->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);
+
+ /*
+ * 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);