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