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.

protocol.c 53KB

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