2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
32 #include <libxml/xmlwriter.h>
36 #define _(msgid) gettext(msgid)
39 #include "pwmd-error.h"
40 #include "util-misc.h"
45 extern void log_write (const char *fmt
, ...);
48 * 'element' must be allocated.
51 is_literal_element (char **element
)
55 if (!element
|| !*element
)
58 if (*(*element
) == '!')
62 for (p
= *element
, c
= p
+ 1; *c
; c
++)
73 valid_xml_element (xmlChar
* element
)
75 char *p
= (char *) element
;
77 if (!element
|| !*element
|| *element
== '!')
90 valid_element_path (char **path
, int with_content
)
92 char **dup
= NULL
, **p
;
98 /* Save some memory by not duplicating the element content. */
101 t
= strv_length (path
);
102 for (i
= 0; i
< t
- 1; i
++)
104 char **tmp
= xrealloc (dup
, (i
+ 2) * sizeof (char *));
113 dup
[i
] = str_dup (path
[i
]);
118 dup
= strv_dup (path
);
123 for (p
= dup
; *p
&& *(*p
); p
++)
125 is_literal_element (&(*p
));
126 if (!valid_xml_element ((xmlChar
*) * p
))
138 attr_ctime (xmlNodePtr n
)
140 char *buf
= str_asprintf ("%li", time (NULL
));
144 return GPG_ERR_ENOMEM
;
146 rc
= add_attribute (n
, "_ctime", buf
);
152 create_new_element (xmlNodePtr parent
, const char *name
, xmlNodePtr
* result
)
154 xmlNodePtr n
= xmlNewNode (NULL
, (xmlChar
*) "element");
158 return GPG_ERR_ENOMEM
;
160 rc
= add_attribute (n
, "_name", name
);
167 *result
= xmlAddChild (parent
, n
);
169 (void) xmlAddChild (parent
, n
);
178 new_root_element (xmlDocPtr doc
, char *name
)
180 xmlNodePtr root
= xmlDocGetRootElement (doc
);
184 return GPG_ERR_BAD_DATA
;
189 if (!valid_xml_element ((xmlChar
*) p
))
190 return GPG_ERR_INV_VALUE
;
192 return create_new_element (root
, p
, NULL
);
199 xmlTextWriterPtr wr
= xmlNewTextWriterDoc (&doc
, 0);
204 if (xmlTextWriterStartDocument (wr
, NULL
, NULL
, NULL
))
207 if (xmlTextWriterStartDTD (wr
, (xmlChar
*) "pwmd", NULL
, NULL
) == -1)
210 if (xmlTextWriterWriteDTDElement (wr
, (xmlChar
*) "pwmd",
211 (xmlChar
*) "(element)") == -1)
214 xmlTextWriterEndDTDElement (wr
);
216 if (xmlTextWriterWriteDTDAttlist (wr
, (xmlChar
*) "element",
217 (xmlChar
*) "_name CDATA #REQUIRED") ==
221 xmlTextWriterEndDTDAttlist (wr
);
222 xmlTextWriterEndDTD (wr
);
224 if (xmlTextWriterStartElement (wr
, (xmlChar
*) "pwmd"))
227 xmlTextWriterEndElement (wr
);
228 xmlTextWriterEndDocument (wr
);
229 xmlFreeTextWriter (wr
);
233 xmlTextWriterEndDocument (wr
);
234 xmlFreeTextWriter (wr
);
242 return create_dtd ();
246 find_element_node (xmlNodePtr node
)
250 if (n
&& n
->type
== XML_ELEMENT_NODE
)
253 for (n
= node
; n
; n
= n
->next
)
255 if (n
->type
== XML_ELEMENT_NODE
)
263 resolve_path (xmlDocPtr doc
, xmlChar
* path
, char ***result
, gpg_error_t
* rc
)
268 req
= str_split ((char *) path
, "\t", 0);
271 *rc
= GPG_ERR_ENOMEM
;
275 n
= find_root_element (doc
, &req
, rc
, NULL
, 0, 0);
283 n
= find_elements (doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
, NULL
,
295 * Lists root element names; the value of the attribute "_name" of an element
296 * "element". If there's a target attribute both literal and non-literal
297 * element names will be added. This is the primary reason why XML entities
298 * cannot be used. There wouldn't be a way to get the literal an non-literal
302 list_root_elements (xmlDocPtr doc
, struct string_s
** result
,
303 int verbose
, int with_target
)
306 struct slist_s
*list
= NULL
;
308 struct string_s
*string
;
311 n
= xmlDocGetRootElement (doc
);
312 if (!n
|| !n
->children
)
313 return GPG_ERR_NO_DATA
;
315 for (n
= n
->children
; n
; n
= n
->next
)
318 xmlChar
*val
, *target
;
319 struct slist_s
*tlist
;
322 if (n
->type
!= XML_ELEMENT_NODE
)
325 a
= xmlHasProp (n
, (xmlChar
*) "_name");
326 if (!a
|| !a
->children
->content
)
329 val
= xmlNodeGetContent (a
->children
);
337 str_asprintf ("!%s%s", (char *) val
,
338 verbose
? find_element_node (n
->children
) ? " +" : "" :
348 tlist
= slist_append (list
, tmp
);
357 target
= node_has_attribute (n
, (xmlChar
*) "target");
365 xmlNodePtr tnode
= resolve_path (doc
, target
, &req
, &rc
);
367 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
)
369 t
= str_asprintf ("%s %s", (char *) val
,
370 rc
== GPG_ERR_ELOOP
? "O" : "E");
375 struct string_s
*realpath
= NULL
;
379 rc
= build_realpath (doc
, (char *) target
, &realpath
);
388 realpath
= string_prepend (realpath
, "T ");
391 t
= str_asprintf ("%s%s%s%s", (char *) val
,
393 && find_element_node (tnode
->children
))
394 || realpath
? " " : "", tnode
395 && find_element_node (tnode
->children
) ?
396 "+" : "", realpath
? realpath
->str
: "");
399 string_free (realpath
, 1);
406 t
= str_dup ((char *) val
);
412 rc
= rc
? rc
: GPG_ERR_ENOMEM
;
416 tlist
= slist_append (list
, t
);
433 total
= slist_length (list
);
435 return GPG_ERR_NO_DATA
;
437 string
= string_new (NULL
);
444 for (i
= 0; i
< total
; i
++)
446 char *val
= slist_nth_data (list
, i
);
448 string_append_printf (string
, "%s\n", val
);
451 string
= string_truncate (string
, string
->len
- 1);
455 total
= slist_length (list
);
456 for (i
= 0; i
< total
; i
++)
457 xfree (slist_nth_data (list
, i
));
464 * Prevents a sibling element past the current element path with the same
468 find_stop_node (xmlNodePtr node
)
472 for (n
= node
->parent
->children
; n
; n
= n
->next
)
482 create_target_elements_cb (xmlNodePtr node
, char **path
,
483 gpg_error_t
* rc
, void *data
)
487 xmlNodePtr parent
= data
;
489 for (i
= 0; req
[i
] && *req
[i
]; i
++)
493 if (parent
&& node
== parent
)
495 *rc
= GPG_ERR_CONFLICT
;
499 is_literal_element (&req
[i
]);
501 if ((n
= find_element (node
, req
[i
], find_stop_node (node
))) == NULL
||
502 (n
&& n
->parent
== node
->parent
))
505 *rc
= create_new_element (node
, req
[i
], &node
);
517 find_text_node (xmlNodePtr node
)
521 if (n
&& n
->type
== XML_TEXT_NODE
)
524 for (n
= node
; n
; n
= n
->next
)
526 if (n
->type
== XML_TEXT_NODE
)
534 create_elements_cb (xmlNodePtr node
, char **elements
,
535 gpg_error_t
* rc
, void *data
)
538 char **req
= elements
;
540 if (node
->type
== XML_TEXT_NODE
)
543 for (i
= 0; req
[i
] && *req
[i
]; i
++)
548 * Strip the first '!' if needed. If there's another, it's an
549 * rc. The syntax has already been checked before calling this
552 is_literal_element (&req
[i
]);
553 n
= find_element (node
, req
[i
], find_stop_node (node
));
556 * If the found element has the same parent as the current element,
557 * they are siblings and the new element needs to be created as a
558 * child of the current element (node).
560 if (n
&& n
->parent
== node
->parent
)
565 *rc
= create_new_element (node
, req
[i
], &node
);
576 /* The root element is really req[0]. It is need as a pointer in case there is
577 * a target attribute so it can be updated. */
579 find_root_element (xmlDocPtr doc
, char ***req
, gpg_error_t
* rc
,
580 int *target
, int recursion_depth
, int stop
)
582 xmlNodePtr n
= xmlDocGetRootElement (doc
);
584 char *root
= str_dup (*req
[0]);
585 int literal
= is_literal_element (&root
);
589 *rc
= GPG_ERR_ENOMEM
;
596 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
598 xmlChar
*t
= xmlGetNodePath (n
);
600 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
609 if (n
->type
== XML_ELEMENT_NODE
)
611 if (depth
== 0 && xmlStrEqual (n
->name
, (xmlChar
*) "pwmd"))
618 if (depth
== 1 && xmlStrEqual (n
->name
, (xmlChar
*) "element"))
620 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "_name");
625 if (xmlStrEqual (content
, (xmlChar
*) root
))
627 char **nreq
, **tmp
= NULL
;
637 content
= node_has_attribute (n
, (xmlChar
*) "target");
642 if (!content
|| stop
)
651 if (strchr ((char *) content
, '\t'))
653 nreq
= str_split ((char *) content
, "\t", 0);
662 *rc
= GPG_ERR_ENOMEM
;
668 tmp
= strv_catv (nreq
, tmp
+ 1);
674 *rc
= GPG_ERR_ENOMEM
;
683 if (strv_printf (&tmp
, "%s", content
) == 0)
687 *rc
= GPG_ERR_ENOMEM
;
693 nreq
= strv_catv (tmp
, nreq
+ 1);
698 *rc
= GPG_ERR_ENOMEM
;
709 find_root_element (doc
, req
, rc
, target
, recursion_depth
,
722 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
727 find_element (xmlNodePtr node
, char *element
, xmlNodePtr stop
)
731 if (!node
|| !element
)
734 for (n
= node
; n
; n
= n
->next
)
736 if (n
->type
!= XML_ELEMENT_NODE
)
742 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
744 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
757 node_has_attribute (xmlNodePtr n
, xmlChar
* attr
)
759 xmlAttrPtr a
= xmlHasProp (n
, attr
);
764 if (!a
->children
|| !a
->children
->content
)
767 return xmlGetProp (n
, attr
);
771 element_to_literal (char **element
)
773 char *p
= str_asprintf ("!%s", *element
);
783 /* Resolves elements in 'req' one at a time. It's recursive in case of
784 * "target" attributes. */
786 find_elements (xmlDocPtr doc
, xmlNodePtr node
,
787 char **req
, gpg_error_t
* rc
, int *target
,
788 xmlNodePtr (*found_fn
) (xmlNodePtr
, char **, gpg_error_t
*,
790 xmlNodePtr (*not_found_fn
) (xmlNodePtr
, char **, gpg_error_t
*,
791 void *), int is_list_command
,
792 int recursion_depth
, void *data
, int stop
)
794 xmlNodePtr n
, last
, last_node
;
801 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
803 xmlChar
*t
= xmlGetNodePath (node
);
805 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
812 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++)
820 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
827 *rc
= GPG_ERR_ENOMEM
;
831 literal
= is_literal_element (&t
);
832 n
= find_element (last
, t
, NULL
);
838 return not_found_fn (found
? last_node
: last_node
->parent
, p
, rc
,
841 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
851 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "target");
852 char **nreq
= NULL
, **nnreq
;
856 if (is_list_command
== 1)
858 if (element_to_literal (&(*p
)) == 0)
860 *rc
= GPG_ERR_ENOMEM
;
871 if (!*(p
+ 1) && stop
)
877 if (strchr ((char *) content
, '\t') != NULL
)
879 if ((nreq
= str_split ((char *) content
, "\t", 0)) == NULL
)
882 *rc
= GPG_ERR_INV_VALUE
;
888 if ((nreq
= str_split ((char *) content
, " ", 0)) == NULL
)
891 *rc
= GPG_ERR_INV_VALUE
;
897 tmp
= find_root_element (doc
, &nreq
, rc
, target
, 0, 0);
907 found_fn (tmp
, nreq
, rc
, p
+ 1, data
);
916 if (!*(nreq
+ 1) && !*(p
+ 1))
922 nnreq
= strv_catv (nreq
+ 1, p
+ 1);
926 if (!nnreq
|| !*nnreq
)
935 n
= find_elements (doc
, tmp
->children
, nnreq
, rc
, NULL
, found_fn
,
936 not_found_fn
, is_list_command
, recursion_depth
,
943 return not_found_fn (tmp
, p
+ 1, rc
, data
);
945 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
951 char **zz
= p
+ 1, **qq
= nnreq
;
953 if (strv_length (nnreq
) > strv_length (p
+ 1))
956 for (; *qq
&& *zz
; zz
++)
959 *zz
= str_dup (*qq
++);
963 *rc
= GPG_ERR_ENOMEM
;
979 update_element_list (struct element_list_s
*elements
)
984 if (!elements
|| !elements
->elements
)
987 line
= strv_join ("\t", elements
->elements
);
992 strv_free (elements
->elements
);
993 elements
->elements
= NULL
;
994 l
= slist_append (elements
->list
, line
);
1004 path_list_recurse (xmlDocPtr doc
, xmlNodePtr node
,
1005 struct element_list_s
*elements
)
1009 gpg_error_t error_flag
= 0;
1011 for (n
= node
; n
; n
= n
->next
)
1013 xmlChar
*target
= NULL
;
1014 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
1019 if (n
->type
!= XML_ELEMENT_NODE
)
1022 if (elements
->verbose
)
1025 (&elements
->elements
, "%s\t!%s%s", elements
->prefix
, a
,
1026 find_element_node (n
->children
) ? " +" : "") == 0)
1029 return GPG_ERR_ENOMEM
;
1033 if (strv_printf (&elements
->elements
, "%s\t!%s", elements
->prefix
, a
)
1037 return GPG_ERR_ENOMEM
;
1040 if (update_element_list (elements
) == 0)
1043 return GPG_ERR_ENOMEM
;
1046 target
= node_has_attribute (n
, (xmlChar
*) "target");
1051 char *save
= elements
->prefix
;
1052 int r
= elements
->resolving
;
1055 struct string_s
*realpath
= NULL
;
1057 tnode
= resolve_path (doc
, target
, &req
, &rc
);
1058 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1060 if (rc
== GPG_ERR_ELOOP
)
1062 xmlChar
*t
= xmlGetNodePath (n
);
1064 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1068 if (elements
->verbose
)
1070 strv_printf (&elements
->elements
, "%s\t%s %s",
1071 elements
->prefix
, a
,
1072 rc
== GPG_ERR_ELOOP
? "O" : "E");
1079 if (!elements
->verbose
&& rc
)
1086 if (elements
->with_target
)
1088 rc
= build_realpath (doc
, (char *) target
, &realpath
);
1096 realpath
= string_prepend (realpath
, "T ");
1099 if (elements
->verbose
)
1101 if (!strv_printf (&elements
->elements
, "%s\t%s%s%s%s",
1102 elements
->prefix
, a
,
1103 (tnode
&& find_element_node (tnode
->children
))
1104 || realpath
? " " : "", tnode
1105 && find_element_node (tnode
->children
) ? "+" :
1106 "", realpath
? realpath
->str
: ""))
1110 return GPG_ERR_ENOMEM
;
1115 (&elements
->elements
, "%s\t%s", elements
->prefix
, a
))
1119 return GPG_ERR_ENOMEM
;
1124 string_free (realpath
, 1);
1126 tmp
= strv_join ("\t", elements
->elements
);
1132 return GPG_ERR_ENOMEM
;
1135 if (update_element_list (elements
) == 0)
1140 return GPG_ERR_ENOMEM
;
1143 if (!error_flag
&& elements
->recurse
)
1145 /* Prune element flags. */
1146 if (elements
->verbose
&& strchr (tmp
, ' '))
1150 for (p
= tmp
; *p
; p
++)
1160 elements
->prefix
= tmp
;
1161 elements
->resolving
= 1;
1162 rc
= create_path_list (doc
, elements
, (char *) target
);
1163 elements
->resolving
= r
;
1164 elements
->prefix
= save
;
1180 if (n
->children
&& elements
->recurse
&& error_flag
!= GPG_ERR_ELOOP
)
1182 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1183 char *save
= elements
->prefix
;
1188 return GPG_ERR_ENOMEM
;
1191 elements
->prefix
= tmp
;
1192 rc
= path_list_recurse (doc
, n
->children
, elements
);
1193 xfree (elements
->prefix
);
1194 elements
->prefix
= save
;
1206 return error_flag
== GPG_ERR_ELOOP
? error_flag
: rc
;
1210 add_attribute (xmlNodePtr node
, const char *name
, const char *value
)
1215 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1216 return GPG_ERR_BAD_DATA
;
1218 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1221 buf
= str_asprintf ("%li", time (NULL
));
1222 rc
= add_attribute (node
, "_mtime", buf
);
1228 * From the element path 'path', find sub-nodes and append them to the list.
1231 create_path_list (xmlDocPtr doc
, struct element_list_s
* elements
, char *path
)
1234 char **req
, **req_orig
;
1238 req
= str_split (path
, "\t", 0);
1241 req
= str_split (path
, " ", 0);
1243 return GPG_ERR_SYNTAX
;
1246 req_orig
= strv_dup (req
);
1249 rc
= GPG_ERR_ENOMEM
;
1253 n
= find_root_element (doc
, &req
, &rc
, &a_target
, 0, 0);
1254 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
)
1255 && elements
->verbose
&& a_target
)
1261 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1272 *req
= str_dup (*req_orig
);
1280 find_elements (doc
, n
->children
, req
+ 1, &rc
, &e_target
, NULL
, NULL
,
1283 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1293 if (!elements
->prefix
)
1298 * If any req_orig element contains no target the element should be
1299 * prefixed with the literal character. Not really crucial if the
1300 * client isn't human because child elements are prefixed for the
1301 * current path. But may be confusing if editing by hand.
1303 elements
->prefix
= strv_join ("\t", req_orig
);
1305 if (!elements
->prefix
)
1307 rc
= GPG_ERR_ENOMEM
;
1311 if (elements
->verbose
)
1314 struct string_s
*realpath
= NULL
;
1316 if (a_target
&& elements
->with_target
)
1318 rc
= build_realpath (doc
, path
, &realpath
);
1322 realpath
= string_prepend (realpath
, "T ");
1325 ret
= strv_printf (&elements
->elements
, "%s%s%s%s",
1327 find_element_node (n
->children
)
1328 || realpath
? " " : "",
1329 find_element_node (n
->children
) ? "+" : "",
1330 realpath
? realpath
->str
: "");
1331 string_free (realpath
, 1);
1334 rc
= GPG_ERR_ENOMEM
;
1338 else if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1340 rc
= GPG_ERR_ENOMEM
;
1344 if (update_element_list (elements
) == 0)
1346 rc
= GPG_ERR_ENOMEM
;
1351 rc
= path_list_recurse (doc
, n
->children
, elements
);
1355 strv_free (req_orig
);
1362 recurse_xpath_nodeset (xmlDocPtr doc
, xmlNodeSetPtr nodes
,
1363 xmlChar
* value
, xmlBufferPtr
* result
, int cmd
,
1364 const xmlChar
* attr
)
1366 int i
= value
? nodes
->nodeNr
- 1 : 0;
1369 buf
= xmlBufferCreate ();
1372 return GPG_ERR_ENOMEM
;
1374 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1376 xmlNodePtr n
= nodes
->nodeTab
[i
];
1382 if (!value
&& !attr
)
1384 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1387 return GPG_ERR_BAD_DATA
;
1395 xmlNodeSetContent (n
, value
);
1396 rc
= update_element_mtime (n
);
1404 rc
= add_attribute (n
, (char *) attr
, (char *) value
);
1406 rc
= delete_attribute (n
, attr
);
1418 convert_root_element (xmlNodePtr n
)
1420 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1426 xmlChar
*t
= xmlGetNodePath (n
);
1429 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1432 return GPG_ERR_AMBIGUOUS_NAME
;
1435 a
= xmlGetProp (n
, (xmlChar
*) "name");
1439 rc
= add_attribute (n
, "_name", (char *) a
);
1445 rc
= delete_attribute (n
, (xmlChar
*) "name");
1450 xmlNodeSetName (n
, (xmlChar
*) "element");
1457 delete_attribute (xmlNodePtr n
, const xmlChar
* name
)
1461 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1462 return GPG_ERR_NOT_FOUND
;
1464 if (xmlRemoveProp (a
) == -1)
1465 return GPG_ERR_BAD_DATA
;
1467 return update_element_mtime (n
);
1471 convert_elements_recurse (xmlDocPtr doc
, xmlNodePtr n
, unsigned depth
)
1477 for (n
= n
->children
; n
; n
= n
->next
)
1479 if (n
->type
== XML_ELEMENT_NODE
)
1485 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1487 xmlChar
*t
= xmlGetNodePath (n
);
1490 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1493 return GPG_ERR_AMBIGUOUS_NAME
;
1496 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1501 xmlChar
*t
= xmlGetNodePath (n
);
1504 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1507 return GPG_ERR_AMBIGUOUS_NAME
;
1510 xmlChar
*tmp
= xmlStrdup (n
->name
);
1513 return GPG_ERR_ENOMEM
;
1515 xmlNodeSetName (n
, (xmlChar
*) "element");
1516 rc
= add_attribute (n
, "_name", (char *) tmp
);
1524 rc
= convert_root_element (n
);
1533 rc
= convert_elements_recurse (doc
, n
, depth
);
1543 /* Renames ALL elements to the new "element" name. Existing element names are
1544 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1545 * elements can contain common characters that the XML parser barfs on (an
1546 * email address for example. */
1548 convert_pre_212_elements (xmlDocPtr doc
)
1550 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1552 log_write (_("Converting pre 2.12 data file..."));
1553 return convert_elements_recurse (doc
, n
, 0);
1557 validate_import (xmlNodePtr node
)
1564 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1566 if (n
->type
== XML_ELEMENT_NODE
)
1568 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1570 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1574 xmlChar
*t
= xmlGetNodePath (n
);
1576 log_write (_("Missing attribute '_name' at %s."), t
);
1578 return GPG_ERR_INV_VALUE
;
1581 if (!valid_xml_element (a
))
1583 xmlChar
*t
= xmlGetNodePath (n
);
1585 log_write (_("'%s' is not a valid element name at %s."), a
,
1589 return GPG_ERR_INV_VALUE
;
1593 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1598 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1600 update_element_mtime (n
);
1605 xmlChar
*t
= xmlGetNodePath (n
);
1607 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1616 rc
= validate_import (n
->children
);
1627 update_element_mtime (xmlNodePtr n
)
1629 return add_attribute (n
, NULL
, NULL
);
1633 unlink_node (xmlNodePtr n
)
1641 rc
= update_element_mtime (n
->parent
);
1648 parse_doc (const char *xml
, size_t len
)
1650 return xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1654 realpath_elements_cb (xmlNodePtr node
, char **target
,
1655 gpg_error_t
* rc
, char **req_orig
, void *data
)
1657 char *path
= *(char **) data
;
1658 char *tmp
= NULL
, *result
;
1663 *(char **) data
= NULL
;
1666 path
= strv_join ("\t", target
);
1670 *rc
= GPG_ERR_ENOMEM
;
1676 tmp
= strv_join ("\t", req_orig
);
1681 *rc
= GPG_ERR_ENOMEM
;
1687 result
= str_asprintf ("%s\t%s", path
, tmp
);
1689 result
= str_dup (path
);
1693 *rc
= GPG_ERR_ENOMEM
;
1701 *(char **) data
= result
;
1706 build_realpath (xmlDocPtr doc
, char *line
, struct string_s
** result
)
1713 struct string_s
*string
;
1716 if (strchr (line
, '\t') != NULL
)
1718 if ((req
= str_split (line
, "\t", 0)) == NULL
)
1719 return GPG_ERR_SYNTAX
;
1723 if ((req
= str_split (line
, " ", 0)) == NULL
)
1724 return GPG_ERR_SYNTAX
;
1727 n
= find_root_element (doc
, &req
, &rc
, NULL
, 0, 0);
1734 rp
= strv_join ("\t", req
);
1738 return GPG_ERR_ENOMEM
;
1743 n
= find_elements (doc
, n
->children
, req
+ 1, &rc
,
1744 NULL
, realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
1753 string
= string_new (rp
);
1757 return GPG_ERR_ENOMEM
;
1760 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
1762 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
1764 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
1768 string_free (string
, 1);
1769 return GPG_ERR_ENOMEM
;
1783 node_to_element_path (xmlNodePtr node
)
1786 struct string_s
*str
= string_new ("");
1789 for (n
= node
; n
; n
= n
->parent
)
1793 for (child
= n
; child
; child
= child
->next
)
1795 if (child
->type
!= XML_ELEMENT_NODE
)
1798 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
1801 str
= string_prepend (str
, (char *) name
);
1803 name
= node_has_attribute (n
, (xmlChar
*) "target");
1805 str
= string_prepend (str
, "\t");
1807 str
= string_prepend (str
, "\t!");
1814 str
= string_erase (str
, 0, 1);
1816 string_free (str
, 0);
1822 * Recurse the element tree beginning at 'node' and find elements who point
1823 * back to 'src' or 'dst'. Also follows target attributes.
1826 find_child_to_target (xmlDocPtr doc
, xmlNodePtr node
,
1827 xmlNodePtr src
, xmlNodePtr dst
, unsigned depth
)
1832 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
1833 return gpg_error (GPG_ERR_ELOOP
);
1835 for (n
= node
; n
; n
= n
->next
)
1839 if (n
->type
!= XML_ELEMENT_NODE
)
1842 if (n
== src
|| n
== dst
)
1843 return GPG_ERR_ELOOP
;
1845 target
= node_has_attribute (n
, (xmlChar
*) "target");
1849 char **result
= NULL
;
1851 tmp
= resolve_path (doc
, target
, &result
, &rc
);
1856 rc
= find_child_to_target (doc
, tmp
, src
, dst
, ++depth
);
1864 rc
= find_child_to_target (doc
, n
->children
, src
, dst
, ++depth
);
1874 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
1879 for (n
= src
; n
; n
= n
->next
)
1881 if (n
->type
!= XML_ELEMENT_NODE
)
1890 rc
= find_child_of_parent (doc
, n
->children
, dst
);
1897 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
1902 for (n
= node
; n
; n
= n
->parent
)
1904 if (n
->type
!= XML_ELEMENT_NODE
)
1908 for (tmp
= n
->next
; tmp
; n
= n
->next
)
1910 if (n
->type
!= XML_ELEMENT_NODE
)
1914 return GPG_ERR_ELOOP
;
1919 return GPG_ERR_ELOOP
;
1926 validate_target_attribute (xmlDocPtr doc
, const char *src
,
1927 xmlNodePtr dst_node
)
1930 xmlNodePtr src_node
;
1931 char **src_req
= NULL
;
1933 src_node
= resolve_path (doc
, (xmlChar
*) src
, &src_req
, &rc
);
1937 /* A destination element is a child of the source element. */
1938 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
1942 /* The destination element is a parent of the source element. */
1943 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
1947 /* A destination child element contains a target to the source element. */
1948 rc
= find_child_to_target (doc
, dst_node
->children
, src_node
, dst_node
, 0);
1953 strv_free (src_req
);