aports/testing/pihole/0001-Support-printing-default-TOML-config.patch
2025-05-12 19:10:20 +00:00

165 lines
5.4 KiB
Diff

From 2606c11dae6d8b1383aad2be2ed57ec2c968baf2 Mon Sep 17 00:00:00 2001
From: Mike Crute <mike@crute.us>
Date: Sat, 10 May 2025 15:09:46 -0700
Subject: [PATCH 1/3] Support printing default TOML config
Allow Linux distributions that want to package the default configuration
to be able to generate the configuration statically for their packages
rather than requiring it to be generated at first start time. This also
allows for distro config management systems to provide better support
for changes to the default over time.
---
src/args.c | 11 ++++++++++
src/config/config.c | 3 +--
src/config/config.h | 1 +
src/config/toml_writer.c | 46 ++++++++++++++++++++++------------------
src/config/toml_writer.h | 1 +
5 files changed, 39 insertions(+), 23 deletions(-)
diff --git a/FTL-6.1/src/args.c b/FTL-6.1/src/args.c
index 363da394..81ec39f4 100644
--- a/FTL-6.1/src/args.c
+++ b/FTL-6.1/src/args.c
@@ -45,6 +45,7 @@
// cJSON_Version()
#include "webserver/cJSON/cJSON.h"
#include "config/cli.h"
+// initConfig
#include "config/config.h"
// compression functions
#include "zip/gzip.h"
@@ -70,6 +71,8 @@
#include "ntp/ntp.h"
// check_capability()
#include "capabilities.h"
+// writeFTLtomlTo
+#include "config/toml_writer.h"
// defined in dnsmasq.c
extern void print_dnsmasq_version(const char *yellow, const char *green, const char *bold, const char *normal);
@@ -284,6 +287,14 @@ void parse_args(int argc, char *argv[])
exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
}
+ // Print default configuration to stdout
+ if(argc > 1 && strcmp(argv[1], "--print-default-config") == 0)
+ {
+ initConfig(&config);
+ writeFTLtomlTo(stdout, false);
+ exit(EXIT_SUCCESS);
+ }
+
// Set config option through CLI
if(argc > 1 && strcmp(argv[1], "--config") == 0)
{
diff --git a/FTL-6.1/src/config/config.c b/FTL-6.1/src/config/config.c
index 59fcb23f..c98add76 100644
--- a/FTL-6.1/src/config/config.c
+++ b/FTL-6.1/src/config/config.c
@@ -46,7 +46,6 @@ uint8_t last_checksum[SHA256_DIGEST_SIZE] = { 0 };
// Private prototypes
static bool port_in_use(const in_port_t port);
static void reset_config_default(struct conf_item *conf_item);
-static void initConfig(struct config *conf);
// Set debug flags from config struct to global debug_flags array
// This is called whenever the config is reloaded and debug flags may have
@@ -390,7 +389,7 @@ void free_config(struct config *conf)
}
}
-static void initConfig(struct config *conf)
+void initConfig(struct config *conf)
{
if(config_initialized)
return;
diff --git a/FTL-6.1/src/config/config.h b/FTL-6.1/src/config/config.h
index 974eb853..780793a2 100644
--- a/FTL-6.1/src/config/config.h
+++ b/FTL-6.1/src/config/config.h
@@ -377,6 +377,7 @@ 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);
+void initConfig(struct config *conf);
// Defined in toml_reader.c
bool readDebugSettings(void);
diff --git a/FTL-6.1/src/config/toml_writer.c b/FTL-6.1/src/config/toml_writer.c
index 8d3ed407..d911a5a1 100644
--- a/FTL-6.1/src/config/toml_writer.c
+++ b/FTL-6.1/src/config/toml_writer.c
@@ -25,27 +25,7 @@
// defined in config/config.c
extern uint8_t last_checksum[SHA256_DIGEST_SIZE];
-bool writeFTLtoml(const bool verbose)
-{
- // Return early without writing if we are in config read-only mode
- if(config.misc.readOnly.v.b)
- {
- log_debug(DEBUG_CONFIG, "Config file is read-only, not writing");
-
- // We need to (re-)calculate the checksum here as it'd otherwise
- // be outdated (in non-read-only mode, it's calculated at the
- // end of this function)
- if(!sha256sum(GLOBALTOMLPATH, last_checksum, false))
- log_err("Unable to create checksum of %s", GLOBALTOMLPATH);
- return true;
- }
-
- // Try to open a temporary config file for writing
- bool locked = false;
- FILE *fp = openFTLtoml("w", 0, &locked);
- if(fp == NULL)
- return false;
-
+void writeFTLtomlTo(FILE* fp, const bool verbose) {
// Write header
fprintf(fp, "# Pi-hole configuration file (%s)\n", get_FTL_version());
fputs("# Encoding: UTF-8\n", fp);
@@ -162,6 +142,30 @@ bool writeFTLtoml(const bool verbose)
// Free cJSON array
cJSON_Delete(env_vars);
+}
+
+bool writeFTLtoml(const bool verbose)
+{
+ // Return early without writing if we are in config read-only mode
+ if(config.misc.readOnly.v.b)
+ {
+ log_debug(DEBUG_CONFIG, "Config file is read-only, not writing");
+
+ // We need to (re-)calculate the checksum here as it'd otherwise
+ // be outdated (in non-read-only mode, it's calculated at the
+ // end of this function)
+ if(!sha256sum(GLOBALTOMLPATH, last_checksum, false))
+ log_err("Unable to create checksum of %s", GLOBALTOMLPATH);
+ return true;
+ }
+
+ // Try to open a temporary config file for writing
+ bool locked = false;
+ FILE *fp = openFTLtoml("w", 0, &locked);
+ if(fp == NULL)
+ return false;
+
+ writeFTLtomlTo(fp, verbose);
// Close file and release exclusive lock
closeFTLtoml(fp, locked);
diff --git a/FTL-6.1/src/config/toml_writer.h b/FTL-6.1/src/config/toml_writer.h
index 2cc82ab3..5366e89c 100644
--- a/FTL-6.1/src/config/toml_writer.h
+++ b/FTL-6.1/src/config/toml_writer.h
@@ -11,5 +11,6 @@
#define TOML_WRITER_H
bool writeFTLtoml(const bool verbose);
+void writeFTLtomlTo(FILE* fp, const bool verbose);
#endif //TOML_WRITER_H
--
2.45.2