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_value (const char *str
)
94 valid_xml_attribute (const char *str
)
98 int ret
= valid_xml_element ((xmlChar
*)str
);
103 wc
= str_to_wchar ((const char *)str
);
108 for (c
= 0; c
< len
; c
++)
116 case 0x0300 ... 0x036F:
117 case 0x203F ... 0x2040:
125 case 'A' ... 'Z': break;
126 case 'a' ... 'z': break;
127 case 0xC0 ... 0xD6: break;
128 case 0xD8 ... 0xF6: break;
129 case 0xF8 ... 0x2FF: break;
130 case 0x370 ... 0x37D: break;
131 case 0x37F ... 0x1FFF: break;
132 case 0x200C ... 0x200D: break;
133 case 0x2070 ... 0x218F: break;
134 case 0x2C00 ... 0x2FEF: break;
135 case 0x3001 ... 0xD7FF: break;
136 case 0xF900 ... 0xFDCF: break;
137 case 0xFDF0 ... 0xFFFD: break;
138 case 0x10000 ... 0xEFFFF: break;
150 valid_xml_element (xmlChar
*str
)
155 if (!str
|| !*str
|| *str
== '!')
158 wc
= str_to_wchar ((const char *)str
);
163 for (c
= 0; c
< len
; c
++)
177 valid_element_path (char **path
, int with_content
)
179 char **dup
= NULL
, **p
;
184 /* Save some memory by not duplicating the element content. */
187 int i
, t
= strv_length (path
);
189 for (i
= 0; i
< t
- 1; i
++)
191 char **tmp
= xrealloc (dup
, (i
+ 2) * sizeof (char *));
200 dup
[i
] = str_dup (path
[i
]);
205 dup
= strv_dup (path
);
210 for (p
= dup
; *p
&& *(*p
); p
++)
212 is_literal_element (&(*p
));
213 if (!valid_xml_element ((xmlChar
*) * p
))
225 attr_ctime (struct client_s
*client
, xmlNodePtr n
)
227 char *buf
= str_asprintf ("%li", time (NULL
));
231 return GPG_ERR_ENOMEM
;
233 rc
= add_attribute (client
, n
, "_ctime", buf
);
239 acl_check (struct client_s
*client
, xmlNodePtr n
)
241 gpg_error_t rc
= GPG_ERR_EACCES
;
242 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
243 char **users
= acl
? str_split((char *)acl
, ",", 0) : NULL
;
247 if (!acl
|| !*acl
|| !users
|| !*users
)
251 return peer_is_invoker(client
);
254 if (!peer_is_invoker(client
))
261 for (p
= users
; *p
; p
++)
264 rc
= acl_check_common (client
, *p
,
265 client
->thd
->remote
? 0 : client
->thd
->peer
->uid
,
266 client
->thd
->remote
? 0 : client
->thd
->peer
->gid
,
269 rc
= acl_check_common (client
, *p
, client
->thd
->peer
->uid
,
270 client
->thd
->peer
->gid
, &allowed
);
277 // ATTR LIST makes use of FLAG_ACL_IGNORE to allow listing of element
278 // attributes that the client is not normally allowed access to.
279 if ((rc
== GPG_ERR_EACCES
|| !allowed
) && client
->flags
& FLAG_ACL_IGNORE
)
282 client
->flags
&= ~FLAG_ACL_IGNORE
;
284 // This flag is used in ATTR LIST to prevent listing attributes of
285 // children whose parent ACL does not allow access to the client.
286 client
->flags
|= FLAG_ACL_ERROR
;
293 return allowed
? 0 : GPG_ERR_EACCES
;
297 create_acl_user (struct client_s
*client
)
300 if (client
->thd
->remote
)
301 return str_asprintf ("#%s", client
->thd
->tls
->fp
);
304 return get_username (client
->thd
->peer
->uid
);
308 create_new_element (struct client_s
*client
, int verify
, xmlNodePtr parent
,
309 const char *name
, xmlNodePtr
* result
)
314 // Allow any client to create a non-existing root element.
315 if (parent
->parent
->type
!= XML_DOCUMENT_NODE
)
317 rc
= acl_check(client
, parent
);
322 n
= xmlNewNode (NULL
, (xmlChar
*) "element");
324 return GPG_ERR_ENOMEM
;
326 rc
= add_attribute (client
, n
, "_name", name
);
328 rc
= attr_ctime (client
, n
);
330 if (!rc
&& verify
&& parent
->parent
->type
!= XML_DOCUMENT_NODE
)
331 rc
= is_element_owner (client
, parent
);
335 xmlNodePtr p
= xmlAddChild (parent
, n
);
336 char *user
= create_acl_user (client
);
341 rc
= add_attribute(client
, p
, "_acl", user
);
351 new_root_element (struct client_s
*client
, xmlDocPtr doc
, char *name
)
353 xmlNodePtr root
= xmlDocGetRootElement (doc
);
357 return GPG_ERR_BAD_DATA
;
362 if (!valid_xml_element ((xmlChar
*) p
))
363 return GPG_ERR_INV_VALUE
;
365 return create_new_element (client
, 0, root
, p
, NULL
);
372 xmlTextWriterPtr wr
= xmlNewTextWriterDoc (&doc
, 0);
377 if (xmlTextWriterStartDocument (wr
, NULL
, "UTF-8", "yes"))
380 if (xmlTextWriterStartDTD (wr
, (xmlChar
*) "pwmd", NULL
, NULL
) == -1)
383 if (xmlTextWriterWriteDTDElement (wr
, (xmlChar
*) "pwmd",
384 (xmlChar
*) "(element)") == -1)
387 xmlTextWriterEndDTDElement (wr
);
389 if (xmlTextWriterWriteDTDAttlist (wr
, (xmlChar
*) "element",
390 (xmlChar
*) "_name CDATA #REQUIRED") ==
394 xmlTextWriterEndDTDAttlist (wr
);
395 xmlTextWriterEndDTD (wr
);
397 if (xmlTextWriterStartElement (wr
, (xmlChar
*) "pwmd"))
400 xmlTextWriterEndElement (wr
);
401 xmlTextWriterEndDocument (wr
);
402 xmlFreeTextWriter (wr
);
406 xmlTextWriterEndDocument (wr
);
407 xmlFreeTextWriter (wr
);
415 return create_dtd ();
419 find_element_node (xmlNodePtr node
)
423 if (n
&& n
->type
== XML_ELEMENT_NODE
)
426 for (n
= node
; n
; n
= n
->next
)
428 if (n
->type
== XML_ELEMENT_NODE
)
436 resolve_path (struct client_s
*client
, xmlDocPtr doc
, xmlChar
* path
,
437 char ***result
, gpg_error_t
* rc
)
442 req
= str_split ((char *) path
, "\t", 0);
445 *rc
= GPG_ERR_ENOMEM
;
449 n
= find_root_element (client
, doc
, &req
, rc
, NULL
, 0, 0);
457 n
= find_elements (client
, doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
, NULL
,
469 * Lists root element names; the value of the attribute "_name" of an element
470 * "element". If there's a target attribute both literal and non-literal
471 * element names will be added. This is the primary reason why XML entities
472 * cannot be used. There wouldn't be a way to get the literal an non-literal
476 list_root_elements (struct client_s
*client
, xmlDocPtr doc
,
477 struct string_s
** result
, int verbose
, int with_target
)
480 struct slist_s
*list
= NULL
;
482 struct string_s
*string
;
485 n
= xmlDocGetRootElement (doc
);
486 if (!n
|| !n
->children
)
487 return GPG_ERR_NO_DATA
;
489 for (n
= n
->children
; n
; n
= n
->next
)
492 xmlChar
*val
, *target
;
493 struct slist_s
*tlist
;
496 if (n
->type
!= XML_ELEMENT_NODE
)
499 a
= xmlHasProp (n
, (xmlChar
*) "_name");
500 if (!a
|| !a
->children
->content
)
503 val
= xmlNodeGetContent (a
->children
);
510 tmp
= str_asprintf ("!%s%s", (char *) val
,
511 verbose
? find_element_node (n
->children
) ? " +"
521 tlist
= slist_append (list
, tmp
);
530 target
= node_has_attribute (n
, (xmlChar
*) "target");
538 xmlNodePtr tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
540 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
541 || rc
== GPG_ERR_EACCES
)
543 t
= str_asprintf ("%s %s%s%s", (char *) val
,
544 rc
== GPG_ERR_ELOOP
? "O" :
545 rc
== GPG_ERR_EACCES
? "P" : "E",
546 with_target
? "T " : "",
547 with_target
? (char *)target
: "");
552 struct string_s
*realpath
= NULL
;
556 rc
= build_realpath (client
, doc
, (char *) target
,
566 realpath
= string_prepend (realpath
, "T ");
569 t
= str_asprintf ("%s%s%s%s", (char *) val
,
571 && find_element_node (tnode
->children
))
572 || realpath
? " " : "", tnode
573 && find_element_node (tnode
->children
) ?
574 "+" : "", realpath
? realpath
->str
: "");
577 string_free (realpath
, 1);
584 t
= str_dup ((char *) val
);
590 rc
= rc
? rc
: GPG_ERR_ENOMEM
;
594 tlist
= slist_append (list
, t
);
611 total
= slist_length (list
);
613 return GPG_ERR_NO_DATA
;
615 string
= string_new (NULL
);
622 for (i
= 0; i
< total
; i
++)
624 char *val
= slist_nth_data (list
, i
);
626 string_append_printf (string
, "%s\n", val
);
629 string
= string_truncate (string
, string
->len
- 1);
633 total
= slist_length (list
);
634 for (i
= 0; i
< total
; i
++)
635 xfree (slist_nth_data (list
, i
));
642 * Prevents a sibling element past the current element path with the same
646 find_stop_node (xmlNodePtr node
)
650 for (n
= node
->parent
->children
; n
; n
= n
->next
)
660 create_target_elements_cb (struct client_s
*client
, int verify
,
661 xmlNodePtr node
, char **path
, gpg_error_t
*rc
,
666 xmlNodePtr parent
= data
;
668 for (i
= 0; req
[i
] && *req
[i
]; i
++)
672 if (parent
&& node
== parent
)
674 *rc
= GPG_ERR_CONFLICT
;
678 is_literal_element (&req
[i
]);
680 if ((n
= find_element (client
, node
, req
[i
],
681 find_stop_node (node
), rc
)) == NULL
||
682 (n
&& n
->parent
== node
->parent
))
686 *rc
= create_new_element (client
, verify
, node
, req
[i
], &node
);
699 find_text_node (xmlNodePtr node
)
703 if (n
&& n
->type
== XML_TEXT_NODE
)
706 for (n
= node
; n
; n
= n
->next
)
708 if (n
->type
== XML_TEXT_NODE
)
716 create_elements_cb (struct client_s
*client
, int verify
, xmlNodePtr node
,
717 char **elements
, gpg_error_t
* rc
, void *data
)
720 char **req
= elements
;
722 if (*rc
&& *rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
725 if (node
->type
== XML_TEXT_NODE
)
728 for (i
= 0; req
[i
] && *req
[i
]; i
++)
733 * Strip the first '!' if needed. If there's another, it's an
734 * rc. The syntax has already been checked before calling this
737 is_literal_element (&req
[i
]);
738 n
= find_element (client
, node
, req
[i
], find_stop_node (node
), rc
);
743 * If the found element has the same parent as the current element,
744 * they are siblings and the new element needs to be created as a
745 * child of the current element (node).
747 if (n
&& n
->parent
== node
->parent
)
752 *rc
= create_new_element (client
, 0, node
, req
[i
], &node
);
763 /* The root element is really req[0]. It is need as a pointer in case there is
764 * a target attribute so it can be updated. */
766 find_root_element (struct client_s
*client
, xmlDocPtr doc
, char ***req
,
767 gpg_error_t
* rc
, int *target
, int recursion_depth
,
770 xmlNodePtr n
= xmlDocGetRootElement (doc
);
772 char *root
= str_dup (*req
[0]);
773 int literal
= is_literal_element (&root
);
777 *rc
= GPG_ERR_ENOMEM
;
784 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
786 xmlChar
*t
= xmlGetNodePath (n
);
788 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
797 if (n
->type
== XML_ELEMENT_NODE
)
799 if (depth
== 0 && xmlStrEqual (n
->name
, (xmlChar
*) "pwmd"))
806 if (depth
== 1 && xmlStrEqual (n
->name
, (xmlChar
*) "element"))
808 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "_name");
813 if (xmlStrEqual (content
, (xmlChar
*) root
))
815 char **nreq
, **tmp
= NULL
;
816 int acl
= client
->flags
& FLAG_ACL_IGNORE
;
818 *rc
= acl_check(client
, n
);
819 if ((*rc
&& *rc
!= GPG_ERR_EACCES
)
820 || (*rc
== GPG_ERR_EACCES
&& !acl
))
830 // This flag is cleared in acl_check() but we always
831 // allow ATTR LIST of root elements.
832 client
->flags
|= FLAG_ACL_IGNORE
;
843 content
= node_has_attribute (n
, (xmlChar
*) "target");
845 if (content
&& target
)
848 if (!content
|| stop
)
857 if (strchr ((char *) content
, '\t'))
859 nreq
= str_split ((char *) content
, "\t", 0);
868 *rc
= GPG_ERR_ENOMEM
;
874 tmp
= strv_catv (nreq
, tmp
+ 1);
880 *rc
= GPG_ERR_ENOMEM
;
889 if (strv_printf (&tmp
, "%s", content
) == 0)
893 *rc
= GPG_ERR_ENOMEM
;
899 nreq
= strv_catv (tmp
, nreq
+ 1);
904 *rc
= GPG_ERR_ENOMEM
;
914 return find_root_element (client
, doc
, req
, rc
, target
,
926 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
931 find_element (struct client_s
*client
, xmlNodePtr node
, char *element
,
932 xmlNodePtr stop
, gpg_error_t
*rc
)
938 if (!node
|| !element
)
941 for (n
= node
; n
; n
= n
->next
)
943 if (n
->type
!= XML_ELEMENT_NODE
)
949 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
951 if (a
&& xmlStrEqual (a
, (xmlChar
*) element
))
955 // Prevent ATTR LIST showing child element attributes for a parent
956 // whos ACL denies the client.
957 if (client
->flags
& FLAG_ACL_ERROR
)
959 *rc
= GPG_ERR_EACCES
;
963 *rc
= acl_check(client
, n
);
977 node_has_attribute (xmlNodePtr n
, xmlChar
* attr
)
979 xmlAttrPtr a
= xmlHasProp (n
, attr
);
984 if (!a
->children
|| !a
->children
->content
)
987 return xmlGetProp (n
, attr
);
991 element_to_literal (char **element
)
993 char *p
= str_asprintf ("!%s", *element
);
1003 /* Resolves elements in 'req' one at a time. It's recursive in case of
1004 * "target" attributes. */
1006 find_elements (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1007 char **req
, gpg_error_t
* rc
, int *target
,
1008 xmlNodePtr (*found_fn
) (struct client_s
*, xmlNodePtr
, char **,
1009 gpg_error_t
*, char **, void *),
1010 xmlNodePtr (*not_found_fn
) (struct client_s
*, int, xmlNodePtr
,
1011 char **, gpg_error_t
*, void *),
1012 int is_list_command
, int recursion_depth
, void *data
, int stop
)
1014 xmlNodePtr n
, last
, last_node
;
1021 if (max_recursion_depth
>= 1 && recursion_depth
> max_recursion_depth
)
1023 xmlChar
*t
= xmlGetNodePath (node
);
1025 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1028 *rc
= GPG_ERR_ELOOP
;
1032 for (last_node
= last
= n
= node
, p
= req
; *p
; p
++)
1040 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1047 *rc
= GPG_ERR_ENOMEM
;
1051 literal
= is_literal_element (&t
);
1052 n
= find_element (client
, last
, t
, NULL
, rc
);
1057 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1059 /* Fixes ATTR LIST when an element does not exist and the parent
1060 * denies access to children. Fixes leaking information about the
1061 * current elements children. */
1062 if (*rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& last_node
!= n
)
1064 gpg_error_t trc
= acl_check (client
, last_node
);
1070 return not_found_fn (client
, 0,
1071 found
? last_node
: last_node
->parent
, p
,
1082 xmlChar
*content
= node_has_attribute (n
, (xmlChar
*) "target");
1083 char **nreq
= NULL
, **nnreq
;
1087 if (is_list_command
== 1)
1089 if (element_to_literal (&(*p
)) == 0)
1091 *rc
= GPG_ERR_ENOMEM
;
1102 if (!*(p
+ 1) && stop
)
1108 if (strchr ((char *) content
, '\t') != NULL
)
1110 if ((nreq
= str_split ((char *) content
, "\t", 0)) == NULL
)
1113 *rc
= GPG_ERR_INV_VALUE
;
1119 if ((nreq
= str_split ((char *) content
, " ", 0)) == NULL
)
1122 *rc
= GPG_ERR_INV_VALUE
;
1128 tmp
= find_root_element (client
, doc
, &nreq
, rc
, target
, 0, 0);
1132 if (not_found_fn
&& *rc
== GPG_ERR_EACCES
)
1133 return not_found_fn (client
, 0, NULL
, p
, rc
, data
);
1139 found_fn (client
, tmp
, nreq
, rc
, p
+ 1, data
);
1148 if (!*(nreq
+ 1) && !*(p
+ 1))
1154 nnreq
= strv_catv (nreq
+ 1, p
+ 1);
1158 if (!nnreq
|| !*nnreq
)
1166 n
= find_elements (client
, doc
, tmp
->children
, nnreq
, rc
, NULL
,
1167 found_fn
, not_found_fn
, is_list_command
,
1168 recursion_depth
, data
, stop
);
1179 return not_found_fn (client
, 0, tmp
, p
, rc
, data
);
1181 *rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
1187 char **zz
= p
+ 1, **qq
= nnreq
;
1189 if (strv_length (nnreq
) > strv_length (p
+ 1))
1192 for (; *qq
&& *zz
; zz
++)
1195 *zz
= str_dup (*qq
++);
1199 *rc
= GPG_ERR_ENOMEM
;
1215 update_element_list (struct element_list_s
*elements
)
1220 if (!elements
|| !elements
->elements
)
1223 line
= strv_join ("\t", elements
->elements
);
1228 strv_free (elements
->elements
);
1229 elements
->elements
= NULL
;
1230 l
= slist_append (elements
->list
, line
);
1240 path_list_recurse (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
1241 struct element_list_s
*elements
)
1245 gpg_error_t error_flag
= 0;
1247 for (n
= node
; n
; n
= n
->next
)
1249 xmlChar
*target
= NULL
;
1250 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
1251 gpg_error_t err
= 0;
1258 if (n
->type
!= XML_ELEMENT_NODE
)
1261 rc
= acl_check(client
, n
);
1263 if (elements
->verbose
)
1266 (&elements
->elements
, "%s\t!%s%s%s", elements
->prefix
, a
,
1267 !rc
&& find_element_node (n
->children
) ? " +" : "",
1268 rc
== GPG_ERR_EACCES
? " P" : rc
? " E" : "") == 0)
1271 return GPG_ERR_ENOMEM
;
1275 if (strv_printf (&elements
->elements
, "%s\t!%s", elements
->prefix
, a
)
1279 return GPG_ERR_ENOMEM
;
1282 if (update_element_list (elements
) == 0)
1285 return GPG_ERR_ENOMEM
;
1288 if (rc
== GPG_ERR_EACCES
)
1300 target
= node_has_attribute (n
, (xmlChar
*) "target");
1304 char *save
= elements
->prefix
;
1305 int r
= elements
->resolving
;
1308 struct string_s
*realpath
= NULL
;
1310 tnode
= resolve_path (client
, doc
, target
, &req
, &rc
);
1311 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_ELEMENT_NOT_FOUND
1312 || rc
== GPG_ERR_EACCES
)
1314 if (rc
== GPG_ERR_ELOOP
)
1316 xmlChar
*t
= xmlGetNodePath (n
);
1318 log_write ("%s: %s", pwmd_strerror (GPG_ERR_ELOOP
), t
);
1322 if (elements
->verbose
)
1324 error_flag
= err
= rc
;
1328 else if (!elements
->verbose
&& rc
)
1335 path
= str_asprintf("%s\t%s", elements
->prefix
, a
);
1336 rc
= validate_target_attribute (client
, client
->doc
, path
, tnode
);
1338 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
1339 || rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1341 if (rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1342 error_flag
= err
= rc
;
1355 strv_printf (&elements
->elements
, "%s\t%s %s%s%s",
1356 elements
->prefix
, a
,
1357 err
== GPG_ERR_ELOOP
? "O" :
1358 err
== GPG_ERR_EACCES
? "P" : "E",
1359 elements
->with_target
? "T " : "",
1360 elements
->with_target
? (char *)target
: "");
1362 else if (!err
&& elements
->with_target
)
1364 rc
= build_realpath (client
, doc
, (char *) target
, &realpath
);
1372 realpath
= string_prepend (realpath
, "T ");
1375 if (!err
&& elements
->verbose
)
1377 if (!strv_printf (&elements
->elements
, "%s\t%s%s%s%s",
1378 elements
->prefix
, a
,
1379 (tnode
&& find_element_node (tnode
->children
))
1380 || realpath
? " " : "", tnode
1381 && find_element_node (tnode
->children
) ? "+" :
1382 "", realpath
? realpath
->str
: ""))
1386 return GPG_ERR_ENOMEM
;
1391 (&elements
->elements
, "%s\t%s", elements
->prefix
, a
))
1395 return GPG_ERR_ENOMEM
;
1399 string_free (realpath
, 1);
1401 tmp
= strv_join ("\t", elements
->elements
);
1406 return GPG_ERR_ENOMEM
;
1409 if (update_element_list (elements
) == 0)
1414 return GPG_ERR_ENOMEM
;
1417 if (!err
&& elements
->recurse
)
1419 /* Prune element flags. */
1420 if (elements
->verbose
&& strchr (tmp
, ' '))
1424 for (p
= tmp
; *p
; p
++)
1434 elements
->prefix
= tmp
;
1435 elements
->resolving
= 1;
1436 rc
= create_path_list (client
, doc
, elements
, (char *) target
);
1437 elements
->resolving
= r
;
1438 elements
->prefix
= save
;
1440 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELOOP
1441 && gpg_err_code(rc
) != GPG_ERR_EACCES
)
1449 error_flag
= err
= rc
;
1458 if (n
->children
&& elements
->recurse
)
1460 char *tmp
= str_asprintf ("%s\t!%s", elements
->prefix
, a
);
1461 char *save
= elements
->prefix
;
1466 return GPG_ERR_ENOMEM
;
1469 elements
->prefix
= tmp
;
1470 rc
= path_list_recurse (client
, doc
, n
->children
, elements
);
1471 xfree (elements
->prefix
);
1472 elements
->prefix
= save
;
1476 if (gpg_err_code(rc
) == GPG_ERR_ELOOP
1477 || gpg_err_code (rc
) == GPG_ERR_EACCES
1478 || gpg_err_code (rc
) == GPG_ERR_ELEMENT_NOT_FOUND
)
1480 error_flag
= err
= rc
;
1494 return error_flag
== GPG_ERR_ELOOP
|| error_flag
== GPG_ERR_EACCES
1499 add_attribute (struct client_s
*client
, xmlNodePtr node
, const char *name
,
1505 if (client
&& name
&& !strcmp (name
, "target"))
1507 rc
= is_element_owner (client
, node
);
1512 if (name
&& !xmlSetProp (node
, (xmlChar
*) name
, (xmlChar
*) value
))
1513 return GPG_ERR_BAD_DATA
;
1515 if (client
&& name
&& !xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_acl"))
1517 xmlChar
*acl
= node_has_attribute (node
, (xmlChar
*) "_acl");
1521 char *user
= create_acl_user (client
);
1525 rc
= add_attribute (client
, node
, (char *) "_acl", user
);
1535 if (name
&& xmlStrEqual ((xmlChar
*) name
, (xmlChar
*) "_mtime"))
1538 buf
= str_asprintf ("%li", time (NULL
));
1539 rc
= add_attribute (client
, node
, "_mtime", buf
);
1545 list_not_found_cb (struct client_s
*client
, int i
, xmlNodePtr node
,
1546 char **req
, gpg_error_t
*rc
, void *data
)
1548 struct element_list_s
*elements
= data
;
1550 if (*rc
!= GPG_ERR_EACCES
)
1553 elements
->data
= strv_dup (req
);
1558 * From the element path 'path', find sub-nodes and append them to the list.
1561 create_path_list (struct client_s
*client
, xmlDocPtr doc
,
1562 struct element_list_s
* elements
, char *path
)
1565 char **req
, **req_orig
;
1571 req
= str_split (path
, "\t", 0);
1574 req
= str_split (path
, " ", 0);
1576 return GPG_ERR_SYNTAX
;
1579 req_orig
= strv_dup (req
);
1582 rc
= GPG_ERR_ENOMEM
;
1586 n
= find_root_element (client
, doc
, &req
, &rc
, &a_target
, 0, 0);
1587 if ((rc
== GPG_ERR_ELEMENT_NOT_FOUND
|| rc
== GPG_ERR_ELOOP
1588 || rc
== GPG_ERR_EACCES
) && elements
->verbose
&& a_target
)
1590 if (rc
== GPG_ERR_EACCES
)
1597 if (rc
== GPG_ERR_EACCES
)
1604 if (!n
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1615 *req
= str_dup (*req_orig
);
1622 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, &e_target
,
1623 NULL
, list_not_found_cb
, 1, 0, elements
, 0);
1624 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
&& elements
->resolving
== 1)
1629 else if (rc
&& rc
!= GPG_ERR_EACCES
)
1636 if (!elements
->prefix
)
1641 * If any req_orig element contains no target the element should be
1642 * prefixed with the literal character. Not really crucial if the
1643 * client isn't human because child elements are prefixed for the
1644 * current path. But may be confusing if editing by hand.
1648 /* This is needed to prune the original requested element path to the
1649 * length of the failed element in the path. */
1650 int x
= strv_length (req_orig
)-strv_length ((char **)elements
->data
);
1654 for (i
= 0; i
<= x
; i
++)
1655 tmp
= strv_cat (tmp
, str_dup (req_orig
[i
]));
1657 elements
->prefix
= strv_join ("\t", tmp
);
1659 strv_free (elements
->data
);
1660 elements
->data
= NULL
;
1665 elements
->prefix
= str_dup (*req_orig
);
1667 elements
->prefix
= strv_join ("\t", req_orig
);
1670 if (!elements
->prefix
)
1672 rc
= GPG_ERR_ENOMEM
;
1676 if (elements
->verbose
)
1679 struct string_s
*realpath
= NULL
;
1681 if (!rc
&& allowed
&& a_target
&& elements
->with_target
)
1683 rc
= build_realpath (client
, doc
, path
, &realpath
);
1687 realpath
= string_prepend (realpath
, "T ");
1690 ret
= strv_printf (&elements
->elements
, "%s%s%s%s%s%s%s",
1692 (allowed
&& n
&& find_element_node (n
->children
))
1693 || realpath
? " " : "",
1694 (allowed
&& n
&& find_element_node (n
->children
)) ? "+" : "",
1695 allowed
&& realpath
? realpath
->str
: "",
1696 rc
== GPG_ERR_ELOOP
? " O" : "",
1697 rc
== GPG_ERR_ELEMENT_NOT_FOUND
? " E" : "",
1698 !allowed
? " P" : "");
1699 string_free (realpath
, 1);
1702 rc
= GPG_ERR_ENOMEM
;
1708 if (strv_printf (&elements
->elements
, "%s", elements
->prefix
) == 0)
1710 rc
= GPG_ERR_ENOMEM
;
1715 if (update_element_list (elements
) == 0)
1717 rc
= GPG_ERR_ENOMEM
;
1723 rc
= path_list_recurse (client
, doc
, n
? n
->children
: n
, elements
);
1726 strv_free (req_orig
);
1732 recurse_xpath_nodeset (struct client_s
*client
, xmlDocPtr doc
,
1733 xmlNodeSetPtr nodes
, xmlChar
* value
,
1734 xmlBufferPtr
* result
, int cmd
, const xmlChar
* attr
)
1736 int i
= value
? nodes
->nodeNr
- 1 : 0;
1739 buf
= xmlBufferCreate ();
1742 return GPG_ERR_ENOMEM
;
1744 for (; value
? i
>= 0 : i
< nodes
->nodeNr
; value
? i
-- : i
++)
1746 xmlNodePtr n
= nodes
->nodeTab
[i
];
1752 if (!value
&& !attr
)
1754 if (xmlNodeDump (buf
, doc
, n
, 0, 0) == -1)
1757 return GPG_ERR_BAD_DATA
;
1765 xmlNodeSetContent (n
, value
);
1766 rc
= update_element_mtime (client
, n
);
1774 rc
= add_attribute (client
, n
, (char *) attr
, (char *) value
);
1776 rc
= delete_attribute (client
, n
, attr
);
1788 convert_root_element (struct client_s
*client
, xmlNodePtr n
)
1790 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1796 xmlChar
*t
= xmlGetNodePath (n
);
1799 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1802 return GPG_ERR_AMBIGUOUS_NAME
;
1805 a
= xmlGetProp (n
, (xmlChar
*) "name");
1809 rc
= add_attribute (client
, n
, "_name", (char *) a
);
1815 rc
= delete_attribute (client
, n
, (xmlChar
*) "name");
1820 xmlNodeSetName (n
, (xmlChar
*) "element");
1827 delete_attribute (struct client_s
*client
, xmlNodePtr n
, const xmlChar
* name
)
1832 if ((a
= xmlHasProp (n
, name
)) == NULL
)
1833 return GPG_ERR_NOT_FOUND
;
1835 if (xmlRemoveProp (a
) == -1)
1836 return GPG_ERR_BAD_DATA
;
1838 if (client
&& xmlStrEqual (name
, (xmlChar
*) "_acl"))
1840 char *user
= create_acl_user (client
);
1842 rc
= add_attribute (client
, n
, (char *) "_acl", user
);
1849 return update_element_mtime (client
, n
);
1853 convert_elements_recurse (struct client_s
*client
, xmlDocPtr doc
,
1854 xmlNodePtr n
, unsigned depth
)
1860 for (n
= n
->children
; n
; n
= n
->next
)
1862 if (n
->type
== XML_ELEMENT_NODE
)
1868 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1870 xmlChar
*t
= xmlGetNodePath (n
);
1873 ("An existing \"element\" already exists. Please rename this element before converting. Path is: %s"),
1876 return GPG_ERR_AMBIGUOUS_NAME
;
1879 a
= xmlGetProp (n
, (xmlChar
*) "_name");
1883 xmlChar
*t
= xmlGetNodePath (n
);
1886 ("An existing \"_name\" attribute already exists. Please rename this attribute before converting. Path is: %s"),
1889 return GPG_ERR_AMBIGUOUS_NAME
;
1892 xmlChar
*tmp
= xmlStrdup (n
->name
);
1895 return GPG_ERR_ENOMEM
;
1897 xmlNodeSetName (n
, (xmlChar
*) "element");
1898 rc
= add_attribute (client
, n
, "_name", (char *) tmp
);
1906 rc
= convert_root_element (client
, n
);
1915 rc
= convert_elements_recurse (client
, doc
, n
, depth
);
1925 /* Renames ALL elements to the new "element" name. Existing element names are
1926 * stored as an attribute "_name". This was introduced in pwmd 2.12 so
1927 * elements can contain common characters that the XML parser barfs on (an
1928 * email address for example. */
1930 convert_pre_212_elements (xmlDocPtr doc
)
1932 xmlNodePtr n
= xmlDocGetRootElement (doc
);
1934 log_write (_("Converting pre 2.12 data file..."));
1935 return convert_elements_recurse (NULL
, doc
, n
, 0);
1939 validate_import (struct client_s
*client
, xmlNodePtr node
)
1946 for (xmlNodePtr n
= node
; n
; n
= n
->next
)
1948 if (n
->type
== XML_ELEMENT_NODE
)
1950 if (xmlStrEqual (n
->name
, (xmlChar
*) "element"))
1952 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
1956 xmlChar
*t
= xmlGetNodePath (n
);
1958 log_write (_("Missing attribute '_name' at %s."), t
);
1960 return GPG_ERR_INV_VALUE
;
1963 if (!valid_xml_element (a
))
1965 xmlChar
*t
= xmlGetNodePath (n
);
1967 log_write (_("'%s' is not a valid element name at %s."), a
,
1971 return GPG_ERR_INV_VALUE
;
1975 a
= xmlGetProp (n
, (xmlChar
*) "_ctime");
1977 attr_ctime (client
, n
);
1980 a
= xmlGetProp (n
, (xmlChar
*) "_mtime");
1982 update_element_mtime (client
, n
);
1987 xmlChar
*t
= xmlGetNodePath (n
);
1989 log_write (_("Warning: unknown element '%s' at %s. Ignoring."),
1998 rc
= validate_import (client
, n
->children
);
2009 update_element_mtime (struct client_s
*client
, xmlNodePtr n
)
2011 return add_attribute (client
, n
, NULL
, NULL
);
2015 unlink_node (struct client_s
*client
, xmlNodePtr n
)
2023 rc
= update_element_mtime (client
, n
->parent
);
2030 parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
2034 xmlResetLastError ();
2035 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2036 if (!doc
&& xmlGetLastError ())
2037 return GPG_ERR_BAD_DATA
;
2040 return !doc
? GPG_ERR_ENOMEM
: 0;
2044 realpath_elements_cb (struct client_s
*client
, xmlNodePtr node
, char **target
,
2045 gpg_error_t
* rc
, char **req_orig
, void *data
)
2047 char *path
= *(char **) data
;
2048 char *tmp
= NULL
, *result
;
2053 *(char **) data
= NULL
;
2056 path
= strv_join ("\t", target
);
2060 *rc
= GPG_ERR_ENOMEM
;
2066 tmp
= strv_join ("\t", req_orig
);
2071 *rc
= GPG_ERR_ENOMEM
;
2077 result
= str_asprintf ("%s\t%s", path
, tmp
);
2079 result
= str_dup (path
);
2083 *rc
= GPG_ERR_ENOMEM
;
2091 *(char **) data
= result
;
2096 build_realpath (struct client_s
*client
, xmlDocPtr doc
, char *line
,
2097 struct string_s
** result
)
2104 struct string_s
*string
;
2107 if (strchr (line
, '\t') != NULL
)
2109 if ((req
= str_split (line
, "\t", 0)) == NULL
)
2110 return GPG_ERR_SYNTAX
;
2114 if ((req
= str_split (line
, " ", 0)) == NULL
)
2115 return GPG_ERR_SYNTAX
;
2118 n
= find_root_element (client
, doc
, &req
, &rc
, NULL
, 0, 0);
2125 rp
= strv_join ("\t", req
);
2129 return GPG_ERR_ENOMEM
;
2134 n
= find_elements (client
, doc
, n
->children
, req
+ 1, &rc
, NULL
,
2135 realpath_elements_cb
, NULL
, 0, 0, &rp
, 0);
2144 string
= string_new (rp
);
2148 return GPG_ERR_ENOMEM
;
2151 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++)
2153 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+ 1) && *(t
+ 1) != '!'))
2155 struct string_s
*s
= string_insert_c (string
, !i
? i
++ : ++i
, '!');
2159 string_free (string
, 1);
2160 return GPG_ERR_ENOMEM
;
2174 node_to_element_path (xmlNodePtr node
)
2177 struct string_s
*str
= string_new ("");
2180 for (n
= node
; n
; n
= n
->parent
)
2184 for (child
= n
; child
; child
= child
->next
)
2186 if (child
->type
!= XML_ELEMENT_NODE
)
2189 xmlChar
*name
= node_has_attribute (n
, (xmlChar
*) "_name");
2192 str
= string_prepend (str
, (char *) name
);
2194 name
= node_has_attribute (n
, (xmlChar
*) "target");
2196 str
= string_prepend (str
, "\t");
2198 str
= string_prepend (str
, "\t!");
2205 str
= string_erase (str
, 0, 1);
2207 string_free (str
, 0);
2213 * Recurse the element tree beginning at 'node' and find elements who point
2214 * back to 'src' or 'dst'. Also follows target attributes.
2217 find_child_to_target (struct client_s
*client
, xmlDocPtr doc
, xmlNodePtr node
,
2218 xmlNodePtr src
, xmlNodePtr dst
, unsigned depth
,
2224 if (max_recursion_depth
>= 1 && depth
> max_recursion_depth
)
2225 return gpg_error (GPG_ERR_ELOOP
);
2227 for (n
= node
; n
; n
= n
->next
)
2231 if (n
->type
!= XML_ELEMENT_NODE
)
2234 if (n
== src
|| n
== dst
)
2235 return GPG_ERR_ELOOP
;
2237 target
= node_has_attribute (n
, (xmlChar
*) "target");
2241 char **result
= NULL
;
2243 tmp
= resolve_path (client
, doc
, target
, &result
, &rc
);
2248 rc
= find_child_to_target (client
, doc
, tmp
, src
, dst
, ++depth
,
2253 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ELEMENT_NOT_FOUND
)
2264 rc
= find_child_to_target (client
, doc
, n
->children
, src
, dst
,
2279 find_child_of_parent (xmlDocPtr doc
, xmlNodePtr src
, xmlNodePtr dst
)
2284 for (n
= src
; n
; n
= n
->next
)
2286 if (n
->type
!= XML_ELEMENT_NODE
)
2295 rc
= find_child_of_parent (doc
, n
->children
, dst
);
2302 find_parent_of_child (xmlDocPtr doc
, xmlNodePtr node
, xmlNodePtr dst
)
2307 for (n
= node
; n
; n
= n
->parent
)
2309 if (n
->type
!= XML_ELEMENT_NODE
)
2313 for (tmp
= n
->next
; tmp
; n
= n
->next
)
2315 if (n
->type
!= XML_ELEMENT_NODE
)
2319 return GPG_ERR_ELOOP
;
2324 return GPG_ERR_ELOOP
;
2331 validate_target_attribute (struct client_s
*client
, xmlDocPtr doc
,
2332 const char *src
, xmlNodePtr dst_node
)
2335 xmlNodePtr src_node
;
2336 char **src_req
= NULL
;
2338 src_node
= resolve_path (client
, doc
, (xmlChar
*) src
, &src_req
, &rc
);
2342 client
->flags
|= FLAG_ACL_IGNORE
;
2343 /* A destination element is a child of the source element. */
2344 rc
= find_child_of_parent (doc
, src_node
->children
, dst_node
);
2348 /* The destination element is a parent of the source element. */
2349 rc
= find_parent_of_child (doc
, src_node
->parent
, dst_node
);
2353 /* A destination child element contains a target to the source element. */
2355 rc
= find_child_to_target (client
, doc
, dst_node
->children
, src_node
,
2361 strv_free (src_req
);
2362 client
->flags
&= ~(FLAG_ACL_IGNORE
| FLAG_ACL_ERROR
);
2366 /* The owner of the element is the first user listed in the _acl attribute
2367 * list. acl_check() should be called before calling this function. An empty
2368 * ACL is an error if the client is not invoking_user.
2371 is_element_owner (struct client_s
*client
, xmlNodePtr n
)
2373 xmlChar
*acl
= node_has_attribute (n
, (xmlChar
*) "_acl");
2375 gpg_error_t rc
= GPG_ERR_EACCES
;
2380 return peer_is_invoker (client
);
2383 users
= str_split((char *)acl
, ",", 0);
2384 if (users
&& *users
)
2389 if (client
->thd
->remote
)
2390 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
2392 user
= get_username (client
->thd
->peer
->uid
);
2394 user
= get_username (client
->thd
->peer
->uid
);
2398 rc
= !strcasecmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
2400 rc
= !strcmp (*users
, user
) ? 0 : GPG_ERR_EACCES
;
2403 rc
= peer_is_invoker (client
);