1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2011 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
32 #include <glib/gi18n.h>
34 #include <libxml/xmlwriter.h>
35 #include "pwmd_error.h"
39 extern void log_write(const gchar
*fmt
, ...);
42 * 'element' must be allocated.
44 gboolean
is_literal_element(gchar
**element
)
48 if (!element
|| !*element
)
51 if (*(*element
) == '!') {
54 for (p
= *element
, c
= p
+1; *c
; c
++)
64 gboolean
valid_xml_element(xmlChar
*element
)
66 gchar
*p
= (gchar
*)element
;
68 if (!element
|| *element
== '!')
72 if (g_ascii_isspace(*p
))
79 gboolean
valid_element_path(gchar
**path
, gboolean with_content
)
81 gchar
**dup
= NULL
, **p
;
87 /* Save some memory by not duplicating the element content. */
89 t
= g_strv_length(path
);
90 for (i
= 0; i
< t
-1; i
++) {
91 gchar
**tmp
= g_realloc(dup
, (i
+2)*sizeof(gchar
*));
99 dup
[i
] = g_strdup(path
[i
]);
104 dup
= g_strdupv(path
);
109 for (p
= dup
; *p
&& *(*p
); p
++) {
110 is_literal_element(&(*p
));
111 if (!valid_xml_element((xmlChar
*)*p
)) {
121 static gpg_error_t
attr_ctime(xmlNodePtr n
)
123 gchar
*buf
= g_strdup_printf("%li", time(NULL
));
127 return GPG_ERR_ENOMEM
;
129 rc
= add_attribute(n
, "_ctime", buf
);
134 static gpg_error_t
create_new_element(xmlNodePtr parent
, const gchar
*name
,
137 xmlNodePtr n
= xmlNewNode(NULL
, (xmlChar
*)"element");
141 return GPG_ERR_ENOMEM
;
143 rc
= add_attribute(n
, "_name", name
);
149 *result
= xmlAddChild(parent
, n
);
151 (void)xmlAddChild(parent
, n
);
159 gpg_error_t
new_root_element(xmlDocPtr doc
, gchar
*name
)
161 xmlNodePtr root
= xmlDocGetRootElement(doc
);
165 return EPWMD_LIBXML_ERROR
;
170 if (!valid_xml_element((xmlChar
*)p
))
171 return GPG_ERR_INV_VALUE
;
173 return create_new_element(root
, p
, NULL
);
176 xmlDocPtr
create_dtd()
179 xmlTextWriterPtr wr
= xmlNewTextWriterDoc(&doc
, 0);
184 if (xmlTextWriterStartDocument(wr
, NULL
, NULL
, NULL
))
187 if (xmlTextWriterStartDTD(wr
, (xmlChar
*)"pwmd", NULL
, NULL
) == -1)
190 if (xmlTextWriterWriteDTDElement(wr
, (xmlChar
*)"pwmd",
191 (xmlChar
*)"(element)") == -1)
194 xmlTextWriterEndDTDElement(wr
);
196 if (xmlTextWriterWriteDTDAttlist(wr
, (xmlChar
*)"element",
197 (xmlChar
*)"_name CDATA #REQUIRED") == -1)
200 xmlTextWriterEndDTDAttlist(wr
);
201 xmlTextWriterEndDTD(wr
);
203 if (xmlTextWriterStartElement(wr
, (xmlChar
*)"pwmd"))
206 xmlTextWriterEndElement(wr
);
207 xmlTextWriterEndDocument(wr
);
208 xmlFreeTextWriter(wr
);
212 xmlTextWriterEndDocument(wr
);
213 xmlFreeTextWriter(wr
);
218 xmlChar
*new_document()
222 xmlDocPtr doc
= create_dtd();
227 xmlDocDumpMemory(doc
, &xml
, &len
);
232 xmlNodePtr
find_element_node(xmlNodePtr node
)
236 if (n
&& n
->type
== XML_ELEMENT_NODE
)
239 for (n
= node
; n
; n
= n
->next
) {
240 if (n
->type
== XML_ELEMENT_NODE
)
248 * Lists root element names; the value of the attribute "_name" of an element
249 * "element". If there's a target attribute both literal and non-literal
250 * element names will be added. This is the primary reason why XML entities
251 * cannot be used. There wouldn't be a way to get the literal an non-literal
254 gpg_error_t
list_root_elements(xmlDocPtr doc
, GString
**result
,
263 n
= xmlDocGetRootElement(doc
);
265 if (!n
|| !n
->children
)
266 return GPG_ERR_NO_VALUE
;
268 for (n
= n
->children
; n
; n
= n
->next
) {
270 xmlChar
*val
, *target
;
274 if (n
->type
!= XML_ELEMENT_NODE
)
277 a
= xmlHasProp(n
, (xmlChar
*)"_name");
279 if (!a
|| !a
->children
->content
)
282 val
= xmlNodeGetContent(a
->children
);
289 tmp
= g_strdup_printf("!%s%s", (gchar
*)val
, verbose
? find_element_node(n
->children
) ? " 1" : " 0" : "");
297 tlist
= g_slist_append(list
, tmp
);
306 target
= node_has_attribute(n
, (xmlChar
*)"target");
312 gchar
**req
= split_input_line((gchar
*)target
, "\t", 0);
313 xmlNodePtr tnode
= find_root_element(doc
, &req
, &rc
, NULL
, 0, FALSE
);
315 if (tnode
&& req
[1]) {
316 tnode
= find_elements(doc
, tnode
->children
, req
+1, &rc
,
317 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
321 t
= g_strdup_printf("%s%s", (gchar
*)val
,
322 tnode
&& find_element_node(tnode
->children
) ? " 1" : " 0");
325 t
= g_strdup((gchar
*)val
);
334 tlist
= g_slist_append(list
, t
);
350 total
= g_slist_length(list
);
353 return GPG_ERR_NO_VALUE
;
355 string
= g_string_new(NULL
);
362 for (i
= 0; i
< total
; i
++) {
363 gchar
*val
= g_slist_nth_data(list
, i
);
365 g_string_append_printf(string
, "%s\n", val
);
368 string
= g_string_truncate(string
, string
->len
- 1);
372 total
= g_slist_length(list
);
374 for (i
= 0; i
< total
; i
++)
375 g_free(g_slist_nth_data(list
, i
));
382 * Prevents a sibling element past the current element path with the same
385 static xmlNodePtr
find_stop_node(xmlNodePtr node
)
389 for (n
= node
->parent
->children
; n
; n
= n
->next
) {
397 xmlNodePtr
create_target_elements_cb(xmlNodePtr node
, gchar
**path
,
398 gpg_error_t
*rc
, void *data
)
402 xmlNodePtr parent
= data
;
404 for (i
= 0; req
[i
] && *req
[i
]; i
++) {
407 if (parent
&& node
== parent
) {
408 *rc
= GPG_ERR_CONFLICT
;
412 is_literal_element(&req
[i
]);
414 if ((n
= find_element(node
, req
[i
], find_stop_node(node
))) == NULL
||
415 (n
&& n
->parent
== node
->parent
)) {
417 *rc
= create_new_element(node
, req
[i
], &node
);
428 xmlNodePtr
find_text_node(xmlNodePtr node
)
432 if (n
&& n
->type
== XML_TEXT_NODE
)
435 for (n
= node
; n
; n
= n
->next
) {
436 if (n
->type
== XML_TEXT_NODE
)
443 xmlNodePtr
create_elements_cb(xmlNodePtr node
, gchar
**elements
,
444 gpg_error_t
*rc
, void *data
)
447 gchar
**req
= elements
;
449 if (node
->type
== XML_TEXT_NODE
)
452 for (i
= 0; req
[i
] && *req
[i
]; i
++) {
456 * Strip the first '!' if needed. If there's another, it's an
457 * rc. The syntax has already been checked before calling this
460 is_literal_element(&req
[i
]);
461 n
= find_element(node
, req
[i
], find_stop_node(node
));
464 * If the found element has the same parent as the current element,
465 * they are siblings and the new element needs to be created as a
466 * child of the current element (node).
468 if (n
&& n
->parent
== node
->parent
)
472 *rc
= create_new_element(node
, req
[i
], &node
);
483 /* The root element is really req[0]. It is need as a pointer in case there is
484 * a target attribute so it can be updated. */
485 xmlNodePtr
find_root_element(xmlDocPtr doc
, gchar
***req
, gpg_error_t
*rc
,
486 gboolean
*target
, gint recursion_depth
, gboolean stop
)
488 xmlNodePtr n
= xmlDocGetRootElement(doc
);
490 gchar
*root
= g_strdup(*req
[0]);
491 gboolean literal
= is_literal_element(&root
);
494 *rc
= GPG_ERR_ENOMEM
;
501 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
502 xmlChar
*t
= xmlGetNodePath(n
);
504 log_write("%s: %s", pwmd_strerror(GPG_ERR_ELOOP
), t
);
512 if (n
->type
== XML_ELEMENT_NODE
) {
513 if (depth
== 0 && xmlStrEqual(n
->name
, (xmlChar
*)"pwmd")) {
519 if (depth
== 1 && xmlStrEqual(n
->name
, (xmlChar
*)"element")) {
520 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"_name");
525 if (xmlStrEqual(content
, (xmlChar
*)root
)) {
526 gchar
**nreq
, **tmp
= NULL
;
528 if (literal
== TRUE
) {
535 content
= node_has_attribute(n
, (xmlChar
*)"target");
540 if (!content
|| stop
) {
548 if (strchr((gchar
*)content
, '\t')) {
549 nreq
= split_input_line((gchar
*)content
, "\t", 0);
557 *rc
= GPG_ERR_ENOMEM
;
563 tmp
= strvcatv(nreq
, tmp
+1);
568 *rc
= GPG_ERR_ENOMEM
;
576 if (strv_printf(&tmp
, "%s", content
) == FALSE
) {
579 *rc
= GPG_ERR_ENOMEM
;
585 nreq
= strvcatv(tmp
, nreq
+1);
589 *rc
= GPG_ERR_ENOMEM
;
599 n
= find_root_element(doc
, req
, rc
, target
, recursion_depth
, FALSE
);
611 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
615 xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
)
619 if (!node
|| !element
)
622 for (n
= node
; n
; n
= n
->next
) {
623 if (n
->type
!= XML_ELEMENT_NODE
)
629 xmlChar
*a
= node_has_attribute(n
, (xmlChar
*)"_name");
631 if (a
&& xmlStrEqual(a
, (xmlChar
*)element
)) {
642 xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
)
644 xmlAttrPtr a
= xmlHasProp(n
, attr
);
649 if (!a
->children
|| !a
->children
->content
)
652 return xmlGetProp(n
, attr
);
655 static gboolean
element_to_literal(gchar
**element
)
657 gchar
*p
= g_strdup_printf("!%s", *element
);
667 /* Resolves elements in 'req' one at a time. It's recursive in case of
668 * "target" attributes. */
669 xmlNodePtr
find_elements(xmlDocPtr doc
, xmlNodePtr node
,
670 gchar
**req
, gpg_error_t
*rc
, gboolean
*target
,
671 xmlNodePtr (*found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, gchar
**, void *),
672 xmlNodePtr (*not_found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, void *),
673 gboolean is_list_command
, gint recursion_depth
, void *data
, gboolean stop
)
675 xmlNodePtr n
, last
, last_node
;
682 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
683 xmlChar
*t
= xmlGetNodePath(node
);
685 log_write("%s: %s", pwmd_strerror(GPG_ERR_ELOOP
), t
);
692 for (last_node
= last
= n
= node
, p
= req
; *p
&& *(*p
); p
++) {
694 gchar
*t
= g_strdup(*p
);
698 *rc
= GPG_ERR_ENOMEM
;
702 literal
= is_literal_element(&t
);
703 n
= find_element(last
, t
, NULL
);
708 return not_found_fn(found
? last_node
: last_node
->parent
, p
, rc
, data
);
710 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
718 if (literal
== FALSE
) {
719 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
720 gchar
**nreq
= NULL
, **nnreq
;
723 if (is_list_command
== TRUE
) {
724 if (element_to_literal(&(*p
)) == FALSE
) {
725 *rc
= GPG_ERR_ENOMEM
;
736 if (!*(p
+1) && stop
) {
741 if (strchr((gchar
*)content
, '\t') != NULL
) {
742 if ((nreq
= split_input_line((gchar
*)content
, "\t", 0)) == NULL
) {
744 *rc
= GPG_ERR_INV_VALUE
;
749 if ((nreq
= split_input_line((gchar
*)content
, " ", 0)) == NULL
) {
751 *rc
= GPG_ERR_INV_VALUE
;
757 tmp
= find_root_element(doc
, &nreq
, rc
, target
, 0, FALSE
);
765 found_fn(tmp
, nreq
, rc
, p
+1, data
);
773 nnreq
= strvcatv(nreq
+1, p
+1);
777 if (!nnreq
|| !*nnreq
) {
785 n
= find_elements(doc
, tmp
->children
, nnreq
, rc
, NULL
, found_fn
,
786 not_found_fn
, is_list_command
, recursion_depth
, data
, stop
);
791 return not_found_fn(tmp
, p
+1, rc
, data
);
793 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
798 gchar
**zz
= p
+1, **qq
= nnreq
;
800 if (g_strv_length(nnreq
) > g_strv_length(p
+1))
803 for (; *qq
&& *zz
; zz
++) {
805 *zz
= g_strdup(*qq
++);
808 *rc
= GPG_ERR_ENOMEM
;
823 static gboolean
update_element_list(struct element_list_s
*elements
)
828 if (!elements
|| !elements
->elements
)
831 line
= g_strjoinv("\t", elements
->elements
);
836 g_strfreev(elements
->elements
);
837 elements
->elements
= NULL
;
838 l
= g_slist_append(elements
->list
, line
);
847 static gpg_error_t
path_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
848 struct element_list_s
*elements
)
853 for (n
= node
; n
; n
= n
->next
) {
854 xmlChar
*target
= NULL
;
855 xmlChar
*a
= node_has_attribute(n
, (xmlChar
*)"_name");
860 if (n
->type
!= XML_ELEMENT_NODE
)
863 if (elements
->verbose
) {
864 if (strv_printf(&elements
->elements
, "%s\t!%s%s", elements
->prefix
, a
, find_element_node(n
->children
) ? " 1" : " 0") == FALSE
) {
866 return GPG_ERR_ENOMEM
;
869 else if (strv_printf(&elements
->elements
, "%s\t!%s", elements
->prefix
, a
) == FALSE
) {
871 return GPG_ERR_ENOMEM
;
874 if (update_element_list(elements
) == FALSE
) {
876 return GPG_ERR_ENOMEM
;
879 target
= node_has_attribute(n
, (xmlChar
*)"target");
883 gchar
*save
= elements
->prefix
;
884 gboolean r
= elements
->resolving
;
888 if (max_recursion_depth
>= 1 && elements
->depth
> max_recursion_depth
) {
889 xmlChar
*t
= xmlGetNodePath(n
);
891 log_write("%s: %s", pwmd_strerror(GPG_ERR_ELOOP
), t
);
895 return GPG_ERR_ELOOP
;
898 if (elements
->verbose
) {
899 gchar
**req
= split_input_line((gchar
*)target
, "\t", 0);
900 xmlNodePtr tnode
= find_root_element(doc
, &req
, &rc
, NULL
, 0, FALSE
);
909 tnode
= find_elements(doc
, tnode
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
918 if (strv_printf(&elements
->elements
, "%s\t%s%s", elements
->prefix
, a
, find_element_node(tnode
->children
) ? " 1" : " 0") == FALSE
) {
921 return GPG_ERR_ENOMEM
;
924 else if (strv_printf(&elements
->elements
, "%s\t%s", elements
->prefix
, a
) == FALSE
) {
927 return GPG_ERR_ENOMEM
;
930 tmp
= g_strjoinv("\t", elements
->elements
);
935 return GPG_ERR_ENOMEM
;
938 if (update_element_list(elements
) == FALSE
) {
942 return GPG_ERR_ENOMEM
;
945 if (elements
->recurse
) {
946 if (elements
->verbose
)
947 tmp
[strlen(tmp
)-2] = 0;
949 elements
->prefix
= tmp
;
950 elements
->resolving
= TRUE
;
951 rc
= create_path_list(doc
, elements
, (gchar
*)target
);
953 elements
->resolving
= r
;
956 elements
->prefix
= save
;
966 if (n
->children
&& elements
->recurse
) {
967 gchar
*tmp
= g_strdup_printf("%s\t!%s", elements
->prefix
, a
);
968 gchar
*save
= elements
->prefix
;
972 return GPG_ERR_ENOMEM
;
975 elements
->prefix
= tmp
;
976 rc
= path_list_recurse(doc
, n
->children
, elements
);
977 g_free(elements
->prefix
);
978 elements
->prefix
= save
;
992 gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
998 if (name
&& !xmlSetProp(node
, (xmlChar
*)name
, (xmlChar
*)value
))
999 return EPWMD_LIBXML_ERROR
;
1001 if (name
&& xmlStrEqual((xmlChar
*)name
, (xmlChar
*)"_mtime"))
1004 buf
= g_strdup_printf("%li", time(NULL
));
1005 rc
= add_attribute(node
, "_mtime", buf
);
1011 * From the element path 'path', find sub-nodes and append them to the list.
1013 gpg_error_t
create_path_list(xmlDocPtr doc
, struct element_list_s
*elements
,
1017 gchar
**req
, **req_orig
;
1019 gboolean a_target
= FALSE
;
1021 req
= split_input_line(path
, "\t", 0);
1024 req
= split_input_line(path
, " ", 0);
1027 return GPG_ERR_SYNTAX
;
1030 req_orig
= g_strdupv(req
);
1033 rc
= GPG_ERR_ENOMEM
;
1037 n
= find_root_element(doc
, &req
, &rc
, &a_target
, 0, FALSE
);
1039 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
1046 if (a_target
== TRUE
) {
1048 *req
= g_strdup(*req_orig
);
1052 gboolean e_target
= FALSE
;
1054 n
= find_elements(doc
, n
->children
, req
+1, &rc
, &e_target
, NULL
, NULL
, TRUE
, 0, NULL
, FALSE
);
1056 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
1064 if (!elements
->prefix
) {
1068 * If any req_orig element contains no target the element should be
1069 * prefixed with the literal character. Not really crucial if the
1070 * client isn't human because child elements are prefixed for the
1071 * current path. But may be confusing if editing by hand.
1073 elements
->prefix
= g_strjoinv("\t", req_orig
);
1075 if (!elements
->prefix
) {
1076 rc
= GPG_ERR_ENOMEM
;
1080 if (elements
->verbose
) {
1081 if (strv_printf(&elements
->elements
, "%s%s", elements
->prefix
, find_element_node(n
->children
) ? " 1" : " 0") == FALSE
) {
1082 rc
= GPG_ERR_ENOMEM
;
1086 else if (strv_printf(&elements
->elements
, "%s", elements
->prefix
) == FALSE
) {
1087 rc
= GPG_ERR_ENOMEM
;
1091 if (update_element_list(elements
) == FALSE
) {
1092 rc
= GPG_ERR_ENOMEM
;
1097 rc
= path_list_recurse(doc
, n
->children
, elements
);
1101 g_strfreev(req_orig
);
1107 gpg_error_t
recurse_xpath_nodeset(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
1108 xmlChar
*value
, xmlBufferPtr
*result
, gboolean cmd
, const xmlChar
*attr
)
1110 gint i
= value
? nodes
->nodeNr
- 1 : 0;
1113 buf
= xmlBufferCreate();
1116 return GPG_ERR_ENOMEM
;
1118 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++) {
1119 xmlNodePtr n
= nodes
->nodeTab
[i
];
1125 if (!value
&& !attr
) {
1126 if (xmlNodeDump(buf
, doc
, n
, 0, 0) == -1) {
1128 return EPWMD_LIBXML_ERROR
;
1135 xmlNodeSetContent(n
, value
);
1136 rc
= update_element_mtime(n
);
1143 rc
= add_attribute(n
, (gchar
*)attr
, (gchar
*)value
);
1145 rc
= delete_attribute(n
, attr
);
1156 /* Common between a version 1 data file and version < 2.12. */
1157 static gpg_error_t
convert_root_element(xmlNodePtr n
)
1159 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
1164 xmlChar
*t
= xmlGetNodePath(n
);
1166 log_write(_("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"), t
);
1168 return GPG_ERR_AMBIGUOUS_NAME
;
1171 a
= xmlGetProp(n
, (xmlChar
*)"name");
1174 rc
= add_attribute(n
, "_name", (gchar
*)a
);
1180 rc
= delete_attribute(n
, (xmlChar
*)"name");
1185 xmlNodeSetName(n
, (xmlChar
*)"element");
1191 /* For a version 1 data file. */
1192 static gpg_error_t
convert_elements_recurse_v1(xmlNodePtr n
)
1194 for (; n
; n
= n
->next
) {
1197 if (n
->type
== XML_ELEMENT_NODE
&&
1198 !xmlStrEqual(n
->name
, (xmlChar
*)"_element")) {
1199 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
1203 xmlChar
*t
= xmlGetNodePath(n
);
1205 log_write(_("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"), t
);
1207 return GPG_ERR_AMBIGUOUS_NAME
;
1210 rc
= add_attribute(n
, "_name", (gchar
*)n
->name
);
1215 xmlNodeSetName(n
, (xmlChar
*)"element");
1218 rc
= convert_elements_recurse_v1(n
->children
);
1227 /* Updates the DTD and renames the root "accounts" and "account" elements from
1228 * a version 1 data file. */
1229 gpg_error_t
convert_xml(gchar
**xml
, goffset
*len
)
1231 gpg_error_t rc
= EPWMD_LIBXML_ERROR
;
1232 xmlDocPtr doc
, new = NULL
;
1235 doc
= xmlReadMemory(*xml
, *len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1238 return EPWMD_LIBXML_ERROR
;
1242 n
= xmlDocGetRootElement(doc
);
1243 xmlNodeSetName(n
, (xmlChar
*)"pwmd");
1245 for (n
= n
->children
; n
; n
= n
->next
) {
1246 if (xmlStrEqual(n
->name
, (xmlChar
*)"account")) {
1247 rc
= convert_root_element(n
);
1254 rc
= convert_elements_recurse_v1(n
->children
);
1268 n
= xmlDocGetRootElement(doc
);
1269 xmlDocSetRootElement(new, n
);
1270 xmlDocDumpMemory(new, (xmlChar
**)xml
, (gint
*)len
);
1271 xmlDocSetRootElement(new, xmlCopyNode(n
, 0));
1282 gpg_error_t
delete_attribute(xmlNodePtr n
, const xmlChar
*name
)
1286 if ((a
= xmlHasProp(n
, name
)) == NULL
)
1287 return GPG_ERR_NOT_FOUND
;
1289 if (xmlRemoveProp(a
) == -1)
1290 return EPWMD_LIBXML_ERROR
;
1292 return update_element_mtime(n
);
1295 static gpg_error_t
convert_elements_recurse(xmlDocPtr doc
, xmlNodePtr n
,
1302 for (n
= n
->children
; n
; n
= n
->next
) {
1303 if (n
->type
== XML_ELEMENT_NODE
) {
1307 if (xmlStrEqual(n
->name
, (xmlChar
*)"element")) {
1308 xmlChar
*t
= xmlGetNodePath(n
);
1310 log_write(_("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"), t
);
1312 return GPG_ERR_AMBIGUOUS_NAME
;
1315 a
= xmlGetProp(n
, (xmlChar
*)"_name");
1319 xmlChar
*t
= xmlGetNodePath(n
);
1321 log_write(_("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"), t
);
1323 return GPG_ERR_AMBIGUOUS_NAME
;
1326 xmlChar
*tmp
= xmlStrdup(n
->name
);
1329 return GPG_ERR_ENOMEM
;
1331 xmlNodeSetName(n
, (xmlChar
*)"element");
1332 rc
= add_attribute(n
, "_name", (gchar
*)tmp
);
1339 rc
= convert_root_element(n
);
1347 rc
= convert_elements_recurse(doc
, n
, depth
);
1357 /* Renames ALL elements to the new "element" name. Existing element names are
1358 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1359 * elements can contain common characters that the XML parser barfs on (an
1360 * email address for example. */
1361 gpg_error_t
convert_elements(xmlDocPtr doc
)
1363 xmlNodePtr n
= xmlDocGetRootElement(doc
);
1366 log_write(_("Converting pre 2.12 data file..."));
1367 rc
= convert_elements_recurse(doc
, n
, 0);
1370 log_write(_("Finished converting. Please SAVE to update."));
1375 gpg_error_t
validate_import(xmlNodePtr node
)
1382 for (xmlNodePtr n
= node
; n
; n
= n
->next
) {
1383 if (n
->type
== XML_ELEMENT_NODE
) {
1384 if (xmlStrEqual(n
->name
, (xmlChar
*)"element")) {
1385 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
1388 xmlChar
*t
= xmlGetNodePath(n
);
1390 log_write(_("Missing attribute '_name' at %s."), t
);
1392 return GPG_ERR_INV_VALUE
;
1395 if (!valid_xml_element(a
)) {
1396 xmlChar
*t
= xmlGetNodePath(n
);
1398 log_write(_("'%s' is not a valid element name at %s."), a
, t
);
1401 return GPG_ERR_INV_VALUE
;
1407 xmlChar
*t
= xmlGetNodePath(n
);
1409 log_write(_("Warning: unknown element '%s' at %s. Ignoring."), n
->name
, t
);
1416 rc
= validate_import(n
->children
);
1426 gpg_error_t
update_element_mtime(xmlNodePtr n
)
1428 return add_attribute(n
, NULL
, NULL
);
1431 gpg_error_t
unlink_node(xmlNodePtr n
)
1439 rc
= update_element_mtime(n
->parent
);