| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "../libnetdata.h" |
| 4 | |
| 5 | // ---------------------------------------------------------------------------- |
| 6 | |
| 7 | static struct { |
| 8 | DYNCFG_TYPE type; |
| 9 | const char *name; |
| 10 | } dyncfg_types[] = { |
| 11 | { .type = DYNCFG_TYPE_SINGLE, .name = "single" }, |
| 12 | { .type = DYNCFG_TYPE_TEMPLATE, .name = "template" }, |
| 13 | { .type = DYNCFG_TYPE_JOB, .name = "job" }, |
| 14 | }; |
| 15 | |
| 16 | DYNCFG_TYPE dyncfg_type2id(const char *type) { |
| 17 | if(!type || !*type) |
| 18 | return DYNCFG_TYPE_SINGLE; |
| 19 | |
| 20 | size_t entries = sizeof(dyncfg_types) / sizeof(dyncfg_types[0]); |
| 21 | for(size_t i = 0; i < entries ;i++) { |
| 22 | if(strcmp(dyncfg_types[i].name, type) == 0) |
| 23 | return dyncfg_types[i].type; |
| 24 | } |
| 25 | |
| 26 | return DYNCFG_TYPE_SINGLE; |
| 27 | } |
| 28 | |
| 29 | const char *dyncfg_id2type(DYNCFG_TYPE type) { |
| 30 | size_t entries = sizeof(dyncfg_types) / sizeof(dyncfg_types[0]); |
| 31 | for(size_t i = 0; i < entries ;i++) { |
| 32 | if(type == dyncfg_types[i].type) |
| 33 | return dyncfg_types[i].name; |
| 34 | } |
| 35 | |
| 36 | return "single"; |
| 37 | } |
| 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | static struct { |
| 42 | DYNCFG_SOURCE_TYPE source_type; |
| 43 | const char *name; |
| 44 | } dyncfg_source_types[] = { |
| 45 | { .source_type = DYNCFG_SOURCE_TYPE_INTERNAL, .name = "internal" }, |
| 46 | { .source_type = DYNCFG_SOURCE_TYPE_STOCK, .name = "stock" }, |
| 47 | { .source_type = DYNCFG_SOURCE_TYPE_USER, .name = "user" }, |
| 48 | { .source_type = DYNCFG_SOURCE_TYPE_DYNCFG, .name = "dyncfg" }, |
| 49 | { .source_type = DYNCFG_SOURCE_TYPE_DISCOVERED, .name = "discovered" }, |
| 50 | }; |
| 51 | |
| 52 | DYNCFG_SOURCE_TYPE dyncfg_source_type2id(const char *source_type) { |
| 53 | if(!source_type || !*source_type) |
| 54 | return DYNCFG_SOURCE_TYPE_INTERNAL; |
| 55 | |
| 56 | size_t entries = sizeof(dyncfg_source_types) / sizeof(dyncfg_source_types[0]); |
| 57 | for(size_t i = 0; i < entries ;i++) { |
| 58 | if(strcmp(dyncfg_source_types[i].name, source_type) == 0) |
| 59 | return dyncfg_source_types[i].source_type; |
| 60 | } |
| 61 | |
| 62 | return DYNCFG_SOURCE_TYPE_INTERNAL; |
| 63 | } |
| 64 | |
| 65 | const char *dyncfg_id2source_type(DYNCFG_SOURCE_TYPE source_type) { |
| 66 | size_t entries = sizeof(dyncfg_source_types) / sizeof(dyncfg_source_types[0]); |
| 67 | for(size_t i = 0; i < entries ;i++) { |
| 68 | if(source_type == dyncfg_source_types[i].source_type) |
| 69 | return dyncfg_source_types[i].name; |
| 70 | } |
| 71 | |
| 72 | return "internal"; |
| 73 | } |
| 74 | |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
| 77 | static struct { |
| 78 | DYNCFG_STATUS status; |
| 79 | const char *name; |
| 80 | } dyncfg_statuses[] = { |
| 81 | { .status = DYNCFG_STATUS_NONE, .name = "none" }, |
| 82 | { .status = DYNCFG_STATUS_ACCEPTED, .name = "accepted" }, |
| 83 | { .status = DYNCFG_STATUS_RUNNING, .name = "running" }, |
| 84 | { .status = DYNCFG_STATUS_FAILED, .name = "failed" }, |
| 85 | { .status = DYNCFG_STATUS_DISABLED, .name = "disabled" }, |
| 86 | { .status = DYNCFG_STATUS_ORPHAN, .name = "orphan" }, |
| 87 | { .status = DYNCFG_STATUS_INCOMPLETE, .name = "incomplete" }, |
| 88 | }; |
| 89 | |
| 90 | DYNCFG_STATUS dyncfg_status2id(const char *status) { |
| 91 | if(!status || !*status) |
| 92 | return DYNCFG_STATUS_NONE; |
| 93 | |
| 94 | size_t entries = sizeof(dyncfg_statuses) / sizeof(dyncfg_statuses[0]); |
| 95 | for(size_t i = 0; i < entries ;i++) { |
| 96 | if(strcmp(dyncfg_statuses[i].name, status) == 0) |
| 97 | return dyncfg_statuses[i].status; |
| 98 | } |
| 99 | |
| 100 | return DYNCFG_STATUS_NONE; |
| 101 | } |
| 102 | |
| 103 | const char *dyncfg_id2status(DYNCFG_STATUS status) { |
| 104 | size_t entries = sizeof(dyncfg_statuses) / sizeof(dyncfg_statuses[0]); |
| 105 | for(size_t i = 0; i < entries ;i++) { |
| 106 | if(status == dyncfg_statuses[i].status) |
| 107 | return dyncfg_statuses[i].name; |
| 108 | } |
| 109 | |
| 110 | return "none"; |
| 111 | } |
| 112 | |
| 113 | // ---------------------------------------------------------------------------- |
| 114 | |
| 115 | static struct { |
| 116 | DYNCFG_CMDS cmd; |
| 117 | const char *name; |
| 118 | } cmd_map[] = { |
| 119 | { .cmd = DYNCFG_CMD_GET, .name = "get" }, |
| 120 | { .cmd = DYNCFG_CMD_SCHEMA, .name = "schema" }, |
| 121 | { .cmd = DYNCFG_CMD_UPDATE, .name = "update" }, |
| 122 | { .cmd = DYNCFG_CMD_ADD, .name = "add" }, |
| 123 | { .cmd = DYNCFG_CMD_TEST, .name = "test" }, |
| 124 | { .cmd = DYNCFG_CMD_REMOVE, .name = "remove" }, |
| 125 | { .cmd = DYNCFG_CMD_ENABLE, .name = "enable" }, |
| 126 | { .cmd = DYNCFG_CMD_DISABLE, .name = "disable" }, |
| 127 | { .cmd = DYNCFG_CMD_RESTART, .name = "restart" }, |
| 128 | { .cmd = DYNCFG_CMD_USERCONFIG, .name = "userconfig" }, |
| 129 | }; |
| 130 | |
| 131 | const char *dyncfg_id2cmd_one(DYNCFG_CMDS cmd) { |
| 132 | for (size_t i = 0; i < sizeof(cmd_map) / sizeof(cmd_map[0]); i++) { |
| 133 | if(cmd == cmd_map[i].cmd) |
| 134 | return cmd_map[i].name; |
| 135 | } |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | DYNCFG_CMDS dyncfg_cmds2id(const char *cmds) { |
| 141 | if(!cmds || !*cmds) |
| 142 | return DYNCFG_CMD_NONE; |
| 143 | |
| 144 | DYNCFG_CMDS result = DYNCFG_CMD_NONE; |
| 145 | const char *p = cmds; |
| 146 | size_t len, i; |
| 147 | |
| 148 | while (*p) { |
| 149 | // Skip any leading spaces |
| 150 | while (*p == ' ') p++; |
| 151 | |
| 152 | // Find the end of the current word |
| 153 | const char *end = p; |
| 154 | while (*end && *end != ' ') end++; |
| 155 | len = end - p; |
| 156 | |
| 157 | // Compare with known commands |
| 158 | for (i = 0; i < sizeof(cmd_map) / sizeof(cmd_map[0]); i++) { |
| 159 | if (strncmp(p, cmd_map[i].name, len) == 0 && cmd_map[i].name[len] == '\0') { |
| 160 | result |= cmd_map[i].cmd; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Move to the next word |
| 166 | p = end; |
| 167 | } |
| 168 | |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | void dyncfg_cmds2fp(DYNCFG_CMDS cmds, FILE *fp) { |
| 173 | for (size_t i = 0; i < sizeof(cmd_map) / sizeof(cmd_map[0]); i++) { |
| 174 | if(cmds & cmd_map[i].cmd) |
| 175 | fprintf(fp, "%s ", cmd_map[i].name); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void dyncfg_cmds2json_array(DYNCFG_CMDS cmds, const char *key, BUFFER *wb) { |
| 180 | buffer_json_member_add_array(wb, key); |
| 181 | for (size_t i = 0; i < sizeof(cmd_map) / sizeof(cmd_map[0]); i++) { |
| 182 | if(cmds & cmd_map[i].cmd) |
| 183 | buffer_json_add_array_item_string(wb, cmd_map[i].name); |
| 184 | } |
| 185 | buffer_json_array_close(wb); |
| 186 | } |
| 187 | |
| 188 | void dyncfg_cmds2buffer(DYNCFG_CMDS cmds, BUFFER *wb) { |
| 189 | size_t added = 0; |
| 190 | for (size_t i = 0; i < sizeof(cmd_map) / sizeof(cmd_map[0]); i++) { |
| 191 | if(cmds & cmd_map[i].cmd) { |
| 192 | if(added) |
| 193 | buffer_fast_strcat(wb, " ", 1); |
| 194 | |
| 195 | buffer_strcat(wb, cmd_map[i].name); |
| 196 | added++; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // ---------------------------------------------------------------------------- |
| 202 | |
| 203 | bool dyncfg_is_valid_id(const char *id) { |
| 204 | const char *s = id; |
| 205 | |
| 206 | while(*s) { |
| 207 | if(isspace((uint8_t)*s) || *s == '\'') return false; |
| 208 | s++; |
| 209 | } |
| 210 | |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | static inline bool is_forbidden_filename_char(char c) { |
| 215 | if(isspace((uint8_t)c) || !isprint((uint8_t)c)) |
| 216 | return true; |
| 217 | |
| 218 | switch(c) { |
| 219 | case '`': // good not to have this in filenames |
| 220 | case '$': // good not to have this in filenames |
| 221 | case '/': // unix does not support this |
| 222 | case ':': // windows does not support this |
| 223 | case '|': // windows does not support this |
| 224 | return true; |
| 225 | |
| 226 | default: |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | char *dyncfg_escape_id_for_filename(const char *id) { |
| 232 | if (id == NULL) return NULL; |
| 233 | |
| 234 | // Allocate memory for the worst case, where every character is escaped. |
| 235 | char *escaped = mallocz(strlen(id) * 3 + 1); // Each char can become '%XX', plus '\0' |
| 236 | if (!escaped) return NULL; |
| 237 | |
| 238 | const char *src = id; |
| 239 | char *dest = escaped; |
| 240 | |
| 241 | while (*src) { |
| 242 | if (is_forbidden_filename_char(*src)) { |
| 243 | sprintf(dest, "%%%02X", (unsigned char)*src); |
| 244 | dest += 3; |
| 245 | } else { |
| 246 | *dest++ = *src; |
| 247 | } |
| 248 | src++; |
| 249 | } |
| 250 | |
| 251 | *dest = '\0'; |
| 252 | return escaped; |
| 253 | } |
| 254 | |
| 255 | // ---------------------------------------------------------------------------- |
| 256 | |
| 257 | int dyncfg_default_response(BUFFER *wb, int code, const char *msg) { |
| 258 | buffer_flush(wb); |
| 259 | wb->content_type = CT_APPLICATION_JSON; |
| 260 | wb->expires = now_realtime_sec(); |
| 261 | |
| 262 | buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_MINIFY); |
| 263 | buffer_json_member_add_uint64(wb, "status", code); |
| 264 | buffer_json_member_add_string(wb, "message", msg); |
| 265 | buffer_json_finalize(wb); |
| 266 | |
| 267 | return code; |
| 268 | } |
| 269 | |
| 270 | int dyncfg_node_find_and_call(DICTIONARY *dyncfg_nodes, const char *transaction, const char *function, |
| 271 | usec_t *stop_monotonic_ut, bool *cancelled, |
| 272 | BUFFER *payload, HTTP_ACCESS access, const char *source, BUFFER *result) { |
| 273 | if(!function || !*function) |
| 274 | return dyncfg_default_response(result, HTTP_RESP_BAD_REQUEST, "command received is empty"); |
| 275 | |
| 276 | CLEAN_CHAR_P *buf = strdupz(function); |
| 277 | |
| 278 | char *words[MAX_FUNCTION_PARAMETERS]; // an array of pointers for the words in this line |
| 279 | size_t num_words = quoted_strings_splitter_whitespace(buf, words, MAX_FUNCTION_PARAMETERS); |
| 280 | |
| 281 | const char *id = get_word(words, num_words, 1); |
| 282 | const char *action = get_word(words, num_words, 2); |
| 283 | const char *add_name = get_word(words, num_words, 3); |
| 284 | |
| 285 | if(!id || !*id) |
| 286 | return dyncfg_default_response(result, HTTP_RESP_BAD_REQUEST, "dyncfg node: id is missing from the request"); |
| 287 | |
| 288 | if(!action || !*action) |
| 289 | return dyncfg_default_response(result, HTTP_RESP_BAD_REQUEST, "dyncfg node: action is missing from the request"); |
| 290 | |
| 291 | DYNCFG_CMDS cmd = dyncfg_cmds2id(action); |
| 292 | if(cmd == DYNCFG_CMD_NONE) |
| 293 | return dyncfg_default_response(result, HTTP_RESP_BAD_REQUEST, "dyncfg node: action given in request is unknown"); |
| 294 | |
| 295 | const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(dyncfg_nodes, id); |
| 296 | if(!item) |
| 297 | return dyncfg_default_response(result, HTTP_RESP_NOT_FOUND, "dyncfg node: id is not found"); |
| 298 | |
| 299 | struct dyncfg_node *df = dictionary_acquired_item_value(item); |
| 300 | |
| 301 | buffer_flush(result); |
| 302 | result->content_type = CT_APPLICATION_JSON; |
| 303 | |
| 304 | int code = df->cb(transaction, id, cmd, add_name, payload, stop_monotonic_ut, cancelled, result, access, source, df->data); |
| 305 | |
| 306 | if(!result->expires) |
| 307 | result->expires = now_realtime_sec(); |
| 308 | |
| 309 | if(!buffer_tostring(result)) |
| 310 | dyncfg_default_response(result, code, ""); |
| 311 | |
| 312 | dictionary_acquired_item_release(dyncfg_nodes, item); |
| 313 | |
| 314 | return code; |
| 315 | } |