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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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:
|
|
|
|
%%% - write mod_piefxis with ejabberdctl commands
|
2016-04-20 11:27:32 +02:00
|
|
|
%%% - Export from mod_offline_sql.erl
|
|
|
|
%%% - Export from mod_private_sql.erl
|
2009-08-07 11:52:45 +02:00
|
|
|
%%% - XEP-227: 6. Security Considerations
|
|
|
|
%%% - 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.
|
|
|
|
%%%% Headers
|
|
|
|
|
|
|
|
-module(ejabberd_piefxis).
|
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 227, '1.0'}).
|
|
|
|
|
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
|
|
|
|
2018-06-14 13:00:47 +02:00
|
|
|
-include("scram.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-08-04 12:37:07 +02:00
|
|
|
-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),
|
2018-12-19 12:33:46 +01:00
|
|
|
Pass = case ejabberd_auth:password_format(LServer) of
|
|
|
|
scram -> format_scram_password(Password);
|
|
|
|
_ -> Password
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Els = get_offline(User, Server) ++
|
|
|
|
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">>,
|
|
|
|
attrs = [{<<"name">>, User},
|
|
|
|
{<<"password">>, Pass}],
|
|
|
|
children = Els})).
|
|
|
|
|
2018-12-19 12:33:46 +01:00
|
|
|
format_scram_password(#scram{storedkey = StoredKey, serverkey = ServerKey,
|
|
|
|
salt = Salt, iterationcount = IterationCount}) ->
|
2015-02-05 16:44:08 +01:00
|
|
|
StoredKeyB64 = base64:encode(StoredKey),
|
|
|
|
ServerKeyB64 = base64:encode(ServerKey),
|
|
|
|
SaltB64 = base64:encode(Salt),
|
2016-11-24 13:06:06 +01:00
|
|
|
IterationCountBin = (integer_to_binary(IterationCount)),
|
2015-02-05 16:44:08 +01:00
|
|
|
<<"scram:", StoredKeyB64/binary, ",", ServerKeyB64/binary, ",", SaltB64/binary, ",", IterationCountBin/binary>>.
|
|
|
|
|
|
|
|
parse_scram_password(PassData) ->
|
|
|
|
Split = binary:split(PassData, <<",">>, [global]),
|
|
|
|
[StoredKeyB64, ServerKeyB64, SaltB64, IterationCountBin] = Split,
|
|
|
|
#scram{
|
|
|
|
storedkey = StoredKeyB64,
|
|
|
|
serverkey = ServerKeyB64,
|
|
|
|
salt = SaltB64,
|
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),
|
|
|
|
case mod_vcard:get_vcard(LUser, LServer) of
|
|
|
|
error -> [];
|
|
|
|
Els -> Els
|
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),
|
|
|
|
case 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}]
|
|
|
|
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) ->
|
|
|
|
case mod_privacy:get_user_lists(User, Server) of
|
|
|
|
{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
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
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),
|
2013-03-14 10:33:02 +01:00
|
|
|
case mod_roster:get_roster(User, Server) of
|
|
|
|
[_|_] = 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
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
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) ->
|
|
|
|
case mod_private:get_data(User, Server) of
|
|
|
|
[_|_] = Els ->
|
2018-01-25 18:02:47 +01:00
|
|
|
[xmpp:encode(#private{sub_els = Els})];
|
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) ->
|
|
|
|
receive
|
|
|
|
{'$gen_event', El} ->
|
|
|
|
case process_el(El, State) of
|
|
|
|
{ok, NewState} ->
|
|
|
|
process_els(NewState);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end
|
|
|
|
after 0 ->
|
|
|
|
{ok, State}
|
|
|
|
end.
|
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}.
|
|
|
|
|
|
|
|
process_user(#xmlel{name = <<"user">>, attrs = Attrs, children = Els},
|
|
|
|
#state{server = LServer} = State) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Name = fxml:get_attr_s(<<"name">>, Attrs),
|
|
|
|
Password = fxml:get_attr_s(<<"password">>, Attrs),
|
2017-04-28 12:23:32 +02:00
|
|
|
PasswordFormat = ejabberd_auth:password_format(LServer),
|
2015-02-05 16:44:08 +01:00
|
|
|
Pass = case PasswordFormat of
|
|
|
|
scram ->
|
2019-06-27 10:32:54 +02:00
|
|
|
case Password of
|
2015-02-05 16:44:08 +01:00
|
|
|
<<"scram:", PassData/binary>> ->
|
|
|
|
parse_scram_password(PassData);
|
|
|
|
P -> P
|
|
|
|
end;
|
|
|
|
_ -> Password
|
|
|
|
end,
|
|
|
|
|
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});
|
2018-05-07 13:02:17 +02:00
|
|
|
{error, invalid_password} when (Password == <<>>) ->
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
%% Example: ``(<<"20080804-231550">>, <<"jabber.example.org">>) ->
|
|
|
|
%% <<"20080804-231550_jabber_example_org.xml">>''
|
|
|
|
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
|
|
|
|
|
|
|
%% @spec () -> string()
|
|
|
|
make_piefxis_xml_head() ->
|
|
|
|
"<?xml version='1.0' encoding='UTF-8'?>".
|
|
|
|
|
|
|
|
%% @spec () -> string()
|
|
|
|
make_piefxis_xml_tail() ->
|
|
|
|
"".
|
|
|
|
|
|
|
|
%% @spec () -> string()
|
|
|
|
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
|
|
|
|
|
|
|
%% @spec () -> string()
|
|
|
|
make_piefxis_server_tail() ->
|
|
|
|
"</server-data>".
|
|
|
|
|
|
|
|
%% @spec (Host::string()) -> string()
|
|
|
|
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
|
|
|
|
|
|
|
%% @spec () -> string()
|
|
|
|
make_piefxis_host_tail() ->
|
|
|
|
"</host>".
|
|
|
|
|
|
|
|
%% @spec (Fn::string()) -> string()
|
|
|
|
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).
|