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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459
  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 "unix-std.h"
  19. #include "xxhash.h"
  20. #include "ottery.h"
  21. #include "cryptobox.h"
  22. #ifdef HAVE_TERMIOS_H
  23. #include <termios.h>
  24. #endif
  25. #ifdef HAVE_READPASSPHRASE_H
  26. #include <readpassphrase.h>
  27. #endif
  28. /* libutil */
  29. #ifdef HAVE_LIBUTIL_H
  30. #include <libutil.h>
  31. #endif
  32. #ifdef __APPLE__
  33. #include <mach/mach_time.h>
  34. #include <mach/mach_init.h>
  35. #include <mach/thread_act.h>
  36. #include <mach/mach_port.h>
  37. #endif
  38. /* poll */
  39. #ifdef HAVE_POLL_H
  40. #include <poll.h>
  41. #endif
  42. #ifdef HAVE_SIGINFO_H
  43. #include <siginfo.h>
  44. #endif
  45. /* sys/wait */
  46. #ifdef HAVE_SYS_WAIT_H
  47. #include <sys/wait.h>
  48. #endif
  49. /* sys/resource.h */
  50. #ifdef HAVE_SYS_RESOURCE_H
  51. #include <sys/resource.h>
  52. #endif
  53. #ifdef HAVE_RDTSC
  54. #ifdef __x86_64__
  55. #include <x86intrin.h>
  56. #endif
  57. #endif
  58. #include <math.h> /* for pow */
  59. #include <glob.h> /* in fact, we require this file ultimately */
  60. #include "zlib.h"
  61. #include "contrib/uthash/utlist.h"
  62. #include "blas-config.h"
  63. /* Check log messages intensity once per minute */
  64. #define CHECK_TIME 60
  65. /* More than 2 log messages per second */
  66. #define BUF_INTENSITY 2
  67. /* Default connect timeout for sync sockets */
  68. #define CONNECT_TIMEOUT 3
  69. /*
  70. * Should be defined in a single point
  71. */
  72. const struct rspamd_controller_pbkdf pbkdf_list[] = {
  73. {
  74. .name = "PBKDF2-blake2b",
  75. .alias = "pbkdf2",
  76. .description = "standard CPU intensive \"slow\" KDF using blake2b hash function",
  77. .type = RSPAMD_CRYPTOBOX_PBKDF2,
  78. .id = RSPAMD_PBKDF_ID_V1,
  79. .complexity = 16000,
  80. .salt_len = 20,
  81. .key_len = rspamd_cryptobox_HASHBYTES / 2
  82. },
  83. {
  84. .name = "Catena-Butterfly",
  85. .alias = "catena",
  86. .description = "modern CPU and memory intensive KDF",
  87. .type = RSPAMD_CRYPTOBOX_CATENA,
  88. .id = RSPAMD_PBKDF_ID_V2,
  89. .complexity = 10,
  90. .salt_len = 20,
  91. .key_len = rspamd_cryptobox_HASHBYTES / 2
  92. }
  93. };
  94. gint
  95. rspamd_socket_nonblocking (gint fd)
  96. {
  97. gint ofl;
  98. ofl = fcntl (fd, F_GETFL, 0);
  99. if (fcntl (fd, F_SETFL, ofl | O_NONBLOCK) == -1) {
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. gint
  105. rspamd_socket_blocking (gint fd)
  106. {
  107. gint ofl;
  108. ofl = fcntl (fd, F_GETFL, 0);
  109. if (fcntl (fd, F_SETFL, ofl & (~O_NONBLOCK)) == -1) {
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. gint
  115. rspamd_socket_poll (gint fd, gint timeout, short events)
  116. {
  117. gint r;
  118. struct pollfd fds[1];
  119. fds->fd = fd;
  120. fds->events = events;
  121. fds->revents = 0;
  122. while ((r = poll (fds, 1, timeout)) < 0) {
  123. if (errno != EINTR) {
  124. break;
  125. }
  126. }
  127. return r;
  128. }
  129. gint
  130. rspamd_socket_create (gint af, gint type, gint protocol, gboolean async)
  131. {
  132. gint fd;
  133. fd = socket (af, type, protocol);
  134. if (fd == -1) {
  135. return -1;
  136. }
  137. /* Set close on exec */
  138. if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
  139. close (fd);
  140. return -1;
  141. }
  142. if (async) {
  143. if (rspamd_socket_nonblocking (fd) == -1) {
  144. close (fd);
  145. return -1;
  146. }
  147. }
  148. return fd;
  149. }
  150. static gint
  151. rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server,
  152. gboolean async, GList **list)
  153. {
  154. gint fd = -1, r, on = 1, s_error;
  155. struct addrinfo *cur;
  156. gpointer ptr;
  157. socklen_t optlen;
  158. cur = addr;
  159. while (cur) {
  160. /* Create socket */
  161. fd = rspamd_socket_create (cur->ai_family, type, cur->ai_protocol, TRUE);
  162. if (fd == -1) {
  163. goto out;
  164. }
  165. if (is_server) {
  166. (void)setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on,
  167. sizeof (gint));
  168. #ifdef HAVE_IPV6_V6ONLY
  169. if (cur->ai_family == AF_INET6) {
  170. setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void *)&on,
  171. sizeof (gint));
  172. }
  173. #endif
  174. r = bind (fd, cur->ai_addr, cur->ai_addrlen);
  175. }
  176. else {
  177. r = connect (fd, cur->ai_addr, cur->ai_addrlen);
  178. }
  179. if (r == -1) {
  180. if (errno != EINPROGRESS) {
  181. goto out;
  182. }
  183. if (!async) {
  184. /* Try to poll */
  185. if (rspamd_socket_poll (fd, CONNECT_TIMEOUT * 1000,
  186. POLLOUT) <= 0) {
  187. errno = ETIMEDOUT;
  188. goto out;
  189. }
  190. else {
  191. /* Make synced again */
  192. if (rspamd_socket_blocking (fd) < 0) {
  193. goto out;
  194. }
  195. }
  196. }
  197. }
  198. else {
  199. /* Still need to check SO_ERROR on socket */
  200. optlen = sizeof (s_error);
  201. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, (void *)&s_error, &optlen) != -1) {
  202. if (s_error) {
  203. errno = s_error;
  204. goto out;
  205. }
  206. }
  207. }
  208. if (list == NULL) {
  209. /* Go out immediately */
  210. break;
  211. }
  212. else if (fd != -1) {
  213. ptr = GINT_TO_POINTER (fd);
  214. *list = g_list_prepend (*list, ptr);
  215. cur = cur->ai_next;
  216. continue;
  217. }
  218. out:
  219. if (fd != -1) {
  220. close (fd);
  221. }
  222. fd = -1;
  223. cur = cur->ai_next;
  224. }
  225. return (fd);
  226. }
  227. gint
  228. rspamd_socket_tcp (struct addrinfo *addr, gboolean is_server, gboolean async)
  229. {
  230. return rspamd_inet_socket_create (SOCK_STREAM, addr, is_server, async, NULL);
  231. }
  232. gint
  233. rspamd_socket_udp (struct addrinfo *addr, gboolean is_server, gboolean async)
  234. {
  235. return rspamd_inet_socket_create (SOCK_DGRAM, addr, is_server, async, NULL);
  236. }
  237. gint
  238. rspamd_socket_unix (const gchar *path,
  239. struct sockaddr_un *addr,
  240. gint type,
  241. gboolean is_server,
  242. gboolean async)
  243. {
  244. socklen_t optlen;
  245. gint fd = -1, s_error, r, serrno, on = 1;
  246. struct stat st;
  247. if (path == NULL)
  248. return -1;
  249. addr->sun_family = AF_UNIX;
  250. rspamd_strlcpy (addr->sun_path, path, sizeof (addr->sun_path));
  251. #ifdef FREEBSD
  252. addr->sun_len = SUN_LEN (addr);
  253. #endif
  254. if (is_server) {
  255. /* Unlink socket if it exists already */
  256. if (lstat (addr->sun_path, &st) != -1) {
  257. if (S_ISSOCK (st.st_mode)) {
  258. if (unlink (addr->sun_path) == -1) {
  259. goto out;
  260. }
  261. }
  262. else {
  263. goto out;
  264. }
  265. }
  266. }
  267. fd = socket (PF_LOCAL, type, 0);
  268. if (fd == -1) {
  269. return -1;
  270. }
  271. if (rspamd_socket_nonblocking (fd) < 0) {
  272. goto out;
  273. }
  274. /* Set close on exec */
  275. if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
  276. goto out;
  277. }
  278. if (is_server) {
  279. (void)setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on,
  280. sizeof (gint));
  281. r = bind (fd, (struct sockaddr *)addr, SUN_LEN (addr));
  282. }
  283. else {
  284. r = connect (fd, (struct sockaddr *)addr, SUN_LEN (addr));
  285. }
  286. if (r == -1) {
  287. if (errno != EINPROGRESS) {
  288. goto out;
  289. }
  290. if (!async) {
  291. /* Try to poll */
  292. if (rspamd_socket_poll (fd, CONNECT_TIMEOUT * 1000, POLLOUT) <= 0) {
  293. errno = ETIMEDOUT;
  294. goto out;
  295. }
  296. else {
  297. /* Make synced again */
  298. if (rspamd_socket_blocking (fd) < 0) {
  299. goto out;
  300. }
  301. }
  302. }
  303. }
  304. else {
  305. /* Still need to check SO_ERROR on socket */
  306. optlen = sizeof (s_error);
  307. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, (void *)&s_error, &optlen) != -1) {
  308. if (s_error) {
  309. errno = s_error;
  310. goto out;
  311. }
  312. }
  313. }
  314. return (fd);
  315. out:
  316. serrno = errno;
  317. if (fd != -1) {
  318. close (fd);
  319. }
  320. errno = serrno;
  321. return (-1);
  322. }
  323. static int
  324. rspamd_prefer_v4_hack (const struct addrinfo *a1, const struct addrinfo *a2)
  325. {
  326. return a1->ai_addr->sa_family - a2->ai_addr->sa_family;
  327. }
  328. /**
  329. * Make a universal socket
  330. * @param credits host, ip or path to unix socket
  331. * @param port port (used for network sockets)
  332. * @param async make this socket asynced
  333. * @param is_server make this socket as server socket
  334. * @param try_resolve try name resolution for a socket (BLOCKING)
  335. */
  336. gint
  337. rspamd_socket (const gchar *credits, guint16 port,
  338. gint type, gboolean async, gboolean is_server, gboolean try_resolve)
  339. {
  340. struct sockaddr_un un;
  341. struct stat st;
  342. struct addrinfo hints, *res;
  343. gint r;
  344. gchar portbuf[8];
  345. if (*credits == '/') {
  346. if (is_server) {
  347. return rspamd_socket_unix (credits, &un, type, is_server, async);
  348. }
  349. else {
  350. r = stat (credits, &st);
  351. if (r == -1) {
  352. /* Unix socket doesn't exists it must be created first */
  353. errno = ENOENT;
  354. return -1;
  355. }
  356. else {
  357. if ((st.st_mode & S_IFSOCK) == 0) {
  358. /* Path is not valid socket */
  359. errno = EINVAL;
  360. return -1;
  361. }
  362. else {
  363. return rspamd_socket_unix (credits,
  364. &un,
  365. type,
  366. is_server,
  367. async);
  368. }
  369. }
  370. }
  371. }
  372. else {
  373. /* TCP related part */
  374. memset (&hints, 0, sizeof (hints));
  375. hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  376. hints.ai_socktype = type; /* Type of the socket */
  377. hints.ai_flags = is_server ? AI_PASSIVE : 0;
  378. hints.ai_protocol = 0; /* Any protocol */
  379. hints.ai_canonname = NULL;
  380. hints.ai_addr = NULL;
  381. hints.ai_next = NULL;
  382. if (!try_resolve) {
  383. hints.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;
  384. }
  385. rspamd_snprintf (portbuf, sizeof (portbuf), "%d", (int)port);
  386. if ((r = getaddrinfo (credits, portbuf, &hints, &res)) == 0) {
  387. LL_SORT2 (res, rspamd_prefer_v4_hack, ai_next);
  388. r = rspamd_inet_socket_create (type, res, is_server, async, NULL);
  389. freeaddrinfo (res);
  390. return r;
  391. }
  392. else {
  393. return -1;
  394. }
  395. }
  396. }
  397. gboolean
  398. rspamd_socketpair (gint pair[2], gint af)
  399. {
  400. gint r = -1, serrno;
  401. #ifdef HAVE_SOCK_SEQPACKET
  402. if (af == SOCK_SEQPACKET) {
  403. r = socketpair (AF_LOCAL, SOCK_SEQPACKET, 0, pair);
  404. if (r == -1) {
  405. r = socketpair (AF_LOCAL, SOCK_DGRAM, 0, pair);
  406. }
  407. }
  408. #endif
  409. if (r == -1) {
  410. r = socketpair (AF_LOCAL, af, 0, pair);
  411. }
  412. if (r == -1) {
  413. return -1;
  414. }
  415. /* Set close on exec */
  416. if (fcntl (pair[0], F_SETFD, FD_CLOEXEC) == -1) {
  417. goto out;
  418. }
  419. if (fcntl (pair[1], F_SETFD, FD_CLOEXEC) == -1) {
  420. goto out;
  421. }
  422. return TRUE;
  423. out:
  424. serrno = errno;
  425. close (pair[0]);
  426. close (pair[1]);
  427. errno = serrno;
  428. return FALSE;
  429. }
  430. #ifdef HAVE_SA_SIGINFO
  431. void
  432. rspamd_signals_init (struct sigaction *signals, void (*sig_handler)(gint,
  433. siginfo_t *,
  434. void *))
  435. #else
  436. void
  437. rspamd_signals_init (struct sigaction *signals, void (*sig_handler)(gint))
  438. #endif
  439. {
  440. struct sigaction sigpipe_act;
  441. /* Setting up signal handlers */
  442. /* SIGUSR1 - reopen config file */
  443. /* SIGUSR2 - worker is ready for accept */
  444. sigemptyset (&signals->sa_mask);
  445. sigaddset (&signals->sa_mask, SIGTERM);
  446. sigaddset (&signals->sa_mask, SIGINT);
  447. sigaddset (&signals->sa_mask, SIGHUP);
  448. sigaddset (&signals->sa_mask, SIGCHLD);
  449. sigaddset (&signals->sa_mask, SIGUSR1);
  450. sigaddset (&signals->sa_mask, SIGUSR2);
  451. sigaddset (&signals->sa_mask, SIGALRM);
  452. #ifdef SIGPOLL
  453. sigaddset (&signals->sa_mask, SIGPOLL);
  454. #endif
  455. #ifdef SIGIO
  456. sigaddset (&signals->sa_mask, SIGIO);
  457. #endif
  458. #ifdef HAVE_SA_SIGINFO
  459. signals->sa_flags = SA_SIGINFO;
  460. signals->sa_handler = NULL;
  461. signals->sa_sigaction = sig_handler;
  462. #else
  463. signals->sa_handler = sig_handler;
  464. signals->sa_flags = 0;
  465. #endif
  466. sigaction (SIGTERM, signals, NULL);
  467. sigaction (SIGINT, signals, NULL);
  468. sigaction (SIGHUP, signals, NULL);
  469. sigaction (SIGCHLD, signals, NULL);
  470. sigaction (SIGUSR1, signals, NULL);
  471. sigaction (SIGUSR2, signals, NULL);
  472. sigaction (SIGALRM, signals, NULL);
  473. #ifdef SIGPOLL
  474. sigaction (SIGPOLL, signals, NULL);
  475. #endif
  476. #ifdef SIGIO
  477. sigaction (SIGIO, signals, NULL);
  478. #endif
  479. /* Ignore SIGPIPE as we handle write errors manually */
  480. sigemptyset (&sigpipe_act.sa_mask);
  481. sigaddset (&sigpipe_act.sa_mask, SIGPIPE);
  482. sigpipe_act.sa_handler = SIG_IGN;
  483. sigpipe_act.sa_flags = 0;
  484. sigaction (SIGPIPE, &sigpipe_act, NULL);
  485. }
  486. #ifndef HAVE_SETPROCTITLE
  487. #ifdef LINUX
  488. static gchar *title_buffer = NULL;
  489. static size_t title_buffer_size = 0;
  490. static gchar *title_progname, *title_progname_full;
  491. #endif
  492. #ifdef LINUX
  493. static void
  494. rspamd_title_dtor (gpointer d)
  495. {
  496. gchar **env = (gchar **)d;
  497. guint i;
  498. for (i = 0; env[i] != NULL; i++) {
  499. g_free (env[i]);
  500. }
  501. g_free (env);
  502. }
  503. #endif
  504. gint
  505. init_title (rspamd_mempool_t *pool,
  506. gint argc, gchar *argv[], gchar *envp[])
  507. {
  508. #ifdef LINUX
  509. gchar *begin_of_buffer = 0, *end_of_buffer = 0;
  510. gint i;
  511. for (i = 0; i < argc; ++i) {
  512. if (!begin_of_buffer) {
  513. begin_of_buffer = argv[i];
  514. }
  515. if (!end_of_buffer || end_of_buffer + 1 == argv[i]) {
  516. end_of_buffer = argv[i] + strlen (argv[i]);
  517. }
  518. }
  519. for (i = 0; envp[i]; ++i) {
  520. if (!begin_of_buffer) {
  521. begin_of_buffer = envp[i];
  522. }
  523. if (!end_of_buffer || end_of_buffer + 1 == envp[i]) {
  524. end_of_buffer = envp[i] + strlen (envp[i]);
  525. }
  526. }
  527. if (!end_of_buffer) {
  528. return 0;
  529. }
  530. gchar **new_environ = g_malloc ((i + 1) * sizeof (envp[0]));
  531. for (i = 0; envp[i]; ++i) {
  532. new_environ[i] = g_strdup (envp[i]);
  533. }
  534. new_environ[i] = NULL;
  535. if (program_invocation_name) {
  536. title_progname_full = g_strdup (program_invocation_name);
  537. gchar *p = strrchr (title_progname_full, '/');
  538. if (p) {
  539. title_progname = p + 1;
  540. }
  541. else {
  542. title_progname = title_progname_full;
  543. }
  544. program_invocation_name = title_progname_full;
  545. program_invocation_short_name = title_progname;
  546. }
  547. environ = new_environ;
  548. title_buffer = begin_of_buffer;
  549. title_buffer_size = end_of_buffer - begin_of_buffer;
  550. rspamd_mempool_add_destructor (pool,
  551. rspamd_title_dtor, new_environ);
  552. #endif
  553. return 0;
  554. }
  555. gint
  556. setproctitle (const gchar *fmt, ...)
  557. {
  558. #if defined(LINUX)
  559. if (!title_buffer || !title_buffer_size) {
  560. errno = ENOMEM;
  561. return -1;
  562. }
  563. memset (title_buffer, '\0', title_buffer_size);
  564. ssize_t written;
  565. if (fmt) {
  566. va_list ap;
  567. written = rspamd_snprintf (title_buffer,
  568. title_buffer_size,
  569. "%s: ",
  570. title_progname);
  571. if (written < 0 || (size_t) written >= title_buffer_size)
  572. return -1;
  573. va_start (ap, fmt);
  574. rspamd_vsnprintf (title_buffer + written,
  575. title_buffer_size - written,
  576. fmt,
  577. ap);
  578. va_end (ap);
  579. }
  580. else {
  581. written = rspamd_snprintf (title_buffer,
  582. title_buffer_size,
  583. "%s",
  584. title_progname);
  585. if (written < 0 || (size_t) written >= title_buffer_size)
  586. return -1;
  587. }
  588. written = strlen (title_buffer);
  589. memset (title_buffer + written, '\0', title_buffer_size - written);
  590. #elif defined(__APPLE__)
  591. /* OSX is broken, ignore this brain damaged system */
  592. #else
  593. /* Last resort (usually broken, but eh...) */
  594. GString *dest;
  595. va_list ap;
  596. dest = g_string_new ("");
  597. va_start (ap, fmt);
  598. rspamd_vprintf_gstring (dest, fmt, ap);
  599. va_end (ap);
  600. g_set_prgname (dest->str);
  601. g_string_free (dest, TRUE);
  602. #endif
  603. return 0;
  604. }
  605. #endif
  606. #ifndef HAVE_PIDFILE
  607. static gint _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit);
  608. static gint
  609. rspamd_pidfile_verify (rspamd_pidfh_t *pfh)
  610. {
  611. struct stat sb;
  612. if (pfh == NULL || pfh->pf_fd == -1)
  613. return (-1);
  614. /*
  615. * Check remembered descriptor.
  616. */
  617. if (fstat (pfh->pf_fd, &sb) == -1)
  618. return (errno);
  619. if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
  620. return -1;
  621. return 0;
  622. }
  623. static gint
  624. rspamd_pidfile_read (const gchar *path, pid_t * pidptr)
  625. {
  626. gchar buf[16], *endptr;
  627. gint error, fd, i;
  628. fd = open (path, O_RDONLY);
  629. if (fd == -1)
  630. return (errno);
  631. i = read (fd, buf, sizeof (buf) - 1);
  632. error = errno; /* Remember errno in case close() wants to change it. */
  633. close (fd);
  634. if (i == -1)
  635. return error;
  636. else if (i == 0)
  637. return EAGAIN;
  638. buf[i] = '\0';
  639. *pidptr = strtol (buf, &endptr, 10);
  640. if (endptr != &buf[i])
  641. return EINVAL;
  642. return 0;
  643. }
  644. rspamd_pidfh_t *
  645. rspamd_pidfile_open (const gchar *path, mode_t mode, pid_t * pidptr)
  646. {
  647. rspamd_pidfh_t *pfh;
  648. struct stat sb;
  649. gint error, fd, len, count;
  650. struct timespec rqtp;
  651. pfh = g_malloc (sizeof (*pfh));
  652. if (pfh == NULL)
  653. return NULL;
  654. if (path == NULL)
  655. len = snprintf (pfh->pf_path,
  656. sizeof (pfh->pf_path),
  657. "/var/run/%s.pid",
  658. g_get_prgname ());
  659. else
  660. len = snprintf (pfh->pf_path, sizeof (pfh->pf_path), "%s", path);
  661. if (len >= (gint)sizeof (pfh->pf_path)) {
  662. g_free (pfh);
  663. errno = ENAMETOOLONG;
  664. return NULL;
  665. }
  666. /*
  667. * Open the PID file and obtain exclusive lock.
  668. * We truncate PID file here only to remove old PID immediatelly,
  669. * PID file will be truncated again in pidfile_write(), so
  670. * pidfile_write() can be called multiple times.
  671. */
  672. fd = open (pfh->pf_path, O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
  673. rspamd_file_lock (fd, TRUE);
  674. if (fd == -1) {
  675. count = 0;
  676. rqtp.tv_sec = 0;
  677. rqtp.tv_nsec = 5000000;
  678. if (errno == EWOULDBLOCK && pidptr != NULL) {
  679. again:
  680. errno = rspamd_pidfile_read (pfh->pf_path, pidptr);
  681. if (errno == 0)
  682. errno = EEXIST;
  683. else if (errno == EAGAIN) {
  684. if (++count <= 3) {
  685. nanosleep (&rqtp, 0);
  686. goto again;
  687. }
  688. }
  689. }
  690. g_free (pfh);
  691. return NULL;
  692. }
  693. /*
  694. * Remember file information, so in pidfile_write() we are sure we write
  695. * to the proper descriptor.
  696. */
  697. if (fstat (fd, &sb) == -1) {
  698. error = errno;
  699. unlink (pfh->pf_path);
  700. close (fd);
  701. g_free (pfh);
  702. errno = error;
  703. return NULL;
  704. }
  705. pfh->pf_fd = fd;
  706. pfh->pf_dev = sb.st_dev;
  707. pfh->pf_ino = sb.st_ino;
  708. return pfh;
  709. }
  710. gint
  711. rspamd_pidfile_write (rspamd_pidfh_t *pfh)
  712. {
  713. gchar pidstr[16];
  714. gint error, fd;
  715. /*
  716. * Check remembered descriptor, so we don't overwrite some other
  717. * file if pidfile was closed and descriptor reused.
  718. */
  719. errno = rspamd_pidfile_verify (pfh);
  720. if (errno != 0) {
  721. /*
  722. * Don't close descriptor, because we are not sure if it's ours.
  723. */
  724. return -1;
  725. }
  726. fd = pfh->pf_fd;
  727. /*
  728. * Truncate PID file, so multiple calls of pidfile_write() are allowed.
  729. */
  730. if (ftruncate (fd, 0) == -1) {
  731. error = errno;
  732. _rspamd_pidfile_remove (pfh, 0);
  733. errno = error;
  734. return -1;
  735. }
  736. rspamd_snprintf (pidstr, sizeof (pidstr), "%P", getpid ());
  737. if (pwrite (fd, pidstr, strlen (pidstr), 0) != (ssize_t) strlen (pidstr)) {
  738. error = errno;
  739. _rspamd_pidfile_remove (pfh, 0);
  740. errno = error;
  741. return -1;
  742. }
  743. return 0;
  744. }
  745. gint
  746. rspamd_pidfile_close (rspamd_pidfh_t *pfh)
  747. {
  748. gint error;
  749. error = rspamd_pidfile_verify (pfh);
  750. if (error != 0) {
  751. errno = error;
  752. return -1;
  753. }
  754. if (close (pfh->pf_fd) == -1)
  755. error = errno;
  756. g_free (pfh);
  757. if (error != 0) {
  758. errno = error;
  759. return -1;
  760. }
  761. return 0;
  762. }
  763. static gint
  764. _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit)
  765. {
  766. gint error;
  767. error = rspamd_pidfile_verify (pfh);
  768. if (error != 0) {
  769. errno = error;
  770. return -1;
  771. }
  772. if (unlink (pfh->pf_path) == -1)
  773. error = errno;
  774. if (!rspamd_file_unlock (pfh->pf_fd, FALSE)) {
  775. if (error == 0)
  776. error = errno;
  777. }
  778. if (close (pfh->pf_fd) == -1) {
  779. if (error == 0)
  780. error = errno;
  781. }
  782. if (freeit)
  783. g_free (pfh);
  784. else
  785. pfh->pf_fd = -1;
  786. if (error != 0) {
  787. errno = error;
  788. return -1;
  789. }
  790. return 0;
  791. }
  792. gint
  793. rspamd_pidfile_remove (rspamd_pidfh_t *pfh)
  794. {
  795. return (_rspamd_pidfile_remove (pfh, 1));
  796. }
  797. #endif
  798. /* Replace %r with rcpt value and %f with from value, new string is allocated in pool */
  799. gchar *
  800. resolve_stat_filename (rspamd_mempool_t * pool,
  801. gchar *pattern,
  802. gchar *rcpt,
  803. gchar *from)
  804. {
  805. gint need_to_format = 0, len = 0;
  806. gint rcptlen, fromlen;
  807. gchar *c = pattern, *new, *s;
  808. if (rcpt) {
  809. rcptlen = strlen (rcpt);
  810. }
  811. else {
  812. rcptlen = 0;
  813. }
  814. if (from) {
  815. fromlen = strlen (from);
  816. }
  817. else {
  818. fromlen = 0;
  819. }
  820. /* Calculate length */
  821. while (*c++) {
  822. if (*c == '%' && *(c + 1) == 'r') {
  823. len += rcptlen;
  824. c += 2;
  825. need_to_format = 1;
  826. continue;
  827. }
  828. else if (*c == '%' && *(c + 1) == 'f') {
  829. len += fromlen;
  830. c += 2;
  831. need_to_format = 1;
  832. continue;
  833. }
  834. len++;
  835. }
  836. /* Do not allocate extra memory if we do not need to format string */
  837. if (!need_to_format) {
  838. return pattern;
  839. }
  840. /* Allocate new string */
  841. new = rspamd_mempool_alloc (pool, len);
  842. c = pattern;
  843. s = new;
  844. /* Format string */
  845. while (*c++) {
  846. if (*c == '%' && *(c + 1) == 'r') {
  847. c += 2;
  848. memcpy (s, rcpt, rcptlen);
  849. s += rcptlen;
  850. continue;
  851. }
  852. *s++ = *c;
  853. }
  854. *s = '\0';
  855. return new;
  856. }
  857. const gchar *
  858. rspamd_log_check_time (gdouble start, gdouble end, gint resolution)
  859. {
  860. gdouble diff;
  861. static gchar res[64];
  862. gchar fmt[32];
  863. diff = (end - start) * 1000.0;
  864. rspamd_snprintf (fmt, sizeof (fmt), "%%.%dfms", resolution);
  865. rspamd_snprintf (res, sizeof (res), fmt, diff);
  866. return (const gchar *)res;
  867. }
  868. #ifdef HAVE_FLOCK
  869. /* Flock version */
  870. gboolean
  871. rspamd_file_lock (gint fd, gboolean async)
  872. {
  873. gint flags;
  874. if (async) {
  875. flags = LOCK_EX | LOCK_NB;
  876. }
  877. else {
  878. flags = LOCK_EX;
  879. }
  880. if (flock (fd, flags) == -1) {
  881. if (async && errno == EAGAIN) {
  882. return FALSE;
  883. }
  884. return FALSE;
  885. }
  886. return TRUE;
  887. }
  888. gboolean
  889. rspamd_file_unlock (gint fd, gboolean async)
  890. {
  891. gint flags;
  892. if (async) {
  893. flags = LOCK_UN | LOCK_NB;
  894. }
  895. else {
  896. flags = LOCK_UN;
  897. }
  898. if (flock (fd, flags) == -1) {
  899. if (async && errno == EAGAIN) {
  900. return FALSE;
  901. }
  902. return FALSE;
  903. }
  904. return TRUE;
  905. }
  906. #else /* HAVE_FLOCK */
  907. /* Fctnl version */
  908. gboolean
  909. rspamd_file_lock (gint fd, gboolean async)
  910. {
  911. struct flock fl = {
  912. .l_type = F_WRLCK,
  913. .l_whence = SEEK_SET,
  914. .l_start = 0,
  915. .l_len = 0
  916. };
  917. if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) {
  918. if (async && (errno == EAGAIN || errno == EACCES)) {
  919. return FALSE;
  920. }
  921. return FALSE;
  922. }
  923. return TRUE;
  924. }
  925. gboolean
  926. rspamd_file_unlock (gint fd, gboolean async)
  927. {
  928. struct flock fl = {
  929. .l_type = F_UNLCK,
  930. .l_whence = SEEK_SET,
  931. .l_start = 0,
  932. .l_len = 0
  933. };
  934. if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) {
  935. if (async && (errno == EAGAIN || errno == EACCES)) {
  936. return FALSE;
  937. }
  938. return FALSE;
  939. }
  940. return TRUE;
  941. }
  942. #endif /* HAVE_FLOCK */
  943. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 22))
  944. void
  945. g_ptr_array_unref (GPtrArray *array)
  946. {
  947. g_ptr_array_free (array, TRUE);
  948. }
  949. gboolean
  950. g_int64_equal (gconstpointer v1, gconstpointer v2)
  951. {
  952. return *((const gint64*) v1) == *((const gint64*) v2);
  953. }
  954. guint
  955. g_int64_hash (gconstpointer v)
  956. {
  957. guint64 v64 = *(guint64 *)v;
  958. return (guint) (v ^ (v >> 32));
  959. }
  960. #endif
  961. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 14))
  962. void
  963. g_queue_clear (GQueue *queue)
  964. {
  965. g_return_if_fail (queue != NULL);
  966. g_list_free (queue->head);
  967. queue->head = queue->tail = NULL;
  968. queue->length = 0;
  969. }
  970. #endif
  971. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 30))
  972. GPtrArray*
  973. g_ptr_array_new_full (guint reserved_size,
  974. GDestroyNotify element_free_func)
  975. {
  976. GPtrArray *array;
  977. array = g_ptr_array_sized_new (reserved_size);
  978. g_ptr_array_set_free_func (array, element_free_func);
  979. return array;
  980. }
  981. #endif
  982. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32))
  983. void
  984. g_queue_free_full (GQueue *queue, GDestroyNotify free_func)
  985. {
  986. GList *cur;
  987. cur = queue->head;
  988. while (cur) {
  989. free_func (cur->data);
  990. cur = g_list_next (cur);
  991. }
  992. g_queue_free (queue);
  993. }
  994. #endif
  995. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 40))
  996. void
  997. g_ptr_array_insert (GPtrArray *array, gint index_, gpointer data)
  998. {
  999. g_return_if_fail (array);
  1000. g_return_if_fail (index_ >= -1);
  1001. g_return_if_fail (index_ <= (gint )array->len);
  1002. g_ptr_array_set_size (array, array->len + 1);
  1003. if (index_ < 0) {
  1004. index_ = array->len;
  1005. }
  1006. if (index_ < array->len) {
  1007. memmove (&(array->pdata[index_ + 1]), &(array->pdata[index_]),
  1008. (array->len - index_) * sizeof(gpointer));
  1009. }
  1010. array->pdata[index_] = data;
  1011. }
  1012. #endif
  1013. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32))
  1014. const gchar *
  1015. g_environ_getenv (gchar **envp, const gchar *variable)
  1016. {
  1017. gsize len;
  1018. gint i;
  1019. if (envp == NULL) {
  1020. return NULL;
  1021. }
  1022. len = strlen (variable);
  1023. for (i = 0; envp[i]; i++) {
  1024. if (strncmp (envp[i], variable, len) == 0 && envp[i][len] == '=') {
  1025. return envp[i] + len + 1;
  1026. }
  1027. }
  1028. return NULL;
  1029. }
  1030. #endif
  1031. gint
  1032. rspamd_fallocate (gint fd, off_t offset, off_t len)
  1033. {
  1034. #if defined(HAVE_FALLOCATE)
  1035. return fallocate (fd, 0, offset, len);
  1036. #elif defined(HAVE_POSIX_FALLOCATE)
  1037. return posix_fallocate (fd, offset, len);
  1038. #else
  1039. /* Return 0 as nothing can be done on this system */
  1040. return 0;
  1041. #endif
  1042. }
  1043. /**
  1044. * Create new mutex
  1045. * @return mutex or NULL
  1046. */
  1047. inline rspamd_mutex_t *
  1048. rspamd_mutex_new (void)
  1049. {
  1050. rspamd_mutex_t *new;
  1051. new = g_malloc0 (sizeof (rspamd_mutex_t));
  1052. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1053. g_mutex_init (&new->mtx);
  1054. #else
  1055. g_static_mutex_init (&new->mtx);
  1056. #endif
  1057. return new;
  1058. }
  1059. /**
  1060. * Lock mutex
  1061. * @param mtx
  1062. */
  1063. inline void
  1064. rspamd_mutex_lock (rspamd_mutex_t *mtx)
  1065. {
  1066. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1067. g_mutex_lock (&mtx->mtx);
  1068. #else
  1069. g_static_mutex_lock (&mtx->mtx);
  1070. #endif
  1071. }
  1072. /**
  1073. * Unlock mutex
  1074. * @param mtx
  1075. */
  1076. inline void
  1077. rspamd_mutex_unlock (rspamd_mutex_t *mtx)
  1078. {
  1079. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1080. g_mutex_unlock (&mtx->mtx);
  1081. #else
  1082. g_static_mutex_unlock (&mtx->mtx);
  1083. #endif
  1084. }
  1085. void
  1086. rspamd_mutex_free (rspamd_mutex_t *mtx)
  1087. {
  1088. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
  1089. g_mutex_clear (&mtx->mtx);
  1090. #endif
  1091. g_free (mtx);
  1092. }
  1093. struct rspamd_thread_data {
  1094. gchar *name;
  1095. gint id;
  1096. GThreadFunc func;
  1097. gpointer data;
  1098. };
  1099. static gpointer
  1100. rspamd_thread_func (gpointer ud)
  1101. {
  1102. struct rspamd_thread_data *td = ud;
  1103. sigset_t s_mask;
  1104. /* Ignore signals in thread */
  1105. sigemptyset (&s_mask);
  1106. sigaddset (&s_mask, SIGINT);
  1107. sigaddset (&s_mask, SIGHUP);
  1108. sigaddset (&s_mask, SIGCHLD);
  1109. sigaddset (&s_mask, SIGUSR1);
  1110. sigaddset (&s_mask, SIGUSR2);
  1111. sigaddset (&s_mask, SIGALRM);
  1112. sigaddset (&s_mask, SIGPIPE);
  1113. pthread_sigmask (SIG_BLOCK, &s_mask, NULL);
  1114. ud = td->func (td->data);
  1115. g_free (td->name);
  1116. g_free (td);
  1117. return ud;
  1118. }
  1119. struct hash_copy_callback_data {
  1120. gpointer (*key_copy_func)(gconstpointer data, gpointer ud);
  1121. gpointer (*value_copy_func)(gconstpointer data, gpointer ud);
  1122. gpointer ud;
  1123. GHashTable *dst;
  1124. };
  1125. static void
  1126. copy_foreach_callback (gpointer key, gpointer value, gpointer ud)
  1127. {
  1128. struct hash_copy_callback_data *cb = ud;
  1129. gpointer nkey, nvalue;
  1130. nkey = cb->key_copy_func ? cb->key_copy_func (key, cb->ud) : (gpointer)key;
  1131. nvalue =
  1132. cb->value_copy_func ? cb->value_copy_func (value,
  1133. cb->ud) : (gpointer)value;
  1134. g_hash_table_insert (cb->dst, nkey, nvalue);
  1135. }
  1136. /**
  1137. * Deep copy of one hash table to another
  1138. * @param src source hash
  1139. * @param dst destination hash
  1140. * @param key_copy_func function called to copy or modify keys (or NULL)
  1141. * @param value_copy_func function called to copy or modify values (or NULL)
  1142. * @param ud user data for copy functions
  1143. */
  1144. void
  1145. rspamd_hash_table_copy (GHashTable *src, GHashTable *dst,
  1146. gpointer (*key_copy_func)(gconstpointer data, gpointer ud),
  1147. gpointer (*value_copy_func)(gconstpointer data, gpointer ud),
  1148. gpointer ud)
  1149. {
  1150. struct hash_copy_callback_data cb;
  1151. if (src != NULL && dst != NULL) {
  1152. cb.key_copy_func = key_copy_func;
  1153. cb.value_copy_func = value_copy_func;
  1154. cb.ud = ud;
  1155. cb.dst = dst;
  1156. g_hash_table_foreach (src, copy_foreach_callback, &cb);
  1157. }
  1158. }
  1159. static volatile sig_atomic_t saved_signo[NSIG];
  1160. static
  1161. void
  1162. read_pass_tmp_sig_handler (int s)
  1163. {
  1164. saved_signo[s] = 1;
  1165. }
  1166. #ifndef _PATH_TTY
  1167. # define _PATH_TTY "/dev/tty"
  1168. #endif
  1169. gint
  1170. rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key)
  1171. {
  1172. #ifdef HAVE_READPASSPHRASE_H
  1173. if (readpassphrase ("Enter passphrase: ", buf, size, RPP_ECHO_OFF |
  1174. RPP_REQUIRE_TTY) == NULL) {
  1175. return 0;
  1176. }
  1177. return strlen (buf);
  1178. #else
  1179. struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
  1180. struct sigaction savetstp, savettin, savettou, savepipe;
  1181. struct termios term, oterm;
  1182. gint input, output, i;
  1183. gchar *end, *p, ch;
  1184. restart:
  1185. if ((input = output = open (_PATH_TTY, O_RDWR)) == -1) {
  1186. errno = ENOTTY;
  1187. return 0;
  1188. }
  1189. (void)fcntl (input, F_SETFD, FD_CLOEXEC);
  1190. /* Turn echo off */
  1191. if (tcgetattr (input, &oterm) != 0) {
  1192. close (input);
  1193. errno = ENOTTY;
  1194. return 0;
  1195. }
  1196. memcpy (&term, &oterm, sizeof(term));
  1197. term.c_lflag &= ~(ECHO | ECHONL);
  1198. if (tcsetattr (input, TCSAFLUSH, &term) == -1) {
  1199. errno = ENOTTY;
  1200. close (input);
  1201. return 0;
  1202. }
  1203. g_assert (write (output, "Enter passphrase: ", sizeof ("Enter passphrase: ") -
  1204. 1) != -1);
  1205. /* Save the current sighandler */
  1206. for (i = 0; i < NSIG; i++) {
  1207. saved_signo[i] = 0;
  1208. }
  1209. sigemptyset (&sa.sa_mask);
  1210. sa.sa_flags = 0;
  1211. sa.sa_handler = read_pass_tmp_sig_handler;
  1212. (void)sigaction (SIGALRM, &sa, &savealrm);
  1213. (void)sigaction (SIGHUP, &sa, &savehup);
  1214. (void)sigaction (SIGINT, &sa, &saveint);
  1215. (void)sigaction (SIGPIPE, &sa, &savepipe);
  1216. (void)sigaction (SIGQUIT, &sa, &savequit);
  1217. (void)sigaction (SIGTERM, &sa, &saveterm);
  1218. (void)sigaction (SIGTSTP, &sa, &savetstp);
  1219. (void)sigaction (SIGTTIN, &sa, &savettin);
  1220. (void)sigaction (SIGTTOU, &sa, &savettou);
  1221. /* Now read a passphrase */
  1222. p = buf;
  1223. end = p + size - 1;
  1224. while (read (input, &ch, 1) == 1 && ch != '\n' && ch != '\r') {
  1225. if (p < end) {
  1226. *p++ = ch;
  1227. }
  1228. }
  1229. *p = '\0';
  1230. g_assert (write (output, "\n", 1) == 1);
  1231. /* Restore terminal state */
  1232. if (memcmp (&term, &oterm, sizeof (term)) != 0) {
  1233. while (tcsetattr (input, TCSAFLUSH, &oterm) == -1 &&
  1234. errno == EINTR && !saved_signo[SIGTTOU]) ;
  1235. }
  1236. /* Restore signal handlers */
  1237. (void)sigaction (SIGALRM, &savealrm, NULL);
  1238. (void)sigaction (SIGHUP, &savehup, NULL);
  1239. (void)sigaction (SIGINT, &saveint, NULL);
  1240. (void)sigaction (SIGQUIT, &savequit, NULL);
  1241. (void)sigaction (SIGPIPE, &savepipe, NULL);
  1242. (void)sigaction (SIGTERM, &saveterm, NULL);
  1243. (void)sigaction (SIGTSTP, &savetstp, NULL);
  1244. (void)sigaction (SIGTTIN, &savettin, NULL);
  1245. (void)sigaction (SIGTTOU, &savettou, NULL);
  1246. close (input);
  1247. /* Send signals pending */
  1248. for (i = 0; i < NSIG; i++) {
  1249. if (saved_signo[i]) {
  1250. kill (getpid (), i);
  1251. switch (i) {
  1252. case SIGTSTP:
  1253. case SIGTTIN:
  1254. case SIGTTOU:
  1255. goto restart;
  1256. }
  1257. }
  1258. }
  1259. return (p - buf);
  1260. #endif
  1261. }
  1262. #ifdef HAVE_CLOCK_GETTIME
  1263. # ifdef CLOCK_MONOTONIC_COARSE
  1264. # define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_COARSE
  1265. # elif defined(CLOCK_MONOTONIC_FAST)
  1266. # define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_FAST
  1267. # else
  1268. # define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC
  1269. # endif
  1270. #endif
  1271. gdouble
  1272. rspamd_get_ticks (gboolean rdtsc_ok)
  1273. {
  1274. gdouble res;
  1275. #ifdef HAVE_RDTSC
  1276. # ifdef __x86_64__
  1277. guint64 r64;
  1278. if (rdtsc_ok) {
  1279. __builtin_ia32_lfence ();
  1280. r64 = __rdtsc ();
  1281. /* Preserve lower 52 bits */
  1282. res = r64 & ((1ULL << 53) - 1);
  1283. return res;
  1284. }
  1285. # endif
  1286. #endif
  1287. #ifdef HAVE_CLOCK_GETTIME
  1288. struct timespec ts;
  1289. gint clk_id = RSPAMD_FAST_MONOTONIC_CLOCK;
  1290. clock_gettime (clk_id, &ts);
  1291. if (rdtsc_ok) {
  1292. res = (double) ts.tv_sec * 1e9 + ts.tv_nsec;
  1293. }
  1294. else {
  1295. res = (double) ts.tv_sec + ts.tv_nsec / 1000000000.;
  1296. }
  1297. # elif defined(__APPLE__)
  1298. if (rdtsc_ok) {
  1299. res = mach_absolute_time ();
  1300. }
  1301. else {
  1302. res = mach_absolute_time () / 1000000000.;
  1303. }
  1304. #else
  1305. struct timeval tv;
  1306. (void)gettimeofday (&tv, NULL);
  1307. if (rdtsc_ok) {
  1308. res = (double) ts.tv_sec * 1e9 + tv.tv_usec * 1e3;
  1309. }
  1310. else {
  1311. res = (double)tv.tv_sec + tv.tv_usec / 1000000.;
  1312. }
  1313. #endif
  1314. return res;
  1315. }
  1316. gdouble
  1317. rspamd_get_virtual_ticks (void)
  1318. {
  1319. gdouble res;
  1320. #ifdef HAVE_CLOCK_GETTIME
  1321. struct timespec ts;
  1322. static clockid_t cid = (clockid_t)-1;
  1323. if (cid == (clockid_t)-1) {
  1324. # ifdef HAVE_CLOCK_GETCPUCLOCKID
  1325. if (clock_getcpuclockid (0, &cid) == -1) {
  1326. # endif
  1327. # ifdef CLOCK_PROCESS_CPUTIME_ID
  1328. cid = CLOCK_PROCESS_CPUTIME_ID;
  1329. # elif defined(CLOCK_PROF)
  1330. cid = CLOCK_PROF;
  1331. # else
  1332. cid = CLOCK_REALTIME;
  1333. # endif
  1334. # ifdef HAVE_CLOCK_GETCPUCLOCKID
  1335. }
  1336. # endif
  1337. }
  1338. clock_gettime (cid, &ts);
  1339. res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.;
  1340. #elif defined(__APPLE__)
  1341. thread_port_t thread = mach_thread_self ();
  1342. mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
  1343. thread_basic_info_data_t info;
  1344. if (thread_info (thread, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS) {
  1345. return -1;
  1346. }
  1347. res = info.user_time.seconds + info.system_time.seconds;
  1348. res += ((gdouble)(info.user_time.microseconds + info.system_time.microseconds)) / 1e6;
  1349. mach_port_deallocate(mach_task_self(), thread);
  1350. #elif defined(HAVE_RUSAGE_SELF)
  1351. struct rusage rusage;
  1352. if (getrusage (RUSAGE_SELF, &rusage) != -1) {
  1353. res = (double) rusage.ru_utime.tv_sec +
  1354. (double) rusage.ru_utime.tv_usec / 1000000.0;
  1355. }
  1356. #else
  1357. res = clock () / (double)CLOCKS_PER_SEC;
  1358. #endif
  1359. return res;
  1360. }
  1361. gdouble
  1362. rspamd_get_calendar_ticks (void)
  1363. {
  1364. gdouble res;
  1365. #ifdef HAVE_CLOCK_GETTIME
  1366. struct timespec ts;
  1367. clock_gettime (CLOCK_REALTIME, &ts);
  1368. res = ts_to_double (&ts);
  1369. #else
  1370. struct timeval tv;
  1371. if (gettimeofday (&tv, NULL) == 0) {
  1372. res = tv_to_double (&tv);
  1373. }
  1374. else {
  1375. res = time (NULL);
  1376. }
  1377. #endif
  1378. return res;
  1379. }
  1380. void
  1381. rspamd_random_hex (guchar *buf, guint64 len)
  1382. {
  1383. static const gchar hexdigests[16] = "0123456789abcdef";
  1384. gint64 i;
  1385. g_assert (len > 0);
  1386. ottery_rand_bytes (buf, ceil (len / 2.0));
  1387. for (i = (gint64)len - 1; i >= 0; i -= 2) {
  1388. buf[i] = hexdigests[buf[i / 2] & 0xf];
  1389. if (i > 0) {
  1390. buf[i - 1] = hexdigests[(buf[i / 2] >> 4) & 0xf];
  1391. }
  1392. }
  1393. }
  1394. gint
  1395. rspamd_shmem_mkstemp (gchar *pattern)
  1396. {
  1397. gint fd = -1;
  1398. gchar *nbuf, *xpos;
  1399. gsize blen;
  1400. xpos = strchr (pattern, 'X');
  1401. if (xpos == NULL) {
  1402. errno = EINVAL;
  1403. return -1;
  1404. }
  1405. blen = strlen (pattern);
  1406. nbuf = g_malloc (blen + 1);
  1407. rspamd_strlcpy (nbuf, pattern, blen + 1);
  1408. xpos = nbuf + (xpos - pattern);
  1409. for (;;) {
  1410. rspamd_random_hex (xpos, blen - (xpos - nbuf));
  1411. fd = shm_open (nbuf, O_RDWR | O_EXCL | O_CREAT, 0600);
  1412. if (fd != -1) {
  1413. rspamd_strlcpy (pattern, nbuf, blen + 1);
  1414. break;
  1415. }
  1416. else if (errno != EEXIST) {
  1417. g_free (nbuf);
  1418. return -1;
  1419. }
  1420. }
  1421. g_free (nbuf);
  1422. return fd;
  1423. }
  1424. void
  1425. rspamd_ptr_array_free_hard (gpointer p)
  1426. {
  1427. GPtrArray *ar = (GPtrArray *)p;
  1428. g_ptr_array_free (ar, TRUE);
  1429. }
  1430. void
  1431. rspamd_array_free_hard (gpointer p)
  1432. {
  1433. GArray *ar = (GArray *)p;
  1434. g_array_free (ar, TRUE);
  1435. }
  1436. void
  1437. rspamd_gstring_free_hard (gpointer p)
  1438. {
  1439. GString *ar = (GString *)p;
  1440. g_string_free (ar, TRUE);
  1441. }
  1442. void rspamd_gerror_free_maybe (gpointer p)
  1443. {
  1444. GError **err;
  1445. if (p) {
  1446. err = (GError **)p;
  1447. if (*err) {
  1448. g_error_free (*err);
  1449. }
  1450. }
  1451. }
  1452. /*
  1453. * Openblas creates threads that are not supported by
  1454. * jemalloc allocator (aside of being bloody stupid). So this hack
  1455. * is intended to set number of threads to one by default.
  1456. * FIXME: is it legit to do so in ctor?
  1457. */
  1458. #ifdef HAVE_OPENBLAS_SET_NUM_THREADS
  1459. extern void openblas_set_num_threads(int num_threads);
  1460. RSPAMD_CONSTRUCTOR (openblas_thread_fix_ctor)
  1461. {
  1462. openblas_set_num_threads (1);
  1463. }
  1464. #endif
  1465. #ifdef HAVE_BLI_THREAD_SET_NUM_THREADS
  1466. extern void bli_thread_set_num_threads(int num_threads);
  1467. RSPAMD_CONSTRUCTOR (blis_thread_fix_ctor)
  1468. {
  1469. bli_thread_set_num_threads (1);
  1470. }
  1471. #endif
  1472. guint64
  1473. rspamd_hash_seed (void)
  1474. {
  1475. #if 0
  1476. static guint64 seed;
  1477. if (seed == 0) {
  1478. seed = ottery_rand_uint64 ();
  1479. }
  1480. #endif
  1481. /* Proved to be random, I promise! */
  1482. /*
  1483. * TODO: discover if it worth to use random seed on run
  1484. * with ordinary hash function or we need to switch to
  1485. * siphash1-3 or other slow cooker function...
  1486. */
  1487. return 0xabf9727ba290690bULL;
  1488. }
  1489. static inline gdouble
  1490. rspamd_double_from_int64 (guint64 x)
  1491. {
  1492. const union { guint64 i; double d; } u = {
  1493. .i = G_GUINT64_CONSTANT(0x3FF) << 52 | x >> 12
  1494. };
  1495. return u.d - 1.0;
  1496. }
  1497. gdouble
  1498. rspamd_random_double (void)
  1499. {
  1500. guint64 rnd_int;
  1501. rnd_int = ottery_rand_uint64 ();
  1502. return rspamd_double_from_int64 (rnd_int);
  1503. }
  1504. static guint64*
  1505. xorshifto_seed (void)
  1506. {
  1507. static guint64 xorshifto_seed[4];
  1508. static bool initialized = false;
  1509. if (G_UNLIKELY(!initialized)) {
  1510. ottery_rand_bytes((void *)xorshifto_seed, sizeof (xorshifto_seed));
  1511. initialized = true;
  1512. }
  1513. return xorshifto_seed;
  1514. }
  1515. static inline guint64
  1516. xoroshiro_rotl (const guint64 x, int k) {
  1517. return (x << k) | (x >> (64 - k));
  1518. }
  1519. gdouble
  1520. rspamd_random_double_fast (void)
  1521. {
  1522. return rspamd_random_double_fast_seed (xorshifto_seed());
  1523. }
  1524. /* xoshiro256+ */
  1525. inline gdouble
  1526. rspamd_random_double_fast_seed (guint64 seed[4])
  1527. {
  1528. const uint64_t result = seed[0] + seed[3];
  1529. const uint64_t t = seed[1] << 17;
  1530. seed[2] ^= seed[0];
  1531. seed[3] ^= seed[1];
  1532. seed[1] ^= seed[2];
  1533. seed[0] ^= seed[3];
  1534. seed[2] ^= t;
  1535. seed[3] = xoroshiro_rotl (seed[3], 45);
  1536. return rspamd_double_from_int64 (result);
  1537. }
  1538. /* xoroshiro256** */
  1539. static inline guint64
  1540. rspamd_random_uint64_fast_seed (guint64 seed[4])
  1541. {
  1542. const uint64_t result = xoroshiro_rotl (seed[1] * 5, 7) * 9;
  1543. const uint64_t t = seed[1] << 17;
  1544. seed[2] ^= seed[0];
  1545. seed[3] ^= seed[1];
  1546. seed[1] ^= seed[2];
  1547. seed[0] ^= seed[3];
  1548. seed[2] ^= t;
  1549. seed[3] = xoroshiro_rotl (seed[3], 45);
  1550. return result;
  1551. }
  1552. guint64
  1553. rspamd_random_uint64_fast (void)
  1554. {
  1555. return rspamd_random_uint64_fast_seed (xorshifto_seed());
  1556. }
  1557. void
  1558. rspamd_random_seed_fast (void)
  1559. {
  1560. (void)xorshifto_seed();
  1561. }
  1562. gdouble
  1563. rspamd_time_jitter (gdouble in, gdouble jitter)
  1564. {
  1565. if (jitter == 0) {
  1566. jitter = in;
  1567. }
  1568. return in + jitter * rspamd_random_double ();
  1569. }
  1570. gboolean
  1571. rspamd_constant_memcmp (const void *a, const void *b, gsize len)
  1572. {
  1573. gsize lena, lenb, i;
  1574. guint16 d, r = 0, m;
  1575. guint16 v;
  1576. const guint8 *aa = (const guint8 *)a,
  1577. *bb = (const guint8 *)b;
  1578. if (len == 0) {
  1579. lena = strlen ((const char*)a);
  1580. lenb = strlen ((const char*)b);
  1581. if (lena != lenb) {
  1582. return FALSE;
  1583. }
  1584. len = lena;
  1585. }
  1586. for (i = 0; i < len; i++) {
  1587. v = ((guint16)(guint8)r) + 255;
  1588. m = v / 256 - 1;
  1589. d = (guint16)((int)aa[i] - (int)bb[i]);
  1590. r |= (d & m);
  1591. }
  1592. return (((gint32)(guint16)((guint32)r + 0x8000) - 0x8000) == 0);
  1593. }
  1594. int
  1595. rspamd_file_xopen (const char *fname, int oflags, guint mode,
  1596. gboolean allow_symlink)
  1597. {
  1598. struct stat sb;
  1599. int fd, flags = oflags;
  1600. if (lstat (fname, &sb) == -1) {
  1601. if (errno != ENOENT) {
  1602. return (-1);
  1603. }
  1604. }
  1605. else if (!S_ISREG (sb.st_mode)) {
  1606. if (S_ISLNK (sb.st_mode)) {
  1607. if (!allow_symlink) {
  1608. return -1;
  1609. }
  1610. }
  1611. else {
  1612. return -1;
  1613. }
  1614. }
  1615. #ifdef HAVE_OCLOEXEC
  1616. flags |= O_CLOEXEC;
  1617. #endif
  1618. #ifdef HAVE_ONOFOLLOW
  1619. if (!allow_symlink) {
  1620. flags |= O_NOFOLLOW;
  1621. fd = open (fname, flags, mode);
  1622. }
  1623. else {
  1624. fd = open (fname, flags, mode);
  1625. }
  1626. #else
  1627. fd = open (fname, flags, mode);
  1628. #endif
  1629. #ifndef HAVE_OCLOEXEC
  1630. int serrno;
  1631. if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
  1632. serrno = errno;
  1633. close (fd);
  1634. errno = serrno;
  1635. return -1;
  1636. }
  1637. #endif
  1638. return (fd);
  1639. }
  1640. gpointer
  1641. rspamd_file_xmap (const char *fname, guint mode, gsize *size,
  1642. gboolean allow_symlink)
  1643. {
  1644. gint fd;
  1645. struct stat sb;
  1646. gpointer map;
  1647. g_assert (fname != NULL);
  1648. g_assert (size != NULL);
  1649. if (mode & PROT_WRITE) {
  1650. fd = rspamd_file_xopen (fname, O_RDWR, 0, allow_symlink);
  1651. }
  1652. else {
  1653. fd = rspamd_file_xopen (fname, O_RDONLY, 0, allow_symlink);
  1654. }
  1655. if (fd == -1) {
  1656. return NULL;
  1657. }
  1658. if (fstat (fd, &sb) == -1 || !S_ISREG (sb.st_mode)) {
  1659. close (fd);
  1660. *size = (gsize)-1;
  1661. return NULL;
  1662. }
  1663. if (sb.st_size == 0) {
  1664. close (fd);
  1665. *size = (gsize)0;
  1666. return NULL;
  1667. }
  1668. map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0);
  1669. close (fd);
  1670. if (map == MAP_FAILED) {
  1671. return NULL;
  1672. }
  1673. *size = sb.st_size;
  1674. return map;
  1675. }
  1676. gpointer
  1677. rspamd_shmem_xmap (const char *fname, guint mode,
  1678. gsize *size)
  1679. {
  1680. gint fd;
  1681. struct stat sb;
  1682. gpointer map;
  1683. g_assert (fname != NULL);
  1684. g_assert (size != NULL);
  1685. #ifdef HAVE_SANE_SHMEM
  1686. if (mode & PROT_WRITE) {
  1687. fd = shm_open (fname, O_RDWR, 0);
  1688. }
  1689. else {
  1690. fd = shm_open (fname, O_RDONLY, 0);
  1691. }
  1692. #else
  1693. if (mode & PROT_WRITE) {
  1694. fd = open (fname, O_RDWR, 0);
  1695. }
  1696. else {
  1697. fd = open (fname, O_RDONLY, 0);
  1698. }
  1699. #endif
  1700. if (fd == -1) {
  1701. return NULL;
  1702. }
  1703. if (fstat (fd, &sb) == -1) {
  1704. close (fd);
  1705. return NULL;
  1706. }
  1707. map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0);
  1708. close (fd);
  1709. if (map == MAP_FAILED) {
  1710. return NULL;
  1711. }
  1712. *size = sb.st_size;
  1713. return map;
  1714. }
  1715. /*
  1716. * A(x - 0.5)^4 + B(x - 0.5)^3 + C(x - 0.5)^2 + D(x - 0.5)
  1717. * A = 32,
  1718. * B = -6
  1719. * C = -7
  1720. * D = 3
  1721. * y = 32(x - 0.5)^4 - 6(x - 0.5)^3 - 7(x - 0.5)^2 + 3(x - 0.5)
  1722. *
  1723. * New approach:
  1724. * y = ((x - bias)*2)^8
  1725. */
  1726. gdouble
  1727. rspamd_normalize_probability (gdouble x, gdouble bias)
  1728. {
  1729. gdouble xx;
  1730. xx = (x - bias) * 2.0;
  1731. return pow (xx, 8);
  1732. }
  1733. /*
  1734. * Calculations from musl libc
  1735. */
  1736. guint64
  1737. rspamd_tm_to_time (const struct tm *tm, glong tz)
  1738. {
  1739. guint64 result;
  1740. gboolean is_leap = FALSE;
  1741. gint leaps, y = tm->tm_year, cycles, rem, centuries;
  1742. glong offset = (tz / 100) * 3600 + (tz % 100) * 60;
  1743. /* How many seconds in each month from the beginning of the year */
  1744. static const gint secs_through_month[] = {
  1745. 0, 31*86400, 59*86400, 90*86400,
  1746. 120*86400, 151*86400, 181*86400, 212*86400,
  1747. 243*86400, 273*86400, 304*86400, 334*86400
  1748. };
  1749. /* Convert year */
  1750. if (tm->tm_year - 2ULL <= 136) {
  1751. leaps = (y - 68) / 4;
  1752. if (!((y - 68) & 3)) {
  1753. leaps--;
  1754. is_leap = 1;
  1755. }
  1756. result = 31536000 * (y - 70) + 86400 * leaps;
  1757. }
  1758. else {
  1759. cycles = (y - 100) / 400;
  1760. rem = (y - 100) % 400;
  1761. if (rem < 0) {
  1762. cycles--;
  1763. rem += 400;
  1764. }
  1765. if (!rem) {
  1766. is_leap = 1;
  1767. centuries = 0;
  1768. leaps = 0;
  1769. }
  1770. else {
  1771. if (rem >= 200) {
  1772. if (rem >= 300) {
  1773. centuries = 3;
  1774. rem -= 300;
  1775. }
  1776. else {
  1777. centuries = 2;
  1778. rem -= 200;
  1779. }
  1780. }
  1781. else {
  1782. if (rem >= 100) {
  1783. centuries = 1;
  1784. rem -= 100;
  1785. }
  1786. else {
  1787. centuries = 0;
  1788. }
  1789. }
  1790. if (!rem) {
  1791. is_leap = 1;
  1792. leaps = 0;
  1793. } else {
  1794. leaps = rem / 4U;
  1795. rem %= 4U;
  1796. is_leap = !rem;
  1797. }
  1798. }
  1799. leaps += 97 * cycles + 24 * centuries - (gint)is_leap;
  1800. result = (y - 100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
  1801. }
  1802. /* Now convert months to seconds */
  1803. result += secs_through_month[tm->tm_mon];
  1804. /* One more day */
  1805. if (is_leap && tm->tm_mon >= 2) {
  1806. result += 86400;
  1807. }
  1808. result += 86400LL * (tm->tm_mday-1);
  1809. result += 3600LL * tm->tm_hour;
  1810. result += 60LL * tm->tm_min;
  1811. result += tm->tm_sec;
  1812. /* Now apply tz offset */
  1813. result -= offset;
  1814. return result;
  1815. }
  1816. void
  1817. rspamd_gmtime (gint64 ts, struct tm *dest)
  1818. {
  1819. guint64 days, secs, years;
  1820. int remdays, remsecs, remyears;
  1821. int leap_400_cycles, leap_100_cycles, leap_4_cycles;
  1822. int months;
  1823. int wday, yday, leap;
  1824. /* From March */
  1825. static const uint8_t days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29};
  1826. static const guint64 leap_epoch = 946684800ULL + 86400 * (31 + 29);
  1827. static const guint64 days_per_400y = 365*400 + 97;
  1828. static const guint64 days_per_100y = 365*100 + 24;
  1829. static const guint64 days_per_4y = 365*4 + 1;
  1830. secs = ts - leap_epoch;
  1831. days = secs / 86400;
  1832. remsecs = secs % 86400;
  1833. if (remsecs < 0) {
  1834. remsecs += 86400;
  1835. days--;
  1836. }
  1837. wday = (3 + days) % 7;
  1838. if (wday < 0) {
  1839. wday += 7;
  1840. }
  1841. /* Deal with gregorian adjustments */
  1842. leap_400_cycles = days / days_per_400y;
  1843. remdays = days % days_per_400y;
  1844. if (remdays < 0) {
  1845. remdays += days_per_400y;
  1846. leap_400_cycles--;
  1847. }
  1848. leap_100_cycles = remdays / days_per_100y;
  1849. if (leap_100_cycles == 4) {
  1850. /* 400 years */
  1851. leap_100_cycles--;
  1852. }
  1853. remdays -= leap_100_cycles * days_per_100y;
  1854. leap_4_cycles = remdays / days_per_4y;
  1855. if (leap_4_cycles == 25) {
  1856. /* 100 years */
  1857. leap_4_cycles--;
  1858. }
  1859. remdays -= leap_4_cycles * days_per_4y;
  1860. remyears = remdays / 365;
  1861. if (remyears == 4) {
  1862. /* Ordinary leap year */
  1863. remyears--;
  1864. }
  1865. remdays -= remyears * 365;
  1866. leap = !remyears && (leap_4_cycles || !leap_100_cycles);
  1867. yday = remdays + 31 + 28 + leap;
  1868. if (yday >= 365 + leap) {
  1869. yday -= 365 + leap;
  1870. }
  1871. years = remyears + 4 * leap_4_cycles + 100 * leap_100_cycles +
  1872. 400ULL * leap_400_cycles;
  1873. for (months=0; days_in_month[months] <= remdays; months++) {
  1874. remdays -= days_in_month[months];
  1875. }
  1876. if (months >= 10) {
  1877. months -= 12;
  1878. years++;
  1879. }
  1880. dest->tm_year = years + 100;
  1881. dest->tm_mon = months + 2;
  1882. dest->tm_mday = remdays + 1;
  1883. dest->tm_wday = wday;
  1884. dest->tm_yday = yday;
  1885. dest->tm_hour = remsecs / 3600;
  1886. dest->tm_min = remsecs / 60 % 60;
  1887. dest->tm_sec = remsecs % 60;
  1888. #if !defined(__sun)
  1889. dest->tm_gmtoff = 0;
  1890. dest->tm_zone = "GMT";
  1891. #endif
  1892. }
  1893. void
  1894. rspamd_localtime (gint64 ts, struct tm *dest)
  1895. {
  1896. time_t t = ts;
  1897. localtime_r (&t, dest);
  1898. }
  1899. gboolean
  1900. rspamd_fstring_gzip (rspamd_fstring_t **in)
  1901. {
  1902. z_stream strm;
  1903. gint rc;
  1904. rspamd_fstring_t *comp, *buf = *in;
  1905. gchar *p;
  1906. gsize remain;
  1907. memset (&strm, 0, sizeof (strm));
  1908. rc = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  1909. MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY);
  1910. if (rc != Z_OK) {
  1911. return FALSE;
  1912. }
  1913. comp = rspamd_fstring_sized_new (deflateBound (&strm, buf->len));
  1914. strm.avail_in = buf->len;
  1915. strm.next_in = (guchar *)buf->str;
  1916. p = comp->str;
  1917. remain = comp->allocated;
  1918. while (strm.avail_in != 0) {
  1919. strm.avail_out = remain;
  1920. strm.next_out = p;
  1921. rc = deflate (&strm, Z_FINISH);
  1922. if (rc != Z_OK && rc != Z_BUF_ERROR) {
  1923. if (rc == Z_STREAM_END) {
  1924. break;
  1925. }
  1926. else {
  1927. rspamd_fstring_free (comp);
  1928. deflateEnd (&strm);
  1929. return FALSE;
  1930. }
  1931. }
  1932. comp->len = strm.total_out;
  1933. if (strm.avail_out == 0 && strm.avail_in != 0) {
  1934. /* Need to allocate more */
  1935. remain = comp->len;
  1936. comp = rspamd_fstring_grow (comp, strm.avail_in);
  1937. p = comp->str + remain;
  1938. remain = comp->allocated - remain;
  1939. }
  1940. }
  1941. deflateEnd (&strm);
  1942. comp->len = strm.total_out;
  1943. rspamd_fstring_free (buf); /* We replace buf with its compressed version */
  1944. *in = comp;
  1945. return TRUE;
  1946. }
  1947. static gboolean
  1948. rspamd_glob_dir (const gchar *full_path, const gchar *pattern,
  1949. gboolean recursive, guint rec_len,
  1950. GPtrArray *res, GError **err)
  1951. {
  1952. glob_t globbuf;
  1953. const gchar *path;
  1954. static gchar pathbuf[PATH_MAX]; /* Static to help recursion */
  1955. guint i;
  1956. gint rc;
  1957. static const guint rec_lim = 16;
  1958. struct stat st;
  1959. if (rec_len > rec_lim) {
  1960. g_set_error (err, g_quark_from_static_string ("glob"), EOVERFLOW,
  1961. "maximum nesting is reached: %d", rec_lim);
  1962. return FALSE;
  1963. }
  1964. memset (&globbuf, 0, sizeof (globbuf));
  1965. if ((rc = glob (full_path, 0, NULL, &globbuf)) != 0) {
  1966. if (rc != GLOB_NOMATCH) {
  1967. g_set_error (err, g_quark_from_static_string ("glob"), errno,
  1968. "glob %s failed: %s", full_path, strerror (errno));
  1969. globfree (&globbuf);
  1970. return FALSE;
  1971. }
  1972. else {
  1973. globfree (&globbuf);
  1974. return TRUE;
  1975. }
  1976. }
  1977. for (i = 0; i < globbuf.gl_pathc; i ++) {
  1978. path = globbuf.gl_pathv[i];
  1979. if (stat (path, &st) == -1) {
  1980. if (errno == EPERM || errno == EACCES || errno == ELOOP) {
  1981. /* Silently ignore */
  1982. continue;
  1983. }
  1984. g_set_error (err, g_quark_from_static_string ("glob"), errno,
  1985. "stat %s failed: %s", path, strerror (errno));
  1986. globfree (&globbuf);
  1987. return FALSE;
  1988. }
  1989. if (S_ISREG (st.st_mode)) {
  1990. g_ptr_array_add (res, g_strdup (path));
  1991. }
  1992. else if (recursive && S_ISDIR (st.st_mode)) {
  1993. rspamd_snprintf (pathbuf, sizeof (pathbuf), "%s%c%s",
  1994. path, G_DIR_SEPARATOR, pattern);
  1995. if (!rspamd_glob_dir (full_path, pattern, recursive, rec_len + 1,
  1996. res, err)) {
  1997. globfree (&globbuf);
  1998. return FALSE;
  1999. }
  2000. }
  2001. }
  2002. globfree (&globbuf);
  2003. return TRUE;
  2004. }
  2005. GPtrArray *
  2006. rspamd_glob_path (const gchar *dir,
  2007. const gchar *pattern,
  2008. gboolean recursive,
  2009. GError **err)
  2010. {
  2011. gchar path[PATH_MAX];
  2012. GPtrArray *res;
  2013. res = g_ptr_array_new_full (32, (GDestroyNotify)g_free);
  2014. rspamd_snprintf (path, sizeof (path), "%s%c%s", dir, G_DIR_SEPARATOR, pattern);
  2015. if (!rspamd_glob_dir (path, pattern, recursive, 0, res, err)) {
  2016. g_ptr_array_free (res, TRUE);
  2017. return NULL;
  2018. }
  2019. return res;
  2020. }
  2021. double
  2022. rspamd_set_counter (struct rspamd_counter_data *cd, gdouble value)
  2023. {
  2024. gdouble cerr;
  2025. /* Cumulative moving average using per-process counter data */
  2026. if (cd->number == 0) {
  2027. cd->mean = 0;
  2028. cd->stddev = 0;
  2029. }
  2030. cd->mean += (value - cd->mean) / (gdouble)(++cd->number);
  2031. cerr = (value - cd->mean) * (value - cd->mean);
  2032. cd->stddev += (cerr - cd->stddev) / (gdouble)(cd->number);
  2033. return cd->mean;
  2034. }
  2035. float
  2036. rspamd_set_counter_ema (struct rspamd_counter_data *cd,
  2037. float value,
  2038. float alpha)
  2039. {
  2040. float diff, incr;
  2041. /* Cumulative moving average using per-process counter data */
  2042. if (cd->number == 0) {
  2043. cd->mean = 0;
  2044. cd->stddev = 0;
  2045. }
  2046. diff = value - cd->mean;
  2047. incr = diff * alpha;
  2048. cd->mean += incr;
  2049. cd->stddev = (1.0f - alpha) * (cd->stddev + diff * incr);
  2050. cd->number ++;
  2051. return cd->mean;
  2052. }
  2053. void
  2054. rspamd_ptr_array_shuffle (GPtrArray *ar)
  2055. {
  2056. if (ar->len < 2) {
  2057. return;
  2058. }
  2059. guint n = ar->len;
  2060. for (guint i = 0; i < n - 1; i++) {
  2061. guint j = i + rspamd_random_uint64_fast () % (n - i);
  2062. gpointer t = g_ptr_array_index (ar, j);
  2063. g_ptr_array_index (ar, j) = g_ptr_array_index (ar, i);
  2064. g_ptr_array_index (ar, i) = t;
  2065. }
  2066. }