54df86
From 80c64be80f2bffdcf5d2432e1e59d633fd68d516 Mon Sep 17 00:00:00 2001
54df86
From: Grace Chin <gchin@redhat.com>
54df86
Date: Mon, 13 Jun 2022 09:02:32 -0400
54df86
Subject: [PATCH 1/4] Add pcmk__is_user_in_group()
54df86
54df86
---
54df86
 lib/common/crmcommon_private.h |  3 +++
54df86
 lib/common/utils.c             | 33 +++++++++++++++++++++++++++++++++
54df86
 2 files changed, 36 insertions(+)
54df86
54df86
diff --git a/lib/common/crmcommon_private.h b/lib/common/crmcommon_private.h
54df86
index 6b7be9c68..c2fcb0adf 100644
54df86
--- a/lib/common/crmcommon_private.h
54df86
+++ b/lib/common/crmcommon_private.h
54df86
@@ -96,6 +96,9 @@ void pcmk__free_acls(GList *acls);
54df86
 G_GNUC_INTERNAL
54df86
 void pcmk__unpack_acl(xmlNode *source, xmlNode *target, const char *user);
54df86
 
54df86
+G_GNUC_INTERNAL
54df86
+bool pcmk__is_user_in_group(const char *user, const char *group);
54df86
+
54df86
 G_GNUC_INTERNAL
54df86
 void pcmk__apply_acl(xmlNode *xml);
54df86
 
54df86
diff --git a/lib/common/utils.c b/lib/common/utils.c
54df86
index 2dfbef278..f23583acb 100644
54df86
--- a/lib/common/utils.c
54df86
+++ b/lib/common/utils.c
54df86
@@ -27,6 +27,7 @@
54df86
 #include <time.h>
54df86
 #include <libgen.h>
54df86
 #include <signal.h>
54df86
+#include <grp.h>
54df86
 
54df86
 #include <qb/qbdefs.h>
54df86
 
54df86
@@ -53,6 +54,38 @@ gboolean crm_config_error = FALSE;
54df86
 gboolean crm_config_warning = FALSE;
54df86
 char *crm_system_name = NULL;
54df86
 
54df86
+bool
54df86
+pcmk__is_user_in_group(const char *user, const char *group)
54df86
+{
54df86
+    struct group *grent;
54df86
+    char **gr_mem;
54df86
+
54df86
+    if (user == NULL || group == NULL) {
54df86
+        return false;
54df86
+    }
54df86
+    
54df86
+    setgrent();
54df86
+    while ((grent = getgrent()) != NULL) {
54df86
+        if (grent->gr_mem == NULL) {
54df86
+            continue;
54df86
+        }
54df86
+
54df86
+        if(strcmp(group, grent->gr_name) != 0) {
54df86
+            continue;
54df86
+        }
54df86
+
54df86
+        gr_mem = grent->gr_mem;
54df86
+        while (*gr_mem != NULL) {
54df86
+            if (!strcmp(user, *gr_mem++)) {
54df86
+                endgrent();
54df86
+                return true;
54df86
+            }
54df86
+        }
54df86
+    }
54df86
+    endgrent();
54df86
+    return false;
54df86
+}
54df86
+
54df86
 int
54df86
 crm_user_lookup(const char *name, uid_t * uid, gid_t * gid)
