Florian Festi 94360a
From 6dd62720fe84f7e2ad902c915b952fc0b29e3dcd Mon Sep 17 00:00:00 2001
Florian Festi 94360a
From: Panu Matilainen <pmatilai@redhat.com>
Florian Festi 94360a
Date: Tue, 15 Feb 2022 11:34:37 +0200
Florian Festi 94360a
Subject: [PATCH] Swap over to dirfd+basename based operation within the fsm
Florian Festi 94360a
Florian Festi 94360a
Within fsm this is just a matter of adjusting error messages to include
Florian Festi 94360a
the directory... if it only wasn't for the plugins requiring absolute
Florian Festi 94360a
paths for outside users. For the plugins, we need to assemble absolute
Florian Festi 94360a
paths as needed, both in ensureDir() and plugin file slots.
Florian Festi 94360a
---
Florian Festi 94360a
 lib/rpmplugins.c | 20 +++++++++++++++++---
Florian Festi 94360a
 2 files changed, 36 insertions(+), 14 deletions(-)
Florian Festi 94360a
Florian Festi 94360a
diff --git a/lib/rpmplugins.c b/lib/rpmplugins.c
Florian Festi 94360a
index 703368c0d..f06fd7895 100644
Florian Festi 94360a
--- a/lib/rpmplugins.c
Florian Festi 94360a
+++ b/lib/rpmplugins.c
Florian Festi 94360a
@@ -350,21 +350,31 @@ rpmRC rpmpluginsCallScriptletPost(rpmPlugins plugins, const char *s_name, int ty
Florian Festi 94360a
     return rc;
Florian Festi 94360a
 }
Florian Festi 94360a
 
Florian Festi 94360a
+static char *abspath(rpmfi fi, const char *path)
Florian Festi 94360a
+{
Florian Festi 94360a
+    if (*path == '/')
Florian Festi 94360a
+	return xstrdup(path);
Florian Festi 94360a
+    else
Florian Festi 94360a
+	return rstrscat(NULL, rpmfiDN(fi), path, NULL);
Florian Festi 94360a
+}
Florian Festi 94360a
+
Florian Festi 94360a
 rpmRC rpmpluginsCallFsmFilePre(rpmPlugins plugins, rpmfi fi, const char *path,
Florian Festi 94360a
 			       mode_t file_mode, rpmFsmOp op)
Florian Festi 94360a
 {
Florian Festi 94360a
     plugin_fsm_file_pre_func hookFunc;
Florian Festi 94360a
     int i;
Florian Festi 94360a
     rpmRC rc = RPMRC_OK;
Florian Festi 94360a
+    char *apath = abspath(fi, path);
Florian Festi 94360a
 
Florian Festi 94360a
     for (i = 0; i < plugins->count; i++) {
Florian Festi 94360a
 	rpmPlugin plugin = plugins->plugins[i];
Florian Festi 94360a
 	RPMPLUGINS_SET_HOOK_FUNC(fsm_file_pre);
Florian Festi 94360a
-	if (hookFunc && hookFunc(plugin, fi, path, file_mode, op) == RPMRC_FAIL) {
Florian Festi 94360a
+	if (hookFunc && hookFunc(plugin, fi, apath, file_mode, op) == RPMRC_FAIL) {
Florian Festi 94360a
 	    rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_pre failed\n", plugin->name);
Florian Festi 94360a
 	    rc = RPMRC_FAIL;
Florian Festi 94360a
 	}
Florian Festi 94360a
     }
Florian Festi 94360a
+    free(apath);
Florian Festi 94360a
 
Florian Festi 94360a
     return rc;
Florian Festi 94360a
 }
Florian Festi 94360a
@@ -375,14 +385,16 @@ rpmRC rpmpluginsCallFsmFilePost(rpmPlugins plugins, rpmfi fi, const char *path,
Florian Festi 94360a
     plugin_fsm_file_post_func hookFunc;
Florian Festi 94360a
     int i;
Florian Festi 94360a
     rpmRC rc = RPMRC_OK;
Florian Festi 94360a
+    char *apath = abspath(fi, path);
Florian Festi 94360a
 
Florian Festi 94360a
     for (i = 0; i < plugins->count; i++) {
Florian Festi 94360a
 	rpmPlugin plugin = plugins->plugins[i];
Florian Festi 94360a
 	RPMPLUGINS_SET_HOOK_FUNC(fsm_file_post);
Florian Festi 94360a
-	if (hookFunc && hookFunc(plugin, fi, path, file_mode, op, res) == RPMRC_FAIL) {
Florian Festi 94360a
+	if (hookFunc && hookFunc(plugin, fi, apath, file_mode, op, res) == RPMRC_FAIL) {
Florian Festi 94360a
 	    rpmlog(RPMLOG_WARNING, "Plugin %s: hook fsm_file_post failed\n", plugin->name);
Florian Festi 94360a
 	}
Florian Festi 94360a
     }
Florian Festi 94360a
+    free(apath);
Florian Festi 94360a
 
Florian Festi 94360a
     return rc;
Florian Festi 94360a
 }
Florian Festi 94360a
@@ -394,15 +406,17 @@ rpmRC rpmpluginsCallFsmFilePrepare(rpmPlugins plugins, rpmfi fi,
Florian Festi 94360a
     plugin_fsm_file_prepare_func hookFunc;
Florian Festi 94360a
     int i;
Florian Festi 94360a
     rpmRC rc = RPMRC_OK;
Florian Festi 94360a
+    char *apath = abspath(fi, path);
Florian Festi 94360a
 
Florian Festi 94360a
     for (i = 0; i < plugins->count; i++) {
Florian Festi 94360a
 	rpmPlugin plugin = plugins->plugins[i];
Florian Festi 94360a
 	RPMPLUGINS_SET_HOOK_FUNC(fsm_file_prepare);
Florian Festi 94360a
-	if (hookFunc && hookFunc(plugin, fi, fd, path, dest, file_mode, op) == RPMRC_FAIL) {
Florian Festi 94360a
+	if (hookFunc && hookFunc(plugin, fi, fd, apath, dest, file_mode, op) == RPMRC_FAIL) {
Florian Festi 94360a
 	    rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_prepare failed\n", plugin->name);
Florian Festi 94360a
 	    rc = RPMRC_FAIL;
Florian Festi 94360a
 	}
Florian Festi 94360a
     }
Florian Festi 94360a
+    free(apath);
Florian Festi 94360a
 
Florian Festi 94360a
     return rc;
Florian Festi 94360a
 }
Florian Festi 94360a
-- 
Florian Festi 94360a
2.41.0
Florian Festi 94360a