Mercurial > prosody-modules
comparison mod_host_guard/mod_host_guard.lua @ 753:9d5731af2c27
Merge with Oliver Gerlich
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 27 Jul 2012 14:29:59 +0100 |
| parents | ce8e7b784be0 |
| children | b16b291d68c3 |
comparison
equal
deleted
inserted
replaced
| 752:9bbd99f2057a | 753:9d5731af2c27 |
|---|---|
| 6 local guard_blockall = module:get_option_set("host_guard_blockall", {}) | 6 local guard_blockall = module:get_option_set("host_guard_blockall", {}) |
| 7 local guard_ball_wl = module:get_option_set("host_guard_blockall_exceptions", {}) | 7 local guard_ball_wl = module:get_option_set("host_guard_blockall_exceptions", {}) |
| 8 local guard_protect = module:get_option_set("host_guard_selective", {}) | 8 local guard_protect = module:get_option_set("host_guard_selective", {}) |
| 9 local guard_block_bl = module:get_option_set("host_guard_blacklist", {}) | 9 local guard_block_bl = module:get_option_set("host_guard_blacklist", {}) |
| 10 | 10 |
| 11 local config = require "core.configmanager" | 11 local config = configmanager |
| 12 local error_reply = require "util.stanza".error_reply | 12 local error_reply = require "util.stanza".error_reply |
| 13 | 13 |
| 14 local function s2s_hook (event) | 14 local function s2s_hook (event) |
| 15 local origin, stanza = event.session or event.origin, event.stanza or false | 15 local origin, stanza = event.session or event.origin, event.stanza or false |
| 16 local to_host, from_host = (not stanza and origin.to_host) or stanza.attr.to, (not stanza and origin.from_host) or stanza.attr.from | 16 local to_host, from_host = (not stanza and origin.to_host) or stanza.attr.to, (not stanza and origin.from_host) or stanza.attr.from |
| 38 end | 38 end |
| 39 | 39 |
| 40 return nil | 40 return nil |
| 41 end | 41 end |
| 42 | 42 |
| 43 local function handle_activation (host) | 43 local function handle_activation (host, u) |
| 44 if guard_blockall:contains(host) or guard_protect:contains(host) then | 44 if guard_blockall:contains(host) or guard_protect:contains(host) then |
| 45 if hosts[host] and hosts[host].events then | 45 if hosts[host] and hosts[host].events then |
| 46 hosts[host].events.add_handler("s2sin-established", s2s_hook, 500) | 46 hosts[host].events.add_handler("s2sin-established", s2s_hook, 500) |
| 47 hosts[host].events.add_handler("route/remote", rr_hook, 500) | 47 hosts[host].events.add_handler("route/remote", rr_hook, 500) |
| 48 hosts[host].events.add_handler("stanza/jabber:server:dialback:result", s2s_hook, 500) | 48 hosts[host].events.add_handler("stanza/jabber:server:dialback:result", s2s_hook, 500) |
| 49 module:log ("debug", "adding host protection for: "..host) | 49 if not u then |
| 50 module:log ("debug", "adding host protection for: "..host) | |
| 51 else | |
| 52 module:log ("debug", "updating or adding host protection for: "..host) | |
| 53 end | |
| 50 end | 54 end |
| 51 end | 55 end |
| 52 end | 56 end |
| 53 | 57 |
| 54 local function handle_deactivation (host) | 58 local function handle_deactivation (host, u, i) |
| 55 if guard_blockall:contains(host) or guard_protect:contains(host) then | 59 if guard_blockall:contains(host) or guard_protect:contains(host) then |
| 56 if hosts[host] and hosts[host].events then | 60 if hosts[host] and hosts[host].events then |
| 57 hosts[host].events.remove_handler("s2sin-established", s2s_hook) | 61 hosts[host].events.remove_handler("s2sin-established", s2s_hook) |
| 58 hosts[host].events.remove_handler("route/remote", rr_hook) | 62 hosts[host].events.remove_handler("route/remote", rr_hook) |
| 59 hosts[host].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook) | 63 hosts[host].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook) |
| 60 module:log ("debug", "removing host protection for: "..host) | 64 if not u and not i then module:log ("debug", "removing host protection for: "..host) end |
| 61 end | 65 end |
| 62 end | 66 end |
| 63 end | 67 end |
| 64 | 68 |
| 65 local function init_hosts() | 69 local function init_hosts(u, i) |
| 66 for n,table in pairs(hosts) do | 70 for n in pairs(hosts) do |
| 67 hosts[n].events.remove_handler("s2sin-established", s2s_hook) | 71 if guard_blockall:contains(n) or guard_protect:contains(n) then |
| 68 hosts[n].events.remove_handler("route/remote", rr_hook) | 72 handle_deactivation(n, u, i) |
| 69 hosts[n].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook) | 73 handle_activation(n, u) |
| 70 if guard_blockall:contains(n) or guard_protect:contains(n) then handle_activation(n) end | 74 end |
| 71 end | 75 end |
| 72 end | 76 end |
| 73 | 77 |
| 74 local function reload() | 78 local function reload() |
| 75 module:log ("debug", "server configuration reloaded, rehashing plugin tables...") | 79 module:log ("debug", "server configuration reloaded, rehashing plugin tables...") |
| 76 guard_blockall = module:get_option_set("host_guard_blockall", {}) | 80 guard_blockall = module:get_option_set("host_guard_blockall", {}) |
| 77 guard_ball_wl = module:get_option_set("host_guard_blockall_exceptions", {}) | 81 guard_ball_wl = module:get_option_set("host_guard_blockall_exceptions", {}) |
| 78 guard_protect = module:get_option_set("host_guard_selective", {}) | 82 guard_protect = module:get_option_set("host_guard_selective", {}) |
| 79 guard_block_bl = module:get_option_set("host_guard_blacklist", {}) | 83 guard_block_bl = module:get_option_set("host_guard_blacklist", {}) |
| 80 | 84 |
| 81 init_hosts() | 85 init_hosts(true) |
| 82 end | 86 end |
| 83 | 87 |
| 84 local function setup() | 88 local function setup() |
| 85 module:log ("debug", "initializing host guard module...") | 89 module:log ("debug", "initializing host guard module...") |
| 86 module:hook ("host-activated", handle_activation) | 90 module:hook ("host-activated", handle_activation) |
| 87 module:hook ("host-deactivated", handle_deactivation) | 91 module:hook ("host-deactivated", handle_deactivation) |
| 88 module:hook ("config-reloaded", reload) | 92 module:hook ("config-reloaded", reload) |
| 89 | 93 |
| 90 init_hosts() | 94 init_hosts(false, true) |
| 95 end | |
| 96 | |
| 97 function module.unload() | |
| 98 module:log ("debug", "removing host handlers as module is being unloaded...") | |
| 99 for n in pairs(hosts) do | |
| 100 hosts[n].events.remove_handler("s2sin-established", s2s_hook) | |
| 101 hosts[n].events.remove_handler("route/remote", rr_hook) | |
| 102 hosts[n].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook) | |
| 103 end | |
| 91 end | 104 end |
| 92 | 105 |
| 93 if prosody.start_time then | 106 if prosody.start_time then |
| 94 setup() | 107 setup() |
| 95 else | 108 else |
