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.c 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661
  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 "lua_compress.h"
  18. #include "lptree.h"
  19. #include "utlist.h"
  20. #include "unix-std.h"
  21. #include "ottery.h"
  22. #include "lua_thread_pool.h"
  23. #include "libstat/stat_api.h"
  24. #include "libserver/rspamd_control.h"
  25. #include <math.h>
  26. /* Lua module init function */
  27. #define MODULE_INIT_FUNC "module_init"
  28. #ifdef WITH_LUA_TRACE
  29. ucl_object_t *lua_traces;
  30. #endif
  31. const luaL_reg null_reg[] = {
  32. {"__tostring", rspamd_lua_class_tostring},
  33. {NULL, NULL}};
  34. static const char rspamd_modules_state_global[] = "rspamd_plugins_state";
  35. static GQuark
  36. lua_error_quark(void)
  37. {
  38. return g_quark_from_static_string("lua-routines");
  39. }
  40. /*
  41. * Used to map string to a pointer
  42. */
  43. KHASH_INIT(lua_class_set, int, int, 1, kh_int_hash_func, kh_int_hash_equal);
  44. struct rspamd_lua_context {
  45. lua_State *L;
  46. khash_t(lua_class_set) * classes;
  47. struct rspamd_lua_context *prev, *next; /* Expensive but we usually have exactly one lua state */
  48. };
  49. struct rspamd_lua_context *rspamd_lua_global_ctx = NULL;
  50. #define RSPAMD_LUA_NCLASSES 64
  51. static inline struct rspamd_lua_context *
  52. rspamd_lua_ctx_by_state(lua_State *L)
  53. {
  54. struct rspamd_lua_context *cur;
  55. DL_FOREACH(rspamd_lua_global_ctx, cur)
  56. {
  57. if (cur->L == L) {
  58. return cur;
  59. }
  60. }
  61. /* When we are using thread pool, this is the case... */
  62. return rspamd_lua_global_ctx;
  63. }
  64. /* Util functions */
  65. /**
  66. * Create new class and store metatable on top of the stack (must be popped if not needed)
  67. * @param L
  68. * @param classname name of class, **MUST** be a static string
  69. * @param func table of class methods
  70. */
  71. void rspamd_lua_new_class(lua_State *L,
  72. const gchar *classname,
  73. const struct luaL_reg *methods)
  74. {
  75. khiter_t k;
  76. gint r, nmethods = 0;
  77. gboolean seen_index = false;
  78. struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L);
  79. if (methods) {
  80. for (;;) {
  81. if (methods[nmethods].name != NULL) {
  82. if (strcmp(methods[nmethods].name, "__index") == 0) {
  83. seen_index = true;
  84. }
  85. nmethods++;
  86. }
  87. else {
  88. break;
  89. }
  90. }
  91. }
  92. lua_createtable(L, 0, 3 + nmethods);
  93. if (!seen_index) {
  94. lua_pushstring(L, "__index");
  95. lua_pushvalue(L, -2); /* pushes the metatable */
  96. lua_settable(L, -3); /* metatable.__index = metatable */
  97. }
  98. lua_pushstring(L, "class");
  99. lua_pushstring(L, classname);
  100. lua_rawset(L, -3);
  101. if (methods) {
  102. luaL_register(L, NULL, methods); /* pushes all methods as MT fields */
  103. }
  104. lua_pushvalue(L, -1); /* Preserves metatable */
  105. int offset = luaL_ref(L, LUA_REGISTRYINDEX);
  106. k = kh_put(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname), &r);
  107. kh_value(ctx->classes, k) = offset;
  108. /* Set class name as a metatable[1] converted to an integer for faster comparisons */
  109. lua_pushinteger(L, GPOINTER_TO_INT(classname));
  110. lua_rawseti(L, -2, 1);
  111. /* MT is left on stack ! */
  112. }
  113. static const gchar *
  114. rspamd_lua_class_tostring_buf(lua_State *L, gboolean print_pointer, gint pos)
  115. {
  116. static gchar buf[64];
  117. const gchar *ret = NULL;
  118. gint pop = 0;
  119. if (!lua_getmetatable(L, pos)) {
  120. goto err;
  121. }
  122. pop++;
  123. lua_pushstring(L, "class");
  124. lua_gettable(L, -2);
  125. pop++;
  126. if (!lua_isstring(L, -1)) {
  127. goto err;
  128. }
  129. if (print_pointer) {
  130. rspamd_snprintf(buf, sizeof(buf), "%s(%p)", lua_tostring(L, -1),
  131. lua_touserdata(L, 1));
  132. }
  133. else {
  134. rspamd_snprintf(buf, sizeof(buf), "%s", lua_tostring(L, -1));
  135. }
  136. ret = buf;
  137. err:
  138. lua_pop(L, pop);
  139. return ret;
  140. }
  141. gint rspamd_lua_class_tostring(lua_State *L)
  142. {
  143. const gchar *p;
  144. p = rspamd_lua_class_tostring_buf(L, TRUE, 1);
  145. if (!p) {
  146. lua_pushstring(L, "invalid object passed to 'lua_common.c:__tostring'");
  147. return lua_error(L);
  148. }
  149. lua_pushstring(L, p);
  150. return 1;
  151. }
  152. void rspamd_lua_setclass(lua_State *L, const gchar *classname, gint objidx)
  153. {
  154. khiter_t k;
  155. struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L);
  156. k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname));
  157. g_assert(k != kh_end(ctx->classes));
  158. lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k));
  159. if (objidx < 0) {
  160. objidx--;
  161. }
  162. lua_setmetatable(L, objidx);
  163. }
  164. void rspamd_lua_class_metatable(lua_State *L, const gchar *classname)
  165. {
  166. khiter_t k;
  167. struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L);
  168. k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname));
  169. g_assert(k != kh_end(ctx->classes));
  170. lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k));
  171. }
  172. void rspamd_lua_add_metamethod(lua_State *L, const gchar *classname,
  173. luaL_Reg *meth)
  174. {
  175. khiter_t k;
  176. struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L);
  177. k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname));
  178. g_assert(k != kh_end(ctx->classes));
  179. lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k));
  180. lua_pushcfunction(L, meth->func);
  181. lua_setfield(L, -2, meth->name);
  182. lua_pop(L, 1); /* remove metatable */
  183. }
  184. /* assume that table is at the top */
  185. void rspamd_lua_table_set(lua_State *L, const gchar *index, const gchar *value)
  186. {
  187. lua_pushstring(L, index);
  188. if (value) {
  189. lua_pushstring(L, value);
  190. }
  191. else {
  192. lua_pushnil(L);
  193. }
  194. lua_settable(L, -3);
  195. }
  196. const gchar *
  197. rspamd_lua_table_get(lua_State *L, const gchar *index)
  198. {
  199. const gchar *result;
  200. lua_pushstring(L, index);
  201. lua_gettable(L, -2);
  202. if (!lua_isstring(L, -1)) {
  203. return NULL;
  204. }
  205. result = lua_tostring(L, -1);
  206. lua_pop(L, 1);
  207. return result;
  208. }
  209. static void
  210. lua_add_actions_global(lua_State *L)
  211. {
  212. gint i;
  213. lua_newtable(L);
  214. for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
  215. lua_pushstring(L, rspamd_action_to_str(i));
  216. lua_pushinteger(L, i);
  217. lua_settable(L, -3);
  218. }
  219. /* Set global table */
  220. lua_setglobal(L, "rspamd_actions");
  221. }
  222. #ifndef __APPLE__
  223. #define OS_SO_SUFFIX ".so"
  224. #else
  225. #define OS_SO_SUFFIX ".dylib"
  226. #endif
  227. void rspamd_lua_set_path(lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars)
  228. {
  229. const gchar *old_path, *additional_path = NULL;
  230. const ucl_object_t *opts = NULL;
  231. const gchar *rulesdir = RSPAMD_RULESDIR,
  232. *lualibdir = RSPAMD_LUALIBDIR,
  233. *libdir = RSPAMD_LIBDIR;
  234. const gchar *t;
  235. gchar path_buf[PATH_MAX];
  236. lua_getglobal(L, "package");
  237. lua_getfield(L, -1, "path");
  238. old_path = luaL_checkstring(L, -1);
  239. if (strstr(old_path, RSPAMD_LUALIBDIR) != NULL) {
  240. /* Path has been already set, do not touch it */
  241. lua_pop(L, 2);
  242. return;
  243. }
  244. if (cfg_obj) {
  245. opts = ucl_object_lookup(cfg_obj, "options");
  246. if (opts != NULL) {
  247. opts = ucl_object_lookup(opts, "lua_path");
  248. if (opts != NULL && ucl_object_type(opts) == UCL_STRING) {
  249. additional_path = ucl_object_tostring(opts);
  250. }
  251. }
  252. }
  253. if (additional_path) {
  254. rspamd_snprintf(path_buf, sizeof(path_buf),
  255. "%s;"
  256. "%s",
  257. additional_path, old_path);
  258. }
  259. else {
  260. /* Try environment */
  261. t = getenv("RULESDIR");
  262. if (t) {
  263. rulesdir = t;
  264. }
  265. t = getenv("LUALIBDIR");
  266. if (t) {
  267. lualibdir = t;
  268. }
  269. t = getenv("LIBDIR");
  270. if (t) {
  271. libdir = t;
  272. }
  273. t = getenv("RSPAMD_LIBDIR");
  274. if (t) {
  275. libdir = t;
  276. }
  277. if (vars) {
  278. t = g_hash_table_lookup(vars, "RULESDIR");
  279. if (t) {
  280. rulesdir = t;
  281. }
  282. t = g_hash_table_lookup(vars, "LUALIBDIR");
  283. if (t) {
  284. lualibdir = t;
  285. }
  286. t = g_hash_table_lookup(vars, "LIBDIR");
  287. if (t) {
  288. libdir = t;
  289. }
  290. t = g_hash_table_lookup(vars, "RSPAMD_LIBDIR");
  291. if (t) {
  292. libdir = t;
  293. }
  294. }
  295. rspamd_snprintf(path_buf, sizeof(path_buf),
  296. "%s/lua/?.lua;"
  297. "%s/?.lua;"
  298. "%s/?.lua;"
  299. "%s/?/init.lua;"
  300. "%s",
  301. RSPAMD_CONFDIR,
  302. rulesdir,
  303. lualibdir, lualibdir,
  304. old_path);
  305. }
  306. lua_pop(L, 1);
  307. lua_pushstring(L, path_buf);
  308. lua_setfield(L, -2, "path");
  309. lua_getglobal(L, "package");
  310. lua_getfield(L, -1, "cpath");
  311. old_path = luaL_checkstring(L, -1);
  312. additional_path = NULL;
  313. if (opts != NULL) {
  314. opts = ucl_object_lookup(opts, "lua_cpath");
  315. if (opts != NULL && ucl_object_type(opts) == UCL_STRING) {
  316. additional_path = ucl_object_tostring(opts);
  317. }
  318. }
  319. if (additional_path) {
  320. rspamd_snprintf(path_buf, sizeof(path_buf),
  321. "%s/?%s;"
  322. "%s",
  323. additional_path,
  324. OS_SO_SUFFIX,
  325. old_path);
  326. }
  327. else {
  328. rspamd_snprintf(path_buf, sizeof(path_buf),
  329. "%s/?%s;"
  330. "%s",
  331. libdir,
  332. OS_SO_SUFFIX,
  333. old_path);
  334. }
  335. lua_pop(L, 1);
  336. lua_pushstring(L, path_buf);
  337. lua_setfield(L, -2, "cpath");
  338. lua_pop(L, 1);
  339. }
  340. static gint
  341. rspamd_lua_cmp_version_components(const gchar *comp1, const gchar *comp2)
  342. {
  343. guint v1, v2;
  344. v1 = strtoul(comp1, NULL, 10);
  345. v2 = strtoul(comp2, NULL, 10);
  346. return v1 - v2;
  347. }
  348. static int
  349. rspamd_lua_rspamd_version_cmp(lua_State *L)
  350. {
  351. const gchar *ver;
  352. gchar **components;
  353. gint ret = 0;
  354. if (lua_type(L, 2) == LUA_TSTRING) {
  355. ver = lua_tostring(L, 2);
  356. components = g_strsplit_set(ver, ".-_", -1);
  357. if (!components) {
  358. return luaL_error(L, "invalid arguments to 'cmp': %s", ver);
  359. }
  360. if (components[0]) {
  361. ret = rspamd_lua_cmp_version_components(components[0],
  362. RSPAMD_VERSION_MAJOR);
  363. }
  364. if (ret) {
  365. goto set;
  366. }
  367. if (components[1]) {
  368. ret = rspamd_lua_cmp_version_components(components[1],
  369. RSPAMD_VERSION_MINOR);
  370. }
  371. if (ret) {
  372. goto set;
  373. }
  374. /*
  375. * XXX: we don't compare git releases assuming that it is meaningless
  376. */
  377. }
  378. else {
  379. return luaL_error(L, "invalid arguments to 'cmp'");
  380. }
  381. set:
  382. g_strfreev(components);
  383. lua_pushinteger(L, ret);
  384. return 1;
  385. }
  386. static int
  387. rspamd_lua_rspamd_version_numeric(lua_State *L)
  388. {
  389. static gint64 version_num = RSPAMD_VERSION_NUM;
  390. const gchar *type;
  391. if (lua_gettop(L) >= 2 && lua_type(L, 1) == LUA_TSTRING) {
  392. type = lua_tostring(L, 1);
  393. if (g_ascii_strcasecmp(type, "short") == 0) {
  394. version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 +
  395. RSPAMD_VERSION_MINOR_NUM * 100 +
  396. RSPAMD_VERSION_PATCH_NUM * 10;
  397. }
  398. else if (g_ascii_strcasecmp(type, "main") == 0) {
  399. version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 +
  400. RSPAMD_VERSION_MINOR_NUM * 100 +
  401. RSPAMD_VERSION_PATCH_NUM * 10;
  402. }
  403. else if (g_ascii_strcasecmp(type, "major") == 0) {
  404. version_num = RSPAMD_VERSION_MAJOR_NUM;
  405. }
  406. else if (g_ascii_strcasecmp(type, "patch") == 0) {
  407. version_num = RSPAMD_VERSION_PATCH_NUM;
  408. }
  409. else if (g_ascii_strcasecmp(type, "minor") == 0) {
  410. version_num = RSPAMD_VERSION_MINOR_NUM;
  411. }
  412. }
  413. lua_pushinteger(L, version_num);
  414. return 1;
  415. }
  416. static int
  417. rspamd_lua_rspamd_version(lua_State *L)
  418. {
  419. const gchar *result = NULL, *type;
  420. if (lua_gettop(L) == 0) {
  421. result = RVERSION;
  422. }
  423. else if (lua_gettop(L) >= 1 && lua_type(L, 1) == LUA_TSTRING) {
  424. /* We got something like string */
  425. type = lua_tostring(L, 1);
  426. if (g_ascii_strcasecmp(type, "short") == 0) {
  427. result = RSPAMD_VERSION_MAJOR
  428. "." RSPAMD_VERSION_MINOR;
  429. }
  430. else if (g_ascii_strcasecmp(type, "main") == 0) {
  431. result = RSPAMD_VERSION_MAJOR "." RSPAMD_VERSION_MINOR "." RSPAMD_VERSION_PATCH;
  432. }
  433. else if (g_ascii_strcasecmp(type, "major") == 0) {
  434. result = RSPAMD_VERSION_MAJOR;
  435. }
  436. else if (g_ascii_strcasecmp(type, "minor") == 0) {
  437. result = RSPAMD_VERSION_MINOR;
  438. }
  439. else if (g_ascii_strcasecmp(type, "patch") == 0) {
  440. result = RSPAMD_VERSION_PATCH;
  441. }
  442. else if (g_ascii_strcasecmp(type, "id") == 0) {
  443. result = RID;
  444. }
  445. else if (g_ascii_strcasecmp(type, "num") == 0) {
  446. return rspamd_lua_rspamd_version_numeric(L);
  447. }
  448. else if (g_ascii_strcasecmp(type, "cmp") == 0) {
  449. return rspamd_lua_rspamd_version_cmp(L);
  450. }
  451. }
  452. lua_pushstring(L, result);
  453. return 1;
  454. }
  455. static gboolean
  456. rspamd_lua_load_env(lua_State *L, const char *fname, gint tbl_pos, GError **err)
  457. {
  458. gint orig_top = lua_gettop(L), err_idx;
  459. gboolean ret = TRUE;
  460. lua_pushcfunction(L, &rspamd_lua_traceback);
  461. err_idx = lua_gettop(L);
  462. if (luaL_loadfile(L, fname) != 0) {
  463. g_set_error(err, g_quark_from_static_string("lua_env"), errno,
  464. "cannot load lua file %s: %s",
  465. fname,
  466. lua_tostring(L, -1));
  467. ret = FALSE;
  468. }
  469. if (ret && lua_pcall(L, 0, 1, err_idx) != 0) {
  470. g_set_error(err, g_quark_from_static_string("lua_env"), errno,
  471. "cannot init lua file %s: %s",
  472. fname,
  473. lua_tostring(L, -1));
  474. ret = FALSE;
  475. }
  476. if (ret && lua_type(L, -1) == LUA_TTABLE) {
  477. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
  478. lua_pushvalue(L, -2); /* Store key */
  479. lua_pushvalue(L, -2); /* Store value */
  480. lua_settable(L, tbl_pos);
  481. }
  482. }
  483. else if (ret) {
  484. g_set_error(err, g_quark_from_static_string("lua_env"), errno,
  485. "invalid return type when loading env from %s: %s",
  486. fname,
  487. lua_typename(L, lua_type(L, -1)));
  488. ret = FALSE;
  489. }
  490. lua_settop(L, orig_top);
  491. return ret;
  492. }
  493. gboolean
  494. rspamd_lua_set_env(lua_State *L, GHashTable *vars, char **lua_env, GError **err)
  495. {
  496. gint orig_top = lua_gettop(L);
  497. gchar **env = g_get_environ();
  498. /* Set known paths as rspamd_paths global */
  499. lua_getglobal(L, "rspamd_paths");
  500. if (lua_isnil(L, -1)) {
  501. const gchar *confdir = RSPAMD_CONFDIR,
  502. *local_confdir = RSPAMD_LOCAL_CONFDIR,
  503. *rundir = RSPAMD_RUNDIR,
  504. *dbdir = RSPAMD_DBDIR,
  505. *logdir = RSPAMD_LOGDIR,
  506. *wwwdir = RSPAMD_WWWDIR,
  507. *pluginsdir = RSPAMD_PLUGINSDIR,
  508. *rulesdir = RSPAMD_RULESDIR,
  509. *lualibdir = RSPAMD_LUALIBDIR,
  510. *prefix = RSPAMD_PREFIX,
  511. *sharedir = RSPAMD_SHAREDIR;
  512. const gchar *t;
  513. /* Try environment */
  514. t = g_environ_getenv(env, "SHAREDIR");
  515. if (t) {
  516. sharedir = t;
  517. }
  518. t = g_environ_getenv(env, "PLUGINSDIR");
  519. if (t) {
  520. pluginsdir = t;
  521. }
  522. t = g_environ_getenv(env, "RULESDIR");
  523. if (t) {
  524. rulesdir = t;
  525. }
  526. t = g_environ_getenv(env, "DBDIR");
  527. if (t) {
  528. dbdir = t;
  529. }
  530. t = g_environ_getenv(env, "RUNDIR");
  531. if (t) {
  532. rundir = t;
  533. }
  534. t = g_environ_getenv(env, "LUALIBDIR");
  535. if (t) {
  536. lualibdir = t;
  537. }
  538. t = g_environ_getenv(env, "LOGDIR");
  539. if (t) {
  540. logdir = t;
  541. }
  542. t = g_environ_getenv(env, "WWWDIR");
  543. if (t) {
  544. wwwdir = t;
  545. }
  546. t = g_environ_getenv(env, "CONFDIR");
  547. if (t) {
  548. confdir = t;
  549. }
  550. t = g_environ_getenv(env, "LOCAL_CONFDIR");
  551. if (t) {
  552. local_confdir = t;
  553. }
  554. if (vars) {
  555. t = g_hash_table_lookup(vars, "SHAREDIR");
  556. if (t) {
  557. sharedir = t;
  558. }
  559. t = g_hash_table_lookup(vars, "PLUGINSDIR");
  560. if (t) {
  561. pluginsdir = t;
  562. }
  563. t = g_hash_table_lookup(vars, "RULESDIR");
  564. if (t) {
  565. rulesdir = t;
  566. }
  567. t = g_hash_table_lookup(vars, "LUALIBDIR");
  568. if (t) {
  569. lualibdir = t;
  570. }
  571. t = g_hash_table_lookup(vars, "RUNDIR");
  572. if (t) {
  573. rundir = t;
  574. }
  575. t = g_hash_table_lookup(vars, "WWWDIR");
  576. if (t) {
  577. wwwdir = t;
  578. }
  579. t = g_hash_table_lookup(vars, "CONFDIR");
  580. if (t) {
  581. confdir = t;
  582. }
  583. t = g_hash_table_lookup(vars, "LOCAL_CONFDIR");
  584. if (t) {
  585. local_confdir = t;
  586. }
  587. t = g_hash_table_lookup(vars, "DBDIR");
  588. if (t) {
  589. dbdir = t;
  590. }
  591. t = g_hash_table_lookup(vars, "LOGDIR");
  592. if (t) {
  593. logdir = t;
  594. }
  595. }
  596. lua_createtable(L, 0, 9);
  597. rspamd_lua_table_set(L, RSPAMD_SHAREDIR_INDEX, sharedir);
  598. rspamd_lua_table_set(L, RSPAMD_CONFDIR_INDEX, confdir);
  599. rspamd_lua_table_set(L, RSPAMD_LOCAL_CONFDIR_INDEX, local_confdir);
  600. rspamd_lua_table_set(L, RSPAMD_RUNDIR_INDEX, rundir);
  601. rspamd_lua_table_set(L, RSPAMD_DBDIR_INDEX, dbdir);
  602. rspamd_lua_table_set(L, RSPAMD_LOGDIR_INDEX, logdir);
  603. rspamd_lua_table_set(L, RSPAMD_WWWDIR_INDEX, wwwdir);
  604. rspamd_lua_table_set(L, RSPAMD_PLUGINSDIR_INDEX, pluginsdir);
  605. rspamd_lua_table_set(L, RSPAMD_RULESDIR_INDEX, rulesdir);
  606. rspamd_lua_table_set(L, RSPAMD_LUALIBDIR_INDEX, lualibdir);
  607. rspamd_lua_table_set(L, RSPAMD_PREFIX_INDEX, prefix);
  608. lua_setglobal(L, "rspamd_paths");
  609. }
  610. lua_getglobal(L, "rspamd_env");
  611. if (lua_isnil(L, -1)) {
  612. lua_newtable(L);
  613. if (vars != NULL) {
  614. GHashTableIter it;
  615. gpointer k, v;
  616. g_hash_table_iter_init(&it, vars);
  617. while (g_hash_table_iter_next(&it, &k, &v)) {
  618. rspamd_lua_table_set(L, k, v);
  619. }
  620. }
  621. gint hostlen = sysconf(_SC_HOST_NAME_MAX);
  622. if (hostlen <= 0) {
  623. hostlen = 256;
  624. }
  625. else {
  626. hostlen++;
  627. }
  628. gchar *hostbuf = g_alloca(hostlen);
  629. memset(hostbuf, 0, hostlen);
  630. gethostname(hostbuf, hostlen - 1);
  631. rspamd_lua_table_set(L, "hostname", hostbuf);
  632. rspamd_lua_table_set(L, "version", RVERSION);
  633. rspamd_lua_table_set(L, "ver_major", RSPAMD_VERSION_MAJOR);
  634. rspamd_lua_table_set(L, "ver_minor", RSPAMD_VERSION_MINOR);
  635. rspamd_lua_table_set(L, "ver_id", RID);
  636. lua_pushstring(L, "ver_num");
  637. lua_pushinteger(L, RSPAMD_VERSION_NUM);
  638. lua_settable(L, -3);
  639. if (env) {
  640. gint lim = g_strv_length(env);
  641. for (gint i = 0; i < lim; i++) {
  642. if (RSPAMD_LEN_CHECK_STARTS_WITH(env[i], strlen(env[i]), "RSPAMD_")) {
  643. const char *var = env[i] + sizeof("RSPAMD_") - 1, *value;
  644. gint varlen;
  645. varlen = strcspn(var, "=");
  646. value = var + varlen;
  647. if (*value == '=') {
  648. value++;
  649. lua_pushlstring(L, var, varlen);
  650. lua_pushstring(L, value);
  651. lua_settable(L, -3);
  652. }
  653. }
  654. }
  655. }
  656. if (lua_env) {
  657. gint lim = g_strv_length(lua_env);
  658. for (gint i = 0; i < lim; i++) {
  659. if (!rspamd_lua_load_env(L, lua_env[i], lua_gettop(L), err)) {
  660. return FALSE;
  661. }
  662. }
  663. }
  664. lua_setglobal(L, "rspamd_env");
  665. }
  666. lua_settop(L, orig_top);
  667. g_strfreev(env);
  668. return TRUE;
  669. }
  670. void rspamd_lua_set_globals(struct rspamd_config *cfg, lua_State *L)
  671. {
  672. struct rspamd_config **pcfg;
  673. gint orig_top = lua_gettop(L);
  674. /* First check for global variable 'config' */
  675. lua_getglobal(L, "config");
  676. if (lua_isnil(L, -1)) {
  677. /* Assign global table to set up attributes */
  678. lua_newtable(L);
  679. lua_setglobal(L, "config");
  680. }
  681. lua_getglobal(L, "metrics");
  682. if (lua_isnil(L, -1)) {
  683. lua_newtable(L);
  684. lua_setglobal(L, "metrics");
  685. }
  686. lua_getglobal(L, "composites");
  687. if (lua_isnil(L, -1)) {
  688. lua_newtable(L);
  689. lua_setglobal(L, "composites");
  690. }
  691. lua_getglobal(L, "rspamd_classifiers");
  692. if (lua_isnil(L, -1)) {
  693. lua_newtable(L);
  694. lua_setglobal(L, "rspamd_classifiers");
  695. }
  696. lua_getglobal(L, "classifiers");
  697. if (lua_isnil(L, -1)) {
  698. lua_newtable(L);
  699. lua_setglobal(L, "classifiers");
  700. }
  701. lua_getglobal(L, "rspamd_version");
  702. if (lua_isnil(L, -1)) {
  703. lua_pushcfunction(L, rspamd_lua_rspamd_version);
  704. lua_setglobal(L, "rspamd_version");
  705. }
  706. if (cfg != NULL) {
  707. pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *));
  708. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  709. *pcfg = cfg;
  710. lua_setglobal(L, "rspamd_config");
  711. }
  712. lua_settop(L, orig_top);
  713. }
  714. #ifdef WITH_LUA_TRACE
  715. static gint
  716. lua_push_trace_data(lua_State *L)
  717. {
  718. if (lua_traces) {
  719. ucl_object_push_lua(L, lua_traces, true);
  720. }
  721. else {
  722. lua_pushnil(L);
  723. }
  724. return 1;
  725. }
  726. #endif
  727. static void *
  728. rspamd_lua_wipe_realloc(void *ud,
  729. void *ptr,
  730. size_t osize,
  731. size_t nsize) RSPAMD_ATTR_ALLOC_SIZE(4);
  732. static void *
  733. rspamd_lua_wipe_realloc(void *ud,
  734. void *ptr,
  735. size_t osize,
  736. size_t nsize)
  737. {
  738. if (nsize == 0) {
  739. if (ptr) {
  740. rspamd_explicit_memzero(ptr, osize);
  741. }
  742. free(ptr);
  743. }
  744. else if (ptr == NULL) {
  745. return malloc(nsize);
  746. }
  747. else {
  748. if (nsize < osize) {
  749. /* Wipe on shrinking (actually never used) */
  750. rspamd_explicit_memzero(((unsigned char *) ptr) + nsize, osize - nsize);
  751. }
  752. return realloc(ptr, nsize);
  753. }
  754. return NULL;
  755. }
  756. #ifndef WITH_LUAJIT
  757. extern int luaopen_bit(lua_State *L);
  758. #endif
  759. static unsigned int lua_initialized = 0;
  760. lua_State *
  761. rspamd_lua_init(bool wipe_mem)
  762. {
  763. lua_State *L;
  764. if (wipe_mem) {
  765. #ifdef WITH_LUAJIT
  766. /* TODO: broken on luajit without GC64 */
  767. L = luaL_newstate();
  768. #else
  769. L = lua_newstate(rspamd_lua_wipe_realloc, NULL);
  770. #endif
  771. }
  772. else {
  773. L = luaL_newstate();
  774. }
  775. struct rspamd_lua_context *ctx;
  776. ctx = (struct rspamd_lua_context *) g_malloc0(sizeof(*ctx));
  777. ctx->L = L;
  778. ctx->classes = kh_init(lua_class_set);
  779. kh_resize(lua_class_set, ctx->classes, RSPAMD_LUA_NCLASSES);
  780. DL_APPEND(rspamd_lua_global_ctx, ctx);
  781. lua_gc(L, LUA_GCSTOP, 0);
  782. luaL_openlibs(L);
  783. luaopen_logger(L);
  784. luaopen_mempool(L);
  785. luaopen_config(L);
  786. luaopen_map(L);
  787. luaopen_trie(L);
  788. luaopen_task(L);
  789. luaopen_textpart(L);
  790. luaopen_mimepart(L);
  791. luaopen_image(L);
  792. luaopen_url(L);
  793. luaopen_classifier(L);
  794. luaopen_statfile(L);
  795. luaopen_regexp(L);
  796. luaopen_cdb(L);
  797. luaopen_xmlrpc(L);
  798. luaopen_http(L);
  799. luaopen_redis(L);
  800. luaopen_upstream(L);
  801. lua_add_actions_global(L);
  802. luaopen_dns_resolver(L);
  803. luaopen_rsa(L);
  804. luaopen_ip(L);
  805. luaopen_expression(L);
  806. luaopen_text(L);
  807. luaopen_util(L);
  808. luaopen_tcp(L);
  809. luaopen_html(L);
  810. luaopen_sqlite3(L);
  811. luaopen_cryptobox(L);
  812. luaopen_dns(L);
  813. luaopen_udp(L);
  814. luaopen_worker(L);
  815. luaopen_kann(L);
  816. luaopen_spf(L);
  817. luaopen_tensor(L);
  818. luaopen_parsers(L);
  819. luaopen_compress(L);
  820. #ifndef WITH_LUAJIT
  821. rspamd_lua_add_preload(L, "bit", luaopen_bit);
  822. lua_settop(L, 0);
  823. #endif
  824. rspamd_lua_new_class(L, rspamd_session_classname, NULL);
  825. lua_pop(L, 1);
  826. rspamd_lua_add_preload(L, "lpeg", luaopen_lpeg);
  827. luaopen_ucl(L);
  828. rspamd_lua_add_preload(L, "ucl", luaopen_ucl);
  829. /* Add plugins global */
  830. lua_newtable(L);
  831. lua_setglobal(L, "rspamd_plugins");
  832. /* Set PRNG */
  833. lua_getglobal(L, "math");
  834. lua_pushstring(L, "randomseed"); /* Push math.randomseed function on top of the stack */
  835. lua_gettable(L, -2);
  836. lua_pushinteger(L, ottery_rand_uint64());
  837. g_assert(lua_pcall(L, 1, 0, 0) == 0);
  838. lua_pop(L, 1); /* math table */
  839. /* Modules state */
  840. lua_newtable(L);
  841. /*
  842. * rspamd_plugins_state = {
  843. * enabled = {},
  844. * disabled_unconfigured = {},
  845. * disabled_redis = {},
  846. * disabled_explicitly = {},
  847. * disabled_failed = {},
  848. * disabled_experimental = {},
  849. * disabled_unknown = {},
  850. * }
  851. */
  852. #define ADD_TABLE(name) \
  853. do { \
  854. lua_pushstring(L, #name); \
  855. lua_newtable(L); \
  856. lua_settable(L, -3); \
  857. } while (0)
  858. ADD_TABLE(enabled);
  859. ADD_TABLE(disabled_unconfigured);
  860. ADD_TABLE(disabled_redis);
  861. ADD_TABLE(disabled_explicitly);
  862. ADD_TABLE(disabled_failed);
  863. ADD_TABLE(disabled_experimental);
  864. ADD_TABLE(disabled_unknown);
  865. #undef ADD_TABLE
  866. lua_setglobal(L, rspamd_modules_state_global);
  867. #ifdef WITH_LUA_TRACE
  868. lua_pushcfunction(L, lua_push_trace_data);
  869. lua_setglobal(L, "get_traces");
  870. #endif
  871. lua_initialized++;
  872. return L;
  873. }
  874. void rspamd_lua_close(lua_State *L)
  875. {
  876. struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L);
  877. /* TODO: we will leak this memory, but I don't know how to resolve
  878. * the chicked-egg problem when lua_close calls GC for many
  879. * userdata that requires classes metatables to be represented
  880. * For now, it is safe to leave it as is, I'm afraid
  881. */
  882. #if 0
  883. int ref;
  884. kh_foreach_value(ctx->classes, ref, {
  885. luaL_unref(L, LUA_REGISTRYINDEX, ref);
  886. });
  887. #endif
  888. lua_close(L);
  889. DL_DELETE(rspamd_lua_global_ctx, ctx);
  890. kh_destroy(lua_class_set, ctx->classes);
  891. g_free(ctx);
  892. lua_initialized--;
  893. }
  894. bool rspamd_lua_is_initialised(void)
  895. {
  896. return lua_initialized != 0;
  897. }
  898. void rspamd_lua_start_gc(struct rspamd_config *cfg)
  899. {
  900. lua_State *L = (lua_State *) cfg->lua_state;
  901. lua_settop(L, 0);
  902. /* Set up GC */
  903. lua_gc(L, LUA_GCCOLLECT, 0);
  904. lua_gc(L, LUA_GCSETSTEPMUL, cfg->lua_gc_step);
  905. lua_gc(L, LUA_GCSETPAUSE, cfg->lua_gc_pause);
  906. lua_gc(L, LUA_GCRESTART, 0);
  907. }
  908. void rspamd_plugins_table_push_elt(lua_State *L, const gchar *field_name,
  909. const gchar *new_elt)
  910. {
  911. lua_getglobal(L, rspamd_modules_state_global);
  912. if (lua_istable(L, -1)) {
  913. lua_pushstring(L, field_name);
  914. lua_gettable(L, -2);
  915. if (lua_istable(L, -1)) {
  916. lua_pushstring(L, new_elt);
  917. lua_newtable(L);
  918. lua_settable(L, -3);
  919. lua_pop(L, 2); /* Global + element */
  920. }
  921. else {
  922. lua_pop(L, 2); /* Global + element */
  923. }
  924. }
  925. else {
  926. lua_pop(L, 1);
  927. }
  928. }
  929. gboolean
  930. rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict)
  931. {
  932. struct rspamd_config **pcfg;
  933. struct script_module *module;
  934. lua_State *L = cfg->lua_state;
  935. gint err_idx, i;
  936. pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *));
  937. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  938. *pcfg = cfg;
  939. lua_setglobal(L, "rspamd_config");
  940. PTR_ARRAY_FOREACH(cfg->script_modules, i, module)
  941. {
  942. if (module->path) {
  943. if (!force_load) {
  944. if (!rspamd_config_is_module_enabled(cfg, module->name)) {
  945. continue;
  946. }
  947. }
  948. lua_pushcfunction(L, &rspamd_lua_traceback);
  949. err_idx = lua_gettop(L);
  950. gsize fsize;
  951. guint8 *data = rspamd_file_xmap(module->path,
  952. PROT_READ, &fsize, TRUE);
  953. guchar digest[rspamd_cryptobox_HASHBYTES];
  954. gchar *lua_fname;
  955. if (data == NULL) {
  956. msg_err_config("cannot mmap %s failed: %s", module->path,
  957. strerror(errno));
  958. lua_settop(L, err_idx - 1); /* Error function */
  959. rspamd_plugins_table_push_elt(L, "disabled_failed",
  960. module->name);
  961. if (strict) {
  962. return FALSE;
  963. }
  964. continue;
  965. }
  966. module->digest = rspamd_mempool_alloc(cfg->cfg_pool,
  967. rspamd_cryptobox_HASHBYTES * 2 + 1);
  968. rspamd_cryptobox_hash(digest, data, fsize, NULL, 0);
  969. rspamd_encode_hex_buf(digest, sizeof(digest),
  970. module->digest, rspamd_cryptobox_HASHBYTES * 2 + 1);
  971. module->digest[rspamd_cryptobox_HASHBYTES * 2] = '\0';
  972. lua_fname = g_malloc(strlen(module->path) + 2);
  973. rspamd_snprintf(lua_fname, strlen(module->path) + 2, "@%s",
  974. module->path);
  975. if (luaL_loadbuffer(L, data, fsize, lua_fname) != 0) {
  976. msg_err_config("load of %s failed: %s", module->path,
  977. lua_tostring(L, -1));
  978. lua_settop(L, err_idx - 1); /* Error function */
  979. rspamd_plugins_table_push_elt(L, "disabled_failed",
  980. module->name);
  981. munmap(data, fsize);
  982. g_free(lua_fname);
  983. if (strict) {
  984. return FALSE;
  985. }
  986. continue;
  987. }
  988. munmap(data, fsize);
  989. g_free(lua_fname);
  990. if (lua_pcall(L, 0, 0, err_idx) != 0) {
  991. msg_err_config("init of %s failed: %s",
  992. module->path,
  993. lua_tostring(L, -1));
  994. lua_settop(L, err_idx - 1);
  995. rspamd_plugins_table_push_elt(L, "disabled_failed",
  996. module->name);
  997. if (strict) {
  998. return FALSE;
  999. }
  1000. continue;
  1001. }
  1002. if (!force_load) {
  1003. msg_info_config("init lua module %s from %s; digest: %*s",
  1004. module->name,
  1005. module->path,
  1006. 10, module->digest);
  1007. }
  1008. lua_pop(L, 1); /* Error function */
  1009. }
  1010. }
  1011. return TRUE;
  1012. }
  1013. void rspamd_lua_dumpstack(lua_State *L)
  1014. {
  1015. gint i, t, r = 0;
  1016. gint top = lua_gettop(L);
  1017. gchar buf[BUFSIZ];
  1018. r += rspamd_snprintf(buf + r, sizeof(buf) - r, "lua stack: ");
  1019. for (i = 1; i <= top; i++) { /* repeat for each level */
  1020. t = lua_type(L, i);
  1021. switch (t) {
  1022. case LUA_TSTRING: /* strings */
  1023. r += rspamd_snprintf(buf + r,
  1024. sizeof(buf) - r,
  1025. "str: %s",
  1026. lua_tostring(L, i));
  1027. break;
  1028. case LUA_TBOOLEAN: /* booleans */
  1029. r += rspamd_snprintf(buf + r, sizeof(buf) - r,
  1030. lua_toboolean(L, i) ? "bool: true" : "bool: false");
  1031. break;
  1032. case LUA_TNUMBER: /* numbers */
  1033. r += rspamd_snprintf(buf + r,
  1034. sizeof(buf) - r,
  1035. "number: %.2f",
  1036. lua_tonumber(L, i));
  1037. break;
  1038. default: /* other values */
  1039. r += rspamd_snprintf(buf + r,
  1040. sizeof(buf) - r,
  1041. "type: %s",
  1042. lua_typename(L, t));
  1043. break;
  1044. }
  1045. if (i < top) {
  1046. r += rspamd_snprintf(buf + r, sizeof(buf) - r,
  1047. " -> "); /* put a separator */
  1048. }
  1049. }
  1050. msg_info("%*s", r, buf);
  1051. }
  1052. gpointer
  1053. rspamd_lua_check_class(lua_State *L, gint index, const gchar *name)
  1054. {
  1055. gpointer p;
  1056. khiter_t k;
  1057. if (lua_type(L, index) == LUA_TUSERDATA) {
  1058. p = lua_touserdata(L, index);
  1059. if (p) {
  1060. if (lua_getmetatable(L, index)) {
  1061. struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L);
  1062. k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(name));
  1063. if (k == kh_end(ctx->classes)) {
  1064. lua_pop(L, 1);
  1065. return NULL;
  1066. }
  1067. lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k));
  1068. if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
  1069. lua_pop(L, 2); /* remove both metatables */
  1070. return p;
  1071. }
  1072. lua_pop(L, 2);
  1073. }
  1074. }
  1075. }
  1076. return NULL;
  1077. }
  1078. int rspamd_lua_typerror(lua_State *L, int narg, const char *tname)
  1079. {
  1080. const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
  1081. luaL_typename(L, narg));
  1082. return luaL_argerror(L, narg, msg);
  1083. }
  1084. void rspamd_lua_add_preload(lua_State *L, const gchar *name, lua_CFunction func)
  1085. {
  1086. lua_getglobal(L, "package");
  1087. lua_pushstring(L, "preload");
  1088. lua_gettable(L, -2);
  1089. lua_pushcfunction(L, func);
  1090. lua_setfield(L, -2, name);
  1091. lua_pop(L, 2); /* preload key + global package */
  1092. }
  1093. gboolean
  1094. rspamd_lua_parse_table_arguments(lua_State *L, gint pos,
  1095. GError **err,
  1096. enum rspamd_lua_parse_arguments_flags how,
  1097. const gchar *extraction_pattern, ...)
  1098. {
  1099. const gchar *p, *key = NULL, *end, *cls;
  1100. va_list ap;
  1101. gboolean required = FALSE, failed = FALSE, is_table;
  1102. enum {
  1103. read_key = 0,
  1104. read_arg,
  1105. read_class_start,
  1106. read_class,
  1107. read_semicolon
  1108. } state = read_key;
  1109. gsize keylen = 0, *valuelen, clslen;
  1110. gint idx = 0, t, direct_userdata = 0;
  1111. g_assert(extraction_pattern != NULL);
  1112. if (pos < 0) {
  1113. /* Get absolute pos */
  1114. pos = lua_gettop(L) + pos + 1;
  1115. }
  1116. if (lua_type(L, pos) == LUA_TTABLE) {
  1117. is_table = TRUE;
  1118. }
  1119. else {
  1120. is_table = FALSE;
  1121. idx = pos;
  1122. }
  1123. p = extraction_pattern;
  1124. end = p + strlen(extraction_pattern);
  1125. va_start(ap, extraction_pattern);
  1126. while (p <= end) {
  1127. switch (state) {
  1128. case read_key:
  1129. if (*p == '=') {
  1130. if (key == NULL) {
  1131. g_set_error(err, lua_error_quark(), 1, "cannot read key");
  1132. va_end(ap);
  1133. return FALSE;
  1134. }
  1135. state = read_arg;
  1136. keylen = p - key;
  1137. }
  1138. else if (*p == '*' && key == NULL) {
  1139. required = TRUE;
  1140. }
  1141. else if (key == NULL) {
  1142. key = p;
  1143. }
  1144. p++;
  1145. break;
  1146. case read_arg:
  1147. g_assert(keylen != 0);
  1148. if (is_table) {
  1149. lua_pushlstring(L, key, keylen);
  1150. lua_gettable(L, pos);
  1151. idx = -1;
  1152. }
  1153. t = lua_type(L, idx);
  1154. switch (*p) {
  1155. case 'S':
  1156. if (t == LUA_TSTRING) {
  1157. *(va_arg(ap, const gchar **)) = lua_tostring(L, idx);
  1158. }
  1159. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1160. failed = TRUE;
  1161. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1162. *(va_arg(ap, const gchar **)) = NULL;
  1163. }
  1164. else {
  1165. (void) va_arg(ap, gchar **);
  1166. }
  1167. }
  1168. else {
  1169. g_set_error(err,
  1170. lua_error_quark(),
  1171. 1,
  1172. "bad type for key:"
  1173. " %.*s: '%s', '%s' is expected",
  1174. (gint) keylen,
  1175. key,
  1176. lua_typename(L, lua_type(L, idx)), "string");
  1177. va_end(ap);
  1178. return FALSE;
  1179. }
  1180. if (is_table) {
  1181. lua_pop(L, 1);
  1182. }
  1183. break;
  1184. case 'I':
  1185. if (t == LUA_TNUMBER) {
  1186. *(va_arg(ap, gint64 *)) = lua_tointeger(L, idx);
  1187. }
  1188. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1189. failed = TRUE;
  1190. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1191. *(va_arg(ap, gint64 *)) = 0;
  1192. }
  1193. else {
  1194. (void) va_arg(ap, gint64 *);
  1195. }
  1196. }
  1197. else {
  1198. g_set_error(err,
  1199. lua_error_quark(),
  1200. 1,
  1201. "bad type for key:"
  1202. " %.*s: '%s', '%s' is expected",
  1203. (gint) keylen,
  1204. key,
  1205. lua_typename(L, lua_type(L, idx)),
  1206. "int64");
  1207. va_end(ap);
  1208. return FALSE;
  1209. }
  1210. if (is_table) {
  1211. lua_pop(L, 1);
  1212. }
  1213. break;
  1214. case 'i':
  1215. if (t == LUA_TNUMBER) {
  1216. *(va_arg(ap, gint32 *)) = lua_tointeger(L, idx);
  1217. }
  1218. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1219. failed = TRUE;
  1220. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1221. *(va_arg(ap, gint32 *)) = 0;
  1222. }
  1223. else {
  1224. (void) va_arg(ap, gint32 *);
  1225. }
  1226. }
  1227. else {
  1228. g_set_error(err,
  1229. lua_error_quark(),
  1230. 1,
  1231. "bad type for key:"
  1232. " %.*s: '%s', '%s' is expected",
  1233. (gint) keylen,
  1234. key,
  1235. lua_typename(L, lua_type(L, idx)),
  1236. "int64");
  1237. va_end(ap);
  1238. return FALSE;
  1239. }
  1240. if (is_table) {
  1241. lua_pop(L, 1);
  1242. }
  1243. break;
  1244. case 'F':
  1245. if (t == LUA_TFUNCTION) {
  1246. if (!is_table) {
  1247. lua_pushvalue(L, idx);
  1248. }
  1249. *(va_arg(ap, gint *)) = luaL_ref(L, LUA_REGISTRYINDEX);
  1250. }
  1251. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1252. failed = TRUE;
  1253. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1254. *(va_arg(ap, gint *)) = -1;
  1255. }
  1256. else {
  1257. (void) va_arg(ap, gint *);
  1258. }
  1259. if (is_table) {
  1260. lua_pop(L, 1);
  1261. }
  1262. }
  1263. else {
  1264. g_set_error(err,
  1265. lua_error_quark(),
  1266. 1,
  1267. "bad type for key:"
  1268. " %.*s: '%s', '%s' is expected",
  1269. (gint) keylen,
  1270. key,
  1271. lua_typename(L, lua_type(L, idx)),
  1272. "function");
  1273. va_end(ap);
  1274. if (is_table) {
  1275. lua_pop(L, 1);
  1276. }
  1277. return FALSE;
  1278. }
  1279. /* luaL_ref pops argument from the stack */
  1280. break;
  1281. case 'B':
  1282. if (t == LUA_TBOOLEAN) {
  1283. *(va_arg(ap, gboolean *)) = lua_toboolean(L, idx);
  1284. }
  1285. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1286. failed = TRUE;
  1287. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1288. *(va_arg(ap, gboolean *)) = 0;
  1289. }
  1290. }
  1291. else {
  1292. g_set_error(err,
  1293. lua_error_quark(),
  1294. 1,
  1295. "bad type for key:"
  1296. " %.*s: '%s', '%s' is expected",
  1297. (gint) keylen,
  1298. key,
  1299. lua_typename(L, lua_type(L, idx)),
  1300. "bool");
  1301. va_end(ap);
  1302. return FALSE;
  1303. }
  1304. if (is_table) {
  1305. lua_pop(L, 1);
  1306. }
  1307. break;
  1308. case 'N':
  1309. if (t == LUA_TNUMBER) {
  1310. *(va_arg(ap, gdouble *)) = lua_tonumber(L, idx);
  1311. }
  1312. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1313. failed = TRUE;
  1314. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1315. *(va_arg(ap, gdouble *)) = 0;
  1316. }
  1317. else {
  1318. (void) va_arg(ap, gdouble *);
  1319. }
  1320. }
  1321. else {
  1322. g_set_error(err,
  1323. lua_error_quark(),
  1324. 1,
  1325. "bad type for key:"
  1326. " %.*s: '%s', '%s' is expected",
  1327. (gint) keylen,
  1328. key,
  1329. lua_typename(L, lua_type(L, idx)),
  1330. "double");
  1331. va_end(ap);
  1332. return FALSE;
  1333. }
  1334. if (is_table) {
  1335. lua_pop(L, 1);
  1336. }
  1337. break;
  1338. case 'D':
  1339. if (t == LUA_TNUMBER) {
  1340. *(va_arg(ap, gdouble *)) = lua_tonumber(L, idx);
  1341. }
  1342. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1343. failed = TRUE;
  1344. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1345. *(va_arg(ap, gdouble *)) = NAN;
  1346. }
  1347. else {
  1348. (void) va_arg(ap, gdouble *);
  1349. }
  1350. }
  1351. else {
  1352. g_set_error(err,
  1353. lua_error_quark(),
  1354. 1,
  1355. "bad type for key:"
  1356. " %.*s: '%s', '%s' is expected",
  1357. (gint) keylen,
  1358. key,
  1359. lua_typename(L, lua_type(L, idx)),
  1360. "double");
  1361. va_end(ap);
  1362. return FALSE;
  1363. }
  1364. if (is_table) {
  1365. lua_pop(L, 1);
  1366. }
  1367. break;
  1368. case 'V':
  1369. valuelen = va_arg(ap, gsize *);
  1370. if (t == LUA_TSTRING) {
  1371. *(va_arg(ap, const gchar **)) = lua_tolstring(L, idx,
  1372. valuelen);
  1373. }
  1374. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1375. failed = TRUE;
  1376. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1377. *(va_arg(ap, const char **)) = NULL;
  1378. *valuelen = 0;
  1379. }
  1380. else {
  1381. (void) va_arg(ap, const char **);
  1382. }
  1383. }
  1384. else {
  1385. g_set_error(err,
  1386. lua_error_quark(),
  1387. 1,
  1388. "bad type for key:"
  1389. " %.*s: '%s', '%s' is expected",
  1390. (gint) keylen,
  1391. key,
  1392. lua_typename(L, lua_type(L, idx)),
  1393. "string");
  1394. va_end(ap);
  1395. return FALSE;
  1396. }
  1397. if (is_table) {
  1398. lua_pop(L, 1);
  1399. }
  1400. break;
  1401. case 'O':
  1402. if (t != LUA_TNONE) {
  1403. *(va_arg(ap, ucl_object_t **)) = ucl_object_lua_import(L,
  1404. idx);
  1405. }
  1406. else {
  1407. failed = TRUE;
  1408. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1409. *(va_arg(ap, ucl_object_t **)) = NULL;
  1410. }
  1411. else {
  1412. (void) va_arg(ap, ucl_object_t **);
  1413. }
  1414. }
  1415. if (is_table) {
  1416. lua_pop(L, 1);
  1417. }
  1418. break;
  1419. case 'U':
  1420. if (t == LUA_TNIL || t == LUA_TNONE) {
  1421. failed = TRUE;
  1422. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1423. *(va_arg(ap, void **)) = NULL;
  1424. }
  1425. else {
  1426. (void) va_arg(ap, void **);
  1427. }
  1428. }
  1429. else if (t != LUA_TUSERDATA) {
  1430. g_set_error(err,
  1431. lua_error_quark(),
  1432. 1,
  1433. "bad type for key:"
  1434. " %.*s: '%s', '%s' is expected",
  1435. (gint) keylen,
  1436. key,
  1437. lua_typename(L, lua_type(L, idx)),
  1438. "int64");
  1439. va_end(ap);
  1440. return FALSE;
  1441. }
  1442. state = read_class_start;
  1443. clslen = 0;
  1444. direct_userdata = 0;
  1445. cls = NULL;
  1446. p++;
  1447. continue;
  1448. case 'u':
  1449. if (t == LUA_TNIL || t == LUA_TNONE) {
  1450. failed = TRUE;
  1451. if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
  1452. *(va_arg(ap, void **)) = NULL;
  1453. }
  1454. else {
  1455. (void) va_arg(ap, void **);
  1456. }
  1457. }
  1458. else if (t != LUA_TUSERDATA) {
  1459. g_set_error(err,
  1460. lua_error_quark(),
  1461. 1,
  1462. "bad type for key:"
  1463. " %.*s: '%s', '%s' is expected",
  1464. (gint) keylen,
  1465. key,
  1466. lua_typename(L, lua_type(L, idx)),
  1467. "int64");
  1468. va_end(ap);
  1469. return FALSE;
  1470. }
  1471. state = read_class_start;
  1472. clslen = 0;
  1473. direct_userdata = 1;
  1474. cls = NULL;
  1475. p++;
  1476. continue;
  1477. default:
  1478. g_assert(0);
  1479. break;
  1480. }
  1481. if (failed && required) {
  1482. g_set_error(err, lua_error_quark(), 2, "required parameter "
  1483. "%.*s is missing",
  1484. (gint) keylen, key);
  1485. va_end(ap);
  1486. return FALSE;
  1487. }
  1488. if (!is_table) {
  1489. idx++;
  1490. }
  1491. /* Reset read params */
  1492. state = read_semicolon;
  1493. failed = FALSE;
  1494. required = FALSE;
  1495. keylen = 0;
  1496. key = NULL;
  1497. p++;
  1498. break;
  1499. case read_class_start:
  1500. if (*p == '{') {
  1501. cls = p + 1;
  1502. state = read_class;
  1503. }
  1504. else {
  1505. if (is_table) {
  1506. lua_pop(L, 1);
  1507. }
  1508. g_set_error(err, lua_error_quark(), 2, "missing classname for "
  1509. "%.*s",
  1510. (gint) keylen, key);
  1511. va_end(ap);
  1512. return FALSE;
  1513. }
  1514. p++;
  1515. break;
  1516. case read_class:
  1517. if (*p == '}') {
  1518. clslen = p - cls;
  1519. if (clslen == 0) {
  1520. if (is_table) {
  1521. lua_pop(L, 1);
  1522. }
  1523. g_set_error(err,
  1524. lua_error_quark(),
  1525. 2,
  1526. "empty classname for "
  1527. "%*.s",
  1528. (gint) keylen,
  1529. key);
  1530. va_end(ap);
  1531. return FALSE;
  1532. }
  1533. const char *static_cls = rspamd_lua_static_classname(cls, clslen);
  1534. /*
  1535. * We skip class check here for speed in non-table mode
  1536. */
  1537. if (!failed && (!is_table ||
  1538. static_cls == NULL ||
  1539. rspamd_lua_check_class(L, idx, static_cls))) {
  1540. if (direct_userdata) {
  1541. void **arg_p = (va_arg(ap, void **));
  1542. *arg_p = lua_touserdata(L, idx);
  1543. }
  1544. else {
  1545. *(va_arg(ap,
  1546. void **)) = *(void **) lua_touserdata(L, idx);
  1547. }
  1548. }
  1549. else {
  1550. if (!failed) {
  1551. g_set_error(err,
  1552. lua_error_quark(),
  1553. 2,
  1554. "invalid class for key %.*s, expected %s, got %s",
  1555. (gint) keylen,
  1556. key,
  1557. static_cls,
  1558. rspamd_lua_class_tostring_buf(L, FALSE, idx));
  1559. va_end(ap);
  1560. return FALSE;
  1561. }
  1562. }
  1563. if (is_table) {
  1564. lua_pop(L, 1);
  1565. }
  1566. else {
  1567. idx++;
  1568. }
  1569. if (failed && required) {
  1570. g_set_error(err,
  1571. lua_error_quark(),
  1572. 2,
  1573. "required parameter "
  1574. "%.*s is missing",
  1575. (gint) keylen,
  1576. key);
  1577. va_end(ap);
  1578. return FALSE;
  1579. }
  1580. /* Reset read params */
  1581. state = read_semicolon;
  1582. failed = FALSE;
  1583. required = FALSE;
  1584. keylen = 0;
  1585. key = NULL;
  1586. }
  1587. p++;
  1588. break;
  1589. case read_semicolon:
  1590. if (*p == ';' || p == end) {
  1591. state = read_key;
  1592. key = NULL;
  1593. keylen = 0;
  1594. failed = FALSE;
  1595. }
  1596. else {
  1597. g_set_error(err, lua_error_quark(), 2, "bad format string: %s,"
  1598. " at char %c, position %d",
  1599. extraction_pattern, *p, (int) (p - extraction_pattern));
  1600. va_end(ap);
  1601. return FALSE;
  1602. }
  1603. p++;
  1604. break;
  1605. }
  1606. }
  1607. va_end(ap);
  1608. return TRUE;
  1609. }
  1610. static void
  1611. rspamd_lua_traceback_string(lua_State *L, luaL_Buffer *buf)
  1612. {
  1613. gint i = 1, r;
  1614. lua_Debug d;
  1615. gchar tmp[256];
  1616. while (lua_getstack(L, i++, &d)) {
  1617. lua_getinfo(L, "nSl", &d);
  1618. r = rspamd_snprintf(tmp, sizeof(tmp), " [%d]:{%s:%d - %s [%s]};",
  1619. i - 1, d.short_src, d.currentline,
  1620. (d.name ? d.name : "<unknown>"), d.what);
  1621. luaL_addlstring(buf, tmp, r);
  1622. }
  1623. }
  1624. gint rspamd_lua_traceback(lua_State *L)
  1625. {
  1626. luaL_Buffer b;
  1627. luaL_buffinit(L, &b);
  1628. rspamd_lua_get_traceback_string(L, &b);
  1629. luaL_pushresult(&b);
  1630. return 1;
  1631. }
  1632. void rspamd_lua_get_traceback_string(lua_State *L, luaL_Buffer *buf)
  1633. {
  1634. const gchar *msg = lua_tostring(L, -1);
  1635. if (msg) {
  1636. luaL_addstring(buf, msg);
  1637. lua_pop(L, 1); /* Error string */
  1638. }
  1639. else {
  1640. luaL_addstring(buf, "unknown error");
  1641. }
  1642. luaL_addstring(buf, "; trace:");
  1643. rspamd_lua_traceback_string(L, buf);
  1644. }
  1645. guint rspamd_lua_table_size(lua_State *L, gint tbl_pos)
  1646. {
  1647. guint tbl_size = 0;
  1648. if (!lua_istable(L, tbl_pos)) {
  1649. return 0;
  1650. }
  1651. #if LUA_VERSION_NUM >= 502
  1652. tbl_size = lua_rawlen(L, tbl_pos);
  1653. #else
  1654. tbl_size = lua_objlen(L, tbl_pos);
  1655. #endif
  1656. return tbl_size;
  1657. }
  1658. static void *
  1659. rspamd_lua_check_udata_common(lua_State *L, gint pos, const gchar *classname,
  1660. gboolean fatal)
  1661. {
  1662. void *p = lua_touserdata(L, pos);
  1663. guint i, top = lua_gettop(L);
  1664. khiter_t k;
  1665. if (p == NULL) {
  1666. goto err;
  1667. }
  1668. else {
  1669. /* Match class */
  1670. if (lua_getmetatable(L, pos)) {
  1671. /* Get mt[1] and check if it is an iteger */
  1672. lua_rawgeti(L, -1, 1);
  1673. if (!lua_isnumber(L, -1)) {
  1674. lua_pop(L, 1);
  1675. goto err;
  1676. }
  1677. lua_Integer idx = lua_tointeger(L, -1);
  1678. lua_pop(L, 1);
  1679. if (idx != GPOINTER_TO_INT(classname)) {
  1680. goto err;
  1681. }
  1682. }
  1683. else {
  1684. goto err;
  1685. }
  1686. }
  1687. lua_settop(L, top);
  1688. return p;
  1689. err:
  1690. if (fatal) {
  1691. const gchar *actual_classname = NULL;
  1692. if (lua_type(L, pos) == LUA_TUSERDATA && lua_getmetatable(L, pos)) {
  1693. lua_pushstring(L, "__index");
  1694. lua_gettable(L, -2);
  1695. lua_pushstring(L, "class");
  1696. lua_gettable(L, -2);
  1697. actual_classname = lua_tostring(L, -1);
  1698. }
  1699. else {
  1700. actual_classname = lua_typename(L, lua_type(L, pos));
  1701. }
  1702. luaL_Buffer buf;
  1703. gchar tmp[512];
  1704. gint r;
  1705. luaL_buffinit(L, &buf);
  1706. r = rspamd_snprintf(tmp, sizeof(tmp),
  1707. "expected %s at position %d, but userdata has "
  1708. "%s metatable; trace: ",
  1709. classname, pos, actual_classname);
  1710. luaL_addlstring(&buf, tmp, r);
  1711. rspamd_lua_traceback_string(L, &buf);
  1712. r = rspamd_snprintf(tmp, sizeof(tmp), " stack(%d): ", top);
  1713. luaL_addlstring(&buf, tmp, r);
  1714. for (i = 1; i <= MIN(top, 10); i++) {
  1715. if (lua_type(L, i) == LUA_TUSERDATA) {
  1716. const char *clsname;
  1717. if (lua_getmetatable(L, i)) {
  1718. lua_pushstring(L, "__index");
  1719. lua_gettable(L, -2);
  1720. lua_pushstring(L, "class");
  1721. lua_gettable(L, -2);
  1722. clsname = lua_tostring(L, -1);
  1723. }
  1724. else {
  1725. clsname = lua_typename(L, lua_type(L, i));
  1726. }
  1727. r = rspamd_snprintf(tmp, sizeof(tmp), "[%d: ud=%s] ", i,
  1728. clsname);
  1729. luaL_addlstring(&buf, tmp, r);
  1730. }
  1731. else {
  1732. r = rspamd_snprintf(tmp, sizeof(tmp), "[%d: %s] ", i,
  1733. lua_typename(L, lua_type(L, i)));
  1734. luaL_addlstring(&buf, tmp, r);
  1735. }
  1736. }
  1737. luaL_pushresult(&buf);
  1738. msg_err("lua type error: %s", lua_tostring(L, -1));
  1739. }
  1740. lua_settop(L, top);
  1741. return NULL;
  1742. }
  1743. void *
  1744. rspamd_lua_check_udata(lua_State *L, gint pos, const gchar *classname)
  1745. {
  1746. return rspamd_lua_check_udata_common(L, pos, classname, TRUE);
  1747. }
  1748. void *
  1749. rspamd_lua_check_udata_maybe(lua_State *L, gint pos, const gchar *classname)
  1750. {
  1751. return rspamd_lua_check_udata_common(L, pos, classname, FALSE);
  1752. }
  1753. struct rspamd_async_session *
  1754. lua_check_session(lua_State *L, gint pos)
  1755. {
  1756. void *ud = rspamd_lua_check_udata(L, pos, rspamd_session_classname);
  1757. luaL_argcheck(L, ud != NULL, pos, "'session' expected");
  1758. return ud ? *((struct rspamd_async_session **) ud) : NULL;
  1759. }
  1760. struct ev_loop *
  1761. lua_check_ev_base(lua_State *L, gint pos)
  1762. {
  1763. void *ud = rspamd_lua_check_udata(L, pos, rspamd_ev_base_classname);
  1764. luaL_argcheck(L, ud != NULL, pos, "'event_base' expected");
  1765. return ud ? *((struct ev_loop **) ud) : NULL;
  1766. }
  1767. static void rspamd_lua_run_postloads_error(struct thread_entry *thread, int ret, const char *msg);
  1768. void rspamd_lua_run_postloads(lua_State *L, struct rspamd_config *cfg,
  1769. struct ev_loop *ev_base, struct rspamd_worker *w)
  1770. {
  1771. struct rspamd_config_cfg_lua_script *sc;
  1772. struct rspamd_config **pcfg;
  1773. struct ev_loop **pev_base;
  1774. struct rspamd_worker **pw;
  1775. /* Execute post load scripts */
  1776. LL_FOREACH(cfg->on_load_scripts, sc)
  1777. {
  1778. struct thread_entry *thread = lua_thread_pool_get_for_config(cfg);
  1779. thread->error_callback = rspamd_lua_run_postloads_error;
  1780. L = thread->lua_state;
  1781. lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref);
  1782. pcfg = lua_newuserdata(L, sizeof(*pcfg));
  1783. *pcfg = cfg;
  1784. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  1785. pev_base = lua_newuserdata(L, sizeof(*pev_base));
  1786. *pev_base = ev_base;
  1787. rspamd_lua_setclass(L, rspamd_ev_base_classname, -1);
  1788. pw = lua_newuserdata(L, sizeof(*pw));
  1789. *pw = w;
  1790. rspamd_lua_setclass(L, rspamd_worker_classname, -1);
  1791. lua_thread_call(thread, 3);
  1792. }
  1793. }
  1794. void rspamd_lua_run_config_post_init(lua_State *L, struct rspamd_config *cfg)
  1795. {
  1796. struct rspamd_config_cfg_lua_script *sc;
  1797. struct rspamd_config **pcfg;
  1798. LL_FOREACH(cfg->post_init_scripts, sc)
  1799. {
  1800. lua_pushcfunction(L, &rspamd_lua_traceback);
  1801. gint err_idx = lua_gettop(L);
  1802. lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref);
  1803. pcfg = lua_newuserdata(L, sizeof(*pcfg));
  1804. *pcfg = cfg;
  1805. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  1806. if (lua_pcall(L, 1, 0, err_idx) != 0) {
  1807. msg_err_config("cannot run config post init script: %s; priority = %d",
  1808. lua_tostring(L, -1), sc->priority);
  1809. }
  1810. lua_settop(L, err_idx - 1);
  1811. }
  1812. }
  1813. void rspamd_lua_run_config_unload(lua_State *L, struct rspamd_config *cfg)
  1814. {
  1815. struct rspamd_config_cfg_lua_script *sc;
  1816. struct rspamd_config **pcfg;
  1817. LL_FOREACH(cfg->config_unload_scripts, sc)
  1818. {
  1819. lua_pushcfunction(L, &rspamd_lua_traceback);
  1820. gint err_idx = lua_gettop(L);
  1821. lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref);
  1822. pcfg = lua_newuserdata(L, sizeof(*pcfg));
  1823. *pcfg = cfg;
  1824. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  1825. if (lua_pcall(L, 1, 0, err_idx) != 0) {
  1826. msg_err_config("cannot run config post init script: %s",
  1827. lua_tostring(L, -1));
  1828. }
  1829. lua_settop(L, err_idx - 1);
  1830. }
  1831. }
  1832. static void
  1833. rspamd_lua_run_postloads_error(struct thread_entry *thread, int ret, const char *msg)
  1834. {
  1835. struct rspamd_config *cfg = thread->cfg;
  1836. msg_err_config("error executing post load code: %s", msg);
  1837. }
  1838. struct rspamd_lua_ref_cbdata {
  1839. lua_State *L;
  1840. gint cbref;
  1841. };
  1842. static void
  1843. rspamd_lua_ref_dtor(gpointer p)
  1844. {
  1845. struct rspamd_lua_ref_cbdata *cbdata = p;
  1846. luaL_unref(cbdata->L, LUA_REGISTRYINDEX, cbdata->cbref);
  1847. }
  1848. void rspamd_lua_add_ref_dtor(lua_State *L, rspamd_mempool_t *pool,
  1849. gint ref)
  1850. {
  1851. struct rspamd_lua_ref_cbdata *cbdata;
  1852. if (ref != -1) {
  1853. cbdata = rspamd_mempool_alloc(pool, sizeof(*cbdata));
  1854. cbdata->cbref = ref;
  1855. cbdata->L = L;
  1856. rspamd_mempool_add_destructor(pool, rspamd_lua_ref_dtor, cbdata);
  1857. }
  1858. }
  1859. gboolean
  1860. rspamd_lua_require_function(lua_State *L, const gchar *modname,
  1861. const gchar *funcname)
  1862. {
  1863. gint table_pos, err_pos;
  1864. lua_pushcfunction(L, &rspamd_lua_traceback);
  1865. err_pos = lua_gettop(L);
  1866. lua_getglobal(L, "require");
  1867. if (lua_isnil(L, -1)) {
  1868. lua_remove(L, err_pos);
  1869. lua_pop(L, 1);
  1870. return FALSE;
  1871. }
  1872. lua_pushstring(L, modname);
  1873. /* Now try to call */
  1874. if (lua_pcall(L, 1, 1, 0) != 0) {
  1875. lua_remove(L, err_pos);
  1876. msg_warn("require of %s.%s failed: %s", modname,
  1877. funcname, lua_tostring(L, -1));
  1878. lua_pop(L, 1);
  1879. return FALSE;
  1880. }
  1881. lua_remove(L, err_pos);
  1882. /* Now we should have a table with results */
  1883. if (funcname) {
  1884. if (!lua_istable(L, -1)) {
  1885. msg_warn("require of %s.%s failed: not a table but %s", modname,
  1886. funcname, lua_typename(L, lua_type(L, -1)));
  1887. lua_pop(L, 1);
  1888. return FALSE;
  1889. }
  1890. table_pos = lua_gettop(L);
  1891. lua_pushstring(L, funcname);
  1892. lua_gettable(L, -2);
  1893. if (lua_type(L, -1) == LUA_TFUNCTION) {
  1894. /* Remove table, preserve just a function */
  1895. lua_remove(L, table_pos);
  1896. return TRUE;
  1897. }
  1898. else {
  1899. msg_warn("require of %s.%s failed: not a function but %s", modname,
  1900. funcname, lua_typename(L, lua_type(L, -1)));
  1901. }
  1902. lua_pop(L, 2);
  1903. return FALSE;
  1904. }
  1905. else if (lua_isfunction(L, -1)) {
  1906. return TRUE;
  1907. }
  1908. else {
  1909. msg_warn("require of %s failed: not a function but %s", modname,
  1910. lua_typename(L, lua_type(L, -1)));
  1911. lua_pop(L, 1);
  1912. return FALSE;
  1913. }
  1914. }
  1915. gint rspamd_lua_function_ref_from_str(lua_State *L, const gchar *str, gsize slen,
  1916. const gchar *modname, GError **err)
  1917. {
  1918. gint err_idx, ref_idx;
  1919. lua_pushcfunction(L, &rspamd_lua_traceback);
  1920. err_idx = lua_gettop(L);
  1921. /* Load file */
  1922. if (luaL_loadbuffer(L, str, slen, modname) != 0) {
  1923. g_set_error(err,
  1924. lua_error_quark(),
  1925. EINVAL,
  1926. "%s: cannot load lua script: %s",
  1927. modname,
  1928. lua_tostring(L, -1));
  1929. lua_settop(L, err_idx - 1); /* Error function */
  1930. return LUA_NOREF;
  1931. }
  1932. /* Now call it */
  1933. if (lua_pcall(L, 0, 1, err_idx) != 0) {
  1934. g_set_error(err,
  1935. lua_error_quark(),
  1936. EINVAL,
  1937. "%s: cannot init lua script: %s",
  1938. modname,
  1939. lua_tostring(L, -1));
  1940. lua_settop(L, err_idx - 1);
  1941. return LUA_NOREF;
  1942. }
  1943. if (!lua_isfunction(L, -1)) {
  1944. g_set_error(err,
  1945. lua_error_quark(),
  1946. EINVAL,
  1947. "%s: cannot init lua script: "
  1948. "must return function not %s",
  1949. modname,
  1950. lua_typename(L, lua_type(L, -1)));
  1951. lua_settop(L, err_idx - 1);
  1952. return LUA_NOREF;
  1953. }
  1954. ref_idx = luaL_ref(L, LUA_REGISTRYINDEX);
  1955. lua_settop(L, err_idx - 1);
  1956. return ref_idx;
  1957. }
  1958. gboolean
  1959. rspamd_lua_try_load_redis(lua_State *L, const ucl_object_t *obj,
  1960. struct rspamd_config *cfg, gint *ref_id)
  1961. {
  1962. gint err_idx;
  1963. struct rspamd_config **pcfg;
  1964. lua_pushcfunction(L, &rspamd_lua_traceback);
  1965. err_idx = lua_gettop(L);
  1966. /* Obtain function */
  1967. if (!rspamd_lua_require_function(L, "lua_redis", "try_load_redis_servers")) {
  1968. msg_err_config("cannot require lua_redis");
  1969. lua_pop(L, 2);
  1970. return FALSE;
  1971. }
  1972. /* Function arguments */
  1973. ucl_object_push_lua(L, obj, false);
  1974. pcfg = lua_newuserdata(L, sizeof(*pcfg));
  1975. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  1976. *pcfg = cfg;
  1977. lua_pushboolean(L, false); /* no_fallback */
  1978. if (lua_pcall(L, 3, 1, err_idx) != 0) {
  1979. msg_err_config("cannot call lua try_load_redis_servers script: %s",
  1980. lua_tostring(L, -1));
  1981. lua_settop(L, 0);
  1982. return FALSE;
  1983. }
  1984. if (lua_istable(L, -1)) {
  1985. if (ref_id) {
  1986. /* Ref table */
  1987. lua_pushvalue(L, -1);
  1988. *ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
  1989. lua_settop(L, 0);
  1990. }
  1991. else {
  1992. /* Leave it on the stack */
  1993. lua_insert(L, err_idx);
  1994. lua_settop(L, err_idx);
  1995. }
  1996. return TRUE;
  1997. }
  1998. else {
  1999. lua_settop(L, 0);
  2000. }
  2001. return FALSE;
  2002. }
  2003. void rspamd_lua_push_full_word(lua_State *L, rspamd_stat_token_t *w)
  2004. {
  2005. gint fl_cnt;
  2006. lua_createtable(L, 4, 0);
  2007. if (w->stemmed.len > 0) {
  2008. lua_pushlstring(L, w->stemmed.begin, w->stemmed.len);
  2009. lua_rawseti(L, -2, 1);
  2010. }
  2011. else {
  2012. lua_pushstring(L, "");
  2013. lua_rawseti(L, -2, 1);
  2014. }
  2015. if (w->normalized.len > 0) {
  2016. lua_pushlstring(L, w->normalized.begin, w->normalized.len);
  2017. lua_rawseti(L, -2, 2);
  2018. }
  2019. else {
  2020. lua_pushstring(L, "");
  2021. lua_rawseti(L, -2, 2);
  2022. }
  2023. if (w->original.len > 0) {
  2024. lua_pushlstring(L, w->original.begin, w->original.len);
  2025. lua_rawseti(L, -2, 3);
  2026. }
  2027. else {
  2028. lua_pushstring(L, "");
  2029. lua_rawseti(L, -2, 3);
  2030. }
  2031. /* Flags part */
  2032. fl_cnt = 1;
  2033. lua_createtable(L, 4, 0);
  2034. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_NORMALISED) {
  2035. lua_pushstring(L, "normalised");
  2036. lua_rawseti(L, -2, fl_cnt++);
  2037. }
  2038. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_BROKEN_UNICODE) {
  2039. lua_pushstring(L, "broken_unicode");
  2040. lua_rawseti(L, -2, fl_cnt++);
  2041. }
  2042. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_UTF) {
  2043. lua_pushstring(L, "utf");
  2044. lua_rawseti(L, -2, fl_cnt++);
  2045. }
  2046. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_TEXT) {
  2047. lua_pushstring(L, "text");
  2048. lua_rawseti(L, -2, fl_cnt++);
  2049. }
  2050. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_HEADER) {
  2051. lua_pushstring(L, "header");
  2052. lua_rawseti(L, -2, fl_cnt++);
  2053. }
  2054. if (w->flags & (RSPAMD_STAT_TOKEN_FLAG_META | RSPAMD_STAT_TOKEN_FLAG_LUA_META)) {
  2055. lua_pushstring(L, "meta");
  2056. lua_rawseti(L, -2, fl_cnt++);
  2057. }
  2058. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_STOP_WORD) {
  2059. lua_pushstring(L, "stop_word");
  2060. lua_rawseti(L, -2, fl_cnt++);
  2061. }
  2062. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_INVISIBLE_SPACES) {
  2063. lua_pushstring(L, "invisible_spaces");
  2064. lua_rawseti(L, -2, fl_cnt++);
  2065. }
  2066. if (w->flags & RSPAMD_STAT_TOKEN_FLAG_STEMMED) {
  2067. lua_pushstring(L, "stemmed");
  2068. lua_rawseti(L, -2, fl_cnt++);
  2069. }
  2070. lua_rawseti(L, -2, 4);
  2071. }
  2072. gint rspamd_lua_push_words(lua_State *L, GArray *words,
  2073. enum rspamd_lua_words_type how)
  2074. {
  2075. rspamd_stat_token_t *w;
  2076. guint i, cnt;
  2077. lua_createtable(L, words->len, 0);
  2078. for (i = 0, cnt = 1; i < words->len; i++) {
  2079. w = &g_array_index(words, rspamd_stat_token_t, i);
  2080. switch (how) {
  2081. case RSPAMD_LUA_WORDS_STEM:
  2082. if (w->stemmed.len > 0) {
  2083. lua_pushlstring(L, w->stemmed.begin, w->stemmed.len);
  2084. lua_rawseti(L, -2, cnt++);
  2085. }
  2086. break;
  2087. case RSPAMD_LUA_WORDS_NORM:
  2088. if (w->normalized.len > 0) {
  2089. lua_pushlstring(L, w->normalized.begin, w->normalized.len);
  2090. lua_rawseti(L, -2, cnt++);
  2091. }
  2092. break;
  2093. case RSPAMD_LUA_WORDS_RAW:
  2094. if (w->original.len > 0) {
  2095. lua_pushlstring(L, w->original.begin, w->original.len);
  2096. lua_rawseti(L, -2, cnt++);
  2097. }
  2098. break;
  2099. case RSPAMD_LUA_WORDS_FULL:
  2100. rspamd_lua_push_full_word(L, w);
  2101. /* Push to the resulting vector */
  2102. lua_rawseti(L, -2, cnt++);
  2103. break;
  2104. default:
  2105. break;
  2106. }
  2107. }
  2108. return 1;
  2109. }
  2110. gchar *
  2111. rspamd_lua_get_module_name(lua_State *L)
  2112. {
  2113. lua_Debug d;
  2114. gchar *p;
  2115. gchar func_buf[128];
  2116. if (lua_getstack(L, 1, &d) == 1) {
  2117. (void) lua_getinfo(L, "Sl", &d);
  2118. if ((p = strrchr(d.short_src, '/')) == NULL) {
  2119. p = d.short_src;
  2120. }
  2121. else {
  2122. p++;
  2123. }
  2124. if (strlen(p) > 20) {
  2125. rspamd_snprintf(func_buf, sizeof(func_buf), "%10s...]:%d", p,
  2126. d.currentline);
  2127. }
  2128. else {
  2129. rspamd_snprintf(func_buf, sizeof(func_buf), "%s:%d", p,
  2130. d.currentline);
  2131. }
  2132. return g_strdup(func_buf);
  2133. }
  2134. return NULL;
  2135. }
  2136. bool rspamd_lua_universal_pcall(lua_State *L, gint cbref, const gchar *strloc,
  2137. gint nret, const gchar *args, GError **err, ...)
  2138. {
  2139. va_list ap;
  2140. const gchar *argp = args, *classname;
  2141. gint err_idx, nargs = 0;
  2142. gpointer *cls_ptr;
  2143. gsize sz;
  2144. /* Error function */
  2145. lua_pushcfunction(L, &rspamd_lua_traceback);
  2146. err_idx = lua_gettop(L);
  2147. va_start(ap, err);
  2148. /* Called function */
  2149. if (cbref > 0) {
  2150. lua_rawgeti(L, LUA_REGISTRYINDEX, cbref);
  2151. }
  2152. else {
  2153. /* Assume that function was on top of the stack */
  2154. lua_pushvalue(L, err_idx - 1);
  2155. }
  2156. /*
  2157. * Possible arguments
  2158. * - i - lua_integer, argument - gint64
  2159. * - n - lua_number, argument - gdouble
  2160. * - s - lua_string, argument - const gchar * (zero terminated)
  2161. * - l - lua_lstring, argument - (size_t + const gchar *) pair
  2162. * - u - lua_userdata, argument - (const char * + void *) - classname + pointer
  2163. * - b - lua_boolean, argument - gboolean (not bool due to varargs promotion)
  2164. * - f - lua_function, argument - int - position of the function on stack (not lua_registry)
  2165. * - t - lua_text, argument - int - position of the lua_text on stack (not lua_registry)
  2166. */
  2167. while (*argp) {
  2168. switch (*argp) {
  2169. case 'i':
  2170. lua_pushinteger(L, va_arg(ap, gint64));
  2171. nargs++;
  2172. break;
  2173. case 'n':
  2174. lua_pushnumber(L, va_arg(ap, gdouble));
  2175. nargs++;
  2176. break;
  2177. case 's':
  2178. lua_pushstring(L, va_arg(ap, const gchar *));
  2179. nargs++;
  2180. break;
  2181. case 'l':
  2182. sz = va_arg(ap, gsize);
  2183. lua_pushlstring(L, va_arg(ap, const gchar *), sz);
  2184. nargs++;
  2185. break;
  2186. case 'b':
  2187. lua_pushboolean(L, va_arg(ap, gboolean));
  2188. nargs++;
  2189. break;
  2190. case 'u':
  2191. classname = va_arg(ap, const gchar *);
  2192. cls_ptr = (gpointer *) lua_newuserdata(L, sizeof(gpointer));
  2193. *cls_ptr = va_arg(ap, gpointer);
  2194. rspamd_lua_setclass(L, classname, -1);
  2195. nargs++;
  2196. break;
  2197. case 'f':
  2198. case 't':
  2199. lua_pushvalue(L, va_arg(ap, gint));
  2200. nargs++;
  2201. break;
  2202. default:
  2203. lua_settop(L, err_idx - 1);
  2204. g_set_error(err, lua_error_quark(), EINVAL,
  2205. "invalid argument character: %c at %s",
  2206. *argp, argp);
  2207. va_end(ap);
  2208. return false;
  2209. }
  2210. argp++;
  2211. }
  2212. if (lua_pcall(L, nargs, nret, err_idx) != 0) {
  2213. g_set_error(err, lua_error_quark(), EBADF,
  2214. "error when calling lua function from %s: %s",
  2215. strloc, lua_tostring(L, -1));
  2216. lua_settop(L, err_idx - 1);
  2217. va_end(ap);
  2218. return false;
  2219. }
  2220. lua_remove(L, err_idx);
  2221. va_end(ap);
  2222. return true;
  2223. }
  2224. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  2225. gint rspamd_lua_geti(lua_State *L, int pos, int i)
  2226. {
  2227. pos = lua_absindex(L, pos);
  2228. lua_pushinteger(L, i);
  2229. lua_gettable(L, pos);
  2230. return lua_type(L, -1);
  2231. }
  2232. #endif