From: Vsevolod Stakhov Date: Sat, 27 May 2017 11:40:54 +0000 (+0100) Subject: [Minor] Add function to canonicalise headers X-Git-Tag: 1.6.0~124 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c407dfd27a5c085e9518534d0ca43507311daf60;p=rspamd.git [Minor] Add function to canonicalise headers --- diff --git a/src/plugins/dkim_check.c b/src/plugins/dkim_check.c index a1db0e5f4..4009662bf 100644 --- a/src/plugins/dkim_check.c +++ b/src/plugins/dkim_check.c @@ -99,6 +99,7 @@ static void dkim_sign_callback (struct rspamd_task *task, void *unused); static gint lua_dkim_sign_handler (lua_State *L); static gint lua_dkim_verify_handler (lua_State *L); +static gint lua_dkim_canonicalize_handler (lua_State *L); /* Initialization */ gint dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx); @@ -314,6 +315,9 @@ dkim_module_config (struct rspamd_config *cfg) lua_pushstring (cfg->lua_state, "verify"); lua_pushcfunction (cfg->lua_state, lua_dkim_verify_handler); lua_settable (cfg->lua_state, -3); + lua_pushstring (cfg->lua_state, "canon_header_relaxed"); + lua_pushcfunction (cfg->lua_state, lua_dkim_canonicalize_handler); + lua_settable (cfg->lua_state, -3); /* Finish dkim key */ lua_settable (cfg->lua_state, -3); } @@ -1531,3 +1535,47 @@ lua_dkim_verify_handler (lua_State *L) return 2; } + +static gint +lua_dkim_canonicalize_handler (lua_State *L) +{ + gsize nlen, vlen; + const gchar *hname = luaL_checklstring (L, 1, &nlen), + *hvalue = luaL_checklstring (L, 2, &vlen); + static gchar st_buf[8192]; + gchar *buf; + guint inlen; + gboolean allocated = FALSE; + goffset r; + + if (hname && hvalue && nlen > 0) { + inlen = nlen + vlen + sizeof (":" CRLF); + + if (inlen > sizeof (st_buf)) { + buf = g_malloc (inlen); + allocated = TRUE; + } + else { + /* Faster */ + buf = st_buf; + } + + r = rspamd_dkim_canonize_header_relaxed_str (hname, hvalue, buf, inlen); + + if (r == -1) { + lua_pushnil (L); + } + else { + lua_pushlstring (L, buf, r); + } + + if (allocated) { + g_free (buf); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +}