]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Add bVarSysConfigPath for plugins
authorEric Bollengier <eric@baculasystems.com>
Tue, 4 Oct 2022 14:24:00 +0000 (16:24 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:56:59 +0000 (13:56 +0200)
bacula/src/filed/fd_plugins.c
bacula/src/filed/fd_plugins.h
bacula/src/filed/filed.c
bacula/src/lib/bsys.c
bacula/src/lib/message.c
bacula/src/lib/protos.h
bacula/src/plugins/fd/pluginlib/metaplugin.cpp

index d9d36cf0de45125a51a04d0b4bbee85e6b1dcae9..37232fe0cf4f16dac0e359eb6ba23aa2c80e9013 100644 (file)
@@ -27,6 +27,7 @@
 
 extern CLIENT *me;
 extern DLL_IMP_EXP char *exepath;
+extern DLL_IMP_EXP char *sysconfigpath;
 extern DLL_IMP_EXP char *version;
 extern DLL_IMP_EXP char *dist_name;
 extern DLL_IMP_EXP int beef;
@@ -1989,6 +1990,9 @@ static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
    case bVarExePath:
       *(char **)value = exepath;
       break;
+   case bVarSysConfigPath:
+      *(char **)value = sysconfigpath;
+      break;
    case bVarVersion:
       *(char **)value = version;
       break;
@@ -2099,6 +2103,7 @@ static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
    case bVarWorkingDir:
    case bVarPluginDir:
    case bVarExePath:
+   case bVarSysConfigPath:
    case bVarVersion:
    case bVarDistName:
       break;
index b43d1f92e1def4e43fd1a01d36a15091299b8538..d4a20bf328354ea9703ee2f2df217e3ea16ef511 100644 (file)
@@ -432,7 +432,8 @@ typedef enum {
   bVarReplace               = 24,
   bVarMaxDedupBlockSize     = 25,
   bVarMinDedupBlockSize     = 26,
-  bVarIsCanceled            = 27
+  bVarIsCanceled            = 27,
+  bVarSysConfigPath         = 28
 } bVariable;
 
 /* Events that are passed to plugin */
index f7957e13329f59ba2e5861abc24121a371b09310..6985d25081687f1233706b7df3a61b7758e404a1 100644 (file)
@@ -218,6 +218,7 @@ int main (int argc, char *argv[])
 
    config = New(CONFIG());
    parse_fd_config(config, configfile, M_ERROR_TERM);
+   set_sysconfig_path(configfile); /* Set bVarSysConfigPath for plugins */
 
    if (init_crypto() != 0) {
       Emsg0(M_ERROR, 0, _("Cryptography library initialization failed.\n"));
index 47768d6b4ca3dc00a166418bffe4d238a9a25b36..1f01a0d9aa7cd5c08ff40bbf1f5a728f47c35106 100644 (file)
@@ -1929,6 +1929,81 @@ void b_uname(POOLMEM *&un)
    }
 }
 
+/* Get path/fname from argument, use realpath to get the full name if
+ * available
+ */
+void get_path_and_fname(const char *name, char **path, char **fname)
+{
+   char *l, *p;
+   char *cpath;
+   char *cargv0;
+   int len;
+   int path_max;
+   bool respath;
+
+   if (name) {
+      /* use a dynamic PATH_MAX and allocate temporary variables */
+      path_max = pathconf(name, _PC_PATH_MAX);
+      if (path_max < 4096){
+         path_max = 4096;
+      }
+      cpath = (char *)malloc(path_max);
+      cargv0 = (char *)malloc(path_max);
+
+      respath = false;
+#ifdef HAVE_REALPATH
+      /* make a canonical argv[0] */
+      if (realpath(name, cargv0) != NULL){
+         respath = true;
+      }
+#endif
+      if (!respath){
+         /* no resolved_path available in cargv0, so populate it */
+         bstrncpy(cargv0, name, path_max);
+      }
+      /* strip trailing filename and save exepath */
+      for (l=p=cargv0; *p; p++) {
+         if (IsPathSeparator(*p)) {
+            l = p;                       /* set pos of last path separator */
+         }
+      }
+      if (IsPathSeparator(*l)) {
+         l++;
+      } else {
+         l = cargv0;
+#if defined(HAVE_WIN32)
+         /* On Windows allow c: drive specification */
+         if (l[1] == ':') {
+            l += 2;
+         }
+#endif
+      }
+      len = strlen(l) + 1;
+      if (*fname) {
+         free(*fname);
+      }
+      *fname = (char *)malloc(len);
+      strcpy(*fname, l);
+      if (*path) {
+         free(*path);
+      }
+      /* separate exepath from exename */
+      *l = 0;
+      *path = bstrdup(cargv0);
+      if (strstr(*path, PathSeparatorUp) != NULL || strstr(*fname, PathSeparatorCur) != NULL || !IsPathSeparator(*path[0])) {
+         /* fallback to legacy code */
+         if (getcwd(cpath, path_max)) {
+            free(*path);
+            *path = (char *)malloc(strlen(cpath) + 1 + len);
+            strcpy(*path, cpath);
+         }
+      }
+      Dmsg2(500, "path=%s fname=%s\n", *path, *fname);
+      free(cpath);
+      free(cargv0);
+   }
+}
+
 #ifdef TEST_PROGRAM
 
 #include "unittests.h"
