594167
From d49d646d00078b201cdde2978b7941d20acb1d4b Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Wed, 2 Mar 2022 16:53:54 +0100
594167
Subject: [PATCH] basic/unit-file: split out the subroutine for symlink
594167
 verification
594167
594167
The old logs used __func__, but this doesn't make sense now, because the
594167
low-level function will be used in other places. So those are adjusted to be
594167
more generic.
594167
594167
(cherry picked from commit 9825181143530af7003fc50567b814dbbee39046)
594167
594167
Related: #2082131
594167
---
594167
 src/basic/unit-file.c | 159 +++++++++++++++++++++++++-----------------
594167
 1 file changed, 96 insertions(+), 63 deletions(-)
594167
594167
diff --git a/src/basic/unit-file.c b/src/basic/unit-file.c
594167
index 96826e2940..25abce932a 100644
594167
--- a/src/basic/unit-file.c
594167
+++ b/src/basic/unit-file.c
594167
@@ -260,6 +260,83 @@ static int directory_name_is_valid(const char *name) {
594167
         return false;
594167
 }
594167
 
594167
+static int unit_file_resolve_symlink(
594167
+                const char *root_dir,
594167
+                char **search_path,
594167
+                const char *dir,
594167
+                int dirfd,
594167
+                const char *filename,
594167
+                char **ret_destination) {
594167
+
594167
+        _cleanup_free_ char *target = NULL, *simplified = NULL, *dst = NULL;
594167
+        int r;
594167
+
594167
+        assert(dir);
594167
+        assert(dirfd >= 0);
594167
+        assert(filename);
594167
+        assert(ret_destination);
594167
+
594167
+        r = readlinkat_malloc(dirfd, filename, &target);
594167
+        if (r < 0)
594167
+                return log_warning_errno(r, "Failed to read symlink %s%s%s: %m",
594167
+                                         dir, dir ? "/" : "", filename);
594167
+
594167
+        bool is_abs = path_is_absolute(target);
594167
+        if (root_dir || !is_abs) {
594167
+                char *target_abs = path_join(is_abs ? root_dir : dir, target);
594167
+                if (!target_abs)
594167
+                        return log_oom();
594167
+
594167
+                free_and_replace(target, target_abs);
594167
+        }
594167
+
594167
+        /* Get rid of "." and ".." components in target path */
594167
+        r = chase_symlinks(target, root_dir, CHASE_NOFOLLOW | CHASE_NONEXISTENT, &simplified, NULL);
594167
+        if (r < 0)
594167
+                return log_warning_errno(r, "Failed to resolve symlink %s/%s pointing to %s: %m",
594167
+                                         dir, filename, target);
594167
+
594167
+        /* Check if the symlink goes outside of our search path.
594167
+         * If yes, it's a linked unit file or mask, and we don't care about the target name.
594167
+         * Let's just store the link source directly.
594167
+         * If not, let's verify that it's a good symlink. */
594167
+        const char *tail = path_startswith_strv(simplified, search_path);
594167
+        if (!tail) {
594167
+                log_debug("Linked unit file: %s/%s → %s", dir, filename, simplified);
594167
+
594167
+                dst = path_join(dir, filename);
594167
+                if (!dst)
594167
+                        return log_oom();
594167
+
594167
+        } else {
594167
+                r = path_extract_filename(simplified, &dst);
594167
+                if (r < 0)
594167
+                        return r;
594167
+
594167
+                bool self_alias = streq(dst, filename);
594167
+
594167
+                if (is_path(tail))
594167
+                        log_full(self_alias ? LOG_DEBUG : LOG_WARNING,
594167
+                                 "Suspicious symlink %s/%s→%s, treating as alias.",
594167
+                                 dir, filename, simplified);
594167
+
594167
+                r = unit_validate_alias_symlink_and_warn(filename, simplified);
594167
+                if (r < 0)
594167
+                        return r;
594167
+
594167
+                if (self_alias)
594167
+                        /* A self-alias that has no effect */
594167
+                        return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
594167
+                                               "Unit file self-alias: %s/%s → %s, ignoring.",
594167
+                                               dir, filename, dst);
594167
+
594167
+                log_debug("Unit file alias: %s/%s → %s", dir, filename, dst);
594167
+        }
594167
+
594167
+        *ret_destination = TAKE_PTR(dst);
594167
+        return 0;
594167
+}
594167
+
594167
 int unit_file_build_name_map(
594167
                 const LookupPaths *lp,
594167
                 uint64_t *cache_timestamp_hash,
594167
@@ -310,10 +387,9 @@ int unit_file_build_name_map(
594167
 
594167
                 FOREACH_DIRENT_ALL(de, d, log_warning_errno(errno, "Failed to read \"%s\", ignoring: %m", *dir)) {
594167
                         _unused_ _cleanup_free_ char *_filename_free = NULL;
594167
-                        _cleanup_free_ char *simplified = NULL;
594167
-                        bool symlink_to_dir = false;
594167
-                        const char *dst = NULL;
594167
                         char *filename;
594167
+                        _cleanup_free_ char *dst = NULL;
594167
+                        bool symlink_to_dir = false;
594167
 
594167
                         /* We only care about valid units and dirs with certain suffixes, let's ignore the
594167
                          * rest. */
594167
@@ -397,77 +473,34 @@ int unit_file_build_name_map(
594167
                                 /* We don't explicitly check for alias loops here. unit_ids_map_get() which
594167
                                  * limits the number of hops should be used to access the map. */
594167
 
594167
-                                _cleanup_free_ char *target = NULL;
594167
-
594167
-                                r = readlinkat_malloc(dirfd(d), de->d_name, &target);
594167
-                                if (r < 0) {
594167
-                                        log_warning_errno(r, "Failed to read symlink %s/%s, ignoring: %m",
594167
-                                                          *dir, de->d_name);
594167
+                                r = unit_file_resolve_symlink(lp->root_dir, lp->search_path,
594167
+                                                              *dir, dirfd(d), de->d_name,
594167
+                                                              &dst);
594167
+                                if (r == -ENOMEM)
594167
+                                        return r;
594167
+                                if (r < 0)  /* we ignore other errors here */
594167
                                         continue;
594167
-                                }
594167
 
594167
-                                const bool is_abs = path_is_absolute(target);
594167
-                                if (lp->root_dir || !is_abs) {
594167
-                                        char *target_abs = path_join(is_abs ? lp->root_dir : *dir, target);
594167
-                                        if (!target_abs)
594167
+                        } else {
594167
+                                dst = TAKE_PTR(_filename_free); /* Grab the copy we made previously, if available. */
594167
+                                if (!dst) {
594167
+                                        dst = strdup(filename);
594167
+                                        if (!dst)
594167
                                                 return log_oom();
594167
-
594167
-                                        free_and_replace(target, target_abs);
594167
                                 }
594167
 
594167
-                                /* Get rid of "." and ".." components in target path */
594167
-                                r = chase_symlinks(target, lp->root_dir, CHASE_NOFOLLOW | CHASE_NONEXISTENT, &simplified, NULL);
594167
-                                if (r < 0) {
594167
-                                        log_warning_errno(r, "Failed to resolve symlink %s pointing to %s, ignoring: %m",
594167
-                                                          filename, target);
594167
-                                        continue;
594167
-                                }
594167
-
594167
-                                /* Check if the symlink goes outside of our search path.
594167
-                                 * If yes, it's a linked unit file or mask, and we don't care about the target name.
594167
-                                 * Let's just store the link source directly.
594167
-                                 * If not, let's verify that it's a good symlink. */
594167
-                                char *tail = path_startswith_strv(simplified, lp->search_path);
594167
-                                if (!tail) {
594167
-                                        log_debug("%s: linked unit file: %s → %s",
594167
-                                                  __func__, filename, simplified);
594167
-
594167
-                                        dst = filename;
594167
-                                } else {
594167
-
594167
-                                        bool self_alias;
594167
-
594167
-                                        dst = basename(simplified);
594167
-                                        self_alias = streq(dst, de->d_name);
594167
-
594167
-                                        if (is_path(tail))
594167
-                                                log_full(self_alias ? LOG_DEBUG : LOG_WARNING,
594167
-                                                         "Suspicious symlink %s→%s, treating as alias.",
594167
-                                                         filename, simplified);
594167
-
594167
-                                        r = unit_validate_alias_symlink_and_warn(filename, simplified);
594167
-                                        if (r < 0)
594167
-                                                continue;
594167
-
594167
-                                        if (self_alias) {
594167
-                                                /* A self-alias that has no effect */
594167
-                                                log_debug("%s: self-alias: %s/%s → %s, ignoring.",
594167
-                                                          __func__, *dir, de->d_name, dst);
594167
-                                                continue;
594167
-                                        }
594167
-
594167
-                                        log_debug("%s: alias: %s/%s → %s", __func__, *dir, de->d_name, dst);
594167
-                                }
594167
-
594167
-                        } else {
594167
-                                dst = filename;
594167
                                 log_debug("%s: normal unit file: %s", __func__, dst);
594167
                         }
594167
 
594167
-                        r = hashmap_put_strdup(&ids, de->d_name, dst);
594167
+                        _cleanup_free_ char *key = strdup(de->d_name);
594167
+                        if (!key)
594167
+                                return log_oom();
594167
+
594167
+                        r = hashmap_ensure_put(&ids, &string_hash_ops_free_free, key, dst);
594167
                         if (r < 0)
594167
                                 return log_warning_errno(r, "Failed to add entry to hashmap (%s→%s): %m",
594167
                                                          de->d_name, dst);
594167
+                        key = dst = NULL;
594167
                 }
594167
         }
594167