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_config.c 102KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181
  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 "libmime/message.h"
  18. #include "libutil/expression.h"
  19. #include "libserver/composites.h"
  20. #include "libserver/cfg_file_private.h"
  21. #include "libmime/lang_detection.h"
  22. #include "lua/lua_map.h"
  23. #include "lua/lua_thread_pool.h"
  24. #include "utlist.h"
  25. #include <math.h>
  26. /***
  27. * This module is used to configure rspamd and is normally available as global
  28. * variable named `rspamd_config`. Unlike other modules, it is not necessary to
  29. * require it before usage.
  30. * @module rspamd_config
  31. * @example
  32. -- Register some callback symbol
  33. local function foo(task)
  34. -- do something
  35. end
  36. rspamd_config:register_symbol('SYMBOL', 1.0, foo)
  37. -- Get configuration
  38. local tab = rspamd_config:get_all_opt('module') -- get table for module's options
  39. local opts = rspamd_config:get_key('options') -- get content of the specified key in rspamd configuration
  40. */
  41. /* Config file methods */
  42. /***
  43. * @method rspamd_config:get_module_opt(mname, optname)
  44. * Returns value of specified option `optname` for a module `mname`,
  45. * @param {string} mname name of module
  46. * @param {string} optname option to get
  47. * @return {string or table} value of the option or `nil` if option is not found
  48. */
  49. LUA_FUNCTION_DEF (config, get_module_opt);
  50. /***
  51. * @method rspamd_config:get_all_opt(mname)
  52. * Returns value of all options for a module `mname`, flattening values into a single table consisting
  53. * of all sections with such a name.
  54. * @param {string} mname name of module
  55. * @return {table} table of all options for `mname` or `nil` if a module's configuration is not found
  56. */
  57. LUA_FUNCTION_DEF (config, get_all_opt);
  58. /***
  59. * @method rspamd_config:get_ucl()
  60. * Returns full configuration as a native Lua object (ucl to lua conversion).
  61. * This method uses caching if possible.
  62. * @return {table} table of all options in the configuration
  63. */
  64. LUA_FUNCTION_DEF (config, get_ucl);
  65. /***
  66. * @method rspamd_config:get_mempool()
  67. * Returns static configuration memory pool.
  68. * @return {mempool} [memory pool](mempool.md) object
  69. */
  70. LUA_FUNCTION_DEF (config, get_mempool);
  71. /***
  72. * @method rspamd_config:get_resolver()
  73. * Returns DNS resolver.
  74. * @return {dns_resolver} opaque DNS resolver pointer if any
  75. */
  76. LUA_FUNCTION_DEF (config, get_resolver);
  77. /***
  78. * @method rspamd_config:add_radix_map(mapline[, description])
  79. * Creates new dynamic map of IP/mask addresses.
  80. * @param {string} mapline URL for a map
  81. * @param {string} description optional map description
  82. * @return {map} radix tree object
  83. * @example
  84. local ip_map = rspamd_config:add_radix_map ('file:///path/to/file', 'my radix map')
  85. ...
  86. local function foo(task)
  87. local ip = task:get_from_ip()
  88. if ip_map:get_key(ip) then
  89. return true
  90. end
  91. return false
  92. end
  93. */
  94. /***
  95. * @method rspamd_config:radix_from_config(mname, optname)
  96. * Creates new embedded map of IP/mask addresses from config.
  97. * @param {string} mname name of module
  98. * @param {string} optname option to get
  99. * @return {map} radix tree object
  100. * @example
  101. local ip_map = rspamd_config:radix_from_config ('mymodule', 'ips')
  102. ...
  103. local function foo(task)
  104. local ip = task:get_from_ip()
  105. if ip_map:get_key(ip) then
  106. return true
  107. end
  108. return false
  109. end
  110. */
  111. /***
  112. * @method rspamd_config:radix_from_ucl(obj)
  113. * Creates new embedded map of IP/mask addresses from object.
  114. * @param {ucl} obj object
  115. * @return {map} radix tree object
  116. */
  117. /***
  118. * @method rspamd_config:add_hash_map(mapline[, description])
  119. * Creates new dynamic map string objects.
  120. * @param {string} mapline URL for a map
  121. * @param {string} description optional map description
  122. * @return {map} hash set object
  123. * @example
  124. local hash_map = rspamd_config:add_hash_map ('file:///path/to/file', 'my hash map')
  125. ...
  126. local function foo(task)
  127. local from = task:get_from()
  128. if hash_map:get_key(from['user']) then
  129. return true
  130. end
  131. return false
  132. end
  133. */
  134. /***
  135. * @method rspamd_config:add_kv_map(mapline[, description])
  136. * Creates new dynamic map of key/values associations.
  137. * @param {string} mapline URL for a map
  138. * @param {string} description optional map description
  139. * @return {map} hash table object
  140. * @example
  141. local kv_map = rspamd_config:add_kv_map ('file:///path/to/file', 'my kv map')
  142. ...
  143. local function foo(task)
  144. local from = task:get_from()
  145. if from then
  146. local value = kv_map:get_key(from['user'])
  147. if value then
  148. return true,value
  149. end
  150. end
  151. return false
  152. end
  153. */
  154. /***
  155. * @method rspamd_config:add_map({args})
  156. * Creates new dynamic map according to the attributes passed.
  157. *
  158. * - `type`: type of map to be created, can be one of the following set:
  159. * + `set`: set of strings
  160. * + `radix`: map of IP addresses to strings
  161. * + `map`: map of strings to strings
  162. * + `regexp`: map of regexps to strings
  163. * + `callback`: map processed by lua callback
  164. * - `url`: url to load map from
  165. * - `description`: map's description
  166. * - `callback`: lua callback for the map
  167. *
  168. * @return {map} `true` if map has been added
  169. * @example
  170. local str = ''
  171. local function process_map(in)
  172. str = in
  173. end
  174. rspamd_config:add_map('http://example.com/map', "settings map", process_map)
  175. */
  176. /***
  177. * @method rspamd_config:get_maps()
  178. * Get all maps defined as an array of rspamd{map} objects
  179. *
  180. * @return {table|rspamd{map}}
  181. */
  182. /***
  183. * @method rspamd_config:get_classifier(name)
  184. * Returns classifier config.
  185. * @param {string} name name of classifier (e.g. `bayes`)
  186. * @return {classifier} classifier object or `nil`
  187. */
  188. LUA_FUNCTION_DEF (config, get_classifier);
  189. /***
  190. * @method rspamd_config:register_symbol(table)
  191. * Register symbol of a specified type in rspamd. This function accepts table of arguments:
  192. *
  193. * - `name`: name of symbol (can be missing for callback symbols)
  194. * - `callback`: function to be called for symbol's check (can be absent for virtual symbols)
  195. * - `weight`: weight of symbol (should normally be 1 or missing)
  196. * - `priority`: priority of symbol (normally 0 or missing)
  197. * - `type`: type of symbol: `normal` (default), `virtual` or `callback`
  198. * - `flags`: various flags split by commas or spaces:
  199. * + `nice` if symbol can produce negative score;
  200. * + `empty` if symbol can be called for empty messages
  201. * + `skip` if symbol should be skipped now
  202. * + `nostat` if symbol should be excluded from stat tokens
  203. * + `trivial` symbol is trivial (e.g. no network requests)
  204. * + `explicit_disable` requires explicit disabling (e.g. via settings)
  205. * + `ignore_passthrough` executed even if passthrough result has been set
  206. * - `parent`: id of parent symbol (useful for virtual symbols)
  207. *
  208. * @return {number} id of symbol registered
  209. */
  210. LUA_FUNCTION_DEF (config, register_symbol);
  211. /***
  212. * @method rspamd_config:register_symbols(callback, [weight], callback_name, [, symbol, ...])
  213. * Register callback function to be called for a set of symbols with initial weight.
  214. * @param {function} callback callback function to be called for a specified symbol
  215. * @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols)
  216. * @param {string} callback_name symbolic name of callback
  217. * @param {list of strings} symbol list of symbols registered by this function
  218. */
  219. LUA_FUNCTION_DEF (config, register_symbols);
  220. /***
  221. * @method rspamd_config:register_virtual_symbol(name, weight,)
  222. * Register virtual symbol that is not associated with any callback.
  223. *
  224. * **This method is deprecated and should not be used in newly written code **
  225. * @param {string} virtual name symbol's name
  226. * @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols)
  227. */
  228. LUA_FUNCTION_DEF (config, register_virtual_symbol);
  229. /***
  230. * @method rspamd_config:register_callback_symbol(name, weight, callback)
  231. * Register callback function to be called for a specified symbol with initial weight. Symbol itself is
  232. * not registered in the metric and is not intended to be visible by a user.
  233. *
  234. * **This method is deprecated and should not be used in newly written code **
  235. * @param {string} name symbol's name (just for unique id purposes)
  236. * @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols)
  237. * @param {function} callback callback function to be called for a specified symbol
  238. */
  239. LUA_FUNCTION_DEF (config, register_callback_symbol);
  240. LUA_FUNCTION_DEF (config, register_callback_symbol_priority);
  241. /***
  242. * @method rspamd_config:register_dependency(id, dep)
  243. * Create a dependency between symbol identified by `id` and a symbol identified
  244. * by some symbolic name `dep`
  245. * @param {number|string} id id or name of source (numeric id is returned by all register_*_symbol)
  246. * @param {string} dep dependency name
  247. * @example
  248. local function cb(task)
  249. ...
  250. end
  251. local id = rspamd_config:register_symbol('SYM', 1.0, cb)
  252. rspamd_config:register_dependency(id, 'OTHER_SYM')
  253. -- Alternative form
  254. rspamd_config:register_dependency('SYMBOL_FROM', 'SYMBOL_TO')
  255. */
  256. LUA_FUNCTION_DEF (config, register_dependency);
  257. /***
  258. * @method rspamd_config:get_symbol_flags(name)
  259. * Returns symbol flags
  260. * @param {string} name symbols's name
  261. * @return {table|string} list of flags for symbol or nil
  262. */
  263. LUA_FUNCTION_DEF (config, get_symbol_flags);
  264. /***
  265. * @method rspamd_config:add_symbol_flags(name, flags)
  266. * Adds flags to a symbol
  267. * @param {string} name symbols's name
  268. * @param {table|string} flags flags to add
  269. * @return {table|string} new set of flags
  270. */
  271. LUA_FUNCTION_DEF (config, add_symbol_flags);
  272. /**
  273. * @method rspamd_config:register_re_selector(name, selector_str)
  274. * Registers selector with the specific name to use in regular expressions in form
  275. * name=/re/$ or name=/re/{selector}
  276. * @param {string} name name of the selector
  277. * @param {selector_str} selector string
  278. * @return true if selector has been registered
  279. */
  280. LUA_FUNCTION_DEF (config, register_re_selector);
  281. /**
  282. * @method rspamd_config:set_symbol({table})
  283. * Sets the value of a specified symbol in a metric. This function accepts table with the following elements:
  284. *
  285. * - `name`: name of symbol (string)
  286. * - `score`: score for symbol (number)
  287. * - `metric`: name of metric (string, optional)
  288. * - `description`: description of symbol (string, optional)
  289. * - `group`: name of group for symbol (string, optional)
  290. * - `one_shot`: turn off multiple hits for a symbol (boolean, optional)
  291. * - `one_param`: turn off multiple options for a symbol (boolean, optional)
  292. * - `flags`: comma separated string of flags:
  293. * + `ignore`: do not strictly check validity of symbol and corresponding rule
  294. * + `one_shot`: turn off multiple hits for a symbol
  295. * + `one_param`: allow only one parameter for a symbol
  296. * - `priority`: priority of symbol's definition
  297. */
  298. LUA_FUNCTION_DEF (config, set_metric_symbol);
  299. /**
  300. * @method rspamd_config:set_action({table})
  301. * Sets the score of a specified action in a metric. This function accepts table with the following elements:
  302. *
  303. * - `action`: name of action (string)
  304. * - `score`: score for action (number)
  305. * - `metric`: name of metric (string, optional)
  306. * - `priority`: priority of action's definition
  307. */
  308. LUA_FUNCTION_DEF (config, set_metric_action);
  309. /**
  310. * @method rspamd_config:get_symbol(name)
  311. * Gets metric data for a specific symbol identified by `name`:
  312. *
  313. * - `score`: score for symbol (number)
  314. * - `description`: description of symbol (string, optional)
  315. * - `group`: name of group for symbol (string, optional)
  316. * - `one_shot`: turn off multiple hits for a symbol (boolean, optional)
  317. * - `flags`: comma separated string of flags:
  318. * + `ignore`: do not strictly check validity of symbol and corresponding rule
  319. * + `one_shot`: turn off multiple hits for a symbol
  320. *
  321. * @param {string} name name of symbol
  322. * @return {table} symbol's definition or nil in case of undefined symbol
  323. */
  324. LUA_FUNCTION_DEF (config, get_metric_symbol);
  325. /**
  326. * @method rspamd_config:get_action(name)
  327. * Gets data for a specific action in config. This function returns number reperesenting action's score
  328. *
  329. * @param {string} name name of action
  330. * @return {number} action's score or nil in case of undefined score or action
  331. */
  332. LUA_FUNCTION_DEF (config, get_metric_action);
  333. /**
  334. * @method rspamd_config:get_all_actions()
  335. * Gets data for all action in config
  336. * @return {table|str->num} action's score or nil in case of undefined score or action
  337. */
  338. LUA_FUNCTION_DEF (config, get_all_actions);
  339. /**
  340. * @method rspamd_config:add_composite(name, expression)
  341. * @param {string} name name of composite symbol
  342. * @param {string} expression symbolic expression of the composite rule
  343. * @return {bool} true if a composite has been added successfully
  344. */
  345. LUA_FUNCTION_DEF (config, add_composite);
  346. /***
  347. * @method rspamd_config:register_pre_filter(callback[, order])
  348. * Register function to be called prior to symbols processing.
  349. * @param {function} callback callback function
  350. * @param {number} order filters are called from lower orders to higher orders, order is equal to 0 by default
  351. * @example
  352. local function check_function(task)
  353. -- It is possible to manipulate the task object here: set settings, set pre-action and so on
  354. ...
  355. end
  356. rspamd_config:register_pre_filter(check_function)
  357. */
  358. LUA_FUNCTION_DEF (config, register_pre_filter);
  359. /***
  360. * @method rspamd_config:register_post_filter(callback[, order])
  361. * Register function to be called after symbols are processed.
  362. *
  363. * @param {function} callback callback function
  364. * @param {number} order filters are called from lower orders to higher orders, order is equal to 0 by default
  365. */
  366. LUA_FUNCTION_DEF (config, register_post_filter);
  367. /* XXX: obsoleted */
  368. LUA_FUNCTION_DEF (config, register_module_option);
  369. /* XXX: not needed now */
  370. LUA_FUNCTION_DEF (config, get_api_version);
  371. /***
  372. * @method rspamd_config:get_key(name)
  373. * Returns configuration section with the specified `name`.
  374. * @param {string} name name of config section
  375. * @return {variant} specific value of section
  376. * @example
  377. local set_section = rspamd_config:get_key("settings")
  378. if type(set_section) == "string" then
  379. -- Just a map of ucl
  380. if rspamd_config:add_map(set_section, "settings map", process_settings_map) then
  381. rspamd_config:register_pre_filter(check_settings)
  382. end
  383. elseif type(set_section) == "table" then
  384. if process_settings_table(set_section) then
  385. rspamd_config:register_pre_filter(check_settings)
  386. end
  387. end
  388. */
  389. LUA_FUNCTION_DEF (config, get_key);
  390. /***
  391. * @method rspamd_config:add_condition(symbol, condition)
  392. * Adds condition callback for specified symbol
  393. * @param {string} symbol symbol's name
  394. * @param {function} condition condition callback
  395. * @return {boolean} true if condition has been added
  396. * @example
  397. rspamd_config:add_condition('FUZZY_DENIED', function(task)
  398. if some_map:find_key(task:get_from()) then return false end
  399. return true
  400. end)
  401. */
  402. LUA_FUNCTION_DEF (config, add_condition);
  403. /***
  404. * @method rspamd_config:enable_symbol(symbol)
  405. * Enables execution for the specified symbol
  406. * @param {string} symbol symbol's name
  407. */
  408. LUA_FUNCTION_DEF (config, enable_symbol);
  409. /***
  410. * @method rspamd_config:disable_symbol(symbol)
  411. * Disables execution for the specified symbol
  412. * @param {string} symbol symbol's name
  413. */
  414. LUA_FUNCTION_DEF (config, disable_symbol);
  415. /***
  416. * @method rspamd_config:get_symbol_parent(symbol)
  417. * Returns a parent symbol for specific symbol (or symbol itself if top level)
  418. * @param {string} symbol symbol's name
  419. */
  420. LUA_FUNCTION_DEF (config, get_symbol_parent);
  421. /***
  422. * @method rspamd_config:__newindex(name, callback)
  423. * This metamethod is called if new indicies are added to the `rspamd_config` object.
  424. * Technically, it is the equivalent of @see rspamd_config:register_symbol where `weight` is 1.0.
  425. * There is also table form invocation that allows to control more things:
  426. *
  427. * - `callback`: has the same meaning and acts as function of task
  428. * - `score`: default score for a symbol
  429. * - `group`: default group for a symbol
  430. * - `description`: default symbol's description
  431. * - `priority`: additional priority value
  432. * - `one_shot`: default value for one shot attribute
  433. * - `condition`: function of task that can enable or disable this specific rule's execution
  434. * @param {string} name index name
  435. * @param {function/table} callback callback to be called
  436. * @return {number} id of the new symbol added
  437. * @example
  438. rspamd_config.R_EMPTY_IMAGE = function (task)
  439. parts = task:get_text_parts()
  440. if parts then
  441. for _,part in ipairs(parts) do
  442. if part:is_empty() then
  443. images = task:get_images()
  444. if images then
  445. -- Symbol `R_EMPTY_IMAGE` is inserted
  446. return true
  447. end
  448. return false
  449. end
  450. end
  451. end
  452. return false
  453. end
  454. rspamd_config.SYMBOL = {
  455. callback = function(task)
  456. ...
  457. end,
  458. score = 5.1,
  459. description = 'sample symbol',
  460. group = 'sample symbols',
  461. condition = function(task)
  462. if task:get_from()[1]['addr'] == 'user@example.com' then
  463. return false
  464. end
  465. return true
  466. end
  467. }
  468. */
  469. LUA_FUNCTION_DEF (config, newindex);
  470. /***
  471. * @method rspamd_config:register_regexp(params)
  472. * Registers new re for further cached usage
  473. * Params is the table with the following fields (mandatory fields are marked with `*`):
  474. * - `re`* : regular expression object
  475. * - `type`*: type of regular expression:
  476. * + `mime`: mime regexp
  477. * + `rawmime`: raw mime regexp
  478. * + `header`: header regexp
  479. * + `rawheader`: raw header expression
  480. * + `body`: raw body regexp
  481. * + `url`: url regexp
  482. * - `header`: for header and rawheader regexp means the name of header
  483. * - `pcre_only`: flag regexp as pcre only regexp
  484. */
  485. LUA_FUNCTION_DEF (config, register_regexp);
  486. /***
  487. * @method rspamd_config:replace_regexp(params)
  488. * Replaces regexp with a new one
  489. * Params is the table with the following fields (mandatory fields are marked with `*`):
  490. * - `old_re`* : old regular expression object (must be in the cache)
  491. * - `new_re`* : old regular expression object (must not be in the cache)
  492. */
  493. LUA_FUNCTION_DEF (config, replace_regexp);
  494. /***
  495. * @method rspamd_config:register_worker_script(worker_type, script)
  496. * Registers the following script for workers of a specified type. The exact type
  497. * of script function depends on worker type
  498. * @param {string} worker_type worker type (e.g. "normal")
  499. * @param {function} script script for a worker
  500. * @return {boolean} `true` if a script has been registered
  501. */
  502. LUA_FUNCTION_DEF (config, register_worker_script);
  503. /***
  504. * @method rspamd_config:add_on_load(function(cfg, ev_base, worker) ... end)
  505. * Registers the following script to be executed when configuration is completely loaded
  506. * @param {function} script function to be executed
  507. * @example
  508. rspamd_config:add_on_load(function(cfg, ev_base, worker)
  509. rspamd_config:add_periodic(ev_base, 1.0, function(cfg, ev_base)
  510. local logger = require "rspamd_logger"
  511. logger.infox(cfg, "periodic function in worker %s", worker:get_name())
  512. return true
  513. end)
  514. end)
  515. */
  516. LUA_FUNCTION_DEF (config, add_on_load);
  517. /***
  518. * @method rspamd_config:add_periodic(event_base, timeout, function(cfg, ev_base) ... end, [jitter = false])
  519. * Registers function to be periodically executed by Rspamd
  520. * @param {ev_base} event_base event base that is needed for async events
  521. * @param {number} timeout time in seconds (could be fractional)
  522. * @param {function} script function to be executed
  523. * @param {boolean} jitter `true` if timeout jittering is needed
  524. * @example
  525. rspamd_config:add_on_load(function(cfg, ev_base)
  526. rspamd_config:add_periodic(ev_base, 1.0, function(cfg, ev_base)
  527. local logger = require "rspamd_logger"
  528. logger.infox(cfg, "periodic function")
  529. return true -- if return false, then the periodic event is removed
  530. end)
  531. end)
  532. */
  533. LUA_FUNCTION_DEF (config, add_periodic);
  534. /***
  535. * @method rspamd_config:get_symbols_count()
  536. * Returns number of symbols registered in rspamd configuration
  537. * @return {number} number of symbols registered in the configuration
  538. */
  539. LUA_FUNCTION_DEF (config, get_symbols_count);
  540. /***
  541. * @method rspamd_config:get_symbols_cksum()
  542. * Returns checksum for all symbols in the cache
  543. * @return {int64} boxed value of the 64 bit checksum
  544. */
  545. LUA_FUNCTION_DEF (config, get_symbols_cksum);
  546. /***
  547. * @method rspamd_config:get_symbols_counters()
  548. * Returns table of all counters in the cache (weights, frequencies etc)
  549. * @return {table|tables} all symbols indexed by name
  550. */
  551. LUA_FUNCTION_DEF (config, get_symbols_counters);
  552. /***
  553. * @method rspamd_config:get_symbols_scores()
  554. * Returns table of all scores defined in config
  555. * @return {table|tables} all symbols indexed by name
  556. */
  557. LUA_FUNCTION_DEF (config, get_symbols_scores);
  558. /***
  559. * @method rspamd_config:get_symbol_callback(name)
  560. * Returns callback function for the specified symbol if it is a lua registered callback
  561. * @return {function} callback function or nil
  562. */
  563. LUA_FUNCTION_DEF (config, get_symbol_callback);
  564. /***
  565. * @method rspamd_config:get_symbol_stat(name)
  566. * Returns table with statistics for a specific symbol:
  567. * - `frequency`: frequency for symbol's hits
  568. * - `stddev`: standard deviation of `frequency`
  569. * - `time`: average time in seconds (floating point)
  570. * - `count`: total number of hits
  571. * @return {table} symbol stats
  572. */
  573. LUA_FUNCTION_DEF (config, get_symbol_stat);
  574. /***
  575. * @method rspamd_config:set_symbol_callback(name, callback)
  576. * Sets callback for the specified symbol
  577. * @return {boolean} true if function has been replaced
  578. */
  579. LUA_FUNCTION_DEF (config, set_symbol_callback);
  580. /***
  581. * @method rspamd_config:register_finish_script(callback)
  582. * Adds new callback that is called on worker process termination when all
  583. * tasks pending are processed
  584. *
  585. * @param callback {function} a function with one argument (rspamd_task)
  586. */
  587. LUA_FUNCTION_DEF (config, register_finish_script);
  588. /***
  589. * @method rspamd_config:register_monitored(url, type, [{params}])
  590. * Registers monitored resource to watch its availability. Supported types:
  591. *
  592. * - `dns`: DNS monitored object
  593. *
  594. * Params are optional table specific for each type. For DNS it supports the
  595. * following options:
  596. *
  597. * - `prefix`: prefix to add before making request
  598. * - `type`: type of request (e.g. 'a' or 'txt')
  599. * - `ipnet`: array of ip/networks to expect on reply
  600. * - `rcode`: expected return code (e.g. `nxdomain`)
  601. *
  602. * Returned object has the following methods:
  603. *
  604. * - `alive`: returns `true` if monitored resource is alive
  605. * - `offline`: returns number of seconds of the current offline period (or 0 if alive)
  606. * - `total_offline`: returns number of seconds of the overall offline
  607. * - `latency`: returns the current average latency in seconds (or 0 if offline)
  608. *
  609. * @param {string} url resource to monitor
  610. * @param {string} type type of monitoring
  611. * @param {table} opts optional parameters
  612. * @return {rspamd_monitored} rspamd monitored object
  613. */
  614. LUA_FUNCTION_DEF (config, register_monitored);
  615. /***
  616. * @method rspamd_config:add_doc(path, option, doc_string, [{params}])
  617. * Adds new documentation string for an option `option` at path `path`
  618. * Options defines optional params, such as:
  619. *
  620. * - `default`: default option value
  621. * - `type`: type of an option (`string`, `number`, `object`, `array` etc)
  622. * - `reqired`: if an option is required
  623. *
  624. * @param {string} path documentation path (e.g. module name)
  625. * @param {string} option name of the option
  626. * @param {string} doc_string documentation string
  627. * @param {table} params optional parameters
  628. */
  629. LUA_FUNCTION_DEF (config, add_doc);
  630. /***
  631. * @method rspamd_config:add_example(path, option, doc_string, example)
  632. * Adds new documentation
  633. *
  634. * @param {string} path documentation path (e.g. module name or nil for top)
  635. * @param {string} option name of the option
  636. * @param {string} doc_string documentation string
  637. * @param {string} example example in ucl format, comments are also parsed
  638. */
  639. LUA_FUNCTION_DEF (config, add_example);
  640. /***
  641. * @method rspamd_config:set_peak_cb(function)
  642. * Sets a function that will be called when frequency of some symbol goes out of
  643. * stddev * 2 over the last period of refreshment.
  644. *
  645. * @example
  646. rspamd_config:set_peak_cb(function(ev_base, sym, mean, stddev, value, error)
  647. -- ev_base: event base for async events (e.g. redis)
  648. -- sym: symbol's name
  649. -- mean: mean frequency value
  650. -- stddev: standard deviation of frequency
  651. -- value: current frequency value
  652. -- error: squared error
  653. local logger = require "rspamd_logger"
  654. logger.infox(rspamd_config, "symbol %s has changed frequency significantly: %s(%s) over %s(%s)",
  655. sym, value, error, mean, stddev)
  656. end)
  657. */
  658. LUA_FUNCTION_DEF (config, set_peak_cb);
  659. /***
  660. * @method rspamd_config:get_cpu_flags()
  661. * Returns architecture dependent flags supported by the CPU
  662. * Currently, only x86 flags are supported:
  663. * - 'ssse3'
  664. * - 'sse42'
  665. * - 'avx'
  666. * - 'avx2'
  667. * @return {table} flag -> true table
  668. */
  669. LUA_FUNCTION_DEF (config, get_cpu_flags);
  670. /***
  671. * @method rspamd_config:has_torch()
  672. * Returns true if Rspamd is compiled with torch support and the runtime CPU
  673. * supports sse4.2 required for torch.
  674. * @return {boolean} true if torch is compiled and supported
  675. */
  676. LUA_FUNCTION_DEF (config, has_torch);
  677. /***
  678. * @method rspamd_config:experimental_enabled()
  679. * Returns true if experimental plugins are enabled
  680. * @return {boolean} true if experimental plugins are enabled
  681. */
  682. LUA_FUNCTION_DEF (config, experimental_enabled);
  683. /***
  684. * @method rspamd_config:load_ucl(filename[, include_trace])
  685. * Loads config from the UCL file (but does not perform parsing using rcl)
  686. * @param {string} filename file to load
  687. * @return true or false + error message
  688. */
  689. LUA_FUNCTION_DEF (config, load_ucl);
  690. /***
  691. * @method rspamd_config:parse_rcl([skip_sections])
  692. * Parses RCL using loaded ucl file
  693. * @param {table|string} sections to skip
  694. * @return true or false + error message
  695. */
  696. LUA_FUNCTION_DEF (config, parse_rcl);
  697. /***
  698. * @method rspamd_config:init_modules()
  699. * Initialize lua and internal modules
  700. * @return true or false
  701. */
  702. LUA_FUNCTION_DEF (config, init_modules);
  703. /***
  704. * @method rspamd_config:init_subsystem(str)
  705. * Initialize config subsystem from a comma separated list:
  706. * - `modules` - init modules
  707. * - `langdet` - language detector
  708. * - `dns` - DNS resolver
  709. * - TODO: add more
  710. */
  711. LUA_FUNCTION_DEF (config, init_subsystem);
  712. /***
  713. * @method rspamd_config:get_tld_path()
  714. * Returns path to TLD file
  715. * @return {string} path to tld file
  716. */
  717. LUA_FUNCTION_DEF (config, get_tld_path);
  718. static const struct luaL_reg configlib_m[] = {
  719. LUA_INTERFACE_DEF (config, get_module_opt),
  720. LUA_INTERFACE_DEF (config, get_mempool),
  721. LUA_INTERFACE_DEF (config, get_resolver),
  722. LUA_INTERFACE_DEF (config, get_all_opt),
  723. LUA_INTERFACE_DEF (config, get_ucl),
  724. LUA_INTERFACE_DEF (config, add_radix_map),
  725. LUA_INTERFACE_DEF (config, radix_from_config),
  726. LUA_INTERFACE_DEF (config, radix_from_ucl),
  727. LUA_INTERFACE_DEF (config, add_hash_map),
  728. LUA_INTERFACE_DEF (config, add_kv_map),
  729. LUA_INTERFACE_DEF (config, add_map),
  730. LUA_INTERFACE_DEF (config, get_maps),
  731. LUA_INTERFACE_DEF (config, get_classifier),
  732. LUA_INTERFACE_DEF (config, register_symbol),
  733. LUA_INTERFACE_DEF (config, register_symbols),
  734. LUA_INTERFACE_DEF (config, register_virtual_symbol),
  735. LUA_INTERFACE_DEF (config, register_callback_symbol),
  736. LUA_INTERFACE_DEF (config, register_callback_symbol_priority),
  737. LUA_INTERFACE_DEF (config, register_dependency),
  738. LUA_INTERFACE_DEF (config, get_symbol_flags),
  739. LUA_INTERFACE_DEF (config, add_symbol_flags),
  740. LUA_INTERFACE_DEF (config, set_metric_symbol),
  741. {"set_symbol", lua_config_set_metric_symbol},
  742. LUA_INTERFACE_DEF (config, set_metric_action),
  743. {"set_action", lua_config_set_metric_action},
  744. LUA_INTERFACE_DEF (config, get_metric_symbol),
  745. {"get_symbol", lua_config_get_metric_symbol},
  746. LUA_INTERFACE_DEF (config, get_metric_action),
  747. {"get_action", lua_config_get_metric_action},
  748. LUA_INTERFACE_DEF (config, get_all_actions),
  749. LUA_INTERFACE_DEF (config, add_composite),
  750. LUA_INTERFACE_DEF (config, register_module_option),
  751. LUA_INTERFACE_DEF (config, register_pre_filter),
  752. LUA_INTERFACE_DEF (config, register_post_filter),
  753. LUA_INTERFACE_DEF (config, get_api_version),
  754. LUA_INTERFACE_DEF (config, get_key),
  755. LUA_INTERFACE_DEF (config, add_condition),
  756. LUA_INTERFACE_DEF (config, enable_symbol),
  757. LUA_INTERFACE_DEF (config, disable_symbol),
  758. LUA_INTERFACE_DEF (config, register_regexp),
  759. LUA_INTERFACE_DEF (config, replace_regexp),
  760. LUA_INTERFACE_DEF (config, register_worker_script),
  761. LUA_INTERFACE_DEF (config, register_re_selector),
  762. LUA_INTERFACE_DEF (config, add_on_load),
  763. LUA_INTERFACE_DEF (config, add_periodic),
  764. LUA_INTERFACE_DEF (config, get_symbols_count),
  765. LUA_INTERFACE_DEF (config, get_symbols_cksum),
  766. LUA_INTERFACE_DEF (config, get_symbols_counters),
  767. LUA_INTERFACE_DEF (config, get_symbols_scores),
  768. LUA_INTERFACE_DEF (config, get_symbol_callback),
  769. LUA_INTERFACE_DEF (config, set_symbol_callback),
  770. LUA_INTERFACE_DEF (config, get_symbol_stat),
  771. LUA_INTERFACE_DEF (config, get_symbol_parent),
  772. LUA_INTERFACE_DEF (config, register_finish_script),
  773. LUA_INTERFACE_DEF (config, register_monitored),
  774. LUA_INTERFACE_DEF (config, add_doc),
  775. LUA_INTERFACE_DEF (config, add_example),
  776. LUA_INTERFACE_DEF (config, set_peak_cb),
  777. LUA_INTERFACE_DEF (config, get_cpu_flags),
  778. LUA_INTERFACE_DEF (config, has_torch),
  779. LUA_INTERFACE_DEF (config, experimental_enabled),
  780. LUA_INTERFACE_DEF (config, load_ucl),
  781. LUA_INTERFACE_DEF (config, parse_rcl),
  782. LUA_INTERFACE_DEF (config, init_modules),
  783. LUA_INTERFACE_DEF (config, init_subsystem),
  784. LUA_INTERFACE_DEF (config, get_tld_path),
  785. {"__tostring", rspamd_lua_class_tostring},
  786. {"__newindex", lua_config_newindex},
  787. {NULL, NULL}
  788. };
  789. LUA_FUNCTION_DEF (monitored, alive);
  790. LUA_FUNCTION_DEF (monitored, latency);
  791. LUA_FUNCTION_DEF (monitored, offline);
  792. LUA_FUNCTION_DEF (monitored, total_offline);
  793. static const struct luaL_reg monitoredlib_m[] = {
  794. LUA_INTERFACE_DEF (monitored, alive),
  795. LUA_INTERFACE_DEF (monitored, latency),
  796. LUA_INTERFACE_DEF (monitored, offline),
  797. LUA_INTERFACE_DEF (monitored, total_offline),
  798. {"__tostring", rspamd_lua_class_tostring},
  799. {NULL, NULL}
  800. };
  801. static const guint64 rspamd_lua_callback_magic = 0x32c118af1e3263c7ULL;
  802. struct rspamd_config *
  803. lua_check_config (lua_State * L, gint pos)
  804. {
  805. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{config}");
  806. luaL_argcheck (L, ud != NULL, pos, "'config' expected");
  807. return ud ? *((struct rspamd_config **)ud) : NULL;
  808. }
  809. static struct rspamd_monitored *
  810. lua_check_monitored (lua_State * L, gint pos)
  811. {
  812. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{monitored}");
  813. luaL_argcheck (L, ud != NULL, pos, "'monitored' expected");
  814. return ud ? *((struct rspamd_monitored **)ud) : NULL;
  815. }
  816. /*** Config functions ***/
  817. static gint
  818. lua_config_get_api_version (lua_State *L)
  819. {
  820. msg_warn ("get_api_version is deprecated, do not use it");
  821. lua_pushnumber (L, 100);
  822. return 1;
  823. }
  824. static gint
  825. lua_config_get_module_opt (lua_State * L)
  826. {
  827. LUA_TRACE_POINT;
  828. struct rspamd_config *cfg = lua_check_config (L, 1);
  829. const gchar *mname, *optname;
  830. const ucl_object_t *obj;
  831. if (cfg) {
  832. mname = luaL_checkstring (L, 2);
  833. optname = luaL_checkstring (L, 3);
  834. if (mname && optname) {
  835. obj = rspamd_config_get_module_opt (cfg, mname, optname);
  836. if (obj) {
  837. return ucl_object_push_lua (L, obj, TRUE);
  838. }
  839. }
  840. }
  841. lua_pushnil (L);
  842. return 1;
  843. }
  844. static int
  845. lua_config_get_mempool (lua_State * L)
  846. {
  847. LUA_TRACE_POINT;
  848. rspamd_mempool_t **ppool;
  849. struct rspamd_config *cfg = lua_check_config (L, 1);
  850. if (cfg != NULL) {
  851. ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *));
  852. rspamd_lua_setclass (L, "rspamd{mempool}", -1);
  853. *ppool = cfg->cfg_pool;
  854. }
  855. else {
  856. lua_pushnil (L);
  857. }
  858. return 1;
  859. }
  860. static int
  861. lua_config_get_resolver (lua_State * L)
  862. {
  863. LUA_TRACE_POINT;
  864. struct rspamd_dns_resolver **pres;
  865. struct rspamd_config *cfg = lua_check_config (L, 1);
  866. if (cfg != NULL && cfg->dns_resolver) {
  867. pres = lua_newuserdata (L, sizeof (*pres));
  868. rspamd_lua_setclass (L, "rspamd{resolver}", -1);
  869. *pres = cfg->dns_resolver;
  870. }
  871. else {
  872. lua_pushnil (L);
  873. }
  874. return 1;
  875. }
  876. static gint
  877. lua_config_get_all_opt (lua_State * L)
  878. {
  879. LUA_TRACE_POINT;
  880. struct rspamd_config *cfg = lua_check_config (L, 1);
  881. const gchar *mname;
  882. const ucl_object_t *obj, *cur, *cur_elt;
  883. ucl_object_iter_t it = NULL;
  884. gint i;
  885. if (cfg) {
  886. mname = luaL_checkstring (L, 2);
  887. if (mname) {
  888. obj = ucl_obj_get_key (cfg->rcl_obj, mname);
  889. /* Flatten object */
  890. if (obj != NULL && (ucl_object_type (obj) == UCL_OBJECT ||
  891. ucl_object_type (obj) == UCL_ARRAY)) {
  892. lua_newtable (L);
  893. it = ucl_object_iterate_new (obj);
  894. LL_FOREACH (obj, cur) {
  895. it = ucl_object_iterate_reset (it, cur);
  896. while ((cur_elt = ucl_object_iterate_safe (it, true))) {
  897. lua_pushstring (L, ucl_object_key (cur_elt));
  898. ucl_object_push_lua (L, cur_elt, true);
  899. lua_settable (L, -3);
  900. }
  901. }
  902. ucl_object_iterate_free (it);
  903. return 1;
  904. }
  905. else if (obj != NULL) {
  906. lua_newtable (L);
  907. i = 1;
  908. LL_FOREACH (obj, cur) {
  909. lua_pushinteger (L, i++);
  910. ucl_object_push_lua (L, cur, true);
  911. lua_settable (L, -3);
  912. }
  913. return 1;
  914. }
  915. }
  916. }
  917. lua_pushnil (L);
  918. return 1;
  919. }
  920. struct rspamd_lua_cached_config {
  921. lua_State *L;
  922. gint ref;
  923. };
  924. static void
  925. lua_config_ucl_dtor (gpointer p)
  926. {
  927. struct rspamd_lua_cached_config *cached = p;
  928. luaL_unref (cached->L, LUA_REGISTRYINDEX, cached->ref);
  929. }
  930. static gint
  931. lua_config_get_ucl (lua_State * L)
  932. {
  933. LUA_TRACE_POINT;
  934. struct rspamd_config *cfg = lua_check_config (L, 1);
  935. struct rspamd_lua_cached_config *cached;
  936. if (cfg) {
  937. cached = rspamd_mempool_get_variable (cfg->cfg_pool, "ucl_cached");
  938. if (cached) {
  939. lua_rawgeti (L, LUA_REGISTRYINDEX, cached->ref);
  940. }
  941. else {
  942. ucl_object_push_lua (L, cfg->rcl_obj, true);
  943. lua_pushvalue (L, -1);
  944. cached = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (*cached));
  945. cached->L = L;
  946. cached->ref = luaL_ref (L, LUA_REGISTRYINDEX);
  947. rspamd_mempool_set_variable (cfg->cfg_pool, "ucl_cached",
  948. cached, lua_config_ucl_dtor);
  949. }
  950. }
  951. else {
  952. return luaL_error (L, "invalid arguments");
  953. }
  954. return 1;
  955. }
  956. static gint
  957. lua_config_get_classifier (lua_State * L)
  958. {
  959. LUA_TRACE_POINT;
  960. struct rspamd_config *cfg = lua_check_config (L, 1);
  961. struct rspamd_classifier_config *clc = NULL, **pclc = NULL;
  962. const gchar *name;
  963. GList *cur;
  964. if (cfg) {
  965. name = luaL_checkstring (L, 2);
  966. cur = g_list_first (cfg->classifiers);
  967. while (cur) {
  968. clc = cur->data;
  969. if (g_ascii_strcasecmp (clc->name, name) == 0) {
  970. pclc = &clc;
  971. break;
  972. }
  973. cur = g_list_next (cur);
  974. }
  975. if (pclc) {
  976. pclc = lua_newuserdata (L,
  977. sizeof (struct rspamd_classifier_config *));
  978. rspamd_lua_setclass (L, "rspamd{classifier}", -1);
  979. *pclc = clc;
  980. return 1;
  981. }
  982. }
  983. lua_pushnil (L);
  984. return 1;
  985. }
  986. struct lua_callback_data {
  987. guint64 magic;
  988. lua_State *L;
  989. gchar *symbol;
  990. union {
  991. gchar *name;
  992. gint ref;
  993. } callback;
  994. gboolean cb_is_ref;
  995. /* Dynamic data */
  996. gint stack_level;
  997. gint order;
  998. struct rspamd_symcache_item *item;
  999. };
  1000. /*
  1001. * Unref symbol if it is local reference
  1002. */
  1003. static void
  1004. lua_destroy_cfg_symbol (gpointer ud)
  1005. {
  1006. struct lua_callback_data *cd = ud;
  1007. /* Unref callback */
  1008. if (cd->cb_is_ref) {
  1009. luaL_unref (cd->L, LUA_REGISTRYINDEX, cd->callback.ref);
  1010. }
  1011. }
  1012. static gint
  1013. lua_config_register_module_option (lua_State *L)
  1014. {
  1015. return 0;
  1016. }
  1017. static gint
  1018. rspamd_compare_order_func (gconstpointer a, gconstpointer b)
  1019. {
  1020. const struct lua_callback_data *cb1 = a, *cb2 = b;
  1021. /* order of call goes from lower to higher */
  1022. return cb2->order - cb1->order;
  1023. }
  1024. static void
  1025. lua_metric_symbol_callback (struct rspamd_task *task,
  1026. struct rspamd_symcache_item *item,
  1027. gpointer ud)
  1028. {
  1029. struct lua_callback_data *cd = ud;
  1030. struct rspamd_task **ptask;
  1031. gint level = lua_gettop (cd->L), nresults, err_idx, ret;
  1032. lua_State *L = cd->L;
  1033. GString *tb;
  1034. struct rspamd_symbol_result *s;
  1035. cd->item = item;
  1036. rspamd_symcache_item_async_inc (task, item, "lua symbol");
  1037. lua_pushcfunction (L, &rspamd_lua_traceback);
  1038. err_idx = lua_gettop (L);
  1039. level ++;
  1040. if (cd->cb_is_ref) {
  1041. lua_rawgeti (L, LUA_REGISTRYINDEX, cd->callback.ref);
  1042. }
  1043. else {
  1044. lua_getglobal (L, cd->callback.name);
  1045. }
  1046. ptask = lua_newuserdata (L, sizeof (struct rspamd_task *));
  1047. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1048. *ptask = task;
  1049. if ((ret = lua_pcall (L, 1, LUA_MULTRET, err_idx)) != 0) {
  1050. tb = lua_touserdata (L, -1);
  1051. msg_err_task ("call to (%s) failed (%d): %v", cd->symbol, ret, tb);
  1052. if (tb) {
  1053. g_string_free (tb, TRUE);
  1054. lua_pop (L, 1);
  1055. }
  1056. }
  1057. else {
  1058. nresults = lua_gettop (L) - level;
  1059. if (nresults >= 1) {
  1060. /* Function returned boolean, so maybe we need to insert result? */
  1061. gint res = 0;
  1062. gint i;
  1063. gdouble flag = 1.0;
  1064. gint type;
  1065. type = lua_type (cd->L, level + 1);
  1066. if (type == LUA_TBOOLEAN) {
  1067. res = lua_toboolean (L, level + 1);
  1068. }
  1069. else if (type == LUA_TNUMBER) {
  1070. res = lua_tonumber (L, level + 1);
  1071. }
  1072. else if (type == LUA_TNIL) {
  1073. /* Can happen sometimes... */
  1074. res = FALSE;
  1075. }
  1076. else {
  1077. g_assert_not_reached ();
  1078. }
  1079. if (res) {
  1080. gint first_opt = 2;
  1081. if (lua_type (L, level + 2) == LUA_TNUMBER) {
  1082. flag = lua_tonumber (L, level + 2);
  1083. /* Shift opt index */
  1084. first_opt = 3;
  1085. }
  1086. else {
  1087. flag = res;
  1088. }
  1089. s = rspamd_task_insert_result (task, cd->symbol, flag, NULL);
  1090. if (s) {
  1091. guint last_pos = lua_gettop (L);
  1092. for (i = level + first_opt; i <= last_pos; i++) {
  1093. if (lua_type (L, i) == LUA_TSTRING) {
  1094. const char *opt = lua_tostring (L, i);
  1095. rspamd_task_add_result_option (task, s, opt);
  1096. }
  1097. else if (lua_type (L, i) == LUA_TTABLE) {
  1098. lua_pushvalue (L, i);
  1099. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  1100. const char *opt = lua_tostring (L, -1);
  1101. rspamd_task_add_result_option (task, s, opt);
  1102. }
  1103. lua_pop (L, 1);
  1104. }
  1105. }
  1106. }
  1107. }
  1108. lua_pop (L, nresults);
  1109. }
  1110. }
  1111. lua_pop (L, 1); /* Error function */
  1112. rspamd_symcache_item_async_dec_check (task, cd->item, "lua symbol");
  1113. g_assert (lua_gettop (L) == level - 1);
  1114. }
  1115. static void lua_metric_symbol_callback_return (struct thread_entry *thread_entry,
  1116. int ret);
  1117. static void lua_metric_symbol_callback_error (struct thread_entry *thread_entry,
  1118. int ret,
  1119. const char *msg);
  1120. static void
  1121. lua_metric_symbol_callback_coro (struct rspamd_task *task,
  1122. struct rspamd_symcache_item *item,
  1123. gpointer ud)
  1124. {
  1125. struct lua_callback_data *cd = ud;
  1126. struct rspamd_task **ptask;
  1127. struct thread_entry *thread_entry;
  1128. rspamd_symcache_item_async_inc (task, item, "lua coro symbol");
  1129. thread_entry = lua_thread_pool_get_for_task (task);
  1130. g_assert(thread_entry->cd == NULL);
  1131. thread_entry->cd = cd;
  1132. lua_State *thread = thread_entry->lua_state;
  1133. cd->stack_level = lua_gettop (thread);
  1134. cd->item = item;
  1135. if (cd->cb_is_ref) {
  1136. lua_rawgeti (thread, LUA_REGISTRYINDEX, cd->callback.ref);
  1137. }
  1138. else {
  1139. lua_getglobal (thread, cd->callback.name);
  1140. }
  1141. ptask = lua_newuserdata (thread, sizeof (struct rspamd_task *));
  1142. rspamd_lua_setclass (thread, "rspamd{task}", -1);
  1143. *ptask = task;
  1144. thread_entry->finish_callback = lua_metric_symbol_callback_return;
  1145. thread_entry->error_callback = lua_metric_symbol_callback_error;
  1146. lua_thread_call (thread_entry, 1);
  1147. }
  1148. static void
  1149. lua_metric_symbol_callback_error (struct thread_entry *thread_entry,
  1150. int ret,
  1151. const char *msg)
  1152. {
  1153. struct lua_callback_data *cd = thread_entry->cd;
  1154. struct rspamd_task *task = thread_entry->task;
  1155. msg_err_task ("call to coroutine (%s) failed (%d): %s", cd->symbol, ret, msg);
  1156. rspamd_symcache_item_async_dec_check (task, cd->item, "lua coro symbol");
  1157. }
  1158. static void
  1159. lua_metric_symbol_callback_return (struct thread_entry *thread_entry, int ret)
  1160. {
  1161. struct lua_callback_data *cd = thread_entry->cd;
  1162. struct rspamd_task *task = thread_entry->task;
  1163. int nresults;
  1164. struct rspamd_symbol_result *s;
  1165. (void)ret;
  1166. lua_State *L = thread_entry->lua_state;
  1167. nresults = lua_gettop (L) - cd->stack_level;
  1168. if (nresults >= 1) {
  1169. /* Function returned boolean, so maybe we need to insert result? */
  1170. gint res = 0;
  1171. gint i;
  1172. gdouble flag = 1.0;
  1173. gint type;
  1174. type = lua_type (L, cd->stack_level + 1);
  1175. if (type == LUA_TBOOLEAN) {
  1176. res = lua_toboolean (L, cd->stack_level + 1);
  1177. }
  1178. else if (type == LUA_TFUNCTION) {
  1179. g_assert_not_reached ();
  1180. }
  1181. else {
  1182. res = lua_tonumber (L, cd->stack_level + 1);
  1183. }
  1184. if (res) {
  1185. gint first_opt = 2;
  1186. if (lua_type (L, cd->stack_level + 2) == LUA_TNUMBER) {
  1187. flag = lua_tonumber (L, cd->stack_level + 2);
  1188. /* Shift opt index */
  1189. first_opt = 3;
  1190. }
  1191. else {
  1192. flag = res;
  1193. }
  1194. s = rspamd_task_insert_result (task, cd->symbol, flag, NULL);
  1195. if (s) {
  1196. guint last_pos = lua_gettop (L);
  1197. for (i = cd->stack_level + first_opt; i <= last_pos; i++) {
  1198. if (lua_type (L, i) == LUA_TSTRING) {
  1199. const char *opt = lua_tostring (L, i);
  1200. rspamd_task_add_result_option (task, s, opt);
  1201. }
  1202. else if (lua_type (L, i) == LUA_TTABLE) {
  1203. lua_pushvalue (L, i);
  1204. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  1205. const char *opt = lua_tostring (L, -1);
  1206. rspamd_task_add_result_option (task, s, opt);
  1207. }
  1208. lua_pop (L, 1);
  1209. }
  1210. }
  1211. }
  1212. }
  1213. lua_pop (L, nresults);
  1214. }
  1215. g_assert (lua_gettop (L) == cd->stack_level); /* we properly cleaned up the stack */
  1216. cd->stack_level = 0;
  1217. rspamd_symcache_item_async_dec_check (task, cd->item, "lua coro symbol");
  1218. }
  1219. static gint
  1220. rspamd_lua_squeeze_rule (lua_State *L,
  1221. struct rspamd_config *cfg,
  1222. const gchar *name,
  1223. gint cbref,
  1224. enum rspamd_symbol_type type,
  1225. gint parent)
  1226. {
  1227. gint ret = -1, err_idx;
  1228. lua_pushcfunction (L, &rspamd_lua_traceback);
  1229. err_idx = lua_gettop (L);
  1230. if (type & SYMBOL_TYPE_VIRTUAL) {
  1231. if (rspamd_lua_require_function (L, "lua_squeeze_rules", "squeeze_virtual")) {
  1232. lua_pushnumber (L, parent);
  1233. if (name) {
  1234. lua_pushstring (L, name);
  1235. }
  1236. else {
  1237. lua_pushnil (L);
  1238. }
  1239. /* Now call for squeeze function */
  1240. if (lua_pcall (L, 2, 1, err_idx) != 0) {
  1241. GString *tb = lua_touserdata (L, -1);
  1242. msg_err_config ("call to squeeze_virtual failed: %v", tb);
  1243. if (tb) {
  1244. g_string_free (tb, TRUE);
  1245. }
  1246. }
  1247. ret = lua_tonumber (L, -1);
  1248. }
  1249. else {
  1250. msg_err_config ("lua_squeeze is absent or bad (missing squeeze_virtual),"
  1251. " your Rspamd installation"
  1252. " is likely corrupted!");
  1253. }
  1254. }
  1255. else if (type & (SYMBOL_TYPE_CALLBACK|SYMBOL_TYPE_NORMAL)) {
  1256. if (rspamd_lua_require_function (L, "lua_squeeze_rules", "squeeze_rule")) {
  1257. if (name) {
  1258. lua_pushstring (L, name);
  1259. }
  1260. else {
  1261. lua_pushnil (L);
  1262. }
  1263. /* Push function reference */
  1264. lua_rawgeti (L, LUA_REGISTRYINDEX, cbref);
  1265. /* Flags */
  1266. lua_createtable (L, 0, 0);
  1267. if (type & SYMBOL_TYPE_MIME_ONLY) {
  1268. lua_pushstring (L, "mime");
  1269. lua_pushboolean (L, true);
  1270. lua_settable (L, -3);
  1271. }
  1272. if (type & SYMBOL_TYPE_FINE) {
  1273. lua_pushstring (L, "fine");
  1274. lua_pushboolean (L, true);
  1275. lua_settable (L, -3);
  1276. }
  1277. if (type & SYMBOL_TYPE_NOSTAT) {
  1278. lua_pushstring (L, "nostat");
  1279. lua_pushboolean (L, true);
  1280. lua_settable (L, -3);
  1281. }
  1282. if (type & SYMBOL_TYPE_EXPLICIT_DISABLE) {
  1283. lua_pushstring (L, "explicit_disable");
  1284. lua_pushboolean (L, true);
  1285. lua_settable (L, -3);
  1286. }
  1287. /* Now call for squeeze function */
  1288. if (lua_pcall (L, 3, 1, err_idx) != 0) {
  1289. GString *tb = lua_touserdata (L, -1);
  1290. msg_err_config ("call to squeeze_rule failed: %v", tb);
  1291. if (tb) {
  1292. g_string_free (tb, TRUE);
  1293. }
  1294. }
  1295. ret = lua_tonumber (L, -1);
  1296. }
  1297. else {
  1298. msg_err_config ("lua_squeeze is absent or bad (missing squeeze_rule),"
  1299. " your Rspamd installation"
  1300. " is likely corrupted!");
  1301. }
  1302. }
  1303. /* No squeeze for everything else */
  1304. /* Cleanup lua stack */
  1305. lua_settop (L, err_idx - 1);
  1306. return ret;
  1307. }
  1308. static gint
  1309. rspamd_register_symbol_fromlua (lua_State *L,
  1310. struct rspamd_config *cfg,
  1311. const gchar *name,
  1312. gint ref,
  1313. gdouble weight,
  1314. gint priority,
  1315. enum rspamd_symbol_type type,
  1316. gint parent,
  1317. gboolean optional,
  1318. gboolean no_squeeze)
  1319. {
  1320. struct lua_callback_data *cd;
  1321. gint ret = -1;
  1322. if (priority == 0 && weight < 0) {
  1323. priority = 1;
  1324. }
  1325. if ((ret = rspamd_symcache_find_symbol (cfg->cache, name)) != -1) {
  1326. if (optional) {
  1327. msg_debug_config ("duplicate symbol: %s, skip registering", name);
  1328. return ret;
  1329. }
  1330. else {
  1331. msg_err_config ("duplicate symbol: %s, skip registering", name);
  1332. return -1;
  1333. }
  1334. }
  1335. if (ref != -1) {
  1336. if (type & SYMBOL_TYPE_USE_CORO) {
  1337. /* Coroutines are incompatible with squeezing */
  1338. no_squeeze = TRUE;
  1339. }
  1340. /*
  1341. * We call for routine called lua_squeeze_rules.squeeze_rule if it exists
  1342. */
  1343. if (no_squeeze || (ret = rspamd_lua_squeeze_rule (L, cfg, name, ref,
  1344. type, parent)) == -1) {
  1345. cd = rspamd_mempool_alloc0 (cfg->cfg_pool,
  1346. sizeof (struct lua_callback_data));
  1347. cd->magic = rspamd_lua_callback_magic;
  1348. cd->cb_is_ref = TRUE;
  1349. cd->callback.ref = ref;
  1350. cd->L = L;
  1351. if (name) {
  1352. cd->symbol = rspamd_mempool_strdup (cfg->cfg_pool, name);
  1353. }
  1354. if (type & SYMBOL_TYPE_USE_CORO) {
  1355. ret = rspamd_symcache_add_symbol (cfg->cache,
  1356. name,
  1357. priority,
  1358. lua_metric_symbol_callback_coro,
  1359. cd,
  1360. type,
  1361. parent);
  1362. }
  1363. else {
  1364. ret = rspamd_symcache_add_symbol (cfg->cache,
  1365. name,
  1366. priority,
  1367. lua_metric_symbol_callback,
  1368. cd,
  1369. type,
  1370. parent);
  1371. }
  1372. rspamd_mempool_add_destructor (cfg->cfg_pool,
  1373. (rspamd_mempool_destruct_t)lua_destroy_cfg_symbol,
  1374. cd);
  1375. }
  1376. }
  1377. else {
  1378. if (!no_squeeze) {
  1379. rspamd_lua_squeeze_rule (L, cfg, name, ref,
  1380. type, parent);
  1381. }
  1382. /* Not a squeezed symbol */
  1383. ret = rspamd_symcache_add_symbol (cfg->cache,
  1384. name,
  1385. priority,
  1386. NULL,
  1387. NULL,
  1388. type,
  1389. parent);
  1390. }
  1391. return ret;
  1392. }
  1393. static gint
  1394. lua_config_register_post_filter (lua_State *L)
  1395. {
  1396. LUA_TRACE_POINT;
  1397. struct rspamd_config *cfg = lua_check_config (L, 1);
  1398. gint order = 0, cbref, ret;
  1399. if (cfg) {
  1400. if (lua_type (L, 3) == LUA_TNUMBER) {
  1401. order = lua_tonumber (L, 3);
  1402. }
  1403. if (lua_type (L, 2) == LUA_TFUNCTION) {
  1404. lua_pushvalue (L, 2);
  1405. /* Get a reference */
  1406. cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  1407. }
  1408. else {
  1409. return luaL_error (L, "invalid type for callback: %s",
  1410. lua_typename (L, lua_type (L, 2)));
  1411. }
  1412. msg_warn_config ("register_post_filter function is deprecated, "
  1413. "use register_symbol instead");
  1414. ret = rspamd_register_symbol_fromlua (L,
  1415. cfg,
  1416. NULL,
  1417. cbref,
  1418. 1.0,
  1419. order,
  1420. SYMBOL_TYPE_POSTFILTER|SYMBOL_TYPE_CALLBACK,
  1421. -1,
  1422. FALSE,
  1423. TRUE);
  1424. lua_pushboolean (L, ret);
  1425. }
  1426. else {
  1427. return luaL_error (L, "invalid arguments");
  1428. }
  1429. return 1;
  1430. }
  1431. static gint
  1432. lua_config_register_pre_filter (lua_State *L)
  1433. {
  1434. LUA_TRACE_POINT;
  1435. struct rspamd_config *cfg = lua_check_config (L, 1);
  1436. gint order = 0, cbref, ret;
  1437. if (cfg) {
  1438. if (lua_type (L, 3) == LUA_TNUMBER) {
  1439. order = lua_tonumber (L, 3);
  1440. }
  1441. if (lua_type (L, 2) == LUA_TFUNCTION) {
  1442. lua_pushvalue (L, 2);
  1443. /* Get a reference */
  1444. cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  1445. }
  1446. else {
  1447. return luaL_error (L, "invalid type for callback: %s",
  1448. lua_typename (L, lua_type (L, 2)));
  1449. }
  1450. msg_warn_config ("register_pre_filter function is deprecated, "
  1451. "use register_symbol instead");
  1452. ret = rspamd_register_symbol_fromlua (L,
  1453. cfg,
  1454. NULL,
  1455. cbref,
  1456. 1.0,
  1457. order,
  1458. SYMBOL_TYPE_PREFILTER|SYMBOL_TYPE_CALLBACK,
  1459. -1,
  1460. FALSE,
  1461. TRUE);
  1462. lua_pushboolean (L, ret);
  1463. }
  1464. else {
  1465. return luaL_error (L, "invalid arguments");
  1466. }
  1467. return 1;
  1468. }
  1469. static gint
  1470. lua_config_get_key (lua_State *L)
  1471. {
  1472. LUA_TRACE_POINT;
  1473. struct rspamd_config *cfg = lua_check_config (L, 1);
  1474. const gchar *name;
  1475. size_t namelen;
  1476. const ucl_object_t *val;
  1477. name = luaL_checklstring(L, 2, &namelen);
  1478. if (name && cfg) {
  1479. val = ucl_object_lookup_len(cfg->rcl_obj, name, namelen);
  1480. if (val != NULL) {
  1481. ucl_object_push_lua (L, val, val->type != UCL_ARRAY);
  1482. }
  1483. else {
  1484. lua_pushnil (L);
  1485. }
  1486. }
  1487. else {
  1488. return luaL_error (L, "invalid arguments");
  1489. }
  1490. return 1;
  1491. }
  1492. static gint
  1493. lua_parse_symbol_flags (const gchar *str)
  1494. {
  1495. int ret = 0;
  1496. if (str) {
  1497. if (strstr (str, "fine") != NULL) {
  1498. ret |= SYMBOL_TYPE_FINE;
  1499. }
  1500. if (strstr (str, "nice") != NULL) {
  1501. ret |= SYMBOL_TYPE_FINE;
  1502. }
  1503. if (strstr (str, "empty") != NULL) {
  1504. ret |= SYMBOL_TYPE_EMPTY;
  1505. }
  1506. if (strstr (str, "skip") != NULL) {
  1507. ret |= SYMBOL_TYPE_SKIPPED;
  1508. }
  1509. if (strstr (str, "nostat") != NULL) {
  1510. ret |= SYMBOL_TYPE_NOSTAT;
  1511. }
  1512. if (strstr (str, "idempotent") != NULL) {
  1513. ret |= SYMBOL_TYPE_IDEMPOTENT;
  1514. }
  1515. if (strstr (str, "squeezed") != NULL) {
  1516. ret |= SYMBOL_TYPE_SQUEEZED;
  1517. }
  1518. if (strstr (str, "trivial") != NULL) {
  1519. ret |= SYMBOL_TYPE_TRIVIAL;
  1520. }
  1521. if (strstr (str, "mime") != NULL) {
  1522. ret |= SYMBOL_TYPE_MIME_ONLY;
  1523. }
  1524. if (strstr (str, "ignore_passthrough") != NULL) {
  1525. ret |= SYMBOL_TYPE_IGNORE_PASSTHROUGH;
  1526. }
  1527. if (strstr (str, "explicit_disable") != NULL) {
  1528. ret |= SYMBOL_TYPE_EXPLICIT_DISABLE;
  1529. }
  1530. if (strstr (str, "coro") != NULL) {
  1531. ret |= SYMBOL_TYPE_USE_CORO;
  1532. }
  1533. }
  1534. return ret;
  1535. }
  1536. static gint
  1537. lua_parse_symbol_type (const gchar *str)
  1538. {
  1539. gint ret = SYMBOL_TYPE_NORMAL;
  1540. gchar **vec;
  1541. guint i, l;
  1542. if (str) {
  1543. vec = g_strsplit_set (str, ",;", -1);
  1544. if (vec) {
  1545. l = g_strv_length (vec);
  1546. for (i = 0; i < l; i ++) {
  1547. str = vec[i];
  1548. if (g_ascii_strcasecmp (str, "virtual") == 0) {
  1549. ret = SYMBOL_TYPE_VIRTUAL;
  1550. } else if (g_ascii_strcasecmp (str, "callback") == 0) {
  1551. ret = SYMBOL_TYPE_CALLBACK;
  1552. } else if (g_ascii_strcasecmp (str, "normal") == 0) {
  1553. ret = SYMBOL_TYPE_NORMAL;
  1554. } else if (g_ascii_strcasecmp (str, "prefilter") == 0) {
  1555. ret = SYMBOL_TYPE_PREFILTER | SYMBOL_TYPE_GHOST;
  1556. } else if (g_ascii_strcasecmp (str, "postfilter") == 0) {
  1557. ret = SYMBOL_TYPE_POSTFILTER | SYMBOL_TYPE_GHOST;
  1558. } else if (g_ascii_strcasecmp (str, "idempotent") == 0) {
  1559. ret = SYMBOL_TYPE_POSTFILTER | SYMBOL_TYPE_GHOST |
  1560. SYMBOL_TYPE_IDEMPOTENT;
  1561. } else {
  1562. gint fl = 0;
  1563. fl = lua_parse_symbol_flags (str);
  1564. if (fl == 0) {
  1565. msg_warn ("bad type: %s", str);
  1566. }
  1567. else {
  1568. ret |= fl;
  1569. }
  1570. }
  1571. }
  1572. g_strfreev (vec);
  1573. }
  1574. }
  1575. return ret;
  1576. }
  1577. static void
  1578. lua_push_symbol_flags (lua_State *L, guint flags)
  1579. {
  1580. guint i = 1;
  1581. lua_newtable (L);
  1582. if (flags & SYMBOL_TYPE_FINE) {
  1583. lua_pushstring (L, "fine");
  1584. lua_rawseti (L, -2, i++);
  1585. }
  1586. if (flags & SYMBOL_TYPE_EMPTY) {
  1587. lua_pushstring (L, "empty");
  1588. lua_rawseti (L, -2, i++);
  1589. }
  1590. if (flags & SYMBOL_TYPE_SQUEEZED) {
  1591. lua_pushstring (L, "squeezed");
  1592. lua_rawseti (L, -2, i++);
  1593. }
  1594. if (flags & SYMBOL_TYPE_EXPLICIT_DISABLE) {
  1595. lua_pushstring (L, "explicit_disable");
  1596. lua_rawseti (L, -2, i++);
  1597. }
  1598. if (flags & SYMBOL_TYPE_IGNORE_PASSTHROUGH) {
  1599. lua_pushstring (L, "ignore_passthrough");
  1600. lua_rawseti (L, -2, i++);
  1601. }
  1602. if (flags & SYMBOL_TYPE_NOSTAT) {
  1603. lua_pushstring (L, "nostat");
  1604. lua_rawseti (L, -2, i++);
  1605. }
  1606. if (flags & SYMBOL_TYPE_IDEMPOTENT) {
  1607. lua_pushstring (L, "idempotent");
  1608. lua_rawseti (L, -2, i++);
  1609. }
  1610. if (flags & SYMBOL_TYPE_MIME_ONLY) {
  1611. lua_pushstring (L, "mime");
  1612. lua_rawseti (L, -2, i++);
  1613. }
  1614. if (flags & SYMBOL_TYPE_TRIVIAL) {
  1615. lua_pushstring (L, "trivial");
  1616. lua_rawseti (L, -2, i++);
  1617. }
  1618. if (flags & SYMBOL_TYPE_SKIPPED) {
  1619. lua_pushstring (L, "skip");
  1620. lua_rawseti (L, -2, i++);
  1621. }
  1622. }
  1623. static gint
  1624. lua_config_get_symbol_flags (lua_State *L)
  1625. {
  1626. struct rspamd_config *cfg = lua_check_config (L, 1);
  1627. const gchar *name = luaL_checkstring (L, 2);
  1628. guint flags;
  1629. if (cfg && name) {
  1630. flags = rspamd_symcache_get_symbol_flags (cfg->cache,
  1631. name);
  1632. if (flags != 0) {
  1633. lua_push_symbol_flags (L, flags);
  1634. }
  1635. else {
  1636. lua_pushnil (L);
  1637. }
  1638. }
  1639. else {
  1640. return luaL_error (L, "invalid arguments");
  1641. }
  1642. return 1;
  1643. }
  1644. static gint
  1645. lua_config_add_symbol_flags (lua_State *L)
  1646. {
  1647. struct rspamd_config *cfg = lua_check_config (L, 1);
  1648. const gchar *name = luaL_checkstring (L, 2);
  1649. guint flags, new_flags = 0;
  1650. if (cfg && name && lua_istable (L, 3)) {
  1651. for (lua_pushnil (L); lua_next (L, 3); lua_pop (L, 1)) {
  1652. new_flags |= lua_parse_symbol_flags (lua_tostring (L, -1));
  1653. }
  1654. flags = rspamd_symcache_get_symbol_flags (cfg->cache,
  1655. name);
  1656. if (flags != 0) {
  1657. rspamd_symcache_add_symbol_flags (cfg->cache, name, new_flags);
  1658. /* Push old flags */
  1659. lua_push_symbol_flags (L, flags);
  1660. }
  1661. else {
  1662. lua_pushnil (L);
  1663. }
  1664. }
  1665. else {
  1666. return luaL_error (L, "invalid arguments");
  1667. }
  1668. return 1;
  1669. }
  1670. static gint
  1671. lua_config_register_symbol (lua_State * L)
  1672. {
  1673. LUA_TRACE_POINT;
  1674. struct rspamd_config *cfg = lua_check_config (L, 1);
  1675. const gchar *name = NULL, *flags_str = NULL, *type_str = NULL,
  1676. *description = NULL, *group = NULL;
  1677. double weight = 0, score = NAN, parent_float = NAN;
  1678. gboolean one_shot = FALSE, no_squeeze = FALSE;
  1679. gint ret = -1, cbref = -1, type, flags = 0;
  1680. gint64 parent = 0, priority = 0, nshots = 0;
  1681. GError *err = NULL;
  1682. if (cfg) {
  1683. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  1684. "name=S;weight=N;callback=F;flags=S;type=S;priority=I;parent=D;"
  1685. "score=D;description=S;group=S;one_shot=B;nshots=I;no_squeeze=B",
  1686. &name, &weight, &cbref, &flags_str, &type_str,
  1687. &priority, &parent_float,
  1688. &score, &description, &group, &one_shot, &nshots, &no_squeeze)) {
  1689. msg_err_config ("bad arguments: %e", err);
  1690. g_error_free (err);
  1691. return luaL_error (L, "invalid arguments");
  1692. }
  1693. if (!no_squeeze) {
  1694. no_squeeze = cfg->disable_lua_squeeze;
  1695. }
  1696. if (nshots == 0) {
  1697. nshots = cfg->default_max_shots;
  1698. }
  1699. type = lua_parse_symbol_type (type_str);
  1700. if (!name && !(type & SYMBOL_TYPE_CALLBACK)) {
  1701. return luaL_error (L, "no symbol name but type is not callback");
  1702. }
  1703. else if (!(type & SYMBOL_TYPE_VIRTUAL) && cbref == -1) {
  1704. return luaL_error (L, "no callback for symbol %s", name);
  1705. }
  1706. if (flags_str) {
  1707. /* Turn off squeezing as well for now */
  1708. /* TODO: deal with it */
  1709. no_squeeze = TRUE;
  1710. type |= lua_parse_symbol_flags (flags_str);
  1711. }
  1712. if (isnan (parent_float)) {
  1713. parent = -1;
  1714. }
  1715. else {
  1716. parent = parent_float;
  1717. }
  1718. ret = rspamd_register_symbol_fromlua (L,
  1719. cfg,
  1720. name,
  1721. cbref,
  1722. weight == 0 ? 1.0 : weight,
  1723. priority,
  1724. type,
  1725. parent,
  1726. FALSE,
  1727. no_squeeze);
  1728. if (!isnan (score) || group) {
  1729. if (one_shot) {
  1730. nshots = 1;
  1731. }
  1732. if (!isnan (score)) {
  1733. rspamd_config_add_symbol (cfg, name,
  1734. score, description, group, flags,
  1735. (guint) priority, nshots);
  1736. }
  1737. else {
  1738. rspamd_config_add_symbol (cfg, name,
  1739. 0.0, description, group, flags,
  1740. (guint) priority, nshots);
  1741. }
  1742. lua_pushstring (L, "groups");
  1743. lua_gettable (L, 2);
  1744. if (lua_istable (L, -1)) {
  1745. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  1746. if (lua_isstring (L, -1)) {
  1747. rspamd_config_add_symbol_group (cfg, name,
  1748. lua_tostring (L, -1));
  1749. }
  1750. else {
  1751. return luaL_error (L, "invalid groups element");
  1752. }
  1753. }
  1754. }
  1755. lua_pop (L, 1);
  1756. }
  1757. }
  1758. else {
  1759. return luaL_error (L, "invalid arguments");
  1760. }
  1761. lua_pushinteger (L, ret);
  1762. return 1;
  1763. }
  1764. static gint
  1765. lua_config_register_symbols (lua_State *L)
  1766. {
  1767. LUA_TRACE_POINT;
  1768. struct rspamd_config *cfg = lua_check_config (L, 1);
  1769. gint i, top, idx, ret = -1;
  1770. const gchar *sym;
  1771. gdouble weight = 1.0;
  1772. if (lua_gettop (L) < 3) {
  1773. if (cfg) {
  1774. msg_err_config ("not enough arguments to register a function");
  1775. }
  1776. lua_error (L);
  1777. return 0;
  1778. }
  1779. if (cfg) {
  1780. if (lua_type (L, 2) == LUA_TSTRING) {
  1781. lua_getglobal (L, luaL_checkstring (L, 2));
  1782. }
  1783. else {
  1784. lua_pushvalue (L, 2);
  1785. }
  1786. idx = luaL_ref (L, LUA_REGISTRYINDEX);
  1787. if (lua_type (L, 3) == LUA_TNUMBER) {
  1788. weight = lua_tonumber (L, 3);
  1789. top = 4;
  1790. }
  1791. else {
  1792. top = 3;
  1793. }
  1794. sym = luaL_checkstring (L, top ++);
  1795. ret = rspamd_register_symbol_fromlua (L,
  1796. cfg,
  1797. sym,
  1798. idx,
  1799. weight,
  1800. 0,
  1801. SYMBOL_TYPE_CALLBACK,
  1802. -1,
  1803. FALSE,
  1804. FALSE);
  1805. for (i = top; i <= lua_gettop (L); i++) {
  1806. if (lua_type (L, i) == LUA_TTABLE) {
  1807. lua_pushvalue (L, i);
  1808. lua_pushnil (L);
  1809. while (lua_next (L, -2)) {
  1810. lua_pushvalue (L, -2);
  1811. sym = luaL_checkstring (L, -2);
  1812. rspamd_symcache_add_symbol (cfg->cache, sym,
  1813. 0, NULL, NULL,
  1814. SYMBOL_TYPE_VIRTUAL, ret);
  1815. lua_pop (L, 2);
  1816. }
  1817. lua_pop (L, 1);
  1818. }
  1819. else if (lua_type (L, i) == LUA_TSTRING) {
  1820. sym = luaL_checkstring (L, i);
  1821. rspamd_symcache_add_symbol (cfg->cache, sym,
  1822. 0, NULL, NULL,
  1823. SYMBOL_TYPE_VIRTUAL, ret);
  1824. }
  1825. }
  1826. }
  1827. lua_pushinteger (L, ret);
  1828. return 1;
  1829. }
  1830. static gint
  1831. lua_config_register_virtual_symbol (lua_State * L)
  1832. {
  1833. LUA_TRACE_POINT;
  1834. struct rspamd_config *cfg = lua_check_config (L, 1);
  1835. const gchar *name;
  1836. double weight;
  1837. gint ret = -1, parent = -1;
  1838. if (cfg) {
  1839. name = luaL_checkstring (L, 2);
  1840. weight = luaL_checknumber (L, 3);
  1841. if (lua_gettop (L) > 3) {
  1842. parent = lua_tonumber (L, 4);
  1843. }
  1844. if (name) {
  1845. ret = rspamd_symcache_add_symbol (cfg->cache, name,
  1846. weight > 0 ? 0 : -1, NULL, NULL,
  1847. SYMBOL_TYPE_VIRTUAL, parent);
  1848. }
  1849. }
  1850. lua_pushinteger (L, ret);
  1851. return 1;
  1852. }
  1853. static gint
  1854. lua_config_register_callback_symbol (lua_State * L)
  1855. {
  1856. LUA_TRACE_POINT;
  1857. struct rspamd_config *cfg = lua_check_config (L, 1);
  1858. const gchar *name = NULL;
  1859. double weight;
  1860. gint ret = -1, top = 2;
  1861. if (cfg) {
  1862. if (lua_type (L, 2) == LUA_TSTRING) {
  1863. /* Legacy syntax */
  1864. name = luaL_checkstring (L, 2);
  1865. top ++;
  1866. }
  1867. weight = luaL_checknumber (L, top);
  1868. if (lua_type (L, top + 1) == LUA_TSTRING) {
  1869. lua_getglobal (L, luaL_checkstring (L, top + 1));
  1870. }
  1871. else {
  1872. lua_pushvalue (L, top + 1);
  1873. }
  1874. ret = rspamd_register_symbol_fromlua (L,
  1875. cfg,
  1876. name,
  1877. luaL_ref (L, LUA_REGISTRYINDEX),
  1878. weight,
  1879. 0,
  1880. SYMBOL_TYPE_CALLBACK,
  1881. -1,
  1882. FALSE,
  1883. lua_type (L, top + 1) == LUA_TSTRING);
  1884. }
  1885. lua_pushinteger (L, ret);
  1886. return 1;
  1887. }
  1888. static gint
  1889. lua_config_register_callback_symbol_priority (lua_State * L)
  1890. {
  1891. LUA_TRACE_POINT;
  1892. struct rspamd_config *cfg = lua_check_config (L, 1);
  1893. const gchar *name = NULL;
  1894. double weight;
  1895. gint priority, ret = -1, top = 2;
  1896. if (cfg) {
  1897. if (lua_type (L, 2) == LUA_TSTRING) {
  1898. /* Legacy syntax */
  1899. name = luaL_checkstring (L, 2);
  1900. top ++;
  1901. }
  1902. weight = luaL_checknumber (L, top);
  1903. priority = luaL_checknumber (L, top + 1);
  1904. if (lua_type (L, top + 2) == LUA_TSTRING) {
  1905. lua_getglobal (L, luaL_checkstring (L, top + 2));
  1906. }
  1907. else {
  1908. lua_pushvalue (L, top + 2);
  1909. }
  1910. ret = rspamd_register_symbol_fromlua (L,
  1911. cfg,
  1912. name,
  1913. luaL_ref (L, LUA_REGISTRYINDEX),
  1914. weight,
  1915. priority,
  1916. SYMBOL_TYPE_CALLBACK,
  1917. -1,
  1918. FALSE,
  1919. lua_type (L, top + 2) == LUA_TSTRING);
  1920. }
  1921. lua_pushinteger (L, ret);
  1922. return 1;
  1923. }
  1924. static gboolean
  1925. rspamd_lua_squeeze_dependency (lua_State *L, struct rspamd_config *cfg,
  1926. const gchar *child,
  1927. const gchar *parent)
  1928. {
  1929. gint err_idx;
  1930. gboolean ret = FALSE;
  1931. g_assert (parent != NULL);
  1932. g_assert (child != NULL);
  1933. lua_pushcfunction (L, &rspamd_lua_traceback);
  1934. err_idx = lua_gettop (L);
  1935. if (rspamd_lua_require_function (L, "lua_squeeze_rules", "squeeze_dependency")) {
  1936. lua_pushstring (L, child);
  1937. lua_pushstring (L, parent);
  1938. if (lua_pcall (L, 2, 1, err_idx) != 0) {
  1939. GString *tb = lua_touserdata (L, -1);
  1940. msg_err_config ("call to squeeze_dependency script failed: %v", tb);
  1941. if (tb) {
  1942. g_string_free (tb, TRUE);
  1943. }
  1944. }
  1945. else {
  1946. ret = lua_toboolean (L, -1);
  1947. }
  1948. }
  1949. else {
  1950. msg_err_config ("cannot get lua_squeeze_rules.squeeze_dependency function");
  1951. }
  1952. lua_settop (L, err_idx - 1);
  1953. return ret;
  1954. }
  1955. static gint
  1956. lua_config_register_dependency (lua_State * L)
  1957. {
  1958. LUA_TRACE_POINT;
  1959. struct rspamd_config *cfg = lua_check_config (L, 1);
  1960. const gchar *parent = NULL, *child = NULL;
  1961. gint child_id;
  1962. gboolean skip_squeeze;
  1963. if (cfg == NULL) {
  1964. lua_error (L);
  1965. return 0;
  1966. }
  1967. skip_squeeze = cfg->disable_lua_squeeze;
  1968. if (lua_type (L, 2) == LUA_TNUMBER) {
  1969. child_id = luaL_checknumber (L, 2);
  1970. parent = luaL_checkstring (L, 3);
  1971. if (!skip_squeeze && lua_isboolean (L, 4)) {
  1972. skip_squeeze = lua_toboolean (L, 4);
  1973. }
  1974. msg_warn_config ("calling for obsolete method to register deps for symbol %d->%s",
  1975. child_id, parent);
  1976. if (child_id > 0 && parent != NULL) {
  1977. if (skip_squeeze || !rspamd_lua_squeeze_dependency (L, cfg,
  1978. rspamd_symcache_symbol_by_id (cfg->cache, child_id),
  1979. parent)) {
  1980. rspamd_symcache_add_dependency (cfg->cache, child_id, parent);
  1981. }
  1982. }
  1983. }
  1984. else {
  1985. child = luaL_checkstring (L,2);
  1986. parent = luaL_checkstring (L, 3);
  1987. if (!skip_squeeze && lua_isboolean (L, 4)) {
  1988. skip_squeeze = lua_toboolean (L, 4);
  1989. }
  1990. if (child != NULL && parent != NULL) {
  1991. if (skip_squeeze || !rspamd_lua_squeeze_dependency (L, cfg, child, parent)) {
  1992. rspamd_symcache_add_delayed_dependency (cfg->cache, child,
  1993. parent);
  1994. }
  1995. }
  1996. }
  1997. return 0;
  1998. }
  1999. static gint
  2000. lua_config_set_metric_symbol (lua_State * L)
  2001. {
  2002. LUA_TRACE_POINT;
  2003. struct rspamd_config *cfg = lua_check_config (L, 1);
  2004. const gchar *description = NULL,
  2005. *group = NULL, *name = NULL, *flags_str = NULL;
  2006. double weight;
  2007. gboolean one_shot = FALSE, one_param = FALSE;
  2008. GError *err = NULL;
  2009. gdouble priority = 0.0;
  2010. guint flags = 0;
  2011. gint64 nshots = 0;
  2012. if (cfg) {
  2013. if (lua_type (L, 2) == LUA_TTABLE) {
  2014. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  2015. "*name=S;score=N;description=S;"
  2016. "group=S;one_shot=B;one_param=B;priority=N;flags=S;"
  2017. "nshots=I",
  2018. &name, &weight, &description,
  2019. &group, &one_shot, &one_param,
  2020. &priority, &flags_str, &nshots)) {
  2021. msg_err_config ("bad arguments: %e", err);
  2022. g_error_free (err);
  2023. return 0;
  2024. }
  2025. }
  2026. else {
  2027. name = luaL_checkstring (L, 2);
  2028. weight = luaL_checknumber (L, 3);
  2029. if (lua_gettop (L) > 3 && lua_type (L, 4) == LUA_TSTRING) {
  2030. description = luaL_checkstring (L, 4);
  2031. }
  2032. if (lua_gettop (L) > 4 && lua_type (L, 5) == LUA_TSTRING) {
  2033. /* XXX: metrics */
  2034. }
  2035. if (lua_gettop (L) > 5 && lua_type (L, 6) == LUA_TSTRING) {
  2036. group = luaL_checkstring (L, 6);
  2037. }
  2038. if (lua_gettop (L) > 6 && lua_type (L, 7) == LUA_TBOOLEAN) {
  2039. one_shot = lua_toboolean (L, 7);
  2040. }
  2041. }
  2042. if (nshots == 0) {
  2043. nshots = cfg->default_max_shots;
  2044. }
  2045. if (one_shot) {
  2046. nshots = 1;
  2047. }
  2048. if (one_param) {
  2049. flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM;
  2050. }
  2051. if (flags_str) {
  2052. if (strstr (flags_str, "one_shot") != NULL) {
  2053. nshots = 1;
  2054. }
  2055. if (strstr (flags_str, "ignore") != NULL) {
  2056. flags |= RSPAMD_SYMBOL_FLAG_IGNORE;
  2057. }
  2058. if (strstr (flags_str, "one_param") != NULL) {
  2059. flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM;
  2060. }
  2061. }
  2062. rspamd_config_add_symbol (cfg, name,
  2063. weight, description, group, flags, (guint) priority, nshots);
  2064. if (lua_type (L, 2) == LUA_TTABLE) {
  2065. lua_pushstring (L, "groups");
  2066. lua_gettable (L, 2);
  2067. if (lua_istable (L, -1)) {
  2068. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  2069. if (lua_isstring (L, -1)) {
  2070. rspamd_config_add_symbol_group (cfg, name,
  2071. lua_tostring (L, -1));
  2072. } else {
  2073. return luaL_error (L, "invalid groups element");
  2074. }
  2075. }
  2076. }
  2077. lua_pop (L, 1);
  2078. }
  2079. }
  2080. else {
  2081. return luaL_error (L, "invalid arguments, rspamd_config expected");
  2082. }
  2083. return 0;
  2084. }
  2085. static gint
  2086. lua_config_get_metric_symbol (lua_State * L)
  2087. {
  2088. LUA_TRACE_POINT;
  2089. struct rspamd_config *cfg = lua_check_config (L, 1);
  2090. const gchar *sym_name = luaL_checkstring (L, 2);
  2091. struct rspamd_symbol *sym_def;
  2092. struct rspamd_symbols_group *sym_group;
  2093. guint i;
  2094. if (cfg && sym_name) {
  2095. sym_def = g_hash_table_lookup (cfg->symbols, sym_name);
  2096. if (sym_def == NULL) {
  2097. lua_pushnil (L);
  2098. }
  2099. else {
  2100. lua_createtable (L, 0, 3);
  2101. lua_pushstring (L, "score");
  2102. lua_pushnumber (L, sym_def->score);
  2103. lua_settable (L, -3);
  2104. if (sym_def->description) {
  2105. lua_pushstring (L, "description");
  2106. lua_pushstring (L, sym_def->description);
  2107. lua_settable (L, -3);
  2108. }
  2109. if (sym_def->gr) {
  2110. lua_pushstring (L, "group");
  2111. lua_pushstring (L, sym_def->gr->name);
  2112. lua_settable (L, -3);
  2113. }
  2114. lua_pushstring (L, "groups");
  2115. lua_createtable (L, sym_def->groups->len, 0);
  2116. PTR_ARRAY_FOREACH (sym_def->groups, i, sym_group) {
  2117. lua_pushstring (L, sym_group->name);
  2118. lua_rawseti (L, -2, i + 1);
  2119. }
  2120. lua_settable (L, -3);
  2121. }
  2122. }
  2123. else {
  2124. luaL_error (L, "Invalid arguments");
  2125. }
  2126. return 1;
  2127. }
  2128. static gint
  2129. lua_config_set_metric_action (lua_State * L)
  2130. {
  2131. LUA_TRACE_POINT;
  2132. struct rspamd_config *cfg = lua_check_config (L, 1);
  2133. const gchar *name = NULL;
  2134. double threshold = NAN;
  2135. GError *err = NULL;
  2136. gdouble priority = 0.0;
  2137. ucl_object_t *obj_tbl = NULL;
  2138. if (cfg) {
  2139. if (lua_type (L, 2) == LUA_TTABLE) {
  2140. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  2141. "*action=S;score=N;"
  2142. "priority=N",
  2143. &name, &threshold,
  2144. &priority)) {
  2145. msg_err_config ("bad arguments: %e", err);
  2146. g_error_free (err);
  2147. return 0;
  2148. }
  2149. }
  2150. else if (lua_type (L, 2) == LUA_TSTRING && lua_type (L, 3) == LUA_TTABLE) {
  2151. name = lua_tostring (L, 2);
  2152. obj_tbl = ucl_object_lua_import (L, 3);
  2153. if (obj_tbl) {
  2154. if (name) {
  2155. rspamd_config_set_action_score (cfg, name, obj_tbl);
  2156. ucl_object_unref (obj_tbl);
  2157. }
  2158. else {
  2159. ucl_object_unref (obj_tbl);
  2160. return luaL_error (L, "invalid first argument, action name expected");
  2161. }
  2162. }
  2163. else {
  2164. return luaL_error (L, "invalid second argument, table expected");
  2165. }
  2166. }
  2167. else {
  2168. return luaL_error (L, "invalid arguments, table expected");
  2169. }
  2170. if (name != NULL && !isnan (threshold) && threshold != 0) {
  2171. obj_tbl = ucl_object_typed_new (UCL_OBJECT);
  2172. ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (threshold),
  2173. "score", 0, false);
  2174. ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (priority),
  2175. "priority", 0, false);
  2176. rspamd_config_set_action_score (cfg, name, obj_tbl);
  2177. ucl_object_unref (obj_tbl);
  2178. }
  2179. }
  2180. else {
  2181. return luaL_error (L, "invalid arguments, rspamd_config expected");
  2182. }
  2183. return 0;
  2184. }
  2185. static gint
  2186. lua_config_get_metric_action (lua_State * L)
  2187. {
  2188. LUA_TRACE_POINT;
  2189. struct rspamd_config *cfg = lua_check_config (L, 1);
  2190. const gchar *act_name = luaL_checkstring (L, 2);
  2191. struct rspamd_action *act;
  2192. if (cfg && act_name) {
  2193. act = rspamd_config_get_action (cfg, act_name);
  2194. if (act) {
  2195. if (!isnan (act->threshold)) {
  2196. lua_pushnumber (L, act->threshold);
  2197. }
  2198. else {
  2199. lua_pushnil (L);
  2200. }
  2201. }
  2202. else {
  2203. lua_pushnil (L);
  2204. }
  2205. }
  2206. else {
  2207. return luaL_error (L, "invalid arguments, rspamd_config expected");
  2208. }
  2209. return 1;
  2210. }
  2211. static gint
  2212. lua_config_get_all_actions (lua_State * L)
  2213. {
  2214. LUA_TRACE_POINT;
  2215. struct rspamd_config *cfg = lua_check_config (L, 1);
  2216. struct rspamd_action *act, *tmp;
  2217. if (cfg) {
  2218. lua_createtable (L, 0, HASH_COUNT (cfg->actions));
  2219. HASH_ITER (hh, cfg->actions, act, tmp) {
  2220. if (!isnan (act->threshold)) {
  2221. lua_pushstring (L, act->name);
  2222. lua_pushnumber (L, act->threshold);
  2223. lua_settable (L, -3);
  2224. }
  2225. }
  2226. }
  2227. else {
  2228. return luaL_error (L, "invalid arguments, rspamd_config expected");
  2229. }
  2230. return 1;
  2231. }
  2232. static gint
  2233. lua_config_add_composite (lua_State * L)
  2234. {
  2235. LUA_TRACE_POINT;
  2236. struct rspamd_config *cfg = lua_check_config (L, 1);
  2237. struct rspamd_expression *expr;
  2238. gchar *name;
  2239. const gchar *expr_str;
  2240. struct rspamd_composite *composite;
  2241. gboolean ret = FALSE, new = TRUE;
  2242. GError *err = NULL;
  2243. if (cfg) {
  2244. name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
  2245. expr_str = luaL_checkstring (L, 3);
  2246. if (name && expr_str) {
  2247. if (!rspamd_parse_expression (expr_str, 0, &composite_expr_subr,
  2248. NULL, cfg->cfg_pool, &err, &expr)) {
  2249. msg_err_config ("cannot parse composite expression %s: %e",
  2250. expr_str,
  2251. err);
  2252. g_error_free (err);
  2253. }
  2254. else {
  2255. if (g_hash_table_lookup (cfg->composite_symbols, name) != NULL) {
  2256. msg_warn_config ("composite %s is redefined", name);
  2257. new = FALSE;
  2258. }
  2259. composite = rspamd_mempool_alloc0 (cfg->cfg_pool,
  2260. sizeof (struct rspamd_composite));
  2261. composite->expr = expr;
  2262. composite->id = g_hash_table_size (cfg->composite_symbols);
  2263. composite->str_expr = rspamd_mempool_strdup (cfg->cfg_pool,
  2264. expr_str);
  2265. composite->sym = name;
  2266. g_hash_table_insert (cfg->composite_symbols,
  2267. (gpointer)name,
  2268. composite);
  2269. if (new) {
  2270. rspamd_symcache_add_symbol (cfg->cache, name,
  2271. 0, NULL, composite, SYMBOL_TYPE_COMPOSITE, -1);
  2272. }
  2273. ret = TRUE;
  2274. }
  2275. }
  2276. }
  2277. lua_pushboolean (L, ret);
  2278. return 1;
  2279. }
  2280. static gint
  2281. lua_config_newindex (lua_State *L)
  2282. {
  2283. LUA_TRACE_POINT;
  2284. struct rspamd_config *cfg = lua_check_config (L, 1);
  2285. const gchar *name;
  2286. gint id, nshots, flags = 0;
  2287. gboolean optional = FALSE, no_squeeze = FALSE;
  2288. name = luaL_checkstring (L, 2);
  2289. if (cfg != NULL && name != NULL && lua_gettop (L) == 3) {
  2290. no_squeeze = cfg->disable_lua_squeeze;
  2291. if (lua_type (L, 3) == LUA_TFUNCTION) {
  2292. /* Normal symbol from just a function */
  2293. lua_pushvalue (L, 3);
  2294. rspamd_register_symbol_fromlua (L,
  2295. cfg,
  2296. name,
  2297. luaL_ref (L, LUA_REGISTRYINDEX),
  2298. 1.0,
  2299. 0,
  2300. SYMBOL_TYPE_NORMAL,
  2301. -1,
  2302. FALSE,
  2303. no_squeeze);
  2304. }
  2305. else if (lua_type (L, 3) == LUA_TTABLE) {
  2306. gint type = SYMBOL_TYPE_NORMAL, priority = 0, idx;
  2307. gdouble weight = 1.0, score = NAN;
  2308. const char *type_str, *group = NULL, *description = NULL;
  2309. no_squeeze = cfg->disable_lua_squeeze;
  2310. /*
  2311. * Table can have the following attributes:
  2312. * "callback" - should be a callback function
  2313. * "weight" - optional weight
  2314. * "priority" - optional priority
  2315. * "type" - optional type (normal, virtual, callback)
  2316. * "flags" - optional flags
  2317. * -- Metric options
  2318. * "score" - optional default score (overridden by metric)
  2319. * "group" - optional default group
  2320. * "one_shot" - optional one shot mode
  2321. * "description" - optional description
  2322. */
  2323. lua_pushvalue (L, 3);
  2324. lua_pushstring (L, "callback");
  2325. lua_gettable (L, -2);
  2326. if (lua_type (L, -1) != LUA_TFUNCTION) {
  2327. lua_pop (L, 2);
  2328. msg_info_config ("cannot find callback definition for %s",
  2329. name);
  2330. return 0;
  2331. }
  2332. idx = luaL_ref (L, LUA_REGISTRYINDEX);
  2333. /* Optional fields */
  2334. lua_pushstring (L, "weight");
  2335. lua_gettable (L, -2);
  2336. if (lua_type (L, -1) == LUA_TNUMBER) {
  2337. weight = lua_tonumber (L, -1);
  2338. }
  2339. lua_pop (L, 1);
  2340. lua_pushstring (L, "priority");
  2341. lua_gettable (L, -2);
  2342. if (lua_type (L, -1) == LUA_TNUMBER) {
  2343. priority = lua_tonumber (L, -1);
  2344. }
  2345. lua_pop (L, 1);
  2346. lua_pushstring (L, "optional");
  2347. lua_gettable (L, -2);
  2348. if (lua_type (L, -1) == LUA_TBOOLEAN) {
  2349. optional = lua_toboolean (L, -1);
  2350. }
  2351. lua_pop (L, 1);
  2352. lua_pushstring (L, "type");
  2353. lua_gettable (L, -2);
  2354. if (lua_type (L, -1) == LUA_TSTRING) {
  2355. type_str = lua_tostring (L, -1);
  2356. type = lua_parse_symbol_type (type_str);
  2357. }
  2358. lua_pop (L, 1);
  2359. lua_pushstring (L, "flags");
  2360. lua_gettable (L, -2);
  2361. if (lua_type (L, -1) == LUA_TSTRING) {
  2362. type_str = lua_tostring (L, -1);
  2363. type |= lua_parse_symbol_flags (type_str);
  2364. }
  2365. lua_pop (L, 1);
  2366. lua_pushstring (L, "condition");
  2367. lua_gettable (L, -2);
  2368. if (lua_type (L, -1) == LUA_TFUNCTION) {
  2369. no_squeeze = TRUE;
  2370. }
  2371. lua_pop (L, 1);
  2372. if (!no_squeeze) {
  2373. lua_pushstring (L, "no_squeeze");
  2374. lua_gettable (L, -2);
  2375. if (lua_toboolean (L, -1)) {
  2376. no_squeeze = TRUE;
  2377. }
  2378. lua_pop (L, 1);
  2379. }
  2380. id = rspamd_register_symbol_fromlua (L,
  2381. cfg,
  2382. name,
  2383. idx,
  2384. weight,
  2385. priority,
  2386. type,
  2387. -1,
  2388. optional,
  2389. no_squeeze);
  2390. if (id != -1) {
  2391. /* Check for condition */
  2392. lua_pushstring (L, "condition");
  2393. lua_gettable (L, -2);
  2394. if (lua_type (L, -1) == LUA_TFUNCTION) {
  2395. gint condref;
  2396. /* Here we pop function from the stack, so no lua_pop is required */
  2397. condref = luaL_ref (L, LUA_REGISTRYINDEX);
  2398. g_assert (name != NULL);
  2399. rspamd_symcache_add_condition_delayed (cfg->cache,
  2400. name, L, condref);
  2401. }
  2402. else {
  2403. lua_pop (L, 1);
  2404. }
  2405. }
  2406. /*
  2407. * Now check if a symbol has not been registered in any metric and
  2408. * insert default value if applicable
  2409. */
  2410. if (g_hash_table_lookup (cfg->symbols, name) == NULL) {
  2411. nshots = cfg->default_max_shots;
  2412. lua_pushstring (L, "score");
  2413. lua_gettable (L, -2);
  2414. if (lua_type (L, -1) == LUA_TNUMBER) {
  2415. score = lua_tonumber (L, -1);
  2416. }
  2417. lua_pop (L, 1);
  2418. lua_pushstring (L, "group");
  2419. lua_gettable (L, -2);
  2420. if (lua_type (L, -1) == LUA_TSTRING) {
  2421. group = lua_tostring (L, -1);
  2422. }
  2423. lua_pop (L, 1);
  2424. if (!isnan (score) || group != NULL) {
  2425. lua_pushstring (L, "description");
  2426. lua_gettable (L, -2);
  2427. if (lua_type (L, -1) == LUA_TSTRING) {
  2428. description = lua_tostring (L, -1);
  2429. }
  2430. lua_pop (L, 1);
  2431. lua_pushstring (L, "one_shot");
  2432. lua_gettable (L, -2);
  2433. if (lua_type (L, -1) == LUA_TBOOLEAN) {
  2434. if (lua_toboolean (L, -1)) {
  2435. nshots = 1;
  2436. }
  2437. }
  2438. lua_pop (L, 1);
  2439. lua_pushstring (L, "one_param");
  2440. lua_gettable (L, -2);
  2441. if (lua_type (L, -1) == LUA_TBOOLEAN) {
  2442. if (lua_toboolean (L, -1)) {
  2443. flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM;
  2444. }
  2445. }
  2446. lua_pop (L, 1);
  2447. /*
  2448. * Do not override the existing symbols (using zero priority),
  2449. * since we are defining default values here
  2450. */
  2451. if (!isnan (score)) {
  2452. rspamd_config_add_symbol (cfg, name, score,
  2453. description, group, flags, 0, nshots);
  2454. }
  2455. else if (group) {
  2456. /* Add with zero score */
  2457. rspamd_config_add_symbol (cfg, name, 0.0,
  2458. description, group, flags, 0, nshots);
  2459. }
  2460. lua_pushstring (L, "groups");
  2461. lua_gettable (L, -2);
  2462. if (lua_istable (L, -1)) {
  2463. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  2464. if (lua_isstring (L, -1)) {
  2465. rspamd_config_add_symbol_group (cfg, name,
  2466. lua_tostring (L, -1));
  2467. }
  2468. else {
  2469. return luaL_error (L, "invalid groups element");
  2470. }
  2471. }
  2472. }
  2473. lua_pop (L, 1);
  2474. }
  2475. }
  2476. /* Remove table from stack */
  2477. lua_pop (L, 1);
  2478. }
  2479. }
  2480. else {
  2481. return luaL_error (L, "invalid arguments");
  2482. }
  2483. return 0;
  2484. }
  2485. static gint
  2486. lua_config_add_condition (lua_State *L)
  2487. {
  2488. LUA_TRACE_POINT;
  2489. struct rspamd_config *cfg = lua_check_config (L, 1);
  2490. const gchar *sym = luaL_checkstring (L, 2);
  2491. gboolean ret = FALSE;
  2492. gint condref;
  2493. if (cfg && sym && lua_type (L, 3) == LUA_TFUNCTION) {
  2494. lua_pushvalue (L, 3);
  2495. condref = luaL_ref (L, LUA_REGISTRYINDEX);
  2496. ret = rspamd_symcache_add_condition_delayed (cfg->cache, sym, L,
  2497. condref);
  2498. if (!ret) {
  2499. luaL_unref (L, LUA_REGISTRYINDEX, condref);
  2500. }
  2501. }
  2502. lua_pushboolean (L, ret);
  2503. return 1;
  2504. }
  2505. static gint
  2506. lua_config_set_peak_cb (lua_State *L)
  2507. {
  2508. LUA_TRACE_POINT;
  2509. struct rspamd_config *cfg = lua_check_config (L, 1);
  2510. gint condref;
  2511. if (cfg && lua_type (L, 2) == LUA_TFUNCTION) {
  2512. lua_pushvalue (L, 2);
  2513. condref = luaL_ref (L, LUA_REGISTRYINDEX);
  2514. rspamd_symcache_set_peak_callback (cfg->cache,
  2515. condref);
  2516. }
  2517. return 0;
  2518. }
  2519. static gint
  2520. lua_config_enable_symbol (lua_State *L)
  2521. {
  2522. LUA_TRACE_POINT;
  2523. struct rspamd_config *cfg = lua_check_config (L, 1);
  2524. const gchar *sym = luaL_checkstring (L, 2);
  2525. if (cfg && sym) {
  2526. rspamd_symcache_enable_symbol_perm (cfg->cache, sym);
  2527. }
  2528. else {
  2529. return luaL_error (L, "invalid arguments");
  2530. }
  2531. return 0;
  2532. }
  2533. static gint
  2534. lua_config_disable_symbol (lua_State *L)
  2535. {
  2536. LUA_TRACE_POINT;
  2537. struct rspamd_config *cfg = lua_check_config (L, 1);
  2538. const gchar *sym = luaL_checkstring (L, 2);
  2539. if (cfg && sym) {
  2540. rspamd_symcache_disable_symbol_perm (cfg->cache, sym);
  2541. }
  2542. else {
  2543. return luaL_error (L, "invalid arguments");
  2544. }
  2545. return 0;
  2546. }
  2547. static gint
  2548. lua_config_register_regexp (lua_State *L)
  2549. {
  2550. LUA_TRACE_POINT;
  2551. struct rspamd_config *cfg = lua_check_config (L, 1);
  2552. struct rspamd_lua_regexp *re = NULL;
  2553. rspamd_regexp_t *cache_re;
  2554. const gchar *type_str = NULL, *header_str = NULL;
  2555. gsize header_len = 0;
  2556. GError *err = NULL;
  2557. enum rspamd_re_type type = RSPAMD_RE_BODY;
  2558. gboolean pcre_only = FALSE;
  2559. guint old_flags;
  2560. /*
  2561. * - `re`* : regular expression object
  2562. * - `type`*: type of regular expression:
  2563. * + `mime`: mime regexp
  2564. * + `rawmime`: raw mime regexp
  2565. * + `header`: header regexp
  2566. * + `rawheader`: raw header expression
  2567. * + `body`: raw body regexp
  2568. * + `url`: url regexp
  2569. * - `header`: for header and rawheader regexp means the name of header
  2570. * - `pcre_only`: allow merely pcre for this regexp
  2571. */
  2572. if (cfg != NULL) {
  2573. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  2574. "*re=U{regexp};*type=S;header=S;pcre_only=B",
  2575. &re, &type_str, &header_str, &pcre_only)) {
  2576. msg_err_config ("cannot get parameters list: %e", err);
  2577. if (err) {
  2578. g_error_free (err);
  2579. }
  2580. }
  2581. else {
  2582. type = rspamd_re_cache_type_from_string (type_str);
  2583. if ((type == RSPAMD_RE_HEADER ||
  2584. type == RSPAMD_RE_RAWHEADER ||
  2585. type == RSPAMD_RE_MIMEHEADER) &&
  2586. header_str == NULL) {
  2587. msg_err_config (
  2588. "header argument is mandatory for header/rawheader regexps");
  2589. }
  2590. else {
  2591. if (pcre_only) {
  2592. old_flags = rspamd_regexp_get_flags (re->re);
  2593. old_flags |= RSPAMD_REGEXP_FLAG_PCRE_ONLY;
  2594. rspamd_regexp_set_flags (re->re, old_flags);
  2595. }
  2596. if (header_str != NULL) {
  2597. /* Include the last \0 */
  2598. header_len = strlen (header_str) + 1;
  2599. }
  2600. cache_re = rspamd_re_cache_add (cfg->re_cache, re->re, type,
  2601. (gpointer) header_str, header_len);
  2602. /*
  2603. * XXX: here are dragons!
  2604. * Actually, lua regexp contains internal rspamd_regexp_t
  2605. * and it owns it.
  2606. * However, after this operation we have some OTHER regexp,
  2607. * which we really would like to use.
  2608. * So we do the following:
  2609. * 1) Remove old re and unref it
  2610. * 2) Replace the internal re with cached one
  2611. * 3) Increase its refcount to share ownership between cache and
  2612. * lua object
  2613. */
  2614. if (cache_re != re->re) {
  2615. rspamd_regexp_unref (re->re);
  2616. re->re = rspamd_regexp_ref (cache_re);
  2617. }
  2618. }
  2619. }
  2620. }
  2621. return 0;
  2622. }
  2623. static gint
  2624. lua_config_replace_regexp (lua_State *L)
  2625. {
  2626. LUA_TRACE_POINT;
  2627. struct rspamd_config *cfg = lua_check_config (L, 1);
  2628. struct rspamd_lua_regexp *old_re = NULL, *new_re = NULL;
  2629. GError *err = NULL;
  2630. if (cfg != NULL) {
  2631. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  2632. "*old_re=U{regexp};*new_re=U{regexp}",
  2633. &old_re, &new_re)) {
  2634. msg_err_config ("cannot get parameters list: %e", err);
  2635. if (err) {
  2636. g_error_free (err);
  2637. }
  2638. }
  2639. else {
  2640. rspamd_re_cache_replace (cfg->re_cache, old_re->re, new_re->re);
  2641. }
  2642. }
  2643. return 0;
  2644. }
  2645. static gint
  2646. lua_config_register_worker_script (lua_State *L)
  2647. {
  2648. LUA_TRACE_POINT;
  2649. struct rspamd_config *cfg = lua_check_config (L, 1);
  2650. const gchar *worker_type = luaL_checkstring (L, 2), *wtype;
  2651. struct rspamd_worker_conf *cf;
  2652. GList *cur;
  2653. struct rspamd_worker_lua_script *sc;
  2654. gboolean found = FALSE;
  2655. if (cfg == NULL || worker_type == NULL || lua_type (L, 3) != LUA_TFUNCTION) {
  2656. return luaL_error (L, "invalid arguments");
  2657. }
  2658. for (cur = g_list_first (cfg->workers); cur != NULL; cur = g_list_next (cur)) {
  2659. cf = cur->data;
  2660. wtype = g_quark_to_string (cf->type);
  2661. if (g_ascii_strcasecmp (wtype, worker_type) == 0) {
  2662. sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc));
  2663. lua_pushvalue (L, 3);
  2664. sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  2665. DL_APPEND (cf->scripts, sc);
  2666. found = TRUE;
  2667. }
  2668. }
  2669. lua_pushboolean (L, found);
  2670. return 1;
  2671. }
  2672. static gint
  2673. lua_config_add_on_load (lua_State *L)
  2674. {
  2675. LUA_TRACE_POINT;
  2676. struct rspamd_config *cfg = lua_check_config (L, 1);
  2677. struct rspamd_config_post_load_script *sc;
  2678. if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) {
  2679. return luaL_error (L, "invalid arguments");
  2680. }
  2681. sc = g_malloc0 (sizeof (*sc));
  2682. lua_pushvalue (L, 2);
  2683. sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  2684. DL_APPEND (cfg->on_load, sc);
  2685. return 0;
  2686. }
  2687. static void lua_periodic_callback_finish (struct thread_entry *thread, int ret);
  2688. static void lua_periodic_callback_error (struct thread_entry *thread, int ret, const char *msg);
  2689. struct rspamd_lua_periodic {
  2690. struct event_base *ev_base;
  2691. struct rspamd_config *cfg;
  2692. lua_State *L;
  2693. gdouble timeout;
  2694. struct event ev;
  2695. gint cbref;
  2696. gboolean need_jitter;
  2697. };
  2698. static void
  2699. lua_periodic_callback (gint unused_fd, short what, gpointer ud)
  2700. {
  2701. struct rspamd_lua_periodic *periodic = ud;
  2702. struct rspamd_config **pcfg, *cfg;
  2703. struct event_base **pev_base;
  2704. struct thread_entry *thread;
  2705. lua_State *L;
  2706. thread = lua_thread_pool_get_for_config (periodic->cfg);
  2707. thread->cd = periodic;
  2708. thread->finish_callback = lua_periodic_callback_finish;
  2709. thread->error_callback = lua_periodic_callback_error;
  2710. L = thread->lua_state;
  2711. lua_rawgeti (L, LUA_REGISTRYINDEX, periodic->cbref);
  2712. pcfg = lua_newuserdata (L, sizeof (*pcfg));
  2713. rspamd_lua_setclass (L, "rspamd{config}", -1);
  2714. cfg = periodic->cfg;
  2715. *pcfg = cfg;
  2716. pev_base = lua_newuserdata (L, sizeof (*pev_base));
  2717. rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
  2718. *pev_base = periodic->ev_base;
  2719. event_del (&periodic->ev);
  2720. lua_thread_call (thread, 2);
  2721. }
  2722. static void
  2723. lua_periodic_callback_finish (struct thread_entry *thread, int ret)
  2724. {
  2725. lua_State *L;
  2726. struct rspamd_lua_periodic *periodic = thread->cd;
  2727. gboolean plan_more = FALSE;
  2728. struct timeval tv;
  2729. gdouble timeout = 0.0;
  2730. L = thread->lua_state;
  2731. #ifdef HAVE_EVENT_NO_CACHE_TIME_FUNC
  2732. event_base_update_cache_time (periodic->ev_base);
  2733. #endif
  2734. if (ret == 0) {
  2735. if (lua_type (L, -1) == LUA_TBOOLEAN) {
  2736. plan_more = lua_toboolean (L, -1);
  2737. timeout = periodic->timeout;
  2738. }
  2739. else if (lua_type (L, -1) == LUA_TNUMBER) {
  2740. timeout = lua_tonumber (L, -1);
  2741. plan_more = timeout > 0 ? TRUE : FALSE;
  2742. }
  2743. lua_pop (L, 1); /* Return value */
  2744. }
  2745. if (plan_more) {
  2746. if (periodic->need_jitter) {
  2747. timeout = rspamd_time_jitter (timeout, 0.0);
  2748. }
  2749. double_to_tv (timeout, &tv);
  2750. event_add (&periodic->ev, &tv);
  2751. }
  2752. else {
  2753. luaL_unref (L, LUA_REGISTRYINDEX, periodic->cbref);
  2754. g_free (periodic);
  2755. }
  2756. }
  2757. static void
  2758. lua_periodic_callback_error (struct thread_entry *thread, int ret, const char *msg)
  2759. {
  2760. struct rspamd_config *cfg;
  2761. struct rspamd_lua_periodic *periodic = thread->cd;
  2762. cfg = periodic->cfg;
  2763. msg_err_config ("call to finishing script failed: %s", msg);
  2764. lua_periodic_callback_finish(thread, ret);
  2765. }
  2766. static gint
  2767. lua_config_add_periodic (lua_State *L)
  2768. {
  2769. LUA_TRACE_POINT;
  2770. struct rspamd_config *cfg = lua_check_config (L, 1);
  2771. struct event_base *ev_base = lua_check_ev_base (L, 2);
  2772. gdouble timeout = lua_tonumber (L, 3);
  2773. struct timeval tv;
  2774. struct rspamd_lua_periodic *periodic;
  2775. gboolean need_jitter = FALSE;
  2776. if (cfg == NULL || timeout < 0 || lua_type (L, 4) != LUA_TFUNCTION) {
  2777. return luaL_error (L, "invalid arguments");
  2778. }
  2779. if (lua_type (L, 5) == LUA_TBOOLEAN) {
  2780. need_jitter = lua_toboolean (L, 5);
  2781. }
  2782. periodic = g_malloc0 (sizeof (*periodic));
  2783. periodic->timeout = timeout;
  2784. periodic->L = L;
  2785. periodic->cfg = cfg;
  2786. periodic->ev_base = ev_base;
  2787. periodic->need_jitter = need_jitter;
  2788. lua_pushvalue (L, 4);
  2789. periodic->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  2790. event_set (&periodic->ev, -1, EV_TIMEOUT, lua_periodic_callback, periodic);
  2791. event_base_set (ev_base, &periodic->ev);
  2792. if (need_jitter) {
  2793. timeout = rspamd_time_jitter (timeout, 0.0);
  2794. }
  2795. double_to_tv (timeout, &tv);
  2796. event_add (&periodic->ev, &tv);
  2797. return 0;
  2798. }
  2799. static gint
  2800. lua_config_get_symbols_count (lua_State *L)
  2801. {
  2802. LUA_TRACE_POINT;
  2803. struct rspamd_config *cfg = lua_check_config (L, 1);
  2804. guint res = 0;
  2805. if (cfg != NULL) {
  2806. res = rspamd_symcache_stats_symbols_count (cfg->cache);
  2807. }
  2808. else {
  2809. return luaL_error (L, "invalid arguments");
  2810. }
  2811. lua_pushinteger (L, res);
  2812. return 1;
  2813. }
  2814. static gint
  2815. lua_config_get_symbols_cksum (lua_State *L)
  2816. {
  2817. LUA_TRACE_POINT;
  2818. struct rspamd_config *cfg = lua_check_config (L, 1);
  2819. guint64 res = 0, *pres;
  2820. if (cfg != NULL) {
  2821. res = rspamd_symcache_get_cksum (cfg->cache);
  2822. }
  2823. else {
  2824. return luaL_error (L, "invalid arguments");
  2825. }
  2826. pres = lua_newuserdata (L, sizeof (res));
  2827. *pres = res;
  2828. rspamd_lua_setclass (L, "rspamd{int64}", -1);
  2829. return 1;
  2830. }
  2831. static gint
  2832. lua_config_get_symbols_counters (lua_State *L)
  2833. {
  2834. LUA_TRACE_POINT;
  2835. struct rspamd_config *cfg = lua_check_config (L, 1);
  2836. ucl_object_t *counters;
  2837. if (cfg != NULL) {
  2838. counters = rspamd_symcache_counters (cfg->cache);
  2839. ucl_object_push_lua (L, counters, true);
  2840. ucl_object_unref (counters);
  2841. }
  2842. else {
  2843. return luaL_error (L, "invalid arguments");
  2844. }
  2845. return 1;
  2846. }
  2847. static void
  2848. lua_metric_symbol_inserter (gpointer k, gpointer v, gpointer ud)
  2849. {
  2850. lua_State *L = (lua_State *) ud;
  2851. const gchar *sym = k;
  2852. struct rspamd_symbol *s = (struct rspamd_symbol *) v;
  2853. lua_pushstring (L, sym);
  2854. lua_createtable (L, 0, 3); /* TODO: add more if needed */
  2855. lua_pushstring (L, "score");
  2856. lua_pushnumber (L, s->score);
  2857. lua_settable (L, -3);
  2858. lua_pushstring (L, "description");
  2859. lua_pushstring (L, s->description);
  2860. lua_settable (L, -3);
  2861. lua_settable (L, -3);
  2862. }
  2863. static gint
  2864. lua_config_get_symbols_scores (lua_State *L)
  2865. {
  2866. LUA_TRACE_POINT;
  2867. struct rspamd_config *cfg = lua_check_config (L, 1);
  2868. if (cfg != NULL) {
  2869. lua_createtable (L, 0, g_hash_table_size (cfg->symbols));
  2870. g_hash_table_foreach (cfg->symbols,
  2871. lua_metric_symbol_inserter,
  2872. L);
  2873. }
  2874. else {
  2875. return luaL_error (L, "invalid arguments");
  2876. }
  2877. return 1;
  2878. }
  2879. static gint
  2880. lua_config_get_symbol_callback (lua_State *L)
  2881. {
  2882. LUA_TRACE_POINT;
  2883. struct rspamd_config *cfg = lua_check_config (L, 1);
  2884. const gchar *sym = luaL_checkstring (L, 2);
  2885. struct rspamd_abstract_callback_data *abs_cbdata;
  2886. struct lua_callback_data *cbd;
  2887. if (cfg != NULL && sym != NULL) {
  2888. abs_cbdata = rspamd_symcache_get_cbdata (cfg->cache, sym);
  2889. if (abs_cbdata == NULL || abs_cbdata->magic != rspamd_lua_callback_magic) {
  2890. lua_pushnil (L);
  2891. }
  2892. else {
  2893. cbd = (struct lua_callback_data *)abs_cbdata;
  2894. if (cbd->cb_is_ref) {
  2895. lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->callback.ref);
  2896. }
  2897. else {
  2898. lua_getglobal (L, cbd->callback.name);
  2899. }
  2900. }
  2901. }
  2902. else {
  2903. return luaL_error (L, "invalid arguments");
  2904. }
  2905. return 1;
  2906. }
  2907. static gint
  2908. lua_config_set_symbol_callback (lua_State *L)
  2909. {
  2910. LUA_TRACE_POINT;
  2911. struct rspamd_config *cfg = lua_check_config (L, 1);
  2912. const gchar *sym = luaL_checkstring (L, 2);
  2913. struct rspamd_abstract_callback_data *abs_cbdata;
  2914. struct lua_callback_data *cbd;
  2915. if (cfg != NULL && sym != NULL && lua_type (L, 3) == LUA_TFUNCTION) {
  2916. abs_cbdata = rspamd_symcache_get_cbdata (cfg->cache, sym);
  2917. if (abs_cbdata == NULL || abs_cbdata->magic != rspamd_lua_callback_magic) {
  2918. lua_pushboolean (L, FALSE);
  2919. }
  2920. else {
  2921. cbd = (struct lua_callback_data *)abs_cbdata;
  2922. if (cbd->cb_is_ref) {
  2923. luaL_unref (L, LUA_REGISTRYINDEX, cbd->callback.ref);
  2924. }
  2925. else {
  2926. cbd->cb_is_ref = TRUE;
  2927. }
  2928. lua_pushvalue (L, 3);
  2929. cbd->callback.ref = luaL_ref (L, LUA_REGISTRYINDEX);
  2930. lua_pushboolean (L, TRUE);
  2931. }
  2932. }
  2933. else {
  2934. return luaL_error (L, "invalid arguments");
  2935. }
  2936. return 1;
  2937. }
  2938. static gint
  2939. lua_config_get_symbol_stat (lua_State *L)
  2940. {
  2941. LUA_TRACE_POINT;
  2942. struct rspamd_config *cfg = lua_check_config (L, 1);
  2943. const gchar *sym = luaL_checkstring (L, 2);
  2944. gdouble freq, stddev, tm;
  2945. guint hits;
  2946. if (cfg != NULL && sym != NULL) {
  2947. if (!rspamd_symcache_stat_symbol (cfg->cache, sym, &freq,
  2948. &stddev, &tm, &hits)) {
  2949. lua_pushnil (L);
  2950. }
  2951. else {
  2952. lua_createtable (L, 0, 4);
  2953. lua_pushstring (L, "frequency");
  2954. lua_pushnumber (L, freq);
  2955. lua_settable (L, -3);
  2956. lua_pushstring (L, "sttdev");
  2957. lua_pushnumber (L, stddev);
  2958. lua_settable (L, -3);
  2959. lua_pushstring (L, "time");
  2960. lua_pushnumber (L, tm);
  2961. lua_settable (L, -3);
  2962. lua_pushstring (L, "hits");
  2963. lua_pushinteger (L, hits);
  2964. lua_settable (L, -3);
  2965. }
  2966. }
  2967. else {
  2968. return luaL_error (L, "invalid arguments");
  2969. }
  2970. return 1;
  2971. }
  2972. static gint
  2973. lua_config_get_symbol_parent (lua_State *L)
  2974. {
  2975. LUA_TRACE_POINT;
  2976. struct rspamd_config *cfg = lua_check_config (L, 1);
  2977. const gchar *sym = luaL_checkstring (L, 2), *parent;
  2978. if (cfg != NULL && sym != NULL) {
  2979. parent = rspamd_symcache_get_parent (cfg->cache, sym);
  2980. if (parent) {
  2981. lua_pushstring (L, parent);
  2982. }
  2983. else {
  2984. lua_pushnil (L);
  2985. }
  2986. }
  2987. else {
  2988. return luaL_error (L, "invalid arguments");
  2989. }
  2990. return 1;
  2991. }
  2992. static gint
  2993. lua_config_register_finish_script (lua_State *L)
  2994. {
  2995. LUA_TRACE_POINT;
  2996. struct rspamd_config *cfg = lua_check_config (L, 1);
  2997. struct rspamd_config_post_load_script *sc;
  2998. if (cfg != NULL && lua_type (L, 2) == LUA_TFUNCTION) {
  2999. sc = g_malloc0 (sizeof (*sc));
  3000. lua_pushvalue (L, 2);
  3001. sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  3002. DL_APPEND (cfg->finish_callbacks, sc);
  3003. }
  3004. else {
  3005. return luaL_error (L, "invalid arguments");
  3006. }
  3007. return 0;
  3008. }
  3009. static gint
  3010. lua_config_register_monitored (lua_State *L)
  3011. {
  3012. LUA_TRACE_POINT;
  3013. struct rspamd_config *cfg = lua_check_config (L, 1);
  3014. struct rspamd_monitored *m, **pm;
  3015. const gchar *url, *type;
  3016. ucl_object_t *params = NULL;
  3017. url = lua_tostring (L, 2);
  3018. type = lua_tostring (L, 3);
  3019. if (cfg != NULL && url != NULL && type != NULL) {
  3020. if (g_ascii_strcasecmp (type, "dns") == 0) {
  3021. lua_Debug ar;
  3022. if (lua_type (L, 4) == LUA_TTABLE) {
  3023. params = ucl_object_lua_import (L, 4);
  3024. }
  3025. /* Get lua line and source */
  3026. lua_getstack (L, 1, &ar);
  3027. lua_getinfo (L, "nSl", &ar);
  3028. m = rspamd_monitored_create_ (cfg->monitored_ctx, url,
  3029. RSPAMD_MONITORED_DNS, RSPAMD_MONITORED_DEFAULT,
  3030. params, ar.short_src);
  3031. if (m) {
  3032. pm = lua_newuserdata (L, sizeof (*pm));
  3033. *pm = m;
  3034. rspamd_lua_setclass (L, "rspamd{monitored}", -1);
  3035. }
  3036. else {
  3037. lua_pushnil (L);
  3038. }
  3039. if (params) {
  3040. ucl_object_unref (params);
  3041. }
  3042. }
  3043. else {
  3044. return luaL_error (L, "invalid monitored type: %s", type);
  3045. }
  3046. }
  3047. else {
  3048. return luaL_error (L, "invalid arguments");
  3049. }
  3050. return 1;
  3051. }
  3052. static gint
  3053. lua_config_add_doc (lua_State *L)
  3054. {
  3055. LUA_TRACE_POINT;
  3056. struct rspamd_config *cfg;
  3057. const gchar *path = NULL, *option, *doc_string;
  3058. const gchar *type_str = NULL, *default_value = NULL;
  3059. ucl_type_t type = UCL_NULL;
  3060. gboolean required = FALSE;
  3061. GError *err = NULL;
  3062. cfg = lua_check_config (L, 1);
  3063. if (lua_type (L, 2 ) == LUA_TSTRING) {
  3064. path = luaL_checkstring (L, 2);
  3065. }
  3066. option = luaL_checkstring (L, 3);
  3067. doc_string = luaL_checkstring (L, 4);
  3068. if (cfg && option && doc_string) {
  3069. if (lua_type (L, 5) == LUA_TTABLE) {
  3070. if (!rspamd_lua_parse_table_arguments (L, 5, &err,
  3071. "type=S;default=S;required=B",
  3072. &type_str, &default_value, &required)) {
  3073. msg_err_config ("cannot get parameters list: %e", err);
  3074. if (err) {
  3075. g_error_free (err);
  3076. }
  3077. if (type_str) {
  3078. if (!ucl_object_string_to_type (type_str, &type)) {
  3079. msg_err_config ("invalid type: %s", type_str);
  3080. }
  3081. }
  3082. }
  3083. }
  3084. rspamd_rcl_add_doc_by_path (cfg, path, doc_string, option,
  3085. type, NULL, 0, default_value, required);
  3086. }
  3087. else {
  3088. return luaL_error (L, "invalid arguments");
  3089. }
  3090. return 0;
  3091. }
  3092. static gint
  3093. lua_config_add_example (lua_State *L)
  3094. {
  3095. LUA_TRACE_POINT;
  3096. struct rspamd_config *cfg;
  3097. const gchar *path = NULL, *option, *doc_string, *example;
  3098. gsize example_len;
  3099. cfg = lua_check_config (L, 1);
  3100. if (lua_type (L, 2 ) == LUA_TSTRING) {
  3101. path = luaL_checkstring (L, 2);
  3102. }
  3103. option = luaL_checkstring (L, 3);
  3104. doc_string = luaL_checkstring (L, 4);
  3105. example = luaL_checklstring (L, 5, &example_len);
  3106. if (cfg && option && doc_string && example) {
  3107. rspamd_rcl_add_doc_by_example (cfg, path, doc_string, option,
  3108. example, example_len);
  3109. }
  3110. else {
  3111. return luaL_error (L, "invalid arguments");
  3112. }
  3113. return 0;
  3114. }
  3115. static gint
  3116. lua_config_get_cpu_flags (lua_State *L)
  3117. {
  3118. LUA_TRACE_POINT;
  3119. struct rspamd_config *cfg = lua_check_config (L, 1);
  3120. struct rspamd_cryptobox_library_ctx *crypto_ctx;
  3121. if (cfg != NULL) {
  3122. crypto_ctx = cfg->libs_ctx->crypto_ctx;
  3123. lua_newtable (L);
  3124. if (crypto_ctx->cpu_config & CPUID_SSSE3) {
  3125. lua_pushstring (L, "ssse3");
  3126. lua_pushboolean (L, true);
  3127. lua_settable (L, -3);
  3128. }
  3129. if (crypto_ctx->cpu_config & CPUID_SSE41) {
  3130. lua_pushstring (L, "sse41");
  3131. lua_pushboolean (L, true);
  3132. lua_settable (L, -3);
  3133. }
  3134. if (crypto_ctx->cpu_config & CPUID_SSE42) {
  3135. lua_pushstring (L, "sse42");
  3136. lua_pushboolean (L, true);
  3137. lua_settable (L, -3);
  3138. }
  3139. if (crypto_ctx->cpu_config & CPUID_SSE2) {
  3140. lua_pushstring (L, "sse2");
  3141. lua_pushboolean (L, true);
  3142. lua_settable (L, -3);
  3143. }
  3144. if (crypto_ctx->cpu_config & CPUID_SSE3) {
  3145. lua_pushstring (L, "sse3");
  3146. lua_pushboolean (L, true);
  3147. lua_settable (L, -3);
  3148. }
  3149. if (crypto_ctx->cpu_config & CPUID_AVX) {
  3150. lua_pushstring (L, "avx");
  3151. lua_pushboolean (L, true);
  3152. lua_settable (L, -3);
  3153. }
  3154. if (crypto_ctx->cpu_config & CPUID_AVX2) {
  3155. lua_pushstring (L, "avx2");
  3156. lua_pushboolean (L, true);
  3157. lua_settable (L, -3);
  3158. }
  3159. }
  3160. else {
  3161. return luaL_error (L, "invalid arguments");
  3162. }
  3163. return 1;
  3164. }
  3165. static gint
  3166. lua_config_has_torch (lua_State *L)
  3167. {
  3168. LUA_TRACE_POINT;
  3169. struct rspamd_config *cfg = lua_check_config (L, 1);
  3170. struct rspamd_cryptobox_library_ctx *crypto_ctx;
  3171. if (cfg != NULL) {
  3172. crypto_ctx = cfg->libs_ctx->crypto_ctx;
  3173. #if !defined(WITH_TORCH) || !defined(WITH_LUAJIT)
  3174. lua_pushboolean (L, false);
  3175. (void)crypto_ctx;
  3176. #else
  3177. if (crypto_ctx->cpu_config & CPUID_SSE2) {
  3178. lua_pushboolean (L, true);
  3179. }
  3180. else {
  3181. lua_pushboolean (L, false);
  3182. }
  3183. #endif
  3184. }
  3185. else {
  3186. return luaL_error (L, "invalid arguments");
  3187. }
  3188. return 1;
  3189. }
  3190. static gint
  3191. lua_config_experimental_enabled (lua_State *L)
  3192. {
  3193. LUA_TRACE_POINT;
  3194. struct rspamd_config *cfg = lua_check_config (L, 1);
  3195. if (cfg != NULL) {
  3196. lua_pushboolean (L, cfg->enable_experimental);
  3197. }
  3198. else {
  3199. return luaL_error (L, "invalid arguments");
  3200. }
  3201. return 1;
  3202. }
  3203. struct rspamd_lua_include_trace_cbdata {
  3204. lua_State *L;
  3205. gint cbref;
  3206. };
  3207. static void
  3208. lua_include_trace_cb (struct ucl_parser *parser,
  3209. const ucl_object_t *parent,
  3210. const ucl_object_t *args,
  3211. const char *path,
  3212. size_t pathlen,
  3213. void *user_data)
  3214. {
  3215. struct rspamd_lua_include_trace_cbdata *cbdata =
  3216. (struct rspamd_lua_include_trace_cbdata *)user_data;
  3217. gint err_idx;
  3218. GString *tb;
  3219. lua_State *L;
  3220. L = cbdata->L;
  3221. lua_pushcfunction (L, &rspamd_lua_traceback);
  3222. err_idx = lua_gettop (L);
  3223. lua_rawgeti (L, LUA_REGISTRYINDEX, cbdata->cbref);
  3224. /* Current filename */
  3225. lua_pushstring (L, ucl_parser_get_cur_file (parser));
  3226. /* Included filename */
  3227. lua_pushlstring (L, path, pathlen);
  3228. /* Params */
  3229. if (args) {
  3230. ucl_object_push_lua (L, args, true);
  3231. }
  3232. else {
  3233. lua_newtable (L);
  3234. }
  3235. /* Parent */
  3236. if (parent) {
  3237. lua_pushstring (L, ucl_object_key (parent));
  3238. }
  3239. else {
  3240. lua_pushnil (L);
  3241. }
  3242. if (lua_pcall (L, 4, 0, err_idx) != 0) {
  3243. tb = lua_touserdata (L, -1);
  3244. msg_err ("lua call to local include trace failed: %v", tb);
  3245. if (tb) {
  3246. g_string_free (tb, TRUE);
  3247. }
  3248. }
  3249. lua_settop (L, err_idx - 1);
  3250. }
  3251. #define LUA_TABLE_TO_HASH(htb, idx) do { \
  3252. lua_pushstring (L, (idx)); \
  3253. lua_gettable (L, -2); \
  3254. if (lua_isstring (L, -1)) { \
  3255. g_hash_table_insert ((htb), (idx), g_strdup (lua_tostring (L, -1))); \
  3256. } \
  3257. lua_pop (L, 1); \
  3258. } while(0)
  3259. static gint
  3260. lua_config_load_ucl (lua_State *L)
  3261. {
  3262. struct rspamd_config *cfg = lua_check_config (L, 1);
  3263. const gchar *filename;
  3264. GHashTable *paths = g_hash_table_new_full (rspamd_str_hash, rspamd_str_equal,
  3265. NULL, g_free);
  3266. GError *err = NULL;
  3267. if (cfg) {
  3268. if (lua_isstring (L, 2)) {
  3269. filename = lua_tostring (L, 2);
  3270. }
  3271. else {
  3272. filename = RSPAMD_CONFDIR "/rspamd.conf";
  3273. }
  3274. /* Convert rspamd_paths */
  3275. lua_getglobal (L, "rspamd_paths");
  3276. if (lua_istable (L, -1)) {
  3277. LUA_TABLE_TO_HASH(paths, RSPAMD_CONFDIR_INDEX);
  3278. LUA_TABLE_TO_HASH(paths, RSPAMD_LOCAL_CONFDIR_INDEX);
  3279. LUA_TABLE_TO_HASH(paths, RSPAMD_RUNDIR_INDEX);
  3280. LUA_TABLE_TO_HASH(paths, RSPAMD_DBDIR_INDEX);
  3281. LUA_TABLE_TO_HASH(paths, RSPAMD_LOGDIR_INDEX);
  3282. LUA_TABLE_TO_HASH(paths, RSPAMD_WWWDIR_INDEX);
  3283. LUA_TABLE_TO_HASH(paths, RSPAMD_PLUGINSDIR_INDEX);
  3284. LUA_TABLE_TO_HASH(paths, RSPAMD_RULESDIR_INDEX);
  3285. LUA_TABLE_TO_HASH(paths, RSPAMD_LUALIBDIR_INDEX);
  3286. LUA_TABLE_TO_HASH(paths, RSPAMD_PREFIX_INDEX);
  3287. }
  3288. lua_pop (L, 1);
  3289. if (lua_isfunction (L, 3)) {
  3290. struct rspamd_lua_include_trace_cbdata cbd;
  3291. lua_pushvalue (L, 3);
  3292. cbd.cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  3293. cbd.L = L;
  3294. if (!rspamd_config_parse_ucl (cfg, filename, paths,
  3295. lua_include_trace_cb, &cbd, lua_toboolean (L, 4), &err)) {
  3296. luaL_unref (L, LUA_REGISTRYINDEX, cbd.cbref);
  3297. lua_pushboolean (L, false);
  3298. lua_pushfstring (L, "failed to load config: %s", err->message);
  3299. g_error_free (err);
  3300. g_hash_table_unref (paths);
  3301. return 2;
  3302. }
  3303. luaL_unref (L, LUA_REGISTRYINDEX, cbd.cbref);
  3304. }
  3305. else {
  3306. if (!rspamd_config_parse_ucl (cfg, filename, paths, NULL, NULL,
  3307. lua_toboolean (L, 3), &err)) {
  3308. lua_pushboolean (L, false);
  3309. lua_pushfstring (L, "failed to load config: %s", err->message);
  3310. g_error_free (err);
  3311. g_hash_table_unref (paths);
  3312. return 2;
  3313. }
  3314. }
  3315. rspamd_rcl_maybe_apply_lua_transform (cfg);
  3316. rspamd_config_calculate_cksum (cfg);
  3317. }
  3318. else {
  3319. return luaL_error (L, "invalid arguments");
  3320. }
  3321. g_hash_table_unref (paths);
  3322. lua_pushboolean (L, true);
  3323. return 1;
  3324. }
  3325. #undef IDX_TO_HASH
  3326. static gint
  3327. lua_config_parse_rcl (lua_State *L)
  3328. {
  3329. LUA_TRACE_POINT;
  3330. struct rspamd_config *cfg = lua_check_config (L, 1);
  3331. GHashTable *excluded = g_hash_table_new_full (rspamd_str_hash, rspamd_str_equal,
  3332. g_free, NULL);
  3333. GError *err = NULL;
  3334. struct rspamd_rcl_section *top;
  3335. if (cfg) {
  3336. if (lua_istable (L, 2)) {
  3337. lua_pushvalue (L, 2);
  3338. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  3339. g_hash_table_insert (excluded, g_strdup (lua_tostring (L, -1)),
  3340. GINT_TO_POINTER (-1));
  3341. }
  3342. lua_pop (L, 1);
  3343. }
  3344. top = rspamd_rcl_config_init (cfg, excluded);
  3345. if (!rspamd_rcl_parse (top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) {
  3346. lua_pushboolean (L, false);
  3347. lua_pushfstring (L, "failed to load config: %s", err->message);
  3348. g_error_free (err);
  3349. g_hash_table_unref (excluded);
  3350. rspamd_rcl_section_free (top);
  3351. return 2;
  3352. }
  3353. }
  3354. else {
  3355. return luaL_error (L, "invalid arguments");
  3356. }
  3357. g_hash_table_unref (excluded);
  3358. rspamd_rcl_section_free (top);
  3359. lua_pushboolean (L, true);
  3360. return 1;
  3361. }
  3362. static gint
  3363. lua_config_init_modules (lua_State *L)
  3364. {
  3365. LUA_TRACE_POINT;
  3366. struct rspamd_config *cfg = lua_check_config (L, 1);
  3367. if (cfg != NULL) {
  3368. rspamd_lua_post_load_config (cfg);
  3369. lua_pushboolean (L, rspamd_init_filters (cfg, FALSE));
  3370. }
  3371. else {
  3372. return luaL_error (L, "invalid arguments");
  3373. }
  3374. return 1;
  3375. }
  3376. static gint
  3377. lua_config_init_subsystem (lua_State *L)
  3378. {
  3379. LUA_TRACE_POINT;
  3380. struct rspamd_config *cfg = lua_check_config (L, 1);
  3381. const gchar *subsystem = luaL_checkstring (L, 2);
  3382. gchar **parts;
  3383. guint nparts, i;
  3384. if (cfg != NULL && subsystem != NULL) {
  3385. parts = g_strsplit_set (subsystem, ";,", -1);
  3386. nparts = g_strv_length (parts);
  3387. for (i = 0; i < nparts; i ++) {
  3388. if (strcmp (parts[i], "filters") == 0) {
  3389. rspamd_lua_post_load_config (cfg);
  3390. rspamd_init_filters (cfg, FALSE);
  3391. }
  3392. else if (strcmp (parts[i], "langdet") == 0) {
  3393. cfg->lang_det = rspamd_language_detector_init (cfg);
  3394. }
  3395. else if (strcmp (parts[i], "stat") == 0) {
  3396. rspamd_stat_init (cfg, NULL);
  3397. }
  3398. else if (strcmp (parts[i], "dns") == 0) {
  3399. struct event_base *ev_base = lua_check_ev_base (L, 3);
  3400. if (ev_base) {
  3401. cfg->dns_resolver = rspamd_dns_resolver_init (rspamd_logger_get_singleton (),
  3402. ev_base,
  3403. cfg);
  3404. }
  3405. else {
  3406. g_strfreev (parts);
  3407. return luaL_error (L, "no event base specified");
  3408. }
  3409. }
  3410. else {
  3411. g_strfreev (parts);
  3412. return luaL_error (L, "invalid param: %s", parts[i]);
  3413. }
  3414. }
  3415. g_strfreev (parts);
  3416. }
  3417. else {
  3418. return luaL_error (L, "invalid arguments");
  3419. }
  3420. return 0;
  3421. }
  3422. static gint
  3423. lua_config_register_re_selector (lua_State *L)
  3424. {
  3425. LUA_TRACE_POINT;
  3426. struct rspamd_config *cfg = lua_check_config (L, 1);
  3427. const gchar *name = luaL_checkstring (L, 2);
  3428. const gchar *selector_str = luaL_checkstring (L, 3);
  3429. const gchar *delimiter = "";
  3430. gint top = lua_gettop (L);
  3431. bool res = false;
  3432. if (cfg && name && selector_str) {
  3433. if (lua_gettop (L) >= 4) {
  3434. delimiter = luaL_checkstring (L, 4);
  3435. }
  3436. if (luaL_dostring (L, "return require \"lua_selectors\"") != 0) {
  3437. msg_warn_config ("cannot require lua_selectors: %s",
  3438. lua_tostring (L, -1));
  3439. }
  3440. else {
  3441. if (lua_type (L, -1) != LUA_TTABLE) {
  3442. msg_warn_config ("lua selectors must return "
  3443. "table and not %s",
  3444. lua_typename (L, lua_type (L, -1)));
  3445. }
  3446. else {
  3447. lua_pushstring (L, "create_selector_closure");
  3448. lua_gettable (L, -2);
  3449. if (lua_type (L, -1) != LUA_TFUNCTION) {
  3450. msg_warn_config ("create_selector_closure must return "
  3451. "function and not %s",
  3452. lua_typename (L, lua_type (L, -1)));
  3453. }
  3454. else {
  3455. gint err_idx, ret;
  3456. GString *tb;
  3457. struct rspamd_config **pcfg;
  3458. lua_pushcfunction (L, &rspamd_lua_traceback);
  3459. err_idx = lua_gettop (L);
  3460. /* Push function */
  3461. lua_pushvalue (L, -2);
  3462. pcfg = lua_newuserdata (L, sizeof (*pcfg));
  3463. rspamd_lua_setclass (L, "rspamd{config}", -1);
  3464. *pcfg = cfg;
  3465. lua_pushstring (L, selector_str);
  3466. lua_pushstring (L, delimiter);
  3467. if ((ret = lua_pcall (L, 3, 1, err_idx)) != 0) {
  3468. tb = lua_touserdata (L, -1);
  3469. msg_err_config ("call to create_selector_closure lua "
  3470. "script failed (%d): %v", ret, tb);
  3471. if (tb) {
  3472. g_string_free (tb, TRUE);
  3473. }
  3474. }
  3475. else {
  3476. if (lua_type (L, -1) != LUA_TFUNCTION) {
  3477. msg_warn_config ("create_selector_closure "
  3478. "invocation must return "
  3479. "function and not %s",
  3480. lua_typename (L, lua_type (L, -1)));
  3481. }
  3482. else {
  3483. ret = luaL_ref (L, LUA_REGISTRYINDEX);
  3484. rspamd_re_cache_add_selector (cfg->re_cache,
  3485. name, ret);
  3486. res = true;
  3487. }
  3488. }
  3489. }
  3490. }
  3491. }
  3492. }
  3493. else {
  3494. return luaL_error (L, "invalid arguments");
  3495. }
  3496. lua_settop (L, top);
  3497. lua_pushboolean (L, res);
  3498. if (res) {
  3499. msg_info_config ("registered regexp selector %s", name);
  3500. }
  3501. return 1;
  3502. }
  3503. static gint
  3504. lua_config_get_tld_path (lua_State *L)
  3505. {
  3506. LUA_TRACE_POINT;
  3507. struct rspamd_config *cfg = lua_check_config (L, 1);
  3508. if (cfg != NULL) {
  3509. lua_pushstring (L, cfg->tld_file);
  3510. }
  3511. else {
  3512. return luaL_error (L, "invalid arguments");
  3513. }
  3514. return 1;
  3515. }
  3516. static gint
  3517. lua_monitored_alive (lua_State *L)
  3518. {
  3519. LUA_TRACE_POINT;
  3520. struct rspamd_monitored *m = lua_check_monitored (L, 1);
  3521. if (m) {
  3522. lua_pushboolean (L, rspamd_monitored_alive (m));
  3523. }
  3524. else {
  3525. return luaL_error (L, "invalid arguments");
  3526. }
  3527. return 1;
  3528. }
  3529. static gint
  3530. lua_monitored_offline (lua_State *L)
  3531. {
  3532. LUA_TRACE_POINT;
  3533. struct rspamd_monitored *m = lua_check_monitored (L, 1);
  3534. if (m) {
  3535. lua_pushnumber (L, rspamd_monitored_offline_time (m));
  3536. }
  3537. else {
  3538. return luaL_error (L, "invalid arguments");
  3539. }
  3540. return 1;
  3541. }
  3542. static gint
  3543. lua_monitored_total_offline (lua_State *L)
  3544. {
  3545. LUA_TRACE_POINT;
  3546. struct rspamd_monitored *m = lua_check_monitored (L, 1);
  3547. if (m) {
  3548. lua_pushnumber (L, rspamd_monitored_total_offline_time (m));
  3549. }
  3550. else {
  3551. return luaL_error (L, "invalid arguments");
  3552. }
  3553. return 1;
  3554. }
  3555. static gint
  3556. lua_monitored_latency (lua_State *L)
  3557. {
  3558. LUA_TRACE_POINT;
  3559. struct rspamd_monitored *m = lua_check_monitored (L, 1);
  3560. if (m) {
  3561. lua_pushnumber (L, rspamd_monitored_latency (m));
  3562. }
  3563. else {
  3564. return luaL_error (L, "invalid arguments");
  3565. }
  3566. return 1;
  3567. }
  3568. void
  3569. luaopen_config (lua_State * L)
  3570. {
  3571. rspamd_lua_new_class (L, "rspamd{config}", configlib_m);
  3572. lua_pop (L, 1);
  3573. rspamd_lua_new_class (L, "rspamd{monitored}", monitoredlib_m);
  3574. lua_pop (L, 1);
  3575. }
  3576. void
  3577. lua_call_finish_script (struct rspamd_config_post_load_script *sc,
  3578. struct rspamd_task *task) {
  3579. struct rspamd_task **ptask;
  3580. struct thread_entry *thread;
  3581. thread = lua_thread_pool_get_for_task (task);
  3582. thread->task = task;
  3583. lua_State *L = thread->lua_state;
  3584. lua_rawgeti (L, LUA_REGISTRYINDEX, sc->cbref);
  3585. ptask = lua_newuserdata (L, sizeof (struct rspamd_task *));
  3586. rspamd_lua_setclass (L, "rspamd{task}", - 1);
  3587. *ptask = task;
  3588. lua_thread_call (thread, 1);
  3589. }