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 void log_write(const gchar
*fmt
, ...);
41 static xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
);
42 static xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
);
45 * 'element' must be allocated.
47 gboolean
is_literal_element(gchar
**element
)
51 if (!element
|| !*element
)
54 if (*(*element
) == '!') {
57 for (p
= *element
, c
= p
+1; *c
; c
++)
68 * Fails if 'element' begins with punctuation or digit or contains whitespace.
70 * I'm not sure about using g_unichar_isspace() rather than isspace()?
72 gboolean
valid_xml_element(xmlChar
*element
)
76 gchar
*p
= (gchar
*)element
;
78 if (!element
|| !*element
)
84 len
= g_utf8_strlen(p
, -1) - 1;
85 c
= g_utf8_get_char(p
++);
87 if (g_unichar_ispunct(c
) == TRUE
|| g_unichar_isdigit(c
) == TRUE
||
88 g_unichar_isspace(c
) == TRUE
)
92 c
= g_utf8_get_char(p
++);
94 if (g_unichar_isspace(c
))
101 gboolean
valid_element_path(gchar
**path
, gboolean has_value
)
105 for (p
= path
; *p
; p
++) {
107 * An empty element is valid and don't check the syntax of the
110 if (has_value
== TRUE
&& (!*(p
+1) || !*p
[0]))
113 if (valid_xml_element((xmlChar
*)*p
) == FALSE
)
120 gpg_error_t
new_account(xmlDocPtr doc
, gchar
*name
)
122 xmlNodePtr root
= xmlDocGetRootElement(doc
);
128 return EPWMD_LIBXML_ERROR
;
133 n
= xmlNewNode(NULL
, (xmlChar
*)"root");
134 n
= xmlAddChild(root
, n
);
135 a
= xmlNewProp(n
, (xmlChar
*)"name", (xmlChar
*)p
);
139 xmlDocPtr
create_dtd(xmlDocPtr doc
)
143 gboolean is_new
= TRUE
;
144 gboolean newnew
= FALSE
;
147 n
= xmlDocGetRootElement(doc
);
152 wr
= xmlNewTextWriterTree(doc
, n
, 0);
157 wr
= xmlNewTextWriterDoc(&doc
, 0);
165 if (xmlTextWriterStartDTD(wr
, (xmlChar
*)"pwmd", NULL
, NULL
) == -1)
168 if (xmlTextWriterWriteDTDElement(wr
, (xmlChar
*)"pwmd",
169 (xmlChar
*)"(root)") == -1)
172 xmlTextWriterEndDTDElement(wr
);
174 if (xmlTextWriterWriteDTDAttlist(wr
, (xmlChar
*)"root",
175 (xmlChar
*)"name CDATA #REQUIRED") == -1)
178 xmlTextWriterEndDTDAttlist(wr
);
179 xmlTextWriterEndDTD(wr
);
182 xmlTextWriterStartElement(wr
, (xmlChar
*)"pwmd");
183 xmlTextWriterEndElement(wr
);
186 xmlTextWriterEndDocument(wr
);
187 xmlFreeTextWriter(wr
);
191 xmlTextWriterEndDocument(wr
);
192 xmlFreeTextWriter(wr
);
199 xmlChar
*new_document()
203 xmlDocPtr doc
= create_dtd(NULL
);
208 xmlDocDumpMemory(doc
, &xml
, &len
);
214 * Lists root account element names. If there's a target attribute both
215 * literal and non-literal element names will be added.
217 gpg_error_t
list_accounts(xmlDocPtr doc
, GString
**result
)
225 n
= xmlDocGetRootElement(doc
);
227 if (!n
|| !n
->children
)
228 return EPWMD_EMPTY_ELEMENT
;
230 for (n
= n
->children
; n
; n
= n
->next
) {
232 xmlChar
*val
, *target
;
236 if (n
->type
!= XML_ELEMENT_NODE
)
239 a
= xmlHasProp(n
, (xmlChar
*)"name");
241 if (!a
|| !a
->children
->content
)
244 val
= xmlNodeGetContent(a
->children
);
247 rc
= gpg_error_from_errno(ENOMEM
);
251 tmp
= g_strdup_printf("!%s", (gchar
*)val
);
255 rc
= gpg_error_from_errno(ENOMEM
);
259 tlist
= g_slist_append(list
, tmp
);
263 rc
= gpg_error_from_errno(ENOMEM
);
268 target
= node_has_attribute(n
, (xmlChar
*)"target");
271 gchar
*t
= g_strdup((gchar
*)val
);
276 rc
= gpg_error_from_errno(ENOMEM
);
280 tlist
= g_slist_append(list
, t
);
285 rc
= gpg_error_from_errno(ENOMEM
);
296 total
= g_slist_length(list
);
299 return EPWMD_EMPTY_ELEMENT
;
301 string
= g_string_new(NULL
);
304 rc
= gpg_error_from_errno(ENOMEM
);
308 for (i
= 0; i
< total
; i
++) {
309 gchar
*val
= g_slist_nth_data(list
, i
);
311 g_string_append_printf(string
, "%s\n", val
);
314 string
= g_string_truncate(string
, string
->len
- 1);
318 total
= g_slist_length(list
);
320 for (i
= 0; i
< total
; i
++)
321 g_free(g_slist_nth_data(list
, i
));
327 // FIXME return a gboolean in case of memory allocation failure
328 gchar
**split_input_line(gchar
*str
, gchar
*delim
, gint n
)
333 return g_strsplit(str
, delim
, n
);
337 * Prevents a sibling element past the current element path with the same
340 static xmlNodePtr
find_stop_node(xmlNodePtr node
)
344 for (n
= node
->parent
->children
; n
; n
= n
->next
) {
353 * Alot like create_elements_cb() but doesn't use the last element of 'req' as
354 * content but as an element.
356 xmlNodePtr
create_target_elements_cb(xmlNodePtr node
, gchar
**path
,
357 gpg_error_t
*rc
, void *data
)
362 if (xmlStrEqual(node
->name
, (xmlChar
*)*req
))
365 for (i
= 0; req
[i
]; i
++) {
368 if ((n
= find_element(node
, req
[i
], find_stop_node(node
))) == NULL
||
369 (n
&& n
->parent
== node
->parent
)) {
370 is_literal_element(&req
[i
]);
371 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
374 *rc
= gpg_error_from_errno(ENOMEM
);
378 node
= xmlAddChild(node
, n
);
381 *rc
= gpg_error_from_errno(ENOMEM
);
392 xmlNodePtr
find_text_node(xmlNodePtr node
)
396 if (n
&& n
->type
== XML_TEXT_NODE
)
399 for (n
= node
; n
; n
= n
->next
) {
400 if (n
->type
== XML_TEXT_NODE
)
407 xmlNodePtr
create_elements_cb(xmlNodePtr node
, gchar
**elements
,
408 gpg_error_t
*rc
, void *data
)
411 gchar
**req
= elements
;
413 if (node
->type
== XML_TEXT_NODE
)
416 if (node
->name
&& xmlStrEqual(node
->name
, (xmlChar
*)*req
))
419 for (i
= 0; req
[i
]; i
++) {
424 * Strip the first '!' if needed. If there's another, it's an
425 * rc. The syntax has already been checked before calling this
428 is_literal_element(&req
[i
]);
432 * The value of the element tree.
435 n
= find_text_node(node
->children
);
438 /* Use AddContent here to prevent overwriting any children. */
439 xmlNodeAddContent(node
, (xmlChar
*)req
[i
]);
440 else if (n
&& !*req
[i
])
441 xmlNodeSetContent(n
, NULL
);
443 xmlNodeSetContent(n
, (xmlChar
*)req
[i
]);
448 n
= find_element(node
, req
[i
], find_stop_node(node
));
451 * If the found element has the same parent as the current element,
452 * they are siblings and the new element needs to be created as a
453 * child of the current element (node).
455 if (n
&& n
->parent
== node
->parent
)
459 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
462 *rc
= gpg_error_from_errno(ENOMEM
);
466 node
= xmlAddChild(node
, n
);
469 *rc
= gpg_error_from_errno(ENOMEM
);
480 xmlNodePtr
find_account(xmlDocPtr doc
, gchar
***req
, gpg_error_t
*rc
,
481 gboolean
*target
, gint recursion_depth
)
483 xmlNodePtr n
= xmlDocGetRootElement(doc
);
485 gchar
*account
= g_strdup(*req
[0]);
486 gboolean literal
= is_literal_element(&account
);
489 *rc
= gpg_error_from_errno(ENOMEM
);
496 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
497 xmlChar
*t
= xmlGetNodePath(n
);
499 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
506 if (n
->type
== XML_ELEMENT_NODE
) {
507 if (depth
== 0 && xmlStrEqual(n
->name
, (xmlChar
*)"pwmd")) {
513 if (depth
== 1 && xmlStrEqual(n
->name
, (xmlChar
*)"root")) {
514 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"name");
519 if (xmlStrEqual(content
, (xmlChar
*)account
)) {
520 gchar
**nreq
, **tmp
= NULL
;
522 if (literal
== TRUE
) {
529 content
= node_has_attribute(n
, (xmlChar
*)"target");
536 if (strchr((gchar
*)content
, '\t')) {
537 nreq
= split_input_line((gchar
*)content
, "\t", 0);
545 *rc
= gpg_error_from_errno(ENOMEM
);
551 tmp
= strvcatv(nreq
, tmp
+1);
555 *rc
= gpg_error_from_errno(ENOMEM
);
563 if (strv_printf(&tmp
, "%s", content
) == FALSE
) {
565 *rc
= gpg_error_from_errno(ENOMEM
);
571 nreq
= strvcatv(tmp
, nreq
+1);
575 *rc
= gpg_error_from_errno(ENOMEM
);
587 n
= find_account(doc
, req
, rc
, target
, recursion_depth
);
597 *rc
= EPWMD_ELEMENT_NOT_FOUND
;
601 static xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
)
605 if (!node
|| !element
)
608 for (n
= node
; n
; n
= n
->next
) {
609 if (n
->type
!= XML_ELEMENT_NODE
)
615 if (xmlStrEqual(n
->name
, (xmlChar
*)element
))
622 static xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
)
624 xmlAttrPtr a
= xmlHasProp(n
, attr
);
629 if (!a
->children
|| !a
->children
->content
)
632 return xmlGetProp(n
, attr
);
635 static gboolean
element_to_literal(gchar
**element
)
637 gchar
*p
= g_strdup_printf("!%s", *element
);
647 xmlNodePtr
find_elements(xmlDocPtr doc
, xmlNodePtr node
,
648 gchar
**req
, gpg_error_t
*rc
, gboolean
*target
,
649 xmlNodePtr (*found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, gchar
**, void *),
650 xmlNodePtr (*not_found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, void *),
651 gboolean is_list_command
, gint recursion_depth
, void *data
)
653 xmlNodePtr n
, last
, last_node
;
660 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
661 xmlChar
*t
= xmlGetNodePath(node
);
663 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
670 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++) {
672 gchar
*t
= g_strdup(*p
);
676 *rc
= gpg_error_from_errno(ENOMEM
);
680 literal
= is_literal_element(&t
);
681 n
= find_element(last
, t
, NULL
);
686 return not_found_fn(found
? last_node
: last_node
->parent
, p
, rc
, data
);
688 *rc
= EPWMD_ELEMENT_NOT_FOUND
;
696 if (literal
== FALSE
) {
697 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
698 gchar
**nreq
= NULL
, **nnreq
;
701 if (is_list_command
== TRUE
) {
702 if (element_to_literal(&(*p
)) == FALSE
) {
703 *rc
= gpg_error_from_errno(ENOMEM
);
711 if (strchr((gchar
*)content
, '\t') != NULL
) {
712 if ((nreq
= split_input_line((gchar
*)content
, "\t", 0)) == NULL
) {
714 *rc
= EPWMD_INVALID_ELEMENT
;
719 if ((nreq
= split_input_line((gchar
*)content
, " ", 0)) == NULL
) {
721 *rc
= EPWMD_INVALID_ELEMENT
;
727 tmp
= find_account(doc
, &nreq
, rc
, target
, 0);
735 found_fn(tmp
, nreq
, rc
, p
+1, data
);
743 nnreq
= strvcatv(nreq
+1, p
+1);
747 if (!nnreq
|| !*nnreq
) {
757 n
= find_elements(doc
, tmp
->children
, nnreq
, rc
, NULL
, found_fn
,
758 not_found_fn
, is_list_command
, recursion_depth
, data
);
761 gchar
**zz
= p
+1, **qq
= nnreq
;
763 if (g_strv_length(nnreq
) > g_strv_length(p
+1))
766 for (; *qq
&& *zz
; zz
++) {
768 *zz
= g_strdup(*qq
++);
771 *rc
= gpg_error_from_errno(ENOMEM
);
786 static gboolean
update_element_list(struct element_list_s
*elements
)
791 if (!elements
|| !elements
->elements
)
794 line
= g_strjoinv("\t", elements
->elements
);
799 g_strfreev(elements
->elements
);
800 elements
->elements
= NULL
;
801 l
= g_slist_append(elements
->list
, line
);
810 static gpg_error_t
path_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
811 struct element_list_s
*elements
)
816 for (n
= node
; n
; n
= n
->next
) {
817 xmlChar
*target
= NULL
;
819 if (n
->type
!= XML_ELEMENT_NODE
)
822 if (strv_printf(&elements
->elements
, "%s\t!%s", elements
->prefix
, n
->name
) == FALSE
)
823 return gpg_err_code_from_errno(ENOMEM
);
825 if (update_element_list(elements
) == FALSE
)
826 return gpg_err_code_from_errno(ENOMEM
);
828 target
= node_has_attribute(n
, (xmlChar
*)"target");
832 gchar
*save
= elements
->prefix
;
833 gboolean r
= elements
->resolving
;
837 if (max_recursion_depth
>= 1 && elements
->depth
> max_recursion_depth
) {
838 xmlChar
*t
= xmlGetNodePath(n
);
839 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
845 if (strv_printf(&elements
->elements
, "%s\t%s", elements
->prefix
, n
->name
) == FALSE
) {
847 return gpg_err_code_from_errno(ENOMEM
);
850 tmp
= g_strjoinv("\t", elements
->elements
);
854 return gpg_err_code_from_errno(ENOMEM
);
857 if (update_element_list(elements
) == FALSE
) {
859 return gpg_err_code_from_errno(ENOMEM
);
862 elements
->prefix
= tmp
;
863 elements
->resolving
= TRUE
;
864 rc
= create_path_list(doc
, elements
, (gchar
*)target
);
866 elements
->resolving
= r
;
869 elements
->prefix
= save
;
877 gchar
*tmp
= g_strdup_printf("%s\t!%s", elements
->prefix
, n
->name
);
878 gchar
*save
= elements
->prefix
;
881 return gpg_err_code_from_errno(ENOMEM
);
883 elements
->prefix
= tmp
;
884 rc
= path_list_recurse(doc
, n
->children
, elements
);
885 g_free(elements
->prefix
);
886 elements
->prefix
= save
;
897 * From the element path 'path', find sub-nodes and append them to the list.
899 gpg_error_t
create_path_list(xmlDocPtr doc
, struct element_list_s
*elements
,
903 gchar
**req
, **req_orig
;
905 gboolean a_target
= FALSE
;
907 req
= split_input_line(path
, "\t", 0);
910 req
= split_input_line(path
, " ", 0);
913 return EPWMD_COMMAND_SYNTAX
;
916 req_orig
= g_strdupv(req
);
919 rc
= gpg_err_code_from_errno(ENOMEM
);
923 n
= find_account(doc
, &req
, &rc
, &a_target
, 0);
925 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
932 if (a_target
== TRUE
) {
934 *req
= g_strdup(*req_orig
);
938 gboolean e_target
= FALSE
;
940 n
= find_elements(doc
, n
->children
, req
+1, &rc
, &e_target
, NULL
, NULL
, TRUE
, 0, NULL
);
942 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
950 if (!elements
->prefix
) {
954 * If any req_orig element contains no target the element should be
955 * prefixed with the literal character. Not really crucial if the
956 * client isn't human because child elements are prefixed for the
957 * current path. But may be confusing if editing by hand.
959 elements
->prefix
= g_strjoinv("\t", req_orig
);
961 if (!elements
->prefix
) {
962 rc
= gpg_err_code_from_errno(ENOMEM
);
966 if (strv_printf(&elements
->elements
, "%s", elements
->prefix
) == FALSE
) {
967 rc
= gpg_err_code_from_errno(ENOMEM
);
971 if (update_element_list(elements
) == FALSE
) {
972 rc
= gpg_err_code_from_errno(ENOMEM
);
977 rc
= path_list_recurse(doc
, n
->children
, elements
);
981 g_strfreev(req_orig
);
987 gpg_error_t
recurse_xpath_nodeset(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
988 xmlChar
*value
, xmlBufferPtr
*result
)
990 gint i
= value
? nodes
->nodeNr
- 1 : 0;
993 buf
= xmlBufferCreate();
996 return gpg_err_code_from_errno(ENOMEM
);
998 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++) {
999 xmlNodePtr n
= nodes
->nodeTab
[i
];
1005 if (xmlNodeDump(buf
, doc
, n
, 0, 0) == -1) {
1007 return EPWMD_LIBXML_ERROR
;
1013 xmlNodeSetContent(n
, value
);
1020 /* Updates the DTD and renames the root "accounts" and "account" elements. */
1021 gpg_error_t
convert_xml(gchar
**xml
, goffset
*len
)
1023 gpg_error_t rc
= EPWMD_LIBXML_ERROR
;
1027 doc
= xmlReadMemory(*xml
, *len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1030 return EPWMD_LIBXML_ERROR
;
1032 doc
= create_dtd(doc
);
1037 n
= xmlDocGetRootElement(doc
);
1038 xmlNodeSetName(n
, (xmlChar
*)"pwmd");
1040 for (n
= n
->children
; n
; n
= n
->next
) {
1041 if (xmlStrcmp(n
->name
, (xmlChar
*)"account") == 0)
1042 xmlNodeSetName(n
, (xmlChar
*)"root");
1046 xmlDocDumpMemory(doc
, (xmlChar
**)xml
, (gint
*)len
);