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.

lua_map.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 "lua_common.h"
  17. #include "libutil/map.h"
  18. #include "libutil/map_helpers.h"
  19. #include "libutil/map_private.h"
  20. #include "contrib/libucl/lua_ucl.h"
  21. /***
  22. * This module is used to manage rspamd maps and map like objects
  23. *
  24. * @module rspamd_map
  25. *
  26. * All maps could be obtained by function `rspamd_config:get_maps()`
  27. * Also see [`lua_maps` module description](lua_maps.html).
  28. */
  29. /***
  30. * @method map:get_key(in)
  31. * Variable method for different types of maps:
  32. *
  33. * - For hash maps it returns boolean and accepts string
  34. * - For kv maps it returns string (or nil) and accepts string
  35. * - For radix maps it returns boolean and accepts IP address (as object, string or number)
  36. *
  37. * @param {vary} in input to check
  38. * @return {bool|string} if a value is found then this function returns string or `True` if not - then it returns `nil` or `False`
  39. */
  40. LUA_FUNCTION_DEF (map, get_key);
  41. /***
  42. * @method map:is_signed()
  43. * Returns `True` if a map is signed
  44. * @return {bool} signed value
  45. */
  46. LUA_FUNCTION_DEF (map, is_signed);
  47. /***
  48. * @method map:get_proto()
  49. * Returns protocol of map as string:
  50. *
  51. * - `http`: for HTTP map
  52. * - `file`: for file map
  53. * @return {string} string representation of the map protocol
  54. */
  55. LUA_FUNCTION_DEF (map, get_proto);
  56. /***
  57. * @method map:get_sign_key()
  58. * Returns pubkey used for signing as base32 string or nil
  59. * @return {string} base32 encoded string or nil
  60. */
  61. LUA_FUNCTION_DEF (map, get_sign_key);
  62. /***
  63. * @method map:set_sign_key(key)
  64. * Set trusted key for signatures for this map
  65. * @param {string} key base32 encoded string or nil
  66. */
  67. LUA_FUNCTION_DEF (map, set_sign_key);
  68. /***
  69. * @method map:set_callback(cb)
  70. * Set callback for a specified callback map.
  71. * @param {function} cb map callback function
  72. */
  73. LUA_FUNCTION_DEF (map, set_callback);
  74. /***
  75. * @method map:get_uri()
  76. * Get uri for a specified map
  77. * @return {string} map's URI
  78. */
  79. LUA_FUNCTION_DEF (map, get_uri);
  80. /***
  81. * @method map:get_stats(reset)
  82. * Get statistics for specific map. It returns table in form:
  83. * [key] => [nhits]
  84. * @param {boolean} reset reset stats if true
  85. * @return {table} map's stat
  86. */
  87. LUA_FUNCTION_DEF (map, get_stats);
  88. /***
  89. * @method map:get_data_digest()
  90. * Get data digest for specific map
  91. * @return {string} 64 bit number represented as string (due to Lua limitations)
  92. */
  93. LUA_FUNCTION_DEF (map, get_data_digest);
  94. /***
  95. * @method map:get_nelts()
  96. * Get number of elements for specific map
  97. * @return {number} number of elements in the map
  98. */
  99. LUA_FUNCTION_DEF (map, get_nelts);
  100. static const struct luaL_reg maplib_m[] = {
  101. LUA_INTERFACE_DEF (map, get_key),
  102. LUA_INTERFACE_DEF (map, is_signed),
  103. LUA_INTERFACE_DEF (map, get_proto),
  104. LUA_INTERFACE_DEF (map, get_sign_key),
  105. LUA_INTERFACE_DEF (map, set_sign_key),
  106. LUA_INTERFACE_DEF (map, set_callback),
  107. LUA_INTERFACE_DEF (map, get_uri),
  108. LUA_INTERFACE_DEF (map, get_stats),
  109. LUA_INTERFACE_DEF (map, get_data_digest),
  110. LUA_INTERFACE_DEF (map, get_nelts),
  111. {"__tostring", rspamd_lua_class_tostring},
  112. {NULL, NULL}
  113. };
  114. struct lua_map_callback_data {
  115. lua_State *L;
  116. gint ref;
  117. rspamd_fstring_t *data;
  118. struct rspamd_lua_map *lua_map;
  119. };
  120. struct rspamd_lua_map *
  121. lua_check_map (lua_State * L, gint pos)
  122. {
  123. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{map}");
  124. luaL_argcheck (L, ud != NULL, pos, "'map' expected");
  125. return ud ? *((struct rspamd_lua_map **)ud) : NULL;
  126. }
  127. gint
  128. lua_config_add_radix_map (lua_State *L)
  129. {
  130. LUA_TRACE_POINT;
  131. struct rspamd_config *cfg = lua_check_config (L, 1);
  132. const gchar *map_line, *description;
  133. struct rspamd_lua_map *map, **pmap;
  134. struct rspamd_map *m;
  135. if (cfg) {
  136. map_line = luaL_checkstring (L, 2);
  137. description = lua_tostring (L, 3);
  138. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  139. map->data.radix = NULL;
  140. map->type = RSPAMD_LUA_MAP_RADIX;
  141. if ((m = rspamd_map_add (cfg, map_line, description,
  142. rspamd_radix_read,
  143. rspamd_radix_fin,
  144. rspamd_radix_dtor,
  145. (void **)&map->data.radix)) == NULL) {
  146. msg_warn_config ("invalid radix map %s", map_line);
  147. lua_pushnil (L);
  148. return 1;
  149. }
  150. map->map = m;
  151. m->lua_map = map;
  152. pmap = lua_newuserdata (L, sizeof (void *));
  153. *pmap = map;
  154. rspamd_lua_setclass (L, "rspamd{map}", -1);
  155. }
  156. else {
  157. return luaL_error (L, "invalid arguments");
  158. }
  159. return 1;
  160. }
  161. gint
  162. lua_config_radix_from_config (lua_State *L)
  163. {
  164. LUA_TRACE_POINT;
  165. struct rspamd_config *cfg = lua_check_config (L, 1);
  166. const gchar *mname, *optname;
  167. const ucl_object_t *obj;
  168. struct rspamd_lua_map *map, **pmap;
  169. ucl_object_t *fake_obj;
  170. struct rspamd_map *m;
  171. if (!cfg) {
  172. return luaL_error (L, "invalid arguments");
  173. }
  174. mname = luaL_checkstring (L, 2);
  175. optname = luaL_checkstring (L, 3);
  176. if (mname && optname) {
  177. obj = rspamd_config_get_module_opt (cfg, mname, optname);
  178. if (obj) {
  179. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  180. map->data.radix = NULL;
  181. map->type = RSPAMD_LUA_MAP_RADIX;
  182. fake_obj = ucl_object_typed_new (UCL_OBJECT);
  183. ucl_object_insert_key (fake_obj, ucl_object_ref (obj),
  184. "data", 0, false);
  185. ucl_object_insert_key (fake_obj, ucl_object_fromstring ("static"),
  186. "url", 0, false);
  187. if ((m = rspamd_map_add_from_ucl (cfg, fake_obj, "static radix map",
  188. rspamd_radix_read,
  189. rspamd_radix_fin,
  190. rspamd_radix_dtor,
  191. (void **)&map->data.radix)) == NULL) {
  192. msg_err_config ("invalid radix map static");
  193. lua_pushnil (L);
  194. ucl_object_unref (fake_obj);
  195. return 1;
  196. }
  197. ucl_object_unref (fake_obj);
  198. pmap = lua_newuserdata (L, sizeof (void *));
  199. map->map = m;
  200. m->lua_map = map;
  201. *pmap = map;
  202. rspamd_lua_setclass (L, "rspamd{map}", -1);
  203. }
  204. else {
  205. msg_warn_config ("Couldnt find config option [%s][%s]", mname,
  206. optname);
  207. lua_pushnil (L);
  208. }
  209. }
  210. else {
  211. return luaL_error (L, "invalid arguments");
  212. }
  213. return 1;
  214. }
  215. gint
  216. lua_config_radix_from_ucl (lua_State *L)
  217. {
  218. LUA_TRACE_POINT;
  219. struct rspamd_config *cfg = lua_check_config (L, 1);
  220. ucl_object_t *obj;
  221. struct rspamd_lua_map *map, **pmap;
  222. ucl_object_t *fake_obj;
  223. struct rspamd_map *m;
  224. if (!cfg) {
  225. return luaL_error (L, "invalid arguments");
  226. }
  227. obj = ucl_object_lua_import (L, 2);
  228. if (obj) {
  229. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  230. map->data.radix = NULL;
  231. map->type = RSPAMD_LUA_MAP_RADIX;
  232. fake_obj = ucl_object_typed_new (UCL_OBJECT);
  233. ucl_object_insert_key (fake_obj, ucl_object_ref (obj),
  234. "data", 0, false);
  235. ucl_object_insert_key (fake_obj, ucl_object_fromstring ("static"),
  236. "url", 0, false);
  237. if ((m = rspamd_map_add_from_ucl (cfg, fake_obj, "static radix map",
  238. rspamd_radix_read,
  239. rspamd_radix_fin,
  240. rspamd_radix_dtor,
  241. (void **)&map->data.radix)) == NULL) {
  242. msg_err_config ("invalid radix map static");
  243. lua_pushnil (L);
  244. ucl_object_unref (fake_obj);
  245. ucl_object_unref (obj);
  246. return 1;
  247. }
  248. ucl_object_unref (fake_obj);
  249. ucl_object_unref (obj);
  250. pmap = lua_newuserdata (L, sizeof (void *));
  251. map->map = m;
  252. m->lua_map = map;
  253. *pmap = map;
  254. rspamd_lua_setclass (L, "rspamd{map}", -1);
  255. }
  256. else {
  257. return luaL_error (L, "invalid arguments");
  258. }
  259. return 1;
  260. }
  261. gint
  262. lua_config_add_hash_map (lua_State *L)
  263. {
  264. LUA_TRACE_POINT;
  265. struct rspamd_config *cfg = lua_check_config (L, 1);
  266. const gchar *map_line, *description;
  267. struct rspamd_lua_map *map, **pmap;
  268. struct rspamd_map *m;
  269. if (cfg) {
  270. map_line = luaL_checkstring (L, 2);
  271. description = lua_tostring (L, 3);
  272. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  273. map->data.hash = NULL;
  274. map->type = RSPAMD_LUA_MAP_SET;
  275. if ((m = rspamd_map_add (cfg, map_line, description,
  276. rspamd_kv_list_read,
  277. rspamd_kv_list_fin,
  278. rspamd_kv_list_dtor,
  279. (void **)&map->data.hash)) == NULL) {
  280. msg_warn_config ("invalid set map %s", map_line);
  281. lua_pushnil (L);
  282. return 1;
  283. }
  284. map->map = m;
  285. m->lua_map = map;
  286. pmap = lua_newuserdata (L, sizeof (void *));
  287. *pmap = map;
  288. rspamd_lua_setclass (L, "rspamd{map}", -1);
  289. }
  290. else {
  291. return luaL_error (L, "invalid arguments");
  292. }
  293. return 1;
  294. }
  295. gint
  296. lua_config_add_kv_map (lua_State *L)
  297. {
  298. LUA_TRACE_POINT;
  299. struct rspamd_config *cfg = lua_check_config (L, 1);
  300. const gchar *map_line, *description;
  301. struct rspamd_lua_map *map, **pmap;
  302. struct rspamd_map *m;
  303. if (cfg) {
  304. map_line = luaL_checkstring (L, 2);
  305. description = lua_tostring (L, 3);
  306. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  307. map->data.hash = NULL;
  308. map->type = RSPAMD_LUA_MAP_HASH;
  309. if ((m = rspamd_map_add (cfg, map_line, description,
  310. rspamd_kv_list_read,
  311. rspamd_kv_list_fin,
  312. rspamd_kv_list_dtor,
  313. (void **)&map->data.hash)) == NULL) {
  314. msg_warn_config ("invalid hash map %s", map_line);
  315. lua_pushnil (L);
  316. return 1;
  317. }
  318. map->map = m;
  319. m->lua_map = map;
  320. pmap = lua_newuserdata (L, sizeof (void *));
  321. *pmap = map;
  322. rspamd_lua_setclass (L, "rspamd{map}", -1);
  323. }
  324. else {
  325. return luaL_error (L, "invalid arguments");
  326. }
  327. return 1;
  328. }
  329. static gchar *
  330. lua_map_read (gchar *chunk, gint len,
  331. struct map_cb_data *data,
  332. gboolean final)
  333. {
  334. struct lua_map_callback_data *cbdata, *old;
  335. if (data->cur_data == NULL) {
  336. old = (struct lua_map_callback_data *)data->prev_data;
  337. cbdata = old;
  338. cbdata->L = old->L;
  339. cbdata->ref = old->ref;
  340. cbdata->lua_map = old->lua_map;
  341. data->cur_data = cbdata;
  342. data->prev_data = NULL;
  343. }
  344. else {
  345. cbdata = (struct lua_map_callback_data *)data->cur_data;
  346. }
  347. if (cbdata->data == NULL) {
  348. cbdata->data = rspamd_fstring_new_init (chunk, len);
  349. }
  350. else {
  351. cbdata->data = rspamd_fstring_append (cbdata->data, chunk, len);
  352. }
  353. return NULL;
  354. }
  355. static void
  356. lua_map_fin (struct map_cb_data *data, void **target)
  357. {
  358. struct lua_map_callback_data *cbdata;
  359. struct rspamd_lua_map **pmap;
  360. struct rspamd_map *map;
  361. map = data->map;
  362. if (data->cur_data) {
  363. cbdata = (struct lua_map_callback_data *)data->cur_data;
  364. }
  365. else {
  366. msg_err_map ("no data read for map");
  367. return;
  368. }
  369. if (cbdata->ref == -1) {
  370. msg_err_map ("map has no callback set");
  371. }
  372. else if (cbdata->data != NULL && cbdata->data->len != 0) {
  373. lua_rawgeti (cbdata->L, LUA_REGISTRYINDEX, cbdata->ref);
  374. lua_pushlstring (cbdata->L, cbdata->data->str, cbdata->data->len);
  375. pmap = lua_newuserdata (cbdata->L, sizeof (void *));
  376. *pmap = cbdata->lua_map;
  377. rspamd_lua_setclass (cbdata->L, "rspamd{map}", -1);
  378. if (lua_pcall (cbdata->L, 2, 0, 0) != 0) {
  379. msg_info_map ("call to %s failed: %s", "local function",
  380. lua_tostring (cbdata->L, -1));
  381. lua_pop (cbdata->L, 1);
  382. }
  383. }
  384. cbdata->data = rspamd_fstring_assign (cbdata->data, "", 0);
  385. if (target) {
  386. *target = data->cur_data;
  387. }
  388. if (data->prev_data) {
  389. data->prev_data = NULL;
  390. }
  391. }
  392. static void
  393. lua_map_dtor (struct map_cb_data *data)
  394. {
  395. struct lua_map_callback_data *cbdata;
  396. if (data->cur_data) {
  397. cbdata = (struct lua_map_callback_data *)data->cur_data;
  398. if (cbdata->ref != -1) {
  399. luaL_unref (cbdata->L, LUA_REGISTRYINDEX, cbdata->ref);
  400. }
  401. if (cbdata->data) {
  402. rspamd_fstring_free (cbdata->data);
  403. }
  404. }
  405. }
  406. gint
  407. lua_config_add_map (lua_State *L)
  408. {
  409. LUA_TRACE_POINT;
  410. struct rspamd_config *cfg = lua_check_config (L, 1);
  411. const char *description = NULL;
  412. const gchar *type = NULL;
  413. ucl_object_t *map_obj = NULL;
  414. struct lua_map_callback_data *cbdata;
  415. struct rspamd_lua_map *map, **pmap;
  416. struct rspamd_map *m;
  417. int cbidx = -1, ret;
  418. GError *err = NULL;
  419. if (cfg) {
  420. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  421. RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT,
  422. "*url=O;description=S;callback=F;type=S",
  423. &map_obj, &description, &cbidx, &type)) {
  424. ret = luaL_error (L, "invalid table arguments: %s", err->message);
  425. g_error_free (err);
  426. if (map_obj) {
  427. ucl_object_unref (map_obj);
  428. }
  429. return ret;
  430. }
  431. g_assert (map_obj != NULL);
  432. if (type == NULL && cbidx != -1) {
  433. type = "callback";
  434. }
  435. else if (type == NULL) {
  436. return luaL_error (L, "invalid map type");
  437. }
  438. if (strcmp (type, "callback") == 0) {
  439. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  440. map->type = RSPAMD_LUA_MAP_CALLBACK;
  441. map->data.cbdata = rspamd_mempool_alloc0 (cfg->cfg_pool,
  442. sizeof (*map->data.cbdata));
  443. cbdata = map->data.cbdata;
  444. cbdata->L = L;
  445. cbdata->data = NULL;
  446. cbdata->lua_map = map;
  447. cbdata->ref = cbidx;
  448. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  449. lua_map_read,
  450. lua_map_fin,
  451. lua_map_dtor,
  452. (void **)&map->data.cbdata)) == NULL) {
  453. if (cbidx != -1) {
  454. luaL_unref (L, LUA_REGISTRYINDEX, cbidx);
  455. }
  456. if (map_obj) {
  457. ucl_object_unref (map_obj);
  458. }
  459. lua_pushnil (L);
  460. return 1;
  461. }
  462. m->lua_map = map;
  463. }
  464. else if (strcmp (type, "set") == 0) {
  465. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  466. map->data.hash = NULL;
  467. map->type = RSPAMD_LUA_MAP_SET;
  468. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  469. rspamd_kv_list_read,
  470. rspamd_kv_list_fin,
  471. rspamd_kv_list_dtor,
  472. (void **)&map->data.hash)) == NULL) {
  473. lua_pushnil (L);
  474. ucl_object_unref (map_obj);
  475. return 1;
  476. }
  477. m->lua_map = map;
  478. }
  479. else if (strcmp (type, "map") == 0 || strcmp (type, "hash") == 0) {
  480. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  481. map->data.hash = NULL;
  482. map->type = RSPAMD_LUA_MAP_HASH;
  483. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  484. rspamd_kv_list_read,
  485. rspamd_kv_list_fin,
  486. rspamd_kv_list_dtor,
  487. (void **)&map->data.hash)) == NULL) {
  488. lua_pushnil (L);
  489. ucl_object_unref (map_obj);
  490. return 1;
  491. }
  492. m->lua_map = map;
  493. }
  494. else if (strcmp (type, "radix") == 0) {
  495. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  496. map->data.radix = NULL;
  497. map->type = RSPAMD_LUA_MAP_RADIX;
  498. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  499. rspamd_radix_read,
  500. rspamd_radix_fin,
  501. rspamd_radix_dtor,
  502. (void **)&map->data.radix)) == NULL) {
  503. lua_pushnil (L);
  504. ucl_object_unref (map_obj);
  505. return 1;
  506. }
  507. m->lua_map = map;
  508. }
  509. else if (strcmp (type, "regexp") == 0) {
  510. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  511. map->data.re_map = NULL;
  512. map->type = RSPAMD_LUA_MAP_REGEXP;
  513. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  514. rspamd_regexp_list_read_single,
  515. rspamd_regexp_list_fin,
  516. rspamd_regexp_list_dtor,
  517. (void **) &map->data.re_map)) == NULL) {
  518. lua_pushnil (L);
  519. ucl_object_unref (map_obj);
  520. return 1;
  521. }
  522. m->lua_map = map;
  523. }
  524. else if (strcmp (type, "regexp_multi") == 0) {
  525. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  526. map->data.re_map = NULL;
  527. map->type = RSPAMD_LUA_MAP_REGEXP_MULTIPLE;
  528. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  529. rspamd_regexp_list_read_multiple,
  530. rspamd_regexp_list_fin,
  531. rspamd_regexp_list_dtor,
  532. (void **) &map->data.re_map)) == NULL) {
  533. lua_pushnil (L);
  534. ucl_object_unref (map_obj);
  535. return 1;
  536. }
  537. m->lua_map = map;
  538. }
  539. else if (strcmp (type, "glob") == 0) {
  540. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  541. map->data.re_map = NULL;
  542. map->type = RSPAMD_LUA_MAP_REGEXP;
  543. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  544. rspamd_glob_list_read_single,
  545. rspamd_regexp_list_fin,
  546. rspamd_regexp_list_dtor,
  547. (void **) &map->data.re_map)) == NULL) {
  548. lua_pushnil (L);
  549. ucl_object_unref (map_obj);
  550. return 1;
  551. }
  552. m->lua_map = map;
  553. }
  554. else if (strcmp (type, "glob_multi") == 0) {
  555. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  556. map->data.re_map = NULL;
  557. map->type = RSPAMD_LUA_MAP_REGEXP_MULTIPLE;
  558. if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
  559. rspamd_glob_list_read_multiple,
  560. rspamd_regexp_list_fin,
  561. rspamd_regexp_list_dtor,
  562. (void **) &map->data.re_map)) == NULL) {
  563. lua_pushnil (L);
  564. ucl_object_unref (map_obj);
  565. return 1;
  566. }
  567. m->lua_map = map;
  568. }
  569. else {
  570. ret = luaL_error (L, "invalid arguments: unknown type '%s'", type);
  571. ucl_object_unref (map_obj);
  572. return ret;
  573. }
  574. map->map = m;
  575. pmap = lua_newuserdata (L, sizeof (void *));
  576. *pmap = map;
  577. rspamd_lua_setclass (L, "rspamd{map}", -1);
  578. }
  579. else {
  580. return luaL_error (L, "invalid arguments");
  581. }
  582. ucl_object_unref (map_obj);
  583. return 1;
  584. }
  585. gint
  586. lua_config_get_maps (lua_State*L)
  587. {
  588. LUA_TRACE_POINT;
  589. struct rspamd_config *cfg = lua_check_config (L, 1);
  590. struct rspamd_lua_map *map, **pmap;
  591. struct rspamd_map *m;
  592. gint i = 1;
  593. GList *cur;
  594. if (cfg) {
  595. lua_newtable (L);
  596. cur = g_list_first (cfg->maps);
  597. while (cur) {
  598. m = cur->data;
  599. if (m->lua_map) {
  600. map = m->lua_map;
  601. }
  602. else {
  603. /* Implement heuristic */
  604. map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
  605. if (m->read_callback == rspamd_radix_read) {
  606. map->type = RSPAMD_LUA_MAP_RADIX;
  607. map->data.radix = *m->user_data;
  608. }
  609. else if (m->read_callback == rspamd_kv_list_read) {
  610. map->type = RSPAMD_LUA_MAP_HASH;
  611. map->data.hash = *m->user_data;
  612. }
  613. else {
  614. map->type = RSPAMD_LUA_MAP_UNKNOWN;
  615. }
  616. map->map = m;
  617. m->lua_map = map;
  618. }
  619. pmap = lua_newuserdata (L, sizeof (*pmap));
  620. *pmap = map;
  621. rspamd_lua_setclass (L, "rspamd{map}", -1);
  622. lua_rawseti (L, -2, i);
  623. cur = g_list_next (cur);
  624. i ++;
  625. }
  626. }
  627. else {
  628. return luaL_error (L, "invalid arguments");
  629. }
  630. return 1;
  631. }
  632. static const gchar *
  633. lua_map_process_string_key (lua_State *L, gint pos, gsize *len)
  634. {
  635. struct rspamd_lua_text *t;
  636. if (lua_type (L, pos) == LUA_TSTRING) {
  637. return lua_tolstring (L, pos, len);
  638. }
  639. else if (lua_type (L, pos) == LUA_TUSERDATA) {
  640. t = lua_check_text (L, pos);
  641. if (t) {
  642. *len = t->len;
  643. return t->start;
  644. }
  645. }
  646. return NULL;
  647. }
  648. /* Radix and hash table functions */
  649. static gint
  650. lua_map_get_key (lua_State * L)
  651. {
  652. LUA_TRACE_POINT;
  653. struct rspamd_lua_map *map = lua_check_map (L, 1);
  654. struct rspamd_radix_map_helper *radix;
  655. struct rspamd_lua_ip *addr = NULL;
  656. const gchar *key, *value = NULL;
  657. gpointer ud;
  658. gsize len;
  659. guint32 key_num = 0;
  660. gboolean ret = FALSE;
  661. if (map) {
  662. if (map->type == RSPAMD_LUA_MAP_RADIX) {
  663. radix = map->data.radix;
  664. if (lua_type (L, 2) == LUA_TSTRING) {
  665. const gchar *addr_str;
  666. addr_str = luaL_checklstring (L, 2, &len);
  667. addr = g_alloca (sizeof (*addr));
  668. addr->addr = g_alloca (rspamd_inet_address_storage_size ());
  669. if (!rspamd_parse_inet_address_ip (addr_str, len, addr->addr)) {
  670. addr = NULL;
  671. msg_warn ("invalid ip address: %*s, when checking map: %s",
  672. (gint)len, addr_str, map->map->name);
  673. }
  674. }
  675. else if (lua_type (L, 2) == LUA_TUSERDATA) {
  676. ud = rspamd_lua_check_udata (L, 2, "rspamd{ip}");
  677. if (ud != NULL) {
  678. addr = *((struct rspamd_lua_ip **)ud);
  679. if (addr->addr == NULL) {
  680. addr = NULL;
  681. }
  682. }
  683. else {
  684. msg_err ("invalid userdata type provided, rspamd{ip} expected");
  685. }
  686. }
  687. else if (lua_type (L, 2) == LUA_TNUMBER) {
  688. key_num = luaL_checkinteger (L, 2);
  689. key_num = htonl (key_num);
  690. }
  691. if (radix) {
  692. gconstpointer p = NULL;
  693. if (addr != NULL) {
  694. if ((p = rspamd_match_radix_map_addr (radix, addr->addr))
  695. != NULL) {
  696. ret = TRUE;
  697. }
  698. else {
  699. p = 0;
  700. }
  701. }
  702. else if (key_num != 0) {
  703. if ((p = rspamd_match_radix_map (radix,
  704. (guint8 *)&key_num, sizeof (key_num))) != NULL) {
  705. ret = TRUE;
  706. }
  707. else {
  708. p = 0;
  709. }
  710. }
  711. value = (const char *)p;
  712. }
  713. if (ret) {
  714. lua_pushstring (L, value);
  715. return 1;
  716. }
  717. }
  718. else if (map->type == RSPAMD_LUA_MAP_SET) {
  719. key = lua_map_process_string_key (L, 2, &len);
  720. if (key && map->data.hash) {
  721. ret = rspamd_match_hash_map (map->data.hash, key) != NULL;
  722. }
  723. }
  724. else if (map->type == RSPAMD_LUA_MAP_REGEXP) {
  725. key = lua_map_process_string_key (L, 2, &len);
  726. if (key && map->data.re_map) {
  727. value = rspamd_match_regexp_map_single (map->data.re_map, key,
  728. len);
  729. if (value) {
  730. lua_pushstring (L, value);
  731. return 1;
  732. }
  733. }
  734. }
  735. else if (map->type == RSPAMD_LUA_MAP_REGEXP_MULTIPLE) {
  736. GPtrArray *ar;
  737. guint i;
  738. const gchar *val;
  739. key = lua_map_process_string_key (L, 2, &len);
  740. if (key && map->data.re_map) {
  741. ar = rspamd_match_regexp_map_all (map->data.re_map, key,
  742. len);
  743. if (ar) {
  744. lua_createtable (L, ar->len, 0);
  745. PTR_ARRAY_FOREACH (ar, i, val) {
  746. lua_pushstring (L, val);
  747. lua_rawseti (L, -2, i + 1);
  748. }
  749. g_ptr_array_free (ar, TRUE);
  750. return 1;
  751. }
  752. }
  753. }
  754. else if (map->type == RSPAMD_LUA_MAP_HASH) {
  755. /* key-value map */
  756. key = lua_map_process_string_key (L, 2, &len);
  757. if (key && map->data.hash) {
  758. value = rspamd_match_hash_map (map->data.hash, key);
  759. }
  760. if (value) {
  761. lua_pushstring (L, value);
  762. return 1;
  763. }
  764. }
  765. else {
  766. /* callback map or unknown type map */
  767. lua_pushnil (L);
  768. return 1;
  769. }
  770. }
  771. else {
  772. return luaL_error (L, "invalid arguments");
  773. }
  774. lua_pushboolean (L, ret);
  775. return 1;
  776. }
  777. static gboolean
  778. lua_map_traverse_cb (gconstpointer key,
  779. gconstpointer value, gsize hits, gpointer ud)
  780. {
  781. lua_State *L = (lua_State *)ud;
  782. lua_pushstring (L, key);
  783. lua_pushinteger (L, hits);
  784. lua_settable (L, -3);
  785. return TRUE;
  786. }
  787. static gint
  788. lua_map_get_stats (lua_State * L)
  789. {
  790. LUA_TRACE_POINT;
  791. struct rspamd_lua_map *map = lua_check_map (L, 1);
  792. gboolean do_reset = FALSE;
  793. if (map != NULL) {
  794. if (lua_isboolean (L, 2)) {
  795. do_reset = lua_toboolean (L, 2);
  796. }
  797. lua_createtable (L, 0, map->map->nelts);
  798. if (map->map->traverse_function) {
  799. rspamd_map_traverse (map->map, lua_map_traverse_cb, L, do_reset);
  800. }
  801. }
  802. else {
  803. return luaL_error (L, "invalid arguments");
  804. }
  805. return 1;
  806. }
  807. static gint
  808. lua_map_get_data_digest (lua_State * L)
  809. {
  810. LUA_TRACE_POINT;
  811. struct rspamd_lua_map *map = lua_check_map (L, 1);
  812. gchar numbuf[64];
  813. if (map != NULL) {
  814. rspamd_snprintf (numbuf, sizeof (numbuf), "%uL", map->map->digest);
  815. lua_pushstring (L, numbuf);
  816. }
  817. else {
  818. return luaL_error (L, "invalid arguments");
  819. }
  820. return 1;
  821. }
  822. static gint
  823. lua_map_get_nelts (lua_State * L)
  824. {
  825. LUA_TRACE_POINT;
  826. struct rspamd_lua_map *map = lua_check_map (L, 1);
  827. if (map != NULL) {
  828. lua_pushinteger (L, map->map->nelts);
  829. }
  830. else {
  831. return luaL_error (L, "invalid arguments");
  832. }
  833. return 1;
  834. }
  835. static int
  836. lua_map_is_signed (lua_State *L)
  837. {
  838. LUA_TRACE_POINT;
  839. struct rspamd_lua_map *map = lua_check_map (L, 1);
  840. gboolean ret = FALSE;
  841. struct rspamd_map_backend *bk;
  842. guint i;
  843. if (map != NULL) {
  844. if (map->map) {
  845. for (i = 0; i < map->map->backends->len; i ++) {
  846. bk = g_ptr_array_index (map->map->backends, i);
  847. if (bk->is_signed && bk->protocol == MAP_PROTO_FILE) {
  848. ret = TRUE;
  849. break;
  850. }
  851. }
  852. }
  853. }
  854. else {
  855. return luaL_error (L, "invalid arguments");
  856. }
  857. lua_pushboolean (L, ret);
  858. return 1;
  859. }
  860. static int
  861. lua_map_get_proto (lua_State *L)
  862. {
  863. LUA_TRACE_POINT;
  864. struct rspamd_lua_map *map = lua_check_map (L, 1);
  865. const gchar *ret = "undefined";
  866. struct rspamd_map_backend *bk;
  867. guint i;
  868. if (map != NULL) {
  869. for (i = 0; i < map->map->backends->len; i ++) {
  870. bk = g_ptr_array_index (map->map->backends, i);
  871. switch (bk->protocol) {
  872. case MAP_PROTO_FILE:
  873. ret = "file";
  874. break;
  875. case MAP_PROTO_HTTP:
  876. ret = "http";
  877. break;
  878. case MAP_PROTO_HTTPS:
  879. ret = "https";
  880. break;
  881. case MAP_PROTO_STATIC:
  882. ret = "static";
  883. break;
  884. }
  885. lua_pushstring (L, ret);
  886. }
  887. }
  888. else {
  889. return luaL_error (L, "invalid arguments");
  890. }
  891. return map->map->backends->len;
  892. }
  893. static int
  894. lua_map_get_sign_key (lua_State *L)
  895. {
  896. LUA_TRACE_POINT;
  897. struct rspamd_lua_map *map = lua_check_map (L, 1);
  898. struct rspamd_map_backend *bk;
  899. guint i;
  900. GString *ret = NULL;
  901. if (map != NULL) {
  902. for (i = 0; i < map->map->backends->len; i ++) {
  903. bk = g_ptr_array_index (map->map->backends, i);
  904. if (bk->trusted_pubkey) {
  905. ret = rspamd_pubkey_print (bk->trusted_pubkey,
  906. RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32);
  907. }
  908. else {
  909. ret = NULL;
  910. }
  911. if (ret) {
  912. lua_pushlstring (L, ret->str, ret->len);
  913. g_string_free (ret, TRUE);
  914. }
  915. else {
  916. lua_pushnil (L);
  917. }
  918. }
  919. }
  920. else {
  921. return luaL_error (L, "invalid arguments");
  922. }
  923. return map->map->backends->len;
  924. }
  925. static int
  926. lua_map_set_sign_key (lua_State *L)
  927. {
  928. LUA_TRACE_POINT;
  929. struct rspamd_lua_map *map = lua_check_map (L, 1);
  930. struct rspamd_map_backend *bk;
  931. const gchar *pk_str;
  932. struct rspamd_cryptobox_pubkey *pk;
  933. gsize len;
  934. guint i;
  935. pk_str = lua_tolstring (L, 2, &len);
  936. if (map && pk_str) {
  937. pk = rspamd_pubkey_from_base32 (pk_str, len, RSPAMD_KEYPAIR_SIGN,
  938. RSPAMD_CRYPTOBOX_MODE_25519);
  939. if (!pk) {
  940. return luaL_error (L, "invalid pubkey string");
  941. }
  942. for (i = 0; i < map->map->backends->len; i ++) {
  943. bk = g_ptr_array_index (map->map->backends, i);
  944. if (bk->trusted_pubkey) {
  945. /* Unref old pk */
  946. rspamd_pubkey_unref (bk->trusted_pubkey);
  947. }
  948. bk->trusted_pubkey = rspamd_pubkey_ref (pk);
  949. }
  950. rspamd_pubkey_unref (pk);
  951. }
  952. else {
  953. return luaL_error (L, "invalid arguments");
  954. }
  955. return 0;
  956. }
  957. static int
  958. lua_map_set_callback (lua_State *L)
  959. {
  960. LUA_TRACE_POINT;
  961. struct rspamd_lua_map *map = lua_check_map (L, 1);
  962. if (!map || map->type != RSPAMD_LUA_MAP_CALLBACK || map->data.cbdata == NULL) {
  963. return luaL_error (L, "invalid map");
  964. }
  965. if (lua_type (L, 2) != LUA_TFUNCTION) {
  966. return luaL_error (L, "invalid callback");
  967. }
  968. lua_pushvalue (L, 2);
  969. /* Get a reference */
  970. map->data.cbdata->ref = luaL_ref (L, LUA_REGISTRYINDEX);
  971. return 0;
  972. }
  973. static int
  974. lua_map_get_uri (lua_State *L)
  975. {
  976. LUA_TRACE_POINT;
  977. struct rspamd_lua_map *map = lua_check_map (L, 1);
  978. const gchar *ret = "undefined";
  979. struct rspamd_map_backend *bk;
  980. guint i;
  981. if (map != NULL) {
  982. for (i = 0; i < map->map->backends->len; i ++) {
  983. bk = g_ptr_array_index (map->map->backends, i);
  984. ret = bk->uri;
  985. lua_pushstring (L, ret);
  986. }
  987. }
  988. else {
  989. return luaL_error (L, "invalid arguments");
  990. }
  991. return map->map->backends->len;
  992. }
  993. void
  994. luaopen_map (lua_State * L)
  995. {
  996. rspamd_lua_new_class (L, "rspamd{map}", maplib_m);
  997. lua_pop (L, 1);
  998. }