2012-07-04 09:27:02 +00:00
|
|
|
/*
|
2012-12-04 03:42:28 +00:00
|
|
|
Serval DNA configuration
|
|
|
|
Copyright (C) 2012 Serval Project Inc.
|
2012-07-04 09:27:02 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-11-14 02:45:13 +00:00
|
|
|
#include <sys/stat.h>
|
2012-07-04 09:27:02 +00:00
|
|
|
#include <ctype.h>
|
2012-07-29 03:05:53 +00:00
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
2012-11-20 03:13:51 +00:00
|
|
|
#include "str.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "mem.h"
|
2012-07-04 09:27:02 +00:00
|
|
|
|
2012-11-14 02:45:13 +00:00
|
|
|
#define CONFFILE_NAME "serval.conf"
|
2012-07-04 09:27:02 +00:00
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
struct file_meta {
|
|
|
|
time_t mtime;
|
|
|
|
off_t size;
|
|
|
|
};
|
2012-07-04 09:27:02 +00:00
|
|
|
|
2012-12-14 05:57:04 +00:00
|
|
|
#define FILE_META_UNKNOWN ((struct file_meta){ .mtime = -1, .size = -1 })
|
|
|
|
|
2012-12-04 06:22:49 +00:00
|
|
|
struct cf_om_node *cf_om_root = NULL;
|
2012-12-14 05:57:04 +00:00
|
|
|
static struct file_meta conffile_meta = FILE_META_UNKNOWN;
|
2012-11-15 02:08:17 +00:00
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
int cf_limbo = 1;
|
|
|
|
struct config_main config;
|
2012-12-14 05:57:04 +00:00
|
|
|
static struct file_meta config_meta = FILE_META_UNKNOWN;
|
2012-12-04 03:42:28 +00:00
|
|
|
|
|
|
|
static const char *conffile_path()
|
2012-07-04 09:27:02 +00:00
|
|
|
{
|
2012-12-04 03:42:28 +00:00
|
|
|
static char path[1024] = "";
|
|
|
|
if (!path[0] && !FORM_SERVAL_INSTANCE_PATH(path, CONFFILE_NAME))
|
|
|
|
abort();
|
|
|
|
return path;
|
2012-07-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
static int get_meta(const char *path, struct file_meta *metap)
|
2012-07-04 09:27:02 +00:00
|
|
|
{
|
2012-12-04 03:42:28 +00:00
|
|
|
struct stat st;
|
|
|
|
if (stat(path, &st) == -1) {
|
2012-07-04 09:27:02 +00:00
|
|
|
if (errno != ENOENT)
|
2012-12-04 03:42:28 +00:00
|
|
|
return WHYF_perror("stat(%s)", path);
|
2012-12-14 05:57:04 +00:00
|
|
|
// Do not return FILE_META_UNKNOWN on ENOENT, otherwise reload logic breaks. A non-existent
|
|
|
|
// file is treated as size == 0.
|
2012-12-04 03:42:28 +00:00
|
|
|
metap->size = 0;
|
|
|
|
metap->mtime = -1;
|
2012-07-04 09:27:02 +00:00
|
|
|
} else {
|
2012-12-04 03:42:28 +00:00
|
|
|
metap->size = st.st_size;
|
|
|
|
metap->mtime = st.st_mtime;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-14 05:57:04 +00:00
|
|
|
static int cmp_meta(const struct file_meta *a, const struct file_meta *b)
|
|
|
|
{
|
|
|
|
return a->mtime < b->mtime ? -1 : a->mtime > b->mtime ? 1 : a->size < b->size ? -1 : a->size > b->size ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reload(const char *path, int *resultp)
|
2012-12-04 03:42:28 +00:00
|
|
|
{
|
|
|
|
struct file_meta meta;
|
2012-12-14 05:57:04 +00:00
|
|
|
if (get_meta(conffile_path(), &meta) == -1)
|
|
|
|
return -1;
|
|
|
|
if (cmp_meta(&meta, &conffile_meta) == 0)
|
|
|
|
return 0;
|
2013-03-12 08:48:30 +00:00
|
|
|
if (conffile_meta.mtime != -1 && serverMode)
|
2012-12-14 05:57:04 +00:00
|
|
|
INFOF("config file %s -- detected new version", conffile_path());
|
2012-12-04 03:42:28 +00:00
|
|
|
char *buf = NULL;
|
2013-03-12 08:48:30 +00:00
|
|
|
if (meta.mtime == -1) {
|
2013-03-25 07:16:56 +00:00
|
|
|
WARNF("config file %s does not exist -- using all defaults", path);
|
2013-03-12 08:48:30 +00:00
|
|
|
} else if (meta.size > CONFIG_FILE_MAX_SIZE) {
|
2012-12-04 03:42:28 +00:00
|
|
|
WHYF("config file %s is too big (%ld bytes exceeds limit %ld)", path, meta.size, CONFIG_FILE_MAX_SIZE);
|
2012-12-14 05:57:04 +00:00
|
|
|
return -1;
|
2012-12-04 07:50:53 +00:00
|
|
|
} else if (meta.size <= 0) {
|
2013-03-25 07:16:56 +00:00
|
|
|
WARNF("config file %s is zero size -- using all defaults", path);
|
2012-12-04 03:42:28 +00:00
|
|
|
} else {
|
|
|
|
FILE *f = fopen(path, "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
WHYF_perror("fopen(%s)", path);
|
2012-12-14 05:57:04 +00:00
|
|
|
return -1;
|
2012-07-04 09:27:02 +00:00
|
|
|
}
|
2012-12-04 03:42:28 +00:00
|
|
|
if ((buf = emalloc(meta.size)) == NULL) {
|
2012-07-04 09:27:02 +00:00
|
|
|
fclose(f);
|
2012-12-14 05:57:04 +00:00
|
|
|
return -1;
|
2012-07-04 09:27:02 +00:00
|
|
|
}
|
2012-12-04 03:42:28 +00:00
|
|
|
if (fread(buf, meta.size, 1, f) != 1) {
|
2012-07-04 09:27:02 +00:00
|
|
|
if (ferror(f))
|
2012-12-04 03:42:28 +00:00
|
|
|
WHYF_perror("fread(%s, %llu)", path, (unsigned long long) meta.size);
|
2012-07-04 09:27:02 +00:00
|
|
|
else
|
2012-12-04 03:42:28 +00:00
|
|
|
WHYF("fread(%s, %llu) hit EOF", path, (unsigned long long) meta.size);
|
|
|
|
free(buf);
|
2012-07-04 09:27:02 +00:00
|
|
|
fclose(f);
|
2012-12-14 05:57:04 +00:00
|
|
|
return -1;
|
2012-07-04 09:27:02 +00:00
|
|
|
}
|
2012-12-04 03:42:28 +00:00
|
|
|
if (fclose(f) == EOF) {
|
2012-12-14 05:57:04 +00:00
|
|
|
WHYF_perror("fclose(%s)", path);
|
2012-12-04 03:42:28 +00:00
|
|
|
free(buf);
|
2012-12-14 05:57:04 +00:00
|
|
|
return -1;
|
2012-07-04 09:27:02 +00:00
|
|
|
}
|
2013-03-12 08:48:30 +00:00
|
|
|
if (serverMode)
|
|
|
|
INFOF("config file %s successfully read %ld bytes", path, (long) meta.size);
|
2012-12-04 03:42:28 +00:00
|
|
|
}
|
2012-12-14 05:57:04 +00:00
|
|
|
conffile_meta = meta;
|
2012-12-04 03:42:28 +00:00
|
|
|
struct cf_om_node *new_root = NULL;
|
2012-12-14 05:57:04 +00:00
|
|
|
*resultp = cf_om_parse(path, buf, meta.size, &new_root);
|
2012-12-04 03:42:28 +00:00
|
|
|
free(buf);
|
2012-12-14 05:57:04 +00:00
|
|
|
if (*resultp == CFERROR)
|
|
|
|
return -1;
|
|
|
|
cf_om_free_node(&cf_om_root);
|
|
|
|
cf_om_root = new_root;
|
|
|
|
return 1;
|
2012-07-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 05:57:04 +00:00
|
|
|
int cf_om_reload()
|
2012-07-05 05:29:50 +00:00
|
|
|
{
|
2012-12-14 05:57:04 +00:00
|
|
|
int result;
|
|
|
|
return reload(conffile_path(), &result);
|
2012-11-15 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 06:22:49 +00:00
|
|
|
int cf_om_load()
|
2012-11-15 02:08:17 +00:00
|
|
|
{
|
2012-12-14 05:57:04 +00:00
|
|
|
conffile_meta = FILE_META_UNKNOWN;
|
|
|
|
return cf_om_reload();
|
2012-11-15 02:08:17 +00:00
|
|
|
}
|
2012-07-04 09:27:02 +00:00
|
|
|
|
2012-12-04 06:22:49 +00:00
|
|
|
int cf_om_save()
|
2012-11-15 02:08:17 +00:00
|
|
|
{
|
2012-12-04 03:42:28 +00:00
|
|
|
if (cf_om_root) {
|
|
|
|
const char *path = conffile_path();
|
2012-07-04 09:27:02 +00:00
|
|
|
char tempfile[1024];
|
|
|
|
FILE *outf = NULL;
|
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(tempfile, "serval.conf.temp"))
|
|
|
|
return -1;
|
|
|
|
if ((outf = fopen(tempfile, "w")) == NULL)
|
|
|
|
return WHYF_perror("fopen(%s, \"w\")", tempfile);
|
2012-12-04 03:42:28 +00:00
|
|
|
struct cf_om_iterator it;
|
2012-12-04 06:22:49 +00:00
|
|
|
for (cf_om_iter_start(&it, cf_om_root); it.node; cf_om_iter_next(&it))
|
2012-12-04 03:42:28 +00:00
|
|
|
if (it.node->text)
|
|
|
|
fprintf(outf, "%s=%s\n", it.node->fullkey, it.node->text);
|
2012-07-04 09:27:02 +00:00
|
|
|
if (fclose(outf) == EOF)
|
|
|
|
return WHYF_perror("fclose(%s)", tempfile);
|
2012-12-14 05:57:04 +00:00
|
|
|
// rename(2) is atomic, so no other process will read a half-written file.
|
2012-12-04 03:42:28 +00:00
|
|
|
if (rename(tempfile, path)) {
|
|
|
|
WHYF_perror("rename(%s, %s)", tempfile, path);
|
2012-07-04 09:27:02 +00:00
|
|
|
unlink(tempfile);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-04 03:42:28 +00:00
|
|
|
struct file_meta meta;
|
|
|
|
if (get_meta(path, &meta) == -1)
|
|
|
|
return -1;
|
|
|
|
INFOF("successfully wrote %s", path);
|
|
|
|
conffile_meta = meta;
|
2012-07-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-29 03:05:53 +00:00
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
int cf_init()
|
|
|
|
{
|
|
|
|
cf_limbo = 1;
|
2012-12-14 05:57:04 +00:00
|
|
|
conffile_meta = config_meta = FILE_META_UNKNOWN;
|
|
|
|
memset(&config, 0, sizeof config);
|
2012-12-04 06:22:49 +00:00
|
|
|
if (cf_dfl_config_main(&config) == CFERROR)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2012-12-04 03:42:28 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static int reload_and_parse(int permissive, int strict)
|
2012-12-04 03:42:28 +00:00
|
|
|
{
|
|
|
|
int result = CFOK;
|
|
|
|
if (cf_limbo)
|
|
|
|
result = cf_dfl_config_main(&config);
|
2012-12-14 05:57:04 +00:00
|
|
|
if (result == CFOK || result == CFEMPTY) {
|
|
|
|
if (reload(conffile_path(), &result) == -1)
|
|
|
|
result = CFERROR;
|
|
|
|
else if (!cf_limbo && cmp_meta(&conffile_meta, &config_meta) == 0)
|
|
|
|
return 0;
|
|
|
|
else {
|
|
|
|
config_meta = conffile_meta;
|
|
|
|
if (result == CFOK || result == CFEMPTY) {
|
|
|
|
struct config_main new_config;
|
|
|
|
memset(&new_config, 0, sizeof new_config);
|
|
|
|
result = cf_dfl_config_main(&new_config);
|
2012-12-04 03:42:28 +00:00
|
|
|
if (result == CFOK || result == CFEMPTY) {
|
2012-12-14 05:57:04 +00:00
|
|
|
result = cf_om_root ? cf_opt_config_main(&new_config, cf_om_root) : CFEMPTY;
|
|
|
|
if (result == CFOK || result == CFEMPTY) {
|
|
|
|
result = CFOK;
|
|
|
|
config = new_config;
|
2013-03-28 13:52:08 +00:00
|
|
|
} else if (result != CFERROR && !strict) {
|
|
|
|
result &= ~CFEMPTY; // don't log "empty" as a problem
|
2012-12-14 05:57:04 +00:00
|
|
|
config = new_config;
|
|
|
|
}
|
2012-12-04 03:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-28 13:52:08 +00:00
|
|
|
int ret = 1;
|
|
|
|
if (result == CFOK) {
|
|
|
|
logConfigChanged();
|
|
|
|
} else {
|
2012-12-14 05:57:04 +00:00
|
|
|
strbuf b = strbuf_alloca(180);
|
|
|
|
strbuf_cf_flag_reason(b, result);
|
2013-03-28 13:52:08 +00:00
|
|
|
if (strict)
|
|
|
|
ret = WHYF("defective config file %s not loaded -- %s", conffile_path(), strbuf_str(b));
|
|
|
|
else {
|
|
|
|
if (!permissive)
|
|
|
|
ret = WHYF("config file %s loaded despite defects -- %s", conffile_path(), strbuf_str(b));
|
|
|
|
else
|
|
|
|
WARNF("config file %s loaded despite defects -- %s", conffile_path(), strbuf_str(b));
|
|
|
|
logConfigChanged();
|
|
|
|
}
|
2012-12-04 03:42:28 +00:00
|
|
|
}
|
2013-03-28 13:52:08 +00:00
|
|
|
cf_limbo = 0; // let log messages out
|
|
|
|
logFlush();
|
|
|
|
return ret;
|
2012-12-04 06:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cf_load()
|
|
|
|
{
|
2012-12-14 05:57:04 +00:00
|
|
|
conffile_meta = config_meta = FILE_META_UNKNOWN;
|
2013-03-28 13:52:08 +00:00
|
|
|
return reload_and_parse(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cf_load_strict()
|
|
|
|
{
|
|
|
|
conffile_meta = config_meta = FILE_META_UNKNOWN;
|
|
|
|
return reload_and_parse(0, 1);
|
2012-12-04 06:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cf_load_permissive()
|
|
|
|
{
|
2012-12-14 05:57:04 +00:00
|
|
|
conffile_meta = config_meta = FILE_META_UNKNOWN;
|
2013-03-28 13:52:08 +00:00
|
|
|
return reload_and_parse(1, 0);
|
2012-12-04 06:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cf_reload()
|
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
return reload_and_parse(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cf_reload_strict()
|
|
|
|
{
|
|
|
|
return reload_and_parse(0, 1);
|
2012-12-04 06:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cf_reload_permissive()
|
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
return reload_and_parse(1, 0);
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|