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_invoker(client
);
184 if (!peer_is_invoker(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
, int verify
, xmlNodePtr parent
,
221 const char *name
, xmlNodePtr
* result
)
226 // Allow any client to create a non-existing root element.
227 if (parent
->parent
->type
!= XML_DOCUMENT_NODE
)
229 rc
= acl_check(client
, parent
);
234 n
= xmlNewNode (NULL
, (xmlChar
*) "element");
236 return GPG_ERR_ENOMEM
;
238 rc
= add_attribute (n
, "_name", name
);
242 if (!rc
&& verify
&& parent
->parent
->type
!= XML_DOCUMENT_NODE
)
244 rc
= peer_is_invoker (client
);
245 if (rc
== GPG_ERR_EPERM
)
246 rc
= is_element_owner (client
, parent
);
251 xmlNodePtr p
= xmlAddChild (parent
, n
);
252 char *user
= create_acl_user (client
);
257 rc
= add_attribute(p
, "_acl", user
);
267 new_root_element (struct client_s
*client
, xmlDocPtr doc
, char *name
)
269 xmlNodePtr root
= xmlDocGetRootElement (doc
);
273 return GPG_ERR_BAD_DATA
;
278 if (!valid_xml_element ((xmlChar
*) p
))
279 return GPG_ERR_INV_VALUE
;
281 return create_new_element (client
, 0, root
, p
, NULL
);
288 xmlTextWriterPtr wr
= xmlNewTextWriterDoc (&doc
, 0);
293 if (xmlTextWriterStartDocument (wr
, NULL
, "UTF-8", "yes"))
296 if (xmlTextWriterStartDTD (wr
, (xmlChar
*) "pwmd", NULL
, NULL
) == -1)
299 if (xmlTextWriterWriteDTDElement (wr
, (xmlChar
*) "pwmd",
300 (xmlChar
*) "(element)") == -1)
303 xmlTextWriterEndDTDElement (wr
);
305 if (xmlTextWriterWriteDTDAttlist (wr
, (xmlChar
*) "element",
306 (xmlChar
*) "_name CDATA #REQUIRED") ==
310 xmlTextWriterEndDTDAttlist (wr
);
311 xmlTextWriterEndDTD (wr
);
313 if (xmlTextWriterStartElement (wr
, (xmlChar
*) "pwmd"))
316 xmlTextWriterEndElement (wr
);
317 xmlTextWriterEndDocument (wr
);
318 xmlFreeTextWriter (wr
);
322 xmlTextWriterEndDocument (wr
);
323 xmlFreeTextWriter (wr
);
331 return create_dtd ();
335 find_element_node (xmlNodePtr node
)
339 if (n
&& n
->type
== XML_ELEMENT_NODE
)
342 for (n
= node
; n
; n
= n
->next
)
344 if (n
->type
== XML_ELEMENT_NODE
)
352 resolve_path (struct client_s
*client
, xmlDocPtr doc
, xmlChar
* path
,
353 char ***result
, gpg_error_t
* rc
)
358 req
= str_split ((char *) path
, "\t", 0);
361 *rc
= GPG_ERR_ENOMEM
;
365 n
= find_root_element (client
, doc
, &req
, rc
, NULL
, 0, 0);
373 n
= find_elements (client
, doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
, NULL
,
385 * Lists root element names; the value of the attribute "_name" of an element
386 * "element". If there's a target attribute both literal and non-literal
387 * element names will be added. This is the primary reason why XML entities
388 * cannot be used. There wouldn't be a way to get the literal an non-literal
392 list_root_elements (struct client_s
*client
, xmlDocPtr doc
,
393 struct string_s
** result
, int verbose
, int with_target
)
396 struct slist_s
*list
= NULL
;
398 struct string_s
*string
;
401 n
= xmlDocGetRootElement (doc
);
402 if (!n
|| !n
->children
)
403 return GPG_ERR_NO_DATA
;
405 for (n
= n
->children
; n
; n
= n
->next
)
408 xmlChar
*val
, *target
;
409 struct slist_s
*tlist
;
412 if (n
->type
!= XML_ELEMENT_NODE
)
415 a
= xmlHasProp (n
, (xmlChar
*) "_name");
416 if (!a
|| !a
->children
->content
)
419 val
= xmlNodeGetContent (a
->children
);
427 str_asprintf ("!%s%s", (char *) val
,
428 verbose
? find_element_node (n
->children
) ? " +" : "" :
438 tlist
= slist_append (list
, tmp
);
447 target
= node_has_attribute (n
, (xmlChar
*) "target");
455 xmlNodePtr tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
457 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
458 || rc
== GPG_ERR_EPERM
)
460 t
= str_asprintf ("%s %s", (char *) val
,
461 rc
== GPG_ERR_ELOOP
? "O" :
462 rc
== GPG_ERR_EPERM
? "P" : "E");
467 struct string_s
*realpath
= NULL
;
471 rc
= build_realpath (client
, doc
, (char *) target
,
481 realpath
= string_prepend (realpath
, "T ");
484 t
= str_asprintf ("%s%s%s%s", (char *) val
,
486 && find_element_node (tnode
->children
))
487 || realpath
? " " : "", tnode
488 && find_element_node (tnode
->children
) ?
489 "+" : "", realpath
? realpath
->str
: "");
492 string_free (realpath
, 1);
499 t
= str_dup ((char *) val
);
505 rc
= rc
? rc
: GPG_ERR_ENOMEM
;
509 tlist
= slist_append (list
, t
);
526 total
= slist_length (list
);
528 return GPG_ERR_NO_DATA
;
530 string
= string_new (NULL
);
537 for (i
= 0; i
< total
; i
++)
539 char *val
= slist_nth_data (list
, i
);
541 string_append_printf (string
, "%s\n", val
);
544 string
= string_truncate (string
, string
->len
- 1);
548 total
= slist_length (list
);
549 for (i
= 0; i
< total
; i
++)
550 xfree (slist_nth_data (list
, i
));
557 * Prevents a sibling element past the current element path with the same
561 find_stop_node (xmlNodePtr node
)
565 for (n
= node
->parent
->children
; n
; n
= n
->next
)
575 create_target_elements_cb (struct client_s
*client
, int verify
,
576 xmlNodePtr node
, char **path
, gpg_error_t
*rc
,
581 xmlNodePtr parent
= data
;
583 for (i
= 0; req
[i
] && *req
[i
]; i
++)
587 if (parent
&& node
== parent
)
589 *rc
= GPG_ERR_CONFLICT
;
593 is_literal_element (&req
[i
]);
595 if ((n
= find_element (client
, node
, req
[i
],
596 find_stop_node (node
), rc
)) == NULL
||
597 (n
&& n
->parent
== node
->parent
))
601 *rc
= create_new_element (client
, verify
, node
, req
[i
], &node
);
614 find_text_node (xmlNodePtr node
)
618 if (n
&& n
->type
== XML_TEXT_NODE
)
621 for (n
= node
; n
; n
= n
->next
)
623 if (n
->type
== XML_TEXT_NODE
)
631 create_elements_cb (struct client_s
*client
, int verify
, xmlNodePtr node
,
632 char **elements
, gpg_error_t
* rc
, void *data
)
635 char **req
= elements
;
637 if (node
->type
== XML_TEXT_NODE
)
640 for (i
= 0; req
[i
] && *req
[i
]; i
++)
645 * Strip the first '!' if needed. If there's another, it's an
646 * rc. The syntax has already been checked before calling this
649 is_literal_element (&req
[i
]);
650 n
= find_element (client
, node
, req
[i
], find_stop_node (node
), rc
);
655 * If the found element has the same parent as the current element,
656 * they are siblings and the new element needs to be created as a
657 * child of the current element (node).
659 if (n
&& n
->parent
== node
->parent
)
664 *rc
= create_new_element (client
, 0, node
, req
[i
], &node
);
675 /* The root element is really req[0]. It is need as a pointer in case there is
676 * a target attribute so it can be updated. */
678 find_root_element (struct client_s
*client
, xmlDocPtr doc
, char ***req
,
679 gpg_error_t
* rc
, int *target
, int recursion_depth
,
682 xmlNodePtr n
= xmlDocGetRootElement (doc
);
684 char *root
= str_dup (*req
[0]);
685 int literal
= is_literal_element (&root
);
689 *rc
= GPG_ERR_ENOMEM
;
696 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
698 xmlChar
*t
= xmlGetNodePath (n
);
700 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
709 if (n
->type
== XML_ELEMENT_NODE
)
711 if (depth
== 0 && xmlStrEqual (n
->name
, (xmlChar
*) "pwmd"))
718 if (depth
== 1 && xmlStrEqual (n
->name
, (xmlChar
*) "element"))
720 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "_name");
725 if (xmlStrEqual (content
, (xmlChar
*) root
))
727 char **nreq
, **tmp
= NULL
;
729 *rc
= acl_check(client
, n
);
745 content
= node_has_attribute (n
, (xmlChar
*) "target");
750 if (!content
|| stop
)
759 if (strchr ((char *) content
, '\t'))
761 nreq
= str_split ((char *) content
, "\t", 0);
770 *rc
= GPG_ERR_ENOMEM
;
776 tmp
= strv_catv (nreq
, tmp
+ 1);
782 *rc
= GPG_ERR_ENOMEM
;
791 if (strv_printf (&tmp
, "%s", content
) == 0)
795 *rc
= GPG_ERR_ENOMEM
;
801 nreq
= strv_catv (tmp
, nreq
+ 1);
806 *rc
= GPG_ERR_ENOMEM
;
817 find_root_element (client
, doc
, req
, rc
, target
,
830 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
835 find_element (struct client_s
*client
, xmlNodePtr node
, char *element
,
836 xmlNodePtr stop
, gpg_error_t
*rc
)
842 if (!node
|| !element
)
845 for (n
= node
; n
; n
= n
->next
)
847 if (n
->type
!= XML_ELEMENT_NODE
)
853 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
855 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
859 *rc
= acl_check(client
, n
);
873 node_has_attribute (xmlNodePtr n
, xmlChar
* attr
)
875 xmlAttrPtr a
= xmlHasProp (n
, attr
);
880 if (!a
->children
|| !a
->children
->content
)
883 return xmlGetProp (n
, attr
);
887 element_to_literal (char **element
)
889 char *p
= str_asprintf ("!%s", *element
);
899 /* Resolves elements in 'req' one at a time. It's recursive in case of
900 * "target" attributes. */
902 find_elements (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
903 char **req
, gpg_error_t
* rc
, int *target
,
904 xmlNodePtr (*found_fn
) (struct client_s
*, xmlNodePtr
, char **,
905 gpg_error_t
*, char **, void *),
906 xmlNodePtr (*not_found_fn
) (struct client_s
*, int, xmlNodePtr
,
907 char **, gpg_error_t
*, void *),
908 int is_list_command
, int recursion_depth
, void *data
, int stop
)
910 xmlNodePtr n
, last
, last_node
;
917 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
919 xmlChar
*t
= xmlGetNodePath (node
);
921 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
928 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++)
936 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
943 *rc
= GPG_ERR_ENOMEM
;
947 literal
= is_literal_element (&t
);
948 n
= find_element (client
, last
, t
, NULL
, rc
);
951 if (*rc
&& *rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
957 return not_found_fn (client
, 1,
958 found
? last_node
: last_node
->parent
, p
,
962 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
973 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "target");
974 char **nreq
= NULL
, **nnreq
;
978 if (is_list_command
== 1)
980 if (element_to_literal (&(*p
)) == 0)
982 *rc
= GPG_ERR_ENOMEM
;
993 if (!*(p
+ 1) && stop
)
999 if (strchr ((char *) content
, '\t') != NULL
)
1001 if ((nreq
= str_split ((char *) content
, "\t", 0)) == NULL
)
1004 *rc
= GPG_ERR_INV_VALUE
;
1010 if ((nreq
= str_split ((char *) content
, " ", 0)) == NULL
)
1013 *rc
= GPG_ERR_INV_VALUE
;
1019 tmp
= find_root_element (client
, doc
, &nreq
, rc
, target
, 0, 0);
1029 found_fn (client
, tmp
, nreq
, rc
, p
+ 1, data
);
1038 if (!*(nreq
+ 1) && !*(p
+ 1))
1044 nnreq
= strv_catv (nreq
+ 1, p
+ 1);
1048 if (!nnreq
|| !*nnreq
)
1057 n
= find_elements (client
, doc
, tmp
->children
, nnreq
, rc
, NULL
,
1058 found_fn
, not_found_fn
, is_list_command
,
1059 recursion_depth
, data
, stop
);
1065 return not_found_fn (client
, 1, tmp
, p
+ 1, rc
, data
);
1067 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1073 char **zz
= p
+ 1, **qq
= nnreq
;
1075 if (strv_length (nnreq
) > strv_length (p
+ 1))
1078 for (; *qq
&& *zz
; zz
++)
1081 *zz
= str_dup (*qq
++);
1085 *rc
= GPG_ERR_ENOMEM
;
1101 update_element_list (struct element_list_s
*elements
)
1106 if (!elements
|| !elements
->elements
)
1109 line
= strv_join ("\t", elements
->elements
);
1114 strv_free (elements
->elements
);
1115 elements
->elements
= NULL
;
1116 l
= slist_append (elements
->list
, line
);
1126 path_list_recurse (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1127 struct element_list_s
*elements
)
1131 gpg_error_t error_flag
= 0;
1133 for (n
= node
; n
; n
= n
->next
)
1135 xmlChar
*target
= NULL
;
1136 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
1143 if (n
->type
!= XML_ELEMENT_NODE
)
1146 rc
= acl_check(client
, n
);
1148 if (elements
->verbose
)
1151 (&elements
->elements
, "%s\t!%s%s%s", elements
->prefix
, a
,
1152 !rc
&& find_element_node (n
->children
) ? " +" : "",
1153 rc
== GPG_ERR_EPERM
? " P" : "") == 0)
1156 return GPG_ERR_ENOMEM
;
1160 if (strv_printf (&elements
->elements
, "%s\t!%s", elements
->prefix
, a
)
1164 return GPG_ERR_ENOMEM
;
1167 if (update_element_list (elements
) == 0)
1170 return GPG_ERR_ENOMEM
;
1174 if (rc
== GPG_ERR_EPERM
)
1186 target
= node_has_attribute (n
, (xmlChar
*) "target");
1190 char *save
= elements
->prefix
;
1191 int r
= elements
->resolving
;
1194 struct string_s
*realpath
= NULL
;
1196 tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
1197 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_ELEMENT_NOT_FOUND
1198 || rc
== GPG_ERR_EPERM
)
1200 if (rc
== GPG_ERR_ELOOP
)
1202 xmlChar
*t
= xmlGetNodePath (n
);
1204 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1208 if (elements
->verbose
)
1210 strv_printf (&elements
->elements
, "%s\t%s %s",
1211 elements
->prefix
, a
,
1212 rc
== GPG_ERR_ELOOP
? "O" :
1213 rc
== GPG_ERR_EPERM
? "P" : "E");
1214 error_flag
= err
= rc
;
1220 if (!elements
->verbose
&& rc
)
1227 if (elements
->with_target
)
1229 rc
= build_realpath (client
, doc
, (char *) target
, &realpath
);
1237 realpath
= string_prepend (realpath
, "T ");
1240 if (elements
->verbose
)
1242 if (!strv_printf (&elements
->elements
, "%s\t%s%s%s%s",
1243 elements
->prefix
, a
,
1244 (tnode
&& find_element_node (tnode
->children
))
1245 || realpath
? " " : "", tnode
1246 && find_element_node (tnode
->children
) ? "+" :
1247 "", realpath
? realpath
->str
: ""))
1251 return GPG_ERR_ENOMEM
;
1256 (&elements
->elements
, "%s\t%s", elements
->prefix
, a
))
1260 return GPG_ERR_ENOMEM
;
1265 string_free (realpath
, 1);
1267 tmp
= strv_join ("\t", elements
->elements
);
1273 return GPG_ERR_ENOMEM
;
1276 if (update_element_list (elements
) == 0)
1281 return GPG_ERR_ENOMEM
;
1284 if (!err
&& elements
->recurse
)
1286 /* Prune element flags. */
1287 if (elements
->verbose
&& strchr (tmp
, ' '))
1291 for (p
= tmp
; *p
; p
++)
1301 elements
->prefix
= tmp
;
1302 elements
->resolving
= 1;
1303 rc
= create_path_list (client
, doc
, elements
, (char *) target
);
1304 elements
->resolving
= r
;
1305 elements
->prefix
= save
;
1307 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELOOP
1308 && gpg_err_code(rc
) != GPG_ERR_EPERM
)
1316 error_flag
= err
= rc
;
1325 if (n
->children
&& elements
->recurse
&& err
!= GPG_ERR_ELOOP
1326 && err
!= GPG_ERR_EPERM
)
1328 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1329 char *save
= elements
->prefix
;
1334 return GPG_ERR_ENOMEM
;
1337 elements
->prefix
= tmp
;
1338 rc
= path_list_recurse (client
, doc
, n
->children
, elements
);
1339 xfree (elements
->prefix
);
1340 elements
->prefix
= save
;
1352 return error_flag
== GPG_ERR_ELOOP
|| error_flag
== GPG_ERR_EPERM
1357 add_attribute (xmlNodePtr node
, const char *name
, const char *value
)
1362 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1363 return GPG_ERR_BAD_DATA
;
1365 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1368 buf
= str_asprintf ("%li", time (NULL
));
1369 rc
= add_attribute (node
, "_mtime", buf
);
1375 * From the element path 'path', find sub-nodes and append them to the list.
1378 create_path_list (struct client_s
*client
, xmlDocPtr doc
,
1379 struct element_list_s
* elements
, char *path
)
1382 char **req
, **req_orig
;
1386 req
= str_split (path
, "\t", 0);
1389 req
= str_split (path
, " ", 0);
1391 return GPG_ERR_SYNTAX
;
1394 req_orig
= strv_dup (req
);
1397 rc
= GPG_ERR_ENOMEM
;
1401 n
= find_root_element (client
, doc
, &req
, &rc
, &a_target
, 0, 0);
1402 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
1403 || rc
== GPG_ERR_EPERM
)
1404 && elements
->verbose
&& a_target
)
1406 if (rc
!= GPG_ERR_EPERM
)
1412 if (rc
== GPG_ERR_EPERM
)
1415 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1426 *req
= str_dup (*req_orig
);
1434 find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, &e_target
,
1435 NULL
, NULL
, 1, 0, NULL
, 0);
1437 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1447 if (!elements
->prefix
)
1452 * If any req_orig element contains no target the element should be
1453 * prefixed with the literal character. Not really crucial if the
1454 * client isn't human because child elements are prefixed for the
1455 * current path. But may be confusing if editing by hand.
1457 elements
->prefix
= strv_join ("\t", req_orig
);
1459 if (!elements
->prefix
)
1461 rc
= GPG_ERR_ENOMEM
;
1465 if (elements
->verbose
)
1468 struct string_s
*realpath
= NULL
;
1469 gpg_error_t allowed
= rc
;
1473 if (!allowed
&& a_target
&& elements
->with_target
)
1475 rc
= build_realpath (client
, doc
, path
, &realpath
);
1479 realpath
= string_prepend (realpath
, "T ");
1482 ret
= strv_printf (&elements
->elements
, "%s%s%s%s%s",
1484 (!allowed
&& n
&& find_element_node (n
->children
))
1485 || realpath
? " " : "",
1486 (!allowed
&& n
&& find_element_node (n
->children
)) ? "+" : "",
1487 !allowed
&& realpath
? realpath
->str
: "",
1488 allowed
? " P" : "");
1489 string_free (realpath
, 1);
1492 rc
= GPG_ERR_ENOMEM
;
1496 else if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1498 rc
= GPG_ERR_ENOMEM
;
1502 if (update_element_list (elements
) == 0)
1504 rc
= GPG_ERR_ENOMEM
;
1509 rc
= path_list_recurse (client
, doc
, n
? n
->children
: n
, elements
);
1513 strv_free (req_orig
);
1520 recurse_xpath_nodeset (struct client_s
*client
, xmlDocPtr doc
,
1521 xmlNodeSetPtr nodes
, xmlChar
* value
,
1522 xmlBufferPtr
* result
, int cmd
, const xmlChar
* attr
)
1524 int i
= value
? nodes
->nodeNr
- 1 : 0;
1527 buf
= xmlBufferCreate ();
1530 return GPG_ERR_ENOMEM
;
1532 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1534 xmlNodePtr n
= nodes
->nodeTab
[i
];
1540 if (!value
&& !attr
)
1542 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1545 return GPG_ERR_BAD_DATA
;
1553 xmlNodeSetContent (n
, value
);
1554 rc
= update_element_mtime (n
);
1562 rc
= add_attribute (n
, (char *) attr
, (char *) value
);
1564 rc
= delete_attribute (client
, n
, attr
);
1576 convert_root_element (struct client_s
*client
, xmlNodePtr n
)
1578 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1584 xmlChar
*t
= xmlGetNodePath (n
);
1587 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1590 return GPG_ERR_AMBIGUOUS_NAME
;
1593 a
= xmlGetProp (n
, (xmlChar
*) "name");
1597 rc
= add_attribute (n
, "_name", (char *) a
);
1603 rc
= delete_attribute (client
, n
, (xmlChar
*) "name");
1608 xmlNodeSetName (n
, (xmlChar
*) "element");
1615 delete_attribute (struct client_s
*client
, xmlNodePtr n
, const xmlChar
* name
)
1620 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1621 return GPG_ERR_NOT_FOUND
;
1623 if (xmlRemoveProp (a
) == -1)
1624 return GPG_ERR_BAD_DATA
;
1626 if (client
&& xmlStrEqual (name
, (xmlChar
*) "_acl"))
1628 char *user
= create_acl_user (client
);
1630 rc
= add_attribute (n
, (char *) "_acl", user
);
1637 return update_element_mtime (n
);
1641 convert_elements_recurse (struct client_s
*client
, xmlDocPtr doc
,
1642 xmlNodePtr n
, unsigned depth
)
1648 for (n
= n
->children
; n
; n
= n
->next
)
1650 if (n
->type
== XML_ELEMENT_NODE
)
1656 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1658 xmlChar
*t
= xmlGetNodePath (n
);
1661 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1664 return GPG_ERR_AMBIGUOUS_NAME
;
1667 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1671 xmlChar
*t
= xmlGetNodePath (n
);
1674 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1677 return GPG_ERR_AMBIGUOUS_NAME
;
1680 xmlChar
*tmp
= xmlStrdup (n
->name
);
1683 return GPG_ERR_ENOMEM
;
1685 xmlNodeSetName (n
, (xmlChar
*) "element");
1686 rc
= add_attribute (n
, "_name", (char *) tmp
);
1694 rc
= convert_root_element (client
, n
);
1703 rc
= convert_elements_recurse (client
, doc
, n
, depth
);
1713 /* Renames ALL elements to the new "element" name. Existing element names are
1714 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1715 * elements can contain common characters that the XML parser barfs on (an
1716 * email address for example. */
1718 convert_pre_212_elements (xmlDocPtr doc
)
1720 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1722 log_write (_("Converting pre 2.12 data file..."));
1723 return convert_elements_recurse (NULL
, doc
, n
, 0);
1727 validate_import (xmlNodePtr node
)
1734 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1736 if (n
->type
== XML_ELEMENT_NODE
)
1738 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1740 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1744 xmlChar
*t
= xmlGetNodePath (n
);
1746 log_write (_("Missing attribute '_name' at %s."), t
);
1748 return GPG_ERR_INV_VALUE
;
1751 if (!valid_xml_element (a
))
1753 xmlChar
*t
= xmlGetNodePath (n
);
1755 log_write (_("'%s' is not a valid element name at %s."), a
,
1759 return GPG_ERR_INV_VALUE
;
1763 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1768 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1770 update_element_mtime (n
);
1775 xmlChar
*t
= xmlGetNodePath (n
);
1777 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1786 rc
= validate_import (n
->children
);
1797 update_element_mtime (xmlNodePtr n
)
1799 return add_attribute (n
, NULL
, NULL
);
1803 unlink_node (xmlNodePtr n
)
1811 rc
= update_element_mtime (n
->parent
);
1818 parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
1822 xmlResetLastError ();
1823 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1824 if (!doc
&& xmlGetLastError ())
1825 return GPG_ERR_BAD_DATA
;
1828 return !doc
? GPG_ERR_ENOMEM
: 0;
1832 realpath_elements_cb (struct client_s
*client
, xmlNodePtr node
, char **target
,
1833 gpg_error_t
* rc
, char **req_orig
, void *data
)
1835 char *path
= *(char **) data
;
1836 char *tmp
= NULL
, *result
;
1841 *(char **) data
= NULL
;
1844 path
= strv_join ("\t", target
);
1848 *rc
= GPG_ERR_ENOMEM
;
1854 tmp
= strv_join ("\t", req_orig
);
1859 *rc
= GPG_ERR_ENOMEM
;
1865 result
= str_asprintf ("%s\t%s", path
, tmp
);
1867 result
= str_dup (path
);
1871 *rc
= GPG_ERR_ENOMEM
;
1879 *(char **) data
= result
;
1884 build_realpath (struct client_s
*client
, xmlDocPtr doc
, char *line
,
1885 struct string_s
** result
)
1892 struct string_s
*string
;
1895 if (strchr (line
, '\t') != NULL
)
1897 if ((req
= str_split (line
, "\t", 0)) == NULL
)
1898 return GPG_ERR_SYNTAX
;
1902 if ((req
= str_split (line
, " ", 0)) == NULL
)
1903 return GPG_ERR_SYNTAX
;
1906 n
= find_root_element (client
, doc
, &req
, &rc
, NULL
, 0, 0);
1913 rp
= strv_join ("\t", req
);
1917 return GPG_ERR_ENOMEM
;
1922 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, NULL
,
1923 realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
1932 string
= string_new (rp
);
1936 return GPG_ERR_ENOMEM
;
1939 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
1941 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
1943 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
1947 string_free (string
, 1);
1948 return GPG_ERR_ENOMEM
;
1962 node_to_element_path (xmlNodePtr node
)
1965 struct string_s
*str
= string_new ("");
1968 for (n
= node
; n
; n
= n
->parent
)
1972 for (child
= n
; child
; child
= child
->next
)
1974 if (child
->type
!= XML_ELEMENT_NODE
)
1977 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
1980 str
= string_prepend (str
, (char *) name
);
1982 name
= node_has_attribute (n
, (xmlChar
*) "target");
1984 str
= string_prepend (str
, "\t");
1986 str
= string_prepend (str
, "\t!");
1993 str
= string_erase (str
, 0, 1);
1995 string_free (str
, 0);
2001 * Recurse the element tree beginning at 'node' and find elements who point
2002 * back to 'src' or 'dst'. Also follows target attributes.
2005 find_child_to_target (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
2006 xmlNodePtr src
, xmlNodePtr dst
, unsigned depth
,
2012 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
2013 return gpg_error (GPG_ERR_ELOOP
);
2015 for (n
= node
; n
; n
= n
->next
)
2019 if (n
->type
!= XML_ELEMENT_NODE
)
2022 if (n
== src
|| n
== dst
)
2023 return GPG_ERR_ELOOP
;
2025 target
= node_has_attribute (n
, (xmlChar
*) "target");
2029 char **result
= NULL
;
2031 tmp
= resolve_path (client
, doc
, target
, &result
, &rc
);
2036 rc
= find_child_to_target (client
, doc
, tmp
, src
, dst
, ++depth
,
2041 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELEMENT_NOT_FOUND
)
2052 rc
= find_child_to_target (client
, doc
, n
->children
, src
, dst
,
2067 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
2072 for (n
= src
; n
; n
= n
->next
)
2074 if (n
->type
!= XML_ELEMENT_NODE
)
2083 rc
= find_child_of_parent (doc
, n
->children
, dst
);
2090 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
2095 for (n
= node
; n
; n
= n
->parent
)
2097 if (n
->type
!= XML_ELEMENT_NODE
)
2101 for (tmp
= n
->next
; tmp
; n
= n
->next
)
2103 if (n
->type
!= XML_ELEMENT_NODE
)
2107 return GPG_ERR_ELOOP
;
2112 return GPG_ERR_ELOOP
;
2119 validate_target_attribute (struct client_s
*client
, xmlDocPtr doc
,
2120 const char *src
, xmlNodePtr dst_node
)
2123 xmlNodePtr src_node
;
2124 char **src_req
= NULL
;
2126 src_node
= resolve_path (client
, doc
, (xmlChar
*) src
, &src_req
, &rc
);
2130 /* A destination element is a child of the source element. */
2131 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
2135 /* The destination element is a parent of the source element. */
2136 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
2140 /* A destination child element contains a target to the source element. */
2141 rc
= find_child_to_target (client
, doc
, dst_node
->children
, src_node
,
2147 strv_free (src_req
);
2151 /* The owner of the element is the first user listed in the _acl attribute
2152 * list. acl_check() should be called before calling this function. An empty
2153 * ACL is an error if the client is not invoking_user.
2156 is_element_owner (struct client_s
*client
, xmlNodePtr n
)
2158 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
2160 gpg_error_t rc
= GPG_ERR_EPERM
;
2168 users
= str_split((char *)acl
, ",", 0);
2169 if (users
&& *users
)
2174 if (client
->thd
->remote
)
2175 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
2177 user
= get_username (client
->thd
->peer
->uid
);
2179 user
= get_username (client
->thd
->peer
->uid
);
2182 rc
= !strcmp (*users
, user
) ? 0 : GPG_ERR_EPERM
;