Skip to content

Commit

Permalink
Check and create v6 migration directory before trying to move/write f…
Browse files Browse the repository at this point in the history
…iles there. This involves config migrations but also Teleporter importing

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Sep 13, 2024
1 parent 29f8c67 commit 2f93a75
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/api/teleporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,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
Expand Down
37 changes: 36 additions & 1 deletion src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,35 @@ static void reset_config_default(struct conf_item *conf_item)
}
}

/**
* @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.
*/
static 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;
}

bool readFTLconf(struct config *conf, const bool rewrite)
{
// Initialize config with default values
Expand Down Expand Up @@ -1624,13 +1653,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));
Expand Down
3 changes: 3 additions & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/config/dnsmasq_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/config/setupVars.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f93a75

Please sign in to comment.