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.

dkim_check.c 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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. /***MODULE:dkim
  17. * rspamd module that checks dkim records of incoming email
  18. *
  19. * Allowed options:
  20. * - symbol_allow (string): symbol to insert in case of allow (default: 'R_DKIM_ALLOW')
  21. * - symbol_reject (string): symbol to insert (default: 'R_DKIM_REJECT')
  22. * - symbol_tempfail (string): symbol to insert in case of temporary fail (default: 'R_DKIM_TEMPFAIL')
  23. * - symbol_permfail (string): symbol to insert in case of permanent failure (default: 'R_DKIM_PERMFAIL')
  24. * - symbol_na (string): symbol to insert in case of no signing (default: 'R_DKIM_NA')
  25. * - whitelist (map): map of whitelisted networks
  26. * - domains (map): map of domains to check
  27. * - strict_multiplier (number): multiplier for strict domains
  28. * - time_jitter (number): jitter in seconds to allow time diff while checking
  29. * - trusted_only (flag): check signatures only for domains in 'domains' map
  30. */
  31. #include "config.h"
  32. #include "libmime/message.h"
  33. #include "libserver/dkim.h"
  34. #include "libutil/hash.h"
  35. #include "libutil/map.h"
  36. #include "libutil/map_helpers.h"
  37. #include "rspamd.h"
  38. #include "utlist.h"
  39. #include "lua/lua_common.h"
  40. #include "libserver/mempool_vars_internal.h"
  41. #define DEFAULT_SYMBOL_REJECT "R_DKIM_REJECT"
  42. #define DEFAULT_SYMBOL_TEMPFAIL "R_DKIM_TEMPFAIL"
  43. #define DEFAULT_SYMBOL_ALLOW "R_DKIM_ALLOW"
  44. #define DEFAULT_SYMBOL_NA "R_DKIM_NA"
  45. #define DEFAULT_SYMBOL_PERMFAIL "R_DKIM_PERMFAIL"
  46. #define DEFAULT_CACHE_SIZE 2048
  47. #define DEFAULT_TIME_JITTER 60
  48. #define DEFAULT_MAX_SIGS 5
  49. static const gchar default_sign_headers[] = ""
  50. "(o)from:(o)sender:(o)reply-to:(o)subject:(o)date:(o)message-id:"
  51. "(o)to:(o)cc:(o)mime-version:(o)content-type:(o)content-transfer-encoding:"
  52. "resent-to:resent-cc:resent-from:resent-sender:resent-message-id:"
  53. "(o)in-reply-to:(o)references:list-id:list-owner:list-unsubscribe:"
  54. "list-subscribe:list-post";
  55. struct dkim_ctx {
  56. struct module_ctx ctx;
  57. const gchar *symbol_reject;
  58. const gchar *symbol_tempfail;
  59. const gchar *symbol_allow;
  60. const gchar *symbol_na;
  61. const gchar *symbol_permfail;
  62. rspamd_mempool_t *dkim_pool;
  63. struct rspamd_radix_map_helper *whitelist_ip;
  64. struct rspamd_hash_map_helper *dkim_domains;
  65. guint strict_multiplier;
  66. guint time_jitter;
  67. rspamd_lru_hash_t *dkim_hash;
  68. rspamd_lru_hash_t *dkim_sign_hash;
  69. const gchar *sign_headers;
  70. gint sign_condition_ref;
  71. guint max_sigs;
  72. gboolean trusted_only;
  73. gboolean check_local;
  74. gboolean check_authed;
  75. };
  76. struct dkim_check_result {
  77. rspamd_dkim_context_t *ctx;
  78. rspamd_dkim_key_t *key;
  79. struct rspamd_task *task;
  80. gint res;
  81. gdouble mult_allow;
  82. gdouble mult_deny;
  83. struct rspamd_async_watcher *w;
  84. struct dkim_check_result *next, *prev, *first;
  85. };
  86. static struct dkim_ctx *dkim_module_ctx = NULL;
  87. static void dkim_symbol_callback (struct rspamd_task *task, void *unused);
  88. static void dkim_sign_callback (struct rspamd_task *task, void *unused);
  89. static gint lua_dkim_sign_handler (lua_State *L);
  90. static gint lua_dkim_verify_handler (lua_State *L);
  91. static gint lua_dkim_canonicalize_handler (lua_State *L);
  92. /* Initialization */
  93. gint dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx);
  94. gint dkim_module_config (struct rspamd_config *cfg);
  95. gint dkim_module_reconfig (struct rspamd_config *cfg);
  96. module_t dkim_module = {
  97. "dkim",
  98. dkim_module_init,
  99. dkim_module_config,
  100. dkim_module_reconfig,
  101. NULL,
  102. RSPAMD_MODULE_VER
  103. };
  104. static void
  105. dkim_module_key_dtor (gpointer k)
  106. {
  107. rspamd_dkim_key_t *key = k;
  108. rspamd_dkim_key_unref (key);
  109. }
  110. gint
  111. dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx)
  112. {
  113. if (dkim_module_ctx == NULL) {
  114. dkim_module_ctx = g_malloc0 (sizeof (struct dkim_ctx));
  115. dkim_module_ctx->dkim_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "dkim");
  116. dkim_module_ctx->sign_headers = default_sign_headers;
  117. dkim_module_ctx->sign_condition_ref = -1;
  118. dkim_module_ctx->max_sigs = DEFAULT_MAX_SIGS;
  119. }
  120. *ctx = (struct module_ctx *)dkim_module_ctx;
  121. rspamd_rcl_add_doc_by_path (cfg,
  122. NULL,
  123. "DKIM check plugin",
  124. "dkim",
  125. UCL_OBJECT,
  126. NULL,
  127. 0,
  128. NULL,
  129. 0);
  130. rspamd_rcl_add_doc_by_path (cfg,
  131. "dkim",
  132. "Map of IP addresses that should be excluded from DKIM checks",
  133. "whitelist",
  134. UCL_STRING,
  135. NULL,
  136. 0,
  137. NULL,
  138. 0);
  139. rspamd_rcl_add_doc_by_path (cfg,
  140. "dkim",
  141. "Symbol that is added if DKIM check is successful",
  142. "symbol_allow",
  143. UCL_STRING,
  144. NULL,
  145. 0,
  146. DEFAULT_SYMBOL_ALLOW,
  147. 0);
  148. rspamd_rcl_add_doc_by_path (cfg,
  149. "dkim",
  150. "Symbol that is added if DKIM check is unsuccessful",
  151. "symbol_reject",
  152. UCL_STRING,
  153. NULL,
  154. 0,
  155. DEFAULT_SYMBOL_REJECT,
  156. 0);
  157. rspamd_rcl_add_doc_by_path (cfg,
  158. "dkim",
  159. "Symbol that is added if DKIM check can't be completed (e.g. DNS failure)",
  160. "symbol_tempfail",
  161. UCL_STRING,
  162. NULL,
  163. 0,
  164. DEFAULT_SYMBOL_TEMPFAIL,
  165. 0);
  166. rspamd_rcl_add_doc_by_path (cfg,
  167. "dkim",
  168. "Symbol that is added if mail is not signed",
  169. "symbol_na",
  170. UCL_STRING,
  171. NULL,
  172. 0,
  173. DEFAULT_SYMBOL_NA,
  174. 0);
  175. rspamd_rcl_add_doc_by_path (cfg,
  176. "dkim",
  177. "Symbol that is added if permanent failure encountered",
  178. "symbol_permfail",
  179. UCL_STRING,
  180. NULL,
  181. 0,
  182. DEFAULT_SYMBOL_PERMFAIL,
  183. 0);
  184. rspamd_rcl_add_doc_by_path (cfg,
  185. "dkim",
  186. "Size of DKIM keys cache",
  187. "dkim_cache_size",
  188. UCL_INT,
  189. NULL,
  190. 0,
  191. G_STRINGIFY (DEFAULT_CACHE_SIZE),
  192. 0);
  193. rspamd_rcl_add_doc_by_path (cfg,
  194. "dkim",
  195. "Allow this time difference when checking DKIM signature time validity",
  196. "time_jitter",
  197. UCL_TIME,
  198. NULL,
  199. 0,
  200. G_STRINGIFY (DEFAULT_TIME_JITTER),
  201. 0);
  202. rspamd_rcl_add_doc_by_path (cfg,
  203. "dkim",
  204. "Domains to check DKIM for (check all domains if this option is empty)",
  205. "domains",
  206. UCL_STRING,
  207. NULL,
  208. 0,
  209. "empty",
  210. 0);
  211. rspamd_rcl_add_doc_by_path (cfg,
  212. "dkim",
  213. "Map of domains that are treated as 'trusted' meaning that DKIM policy failure has more significant score",
  214. "trusted_domains",
  215. UCL_STRING,
  216. NULL,
  217. 0,
  218. "empty",
  219. 0);
  220. rspamd_rcl_add_doc_by_path (cfg,
  221. "dkim",
  222. "Multiply dkim score by this factor for trusted domains",
  223. "strict_multiplier",
  224. UCL_FLOAT,
  225. NULL,
  226. 0,
  227. NULL,
  228. 0);
  229. rspamd_rcl_add_doc_by_path (cfg,
  230. "dkim",
  231. "Check DKIM policies merely for `trusted_domains`",
  232. "trusted_only",
  233. UCL_BOOLEAN,
  234. NULL,
  235. 0,
  236. "false",
  237. 0);
  238. rspamd_rcl_add_doc_by_path (cfg,
  239. "dkim",
  240. "Lua script that tells if a message should be signed and with what params",
  241. "sign_condition",
  242. UCL_STRING,
  243. NULL,
  244. 0,
  245. "empty",
  246. 0);
  247. rspamd_rcl_add_doc_by_path (cfg,
  248. "dkim",
  249. "Obsoleted: maximum number of DKIM signatures to check",
  250. "max_sigs",
  251. UCL_INT,
  252. NULL,
  253. 0,
  254. "n/a",
  255. 0);
  256. rspamd_rcl_add_doc_by_path (cfg,
  257. "dkim",
  258. "Headers used in signing",
  259. "sign_headers",
  260. UCL_STRING,
  261. NULL,
  262. 0,
  263. default_sign_headers,
  264. 0);
  265. return 0;
  266. }
  267. gint
  268. dkim_module_config (struct rspamd_config *cfg)
  269. {
  270. const ucl_object_t *value;
  271. gint res = TRUE, cb_id = -1;
  272. guint cache_size, sign_cache_size;
  273. gboolean got_trusted = FALSE;
  274. /* Register global methods */
  275. lua_getglobal (cfg->lua_state, "rspamd_plugins");
  276. if (lua_type (cfg->lua_state, -1) == LUA_TTABLE) {
  277. lua_pushstring (cfg->lua_state, "dkim");
  278. lua_createtable (cfg->lua_state, 0, 1);
  279. /* Set methods */
  280. lua_pushstring (cfg->lua_state, "sign");
  281. lua_pushcfunction (cfg->lua_state, lua_dkim_sign_handler);
  282. lua_settable (cfg->lua_state, -3);
  283. lua_pushstring (cfg->lua_state, "verify");
  284. lua_pushcfunction (cfg->lua_state, lua_dkim_verify_handler);
  285. lua_settable (cfg->lua_state, -3);
  286. lua_pushstring (cfg->lua_state, "canon_header_relaxed");
  287. lua_pushcfunction (cfg->lua_state, lua_dkim_canonicalize_handler);
  288. lua_settable (cfg->lua_state, -3);
  289. /* Finish dkim key */
  290. lua_settable (cfg->lua_state, -3);
  291. }
  292. lua_pop (cfg->lua_state, 1); /* Remove global function */
  293. dkim_module_ctx->whitelist_ip = NULL;
  294. if ((value =
  295. rspamd_config_get_module_opt (cfg, "options", "check_local")) != NULL) {
  296. dkim_module_ctx->check_local = ucl_object_toboolean (value);
  297. }
  298. else {
  299. dkim_module_ctx->check_local = FALSE;
  300. }
  301. if ((value =
  302. rspamd_config_get_module_opt (cfg, "options", "check_authed")) != NULL) {
  303. dkim_module_ctx->check_authed = ucl_object_toboolean (value);
  304. }
  305. else {
  306. dkim_module_ctx->check_authed = FALSE;
  307. }
  308. if ((value =
  309. rspamd_config_get_module_opt (cfg, "dkim", "symbol_reject")) != NULL) {
  310. dkim_module_ctx->symbol_reject = ucl_object_tostring (value);
  311. }
  312. else {
  313. dkim_module_ctx->symbol_reject = DEFAULT_SYMBOL_REJECT;
  314. }
  315. if ((value =
  316. rspamd_config_get_module_opt (cfg, "dkim",
  317. "symbol_tempfail")) != NULL) {
  318. dkim_module_ctx->symbol_tempfail = ucl_object_tostring (value);
  319. }
  320. else {
  321. dkim_module_ctx->symbol_tempfail = DEFAULT_SYMBOL_TEMPFAIL;
  322. }
  323. if ((value =
  324. rspamd_config_get_module_opt (cfg, "dkim", "symbol_allow")) != NULL) {
  325. dkim_module_ctx->symbol_allow = ucl_object_tostring (value);
  326. }
  327. else {
  328. dkim_module_ctx->symbol_allow = DEFAULT_SYMBOL_ALLOW;
  329. }
  330. if ((value =
  331. rspamd_config_get_module_opt (cfg, "dkim", "symbol_na")) != NULL) {
  332. dkim_module_ctx->symbol_na = ucl_object_tostring (value);
  333. }
  334. else {
  335. dkim_module_ctx->symbol_na = DEFAULT_SYMBOL_NA;
  336. }
  337. if ((value =
  338. rspamd_config_get_module_opt (cfg, "dkim", "symbol_permfail")) != NULL) {
  339. dkim_module_ctx->symbol_permfail = ucl_object_tostring (value);
  340. }
  341. else {
  342. dkim_module_ctx->symbol_permfail = DEFAULT_SYMBOL_PERMFAIL;
  343. }
  344. if ((value =
  345. rspamd_config_get_module_opt (cfg, "dkim",
  346. "dkim_cache_size")) != NULL) {
  347. cache_size = ucl_object_toint (value);
  348. }
  349. else {
  350. cache_size = DEFAULT_CACHE_SIZE;
  351. }
  352. if ((value =
  353. rspamd_config_get_module_opt (cfg, "dkim",
  354. "sign_cache_size")) != NULL) {
  355. sign_cache_size = ucl_object_toint (value);
  356. }
  357. else {
  358. sign_cache_size = 128;
  359. }
  360. if ((value =
  361. rspamd_config_get_module_opt (cfg, "dkim", "time_jitter")) != NULL) {
  362. dkim_module_ctx->time_jitter = ucl_object_todouble (value);
  363. }
  364. else {
  365. dkim_module_ctx->time_jitter = DEFAULT_TIME_JITTER;
  366. }
  367. if ((value =
  368. rspamd_config_get_module_opt (cfg, "dkim", "max_sigs")) != NULL) {
  369. dkim_module_ctx->max_sigs = ucl_object_toint (value);
  370. }
  371. if ((value =
  372. rspamd_config_get_module_opt (cfg, "dkim", "whitelist")) != NULL) {
  373. rspamd_config_radix_from_ucl (cfg, value, "DKIM whitelist",
  374. &dkim_module_ctx->whitelist_ip, NULL);
  375. rspamd_mempool_add_destructor (dkim_module_ctx->dkim_pool,
  376. (rspamd_mempool_destruct_t)rspamd_map_helper_destroy_radix,
  377. dkim_module_ctx->whitelist_ip);
  378. }
  379. if ((value =
  380. rspamd_config_get_module_opt (cfg, "dkim", "domains")) != NULL) {
  381. if (!rspamd_map_add_from_ucl (cfg, value,
  382. "DKIM domains",
  383. rspamd_kv_list_read,
  384. rspamd_kv_list_fin,
  385. rspamd_kv_list_dtor,
  386. (void **)&dkim_module_ctx->dkim_domains)) {
  387. msg_warn_config ("cannot load dkim domains list from %s",
  388. ucl_object_tostring (value));
  389. }
  390. else {
  391. rspamd_mempool_add_destructor (dkim_module_ctx->dkim_pool,
  392. (rspamd_mempool_destruct_t)rspamd_map_helper_destroy_hash,
  393. dkim_module_ctx->dkim_domains);
  394. got_trusted = TRUE;
  395. }
  396. }
  397. if (!got_trusted && (value =
  398. rspamd_config_get_module_opt (cfg, "dkim", "trusted_domains")) != NULL) {
  399. if (!rspamd_map_add_from_ucl (cfg, value,
  400. "DKIM domains",
  401. rspamd_kv_list_read,
  402. rspamd_kv_list_fin,
  403. rspamd_kv_list_dtor,
  404. (void **)&dkim_module_ctx->dkim_domains)) {
  405. msg_warn_config ("cannot load dkim domains list from %s",
  406. ucl_object_tostring (value));
  407. }
  408. else {
  409. rspamd_mempool_add_destructor (dkim_module_ctx->dkim_pool,
  410. (rspamd_mempool_destruct_t)rspamd_map_helper_destroy_hash,
  411. dkim_module_ctx->dkim_domains);
  412. got_trusted = TRUE;
  413. }
  414. }
  415. if ((value =
  416. rspamd_config_get_module_opt (cfg, "dkim",
  417. "strict_multiplier")) != NULL) {
  418. dkim_module_ctx->strict_multiplier = ucl_object_toint (value);
  419. }
  420. else {
  421. dkim_module_ctx->strict_multiplier = 1;
  422. }
  423. if ((value =
  424. rspamd_config_get_module_opt (cfg, "dkim", "trusted_only")) != NULL) {
  425. dkim_module_ctx->trusted_only = ucl_object_toboolean (value);
  426. }
  427. else {
  428. dkim_module_ctx->trusted_only = FALSE;
  429. }
  430. if ((value =
  431. rspamd_config_get_module_opt (cfg, "dkim", "sign_headers")) != NULL) {
  432. dkim_module_ctx->sign_headers = ucl_object_tostring (value);
  433. }
  434. dkim_module_ctx->dkim_hash = rspamd_lru_hash_new (
  435. cache_size,
  436. g_free,
  437. dkim_module_key_dtor);
  438. dkim_module_ctx->dkim_sign_hash = rspamd_lru_hash_new (
  439. sign_cache_size,
  440. g_free,
  441. (GDestroyNotify)rspamd_dkim_sign_key_unref);
  442. if (dkim_module_ctx->trusted_only && !got_trusted) {
  443. msg_err_config (
  444. "trusted_only option is set and no trusted domains are defined; disabling dkim module completely as it is useless in this case");
  445. }
  446. else {
  447. if (!rspamd_config_is_module_enabled (cfg, "dkim")) {
  448. return TRUE;
  449. }
  450. cb_id = rspamd_symbols_cache_add_symbol (cfg->cache,
  451. dkim_module_ctx->symbol_reject,
  452. 0,
  453. dkim_symbol_callback,
  454. NULL,
  455. SYMBOL_TYPE_NORMAL|SYMBOL_TYPE_FINE,
  456. -1);
  457. rspamd_symbols_cache_add_symbol (cfg->cache,
  458. dkim_module_ctx->symbol_na,
  459. 0,
  460. NULL, NULL,
  461. SYMBOL_TYPE_VIRTUAL|SYMBOL_TYPE_FINE,
  462. cb_id);
  463. rspamd_symbols_cache_add_symbol (cfg->cache,
  464. dkim_module_ctx->symbol_permfail,
  465. 0,
  466. NULL, NULL,
  467. SYMBOL_TYPE_VIRTUAL|SYMBOL_TYPE_FINE,
  468. cb_id);
  469. rspamd_symbols_cache_add_symbol (cfg->cache,
  470. dkim_module_ctx->symbol_tempfail,
  471. 0,
  472. NULL, NULL,
  473. SYMBOL_TYPE_VIRTUAL|SYMBOL_TYPE_FINE,
  474. cb_id);
  475. rspamd_symbols_cache_add_symbol (cfg->cache,
  476. dkim_module_ctx->symbol_allow,
  477. 0,
  478. NULL, NULL,
  479. SYMBOL_TYPE_VIRTUAL|SYMBOL_TYPE_FINE,
  480. cb_id);
  481. msg_info_config ("init internal dkim module");
  482. #ifndef HAVE_OPENSSL
  483. msg_warn_config (
  484. "openssl is not found so dkim rsa check is disabled, only check body hash, it is NOT safe to trust these results");
  485. #endif
  486. }
  487. if ((value = rspamd_config_get_module_opt (cfg, "dkim", "sign_condition"))
  488. != NULL) {
  489. const gchar *lua_script;
  490. lua_script = ucl_object_tostring (value);
  491. if (lua_script) {
  492. if (luaL_dostring (cfg->lua_state, lua_script) != 0) {
  493. msg_err_config ("cannot execute lua script for dkim "
  494. "sign condition: %s", lua_tostring (cfg->lua_state, -1));
  495. }
  496. else {
  497. if (lua_type (cfg->lua_state, -1) == LUA_TFUNCTION) {
  498. dkim_module_ctx->sign_condition_ref = luaL_ref (cfg->lua_state,
  499. LUA_REGISTRYINDEX);
  500. rspamd_lua_add_ref_dtor (cfg->lua_state,
  501. dkim_module_ctx->dkim_pool,
  502. dkim_module_ctx->sign_condition_ref);
  503. rspamd_symbols_cache_add_symbol (cfg->cache,
  504. "DKIM_SIGN",
  505. 0,
  506. dkim_sign_callback,
  507. NULL,
  508. SYMBOL_TYPE_CALLBACK|SYMBOL_TYPE_FINE,
  509. -1);
  510. msg_info_config ("init condition script for DKIM signing");
  511. /*
  512. * Allow dkim signing to be executed only after dkim check
  513. */
  514. if (cb_id > 0) {
  515. rspamd_symbols_cache_add_delayed_dependency (cfg->cache,
  516. "DKIM_SIGN", dkim_module_ctx->symbol_reject);
  517. }
  518. rspamd_config_add_symbol (cfg,
  519. "DKIM_SIGN", 0.0, "DKIM signature fake symbol",
  520. "dkim", RSPAMD_SYMBOL_FLAG_IGNORE, 1, 1);
  521. rspamd_config_add_symbol (cfg,
  522. "DKIM_TRACE", 0.0, "DKIM trace symbol",
  523. "policies", RSPAMD_SYMBOL_FLAG_IGNORE, 1, 1);
  524. rspamd_config_add_symbol_group (cfg, "DKIM_SIGN", "dkim");
  525. rspamd_config_add_symbol_group (cfg, "DKIM_TRACE", "dkim");
  526. }
  527. else {
  528. msg_err_config ("lua script must return "
  529. "function(task) and not %s",
  530. lua_typename (cfg->lua_state,
  531. lua_type (cfg->lua_state, -1)));
  532. }
  533. }
  534. }
  535. }
  536. return res;
  537. }
  538. rspamd_dkim_sign_key_t *
  539. dkim_module_load_key_format (lua_State *L, struct rspamd_task *task,
  540. const gchar *key, gsize keylen,
  541. enum rspamd_dkim_sign_key_type kt)
  542. {
  543. guchar h[rspamd_cryptobox_HASHBYTES],
  544. hex_hash[rspamd_cryptobox_HASHBYTES * 2 + 1];
  545. rspamd_dkim_sign_key_t *ret;
  546. GError *err = NULL;
  547. memset (hex_hash, 0, sizeof (hex_hash));
  548. rspamd_cryptobox_hash (h, key, keylen, NULL, 0);
  549. rspamd_encode_hex_buf (h, sizeof (h), hex_hash, sizeof (hex_hash));
  550. ret = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_sign_hash,
  551. hex_hash, time (NULL));
  552. if (ret == NULL) {
  553. ret = rspamd_dkim_sign_key_load (key, keylen, kt, &err);
  554. if (ret == NULL) {
  555. msg_err_task ("cannot load private key: %e", err);
  556. g_error_free (err);
  557. return NULL;
  558. }
  559. rspamd_lru_hash_insert (dkim_module_ctx->dkim_sign_hash,
  560. g_strdup (hex_hash), ret,
  561. time (NULL), 0);
  562. }
  563. return ret;
  564. }
  565. static gint
  566. lua_dkim_sign_handler (lua_State *L)
  567. {
  568. struct rspamd_task *task = lua_check_task (L, 1);
  569. gint64 arc_idx = 0, expire = 0;
  570. enum rspamd_dkim_type sign_type = RSPAMD_DKIM_NORMAL;
  571. GError *err = NULL;
  572. GString *hdr;
  573. const gchar *selector = NULL, *domain = NULL, *key = NULL, *rawkey = NULL,
  574. *headers = NULL, *sign_type_str = NULL, *arc_cv = NULL,
  575. *pubkey = NULL;
  576. rspamd_dkim_sign_context_t *ctx;
  577. rspamd_dkim_sign_key_t *dkim_key;
  578. gsize rawlen = 0, keylen = 0;
  579. gboolean no_cache = FALSE, strict_pubkey_check = FALSE;
  580. luaL_argcheck (L, lua_type (L, 2) == LUA_TTABLE, 2, "'table' expected");
  581. /*
  582. * Get the following elements:
  583. * - selector
  584. * - domain
  585. * - key
  586. */
  587. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  588. "key=V;rawkey=V;*domain=S;*selector=S;no_cache=B;headers=S;"
  589. "sign_type=S;arc_idx=I;arc_cv=S;expire=I;pubkey=S;"
  590. "strict_pubkey_check=B",
  591. &keylen, &key, &rawlen, &rawkey, &domain,
  592. &selector, &no_cache, &headers,
  593. &sign_type_str, &arc_idx, &arc_cv, &expire, &pubkey,
  594. &strict_pubkey_check)) {
  595. msg_err_task ("cannot parse table arguments: %e",
  596. err);
  597. g_error_free (err);
  598. lua_pushboolean (L, FALSE);
  599. return 1;
  600. }
  601. if (headers == NULL) {
  602. headers = dkim_module_ctx->sign_headers;
  603. }
  604. if (dkim_module_ctx->dkim_sign_hash == NULL) {
  605. dkim_module_ctx->dkim_sign_hash = rspamd_lru_hash_new (
  606. 128,
  607. g_free, /* Keys are just C-strings */
  608. (GDestroyNotify)rspamd_dkim_sign_key_unref);
  609. }
  610. #define PEM_SIG "-----BEGIN"
  611. if (key) {
  612. if (key[0] == '.' || key[0] == '/') {
  613. /* Likely raw path */
  614. dkim_key = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_sign_hash,
  615. key, time (NULL));
  616. if (dkim_key == NULL) {
  617. dkim_key = rspamd_dkim_sign_key_load (key, strlen (key),
  618. RSPAMD_DKIM_SIGN_KEY_FILE, &err);
  619. if (dkim_key == NULL) {
  620. msg_err_task ("cannot load dkim key %s: %e",
  621. key, err);
  622. g_error_free (err);
  623. lua_pushboolean (L, FALSE);
  624. return 1;
  625. }
  626. rspamd_lru_hash_insert (dkim_module_ctx->dkim_sign_hash,
  627. g_strdup (key), dkim_key,
  628. time (NULL), 0);
  629. }
  630. }
  631. else if (keylen > sizeof (PEM_SIG) &&
  632. strncmp (key, PEM_SIG, sizeof (PEM_SIG) - 1) == 0) {
  633. /* Pem header found */
  634. dkim_key = dkim_module_load_key_format (L, task, key, keylen,
  635. RSPAMD_DKIM_SIGN_KEY_PEM);
  636. if (dkim_key == NULL) {
  637. lua_pushboolean (L, FALSE);
  638. return 1;
  639. }
  640. }
  641. else {
  642. dkim_key = dkim_module_load_key_format (L, task, key, keylen,
  643. RSPAMD_DKIM_SIGN_KEY_BASE64);
  644. if (dkim_key == NULL) {
  645. lua_pushboolean (L, FALSE);
  646. return 1;
  647. }
  648. }
  649. }
  650. else if (rawkey) {
  651. key = rawkey;
  652. keylen = rawlen;
  653. if (keylen > sizeof (PEM_SIG) &&
  654. strncmp (key, PEM_SIG, sizeof (PEM_SIG) - 1) == 0) {
  655. /* Pem header found */
  656. dkim_key = dkim_module_load_key_format (L, task, key, keylen,
  657. RSPAMD_DKIM_SIGN_KEY_PEM);
  658. if (dkim_key == NULL) {
  659. lua_pushboolean (L, FALSE);
  660. return 1;
  661. }
  662. }
  663. else {
  664. dkim_key = dkim_module_load_key_format (L, task, key, keylen,
  665. RSPAMD_DKIM_SIGN_KEY_BASE64);
  666. if (dkim_key == NULL) {
  667. lua_pushboolean (L, FALSE);
  668. return 1;
  669. }
  670. }
  671. }
  672. else {
  673. msg_err_task ("neither key nor rawkey are specified");
  674. lua_pushboolean (L, FALSE);
  675. return 1;
  676. }
  677. #undef PEM_SIG
  678. if (sign_type_str) {
  679. if (strcmp (sign_type_str, "dkim") == 0) {
  680. sign_type = RSPAMD_DKIM_NORMAL;
  681. }
  682. else if (strcmp (sign_type_str, "arc-sign") == 0) {
  683. sign_type = RSPAMD_DKIM_ARC_SIG;
  684. if (arc_idx == 0) {
  685. lua_settop (L, 0);
  686. return luaL_error (L, "no arc idx specified");
  687. }
  688. }
  689. else if (strcmp (sign_type_str, "arc-seal") == 0) {
  690. sign_type = RSPAMD_DKIM_ARC_SEAL;
  691. if (arc_cv == NULL) {
  692. lua_settop (L, 0);
  693. return luaL_error (L, "no arc cv specified");
  694. }
  695. if (arc_idx == 0) {
  696. lua_settop (L, 0);
  697. return luaL_error (L, "no arc idx specified");
  698. }
  699. }
  700. else {
  701. lua_settop (L, 0);
  702. return luaL_error (L, "unknown sign type: %s",
  703. sign_type_str);
  704. }
  705. }
  706. if (pubkey != NULL) {
  707. /* Also check if private and public keys match */
  708. rspamd_dkim_key_t *pk;
  709. gsize keylen = strlen (pubkey);
  710. pk = rspamd_dkim_parse_key (pubkey, &keylen, NULL);
  711. if (pk == NULL) {
  712. if (strict_pubkey_check) {
  713. msg_err_task ("cannot parse pubkey from string: %s, skip signing",
  714. pubkey);
  715. lua_pushboolean (L, FALSE);
  716. return 1;
  717. }
  718. else {
  719. msg_warn_task ("cannot parse pubkey from string: %s",
  720. pubkey);
  721. }
  722. }
  723. else {
  724. GError *te = NULL;
  725. /* We have parsed the key, so try to check keys */
  726. if (!rspamd_dkim_match_keys (pk, dkim_key, &te)) {
  727. if (strict_pubkey_check) {
  728. msg_err_task ("public key for %s/%s does not match private "
  729. "key: %e, skip signing",
  730. domain, selector, te);
  731. g_error_free (te);
  732. lua_pushboolean (L, FALSE);
  733. rspamd_dkim_key_unref (pk);
  734. return 1;
  735. }
  736. else {
  737. msg_warn_task ("public key for %s/%s does not match private "
  738. "key: %e",
  739. domain, selector, te);
  740. g_error_free (te);
  741. }
  742. }
  743. rspamd_dkim_key_unref (pk);
  744. }
  745. }
  746. ctx = rspamd_create_dkim_sign_context (task, dkim_key,
  747. DKIM_CANON_RELAXED, DKIM_CANON_RELAXED,
  748. headers, sign_type, &err);
  749. if (ctx == NULL) {
  750. msg_err_task ("cannot create sign context: %e",
  751. err);
  752. g_error_free (err);
  753. lua_pushboolean (L, FALSE);
  754. return 1;
  755. }
  756. hdr = rspamd_dkim_sign (task, selector, domain, 0,
  757. expire, arc_idx, arc_cv, ctx);
  758. if (hdr) {
  759. if (!no_cache) {
  760. rspamd_mempool_set_variable (task->task_pool, "dkim-signature",
  761. hdr, rspamd_gstring_free_hard);
  762. }
  763. lua_pushboolean (L, TRUE);
  764. lua_pushlstring (L, hdr->str, hdr->len);
  765. return 2;
  766. }
  767. lua_pushboolean (L, FALSE);
  768. lua_pushnil (L);
  769. return 2;
  770. }
  771. gint
  772. dkim_module_reconfig (struct rspamd_config *cfg)
  773. {
  774. struct module_ctx saved_ctx;
  775. saved_ctx = dkim_module_ctx->ctx;
  776. rspamd_mempool_delete (dkim_module_ctx->dkim_pool);
  777. if (dkim_module_ctx->dkim_hash) {
  778. rspamd_lru_hash_destroy (dkim_module_ctx->dkim_hash);
  779. }
  780. if (dkim_module_ctx->dkim_sign_hash) {
  781. rspamd_lru_hash_destroy (dkim_module_ctx->dkim_sign_hash);
  782. }
  783. memset (dkim_module_ctx, 0, sizeof (*dkim_module_ctx));
  784. dkim_module_ctx->ctx = saved_ctx;
  785. dkim_module_ctx->dkim_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "dkim");
  786. dkim_module_ctx->sign_headers = default_sign_headers;
  787. dkim_module_ctx->sign_condition_ref = -1;
  788. dkim_module_ctx->max_sigs = DEFAULT_MAX_SIGS;
  789. return dkim_module_config (cfg);
  790. }
  791. /*
  792. * Parse strict value for domain in format: 'reject_multiplier:deny_multiplier'
  793. */
  794. static gboolean
  795. dkim_module_parse_strict (const gchar *value, gdouble *allow, gdouble *deny)
  796. {
  797. const gchar *colon;
  798. gchar *err = NULL;
  799. gdouble val;
  800. gchar numbuf[64];
  801. colon = strchr (value, ':');
  802. if (colon) {
  803. rspamd_strlcpy (numbuf, value,
  804. MIN (sizeof (numbuf), (colon - value) + 1));
  805. val = strtod (numbuf, &err);
  806. if (err == NULL || *err == '\0') {
  807. *deny = val;
  808. colon++;
  809. rspamd_strlcpy (numbuf, colon, sizeof (numbuf));
  810. err = NULL;
  811. val = strtod (numbuf, &err);
  812. if (err == NULL || *err == '\0') {
  813. *allow = val;
  814. return TRUE;
  815. }
  816. }
  817. }
  818. return FALSE;
  819. }
  820. static void
  821. dkim_module_check (struct dkim_check_result *res)
  822. {
  823. gboolean all_done = TRUE;
  824. const gchar *strict_value;
  825. struct dkim_check_result *first, *cur = NULL;
  826. first = res->first;
  827. DL_FOREACH (first, cur) {
  828. if (cur->ctx == NULL) {
  829. continue;
  830. }
  831. if (cur->key != NULL && cur->res == -1) {
  832. cur->res = rspamd_dkim_check (cur->ctx, cur->key, cur->task);
  833. if (dkim_module_ctx->dkim_domains != NULL) {
  834. /* Perform strict check */
  835. if ((strict_value =
  836. rspamd_match_hash_map (dkim_module_ctx->dkim_domains,
  837. rspamd_dkim_get_domain (cur->ctx))) != NULL) {
  838. if (!dkim_module_parse_strict (strict_value, &cur->mult_allow,
  839. &cur->mult_deny)) {
  840. cur->mult_allow = dkim_module_ctx->strict_multiplier;
  841. cur->mult_deny = dkim_module_ctx->strict_multiplier;
  842. }
  843. }
  844. }
  845. }
  846. }
  847. DL_FOREACH (first, cur) {
  848. if (cur->ctx == NULL) {
  849. continue;
  850. }
  851. if (cur->res == -1) {
  852. /* Still need a key */
  853. all_done = FALSE;
  854. }
  855. }
  856. if (all_done) {
  857. DL_FOREACH (first, cur) {
  858. const gchar *symbol = NULL, *trace = NULL;
  859. gdouble symbol_weight = 1.0;
  860. if (cur->ctx == NULL) {
  861. continue;
  862. }
  863. if (cur->res == DKIM_REJECT) {
  864. symbol = dkim_module_ctx->symbol_reject;
  865. trace = "-";
  866. symbol_weight = cur->mult_deny * 1.0;
  867. }
  868. else if (cur->res == DKIM_CONTINUE) {
  869. symbol = dkim_module_ctx->symbol_allow;
  870. trace = "+";
  871. symbol_weight = cur->mult_allow * 1.0;
  872. }
  873. else if (cur->res == DKIM_PERM_ERROR) {
  874. trace = "~";
  875. symbol = dkim_module_ctx->symbol_permfail;
  876. }
  877. else if (cur->res == DKIM_TRYAGAIN) {
  878. trace = "?";
  879. symbol = dkim_module_ctx->symbol_tempfail;
  880. }
  881. if (symbol != NULL) {
  882. const gchar *domain = rspamd_dkim_get_domain (cur->ctx);
  883. gsize tracelen;
  884. gchar *tracebuf;
  885. tracelen = strlen (domain) + 3; /* :<trace>\0 */
  886. tracebuf = rspamd_mempool_alloc (cur->task->task_pool,
  887. tracelen);
  888. rspamd_snprintf (tracebuf, tracelen, "%s:%s", domain, trace);
  889. rspamd_task_insert_result (cur->task,
  890. symbol,
  891. symbol_weight,
  892. domain);
  893. rspamd_task_insert_result (cur->task,
  894. "DKIM_TRACE",
  895. 0.0,
  896. tracebuf);
  897. }
  898. }
  899. rspamd_session_watcher_pop (res->task->s, res->w);
  900. }
  901. }
  902. static void
  903. dkim_module_key_handler (rspamd_dkim_key_t *key,
  904. gsize keylen,
  905. rspamd_dkim_context_t *ctx,
  906. gpointer ud,
  907. GError *err)
  908. {
  909. struct dkim_check_result *res = ud;
  910. struct rspamd_task *task;
  911. task = res->task;
  912. if (key != NULL) {
  913. /*
  914. * We actually receive key with refcount = 1, so we just assume that
  915. * lru hash owns this object now
  916. */
  917. rspamd_lru_hash_insert (dkim_module_ctx->dkim_hash,
  918. g_strdup (rspamd_dkim_get_dns_key (ctx)),
  919. key, res->task->tv.tv_sec, rspamd_dkim_key_get_ttl (key));
  920. /* Another ref belongs to the check context */
  921. res->key = rspamd_dkim_key_ref (key);
  922. /* Release key when task is processed */
  923. rspamd_mempool_add_destructor (res->task->task_pool,
  924. dkim_module_key_dtor, res->key);
  925. }
  926. else {
  927. /* Insert tempfail symbol */
  928. msg_info_task ("cannot get key for domain %s: %e",
  929. rspamd_dkim_get_dns_key (ctx), err);
  930. if (err != NULL) {
  931. if (err->code == DKIM_SIGERROR_NOKEY) {
  932. res->res = DKIM_TRYAGAIN;
  933. }
  934. else {
  935. res->res = DKIM_PERM_ERROR;
  936. }
  937. }
  938. }
  939. if (err) {
  940. g_error_free (err);
  941. }
  942. dkim_module_check (res);
  943. }
  944. static void
  945. dkim_symbol_callback (struct rspamd_task *task, void *unused)
  946. {
  947. GPtrArray *hlist;
  948. rspamd_dkim_context_t *ctx;
  949. rspamd_dkim_key_t *key;
  950. GError *err = NULL;
  951. struct rspamd_mime_header *rh;
  952. struct dkim_check_result *res = NULL, *cur;
  953. guint checked = 0, i, *dmarc_checks;
  954. /* Allow dmarc */
  955. dmarc_checks = rspamd_mempool_get_variable (task->task_pool,
  956. RSPAMD_MEMPOOL_DMARC_CHECKS);
  957. if (dmarc_checks) {
  958. (*dmarc_checks) ++;
  959. }
  960. else {
  961. dmarc_checks = rspamd_mempool_alloc (task->task_pool,
  962. sizeof (*dmarc_checks));
  963. *dmarc_checks = 1;
  964. rspamd_mempool_set_variable (task->task_pool,
  965. RSPAMD_MEMPOOL_DMARC_CHECKS,
  966. dmarc_checks, NULL);
  967. }
  968. /* First check if plugin should be enabled */
  969. if ((!dkim_module_ctx->check_authed && task->user != NULL)
  970. || (!dkim_module_ctx->check_local &&
  971. rspamd_inet_address_is_local (task->from_addr, TRUE))) {
  972. msg_info_task ("skip DKIM checks for local networks and authorized users");
  973. return;
  974. }
  975. /* Check whitelist */
  976. if (rspamd_match_radix_map_addr (dkim_module_ctx->whitelist_ip,
  977. task->from_addr) != NULL) {
  978. msg_info_task ("skip DKIM checks for whitelisted address");
  979. return;
  980. }
  981. /* Now check if a message has its signature */
  982. hlist = rspamd_message_get_header_array (task,
  983. RSPAMD_DKIM_SIGNHEADER,
  984. FALSE);
  985. if (hlist != NULL && hlist->len > 0) {
  986. msg_debug_task ("dkim signature found");
  987. PTR_ARRAY_FOREACH (hlist, i, rh) {
  988. if (rh->decoded == NULL || rh->decoded[0] == '\0') {
  989. msg_info_task ("<%s> cannot load empty DKIM context",
  990. task->message_id);
  991. continue;
  992. }
  993. cur = rspamd_mempool_alloc0 (task->task_pool, sizeof (*cur));
  994. cur->first = res;
  995. cur->res = -1;
  996. cur->task = task;
  997. cur->mult_allow = 1.0;
  998. cur->mult_deny = 1.0;
  999. ctx = rspamd_create_dkim_context (rh->decoded,
  1000. task->task_pool,
  1001. dkim_module_ctx->time_jitter,
  1002. RSPAMD_DKIM_NORMAL,
  1003. &err);
  1004. if (ctx == NULL) {
  1005. if (err != NULL) {
  1006. msg_info_task ("<%s> cannot parse DKIM context: %e",
  1007. task->message_id, err);
  1008. g_error_free (err);
  1009. err = NULL;
  1010. }
  1011. else {
  1012. msg_info_task ("<%s> cannot parse DKIM context: "
  1013. "unknown error",
  1014. task->message_id);
  1015. }
  1016. continue;
  1017. }
  1018. else {
  1019. /* Get key */
  1020. cur->ctx = ctx;
  1021. if (dkim_module_ctx->trusted_only &&
  1022. (dkim_module_ctx->dkim_domains == NULL ||
  1023. rspamd_match_hash_map (dkim_module_ctx->dkim_domains,
  1024. rspamd_dkim_get_domain (ctx)) == NULL)) {
  1025. msg_debug_task ("skip dkim check for %s domain",
  1026. rspamd_dkim_get_domain (ctx));
  1027. continue;
  1028. }
  1029. key = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_hash,
  1030. rspamd_dkim_get_dns_key (ctx),
  1031. task->tv.tv_sec);
  1032. if (key != NULL) {
  1033. cur->key = rspamd_dkim_key_ref (key);
  1034. /* Release key when task is processed */
  1035. rspamd_mempool_add_destructor (task->task_pool,
  1036. dkim_module_key_dtor, cur->key);
  1037. }
  1038. else {
  1039. if (!rspamd_get_dkim_key (ctx,
  1040. task,
  1041. dkim_module_key_handler,
  1042. cur)) {
  1043. continue;
  1044. }
  1045. }
  1046. }
  1047. if (res == NULL) {
  1048. res = cur;
  1049. res->prev = res;
  1050. res->w = rspamd_session_get_watcher (task->s);
  1051. }
  1052. else {
  1053. cur->w = res->w;
  1054. DL_APPEND (res, cur);
  1055. }
  1056. checked ++;
  1057. if (checked > dkim_module_ctx->max_sigs) {
  1058. msg_info_task ("message has multiple signatures but we"
  1059. " stopped after %d checked signatures as limit"
  1060. " is reached", checked);
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. else {
  1066. rspamd_task_insert_result (task,
  1067. dkim_module_ctx->symbol_na,
  1068. 1.0,
  1069. NULL);
  1070. }
  1071. if (res != NULL) {
  1072. rspamd_session_watcher_push (task->s);
  1073. dkim_module_check (res);
  1074. }
  1075. }
  1076. static void
  1077. dkim_sign_callback (struct rspamd_task *task, void *unused)
  1078. {
  1079. lua_State *L;
  1080. struct rspamd_task **ptask;
  1081. gboolean sign = FALSE;
  1082. gint err_idx;
  1083. gint64 arc_idx = 0;
  1084. gsize len;
  1085. GString *tb, *hdr;
  1086. GError *err = NULL;
  1087. const gchar *selector = NULL, *domain = NULL, *key = NULL, *key_type = NULL,
  1088. *sign_type_str = NULL, *lru_key, *arc_cv = NULL;
  1089. rspamd_dkim_sign_context_t *ctx;
  1090. rspamd_dkim_sign_key_t *dkim_key;
  1091. enum rspamd_dkim_sign_key_type key_sign_type = RSPAMD_DKIM_SIGN_KEY_FILE;
  1092. enum rspamd_dkim_type sign_type = RSPAMD_DKIM_NORMAL;
  1093. guchar h[rspamd_cryptobox_HASHBYTES],
  1094. hex_hash[rspamd_cryptobox_HASHBYTES * 2 + 1];
  1095. if (dkim_module_ctx->sign_condition_ref != -1) {
  1096. sign = FALSE;
  1097. L = task->cfg->lua_state;
  1098. lua_pushcfunction (L, &rspamd_lua_traceback);
  1099. err_idx = lua_gettop (L);
  1100. lua_rawgeti (L, LUA_REGISTRYINDEX, dkim_module_ctx->sign_condition_ref);
  1101. ptask = lua_newuserdata (L, sizeof (struct rspamd_task *));
  1102. *ptask = task;
  1103. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1104. if (lua_pcall (L, 1, 1, err_idx) != 0) {
  1105. tb = lua_touserdata (L, -1);
  1106. msg_err_task ("call to user extraction script failed: %v", tb);
  1107. g_string_free (tb, TRUE);
  1108. }
  1109. else {
  1110. if (lua_istable (L, -1)) {
  1111. /*
  1112. * Get the following elements:
  1113. * - selector
  1114. * - domain
  1115. * - key
  1116. */
  1117. if (!rspamd_lua_parse_table_arguments (L, -1, &err,
  1118. "*key=V;*domain=S;*selector=S;type=S;key_type=S;"
  1119. "sign_type=S;arc_cv=S;arc_idx=I",
  1120. &len, &key, &domain, &selector,
  1121. &key_type, &key_type, &sign_type_str, &arc_cv,
  1122. &arc_idx)) {
  1123. msg_err_task ("invalid return value from sign condition: %e",
  1124. err);
  1125. g_error_free (err);
  1126. return;
  1127. }
  1128. if (key_type) {
  1129. if (strcmp (key_type, "file") == 0) {
  1130. key_sign_type = RSPAMD_DKIM_SIGN_KEY_FILE;
  1131. }
  1132. else if (strcmp (key_type, "base64") == 0) {
  1133. key_sign_type = RSPAMD_DKIM_SIGN_KEY_BASE64;
  1134. }
  1135. else if (strcmp (key_type, "pem") == 0) {
  1136. key_sign_type = RSPAMD_DKIM_SIGN_KEY_PEM;
  1137. }
  1138. else if (strcmp (key_type, "der") == 0 ||
  1139. strcmp (key_type, "raw") == 0) {
  1140. key_sign_type = RSPAMD_DKIM_SIGN_KEY_DER;
  1141. }
  1142. else {
  1143. lua_settop (L, 0);
  1144. luaL_error (L, "unknown key type: %s",
  1145. key_type);
  1146. return;
  1147. }
  1148. }
  1149. if (sign_type_str) {
  1150. if (strcmp (sign_type_str, "dkim") == 0) {
  1151. sign_type = RSPAMD_DKIM_NORMAL;
  1152. }
  1153. else if (strcmp (sign_type_str, "arc-sign") == 0) {
  1154. sign_type = RSPAMD_DKIM_ARC_SIG;
  1155. if (arc_idx == 0) {
  1156. lua_settop (L, 0);
  1157. luaL_error (L, "no arc idx specified");
  1158. return;
  1159. }
  1160. }
  1161. else if (strcmp (sign_type_str, "arc-seal") == 0) {
  1162. sign_type = RSPAMD_DKIM_ARC_SEAL;
  1163. if (arc_cv == NULL) {
  1164. lua_settop (L, 0);
  1165. luaL_error (L, "no arc cv specified");
  1166. return;
  1167. }
  1168. if (arc_idx == 0) {
  1169. lua_settop (L, 0);
  1170. luaL_error (L, "no arc idx specified");
  1171. return;
  1172. }
  1173. }
  1174. else {
  1175. lua_settop (L, 0);
  1176. luaL_error (L, "unknown sign type: %s",
  1177. sign_type_str);
  1178. return;
  1179. }
  1180. }
  1181. if (key_sign_type == RSPAMD_DKIM_SIGN_KEY_FILE) {
  1182. dkim_key = rspamd_lru_hash_lookup (
  1183. dkim_module_ctx->dkim_sign_hash,
  1184. key, time (NULL));
  1185. lru_key = key;
  1186. }
  1187. else {
  1188. /* Prehash */
  1189. memset (hex_hash, 0, sizeof (hex_hash));
  1190. rspamd_cryptobox_hash (h, key, len, NULL, 0);
  1191. rspamd_encode_hex_buf (h, sizeof (h),
  1192. hex_hash, sizeof (hex_hash));
  1193. dkim_key = rspamd_lru_hash_lookup (
  1194. dkim_module_ctx->dkim_sign_hash,
  1195. hex_hash, time (NULL));
  1196. lru_key = hex_hash;
  1197. }
  1198. if (dkim_key == NULL) {
  1199. dkim_key = rspamd_dkim_sign_key_load (key, len,
  1200. key_sign_type, &err);
  1201. if (dkim_key == NULL) {
  1202. msg_err_task ("cannot load dkim key %s: %e",
  1203. lru_key, err);
  1204. g_error_free (err);
  1205. return;
  1206. }
  1207. rspamd_lru_hash_insert (dkim_module_ctx->dkim_sign_hash,
  1208. g_strdup (lru_key), dkim_key,
  1209. time (NULL), 0);
  1210. }
  1211. else if (rspamd_dkim_sign_key_maybe_invalidate (dkim_key,
  1212. key_sign_type, key, len)) {
  1213. /*
  1214. * Invalidate and reload DKIM key,
  1215. * removal from lru cache also cleanup the key and value
  1216. */
  1217. rspamd_lru_hash_remove (dkim_module_ctx->dkim_sign_hash,
  1218. lru_key);
  1219. dkim_key = rspamd_dkim_sign_key_load (key, len,
  1220. key_sign_type, &err);
  1221. if (dkim_key == NULL) {
  1222. msg_err_task ("cannot load dkim key %s: %e",
  1223. lru_key, err);
  1224. g_error_free (err);
  1225. return;
  1226. }
  1227. rspamd_lru_hash_insert (dkim_module_ctx->dkim_sign_hash,
  1228. g_strdup (lru_key), dkim_key,
  1229. time (NULL), 0);
  1230. }
  1231. ctx = rspamd_create_dkim_sign_context (task, dkim_key,
  1232. DKIM_CANON_RELAXED, DKIM_CANON_RELAXED,
  1233. dkim_module_ctx->sign_headers,
  1234. sign_type,
  1235. &err);
  1236. if (ctx == NULL) {
  1237. msg_err_task ("cannot create sign context: %e",
  1238. err);
  1239. g_error_free (err);
  1240. return;
  1241. }
  1242. hdr = rspamd_dkim_sign (task, selector, domain, 0, 0,
  1243. arc_idx, arc_cv,
  1244. ctx);
  1245. if (hdr) {
  1246. rspamd_mempool_set_variable (task->task_pool,
  1247. "dkim-signature",
  1248. hdr, rspamd_gstring_free_hard);
  1249. }
  1250. sign = TRUE;
  1251. }
  1252. else {
  1253. sign = FALSE;
  1254. }
  1255. }
  1256. /* Result + error function */
  1257. lua_settop (L, 0);
  1258. if (!sign) {
  1259. msg_debug_task ("skip signing as dkim condition callback returned"
  1260. " false");
  1261. return;
  1262. }
  1263. }
  1264. }
  1265. struct rspamd_dkim_lua_verify_cbdata {
  1266. rspamd_dkim_context_t *ctx;
  1267. struct rspamd_task *task;
  1268. lua_State *L;
  1269. rspamd_dkim_key_t *key;
  1270. gint cbref;
  1271. };
  1272. static void
  1273. dkim_module_lua_push_verify_result (struct rspamd_dkim_lua_verify_cbdata *cbd,
  1274. gint code, GError *err)
  1275. {
  1276. struct rspamd_task **ptask, *task;
  1277. const gchar *error_str = "unknown error";
  1278. gboolean success = FALSE;
  1279. task = cbd->task;
  1280. switch (code) {
  1281. case DKIM_CONTINUE:
  1282. error_str = NULL;
  1283. success = TRUE;
  1284. break;
  1285. case DKIM_REJECT:
  1286. if (err) {
  1287. error_str = err->message;
  1288. }
  1289. else {
  1290. error_str = "reject";
  1291. }
  1292. break;
  1293. case DKIM_TRYAGAIN:
  1294. if (err) {
  1295. error_str = err->message;
  1296. }
  1297. else {
  1298. error_str = "tempfail";
  1299. }
  1300. break;
  1301. case DKIM_NOTFOUND:
  1302. if (err) {
  1303. error_str = err->message;
  1304. }
  1305. else {
  1306. error_str = "not found";
  1307. }
  1308. break;
  1309. case DKIM_RECORD_ERROR:
  1310. if (err) {
  1311. error_str = err->message;
  1312. }
  1313. else {
  1314. error_str = "bad record";
  1315. }
  1316. break;
  1317. case DKIM_PERM_ERROR:
  1318. if (err) {
  1319. error_str = err->message;
  1320. }
  1321. else {
  1322. error_str = "permanent error";
  1323. }
  1324. break;
  1325. default:
  1326. break;
  1327. }
  1328. lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbd->cbref);
  1329. ptask = lua_newuserdata (cbd->L, sizeof (*ptask));
  1330. *ptask = task;
  1331. lua_pushboolean (cbd->L, success);
  1332. lua_pushstring (cbd->L, error_str);
  1333. if (cbd->ctx) {
  1334. lua_pushstring (cbd->L, rspamd_dkim_get_domain (cbd->ctx));
  1335. }
  1336. else {
  1337. lua_pushnil (cbd->L);
  1338. }
  1339. if (lua_pcall (cbd->L, 4, 0, 0) != 0) {
  1340. msg_err_task ("call to verify callback failed: %s",
  1341. lua_tostring (cbd->L, -1));
  1342. lua_pop (cbd->L, 1);
  1343. }
  1344. luaL_unref (cbd->L, LUA_REGISTRYINDEX, cbd->cbref);
  1345. }
  1346. static void
  1347. dkim_module_lua_on_key (rspamd_dkim_key_t *key,
  1348. gsize keylen,
  1349. rspamd_dkim_context_t *ctx,
  1350. gpointer ud,
  1351. GError *err)
  1352. {
  1353. struct rspamd_dkim_lua_verify_cbdata *cbd = ud;
  1354. struct rspamd_task *task;
  1355. gint ret;
  1356. task = cbd->task;
  1357. if (key != NULL) {
  1358. /*
  1359. * We actually receive key with refcount = 1, so we just assume that
  1360. * lru hash owns this object now
  1361. */
  1362. rspamd_lru_hash_insert (dkim_module_ctx->dkim_hash,
  1363. g_strdup (rspamd_dkim_get_dns_key (ctx)),
  1364. key, cbd->task->tv.tv_sec, rspamd_dkim_key_get_ttl (key));
  1365. /* Another ref belongs to the check context */
  1366. cbd->key = rspamd_dkim_key_ref (key);
  1367. /* Release key when task is processed */
  1368. rspamd_mempool_add_destructor (cbd->task->task_pool,
  1369. dkim_module_key_dtor, cbd->key);
  1370. }
  1371. else {
  1372. /* Insert tempfail symbol */
  1373. msg_info_task ("cannot get key for domain %s: %e",
  1374. rspamd_dkim_get_dns_key (ctx), err);
  1375. if (err != NULL) {
  1376. if (err->code == DKIM_SIGERROR_NOKEY) {
  1377. dkim_module_lua_push_verify_result (cbd, DKIM_TRYAGAIN, err);
  1378. }
  1379. else {
  1380. dkim_module_lua_push_verify_result (cbd, DKIM_PERM_ERROR, err);
  1381. }
  1382. }
  1383. else {
  1384. dkim_module_lua_push_verify_result (cbd, DKIM_TRYAGAIN, NULL);
  1385. }
  1386. if (err) {
  1387. g_error_free (err);
  1388. }
  1389. return;
  1390. }
  1391. ret = rspamd_dkim_check (cbd->ctx, cbd->key, cbd->task);
  1392. dkim_module_lua_push_verify_result (cbd, ret, NULL);
  1393. }
  1394. static gint
  1395. lua_dkim_verify_handler (lua_State *L)
  1396. {
  1397. struct rspamd_task *task = lua_check_task (L, 1);
  1398. const gchar *sig = luaL_checkstring (L, 2);
  1399. rspamd_dkim_context_t *ctx;
  1400. struct rspamd_dkim_lua_verify_cbdata *cbd;
  1401. rspamd_dkim_key_t *key;
  1402. gint ret;
  1403. GError *err = NULL;
  1404. const gchar *type_str = NULL;
  1405. enum rspamd_dkim_type type = RSPAMD_DKIM_NORMAL;
  1406. if (task && sig && lua_isfunction (L, 3)) {
  1407. if (lua_isstring (L, 4)) {
  1408. type_str = lua_tostring (L, 4);
  1409. if (type_str) {
  1410. if (strcmp (type_str, "dkim") == 0) {
  1411. type = RSPAMD_DKIM_NORMAL;
  1412. }
  1413. else if (strcmp (type_str, "arc-sign") == 0) {
  1414. type = RSPAMD_DKIM_ARC_SIG;
  1415. }
  1416. else if (strcmp (type_str, "arc-seal") == 0) {
  1417. type = RSPAMD_DKIM_ARC_SEAL;
  1418. }
  1419. else {
  1420. lua_settop (L, 0);
  1421. return luaL_error (L, "unknown sign type: %s",
  1422. type_str);
  1423. }
  1424. }
  1425. }
  1426. ctx = rspamd_create_dkim_context (sig,
  1427. task->task_pool,
  1428. dkim_module_ctx->time_jitter,
  1429. type,
  1430. &err);
  1431. if (ctx == NULL) {
  1432. lua_pushboolean (L, false);
  1433. if (err) {
  1434. lua_pushstring (L, err->message);
  1435. g_error_free (err);
  1436. }
  1437. else {
  1438. lua_pushstring (L, "unknown error");
  1439. }
  1440. return 2;
  1441. }
  1442. cbd = rspamd_mempool_alloc (task->task_pool, sizeof (*cbd));
  1443. cbd->L = L;
  1444. cbd->task = task;
  1445. lua_pushvalue (L, 3);
  1446. cbd->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  1447. cbd->ctx = ctx;
  1448. cbd->key = NULL;
  1449. key = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_hash,
  1450. rspamd_dkim_get_dns_key (ctx),
  1451. task->tv.tv_sec);
  1452. if (key != NULL) {
  1453. cbd->key = rspamd_dkim_key_ref (key);
  1454. /* Release key when task is processed */
  1455. rspamd_mempool_add_destructor (task->task_pool,
  1456. dkim_module_key_dtor, cbd->key);
  1457. ret = rspamd_dkim_check (cbd->ctx, cbd->key, cbd->task);
  1458. dkim_module_lua_push_verify_result (cbd, ret, NULL);
  1459. }
  1460. else {
  1461. rspamd_get_dkim_key (ctx,
  1462. task,
  1463. dkim_module_lua_on_key,
  1464. cbd);
  1465. }
  1466. }
  1467. else {
  1468. return luaL_error (L, "invalid arguments");
  1469. }
  1470. lua_pushboolean (L, TRUE);
  1471. lua_pushnil (L);
  1472. return 2;
  1473. }
  1474. static gint
  1475. lua_dkim_canonicalize_handler (lua_State *L)
  1476. {
  1477. gsize nlen, vlen;
  1478. const gchar *hname = luaL_checklstring (L, 1, &nlen),
  1479. *hvalue = luaL_checklstring (L, 2, &vlen);
  1480. static gchar st_buf[8192];
  1481. gchar *buf;
  1482. guint inlen;
  1483. gboolean allocated = FALSE;
  1484. goffset r;
  1485. if (hname && hvalue && nlen > 0) {
  1486. inlen = nlen + vlen + sizeof (":" CRLF);
  1487. if (inlen > sizeof (st_buf)) {
  1488. buf = g_malloc (inlen);
  1489. allocated = TRUE;
  1490. }
  1491. else {
  1492. /* Faster */
  1493. buf = st_buf;
  1494. }
  1495. r = rspamd_dkim_canonize_header_relaxed_str (hname, hvalue, buf, inlen);
  1496. if (r == -1) {
  1497. lua_pushnil (L);
  1498. }
  1499. else {
  1500. lua_pushlstring (L, buf, r);
  1501. }
  1502. if (allocated) {
  1503. g_free (buf);
  1504. }
  1505. }
  1506. else {
  1507. return luaL_error (L, "invalid arguments");
  1508. }
  1509. return 1;
  1510. }