From: Eric Bollengier Date: Tue, 4 Oct 2022 14:24:00 +0000 (+0200) Subject: Add bVarSysConfigPath for plugins X-Git-Tag: Beta-15.0.0~425 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=039f9f3e9a43529afc06d0c5dd37a2e8e2e85275;p=thirdparty%2Fbacula.git Add bVarSysConfigPath for plugins --- diff --git a/bacula/src/filed/fd_plugins.c b/bacula/src/filed/fd_plugins.c index d9d36cf0d..37232fe0c 100644 --- a/bacula/src/filed/fd_plugins.c +++ b/bacula/src/filed/fd_plugins.c @@ -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; diff --git a/bacula/src/filed/fd_plugins.h b/bacula/src/filed/fd_plugins.h index b43d1f92e..d4a20bf32 100644 --- a/bacula/src/filed/fd_plugins.h +++ b/bacula/src/filed/fd_plugins.h @@ -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 */ diff --git a/bacula/src/filed/filed.c b/bacula/src/filed/filed.c index f7957e133..6985d2508 100644 --- a/bacula/src/filed/filed.c +++ b/bacula/src/filed/filed.c @@ -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")); diff --git a/bacula/src/lib/bsys.c b/bacula/src/lib/bsys.c index 47768d6b4..1f01a0d9a 100644 --- a/bacula/src/lib/bsys.c +++ b/bacula/src/lib/bsys.c @@ -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 diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index ad20fc910..ba9df681f 100644 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -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, ¬used); + 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; diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index b8f96284f..86fa85e23 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -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); diff --git a/bacula/src/plugins/fd/pluginlib/metaplugin.cpp b/bacula/src/plugins/fd/pluginlib/metaplugin.cpp index a337c71e2..bea06fc72 100644 --- a/bacula/src/plugins/fd/pluginlib/metaplugin.cpp +++ b/bacula/src/plugins/fd/pluginlib/metaplugin.cpp @@ -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 */