You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dynamic_cfg.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamd.h"
  18. #include "libserver/maps/map.h"
  19. #include "scan_result.h"
  20. #include "dynamic_cfg.h"
  21. #include "unix-std.h"
  22. #include "lua/lua_common.h"
  23. #include <math.h>
  24. struct config_json_buf {
  25. GString *buf;
  26. struct rspamd_config *cfg;
  27. };
  28. /**
  29. * Apply configuration to the specified configuration
  30. * @param conf_metrics
  31. * @param cfg
  32. */
  33. static void
  34. apply_dynamic_conf (const ucl_object_t *top, struct rspamd_config *cfg)
  35. {
  36. gint test_act;
  37. const ucl_object_t *cur_elt, *cur_nm, *it_val;
  38. ucl_object_iter_t it = NULL;
  39. const gchar *name;
  40. gdouble nscore;
  41. static const guint priority = 3;
  42. while ((cur_elt = ucl_object_iterate (top, &it, true))) {
  43. if (ucl_object_type (cur_elt) != UCL_OBJECT) {
  44. msg_err ("loaded json array element is not an object");
  45. continue;
  46. }
  47. cur_nm = ucl_object_lookup (cur_elt, "metric");
  48. if (!cur_nm || ucl_object_type (cur_nm) != UCL_STRING) {
  49. msg_err (
  50. "loaded json metric object element has no 'metric' attribute");
  51. continue;
  52. }
  53. cur_nm = ucl_object_lookup (cur_elt, "symbols");
  54. /* Parse symbols */
  55. if (cur_nm && ucl_object_type (cur_nm) == UCL_ARRAY) {
  56. ucl_object_iter_t nit = NULL;
  57. while ((it_val = ucl_object_iterate (cur_nm, &nit, true))) {
  58. if (ucl_object_lookup (it_val, "name") &&
  59. ucl_object_lookup (it_val, "value")) {
  60. const ucl_object_t *n =
  61. ucl_object_lookup (it_val, "name");
  62. const ucl_object_t *v =
  63. ucl_object_lookup (it_val, "value");
  64. nscore = ucl_object_todouble (v);
  65. /*
  66. * We use priority = 3 here
  67. */
  68. rspamd_config_add_symbol (cfg,
  69. ucl_object_tostring (n), nscore, NULL, NULL,
  70. 0, priority, cfg->default_max_shots);
  71. }
  72. else {
  73. msg_info (
  74. "json symbol object has no mandatory 'name' and 'value' attributes");
  75. }
  76. }
  77. }
  78. else {
  79. ucl_object_t *arr;
  80. arr = ucl_object_typed_new (UCL_ARRAY);
  81. ucl_object_insert_key ((ucl_object_t *)cur_elt, arr, "symbols",
  82. sizeof ("symbols") - 1, false);
  83. }
  84. cur_nm = ucl_object_lookup (cur_elt, "actions");
  85. /* Parse actions */
  86. if (cur_nm && ucl_object_type (cur_nm) == UCL_ARRAY) {
  87. ucl_object_iter_t nit = NULL;
  88. while ((it_val = ucl_object_iterate (cur_nm, &nit, true))) {
  89. const ucl_object_t *n = ucl_object_lookup (it_val, "name");
  90. const ucl_object_t *v = ucl_object_lookup (it_val, "value");
  91. if (n != NULL && v != NULL) {
  92. name = ucl_object_tostring (n);
  93. if (!name || !rspamd_action_from_str (name, &test_act)) {
  94. msg_err ("unknown action: %s",
  95. ucl_object_tostring (ucl_object_lookup (it_val,
  96. "name")));
  97. continue;
  98. }
  99. if (ucl_object_type (v) == UCL_NULL) {
  100. nscore = NAN;
  101. }
  102. else {
  103. nscore = ucl_object_todouble (v);
  104. }
  105. ucl_object_t *obj_tbl = ucl_object_typed_new (UCL_OBJECT);
  106. ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (nscore),
  107. "score", 0, false);
  108. ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (priority),
  109. "priority", 0, false);
  110. rspamd_config_set_action_score (cfg, name, obj_tbl);
  111. ucl_object_unref (obj_tbl);
  112. }
  113. else {
  114. msg_info (
  115. "json action object has no mandatory 'name' and 'value' attributes");
  116. }
  117. }
  118. }
  119. else {
  120. ucl_object_t *arr;
  121. arr = ucl_object_typed_new (UCL_ARRAY);
  122. ucl_object_insert_key ((ucl_object_t *)cur_elt, arr, "actions",
  123. sizeof ("actions") - 1, false);
  124. }
  125. }
  126. }
  127. /* Callbacks for reading json dynamic rules */
  128. static gchar *
  129. json_config_read_cb (gchar * chunk,
  130. gint len,
  131. struct map_cb_data *data,
  132. gboolean final)
  133. {
  134. struct config_json_buf *jb, *pd;
  135. pd = data->prev_data;
  136. g_assert (pd != NULL);
  137. if (data->cur_data == NULL) {
  138. jb = g_malloc0 (sizeof (*jb));
  139. jb->cfg = pd->cfg;
  140. data->cur_data = jb;
  141. }
  142. else {
  143. jb = data->cur_data;
  144. }
  145. if (jb->buf == NULL) {
  146. /* Allocate memory for buffer */
  147. jb->buf = g_string_sized_new (MAX (len, BUFSIZ));
  148. }
  149. g_string_append_len (jb->buf, chunk, len);
  150. return NULL;
  151. }
  152. static void
  153. json_config_fin_cb (struct map_cb_data *data, void **target)
  154. {
  155. struct config_json_buf *jb;
  156. ucl_object_t *top;
  157. struct ucl_parser *parser;
  158. /* Now parse json */
  159. if (data->cur_data) {
  160. jb = data->cur_data;
  161. }
  162. else {
  163. return;
  164. }
  165. if (jb->buf == NULL) {
  166. msg_err ("no data read");
  167. return;
  168. }
  169. parser = ucl_parser_new (0);
  170. if (!ucl_parser_add_chunk (parser, jb->buf->str, jb->buf->len)) {
  171. msg_err ("cannot load json data: parse error %s",
  172. ucl_parser_get_error (parser));
  173. ucl_parser_free (parser);
  174. return;
  175. }
  176. top = ucl_parser_get_object (parser);
  177. ucl_parser_free (parser);
  178. if (ucl_object_type (top) != UCL_ARRAY) {
  179. ucl_object_unref (top);
  180. msg_err ("loaded json is not an array");
  181. return;
  182. }
  183. ucl_object_unref (jb->cfg->current_dynamic_conf);
  184. apply_dynamic_conf (top, jb->cfg);
  185. jb->cfg->current_dynamic_conf = top;
  186. if (target) {
  187. *target = data->cur_data;
  188. }
  189. if (data->prev_data) {
  190. jb = data->prev_data;
  191. /* Clean prev data */
  192. if (jb->buf) {
  193. g_string_free (jb->buf, TRUE);
  194. }
  195. g_free (jb);
  196. }
  197. }
  198. static void
  199. json_config_dtor_cb (struct map_cb_data *data)
  200. {
  201. struct config_json_buf *jb;
  202. if (data->cur_data) {
  203. jb = data->cur_data;
  204. /* Clean prev data */
  205. if (jb->buf) {
  206. g_string_free (jb->buf, TRUE);
  207. }
  208. if (jb->cfg && jb->cfg->current_dynamic_conf) {
  209. ucl_object_unref (jb->cfg->current_dynamic_conf);
  210. }
  211. g_free (jb);
  212. }
  213. }
  214. /**
  215. * Init dynamic configuration using map logic and specific configuration
  216. * @param cfg config file
  217. */
  218. void
  219. init_dynamic_config (struct rspamd_config *cfg)
  220. {
  221. struct config_json_buf *jb, **pjb;
  222. if (cfg->dynamic_conf == NULL) {
  223. /* No dynamic conf has been specified, so do not try to load it */
  224. return;
  225. }
  226. /* Now try to add map with json data */
  227. jb = g_malloc (sizeof (struct config_json_buf));
  228. pjb = g_malloc (sizeof (struct config_json_buf *));
  229. jb->buf = NULL;
  230. jb->cfg = cfg;
  231. *pjb = jb;
  232. cfg->current_dynamic_conf = ucl_object_typed_new (UCL_ARRAY);
  233. rspamd_mempool_add_destructor (cfg->cfg_pool,
  234. (rspamd_mempool_destruct_t)g_free,
  235. pjb);
  236. if (!rspamd_map_add (cfg,
  237. cfg->dynamic_conf,
  238. "Dynamic configuration map",
  239. json_config_read_cb,
  240. json_config_fin_cb,
  241. json_config_dtor_cb,
  242. (void **)pjb, NULL, RSPAMD_MAP_DEFAULT)) {
  243. msg_err ("cannot add map for configuration %s", cfg->dynamic_conf);
  244. }
  245. }
  246. /**
  247. * Dump dynamic configuration to the disk
  248. * @param cfg
  249. * @return
  250. */
  251. gboolean
  252. dump_dynamic_config (struct rspamd_config *cfg)
  253. {
  254. struct stat st;
  255. gchar *dir, pathbuf[PATH_MAX];
  256. gint fd;
  257. if (cfg->dynamic_conf == NULL || cfg->current_dynamic_conf == NULL) {
  258. /* No dynamic conf has been specified, so do not try to dump it */
  259. msg_err ("cannot save dynamic conf as it is not specified");
  260. return FALSE;
  261. }
  262. dir = g_path_get_dirname (cfg->dynamic_conf);
  263. if (dir == NULL) {
  264. msg_err ("invalid path: %s", cfg->dynamic_conf);
  265. return FALSE;
  266. }
  267. if (stat (cfg->dynamic_conf, &st) == -1) {
  268. msg_debug ("%s is unavailable: %s", cfg->dynamic_conf,
  269. strerror (errno));
  270. st.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  271. }
  272. if (access (dir, W_OK | R_OK) == -1) {
  273. msg_warn ("%s is inaccessible: %s", dir, strerror (errno));
  274. g_free (dir);
  275. return FALSE;
  276. }
  277. rspamd_snprintf (pathbuf,
  278. sizeof (pathbuf),
  279. "%s%crconf-XXXXXX",
  280. dir,
  281. G_DIR_SEPARATOR);
  282. g_free (dir);
  283. #ifdef HAVE_MKSTEMP
  284. /* Umask is set before */
  285. fd = mkstemp (pathbuf);
  286. #else
  287. fd = g_mkstemp_full (pathbuf, O_RDWR, S_IWUSR | S_IRUSR);
  288. #endif
  289. if (fd == -1) {
  290. msg_err ("mkstemp error: %s", strerror (errno));
  291. return FALSE;
  292. }
  293. struct ucl_emitter_functions *emitter_functions;
  294. FILE *fp;
  295. fp = fdopen (fd, "w");
  296. emitter_functions = ucl_object_emit_file_funcs (fp);
  297. if (!ucl_object_emit_full (cfg->current_dynamic_conf, UCL_EMIT_JSON,
  298. emitter_functions, NULL)) {
  299. msg_err ("cannot emit ucl object: %s", strerror (errno));
  300. ucl_object_emit_funcs_free (emitter_functions);
  301. fclose (fp);
  302. return FALSE;
  303. }
  304. (void)unlink (cfg->dynamic_conf);
  305. /* Rename old config */
  306. if (rename (pathbuf, cfg->dynamic_conf) == -1) {
  307. msg_err ("rename error: %s", strerror (errno));
  308. fclose (fp);
  309. ucl_object_emit_funcs_free (emitter_functions);
  310. unlink (pathbuf);
  311. return FALSE;
  312. }
  313. /* Set permissions */
  314. if (chmod (cfg->dynamic_conf, st.st_mode) == -1) {
  315. msg_warn ("chmod failed: %s", strerror (errno));
  316. }
  317. fclose (fp);
  318. ucl_object_emit_funcs_free (emitter_functions);
  319. return TRUE;
  320. }
  321. static ucl_object_t*
  322. new_dynamic_metric (const gchar *metric_name, ucl_object_t *top)
  323. {
  324. ucl_object_t *metric;
  325. metric = ucl_object_typed_new (UCL_OBJECT);
  326. ucl_object_insert_key (metric, ucl_object_fromstring (metric_name),
  327. "metric", sizeof ("metric") - 1, true);
  328. ucl_object_insert_key (metric, ucl_object_typed_new (UCL_ARRAY),
  329. "actions", sizeof ("actions") - 1, false);
  330. ucl_object_insert_key (metric, ucl_object_typed_new (UCL_ARRAY),
  331. "symbols", sizeof ("symbols") - 1, false);
  332. ucl_array_append (top, metric);
  333. return metric;
  334. }
  335. static ucl_object_t *
  336. dynamic_metric_find_elt (const ucl_object_t *arr, const gchar *name)
  337. {
  338. ucl_object_iter_t it = NULL;
  339. const ucl_object_t *cur, *n;
  340. it = ucl_object_iterate_new (arr);
  341. while ((cur = ucl_object_iterate_safe (it, true)) != NULL) {
  342. if (cur->type == UCL_OBJECT) {
  343. n = ucl_object_lookup (cur, "name");
  344. if (n && n->type == UCL_STRING &&
  345. strcmp (name, ucl_object_tostring (n)) == 0) {
  346. ucl_object_iterate_free (it);
  347. return (ucl_object_t *)ucl_object_lookup (cur, "value");
  348. }
  349. }
  350. }
  351. ucl_object_iterate_free (it);
  352. return NULL;
  353. }
  354. static ucl_object_t *
  355. dynamic_metric_find_metric (const ucl_object_t *arr, const gchar *metric)
  356. {
  357. ucl_object_iter_t it = NULL;
  358. const ucl_object_t *cur, *n;
  359. it = ucl_object_iterate_new (arr);
  360. while ((cur = ucl_object_iterate_safe (it, true)) != NULL) {
  361. if (cur->type == UCL_OBJECT) {
  362. n = ucl_object_lookup (cur, "metric");
  363. if (n && n->type == UCL_STRING &&
  364. strcmp (metric, ucl_object_tostring (n)) == 0) {
  365. ucl_object_iterate_free (it);
  366. return (ucl_object_t *)cur;
  367. }
  368. }
  369. }
  370. ucl_object_iterate_free (it);
  371. return NULL;
  372. }
  373. static ucl_object_t *
  374. new_dynamic_elt (ucl_object_t *arr, const gchar *name, gdouble value)
  375. {
  376. ucl_object_t *n;
  377. n = ucl_object_typed_new (UCL_OBJECT);
  378. ucl_object_insert_key (n, ucl_object_fromstring (name), "name",
  379. sizeof ("name") - 1, false);
  380. ucl_object_insert_key (n, ucl_object_fromdouble (value), "value",
  381. sizeof ("value") - 1, false);
  382. ucl_array_append (arr, n);
  383. return n;
  384. }
  385. static gint
  386. rspamd_maybe_add_lua_dynsym (struct rspamd_config *cfg,
  387. const gchar *sym,
  388. gdouble score)
  389. {
  390. lua_State *L = cfg->lua_state;
  391. gint ret = -1;
  392. struct rspamd_config **pcfg;
  393. lua_getglobal (L, "rspamd_plugins");
  394. if (lua_type (L, -1) == LUA_TTABLE) {
  395. lua_pushstring (L, "dynamic_conf");
  396. lua_gettable (L, -2);
  397. if (lua_type (L, -1) == LUA_TTABLE) {
  398. lua_pushstring (L, "add_symbol");
  399. lua_gettable (L, -2);
  400. if (lua_type (L, -1) == LUA_TFUNCTION) {
  401. pcfg = lua_newuserdata (L, sizeof (*pcfg));
  402. *pcfg = cfg;
  403. rspamd_lua_setclass (L, "rspamd{config}", -1);
  404. lua_pushstring (L, sym);
  405. lua_pushnumber (L, score);
  406. if (lua_pcall (L, 3, 1, 0) != 0) {
  407. msg_err_config ("cannot execute add_symbol script: %s",
  408. lua_tostring (L, -1));
  409. }
  410. else {
  411. ret = lua_toboolean (L, -1);
  412. }
  413. lua_pop (L, 1);
  414. }
  415. else {
  416. lua_pop (L, 1);
  417. }
  418. }
  419. lua_pop (L, 1);
  420. }
  421. lua_pop (L, 1);
  422. return ret;
  423. }
  424. static gint
  425. rspamd_maybe_add_lua_dynact (struct rspamd_config *cfg,
  426. const gchar *action,
  427. gdouble score)
  428. {
  429. lua_State *L = cfg->lua_state;
  430. gint ret = -1;
  431. struct rspamd_config **pcfg;
  432. lua_getglobal (L, "rspamd_plugins");
  433. if (lua_type (L, -1) == LUA_TTABLE) {
  434. lua_pushstring (L, "dynamic_conf");
  435. lua_gettable (L, -2);
  436. if (lua_type (L, -1) == LUA_TTABLE) {
  437. lua_pushstring (L, "add_action");
  438. lua_gettable (L, -2);
  439. if (lua_type (L, -1) == LUA_TFUNCTION) {
  440. pcfg = lua_newuserdata (L, sizeof (*pcfg));
  441. *pcfg = cfg;
  442. rspamd_lua_setclass (L, "rspamd{config}", -1);
  443. lua_pushstring (L, action);
  444. lua_pushnumber (L, score);
  445. if (lua_pcall (L, 3, 1, 0) != 0) {
  446. msg_err_config ("cannot execute add_action script: %s",
  447. lua_tostring (L, -1));
  448. }
  449. else {
  450. ret = lua_toboolean (L, -1);
  451. }
  452. lua_pop (L, 1);
  453. }
  454. else {
  455. lua_pop (L, 1);
  456. }
  457. }
  458. lua_pop (L, 1);
  459. }
  460. lua_pop (L, 1);
  461. return ret;
  462. }
  463. /**
  464. * Add symbol for specified metric
  465. * @param cfg config file object
  466. * @param metric metric's name
  467. * @param symbol symbol's name
  468. * @param value value of symbol
  469. * @return
  470. */
  471. gboolean
  472. add_dynamic_symbol (struct rspamd_config *cfg,
  473. const gchar *metric_name,
  474. const gchar *symbol,
  475. gdouble value)
  476. {
  477. ucl_object_t *metric, *syms;
  478. gint ret;
  479. if ((ret = rspamd_maybe_add_lua_dynsym (cfg, symbol, value)) != -1) {
  480. return ret == 0 ? FALSE : TRUE;
  481. }
  482. if (cfg->dynamic_conf == NULL) {
  483. msg_info ("dynamic conf is disabled");
  484. return FALSE;
  485. }
  486. metric = dynamic_metric_find_metric (cfg->current_dynamic_conf,
  487. metric_name);
  488. if (metric == NULL) {
  489. metric = new_dynamic_metric (metric_name, cfg->current_dynamic_conf);
  490. }
  491. syms = (ucl_object_t *)ucl_object_lookup (metric, "symbols");
  492. if (syms != NULL) {
  493. ucl_object_t *sym;
  494. sym = dynamic_metric_find_elt (syms, symbol);
  495. if (sym) {
  496. sym->value.dv = value;
  497. }
  498. else {
  499. new_dynamic_elt (syms, symbol, value);
  500. }
  501. }
  502. apply_dynamic_conf (cfg->current_dynamic_conf, cfg);
  503. return TRUE;
  504. }
  505. gboolean
  506. remove_dynamic_symbol (struct rspamd_config *cfg,
  507. const gchar *metric_name,
  508. const gchar *symbol)
  509. {
  510. ucl_object_t *metric, *syms;
  511. gboolean ret = FALSE;
  512. if (cfg->dynamic_conf == NULL) {
  513. msg_info ("dynamic conf is disabled");
  514. return FALSE;
  515. }
  516. metric = dynamic_metric_find_metric (cfg->current_dynamic_conf,
  517. metric_name);
  518. if (metric == NULL) {
  519. return FALSE;
  520. }
  521. syms = (ucl_object_t *)ucl_object_lookup (metric, "symbols");
  522. if (syms != NULL) {
  523. ucl_object_t *sym;
  524. sym = dynamic_metric_find_elt (syms, symbol);
  525. if (sym) {
  526. ret = ucl_array_delete ((ucl_object_t *)syms, sym) != NULL;
  527. if (ret) {
  528. ucl_object_unref (sym);
  529. }
  530. }
  531. }
  532. if (ret) {
  533. apply_dynamic_conf (cfg->current_dynamic_conf, cfg);
  534. }
  535. return ret;
  536. }
  537. /**
  538. * Add action for specified metric
  539. * @param cfg config file object
  540. * @param metric metric's name
  541. * @param action action's name
  542. * @param value value of symbol
  543. * @return
  544. */
  545. gboolean
  546. add_dynamic_action (struct rspamd_config *cfg,
  547. const gchar *metric_name,
  548. guint action,
  549. gdouble value)
  550. {
  551. ucl_object_t *metric, *acts;
  552. const gchar *action_name = rspamd_action_to_str (action);
  553. gint ret;
  554. if ((ret = rspamd_maybe_add_lua_dynact (cfg, action_name, value)) != -1) {
  555. return ret == 0 ? FALSE : TRUE;
  556. }
  557. if (cfg->dynamic_conf == NULL) {
  558. msg_info ("dynamic conf is disabled");
  559. return FALSE;
  560. }
  561. metric = dynamic_metric_find_metric (cfg->current_dynamic_conf,
  562. metric_name);
  563. if (metric == NULL) {
  564. metric = new_dynamic_metric (metric_name, cfg->current_dynamic_conf);
  565. }
  566. acts = (ucl_object_t *)ucl_object_lookup (metric, "actions");
  567. if (acts != NULL) {
  568. ucl_object_t *act;
  569. act = dynamic_metric_find_elt (acts, action_name);
  570. if (act) {
  571. act->value.dv = value;
  572. }
  573. else {
  574. new_dynamic_elt (acts, action_name, value);
  575. }
  576. }
  577. apply_dynamic_conf (cfg->current_dynamic_conf, cfg);
  578. return TRUE;
  579. }
  580. gboolean
  581. remove_dynamic_action (struct rspamd_config *cfg,
  582. const gchar *metric_name,
  583. guint action)
  584. {
  585. ucl_object_t *metric, *acts;
  586. const gchar *action_name = rspamd_action_to_str (action);
  587. gboolean ret = FALSE;
  588. if (cfg->dynamic_conf == NULL) {
  589. msg_info ("dynamic conf is disabled");
  590. return FALSE;
  591. }
  592. metric = dynamic_metric_find_metric (cfg->current_dynamic_conf,
  593. metric_name);
  594. if (metric == NULL) {
  595. return FALSE;
  596. }
  597. acts = (ucl_object_t *)ucl_object_lookup (metric, "actions");
  598. if (acts != NULL) {
  599. ucl_object_t *act;
  600. act = dynamic_metric_find_elt (acts, action_name);
  601. if (act) {
  602. ret = ucl_array_delete (acts, act) != NULL;
  603. }
  604. if (ret) {
  605. ucl_object_unref (act);
  606. }
  607. }
  608. if (ret) {
  609. apply_dynamic_conf (cfg->current_dynamic_conf, cfg);
  610. }
  611. return ret;
  612. }