Merge tag 'samba-4.19.1' into v4-19-stable
[Samba.git] / ctdb / database / database_conf.c
blob4c7cb2d9ffe55f8cf0f378e3e92e0a34e61dd6f2
1 /*
2 CTDB database config handling
4 Copyright (C) Martin Schwenke 2018
6 database_conf_validate_lock_debug_script() based on
7 event_conf_validatye_debug_script():
9 Copyright (C) Amitay Isaacs 2018
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "replace.h"
26 #include "system/filesys.h"
27 #include "system/dir.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/samba_util.h"
32 #include "common/conf.h"
33 #include "common/path.h"
35 #include "database_conf.h"
37 #define DATABASE_CONF_VOLATILE_DB_DIR_DEFAULT CTDB_VARDIR "/volatile"
38 #define DATABASE_CONF_PERSISTENT_DB_DIR_DEFAULT CTDB_VARDIR "/persistent"
39 #define DATABASE_CONF_STATE_DB_DIR_DEFAULT CTDB_VARDIR "/state"
41 static bool check_static_string_change(const char *key,
42 const char *old_value,
43 const char *new_value,
44 enum conf_update_mode mode)
46 if (mode == CONF_MODE_RELOAD) {
47 if (strcmp(old_value, new_value) != 0) {
48 D_WARNING("Ignoring update of [%s] -> %s\n",
49 DATABASE_CONF_SECTION,
50 key);
54 return true;
57 static bool check_static_boolean_change(const char *key,
58 bool old_value,
59 bool new_value,
60 enum conf_update_mode mode)
62 if (mode == CONF_MODE_RELOAD || CONF_MODE_API) {
63 if (old_value != new_value) {
64 D_WARNING("Ignoring update of [%s] -> %s\n",
65 DATABASE_CONF_SECTION,
66 key);
70 return true;
73 static bool database_conf_validate_lock_debug_script(const char *key,
74 const char *old_script,
75 const char *new_script,
76 enum conf_update_mode mode)
78 char script[PATH_MAX];
79 char script_path[PATH_MAX];
80 struct stat st;
81 size_t len;
82 int ret;
84 if (new_script == NULL) {
85 return true;
88 len = strlcpy(script, new_script, sizeof(script));
89 if (len >= sizeof(script)) {
90 D_ERR("lock debug script name too long\n");
91 return false;
94 ret = snprintf(script_path,
95 sizeof(script_path),
96 "%s/%s",
97 path_etcdir(),
98 basename(script));
99 if (ret < 0 || (size_t)ret >= sizeof(script_path)) {
100 D_ERR("lock debug script path too long\n");
101 return false;
104 ret = stat(script_path, &st);
105 if (ret == -1) {
106 D_ERR("lock debug script %s does not exist\n", script_path);
107 return false;
110 if (! S_ISREG(st.st_mode)) {
111 D_ERR("lock debug script %s is not a file\n", script_path);
112 return false;
114 if (! (st.st_mode & S_IXUSR)) {
115 D_ERR("lock debug script %s is not executable\n", script_path);
116 return false;
119 return true;
122 static bool database_conf_validate_db_dir(const char *key,
123 const char *old_dir,
124 const char *new_dir,
125 enum conf_update_mode mode)
127 if (! directory_exist(new_dir)) {
128 D_ERR("%s \"%s\" does not exist\n", key, new_dir);
129 return false;
132 /* This sometimes warns but always returns true */
133 return check_static_string_change(key, old_dir, new_dir, mode);
136 void database_conf_init(struct conf_context *conf)
138 conf_define_section(conf, DATABASE_CONF_SECTION, NULL);
140 conf_define_string(conf,
141 DATABASE_CONF_SECTION,
142 DATABASE_CONF_VOLATILE_DB_DIR,
143 DATABASE_CONF_VOLATILE_DB_DIR_DEFAULT,
144 database_conf_validate_db_dir);
145 conf_define_string(conf,
146 DATABASE_CONF_SECTION,
147 DATABASE_CONF_PERSISTENT_DB_DIR,
148 DATABASE_CONF_PERSISTENT_DB_DIR_DEFAULT,
149 database_conf_validate_db_dir);
150 conf_define_string(conf,
151 DATABASE_CONF_SECTION,
152 DATABASE_CONF_STATE_DB_DIR,
153 DATABASE_CONF_STATE_DB_DIR_DEFAULT,
154 database_conf_validate_db_dir);
155 conf_define_string(conf,
156 DATABASE_CONF_SECTION,
157 DATABASE_CONF_LOCK_DEBUG_SCRIPT,
158 NULL,
159 database_conf_validate_lock_debug_script);
160 conf_define_boolean(conf,
161 DATABASE_CONF_SECTION,
162 DATABASE_CONF_TDB_MUTEXES,
163 true,
164 check_static_boolean_change);