summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMickaël Rémond <mickael.remond@process-one.net>2016-03-31 11:37:14 +0200
committerMickaël Rémond <mickael.remond@process-one.net>2016-03-31 11:37:14 +0200
commit7c2998a55d78d1345b706192c55a9d03c649c38a (patch)
treeb092154b65f17eedc5a90e2e7576ca1d44c09cca /src
parentmix version updated for 16.03 release (diff)
parentFix Dialyzer inconsistency (diff)
Merge pull request #1044 from processone/http-api
Add ability to call open ejabberd commands through ReST API
Diffstat (limited to 'src')
-rw-r--r--src/ejabberd_commands.erl12
-rw-r--r--src/ejabberd_http.erl1
-rw-r--r--src/mod_http_api.erl24
3 files changed, 30 insertions, 7 deletions
diff --git a/src/ejabberd_commands.erl b/src/ejabberd_commands.erl
index 21872aa3..57285d6c 100644
--- a/src/ejabberd_commands.erl
+++ b/src/ejabberd_commands.erl
@@ -212,6 +212,7 @@
list_commands/0,
get_command_format/1,
get_command_format/2,
+ get_command_policy/1,
get_command_definition/1,
get_tags_commands/0,
get_commands/0,
@@ -338,6 +339,17 @@ get_command_format(Name, Auth) ->
{Args, Result}
end.
+-spec get_command_policy(atom()) -> {ok, open|user|admin|restricted} | {error, command_not_found}.
+
+%% @doc return command policy.
+get_command_policy(Name) ->
+ case get_command_definition(Name) of
+ #ejabberd_commands{policy = Policy} ->
+ {ok, Policy};
+ command_not_found ->
+ {error, command_not_found}
+ end.
+
-spec get_command_definition(atom()) -> ejabberd_commands() | command_not_found.
%% @doc Get the definition record of a command.
diff --git a/src/ejabberd_http.erl b/src/ejabberd_http.erl
index d8d1ddd4..6b53f46c 100644
--- a/src/ejabberd_http.erl
+++ b/src/ejabberd_http.erl
@@ -753,6 +753,7 @@ code_to_phrase(503) -> <<"Service Unavailable">>;
code_to_phrase(504) -> <<"Gateway Timeout">>;
code_to_phrase(505) -> <<"HTTP Version Not Supported">>.
+-spec parse_auth(binary()) -> {binary(), binary()} | {oauth, binary(), []} | undefined.
parse_auth(<<"Basic ", Auth64/binary>>) ->
Auth = jlib:decode_base64(Auth64),
%% Auth should be a string with the format: user@server:password
diff --git a/src/mod_http_api.erl b/src/mod_http_api.erl
index 15fe3636..c2b7d110 100644
--- a/src/mod_http_api.erl
+++ b/src/mod_http_api.erl
@@ -116,7 +116,6 @@ start(_Host, _Opts) ->
stop(_Host) ->
ok.
-
%% ----------
%% basic auth
%% ----------
@@ -124,12 +123,13 @@ stop(_Host) ->
check_permissions(Request, Command) ->
case catch binary_to_existing_atom(Command, utf8) of
Call when is_atom(Call) ->
- check_permissions2(Request, Call);
+ {ok, CommandPolicy} = ejabberd_commands:get_command_policy(Call),
+ check_permissions2(Request, Call, CommandPolicy);
_ ->
unauthorized_response()
end.
-check_permissions2(#request{auth = HTTPAuth, headers = Headers}, Call)
+check_permissions2(#request{auth = HTTPAuth, headers = Headers}, Call, _)
when HTTPAuth /= undefined ->
Admin =
case lists:keysearch(<<"X-Admin">>, 1, Headers) of
@@ -162,7 +162,9 @@ check_permissions2(#request{auth = HTTPAuth, headers = Headers}, Call)
{ok, A} -> {allowed, Call, A};
_ -> unauthorized_response()
end;
-check_permissions2(#request{ip={IP, _Port}}, Call) ->
+check_permissions2(_Request, Call, open) ->
+ {allowed, Call, noauth};
+check_permissions2(#request{ip={IP, _Port}}, Call, _Policy) ->
Access = gen_mod:get_module_opt(global, ?MODULE, admin_ip_access,
mod_opt_type(admin_ip_access),
none),
@@ -181,7 +183,9 @@ check_permissions2(#request{ip={IP, _Port}}, Call) ->
end;
_ ->
unauthorized_response()
- end.
+ end;
+check_permissions2(_Request, _Call, _Policy) ->
+ unauthorized_response().
oauth_check_token(Scope, Token) when is_atom(Scope) ->
oauth_check_token(atom_to_binary(Scope, utf8), Token);
@@ -207,7 +211,8 @@ process([Call], #request{method = 'POST', data = Data, ip = IP} = Req) ->
{allowed, Cmd, Auth} ->
{Code, Result} = handle(Cmd, Auth, Args),
json_response(Code, jiffy:encode(Result));
- ErrorResponse -> %% Should we reply 403 ?
+ %% Warning: check_permission direcly formats 401 reply if not authorized
+ ErrorResponse ->
ErrorResponse
end
catch _:Error ->
@@ -225,6 +230,7 @@ process([Call], #request{method = 'GET', q = Data, ip = IP} = Req) ->
{allowed, Cmd, Auth} ->
{Code, Result} = handle(Cmd, Auth, Args),
json_response(Code, jiffy:encode(Result));
+ %% Warning: check_permission direcly formats 401 reply if not authorized
ErrorResponse ->
ErrorResponse
end
@@ -273,6 +279,7 @@ handle2(Call, Auth, Args) when is_atom(Call), is_list(Args) ->
0 -> {200, <<"OK">>};
1 -> {500, <<"500 Internal server error">>};
400 -> {400, <<"400 Bad Request">>};
+ 401 -> {401, <<"401 Unauthorized">>};
404 -> {404, <<"404 Not found">>};
Res -> format_command_result(Call, Auth, Res)
end.
@@ -360,6 +367,7 @@ ejabberd_command(Auth, Cmd, Args, Default) ->
end,
case catch ejabberd_commands:execute_command(Access, Auth, Cmd, Args) of
{'EXIT', _} -> Default;
+ {error, account_unprivileged} -> 401;
{error, _} -> Default;
Result -> Result
end.
@@ -434,7 +442,9 @@ json_response(Code, Body) when is_integer(Code) ->
log(Call, Args, {Addr, Port}) ->
AddrS = jlib:ip_to_list({Addr, Port}),
- ?INFO_MSG("API call ~s ~p from ~s:~p", [Call, Args, AddrS, Port]).
+ ?INFO_MSG("API call ~s ~p from ~s:~p", [Call, Args, AddrS, Port]);
+log(Call, Args, IP) ->
+ ?INFO_MSG("API call ~s ~p (~p)", [Call, Args, IP]).
mod_opt_type(admin_ip_access) ->
fun(Access) when is_atom(Access) -> Access end;