From 29462f00538765d4cb90a3d085367c23a95df0d1 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Wed, 25 Oct 2017 21:21:52 +0300 Subject: [PATCH] Make it possible to convert to SQL for any current db_type It is now possible to run `export` command even when the current `db_type` is not `sql` --- include/ejabberd_auth.hrl | 22 ++++++++++++++++++++++ src/ejabberd_auth.erl | 5 +---- src/ejabberd_auth_mnesia.erl | 29 ++--------------------------- src/ejabberd_auth_riak.erl | 4 +--- src/ejabberd_auth_sql.erl | 26 +++++++++++++++++++++++++- src/ejd2sql.erl | 9 +++++++-- src/mod_pubsub.erl | 5 +---- 7 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 include/ejabberd_auth.hrl diff --git a/include/ejabberd_auth.hrl b/include/ejabberd_auth.hrl new file mode 100644 index 000000000..48fa785e3 --- /dev/null +++ b/include/ejabberd_auth.hrl @@ -0,0 +1,22 @@ +%%%---------------------------------------------------------------------- +%%% +%%% ejabberd, Copyright (C) 2002-2017 ProcessOne +%%% +%%% This program is free software; you can redistribute it and/or +%%% modify it under the terms of the GNU General Public License as +%%% published by the Free Software Foundation; either version 2 of the +%%% License, or (at your option) any later version. +%%% +%%% This program is distributed in the hope that it will be useful, +%%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +%%% General Public License for more details. +%%% +%%% You should have received a copy of the GNU General Public License along +%%% with this program; if not, write to the Free Software Foundation, Inc., +%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +%%% +%%%---------------------------------------------------------------------- + +-record(passwd, {us = {<<"">>, <<"">>} :: {binary(), binary()} | '$1', + password = <<"">> :: binary() | scram() | '_'}). diff --git a/src/ejabberd_auth.erl b/src/ejabberd_auth.erl index b34925ff0..4ab82a4e3 100644 --- a/src/ejabberd_auth.erl +++ b/src/ejabberd_auth.erl @@ -35,7 +35,7 @@ check_password/6, check_password_with_authmodule/4, check_password_with_authmodule/6, try_register/3, get_users/0, get_users/1, password_to_scram/1, - get_users/2, export/1, import_info/0, + get_users/2, import_info/0, count_users/1, import/5, import_start/2, count_users/2, get_password/2, get_password_s/2, get_password_with_authmodule/2, @@ -798,9 +798,6 @@ validate_credentials(User, Server, Password) -> end end. -export(Server) -> - ejabberd_auth_mnesia:export(Server). - import_info() -> [{<<"users">>, 3}]. diff --git a/src/ejabberd_auth_mnesia.erl b/src/ejabberd_auth_mnesia.erl index 690152674..14b8e5a2d 100644 --- a/src/ejabberd_auth_mnesia.erl +++ b/src/ejabberd_auth_mnesia.erl @@ -34,16 +34,14 @@ -export([start/1, stop/1, set_password/3, try_register/3, get_users/2, init_db/0, count_users/2, get_password/2, - remove_user/2, store_type/1, export/1, import/2, + remove_user/2, store_type/1, import/2, plain_password_required/1, use_cache/1]). -export([need_transform/1, transform/1]). -include("ejabberd.hrl"). -include("logger.hrl"). -include("ejabberd_sql_pt.hrl"). - --record(passwd, {us = {<<"">>, <<"">>} :: {binary(), binary()} | '$1', - password = <<"">> :: binary() | scram() | '_'}). +-include("ejabberd_auth.hrl"). -record(reg_users_counter, {vhost = <<"">> :: binary(), count = 0 :: integer() | '$1'}). @@ -272,29 +270,6 @@ transform(#passwd{password = Password} = P) when is_record(Password, scram) -> P. -export(_Server) -> - [{passwd, - fun(Host, #passwd{us = {LUser, LServer}, password = Password}) - when LServer == Host, - is_binary(Password) -> - [?SQL("delete from users where username=%(LUser)s;"), - ?SQL("insert into users(username, password) " - "values (%(LUser)s, %(Password)s);")]; - (Host, #passwd{us = {LUser, LServer}, password = #scram{} = Scram}) - when LServer == Host -> - StoredKey = Scram#scram.storedkey, - ServerKey = Scram#scram.serverkey, - Salt = Scram#scram.salt, - IterationCount = Scram#scram.iterationcount, - [?SQL("delete from users where username=%(LUser)s;"), - ?SQL("insert into users(username, password, serverkey, salt, " - "iterationcount) " - "values (%(LUser)s, %(StoredKey)s, %(ServerKey)s," - " %(Salt)s, %(IterationCount)d);")]; - (_Host, _R) -> - [] - end}]. - import(LServer, [LUser, Password, _TimeStamp]) -> mnesia:dirty_write( #passwd{us = {LUser, LServer}, password = Password}). diff --git a/src/ejabberd_auth_riak.erl b/src/ejabberd_auth_riak.erl index fccaba102..37bd3daf4 100644 --- a/src/ejabberd_auth_riak.erl +++ b/src/ejabberd_auth_riak.erl @@ -40,9 +40,7 @@ -include("ejabberd.hrl"). -include("ejabberd_sql_pt.hrl"). - --record(passwd, {us = {<<"">>, <<"">>} :: {binary(), binary()} | '$1', - password = <<"">> :: binary() | scram() | '_'}). +-include("ejabberd_auth.hrl"). start(_Host) -> ok. diff --git a/src/ejabberd_auth_sql.erl b/src/ejabberd_auth_sql.erl index 0d7c7b375..15e5076c9 100644 --- a/src/ejabberd_auth_sql.erl +++ b/src/ejabberd_auth_sql.erl @@ -35,11 +35,12 @@ -export([start/1, stop/1, set_password/3, try_register/3, get_users/2, count_users/2, get_password/2, remove_user/2, store_type/1, plain_password_required/1, - convert_to_scram/1, opt_type/1]). + convert_to_scram/1, opt_type/1, export/1]). -include("ejabberd.hrl"). -include("logger.hrl"). -include("ejabberd_sql_pt.hrl"). +-include("ejabberd_auth.hrl"). -define(SALT_LENGTH, 16). @@ -288,6 +289,29 @@ convert_to_scram(Server) -> end end. +export(_Server) -> + [{passwd, + fun(Host, #passwd{us = {LUser, LServer}, password = Password}) + when LServer == Host, + is_binary(Password) -> + [?SQL("delete from users where username=%(LUser)s;"), + ?SQL("insert into users(username, password) " + "values (%(LUser)s, %(Password)s);")]; + (Host, #passwd{us = {LUser, LServer}, password = #scram{} = Scram}) + when LServer == Host -> + StoredKey = Scram#scram.storedkey, + ServerKey = Scram#scram.serverkey, + Salt = Scram#scram.salt, + IterationCount = Scram#scram.iterationcount, + [?SQL("delete from users where username=%(LUser)s;"), + ?SQL("insert into users(username, password, serverkey, salt, " + "iterationcount) " + "values (%(LUser)s, %(StoredKey)s, %(ServerKey)s," + " %(Salt)s, %(IterationCount)d);")]; + (_Host, _R) -> + [] + end}]. + -spec opt_type(pgsql_users_number_estimate) -> fun((boolean()) -> boolean()); (atom()) -> [atom()]. opt_type(pgsql_users_number_estimate) -> diff --git a/src/ejd2sql.erl b/src/ejd2sql.erl index c801eb973..7b98d2494 100644 --- a/src/ejd2sql.erl +++ b/src/ejd2sql.erl @@ -73,7 +73,12 @@ export(Server, Output) -> end, Modules), close_output(Output, IO). -export(Server, Output, Module) -> +export(Server, Output, Module1) -> + Module = case Module1 of + mod_pubsub -> pubsub_db; + _ -> Module1 + end, + SQLMod = gen_mod:db_mod(sql, Module), LServer = jid:nameprep(iolist_to_binary(Server)), IO = prepare_output(Output), lists:foreach( @@ -84,7 +89,7 @@ export(Server, Output, Module) -> ?ERROR_MSG("Failed export for module ~p and table ~p: ~p", [Module, Table, Reason]) end - end, Module:export(Server)), + end, SQLMod:export(Server)), close_output(Output, IO). delete(Server) -> diff --git a/src/mod_pubsub.erl b/src/mod_pubsub.erl index 1028a048d..913c76f93 100644 --- a/src/mod_pubsub.erl +++ b/src/mod_pubsub.erl @@ -89,7 +89,7 @@ %% API and gen_server callbacks -export([start/2, stop/1, init/1, handle_call/3, handle_cast/2, handle_info/2, - terminate/2, code_change/3, depends/2, export/1, mod_opt_type/1]). + terminate/2, code_change/3, depends/2, mod_opt_type/1]). %%==================================================================== %% API @@ -3825,9 +3825,6 @@ purge_offline(Host, LJID, Node) -> Error end. -export(Server) -> - pubsub_db_sql:export(Server). - mod_opt_type(access_createnode) -> fun acl:access_rules_validator/1; mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end; mod_opt_type(host) -> fun iolist_to_binary/1;