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 12KB

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