dcavalca / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
923a60
From e9bef2f8146ccf152459248775eec8e8ce123865 Mon Sep 17 00:00:00 2001
923a60
From: Lennart Poettering <lennart@poettering.net>
923a60
Date: Wed, 22 Apr 2015 22:54:23 +0200
923a60
Subject: [PATCH] journalctl: rework code that checks whether we have access to
923a60
 /var/log/journal
923a60
923a60
- fix some memory leaks on error conditions
923a60
923a60
- handle all error cases properly, and log about failures
923a60
923a60
- move HAVE_ACL and no-HAVE_ACL code closer to each other
923a60
923a60
Cherry-picked from: e346512c684e9efae84c6442f7e6a5781564ecde
923a60
Related: #1318994
923a60
---
923a60
 src/journal/journalctl.c | 120 ++++++++++++++++++++-------------------
923a60
 src/shared/acl-util.c    | 102 ++++++++++++++++++---------------
923a60
 src/shared/acl-util.h    |   2 +-
923a60
 3 files changed, 119 insertions(+), 105 deletions(-)
923a60
923a60
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
923a60
index 6ba8847798..f60e6415f8 100644
923a60
--- a/src/journal/journalctl.c
923a60
+++ b/src/journal/journalctl.c
923a60
@@ -1680,61 +1680,76 @@ static int verify(sd_journal *j) {
923a60
         return r;
923a60
 }
923a60
 
923a60
-#ifdef HAVE_ACL
923a60
 static int access_check_var_log_journal(sd_journal *j) {
923a60
+#ifdef HAVE_ACL
923a60
         _cleanup_strv_free_ char **g = NULL;
923a60
-        bool have_access;
923a60
+        const char* dir;
923a60
+#endif
923a60
         int r;
923a60
 
923a60
         assert(j);
923a60
 
923a60
-        have_access = in_group("systemd-journal") > 0;
923a60
+        if (arg_quiet)
923a60
+                return 0;
923a60
 
923a60
-        if (!have_access) {
923a60
-                const char* dir;
923a60
+        /* If we are root, we should have access, don't warn. */
923a60
+        if (getuid() == 0)
923a60
+                return 0;
923a60
 
923a60
-                if (access("/run/log/journal", F_OK) >= 0)
923a60
-                        dir = "/run/log/journal";
923a60
-                else
923a60
-                        dir = "/var/log/journal";
923a60
+        /* If we are in the 'systemd-journal' group, we should have
923a60
+         * access too. */
923a60
+        r = in_group("systemd-journal");
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to check if we are in the 'systemd-journal' group: %m");
923a60
+        if (r > 0)
923a60
+                return 0;
923a60
 
923a60
-                /* Let's enumerate all groups from the default ACL of
923a60
-                 * the directory, which generally should allow access
923a60
-                 * to most journal files too */
923a60
-                r = search_acl_groups(&g, dir, &have_access);
923a60
-                if (r < 0)
923a60
-                        return r;
923a60
-        }
923a60
+#ifdef HAVE_ACL
923a60
+        if (laccess("/run/log/journal", F_OK) >= 0)
923a60
+                dir = "/run/log/journal";
923a60
+        else
923a60
+                dir = "/var/log/journal";
923a60
 
923a60
-        if (!have_access) {
923a60
+        /* If we are in any of the groups listed in the journal ACLs,
923a60
+         * then all is good, too. Let's enumerate all groups from the
923a60
+         * default ACL of the directory, which generally should allow
923a60
+         * access to most journal files too. */
923a60
+        r = acl_search_groups(dir, &g);
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to search journal ACL: %m");
923a60
+        if (r > 0)
923a60
+                return 0;
923a60
 
923a60
-                if (strv_isempty(g))
923a60
-                        log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
923a60
-                                   "      Users in the 'systemd-journal' group can see all messages. Pass -q to\n"
923a60
-                                   "      turn off this notice.");
923a60
-                else {
923a60
-                        _cleanup_free_ char *s = NULL;
923a60
+        /* Print a pretty list, if there were ACLs set. */
923a60
+        if (!strv_isempty(g)) {
923a60
+                _cleanup_free_ char *s = NULL;
923a60
 
923a60
-                        r = strv_extend(&g, "systemd-journal");
923a60
-                        if (r < 0)
923a60
-                                return log_oom();
923a60
+                /* Thre are groups in the ACL, let's list them */
923a60
+                r = strv_extend(&g, "systemd-journal");
923a60
+                if (r < 0)
923a60
+                        return log_oom();
923a60
 
923a60
-                        strv_sort(g);
923a60
-                        strv_uniq(g);
923a60
+                strv_sort(g);
923a60
+                strv_uniq(g);
923a60
 
923a60
-                        s = strv_join(g, "', '");
923a60
-                        if (!s)
923a60
-                                return log_oom();
923a60
+                s = strv_join(g, "', '");
923a60
+                if (!s)
923a60
+                        return log_oom();
923a60
 
923a60
-                        log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
923a60
-                                   "      Users in groups '%s' can see all messages.\n"
923a60
-                                   "      Pass -q to turn off this notice.", s);
923a60
-                }
923a60
+                log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
923a60
+                           "      Users in groups '%s' can see all messages.\n"
923a60
+                           "      Pass -q to turn off this notice.", s);
923a60
+                return 1;
923a60
         }
