From cbe84eb50c73dab2517e2a5197567eed79b8abb9 Mon Sep 17 00:00:00 2001 From: Evgeny Khramtsov Date: Mon, 8 Jul 2019 23:14:31 +0300 Subject: [PATCH] Check virtual host before running the command --- src/ejabberd_admin.erl | 53 +++++++++++++++++++++++++++++------------- src/ejabberd_ctl.erl | 6 +++-- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/ejabberd_admin.erl b/src/ejabberd_admin.erl index da50b013b..bde2cde8f 100644 --- a/src/ejabberd_admin.erl +++ b/src/ejabberd_admin.erl @@ -498,27 +498,42 @@ update_module(ModuleNameString) -> %%% register(User, Host, Password) -> - case ejabberd_auth:try_register(User, Host, Password) of - ok -> - {ok, io_lib:format("User ~s@~s successfully registered", [User, Host])}; - {error, exists} -> - Msg = io_lib:format("User ~s@~s already registered", [User, Host]), - {error, conflict, 10090, Msg}; - {error, Reason} -> - String = io_lib:format("Can't register user ~s@~s at node ~p: ~s", - [User, Host, node(), - mod_register:format_error(Reason)]), - {error, cannot_register, 10001, String} + case is_my_host(Host) of + true -> + case ejabberd_auth:try_register(User, Host, Password) of + ok -> + {ok, io_lib:format("User ~s@~s successfully registered", [User, Host])}; + {error, exists} -> + Msg = io_lib:format("User ~s@~s already registered", [User, Host]), + {error, conflict, 10090, Msg}; + {error, Reason} -> + String = io_lib:format("Can't register user ~s@~s at node ~p: ~s", + [User, Host, node(), + mod_register:format_error(Reason)]), + {error, cannot_register, 10001, String} + end; + false -> + {error, cannot_register, 10001, "Unknown virtual host"} end. unregister(User, Host) -> - ejabberd_auth:remove_user(User, Host), - {ok, ""}. + case is_my_host(Host) of + true -> + ejabberd_auth:remove_user(User, Host), + {ok, ""}; + false -> + {error, "Unknown virtual host"} + end. registered_users(Host) -> - Users = ejabberd_auth:get_users(Host), - SUsers = lists:sort(Users), - lists:map(fun({U, _S}) -> U end, SUsers). + case is_my_host(Host) of + true -> + Users = ejabberd_auth:get_users(Host), + SUsers = lists:sort(Users), + lists:map(fun({U, _S}) -> U end, SUsers); + false -> + {error, "Unknown virtual host"} + end. registered_vhosts() -> ejabberd_option:hosts(). @@ -812,3 +827,9 @@ mnesia_change_nodename(FromString, ToString, Source, Target) -> clear_cache() -> Nodes = ejabberd_cluster:get_nodes(), lists:foreach(fun(T) -> ets_cache:clear(T, Nodes) end, ets_cache:all()). + +-spec is_my_host(binary()) -> boolean(). +is_my_host(Host) -> + try ejabberd_router:is_my_host(Host) + catch _:{invalid_domain, _} -> false + end. diff --git a/src/ejabberd_ctl.erl b/src/ejabberd_ctl.erl index 989064f66..c9dfff9c6 100644 --- a/src/ejabberd_ctl.erl +++ b/src/ejabberd_ctl.erl @@ -331,8 +331,10 @@ try_call_command(Args, Auth, AccessCommands, Version) -> throw:Error -> {io_lib:format("~p", [Error]), ?STATUS_ERROR}; ?EX_RULE(A, Why, Stack) -> - {io_lib:format("Problem '~p ~p' occurred executing the command.~nStacktrace: ~p", - [A, Why, ?EX_STACK(Stack)]), ?STATUS_ERROR} + StackTrace = ?EX_STACK(Stack), + {io_lib:format("Unhandled exception occurred executing the command:~n** ~s", + [misc:format_exception(2, A, Why, StackTrace)]), + ?STATUS_ERROR} end. %% @spec (Args::[string()], Auth, AccessCommands) -> string() | integer() | {string(), integer()} | {error, ErrorType} -- 2.40.0