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_cdb.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright 2024 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 "cdb.h"
  18. #define CDB_REFRESH_TIME 60
  19. /***
  20. * @module rspamd_cdb
  21. * Rspamd CDB module is used to read and write key/value pairs to the CDB file
  22. *
  23. * @example
  24. local rspamd_cdb = require "rspamd_cdb"
  25. rspamd_cdb.build('/tmp/test.cdb'):add('test', 'value'):finalize()
  26. local c = rspamd_cdb.open('/tmp/test.cdb')
  27. c:find('test')
  28. -- will return 'value'
  29. */
  30. /***
  31. * @function rspamd_cdb.open(filename, [ev_base])
  32. * Opens an existing CDB for reading. If `ev_base` is specified, then cdb file is added
  33. * for monitoring, that will get updates on disk file changes.
  34. * @param {string} filename path to file
  35. * @param {ev_base} event loop object
  36. * @return {rspamd_cdb} cdb object
  37. */
  38. LUA_FUNCTION_DEF(cdb, create);
  39. /***
  40. * @method rspamd_cdb:find(key)
  41. * Finds a specific key in cdb and returns a string or nil if a key has not been found
  42. * @param {string} key key to find
  43. * @return {string/nil} value for the specific key
  44. */
  45. LUA_FUNCTION_DEF(cdb, lookup);
  46. /***
  47. * @method rspamd_cdb:get_name()
  48. * Returns filename for the specific cdb
  49. * @return {string} filename for cdb
  50. */
  51. LUA_FUNCTION_DEF(cdb, get_name);
  52. LUA_FUNCTION_DEF(cdb, destroy);
  53. /***
  54. * @function rspamd_cdb.build(filename, [mode])
  55. * Creates a new cdb in a file (existing one will be overwritten!). The object
  56. * returned can be used merely for adding data. Upon finalizing, the data is written to
  57. * disk and cdb can no longer be changed.
  58. * @param {string} filename path to file
  59. * @param {int} mode numeric mode to create a file
  60. * @return {rspamd_cdb_builder} cdb builder object (or nil + error message)
  61. */
  62. LUA_FUNCTION_DEF(cdb, build);
  63. /***
  64. * @method rspamd_cdb_builder:add(key, value)
  65. * Adds new value to cdb in the builder mode
  66. * @param {string} key key to add
  67. * @param {string} value value to associate with the key
  68. * @return {rspamd_cdb_builder} the same object to allow chaining calls
  69. */
  70. LUA_FUNCTION_DEF(cdb_builder, add);
  71. /***
  72. * @method rspamd_cdb_builder:finalize()
  73. * Finalizes the CDB and writes it to disk. This method also closes FD associated with
  74. * CDB builder. No further additions are allowed after this point
  75. */
  76. LUA_FUNCTION_DEF(cdb_builder, finalize);
  77. LUA_FUNCTION_DEF(cdb_builder, dtor);
  78. static const struct luaL_reg cdblib_m[] = {
  79. LUA_INTERFACE_DEF(cdb, lookup),
  80. {"find", lua_cdb_lookup},
  81. LUA_INTERFACE_DEF(cdb, get_name),
  82. {"__tostring", rspamd_lua_class_tostring},
  83. {"__gc", lua_cdb_destroy},
  84. {NULL, NULL}};
  85. static const struct luaL_reg cdbbuilderlib_m[] = {
  86. LUA_INTERFACE_DEF(cdb_builder, add),
  87. LUA_INTERFACE_DEF(cdb_builder, finalize),
  88. {"__tostring", rspamd_lua_class_tostring},
  89. {"__gc", lua_cdb_builder_dtor},
  90. {NULL, NULL}};
  91. static const struct luaL_reg cdblib_f[] = {
  92. LUA_INTERFACE_DEF(cdb, create),
  93. {"open", lua_cdb_create},
  94. {"build", lua_cdb_build},
  95. {NULL, NULL}};
  96. static struct cdb *
  97. lua_check_cdb(lua_State *L, int pos)
  98. {
  99. void *ud = rspamd_lua_check_udata(L, pos, rspamd_cdb_classname);
  100. luaL_argcheck(L, ud != NULL, pos, "'cdb' expected");
  101. return ud ? *((struct cdb **) ud) : NULL;
  102. }
  103. static struct cdb_make *
  104. lua_check_cdb_builder(lua_State *L, int pos)
  105. {
  106. void *ud = rspamd_lua_check_udata(L, pos, rspamd_cdb_builder_classname);
  107. luaL_argcheck(L, ud != NULL, pos, "'cdb_builder' expected");
  108. return ud ? ((struct cdb_make *) ud) : NULL;
  109. }
  110. static const char *
  111. lua_cdb_get_input(lua_State *L, int pos, gsize *olen)
  112. {
  113. int t = lua_type(L, pos);
  114. switch (t) {
  115. case LUA_TSTRING:
  116. return lua_tolstring(L, pos, olen);
  117. case LUA_TNUMBER: {
  118. static char numbuf[sizeof(lua_Number)];
  119. lua_Number n = lua_tonumber(L, pos);
  120. memcpy(numbuf, &n, sizeof(numbuf));
  121. *olen = sizeof(n);
  122. return numbuf;
  123. }
  124. case LUA_TUSERDATA: {
  125. void *p = rspamd_lua_check_udata_maybe(L, pos, rspamd_text_classname);
  126. if (p) {
  127. struct rspamd_lua_text *t = (struct rspamd_lua_text *) p;
  128. *olen = t->len;
  129. return t->start;
  130. }
  131. p = rspamd_lua_check_udata_maybe(L, pos, rspamd_int64_classname);
  132. if (p) {
  133. static char numbuf[sizeof(int64_t)];
  134. memcpy(numbuf, p, sizeof(numbuf));
  135. *olen = sizeof(numbuf);
  136. return numbuf;
  137. }
  138. }
  139. default:
  140. break;
  141. }
  142. return NULL;
  143. }
  144. static gint
  145. lua_cdb_create(lua_State *L)
  146. {
  147. struct cdb *cdb, **pcdb;
  148. const gchar *filename;
  149. gint fd;
  150. struct ev_loop *ev_base = NULL;
  151. if (lua_type(L, 2) == LUA_TUSERDATA) {
  152. ev_base = lua_check_ev_base(L, 2);
  153. }
  154. filename = luaL_checkstring(L, 1);
  155. /* If file begins with cdb://, just skip it */
  156. if (g_ascii_strncasecmp(filename, "cdb://", sizeof("cdb://") - 1) == 0) {
  157. filename += sizeof("cdb://") - 1;
  158. }
  159. if ((fd = open(filename, O_RDONLY)) == -1) {
  160. msg_warn("cannot open cdb: %s, %s", filename, strerror(errno));
  161. lua_pushnil(L);
  162. }
  163. else {
  164. cdb = g_malloc0(sizeof(struct cdb));
  165. cdb->filename = g_strdup(filename);
  166. if (cdb_init(cdb, fd) == -1) {
  167. g_free(cdb->filename);
  168. g_free(cdb);
  169. msg_warn("cannot open cdb: %s, %s", filename, strerror(errno));
  170. lua_pushnil(L);
  171. }
  172. else {
  173. #ifdef HAVE_READAHEAD
  174. struct stat st;
  175. /*
  176. * Do not readahead more than 100mb,
  177. * which is enough for the vast majority of the use cases
  178. */
  179. static const size_t max_readahead = 100 * 0x100000;
  180. if (fstat(cdb_fileno(cdb), &st) != 1) {
  181. /* Must always be true because cdb_init calls it as well */
  182. if (readahead(cdb_fileno(cdb), 0, MIN(max_readahead, st.st_size)) == -1) {
  183. msg_warn("cannot readahead cdb: %s, %s", filename, strerror(errno));
  184. }
  185. }
  186. #endif
  187. if (ev_base) {
  188. cdb_add_timer(cdb, ev_base, CDB_REFRESH_TIME);
  189. }
  190. pcdb = lua_newuserdata(L, sizeof(struct cdb *));
  191. rspamd_lua_setclass(L, rspamd_cdb_classname, -1);
  192. *pcdb = cdb;
  193. }
  194. }
  195. return 1;
  196. }
  197. static gint
  198. lua_cdb_get_name(lua_State *L)
  199. {
  200. struct cdb *cdb = lua_check_cdb(L, 1);
  201. if (!cdb) {
  202. lua_error(L);
  203. return 1;
  204. }
  205. lua_pushstring(L, cdb->filename);
  206. return 1;
  207. }
  208. static gint
  209. lua_cdb_lookup(lua_State *L)
  210. {
  211. struct cdb *cdb = lua_check_cdb(L, 1);
  212. gsize klen;
  213. const gchar *what = lua_cdb_get_input(L, 2, &klen);
  214. if (!cdb || what == NULL) {
  215. return lua_error(L);
  216. }
  217. if (cdb_find(cdb, what, klen) > 0) {
  218. /* Extract and push value to lua as string */
  219. lua_pushlstring(L, cdb_getdata(cdb), cdb_datalen(cdb));
  220. }
  221. else {
  222. lua_pushnil(L);
  223. }
  224. return 1;
  225. }
  226. static gint
  227. lua_cdb_destroy(lua_State *L)
  228. {
  229. struct cdb *cdb = lua_check_cdb(L, 1);
  230. if (cdb) {
  231. cdb_free(cdb);
  232. if (cdb->cdb_fd != -1) {
  233. (void) close(cdb->cdb_fd);
  234. }
  235. g_free(cdb->filename);
  236. g_free(cdb);
  237. }
  238. return 0;
  239. }
  240. static gint
  241. lua_cdb_build(lua_State *L)
  242. {
  243. const char *filename = luaL_checkstring(L, 1);
  244. int fd, mode = 00755;
  245. if (filename == NULL) {
  246. return luaL_error(L, "invalid arguments, filename expected");
  247. }
  248. /* If file begins with cdb://, just skip it */
  249. if (g_ascii_strncasecmp(filename, "cdb://", sizeof("cdb://") - 1) == 0) {
  250. filename += sizeof("cdb://") - 1;
  251. }
  252. if (lua_isnumber(L, 2)) {
  253. mode = lua_tointeger(L, 2);
  254. }
  255. fd = rspamd_file_xopen(filename, O_RDWR | O_CREAT | O_TRUNC, mode, 0);
  256. if (fd == -1) {
  257. lua_pushnil(L);
  258. lua_pushfstring(L, "cannot open cdb: %s, %s", filename, strerror(errno));
  259. return 2;
  260. }
  261. struct cdb_make *cdbm = lua_newuserdata(L, sizeof(struct cdb_make));
  262. g_assert(cdb_make_start(cdbm, fd) == 0);
  263. rspamd_lua_setclass(L, rspamd_cdb_builder_classname, -1);
  264. return 1;
  265. }
  266. static gint
  267. lua_cdb_builder_add(lua_State *L)
  268. {
  269. struct cdb_make *cdbm = lua_check_cdb_builder(L, 1);
  270. gsize data_sz, key_sz;
  271. const char *key = lua_cdb_get_input(L, 2, &key_sz);
  272. const char *data = lua_cdb_get_input(L, 3, &data_sz);
  273. if (cdbm == NULL || key == NULL || data == NULL || cdbm->cdb_fd == -1) {
  274. return luaL_error(L, "invalid arguments");
  275. }
  276. if (cdb_make_add(cdbm, key, key_sz, data, data_sz) == -1) {
  277. lua_pushvalue(L, 1);
  278. lua_pushfstring(L, "cannot push value to cdb: %s", strerror(errno));
  279. return 2;
  280. }
  281. /* Allow chaining */
  282. lua_pushvalue(L, 1);
  283. return 1;
  284. }
  285. static gint
  286. lua_cdb_builder_finalize(lua_State *L)
  287. {
  288. struct cdb_make *cdbm = lua_check_cdb_builder(L, 1);
  289. if (cdbm == NULL || cdbm->cdb_fd == -1) {
  290. return luaL_error(L, "invalid arguments");
  291. }
  292. if (cdb_make_finish(cdbm) == -1) {
  293. lua_pushvalue(L, 1);
  294. lua_pushfstring(L, "cannot finish value to cdb: %s", strerror(errno));
  295. return 2;
  296. }
  297. close(cdbm->cdb_fd);
  298. cdbm->cdb_fd = -1; /* To distinguish finalized object */
  299. /* Allow chaining */
  300. lua_pushvalue(L, 1);
  301. return 1;
  302. }
  303. static gint
  304. lua_cdb_builder_dtor(lua_State *L)
  305. {
  306. struct cdb_make *cdbm = lua_check_cdb_builder(L, 1);
  307. if (cdbm == NULL) {
  308. return luaL_error(L, "invalid arguments");
  309. }
  310. if (cdbm->cdb_fd != -1) {
  311. cdb_make_finish(cdbm);
  312. close(cdbm->cdb_fd);
  313. cdbm->cdb_fd = -1; /* Finalized object */
  314. }
  315. return 0;
  316. }
  317. static gint
  318. lua_load_cdb(lua_State *L)
  319. {
  320. lua_newtable(L);
  321. luaL_register(L, NULL, cdblib_f);
  322. return 1;
  323. }
  324. void luaopen_cdb(lua_State *L)
  325. {
  326. rspamd_lua_new_class(L, rspamd_cdb_classname, cdblib_m);
  327. lua_pop(L, 1);
  328. rspamd_lua_new_class(L, rspamd_cdb_builder_classname, cdbbuilderlib_m);
  329. lua_pop(L, 1);
  330. rspamd_lua_add_preload(L, "rspamd_cdb", lua_load_cdb);
  331. }