Skip to content

Commit

Permalink
Update session management logic in lua plugins
Browse files Browse the repository at this point in the history
The previous TODO comment has been addressed and removed from the monitoring plugin. In the callback plugin, logic has been modified that handles session expiry and cleanup. Now, if an IMAP command is "NOOP", the session will expire in 24 hours.

Signed-off-by: Christian Roessner <c@roessner.co>
  • Loading branch information
Christian Roessner committed Jul 8, 2024
1 parent 4462bcb commit f70134e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
28 changes: 21 additions & 7 deletions server/lua-plugins.d/callback/callback.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ function nauthilus_run_callback()
result.state = "client disconnected"
result.dovecot_session = "unknown"

---@type boolean is_cmd_noop
local is_cmd_noop = false

---@param k table
---@param v any
for k, v in pairs(body_table) do
Expand Down Expand Up @@ -81,6 +84,10 @@ function nauthilus_run_callback()
result.remote_ip = field_value
elseif field_name == "remote_port" then
result.remote_port = field_value
elseif field_name == "cmd_name" then
if field_value == "NOOP" then
is_cmd_noop = true
end
end
end
end
Expand All @@ -89,14 +96,21 @@ function nauthilus_run_callback()

if result.category == "service:imap" or result.category == "service:lmtp" then
if result.dovecot_session ~= "unknown" then
-- Cleanup dovecot session
---@type string deleted
---@type string err_redis_hdel
local deleted, err_redis_hdel = nauthilus.redis_hdel("ntc:DS:" .. crypto.md5(result.user), result.dovecot_session)
if err_redis_hdel ~= nil then
result.removed_session_failure = err_redis_hdel
---@type string redis_key
local redis_key = "ntc:DS:" .. crypto.md5(result.user)

if is_cmd_noop then
nauthilus.redis_expire(redis_key, 86400)
else
result.removed_session = deleted
-- Cleanup dovecot session
---@type string deleted
---@type string err_redis_hdel
local deleted, err_redis_hdel = nauthilus.redis_hdel(redis_key, result.dovecot_session)
if err_redis_hdel ~= nil then
result.removed_session_failure = err_redis_hdel
else
result.removed_session = deleted
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion server/lua-plugins.d/filters/monitoring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local crypto = require("crypto")

function nauthilus_call_filter(request)
---@return string
local function get_dovecot_session() -- TODO: Trach server port as well
local function get_dovecot_session()
---@type table headers
local headers = nauthilus:get_all_http_request_headers()
---@param header_name string
Expand Down

0 comments on commit f70134e

Please sign in to comment.