54df86
 {
54df86
-- 
54df86
2.31.1
54df86
54df86
54df86
From 5fbe5c310de00390fb36d866823a7745ba4812e3 Mon Sep 17 00:00:00 2001
54df86
From: Grace Chin <gchin@redhat.com>
54df86
Date: Mon, 13 Jun 2022 09:04:57 -0400
54df86
Subject: [PATCH 2/4] Add unit test for pcmk__is_user_in_group()
54df86
54df86
---
54df86
 lib/common/Makefile.am                        |  2 +-
54df86
 lib/common/mock.c                             | 31 +++++--
54df86
 lib/common/mock_private.h                     | 11 +++
54df86
 lib/common/tests/acl/Makefile.am              | 11 ++-
54df86
 .../tests/acl/pcmk__is_user_in_group_test.c   | 92 +++++++++++++++++++
54df86
 5 files changed, 137 insertions(+), 10 deletions(-)
54df86
 create mode 100644 lib/common/tests/acl/pcmk__is_user_in_group_test.c
54df86
54df86
diff --git a/lib/common/Makefile.am b/lib/common/Makefile.am
54df86
index d7aae53bf..04d56dc3c 100644
54df86
--- a/lib/common/Makefile.am
54df86
+++ b/lib/common/Makefile.am
54df86
@@ -94,7 +94,7 @@ libcrmcommon_la_SOURCES	+= watchdog.c
54df86
 libcrmcommon_la_SOURCES	+= xml.c
54df86
 libcrmcommon_la_SOURCES	+= xpath.c
54df86
 
54df86
-WRAPPED = calloc getenv getpwnam_r uname
54df86
+WRAPPED = calloc getenv getpwnam_r uname setgrent getgrent endgrent
54df86
 WRAPPED_FLAGS = $(foreach fn,$(WRAPPED),-Wl,--wrap=$(fn))
54df86
 
54df86
 libcrmcommon_test_la_SOURCES	= $(libcrmcommon_la_SOURCES)
54df86
diff --git a/lib/common/mock.c b/lib/common/mock.c
54df86
index 55812ddbc..fa9431e6d 100644
54df86
--- a/lib/common/mock.c
54df86
+++ b/lib/common/mock.c
54df86
@@ -11,6 +11,7 @@
54df86
 #include <stdlib.h>
54df86
 #include <sys/types.h>
54df86
 #include <sys/utsname.h>
54df86
+#include <grp.h>
54df86
 
54df86
 #include "mock_private.h"
54df86
 
54df86
@@ -18,13 +19,13 @@
54df86
  * libcrmcommon_test.a, not into libcrmcommon.so.  It is used to support
54df86
  * constructing mock versions of library functions for unit testing.
54df86
  *
54df86
- * Each unit test will only ever want to use a mocked version of one or two
54df86
- * library functions.  However, we need to mark all the mocked functions as
54df86
- * wrapped (with -Wl,--wrap= in the LDFLAGS) in libcrmcommon_test.a so that
54df86
- * all those unit tests can share the same special test library.  The unit
54df86
- * test then defines its own wrapped function.  Because a unit test won't
54df86
- * define every single wrapped function, there will be undefined references
54df86
- * at link time.
54df86
+ * Each unit test will only ever want to use a mocked version of a few
54df86
+ * library functions (i.e. not all of them). However, we need to mark all
54df86
+ * the mocked functions as wrapped (with -Wl,--wrap= in the LDFLAGS) in
54df86
+ * libcrmcommon_test.a so that all those unit tests can share the same
54df86
+ * special test library.  The unit test then defines its own wrapped
54df86
+ * function. Because a unit test won't define every single wrapped
54df86
+ * function, there will be undefined references at link time.
54df86
  *
54df86
  * This file takes care of those undefined references.  It defines a
54df86
  * wrapped version of every function that simply calls the real libc
54df86
@@ -74,3 +75,19 @@ int __attribute__((weak))
54df86
 __wrap_uname(struct utsname *buf) {
54df86
     return __real_uname(buf);
54df86
 }
54df86
+
54df86
+void __attribute__((weak))
54df86
+__wrap_setgrent(void) {
54df86
+    __real_setgrent();
54df86
+}
54df86
+
54df86
+struct group * __attribute__((weak))
54df86
+__wrap_getgrent(void) {
54df86
+    return __real_getgrent();
54df86
+}
54df86
+
54df86
+void __attribute__((weak))
54df86
+__wrap_endgrent(void) {
54df86
+    __real_endgrent();
54df86
+}
54df86
+
54df86
diff --git a/lib/common/mock_private.h b/lib/common/mock_private.h
54df86
index 3df7c9839..0c1134cc3 100644
54df86
--- a/lib/common/mock_private.h
54df86
+++ b/lib/common/mock_private.h
54df86
@@ -14,6 +14,7 @@
54df86
 #include <stdlib.h>
54df86
 #include <sys/types.h>
54df86
 #include <sys/utsname.h>
54df86
+#include <grp.h>
54df86
 
54df86
 /* This header is for the sole use of libcrmcommon_test. */
54df86
 
54df86
@@ -31,4 +32,14 @@ int __wrap_getpwnam_r(const char *name, struct passwd *pwd,
54df86
 int __real_uname(struct utsname *buf);
54df86
 int __wrap_uname(struct utsname *buf);
54df86
 
54df86
+void __real_setgrent(void);
54df86
+void __wrap_setgrent(void);
54df86
+
54df86
+struct group *__real_getgrent(void);
54df86
+struct group *__wrap_getgrent(void);
54df86
+
54df86
+void __real_endgrent(void);
54df86
+void __wrap_endgrent(void);
54df86
+
54df86
+
54df86
 #endif  // MOCK_PRIVATE__H
54df86
diff --git a/lib/common/tests/acl/Makefile.am b/lib/common/tests/acl/Makefile.am
54df86
index 679c9cb8e..a73fc354c 100644
54df86
--- a/lib/common/tests/acl/Makefile.am
54df86
+++ b/lib/common/tests/acl/Makefile.am
54df86
@@ -1,19 +1,26 @@
54df86
 #
54df86
-# Copyright 2021 the Pacemaker project contributors
54df86
+# Copyright 2021-2022 the Pacemaker project contributors
54df86
 #
54df86
 # The version control history for this file may have further details.
54df86
 #
54df86
 # This source code is licensed under the GNU General Public License version 2
54df86
 # or later (GPLv2+) WITHOUT ANY WARRANTY.
54df86
 #
54df86
-AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
54df86
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/lib/common
54df86
 LDADD = $(top_builddir)/lib/common/libcrmcommon.la -lcmocka
54df86
 
54df86
+pcmk__is_user_in_group_test_LDADD = $(top_builddir)/lib/common/libcrmcommon_test.la -lcmocka
54df86
+pcmk__is_user_in_group_test_LDFLAGS = \
54df86
+    -Wl,--wrap=setgrent \
54df86
+    -Wl,--wrap=getgrent \
54df86
+    -Wl,--wrap=endgrent
54df86
+
54df86
 include $(top_srcdir)/mk/tap.mk
54df86
 
54df86
 # Add "_test" to the end of all test program names to simplify .gitignore.
54df86
 
54df86
 check_PROGRAMS = \
54df86
+    pcmk__is_user_in_group_test \
54df86
 	pcmk_acl_required_test \
54df86
     xml_acl_denied_test \
54df86
     xml_acl_enabled_test
54df86
diff --git a/lib/common/tests/acl/pcmk__is_user_in_group_test.c b/lib/common/tests/acl/pcmk__is_user_in_group_test.c
54df86
new file mode 100644
54df86
index 000000000..67b8c2c7c
54df86
--- /dev/null
54df86
+++ b/lib/common/tests/acl/pcmk__is_user_in_group_test.c
54df86
@@ -0,0 +1,92 @@
54df86
+/*
54df86
+ * Copyright 2020-2022 the Pacemaker project contributors
54df86
+ *
54df86
+ * The version control history for this file may have further details.
54df86
+ *
54df86
+ * This source code is licensed under the GNU Lesser General Public License
54df86
+ * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
54df86
+ */
54df86
+
54df86
+#include <crm_internal.h>
54df86
+#include <crm/common/acl.h>
54df86
+#include "../../crmcommon_private.h"
54df86
+
54df86
+#include "mock_private.h"
54df86
+
54df86
+#include <stdarg.h>
54df86
+#include <stddef.h>
54df86
+#include <stdint.h>
54df86
+#include <setjmp.h>
54df86
+#include <cmocka.h>
54df86
+
54df86
+// THe index of the group that is going to be returned next from "get group entry" (getgrent)
54df86
+static int group_idx = 0;
54df86
+
54df86
+// Data used for testing
54df86
+static const char* grp0_members[] = {
54df86
+    "user0", "user1", NULL
54df86
+};
54df86
+
54df86
+static const char* grp1_members[] = {
54df86
+    "user1", NULL
54df86
+};
54df86
+
54df86
+static const char* grp2_members[] = {
54df86
+    "user2", "user1", NULL
54df86
+};
54df86
+
54df86
+// an array of "groups" (a struct from grp.h), the members of the groups are initalized here to some testing data.
54df86
+// Casting away the consts to make the compiler happy and simplify initialization. 
54df86
+// We never actually change these variables during the test!
54df86
+// string literal = const char* (cannot be changed b/c ? ) vs. char* (its getting casted to this)
54df86
+static const int NUM_GROUPS = 3;
54df86
+static struct group groups[] = {
54df86
+    {(char*)"grp0", (char*)"", 0, (char**)grp0_members},
54df86
+    {(char*)"grp1", (char*)"", 1, (char**)grp1_members},
54df86
+    {(char*)"grp2", (char*)"", 2, (char**)grp2_members},
54df86
+};
54df86
+
54df86
+// This function resets the group_idx to 0.
54df86
+void
54df86
+__wrap_setgrent(void) {
54df86
+    group_idx = 0;
54df86
+}
54df86
+
54df86
+// This function returns the next group entry in the list of groups, or
54df86
+// NULL if there aren't any left.
54df86
+// group_idx is a global variable which keeps track of where you are in the list
54df86
+struct group *
54df86
+__wrap_getgrent(void) {
54df86
+    if(group_idx >= NUM_GROUPS) return NULL;
54df86
+    return &groups[group_idx++];
54df86
+}
54df86
+
54df86
+void
54df86
+__wrap_endgrent(void) {
54df86
+}
54df86
+
54df86
+static void
54df86
+is_pcmk__is_user_in_group(void **state)
54df86
+{
54df86
+    // null user
54df86
+    assert_false(pcmk__is_user_in_group(NULL, "grp0"));
54df86
+    // null group
54df86
+    assert_false(pcmk__is_user_in_group("user0", NULL));
54df86
+    // nonexistent group
54df86
+    assert_false(pcmk__is_user_in_group("user0", "nonexistent_group"));
54df86
+    // user is in group
54df86
+    assert_true(pcmk__is_user_in_group("user0", "grp0"));
54df86
+    // user is not in group
54df86
+    assert_false(pcmk__is_user_in_group("user2", "grp0"));
54df86
+}
54df86
+
54df86
+int
54df86
+main(int argc, char **argv)
54df86
+{
54df86
+    const struct CMUnitTest tests[] = {
54df86
+        cmocka_unit_test(is_pcmk__is_user_in_group)
54df86
+    };
54df86
+
54df86
+    cmocka_set_message_output(CM_OUTPUT_TAP);
54df86
+    return cmocka_run_group_tests(tests, NULL, NULL);
54df86
+}
54df86
-- 
54df86
2.31.1
54df86
54df86
54df86
From 1bb7fda60f5b8547d7457f20543b7e50089cf06b Mon Sep 17 00:00:00 2001
54df86
From: Grace Chin <gchin@redhat.com>
54df86
Date: Mon, 13 Jun 2022 09:17:36 -0400
54df86
Subject: [PATCH 3/4] Add ACL group support
54df86
54df86
closes T61
54df86
---
54df86
 lib/common/acl.c | 7 +++++++
54df86
 1 file changed, 7 insertions(+)
54df86
54df86
diff --git a/lib/common/acl.c b/lib/common/acl.c
54df86
index f68069bbd..d7f8469b1 100644
54df86
--- a/lib/common/acl.c
54df86
+++ b/lib/common/acl.c
54df86
@@ -320,6 +320,13 @@ pcmk__unpack_acl(xmlNode *source, xmlNode *target, const char *user)
54df86
                         crm_debug("Unpacking ACLs for user '%s'", id);
54df86
                         p->acls = parse_acl_entry(acls, child, p->acls);
54df86
                     }
54df86
+                } else if (!strcmp(tag, XML_ACL_TAG_GROUP)) {
54df86
+                    const char *id = crm_element_value(child, XML_ATTR_ID);
54df86
+
54df86
+                    if (id && pcmk__is_user_in_group(user,id)) {
54df86
+                        crm_debug("Unpacking ACLs for group '%s'", id);
54df86
+                        p->acls = parse_acl_entry(acls, child, p->acls);
54df86
+                    }
54df86
                 }
