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
);
1164 elements
->resolving
= r
;
1166 elements
->prefix
= save
;
1177 if (n
->children
&& elements
->recurse
&& error_flag
!= GPG_ERR_ELOOP
)
1179 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1180 char *save
= elements
->prefix
;
1185 return GPG_ERR_ENOMEM
;
1188 elements
->prefix
= tmp
;
1189 rc
= path_list_recurse (doc
, n
->children
, elements
);
1190 xfree (elements
->prefix
);
1191 elements
->prefix
= save
;
1203 return error_flag
== GPG_ERR_ELOOP
? error_flag
: rc
;
1207 add_attribute (xmlNodePtr node
, const char *name
, const char *value
)
1212 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1213 return GPG_ERR_BAD_DATA
;
1215 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1218 buf
= str_asprintf ("%li", time (NULL
));
1219 rc
= add_attribute (node
, "_mtime", buf
);
1225 * From the element path 'path', find sub-nodes and append them to the list.
1228 create_path_list (xmlDocPtr doc
, struct element_list_s
* elements
, char *path
)
1231 char **req
, **req_orig
;
1235 req
= str_split (path
, "\t", 0);
1238 req
= str_split (path
, " ", 0);
1240 return GPG_ERR_SYNTAX
;
1243 req_orig
= strv_dup (req
);
1246 rc
= GPG_ERR_ENOMEM
;
1250 n
= find_root_element (doc
, &req
, &rc
, &a_target
, 0, 0);
1251 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
)
1252 && elements
->verbose
&& a_target
)
1258 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1269 *req
= str_dup (*req_orig
);
1277 find_elements (doc
, n
->children
, req
+ 1, &rc
, &e_target
, NULL
, NULL
,
1280 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1290 if (!elements
->prefix
)
1295 * If any req_orig element contains no target the element should be
1296 * prefixed with the literal character. Not really crucial if the
1297 * client isn't human because child elements are prefixed for the
1298 * current path. But may be confusing if editing by hand.
1300 elements
->prefix
= strv_join ("\t", req_orig
);
1302 if (!elements
->prefix
)
1304 rc
= GPG_ERR_ENOMEM
;
1308 if (elements
->verbose
)
1311 struct string_s
*realpath
= NULL
;
1313 if (a_target
&& elements
->with_target
)
1315 rc
= build_realpath (doc
, path
, &realpath
);
1319 realpath
= string_prepend (realpath
, "T ");
1322 ret
= strv_printf (&elements
->elements
, "%s%s%s%s",
1324 find_element_node (n
->children
)
1325 || realpath
? " " : "",
1326 find_element_node (n
->children
) ? "+" : "",
1327 realpath
? realpath
->str
: "");
1330 rc
= GPG_ERR_ENOMEM
;
1334 else if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1336 rc
= GPG_ERR_ENOMEM
;
1340 if (update_element_list (elements
) == 0)
1342 rc
= GPG_ERR_ENOMEM
;
1347 rc
= path_list_recurse (doc
, n
->children
, elements
);
1351 strv_free (req_orig
);
1358 recurse_xpath_nodeset (xmlDocPtr doc
, xmlNodeSetPtr nodes
,
1359 xmlChar
* value
, xmlBufferPtr
* result
, int cmd
,
1360 const xmlChar
* attr
)
1362 int i
= value
? nodes
->nodeNr
- 1 : 0;
1365 buf
= xmlBufferCreate ();
1368 return GPG_ERR_ENOMEM
;
1370 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1372 xmlNodePtr n
= nodes
->nodeTab
[i
];
1378 if (!value
&& !attr
)
1380 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1383 return GPG_ERR_BAD_DATA
;
1391 xmlNodeSetContent (n
, value
);
1392 rc
= update_element_mtime (n
);
1400 rc
= add_attribute (n
, (char *) attr
, (char *) value
);
1402 rc
= delete_attribute (n
, attr
);
1414 convert_root_element (xmlNodePtr n
)
1416 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1422 xmlChar
*t
= xmlGetNodePath (n
);
1425 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1428 return GPG_ERR_AMBIGUOUS_NAME
;
1431 a
= xmlGetProp (n
, (xmlChar
*) "name");
1435 rc
= add_attribute (n
, "_name", (char *) a
);
1441 rc
= delete_attribute (n
, (xmlChar
*) "name");
1446 xmlNodeSetName (n
, (xmlChar
*) "element");
1453 delete_attribute (xmlNodePtr n
, const xmlChar
* name
)
1457 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1458 return GPG_ERR_NOT_FOUND
;
1460 if (xmlRemoveProp (a
) == -1)
1461 return GPG_ERR_BAD_DATA
;
1463 return update_element_mtime (n
);
1467 convert_elements_recurse (xmlDocPtr doc
, xmlNodePtr n
, unsigned depth
)
1473 for (n
= n
->children
; n
; n
= n
->next
)
1475 if (n
->type
== XML_ELEMENT_NODE
)
1481 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1483 xmlChar
*t
= xmlGetNodePath (n
);
1486 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1489 return GPG_ERR_AMBIGUOUS_NAME
;
1492 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1497 xmlChar
*t
= xmlGetNodePath (n
);
1500 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1503 return GPG_ERR_AMBIGUOUS_NAME
;
1506 xmlChar
*tmp
= xmlStrdup (n
->name
);
1509 return GPG_ERR_ENOMEM
;
1511 xmlNodeSetName (n
, (xmlChar
*) "element");
1512 rc
= add_attribute (n
, "_name", (char *) tmp
);
1520 rc
= convert_root_element (n
);
1529 rc
= convert_elements_recurse (doc
, n
, depth
);
1539 /* Renames ALL elements to the new "element" name. Existing element names are
1540 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1541 * elements can contain common characters that the XML parser barfs on (an
1542 * email address for example. */
1544 convert_pre_212_elements (xmlDocPtr doc
)
1546 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1548 log_write (_("Converting pre 2.12 data file..."));
1549 return convert_elements_recurse (doc
, n
, 0);
1553 validate_import (xmlNodePtr node
)
1560 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1562 if (n
->type
== XML_ELEMENT_NODE
)
1564 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1566 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1570 xmlChar
*t
= xmlGetNodePath (n
);
1572 log_write (_("Missing attribute '_name' at %s."), t
);
1574 return GPG_ERR_INV_VALUE
;
1577 if (!valid_xml_element (a
))
1579 xmlChar
*t
= xmlGetNodePath (n
);
1581 log_write (_("'%s' is not a valid element name at %s."), a
,
1585 return GPG_ERR_INV_VALUE
;
1589 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1594 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1596 update_element_mtime (n
);
1601 xmlChar
*t
= xmlGetNodePath (n
);
1603 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1612 rc
= validate_import (n
->children
);
1623 update_element_mtime (xmlNodePtr n
)
1625 return add_attribute (n
, NULL
, NULL
);
1629 unlink_node (xmlNodePtr n
)
1637 rc
= update_element_mtime (n
->parent
);
1644 parse_doc (const char *xml
, size_t len
)
1646 return xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1650 realpath_elements_cb (xmlNodePtr node
, char **target
,
1651 gpg_error_t
* rc
, char **req_orig
, void *data
)
1653 char *path
= *(char **) data
;
1654 char *tmp
= NULL
, *result
;
1659 *(char **) data
= NULL
;
1662 path
= strv_join ("\t", target
);
1666 *rc
= GPG_ERR_ENOMEM
;
1672 tmp
= strv_join ("\t", req_orig
);
1677 *rc
= GPG_ERR_ENOMEM
;
1683 result
= str_asprintf ("%s\t%s", path
, tmp
);
1685 result
= str_dup (path
);
1689 *rc
= GPG_ERR_ENOMEM
;
1697 *(char **) data
= result
;
1702 build_realpath (xmlDocPtr doc
, char *line
, struct string_s
** result
)
1709 struct string_s
*string
;
1712 if (strchr (line
, '\t') != NULL
)
1714 if ((req
= str_split (line
, "\t", 0)) == NULL
)
1715 return GPG_ERR_SYNTAX
;
1719 if ((req
= str_split (line
, " ", 0)) == NULL
)
1720 return GPG_ERR_SYNTAX
;
1723 n
= find_root_element (doc
, &req
, &rc
, NULL
, 0, 0);
1730 rp
= strv_join ("\t", req
);
1734 return GPG_ERR_ENOMEM
;
1739 n
= find_elements (doc
, n
->children
, req
+ 1, &rc
,
1740 NULL
, realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
1749 string
= string_new (rp
);
1753 return GPG_ERR_ENOMEM
;
1756 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
1758 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
1760 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
1764 string_free (string
, 1);
1765 return GPG_ERR_ENOMEM
;
1779 node_to_element_path (xmlNodePtr node
)
1782 struct string_s
*str
= string_new ("");
1785 for (n
= node
; n
; n
= n
->parent
)
1789 for (child
= n
; child
; child
= child
->next
)
1791 if (child
->type
!= XML_ELEMENT_NODE
)
1794 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
1797 str
= string_prepend (str
, (char *) name
);
1799 name
= node_has_attribute (n
, (xmlChar
*) "target");
1801 str
= string_prepend (str
, "\t");
1803 str
= string_prepend (str
, "\t!");
1810 str
= string_erase (str
, 0, 1);
1812 string_free (str
, 0);
1818 * Recurse the element tree beginning at 'node' and find elements who point
1819 * back to 'src' or 'dst'. Also follows target attributes.
1822 find_child_to_target (xmlDocPtr doc
, xmlNodePtr node
,
1823 xmlNodePtr src
, xmlNodePtr dst
)
1828 for (n
= node
; n
; n
= n
->next
)
1832 if (n
->type
!= XML_ELEMENT_NODE
)
1835 if (n
== src
|| n
== dst
)
1836 return GPG_ERR_ELOOP
;
1838 target
= node_has_attribute (n
, (xmlChar
*) "target");
1842 char **result
= NULL
;
1844 tmp
= resolve_path (doc
, target
, &result
, &rc
);
1849 rc
= find_child_to_target (doc
, tmp
, src
, dst
);
1857 rc
= find_child_to_target (doc
, n
->children
, src
, dst
);
1866 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
1871 for (n
= src
; n
; n
= n
->next
)
1873 if (n
->type
!= XML_ELEMENT_NODE
)
1882 rc
= find_child_of_parent (doc
, n
->children
, dst
);
1889 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
1894 for (n
= node
; n
; n
= n
->parent
)
1896 if (n
->type
!= XML_ELEMENT_NODE
)
1900 for (tmp
= n
->next
; tmp
; n
= n
->next
)
1902 if (n
->type
!= XML_ELEMENT_NODE
)
1906 return GPG_ERR_ELOOP
;
1911 return GPG_ERR_ELOOP
;
1918 validate_target_attribute (xmlDocPtr doc
, const char *src
,
1919 xmlNodePtr dst_node
)
1922 xmlNodePtr src_node
;
1923 char **src_req
= NULL
;
1925 src_node
= resolve_path (doc
, (xmlChar
*) src
, &src_req
, &rc
);
1929 /* A destination element is a child of the source element. */
1930 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
1934 /* The destination element is a parent of the source element. */
1935 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
1939 /* A destination child element contains a target to the source element. */
1940 rc
= find_child_to_target (doc
, dst_node
->children
, src_node
, dst_node
);
1945 strv_free (src_req
);