Skip to content

Commit

Permalink
Improved config file handling.
Browse files Browse the repository at this point in the history
The old implementation could not distinguish between a config file given
as command line parameter and the compiled default.

Older versions of bareos (15.2.x) did handle command line parameter
as follows:

If the parameter is a path (contains a "/"), it will be as expected (absolute path
or path relative to the current directory).

If the parameter is a filename (does not contain a "/"), it will be treated as relative
to the compiled config directory (e.g. /etc/bareos/).

This is inconsistent.

A prior patch changed this behavior. However, this patch extends the
search for the config file.

No config file paramter given:
  * use default config file, fall back to default config include
    directories.
Config file parameter is given and it is an existing file:
  * this file will be used. It can be specified either an absolute or a
    relative path (relative to the current directory).
Config file parameter is given and it is a directory:
  * tries to find the default config filename in the configured
    directory. If that fails, it tries to find the config include
    directories under the configured directory.
Parameter is given but does not exists:
  * For backward compability (for programs not adapted to the new
    mechanism like bat and tray-monitor):
    * try to find the given path relative to the default config directory.
  • Loading branch information
joergsteffens authored and Marco van Wieringen committed May 20, 2016
1 parent 9d6c3e6 commit 9555e7c
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 90 deletions.
6 changes: 0 additions & 6 deletions src/console/console.c
Expand Up @@ -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, _(
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/console/console_conf.c
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/console/console_conf.h
Expand Up @@ -26,6 +26,8 @@
* Kern Sibbald, Sep MM
*/

#define CONFIG_FILE "bconsole.conf" /* default configuration file */

/*
* Resource codes -- they must be sequential for indexing
*/
Expand Down
6 changes: 0 additions & 6 deletions src/dird/dird.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions src/dird/dird_conf.c
Expand Up @@ -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");
}

Expand Down
2 changes: 2 additions & 0 deletions src/dird/dird_conf.h
Expand Up @@ -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
*/
Expand Down
8 changes: 1 addition & 7 deletions src/filed/filed.c
Expand Up @@ -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;

Expand All @@ -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 <file> use <file> as configuration file\n"
" -c <path> use <path> as configuration file or directory\n"
" -d <nn> set debug level to <nn>\n"
" -dt print timestamp in debug output\n"
" -f run in foreground (for debugging)\n"
Expand Down Expand Up @@ -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"));
}
Expand Down
5 changes: 5 additions & 0 deletions src/filed/filed.h
Expand Up @@ -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
Expand All @@ -51,3 +54,5 @@ void terminate_filed(int sig);
* File Daemon protocol version
*/
const int FD_PROTOCOL_VERSION = 54;

#endif /* __FILED_H_ */
1 change: 1 addition & 0 deletions src/filed/filed_conf.c
Expand Up @@ -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");
}

Expand Down
2 changes: 2 additions & 0 deletions src/filed/filed_conf.h
Expand Up @@ -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
*/
Expand Down
26 changes: 22 additions & 4 deletions src/lib/bsys.c
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand Down
151 changes: 122 additions & 29 deletions src/lib/parse_conf.c
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);

Expand Down

0 comments on commit 9555e7c

Please sign in to comment.