@@ -2197,6 +2272,31 @@ int main(int argc, char **argv)
       ok(bstrcasestr(p1, "xxxxxxxxxxxxxxxxxxxxxxxxx") == NULL, "Test with non existing string");
       is(bstrcasestr(p1, " iS"), " is a test", "Test with a middle string");
    }
+   {
+      char *fname = NULL;
+      char *path = NULL;
+      char cwd[512];
+      bstrncpy(cwd, getenv("PWD"), sizeof(cwd));
+      bstrncat(cwd, "/", sizeof(cwd));
+      get_path_and_fname("./config", &path, &fname);
+      is(fname, "config", "get_path_and_fname(./config)");
+      is(path, cwd, "get_path_and_fname(./config)");
+
+      get_path_and_fname("/etc/passwd", &path, &fname);
+      is(fname, "passwd", "get_path_and_fname(/etc/passwd)");
+      is(path, "/etc/", "get_path_and_fname(/etc/passwd)");
+
+      get_path_and_fname("/tmp/../etc/passwd", &path, &fname);
+      is(fname, "passwd", "get_path_and_fname(/tmp/../etc/passwd)");
+      is(path, "/etc/", "get_path_and_fname(/tmp/../etc/passwd)");
+
+      get_path_and_fname("/etc/./passwd", &path, &fname);
+      is(fname, "passwd", "get_path_and_fname(/etc/./passwd)");
+      is(path, "/etc/", "get_path_and_fname(/etc/./passwd)");
+
+      free(path);
+      free(fname);
+   }
    return report();
 }
 #endif
index ad20fc910adf7b68d6fc2a1f3f57a8edd29e2f30..ba9df681fc92238b109fc91a1284db35ebd39824 100644 (file)
@@ -48,6 +48,7 @@ const char *assert_msg = NULL;        /* ASSERT2 error message */
 const char *version = VERSION " (" BDATE ")";
 const char *dist_name = DISTNAME " " DISTVER;
 char *exepath = (char *)NULL;
+char *sysconfigpath = (char *)NULL;
 char *exename = (char *)NULL;
 char db_engine_name[50] = {0};        /* Database engine name or type */
 char con_fname[500];                  /* Console filename */
@@ -237,7 +238,6 @@ void register_message_callback(void msg_callback(int type, char *msg))
    message_callback = msg_callback;
 }
 
