]> granicus.if.org Git - ejabberd/commitdiff
Check virtual host before running the command
authorEvgeny Khramtsov <ekhramtsov@process-one.net>
Mon, 8 Jul 2019 20:14:31 +0000 (23:14 +0300)
committerEvgeny Khramtsov <ekhramtsov@process-one.net>
Mon, 8 Jul 2019 20:14:31 +0000 (23:14 +0300)
src/ejabberd_admin.erl
src/ejabberd_ctl.erl

index da50b013be724285e6673bd93f91587bd6416ed1..bde2cde8f5dc5c8c3c5eee0f3d04bbb8ee746d59 100644 (file)
@@ -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.
index 989064f66820c170b015a9c67f7df338e1df3987..c9dfff9c600dd79a01fcd7842da847d81cc8fef0 100644 (file)
@@ -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}