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_util.c 81KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "lua_common.h"
  17. #include "unix-std.h"
  18. #include "lua_compress.h"
  19. #include "libmime/email_addr.h"
  20. #include "libmime/content_type.h"
  21. #include "libmime/mime_headers.h"
  22. #include "libutil/hash.h"
  23. #include "lua_parsers.h"
  24. #ifdef WITH_LUA_REPL
  25. #include "replxx.h"
  26. #endif
  27. #include <math.h>
  28. #include <glob.h>
  29. #include "unicode/uspoof.h"
  30. #include "unicode/uscript.h"
  31. #include "contrib/fastutf8/fastutf8.h"
  32. /***
  33. * @module rspamd_util
  34. * This module contains some generic purpose utilities that could be useful for
  35. * testing and production rules.
  36. */
  37. /***
  38. * @function util.create_event_base()
  39. * Creates new event base for processing asynchronous events
  40. * @return {ev_base} new event processing base
  41. */
  42. LUA_FUNCTION_DEF(util, create_event_base);
  43. /***
  44. * @function util.load_rspamd_config(filename)
  45. * Load rspamd config from the specified file
  46. * @return {confg} new configuration object suitable for access
  47. */
  48. LUA_FUNCTION_DEF(util, load_rspamd_config);
  49. /***
  50. * @function util.config_from_ucl(any, string)
  51. * Load rspamd config from ucl represented by any lua table
  52. * @return {confg} new configuration object suitable for access
  53. */
  54. LUA_FUNCTION_DEF(util, config_from_ucl);
  55. /***
  56. * @function util.encode_base64(input[, str_len, [newlines_type]])
  57. * Encodes data in base64 breaking lines if needed
  58. * @param {text or string} input input data
  59. * @param {number} str_len optional size of lines or 0 if split is not needed
  60. * @return {rspamd_text} encoded data chunk
  61. */
  62. LUA_FUNCTION_DEF(util, encode_base64);
  63. /***
  64. * @function util.encode_qp(input[, str_len, [newlines_type]])
  65. * Encodes data in quoted printable breaking lines if needed
  66. * @param {text or string} input input data
  67. * @param {number} str_len optional size of lines or 0 if split is not needed
  68. * @return {rspamd_text} encoded data chunk
  69. */
  70. LUA_FUNCTION_DEF(util, encode_qp);
  71. /***
  72. * @function util.decode_qp(input)
  73. * Decodes data from quoted printable
  74. * @param {text or string} input input data
  75. * @return {rspamd_text} decoded data chunk
  76. */
  77. LUA_FUNCTION_DEF(util, decode_qp);
  78. /***
  79. * @function util.decode_base64(input)
  80. * Decodes data from base64 ignoring whitespace characters
  81. * @param {text or string} input data to decode; if `rspamd{text}` is used then the string is modified **in-place**
  82. * @return {rspamd_text} decoded data chunk
  83. */
  84. LUA_FUNCTION_DEF(util, decode_base64);
  85. /***
  86. * @function util.encode_base32(input, [b32type = 'default'])
  87. * Encodes data in base32 breaking lines if needed
  88. * @param {text or string} input input data
  89. * @param {string} b32type base32 type (default, bleach, rfc)
  90. * @return {rspamd_text} encoded data chunk
  91. */
  92. LUA_FUNCTION_DEF(util, encode_base32);
  93. /***
  94. * @function util.decode_base32(input, [b32type = 'default'])
  95. * Decodes data from base32 ignoring whitespace characters
  96. * @param {text or string} input data to decode
  97. * @param {string} b32type base32 type (default, bleach, rfc)
  98. * @return {rspamd_text} decoded data chunk
  99. */
  100. LUA_FUNCTION_DEF(util, decode_base32);
  101. /***
  102. * @function util.decode_url(input)
  103. * Decodes data from url encoding
  104. * @param {text or string} input data to decode
  105. * @return {rspamd_text} decoded data chunk
  106. */
  107. LUA_FUNCTION_DEF(util, decode_url);
  108. /***
  109. * @function util.tokenize_text(input[, exceptions])
  110. * Create tokens from a text using optional exceptions list
  111. * @param {text/string} input input data
  112. * @param {table} exceptions, a table of pairs containing <start_pos,length> of exceptions in the input
  113. * @return {table/strings} list of strings representing words in the text
  114. */
  115. LUA_FUNCTION_DEF(util, tokenize_text);
  116. LUA_FUNCTION_DEF(util, process_message);
  117. /***
  118. * @function util.tanh(num)
  119. * Calculates hyperbolic tangent of the specified floating point value
  120. * @param {number} num input number
  121. * @return {number} hyperbolic tangent of the variable
  122. */
  123. LUA_FUNCTION_DEF(util, tanh);
  124. /***
  125. * @function util.parse_html(input)
  126. * Parses HTML and returns the according text
  127. * @param {string|text} in input HTML
  128. * @return {rspamd_text} processed text with no HTML tags
  129. */
  130. LUA_FUNCTION_DEF(util, parse_html);
  131. /***
  132. * @function util.levenshtein_distance(s1, s2)
  133. * Returns levenstein distance between two strings
  134. * @param {string} s1 the first string
  135. * @param {string} s2 the second string
  136. * @return {number} number of differences in two strings
  137. */
  138. LUA_FUNCTION_DEF(util, levenshtein_distance);
  139. /***
  140. * @function util.fold_header(name, value, [how, [stop_chars]])
  141. * Fold rfc822 header according to the folding rules
  142. *
  143. * @param {string} name name of the header
  144. * @param {string} value value of the header
  145. * @param {string} how "cr" for \r, "lf" for \n and "crlf" for \r\n (default)
  146. * @param {string} stop_chars also fold header when the
  147. * @return {string} Folded value of the header
  148. */
  149. LUA_FUNCTION_DEF(util, fold_header);
  150. /***
  151. * @function util.is_uppercase(str)
  152. * Returns true if a string is all uppercase
  153. *
  154. * @param {string} str input string
  155. * @return {bool} true if a string is all uppercase
  156. */
  157. LUA_FUNCTION_DEF(util, is_uppercase);
  158. /***
  159. * @function util.humanize_number(num)
  160. * Returns humanized representation of given number (like 1k instead of 1000)
  161. *
  162. * @param {number} num number to humanize
  163. * @return {string} humanized representation of a number
  164. */
  165. LUA_FUNCTION_DEF(util, humanize_number);
  166. /***
  167. * @function util.get_tld(host)
  168. * Returns effective second level domain part (eSLD) for the specified host
  169. *
  170. * @param {string} host hostname
  171. * @return {string} eSLD part of the hostname or the full hostname if eSLD was not found
  172. */
  173. LUA_FUNCTION_DEF(util, get_tld);
  174. /***
  175. * @function util.glob(pattern)
  176. * Returns results for the glob match for the specified pattern
  177. *
  178. * @param {string} pattern glob pattern to match ('?' and '*' are supported)
  179. * @return {table/string} list of matched files
  180. */
  181. LUA_FUNCTION_DEF(util, glob);
  182. /***
  183. * @function util.parse_mail_address(str, [pool])
  184. * Parses email address and returns a table of tables in the following format:
  185. *
  186. * - `raw` - the original value without any processing
  187. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  188. * - `addr` - address part of the address
  189. * - `user` - user part (if present) of the address, e.g. `blah`
  190. * - `domain` - domain part (if present), e.g. `foo.com`
  191. * - `flags` - table with following keys set to true if given condition fulfilled:
  192. * - [valid] - valid SMTP address in conformity with https://tools.ietf.org/html/rfc5321#section-4.1.
  193. * - [ip] - domain is IPv4/IPv6 address
  194. * - [braced] - angled `<blah@foo.com>` address
  195. * - [quoted] - quoted user part
  196. * - [empty] - empty address
  197. * - [backslash] - user part contains backslash
  198. * - [8bit] - contains 8bit characters
  199. *
  200. * @param {string} str input string
  201. * @param {rspamd_mempool} pool memory pool to use
  202. * @return {table/tables} parsed list of mail addresses
  203. */
  204. LUA_FUNCTION_DEF(util, parse_mail_address);
  205. /***
  206. * @function util.strlen_utf8(str)
  207. * Returns length of string encoded in utf-8 in characters.
  208. * If invalid characters are found, then this function returns number of bytes.
  209. * @param {string} str utf8 encoded string
  210. * @return {number} number of characters in string
  211. */
  212. LUA_FUNCTION_DEF(util, strlen_utf8);
  213. /***
  214. * @function util.lower_utf8(str)
  215. * Converts utf8 string to lower case
  216. * @param {string} str utf8 encoded string
  217. * @return {string} lowercased utf8 string
  218. */
  219. LUA_FUNCTION_DEF(util, lower_utf8);
  220. /***
  221. * @function util.normalize_utf8(str)
  222. * Gets a string in UTF8 and normalises it to NFKC_Casefold form
  223. * @param {string} str utf8 encoded string
  224. * @return {string,integer} lowercased utf8 string + result of the normalisation (use bit.band to check):
  225. * RSPAMD_UNICODE_NORM_NORMAL = 0,
  226. * RSPAMD_UNICODE_NORM_UNNORMAL = (1 << 0),
  227. * RSPAMD_UNICODE_NORM_ZERO_SPACES = (1 << 1),
  228. * RSPAMD_UNICODE_NORM_ERROR = (1 << 2),
  229. * RSPAMD_UNICODE_NORM_OVERFLOW = (1 << 3)
  230. */
  231. LUA_FUNCTION_DEF(util, normalize_utf8);
  232. /***
  233. * @function util.transliterate(str)
  234. * Converts utf8 encoded string to latin transliteration
  235. * @param {string/text} str utf8 encoded string
  236. * @return {text} transliterated string
  237. */
  238. LUA_FUNCTION_DEF(util, transliterate);
  239. /***
  240. * @function util.strequal_caseless(str1, str2)
  241. * Compares two strings regardless of their case using ascii comparison.
  242. * Returns `true` if `str1` is equal to `str2`
  243. * @param {string} str1 utf8 encoded string
  244. * @param {string} str2 utf8 encoded string
  245. * @return {bool} result of comparison
  246. */
  247. LUA_FUNCTION_DEF(util, strequal_caseless);
  248. /***
  249. * @function util.strequal_caseless_utf8(str1, str2)
  250. * Compares two utf8 strings regardless of their case using utf8 collation rules.
  251. * Returns `true` if `str1` is equal to `str2`
  252. * @param {string} str1 utf8 encoded string
  253. * @param {string} str2 utf8 encoded string
  254. * @return {bool} result of comparison
  255. */
  256. LUA_FUNCTION_DEF(util, strequal_caseless_utf8);
  257. /***
  258. * @function util.get_ticks()
  259. * Returns current number of ticks as floating point number
  260. * @return {number} number of current clock ticks (monotonically increasing)
  261. */
  262. LUA_FUNCTION_DEF(util, get_ticks);
  263. /***
  264. * @function util.get_time()
  265. * Returns current time as unix time in floating point representation
  266. * @return {number} number of seconds since 01.01.1970
  267. */
  268. LUA_FUNCTION_DEF(util, get_time);
  269. /***
  270. * @function util.time_to_string(seconds)
  271. * Converts time from Unix time to HTTP date format
  272. * @param {number} seconds unix timestamp
  273. * @return {string} date as HTTP date
  274. */
  275. LUA_FUNCTION_DEF(util, time_to_string);
  276. /***
  277. * @function util.stat(fname)
  278. * Performs stat(2) on a specified filepath and returns table of values
  279. *
  280. * - `size`: size of file in bytes
  281. * - `type`: type of filepath: `regular`, `directory`, `special`
  282. * - `mtime`: modification time as unix time
  283. *
  284. * @return {string,table} string is returned when error is occurred
  285. * @example
  286. *
  287. * local err,st = util.stat('/etc/password')
  288. *
  289. * if err then
  290. * -- handle error
  291. * else
  292. * print(st['size'])
  293. * end
  294. */
  295. LUA_FUNCTION_DEF(util, stat);
  296. /***
  297. * @function util.unlink(fname)
  298. * Removes the specified file from the filesystem
  299. *
  300. * @param {string} fname filename to remove
  301. * @return {boolean,[string]} true if file has been deleted or false,'error string'
  302. */
  303. LUA_FUNCTION_DEF(util, unlink);
  304. /***
  305. * @function util.lock_file(fname, [fd])
  306. * Lock the specified file. This function returns {number} which must be passed to `util.unlock_file` after usage
  307. * or you'll have a resource leak
  308. *
  309. * @param {string} fname filename to lock
  310. * @param {number} fd use the specified fd instead of opening one
  311. * @return {number|nil,string} number if locking was successful or nil + error otherwise
  312. */
  313. LUA_FUNCTION_DEF(util, lock_file);
  314. /***
  315. * @function util.unlock_file(fd, [close_fd])
  316. * Unlock the specified file closing the file descriptor associated.
  317. *
  318. * @param {number} fd descriptor to unlock
  319. * @param {boolean} close_fd close descriptor on unlocking (default: TRUE)
  320. * @return {boolean[,string]} true if a file was unlocked
  321. */
  322. LUA_FUNCTION_DEF(util, unlock_file);
  323. /***
  324. * @function util.create_file(fname, [mode])
  325. * Creates the specified file with the default mode 0644
  326. *
  327. * @param {string} fname filename to create
  328. * @param {number} mode open mode (you should use octal number here)
  329. * @return {number|nil,string} file descriptor or pair nil + error string
  330. */
  331. LUA_FUNCTION_DEF(util, create_file);
  332. /***
  333. * @function util.close_file(fd)
  334. * Closes descriptor fd
  335. *
  336. * @param {number} fd descriptor to close
  337. * @return {boolean[,string]} true if a file was closed
  338. */
  339. LUA_FUNCTION_DEF(util, close_file);
  340. /***
  341. * @function util.random_hex(size)
  342. * Returns random hex string of the specified size
  343. *
  344. * @param {number} len length of desired string in bytes
  345. * @return {string} string with random hex digests
  346. */
  347. LUA_FUNCTION_DEF(util, random_hex);
  348. /***
  349. * @function util.zstd_compress(data, [level=1])
  350. * Compresses input using zstd compression
  351. *
  352. * @param {string/rspamd_text} data input data
  353. * @return {rspamd_text} compressed data
  354. */
  355. LUA_FUNCTION_DEF(util, zstd_compress);
  356. /***
  357. * @function util.zstd_decompress(data)
  358. * Decompresses input using zstd algorithm
  359. *
  360. * @param {string/rspamd_text} data compressed data
  361. * @return {error,rspamd_text} pair of error + decompressed text
  362. */
  363. LUA_FUNCTION_DEF(util, zstd_decompress);
  364. /***
  365. * @function util.gzip_decompress(data, [size_limit])
  366. * Decompresses input using gzip algorithm
  367. *
  368. * @param {string/rspamd_text} data compressed data
  369. * @param {integer} size_limit optional size limit
  370. * @return {rspamd_text} decompressed text
  371. */
  372. LUA_FUNCTION_DEF(util, gzip_decompress);
  373. /***
  374. * @function util.inflate(data, [size_limit])
  375. * Decompresses input using inflate algorithm
  376. *
  377. * @param {string/rspamd_text} data compressed data
  378. * @param {integer} size_limit optional size limit
  379. * @return {rspamd_text} decompressed text
  380. */
  381. LUA_FUNCTION_DEF(util, inflate);
  382. /***
  383. * @function util.gzip_compress(data, [level=1])
  384. * Compresses input using gzip compression
  385. *
  386. * @param {string/rspamd_text} data input data
  387. * @return {rspamd_text} compressed data
  388. */
  389. LUA_FUNCTION_DEF(util, gzip_compress);
  390. /***
  391. * @function util.normalize_prob(prob, [bias = 0.5])
  392. * Normalize probabilities using polynom
  393. *
  394. * @param {number} prob probability param
  395. * @param {number} bias number to subtract for making the final solution
  396. * @return {number} normalized number
  397. */
  398. LUA_FUNCTION_DEF(util, normalize_prob);
  399. /***
  400. * @function util.is_utf_spoofed(str, [str2])
  401. * Returns true if a string is spoofed (possibly with another string `str2`)
  402. * @return {boolean} true if a string is spoofed
  403. */
  404. LUA_FUNCTION_DEF(util, is_utf_spoofed);
  405. /**
  406. * @function util.is_utf_mixed_script(str)
  407. * Returns true if a string contains mixed unicode scripts
  408. * @param {string} String to check
  409. * @return {boolean} true if a string contains chars with mixed unicode script
  410. */
  411. LUA_FUNCTION_DEF(util, is_utf_mixed_script);
  412. /**
  413. * @function util.is_utf_outside_range(str, range_start, range_end)
  414. * Returns true if a string contains chars outside range
  415. * @param {string} String to check
  416. * @param {number} start of character range similar to uset_addRange
  417. * @param {number} end of character range similar to uset_addRange
  418. * @return {boolean} true if a string contains chars outside selected utf range
  419. */
  420. LUA_FUNCTION_DEF(util, is_utf_outside_range);
  421. /***
  422. * @function util.get_string_stats(str)
  423. * Returns table with number of letters and digits in string
  424. * @return {table} with string stats keys are "digits" and "letters"
  425. */
  426. LUA_FUNCTION_DEF(util, get_string_stats);
  427. /***
  428. * @function util.is_valid_utf8(str)
  429. * Returns true if a string is valid UTF8 string
  430. * @return {boolean} true if a string is spoofed
  431. */
  432. LUA_FUNCTION_DEF(util, is_valid_utf8);
  433. /***
  434. * @function util.has_obscured_unicode(str)
  435. * Returns true if a string has obscure UTF symbols (zero width spaces, order marks), ignores invalid utf characters
  436. * @return {boolean} true if a has obscured unicode characters (+ character and offset if found)
  437. */
  438. LUA_FUNCTION_DEF(util, has_obscured_unicode);
  439. /***
  440. * @function util.readline([prompt])
  441. * Returns string read from stdin with history and editing support
  442. * @return {string} string read from the input (with line endings stripped)
  443. */
  444. LUA_FUNCTION_DEF(util, readline);
  445. /***
  446. * @function util.readpassphrase([prompt])
  447. * Returns string read from stdin disabling echo
  448. * @return {string} string read from the input (with line endings stripped)
  449. */
  450. LUA_FUNCTION_DEF(util, readpassphrase);
  451. /***
  452. * @function util.file_exists(file)
  453. * Checks if a specified file exists and is available for reading
  454. * @return {boolean,string} true if file exists + string error if not
  455. */
  456. LUA_FUNCTION_DEF(util, file_exists);
  457. /***
  458. * @function util.mkdir(dir[, recursive])
  459. * Creates a specified directory
  460. * @return {boolean[,error]} true if directory has been created
  461. */
  462. LUA_FUNCTION_DEF(util, mkdir);
  463. /***
  464. * @function util.umask(mask)
  465. * Sets new umask. Accepts either numeric octal string, e.g. '022' or a plain
  466. * number, e.g. 0x12 (since Lua does not support octal integrals)
  467. * @return {number} old umask
  468. */
  469. LUA_FUNCTION_DEF(util, umask);
  470. /***
  471. * @function util.isatty()
  472. * Returns if stdout is a tty
  473. * @return {boolean} true in case of output being tty
  474. */
  475. LUA_FUNCTION_DEF(util, isatty);
  476. /***
  477. * @function util.pack(fmt, ...)
  478. *
  479. * Backport of Lua 5.3 `string.pack` function:
  480. * Returns a binary string containing the values v1, v2, etc. packed (that is,
  481. * serialized in binary form) according to the format string `fmt`
  482. * A format string is a sequence of conversion options. The conversion
  483. * options are as follows:
  484. *
  485. * * <: sets little endian
  486. * * >: sets big endian
  487. * * =: sets native endian
  488. * * ![n]: sets maximum alignment to n (default is native alignment)
  489. * * b: a signed byte (char)
  490. * * B: an unsigned byte (char)
  491. * * h: a signed short (native size)
  492. * * H: an unsigned short (native size)
  493. * * l: a signed long (native size)
  494. * * L: an unsigned long (native size)
  495. * * j: a lua_Integer
  496. * * J: a lua_Unsigned
  497. * * T: a size_t (native size)
  498. * * i[n]: a signed int with n bytes (default is native size)
  499. * * I[n]: an unsigned int with n bytes (default is native size)
  500. * * f: a float (native size)
  501. * * d: a double (native size)
  502. * * n: a lua_Number
  503. * * cn: a fixed-sized string with n bytes
  504. * * z: a zero-terminated string
  505. * * s[n]: a string preceded by its length coded as an unsigned integer with
  506. * * n bytes (default is a size_t)
  507. * * x: one byte of padding
  508. * * Xop: an empty item that aligns according to option op (which is otherwise ignored)
  509. * * ' ': (empty space) ignored
  510. *
  511. * (A "[n]" means an optional integral numeral.) Except for padding, spaces,
  512. * and configurations (options "xX <=>!"), each option corresponds to an
  513. * argument (in string.pack) or a result (in string.unpack).
  514. *
  515. * For options "!n", "sn", "in", and "In", n can be any integer between 1 and
  516. * All integral options check overflows; string.pack checks whether the given
  517. * value fits in the given size; string.unpack checks whether the read value
  518. * fits in a Lua integer.
  519. *
  520. * Any format string starts as if prefixed by "!1=", that is, with maximum
  521. * alignment of 1 (no alignment) and native endianness.
  522. *
  523. * Alignment works as follows: For each option, the format gets extra padding
  524. * until the data starts at an offset that is a multiple of the minimum
  525. * between the option size and the maximum alignment; this minimum must be a
  526. * power of 2. Options "c" and "z" are not aligned; option "s" follows the
  527. * alignment of its starting integer.
  528. *
  529. * All padding is filled with zeros by string.pack (and ignored by unpack).
  530. */
  531. LUA_FUNCTION_DEF(util, pack);
  532. /***
  533. * @function util.packsize(fmt)
  534. *
  535. * Returns size of the packed binary string returned for the same `fmt` argument
  536. * by @see util.pack
  537. */
  538. LUA_FUNCTION_DEF(util, packsize);
  539. /***
  540. * @function util.unpack(fmt, s [, pos])
  541. * Unpacks string `s` according to the format string `fmt` as described in
  542. * @see util.pack
  543. *
  544. * @returns {multiple} list of unpacked values according to `fmt`
  545. */
  546. LUA_FUNCTION_DEF(util, unpack);
  547. /***
  548. * @function util.caseless_hash(str[, seed])
  549. * Calculates caseless non-crypto hash from a string or rspamd text
  550. * @param str string or lua_text
  551. * @param seed mandatory seed (0xdeadbabe by default)
  552. * @return {int64} boxed int64_t
  553. */
  554. LUA_FUNCTION_DEF(util, caseless_hash);
  555. /***
  556. * @function util.caseless_hash_fast(str[, seed])
  557. * Calculates caseless non-crypto hash from a string or rspamd text
  558. * @param str string or lua_text
  559. * @param seed mandatory seed (0xdeadbabe by default)
  560. * @return {number} number from int64_t
  561. */
  562. LUA_FUNCTION_DEF(util, caseless_hash_fast);
  563. /***
  564. * @function util.get_hostname()
  565. * Returns hostname for this machine
  566. * @return {string} hostname
  567. */
  568. LUA_FUNCTION_DEF(util, get_hostname);
  569. /***
  570. * @function util.parse_content_type(ct_string, mempool)
  571. * Parses content-type string to a table:
  572. * - `type`
  573. * - `subtype`
  574. * - `charset`
  575. * - `boundary`
  576. * - other attributes
  577. *
  578. * @param {string} ct_string content type as string
  579. * @param {rspamd_mempool} mempool needed to store temporary data (e.g. task pool)
  580. * @return table or nil if cannot parse content type
  581. */
  582. LUA_FUNCTION_DEF(util, parse_content_type);
  583. /***
  584. * @function util.mime_header_encode(hdr)
  585. * Encodes header if needed
  586. * @param {string} hdr input header
  587. * @return encoded header
  588. */
  589. LUA_FUNCTION_DEF(util, mime_header_encode);
  590. /***
  591. * @function util.btc_polymod(input_values)
  592. * Performs bitcoin polymod function
  593. * @param {table|numbers} input_values
  594. * @return {boolean} true if polymod has been successful
  595. */
  596. LUA_FUNCTION_DEF(util, btc_polymod);
  597. /***
  598. * @function util.parse_smtp_date(str[, local_tz])
  599. * Converts an SMTP date string to unix timestamp
  600. * @param {string} str input string
  601. * @param {boolean} local_tz convert to local tz if `true`
  602. * @return {number} time as unix timestamp (converted to float)
  603. */
  604. LUA_FUNCTION_DEF(util, parse_smtp_date);
  605. static const struct luaL_reg utillib_f[] = {
  606. LUA_INTERFACE_DEF(util, create_event_base),
  607. LUA_INTERFACE_DEF(util, load_rspamd_config),
  608. LUA_INTERFACE_DEF(util, config_from_ucl),
  609. LUA_INTERFACE_DEF(util, process_message),
  610. LUA_INTERFACE_DEF(util, encode_base64),
  611. LUA_INTERFACE_DEF(util, encode_qp),
  612. LUA_INTERFACE_DEF(util, decode_qp),
  613. LUA_INTERFACE_DEF(util, decode_base64),
  614. LUA_INTERFACE_DEF(util, encode_base32),
  615. LUA_INTERFACE_DEF(util, decode_base32),
  616. LUA_INTERFACE_DEF(util, decode_url),
  617. LUA_INTERFACE_DEF(util, tokenize_text),
  618. LUA_INTERFACE_DEF(util, tanh),
  619. LUA_INTERFACE_DEF(util, parse_html),
  620. LUA_INTERFACE_DEF(util, levenshtein_distance),
  621. LUA_INTERFACE_DEF(util, fold_header),
  622. LUA_INTERFACE_DEF(util, is_uppercase),
  623. LUA_INTERFACE_DEF(util, humanize_number),
  624. LUA_INTERFACE_DEF(util, get_tld),
  625. LUA_INTERFACE_DEF(util, glob),
  626. {"parse_addr", lua_util_parse_mail_address},
  627. LUA_INTERFACE_DEF(util, parse_mail_address),
  628. LUA_INTERFACE_DEF(util, strlen_utf8),
  629. LUA_INTERFACE_DEF(util, lower_utf8),
  630. LUA_INTERFACE_DEF(util, normalize_utf8),
  631. LUA_INTERFACE_DEF(util, transliterate),
  632. LUA_INTERFACE_DEF(util, strequal_caseless),
  633. LUA_INTERFACE_DEF(util, strequal_caseless_utf8),
  634. LUA_INTERFACE_DEF(util, get_ticks),
  635. LUA_INTERFACE_DEF(util, get_time),
  636. LUA_INTERFACE_DEF(util, time_to_string),
  637. LUA_INTERFACE_DEF(util, stat),
  638. LUA_INTERFACE_DEF(util, unlink),
  639. LUA_INTERFACE_DEF(util, lock_file),
  640. LUA_INTERFACE_DEF(util, unlock_file),
  641. LUA_INTERFACE_DEF(util, create_file),
  642. LUA_INTERFACE_DEF(util, close_file),
  643. LUA_INTERFACE_DEF(util, random_hex),
  644. LUA_INTERFACE_DEF(util, zstd_compress),
  645. LUA_INTERFACE_DEF(util, zstd_decompress),
  646. LUA_INTERFACE_DEF(util, gzip_compress),
  647. LUA_INTERFACE_DEF(util, gzip_decompress),
  648. LUA_INTERFACE_DEF(util, inflate),
  649. LUA_INTERFACE_DEF(util, normalize_prob),
  650. LUA_INTERFACE_DEF(util, caseless_hash),
  651. LUA_INTERFACE_DEF(util, caseless_hash_fast),
  652. LUA_INTERFACE_DEF(util, is_utf_spoofed),
  653. LUA_INTERFACE_DEF(util, is_utf_mixed_script),
  654. LUA_INTERFACE_DEF(util, is_utf_outside_range),
  655. LUA_INTERFACE_DEF(util, get_string_stats),
  656. LUA_INTERFACE_DEF(util, is_valid_utf8),
  657. LUA_INTERFACE_DEF(util, has_obscured_unicode),
  658. LUA_INTERFACE_DEF(util, readline),
  659. LUA_INTERFACE_DEF(util, readpassphrase),
  660. LUA_INTERFACE_DEF(util, file_exists),
  661. LUA_INTERFACE_DEF(util, mkdir),
  662. LUA_INTERFACE_DEF(util, umask),
  663. LUA_INTERFACE_DEF(util, isatty),
  664. LUA_INTERFACE_DEF(util, get_hostname),
  665. LUA_INTERFACE_DEF(util, parse_content_type),
  666. LUA_INTERFACE_DEF(util, mime_header_encode),
  667. LUA_INTERFACE_DEF(util, pack),
  668. LUA_INTERFACE_DEF(util, unpack),
  669. LUA_INTERFACE_DEF(util, packsize),
  670. LUA_INTERFACE_DEF(util, btc_polymod),
  671. LUA_INTERFACE_DEF(util, parse_smtp_date),
  672. {NULL, NULL}};
  673. LUA_FUNCTION_DEF(int64, tostring);
  674. LUA_FUNCTION_DEF(int64, fromstring);
  675. LUA_FUNCTION_DEF(int64, tonumber);
  676. LUA_FUNCTION_DEF(int64, hex);
  677. static const struct luaL_reg int64lib_f[] = {
  678. LUA_INTERFACE_DEF(int64, fromstring),
  679. {NULL, NULL}};
  680. static const struct luaL_reg int64lib_m[] = {
  681. LUA_INTERFACE_DEF(int64, tostring),
  682. LUA_INTERFACE_DEF(int64, tonumber),
  683. LUA_INTERFACE_DEF(int64, hex),
  684. {"__tostring", lua_int64_tostring},
  685. {NULL, NULL}};
  686. LUA_FUNCTION_DEF(ev_base, loop);
  687. static const struct luaL_reg ev_baselib_m[] = {
  688. LUA_INTERFACE_DEF(ev_base, loop),
  689. {"__tostring", rspamd_lua_class_tostring},
  690. {NULL, NULL}};
  691. static int64_t
  692. lua_check_int64(lua_State *L, int pos)
  693. {
  694. void *ud = rspamd_lua_check_udata(L, pos, rspamd_int64_classname);
  695. luaL_argcheck(L, ud != NULL, pos, "'int64' expected");
  696. return ud ? *((int64_t *) ud) : 0LL;
  697. }
  698. static int
  699. lua_util_create_event_base(lua_State *L)
  700. {
  701. LUA_TRACE_POINT;
  702. struct ev_loop **pev_base;
  703. pev_base = lua_newuserdata(L, sizeof(struct ev_loop *));
  704. rspamd_lua_setclass(L, rspamd_ev_base_classname, -1);
  705. *pev_base = ev_loop_new(EVFLAG_SIGNALFD | EVBACKEND_ALL);
  706. return 1;
  707. }
  708. static int
  709. lua_util_load_rspamd_config(lua_State *L)
  710. {
  711. LUA_TRACE_POINT;
  712. struct rspamd_config *cfg, **pcfg;
  713. const char *cfg_name;
  714. cfg_name = luaL_checkstring(L, 1);
  715. if (cfg_name) {
  716. cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA);
  717. cfg->lua_state = L;
  718. if (rspamd_config_read(cfg, cfg_name, NULL, NULL, NULL, FALSE, NULL)) {
  719. msg_err_config("cannot load config from %s", cfg_name);
  720. lua_pushnil(L);
  721. }
  722. else {
  723. rspamd_config_post_load(cfg, 0);
  724. pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *));
  725. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  726. *pcfg = cfg;
  727. }
  728. }
  729. return 1;
  730. }
  731. static int
  732. parse_config_options(const char *str_options)
  733. {
  734. int ret = 0;
  735. char **vec;
  736. const char *str;
  737. unsigned int i, l;
  738. vec = g_strsplit_set(str_options, ",;", -1);
  739. if (vec) {
  740. l = g_strv_length(vec);
  741. for (i = 0; i < l; i++) {
  742. str = vec[i];
  743. if (g_ascii_strcasecmp(str, "INIT_URL") == 0) {
  744. ret |= RSPAMD_CONFIG_INIT_URL;
  745. }
  746. else if (g_ascii_strcasecmp(str, "INIT_LIBS") == 0) {
  747. ret |= RSPAMD_CONFIG_INIT_LIBS;
  748. }
  749. else if (g_ascii_strcasecmp(str, "INIT_SYMCACHE") == 0) {
  750. ret |= RSPAMD_CONFIG_INIT_SYMCACHE;
  751. }
  752. else if (g_ascii_strcasecmp(str, "INIT_VALIDATE") == 0) {
  753. ret |= RSPAMD_CONFIG_INIT_VALIDATE;
  754. }
  755. else if (g_ascii_strcasecmp(str, "INIT_NO_TLD") == 0) {
  756. ret |= RSPAMD_CONFIG_INIT_NO_TLD;
  757. }
  758. else if (g_ascii_strcasecmp(str, "INIT_PRELOAD_MAPS") == 0) {
  759. ret |= RSPAMD_CONFIG_INIT_PRELOAD_MAPS;
  760. }
  761. else {
  762. msg_warn("bad type: %s", str);
  763. }
  764. }
  765. g_strfreev(vec);
  766. }
  767. return ret;
  768. }
  769. static int
  770. lua_util_config_from_ucl(lua_State *L)
  771. {
  772. LUA_TRACE_POINT;
  773. struct rspamd_config *cfg = NULL, **pcfg;
  774. struct rspamd_rcl_sections_map *top;
  775. GError *err = NULL;
  776. ucl_object_t *obj;
  777. const char *str_options = NULL;
  778. int int_options = 0;
  779. obj = ucl_object_lua_import(L, 1);
  780. if (lua_gettop(L) == 2) {
  781. if (lua_type(L, 2) == LUA_TSTRING) {
  782. str_options = lua_tostring(L, 2);
  783. int_options = parse_config_options(str_options);
  784. }
  785. else {
  786. msg_err("config_from_ucl: second parameter is expected to be string");
  787. ucl_object_unref(obj);
  788. lua_pushnil(L);
  789. }
  790. }
  791. if (obj) {
  792. cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA);
  793. cfg->lua_state = L;
  794. cfg->cfg_ucl_obj = obj;
  795. top = rspamd_rcl_config_init(cfg, NULL);
  796. if (!rspamd_rcl_parse(top, cfg, cfg, cfg->cfg_pool, cfg->cfg_ucl_obj, &err)) {
  797. msg_err("rcl parse error: %s", err->message);
  798. ucl_object_unref(obj);
  799. lua_pushnil(L);
  800. }
  801. else {
  802. if (int_options & RSPAMD_CONFIG_INIT_LIBS) {
  803. cfg->libs_ctx = rspamd_init_libs();
  804. }
  805. rspamd_config_post_load(cfg, int_options);
  806. pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *));
  807. rspamd_lua_setclass(L, rspamd_config_classname, -1);
  808. *pcfg = cfg;
  809. }
  810. rspamd_rcl_sections_free(top);
  811. }
  812. return 1;
  813. }
  814. static gboolean
  815. lua_util_task_fin(struct rspamd_task *task, void *ud)
  816. {
  817. ucl_object_t **target = ud;
  818. *target = rspamd_protocol_write_ucl(task, RSPAMD_PROTOCOL_DEFAULT);
  819. rdns_resolver_release(task->resolver->r);
  820. return TRUE;
  821. }
  822. static int
  823. lua_util_process_message(lua_State *L)
  824. {
  825. LUA_TRACE_POINT;
  826. struct rspamd_config *cfg = lua_check_config(L, 1);
  827. const char *message;
  828. gsize mlen;
  829. struct rspamd_task *task;
  830. struct ev_loop *base;
  831. ucl_object_t *res = NULL;
  832. message = luaL_checklstring(L, 2, &mlen);
  833. if (cfg != NULL && message != NULL) {
  834. base = ev_loop_new(EVFLAG_SIGNALFD | EVBACKEND_ALL);
  835. rspamd_init_filters(cfg, false, false);
  836. task = rspamd_task_new(NULL, cfg, NULL, NULL, base, FALSE);
  837. task->msg.begin = rspamd_mempool_alloc(task->task_pool, mlen);
  838. rspamd_strlcpy((gpointer) task->msg.begin, message, mlen);
  839. task->msg.len = mlen;
  840. task->fin_callback = lua_util_task_fin;
  841. task->fin_arg = &res;
  842. task->resolver = rspamd_dns_resolver_init(NULL, base, cfg);
  843. task->s = rspamd_session_create(task->task_pool, rspamd_task_fin,
  844. NULL, (event_finalizer_t) rspamd_task_free, task);
  845. if (!rspamd_task_load_message(task, NULL, message, mlen)) {
  846. lua_pushnil(L);
  847. }
  848. else {
  849. if (rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL)) {
  850. ev_loop(base, 0);
  851. if (res != NULL) {
  852. ucl_object_push_lua(L, res, true);
  853. ucl_object_unref(res);
  854. }
  855. else {
  856. ucl_object_push_lua(L,
  857. rspamd_protocol_write_ucl(task, RSPAMD_PROTOCOL_DEFAULT),
  858. true);
  859. rdns_resolver_release(task->resolver->r);
  860. rspamd_session_destroy(task->s);
  861. }
  862. }
  863. else {
  864. lua_pushnil(L);
  865. }
  866. }
  867. ev_loop_destroy(base);
  868. }
  869. else {
  870. lua_pushnil(L);
  871. }
  872. return 1;
  873. }
  874. static int
  875. lua_util_encode_base64(lua_State *L)
  876. {
  877. LUA_TRACE_POINT;
  878. struct rspamd_lua_text *t;
  879. char *out;
  880. gsize outlen;
  881. long str_lim = 0;
  882. gboolean fold = FALSE;
  883. t = lua_check_text_or_string(L, 1);
  884. if (lua_gettop(L) > 1) {
  885. str_lim = luaL_checkinteger(L, 2);
  886. fold = str_lim > 0;
  887. }
  888. if (t == NULL) {
  889. return luaL_error(L, "invalid arguments");
  890. }
  891. else {
  892. if (fold) {
  893. out = rspamd_encode_base64(t->start, t->len, str_lim, &outlen);
  894. }
  895. else {
  896. enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF;
  897. if (lua_type(L, 3) == LUA_TSTRING) {
  898. const char *how_str = lua_tostring(L, 3);
  899. if (g_ascii_strcasecmp(how_str, "cr") == 0) {
  900. how = RSPAMD_TASK_NEWLINES_CR;
  901. }
  902. else if (g_ascii_strcasecmp(how_str, "lf") == 0) {
  903. how = RSPAMD_TASK_NEWLINES_LF;
  904. }
  905. else if (g_ascii_strcasecmp(how_str, "crlf") != 0) {
  906. return luaL_error(L, "invalid newline style: %s", how_str);
  907. }
  908. }
  909. out = rspamd_encode_base64_fold(t->start, t->len, str_lim, &outlen, how);
  910. }
  911. if (out != NULL) {
  912. lua_new_text(L, out, outlen, TRUE);
  913. }
  914. else {
  915. lua_pushnil(L);
  916. }
  917. }
  918. return 1;
  919. }
  920. static int
  921. lua_util_encode_qp(lua_State *L)
  922. {
  923. LUA_TRACE_POINT;
  924. struct rspamd_lua_text *t;
  925. const char *s = NULL;
  926. char *out;
  927. gsize inlen, outlen;
  928. unsigned int str_lim = 0;
  929. if (lua_type(L, 1) == LUA_TSTRING) {
  930. s = luaL_checklstring(L, 1, &inlen);
  931. }
  932. else if (lua_type(L, 1) == LUA_TUSERDATA) {
  933. t = lua_check_text(L, 1);
  934. if (t != NULL) {
  935. s = t->start;
  936. inlen = t->len;
  937. }
  938. }
  939. if (lua_gettop(L) > 1) {
  940. str_lim = luaL_checknumber(L, 2);
  941. }
  942. if (s == NULL) {
  943. lua_pushnil(L);
  944. }
  945. else {
  946. enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF;
  947. if (lua_type(L, 3) == LUA_TSTRING) {
  948. const char *how_str = lua_tostring(L, 3);
  949. if (g_ascii_strcasecmp(how_str, "cr") == 0) {
  950. how = RSPAMD_TASK_NEWLINES_CR;
  951. }
  952. else if (g_ascii_strcasecmp(how_str, "lf") == 0) {
  953. how = RSPAMD_TASK_NEWLINES_LF;
  954. }
  955. else if (g_ascii_strcasecmp(how_str, "crlf") != 0) {
  956. return luaL_error(L, "invalid newline style: %s", how_str);
  957. }
  958. }
  959. out = rspamd_encode_qp_fold(s, inlen, str_lim, &outlen, how);
  960. if (out != NULL) {
  961. t = lua_newuserdata(L, sizeof(*t));
  962. rspamd_lua_setclass(L, rspamd_text_classname, -1);
  963. t->start = out;
  964. t->len = outlen;
  965. /* Need destruction */
  966. t->flags = RSPAMD_TEXT_FLAG_OWN;
  967. }
  968. else {
  969. lua_pushnil(L);
  970. }
  971. }
  972. return 1;
  973. }
  974. static int
  975. lua_util_decode_qp(lua_State *L)
  976. {
  977. LUA_TRACE_POINT;
  978. struct rspamd_lua_text *t, *out;
  979. const char *s = NULL;
  980. gsize inlen = 0;
  981. gssize outlen;
  982. if (lua_type(L, 1) == LUA_TSTRING) {
  983. s = luaL_checklstring(L, 1, &inlen);
  984. }
  985. else if (lua_type(L, 1) == LUA_TUSERDATA) {
  986. t = lua_check_text(L, 1);
  987. if (t != NULL) {
  988. s = t->start;
  989. inlen = t->len;
  990. }
  991. }
  992. if (s == NULL) {
  993. lua_pushnil(L);
  994. }
  995. else {
  996. out = lua_newuserdata(L, sizeof(*t));
  997. rspamd_lua_setclass(L, rspamd_text_classname, -1);
  998. out->start = g_malloc(inlen + 1);
  999. out->flags = RSPAMD_TEXT_FLAG_OWN;
  1000. outlen = rspamd_decode_qp_buf(s, inlen, (char *) out->start, inlen + 1);
  1001. if (outlen > 0) {
  1002. out->len = outlen;
  1003. }
  1004. else {
  1005. /*
  1006. * It removes out and frees memory on gc due to RSPAMD_TEXT_FLAG_OWN
  1007. */
  1008. lua_pop(L, 1);
  1009. lua_pushnil(L);
  1010. }
  1011. }
  1012. return 1;
  1013. }
  1014. static int
  1015. lua_util_decode_base64(lua_State *L)
  1016. {
  1017. LUA_TRACE_POINT;
  1018. struct rspamd_lua_text *t;
  1019. const char *s = NULL;
  1020. gsize inlen = 0, outlen;
  1021. if (lua_type(L, 1) == LUA_TSTRING) {
  1022. s = luaL_checklstring(L, 1, &inlen);
  1023. }
  1024. else if (lua_type(L, 1) == LUA_TUSERDATA) {
  1025. t = lua_check_text(L, 1);
  1026. if (t != NULL) {
  1027. s = t->start;
  1028. inlen = t->len;
  1029. }
  1030. }
  1031. if (s != NULL) {
  1032. t = lua_newuserdata(L, sizeof(*t));
  1033. rspamd_lua_setclass(L, rspamd_text_classname, -1);
  1034. t->len = (inlen / 4) * 3 + 3;
  1035. t->start = g_malloc(t->len);
  1036. rspamd_cryptobox_base64_decode(s, inlen, (unsigned char *) t->start,
  1037. &outlen);
  1038. t->len = outlen;
  1039. t->flags = RSPAMD_TEXT_FLAG_OWN;
  1040. }
  1041. else {
  1042. lua_pushnil(L);
  1043. }
  1044. return 1;
  1045. }
  1046. static int
  1047. lua_util_encode_base32(lua_State *L)
  1048. {
  1049. LUA_TRACE_POINT;
  1050. struct rspamd_lua_text *t;
  1051. const char *s = NULL;
  1052. char *out;
  1053. enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT;
  1054. gsize inlen, outlen;
  1055. if (lua_type(L, 1) == LUA_TSTRING) {
  1056. s = luaL_checklstring(L, 1, &inlen);
  1057. }
  1058. else if (lua_type(L, 1) == LUA_TUSERDATA) {
  1059. t = lua_check_text(L, 1);
  1060. if (t != NULL) {
  1061. s = t->start;
  1062. inlen = t->len;
  1063. }
  1064. }
  1065. if (lua_type(L, 2) == LUA_TSTRING) {
  1066. btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2));
  1067. if (btype == RSPAMD_BASE32_INVALID) {
  1068. return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2));
  1069. }
  1070. }
  1071. if (s == NULL) {
  1072. return luaL_error(L, "invalid arguments");
  1073. }
  1074. else {
  1075. out = rspamd_encode_base32(s, inlen, btype);
  1076. if (out != NULL) {
  1077. t = lua_newuserdata(L, sizeof(*t));
  1078. outlen = strlen(out);
  1079. rspamd_lua_setclass(L, rspamd_text_classname, -1);
  1080. t->start = out;
  1081. t->len = outlen;
  1082. /* Need destruction */
  1083. t->flags = RSPAMD_TEXT_FLAG_OWN;
  1084. }
  1085. else {
  1086. lua_pushnil(L);
  1087. }
  1088. }
  1089. return 1;
  1090. }
  1091. static int
  1092. lua_util_decode_base32(lua_State *L)
  1093. {
  1094. LUA_TRACE_POINT;
  1095. struct rspamd_lua_text *t;
  1096. const char *s = NULL;
  1097. gsize inlen, outlen;
  1098. enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT;
  1099. if (lua_type(L, 1) == LUA_TSTRING) {
  1100. s = luaL_checklstring(L, 1, &inlen);
  1101. }
  1102. else if (lua_type(L, 1) == LUA_TUSERDATA) {
  1103. t = lua_check_text(L, 1);
  1104. if (t != NULL) {
  1105. s = t->start;
  1106. inlen = t->len;
  1107. }
  1108. }
  1109. if (lua_type(L, 2) == LUA_TSTRING) {
  1110. btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2));
  1111. if (btype == RSPAMD_BASE32_INVALID) {
  1112. return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2));
  1113. }
  1114. }
  1115. if (s != NULL) {
  1116. unsigned char *decoded;
  1117. decoded = rspamd_decode_base32(s, inlen, &outlen, btype);
  1118. if (decoded) {
  1119. t = lua_newuserdata(L, sizeof(*t));
  1120. rspamd_lua_setclass(L, rspamd_text_classname, -1);
  1121. t->start = (const char *) decoded;
  1122. t->len = outlen;
  1123. t->flags = RSPAMD_TEXT_FLAG_OWN;
  1124. }
  1125. else {
  1126. lua_pushnil(L);
  1127. }
  1128. }
  1129. else {
  1130. lua_pushnil(L);
  1131. }
  1132. return 1;
  1133. }
  1134. static int
  1135. lua_util_decode_url(lua_State *L)
  1136. {
  1137. LUA_TRACE_POINT;
  1138. struct rspamd_lua_text *t;
  1139. t = lua_check_text_or_string(L, 1);
  1140. if (t != NULL) {
  1141. struct rspamd_lua_text *out = lua_new_text(L, NULL, t->len, TRUE);
  1142. out->len = rspamd_url_decode((char *) out->start, t->start, t->len);
  1143. }
  1144. else {
  1145. lua_pushnil(L);
  1146. }
  1147. return 1;
  1148. }
  1149. static int
  1150. lua_util_tokenize_text(lua_State *L)
  1151. {
  1152. return lua_parsers_tokenize_text(L);
  1153. }
  1154. static int
  1155. lua_util_tanh(lua_State *L)
  1156. {
  1157. LUA_TRACE_POINT;
  1158. double in = luaL_checknumber(L, 1);
  1159. lua_pushnumber(L, tanh(in));
  1160. return 1;
  1161. }
  1162. static int
  1163. lua_util_parse_html(lua_State *L)
  1164. {
  1165. return lua_parsers_parse_html(L);
  1166. }
  1167. static int
  1168. lua_util_levenshtein_distance(lua_State *L)
  1169. {
  1170. LUA_TRACE_POINT;
  1171. struct rspamd_lua_text *t1, *t2;
  1172. int dist = 0;
  1173. unsigned int replace_cost = 1;
  1174. t1 = lua_check_text_or_string(L, 1);
  1175. t2 = lua_check_text_or_string(L, 2);
  1176. if (lua_isnumber(L, 3)) {
  1177. replace_cost = lua_tointeger(L, 3);
  1178. }
  1179. if (t1 && t2) {
  1180. dist = rspamd_strings_levenshtein_distance(t1->start, t1->len, t2->start, t2->len,
  1181. replace_cost);
  1182. }
  1183. else {
  1184. return luaL_error(L, "invalid arguments");
  1185. }
  1186. lua_pushinteger(L, dist);
  1187. return 1;
  1188. }
  1189. static int
  1190. lua_util_fold_header(lua_State *L)
  1191. {
  1192. LUA_TRACE_POINT;
  1193. const char *how, *stop_chars = NULL;
  1194. struct rspamd_lua_text *name, *value;
  1195. GString *folded;
  1196. name = lua_check_text_or_string(L, 1);
  1197. value = lua_check_text_or_string(L, 2);
  1198. if (name && value) {
  1199. if (lua_isstring(L, 3)) {
  1200. how = lua_tostring(L, 3);
  1201. if (lua_isstring(L, 4)) {
  1202. stop_chars = lua_tostring(L, 4);
  1203. }
  1204. if (strcmp(how, "cr") == 0) {
  1205. folded = rspamd_header_value_fold(name->start, name->len,
  1206. value->start, value->len,
  1207. 0,
  1208. RSPAMD_TASK_NEWLINES_CR, stop_chars);
  1209. }
  1210. else if (strcmp(how, "lf") == 0) {
  1211. folded = rspamd_header_value_fold(name->start, name->len,
  1212. value->start, value->len, 0,
  1213. RSPAMD_TASK_NEWLINES_LF, stop_chars);
  1214. }
  1215. else {
  1216. folded = rspamd_header_value_fold(name->start, name->len,
  1217. value->start, value->len, 0,
  1218. RSPAMD_TASK_NEWLINES_CRLF, stop_chars);
  1219. }
  1220. }
  1221. else {
  1222. folded = rspamd_header_value_fold(name->start, name->len,
  1223. value->start, value->len, 0,
  1224. RSPAMD_TASK_NEWLINES_CRLF, stop_chars);
  1225. }
  1226. if (folded) {
  1227. lua_pushlstring(L, folded->str, folded->len);
  1228. g_string_free(folded, TRUE);
  1229. return 1;
  1230. }
  1231. }
  1232. lua_pushnil(L);
  1233. return 1;
  1234. }
  1235. static int
  1236. lua_util_is_uppercase(lua_State *L)
  1237. {
  1238. LUA_TRACE_POINT;
  1239. int32_t i = 0;
  1240. UChar32 uc;
  1241. bool is_upper = false, is_lower = false, is_other = false;
  1242. struct rspamd_lua_text *t = lua_check_text_or_string(L, 1);
  1243. if (t) {
  1244. while (i >= 0 && i < t->len) {
  1245. U8_NEXT(t->start, i, t->len, uc);
  1246. if (uc < 0) {
  1247. break;
  1248. }
  1249. if (u_isupper(uc)) {
  1250. is_upper = true;
  1251. }
  1252. else if (u_islower(uc)) {
  1253. is_lower = true;
  1254. break;
  1255. }
  1256. else if (u_charType(uc) == U_OTHER_LETTER) {
  1257. is_other = true;
  1258. break;
  1259. }
  1260. }
  1261. }
  1262. if (is_upper && !is_lower && !is_other) {
  1263. lua_pushboolean(L, TRUE);
  1264. }
  1265. else {
  1266. lua_pushboolean(L, FALSE);
  1267. }
  1268. return 1;
  1269. }
  1270. static int
  1271. lua_util_humanize_number(lua_State *L)
  1272. {
  1273. LUA_TRACE_POINT;
  1274. int64_t number = luaL_checkinteger(L, 1);
  1275. char numbuf[32];
  1276. rspamd_snprintf(numbuf, sizeof(numbuf), "%hL", number);
  1277. lua_pushstring(L, numbuf);
  1278. return 1;
  1279. }
  1280. static int
  1281. lua_util_get_tld(lua_State *L)
  1282. {
  1283. LUA_TRACE_POINT;
  1284. const char *host;
  1285. gsize hostlen;
  1286. rspamd_ftok_t tld;
  1287. host = luaL_checklstring(L, 1, &hostlen);
  1288. if (host) {
  1289. if (!rspamd_url_find_tld(host, hostlen, &tld)) {
  1290. lua_pushlstring(L, host, hostlen);
  1291. }
  1292. else {
  1293. lua_pushlstring(L, tld.begin, tld.len);
  1294. }
  1295. }
  1296. else {
  1297. lua_pushnil(L);
  1298. }
  1299. return 1;
  1300. }
  1301. static int
  1302. lua_util_glob(lua_State *L)
  1303. {
  1304. LUA_TRACE_POINT;
  1305. const char *pattern;
  1306. glob_t gl;
  1307. int top, i, flags = 0;
  1308. top = lua_gettop(L);
  1309. memset(&gl, 0, sizeof(gl));
  1310. for (i = 1; i <= top; i++, flags |= GLOB_APPEND) {
  1311. pattern = luaL_checkstring(L, i);
  1312. if (pattern) {
  1313. if (glob(pattern, flags, NULL, &gl) != 0) {
  1314. /* There is no way to return error here, so just create an table */
  1315. lua_createtable(L, 0, 0);
  1316. globfree(&gl);
  1317. return 1;
  1318. }
  1319. }
  1320. }
  1321. lua_createtable(L, gl.gl_pathc, 0);
  1322. /* Push results */
  1323. for (i = 0; i < (int) gl.gl_pathc; i++) {
  1324. lua_pushstring(L, gl.gl_pathv[i]);
  1325. lua_rawseti(L, -2, i + 1);
  1326. }
  1327. globfree(&gl);
  1328. return 1;
  1329. }
  1330. static int
  1331. lua_util_parse_mail_address(lua_State *L)
  1332. {
  1333. return lua_parsers_parse_mail_address(L);
  1334. }
  1335. static int
  1336. lua_util_strlen_utf8(lua_State *L)
  1337. {
  1338. LUA_TRACE_POINT;
  1339. struct rspamd_lua_text *t;
  1340. t = lua_check_text_or_string(L, 1);
  1341. if (t) {
  1342. int32_t i = 0, nchars = 0;
  1343. UChar32 uc;
  1344. while (i < t->len) {
  1345. U8_NEXT((uint8_t *) t->start, i, t->len, uc);
  1346. nchars++;
  1347. }
  1348. lua_pushinteger(L, nchars);
  1349. }
  1350. else {
  1351. return luaL_error(L, "invalid arguments");
  1352. }
  1353. return 1;
  1354. }
  1355. static int
  1356. lua_util_lower_utf8(lua_State *L)
  1357. {
  1358. LUA_TRACE_POINT;
  1359. struct rspamd_lua_text *t;
  1360. char *dst;
  1361. UChar32 uc;
  1362. UBool err = 0;
  1363. int32_t i = 0, j = 0;
  1364. t = lua_check_text_or_string(L, 1);
  1365. if (t) {
  1366. dst = g_malloc(t->len);
  1367. while (i < t->len && err == 0) {
  1368. U8_NEXT((uint8_t *) t->start, i, t->len, uc);
  1369. uc = u_tolower(uc);
  1370. U8_APPEND(dst, j, t->len, uc, err);
  1371. }
  1372. if (lua_isstring(L, 1)) {
  1373. lua_pushlstring(L, dst, j);
  1374. g_free(dst);
  1375. }
  1376. else {
  1377. t = lua_new_text(L, dst, j, FALSE);
  1378. /* We have actually allocated text data before */
  1379. t->flags |= RSPAMD_TEXT_FLAG_OWN;
  1380. }
  1381. }
  1382. else {
  1383. return luaL_error(L, "invalid arguments");
  1384. }
  1385. return 1;
  1386. }
  1387. static int
  1388. lua_util_normalize_utf8(lua_State *L)
  1389. {
  1390. LUA_TRACE_POINT;
  1391. struct rspamd_lua_text *t;
  1392. bool is_text = lua_type(L, 1) == LUA_TUSERDATA;
  1393. t = lua_check_text_or_string(L, 1);
  1394. if (!t) {
  1395. return luaL_error(L, "invalid arguments");
  1396. }
  1397. char *cpy = g_malloc(t->len + 1);
  1398. memcpy(cpy, t->start, t->len);
  1399. cpy[t->len] = '\0';
  1400. gsize len = t->len;
  1401. enum rspamd_utf8_normalise_result res = rspamd_normalise_unicode_inplace(cpy, &len);
  1402. if (is_text) {
  1403. struct rspamd_lua_text *out = lua_new_text(L, cpy, len, FALSE);
  1404. out->flags |= RSPAMD_TEXT_FLAG_OWN;
  1405. }
  1406. else {
  1407. lua_pushlstring(L, cpy, len);
  1408. g_free(cpy);
  1409. }
  1410. lua_pushinteger(L, res);
  1411. return 2;
  1412. }
  1413. static int
  1414. lua_util_transliterate(lua_State *L)
  1415. {
  1416. LUA_TRACE_POINT;
  1417. struct rspamd_lua_text *t;
  1418. t = lua_check_text_or_string(L, 1);
  1419. if (!t) {
  1420. return luaL_error(L, "invalid arguments");
  1421. }
  1422. gsize outlen;
  1423. char *transliterated = rspamd_utf8_transliterate(t->start, t->len, &outlen);
  1424. lua_new_text(L, transliterated, outlen, TRUE);
  1425. return 1;
  1426. }
  1427. static int
  1428. lua_util_strequal_caseless(lua_State *L)
  1429. {
  1430. LUA_TRACE_POINT;
  1431. struct rspamd_lua_text *t1, *t2;
  1432. int ret = -1;
  1433. t1 = lua_check_text_or_string(L, 1);
  1434. t2 = lua_check_text_or_string(L, 2);
  1435. if (t1 && t2) {
  1436. if (t1->len == t2->len) {
  1437. ret = rspamd_lc_cmp(t1->start, t2->start, t1->len);
  1438. }
  1439. else {
  1440. ret = t1->len - t2->len;
  1441. }
  1442. }
  1443. else {
  1444. return luaL_error(L, "invalid arguments");
  1445. }
  1446. lua_pushboolean(L, (ret == 0) ? true : false);
  1447. return 1;
  1448. }
  1449. static int
  1450. lua_util_strequal_caseless_utf8(lua_State *L)
  1451. {
  1452. LUA_TRACE_POINT;
  1453. struct rspamd_lua_text *t1, *t2;
  1454. int ret = -1;
  1455. t1 = lua_check_text_or_string(L, 1);
  1456. t2 = lua_check_text_or_string(L, 2);
  1457. if (t1 && t2) {
  1458. ret = rspamd_utf8_strcmp_sizes(t1->start, t1->len, t2->start, t2->len);
  1459. }
  1460. else {
  1461. return luaL_error(L, "invalid arguments");
  1462. }
  1463. lua_pushboolean(L, (ret == 0) ? true : false);
  1464. return 1;
  1465. }
  1466. static int
  1467. lua_util_get_ticks(lua_State *L)
  1468. {
  1469. LUA_TRACE_POINT;
  1470. double ticks;
  1471. gboolean rdtsc = FALSE;
  1472. if (lua_isboolean(L, 1)) {
  1473. rdtsc = lua_toboolean(L, 1);
  1474. }
  1475. ticks = rspamd_get_ticks(rdtsc);
  1476. lua_pushnumber(L, ticks);
  1477. return 1;
  1478. }
  1479. static int
  1480. lua_util_get_time(lua_State *L)
  1481. {
  1482. LUA_TRACE_POINT;
  1483. lua_pushnumber(L, ev_time());
  1484. return 1;
  1485. }
  1486. static int
  1487. lua_util_time_to_string(lua_State *L)
  1488. {
  1489. LUA_TRACE_POINT;
  1490. double seconds;
  1491. char timebuf[128];
  1492. if (lua_isnumber(L, 1)) {
  1493. seconds = lua_tonumber(L, 1);
  1494. }
  1495. else {
  1496. seconds = ev_time();
  1497. }
  1498. rspamd_http_date_format(timebuf, sizeof(timebuf), seconds);
  1499. lua_pushstring(L, timebuf);
  1500. return 1;
  1501. }
  1502. static int
  1503. lua_util_stat(lua_State *L)
  1504. {
  1505. LUA_TRACE_POINT;
  1506. const char *fpath;
  1507. struct stat st;
  1508. fpath = luaL_checkstring(L, 1);
  1509. if (fpath) {
  1510. if (stat(fpath, &st) == -1) {
  1511. lua_pushstring(L, strerror(errno));
  1512. lua_pushnil(L);
  1513. }
  1514. else {
  1515. lua_pushnil(L);
  1516. lua_createtable(L, 0, 3);
  1517. lua_pushstring(L, "size");
  1518. lua_pushinteger(L, st.st_size);
  1519. lua_settable(L, -3);
  1520. lua_pushstring(L, "mtime");
  1521. lua_pushinteger(L, st.st_mtime);
  1522. lua_settable(L, -3);
  1523. lua_pushstring(L, "type");
  1524. if (S_ISREG(st.st_mode)) {
  1525. lua_pushstring(L, "regular");
  1526. }
  1527. else if (S_ISDIR(st.st_mode)) {
  1528. lua_pushstring(L, "directory");
  1529. }
  1530. else {
  1531. lua_pushstring(L, "special");
  1532. }
  1533. lua_settable(L, -3);
  1534. }
  1535. }
  1536. else {
  1537. return luaL_error(L, "invalid arguments");
  1538. }
  1539. return 2;
  1540. }
  1541. static int
  1542. lua_util_unlink(lua_State *L)
  1543. {
  1544. LUA_TRACE_POINT;
  1545. const char *fpath;
  1546. int ret;
  1547. fpath = luaL_checkstring(L, 1);
  1548. if (fpath) {
  1549. ret = unlink(fpath);
  1550. if (ret == -1) {
  1551. lua_pushboolean(L, false);
  1552. lua_pushstring(L, strerror(errno));
  1553. return 2;
  1554. }
  1555. lua_pushboolean(L, true);
  1556. }
  1557. else {
  1558. return luaL_error(L, "invalid arguments");
  1559. }
  1560. return 1;
  1561. }
  1562. static int
  1563. lua_util_lock_file(lua_State *L)
  1564. {
  1565. LUA_TRACE_POINT;
  1566. const char *fpath;
  1567. int fd = -1;
  1568. gboolean own = FALSE;
  1569. #if !HAVE_FLOCK
  1570. struct flock fl = {
  1571. .l_type = F_WRLCK,
  1572. .l_whence = SEEK_SET,
  1573. .l_start = 0,
  1574. .l_len = 0};
  1575. #endif
  1576. fpath = luaL_checkstring(L, 1);
  1577. if (fpath) {
  1578. if (lua_isnumber(L, 2)) {
  1579. fd = lua_tointeger(L, 2);
  1580. }
  1581. else {
  1582. fd = open(fpath, O_RDONLY);
  1583. own = TRUE;
  1584. }
  1585. if (fd == -1) {
  1586. lua_pushnil(L);
  1587. lua_pushstring(L, strerror(errno));
  1588. return 2;
  1589. }
  1590. #if HAVE_FLOCK
  1591. if (flock(fd, LOCK_EX) == -1) {
  1592. #else
  1593. if (fcntl(fd, F_SETLKW, &fl) == -1) {
  1594. #endif
  1595. lua_pushnil(L);
  1596. lua_pushstring(L, strerror(errno));
  1597. if (own) {
  1598. close(fd);
  1599. }
  1600. return 2;
  1601. }
  1602. lua_pushinteger(L, fd);
  1603. }
  1604. else {
  1605. return luaL_error(L, "invalid arguments");
  1606. }
  1607. return 1;
  1608. }
  1609. static int
  1610. lua_util_unlock_file(lua_State *L)
  1611. {
  1612. LUA_TRACE_POINT;
  1613. int fd = -1, ret, serrno;
  1614. gboolean do_close = TRUE;
  1615. #if !HAVE_FLOCK
  1616. struct flock fl = {
  1617. .l_type = F_UNLCK,
  1618. .l_whence = SEEK_SET,
  1619. .l_start = 0,
  1620. .l_len = 0};
  1621. #endif
  1622. if (lua_isnumber(L, 1)) {
  1623. fd = lua_tointeger(L, 1);
  1624. if (lua_isboolean(L, 2)) {
  1625. do_close = lua_toboolean(L, 2);
  1626. }
  1627. #if HAVE_FLOCK
  1628. ret = flock(fd, LOCK_UN);
  1629. #else
  1630. ret = fcntl(fd, F_SETLKW, &fl);
  1631. #endif
  1632. if (do_close) {
  1633. serrno = errno;
  1634. close(fd);
  1635. errno = serrno;
  1636. }
  1637. if (ret == -1) {
  1638. lua_pushboolean(L, false);
  1639. lua_pushstring(L, strerror(errno));
  1640. return 2;
  1641. }
  1642. lua_pushboolean(L, true);
  1643. }
  1644. else {
  1645. return luaL_error(L, "invalid arguments");
  1646. }
  1647. return 1;
  1648. }
  1649. static int
  1650. lua_util_create_file(lua_State *L)
  1651. {
  1652. LUA_TRACE_POINT;
  1653. int fd, mode = 00644;
  1654. const char *fpath;
  1655. fpath = luaL_checkstring(L, 1);
  1656. if (fpath) {
  1657. if (lua_isnumber(L, 2)) {
  1658. mode = lua_tointeger(L, 2);
  1659. }
  1660. fd = rspamd_file_xopen(fpath, O_RDWR | O_CREAT | O_TRUNC, mode, 0);
  1661. if (fd == -1) {
  1662. lua_pushnil(L);
  1663. lua_pushstring(L, strerror(errno));
  1664. return 2;
  1665. }
  1666. lua_pushinteger(L, fd);
  1667. }
  1668. else {
  1669. return luaL_error(L, "invalid arguments");
  1670. }
  1671. return 1;
  1672. }
  1673. static int
  1674. lua_util_close_file(lua_State *L)
  1675. {
  1676. LUA_TRACE_POINT;
  1677. int fd = -1;
  1678. if (lua_isnumber(L, 1)) {
  1679. fd = lua_tointeger(L, 1);
  1680. if (close(fd) == -1) {
  1681. lua_pushboolean(L, false);
  1682. lua_pushstring(L, strerror(errno));
  1683. return 2;
  1684. }
  1685. lua_pushboolean(L, true);
  1686. }
  1687. else {
  1688. return luaL_error(L, "invalid arguments");
  1689. }
  1690. return 1;
  1691. }
  1692. static int
  1693. lua_util_random_hex(lua_State *L)
  1694. {
  1695. LUA_TRACE_POINT;
  1696. char *buf;
  1697. int buflen;
  1698. buflen = lua_tointeger(L, 1);
  1699. if (buflen <= 0) {
  1700. return luaL_error(L, "invalid arguments");
  1701. }
  1702. buf = g_malloc(buflen);
  1703. rspamd_random_hex(buf, buflen);
  1704. lua_pushlstring(L, buf, buflen);
  1705. g_free(buf);
  1706. return 1;
  1707. }
  1708. static int
  1709. lua_util_zstd_compress(lua_State *L)
  1710. {
  1711. return lua_compress_zstd_compress(L);
  1712. }
  1713. static int
  1714. lua_util_zstd_decompress(lua_State *L)
  1715. {
  1716. return lua_compress_zstd_decompress(L);
  1717. }
  1718. static int
  1719. lua_util_gzip_compress(lua_State *L)
  1720. {
  1721. return lua_compress_zlib_compress(L);
  1722. }
  1723. static int
  1724. lua_util_gzip_decompress(lua_State *L)
  1725. {
  1726. return lua_compress_zlib_decompress(L, true);
  1727. }
  1728. static int
  1729. lua_util_inflate(lua_State *L)
  1730. {
  1731. return lua_compress_zlib_decompress(L, false);
  1732. }
  1733. static int
  1734. lua_util_normalize_prob(lua_State *L)
  1735. {
  1736. LUA_TRACE_POINT;
  1737. double x, bias = 0.5;
  1738. x = lua_tonumber(L, 1);
  1739. if (lua_type(L, 2) == LUA_TNUMBER) {
  1740. bias = lua_tonumber(L, 2);
  1741. }
  1742. lua_pushnumber(L, rspamd_normalize_probability(x, bias));
  1743. return 1;
  1744. }
  1745. static int
  1746. lua_util_caseless_hash(lua_State *L)
  1747. {
  1748. LUA_TRACE_POINT;
  1749. uint64_t seed = 0xdeadbabe, h;
  1750. struct rspamd_lua_text *t = NULL;
  1751. int64_t *r;
  1752. t = lua_check_text_or_string(L, 1);
  1753. if (t == NULL || t->start == NULL) {
  1754. return luaL_error(L, "invalid arguments");
  1755. }
  1756. if (lua_type(L, 2) == LUA_TNUMBER) {
  1757. seed = lua_tointeger(L, 2);
  1758. }
  1759. else if (lua_type(L, 2) == LUA_TUSERDATA) {
  1760. seed = lua_check_int64(L, 2);
  1761. }
  1762. h = rspamd_icase_hash(t->start, t->len, seed);
  1763. r = lua_newuserdata(L, sizeof(*r));
  1764. *r = h;
  1765. rspamd_lua_setclass(L, rspamd_int64_classname, -1);
  1766. return 1;
  1767. }
  1768. static int
  1769. lua_util_caseless_hash_fast(lua_State *L)
  1770. {
  1771. LUA_TRACE_POINT;
  1772. uint64_t seed = 0xdeadbabe, h;
  1773. struct rspamd_lua_text *t = NULL;
  1774. union {
  1775. uint64_t i;
  1776. double d;
  1777. } u;
  1778. t = lua_check_text_or_string(L, 1);
  1779. if (t == NULL || t->start == NULL) {
  1780. return luaL_error(L, "invalid arguments");
  1781. }
  1782. if (lua_type(L, 2) == LUA_TNUMBER) {
  1783. seed = lua_tointeger(L, 2);
  1784. }
  1785. else if (lua_type(L, 2) == LUA_TUSERDATA) {
  1786. seed = lua_check_int64(L, 2);
  1787. }
  1788. /*
  1789. * Here, we loose entropy from 64 bits to 52 bits roughly, however,
  1790. * it is still fine for practical applications
  1791. */
  1792. h = rspamd_icase_hash(t->start, t->len, seed);
  1793. u.i = G_GUINT64_CONSTANT(0x3FF) << 52 | h >> 12;
  1794. lua_pushnumber(L, u.d - 1.0);
  1795. return 1;
  1796. }
  1797. static int
  1798. lua_util_is_utf_spoofed(lua_State *L)
  1799. {
  1800. LUA_TRACE_POINT;
  1801. gsize l1, l2;
  1802. int ret, nres = 2;
  1803. const char *s1 = lua_tolstring(L, 1, &l1),
  1804. *s2 = lua_tolstring(L, 2, &l2);
  1805. static USpoofChecker *spc, *spc_sgl;
  1806. UErrorCode uc_err = U_ZERO_ERROR;
  1807. if (s1 && s2) {
  1808. if (spc == NULL) {
  1809. spc = uspoof_open(&uc_err);
  1810. if (uc_err != U_ZERO_ERROR) {
  1811. msg_err("cannot init spoof checker: %s", u_errorName(uc_err));
  1812. lua_pushboolean(L, false);
  1813. return 1;
  1814. }
  1815. }
  1816. ret = uspoof_areConfusableUTF8(spc, s1, l1, s2, l2, &uc_err);
  1817. }
  1818. else if (s1) {
  1819. /* We have just s1, not s2 */
  1820. if (spc_sgl == NULL) {
  1821. spc_sgl = uspoof_open(&uc_err);
  1822. if (uc_err != U_ZERO_ERROR) {
  1823. msg_err("cannot init spoof checker: %s", u_errorName(uc_err));
  1824. lua_pushboolean(L, false);
  1825. return 1;
  1826. }
  1827. uspoof_setChecks(spc_sgl,
  1828. USPOOF_INVISIBLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE,
  1829. &uc_err);
  1830. if (uc_err != U_ZERO_ERROR) {
  1831. msg_err("Cannot set proper checks for uspoof: %s", u_errorName(uc_err));
  1832. lua_pushboolean(L, false);
  1833. uspoof_close(spc);
  1834. return 1;
  1835. }
  1836. }
  1837. ret = uspoof_checkUTF8(spc_sgl, s1, l1, NULL, &uc_err);
  1838. }
  1839. else {
  1840. return luaL_error(L, "invalid arguments");
  1841. }
  1842. lua_pushboolean(L, !!(ret != 0));
  1843. switch (ret) {
  1844. case 0:
  1845. nres = 1;
  1846. break;
  1847. case USPOOF_SINGLE_SCRIPT_CONFUSABLE:
  1848. lua_pushstring(L, "single");
  1849. break;
  1850. case USPOOF_MIXED_SCRIPT_CONFUSABLE:
  1851. lua_pushstring(L, "multiple");
  1852. break;
  1853. case USPOOF_WHOLE_SCRIPT_CONFUSABLE:
  1854. lua_pushstring(L, "whole");
  1855. break;
  1856. default:
  1857. lua_pushstring(L, "unknown");
  1858. break;
  1859. }
  1860. return nres;
  1861. }
  1862. static int
  1863. lua_util_is_utf_mixed_script(lua_State *L)
  1864. {
  1865. LUA_TRACE_POINT;
  1866. gsize len_of_string;
  1867. const unsigned char *string_to_check = lua_tolstring(L, 1, &len_of_string);
  1868. UScriptCode last_script_code = USCRIPT_INVALID_CODE;
  1869. UErrorCode uc_err = U_ZERO_ERROR;
  1870. if (string_to_check) {
  1871. uint index = 0;
  1872. UChar32 char_to_check = 0;
  1873. while (index < len_of_string) {
  1874. U8_NEXT(string_to_check, index, len_of_string, char_to_check);
  1875. if (char_to_check < 0) {
  1876. return luaL_error(L, "passed string is not valid utf");
  1877. }
  1878. UScriptCode current_script_code = uscript_getScript(char_to_check, &uc_err);
  1879. if (uc_err != U_ZERO_ERROR) {
  1880. msg_err("cannot get unicode script for character, error: %s",
  1881. u_errorName(uc_err));
  1882. lua_pushboolean(L, false);
  1883. return 1;
  1884. }
  1885. if (current_script_code != USCRIPT_COMMON &&
  1886. current_script_code != USCRIPT_INHERITED) {
  1887. if (last_script_code == USCRIPT_INVALID_CODE) {
  1888. last_script_code = current_script_code;
  1889. }
  1890. else {
  1891. if (last_script_code != current_script_code) {
  1892. lua_pushboolean(L, true);
  1893. return 1;
  1894. }
  1895. }
  1896. }
  1897. }
  1898. }
  1899. else {
  1900. return luaL_error(L, "invalid arguments");
  1901. }
  1902. lua_pushboolean(L, false);
  1903. return 1;
  1904. }
  1905. static int
  1906. lua_util_get_string_stats(lua_State *L)
  1907. {
  1908. LUA_TRACE_POINT;
  1909. int num_of_digits = 0, num_of_letters = 0;
  1910. struct rspamd_lua_text *t;
  1911. t = lua_check_text_or_string(L, 1);
  1912. if (t) {
  1913. const char *p = t->start, *end = t->start + t->len;
  1914. while (p < end) {
  1915. if (g_ascii_isdigit(*p)) {
  1916. num_of_digits++;
  1917. }
  1918. else if (g_ascii_isalpha(*p)) {
  1919. num_of_letters++;
  1920. }
  1921. p++;
  1922. }
  1923. }
  1924. else {
  1925. return luaL_error(L, "invalid arguments");
  1926. }
  1927. lua_createtable(L, 0, 2);
  1928. lua_pushstring(L, "digits");
  1929. lua_pushinteger(L, num_of_digits);
  1930. lua_settable(L, -3);
  1931. lua_pushstring(L, "letters");
  1932. lua_pushinteger(L, num_of_letters);
  1933. lua_settable(L, -3);
  1934. return 1;
  1935. }
  1936. static int
  1937. lua_util_is_utf_outside_range(lua_State *L)
  1938. {
  1939. LUA_TRACE_POINT;
  1940. int ret;
  1941. struct rspamd_lua_text *t = lua_check_text_or_string(L, 1);
  1942. uint32_t range_start = lua_tointeger(L, 2);
  1943. uint32_t range_end = lua_tointeger(L, 3);
  1944. static rspamd_lru_hash_t *validators;
  1945. if (validators == NULL) {
  1946. validators = rspamd_lru_hash_new_full(16, g_free, (GDestroyNotify) uspoof_close, g_int64_hash, g_int64_equal);
  1947. }
  1948. if (t) {
  1949. uint64_t hash_key = (uint64_t) range_end << 32 || range_start;
  1950. USpoofChecker *validator = rspamd_lru_hash_lookup(validators, &hash_key, 0);
  1951. UErrorCode uc_err = U_ZERO_ERROR;
  1952. if (validator == NULL) {
  1953. USet *allowed_chars;
  1954. uint64_t *creation_hash_key = g_malloc(sizeof(uint64_t));
  1955. *creation_hash_key = hash_key;
  1956. validator = uspoof_open(&uc_err);
  1957. if (uc_err != U_ZERO_ERROR) {
  1958. msg_err("cannot init spoof checker: %s", u_errorName(uc_err));
  1959. lua_pushboolean(L, false);
  1960. uspoof_close(validator);
  1961. g_free(creation_hash_key);
  1962. return 1;
  1963. }
  1964. allowed_chars = uset_openEmpty();
  1965. uset_addRange(allowed_chars, range_start, range_end);
  1966. uspoof_setAllowedChars(validator, allowed_chars, &uc_err);
  1967. uspoof_setChecks(validator,
  1968. USPOOF_CHAR_LIMIT | USPOOF_ANY_CASE, &uc_err);
  1969. uset_close(allowed_chars);
  1970. if (uc_err != U_ZERO_ERROR) {
  1971. msg_err("Cannot configure uspoof: %s", u_errorName(uc_err));
  1972. lua_pushboolean(L, false);
  1973. uspoof_close(validator);
  1974. g_free(creation_hash_key);
  1975. return 1;
  1976. }
  1977. rspamd_lru_hash_insert(validators, creation_hash_key, validator,
  1978. 0, 0);
  1979. }
  1980. int32_t pos = 0;
  1981. ret = uspoof_checkUTF8(validator, t->start, t->len, &pos,
  1982. &uc_err);
  1983. }
  1984. else {
  1985. return luaL_error(L, "invalid arguments");
  1986. }
  1987. lua_pushboolean(L, !!(ret != 0));
  1988. return 1;
  1989. }
  1990. static int
  1991. lua_util_get_hostname(lua_State *L)
  1992. {
  1993. LUA_TRACE_POINT;
  1994. char *hostbuf;
  1995. gsize hostlen;
  1996. hostlen = sysconf(_SC_HOST_NAME_MAX);
  1997. if (hostlen <= 0) {
  1998. hostlen = 256;
  1999. }
  2000. else {
  2001. hostlen++;
  2002. }
  2003. hostbuf = g_alloca(hostlen);
  2004. memset(hostbuf, 0, hostlen);
  2005. gethostname(hostbuf, hostlen - 1);
  2006. lua_pushstring(L, hostbuf);
  2007. return 1;
  2008. }
  2009. static int
  2010. lua_util_parse_content_type(lua_State *L)
  2011. {
  2012. return lua_parsers_parse_content_type(L);
  2013. }
  2014. static int
  2015. lua_util_mime_header_encode(lua_State *L)
  2016. {
  2017. LUA_TRACE_POINT;
  2018. gsize len;
  2019. const char *hdr = luaL_checklstring(L, 1, &len);
  2020. char *encoded;
  2021. if (!hdr) {
  2022. return luaL_error(L, "invalid arguments");
  2023. }
  2024. encoded = rspamd_mime_header_encode(hdr, len);
  2025. lua_pushstring(L, encoded);
  2026. g_free(encoded);
  2027. return 1;
  2028. }
  2029. static int
  2030. lua_util_is_valid_utf8(lua_State *L)
  2031. {
  2032. LUA_TRACE_POINT;
  2033. struct rspamd_lua_text *t = lua_check_text_or_string(L, 1);
  2034. if (t) {
  2035. goffset error_offset = rspamd_fast_utf8_validate(t->start, t->len);
  2036. if (error_offset == 0) {
  2037. lua_pushboolean(L, true);
  2038. }
  2039. else {
  2040. lua_pushboolean(L, false);
  2041. lua_pushinteger(L, error_offset);
  2042. return 2;
  2043. }
  2044. }
  2045. else {
  2046. return luaL_error(L, "invalid arguments");
  2047. }
  2048. return 1;
  2049. }
  2050. static int
  2051. lua_util_has_obscured_unicode(lua_State *L)
  2052. {
  2053. LUA_TRACE_POINT;
  2054. int32_t i = 0, prev_i;
  2055. UChar32 uc;
  2056. struct rspamd_lua_text *t = lua_check_text_or_string(L, 1);
  2057. while (i < t->len) {
  2058. prev_i = i;
  2059. U8_NEXT(t->start, i, t->len, uc);
  2060. if (uc > 0) {
  2061. if (IS_OBSCURED_CHAR(uc)) {
  2062. lua_pushboolean(L, true);
  2063. lua_pushinteger(L, uc); /* Character */
  2064. lua_pushinteger(L, prev_i); /* Offset */
  2065. return 3;
  2066. }
  2067. }
  2068. }
  2069. lua_pushboolean(L, false);
  2070. return 1;
  2071. }
  2072. static int
  2073. lua_util_readline(lua_State *L)
  2074. {
  2075. LUA_TRACE_POINT;
  2076. const char *prompt = "";
  2077. char *input = NULL;
  2078. if (lua_type(L, 1) == LUA_TSTRING) {
  2079. prompt = lua_tostring(L, 1);
  2080. }
  2081. #ifdef WITH_LUA_REPL
  2082. static Replxx *rx_instance = NULL;
  2083. if (rx_instance == NULL) {
  2084. rx_instance = replxx_init();
  2085. /* See https://github.com/AmokHuginnsson/replxx/issues/137 */
  2086. replxx_history_add(rx_instance, "");
  2087. }
  2088. input = (char *) replxx_input(rx_instance, prompt);
  2089. if (input) {
  2090. lua_pushstring(L, input);
  2091. }
  2092. else {
  2093. lua_pushnil(L);
  2094. }
  2095. #else
  2096. size_t linecap = 0;
  2097. ssize_t linelen;
  2098. fprintf(stdout, "%s ", prompt);
  2099. linelen = getline(&input, &linecap, stdin);
  2100. if (linelen > 0) {
  2101. if (input[linelen - 1] == '\n') {
  2102. linelen--;
  2103. }
  2104. lua_pushlstring(L, input, linelen);
  2105. free(input);
  2106. }
  2107. else {
  2108. lua_pushnil(L);
  2109. }
  2110. #endif
  2111. return 1;
  2112. }
  2113. static int
  2114. lua_util_readpassphrase(lua_State *L)
  2115. {
  2116. LUA_TRACE_POINT;
  2117. char test_password[8192];
  2118. gsize r;
  2119. r = rspamd_read_passphrase(test_password, sizeof(test_password), 0, NULL);
  2120. if (r > 0) {
  2121. lua_pushlstring(L, test_password, r);
  2122. }
  2123. else {
  2124. lua_pushnil(L);
  2125. }
  2126. /* In fact, we still pass it to Lua which is not very safe */
  2127. rspamd_explicit_memzero(test_password, sizeof(test_password));
  2128. return 1;
  2129. }
  2130. static int
  2131. lua_util_file_exists(lua_State *L)
  2132. {
  2133. LUA_TRACE_POINT;
  2134. const char *fname = luaL_checkstring(L, 1);
  2135. int serrno;
  2136. if (fname) {
  2137. if (access(fname, R_OK) == -1) {
  2138. serrno = errno;
  2139. lua_pushboolean(L, false);
  2140. lua_pushstring(L, strerror(serrno));
  2141. }
  2142. else {
  2143. lua_pushboolean(L, true);
  2144. lua_pushnil(L);
  2145. }
  2146. }
  2147. else {
  2148. return luaL_error(L, "invalid arguments");
  2149. }
  2150. return 2;
  2151. }
  2152. static int
  2153. lua_util_mkdir(lua_State *L)
  2154. {
  2155. LUA_TRACE_POINT;
  2156. const char *dname = luaL_checkstring(L, 1);
  2157. gboolean recursive = FALSE;
  2158. int r = -1;
  2159. if (dname) {
  2160. if (lua_isboolean(L, 2)) {
  2161. recursive = lua_toboolean(L, 2);
  2162. }
  2163. if (recursive) {
  2164. char path[PATH_MAX];
  2165. gsize len, i;
  2166. len = rspamd_strlcpy(path, dname, sizeof(path));
  2167. /* Strip last / */
  2168. if (path[len - 1] == '/') {
  2169. path[len - 1] = '\0';
  2170. len--;
  2171. }
  2172. for (i = 1; i < len; i++) {
  2173. if (path[i] == '/') {
  2174. path[i] = '\0';
  2175. errno = 0;
  2176. r = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  2177. if (r == -1 && errno != EEXIST) {
  2178. break;
  2179. }
  2180. path[i] = '/';
  2181. }
  2182. }
  2183. /* Final path component */
  2184. r = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  2185. }
  2186. else {
  2187. r = mkdir(dname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  2188. }
  2189. if (r == -1 && errno != EEXIST) {
  2190. lua_pushboolean(L, false);
  2191. lua_pushstring(L, strerror(errno));
  2192. return 2;
  2193. }
  2194. lua_pushboolean(L, true);
  2195. }
  2196. else {
  2197. return luaL_error(L, "invalid arguments");
  2198. }
  2199. return 1;
  2200. }
  2201. static int
  2202. lua_util_umask(lua_State *L)
  2203. {
  2204. LUA_TRACE_POINT;
  2205. mode_t mask = 0, old;
  2206. if (lua_type(L, 1) == LUA_TSTRING) {
  2207. const char *str = lua_tostring(L, 1);
  2208. if (str[0] == '0') {
  2209. /* e.g. '022' */
  2210. mask = strtol(str, NULL, 8);
  2211. }
  2212. else {
  2213. /* XXX: implement modestring parsing at some point */
  2214. return luaL_error(L, "invalid arguments");
  2215. }
  2216. }
  2217. else if (lua_type(L, 1) == LUA_TNUMBER) {
  2218. mask = lua_tointeger(L, 1);
  2219. }
  2220. else {
  2221. return luaL_error(L, "invalid arguments");
  2222. }
  2223. old = umask(mask);
  2224. lua_pushinteger(L, old);
  2225. return 1;
  2226. }
  2227. static int
  2228. lua_util_isatty(lua_State *L)
  2229. {
  2230. LUA_TRACE_POINT;
  2231. if (isatty(STDOUT_FILENO)) {
  2232. lua_pushboolean(L, true);
  2233. }
  2234. else {
  2235. lua_pushboolean(L, false);
  2236. }
  2237. return 1;
  2238. }
  2239. /* Backport from Lua 5.3 */
  2240. /******************************************************************************
  2241. * Copyright (C) 1994-2016 Lua.org, PUC-Rio.
  2242. *
  2243. * Permission is hereby granted, free of charge, to any person obtaining
  2244. * a copy of this software and associated documentation files (the
  2245. * "Software"), to deal in the Software without restriction, including
  2246. * without limitation the rights to use, copy, modify, merge, publish,
  2247. * distribute, sublicense, and/or sell copies of the Software, and to
  2248. * permit persons to whom the Software is furnished to do so, subject to
  2249. * the following conditions:
  2250. *
  2251. * The above copyright notice and this permission notice shall be
  2252. * included in all copies or substantial portions of the Software.
  2253. *
  2254. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2255. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2256. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  2257. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  2258. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  2259. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  2260. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  2261. ******************************************************************************/
  2262. /*
  2263. ** {======================================================
  2264. ** PACK/UNPACK
  2265. ** =======================================================
  2266. */
  2267. /* value used for padding */
  2268. #if !defined(LUA_PACKPADBYTE)
  2269. #define LUA_PACKPADBYTE 0x00
  2270. #endif
  2271. /* maximum size for the binary representation of an integer */
  2272. #define MAXINTSIZE 16
  2273. /* number of bits in a character */
  2274. #define NB CHAR_BIT
  2275. /* mask for one character (NB 1's) */
  2276. #define MC ((1 << NB) - 1)
  2277. /* size of a lua_Integer */
  2278. #define SZINT ((int) sizeof(lua_Integer))
  2279. #define MAX_SIZET ((size_t) (~(size_t) 0))
  2280. #define MAXSIZE \
  2281. (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t) (INT_MAX))
  2282. /* dummy union to get native endianness */
  2283. static const union {
  2284. int dummy;
  2285. char little; /* true if machine is little endian */
  2286. } nativeendian = {1};
  2287. /* dummy structure to get native alignment requirements */
  2288. struct cD {
  2289. char c;
  2290. union {
  2291. double d;
  2292. void *p;
  2293. lua_Integer i;
  2294. lua_Number n;
  2295. } u;
  2296. };
  2297. #define MAXALIGN (offsetof(struct cD, u))
  2298. /*
  2299. ** Union for serializing floats
  2300. */
  2301. typedef union Ftypes {
  2302. float f;
  2303. double d;
  2304. lua_Number n;
  2305. char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
  2306. } Ftypes;
  2307. /*
  2308. ** information to pack/unpack stuff
  2309. */
  2310. typedef struct Header {
  2311. lua_State *L;
  2312. int islittle;
  2313. int maxalign;
  2314. } Header;
  2315. /*
  2316. ** options for pack/unpack
  2317. */
  2318. typedef enum KOption {
  2319. Kint, /* signed integers */
  2320. Kuint, /* unsigned integers */
  2321. Kfloat, /* floating-point numbers */
  2322. Kchar, /* fixed-length strings */
  2323. Kstring, /* strings with prefixed length */
  2324. Kzstr, /* zero-terminated strings */
  2325. Kpadding, /* padding */
  2326. Kpaddalign, /* padding for alignment */
  2327. Knop /* no-op (configuration or spaces) */
  2328. } KOption;
  2329. #if LUA_VERSION_NUM <= 502
  2330. #define lua_Unsigned size_t
  2331. #endif
  2332. #if LUA_VERSION_NUM < 502
  2333. #define lua_Unsigned size_t
  2334. typedef struct luaL_Buffer_53 {
  2335. luaL_Buffer b; /* make incorrect code crash! */
  2336. char *ptr;
  2337. size_t nelems;
  2338. size_t capacity;
  2339. lua_State *L2;
  2340. } luaL_Buffer_53;
  2341. #define luaL_Buffer luaL_Buffer_53
  2342. #define COMPAT53_PREFIX lua
  2343. #undef COMPAT53_API
  2344. #if defined(__GNUC__) || defined(__clang__)
  2345. #define COMPAT53_API __attribute__((__unused__)) static
  2346. #else
  2347. #define COMPAT53_API static
  2348. #endif
  2349. #define COMPAT53_CONCAT_HELPER(a, b) a##b
  2350. #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
  2351. #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
  2352. COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B);
  2353. #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
  2354. COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s);
  2355. #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
  2356. COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l);
  2357. #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
  2358. COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B);
  2359. #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
  2360. COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B);
  2361. #undef luaL_buffinitsize
  2362. #define luaL_buffinitsize(L, B, s) \
  2363. (luaL_buffinit(L, B), luaL_prepbuffsize(B, s))
  2364. #undef luaL_prepbuffer
  2365. #define luaL_prepbuffer(B) \
  2366. luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
  2367. #undef luaL_addchar
  2368. #define luaL_addchar(B, c) \
  2369. ((void) ((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \
  2370. ((B)->ptr[(B)->nelems++] = (c)))
  2371. #undef luaL_addsize
  2372. #define luaL_addsize(B, s) \
  2373. ((B)->nelems += (s))
  2374. #undef luaL_addstring
  2375. #define luaL_addstring(B, s) \
  2376. luaL_addlstring(B, s, strlen(s))
  2377. #undef luaL_pushresultsize
  2378. #define luaL_pushresultsize(B, s) \
  2379. (luaL_addsize(B, s), luaL_pushresult(B))
  2380. COMPAT53_API void
  2381. luaL_buffinit(lua_State *L, luaL_Buffer_53 *B)
  2382. {
  2383. /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
  2384. B->b.p = NULL;
  2385. B->b.L = NULL;
  2386. B->b.lvl = 0;
  2387. /* reuse the buffer from the 5.1-style luaL_Buffer though! */
  2388. B->ptr = B->b.buffer;
  2389. B->nelems = 0;
  2390. B->capacity = LUAL_BUFFERSIZE;
  2391. B->L2 = L;
  2392. }
  2393. COMPAT53_API char *
  2394. luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s)
  2395. {
  2396. if (B->capacity - B->nelems < s) { /* needs to grow */
  2397. char *newptr = NULL;
  2398. size_t newcap = B->capacity * 2;
  2399. if (newcap - B->nelems < s)
  2400. newcap = B->nelems + s;
  2401. if (newcap < B->capacity) /* overflow */
  2402. luaL_error(B->L2, "buffer too large");
  2403. newptr = (char *) lua_newuserdata(B->L2, newcap);
  2404. memcpy(newptr, B->ptr, B->nelems);
  2405. if (B->ptr != B->b.buffer) {
  2406. lua_replace(B->L2, -2); /* remove old buffer */
  2407. }
  2408. B->ptr = newptr;
  2409. B->capacity = newcap;
  2410. }
  2411. return B->ptr + B->nelems;
  2412. }
  2413. COMPAT53_API void
  2414. luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l)
  2415. {
  2416. memcpy(luaL_prepbuffsize(B, l), s, l);
  2417. luaL_addsize(B, l);
  2418. }
  2419. COMPAT53_API void
  2420. luaL_addvalue(luaL_Buffer_53 *B)
  2421. {
  2422. size_t len = 0;
  2423. const char *s = lua_tolstring(B->L2, -1, &len);
  2424. if (!s)
  2425. luaL_error(B->L2, "cannot convert value to string");
  2426. if (B->ptr != B->b.buffer) {
  2427. lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
  2428. }
  2429. luaL_addlstring(B, s, len);
  2430. lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
  2431. }
  2432. COMPAT53_API void
  2433. luaL_pushresult(luaL_Buffer_53 *B)
  2434. {
  2435. lua_pushlstring(B->L2, B->ptr, B->nelems);
  2436. if (B->ptr != B->b.buffer) {
  2437. lua_replace(B->L2, -2); /* remove userdata buffer */
  2438. }
  2439. }
  2440. #endif
  2441. /*
  2442. ** Read an integer numeral from string 'fmt' or return 'df' if
  2443. ** there is no numeral
  2444. */
  2445. static int
  2446. digit(int c)
  2447. {
  2448. return '0' <= c && c <= '9';
  2449. }
  2450. static int
  2451. getnum(const char **fmt, int df)
  2452. {
  2453. if (!digit(**fmt)) /* no number? */
  2454. return df; /* return default value */
  2455. else {
  2456. int a = 0;
  2457. do {
  2458. a = a * 10 + (*((*fmt)++) - '0');
  2459. } while (digit(**fmt) && a <= ((int) MAXSIZE - 9) / 10);
  2460. return a;
  2461. }
  2462. }
  2463. /*
  2464. ** Read an integer numeral and raises an error if it is larger
  2465. ** than the maximum size for integers.
  2466. */
  2467. static int
  2468. getnumlimit(Header *h, const char **fmt, int df)
  2469. {
  2470. int sz = getnum(fmt, df);
  2471. if (sz > MAXINTSIZE || sz <= 0)
  2472. luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
  2473. sz, MAXINTSIZE);
  2474. return sz;
  2475. }
  2476. /*
  2477. ** Initialize Header
  2478. */
  2479. static void
  2480. initheader(lua_State *L, Header *h)
  2481. {
  2482. h->L = L;
  2483. h->islittle = nativeendian.little;
  2484. h->maxalign = 1;
  2485. }
  2486. /*
  2487. ** Read and classify next option. 'size' is filled with option's size.
  2488. */
  2489. static KOption
  2490. getoption(Header *h, const char **fmt, int *size)
  2491. {
  2492. int opt = *((*fmt)++);
  2493. *size = 0; /* default */
  2494. switch (opt) {
  2495. case 'b':
  2496. *size = sizeof(char);
  2497. return Kint;
  2498. case 'B':
  2499. *size = sizeof(char);
  2500. return Kuint;
  2501. case 'h':
  2502. *size = sizeof(short);
  2503. return Kint;
  2504. case 'H':
  2505. *size = sizeof(short);
  2506. return Kuint;
  2507. case 'l':
  2508. *size = sizeof(long);
  2509. return Kint;
  2510. case 'L':
  2511. *size = sizeof(long);
  2512. return Kuint;
  2513. case 'j':
  2514. *size = sizeof(lua_Integer);
  2515. return Kint;
  2516. case 'J':
  2517. *size = sizeof(lua_Integer);
  2518. return Kuint;
  2519. case 'T':
  2520. *size = sizeof(size_t);
  2521. return Kuint;
  2522. case 'f':
  2523. *size = sizeof(float);
  2524. return Kfloat;
  2525. case 'd':
  2526. *size = sizeof(double);
  2527. return Kfloat;
  2528. case 'n':
  2529. *size = sizeof(lua_Number);
  2530. return Kfloat;
  2531. case 'i':
  2532. *size = getnumlimit(h, fmt, sizeof(int));
  2533. return Kint;
  2534. case 'I':
  2535. *size = getnumlimit(h, fmt, sizeof(int));
  2536. return Kuint;
  2537. case 's':
  2538. *size = getnumlimit(h, fmt, sizeof(size_t));
  2539. return Kstring;
  2540. case 'c':
  2541. *size = getnum(fmt, -1);
  2542. if (*size == -1)
  2543. luaL_error(h->L, "missing size for format option 'c'");
  2544. return Kchar;
  2545. case 'z':
  2546. return Kzstr;
  2547. case 'x':
  2548. *size = 1;
  2549. return Kpadding;
  2550. case 'X':
  2551. return Kpaddalign;
  2552. case ' ':
  2553. break;
  2554. case '<':
  2555. h->islittle = 1;
  2556. break;
  2557. case '>':
  2558. h->islittle = 0;
  2559. break;
  2560. case '=':
  2561. h->islittle = nativeendian.little;
  2562. break;
  2563. case '!':
  2564. h->maxalign = getnumlimit(h, fmt, MAXALIGN);
  2565. break;
  2566. default:
  2567. luaL_error(h->L, "invalid format option '%c'", opt);
  2568. }
  2569. return Knop;
  2570. }
  2571. /*
  2572. ** Read, classify, and fill other details about the next option.
  2573. ** 'psize' is filled with option's size, 'notoalign' with its
  2574. ** alignment requirements.
  2575. ** Local variable 'size' gets the size to be aligned. (Kpadal option
  2576. ** always gets its full alignment, other options are limited by
  2577. ** the maximum alignment ('maxalign'). Kchar option needs no alignment
  2578. ** despite its size.
  2579. */
  2580. static KOption
  2581. getdetails(Header *h, size_t totalsize,
  2582. const char **fmt, int *psize, int *ntoalign)
  2583. {
  2584. KOption opt = getoption(h, fmt, psize);
  2585. int align = *psize; /* usually, alignment follows size */
  2586. if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
  2587. if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
  2588. luaL_argerror(h->L, 1, "invalid next option for option 'X'");
  2589. }
  2590. if (align <= 1 || opt == Kchar) /* need no alignment? */
  2591. *ntoalign = 0;
  2592. else {
  2593. if (align > h->maxalign) /* enforce maximum alignment */
  2594. align = h->maxalign;
  2595. if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */
  2596. luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
  2597. *ntoalign = (align - (int) (totalsize & (align - 1))) & (align - 1);
  2598. }
  2599. return opt;
  2600. }
  2601. /*
  2602. ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
  2603. ** The final 'if' handles the case when 'size' is larger than
  2604. ** the size of a Lua integer, correcting the extra sign-extension
  2605. ** bytes if necessary (by default they would be zeros).
  2606. */
  2607. static void
  2608. packint(luaL_Buffer *b, lua_Unsigned n,
  2609. int islittle, int size, int neg)
  2610. {
  2611. char *buff = luaL_prepbuffsize(b, size);
  2612. int i;
  2613. buff[islittle ? 0 : size - 1] = (char) (n & MC); /* first byte */
  2614. for (i = 1; i < size; i++) {
  2615. n >>= NB;
  2616. buff[islittle ? i : size - 1 - i] = (char) (n & MC);
  2617. }
  2618. if (neg && size > SZINT) { /* negative number need sign extension? */
  2619. for (i = SZINT; i < size; i++) /* correct extra bytes */
  2620. buff[islittle ? i : size - 1 - i] = (char) MC;
  2621. }
  2622. luaL_addsize(b, size); /* add result to buffer */
  2623. }
  2624. /*
  2625. ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
  2626. ** given 'islittle' is different from native endianness.
  2627. */
  2628. static void
  2629. copywithendian(volatile char *dest, volatile const char *src,
  2630. int size, int islittle)
  2631. {
  2632. if (islittle == nativeendian.little) {
  2633. while (size-- != 0)
  2634. *(dest++) = *(src++);
  2635. }
  2636. else {
  2637. dest += size - 1;
  2638. while (size-- != 0)
  2639. *(dest--) = *(src++);
  2640. }
  2641. }
  2642. static int
  2643. lua_util_pack(lua_State *L)
  2644. {
  2645. luaL_Buffer b;
  2646. Header h;
  2647. const char *fmt = luaL_checkstring(L, 1); /* format string */
  2648. int arg = 1; /* current argument to pack */
  2649. size_t totalsize = 0; /* accumulate total size of result */
  2650. initheader(L, &h);
  2651. lua_pushnil(L); /* mark to separate arguments from string buffer */
  2652. luaL_buffinit(L, &b);
  2653. while (*fmt != '\0') {
  2654. int size, ntoalign;
  2655. KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
  2656. totalsize += ntoalign + size;
  2657. while (ntoalign-- > 0)
  2658. luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */
  2659. arg++;
  2660. switch (opt) {
  2661. case Kint: { /* signed integers */
  2662. lua_Integer n = luaL_checkinteger(L, arg);
  2663. if (size < SZINT) { /* need overflow check? */
  2664. lua_Integer lim = (lua_Integer) 1 << ((size * NB) - 1);
  2665. luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
  2666. }
  2667. packint(&b, (lua_Unsigned) n, h.islittle, size, (n < 0));
  2668. break;
  2669. }
  2670. case Kuint: { /* unsigned integers */
  2671. lua_Integer n = luaL_checkinteger(L, arg);
  2672. if (size < SZINT) /* need overflow check? */
  2673. luaL_argcheck(L,
  2674. (lua_Unsigned) n < ((lua_Unsigned) 1 << (size * NB)),
  2675. arg,
  2676. "unsigned overflow");
  2677. packint(&b, (lua_Unsigned) n, h.islittle, size, 0);
  2678. break;
  2679. }
  2680. case Kfloat: { /* floating-point options */
  2681. volatile Ftypes u;
  2682. char *buff = luaL_prepbuffsize(&b, size);
  2683. lua_Number n = luaL_checknumber(L, arg); /* get argument */
  2684. if (size == sizeof(u.f))
  2685. u.f = (float) n; /* copy it into 'u' */
  2686. else if (size == sizeof(u.d))
  2687. u.d = (double) n;
  2688. else
  2689. u.n = n;
  2690. /* move 'u' to final result, correcting endianness if needed */
  2691. copywithendian(buff, u.buff, size, h.islittle);
  2692. luaL_addsize(&b, size);
  2693. break;
  2694. }
  2695. case Kchar: { /* fixed-size string */
  2696. size_t len;
  2697. const char *s = luaL_checklstring(L, arg, &len);
  2698. if ((size_t) size <=
  2699. len) /* string larger than (or equal to) needed? */
  2700. luaL_addlstring(&b,
  2701. s,
  2702. size); /* truncate string to asked size */
  2703. else { /* string smaller than needed */
  2704. luaL_addlstring(&b, s, len); /* add it all */
  2705. while (len++ < (size_t) size) /* pad extra space */
  2706. luaL_addchar(&b, LUA_PACKPADBYTE);
  2707. }
  2708. break;
  2709. }
  2710. case Kstring: { /* strings with length count */
  2711. size_t len;
  2712. const char *s = luaL_checklstring(L, arg, &len);
  2713. luaL_argcheck(L, size >= (int) sizeof(size_t) || len < ((size_t) 1 << (size * NB)),
  2714. arg, "string length does not fit in given size");
  2715. packint(&b,
  2716. (lua_Unsigned) len,
  2717. h.islittle,
  2718. size,
  2719. 0); /* pack length */
  2720. luaL_addlstring(&b, s, len);
  2721. totalsize += len;
  2722. break;
  2723. }
  2724. case Kzstr: { /* zero-terminated string */
  2725. size_t len;
  2726. const char *s = luaL_checklstring(L, arg, &len);
  2727. luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
  2728. luaL_addlstring(&b, s, len);
  2729. luaL_addchar(&b, '\0'); /* add zero at the end */
  2730. totalsize += len + 1;
  2731. break;
  2732. }
  2733. case Kpadding:
  2734. luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */
  2735. case Kpaddalign:
  2736. case Knop:
  2737. arg--; /* undo increment */
  2738. break;
  2739. }
  2740. }
  2741. luaL_pushresult(&b);
  2742. return 1;
  2743. }
  2744. static int
  2745. lua_util_packsize(lua_State *L)
  2746. {
  2747. Header h;
  2748. const char *fmt = luaL_checkstring(L, 1); /* format string */
  2749. size_t totalsize = 0; /* accumulate total size of result */
  2750. initheader(L, &h);
  2751. while (*fmt != '\0') {
  2752. int size, ntoalign;
  2753. KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
  2754. size += ntoalign; /* total space used by option */
  2755. luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
  2756. "format result too large");
  2757. totalsize += size;
  2758. switch (opt) {
  2759. case Kstring: /* strings with length count */
  2760. case Kzstr: /* zero-terminated string */
  2761. luaL_argerror(L, 1, "variable-length format");
  2762. /* call never return, but to avoid warnings: */ /* FALLTHROUGH */
  2763. default:
  2764. break;
  2765. }
  2766. }
  2767. lua_pushinteger(L, (lua_Integer) totalsize);
  2768. return 1;
  2769. }
  2770. /*
  2771. ** Unpack an integer with 'size' bytes and 'islittle' endianness.
  2772. ** If size is smaller than the size of a Lua integer and integer
  2773. ** is signed, must do sign extension (propagating the sign to the
  2774. ** higher bits); if size is larger than the size of a Lua integer,
  2775. ** it must check the unread bytes to see whether they do not cause an
  2776. ** overflow.
  2777. */
  2778. static lua_Integer
  2779. unpackint(lua_State *L, const char *str,
  2780. int islittle, int size, int issigned)
  2781. {
  2782. lua_Unsigned res = 0;
  2783. int i;
  2784. int limit = (size <= SZINT) ? size : SZINT;
  2785. for (i = limit - 1; i >= 0; i--) {
  2786. res <<= NB;
  2787. res |= (lua_Unsigned) (unsigned char) str[islittle ? i : size - 1 - i];
  2788. }
  2789. if (size < SZINT) { /* real size smaller than lua_Integer? */
  2790. if (issigned) { /* needs sign extension? */
  2791. lua_Unsigned mask = (lua_Unsigned) 1 << (size * NB - 1);
  2792. res = ((res ^ mask) - mask); /* do sign extension */
  2793. }
  2794. }
  2795. else if (size > SZINT) { /* must check unread bytes */
  2796. int mask = (!issigned || (lua_Integer) res >= 0) ? 0 : MC;
  2797. for (i = limit; i < size; i++) {
  2798. if ((unsigned char) str[islittle ? i : size - 1 - i] != mask)
  2799. luaL_error(L,
  2800. "%d-byte integer does not fit into Lua Integer",
  2801. size);
  2802. }
  2803. }
  2804. return (lua_Integer) res;
  2805. }
  2806. static lua_Integer
  2807. posrelat(lua_Integer pos, size_t len)
  2808. {
  2809. if (pos >= 0)
  2810. return pos;
  2811. else if (0u - (size_t) pos > len)
  2812. return 0;
  2813. else
  2814. return (lua_Integer) len + pos + 1;
  2815. }
  2816. static int
  2817. lua_util_unpack(lua_State *L)
  2818. {
  2819. Header h;
  2820. const char *fmt = luaL_checkstring(L, 1);
  2821. size_t ld;
  2822. const char *data;
  2823. int n = 0; /* number of results */
  2824. if (lua_type(L, 2) == LUA_TUSERDATA) {
  2825. struct rspamd_lua_text *t = lua_check_text(L, 2);
  2826. if (!t) {
  2827. return luaL_error(L, "invalid arguments");
  2828. }
  2829. data = t->start;
  2830. ld = t->len;
  2831. }
  2832. else {
  2833. data = luaL_checklstring(L, 2, &ld);
  2834. }
  2835. size_t pos = (size_t) posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
  2836. luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
  2837. initheader(L, &h);
  2838. while (*fmt != '\0') {
  2839. int size, ntoalign;
  2840. KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
  2841. if ((size_t) ntoalign + size > ~pos || pos + ntoalign + size > ld)
  2842. luaL_argerror(L, 2, "data string too short");
  2843. pos += ntoalign; /* skip alignment */
  2844. /* stack space for item + next position */
  2845. luaL_checkstack(L, 2, "too many results");
  2846. n++;
  2847. switch (opt) {
  2848. case Kint:
  2849. case Kuint: {
  2850. lua_Integer res = unpackint(L, data + pos, h.islittle, size,
  2851. (opt == Kint));
  2852. lua_pushinteger(L, res);
  2853. break;
  2854. }
  2855. case Kfloat: {
  2856. volatile Ftypes u;
  2857. lua_Number num;
  2858. copywithendian(u.buff, data + pos, size, h.islittle);
  2859. if (size == sizeof(u.f))
  2860. num = (lua_Number) u.f;
  2861. else if (size == sizeof(u.d))
  2862. num = (lua_Number) u.d;
  2863. else
  2864. num = u.n;
  2865. lua_pushnumber(L, num);
  2866. break;
  2867. }
  2868. case Kchar: {
  2869. lua_pushlstring(L, data + pos, size);
  2870. break;
  2871. }
  2872. case Kstring: {
  2873. size_t len = (size_t) unpackint(L,
  2874. data + pos,
  2875. h.islittle,
  2876. size,
  2877. 0);
  2878. luaL_argcheck(L,
  2879. pos + len + size <= ld,
  2880. 2,
  2881. "data string too short");
  2882. lua_pushlstring(L, data + pos + size, len);
  2883. pos += len; /* skip string */
  2884. break;
  2885. }
  2886. case Kzstr: {
  2887. size_t len = (int) strlen(data + pos);
  2888. lua_pushlstring(L, data + pos, len);
  2889. pos += len + 1; /* skip string plus final '\0' */
  2890. break;
  2891. }
  2892. case Kpaddalign:
  2893. case Kpadding:
  2894. case Knop:
  2895. n--; /* undo increment */
  2896. break;
  2897. }
  2898. pos += size;
  2899. }
  2900. lua_pushinteger(L, pos + 1); /* next position */
  2901. return n + 1;
  2902. }
  2903. static int
  2904. lua_util_btc_polymod(lua_State *L)
  2905. {
  2906. uint64_t c = 1;
  2907. if (lua_type(L, 1) != LUA_TTABLE) {
  2908. return luaL_error(L, "invalid arguments");
  2909. }
  2910. for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
  2911. uint8_t c0 = c >> 35;
  2912. uint64_t d = lua_tointeger(L, -1);
  2913. c = ((c & 0x07ffffffff) << 5) ^ d;
  2914. if (c0 & 0x01) c ^= 0x98f2bc8e61;
  2915. if (c0 & 0x02) c ^= 0x79b76d99e2;
  2916. if (c0 & 0x04) c ^= 0xf33e5fb3c4;
  2917. if (c0 & 0x08) c ^= 0xae2eabe2a8;
  2918. if (c0 & 0x10) c ^= 0x1e4f43e470;
  2919. }
  2920. if ((c ^ 1) == 0) {
  2921. lua_pushboolean(L, true);
  2922. }
  2923. else {
  2924. lua_pushboolean(L, false);
  2925. }
  2926. return 1;
  2927. }
  2928. static int
  2929. lua_util_parse_smtp_date(lua_State *L)
  2930. {
  2931. return lua_parsers_parse_smtp_date(L);
  2932. }
  2933. static int
  2934. lua_load_util(lua_State *L)
  2935. {
  2936. lua_newtable(L);
  2937. luaL_register(L, NULL, utillib_f);
  2938. return 1;
  2939. }
  2940. static int
  2941. lua_load_int64(lua_State *L)
  2942. {
  2943. lua_newtable(L);
  2944. luaL_register(L, NULL, int64lib_f);
  2945. return 1;
  2946. }
  2947. void luaopen_util(lua_State *L)
  2948. {
  2949. rspamd_lua_new_class(L, rspamd_ev_base_classname, ev_baselib_m);
  2950. lua_pop(L, 1);
  2951. rspamd_lua_new_class(L, rspamd_int64_classname, int64lib_m);
  2952. lua_pop(L, 1);
  2953. rspamd_lua_add_preload(L, "rspamd_util", lua_load_util);
  2954. rspamd_lua_add_preload(L, "rspamd_int64", lua_load_int64);
  2955. }
  2956. static int
  2957. lua_int64_tostring(lua_State *L)
  2958. {
  2959. int64_t n = lua_check_int64(L, 1);
  2960. char buf[32];
  2961. bool is_signed = false;
  2962. if (lua_isboolean(L, 2)) {
  2963. is_signed = lua_toboolean(L, 2);
  2964. }
  2965. if (is_signed) {
  2966. rspamd_snprintf(buf, sizeof(buf), "%L", n);
  2967. }
  2968. else {
  2969. rspamd_snprintf(buf, sizeof(buf), "%uL", n);
  2970. }
  2971. lua_pushstring(L, buf);
  2972. return 1;
  2973. }
  2974. static int
  2975. lua_int64_fromstring(lua_State *L)
  2976. {
  2977. struct rspamd_lua_text *t = lua_check_text_or_string(L, 1);
  2978. if (t && t->len > 0) {
  2979. uint64_t u64;
  2980. const char *p = t->start;
  2981. gsize len = t->len;
  2982. bool neg = false;
  2983. /*
  2984. * We use complicated negation to allow both signed and unsinged values to
  2985. * fit into result.
  2986. * So we read int64 as unsigned and copy it to signed number.
  2987. * If we wanted u64 this allows to have the same memory representation of
  2988. * signed and unsigned.
  2989. * If we wanted signed i64 we still can use -1000500 and it will be parsed
  2990. * properly
  2991. */
  2992. if (*p == '-') {
  2993. neg = true;
  2994. p++;
  2995. len--;
  2996. }
  2997. if (!rspamd_strtou64(p, len, &u64)) {
  2998. lua_pushnil(L);
  2999. lua_pushstring(L, "invalid number");
  3000. return 2;
  3001. }
  3002. int64_t *i64_p = lua_newuserdata(L, sizeof(int64_t));
  3003. rspamd_lua_setclass(L, rspamd_int64_classname, -1);
  3004. memcpy(i64_p, &u64, sizeof(u64));
  3005. if (neg) {
  3006. *i64_p = -(*i64_p);
  3007. }
  3008. }
  3009. else {
  3010. }
  3011. return 1;
  3012. }
  3013. static int
  3014. lua_int64_tonumber(lua_State *L)
  3015. {
  3016. int64_t n = lua_check_int64(L, 1);
  3017. double d;
  3018. d = n;
  3019. lua_pushinteger(L, d);
  3020. return 1;
  3021. }
  3022. static int
  3023. lua_int64_hex(lua_State *L)
  3024. {
  3025. int64_t n = lua_check_int64(L, 1);
  3026. char buf[32];
  3027. rspamd_snprintf(buf, sizeof(buf), "%XL", n);
  3028. lua_pushstring(L, buf);
  3029. return 1;
  3030. }
  3031. static int
  3032. lua_ev_base_loop(lua_State *L)
  3033. {
  3034. int flags = 0;
  3035. struct ev_loop *ev_base;
  3036. ev_base = lua_check_ev_base(L, 1);
  3037. if (lua_isnumber(L, 2)) {
  3038. flags = lua_tointeger(L, 2);
  3039. }
  3040. int ret = ev_run(ev_base, flags);
  3041. lua_pushinteger(L, ret);
  3042. return 1;
  3043. }