From bfe5b859214772fed76ed04e29224fdd4e028a3e Mon Sep 17 00:00:00 2001
From: INODE64 <ffelix@inode64.com>
Date: Thu, 23 Apr 2026 15:29:59 +0200
Subject: [PATCH] Add support for configuration file override and version

---
 src/aide.c | 41 +++++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)

diff --git a/src/aide.c b/src/aide.c
index 6f728a9..e487e47 100644
--- a/src/aide.c
+++ b/src/aide.c
@@ -59,6 +59,8 @@
 db_config* conf;
 char* before = NULL;
 char* after = NULL;
+static char* config_override = NULL;
+static bool print_version_requested = false;
 
 #ifndef MAXHOSTNAMELEN
 #define MAXHOSTNAMELEN 256
@@ -149,16 +151,20 @@ static void print_version(void)
   fprintf(stdout, "Compile-time options:\n%s\n", AIDECOMPILEOPTIONS);
   fprintf(stdout, "Default config values:\n");
   fprintf(stdout, "config file: %s\n", conf->config_file?conf->config_file:"<none>");
+  const char *database_in = conf->database_in.url ? conf->database_in.url->raw :
 #ifdef DEFAULT_DB
-  fprintf(stdout, "database_in: %s\n", DEFAULT_DB);
+      DEFAULT_DB;
 #else
-  fprintf(stdout, "database_in: <none>\n");
+      "<none>";
 #endif
+  const char *database_out = conf->database_out.url ? conf->database_out.url->raw :
 #ifdef DEFAULT_DB_OUT
-  fprintf(stdout, "database_out: %s\n", DEFAULT_DB_OUT),
+      DEFAULT_DB_OUT;
 #else
-  fprintf(stdout, "database_out: <none>\n"),
+      "<none>";
 #endif
+  fprintf(stdout, "database_in: %s\n", database_in);
+  fprintf(stdout, "database_out: %s\n", database_out);
 
   fprintf(stdout, "\nAvailable compiled-in attributes:\n");
   DB_ATTR_TYPE extra_attributes = get_groupval("X");
@@ -267,12 +273,14 @@ static void read_param(int argc,char**argv)
 	break;
       }
       case 'v':{
-	print_version();
+        print_version_requested = true;
 	break;
       }
       case 'c':{
-	  conf->config_file=optarg;
-      log_msg(LOG_LEVEL_INFO,_("(--config): set config file to '%s'"), conf->config_file);
+	free(config_override);
+	config_override = checked_strdup(optarg);
+	conf->config_file = config_override;
+	log_msg(LOG_LEVEL_INFO,_("(--config): set config file to '%s'"), conf->config_file);
 	break;
       }
       case 'B': {
@@ -418,7 +426,7 @@ static void read_param(int argc,char**argv)
       }
   }
 
-  if(optind<argc){
+  if(!print_version_requested && optind<argc){
     fprintf(stderr, "%s: extra parameter: '%s'\n", argv[0], argv[optind]);
     exit(INVALID_ARGUMENT_ERROR);
   }
@@ -676,6 +684,23 @@ int main(int argc,char**argv)
   log_msg(LOG_LEVEL_INFO, "read command line parameters");
   read_param(argc,argv);
 
+  if (print_version_requested) {
+      if (config_override) {
+          log_msg(LOG_LEVEL_INFO, "parse configuration");
+          progress_status(PROGRESS_CONFIG, NULL);
+          errorno=parse_config(before, config_override, after);
+          if (errorno==RETFAIL){
+              exit(INVALID_CONFIGURELINE_ERROR);
+          }
+          free(before);
+          before = NULL;
+          free(after);
+          after = NULL;
+          setdefaults_after_config();
+      }
+      print_version();
+  }
+
   int stderr_isatty = isatty(STDERR_FILENO);
   if (stderr_isatty == 0) {
         log_msg(LOG_LEVEL_DEBUG, "isatty() failed for 'STDERR_FILENO': %s", strerror(errno));
-- 
2.53.0

