dryang / rpms / systemd

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