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_common.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #ifndef RSPAMD_LUA_H
  2. #define RSPAMD_LUA_H
  3. #include "config.h"
  4. #include <lua.h>
  5. #include <lauxlib.h>
  6. #include <lualib.h>
  7. #include <stdbool.h>
  8. #include "rspamd.h"
  9. #include "ucl.h"
  10. #include "lua_ucl.h"
  11. #ifndef lua_open
  12. #define lua_open() luaL_newstate ()
  13. #endif
  14. #ifndef luaL_reg
  15. #define luaL_reg luaL_Reg
  16. #endif
  17. #define LUA_ENUM(L, name, val) \
  18. lua_pushlstring (L, # name, sizeof(# name) - 1); \
  19. lua_pushinteger (L, val); \
  20. lua_settable (L, -3);
  21. #if LUA_VERSION_NUM > 501 && !defined LUA_COMPAT_MODULE
  22. static inline void
  23. luaL_register (lua_State *L, const gchar *name, const struct luaL_reg *methods)
  24. {
  25. if (name != NULL) {
  26. lua_newtable (L);
  27. }
  28. luaL_setfuncs (L, methods, 0);
  29. if (name != NULL) {
  30. lua_pushvalue (L, -1);
  31. lua_setglobal (L, name);
  32. }
  33. }
  34. #endif
  35. /* Interface definitions */
  36. #define LUA_FUNCTION_DEF(class, name) static int lua_ ## class ## _ ## name ( \
  37. lua_State * L)
  38. #define LUA_PUBLIC_FUNCTION_DEF(class, name) int lua_ ## class ## _ ## name ( \
  39. lua_State * L)
  40. #define LUA_INTERFACE_DEF(class, name) { # name, lua_ ## class ## _ ## name }
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. extern const luaL_reg null_reg[];
  45. #define RSPAMD_LUA_API_VERSION 12
  46. /* Locked lua state with mutex */
  47. struct lua_locked_state {
  48. lua_State *L;
  49. rspamd_mutex_t *m;
  50. };
  51. /**
  52. * Lua IP address structure
  53. */
  54. struct rspamd_lua_ip {
  55. rspamd_inet_addr_t *addr;
  56. };
  57. #define RSPAMD_TEXT_FLAG_OWN (1u << 0u)
  58. #define RSPAMD_TEXT_FLAG_MMAPED (1u << 1u)
  59. #define RSPAMD_TEXT_FLAG_WIPE (1u << 2u)
  60. #define RSPAMD_TEXT_FLAG_SYSMALLOC (1u << 3u)
  61. struct rspamd_lua_text {
  62. const gchar *start;
  63. guint len;
  64. guint flags;
  65. };
  66. struct rspamd_lua_url {
  67. struct rspamd_url *url;
  68. };
  69. struct rspamd_lua_regexp {
  70. rspamd_regexp_t *re;
  71. gchar *module;
  72. gchar *re_pattern;
  73. gsize match_limit;
  74. gint re_flags;
  75. };
  76. struct rspamd_map;
  77. struct lua_map_callback_data;
  78. struct radix_tree_compressed;
  79. struct rspamd_mime_header;
  80. enum rspamd_lua_map_type {
  81. RSPAMD_LUA_MAP_RADIX = 0,
  82. RSPAMD_LUA_MAP_SET,
  83. RSPAMD_LUA_MAP_HASH,
  84. RSPAMD_LUA_MAP_REGEXP,
  85. RSPAMD_LUA_MAP_REGEXP_MULTIPLE,
  86. RSPAMD_LUA_MAP_CALLBACK,
  87. RSPAMD_LUA_MAP_UNKNOWN,
  88. };
  89. struct rspamd_lua_map {
  90. struct rspamd_map *map;
  91. enum rspamd_lua_map_type type;
  92. guint flags;
  93. union {
  94. struct rspamd_radix_map_helper *radix;
  95. struct rspamd_hash_map_helper *hash;
  96. struct rspamd_regexp_map_helper *re_map;
  97. struct lua_map_callback_data *cbdata;
  98. } data;
  99. };
  100. struct rspamd_lua_cached_entry {
  101. gint ref;
  102. guchar id[4];
  103. };
  104. /* Common utility functions */
  105. /**
  106. * Create and register new class
  107. */
  108. void rspamd_lua_new_class (lua_State *L,
  109. const gchar *classname,
  110. const struct luaL_reg *methods);
  111. /**
  112. * Create and register new class with static methods
  113. */
  114. void rspamd_lua_new_class_full (lua_State *L,
  115. const gchar *classname,
  116. const gchar *static_name,
  117. const struct luaL_reg *methods,
  118. const struct luaL_reg *func);
  119. /**
  120. * Set class name for object at @param objidx position
  121. */
  122. void rspamd_lua_setclass (lua_State *L, const gchar *classname, gint objidx);
  123. /**
  124. * Set index of table to value (like t['index'] = value)
  125. */
  126. void rspamd_lua_table_set (lua_State *L, const gchar *index, const gchar *value);
  127. /**
  128. * Get string value of index in a table (return t['index'])
  129. */
  130. const gchar *rspamd_lua_table_get (lua_State *L, const gchar *index);
  131. /**
  132. * Convert classname to string
  133. */
  134. gint rspamd_lua_class_tostring (lua_State *L);
  135. /**
  136. * Check whether the argument at specified index is of the specified class
  137. */
  138. gpointer rspamd_lua_check_class (lua_State *L, gint index, const gchar *name);
  139. /**
  140. * Initialize lua and bindings
  141. */
  142. lua_State *rspamd_lua_init (bool wipe_mem);
  143. void rspamd_lua_start_gc (struct rspamd_config *cfg);
  144. /**
  145. * Sets field in a global variable
  146. * @param L
  147. * @param global_name
  148. * @param field_name
  149. * @param new_elt
  150. */
  151. void
  152. rspamd_plugins_table_push_elt (lua_State *L, const gchar *field_name,
  153. const gchar *new_elt);
  154. /**
  155. * Load and initialize lua plugins
  156. */
  157. gboolean
  158. rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load);
  159. /**
  160. * Initialize new locked lua_State structure
  161. */
  162. struct lua_locked_state *rspamd_init_lua_locked (struct rspamd_config *cfg);
  163. /**
  164. * Free locked state structure
  165. */
  166. void rspamd_free_lua_locked (struct lua_locked_state *st);
  167. /**
  168. * Push lua ip address
  169. */
  170. void rspamd_lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr);
  171. /**
  172. * Push rspamd task structure to lua
  173. */
  174. void rspamd_lua_task_push (lua_State *L, struct rspamd_task *task);
  175. /**
  176. * Return lua ip structure at the specified address
  177. */
  178. struct rspamd_lua_ip *lua_check_ip (lua_State *L, gint pos);
  179. struct rspamd_lua_text *lua_check_text (lua_State *L, gint pos);
  180. struct rspamd_lua_regexp *lua_check_regexp (lua_State *L, gint pos);
  181. enum rspamd_lua_task_header_type {
  182. RSPAMD_TASK_HEADER_PUSH_SIMPLE = 0,
  183. RSPAMD_TASK_HEADER_PUSH_RAW,
  184. RSPAMD_TASK_HEADER_PUSH_FULL,
  185. RSPAMD_TASK_HEADER_PUSH_COUNT,
  186. };
  187. gint rspamd_lua_push_header (lua_State *L,
  188. struct rspamd_mime_header *h,
  189. enum rspamd_lua_task_header_type how);
  190. /**
  191. * Push specific header to lua
  192. */
  193. gint rspamd_lua_push_header_array (lua_State *L,
  194. const gchar *name,
  195. struct rspamd_mime_header *rh,
  196. enum rspamd_lua_task_header_type how,
  197. gboolean strong);
  198. /**
  199. * Check for task at the specified position
  200. */
  201. struct rspamd_task *lua_check_task (lua_State *L, gint pos);
  202. struct rspamd_task *lua_check_task_maybe (lua_State *L, gint pos);
  203. struct rspamd_lua_map *lua_check_map (lua_State *L, gint pos);
  204. /**
  205. * Push ip address from a string (nil is pushed if a string cannot be converted)
  206. */
  207. void rspamd_lua_ip_push_fromstring (lua_State *L, const gchar *ip_str);
  208. /**
  209. * Create type error
  210. */
  211. int rspamd_lua_typerror (lua_State *L, int narg, const char *tname);
  212. /**
  213. * Open libraries functions
  214. */
  215. /**
  216. * Add preload function
  217. */
  218. void rspamd_lua_add_preload (lua_State *L, const gchar *name, lua_CFunction func);
  219. void luaopen_task (lua_State *L);
  220. void luaopen_config (lua_State *L);
  221. void luaopen_map (lua_State *L);
  222. void luaopen_trie (lua_State *L);
  223. void luaopen_textpart (lua_State *L);
  224. void luaopen_mimepart (lua_State *L);
  225. void luaopen_image (lua_State *L);
  226. void luaopen_url (lua_State *L);
  227. void luaopen_classifier (lua_State *L);
  228. void luaopen_statfile (lua_State *L);
  229. void luaopen_regexp (lua_State *L);
  230. void luaopen_cdb (lua_State *L);
  231. void luaopen_xmlrpc (lua_State *L);
  232. void luaopen_http (lua_State *L);
  233. void luaopen_redis (lua_State *L);
  234. void luaopen_upstream (lua_State *L);
  235. void luaopen_mempool (lua_State *L);
  236. void luaopen_dns_resolver (lua_State *L);
  237. void luaopen_rsa (lua_State *L);
  238. void luaopen_ip (lua_State *L);
  239. void luaopen_expression (lua_State *L);
  240. void luaopen_logger (lua_State *L);
  241. void luaopen_text (lua_State *L);
  242. void luaopen_util (lua_State *L);
  243. void luaopen_tcp (lua_State *L);
  244. void luaopen_html (lua_State *L);
  245. void luaopen_sqlite3 (lua_State *L);
  246. void luaopen_cryptobox (lua_State *L);
  247. void luaopen_dns (lua_State *L);
  248. void luaopen_udp (lua_State *L);
  249. void luaopen_worker (lua_State *L);
  250. void luaopen_kann (lua_State *L);
  251. void rspamd_lua_dostring (const gchar *line);
  252. double rspamd_lua_normalize (struct rspamd_config *cfg,
  253. long double score,
  254. void *params);
  255. /* Config file functions */
  256. void rspamd_lua_post_load_config (struct rspamd_config *cfg);
  257. gboolean rspamd_lua_handle_param (struct rspamd_task *task,
  258. gchar *mname,
  259. gchar *optname,
  260. enum lua_var_type expected_type,
  261. gpointer *res);
  262. gboolean rspamd_lua_check_condition (struct rspamd_config *cfg,
  263. const gchar *condition);
  264. void rspamd_lua_dumpstack (lua_State *L);
  265. /* Set lua path according to the configuration */
  266. void rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj,
  267. GHashTable *vars);
  268. /* Set some lua globals */
  269. gboolean rspamd_lua_set_env (lua_State *L, GHashTable *vars, char **lua_env,
  270. GError **err);
  271. void rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L);
  272. struct memory_pool_s *rspamd_lua_check_mempool (lua_State *L, gint pos);
  273. struct rspamd_config *lua_check_config (lua_State *L, gint pos);
  274. struct rspamd_async_session *lua_check_session (lua_State *L, gint pos);
  275. struct ev_loop *lua_check_ev_base (lua_State *L, gint pos);
  276. struct rspamd_dns_resolver *lua_check_dns_resolver (lua_State *L, gint pos);
  277. enum rspamd_lua_parse_arguments_flags {
  278. RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT = 0,
  279. RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING,
  280. };
  281. /**
  282. * Extract an arguments from lua table according to format string. Supported arguments are:
  283. * [*]key=S|I|N|B|V|U{a-z};[key=...]
  284. * - S - const char *
  285. * - I - gint64_t
  286. * - N - double
  287. * - B - boolean
  288. * - V - size_t + const char *
  289. * - U{classname} - userdata of the following class (stored in gpointer)
  290. * - F - function
  291. * - O - ucl_object_t *
  292. *
  293. * If any of keys is prefixed with `*` then it is treated as required argument
  294. * @param L lua state
  295. * @param pos at which pos start extraction
  296. * @param err error pointer
  297. * @param how extraction type
  298. * @param extraction_pattern static pattern
  299. * @return TRUE if a table has been parsed
  300. */
  301. gboolean rspamd_lua_parse_table_arguments (lua_State *L, gint pos,
  302. GError **err,
  303. enum rspamd_lua_parse_arguments_flags how,
  304. const gchar *extraction_pattern, ...);
  305. gint rspamd_lua_traceback (lua_State *L);
  306. /**
  307. * Returns stack trace as a string. Caller should clear memory.
  308. * @param L
  309. * @return
  310. */
  311. void rspamd_lua_get_traceback_string (lua_State *L, luaL_Buffer *buf);
  312. /**
  313. * Returns size of table at position `tbl_pos`
  314. */
  315. guint rspamd_lua_table_size (lua_State *L, gint tbl_pos);
  316. void lua_push_emails_address_list (lua_State *L, GPtrArray *addrs, int flags);
  317. #define TRACE_POINTS 6
  318. struct lua_logger_trace {
  319. gint cur_level;
  320. gconstpointer traces[TRACE_POINTS];
  321. };
  322. enum lua_logger_escape_type {
  323. LUA_ESCAPE_NONE = (0u),
  324. LUA_ESCAPE_UNPRINTABLE = (1u << 0u),
  325. LUA_ESCAPE_NEWLINES = (1u << 1u),
  326. LUA_ESCAPE_8BIT = (1u << 2u),
  327. };
  328. #define LUA_ESCAPE_LOG (LUA_ESCAPE_UNPRINTABLE|LUA_ESCAPE_NEWLINES)
  329. #define LUA_ESCAPE_ALL (LUA_ESCAPE_UNPRINTABLE|LUA_ESCAPE_NEWLINES|LUA_ESCAPE_8BIT)
  330. /**
  331. * Log lua object to string
  332. * @param L
  333. * @param pos
  334. * @param outbuf
  335. * @param len
  336. * @return
  337. */
  338. gsize lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf,
  339. gsize len, struct lua_logger_trace *trace,
  340. enum lua_logger_escape_type esc_type);
  341. /**
  342. * Safely checks userdata to match specified class
  343. * @param L
  344. * @param pos
  345. * @param classname
  346. */
  347. void *rspamd_lua_check_udata (lua_State *L, gint pos, const gchar *classname);
  348. /**
  349. * Safely checks userdata to match specified class
  350. * @param L
  351. * @param pos
  352. * @param classname
  353. */
  354. void *rspamd_lua_check_udata_maybe (lua_State *L, gint pos, const gchar *classname);
  355. /**
  356. * Call finishing script with the specified task
  357. * @param sc
  358. * @param task
  359. */
  360. void lua_call_finish_script (struct rspamd_config_cfg_lua_script *sc,
  361. struct rspamd_task *task);
  362. /**
  363. * Run post-load operations
  364. * @param L
  365. * @param cfg
  366. * @param ev_base
  367. */
  368. void rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg,
  369. struct ev_loop *ev_base, struct rspamd_worker *w);
  370. void rspamd_lua_run_config_post_init (lua_State *L, struct rspamd_config *cfg);
  371. void rspamd_lua_run_config_unload (lua_State *L, struct rspamd_config *cfg);
  372. /**
  373. * Adds new destructor for a local function for specific pool
  374. * @param L
  375. * @param pool
  376. * @param ref
  377. */
  378. void rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool,
  379. gint ref);
  380. /**
  381. * Tries to load some module using `require` and get some method from it
  382. * @param L
  383. * @param modname
  384. * @param funcname
  385. * @return TRUE if function exists in that module, the function is pushed in stack, otherwise stack is unchanged and FALSE is returned
  386. */
  387. gboolean rspamd_lua_require_function (lua_State *L, const gchar *modname,
  388. const gchar *funcname);
  389. /**
  390. * Tries to load redis server definition from ucl object specified
  391. * @param L
  392. * @param obj
  393. * @param cfg
  394. * @return
  395. */
  396. gboolean rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj,
  397. struct rspamd_config *cfg, gint *ref_id);
  398. struct rspamd_stat_token_s;
  399. /**
  400. * Pushes a single word into Lua
  401. * @param L
  402. * @param word
  403. */
  404. void rspamd_lua_push_full_word (lua_State *L, struct rspamd_stat_token_s *word);
  405. enum rspamd_lua_words_type {
  406. RSPAMD_LUA_WORDS_STEM = 0,
  407. RSPAMD_LUA_WORDS_NORM,
  408. RSPAMD_LUA_WORDS_RAW,
  409. RSPAMD_LUA_WORDS_FULL,
  410. RSPAMD_LUA_WORDS_MAX
  411. };
  412. /**
  413. * Pushes words (rspamd_stat_token_t) to Lua
  414. * @param L
  415. * @param words
  416. * @param how
  417. */
  418. gint rspamd_lua_push_words (lua_State *L, GArray *words,
  419. enum rspamd_lua_words_type how);
  420. /* Paths defs */
  421. #define RSPAMD_CONFDIR_INDEX "CONFDIR"
  422. #define RSPAMD_LOCAL_CONFDIR_INDEX "LOCAL_CONFDIR"
  423. #define RSPAMD_RUNDIR_INDEX "RUNDIR"
  424. #define RSPAMD_DBDIR_INDEX "DBDIR"
  425. #define RSPAMD_LOGDIR_INDEX "LOGDIR"
  426. #define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR"
  427. #define RSPAMD_SHAREDIR_INDEX "SHAREDIR"
  428. #define RSPAMD_RULESDIR_INDEX "RULESDIR"
  429. #define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR"
  430. #define RSPAMD_WWWDIR_INDEX "WWWDIR"
  431. #define RSPAMD_PREFIX_INDEX "PREFIX"
  432. #define RSPAMD_VERSION_INDEX "VERSION"
  433. #ifdef WITH_LUA_TRACE
  434. extern ucl_object_t *lua_traces;
  435. #define LUA_TRACE_POINT do { \
  436. ucl_object_t *func_obj; \
  437. if (lua_traces == NULL) { lua_traces = ucl_object_typed_new (UCL_OBJECT); } \
  438. func_obj = (ucl_object_t *)ucl_object_lookup (lua_traces, G_STRFUNC); \
  439. if (func_obj == NULL) { \
  440. func_obj = ucl_object_typed_new (UCL_INT); \
  441. ucl_object_insert_key (lua_traces, func_obj, G_STRFUNC, 0, false); \
  442. } \
  443. func_obj->value.iv ++; \
  444. } while(0)
  445. #else
  446. #define LUA_TRACE_POINT do {} while(0)
  447. #endif
  448. #ifdef __cplusplus
  449. }
  450. #endif
  451. #endif /* RSPAMD_LUA_H */