@cryptotaxi247 / netdata-1 / commits / 72b72c20a

dyncfg files on disk do not contain colons (#17694)

* dyncfg files on disk do not contain colons * renamed files

Costa Tsaousis committed May 17, 2024 at 16:57 UTC 72b72c20aa78a049ff64100f8ae9e3891cfd99eb
5 files changed +25 -15
CMakeLists.txt
+2 -2
@@ -2848,8 +2848,8 @@ install(PROGRAMS
2848
2849 # confs
2850 install(FILES
2851 - src/collectors/systemd-journal.plugin/schema.d/systemd-journal:monitored-directories.json
2852 - src/health/schema.d/health:alert:prototype.json
2851 + src/collectors/systemd-journal.plugin/schema.d/systemd-journal%3Amonitored-directories.json
2852 + src/health/schema.d/health%3Aalert%3Aprototype.json
2853 COMPONENT netdata
2854 DESTINATION usr/lib/netdata/conf.d/schema.d)
2855
src/collectors/systemd-journal.plugin/schema.d/systemd-journal%3Amonitored-directories.json renamed
src/daemon/config/dyncfg-files.c
+18 -6
@@ -61,7 +61,10 @@ void dyncfg_file_save(const char *id, DYNCFG *df) {
61 fclose(fp);
62 }
63
64 -void dyncfg_file_load(const char *filename) {
64 +void dyncfg_file_load(const char *d_name) {
65 + char filename[PATH_MAX];
66 + snprintf(filename, sizeof(filename), "%s/%s", dyncfg_globals.dir, d_name);
67 +
68 FILE *fp = fopen(filename, "r");
69 if (!fp) {
70 nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: cannot open file '%s'", filename);
@@ -176,6 +179,18 @@ void dyncfg_file_load(const char *filename) {
179 dyncfg_set_current_from_dyncfg(&tmp);
180
181 dictionary_set(dyncfg_globals.nodes, id, &tmp, sizeof(tmp));
182 +
183 + // check if we need to rename the file
184 + CLEAN_CHAR_P *fixed_id = dyncfg_escape_id_for_filename(id);
185 + char fixed_filename[PATH_MAX];
186 + snprintf(fixed_filename, sizeof(fixed_filename), "%s/%s.dyncfg", dyncfg_globals.dir, fixed_id);
187 +
188 + if(strcmp(filename, fixed_filename) != 0) {
189 + if(rename(filename, fixed_filename) != 0)
190 + nd_log(NDLS_DAEMON, NDLP_ERR,
191 + "DYNCFG: cannot rename file '%s' into '%s'. Saving a new configuraton may not overwrite the old one.",
192 + filename, fixed_filename);
193 + }
194 }
195
196 void dyncfg_load_all(void) {
@@ -186,12 +201,9 @@ void dyncfg_load_all(void) {
201 }
202
203 struct dirent *entry;
189 - char filepath[PATH_MAX];
204 while ((entry = readdir(dir)) != NULL) {
191 - if ((entry->d_type == DT_REG || entry->d_type == DT_LNK) && strendswith(entry->d_name, ".dyncfg")) {
192 - snprintf(filepath, sizeof(filepath), "%s/%s", dyncfg_globals.dir, entry->d_name);
193 - dyncfg_file_load(filepath);
194 - }
205 + if ((entry->d_type == DT_REG || entry->d_type == DT_LNK) && strendswith(entry->d_name, ".dyncfg"))
206 + dyncfg_file_load(entry->d_name);
207 }
208
209 closedir(dir);
src/health/schema.d/health%3Aalert%3Aprototype.json renamed
src/libnetdata/config/dyncfg.c
+5 -7
@@ -215,15 +215,13 @@ static inline bool is_forbidden_char(char c) {
215 return true;
216
217 switch(c) {
218 - case '/':
218 + case '`': // good not to have this in filenames
219 + case '$': // good not to have this in filenames
220 + case '/': // unix does not support this
221 + case ':': // windows does not support this
222 + case '|': // windows does not support this
223 return true;
224
221 -#ifdef COMPILED_FOR_WINDOWS
222 - case ':':
223 - case '|':
224 - return true;
225 -#endif
226 -
225 default:
226 return false;
227 }