2009-08-07 11:52:45 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_piefxis.erl
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% Author : Pablo Polvorin, Vidal Santiago Martinez, Evgeniy Khramtsov
|
2009-08-07 11:52:45 +02:00
|
|
|
%%% Purpose : XEP-0227: Portable Import/Export Format for XMPP-IM Servers
|
|
|
|
%%% Created : 17 Jul 2008 by Pablo Polvorin <pablo.polvorin@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 ProcessOne
|
2009-08-07 11:52:45 +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.
|
|
|
|
%%%
|
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.
|
2009-08-07 11:52:45 +02:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%% Not implemented:
|
2021-09-22 12:53:18 +02:00
|
|
|
%%% - PEP nodes export/import
|
|
|
|
%%% - message archives export/import
|
2009-08-07 11:52:45 +02:00
|
|
|
%%% - write mod_piefxis with ejabberdctl commands
|
|
|
|
%%% - Other schemas of XInclude are not tested, and may not be imported correctly.
|
|
|
|
%%% - If a host has many users, split that host in XML files with 50 users each.
|
|
|
|
|
|
|
|
-module(ejabberd_piefxis).
|
|
|
|
|
2024-11-08 19:27:22 +01:00
|
|
|
-protocol({xep, 227, '1.1', '2.1.0', "partial", ""}).
|
2015-05-21 17:02:36 +02:00
|
|
|
|
2017-04-28 12:23:32 +02:00
|
|
|
-export([import_file/1, export_server/1, export_host/2]).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(CHUNK_SIZE, 1024*20). %20k
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/scram.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
-include("mod_privacy.hrl").
|
|
|
|
-include("mod_roster.hrl").
|
2009-08-07 11:52:45 +02:00
|
|
|
|
|
|
|
%%-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
%%-include_lib("exmpp/include/exmpp_client.hrl").
|
|
|
|
%% Copied from exmpp header files:
|
|
|
|
%% Copied from mod_private.erl
|
|
|
|
%%-define(ERROR_MSG(M,Args),io:format(M,Args)).
|
|
|
|
%%-define(INFO_MSG(M,Args),ok).
|
|
|
|
%%%==================================
|
|
|
|
%%%% Import file
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(NS_PIE, <<"urn:xmpp:pie:0">>).
|
|
|
|
-define(NS_PIEFXIS, <<"http://www.xmpp.org/extensions/xep-0227.html#ns">>).
|
|
|
|
-define(NS_XI, <<"http://www.w3.org/2001/XInclude">>).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-record(state, {xml_stream_state :: fxml_stream:xml_stream_state() | undefined,
|
2013-03-14 10:33:02 +01:00
|
|
|
user = <<"">> :: binary(),
|
|
|
|
server = <<"">> :: binary(),
|
2017-02-18 07:36:27 +01:00
|
|
|
fd = self() :: file:io_device(),
|
2013-03-14 10:33:02 +01:00
|
|
|
dir = <<"">> :: binary()}).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-type state() :: #state{}.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
|
|
|
%%File could be large.. we read it in chunks
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
import_file(FileName) ->
|
|
|
|
import_file(FileName, #state{}).
|
|
|
|
|
|
|
|
-spec import_file(binary(), state()) -> ok | {error, atom()}.
|
|
|
|
import_file(FileName, State) ->
|
|
|
|
case file:open(FileName, [read, binary]) of
|
|
|
|
{ok, Fd} ->
|
|
|
|
Dir = filename:dirname(FileName),
|
2016-02-03 19:03:17 +01:00
|
|
|
XMLStreamState = fxml_stream:new(self(), infinity),
|
2013-03-14 10:33:02 +01:00
|
|
|
Res = process(State#state{xml_stream_state = XMLStreamState,
|
|
|
|
fd = Fd,
|
|
|
|
dir = Dir}),
|
|
|
|
file:close(Fd),
|
|
|
|
Res;
|
|
|
|
{error, Reason} ->
|
|
|
|
ErrTxt = file:format_error(Reason),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to open file '~ts': ~ts", [FileName, ErrTxt]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Reason}
|
2009-08-07 11:52:45 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec export_server(binary()) -> any().
|
|
|
|
export_server(Dir) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
export_hosts(ejabberd_option:hosts(), Dir).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec export_host(binary(), binary()) -> any().
|
|
|
|
export_host(Dir, Host) ->
|
|
|
|
export_hosts([Host], Dir).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
export_hosts(Hosts, Dir) ->
|
|
|
|
FnT = make_filename_template(),
|
|
|
|
DFn = make_main_basefilename(Dir, FnT),
|
|
|
|
case file:open(DFn, [raw, write]) of
|
|
|
|
{ok, Fd} ->
|
|
|
|
print(Fd, make_piefxis_xml_head()),
|
|
|
|
print(Fd, make_piefxis_server_head()),
|
|
|
|
FilesAndHosts = [{make_host_filename(FnT, Host), Host}
|
|
|
|
|| Host <- Hosts],
|
|
|
|
lists:foreach(
|
|
|
|
fun({FnH, _}) ->
|
|
|
|
print(Fd, make_xinclude(FnH))
|
|
|
|
end, FilesAndHosts),
|
|
|
|
print(Fd, make_piefxis_server_tail()),
|
|
|
|
print(Fd, make_piefxis_xml_tail()),
|
|
|
|
file:close(Fd),
|
|
|
|
lists:foldl(
|
|
|
|
fun({FnH, Host}, ok) ->
|
|
|
|
export_host(Dir, FnH, Host);
|
|
|
|
(_, Err) ->
|
|
|
|
Err
|
|
|
|
end, ok, FilesAndHosts);
|
|
|
|
{error, Reason} ->
|
|
|
|
ErrTxt = file:format_error(Reason),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to open file '~ts': ~ts", [DFn, ErrTxt]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Reason}
|
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
export_host(Dir, FnH, Host) ->
|
|
|
|
DFn = make_host_basefilename(Dir, FnH),
|
|
|
|
case file:open(DFn, [raw, write]) of
|
|
|
|
{ok, Fd} ->
|
|
|
|
print(Fd, make_piefxis_xml_head()),
|
|
|
|
print(Fd, make_piefxis_host_head(Host)),
|
2017-05-11 13:37:21 +02:00
|
|
|
Users = ejabberd_auth:get_users(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
case export_users(Users, Host, Fd) of
|
|
|
|
ok ->
|
|
|
|
print(Fd, make_piefxis_host_tail()),
|
|
|
|
print(Fd, make_piefxis_xml_tail()),
|
|
|
|
file:close(Fd),
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
file:close(Fd),
|
|
|
|
file:delete(DFn),
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
|
|
|
ErrTxt = file:format_error(Reason),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to open file '~ts': ~ts", [DFn, ErrTxt]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Reason}
|
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
export_users([{User, _S}|Users], Server, Fd) ->
|
|
|
|
case export_user(User, Server, Fd) of
|
|
|
|
ok ->
|
|
|
|
export_users(Users, Server, Fd);
|
|
|
|
Err ->
|
|
|
|
Err
|
2009-08-07 11:52:45 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
export_users([], _Server, _Fd) ->
|
2009-08-07 11:52:45 +02:00
|
|
|
ok.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
export_user(User, Server, Fd) ->
|
2015-02-05 16:44:08 +01:00
|
|
|
Password = ejabberd_auth:get_password_s(User, Server),
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2021-09-22 12:53:18 +02:00
|
|
|
{PassPlain, PassScram} = case ejabberd_auth:password_format(LServer) of
|
|
|
|
scram -> {[], [format_scram_password(Password)]};
|
2021-11-08 13:43:18 +01:00
|
|
|
_ when Password == <<"">> -> {[], []};
|
2021-09-22 12:53:18 +02:00
|
|
|
_ -> {[{<<"password">>, Password}], []}
|
2018-12-19 12:33:46 +01:00
|
|
|
end,
|
2021-09-22 12:53:18 +02:00
|
|
|
Els =
|
|
|
|
PassScram ++
|
|
|
|
get_offline(User, Server) ++
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vcard(User, Server) ++
|
|
|
|
get_privacy(User, Server) ++
|
|
|
|
get_roster(User, Server) ++
|
|
|
|
get_private(User, Server),
|
2016-02-03 19:03:17 +01:00
|
|
|
print(Fd, fxml:element_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"user">>,
|
2021-09-22 12:53:18 +02:00
|
|
|
attrs = [{<<"name">>, User} | PassPlain],
|
2013-03-14 10:33:02 +01:00
|
|
|
children = Els})).
|
|
|
|
|
2020-12-08 12:06:52 +01:00
|
|
|
format_scram_password(#scram{hash = Hash, storedkey = StoredKey, serverkey = ServerKey,
|
2018-12-19 12:33:46 +01:00
|
|
|
salt = Salt, iterationcount = IterationCount}) ->
|
2021-09-22 12:53:18 +02:00
|
|
|
StoredKeyB64 = base64:encode(StoredKey),
|
|
|
|
ServerKeyB64 = base64:encode(ServerKey),
|
|
|
|
SaltB64 = base64:encode(Salt),
|
|
|
|
IterationCountBin = (integer_to_binary(IterationCount)),
|
|
|
|
MechanismB = case Hash of
|
|
|
|
sha -> <<"SCRAM-SHA-1">>;
|
|
|
|
sha256 -> <<"SCRAM-SHA-256">>;
|
|
|
|
sha512 -> <<"SCRAM-SHA-512">>
|
|
|
|
end,
|
|
|
|
Children =
|
|
|
|
[
|
|
|
|
#xmlel{name = <<"iter-count">>,
|
|
|
|
children = [{xmlcdata, IterationCountBin}]},
|
|
|
|
#xmlel{name = <<"salt">>,
|
|
|
|
children = [{xmlcdata, SaltB64}]},
|
|
|
|
#xmlel{name = <<"server-key">>,
|
|
|
|
children = [{xmlcdata, ServerKeyB64}]},
|
|
|
|
#xmlel{name = <<"stored-key">>,
|
|
|
|
children = [{xmlcdata, StoredKeyB64}]}
|
|
|
|
],
|
|
|
|
#xmlel{name = <<"scram-credentials">>,
|
|
|
|
attrs = [{<<"xmlns">>, <<?NS_PIE/binary, "#scram">>},
|
|
|
|
{<<"mechanism">>, MechanismB}],
|
|
|
|
children = Children}.
|
|
|
|
|
|
|
|
parse_scram_password(#xmlel{attrs = Attrs} = El) ->
|
|
|
|
Hash = case fxml:get_attr_s(<<"mechanism">>, Attrs) of
|
|
|
|
<<"SCRAM-SHA-1">> -> sha;
|
|
|
|
<<"SCRAM-SHA-256">> -> sha256;
|
|
|
|
<<"SCRAM-SHA-512">> -> sha512
|
|
|
|
end,
|
|
|
|
StoredKeyB64 = fxml:get_path_s(El, [{elem, <<"stored-key">>}, cdata]),
|
|
|
|
ServerKeyB64 = fxml:get_path_s(El, [{elem, <<"server-key">>}, cdata]),
|
|
|
|
IterationCountBin = fxml:get_path_s(El, [{elem, <<"iter-count">>}, cdata]),
|
|
|
|
SaltB64 = fxml:get_path_s(El, [{elem, <<"salt">>}, cdata]),
|
|
|
|
#scram{
|
|
|
|
storedkey = base64:decode(StoredKeyB64),
|
|
|
|
serverkey = base64:decode(ServerKeyB64),
|
|
|
|
salt = base64:decode(SaltB64),
|
|
|
|
hash = Hash,
|
|
|
|
iterationcount = (binary_to_integer(IterationCountBin))
|
|
|
|
};
|
2015-02-05 16:44:08 +01:00
|
|
|
|
|
|
|
parse_scram_password(PassData) ->
|
|
|
|
Split = binary:split(PassData, <<",">>, [global]),
|
2020-12-08 12:06:52 +01:00
|
|
|
[Hash, StoredKeyB64, ServerKeyB64, SaltB64, IterationCountBin] =
|
|
|
|
case Split of
|
|
|
|
[K1, K2, K3, K4] -> [sha, K1, K2, K3, K4];
|
|
|
|
[<<"sha256">>, K1, K2, K3, K4] -> [sha256, K1, K2, K3, K4];
|
|
|
|
[<<"sha512">>, K1, K2, K3, K4] -> [sha512, K1, K2, K3, K4]
|
|
|
|
end,
|
2015-02-05 16:44:08 +01:00
|
|
|
#scram{
|
2020-02-11 12:48:19 +01:00
|
|
|
storedkey = base64:decode(StoredKeyB64),
|
|
|
|
serverkey = base64:decode(ServerKeyB64),
|
|
|
|
salt = base64:decode(SaltB64),
|
2020-12-08 12:06:52 +01:00
|
|
|
hash = Hash,
|
2016-11-24 13:06:06 +01:00
|
|
|
iterationcount = (binary_to_integer(IterationCountBin))
|
2015-02-05 16:44:08 +01:00
|
|
|
}.
|
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec get_vcard(binary(), binary()) -> [xmlel()].
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vcard(User, Server) ->
|
2016-08-04 12:37:07 +02:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2021-09-22 12:11:25 +02:00
|
|
|
try mod_vcard:get_vcard(LUser, LServer) of
|
2016-08-04 12:37:07 +02:00
|
|
|
error -> [];
|
|
|
|
Els -> Els
|
2021-09-22 12:11:25 +02:00
|
|
|
catch
|
|
|
|
error:{module_not_loaded, _, _} -> []
|
2009-08-07 11:52:45 +02:00
|
|
|
end.
|
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec get_offline(binary(), binary()) -> [xmlel()].
|
2013-03-14 10:33:02 +01:00
|
|
|
get_offline(User, Server) ->
|
2016-08-04 12:37:07 +02:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2021-09-22 12:11:25 +02:00
|
|
|
try mod_offline:get_offline_els(LUser, LServer) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
[];
|
|
|
|
Els ->
|
2016-08-04 12:37:07 +02:00
|
|
|
NewEls = lists:map(fun xmpp:encode/1, Els),
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"offline-messages">>, children = NewEls}]
|
2021-09-22 12:11:25 +02:00
|
|
|
catch
|
|
|
|
error:{module_not_loaded, _, _} -> []
|
2013-03-14 10:33:02 +01:00
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec get_privacy(binary(), binary()) -> [xmlel()].
|
2013-03-14 10:33:02 +01:00
|
|
|
get_privacy(User, Server) ->
|
2021-09-22 12:11:25 +02:00
|
|
|
try mod_privacy:get_user_lists(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, #privacy{default = Default,
|
|
|
|
lists = [_|_] = Lists}} ->
|
|
|
|
XLists = lists:map(
|
|
|
|
fun({Name, Items}) ->
|
|
|
|
XItems = lists:map(
|
2016-08-04 12:37:07 +02:00
|
|
|
fun mod_privacy:encode_list_item/1,
|
|
|
|
Items),
|
|
|
|
#privacy_list{name = Name, items = XItems}
|
2013-03-14 10:33:02 +01:00
|
|
|
end, Lists),
|
2016-08-04 12:37:07 +02:00
|
|
|
[xmpp:encode(#privacy_query{default = Default, lists = XLists})];
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
2021-09-22 12:11:25 +02:00
|
|
|
catch
|
|
|
|
error:{module_not_loaded, _, _} -> []
|
2013-03-14 10:33:02 +01:00
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec get_roster(binary(), binary()) -> [xmlel()].
|
2013-03-14 10:33:02 +01:00
|
|
|
get_roster(User, Server) ->
|
2017-02-25 08:01:01 +01:00
|
|
|
JID = jid:make(User, Server),
|
2021-09-22 12:11:25 +02:00
|
|
|
try mod_roster:get_roster(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[_|_] = Items ->
|
|
|
|
Subs =
|
|
|
|
lists:flatmap(
|
|
|
|
fun(#roster{ask = Ask,
|
|
|
|
askmessage = Msg} = R)
|
|
|
|
when Ask == in; Ask == both ->
|
|
|
|
Status = if is_binary(Msg) -> (Msg);
|
|
|
|
true -> <<"">>
|
|
|
|
end,
|
2016-08-04 12:37:07 +02:00
|
|
|
[xmpp:encode(
|
|
|
|
#presence{from = jid:make(R#roster.jid),
|
|
|
|
to = JID,
|
|
|
|
type = subscribe,
|
|
|
|
status = xmpp:mk_text(Status)})];
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Items),
|
|
|
|
Rs = lists:flatmap(
|
|
|
|
fun(#roster{ask = in, subscription = none}) ->
|
|
|
|
[];
|
|
|
|
(R) ->
|
2016-08-04 12:37:07 +02:00
|
|
|
[mod_roster:encode_item(R)]
|
2013-03-14 10:33:02 +01:00
|
|
|
end, Items),
|
2016-08-04 12:37:07 +02:00
|
|
|
[xmpp:encode(#roster_query{items = Rs}) | Subs];
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
2021-09-22 12:11:25 +02:00
|
|
|
catch
|
|
|
|
error:{module_not_loaded, _, _} -> []
|
2013-03-14 10:33:02 +01:00
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec get_private(binary(), binary()) -> [xmlel()].
|
2013-03-14 10:33:02 +01:00
|
|
|
get_private(User, Server) ->
|
2021-09-22 12:11:25 +02:00
|
|
|
try mod_private:get_data(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[_|_] = Els ->
|
2018-01-25 18:02:47 +01:00
|
|
|
[xmpp:encode(#private{sub_els = Els})];
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
2021-09-22 12:11:25 +02:00
|
|
|
catch
|
|
|
|
error:{module_not_loaded, _, _} -> []
|
2013-03-14 10:33:02 +01:00
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process(#state{xml_stream_state = XMLStreamState, fd = Fd} = State) ->
|
|
|
|
case file:read(Fd, ?CHUNK_SIZE) of
|
|
|
|
{ok, Data} ->
|
2016-02-03 19:03:17 +01:00
|
|
|
NewXMLStreamState = fxml_stream:parse(XMLStreamState, Data),
|
2013-03-14 10:33:02 +01:00
|
|
|
case process_els(State#state{xml_stream_state =
|
|
|
|
NewXMLStreamState}) of
|
|
|
|
{ok, NewState} ->
|
|
|
|
process(NewState);
|
|
|
|
Err ->
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml_stream:close(NewXMLStreamState),
|
2013-03-14 10:33:02 +01:00
|
|
|
Err
|
|
|
|
end;
|
|
|
|
eof ->
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml_stream:close(XMLStreamState),
|
2013-03-14 10:33:02 +01:00
|
|
|
ok
|
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_els(State) ->
|
2020-02-18 15:02:14 +01:00
|
|
|
Els = gather_els(State, []),
|
|
|
|
process_els(State, lists:reverse(Els)).
|
|
|
|
|
|
|
|
gather_els(State, List) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
receive
|
|
|
|
{'$gen_event', El} ->
|
2020-02-18 15:02:14 +01:00
|
|
|
gather_els(State, [El | List])
|
2013-03-14 10:33:02 +01:00
|
|
|
after 0 ->
|
2020-02-18 15:02:14 +01:00
|
|
|
List
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_els(State, [El | Tail]) ->
|
|
|
|
case process_el(El, State) of
|
|
|
|
{ok, NewState} ->
|
|
|
|
process_els(NewState, Tail);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
process_els(State, []) ->
|
|
|
|
{ok, State}.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_el({xmlstreamstart, <<"server-data">>, Attrs}, State) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_PIEFXIS ->
|
|
|
|
{ok, State};
|
|
|
|
?NS_PIE ->
|
|
|
|
{ok, State};
|
|
|
|
NS ->
|
2019-09-23 14:17:20 +02:00
|
|
|
stop("Unknown 'server-data' namespace = ~ts", [NS])
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
process_el({xmlstreamend, _}, State) ->
|
|
|
|
{ok, State};
|
|
|
|
process_el({xmlstreamcdata, _}, State) ->
|
|
|
|
{ok, State};
|
|
|
|
process_el({xmlstreamelement, #xmlel{name = <<"xi:include">>,
|
|
|
|
attrs = Attrs}},
|
|
|
|
#state{dir = Dir, user = <<"">>} = State) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
FileName = fxml:get_attr_s(<<"href">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
case import_file(filename:join([Dir, FileName]), State) of
|
|
|
|
ok ->
|
|
|
|
{ok, State};
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
process_el({xmlstreamstart, <<"host">>, Attrs}, State) ->
|
|
|
|
process_el({xmlstreamelement, #xmlel{name = <<"host">>,
|
|
|
|
attrs = Attrs}}, State);
|
|
|
|
process_el({xmlstreamelement, #xmlel{name = <<"host">>,
|
|
|
|
attrs = Attrs,
|
|
|
|
children = Els}}, State) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
JIDS = fxml:get_attr_s(<<"jid">>, Attrs),
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(JIDS) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#jid{lserver = S} ->
|
2016-12-29 22:00:36 +01:00
|
|
|
case ejabberd_router:is_my_host(S) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
process_users(Els, State#state{server = S});
|
|
|
|
false ->
|
2019-09-23 14:17:20 +02:00
|
|
|
stop("Unknown host: ~ts", [S])
|
2017-02-26 08:07:12 +01:00
|
|
|
end
|
|
|
|
catch _:{bad_jid, _} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
stop("Invalid 'jid': ~ts", [JIDS])
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
process_el({xmlstreamstart, <<"user">>, Attrs}, State = #state{server = S})
|
|
|
|
when S /= <<"">> ->
|
|
|
|
process_el({xmlstreamelement, #xmlel{name = <<"user">>, attrs = Attrs}},
|
|
|
|
State);
|
|
|
|
process_el({xmlstreamelement, #xmlel{name = <<"user">>} = El},
|
|
|
|
State = #state{server = S}) when S /= <<"">> ->
|
|
|
|
process_user(El, State);
|
|
|
|
process_el({xmlstreamelement, El}, State = #state{server = S, user = U})
|
|
|
|
when S /= <<"">>, U /= <<"">> ->
|
|
|
|
process_user_el(El, State);
|
|
|
|
process_el({xmlstreamelement, El}, _State) ->
|
|
|
|
stop("Unexpected tag: ~p", [El]);
|
|
|
|
process_el({xmlstreamstart, El, Attrs}, _State) ->
|
|
|
|
stop("Unexpected payload: ~p", [{El, Attrs}]);
|
|
|
|
process_el({xmlstreamerror, Err}, _State) ->
|
|
|
|
stop("Failed to process element = ~p", [Err]).
|
|
|
|
|
|
|
|
process_users([#xmlel{} = El|Els], State) ->
|
|
|
|
case process_user(El, State) of
|
|
|
|
{ok, NewState} ->
|
|
|
|
process_users(Els, NewState);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
process_users([_|Els], State) ->
|
|
|
|
process_users(Els, State);
|
|
|
|
process_users([], State) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
2021-09-22 12:53:18 +02:00
|
|
|
process_user(#xmlel{name = <<"user">>, attrs = Attrs, children = Els} = El,
|
2013-03-14 10:33:02 +01:00
|
|
|
#state{server = LServer} = State) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Name = fxml:get_attr_s(<<"name">>, Attrs),
|
2021-09-22 12:53:18 +02:00
|
|
|
Pass = process_password(El, LServer),
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nodeprep(Name) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error ->
|
2019-09-23 14:17:20 +02:00
|
|
|
stop("Invalid 'user': ~ts", [Name]);
|
2013-03-14 10:33:02 +01:00
|
|
|
LUser ->
|
|
|
|
case ejabberd_auth:try_register(LUser, LServer, Pass) of
|
2017-05-11 13:37:21 +02:00
|
|
|
ok ->
|
2013-03-14 10:33:02 +01:00
|
|
|
process_user_els(Els, State#state{user = LUser});
|
2021-09-22 12:53:18 +02:00
|
|
|
{error, invalid_password} when (Pass == <<>>) ->
|
2018-05-07 13:02:17 +02:00
|
|
|
process_user_els(Els, State#state{user = LUser});
|
2017-05-11 13:37:21 +02:00
|
|
|
{error, Err} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
stop("Failed to create user '~ts': ~p", [Name, Err])
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2021-09-22 12:53:18 +02:00
|
|
|
process_password(#xmlel{name = <<"user">>, attrs = Attrs} = El, LServer) ->
|
|
|
|
{PassPlain, PassOldScram} = case fxml:get_attr_s(<<"password">>, Attrs) of
|
|
|
|
<<"scram:", PassData/binary>> -> {<<"">>, PassData};
|
|
|
|
P -> {P, false}
|
|
|
|
end,
|
|
|
|
ScramCred = fxml:get_subtag(El, <<"scram-credentials">>),
|
|
|
|
PasswordFormat = ejabberd_auth:password_format(LServer),
|
|
|
|
case {PassPlain, PassOldScram, ScramCred, PasswordFormat} of
|
|
|
|
{PassPlain, false, false, plain} -> PassPlain;
|
|
|
|
{<<"">>, false, ScramCred, plain} -> parse_scram_password(ScramCred);
|
|
|
|
{<<"">>, PassOldScram, false, plain} -> parse_scram_password(PassOldScram);
|
|
|
|
{PassPlain, false, false, scram} -> PassPlain;
|
|
|
|
{<<"">>, false, ScramCred, scram} -> parse_scram_password(ScramCred);
|
|
|
|
{<<"">>, PassOldScram, false, scram} -> parse_scram_password(PassOldScram)
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_user_els([#xmlel{} = El|Els], State) ->
|
|
|
|
case process_user_el(El, State) of
|
|
|
|
{ok, NewState} ->
|
|
|
|
process_user_els(Els, NewState);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
process_user_els([_|Els], State) ->
|
|
|
|
process_user_els(Els, State);
|
|
|
|
process_user_els([], State) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
process_user_el(#xmlel{name = Name, attrs = Attrs, children = Els} = El,
|
|
|
|
State) ->
|
2016-08-04 12:37:07 +02:00
|
|
|
try
|
|
|
|
case {Name, fxml:get_attr_s(<<"xmlns">>, Attrs)} of
|
|
|
|
{<<"query">>, ?NS_ROSTER} ->
|
|
|
|
process_roster(xmpp:decode(El), State);
|
|
|
|
{<<"query">>, ?NS_PRIVACY} ->
|
|
|
|
%% Make sure <list/> elements go before <active/> and <default/>
|
|
|
|
process_privacy(xmpp:decode(El), State);
|
|
|
|
{<<"query">>, ?NS_PRIVATE} ->
|
|
|
|
process_private(xmpp:decode(El), State);
|
|
|
|
{<<"vCard">>, ?NS_VCARD} ->
|
2018-07-11 11:38:17 +02:00
|
|
|
process_vcard(xmpp:decode(El), State);
|
2016-09-24 13:17:21 +02:00
|
|
|
{<<"offline-messages">>, NS} ->
|
|
|
|
Msgs = [xmpp:decode(E, NS, [ignore_els]) || E <- Els],
|
2016-08-04 12:37:07 +02:00
|
|
|
process_offline_msgs(Msgs, State);
|
2016-09-24 13:17:21 +02:00
|
|
|
{<<"presence">>, ?NS_CLIENT} ->
|
|
|
|
process_presence(xmpp:decode(El, ?NS_CLIENT, [ignore_els]), State);
|
2016-08-04 12:37:07 +02:00
|
|
|
_ ->
|
|
|
|
{ok, State}
|
|
|
|
end
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
ErrTxt = xmpp:format_error(Why),
|
2019-09-23 14:17:20 +02:00
|
|
|
stop("failed to decode XML '~ts': ~ts",
|
2016-08-04 12:37:07 +02:00
|
|
|
[fxml:element_to_binary(El), ErrTxt])
|
2013-03-14 10:33:02 +01:00
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec process_offline_msgs([stanza()], state()) -> {ok, state()} | {error, _}.
|
|
|
|
process_offline_msgs([#message{} = Msg|Msgs], State) ->
|
|
|
|
case process_offline_msg(Msg, State) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, NewState} ->
|
2016-08-04 12:37:07 +02:00
|
|
|
process_offline_msgs(Msgs, NewState);
|
2013-03-14 10:33:02 +01:00
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
2016-08-04 12:37:07 +02:00
|
|
|
process_offline_msgs([_|Msgs], State) ->
|
|
|
|
process_offline_msgs(Msgs, State);
|
2013-03-14 10:33:02 +01:00
|
|
|
process_offline_msgs([], State) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec process_roster(roster_query(), state()) -> {ok, state()} | {error, _}.
|
|
|
|
process_roster(RosterQuery, State = #state{user = U, server = S}) ->
|
|
|
|
case mod_roster:set_items(U, S, RosterQuery) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, _} ->
|
|
|
|
{ok, State};
|
|
|
|
Err ->
|
|
|
|
stop("Failed to write roster: ~p", [Err])
|
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec process_privacy(privacy_query(), state()) -> {ok, state()} | {error, _}.
|
|
|
|
process_privacy(#privacy_query{lists = Lists,
|
|
|
|
default = Default,
|
2018-07-11 12:02:09 +02:00
|
|
|
active = Active},
|
2016-08-04 12:37:07 +02:00
|
|
|
State = #state{user = U, server = S}) ->
|
|
|
|
JID = jid:make(U, S),
|
2018-07-11 12:02:09 +02:00
|
|
|
if Lists /= undefined ->
|
|
|
|
process_privacy2(JID, #privacy_query{lists = Lists});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if Active /= undefined ->
|
|
|
|
process_privacy2(JID, #privacy_query{active = Active});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if Default /= undefined ->
|
|
|
|
process_privacy2(JID, #privacy_query{default = Default});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
process_privacy2(JID, PQ) ->
|
|
|
|
case mod_privacy:process_iq(#iq{type = set, id = p1_rand:get_string(),
|
|
|
|
from = JID, to = JID,
|
|
|
|
sub_els = [PQ]}) of
|
|
|
|
#iq{type = error} = ResIQ ->
|
|
|
|
#stanza_error{reason = Reason} = xmpp:get_error(ResIQ),
|
|
|
|
if Reason /= 'item-not-found' ->
|
2016-08-04 12:37:07 +02:00
|
|
|
%% Failed to set default list because there is no
|
|
|
|
%% list with such name. We shouldn't stop here.
|
2018-07-11 12:02:09 +02:00
|
|
|
stop("Failed to write default privacy: ~p", [Reason]);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2009-11-23 19:34:36 +01:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec process_private(private(), state()) -> {ok, state()} | {error, _}.
|
|
|
|
process_private(Private, State = #state{user = U, server = S}) ->
|
|
|
|
JID = jid:make(U, S),
|
2018-07-05 10:51:49 +02:00
|
|
|
IQ = #iq{type = set, id = p1_rand:get_string(),
|
2016-08-04 12:37:07 +02:00
|
|
|
from = JID, to = JID, sub_els = [Private]},
|
|
|
|
case mod_private:process_sm_iq(IQ) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{type = result} ->
|
|
|
|
{ok, State};
|
|
|
|
Err ->
|
|
|
|
stop("Failed to write private: ~p", [Err])
|
|
|
|
end.
|
2009-11-23 19:34:36 +01:00
|
|
|
|
2019-06-27 10:32:54 +02:00
|
|
|
-spec process_vcard(xmpp_element(), state()) -> {ok, state()} | {error, _}.
|
2013-03-14 10:33:02 +01:00
|
|
|
process_vcard(El, State = #state{user = U, server = S}) ->
|
2016-08-04 12:37:07 +02:00
|
|
|
JID = jid:make(U, S),
|
2018-07-05 10:51:49 +02:00
|
|
|
IQ = #iq{type = set, id = p1_rand:get_string(),
|
2016-08-04 12:37:07 +02:00
|
|
|
from = JID, to = JID, sub_els = [El]},
|
|
|
|
case mod_vcard:process_sm_iq(IQ) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{type = result} ->
|
|
|
|
{ok, State};
|
|
|
|
Err ->
|
|
|
|
stop("Failed to write vcard: ~p", [Err])
|
|
|
|
end.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec process_offline_msg(message(), state()) -> {ok, state()} | {error, _}.
|
|
|
|
process_offline_msg(#message{from = undefined}, _State) ->
|
|
|
|
stop("No 'from' attribute found", []);
|
|
|
|
process_offline_msg(Msg, State = #state{user = U, server = S}) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
To = jid:make(U, S),
|
|
|
|
ejabberd_hooks:run_fold(
|
2017-02-23 16:55:35 +01:00
|
|
|
offline_message_hook, To#jid.lserver, {pass, xmpp:set_to(Msg, To)}, []),
|
2017-02-16 09:00:26 +01:00
|
|
|
{ok, State}.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2016-08-04 12:37:07 +02:00
|
|
|
-spec process_presence(presence(), state()) -> {ok, state()} | {error, _}.
|
|
|
|
process_presence(#presence{from = undefined}, _State) ->
|
|
|
|
stop("No 'from' attribute found", []);
|
|
|
|
process_presence(Pres, #state{user = U, server = S} = State) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
To = jid:make(U, S),
|
|
|
|
NewPres = xmpp:set_to(Pres, To),
|
|
|
|
ejabberd_router:route(NewPres),
|
2016-08-04 12:37:07 +02:00
|
|
|
{ok, State}.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
stop(Fmt, Args) ->
|
|
|
|
?ERROR_MSG(Fmt, Args),
|
|
|
|
{error, import_failed}.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
make_filename_template() ->
|
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
|
2016-11-24 13:06:06 +01:00
|
|
|
str:format("~4..0w~2..0w~2..0w-~2..0w~2..0w~2..0w",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second]).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
make_main_basefilename(Dir, FnT) ->
|
|
|
|
Filename2 = <<FnT/binary, ".xml">>,
|
|
|
|
filename:join([Dir, Filename2]).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
%% @doc Make the filename for the host.
|
2021-01-11 20:18:28 +01:00
|
|
|
%% Example: ``(<<"20080804-231550">>, <<"xmpp.domain.tld">>) ->
|
|
|
|
%% <<"20080804-231550_xmpp_domain_tld.xml">>''
|
2013-03-14 10:33:02 +01:00
|
|
|
make_host_filename(FnT, Host) ->
|
|
|
|
Host2 = str:join(str:tokens(Host, <<".">>), <<"_">>),
|
|
|
|
<<FnT/binary, "_", Host2/binary, ".xml">>.
|
2009-08-07 11:52:45 +02:00
|
|
|
|
|
|
|
%%%==================================
|
|
|
|
%%%% PIEFXIS formatting
|
2013-03-14 10:33:02 +01:00
|
|
|
make_host_basefilename(Dir, FnT) ->
|
|
|
|
filename:join([Dir, FnT]).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
|
|
|
make_piefxis_xml_head() ->
|
|
|
|
"<?xml version='1.0' encoding='UTF-8'?>".
|
|
|
|
|
|
|
|
make_piefxis_xml_tail() ->
|
|
|
|
"".
|
|
|
|
|
|
|
|
make_piefxis_server_head() ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("<server-data xmlns='~ts' xmlns:xi='~ts'>",
|
2013-03-14 10:33:02 +01:00
|
|
|
[?NS_PIE, ?NS_XI]).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
|
|
|
make_piefxis_server_tail() ->
|
|
|
|
"</server-data>".
|
|
|
|
|
|
|
|
make_piefxis_host_head(Host) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("<host xmlns='~ts' xmlns:xi='~ts' jid='~ts'>",
|
2013-03-14 10:33:02 +01:00
|
|
|
[?NS_PIE, ?NS_XI, Host]).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
|
|
|
make_piefxis_host_tail() ->
|
|
|
|
"</host>".
|
|
|
|
|
|
|
|
make_xinclude(Fn) ->
|
|
|
|
Base = filename:basename(Fn),
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("<xi:include href='~ts'/>", [Base]).
|
2009-08-07 11:52:45 +02:00
|
|
|
|
|
|
|
print(Fd, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
file:write(Fd, String).
|