-
 /*
  * Set daemon name. Also, find canonical execution
  *  path.  Note, exepath has spare room for tacking on
@@ -250,81 +250,24 @@ void register_message_callback(void msg_callback(int type, char *msg))
  */
 void my_name_is(int argc, char *argv[], const char *name)
 {
-   char *l, *p;
-   char *cpath;
-   char *cargv0;
-   int len;
-   int path_max;
-   bool respath;
-
    if (gethostname(host_name, sizeof(host_name)) != 0) {
       bstrncpy(host_name, "Hostname unknown", sizeof(host_name));
    }
    bstrncpy(my_name, name, sizeof(my_name));
 
    if (argc>0 && argv && argv[0]) {
-      /* use a dynamic PATH_MAX and allocate temporary variables */
-      path_max = pathconf(argv[0], _PC_PATH_MAX);
-      if (path_max < 4096){
-         path_max = 4096;
-      }
-      cpath = (char *)malloc(path_max);
-      cargv0 = (char *)malloc(path_max);
-
-      respath = false;
-#ifdef HAVE_REALPATH
-      /* make a canonical argv[0] */
-      if (realpath(argv[0], cargv0) != NULL){
-         respath = true;
-      }
-#endif
-      if (!respath){
-         /* no resolved_path available in cargv0, so populate it */
-         bstrncpy(cargv0, argv[0], path_max);
-      }
-      /* strip trailing filename and save exepath */
-      for (l=p=cargv0; *p; p++) {
-         if (IsPathSeparator(*p)) {
-            l = p;                       /* set pos of last path separator */
-         }
-      }
-      if (IsPathSeparator(*l)) {
-         l++;
-      } else {
-         l = cargv0;
-#if defined(HAVE_WIN32)
-         /* On Windows allow c: drive specification */
-         if (l[1] == ':') {
-            l += 2;
-         }
-#endif
-      }
-      len = strlen(l) + 1;
-      if (exename) {
-         free(exename);
-      }
-      exename = (char *)malloc(len);
-      strcpy(exename, l);
-      if (exepath) {
-         free(exepath);
-      }
-      /* separate exepath from exename */
-      *l = 0;
-      exepath = bstrdup(cargv0);
-      if (strstr(exepath, PathSeparatorUp) != NULL || strstr(exepath, PathSeparatorCur) != NULL || !IsPathSeparator(exepath[0])) {
-         /* fallback to legacy code */
-         if (getcwd(cpath, path_max)) {
-            free(exepath);
-            exepath = (char *)malloc(strlen(cpath) + 1 + len);
-            strcpy(exepath, cpath);
-         }
-      }
-      Dmsg2(500, "exepath=%s\nexename=%s\n", exepath, exename);
-      free(cpath);
-      free(cargv0);
+      get_path_and_fname(argv[0], &exepath, &exename);
    }
 }
 
+void set_sysconfig_path(const char *file)
+{
+   char *notused=NULL;
+   /* /opt/bacula/etc/bacula-fd.conf -> /opt/bacula/etc/ */
+   get_path_and_fname(file, &sysconfigpath, &notused);
+   bfree_and_null(notused);
+}
+
 /* Set special ASSERT2 message where debugger can find it */
 void
 set_assert_msg(const char *file, int line, const char *msg)
@@ -787,6 +730,10 @@ void term_msg()
       free(exename);
       exename = NULL;
    }
+   if (sysconfigpath) {
+      free(sysconfigpath);
+      sysconfigpath = NULL;
+   }
    if (trace_fd != -1) {
       close(trace_fd);
       trace_fd = -1;
index b8f96284f7b025c03949253199481020afc98d13..86fa85e23d70e49e0b4b573570fbfc291a62bd99 100644 (file)
@@ -70,6 +70,8 @@ int display_global_item(HPKT &hpkt); //
 void display_collector_types(HPKT &hpkt);
 
 /* bsys.c */
+
+void get_path_and_fname(const char *file, char **path, char **fname);
 const char *get_timezone();
 int get_user_home_directory(const char *user, POOLMEM *&home);
 int get_home_directories(const char *grpname, alist *dirs);
@@ -294,6 +296,7 @@ extern "C" {
 }
 
 /* message.c */
+void       set_sysconfig_path    (const char *file);
 void       my_name_is            (int argc, char *argv[], const char *name);
 void       init_msg              (JCR *jcr, MSGS *msg, job_code_callback_t job_code_callback = NULL);
 void       term_msg              (void);
index a337c71e257ee95ac11c4e16943acb7f9dcd174c..bea06fc72263133e20efe368f56364dd2f768e04 100644 (file)
@@ -873,7 +873,28 @@ bRC METAPLUGIN::send_parameters(bpContext *ctx, char *command)
       return bRC_Error;
    }
 #endif
-
+   const char *dir;
+   bfuncs->getBaculaValue(ctx, bVarWorkingDir, &dir);
+   Mmsg(cmd, "WorkingDir=%s\n", dir);
+   rc = backend.ctx->write_command(ctx, cmd);
+   if (rc < 0) {
+      /* error */
+      return bRC_Error;
+   }
+   bfuncs->getBaculaValue(ctx, bVarSysConfigPath, &dir);
+   Mmsg(cmd, "SysconfigPath=%s\n", dir);
+   rc = backend.ctx->write_command(ctx, cmd);
+   if (rc < 0) {
+      /* error */
+      return bRC_Error;
+   }
+   bfuncs->getBaculaValue(ctx, bVarExePath, &dir);
+   Mmsg(cmd, "ExePath=%s\n", dir);
+   rc = backend.ctx->write_command(ctx, cmd);
+   if (rc < 0) {
+      /* error */
+      return bRC_Error;
+   }
    // signal end of parameters block
    backend.ctx->signal_eod(ctx);
    /* ack Params command */