2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
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"
48 extern void log_write (const char *fmt
, ...);
51 * 'element' must be allocated.
54 is_literal_element (char **element
)
58 if (!element
|| !*element
)
61 if (*(*element
) == '!')
65 for (p
= *element
, c
= p
+ 1; *c
; c
++)
76 valid_xml_attribute (const char *str
)
78 return valid_xml_element ((xmlChar
*)str
);
82 valid_xml_element (xmlChar
*str
)
87 if (!str
|| !*str
|| *str
== '!')
90 wc
= str_to_wchar ((const char *)str
);
93 for (c
= 0; c
< len
; c
++)
107 valid_element_path (char **path
, int with_content
)
109 char **dup
= NULL
, **p
;
114 /* Save some memory by not duplicating the element content. */
117 int i
, t
= strv_length (path
);
119 for (i
= 0; i
< t
- 1; i
++)
121 char **tmp
= xrealloc (dup
, (i
+ 2) * sizeof (char *));
130 dup
[i
] = str_dup (path
[i
]);
135 dup
= strv_dup (path
);
140 for (p
= dup
; *p
&& *(*p
); p
++)
142 is_literal_element (&(*p
));
143 if (!valid_xml_element ((xmlChar
*) * p
))
155 attr_ctime (xmlNodePtr n
)
157 char *buf
= str_asprintf ("%li", time (NULL
));
161 return GPG_ERR_ENOMEM
;
163 rc
= add_attribute (n
, "_ctime", buf
);
168 // FIXME add parameter to want RW or RO access to the XML node.
170 acl_check (struct client_s
*client
, xmlNodePtr n
)
172 gpg_error_t rc
= GPG_ERR_EPERM
;
173 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
174 char **users
= acl
? str_split((char *)acl
, ",", 0) : NULL
;
178 if (!acl
|| !users
|| !*users
)
181 return peer_is_self(client
);
184 if (!peer_is_self(client
))
187 for (p
= users
; *p
; p
++)
190 rc
= acl_check_common (client
, *p
,
191 client
->thd
->remote
? 0 : client
->thd
->peer
->uid
,
192 client
->thd
->remote
? 0 : client
->thd
->peer
->gid
,
195 rc
= acl_check_common (client
, *p
, client
->thd
->peer
->uid
,
196 client
->thd
->peer
->gid
, &allowed
);
205 return allowed
? 0 : GPG_ERR_EPERM
;
209 create_acl_user (struct client_s
*client
)
212 if (client
->thd
->remote
)
213 return str_asprintf ("#%s", client
->thd
->tls
->fp
);
216 return get_username (client
->thd
->peer
->uid
);
220 create_new_element (struct client_s
*client
, xmlNodePtr parent
,
221 const char *name
, xmlNodePtr
* result
)
226 rc
= acl_check(client
, parent
);
230 n
= xmlNewNode (NULL
, (xmlChar
*) "element");
232 return GPG_ERR_ENOMEM
;
234 rc
= add_attribute (n
, "_name", name
);
240 xmlNodePtr p
= xmlAddChild (parent
, n
);
241 char *user
= create_acl_user (client
);
246 rc
= add_attribute(p
, "_acl", user
);
256 new_root_element (struct client_s
*client
, xmlDocPtr doc
, char *name
)
258 xmlNodePtr root
= xmlDocGetRootElement (doc
);
262 return GPG_ERR_BAD_DATA
;
267 if (!valid_xml_element ((xmlChar
*) p
))
268 return GPG_ERR_INV_VALUE
;
270 return create_new_element (client
, root
, p
, NULL
);
277 xmlTextWriterPtr wr
= xmlNewTextWriterDoc (&doc
, 0);
282 if (xmlTextWriterStartDocument (wr
, NULL
, "UTF-8", "yes"))
285 if (xmlTextWriterStartDTD (wr
, (xmlChar
*) "pwmd", NULL
, NULL
) == -1)
288 if (xmlTextWriterWriteDTDElement (wr
, (xmlChar
*) "pwmd",
289 (xmlChar
*) "(element)") == -1)
292 xmlTextWriterEndDTDElement (wr
);
294 if (xmlTextWriterWriteDTDAttlist (wr
, (xmlChar
*) "element",
295 (xmlChar
*) "_name CDATA #REQUIRED") ==
299 xmlTextWriterEndDTDAttlist (wr
);
300 xmlTextWriterEndDTD (wr
);
302 if (xmlTextWriterStartElement (wr
, (xmlChar
*) "pwmd"))
305 xmlTextWriterEndElement (wr
);
306 xmlTextWriterEndDocument (wr
);
307 xmlFreeTextWriter (wr
);
311 xmlTextWriterEndDocument (wr
);
312 xmlFreeTextWriter (wr
);
320 return create_dtd ();
324 find_element_node (xmlNodePtr node
)
328 if (n
&& n
->type
== XML_ELEMENT_NODE
)
331 for (n
= node
; n
; n
= n
->next
)
333 if (n
->type
== XML_ELEMENT_NODE
)
341 resolve_path (struct client_s
*client
, xmlDocPtr doc
, xmlChar
* path
,
342 char ***result
, gpg_error_t
* rc
)
347 req
= str_split ((char *) path
, "\t", 0);
350 *rc
= GPG_ERR_ENOMEM
;
354 n
= find_root_element (client
, doc
, &req
, rc
, NULL
, 0, 0);
362 n
= find_elements (client
, doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
, NULL
,
374 * Lists root element names; the value of the attribute "_name" of an element
375 * "element". If there's a target attribute both literal and non-literal
376 * element names will be added. This is the primary reason why XML entities
377 * cannot be used. There wouldn't be a way to get the literal an non-literal
381 list_root_elements (struct client_s
*client
, xmlDocPtr doc
,
382 struct string_s
** result
, int verbose
, int with_target
)
385 struct slist_s
*list
= NULL
;
387 struct string_s
*string
;
390 n
= xmlDocGetRootElement (doc
);
391 if (!n
|| !n
->children
)
392 return GPG_ERR_NO_DATA
;
394 for (n
= n
->children
; n
; n
= n
->next
)
397 xmlChar
*val
, *target
;
398 struct slist_s
*tlist
;
401 if (n
->type
!= XML_ELEMENT_NODE
)
404 a
= xmlHasProp (n
, (xmlChar
*) "_name");
405 if (!a
|| !a
->children
->content
)
408 val
= xmlNodeGetContent (a
->children
);
416 str_asprintf ("!%s%s", (char *) val
,
417 verbose
? find_element_node (n
->children
) ? " +" : "" :
427 tlist
= slist_append (list
, tmp
);
436 target
= node_has_attribute (n
, (xmlChar
*) "target");
444 xmlNodePtr tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
446 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
447 || rc
== GPG_ERR_EPERM
)
449 t
= str_asprintf ("%s %s", (char *) val
,
450 rc
== GPG_ERR_ELOOP
? "O" :
451 rc
== GPG_ERR_EPERM
? "P" : "E");
456 struct string_s
*realpath
= NULL
;
460 rc
= build_realpath (client
, doc
, (char *) target
,
470 realpath
= string_prepend (realpath
, "T ");
473 t
= str_asprintf ("%s%s%s%s", (char *) val
,
475 && find_element_node (tnode
->children
))
476 || realpath
? " " : "", tnode
477 && find_element_node (tnode
->children
) ?
478 "+" : "", realpath
? realpath
->str
: "");
481 string_free (realpath
, 1);
488 t
= str_dup ((char *) val
);
494 rc
= rc
? rc
: GPG_ERR_ENOMEM
;
498 tlist
= slist_append (list
, t
);
515 total
= slist_length (list
);
517 return GPG_ERR_NO_DATA
;
519 string
= string_new (NULL
);
526 for (i
= 0; i
< total
; i
++)
528 char *val
= slist_nth_data (list
, i
);
530 string_append_printf (string
, "%s\n", val
);
533 string
= string_truncate (string
, string
->len
- 1);
537 total
= slist_length (list
);
538 for (i
= 0; i
< total
; i
++)
539 xfree (slist_nth_data (list
, i
));
546 * Prevents a sibling element past the current element path with the same
550 find_stop_node (xmlNodePtr node
)
554 for (n
= node
->parent
->children
; n
; n
= n
->next
)
564 create_target_elements_cb (struct client_s
*client
,
565 xmlNodePtr node
, char **path
, gpg_error_t
*rc
,
570 xmlNodePtr parent
= data
;
572 for (i
= 0; req
[i
] && *req
[i
]; i
++)
576 if (parent
&& node
== parent
)
578 *rc
= GPG_ERR_CONFLICT
;
582 is_literal_element (&req
[i
]);
584 if ((n
= find_element (client
, node
, req
[i
],
585 find_stop_node (node
), rc
)) == NULL
||
586 (n
&& n
->parent
== node
->parent
))
590 *rc
= create_new_element (client
, node
, req
[i
], &node
);
603 find_text_node (xmlNodePtr node
)
607 if (n
&& n
->type
== XML_TEXT_NODE
)
610 for (n
= node
; n
; n
= n
->next
)
612 if (n
->type
== XML_TEXT_NODE
)
620 create_elements_cb (struct client_s
*client
, xmlNodePtr node
, char **elements
,
621 gpg_error_t
* rc
, void *data
)
624 char **req
= elements
;
626 if (node
->type
== XML_TEXT_NODE
)
629 for (i
= 0; req
[i
] && *req
[i
]; i
++)
634 * Strip the first '!' if needed. If there's another, it's an
635 * rc. The syntax has already been checked before calling this
638 is_literal_element (&req
[i
]);
639 n
= find_element (client
, node
, req
[i
], find_stop_node (node
), rc
);
644 * If the found element has the same parent as the current element,
645 * they are siblings and the new element needs to be created as a
646 * child of the current element (node).
648 if (n
&& n
->parent
== node
->parent
)
653 *rc
= create_new_element (client
, node
, req
[i
], &node
);
664 /* The root element is really req[0]. It is need as a pointer in case there is
665 * a target attribute so it can be updated. */
667 find_root_element (struct client_s
*client
, xmlDocPtr doc
, char ***req
,
668 gpg_error_t
* rc
, int *target
, int recursion_depth
,
671 xmlNodePtr n
= xmlDocGetRootElement (doc
);
673 char *root
= str_dup (*req
[0]);
674 int literal
= is_literal_element (&root
);
678 *rc
= GPG_ERR_ENOMEM
;
685 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
687 xmlChar
*t
= xmlGetNodePath (n
);
689 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
698 if (n
->type
== XML_ELEMENT_NODE
)
700 if (depth
== 0 && xmlStrEqual (n
->name
, (xmlChar
*) "pwmd"))
707 if (depth
== 1 && xmlStrEqual (n
->name
, (xmlChar
*) "element"))
709 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "_name");
714 if (xmlStrEqual (content
, (xmlChar
*) root
))
716 char **nreq
, **tmp
= NULL
;
718 *rc
= acl_check(client
, n
);
734 content
= node_has_attribute (n
, (xmlChar
*) "target");
739 if (!content
|| stop
)
748 if (strchr ((char *) content
, '\t'))
750 nreq
= str_split ((char *) content
, "\t", 0);
759 *rc
= GPG_ERR_ENOMEM
;
765 tmp
= strv_catv (nreq
, tmp
+ 1);
771 *rc
= GPG_ERR_ENOMEM
;
780 if (strv_printf (&tmp
, "%s", content
) == 0)
784 *rc
= GPG_ERR_ENOMEM
;
790 nreq
= strv_catv (tmp
, nreq
+ 1);
795 *rc
= GPG_ERR_ENOMEM
;
806 find_root_element (client
, doc
, req
, rc
, target
,
819 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
824 find_element (struct client_s
*client
, xmlNodePtr node
, char *element
,
825 xmlNodePtr stop
, gpg_error_t
*rc
)
831 if (!node
|| !element
)
834 for (n
= node
; n
; n
= n
->next
)
836 if (n
->type
!= XML_ELEMENT_NODE
)
842 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
844 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
848 *rc
= acl_check(client
, n
);
862 node_has_attribute (xmlNodePtr n
, xmlChar
* attr
)
864 xmlAttrPtr a
= xmlHasProp (n
, attr
);
869 if (!a
->children
|| !a
->children
->content
)
872 return xmlGetProp (n
, attr
);
876 element_to_literal (char **element
)
878 char *p
= str_asprintf ("!%s", *element
);
888 /* Resolves elements in 'req' one at a time. It's recursive in case of
889 * "target" attributes. */
891 find_elements (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
892 char **req
, gpg_error_t
* rc
, int *target
,
893 xmlNodePtr (*found_fn
) (struct client_s
*, xmlNodePtr
, char **,
894 gpg_error_t
*, char **, void *),
895 xmlNodePtr (*not_found_fn
) (struct client_s
*, xmlNodePtr
,
896 char **, gpg_error_t
*, void *),
897 int is_list_command
, int recursion_depth
, void *data
, int stop
)
899 xmlNodePtr n
, last
, last_node
;
906 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
908 xmlChar
*t
= xmlGetNodePath (node
);
910 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
917 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++)
925 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
932 *rc
= GPG_ERR_ENOMEM
;
936 literal
= is_literal_element (&t
);
937 n
= find_element (client
, last
, t
, NULL
, rc
);
940 if (*rc
&& *rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
946 return not_found_fn (client
, found
? last_node
: last_node
->parent
,
950 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
961 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "target");
962 char **nreq
= NULL
, **nnreq
;
966 if (is_list_command
== 1)
968 if (element_to_literal (&(*p
)) == 0)
970 *rc
= GPG_ERR_ENOMEM
;
981 if (!*(p
+ 1) && stop
)
987 if (strchr ((char *) content
, '\t') != NULL
)
989 if ((nreq
= str_split ((char *) content
, "\t", 0)) == NULL
)
992 *rc
= GPG_ERR_INV_VALUE
;
998 if ((nreq
= str_split ((char *) content
, " ", 0)) == NULL
)
1001 *rc
= GPG_ERR_INV_VALUE
;
1007 tmp
= find_root_element (client
, doc
, &nreq
, rc
, target
, 0, 0);
1017 found_fn (client
, tmp
, nreq
, rc
, p
+ 1, data
);
1026 if (!*(nreq
+ 1) && !*(p
+ 1))
1032 nnreq
= strv_catv (nreq
+ 1, p
+ 1);
1036 if (!nnreq
|| !*nnreq
)
1045 n
= find_elements (client
, doc
, tmp
->children
, nnreq
, rc
, NULL
,
1046 found_fn
, not_found_fn
, is_list_command
,
1047 recursion_depth
, data
, stop
);
1053 return not_found_fn (client
, tmp
, p
+ 1, rc
, data
);
1055 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1061 char **zz
= p
+ 1, **qq
= nnreq
;
1063 if (strv_length (nnreq
) > strv_length (p
+ 1))
1066 for (; *qq
&& *zz
; zz
++)
1069 *zz
= str_dup (*qq
++);
1073 *rc
= GPG_ERR_ENOMEM
;
1089 update_element_list (struct element_list_s
*elements
)
1094 if (!elements
|| !elements
->elements
)
1097 line
= strv_join ("\t", elements
->elements
);
1102 strv_free (elements
->elements
);
1103 elements
->elements
= NULL
;
1104 l
= slist_append (elements
->list
, line
);
1114 path_list_recurse (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1115 struct element_list_s
*elements
)
1119 gpg_error_t error_flag
= 0;
1121 for (n
= node
; n
; n
= n
->next
)
1123 xmlChar
*target
= NULL
;
1124 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
1131 if (n
->type
!= XML_ELEMENT_NODE
)
1134 rc
= acl_check(client
, n
);
1136 if (elements
->verbose
)
1139 (&elements
->elements
, "%s\t!%s%s%s", elements
->prefix
, a
,
1140 !rc
&& find_element_node (n
->children
) ? " +" : "",
1141 rc
== GPG_ERR_EPERM
? " P" : "") == 0)
1144 return GPG_ERR_ENOMEM
;
1148 if (strv_printf (&elements
->elements
, "%s\t!%s", elements
->prefix
, a
)
1152 return GPG_ERR_ENOMEM
;
1155 if (update_element_list (elements
) == 0)
1158 return GPG_ERR_ENOMEM
;
1165 return rc
== GPG_ERR_EPERM
? 0 : rc
;
1168 target
= node_has_attribute (n
, (xmlChar
*) "target");
1172 char *save
= elements
->prefix
;
1173 int r
= elements
->resolving
;
1176 struct string_s
*realpath
= NULL
;
1178 tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
1179 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_ELEMENT_NOT_FOUND
1180 || rc
== GPG_ERR_EPERM
)
1182 if (rc
== GPG_ERR_ELOOP
)
1184 xmlChar
*t
= xmlGetNodePath (n
);
1186 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1190 if (elements
->verbose
)
1192 strv_printf (&elements
->elements
, "%s\t%s %s",
1193 elements
->prefix
, a
,
1194 rc
== GPG_ERR_ELOOP
? "O" :
1195 rc
== GPG_ERR_EPERM
? "P" : "E");
1196 error_flag
= err
= rc
;
1202 if (!elements
->verbose
&& rc
)
1209 if (elements
->with_target
)
1211 rc
= build_realpath (client
, doc
, (char *) target
, &realpath
);
1219 realpath
= string_prepend (realpath
, "T ");
1222 if (elements
->verbose
)
1224 if (!strv_printf (&elements
->elements
, "%s\t%s%s%s%s",
1225 elements
->prefix
, a
,
1226 (tnode
&& find_element_node (tnode
->children
))
1227 || realpath
? " " : "", tnode
1228 && find_element_node (tnode
->children
) ? "+" :
1229 "", realpath
? realpath
->str
: ""))
1233 return GPG_ERR_ENOMEM
;
1238 (&elements
->elements
, "%s\t%s", elements
->prefix
, a
))
1242 return GPG_ERR_ENOMEM
;
1247 string_free (realpath
, 1);
1249 tmp
= strv_join ("\t", elements
->elements
);
1255 return GPG_ERR_ENOMEM
;
1258 if (update_element_list (elements
) == 0)
1263 return GPG_ERR_ENOMEM
;
1266 if (!err
&& elements
->recurse
)
1268 /* Prune element flags. */
1269 if (elements
->verbose
&& strchr (tmp
, ' '))
1273 for (p
= tmp
; *p
; p
++)
1283 elements
->prefix
= tmp
;
1284 elements
->resolving
= 1;
1285 rc
= create_path_list (client
, doc
, elements
, (char *) target
);
1286 elements
->resolving
= r
;
1287 elements
->prefix
= save
;
1289 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELOOP
)
1297 error_flag
= err
= rc
;
1306 if (n
->children
&& elements
->recurse
&& err
!= GPG_ERR_ELOOP
)
1308 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1309 char *save
= elements
->prefix
;
1314 return GPG_ERR_ENOMEM
;
1317 elements
->prefix
= tmp
;
1318 rc
= path_list_recurse (client
, doc
, n
->children
, elements
);
1319 xfree (elements
->prefix
);
1320 elements
->prefix
= save
;
1332 return error_flag
== GPG_ERR_ELOOP
? error_flag
: rc
;
1336 add_attribute (xmlNodePtr node
, const char *name
, const char *value
)
1341 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1342 return GPG_ERR_BAD_DATA
;
1344 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1347 buf
= str_asprintf ("%li", time (NULL
));
1348 rc
= add_attribute (node
, "_mtime", buf
);
1354 * From the element path 'path', find sub-nodes and append them to the list.
1357 create_path_list (struct client_s
*client
, xmlDocPtr doc
,
1358 struct element_list_s
* elements
, char *path
)
1361 char **req
, **req_orig
;
1365 req
= str_split (path
, "\t", 0);
1368 req
= str_split (path
, " ", 0);
1370 return GPG_ERR_SYNTAX
;
1373 req_orig
= strv_dup (req
);
1376 rc
= GPG_ERR_ENOMEM
;
1380 n
= find_root_element (client
, doc
, &req
, &rc
, &a_target
, 0, 0);
1381 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
1382 || rc
== GPG_ERR_EPERM
)
1383 && elements
->verbose
&& a_target
)
1385 if (rc
!= GPG_ERR_EPERM
)
1391 if (rc
== GPG_ERR_EPERM
)
1394 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1405 *req
= str_dup (*req_orig
);
1413 find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, &e_target
,
1414 NULL
, NULL
, 1, 0, NULL
, 0);
1416 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1426 if (!elements
->prefix
)
1431 * If any req_orig element contains no target the element should be
1432 * prefixed with the literal character. Not really crucial if the
1433 * client isn't human because child elements are prefixed for the
1434 * current path. But may be confusing if editing by hand.
1436 elements
->prefix
= strv_join ("\t", req_orig
);
1438 if (!elements
->prefix
)
1440 rc
= GPG_ERR_ENOMEM
;
1444 if (elements
->verbose
)
1447 struct string_s
*realpath
= NULL
;
1448 gpg_error_t allowed
= rc
;
1452 if (!allowed
&& a_target
&& elements
->with_target
)
1454 rc
= build_realpath (client
, doc
, path
, &realpath
);
1458 realpath
= string_prepend (realpath
, "T ");
1461 ret
= strv_printf (&elements
->elements
, "%s%s%s%s%s",
1463 (!allowed
&& n
&& find_element_node (n
->children
))
1464 || realpath
? " " : "",
1465 (!allowed
&& n
&& find_element_node (n
->children
)) ? "+" : "",
1466 !allowed
&& realpath
? realpath
->str
: "",
1467 allowed
? " P" : "");
1468 string_free (realpath
, 1);
1471 rc
= GPG_ERR_ENOMEM
;
1475 else if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1477 rc
= GPG_ERR_ENOMEM
;
1481 if (update_element_list (elements
) == 0)
1483 rc
= GPG_ERR_ENOMEM
;
1488 rc
= path_list_recurse (client
, doc
, n
? n
->children
: n
, elements
);
1492 strv_free (req_orig
);
1499 recurse_xpath_nodeset (struct client_s
*client
, xmlDocPtr doc
,
1500 xmlNodeSetPtr nodes
, xmlChar
* value
,
1501 xmlBufferPtr
* result
, int cmd
, const xmlChar
* attr
)
1503 int i
= value
? nodes
->nodeNr
- 1 : 0;
1506 buf
= xmlBufferCreate ();
1509 return GPG_ERR_ENOMEM
;
1511 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1513 xmlNodePtr n
= nodes
->nodeTab
[i
];
1519 if (!value
&& !attr
)
1521 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1524 return GPG_ERR_BAD_DATA
;
1532 xmlNodeSetContent (n
, value
);
1533 rc
= update_element_mtime (n
);
1541 rc
= add_attribute (n
, (char *) attr
, (char *) value
);
1543 rc
= delete_attribute (client
, n
, attr
);
1555 convert_root_element (struct client_s
*client
, xmlNodePtr n
)
1557 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1563 xmlChar
*t
= xmlGetNodePath (n
);
1566 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1569 return GPG_ERR_AMBIGUOUS_NAME
;
1572 a
= xmlGetProp (n
, (xmlChar
*) "name");
1576 rc
= add_attribute (n
, "_name", (char *) a
);
1582 rc
= delete_attribute (client
, n
, (xmlChar
*) "name");
1587 xmlNodeSetName (n
, (xmlChar
*) "element");
1594 delete_attribute (struct client_s
*client
, xmlNodePtr n
, const xmlChar
* name
)
1599 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1600 return GPG_ERR_NOT_FOUND
;
1602 if (xmlRemoveProp (a
) == -1)
1603 return GPG_ERR_BAD_DATA
;
1605 if (client
&& xmlStrEqual (name
, (xmlChar
*) "_acl"))
1607 char *user
= create_acl_user (client
);
1609 rc
= add_attribute (n
, (char *) "_acl", user
);
1616 return update_element_mtime (n
);
1620 convert_elements_recurse (struct client_s
*client
, xmlDocPtr doc
,
1621 xmlNodePtr n
, unsigned depth
)
1627 for (n
= n
->children
; n
; n
= n
->next
)
1629 if (n
->type
== XML_ELEMENT_NODE
)
1635 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1637 xmlChar
*t
= xmlGetNodePath (n
);
1640 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1643 return GPG_ERR_AMBIGUOUS_NAME
;
1646 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1650 xmlChar
*t
= xmlGetNodePath (n
);
1653 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1656 return GPG_ERR_AMBIGUOUS_NAME
;
1659 xmlChar
*tmp
= xmlStrdup (n
->name
);
1662 return GPG_ERR_ENOMEM
;
1664 xmlNodeSetName (n
, (xmlChar
*) "element");
1665 rc
= add_attribute (n
, "_name", (char *) tmp
);
1673 rc
= convert_root_element (client
, n
);
1682 rc
= convert_elements_recurse (client
, doc
, n
, depth
);
1692 /* Renames ALL elements to the new "element" name. Existing element names are
1693 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1694 * elements can contain common characters that the XML parser barfs on (an
1695 * email address for example. */
1697 convert_pre_212_elements (xmlDocPtr doc
)
1699 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1701 log_write (_("Converting pre 2.12 data file..."));
1702 return convert_elements_recurse (NULL
, doc
, n
, 0);
1706 validate_import (xmlNodePtr node
)
1713 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1715 if (n
->type
== XML_ELEMENT_NODE
)
1717 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1719 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1723 xmlChar
*t
= xmlGetNodePath (n
);
1725 log_write (_("Missing attribute '_name' at %s."), t
);
1727 return GPG_ERR_INV_VALUE
;
1730 if (!valid_xml_element (a
))
1732 xmlChar
*t
= xmlGetNodePath (n
);
1734 log_write (_("'%s' is not a valid element name at %s."), a
,
1738 return GPG_ERR_INV_VALUE
;
1742 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1747 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1749 update_element_mtime (n
);
1754 xmlChar
*t
= xmlGetNodePath (n
);
1756 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1765 rc
= validate_import (n
->children
);
1776 update_element_mtime (xmlNodePtr n
)
1778 return add_attribute (n
, NULL
, NULL
);
1782 unlink_node (xmlNodePtr n
)
1790 rc
= update_element_mtime (n
->parent
);
1797 parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
1801 xmlResetLastError ();
1802 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1803 if (!doc
&& xmlGetLastError ())
1804 return GPG_ERR_BAD_DATA
;
1807 return !doc
? GPG_ERR_ENOMEM
: 0;
1811 realpath_elements_cb (struct client_s
*client
, xmlNodePtr node
, char **target
,
1812 gpg_error_t
* rc
, char **req_orig
, void *data
)
1814 char *path
= *(char **) data
;
1815 char *tmp
= NULL
, *result
;
1820 *(char **) data
= NULL
;
1823 path
= strv_join ("\t", target
);
1827 *rc
= GPG_ERR_ENOMEM
;
1833 tmp
= strv_join ("\t", req_orig
);
1838 *rc
= GPG_ERR_ENOMEM
;
1844 result
= str_asprintf ("%s\t%s", path
, tmp
);
1846 result
= str_dup (path
);
1850 *rc
= GPG_ERR_ENOMEM
;
1858 *(char **) data
= result
;
1863 build_realpath (struct client_s
*client
, xmlDocPtr doc
, char *line
,
1864 struct string_s
** result
)
1871 struct string_s
*string
;
1874 if (strchr (line
, '\t') != NULL
)
1876 if ((req
= str_split (line
, "\t", 0)) == NULL
)
1877 return GPG_ERR_SYNTAX
;
1881 if ((req
= str_split (line
, " ", 0)) == NULL
)
1882 return GPG_ERR_SYNTAX
;
1885 n
= find_root_element (client
, doc
, &req
, &rc
, NULL
, 0, 0);
1892 rp
= strv_join ("\t", req
);
1896 return GPG_ERR_ENOMEM
;
1901 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, NULL
,
1902 realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
1911 string
= string_new (rp
);
1915 return GPG_ERR_ENOMEM
;
1918 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
1920 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
1922 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
1926 string_free (string
, 1);
1927 return GPG_ERR_ENOMEM
;
1941 node_to_element_path (xmlNodePtr node
)
1944 struct string_s
*str
= string_new ("");
1947 for (n
= node
; n
; n
= n
->parent
)
1951 for (child
= n
; child
; child
= child
->next
)
1953 if (child
->type
!= XML_ELEMENT_NODE
)
1956 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
1959 str
= string_prepend (str
, (char *) name
);
1961 name
= node_has_attribute (n
, (xmlChar
*) "target");
1963 str
= string_prepend (str
, "\t");
1965 str
= string_prepend (str
, "\t!");
1972 str
= string_erase (str
, 0, 1);
1974 string_free (str
, 0);
1980 * Recurse the element tree beginning at 'node' and find elements who point
1981 * back to 'src' or 'dst'. Also follows target attributes.
1984 find_child_to_target (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1985 xmlNodePtr src
, xmlNodePtr dst
, unsigned depth
,
1991 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
1992 return gpg_error (GPG_ERR_ELOOP
);
1994 for (n
= node
; n
; n
= n
->next
)
1998 if (n
->type
!= XML_ELEMENT_NODE
)
2001 if (n
== src
|| n
== dst
)
2002 return GPG_ERR_ELOOP
;
2004 target
= node_has_attribute (n
, (xmlChar
*) "target");
2008 char **result
= NULL
;
2010 tmp
= resolve_path (client
, doc
, target
, &result
, &rc
);
2015 rc
= find_child_to_target (client
, doc
, tmp
, src
, dst
, ++depth
,
2020 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELEMENT_NOT_FOUND
)
2031 rc
= find_child_to_target (client
, doc
, n
->children
, src
, dst
,
2046 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
2051 for (n
= src
; n
; n
= n
->next
)
2053 if (n
->type
!= XML_ELEMENT_NODE
)
2062 rc
= find_child_of_parent (doc
, n
->children
, dst
);
2069 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
2074 for (n
= node
; n
; n
= n
->parent
)
2076 if (n
->type
!= XML_ELEMENT_NODE
)
2080 for (tmp
= n
->next
; tmp
; n
= n
->next
)
2082 if (n
->type
!= XML_ELEMENT_NODE
)
2086 return GPG_ERR_ELOOP
;
2091 return GPG_ERR_ELOOP
;
2098 validate_target_attribute (struct client_s
*client
, xmlDocPtr doc
,
2099 const char *src
, xmlNodePtr dst_node
)
2102 xmlNodePtr src_node
;
2103 char **src_req
= NULL
;
2105 src_node
= resolve_path (client
, doc
, (xmlChar
*) src
, &src_req
, &rc
);
2109 /* A destination element is a child of the source element. */
2110 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
2114 /* The destination element is a parent of the source element. */
2115 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
2119 /* A destination child element contains a target to the source element. */
2120 rc
= find_child_to_target (client
, doc
, dst_node
->children
, src_node
,
2126 strv_free (src_req
);
2130 /* The owner of the element is the first user listed in the _acl attribute
2131 * list. acl_check() should be called before calling this function. An empty
2132 * ACL is an error if the client is not invoking_user.
2135 is_element_owner (struct client_s
*client
, xmlNodePtr n
)
2137 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
2139 gpg_error_t rc
= GPG_ERR_EPERM
;
2147 users
= str_split((char *)acl
, ",", 0);
2148 if (users
&& *users
)
2153 if (client
->thd
->remote
)
2154 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
2156 user
= get_username (client
->thd
->peer
->uid
);
2158 user
= get_username (client
->thd
->peer
->uid
);
2161 rc
= !strcmp (*users
, user
) ? 0 : GPG_ERR_EPERM
;