2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
32 #include <libxml/xmlwriter.h>
34 #include <sys/types.h>
39 #define _(msgid) gettext(msgid)
42 #include "pwmd-error.h"
43 #include "util-misc.h"
49 extern void log_write (const char *fmt
, ...);
52 * 'element' must be allocated.
55 is_literal_element (char **element
)
59 if (!element
|| !*element
)
62 if (*(*element
) == '!')
66 for (p
= *element
, c
= p
+ 1; *c
; c
++)
77 valid_xml_attribute (const char *str
)
81 int ret
= valid_xml_element ((xmlChar
*)str
);
86 wc
= str_to_wchar ((const char *)str
);
91 for (c
= 0; c
< len
; c
++)
99 case 0x0300 ... 0x036F:
100 case 0x203F ... 0x2040:
108 case 'A' ... 'Z': break;
109 case 'a' ... 'z': break;
110 case 0xC0 ... 0xD6: break;
111 case 0xD8 ... 0xF6: break;
112 case 0xF8 ... 0x2FF: break;
113 case 0x370 ... 0x37D: break;
114 case 0x37F ... 0x1FFF: break;
115 case 0x200C ... 0x200D: break;
116 case 0x2070 ... 0x218F: break;
117 case 0x2C00 ... 0x2FEF: break;
118 case 0x3001 ... 0xD7FF: break;
119 case 0xF900 ... 0xFDCF: break;
120 case 0xFDF0 ... 0xFFFD: break;
121 case 0x10000 ... 0xEFFFF: break;
133 valid_xml_element (xmlChar
*str
)
138 if (!str
|| !*str
|| *str
== '!')
141 wc
= str_to_wchar ((const char *)str
);
146 for (c
= 0; c
< len
; c
++)
160 valid_element_path (char **path
, int with_content
)
162 char **dup
= NULL
, **p
;
167 /* Save some memory by not duplicating the element content. */
170 int i
, t
= strv_length (path
);
172 for (i
= 0; i
< t
- 1; i
++)
174 char **tmp
= xrealloc (dup
, (i
+ 2) * sizeof (char *));
183 dup
[i
] = str_dup (path
[i
]);
188 dup
= strv_dup (path
);
193 for (p
= dup
; *p
&& *(*p
); p
++)
195 is_literal_element (&(*p
));
196 if (!valid_xml_element ((xmlChar
*) * p
))
208 attr_ctime (struct client_s
*client
, xmlNodePtr n
)
210 char *buf
= str_asprintf ("%li", time (NULL
));
214 return GPG_ERR_ENOMEM
;
216 rc
= add_attribute (client
, n
, "_ctime", buf
);
222 acl_check (struct client_s
*client
, xmlNodePtr n
)
224 gpg_error_t rc
= GPG_ERR_EACCES
;
225 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
226 char **users
= acl
? str_split((char *)acl
, ",", 0) : NULL
;
230 if (!acl
|| !*acl
|| !users
|| !*users
)
234 return peer_is_invoker(client
);
237 if (!peer_is_invoker(client
))
240 for (p
= users
; *p
; p
++)
243 rc
= acl_check_common (client
, *p
,
244 client
->thd
->remote
? 0 : client
->thd
->peer
->uid
,
245 client
->thd
->remote
? 0 : client
->thd
->peer
->gid
,
248 rc
= acl_check_common (client
, *p
, client
->thd
->peer
->uid
,
249 client
->thd
->peer
->gid
, &allowed
);
256 // ATTR LIST makes use of FLAG_ACL_IGNORE to allow listing of element
257 // attributes that the client is not normally allowed access to.
258 if ((rc
== GPG_ERR_EACCES
|| !allowed
) && client
->flags
& FLAG_ACL_IGNORE
)
261 client
->flags
&= ~FLAG_ACL_IGNORE
;
263 // This flag is used in ATTR LIST to prevent listing attributes of
264 // children whose parent ACL does not allow access to the client.
265 client
->flags
|= FLAG_ACL_ERROR
;
272 return allowed
? 0 : GPG_ERR_EACCES
;
276 create_acl_user (struct client_s
*client
)
279 if (client
->thd
->remote
)
280 return str_asprintf ("#%s", client
->thd
->tls
->fp
);
283 return get_username (client
->thd
->peer
->uid
);
287 create_new_element (struct client_s
*client
, int verify
, xmlNodePtr parent
,
288 const char *name
, xmlNodePtr
* result
)
293 // Allow any client to create a non-existing root element.
294 if (parent
->parent
->type
!= XML_DOCUMENT_NODE
)
296 rc
= acl_check(client
, parent
);
301 n
= xmlNewNode (NULL
, (xmlChar
*) "element");
303 return GPG_ERR_ENOMEM
;
305 rc
= add_attribute (client
, n
, "_name", name
);
307 rc
= attr_ctime (client
, n
);
309 if (!rc
&& verify
&& parent
->parent
->type
!= XML_DOCUMENT_NODE
)
310 rc
= is_element_owner (client
, parent
);
314 xmlNodePtr p
= xmlAddChild (parent
, n
);
315 char *user
= create_acl_user (client
);
320 rc
= add_attribute(client
, p
, "_acl", user
);
330 new_root_element (struct client_s
*client
, xmlDocPtr doc
, char *name
)
332 xmlNodePtr root
= xmlDocGetRootElement (doc
);
336 return GPG_ERR_BAD_DATA
;
341 if (!valid_xml_element ((xmlChar
*) p
))
342 return GPG_ERR_INV_VALUE
;
344 return create_new_element (client
, 0, root
, p
, NULL
);
351 xmlTextWriterPtr wr
= xmlNewTextWriterDoc (&doc
, 0);
356 if (xmlTextWriterStartDocument (wr
, NULL
, "UTF-8", "yes"))
359 if (xmlTextWriterStartDTD (wr
, (xmlChar
*) "pwmd", NULL
, NULL
) == -1)
362 if (xmlTextWriterWriteDTDElement (wr
, (xmlChar
*) "pwmd",
363 (xmlChar
*) "(element)") == -1)
366 xmlTextWriterEndDTDElement (wr
);
368 if (xmlTextWriterWriteDTDAttlist (wr
, (xmlChar
*) "element",
369 (xmlChar
*) "_name CDATA #REQUIRED") ==
373 xmlTextWriterEndDTDAttlist (wr
);
374 xmlTextWriterEndDTD (wr
);
376 if (xmlTextWriterStartElement (wr
, (xmlChar
*) "pwmd"))
379 xmlTextWriterEndElement (wr
);
380 xmlTextWriterEndDocument (wr
);
381 xmlFreeTextWriter (wr
);
385 xmlTextWriterEndDocument (wr
);
386 xmlFreeTextWriter (wr
);
394 return create_dtd ();
398 find_element_node (xmlNodePtr node
)
402 if (n
&& n
->type
== XML_ELEMENT_NODE
)
405 for (n
= node
; n
; n
= n
->next
)
407 if (n
->type
== XML_ELEMENT_NODE
)
415 resolve_path (struct client_s
*client
, xmlDocPtr doc
, xmlChar
* path
,
416 char ***result
, gpg_error_t
* rc
)
421 req
= str_split ((char *) path
, "\t", 0);
424 *rc
= GPG_ERR_ENOMEM
;
428 n
= find_root_element (client
, doc
, &req
, rc
, NULL
, 0, 0);
436 n
= find_elements (client
, doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
, NULL
,
448 * Lists root element names; the value of the attribute "_name" of an element
449 * "element". If there's a target attribute both literal and non-literal
450 * element names will be added. This is the primary reason why XML entities
451 * cannot be used. There wouldn't be a way to get the literal an non-literal
455 list_root_elements (struct client_s
*client
, xmlDocPtr doc
,
456 struct string_s
** result
, int verbose
, int with_target
)
459 struct slist_s
*list
= NULL
;
461 struct string_s
*string
;
464 n
= xmlDocGetRootElement (doc
);
465 if (!n
|| !n
->children
)
466 return GPG_ERR_NO_DATA
;
468 for (n
= n
->children
; n
; n
= n
->next
)
471 xmlChar
*val
, *target
;
472 struct slist_s
*tlist
;
475 if (n
->type
!= XML_ELEMENT_NODE
)
478 a
= xmlHasProp (n
, (xmlChar
*) "_name");
479 if (!a
|| !a
->children
->content
)
482 val
= xmlNodeGetContent (a
->children
);
489 tmp
= str_asprintf ("!%s%s", (char *) val
,
490 verbose
? find_element_node (n
->children
) ? " +"
500 tlist
= slist_append (list
, tmp
);
509 target
= node_has_attribute (n
, (xmlChar
*) "target");
517 xmlNodePtr tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
519 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
520 || rc
== GPG_ERR_EACCES
)
522 t
= str_asprintf ("%s %s", (char *) val
,
523 rc
== GPG_ERR_ELOOP
? "O" :
524 rc
== GPG_ERR_EACCES
? "P" : "E");
529 struct string_s
*realpath
= NULL
;
533 rc
= build_realpath (client
, doc
, (char *) target
,
543 realpath
= string_prepend (realpath
, "T ");
546 t
= str_asprintf ("%s%s%s%s", (char *) val
,
548 && find_element_node (tnode
->children
))
549 || realpath
? " " : "", tnode
550 && find_element_node (tnode
->children
) ?
551 "+" : "", realpath
? realpath
->str
: "");
554 string_free (realpath
, 1);
561 t
= str_dup ((char *) val
);
567 rc
= rc
? rc
: GPG_ERR_ENOMEM
;
571 tlist
= slist_append (list
, t
);
588 total
= slist_length (list
);
590 return GPG_ERR_NO_DATA
;
592 string
= string_new (NULL
);
599 for (i
= 0; i
< total
; i
++)
601 char *val
= slist_nth_data (list
, i
);
603 string_append_printf (string
, "%s\n", val
);
606 string
= string_truncate (string
, string
->len
- 1);
610 total
= slist_length (list
);
611 for (i
= 0; i
< total
; i
++)
612 xfree (slist_nth_data (list
, i
));
619 * Prevents a sibling element past the current element path with the same
623 find_stop_node (xmlNodePtr node
)
627 for (n
= node
->parent
->children
; n
; n
= n
->next
)
637 create_target_elements_cb (struct client_s
*client
, int verify
,
638 xmlNodePtr node
, char **path
, gpg_error_t
*rc
,
643 xmlNodePtr parent
= data
;
645 for (i
= 0; req
[i
] && *req
[i
]; i
++)
649 if (parent
&& node
== parent
)
651 *rc
= GPG_ERR_CONFLICT
;
655 is_literal_element (&req
[i
]);
657 if ((n
= find_element (client
, node
, req
[i
],
658 find_stop_node (node
), rc
)) == NULL
||
659 (n
&& n
->parent
== node
->parent
))
663 *rc
= create_new_element (client
, verify
, node
, req
[i
], &node
);
676 find_text_node (xmlNodePtr node
)
680 if (n
&& n
->type
== XML_TEXT_NODE
)
683 for (n
= node
; n
; n
= n
->next
)
685 if (n
->type
== XML_TEXT_NODE
)
693 create_elements_cb (struct client_s
*client
, int verify
, xmlNodePtr node
,
694 char **elements
, gpg_error_t
* rc
, void *data
)
697 char **req
= elements
;
699 if (node
->type
== XML_TEXT_NODE
)
702 for (i
= 0; req
[i
] && *req
[i
]; i
++)
707 * Strip the first '!' if needed. If there's another, it's an
708 * rc. The syntax has already been checked before calling this
711 is_literal_element (&req
[i
]);
712 n
= find_element (client
, node
, req
[i
], find_stop_node (node
), rc
);
717 * If the found element has the same parent as the current element,
718 * they are siblings and the new element needs to be created as a
719 * child of the current element (node).
721 if (n
&& n
->parent
== node
->parent
)
726 *rc
= create_new_element (client
, 0, node
, req
[i
], &node
);
737 /* The root element is really req[0]. It is need as a pointer in case there is
738 * a target attribute so it can be updated. */
740 find_root_element (struct client_s
*client
, xmlDocPtr doc
, char ***req
,
741 gpg_error_t
* rc
, int *target
, int recursion_depth
,
744 xmlNodePtr n
= xmlDocGetRootElement (doc
);
746 char *root
= str_dup (*req
[0]);
747 int literal
= is_literal_element (&root
);
751 *rc
= GPG_ERR_ENOMEM
;
758 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
760 xmlChar
*t
= xmlGetNodePath (n
);
762 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
771 if (n
->type
== XML_ELEMENT_NODE
)
773 if (depth
== 0 && xmlStrEqual (n
->name
, (xmlChar
*) "pwmd"))
780 if (depth
== 1 && xmlStrEqual (n
->name
, (xmlChar
*) "element"))
782 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "_name");
787 if (xmlStrEqual (content
, (xmlChar
*) root
))
789 char **nreq
, **tmp
= NULL
;
790 int acl
= client
->flags
& FLAG_ACL_IGNORE
;
792 *rc
= acl_check(client
, n
);
793 if ((*rc
&& *rc
!= GPG_ERR_EACCES
)
794 || (*rc
== GPG_ERR_EACCES
&& !acl
))
804 // This flag is cleared in acl_check() but we always
805 // allow ATTR LIST of root elements.
806 client
->flags
|= FLAG_ACL_IGNORE
;
817 content
= node_has_attribute (n
, (xmlChar
*) "target");
819 if (content
&& target
)
822 if (!content
|| stop
)
831 if (strchr ((char *) content
, '\t'))
833 nreq
= str_split ((char *) content
, "\t", 0);
842 *rc
= GPG_ERR_ENOMEM
;
848 tmp
= strv_catv (nreq
, tmp
+ 1);
854 *rc
= GPG_ERR_ENOMEM
;
863 if (strv_printf (&tmp
, "%s", content
) == 0)
867 *rc
= GPG_ERR_ENOMEM
;
873 nreq
= strv_catv (tmp
, nreq
+ 1);
878 *rc
= GPG_ERR_ENOMEM
;
888 n
= find_root_element (client
, doc
, req
, rc
, target
,
901 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
906 find_element (struct client_s
*client
, xmlNodePtr node
, char *element
,
907 xmlNodePtr stop
, gpg_error_t
*rc
)
913 if (!node
|| !element
)
916 for (n
= node
; n
; n
= n
->next
)
918 if (n
->type
!= XML_ELEMENT_NODE
)
924 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
926 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
930 // Prevent ATTR LIST showing child element attributes for a parent
931 // whos ACL denies the client.
932 if (client
->flags
& FLAG_ACL_ERROR
)
934 *rc
= GPG_ERR_EACCES
;
938 *rc
= acl_check(client
, n
);
952 node_has_attribute (xmlNodePtr n
, xmlChar
* attr
)
954 xmlAttrPtr a
= xmlHasProp (n
, attr
);
959 if (!a
->children
|| !a
->children
->content
)
962 return xmlGetProp (n
, attr
);
966 element_to_literal (char **element
)
968 char *p
= str_asprintf ("!%s", *element
);
978 /* Resolves elements in 'req' one at a time. It's recursive in case of
979 * "target" attributes. */
981 find_elements (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
982 char **req
, gpg_error_t
* rc
, int *target
,
983 xmlNodePtr (*found_fn
) (struct client_s
*, xmlNodePtr
, char **,
984 gpg_error_t
*, char **, void *),
985 xmlNodePtr (*not_found_fn
) (struct client_s
*, int, xmlNodePtr
,
986 char **, gpg_error_t
*, void *),
987 int is_list_command
, int recursion_depth
, void *data
, int stop
)
989 xmlNodePtr n
, last
, last_node
;
996 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
998 xmlChar
*t
= xmlGetNodePath (node
);
1000 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1003 *rc
= GPG_ERR_ELOOP
;
1007 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++)
1015 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1022 *rc
= GPG_ERR_ENOMEM
;
1026 literal
= is_literal_element (&t
);
1027 n
= find_element (client
, last
, t
, NULL
, rc
);
1030 if (*rc
&& *rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1036 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1039 return not_found_fn (client
, 0,
1040 found
? last_node
: last_node
->parent
, p
,
1051 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "target");
1052 char **nreq
= NULL
, **nnreq
;
1056 if (is_list_command
== 1)
1058 if (element_to_literal (&(*p
)) == 0)
1060 *rc
= GPG_ERR_ENOMEM
;
1071 if (!*(p
+ 1) && stop
)
1077 if (strchr ((char *) content
, '\t') != NULL
)
1079 if ((nreq
= str_split ((char *) content
, "\t", 0)) == NULL
)
1082 *rc
= GPG_ERR_INV_VALUE
;
1088 if ((nreq
= str_split ((char *) content
, " ", 0)) == NULL
)
1091 *rc
= GPG_ERR_INV_VALUE
;
1097 tmp
= find_root_element (client
, doc
, &nreq
, rc
, target
, 0, 0);
1101 if (not_found_fn
&& *rc
== GPG_ERR_EACCES
)
1102 return not_found_fn (client
, 0, NULL
, p
, rc
, data
);
1109 found_fn (client
, tmp
, nreq
, rc
, p
+ 1, data
);
1118 if (!*(nreq
+ 1) && !*(p
+ 1))
1124 nnreq
= strv_catv (nreq
+ 1, p
+ 1);
1128 if (!nnreq
|| !*nnreq
)
1137 n
= find_elements (client
, doc
, tmp
->children
, nnreq
, rc
, NULL
,
1138 found_fn
, not_found_fn
, is_list_command
,
1139 recursion_depth
, data
, stop
);
1145 return not_found_fn (client
, 0, tmp
, p
+ 1, rc
, data
);
1147 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1153 char **zz
= p
+ 1, **qq
= nnreq
;
1155 if (strv_length (nnreq
) > strv_length (p
+ 1))
1158 for (; *qq
&& *zz
; zz
++)
1161 *zz
= str_dup (*qq
++);
1165 *rc
= GPG_ERR_ENOMEM
;
1181 update_element_list (struct element_list_s
*elements
)
1186 if (!elements
|| !elements
->elements
)
1189 line
= strv_join ("\t", elements
->elements
);
1194 strv_free (elements
->elements
);
1195 elements
->elements
= NULL
;
1196 l
= slist_append (elements
->list
, line
);
1206 path_list_recurse (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1207 struct element_list_s
*elements
)
1211 gpg_error_t error_flag
= 0;
1213 for (n
= node
; n
; n
= n
->next
)
1215 xmlChar
*target
= NULL
;
1216 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
1217 gpg_error_t err
= 0;
1224 if (n
->type
!= XML_ELEMENT_NODE
)
1227 rc
= acl_check(client
, n
);
1229 if (elements
->verbose
)
1232 (&elements
->elements
, "%s\t!%s%s%s", elements
->prefix
, a
,
1233 !rc
&& find_element_node (n
->children
) ? " +" : "",
1234 rc
== GPG_ERR_EACCES
? " P" : rc
? " E" : "") == 0)
1237 return GPG_ERR_ENOMEM
;
1241 if (strv_printf (&elements
->elements
, "%s\t!%s", elements
->prefix
, a
)
1245 return GPG_ERR_ENOMEM
;
1248 if (update_element_list (elements
) == 0)
1251 return GPG_ERR_ENOMEM
;
1254 if (rc
== GPG_ERR_EACCES
)
1266 target
= node_has_attribute (n
, (xmlChar
*) "target");
1270 char *save
= elements
->prefix
;
1271 int r
= elements
->resolving
;
1274 struct string_s
*realpath
= NULL
;
1276 tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
1277 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_ELEMENT_NOT_FOUND
1278 || rc
== GPG_ERR_EACCES
)
1280 if (rc
== GPG_ERR_ELOOP
)
1282 xmlChar
*t
= xmlGetNodePath (n
);
1284 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1288 if (elements
->verbose
)
1290 error_flag
= err
= rc
;
1294 else if (!elements
->verbose
&& rc
)
1301 path
= str_asprintf("%s\t%s", elements
->prefix
, a
);
1302 rc
= validate_target_attribute (client
, client
->doc
, path
, tnode
);
1304 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
1305 || rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1307 if (rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1308 error_flag
= err
= rc
;
1321 strv_printf (&elements
->elements
, "%s\t%s %s", elements
->prefix
,
1323 err
== GPG_ERR_ELOOP
? "O" :
1324 err
== GPG_ERR_EACCES
? "P" : "E");
1327 if (!err
&& elements
->with_target
)
1329 rc
= build_realpath (client
, doc
, (char *) target
, &realpath
);
1337 realpath
= string_prepend (realpath
, "T ");
1340 if (!err
&& elements
->verbose
)
1342 if (!strv_printf (&elements
->elements
, "%s\t%s%s%s%s",
1343 elements
->prefix
, a
,
1344 (tnode
&& find_element_node (tnode
->children
))
1345 || realpath
? " " : "", tnode
1346 && find_element_node (tnode
->children
) ? "+" :
1347 "", realpath
? realpath
->str
: ""))
1351 return GPG_ERR_ENOMEM
;
1356 (&elements
->elements
, "%s\t%s", elements
->prefix
, a
))
1360 return GPG_ERR_ENOMEM
;
1364 string_free (realpath
, 1);
1366 tmp
= strv_join ("\t", elements
->elements
);
1371 return GPG_ERR_ENOMEM
;
1374 if (update_element_list (elements
) == 0)
1379 return GPG_ERR_ENOMEM
;
1382 if (!err
&& elements
->recurse
)
1384 /* Prune element flags. */
1385 if (elements
->verbose
&& strchr (tmp
, ' '))
1389 for (p
= tmp
; *p
; p
++)
1399 elements
->prefix
= tmp
;
1400 elements
->resolving
= 1;
1401 rc
= create_path_list (client
, doc
, elements
, (char *) target
);
1402 elements
->resolving
= r
;
1403 elements
->prefix
= save
;
1405 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELOOP
1406 && gpg_err_code(rc
) != GPG_ERR_EACCES
)
1414 error_flag
= err
= rc
;
1423 if (n
->children
&& elements
->recurse
&& err
!= GPG_ERR_EACCES
)
1425 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1426 char *save
= elements
->prefix
;
1431 return GPG_ERR_ENOMEM
;
1434 elements
->prefix
= tmp
;
1435 rc
= path_list_recurse (client
, doc
, n
->children
, elements
);
1436 xfree (elements
->prefix
);
1437 elements
->prefix
= save
;
1441 if (gpg_err_code(rc
) == GPG_ERR_ELOOP
1442 || gpg_err_code (rc
) == GPG_ERR_EACCES
1443 || gpg_err_code (rc
) == GPG_ERR_ELEMENT_NOT_FOUND
)
1445 error_flag
= err
= rc
;
1459 return error_flag
== GPG_ERR_ELOOP
|| error_flag
== GPG_ERR_EACCES
1464 add_attribute (struct client_s
*client
, xmlNodePtr node
, const char *name
,
1470 if (client
&& name
&& !strcmp (name
, "target"))
1472 rc
= is_element_owner (client
, node
);
1477 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1478 return GPG_ERR_BAD_DATA
;
1480 if (client
&& name
&& !xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_acl"))
1482 xmlChar
*acl
= node_has_attribute (node
, (xmlChar
*) "_acl");
1486 char *user
= create_acl_user (client
);
1490 rc
= add_attribute (client
, node
, (char *) "_acl", user
);
1500 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1503 buf
= str_asprintf ("%li", time (NULL
));
1504 rc
= add_attribute (client
, node
, "_mtime", buf
);
1510 list_not_found_cb (struct client_s
*client
, int i
, xmlNodePtr node
,
1511 char **req
, gpg_error_t
*rc
, void *data
)
1513 struct element_list_s
*elements
= data
;
1515 if (*rc
!= GPG_ERR_EACCES
)
1518 elements
->data
= req
;
1523 * From the element path 'path', find sub-nodes and append them to the list.
1526 create_path_list (struct client_s
*client
, xmlDocPtr doc
,
1527 struct element_list_s
* elements
, char *path
)
1530 char **req
, **req_orig
;
1535 req
= str_split (path
, "\t", 0);
1538 req
= str_split (path
, " ", 0);
1540 return GPG_ERR_SYNTAX
;
1543 req_orig
= strv_dup (req
);
1546 rc
= GPG_ERR_ENOMEM
;
1550 n
= find_root_element (client
, doc
, &req
, &rc
, &a_target
, 0, 0);
1551 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
1552 || rc
== GPG_ERR_EACCES
)
1553 && elements
->verbose
&& a_target
)
1555 if (rc
!= GPG_ERR_EACCES
)
1563 if (rc
== GPG_ERR_EACCES
)
1569 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1580 *req
= str_dup (*req_orig
);
1587 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, &e_target
,
1588 NULL
, list_not_found_cb
, 1, 0, elements
, 0);
1589 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1594 else if (!n
&& rc
&& rc
!= GPG_ERR_EACCES
)
1599 if (!elements
->prefix
)
1604 * If any req_orig element contains no target the element should be
1605 * prefixed with the literal character. Not really crucial if the
1606 * client isn't human because child elements are prefixed for the
1607 * current path. But may be confusing if editing by hand.
1611 /* This is needed to prune the original requested element path to the
1612 * length of the failed element in the path. */
1613 int x
= strv_length (req_orig
)-strv_length ((char **)elements
->data
);
1617 for (i
= 0; i
<= x
; i
++)
1618 tmp
= strv_cat (tmp
, str_dup (req_orig
[i
]));
1620 elements
->prefix
= strv_join ("\t", tmp
);
1622 elements
->data
= NULL
;
1627 elements
->prefix
= str_dup (*req_orig
);
1629 elements
->prefix
= strv_join ("\t", req_orig
);
1632 if (!elements
->prefix
)
1634 rc
= GPG_ERR_ENOMEM
;
1638 if (elements
->verbose
)
1641 struct string_s
*realpath
= NULL
;
1642 gpg_error_t allowed
= rc
;
1646 if (!allowed
&& a_target
&& elements
->with_target
)
1648 rc
= build_realpath (client
, doc
, path
, &realpath
);
1652 realpath
= string_prepend (realpath
, "T ");
1655 ret
= strv_printf (&elements
->elements
, "%s%s%s%s%s",
1657 (!allowed
&& n
&& find_element_node (n
->children
))
1658 || realpath
? " " : "",
1659 (!allowed
&& n
&& find_element_node (n
->children
)) ? "+" : "",
1660 !allowed
&& realpath
? realpath
->str
: "",
1661 allowed
? " P" : "");
1662 string_free (realpath
, 1);
1665 rc
= GPG_ERR_ENOMEM
;
1669 else if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1671 rc
= GPG_ERR_ENOMEM
;
1675 if (update_element_list (elements
) == 0)
1677 rc
= GPG_ERR_ENOMEM
;
1682 rc
= path_list_recurse (client
, doc
, n
? n
->children
: n
, elements
);
1685 strv_free (req_orig
);
1691 recurse_xpath_nodeset (struct client_s
*client
, xmlDocPtr doc
,
1692 xmlNodeSetPtr nodes
, xmlChar
* value
,
1693 xmlBufferPtr
* result
, int cmd
, const xmlChar
* attr
)
1695 int i
= value
? nodes
->nodeNr
- 1 : 0;
1698 buf
= xmlBufferCreate ();
1701 return GPG_ERR_ENOMEM
;
1703 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1705 xmlNodePtr n
= nodes
->nodeTab
[i
];
1711 if (!value
&& !attr
)
1713 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1716 return GPG_ERR_BAD_DATA
;
1724 xmlNodeSetContent (n
, value
);
1725 rc
= update_element_mtime (client
, n
);
1733 rc
= add_attribute (client
, n
, (char *) attr
, (char *) value
);
1735 rc
= delete_attribute (client
, n
, attr
);
1747 convert_root_element (struct client_s
*client
, xmlNodePtr n
)
1749 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1755 xmlChar
*t
= xmlGetNodePath (n
);
1758 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1761 return GPG_ERR_AMBIGUOUS_NAME
;
1764 a
= xmlGetProp (n
, (xmlChar
*) "name");
1768 rc
= add_attribute (client
, n
, "_name", (char *) a
);
1774 rc
= delete_attribute (client
, n
, (xmlChar
*) "name");
1779 xmlNodeSetName (n
, (xmlChar
*) "element");
1786 delete_attribute (struct client_s
*client
, xmlNodePtr n
, const xmlChar
* name
)
1791 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1792 return GPG_ERR_NOT_FOUND
;
1794 if (xmlRemoveProp (a
) == -1)
1795 return GPG_ERR_BAD_DATA
;
1797 if (client
&& xmlStrEqual (name
, (xmlChar
*) "_acl"))
1799 char *user
= create_acl_user (client
);
1801 rc
= add_attribute (client
, n
, (char *) "_acl", user
);
1808 return update_element_mtime (client
, n
);
1812 convert_elements_recurse (struct client_s
*client
, xmlDocPtr doc
,
1813 xmlNodePtr n
, unsigned depth
)
1819 for (n
= n
->children
; n
; n
= n
->next
)
1821 if (n
->type
== XML_ELEMENT_NODE
)
1827 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1829 xmlChar
*t
= xmlGetNodePath (n
);
1832 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1835 return GPG_ERR_AMBIGUOUS_NAME
;
1838 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1842 xmlChar
*t
= xmlGetNodePath (n
);
1845 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1848 return GPG_ERR_AMBIGUOUS_NAME
;
1851 xmlChar
*tmp
= xmlStrdup (n
->name
);
1854 return GPG_ERR_ENOMEM
;
1856 xmlNodeSetName (n
, (xmlChar
*) "element");
1857 rc
= add_attribute (client
, n
, "_name", (char *) tmp
);
1865 rc
= convert_root_element (client
, n
);
1874 rc
= convert_elements_recurse (client
, doc
, n
, depth
);
1884 /* Renames ALL elements to the new "element" name. Existing element names are
1885 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1886 * elements can contain common characters that the XML parser barfs on (an
1887 * email address for example. */
1889 convert_pre_212_elements (xmlDocPtr doc
)
1891 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1893 log_write (_("Converting pre 2.12 data file..."));
1894 return convert_elements_recurse (NULL
, doc
, n
, 0);
1898 validate_import (struct client_s
*client
, xmlNodePtr node
)
1905 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1907 if (n
->type
== XML_ELEMENT_NODE
)
1909 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1911 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1915 xmlChar
*t
= xmlGetNodePath (n
);
1917 log_write (_("Missing attribute '_name' at %s."), t
);
1919 return GPG_ERR_INV_VALUE
;
1922 if (!valid_xml_element (a
))
1924 xmlChar
*t
= xmlGetNodePath (n
);
1926 log_write (_("'%s' is not a valid element name at %s."), a
,
1930 return GPG_ERR_INV_VALUE
;
1934 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1936 attr_ctime (client
, n
);
1939 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1941 update_element_mtime (client
, n
);
1946 xmlChar
*t
= xmlGetNodePath (n
);
1948 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1957 rc
= validate_import (client
, n
->children
);
1968 update_element_mtime (struct client_s
*client
, xmlNodePtr n
)
1970 return add_attribute (client
, n
, NULL
, NULL
);
1974 unlink_node (struct client_s
*client
, xmlNodePtr n
)
1982 rc
= update_element_mtime (client
, n
->parent
);
1989 parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
1993 xmlResetLastError ();
1994 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1995 if (!doc
&& xmlGetLastError ())
1996 return GPG_ERR_BAD_DATA
;
1999 return !doc
? GPG_ERR_ENOMEM
: 0;
2003 realpath_elements_cb (struct client_s
*client
, xmlNodePtr node
, char **target
,
2004 gpg_error_t
* rc
, char **req_orig
, void *data
)
2006 char *path
= *(char **) data
;
2007 char *tmp
= NULL
, *result
;
2012 *(char **) data
= NULL
;
2015 path
= strv_join ("\t", target
);
2019 *rc
= GPG_ERR_ENOMEM
;
2025 tmp
= strv_join ("\t", req_orig
);
2030 *rc
= GPG_ERR_ENOMEM
;
2036 result
= str_asprintf ("%s\t%s", path
, tmp
);
2038 result
= str_dup (path
);
2042 *rc
= GPG_ERR_ENOMEM
;
2050 *(char **) data
= result
;
2055 build_realpath (struct client_s
*client
, xmlDocPtr doc
, char *line
,
2056 struct string_s
** result
)
2063 struct string_s
*string
;
2066 if (strchr (line
, '\t') != NULL
)
2068 if ((req
= str_split (line
, "\t", 0)) == NULL
)
2069 return GPG_ERR_SYNTAX
;
2073 if ((req
= str_split (line
, " ", 0)) == NULL
)
2074 return GPG_ERR_SYNTAX
;
2077 n
= find_root_element (client
, doc
, &req
, &rc
, NULL
, 0, 0);
2084 rp
= strv_join ("\t", req
);
2088 return GPG_ERR_ENOMEM
;
2093 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, NULL
,
2094 realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
2103 string
= string_new (rp
);
2107 return GPG_ERR_ENOMEM
;
2110 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
2112 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
2114 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
2118 string_free (string
, 1);
2119 return GPG_ERR_ENOMEM
;
2133 node_to_element_path (xmlNodePtr node
)
2136 struct string_s
*str
= string_new ("");
2139 for (n
= node
; n
; n
= n
->parent
)
2143 for (child
= n
; child
; child
= child
->next
)
2145 if (child
->type
!= XML_ELEMENT_NODE
)
2148 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
2151 str
= string_prepend (str
, (char *) name
);
2153 name
= node_has_attribute (n
, (xmlChar
*) "target");
2155 str
= string_prepend (str
, "\t");
2157 str
= string_prepend (str
, "\t!");
2164 str
= string_erase (str
, 0, 1);
2166 string_free (str
, 0);
2172 * Recurse the element tree beginning at 'node' and find elements who point
2173 * back to 'src' or 'dst'. Also follows target attributes.
2176 find_child_to_target (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
2177 xmlNodePtr src
, xmlNodePtr dst
, unsigned depth
,
2183 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
2184 return gpg_error (GPG_ERR_ELOOP
);
2186 for (n
= node
; n
; n
= n
->next
)
2190 if (n
->type
!= XML_ELEMENT_NODE
)
2193 if (n
== src
|| n
== dst
)
2194 return GPG_ERR_ELOOP
;
2196 target
= node_has_attribute (n
, (xmlChar
*) "target");
2200 char **result
= NULL
;
2202 tmp
= resolve_path (client
, doc
, target
, &result
, &rc
);
2207 rc
= find_child_to_target (client
, doc
, tmp
, src
, dst
, ++depth
,
2212 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELEMENT_NOT_FOUND
)
2223 rc
= find_child_to_target (client
, doc
, n
->children
, src
, dst
,
2238 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
2243 for (n
= src
; n
; n
= n
->next
)
2245 if (n
->type
!= XML_ELEMENT_NODE
)
2254 rc
= find_child_of_parent (doc
, n
->children
, dst
);
2261 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
2266 for (n
= node
; n
; n
= n
->parent
)
2268 if (n
->type
!= XML_ELEMENT_NODE
)
2272 for (tmp
= n
->next
; tmp
; n
= n
->next
)
2274 if (n
->type
!= XML_ELEMENT_NODE
)
2278 return GPG_ERR_ELOOP
;
2283 return GPG_ERR_ELOOP
;
2290 validate_target_attribute (struct client_s
*client
, xmlDocPtr doc
,
2291 const char *src
, xmlNodePtr dst_node
)
2294 xmlNodePtr src_node
;
2295 char **src_req
= NULL
;
2297 src_node
= resolve_path (client
, doc
, (xmlChar
*) src
, &src_req
, &rc
);
2301 /* A destination element is a child of the source element. */
2302 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
2306 /* The destination element is a parent of the source element. */
2307 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
2311 /* A destination child element contains a target to the source element. */
2312 rc
= find_child_to_target (client
, doc
, dst_node
->children
, src_node
,
2318 strv_free (src_req
);
2322 /* The owner of the element is the first user listed in the _acl attribute
2323 * list. acl_check() should be called before calling this function. An empty
2324 * ACL is an error if the client is not invoking_user.
2327 is_element_owner (struct client_s
*client
, xmlNodePtr n
)
2329 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
2331 gpg_error_t rc
= GPG_ERR_EACCES
;
2336 return peer_is_invoker (client
);
2339 users
= str_split((char *)acl
, ",", 0);
2340 if (users
&& *users
)
2345 if (client
->thd
->remote
)
2346 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
2348 user
= get_username (client
->thd
->peer
->uid
);
2350 user
= get_username (client
->thd
->peer
->uid
);
2354 rc
= !strcasecmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
2356 rc
= !strcmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
2359 rc
= peer_is_invoker (client
);