diff --git a/src/console/console.c b/src/console/console.c index 3d73b831b20..547539c2d9e 100644 --- a/src/console/console.c +++ b/src/console/console.c @@ -107,8 +107,6 @@ static int eolcmd(FILE *input, BSOCK *UA_sock); #endif -#define CONFIG_FILE "bconsole.conf" /* default configuration file */ - static void usage() { fprintf(stderr, _( @@ -1202,10 +1200,6 @@ int main(int argc, char *argv[]) exit(1); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - if (export_config_schema) { POOL_MEM buffer; diff --git a/src/console/console_conf.c b/src/console/console_conf.c index ad715e69ec4..abb8eb02432 100644 --- a/src/console/console_conf.c +++ b/src/console/console_conf.c @@ -296,6 +296,8 @@ void init_cons_config(CONFIG *config, const char *configfile, int exit_code) R_LAST, resources, res_head); + config->set_default_config_filename(CONFIG_FILE); + config->set_config_include_dir("bconsole.d"); } bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code) diff --git a/src/console/console_conf.h b/src/console/console_conf.h index 1ca30add412..56961153852 100644 --- a/src/console/console_conf.h +++ b/src/console/console_conf.h @@ -26,6 +26,8 @@ * Kern Sibbald, Sep MM */ +#define CONFIG_FILE "bconsole.conf" /* default configuration file */ + /* * Resource codes -- they must be sequential for indexing */ diff --git a/src/dird/dird.c b/src/dird/dird.c index 8daea7210bd..9587579bf63 100644 --- a/src/dird/dird.c +++ b/src/dird/dird.c @@ -94,8 +94,6 @@ struct resource_table_reference { static bool check_catalog(cat_op mode); -#define CONFIG_FILE "bareos-dir.conf" /* default configuration file */ - static void free_saved_resources(resource_table_reference *table) { int num = my_config->m_r_last - my_config->m_r_first + 1; @@ -317,10 +315,6 @@ int main (int argc, char *argv[]) usage(); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - /* * See if we want to drop privs. */ diff --git a/src/dird/dird_conf.c b/src/dird/dird_conf.c index 182ee070408..77e00b5b50c 100644 --- a/src/dird/dird_conf.c +++ b/src/dird/dird_conf.c @@ -4085,6 +4085,7 @@ void init_dir_config(CONFIG *config, const char *configfile, int exit_code) R_LAST, resources, res_head); + config->set_default_config_filename(CONFIG_FILE); config->set_config_include_dir("bareos-dir.d"); } diff --git a/src/dird/dird_conf.h b/src/dird/dird_conf.h index 6a8b921b993..c6be774e768 100644 --- a/src/dird/dird_conf.h +++ b/src/dird/dird_conf.h @@ -28,6 +28,8 @@ /* NOTE: #includes at the end of this file */ +#define CONFIG_FILE "bareos-dir.conf" /* default configuration file */ + /* * Resource codes -- they must be sequential for indexing */ diff --git a/src/filed/filed.c b/src/filed/filed.c index 44859de6e64..dba699138ed 100644 --- a/src/filed/filed.c +++ b/src/filed/filed.c @@ -47,8 +47,6 @@ bool backup_only_mode = false; bool restore_only_mode = false; void *start_heap; -#define CONFIG_FILE "bareos-fd.conf" /* default config file */ - char *configfile = NULL; static bool foreground = false; @@ -59,7 +57,7 @@ PROG_COPYRIGHT "\nVersion: %s (%s)\n\n" "Usage: bareos-fd [options] [-c config_file]\n" " -b backup only mode\n" -" -c use as configuration file\n" +" -c use as configuration file or directory\n" " -d set debug level to \n" " -dt print timestamp in debug output\n" " -f run in foreground (for debugging)\n" @@ -199,10 +197,6 @@ int main (int argc, char *argv[]) usage(); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - if (!uid && keep_readall_caps) { Emsg0(M_ERROR_TERM, 0, _("-k option has no meaning without -u option.\n")); } diff --git a/src/filed/filed.h b/src/filed/filed.h index 3599927283b..ba700405cd5 100644 --- a/src/filed/filed.h +++ b/src/filed/filed.h @@ -26,6 +26,9 @@ * Kern Sibbald, Jan MMI */ +#ifndef __FILED_H_ +#define __FILED_H_ + #define FILE_DAEMON 1 #include "filed_conf.h" #ifdef HAVE_WIN32 @@ -51,3 +54,5 @@ void terminate_filed(int sig); * File Daemon protocol version */ const int FD_PROTOCOL_VERSION = 54; + +#endif /* __FILED_H_ */ diff --git a/src/filed/filed_conf.c b/src/filed/filed_conf.c index e00b8a6e9ee..99a895f7144 100644 --- a/src/filed/filed_conf.c +++ b/src/filed/filed_conf.c @@ -540,6 +540,7 @@ void init_fd_config(CONFIG *config, const char *configfile, int exit_code) R_LAST, resources, res_head); + config->set_default_config_filename(CONFIG_FILE); config->set_config_include_dir("bareos-fd.d"); } diff --git a/src/filed/filed_conf.h b/src/filed/filed_conf.h index 5895c17923d..8be3a686858 100644 --- a/src/filed/filed_conf.h +++ b/src/filed/filed_conf.h @@ -26,6 +26,8 @@ * Kern Sibbald, Sep MM */ +#define CONFIG_FILE "bareos-fd.conf" /* default config file */ + /* * Resource codes -- they must be sequential for indexing */ diff --git a/src/lib/bsys.c b/src/lib/bsys.c index 7b20690674e..2792387d0dd 100644 --- a/src/lib/bsys.c +++ b/src/lib/bsys.c @@ -1005,7 +1005,9 @@ bool path_get_directory(POOL_MEM &directory, POOL_MEM &path) } if (path_is_directory(directory)) { - /* make sure, path ends with path separator */ + /* + * Make sure, path ends with path separator + */ path_append(directory, ""); return true; } @@ -1015,8 +1017,16 @@ bool path_get_directory(POOL_MEM &directory, POOL_MEM &path) bool path_append(char *path, const char *extra, unsigned int max_path) { - int path_len = strlen(path); - if ((path_len + 1 + strlen(extra)) > max_path) { + int path_len; + int required_length; + + if (!path || !extra) { + return true; + } + + path_len = strlen(path); + required_length = path_len + 1 + strlen(extra); + if (required_length > max_path) { return false; } @@ -1029,15 +1039,23 @@ bool path_append(char *path, const char *extra, unsigned int max_path) } memcpy(path + path_len, extra, strlen(extra) + 1); + return true; } bool path_append(POOL_MEM &path, const char *extra) { - int required_length = path.strlen() + 1 + strlen(extra); + int required_length; + + if (!extra) { + return true; + } + + required_length = path.strlen() + 1 + strlen(extra); if (!path.check_size(required_length)) { return false; } + return path_append(path.c_str(), extra, required_length); } diff --git a/src/lib/parse_conf.c b/src/lib/parse_conf.c index e52f7d3cbde..47483c98312 100644 --- a/src/lib/parse_conf.c +++ b/src/lib/parse_conf.c @@ -120,6 +120,11 @@ void CONFIG::init(const char *cf, m_res_head = res_head; } +void CONFIG::set_default_config_filename(const char *filename) +{ + m_config_default_filename = bstrdup(filename); +} + void CONFIG::set_config_include_dir(const char* rel_path) { m_config_include_dir = bstrdup(rel_path); @@ -138,8 +143,8 @@ bool CONFIG::parse_config() } first = false; - if (!find_config_file(config_path)) { - Jmsg0(NULL, M_ABORT, 0, _("Failed to find config filename.\n")); + if (!find_config_path(config_path)) { + Jmsg0(NULL, M_ERROR_TERM, 0, _("Failed to find config filename.\n")); } Dmsg1(100, "config file = %s\n", config_path.c_str()); return parse_config_file(config_path.c_str(), NULL, m_scan_error, m_scan_warning, m_err_type); @@ -434,43 +439,119 @@ static inline void set_env(const char *key, const char *value) } #endif +bool CONFIG::get_config_file(POOL_MEM &full_path, const char *config_dir, const char *config_filename) +{ + bool found = false; + + if (!path_is_directory(config_dir)) { + return false; + } + + if (config_filename) { + full_path.strcpy(config_dir); + if (path_append(full_path, config_filename)) { + if (path_exists(full_path)) { + m_config_dir = bstrdup(config_dir); + found = true; + } + } + } + + return found; +} + +bool CONFIG::get_config_include_path(POOL_MEM &full_path, const char *config_dir) +{ + bool found = false; + + if (m_config_include_dir) { + /* + * Set full_path to the initial part of the include path, + * so it can be used as result, even on errors. + * On success, full_path will be overwritten with the full path. + */ + full_path.strcpy(config_dir); + path_append(full_path, m_config_include_dir); + if (path_is_directory(full_path)) { + m_config_dir = bstrdup(config_dir); + /* + * Set full_path to wildcard path. + */ + if (get_path_of_resource(full_path, NULL, NULL, NULL, true)) { + m_use_config_include_dir = true; + found = true; + } + } + } + + return found; +} + /* * Returns false on error * true on OK, with full_path set to where config file should be */ -bool CONFIG::find_config_file(POOL_MEM &full_path) +bool CONFIG::find_config_path(POOL_MEM &full_path) { bool found = false; POOL_MEM config_dir; + POOL_MEM config_path_file; - /* - * If a full path specified, use it - */ - if (path_is_absolute(m_cf)) { - full_path.strcpy(m_cf); - path_get_directory(config_dir, full_path); - set_env("BAREOS_CFGDIR", config_dir.c_str()); - found = true; - } else { + if (!m_cf) { /* - * config_file is default file name, now find default directory. + * No path is given, so use the defaults. */ - config_dir.strcpy(get_default_configdir()); - full_path.strcpy(config_dir); - - if (path_append(full_path, m_cf)) { - if((!path_exists(full_path)) && (m_config_include_dir)) { - /* - * Default configdir plus config file name is not accessable. - * Use include directory structure instead. - */ - if (get_path_of_resource(full_path, NULL, NULL, NULL, true)) { - m_use_config_include_dir = true; - } + found = get_config_file(full_path, get_default_configdir(), m_config_default_filename); + if (!found) { + config_path_file.strcpy(full_path); + found = get_config_include_path(full_path, get_default_configdir()); + } + if (!found) { + Jmsg2(NULL, M_ERROR, 0, + _("Failed to read config file at the default locations " + "\"%s\" (config file path) and \"%s\" (config include directory).\n"), + config_path_file.c_str(), full_path.c_str()); + } + } else if (path_exists(m_cf)) { + /* + * Path is given and exists. + */ + if (path_is_directory(m_cf)) { + found = get_config_file(full_path, m_cf, m_config_default_filename); + if (!found) { + config_path_file.strcpy(full_path); + found = get_config_include_path(full_path, m_cf); + } + if (!found) { + Jmsg3(NULL, M_ERROR, 0, + _("Failed to find configuration files under directory \"%s\". " + "Did look for \"%s\" (config file path) and \"%s\" (config include directory).\n"), + m_cf, config_path_file.c_str(), full_path.c_str()); } + } else { + full_path.strcpy(m_cf); + path_get_directory(config_dir, full_path); + m_config_dir = bstrdup(config_dir.c_str()); found = true; - set_env("BAREOS_CFGDIR", config_dir.c_str()); } + } else if (!m_config_default_filename) { + /* + * Compatibility with older versions. + * If m_config_default_filename is not set, + * m_cf may contain what is expected in m_config_default_filename. + */ + found = get_config_file(full_path, get_default_configdir(), m_cf); + if (!found) { + Jmsg2(NULL, M_ERROR, 0, + _("Failed to find configuration files at \"%s\" and \"%s\".\n"), + m_cf, full_path.c_str()); + } + } else { + Jmsg1(NULL, M_ERROR, 0, _("Failed to read config file \"%s\"\n"), m_cf); + } + + if (found) { + set_env("BAREOS_CFGDIR", m_config_dir); } return found; @@ -483,7 +564,15 @@ void CONFIG::free_resources() m_res_head[i-m_r_first] = NULL; } - if (m_config_include_dir) { + if (m_config_default_filename) { + free((void *)m_config_default_filename); + } + + if (m_config_dir) { + free((void *)m_config_dir); + } + + if (m_config_include_dir) { free((void *)m_config_include_dir); } } @@ -769,7 +858,11 @@ bool CONFIG::get_path_of_resource(POOL_MEM &path, const char *component, resourcetype_lowercase.toLower(); if (!component) { - component = m_config_include_dir; + if (m_config_include_dir) { + component = m_config_include_dir; + } else { + return false; + } } if (resourcetype_lowercase.strlen() <= 0) { @@ -788,7 +881,7 @@ bool CONFIG::get_path_of_resource(POOL_MEM &path, const char *component, } } - path.strcpy(get_default_configdir()); + path.strcpy(m_config_dir); rel_path.bsprintf(m_config_include_naming_format, component, resourcetype_lowercase.c_str(), name); path_append(path, rel_path); diff --git a/src/lib/parse_conf.h b/src/lib/parse_conf.h index 04422da6601..5f656c565d1 100644 --- a/src/lib/parse_conf.h +++ b/src/lib/parse_conf.h @@ -411,6 +411,7 @@ class CONFIG { int32_t r_last, RES_TABLE *resources, RES **res_head); + void set_default_config_filename(const char *filename); void set_config_include_dir(const char *rel_path); bool is_using_config_include_dir() { return m_use_config_include_dir; }; bool parse_config(); @@ -432,13 +433,17 @@ class CONFIG { const char *resourcetype, const char *name, bool error_if_exits = false); protected: + const char *m_config_default_filename; /* default config filename, that is used, if no filename is given */ + const char *m_config_dir; /* base directory of configuration files */ const char *m_config_include_dir; /* rel. path to the config include directory (bareos-dir.d, bareos-sd.d, bareos-fd.d, ...) */ bool m_use_config_include_dir; /* Use the config include directory */ const char *m_config_include_naming_format; /* Format string for file paths of resources */ const char *get_default_configdir(); - bool find_config_file(POOL_MEM &full_path); + bool get_config_file(POOL_MEM &full_path, const char *config_dir, const char *config_filename); + bool get_config_include_path(POOL_MEM &full_path, const char *config_dir); + bool find_config_path(POOL_MEM &full_path); }; CONFIG *new_config_parser(); diff --git a/src/stored/bcopy.c b/src/stored/bcopy.c index d00d9d3b0ee..4a64dd450fc 100644 --- a/src/stored/bcopy.c +++ b/src/stored/bcopy.c @@ -51,8 +51,6 @@ static uint32_t jobs = 0; static DEV_BLOCK *out_block; static SESSION_LABEL sessrec; -#define CONFIG_FILE "bareos-sd.conf" - static void usage() { fprintf(stderr, _( @@ -164,10 +162,6 @@ int main (int argc, char *argv[]) working_directory = wd; - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - my_config = new_config_parser(); parse_sd_config(my_config, configfile, M_ERROR_TERM); diff --git a/src/stored/bextract.c b/src/stored/bextract.c index 25c29184d47..e06dea466e0 100644 --- a/src/stored/bextract.c +++ b/src/stored/bextract.c @@ -62,8 +62,6 @@ static char *wbuf; /* write buffer address */ static uint32_t wsize; /* write size */ static uint64_t fileAddr = 0; /* file write address */ -#define CONFIG_FILE "bareos-sd.conf" - static void usage() { fprintf(stderr, _( @@ -196,10 +194,6 @@ int main (int argc, char *argv[]) usage(); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - my_config = new_config_parser(); parse_sd_config(my_config, configfile, M_ERROR_TERM); diff --git a/src/stored/bls.c b/src/stored/bls.c index 242bd05dd35..67e43dff333 100644 --- a/src/stored/bls.c +++ b/src/stored/bls.c @@ -52,8 +52,6 @@ static SESSION_LABEL sessrec; static uint32_t num_files = 0; static ATTR *attr; -#define CONFIG_FILE "bareos-sd.conf" - static FF_PKT *ff; static BSR *bsr = NULL; @@ -207,10 +205,6 @@ int main (int argc, char *argv[]) usage(); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - my_config = new_config_parser(); parse_sd_config(my_config, configfile, M_ERROR_TERM); diff --git a/src/stored/bscan.c b/src/stored/bscan.c index c57a3a0a8a6..4e074771156 100644 --- a/src/stored/bscan.c +++ b/src/stored/bscan.c @@ -96,8 +96,6 @@ static int num_media = 0; static int num_files = 0; static int num_restoreobjects = 0; -#define CONFIG_FILE "bareos-sd.conf" - static void usage() { fprintf(stderr, _( @@ -106,7 +104,7 @@ PROG_COPYRIGHT "Usage: bscan [ options ] \n" " -B specify the database driver name (default NULL) \n" " -b specify a bootstrap file\n" -" -c specify storage daemon configuration file (default: %s)\n" +" -c specify storage daemon configuration file\n" " -d set debug level to \n" " -dt print timestamp in debug output\n" " -m update media info in database\n" @@ -127,7 +125,7 @@ PROG_COPYRIGHT " -w specify working directory (default from configuration file)\n" " -? print this message\n\n" "example:\n" -"bscan -B postgresql -V Full-0001 FileStorage\n"), 2001, VERSION, BDATE, CONFIG_FILE, backend_directory); +"bscan -B postgresql -V Full-0001 FileStorage\n"), 2001, VERSION, BDATE, backend_directory); exit(1); } @@ -259,10 +257,6 @@ int main (int argc, char *argv[]) usage(); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - my_config = new_config_parser(); parse_sd_config(my_config, configfile, M_ERROR_TERM); diff --git a/src/stored/btape.c b/src/stored/btape.c index c09c4d8a3a7..5a26a1882e6 100644 --- a/src/stored/btape.c +++ b/src/stored/btape.c @@ -85,7 +85,6 @@ static bool do_unfill(); /* Static variables */ -#define CONFIG_FILE "bareos-sd.conf" #define MAX_CMD_ARGS 30 static POOLMEM *cmd; @@ -253,10 +252,6 @@ int main(int margc, char *margv[]) init_signals(terminate_btape); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - daemon_start_time = time(NULL); my_config = new_config_parser(); diff --git a/src/stored/stored.c b/src/stored/stored.c index f71c1896958..f140e6f22c8 100644 --- a/src/stored/stored.c +++ b/src/stored/stored.c @@ -49,8 +49,6 @@ static void cleanup_old_files(); extern "C" void *device_initialization(void *arg); -#define CONFIG_FILE "bareos-sd.conf" /* Default config file */ - /* Global variables exported */ char OK_msg[] = "3000 OK\n"; char TERM_msg[] = "3999 Terminate\n"; @@ -214,10 +212,6 @@ int main (int argc, char *argv[]) usage(); } - if (configfile == NULL) { - configfile = bstrdup(CONFIG_FILE); - } - /* * See if we want to drop privs. */ diff --git a/src/stored/stored_conf.c b/src/stored/stored_conf.c index 8d07ed4c760..1eb8d4a65cd 100644 --- a/src/stored/stored_conf.c +++ b/src/stored/stored_conf.c @@ -845,6 +845,7 @@ void init_sd_config(CONFIG *config, const char *configfile, int exit_code) R_LAST, resources, res_head); + config->set_default_config_filename(CONFIG_FILE); config->set_config_include_dir("bareos-sd.d"); } diff --git a/src/stored/stored_conf.h b/src/stored/stored_conf.h index e4d40d6cc20..e371230f812 100644 --- a/src/stored/stored_conf.h +++ b/src/stored/stored_conf.h @@ -21,6 +21,8 @@ 02110-1301, USA. */ +#define CONFIG_FILE "bareos-sd.conf" /* Default config file */ + /* * Resource codes -- they must be sequential for indexing */