1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2010 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
33 #include <libxml/xmlwriter.h>
34 #include "pwmd_error.h"
38 extern void log_write(const gchar
*fmt
, ...);
41 * 'element' must be allocated.
43 gboolean
is_literal_element(gchar
**element
)
47 if (!element
|| !*element
)
50 if (*(*element
) == '!') {
53 for (p
= *element
, c
= p
+1; *c
; c
++)
63 gboolean
valid_xml_element(xmlChar
*element
)
68 if (strchr((gchar
*)element
, '\t') || strchr((gchar
*)element
, ' ') ||
75 gpg_error_t
new_root_element(xmlDocPtr doc
, gchar
*name
)
77 xmlNodePtr root
= xmlDocGetRootElement(doc
);
82 return EPWMD_LIBXML_ERROR
;
87 if (!valid_xml_element((xmlChar
*)p
))
88 return GPG_ERR_INV_VALUE
;
90 n
= xmlNewNode(NULL
, (xmlChar
*)"element");
91 n
= xmlAddChild(root
, n
);
92 return add_attribute(n
, "_name", p
);
95 xmlDocPtr
create_dtd()
98 xmlTextWriterPtr wr
= xmlNewTextWriterDoc(&doc
, 0);
103 if (xmlTextWriterStartDocument(wr
, NULL
, NULL
, NULL
))
106 if (xmlTextWriterStartDTD(wr
, (xmlChar
*)"pwmd", NULL
, NULL
) == -1)
109 if (xmlTextWriterWriteDTDElement(wr
, (xmlChar
*)"pwmd",
110 (xmlChar
*)"(element)") == -1)
113 xmlTextWriterEndDTDElement(wr
);
115 if (xmlTextWriterWriteDTDAttlist(wr
, (xmlChar
*)"element",
116 (xmlChar
*)"_name CDATA #REQUIRED") == -1)
119 xmlTextWriterEndDTDAttlist(wr
);
120 xmlTextWriterEndDTD(wr
);
122 if (xmlTextWriterStartElement(wr
, (xmlChar
*)"pwmd"))
125 xmlTextWriterEndElement(wr
);
126 xmlTextWriterEndDocument(wr
);
127 xmlFreeTextWriter(wr
);
131 xmlTextWriterEndDocument(wr
);
132 xmlFreeTextWriter(wr
);
137 xmlChar
*new_document()
141 xmlDocPtr doc
= create_dtd();
146 xmlDocDumpMemory(doc
, &xml
, &len
);
151 xmlNodePtr
find_element_node(xmlNodePtr node
)
155 if (n
&& n
->type
== XML_ELEMENT_NODE
)
158 for (n
= node
; n
; n
= n
->next
) {
159 if (n
->type
== XML_ELEMENT_NODE
)
167 * Lists root element names; the value of the attribute "_name" of an element
168 * "element". If there's a target attribute both literal and non-literal
169 * element names will be added. This is the primary reason why XML entities
170 * cannot be used. There wouldn't be a way to get the literal an non-literal
173 gpg_error_t
list_root_elements(xmlDocPtr doc
, GString
**result
,
182 n
= xmlDocGetRootElement(doc
);
184 if (!n
|| !n
->children
)
185 return GPG_ERR_NO_VALUE
;
187 for (n
= n
->children
; n
; n
= n
->next
) {
189 xmlChar
*val
, *target
;
193 if (n
->type
!= XML_ELEMENT_NODE
)
196 a
= xmlHasProp(n
, (xmlChar
*)"_name");
198 if (!a
|| !a
->children
->content
)
201 val
= xmlNodeGetContent(a
->children
);
208 tmp
= g_strdup_printf("!%s%s", (gchar
*)val
, verbose
? find_element_node(n
->children
) ? " 1" : " 0" : "");
216 tlist
= g_slist_append(list
, tmp
);
225 target
= node_has_attribute(n
, (xmlChar
*)"target");
231 gchar
**req
= split_input_line((gchar
*)target
, "\t", 0);
232 xmlNodePtr tnode
= find_root_element(doc
, &req
, &rc
, NULL
, 0, FALSE
);
234 if (tnode
&& req
[1]) {
235 tnode
= find_elements(doc
, tnode
->children
, req
+1, &rc
,
236 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
240 t
= g_strdup_printf("%s%s", (gchar
*)val
,
241 tnode
&& find_element_node(tnode
->children
) ? " 1" : " 0");
244 t
= g_strdup((gchar
*)val
);
253 tlist
= g_slist_append(list
, t
);
269 total
= g_slist_length(list
);
272 return GPG_ERR_NO_VALUE
;
274 string
= g_string_new(NULL
);
281 for (i
= 0; i
< total
; i
++) {
282 gchar
*val
= g_slist_nth_data(list
, i
);
284 g_string_append_printf(string
, "%s\n", val
);
287 string
= g_string_truncate(string
, string
->len
- 1);
291 total
= g_slist_length(list
);
293 for (i
= 0; i
< total
; i
++)
294 g_free(g_slist_nth_data(list
, i
));
301 * Prevents a sibling element past the current element path with the same
304 static xmlNodePtr
find_stop_node(xmlNodePtr node
)
308 for (n
= node
->parent
->children
; n
; n
= n
->next
) {
317 * Alot like create_elements_cb() but doesn't use the last element of 'req' as
318 * content but as an element.
320 xmlNodePtr
create_target_elements_cb(xmlNodePtr node
, gchar
**path
,
321 gpg_error_t
*rc
, void *data
)
326 for (i
= 0; req
[i
]; i
++) {
329 if ((n
= find_element(node
, req
[i
], find_stop_node(node
))) == NULL
||
330 (n
&& n
->parent
== node
->parent
)) {
331 is_literal_element(&req
[i
]);
333 if (!valid_xml_element((xmlChar
*)req
[i
])) {
334 *rc
= GPG_ERR_INV_VALUE
;
338 //n = xmlNewNode(NULL, (xmlChar *)req[i]);
339 n
= xmlNewNode(NULL
, (xmlChar
*)"element");
342 *rc
= GPG_ERR_ENOMEM
;
346 add_attribute(n
, "_name", req
[i
]);
347 node
= xmlAddChild(node
, n
);
350 *rc
= GPG_ERR_ENOMEM
;
361 xmlNodePtr
find_text_node(xmlNodePtr node
)
365 if (n
&& n
->type
== XML_TEXT_NODE
)
368 for (n
= node
; n
; n
= n
->next
) {
369 if (n
->type
== XML_TEXT_NODE
)
376 xmlNodePtr
create_elements_cb(xmlNodePtr node
, gchar
**elements
,
377 gpg_error_t
*rc
, void *data
)
380 gchar
**req
= elements
;
382 if (node
->type
== XML_TEXT_NODE
)
385 xmlChar
*a
= node_has_attribute(node
, (xmlChar
*)"_name");
390 for (i
= 0; req
[i
]; i
++) {
395 * Strip the first '!' if needed. If there's another, it's an
396 * rc. The syntax has already been checked before calling this
399 is_literal_element(&req
[i
]);
403 * The value of the element tree.
406 n
= find_text_node(node
->children
);
409 /* Use AddContent here to prevent overwriting any children. */
410 xmlNodeAddContent(node
, (xmlChar
*)req
[i
]);
411 else if (n
&& !*req
[i
])
412 xmlNodeSetContent(n
, NULL
);
414 xmlNodeSetContent(n
, (xmlChar
*)req
[i
]);
419 n
= find_element(node
, req
[i
], find_stop_node(node
));
422 * If the found element has the same parent as the current element,
423 * they are siblings and the new element needs to be created as a
424 * child of the current element (node).
426 if (n
&& n
->parent
== node
->parent
)
430 if (!valid_xml_element((xmlChar
*)req
[i
])) {
431 *rc
= GPG_ERR_INV_VALUE
;
435 //n = xmlNewNode(NULL, (xmlChar *)req[i]);
436 n
= xmlNewNode(NULL
, (xmlChar
*)"element");
439 *rc
= GPG_ERR_ENOMEM
;
443 *rc
= add_attribute(n
, "_name", req
[i
]);
448 node
= xmlAddChild(node
, n
);
451 *rc
= GPG_ERR_ENOMEM
;
462 /* The root element is really req[0]. It is need as a pointer in case there is
463 * a target attribute so it can be updated. */
464 xmlNodePtr
find_root_element(xmlDocPtr doc
, gchar
***req
, gpg_error_t
*rc
,
465 gboolean
*target
, gint recursion_depth
, gboolean stop
)
467 xmlNodePtr n
= xmlDocGetRootElement(doc
);
469 gchar
*root
= g_strdup(*req
[0]);
470 gboolean literal
= is_literal_element(&root
);
473 *rc
= GPG_ERR_ENOMEM
;
480 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
481 xmlChar
*t
= xmlGetNodePath(n
);
483 log_write("%s: %s", pwmd_strerror(GPG_ERR_ELOOP
), t
);
491 if (n
->type
== XML_ELEMENT_NODE
) {
492 if (depth
== 0 && xmlStrEqual(n
->name
, (xmlChar
*)"pwmd")) {
498 if (depth
== 1 && xmlStrEqual(n
->name
, (xmlChar
*)"element")) {
499 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"_name");
504 if (xmlStrEqual(content
, (xmlChar
*)root
)) {
505 gchar
**nreq
, **tmp
= NULL
;
507 if (literal
== TRUE
) {
514 content
= node_has_attribute(n
, (xmlChar
*)"target");
519 if (!content
|| stop
) {
527 if (strchr((gchar
*)content
, '\t')) {
528 nreq
= split_input_line((gchar
*)content
, "\t", 0);
536 *rc
= GPG_ERR_ENOMEM
;
542 tmp
= strvcatv(nreq
, tmp
+1);
547 *rc
= GPG_ERR_ENOMEM
;
555 if (strv_printf(&tmp
, "%s", content
) == FALSE
) {
558 *rc
= GPG_ERR_ENOMEM
;
564 nreq
= strvcatv(tmp
, nreq
+1);
568 *rc
= GPG_ERR_ENOMEM
;
578 n
= find_root_element(doc
, req
, rc
, target
, recursion_depth
, FALSE
);
590 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
594 xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
)
598 if (!node
|| !element
)
601 for (n
= node
; n
; n
= n
->next
) {
602 if (n
->type
!= XML_ELEMENT_NODE
)
608 xmlChar
*a
= node_has_attribute(n
, (xmlChar
*)"_name");
610 if (a
&& xmlStrEqual(a
, (xmlChar
*)element
)) {
621 xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
)
623 xmlAttrPtr a
= xmlHasProp(n
, attr
);
628 if (!a
->children
|| !a
->children
->content
)
631 return xmlGetProp(n
, attr
);
634 static gboolean
element_to_literal(gchar
**element
)
636 gchar
*p
= g_strdup_printf("!%s", *element
);
646 /* Resolves elements in 'req' one at a time. It's recursive in case of
647 * "target" attributes. */
648 xmlNodePtr
find_elements(xmlDocPtr doc
, xmlNodePtr node
,
649 gchar
**req
, gpg_error_t
*rc
, gboolean
*target
,
650 xmlNodePtr (*found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, gchar
**, void *),
651 xmlNodePtr (*not_found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, void *),
652 gboolean is_list_command
, gint recursion_depth
, void *data
, gboolean stop
)
654 xmlNodePtr n
, last
, last_node
;
661 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
662 xmlChar
*t
= xmlGetNodePath(node
);
664 log_write("%s: %s", pwmd_strerror(GPG_ERR_ELOOP
), t
);
671 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++) {
673 gchar
*t
= g_strdup(*p
);
677 *rc
= GPG_ERR_ENOMEM
;
681 literal
= is_literal_element(&t
);
682 n
= find_element(last
, t
, NULL
);
687 return not_found_fn(found
? last_node
: last_node
->parent
, p
, rc
, data
);
689 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
697 if (literal
== FALSE
) {
698 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
699 gchar
**nreq
= NULL
, **nnreq
;
702 if (is_list_command
== TRUE
) {
703 if (element_to_literal(&(*p
)) == FALSE
) {
704 *rc
= GPG_ERR_ENOMEM
;
715 if (!*(p
+1) && stop
) {
720 if (strchr((gchar
*)content
, '\t') != NULL
) {
721 if ((nreq
= split_input_line((gchar
*)content
, "\t", 0)) == NULL
) {
723 *rc
= GPG_ERR_INV_VALUE
;
728 if ((nreq
= split_input_line((gchar
*)content
, " ", 0)) == NULL
) {
730 *rc
= GPG_ERR_INV_VALUE
;
736 tmp
= find_root_element(doc
, &nreq
, rc
, target
, 0, FALSE
);
744 found_fn(tmp
, nreq
, rc
, p
+1, data
);
752 nnreq
= strvcatv(nreq
+1, p
+1);
756 if (!nnreq
|| !*nnreq
) {
763 n
= find_elements(doc
, tmp
->children
, nnreq
, rc
, NULL
, found_fn
,
764 not_found_fn
, is_list_command
, recursion_depth
, data
, stop
);
767 gchar
**zz
= p
+1, **qq
= nnreq
;
769 if (g_strv_length(nnreq
) > g_strv_length(p
+1))
772 for (; *qq
&& *zz
; zz
++) {
774 *zz
= g_strdup(*qq
++);
777 *rc
= GPG_ERR_ENOMEM
;
792 static gboolean
update_element_list(struct element_list_s
*elements
)
797 if (!elements
|| !elements
->elements
)
800 line
= g_strjoinv("\t", elements
->elements
);
805 g_strfreev(elements
->elements
);
806 elements
->elements
= NULL
;
807 l
= g_slist_append(elements
->list
, line
);
816 static gpg_error_t
path_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
817 struct element_list_s
*elements
)
822 for (n
= node
; n
; n
= n
->next
) {
823 xmlChar
*target
= NULL
;
824 xmlChar
*a
= node_has_attribute(n
, (xmlChar
*)"_name");
829 if (n
->type
!= XML_ELEMENT_NODE
)
832 if (elements
->verbose
) {
833 if (strv_printf(&elements
->elements
, "%s\t!%s%s", elements
->prefix
, a
, find_element_node(n
->children
) ? " 1" : " 0") == FALSE
) {
835 return GPG_ERR_ENOMEM
;
838 else if (strv_printf(&elements
->elements
, "%s\t!%s", elements
->prefix
, a
) == FALSE
) {
840 return GPG_ERR_ENOMEM
;
843 if (update_element_list(elements
) == FALSE
) {
845 return GPG_ERR_ENOMEM
;
848 target
= node_has_attribute(n
, (xmlChar
*)"target");
852 gchar
*save
= elements
->prefix
;
853 gboolean r
= elements
->resolving
;
857 if (max_recursion_depth
>= 1 && elements
->depth
> max_recursion_depth
) {
858 xmlChar
*t
= xmlGetNodePath(n
);
860 log_write("%s: %s", pwmd_strerror(GPG_ERR_ELOOP
), t
);
864 return GPG_ERR_ELOOP
;
867 if (elements
->verbose
) {
868 gchar
**req
= split_input_line((gchar
*)target
, "\t", 0);
869 xmlNodePtr tnode
= find_root_element(doc
, &req
, &rc
, NULL
, 0, FALSE
);
878 tnode
= find_elements(doc
, tnode
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
887 if (strv_printf(&elements
->elements
, "%s\t%s%s", elements
->prefix
, a
, find_element_node(tnode
->children
) ? " 1" : " 0") == FALSE
) {
890 return GPG_ERR_ENOMEM
;
893 else if (strv_printf(&elements
->elements
, "%s\t%s", elements
->prefix
, a
) == FALSE
) {
896 return GPG_ERR_ENOMEM
;
899 tmp
= g_strjoinv("\t", elements
->elements
);
904 return GPG_ERR_ENOMEM
;
907 if (update_element_list(elements
) == FALSE
) {
911 return GPG_ERR_ENOMEM
;
914 if (elements
->recurse
) {
915 if (elements
->verbose
)
916 tmp
[strlen(tmp
)-2] = 0;
918 elements
->prefix
= tmp
;
919 elements
->resolving
= TRUE
;
920 rc
= create_path_list(doc
, elements
, (gchar
*)target
);
922 elements
->resolving
= r
;
925 elements
->prefix
= save
;
935 if (n
->children
&& elements
->recurse
) {
936 gchar
*tmp
= g_strdup_printf("%s\t!%s", elements
->prefix
, a
);
937 gchar
*save
= elements
->prefix
;
941 return GPG_ERR_ENOMEM
;
944 elements
->prefix
= tmp
;
945 rc
= path_list_recurse(doc
, n
->children
, elements
);
946 g_free(elements
->prefix
);
947 elements
->prefix
= save
;
961 gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
967 if (name
&& !xmlSetProp(node
, (xmlChar
*)name
, (xmlChar
*)value
))
968 return EPWMD_LIBXML_ERROR
;
970 if (name
&& xmlStrEqual((xmlChar
*)name
, (xmlChar
*)"_mtime"))
973 buf
= g_strdup_printf("%li", time(NULL
));
974 rc
= add_attribute(node
, "_mtime", buf
);
980 * From the element path 'path', find sub-nodes and append them to the list.
982 gpg_error_t
create_path_list(xmlDocPtr doc
, struct element_list_s
*elements
,
986 gchar
**req
, **req_orig
;
988 gboolean a_target
= FALSE
;
990 req
= split_input_line(path
, "\t", 0);
993 req
= split_input_line(path
, " ", 0);
996 return GPG_ERR_SYNTAX
;
999 req_orig
= g_strdupv(req
);
1002 rc
= GPG_ERR_ENOMEM
;
1006 n
= find_root_element(doc
, &req
, &rc
, &a_target
, 0, FALSE
);
1008 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
1015 if (a_target
== TRUE
) {
1017 *req
= g_strdup(*req_orig
);
1021 gboolean e_target
= FALSE
;
1023 n
= find_elements(doc
, n
->children
, req
+1, &rc
, &e_target
, NULL
, NULL
, TRUE
, 0, NULL
, FALSE
);
1025 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
1033 if (!elements
->prefix
) {
1037 * If any req_orig element contains no target the element should be
1038 * prefixed with the literal character. Not really crucial if the
1039 * client isn't human because child elements are prefixed for the
1040 * current path. But may be confusing if editing by hand.
1042 elements
->prefix
= g_strjoinv("\t", req_orig
);
1044 if (!elements
->prefix
) {
1045 rc
= GPG_ERR_ENOMEM
;
1049 if (elements
->verbose
) {
1050 if (strv_printf(&elements
->elements
, "%s%s", elements
->prefix
, find_element_node(n
->children
) ? " 1" : " 0") == FALSE
) {
1051 rc
= GPG_ERR_ENOMEM
;
1055 else if (strv_printf(&elements
->elements
, "%s", elements
->prefix
) == FALSE
) {
1056 rc
= GPG_ERR_ENOMEM
;
1060 if (update_element_list(elements
) == FALSE
) {
1061 rc
= GPG_ERR_ENOMEM
;
1066 rc
= path_list_recurse(doc
, n
->children
, elements
);
1070 g_strfreev(req_orig
);
1076 gpg_error_t
recurse_xpath_nodeset(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
1077 xmlChar
*value
, xmlBufferPtr
*result
, gboolean cmd
, const xmlChar
*attr
)
1079 gint i
= value
? nodes
->nodeNr
- 1 : 0;
1082 buf
= xmlBufferCreate();
1085 return GPG_ERR_ENOMEM
;
1087 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++) {
1088 xmlNodePtr n
= nodes
->nodeTab
[i
];
1094 if (!value
&& !attr
) {
1095 if (xmlNodeDump(buf
, doc
, n
, 0, 0) == -1) {
1097 return EPWMD_LIBXML_ERROR
;
1104 xmlNodeSetContent(n
, value
);
1105 rc
= update_element_mtime(n
);
1112 rc
= add_attribute(n
, (gchar
*)attr
, (gchar
*)value
);
1114 rc
= delete_attribute(n
, attr
);
1125 static gpg_error_t
convert_root_element(xmlNodePtr n
)
1127 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
1132 xmlChar
*t
= xmlGetNodePath(n
);
1134 log_write(N_("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"), t
);
1136 return GPG_ERR_AMBIGUOUS_NAME
;
1139 a
= xmlGetProp(n
, (xmlChar
*)"name");
1142 rc
= add_attribute(n
, "_name", (gchar
*)a
);
1148 rc
= delete_attribute(n
, (xmlChar
*)"name");
1153 xmlNodeSetName(n
, (xmlChar
*)"element");
1159 /* Updates the DTD and renames the root "accounts" and "account" elements. */
1160 gpg_error_t
convert_xml(gchar
**xml
, goffset
*len
)
1162 gpg_error_t rc
= EPWMD_LIBXML_ERROR
;
1163 xmlDocPtr doc
, new = NULL
;
1166 doc
= xmlReadMemory(*xml
, *len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1169 return EPWMD_LIBXML_ERROR
;
1173 n
= xmlDocGetRootElement(doc
);
1174 xmlNodeSetName(n
, (xmlChar
*)"pwmd");
1176 for (n
= n
->children
; n
; n
= n
->next
) {
1177 if (xmlStrEqual(n
->name
, (xmlChar
*)"account")) {
1178 rc
= convert_root_element(n
);
1192 n
= xmlDocGetRootElement(doc
);
1193 xmlDocSetRootElement(new, n
);
1194 xmlDocDumpMemory(new, (xmlChar
**)xml
, (gint
*)len
);
1195 xmlDocSetRootElement(new, xmlCopyNode(n
, 0));
1206 gpg_error_t
delete_attribute(xmlNodePtr n
, const xmlChar
*name
)
1210 if ((a
= xmlHasProp(n
, name
)) == NULL
)
1211 return GPG_ERR_NOT_FOUND
;
1213 if (xmlRemoveProp(a
) == -1)
1214 return EPWMD_LIBXML_ERROR
;
1216 return update_element_mtime(n
);
1219 static gpg_error_t
convert_elements_recurse(xmlDocPtr doc
, xmlNodePtr n
,
1226 for (n
= n
->children
; n
; n
= n
->next
) {
1227 if (n
->type
== XML_ELEMENT_NODE
) {
1231 if (xmlStrEqual(n
->name
, (xmlChar
*)"element")) {
1232 xmlChar
*t
= xmlGetNodePath(n
);
1234 log_write(N_("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"), t
);
1236 return GPG_ERR_AMBIGUOUS_NAME
;
1239 a
= xmlGetProp(n
, (xmlChar
*)"_name");
1243 xmlChar
*t
= xmlGetNodePath(n
);
1245 log_write(N_("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"), t
);
1247 return GPG_ERR_AMBIGUOUS_NAME
;
1250 xmlChar
*tmp
= xmlStrdup(n
->name
);
1253 return GPG_ERR_ENOMEM
;
1255 xmlNodeSetName(n
, (xmlChar
*)"element");
1256 rc
= add_attribute(n
, "_name", (gchar
*)tmp
);
1263 rc
= convert_root_element(n
);
1271 rc
= convert_elements_recurse(doc
, n
, depth
);
1281 /* Renames ALL elements to the new "element" name. Existing element names are
1282 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1283 * elements can contain common characters that the XML parser barfs on (an
1284 * email address for example. */
1285 gpg_error_t
convert_elements(xmlDocPtr doc
)
1287 xmlNodePtr n
= xmlDocGetRootElement(doc
);
1290 log_write(N_("Converting pre 2.12 data file..."));
1291 rc
= convert_elements_recurse(doc
, n
, 0);
1294 log_write(N_("Finished converting. Please SAVE to update."));
1299 gpg_error_t
validate_import(xmlNodePtr node
)
1306 for (xmlNodePtr n
= node
; n
; n
= n
->next
) {
1307 if (n
->type
== XML_ELEMENT_NODE
) {
1308 if (xmlStrEqual(n
->name
, (xmlChar
*)"element")) {
1309 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
1312 xmlChar
*t
= xmlGetNodePath(n
);
1314 log_write(N_("Missing attribute '_name' at %s."), t
);
1316 return GPG_ERR_INV_VALUE
;
1319 if (!valid_xml_element(a
)) {
1320 xmlChar
*t
= xmlGetNodePath(n
);
1322 log_write(N_("'%s' is not a valid element name at %s."), a
, t
);
1325 return GPG_ERR_INV_VALUE
;
1331 xmlChar
*t
= xmlGetNodePath(n
);
1333 log_write(N_("Warning: unknown element '%s' at %s. Ignoring."), n
->name
, t
);
1340 rc
= validate_import(n
->children
);
1350 gpg_error_t
update_element_mtime(xmlNodePtr n
)
1352 return add_attribute(n
, NULL
, NULL
);
1355 gpg_error_t
unlink_node(xmlNodePtr n
)
1363 rc
= update_element_mtime(n
->parent
);