1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2007 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 02111-1307 USA
35 #include "pwmd_error.h"
38 void log_write(const gchar
*fmt
, ...);
39 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...);
41 gboolean
is_literal_element_str(gchar
*element
)
43 if (!element
|| !*element
)
46 return *element
== '!' ? TRUE
: FALSE
;
50 * 'element' must be allocated.
52 gboolean
is_literal_element(gchar
**element
)
56 if (!element
|| !*element
)
59 if (*(*element
) == '!') {
62 for (p
= *element
, c
= p
+1; *c
; c
++)
73 * Fails if 'element' begins with punctuation or digit or contains whitespace.
75 * I'm not sure about using g_unichar_isspace() rather than isspace()?
77 gboolean
valid_xml_element(xmlChar
*element
)
81 gchar
*p
= (gchar
*)element
;
83 if (!element
|| !*element
)
89 len
= g_utf8_strlen(p
, -1) - 1;
90 c
= g_utf8_get_char(p
++);
92 if (g_unichar_ispunct(c
) == TRUE
|| g_unichar_isdigit(c
) == TRUE
)
96 c
= g_utf8_get_char(p
++);
98 if (g_unichar_isspace(c
))
105 gboolean
valid_element_path(gchar
**path
, gboolean has_value
)
109 for (p
= path
; *p
; p
++) {
111 * An empty element is valid and don't check the syntax of the
114 if (has_value
== TRUE
&& (!*(p
+1) || !*p
[0]))
117 if (valid_xml_element((xmlChar
*)*p
) == FALSE
)
124 gpg_error_t
new_account(xmlDocPtr doc
, gchar
*name
)
126 xmlNodePtr root
= xmlDocGetRootElement(doc
);
132 return EPWMD_LIBXML_ERROR
;
134 if (is_literal_element_str(p
))
137 n
= xmlNewNode(NULL
, (xmlChar
*)"account");
138 n
= xmlAddChild(root
, n
);
139 a
= xmlNewProp(n
, (xmlChar
*)"name", (xmlChar
*)p
);
143 xmlChar
*new_document()
146 const xmlChar
*line
= (xmlChar
*)
147 "<?xml version=\"1.0\"?>\n"
148 "<!DOCTYPE accounts [\n"
149 "<!ELEMENT accounts (account*)>\n"
150 "<!ATTLIST account name CDATA #REQUIRED>\n"
154 buf
= gcry_calloc(1, xmlStrlen(line
) + 1);
155 return buf
? xmlStrcat(buf
, line
) : NULL
;
159 * The "target" attribute is ignored here.
161 gpg_error_t
list_accounts(xmlDocPtr doc
, GString
**result
)
167 gpg_error_t error
= 0;
169 n
= xmlDocGetRootElement(doc
);
171 if (!n
|| !n
->children
)
172 return EPWMD_EMPTY_ELEMENT
;
174 for (n
= n
->children
; n
; n
= n
->next
) {
176 xmlChar
*val
, *target
;
180 if (n
->type
!= XML_ELEMENT_NODE
)
183 a
= xmlHasProp(n
, (xmlChar
*)"name");
185 if (!a
|| !a
->children
->content
)
188 val
= xmlNodeGetContent(a
->children
);
191 error
= gpg_error_from_errno(ENOMEM
);
195 tmp
= g_strdup_printf("!%s", (gchar
*)val
);
199 error
= gpg_error_from_errno(ENOMEM
);
203 tlist
= g_slist_append(list
, tmp
);
207 error
= gpg_error_from_errno(ENOMEM
);
212 target
= node_has_attribute(n
, (xmlChar
*)"target");
215 gchar
*t
= g_strdup((gchar
*)val
);
219 error
= gpg_error_from_errno(ENOMEM
);
223 tlist
= g_slist_append(list
, t
);
227 error
= gpg_error_from_errno(ENOMEM
);
237 total
= g_slist_length(list
);
240 return EPWMD_EMPTY_ELEMENT
;
242 string
= g_string_new(NULL
);
245 error
= gpg_error_from_errno(ENOMEM
);
249 for (i
= 0; i
< total
; i
++) {
250 gchar
*val
= g_slist_nth_data(list
, i
);
252 g_string_append_printf(string
, "%s\n", val
);
255 string
= g_string_truncate(string
, string
->len
- 1);
259 total
= g_slist_length(list
);
261 for (i
= 0; i
< total
; i
++)
262 g_free(g_slist_nth_data(list
, i
));
268 // FIXME return a gboolean in case of memory allocation failure
269 gchar
**split_input_line(gchar
*str
, gchar
*delim
, gint n
)
274 return g_strsplit(str
, delim
, n
);
277 static gchar
**append_element_path(gchar
**dst
, gchar
**src
)
291 i
= g_strv_length(d
);
293 for (p
= src
; *p
; p
++) {
296 pa
= g_realloc(d
, (i
+ 2) * sizeof(gchar
*));
317 static xmlNodePtr
find_stop_node(xmlNodePtr node
)
321 for (n
= node
->parent
->children
; n
; n
= n
->next
) {
330 * Alot like create_elements_cb() but doesn't use the last element of 'req' as
331 * content but as an element.
333 xmlNodePtr
create_target_elements_cb(xmlNodePtr node
, gchar
**path
,
334 gpg_error_t
*error
, void *data
)
339 if (xmlStrEqual(node
->name
, (xmlChar
*)*req
))
342 for (i
= 0; req
[i
]; i
++) {
345 if ((n
= find_element(node
, req
[i
], find_stop_node(node
))) == NULL
||
346 (n
&& n
->parent
== node
->parent
)) {
347 is_literal_element(&req
[i
]);
348 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
351 *error
= gpg_error_from_errno(ENOMEM
);
355 node
= xmlAddChild(node
, n
);
358 *error
= gpg_error_from_errno(ENOMEM
);
369 xmlNodePtr
find_text_node(xmlNodePtr node
)
373 if (n
&& n
->type
== XML_TEXT_NODE
)
376 for (n
= node
; n
; n
= n
->next
) {
377 if (n
->type
== XML_TEXT_NODE
)
384 xmlNodePtr
create_elements_cb(xmlNodePtr node
, gchar
**elements
,
385 gpg_error_t
*error
, void *data
)
388 gchar
**req
= elements
;
390 if (node
->type
== XML_TEXT_NODE
)
393 if (node
->name
&& xmlStrEqual(node
->name
, (xmlChar
*)*req
))
396 for (i
= 0; req
[i
]; i
++) {
401 * Strip the first '!' if needed. If there's another, it's an
402 * error. The syntax has already been checked before calling this
405 is_literal_element(&req
[i
]);
409 * The value of the element tree.
412 n
= find_text_node(node
->children
);
415 /* Use AddContent here to prevent overwriting any children. */
416 xmlNodeAddContent(node
, (xmlChar
*)req
[i
]);
417 else if (n
&& !*req
[i
])
418 xmlNodeSetContent(n
, NULL
);
420 xmlNodeSetContent(n
, (xmlChar
*)req
[i
]);
425 n
= find_element(node
, req
[i
], find_stop_node(node
));
428 * If the found element has the same parent as the current element,
429 * they are siblings and the new element needs to be created as a
430 * child of the current element (node).
432 if (n
&& n
->parent
== node
->parent
)
436 n
= xmlNewNode(NULL
, (xmlChar
*)req
[i
]);
439 *error
= gpg_error_from_errno(ENOMEM
);
443 node
= xmlAddChild(node
, n
);
446 *error
= gpg_error_from_errno(ENOMEM
);
457 xmlNodePtr
find_account(xmlDocPtr doc
, gchar
***req
, gpg_error_t
*error
,
458 gboolean
*target
, gint recursion_depth
)
460 xmlNodePtr n
= xmlDocGetRootElement(doc
);
462 gchar
*account
= g_strdup(*req
[0]);
463 gboolean literal
= is_literal_element(&account
);
466 *error
= gpg_error_from_errno(ENOMEM
);
473 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
474 xmlChar
*t
= xmlGetNodePath(n
);
476 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
483 if (n
->type
== XML_ELEMENT_NODE
) {
484 if (depth
== 0 && xmlStrEqual(n
->name
, (xmlChar
*)"accounts")) {
490 if (depth
== 1 && xmlStrEqual(n
->name
, (xmlChar
*)"account")) {
491 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"name");
496 if (xmlStrEqual(content
, (xmlChar
*)account
)) {
497 gchar
**nreq
, **tmp
= NULL
;
499 if (literal
== TRUE
) {
504 content
= node_has_attribute(n
, (xmlChar
*)"target");
511 if (strchr((gchar
*)content
, '\t')) {
512 nreq
= split_input_line((gchar
*)content
, "\t", 0);
519 *error
= gpg_error_from_errno(ENOMEM
);
525 tmp
= append_element_path(nreq
, tmp
+1);
529 *error
= gpg_error_from_errno(ENOMEM
);
537 if (strv_printf(&tmp
, "%s", content
) == FALSE
) {
538 *error
= gpg_error_from_errno(ENOMEM
);
543 nreq
= append_element_path(tmp
, nreq
+1);
547 *error
= gpg_error_from_errno(ENOMEM
);
559 n
= find_account(doc
, req
, error
, target
, recursion_depth
);
569 *error
= EPWMD_ELEMENT_NOT_FOUND
;
573 xmlNodePtr
find_sibling(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
)
577 if (!node
|| !element
)
580 for (n
= node
; n
; n
= n
->next
) {
581 if (n
->type
!= XML_ELEMENT_NODE
)
587 if (xmlStrEqual(n
->name
, (xmlChar
*)element
))
594 xmlNodePtr
find_element(xmlNodePtr node
, gchar
*element
, xmlNodePtr stop
)
596 if (!node
|| !element
)
599 return find_sibling(node
, element
, stop
);
602 // FIXME: return xmlNodeGetContent(a->children).
603 xmlChar
*node_has_attribute(xmlNodePtr n
, xmlChar
*attr
)
605 xmlAttrPtr a
= xmlHasProp(n
, attr
);
610 if (!a
->children
|| !a
->children
->content
)
613 return a
->children
->content
;
616 static gboolean
element_to_literal(gchar
**element
)
618 gchar
*p
= g_strdup_printf("!%s", *element
);
628 xmlNodePtr
find_elements(xmlDocPtr doc
, xmlNodePtr node
,
629 gchar
**req
, gpg_error_t
*error
, gboolean
*target
,
630 xmlNodePtr (*found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, void *),
631 xmlNodePtr (*not_found_fn
)(xmlNodePtr
, gchar
**, gpg_error_t
*, void *),
632 gboolean is_list_command
, gint recursion_depth
, void *data
)
634 xmlNodePtr n
, last
, last_node
;
641 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
) {
642 xmlChar
*t
= xmlGetNodePath(node
);
644 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
651 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++) {
653 gchar
*t
= g_strdup(*p
);
657 *error
= gpg_error_from_errno(ENOMEM
);
661 literal
= is_literal_element(&t
);
662 n
= find_element(last
, t
, NULL
);
667 return not_found_fn(found
? last_node
: last_node
->parent
, p
, error
, data
);
669 *error
= EPWMD_ELEMENT_NOT_FOUND
;
677 if (literal
== FALSE
) {
678 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
679 gchar
**nreq
= NULL
, **nnreq
;
682 if (is_list_command
== TRUE
) {
683 if (element_to_literal(&(*p
)) == FALSE
) {
684 *error
= gpg_error_from_errno(ENOMEM
);
692 if (strchr((gchar
*)content
, '\t') != NULL
) {
693 if ((nreq
= split_input_line((gchar
*)content
, "\t", 0)) == NULL
) {
694 *error
= EPWMD_INVALID_ELEMENT
;
699 if ((nreq
= split_input_line((gchar
*)content
, " ", 0)) == NULL
) {
700 *error
= EPWMD_INVALID_ELEMENT
;
705 tmp
= find_account(doc
, &nreq
, error
, target
, 0);
713 found_fn(n
, nreq
, error
, data
);
715 nnreq
= append_element_path(nreq
+1, p
+1);
719 if (!nnreq
|| !*nnreq
) {
729 n
= find_elements(doc
, tmp
->children
, nnreq
, error
, NULL
, found_fn
,
730 not_found_fn
, is_list_command
, recursion_depth
, data
);
733 gchar
**zz
= p
+1, **qq
= nnreq
;
735 if (g_strv_length(nnreq
) > g_strv_length(p
+1))
738 for (; *qq
&& *zz
; zz
++) {
740 *zz
= g_strdup(*qq
++);
743 *error
= gpg_error_from_errno(ENOMEM
);
758 gboolean
node_has_child_element(xmlNodePtr node
)
765 for (n
= node
; n
; n
= n
->next
) {
766 if (n
->type
== XML_ELEMENT_NODE
)
770 return node_has_child_element(n
->children
);
776 static gboolean
update_element_list(struct element_list_s
*elements
)
781 if (!elements
|| !elements
->elements
)
784 line
= g_strjoinv("\t", elements
->elements
);
789 g_strfreev(elements
->elements
);
790 elements
->elements
= NULL
;
791 l
= g_slist_append(elements
->list
, line
);
800 static gpg_error_t
path_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
801 struct element_list_s
*elements
)
806 for (n
= node
; n
; n
= n
->next
) {
807 xmlChar
*target
= NULL
;
809 if (n
->type
!= XML_ELEMENT_NODE
)
812 target
= node_has_attribute(n
, (xmlChar
*)"target");
814 if (strv_printf(&elements
->elements
, "%s\t!%s", elements
->prefix
, n
->name
) == FALSE
)
815 return gpg_err_code_from_errno(ENOMEM
);
817 if (update_element_list(elements
) == FALSE
)
818 return gpg_err_code_from_errno(ENOMEM
);
822 gchar
*save
= elements
->prefix
;
823 gboolean r
= elements
->resolving
;
827 if (max_recursion_depth
>= 1 && elements
->depth
> max_recursion_depth
) {
828 xmlChar
*t
= xmlGetNodePath(n
);
829 log_write("%s: %s", pwmd_strerror(EPWMD_LOOP
), t
);
834 if (strv_printf(&elements
->elements
, "%s\t%s", elements
->prefix
, n
->name
) == FALSE
)
835 return gpg_err_code_from_errno(ENOMEM
);
837 tmp
= g_strjoinv("\t", elements
->elements
);
840 return gpg_err_code_from_errno(ENOMEM
);
842 if (update_element_list(elements
) == FALSE
)
843 return gpg_err_code_from_errno(ENOMEM
);
845 elements
->prefix
= tmp
;
846 elements
->resolving
= TRUE
;
847 rc
= create_path_list(doc
, elements
, (gchar
*)target
);
848 elements
->resolving
= r
;
851 elements
->prefix
= save
;
859 gchar
*tmp
= g_strdup_printf("%s\t!%s", elements
->prefix
, n
->name
);
860 gchar
*save
= elements
->prefix
;
863 return gpg_err_code_from_errno(ENOMEM
);
865 elements
->prefix
= tmp
;
866 rc
= path_list_recurse(doc
, n
->children
, elements
);
867 g_free(elements
->prefix
);
868 elements
->prefix
= save
;
879 * From the element path 'path', find sub-nodes and append them to the list.
881 gpg_error_t
create_path_list(xmlDocPtr doc
, struct element_list_s
*elements
,
885 gchar
**req
, **req_orig
;
887 gboolean a_target
= FALSE
;
889 req
= split_input_line(path
, "\t", 0);
892 req
= split_input_line(path
, " ", 0);
895 return EPWMD_COMMAND_SYNTAX
;
898 req_orig
= g_strdupv(req
);
901 rc
= gpg_err_code_from_errno(ENOMEM
);
905 n
= find_account(doc
, &req
, &rc
, &a_target
, 0);
907 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
914 if (a_target
== TRUE
) {
916 *req
= g_strdup(*req_orig
);
920 gboolean e_target
= FALSE
;
922 n
= find_elements(doc
, n
->children
, req
+1, &rc
, &e_target
, NULL
, NULL
, TRUE
, 0, NULL
);
924 if (!n
&& rc
== EPWMD_ELEMENT_NOT_FOUND
&& elements
->resolving
== TRUE
) {
932 if (!elements
->prefix
) {
936 * If any req_orig element contains no target the element should be prefixed with
937 * the literal character. Not really crucial if the client isn't human
938 * because sub-nodes/sub-elements are prefixed when needed.
940 elements
->prefix
= g_strjoinv("\t", req_orig
);
942 if (!elements
->prefix
) {
943 rc
= gpg_err_code_from_errno(ENOMEM
);
947 if (strv_printf(&elements
->elements
, "%s", elements
->prefix
) == FALSE
) {
948 rc
= gpg_err_code_from_errno(ENOMEM
);
952 if (update_element_list(elements
) == FALSE
) {
953 rc
= gpg_err_code_from_errno(ENOMEM
);
958 rc
= path_list_recurse(doc
, n
->children
, elements
);
962 g_strfreev(req_orig
);