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
30 #include <libxml/xmlwriter.h>
36 #include "pwmd_error.h"
40 static xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
);
43 * 'element' must be allocated.
45 gboolean
is_literal_element(gchar
**element
)
49 if (!element
|| !*element
)
52 if (*(*element
) == '!') {
55 for (p
= *element
, c
= p
+1; *c
; c
++)
66 * Fails if 'element' begins with punctuation or digit or contains whitespace.
68 * I'm not sure about using g_unichar_isspace() rather than isspace()?
70 gboolean
valid_xml_element(xmlChar
*element
)
74 gchar
*p
= (gchar
*)element
;
76 if (!element
|| !*element
)
79 len
= g_utf8_strlen(p
, -1) - 1;
80 c
= g_utf8_get_char(p
++);
82 if (g_unichar_ispunct(c
) == TRUE
|| g_unichar_isdigit(c
) == TRUE
||
83 g_unichar_isspace(c
) == TRUE
)
87 c
= g_utf8_get_char(p
++);
89 if (g_unichar_isspace(c
))
96 gpg_error_t
new_root_element(xmlDocPtr doc
, gchar
*name
)
98 xmlNodePtr root
= xmlDocGetRootElement(doc
);
104 return EPWMD_LIBXML_ERROR
;
109 if (!valid_xml_element((xmlChar
*)p
))
110 return EPWMD_INVALID_ELEMENT
;
112 n
= xmlNewNode(NULL
, (xmlChar
*)"root");
113 n
= xmlAddChild(root
, n
);
114 a
= xmlNewProp(n
, (xmlChar
*)"name", (xmlChar
*)p
);
118 xmlDocPtr
create_dtd()
121 xmlTextWriterPtr wr
= xmlNewTextWriterDoc(&doc
, 0);
126 if (xmlTextWriterStartDocument(wr
, NULL
, NULL
, NULL
))
129 if (xmlTextWriterStartDTD(wr
, (xmlChar
*)"pwmd", NULL
, NULL
) == -1)
132 if (xmlTextWriterWriteDTDElement(wr
, (xmlChar
*)"pwmd",
133 (xmlChar
*)"(root)") == -1)
136 xmlTextWriterEndDTDElement(wr
);
138 if (xmlTextWriterWriteDTDAttlist(wr
, (xmlChar
*)"root",
139 (xmlChar
*)"name CDATA #REQUIRED") == -1)
142 xmlTextWriterEndDTDAttlist(wr
);
143 xmlTextWriterEndDTD(wr
);
145 if (xmlTextWriterStartElement(wr
, (xmlChar
*)"pwmd"))
148 xmlTextWriterEndElement(wr
);
149 xmlTextWriterEndDocument(wr
);
150 xmlFreeTextWriter(wr
);
154 xmlTextWriterEndDocument(wr
);
155 xmlFreeTextWriter(wr
);
160 xmlChar
*new_document()
164 xmlDocPtr doc
= create_dtd();
169 xmlDocDumpMemory(doc
, &xml
, &len
);
175 * Lists root element names; the value of the attribute "name" of an element
176 * "root". If there's a target attribute both literal and non-literal element
177 * names will be added. This is the primary reason why XML entities cannot be
178 * used. There wouldn't be a way to get the literal an non-literal element
181 gpg_error_t
list_root_elements(xmlDocPtr doc
, GString
**result
)
189 n
= xmlDocGetRootElement(doc
);
191 if (!n
|| !n
->children
)
192 return EPWMD_EMPTY_ELEMENT
;
194 for (n
= n
->children
; n
; n
= n
->next
) {
196 xmlChar
*val
, *target
;
200 if (n
->type
!= XML_ELEMENT_NODE
)
203 a
= xmlHasProp(n
, (xmlChar
*)"name");
205 if (!a
|| !a
->children
->content
)
208 val
= xmlNodeGetContent(a
->children
);
211 rc
= gpg_error_from_errno(ENOMEM
);
215 tmp
= g_strdup_printf("!%s", (gchar
*)val
);
219 rc
= gpg_error_from_errno(ENOMEM
);
223 tlist
= g_slist_append(list
, tmp
);
227 rc
= gpg_error_from_errno(ENOMEM
);
232 target
= node_has_attribute(n
, (xmlChar
*)"target");
235 gchar
*t
= g_strdup((gchar
*)val
);
240 rc
= gpg_error_from_errno(ENOMEM
);
244 tlist
= g_slist_append(list
, t
);
249 rc
= gpg_error_from_errno(ENOMEM
);
260 total
= g_slist_length(list
);
263 return EPWMD_EMPTY_ELEMENT
;
265 string
= g_string_new(NULL
);
268 rc
= gpg_error_from_errno(ENOMEM
);
272 for (i
= 0; i
< total
; i
++) {
273 gchar
*val
= g_slist_nth_data(list
, i
);
275 g_string_append_printf(string
, "%s\n", val
);
278 string
= g_string_truncate(string
, string
->len
- 1);
282 total
= g_slist_length(list
);
284 for (i
= 0; i
< total
; i
++)
285 g_free(g_slist_nth_data(list
, i
));
292 * Prevents a sibling element past the current element path with the same
295 static xmlNodePtr
find_stop_node(xmlNodePtr node
)
299 for (n
= node
->parent
->children
; n
; n
= n
->next
) {
308 * Alot like create_elements_cb() but doesn't use the last element of 'req' as
309 * content but as an element.
311 xmlNodePtr
create_target_elements_cb(xmlNodePtr node
, gchar
**path
,
312 gpg_error_t
*rc
, void *data
)
317 if (xmlStrEqual(node
->name
, (xmlChar
*)*req
))
320 for (i
= 0; req
[i
]; i
++) {
323 if ((n
= find_element(node
, req
[i
], find_stop_node(node
))) == NULL
||
324 (n
&& n
->parent
== node
->parent
)) {
325 is_literal_element(&req
[i
]);
327 if (!valid_xml_element((xmlChar
*)req
[i
])) {
328 *rc
= EPWMD_INVALID_ELEMENT
;
332 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
335 *rc
= gpg_error_from_errno(ENOMEM
);
339 node
= xmlAddChild(node
, n
);
342 *rc
= gpg_error_from_errno(ENOMEM
);
353 xmlNodePtr
find_text_node(xmlNodePtr node
)
357 if (n
&& n
->type
== XML_TEXT_NODE
)
360 for (n
= node
; n
; n
= n
->next
) {
361 if (n
->type
== XML_TEXT_NODE
)
368 xmlNodePtr
create_elements_cb(xmlNodePtr node
, gchar
**elements
,
369 gpg_error_t
*rc
, void *data
)
372 gchar
**req
= elements
;
374 if (node
->type
== XML_TEXT_NODE
)
377 if (node
->name
&& xmlStrEqual(node
->name
, (xmlChar
*)*req
))
380 for (i
= 0; req
[i
]; i
++) {
385 * Strip the first '!' if needed. If there's another, it's an
386 * rc. The syntax has already been checked before calling this
389 is_literal_element(&req
[i
]);
393 * The value of the element tree.
396 n
= find_text_node(node
->children
);
399 /* Use AddContent here to prevent overwriting any children. */
400 xmlNodeAddContent(node
, (xmlChar
*)req
[i
]);
401 else if (n
&& !*req
[i
])
402 xmlNodeSetContent(n
, NULL
);
404 xmlNodeSetContent(n
, (xmlChar
*)req
[i
]);
409 n
= find_element(node
, req
[i
], find_stop_node(node
));
412 * If the found element has the same parent as the current element,
413 * they are siblings and the new element needs to be created as a
414 * child of the current element (node).
416 if (n
&& n
->parent
== node
->parent
)
420 if (!valid_xml_element((xmlChar
*)req
[i
])) {
421 *rc
= EPWMD_INVALID_ELEMENT
;
425 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
428 *rc
= gpg_error_from_errno(ENOMEM
);
432 node
= xmlAddChild(node
, n
);
435 *rc
= gpg_error_from_errno(ENOMEM
);
446 /* The root element is really req[0]. It is need as a pointer in case there is
447 * a target attribute so it can be updated. */
448 xmlNodePtr
find_root_element(xmlDocPtr doc
, gchar
***req
, gpg_error_t
*rc
,
449 gboolean
*target
, gint recursion_depth
, gboolean stop
)
451 xmlNodePtr n
= xmlDocGetRootElement(doc
);
453 gchar
*root
= g_strdup(*req
[0]);
454 gboolean literal
= is_literal_element(&root
);
457 *rc
= gpg_error_from_errno(ENOMEM
);
464 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
465 xmlChar
*t
= xmlGetNodePath(n
);
467 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
475 if (n
->type
== XML_ELEMENT_NODE
) {
476 if (depth
== 0 && xmlStrEqual(n
->name
, (xmlChar
*)"pwmd")) {
482 if (depth
== 1 && xmlStrEqual(n
->name
, (xmlChar
*)"root")) {
483 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"name");
488 if (xmlStrEqual(content
, (xmlChar
*)root
)) {
489 gchar
**nreq
, **tmp
= NULL
;
491 if (literal
== TRUE
) {
498 content
= node_has_attribute(n
, (xmlChar
*)"target");
503 if (!content
|| stop
) {
511 if (strchr((gchar
*)content
, '\t')) {
512 nreq
= split_input_line((gchar
*)content
, "\t", 0);
520 *rc
= gpg_error_from_errno(ENOMEM
);
526 tmp
= strvcatv(nreq
, tmp
+1);
531 *rc
= gpg_error_from_errno(ENOMEM
);
539 if (strv_printf(&tmp
, "%s", content
) == FALSE
) {
542 *rc
= gpg_error_from_errno(ENOMEM
);
548 nreq
= strvcatv(tmp
, nreq
+1);
552 *rc
= gpg_error_from_errno(ENOMEM
);
562 n
= find_root_element(doc
, req
, rc
, target
, recursion_depth
, FALSE
);
574 *rc
= EPWMD_ELEMENT_NOT_FOUND
;
578 static xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
)
582 if (!node
|| !element
)
585 for (n
= node
; n
; n
= n
->next
) {
586 if (n
->type
!= XML_ELEMENT_NODE
)
592 if (xmlStrEqual(n
->name
, (xmlChar
*)element
))
599 xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
)
601 xmlAttrPtr a
= xmlHasProp(n
, attr
);
606 if (!a
->children
|| !a
->children
->content
)
609 return xmlGetProp(n
, attr
);
612 static gboolean
element_to_literal(gchar
**element
)
614 gchar
*p
= g_strdup_printf("!%s", *element
);
624 /* Resolves elements in 'req' one at a time. It's recursive in case of
625 * "target" attributes. */
626 xmlNodePtr
find_elements(xmlDocPtr doc
, xmlNodePtr node
,
627 gchar
**req
, gpg_error_t
*rc
, gboolean
*target
,
628 xmlNodePtr (*found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, gchar
**, void *),
629 xmlNodePtr (*not_found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, void *),
630 gboolean is_list_command
, gint recursion_depth
, void *data
, gboolean stop
)
632 xmlNodePtr n
, last
, last_node
;
639 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
640 xmlChar
*t
= xmlGetNodePath(node
);
642 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
649 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++) {
651 gchar
*t
= g_strdup(*p
);
655 *rc
= gpg_error_from_errno(ENOMEM
);
659 literal
= is_literal_element(&t
);
660 n
= find_element(last
, t
, NULL
);
665 return not_found_fn(found
? last_node
: last_node
->parent
, p
, rc
, data
);
667 *rc
= EPWMD_ELEMENT_NOT_FOUND
;
675 if (literal
== FALSE
) {
676 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
677 gchar
**nreq
= NULL
, **nnreq
;
680 if (is_list_command
== TRUE
) {
681 if (element_to_literal(&(*p
)) == FALSE
) {
682 *rc
= gpg_error_from_errno(ENOMEM
);
693 if (!*(p
+1) && stop
) {
698 if (strchr((gchar
*)content
, '\t') != NULL
) {
699 if ((nreq
= split_input_line((gchar
*)content
, "\t", 0)) == NULL
) {
701 *rc
= EPWMD_INVALID_ELEMENT
;
706 if ((nreq
= split_input_line((gchar
*)content
, " ", 0)) == NULL
) {
708 *rc
= EPWMD_INVALID_ELEMENT
;
714 tmp
= find_root_element(doc
, &nreq
, rc
, target
, 0, FALSE
);
722 found_fn(tmp
, nreq
, rc
, p
+1, data
);
730 nnreq
= strvcatv(nreq
+1, p
+1);
734 if (!nnreq
|| !*nnreq
) {
741 n
= find_elements(doc
, tmp
->children
, nnreq
, rc
, NULL
, found_fn
,
742 not_found_fn
, is_list_command
, recursion_depth
, data
, stop
);
745 gchar
**zz
= p
+1, **qq
= nnreq
;
747 if (g_strv_length(nnreq
) > g_strv_length(p
+1))
750 for (; *qq
&& *zz
; zz
++) {
752 *zz
= g_strdup(*qq
++);
755 *rc
= gpg_error_from_errno(ENOMEM
);
770 static gboolean
update_element_list(struct element_list_s
*elements
)
775 if (!elements
|| !elements
->elements
)
778 line
= g_strjoinv("\t", elements
->elements
);
783 g_strfreev(elements
->elements
);
784 elements
->elements
= NULL
;
785 l
= g_slist_append(elements
->list
, line
);
794 static gpg_error_t
path_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
795 struct element_list_s
*elements
)
800 for (n
= node
; n
; n
= n
->next
) {
801 xmlChar
*target
= NULL
;
803 if (n
->type
!= XML_ELEMENT_NODE
)
806 if (strv_printf(&elements
->elements
, "%s\t!%s", elements
->prefix
, n
->name
) == FALSE
)
807 return gpg_err_code_from_errno(ENOMEM
);
809 if (update_element_list(elements
) == FALSE
)
810 return gpg_err_code_from_errno(ENOMEM
);
812 target
= node_has_attribute(n
, (xmlChar
*)"target");
816 gchar
*save
= elements
->prefix
;
817 gboolean r
= elements
->resolving
;
821 if (max_recursion_depth
>= 1 && elements
->depth
> max_recursion_depth
) {
822 xmlChar
*t
= xmlGetNodePath(n
);
823 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
829 if (strv_printf(&elements
->elements
, "%s\t%s", elements
->prefix
, n
->name
) == FALSE
) {
831 return gpg_err_code_from_errno(ENOMEM
);
834 tmp
= g_strjoinv("\t", elements
->elements
);
838 return gpg_err_code_from_errno(ENOMEM
);
841 if (update_element_list(elements
) == FALSE
) {
843 return gpg_err_code_from_errno(ENOMEM
);
846 elements
->prefix
= tmp
;
847 elements
->resolving
= TRUE
;
848 rc
= create_path_list(doc
, elements
, (gchar
*)target
);
850 elements
->resolving
= r
;
853 elements
->prefix
= save
;
861 gchar
*tmp
= g_strdup_printf("%s\t!%s", elements
->prefix
, n
->name
);
862 gchar
*save
= elements
->prefix
;
865 return gpg_err_code_from_errno(ENOMEM
);
867 elements
->prefix
= tmp
;
868 rc
= path_list_recurse(doc
, n
->children
, elements
);
869 g_free(elements
->prefix
);
870 elements
->prefix
= save
;
880 gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
885 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
886 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
889 return EPWMD_LIBXML_ERROR
;
892 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
897 gpg_error_t
update_timestamp(xmlDocPtr doc
)
899 xmlNodePtr n
= xmlDocGetRootElement(doc
);
900 gchar
*t
= g_strdup_printf("%li", time(NULL
));
903 rc
= add_attribute(n
, "age", t
);
909 * From the element path 'path', find sub-nodes and append them to the list.
911 gpg_error_t
create_path_list(xmlDocPtr doc
, struct element_list_s
*elements
,
915 gchar
**req
, **req_orig
;
917 gboolean a_target
= FALSE
;
919 req
= split_input_line(path
, "\t", 0);
922 req
= split_input_line(path
, " ", 0);
925 return EPWMD_COMMAND_SYNTAX
;
928 req_orig
= g_strdupv(req
);
931 rc
= gpg_err_code_from_errno(ENOMEM
);
935 n
= find_root_element(doc
, &req
, &rc
, &a_target
, 0, FALSE
);
937 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
944 if (a_target
== TRUE
) {
946 *req
= g_strdup(*req_orig
);
950 gboolean e_target
= FALSE
;
952 n
= find_elements(doc
, n
->children
, req
+1, &rc
, &e_target
, NULL
, NULL
, TRUE
, 0, NULL
, FALSE
);
954 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
962 if (!elements
->prefix
) {
966 * If any req_orig element contains no target the element should be
967 * prefixed with the literal character. Not really crucial if the
968 * client isn't human because child elements are prefixed for the
969 * current path. But may be confusing if editing by hand.
971 elements
->prefix
= g_strjoinv("\t", req_orig
);
973 if (!elements
->prefix
) {
974 rc
= gpg_err_code_from_errno(ENOMEM
);
978 if (strv_printf(&elements
->elements
, "%s", elements
->prefix
) == FALSE
) {
979 rc
= gpg_err_code_from_errno(ENOMEM
);
983 if (update_element_list(elements
) == FALSE
) {
984 rc
= gpg_err_code_from_errno(ENOMEM
);
989 rc
= path_list_recurse(doc
, n
->children
, elements
);
993 g_strfreev(req_orig
);
999 gpg_error_t
recurse_xpath_nodeset(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
1000 xmlChar
*value
, xmlBufferPtr
*result
)
1002 gint i
= value
? nodes
->nodeNr
- 1 : 0;
1005 buf
= xmlBufferCreate();
1008 return gpg_err_code_from_errno(ENOMEM
);
1010 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++) {
1011 xmlNodePtr n
= nodes
->nodeTab
[i
];
1017 if (xmlNodeDump(buf
, doc
, n
, 0, 0) == -1) {
1019 return EPWMD_LIBXML_ERROR
;
1025 xmlNodeSetContent(n
, value
);
1032 /* Updates the DTD and renames the root "accounts" and "account" elements. */
1033 gpg_error_t
convert_xml(gchar
**xml
, goffset
*len
)
1035 gpg_error_t rc
= EPWMD_LIBXML_ERROR
;
1036 xmlDocPtr doc
, new = NULL
;
1039 doc
= xmlReadMemory(*xml
, *len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1042 return EPWMD_LIBXML_ERROR
;
1046 n
= xmlDocGetRootElement(doc
);
1047 xmlNodeSetName(n
, (xmlChar
*)"pwmd");
1049 for (n
= n
->children
; n
; n
= n
->next
) {
1050 if (xmlStrcmp(n
->name
, (xmlChar
*)"account") == 0)
1051 xmlNodeSetName(n
, (xmlChar
*)"root");
1059 n
= xmlDocGetRootElement(doc
);
1060 xmlDocSetRootElement(new, n
);
1061 xmlDocDumpMemory(new, (xmlChar
**)xml
, (gint
*)len
);
1062 xmlDocSetRootElement(new, xmlCopyNode(n
, 0));