923a60
+#endif
923a60
 
923a60
-        return 0;
923a60
+        /* If no ACLs were found, print a short version of the message. */
923a60
+        log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
923a60
+                   "      Users in the 'systemd-journal' group can see all messages. Pass -q to\n"
923a60
+                   "      turn off this notice.");
923a60
+
923a60
+        return 1;
923a60
 }
923a60
-#endif
923a60
 
923a60
 static int access_check(sd_journal *j) {
923a60
         Iterator it;
923a60
@@ -1746,30 +1761,15 @@ static int access_check(sd_journal *j) {
923a60
         if (set_isempty(j->errors)) {
923a60
                 if (ordered_hashmap_isempty(j->files))
923a60
                         log_notice("No journal files were found.");
923a60
+
923a60
                 return 0;
923a60
         }
923a60
 
923a60
         if (set_contains(j->errors, INT_TO_PTR(-EACCES))) {
923a60
-#ifdef HAVE_ACL
923a60
-                /* If /run/log/journal or /var/log/journal exist, try
923a60
-                   to pring a nice notice if the user lacks access to it. */
923a60
-                if (!arg_quiet && geteuid() != 0) {
923a60
-                        r = access_check_var_log_journal(j);
923a60
-                        if (r < 0)
923a60
-                                return r;
923a60
-                }
923a60
-#else
923a60
-                if (geteuid() != 0 && in_group("systemd-journal") <= 0) {
923a60
-                        log_error("Unprivileged users cannot access messages. Users in the 'systemd-journal' group\n"
923a60
-                                  "group may access messages.");
923a60
-                        return -EACCES;
923a60
-                }
923a60
-#endif
923a60
+                (void) access_check_var_log_journal(j);
923a60
 
923a60
-                if (ordered_hashmap_isempty(j->files)) {
923a60
-                        log_error("No journal files were opened due to insufficient permissions.");
923a60
-                        r = -EACCES;
923a60
-                }
923a60
+                if (ordered_hashmap_isempty(j->files))
923a60
+                        r = log_error_errno(EACCES, "No journal files were opened due to insufficient permissions.");
923a60
         }
923a60
 
923a60
         SET_FOREACH(code, j->errors, it) {
923a60
@@ -1778,8 +1778,12 @@ static int access_check(sd_journal *j) {
923a60
                 err = -PTR_TO_INT(code);
923a60
                 assert(err > 0);
923a60
 
923a60
-                if (err != EACCES)
923a60
-                        log_warning_errno(err, "Error was encountered while opening journal files: %m");
923a60
+                if (err == EACCES)
923a60
+                        continue;
923a60
+
923a60
+                log_warning_errno(err, "Error was encountered while opening journal files: %m");
923a60
+                if (r == 0)
923a60
+                        r = -err;
923a60
         }
923a60
 
923a60
         return r;
923a60
diff --git a/src/shared/acl-util.c b/src/shared/acl-util.c
923a60
index e67e9acb6a..d18a02f503 100644
923a60
--- a/src/shared/acl-util.c
923a60
+++ b/src/shared/acl-util.c
923a60
@@ -82,17 +82,18 @@ int calc_acl_mask_if_needed(acl_t *acl_p) {
923a60
 
923a60
                 if (tag == ACL_MASK)
923a60
                         return 0;
923a60
-                if (IN_SET(tag, ACL_USER, ACL_GROUP))
923a60
-                        goto calc;
923a60
+
923a60
+                if (IN_SET(tag, ACL_USER, ACL_GROUP)) {
923a60
+                        if (acl_calc_mask(acl_p) < 0)
923a60
+                                return -errno;
923a60
+
923a60
+                        return 1;
923a60
+                }
923a60
         }
923a60
         if (r < 0)
923a60
                 return -errno;
923a60
-        return 0;
923a60
 
923a60
-calc:
923a60
-        if (acl_calc_mask(acl_p) < 0)
923a60
-                return -errno;
923a60
-        return 1;
923a60
+        return 0;
923a60
 }
923a60
 
923a60
 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
923a60
@@ -159,59 +160,68 @@ int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
923a60
         return 0;
923a60
 }
923a60
 
923a60
-int search_acl_groups(char*** dst, const char* path, bool* belong) {
923a60
-        acl_t acl;
923a60
+int acl_search_groups(const char *path, char ***ret_groups) {
923a60
+        _cleanup_strv_free_ char **g = NULL;
923a60
+        _cleanup_(acl_free) acl_t acl = NULL;
923a60
+        bool ret = false;
923a60
+        acl_entry_t entry;
923a60
+        int r;
923a60
 
923a60
         assert(path);
923a60
-        assert(belong);
923a60
 
923a60
         acl = acl_get_file(path, ACL_TYPE_DEFAULT);
923a60
-        if (acl) {
923a60
-                acl_entry_t entry;
923a60
-                int r;
923a60
-
923a60
-                r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
923a60
-                while (r > 0) {
923a60
-                        acl_tag_t tag;
923a60
-                        gid_t *gid;
923a60
-                        char *name;
923a60
+        if (!acl)
923a60
+                return -errno;
923a60
 
923a60
-                        r = acl_get_tag_type(entry, &tag;;
923a60
-                        if (r < 0)
923a60
-                                break;
923a60
+        r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
923a60
+        for (;;) {
923a60
+                _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
923a60
+                acl_tag_t tag;
923a60
+
923a60
+                if (r < 0)
923a60
+                        return -errno;
923a60
+                if (r == 0)
923a60
+                        break;
923a60
+
923a60
+                if (acl_get_tag_type(entry, &tag) < 0)
923a60
+                        return -errno;
923a60
 
923a60
-                        if (tag != ACL_GROUP)
923a60
-                                goto next;
923a60
+                if (tag != ACL_GROUP)
923a60
+                        goto next;
923a60
 
923a60
-                        gid = acl_get_qualifier(entry);
923a60
-                        if (!gid)
923a60
-                                break;
923a60
+                gid = acl_get_qualifier(entry);
923a60
+                if (!gid)
923a60
+                        return -errno;
923a60
+
923a60
+                if (in_gid(*gid) > 0) {
923a60
+                        if (!ret_groups)
923a60
+                                return true;
923a60
 
923a60
-                        if (in_gid(*gid) > 0) {
923a60
-                                *belong = true;
923a60
-                                break;
923a60
-                        }
923a60
+                        ret = true;
923a60
+                }
923a60
+
923a60
+                if (ret_groups) {
923a60
+                        char *name;
923a60
 
923a60
                         name = gid_to_name(*gid);
923a60
-                        if (!name) {
923a60
-                                acl_free(acl);
923a60
-                                return log_oom();
923a60
-                        }
923a60
-
923a60
-                        r = strv_consume(dst, name);
923a60
-                        if (r < 0) {
923a60
-                                acl_free(acl);
923a60
-                                return log_oom();
923a60
-                        }
923a60
-
923a60
-                next:
923a60
-                        r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
923a60
+                        if (!name)
923a60
+                                return -ENOMEM;
923a60
+
923a60
+                        r = strv_consume(&g, name);
923a60
+                        if (r < 0)
923a60
+                                return r;
923a60
                 }
923a60
 
923a60
-                acl_free(acl);
923a60
+        next:
923a60
+                r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
923a60
         }
923a60
 
923a60
-        return 0;
923a60
+        if (ret_groups) {
923a60
+                *ret_groups = g;
923a60
+                g = NULL;
923a60
+        }
923a60
+
923a60
+        return ret;
923a60
 }
923a60
 
923a60
 int parse_acl(char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
923a60
diff --git a/src/shared/acl-util.h b/src/shared/acl-util.h
923a60
index fdb90063fa..c8bcc266d0 100644
923a60
--- a/src/shared/acl-util.h
923a60
+++ b/src/shared/acl-util.h
923a60
@@ -32,7 +32,7 @@
923a60
 int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry);
923a60
 int calc_acl_mask_if_needed(acl_t *acl_p);
923a60
 int add_base_acls_if_needed(acl_t *acl_p, const char *path);
923a60
-int search_acl_groups(char*** dst, const char* path, bool* belong);
923a60
+int acl_search_groups(const char* path, char ***ret_groups);
923a60
 int parse_acl(char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask);
923a60
 int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl);
923a60