2008-10-12 13:53:25 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_commands.erl
|
|
|
|
%%% Author : Badlop <badlop@process-one.net>
|
|
|
|
%%% Purpose : Management of ejabberd commands
|
|
|
|
%%% Created : 20 May 2008 by Badlop <badlop@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2008-10-12 13:53:25 +02:00
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2008-10-12 13:53:25 +02:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%% @headerfile "ejabberd_commands.hrl"
|
|
|
|
|
|
|
|
%%% @doc Management of ejabberd commands.
|
|
|
|
%%%
|
|
|
|
%%% An ejabberd command is an abstract function identified by a name,
|
|
|
|
%%% with a defined number and type of calling arguments and type of
|
|
|
|
%%% result, that can be defined in any Erlang module and executed
|
|
|
|
%%% using any valid frontend.
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% == Define a new ejabberd command ==
|
|
|
|
%%%
|
|
|
|
%%% ejabberd commands can be defined and registered in
|
|
|
|
%%% any Erlang module.
|
|
|
|
%%%
|
|
|
|
%%% Some commands are procedures; and their purpose is to perform an
|
|
|
|
%%% action in the server, so the command result is only some result
|
|
|
|
%%% code or result tuple. Other commands are inspectors, and their
|
|
|
|
%%% purpose is to gather some information about the server and return
|
|
|
|
%%% a detailed response: it can be integer, string, atom, tuple, list
|
|
|
|
%%% or a mix of those ones.
|
|
|
|
%%%
|
|
|
|
%%% The arguments and result of an ejabberd command are strictly
|
|
|
|
%%% defined. The number and format of the arguments provided when
|
|
|
|
%%% calling an ejabberd command must match the definition of that
|
|
|
|
%%% command. The format of the result provided by an ejabberd command
|
|
|
|
%%% must be exactly its definition. For example, if a command is said
|
|
|
|
%%% to return an integer, it must always return an integer (except in
|
|
|
|
%%% case of a crash).
|
|
|
|
%%%
|
|
|
|
%%% If you are developing an Erlang module that will run inside
|
|
|
|
%%% ejabberd and you want to provide a new ejabberd command to
|
|
|
|
%%% administer some task related to your module, you only need to:
|
|
|
|
%%% implement a function, define the command, and register it.
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% === Define a new ejabberd command ===
|
|
|
|
%%%
|
|
|
|
%%% An ejabberd command is defined using the Erlang record
|
|
|
|
%%% 'ejabberd_commands'. This record has several elements that you
|
|
|
|
%%% must define. Note that 'tags', 'desc' and 'longdesc' are optional.
|
|
|
|
%%%
|
|
|
|
%%% For example let's define an ejabberd command 'pow' that gets the
|
|
|
|
%%% integers 'base' and 'exponent'. Its result will be an integer
|
|
|
|
%%% 'power':
|
|
|
|
%%%
|
|
|
|
%%% <pre>#ejabberd_commands{name = pow, tags = [test],
|
|
|
|
%%% desc = "Return the power of base for exponent",
|
|
|
|
%%% longdesc = "This is an example command. The formula is:\n"
|
|
|
|
%%% " power = base ^ exponent",
|
|
|
|
%%% module = ?MODULE, function = pow,
|
|
|
|
%%% args = [{base, integer}, {exponent, integer}],
|
|
|
|
%%% result = {power, integer}}</pre>
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% === Implement the function associated to the command ===
|
|
|
|
%%%
|
|
|
|
%%% Now implement a function in your module that matches the arguments
|
|
|
|
%%% and result of the ejabberd command.
|
|
|
|
%%%
|
|
|
|
%%% For example the function calc_power gets two integers Base and
|
|
|
|
%%% Exponent. It calculates the power and rounds to an integer:
|
|
|
|
%%%
|
|
|
|
%%% <pre>calc_power(Base, Exponent) ->
|
|
|
|
%%% PowFloat = math:pow(Base, Exponent),
|
|
|
|
%%% round(PowFloat).</pre>
|
|
|
|
%%%
|
2016-03-31 13:53:31 +02:00
|
|
|
%%% Since this function will be called by ejabberd_commands, it must
|
|
|
|
%%% be exported.
|
2008-10-12 13:53:25 +02:00
|
|
|
%%% Add to your module:
|
|
|
|
%%% <pre>-export([calc_power/2]).</pre>
|
|
|
|
%%%
|
|
|
|
%%% Only some types of result formats are allowed.
|
|
|
|
%%% If the format is defined as 'rescode', then your function must return:
|
|
|
|
%%% ok | true | atom()
|
|
|
|
%%% where the atoms ok and true as considered positive answers,
|
|
|
|
%%% and any other response atom is considered negative.
|
|
|
|
%%%
|
|
|
|
%%% If the format is defined as 'restuple', then the command must return:
|
|
|
|
%%% {rescode(), string()}
|
|
|
|
%%%
|
|
|
|
%%% If the format is defined as '{list, something()}', then the command
|
|
|
|
%%% must return a list of something().
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% === Register the command ===
|
|
|
|
%%%
|
|
|
|
%%% Define this function and put inside the #ejabberd_command you
|
|
|
|
%%% defined in the beginning:
|
|
|
|
%%%
|
|
|
|
%%% <pre>commands() ->
|
|
|
|
%%% [
|
|
|
|
%%%
|
|
|
|
%%% ].</pre>
|
|
|
|
%%%
|
|
|
|
%%% You need to include this header file in order to use the record:
|
|
|
|
%%%
|
|
|
|
%%% <pre>-include("ejabberd_commands.hrl").</pre>
|
|
|
|
%%%
|
|
|
|
%%% When your module is initialized or started, register your commands:
|
|
|
|
%%%
|
|
|
|
%%% <pre>ejabberd_commands:register_commands(commands()),</pre>
|
|
|
|
%%%
|
|
|
|
%%% And when your module is stopped, unregister your commands:
|
|
|
|
%%%
|
|
|
|
%%% <pre>ejabberd_commands:unregister_commands(commands()),</pre>
|
|
|
|
%%%
|
|
|
|
%%% That's all! Now when your module is started, the command will be
|
|
|
|
%%% registered and any frontend can access it. For example:
|
|
|
|
%%%
|
|
|
|
%%% <pre>$ ejabberdctl help pow
|
|
|
|
%%%
|
|
|
|
%%% Command Name: pow
|
|
|
|
%%%
|
|
|
|
%%% Arguments: base::integer
|
|
|
|
%%% exponent::integer
|
|
|
|
%%%
|
|
|
|
%%% Returns: power::integer
|
|
|
|
%%%
|
|
|
|
%%% Tags: test
|
|
|
|
%%%
|
|
|
|
%%% Description: Return the power of base for exponent
|
|
|
|
%%%
|
|
|
|
%%% This is an example command. The formula is:
|
|
|
|
%%% power = base ^ exponent
|
|
|
|
%%%
|
|
|
|
%%% $ ejabberdctl pow 3 4
|
|
|
|
%%% 81
|
|
|
|
%%% </pre>
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% == Execute an ejabberd command ==
|
|
|
|
%%%
|
|
|
|
%%% ejabberd commands are mean to be executed using any valid
|
|
|
|
%%% frontend. An ejabberd commands is implemented in a regular Erlang
|
|
|
|
%%% function, so it is also possible to execute this function in any
|
|
|
|
%%% Erlang module, without dealing with the associated ejabberd
|
|
|
|
%%% commands.
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% == Frontend to ejabberd commands ==
|
|
|
|
%%%
|
|
|
|
%%% Currently there are two frontends to ejabberd commands: the shell
|
|
|
|
%%% script {@link ejabberd_ctl. ejabberdctl}, and the XML-RPC server
|
2008-10-13 11:01:08 +02:00
|
|
|
%%% ejabberd_xmlrpc.
|
2008-10-12 13:53:25 +02:00
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% === ejabberdctl as a frontend to ejabberd commands ===
|
|
|
|
%%%
|
|
|
|
%%% It is possible to use ejabberdctl to get documentation of any
|
|
|
|
%%% command. But ejabberdctl does not support all the argument types
|
|
|
|
%%% allowed in ejabberd commands, so there are some ejabberd commands
|
|
|
|
%%% that cannot be executed using ejabberdctl.
|
|
|
|
%%%
|
|
|
|
%%% Also note that the ejabberdctl shell administration script also
|
|
|
|
%%% manages ejabberdctl commands, which are unrelated to ejabberd
|
|
|
|
%%% commands and can only be executed using ejabberdctl.
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% === ejabberd_xmlrpc as a frontend to ejabberd commands ===
|
|
|
|
%%%
|
|
|
|
%%% ejabberd_xmlrpc provides an XML-RPC server to execute ejabberd commands.
|
|
|
|
%%% ejabberd_xmlrpc is a contributed module published in ejabberd-modules SVN.
|
|
|
|
%%%
|
|
|
|
%%% Since ejabberd_xmlrpc does not provide any method to get documentation
|
|
|
|
%%% of the ejabberd commands, please use ejabberdctl to know which
|
|
|
|
%%% commands are available, and their usage.
|
|
|
|
%%%
|
|
|
|
%%% The number and format of the arguments provided when calling an
|
|
|
|
%%% ejabberd command must match the definition of that command. Please
|
|
|
|
%%% make sure the XML-RPC call provides the required arguments, with
|
|
|
|
%%% the specified format. The order of the arguments in an XML-RPC
|
|
|
|
%%% call is not important, because all the data is tagged and will be
|
2008-10-13 11:01:08 +02:00
|
|
|
%%% correctly prepared by ejabberd_xmlrpc before executing the ejabberd
|
2008-10-12 13:53:25 +02:00
|
|
|
%%% command.
|
|
|
|
|
|
|
|
%%% TODO: consider this feature:
|
|
|
|
%%% All commands are catched. If an error happens, return the restuple:
|
|
|
|
%%% {error, flattened error string}
|
2016-03-31 13:53:31 +02:00
|
|
|
%%% This means that ecomm call APIs (ejabberd_ctl, ejabberd_xmlrpc)
|
|
|
|
%%% need to allows this. And ejabberd_xmlrpc must be prepared to
|
|
|
|
%%% handle such an unexpected response.
|
2008-10-12 13:53:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
-module(ejabberd_commands).
|
|
|
|
-author('badlop@process-one.net').
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-behaviour(gen_server).
|
2017-04-29 10:39:40 +02:00
|
|
|
-behaviour(ejabberd_config).
|
2017-02-26 13:10:59 +01:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
-define(DEFAULT_VERSION, 1000000).
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-export([start_link/0,
|
2008-10-12 13:53:25 +02:00
|
|
|
list_commands/0,
|
2016-03-31 13:53:31 +02:00
|
|
|
list_commands/1,
|
2008-10-12 13:53:25 +02:00
|
|
|
get_command_format/1,
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_format/2,
|
|
|
|
get_command_format/3,
|
2008-10-12 13:53:25 +02:00
|
|
|
get_command_definition/1,
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_definition/2,
|
2008-10-12 13:53:25 +02:00
|
|
|
get_tags_commands/0,
|
2016-03-31 13:53:31 +02:00
|
|
|
get_tags_commands/1,
|
2016-10-05 13:21:11 +02:00
|
|
|
get_exposed_commands/0,
|
2008-10-12 13:53:25 +02:00
|
|
|
register_commands/1,
|
2016-10-05 13:21:11 +02:00
|
|
|
unregister_commands/1,
|
|
|
|
expose_commands/1,
|
|
|
|
opt_type/1,
|
|
|
|
get_commands_spec/0,
|
|
|
|
get_commands_definition/0,
|
|
|
|
get_commands_definition/1,
|
|
|
|
execute_command2/3,
|
|
|
|
execute_command2/4]).
|
2017-02-26 13:10:59 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2008-10-12 13:53:25 +02:00
|
|
|
|
|
|
|
-include("ejabberd_commands.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-03-31 13:53:31 +02:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2015-10-19 19:16:04 +02:00
|
|
|
-define(POLICY_ACCESS, '$policy').
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
2016-01-26 10:00:11 +01:00
|
|
|
get_commands_spec() ->
|
|
|
|
[
|
2015-12-28 12:19:49 +01:00
|
|
|
#ejabberd_commands{name = gen_html_doc_for_commands, tags = [documentation],
|
|
|
|
desc = "Generates html documentation for ejabberd_commands",
|
|
|
|
module = ejabberd_commands_doc, function = generate_html_output,
|
|
|
|
args = [{file, binary}, {regexp, binary}, {examples, binary}],
|
|
|
|
result = {res, rescode},
|
|
|
|
args_desc = ["Path to file where generated "
|
|
|
|
"documentation should be stored",
|
|
|
|
"Regexp matching names of commands or modules "
|
|
|
|
"that will be included inside generated document",
|
2018-01-27 17:35:38 +01:00
|
|
|
"Comma separated list of languages (chosen from java, perl, xmlrpc, json)"
|
2015-12-28 12:19:49 +01:00
|
|
|
"that will have example invocation include in markdown document"],
|
2018-01-27 17:35:38 +01:00
|
|
|
result_desc = "0 if command failed, 1 when succeeded",
|
2015-12-28 12:19:49 +01:00
|
|
|
args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
|
|
|
|
result_example = ok},
|
|
|
|
#ejabberd_commands{name = gen_markdown_doc_for_commands, tags = [documentation],
|
|
|
|
desc = "Generates markdown documentation for ejabberd_commands",
|
|
|
|
module = ejabberd_commands_doc, function = generate_md_output,
|
|
|
|
args = [{file, binary}, {regexp, binary}, {examples, binary}],
|
|
|
|
result = {res, rescode},
|
|
|
|
args_desc = ["Path to file where generated "
|
|
|
|
"documentation should be stored",
|
|
|
|
"Regexp matching names of commands or modules "
|
|
|
|
"that will be included inside generated document",
|
2018-01-27 17:35:38 +01:00
|
|
|
"Comma separated list of languages (chosen from java, perl, xmlrpc, json)"
|
2015-12-28 12:19:49 +01:00
|
|
|
"that will have example invocation include in markdown document"],
|
2018-01-27 17:35:38 +01:00
|
|
|
result_desc = "0 if command failed, 1 when succeeded",
|
2015-12-28 12:19:49 +01:00
|
|
|
args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
|
2016-01-26 10:00:11 +01:00
|
|
|
result_example = ok}].
|
2017-02-26 13:10:59 +01:00
|
|
|
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2016-11-19 11:05:13 +01:00
|
|
|
try mnesia:transform_table(ejabberd_commands, ignore,
|
|
|
|
record_info(fields, ejabberd_commands))
|
|
|
|
catch exit:{aborted, {no_exists, _}} -> ok
|
|
|
|
end,
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, ejabberd_commands,
|
2016-07-26 12:15:03 +02:00
|
|
|
[{ram_copies, [node()]},
|
2016-03-31 13:53:31 +02:00
|
|
|
{local_content, true},
|
2016-07-26 12:15:03 +02:00
|
|
|
{attributes, record_info(fields, ejabberd_commands)},
|
|
|
|
{type, bag}]),
|
2016-10-05 13:21:11 +02:00
|
|
|
register_commands(get_commands_spec()),
|
2017-02-26 13:10:59 +01:00
|
|
|
ejabberd_access_permissions:register_permission_addon(?MODULE, fun permission_addon/0),
|
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec register_commands([ejabberd_commands()]) -> ok.
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
%% @doc Register ejabberd commands.
|
2016-03-31 13:53:31 +02:00
|
|
|
%% If a command is already registered, a warning is printed and the
|
|
|
|
%% old command is preserved.
|
2016-07-26 12:15:03 +02:00
|
|
|
%% A registered command is not directly available to be called through
|
|
|
|
%% ejabberd ReST API. It need to be exposed to be available through API.
|
2008-10-12 13:53:25 +02:00
|
|
|
register_commands(Commands) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Command) ->
|
2016-07-26 12:15:03 +02:00
|
|
|
%% XXX check if command exists
|
|
|
|
mnesia:dirty_write(Command)
|
|
|
|
%% ?DEBUG("This command is already defined:~n~p", [Command])
|
2008-10-12 13:53:25 +02:00
|
|
|
end,
|
2016-10-05 13:21:11 +02:00
|
|
|
Commands),
|
|
|
|
ejabberd_access_permissions:invalidate(),
|
|
|
|
ok.
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec unregister_commands([ejabberd_commands()]) -> ok.
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
%% @doc Unregister ejabberd commands.
|
|
|
|
unregister_commands(Commands) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Command) ->
|
2016-03-31 13:53:31 +02:00
|
|
|
mnesia:dirty_delete_object(Command)
|
2008-10-12 13:53:25 +02:00
|
|
|
end,
|
2016-10-05 13:21:11 +02:00
|
|
|
Commands),
|
|
|
|
ejabberd_access_permissions:invalidate(),
|
|
|
|
ok.
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2016-07-26 12:15:03 +02:00
|
|
|
%% @doc Expose command through ejabberd ReST API.
|
|
|
|
%% Pass a list of command names or policy to expose.
|
|
|
|
-spec expose_commands([ejabberd_commands()|atom()|open|user|admin|restricted]) -> ok | {error, atom()}.
|
|
|
|
|
|
|
|
expose_commands(Commands) ->
|
|
|
|
Names = lists:map(fun(#ejabberd_commands{name = Name}) ->
|
|
|
|
Name;
|
|
|
|
(Name) when is_atom(Name) ->
|
|
|
|
Name
|
|
|
|
end,
|
|
|
|
Commands),
|
|
|
|
|
2017-02-16 18:47:47 +01:00
|
|
|
case ejabberd_config:add_option(commands, [{add_commands, Names}]) of
|
2017-07-06 17:18:44 +02:00
|
|
|
ok ->
|
|
|
|
ok;
|
2016-07-26 12:15:03 +02:00
|
|
|
{aborted, Reason} ->
|
|
|
|
{error, Reason};
|
|
|
|
{atomic, Result} ->
|
|
|
|
Result
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec list_commands() -> [{atom(), [aterm()], string()}].
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
%% @doc Get a list of all the available commands, arguments and description.
|
|
|
|
list_commands() ->
|
2016-03-31 13:53:31 +02:00
|
|
|
list_commands(?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
-spec list_commands(integer()) -> [{atom(), [aterm()], string()}].
|
|
|
|
|
|
|
|
%% @doc Get a list of all the available commands, arguments and
|
|
|
|
%% description in a given API verion.
|
|
|
|
list_commands(Version) ->
|
|
|
|
Commands = get_commands_definition(Version),
|
|
|
|
[{Name, Args, Desc} || #ejabberd_commands{name = Name,
|
2016-07-25 11:43:49 +02:00
|
|
|
args = Args,
|
|
|
|
desc = Desc} <- Commands].
|
2016-03-31 13:53:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
-spec list_commands_policy(integer()) ->
|
|
|
|
[{atom(), [aterm()], string(), atom()}].
|
|
|
|
|
|
|
|
%% @doc Get a list of all the available commands, arguments,
|
|
|
|
%% description, and policy in a given API version.
|
|
|
|
list_commands_policy(Version) ->
|
|
|
|
Commands = get_commands_definition(Version),
|
|
|
|
[{Name, Args, Desc, Policy} ||
|
2016-07-25 11:43:49 +02:00
|
|
|
#ejabberd_commands{name = Name,
|
|
|
|
args = Args,
|
|
|
|
desc = Desc,
|
|
|
|
policy = Policy} <- Commands].
|
2016-03-31 13:53:31 +02:00
|
|
|
|
|
|
|
-spec get_command_format(atom()) -> {[aterm()], rterm()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
%% @doc Get the format of arguments and result of a command.
|
|
|
|
get_command_format(Name) ->
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_format(Name, noauth, ?DEFAULT_VERSION).
|
|
|
|
get_command_format(Name, Version) when is_integer(Version) ->
|
|
|
|
get_command_format(Name, noauth, Version);
|
|
|
|
get_command_format(Name, Auth) ->
|
|
|
|
get_command_format(Name, Auth, ?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
-spec get_command_format(atom(),
|
|
|
|
{binary(), binary(), binary(), boolean()} |
|
|
|
|
noauth | admin,
|
|
|
|
integer()) ->
|
|
|
|
{[aterm()], rterm()}.
|
|
|
|
|
|
|
|
get_command_format(Name, Auth, Version) ->
|
2016-05-25 13:01:07 +02:00
|
|
|
Admin = is_admin(Name, Auth, #{}),
|
2016-03-31 13:53:31 +02:00
|
|
|
#ejabberd_commands{args = Args,
|
|
|
|
result = Result,
|
2016-07-25 11:43:49 +02:00
|
|
|
policy = Policy} =
|
|
|
|
get_command_definition(Name, Version),
|
2016-03-31 13:53:31 +02:00
|
|
|
case Policy of
|
2016-07-25 11:43:49 +02:00
|
|
|
user when Admin;
|
|
|
|
Auth == noauth ->
|
|
|
|
{[{user, binary}, {server, binary} | Args], Result};
|
|
|
|
_ ->
|
|
|
|
{Args, Result}
|
2008-10-12 13:53:25 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-19 04:27:49 +02:00
|
|
|
%% The oauth scopes for a command are the command name itself,
|
|
|
|
%% also might include either 'ejabberd:user' or 'ejabberd:admin'
|
|
|
|
cmd_scope(#ejabberd_commands{policy = Policy, name = Name}) ->
|
|
|
|
[erlang:atom_to_binary(Name,utf8)] ++ [<<"ejabberd:user">> || Policy == user] ++ [<<"ejabberd:admin">> || Policy == admin].
|
|
|
|
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
-spec get_command_definition(atom()) -> ejabberd_commands().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
%% @doc Get the definition record of a command.
|
|
|
|
get_command_definition(Name) ->
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_definition(Name, ?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
-spec get_command_definition(atom(), integer()) -> ejabberd_commands().
|
|
|
|
|
|
|
|
%% @doc Get the definition record of a command in a given API version.
|
|
|
|
get_command_definition(Name, Version) ->
|
|
|
|
case lists:reverse(
|
2016-07-25 11:43:49 +02:00
|
|
|
lists:sort(
|
|
|
|
mnesia:dirty_select(
|
|
|
|
ejabberd_commands,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#ejabberd_commands{name = N, version = V} = C)
|
|
|
|
when N == Name, V =< Version ->
|
|
|
|
{V, C}
|
|
|
|
end)))) of
|
|
|
|
[{_, Command} | _ ] -> Command;
|
2016-07-31 22:48:24 +02:00
|
|
|
_E -> throw({error, unknown_command})
|
2008-10-12 13:53:25 +02:00
|
|
|
end.
|
|
|
|
|
2016-10-05 13:21:11 +02:00
|
|
|
get_commands_definition() ->
|
|
|
|
get_commands_definition(?DEFAULT_VERSION).
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
-spec get_commands_definition(integer()) -> [ejabberd_commands()].
|
|
|
|
|
|
|
|
% @doc Returns all commands for a given API version
|
|
|
|
get_commands_definition(Version) ->
|
|
|
|
L = lists:reverse(
|
2016-07-25 11:43:49 +02:00
|
|
|
lists:sort(
|
|
|
|
mnesia:dirty_select(
|
|
|
|
ejabberd_commands,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#ejabberd_commands{name = Name, version = V} = C)
|
|
|
|
when V =< Version ->
|
|
|
|
{Name, V, C}
|
|
|
|
end)))),
|
2016-03-31 13:53:31 +02:00
|
|
|
F = fun({_Name, _V, Command}, []) ->
|
2016-07-25 11:43:49 +02:00
|
|
|
[Command];
|
|
|
|
({Name, _V, _Command}, [#ejabberd_commands{name=Name}|_T] = Acc) ->
|
|
|
|
Acc;
|
|
|
|
({_Name, _V, Command}, Acc) -> [Command | Acc]
|
|
|
|
end,
|
2016-03-31 13:53:31 +02:00
|
|
|
lists:foldl(F, [], L).
|
|
|
|
|
2016-10-05 13:21:11 +02:00
|
|
|
execute_command2(Name, Arguments, CallerInfo) ->
|
2016-10-14 13:55:50 +02:00
|
|
|
execute_command2(Name, Arguments, CallerInfo, ?DEFAULT_VERSION).
|
2016-10-05 13:21:11 +02:00
|
|
|
|
|
|
|
execute_command2(Name, Arguments, CallerInfo, Version) ->
|
|
|
|
Command = get_command_definition(Name, Version),
|
|
|
|
case ejabberd_access_permissions:can_access(Name, CallerInfo) of
|
|
|
|
allow ->
|
|
|
|
do_execute_command(Command, Arguments);
|
|
|
|
_ ->
|
|
|
|
throw({error, access_rules_unauthorized})
|
|
|
|
end.
|
|
|
|
|
2016-07-23 17:57:44 +02:00
|
|
|
|
|
|
|
do_execute_command(Command, Arguments) ->
|
2008-10-12 13:53:25 +02:00
|
|
|
Module = Command#ejabberd_commands.module,
|
|
|
|
Function = Command#ejabberd_commands.function,
|
2009-10-08 19:22:48 +02:00
|
|
|
?DEBUG("Executing command ~p:~p with Args=~p", [Module, Function, Arguments]),
|
2018-12-11 11:07:07 +01:00
|
|
|
ejabberd_hooks:run(api_call, [Module, Function, Arguments]),
|
2016-03-31 13:53:31 +02:00
|
|
|
apply(Module, Function, Arguments).
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_tags_commands() -> [{string(), [string()]}].
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
%% @spec () -> [{Tag::string(), [CommandName::string()]}]
|
|
|
|
%% @doc Get all the tags and associated commands.
|
|
|
|
get_tags_commands() ->
|
2016-03-31 13:53:31 +02:00
|
|
|
get_tags_commands(?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
-spec get_tags_commands(integer()) -> [{string(), [string()]}].
|
|
|
|
|
|
|
|
%% @spec (integer) -> [{Tag::string(), [CommandName::string()]}]
|
|
|
|
%% @doc Get all the tags and associated commands in a given API version
|
|
|
|
get_tags_commands(Version) ->
|
|
|
|
CommandTags = [{Name, Tags} ||
|
|
|
|
#ejabberd_commands{name = Name, tags = Tags}
|
|
|
|
<- get_commands_definition(Version)],
|
2008-10-12 13:53:25 +02:00
|
|
|
Dict = lists:foldl(
|
2016-03-31 13:53:31 +02:00
|
|
|
fun({CommandNameAtom, CTags}, D) ->
|
2008-10-12 13:53:25 +02:00
|
|
|
CommandName = atom_to_list(CommandNameAtom),
|
|
|
|
case CTags of
|
|
|
|
[] ->
|
|
|
|
orddict:append("untagged", CommandName, D);
|
|
|
|
_ ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(TagAtom, DD) ->
|
|
|
|
Tag = atom_to_list(TagAtom),
|
|
|
|
orddict:append(Tag, CommandName, DD)
|
|
|
|
end,
|
|
|
|
D,
|
|
|
|
CTags)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
orddict:new(),
|
|
|
|
CommandTags),
|
|
|
|
orddict:to_list(Dict).
|
2009-04-17 15:43:15 +02:00
|
|
|
|
|
|
|
%% -----------------------------
|
|
|
|
%% Access verification
|
|
|
|
%% -----------------------------
|
|
|
|
|
2015-09-25 14:53:25 +02:00
|
|
|
-spec check_auth(ejabberd_commands(), noauth) -> noauth_provided;
|
|
|
|
(ejabberd_commands(),
|
|
|
|
{binary(), binary(), binary(), boolean()}) ->
|
|
|
|
{ok, binary(), binary()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-09-25 14:53:25 +02:00
|
|
|
check_auth(_Command, noauth) ->
|
2010-01-09 01:39:33 +01:00
|
|
|
no_auth_provided;
|
2015-09-25 14:53:25 +02:00
|
|
|
check_auth(Command, {User, Server, {oauth, Token}, _}) ->
|
2016-07-19 04:27:49 +02:00
|
|
|
ScopeList = cmd_scope(Command),
|
|
|
|
case ejabberd_oauth:check_token(User, Server, ScopeList, Token) of
|
2015-09-25 14:53:25 +02:00
|
|
|
true ->
|
|
|
|
{ok, User, Server};
|
2016-07-30 18:51:54 +02:00
|
|
|
_ ->
|
2015-09-25 14:53:25 +02:00
|
|
|
throw({error, invalid_account_data})
|
|
|
|
end;
|
|
|
|
check_auth(_Command, {User, Server, Password, _}) when is_binary(Password) ->
|
2009-04-17 15:43:15 +02:00
|
|
|
%% Check the account exists and password is valid
|
2015-04-09 03:21:09 +02:00
|
|
|
case ejabberd_auth:check_password(User, <<"">>, Server, Password) of
|
2015-10-07 00:06:58 +02:00
|
|
|
true -> {ok, User, Server};
|
|
|
|
_ -> throw({error, invalid_account_data})
|
2009-04-17 15:43:15 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-26 12:15:03 +02:00
|
|
|
get_exposed_commands() ->
|
|
|
|
get_exposed_commands(?DEFAULT_VERSION).
|
|
|
|
get_exposed_commands(Version) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
Opts0 = ejabberd_config:get_option(commands, []),
|
2016-04-21 11:15:53 +02:00
|
|
|
Opts = lists:map(fun(V) when is_tuple(V) -> [V]; (V) -> V end, Opts0),
|
2016-03-31 13:53:31 +02:00
|
|
|
CommandsList = list_commands_policy(Version),
|
2015-09-25 14:53:25 +02:00
|
|
|
OpenCmds = [N || {N, _, _, open} <- CommandsList],
|
|
|
|
RestrictedCmds = [N || {N, _, _, restricted} <- CommandsList],
|
|
|
|
AdminCmds = [N || {N, _, _, admin} <- CommandsList],
|
|
|
|
UserCmds = [N || {N, _, _, user} <- CommandsList],
|
|
|
|
Cmds =
|
|
|
|
lists:foldl(
|
2016-03-29 13:19:16 +02:00
|
|
|
fun([{add_commands, L}], Acc) ->
|
2016-07-25 11:43:49 +02:00
|
|
|
Cmds = expand_commands(L, OpenCmds, UserCmds, AdminCmds, RestrictedCmds),
|
2015-09-25 14:53:25 +02:00
|
|
|
lists:usort(Cmds ++ Acc);
|
2016-03-29 13:19:16 +02:00
|
|
|
([{remove_commands, L}], Acc) ->
|
2016-07-25 11:43:49 +02:00
|
|
|
Cmds = expand_commands(L, OpenCmds, UserCmds, AdminCmds, RestrictedCmds),
|
2015-09-25 14:53:25 +02:00
|
|
|
Acc -- Cmds;
|
|
|
|
(_, Acc) -> Acc
|
2016-07-25 11:43:49 +02:00
|
|
|
end, [], Opts),
|
2015-09-25 14:53:25 +02:00
|
|
|
Cmds.
|
|
|
|
|
2016-07-25 18:28:05 +02:00
|
|
|
%% This is used to allow mixing command policy (like open, user, admin, restricted), with command entry
|
2017-02-06 21:40:37 +01:00
|
|
|
expand_commands(L, OpenCmds, UserCmds, AdminCmds, RestrictedCmds) when is_atom(L) ->
|
|
|
|
expand_commands([L], OpenCmds, UserCmds, AdminCmds, RestrictedCmds);
|
2016-07-25 11:43:49 +02:00
|
|
|
expand_commands(L, OpenCmds, UserCmds, AdminCmds, RestrictedCmds) when is_list(L) ->
|
2016-07-25 18:28:05 +02:00
|
|
|
lists:foldl(fun(open, Acc) -> OpenCmds ++ Acc;
|
|
|
|
(user, Acc) -> UserCmds ++ Acc;
|
|
|
|
(admin, Acc) -> AdminCmds ++ Acc;
|
|
|
|
(restricted, Acc) -> RestrictedCmds ++ Acc;
|
|
|
|
(Command, Acc) when is_atom(Command) ->
|
2016-07-26 11:57:38 +02:00
|
|
|
[Command|Acc]
|
2016-07-25 18:28:05 +02:00
|
|
|
end, [], L).
|
2016-07-25 11:43:49 +02:00
|
|
|
|
2016-05-25 13:01:07 +02:00
|
|
|
is_admin(_Name, admin, _Extra) ->
|
2015-09-25 14:53:25 +02:00
|
|
|
true;
|
2016-05-25 13:01:07 +02:00
|
|
|
is_admin(_Name, {_User, _Server, _, false}, _Extra) ->
|
2015-09-25 14:53:25 +02:00
|
|
|
false;
|
2016-10-05 13:21:11 +02:00
|
|
|
is_admin(_Name, Map, _extra) when is_map(Map) ->
|
|
|
|
true;
|
2016-05-25 13:01:07 +02:00
|
|
|
is_admin(Name, Auth, Extra) ->
|
|
|
|
{ACLInfo, Server} = case Auth of
|
|
|
|
{U, S, _, _} ->
|
2017-02-25 08:01:01 +01:00
|
|
|
{Extra#{usr=>jid:split(jid:make(U, S))}, S};
|
2016-05-25 13:01:07 +02:00
|
|
|
_ ->
|
|
|
|
{Extra, global}
|
|
|
|
end,
|
2017-04-29 10:39:40 +02:00
|
|
|
AdminAccess = ejabberd_config:get_option(commands_admin_access, none),
|
2016-05-25 13:01:07 +02:00
|
|
|
case acl:access_matches(AdminAccess, ACLInfo, Server) of
|
2015-09-25 14:53:25 +02:00
|
|
|
allow ->
|
|
|
|
case catch check_auth(get_command_definition(Name), Auth) of
|
|
|
|
{ok, _, _} -> true;
|
2016-05-25 13:01:07 +02:00
|
|
|
no_auth_provided -> true;
|
2015-09-25 14:53:25 +02:00
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
deny -> false
|
|
|
|
end.
|
|
|
|
|
2016-10-05 13:21:11 +02:00
|
|
|
permission_addon() ->
|
|
|
|
[{<<"'commands' option compatibility shim">>,
|
|
|
|
{[],
|
2017-04-29 10:39:40 +02:00
|
|
|
[{access, ejabberd_config:get_option(commands_admin_access, none)}],
|
2016-10-05 13:21:11 +02:00
|
|
|
{get_exposed_commands(), []}}}].
|
|
|
|
|
2018-09-09 08:59:08 +02:00
|
|
|
-spec opt_type(atom()) -> fun((any()) -> any()) | [atom()].
|
2016-06-21 12:28:53 +02:00
|
|
|
opt_type(commands_admin_access) -> fun acl:access_rules_validator/1;
|
2015-09-25 14:53:25 +02:00
|
|
|
opt_type(commands) ->
|
|
|
|
fun(V) when is_list(V) -> V end;
|
|
|
|
opt_type(_) -> [commands, commands_admin_access].
|