803fb7
From c7d4f7a5a6cef5a46f905141e2ac27da3c96d2b7 Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
803fb7
Date: Mon, 23 Feb 2015 23:19:54 -0500
803fb7
Subject: [PATCH] tmpfiles: avoid creating duplicate acl entries
803fb7
803fb7
https://bugs.freedesktop.org/show_bug.cgi?id=89202
803fb7
https://bugs.debian.org/778656
803fb7
803fb7
Status quo ante can be restored with:
803fb7
  getfacl -p /var/log/journal/`cat /etc/machine-id`|grep -v '^#'|sort -u|sudo setfacl --set-file=- /var/log/journal/`cat /etc/machine-id`
803fb7
803fb7
(cherry picked from commit 1c73f3bc29111a00738569c9d40a989b161a0624)
803fb7
---
803fb7
 src/shared/acl-util.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++--
803fb7
 src/shared/acl-util.h |  4 +++
803fb7
 2 files changed, 81 insertions(+), 2 deletions(-)
803fb7
803fb7
diff --git a/src/shared/acl-util.c b/src/shared/acl-util.c
803fb7
index a4ff1ab87..cbe09d7ab 100644
803fb7
--- a/src/shared/acl-util.c
803fb7
+++ b/src/shared/acl-util.c
803fb7
@@ -282,6 +282,77 @@ int parse_acl(char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask)
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
+static int acl_entry_equal(acl_entry_t a, acl_entry_t b) {
803fb7
+        acl_tag_t tag_a, tag_b;
803fb7
+
803fb7
+        if (acl_get_tag_type(a, &tag_a) < 0)
803fb7
+                return -errno;
803fb7
+
803fb7
+        if (acl_get_tag_type(b, &tag_b) < 0)
803fb7
+                return -errno;
803fb7
+
803fb7
+        if (tag_a != tag_b)
803fb7
+                return false;
803fb7
+
803fb7
+        switch (tag_a) {
803fb7
+        case ACL_USER_OBJ:
803fb7
+        case ACL_GROUP_OBJ:
803fb7
+        case ACL_MASK:
803fb7
+        case ACL_OTHER:
803fb7
+                /* can have only one of those */
803fb7
+                return true;
803fb7
+        case ACL_USER: {
803fb7
+                _cleanup_(acl_free_uid_tpp) uid_t *uid_a, *uid_b;
803fb7
+
803fb7
+                uid_a = acl_get_qualifier(a);
803fb7
+                if (!uid_a)
803fb7
+                        return -errno;
803fb7
+
803fb7
+                uid_b = acl_get_qualifier(b);
803fb7
+                if (!uid_b)
803fb7
+                        return -errno;
803fb7
+
803fb7
+                return *uid_a == *uid_b;
803fb7
+        }
803fb7
+        case ACL_GROUP: {
803fb7
+                _cleanup_(acl_free_gid_tpp) gid_t *gid_a, *gid_b;
803fb7
+
803fb7
+                gid_a = acl_get_qualifier(a);
803fb7
+                if (!gid_a)
803fb7
+                        return -errno;
803fb7
+
803fb7
+                gid_b = acl_get_qualifier(b);
803fb7
+                if (!gid_b)
803fb7
+                        return -errno;
803fb7
+
803fb7
+                return *gid_a == *gid_b;
803fb7
+        }
803fb7
+        default:
803fb7
+                assert_not_reached("Unknown acl tag type");
803fb7
+        }
803fb7
+}
803fb7
+
803fb7
+static int find_acl_entry(acl_t acl, acl_entry_t entry, acl_entry_t *out) {
803fb7
+        acl_entry_t i;
803fb7
+        int r;
803fb7
+
803fb7
+        for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
803fb7
+             r > 0;
803fb7
+             r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
803fb7
+
803fb7
+                r = acl_entry_equal(i, entry);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+                if (r > 0) {
803fb7
+                        *out = i;
803fb7
+                        return 1;
803fb7
+                }
803fb7
+        }
803fb7
+        if (r < 0)
803fb7
+                return -errno;
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl) {
803fb7
         _cleanup_(acl_freep) acl_t old;
803fb7
         acl_entry_t i;
803fb7
@@ -297,8 +368,12 @@ int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl) {
803fb7
 
803fb7
                 acl_entry_t j;
803fb7
 
803fb7
-                if (acl_create_entry(&old, &j) < 0)
803fb7
-                        return -errno;
803fb7
+                r = find_acl_entry(old, i, &j);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+                if (r == 0)
803fb7
+                        if (acl_create_entry(&old, &j) < 0)
803fb7
+                                return -errno;
803fb7
 
803fb7
                 if (acl_copy_entry(j, i) < 0)
803fb7
                         return -errno;
803fb7
diff --git a/src/shared/acl-util.h b/src/shared/acl-util.h
803fb7
index 90e88ffa2..fdb90063f 100644
803fb7
--- a/src/shared/acl-util.h
803fb7
+++ b/src/shared/acl-util.h
803fb7
@@ -41,5 +41,9 @@ int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl);
803fb7
 DEFINE_TRIVIAL_CLEANUP_FUNC(acl_t, acl_free);
803fb7
 #define acl_free_charp acl_free
803fb7
 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, acl_free_charp);
803fb7
+#define acl_free_uid_tp acl_free
803fb7
+DEFINE_TRIVIAL_CLEANUP_FUNC(uid_t*, acl_free_uid_tp);
803fb7
+#define acl_free_gid_tp acl_free
803fb7
+DEFINE_TRIVIAL_CLEANUP_FUNC(gid_t*, acl_free_gid_tp);
803fb7
 
803fb7
 #endif