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.

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