From 850d263ec3bc6fb0faaf72c361e7b38ed534ebeb Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 13 Sep 2024 15:52:13 +0200 Subject: [PATCH] Check and create v6 migration directory before trying to move/write files there. This involves config migrations but also Teleporter importing Signed-off-by: DL6ER --- src/api/teleporter.c | 5 +++++ src/config/config.c | 37 ++++++++++++++++++++++++++++++++++++- src/config/config.h | 4 ++++ src/config/dnsmasq_config.h | 6 +++--- src/config/setupVars.c | 2 +- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/api/teleporter.c b/src/api/teleporter.c index bfead147..d3e2b902 100644 --- a/src/api/teleporter.c +++ b/src/api/teleporter.c @@ -29,6 +29,8 @@ #include // restart_ftl() #include "signals.h" +// create_migration_target_v6() +#include "config/config.h" #define MAXFILESIZE (50u*1024*1024) @@ -264,6 +266,9 @@ static int api_teleporter_POST(struct ftl_conn *api) NULL); } + // Ensure v6 migration directory exists + create_migration_target_v6(); + // Check if we received something that claims to be a ZIP archive // - filename should end in ".zip" // - the data itself diff --git a/src/config/config.c b/src/config/config.c index 71576ee8..60b17acb 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -1624,13 +1624,19 @@ bool readFTLconf(struct config *conf, const bool rewrite) if(!rewrite) return false; + // Check if MIGRATION_TARGET_V6 exists and is a directory + // Ideally, this directory should be created by the installer but users + // may have deleted it manually and it is necessary for restoring + // Teleporter files + create_migration_target_v6(); + // If no previous config file could be read, we are likely either running // for the first time or we are upgrading from a version prior to v6.0 // In this case, we try to read the legacy config files const char *path = ""; if((path = readFTLlegacy(conf)) != NULL) { - const char *target = "/etc/pihole/migration_backup_v6/pihole-FTL.conf"; + const char *target = MIGRATION_TARGET_V6"/pihole-FTL.conf"; log_info("Moving %s to %s", path, target); if(rename(path, target) != 0) log_warn("Unable to move %s to %s: %s", path, target, strerror(errno)); @@ -1903,3 +1909,32 @@ static bool port_in_use(const in_port_t port) close(sock); return false; } + +/** + * @brief Create a migration target directory for version 6. + * + * This function creates a directory for migration target version 6. If the directory + * already exists, it does nothing. The function also changes the ownership of the + * directory to the user running the FTL program. + * + * @return true if the directory creation and ownership change were successful, false otherwise. + */ +bool create_migration_target_v6(void) +{ + if(mkdir(MIGRATION_TARGET_V6, 0755) != 0 && errno != EEXIST) + { + log_err("Unable to create directory %s: %s", MIGRATION_TARGET_V6, strerror(errno)); + return false; + } + else + { + // Change ownership of the directory to the user running FTL + if(chown(MIGRATION_TARGET_V6, getuid(), getgid()) != 0) + { + log_err("Unable to change ownership of %s: %s", MIGRATION_TARGET_V6, strerror(errno)); + return false; + } + } + + return true; +} diff --git a/src/config/config.h b/src/config/config.h index bdabc0f9..203347a3 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -39,6 +39,9 @@ // Location of the legacy (pre-v6.0) config file #define GLOBALCONFFILE_LEGACY "/etc/pihole/pihole-FTL.conf" +// Migration target for the legacy (pre-v6.0) config file +#define MIGRATION_TARGET_V6 "/etc/pihole/migration_backup_v6" + union conf_value { bool b; // boolean value int i; // integer value @@ -363,6 +366,7 @@ bool check_paths_equal(char **paths1, char **paths2, unsigned int max_level) __a const char *get_conf_type_str(const enum conf_type type) __attribute__ ((const)); void replace_config(struct config *newconf); void reread_config(void); +bool create_migration_target_v6(void); // Defined in toml_reader.c bool readDebugSettings(void); diff --git a/src/config/dnsmasq_config.h b/src/config/dnsmasq_config.h index 2b40042a..df660790 100644 --- a/src/config/dnsmasq_config.h +++ b/src/config/dnsmasq_config.h @@ -24,12 +24,12 @@ bool write_custom_list(void); #define DNSMASQ_PH_CONFIG "/etc/pihole/dnsmasq.conf" #define DNSMASQ_TEMP_CONF "/etc/pihole/dnsmasq.conf.temp" -#define DNSMASQ_STATIC_LEASES "/etc/pihole/migration_backup_v6/04-pihole-static-dhcp.conf" -#define DNSMASQ_CNAMES "/etc/pihole/migration_backup_v6/05-pihole-custom-cname.conf" +#define DNSMASQ_STATIC_LEASES MIGRATION_TARGET_V6"/04-pihole-static-dhcp.conf" +#define DNSMASQ_CNAMES MIGRATION_TARGET_V6"/05-pihole-custom-cname.conf" #define DNSMASQ_HOSTSDIR "/etc/pihole/hosts" #define DNSMASQ_CUSTOM_LIST DNSMASQ_HOSTSDIR"/custom.list" #define DNSMASQ_CUSTOM_LIST_LEGACY "/etc/pihole/custom.list" -#define DNSMASQ_CUSTOM_LIST_LEGACY_TARGET "/etc/pihole/migration_backup_v6/custom.list" +#define DNSMASQ_CUSTOM_LIST_LEGACY_TARGET MIGRATION_TARGET_V6"/custom.list" #define DHCPLEASESFILE "/etc/pihole/dhcp.leases" #endif //DNSMASQ_CONFIG_H diff --git a/src/config/setupVars.c b/src/config/setupVars.c index e7e3a55c..3a82beeb 100644 --- a/src/config/setupVars.c +++ b/src/config/setupVars.c @@ -589,7 +589,7 @@ void importsetupVarsConf(void) get_conf_string_from_setupVars("WEB_PORTS", &config.webserver.port); // Move the setupVars.conf file to the migration directory - const char *setupVars_target = "/etc/pihole/migration_backup_v6/setupVars.conf"; + const char *setupVars_target = MIGRATION_TARGET_V6"/setupVars.conf"; if(rename(config.files.setupVars.v.s, setupVars_target) != 0) log_warn("Could not move %s to %s", config.files.setupVars.v.s, setupVars_target); else