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.

util.c 61KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081
  1. /*-
  2. * Copyright 2017 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 "config.h"
  17. #include "util.h"
  18. #include "cfg_file.h"
  19. #include "rspamd.h"
  20. #include "unix-std.h"
  21. #include "xxhash.h"
  22. #include "ottery.h"
  23. #include "cryptobox.h"
  24. #include "libutil/map.h"
  25. #define ZSTD_STATIC_LINKING_ONLY
  26. #include "contrib/zstd/zstd.h"
  27. #include "contrib/zstd/zdict.h"
  28. #ifdef HAVE_OPENSSL
  29. #include <openssl/rand.h>
  30. #include <openssl/err.h>
  31. #include <openssl/evp.h>
  32. #include <openssl/ssl.h>
  33. #include <openssl/conf.h>
  34. #include <openssl/engine.h>
  35. #endif
  36. #ifdef HAVE_TERMIOS_H
  37. #include <termios.h>
  38. #endif
  39. #ifdef HAVE_READPASSPHRASE_H
  40. #include <readpassphrase.h>
  41. #endif
  42. #ifdef HAVE_LOCALE_H
  43. #include <locale.h>
  44. #endif
  45. /* libutil */
  46. #ifdef HAVE_LIBUTIL_H
  47. #include <libutil.h>
  48. #endif
  49. #ifdef __APPLE__
  50. #include <mach/mach_time.h>
  51. #include <mach/mach_init.h>
  52. #include <mach/thread_act.h>
  53. #include <mach/mach_port.h>
  54. #endif
  55. #ifdef WITH_GPERF_TOOLS
  56. #include <gperftools/profiler.h>
  57. #endif
  58. /* poll */
  59. #ifdef HAVE_POLL_H
  60. #include <poll.h>
  61. #endif
  62. #ifdef HAVE_SIGINFO_H
  63. #include <siginfo.h>
  64. #endif
  65. /* sys/wait */
  66. #ifdef HAVE_SYS_WAIT_H
  67. #include <sys/wait.h>
  68. #endif
  69. /* sys/resource.h */
  70. #ifdef HAVE_SYS_RESOURCE_H
  71. #include <sys/resource.h>
  72. #endif
  73. #ifdef HAVE_RDTSC
  74. #ifdef __x86_64__
  75. #include <x86intrin.h>
  76. #endif
  77. #endif
  78. #include <math.h> /* for pow */
  79. #include <glob.h> /* in fact, we require this file ultimately */
  80. #include "cryptobox.h"
  81. #include "zlib.h"
  82. #include "contrib/uthash/utlist.h"
  83. /* Check log messages intensity once per minute */
  84. #define CHECK_TIME 60
  85. /* More than 2 log messages per second */
  86. #define BUF_INTENSITY 2
  87. /* Default connect timeout for sync sockets */
  88. #define CONNECT_TIMEOUT 3
  89. const struct rspamd_controller_pbkdf pbkdf_list[] = {
  90. {
  91. .name = "PBKDF2-blake2b",
  92. .alias = "pbkdf2",
  93. .description = "standard CPU intensive \"slow\" KDF using blake2b hash function",
  94. .type = RSPAMD_CRYPTOBOX_PBKDF2,
  95. .id = RSPAMD_PBKDF_ID_V1,
  96. .complexity = 16000,
  97. .salt_len = 20,
  98. .key_len = rspamd_cryptobox_HASHBYTES / 2
  99. },
  100. {
  101. .name = "Catena-Butterfly",
  102. .alias = "catena",
  103. .description = "modern CPU and memory intensive KDF",
  104. .type = RSPAMD_CRYPTOBOX_CATENA,
  105. .id = RSPAMD_PBKDF_ID_V2,
  106. .complexity = 10,
  107. .salt_len = 20,
  108. .key_len = rspamd_cryptobox_HASHBYTES / 2
  109. }
  110. };
  111. gint
  112. rspamd_socket_nonblocking (gint fd)
  113. {
  114. gint ofl;
  115. ofl = fcntl (fd, F_GETFL, 0);
  116. if (fcntl (fd, F_SETFL, ofl | O_NONBLOCK) == -1) {
  117. msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno));
  118. return -1;
  119. }
  120. return 0;
  121. }
  122. gint
  123. rspamd_socket_blocking (gint fd)
  124. {
  125. gint ofl;
  126. ofl = fcntl (fd, F_GETFL, 0);
  127. if (fcntl (fd, F_SETFL, ofl & (~O_NONBLOCK)) == -1) {
  128. msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno));
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. gint
  134. rspamd_socket_poll (gint fd, gint timeout, short events)
  135. {
  136. gint r;
  137. struct pollfd fds[1];
  138. fds->fd = fd;
  139. fds->events = events;
  140. fds->revents = 0;
  141. while ((r = poll (fds, 1, timeout)) < 0) {
  142. if (errno != EINTR) {
  143. break;
  144. }
  145. }
  146. return r;
  147. }
  148. gint
  149. rspamd_socket_create (gint af, gint type, gint protocol, gboolean async)
  150. {
  151. gint fd;
  152. fd = socket (af, type, protocol);
  153. if (fd == -1) {
  154. msg_warn ("socket failed: %d, '%s'", errno, strerror (errno));
  155. return -1;
  156. }
  157. /* Set close on exec */
  158. if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
  159. msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno));
  160. close (fd);
  161. return -1;
  162. }
  163. if (async) {
  164. if (rspamd_socket_nonblocking (fd) == -1) {
  165. close (fd);
  166. return -1;
  167. }
  168. }
  169. return fd;
  170. }
  171. static gint
  172. rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server,
  173. gboolean async, GList **list)
  174. {
  175. gint fd = -1, r, on = 1, s_error;
  176. struct addrinfo *cur;
  177. gpointer ptr;
  178. socklen_t optlen;
  179. cur = addr;
  180. while (cur) {
  181. /* Create socket */
  182. fd = rspamd_socket_create (cur->ai_family, type, cur->ai_protocol, TRUE);
  183. if (fd == -1) {
  184. goto out;
  185. }
  186. if (is_server) {
  187. if (setsockopt (fd,
  188. SOL_SOCKET,
  189. SO_REUSEADDR,
  190. (const void *)&on,
  191. sizeof (gint)) == -1) {
  192. msg_warn ("setsockopt failed: %d, '%s'", errno,
  193. strerror (errno));
  194. }
  195. #ifdef HAVE_IPV6_V6ONLY
  196. if (cur->ai_family == AF_INET6) {
  197. if (setsockopt (fd,
  198. IPPROTO_IPV6,
  199. IPV6_V6ONLY,
  200. (const void *)&on,
  201. sizeof (gint)) == -1) {
  202. msg_warn ("setsockopt failed: %d, '%s'", errno,
  203. strerror (errno));
  204. }
  205. }
  206. #endif
  207. r = bind (fd, cur->ai_addr, cur->ai_addrlen);
  208. }
  209. else {
  210. r = connect (fd, cur->ai_addr, cur->ai_addrlen);
  211. }
  212. if (r == -1) {
  213. if (errno != EINPROGRESS) {
  214. msg_warn ("bind/connect failed: %d, '%s'", errno,
  215. strerror (errno));
  216. goto out;
  217. }
  218. if (!async) {
  219. /* Try to poll */
  220. if (rspamd_socket_poll (fd, CONNECT_TIMEOUT * 1000,
  221. POLLOUT) <= 0) {
  222. errno = ETIMEDOUT;
  223. msg_warn ("bind/connect failed: timeout");
  224. goto out;
  225. }
  226. else {
  227. /* Make synced again */
  228. if (rspamd_socket_blocking (fd) < 0) {
  229. goto out;
  230. }
  231. }
  232. }
  233. }
  234. else {
  235. /* Still need to check SO_ERROR on socket */
  236. optlen = sizeof (s_error);
  237. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, (void *)&s_error, &optlen) != -1) {
  238. if (s_error) {
  239. errno = s_error;
  240. goto out;
  241. }
  242. }
  243. }
  244. if (list == NULL) {
  245. /* Go out immediately */
  246. break;
  247. }
  248. else if (fd != -1) {
  249. ptr = GINT_TO_POINTER (fd);
  250. *list = g_list_prepend (*list, ptr);
  251. cur = cur->ai_next;
  252. continue;
  253. }
  254. out:
  255. if (fd != -1) {
  256. close (fd);
  257. }
  258. fd = -1;
  259. cur = cur->ai_next;
  260. }
  261. return (fd);
  262. }
  263. gint
  264. rspamd_socket_tcp (struct addrinfo *addr, gboolean is_server, gboolean async)
  265. {
  266. return rspamd_inet_socket_create (SOCK_STREAM, addr, is_server, async, NULL);
  267. }
  268. gint
  269. rspamd_socket_udp (struct addrinfo *addr, gboolean is_server, gboolean async)
  270. {
  271. return rspamd_inet_socket_create (SOCK_DGRAM, addr, is_server, async, NULL);
  272. }
  273. gint
  274. rspamd_socket_unix (const gchar *path,
  275. struct sockaddr_un *addr,
  276. gint type,
  277. gboolean is_server,
  278. gboolean async)
  279. {
  280. socklen_t optlen;
  281. gint fd = -1, s_error, r, serrno, on = 1;
  282. struct stat st;
  283. if (path == NULL)
  284. return -1;
  285. addr->sun_family = AF_UNIX;
  286. rspamd_strlcpy (addr->sun_path, path, sizeof (addr->sun_path));
  287. #ifdef FREEBSD
  288. addr->sun_len = SUN_LEN (addr);
  289. #endif
  290. if (is_server) {
  291. /* Unlink socket if it exists already */
  292. if (lstat (addr->sun_path, &st) != -1) {
  293. if (S_ISSOCK (st.st_mode)) {
  294. if (unlink (addr->sun_path) == -1) {
  295. msg_warn ("unlink %s failed: %d, '%s'",
  296. addr->sun_path,
  297. errno,
  298. strerror (errno));
  299. goto out;
  300. }
  301. }
  302. else {
  303. msg_warn ("%s is not a socket", addr->sun_path);
  304. goto out;
  305. }
  306. }
  307. }
  308. fd = socket (PF_LOCAL, type, 0);
  309. if (fd == -1) {
  310. msg_warn ("socket failed %s: %d, '%s'",
  311. addr->sun_path,
  312. errno,
  313. strerror (errno));
  314. return -1;
  315. }
  316. if (rspamd_socket_nonblocking (fd) < 0) {
  317. goto out;
  318. }
  319. /* Set close on exec */
  320. if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
  321. msg_warn ("fcntl failed %s: %d, '%s'", addr->sun_path, errno,
  322. strerror (errno));
  323. goto out;
  324. }
  325. if (is_server) {
  326. if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on,
  327. sizeof (gint)) == -1) {
  328. msg_warn ("setsockopt failed: %d, '%s'", errno,
  329. strerror (errno));
  330. }
  331. r = bind (fd, (struct sockaddr *)addr, SUN_LEN (addr));
  332. }
  333. else {
  334. r = connect (fd, (struct sockaddr *)addr, SUN_LEN (addr));
  335. }
  336. if (r == -1) {
  337. if (errno != EINPROGRESS) {
  338. msg_warn ("bind/connect failed %s: %d, '%s'",
  339. addr->sun_path,
  340. errno,
  341. strerror (errno));
  342. goto out;
  343. }
  344. if (!async) {
  345. /* Try to poll */
  346. if (rspamd_socket_poll (fd, CONNECT_TIMEOUT * 1000, POLLOUT) <= 0) {
  347. errno = ETIMEDOUT;
  348. msg_warn ("bind/connect failed %s: timeout", addr->sun_path);
  349. goto out;
  350. }
  351. else {
  352. /* Make synced again */
  353. if (rspamd_socket_blocking (fd) < 0) {
  354. goto out;
  355. }
  356. }
  357. }
  358. }
  359. else {
  360. /* Still need to check SO_ERROR on socket */
  361. optlen = sizeof (s_error);
  362. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, (void *)&s_error, &optlen) != -1) {
  363. if (s_error) {
  364. errno = s_error;
  365. goto out;
  366. }
  367. }
  368. }
  369. return (fd);
  370. out:
  371. serrno = errno;
  372. if (fd != -1) {
  373. close (fd);
  374. }
  375. errno = serrno;
  376. return (-1);
  377. }
  378. static int
  379. rspamd_prefer_v4_hack (const struct addrinfo *a1, const struct addrinfo *a2)
  380. {
  381. return a1->ai_addr->sa_family - a2->ai_addr->sa_family;
  382. }
  383. /**
  384. * Make a universal socket
  385. * @param credits host, ip or path to unix socket
  386. * @param port port (used for network sockets)
  387. * @param async make this socket asynced
  388. * @param is_server make this socket as server socket
  389. * @param try_resolve try name resolution for a socket (BLOCKING)
  390. */
  391. gint
  392. rspamd_socket (const gchar *credits, guint16 port,
  393. gint type, gboolean async, gboolean is_server, gboolean try_resolve)
  394. {
  395. struct sockaddr_un un;
  396. struct stat st;
  397. struct addrinfo hints, *res;
  398. gint r;
  399. gchar portbuf[8];
  400. if (*credits == '/') {
  401. if (is_server) {
  402. return rspamd_socket_unix (credits, &un, type, is_server, async);
  403. }
  404. else {
  405. r = stat (credits, &st);
  406. if (r == -1) {
  407. /* Unix socket doesn't exists it must be created first */
  408. errno = ENOENT;
  409. return -1;
  410. }
  411. else {
  412. if ((st.st_mode & S_IFSOCK) == 0) {
  413. /* Path is not valid socket */
  414. errno = EINVAL;
  415. return -1;
  416. }
  417. else {
  418. return rspamd_socket_unix (credits,
  419. &un,
  420. type,
  421. is_server,
  422. async);
  423. }
  424. }
  425. }
  426. }
  427. else {
  428. /* TCP related part */
  429. memset (&hints, 0, sizeof (hints));
  430. hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  431. hints.ai_socktype = type; /* Type of the socket */
  432. hints.ai_flags = is_server ? AI_PASSIVE : 0;
  433. hints.ai_protocol = 0; /* Any protocol */
  434. hints.ai_canonname = NULL;
  435. hints.ai_addr = NULL;
  436. hints.ai_next = NULL;
  437. if (!try_resolve) {
  438. hints.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;
  439. }
  440. rspamd_snprintf (portbuf, sizeof (portbuf), "%d", (int)port);
  441. if ((r = getaddrinfo (credits, portbuf, &hints, &res)) == 0) {
  442. LL_SORT2 (res, rspamd_prefer_v4_hack, ai_next);
  443. r = rspamd_inet_socket_create (type, res, is_server, async, NULL);
  444. freeaddrinfo (res);
  445. return r;
  446. }
  447. else {
  448. msg_err ("address resolution for %s failed: %s",
  449. credits,
  450. gai_strerror (r));
  451. return -1;
  452. }
  453. }
  454. }
  455. /**
  456. * Make universal stream socket
  457. * @param credits host, ip or path to unix socket
  458. * @param port port (used for network sockets)
  459. * @param async make this socket asynced
  460. * @param is_server make this socket as server socket
  461. * @param try_resolve try name resolution for a socket (BLOCKING)
  462. */
  463. GList *
  464. rspamd_sockets_list (const gchar *credits, guint16 port,
  465. gint type, gboolean async, gboolean is_server, gboolean try_resolve)
  466. {
  467. struct sockaddr_un un;
  468. struct stat st;
  469. struct addrinfo hints, *res;
  470. gint r, fd = -1, serrno;
  471. gchar portbuf[8], **strv, **cur;
  472. GList *result = NULL, *rcur;
  473. gpointer ptr;
  474. strv = g_strsplit_set (credits, ",", -1);
  475. if (strv == NULL) {
  476. msg_err ("invalid sockets credentials: %s", credits);
  477. return NULL;
  478. }
  479. cur = strv;
  480. while (*cur != NULL) {
  481. if (*credits == '/') {
  482. if (is_server) {
  483. fd = rspamd_socket_unix (credits, &un, type, is_server, async);
  484. }
  485. else {
  486. r = stat (credits, &st);
  487. if (r == -1) {
  488. /* Unix socket doesn't exists it must be created first */
  489. errno = ENOENT;
  490. goto err;
  491. }
  492. else {
  493. if ((st.st_mode & S_IFSOCK) == 0) {
  494. /* Path is not valid socket */
  495. errno = EINVAL;
  496. goto err;
  497. }
  498. else {
  499. fd = rspamd_socket_unix (credits,
  500. &un,
  501. type,
  502. is_server,
  503. async);
  504. }
  505. }
  506. }
  507. if (fd != -1) {
  508. ptr = GINT_TO_POINTER (fd);
  509. result = g_list_prepend (result, ptr);
  510. fd = -1;
  511. }
  512. else {
  513. goto err;
  514. }
  515. }
  516. else {
  517. /* TCP related part */
  518. memset (&hints, 0, sizeof (hints));
  519. hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  520. hints.ai_socktype = type; /* Type of the socket */
  521. hints.ai_flags = is_server ? AI_PASSIVE : 0;
  522. hints.ai_protocol = 0; /* Any protocol */
  523. hints.ai_canonname = NULL;
  524. hints.ai_addr = NULL;
  525. hints.ai_next = NULL;
  526. if (!try_resolve) {
  527. hints.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;
  528. }
  529. rspamd_snprintf (portbuf, sizeof (portbuf), "%d", (int)port);
  530. if ((r = getaddrinfo (credits, portbuf, &hints, &res)) == 0) {
  531. LL_SORT2 (res, rspamd_prefer_v4_hack, ai_next);
  532. fd = rspamd_inet_socket_create (type, res, is_server, async,
  533. &result);
  534. freeaddrinfo (res);
  535. if (result == NULL) {
  536. goto err;
  537. }
  538. }
  539. else {
  540. msg_err ("address resolution for %s failed: %s",
  541. credits,
  542. gai_strerror (r));
  543. goto err;
  544. }
  545. }
  546. cur++;
  547. }
  548. g_strfreev (strv);
  549. return result;
  550. err:
  551. g_strfreev (strv);
  552. serrno = errno;
  553. rcur = result;
  554. while (rcur != NULL) {
  555. ptr = rcur->data;
  556. fd = GPOINTER_TO_INT (ptr);
  557. if (fd != -1) {
  558. close (fd);
  559. }
  560. rcur = g_list_next (rcur);
  561. }
  562. if (result != NULL) {
  563. g_list_free (result);
  564. }
  565. errno = serrno;
  566. return NULL;
  567. }
  568. gboolean
  569. rspamd_socketpair (gint pair[2], gboolean is_stream)
  570. {
  571. gint r, serrno;
  572. if (!is_stream) {
  573. #ifdef HAVE_SOCK_SEQPACKET
  574. r = socketpair (AF_LOCAL, SOCK_SEQPACKET, 0, pair);
  575. if (r == -1) {
  576. msg_warn ("seqpacket socketpair failed: %d, '%s'",
  577. errno,
  578. strerror (errno));
  579. r = socketpair (AF_LOCAL, SOCK_DGRAM, 0, pair);
  580. }
  581. #else
  582. r = socketpair (AF_LOCAL, SOCK_DGRAM, 0, pair);
  583. #endif
  584. }
  585. else {
  586. r = socketpair (AF_LOCAL, SOCK_STREAM, 0, pair);
  587. }
  588. if (r == -1) {
  589. msg_warn ("socketpair failed: %d, '%s'", errno, strerror (
  590. errno));
  591. return -1;
  592. }
  593. /* Set close on exec */
  594. if (fcntl (pair[0], F_SETFD, FD_CLOEXEC) == -1) {
  595. msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno));
  596. goto out;
  597. }
  598. if (fcntl (pair[1], F_SETFD, FD_CLOEXEC) == -1) {
  599. msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno));
  600. goto out;
  601. }
  602. return TRUE;
  603. out:
  604. serrno = errno;
  605. close (pair[0]);
  606. close (pair[1]);
  607. errno = serrno;
  608. return FALSE;
  609. }
  610. gint
  611. rspamd_write_pid (struct rspamd_main *main)
  612. {
  613. pid_t pid;
  614. if (main->cfg->pid_file == NULL) {
  615. return -1;
  616. }
  617. main->pfh = rspamd_pidfile_open (main->cfg->pid_file, 0644, &pid);
  618. if (main->pfh == NULL) {
  619. return -1;
  620. }
  621. if (main->is_privilleged) {
  622. /* Force root user as owner of pid file */
  623. #ifdef HAVE_PIDFILE_FILENO
  624. if (fchown (pidfile_fileno (main->pfh), 0, 0) == -1) {
  625. #else
  626. if (fchown (main->pfh->pf_fd, 0, 0) == -1) {
  627. #endif
  628. msg_err ("cannot chown of pidfile %s to 0:0 user",
  629. main->cfg->pid_file);
  630. }
  631. }
  632. rspamd_pidfile_write (main->pfh);
  633. return 0;
  634. }
  635. #ifdef HAVE_SA_SIGINFO
  636. void
  637. rspamd_signals_init (struct sigaction *signals, void (*sig_handler)(gint,
  638. siginfo_t *,
  639. void *))
  640. #else
  641. void
  642. rspamd_signals_init (struct sigaction *signals, void (*sig_handler)(gint))
  643. #endif
  644. {
  645. struct sigaction sigpipe_act;
  646. /* Setting up signal handlers */
  647. /* SIGUSR1 - reopen config file */
  648. /* SIGUSR2 - worker is ready for accept */
  649. sigemptyset (&signals->sa_mask);
  650. sigaddset (&signals->sa_mask, SIGTERM);
  651. sigaddset (&signals->sa_mask, SIGINT);
  652. sigaddset (&signals->sa_mask, SIGHUP);
  653. sigaddset (&signals->sa_mask, SIGCHLD);
  654. sigaddset (&signals->sa_mask, SIGUSR1);
  655. sigaddset (&signals->sa_mask, SIGUSR2);
  656. sigaddset (&signals->sa_mask, SIGALRM);
  657. #ifdef SIGPOLL
  658. sigaddset (&signals->sa_mask, SIGPOLL);
  659. #endif
  660. #ifdef SIGIO
  661. sigaddset (&signals->sa_mask, SIGIO);
  662. #endif
  663. #ifdef HAVE_SA_SIGINFO
  664. signals->sa_flags = SA_SIGINFO;
  665. signals->sa_handler = NULL;
  666. signals->sa_sigaction = sig_handler;
  667. #else
  668. signals->sa_handler = sig_handler;
  669. signals->sa_flags = 0;
  670. #endif
  671. sigaction (SIGTERM, signals, NULL);
  672. sigaction (SIGINT, signals, NULL);
  673. sigaction (SIGHUP, signals, NULL);
  674. sigaction (SIGCHLD, signals, NULL);
  675. sigaction (SIGUSR1, signals, NULL);
  676. sigaction (SIGUSR2, signals, NULL);
  677. sigaction (SIGALRM, signals, NULL);
  678. #ifdef SIGPOLL
  679. sigaction (SIGPOLL, signals, NULL);
  680. #endif
  681. #ifdef SIGIO
  682. sigaction (SIGIO, signals, NULL);
  683. #endif
  684. /* Ignore SIGPIPE as we handle write errors manually */
  685. sigemptyset (&sigpipe_act.sa_mask);
  686. sigaddset (&sigpipe_act.sa_mask, SIGPIPE);
  687. sigpipe_act.sa_handler = SIG_IGN;
  688. sigpipe_act.sa_flags = 0;
  689. sigaction (SIGPIPE, &sigpipe_act, NULL);
  690. }
  691. static void
  692. pass_signal_cb (gpointer key, gpointer value, gpointer ud)
  693. {
  694. struct rspamd_worker *cur = value;
  695. gint signo = GPOINTER_TO_INT (ud);
  696. kill (cur->pid, signo);
  697. }
  698. void
  699. rspamd_pass_signal (GHashTable * workers, gint signo)
  700. {
  701. g_hash_table_foreach (workers, pass_signal_cb, GINT_TO_POINTER (signo));
  702. }
  703. #ifndef HAVE_SETPROCTITLE
  704. #if !defined(DARWIN) && !defined(SOLARIS) && !defined(__APPLE__)
  705. static gchar *title_buffer = 0;
  706. static size_t title_buffer_size = 0;
  707. static gchar *title_progname, *title_progname_full;
  708. #endif
  709. gint
  710. setproctitle (const gchar *fmt, ...)
  711. {
  712. #if defined(DARWIN) || defined(SOLARIS) || defined(__APPLE__)
  713. GString *dest;
  714. va_list ap;
  715. dest = g_string_new ("");
  716. va_start (ap, fmt);
  717. rspamd_vprintf_gstring (dest, fmt, ap);
  718. va_end (ap);
  719. g_set_prgname (dest->str);
  720. g_string_free (dest, TRUE);
  721. return 0;
  722. #else
  723. if (!title_buffer || !title_buffer_size) {
  724. errno = ENOMEM;
  725. return -1;
  726. }
  727. memset (title_buffer, '\0', title_buffer_size);
  728. ssize_t written;
  729. if (fmt) {
  730. ssize_t written2;
  731. va_list ap;
  732. written = snprintf (title_buffer,
  733. title_buffer_size,
  734. "%s: ",
  735. title_progname);
  736. if (written < 0 || (size_t) written >= title_buffer_size)
  737. return -1;
  738. va_start (ap, fmt);
  739. written2 = vsnprintf (title_buffer + written,
  740. title_buffer_size - written,
  741. fmt,
  742. ap);
  743. va_end (ap);
  744. if (written2 < 0 || (size_t) written2 >= title_buffer_size - written)
  745. return -1;
  746. }
  747. else {
  748. written = snprintf (title_buffer,
  749. title_buffer_size,
  750. "%s",
  751. title_progname);
  752. if (written < 0 || (size_t) written >= title_buffer_size)
  753. return -1;
  754. }
  755. written = strlen (title_buffer);
  756. memset (title_buffer + written, '\0', title_buffer_size - written);
  757. return 0;
  758. #endif
  759. }
  760. #if !(defined(DARWIN) || defined(SOLARIS) || defined(__APPLE__))
  761. static void
  762. rspamd_title_dtor (gpointer d)
  763. {
  764. gchar **env = (gchar **)d;
  765. guint i;
  766. for (i = 0; env[i] != NULL; i++) {
  767. g_free (env[i]);
  768. }
  769. g_free (env);
  770. }
  771. #endif
  772. /*
  773. It has to be _init function, because __attribute__((constructor))
  774. functions gets called without arguments.
  775. */
  776. gint
  777. init_title (struct rspamd_main *rspamd_main,
  778. gint argc, gchar *argv[], gchar *envp[])
  779. {
  780. #if defined(DARWIN) || defined(SOLARIS) || defined(__APPLE__)
  781. /* XXX: try to handle these OSes too */
  782. return 0;
  783. #else
  784. gchar *begin_of_buffer = 0, *end_of_buffer = 0;
  785. gint i;
  786. for (i = 0; i < argc; ++i) {
  787. if (!begin_of_buffer) {
  788. begin_of_buffer = argv[i];
  789. }
  790. if (!end_of_buffer || end_of_buffer + 1 == argv[i]) {
  791. end_of_buffer = argv[i] + strlen (argv[i]);
  792. }
  793. }
  794. for (i = 0; envp[i]; ++i) {
  795. if (!begin_of_buffer) {
  796. begin_of_buffer = envp[i];
  797. }
  798. if (!end_of_buffer || end_of_buffer + 1 == envp[i]) {
  799. end_of_buffer = envp[i] + strlen (envp[i]);
  800. }
  801. }
  802. if (!end_of_buffer) {
  803. return 0;
  804. }
  805. gchar **new_environ = g_malloc ((i + 1) * sizeof (envp[0]));
  806. for (i = 0; envp[i]; ++i) {
  807. new_environ[i] = g_strdup (envp[i]);
  808. }
  809. new_environ[i] = NULL;
  810. if (program_invocation_name) {
  811. title_progname_full = g_strdup (program_invocation_name);
  812. gchar *p = strrchr (title_progname_full, '/');
  813. if (p) {
  814. title_progname = p + 1;
  815. }
  816. else {
  817. title_progname = title_progname_full;
  818. }
  819. program_invocation_name = title_progname_full;
  820. program_invocation_short_name = title_progname;
  821. }
  822. environ = new_environ;
  823. title_buffer = begin_of_buffer;
  824. title_buffer_size = end_of_buffer - begin_of_buffer;
  825. rspamd_mempool_add_destructor (rspamd_main->server_pool,
  826. rspamd_title_dtor, new_environ);
  827. return 0;
  828. #endif
  829. }
  830. #endif
  831. #ifndef HAVE_PIDFILE
  832. static gint _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit);
  833. static gint
  834. rspamd_pidfile_verify (rspamd_pidfh_t *pfh)
  835. {
  836. struct stat sb;
  837. if (pfh == NULL || pfh->pf_fd == -1)
  838. return (-1);
  839. /*
  840. * Check remembered descriptor.
  841. */
  842. if (fstat (pfh->pf_fd, &sb) == -1)
  843. return (errno);
  844. if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
  845. return -1;
  846. return 0;
  847. }
  848. static gint
  849. rspamd_pidfile_read (const gchar *path, pid_t * pidptr)
  850. {
  851. gchar buf[16], *endptr;
  852. gint error, fd, i;
  853. fd = open (path, O_RDONLY);
  854. if (fd == -1)
  855. return (errno);
  856. i = read (fd, buf, sizeof (buf) - 1);
  857. error = errno; /* Remember errno in case close() wants to change it. */
  858. close (fd);
  859. if (i == -1)
  860. return error;
  861. else if (i == 0)
  862. return EAGAIN;
  863. buf[i] = '\0';
  864. *pidptr = strtol (buf, &endptr, 10);
  865. if (endptr != &buf[i])
  866. return EINVAL;
  867. return 0;
  868. }
  869. rspamd_pidfh_t *
  870. rspamd_pidfile_open (const gchar *path, mode_t mode, pid_t * pidptr)
  871. {
  872. rspamd_pidfh_t *pfh;
  873. struct stat sb;
  874. gint error, fd, len, count;
  875. struct timespec rqtp;
  876. pfh = g_malloc (sizeof (*pfh));
  877. if (pfh == NULL)
  878. return NULL;
  879. if (path == NULL)
  880. len = snprintf (pfh->pf_path,
  881. sizeof (pfh->pf_path),
  882. "/var/run/%s.pid",
  883. g_get_prgname ());
  884. else
  885. len = snprintf (pfh->pf_path, sizeof (pfh->pf_path), "%s", path);
  886. if (len >= (gint)sizeof (pfh->pf_path)) {
  887. g_free (pfh);
  888. errno = ENAMETOOLONG;
  889. return NULL;
  890. }
  891. /*
  892. * Open the PID file and obtain exclusive lock.
  893. * We truncate PID file here only to remove old PID immediatelly,
  894. * PID file will be truncated again in pidfile_write(), so
  895. * pidfile_write() can be called multiple times.
  896. */
  897. fd = open (pfh->pf_path, O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
  898. rspamd_file_lock (fd, TRUE);
  899. if (fd == -1) {
  900. count = 0;
  901. rqtp.tv_sec = 0;
  902. rqtp.tv_nsec = 5000000;
  903. if (errno == EWOULDBLOCK && pidptr != NULL) {
  904. again:
  905. errno = rspamd_pidfile_read (pfh->pf_path, pidptr);
  906. if (errno == 0)
  907. errno = EEXIST;
  908. else if (errno == EAGAIN) {
  909. if (++count <= 3) {
  910. nanosleep (&rqtp, 0);
  911. goto again;
  912. }
  913. }
  914. }
  915. g_free (pfh);
  916. return NULL;
  917. }
  918. /*
  919. * Remember file information, so in pidfile_write() we are sure we write
  920. * to the proper descriptor.
  921. */
  922. if (fstat (fd, &sb) == -1) {
  923. error = errno;
  924. unlink (pfh->pf_path);
  925. close (fd);
  926. g_free (pfh);
  927. errno = error;
  928. return NULL;
  929. }
  930. pfh->pf_fd = fd;
  931. pfh->pf_dev = sb.st_dev;
  932. pfh->pf_ino = sb.st_ino;
  933. return pfh;
  934. }
  935. gint
  936. rspamd_pidfile_write (rspamd_pidfh_t *pfh)
  937. {
  938. gchar pidstr[16];
  939. gint error, fd;
  940. /*
  941. * Check remembered descriptor, so we don't overwrite some other
  942. * file if pidfile was closed and descriptor reused.
  943. */
  944. errno = rspamd_pidfile_verify (pfh);
  945. if (errno != 0) {
  946. /*
  947. * Don't close descriptor, because we are not sure if it's ours.
  948. */
  949. return -1;
  950. }
  951. fd = pfh->pf_fd;
  952. /*
  953. * Truncate PID file, so multiple calls of pidfile_write() are allowed.
  954. */
  955. if (ftruncate (fd, 0) == -1) {
  956. error = errno;
  957. _rspamd_pidfile_remove (pfh, 0);
  958. errno = error;
  959. return -1;
  960. }
  961. rspamd_snprintf (pidstr, sizeof (pidstr), "%P", getpid ());
  962. if (pwrite (fd, pidstr, strlen (pidstr), 0) != (ssize_t) strlen (pidstr)) {
  963. error = errno;
  964. _rspamd_pidfile_remove (pfh, 0);
  965. errno = error;
  966. return -1;
  967. }
  968. return 0;
  969. }
  970. gint
  971. rspamd_pidfile_close (rspamd_pidfh_t *pfh)
  972. {
  973. gint error;
  974. error = rspamd_pidfile_verify (pfh);
  975. if (error != 0) {
  976. errno = error;
  977. return -1;
  978. }
  979. if (close (pfh->pf_fd) == -1)
  980. error = errno;
  981. g_free (pfh);
  982. if (error != 0) {
  983. errno = error;
  984. return -1;
  985. }
  986. return 0;
  987. }
  988. static gint
  989. _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit)
  990. {
  991. gint error;
  992. error = rspamd_pidfile_verify (pfh);
  993. if (error != 0) {
  994. errno = error;
  995. return -1;
  996. }
  997. if (unlink (pfh->pf_path) == -1)
  998. error = errno;
  999. if (!rspamd_file_unlock (pfh->pf_fd, FALSE)) {
  1000. if (error == 0)
  1001. error = errno;
  1002. }
  1003. if (close (pfh->pf_fd) == -1) {
  1004. if (error == 0)
  1005. error = errno;
  1006. }
  1007. if (freeit)
  1008. g_free (pfh);
  1009. else
  1010. pfh->pf_fd = -1;
  1011. if (error != 0) {
  1012. errno = error;
  1013. return -1;
  1014. }
  1015. return 0;
  1016. }
  1017. gint
  1018. rspamd_pidfile_remove (rspamd_pidfh_t *pfh)
  1019. {
  1020. return (_rspamd_pidfile_remove (pfh, 1));
  1021. }
  1022. #endif
  1023. /* Replace %r with rcpt value and %f with from value, new string is allocated in pool */
  1024. gchar *
  1025. resolve_stat_filename (rspamd_mempool_t * pool,
  1026. gchar *pattern,
  1027. gchar *rcpt,
  1028. gchar *from)
  1029. {
  1030. gint need_to_format = 0, len = 0;
  1031. gint rcptlen, fromlen;
  1032. gchar *c = pattern, *new, *s;
  1033. if (rcpt) {
  1034. rcptlen = strlen (rcpt);
  1035. }
  1036. else {
  1037. rcptlen = 0;
  1038. }
  1039. if (from) {
  1040. fromlen = strlen (from);
  1041. }
  1042. else {
  1043. fromlen = 0;
  1044. }
  1045. /* Calculate length */
  1046. while (*c++) {
  1047. if (*c == '%' && *(c + 1) == 'r') {
  1048. len += rcptlen;
  1049. c += 2;
  1050. need_to_format = 1;
  1051. continue;
  1052. }
  1053. else if (*c == '%' && *(c + 1) == 'f') {
  1054. len += fromlen;
  1055. c += 2;
  1056. need_to_format = 1;
  1057. continue;
  1058. }
  1059. len++;
  1060. }
  1061. /* Do not allocate extra memory if we do not need to format string */
  1062. if (!need_to_format) {
  1063. return pattern;
  1064. }
  1065. /* Allocate new string */
  1066. new = rspamd_mempool_alloc (pool, len);
  1067. c = pattern;
  1068. s = new;
  1069. /* Format string */
  1070. while (*c++) {
  1071. if (*c == '%' && *(c + 1) == 'r') {
  1072. c += 2;
  1073. memcpy (s, rcpt, rcptlen);
  1074. s += rcptlen;
  1075. continue;
  1076. }
  1077. *s++ = *c;
  1078. }
  1079. *s = '\0';
  1080. return new;
  1081. }
  1082. const gchar *
  1083. rspamd_log_check_time (gdouble start, gdouble end, gint resolution)
  1084. {
  1085. gdouble diff;
  1086. static gchar res[64];
  1087. gchar fmt[32];
  1088. diff = (end - start) * 1000.0;
  1089. rspamd_snprintf (fmt, sizeof (fmt), "%%.%dfms", resolution);
  1090. rspamd_snprintf (res, sizeof (res), fmt, diff);
  1091. return (const gchar *)res;
  1092. }
  1093. void
  1094. gperf_profiler_init (struct rspamd_config *cfg, const gchar *descr)
  1095. {
  1096. #if defined(WITH_GPERF_TOOLS)
  1097. gchar prof_path[PATH_MAX];
  1098. const gchar *prefix;
  1099. if (getenv ("CPUPROFILE")) {
  1100. /* disable inherited Profiler enabled in master process */
  1101. ProfilerStop ();
  1102. }
  1103. if (cfg != NULL) {
  1104. /* Try to create temp directory for gmon.out and chdir to it */
  1105. if (cfg->profile_path == NULL) {
  1106. cfg->profile_path =
  1107. g_strdup_printf ("%s/rspamd-profile", cfg->temp_dir);
  1108. }
  1109. prefix = cfg->profile_path;
  1110. }
  1111. else {
  1112. prefix = "/tmp/rspamd-profile";
  1113. }
  1114. snprintf (prof_path,
  1115. sizeof (prof_path),
  1116. "%s-%s.%d",
  1117. prefix,
  1118. descr,
  1119. (gint)getpid ());
  1120. if (ProfilerStart (prof_path)) {
  1121. /* start ITIMER_PROF timer */
  1122. ProfilerRegisterThread ();
  1123. }
  1124. else {
  1125. msg_warn ("cannot start google perftools profiler");
  1126. }
  1127. #endif
  1128. }
  1129. void
  1130. gperf_profiler_stop (void)
  1131. {
  1132. #if defined(WITH_GPERF_TOOLS)
  1133. ProfilerStop ();
  1134. #endif
  1135. }
  1136. #ifdef HAVE_FLOCK
  1137. /* Flock version */
  1138. gboolean
  1139. rspamd_file_lock (gint fd, gboolean async)
  1140. {
  1141. gint flags;
  1142. if (async) {
  1143. flags = LOCK_EX | LOCK_NB;
  1144. }
  1145. else {
  1146. flags = LOCK_EX;
  1147. }
  1148. if (flock (fd, flags) == -1) {
  1149. if (async && errno == EAGAIN) {
  1150. return FALSE;
  1151. }
  1152. if (errno != ENOTSUP) {
  1153. msg_warn ("lock on file failed: %s", strerror (errno));
  1154. }
  1155. return FALSE;
  1156. }
  1157. return TRUE;
  1158. }
  1159. gboolean
  1160. rspamd_file_unlock (gint fd, gboolean async)
  1161. {
  1162. gint flags;
  1163. if (async) {
  1164. flags = LOCK_UN | LOCK_NB;
  1165. }
  1166. else {
  1167. flags = LOCK_UN;
  1168. }
  1169. if (flock (fd, flags) == -1) {
  1170. if (async && errno == EAGAIN) {
  1171. return FALSE;
  1172. }
  1173. if (errno != ENOTSUP) {
  1174. msg_warn ("unlock on file failed: %s", strerror (errno));
  1175. }
  1176. return FALSE;
  1177. }
  1178. return TRUE;
  1179. }
  1180. #else /* HAVE_FLOCK */
  1181. /* Fctnl version */
  1182. gboolean
  1183. rspamd_file_lock (gint fd, gboolean async)
  1184. {
  1185. struct flock fl = {
  1186. .l_type = F_WRLCK,
  1187. .l_whence = SEEK_SET,
  1188. .l_start = 0,
  1189. .l_len = 0
  1190. };
  1191. if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) {
  1192. if (async && (errno == EAGAIN || errno == EACCES)) {
  1193. return FALSE;
  1194. }
  1195. if (errno != ENOTSUP) {
  1196. msg_warn ("lock on file failed: %s", strerror (errno));
  1197. }
  1198. return FALSE;
  1199. }
  1200. return TRUE;
  1201. }
  1202. gboolean
  1203. rspamd_file_unlock (gint fd, gboolean async)
  1204. {
  1205. struct flock fl = {
  1206. .l_type = F_UNLCK,
  1207. .l_whence = SEEK_SET,
  1208. .l_start = 0,
  1209. .l_len = 0
  1210. };
  1211. if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) {
  1212. if (async && (errno == EAGAIN || errno == EACCES)) {
  1213. return FALSE;
  1214. }
  1215. if (errno != ENOTSUP) {
  1216. msg_warn ("unlock on file failed: %s", strerror (errno));
  1217. }
  1218. return FALSE;
  1219. }
  1220. return TRUE;
  1221. }
  1222. #endif /* HAVE_FLOCK */
  1223. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 22))
  1224. void
  1225. g_ptr_array_unref (GPtrArray *array)
  1226. {
  1227. g_ptr_array_free (array, TRUE);
  1228. }
  1229. gboolean
  1230. g_int64_equal (gconstpointer v1, gconstpointer v2)
  1231. {
  1232. return *((const gint64*) v1) == *((const gint64*) v2);
  1233. }
  1234. guint
  1235. g_int64_hash (gconstpointer v)
  1236. {
  1237. guint64 v64 = *(guint64 *)v;
  1238. return (guint) (v ^ (v >> 32));
  1239. }
  1240. #endif
  1241. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 14))
  1242. void
  1243. g_queue_clear (GQueue *queue)
  1244. {
  1245. g_return_if_fail (queue != NULL);
  1246. g_list_free (queue->head);
  1247. queue->head = queue->tail = NULL;
  1248. queue->length = 0;
  1249. }
  1250. #endif
  1251. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 30))
  1252. GPtrArray*
  1253. g_ptr_array_new_full (guint reserved_size,
  1254. GDestroyNotify element_free_func)
  1255. {
  1256. GPtrArray *array;
  1257. array = g_ptr_array_sized_new (reserved_size);
  1258. g_ptr_array_set_free_func (array, element_free_func);
  1259. return array;
  1260. }
  1261. #endif
  1262. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32))
  1263. void
  1264. g_queue_free_full (GQueue *queue, GDestroyNotify free_func)
  1265. {
  1266. GList *cur;
  1267. cur = queue->head;
  1268. while (cur) {
  1269. free_func (cur->data);
  1270. cur = g_list_next (cur);
  1271. }
  1272. g_queue_free (queue);
  1273. }
  1274. #endif
  1275. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 40))
  1276. void
  1277. g_ptr_array_insert (GPtrArray *array, gint index_, gpointer data)
  1278. {
  1279. g_return_if_fail (array);
  1280. g_return_if_fail (index_ >= -1);
  1281. g_return_if_fail (index_ <= (gint )array->len);
  1282. g_ptr_array_set_size (array, array->len + 1);
  1283. if (index_ < 0) {
  1284. index_ = array->len;
  1285. }
  1286. if (index_ < array->len) {
  1287. memmove (&(array->pdata[index_ + 1]), &(array->pdata[index_]),
  1288. (array->len - index_) * sizeof(gpointer));
  1289. }
  1290. array->pdata[index_] = data;
  1291. }
  1292. #endif
  1293. gint
  1294. rspamd_fallocate (gint fd, off_t offset, off_t len)
  1295. {
  1296. #if defined(HAVE_FALLOCATE)
  1297. return fallocate (fd, 0, offset, len);
  1298. #elif defined(HAVE_POSIX_FALLOCATE)
  1299. return posix_fallocate (fd, offset, len);
  1300. #else
  1301. /* Return 0 as nothing can be done on this system */
  1302. return 0;
  1303. #endif
  1304. }
  1305. /**
  1306. * Create new mutex
  1307. * @return mutex or NULL
  1308. */
  1309. inline rspamd_mutex_t *
  1310. rspamd_mutex_new (void)
  1311. {
  1312. rspamd_mutex_t *new;
  1313. new = g_malloc0 (sizeof (rspamd_mutex_t));
  1314. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1315. g_mutex_init (&new->mtx);
  1316. #else
  1317. g_static_mutex_init (&new->mtx);
  1318. #endif
  1319. return new;
  1320. }
  1321. /**
  1322. * Lock mutex
  1323. * @param mtx
  1324. */
  1325. inline void
  1326. rspamd_mutex_lock (rspamd_mutex_t *mtx)
  1327. {
  1328. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1329. g_mutex_lock (&mtx->mtx);
  1330. #else
  1331. g_static_mutex_lock (&mtx->mtx);
  1332. #endif
  1333. }
  1334. /**
  1335. * Unlock mutex
  1336. * @param mtx
  1337. */
  1338. inline void
  1339. rspamd_mutex_unlock (rspamd_mutex_t *mtx)
  1340. {
  1341. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1342. g_mutex_unlock (&mtx->mtx);
  1343. #else
  1344. g_static_mutex_unlock (&mtx->mtx);
  1345. #endif
  1346. }
  1347. void
  1348. rspamd_mutex_free (rspamd_mutex_t *mtx)
  1349. {
  1350. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1351. g_mutex_clear (&mtx->mtx);
  1352. #endif
  1353. g_free (mtx);
  1354. }
  1355. struct rspamd_thread_data {
  1356. gchar *name;
  1357. gint id;
  1358. GThreadFunc func;
  1359. gpointer data;
  1360. };
  1361. static gpointer
  1362. rspamd_thread_func (gpointer ud)
  1363. {
  1364. struct rspamd_thread_data *td = ud;
  1365. sigset_t s_mask;
  1366. /* Ignore signals in thread */
  1367. sigemptyset (&s_mask);
  1368. sigaddset (&s_mask, SIGINT);
  1369. sigaddset (&s_mask, SIGHUP);
  1370. sigaddset (&s_mask, SIGCHLD);
  1371. sigaddset (&s_mask, SIGUSR1);
  1372. sigaddset (&s_mask, SIGUSR2);
  1373. sigaddset (&s_mask, SIGALRM);
  1374. sigaddset (&s_mask, SIGPIPE);
  1375. pthread_sigmask (SIG_BLOCK, &s_mask, NULL);
  1376. ud = td->func (td->data);
  1377. g_free (td->name);
  1378. g_free (td);
  1379. return ud;
  1380. }
  1381. /**
  1382. * Create new named thread
  1383. * @param name name pattern
  1384. * @param func function to start
  1385. * @param data data to pass to function
  1386. * @param err error pointer
  1387. * @return new thread object that can be joined
  1388. */
  1389. GThread *
  1390. rspamd_create_thread (const gchar *name,
  1391. GThreadFunc func,
  1392. gpointer data,
  1393. GError **err)
  1394. {
  1395. GThread *new;
  1396. struct rspamd_thread_data *td;
  1397. static gint32 id;
  1398. guint r;
  1399. r = strlen (name);
  1400. td = g_malloc (sizeof (struct rspamd_thread_data));
  1401. td->id = ++id;
  1402. td->name = g_malloc (r + sizeof ("4294967296"));
  1403. td->func = func;
  1404. td->data = data;
  1405. rspamd_snprintf (td->name, r + sizeof ("4294967296"), "%s-%d", name, id);
  1406. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 32))
  1407. new = g_thread_try_new (td->name, rspamd_thread_func, td, err);
  1408. #else
  1409. new = g_thread_create (rspamd_thread_func, td, TRUE, err);
  1410. #endif
  1411. return new;
  1412. }
  1413. struct hash_copy_callback_data {
  1414. gpointer (*key_copy_func)(gconstpointer data, gpointer ud);
  1415. gpointer (*value_copy_func)(gconstpointer data, gpointer ud);
  1416. gpointer ud;
  1417. GHashTable *dst;
  1418. };
  1419. static void
  1420. copy_foreach_callback (gpointer key, gpointer value, gpointer ud)
  1421. {
  1422. struct hash_copy_callback_data *cb = ud;
  1423. gpointer nkey, nvalue;
  1424. nkey = cb->key_copy_func ? cb->key_copy_func (key, cb->ud) : (gpointer)key;
  1425. nvalue =
  1426. cb->value_copy_func ? cb->value_copy_func (value,
  1427. cb->ud) : (gpointer)value;
  1428. g_hash_table_insert (cb->dst, nkey, nvalue);
  1429. }
  1430. /**
  1431. * Deep copy of one hash table to another
  1432. * @param src source hash
  1433. * @param dst destination hash
  1434. * @param key_copy_func function called to copy or modify keys (or NULL)
  1435. * @param value_copy_func function called to copy or modify values (or NULL)
  1436. * @param ud user data for copy functions
  1437. */
  1438. void
  1439. rspamd_hash_table_copy (GHashTable *src, GHashTable *dst,
  1440. gpointer (*key_copy_func)(gconstpointer data, gpointer ud),
  1441. gpointer (*value_copy_func)(gconstpointer data, gpointer ud),
  1442. gpointer ud)
  1443. {
  1444. struct hash_copy_callback_data cb;
  1445. if (src != NULL && dst != NULL) {
  1446. cb.key_copy_func = key_copy_func;
  1447. cb.value_copy_func = value_copy_func;
  1448. cb.ud = ud;
  1449. cb.dst = dst;
  1450. g_hash_table_foreach (src, copy_foreach_callback, &cb);
  1451. }
  1452. }
  1453. static volatile sig_atomic_t saved_signo[NSIG];
  1454. static
  1455. void
  1456. read_pass_tmp_sig_handler (int s)
  1457. {
  1458. saved_signo[s] = 1;
  1459. }
  1460. #ifndef _PATH_TTY
  1461. # define _PATH_TTY "/dev/tty"
  1462. #endif
  1463. gint
  1464. rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key)
  1465. {
  1466. #ifdef HAVE_PASSPHRASE_H
  1467. gint len = 0;
  1468. gchar pass[BUFSIZ];
  1469. if (readpassphrase ("Enter passphrase: ", buf, size, RPP_ECHO_OFF |
  1470. RPP_REQUIRE_TTY) == NULL) {
  1471. return 0;
  1472. }
  1473. return strlen (buf);
  1474. #else
  1475. struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
  1476. struct sigaction savetstp, savettin, savettou, savepipe;
  1477. struct termios term, oterm;
  1478. gint input, output, i;
  1479. gchar *end, *p, ch;
  1480. restart:
  1481. if ((input = output = open (_PATH_TTY, O_RDWR)) == -1) {
  1482. errno = ENOTTY;
  1483. return 0;
  1484. }
  1485. if (fcntl (input, F_SETFD, FD_CLOEXEC) == -1) {
  1486. msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno));
  1487. }
  1488. /* Turn echo off */
  1489. if (tcgetattr (input, &oterm) != 0) {
  1490. close (input);
  1491. errno = ENOTTY;
  1492. return 0;
  1493. }
  1494. memcpy (&term, &oterm, sizeof(term));
  1495. term.c_lflag &= ~(ECHO | ECHONL);
  1496. if (tcsetattr (input, TCSAFLUSH, &term) == -1) {
  1497. errno = ENOTTY;
  1498. close (input);
  1499. return 0;
  1500. }
  1501. g_assert (write (output, "Enter passphrase: ", sizeof ("Enter passphrase: ") -
  1502. 1) != -1);
  1503. /* Save the current sighandler */
  1504. for (i = 0; i < NSIG; i++) {
  1505. saved_signo[i] = 0;
  1506. }
  1507. sigemptyset (&sa.sa_mask);
  1508. sa.sa_flags = 0;
  1509. sa.sa_handler = read_pass_tmp_sig_handler;
  1510. (void)sigaction (SIGALRM, &sa, &savealrm);
  1511. (void)sigaction (SIGHUP, &sa, &savehup);
  1512. (void)sigaction (SIGINT, &sa, &saveint);
  1513. (void)sigaction (SIGPIPE, &sa, &savepipe);
  1514. (void)sigaction (SIGQUIT, &sa, &savequit);
  1515. (void)sigaction (SIGTERM, &sa, &saveterm);
  1516. (void)sigaction (SIGTSTP, &sa, &savetstp);
  1517. (void)sigaction (SIGTTIN, &sa, &savettin);
  1518. (void)sigaction (SIGTTOU, &sa, &savettou);
  1519. /* Now read a passphrase */
  1520. p = buf;
  1521. end = p + size - 1;
  1522. while (read (input, &ch, 1) == 1 && ch != '\n' && ch != '\r') {
  1523. if (p < end) {
  1524. *p++ = ch;
  1525. }
  1526. }
  1527. *p = '\0';
  1528. g_assert (write (output, "\n", 1) == 1);
  1529. /* Restore terminal state */
  1530. if (memcmp (&term, &oterm, sizeof (term)) != 0) {
  1531. while (tcsetattr (input, TCSAFLUSH, &oterm) == -1 &&
  1532. errno == EINTR && !saved_signo[SIGTTOU]) ;
  1533. }
  1534. /* Restore signal handlers */
  1535. (void)sigaction (SIGALRM, &savealrm, NULL);
  1536. (void)sigaction (SIGHUP, &savehup, NULL);
  1537. (void)sigaction (SIGINT, &saveint, NULL);
  1538. (void)sigaction (SIGQUIT, &savequit, NULL);
  1539. (void)sigaction (SIGPIPE, &savepipe, NULL);
  1540. (void)sigaction (SIGTERM, &saveterm, NULL);
  1541. (void)sigaction (SIGTSTP, &savetstp, NULL);
  1542. (void)sigaction (SIGTTIN, &savettin, NULL);
  1543. (void)sigaction (SIGTTOU, &savettou, NULL);
  1544. close (input);
  1545. /* Send signals pending */
  1546. for (i = 0; i < NSIG; i++) {
  1547. if (saved_signo[i]) {
  1548. kill (getpid (), i);
  1549. switch (i) {
  1550. case SIGTSTP:
  1551. case SIGTTIN:
  1552. case SIGTTOU:
  1553. goto restart;
  1554. }
  1555. }
  1556. }
  1557. return (p - buf);
  1558. #endif
  1559. }
  1560. #ifdef HAVE_CLOCK_GETTIME
  1561. # ifdef CLOCK_MONOTONIC_COARSE
  1562. # define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_COARSE
  1563. # elif defined(CLOCK_MONOTONIC_FAST)
  1564. # define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_FAST
  1565. # else
  1566. # define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC
  1567. # endif
  1568. #endif
  1569. gdouble
  1570. rspamd_get_ticks (gboolean rdtsc_ok)
  1571. {
  1572. gdouble res;
  1573. #ifdef HAVE_RDTSC
  1574. # ifdef __x86_64__
  1575. guint64 r64;
  1576. if (rdtsc_ok) {
  1577. __builtin_ia32_lfence ();
  1578. r64 = __rdtsc ();
  1579. /* Preserve lower 52 bits */
  1580. res = r64 & ((1ULL << 53) - 1);
  1581. return res;
  1582. }
  1583. # endif
  1584. #endif
  1585. #ifdef HAVE_CLOCK_GETTIME
  1586. struct timespec ts;
  1587. gint clk_id = RSPAMD_FAST_MONOTONIC_CLOCK;
  1588. clock_gettime (clk_id, &ts);
  1589. if (rdtsc_ok) {
  1590. res = (double) ts.tv_sec * 1e9 + ts.tv_nsec;
  1591. }
  1592. else {
  1593. res = (double) ts.tv_sec + ts.tv_nsec / 1000000000.;
  1594. }
  1595. # elif defined(__APPLE__)
  1596. if (rdtsc_ok) {
  1597. res = mach_absolute_time ();
  1598. }
  1599. else {
  1600. res = mach_absolute_time () / 1000000000.;
  1601. }
  1602. #else
  1603. struct timeval tv;
  1604. (void)gettimeofday (&tv, NULL);
  1605. if (rdtsc_ok) {
  1606. res = (double) ts.tv_sec * 1e9 + tv.tv_usec * 1e3;
  1607. }
  1608. else {
  1609. res = (double)tv.tv_sec + tv.tv_usec / 1000000.;
  1610. }
  1611. #endif
  1612. return res;
  1613. }
  1614. gdouble
  1615. rspamd_get_virtual_ticks (void)
  1616. {
  1617. gdouble res;
  1618. #ifdef HAVE_CLOCK_GETTIME
  1619. struct timespec ts;
  1620. static clockid_t cid = (clockid_t)-1;
  1621. if (cid == (clockid_t)-1) {
  1622. # ifdef HAVE_CLOCK_GETCPUCLOCKID
  1623. if (clock_getcpuclockid (0, &cid) == -1) {
  1624. # endif
  1625. # ifdef CLOCK_PROCESS_CPUTIME_ID
  1626. cid = CLOCK_PROCESS_CPUTIME_ID;
  1627. # elif defined(CLOCK_PROF)
  1628. cid = CLOCK_PROF;
  1629. # else
  1630. cid = CLOCK_REALTIME;
  1631. # endif
  1632. # ifdef HAVE_CLOCK_GETCPUCLOCKID
  1633. }
  1634. # endif
  1635. }
  1636. clock_gettime (cid, &ts);
  1637. res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.;
  1638. #elif defined(__APPLE__)
  1639. thread_port_t thread = mach_thread_self ();
  1640. mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
  1641. thread_basic_info_data_t info;
  1642. if (thread_info (thread, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS) {
  1643. return -1;
  1644. }
  1645. res = info.user_time.seconds + info.system_time.seconds;
  1646. res += ((gdouble)(info.user_time.microseconds + info.system_time.microseconds)) / 1e6;
  1647. mach_port_deallocate(mach_task_self(), thread);
  1648. #elif defined(HAVE_RUSAGE_SELF)
  1649. struct rusage rusage;
  1650. if (getrusage (RUSAGE_SELF, &rusage) != -1) {
  1651. res = (double) rusage.ru_utime.tv_sec +
  1652. (double) rusage.ru_utime.tv_usec / 1000000.0;
  1653. }
  1654. #else
  1655. res = clock () / (double)CLOCKS_PER_SEC;
  1656. #endif
  1657. return res;
  1658. }
  1659. gdouble
  1660. rspamd_get_calendar_ticks (void)
  1661. {
  1662. gdouble res;
  1663. #ifdef HAVE_CLOCK_GETTIME
  1664. struct timespec ts;
  1665. clock_gettime (CLOCK_REALTIME, &ts);
  1666. res = ts_to_double (&ts);
  1667. #else
  1668. struct timeval tv;
  1669. if (gettimeofday (&tv, NULL) == 0) {
  1670. res = tv_to_double (&tv);
  1671. }
  1672. else {
  1673. res = time (NULL);
  1674. }
  1675. #endif
  1676. return res;
  1677. }
  1678. /* Required for tweetnacl */
  1679. void
  1680. randombytes (guchar *buf, guint64 len)
  1681. {
  1682. ottery_rand_bytes (buf, (size_t)len);
  1683. }
  1684. void
  1685. rspamd_random_hex (guchar *buf, guint64 len)
  1686. {
  1687. static const gchar hexdigests[16] = "0123456789abcdef";
  1688. gint64 i;
  1689. g_assert (len > 0);
  1690. ottery_rand_bytes (buf, ceil (len / 2.0));
  1691. for (i = (gint64)len - 1; i >= 0; i -= 2) {
  1692. buf[i] = hexdigests[buf[i / 2] & 0xf];
  1693. if (i > 0) {
  1694. buf[i - 1] = hexdigests[(buf[i / 2] >> 4) & 0xf];
  1695. }
  1696. }
  1697. }
  1698. gint
  1699. rspamd_shmem_mkstemp (gchar *pattern)
  1700. {
  1701. gint fd = -1;
  1702. gchar *nbuf, *xpos;
  1703. gsize blen;
  1704. xpos = strchr (pattern, 'X');
  1705. if (xpos == NULL) {
  1706. errno = EINVAL;
  1707. return -1;
  1708. }
  1709. blen = strlen (pattern);
  1710. nbuf = g_malloc (blen + 1);
  1711. rspamd_strlcpy (nbuf, pattern, blen + 1);
  1712. xpos = nbuf + (xpos - pattern);
  1713. for (;;) {
  1714. rspamd_random_hex (xpos, blen - (xpos - nbuf));
  1715. fd = shm_open (nbuf, O_RDWR | O_EXCL | O_CREAT, 0600);
  1716. if (fd != -1) {
  1717. rspamd_strlcpy (pattern, nbuf, blen + 1);
  1718. break;
  1719. }
  1720. else if (errno != EEXIST) {
  1721. msg_err ("%s: failed to create temp shmem %s: %s",
  1722. G_STRLOC, nbuf, strerror (errno));
  1723. g_free (nbuf);
  1724. return -1;
  1725. }
  1726. }
  1727. g_free (nbuf);
  1728. return fd;
  1729. }
  1730. void
  1731. rspamd_ptr_array_free_hard (gpointer p)
  1732. {
  1733. GPtrArray *ar = (GPtrArray *)p;
  1734. g_ptr_array_free (ar, TRUE);
  1735. }
  1736. void
  1737. rspamd_array_free_hard (gpointer p)
  1738. {
  1739. GArray *ar = (GArray *)p;
  1740. g_array_free (ar, TRUE);
  1741. }
  1742. void
  1743. rspamd_gstring_free_hard (gpointer p)
  1744. {
  1745. GString *ar = (GString *)p;
  1746. g_string_free (ar, TRUE);
  1747. }
  1748. void rspamd_gerror_free_maybe (gpointer p)
  1749. {
  1750. GError **err;
  1751. if (p) {
  1752. err = (GError **)p;
  1753. if (*err) {
  1754. g_error_free (*err);
  1755. }
  1756. }
  1757. }
  1758. struct rspamd_external_libs_ctx *
  1759. rspamd_init_libs (void)
  1760. {
  1761. struct rlimit rlim;
  1762. struct rspamd_external_libs_ctx *ctx;
  1763. struct ottery_config *ottery_cfg;
  1764. gint ssl_options;
  1765. ctx = g_malloc0 (sizeof (*ctx));
  1766. ctx->crypto_ctx = rspamd_cryptobox_init ();
  1767. ottery_cfg = g_malloc0 (ottery_get_sizeof_config ());
  1768. ottery_config_init (ottery_cfg);
  1769. ctx->ottery_cfg = ottery_cfg;
  1770. /* Check if we have rdrand */
  1771. if ((ctx->crypto_ctx->cpu_config & CPUID_RDRAND) == 0) {
  1772. ottery_config_disable_entropy_sources (ottery_cfg,
  1773. OTTERY_ENTROPY_SRC_RDRAND);
  1774. }
  1775. g_assert (ottery_init (ottery_cfg) == 0);
  1776. #ifdef HAVE_LOCALE_H
  1777. if (getenv ("LANG") == NULL) {
  1778. setlocale (LC_ALL, "C");
  1779. setlocale (LC_CTYPE, "C");
  1780. setlocale (LC_MESSAGES, "C");
  1781. setlocale (LC_TIME, "C");
  1782. }
  1783. else {
  1784. /* Just set the default locale */
  1785. setlocale (LC_ALL, "");
  1786. /* But for some issues we still want C locale */
  1787. setlocale (LC_NUMERIC, "C");
  1788. }
  1789. #endif
  1790. #ifdef HAVE_OPENSSL
  1791. ERR_load_crypto_strings ();
  1792. SSL_load_error_strings ();
  1793. OpenSSL_add_all_algorithms ();
  1794. OpenSSL_add_all_digests ();
  1795. OpenSSL_add_all_ciphers ();
  1796. #if OPENSSL_VERSION_NUMBER >= 0x1000104fL && !defined(LIBRESSL_VERSION_NUMBER)
  1797. ENGINE_load_builtin_engines ();
  1798. if ((ctx->crypto_ctx->cpu_config & CPUID_RDRAND) == 0) {
  1799. RAND_set_rand_engine (NULL);
  1800. }
  1801. #endif
  1802. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  1803. SSL_library_init ();
  1804. #else
  1805. OPENSSL_init_ssl (0, NULL);
  1806. #endif
  1807. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  1808. OPENSSL_config (NULL);
  1809. #endif
  1810. if (RAND_poll () == 0) {
  1811. guchar seed[128];
  1812. /* Try to use ottery to seed rand */
  1813. ottery_rand_bytes (seed, sizeof (seed));
  1814. RAND_seed (seed, sizeof (seed));
  1815. rspamd_explicit_memzero (seed, sizeof (seed));
  1816. }
  1817. ctx->ssl_ctx = SSL_CTX_new (SSLv23_method ());
  1818. SSL_CTX_set_verify (ctx->ssl_ctx, SSL_VERIFY_PEER, NULL);
  1819. SSL_CTX_set_verify_depth (ctx->ssl_ctx, 4);
  1820. ssl_options = SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3;
  1821. #ifdef SSL_OP_NO_COMPRESSION
  1822. ssl_options |= SSL_OP_NO_COMPRESSION;
  1823. #elif OPENSSL_VERSION_NUMBER >= 0x00908000L
  1824. sk_SSL_COMP_zero (SSL_COMP_get_compression_methods ());
  1825. #endif
  1826. SSL_CTX_set_options (ctx->ssl_ctx, ssl_options);
  1827. ctx->ssl_ctx_noverify = SSL_CTX_new (SSLv23_method ());
  1828. SSL_CTX_set_verify (ctx->ssl_ctx_noverify, SSL_VERIFY_NONE, NULL);
  1829. SSL_CTX_set_options (ctx->ssl_ctx_noverify, ssl_options);
  1830. #endif
  1831. #ifdef SSL_SESS_CACHE_BOTH
  1832. SSL_CTX_set_session_cache_mode (ctx->ssl_ctx, SSL_SESS_CACHE_BOTH);
  1833. #endif
  1834. rspamd_random_seed_fast ();
  1835. /* Set stack size for pcre */
  1836. getrlimit (RLIMIT_STACK, &rlim);
  1837. rlim.rlim_cur = 100 * 1024 * 1024;
  1838. rlim.rlim_max = rlim.rlim_cur;
  1839. setrlimit (RLIMIT_STACK, &rlim);
  1840. gint magic_flags = 0;
  1841. /* Unless trusty and other crap is supported... */
  1842. #if 0
  1843. #ifdef MAGIC_NO_CHECK_BUILTIN
  1844. magic_flags = MAGIC_NO_CHECK_BUILTIN;
  1845. #endif
  1846. #endif
  1847. magic_flags |= MAGIC_MIME|MAGIC_NO_CHECK_COMPRESS|
  1848. MAGIC_NO_CHECK_ELF|MAGIC_NO_CHECK_TAR;
  1849. #ifdef MAGIC_NO_CHECK_CDF
  1850. magic_flags |= MAGIC_NO_CHECK_CDF;
  1851. #endif
  1852. #ifdef MAGIC_NO_CHECK_ENCODING
  1853. magic_flags |= MAGIC_NO_CHECK_ENCODING;
  1854. #endif
  1855. #ifdef MAGIC_NO_CHECK_TAR
  1856. magic_flags |= MAGIC_NO_CHECK_TAR;
  1857. #endif
  1858. #ifdef MAGIC_NO_CHECK_TEXT
  1859. magic_flags |= MAGIC_NO_CHECK_TEXT;
  1860. #endif
  1861. #ifdef MAGIC_NO_CHECK_TOKENS
  1862. magic_flags |= MAGIC_NO_CHECK_TOKENS;
  1863. #endif
  1864. #ifdef MAGIC_NO_CHECK_JSON
  1865. magic_flags |= MAGIC_NO_CHECK_JSON;
  1866. #endif
  1867. ctx->libmagic = magic_open (magic_flags);
  1868. ctx->local_addrs = rspamd_inet_library_init ();
  1869. REF_INIT_RETAIN (ctx, rspamd_deinit_libs);
  1870. return ctx;
  1871. }
  1872. static struct zstd_dictionary *
  1873. rspamd_open_zstd_dictionary (const char *path)
  1874. {
  1875. struct zstd_dictionary *dict;
  1876. dict = g_malloc0 (sizeof (*dict));
  1877. dict->dict = rspamd_file_xmap (path, PROT_READ, &dict->size, TRUE);
  1878. if (dict->dict == NULL) {
  1879. g_free (dict);
  1880. return NULL;
  1881. }
  1882. dict->id = ZDICT_getDictID (dict->dict, dict->size);
  1883. if (dict->id == 0) {
  1884. g_free (dict);
  1885. return NULL;
  1886. }
  1887. return dict;
  1888. }
  1889. static void
  1890. rspamd_free_zstd_dictionary (struct zstd_dictionary *dict)
  1891. {
  1892. if (dict) {
  1893. munmap (dict->dict, dict->size);
  1894. g_free (dict);
  1895. }
  1896. }
  1897. void
  1898. rspamd_config_libs (struct rspamd_external_libs_ctx *ctx,
  1899. struct rspamd_config *cfg)
  1900. {
  1901. static const char secure_ciphers[] = "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4";
  1902. size_t r;
  1903. g_assert (cfg != NULL);
  1904. if (ctx != NULL) {
  1905. if (cfg->local_addrs) {
  1906. rspamd_config_radix_from_ucl (cfg, cfg->local_addrs,
  1907. "Local addresses",
  1908. ctx->local_addrs, NULL);
  1909. }
  1910. if (cfg->ssl_ca_path) {
  1911. if (SSL_CTX_load_verify_locations (ctx->ssl_ctx, cfg->ssl_ca_path,
  1912. NULL) != 1) {
  1913. msg_err_config ("cannot load CA certs from %s: %s",
  1914. cfg->ssl_ca_path,
  1915. ERR_error_string (ERR_get_error (), NULL));
  1916. }
  1917. } else {
  1918. msg_debug_config ("ssl_ca_path is not set, using default CA path");
  1919. SSL_CTX_set_default_verify_paths (ctx->ssl_ctx);
  1920. }
  1921. if (cfg->ssl_ciphers) {
  1922. if (SSL_CTX_set_cipher_list (ctx->ssl_ctx, cfg->ssl_ciphers) != 1) {
  1923. msg_err_config (
  1924. "cannot set ciphers set to %s: %s; fallback to %s",
  1925. cfg->ssl_ciphers,
  1926. ERR_error_string (ERR_get_error (), NULL),
  1927. secure_ciphers);
  1928. /* Default settings */
  1929. SSL_CTX_set_cipher_list (ctx->ssl_ctx, secure_ciphers);
  1930. }
  1931. }
  1932. if (ctx->libmagic) {
  1933. magic_load (ctx->libmagic, cfg->magic_file);
  1934. }
  1935. rspamd_free_zstd_dictionary (ctx->in_dict);
  1936. rspamd_free_zstd_dictionary (ctx->out_dict);
  1937. if (ctx->out_zstream) {
  1938. ZSTD_freeCStream (ctx->out_zstream);
  1939. ctx->out_zstream = NULL;
  1940. }
  1941. if (ctx->in_zstream) {
  1942. ZSTD_freeDStream (ctx->in_zstream);
  1943. ctx->in_zstream = NULL;
  1944. }
  1945. if (cfg->zstd_input_dictionary) {
  1946. ctx->in_dict = rspamd_open_zstd_dictionary (
  1947. cfg->zstd_input_dictionary);
  1948. if (ctx->in_dict == NULL) {
  1949. msg_err_config ("cannot open zstd dictionary in %s",
  1950. cfg->zstd_input_dictionary);
  1951. }
  1952. }
  1953. if (cfg->zstd_output_dictionary) {
  1954. ctx->out_dict = rspamd_open_zstd_dictionary (
  1955. cfg->zstd_output_dictionary);
  1956. if (ctx->out_dict == NULL) {
  1957. msg_err_config ("cannot open zstd dictionary in %s",
  1958. cfg->zstd_output_dictionary);
  1959. }
  1960. }
  1961. /* Init decompression */
  1962. ctx->in_zstream = ZSTD_createDStream ();
  1963. r = ZSTD_initDStream (ctx->in_zstream);
  1964. if (ZSTD_isError (r)) {
  1965. msg_err ("cannot init decompression stream: %s",
  1966. ZSTD_getErrorName (r));
  1967. ZSTD_freeDStream (ctx->in_zstream);
  1968. ctx->in_zstream = NULL;
  1969. }
  1970. /* Init compression */
  1971. ctx->out_zstream = ZSTD_createCStream ();
  1972. r = ZSTD_initCStream (ctx->out_zstream, 1);
  1973. if (ZSTD_isError (r)) {
  1974. msg_err ("cannot init compression stream: %s",
  1975. ZSTD_getErrorName (r));
  1976. ZSTD_freeCStream (ctx->out_zstream);
  1977. ctx->out_zstream = NULL;
  1978. }
  1979. }
  1980. }
  1981. gboolean
  1982. rspamd_libs_reset_decompression (struct rspamd_external_libs_ctx *ctx)
  1983. {
  1984. gsize r;
  1985. if (ctx->in_zstream == NULL) {
  1986. return FALSE;
  1987. }
  1988. else {
  1989. r = ZSTD_resetDStream (ctx->in_zstream);
  1990. if (ZSTD_isError (r)) {
  1991. msg_err ("cannot init decompression stream: %s",
  1992. ZSTD_getErrorName (r));
  1993. ZSTD_freeDStream (ctx->in_zstream);
  1994. ctx->in_zstream = NULL;
  1995. return FALSE;
  1996. }
  1997. }
  1998. return TRUE;
  1999. }
  2000. gboolean
  2001. rspamd_libs_reset_compression (struct rspamd_external_libs_ctx *ctx)
  2002. {
  2003. gsize r;
  2004. if (ctx->out_zstream == NULL) {
  2005. return FALSE;
  2006. }
  2007. else {
  2008. /* Dictionary will be reused automatically if specified */
  2009. r = ZSTD_resetCStream (ctx->out_zstream, 0);
  2010. if (ZSTD_isError (r)) {
  2011. msg_err ("cannot init compression stream: %s",
  2012. ZSTD_getErrorName (r));
  2013. ZSTD_freeCStream (ctx->out_zstream);
  2014. ctx->out_zstream = NULL;
  2015. return FALSE;
  2016. }
  2017. }
  2018. return TRUE;
  2019. }
  2020. void
  2021. rspamd_deinit_libs (struct rspamd_external_libs_ctx *ctx)
  2022. {
  2023. if (ctx != NULL) {
  2024. if (ctx->libmagic) {
  2025. magic_close (ctx->libmagic);
  2026. }
  2027. g_free (ctx->ottery_cfg);
  2028. #ifdef HAVE_OPENSSL
  2029. EVP_cleanup ();
  2030. ERR_free_strings ();
  2031. SSL_CTX_free (ctx->ssl_ctx);
  2032. SSL_CTX_free (ctx->ssl_ctx_noverify);
  2033. #endif
  2034. rspamd_inet_library_destroy ();
  2035. rspamd_free_zstd_dictionary (ctx->in_dict);
  2036. rspamd_free_zstd_dictionary (ctx->out_dict);
  2037. if (ctx->out_zstream) {
  2038. ZSTD_freeCStream (ctx->out_zstream);
  2039. }
  2040. if (ctx->in_zstream) {
  2041. ZSTD_freeDStream (ctx->in_zstream);
  2042. }
  2043. g_free (ctx);
  2044. }
  2045. }
  2046. guint64
  2047. rspamd_hash_seed (void)
  2048. {
  2049. static guint64 seed;
  2050. if (seed == 0) {
  2051. seed = ottery_rand_uint64 ();
  2052. }
  2053. return seed;
  2054. }
  2055. static inline gdouble
  2056. rspamd_double_from_int64 (guint64 x)
  2057. {
  2058. const union { guint64 i; double d; } u = {
  2059. .i = G_GUINT64_CONSTANT(0x3FF) << 52 | x >> 12
  2060. };
  2061. return u.d - 1.0;
  2062. }
  2063. gdouble
  2064. rspamd_random_double (void)
  2065. {
  2066. guint64 rnd_int;
  2067. rnd_int = ottery_rand_uint64 ();
  2068. return rspamd_double_from_int64 (rnd_int);
  2069. }
  2070. static guint64 xorshifto_seed[2];
  2071. static inline guint64
  2072. xoroshiro_rotl (const guint64 x, int k) {
  2073. return (x << k) | (x >> (64 - k));
  2074. }
  2075. gdouble
  2076. rspamd_random_double_fast (void)
  2077. {
  2078. const guint64 s0 = xorshifto_seed[0];
  2079. guint64 s1 = xorshifto_seed[1];
  2080. const guint64 result = s0 + s1;
  2081. s1 ^= s0;
  2082. xorshifto_seed[0] = xoroshiro_rotl(s0, 55) ^ s1 ^ (s1 << 14);
  2083. xorshifto_seed[1] = xoroshiro_rotl (s1, 36);
  2084. return rspamd_double_from_int64 (result);
  2085. }
  2086. guint64
  2087. rspamd_random_uint64_fast (void)
  2088. {
  2089. const guint64 s0 = xorshifto_seed[0];
  2090. guint64 s1 = xorshifto_seed[1];
  2091. const guint64 result = s0 + s1;
  2092. s1 ^= s0;
  2093. xorshifto_seed[0] = xoroshiro_rotl(s0, 55) ^ s1 ^ (s1 << 14);
  2094. xorshifto_seed[1] = xoroshiro_rotl (s1, 36);
  2095. return result;
  2096. }
  2097. void
  2098. rspamd_random_seed_fast (void)
  2099. {
  2100. ottery_rand_bytes (xorshifto_seed, sizeof (xorshifto_seed));
  2101. }
  2102. gdouble
  2103. rspamd_time_jitter (gdouble in, gdouble jitter)
  2104. {
  2105. if (jitter == 0) {
  2106. jitter = in;
  2107. }
  2108. return in + jitter * rspamd_random_double ();
  2109. }
  2110. gboolean
  2111. rspamd_constant_memcmp (const guchar *a, const guchar *b, gsize len)
  2112. {
  2113. gsize lena, lenb, i;
  2114. guint16 d, r = 0, m;
  2115. guint16 v;
  2116. if (len == 0) {
  2117. lena = strlen (a);
  2118. lenb = strlen (b);
  2119. if (lena != lenb) {
  2120. return FALSE;
  2121. }
  2122. len = lena;
  2123. }
  2124. for (i = 0; i < len; i++) {
  2125. v = ((guint16)(guint8)r) + 255;
  2126. m = v / 256 - 1;
  2127. d = (guint16)((int)a[i] - (int)b[i]);
  2128. r |= (d & m);
  2129. }
  2130. return (((gint32)(guint16)((guint32)r + 0x8000) - 0x8000) == 0);
  2131. }
  2132. #if !defined(LIBEVENT_VERSION_NUMBER) || LIBEVENT_VERSION_NUMBER < 0x02000000UL
  2133. struct event_base *
  2134. event_get_base (struct event *ev)
  2135. {
  2136. return ev->ev_base;
  2137. }
  2138. #endif
  2139. int
  2140. rspamd_event_pending (struct event *ev, short what)
  2141. {
  2142. if (ev->ev_base == NULL) {
  2143. return 0;
  2144. }
  2145. return event_pending (ev, what, NULL);
  2146. }
  2147. int
  2148. rspamd_file_xopen (const char *fname, int oflags, guint mode,
  2149. gboolean allow_symlink)
  2150. {
  2151. struct stat sb;
  2152. int fd, flags = oflags;
  2153. if (lstat (fname, &sb) == -1) {
  2154. if (errno != ENOENT) {
  2155. return (-1);
  2156. }
  2157. }
  2158. else if (!S_ISREG (sb.st_mode)) {
  2159. if (S_ISLNK (sb.st_mode)) {
  2160. if (!allow_symlink) {
  2161. return -1;
  2162. }
  2163. }
  2164. else {
  2165. return -1;
  2166. }
  2167. }
  2168. #ifdef HAVE_OCLOEXEC
  2169. flags |= O_CLOEXEC;
  2170. #endif
  2171. #ifdef HAVE_ONOFOLLOW
  2172. if (!allow_symlink) {
  2173. flags |= O_NOFOLLOW;
  2174. fd = open (fname, flags, mode);
  2175. }
  2176. else {
  2177. fd = open (fname, flags, mode);
  2178. }
  2179. #else
  2180. fd = open (fname, flags, mode);
  2181. #endif
  2182. #ifndef HAVE_OCLOEXEC
  2183. if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
  2184. msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno));
  2185. close (fd);
  2186. return -1;
  2187. }
  2188. #endif
  2189. return (fd);
  2190. }
  2191. gpointer
  2192. rspamd_file_xmap (const char *fname, guint mode, gsize *size,
  2193. gboolean allow_symlink)
  2194. {
  2195. gint fd;
  2196. struct stat sb;
  2197. gpointer map;
  2198. g_assert (fname != NULL);
  2199. g_assert (size != NULL);
  2200. if (mode & PROT_WRITE) {
  2201. fd = rspamd_file_xopen (fname, O_RDWR, 0, allow_symlink);
  2202. }
  2203. else {
  2204. fd = rspamd_file_xopen (fname, O_RDONLY, 0, allow_symlink);
  2205. }
  2206. if (fd == -1) {
  2207. return NULL;
  2208. }
  2209. if (fstat (fd, &sb) == -1 || !S_ISREG (sb.st_mode)) {
  2210. close (fd);
  2211. *size = (gsize)-1;
  2212. return NULL;
  2213. }
  2214. if (sb.st_size == 0) {
  2215. close (fd);
  2216. *size = (gsize)0;
  2217. return NULL;
  2218. }
  2219. map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0);
  2220. close (fd);
  2221. if (map == MAP_FAILED) {
  2222. return NULL;
  2223. }
  2224. *size = sb.st_size;
  2225. return map;
  2226. }
  2227. gpointer
  2228. rspamd_shmem_xmap (const char *fname, guint mode,
  2229. gsize *size)
  2230. {
  2231. gint fd;
  2232. struct stat sb;
  2233. gpointer map;
  2234. g_assert (fname != NULL);
  2235. g_assert (size != NULL);
  2236. #ifdef HAVE_SANE_SHMEM
  2237. if (mode & PROT_WRITE) {
  2238. fd = shm_open (fname, O_RDWR, 0);
  2239. }
  2240. else {
  2241. fd = shm_open (fname, O_RDONLY, 0);
  2242. }
  2243. #else
  2244. if (mode & PROT_WRITE) {
  2245. fd = open (fname, O_RDWR, 0);
  2246. }
  2247. else {
  2248. fd = open (fname, O_RDONLY, 0);
  2249. }
  2250. #endif
  2251. if (fd == -1) {
  2252. return NULL;
  2253. }
  2254. if (fstat (fd, &sb) == -1) {
  2255. close (fd);
  2256. return NULL;
  2257. }
  2258. map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0);
  2259. close (fd);
  2260. if (map == MAP_FAILED) {
  2261. return NULL;
  2262. }
  2263. *size = sb.st_size;
  2264. return map;
  2265. }
  2266. /*
  2267. * A(x - 0.5)^4 + B(x - 0.5)^3 + C(x - 0.5)^2 + D(x - 0.5)
  2268. * A = 32,
  2269. * B = -6
  2270. * C = -7
  2271. * D = 3
  2272. * y = 32(x - 0.5)^4 - 6(x - 0.5)^3 - 7(x - 0.5)^2 + 3(x - 0.5)
  2273. *
  2274. * New approach:
  2275. * y = ((x - bias)*2)^8
  2276. */
  2277. gdouble
  2278. rspamd_normalize_probability (gdouble x, gdouble bias)
  2279. {
  2280. gdouble xx;
  2281. xx = (x - bias) * 2.0;
  2282. return pow (xx, 8);
  2283. }
  2284. /*
  2285. * Calculations from musl libc
  2286. */
  2287. guint64
  2288. rspamd_tm_to_time (const struct tm *tm, glong tz)
  2289. {
  2290. guint64 result;
  2291. gboolean is_leap = FALSE;
  2292. gint leaps, y = tm->tm_year, cycles, rem, centuries;
  2293. glong offset = (tz / 100) * 3600 + (tz % 100) * 60;
  2294. /* How many seconds in each month from the beginning of the year */
  2295. static const gint secs_through_month[] = {
  2296. 0, 31*86400, 59*86400, 90*86400,
  2297. 120*86400, 151*86400, 181*86400, 212*86400,
  2298. 243*86400, 273*86400, 304*86400, 334*86400
  2299. };
  2300. /* Convert year */
  2301. if (tm->tm_year - 2ULL <= 136) {
  2302. leaps = (y - 68) / 4;
  2303. if (!((y - 68) & 3)) {
  2304. leaps--;
  2305. is_leap = 1;
  2306. }
  2307. result = 31536000 * (y - 70) + 86400 * leaps;
  2308. }
  2309. else {
  2310. cycles = (y - 100) / 400;
  2311. rem = (y - 100) % 400;
  2312. if (rem < 0) {
  2313. cycles--;
  2314. rem += 400;
  2315. }
  2316. if (!rem) {
  2317. is_leap = 1;
  2318. centuries = 0;
  2319. leaps = 0;
  2320. }
  2321. else {
  2322. if (rem >= 200) {
  2323. if (rem >= 300) {
  2324. centuries = 3;
  2325. rem -= 300;
  2326. }
  2327. else {
  2328. centuries = 2;
  2329. rem -= 200;
  2330. }
  2331. }
  2332. else {
  2333. if (rem >= 100) {
  2334. centuries = 1;
  2335. rem -= 100;
  2336. }
  2337. else {
  2338. centuries = 0;
  2339. }
  2340. }
  2341. if (!rem) {
  2342. is_leap = 1;
  2343. leaps = 0;
  2344. } else {
  2345. leaps = rem / 4U;
  2346. rem %= 4U;
  2347. is_leap = !rem;
  2348. }
  2349. }
  2350. leaps += 97 * cycles + 24 * centuries - (gint)is_leap;
  2351. result = (y - 100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
  2352. }
  2353. /* Now convert months to seconds */
  2354. result += secs_through_month[tm->tm_mon];
  2355. /* One more day */
  2356. if (is_leap && tm->tm_mon >= 2) {
  2357. result += 86400;
  2358. }
  2359. result += 86400LL * (tm->tm_mday-1);
  2360. result += 3600LL * tm->tm_hour;
  2361. result += 60LL * tm->tm_min;
  2362. result += tm->tm_sec;
  2363. /* Now apply tz offset */
  2364. result -= offset;
  2365. return result;
  2366. }
  2367. void
  2368. rspamd_gmtime (gint64 ts, struct tm *dest)
  2369. {
  2370. guint64 days, secs, years;
  2371. int remdays, remsecs, remyears;
  2372. int leap_400_cycles, leap_100_cycles, leap_4_cycles;
  2373. int months;
  2374. int wday, yday, leap;
  2375. /* From March */
  2376. static const uint8_t days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29};
  2377. static const guint64 leap_epoch = 946684800ULL + 86400 * (31 + 29);
  2378. static const guint64 days_per_400y = 365*400 + 97;
  2379. static const guint64 days_per_100y = 365*100 + 24;
  2380. static const guint64 days_per_4y = 365*4 + 1;
  2381. secs = ts - leap_epoch;
  2382. days = secs / 86400;
  2383. remsecs = secs % 86400;
  2384. if (remsecs < 0) {
  2385. remsecs += 86400;
  2386. days--;
  2387. }
  2388. wday = (3 + days) % 7;
  2389. if (wday < 0) {
  2390. wday += 7;
  2391. }
  2392. /* Deal with gregorian adjustments */
  2393. leap_400_cycles = days / days_per_400y;
  2394. remdays = days % days_per_400y;
  2395. if (remdays < 0) {
  2396. remdays += days_per_400y;
  2397. leap_400_cycles--;
  2398. }
  2399. leap_100_cycles = remdays / days_per_100y;
  2400. if (leap_100_cycles == 4) {
  2401. /* 400 years */
  2402. leap_100_cycles--;
  2403. }
  2404. remdays -= leap_100_cycles * days_per_100y;
  2405. leap_4_cycles = remdays / days_per_4y;
  2406. if (leap_4_cycles == 25) {
  2407. /* 100 years */
  2408. leap_4_cycles--;
  2409. }
  2410. remdays -= leap_4_cycles * days_per_4y;
  2411. remyears = remdays / 365;
  2412. if (remyears == 4) {
  2413. /* Ordinary leap year */
  2414. remyears--;
  2415. }
  2416. remdays -= remyears * 365;
  2417. leap = !remyears && (leap_4_cycles || !leap_100_cycles);
  2418. yday = remdays + 31 + 28 + leap;
  2419. if (yday >= 365 + leap) {
  2420. yday -= 365 + leap;
  2421. }
  2422. years = remyears + 4 * leap_4_cycles + 100 * leap_100_cycles +
  2423. 400ULL * leap_400_cycles;
  2424. for (months=0; days_in_month[months] <= remdays; months++) {
  2425. remdays -= days_in_month[months];
  2426. }
  2427. if (months >= 10) {
  2428. months -= 12;
  2429. years++;
  2430. }
  2431. dest->tm_year = years + 100;
  2432. dest->tm_mon = months + 2;
  2433. dest->tm_mday = remdays + 1;
  2434. dest->tm_wday = wday;
  2435. dest->tm_yday = yday;
  2436. dest->tm_hour = remsecs / 3600;
  2437. dest->tm_min = remsecs / 60 % 60;
  2438. dest->tm_sec = remsecs % 60;
  2439. #if !defined(__sun)
  2440. dest->tm_gmtoff = 0;
  2441. dest->tm_zone = "GMT";
  2442. #endif
  2443. }
  2444. void
  2445. rspamd_localtime (gint64 ts, struct tm *dest)
  2446. {
  2447. time_t t = ts;
  2448. localtime_r (&t, dest);
  2449. }
  2450. gboolean
  2451. rspamd_fstring_gzip (rspamd_fstring_t **in)
  2452. {
  2453. z_stream strm;
  2454. gint rc;
  2455. rspamd_fstring_t *comp, *buf = *in;
  2456. gchar *p;
  2457. gsize remain;
  2458. memset (&strm, 0, sizeof (strm));
  2459. rc = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  2460. MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY);
  2461. if (rc != Z_OK) {
  2462. return FALSE;
  2463. }
  2464. comp = rspamd_fstring_sized_new (deflateBound (&strm, buf->len));
  2465. strm.avail_in = buf->len;
  2466. strm.next_in = (guchar *)buf->str;
  2467. p = comp->str;
  2468. remain = comp->allocated;
  2469. while (strm.avail_in != 0) {
  2470. strm.avail_out = remain;
  2471. strm.next_out = p;
  2472. rc = deflate (&strm, Z_FINISH);
  2473. if (rc != Z_OK && rc != Z_BUF_ERROR) {
  2474. if (rc == Z_STREAM_END) {
  2475. break;
  2476. }
  2477. else {
  2478. rspamd_fstring_free (comp);
  2479. deflateEnd (&strm);
  2480. return FALSE;
  2481. }
  2482. }
  2483. comp->len = strm.total_out;
  2484. if (strm.avail_out == 0 && strm.avail_in != 0) {
  2485. /* Need to allocate more */
  2486. remain = comp->len;
  2487. comp = rspamd_fstring_grow (comp, strm.avail_in);
  2488. p = comp->str + remain;
  2489. remain = comp->allocated - remain;
  2490. }
  2491. }
  2492. deflateEnd (&strm);
  2493. comp->len = strm.total_out;
  2494. rspamd_fstring_free (buf); /* We replace buf with its compressed version */
  2495. *in = comp;
  2496. return TRUE;
  2497. }
  2498. static gboolean
  2499. rspamd_glob_dir (const gchar *full_path, const gchar *pattern,
  2500. gboolean recursive, guint rec_len,
  2501. GPtrArray *res, GError **err)
  2502. {
  2503. glob_t globbuf;
  2504. const gchar *path;
  2505. static gchar pathbuf[PATH_MAX]; /* Static to help recursion */
  2506. guint i;
  2507. gint rc;
  2508. static const guint rec_lim = 16;
  2509. struct stat st;
  2510. if (rec_len > rec_lim) {
  2511. g_set_error (err, g_quark_from_static_string ("glob"), EOVERFLOW,
  2512. "maximum nesting is reached: %d", rec_lim);
  2513. return FALSE;
  2514. }
  2515. memset (&globbuf, 0, sizeof (globbuf));
  2516. if ((rc = glob (full_path, 0, NULL, &globbuf)) != 0) {
  2517. if (rc != GLOB_NOMATCH) {
  2518. g_set_error (err, g_quark_from_static_string ("glob"), errno,
  2519. "glob %s failed: %s", full_path, strerror (errno));
  2520. globfree (&globbuf);
  2521. return FALSE;
  2522. }
  2523. else {
  2524. globfree (&globbuf);
  2525. return TRUE;
  2526. }
  2527. }
  2528. for (i = 0; i < globbuf.gl_pathc; i ++) {
  2529. path = globbuf.gl_pathv[i];
  2530. if (stat (path, &st) == -1) {
  2531. if (errno == EPERM || errno == EACCES || errno == ELOOP) {
  2532. /* Silently ignore */
  2533. continue;
  2534. }
  2535. g_set_error (err, g_quark_from_static_string ("glob"), errno,
  2536. "stat %s failed: %s", path, strerror (errno));
  2537. globfree (&globbuf);
  2538. return FALSE;
  2539. }
  2540. if (S_ISREG (st.st_mode)) {
  2541. g_ptr_array_add (res, g_strdup (path));
  2542. }
  2543. else if (recursive && S_ISDIR (st.st_mode)) {
  2544. rspamd_snprintf (pathbuf, sizeof (pathbuf), "%s%c%s",
  2545. path, G_DIR_SEPARATOR, pattern);
  2546. if (!rspamd_glob_dir (full_path, pattern, recursive, rec_len + 1,
  2547. res, err)) {
  2548. globfree (&globbuf);
  2549. return FALSE;
  2550. }
  2551. }
  2552. }
  2553. globfree (&globbuf);
  2554. return TRUE;
  2555. }
  2556. GPtrArray *
  2557. rspamd_glob_path (const gchar *dir,
  2558. const gchar *pattern,
  2559. gboolean recursive,
  2560. GError **err)
  2561. {
  2562. gchar path[PATH_MAX];
  2563. GPtrArray *res;
  2564. res = g_ptr_array_new_full (32, (GDestroyNotify)g_free);
  2565. rspamd_snprintf (path, sizeof (path), "%s%c%s", dir, G_DIR_SEPARATOR, pattern);
  2566. if (!rspamd_glob_dir (path, pattern, recursive, 0, res, err)) {
  2567. g_ptr_array_free (res, TRUE);
  2568. return NULL;
  2569. }
  2570. return res;
  2571. }
  2572. double
  2573. rspamd_set_counter (struct rspamd_counter_data *cd, gdouble value)
  2574. {
  2575. gdouble cerr;
  2576. /* Cumulative moving average using per-process counter data */
  2577. if (cd->number == 0) {
  2578. cd->mean = 0;
  2579. cd->stddev = 0;
  2580. }
  2581. cd->mean += (value - cd->mean) / (gdouble)(++cd->number);
  2582. cerr = (value - cd->mean) * (value - cd->mean);
  2583. cd->stddev += (cerr - cd->stddev) / (gdouble)(cd->number);
  2584. return cd->mean;
  2585. }
  2586. double
  2587. rspamd_set_counter_ema (struct rspamd_counter_data *cd,
  2588. gdouble value,
  2589. gdouble alpha)
  2590. {
  2591. gdouble diff, incr;
  2592. /* Cumulative moving average using per-process counter data */
  2593. if (cd->number == 0) {
  2594. cd->mean = 0;
  2595. cd->stddev = 0;
  2596. }
  2597. diff = value - cd->mean;
  2598. incr = diff * alpha;
  2599. cd->mean += incr;
  2600. cd->stddev = (1 - alpha) * (cd->stddev + diff * incr);
  2601. cd->number ++;
  2602. return cd->mean;
  2603. }