2003-01-18 20:42:48 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : acl.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-01-29 21:21:14 +01:00
|
|
|
%%% Purpose : ACL support
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 18 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2007-12-24 12:41:41 +01: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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
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.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2003-01-18 20:42:48 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(acl).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
-behaviour(gen_server).
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2016-04-06 14:48:05 +02:00
|
|
|
-export([add_access/3, clear/0]).
|
2017-02-24 10:05:47 +01:00
|
|
|
-export([start_link/0, add/3, add_list/3, add_local/3, add_list_local/3,
|
2017-12-26 19:37:11 +01:00
|
|
|
load_from_config/0, reload_from_config/0, match_rule/3,
|
|
|
|
any_rules_allowed/3, transform_options/1, opt_type/1,
|
|
|
|
acl_rule_matches/3, acl_rule_verify/1, access_matches/3,
|
2016-06-21 12:26:31 +02:00
|
|
|
transform_access_rules_config/1,
|
2018-05-04 09:52:06 +02:00
|
|
|
parse_ip_netmask/1, ip_matches_mask/3,
|
2016-10-05 13:21:11 +02:00
|
|
|
access_rules_validator/1, shaper_rules_validator/1,
|
|
|
|
normalize_spec/1, resolve_access/2]).
|
2017-02-24 10:05:47 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2016-04-06 13:59:06 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-26 09:01:59 +02:00
|
|
|
-include("jid.hrl").
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2003-01-29 18:12:23 +01:00
|
|
|
-record(acl, {aclname, aclspec}).
|
2013-12-10 08:40:43 +01:00
|
|
|
-record(access, {name :: aclname(),
|
2013-08-12 14:25:05 +02:00
|
|
|
rules = [] :: [access_rule()]}).
|
2017-02-24 10:05:47 +01:00
|
|
|
-record(state, {}).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-type regexp() :: binary().
|
2016-04-06 14:48:05 +02:00
|
|
|
-type iprange() :: {inet:ip_address(), integer()} | binary().
|
2013-03-14 10:33:02 +01:00
|
|
|
-type glob() :: binary().
|
2013-08-12 14:25:05 +02:00
|
|
|
-type access_name() :: atom().
|
|
|
|
-type access_rule() :: {atom(), any()}.
|
|
|
|
-type host() :: binary().
|
2013-03-14 10:33:02 +01:00
|
|
|
-type aclname() :: {atom(), binary() | global}.
|
|
|
|
-type aclspec() :: all | none |
|
2013-08-12 14:25:05 +02:00
|
|
|
{user, {binary(), host()} | binary()} |
|
2013-03-14 10:33:02 +01:00
|
|
|
{server, binary()} |
|
|
|
|
{resource, binary()} |
|
2013-08-12 14:25:05 +02:00
|
|
|
{user_regexp, {regexp(), host()} | regexp()} |
|
|
|
|
{shared_group, {binary(), host()} | binary()} |
|
|
|
|
{user_regexp, {regexp(), host()} | regexp()} |
|
2013-03-14 10:33:02 +01:00
|
|
|
{server_regexp, regexp()} |
|
|
|
|
{resource_regexp, regexp()} |
|
2013-08-12 14:25:05 +02:00
|
|
|
{node_regexp, {regexp(), regexp()}} |
|
|
|
|
{user_glob, {glob(), host()} | glob()} |
|
2013-03-14 10:33:02 +01:00
|
|
|
{server_glob, glob()} |
|
|
|
|
{resource_glob, glob()} |
|
2016-04-06 14:48:05 +02:00
|
|
|
{ip, iprange()} |
|
2013-08-12 14:25:05 +02:00
|
|
|
{node_glob, {glob(), glob()}}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-type acl() :: #acl{aclname :: aclname(),
|
|
|
|
aclspec :: aclspec()}.
|
|
|
|
|
|
|
|
-export_type([acl/0]).
|
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, acl,
|
2013-08-12 14:25:05 +02:00
|
|
|
[{ram_copies, [node()]}, {type, bag},
|
2013-07-27 14:45:39 +02:00
|
|
|
{local_content, true},
|
2003-01-29 18:12:23 +01:00
|
|
|
{attributes, record_info(fields, acl)}]),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, access,
|
2013-08-12 14:25:05 +02:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes, record_info(fields, access)}]),
|
2017-12-26 19:37:11 +01:00
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, reload_from_config, 20),
|
2013-08-12 14:25:05 +02:00
|
|
|
load_from_config(),
|
2017-02-24 10:05:47 +01:00
|
|
|
{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) ->
|
2003-01-21 21:36:55 +01:00
|
|
|
ok.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
2013-07-27 14:45:39 +02:00
|
|
|
-spec add(binary(), aclname(), aclspec()) -> ok | {error, any()}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
add(Host, ACLName, ACLSpec) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
{ResL, BadNodes} = ejabberd_cluster:multicall(
|
2013-07-27 14:45:39 +02:00
|
|
|
?MODULE, add_local,
|
|
|
|
[Host, ACLName, ACLSpec]),
|
|
|
|
case lists:keyfind(aborted, 1, ResL) of
|
|
|
|
false when BadNodes == [] ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
{error, {failed_nodes, BadNodes}};
|
|
|
|
Err ->
|
|
|
|
{error, Err}
|
|
|
|
end.
|
|
|
|
|
|
|
|
add_local(Host, ACLName, ACLSpec) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2005-06-20 05:18:13 +02:00
|
|
|
mnesia:write(#acl{aclname = {ACLName, Host},
|
2008-02-12 02:04:47 +01:00
|
|
|
aclspec = normalize_spec(ACLSpec)})
|
2003-01-29 18:12:23 +01:00
|
|
|
end,
|
2013-07-27 14:45:39 +02:00
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2013-07-27 14:45:39 +02:00
|
|
|
-spec add_list(binary(), [acl()], boolean()) -> ok | {error, any()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
add_list(Host, ACLs, Clear) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
{ResL, BadNodes} = ejabberd_cluster:multicall(
|
2013-07-27 14:45:39 +02:00
|
|
|
?MODULE, add_list_local,
|
|
|
|
[Host, ACLs, Clear]),
|
|
|
|
case lists:keyfind(aborted, 1, ResL) of
|
|
|
|
false when BadNodes == [] ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
{error, {failed_nodes, BadNodes}};
|
|
|
|
Err ->
|
|
|
|
{error, Err}
|
|
|
|
end.
|
|
|
|
|
|
|
|
add_list_local(Host, ACLs, Clear) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
if Clear ->
|
|
|
|
Ks = mnesia:select(acl,
|
|
|
|
[{{acl, {'$1', Host}, '$2'}, [],
|
|
|
|
['$1']}]),
|
|
|
|
lists:foreach(fun (K) -> mnesia:delete({acl, {K, Host}})
|
|
|
|
end,
|
|
|
|
Ks);
|
|
|
|
true -> ok
|
2003-02-01 21:21:28 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (ACL) ->
|
2003-02-01 21:21:28 +01:00
|
|
|
case ACL of
|
2013-03-14 10:33:02 +01:00
|
|
|
#acl{aclname = ACLName,
|
|
|
|
aclspec = ACLSpec} ->
|
|
|
|
mnesia:write(#acl{aclname =
|
|
|
|
{ACLName,
|
|
|
|
Host},
|
|
|
|
aclspec =
|
|
|
|
normalize_spec(ACLSpec)})
|
2003-02-01 21:21:28 +01:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
ACLs)
|
2003-02-01 21:21:28 +01:00
|
|
|
end,
|
2013-07-27 14:45:39 +02:00
|
|
|
mnesia:transaction(F).
|
2003-02-01 21:21:28 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-spec add_access(binary() | global,
|
|
|
|
access_name(), [access_rule()]) -> ok | {error, any()}.
|
|
|
|
|
|
|
|
add_access(Host, Access, Rules) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
Obj = #access{name = {Access, Host}, rules = Rules},
|
|
|
|
case mnesia:transaction(fun() -> mnesia:write(Obj) end) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
{error, Err}
|
2013-08-12 14:25:05 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec load_from_config() -> ok.
|
|
|
|
|
|
|
|
load_from_config() ->
|
2018-06-14 13:00:47 +02:00
|
|
|
Hosts = [global|ejabberd_config:get_myhosts()],
|
2013-08-12 14:25:05 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
ACLs = ejabberd_config:get_option(
|
2017-04-29 10:39:40 +02:00
|
|
|
{acl, Host}, []),
|
2013-08-12 14:25:05 +02:00
|
|
|
AccessRules = ejabberd_config:get_option(
|
2017-04-29 10:39:40 +02:00
|
|
|
{access, Host}, []),
|
2016-05-25 09:19:46 +02:00
|
|
|
AccessRulesNew = ejabberd_config:get_option(
|
2017-04-29 10:39:40 +02:00
|
|
|
{access_rules, Host}, []),
|
2016-05-25 09:19:46 +02:00
|
|
|
ShaperRules = ejabberd_config:get_option(
|
2017-04-29 10:39:40 +02:00
|
|
|
{shaper_rules, Host}, []),
|
2013-08-12 14:25:05 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({ACLName, SpecList}) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({ACLType, ACLSpecs}) when is_list(ACLSpecs) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(ACLSpec) ->
|
|
|
|
add(Host, ACLName,
|
|
|
|
{ACLType, ACLSpec})
|
|
|
|
end, lists:flatten(ACLSpecs));
|
|
|
|
({ACLType, ACLSpecs}) ->
|
|
|
|
add(Host, ACLName, {ACLType, ACLSpecs})
|
|
|
|
end, lists:flatten(SpecList))
|
|
|
|
end, ACLs),
|
2016-05-25 09:19:46 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({Access, Rules}) ->
|
|
|
|
NRules = lists:map(fun({ACL, Type}) ->
|
|
|
|
{Type, [{acl, ACL}]}
|
|
|
|
end, Rules),
|
|
|
|
add_access(Host, Access, NRules ++ [{deny, [all]}])
|
|
|
|
end, AccessRules),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Access, Rules}) ->
|
|
|
|
add_access(Host, Access, Rules)
|
|
|
|
end, AccessRulesNew),
|
2013-08-12 14:25:05 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({Access, Rules}) ->
|
|
|
|
add_access(Host, Access, Rules)
|
2016-05-25 09:19:46 +02:00
|
|
|
end, ShaperRules)
|
2013-08-12 14:25:05 +02:00
|
|
|
end, Hosts).
|
|
|
|
|
2017-12-26 19:37:11 +01:00
|
|
|
-spec reload_from_config() -> ok.
|
|
|
|
|
|
|
|
reload_from_config() ->
|
|
|
|
clear(),
|
|
|
|
load_from_config().
|
|
|
|
|
2016-04-06 14:48:05 +02:00
|
|
|
%% Delete all previous set ACLs and Access rules
|
|
|
|
clear() ->
|
|
|
|
mnesia:clear_table(acl),
|
|
|
|
mnesia:clear_table(access),
|
|
|
|
ok.
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
b(S) ->
|
|
|
|
iolist_to_binary(S).
|
|
|
|
|
|
|
|
nodeprep(S) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nodeprep(b(S)).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
|
|
|
nameprep(S) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nameprep(b(S)).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
|
|
|
resourceprep(S) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:resourceprep(b(S)).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2016-05-30 12:30:44 +02:00
|
|
|
split_user_server(Str, NormFunUsr, NormFunSrv) ->
|
|
|
|
case binary:split(Str, <<"@">>) of
|
|
|
|
[U, S] ->
|
|
|
|
{NormFunUsr(U), NormFunSrv(S)};
|
|
|
|
_ ->
|
|
|
|
NormFunUsr(Str)
|
|
|
|
end.
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
normalize_spec(Spec) ->
|
|
|
|
case Spec of
|
|
|
|
all -> all;
|
|
|
|
none -> none;
|
2017-05-04 11:18:16 +02:00
|
|
|
{acl, N} when is_atom(N) ->
|
|
|
|
{acl, N};
|
|
|
|
{user, {U, S}} when is_binary(U), is_binary(S) ->
|
|
|
|
{user, {nodeprep(U), nameprep(S)}};
|
|
|
|
{user, U} when is_binary(U) ->
|
|
|
|
{user, split_user_server(U, fun nodeprep/1, fun nameprep/1)};
|
|
|
|
{shared_group, {G, H}} when is_binary(G), is_binary(H) ->
|
|
|
|
{shared_group, {b(G), nameprep(H)}};
|
|
|
|
{shared_group, G} when is_binary(G) ->
|
|
|
|
{shared_group, split_user_server(G, fun b/1, fun nameprep/1)};
|
|
|
|
{user_regexp, {UR, S}} when is_binary(UR), is_binary(S) ->
|
|
|
|
{user_regexp, {b(UR), nameprep(S)}};
|
|
|
|
{user_regexp, UR} when is_binary(UR) ->
|
|
|
|
{user_regexp, split_user_server(UR, fun b/1, fun nameprep/1)};
|
|
|
|
{node_regexp, {UR, SR}} when is_binary(UR), is_binary(SR) ->
|
|
|
|
{node_regexp, {b(UR), b(SR)}};
|
|
|
|
{user_glob, {UR, S}} when is_binary(UR), is_binary(S) ->
|
|
|
|
{user_glob, {b(UR), nameprep(S)}};
|
|
|
|
{user_glob, UR} when is_binary(UR) ->
|
|
|
|
{user_glob, split_user_server(UR, fun b/1, fun nameprep/1)};
|
|
|
|
{node_glob, {UR, SR}} when is_binary(UR), is_binary(SR) ->
|
|
|
|
{node_glob, {b(UR), b(SR)}};
|
|
|
|
{server, S} when is_binary(S) ->
|
|
|
|
{server, nameprep(S)};
|
|
|
|
{resource, R} when is_binary(R) ->
|
|
|
|
{resource, resourceprep(R)};
|
|
|
|
{server_regexp, SR} when is_binary(SR) ->
|
|
|
|
{server_regexp, b(SR)};
|
|
|
|
{resource_regexp, R} when is_binary(R) ->
|
|
|
|
{resource_regexp, b(R)};
|
|
|
|
{server_glob, S} when is_binary(S) ->
|
|
|
|
{server_glob, b(S)};
|
|
|
|
{resource_glob, R} when is_binary(R) ->
|
|
|
|
{resource_glob, b(R)};
|
|
|
|
{ip, {Net, Mask}} when is_binary(Net), is_integer(Mask) ->
|
|
|
|
{ip, {Net, Mask}};
|
2013-08-12 14:25:05 +02:00
|
|
|
{ip, S} ->
|
|
|
|
case parse_ip_netmask(b(S)) of
|
|
|
|
{ok, Net, Mask} ->
|
|
|
|
{ip, {Net, Mask}};
|
|
|
|
error ->
|
2018-09-19 22:12:14 +02:00
|
|
|
?WARNING_MSG("Invalid network address: ~p", [S]),
|
2013-08-12 14:25:05 +02:00
|
|
|
none
|
2017-05-04 11:18:16 +02:00
|
|
|
end;
|
|
|
|
BadVal ->
|
|
|
|
throw({<<"Invalid acl value">>, BadVal})
|
2013-08-12 14:25:05 +02:00
|
|
|
end.
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec any_rules_allowed(global | binary(), [access_name()],
|
2016-07-23 16:21:00 +02:00
|
|
|
jid() | ljid() | inet:ip_address()) -> boolean().
|
|
|
|
|
|
|
|
any_rules_allowed(Host, Access, Entity) ->
|
|
|
|
lists:any(fun (Rule) ->
|
|
|
|
allow == acl:match_rule(Host, Rule, Entity)
|
|
|
|
end,
|
|
|
|
Access).
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-spec match_rule(global | binary(), access_name(),
|
|
|
|
jid() | ljid() | inet:ip_address()) -> any().
|
|
|
|
|
2016-05-25 09:19:46 +02:00
|
|
|
match_rule(Host, Access, IP) when tuple_size(IP) == 4;
|
|
|
|
tuple_size(IP) == 8 ->
|
|
|
|
access_matches(Access, #{ip => IP}, Host);
|
2013-08-12 14:25:05 +02:00
|
|
|
match_rule(Host, Access, JID) ->
|
2016-05-25 09:19:46 +02:00
|
|
|
access_matches(Access, #{usr => jid:tolower(JID)}, Host).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2016-05-25 09:19:46 +02:00
|
|
|
-spec acl_rule_verify(aclspec()) -> boolean().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_verify(all) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
true;
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_verify(none) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({ip, {{A,B,C,D}, Mask}})
|
|
|
|
when is_integer(A), is_integer(B), is_integer(C), is_integer(D),
|
|
|
|
A >= 0, A =< 255, B >= 0, B =< 255, C >= 0, C =< 255, D >= 0, D =< 255,
|
|
|
|
is_integer(Mask), Mask >= 0, Mask =< 32 ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({ip, {{A,B,C,D,E,F,G,H}, Mask}}) when
|
|
|
|
is_integer(A), is_integer(B), is_integer(C), is_integer(D),
|
|
|
|
is_integer(E), is_integer(F), is_integer(G), is_integer(H),
|
|
|
|
A >= 0, A =< 65535, B >= 0, B =< 65535, C >= 0, C =< 65535, D >= 0, D =< 65535,
|
|
|
|
E >= 0, E =< 65535, F >= 0, F =< 65535, G >= 0, G =< 65535, H >= 0, H =< 65535,
|
|
|
|
is_integer(Mask), Mask >= 0, Mask =< 64 ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({user, {U, S}}) when is_binary(U), is_binary(S) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({user, U}) when is_binary(U) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({server, S}) when is_binary(S) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({resource, R}) when is_binary(R) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({shared_group, {G, H}}) when is_binary(G), is_binary(H) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({shared_group, G}) when is_binary(G) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({user_regexp, {UR, S}}) when is_binary(UR), is_binary(S) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({user_regexp, UR}) when is_binary(UR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({server_regexp, SR}) when is_binary(SR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({resource_regexp, RR}) when is_binary(RR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({node_regexp, {UR, SR}}) when is_binary(UR), is_binary(SR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({user_glob, {UR, S}}) when is_binary(UR), is_binary(S) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({user_glob, UR}) when is_binary(UR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({server_glob, SR}) when is_binary(SR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({resource_glob, RR}) when is_binary(RR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify({node_glob, {UR, SR}}) when is_binary(UR), is_binary(SR) ->
|
|
|
|
true;
|
|
|
|
acl_rule_verify(_Spec) ->
|
|
|
|
false.
|
2016-06-21 12:26:31 +02:00
|
|
|
invalid_syntax(Msg, Data) ->
|
2016-11-24 13:06:06 +01:00
|
|
|
throw({invalid_syntax, (str:format(Msg, Data))}).
|
2016-06-21 12:26:31 +02:00
|
|
|
|
|
|
|
acl_rules_verify([{acl, Name} | Rest], true) when is_atom(Name) ->
|
|
|
|
acl_rules_verify(Rest, true);
|
|
|
|
acl_rules_verify([{acl, Name} = Rule | _Rest], false) when is_atom(Name) ->
|
|
|
|
invalid_syntax(<<"Using acl: rules not allowed: ~p">>, [Rule]);
|
|
|
|
acl_rules_verify([Rule | Rest], AllowAcl) ->
|
|
|
|
case acl_rule_verify(Rule) of
|
|
|
|
false ->
|
|
|
|
invalid_syntax(<<"Invalid rule: ~p">>, [Rule]);
|
|
|
|
true ->
|
|
|
|
acl_rules_verify(Rest, AllowAcl)
|
|
|
|
end;
|
|
|
|
acl_rules_verify([], _AllowAcl) ->
|
|
|
|
true;
|
|
|
|
acl_rules_verify(Rules, _AllowAcl) ->
|
|
|
|
invalid_syntax(<<"Not a acl rules list: ~p">>, [Rules]).
|
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
|
2003-01-29 21:21:14 +01:00
|
|
|
|
2016-05-25 09:19:46 +02:00
|
|
|
all_acl_rules_matches([], _Data, _Host) ->
|
|
|
|
false;
|
|
|
|
all_acl_rules_matches(Rules, Data, Host) ->
|
|
|
|
all_acl_rules_matches2(Rules, Data, Host).
|
|
|
|
|
|
|
|
all_acl_rules_matches2([Rule | Tail], Data, Host) ->
|
|
|
|
case acl_rule_matches(Rule, Data, Host) of
|
|
|
|
true ->
|
|
|
|
all_acl_rules_matches2(Tail, Data, Host);
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
all_acl_rules_matches2([], _Data, _Host) ->
|
|
|
|
true.
|
|
|
|
|
2016-06-24 15:09:51 +02:00
|
|
|
any_acl_rules_matches([], _Data, _Host) ->
|
|
|
|
false;
|
|
|
|
any_acl_rules_matches([Rule|Tail], Data, Host) ->
|
|
|
|
case acl_rule_matches(Rule, Data, Host) of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
false ->
|
|
|
|
any_acl_rules_matches(Tail, Data, Host)
|
|
|
|
end.
|
|
|
|
|
2016-05-25 09:19:46 +02:00
|
|
|
-spec acl_rule_matches(aclspec(), any(), global|binary()) -> boolean().
|
|
|
|
|
|
|
|
acl_rule_matches(all, _Data, _Host) ->
|
|
|
|
true;
|
|
|
|
acl_rule_matches({acl, all}, _Data, _Host) ->
|
|
|
|
true;
|
|
|
|
acl_rule_matches({acl, Name}, Data, Host) ->
|
|
|
|
ACLs = get_aclspecs(Name, Host),
|
|
|
|
RawACLs = lists:map(fun(#acl{aclspec = R}) -> R end, ACLs),
|
2016-06-24 15:09:51 +02:00
|
|
|
any_acl_rules_matches(RawACLs, Data, Host);
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_matches({ip, {Net, Mask}}, #{ip := {IP, _Port}}, _Host) ->
|
2018-05-04 09:52:06 +02:00
|
|
|
ip_matches_mask(IP, Net, Mask);
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_matches({ip, {Net, Mask}}, #{ip := IP}, _Host) ->
|
2018-05-04 09:52:06 +02:00
|
|
|
ip_matches_mask(IP, Net, Mask);
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_matches({user, {U, S}}, #{usr := {U, S, _}}, _Host) ->
|
|
|
|
true;
|
|
|
|
acl_rule_matches({user, U}, #{usr := {U, S, _}}, _Host) ->
|
2018-06-14 13:00:47 +02:00
|
|
|
lists:member(S, ejabberd_config:get_myhosts());
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_matches({server, S}, #{usr := {_, S, _}}, _Host) ->
|
|
|
|
true;
|
|
|
|
acl_rule_matches({resource, R}, #{usr := {_, _, R}}, _Host) ->
|
|
|
|
true;
|
|
|
|
acl_rule_matches({shared_group, {G, H}}, #{usr := {U, S, _}}, _Host) ->
|
|
|
|
Mod = loaded_shared_roster_module(H),
|
|
|
|
Mod:is_user_in_group({U, S}, G, H);
|
|
|
|
acl_rule_matches({shared_group, G}, #{usr := {U, S, _}}, Host) ->
|
|
|
|
Mod = loaded_shared_roster_module(Host),
|
|
|
|
Mod:is_user_in_group({U, S}, G, Host);
|
|
|
|
acl_rule_matches({user_regexp, {UR, S}}, #{usr := {U, S, _}}, _Host) ->
|
|
|
|
is_regexp_match(U, UR);
|
|
|
|
acl_rule_matches({user_regexp, UR}, #{usr := {U, S, _}}, _Host) ->
|
2018-06-14 13:00:47 +02:00
|
|
|
lists:member(S, ejabberd_config:get_myhosts()) andalso is_regexp_match(U, UR);
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_matches({server_regexp, SR}, #{usr := {_, S, _}}, _Host) ->
|
|
|
|
is_regexp_match(S, SR);
|
|
|
|
acl_rule_matches({resource_regexp, RR}, #{usr := {_, _, R}}, _Host) ->
|
|
|
|
is_regexp_match(R, RR);
|
|
|
|
acl_rule_matches({node_regexp, {UR, SR}}, #{usr := {U, S, _}}, _Host) ->
|
|
|
|
is_regexp_match(U, UR) andalso is_regexp_match(S, SR);
|
|
|
|
acl_rule_matches({user_glob, {UR, S}}, #{usr := {U, S, _}}, _Host) ->
|
|
|
|
is_glob_match(U, UR);
|
|
|
|
acl_rule_matches({user_glob, UR}, #{usr := {U, S, _}}, _Host) ->
|
2018-06-14 13:00:47 +02:00
|
|
|
lists:member(S, ejabberd_config:get_myhosts()) andalso is_glob_match(U, UR);
|
2016-05-25 09:19:46 +02:00
|
|
|
acl_rule_matches({server_glob, SR}, #{usr := {_, S, _}}, _Host) ->
|
|
|
|
is_glob_match(S, SR);
|
|
|
|
acl_rule_matches({resource_glob, RR}, #{usr := {_, _, R}}, _Host) ->
|
|
|
|
is_glob_match(R, RR);
|
|
|
|
acl_rule_matches({node_glob, {UR, SR}}, #{usr := {U, S, _}}, _Host) ->
|
|
|
|
is_glob_match(U, UR) andalso is_glob_match(S, SR);
|
|
|
|
acl_rule_matches(_ACL, _Data, _Host) ->
|
|
|
|
false.
|
2016-04-08 19:45:25 +02:00
|
|
|
|
2016-10-05 13:21:11 +02:00
|
|
|
resolve_access(all, _Host) ->
|
|
|
|
all;
|
|
|
|
resolve_access(none, _Host) ->
|
|
|
|
none;
|
|
|
|
resolve_access(Name, Host) when is_atom(Name) ->
|
2016-09-19 11:54:46 +02:00
|
|
|
GAccess = mnesia:dirty_read(access, {Name, global}),
|
2016-05-25 09:19:46 +02:00
|
|
|
LAccess =
|
2016-10-05 13:21:11 +02:00
|
|
|
if Host /= global -> mnesia:dirty_read(access, {Name, Host});
|
2016-05-25 09:19:46 +02:00
|
|
|
true -> []
|
|
|
|
end,
|
|
|
|
case GAccess ++ LAccess of
|
|
|
|
[] ->
|
2016-10-05 13:21:11 +02:00
|
|
|
[];
|
2016-05-25 09:19:46 +02:00
|
|
|
AccessList ->
|
2016-10-05 13:21:11 +02:00
|
|
|
lists:flatmap(
|
2016-05-25 09:19:46 +02:00
|
|
|
fun(#access{rules = Rs}) ->
|
|
|
|
Rs
|
2016-10-05 13:21:11 +02:00
|
|
|
end, AccessList)
|
2016-05-25 09:19:46 +02:00
|
|
|
end;
|
2016-10-05 13:21:11 +02:00
|
|
|
resolve_access(Rules, _Host) when is_list(Rules) ->
|
|
|
|
Rules.
|
|
|
|
|
2017-02-16 15:38:30 +01:00
|
|
|
-spec access_matches(atom()|list(), any(), global|binary()) -> allow|deny|atom()|integer().
|
2016-10-05 13:21:11 +02:00
|
|
|
access_matches(Rules, Data, Host) ->
|
|
|
|
case resolve_access(Rules, Host) of
|
|
|
|
all -> allow;
|
|
|
|
none -> deny;
|
|
|
|
RRules -> access_rules_matches(RRules, Data, Host)
|
|
|
|
end.
|
2016-05-25 09:19:46 +02:00
|
|
|
|
|
|
|
-spec access_rules_matches(list(), any(), global|binary()) -> any().
|
|
|
|
|
|
|
|
access_rules_matches(AR, Data, Host) ->
|
|
|
|
access_rules_matches(AR, Data, Host, deny).
|
|
|
|
|
|
|
|
access_rules_matches([{Type, Acls} | Rest], Data, Host, Default) ->
|
|
|
|
case all_acl_rules_matches(Acls, Data, Host) of
|
|
|
|
false ->
|
|
|
|
access_rules_matches(Rest, Data, Host, Default);
|
|
|
|
true ->
|
|
|
|
Type
|
|
|
|
end;
|
|
|
|
access_rules_matches([], _Data, _Host, Default) ->
|
|
|
|
Default.
|
2016-04-08 19:45:25 +02:00
|
|
|
|
2016-05-25 09:19:46 +02:00
|
|
|
get_aclspecs(ACL, Host) ->
|
2016-09-19 11:54:46 +02:00
|
|
|
mnesia:dirty_read(acl, {ACL, Host}) ++ mnesia:dirty_read(acl, {ACL, global}).
|
2016-04-08 19:45:25 +02:00
|
|
|
|
2003-01-29 21:21:14 +01:00
|
|
|
is_regexp_match(String, RegExp) ->
|
2011-11-22 23:11:21 +01:00
|
|
|
case ejabberd_regexp:run(String, RegExp) of
|
2013-03-14 10:33:02 +01:00
|
|
|
nomatch -> false;
|
|
|
|
match -> true;
|
|
|
|
{error, ErrDesc} ->
|
|
|
|
?ERROR_MSG("Wrong regexp ~p in ACL: ~p",
|
|
|
|
[RegExp, ErrDesc]),
|
|
|
|
false
|
2003-01-29 21:21:14 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
is_glob_match(String, Glob) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
is_regexp_match(String,
|
|
|
|
ejabberd_regexp:sh_to_awk(Glob)).
|
2003-01-29 21:21:14 +01:00
|
|
|
|
2018-05-04 09:52:06 +02:00
|
|
|
ip_matches_mask({_, _, _, _} = IP, {_, _, _, _} = Net, Mask) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
IPInt = ip_to_integer(IP),
|
|
|
|
NetInt = ip_to_integer(Net),
|
|
|
|
M = bnot (1 bsl (32 - Mask) - 1),
|
|
|
|
IPInt band M =:= NetInt band M;
|
2018-05-04 09:52:06 +02:00
|
|
|
ip_matches_mask({_, _, _, _, _, _, _, _} = IP,
|
|
|
|
{_, _, _, _, _, _, _, _} = Net, Mask) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
IPInt = ip_to_integer(IP),
|
|
|
|
NetInt = ip_to_integer(Net),
|
|
|
|
M = bnot (1 bsl (128 - Mask) - 1),
|
|
|
|
IPInt band M =:= NetInt band M;
|
2018-05-04 09:52:06 +02:00
|
|
|
ip_matches_mask({_, _, _, _} = IP,
|
|
|
|
{0, 0, 0, 0, 0, 16#FFFF, _, _} = Net, Mask) ->
|
|
|
|
IPInt = ip_to_integer({0, 0, 0, 0, 0, 16#FFFF, 0, 0}) + ip_to_integer(IP),
|
|
|
|
NetInt = ip_to_integer(Net),
|
|
|
|
M = bnot (1 bsl (128 - Mask) - 1),
|
|
|
|
IPInt band M =:= NetInt band M;
|
|
|
|
ip_matches_mask({0, 0, 0, 0, 0, 16#FFFF, _, _} = IP,
|
|
|
|
{_, _, _, _} = Net, Mask) ->
|
|
|
|
IPInt = ip_to_integer(IP) - ip_to_integer({0, 0, 0, 0, 0, 16#FFFF, 0, 0}),
|
|
|
|
NetInt = ip_to_integer(Net),
|
|
|
|
M = bnot (1 bsl (32 - Mask) - 1),
|
|
|
|
IPInt band M =:= NetInt band M;
|
|
|
|
ip_matches_mask(_, _, _) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
false.
|
|
|
|
|
|
|
|
ip_to_integer({IP1, IP2, IP3, IP4}) ->
|
|
|
|
IP1 bsl 8 bor IP2 bsl 8 bor IP3 bsl 8 bor IP4;
|
|
|
|
ip_to_integer({IP1, IP2, IP3, IP4, IP5, IP6, IP7,
|
|
|
|
IP8}) ->
|
|
|
|
IP1 bsl 16 bor IP2 bsl 16 bor IP3 bsl 16 bor IP4 bsl 16
|
|
|
|
bor IP5
|
|
|
|
bsl 16
|
|
|
|
bor IP6
|
|
|
|
bsl 16
|
|
|
|
bor IP7
|
|
|
|
bsl 16
|
|
|
|
bor IP8.
|
|
|
|
|
2012-04-04 12:37:43 +02:00
|
|
|
loaded_shared_roster_module(Host) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
case gen_mod:is_loaded(Host, mod_shared_roster_ldap) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> mod_shared_roster_ldap;
|
|
|
|
false -> mod_shared_roster
|
|
|
|
end.
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
parse_ip_netmask(S) ->
|
|
|
|
case str:tokens(S, <<"/">>) of
|
|
|
|
[IPStr] ->
|
|
|
|
case inet_parse:address(binary_to_list(IPStr)) of
|
|
|
|
{ok, {_, _, _, _} = IP} -> {ok, IP, 32};
|
|
|
|
{ok, {_, _, _, _, _, _, _, _} = IP} -> {ok, IP, 128};
|
|
|
|
_ -> error
|
|
|
|
end;
|
|
|
|
[IPStr, MaskStr] ->
|
2016-07-26 09:01:59 +02:00
|
|
|
case catch binary_to_integer(MaskStr) of
|
2013-08-12 14:25:05 +02:00
|
|
|
Mask when is_integer(Mask), Mask >= 0 ->
|
|
|
|
case inet_parse:address(binary_to_list(IPStr)) of
|
|
|
|
{ok, {_, _, _, _} = IP} when Mask =< 32 ->
|
|
|
|
{ok, IP, Mask};
|
|
|
|
{ok, {_, _, _, _, _, _, _, _} = IP} when Mask =< 128 ->
|
|
|
|
{ok, IP, Mask};
|
|
|
|
_ -> error
|
|
|
|
end;
|
|
|
|
_ -> error
|
|
|
|
end;
|
|
|
|
_ -> error
|
2012-04-04 12:37:43 +02:00
|
|
|
end.
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2016-06-16 11:04:01 +02:00
|
|
|
transform_access_rules_config(Config) when is_list(Config) ->
|
|
|
|
lists:map(fun transform_access_rules_config2/1, lists:flatten(Config));
|
2016-05-25 09:19:46 +02:00
|
|
|
transform_access_rules_config(Config) ->
|
2016-06-16 11:04:01 +02:00
|
|
|
transform_access_rules_config([Config]).
|
2016-05-25 09:19:46 +02:00
|
|
|
|
2016-06-16 11:04:01 +02:00
|
|
|
transform_access_rules_config2(Type) when is_integer(Type); is_atom(Type) ->
|
2016-06-15 19:20:27 +02:00
|
|
|
{Type, [all]};
|
|
|
|
transform_access_rules_config2({Type, ACL}) when is_atom(ACL) ->
|
|
|
|
{Type, [{acl, ACL}]};
|
2016-05-25 09:19:46 +02:00
|
|
|
transform_access_rules_config2({Res, Rules}) when is_list(Rules) ->
|
2016-06-15 19:20:27 +02:00
|
|
|
T = lists:map(fun({Type, Args}) when is_list(Args) ->
|
|
|
|
normalize_spec({Type, hd(lists:flatten(Args))});
|
|
|
|
(V) -> normalize_spec(V)
|
|
|
|
end, lists:flatten(Rules)),
|
|
|
|
{Res, T};
|
2016-05-25 09:19:46 +02:00
|
|
|
transform_access_rules_config2({Res, Rule}) ->
|
|
|
|
{Res, [Rule]}.
|
|
|
|
|
2016-06-21 12:26:31 +02:00
|
|
|
access_rules_validator(Name) when is_atom(Name) ->
|
|
|
|
Name;
|
|
|
|
access_rules_validator(Rules0) ->
|
|
|
|
Rules = transform_access_rules_config(Rules0),
|
|
|
|
access_shaper_rules_validator(Rules, fun(allow) -> true;
|
|
|
|
(deny) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end),
|
2017-04-29 10:39:40 +02:00
|
|
|
Rules.
|
2016-06-21 12:26:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
shaper_rules_validator(Name) when is_atom(Name) ->
|
|
|
|
Name;
|
|
|
|
shaper_rules_validator(Rules0) ->
|
|
|
|
Rules = transform_access_rules_config(Rules0),
|
|
|
|
access_shaper_rules_validator(Rules, fun(V) when is_atom(V) -> true;
|
|
|
|
(V2) when is_integer(V2) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end),
|
2017-04-29 10:39:40 +02:00
|
|
|
Rules.
|
2016-06-21 12:26:31 +02:00
|
|
|
|
|
|
|
access_shaper_rules_validator([{Type, Acls} = Rule | Rest], RuleTypeCheck) ->
|
|
|
|
case RuleTypeCheck(Type) of
|
|
|
|
true ->
|
|
|
|
case acl_rules_verify(Acls, true) of
|
|
|
|
true ->
|
|
|
|
access_shaper_rules_validator(Rest, RuleTypeCheck);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
invalid_syntax(<<"Invalid rule type: ~p in rule ~p">>, [Type, Rule])
|
|
|
|
end;
|
|
|
|
access_shaper_rules_validator([], _RuleTypeCheck) ->
|
|
|
|
true;
|
|
|
|
access_shaper_rules_validator(Value, _RuleTypeCheck) ->
|
|
|
|
invalid_syntax(<<"Not a rule definition: ~p">>, [Value]).
|
|
|
|
|
2016-05-25 09:19:46 +02:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_options(Opts) ->
|
|
|
|
Opts1 = lists:foldl(fun transform_options/2, [], Opts),
|
|
|
|
{ACLOpts, Opts2} = lists:mapfoldl(
|
|
|
|
fun({acl, Os}, Acc) ->
|
|
|
|
{Os, Acc};
|
|
|
|
(O, Acc) ->
|
|
|
|
{[], [O|Acc]}
|
|
|
|
end, [], Opts1),
|
|
|
|
{AccessOpts, Opts3} = lists:mapfoldl(
|
|
|
|
fun({access, Os}, Acc) ->
|
|
|
|
{Os, Acc};
|
|
|
|
(O, Acc) ->
|
|
|
|
{[], [O|Acc]}
|
|
|
|
end, [], Opts2),
|
2016-05-25 09:19:46 +02:00
|
|
|
{NewAccessOpts, Opts4} = lists:mapfoldl(
|
|
|
|
fun({access_rules, Os}, Acc) ->
|
|
|
|
{Os, Acc};
|
|
|
|
(O, Acc) ->
|
|
|
|
{[], [O|Acc]}
|
|
|
|
end, [], Opts3),
|
|
|
|
{ShaperOpts, Opts5} = lists:mapfoldl(
|
|
|
|
fun({shaper_rules, Os}, Acc) ->
|
|
|
|
{Os, Acc};
|
|
|
|
(O, Acc) ->
|
|
|
|
{[], [O|Acc]}
|
|
|
|
end, [], Opts4),
|
2013-08-12 14:25:05 +02:00
|
|
|
ACLOpts1 = ejabberd_config:collect_options(lists:flatten(ACLOpts)),
|
|
|
|
AccessOpts1 = case ejabberd_config:collect_options(
|
|
|
|
lists:flatten(AccessOpts)) of
|
|
|
|
[] -> [];
|
|
|
|
L1 -> [{access, L1}]
|
|
|
|
end,
|
|
|
|
ACLOpts2 = case lists:map(
|
|
|
|
fun({ACLName, Os}) ->
|
|
|
|
{ACLName, ejabberd_config:collect_options(Os)}
|
|
|
|
end, ACLOpts1) of
|
|
|
|
[] -> [];
|
|
|
|
L2 -> [{acl, L2}]
|
|
|
|
end,
|
2016-05-25 09:19:46 +02:00
|
|
|
NewAccessOpts1 = case lists:map(
|
|
|
|
fun({NAName, Os}) ->
|
|
|
|
{NAName, transform_access_rules_config(Os)}
|
|
|
|
end, lists:flatten(NewAccessOpts)) of
|
|
|
|
[] -> [];
|
|
|
|
L3 -> [{access_rules, L3}]
|
|
|
|
end,
|
|
|
|
ShaperOpts1 = case lists:map(
|
|
|
|
fun({SName, Ss}) ->
|
|
|
|
{SName, transform_access_rules_config(Ss)}
|
|
|
|
end, lists:flatten(ShaperOpts)) of
|
|
|
|
[] -> [];
|
|
|
|
L4 -> [{shaper_rules, L4}]
|
|
|
|
end,
|
|
|
|
ACLOpts2 ++ AccessOpts1 ++ NewAccessOpts1 ++ ShaperOpts1 ++ Opts5.
|
2013-08-12 14:25:05 +02:00
|
|
|
|
|
|
|
transform_options({acl, Name, Type}, Opts) ->
|
|
|
|
T = case Type of
|
|
|
|
all -> all;
|
|
|
|
none -> none;
|
2013-11-06 17:43:24 +01:00
|
|
|
{user, U} -> {user, [b(U)]};
|
|
|
|
{user, U, S} -> {user, [[{b(U), b(S)}]]};
|
|
|
|
{shared_group, G} -> {shared_group, [b(G)]};
|
|
|
|
{shared_group, G, H} -> {shared_group, [[{b(G), b(H)}]]};
|
|
|
|
{user_regexp, UR} -> {user_regexp, [b(UR)]};
|
|
|
|
{user_regexp, UR, S} -> {user_regexp, [[{b(UR), b(S)}]]};
|
|
|
|
{node_regexp, UR, SR} -> {node_regexp, [[{b(UR), b(SR)}]]};
|
|
|
|
{user_glob, UR} -> {user_glob, [b(UR)]};
|
|
|
|
{user_glob, UR, S} -> {user_glob, [[{b(UR), b(S)}]]};
|
|
|
|
{node_glob, UR, SR} -> {node_glob, [[{b(UR), b(SR)}]]};
|
|
|
|
{server, S} -> {server, [b(S)]};
|
|
|
|
{resource, R} -> {resource, [b(R)]};
|
|
|
|
{server_regexp, SR} -> {server_regexp, [b(SR)]};
|
|
|
|
{server_glob, S} -> {server_glob, [b(S)]};
|
|
|
|
{ip, S} -> {ip, [b(S)]};
|
2016-09-08 10:27:14 +02:00
|
|
|
{resource_glob, R} -> {resource_glob, [b(R)]};
|
2016-09-08 08:44:49 +02:00
|
|
|
{resource_regexp, R} -> {resource_regexp, [b(R)]}
|
2013-08-12 14:25:05 +02:00
|
|
|
end,
|
|
|
|
[{acl, [{Name, [T]}]}|Opts];
|
|
|
|
transform_options({access, Name, Rules}, Opts) ->
|
|
|
|
NewRules = [{ACL, Action} || {Action, ACL} <- Rules],
|
|
|
|
[{access, [{Name, NewRules}]}|Opts];
|
|
|
|
transform_options(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(access) -> fun (V) -> V end;
|
2016-05-25 09:19:46 +02:00
|
|
|
opt_type(access_rules) -> fun (V) -> V end;
|
|
|
|
opt_type(shaper_rules) -> fun (V) -> V end;
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(acl) -> fun (V) -> V end;
|
2016-06-16 11:00:38 +02:00
|
|
|
opt_type(_) -> [access, acl, access_rules, shaper_rules].
|