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.

surbl.c 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * Copyright (c) 2009, Rambler media
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY Rambler media ''AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL Rambler BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /***MODULE:surbl
  25. * rspamd module that implements SURBL url checking
  26. *
  27. * Allowed options:
  28. * - weight (integer): weight of symbol
  29. * Redirecotor options:
  30. * - redirector (string): address of http redirector utility in format "host:port"
  31. * - redirector_connect_timeout (seconds): redirector connect timeout (default: 1s)
  32. * - redirector_read_timeout (seconds): timeout for reading data (default: 5s)
  33. * - redirector_hosts_map (map string): map that contains domains to check with redirector
  34. * Surbl options:
  35. * - exceptions (map string): map of domains that should be checked via surbl using 3 (e.g. somehost.domain.com)
  36. * components of domain name instead of normal 2 (e.g. domain.com)
  37. * - whitelist (map string): map of domains that should be whitelisted for surbl checks
  38. * - max_urls (integer): maximum allowed number of urls in message to be checked
  39. * - suffix (string): surbl address (for example insecure-bl.rambler.ru), may contain %b if bits are used (read documentation about it)
  40. * - bit (string): describes a prefix for a single bit
  41. */
  42. #include "config.h"
  43. #include "main.h"
  44. #include "message.h"
  45. #include "cfg_file.h"
  46. #include "expressions.h"
  47. #include "util.h"
  48. #include "view.h"
  49. #include "map.h"
  50. #include "dns.h"
  51. #include "cfg_xml.h"
  52. #include "hash.h"
  53. #include "surbl.h"
  54. static struct surbl_ctx *surbl_module_ctx = NULL;
  55. static gint surbl_filter (struct worker_task *task);
  56. static void surbl_test_url (struct worker_task *task, void *user_data);
  57. static void dns_callback (struct rspamd_dns_reply *reply, gpointer arg);
  58. static void process_dns_results (struct worker_task *task, struct suffix_item *suffix, gchar *url, guint32 addr);
  59. static gint urls_command_handler (struct worker_task *task);
  60. #define NO_REGEXP (gpointer)-1
  61. #define SURBL_ERROR surbl_error_quark ()
  62. #define WHITELIST_ERROR 0
  63. #define CONVERSION_ERROR 1
  64. GQuark
  65. surbl_error_quark (void)
  66. {
  67. return g_quark_from_static_string ("surbl-error-quark");
  68. }
  69. /* Initialization */
  70. gint surbl_module_init (struct config_file *cfg, struct module_ctx **ctx);
  71. gint surbl_module_config (struct config_file *cfg);
  72. gint surbl_module_reconfig (struct config_file *cfg);
  73. module_t surbl_module = {
  74. "surbl",
  75. surbl_module_init,
  76. surbl_module_config,
  77. surbl_module_reconfig
  78. };
  79. static void
  80. exception_insert (gpointer st, gconstpointer key, gpointer value)
  81. {
  82. GHashTable **t = st;
  83. gint level = 0;
  84. const gchar *p = key;
  85. f_str_t *val;
  86. while (*p) {
  87. if (*p == '.') {
  88. level ++;
  89. }
  90. p ++;
  91. }
  92. if (level >= MAX_LEVELS) {
  93. msg_err ("invalid domain in exceptions list: %s, levels: %d", (gchar *)key, level);
  94. return;
  95. }
  96. val = g_malloc (sizeof (f_str_t));
  97. val->begin = (gchar *)key;
  98. val->len = strlen (key);
  99. if (t[level] == NULL) {
  100. t[level] = g_hash_table_new_full (fstr_strcase_hash, fstr_strcase_equal, g_free, NULL);
  101. }
  102. g_hash_table_insert (t[level], val, value);
  103. }
  104. static gchar *
  105. read_exceptions_list (memory_pool_t * pool, gchar * chunk, gint len, struct map_cb_data *data)
  106. {
  107. if (data->cur_data == NULL) {
  108. data->cur_data = memory_pool_alloc0 (pool, sizeof (GHashTable *) * MAX_LEVELS);
  109. }
  110. return abstract_parse_list (pool, chunk, len, data, (insert_func) exception_insert);
  111. }
  112. static void
  113. fin_exceptions_list (memory_pool_t * pool, struct map_cb_data *data)
  114. {
  115. GHashTable **t;
  116. gint i;
  117. if (data->prev_data) {
  118. t = data->prev_data;
  119. for (i = 0; i < MAX_LEVELS; i ++) {
  120. if (t[i] != NULL) {
  121. g_hash_table_destroy (t[i]);
  122. }
  123. }
  124. }
  125. }
  126. static void
  127. redirector_insert (gpointer st, gconstpointer key, gpointer value)
  128. {
  129. GHashTable *t = st;
  130. const gchar *p = key, *begin = key;
  131. gchar *new;
  132. gsize len;
  133. GRegex *re = NO_REGEXP;
  134. GError *err = NULL;
  135. guint idx;
  136. while (*p && !g_ascii_isspace (*p)) {
  137. p ++;
  138. }
  139. len = p - begin;
  140. new = g_malloc (len + 1);
  141. memcpy (new, begin, len);
  142. new[len] = '\0';
  143. idx = surbl_module_ctx->redirector_ptrs->len;
  144. rspamd_trie_insert (surbl_module_ctx->redirector_trie, new, idx);
  145. g_ptr_array_add (surbl_module_ctx->redirector_ptrs, new);
  146. if (g_ascii_isspace (*p)) {
  147. while (g_ascii_isspace (*p) && *p) {
  148. p ++;
  149. }
  150. if (*p) {
  151. re = g_regex_new (p, G_REGEX_RAW | G_REGEX_OPTIMIZE | G_REGEX_NO_AUTO_CAPTURE | G_REGEX_CASELESS,
  152. 0, &err);
  153. if (re == NULL) {
  154. msg_warn ("could not read regexp: %s while reading regexp %s", err->message, p);
  155. re = NO_REGEXP;
  156. }
  157. }
  158. }
  159. g_hash_table_insert (t, new, re);
  160. }
  161. static void
  162. redirector_item_free (gpointer p)
  163. {
  164. GRegex *re;
  165. if (p != NULL && p != NO_REGEXP) {
  166. re = (GRegex *)p;
  167. g_regex_unref (re);
  168. }
  169. }
  170. static gchar *
  171. read_redirectors_list (memory_pool_t * pool, gchar * chunk, gint len, struct map_cb_data *data)
  172. {
  173. if (data->cur_data == NULL) {
  174. data->cur_data = g_hash_table_new_full (rspamd_strcase_hash, rspamd_strcase_equal, g_free, redirector_item_free);
  175. }
  176. return abstract_parse_list (pool, chunk, len, data, (insert_func) redirector_insert);
  177. }
  178. void
  179. fin_redirectors_list (memory_pool_t * pool, struct map_cb_data *data)
  180. {
  181. if (data->prev_data) {
  182. g_hash_table_destroy (data->prev_data);
  183. }
  184. }
  185. gint
  186. surbl_module_init (struct config_file *cfg, struct module_ctx **ctx)
  187. {
  188. surbl_module_ctx = g_malloc (sizeof (struct surbl_ctx));
  189. surbl_module_ctx->filter = surbl_filter;
  190. surbl_module_ctx->use_redirector = 0;
  191. surbl_module_ctx->suffixes = NULL;
  192. surbl_module_ctx->bits = NULL;
  193. surbl_module_ctx->surbl_pool = memory_pool_new (memory_pool_get_size ());
  194. surbl_module_ctx->tld2_file = NULL;
  195. surbl_module_ctx->whitelist_file = NULL;
  196. surbl_module_ctx->redirectors_number = 0;
  197. surbl_module_ctx->redirector_trie = rspamd_trie_create (TRUE);
  198. surbl_module_ctx->redirector_ptrs = g_ptr_array_new ();
  199. surbl_module_ctx->redirector_hosts = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
  200. surbl_module_ctx->whitelist = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
  201. /* Zero exceptions hashes */
  202. surbl_module_ctx->exceptions = memory_pool_alloc0 (surbl_module_ctx->surbl_pool, MAX_LEVELS * sizeof (GHashTable *));
  203. /* Register destructors */
  204. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_hash_table_destroy, surbl_module_ctx->whitelist);
  205. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_hash_table_destroy, surbl_module_ctx->redirector_hosts);
  206. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) rspamd_trie_free, surbl_module_ctx->redirector_trie);
  207. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_ptr_array_unref, surbl_module_ctx->redirector_ptrs);
  208. *ctx = (struct module_ctx *)surbl_module_ctx;
  209. register_protocol_command ("urls", urls_command_handler);
  210. /* Register module options */
  211. register_module_opt ("surbl", "redirector", MODULE_OPT_TYPE_STRING);
  212. register_module_opt ("surbl", "redirector_symbol", MODULE_OPT_TYPE_STRING);
  213. register_module_opt ("surbl", "url_expire", MODULE_OPT_TYPE_TIME);
  214. register_module_opt ("surbl", "redirector_connect_timeout", MODULE_OPT_TYPE_TIME);
  215. register_module_opt ("surbl", "redirector_read_timeout", MODULE_OPT_TYPE_TIME);
  216. register_module_opt ("surbl", "max_urls", MODULE_OPT_TYPE_UINT);
  217. register_module_opt ("surbl", "redirector_hosts_map", MODULE_OPT_TYPE_STRING);
  218. register_module_opt ("surbl", "exceptions", MODULE_OPT_TYPE_STRING);
  219. register_module_opt ("surbl", "whitelist", MODULE_OPT_TYPE_STRING);
  220. register_module_opt ("surbl", "/^suffix_.*$/", MODULE_OPT_TYPE_STRING);
  221. register_module_opt ("surbl", "/^options_.*$/", MODULE_OPT_TYPE_STRING);
  222. register_module_opt ("surbl", "/^bit_.*$/", MODULE_OPT_TYPE_STRING);
  223. return 0;
  224. }
  225. /*
  226. * Register virtual symbols for suffixes with bit wildcard
  227. */
  228. static void
  229. register_bit_symbols (struct config_file *cfg)
  230. {
  231. gchar *c, *symbol;
  232. GList *symit, *cur;
  233. struct surbl_bit_item *bit;
  234. struct suffix_item *suffix;
  235. gint len;
  236. symit = surbl_module_ctx->suffixes;
  237. while (symit) {
  238. suffix = symit->data;
  239. if ((c = strchr (suffix->symbol, '%')) != NULL && *(c + 1) == 'b') {
  240. cur = g_list_first (surbl_module_ctx->bits);
  241. while (cur) {
  242. bit = (struct surbl_bit_item *)cur->data;
  243. len = strlen (suffix->symbol) - 2 + strlen (bit->symbol) + 1;
  244. *c = '\0';
  245. symbol = memory_pool_alloc (cfg->cfg_pool, len);
  246. rspamd_snprintf (symbol, len, "%s%s%s", suffix->symbol, bit->symbol, c + 2);
  247. *c = '%';
  248. register_virtual_symbol (&cfg->cache, symbol, 1);
  249. cur = g_list_next (cur);
  250. }
  251. }
  252. else {
  253. register_virtual_symbol (&cfg->cache, suffix->symbol, 1);
  254. }
  255. symit = g_list_next (symit);
  256. }
  257. }
  258. gint
  259. surbl_module_config (struct config_file *cfg)
  260. {
  261. GList *cur_opt;
  262. struct module_opt *cur;
  263. struct suffix_item *new_suffix;
  264. struct surbl_bit_item *new_bit;
  265. gchar *value, *str, **strvec, *optbuf;
  266. guint32 bit;
  267. gint i, idx;
  268. if ((value = get_module_opt (cfg, "surbl", "redirector")) != NULL) {
  269. strvec = g_strsplit_set (value, ",;", -1);
  270. i = g_strv_length (strvec);
  271. surbl_module_ctx->redirectors = memory_pool_alloc0 (surbl_module_ctx->surbl_pool,
  272. i * sizeof (struct redirector_upstream));
  273. idx = 0;
  274. i --;
  275. for (; i >= 0; i --) {
  276. if (! parse_host_port (strvec[i], &surbl_module_ctx->redirectors[idx].ina,
  277. &surbl_module_ctx->redirectors[idx].port)) {
  278. msg_warn ("invalid redirector definition: %s", strvec[idx]);
  279. }
  280. else {
  281. if (surbl_module_ctx->redirectors[idx].port != 0) {
  282. surbl_module_ctx->redirectors[idx].name = memory_pool_strdup (surbl_module_ctx->surbl_pool, strvec[i]);
  283. surbl_module_ctx->redirectors[idx].up.priority = 100;
  284. msg_info ("add redirector %s", surbl_module_ctx->redirectors[idx].name);
  285. idx ++;
  286. }
  287. }
  288. }
  289. surbl_module_ctx->redirectors_number = idx;
  290. surbl_module_ctx->use_redirector = (surbl_module_ctx->redirectors_number != 0);
  291. g_strfreev (strvec);
  292. }
  293. if ((value = get_module_opt (cfg, "surbl", "redirector_symbol")) != NULL) {
  294. surbl_module_ctx->redirector_symbol = memory_pool_strdup (surbl_module_ctx->surbl_pool, value);
  295. register_virtual_symbol (&cfg->cache, surbl_module_ctx->redirector_symbol, 1.0);
  296. }
  297. else {
  298. surbl_module_ctx->redirector_symbol = NULL;
  299. }
  300. if ((value = get_module_opt (cfg, "surbl", "weight")) != NULL) {
  301. surbl_module_ctx->weight = atoi (value);
  302. }
  303. else {
  304. surbl_module_ctx->weight = DEFAULT_SURBL_WEIGHT;
  305. }
  306. if ((value = get_module_opt (cfg, "surbl", "url_expire")) != NULL) {
  307. surbl_module_ctx->url_expire = cfg_parse_time (value, TIME_SECONDS) / 1000;
  308. }
  309. else {
  310. surbl_module_ctx->url_expire = DEFAULT_SURBL_URL_EXPIRE;
  311. }
  312. if ((value = get_module_opt (cfg, "surbl", "redirector_connect_timeout")) != NULL) {
  313. surbl_module_ctx->connect_timeout = cfg_parse_time (value, TIME_SECONDS);
  314. }
  315. else {
  316. surbl_module_ctx->connect_timeout = DEFAULT_REDIRECTOR_CONNECT_TIMEOUT;
  317. }
  318. if ((value = get_module_opt (cfg, "surbl", "redirector_read_timeout")) != NULL) {
  319. surbl_module_ctx->read_timeout = cfg_parse_time (value, TIME_SECONDS);
  320. }
  321. else {
  322. surbl_module_ctx->read_timeout = DEFAULT_REDIRECTOR_READ_TIMEOUT;
  323. }
  324. if ((value = get_module_opt (cfg, "surbl", "redirector_hosts_map")) != NULL) {
  325. add_map (value, read_redirectors_list, fin_redirectors_list, (void **)&surbl_module_ctx->redirector_hosts);
  326. }
  327. else {
  328. surbl_module_ctx->read_timeout = DEFAULT_REDIRECTOR_READ_TIMEOUT;
  329. }
  330. if ((value = get_module_opt (cfg, "surbl", "max_urls")) != NULL) {
  331. surbl_module_ctx->max_urls = atoi (value);
  332. }
  333. else {
  334. surbl_module_ctx->max_urls = DEFAULT_SURBL_MAX_URLS;
  335. }
  336. if ((value = get_module_opt (cfg, "surbl", "exceptions")) != NULL) {
  337. if (add_map (value, read_exceptions_list, fin_exceptions_list, (void **)&surbl_module_ctx->exceptions)) {
  338. surbl_module_ctx->tld2_file = memory_pool_strdup (surbl_module_ctx->surbl_pool, value + sizeof ("file://") - 1);
  339. }
  340. }
  341. if ((value = get_module_opt (cfg, "surbl", "whitelist")) != NULL) {
  342. if (add_map (value, read_host_list, fin_host_list, (void **)&surbl_module_ctx->whitelist)) {
  343. surbl_module_ctx->whitelist_file = memory_pool_strdup (surbl_module_ctx->surbl_pool, value + sizeof ("file://") - 1);
  344. }
  345. }
  346. cur_opt = g_hash_table_lookup (cfg->modules_opts, "surbl");
  347. while (cur_opt) {
  348. cur = cur_opt->data;
  349. if (!g_strncasecmp (cur->param, "suffix", sizeof ("suffix") - 1)) {
  350. if ((str = strchr (cur->param, '_')) != NULL) {
  351. new_suffix = memory_pool_alloc (surbl_module_ctx->surbl_pool, sizeof (struct suffix_item));
  352. *str = '\0';
  353. new_suffix->symbol = memory_pool_strdup (surbl_module_ctx->surbl_pool, str + 1);
  354. new_suffix->suffix = memory_pool_strdup (surbl_module_ctx->surbl_pool, cur->value);
  355. new_suffix->options = 0;
  356. *str = '_';
  357. /* Search for options */
  358. i = strlen (new_suffix->symbol) + sizeof ("options_");
  359. optbuf = g_malloc (i);
  360. rspamd_snprintf (optbuf, i, "options_%s", new_suffix->symbol);
  361. if ((value = get_module_opt (cfg, "surbl", optbuf)) != NULL) {
  362. if (strstr (value, "noip") != NULL) {
  363. new_suffix->options |= SURBL_OPTION_NOIP;
  364. }
  365. }
  366. g_free (optbuf);
  367. /* Insert suffix to the list */
  368. msg_debug ("add new surbl suffix: %s with symbol: %s", new_suffix->suffix, new_suffix->symbol);
  369. surbl_module_ctx->suffixes = g_list_prepend (surbl_module_ctx->suffixes, new_suffix);
  370. register_callback_symbol (&cfg->cache, new_suffix->symbol, 1, surbl_test_url, new_suffix);
  371. }
  372. }
  373. /* Search for bits */
  374. else if (!g_strncasecmp (cur->param, "bit", sizeof ("bit") - 1)) {
  375. if ((str = strchr (cur->param, '_')) != NULL) {
  376. bit = strtoul (str + 1, NULL, 10);
  377. if (bit != 0) {
  378. new_bit = memory_pool_alloc (surbl_module_ctx->surbl_pool, sizeof (struct surbl_bit_item));
  379. new_bit->bit = bit;
  380. new_bit->symbol = memory_pool_strdup (surbl_module_ctx->surbl_pool, cur->value);
  381. msg_debug ("add new bit suffix: %d with symbol: %s", (gint)new_bit->bit, new_bit->symbol);
  382. surbl_module_ctx->bits = g_list_prepend (surbl_module_ctx->bits, new_bit);
  383. }
  384. }
  385. }
  386. cur_opt = g_list_next (cur_opt);
  387. }
  388. /* Add default suffix */
  389. if (surbl_module_ctx->suffixes == NULL) {
  390. new_suffix = memory_pool_alloc (surbl_module_ctx->surbl_pool, sizeof (struct suffix_item));
  391. new_suffix->suffix = memory_pool_strdup (surbl_module_ctx->surbl_pool, DEFAULT_SURBL_SUFFIX);
  392. new_suffix->symbol = memory_pool_strdup (surbl_module_ctx->surbl_pool, DEFAULT_SURBL_SYMBOL);
  393. msg_debug ("add default surbl suffix: %s with symbol: %s", new_suffix->suffix, new_suffix->symbol);
  394. surbl_module_ctx->suffixes = g_list_prepend (surbl_module_ctx->suffixes, new_suffix);
  395. register_symbol (&cfg->cache, new_suffix->symbol, 1, surbl_test_url, new_suffix);
  396. }
  397. register_bit_symbols (cfg);
  398. if (surbl_module_ctx->suffixes != NULL) {
  399. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_list_free,
  400. surbl_module_ctx->suffixes);
  401. }
  402. if (surbl_module_ctx->bits != NULL) {
  403. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_list_free,
  404. surbl_module_ctx->bits);
  405. }
  406. return TRUE;
  407. }
  408. gint
  409. surbl_module_reconfig (struct config_file *cfg)
  410. {
  411. /* Delete pool and objects */
  412. memory_pool_delete (surbl_module_ctx->surbl_pool);
  413. /* Reinit module */
  414. surbl_module_ctx->filter = surbl_filter;
  415. surbl_module_ctx->use_redirector = 0;
  416. surbl_module_ctx->suffixes = NULL;
  417. surbl_module_ctx->bits = NULL;
  418. surbl_module_ctx->surbl_pool = memory_pool_new (memory_pool_get_size ());
  419. surbl_module_ctx->tld2_file = NULL;
  420. surbl_module_ctx->whitelist_file = NULL;
  421. surbl_module_ctx->redirectors_number = 0;
  422. surbl_module_ctx->redirector_trie = rspamd_trie_create (TRUE);
  423. surbl_module_ctx->redirector_hosts = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
  424. surbl_module_ctx->whitelist = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
  425. /* Zero exceptions hashes */
  426. surbl_module_ctx->exceptions = memory_pool_alloc0 (surbl_module_ctx->surbl_pool, MAX_LEVELS * sizeof (GHashTable *));
  427. /* Register destructors */
  428. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_hash_table_destroy, surbl_module_ctx->whitelist);
  429. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_hash_table_destroy, surbl_module_ctx->redirector_hosts);
  430. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_list_free, surbl_module_ctx->suffixes);
  431. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_list_free, surbl_module_ctx->bits);
  432. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) rspamd_trie_free, surbl_module_ctx->redirector_trie);
  433. memory_pool_add_destructor (surbl_module_ctx->surbl_pool, (pool_destruct_func) g_ptr_array_unref, surbl_module_ctx->redirector_ptrs);
  434. /* Perform configure */
  435. return surbl_module_config (cfg);
  436. }
  437. static gchar *
  438. format_surbl_request (memory_pool_t * pool, f_str_t * hostname, struct suffix_item *suffix,
  439. gboolean append_suffix, GError ** err, gboolean forced)
  440. {
  441. GHashTable *t;
  442. gchar *result = NULL, *dots[MAX_LEVELS], num_buf[sizeof("18446744073709551616")], *p;
  443. gint len, slen, r, i, dots_num = 0, level = MAX_LEVELS;
  444. gboolean is_numeric = TRUE;
  445. guint64 ip_num;
  446. f_str_t f;
  447. if (G_LIKELY (suffix != NULL)) {
  448. slen = strlen (suffix->suffix);
  449. }
  450. else if (!append_suffix) {
  451. slen = 0;
  452. }
  453. else {
  454. g_assert_not_reached ();
  455. }
  456. len = hostname->len + slen + 2;
  457. p = hostname->begin;
  458. while (p - hostname->begin < (gint)hostname->len && dots_num < MAX_LEVELS) {
  459. if (*p == '.') {
  460. dots[dots_num] = p;
  461. dots_num ++;
  462. }
  463. else if (! g_ascii_isdigit (*p)) {
  464. is_numeric = FALSE;
  465. }
  466. p ++;
  467. }
  468. /* Check for numeric expressions */
  469. if (is_numeric && dots_num == 3) {
  470. /* This is ip address */
  471. if (suffix != NULL && (suffix->options & SURBL_OPTION_NOIP) != 0) {
  472. /* Ignore such requests */
  473. msg_info ("ignore request of ip url for list %s", suffix->symbol);
  474. return NULL;
  475. }
  476. result = memory_pool_alloc (pool, len);
  477. r = rspamd_snprintf (result, len, "%*s.%*s.%*s.%*s",
  478. (gint)(hostname->len - (dots[2] - hostname->begin + 1)),
  479. dots[2] + 1,
  480. (gint)(dots[2] - dots[1] - 1),
  481. dots[1] + 1,
  482. (gint)(dots[1] - dots[0] - 1),
  483. dots[0] + 1,
  484. (gint)(dots[0] - hostname->begin),
  485. hostname->begin);
  486. }
  487. else if (is_numeric && dots_num == 0) {
  488. /* This is number */
  489. if (suffix != NULL && (suffix->options & SURBL_OPTION_NOIP) != 0) {
  490. /* Ignore such requests */
  491. msg_info ("ignore request of ip url for list %s", suffix->symbol);
  492. return NULL;
  493. }
  494. rspamd_strlcpy (num_buf, hostname->begin, MIN (hostname->len + 1, sizeof (num_buf)));
  495. errno = 0;
  496. ip_num = strtoull (num_buf, NULL, 10);
  497. if (errno != 0) {
  498. msg_info ("cannot convert ip to number '%s': %s", num_buf, strerror (errno));
  499. g_set_error (err, SURBL_ERROR, /* error domain */
  500. CONVERSION_ERROR, /* error code */
  501. "URL cannot be decoded");
  502. return NULL;
  503. }
  504. len = sizeof ("255.255.255.255") + slen;
  505. result = memory_pool_alloc (pool, len);
  506. /* Hack for bugged windows resolver */
  507. ip_num &= 0xFFFFFFFF;
  508. /* Get octets */
  509. r = rspamd_snprintf (result, len, "%ud.%ud.%ud.%ud",
  510. (guint32) ip_num & 0x000000FF, (guint32) (ip_num & 0x0000FF00) >> 8, (guint32) (ip_num & 0x00FF0000) >> 16, (guint32) (ip_num & 0xFF000000) >> 24);
  511. }
  512. else {
  513. /* Not a numeric url */
  514. result = memory_pool_alloc (pool, len);
  515. /* Now we should try to check for exceptions */
  516. if (! forced) {
  517. for (i = MAX_LEVELS - 1; i >= 0; i --) {
  518. t = surbl_module_ctx->exceptions[i];
  519. if (t != NULL && dots_num >= i + 1) {
  520. f.begin = dots[dots_num - i - 1] + 1;
  521. f.len = hostname->len - (dots[dots_num - i - 1] - hostname->begin + 1);
  522. if (g_hash_table_lookup (t, &f) != NULL) {
  523. level = dots_num - i - 1;
  524. break;
  525. }
  526. }
  527. }
  528. }
  529. if (level != MAX_LEVELS) {
  530. if (level == 0) {
  531. r = rspamd_snprintf (result, len, "%*s", (gint)hostname->len, hostname->begin);
  532. }
  533. else {
  534. r = rspamd_snprintf (result, len, "%*s",
  535. (gint)(hostname->len - (dots[level - 1] - hostname->begin + 1)),
  536. dots[level - 1] + 1);
  537. }
  538. }
  539. else if (dots_num >= 2) {
  540. r = rspamd_snprintf (result, len, "%*s",
  541. (gint)(hostname->len - (dots[dots_num - 2] - hostname->begin + 1)),
  542. dots[dots_num - 2] + 1);
  543. }
  544. else {
  545. r = rspamd_snprintf (result, len, "%*s", (gint)hostname->len, hostname->begin);
  546. }
  547. }
  548. if (!forced && g_hash_table_lookup (surbl_module_ctx->whitelist, result) != NULL) {
  549. msg_debug ("url %s is whitelisted", result);
  550. g_set_error (err, SURBL_ERROR, /* error domain */
  551. WHITELIST_ERROR, /* error code */
  552. "URL is whitelisted: %s", /* error message format string */
  553. result);
  554. return NULL;
  555. }
  556. if (append_suffix) {
  557. rspamd_snprintf (result + r, len - r, ".%s", suffix->suffix);
  558. }
  559. msg_debug ("request: %s, dots: %d, level: %d, orig: %*s", result, dots_num, level, (gint)hostname->len, hostname->begin);
  560. return result;
  561. }
  562. static void
  563. make_surbl_requests (struct uri *url, struct worker_task *task,
  564. struct suffix_item *suffix, gboolean forced)
  565. {
  566. gchar *surbl_req;
  567. f_str_t f;
  568. GError *err = NULL;
  569. struct dns_param *param;
  570. f.begin = url->host;
  571. f.len = url->hostlen;
  572. if (check_view (task->cfg->views, suffix->symbol, task)) {
  573. if ((surbl_req = format_surbl_request (task->task_pool, &f, suffix, TRUE, &err, forced)) != NULL) {
  574. param = memory_pool_alloc (task->task_pool, sizeof (struct dns_param));
  575. param->url = url;
  576. param->task = task;
  577. param->suffix = suffix;
  578. param->host_resolve = memory_pool_strdup (task->task_pool, surbl_req);
  579. debug_task ("send surbl dns request %s", surbl_req);
  580. if (make_dns_request (task->resolver, task->s, task->task_pool, dns_callback, (void *)param, DNS_REQUEST_A, surbl_req)) {
  581. task->dns_requests ++;
  582. }
  583. }
  584. else if (err != NULL && err->code != WHITELIST_ERROR) {
  585. msg_info ("cannot format url string for surbl %s, %s", struri (url), err->message);
  586. g_error_free (err);
  587. return;
  588. }
  589. else if (err != NULL) {
  590. g_error_free (err);
  591. }
  592. }
  593. else {
  594. debug_task ("skipping symbol that is not in view: %s", suffix->symbol);
  595. }
  596. }
  597. static void
  598. process_dns_results (struct worker_task *task, struct suffix_item *suffix, gchar *url, guint32 addr)
  599. {
  600. gchar *c, *symbol;
  601. GList *cur;
  602. struct surbl_bit_item *bit;
  603. gint len, found = 0;
  604. if ((c = strchr (suffix->symbol, '%')) != NULL && *(c + 1) == 'b') {
  605. cur = g_list_first (surbl_module_ctx->bits);
  606. while (cur) {
  607. bit = (struct surbl_bit_item *)cur->data;
  608. debug_task ("got result(%d) AND bit(%d): %d", (gint)addr, (gint)ntohl (bit->bit), (gint)bit->bit & (gint)ntohl (addr));
  609. if (((gint)bit->bit & (gint)ntohl (addr)) != 0) {
  610. len = strlen (suffix->symbol) - 2 + strlen (bit->symbol) + 1;
  611. *c = '\0';
  612. symbol = memory_pool_alloc (task->task_pool, len);
  613. rspamd_snprintf (symbol, len, "%s%s%s", suffix->symbol, bit->symbol, c + 2);
  614. *c = '%';
  615. insert_result (task, symbol, 1, g_list_prepend (NULL, memory_pool_strdup (task->task_pool, url)));
  616. found = 1;
  617. }
  618. cur = g_list_next (cur);
  619. }
  620. if (!found) {
  621. insert_result (task, suffix->symbol, 1, g_list_prepend (NULL, memory_pool_strdup (task->task_pool, url)));
  622. }
  623. }
  624. else {
  625. insert_result (task, suffix->symbol, 1, g_list_prepend (NULL, memory_pool_strdup (task->task_pool, url)));
  626. }
  627. }
  628. static void
  629. dns_callback (struct rspamd_dns_reply *reply, gpointer arg)
  630. {
  631. struct dns_param *param = (struct dns_param *)arg;
  632. struct worker_task *task = param->task;
  633. union rspamd_reply_element *elt;
  634. debug_task ("in surbl request callback");
  635. /* If we have result from DNS server, this url exists in SURBL, so increase score */
  636. if (reply->code == DNS_RC_NOERROR && reply->elements) {
  637. msg_info ("<%s> domain [%s] is in surbl %s", param->task->message_id, param->host_resolve, param->suffix->suffix);
  638. elt = reply->elements->data;
  639. process_dns_results (param->task, param->suffix, param->host_resolve, (guint32)elt->a.addr[0].s_addr);
  640. }
  641. else {
  642. debug_task ("<%s> domain [%s] is not in surbl %s", param->task->message_id, param->host_resolve, param->suffix->suffix);
  643. }
  644. }
  645. static void
  646. memcached_callback (memcached_ctx_t * ctx, memc_error_t error, void *data)
  647. {
  648. struct memcached_param *param = (struct memcached_param *)data;
  649. gint *url_count;
  650. switch (ctx->op) {
  651. case CMD_CONNECT:
  652. if (error != OK) {
  653. msg_info ("memcached returned error %s on CONNECT stage", memc_strerror (error));
  654. memc_close_ctx (param->ctx);
  655. }
  656. else {
  657. memc_get (param->ctx, param->ctx->param);
  658. }
  659. break;
  660. case CMD_READ:
  661. if (error != OK) {
  662. msg_info ("memcached returned error %s on READ stage", memc_strerror (error));
  663. memc_close_ctx (param->ctx);
  664. }
  665. else {
  666. url_count = (gint *)param->ctx->param->buf;
  667. /* Do not check DNS for urls that have count more than max_urls */
  668. if (*url_count > (gint)surbl_module_ctx->max_urls) {
  669. msg_info ("url '%s' has count %d, max: %d", struri (param->url), *url_count, surbl_module_ctx->max_urls);
  670. /*
  671. * XXX: try to understand why we should use memcached here
  672. * insert_result (param->task, surbl_module_ctx->metric, surbl_module_ctx->symbol, 1);
  673. */
  674. }
  675. (*url_count)++;
  676. memc_set (param->ctx, param->ctx->param, surbl_module_ctx->url_expire);
  677. }
  678. break;
  679. case CMD_WRITE:
  680. if (error != OK) {
  681. msg_info ("memcached returned error %s on WRITE stage", memc_strerror (error));
  682. }
  683. memc_close_ctx (param->ctx);
  684. make_surbl_requests (param->url, param->task, param->suffix, FALSE);
  685. break;
  686. default:
  687. return;
  688. }
  689. }
  690. static void
  691. register_memcached_call (struct uri *url, struct worker_task *task, struct suffix_item *suffix)
  692. {
  693. struct memcached_param *param;
  694. struct memcached_server *selected;
  695. memcached_param_t *cur_param;
  696. gchar *sum_str;
  697. gint *url_count;
  698. param = memory_pool_alloc (task->task_pool, sizeof (struct memcached_param));
  699. cur_param = memory_pool_alloc0 (task->task_pool, sizeof (memcached_param_t));
  700. url_count = memory_pool_alloc (task->task_pool, sizeof (gint));
  701. param->url = url;
  702. param->task = task;
  703. param->suffix = suffix;
  704. param->ctx = memory_pool_alloc0 (task->task_pool, sizeof (memcached_ctx_t));
  705. cur_param->buf = (gchar *) url_count;
  706. cur_param->bufsize = sizeof (gint);
  707. sum_str = g_compute_checksum_for_string (G_CHECKSUM_MD5, struri (url), -1);
  708. rspamd_strlcpy (cur_param->key, sum_str, sizeof (cur_param->key));
  709. g_free (sum_str);
  710. selected = (struct memcached_server *)get_upstream_by_hash ((void *)task->cfg->memcached_servers,
  711. task->cfg->memcached_servers_num, sizeof (struct memcached_server),
  712. time (NULL), task->cfg->memcached_error_time, task->cfg->memcached_dead_time, task->cfg->memcached_maxerrors, cur_param->key, strlen (cur_param->key));
  713. if (selected == NULL) {
  714. msg_err ("no memcached servers can be selected");
  715. return;
  716. }
  717. param->ctx->callback = memcached_callback;
  718. param->ctx->callback_data = (void *)param;
  719. param->ctx->protocol = task->cfg->memcached_protocol;
  720. memcpy (&param->ctx->addr, &selected->addr, sizeof (struct in_addr));
  721. param->ctx->port = selected->port;
  722. param->ctx->timeout.tv_sec = task->cfg->memcached_connect_timeout / 1000;
  723. param->ctx->timeout.tv_sec = task->cfg->memcached_connect_timeout - param->ctx->timeout.tv_sec * 1000;
  724. param->ctx->sock = -1;
  725. #ifdef WITH_DEBUG
  726. param->ctx->options = MEMC_OPT_DEBUG;
  727. #else
  728. param->ctx->options = 0;
  729. #endif
  730. param->ctx->param = cur_param;
  731. memc_init_ctx (param->ctx);
  732. }
  733. static void
  734. free_redirector_session (void *ud)
  735. {
  736. struct redirector_param *param = (struct redirector_param *)ud;
  737. event_del (&param->ev);
  738. close (param->sock);
  739. }
  740. static void
  741. redirector_callback (gint fd, short what, void *arg)
  742. {
  743. struct redirector_param *param = (struct redirector_param *)arg;
  744. struct worker_task *task = param->task;
  745. gchar url_buf[1024];
  746. gint r;
  747. struct timeval *timeout;
  748. gchar *p, *c;
  749. switch (param->state) {
  750. case STATE_CONNECT:
  751. /* We have write readiness after connect call, so reinit event */
  752. if (what == EV_WRITE) {
  753. timeout = memory_pool_alloc (param->task->task_pool, sizeof (struct timeval));
  754. timeout->tv_sec = surbl_module_ctx->read_timeout / 1000;
  755. timeout->tv_usec = (surbl_module_ctx->read_timeout - timeout->tv_sec * 1000) * 1000;
  756. event_del (&param->ev);
  757. event_set (&param->ev, param->sock, EV_READ | EV_PERSIST, redirector_callback, (void *)param);
  758. event_add (&param->ev, timeout);
  759. r = rspamd_snprintf (url_buf, sizeof (url_buf), "GET %s HTTP/1.0\r\n\r\n", struri (param->url));
  760. if (write (param->sock, url_buf, r) == -1) {
  761. msg_err ("write failed %s to %s", strerror (errno), param->redirector->name);
  762. upstream_fail (&param->redirector->up, param->task->tv.tv_sec);
  763. remove_normal_event (param->task->s, free_redirector_session, param);
  764. return;
  765. }
  766. param->state = STATE_READ;
  767. }
  768. else {
  769. msg_info ("<%s> connection to redirector %s timed out while waiting for write",
  770. param->task->message_id, param->redirector->name);
  771. upstream_fail (&param->redirector->up, param->task->tv.tv_sec);
  772. remove_normal_event (param->task->s, free_redirector_session, param);
  773. return;
  774. }
  775. break;
  776. case STATE_READ:
  777. if (what == EV_READ) {
  778. r = read (param->sock, url_buf, sizeof (url_buf) - 1);
  779. if (r <= 0) {
  780. msg_err ("read failed: %s from %s", strerror (errno), param->redirector->name);
  781. upstream_fail (&param->redirector->up, param->task->tv.tv_sec);
  782. make_surbl_requests (param->url, param->task, param->suffix, FALSE);
  783. remove_normal_event (param->task->s, free_redirector_session, param);
  784. return;
  785. }
  786. url_buf[r - 1] = '\0';
  787. if ((p = strstr (url_buf, "Uri: ")) != NULL) {
  788. p += sizeof ("Uri: ") - 1;
  789. c = p;
  790. while (p++ < url_buf + sizeof (url_buf) - 1) {
  791. if (*p == '\r' || *p == '\n') {
  792. *p = '\0';
  793. break;
  794. }
  795. }
  796. if (*p == '\0') {
  797. debug_task ("<%s> got reply from redirector: '%s' -> '%s'", param->task->message_id, struri (param->url), c);
  798. parse_uri (param->url, memory_pool_strdup (param->task->task_pool, c), param->task->task_pool);
  799. }
  800. }
  801. upstream_ok (&param->redirector->up, param->task->tv.tv_sec);
  802. remove_normal_event (param->task->s, free_redirector_session, param);
  803. }
  804. else {
  805. msg_info ("<%s> reading redirector %s timed out, while waiting for read",
  806. param->redirector->name, param->task->message_id);
  807. upstream_fail (&param->redirector->up, param->task->tv.tv_sec);
  808. remove_normal_event (param->task->s, free_redirector_session, param);
  809. }
  810. break;
  811. }
  812. }
  813. static void
  814. register_redirector_call (struct uri *url, struct worker_task *task,
  815. struct suffix_item *suffix, const gchar *rule)
  816. {
  817. gint s = -1;
  818. struct redirector_param *param;
  819. struct timeval *timeout;
  820. struct redirector_upstream *selected;
  821. selected = (struct redirector_upstream *)get_upstream_round_robin (surbl_module_ctx->redirectors,
  822. surbl_module_ctx->redirectors_number,
  823. sizeof (struct redirector_upstream),
  824. task->tv.tv_sec, DEFAULT_UPSTREAM_ERROR_TIME,
  825. DEFAULT_UPSTREAM_DEAD_TIME, DEFAULT_UPSTREAM_MAXERRORS);
  826. if (selected) {
  827. s = make_tcp_socket (&selected->ina, selected->port, FALSE, TRUE);
  828. }
  829. if (s == -1) {
  830. msg_info ("<%s> cannot create tcp socket failed: %s", task->message_id, strerror (errno));
  831. make_surbl_requests (url, task, suffix, FALSE);
  832. return;
  833. }
  834. param = memory_pool_alloc (task->task_pool, sizeof (struct redirector_param));
  835. param->url = url;
  836. param->task = task;
  837. param->state = STATE_CONNECT;
  838. param->sock = s;
  839. param->suffix = suffix;
  840. param->redirector = selected;
  841. timeout = memory_pool_alloc (task->task_pool, sizeof (struct timeval));
  842. timeout->tv_sec = surbl_module_ctx->connect_timeout / 1000;
  843. timeout->tv_usec = (surbl_module_ctx->connect_timeout - timeout->tv_sec * 1000) * 1000;
  844. event_set (&param->ev, s, EV_WRITE, redirector_callback, (void *)param);
  845. event_add (&param->ev, timeout);
  846. register_async_event (task->s, free_redirector_session, param, FALSE);
  847. msg_info ("<%s> registered redirector call for %s to %s, according to rule: %s",
  848. task->message_id, struri (url), selected->name, rule);
  849. }
  850. static gboolean
  851. surbl_tree_url_callback (gpointer key, gpointer value, void *data)
  852. {
  853. struct redirector_param *param = data;
  854. struct worker_task *task;
  855. struct uri *url = value;
  856. gchar *red_domain;
  857. const gchar *pos;
  858. GRegex *re;
  859. guint idx, len;
  860. task = param->task;
  861. debug_task ("check url %s", struri (url));
  862. if (url->hostlen <= 0) {
  863. return FALSE;
  864. }
  865. if (surbl_module_ctx->use_redirector) {
  866. /* Search in trie */
  867. if (surbl_module_ctx->redirector_trie &&
  868. (pos = rspamd_trie_lookup (surbl_module_ctx->redirector_trie, url->host, url->hostlen, &idx)) != NULL &&
  869. idx < surbl_module_ctx->redirector_ptrs->len) {
  870. /* Get corresponding prefix */
  871. red_domain = g_ptr_array_index (surbl_module_ctx->redirector_ptrs, idx);
  872. if (red_domain != NULL) {
  873. len = strlen (red_domain);
  874. /* First check that we have found domain at the end of host */
  875. if (pos + len == url->host + url->hostlen &&
  876. (pos == url->host || *(pos - 1) == '.')) {
  877. /* Try to find corresponding regexp */
  878. re = g_hash_table_lookup (surbl_module_ctx->redirector_hosts, red_domain);
  879. if (re != NULL && (re == NO_REGEXP || g_regex_match (re, url->string, 0, NULL))) {
  880. /* If no regexp found or founded regexp matches url string register redirector's call */
  881. if (surbl_module_ctx->redirector_symbol != NULL) {
  882. insert_result (param->task, surbl_module_ctx->redirector_symbol, 1, g_list_prepend (NULL, red_domain));
  883. }
  884. register_redirector_call (url, param->task, param->suffix, red_domain);
  885. return FALSE;
  886. }
  887. }
  888. }
  889. }
  890. make_surbl_requests (url, param->task, param->suffix, FALSE);
  891. }
  892. else {
  893. if (param->task->worker->srv->cfg->memcached_servers_num > 0) {
  894. register_memcached_call (url, param->task, param->suffix);
  895. }
  896. else {
  897. make_surbl_requests (url, param->task, param->suffix, FALSE);
  898. }
  899. }
  900. return FALSE;
  901. }
  902. static void
  903. surbl_test_url (struct worker_task *task, void *user_data)
  904. {
  905. struct redirector_param param;
  906. struct suffix_item *suffix = user_data;
  907. param.task = task;
  908. param.suffix = suffix;
  909. g_tree_foreach (task->urls, surbl_tree_url_callback, &param);
  910. }
  911. static gint
  912. surbl_filter (struct worker_task *task)
  913. {
  914. /* XXX: remove this shit */
  915. return 0;
  916. }
  917. /*
  918. * Handlers of URLS command
  919. */
  920. struct urls_tree_cb_data {
  921. gchar *buf;
  922. gsize len;
  923. gsize off;
  924. struct worker_task *task;
  925. };
  926. static gboolean
  927. calculate_buflen_cb (gpointer key, gpointer value, gpointer cbdata)
  928. {
  929. struct urls_tree_cb_data *cb = cbdata;
  930. struct uri *url = value;
  931. cb->len += strlen (struri (url)) + url->hostlen + sizeof (" <\"\">, ") - 1;
  932. return FALSE;
  933. }
  934. static gboolean
  935. write_urls_buffer (gpointer key, gpointer value, gpointer cbdata)
  936. {
  937. struct urls_tree_cb_data *cb = cbdata;
  938. struct uri *url = value;
  939. f_str_t f;
  940. gchar *urlstr;
  941. gsize len;
  942. f.begin = url->host;
  943. f.len = url->hostlen;
  944. if ((urlstr = format_surbl_request (cb->task->task_pool, &f, NULL, FALSE, NULL, FALSE)) != NULL) {
  945. len = strlen (urlstr);
  946. if (cb->off + len >= cb->len) {
  947. msg_info ("cannot write urls header completely, stripped reply at: %z", cb->off);
  948. return TRUE;
  949. }
  950. else {
  951. cb->off += rspamd_snprintf (cb->buf + cb->off, cb->len - cb->off, " %s <\"%s\">,",
  952. urlstr, struri (url));
  953. }
  954. }
  955. return FALSE;
  956. }
  957. static gboolean
  958. urls_command_handler (struct worker_task *task)
  959. {
  960. struct urls_tree_cb_data cb;
  961. /* First calculate buffer length */
  962. cb.len = sizeof (RSPAMD_REPLY_BANNER "/1.0 0 " SPAMD_OK CRLF "Urls: " CRLF);
  963. cb.off = 0;
  964. g_tree_foreach (task->urls, calculate_buflen_cb, &cb);
  965. cb.buf = memory_pool_alloc (task->task_pool, cb.len * sizeof (gchar));
  966. cb.off += rspamd_snprintf (cb.buf + cb.off, cb.len - cb.off, "%s/%s 0 %s" CRLF "Urls:",
  967. (task->proto == SPAMC_PROTO) ? SPAMD_REPLY_BANNER : RSPAMD_REPLY_BANNER,
  968. "1.3", SPAMD_OK);
  969. cb.task = task;
  970. /* Write urls to buffer */
  971. g_tree_foreach (task->urls, write_urls_buffer, &cb);
  972. /* Strip last ',' */
  973. if (cb.buf[cb.off - 1] == ',') {
  974. cb.buf[--cb.off] = '\0';
  975. }
  976. /* Write result */
  977. if (! rspamd_dispatcher_write (task->dispatcher, cb.buf, cb.off, FALSE, TRUE)) {
  978. return FALSE;
  979. }
  980. if (!rspamd_dispatcher_write (task->dispatcher, CRLF, sizeof (CRLF) - 1, FALSE, TRUE)) {
  981. return FALSE;
  982. }
  983. task->state = STATE_REPLY;
  984. return TRUE;
  985. }
  986. /*
  987. * vi:ts=4
  988. */