]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Improve error reporting for DKIM key access issues
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Jun 2018 10:32:43 +0000 (11:32 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Jun 2018 10:32:43 +0000 (11:32 +0100)
rules/rspamd.lua
src/lua/lua_util.c
src/plugins/lua/arc.lua
src/plugins/lua/dkim_signing.lua

index 6b53828eed0a30b398d6fad81140888bdb7f8f92..a193eb4955653344d97b7df2927ea183f73b7e1b 100644 (file)
@@ -23,6 +23,7 @@ rspamd_maps = {} -- Global maps
 
 local local_conf = rspamd_paths['CONFDIR']
 local local_rules = rspamd_paths['RULESDIR']
+local rspamd_util = require "rspamd_util"
 
 dofile(local_rules .. '/regexp/headers.lua')
 dofile(local_rules .. '/regexp/misc.lua')
@@ -36,26 +37,16 @@ dofile(local_rules .. '/http_headers.lua')
 dofile(local_rules .. '/forwarding.lua')
 dofile(local_rules .. '/mid.lua')
 
-local function file_exists(filename)
-  local file = io.open(filename)
-  if file then
-    io.close(file)
-    return true
-  else
-    return false
-  end
-end
-
-if file_exists(local_conf .. '/rspamd.local.lua') then
+if rspamd_util.file_exists(local_conf .. '/rspamd.local.lua') then
   dofile(local_conf .. '/rspamd.local.lua')
 else
   -- Legacy lua/rspamd.local.lua
-  if file_exists(local_conf .. '/lua/rspamd.local.lua') then
+  if rspamd_util.file_exists(local_conf .. '/lua/rspamd.local.lua') then
     dofile(local_conf .. '/lua/rspamd.local.lua')
   end
 end
 
-if file_exists(local_rules .. '/rspamd.classifiers.lua') then
+if rspamd_util.file_exists(local_rules .. '/rspamd.classifiers.lua') then
   dofile(local_rules .. '/rspamd.classifiers.lua')
 end
 
index f99ae7d1ff3695d0849c6fa299733f20b9dfbd4d..063a7aab7340c769e3588a41f8a6557e30c3b5d2 100644 (file)
@@ -414,7 +414,7 @@ LUA_FUNCTION_DEF (util, readpassphrase);
 /***
  * @function util.file_exists(file)
  * Checks if a specified file exists and is available for reading
- * @return {boolean} true if file exists
+ * @return {boolean,string} true if file exists + string error if not
  */
 LUA_FUNCTION_DEF (util, file_exists);
 
@@ -2399,15 +2399,24 @@ static gint
 lua_util_file_exists (lua_State *L)
 {
        const gchar *fname = luaL_checkstring (L, 1);
+       gint serrno;
 
        if (fname) {
-               lua_pushboolean (L, access (fname, R_OK) != -1);
+               if (access (fname, R_OK) == -1) {
+                       serrno = errno;
+                       lua_pushboolean (L, false);
+                       lua_pushstring (L, strerror (serrno));
+               }
+               else {
+                       lua_pushboolean (L, true);
+                       lua_pushnil (L);
+               }
        }
        else {
                return luaL_error (L, "invalid arguments");
        }
 
-       return 1;
+       return 2;
 }
 
 static gint
index b240b3b3cbb1789d2ed7d841e1f1c785f9eb3135..83b02c32e1cea847444469f88428c401684f70c4 100644 (file)
@@ -510,10 +510,16 @@ local function arc_signing_cb(task)
   else
     if (p.key and p.selector) then
       p.key = lua_util.template(p.key, {domain = p.domain, selector = p.selector})
-      if not rspamd_util.file_exists(p.key) then
-        rspamd_logger.debugm(N, task, 'file %s does not exists', p.key)
+      local exists,err = rspamd_util.file_exists(p.key)
+      if not exists then
+        if err and err == 'No such file or directory' then
+          rspamd_logger.debugm(N, task, 'cannot read key from %s: %s', p.key, err)
+        else
+          rspamd_logger.warnx(N, task, 'cannot read key from %s: %s', p.key, err)
+        end
         return false
       end
+
       local dret, hdr = dkim_sign(task, p)
       if dret then
         return arc_sign_seal(task, p, hdr)
index 9b8f0a04771be09afbcd82334ac4e622b4d932dc..5e057cf9ea9830200782c7895ae1569e82e47ffb 100644 (file)
@@ -117,10 +117,16 @@ local function dkim_signing_cb(task)
   else
     if (p.key and p.selector) then
       p.key = lutil.template(p.key, {domain = p.domain, selector = p.selector})
-      if not rspamd_util.file_exists(p.key) then
-        rspamd_logger.debugm(N, task, 'file %s does not exists', p.key)
+      local exists,err = rspamd_util.file_exists(p.key)
+      if not exists then
+        if err and err == 'No such file or directory' then
+          rspamd_logger.debugm(N, task, 'cannot read key from %s: %s', p.key, err)
+        else
+          rspamd_logger.warnx(N, task, 'cannot read key from %s: %s', p.key, err)
+        end
         return false
       end
+
       local sret, _ = sign_func(task, p)
       return sret
     else