]> granicus.if.org Git - ejabberd/commitdiff
Start/stop auth modules when host is added/deleted
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>
Thu, 23 Feb 2017 13:19:22 +0000 (16:19 +0300)
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>
Thu, 23 Feb 2017 13:19:22 +0000 (16:19 +0300)
src/ejabberd_auth.erl
src/ejabberd_auth_external.erl
src/ejabberd_auth_ldap.erl
src/ejabberd_auth_mnesia.erl
src/ejabberd_auth_pam.erl
src/ejabberd_auth_riak.erl
src/ejabberd_auth_sql.erl
src/gen_mod.erl

index 88b39f48aec9bebb9415635cdd94cea4d50d1829..fd5b6b2444fb65eba854a1184cc5b63fa5eb3dc8 100644 (file)
@@ -32,7 +32,7 @@
 -author('alexey@process-one.net').
 
 %% External exports
--export([start/0, set_password/3, check_password/4,
+-export([start/0, start/1, stop/1, set_password/3, check_password/4,
         check_password/6, check_password_with_authmodule/4,
         check_password_with_authmodule/6, try_register/3,
         dirty_get_registered_users/0, get_vh_registered_users/1,
@@ -61,6 +61,7 @@
                  {offset, integer()}].
 
 -callback start(binary()) -> any().
+-callback stop(binary()) -> any().
 -callback plain_password_required() -> boolean().
 -callback store_type() -> plain | external | scram.
 -callback set_password(binary(), binary(), binary()) -> ok | {error, atom()}.
 -callback get_password_s(binary(), binary()) -> password().
 
 start() ->
-    %% This is only executed by ejabberd_c2s for non-SASL auth client
-    lists:foreach(fun (Host) ->
-                         lists:foreach(fun (M) -> M:start(Host) end,
-                                       auth_modules(Host))
-                 end,
-                 ?MYHOSTS).
+    ets:new(ejabberd_auth_modules, [named_table, public]),
+    ejabberd_hooks:add(host_up, ?MODULE, start, 30),
+    ejabberd_hooks:add(host_down, ?MODULE, stop, 80),
+    lists:foreach(fun start/1, ?MYHOSTS).
+
+start(Host) ->
+    Modules = auth_modules_from_config(Host),
+    ets:insert(ejabberd_auth_modules, {Host, Modules}),
+    lists:foreach(fun(M) -> M:start(Host) end, Modules).
+
+stop(Host) ->
+    OldModules = auth_modules(Host),
+    ets:delete(ejabberd_auth_modules, Host),
+    lists:foreach(fun(M) -> M:stop(Host) end, OldModules).
 
 plain_password_required(Server) ->
     lists:any(fun (M) -> M:plain_password_required() end,
@@ -429,21 +438,29 @@ backend_type(Mod) ->
 %% Return the lists of all the auth modules actually used in the
 %% configuration
 auth_modules() ->
-    lists:usort(lists:flatmap(fun (Server) ->
-                                     auth_modules(Server)
-                             end,
-                             ?MYHOSTS)).
+    lists:usort(lists:flatmap(fun auth_modules/1, ?MYHOSTS)).
 
 -spec auth_modules(binary()) -> [atom()].
 
 %% Return the list of authenticated modules for a given host
 auth_modules(Server) ->
+    LServer = jid:nameprep(Server),
+    try ets:lookup(ejabberd_auth_modules, LServer) of
+       [{_, Modules}] -> Modules;
+       _ -> []
+    catch error:badarg ->
+           %% ejabberd_auth is not started yet
+           auth_modules_from_config(Server)
+    end.
+
+-spec auth_modules_from_config(binary()) -> [module()].
+auth_modules_from_config(Server) ->
     LServer = jid:nameprep(Server),
     Default = ejabberd_config:default_db(LServer, ?MODULE),
     Methods = ejabberd_config:get_option(
                 {auth_method, LServer}, opt_type(auth_method), [Default]),
     [jlib:binary_to_atom(<<"ejabberd_auth_",
-                           (jlib:atom_to_binary(M))/binary>>)
+                          (jlib:atom_to_binary(M))/binary>>)
      || M <- Methods].
 
 export(Server) ->
index 333543b0b6950a9ecae28f976af20484ce594a08..4ba76cd99e093af2c3940e9fb511a4372855db97 100644 (file)
@@ -31,7 +31,7 @@
 
 -behaviour(ejabberd_auth).
 
--export([start/1, set_password/3, check_password/4,
+-export([start/1, stop/1, set_password/3, check_password/4,
         check_password/6, try_register/3,
         dirty_get_registered_users/0, get_vh_registered_users/1,
         get_vh_registered_users/2,
@@ -58,6 +58,10 @@ start(Host) ->
     check_cache_last_options(Host),
     ejabberd_auth_mnesia:start(Host).
 
+stop(Host) ->
+    extauth:stop(Host),
+    ejabberd_auth_mnesia:stop(Host).
+
 check_cache_last_options(Server) ->
     case get_cache_option(Server) of
       false -> no_cache;
index 75c6a0f6e3070769ad7db477adc7bacdb1fc8841..18770a512d45f065c5ccb68643c79119af2654f5 100644 (file)
@@ -90,7 +90,6 @@ start(Host) ->
 
 stop(Host) ->
     Proc = gen_mod:get_module_proc(Host, ?MODULE),
-    gen_server:call(Proc, stop),
     supervisor:terminate_child(ejabberd_sup, Proc),
     supervisor:delete_child(ejabberd_sup, Proc).
 
@@ -101,6 +100,7 @@ start_link(Host) ->
 terminate(_Reason, _State) -> ok.
 
 init(Host) ->
+    process_flag(trap_exit, true),
     State = parse_options(Host),
     eldap_pool:start_link(State#state.eldap_id,
                          State#state.servers, State#state.backups,
index 3145d90ca79e9b3bf4d0d2ae003e093a0ee12d9c..9d1632ef8559e5eb965d86ec558e48072c9b2d43 100644 (file)
@@ -33,7 +33,7 @@
 
 -behaviour(ejabberd_auth).
 
--export([start/1, set_password/3, check_password/4,
+-export([start/1, stop/1, set_password/3, check_password/4,
         check_password/6, try_register/3,
         dirty_get_registered_users/0, get_vh_registered_users/1,
         get_vh_registered_users/2, init_db/0,
@@ -65,6 +65,9 @@ start(Host) ->
     maybe_alert_password_scrammed_without_option(),
     ok.
 
+stop(_Host) ->
+    ok.
+
 init_db() ->
     ejabberd_mnesia:create(?MODULE, passwd,
                        [{disc_copies, [node()]},
index 9e5e63ac11216963d8e00aa318002a9a3a5ca28c..51ad3a8815e7bb8ad5142d81d16ea33b41f458ce 100644 (file)
@@ -30,7 +30,7 @@
 
 -behaviour(ejabberd_auth).
 
--export([start/1, set_password/3, check_password/4,
+-export([start/1, stop/1, set_password/3, check_password/4,
         check_password/6, try_register/3,
         dirty_get_registered_users/0, get_vh_registered_users/1,
         get_vh_registered_users/2,
@@ -43,6 +43,9 @@
 start(_Host) ->
     ejabberd:start_app(epam).
 
+stop(_Host) ->
+    ok.
+
 set_password(_User, _Server, _Password) ->
     {error, not_allowed}.
 
index 6067c35edbea1dd3263c3a8ae18fa242cd055b0c..69b1862c12f795c34fa98cbb0e472356a72e58f3 100644 (file)
@@ -34,7 +34,7 @@
 -behaviour(ejabberd_auth).
 
 %% External exports
--export([start/1, set_password/3, check_password/4,
+-export([start/1, stop/1, set_password/3, check_password/4,
         check_password/6, try_register/3,
         dirty_get_registered_users/0, get_vh_registered_users/1,
         get_vh_registered_users/2,
@@ -56,6 +56,9 @@
 start(_Host) ->
     ok.
 
+stop(_Host) ->
+    ok.
+
 plain_password_required() ->
     case is_scrammed() of
       false -> false;
index d8fc61703431537c977fad73b6e3f0e91192eea3..2cc7a278adeb61aca9f773f022995f33f54588f6 100644 (file)
@@ -33,7 +33,7 @@
 
 -behaviour(ejabberd_auth).
 
--export([start/1, set_password/3, check_password/4,
+-export([start/1, stop/1, set_password/3, check_password/4,
         check_password/6, try_register/3,
         dirty_get_registered_users/0, get_vh_registered_users/1,
         get_vh_registered_users/2,
@@ -54,6 +54,8 @@
 %%%----------------------------------------------------------------------
 start(_Host) -> ok.
 
+stop(_Host) -> ok.
+
 plain_password_required() ->
     case is_scrammed() of
       false -> false;
index 178fccb1fca5476e503b24fbc62d2b420d53ab64..77edb0e906f43995a61a3765439004f02c40a54d 100644 (file)
@@ -81,7 +81,7 @@ start_link() ->
 init([]) ->
     ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 50),
     ejabberd_hooks:add(host_up, ?MODULE, start_modules, 40),
-    ejabberd_hooks:add(host_down, ?MODULE, stop_modules, 80),
+    ejabberd_hooks:add(host_down, ?MODULE, stop_modules, 70),
     ets:new(ejabberd_modules,
            [named_table, public,
             {keypos, #ejabberd_module.module_host}]),