13 #include <sys/types.h>
19 #include <libxml/tree.h>
27 #define MAX_XPATH_LEN 4096
29 typedef struct xml_acl_s {
48 g_list_free_full(acls, free_acl);
70 if ((tag == NULL) && (ref == NULL) && (xpath == NULL)) {
72 crm_trace(
"Ignoring ACL <%s> element without selection criteria",
73 crm_element_name(xml));
82 acl->xpath = strdup(xpath);
84 crm_trace(
"Unpacked ACL <%s> element using xpath: %s",
85 crm_element_name(xml), acl->xpath);
126 acl->xpath = strdup(buffer);
129 crm_trace(
"Unpacked ACL <%s> element as xpath: %s",
130 crm_element_name(xml), acl->xpath);
133 return g_list_append(acls, acl);
147 parse_acl_entry(xmlNode *acl_top, xmlNode *acl_entry, GList *acls)
149 xmlNode *child = NULL;
151 for (child = pcmk__xe_first_child(acl_entry); child;
152 child = pcmk__xe_next(child)) {
153 const char *tag = crm_element_name(child);
158 crm_trace(
"Unpacking ACL <%s> element of kind '%s'", tag, kind);
161 crm_trace(
"Unpacking ACL <%s> element", tag);
169 xmlNode *role = NULL;
171 for (role = pcmk__xe_first_child(acl_top); role;
172 role = pcmk__xe_next(role)) {
177 if (role_id && strcmp(ref_role, role_id) == 0) {
178 crm_trace(
"Unpacking referenced role '%s' in ACL <%s> element",
179 role_id, crm_element_name(acl_entry));
180 acls = parse_acl_entry(acl_top, role, acls);
197 crm_warn(
"Ignoring unknown ACL %s '%s'",
198 (kind?
"kind" :
"element"), tag);
247 xmlXPathObjectPtr xpathObj = NULL;
250 crm_trace(
"Skipping ACLs for user '%s' because not enabled for this XML",
255 for (aIter = p->
acls; aIter != NULL; aIter = aIter->next) {
256 int max = 0, lpc = 0;
260 max = numXpathResults(xpathObj);
262 for (lpc = 0; lpc < max; lpc++) {
267 crm_trace(
"Applying %s ACL to %s matched by %s",
268 acl_to_text(acl->mode), path, acl->xpath);
272 crm_trace(
"Applied %s ACL %s (%d match%s)",
273 acl_to_text(acl->mode), acl->xpath, max,
274 ((max == 1)?
"" :
"es"));
292 if ((target == NULL) || (target->doc == NULL)
293 || (target->doc->_private == NULL)) {
297 p = target->doc->_private;
299 crm_trace(
"Not unpacking ACLs because not required for user '%s'",
302 }
else if (p->
acls == NULL) {
307 p->
user = strdup(user);
310 xmlNode *child = NULL;
312 for (child = pcmk__xe_first_child(acls); child;
313 child = pcmk__xe_next(child)) {
314 const char *tag = crm_element_name(child);
320 if (
id && strcmp(
id, user) == 0) {
321 crm_debug(
"Unpacking ACLs for user '%s'",
id);
322 p->
acls = parse_acl_entry(acls, child, p->
acls);
336 }
else if (pcmk_all_flags_set(allowed, requested)) {
351 purge_xml_attributes(xmlNode *xml)
353 xmlNode *child = NULL;
354 xmlAttr *xIter = NULL;
355 bool readable_children =
false;
359 crm_trace(
"%s[@id=%s] is readable", crm_element_name(xml),
ID(xml));
363 xIter = xml->properties;
364 while (xIter != NULL) {
365 xmlAttr *tmp = xIter;
366 const char *prop_name = (
const char *)xIter->name;
373 xmlUnsetProp(xml, tmp->name);
376 child = pcmk__xml_first_child(xml);
377 while ( child != NULL ) {
378 xmlNode *tmp = child;
380 child = pcmk__xml_next(child);
381 readable_children |= purge_xml_attributes(tmp);
384 if (!readable_children) {
387 return readable_children;
412 crm_trace(
"Not filtering XML because ACLs not required for user '%s'",
417 crm_trace(
"Filtering XML copy using user '%s' ACLs", user);
419 if (target == NULL) {
427 doc = target->doc->_private;
428 for(aIter = doc->
acls; aIter != NULL && target; aIter = aIter->next) {
435 }
else if (acl->xpath) {
437 xmlXPathObjectPtr xpathObj =
xpath_search(target, acl->xpath);
439 max = numXpathResults(xpathObj);
440 for(lpc = 0; lpc < max; lpc++) {
443 if (!purge_xml_attributes(match) && (match == target)) {
444 crm_trace(
"ACLs deny user '%s' access to entire XML document",
450 crm_trace(
"ACLs deny user '%s' access to %s (%d %s)",
451 user, acl->xpath, max,
457 if (!purge_xml_attributes(target)) {
458 crm_trace(
"ACLs deny user '%s' access to entire XML document", user);
463 g_list_free_full(doc->
acls, free_acl);
467 crm_trace(
"User '%s' without ACLs denied access to entire XML document",
493 implicitly_allowed(xmlNode *xml)
497 for (xmlAttr *prop = xml->properties; prop != NULL; prop = prop->next) {
498 if (strcmp((
const char *) prop->name,
XML_ATTR_ID) != 0) {
513 #define display_id(xml) (ID(xml)? ID(xml) : "<unset>")
534 if (implicitly_allowed(xml)) {
535 crm_trace(
"Creation of <%s> scaffolding with id=\"%s\""
536 " is implicitly allowed",
540 crm_trace(
"ACLs allow creation of <%s> with id=\"%s\"",
543 }
else if (check_top) {
544 crm_trace(
"ACLs disallow creation of <%s> with id=\"%s\"",
550 crm_notice(
"ACLs would disallow creation of %s<%s> with id=\"%s\" ",
551 ((xml == xmlDocGetRootElement(xml->doc))?
"root element " :
""),
556 for (xmlNode *cIter = pcmk__xml_first_child(xml); cIter != NULL; ) {
557 xmlNode *child = cIter;
558 cIter = pcmk__xml_next(cIter);
566 if (xml && xml->doc && xml->doc->_private){
590 if (xml && xml->doc && xml->doc->_private){
607 xmlNode *parent = xml;
619 if (docp->
acls == NULL) {
620 crm_trace(
"User '%s' without ACLs denied %s access to %s",
621 docp->
user, acl_to_text(mode), buffer);
632 xmlAttr *attr = xmlHasProp(xml, (
pcmkXmlStr) name);
639 while (parent && parent->_private) {
641 if (test_acl_mode(p->
flags, mode)) {
645 crm_trace(
"%sACL denies user '%s' %s access to %s",
646 (parent != xml) ?
"Parent " :
"", docp->
user,
647 acl_to_text(mode), buffer);
651 parent = parent->parent;
654 crm_trace(
"Default ACL denies user '%s' %s access to %s",
655 docp->
user, acl_to_text(mode), buffer);
673 if (pcmk__str_empty(user)) {
674 crm_trace(
"ACLs not required because no user set");
678 crm_trace(
"ACLs not required for privileged user %s", user);
688 struct passwd *pwent = getpwuid(uid);
691 crm_perror(LOG_INFO,
"Cannot get user details for user ID %d", uid);
694 return strdup(pwent->pw_name);
717 const char *peer_user)
719 static const char *effective_user = NULL;
720 const char *requested_user = NULL;
721 const char *user = NULL;
723 if (effective_user == NULL) {
725 if (effective_user == NULL) {
726 effective_user = strdup(
"#unprivileged");
727 CRM_CHECK(effective_user != NULL,
return NULL);
728 crm_err(
"Unable to determine effective user, assuming unprivileged for ACLs");
733 if (requested_user == NULL) {
742 if (!pcmk__is_privileged(effective_user)) {
746 user = effective_user;
748 }
else if (peer_user == NULL && requested_user == NULL) {
752 user = effective_user;
754 }
else if (peer_user == NULL) {
756 user = requested_user;
758 }
else if (!pcmk__is_privileged(peer_user)) {
764 }
else if (requested_user == NULL) {
770 user = requested_user;
782 return requested_user;
#define CRM_CHECK(expr, failure_action)
#define crm_notice(fmt, args...)
void pcmk__free_acls(GList *acls)
G_GNUC_INTERNAL int pcmk__element_xpath(const char *prefix, xmlNode *xml, char *buffer, int offset, size_t buffer_size)
bool xml_acl_filtered_copy(const char *user, xmlNode *acl_source, xmlNode *xml, xmlNode **result)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
#define XML_ACL_TAG_WRITE
void pcmk__apply_creation_acl(xmlNode *xml, bool check_top)
#define CRM_LOG_ASSERT(expr)
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
G_GNUC_INTERNAL void pcmk__set_xml_doc_flag(xmlNode *xml, enum xml_private_flags flag)
xmlNode * copy_xml(xmlNode *src_node)
#define XML_ACL_ATTR_REFv1
#define XML_ACL_ATTR_KIND
bool pcmk__check_acl(xmlNode *xml, const char *name, enum xml_private_flags mode)
#define crm_warn(fmt, args...)
void pcmk_free_xml_subtree(xmlNode *xml)
#define crm_debug(fmt, args...)
bool xml_acl_enabled(xmlNode *xml)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
#define XML_ACL_ATTR_XPATH
#define XML_ACL_TAG_PERMISSION
#define crm_trace(fmt, args...)
const char * pcmk__update_acl_user(xmlNode *request, const char *field, const char *peer_user)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
#define XML_ACL_TAG_USERv1
Wrappers for and extensions to libxml2.
#define XML_ACL_ATTR_ATTRIBUTE
void pcmk__unpack_acl(xmlNode *source, xmlNode *target, const char *user)
void free_xml(xmlNode *child)
#define XML_ACL_ATTR_TAGv1
const xmlChar * pcmkXmlStr
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
#define pcmk__plural_alt(i, s1, s2)
#define XML_ACL_TAG_ROLE_REFv1
void pcmk__apply_acl(xmlNode *xml)
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
bool xml_acl_denied(xmlNode *xml)
void xml_acl_disable(xmlNode *xml)
char * xml_get_path(xmlNode *xml)
#define XML_ACL_TAG_ROLE_REF
G_GNUC_INTERNAL bool pcmk__tracking_xml_changes(xmlNode *xml, bool lazy)
#define pcmk__set_xml_flags(xml_priv, flags_to_set)
char * pcmk__uid2username(uid_t uid)
#define pcmk__clear_xml_flags(xml_priv, flags_to_clear)
void freeXpathObject(xmlXPathObjectPtr xpathObj)
struct xml_acl_s xml_acl_t
bool pcmk_acl_required(const char *user)
Check whether ACLs are required for a given user.