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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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 "http_private.h"
  21. #include "worker_private.h"
  22. #include "libserver/cfg_file_private.h"
  23. #include "libmime/filter_private.h"
  24. #include "contrib/zstd/zstd.h"
  25. #include "lua/lua_common.h"
  26. #include "unix-std.h"
  27. #include "protocol_internal.h"
  28. #include "libserver/mempool_vars_internal.h"
  29. #include "task.h"
  30. #include <math.h>
  31. INIT_LOG_MODULE(protocol)
  32. #define msg_err_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  33. "protocol", task->task_pool->tag.uid, \
  34. G_STRFUNC, \
  35. __VA_ARGS__)
  36. #define msg_warn_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  37. "protocol", task->task_pool->tag.uid, \
  38. G_STRFUNC, \
  39. __VA_ARGS__)
  40. #define msg_info_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  41. "protocol", task->task_pool->tag.uid, \
  42. G_STRFUNC, \
  43. __VA_ARGS__)
  44. #define msg_debug_protocol(...) rspamd_conditional_debug_fast (NULL, NULL, \
  45. rspamd_protocol_log_id, "protocol", task->task_pool->tag.uid, \
  46. G_STRFUNC, \
  47. __VA_ARGS__)
  48. static GQuark
  49. rspamd_protocol_quark (void)
  50. {
  51. return g_quark_from_static_string ("protocol-error");
  52. }
  53. /*
  54. * Remove <> from the fixed string and copy it to the pool
  55. */
  56. static gchar *
  57. rspamd_protocol_escape_braces (struct rspamd_task *task, rspamd_fstring_t *in)
  58. {
  59. guint nchars = 0;
  60. const gchar *p;
  61. rspamd_ftok_t tok;
  62. gboolean has_obrace = FALSE;
  63. g_assert (in != NULL);
  64. g_assert (in->len > 0);
  65. p = in->str;
  66. while ((g_ascii_isspace (*p) || *p == '<') && nchars < in->len) {
  67. if (*p == '<') {
  68. has_obrace = TRUE;
  69. }
  70. p++;
  71. nchars ++;
  72. }
  73. tok.begin = p;
  74. p = in->str + in->len - 1;
  75. tok.len = in->len - nchars;
  76. while (g_ascii_isspace (*p) && tok.len > 0) {
  77. p--;
  78. tok.len --;
  79. }
  80. if (has_obrace && *p == '>') {
  81. tok.len --;
  82. }
  83. return rspamd_mempool_ftokdup (task->task_pool, &tok);
  84. }
  85. #define CMD_CHECK(str, cmd, len) (sizeof(cmd) - 1 == (len) && rspamd_lc_cmp((str), (cmd), (len)) == 0)
  86. static gboolean
  87. rspamd_protocol_handle_url (struct rspamd_task *task,
  88. struct rspamd_http_message *msg)
  89. {
  90. GHashTable *query_args;
  91. GHashTableIter it;
  92. struct http_parser_url u;
  93. const gchar *p;
  94. gsize pathlen;
  95. rspamd_ftok_t *key, *value;
  96. gpointer k, v;
  97. if (msg->url == NULL || msg->url->len == 0) {
  98. g_set_error (&task->err, rspamd_protocol_quark(), 400, "missing command");
  99. return FALSE;
  100. }
  101. if (http_parser_parse_url (msg->url->str, msg->url->len, 0, &u) != 0) {
  102. g_set_error (&task->err, rspamd_protocol_quark(), 400, "bad request URL");
  103. return FALSE;
  104. }
  105. if (!(u.field_set & (1 << UF_PATH))) {
  106. g_set_error (&task->err, rspamd_protocol_quark(), 400,
  107. "bad request URL: missing path");
  108. return FALSE;
  109. }
  110. p = msg->url->str + u.field_data[UF_PATH].off;
  111. pathlen = u.field_data[UF_PATH].len;
  112. if (*p == '/') {
  113. p ++;
  114. pathlen --;
  115. }
  116. switch (*p) {
  117. case 'c':
  118. case 'C':
  119. /* check */
  120. if (CMD_CHECK (p, MSG_CMD_CHECK_V2, pathlen)) {
  121. task->cmd = CMD_CHECK_V2;
  122. }
  123. else if (CMD_CHECK (p, MSG_CMD_CHECK, pathlen)) {
  124. task->cmd = CMD_CHECK;
  125. }
  126. else {
  127. goto err;
  128. }
  129. break;
  130. case 's':
  131. case 'S':
  132. /* symbols, skip */
  133. if (CMD_CHECK (p, MSG_CMD_SYMBOLS, pathlen)) {
  134. task->cmd = CMD_SYMBOLS;
  135. }
  136. else if (CMD_CHECK (p, MSG_CMD_SCAN, pathlen)) {
  137. task->cmd = CMD_CHECK_V2;
  138. }
  139. else if (CMD_CHECK (p, MSG_CMD_SKIP, pathlen)) {
  140. task->cmd = CMD_SKIP;
  141. }
  142. else {
  143. goto err;
  144. }
  145. break;
  146. case 'p':
  147. case 'P':
  148. /* ping, process */
  149. if (CMD_CHECK (p, MSG_CMD_PING, pathlen)) {
  150. task->cmd = CMD_PING;
  151. }
  152. else if (CMD_CHECK (p, MSG_CMD_PROCESS, pathlen)) {
  153. task->cmd = CMD_PROCESS;
  154. }
  155. else {
  156. goto err;
  157. }
  158. break;
  159. case 'r':
  160. case 'R':
  161. /* report, report_ifspam */
  162. if (CMD_CHECK (p, MSG_CMD_REPORT, pathlen)) {
  163. task->cmd = CMD_REPORT;
  164. }
  165. else if (CMD_CHECK (p, MSG_CMD_REPORT_IFSPAM, pathlen)) {
  166. task->cmd = CMD_REPORT_IFSPAM;
  167. }
  168. else {
  169. goto err;
  170. }
  171. break;
  172. default:
  173. goto err;
  174. }
  175. if (u.field_set & (1u << UF_QUERY)) {
  176. /* In case if we have a query, we need to store it somewhere */
  177. query_args = rspamd_http_message_parse_query (msg);
  178. /* Insert the rest of query params as HTTP headers */
  179. g_hash_table_iter_init (&it, query_args);
  180. while (g_hash_table_iter_next (&it, &k, &v)) {
  181. key = k;
  182. value = v;
  183. /* Steal strings */
  184. g_hash_table_iter_steal (&it);
  185. rspamd_task_add_request_header (task, key, value);
  186. msg_debug_protocol ("added header \"%T\" -> \"%T\" from HTTP query",
  187. key, value);
  188. }
  189. g_hash_table_unref (query_args);
  190. }
  191. return TRUE;
  192. err:
  193. g_set_error (&task->err, rspamd_protocol_quark(), 400, "invalid command");
  194. return FALSE;
  195. }
  196. #define IF_HEADER(name) \
  197. srch.begin = (name); \
  198. srch.len = sizeof (name) - 1; \
  199. if (rspamd_ftok_casecmp (hn_tok, &srch) == 0)
  200. gboolean
  201. rspamd_protocol_handle_headers (struct rspamd_task *task,
  202. struct rspamd_http_message *msg)
  203. {
  204. rspamd_fstring_t *hn, *hv;
  205. rspamd_ftok_t *hn_tok, *hv_tok, srch;
  206. gboolean fl, has_ip = FALSE;
  207. struct rspamd_http_header *header, *h, *htmp;
  208. struct rspamd_email_address *addr;
  209. HASH_ITER (hh, msg->headers, header, htmp) {
  210. DL_FOREACH (header, h) {
  211. hn = rspamd_fstring_new_init (h->name.begin, h->name.len);
  212. hv = rspamd_fstring_new_init (h->value.begin, h->value.len);
  213. hn_tok = rspamd_ftok_map (hn);
  214. hv_tok = rspamd_ftok_map (hv);
  215. switch (*hn_tok->begin) {
  216. case 'd':
  217. case 'D':
  218. IF_HEADER (DELIVER_TO_HEADER) {
  219. task->deliver_to = rspamd_protocol_escape_braces (task, hv);
  220. msg_debug_protocol ("read deliver-to header, value: %s",
  221. task->deliver_to);
  222. }
  223. else {
  224. msg_debug_protocol ("wrong header: %V", hn);
  225. }
  226. break;
  227. case 'h':
  228. case 'H':
  229. IF_HEADER (HELO_HEADER) {
  230. task->helo = rspamd_mempool_ftokdup (task->task_pool, hv_tok);
  231. msg_debug_protocol ("read helo header, value: %s", task->helo);
  232. }
  233. IF_HEADER (HOSTNAME_HEADER) {
  234. task->hostname = rspamd_mempool_ftokdup (task->task_pool,
  235. hv_tok);
  236. msg_debug_protocol ("read hostname header, value: %s", task->hostname);
  237. }
  238. break;
  239. case 'f':
  240. case 'F':
  241. IF_HEADER (FROM_HEADER) {
  242. task->from_envelope = rspamd_email_address_from_smtp (hv->str,
  243. hv->len);
  244. msg_debug_protocol ("read from header, value: %V", hv);
  245. if (!task->from_envelope) {
  246. msg_err_protocol ("bad from header: '%V'", hv);
  247. task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS;
  248. }
  249. }
  250. IF_HEADER (FILENAME_HEADER) {
  251. task->msg.fpath = rspamd_mempool_ftokdup (task->task_pool,
  252. hv_tok);
  253. msg_debug_protocol ("read filename header, value: %s", task->msg.fpath);
  254. }
  255. break;
  256. case 'j':
  257. case 'J':
  258. IF_HEADER (JSON_HEADER) {
  259. msg_debug_protocol ("read json header, value: %V", hv);
  260. fl = rspamd_config_parse_flag (hv->str, hv->len);
  261. if (fl) {
  262. task->flags |= RSPAMD_TASK_FLAG_JSON;
  263. }
  264. else {
  265. task->flags &= ~RSPAMD_TASK_FLAG_JSON;
  266. }
  267. }
  268. else {
  269. msg_debug_protocol ("wrong header: %V", hn);
  270. }
  271. break;
  272. case 'q':
  273. case 'Q':
  274. IF_HEADER (QUEUE_ID_HEADER) {
  275. task->queue_id = rspamd_mempool_ftokdup (task->task_pool,
  276. hv_tok);
  277. msg_debug_protocol ("read queue_id header, value: %s", task->queue_id);
  278. }
  279. else {
  280. msg_debug_protocol ("wrong header: %V", hn);
  281. }
  282. break;
  283. case 'r':
  284. case 'R':
  285. IF_HEADER (RCPT_HEADER) {
  286. const gchar *p, *end;
  287. gsize cur_len;
  288. p = hv->str;
  289. end = p + hv->len;
  290. while (p < end) {
  291. cur_len = rspamd_memcspn (p, ",", end - p);
  292. if (cur_len > 0) {
  293. addr = rspamd_email_address_from_smtp (p, cur_len);
  294. if (addr) {
  295. if (task->rcpt_envelope == NULL) {
  296. task->rcpt_envelope = g_ptr_array_sized_new (
  297. 2);
  298. }
  299. g_ptr_array_add (task->rcpt_envelope, addr);
  300. } else {
  301. msg_err_protocol ("bad rcpt header: '%T'",
  302. &h->value);
  303. task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS;
  304. }
  305. p += cur_len;
  306. }
  307. while (p < end && *p == ',') {
  308. p ++;
  309. }
  310. }
  311. msg_debug_protocol ("read rcpt header, value: %V", hv);
  312. }
  313. IF_HEADER (RAW_DATA_HEADER) {
  314. srch.begin = "yes";
  315. srch.len = 3;
  316. msg_debug_protocol ("read raw data header, value: %V", hv);
  317. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  318. task->flags &= ~RSPAMD_TASK_FLAG_MIME;
  319. msg_debug_protocol ("disable mime parsing");
  320. }
  321. }
  322. break;
  323. case 'i':
  324. case 'I':
  325. IF_HEADER (IP_ADDR_HEADER) {
  326. if (!rspamd_parse_inet_address (&task->from_addr, hv->str, hv->len)) {
  327. msg_err_protocol ("bad ip header: '%V'", hv);
  328. }
  329. else {
  330. msg_debug_protocol ("read IP header, value: %V", hv);
  331. has_ip = TRUE;
  332. }
  333. }
  334. else {
  335. msg_debug_protocol ("wrong header: %V", hn);
  336. }
  337. break;
  338. case 'p':
  339. case 'P':
  340. IF_HEADER (PASS_HEADER) {
  341. srch.begin = "all";
  342. srch.len = 3;
  343. msg_debug_protocol ("read pass header, value: %V", hv);
  344. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  345. task->flags |= RSPAMD_TASK_FLAG_PASS_ALL;
  346. msg_debug_protocol ("pass all filters");
  347. }
  348. }
  349. IF_HEADER (PROFILE_HEADER) {
  350. msg_debug_protocol ("read profile header, value: %V", hv);
  351. task->flags |= RSPAMD_TASK_FLAG_PROFILE;
  352. }
  353. break;
  354. case 's':
  355. case 'S':
  356. IF_HEADER (SUBJECT_HEADER) {
  357. msg_debug_protocol ("read subject header, value: %V", hv);
  358. task->subject = rspamd_mempool_ftokdup (task->task_pool, hv_tok);
  359. }
  360. IF_HEADER (SETTINGS_ID_HEADER) {
  361. guint64 h;
  362. guint32 *hp;
  363. msg_debug_protocol ("read settings-id header, value: %V", hv);
  364. h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64,
  365. hv_tok->begin, hv_tok->len, 0xdeadbabe);
  366. hp = rspamd_mempool_alloc (task->task_pool, sizeof (*hp));
  367. memcpy (hp, &h, sizeof (*hp));
  368. rspamd_mempool_set_variable (task->task_pool,
  369. RSPAMD_MEMPOOL_SETTINGS_HASH,
  370. hp, NULL);
  371. }
  372. break;
  373. case 'u':
  374. case 'U':
  375. IF_HEADER (USER_HEADER) {
  376. /*
  377. * We must ignore User header in case of spamc, as SA has
  378. * different meaning of this header
  379. */
  380. msg_debug_protocol ("read user header, value: %V", hv);
  381. if (!RSPAMD_TASK_IS_SPAMC (task)) {
  382. task->user = rspamd_mempool_ftokdup (task->task_pool,
  383. hv_tok);
  384. }
  385. else {
  386. msg_info_protocol ("ignore user header: legacy SA protocol");
  387. }
  388. }
  389. IF_HEADER (URLS_HEADER) {
  390. srch.begin = "extended";
  391. srch.len = 8;
  392. msg_debug_protocol ("read urls header, value: %V", hv);
  393. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  394. task->flags |= RSPAMD_TASK_FLAG_EXT_URLS;
  395. msg_debug_protocol ("extended urls information");
  396. }
  397. }
  398. IF_HEADER (USER_AGENT_HEADER) {
  399. msg_debug_protocol ("read user-agent header, value: %V", hv);
  400. if (hv_tok->len == 6 &&
  401. rspamd_lc_cmp (hv_tok->begin, "rspamc", 6) == 0) {
  402. task->flags |= RSPAMD_TASK_FLAG_LOCAL_CLIENT;
  403. }
  404. }
  405. break;
  406. case 'l':
  407. case 'L':
  408. IF_HEADER (NO_LOG_HEADER) {
  409. msg_debug_protocol ("read log header, value: %V", hv);
  410. srch.begin = "no";
  411. srch.len = 2;
  412. if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) {
  413. task->flags |= RSPAMD_TASK_FLAG_NO_LOG;
  414. }
  415. }
  416. break;
  417. case 'm':
  418. case 'M':
  419. IF_HEADER (MLEN_HEADER) {
  420. msg_debug_protocol ("read message length header, value: %V", hv);
  421. if (!rspamd_strtoul (hv_tok->begin,
  422. hv_tok->len,
  423. &task->message_len)) {
  424. msg_err_protocol ("Invalid message length header: %V", hv);
  425. }
  426. else {
  427. task->flags |= RSPAMD_TASK_FLAG_HAS_CONTROL;
  428. }
  429. }
  430. IF_HEADER (MTA_TAG_HEADER) {
  431. gchar *mta_tag;
  432. mta_tag = rspamd_mempool_ftokdup (task->task_pool, hv_tok);
  433. rspamd_mempool_set_variable (task->task_pool,
  434. RSPAMD_MEMPOOL_MTA_TAG,
  435. mta_tag, NULL);
  436. msg_debug_protocol ("read MTA-Tag header, value: %s", mta_tag);
  437. }
  438. IF_HEADER (MTA_NAME_HEADER) {
  439. gchar *mta_name;
  440. mta_name = rspamd_mempool_ftokdup (task->task_pool, hv_tok);
  441. rspamd_mempool_set_variable (task->task_pool,
  442. RSPAMD_MEMPOOL_MTA_NAME,
  443. mta_name, NULL);
  444. msg_debug_protocol ("read MTA-Name header, value: %s", mta_name);
  445. }
  446. IF_HEADER (MILTER_HEADER) {
  447. task->flags |= RSPAMD_TASK_FLAG_MILTER;
  448. msg_debug_protocol ("read Milter header, value: %V", hv);
  449. }
  450. break;
  451. case 't':
  452. case 'T':
  453. IF_HEADER (TLS_CIPHER_HEADER) {
  454. task->flags |= RSPAMD_TASK_FLAG_SSL;
  455. msg_debug_protocol ("read TLS cipher header, value: %V", hv);
  456. }
  457. break;
  458. default:
  459. msg_debug_protocol ("generic header: %V", hn);
  460. break;
  461. }
  462. rspamd_task_add_request_header (task, hn_tok, hv_tok);
  463. }
  464. }
  465. if (!has_ip) {
  466. task->flags |= RSPAMD_TASK_FLAG_NO_IP;
  467. }
  468. return TRUE;
  469. }
  470. #define BOOL_TO_FLAG(val, flags, flag) do { \
  471. if ((val)) (flags) |= (flag); \
  472. else (flags) &= ~(flag); \
  473. } while(0)
  474. gboolean
  475. rspamd_protocol_parse_task_flags (rspamd_mempool_t *pool,
  476. const ucl_object_t *obj,
  477. gpointer ud,
  478. struct rspamd_rcl_section *section,
  479. GError **err)
  480. {
  481. struct rspamd_rcl_struct_parser *pd = ud;
  482. gint *target;
  483. const gchar *key;
  484. gboolean value;
  485. target = (gint *)(((gchar *)pd->user_struct) + pd->offset);
  486. key = ucl_object_key (obj);
  487. value = ucl_object_toboolean (obj);
  488. if (key != NULL) {
  489. if (g_ascii_strcasecmp (key, "pass_all") == 0) {
  490. BOOL_TO_FLAG (value, *target, RSPAMD_TASK_FLAG_PASS_ALL);
  491. }
  492. else if (g_ascii_strcasecmp (key, "no_log") == 0) {
  493. BOOL_TO_FLAG (value, *target, RSPAMD_TASK_FLAG_NO_LOG);
  494. }
  495. }
  496. return TRUE;
  497. }
  498. static struct rspamd_rcl_section *control_parser = NULL;
  499. static void
  500. rspamd_protocol_control_parser_init (void)
  501. {
  502. struct rspamd_rcl_section *sub;
  503. if (control_parser == NULL) {
  504. sub = rspamd_rcl_add_section (&control_parser,
  505. "*",
  506. NULL,
  507. NULL,
  508. UCL_OBJECT,
  509. FALSE,
  510. TRUE);
  511. /* Default handlers */
  512. rspamd_rcl_add_default_handler (sub,
  513. "ip",
  514. rspamd_rcl_parse_struct_addr,
  515. G_STRUCT_OFFSET (struct rspamd_task, from_addr),
  516. 0,
  517. NULL);
  518. rspamd_rcl_add_default_handler (sub,
  519. "from",
  520. rspamd_rcl_parse_struct_mime_addr,
  521. G_STRUCT_OFFSET (struct rspamd_task, from_envelope),
  522. 0,
  523. NULL);
  524. rspamd_rcl_add_default_handler (sub,
  525. "rcpt",
  526. rspamd_rcl_parse_struct_mime_addr,
  527. G_STRUCT_OFFSET (struct rspamd_task, rcpt_envelope),
  528. 0,
  529. NULL);
  530. rspamd_rcl_add_default_handler (sub,
  531. "helo",
  532. rspamd_rcl_parse_struct_string,
  533. G_STRUCT_OFFSET (struct rspamd_task, helo),
  534. 0,
  535. NULL);
  536. rspamd_rcl_add_default_handler (sub,
  537. "user",
  538. rspamd_rcl_parse_struct_string,
  539. G_STRUCT_OFFSET (struct rspamd_task, user),
  540. 0,
  541. NULL);
  542. rspamd_rcl_add_default_handler (sub,
  543. "pass_all",
  544. rspamd_protocol_parse_task_flags,
  545. G_STRUCT_OFFSET (struct rspamd_task, flags),
  546. 0,
  547. NULL);
  548. rspamd_rcl_add_default_handler (sub,
  549. "json",
  550. rspamd_protocol_parse_task_flags,
  551. G_STRUCT_OFFSET (struct rspamd_task, flags),
  552. 0,
  553. NULL);
  554. }
  555. }
  556. gboolean
  557. rspamd_protocol_handle_control (struct rspamd_task *task,
  558. const ucl_object_t *control)
  559. {
  560. GError *err = NULL;
  561. rspamd_protocol_control_parser_init ();
  562. if (!rspamd_rcl_parse (control_parser, task->cfg, task, task->task_pool,
  563. control, &err)) {
  564. msg_warn_protocol ("cannot parse control block: %e", err);
  565. g_error_free (err);
  566. return FALSE;
  567. }
  568. return TRUE;
  569. }
  570. gboolean
  571. rspamd_protocol_handle_request (struct rspamd_task *task,
  572. struct rspamd_http_message *msg)
  573. {
  574. gboolean ret = TRUE;
  575. if (msg->method == HTTP_SYMBOLS) {
  576. task->cmd = CMD_CHECK_V2;
  577. task->flags &= ~RSPAMD_TASK_FLAG_JSON;
  578. }
  579. else if (msg->method == HTTP_CHECK) {
  580. task->cmd = CMD_CHECK_V2;
  581. task->flags &= ~RSPAMD_TASK_FLAG_JSON;
  582. }
  583. else {
  584. task->flags |= RSPAMD_TASK_FLAG_JSON;
  585. ret = rspamd_protocol_handle_url (task, msg);
  586. }
  587. if (msg->flags & RSPAMD_HTTP_FLAG_SPAMC) {
  588. task->flags &= ~RSPAMD_TASK_FLAG_JSON;
  589. task->flags |= RSPAMD_TASK_FLAG_SPAMC;
  590. }
  591. return ret;
  592. }
  593. /* Structure for writing tree data */
  594. struct tree_cb_data {
  595. ucl_object_t *top;
  596. struct rspamd_task *task;
  597. };
  598. static ucl_object_t *
  599. rspamd_protocol_extended_url (struct rspamd_task *task,
  600. struct rspamd_url *url,
  601. const gchar *encoded, gsize enclen)
  602. {
  603. ucl_object_t *obj, *elt;
  604. obj = ucl_object_typed_new (UCL_OBJECT);
  605. elt = ucl_object_fromlstring (encoded, enclen);
  606. ucl_object_insert_key (obj, elt, "url", 0, false);
  607. if (url->surbllen > 0) {
  608. elt = ucl_object_fromlstring (url->surbl, url->surbllen);
  609. ucl_object_insert_key (obj, elt, "surbl", 0, false);
  610. }
  611. if (url->hostlen > 0) {
  612. elt = ucl_object_fromlstring (url->host, url->hostlen);
  613. ucl_object_insert_key (obj, elt, "host", 0, false);
  614. }
  615. elt = ucl_object_frombool (url->flags & RSPAMD_URL_FLAG_PHISHED);
  616. ucl_object_insert_key (obj, elt, "phished", 0, false);
  617. elt = ucl_object_frombool (url->flags & RSPAMD_URL_FLAG_REDIRECTED);
  618. ucl_object_insert_key (obj, elt, "redirected", 0, false);
  619. if (url->phished_url) {
  620. encoded = rspamd_url_encode (url->phished_url, &enclen, task->task_pool);
  621. elt = rspamd_protocol_extended_url (task, url->phished_url, encoded,
  622. enclen);
  623. ucl_object_insert_key (obj, elt, "orig_url", 0, false);
  624. }
  625. return obj;
  626. }
  627. /*
  628. * Callback for writing urls
  629. */
  630. static void
  631. urls_protocol_cb (gpointer key, gpointer value, gpointer ud)
  632. {
  633. struct tree_cb_data *cb = ud;
  634. struct rspamd_url *url = value;
  635. ucl_object_t *obj;
  636. struct rspamd_task *task = cb->task;
  637. const gchar *user_field = "unknown", *encoded;
  638. gboolean has_user = FALSE;
  639. guint len = 0;
  640. gsize enclen;
  641. encoded = rspamd_url_encode (url, &enclen, task->task_pool);
  642. if (!(task->flags & RSPAMD_TASK_FLAG_EXT_URLS)) {
  643. obj = ucl_object_fromlstring (encoded, enclen);
  644. }
  645. else {
  646. obj = rspamd_protocol_extended_url (task, url, encoded, enclen);
  647. }
  648. ucl_array_append (cb->top, obj);
  649. if (cb->task->cfg->log_urls) {
  650. if (task->user) {
  651. user_field = task->user;
  652. len = strlen (task->user);
  653. has_user = TRUE;
  654. }
  655. else if (task->from_envelope) {
  656. user_field = task->from_envelope->addr;
  657. len = task->from_envelope->addr_len;
  658. }
  659. msg_notice_task_encrypted ("<%s> %s: %*s; ip: %s; URL: %*s",
  660. task->message_id,
  661. has_user ? "user" : "from",
  662. len, user_field,
  663. rspamd_inet_address_to_string (task->from_addr),
  664. (gint)enclen, encoded);
  665. }
  666. }
  667. static ucl_object_t *
  668. rspamd_urls_tree_ucl (GHashTable *input, struct rspamd_task *task)
  669. {
  670. struct tree_cb_data cb;
  671. ucl_object_t *obj;
  672. obj = ucl_object_typed_new (UCL_ARRAY);
  673. cb.top = obj;
  674. cb.task = task;
  675. g_hash_table_foreach (input, urls_protocol_cb, &cb);
  676. return obj;
  677. }
  678. static void
  679. emails_protocol_cb (gpointer key, gpointer value, gpointer ud)
  680. {
  681. struct tree_cb_data *cb = ud;
  682. struct rspamd_url *url = value;
  683. ucl_object_t *obj;
  684. if (url->userlen > 0 && url->hostlen > 0 &&
  685. url->host == url->user + url->userlen + 1) {
  686. obj = ucl_object_fromlstring (url->user,
  687. url->userlen + url->hostlen + 1);
  688. ucl_array_append (cb->top, obj);
  689. }
  690. }
  691. static ucl_object_t *
  692. rspamd_emails_tree_ucl (GHashTable *input, struct rspamd_task *task)
  693. {
  694. struct tree_cb_data cb;
  695. ucl_object_t *obj;
  696. obj = ucl_object_typed_new (UCL_ARRAY);
  697. cb.top = obj;
  698. cb.task = task;
  699. g_hash_table_foreach (input, emails_protocol_cb, &cb);
  700. return obj;
  701. }
  702. /* Write new subject */
  703. static const gchar *
  704. rspamd_protocol_rewrite_subject (struct rspamd_task *task)
  705. {
  706. GString *subj_buf;
  707. gchar *res;
  708. const gchar *s, *c, *p;
  709. gsize slen = 0;
  710. c = rspamd_mempool_get_variable (task->task_pool, "metric_subject");
  711. if (c == NULL) {
  712. c = task->cfg->subject;
  713. }
  714. if (c == NULL) {
  715. c = SPAM_SUBJECT;
  716. }
  717. p = c;
  718. s = task->subject;
  719. if (s) {
  720. slen = strlen (s);
  721. }
  722. subj_buf = g_string_sized_new (strlen (c) + slen);
  723. while (*p) {
  724. if (*p == '%') {
  725. switch (p[1]) {
  726. case 's':
  727. g_string_append_len (subj_buf, c, p - c);
  728. if (s) {
  729. g_string_append_len (subj_buf, s, slen);
  730. }
  731. c = p + 2;
  732. p += 2;
  733. break;
  734. case 'd':
  735. g_string_append_len (subj_buf, c, p - c);
  736. rspamd_printf_gstring (subj_buf, "%.2f", task->result->score);
  737. c = p + 2;
  738. p += 2;
  739. break;
  740. case '%':
  741. g_string_append_len (subj_buf, c, p - c);
  742. g_string_append_c (subj_buf, '%');
  743. c = p + 2;
  744. p += 2;
  745. default:
  746. p ++; /* Just % something unknown */
  747. break;
  748. }
  749. }
  750. else {
  751. p++;
  752. }
  753. }
  754. if (p > c) {
  755. g_string_append_len (subj_buf, c, p - c);
  756. }
  757. res = rspamd_mime_header_encode (subj_buf->str, subj_buf->len);
  758. rspamd_mempool_add_destructor (task->task_pool,
  759. (rspamd_mempool_destruct_t)g_free,
  760. res);
  761. g_string_free (subj_buf, TRUE);
  762. return res;
  763. }
  764. static ucl_object_t *
  765. rspamd_metric_symbol_ucl (struct rspamd_task *task, struct rspamd_symbol_result *sym)
  766. {
  767. ucl_object_t *obj = NULL, *ar;
  768. const gchar *description = NULL;
  769. struct rspamd_symbol_option *opt;
  770. if (sym->sym != NULL) {
  771. description = sym->sym->description;
  772. }
  773. obj = ucl_object_typed_new (UCL_OBJECT);
  774. ucl_object_insert_key (obj, ucl_object_fromstring (
  775. sym->name), "name", 0, false);
  776. ucl_object_insert_key (obj, ucl_object_fromdouble (
  777. sym->score), "score", 0, false);
  778. if (task->cmd == CMD_CHECK_V2) {
  779. if (sym->sym) {
  780. ucl_object_insert_key (obj, ucl_object_fromdouble (
  781. sym->sym->score), "metric_score", 0, false);
  782. }
  783. else {
  784. ucl_object_insert_key (obj, ucl_object_fromdouble (0.0),
  785. "metric_score", 0, false);
  786. }
  787. }
  788. if (description) {
  789. ucl_object_insert_key (obj, ucl_object_fromstring (
  790. description), "description", 0, false);
  791. }
  792. if (sym->options != NULL) {
  793. ar = ucl_object_typed_new (UCL_ARRAY);
  794. DL_FOREACH (sym->opts_head, opt) {
  795. ucl_array_append (ar, ucl_object_fromstring (opt->option));
  796. }
  797. ucl_object_insert_key (obj, ar, "options", 0, false);
  798. }
  799. return obj;
  800. }
  801. static ucl_object_t *
  802. rspamd_metric_result_ucl (struct rspamd_task *task,
  803. struct rspamd_metric_result *mres, ucl_object_t *top)
  804. {
  805. struct rspamd_symbol_result *sym;
  806. gboolean is_spam;
  807. struct rspamd_action *action;
  808. ucl_object_t *obj = NULL, *sobj;
  809. const gchar *subject;
  810. action = rspamd_check_action_metric (task);
  811. is_spam = !(action->flags & RSPAMD_ACTION_HAM);
  812. if (task->cmd != CMD_CHECK_V2) {
  813. obj = ucl_object_typed_new (UCL_OBJECT);
  814. ucl_object_insert_key (obj,
  815. ucl_object_frombool (is_spam),
  816. "is_spam", 0, false);
  817. }
  818. else {
  819. obj = top;
  820. }
  821. ucl_object_insert_key (obj,
  822. ucl_object_frombool (RSPAMD_TASK_IS_SKIPPED (task)),
  823. "is_skipped", 0, false);
  824. if (!isnan (mres->score)) {
  825. ucl_object_insert_key (obj, ucl_object_fromdouble (mres->score),
  826. "score", 0, false);
  827. } else {
  828. ucl_object_insert_key (obj,
  829. ucl_object_fromdouble (0.0), "score", 0, false);
  830. }
  831. ucl_object_insert_key (obj,
  832. ucl_object_fromdouble (rspamd_task_get_required_score (task, mres)),
  833. "required_score", 0, false);
  834. ucl_object_insert_key (obj,
  835. ucl_object_fromstring (action->name),
  836. "action", 0, false);
  837. if (action->action_type == METRIC_ACTION_REWRITE_SUBJECT) {
  838. subject = rspamd_protocol_rewrite_subject (task);
  839. if (subject) {
  840. ucl_object_insert_key (obj, ucl_object_fromstring (subject),
  841. "subject", 0, false);
  842. }
  843. }
  844. /* Now handle symbols */
  845. if (task->cmd == CMD_CHECK_V2) {
  846. obj = ucl_object_typed_new (UCL_OBJECT);
  847. }
  848. kh_foreach_value_ptr (mres->symbols, sym, {
  849. if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
  850. sobj = rspamd_metric_symbol_ucl (task, sym);
  851. ucl_object_insert_key (obj, sobj, sym->name, 0, false);
  852. }
  853. });
  854. if (task->cmd == CMD_CHECK_V2) {
  855. ucl_object_insert_key (top, obj, "symbols", 0, false);
  856. }
  857. else {
  858. ucl_object_insert_key (top, obj, DEFAULT_METRIC, 0, false);
  859. }
  860. return obj;
  861. }
  862. void
  863. rspamd_ucl_torspamc_output (const ucl_object_t *top,
  864. rspamd_fstring_t **out)
  865. {
  866. const ucl_object_t *symbols, *score,
  867. *required_score, *is_spam, *elt, *cur;
  868. ucl_object_iter_t iter = NULL;
  869. score = ucl_object_lookup (top, "score");
  870. required_score = ucl_object_lookup (top, "required_score");
  871. is_spam = ucl_object_lookup (top, "is_spam");
  872. rspamd_printf_fstring (out,
  873. "Metric: default; %s; %.2f / %.2f / 0.0\r\n",
  874. ucl_object_toboolean (is_spam) ? "True" : "False",
  875. ucl_object_todouble (score),
  876. ucl_object_todouble (required_score));
  877. elt = ucl_object_lookup (top, "action");
  878. if (elt != NULL) {
  879. rspamd_printf_fstring (out, "Action: %s\r\n",
  880. ucl_object_tostring (elt));
  881. }
  882. elt = ucl_object_lookup (top, "subject");
  883. if (elt != NULL) {
  884. rspamd_printf_fstring (out, "Subject: %s\r\n",
  885. ucl_object_tostring (elt));
  886. }
  887. symbols = ucl_object_lookup (top, "symbols");
  888. if (symbols != NULL) {
  889. iter = NULL;
  890. while ((elt = ucl_object_iterate (symbols, &iter, true)) != NULL) {
  891. if (elt->type == UCL_OBJECT) {
  892. const ucl_object_t *sym_score;
  893. sym_score = ucl_object_lookup (elt, "score");
  894. rspamd_printf_fstring (out, "Symbol: %s(%.2f)\r\n",
  895. ucl_object_key (elt),
  896. ucl_object_todouble (sym_score));
  897. }
  898. }
  899. }
  900. elt = ucl_object_lookup (top, "messages");
  901. if (elt != NULL) {
  902. iter = NULL;
  903. while ((cur = ucl_object_iterate (elt, &iter, true)) != NULL) {
  904. if (cur->type == UCL_STRING) {
  905. rspamd_printf_fstring (out, "Message: %s\r\n",
  906. ucl_object_tostring (cur));
  907. }
  908. }
  909. }
  910. elt = ucl_object_lookup (top, "message-id");
  911. if (elt != NULL) {
  912. rspamd_printf_fstring (out, "Message-ID: %s\r\n",
  913. ucl_object_tostring (elt));
  914. }
  915. }
  916. void
  917. rspamd_ucl_tospamc_output (const ucl_object_t *top,
  918. rspamd_fstring_t **out)
  919. {
  920. const ucl_object_t *symbols, *score,
  921. *required_score, *is_spam, *elt;
  922. ucl_object_iter_t iter = NULL;
  923. rspamd_fstring_t *f;
  924. score = ucl_object_lookup (top, "score");
  925. required_score = ucl_object_lookup (top, "required_score");
  926. is_spam = ucl_object_lookup (top, "is_spam");
  927. rspamd_printf_fstring (out,
  928. "Spam: %s ; %.2f / %.2f\r\n\r\n",
  929. ucl_object_toboolean (is_spam) ? "True" : "False",
  930. ucl_object_todouble (score),
  931. ucl_object_todouble (required_score));
  932. symbols = ucl_object_lookup (top, "symbols");
  933. if (symbols != NULL) {
  934. while ((elt = ucl_object_iterate (symbols, &iter, true)) != NULL) {
  935. if (elt->type == UCL_OBJECT) {
  936. rspamd_printf_fstring (out, "%s,",
  937. ucl_object_key (elt));
  938. }
  939. }
  940. /* Ugly hack, but the whole spamc is ugly */
  941. f = *out;
  942. if (f->str[f->len - 1] == ',') {
  943. f->len --;
  944. *out = rspamd_fstring_append (*out, CRLF, 2);
  945. }
  946. }
  947. }
  948. static void
  949. rspamd_protocol_output_profiling (struct rspamd_task *task,
  950. ucl_object_t *top)
  951. {
  952. GHashTable *tbl;
  953. GHashTableIter it;
  954. gpointer k, v;
  955. ucl_object_t *prof;
  956. gdouble val;
  957. prof = ucl_object_typed_new (UCL_OBJECT);
  958. tbl = rspamd_mempool_get_variable (task->task_pool, "profile");
  959. if (tbl) {
  960. g_hash_table_iter_init (&it, tbl);
  961. while (g_hash_table_iter_next (&it, &k, &v)) {
  962. val = *(gdouble *)v;
  963. ucl_object_insert_key (prof, ucl_object_fromdouble (val),
  964. (const char *)k, 0, false);
  965. }
  966. }
  967. ucl_object_insert_key (top, prof, "profile", 0, false);
  968. }
  969. ucl_object_t *
  970. rspamd_protocol_write_ucl (struct rspamd_task *task,
  971. enum rspamd_protocol_flags flags)
  972. {
  973. ucl_object_t *top = NULL;
  974. GString *dkim_sig;
  975. const ucl_object_t *milter_reply;
  976. rspamd_task_set_finish_time (task);
  977. top = ucl_object_typed_new (UCL_OBJECT);
  978. rspamd_mempool_add_destructor (task->task_pool,
  979. (rspamd_mempool_destruct_t)ucl_object_unref, top);
  980. if (flags & RSPAMD_PROTOCOL_METRICS) {
  981. rspamd_metric_result_ucl (task, task->result, top);
  982. }
  983. if (flags & RSPAMD_PROTOCOL_MESSAGES) {
  984. if (G_UNLIKELY (task->cfg->compat_messages)) {
  985. const ucl_object_t *cur;
  986. ucl_object_t *msg_object;
  987. ucl_object_iter_t iter = NULL;
  988. msg_object = ucl_object_typed_new (UCL_ARRAY);
  989. while ((cur = ucl_object_iterate (task->messages, &iter, true)) != NULL) {
  990. if (cur->type == UCL_STRING) {
  991. ucl_array_append (msg_object, ucl_object_ref (cur));
  992. }
  993. }
  994. ucl_object_insert_key (top, msg_object, "messages", 0, false);
  995. }
  996. else {
  997. ucl_object_insert_key (top, ucl_object_ref (task->messages),
  998. "messages", 0, false);
  999. }
  1000. }
  1001. if (flags & RSPAMD_PROTOCOL_URLS) {
  1002. if (task->flags & RSPAMD_TASK_FLAG_EXT_URLS) {
  1003. if (g_hash_table_size (task->urls) > 0) {
  1004. ucl_object_insert_key (top,
  1005. rspamd_urls_tree_ucl (task->urls, task),
  1006. "urls", 0, false);
  1007. }
  1008. if (g_hash_table_size (task->emails) > 0) {
  1009. ucl_object_insert_key (top,
  1010. rspamd_emails_tree_ucl (task->emails, task),
  1011. "emails", 0, false);
  1012. }
  1013. }
  1014. }
  1015. if (flags & RSPAMD_PROTOCOL_EXTRA) {
  1016. if (G_UNLIKELY (RSPAMD_TASK_IS_PROFILING (task))) {
  1017. rspamd_protocol_output_profiling (task, top);
  1018. }
  1019. }
  1020. if (flags & RSPAMD_PROTOCOL_BASIC) {
  1021. ucl_object_insert_key (top, ucl_object_fromstring (task->message_id),
  1022. "message-id", 0, false);
  1023. ucl_object_insert_key (top,
  1024. ucl_object_fromdouble (task->time_real_finish - task->time_real),
  1025. "time_real", 0, false);
  1026. ucl_object_insert_key (top,
  1027. ucl_object_fromdouble (task->time_virtual_finish - task->time_virtual),
  1028. "time_virtual", 0, false);
  1029. }
  1030. if (flags & RSPAMD_PROTOCOL_DKIM) {
  1031. dkim_sig = rspamd_mempool_get_variable (task->task_pool,
  1032. RSPAMD_MEMPOOL_DKIM_SIGNATURE);
  1033. if (dkim_sig) {
  1034. GString *folded_header;
  1035. if (task->flags & RSPAMD_TASK_FLAG_MILTER) {
  1036. folded_header = rspamd_header_value_fold ("DKIM-Signature",
  1037. dkim_sig->str, 80, RSPAMD_TASK_NEWLINES_LF, NULL);
  1038. }
  1039. else {
  1040. folded_header = rspamd_header_value_fold ("DKIM-Signature",
  1041. dkim_sig->str, 80, task->nlines_type, NULL);
  1042. }
  1043. /*
  1044. * According to milter docs, we need to be extra careful
  1045. * when folding headers:
  1046. * Neither the name nor the value of the header is checked for standards
  1047. * compliance. However, each line of the header must be under 2048
  1048. * characters and should be under 998 characters.
  1049. * If longer headers are needed, make them multi-line.
  1050. * To make a multi-line header, insert a line feed (ASCII 0x0a, or \n
  1051. * in C) followed by at least one whitespace character such as a
  1052. * space (ASCII 0x20) or tab (ASCII 0x09, or \t in C).
  1053. * The line feed should NOT be preceded by a carriage return (ASCII 0x0d);
  1054. * the MTA will add this automatically.
  1055. * It is the filter writer's responsibility to ensure that no
  1056. * standards are violated.
  1057. */
  1058. ucl_object_insert_key (top,
  1059. ucl_object_fromstring_common (folded_header->str,
  1060. folded_header->len, UCL_STRING_RAW),
  1061. "dkim-signature", 0, false);
  1062. g_string_free (folded_header, TRUE);
  1063. }
  1064. }
  1065. if (flags & RSPAMD_PROTOCOL_RMILTER) {
  1066. milter_reply = rspamd_mempool_get_variable (task->task_pool,
  1067. RSPAMD_MEMPOOL_MILTER_REPLY);
  1068. if (milter_reply) {
  1069. if (task->cmd == CMD_CHECK_V2) {
  1070. ucl_object_insert_key (top, ucl_object_ref (milter_reply),
  1071. "milter", 0, false);
  1072. }
  1073. else {
  1074. ucl_object_insert_key (top, ucl_object_ref (milter_reply),
  1075. "rmilter", 0, false);
  1076. }
  1077. }
  1078. }
  1079. return top;
  1080. }
  1081. void
  1082. rspamd_protocol_http_reply (struct rspamd_http_message *msg,
  1083. struct rspamd_task *task, ucl_object_t **pobj)
  1084. {
  1085. struct rspamd_metric_result *metric_res;
  1086. GHashTableIter hiter;
  1087. const struct rspamd_re_cache_stat *restat;
  1088. gpointer h, v;
  1089. ucl_object_t *top = NULL;
  1090. rspamd_fstring_t *reply;
  1091. gint flags = RSPAMD_PROTOCOL_DEFAULT;
  1092. struct rspamd_action *action;
  1093. /* Write custom headers */
  1094. g_hash_table_iter_init (&hiter, task->reply_headers);
  1095. while (g_hash_table_iter_next (&hiter, &h, &v)) {
  1096. rspamd_ftok_t *hn = h, *hv = v;
  1097. rspamd_http_message_add_header (msg, hn->begin, hv->begin);
  1098. }
  1099. if (task->cfg->log_urls || (task->flags & RSPAMD_TASK_FLAG_EXT_URLS)) {
  1100. flags |= RSPAMD_PROTOCOL_URLS;
  1101. }
  1102. top = rspamd_protocol_write_ucl (task, flags);
  1103. if (pobj) {
  1104. *pobj = top;
  1105. }
  1106. if (!(task->flags & RSPAMD_TASK_FLAG_NO_LOG)) {
  1107. rspamd_roll_history_update (task->worker->srv->history, task);
  1108. }
  1109. rspamd_task_write_log (task);
  1110. if (task->cfg->log_flags & RSPAMD_LOG_FLAG_RE_CACHE) {
  1111. restat = rspamd_re_cache_get_stat (task->re_rt);
  1112. g_assert (restat != NULL);
  1113. msg_notice_task (
  1114. "regexp statistics: %ud pcre regexps scanned, %ud regexps matched,"
  1115. " %ud regexps total, %ud regexps cached,"
  1116. " %HL bytes scanned using pcre, %HL bytes scanned total",
  1117. restat->regexp_checked,
  1118. restat->regexp_matched,
  1119. restat->regexp_total,
  1120. restat->regexp_fast_cached,
  1121. restat->bytes_scanned_pcre,
  1122. restat->bytes_scanned);
  1123. }
  1124. reply = rspamd_fstring_sized_new (1000);
  1125. if (msg->method < HTTP_SYMBOLS && !RSPAMD_TASK_IS_SPAMC (task)) {
  1126. rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply);
  1127. }
  1128. else {
  1129. if (RSPAMD_TASK_IS_SPAMC (task)) {
  1130. rspamd_ucl_tospamc_output (top, &reply);
  1131. }
  1132. else {
  1133. rspamd_ucl_torspamc_output (top, &reply);
  1134. }
  1135. }
  1136. if ((task->flags & RSPAMD_TASK_FLAG_COMPRESSED) &&
  1137. rspamd_libs_reset_compression (task->cfg->libs_ctx)) {
  1138. /* We can compress output */
  1139. ZSTD_inBuffer zin;
  1140. ZSTD_outBuffer zout;
  1141. ZSTD_CStream *zstream;
  1142. rspamd_fstring_t *compressed_reply;
  1143. gsize r;
  1144. zstream = task->cfg->libs_ctx->out_zstream;
  1145. compressed_reply = rspamd_fstring_sized_new (ZSTD_compressBound (reply->len));
  1146. zin.pos = 0;
  1147. zin.src = reply->str;
  1148. zin.size = reply->len;
  1149. zout.pos = 0;
  1150. zout.dst = compressed_reply->str;
  1151. zout.size = compressed_reply->allocated;
  1152. while (zin.pos < zin.size) {
  1153. r = ZSTD_compressStream (zstream, &zout, &zin);
  1154. if (ZSTD_isError (r)) {
  1155. msg_err_protocol ("cannot compress: %s", ZSTD_getErrorName (r));
  1156. rspamd_fstring_free (compressed_reply);
  1157. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1158. goto end;
  1159. }
  1160. }
  1161. ZSTD_flushStream (zstream, &zout);
  1162. r = ZSTD_endStream (zstream, &zout);
  1163. if (ZSTD_isError (r)) {
  1164. msg_err_protocol ("cannot finalize compress: %s", ZSTD_getErrorName (r));
  1165. rspamd_fstring_free (compressed_reply);
  1166. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1167. goto end;
  1168. }
  1169. msg_info_protocol ("writing compressed results: %z bytes before "
  1170. "%z bytes after", zin.pos, zout.pos);
  1171. compressed_reply->len = zout.pos;
  1172. rspamd_fstring_free (reply);
  1173. rspamd_http_message_set_body_from_fstring_steal (msg, compressed_reply);
  1174. rspamd_http_message_add_header (msg, "Compression", "zstd");
  1175. if (task->cfg->libs_ctx->out_dict &&
  1176. task->cfg->libs_ctx->out_dict->id != 0) {
  1177. gchar dict_str[32];
  1178. rspamd_snprintf (dict_str, sizeof (dict_str), "%ud",
  1179. task->cfg->libs_ctx->out_dict->id);
  1180. rspamd_http_message_add_header (msg, "Dictionary", dict_str);
  1181. }
  1182. }
  1183. else {
  1184. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1185. }
  1186. end:
  1187. if (!(task->flags & RSPAMD_TASK_FLAG_NO_STAT)) {
  1188. /* Update stat for default metric */
  1189. metric_res = task->result;
  1190. if (metric_res != NULL) {
  1191. action = rspamd_check_action_metric (task);
  1192. /* TODO: handle custom actions in stats */
  1193. if (action->action_type == METRIC_ACTION_SOFT_REJECT &&
  1194. (task->flags & RSPAMD_TASK_FLAG_GREYLISTED)) {
  1195. /* Set stat action to greylist to display greylisted messages */
  1196. #ifndef HAVE_ATOMIC_BUILTINS
  1197. task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST]++;
  1198. #else
  1199. __atomic_add_fetch (&task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST],
  1200. 1, __ATOMIC_RELEASE);
  1201. #endif
  1202. }
  1203. else if (action->action_type < METRIC_ACTION_MAX) {
  1204. #ifndef HAVE_ATOMIC_BUILTINS
  1205. task->worker->srv->stat->actions_stat[action->action_type]++;
  1206. #else
  1207. __atomic_add_fetch (&task->worker->srv->stat->actions_stat[action->action_type],
  1208. 1, __ATOMIC_RELEASE);
  1209. #endif
  1210. }
  1211. }
  1212. /* Increase counters */
  1213. #ifndef HAVE_ATOMIC_BUILTINS
  1214. task->worker->srv->stat->messages_scanned++;
  1215. #else
  1216. __atomic_add_fetch (&task->worker->srv->stat->messages_scanned,
  1217. 1, __ATOMIC_RELEASE);
  1218. #endif
  1219. }
  1220. }
  1221. void
  1222. rspamd_protocol_write_log_pipe (struct rspamd_task *task)
  1223. {
  1224. struct rspamd_worker_log_pipe *lp;
  1225. struct rspamd_protocol_log_message_sum *ls;
  1226. lua_State *L = task->cfg->lua_state;
  1227. struct rspamd_metric_result *mres;
  1228. struct rspamd_symbol_result *sym;
  1229. gint id, i;
  1230. guint32 *sid, n = 0, nextra = 0;
  1231. gsize sz;
  1232. GArray *extra;
  1233. struct rspamd_protocol_log_symbol_result er;
  1234. struct rspamd_task **ptask;
  1235. /* Get extra results from lua plugins */
  1236. extra = g_array_new (FALSE, FALSE, sizeof (er));
  1237. lua_getglobal (L, "rspamd_plugins");
  1238. if (lua_istable (L, -1)) {
  1239. lua_pushnil (L);
  1240. while (lua_next (L, -2)) {
  1241. if (lua_istable (L, -1)) {
  1242. lua_pushvalue (L, -2);
  1243. /* stack:
  1244. * -1: copy of key
  1245. * -2: value (module table)
  1246. * -3: key (module name)
  1247. * -4: global
  1248. */
  1249. lua_pushstring (L, "log_callback");
  1250. lua_gettable (L, -3);
  1251. /* stack:
  1252. * -1: func
  1253. * -2: copy of key
  1254. * -3: value (module table)
  1255. * -3: key (module name)
  1256. * -4: global
  1257. */
  1258. if (lua_isfunction (L, -1)) {
  1259. ptask = lua_newuserdata (L, sizeof (*ptask));
  1260. *ptask = task;
  1261. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1262. /* stack:
  1263. * -1: task
  1264. * -2: func
  1265. * -3: key copy
  1266. * -4: value (module table)
  1267. * -5: key (module name)
  1268. * -6: global
  1269. */
  1270. msg_debug_protocol ("calling for %s", lua_tostring (L, -3));
  1271. if (lua_pcall (L, 1, 1, 0) != 0) {
  1272. msg_info_protocol ("call to log callback %s failed: %s",
  1273. lua_tostring (L, -2), lua_tostring (L, -1));
  1274. lua_pop (L, 1);
  1275. /* stack:
  1276. * -1: key copy
  1277. * -2: value
  1278. * -3: key
  1279. */
  1280. }
  1281. else {
  1282. /* stack:
  1283. * -1: result
  1284. * -2: key copy
  1285. * -3: value
  1286. * -4: key
  1287. */
  1288. if (lua_istable (L, -1)) {
  1289. /* Another iteration */
  1290. lua_pushnil (L);
  1291. while (lua_next (L, -2)) {
  1292. /* stack:
  1293. * -1: value
  1294. * -2: key
  1295. * -3: result table (pcall)
  1296. * -4: key copy (parent)
  1297. * -5: value (parent)
  1298. * -6: key (parent)
  1299. */
  1300. if (lua_istable (L, -1)) {
  1301. er.id = 0;
  1302. er.score = 0.0;
  1303. lua_rawgeti (L, -1, 1);
  1304. if (lua_isnumber (L, -1)) {
  1305. er.id = lua_tonumber (L, -1);
  1306. }
  1307. lua_rawgeti (L, -2, 2);
  1308. if (lua_isnumber (L, -1)) {
  1309. er.score = lua_tonumber (L, -1);
  1310. }
  1311. /* stack:
  1312. * -1: value[2]
  1313. * -2: value[1]
  1314. * -3: values
  1315. * -4: key
  1316. * -5: result table (pcall)
  1317. * -6: key copy (parent)
  1318. * -7: value (parent)
  1319. * -8: key (parent)
  1320. */
  1321. lua_pop (L, 2); /* Values */
  1322. g_array_append_val (extra, er);
  1323. }
  1324. lua_pop (L, 1); /* Value for lua_next */
  1325. }
  1326. lua_pop (L, 1); /* Table result of pcall */
  1327. }
  1328. else {
  1329. msg_info_protocol ("call to log callback %s returned "
  1330. "wrong type: %s",
  1331. lua_tostring (L, -2),
  1332. lua_typename (L, lua_type (L, -1)));
  1333. lua_pop (L, 1); /* Returned error */
  1334. }
  1335. }
  1336. }
  1337. else {
  1338. lua_pop (L, 1);
  1339. /* stack:
  1340. * -1: key copy
  1341. * -2: value
  1342. * -3: key
  1343. */
  1344. }
  1345. }
  1346. lua_pop (L, 2); /* Top table + key copy */
  1347. }
  1348. lua_pop (L, 1); /* rspamd_plugins global */
  1349. }
  1350. else {
  1351. lua_pop (L, 1);
  1352. }
  1353. nextra = extra->len;
  1354. LL_FOREACH (task->cfg->log_pipes, lp) {
  1355. if (lp->fd != -1) {
  1356. switch (lp->type) {
  1357. case RSPAMD_LOG_PIPE_SYMBOLS:
  1358. mres = task->result;
  1359. if (mres) {
  1360. n = kh_size (mres->symbols);
  1361. sz = sizeof (*ls) +
  1362. sizeof (struct rspamd_protocol_log_symbol_result) *
  1363. (n + nextra);
  1364. ls = g_malloc0 (sz);
  1365. /* Handle settings id */
  1366. sid = rspamd_mempool_get_variable (task->task_pool,
  1367. "settings_hash");
  1368. if (sid) {
  1369. ls->settings_id = *sid;
  1370. }
  1371. else {
  1372. ls->settings_id = 0;
  1373. }
  1374. ls->score = mres->score;
  1375. ls->required_score = rspamd_task_get_required_score (task,
  1376. mres);
  1377. ls->nresults = n;
  1378. ls->nextra = nextra;
  1379. i = 0;
  1380. kh_foreach_value_ptr (mres->symbols, sym, {
  1381. id = rspamd_symcache_find_symbol (task->cfg->cache,
  1382. sym->name);
  1383. if (id >= 0) {
  1384. ls->results[i].id = id;
  1385. ls->results[i].score = sym->score;
  1386. }
  1387. else {
  1388. ls->results[i].id = -1;
  1389. ls->results[i].score = 0.0;
  1390. }
  1391. i ++;
  1392. });
  1393. memcpy (&ls->results[n], extra->data, nextra * sizeof (er));
  1394. }
  1395. else {
  1396. sz = sizeof (*ls);
  1397. ls = g_malloc0 (sz);
  1398. ls->nresults = 0;
  1399. }
  1400. /* We don't really care about return value here */
  1401. if (write (lp->fd, ls, sz) == -1) {
  1402. msg_info_protocol ("cannot write to log pipe: %s",
  1403. strerror (errno));
  1404. }
  1405. g_free (ls);
  1406. break;
  1407. default:
  1408. msg_err_protocol ("unknown log format %d", lp->type);
  1409. break;
  1410. }
  1411. }
  1412. }
  1413. g_array_free (extra, TRUE);
  1414. }
  1415. void
  1416. rspamd_protocol_write_reply (struct rspamd_task *task)
  1417. {
  1418. struct rspamd_http_message *msg;
  1419. const gchar *ctype = "application/json";
  1420. rspamd_fstring_t *reply;
  1421. msg = rspamd_http_new_message (HTTP_RESPONSE);
  1422. if (rspamd_http_connection_is_encrypted (task->http_conn)) {
  1423. msg_info_protocol ("<%s> writing encrypted reply", task->message_id);
  1424. }
  1425. if (!RSPAMD_TASK_IS_JSON (task)) {
  1426. /* Turn compatibility on */
  1427. msg->method = HTTP_SYMBOLS;
  1428. }
  1429. if (RSPAMD_TASK_IS_SPAMC (task)) {
  1430. msg->flags |= RSPAMD_HTTP_FLAG_SPAMC;
  1431. }
  1432. msg->date = time (NULL);
  1433. msg_debug_protocol ("writing reply to client");
  1434. if (task->err != NULL) {
  1435. ucl_object_t *top = NULL;
  1436. top = ucl_object_typed_new (UCL_OBJECT);
  1437. msg->code = 500 + task->err->code % 100;
  1438. msg->status = rspamd_fstring_new_init (task->err->message,
  1439. strlen (task->err->message));
  1440. ucl_object_insert_key (top, ucl_object_fromstring (task->err->message),
  1441. "error", 0, false);
  1442. ucl_object_insert_key (top,
  1443. ucl_object_fromstring (g_quark_to_string (task->err->domain)),
  1444. "error_domain", 0, false);
  1445. reply = rspamd_fstring_sized_new (256);
  1446. rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply);
  1447. ucl_object_unref (top);
  1448. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  1449. }
  1450. else {
  1451. msg->status = rspamd_fstring_new_init ("OK", 2);
  1452. switch (task->cmd) {
  1453. case CMD_REPORT_IFSPAM:
  1454. case CMD_REPORT:
  1455. case CMD_CHECK:
  1456. case CMD_SYMBOLS:
  1457. case CMD_PROCESS:
  1458. case CMD_SKIP:
  1459. case CMD_CHECK_V2:
  1460. rspamd_protocol_http_reply (msg, task, NULL);
  1461. rspamd_protocol_write_log_pipe (task);
  1462. break;
  1463. case CMD_PING:
  1464. rspamd_http_message_set_body (msg, "pong" CRLF, 6);
  1465. ctype = "text/plain";
  1466. break;
  1467. case CMD_OTHER:
  1468. msg_err_protocol ("BROKEN");
  1469. break;
  1470. }
  1471. }
  1472. rspamd_http_connection_reset (task->http_conn);
  1473. rspamd_http_connection_write_message (task->http_conn, msg, NULL,
  1474. ctype, task, task->sock, &task->tv, task->ev_base);
  1475. task->processed_stages |= RSPAMD_TASK_STAGE_REPLIED;
  1476. }