Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

protocol.c 53KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamd.h"
  18. #include "message.h"
  19. #include "utlist.h"
  20. #include "libserver/http/http_private.h"
  21. #include "worker_private.h"
  22. #include "libserver/cfg_file_private.h"
  23. #include "libmime/scan_result_private.h"
  24. #include "contrib/zstd/zstd.h"
  25. #include "lua/lua_common.h"
  26. #include "unix-std.h"
  27. #include "protocol_internal.h"
  28. #include "libserver/mempool_vars_internal.h"
  29. #include "contrib/fastutf8/fastutf8.h"
  30. #include "task.h"
  31. #include <math.h>
  32. INIT_LOG_MODULE(protocol)
  33. #define msg_err_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  34. "protocol", task->task_pool->tag.uid, \
  35. G_STRFUNC, \
  36. __VA_ARGS__)
  37. #define msg_warn_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  38. "protocol", task->task_pool->tag.uid, \
  39. G_STRFUNC, \
  40. __VA_ARGS__)
  41. #define msg_info_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  42. "protocol", task->task_pool->tag.uid, \
  43. G_STRFUNC, \
  44. __VA_ARGS__)
  45. #define msg_debug_protocol(...) rspamd_conditional_debug_fast (NULL, NULL, \
  46. rspamd_protocol_log_id, "protocol", task->task_pool->tag.uid, \
  47. G_STRFUNC, \
  48. __VA_ARGS__)
  49. static GQuark
  50. rspamd_protocol_quark (void)
  51. {
  52. return g_quark_from_static_string ("protocol-error");
  53. }
  54. /*
  55. * Remove <> from the fixed string and copy it to the pool
  56. */
  57. static gchar *
  58. rspamd_protocol_escape_braces (struct rspamd_task *task, rspamd_ftok_t *in)
  59. {
  60. guint nchars = 0;
  61. const gchar *p;
  62. rspamd_ftok_t tok;
  63. gboolean has_obrace = FALSE;
  64. g_assert (in != NULL);
  65. g_assert (in->len > 0);
  66. p = in->begin;
  67. while ((g_ascii_isspace (*p) || *p == '<') && nchars < in->len) {
  68. if (*p == '<') {
  69. has_obrace = TRUE;
  70. }
  71. p++;
  72. nchars ++;
  73. }
  74. tok.begin = p;
  75. p = in->begin + in->len - 1;
  76. tok.len = in->len - nchars;
  77. while (g_ascii_isspace (*p) && tok.len > 0) {
  78. p--;
  79. tok.len --;
  80. }
  81. if (has_obrace && *p == '>') {
  82. tok.len --;
  83. }
  84. return rspamd_mempool_ftokdup (task->task_pool, &tok);
  85. }
  86. #define COMPARE_CMD(str, cmd, len) (sizeof(cmd) - 1 == (len) && rspamd_lc_cmp((str), (cmd), (len)) == 0)
  87. static gboolean
  88. rspamd_protocol_handle_url (struct rspamd_task *task,
  89. struct rspamd_http_message *msg)
  90. {
  91. GHashTable *query_args;
  92. GHashTableIter it;
  93. struct http_parser_url u;
  94. const gchar *p;
  95. gsize pathlen;
  96. rspamd_ftok_t *key, *value;
  97. gpointer k, v;
  98. if (msg->url == NULL || msg->url->len == 0) {
  99. g_set_error (&task->err, rspamd_protocol_quark(), 400, "missing command");
  100. return FALSE;
  101. }
  102. if (http_parser_parse_url (msg->url->str, msg->url->len, 0, &u) != 0) {
  103. g_set_error (&task->err, rspamd_protocol_quark(), 400, "bad request URL");
  104. return FALSE;
  105. }
  106. if (!(u.field_set & (1 << UF_PATH))) {
  107. g_set_error (&task->err, rspamd_protocol_quark(), 400,
  108. "bad request URL: missing path");
  109. return FALSE;
  110. }
  111. p = msg->url->str + u.field_data[UF_PATH].off;
  112. pathlen = u.field_data[UF_PATH].len;
  113. if (*p == '/') {
  114. p ++;
  115. pathlen --;
  116. }
  117. switch (*p) {
  118. case 'c':
  119. case 'C':
  120. /* check */
  121. if (COMPARE_CMD (p, MSG_CMD_CHECK_V2, pathlen)) {
  122. task->cmd = CMD_CHECK_V2;
  123. msg_debug_protocol ("got checkv2 command");
  124. }
  125. else if (COMPARE_CMD (p, MSG_CMD_CHECK, pathlen)) {
  126. task->cmd = CMD_CHECK;
  127. msg_debug_protocol ("got check command");
  128. }
  129. else {
  130. goto err;
  131. }
  132. break;
  133. case 's':
  134. case 'S':
  135. /* symbols, skip */
  136. if (COMPARE_CMD (p, MSG_CMD_SYMBOLS, pathlen)) {
  137. task->cmd = CMD_CHECK;
  138. msg_debug_protocol ("got symbols -> old check command");
  139. }
  140. else if (COMPARE_CMD (p, MSG_CMD_SCAN, pathlen)) {
  141. task->cmd = CMD_CHECK;
  142. msg_debug_protocol ("got scan -> old check command");
  143. }
  144. else if (COMPARE_CMD (p, MSG_CMD_SKIP, pathlen)) {
  145. msg_debug_protocol ("got skip command");
  146. task->cmd = CMD_SKIP;
  147. }
  148. else {
  149. goto err;
  150. }
  151. break;
  152. case 'p':
  153. case 'P':
  154. /* ping, process */
  155. if (COMPARE_CMD (p, MSG_CMD_PING, pathlen)) {
  156. msg_debug_protocol ("got ping command");
  157. task->cmd = CMD_PING;
  158. task->flags |= RSPAMD_TASK_FLAG_SKIP;
  159. task->processed_stages |= RSPAMD_TASK_STAGE_DONE; /* Skip all */
  160. }
  161. else if (COMPARE_CMD (p, MSG_CMD_PROCESS, pathlen)) {
  162. msg_debug_protocol ("got process -> old check command");
  163. task->cmd = CMD_CHECK;
  164. }
  165. else {
  166. goto err;
  167. }
  168. break;
  169. case 'r':
  170. case 'R':
  171. /* report, report_ifspam */
  172. if (COMPARE_CMD (p, MSG_CMD_REPORT, pathlen)) {
  173. msg_debug_protocol ("got report -> old check command");
  174. task->cmd = CMD_CHECK;
  175. }
  176. else if (COMPARE_CMD (p, MSG_CMD_REPORT_IFSPAM, pathlen)) {
  177. msg_debug_protocol ("got reportifspam -> old check command");
  178. task->cmd = CMD_CHECK;
  179. }
  180. else {
  181. goto err;
  182. }
  183. break;
  184. default:
  185. goto err;
  186. }
  187. if (u.field_set & (1u << UF_QUERY)) {
  188. /* In case if we have a query, we need to store it somewhere */
  189. query_args = rspamd_http_message_parse_query (msg);
  190. /* Insert the rest of query params as HTTP headers */
  191. g_hash_table_iter_init (&it, query_args);
  192. while (g_hash_table_iter_next (&it, &k, &v)) {
  193. gchar *key_cpy;
  194. key = k;
  195. value = v;
  196. key_cpy = rspamd_mempool_ftokdup (task->task_pool, key);
  197. rspamd_http_message_add_header_len (msg, key_cpy,
  198. value->begin, value->len);
  199. msg_debug_protocol ("added header \"%T\" -> \"%T\" from HTTP query",
  200. key, value);
  201. }
  202. g_hash_table_unref (query_args);
  203. }
  204. return TRUE;
  205. err:
  206. g_set_error (&task->err, rspamd_protocol_quark(), 400, "invalid command");
  207. return FALSE;
  208. }
  209. static void
  210. rspamd_protocol_process_recipients (struct rspamd_task *task,
  211. const rspamd_ftok_t *hdr)
  212. {
  213. enum {
  214. skip_spaces,
  215. quoted_string,
  216. normal_string,
  217. } state = skip_spaces;
  218. const gchar *p, *end, *start_addr;
  219. struct rspamd_email_address *addr;
  220. p = hdr->begin;
  221. end = hdr->begin + hdr->len;
  222. start_addr = NULL;
  223. while (p < end) {
  224. switch (state) {
  225. case skip_spaces:
  226. if (g_ascii_isspace (*p)) {
  227. p ++;
  228. }
  229. else if (*p == '"') {
  230. start_addr = p;
  231. p ++;
  232. state = quoted_string;
  233. }
  234. else {
  235. state = normal_string;
  236. start_addr = p;
  237. }
  238. break;
  239. case quoted_string:
  240. if (*p == '"') {
  241. state = normal_string;
  242. p ++;
  243. }
  244. else if (*p == '\\') {
  245. /* Quoted pair */
  246. p += 2;
  247. }
  248. else {
  249. p ++;
  250. }
  251. break;
  252. case normal_string:
  253. if (*p == '"') {
  254. state = quoted_string;
  255. p ++;
  256. }
  257. else if (*p == ',' && start_addr != NULL && p > start_addr) {
  258. /* We have finished address, check what we have */
  259. addr = rspamd_email_address_from_smtp (start_addr,
  260. p - start_addr);
  261. if (addr) {
  262. if (task->rcpt_envelope == NULL) {
  263. task->rcpt_envelope = g_ptr_array_sized_new (
  264. 2);
  265. }
  266. g_ptr_array_add (task->rcpt_envelope, addr);
  267. }
  268. else {
  269. msg_err_protocol ("bad rcpt address: '%*s'",
  270. (int)(p - start_addr), start_addr);
  271. task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS;
  272. }
  273. start_addr = NULL;
  274. p ++;
  275. state = skip_spaces;
  276. }
  277. else {
  278. p ++;
  279. }
  280. break;
  281. }
  282. }
  283. /* Check remainder */
  284. if (start_addr && p > start_addr) {
  285. switch (state) {
  286. case normal_string:
  287. addr = rspamd_email_address_from_smtp (start_addr, end - start_addr);
  288. if (addr) {
  289. if (task->rcpt_envelope == NULL) {
  290. task->rcpt_envelope = g_ptr_array_sized_new (
  291. 2);
  292. }
  293. g_ptr_array_add (task->rcpt_envelope, addr);
  294. }
  295. else {
  296. msg_err_protocol ("bad rcpt address: '%*s'",
  297. (int)(end - start_addr), start_addr);
  298. task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS;
  299. }
  300. break;
  301. case skip_spaces:
  302. /* Do nothing */
  303. break;
  304. case quoted_string:
  305. default:
  306. msg_err_protocol ("bad state when parsing rcpt address: '%*s'",
  307. (int)(end - start_addr), start_addr);
  308. task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS;
  309. }
  310. }
  311. }
  312. #define COMPARE_FLAG_LIT(lit) (len == sizeof(lit) - 1 && memcmp ((lit), str, len) == 0)
  313. #define CHECK_PROTOCOL_FLAG(lit, fl) do { \
  314. if (!known && COMPARE_FLAG_LIT(lit)) { \
  315. task->protocol_flags |= (fl); \
  316. known = TRUE; \
  317. msg_debug_protocol ("add protocol flag %s", lit); \
  318. } \
  319. } while (0)
  320. #define CHECK_TASK_FLAG(lit, fl) do { \
  321. if (!known && COMPARE_FLAG_LIT(lit)) { \
  322. task->flags |= (fl); \
  323. known = TRUE; \
  324. msg_debug_protocol ("add task flag %s", lit); \
  325. } \
  326. } while (0)
  327. static void
  328. rspamd_protocol_handle_flag (struct rspamd_task *task, const gchar *str,
  329. gsize len)
  330. {
  331. gboolean known = FALSE;
  332. CHECK_TASK_FLAG("pass_all", RSPAMD_TASK_FLAG_PASS_ALL);
  333. CHECK_TASK_FLAG("no_log", RSPAMD_TASK_FLAG_NO_LOG);
  334. CHECK_TASK_FLAG("skip", RSPAMD_TASK_FLAG_SKIP);
  335. CHECK_TASK_FLAG("skip_process", RSPAMD_TASK_FLAG_SKIP_PROCESS);
  336. CHECK_TASK_FLAG("no_stat", RSPAMD_TASK_FLAG_NO_STAT);
  337. CHECK_TASK_FLAG("ssl", RSPAMD_TASK_FLAG_SSL);
  338. CHECK_TASK_FLAG("profile", RSPAMD_TASK_FLAG_PROFILE);
  339. CHECK_PROTOCOL_FLAG("milter", RSPAMD_TASK_PROTOCOL_FLAG_MILTER);
  340. CHECK_PROTOCOL_FLAG("zstd", RSPAMD_TASK_PROTOCOL_FLAG_COMPRESSED);
  341. CHECK_PROTOCOL_FLAG("ext_urls", RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS);
  342. CHECK_PROTOCOL_FLAG("body_block", RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK);
  343. CHECK_PROTOCOL_FLAG("groups", RSPAMD_TASK_PROTOCOL_FLAG_GROUPS);
  344. if (!known) {
  345. msg_warn_protocol ("unknown flag: %*s", (gint)len, str);
  346. }
  347. }
  348. #undef COMPARE_FLAG
  349. #undef CHECK_PROTOCOL_FLAG
  350. static void
  351. rspamd_protocol_process_flags (struct rspamd_task *task, const rspamd_ftok_t *hdr)
  352. {
  353. enum {
  354. skip_spaces,
  355. read_flag,
  356. } state = skip_spaces;
  357. const gchar *p, *end, *start;
  358. p = hdr->begin;
  359. end = hdr->begin + hdr->len;
  360. start = NULL;
  361. while (p < end) {
  362. switch (state) {
  363. case skip_spaces:
  364. if (g_ascii_isspace (*p)) {
  365. p ++;
  366. }
  367. else {
  368. state = read_flag;
  369. start = p;
  370. }
  371. break;
  372. case read_flag:
  373. if (*p == ',') {
  374. if (p > start) {
  375. rspamd_protocol_handle_flag (task, start, p - start);
  376. }
  377. start = NULL;
  378. state = skip_spaces;
  379. p ++;
  380. }
  381. else {
  382. p ++;
  383. }
  384. break;
  385. }
  386. }
  387. /* Check remainder */
  388. if (start && end > start && state == read_flag) {
  389. rspamd_protocol_handle_flag (task, start, end - start);
  390. }
  391. }
  392. #define IF_HEADER(name) \
  393. srch.begin = (name); \
  394. srch.len = sizeof (name) - 1; \
  395. if (rspamd_ftok_casecmp (hn_tok, &srch) == 0)
  396. gboolean
  397. rspamd_protocol_handle_headers (struct rspamd_task *task,
  398. struct rspamd_http_message *msg)
  399. {
  400. rspamd_ftok_t *hn_tok, *hv_tok, srch;
  401. gboolean has_ip = FALSE, seen_settings_header = FALSE;
  402. struct rspamd_http_header *header, *h;
  403. gchar *ntok;
  404. kh_foreach_value (msg->headers, header, {
  405. DL_FOREACH (header, h) {
  406. ntok = rspamd_mempool_ftokdup (task->task_pool, &h->name);
  407. hn_tok = rspamd_mempool_alloc (task->task_pool, sizeof (*hn_tok));
  408. hn_tok->begin = ntok;
  409. hn_tok->len = h->name.len;
  410. ntok = rspamd_mempool_ftokdup (task->task_pool, &h->value);
  411. hv_tok = rspamd_mempool_alloc (task->task_pool, sizeof (*hv_tok));
  412. hv_tok->begin = ntok;
  413. hv_tok->len = h->value.len;
  414. switch (*hn_tok->begin) {
  415. case 'd':
  416. case 'D':
  417. IF_HEADER (DELIVER_TO_HEADER) {
  418. task->deliver_to = rspamd_protocol_escape_braces (task, hv_tok);
  419. msg_debug_protocol ("read deliver-to header, value: %s",
  420. task->deliver_to);
  421. }
  422. else {
  423. msg_debug_protocol ("wrong header: %T", hn_tok);
  424. }
  425. break;
  426. case 'h':
  427. case 'H':
  428. IF_HEADER (HELO_HEADER) {
  429. task->helo = rspamd_mempool_ftokdup (task->task_pool, hv_tok);
  430. msg_debug_protocol ("read helo header, value: %s", task->helo);
  431. }
  432. IF_HEADER (HOSTNAME_HEADER) {
  433. task->hostname = rspamd_mempool_ftokdup (task->task_pool,
  434. hv_tok);
  435. msg_debug_protocol ("read hostname header, value: %s", task->hostname);
  436. }
  437. break;
  438. case 'f':
  439. case 'F':
  440. IF_HEADER (FROM_HEADER) {
  441. task->from_envelope = rspamd_email_address_from_smtp (
  442. hv_tok->begin,
  443. hv_tok->len);
  444. msg_debug_protocol ("read from header, value: %T", hv_tok);
  445. if (!task->from_envelope) {
  446. msg_err_protocol ("bad from header: '%T'", hv_tok);
  447. task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS;
  448. }
  449. }
  450. IF_HEADER (FILENAME_HEADER) {
  451. task->msg.fpath = rspamd_mempool_ftokdup (task->task_pool,
  452. hv_tok);
  453. msg_debug_protocol ("read filename header, value: %s", task->msg.fpath);
  454. }
  455. IF_HEADER (FLAGS_HEADER) {
  456. msg_debug_protocol ("read flags header, value: %T", hv_tok);
  457. rspamd_protocol_process_flags (task, hv_tok);
  458. }
  459. break;
  460. case 'q':
  461. case 'Q':
  462. IF_HEADER (QUEUE_ID_HEADER) {
  463. task->queue_id = rspamd_mempool_ftokdup (task->task_pool,
  464. hv_tok);
  465. msg_debug_protocol ("read queue_id header, value: %s", task->queue_id);
  466. }
  467. else {
  468. msg_debug_protocol ("wrong header: %T", hn_tok);
  469. }
  470. break;
  471. case 'r':
  472. case 'R':
  473. IF_HEADER (RCPT_HEADER) {
  474. rspamd_protocol_process_recipients (task, hv_tok);
  475. msg_debug_protocol ("read rcpt header, value: %T", hv_tok);
  476. }
  477. IF_HEADER (RAW_DATA_HEADER) {
  478. srch.begin = "yes";
  479. srch.len = 3;
  480. msg_debug_protocol ("read raw data header, value: %T", hv_tok);
  481. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  482. task->flags &= ~RSPAMD_TASK_FLAG_MIME;
  483. msg_debug_protocol ("disable mime parsing");
  484. }
  485. }
  486. break;
  487. case 'i':
  488. case 'I':
  489. IF_HEADER (IP_ADDR_HEADER) {
  490. if (!rspamd_parse_inet_address (&task->from_addr,
  491. hv_tok->begin, hv_tok->len,
  492. RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
  493. msg_err_protocol ("bad ip header: '%T'", hv_tok);
  494. }
  495. else {
  496. msg_debug_protocol ("read IP header, value: %T", hv_tok);
  497. has_ip = TRUE;
  498. }
  499. }
  500. else {
  501. msg_debug_protocol ("wrong header: %T", hn_tok);
  502. }
  503. break;
  504. case 'p':
  505. case 'P':
  506. IF_HEADER (PASS_HEADER) {
  507. srch.begin = "all";
  508. srch.len = 3;
  509. msg_debug_protocol ("read pass header, value: %T", hv_tok);
  510. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  511. task->flags |= RSPAMD_TASK_FLAG_PASS_ALL;
  512. msg_debug_protocol ("pass all filters");
  513. }
  514. }
  515. IF_HEADER (PROFILE_HEADER) {
  516. msg_debug_protocol ("read profile header, value: %T", hv_tok);
  517. task->flags |= RSPAMD_TASK_FLAG_PROFILE;
  518. }
  519. break;
  520. case 's':
  521. case 'S':
  522. IF_HEADER (SETTINGS_ID_HEADER) {
  523. msg_debug_protocol ("read settings-id header, value: %T", hv_tok);
  524. task->settings_elt = rspamd_config_find_settings_name_ref (
  525. task->cfg, hv_tok->begin, hv_tok->len);
  526. if (task->settings_elt == NULL) {
  527. GString *known_ids = g_string_new (NULL);
  528. struct rspamd_config_settings_elt *cur;
  529. DL_FOREACH (task->cfg->setting_ids, cur) {
  530. rspamd_printf_gstring (known_ids, "%s(%ud);",
  531. cur->name, cur->id);
  532. }
  533. msg_warn_protocol ("unknown settings id: %T(%d); known_ids: %v",
  534. hv_tok,
  535. rspamd_config_name_to_id (hv_tok->begin, hv_tok->len),
  536. known_ids);
  537. g_string_free (known_ids, TRUE);
  538. }
  539. else {
  540. msg_debug_protocol ("applied settings id %T -> %ud", hv_tok,
  541. task->settings_elt->id);
  542. }
  543. }
  544. IF_HEADER (SETTINGS_HEADER) {
  545. msg_debug_protocol ("read settings header, value: %T", hv_tok);
  546. seen_settings_header = TRUE;
  547. }
  548. break;
  549. case 'u':
  550. case 'U':
  551. IF_HEADER (USER_HEADER) {
  552. /*
  553. * We must ignore User header in case of spamc, as SA has
  554. * different meaning of this header
  555. */
  556. msg_debug_protocol ("read user header, value: %T", hv_tok);
  557. if (!RSPAMD_TASK_IS_SPAMC (task)) {
  558. task->user = rspamd_mempool_ftokdup (task->task_pool,
  559. hv_tok);
  560. }
  561. else {
  562. msg_info_protocol ("ignore user header: legacy SA protocol");
  563. }
  564. }
  565. IF_HEADER (URLS_HEADER) {
  566. msg_debug_protocol ("read urls header, value: %T", hv_tok);
  567. srch.begin = "extended";
  568. srch.len = 8;
  569. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  570. task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS;
  571. msg_debug_protocol ("extended urls information");
  572. }
  573. /* TODO: add more formats there */
  574. }
  575. IF_HEADER (USER_AGENT_HEADER) {
  576. msg_debug_protocol ("read user-agent header, value: %T", hv_tok);
  577. if (hv_tok->len == 6 &&
  578. rspamd_lc_cmp (hv_tok->begin, "rspamc", 6) == 0) {
  579. task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_LOCAL_CLIENT;
  580. }
  581. }
  582. break;
  583. case 'l':
  584. case 'L':
  585. IF_HEADER (NO_LOG_HEADER) {
  586. msg_debug_protocol ("read log header, value: %T", hv_tok);
  587. srch.begin = "no";
  588. srch.len = 2;
  589. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  590. task->flags |= RSPAMD_TASK_FLAG_NO_LOG;
  591. }
  592. }
  593. break;
  594. case 'm':
  595. case 'M':
  596. IF_HEADER (MLEN_HEADER) {
  597. msg_debug_protocol ("read message length header, value: %T",
  598. hv_tok);
  599. task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_HAS_CONTROL;
  600. }
  601. IF_HEADER (MTA_TAG_HEADER) {
  602. gchar *mta_tag;
  603. mta_tag = rspamd_mempool_ftokdup (task->task_pool, hv_tok);
  604. rspamd_mempool_set_variable (task->task_pool,
  605. RSPAMD_MEMPOOL_MTA_TAG,
  606. mta_tag, NULL);
  607. msg_debug_protocol ("read MTA-Tag header, value: %s", mta_tag);
  608. }
  609. IF_HEADER (MTA_NAME_HEADER) {
  610. gchar *mta_name;
  611. mta_name = rspamd_mempool_ftokdup (task->task_pool, hv_tok);
  612. rspamd_mempool_set_variable (task->task_pool,
  613. RSPAMD_MEMPOOL_MTA_NAME,
  614. mta_name, NULL);
  615. msg_debug_protocol ("read MTA-Name header, value: %s", mta_name);
  616. }
  617. IF_HEADER (MILTER_HEADER) {
  618. task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_MILTER;
  619. msg_debug_protocol ("read Milter header, value: %T", hv_tok);
  620. }
  621. break;
  622. case 't':
  623. case 'T':
  624. IF_HEADER (TLS_CIPHER_HEADER) {
  625. task->flags |= RSPAMD_TASK_FLAG_SSL;
  626. msg_debug_protocol ("read TLS cipher header, value: %T", hv_tok);
  627. }
  628. break;
  629. default:
  630. msg_debug_protocol ("generic header: %T", hn_tok);
  631. break;
  632. }
  633. rspamd_task_add_request_header (task, hn_tok, hv_tok);
  634. }
  635. }); /* End of kh_foreach_value */
  636. if (seen_settings_header && task->settings_elt) {
  637. msg_warn_task ("ignore settings id %s as settings header is also presented",
  638. task->settings_elt->name);
  639. REF_RELEASE (task->settings_elt);
  640. task->settings_elt = NULL;
  641. }
  642. if (!has_ip) {
  643. task->flags |= RSPAMD_TASK_FLAG_NO_IP;
  644. }
  645. return TRUE;
  646. }
  647. #define BOOL_TO_FLAG(val, flags, flag) do { \
  648. if ((val)) (flags) |= (flag); \
  649. else (flags) &= ~(flag); \
  650. } while(0)
  651. gboolean
  652. rspamd_protocol_parse_task_flags (rspamd_mempool_t *pool,
  653. const ucl_object_t *obj,
  654. gpointer ud,
  655. struct rspamd_rcl_section *section,
  656. GError **err)
  657. {
  658. struct rspamd_rcl_struct_parser *pd = ud;
  659. gint *target;
  660. const gchar *key;
  661. gboolean value;
  662. target = (gint *)(((gchar *)pd->user_struct) + pd->offset);
  663. key = ucl_object_key (obj);
  664. value = ucl_object_toboolean (obj);
  665. if (key != NULL) {
  666. if (g_ascii_strcasecmp (key, "pass_all") == 0) {
  667. BOOL_TO_FLAG (value, *target, RSPAMD_TASK_FLAG_PASS_ALL);
  668. }
  669. else if (g_ascii_strcasecmp (key, "no_log") == 0) {
  670. BOOL_TO_FLAG (value, *target, RSPAMD_TASK_FLAG_NO_LOG);
  671. }
  672. }
  673. return TRUE;
  674. }
  675. static struct rspamd_rcl_section *control_parser = NULL;
  676. static void
  677. rspamd_protocol_control_parser_init (void)
  678. {
  679. struct rspamd_rcl_section *sub;
  680. if (control_parser == NULL) {
  681. sub = rspamd_rcl_add_section (&control_parser,
  682. "*",
  683. NULL,
  684. NULL,
  685. UCL_OBJECT,
  686. FALSE,
  687. TRUE);
  688. /* Default handlers */
  689. rspamd_rcl_add_default_handler (sub,
  690. "ip",
  691. rspamd_rcl_parse_struct_addr,
  692. G_STRUCT_OFFSET (struct rspamd_task, from_addr),
  693. 0,
  694. NULL);
  695. rspamd_rcl_add_default_handler (sub,
  696. "from",
  697. rspamd_rcl_parse_struct_mime_addr,
  698. G_STRUCT_OFFSET (struct rspamd_task, from_envelope),
  699. 0,
  700. NULL);
  701. rspamd_rcl_add_default_handler (sub,
  702. "rcpt",
  703. rspamd_rcl_parse_struct_mime_addr,
  704. G_STRUCT_OFFSET (struct rspamd_task, rcpt_envelope),
  705. 0,
  706. NULL);
  707. rspamd_rcl_add_default_handler (sub,
  708. "helo",
  709. rspamd_rcl_parse_struct_string,
  710. G_STRUCT_OFFSET (struct rspamd_task, helo),
  711. 0,
  712. NULL);
  713. rspamd_rcl_add_default_handler (sub,
  714. "user",
  715. rspamd_rcl_parse_struct_string,
  716. G_STRUCT_OFFSET (struct rspamd_task, user),
  717. 0,
  718. NULL);
  719. rspamd_rcl_add_default_handler (sub,
  720. "pass_all",
  721. rspamd_protocol_parse_task_flags,
  722. G_STRUCT_OFFSET (struct rspamd_task, flags),
  723. 0,
  724. NULL);
  725. rspamd_rcl_add_default_handler (sub,
  726. "json",
  727. rspamd_protocol_parse_task_flags,
  728. G_STRUCT_OFFSET (struct rspamd_task, flags),
  729. 0,
  730. NULL);
  731. }
  732. }
  733. gboolean
  734. rspamd_protocol_handle_control (struct rspamd_task *task,
  735. const ucl_object_t *control)
  736. {
  737. GError *err = NULL;
  738. rspamd_protocol_control_parser_init ();
  739. if (!rspamd_rcl_parse (control_parser, task->cfg, task, task->task_pool,
  740. control, &err)) {
  741. msg_warn_protocol ("cannot parse control block: %e", err);
  742. g_error_free (err);
  743. return FALSE;
  744. }
  745. return TRUE;
  746. }
  747. gboolean
  748. rspamd_protocol_handle_request (struct rspamd_task *task,
  749. struct rspamd_http_message *msg)
  750. {
  751. gboolean ret = TRUE;
  752. if (msg->method == HTTP_SYMBOLS) {
  753. msg_debug_protocol ("got legacy SYMBOLS method, enable rspamc protocol workaround");
  754. task->cmd = CMD_CHECK_RSPAMC;
  755. }
  756. else if (msg->method == HTTP_CHECK) {
  757. msg_debug_protocol ("got legacy CHECK method, enable rspamc protocol workaround");
  758. task->cmd = CMD_CHECK_RSPAMC;
  759. }
  760. else {
  761. ret = rspamd_protocol_handle_url (task, msg);
  762. }
  763. if (msg->flags & RSPAMD_HTTP_FLAG_SPAMC) {
  764. msg_debug_protocol ("got legacy SA input, enable spamc protocol workaround");
  765. task->cmd = CMD_CHECK_SPAMC;
  766. }
  767. return ret;
  768. }
  769. /* Structure for writing tree data */
  770. struct tree_cb_data {
  771. ucl_object_t *top;
  772. khash_t (rspamd_url_host_hash) *seen;
  773. struct rspamd_task *task;
  774. };
  775. static ucl_object_t *
  776. rspamd_protocol_extended_url (struct rspamd_task *task,
  777. struct rspamd_url *url,
  778. const gchar *encoded, gsize enclen)
  779. {
  780. ucl_object_t *obj, *elt;
  781. obj = ucl_object_typed_new (UCL_OBJECT);
  782. elt = ucl_object_fromstring_common (encoded, enclen, 0);
  783. ucl_object_insert_key (obj, elt, "url", 0, false);
  784. if (url->tldlen > 0) {
  785. elt = ucl_object_fromstring_common (rspamd_url_tld_unsafe (url),
  786. url->tldlen, 0);
  787. ucl_object_insert_key (obj, elt, "tld", 0, false);
  788. }
  789. if (url->hostlen > 0) {
  790. elt = ucl_object_fromstring_common (rspamd_url_host_unsafe (url),
  791. url->hostlen, 0);
  792. ucl_object_insert_key (obj, elt, "host", 0, false);
  793. }
  794. elt = ucl_object_frombool (url->flags & RSPAMD_URL_FLAG_PHISHED);
  795. ucl_object_insert_key (obj, elt, "phished", 0, false);
  796. elt = ucl_object_frombool (url->flags & RSPAMD_URL_FLAG_REDIRECTED);
  797. ucl_object_insert_key (obj, elt, "redirected", 0, false);
  798. if (url->phished_url) {
  799. encoded = rspamd_url_encode (url->phished_url, &enclen, task->task_pool);
  800. elt = rspamd_protocol_extended_url (task, url->phished_url, encoded,
  801. enclen);
  802. ucl_object_insert_key (obj, elt, "orig_url", 0, false);
  803. }
  804. return obj;
  805. }
  806. /*
  807. * Callback for writing urls
  808. */
  809. static void
  810. urls_protocol_cb (struct rspamd_url *url, struct tree_cb_data *cb)
  811. {
  812. ucl_object_t *obj;
  813. struct rspamd_task *task = cb->task;
  814. const gchar *user_field = "unknown", *encoded = NULL;
  815. gboolean has_user = FALSE;
  816. guint len = 0;
  817. gsize enclen = 0;
  818. if (!(task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS)) {
  819. if (url->hostlen > 0) {
  820. if (rspamd_url_host_set_has (cb->seen, url)) {
  821. return;
  822. }
  823. goffset err_offset;
  824. if ((err_offset = rspamd_fast_utf8_validate (rspamd_url_host_unsafe (url),
  825. url->hostlen)) == 0) {
  826. obj = ucl_object_fromstring_common (rspamd_url_host_unsafe (url),
  827. url->hostlen, 0);
  828. }
  829. else {
  830. obj = ucl_object_fromstring_common (rspamd_url_host_unsafe (url),
  831. err_offset - 1, 0);
  832. }
  833. }
  834. else {
  835. return;
  836. }
  837. rspamd_url_host_set_add (cb->seen, url);
  838. }
  839. else {
  840. encoded = rspamd_url_encode (url, &enclen, task->task_pool);
  841. obj = rspamd_protocol_extended_url (task, url, encoded, enclen);
  842. }
  843. ucl_array_append (cb->top, obj);
  844. if (cb->task->cfg->log_urls) {
  845. if (task->user) {
  846. user_field = task->user;
  847. len = strlen (task->user);
  848. has_user = TRUE;
  849. }
  850. else if (task->from_envelope) {
  851. user_field = task->from_envelope->addr;
  852. len = task->from_envelope->addr_len;
  853. }
  854. if (!encoded) {
  855. encoded = rspamd_url_encode (url, &enclen, task->task_pool);
  856. }
  857. msg_notice_task_encrypted ("<%s> %s: %*s; ip: %s; URL: %*s",
  858. MESSAGE_FIELD_CHECK (task, message_id),
  859. has_user ? "user" : "from",
  860. len, user_field,
  861. rspamd_inet_address_to_string (task->from_addr),
  862. (gint)enclen, encoded);
  863. }
  864. }
  865. static ucl_object_t *
  866. rspamd_urls_tree_ucl (khash_t (rspamd_url_hash) *set,
  867. struct rspamd_task *task)
  868. {
  869. struct tree_cb_data cb;
  870. ucl_object_t *obj;
  871. struct rspamd_url *u;
  872. obj = ucl_object_typed_new (UCL_ARRAY);
  873. cb.top = obj;
  874. cb.task = task;
  875. cb.seen = kh_init (rspamd_url_host_hash);
  876. kh_foreach_key (set, u, {
  877. if (!(u->protocol & PROTOCOL_MAILTO)) {
  878. urls_protocol_cb (u, &cb);
  879. }
  880. });
  881. kh_destroy (rspamd_url_host_hash, cb.seen);
  882. return obj;
  883. }
  884. static void
  885. emails_protocol_cb (struct rspamd_url *url, struct tree_cb_data *cb)
  886. {
  887. ucl_object_t *obj;
  888. if (url->userlen > 0 && url->hostlen > 0) {
  889. obj = ucl_object_fromlstring (rspamd_url_user_unsafe (url),
  890. url->userlen + url->hostlen + 1);
  891. ucl_array_append (cb->top, obj);
  892. }
  893. }
  894. static ucl_object_t *
  895. rspamd_emails_tree_ucl (khash_t (rspamd_url_hash) *set,
  896. struct rspamd_task *task)
  897. {
  898. struct tree_cb_data cb;
  899. ucl_object_t *obj;
  900. struct rspamd_url *u;
  901. obj = ucl_object_typed_new (UCL_ARRAY);
  902. cb.top = obj;
  903. cb.task = task;
  904. kh_foreach_key (set, u, {
  905. if ((u->protocol & PROTOCOL_MAILTO)) {
  906. emails_protocol_cb (u, &cb);
  907. }
  908. });
  909. return obj;
  910. }
  911. /* Write new subject */
  912. static const gchar *
  913. rspamd_protocol_rewrite_subject (struct rspamd_task *task)
  914. {
  915. GString *subj_buf;
  916. gchar *res;
  917. const gchar *s, *c, *p;
  918. gsize slen = 0;
  919. c = rspamd_mempool_get_variable (task->task_pool, "metric_subject");
  920. if (c == NULL) {
  921. c = task->cfg->subject;
  922. }
  923. if (c == NULL) {
  924. c = SPAM_SUBJECT;
  925. }
  926. p = c;
  927. s = MESSAGE_FIELD_CHECK (task, subject);
  928. if (s) {
  929. slen = strlen (s);
  930. }
  931. subj_buf = g_string_sized_new (strlen (c) + slen);
  932. while (*p) {
  933. if (*p == '%') {
  934. switch (p[1]) {
  935. case 's':
  936. g_string_append_len (subj_buf, c, p - c);
  937. if (s) {
  938. g_string_append_len (subj_buf, s, slen);
  939. }
  940. c = p + 2;
  941. p += 2;
  942. break;
  943. case 'd':
  944. g_string_append_len (subj_buf, c, p - c);
  945. rspamd_printf_gstring (subj_buf, "%.2f", task->result->score);
  946. c = p + 2;
  947. p += 2;
  948. break;
  949. case '%':
  950. g_string_append_len (subj_buf, c, p - c);
  951. g_string_append_c (subj_buf, '%');
  952. c = p + 2;
  953. p += 2;
  954. default:
  955. p ++; /* Just % something unknown */
  956. break;
  957. }
  958. }
  959. else {
  960. p++;
  961. }
  962. }
  963. if (p > c) {
  964. g_string_append_len (subj_buf, c, p - c);
  965. }
  966. res = rspamd_mime_header_encode (subj_buf->str, subj_buf->len);
  967. rspamd_mempool_add_destructor (task->task_pool,
  968. (rspamd_mempool_destruct_t)g_free,
  969. res);
  970. g_string_free (subj_buf, TRUE);
  971. return res;
  972. }
  973. static ucl_object_t *
  974. rspamd_metric_symbol_ucl (struct rspamd_task *task, struct rspamd_symbol_result *sym)
  975. {
  976. ucl_object_t *obj = NULL, *ar;
  977. const gchar *description = NULL;
  978. struct rspamd_symbol_option *opt;
  979. if (sym->sym != NULL) {
  980. description = sym->sym->description;
  981. }
  982. obj = ucl_object_typed_new (UCL_OBJECT);
  983. ucl_object_insert_key (obj, ucl_object_fromstring (
  984. sym->name), "name", 0, false);
  985. ucl_object_insert_key (obj, ucl_object_fromdouble (
  986. sym->score), "score", 0, false);
  987. if (task->cmd == CMD_CHECK_V2) {
  988. if (sym->sym) {
  989. ucl_object_insert_key (obj, ucl_object_fromdouble (
  990. sym->sym->score), "metric_score", 0, false);
  991. }
  992. else {
  993. ucl_object_insert_key (obj, ucl_object_fromdouble (0.0),
  994. "metric_score", 0, false);
  995. }
  996. }
  997. if (description) {
  998. ucl_object_insert_key (obj, ucl_object_fromstring (description),
  999. "description", 0, false);
  1000. }
  1001. if (sym->options != NULL) {
  1002. ar = ucl_object_typed_new (UCL_ARRAY);
  1003. DL_FOREACH (sym->opts_head, opt) {
  1004. ucl_array_append (ar, ucl_object_fromstring_common (opt->option,
  1005. opt->optlen, 0));
  1006. }
  1007. ucl_object_insert_key (obj, ar, "options", 0, false);
  1008. }
  1009. return obj;
  1010. }
  1011. static ucl_object_t *
  1012. rspamd_metric_group_ucl (struct rspamd_task *task,
  1013. struct rspamd_symbols_group *gr, gdouble score)
  1014. {
  1015. ucl_object_t *obj = NULL;
  1016. obj = ucl_object_typed_new (UCL_OBJECT);
  1017. ucl_object_insert_key (obj, ucl_object_fromdouble (score),
  1018. "score", 0, false);
  1019. if (gr->description) {
  1020. ucl_object_insert_key (obj, ucl_object_fromstring (gr->description),
  1021. "description", 0, false);
  1022. }
  1023. return obj;
  1024. }
  1025. static ucl_object_t *
  1026. rspamd_scan_result_ucl (struct rspamd_task *task,
  1027. struct rspamd_scan_result *mres, ucl_object_t *top)
  1028. {
  1029. struct rspamd_symbol_result *sym;
  1030. gboolean is_spam;
  1031. struct rspamd_action *action;
  1032. ucl_object_t *obj = NULL, *sobj;
  1033. const gchar *subject;
  1034. struct rspamd_passthrough_result *pr = NULL;
  1035. action = rspamd_check_action_metric (task, &pr, NULL);
  1036. is_spam = !(action->flags & RSPAMD_ACTION_HAM);
  1037. if (task->cmd == CMD_CHECK) {
  1038. obj = ucl_object_typed_new (UCL_OBJECT);
  1039. ucl_object_insert_key (obj,
  1040. ucl_object_frombool (is_spam),
  1041. "is_spam", 0, false);
  1042. }
  1043. else {
  1044. obj = top;
  1045. }
  1046. if (pr && pr->message && !(pr->flags & RSPAMD_PASSTHROUGH_NO_SMTP_MESSAGE)) {
  1047. /* Add smtp message if it does not exists: see #3269 for details */
  1048. if (ucl_object_lookup (task->messages, "smtp_message") == NULL) {
  1049. ucl_object_insert_key (task->messages,
  1050. ucl_object_fromstring_common (pr->message, 0, UCL_STRING_RAW),
  1051. "smtp_message", 0,
  1052. false);
  1053. }
  1054. }
  1055. ucl_object_insert_key (obj,
  1056. ucl_object_frombool (RSPAMD_TASK_IS_SKIPPED (task)),
  1057. "is_skipped", 0, false);
  1058. if (!isnan (mres->score)) {
  1059. ucl_object_insert_key (obj, ucl_object_fromdouble (mres->score),
  1060. "score", 0, false);
  1061. } else {
  1062. ucl_object_insert_key (obj,
  1063. ucl_object_fromdouble (0.0), "score", 0, false);
  1064. }
  1065. ucl_object_insert_key (obj,
  1066. ucl_object_fromdouble (rspamd_task_get_required_score (task, mres)),
  1067. "required_score", 0, false);
  1068. ucl_object_insert_key (obj,
  1069. ucl_object_fromstring (action->name),
  1070. "action", 0, false);
  1071. if (action->action_type == METRIC_ACTION_REWRITE_SUBJECT) {
  1072. subject = rspamd_protocol_rewrite_subject (task);
  1073. if (subject) {
  1074. ucl_object_insert_key (obj, ucl_object_fromstring (subject),
  1075. "subject", 0, false);
  1076. }
  1077. }
  1078. if (action->flags & RSPAMD_ACTION_MILTER) {
  1079. /* Treat milter action specially */
  1080. if (action->action_type == METRIC_ACTION_DISCARD) {
  1081. ucl_object_insert_key (obj, ucl_object_fromstring ("discard"),
  1082. "reject", 0, false);
  1083. }
  1084. else if (action->action_type == METRIC_ACTION_QUARANTINE) {
  1085. ucl_object_insert_key (obj, ucl_object_fromstring ("quarantine"),
  1086. "reject", 0, false);
  1087. }
  1088. }
  1089. /* Now handle symbols */
  1090. if (task->cmd != CMD_CHECK) {
  1091. /* For checkv2 we insert symbols as a separate object */
  1092. obj = ucl_object_typed_new (UCL_OBJECT);
  1093. }
  1094. kh_foreach_value (mres->symbols, sym, {
  1095. if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
  1096. sobj = rspamd_metric_symbol_ucl (task, sym);
  1097. ucl_object_insert_key (obj, sobj, sym->name, 0, false);
  1098. }
  1099. })
  1100. if (task->cmd != CMD_CHECK) {
  1101. /* For checkv2 we insert symbols as a separate object */
  1102. ucl_object_insert_key (top, obj, "symbols", 0, false);
  1103. }
  1104. else {
  1105. /* For legacy check we just insert it as "default" all together */
  1106. ucl_object_insert_key (top, obj, DEFAULT_METRIC, 0, false);
  1107. }
  1108. /* Handle groups if needed */
  1109. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_GROUPS) {
  1110. struct rspamd_symbols_group *gr;
  1111. gdouble gr_score;
  1112. obj = ucl_object_typed_new (UCL_OBJECT);
  1113. ucl_object_reserve (obj, kh_size (mres->sym_groups));
  1114. kh_foreach (mres->sym_groups, gr, gr_score,{
  1115. if (task->cfg->public_groups_only &&
  1116. !(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)) {
  1117. continue;
  1118. }
  1119. sobj = rspamd_metric_group_ucl (task, gr, gr_score);
  1120. ucl_object_insert_key (obj, sobj, gr->name, 0, false);
  1121. });
  1122. ucl_object_insert_key (top, obj, "groups", 0, false);
  1123. }
  1124. return obj;
  1125. }
  1126. void
  1127. rspamd_ucl_torspamc_output (const ucl_object_t *top,
  1128. rspamd_fstring_t **out)
  1129. {
  1130. const ucl_object_t *symbols, *score,
  1131. *required_score, *is_spam, *elt, *cur;
  1132. ucl_object_iter_t iter = NULL;
  1133. score = ucl_object_lookup (top, "score");
  1134. required_score = ucl_object_lookup (top, "required_score");
  1135. is_spam = ucl_object_lookup (top, "is_spam");
  1136. rspamd_printf_fstring (out,
  1137. "Metric: default; %s; %.2f / %.2f / 0.0\r\n",
  1138. ucl_object_toboolean (is_spam) ? "True" : "False",
  1139. ucl_object_todouble (score),
  1140. ucl_object_todouble (required_score));
  1141. elt = ucl_object_lookup (top, "action");
  1142. if (elt != NULL) {
  1143. rspamd_printf_fstring (out, "Action: %s\r\n",
  1144. ucl_object_tostring (elt));
  1145. }
  1146. elt = ucl_object_lookup (top, "subject");
  1147. if (elt != NULL) {
  1148. rspamd_printf_fstring (out, "Subject: %s\r\n",
  1149. ucl_object_tostring (elt));
  1150. }
  1151. symbols = ucl_object_lookup (top, "symbols");
  1152. if (symbols != NULL) {
  1153. iter = NULL;
  1154. while ((elt = ucl_object_iterate (symbols, &iter, true)) != NULL) {
  1155. if (elt->type == UCL_OBJECT) {
  1156. const ucl_object_t *sym_score;
  1157. sym_score = ucl_object_lookup (elt, "score");
  1158. rspamd_printf_fstring (out, "Symbol: %s(%.2f)\r\n",
  1159. ucl_object_key (elt),
  1160. ucl_object_todouble (sym_score));
  1161. }
  1162. }
  1163. }
  1164. elt = ucl_object_lookup (top, "messages");
  1165. if (elt != NULL) {
  1166. iter = NULL;
  1167. while ((cur = ucl_object_iterate (elt, &iter, true)) != NULL) {
  1168. if (cur->type == UCL_STRING) {
  1169. rspamd_printf_fstring (out, "Message: %s\r\n",
  1170. ucl_object_tostring (cur));
  1171. }
  1172. }
  1173. }
  1174. elt = ucl_object_lookup (top, "message-id");
  1175. if (elt != NULL) {
  1176. rspamd_printf_fstring (out, "Message-ID: %s\r\n",
  1177. ucl_object_tostring (elt));
  1178. }
  1179. }
  1180. void
  1181. rspamd_ucl_tospamc_output (const ucl_object_t *top,
  1182. rspamd_fstring_t **out)
  1183. {
  1184. const ucl_object_t *symbols, *score,
  1185. *required_score, *is_spam, *elt;
  1186. ucl_object_iter_t iter = NULL;
  1187. rspamd_fstring_t *f;
  1188. score = ucl_object_lookup (top, "score");
  1189. required_score = ucl_object_lookup (top, "required_score");
  1190. is_spam = ucl_object_lookup (top, "is_spam");
  1191. rspamd_printf_fstring (out,
  1192. "Spam: %s ; %.2f / %.2f\r\n\r\n",
  1193. ucl_object_toboolean (is_spam) ? "True" : "False",
  1194. ucl_object_todouble (score),
  1195. ucl_object_todouble (required_score));
  1196. symbols = ucl_object_lookup (top, "symbols");
  1197. if (symbols != NULL) {
  1198. while ((elt = ucl_object_iterate (symbols, &iter, true)) != NULL) {
  1199. if (elt->type == UCL_OBJECT) {
  1200. rspamd_printf_fstring (out, "%s,",
  1201. ucl_object_key (elt));
  1202. }
  1203. }
  1204. /* Ugly hack, but the whole spamc is ugly */
  1205. f = *out;
  1206. if (f->str[f->len - 1] == ',') {
  1207. f->len --;
  1208. *out = rspamd_fstring_append (*out, CRLF, 2);
  1209. }
  1210. }
  1211. }
  1212. static void
  1213. rspamd_protocol_output_profiling (struct rspamd_task *task,
  1214. ucl_object_t *top)
  1215. {
  1216. GHashTable *tbl;
  1217. GHashTableIter it;
  1218. gpointer k, v;
  1219. ucl_object_t *prof;
  1220. gdouble val;
  1221. prof = ucl_object_typed_new (UCL_OBJECT);
  1222. tbl = rspamd_mempool_get_variable (task->task_pool, "profile");
  1223. if (tbl) {
  1224. g_hash_table_iter_init (&it, tbl);
  1225. while (g_hash_table_iter_next (&it, &k, &v)) {
  1226. val = *(gdouble *)v;
  1227. ucl_object_insert_key (prof, ucl_object_fromdouble (val),
  1228. (const char *)k, 0, false);
  1229. }
  1230. }
  1231. ucl_object_insert_key (top, prof, "profile", 0, false);
  1232. }
  1233. ucl_object_t *
  1234. rspamd_protocol_write_ucl (struct rspamd_task *task,
  1235. enum rspamd_protocol_flags flags)
  1236. {
  1237. ucl_object_t *top = NULL;
  1238. GString *dkim_sig;
  1239. GList *dkim_sigs;
  1240. const ucl_object_t *milter_reply;
  1241. rspamd_task_set_finish_time (task);
  1242. top = ucl_object_typed_new (UCL_OBJECT);
  1243. rspamd_mempool_add_destructor (task->task_pool,
  1244. (rspamd_mempool_destruct_t)ucl_object_unref, top);
  1245. if (flags & RSPAMD_PROTOCOL_METRICS) {
  1246. rspamd_scan_result_ucl (task, task->result, top);
  1247. }
  1248. if (flags & RSPAMD_PROTOCOL_MESSAGES) {
  1249. if (G_UNLIKELY (task->cfg->compat_messages)) {
  1250. const ucl_object_t *cur;
  1251. ucl_object_t *msg_object;
  1252. ucl_object_iter_t iter = NULL;
  1253. msg_object = ucl_object_typed_new (UCL_ARRAY);
  1254. while ((cur = ucl_object_iterate (task->messages, &iter, true)) != NULL) {
  1255. if (cur->type == UCL_STRING) {
  1256. ucl_array_append (msg_object, ucl_object_ref (cur));
  1257. }
  1258. }
  1259. ucl_object_insert_key (top, msg_object, "messages", 0, false);
  1260. }
  1261. else {
  1262. ucl_object_insert_key (top, ucl_object_ref (task->messages),
  1263. "messages", 0, false);
  1264. }
  1265. }
  1266. if (flags & RSPAMD_PROTOCOL_URLS && task->message) {
  1267. if (kh_size (MESSAGE_FIELD (task, urls)) > 0) {
  1268. ucl_object_insert_key (top,
  1269. rspamd_urls_tree_ucl (MESSAGE_FIELD (task, urls), task),
  1270. "urls", 0, false);
  1271. ucl_object_insert_key (top,
  1272. rspamd_emails_tree_ucl (MESSAGE_FIELD (task, urls), task),
  1273. "emails", 0, false);
  1274. }
  1275. }
  1276. if (flags & RSPAMD_PROTOCOL_EXTRA) {
  1277. if (G_UNLIKELY (RSPAMD_TASK_IS_PROFILING (task))) {
  1278. rspamd_protocol_output_profiling (task, top);
  1279. }
  1280. }
  1281. if (flags & RSPAMD_PROTOCOL_BASIC) {
  1282. ucl_object_insert_key (top,
  1283. ucl_object_fromstring (MESSAGE_FIELD_CHECK (task, message_id)),
  1284. "message-id", 0, false);
  1285. ucl_object_insert_key (top,
  1286. ucl_object_fromdouble (task->time_real_finish - task->task_timestamp),
  1287. "time_real", 0, false);
  1288. }
  1289. if (flags & RSPAMD_PROTOCOL_DKIM) {
  1290. dkim_sigs = rspamd_mempool_get_variable (task->task_pool,
  1291. RSPAMD_MEMPOOL_DKIM_SIGNATURE);
  1292. if (dkim_sigs) {
  1293. if (dkim_sigs->next) {
  1294. /* Multiple DKIM signatures */
  1295. ucl_object_t *ar = ucl_object_typed_new (UCL_ARRAY);
  1296. for (; dkim_sigs != NULL; dkim_sigs = dkim_sigs->next) {
  1297. GString *folded_header;
  1298. dkim_sig = (GString *) dkim_sigs->data;
  1299. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER ||
  1300. !task->message) {
  1301. folded_header = rspamd_header_value_fold ("DKIM-Signature",
  1302. dkim_sig->str, 80, RSPAMD_TASK_NEWLINES_LF, NULL);
  1303. }
  1304. else {
  1305. folded_header = rspamd_header_value_fold ("DKIM-Signature",
  1306. dkim_sig->str, 80,
  1307. MESSAGE_FIELD (task, nlines_type),
  1308. NULL);
  1309. }
  1310. ucl_array_append (ar,
  1311. ucl_object_fromstring_common (folded_header->str,
  1312. folded_header->len, UCL_STRING_RAW));
  1313. g_string_free (folded_header, TRUE);
  1314. }
  1315. ucl_object_insert_key (top,
  1316. ar,
  1317. "dkim-signature", 0,
  1318. false);
  1319. }
  1320. else {
  1321. /* Single DKIM signature */
  1322. GString *folded_header;
  1323. dkim_sig = (GString *) dkim_sigs->data;
  1324. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) {
  1325. folded_header = rspamd_header_value_fold ("DKIM-Signature",
  1326. dkim_sig->str, 80, RSPAMD_TASK_NEWLINES_LF, NULL);
  1327. }
  1328. else {
  1329. folded_header = rspamd_header_value_fold ("DKIM-Signature",
  1330. dkim_sig->str, 80, MESSAGE_FIELD (task, nlines_type),
  1331. NULL);
  1332. }
  1333. ucl_object_insert_key (top,
  1334. ucl_object_fromstring_common (folded_header->str,
  1335. folded_header->len, UCL_STRING_RAW),
  1336. "dkim-signature", 0, false);
  1337. g_string_free (folded_header, TRUE);
  1338. }
  1339. }
  1340. }
  1341. if (flags & RSPAMD_PROTOCOL_RMILTER) {
  1342. milter_reply = rspamd_mempool_get_variable (task->task_pool,
  1343. RSPAMD_MEMPOOL_MILTER_REPLY);
  1344. if (milter_reply) {
  1345. if (task->cmd != CMD_CHECK) {
  1346. ucl_object_insert_key (top, ucl_object_ref (milter_reply),
  1347. "milter", 0, false);
  1348. }
  1349. else {
  1350. ucl_object_insert_key (top, ucl_object_ref (milter_reply),
  1351. "rmilter", 0, false);
  1352. }
  1353. }
  1354. }
  1355. return top;
  1356. }
  1357. void
  1358. rspamd_protocol_http_reply (struct rspamd_http_message *msg,
  1359. struct rspamd_task *task, ucl_object_t **pobj)
  1360. {
  1361. struct rspamd_scan_result *metric_res;
  1362. const struct rspamd_re_cache_stat *restat;
  1363. ucl_object_t *top = NULL;
  1364. rspamd_fstring_t *reply;
  1365. gint flags = RSPAMD_PROTOCOL_DEFAULT;
  1366. struct rspamd_action *action;
  1367. /* Removed in 2.0 */
  1368. #if 0
  1369. GHashTableIter hiter;
  1370. gpointer h, v;
  1371. /* Write custom headers */
  1372. g_hash_table_iter_init (&hiter, task->reply_headers);
  1373. while (g_hash_table_iter_next (&hiter, &h, &v)) {
  1374. rspamd_ftok_t *hn = h, *hv = v;
  1375. rspamd_http_message_add_header (msg, hn->begin, hv->begin);
  1376. }
  1377. #endif
  1378. flags |= RSPAMD_PROTOCOL_URLS;
  1379. top = rspamd_protocol_write_ucl (task, flags);
  1380. if (pobj) {
  1381. *pobj = top;
  1382. }
  1383. if (!(task->flags & RSPAMD_TASK_FLAG_NO_LOG)) {
  1384. rspamd_roll_history_update (task->worker->srv->history, task);
  1385. }
  1386. else {
  1387. msg_debug_protocol ("skip history update due to no log flag");
  1388. }
  1389. rspamd_task_write_log (task);
  1390. if (task->cfg->log_flags & RSPAMD_LOG_FLAG_RE_CACHE) {
  1391. restat = rspamd_re_cache_get_stat (task->re_rt);
  1392. g_assert (restat != NULL);
  1393. msg_notice_task (
  1394. "regexp statistics: %ud pcre regexps scanned, %ud regexps matched,"
  1395. " %ud regexps total, %ud regexps cached,"
  1396. " %HL scanned using pcre, %HL scanned total",
  1397. restat->regexp_checked,
  1398. restat->regexp_matched,
  1399. restat->regexp_total,
  1400. restat->regexp_fast_cached,
  1401. restat->bytes_scanned_pcre,
  1402. restat->bytes_scanned);
  1403. }
  1404. reply = rspamd_fstring_sized_new (1000);
  1405. if (msg->method < HTTP_SYMBOLS && !RSPAMD_TASK_IS_SPAMC (task)) {
  1406. msg_debug_protocol ("writing json reply");
  1407. rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply);
  1408. }
  1409. else {
  1410. if (RSPAMD_TASK_IS_SPAMC (task)) {
  1411. msg_debug_protocol ("writing spamc legacy reply to client");
  1412. rspamd_ucl_tospamc_output (top, &reply);
  1413. }
  1414. else {
  1415. msg_debug_protocol ("writing rspamc legacy reply to client");
  1416. rspamd_ucl_torspamc_output (top, &reply);
  1417. }
  1418. }
  1419. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK) {
  1420. /* Check if we need to insert a body block */
  1421. if (task->flags & RSPAMD_TASK_FLAG_MESSAGE_REWRITE) {
  1422. GString *hdr_offset = g_string_sized_new (30);
  1423. rspamd_printf_gstring (hdr_offset, "%z", RSPAMD_FSTRING_LEN (reply));
  1424. rspamd_http_message_add_header (msg, MESSAGE_OFFSET_HEADER,
  1425. hdr_offset->str);
  1426. msg_debug_protocol ("write body block at position %s",
  1427. hdr_offset->str);
  1428. g_string_free (hdr_offset, TRUE);
  1429. /* In case of milter, we append just body, otherwise - full message */
  1430. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) {
  1431. const gchar *start;
  1432. goffset len, hdr_off;
  1433. start = task->msg.begin;
  1434. len = task->msg.len;
  1435. hdr_off = MESSAGE_FIELD (task, raw_headers_content).len;
  1436. if (hdr_off < len) {
  1437. start += hdr_off;
  1438. len -= hdr_off;
  1439. /* The problem here is that we need not end of headers, we need
  1440. * start of body.
  1441. *
  1442. * Hence, we need to skip one \r\n till there is anything else in
  1443. * a line.
  1444. */
  1445. if (*start == '\r' && len > 0) {
  1446. start ++;
  1447. len --;
  1448. }
  1449. if (*start == '\n' && len > 0) {
  1450. start ++;
  1451. len --;
  1452. }
  1453. msg_debug_protocol ("milter version of body block size %d",
  1454. (int)len);
  1455. reply = rspamd_fstring_append (reply, start, len);
  1456. }
  1457. }
  1458. else {
  1459. msg_debug_protocol ("general version of body block size %d",
  1460. (int)task->msg.len);
  1461. reply = rspamd_fstring_append (reply,
  1462. task->msg.begin, task->msg.len);
  1463. }
  1464. }
  1465. }
  1466. if ((task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_COMPRESSED) &&
  1467. rspamd_libs_reset_compression (task->cfg->libs_ctx)) {
  1468. /* We can compress output */
  1469. ZSTD_inBuffer zin;
  1470. ZSTD_outBuffer zout;
  1471. ZSTD_CStream *zstream;
  1472. rspamd_fstring_t *compressed_reply;
  1473. gsize r;
  1474. zstream = task->cfg->libs_ctx->out_zstream;
  1475. compressed_reply = rspamd_fstring_sized_new (ZSTD_compressBound (reply->len));
  1476. zin.pos = 0;
  1477. zin.src = reply->str;
  1478. zin.size = reply->len;
  1479. zout.pos = 0;
  1480. zout.dst = compressed_reply->str;
  1481. zout.size = compressed_reply->allocated;
  1482. while (zin.pos < zin.size) {
  1483. r = ZSTD_compressStream (zstream, &zout, &zin);
  1484. if (ZSTD_isError (r)) {
  1485. msg_err_protocol ("cannot compress: %s", ZSTD_getErrorName (r));
  1486. rspamd_fstring_free (compressed_reply);
  1487. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1488. goto end;
  1489. }
  1490. }
  1491. ZSTD_flushStream (zstream, &zout);
  1492. r = ZSTD_endStream (zstream, &zout);
  1493. if (ZSTD_isError (r)) {
  1494. msg_err_protocol ("cannot finalize compress: %s", ZSTD_getErrorName (r));
  1495. rspamd_fstring_free (compressed_reply);
  1496. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1497. goto end;
  1498. }
  1499. msg_info_protocol ("writing compressed results: %z bytes before "
  1500. "%z bytes after", zin.pos, zout.pos);
  1501. compressed_reply->len = zout.pos;
  1502. rspamd_fstring_free (reply);
  1503. rspamd_http_message_set_body_from_fstring_steal (msg, compressed_reply);
  1504. rspamd_http_message_add_header (msg, COMPRESSION_HEADER, "zstd");
  1505. if (task->cfg->libs_ctx->out_dict &&
  1506. task->cfg->libs_ctx->out_dict->id != 0) {
  1507. gchar dict_str[32];
  1508. rspamd_snprintf (dict_str, sizeof (dict_str), "%ud",
  1509. task->cfg->libs_ctx->out_dict->id);
  1510. rspamd_http_message_add_header (msg, "Dictionary", dict_str);
  1511. }
  1512. }
  1513. else {
  1514. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1515. }
  1516. end:
  1517. if (!(task->flags & RSPAMD_TASK_FLAG_NO_STAT)) {
  1518. /* Update stat for default metric */
  1519. msg_debug_protocol ("skip stats update due to no_stat flag");
  1520. metric_res = task->result;
  1521. if (metric_res != NULL) {
  1522. action = rspamd_check_action_metric (task, NULL, NULL);
  1523. /* TODO: handle custom actions in stats */
  1524. if (action->action_type == METRIC_ACTION_SOFT_REJECT &&
  1525. (task->flags & RSPAMD_TASK_FLAG_GREYLISTED)) {
  1526. /* Set stat action to greylist to display greylisted messages */
  1527. #ifndef HAVE_ATOMIC_BUILTINS
  1528. task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST]++;
  1529. #else
  1530. __atomic_add_fetch (&task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST],
  1531. 1, __ATOMIC_RELEASE);
  1532. #endif
  1533. }
  1534. else if (action->action_type < METRIC_ACTION_MAX) {
  1535. #ifndef HAVE_ATOMIC_BUILTINS
  1536. task->worker->srv->stat->actions_stat[action->action_type]++;
  1537. #else
  1538. __atomic_add_fetch (&task->worker->srv->stat->actions_stat[action->action_type],
  1539. 1, __ATOMIC_RELEASE);
  1540. #endif
  1541. }
  1542. }
  1543. /* Increase counters */
  1544. #ifndef HAVE_ATOMIC_BUILTINS
  1545. task->worker->srv->stat->messages_scanned++;
  1546. #else
  1547. __atomic_add_fetch (&task->worker->srv->stat->messages_scanned,
  1548. 1, __ATOMIC_RELEASE);
  1549. #endif
  1550. }
  1551. }
  1552. void
  1553. rspamd_protocol_write_log_pipe (struct rspamd_task *task)
  1554. {
  1555. struct rspamd_worker_log_pipe *lp;
  1556. struct rspamd_protocol_log_message_sum *ls;
  1557. lua_State *L = task->cfg->lua_state;
  1558. struct rspamd_scan_result *mres;
  1559. struct rspamd_symbol_result *sym;
  1560. gint id, i;
  1561. guint32 n = 0, nextra = 0;
  1562. gsize sz;
  1563. GArray *extra;
  1564. struct rspamd_protocol_log_symbol_result er;
  1565. struct rspamd_task **ptask;
  1566. /* Get extra results from lua plugins */
  1567. extra = g_array_new (FALSE, FALSE, sizeof (er));
  1568. lua_getglobal (L, "rspamd_plugins");
  1569. if (lua_istable (L, -1)) {
  1570. lua_pushnil (L);
  1571. while (lua_next (L, -2)) {
  1572. if (lua_istable (L, -1)) {
  1573. lua_pushvalue (L, -2);
  1574. /* stack:
  1575. * -1: copy of key
  1576. * -2: value (module table)
  1577. * -3: key (module name)
  1578. * -4: global
  1579. */
  1580. lua_pushstring (L, "log_callback");
  1581. lua_gettable (L, -3);
  1582. /* stack:
  1583. * -1: func
  1584. * -2: copy of key
  1585. * -3: value (module table)
  1586. * -3: key (module name)
  1587. * -4: global
  1588. */
  1589. if (lua_isfunction (L, -1)) {
  1590. ptask = lua_newuserdata (L, sizeof (*ptask));
  1591. *ptask = task;
  1592. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1593. /* stack:
  1594. * -1: task
  1595. * -2: func
  1596. * -3: key copy
  1597. * -4: value (module table)
  1598. * -5: key (module name)
  1599. * -6: global
  1600. */
  1601. msg_debug_protocol ("calling for %s", lua_tostring (L, -3));
  1602. if (lua_pcall (L, 1, 1, 0) != 0) {
  1603. msg_info_protocol ("call to log callback %s failed: %s",
  1604. lua_tostring (L, -2), lua_tostring (L, -1));
  1605. lua_pop (L, 1);
  1606. /* stack:
  1607. * -1: key copy
  1608. * -2: value
  1609. * -3: key
  1610. */
  1611. }
  1612. else {
  1613. /* stack:
  1614. * -1: result
  1615. * -2: key copy
  1616. * -3: value
  1617. * -4: key
  1618. */
  1619. if (lua_istable (L, -1)) {
  1620. /* Another iteration */
  1621. lua_pushnil (L);
  1622. while (lua_next (L, -2)) {
  1623. /* stack:
  1624. * -1: value
  1625. * -2: key
  1626. * -3: result table (pcall)
  1627. * -4: key copy (parent)
  1628. * -5: value (parent)
  1629. * -6: key (parent)
  1630. */
  1631. if (lua_istable (L, -1)) {
  1632. er.id = 0;
  1633. er.score = 0.0;
  1634. lua_rawgeti (L, -1, 1);
  1635. if (lua_isnumber (L, -1)) {
  1636. er.id = lua_tonumber (L, -1);
  1637. }
  1638. lua_rawgeti (L, -2, 2);
  1639. if (lua_isnumber (L, -1)) {
  1640. er.score = lua_tonumber (L, -1);
  1641. }
  1642. /* stack:
  1643. * -1: value[2]
  1644. * -2: value[1]
  1645. * -3: values
  1646. * -4: key
  1647. * -5: result table (pcall)
  1648. * -6: key copy (parent)
  1649. * -7: value (parent)
  1650. * -8: key (parent)
  1651. */
  1652. lua_pop (L, 2); /* Values */
  1653. g_array_append_val (extra, er);
  1654. }
  1655. lua_pop (L, 1); /* Value for lua_next */
  1656. }
  1657. lua_pop (L, 1); /* Table result of pcall */
  1658. }
  1659. else {
  1660. msg_info_protocol ("call to log callback %s returned "
  1661. "wrong type: %s",
  1662. lua_tostring (L, -2),
  1663. lua_typename (L, lua_type (L, -1)));
  1664. lua_pop (L, 1); /* Returned error */
  1665. }
  1666. }
  1667. }
  1668. else {
  1669. lua_pop (L, 1);
  1670. /* stack:
  1671. * -1: key copy
  1672. * -2: value
  1673. * -3: key
  1674. */
  1675. }
  1676. }
  1677. lua_pop (L, 2); /* Top table + key copy */
  1678. }
  1679. lua_pop (L, 1); /* rspamd_plugins global */
  1680. }
  1681. else {
  1682. lua_pop (L, 1);
  1683. }
  1684. nextra = extra->len;
  1685. LL_FOREACH (task->cfg->log_pipes, lp) {
  1686. if (lp->fd != -1) {
  1687. switch (lp->type) {
  1688. case RSPAMD_LOG_PIPE_SYMBOLS:
  1689. mres = task->result;
  1690. if (mres) {
  1691. n = kh_size (mres->symbols);
  1692. sz = sizeof (*ls) +
  1693. sizeof (struct rspamd_protocol_log_symbol_result) *
  1694. (n + nextra);
  1695. ls = g_malloc0 (sz);
  1696. /* Handle settings id */
  1697. if (task->settings_elt) {
  1698. ls->settings_id = task->settings_elt->id;
  1699. }
  1700. else {
  1701. ls->settings_id = 0;
  1702. }
  1703. ls->score = mres->score;
  1704. ls->required_score = rspamd_task_get_required_score (task,
  1705. mres);
  1706. ls->nresults = n;
  1707. ls->nextra = nextra;
  1708. i = 0;
  1709. kh_foreach_value (mres->symbols, sym, {
  1710. id = rspamd_symcache_find_symbol (task->cfg->cache,
  1711. sym->name);
  1712. if (id >= 0) {
  1713. ls->results[i].id = id;
  1714. ls->results[i].score = sym->score;
  1715. }
  1716. else {
  1717. ls->results[i].id = -1;
  1718. ls->results[i].score = 0.0;
  1719. }
  1720. i ++;
  1721. });
  1722. memcpy (&ls->results[n], extra->data, nextra * sizeof (er));
  1723. }
  1724. else {
  1725. sz = sizeof (*ls);
  1726. ls = g_malloc0 (sz);
  1727. ls->nresults = 0;
  1728. }
  1729. /* We don't really care about return value here */
  1730. if (write (lp->fd, ls, sz) == -1) {
  1731. msg_info_protocol ("cannot write to log pipe: %s",
  1732. strerror (errno));
  1733. }
  1734. g_free (ls);
  1735. break;
  1736. default:
  1737. msg_err_protocol ("unknown log format %d", lp->type);
  1738. break;
  1739. }
  1740. }
  1741. }
  1742. g_array_free (extra, TRUE);
  1743. }
  1744. void
  1745. rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout)
  1746. {
  1747. struct rspamd_http_message *msg;
  1748. const gchar *ctype = "application/json";
  1749. rspamd_fstring_t *reply;
  1750. msg = rspamd_http_new_message (HTTP_RESPONSE);
  1751. if (rspamd_http_connection_is_encrypted (task->http_conn)) {
  1752. msg_info_protocol ("<%s> writing encrypted reply",
  1753. MESSAGE_FIELD_CHECK (task, message_id));
  1754. }
  1755. /* Compatibility */
  1756. if (task->cmd == CMD_CHECK_RSPAMC) {
  1757. msg->method = HTTP_SYMBOLS;
  1758. }
  1759. else if (task->cmd == CMD_CHECK_SPAMC) {
  1760. msg->method = HTTP_SYMBOLS;
  1761. msg->flags |= RSPAMD_HTTP_FLAG_SPAMC;
  1762. }
  1763. if (task->err != NULL) {
  1764. msg_debug_protocol ("writing error reply to client");
  1765. ucl_object_t *top = NULL;
  1766. top = ucl_object_typed_new (UCL_OBJECT);
  1767. msg->code = 500 + task->err->code % 100;
  1768. msg->status = rspamd_fstring_new_init (task->err->message,
  1769. strlen (task->err->message));
  1770. ucl_object_insert_key (top, ucl_object_fromstring (task->err->message),
  1771. "error", 0, false);
  1772. ucl_object_insert_key (top,
  1773. ucl_object_fromstring (g_quark_to_string (task->err->domain)),
  1774. "error_domain", 0, false);
  1775. reply = rspamd_fstring_sized_new (256);
  1776. rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply);
  1777. ucl_object_unref (top);
  1778. /* We also need to validate utf8 */
  1779. if (rspamd_fast_utf8_validate (reply->str, reply->len) != 0) {
  1780. gsize valid_len;
  1781. gchar *validated;
  1782. /* We copy reply several times here but it should be a rare case */
  1783. validated = rspamd_str_make_utf_valid (reply->str, reply->len,
  1784. &valid_len, task->task_pool);
  1785. rspamd_http_message_set_body (msg, validated, valid_len);
  1786. rspamd_fstring_free (reply);
  1787. }
  1788. else {
  1789. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1790. }
  1791. }
  1792. else {
  1793. msg->status = rspamd_fstring_new_init ("OK", 2);
  1794. switch (task->cmd) {
  1795. case CMD_CHECK:
  1796. case CMD_CHECK_RSPAMC:
  1797. case CMD_CHECK_SPAMC:
  1798. case CMD_SKIP:
  1799. case CMD_CHECK_V2:
  1800. rspamd_protocol_http_reply (msg, task, NULL);
  1801. rspamd_protocol_write_log_pipe (task);
  1802. break;
  1803. case CMD_PING:
  1804. msg_debug_protocol ("writing pong to client");
  1805. rspamd_http_message_set_body (msg, "pong" CRLF, 6);
  1806. ctype = "text/plain";
  1807. break;
  1808. default:
  1809. msg_err_protocol ("BROKEN");
  1810. break;
  1811. }
  1812. }
  1813. ev_now_update (task->event_loop);
  1814. msg->date = ev_time ();
  1815. rspamd_http_connection_reset (task->http_conn);
  1816. rspamd_http_connection_write_message (task->http_conn, msg, NULL,
  1817. ctype, task, timeout);
  1818. task->processed_stages |= RSPAMD_TASK_STAGE_REPLIED;
  1819. }