teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame 0001-tmpfiles-make-purge-hard-to-mis-use.patch

1cdae0
From 1e788a7fb535a37a8268aa7dc5130f670eb72a6b Mon Sep 17 00:00:00 2001
1cdae0
From: Daan De Meyer <daan.j.demeyer@gmail.com>
1cdae0
Date: Tue, 23 Jul 2024 13:14:05 +0200
Zbigniew Jędrzejewski-Szmek b3e1d5
Subject: [PATCH] tmpfiles: make --purge hard to (mis-)use
Zbigniew Jędrzejewski-Szmek b3e1d5
Zbigniew Jędrzejewski-Szmek b3e1d5
Follow-up for https://github.com/systemd/systemd/pull/33383.
Zbigniew Jędrzejewski-Szmek b3e1d5
---
1cdae0
 src/tmpfiles/tmpfiles.c           | 17 +++++++++++++++++
1cdae0
 test/units/TEST-22-TMPFILES.18.sh |  4 ++--
1cdae0
 2 files changed, 19 insertions(+), 2 deletions(-)
Zbigniew Jędrzejewski-Szmek b3e1d5
Zbigniew Jędrzejewski-Szmek b3e1d5
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
1cdae0
index 8cc8c1ccd6..14048545db 100644
Zbigniew Jędrzejewski-Szmek b3e1d5
--- a/src/tmpfiles/tmpfiles.c
Zbigniew Jędrzejewski-Szmek b3e1d5
+++ b/src/tmpfiles/tmpfiles.c
1cdae0
@@ -4197,6 +4197,7 @@ static int parse_argv(int argc, char *argv[]) {
Zbigniew Jędrzejewski-Szmek b3e1d5
                 ARG_IMAGE_POLICY,
Zbigniew Jędrzejewski-Szmek b3e1d5
                 ARG_REPLACE,
Zbigniew Jędrzejewski-Szmek b3e1d5
                 ARG_DRY_RUN,
Zbigniew Jędrzejewski-Szmek b3e1d5
+                ARG_DESTROY_DATA,
Zbigniew Jędrzejewski-Szmek b3e1d5
                 ARG_NO_PAGER,
Zbigniew Jędrzejewski-Szmek b3e1d5
         };
Zbigniew Jędrzejewski-Szmek b3e1d5
 
1cdae0
@@ -4220,10 +4221,18 @@ static int parse_argv(int argc, char *argv[]) {
Zbigniew Jędrzejewski-Szmek b3e1d5
                 { "replace",        required_argument,   NULL, ARG_REPLACE        },
Zbigniew Jędrzejewski-Szmek b3e1d5
                 { "dry-run",        no_argument,         NULL, ARG_DRY_RUN        },
Zbigniew Jędrzejewski-Szmek b3e1d5
                 { "no-pager",       no_argument,         NULL, ARG_NO_PAGER       },
Zbigniew Jędrzejewski-Szmek b3e1d5
+
Zbigniew Jędrzejewski-Szmek b3e1d5
+                /* This is not documented on purpose.
Zbigniew Jędrzejewski-Szmek b3e1d5
+                 * If you think --purge should be allowed without jumping through hoops,
Zbigniew Jędrzejewski-Szmek b3e1d5
+                 * consider opening a bug report with the description of the use case.
Zbigniew Jędrzejewski-Szmek b3e1d5
+                 */
Zbigniew Jędrzejewski-Szmek b3e1d5
+                { "destroy-data",   no_argument,         NULL, ARG_DESTROY_DATA   },
Zbigniew Jędrzejewski-Szmek b3e1d5
+
Zbigniew Jędrzejewski-Szmek b3e1d5
                 {}
Zbigniew Jędrzejewski-Szmek b3e1d5
         };
Zbigniew Jędrzejewski-Szmek b3e1d5
 
Zbigniew Jędrzejewski-Szmek b3e1d5
         int c, r;
Zbigniew Jędrzejewski-Szmek b3e1d5
+        bool destroy_data = false;
Zbigniew Jędrzejewski-Szmek b3e1d5
 
