2003-01-16 21:24:53 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_private.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Support for private storage.
|
|
|
|
%%% Created : 16 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 ProcessOne
|
2007-12-24 13:58:05 +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 13:58:05 +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-16 21:24:53 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%%% Database schema (version / storage / table)
|
|
|
|
%%%
|
|
|
|
%%% 2.1.x / mnesia / private_storage
|
|
|
|
%%% usns = {Username::string(), Host::string(), Namespace::string()}
|
|
|
|
%%% xml = xmlelement()
|
|
|
|
%%%
|
|
|
|
%%% 2.1.x / odbc / private_storage
|
|
|
|
%%% username = varchar250
|
|
|
|
%%% namespace = varchar250
|
|
|
|
%%% data = text
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-prealpha / mnesia / private_storage
|
|
|
|
%%% usns = {Username::binary(), Host::binary(), Namespace::atom()}
|
|
|
|
%%% xml = xmlel()
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-prealpha / odbc / private_storage
|
|
|
|
%%% Same as 2.1.x
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-alpha / mnesia / private_storage
|
|
|
|
%%% user_host_ns = {Username::binary(), Host::binary(), Namespace::atom()}
|
|
|
|
%%% xml = xmlel()
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-alpha / odbc / private_storage
|
|
|
|
%%% user = varchar
|
|
|
|
%%% host = varchar
|
|
|
|
%%% ns = varchar250
|
|
|
|
%%% xml = text
|
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
-module(mod_private).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2004-07-11 22:51:54 +02:00
|
|
|
process_sm_iq/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/2]).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%% TODO: usns instead of user_host_ns requires no migration
|
|
|
|
-record(private_storage, {user_host_ns, xml}).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2011-07-15 02:50:04 +02:00
|
|
|
start(Host, Opts) when is_list(Host) ->
|
|
|
|
start(list_to_binary(Host), Opts);
|
|
|
|
start(HostB, Opts) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2010-07-22 18:44:33 +02:00
|
|
|
Backend = gen_mod:get_opt(backend, Opts, mnesia),
|
|
|
|
gen_storage:create_table(Backend, HostB, private_storage,
|
|
|
|
[{disc_only_copies, [node()]},
|
2011-07-15 02:50:04 +02:00
|
|
|
{odbc_host, HostB},
|
2010-07-22 18:44:33 +02:00
|
|
|
{attributes, record_info(fields, private_storage)},
|
2010-07-22 18:49:12 +02:00
|
|
|
{types, [{user_host_ns, {binary, binary, atom}}, {xml, xmlel}]}]),
|
2011-07-15 02:50:04 +02:00
|
|
|
update_table(HostB, Backend),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_PRIVATE,
|
2004-07-11 22:51:54 +02:00
|
|
|
?MODULE, process_sm_iq, IQDisc).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_PRIVATE).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
process_sm_iq(From, To, #iq{type = Type} = IQ_Rec) ->
|
|
|
|
case check_packet(From, To, IQ_Rec) of
|
|
|
|
ok ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_set(From, To, IQ_Rec);
|
2003-01-16 21:24:53 +01:00
|
|
|
get ->
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_get(From, To, IQ_Rec)
|
2003-01-16 21:24:53 +01:00
|
|
|
end;
|
2008-10-07 14:20:09 +02:00
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_get(From, _To, #iq{payload = SubEl} = IQ_Rec) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
LUser = exmpp_jid:prep_node(From),
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
case catch get_data(LUser,
|
|
|
|
LServer,
|
|
|
|
exmpp_xml:get_child_elements(SubEl)) of
|
|
|
|
{'EXIT', _Reason} ->
|
2011-03-09 14:30:54 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error');
|
2008-10-07 14:20:09 +02:00
|
|
|
Res ->
|
|
|
|
exmpp_iq:result(IQ_Rec, #xmlel{ns = ?NS_PRIVATE,
|
|
|
|
name = 'query',
|
|
|
|
children = Res})
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
process_iq_set(From, _To, #iq{payload = SubEl} = IQ_Rec) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
LUser = exmpp_jid:prep_node(From),
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
|
|
|
set_data(LUser, LServer, El)
|
|
|
|
end, exmpp_xml:get_child_elements(SubEl))
|
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:transaction(LServer, private_storage, F),
|
2008-10-07 14:20:09 +02:00
|
|
|
exmpp_iq:result(IQ_Rec).
|
|
|
|
|
|
|
|
|
|
|
|
check_packet(From, To, IQ_Rec) ->
|
|
|
|
check_packet(From, To, IQ_Rec, [ fun check_domain/3,
|
|
|
|
fun check_user/3,
|
|
|
|
fun check_ns/3]).
|
|
|
|
check_packet(_From, _To, _IQ_Rec, []) ->
|
|
|
|
ok;
|
|
|
|
check_packet(From, To, IQ_Rec, [F | R]) ->
|
|
|
|
case F(From, To, IQ_Rec) of
|
|
|
|
{error, _} = Error -> Error;
|
|
|
|
ok -> check_packet(From, To, IQ_Rec, R)
|
|
|
|
end.
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
check_domain(From, _To, _IQ_Rec) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(From),
|
2010-07-22 18:42:18 +02:00
|
|
|
case ?IS_MY_HOST(LServer) of
|
2008-10-07 14:20:09 +02:00
|
|
|
true -> ok;
|
|
|
|
false -> {error, 'not-allowed'}
|
|
|
|
end.
|
|
|
|
|
|
|
|
% the iq can't be directed to another jid
|
|
|
|
check_user(From, To, _IQ_Rec) ->
|
2009-06-01 18:50:36 +02:00
|
|
|
case exmpp_jid:bare_compare(From, To) of
|
2008-10-07 14:20:09 +02:00
|
|
|
true -> ok;
|
|
|
|
false -> {error, 'forbidden'}
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
%there must be at least one child, and every child should have
|
|
|
|
%a namespace specified (reject if the namespace is jabber:iq:private,
|
|
|
|
%the same than the parent element).
|
|
|
|
check_ns(_From, _To, #iq{payload = SubEl}) ->
|
|
|
|
case exmpp_xml:get_child_elements(SubEl) of
|
|
|
|
[] ->
|
|
|
|
{error, 'not-acceptable'};
|
|
|
|
Children ->
|
|
|
|
case lists:any(fun(Child) ->
|
|
|
|
exmpp_xml:get_ns_as_atom(Child) =:= ?NS_PRIVATE
|
|
|
|
end, Children) of
|
|
|
|
true -> {error, 'not-acceptable'};
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%% The xml is stored as xmlel() in mnesia, but as text in odbc
|
2008-10-07 14:20:09 +02:00
|
|
|
set_data(LUser, LServer, El) ->
|
|
|
|
XMLNS = exmpp_xml:get_ns_as_atom(El),
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:write(LServer,
|
2010-07-22 18:49:12 +02:00
|
|
|
#private_storage{user_host_ns = {LUser, LServer, XMLNS},
|
2010-07-22 18:44:33 +02:00
|
|
|
xml = El}).
|
2008-10-07 14:20:09 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(LUser, LServer, Els) ->
|
|
|
|
get_data(LUser, LServer, Els, []).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(_LUser, _LServer, [], Res) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
lists:reverse(Res);
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(LUser, LServer, [El | Els], Res) ->
|
2008-10-07 14:20:09 +02:00
|
|
|
XMLNS = exmpp_xml:get_ns_as_atom(El),
|
2010-07-22 18:44:33 +02:00
|
|
|
case gen_storage:dirty_read(LServer, private_storage, {LUser, LServer, XMLNS}) of
|
2008-10-07 14:20:09 +02:00
|
|
|
[R] ->
|
|
|
|
get_data(LUser, LServer, Els,
|
|
|
|
[R#private_storage.xml | Res]);
|
|
|
|
[] ->
|
|
|
|
get_data(LUser, LServer, Els,
|
|
|
|
[El | Res])
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
remove_user(User, Server)
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-10-07 14:20:09 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
F = fun() ->
|
2010-07-22 18:44:33 +02:00
|
|
|
Records = gen_storage:select(LServer, private_storage,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host_ns, {LUser, LServer, '_'}}]),
|
2010-07-22 18:44:33 +02:00
|
|
|
lists:foreach(
|
2010-07-22 18:49:12 +02:00
|
|
|
fun(#private_storage{user_host_ns = USNS}) ->
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:delete(LServer, {private_storage, USNS})
|
|
|
|
end, Records)
|
2008-10-07 14:20:09 +02:00
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:transaction(LServer, private_storage, F)
|
2008-10-07 14:20:09 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2011-07-15 02:50:04 +02:00
|
|
|
update_table(global, Storage) ->
|
|
|
|
[update_table(HostB, Storage) || HostB <- ejabberd_hosts:get_hosts(ejabberd)];
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
update_table(Host, mnesia) ->
|
|
|
|
gen_storage_migration:migrate_mnesia(
|
|
|
|
Host, private_storage,
|
|
|
|
[{private_storage, [usns, xml],
|
|
|
|
fun({private_storage, {User, Server, NS}, Xml}) ->
|
|
|
|
U1 = list_to_binary(User),
|
|
|
|
S1 = list_to_binary(Server),
|
|
|
|
NS1 = list_to_atom(NS),
|
|
|
|
El1 = exmpp_xml:xmlelement_to_xmlel(Xml, [?NS_PRIVATE],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
#private_storage{user_host_ns = {U1, S1, NS1},
|
|
|
|
xml = El1}
|
|
|
|
end}]);
|
|
|
|
|
|
|
|
update_table(Host, odbc) ->
|
|
|
|
gen_storage_migration:migrate_odbc(
|
|
|
|
Host, [private_storage],
|
|
|
|
[{"private_storage", ["username", "namespace", "data"],
|
|
|
|
fun(_, Username, Namespace, Data) ->
|
|
|
|
[#private_storage{user_host_ns = {Username, Host, Namespace},
|
|
|
|
xml = Data}]
|
|
|
|
end}]).
|