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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-18 20:42:48 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(acl).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-18 20:42:48 +01:00
|
|
|
|
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").
|
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @type aclspec() = all | JID_Exact | JID_Regexp | JID_Glob | Shared_Group
|
|
|
|
%% JID_Exact = {user, U} | {user, U, S} | {server, S} | {resource, R}
|
|
|
|
%% U = string()
|
|
|
|
%% S = string()
|
|
|
|
%% R = string()
|
|
|
|
%% JID_Regexp = {user_regexp, UR} | {user_regexp, UR, S} | {server_regexp, SR} | {resource_regexp, RR} | {node_regexp, UR, SR}
|
|
|
|
%% UR = string()
|
|
|
|
%% SR = string()
|
|
|
|
%% RR = string()
|
|
|
|
%% JID_Glob = {user_glob, UG} | {user_glob, UG, S} | {server_glob, SG} | {resource_glob, RG} | {node_glob, UG, SG}
|
|
|
|
%% UG = string()
|
|
|
|
%% SG = string()
|
|
|
|
%% RG = string()
|
|
|
|
%% Shared_Group = {shared_group, G} | {shared_group, G, H}
|
|
|
|
%% G = string()
|
|
|
|
%% H = string().
|
|
|
|
|
|
|
|
%% @type acl() = {acl, ACLName, ACLSpec}
|
|
|
|
%% ACLName = atom()
|
|
|
|
%% ACLSpec = aclspec().
|
|
|
|
%% Record in its Ejabberd-configuration-file variant.
|
|
|
|
|
|
|
|
%% @type storedacl() = {acl, {ACLName, Host}, ACLSpec}
|
|
|
|
%% ACLName = atom()
|
|
|
|
%% Host = global | string()
|
|
|
|
%% ACLSpec = aclspec().
|
|
|
|
%% Record in its Mnesia-table-record variant.
|
|
|
|
|
2003-01-29 18:12:23 +01:00
|
|
|
-record(acl, {aclname, aclspec}).
|
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec () -> ok
|
|
|
|
|
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
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (Host, ACLName, ACLSpec) -> storedacl()
|
|
|
|
%% Host = global | string()
|
|
|
|
%% ACLName = atom()
|
|
|
|
%% ACLSpec = aclspec()
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
to_record(Host, ACLName, ACLSpec) ->
|
2008-02-12 02:04:47 +01:00
|
|
|
#acl{aclname = {ACLName, Host}, aclspec = normalize_spec(ACLSpec)}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (Host, ACLName, ACLSpec) -> {atomic, ok} | {aborted, Reason}
|
|
|
|
%% Host = global | string()
|
|
|
|
%% ACLName = atom()
|
|
|
|
%% ACLSpec = all | none | aclspec()
|
|
|
|
%% Reason = term()
|
|
|
|
|
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},
|
2008-02-12 02:04:47 +01:00
|
|
|
aclspec = normalize_spec(ACLSpec)})
|
2003-01-29 18:12:23 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (Host, ACLs, Clear) -> ok | false
|
|
|
|
%% Host = global | string()
|
|
|
|
%% ACLs = [acl()]
|
|
|
|
%% Clear = bool()
|
|
|
|
|
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},
|
2008-02-12 02:04:47 +01:00
|
|
|
aclspec = normalize_spec(ACLSpec)})
|
2003-02-01 21:21:28 +01:00
|
|
|
end
|
|
|
|
end, ACLs)
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, _} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (String) -> Prepd_String
|
|
|
|
%% String = string()
|
|
|
|
%% Prepd_String = string()
|
|
|
|
|
|
|
|
normalize(String) ->
|
|
|
|
exmpp_stringprep:nodeprep(String).
|
|
|
|
|
|
|
|
%% @spec (ACLSpec) -> Normalized_ACLSpec
|
|
|
|
%% ACLSpec = all | none | aclspec()
|
|
|
|
%% Normalized_ACLSpec = aclspec()
|
|
|
|
|
2008-02-12 02:04:47 +01:00
|
|
|
normalize_spec({A, B}) ->
|
|
|
|
{A, normalize(B)};
|
|
|
|
normalize_spec({A, B, C}) ->
|
|
|
|
{A, normalize(B), normalize(C)};
|
|
|
|
normalize_spec(all) ->
|
|
|
|
all;
|
|
|
|
normalize_spec(none) ->
|
|
|
|
none.
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (Host, Rule, JID) -> Access
|
|
|
|
%% Host = global | string()
|
|
|
|
%% Rule = all | none | atom()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Access = allow | deny | atom()
|
|
|
|
|
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
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (ACLs, JID, Host) -> Access
|
|
|
|
%% ACLs = [{Access, ACLName}]
|
|
|
|
%% Access = deny | atom()
|
|
|
|
%% ACLName = atom()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Host = string()
|
|
|
|
|
2007-07-30 13:06:50 +02:00
|
|
|
match_acls([], _, _Host) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
deny;
|
2009-01-22 16:54:03 +01:00
|
|
|
match_acls([{Access, ACLName} | ACLs], JID, Host) ->
|
|
|
|
case match_acl(ACLName, 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.
|
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (ACLName, JID, Host) -> bool()
|
|
|
|
%% ACLName = all | none | atom()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Host = string()
|
|
|
|
|
|
|
|
match_acl(ACLName, JID, Host) ->
|
|
|
|
case ACLName of
|
2003-02-01 21:21:28 +01:00
|
|
|
all -> true;
|
|
|
|
none -> false;
|
|
|
|
_ ->
|
2009-06-01 18:40:51 +02:00
|
|
|
User = exmpp_jid:prep_node_as_list(JID),
|
2009-06-01 18:38:28 +02:00
|
|
|
Server = exmpp_jid:prep_domain_as_list(JID),
|
2009-06-01 18:43:15 +02:00
|
|
|
Resource = exmpp_jid:prep_resource_as_list(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;
|
2008-04-25 11:25:31 +02:00
|
|
|
{resource, R} ->
|
|
|
|
R == Resource;
|
2003-02-01 21:21:28 +01:00
|
|
|
{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);
|
2009-01-19 11:14:04 +01:00
|
|
|
{shared_group, G} ->
|
|
|
|
mod_shared_roster:is_user_in_group({User, Server}, G, Host);
|
|
|
|
{shared_group, G, H} ->
|
|
|
|
mod_shared_roster:is_user_in_group({User, Server}, G, H);
|
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);
|
2008-04-25 11:25:31 +02:00
|
|
|
{resource_regexp, RR} ->
|
|
|
|
is_regexp_match(Resource, RR);
|
2003-02-01 21:21:28 +01:00
|
|
|
{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);
|
2008-04-25 11:25:31 +02:00
|
|
|
{resource_glob, RR} ->
|
|
|
|
is_glob_match(Resource, RR);
|
2003-02-01 21:21:28 +01:00
|
|
|
{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,
|
2009-01-22 16:54:03 +01:00
|
|
|
ets:lookup(acl, {ACLName, global}) ++
|
|
|
|
ets:lookup(acl, {ACLName, Host}))
|
2003-02-01 21:21:28 +01:00
|
|
|
end.
|
2003-01-29 21:21:14 +01:00
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (String, RegExp) -> bool()
|
2009-09-02 16:25:42 +02:00
|
|
|
%% String = string() | undefined
|
2009-01-22 16:54:03 +01:00
|
|
|
%% RegExp = string()
|
|
|
|
|
2009-09-02 16:26:01 +02:00
|
|
|
is_regexp_match(undefined, _RegExp) ->
|
2009-09-02 16:25:42 +02:00
|
|
|
false;
|
2003-01-29 21:21:14 +01:00
|
|
|
is_regexp_match(String, RegExp) ->
|
2009-09-02 16:26:01 +02:00
|
|
|
try re:run(String, RegExp, [{capture, none}]) of
|
2003-01-29 21:21:14 +01:00
|
|
|
nomatch ->
|
|
|
|
false;
|
2009-09-02 16:26:01 +02:00
|
|
|
match ->
|
|
|
|
true
|
|
|
|
catch
|
|
|
|
_:ErrDesc ->
|
2003-01-29 21:21:14 +01:00
|
|
|
?ERROR_MSG(
|
2009-09-02 16:26:01 +02:00
|
|
|
"Wrong regexp ~p in ACL:~n~p",
|
|
|
|
[RegExp, ErrDesc]),
|
2003-01-29 21:21:14 +01:00
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2009-01-22 16:54:03 +01:00
|
|
|
%% @spec (String, Glob) -> bool()
|
2009-09-02 16:25:42 +02:00
|
|
|
%% String = string() | undefined
|
2009-01-22 16:54:03 +01:00
|
|
|
%% Glob = string()
|
|
|
|
|
2003-01-29 21:21:14 +01:00
|
|
|
is_glob_match(String, Glob) ->
|
2009-09-02 16:26:01 +02:00
|
|
|
is_regexp_match(String, xmerl_regexp:sh_to_awk(Glob)).
|
2003-01-29 21:21:14 +01:00
|
|
|
|
|
|
|
|