2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
34 #include <libxml/xmlwriter.h>
36 #include <sys/types.h>
41 #define _(msgid) gettext(msgid)
44 #include "pwmd-error.h"
45 #include "util-misc.h"
51 #define XML_LIST_FLAG_CHILDREN 0x0001
52 #define XML_LIST_HAS_TARGET 0x0002
53 #define XML_LIST_CHILD_TARGET 0x0004
54 #define XML_LIST_TARGET_ERROR 0x0008
55 #define XML_LIST_CHECK 0x0010
57 const char *reserved_attributes
[] = {
58 "_name", "_mtime", "_ctime", "_acl", "target",
62 extern void log_write (const char *fmt
, ...);
65 xml_valid_attribute_value (const char *str
)
82 xml_valid_attribute (const char *str
)
86 int ret
= xml_valid_element ((xmlChar
*)str
);
91 wc
= str_to_wchar ((const char *)str
);
96 for (c
= 0; c
< len
; c
++)
104 case 0x0300 ... 0x036F:
105 case 0x203F ... 0x2040:
113 case 'A' ... 'Z': break;
114 case 'a' ... 'z': break;
115 case 0xC0 ... 0xD6: break;
116 case 0xD8 ... 0xF6: break;
117 case 0xF8 ... 0x2FF: break;
118 case 0x370 ... 0x37D: break;
119 case 0x37F ... 0x1FFF: break;
120 case 0x200C ... 0x200D: break;
121 case 0x2070 ... 0x218F: break;
122 case 0x2C00 ... 0x2FEF: break;
123 case 0x3001 ... 0xD7FF: break;
124 case 0xF900 ... 0xFDCF: break;
125 case 0xFDF0 ... 0xFFFD: break;
126 case 0x10000 ... 0xEFFFF: break;
138 xml_valid_element (xmlChar
*str
)
146 wc
= str_to_wchar ((const char *)str
);
151 for (c
= 0; c
< len
; c
++)
165 xml_valid_element_path (char **path
, int with_content
)
167 char **dup
= NULL
, **p
;
172 /* Save some memory by not duplicating the element content. */
175 int i
, t
= strv_length (path
);
177 for (i
= 0; i
< t
- 1; i
++)
179 char **tmp
= xrealloc (dup
, (i
+ 2) * sizeof (char *));
188 dup
[i
] = str_dup (path
[i
]);
193 dup
= strv_dup (path
);
198 for (p
= dup
; *p
&& *(*p
); p
++)
200 if (!xml_valid_element ((xmlChar
*) * p
))
212 attr_ctime (struct client_s
*client
, xmlNodePtr n
)
214 char *buf
= str_asprintf ("%li", time (NULL
));
218 return GPG_ERR_ENOMEM
;
220 rc
= xml_add_attribute (client
, n
, "_ctime", buf
);
226 acl_check (struct client_s
*client
, xmlNodePtr n
)
228 gpg_error_t rc
= GPG_ERR_EACCES
;
229 xmlChar
*acl
= xml_attribute_value (n
, (xmlChar
*) "_acl");
230 char **users
= acl
? str_split((char *)acl
, ",", 0) : NULL
;
234 if (!acl
|| !*acl
|| !users
|| !*users
)
238 return peer_is_invoker(client
);
241 if (!peer_is_invoker(client
))
248 for (p
= users
; p
&& *p
; p
++)
251 rc
= acl_check_common (client
, *p
,
252 client
->thd
->remote
? 0 : client
->thd
->peer
->uid
,
253 client
->thd
->remote
? 0 : client
->thd
->peer
->gid
,
256 rc
= acl_check_common (client
, *p
, client
->thd
->peer
->uid
,
257 client
->thd
->peer
->gid
, &allowed
);
267 return allowed
? 0 : GPG_ERR_EACCES
;
271 create_acl_user (struct client_s
*client
)
274 if (client
->thd
->remote
)
275 return str_asprintf ("#%s", client
->thd
->tls
->fp
);
278 return get_username (client
->thd
->peer
->uid
);
282 create_new_element (struct client_s
*client
, int verify
, xmlNodePtr parent
,
283 const char *name
, xmlNodePtr
* result
)
288 // Allow any client to create a non-existing root element.
289 if (parent
->parent
->type
!= XML_DOCUMENT_NODE
)
291 rc
= acl_check(client
, parent
);
296 n
= xmlNewNode (NULL
, (xmlChar
*) "element");
298 return GPG_ERR_ENOMEM
;
300 rc
= xml_add_attribute (client
, n
, "_name", name
);
302 rc
= attr_ctime (client
, n
);
304 if (!rc
&& verify
&& parent
->parent
->type
!= XML_DOCUMENT_NODE
)
305 rc
= xml_is_element_owner (client
, parent
);
309 xmlNodePtr p
= xmlAddChild (parent
, n
);
310 char *user
= create_acl_user (client
);
315 rc
= xml_add_attribute(client
, p
, "_acl", user
);
325 xml_new_root_element (struct client_s
*client
, xmlDocPtr doc
, char *name
)
327 xmlNodePtr root
= xmlDocGetRootElement (doc
);
330 return GPG_ERR_BAD_DATA
;
332 if (!xml_valid_element ((xmlChar
*) name
))
333 return GPG_ERR_INV_VALUE
;
335 return create_new_element (client
, 0, root
, name
, NULL
);
342 xmlTextWriterPtr wr
= xmlNewTextWriterDoc (&doc
, 0);
347 if (xmlTextWriterStartDocument (wr
, NULL
, "UTF-8", "yes"))
350 if (xmlTextWriterStartDTD (wr
, (xmlChar
*) "pwmd", NULL
, NULL
) == -1)
353 if (xmlTextWriterWriteDTDElement (wr
, (xmlChar
*) "pwmd",
354 (xmlChar
*) "(element)") == -1)
357 xmlTextWriterEndDTDElement (wr
);
359 if (xmlTextWriterWriteDTDAttlist (wr
, (xmlChar
*) "element",
360 (xmlChar
*) "_name CDATA #REQUIRED") ==
364 xmlTextWriterEndDTDAttlist (wr
);
365 xmlTextWriterEndDTD (wr
);
367 if (xmlTextWriterStartElement (wr
, (xmlChar
*) "pwmd"))
370 xmlTextWriterEndElement (wr
);
371 xmlTextWriterEndDocument (wr
);
372 xmlFreeTextWriter (wr
);
376 xmlTextWriterEndDocument (wr
);
377 xmlFreeTextWriter (wr
);
385 return create_dtd ();
389 find_element_node (xmlNodePtr node
)
393 if (n
&& n
->type
== XML_ELEMENT_NODE
)
396 for (n
= node
; n
; n
= n
->next
)
398 if (n
->type
== XML_ELEMENT_NODE
)
406 xml_resolve_path (struct client_s
*client
, xmlDocPtr doc
, xmlChar
* path
,
407 char ***result
, gpg_error_t
*rc
)
410 struct xml_request_s
*req
;
412 *rc
= xml_new_request (client
, (char *)path
, XML_CMD_REALPATH
, &req
);
416 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (doc
), rc
);
421 *result
= strv_dup (req
->args
);
424 *rc
= GPG_ERR_ENOMEM
;
430 xml_free_request (req
);
435 xml_find_text_node (xmlNodePtr node
)
439 if (n
&& n
->type
== XML_TEXT_NODE
)
442 for (n
= node
; n
; n
= n
->next
)
444 if (n
->type
== XML_TEXT_NODE
)
451 /* Create an element 'path' starting at 'node'. Returns the node of the final
455 xml_create_element_path (struct client_s
*client
, struct xml_request_s
*req
,
456 xmlNodePtr node
, char **path
, gpg_error_t
*rc
)
460 if (!xml_valid_element_path (path
, 0))
462 *rc
= GPG_ERR_INV_VALUE
;
466 /* The parent node will be an XML_ELEMENT_NODE. Otherwise we would be
467 * creating element content. */
468 if (node
->type
== XML_TEXT_NODE
)
471 for (p
= path
; p
&& *p
; p
++)
475 if (node
!= xmlDocGetRootElement (req
->doc
))
477 n
= xml_find_element (client
, node
, *p
, rc
);
478 if (*rc
&& *rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
485 * If the found element has the same parent as the current element then
486 * they are siblings and the new element needs to be created as a child
487 * of the current element (node).
489 if (n
&& n
->parent
== node
->parent
)
494 *rc
= create_new_element (client
, 0, node
, *p
, &node
);
505 /* Find the first XML_ELEMENT_NODE whose _name attribute matches 'element'. */
507 xml_find_element (struct client_s
*client
, xmlNodePtr node
, const char *element
,
514 if (!node
|| !element
)
516 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
520 for (n
= node
; n
; n
= n
->next
)
522 if (n
->type
!= XML_ELEMENT_NODE
)
525 xmlChar
*a
= xml_attribute_value (n
, (xmlChar
*) "_name");
526 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
529 *rc
= acl_check (client
, n
);
530 return n
; // Not NULL since the node may be needed later
536 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
541 xml_attribute_value (xmlNodePtr n
, xmlChar
* attr
)
543 xmlAttrPtr a
= xmlHasProp (n
, attr
);
548 if (!a
->children
|| !a
->children
->content
)
551 return xmlGetProp (n
, attr
);
555 remove_non_reserved_attributes (xmlNodePtr n
)
559 for (a
= n
->properties
; a
; a
= a
->next
)
561 if (xml_reserved_attribute ((char *)a
->name
))
564 (void)xml_delete_attribute (NULL
, n
, a
->name
);
569 xml_add_attribute (struct client_s
*client
, xmlNodePtr node
, const char *name
,
576 if (client
&& name
&& !strcmp (name
, "target"))
578 rc
= xml_is_element_owner (client
, node
);
584 else if (name
&& !strcmp (name
, "expire"))
589 if (!value
|| !*value
)
590 return GPG_ERR_INV_VALUE
;
593 e
= strtoul (value
, &p
, 10);
594 if (errno
|| *p
|| e
== ULONG_MAX
|| *value
== '-')
595 return GPG_ERR_INV_VALUE
;
598 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
599 return GPG_ERR_BAD_DATA
;
601 if (client
&& name
&& !xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_acl"))
603 xmlChar
*acl
= xml_attribute_value (node
, (xmlChar
*) "_acl");
607 char *user
= create_acl_user (client
);
611 rc
= xml_add_attribute (client
, node
, (char *) "_acl", user
);
621 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
625 remove_non_reserved_attributes (node
);
627 buf
= str_asprintf ("%lu", time (NULL
));
628 rc
= xml_add_attribute (client
, node
, "_mtime", buf
);
634 append_path_to_list (struct client_s
*client
, struct element_list_s
*elements
,
635 const char *path
, gpg_error_t rc
)
638 struct string_s
*line
;
640 line
= string_new (NULL
);
642 return GPG_ERR_ENOMEM
;
644 if (rc
== GPG_ERR_ELOOP
)
645 string_append (line
, "O");
646 else if (rc
== GPG_ERR_EACCES
)
647 string_append (line
, "P");
649 string_append (line
, "E");
651 if (rc
|| elements
->target
)
652 string_prepend (line
, " ");
654 if ((!rc
|| rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
655 || rc
== GPG_ERR_ELEMENT_NOT_FOUND
) && elements
->target
)
656 string_append_printf (line
, "%sT %s",
657 (!rc
&& elements
->flags
& XML_LIST_FLAG_CHILDREN
) ? "+" : "",
659 else if (!rc
&& (elements
->flags
& XML_LIST_FLAG_CHILDREN
))
660 string_prepend (line
, " +");
662 if (elements
->prefix
&& elements
->prefix
->str
)
663 string_prepend (line
, elements
->prefix
->str
);
665 string_prepend (line
, path
);
667 l
= slist_append (elements
->list
, line
->str
);
671 if (!(elements
->flags
& XML_LIST_CHECK
))
677 string_free (line
, 0);
681 /* Removes the final element from the element prefix. */
683 pop_element_prefix (struct element_list_s
*elements
)
685 char *p
= strrchr (elements
->prefix
->str
, '\t');
688 string_truncate (elements
->prefix
,
689 elements
->prefix
->len
- strlen (p
));
692 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
693 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
696 * From the element path 'path', find sub-nodes and append them to the list.
699 xml_create_path_list (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
700 struct element_list_s
*elements
, char *path
,
704 struct xml_request_s
*req
= NULL
;
705 gpg_error_t trc
= rc
;
708 target_orig
= rc
&& elements
->target
? str_dup (elements
->target
) : NULL
;
709 rc
= xml_new_request (client
, path
, XML_CMD_LIST
, &req
);
716 req
->data
= elements
;
717 n
= xml_find_elements (client
, req
,
718 node
? node
: xmlDocGetRootElement (doc
), &rc
);
721 find_element_node (n
->children
) ? XML_LIST_FLAG_CHILDREN
: 0;
725 xfree (elements
->target
);
726 elements
->target
= target_orig
;
728 elements
->flags
|= XML_LIST_HAS_TARGET
;
733 rc
= append_path_to_list (client
, elements
, path
, trc
? trc
: rc
);
734 xmlFree (elements
->target
);
735 elements
->target
= NULL
;
737 if (rc
|| trc
|| (elements
->flags
& XML_LIST_CHECK
)
738 || (!elements
->recurse
&& elements
->depth
) || elements
->root_only
)
740 xml_free_request (req
);
746 if (!rc
&& n
&& n
->children
)
748 for (n
= n
->children
; n
; n
= n
->next
)
750 xmlChar
*target
= NULL
;
751 xmlNodePtr tmp
= NULL
;
753 struct string_s
*newpath
;
755 if (n
->type
!= XML_ELEMENT_NODE
)
758 name
= xml_attribute_value (n
, (xmlChar
*) "_name");
762 newpath
= string_new (NULL
);
770 target
= xml_attribute_value (n
, (xmlChar
*) "target");
773 rc
= xml_validate_target (client
, req
->doc
, (char *)target
, n
);
774 elements
->target
= (char *)target
;
775 elements
->flags
|= rc
? XML_LIST_TARGET_ERROR
: 0;
780 if (RESUMABLE_ERROR (rc
))
784 /* If there is a target for this node then resolve the full path
785 * starting at the document root. Otherwise, resolve the next
786 * element starting from the child node of the current node. In
787 * either case, append the current element in the element path to
788 * the prefix of the list string.
792 string_truncate (newpath
, 0);
793 s
= string_append_printf (newpath
, "%s", (char *)target
);
796 s
= string_append (newpath
, (char *)name
);
801 string_free (newpath
, 1);
807 if (!elements
->depth
)
809 /* There may be a single remaining element in the prefix left
810 * over from the previous truncation (see below). It is safe
811 * to truncate it here since depth == 0. */
812 string_truncate (elements
->prefix
, 0);
813 s
= string_append_printf (elements
->prefix
, "%s\t%s",
817 s
= string_append_printf (elements
->prefix
, "\t%s", name
);
822 string_free (newpath
, 1);
827 elements
->prefix
= s
;
829 rc
= xml_create_path_list (client
, doc
, target
? NULL
: tmp
,
830 elements
, newpath
->str
, rc
);
833 pop_element_prefix (elements
);
836 string_free (newpath
, 1);
844 xml_free_request (req
);
849 xml_recurse_xpath_nodeset (struct client_s
*client
, xmlDocPtr doc
,
850 xmlNodeSetPtr nodes
, xmlChar
* value
,
851 xmlBufferPtr
* result
, int cmd
, const xmlChar
* attr
)
853 int i
= value
? nodes
->nodeNr
- 1 : 0;
856 buf
= xmlBufferCreate ();
859 return GPG_ERR_ENOMEM
;
861 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
863 xmlNodePtr n
= nodes
->nodeTab
[i
];
871 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
874 return GPG_ERR_BAD_DATA
;
882 xmlNodeSetContent (n
, value
);
883 rc
= xml_update_element_mtime (client
, n
);
891 rc
= xml_add_attribute (client
, n
, (char *) attr
, (char *) value
);
893 rc
= xml_delete_attribute (client
, n
, attr
);
905 xml_delete_attribute (struct client_s
*client
, xmlNodePtr n
,
911 if ((a
= xmlHasProp (n
, name
)) == NULL
)
912 return GPG_ERR_NOT_FOUND
;
914 if (xmlRemoveProp (a
) == -1)
915 return GPG_ERR_BAD_DATA
;
917 if (client
&& xmlStrEqual (name
, (xmlChar
*) "_acl"))
919 char *user
= create_acl_user (client
);
921 rc
= xml_add_attribute (client
, n
, (char *) "_acl", user
);
928 return xml_update_element_mtime (client
, n
);
932 xml_validate_import (struct client_s
*client
, xmlNodePtr node
)
939 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
941 if (n
->type
== XML_ELEMENT_NODE
)
943 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
945 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
949 xmlChar
*t
= xmlGetNodePath (n
);
951 log_write (_("Missing attribute '_name' at %s."), t
);
953 return GPG_ERR_INV_VALUE
;
956 if (!xml_valid_element (a
))
958 xmlChar
*t
= xmlGetNodePath (n
);
960 log_write (_("'%s' is not a valid element name at %s."), a
,
964 return GPG_ERR_INV_VALUE
;
968 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
970 attr_ctime (client
, n
);
973 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
975 xml_update_element_mtime (client
, n
);
980 xmlChar
*t
= xmlGetNodePath (n
);
981 xmlNodePtr tmp
= n
->next
;
983 log_write (_("Warning: unknown element '%s' at %s. Removing."),
986 (void)xml_unlink_node (client
, n
);
987 return xml_validate_import (client
, tmp
);
993 rc
= xml_validate_import (client
, n
->children
);
1003 xml_update_element_mtime (struct client_s
*client
, xmlNodePtr n
)
1005 return xml_add_attribute (client
, n
, NULL
, NULL
);
1009 xml_unlink_node (struct client_s
*client
, xmlNodePtr n
)
1017 rc
= xml_update_element_mtime (client
, n
->parent
);
1020 xmlFreeNodeList (n
);
1025 xml_parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
1029 xmlResetLastError ();
1030 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1031 if (!doc
&& xmlGetLastError ())
1032 return GPG_ERR_BAD_DATA
;
1035 return !doc
? GPG_ERR_ENOMEM
: 0;
1038 /* Resolves the 'target' attribute found in 'node' and appends 'path' to the
1039 * target. 'path' is any remaining elements in the element path after this
1040 * element 'node' as passed to xml_find_elements() or the initial command. This
1041 * function may be recursive as it calls xml_find_elements().
1044 do_realpath (struct client_s
*client
, struct xml_request_s
*req
,
1045 xmlNodePtr node
, char **path
, const xmlChar
*target
,
1048 char **result
= NULL
;
1049 struct xml_request_s
*nreq
;
1053 result
= str_split ((char *)target
, "\t", 0);
1056 *rc
= GPG_ERR_ENOMEM
;
1060 /* Append the original path to the target path. */
1063 char **p
= strv_catv (result
, path
);
1068 *rc
= GPG_ERR_ENOMEM
;
1075 line
= strv_join ("\t", result
);
1079 *rc
= GPG_ERR_ENOMEM
;
1083 /* To prevent overwriting any original client request flags. We are only
1084 * interested in the path here. */
1085 *rc
= xml_new_request (client
, line
, XML_CMD_REALPATH
, &nreq
);
1093 nreq
->depth
= req
->depth
;
1095 node
= xml_find_elements (client
, nreq
, xmlDocGetRootElement (nreq
->doc
), rc
);
1097 /* Update the original client request path with the resolved one. */
1100 strv_free (req
->args
);
1101 req
->args
= nreq
->args
;
1105 xml_free_request (nreq
);
1111 node_to_element_path (xmlNodePtr node
)
1114 struct string_s
*str
= string_new ("");
1117 for (n
= node
; n
; n
= n
->parent
)
1121 for (child
= n
; child
; child
= child
->next
)
1123 if (child
->type
!= XML_ELEMENT_NODE
)
1126 xmlChar
*name
= xml_attribute_value (n
, (xmlChar
*) "_name");
1129 str
= string_prepend (str
, (char *) name
);
1131 name
= xml_attribute_value (n
, (xmlChar
*) "target");
1133 str
= string_prepend (str
, "\t");
1135 str
= string_prepend (str
, "\t!");
1142 str
= string_erase (str
, 0, 1);
1144 string_free (str
, 0);
1149 /* Tests if 'path' references 'node' somewhere. */
1151 element_related (struct client_s
*client
, xmlDocPtr doc
, const char *path
,
1152 xmlNodePtr node
, unsigned depth
)
1154 gpg_error_t rc
= GPG_ERR_NO_DATA
;
1156 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1158 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
1159 return GPG_ERR_ELOOP
;
1161 p
= str_split (path
, "\t", 0);
1163 return GPG_ERR_ENOMEM
;
1165 for (n
= n
->children
; n
&& *p
; p
++, n
= n
->next
)
1169 struct string_s
*str
= NULL
;
1171 t
= xml_find_element (client
, n
, *p
, &rc
);
1174 rc
= GPG_ERR_NO_DATA
;
1184 target
= xml_attribute_value (t
, (xmlChar
*)"target");
1188 str
= string_new ((char *)target
);
1192 rc
= GPG_ERR_ENOMEM
;
1200 struct string_s
*tmp
;
1202 tmp
= string_append_printf (str
, "\t%s", *p
);
1205 string_free (str
, 1);
1207 return GPG_ERR_ENOMEM
;
1214 rc
= element_related (client
, doc
, str
->str
, n
->children
, ++depth
);
1215 if (rc
== GPG_ERR_ELOOP
)
1216 rc
= GPG_ERR_NO_DATA
;
1218 string_free(str
, 1);
1227 * Recurse the element tree beginning at 'node' and find elements who point
1228 * back to 'dst'. Also follows target attributes.
1231 find_child_to_target (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1232 xmlNodePtr dst
, unsigned depth
, int is_target
)
1237 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
1238 return node
== dst
? GPG_ERR_ELOOP
: 0;
1240 for (n
= node
; n
; n
= n
->next
)
1244 if (n
->type
!= XML_ELEMENT_NODE
)
1247 if (n
== dst
&& depth
)
1248 return GPG_ERR_ELOOP
;
1250 target
= xml_attribute_value (n
, (xmlChar
*) "target");
1254 char **result
= NULL
;
1256 tmp
= xml_resolve_path (client
, doc
, target
, &result
, &rc
);
1260 rc
= find_child_to_target (client
, doc
, tmp
, dst
, ++depth
, 1);
1263 else if (rc
== GPG_ERR_ELOOP
)
1265 gpg_error_t trc
= element_related (client
, doc
, (char *)target
,
1268 if (trc
&& trc
!= GPG_ERR_NO_DATA
)
1270 else if (trc
== GPG_ERR_NO_DATA
)
1276 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1288 rc
= find_child_to_target (client
, doc
, n
->children
, dst
, ++depth
, 0);
1302 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
1307 for (n
= src
; n
; n
= n
->next
)
1309 if (n
->type
!= XML_ELEMENT_NODE
)
1318 rc
= find_child_of_parent (doc
, n
->children
, dst
);
1325 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
1330 for (n
= node
; n
; n
= n
->parent
)
1332 if (n
->type
!= XML_ELEMENT_NODE
)
1336 for (tmp
= n
->next
; tmp
; n
= n
->next
)
1338 if (n
->type
!= XML_ELEMENT_NODE
)
1342 return GPG_ERR_ELOOP
;
1347 return GPG_ERR_ELOOP
;
1354 xml_free_element_list (struct element_list_s
*elements
)
1360 int total
= slist_length (elements
->list
);
1363 for (i
= 0; i
< total
; i
++)
1365 char *tmp
= slist_nth_data (elements
->list
, i
);
1369 slist_free (elements
->list
);
1372 string_free (elements
->prefix
, 1);
1373 xfree (elements
->target
);
1378 /* This behaves like the LIST command with a specified path except it returns
1382 xml_check_recursion (struct client_s
*client
, struct xml_request_s
*req
)
1385 struct element_list_s
*elements
;
1387 char **dup
= strv_dup (req
->args
);
1390 return GPG_ERR_ENOMEM
;
1393 elements
= xcalloc (1, sizeof (struct element_list_s
));
1396 rc
= GPG_ERR_ENOMEM
;
1400 path
= strv_join ("\t", dup
);
1403 rc
= GPG_ERR_ENOMEM
;
1407 elements
->flags
|= XML_LIST_CHECK
;
1408 elements
->prefix
= string_new (NULL
);
1409 if (!elements
->prefix
)
1411 rc
= GPG_ERR_ENOMEM
;
1415 rc
= xml_create_path_list (client
, req
->doc
, xmlDocGetRootElement (req
->doc
),
1419 xml_free_element_list (elements
);
1426 xml_validate_target (struct client_s
*client
, xmlDocPtr doc
, const char *src
,
1430 xmlNodePtr src_node
;
1431 char **src_req
= NULL
;
1433 src_node
= xml_resolve_path (client
, doc
, (xmlChar
*) src
, &src_req
, &rc
);
1437 client
->flags
|= FLAG_ACL_IGNORE
;
1438 /* A destination element is a child of the source element. */
1439 rc
= find_child_of_parent (doc
, src_node
->children
, dst
);
1443 /* The destination element is a parent of the source element. */
1444 rc
= find_parent_of_child (doc
, src_node
->parent
, dst
);
1448 /* A destination child element contains a target to the source element. */
1450 rc
= find_child_to_target (client
, doc
,
1451 dst
->children
? dst
->children
: dst
, dst
, 0, 0);
1456 strv_free (src_req
);
1457 client
->flags
&= ~(FLAG_ACL_IGNORE
| FLAG_ACL_ERROR
);
1461 /* The owner of the element is the first user listed in the _acl attribute
1462 * list. acl_check() should be called before calling this function. An empty
1463 * ACL is an error if the client is not an invoking_user.
1466 xml_is_element_owner (struct client_s
*client
, xmlNodePtr n
)
1468 xmlChar
*acl
= xml_attribute_value (n
, (xmlChar
*) "_acl");
1470 gpg_error_t rc
= GPG_ERR_EACCES
;
1475 return peer_is_invoker (client
);
1478 users
= str_split((char *)acl
, ",", 0);
1479 if (users
&& *users
)
1484 if (client
->thd
->remote
)
1485 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
1487 user
= get_username (client
->thd
->peer
->uid
);
1489 user
= get_username (client
->thd
->peer
->uid
);
1493 rc
= !strcasecmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
1495 rc
= !strcmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
1498 rc
= peer_is_invoker (client
);
1508 /* Find elements by matching element names in req->args starting at 'node'.
1509 * Returns the node of the final element in req->args on success or NULL on
1510 * failure and sets 'rc' to the error code. Some commands may need special
1511 * handling and will return the node of the previous found element.
1514 xml_find_elements (struct client_s
*client
, struct xml_request_s
*req
,
1515 xmlNodePtr node
, gpg_error_t
*rc
)
1518 char **p
= req
->args
;
1523 if (max_recursion_depth
>= 1 && req
->depth
> max_recursion_depth
)
1526 *rc
= GPG_ERR_ELOOP
;
1530 /* Beginning of a command/root element (<pwmd>). */
1531 if (node
== xmlDocGetRootElement (req
->doc
))
1533 if (!node
->children
) // Empty tree
1535 if (req
->cmd
== XML_CMD_STORE
)
1536 return xml_create_element_path (client
, req
, node
, req
->args
, rc
);
1538 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1543 node
= node
->children
; // Start searching at the first child of the root
1546 for (last
= n
= node
, p
= req
->args
; *p
; p
++)
1548 xmlNodePtr tmp
= NULL
;
1549 struct element_list_s
*elements
= NULL
;
1551 // FIXME should not be reached in this function?
1554 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1559 n
= xml_find_element (client
, n
, *p
, rc
);
1562 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
&& *rc
!= GPG_ERR_EACCES
)
1568 if (*rc
== GPG_ERR_EACCES
1569 && (req
->cmd
== XML_CMD_ATTR_LIST
|| req
->cmd
== XML_CMD_ATTR
1570 || req
->cmd
== XML_CMD_ATTR_EXPIRE
))
1580 /* Fixes ATTR LIST when an element does not exist and the parent
1581 * denies access to children. Fixes leaking information about the
1582 * current elements children. */
1583 if (*rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& last
!= n
&& last
!= node
)
1585 gpg_error_t trc
= acl_check (client
, last
);
1590 if (*rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1592 if (req
->cmd
== XML_CMD_STORE
|| req
->cmd
== XML_CMD_ATTR_TARGET
)
1595 n
= xml_create_element_path (client
, req
,
1597 && node
->parent
== xmlDocGetRootElement (req
->doc
)
1598 ? node
->parent
: last
, p
, rc
);
1604 if (req
->cmd
!= XML_CMD_LIST
)
1610 // Fall through to let LIST flags be appended.
1615 /* Follow a "target" attribute for each one found in each element. */
1616 xmlChar
*target
= xml_attribute_value (n
, (xmlChar
*) "target");
1617 if (!target
|| !*target
)
1627 /* This is the final element in req->args. Done. */
1638 if (req
->cmd
== XML_CMD_REALPATH
)
1640 n
= do_realpath (client
, req
, n
, p
+1, target
, rc
);
1645 else if (req
->cmd
== XML_CMD_DELETE
|| req
->cmd
== XML_CMD_RENAME
1646 || req
->cmd
== XML_CMD_MOVE
|| req
->cmd
== XML_CMD_ATTR
1647 || req
->cmd
== XML_CMD_ATTR_TARGET
1648 || req
->cmd
== XML_CMD_ATTR_LIST
)
1650 /* No more elements to resolve. Return the node with the target. */
1658 else if (req
->cmd
== XML_CMD_LIST
)
1660 elements
= req
->data
;
1661 if (elements
&& !(elements
->flags
& XML_LIST_CHILD_TARGET
))
1663 /* No further elements. Use this target in the list flags. */
1665 elements
->flags
|= XML_LIST_HAS_TARGET
;
1668 /* Prevent replacing any following target value with the
1669 * current target value. */
1675 struct element_list_s
*e
= xcalloc (1, sizeof (struct element_list_s
));
1677 e
->target
= str_dup ((char *)target
);
1678 e
->flags
|= XML_LIST_CHILD_TARGET
;
1681 e
->flags
|= XML_LIST_HAS_TARGET
;
1683 req
->data
= elements
= e
;
1686 *rc
= xml_validate_target (client
, req
->doc
, (char *)target
, n
);
1687 /* Ignore errors for elements elsewhere in the document. */
1688 if (*rc
== GPG_ERR_EACCES
)
1695 /* Create a new path based on the value of the current "target"
1697 char **nargs
= str_split ((char *)target
, "\t", 0);
1701 *rc
= GPG_ERR_INV_VALUE
;
1702 req
->data
= req
->cmd
== XML_CMD_LIST
? elements
: req
->data
;
1707 /* Append the remaining elements to the target attributes path. */
1708 char **nnargs
= strv_catv (nargs
, p
+1);
1710 char **orig
= req
->args
;
1712 tmp
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
1718 if (req
->cmd
== XML_CMD_LIST
&& elements
)
1720 /* A child node contained a target. Use this value rather than the
1721 * current target value as the target value for the list flag. */
1722 if (req
->data
&& req
->data
!= elements
)
1724 struct element_list_s
*e
= req
->data
;
1727 target
= xmlStrdup ((xmlChar
*)e
->target
);
1730 if ((e
->flags
& XML_LIST_HAS_TARGET
))
1731 elements
->flags
|= XML_LIST_HAS_TARGET
;
1736 req
->data
= elements
;
1739 if (!(elements
->flags
& XML_LIST_TARGET_ERROR
))
1741 xfree (elements
->target
);
1742 elements
->target
= NULL
;
1744 /* Restore the original target flag. */
1745 if ((elements
->flags
& XML_LIST_HAS_TARGET
))
1746 elements
->target
= str_dup ((char *)target
);
1753 return *rc
? NULL
: tmp
;
1761 xml_new_request (struct client_s
*client
, const char *line
, xml_command_t cmd
,
1762 struct xml_request_s
**result
)
1764 struct xml_request_s
*req
;
1765 char **pp
= str_split ((char *) line
, "\t", 0);
1770 return GPG_ERR_SYNTAX
;
1773 req
= xcalloc (1, sizeof (struct xml_request_s
));
1777 return GPG_ERR_ENOMEM
;
1782 req
->doc
= client
? client
->doc
: NULL
;
1788 xml_free_request (struct xml_request_s
*r
)
1793 strv_free (r
->args
);
1798 xml_reserved_attribute (const char *name
)
1802 for (i
= 0; reserved_attributes
[i
]; i
++)
1804 if (!strcmp (name
, reserved_attributes
[i
]))