naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
ecbff1
From d23386f61d810dab77e9d9d9130adbd826ea823f Mon Sep 17 00:00:00 2001
ecbff1
From: Michal Sekletar <msekleta@redhat.com>
ecbff1
Date: Thu, 7 Sep 2017 15:49:08 +0200
ecbff1
Subject: [PATCH] tmpfiles: substitute % specifiers in arguments for writing
ecbff1
 files and xattrs
ecbff1
ecbff1
(cherry-picked from commit bd550f78eb261c757cbff85acdb55563c56521f2)
ecbff1
ecbff1
Related: #1299714
ecbff1
---
de8967
 src/tmpfiles/tmpfiles.c | 79 ++++++++++++++++++++++-------------------
ecbff1
 1 file changed, 42 insertions(+), 37 deletions(-)
ecbff1
ecbff1
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
ecbff1
index 70e0cc2fa..ddb274fce 100644
ecbff1
--- a/src/tmpfiles/tmpfiles.c
ecbff1
+++ b/src/tmpfiles/tmpfiles.c
ecbff1
@@ -151,6 +151,14 @@ static const char conf_file_dirs[] = CONF_DIRS_NULSTR("tmpfiles");
ecbff1
 static Hashmap *items = NULL, *globs = NULL;
ecbff1
 static Set *unix_sockets = NULL;
ecbff1
 
