2016-10-05 13:21:11 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_access_permissions.erl
|
|
|
|
%%% Author : Paweł Chmielowski <pawel@process-one.net>
|
|
|
|
%%% Purpose : Administrative functions and commands
|
|
|
|
%%% Created : 7 Sep 2016 by Paweł Chmielowski <pawel@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2022-02-10 17:21:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2022 ProcessOne
|
2016-10-05 13:21:11 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(ejabberd_access_permissions).
|
|
|
|
-author("pawel@process-one.net").
|
|
|
|
|
|
|
|
-include("ejabberd_commands.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/0,
|
|
|
|
can_access/2,
|
|
|
|
invalidate/0,
|
2019-06-14 11:33:26 +02:00
|
|
|
validator/0,
|
|
|
|
show_current_definitions/0]).
|
2016-10-05 13:21:11 +02:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2,
|
|
|
|
code_change/3]).
|
|
|
|
|
|
|
|
-define(SERVER, ?MODULE).
|
2021-06-24 15:15:13 +02:00
|
|
|
-define(CACHE_TAB, access_permissions_cache).
|
2016-10-05 13:21:11 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-record(state,
|
|
|
|
{definitions = none :: none | [definition()]}).
|
|
|
|
|
|
|
|
-type state() :: #state{}.
|
|
|
|
-type rule() :: {access, acl:access()} |
|
|
|
|
{acl, all | none | acl:acl_rule()}.
|
|
|
|
-type what() :: all | none | [atom() | {tag, atom()}].
|
|
|
|
-type who() :: rule() | {oauth, {[binary()], [rule()]}}.
|
|
|
|
-type from() :: atom().
|
|
|
|
-type permission() :: {binary(), {[from()], [who()], {what(), what()}}}.
|
|
|
|
-type definition() :: {binary(), {[from()], [who()], [atom()] | all}}.
|
|
|
|
-type caller_info() :: #{caller_module => module(),
|
|
|
|
caller_host => global | binary(),
|
|
|
|
tag => binary() | none,
|
|
|
|
extra_permissions => [definition()],
|
|
|
|
atom() => term()}.
|
|
|
|
|
|
|
|
-export_type([permission/0]).
|
2016-10-05 13:21:11 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec can_access(atom(), caller_info()) -> allow | deny.
|
2016-10-05 13:21:11 +02:00
|
|
|
can_access(Cmd, CallerInfo) ->
|
2021-06-24 15:15:13 +02:00
|
|
|
Defs0 = show_current_definitions(),
|
|
|
|
CallerModule = maps:get(caller_module, CallerInfo, none),
|
|
|
|
Host = maps:get(caller_host, CallerInfo, global),
|
|
|
|
Tag = maps:get(tag, CallerInfo, none),
|
|
|
|
Defs = maps:get(extra_permissions, CallerInfo, []) ++ Defs0,
|
|
|
|
Res = lists:foldl(
|
|
|
|
fun({Name, _} = Def, none) ->
|
|
|
|
case matches_definition(Def, Cmd, CallerModule, Tag, Host, CallerInfo) of
|
|
|
|
true ->
|
|
|
|
?DEBUG("Command '~p' execution allowed by rule "
|
|
|
|
"'~ts' (CallerInfo=~p)", [Cmd, Name, CallerInfo]),
|
|
|
|
allow;
|
|
|
|
_ ->
|
|
|
|
none
|
|
|
|
end;
|
|
|
|
(_, Val) ->
|
|
|
|
Val
|
|
|
|
end, none, Defs),
|
|
|
|
case Res of
|
|
|
|
allow -> allow;
|
|
|
|
_ ->
|
|
|
|
?DEBUG("Command '~p' execution denied "
|
|
|
|
"(CallerInfo=~p)", [Cmd, CallerInfo]),
|
|
|
|
deny
|
|
|
|
end.
|
2016-10-05 13:21:11 +02:00
|
|
|
|
|
|
|
-spec invalidate() -> ok.
|
|
|
|
invalidate() ->
|
2021-06-24 15:15:13 +02:00
|
|
|
gen_server:cast(?MODULE, invalidate),
|
|
|
|
ets_cache:delete(?CACHE_TAB, definitions).
|
2016-10-05 13:21:11 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec show_current_definitions() -> [definition()].
|
2016-10-05 13:21:11 +02:00
|
|
|
show_current_definitions() ->
|
2021-06-24 15:15:13 +02:00
|
|
|
ets_cache:lookup(?CACHE_TAB, definitions,
|
|
|
|
fun() ->
|
|
|
|
{cache, gen_server:call(?MODULE, show_current_definitions)}
|
|
|
|
end).
|
2016-10-05 13:21:11 +02:00
|
|
|
start_link() ->
|
2021-06-24 15:15:13 +02:00
|
|
|
ets_cache:new(?CACHE_TAB, [{max_size, 2}]),
|
2016-10-05 13:21:11 +02:00
|
|
|
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec init([]) -> {ok, state()}.
|
2016-10-05 13:21:11 +02:00
|
|
|
init([]) ->
|
2017-02-27 07:55:54 +01:00
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, invalidate, 90),
|
2021-06-24 15:15:13 +02:00
|
|
|
ets_cache:new(access_permissions),
|
2016-10-05 13:21:11 +02:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2021-06-24 15:15:13 +02:00
|
|
|
-spec handle_call(show_current_definitions | term(),
|
2019-06-14 11:33:26 +02:00
|
|
|
term(), state()) -> {reply, term(), state()}.
|
2016-10-05 13:21:11 +02:00
|
|
|
handle_call(show_current_definitions, _From, State) ->
|
|
|
|
{State2, Defs} = get_definitions(State),
|
|
|
|
{reply, Defs, State2};
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2016-10-05 13:21:11 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec handle_cast(invalidate | term(), state()) -> {noreply, state()}.
|
2016-10-05 13:21:11 +02:00
|
|
|
handle_cast(invalidate, State) ->
|
|
|
|
{noreply, State#state{definitions = none}};
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2016-10-05 13:21:11 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2016-10-05 13:21:11 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
2017-02-27 07:55:54 +01:00
|
|
|
ejabberd_hooks:delete(config_reloaded, ?MODULE, invalidate, 90).
|
2016-10-05 13:21:11 +02:00
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_definitions(state()) -> {state(), [definition()]}.
|
2017-02-03 15:22:35 +01:00
|
|
|
get_definitions(#state{definitions = Defs} = State) when Defs /= none ->
|
|
|
|
{State, Defs};
|
2019-06-14 11:33:26 +02:00
|
|
|
get_definitions(#state{definitions = none} = State) ->
|
|
|
|
ApiPerms = ejabberd_option:api_permissions(),
|
2017-02-03 15:22:35 +01:00
|
|
|
AllCommands = ejabberd_commands:get_commands_definition(),
|
|
|
|
NDefs0 = lists:map(
|
|
|
|
fun({Name, {From, Who, {Add, Del}}}) ->
|
|
|
|
Cmds = filter_commands_with_permissions(AllCommands, Add, Del),
|
|
|
|
{Name, {From, Who, Cmds}}
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ApiPerms),
|
2017-02-03 15:22:35 +01:00
|
|
|
NDefs = case lists:keyfind(<<"console commands">>, 1, NDefs0) of
|
|
|
|
false ->
|
|
|
|
[{<<"console commands">>,
|
|
|
|
{[ejabberd_ctl],
|
|
|
|
[{acl, all}],
|
|
|
|
filter_commands_with_permissions(AllCommands, all, none)}} | NDefs0];
|
|
|
|
_ ->
|
|
|
|
NDefs0
|
2016-10-05 13:21:11 +02:00
|
|
|
end,
|
|
|
|
{State#state{definitions = NDefs}, NDefs}.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec matches_definition(definition(), atom(), module(),
|
|
|
|
atom(), global | binary(), caller_info()) -> boolean().
|
2019-01-30 12:56:52 +01:00
|
|
|
matches_definition({_Name, {From, Who, What}}, Cmd, Module, Tag, Host, CallerInfo) ->
|
2016-10-14 13:55:50 +02:00
|
|
|
case What == all orelse lists:member(Cmd, What) of
|
2016-10-05 13:21:11 +02:00
|
|
|
true ->
|
2019-01-30 13:09:06 +01:00
|
|
|
{Tags, Modules} = lists:partition(fun({tag, _}) -> true; (_) -> false end, From),
|
|
|
|
case (Modules == [] orelse lists:member(Module, Modules)) andalso
|
|
|
|
(Tags == [] orelse lists:member({tag, Tag}, Tags)) of
|
2016-10-05 13:21:11 +02:00
|
|
|
true ->
|
2016-10-06 11:03:26 +02:00
|
|
|
Scope = maps:get(oauth_scope, CallerInfo, none),
|
2016-10-05 13:21:11 +02:00
|
|
|
lists:any(
|
2019-06-14 11:33:26 +02:00
|
|
|
fun({access, Access}) when Scope == none ->
|
|
|
|
acl:match_rule(Host, Access, CallerInfo) == allow;
|
|
|
|
({acl, Name} = Acl) when Scope == none, is_atom(Name) ->
|
|
|
|
acl:match_acl(Host, Acl, CallerInfo);
|
|
|
|
({acl, Acl}) when Scope == none ->
|
|
|
|
acl:match_acl(Host, Acl, CallerInfo);
|
|
|
|
({oauth, {Scopes, List}}) when Scope /= none ->
|
|
|
|
case ejabberd_oauth:scope_in_scope_list(Scope, Scopes) of
|
|
|
|
true ->
|
|
|
|
lists:any(
|
|
|
|
fun({access, Access}) ->
|
|
|
|
acl:match_rule(Host, Access, CallerInfo) == allow;
|
|
|
|
({acl, Name} = Acl) when is_atom(Name) ->
|
|
|
|
acl:match_acl(Host, Acl, CallerInfo);
|
|
|
|
({acl, Acl}) ->
|
|
|
|
acl:match_acl(Host, Acl, CallerInfo)
|
|
|
|
end, List);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
false
|
|
|
|
end, Who);
|
2016-10-05 13:21:11 +02:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec filter_commands_with_permissions([#ejabberd_commands{}], what(), what()) -> [atom()].
|
2016-10-05 13:21:11 +02:00
|
|
|
filter_commands_with_permissions(AllCommands, Add, Del) ->
|
|
|
|
CommandsAdd = filter_commands_with_patterns(AllCommands, Add, []),
|
|
|
|
CommandsDel = filter_commands_with_patterns(CommandsAdd, Del, []),
|
|
|
|
lists:map(fun(#ejabberd_commands{name = N}) -> N end,
|
|
|
|
CommandsAdd -- CommandsDel).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec filter_commands_with_patterns([#ejabberd_commands{}], what(),
|
|
|
|
[#ejabberd_commands{}]) -> [#ejabberd_commands{}].
|
2016-10-05 13:21:11 +02:00
|
|
|
filter_commands_with_patterns([], _Patterns, Acc) ->
|
|
|
|
Acc;
|
|
|
|
filter_commands_with_patterns([C | CRest], Patterns, Acc) ->
|
|
|
|
case command_matches_patterns(C, Patterns) of
|
|
|
|
true ->
|
|
|
|
filter_commands_with_patterns(CRest, Patterns, [C | Acc]);
|
|
|
|
_ ->
|
|
|
|
filter_commands_with_patterns(CRest, Patterns, Acc)
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec command_matches_patterns(#ejabberd_commands{}, what()) -> boolean().
|
2016-10-05 13:21:11 +02:00
|
|
|
command_matches_patterns(_, all) ->
|
|
|
|
true;
|
|
|
|
command_matches_patterns(_, none) ->
|
|
|
|
false;
|
|
|
|
command_matches_patterns(_, []) ->
|
|
|
|
false;
|
|
|
|
command_matches_patterns(#ejabberd_commands{tags = Tags} = C, [{tag, Tag} | Tail]) ->
|
|
|
|
case lists:member(Tag, Tags) of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
command_matches_patterns(C, Tail)
|
|
|
|
end;
|
|
|
|
command_matches_patterns(#ejabberd_commands{name = Name}, [Name | _Tail]) ->
|
|
|
|
true;
|
|
|
|
command_matches_patterns(C, [_ | Tail]) ->
|
|
|
|
command_matches_patterns(C, Tail).
|
|
|
|
|
|
|
|
%%%===================================================================
|
2019-06-14 11:33:26 +02:00
|
|
|
%%% Validators
|
2016-10-05 13:21:11 +02:00
|
|
|
%%%===================================================================
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec parse_what([binary()]) -> {what(), what()}.
|
|
|
|
parse_what(Defs) ->
|
|
|
|
{A, D} =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Def, {Add, Del}) ->
|
|
|
|
case parse_single_what(Def) of
|
|
|
|
{error, Err} ->
|
|
|
|
econf:fail({invalid_syntax, [Err, ": ", Def]});
|
|
|
|
all ->
|
|
|
|
{case Add of none -> none; _ -> all end, Del};
|
|
|
|
{neg, all} ->
|
|
|
|
{none, all};
|
|
|
|
{neg, Value} ->
|
|
|
|
{Add, case Del of L when is_list(L) -> [Value | L]; L2 -> L2 end};
|
|
|
|
Value ->
|
|
|
|
{case Add of L when is_list(L) -> [Value | L]; L2 -> L2 end, Del}
|
|
|
|
end
|
|
|
|
end, {[], []}, Defs),
|
2016-10-05 13:21:11 +02:00
|
|
|
case {A, D} of
|
|
|
|
{[], _} ->
|
|
|
|
{none, all};
|
|
|
|
{A2, []} ->
|
|
|
|
{A2, none};
|
|
|
|
V ->
|
|
|
|
V
|
2019-06-14 11:33:26 +02:00
|
|
|
end.
|
2016-10-05 13:21:11 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec parse_single_what(binary()) -> atom() | {neg, atom()} | {tag, atom()} | {error, string()}.
|
2016-10-05 13:21:11 +02:00
|
|
|
parse_single_what(<<"*">>) ->
|
|
|
|
all;
|
|
|
|
parse_single_what(<<"!*">>) ->
|
|
|
|
{neg, all};
|
|
|
|
parse_single_what(<<"!", Rest/binary>>) ->
|
|
|
|
case parse_single_what(Rest) of
|
|
|
|
{neg, _} ->
|
2019-06-14 11:33:26 +02:00
|
|
|
{error, "double negation"};
|
2016-10-05 13:21:11 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err;
|
|
|
|
V ->
|
|
|
|
{neg, V}
|
|
|
|
end;
|
|
|
|
parse_single_what(<<"[tag:", Rest/binary>>) ->
|
|
|
|
case binary:split(Rest, <<"]">>) of
|
|
|
|
[TagName, <<"">>] ->
|
|
|
|
case parse_single_what(TagName) of
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err;
|
|
|
|
V when is_atom(V) ->
|
|
|
|
{tag, V};
|
|
|
|
_ ->
|
2019-06-14 11:33:26 +02:00
|
|
|
{error, "invalid tag"}
|
2016-10-05 13:21:11 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2019-06-14 11:33:26 +02:00
|
|
|
{error, "invalid tag"}
|
2016-10-05 13:21:11 +02:00
|
|
|
end;
|
2019-06-14 11:33:26 +02:00
|
|
|
parse_single_what(B) ->
|
|
|
|
case re:run(B, "^[a-z0-9_\\-]*$") of
|
|
|
|
nomatch -> {error, "invalid command"};
|
|
|
|
_ -> binary_to_atom(B, latin1)
|
2016-10-05 13:21:11 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
validator(Map, Opts) ->
|
|
|
|
econf:and_then(
|
|
|
|
fun(L) when is_list(L) ->
|
|
|
|
lists:map(
|
|
|
|
fun({K, V}) -> {(econf:atom())(K), V};
|
|
|
|
(A) -> {acl, (econf:atom())(A)}
|
|
|
|
end, lists:flatten(L));
|
|
|
|
(A) ->
|
|
|
|
[{acl, (econf:atom())(A)}]
|
|
|
|
end,
|
|
|
|
econf:and_then(
|
|
|
|
econf:options(maps:merge(acl:validators(), Map), Opts),
|
|
|
|
fun(Rules) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({Type, Rs}) when is_list(Rs) ->
|
|
|
|
case maps:is_key(Type, acl:validators()) of
|
|
|
|
true -> [{acl, {Type, R}} || R <- Rs];
|
|
|
|
false -> [{Type, Rs}]
|
|
|
|
end;
|
|
|
|
(Other) ->
|
|
|
|
[Other]
|
|
|
|
end, Rules)
|
|
|
|
end)).
|
|
|
|
|
|
|
|
validator(from) ->
|
|
|
|
fun(L) when is_list(L) ->
|
|
|
|
lists:map(
|
|
|
|
fun({K, V}) -> {(econf:enum([tag]))(K), (econf:binary())(V)};
|
2022-04-11 15:33:06 +02:00
|
|
|
(A) -> (econf:enum([ejabberd_xmlrpc, mod_cron, mod_http_api, ejabberd_ctl]))(A)
|
2019-06-14 11:33:26 +02:00
|
|
|
end, lists:flatten(L));
|
|
|
|
(A) ->
|
2022-04-11 15:33:06 +02:00
|
|
|
[(econf:enum([ejabberd_xmlrpc, mod_cron, mod_http_api, ejabberd_ctl]))(A)]
|
2019-06-14 11:33:26 +02:00
|
|
|
end;
|
|
|
|
validator(what) ->
|
|
|
|
econf:and_then(
|
|
|
|
econf:list_or_single(econf:non_empty(econf:binary())),
|
|
|
|
fun parse_what/1);
|
|
|
|
validator(who) ->
|
|
|
|
validator(#{access => econf:acl(), oauth => validator(oauth)}, []);
|
|
|
|
validator(oauth) ->
|
|
|
|
econf:and_then(
|
|
|
|
validator(#{access => econf:acl(),
|
|
|
|
scope => econf:non_empty(
|
|
|
|
econf:list_or_single(econf:binary()))},
|
|
|
|
[{required, [scope]}]),
|
|
|
|
fun(Os) ->
|
|
|
|
{[Scopes], Rest} = proplists:split(Os, [scope]),
|
|
|
|
{lists:flatten([S || {_, S} <- Scopes]), Rest}
|
|
|
|
end).
|
|
|
|
|
|
|
|
validator() ->
|
|
|
|
econf:map(
|
|
|
|
econf:binary(),
|
|
|
|
econf:and_then(
|
|
|
|
econf:options(
|
|
|
|
#{from => validator(from),
|
|
|
|
what => validator(what),
|
|
|
|
who => validator(who)}),
|
|
|
|
fun(Os) ->
|
|
|
|
{proplists:get_value(from, Os, []),
|
|
|
|
proplists:get_value(who, Os, none),
|
|
|
|
proplists:get_value(what, Os, [])}
|
|
|
|
end),
|
|
|
|
[unique]).
|