1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 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
);
41 static xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
);
44 * 'element' must be allocated.
46 gboolean
is_literal_element(gchar
**element
)
50 if (!element
|| !*element
)
53 if (*(*element
) == '!') {
56 for (p
= *element
, c
= p
+1; *c
; c
++)
67 * Fails if 'element' begins with punctuation or digit or contains whitespace.
69 * I'm not sure about using g_unichar_isspace() rather than isspace()?
71 gboolean
valid_xml_element(xmlChar
*element
)
75 gchar
*p
= (gchar
*)element
;
77 if (!element
|| !*element
)
80 len
= g_utf8_strlen(p
, -1) - 1;
81 c
= g_utf8_get_char(p
++);
83 if (g_unichar_ispunct(c
) == TRUE
|| g_unichar_isdigit(c
) == TRUE
||
84 g_unichar_isspace(c
) == TRUE
)
88 c
= g_utf8_get_char(p
++);
90 if (g_unichar_isspace(c
))
97 gpg_error_t
new_account(xmlDocPtr doc
, gchar
*name
)
99 xmlNodePtr root
= xmlDocGetRootElement(doc
);
105 return EPWMD_LIBXML_ERROR
;
110 if (!valid_xml_element((xmlChar
*)p
))
111 return EPWMD_INVALID_ELEMENT
;
113 n
= xmlNewNode(NULL
, (xmlChar
*)"root");
114 n
= xmlAddChild(root
, n
);
115 a
= xmlNewProp(n
, (xmlChar
*)"name", (xmlChar
*)p
);
119 xmlDocPtr
create_dtd()
122 xmlTextWriterPtr wr
= xmlNewTextWriterDoc(&doc
, 0);
127 if (xmlTextWriterStartDocument(wr
, NULL
, NULL
, NULL
))
130 if (xmlTextWriterStartDTD(wr
, (xmlChar
*)"pwmd", NULL
, NULL
) == -1)
133 if (xmlTextWriterWriteDTDElement(wr
, (xmlChar
*)"pwmd",
134 (xmlChar
*)"(root)") == -1)
137 xmlTextWriterEndDTDElement(wr
);
139 if (xmlTextWriterWriteDTDAttlist(wr
, (xmlChar
*)"root",
140 (xmlChar
*)"name CDATA #REQUIRED") == -1)
143 xmlTextWriterEndDTDAttlist(wr
);
144 xmlTextWriterEndDTD(wr
);
146 if (xmlTextWriterStartElement(wr
, (xmlChar
*)"pwmd"))
149 xmlTextWriterEndElement(wr
);
150 xmlTextWriterEndDocument(wr
);
151 xmlFreeTextWriter(wr
);
155 xmlTextWriterEndDocument(wr
);
156 xmlFreeTextWriter(wr
);
161 xmlChar
*new_document()
165 xmlDocPtr doc
= create_dtd();
170 xmlDocDumpMemory(doc
, &xml
, &len
);
176 * Lists root account element names. If there's a target attribute both
177 * literal and non-literal element names will be added.
179 gpg_error_t
list_accounts(xmlDocPtr doc
, GString
**result
)
187 n
= xmlDocGetRootElement(doc
);
189 if (!n
|| !n
->children
)
190 return EPWMD_EMPTY_ELEMENT
;
192 for (n
= n
->children
; n
; n
= n
->next
) {
194 xmlChar
*val
, *target
;
198 if (n
->type
!= XML_ELEMENT_NODE
)
201 a
= xmlHasProp(n
, (xmlChar
*)"name");
203 if (!a
|| !a
->children
->content
)
206 val
= xmlNodeGetContent(a
->children
);
209 rc
= gpg_error_from_errno(ENOMEM
);
213 tmp
= g_strdup_printf("!%s", (gchar
*)val
);
217 rc
= gpg_error_from_errno(ENOMEM
);
221 tlist
= g_slist_append(list
, tmp
);
225 rc
= gpg_error_from_errno(ENOMEM
);
230 target
= node_has_attribute(n
, (xmlChar
*)"target");
233 gchar
*t
= g_strdup((gchar
*)val
);
238 rc
= gpg_error_from_errno(ENOMEM
);
242 tlist
= g_slist_append(list
, t
);
247 rc
= gpg_error_from_errno(ENOMEM
);
258 total
= g_slist_length(list
);
261 return EPWMD_EMPTY_ELEMENT
;
263 string
= g_string_new(NULL
);
266 rc
= gpg_error_from_errno(ENOMEM
);
270 for (i
= 0; i
< total
; i
++) {
271 gchar
*val
= g_slist_nth_data(list
, i
);
273 g_string_append_printf(string
, "%s\n", val
);
276 string
= g_string_truncate(string
, string
->len
- 1);
280 total
= g_slist_length(list
);
282 for (i
= 0; i
< total
; i
++)
283 g_free(g_slist_nth_data(list
, i
));
290 * Prevents a sibling element past the current element path with the same
293 static xmlNodePtr
find_stop_node(xmlNodePtr node
)
297 for (n
= node
->parent
->children
; n
; n
= n
->next
) {
306 * Alot like create_elements_cb() but doesn't use the last element of 'req' as
307 * content but as an element.
309 xmlNodePtr
create_target_elements_cb(xmlNodePtr node
, gchar
**path
,
310 gpg_error_t
*rc
, void *data
)
315 if (xmlStrEqual(node
->name
, (xmlChar
*)*req
))
318 for (i
= 0; req
[i
]; i
++) {
321 if ((n
= find_element(node
, req
[i
], find_stop_node(node
))) == NULL
||
322 (n
&& n
->parent
== node
->parent
)) {
323 is_literal_element(&req
[i
]);
325 if (!valid_xml_element((xmlChar
*)req
[i
])) {
326 *rc
= EPWMD_INVALID_ELEMENT
;
330 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
333 *rc
= gpg_error_from_errno(ENOMEM
);
337 node
= xmlAddChild(node
, n
);
340 *rc
= gpg_error_from_errno(ENOMEM
);
351 xmlNodePtr
find_text_node(xmlNodePtr node
)
355 if (n
&& n
->type
== XML_TEXT_NODE
)
358 for (n
= node
; n
; n
= n
->next
) {
359 if (n
->type
== XML_TEXT_NODE
)
366 xmlNodePtr
create_elements_cb(xmlNodePtr node
, gchar
**elements
,
367 gpg_error_t
*rc
, void *data
)
370 gchar
**req
= elements
;
372 if (node
->type
== XML_TEXT_NODE
)
375 if (node
->name
&& xmlStrEqual(node
->name
, (xmlChar
*)*req
))
378 for (i
= 0; req
[i
]; i
++) {
383 * Strip the first '!' if needed. If there's another, it's an
384 * rc. The syntax has already been checked before calling this
387 is_literal_element(&req
[i
]);
391 * The value of the element tree.
394 n
= find_text_node(node
->children
);
397 /* Use AddContent here to prevent overwriting any children. */
398 xmlNodeAddContent(node
, (xmlChar
*)req
[i
]);
399 else if (n
&& !*req
[i
])
400 xmlNodeSetContent(n
, NULL
);
402 xmlNodeSetContent(n
, (xmlChar
*)req
[i
]);
407 n
= find_element(node
, req
[i
], find_stop_node(node
));
410 * If the found element has the same parent as the current element,
411 * they are siblings and the new element needs to be created as a
412 * child of the current element (node).
414 if (n
&& n
->parent
== node
->parent
)
418 if (!valid_xml_element((xmlChar
*)req
[i
])) {
419 *rc
= EPWMD_INVALID_ELEMENT
;
423 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
426 *rc
= gpg_error_from_errno(ENOMEM
);
430 node
= xmlAddChild(node
, n
);
433 *rc
= gpg_error_from_errno(ENOMEM
);
444 xmlNodePtr
find_account(xmlDocPtr doc
, gchar
***req
, gpg_error_t
*rc
,
445 gboolean
*target
, gint recursion_depth
)
447 xmlNodePtr n
= xmlDocGetRootElement(doc
);
449 gchar
*account
= g_strdup(*req
[0]);
450 gboolean literal
= is_literal_element(&account
);
453 *rc
= gpg_error_from_errno(ENOMEM
);
460 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
461 xmlChar
*t
= xmlGetNodePath(n
);
463 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
470 if (n
->type
== XML_ELEMENT_NODE
) {
471 if (depth
== 0 && xmlStrEqual(n
->name
, (xmlChar
*)"pwmd")) {
477 if (depth
== 1 && xmlStrEqual(n
->name
, (xmlChar
*)"root")) {
478 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"name");
483 if (xmlStrEqual(content
, (xmlChar
*)account
)) {
484 gchar
**nreq
, **tmp
= NULL
;
486 if (literal
== TRUE
) {
493 content
= node_has_attribute(n
, (xmlChar
*)"target");
500 if (strchr((gchar
*)content
, '\t')) {
501 nreq
= split_input_line((gchar
*)content
, "\t", 0);
509 *rc
= gpg_error_from_errno(ENOMEM
);
515 tmp
= strvcatv(nreq
, tmp
+1);
519 *rc
= gpg_error_from_errno(ENOMEM
);
527 if (strv_printf(&tmp
, "%s", content
) == FALSE
) {
529 *rc
= gpg_error_from_errno(ENOMEM
);
535 nreq
= strvcatv(tmp
, nreq
+1);
539 *rc
= gpg_error_from_errno(ENOMEM
);
551 n
= find_account(doc
, req
, rc
, target
, recursion_depth
);
561 *rc
= EPWMD_ELEMENT_NOT_FOUND
;
565 static xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
)
569 if (!node
|| !element
)
572 for (n
= node
; n
; n
= n
->next
) {
573 if (n
->type
!= XML_ELEMENT_NODE
)
579 if (xmlStrEqual(n
->name
, (xmlChar
*)element
))
586 static xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
)
588 xmlAttrPtr a
= xmlHasProp(n
, attr
);
593 if (!a
->children
|| !a
->children
->content
)
596 return xmlGetProp(n
, attr
);
599 static gboolean
element_to_literal(gchar
**element
)
601 gchar
*p
= g_strdup_printf("!%s", *element
);
611 xmlNodePtr
find_elements(xmlDocPtr doc
, xmlNodePtr node
,
612 gchar
**req
, gpg_error_t
*rc
, gboolean
*target
,
613 xmlNodePtr (*found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, gchar
**, void *),
614 xmlNodePtr (*not_found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, void *),
615 gboolean is_list_command
, gint recursion_depth
, void *data
)
617 xmlNodePtr n
, last
, last_node
;
624 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
625 xmlChar
*t
= xmlGetNodePath(node
);
627 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
634 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++) {
636 gchar
*t
= g_strdup(*p
);
640 *rc
= gpg_error_from_errno(ENOMEM
);
644 literal
= is_literal_element(&t
);
645 n
= find_element(last
, t
, NULL
);
650 return not_found_fn(found
? last_node
: last_node
->parent
, p
, rc
, data
);
652 *rc
= EPWMD_ELEMENT_NOT_FOUND
;
660 if (literal
== FALSE
) {
661 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
662 gchar
**nreq
= NULL
, **nnreq
;
665 if (is_list_command
== TRUE
) {
666 if (element_to_literal(&(*p
)) == FALSE
) {
667 *rc
= gpg_error_from_errno(ENOMEM
);
675 if (strchr((gchar
*)content
, '\t') != NULL
) {
676 if ((nreq
= split_input_line((gchar
*)content
, "\t", 0)) == NULL
) {
678 *rc
= EPWMD_INVALID_ELEMENT
;
683 if ((nreq
= split_input_line((gchar
*)content
, " ", 0)) == NULL
) {
685 *rc
= EPWMD_INVALID_ELEMENT
;
691 tmp
= find_account(doc
, &nreq
, rc
, target
, 0);
699 found_fn(tmp
, nreq
, rc
, p
+1, data
);
707 nnreq
= strvcatv(nreq
+1, p
+1);
711 if (!nnreq
|| !*nnreq
) {
721 n
= find_elements(doc
, tmp
->children
, nnreq
, rc
, NULL
, found_fn
,
722 not_found_fn
, is_list_command
, recursion_depth
, data
);
725 gchar
**zz
= p
+1, **qq
= nnreq
;
727 if (g_strv_length(nnreq
) > g_strv_length(p
+1))
730 for (; *qq
&& *zz
; zz
++) {
732 *zz
= g_strdup(*qq
++);
735 *rc
= gpg_error_from_errno(ENOMEM
);
750 static gboolean
update_element_list(struct element_list_s
*elements
)
755 if (!elements
|| !elements
->elements
)
758 line
= g_strjoinv("\t", elements
->elements
);
763 g_strfreev(elements
->elements
);
764 elements
->elements
= NULL
;
765 l
= g_slist_append(elements
->list
, line
);
774 static gpg_error_t
path_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
775 struct element_list_s
*elements
)
780 for (n
= node
; n
; n
= n
->next
) {
781 xmlChar
*target
= NULL
;
783 if (n
->type
!= XML_ELEMENT_NODE
)
786 if (strv_printf(&elements
->elements
, "%s\t!%s", elements
->prefix
, n
->name
) == FALSE
)
787 return gpg_err_code_from_errno(ENOMEM
);
789 if (update_element_list(elements
) == FALSE
)
790 return gpg_err_code_from_errno(ENOMEM
);
792 target
= node_has_attribute(n
, (xmlChar
*)"target");
796 gchar
*save
= elements
->prefix
;
797 gboolean r
= elements
->resolving
;
801 if (max_recursion_depth
>= 1 && elements
->depth
> max_recursion_depth
) {
802 xmlChar
*t
= xmlGetNodePath(n
);
803 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
809 if (strv_printf(&elements
->elements
, "%s\t%s", elements
->prefix
, n
->name
) == FALSE
) {
811 return gpg_err_code_from_errno(ENOMEM
);
814 tmp
= g_strjoinv("\t", elements
->elements
);
818 return gpg_err_code_from_errno(ENOMEM
);
821 if (update_element_list(elements
) == FALSE
) {
823 return gpg_err_code_from_errno(ENOMEM
);
826 elements
->prefix
= tmp
;
827 elements
->resolving
= TRUE
;
828 rc
= create_path_list(doc
, elements
, (gchar
*)target
);
830 elements
->resolving
= r
;
833 elements
->prefix
= save
;
841 gchar
*tmp
= g_strdup_printf("%s\t!%s", elements
->prefix
, n
->name
);
842 gchar
*save
= elements
->prefix
;
845 return gpg_err_code_from_errno(ENOMEM
);
847 elements
->prefix
= tmp
;
848 rc
= path_list_recurse(doc
, n
->children
, elements
);
849 g_free(elements
->prefix
);
850 elements
->prefix
= save
;
860 gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
865 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
866 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
869 return EPWMD_LIBXML_ERROR
;
872 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
877 gpg_error_t
update_timestamp(xmlDocPtr doc
)
879 xmlNodePtr n
= xmlDocGetRootElement(doc
);
880 gchar
*t
= g_strdup_printf("%li", time(NULL
));
883 rc
= add_attribute(n
, "age", t
);
889 * From the element path 'path', find sub-nodes and append them to the list.
891 gpg_error_t
create_path_list(xmlDocPtr doc
, struct element_list_s
*elements
,
895 gchar
**req
, **req_orig
;
897 gboolean a_target
= FALSE
;
899 req
= split_input_line(path
, "\t", 0);
902 req
= split_input_line(path
, " ", 0);
905 return EPWMD_COMMAND_SYNTAX
;
908 req_orig
= g_strdupv(req
);
911 rc
= gpg_err_code_from_errno(ENOMEM
);
915 n
= find_account(doc
, &req
, &rc
, &a_target
, 0);
917 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
924 if (a_target
== TRUE
) {
926 *req
= g_strdup(*req_orig
);
930 gboolean e_target
= FALSE
;
932 n
= find_elements(doc
, n
->children
, req
+1, &rc
, &e_target
, NULL
, NULL
, TRUE
, 0, NULL
);
934 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
942 if (!elements
->prefix
) {
946 * If any req_orig element contains no target the element should be
947 * prefixed with the literal character. Not really crucial if the
948 * client isn't human because child elements are prefixed for the
949 * current path. But may be confusing if editing by hand.
951 elements
->prefix
= g_strjoinv("\t", req_orig
);
953 if (!elements
->prefix
) {
954 rc
= gpg_err_code_from_errno(ENOMEM
);
958 if (strv_printf(&elements
->elements
, "%s", elements
->prefix
) == FALSE
) {
959 rc
= gpg_err_code_from_errno(ENOMEM
);
963 if (update_element_list(elements
) == FALSE
) {
964 rc
= gpg_err_code_from_errno(ENOMEM
);
969 rc
= path_list_recurse(doc
, n
->children
, elements
);
973 g_strfreev(req_orig
);
979 gpg_error_t
recurse_xpath_nodeset(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
980 xmlChar
*value
, xmlBufferPtr
*result
)
982 gint i
= value
? nodes
->nodeNr
- 1 : 0;
985 buf
= xmlBufferCreate();
988 return gpg_err_code_from_errno(ENOMEM
);
990 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++) {
991 xmlNodePtr n
= nodes
->nodeTab
[i
];
997 if (xmlNodeDump(buf
, doc
, n
, 0, 0) == -1) {
999 return EPWMD_LIBXML_ERROR
;
1005 xmlNodeSetContent(n
, value
);
1012 /* Updates the DTD and renames the root "accounts" and "account" elements. */
1013 gpg_error_t
convert_xml(gchar
**xml
, goffset
*len
)
1015 gpg_error_t rc
= EPWMD_LIBXML_ERROR
;
1016 xmlDocPtr doc
, new = NULL
;
1019 doc
= xmlReadMemory(*xml
, *len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1022 return EPWMD_LIBXML_ERROR
;
1026 n
= xmlDocGetRootElement(doc
);
1027 xmlNodeSetName(n
, (xmlChar
*)"pwmd");
1029 for (n
= n
->children
; n
; n
= n
->next
) {
1030 if (xmlStrcmp(n
->name
, (xmlChar
*)"account") == 0)
1031 xmlNodeSetName(n
, (xmlChar
*)"root");
1039 n
= xmlDocGetRootElement(doc
);
1040 xmlDocSetRootElement(new, n
);
1041 xmlDocDumpMemory(new, (xmlChar
**)xml
, (gint
*)len
);
1042 xmlDocSetRootElement(new, xmlCopyNode(n
, 0));