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%s%s", (char *) val
,
523 rc
== GPG_ERR_ELOOP
? "O" :
524 rc
== GPG_ERR_EACCES
? "P" : "E",
525 with_target
? "T " : "",
526 with_target
? (char *)target
: "");
531 struct string_s
*realpath
= NULL
;
535 rc
= build_realpath (client
, doc
, (char *) target
,
545 realpath
= string_prepend (realpath
, "T ");
548 t
= str_asprintf ("%s%s%s%s", (char *) val
,
550 && find_element_node (tnode
->children
))
551 || realpath
? " " : "", tnode
552 && find_element_node (tnode
->children
) ?
553 "+" : "", realpath
? realpath
->str
: "");
556 string_free (realpath
, 1);
563 t
= str_dup ((char *) val
);
569 rc
= rc
? rc
: GPG_ERR_ENOMEM
;
573 tlist
= slist_append (list
, t
);
590 total
= slist_length (list
);
592 return GPG_ERR_NO_DATA
;
594 string
= string_new (NULL
);
601 for (i
= 0; i
< total
; i
++)
603 char *val
= slist_nth_data (list
, i
);
605 string_append_printf (string
, "%s\n", val
);
608 string
= string_truncate (string
, string
->len
- 1);
612 total
= slist_length (list
);
613 for (i
= 0; i
< total
; i
++)
614 xfree (slist_nth_data (list
, i
));
621 * Prevents a sibling element past the current element path with the same
625 find_stop_node (xmlNodePtr node
)
629 for (n
= node
->parent
->children
; n
; n
= n
->next
)
639 create_target_elements_cb (struct client_s
*client
, int verify
,
640 xmlNodePtr node
, char **path
, gpg_error_t
*rc
,
645 xmlNodePtr parent
= data
;
647 for (i
= 0; req
[i
] && *req
[i
]; i
++)
651 if (parent
&& node
== parent
)
653 *rc
= GPG_ERR_CONFLICT
;
657 is_literal_element (&req
[i
]);
659 if ((n
= find_element (client
, node
, req
[i
],
660 find_stop_node (node
), rc
)) == NULL
||
661 (n
&& n
->parent
== node
->parent
))
665 *rc
= create_new_element (client
, verify
, node
, req
[i
], &node
);
678 find_text_node (xmlNodePtr node
)
682 if (n
&& n
->type
== XML_TEXT_NODE
)
685 for (n
= node
; n
; n
= n
->next
)
687 if (n
->type
== XML_TEXT_NODE
)
695 create_elements_cb (struct client_s
*client
, int verify
, xmlNodePtr node
,
696 char **elements
, gpg_error_t
* rc
, void *data
)
699 char **req
= elements
;
701 if (*rc
&& *rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
704 if (node
->type
== XML_TEXT_NODE
)
707 for (i
= 0; req
[i
] && *req
[i
]; i
++)
712 * Strip the first '!' if needed. If there's another, it's an
713 * rc. The syntax has already been checked before calling this
716 is_literal_element (&req
[i
]);
717 n
= find_element (client
, node
, req
[i
], find_stop_node (node
), rc
);
722 * If the found element has the same parent as the current element,
723 * they are siblings and the new element needs to be created as a
724 * child of the current element (node).
726 if (n
&& n
->parent
== node
->parent
)
731 *rc
= create_new_element (client
, 0, node
, req
[i
], &node
);
742 /* The root element is really req[0]. It is need as a pointer in case there is
743 * a target attribute so it can be updated. */
745 find_root_element (struct client_s
*client
, xmlDocPtr doc
, char ***req
,
746 gpg_error_t
* rc
, int *target
, int recursion_depth
,
749 xmlNodePtr n
= xmlDocGetRootElement (doc
);
751 char *root
= str_dup (*req
[0]);
752 int literal
= is_literal_element (&root
);
756 *rc
= GPG_ERR_ENOMEM
;
763 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
765 xmlChar
*t
= xmlGetNodePath (n
);
767 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
776 if (n
->type
== XML_ELEMENT_NODE
)
778 if (depth
== 0 && xmlStrEqual (n
->name
, (xmlChar
*) "pwmd"))
785 if (depth
== 1 && xmlStrEqual (n
->name
, (xmlChar
*) "element"))
787 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "_name");
792 if (xmlStrEqual (content
, (xmlChar
*) root
))
794 char **nreq
, **tmp
= NULL
;
795 int acl
= client
->flags
& FLAG_ACL_IGNORE
;
797 *rc
= acl_check(client
, n
);
798 if ((*rc
&& *rc
!= GPG_ERR_EACCES
)
799 || (*rc
== GPG_ERR_EACCES
&& !acl
))
809 // This flag is cleared in acl_check() but we always
810 // allow ATTR LIST of root elements.
811 client
->flags
|= FLAG_ACL_IGNORE
;
822 content
= node_has_attribute (n
, (xmlChar
*) "target");
824 if (content
&& target
)
827 if (!content
|| stop
)
836 if (strchr ((char *) content
, '\t'))
838 nreq
= str_split ((char *) content
, "\t", 0);
847 *rc
= GPG_ERR_ENOMEM
;
853 tmp
= strv_catv (nreq
, tmp
+ 1);
859 *rc
= GPG_ERR_ENOMEM
;
868 if (strv_printf (&tmp
, "%s", content
) == 0)
872 *rc
= GPG_ERR_ENOMEM
;
878 nreq
= strv_catv (tmp
, nreq
+ 1);
883 *rc
= GPG_ERR_ENOMEM
;
893 return find_root_element (client
, doc
, req
, rc
, target
,
905 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
910 find_element (struct client_s
*client
, xmlNodePtr node
, char *element
,
911 xmlNodePtr stop
, gpg_error_t
*rc
)
917 if (!node
|| !element
)
920 for (n
= node
; n
; n
= n
->next
)
922 if (n
->type
!= XML_ELEMENT_NODE
)
928 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
930 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
934 // Prevent ATTR LIST showing child element attributes for a parent
935 // whos ACL denies the client.
936 if (client
->flags
& FLAG_ACL_ERROR
)
938 *rc
= GPG_ERR_EACCES
;
942 *rc
= acl_check(client
, n
);
956 node_has_attribute (xmlNodePtr n
, xmlChar
* attr
)
958 xmlAttrPtr a
= xmlHasProp (n
, attr
);
963 if (!a
->children
|| !a
->children
->content
)
966 return xmlGetProp (n
, attr
);
970 element_to_literal (char **element
)
972 char *p
= str_asprintf ("!%s", *element
);
982 /* Resolves elements in 'req' one at a time. It's recursive in case of
983 * "target" attributes. */
985 find_elements (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
986 char **req
, gpg_error_t
* rc
, int *target
,
987 xmlNodePtr (*found_fn
) (struct client_s
*, xmlNodePtr
, char **,
988 gpg_error_t
*, char **, void *),
989 xmlNodePtr (*not_found_fn
) (struct client_s
*, int, xmlNodePtr
,
990 char **, gpg_error_t
*, void *),
991 int is_list_command
, int recursion_depth
, void *data
, int stop
)
993 xmlNodePtr n
, last
, last_node
;
1000 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
1002 xmlChar
*t
= xmlGetNodePath (node
);
1004 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1007 *rc
= GPG_ERR_ELOOP
;
1011 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++)
1019 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1026 *rc
= GPG_ERR_ENOMEM
;
1030 literal
= is_literal_element (&t
);
1031 n
= find_element (client
, last
, t
, NULL
, rc
);
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
);
1108 found_fn (client
, tmp
, nreq
, rc
, p
+ 1, data
);
1117 if (!*(nreq
+ 1) && !*(p
+ 1))
1123 nnreq
= strv_catv (nreq
+ 1, p
+ 1);
1127 if (!nnreq
|| !*nnreq
)
1135 n
= find_elements (client
, doc
, tmp
->children
, nnreq
, rc
, NULL
,
1136 found_fn
, not_found_fn
, is_list_command
,
1137 recursion_depth
, data
, stop
);
1148 return not_found_fn (client
, 0, tmp
, p
, rc
, data
);
1150 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1156 char **zz
= p
+ 1, **qq
= nnreq
;
1158 if (strv_length (nnreq
) > strv_length (p
+ 1))
1161 for (; *qq
&& *zz
; zz
++)
1164 *zz
= str_dup (*qq
++);
1168 *rc
= GPG_ERR_ENOMEM
;
1184 update_element_list (struct element_list_s
*elements
)
1189 if (!elements
|| !elements
->elements
)
1192 line
= strv_join ("\t", elements
->elements
);
1197 strv_free (elements
->elements
);
1198 elements
->elements
= NULL
;
1199 l
= slist_append (elements
->list
, line
);
1209 path_list_recurse (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1210 struct element_list_s
*elements
)
1214 gpg_error_t error_flag
= 0;
1216 for (n
= node
; n
; n
= n
->next
)
1218 xmlChar
*target
= NULL
;
1219 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
1220 gpg_error_t err
= 0;
1227 if (n
->type
!= XML_ELEMENT_NODE
)
1230 rc
= acl_check(client
, n
);
1232 if (elements
->verbose
)
1235 (&elements
->elements
, "%s\t!%s%s%s", elements
->prefix
, a
,
1236 !rc
&& find_element_node (n
->children
) ? " +" : "",
1237 rc
== GPG_ERR_EACCES
? " P" : rc
? " E" : "") == 0)
1240 return GPG_ERR_ENOMEM
;
1244 if (strv_printf (&elements
->elements
, "%s\t!%s", elements
->prefix
, a
)
1248 return GPG_ERR_ENOMEM
;
1251 if (update_element_list (elements
) == 0)
1254 return GPG_ERR_ENOMEM
;
1257 if (rc
== GPG_ERR_EACCES
)
1269 target
= node_has_attribute (n
, (xmlChar
*) "target");
1273 char *save
= elements
->prefix
;
1274 int r
= elements
->resolving
;
1277 struct string_s
*realpath
= NULL
;
1279 tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
1280 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_ELEMENT_NOT_FOUND
1281 || rc
== GPG_ERR_EACCES
)
1283 if (rc
== GPG_ERR_ELOOP
)
1285 xmlChar
*t
= xmlGetNodePath (n
);
1287 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1291 if (elements
->verbose
)
1293 error_flag
= err
= rc
;
1297 else if (!elements
->verbose
&& rc
)
1304 path
= str_asprintf("%s\t%s", elements
->prefix
, a
);
1305 rc
= validate_target_attribute (client
, client
->doc
, path
, tnode
);
1307 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
1308 || rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1310 if (rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1311 error_flag
= err
= rc
;
1324 strv_printf (&elements
->elements
, "%s\t%s %s%s%s",
1325 elements
->prefix
, a
,
1326 err
== GPG_ERR_ELOOP
? "O" :
1327 err
== GPG_ERR_EACCES
? "P" : "E",
1328 elements
->with_target
? "T " : "",
1329 elements
->with_target
? (char *)target
: "");
1331 else if (!err
&& elements
->with_target
)
1333 rc
= build_realpath (client
, doc
, (char *) target
, &realpath
);
1341 realpath
= string_prepend (realpath
, "T ");
1344 if (!err
&& elements
->verbose
)
1346 if (!strv_printf (&elements
->elements
, "%s\t%s%s%s%s",
1347 elements
->prefix
, a
,
1348 (tnode
&& find_element_node (tnode
->children
))
1349 || realpath
? " " : "", tnode
1350 && find_element_node (tnode
->children
) ? "+" :
1351 "", realpath
? realpath
->str
: ""))
1355 return GPG_ERR_ENOMEM
;
1360 (&elements
->elements
, "%s\t%s", elements
->prefix
, a
))
1364 return GPG_ERR_ENOMEM
;
1368 string_free (realpath
, 1);
1370 tmp
= strv_join ("\t", elements
->elements
);
1375 return GPG_ERR_ENOMEM
;
1378 if (update_element_list (elements
) == 0)
1383 return GPG_ERR_ENOMEM
;
1386 if (!err
&& elements
->recurse
)
1388 /* Prune element flags. */
1389 if (elements
->verbose
&& strchr (tmp
, ' '))
1393 for (p
= tmp
; *p
; p
++)
1403 elements
->prefix
= tmp
;
1404 elements
->resolving
= 1;
1405 rc
= create_path_list (client
, doc
, elements
, (char *) target
);
1406 elements
->resolving
= r
;
1407 elements
->prefix
= save
;
1409 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELOOP
1410 && gpg_err_code(rc
) != GPG_ERR_EACCES
)
1418 error_flag
= err
= rc
;
1427 if (n
->children
&& elements
->recurse
)
1429 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1430 char *save
= elements
->prefix
;
1435 return GPG_ERR_ENOMEM
;
1438 elements
->prefix
= tmp
;
1439 rc
= path_list_recurse (client
, doc
, n
->children
, elements
);
1440 xfree (elements
->prefix
);
1441 elements
->prefix
= save
;
1445 if (gpg_err_code(rc
) == GPG_ERR_ELOOP
1446 || gpg_err_code (rc
) == GPG_ERR_EACCES
1447 || gpg_err_code (rc
) == GPG_ERR_ELEMENT_NOT_FOUND
)
1449 error_flag
= err
= rc
;
1463 return error_flag
== GPG_ERR_ELOOP
|| error_flag
== GPG_ERR_EACCES
1468 add_attribute (struct client_s
*client
, xmlNodePtr node
, const char *name
,
1474 if (client
&& name
&& !strcmp (name
, "target"))
1476 rc
= is_element_owner (client
, node
);
1481 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1482 return GPG_ERR_BAD_DATA
;
1484 if (client
&& name
&& !xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_acl"))
1486 xmlChar
*acl
= node_has_attribute (node
, (xmlChar
*) "_acl");
1490 char *user
= create_acl_user (client
);
1494 rc
= add_attribute (client
, node
, (char *) "_acl", user
);
1504 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1507 buf
= str_asprintf ("%li", time (NULL
));
1508 rc
= add_attribute (client
, node
, "_mtime", buf
);
1514 list_not_found_cb (struct client_s
*client
, int i
, xmlNodePtr node
,
1515 char **req
, gpg_error_t
*rc
, void *data
)
1517 struct element_list_s
*elements
= data
;
1519 if (*rc
!= GPG_ERR_EACCES
)
1522 elements
->data
= strv_dup (req
);
1527 * From the element path 'path', find sub-nodes and append them to the list.
1530 create_path_list (struct client_s
*client
, xmlDocPtr doc
,
1531 struct element_list_s
* elements
, char *path
)
1534 char **req
, **req_orig
;
1540 req
= str_split (path
, "\t", 0);
1543 req
= str_split (path
, " ", 0);
1545 return GPG_ERR_SYNTAX
;
1548 req_orig
= strv_dup (req
);
1551 rc
= GPG_ERR_ENOMEM
;
1555 n
= find_root_element (client
, doc
, &req
, &rc
, &a_target
, 0, 0);
1556 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
1557 || rc
== GPG_ERR_EACCES
) && elements
->verbose
&& a_target
)
1559 if (rc
== GPG_ERR_EACCES
)
1566 if (rc
== GPG_ERR_EACCES
)
1573 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1584 *req
= str_dup (*req_orig
);
1591 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, &e_target
,
1592 NULL
, list_not_found_cb
, 1, 0, elements
, 0);
1593 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1598 else if (rc
&& rc
!= GPG_ERR_EACCES
)
1605 if (!elements
->prefix
)
1610 * If any req_orig element contains no target the element should be
1611 * prefixed with the literal character. Not really crucial if the
1612 * client isn't human because child elements are prefixed for the
1613 * current path. But may be confusing if editing by hand.
1617 /* This is needed to prune the original requested element path to the
1618 * length of the failed element in the path. */
1619 int x
= strv_length (req_orig
)-strv_length ((char **)elements
->data
);
1623 for (i
= 0; i
<= x
; i
++)
1624 tmp
= strv_cat (tmp
, str_dup (req_orig
[i
]));
1626 elements
->prefix
= strv_join ("\t", tmp
);
1628 strv_free (elements
->data
);
1629 elements
->data
= NULL
;
1634 elements
->prefix
= str_dup (*req_orig
);
1636 elements
->prefix
= strv_join ("\t", req_orig
);
1639 if (!elements
->prefix
)
1641 rc
= GPG_ERR_ENOMEM
;
1645 if (elements
->verbose
)
1648 struct string_s
*realpath
= NULL
;
1650 if (!rc
&& allowed
&& a_target
&& elements
->with_target
)
1652 rc
= build_realpath (client
, doc
, path
, &realpath
);
1656 realpath
= string_prepend (realpath
, "T ");
1659 ret
= strv_printf (&elements
->elements
, "%s%s%s%s%s%s%s",
1661 (allowed
&& n
&& find_element_node (n
->children
))
1662 || realpath
? " " : "",
1663 (allowed
&& n
&& find_element_node (n
->children
)) ? "+" : "",
1664 allowed
&& realpath
? realpath
->str
: "",
1665 rc
== GPG_ERR_ELOOP
? " O" : "",
1666 rc
== GPG_ERR_ELEMENT_NOT_FOUND
? " E" : "",
1667 !allowed
? " P" : "");
1668 string_free (realpath
, 1);
1671 rc
= GPG_ERR_ENOMEM
;
1677 if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1679 rc
= GPG_ERR_ENOMEM
;
1684 if (update_element_list (elements
) == 0)
1686 rc
= GPG_ERR_ENOMEM
;
1692 rc
= path_list_recurse (client
, doc
, n
? n
->children
: n
, elements
);
1695 strv_free (req_orig
);
1701 recurse_xpath_nodeset (struct client_s
*client
, xmlDocPtr doc
,
1702 xmlNodeSetPtr nodes
, xmlChar
* value
,
1703 xmlBufferPtr
* result
, int cmd
, const xmlChar
* attr
)
1705 int i
= value
? nodes
->nodeNr
- 1 : 0;
1708 buf
= xmlBufferCreate ();
1711 return GPG_ERR_ENOMEM
;
1713 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1715 xmlNodePtr n
= nodes
->nodeTab
[i
];
1721 if (!value
&& !attr
)
1723 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1726 return GPG_ERR_BAD_DATA
;
1734 xmlNodeSetContent (n
, value
);
1735 rc
= update_element_mtime (client
, n
);
1743 rc
= add_attribute (client
, n
, (char *) attr
, (char *) value
);
1745 rc
= delete_attribute (client
, n
, attr
);
1757 convert_root_element (struct client_s
*client
, xmlNodePtr n
)
1759 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1765 xmlChar
*t
= xmlGetNodePath (n
);
1768 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1771 return GPG_ERR_AMBIGUOUS_NAME
;
1774 a
= xmlGetProp (n
, (xmlChar
*) "name");
1778 rc
= add_attribute (client
, n
, "_name", (char *) a
);
1784 rc
= delete_attribute (client
, n
, (xmlChar
*) "name");
1789 xmlNodeSetName (n
, (xmlChar
*) "element");
1796 delete_attribute (struct client_s
*client
, xmlNodePtr n
, const xmlChar
* name
)
1801 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1802 return GPG_ERR_NOT_FOUND
;
1804 if (xmlRemoveProp (a
) == -1)
1805 return GPG_ERR_BAD_DATA
;
1807 if (client
&& xmlStrEqual (name
, (xmlChar
*) "_acl"))
1809 char *user
= create_acl_user (client
);
1811 rc
= add_attribute (client
, n
, (char *) "_acl", user
);
1818 return update_element_mtime (client
, n
);
1822 convert_elements_recurse (struct client_s
*client
, xmlDocPtr doc
,
1823 xmlNodePtr n
, unsigned depth
)
1829 for (n
= n
->children
; n
; n
= n
->next
)
1831 if (n
->type
== XML_ELEMENT_NODE
)
1837 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1839 xmlChar
*t
= xmlGetNodePath (n
);
1842 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1845 return GPG_ERR_AMBIGUOUS_NAME
;
1848 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1852 xmlChar
*t
= xmlGetNodePath (n
);
1855 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1858 return GPG_ERR_AMBIGUOUS_NAME
;
1861 xmlChar
*tmp
= xmlStrdup (n
->name
);
1864 return GPG_ERR_ENOMEM
;
1866 xmlNodeSetName (n
, (xmlChar
*) "element");
1867 rc
= add_attribute (client
, n
, "_name", (char *) tmp
);
1875 rc
= convert_root_element (client
, n
);
1884 rc
= convert_elements_recurse (client
, doc
, n
, depth
);
1894 /* Renames ALL elements to the new "element" name. Existing element names are
1895 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1896 * elements can contain common characters that the XML parser barfs on (an
1897 * email address for example. */
1899 convert_pre_212_elements (xmlDocPtr doc
)
1901 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1903 log_write (_("Converting pre 2.12 data file..."));
1904 return convert_elements_recurse (NULL
, doc
, n
, 0);
1908 validate_import (struct client_s
*client
, xmlNodePtr node
)
1915 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1917 if (n
->type
== XML_ELEMENT_NODE
)
1919 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1921 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1925 xmlChar
*t
= xmlGetNodePath (n
);
1927 log_write (_("Missing attribute '_name' at %s."), t
);
1929 return GPG_ERR_INV_VALUE
;
1932 if (!valid_xml_element (a
))
1934 xmlChar
*t
= xmlGetNodePath (n
);
1936 log_write (_("'%s' is not a valid element name at %s."), a
,
1940 return GPG_ERR_INV_VALUE
;
1944 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1946 attr_ctime (client
, n
);
1949 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1951 update_element_mtime (client
, n
);
1956 xmlChar
*t
= xmlGetNodePath (n
);
1958 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1967 rc
= validate_import (client
, n
->children
);
1978 update_element_mtime (struct client_s
*client
, xmlNodePtr n
)
1980 return add_attribute (client
, n
, NULL
, NULL
);
1984 unlink_node (struct client_s
*client
, xmlNodePtr n
)
1992 rc
= update_element_mtime (client
, n
->parent
);
1999 parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
2003 xmlResetLastError ();
2004 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2005 if (!doc
&& xmlGetLastError ())
2006 return GPG_ERR_BAD_DATA
;
2009 return !doc
? GPG_ERR_ENOMEM
: 0;
2013 realpath_elements_cb (struct client_s
*client
, xmlNodePtr node
, char **target
,
2014 gpg_error_t
* rc
, char **req_orig
, void *data
)
2016 char *path
= *(char **) data
;
2017 char *tmp
= NULL
, *result
;
2022 *(char **) data
= NULL
;
2025 path
= strv_join ("\t", target
);
2029 *rc
= GPG_ERR_ENOMEM
;
2035 tmp
= strv_join ("\t", req_orig
);
2040 *rc
= GPG_ERR_ENOMEM
;
2046 result
= str_asprintf ("%s\t%s", path
, tmp
);
2048 result
= str_dup (path
);
2052 *rc
= GPG_ERR_ENOMEM
;
2060 *(char **) data
= result
;
2065 build_realpath (struct client_s
*client
, xmlDocPtr doc
, char *line
,
2066 struct string_s
** result
)
2073 struct string_s
*string
;
2076 if (strchr (line
, '\t') != NULL
)
2078 if ((req
= str_split (line
, "\t", 0)) == NULL
)
2079 return GPG_ERR_SYNTAX
;
2083 if ((req
= str_split (line
, " ", 0)) == NULL
)
2084 return GPG_ERR_SYNTAX
;
2087 n
= find_root_element (client
, doc
, &req
, &rc
, NULL
, 0, 0);
2094 rp
= strv_join ("\t", req
);
2098 return GPG_ERR_ENOMEM
;
2103 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, NULL
,
2104 realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
2113 string
= string_new (rp
);
2117 return GPG_ERR_ENOMEM
;
2120 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
2122 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
2124 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
2128 string_free (string
, 1);
2129 return GPG_ERR_ENOMEM
;
2143 node_to_element_path (xmlNodePtr node
)
2146 struct string_s
*str
= string_new ("");
2149 for (n
= node
; n
; n
= n
->parent
)
2153 for (child
= n
; child
; child
= child
->next
)
2155 if (child
->type
!= XML_ELEMENT_NODE
)
2158 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
2161 str
= string_prepend (str
, (char *) name
);
2163 name
= node_has_attribute (n
, (xmlChar
*) "target");
2165 str
= string_prepend (str
, "\t");
2167 str
= string_prepend (str
, "\t!");
2174 str
= string_erase (str
, 0, 1);
2176 string_free (str
, 0);
2182 * Recurse the element tree beginning at 'node' and find elements who point
2183 * back to 'src' or 'dst'. Also follows target attributes.
2186 find_child_to_target (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
2187 xmlNodePtr src
, xmlNodePtr dst
, unsigned depth
,
2193 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
2194 return gpg_error (GPG_ERR_ELOOP
);
2196 for (n
= node
; n
; n
= n
->next
)
2200 if (n
->type
!= XML_ELEMENT_NODE
)
2203 if (n
== src
|| n
== dst
)
2204 return GPG_ERR_ELOOP
;
2206 target
= node_has_attribute (n
, (xmlChar
*) "target");
2210 char **result
= NULL
;
2212 tmp
= resolve_path (client
, doc
, target
, &result
, &rc
);
2217 rc
= find_child_to_target (client
, doc
, tmp
, src
, dst
, ++depth
,
2222 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELEMENT_NOT_FOUND
)
2233 rc
= find_child_to_target (client
, doc
, n
->children
, src
, dst
,
2248 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
2253 for (n
= src
; n
; n
= n
->next
)
2255 if (n
->type
!= XML_ELEMENT_NODE
)
2264 rc
= find_child_of_parent (doc
, n
->children
, dst
);
2271 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
2276 for (n
= node
; n
; n
= n
->parent
)
2278 if (n
->type
!= XML_ELEMENT_NODE
)
2282 for (tmp
= n
->next
; tmp
; n
= n
->next
)
2284 if (n
->type
!= XML_ELEMENT_NODE
)
2288 return GPG_ERR_ELOOP
;
2293 return GPG_ERR_ELOOP
;
2300 validate_target_attribute (struct client_s
*client
, xmlDocPtr doc
,
2301 const char *src
, xmlNodePtr dst_node
)
2304 xmlNodePtr src_node
;
2305 char **src_req
= NULL
;
2307 src_node
= resolve_path (client
, doc
, (xmlChar
*) src
, &src_req
, &rc
);
2311 client
->flags
|= FLAG_ACL_IGNORE
;
2312 /* A destination element is a child of the source element. */
2313 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
2317 /* The destination element is a parent of the source element. */
2318 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
2322 /* A destination child element contains a target to the source element. */
2323 rc
= find_child_to_target (client
, doc
, dst_node
->children
, src_node
,
2329 strv_free (src_req
);
2330 client
->flags
&= ~(FLAG_ACL_IGNORE
| FLAG_ACL_ERROR
);
2334 /* The owner of the element is the first user listed in the _acl attribute
2335 * list. acl_check() should be called before calling this function. An empty
2336 * ACL is an error if the client is not invoking_user.
2339 is_element_owner (struct client_s
*client
, xmlNodePtr n
)
2341 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
2343 gpg_error_t rc
= GPG_ERR_EACCES
;
2348 return peer_is_invoker (client
);
2351 users
= str_split((char *)acl
, ",", 0);
2352 if (users
&& *users
)
2357 if (client
->thd
->remote
)
2358 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
2360 user
= get_username (client
->thd
->peer
->uid
);
2362 user
= get_username (client
->thd
->peer
->uid
);
2366 rc
= !strcasecmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
2368 rc
= !strcmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
2371 rc
= peer_is_invoker (client
);