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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  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 (
  1315. "DKIM-Signature", strlen ("DKIM-Signature"),
  1316. dkim_sig->str, dkim_sig->len,
  1317. 80, RSPAMD_TASK_NEWLINES_LF, NULL);
  1318. }
  1319. else {
  1320. folded_header = rspamd_header_value_fold (
  1321. "DKIM-Signature", strlen ("DKIM-Signature"),
  1322. dkim_sig->str, dkim_sig->len,
  1323. 80,
  1324. MESSAGE_FIELD (task, nlines_type),
  1325. NULL);
  1326. }
  1327. ucl_array_append (ar,
  1328. ucl_object_fromstring_common (folded_header->str,
  1329. folded_header->len, UCL_STRING_RAW));
  1330. g_string_free (folded_header, TRUE);
  1331. }
  1332. ucl_object_insert_key (top,
  1333. ar,
  1334. "dkim-signature", 0,
  1335. false);
  1336. }
  1337. else {
  1338. /* Single DKIM signature */
  1339. GString *folded_header;
  1340. dkim_sig = (GString *) dkim_sigs->data;
  1341. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) {
  1342. folded_header = rspamd_header_value_fold (
  1343. "DKIM-Signature", strlen ("DKIM-Signature"),
  1344. dkim_sig->str, dkim_sig->len,
  1345. 80, RSPAMD_TASK_NEWLINES_LF, NULL);
  1346. }
  1347. else {
  1348. folded_header = rspamd_header_value_fold (
  1349. "DKIM-Signature", strlen ("DKIM-Signature"),
  1350. dkim_sig->str, dkim_sig->len,
  1351. 80, MESSAGE_FIELD (task, nlines_type),
  1352. NULL);
  1353. }
  1354. ucl_object_insert_key (top,
  1355. ucl_object_fromstring_common (folded_header->str,
  1356. folded_header->len, UCL_STRING_RAW),
  1357. "dkim-signature", 0, false);
  1358. g_string_free (folded_header, TRUE);
  1359. }
  1360. }
  1361. }
  1362. if (flags & RSPAMD_PROTOCOL_RMILTER) {
  1363. milter_reply = rspamd_mempool_get_variable (task->task_pool,
  1364. RSPAMD_MEMPOOL_MILTER_REPLY);
  1365. if (milter_reply) {
  1366. if (task->cmd != CMD_CHECK) {
  1367. ucl_object_insert_key (top, ucl_object_ref (milter_reply),
  1368. "milter", 0, false);
  1369. }
  1370. else {
  1371. ucl_object_insert_key (top, ucl_object_ref (milter_reply),
  1372. "rmilter", 0, false);
  1373. }
  1374. }
  1375. }
  1376. return top;
  1377. }
  1378. void
  1379. rspamd_protocol_http_reply (struct rspamd_http_message *msg,
  1380. struct rspamd_task *task, ucl_object_t **pobj)
  1381. {
  1382. struct rspamd_scan_result *metric_res;
  1383. const struct rspamd_re_cache_stat *restat;
  1384. ucl_object_t *top = NULL;
  1385. rspamd_fstring_t *reply;
  1386. gint flags = RSPAMD_PROTOCOL_DEFAULT;
  1387. struct rspamd_action *action;
  1388. /* Removed in 2.0 */
  1389. #if 0
  1390. GHashTableIter hiter;
  1391. gpointer h, v;
  1392. /* Write custom headers */
  1393. g_hash_table_iter_init (&hiter, task->reply_headers);
  1394. while (g_hash_table_iter_next (&hiter, &h, &v)) {
  1395. rspamd_ftok_t *hn = h, *hv = v;
  1396. rspamd_http_message_add_header (msg, hn->begin, hv->begin);
  1397. }
  1398. #endif
  1399. flags |= RSPAMD_PROTOCOL_URLS;
  1400. top = rspamd_protocol_write_ucl (task, flags);
  1401. if (pobj) {
  1402. *pobj = top;
  1403. }
  1404. if (!(task->flags & RSPAMD_TASK_FLAG_NO_LOG)) {
  1405. rspamd_roll_history_update (task->worker->srv->history, task);
  1406. }
  1407. else {
  1408. msg_debug_protocol ("skip history update due to no log flag");
  1409. }
  1410. rspamd_task_write_log (task);
  1411. if (task->cfg->log_flags & RSPAMD_LOG_FLAG_RE_CACHE) {
  1412. restat = rspamd_re_cache_get_stat (task->re_rt);
  1413. g_assert (restat != NULL);
  1414. msg_notice_task (
  1415. "regexp statistics: %ud pcre regexps scanned, %ud regexps matched,"
  1416. " %ud regexps total, %ud regexps cached,"
  1417. " %HL scanned using pcre, %HL scanned total",
  1418. restat->regexp_checked,
  1419. restat->regexp_matched,
  1420. restat->regexp_total,
  1421. restat->regexp_fast_cached,
  1422. restat->bytes_scanned_pcre,
  1423. restat->bytes_scanned);
  1424. }
  1425. reply = rspamd_fstring_sized_new (1000);
  1426. if (msg->method < HTTP_SYMBOLS && !RSPAMD_TASK_IS_SPAMC (task)) {
  1427. msg_debug_protocol ("writing json reply");
  1428. rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply);
  1429. }
  1430. else {
  1431. if (RSPAMD_TASK_IS_SPAMC (task)) {
  1432. msg_debug_protocol ("writing spamc legacy reply to client");
  1433. rspamd_ucl_tospamc_output (top, &reply);
  1434. }
  1435. else {
  1436. msg_debug_protocol ("writing rspamc legacy reply to client");
  1437. rspamd_ucl_torspamc_output (top, &reply);
  1438. }
  1439. }
  1440. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK) {
  1441. /* Check if we need to insert a body block */
  1442. if (task->flags & RSPAMD_TASK_FLAG_MESSAGE_REWRITE) {
  1443. GString *hdr_offset = g_string_sized_new (30);
  1444. rspamd_printf_gstring (hdr_offset, "%z", RSPAMD_FSTRING_LEN (reply));
  1445. rspamd_http_message_add_header (msg, MESSAGE_OFFSET_HEADER,
  1446. hdr_offset->str);
  1447. msg_debug_protocol ("write body block at position %s",
  1448. hdr_offset->str);
  1449. g_string_free (hdr_offset, TRUE);
  1450. /* In case of milter, we append just body, otherwise - full message */
  1451. if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) {
  1452. const gchar *start;
  1453. goffset len, hdr_off;
  1454. start = task->msg.begin;
  1455. len = task->msg.len;
  1456. hdr_off = MESSAGE_FIELD (task, raw_headers_content).len;
  1457. if (hdr_off < len) {
  1458. start += hdr_off;
  1459. len -= hdr_off;
  1460. /* The problem here is that we need not end of headers, we need
  1461. * start of body.
  1462. *
  1463. * Hence, we need to skip one \r\n till there is anything else in
  1464. * a line.
  1465. */
  1466. if (*start == '\r' && len > 0) {
  1467. start ++;
  1468. len --;
  1469. }
  1470. if (*start == '\n' && len > 0) {
  1471. start ++;
  1472. len --;
  1473. }
  1474. msg_debug_protocol ("milter version of body block size %d",
  1475. (int)len);
  1476. reply = rspamd_fstring_append (reply, start, len);
  1477. }
  1478. }
  1479. else {
  1480. msg_debug_protocol ("general version of body block size %d",
  1481. (int)task->msg.len);
  1482. reply = rspamd_fstring_append (reply,
  1483. task->msg.begin, task->msg.len);
  1484. }
  1485. }
  1486. }
  1487. if ((task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_COMPRESSED) &&
  1488. rspamd_libs_reset_compression (task->cfg->libs_ctx)) {
  1489. /* We can compress output */
  1490. ZSTD_inBuffer zin;
  1491. ZSTD_outBuffer zout;
  1492. ZSTD_CStream *zstream;
  1493. rspamd_fstring_t *compressed_reply;
  1494. gsize r;
  1495. zstream = task->cfg->libs_ctx->out_zstream;
  1496. compressed_reply = rspamd_fstring_sized_new (ZSTD_compressBound (reply->len));
  1497. zin.pos = 0;
  1498. zin.src = reply->str;
  1499. zin.size = reply->len;
  1500. zout.pos = 0;
  1501. zout.dst = compressed_reply->str;
  1502. zout.size = compressed_reply->allocated;
  1503. while (zin.pos < zin.size) {
  1504. r = ZSTD_compressStream (zstream, &zout, &zin);
  1505. if (ZSTD_isError (r)) {
  1506. msg_err_protocol ("cannot compress: %s", ZSTD_getErrorName (r));
  1507. rspamd_fstring_free (compressed_reply);
  1508. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1509. goto end;
  1510. }
  1511. }
  1512. ZSTD_flushStream (zstream, &zout);
  1513. r = ZSTD_endStream (zstream, &zout);
  1514. if (ZSTD_isError (r)) {
  1515. msg_err_protocol ("cannot finalize compress: %s", ZSTD_getErrorName (r));
  1516. rspamd_fstring_free (compressed_reply);
  1517. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1518. goto end;
  1519. }
  1520. msg_info_protocol ("writing compressed results: %z bytes before "
  1521. "%z bytes after", zin.pos, zout.pos);
  1522. compressed_reply->len = zout.pos;
  1523. rspamd_fstring_free (reply);
  1524. rspamd_http_message_set_body_from_fstring_steal (msg, compressed_reply);
  1525. rspamd_http_message_add_header (msg, COMPRESSION_HEADER, "zstd");
  1526. if (task->cfg->libs_ctx->out_dict &&
  1527. task->cfg->libs_ctx->out_dict->id != 0) {
  1528. gchar dict_str[32];
  1529. rspamd_snprintf (dict_str, sizeof (dict_str), "%ud",
  1530. task->cfg->libs_ctx->out_dict->id);
  1531. rspamd_http_message_add_header (msg, "Dictionary", dict_str);
  1532. }
  1533. }
  1534. else {
  1535. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1536. }
  1537. end:
  1538. if (!(task->flags & RSPAMD_TASK_FLAG_NO_STAT)) {
  1539. /* Update stat for default metric */
  1540. msg_debug_protocol ("skip stats update due to no_stat flag");
  1541. metric_res = task->result;
  1542. if (metric_res != NULL) {
  1543. action = rspamd_check_action_metric (task, NULL, NULL);
  1544. /* TODO: handle custom actions in stats */
  1545. if (action->action_type == METRIC_ACTION_SOFT_REJECT &&
  1546. (task->flags & RSPAMD_TASK_FLAG_GREYLISTED)) {
  1547. /* Set stat action to greylist to display greylisted messages */
  1548. #ifndef HAVE_ATOMIC_BUILTINS
  1549. task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST]++;
  1550. #else
  1551. __atomic_add_fetch (&task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST],
  1552. 1, __ATOMIC_RELEASE);
  1553. #endif
  1554. }
  1555. else if (action->action_type < METRIC_ACTION_MAX) {
  1556. #ifndef HAVE_ATOMIC_BUILTINS
  1557. task->worker->srv->stat->actions_stat[action->action_type]++;
  1558. #else
  1559. __atomic_add_fetch (&task->worker->srv->stat->actions_stat[action->action_type],
  1560. 1, __ATOMIC_RELEASE);
  1561. #endif
  1562. }
  1563. }
  1564. /* Increase counters */
  1565. #ifndef HAVE_ATOMIC_BUILTINS
  1566. task->worker->srv->stat->messages_scanned++;
  1567. #else
  1568. __atomic_add_fetch (&task->worker->srv->stat->messages_scanned,
  1569. 1, __ATOMIC_RELEASE);
  1570. #endif
  1571. }
  1572. }
  1573. void
  1574. rspamd_protocol_write_log_pipe (struct rspamd_task *task)
  1575. {
  1576. struct rspamd_worker_log_pipe *lp;
  1577. struct rspamd_protocol_log_message_sum *ls;
  1578. lua_State *L = task->cfg->lua_state;
  1579. struct rspamd_scan_result *mres;
  1580. struct rspamd_symbol_result *sym;
  1581. gint id, i;
  1582. guint32 n = 0, nextra = 0;
  1583. gsize sz;
  1584. GArray *extra;
  1585. struct rspamd_protocol_log_symbol_result er;
  1586. struct rspamd_task **ptask;
  1587. /* Get extra results from lua plugins */
  1588. extra = g_array_new (FALSE, FALSE, sizeof (er));
  1589. lua_getglobal (L, "rspamd_plugins");
  1590. if (lua_istable (L, -1)) {
  1591. lua_pushnil (L);
  1592. while (lua_next (L, -2)) {
  1593. if (lua_istable (L, -1)) {
  1594. lua_pushvalue (L, -2);
  1595. /* stack:
  1596. * -1: copy of key
  1597. * -2: value (module table)
  1598. * -3: key (module name)
  1599. * -4: global
  1600. */
  1601. lua_pushstring (L, "log_callback");
  1602. lua_gettable (L, -3);
  1603. /* stack:
  1604. * -1: func
  1605. * -2: copy of key
  1606. * -3: value (module table)
  1607. * -3: key (module name)
  1608. * -4: global
  1609. */
  1610. if (lua_isfunction (L, -1)) {
  1611. ptask = lua_newuserdata (L, sizeof (*ptask));
  1612. *ptask = task;
  1613. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1614. /* stack:
  1615. * -1: task
  1616. * -2: func
  1617. * -3: key copy
  1618. * -4: value (module table)
  1619. * -5: key (module name)
  1620. * -6: global
  1621. */
  1622. msg_debug_protocol ("calling for %s", lua_tostring (L, -3));
  1623. if (lua_pcall (L, 1, 1, 0) != 0) {
  1624. msg_info_protocol ("call to log callback %s failed: %s",
  1625. lua_tostring (L, -2), lua_tostring (L, -1));
  1626. lua_pop (L, 1);
  1627. /* stack:
  1628. * -1: key copy
  1629. * -2: value
  1630. * -3: key
  1631. */
  1632. }
  1633. else {
  1634. /* stack:
  1635. * -1: result
  1636. * -2: key copy
  1637. * -3: value
  1638. * -4: key
  1639. */
  1640. if (lua_istable (L, -1)) {
  1641. /* Another iteration */
  1642. lua_pushnil (L);
  1643. while (lua_next (L, -2)) {
  1644. /* stack:
  1645. * -1: value
  1646. * -2: key
  1647. * -3: result table (pcall)
  1648. * -4: key copy (parent)
  1649. * -5: value (parent)
  1650. * -6: key (parent)
  1651. */
  1652. if (lua_istable (L, -1)) {
  1653. er.id = 0;
  1654. er.score = 0.0;
  1655. lua_rawgeti (L, -1, 1);
  1656. if (lua_isnumber (L, -1)) {
  1657. er.id = lua_tonumber (L, -1);
  1658. }
  1659. lua_rawgeti (L, -2, 2);
  1660. if (lua_isnumber (L, -1)) {
  1661. er.score = lua_tonumber (L, -1);
  1662. }
  1663. /* stack:
  1664. * -1: value[2]
  1665. * -2: value[1]
  1666. * -3: values
  1667. * -4: key
  1668. * -5: result table (pcall)
  1669. * -6: key copy (parent)
  1670. * -7: value (parent)
  1671. * -8: key (parent)
  1672. */
  1673. lua_pop (L, 2); /* Values */
  1674. g_array_append_val (extra, er);
  1675. }
  1676. lua_pop (L, 1); /* Value for lua_next */
  1677. }
  1678. lua_pop (L, 1); /* Table result of pcall */
  1679. }
  1680. else {
  1681. msg_info_protocol ("call to log callback %s returned "
  1682. "wrong type: %s",
  1683. lua_tostring (L, -2),
  1684. lua_typename (L, lua_type (L, -1)));
  1685. lua_pop (L, 1); /* Returned error */
  1686. }
  1687. }
  1688. }
  1689. else {
  1690. lua_pop (L, 1);
  1691. /* stack:
  1692. * -1: key copy
  1693. * -2: value
  1694. * -3: key
  1695. */
  1696. }
  1697. }
  1698. lua_pop (L, 2); /* Top table + key copy */
  1699. }
  1700. lua_pop (L, 1); /* rspamd_plugins global */
  1701. }
  1702. else {
  1703. lua_pop (L, 1);
  1704. }
  1705. nextra = extra->len;
  1706. LL_FOREACH (task->cfg->log_pipes, lp) {
  1707. if (lp->fd != -1) {
  1708. switch (lp->type) {
  1709. case RSPAMD_LOG_PIPE_SYMBOLS:
  1710. mres = task->result;
  1711. if (mres) {
  1712. n = kh_size (mres->symbols);
  1713. sz = sizeof (*ls) +
  1714. sizeof (struct rspamd_protocol_log_symbol_result) *
  1715. (n + nextra);
  1716. ls = g_malloc0 (sz);
  1717. /* Handle settings id */
  1718. if (task->settings_elt) {
  1719. ls->settings_id = task->settings_elt->id;
  1720. }
  1721. else {
  1722. ls->settings_id = 0;
  1723. }
  1724. ls->score = mres->score;
  1725. ls->required_score = rspamd_task_get_required_score (task,
  1726. mres);
  1727. ls->nresults = n;
  1728. ls->nextra = nextra;
  1729. i = 0;
  1730. kh_foreach_value (mres->symbols, sym, {
  1731. id = rspamd_symcache_find_symbol (task->cfg->cache,
  1732. sym->name);
  1733. if (id >= 0) {
  1734. ls->results[i].id = id;
  1735. ls->results[i].score = sym->score;
  1736. }
  1737. else {
  1738. ls->results[i].id = -1;
  1739. ls->results[i].score = 0.0;
  1740. }
  1741. i ++;
  1742. });
  1743. memcpy (&ls->results[n], extra->data, nextra * sizeof (er));
  1744. }
  1745. else {
  1746. sz = sizeof (*ls);
  1747. ls = g_malloc0 (sz);
  1748. ls->nresults = 0;
  1749. }
  1750. /* We don't really care about return value here */
  1751. if (write (lp->fd, ls, sz) == -1) {
  1752. msg_info_protocol ("cannot write to log pipe: %s",
  1753. strerror (errno));
  1754. }
  1755. g_free (ls);
  1756. break;
  1757. default:
  1758. msg_err_protocol ("unknown log format %d", lp->type);
  1759. break;
  1760. }
  1761. }
  1762. }
  1763. g_array_free (extra, TRUE);
  1764. }
  1765. void
  1766. rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout)
  1767. {
  1768. struct rspamd_http_message *msg;
  1769. const gchar *ctype = "application/json";
  1770. rspamd_fstring_t *reply;
  1771. msg = rspamd_http_new_message (HTTP_RESPONSE);
  1772. if (rspamd_http_connection_is_encrypted (task->http_conn)) {
  1773. msg_info_protocol ("<%s> writing encrypted reply",
  1774. MESSAGE_FIELD_CHECK (task, message_id));
  1775. }
  1776. /* Compatibility */
  1777. if (task->cmd == CMD_CHECK_RSPAMC) {
  1778. msg->method = HTTP_SYMBOLS;
  1779. }
  1780. else if (task->cmd == CMD_CHECK_SPAMC) {
  1781. msg->method = HTTP_SYMBOLS;
  1782. msg->flags |= RSPAMD_HTTP_FLAG_SPAMC;
  1783. }
  1784. if (task->err != NULL) {
  1785. msg_debug_protocol ("writing error reply to client");
  1786. ucl_object_t *top = NULL;
  1787. top = ucl_object_typed_new (UCL_OBJECT);
  1788. msg->code = 500 + task->err->code % 100;
  1789. msg->status = rspamd_fstring_new_init (task->err->message,
  1790. strlen (task->err->message));
  1791. ucl_object_insert_key (top, ucl_object_fromstring (task->err->message),
  1792. "error", 0, false);
  1793. ucl_object_insert_key (top,
  1794. ucl_object_fromstring (g_quark_to_string (task->err->domain)),
  1795. "error_domain", 0, false);
  1796. reply = rspamd_fstring_sized_new (256);
  1797. rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply);
  1798. ucl_object_unref (top);
  1799. /* We also need to validate utf8 */
  1800. if (rspamd_fast_utf8_validate (reply->str, reply->len) != 0) {
  1801. gsize valid_len;
  1802. gchar *validated;
  1803. /* We copy reply several times here but it should be a rare case */
  1804. validated = rspamd_str_make_utf_valid (reply->str, reply->len,
  1805. &valid_len, task->task_pool);
  1806. rspamd_http_message_set_body (msg, validated, valid_len);
  1807. rspamd_fstring_free (reply);
  1808. }
  1809. else {
  1810. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1811. }
  1812. }
  1813. else {
  1814. msg->status = rspamd_fstring_new_init ("OK", 2);
  1815. switch (task->cmd) {
  1816. case CMD_CHECK:
  1817. case CMD_CHECK_RSPAMC:
  1818. case CMD_CHECK_SPAMC:
  1819. case CMD_SKIP:
  1820. case CMD_CHECK_V2:
  1821. rspamd_protocol_http_reply (msg, task, NULL);
  1822. rspamd_protocol_write_log_pipe (task);
  1823. break;
  1824. case CMD_PING:
  1825. msg_debug_protocol ("writing pong to client");
  1826. rspamd_http_message_set_body (msg, "pong" CRLF, 6);
  1827. ctype = "text/plain";
  1828. break;
  1829. default:
  1830. msg_err_protocol ("BROKEN");
  1831. break;
  1832. }
  1833. }
  1834. ev_now_update (task->event_loop);
  1835. msg->date = ev_time ();
  1836. rspamd_http_connection_reset (task->http_conn);
  1837. rspamd_http_connection_write_message (task->http_conn, msg, NULL,
  1838. ctype, task, timeout);
  1839. task->processed_stages |= RSPAMD_TASK_STAGE_REPLIED;
  1840. }