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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "lua_common.h"
  17. #include "lptree.h"
  18. #include "utlist.h"
  19. #include "unix-std.h"
  20. #include "worker_util.h"
  21. #include "ottery.h"
  22. #include "rspamd_control.h"
  23. #include <math.h>
  24. #include <sys/wait.h>
  25. #include <src/libserver/rspamd_control.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. };
  35. LUA_FUNCTION_DEF (worker, get_name);
  36. LUA_FUNCTION_DEF (worker, get_stat);
  37. LUA_FUNCTION_DEF (worker, get_index);
  38. LUA_FUNCTION_DEF (worker, get_pid);
  39. LUA_FUNCTION_DEF (worker, is_scanner);
  40. LUA_FUNCTION_DEF (worker, is_primary_controller);
  41. LUA_FUNCTION_DEF (worker, spawn_process);
  42. const luaL_reg worker_reg[] = {
  43. LUA_INTERFACE_DEF (worker, get_name),
  44. LUA_INTERFACE_DEF (worker, get_stat),
  45. LUA_INTERFACE_DEF (worker, get_index),
  46. LUA_INTERFACE_DEF (worker, get_pid),
  47. LUA_INTERFACE_DEF (worker, spawn_process),
  48. LUA_INTERFACE_DEF (worker, is_scanner),
  49. LUA_INTERFACE_DEF (worker, is_primary_controller),
  50. {"__tostring", rspamd_lua_class_tostring},
  51. {NULL, NULL}
  52. };
  53. static const char rspamd_modules_state_global[] = "rspamd_plugins_state";
  54. static GQuark
  55. lua_error_quark (void)
  56. {
  57. return g_quark_from_static_string ("lua-routines");
  58. }
  59. /* Util functions */
  60. /**
  61. * Create new class and store metatable on top of the stack
  62. * @param L
  63. * @param classname name of class
  64. * @param func table of class methods
  65. */
  66. void
  67. rspamd_lua_new_class (lua_State * L,
  68. const gchar *classname,
  69. const struct luaL_reg *methods)
  70. {
  71. luaL_newmetatable (L, classname); /* mt */
  72. lua_pushstring (L, "__index");
  73. lua_pushvalue (L, -2); /* pushes the metatable */
  74. lua_settable (L, -3); /* metatable.__index = metatable */
  75. lua_pushstring (L, "class"); /* mt,"__index",it,"class" */
  76. lua_pushstring (L, classname); /* mt,"__index",it,"class",classname */
  77. lua_rawset (L, -3); /* mt,"__index",it */
  78. luaL_register (L, NULL, methods);
  79. }
  80. /**
  81. * Create and register new class with static methods and store metatable on top of the stack
  82. */
  83. void
  84. rspamd_lua_new_class_full (lua_State *L,
  85. const gchar *classname,
  86. const gchar *static_name,
  87. const struct luaL_reg *methods,
  88. const struct luaL_reg *func)
  89. {
  90. rspamd_lua_new_class (L, classname, methods);
  91. luaL_register (L, static_name, func);
  92. }
  93. static const gchar *
  94. rspamd_lua_class_tostring_buf (lua_State *L, gboolean print_pointer, gint pos)
  95. {
  96. static gchar buf[64];
  97. const gchar *ret = NULL;
  98. gint pop = 0;
  99. if (!lua_getmetatable (L, pos)) {
  100. goto err;
  101. }
  102. lua_pushstring (L, "__index");
  103. lua_gettable (L, -2);
  104. pop ++;
  105. if (!lua_istable (L, -1)) {
  106. goto err;
  107. }
  108. lua_pushstring (L, "class");
  109. lua_gettable (L, -2);
  110. pop ++;
  111. if (!lua_isstring (L, -1)) {
  112. goto err;
  113. }
  114. if (print_pointer) {
  115. rspamd_snprintf (buf, sizeof (buf), "%s(%p)", lua_tostring (L, -1),
  116. lua_touserdata (L, 1));
  117. }
  118. else {
  119. rspamd_snprintf (buf, sizeof (buf), "%s", lua_tostring (L, -1));
  120. }
  121. ret = buf;
  122. err:
  123. lua_pop (L, pop);
  124. return ret;
  125. }
  126. gint
  127. rspamd_lua_class_tostring (lua_State * L)
  128. {
  129. const gchar *p;
  130. p = rspamd_lua_class_tostring_buf (L, TRUE, 1);
  131. if (!p) {
  132. lua_pushstring (L, "invalid object passed to 'lua_common.c:__tostring'");
  133. return lua_error (L);
  134. }
  135. lua_pushstring (L, p);
  136. return 1;
  137. }
  138. void
  139. rspamd_lua_setclass (lua_State * L, const gchar *classname, gint objidx)
  140. {
  141. luaL_getmetatable (L, classname);
  142. if (objidx < 0) {
  143. objidx--;
  144. }
  145. lua_setmetatable (L, objidx);
  146. }
  147. /* assume that table is at the top */
  148. void
  149. rspamd_lua_table_set (lua_State * L, const gchar *index, const gchar *value)
  150. {
  151. lua_pushstring (L, index);
  152. if (value) {
  153. lua_pushstring (L, value);
  154. }
  155. else {
  156. lua_pushnil (L);
  157. }
  158. lua_settable (L, -3);
  159. }
  160. const gchar *
  161. rspamd_lua_table_get (lua_State *L, const gchar *index)
  162. {
  163. const gchar *result;
  164. lua_pushstring (L, index);
  165. lua_gettable (L, -2);
  166. if (!lua_isstring (L, -1)) {
  167. return NULL;
  168. }
  169. result = lua_tostring (L, -1);
  170. lua_pop (L, 1);
  171. return result;
  172. }
  173. static void
  174. lua_add_actions_global (lua_State *L)
  175. {
  176. gint i;
  177. lua_newtable (L);
  178. for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
  179. lua_pushstring (L, rspamd_action_to_str (i));
  180. lua_pushinteger (L, i);
  181. lua_settable (L, -3);
  182. }
  183. /* Set global table */
  184. lua_setglobal (L, "rspamd_actions");
  185. }
  186. #ifndef __APPLE__
  187. #define OS_SO_SUFFIX ".so"
  188. #else
  189. #define OS_SO_SUFFIX ".dylib"
  190. #endif
  191. void
  192. rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars)
  193. {
  194. const gchar *old_path, *additional_path = NULL;
  195. const ucl_object_t *opts = NULL;
  196. const gchar *pluginsdir = RSPAMD_PLUGINSDIR,
  197. *rulesdir = RSPAMD_RULESDIR,
  198. *lualibdir = RSPAMD_LUALIBDIR,
  199. *libdir = RSPAMD_LIBDIR;
  200. const gchar *t;
  201. gchar path_buf[PATH_MAX];
  202. lua_getglobal (L, "package");
  203. lua_getfield (L, -1, "path");
  204. old_path = luaL_checkstring (L, -1);
  205. if (strstr (old_path, RSPAMD_PLUGINSDIR) != NULL) {
  206. /* Path has been already set, do not touch it */
  207. lua_pop (L, 2);
  208. return;
  209. }
  210. if (cfg_obj) {
  211. opts = ucl_object_lookup (cfg_obj, "options");
  212. if (opts != NULL) {
  213. opts = ucl_object_lookup (opts, "lua_path");
  214. if (opts != NULL && ucl_object_type (opts) == UCL_STRING) {
  215. additional_path = ucl_object_tostring (opts);
  216. }
  217. }
  218. }
  219. /* Try environment */
  220. t = getenv ("PLUGINSDIR");
  221. if (t) {
  222. pluginsdir = t;
  223. }
  224. t = getenv ("RULESDIR");
  225. if (t) {
  226. rulesdir = t;
  227. }
  228. t = getenv ("LUALIBDIR");
  229. if (t) {
  230. lualibdir = t;
  231. }
  232. t = getenv ("LIBDIR");
  233. if (t) {
  234. libdir = t;
  235. }
  236. t = getenv ("RSPAMD_LIBDIR");
  237. if (t) {
  238. libdir = t;
  239. }
  240. if (vars) {
  241. t = g_hash_table_lookup (vars, "PLUGINSDIR");
  242. if (t) {
  243. pluginsdir = t;
  244. }
  245. t = g_hash_table_lookup (vars, "RULESDIR");
  246. if (t) {
  247. rulesdir = t;
  248. }
  249. t = g_hash_table_lookup (vars, "LUALIBDIR");
  250. if (t) {
  251. lualibdir = t;
  252. }
  253. t = g_hash_table_lookup (vars, "LIBDIR");
  254. if (t) {
  255. libdir = t;
  256. }
  257. t = g_hash_table_lookup (vars, "RSPAMD_LIBDIR");
  258. if (t) {
  259. libdir = t;
  260. }
  261. }
  262. if (additional_path) {
  263. rspamd_snprintf (path_buf, sizeof (path_buf),
  264. "%s/lua/?.lua;"
  265. "%s/lua/?.lua;"
  266. "%s/?.lua;"
  267. "%s/?.lua;"
  268. "%s/?/init.lua;"
  269. "%s;"
  270. "%s",
  271. pluginsdir, RSPAMD_CONFDIR, rulesdir,
  272. lualibdir, lualibdir,
  273. additional_path, old_path);
  274. }
  275. else {
  276. rspamd_snprintf (path_buf, sizeof (path_buf),
  277. "%s/lua/?.lua;"
  278. "%s/lua/?.lua;"
  279. "%s/?.lua;"
  280. "%s/?.lua;"
  281. "%s/?/init.lua;"
  282. "%s",
  283. pluginsdir, RSPAMD_CONFDIR, rulesdir,
  284. lualibdir, lualibdir,
  285. old_path);
  286. }
  287. lua_pop (L, 1);
  288. lua_pushstring (L, path_buf);
  289. lua_setfield (L, -2, "path");
  290. lua_getglobal (L, "package");
  291. lua_getfield (L, -1, "cpath");
  292. old_path = luaL_checkstring (L, -1);
  293. additional_path = NULL;
  294. if (opts != NULL) {
  295. opts = ucl_object_lookup (opts, "lua_cpath");
  296. if (opts != NULL && ucl_object_type (opts) == UCL_STRING) {
  297. additional_path = ucl_object_tostring (opts);
  298. }
  299. }
  300. if (additional_path) {
  301. rspamd_snprintf (path_buf, sizeof (path_buf),
  302. "%s/?%s;"
  303. "%s;"
  304. "%s",
  305. libdir,
  306. OS_SO_SUFFIX,
  307. additional_path,
  308. old_path);
  309. }
  310. else {
  311. rspamd_snprintf (path_buf, sizeof (path_buf),
  312. "%s/?%s;"
  313. "%s",
  314. libdir,
  315. OS_SO_SUFFIX,
  316. old_path);
  317. }
  318. lua_pop (L, 1);
  319. lua_pushstring (L, path_buf);
  320. lua_setfield (L, -2, "cpath");
  321. lua_pop (L, 1);
  322. }
  323. static gint
  324. rspamd_lua_cmp_version_components (const gchar *comp1, const gchar *comp2)
  325. {
  326. guint v1, v2;
  327. v1 = strtoul (comp1, NULL, 10);
  328. v2 = strtoul (comp2, NULL, 10);
  329. return v1 - v2;
  330. }
  331. static int
  332. rspamd_lua_rspamd_version_cmp (lua_State *L)
  333. {
  334. const gchar *ver;
  335. gchar **components;
  336. gint ret = 0;
  337. if (lua_type (L, 2) == LUA_TSTRING) {
  338. ver = lua_tostring (L, 2);
  339. components = g_strsplit_set (ver, ".-_", -1);
  340. if (!components) {
  341. return luaL_error (L, "invalid arguments to 'cmp': %s", ver);
  342. }
  343. if (components[0]) {
  344. ret = rspamd_lua_cmp_version_components (components[0],
  345. RSPAMD_VERSION_MAJOR);
  346. }
  347. if (ret) {
  348. goto set;
  349. }
  350. if (components[1]) {
  351. ret = rspamd_lua_cmp_version_components (components[1],
  352. RSPAMD_VERSION_MINOR);
  353. }
  354. if (ret) {
  355. goto set;
  356. }
  357. if (components[2]) {
  358. ret = rspamd_lua_cmp_version_components (components[2],
  359. RSPAMD_VERSION_PATCH);
  360. }
  361. /*
  362. * XXX: we don't compare git releases assuming that it is meaningless
  363. */
  364. }
  365. else {
  366. return luaL_error (L, "invalid arguments to 'cmp'");
  367. }
  368. set:
  369. g_strfreev (components);
  370. lua_pushnumber (L, ret);
  371. return 1;
  372. }
  373. static int
  374. rspamd_lua_rspamd_version_numeric (lua_State *L)
  375. {
  376. static gint64 version_num = RSPAMD_VERSION_NUM;
  377. const gchar *type;
  378. if (lua_gettop (L) >= 2 && lua_type (L, 1) == LUA_TSTRING) {
  379. type = lua_tostring (L, 1);
  380. if (g_ascii_strcasecmp (type, "short") == 0) {
  381. version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 +
  382. RSPAMD_VERSION_MINOR_NUM * 100 +
  383. RSPAMD_VERSION_PATCH_NUM * 10;
  384. }
  385. else if (g_ascii_strcasecmp (type, "main") == 0) {
  386. version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 +
  387. RSPAMD_VERSION_MINOR_NUM * 100;
  388. }
  389. else if (g_ascii_strcasecmp (type, "major") == 0) {
  390. version_num = RSPAMD_VERSION_MAJOR_NUM;
  391. }
  392. else if (g_ascii_strcasecmp (type, "minor") == 0) {
  393. version_num = RSPAMD_VERSION_MINOR_NUM;
  394. }
  395. else if (g_ascii_strcasecmp (type, "patch") == 0) {
  396. version_num = RSPAMD_VERSION_PATCH_NUM;
  397. }
  398. }
  399. lua_pushnumber (L, version_num);
  400. return 1;
  401. }
  402. static int
  403. rspamd_lua_rspamd_version (lua_State *L)
  404. {
  405. const gchar *result = NULL, *type;
  406. if (lua_gettop (L) == 0) {
  407. result = RVERSION;
  408. }
  409. else if (lua_gettop (L) >= 1 && lua_type (L, 1) == LUA_TSTRING) {
  410. /* We got something like string */
  411. type = lua_tostring (L, 1);
  412. if (g_ascii_strcasecmp (type, "short") == 0) {
  413. result = RSPAMD_VERSION_MAJOR
  414. "." RSPAMD_VERSION_MINOR
  415. "." RSPAMD_VERSION_PATCH;
  416. }
  417. else if (g_ascii_strcasecmp (type, "main") == 0) {
  418. result = RSPAMD_VERSION_MAJOR "." RSPAMD_VERSION_MINOR;
  419. }
  420. else if (g_ascii_strcasecmp (type, "major") == 0) {
  421. result = RSPAMD_VERSION_MAJOR;
  422. }
  423. else if (g_ascii_strcasecmp (type, "minor") == 0) {
  424. result = RSPAMD_VERSION_MINOR;
  425. }
  426. else if (g_ascii_strcasecmp (type, "patch") == 0) {
  427. result = RSPAMD_VERSION_PATCH;
  428. }
  429. else if (g_ascii_strcasecmp (type, "id") == 0) {
  430. result = RID;
  431. }
  432. else if (g_ascii_strcasecmp (type, "num") == 0) {
  433. return rspamd_lua_rspamd_version_numeric (L);
  434. }
  435. else if (g_ascii_strcasecmp (type, "cmp") == 0) {
  436. return rspamd_lua_rspamd_version_cmp (L);
  437. }
  438. }
  439. lua_pushstring (L, result);
  440. return 1;
  441. }
  442. void
  443. rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L,
  444. GHashTable *vars)
  445. {
  446. struct rspamd_config **pcfg;
  447. gint orig_top = lua_gettop (L);
  448. /* First check for global variable 'config' */
  449. lua_getglobal (L, "config");
  450. if (lua_isnil (L, -1)) {
  451. /* Assign global table to set up attributes */
  452. lua_newtable (L);
  453. lua_setglobal (L, "config");
  454. }
  455. lua_getglobal (L, "metrics");
  456. if (lua_isnil (L, -1)) {
  457. lua_newtable (L);
  458. lua_setglobal (L, "metrics");
  459. }
  460. lua_getglobal (L, "composites");
  461. if (lua_isnil (L, -1)) {
  462. lua_newtable (L);
  463. lua_setglobal (L, "composites");
  464. }
  465. lua_getglobal (L, "rspamd_classifiers");
  466. if (lua_isnil (L, -1)) {
  467. lua_newtable (L);
  468. lua_setglobal (L, "rspamd_classifiers");
  469. }
  470. lua_getglobal (L, "classifiers");
  471. if (lua_isnil (L, -1)) {
  472. lua_newtable (L);
  473. lua_setglobal (L, "classifiers");
  474. }
  475. lua_getglobal (L, "rspamd_version");
  476. if (lua_isnil (L, -1)) {
  477. lua_pushcfunction (L, rspamd_lua_rspamd_version);
  478. lua_setglobal (L, "rspamd_version");
  479. }
  480. if (cfg != NULL) {
  481. pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
  482. rspamd_lua_setclass (L, "rspamd{config}", -1);
  483. *pcfg = cfg;
  484. lua_setglobal (L, "rspamd_config");
  485. }
  486. lua_settop (L, orig_top);
  487. /* Set known paths as rspamd_paths global */
  488. lua_getglobal (L, "rspamd_paths");
  489. if (lua_isnil (L, -1)) {
  490. const gchar *confdir = RSPAMD_CONFDIR, *rundir = RSPAMD_RUNDIR,
  491. *dbdir = RSPAMD_DBDIR, *logdir = RSPAMD_LOGDIR,
  492. *wwwdir = RSPAMD_WWWDIR, *pluginsdir = RSPAMD_PLUGINSDIR,
  493. *rulesdir = RSPAMD_RULESDIR, *lualibdir = RSPAMD_LUALIBDIR,
  494. *prefix = RSPAMD_PREFIX;
  495. const gchar *t;
  496. /* Try environment */
  497. t = getenv ("PLUGINSDIR");
  498. if (t) {
  499. pluginsdir = t;
  500. }
  501. t = getenv ("RULESDIR");
  502. if (t) {
  503. rulesdir = t;
  504. }
  505. t = getenv ("DBDIR");
  506. if (t) {
  507. dbdir = t;
  508. }
  509. t = getenv ("RUNDIR");
  510. if (t) {
  511. rundir = t;
  512. }
  513. t = getenv ("LUALIBDIR");
  514. if (t) {
  515. lualibdir = t;
  516. }
  517. t = getenv ("LOGDIR");
  518. if (t) {
  519. logdir = t;
  520. }
  521. t = getenv ("WWWDIR");
  522. if (t) {
  523. wwwdir = t;
  524. }
  525. t = getenv ("CONFDIR");
  526. if (t) {
  527. confdir = t;
  528. }
  529. if (vars) {
  530. t = g_hash_table_lookup (vars, "PLUGINSDIR");
  531. if (t) {
  532. pluginsdir = t;
  533. }
  534. t = g_hash_table_lookup (vars, "RULESDIR");
  535. if (t) {
  536. rulesdir = t;
  537. }
  538. t = g_hash_table_lookup (vars, "LUALIBDIR");
  539. if (t) {
  540. lualibdir = t;
  541. }
  542. t = g_hash_table_lookup (vars, "RUNDIR");
  543. if (t) {
  544. rundir = t;
  545. }
  546. t = g_hash_table_lookup (vars, "WWWDIR");
  547. if (t) {
  548. wwwdir = t;
  549. }
  550. t = g_hash_table_lookup (vars, "CONFDIR");
  551. if (t) {
  552. confdir = t;
  553. }
  554. t = g_hash_table_lookup (vars, "DBDIR");
  555. if (t) {
  556. dbdir = t;
  557. }
  558. t = g_hash_table_lookup (vars, "LOGDIR");
  559. if (t) {
  560. logdir = t;
  561. }
  562. }
  563. lua_createtable (L, 0, 9);
  564. rspamd_lua_table_set (L, RSPAMD_CONFDIR_INDEX, confdir);
  565. rspamd_lua_table_set (L, RSPAMD_RUNDIR_INDEX, rundir);
  566. rspamd_lua_table_set (L, RSPAMD_DBDIR_INDEX, dbdir);
  567. rspamd_lua_table_set (L, RSPAMD_LOGDIR_INDEX, logdir);
  568. rspamd_lua_table_set (L, RSPAMD_WWWDIR_INDEX, wwwdir);
  569. rspamd_lua_table_set (L, RSPAMD_PLUGINSDIR_INDEX, pluginsdir);
  570. rspamd_lua_table_set (L, RSPAMD_RULESDIR_INDEX, rulesdir);
  571. rspamd_lua_table_set (L, RSPAMD_LUALIBDIR_INDEX, lualibdir);
  572. rspamd_lua_table_set (L, RSPAMD_PREFIX_INDEX, prefix);
  573. lua_setglobal (L, "rspamd_paths");
  574. }
  575. lua_settop (L, orig_top);
  576. }
  577. #ifdef WITH_LUA_TRACE
  578. static gint
  579. lua_push_trace_data (lua_State *L)
  580. {
  581. if (lua_traces) {
  582. ucl_object_push_lua (L, lua_traces, true);
  583. }
  584. else {
  585. lua_pushnil (L);
  586. }
  587. return 1;
  588. }
  589. #endif
  590. lua_State *
  591. rspamd_lua_init ()
  592. {
  593. lua_State *L;
  594. L = luaL_newstate ();
  595. luaL_openlibs (L);
  596. luaopen_logger (L);
  597. luaopen_mempool (L);
  598. luaopen_config (L);
  599. luaopen_map (L);
  600. luaopen_trie (L);
  601. luaopen_task (L);
  602. luaopen_textpart (L);
  603. luaopen_mimepart (L);
  604. luaopen_image (L);
  605. luaopen_url (L);
  606. luaopen_classifier (L);
  607. luaopen_statfile (L);
  608. luaopen_regexp (L);
  609. luaopen_cdb (L);
  610. luaopen_xmlrpc (L);
  611. luaopen_http (L);
  612. luaopen_redis (L);
  613. luaopen_upstream (L);
  614. lua_add_actions_global (L);
  615. luaopen_dns_resolver (L);
  616. luaopen_rsa (L);
  617. luaopen_ip (L);
  618. luaopen_expression (L);
  619. luaopen_text (L);
  620. luaopen_util (L);
  621. luaopen_tcp (L);
  622. luaopen_html (L);
  623. luaopen_fann (L);
  624. luaopen_sqlite3 (L);
  625. luaopen_cryptobox (L);
  626. luaL_newmetatable (L, "rspamd{ev_base}");
  627. lua_pushstring (L, "class");
  628. lua_pushstring (L, "rspamd{ev_base}");
  629. lua_rawset (L, -3);
  630. lua_pop (L, 1);
  631. luaL_newmetatable (L, "rspamd{session}");
  632. lua_pushstring (L, "class");
  633. lua_pushstring (L, "rspamd{session}");
  634. lua_rawset (L, -3);
  635. lua_pop (L, 1);
  636. rspamd_lua_new_class (L, "rspamd{worker}", worker_reg);
  637. rspamd_lua_add_preload (L, "lpeg", luaopen_lpeg);
  638. luaopen_ucl (L);
  639. rspamd_lua_add_preload (L, "ucl", luaopen_ucl);
  640. /* Add plugins global */
  641. lua_newtable (L);
  642. lua_setglobal (L, "rspamd_plugins");
  643. /* Set PRNG */
  644. lua_getglobal (L, "math");
  645. lua_pushstring (L, "randomseed");
  646. lua_gettable (L, -2);
  647. lua_pushnumber (L, ottery_rand_uint64 ());
  648. lua_pcall (L, 1, 0, 0);
  649. lua_pop (L, 1); /* math table */
  650. /* Modules state */
  651. lua_newtable (L);
  652. /*
  653. * rspamd_plugins_state = {
  654. * enabled = {},
  655. * disabled_unconfigured = {},
  656. * disabled_redis = {},
  657. * disabled_explicitly = {},
  658. * disabled_failed = {},
  659. * disabled_experimental = {},
  660. * }
  661. */
  662. #define ADD_TABLE(name) do { \
  663. lua_pushstring (L, #name); \
  664. lua_newtable (L); \
  665. lua_settable (L, -3); \
  666. } while (0)
  667. ADD_TABLE (enabled);
  668. ADD_TABLE (disabled_unconfigured);
  669. ADD_TABLE (disabled_redis);
  670. ADD_TABLE (disabled_explicitly);
  671. ADD_TABLE (disabled_failed);
  672. ADD_TABLE (disabled_experimental);
  673. #undef ADD_TABLE
  674. lua_setglobal (L, rspamd_modules_state_global);
  675. #ifdef WITH_LUA_TRACE
  676. lua_pushcfunction (L, lua_push_trace_data);
  677. lua_setglobal (L, "get_traces");
  678. #endif
  679. return L;
  680. }
  681. /**
  682. * Initialize new locked lua_State structure
  683. */
  684. struct lua_locked_state *
  685. rspamd_init_lua_locked (struct rspamd_config *cfg)
  686. {
  687. struct lua_locked_state *new;
  688. new = g_malloc0 (sizeof (struct lua_locked_state));
  689. new->L = rspamd_lua_init ();
  690. new->m = rspamd_mutex_new ();
  691. return new;
  692. }
  693. /**
  694. * Free locked state structure
  695. */
  696. void
  697. rspamd_free_lua_locked (struct lua_locked_state *st)
  698. {
  699. g_assert (st != NULL);
  700. lua_close (st->L);
  701. rspamd_mutex_free (st->m);
  702. g_free (st);
  703. }
  704. void
  705. rspamd_plugins_table_push_elt (lua_State *L, const gchar *field_name,
  706. const gchar *new_elt)
  707. {
  708. lua_getglobal (L, rspamd_modules_state_global);
  709. lua_pushstring (L, field_name);
  710. lua_gettable (L, -2);
  711. lua_pushstring (L, new_elt);
  712. lua_newtable (L);
  713. lua_settable (L, -3);
  714. lua_pop (L, 2); /* Global + element */
  715. }
  716. gboolean
  717. rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load)
  718. {
  719. struct rspamd_config **pcfg;
  720. GList *cur;
  721. struct script_module *module;
  722. lua_State *L = cfg->lua_state;
  723. GString *tb;
  724. gint err_idx;
  725. cur = g_list_first (cfg->script_modules);
  726. while (cur) {
  727. module = cur->data;
  728. if (module->path) {
  729. if (!force_load) {
  730. if (!rspamd_config_is_module_enabled (cfg, module->name)) {
  731. cur = g_list_next (cur);
  732. continue;
  733. }
  734. }
  735. lua_pushcfunction (L, &rspamd_lua_traceback);
  736. err_idx = lua_gettop (L);
  737. if (luaL_loadfile (L, module->path) != 0) {
  738. msg_err_config ("load of %s failed: %s", module->path,
  739. lua_tostring (L, -1));
  740. lua_pop (L, 1); /* Error function */
  741. rspamd_plugins_table_push_elt (L, "disabled_failed",
  742. module->name);
  743. cur = g_list_next (cur);
  744. continue;
  745. }
  746. /* Initialize config structure */
  747. pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
  748. rspamd_lua_setclass (L, "rspamd{config}", -1);
  749. *pcfg = cfg;
  750. lua_setglobal (L, "rspamd_config");
  751. if (lua_pcall (L, 0, 0, err_idx) != 0) {
  752. tb = lua_touserdata (L, -1);
  753. msg_err_config ("init of %s failed: %v",
  754. module->path,
  755. tb);
  756. g_string_free (tb, TRUE);
  757. lua_pop (L, 2); /* Result and error function */
  758. rspamd_plugins_table_push_elt (L, "disabled_failed",
  759. module->name);
  760. cur = g_list_next (cur);
  761. continue;
  762. }
  763. if (!force_load) {
  764. msg_info_config ("init lua module %s", module->name);
  765. }
  766. lua_pop (L, 1); /* Error function */
  767. }
  768. cur = g_list_next (cur);
  769. }
  770. return TRUE;
  771. }
  772. void
  773. rspamd_lua_dumpstack (lua_State *L)
  774. {
  775. gint i, t, r = 0;
  776. gint top = lua_gettop (L);
  777. gchar buf[BUFSIZ];
  778. r += rspamd_snprintf (buf + r, sizeof (buf) - r, "lua stack: ");
  779. for (i = 1; i <= top; i++) { /* repeat for each level */
  780. t = lua_type (L, i);
  781. switch (t)
  782. {
  783. case LUA_TSTRING: /* strings */
  784. r += rspamd_snprintf (buf + r,
  785. sizeof (buf) - r,
  786. "str: %s",
  787. lua_tostring (L, i));
  788. break;
  789. case LUA_TBOOLEAN: /* booleans */
  790. r += rspamd_snprintf (buf + r, sizeof (buf) - r,
  791. lua_toboolean (L, i) ? "bool: true" : "bool: false");
  792. break;
  793. case LUA_TNUMBER: /* numbers */
  794. r += rspamd_snprintf (buf + r,
  795. sizeof (buf) - r,
  796. "number: %.2f",
  797. lua_tonumber (L, i));
  798. break;
  799. default: /* other values */
  800. r += rspamd_snprintf (buf + r,
  801. sizeof (buf) - r,
  802. "type: %s",
  803. lua_typename (L, t));
  804. break;
  805. }
  806. if (i < top) {
  807. r += rspamd_snprintf (buf + r, sizeof (buf) - r, " -> "); /* put a separator */
  808. }
  809. }
  810. msg_info (buf);
  811. }
  812. gpointer
  813. rspamd_lua_check_class (lua_State *L, gint index, const gchar *name)
  814. {
  815. gpointer p;
  816. if (lua_type (L, index) == LUA_TUSERDATA) {
  817. p = lua_touserdata (L, index);
  818. if (p) {
  819. if (lua_getmetatable (L, index)) {
  820. lua_getfield (L, LUA_REGISTRYINDEX, name); /* get correct metatable */
  821. if (lua_rawequal (L, -1, -2)) { /* does it have the correct mt? */
  822. lua_pop (L, 2); /* remove both metatables */
  823. return p;
  824. }
  825. lua_pop (L, 2);
  826. }
  827. }
  828. }
  829. return NULL;
  830. }
  831. int
  832. rspamd_lua_typerror (lua_State *L, int narg, const char *tname)
  833. {
  834. const char *msg = lua_pushfstring (L, "%s expected, got %s", tname,
  835. luaL_typename (L, narg));
  836. return luaL_argerror (L, narg, msg);
  837. }
  838. void
  839. rspamd_lua_add_preload (lua_State *L, const gchar *name, lua_CFunction func)
  840. {
  841. lua_getglobal (L, "package");
  842. lua_pushstring (L, "preload");
  843. lua_gettable (L, -2);
  844. lua_pushcfunction (L, func);
  845. lua_setfield (L, -2, name);
  846. lua_pop (L, 2); /* preload key + global package */
  847. }
  848. gboolean
  849. rspamd_lua_parse_table_arguments (lua_State *L, gint pos,
  850. GError **err, const gchar *extraction_pattern, ...)
  851. {
  852. const gchar *p, *key = NULL, *end, *cls;
  853. va_list ap;
  854. gboolean required = FALSE, failed = FALSE, is_table;
  855. gchar classbuf[128];
  856. enum {
  857. read_key = 0,
  858. read_arg,
  859. read_class_start,
  860. read_class,
  861. read_semicolon
  862. } state = read_key;
  863. gsize keylen = 0, *valuelen, clslen;
  864. gint idx = 0, t;
  865. g_assert (extraction_pattern != NULL);
  866. if (pos < 0) {
  867. /* Get absolute pos */
  868. pos = lua_gettop (L) + pos + 1;
  869. }
  870. if (lua_type (L, pos) == LUA_TTABLE) {
  871. is_table = TRUE;
  872. }
  873. else {
  874. is_table = FALSE;
  875. idx = pos;
  876. }
  877. p = extraction_pattern;
  878. end = p + strlen (extraction_pattern);
  879. va_start (ap, extraction_pattern);
  880. while (p <= end) {
  881. switch (state) {
  882. case read_key:
  883. if (*p == '=') {
  884. if (key == NULL) {
  885. g_set_error (err, lua_error_quark (), 1, "cannot read key");
  886. va_end (ap);
  887. return FALSE;
  888. }
  889. state = read_arg;
  890. keylen = p - key;
  891. }
  892. else if (*p == '*' && key == NULL) {
  893. required = TRUE;
  894. }
  895. else if (key == NULL) {
  896. key = p;
  897. }
  898. p ++;
  899. break;
  900. case read_arg:
  901. g_assert (keylen != 0);
  902. if (is_table) {
  903. lua_pushlstring (L, key, keylen);
  904. lua_gettable (L, pos);
  905. idx = -1;
  906. }
  907. t = lua_type (L, idx);
  908. switch (g_ascii_toupper (*p)) {
  909. case 'S':
  910. if (t == LUA_TSTRING) {
  911. *(va_arg (ap, const gchar **)) = lua_tostring (L, idx);
  912. }
  913. else if (t == LUA_TNIL || t == LUA_TNONE) {
  914. failed = TRUE;
  915. *(va_arg (ap, const gchar **)) = NULL;
  916. }
  917. else {
  918. g_set_error (err,
  919. lua_error_quark (),
  920. 1,
  921. "bad type for key:"
  922. " %.*s: '%s', '%s' is expected",
  923. (gint) keylen,
  924. key,
  925. lua_typename (L, lua_type (L, idx)), "string");
  926. va_end (ap);
  927. return FALSE;
  928. }
  929. if (is_table) {
  930. lua_pop (L, 1);
  931. }
  932. break;
  933. case 'I':
  934. if (t == LUA_TNUMBER) {
  935. *(va_arg (ap, gint64 *)) = lua_tonumber (L, idx);
  936. }
  937. else if (t == LUA_TNIL || t == LUA_TNONE) {
  938. failed = TRUE;
  939. *(va_arg (ap, gint64 *)) = 0;
  940. }
  941. else {
  942. g_set_error (err,
  943. lua_error_quark (),
  944. 1,
  945. "bad type for key:"
  946. " %.*s: '%s', '%s' is expected",
  947. (gint) keylen,
  948. key,
  949. lua_typename (L, lua_type (L, idx)),
  950. "int64");
  951. va_end (ap);
  952. return FALSE;
  953. }
  954. if (is_table) {
  955. lua_pop (L, 1);
  956. }
  957. break;
  958. case 'F':
  959. if (t == LUA_TFUNCTION) {
  960. if (!is_table) {
  961. lua_pushvalue (L, idx);
  962. }
  963. *(va_arg (ap, gint *)) = luaL_ref (L, LUA_REGISTRYINDEX);
  964. }
  965. else if (t == LUA_TNIL || t == LUA_TNONE) {
  966. failed = TRUE;
  967. *(va_arg (ap, gint *)) = -1;
  968. if (is_table) {
  969. lua_pop (L, 1);
  970. }
  971. }
  972. else {
  973. g_set_error (err,
  974. lua_error_quark (),
  975. 1,
  976. "bad type for key:"
  977. " %.*s: '%s', '%s' is expected",
  978. (gint) keylen,
  979. key,
  980. lua_typename (L, lua_type (L, idx)),
  981. "function");
  982. va_end (ap);
  983. if (is_table) {
  984. lua_pop (L, 1);
  985. }
  986. return FALSE;
  987. }
  988. /* luaL_ref pops argument from the stack */
  989. break;
  990. case 'B':
  991. if (t == LUA_TBOOLEAN) {
  992. *(va_arg (ap, gboolean *)) = lua_toboolean (L, idx);
  993. }
  994. else if (t == LUA_TNIL || t == LUA_TNONE) {
  995. failed = TRUE;
  996. *(va_arg (ap, gboolean *)) = 0;
  997. }
  998. else {
  999. g_set_error (err,
  1000. lua_error_quark (),
  1001. 1,
  1002. "bad type for key:"
  1003. " %.*s: '%s', '%s' is expected",
  1004. (gint) keylen,
  1005. key,
  1006. lua_typename (L, lua_type (L, idx)),
  1007. "bool");
  1008. va_end (ap);
  1009. return FALSE;
  1010. }
  1011. if (is_table) {
  1012. lua_pop (L, 1);
  1013. }
  1014. break;
  1015. case 'N':
  1016. if (t == LUA_TNUMBER) {
  1017. *(va_arg (ap, gdouble *)) = lua_tonumber (L, idx);
  1018. }
  1019. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1020. failed = TRUE;
  1021. *(va_arg (ap, gdouble *)) = 0;
  1022. }
  1023. else {
  1024. g_set_error (err,
  1025. lua_error_quark (),
  1026. 1,
  1027. "bad type for key:"
  1028. " %.*s: '%s', '%s' is expected",
  1029. (gint) keylen,
  1030. key,
  1031. lua_typename (L, lua_type (L, idx)),
  1032. "double");
  1033. va_end (ap);
  1034. return FALSE;
  1035. }
  1036. if (is_table) {
  1037. lua_pop (L, 1);
  1038. }
  1039. break;
  1040. case 'D':
  1041. if (t == LUA_TNUMBER) {
  1042. *(va_arg (ap, gdouble *)) = lua_tonumber (L, idx);
  1043. }
  1044. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1045. failed = TRUE;
  1046. *(va_arg (ap, gdouble *)) = NAN;
  1047. }
  1048. else {
  1049. g_set_error (err,
  1050. lua_error_quark (),
  1051. 1,
  1052. "bad type for key:"
  1053. " %.*s: '%s', '%s' is expected",
  1054. (gint) keylen,
  1055. key,
  1056. lua_typename (L, lua_type (L, idx)),
  1057. "double");
  1058. va_end (ap);
  1059. return FALSE;
  1060. }
  1061. if (is_table) {
  1062. lua_pop (L, 1);
  1063. }
  1064. break;
  1065. case 'V':
  1066. valuelen = va_arg (ap, gsize *);
  1067. if (t == LUA_TSTRING) {
  1068. *(va_arg (ap, const gchar **)) = lua_tolstring (L, idx,
  1069. valuelen);
  1070. }
  1071. else if (t == LUA_TNIL || t == LUA_TNONE) {
  1072. failed = TRUE;
  1073. *(va_arg (ap, const char **)) = NULL;
  1074. *valuelen = 0;
  1075. }
  1076. else {
  1077. g_set_error (err,
  1078. lua_error_quark (),
  1079. 1,
  1080. "bad type for key:"
  1081. " %.*s: '%s', '%s' is expected",
  1082. (gint) keylen,
  1083. key,
  1084. lua_typename (L, lua_type (L, idx)),
  1085. "string");
  1086. va_end (ap);
  1087. return FALSE;
  1088. }
  1089. if (is_table) {
  1090. lua_pop (L, 1);
  1091. }
  1092. break;
  1093. case 'O':
  1094. if (t != LUA_TNONE) {
  1095. *(va_arg (ap, ucl_object_t **)) = ucl_object_lua_import (L,
  1096. idx);
  1097. }
  1098. else {
  1099. failed = TRUE;
  1100. *(va_arg (ap, ucl_object_t **)) = NULL;
  1101. }
  1102. if (is_table) {
  1103. lua_pop (L, 1);
  1104. }
  1105. break;
  1106. case 'U':
  1107. if (t == LUA_TNIL || t == LUA_TNONE) {
  1108. failed = TRUE;
  1109. *(va_arg (ap, void **)) = NULL;
  1110. }
  1111. else if (t != LUA_TUSERDATA) {
  1112. g_set_error (err,
  1113. lua_error_quark (),
  1114. 1,
  1115. "bad type for key:"
  1116. " %.*s: '%s', '%s' is expected",
  1117. (gint) keylen,
  1118. key,
  1119. lua_typename (L, lua_type (L, idx)),
  1120. "int64");
  1121. va_end (ap);
  1122. return FALSE;
  1123. }
  1124. state = read_class_start;
  1125. clslen = 0;
  1126. cls = NULL;
  1127. p ++;
  1128. continue;
  1129. default:
  1130. g_assert (0);
  1131. break;
  1132. }
  1133. if (failed && required) {
  1134. g_set_error (err, lua_error_quark (), 2, "required parameter "
  1135. "%.*s is missing", (gint)keylen, key);
  1136. va_end (ap);
  1137. return FALSE;
  1138. }
  1139. if (!is_table) {
  1140. idx ++;
  1141. }
  1142. /* Reset read params */
  1143. state = read_semicolon;
  1144. failed = FALSE;
  1145. required = FALSE;
  1146. keylen = 0;
  1147. key = NULL;
  1148. p ++;
  1149. break;
  1150. case read_class_start:
  1151. if (*p == '{') {
  1152. cls = p + 1;
  1153. state = read_class;
  1154. }
  1155. else {
  1156. if (is_table) {
  1157. lua_pop (L, 1);
  1158. }
  1159. g_set_error (err, lua_error_quark (), 2, "missing classname for "
  1160. "%.*s", (gint)keylen, key);
  1161. va_end (ap);
  1162. return FALSE;
  1163. }
  1164. p ++;
  1165. break;
  1166. case read_class:
  1167. if (*p == '}') {
  1168. clslen = p - cls;
  1169. if (clslen == 0) {
  1170. if (is_table) {
  1171. lua_pop (L, 1);
  1172. }
  1173. g_set_error (err,
  1174. lua_error_quark (),
  1175. 2,
  1176. "empty classname for "
  1177. "%*.s",
  1178. (gint) keylen,
  1179. key);
  1180. va_end (ap);
  1181. return FALSE;
  1182. }
  1183. rspamd_snprintf (classbuf, sizeof (classbuf), "rspamd{%*s}",
  1184. (gint) clslen, cls);
  1185. /*
  1186. * We skip class check here for speed in non-table mode
  1187. */
  1188. if (!failed && (!is_table ||
  1189. rspamd_lua_check_class (L, idx, classbuf))) {
  1190. *(va_arg (ap, void **)) = *(void **)lua_touserdata (L, idx);
  1191. }
  1192. else {
  1193. if (!failed) {
  1194. g_set_error (err,
  1195. lua_error_quark (),
  1196. 2,
  1197. "invalid class for key %.*s, expected %s, got %s",
  1198. (gint) keylen,
  1199. key,
  1200. classbuf,
  1201. rspamd_lua_class_tostring_buf (L, FALSE, idx));
  1202. va_end (ap);
  1203. return FALSE;
  1204. }
  1205. }
  1206. if (is_table) {
  1207. lua_pop (L, 1);
  1208. }
  1209. else {
  1210. idx ++;
  1211. }
  1212. if (failed && required) {
  1213. g_set_error (err,
  1214. lua_error_quark (),
  1215. 2,
  1216. "required parameter "
  1217. "%.*s is missing",
  1218. (gint) keylen,
  1219. key);
  1220. va_end (ap);
  1221. return FALSE;
  1222. }
  1223. /* Reset read params */
  1224. state = read_semicolon;
  1225. failed = FALSE;
  1226. required = FALSE;
  1227. keylen = 0;
  1228. key = NULL;
  1229. }
  1230. p ++;
  1231. break;
  1232. case read_semicolon:
  1233. if (*p == ';' || p == end) {
  1234. state = read_key;
  1235. key = NULL;
  1236. keylen = 0;
  1237. failed = FALSE;
  1238. }
  1239. else {
  1240. g_set_error (err, lua_error_quark (), 2, "bad format string: %s,"
  1241. " at char %c, position %d",
  1242. extraction_pattern, *p, (int)(p - extraction_pattern));
  1243. va_end (ap);
  1244. return FALSE;
  1245. }
  1246. p++;
  1247. break;
  1248. }
  1249. }
  1250. va_end (ap);
  1251. return TRUE;
  1252. }
  1253. static void
  1254. rspamd_lua_traceback_string (lua_State *L, GString *s)
  1255. {
  1256. gint i = 1;
  1257. lua_Debug d;
  1258. while (lua_getstack (L, i++, &d)) {
  1259. lua_getinfo (L, "nSl", &d);
  1260. g_string_append_printf (s, " [%d]:{%s:%d - %s [%s]};",
  1261. i - 1, d.short_src, d.currentline,
  1262. (d.name ? d.name : "<unknown>"), d.what);
  1263. }
  1264. }
  1265. gint
  1266. rspamd_lua_traceback (lua_State *L)
  1267. {
  1268. GString *tb;
  1269. const gchar *msg = lua_tostring (L, 1);
  1270. tb = g_string_sized_new (100);
  1271. g_string_append_printf (tb, "%s; trace:", msg);
  1272. rspamd_lua_traceback_string (L, tb);
  1273. lua_pushlightuserdata (L, tb);
  1274. return 1;
  1275. }
  1276. guint
  1277. rspamd_lua_table_size (lua_State *L, gint tbl_pos)
  1278. {
  1279. guint tbl_size = 0;
  1280. if (!lua_istable (L, tbl_pos)) {
  1281. return 0;
  1282. }
  1283. #if LUA_VERSION_NUM >= 502
  1284. tbl_size = lua_rawlen (L, tbl_pos);
  1285. #else
  1286. tbl_size = lua_objlen (L, tbl_pos);
  1287. #endif
  1288. return tbl_size;
  1289. }
  1290. static void *
  1291. rspamd_lua_check_udata_common (lua_State *L, gint pos, const gchar *classname,
  1292. gboolean fatal)
  1293. {
  1294. void *p = lua_touserdata (L, pos);
  1295. GString *err_msg;
  1296. guint i, top = lua_gettop (L);
  1297. if (p == NULL) {
  1298. goto err;
  1299. }
  1300. else {
  1301. /* Match class */
  1302. if (lua_getmetatable (L, pos)) {
  1303. luaL_getmetatable (L, classname);
  1304. if (!lua_rawequal (L, -1, -2)) {
  1305. goto err;
  1306. }
  1307. }
  1308. else {
  1309. goto err;
  1310. }
  1311. }
  1312. lua_settop (L, top);
  1313. return p;
  1314. err:
  1315. if (fatal) {
  1316. const gchar *actual_classname = NULL;
  1317. if (lua_getmetatable (L, pos)) {
  1318. lua_pushstring (L, "__index");
  1319. lua_gettable (L, -3);
  1320. lua_pushstring (L, "class");
  1321. lua_gettable (L, -2);
  1322. actual_classname = lua_tostring (L, -1);
  1323. }
  1324. else {
  1325. actual_classname = lua_typename (L, lua_type (L, -1));
  1326. }
  1327. err_msg = g_string_sized_new (100);
  1328. rspamd_printf_gstring (err_msg, "expected %s at %d, but userdata has "
  1329. "%s metatable; trace: ",
  1330. classname, pos, actual_classname);
  1331. rspamd_lua_traceback_string (L, err_msg);
  1332. rspamd_printf_gstring (err_msg, " stack(%d): ", top);
  1333. for (i = 1; i <= MIN (top, 10); i ++) {
  1334. if (lua_type (L, i) == LUA_TUSERDATA) {
  1335. lua_getmetatable (L, i);
  1336. lua_pushstring (L, "__index");
  1337. lua_gettable (L, -3);
  1338. lua_pushstring (L, "class");
  1339. lua_gettable (L, -2);
  1340. rspamd_printf_gstring (err_msg, "[%d: ud=%s] ", i,
  1341. lua_tostring (L, -1));
  1342. lua_pop (L, 3);
  1343. }
  1344. else {
  1345. rspamd_printf_gstring (err_msg, "[%d: %s] ", i,
  1346. lua_typename (L, lua_type (L, i)));
  1347. }
  1348. }
  1349. msg_err ("lua type error: %v", err_msg);
  1350. g_string_free (err_msg, TRUE);
  1351. }
  1352. lua_settop (L, top);
  1353. return NULL;
  1354. }
  1355. void *
  1356. rspamd_lua_check_udata (lua_State *L, gint pos, const gchar *classname)
  1357. {
  1358. return rspamd_lua_check_udata_common (L, pos, classname, TRUE);
  1359. }
  1360. void *
  1361. rspamd_lua_check_udata_maybe (lua_State *L, gint pos, const gchar *classname)
  1362. {
  1363. return rspamd_lua_check_udata_common (L, pos, classname, FALSE);
  1364. }
  1365. struct rspamd_async_session*
  1366. lua_check_session (lua_State * L, gint pos)
  1367. {
  1368. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{session}");
  1369. luaL_argcheck (L, ud != NULL, pos, "'session' expected");
  1370. return ud ? *((struct rspamd_async_session **)ud) : NULL;
  1371. }
  1372. struct event_base*
  1373. lua_check_ev_base (lua_State * L, gint pos)
  1374. {
  1375. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{ev_base}");
  1376. luaL_argcheck (L, ud != NULL, pos, "'event_base' expected");
  1377. return ud ? *((struct event_base **)ud) : NULL;
  1378. }
  1379. gboolean
  1380. rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg,
  1381. struct event_base *ev_base, struct rspamd_worker *w)
  1382. {
  1383. struct rspamd_config_post_load_script *sc;
  1384. struct rspamd_config **pcfg;
  1385. struct event_base **pev_base;
  1386. struct rspamd_worker **pw;
  1387. gint err_idx;
  1388. GString *tb;
  1389. /* Execute post load scripts */
  1390. LL_FOREACH (cfg->on_load, sc) {
  1391. lua_pushcfunction (L, &rspamd_lua_traceback);
  1392. err_idx = lua_gettop (L);
  1393. lua_rawgeti (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
  1394. pcfg = lua_newuserdata (cfg->lua_state, sizeof (*pcfg));
  1395. *pcfg = cfg;
  1396. rspamd_lua_setclass (cfg->lua_state, "rspamd{config}", -1);
  1397. pev_base = lua_newuserdata (cfg->lua_state, sizeof (*pev_base));
  1398. *pev_base = ev_base;
  1399. rspamd_lua_setclass (cfg->lua_state, "rspamd{ev_base}", -1);
  1400. pw = lua_newuserdata (cfg->lua_state, sizeof (*pw));
  1401. *pw = w;
  1402. rspamd_lua_setclass (cfg->lua_state, "rspamd{worker}", -1);
  1403. if (lua_pcall (cfg->lua_state, 3, 0, err_idx) != 0) {
  1404. tb = lua_touserdata (L, -1);
  1405. msg_err_config ("error executing post load code: %v", tb);
  1406. g_string_free (tb, TRUE);
  1407. lua_settop (L, err_idx - 1);
  1408. return FALSE;
  1409. }
  1410. lua_settop (L, err_idx - 1);
  1411. }
  1412. return TRUE;
  1413. }
  1414. static struct rspamd_worker *
  1415. lua_check_worker (lua_State *L, gint pos)
  1416. {
  1417. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{worker}");
  1418. luaL_argcheck (L, ud != NULL, pos, "'worker' expected");
  1419. return ud ? *((struct rspamd_worker **)ud) : NULL;
  1420. }
  1421. static gint
  1422. lua_worker_get_stat (lua_State *L)
  1423. {
  1424. struct rspamd_worker *w = lua_check_worker (L, 1);
  1425. if (w) {
  1426. rspamd_mempool_stat_t mem_st;
  1427. struct rspamd_stat *stat, stat_copy;
  1428. ucl_object_t *top, *sub;
  1429. gint i;
  1430. guint64 spam = 0, ham = 0;
  1431. memset (&mem_st, 0, sizeof (mem_st));
  1432. rspamd_mempool_stat (&mem_st);
  1433. memcpy (&stat_copy, w->srv->stat, sizeof (stat_copy));
  1434. stat = &stat_copy;
  1435. top = ucl_object_typed_new (UCL_OBJECT);
  1436. ucl_object_insert_key (top, ucl_object_fromint (
  1437. stat->messages_scanned), "scanned", 0, false);
  1438. ucl_object_insert_key (top, ucl_object_fromint (
  1439. stat->messages_learned), "learned", 0, false);
  1440. if (stat->messages_scanned > 0) {
  1441. sub = ucl_object_typed_new (UCL_OBJECT);
  1442. for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
  1443. ucl_object_insert_key (sub,
  1444. ucl_object_fromint (stat->actions_stat[i]),
  1445. rspamd_action_to_str (i), 0, false);
  1446. if (i < METRIC_ACTION_GREYLIST) {
  1447. spam += stat->actions_stat[i];
  1448. }
  1449. else {
  1450. ham += stat->actions_stat[i];
  1451. }
  1452. }
  1453. ucl_object_insert_key (top, sub, "actions", 0, false);
  1454. }
  1455. else {
  1456. sub = ucl_object_typed_new (UCL_OBJECT);
  1457. for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
  1458. ucl_object_insert_key (sub,
  1459. 0,
  1460. rspamd_action_to_str (i), 0, false);
  1461. }
  1462. ucl_object_insert_key (top, sub, "actions", 0, false);
  1463. }
  1464. ucl_object_insert_key (top, ucl_object_fromint (
  1465. spam), "spam_count", 0, false);
  1466. ucl_object_insert_key (top, ucl_object_fromint (
  1467. ham), "ham_count", 0, false);
  1468. ucl_object_insert_key (top,
  1469. ucl_object_fromint (stat->connections_count), "connections", 0, false);
  1470. ucl_object_insert_key (top,
  1471. ucl_object_fromint (stat->control_connections_count),
  1472. "control_connections", 0, false);
  1473. ucl_object_insert_key (top,
  1474. ucl_object_fromint (mem_st.pools_allocated), "pools_allocated", 0,
  1475. false);
  1476. ucl_object_insert_key (top,
  1477. ucl_object_fromint (mem_st.pools_freed), "pools_freed", 0, false);
  1478. ucl_object_insert_key (top,
  1479. ucl_object_fromint (mem_st.bytes_allocated), "bytes_allocated", 0,
  1480. false);
  1481. ucl_object_insert_key (top,
  1482. ucl_object_fromint (
  1483. mem_st.chunks_allocated), "chunks_allocated", 0, false);
  1484. ucl_object_insert_key (top,
  1485. ucl_object_fromint (mem_st.shared_chunks_allocated),
  1486. "shared_chunks_allocated", 0, false);
  1487. ucl_object_insert_key (top,
  1488. ucl_object_fromint (mem_st.chunks_freed), "chunks_freed", 0, false);
  1489. ucl_object_insert_key (top,
  1490. ucl_object_fromint (
  1491. mem_st.oversized_chunks), "chunks_oversized", 0, false);
  1492. ucl_object_push_lua (L, top, true);
  1493. ucl_object_unref (top);
  1494. }
  1495. else {
  1496. return luaL_error (L, "invalid arguments");
  1497. }
  1498. return 1;
  1499. }
  1500. static gint
  1501. lua_worker_get_name (lua_State *L)
  1502. {
  1503. struct rspamd_worker *w = lua_check_worker (L, 1);
  1504. if (w) {
  1505. lua_pushstring (L, g_quark_to_string (w->type));
  1506. }
  1507. else {
  1508. return luaL_error (L, "invalid arguments");
  1509. }
  1510. return 1;
  1511. }
  1512. static gint
  1513. lua_worker_get_index (lua_State *L)
  1514. {
  1515. struct rspamd_worker *w = lua_check_worker (L, 1);
  1516. if (w) {
  1517. lua_pushnumber (L, w->index);
  1518. }
  1519. else {
  1520. return luaL_error (L, "invalid arguments");
  1521. }
  1522. return 1;
  1523. }
  1524. static gint
  1525. lua_worker_get_pid (lua_State *L)
  1526. {
  1527. struct rspamd_worker *w = lua_check_worker (L, 1);
  1528. if (w) {
  1529. lua_pushnumber (L, w->pid);
  1530. }
  1531. else {
  1532. return luaL_error (L, "invalid arguments");
  1533. }
  1534. return 1;
  1535. }
  1536. static gint
  1537. lua_worker_is_scanner (lua_State *L)
  1538. {
  1539. struct rspamd_worker *w = lua_check_worker (L, 1);
  1540. if (w) {
  1541. lua_pushboolean (L, rspamd_worker_is_scanner (w));
  1542. }
  1543. else {
  1544. return luaL_error (L, "invalid arguments");
  1545. }
  1546. return 1;
  1547. }
  1548. static gint
  1549. lua_worker_is_primary_controller (lua_State *L)
  1550. {
  1551. struct rspamd_worker *w = lua_check_worker (L, 1);
  1552. if (w) {
  1553. lua_pushboolean (L, rspamd_worker_is_primary_controller (w));
  1554. }
  1555. else {
  1556. return luaL_error (L, "invalid arguments");
  1557. }
  1558. return 1;
  1559. }
  1560. struct rspamd_lua_process_cbdata {
  1561. gint sp[2];
  1562. gint func_cbref;
  1563. gint cb_cbref;
  1564. gboolean replied;
  1565. gboolean is_error;
  1566. pid_t cpid;
  1567. lua_State *L;
  1568. guint64 sz;
  1569. GString *io_buf;
  1570. GString *out_buf;
  1571. goffset out_pos;
  1572. struct rspamd_worker *wrk;
  1573. struct event_base *ev_base;
  1574. struct event ev;
  1575. };
  1576. static void
  1577. rspamd_lua_execute_lua_subprocess (lua_State *L,
  1578. struct rspamd_lua_process_cbdata *cbdata)
  1579. {
  1580. gint err_idx, r;
  1581. GString *tb;
  1582. guint64 wlen = 0;
  1583. const gchar *ret;
  1584. gsize retlen;
  1585. lua_pushcfunction (L, &rspamd_lua_traceback);
  1586. err_idx = lua_gettop (L);
  1587. lua_rawgeti (L, LUA_REGISTRYINDEX, cbdata->func_cbref);
  1588. if (lua_pcall (L, 0, 1, err_idx) != 0) {
  1589. tb = lua_touserdata (L, -1);
  1590. msg_err ("call to subprocess failed: %v", tb);
  1591. /* Indicate error */
  1592. wlen = (1ULL << 63) + tb->len;
  1593. g_string_free (tb, TRUE);
  1594. r = write (cbdata->sp[1], &wlen, sizeof (wlen));
  1595. if (r == -1) {
  1596. msg_err ("write failed: %s", strerror (errno));
  1597. }
  1598. r = write (cbdata->sp[1], tb->str, tb->len);
  1599. if (r == -1) {
  1600. msg_err ("write failed: %s", strerror (errno));
  1601. }
  1602. lua_pop (L, 1);
  1603. }
  1604. else {
  1605. ret = lua_tolstring (L, -1, &retlen);
  1606. wlen = retlen;
  1607. r = write (cbdata->sp[1], &wlen, sizeof (wlen));
  1608. if (r == -1) {
  1609. msg_err ("write failed: %s", strerror (errno));
  1610. }
  1611. r = write (cbdata->sp[1], ret, retlen);
  1612. if (r == -1) {
  1613. msg_err ("write failed: %s", strerror (errno));
  1614. }
  1615. }
  1616. lua_pop (L, 1); /* Error function */
  1617. }
  1618. static void
  1619. rspamd_lua_call_on_complete (lua_State *L,
  1620. struct rspamd_lua_process_cbdata *cbdata,
  1621. const gchar *err_msg,
  1622. const gchar *data, gsize datalen)
  1623. {
  1624. gint err_idx;
  1625. GString *tb;
  1626. lua_pushcfunction (L, &rspamd_lua_traceback);
  1627. err_idx = lua_gettop (L);
  1628. lua_rawgeti (L, LUA_REGISTRYINDEX, cbdata->cb_cbref);
  1629. if (err_msg) {
  1630. lua_pushstring (L, err_msg);
  1631. }
  1632. else {
  1633. lua_pushnil (L);
  1634. }
  1635. if (data) {
  1636. lua_pushlstring (L, data, datalen);
  1637. }
  1638. else {
  1639. lua_pushnil (L);
  1640. }
  1641. if (lua_pcall (L, 2, 0, err_idx) != 0) {
  1642. tb = lua_touserdata (L, -1);
  1643. msg_err ("call to subprocess callback script failed: %v", tb);
  1644. lua_pop (L, 1);
  1645. }
  1646. lua_pop (L, 1); /* Error function */
  1647. }
  1648. static gboolean
  1649. rspamd_lua_cld_handler (struct rspamd_worker_signal_handler *sigh, void *ud)
  1650. {
  1651. struct rspamd_lua_process_cbdata *cbdata = ud;
  1652. struct rspamd_srv_command srv_cmd;
  1653. lua_State *L;
  1654. pid_t died;
  1655. gint res = 0;
  1656. /* Are we called by a correct children ? */
  1657. died = waitpid (cbdata->cpid, &res, WNOHANG);
  1658. if (died <= 0) {
  1659. /* Wait more */
  1660. return TRUE;
  1661. }
  1662. L = cbdata->L;
  1663. msg_info ("handled SIGCHLD from %p", cbdata->cpid);
  1664. if (!cbdata->replied) {
  1665. /* We still need to call on_complete callback */
  1666. rspamd_lua_call_on_complete (cbdata->L, cbdata,
  1667. "Worker has died without reply", NULL, 0);
  1668. event_del (&cbdata->ev);
  1669. }
  1670. /* Free structures */
  1671. close (cbdata->sp[0]);
  1672. luaL_unref (L, LUA_REGISTRYINDEX, cbdata->func_cbref);
  1673. luaL_unref (L, LUA_REGISTRYINDEX, cbdata->cb_cbref);
  1674. g_string_free (cbdata->io_buf, TRUE);
  1675. if (cbdata->out_buf) {
  1676. g_string_free (cbdata->out_buf, TRUE);
  1677. }
  1678. /* Notify main */
  1679. memset (&srv_cmd, 0, sizeof (srv_cmd));
  1680. srv_cmd.type = RSPAMD_SRV_ON_FORK;
  1681. srv_cmd.cmd.on_fork.state = child_dead;
  1682. srv_cmd.cmd.on_fork.cpid = cbdata->cpid;
  1683. srv_cmd.cmd.on_fork.ppid = getpid ();
  1684. rspamd_srv_send_command (cbdata->wrk, cbdata->ev_base, &srv_cmd, -1,
  1685. NULL, NULL);
  1686. g_free (cbdata);
  1687. /* We are done with this SIGCHLD */
  1688. return FALSE;
  1689. }
  1690. static void
  1691. rspamd_lua_subprocess_io (gint fd, short what, gpointer ud)
  1692. {
  1693. struct rspamd_lua_process_cbdata *cbdata = ud;
  1694. gssize r;
  1695. if (cbdata->sz == (guint64)-1) {
  1696. guint64 sz;
  1697. /* We read size of reply + flags first */
  1698. r = read (cbdata->sp[0], cbdata->io_buf->str + cbdata->io_buf->len,
  1699. sizeof (guint64) - cbdata->io_buf->len);
  1700. if (r == 0) {
  1701. rspamd_lua_call_on_complete (cbdata->L, cbdata,
  1702. "Unexpected EOF", NULL, 0);
  1703. event_del (&cbdata->ev);
  1704. cbdata->replied = TRUE;
  1705. kill (cbdata->cpid, SIGTERM);
  1706. return;
  1707. }
  1708. else if (r == -1) {
  1709. if (errno == EAGAIN || errno == EINTR) {
  1710. return;
  1711. }
  1712. else {
  1713. rspamd_lua_call_on_complete (cbdata->L, cbdata,
  1714. strerror (errno), NULL, 0);
  1715. event_del (&cbdata->ev);
  1716. cbdata->replied = TRUE;
  1717. kill (cbdata->cpid, SIGTERM);
  1718. return;
  1719. }
  1720. }
  1721. cbdata->io_buf->len += r;
  1722. if (cbdata->io_buf->len == sizeof (guint64)) {
  1723. memcpy ((guchar *)&sz, cbdata->io_buf->str, sizeof (sz));
  1724. if (sz & (1ULL << 63)) {
  1725. cbdata->is_error = TRUE;
  1726. sz &= ~(1ULL << 63);
  1727. }
  1728. cbdata->io_buf->len = 0;
  1729. cbdata->sz = sz;
  1730. g_string_set_size (cbdata->io_buf, sz + 1);
  1731. cbdata->io_buf->len = 0;
  1732. }
  1733. }
  1734. else {
  1735. /* Read data */
  1736. r = read (cbdata->sp[0], cbdata->io_buf->str + cbdata->io_buf->len,
  1737. cbdata->sz - cbdata->io_buf->len);
  1738. if (r == 0) {
  1739. rspamd_lua_call_on_complete (cbdata->L, cbdata,
  1740. "Unexpected EOF", NULL, 0);
  1741. event_del (&cbdata->ev);
  1742. cbdata->replied = TRUE;
  1743. kill (cbdata->cpid, SIGTERM);
  1744. return;
  1745. }
  1746. else if (r == -1) {
  1747. if (errno == EAGAIN || errno == EINTR) {
  1748. return;
  1749. }
  1750. else {
  1751. rspamd_lua_call_on_complete (cbdata->L, cbdata,
  1752. strerror (errno), NULL, 0);
  1753. event_del (&cbdata->ev);
  1754. cbdata->replied = TRUE;
  1755. kill (cbdata->cpid, SIGTERM);
  1756. return;
  1757. }
  1758. }
  1759. cbdata->io_buf->len += r;
  1760. if (cbdata->io_buf->len == cbdata->sz) {
  1761. gchar rep[4];
  1762. /* Finished reading data */
  1763. if (cbdata->is_error) {
  1764. cbdata->io_buf->str[cbdata->io_buf->len] = '\0';
  1765. rspamd_lua_call_on_complete (cbdata->L, cbdata,
  1766. cbdata->io_buf->str, NULL, 0);
  1767. }
  1768. else {
  1769. rspamd_lua_call_on_complete (cbdata->L, cbdata,
  1770. NULL, cbdata->io_buf->str, cbdata->io_buf->len);
  1771. }
  1772. event_del (&cbdata->ev);
  1773. cbdata->replied = TRUE;
  1774. /* Write reply to the child */
  1775. rspamd_socket_blocking (cbdata->sp[0]);
  1776. memset (rep, 0, sizeof (rep));
  1777. (void)write (cbdata->sp[0], rep, sizeof (rep));
  1778. }
  1779. }
  1780. }
  1781. static gint
  1782. lua_worker_spawn_process (lua_State *L)
  1783. {
  1784. struct rspamd_worker *w = lua_check_worker (L, 1);
  1785. struct rspamd_lua_process_cbdata *cbdata;
  1786. struct rspamd_abstract_worker_ctx *actx;
  1787. struct rspamd_srv_command srv_cmd;
  1788. const gchar *cmdline = NULL, *input = NULL;
  1789. gsize inputlen = 0;
  1790. pid_t pid;
  1791. GError *err = NULL;
  1792. gint func_cbref, cb_cbref;
  1793. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  1794. "func=F;exec=S;stdin=V;*on_complete=F", &func_cbref,
  1795. &cmdline, &inputlen, &input, &cb_cbref)) {
  1796. msg_err ("cannot get parameters list: %e", err);
  1797. if (err) {
  1798. g_error_free (err);
  1799. }
  1800. return 0;
  1801. }
  1802. cbdata = g_malloc0 (sizeof (*cbdata));
  1803. cbdata->cb_cbref = cb_cbref;
  1804. cbdata->func_cbref = func_cbref;
  1805. if (input) {
  1806. cbdata->out_buf = g_string_new_len (input, inputlen);
  1807. cbdata->out_pos = 0;
  1808. }
  1809. if (rspamd_socketpair (cbdata->sp, TRUE) == -1) {
  1810. msg_err ("cannot spawn socketpair: %s", strerror (errno));
  1811. g_free (cbdata);
  1812. luaL_unref (L, LUA_REGISTRYINDEX, cbdata->func_cbref);
  1813. luaL_unref (L, LUA_REGISTRYINDEX, cbdata->cb_cbref);
  1814. return 0;
  1815. }
  1816. actx = w->ctx;
  1817. cbdata->wrk = w;
  1818. cbdata->L = L;
  1819. cbdata->ev_base = actx->ev_base;
  1820. cbdata->sz = (guint64)-1;
  1821. pid = fork ();
  1822. if (pid == -1) {
  1823. msg_err ("cannot spawn process: %s", strerror (errno));
  1824. close (cbdata->sp[0]);
  1825. close (cbdata->sp[1]);
  1826. luaL_unref (L, LUA_REGISTRYINDEX, cbdata->func_cbref);
  1827. luaL_unref (L, LUA_REGISTRYINDEX, cbdata->cb_cbref);
  1828. g_free (cbdata);
  1829. return 0;
  1830. }
  1831. else if (pid == 0) {
  1832. /* Child */
  1833. gint rc;
  1834. gchar inbuf[4];
  1835. rspamd_log_update_pid (w->cf->type, w->srv->logger);
  1836. rc = ottery_init (w->srv->cfg->libs_ctx->ottery_cfg);
  1837. if (rc != OTTERY_ERR_NONE) {
  1838. msg_err ("cannot initialize PRNG: %d", rc);
  1839. abort ();
  1840. }
  1841. rspamd_random_seed_fast ();
  1842. #ifdef HAVE_EVUTIL_RNG_INIT
  1843. evutil_secure_rng_init ();
  1844. #endif
  1845. close (cbdata->sp[0]);
  1846. /* Here we assume that we can block on writing results */
  1847. rspamd_socket_blocking (cbdata->sp[1]);
  1848. event_reinit (cbdata->ev_base);
  1849. g_hash_table_remove_all (w->signal_events);
  1850. rspamd_worker_unblock_signals ();
  1851. rspamd_lua_execute_lua_subprocess (L, cbdata);
  1852. /* Wait for parent to reply and exit */
  1853. rc = read (cbdata->sp[1], inbuf, sizeof (inbuf));
  1854. if (memcmp (inbuf, "\0\0\0\0", 4) == 0) {
  1855. exit (EXIT_SUCCESS);
  1856. }
  1857. else {
  1858. msg_err ("got invalid reply from parent");
  1859. exit (EXIT_FAILURE);
  1860. }
  1861. }
  1862. cbdata->cpid = pid;
  1863. cbdata->io_buf = g_string_sized_new (8);
  1864. /* Notify main */
  1865. memset (&srv_cmd, 0, sizeof (srv_cmd));
  1866. srv_cmd.type = RSPAMD_SRV_ON_FORK;
  1867. srv_cmd.cmd.on_fork.state = child_create;
  1868. srv_cmd.cmd.on_fork.cpid = pid;
  1869. srv_cmd.cmd.on_fork.ppid = getpid ();
  1870. rspamd_srv_send_command (w, cbdata->ev_base, &srv_cmd, -1, NULL, NULL);
  1871. close (cbdata->sp[1]);
  1872. rspamd_socket_nonblocking (cbdata->sp[0]);
  1873. /* Parent */
  1874. rspamd_worker_set_signal_handler (SIGCHLD, w, cbdata->ev_base,
  1875. rspamd_lua_cld_handler,
  1876. cbdata);
  1877. /* Add result pipe waiting */
  1878. event_set (&cbdata->ev, cbdata->sp[0], EV_READ | EV_PERSIST,
  1879. rspamd_lua_subprocess_io, cbdata);
  1880. event_base_set (cbdata->ev_base, &cbdata->ev);
  1881. /* TODO: maybe add timeout? */
  1882. event_add (&cbdata->ev, NULL);
  1883. return 0;
  1884. }
  1885. struct rspamd_lua_ref_cbdata {
  1886. lua_State *L;
  1887. gint cbref;
  1888. };
  1889. static void
  1890. rspamd_lua_ref_dtor (gpointer p)
  1891. {
  1892. struct rspamd_lua_ref_cbdata *cbdata = p;
  1893. luaL_unref (cbdata->L, LUA_REGISTRYINDEX, cbdata->cbref);
  1894. }
  1895. void
  1896. rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool,
  1897. gint ref)
  1898. {
  1899. struct rspamd_lua_ref_cbdata *cbdata;
  1900. if (ref != -1) {
  1901. cbdata = rspamd_mempool_alloc (pool, sizeof (*cbdata));
  1902. cbdata->cbref = ref;
  1903. cbdata->L = L;
  1904. rspamd_mempool_add_destructor (pool, rspamd_lua_ref_dtor, cbdata);
  1905. }
  1906. }
  1907. gboolean
  1908. rspamd_lua_require_function (lua_State *L, const gchar *modname,
  1909. const gchar *funcname)
  1910. {
  1911. gint table_pos;
  1912. lua_getglobal (L, "require");
  1913. if (lua_isnil (L, -1)) {
  1914. lua_pop (L, 1);
  1915. return FALSE;
  1916. }
  1917. lua_pushstring (L, modname);
  1918. /* Now try to call */
  1919. if (lua_pcall (L, 1, 1, 0) != 0) {
  1920. lua_pop (L, 1);
  1921. return FALSE;
  1922. }
  1923. /* Now we should have a table with results */
  1924. if (!lua_istable (L, -1)) {
  1925. lua_pop (L, 1);
  1926. return FALSE;
  1927. }
  1928. table_pos = lua_gettop (L);
  1929. lua_pushstring (L, funcname);
  1930. lua_gettable (L, -2);
  1931. if (lua_type (L, -1) == LUA_TFUNCTION) {
  1932. /* Remove table, preserve just a function */
  1933. lua_remove (L, table_pos);
  1934. return TRUE;
  1935. }
  1936. lua_pop (L, 2);
  1937. return FALSE;
  1938. }