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 (struct client_s
*client
, xmlNodePtr n
)
157 char *buf
= str_asprintf ("%li", time (NULL
));
161 return GPG_ERR_ENOMEM
;
163 rc
= add_attribute (client
, n
, "_ctime", buf
);
169 acl_check (struct client_s
*client
, xmlNodePtr n
)
171 gpg_error_t rc
= GPG_ERR_EACCES
;
172 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
173 char **users
= acl
? str_split((char *)acl
, ",", 0) : NULL
;
177 if (!acl
|| !users
|| !*users
)
180 return peer_is_invoker(client
);
183 if (!peer_is_invoker(client
))
186 for (p
= users
; *p
; p
++)
189 rc
= acl_check_common (client
, *p
,
190 client
->thd
->remote
? 0 : client
->thd
->peer
->uid
,
191 client
->thd
->remote
? 0 : client
->thd
->peer
->gid
,
194 rc
= acl_check_common (client
, *p
, client
->thd
->peer
->uid
,
195 client
->thd
->peer
->gid
, &allowed
);
204 return allowed
? 0 : GPG_ERR_EACCES
;
208 create_acl_user (struct client_s
*client
)
211 if (client
->thd
->remote
)
212 return str_asprintf ("#%s", client
->thd
->tls
->fp
);
215 return get_username (client
->thd
->peer
->uid
);
219 create_new_element (struct client_s
*client
, int verify
, xmlNodePtr parent
,
220 const char *name
, xmlNodePtr
* result
)
225 // Allow any client to create a non-existing root element.
226 if (parent
->parent
->type
!= XML_DOCUMENT_NODE
)
228 rc
= acl_check(client
, parent
);
233 n
= xmlNewNode (NULL
, (xmlChar
*) "element");
235 return GPG_ERR_ENOMEM
;
237 rc
= add_attribute (client
, n
, "_name", name
);
239 rc
= attr_ctime (client
, n
);
241 if (!rc
&& verify
&& parent
->parent
->type
!= XML_DOCUMENT_NODE
)
243 rc
= peer_is_invoker (client
);
244 if (rc
== GPG_ERR_EACCES
)
245 rc
= is_element_owner (client
, parent
);
250 xmlNodePtr p
= xmlAddChild (parent
, n
);
251 char *user
= create_acl_user (client
);
256 rc
= add_attribute(client
, p
, "_acl", user
);
266 new_root_element (struct client_s
*client
, xmlDocPtr doc
, char *name
)
268 xmlNodePtr root
= xmlDocGetRootElement (doc
);
272 return GPG_ERR_BAD_DATA
;
277 if (!valid_xml_element ((xmlChar
*) p
))
278 return GPG_ERR_INV_VALUE
;
280 return create_new_element (client
, 0, root
, p
, NULL
);
287 xmlTextWriterPtr wr
= xmlNewTextWriterDoc (&doc
, 0);
292 if (xmlTextWriterStartDocument (wr
, NULL
, "UTF-8", "yes"))
295 if (xmlTextWriterStartDTD (wr
, (xmlChar
*) "pwmd", NULL
, NULL
) == -1)
298 if (xmlTextWriterWriteDTDElement (wr
, (xmlChar
*) "pwmd",
299 (xmlChar
*) "(element)") == -1)
302 xmlTextWriterEndDTDElement (wr
);
304 if (xmlTextWriterWriteDTDAttlist (wr
, (xmlChar
*) "element",
305 (xmlChar
*) "_name CDATA #REQUIRED") ==
309 xmlTextWriterEndDTDAttlist (wr
);
310 xmlTextWriterEndDTD (wr
);
312 if (xmlTextWriterStartElement (wr
, (xmlChar
*) "pwmd"))
315 xmlTextWriterEndElement (wr
);
316 xmlTextWriterEndDocument (wr
);
317 xmlFreeTextWriter (wr
);
321 xmlTextWriterEndDocument (wr
);
322 xmlFreeTextWriter (wr
);
330 return create_dtd ();
334 find_element_node (xmlNodePtr node
)
338 if (n
&& n
->type
== XML_ELEMENT_NODE
)
341 for (n
= node
; n
; n
= n
->next
)
343 if (n
->type
== XML_ELEMENT_NODE
)
351 resolve_path (struct client_s
*client
, xmlDocPtr doc
, xmlChar
* path
,
352 char ***result
, gpg_error_t
* rc
)
357 req
= str_split ((char *) path
, "\t", 0);
360 *rc
= GPG_ERR_ENOMEM
;
364 n
= find_root_element (client
, doc
, &req
, rc
, NULL
, 0, 0);
372 n
= find_elements (client
, doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
, NULL
,
384 * Lists root element names; the value of the attribute "_name" of an element
385 * "element". If there's a target attribute both literal and non-literal
386 * element names will be added. This is the primary reason why XML entities
387 * cannot be used. There wouldn't be a way to get the literal an non-literal
391 list_root_elements (struct client_s
*client
, xmlDocPtr doc
,
392 struct string_s
** result
, int verbose
, int with_target
)
395 struct slist_s
*list
= NULL
;
397 struct string_s
*string
;
400 n
= xmlDocGetRootElement (doc
);
401 if (!n
|| !n
->children
)
402 return GPG_ERR_NO_DATA
;
404 for (n
= n
->children
; n
; n
= n
->next
)
407 xmlChar
*val
, *target
;
408 struct slist_s
*tlist
;
411 if (n
->type
!= XML_ELEMENT_NODE
)
414 a
= xmlHasProp (n
, (xmlChar
*) "_name");
415 if (!a
|| !a
->children
->content
)
418 val
= xmlNodeGetContent (a
->children
);
425 tmp
= str_asprintf ("!%s%s", (char *) val
,
426 verbose
? find_element_node (n
->children
) ? " +"
436 tlist
= slist_append (list
, tmp
);
445 target
= node_has_attribute (n
, (xmlChar
*) "target");
453 xmlNodePtr tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
455 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
456 || rc
== GPG_ERR_EACCES
)
458 t
= str_asprintf ("%s %s", (char *) val
,
459 rc
== GPG_ERR_ELOOP
? "O" :
460 rc
== GPG_ERR_EACCES
? "P" : "E");
465 struct string_s
*realpath
= NULL
;
469 rc
= build_realpath (client
, doc
, (char *) target
,
479 realpath
= string_prepend (realpath
, "T ");
482 t
= str_asprintf ("%s%s%s%s", (char *) val
,
484 && find_element_node (tnode
->children
))
485 || realpath
? " " : "", tnode
486 && find_element_node (tnode
->children
) ?
487 "+" : "", realpath
? realpath
->str
: "");
490 string_free (realpath
, 1);
497 t
= str_dup ((char *) val
);
503 rc
= rc
? rc
: GPG_ERR_ENOMEM
;
507 tlist
= slist_append (list
, t
);
524 total
= slist_length (list
);
526 return GPG_ERR_NO_DATA
;
528 string
= string_new (NULL
);
535 for (i
= 0; i
< total
; i
++)
537 char *val
= slist_nth_data (list
, i
);
539 string_append_printf (string
, "%s\n", val
);
542 string
= string_truncate (string
, string
->len
- 1);
546 total
= slist_length (list
);
547 for (i
= 0; i
< total
; i
++)
548 xfree (slist_nth_data (list
, i
));
555 * Prevents a sibling element past the current element path with the same
559 find_stop_node (xmlNodePtr node
)
563 for (n
= node
->parent
->children
; n
; n
= n
->next
)
573 create_target_elements_cb (struct client_s
*client
, int verify
,
574 xmlNodePtr node
, char **path
, gpg_error_t
*rc
,
579 xmlNodePtr parent
= data
;
581 for (i
= 0; req
[i
] && *req
[i
]; i
++)
585 if (parent
&& node
== parent
)
587 *rc
= GPG_ERR_CONFLICT
;
591 is_literal_element (&req
[i
]);
593 if ((n
= find_element (client
, node
, req
[i
],
594 find_stop_node (node
), rc
)) == NULL
||
595 (n
&& n
->parent
== node
->parent
))
599 *rc
= create_new_element (client
, verify
, node
, req
[i
], &node
);
612 find_text_node (xmlNodePtr node
)
616 if (n
&& n
->type
== XML_TEXT_NODE
)
619 for (n
= node
; n
; n
= n
->next
)
621 if (n
->type
== XML_TEXT_NODE
)
629 create_elements_cb (struct client_s
*client
, int verify
, xmlNodePtr node
,
630 char **elements
, gpg_error_t
* rc
, void *data
)
633 char **req
= elements
;
635 if (node
->type
== XML_TEXT_NODE
)
638 for (i
= 0; req
[i
] && *req
[i
]; i
++)
643 * Strip the first '!' if needed. If there's another, it's an
644 * rc. The syntax has already been checked before calling this
647 is_literal_element (&req
[i
]);
648 n
= find_element (client
, node
, req
[i
], find_stop_node (node
), rc
);
653 * If the found element has the same parent as the current element,
654 * they are siblings and the new element needs to be created as a
655 * child of the current element (node).
657 if (n
&& n
->parent
== node
->parent
)
662 *rc
= create_new_element (client
, 0, node
, req
[i
], &node
);
673 /* The root element is really req[0]. It is need as a pointer in case there is
674 * a target attribute so it can be updated. */
676 find_root_element (struct client_s
*client
, xmlDocPtr doc
, char ***req
,
677 gpg_error_t
* rc
, int *target
, int recursion_depth
,
680 xmlNodePtr n
= xmlDocGetRootElement (doc
);
682 char *root
= str_dup (*req
[0]);
683 int literal
= is_literal_element (&root
);
687 *rc
= GPG_ERR_ENOMEM
;
694 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
696 xmlChar
*t
= xmlGetNodePath (n
);
698 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
707 if (n
->type
== XML_ELEMENT_NODE
)
709 if (depth
== 0 && xmlStrEqual (n
->name
, (xmlChar
*) "pwmd"))
716 if (depth
== 1 && xmlStrEqual (n
->name
, (xmlChar
*) "element"))
718 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "_name");
723 if (xmlStrEqual (content
, (xmlChar
*) root
))
725 char **nreq
, **tmp
= NULL
;
727 *rc
= acl_check(client
, n
);
743 content
= node_has_attribute (n
, (xmlChar
*) "target");
748 if (!content
|| stop
)
757 if (strchr ((char *) content
, '\t'))
759 nreq
= str_split ((char *) content
, "\t", 0);
768 *rc
= GPG_ERR_ENOMEM
;
774 tmp
= strv_catv (nreq
, tmp
+ 1);
780 *rc
= GPG_ERR_ENOMEM
;
789 if (strv_printf (&tmp
, "%s", content
) == 0)
793 *rc
= GPG_ERR_ENOMEM
;
799 nreq
= strv_catv (tmp
, nreq
+ 1);
804 *rc
= GPG_ERR_ENOMEM
;
814 n
= find_root_element (client
, doc
, req
, rc
, target
,
827 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
832 find_element (struct client_s
*client
, xmlNodePtr node
, char *element
,
833 xmlNodePtr stop
, gpg_error_t
*rc
)
839 if (!node
|| !element
)
842 for (n
= node
; n
; n
= n
->next
)
844 if (n
->type
!= XML_ELEMENT_NODE
)
850 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
852 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
856 *rc
= acl_check(client
, n
);
870 node_has_attribute (xmlNodePtr n
, xmlChar
* attr
)
872 xmlAttrPtr a
= xmlHasProp (n
, attr
);
877 if (!a
->children
|| !a
->children
->content
)
880 return xmlGetProp (n
, attr
);
884 element_to_literal (char **element
)
886 char *p
= str_asprintf ("!%s", *element
);
896 /* Resolves elements in 'req' one at a time. It's recursive in case of
897 * "target" attributes. */
899 find_elements (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
900 char **req
, gpg_error_t
* rc
, int *target
,
901 xmlNodePtr (*found_fn
) (struct client_s
*, xmlNodePtr
, char **,
902 gpg_error_t
*, char **, void *),
903 xmlNodePtr (*not_found_fn
) (struct client_s
*, int, xmlNodePtr
,
904 char **, gpg_error_t
*, void *),
905 int is_list_command
, int recursion_depth
, void *data
, int stop
)
907 xmlNodePtr n
, last
, last_node
;
914 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
916 xmlChar
*t
= xmlGetNodePath (node
);
918 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
925 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++)
933 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
940 *rc
= GPG_ERR_ENOMEM
;
944 literal
= is_literal_element (&t
);
945 n
= find_element (client
, last
, t
, NULL
, rc
);
948 if (*rc
&& *rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
954 return not_found_fn (client
, 0,
955 found
? last_node
: last_node
->parent
, p
,
959 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
970 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "target");
971 char **nreq
= NULL
, **nnreq
;
975 if (is_list_command
== 1)
977 if (element_to_literal (&(*p
)) == 0)
979 *rc
= GPG_ERR_ENOMEM
;
990 if (!*(p
+ 1) && stop
)
996 if (strchr ((char *) content
, '\t') != NULL
)
998 if ((nreq
= str_split ((char *) content
, "\t", 0)) == NULL
)
1001 *rc
= GPG_ERR_INV_VALUE
;
1007 if ((nreq
= str_split ((char *) content
, " ", 0)) == NULL
)
1010 *rc
= GPG_ERR_INV_VALUE
;
1016 tmp
= find_root_element (client
, doc
, &nreq
, rc
, target
, 0, 0);
1026 found_fn (client
, tmp
, nreq
, rc
, p
+ 1, data
);
1035 if (!*(nreq
+ 1) && !*(p
+ 1))
1041 nnreq
= strv_catv (nreq
+ 1, p
+ 1);
1045 if (!nnreq
|| !*nnreq
)
1054 n
= find_elements (client
, doc
, tmp
->children
, nnreq
, rc
, NULL
,
1055 found_fn
, not_found_fn
, is_list_command
,
1056 recursion_depth
, data
, stop
);
1062 return not_found_fn (client
, 0, tmp
, p
+ 1, rc
, data
);
1064 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1070 char **zz
= p
+ 1, **qq
= nnreq
;
1072 if (strv_length (nnreq
) > strv_length (p
+ 1))
1075 for (; *qq
&& *zz
; zz
++)
1078 *zz
= str_dup (*qq
++);
1082 *rc
= GPG_ERR_ENOMEM
;
1098 update_element_list (struct element_list_s
*elements
)
1103 if (!elements
|| !elements
->elements
)
1106 line
= strv_join ("\t", elements
->elements
);
1111 strv_free (elements
->elements
);
1112 elements
->elements
= NULL
;
1113 l
= slist_append (elements
->list
, line
);
1123 path_list_recurse (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1124 struct element_list_s
*elements
)
1128 gpg_error_t error_flag
= 0;
1130 for (n
= node
; n
; n
= n
->next
)
1132 xmlChar
*target
= NULL
;
1133 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
1134 gpg_error_t err
= 0;
1141 if (n
->type
!= XML_ELEMENT_NODE
)
1144 rc
= acl_check(client
, n
);
1146 if (elements
->verbose
)
1149 (&elements
->elements
, "%s\t!%s%s%s", elements
->prefix
, a
,
1150 !rc
&& find_element_node (n
->children
) ? " +" : "",
1151 rc
== GPG_ERR_EACCES
? " P" : "") == 0)
1154 return GPG_ERR_ENOMEM
;
1158 if (strv_printf (&elements
->elements
, "%s\t!%s", elements
->prefix
, a
)
1162 return GPG_ERR_ENOMEM
;
1165 if (update_element_list (elements
) == 0)
1168 return GPG_ERR_ENOMEM
;
1172 if (rc
== GPG_ERR_EACCES
)
1184 target
= node_has_attribute (n
, (xmlChar
*) "target");
1188 char *save
= elements
->prefix
;
1189 int r
= elements
->resolving
;
1192 struct string_s
*realpath
= NULL
;
1194 tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
1195 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_ELEMENT_NOT_FOUND
1196 || rc
== GPG_ERR_EACCES
)
1198 if (rc
== GPG_ERR_ELOOP
)
1200 xmlChar
*t
= xmlGetNodePath (n
);
1202 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1206 if (elements
->verbose
)
1208 error_flag
= err
= rc
;
1212 else if (!elements
->verbose
&& rc
)
1219 path
= str_asprintf("%s\t%s", elements
->prefix
, a
);
1220 rc
= validate_target_attribute (client
, client
->doc
, path
, tnode
);
1222 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
1223 || rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1225 if (rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1226 error_flag
= err
= rc
;
1239 strv_printf (&elements
->elements
, "%s\t%s %s", elements
->prefix
,
1241 err
== GPG_ERR_ELOOP
? "O" :
1242 err
== GPG_ERR_EACCES
? "P" : "E");
1245 if (!err
&& elements
->with_target
)
1247 rc
= build_realpath (client
, doc
, (char *) target
, &realpath
);
1255 realpath
= string_prepend (realpath
, "T ");
1258 if (!err
&& elements
->verbose
)
1260 if (!strv_printf (&elements
->elements
, "%s\t%s%s%s%s",
1261 elements
->prefix
, a
,
1262 (tnode
&& find_element_node (tnode
->children
))
1263 || realpath
? " " : "", tnode
1264 && find_element_node (tnode
->children
) ? "+" :
1265 "", realpath
? realpath
->str
: ""))
1269 return GPG_ERR_ENOMEM
;
1274 (&elements
->elements
, "%s\t%s", elements
->prefix
, a
))
1278 return GPG_ERR_ENOMEM
;
1282 string_free (realpath
, 1);
1284 tmp
= strv_join ("\t", elements
->elements
);
1289 return GPG_ERR_ENOMEM
;
1292 if (update_element_list (elements
) == 0)
1297 return GPG_ERR_ENOMEM
;
1300 if (!err
&& elements
->recurse
)
1302 /* Prune element flags. */
1303 if (elements
->verbose
&& strchr (tmp
, ' '))
1307 for (p
= tmp
; *p
; p
++)
1317 elements
->prefix
= tmp
;
1318 elements
->resolving
= 1;
1319 rc
= create_path_list (client
, doc
, elements
, (char *) target
);
1320 elements
->resolving
= r
;
1321 elements
->prefix
= save
;
1323 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELOOP
1324 && gpg_err_code(rc
) != GPG_ERR_EACCES
)
1332 error_flag
= err
= rc
;
1341 if (n
->children
&& elements
->recurse
&& err
!= GPG_ERR_EACCES
)
1343 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1344 char *save
= elements
->prefix
;
1349 return GPG_ERR_ENOMEM
;
1352 elements
->prefix
= tmp
;
1353 rc
= path_list_recurse (client
, doc
, n
->children
, elements
);
1354 xfree (elements
->prefix
);
1355 elements
->prefix
= save
;
1359 if (gpg_err_code(rc
) == GPG_ERR_ELOOP
1360 || gpg_err_code (rc
) == GPG_ERR_EACCES
1361 || gpg_err_code (rc
) == GPG_ERR_ELEMENT_NOT_FOUND
)
1363 error_flag
= err
= rc
;
1377 return error_flag
== GPG_ERR_ELOOP
|| error_flag
== GPG_ERR_EACCES
1382 add_attribute (struct client_s
*client
, xmlNodePtr node
, const char *name
,
1388 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1389 return GPG_ERR_BAD_DATA
;
1391 if (client
&& name
&& !xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_acl"))
1393 xmlChar
*acl
= node_has_attribute (node
, (xmlChar
*) "_acl");
1397 char *user
= create_acl_user (client
);
1401 rc
= add_attribute (client
, node
, (char *) "_acl", user
);
1411 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1414 buf
= str_asprintf ("%li", time (NULL
));
1415 rc
= add_attribute (client
, node
, "_mtime", buf
);
1421 * From the element path 'path', find sub-nodes and append them to the list.
1424 create_path_list (struct client_s
*client
, xmlDocPtr doc
,
1425 struct element_list_s
* elements
, char *path
)
1428 char **req
, **req_orig
;
1432 req
= str_split (path
, "\t", 0);
1435 req
= str_split (path
, " ", 0);
1437 return GPG_ERR_SYNTAX
;
1440 req_orig
= strv_dup (req
);
1443 rc
= GPG_ERR_ENOMEM
;
1447 n
= find_root_element (client
, doc
, &req
, &rc
, &a_target
, 0, 0);
1448 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
1449 || rc
== GPG_ERR_EACCES
)
1450 && elements
->verbose
&& a_target
)
1452 if (rc
!= GPG_ERR_EACCES
)
1458 if (rc
== GPG_ERR_EACCES
)
1461 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1472 *req
= str_dup (*req_orig
);
1480 find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, &e_target
,
1481 NULL
, NULL
, 1, 0, NULL
, 0);
1483 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1493 if (!elements
->prefix
)
1498 * If any req_orig element contains no target the element should be
1499 * prefixed with the literal character. Not really crucial if the
1500 * client isn't human because child elements are prefixed for the
1501 * current path. But may be confusing if editing by hand.
1503 elements
->prefix
= strv_join ("\t", req_orig
);
1505 if (!elements
->prefix
)
1507 rc
= GPG_ERR_ENOMEM
;
1511 if (elements
->verbose
)
1514 struct string_s
*realpath
= NULL
;
1515 gpg_error_t allowed
= rc
;
1519 if (!allowed
&& a_target
&& elements
->with_target
)
1521 rc
= build_realpath (client
, doc
, path
, &realpath
);
1525 realpath
= string_prepend (realpath
, "T ");
1528 ret
= strv_printf (&elements
->elements
, "%s%s%s%s%s",
1530 (!allowed
&& n
&& find_element_node (n
->children
))
1531 || realpath
? " " : "",
1532 (!allowed
&& n
&& find_element_node (n
->children
)) ? "+" : "",
1533 !allowed
&& realpath
? realpath
->str
: "",
1534 allowed
? " P" : "");
1535 string_free (realpath
, 1);
1538 rc
= GPG_ERR_ENOMEM
;
1542 else if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1544 rc
= GPG_ERR_ENOMEM
;
1548 if (update_element_list (elements
) == 0)
1550 rc
= GPG_ERR_ENOMEM
;
1555 rc
= path_list_recurse (client
, doc
, n
? n
->children
: n
, elements
);
1559 strv_free (req_orig
);
1566 recurse_xpath_nodeset (struct client_s
*client
, xmlDocPtr doc
,
1567 xmlNodeSetPtr nodes
, xmlChar
* value
,
1568 xmlBufferPtr
* result
, int cmd
, const xmlChar
* attr
)
1570 int i
= value
? nodes
->nodeNr
- 1 : 0;
1573 buf
= xmlBufferCreate ();
1576 return GPG_ERR_ENOMEM
;
1578 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1580 xmlNodePtr n
= nodes
->nodeTab
[i
];
1586 if (!value
&& !attr
)
1588 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1591 return GPG_ERR_BAD_DATA
;
1599 xmlNodeSetContent (n
, value
);
1600 rc
= update_element_mtime (client
, n
);
1608 rc
= add_attribute (client
, n
, (char *) attr
, (char *) value
);
1610 rc
= delete_attribute (client
, n
, attr
);
1622 convert_root_element (struct client_s
*client
, xmlNodePtr n
)
1624 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1630 xmlChar
*t
= xmlGetNodePath (n
);
1633 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1636 return GPG_ERR_AMBIGUOUS_NAME
;
1639 a
= xmlGetProp (n
, (xmlChar
*) "name");
1643 rc
= add_attribute (client
, n
, "_name", (char *) a
);
1649 rc
= delete_attribute (client
, n
, (xmlChar
*) "name");
1654 xmlNodeSetName (n
, (xmlChar
*) "element");
1661 delete_attribute (struct client_s
*client
, xmlNodePtr n
, const xmlChar
* name
)
1666 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1667 return GPG_ERR_NOT_FOUND
;
1669 if (xmlRemoveProp (a
) == -1)
1670 return GPG_ERR_BAD_DATA
;
1672 if (client
&& xmlStrEqual (name
, (xmlChar
*) "_acl"))
1674 char *user
= create_acl_user (client
);
1676 rc
= add_attribute (client
, n
, (char *) "_acl", user
);
1683 return update_element_mtime (client
, n
);
1687 convert_elements_recurse (struct client_s
*client
, xmlDocPtr doc
,
1688 xmlNodePtr n
, unsigned depth
)
1694 for (n
= n
->children
; n
; n
= n
->next
)
1696 if (n
->type
== XML_ELEMENT_NODE
)
1702 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1704 xmlChar
*t
= xmlGetNodePath (n
);
1707 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1710 return GPG_ERR_AMBIGUOUS_NAME
;
1713 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1717 xmlChar
*t
= xmlGetNodePath (n
);
1720 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1723 return GPG_ERR_AMBIGUOUS_NAME
;
1726 xmlChar
*tmp
= xmlStrdup (n
->name
);
1729 return GPG_ERR_ENOMEM
;
1731 xmlNodeSetName (n
, (xmlChar
*) "element");
1732 rc
= add_attribute (client
, n
, "_name", (char *) tmp
);
1740 rc
= convert_root_element (client
, n
);
1749 rc
= convert_elements_recurse (client
, doc
, n
, depth
);
1759 /* Renames ALL elements to the new "element" name. Existing element names are
1760 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1761 * elements can contain common characters that the XML parser barfs on (an
1762 * email address for example. */
1764 convert_pre_212_elements (xmlDocPtr doc
)
1766 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1768 log_write (_("Converting pre 2.12 data file..."));
1769 return convert_elements_recurse (NULL
, doc
, n
, 0);
1773 validate_import (struct client_s
*client
, xmlNodePtr node
)
1780 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1782 if (n
->type
== XML_ELEMENT_NODE
)
1784 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1786 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1790 xmlChar
*t
= xmlGetNodePath (n
);
1792 log_write (_("Missing attribute '_name' at %s."), t
);
1794 return GPG_ERR_INV_VALUE
;
1797 if (!valid_xml_element (a
))
1799 xmlChar
*t
= xmlGetNodePath (n
);
1801 log_write (_("'%s' is not a valid element name at %s."), a
,
1805 return GPG_ERR_INV_VALUE
;
1809 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1811 attr_ctime (client
, n
);
1814 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1816 update_element_mtime (client
, n
);
1821 xmlChar
*t
= xmlGetNodePath (n
);
1823 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1832 rc
= validate_import (client
, n
->children
);
1843 update_element_mtime (struct client_s
*client
, xmlNodePtr n
)
1845 return add_attribute (client
, n
, NULL
, NULL
);
1849 unlink_node (struct client_s
*client
, xmlNodePtr n
)
1857 rc
= update_element_mtime (client
, n
->parent
);
1864 parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
1868 xmlResetLastError ();
1869 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
1870 if (!doc
&& xmlGetLastError ())
1871 return GPG_ERR_BAD_DATA
;
1874 return !doc
? GPG_ERR_ENOMEM
: 0;
1878 realpath_elements_cb (struct client_s
*client
, xmlNodePtr node
, char **target
,
1879 gpg_error_t
* rc
, char **req_orig
, void *data
)
1881 char *path
= *(char **) data
;
1882 char *tmp
= NULL
, *result
;
1887 *(char **) data
= NULL
;
1890 path
= strv_join ("\t", target
);
1894 *rc
= GPG_ERR_ENOMEM
;
1900 tmp
= strv_join ("\t", req_orig
);
1905 *rc
= GPG_ERR_ENOMEM
;
1911 result
= str_asprintf ("%s\t%s", path
, tmp
);
1913 result
= str_dup (path
);
1917 *rc
= GPG_ERR_ENOMEM
;
1925 *(char **) data
= result
;
1930 build_realpath (struct client_s
*client
, xmlDocPtr doc
, char *line
,
1931 struct string_s
** result
)
1938 struct string_s
*string
;
1941 if (strchr (line
, '\t') != NULL
)
1943 if ((req
= str_split (line
, "\t", 0)) == NULL
)
1944 return GPG_ERR_SYNTAX
;
1948 if ((req
= str_split (line
, " ", 0)) == NULL
)
1949 return GPG_ERR_SYNTAX
;
1952 n
= find_root_element (client
, doc
, &req
, &rc
, NULL
, 0, 0);
1959 rp
= strv_join ("\t", req
);
1963 return GPG_ERR_ENOMEM
;
1968 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, NULL
,
1969 realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
1978 string
= string_new (rp
);
1982 return GPG_ERR_ENOMEM
;
1985 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
1987 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
1989 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
1993 string_free (string
, 1);
1994 return GPG_ERR_ENOMEM
;
2008 node_to_element_path (xmlNodePtr node
)
2011 struct string_s
*str
= string_new ("");
2014 for (n
= node
; n
; n
= n
->parent
)
2018 for (child
= n
; child
; child
= child
->next
)
2020 if (child
->type
!= XML_ELEMENT_NODE
)
2023 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
2026 str
= string_prepend (str
, (char *) name
);
2028 name
= node_has_attribute (n
, (xmlChar
*) "target");
2030 str
= string_prepend (str
, "\t");
2032 str
= string_prepend (str
, "\t!");
2039 str
= string_erase (str
, 0, 1);
2041 string_free (str
, 0);
2047 * Recurse the element tree beginning at 'node' and find elements who point
2048 * back to 'src' or 'dst'. Also follows target attributes.
2051 find_child_to_target (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
2052 xmlNodePtr src
, xmlNodePtr dst
, unsigned depth
,
2058 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
2059 return gpg_error (GPG_ERR_ELOOP
);
2061 for (n
= node
; n
; n
= n
->next
)
2065 if (n
->type
!= XML_ELEMENT_NODE
)
2068 if (n
== src
|| n
== dst
)
2069 return GPG_ERR_ELOOP
;
2071 target
= node_has_attribute (n
, (xmlChar
*) "target");
2075 char **result
= NULL
;
2077 tmp
= resolve_path (client
, doc
, target
, &result
, &rc
);
2082 rc
= find_child_to_target (client
, doc
, tmp
, src
, dst
, ++depth
,
2087 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELEMENT_NOT_FOUND
)
2098 rc
= find_child_to_target (client
, doc
, n
->children
, src
, dst
,
2113 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
2118 for (n
= src
; n
; n
= n
->next
)
2120 if (n
->type
!= XML_ELEMENT_NODE
)
2129 rc
= find_child_of_parent (doc
, n
->children
, dst
);
2136 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
2141 for (n
= node
; n
; n
= n
->parent
)
2143 if (n
->type
!= XML_ELEMENT_NODE
)
2147 for (tmp
= n
->next
; tmp
; n
= n
->next
)
2149 if (n
->type
!= XML_ELEMENT_NODE
)
2153 return GPG_ERR_ELOOP
;
2158 return GPG_ERR_ELOOP
;
2165 validate_target_attribute (struct client_s
*client
, xmlDocPtr doc
,
2166 const char *src
, xmlNodePtr dst_node
)
2169 xmlNodePtr src_node
;
2170 char **src_req
= NULL
;
2172 src_node
= resolve_path (client
, doc
, (xmlChar
*) src
, &src_req
, &rc
);
2176 /* A destination element is a child of the source element. */
2177 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
2181 /* The destination element is a parent of the source element. */
2182 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
2186 /* A destination child element contains a target to the source element. */
2187 rc
= find_child_to_target (client
, doc
, dst_node
->children
, src_node
,
2193 strv_free (src_req
);
2197 /* The owner of the element is the first user listed in the _acl attribute
2198 * list. acl_check() should be called before calling this function. An empty
2199 * ACL is an error if the client is not invoking_user.
2202 is_element_owner (struct client_s
*client
, xmlNodePtr n
)
2204 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
2206 gpg_error_t rc
= GPG_ERR_EACCES
;
2214 users
= str_split((char *)acl
, ",", 0);
2215 if (users
&& *users
)
2220 if (client
->thd
->remote
)
2221 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
2223 user
= get_username (client
->thd
->peer
->uid
);
2225 user
= get_username (client
->thd
->peer
->uid
);
2228 rc
= !strcmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;