54df86
             }
54df86
         }
54df86
-- 
54df86
2.31.1
54df86
54df86
54df86
From f4efd55d9424d34908ba3e2bcffe16c00b2cf660 Mon Sep 17 00:00:00 2001
54df86
From: Grace Chin <gchin@redhat.com>
54df86
Date: Mon, 13 Jun 2022 09:20:36 -0400
54df86
Subject: [PATCH 4/4] Allow acl_target and acl_group elements to take a 'name'
54df86
 attribute to use a name different from 'id'
54df86
54df86
closes T60
54df86
---
54df86
 include/crm/msg_xml.h |  1 +
54df86
 lib/common/acl.c      | 21 +++++++++++++++++----
54df86
 2 files changed, 18 insertions(+), 4 deletions(-)
54df86
54df86
diff --git a/include/crm/msg_xml.h b/include/crm/msg_xml.h
54df86
index b36dcf060..6470520b1 100644
54df86
--- a/include/crm/msg_xml.h
54df86
+++ b/include/crm/msg_xml.h
54df86
@@ -133,6 +133,7 @@ extern "C" {
54df86
 #  define XML_ATTR_VERSION		"version"
54df86
 #  define XML_ATTR_DESC			"description"
54df86
 #  define XML_ATTR_ID			"id"
54df86
+#  define XML_ATTR_NAME			"name"
54df86
 #  define XML_ATTR_IDREF			"id-ref"
54df86
 #  define XML_ATTR_ID_LONG		"long-id"
54df86
 #  define XML_ATTR_TYPE			"type"
54df86
diff --git a/lib/common/acl.c b/lib/common/acl.c
54df86
index d7f8469b1..b9f7472ee 100644
54df86
--- a/lib/common/acl.c
54df86
+++ b/lib/common/acl.c
54df86
@@ -278,8 +278,13 @@ pcmk__apply_acl(xmlNode *xml)
54df86
 
54df86
 /*!
54df86
  * \internal
54df86
- * \brief Unpack ACLs for a given user
54df86
- *
54df86
+ * \brief Unpack ACLs for a given user into the
54df86
+ * metadata of the target XML tree
54df86
+ * 
54df86
+ * Taking the description of ACLs from the source XML tree and 
54df86
+ * marking up the target XML tree with access information for the
54df86
+ * given user by tacking it onto the relevant nodes
54df86
+ * 
54df86
  * \param[in]     source  XML with ACL definitions
54df86
  * \param[in,out] target  XML that ACLs will be applied to
54df86
  * \param[in]     user    Username whose ACLs need to be unpacked
54df86
@@ -314,14 +319,22 @@ pcmk__unpack_acl(xmlNode *source, xmlNode *target, const char *user)
54df86
 
54df86
                 if (!strcmp(tag, XML_ACL_TAG_USER)
54df86
                     || !strcmp(tag, XML_ACL_TAG_USERv1)) {
54df86
-                    const char *id = crm_element_value(child, XML_ATTR_ID);
54df86
+                    const char *id = crm_element_value(child, XML_ATTR_NAME);
54df86
+
54df86
+                    if (id == NULL) {
54df86
+                        id = crm_element_value(child, XML_ATTR_ID);
54df86
+                    }
54df86
 
54df86
                     if (id && strcmp(id, user) == 0) {
54df86
                         crm_debug("Unpacking ACLs for user '%s'", id);
54df86
                         p->acls = parse_acl_entry(acls, child, p->acls);
54df86
                     }
54df86
                 } else if (!strcmp(tag, XML_ACL_TAG_GROUP)) {
54df86
-                    const char *id = crm_element_value(child, XML_ATTR_ID);
54df86
+                    const char *id = crm_element_value(child, XML_ATTR_NAME);
54df86
+
54df86
+                    if (id == NULL) {
54df86
+                        id = crm_element_value(child, XML_ATTR_ID);
54df86
+                    }
54df86
 
54df86
                     if (id && pcmk__is_user_in_group(user,id)) {
54df86
                         crm_debug("Unpacking ACLs for group '%s'", id);
54df86
-- 
54df86
2.31.1
54df86