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