Zbigniew Jędrzejewski-Szmek b3e1d5
         assert(argc >= 0);
Zbigniew Jędrzejewski-Szmek b3e1d5
         assert(argv);
1cdae0
@@ -4330,6 +4339,10 @@ static int parse_argv(int argc, char *argv[]) {
Zbigniew Jędrzejewski-Szmek b3e1d5
                         arg_dry_run = true;
Zbigniew Jędrzejewski-Szmek b3e1d5
                         break;
Zbigniew Jędrzejewski-Szmek b3e1d5
 
Zbigniew Jędrzejewski-Szmek b3e1d5
+                case ARG_DESTROY_DATA:
Zbigniew Jędrzejewski-Szmek b3e1d5
+                        destroy_data = true;
Zbigniew Jędrzejewski-Szmek b3e1d5
+                        break;
Zbigniew Jędrzejewski-Szmek b3e1d5
+
Zbigniew Jędrzejewski-Szmek b3e1d5
                 case ARG_NO_PAGER:
Zbigniew Jędrzejewski-Szmek b3e1d5
                         arg_pager_flags |= PAGER_DISABLE;
Zbigniew Jędrzejewski-Szmek b3e1d5
                         break;
1cdae0
@@ -4349,6 +4362,10 @@ static int parse_argv(int argc, char *argv[]) {
Zbigniew Jędrzejewski-Szmek b3e1d5
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
Zbigniew Jędrzejewski-Szmek b3e1d5
                                        "Refusing --purge without specification of a configuration file.");
Zbigniew Jędrzejewski-Szmek b3e1d5
 
Zbigniew Jędrzejewski-Szmek b3e1d5
+        if (FLAGS_SET(arg_operation, OPERATION_PURGE) && !arg_dry_run && !destroy_data)
Zbigniew Jędrzejewski-Szmek b3e1d5
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
Zbigniew Jędrzejewski-Szmek b3e1d5
+                                       "Refusing --purge without --destroy-data.");
Zbigniew Jędrzejewski-Szmek b3e1d5
+
Zbigniew Jędrzejewski-Szmek b3e1d5
         if (arg_replace && arg_cat_flags != CAT_CONFIG_OFF)
Zbigniew Jędrzejewski-Szmek b3e1d5
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
Zbigniew Jędrzejewski-Szmek b3e1d5
                                        "Option --replace= is not supported with --cat-config/--tldr.");
1cdae0
diff --git a/test/units/TEST-22-TMPFILES.18.sh b/test/units/TEST-22-TMPFILES.18.sh
1cdae0
index 5d24197c81..de23bbb95f 100755
1cdae0
--- a/test/units/TEST-22-TMPFILES.18.sh
1cdae0
+++ b/test/units/TEST-22-TMPFILES.18.sh
1cdae0
@@ -21,7 +21,7 @@ systemd-tmpfiles --purge --dry-run - <<<"$c"
1cdae0
 test -f /tmp/somedir/somefile
1cdae0
 grep -q baz /tmp/somedir/somefile
1cdae0
 
1cdae0
-systemd-tmpfiles --purge - <<<"$c"
1cdae0
+systemd-tmpfiles --purge --destroy-data - <<<"$c"
1cdae0
 test ! -f /tmp/somedir/somefile
1cdae0
 test ! -d /tmp/somedir/
1cdae0
 
1cdae0
@@ -29,6 +29,6 @@ systemd-tmpfiles --create --purge --dry-run - <<<"$c"
1cdae0
 test ! -f /tmp/somedir/somefile
1cdae0
 test ! -d /tmp/somedir/
1cdae0
 
1cdae0
-systemd-tmpfiles --create --purge - <<<"$c"
1cdae0
+systemd-tmpfiles --create --destroy-data --purge - <<<"$c"
1cdae0
 test -f /tmp/somedir/somefile
1cdae0
 grep -q baz /tmp/somedir/somefile
1cdae0
-- 
1cdae0
2.45.2
1cdae0