ecbff1
+static const Specifier specifier_table[] = {
ecbff1
+        { 'm', specifier_machine_id, NULL },
ecbff1
+        { 'b', specifier_boot_id, NULL },
ecbff1
+        { 'H', specifier_host_name, NULL },
ecbff1
+        { 'v', specifier_kernel_release, NULL },
ecbff1
+        {}
ecbff1
+};
ecbff1
+
ecbff1
 static bool needs_glob(ItemType t) {
ecbff1
         return IN_SET(t,
ecbff1
                       WRITE_FILE,
ecbff1
@@ -657,8 +665,7 @@ static int path_set_perms(Item *i, const char *path) {
ecbff1
         return label_fix(path, false, false);
ecbff1
 }
ecbff1
 
ecbff1
-static int get_xattrs_from_arg(Item *i) {
ecbff1
-        char *xattr;
ecbff1
+static int parse_xattrs_from_arg(Item *i) {
ecbff1
         const char *p;
ecbff1
         int r;
ecbff1
 
ecbff1
@@ -667,35 +674,37 @@ static int get_xattrs_from_arg(Item *i) {
ecbff1
 
ecbff1
         p = i->argument;
ecbff1
 
ecbff1
-        while ((r = unquote_first_word(&p, &xattr, false)) > 0) {
ecbff1
-                _cleanup_free_ char *tmp = NULL, *name = NULL,
ecbff1
-                        *value = NULL, *value2 = NULL, *_xattr = xattr;
ecbff1
+        for (;;) {
ecbff1
+                _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL, *xattr_replaced = NULL;
ecbff1
+
ecbff1
+                r = unquote_first_word(&p, &xattr, false);
ecbff1
+                if (r < 0)
ecbff1
+                        log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p);
ecbff1
+                if (r <= 0)
ecbff1
+                        break;
ecbff1
+
ecbff1
+                r = specifier_printf(xattr, specifier_table, NULL, &xattr_replaced);
ecbff1
+                if (r < 0)
ecbff1
+                        return log_error_errno(r, "Failed to replace specifiers in extended attribute '%s': %m", xattr);
ecbff1
 
ecbff1
-                r = split_pair(xattr, "=", &name, &value);
ecbff1
+                r = split_pair(xattr_replaced, "=", &name, &value);
ecbff1
                 if (r < 0) {
ecbff1
                         log_warning("Illegal xattr found: \"%s\" - ignoring.", xattr);
ecbff1
                         continue;
ecbff1
                 }
ecbff1
 
ecbff1
-                if (strempty(name) || strempty(value)) {
ecbff1
-                        log_warning("Malformed xattr found: \"%s\" - ignoring.", xattr);
ecbff1
+                if (isempty(name) || isempty(value)) {
ecbff1
+                        log_warning("Malformed extended attribute found, ignoring: %s", xattr);
ecbff1
                         continue;
ecbff1
                 }
ecbff1
 
ecbff1
-                tmp = unquote(value, "\"");
ecbff1
-                if (!tmp)
ecbff1
-                        return log_oom();
ecbff1
-
ecbff1
-                value2 = cunescape(tmp);
ecbff1
-                if (!value2)
ecbff1
+                if (strv_push_pair(&i->xattrs, name, value) < 0)
ecbff1
                         return log_oom();
ecbff1
 
ecbff1
-                if (strv_push_pair(&i->xattrs, name, value2) < 0)
ecbff1
-                        return log_oom();
ecbff1
-                name = value2 = NULL;
ecbff1
+                name = value = NULL;
ecbff1
         }
ecbff1
 
ecbff1
-        return r;
ecbff1
+        return 0;
ecbff1
 }
ecbff1
 
ecbff1
 static int path_set_xattrs(Item *i, const char *path) {
ecbff1
@@ -708,17 +717,16 @@ static int path_set_xattrs(Item *i, const char *path) {
ecbff1
                 int n;
ecbff1
 
ecbff1
                 n = strlen(*value);
ecbff1
-                log_debug("\"%s\": setting xattr \"%s=%s\"", path, *name, *value);
ecbff1
+                log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path);
ecbff1
                 if (lsetxattr(path, *name, *value, n, 0) < 0) {
ecbff1
-                        log_error("Setting extended attribute %s=%s on %s failed: %m",
ecbff1
-                                  *name, *value, path);
ecbff1
+                        log_error("Setting extended attribute %s=%s on %s failed: %m", *name, *value, path);
ecbff1
                         return -errno;
ecbff1
                 }
ecbff1
         }
ecbff1
         return 0;
ecbff1
 }
ecbff1
 
ecbff1
-static int get_acls_from_arg(Item *item) {
ecbff1
+static int parse_acls_from_arg(Item *item) {
ecbff1
 #ifdef HAVE_ACL
ecbff1
         int r;
ecbff1
 
ecbff1
@@ -726,6 +734,7 @@ static int get_acls_from_arg(Item *item) {
ecbff1
 
ecbff1
         /* If force (= modify) is set, we will not modify the acl
ecbff1
          * afterwards, so the mask can be added now if necessary. */
ecbff1
+
ecbff1
         r = parse_acl(item->argument, &item->acl_access, &item->acl_default, !item->force);
ecbff1
         if (r < 0)
ecbff1
                 log_warning_errno(r, "Failed to parse ACL \"%s\": %m. Ignoring",
ecbff1
@@ -839,7 +848,7 @@ static int path_set_acls(Item *item, const char *path) {
ecbff1
          FS_TOPDIR_FL       |                   \
ecbff1
          FS_NOCOW_FL)
ecbff1
 
ecbff1
-static int get_attribute_from_arg(Item *item) {
ecbff1
+static int parse_attribute_from_arg(Item *item) {
ecbff1
 
ecbff1
         static const struct {
ecbff1
                 char character;
ecbff1
@@ -993,7 +1002,7 @@ static int write_one_file(Item *i, const char *path) {
ecbff1
         }
ecbff1
 
ecbff1
         if (i->argument) {
ecbff1
-                _cleanup_free_ char *unescaped;
ecbff1
+                _cleanup_free_ char *unescaped = NULL, *replaced = NULL;
ecbff1
 
ecbff1
                 log_debug("%s to \"%s\".",
ecbff1
                           i->type == CREATE_FILE ? "Appending" : "Writing", path);
ecbff1
@@ -1002,7 +1011,11 @@ static int write_one_file(Item *i, const char *path) {
ecbff1
                 if (!unescaped)
ecbff1
                         return log_oom();
ecbff1
 
ecbff1
-                r = loop_write(fd, unescaped, strlen(unescaped), false);
ecbff1
+                r = specifier_printf(unescaped, specifier_table, NULL, &replaced);
ecbff1
+                if (r < 0)
ecbff1
+                        return log_error_errno(r, "Failed to replace specifiers in parameter to write '%s': %m", unescaped);
ecbff1
+
ecbff1
+                r = loop_write(fd, replaced, strlen(replaced), false);
ecbff1
                 if (r < 0)
ecbff1
                         return log_error_errno(r, "Failed to write file \"%s\": %m", path);
ecbff1
         } else
ecbff1
@@ -1712,14 +1725,6 @@ static bool should_include_path(const char *path) {
ecbff1
 
ecbff1
 static int parse_line(const char *fname, unsigned line, const char *buffer) {
ecbff1
 
ecbff1
-        static const Specifier specifier_table[] = {
ecbff1
-                { 'm', specifier_machine_id, NULL },
ecbff1
-                { 'b', specifier_boot_id, NULL },
ecbff1
-                { 'H', specifier_host_name, NULL },
ecbff1
-                { 'v', specifier_kernel_release, NULL },
ecbff1
-                {}
ecbff1
-        };
ecbff1
-
ecbff1
         _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
ecbff1
         _cleanup_(item_free_contents) Item i = {};
ecbff1
         ItemArray *existing;
ecbff1
@@ -1801,7 +1806,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
ecbff1
         case RELABEL_PATH:
ecbff1
         case RECURSIVE_RELABEL_PATH:
ecbff1
                 if (i.argument)
ecbff1
-                        log_warning("[%s:%u] %c lines don't take argument field, ignoring.", fname, line, i.type);
ecbff1
+                        log_warning("[%s:%u] %c lines don't take argument fields, ignoring.", fname, line, i.type);
ecbff1
 
ecbff1
                 break;
ecbff1
 
ecbff1
@@ -1861,7 +1866,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
ecbff1
                         log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
ecbff1
                         return -EBADMSG;
ecbff1
                 }
ecbff1
-                r = get_xattrs_from_arg(&i);
ecbff1
+                r = parse_xattrs_from_arg(&i);
ecbff1
                 if (r < 0)
ecbff1
                         return r;
ecbff1
                 break;
ecbff1
@@ -1872,7 +1877,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
ecbff1
                         log_error("[%s:%u] Set ACLs requires argument.", fname, line);
ecbff1
                         return -EBADMSG;
ecbff1
                 }
ecbff1
-                r = get_acls_from_arg(&i);
ecbff1
+                r = parse_acls_from_arg(&i);
ecbff1
                 if (r < 0)
ecbff1
                         return r;
ecbff1
                 break;
ecbff1
@@ -1883,7 +1888,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
ecbff1
                         log_error("[%s:%u] Set file attribute requires argument.", fname, line);
ecbff1
                         return -EBADMSG;
ecbff1
                 }
ecbff1
-                r = get_attribute_from_arg(&i);
ecbff1
+                r = parse_attribute_from_arg(&i);
ecbff1
                 if (r < 0)
ecbff1
                         return r;
ecbff1
                 break;