2003-01-18 20:42:48 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : acl.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2003-01-29 21:21:14 +01:00
|
|
|
%%% Purpose : ACL support
|
2003-01-18 20:42:48 +01:00
|
|
|
%%% Created : 18 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(acl).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-01-29 21:21:14 +01:00
|
|
|
-export([start/0,
|
2005-06-20 05:18:13 +02:00
|
|
|
to_record/3,
|
|
|
|
add/3,
|
|
|
|
add_list/3,
|
|
|
|
match_rule/3,
|
2003-01-29 21:21:14 +01:00
|
|
|
% for debugging only
|
2005-06-20 05:18:13 +02:00
|
|
|
match_acl/3]).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2003-01-29 18:12:23 +01:00
|
|
|
-record(acl, {aclname, aclspec}).
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
start() ->
|
2003-01-29 18:12:23 +01:00
|
|
|
mnesia:create_table(acl,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, acl)}]),
|
|
|
|
mnesia:add_table_copy(acl, node(), ram_copies),
|
2003-01-21 21:36:55 +01:00
|
|
|
ok.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
to_record(Host, ACLName, ACLSpec) ->
|
|
|
|
#acl{aclname = {ACLName, Host}, aclspec = ACLSpec}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
add(Host, ACLName, ACLSpec) ->
|
2003-01-29 18:12:23 +01:00
|
|
|
F = fun() ->
|
2005-06-20 05:18:13 +02:00
|
|
|
mnesia:write(#acl{aclname = {ACLName, Host},
|
|
|
|
aclspec = ACLSpec})
|
2003-01-29 18:12:23 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
add_list(Host, ACLs, Clear) ->
|
2003-02-01 21:21:28 +01:00
|
|
|
F = fun() ->
|
|
|
|
if
|
|
|
|
Clear ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Ks = mnesia:select(
|
|
|
|
acl, [{{acl, {'$1', Host}, '$2'}, [], ['$1']}]),
|
2003-02-01 21:21:28 +01:00
|
|
|
lists:foreach(fun(K) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
mnesia:delete({acl, {K, Host}})
|
2003-02-01 21:21:28 +01:00
|
|
|
end, Ks);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
lists:foreach(fun(ACL) ->
|
|
|
|
case ACL of
|
2005-06-20 05:18:13 +02:00
|
|
|
#acl{aclname = ACLName,
|
|
|
|
aclspec = ACLSpec} ->
|
|
|
|
mnesia:write(
|
|
|
|
#acl{aclname = {ACLName, Host},
|
|
|
|
aclspec = ACLSpec})
|
2003-02-01 21:21:28 +01:00
|
|
|
end
|
|
|
|
end, ACLs)
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, _} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
match_rule(global, Rule, JID) ->
|
2003-02-01 21:21:28 +01:00
|
|
|
case Rule of
|
|
|
|
all -> allow;
|
|
|
|
none -> deny;
|
|
|
|
_ ->
|
2005-06-20 05:18:13 +02:00
|
|
|
case ejabberd_config:get_global_option({access, Rule, global}) of
|
2003-02-01 21:21:28 +01:00
|
|
|
undefined ->
|
|
|
|
deny;
|
2005-06-20 05:18:13 +02:00
|
|
|
GACLs ->
|
|
|
|
match_acls(GACLs, JID, global)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
|
|
|
match_rule(Host, Rule, JID) ->
|
|
|
|
case Rule of
|
|
|
|
all -> allow;
|
|
|
|
none -> deny;
|
|
|
|
_ ->
|
|
|
|
case ejabberd_config:get_global_option({access, Rule, global}) of
|
|
|
|
undefined ->
|
|
|
|
case ejabberd_config:get_global_option({access, Rule, Host}) of
|
|
|
|
undefined ->
|
|
|
|
deny;
|
|
|
|
ACLs ->
|
|
|
|
match_acls(ACLs, JID, Host)
|
|
|
|
end;
|
|
|
|
GACLs ->
|
|
|
|
case ejabberd_config:get_global_option({access, Rule, Host}) of
|
|
|
|
undefined ->
|
|
|
|
match_acls(GACLs, JID, Host);
|
|
|
|
ACLs ->
|
2005-07-15 01:48:57 +02:00
|
|
|
case lists:reverse(GACLs) of
|
|
|
|
[{allow, all} | Rest] ->
|
|
|
|
match_acls(
|
|
|
|
lists:reverse(Rest) ++ ACLs ++
|
|
|
|
[{allow, all}],
|
|
|
|
JID, Host);
|
|
|
|
_ ->
|
|
|
|
match_acls(GACLs ++ ACLs, JID, Host)
|
|
|
|
end
|
2005-06-20 05:18:13 +02:00
|
|
|
end
|
2003-02-01 21:21:28 +01:00
|
|
|
end
|
2003-01-19 21:17:56 +01:00
|
|
|
end.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2007-07-30 13:06:50 +02:00
|
|
|
match_acls([], _, _Host) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
deny;
|
2005-06-20 05:18:13 +02:00
|
|
|
match_acls([{Access, ACL} | ACLs], JID, Host) ->
|
|
|
|
case match_acl(ACL, JID, Host) of
|
2003-01-18 20:42:48 +01:00
|
|
|
true ->
|
|
|
|
Access;
|
|
|
|
_ ->
|
2005-06-20 05:18:13 +02:00
|
|
|
match_acls(ACLs, JID, Host)
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
match_acl(ACL, JID, Host) ->
|
2003-02-01 21:21:28 +01:00
|
|
|
case ACL of
|
|
|
|
all -> true;
|
|
|
|
none -> false;
|
|
|
|
_ ->
|
2007-07-30 13:06:50 +02:00
|
|
|
{User, Server, _Resource} = jlib:jid_tolower(JID),
|
2003-02-01 21:21:28 +01:00
|
|
|
lists:any(fun(#acl{aclspec = Spec}) ->
|
|
|
|
case Spec of
|
|
|
|
all ->
|
|
|
|
true;
|
|
|
|
{user, U} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
(U == User)
|
|
|
|
andalso
|
|
|
|
((Host == Server) orelse
|
|
|
|
((Host == global) andalso
|
|
|
|
lists:member(Server, ?MYHOSTS)));
|
2003-02-01 21:21:28 +01:00
|
|
|
{user, U, S} ->
|
|
|
|
(U == User) andalso (S == Server);
|
|
|
|
{server, S} ->
|
|
|
|
S == Server;
|
|
|
|
{user_regexp, UR} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
((Host == Server) orelse
|
|
|
|
((Host == global) andalso
|
|
|
|
lists:member(Server, ?MYHOSTS)))
|
|
|
|
andalso is_regexp_match(User, UR);
|
2003-02-01 21:21:28 +01:00
|
|
|
{user_regexp, UR, S} ->
|
|
|
|
(S == Server) andalso
|
|
|
|
is_regexp_match(User, UR);
|
|
|
|
{server_regexp, SR} ->
|
|
|
|
is_regexp_match(Server, SR);
|
|
|
|
{node_regexp, UR, SR} ->
|
|
|
|
is_regexp_match(Server, SR) andalso
|
|
|
|
is_regexp_match(User, UR);
|
|
|
|
{user_glob, UR} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
((Host == Server) orelse
|
|
|
|
((Host == global) andalso
|
|
|
|
lists:member(Server, ?MYHOSTS)))
|
|
|
|
andalso
|
2003-02-01 21:21:28 +01:00
|
|
|
is_glob_match(User, UR);
|
|
|
|
{user_glob, UR, S} ->
|
|
|
|
(S == Server) andalso
|
|
|
|
is_glob_match(User, UR);
|
|
|
|
{server_glob, SR} ->
|
|
|
|
is_glob_match(Server, SR);
|
|
|
|
{node_glob, UR, SR} ->
|
|
|
|
is_glob_match(Server, SR) andalso
|
2006-07-18 14:29:17 +02:00
|
|
|
is_glob_match(User, UR);
|
|
|
|
WrongSpec ->
|
|
|
|
?ERROR_MSG(
|
|
|
|
"Wrong ACL expression: ~p~n"
|
|
|
|
"Check your config file and reload it with the override_acls option enabled",
|
|
|
|
[WrongSpec]),
|
|
|
|
false
|
2003-02-01 21:21:28 +01:00
|
|
|
end
|
2005-06-20 05:18:13 +02:00
|
|
|
end,
|
|
|
|
ets:lookup(acl, {ACL, global}) ++
|
|
|
|
ets:lookup(acl, {ACL, Host}))
|
2003-02-01 21:21:28 +01:00
|
|
|
end.
|
2003-01-29 21:21:14 +01:00
|
|
|
|
|
|
|
is_regexp_match(String, RegExp) ->
|
|
|
|
case regexp:first_match(String, RegExp) of
|
|
|
|
nomatch ->
|
|
|
|
false;
|
|
|
|
{match, _, _} ->
|
|
|
|
true;
|
|
|
|
{error, ErrDesc} ->
|
|
|
|
?ERROR_MSG(
|
|
|
|
"Wrong regexp ~p in ACL: ~p",
|
|
|
|
[RegExp, lists:flatten(regexp:format_error(ErrDesc))]),
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_glob_match(String, Glob) ->
|
|
|
|
is_regexp_match(String, regexp:sh_to_awk(Glob)).
|
|
|
|
|
|
|
|
|