2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_c2s.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-12-17 21:13:21 +01:00
|
|
|
%%% Purpose : Serve C2S connection
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 16 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-07-31 13:18:49 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 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.
|
|
|
|
%%%
|
|
|
|
%%% 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
|
|
|
|
%%%
|
2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_c2s).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2006-01-27 06:00:27 +01:00
|
|
|
-update_info({update, 0}).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2003-07-20 22:35:35 +02:00
|
|
|
-export([start/2,
|
|
|
|
start_link/2,
|
|
|
|
send_text/2,
|
2004-03-02 22:16:55 +01:00
|
|
|
send_element/2,
|
2006-09-25 05:51:11 +02:00
|
|
|
socket_type/0,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_presence/1,
|
2008-12-08 15:10:55 +01:00
|
|
|
get_subscribed/1]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
2003-03-09 21:46:47 +01:00
|
|
|
-export([init/1,
|
|
|
|
wait_for_stream/2,
|
|
|
|
wait_for_auth/2,
|
2004-08-01 22:12:20 +02:00
|
|
|
wait_for_feature_request/2,
|
2003-11-07 21:51:23 +01:00
|
|
|
wait_for_bind/2,
|
2003-06-07 19:30:25 +02:00
|
|
|
wait_for_session/2,
|
2003-03-12 20:48:05 +01:00
|
|
|
wait_for_sasl_response/2,
|
2003-03-09 21:46:47 +01:00
|
|
|
session_established/2,
|
2003-01-24 21:18:33 +01:00
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
code_change/4,
|
2002-11-30 19:46:16 +01:00
|
|
|
handle_info/3,
|
2002-11-29 21:55:12 +01:00
|
|
|
terminate/3]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2008-04-10 10:14:38 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
-define(SETS, gb_sets).
|
2007-12-01 06:16:30 +01:00
|
|
|
-define(DICT, dict).
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2008-11-08 18:50:54 +01:00
|
|
|
%% pres_a contains all the presence available send (either through roster mechanism or directed).
|
|
|
|
%% Directed presence unavailable remove user from pres_a.
|
2006-09-05 06:26:28 +02:00
|
|
|
-record(state, {socket,
|
2006-10-01 03:53:37 +02:00
|
|
|
sockmod,
|
2007-04-12 08:08:32 +02:00
|
|
|
socket_monitor,
|
2003-04-15 15:48:43 +02:00
|
|
|
streamid,
|
2003-03-09 21:46:47 +01:00
|
|
|
sasl_state,
|
2003-02-01 21:21:28 +01:00
|
|
|
access,
|
2003-02-09 20:17:23 +01:00
|
|
|
shaper,
|
2006-01-19 03:17:31 +01:00
|
|
|
zlib = false,
|
2004-07-28 22:08:53 +02:00
|
|
|
tls = false,
|
2004-12-05 21:54:55 +01:00
|
|
|
tls_required = false,
|
2004-07-28 22:08:53 +02:00
|
|
|
tls_enabled = false,
|
|
|
|
tls_options = [],
|
2005-07-15 00:28:21 +02:00
|
|
|
authenticated = false,
|
2003-10-07 22:31:44 +02:00
|
|
|
jid,
|
2002-12-17 21:49:45 +01:00
|
|
|
user = "", server = ?MYNAME, resource = "",
|
2006-01-24 00:13:06 +01:00
|
|
|
sid,
|
2002-12-17 21:49:45 +01:00
|
|
|
pres_t = ?SETS:new(),
|
|
|
|
pres_f = ?SETS:new(),
|
|
|
|
pres_a = ?SETS:new(),
|
|
|
|
pres_i = ?SETS:new(),
|
|
|
|
pres_last, pres_pri,
|
|
|
|
pres_timestamp,
|
2003-08-03 21:09:40 +02:00
|
|
|
pres_invis = false,
|
2008-04-10 10:14:38 +02:00
|
|
|
privacy_list = #userlist{},
|
2008-02-15 17:35:32 +01:00
|
|
|
conn = unknown,
|
2008-04-22 19:41:30 +02:00
|
|
|
auth_module = unknown,
|
2007-05-21 05:21:34 +02:00
|
|
|
ip,
|
2004-01-18 21:42:09 +01:00
|
|
|
lang}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
%-define(DBGFSM, true).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
|
2007-09-14 16:16:36 +02:00
|
|
|
%% Module start with or without supervisor:
|
|
|
|
-ifdef(NO_TRANSIENT_SUPERVISORS).
|
|
|
|
-define(SUPERVISOR_START, gen_fsm:start(ejabberd_c2s, [SockData, Opts],
|
|
|
|
?FSMOPTS)).
|
|
|
|
-else.
|
|
|
|
-define(SUPERVISOR_START, supervisor:start_child(ejabberd_c2s_sup,
|
|
|
|
[SockData, Opts])).
|
|
|
|
-endif.
|
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
%% This is the timeout to apply between event when starting a new
|
|
|
|
%% session:
|
2007-12-06 18:42:25 +01:00
|
|
|
-define(C2S_OPEN_TIMEOUT, 60000).
|
2007-09-14 16:18:01 +02:00
|
|
|
-define(C2S_HIBERNATE_TIMEOUT, 90000).
|
2007-09-14 16:17:42 +02:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
-define(STREAM_HEADER,
|
2002-11-19 21:58:49 +01:00
|
|
|
"<?xml version='1.0'?>"
|
2002-11-18 21:39:47 +01:00
|
|
|
"<stream:stream xmlns='jabber:client' "
|
|
|
|
"xmlns:stream='http://etherx.jabber.org/streams' "
|
2004-07-10 00:34:26 +02:00
|
|
|
"id='~s' from='~s'~s~s>"
|
2002-11-18 21:39:47 +01:00
|
|
|
).
|
|
|
|
|
2002-11-19 21:58:49 +01:00
|
|
|
-define(STREAM_TRAILER, "</stream:stream>").
|
|
|
|
|
2003-03-15 22:11:20 +01:00
|
|
|
-define(INVALID_NS_ERR,
|
|
|
|
xml:element_to_string(?SERR_INVALID_NAMESPACE)).
|
2003-03-02 21:55:16 +01:00
|
|
|
-define(INVALID_XML_ERR,
|
2003-03-09 21:46:47 +01:00
|
|
|
xml:element_to_string(?SERR_XML_NOT_WELL_FORMED)).
|
2005-04-17 20:08:34 +02:00
|
|
|
-define(HOST_UNKNOWN_ERR,
|
|
|
|
xml:element_to_string(?SERR_HOST_UNKNOWN)).
|
2004-12-05 21:54:55 +01:00
|
|
|
-define(POLICY_VIOLATION_ERR(Lang, Text),
|
|
|
|
xml:element_to_string(?SERRT_POLICY_VIOLATION(Lang, Text))).
|
2008-12-08 10:21:36 +01:00
|
|
|
-define(INVALID_FROM,
|
|
|
|
xml:element_to_string(?SERR_INVALID_FROM)).
|
|
|
|
|
2002-11-19 21:58:49 +01:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-07-20 22:35:35 +02:00
|
|
|
start(SockData, Opts) ->
|
2007-09-14 16:16:36 +02:00
|
|
|
?SUPERVISOR_START.
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2003-07-14 20:06:03 +02:00
|
|
|
start_link(SockData, Opts) ->
|
|
|
|
gen_fsm:start_link(ejabberd_c2s, [SockData, Opts], ?FSMOPTS).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2006-09-25 05:51:11 +02:00
|
|
|
socket_type() ->
|
|
|
|
xml_stream.
|
|
|
|
|
2004-03-02 22:16:55 +01:00
|
|
|
%% Return Username, Resource and presence information
|
|
|
|
get_presence(FsmRef) ->
|
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef, {get_presence}, 1000).
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
2007-10-30 18:38:40 +01:00
|
|
|
%% {stop, StopReason}
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2006-10-01 03:53:37 +02:00
|
|
|
init([{SockMod, Socket}, Opts]) ->
|
2003-02-01 21:21:28 +01:00
|
|
|
Access = case lists:keysearch(access, 1, Opts) of
|
2003-02-09 20:17:23 +01:00
|
|
|
{value, {_, A}} -> A;
|
|
|
|
_ -> all
|
|
|
|
end,
|
|
|
|
Shaper = case lists:keysearch(shaper, 1, Opts) of
|
|
|
|
{value, {_, S}} -> S;
|
|
|
|
_ -> none
|
2003-02-01 21:21:28 +01:00
|
|
|
end,
|
2006-01-19 03:17:31 +01:00
|
|
|
Zlib = lists:member(zlib, Opts),
|
2004-08-08 21:07:55 +02:00
|
|
|
StartTLS = lists:member(starttls, Opts),
|
2004-12-05 21:54:55 +01:00
|
|
|
StartTLSRequired = lists:member(starttls_required, Opts),
|
2004-08-08 21:07:55 +02:00
|
|
|
TLSEnabled = lists:member(tls, Opts),
|
2004-12-05 21:54:55 +01:00
|
|
|
TLS = StartTLS orelse StartTLSRequired orelse TLSEnabled,
|
2004-07-28 22:08:53 +02:00
|
|
|
TLSOpts = lists:filter(fun({certfile, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Opts),
|
2008-11-03 14:54:42 +01:00
|
|
|
Zlib = lists:member(zlib, Opts) andalso (not StartTLSRequired),
|
2007-05-21 05:21:34 +02:00
|
|
|
IP = peerip(SockMod, Socket),
|
2008-05-05 18:22:06 +02:00
|
|
|
%% Check if IP is blacklisted:
|
|
|
|
case is_ip_blacklisted(IP) of
|
|
|
|
true ->
|
|
|
|
?INFO_MSG("Connection attempt from blacklisted IP: ~s",
|
|
|
|
[jlib:ip_to_list(IP)]),
|
|
|
|
{stop, normal};
|
|
|
|
false ->
|
|
|
|
Socket1 =
|
|
|
|
if
|
|
|
|
TLSEnabled ->
|
|
|
|
SockMod:starttls(Socket, TLSOpts);
|
|
|
|
true ->
|
|
|
|
Socket
|
|
|
|
end,
|
|
|
|
SocketMonitor = SockMod:monitor(Socket1),
|
|
|
|
{ok, wait_for_stream, #state{socket = Socket1,
|
|
|
|
sockmod = SockMod,
|
|
|
|
socket_monitor = SocketMonitor,
|
|
|
|
zlib = Zlib,
|
|
|
|
tls = TLS,
|
|
|
|
tls_required = StartTLSRequired,
|
|
|
|
tls_enabled = TLSEnabled,
|
|
|
|
tls_options = TLSOpts,
|
|
|
|
streamid = new_id(),
|
|
|
|
access = Access,
|
|
|
|
shaper = Shaper,
|
|
|
|
ip = IP},
|
|
|
|
?C2S_OPEN_TIMEOUT}
|
|
|
|
end.
|
2004-07-28 22:08:53 +02:00
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Return list of all available resources of contacts,
|
2008-08-27 11:28:47 +02:00
|
|
|
get_subscribed(FsmRef) ->
|
2008-12-08 15:10:55 +01:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef, get_subscribed, 1000).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2007-10-30 18:38:40 +01:00
|
|
|
%% {stop, Reason, NewStateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) ->
|
2004-07-10 00:34:26 +02:00
|
|
|
DefaultLang = case ?MYLANG of
|
|
|
|
undefined ->
|
|
|
|
" xml:lang='en'";
|
|
|
|
DL ->
|
|
|
|
" xml:lang='" ++ DL ++ "'"
|
|
|
|
end,
|
2003-03-09 21:46:47 +01:00
|
|
|
case xml:get_attr_s("xmlns:stream", Attrs) of
|
|
|
|
?NS_STREAM ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Server = jlib:nameprep(xml:get_attr_s("to", Attrs)),
|
|
|
|
case lists:member(Server, ?MYHOSTS) of
|
|
|
|
true ->
|
|
|
|
Lang = xml:get_attr_s("xml:lang", Attrs),
|
2007-02-20 00:35:42 +01:00
|
|
|
change_shaper(StateData, jlib:make_jid("", Server, "")),
|
2005-04-17 20:08:34 +02:00
|
|
|
case xml:get_attr_s("version", Attrs) of
|
|
|
|
"1.0" ->
|
|
|
|
Header = io_lib:format(?STREAM_HEADER,
|
|
|
|
[StateData#state.streamid,
|
|
|
|
Server,
|
|
|
|
" version='1.0'",
|
|
|
|
DefaultLang]),
|
|
|
|
send_text(StateData, Header),
|
2005-07-15 00:28:21 +02:00
|
|
|
case StateData#state.authenticated of
|
2005-04-17 20:08:34 +02:00
|
|
|
false ->
|
|
|
|
SASLState =
|
|
|
|
cyrsasl:server_new(
|
|
|
|
"jabber", Server, "", [],
|
|
|
|
fun(U) ->
|
2008-04-22 19:41:30 +02:00
|
|
|
ejabberd_auth:get_password_with_authmodule(
|
2005-04-17 20:08:34 +02:00
|
|
|
U, Server)
|
|
|
|
end,
|
|
|
|
fun(U, P) ->
|
2008-04-22 19:41:30 +02:00
|
|
|
ejabberd_auth:check_password_with_authmodule(
|
2005-04-17 20:08:34 +02:00
|
|
|
U, Server, P)
|
|
|
|
end),
|
|
|
|
Mechs = lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
{xmlelement, "mechanism", [],
|
|
|
|
[{xmlcdata, S}]}
|
2005-07-13 05:24:13 +02:00
|
|
|
end, cyrsasl:listmech(Server)),
|
2006-10-01 03:53:37 +02:00
|
|
|
SockMod =
|
|
|
|
(StateData#state.sockmod):get_sockmod(
|
|
|
|
StateData#state.socket),
|
2006-01-19 03:17:31 +01:00
|
|
|
Zlib = StateData#state.zlib,
|
|
|
|
CompressFeature =
|
|
|
|
case Zlib andalso
|
2006-03-25 22:16:49 +01:00
|
|
|
(SockMod == gen_tcp) of
|
2006-01-19 03:17:31 +01:00
|
|
|
true ->
|
|
|
|
[{xmlelement, "compression",
|
|
|
|
[{"xmlns", ?NS_FEATURE_COMPRESS}],
|
|
|
|
[{xmlelement, "method",
|
|
|
|
[], [{xmlcdata, "zlib"}]}]}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2005-04-17 20:08:34 +02:00
|
|
|
TLS = StateData#state.tls,
|
|
|
|
TLSEnabled = StateData#state.tls_enabled,
|
|
|
|
TLSRequired = StateData#state.tls_required,
|
|
|
|
TLSFeature =
|
|
|
|
case (TLS == true) andalso
|
|
|
|
(TLSEnabled == false) andalso
|
|
|
|
(SockMod == gen_tcp) of
|
2004-12-05 21:54:55 +01:00
|
|
|
true ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case TLSRequired of
|
|
|
|
true ->
|
|
|
|
[{xmlelement, "starttls",
|
|
|
|
[{"xmlns", ?NS_TLS}],
|
|
|
|
[{xmlelement, "required",
|
|
|
|
[], []}]}];
|
|
|
|
_ ->
|
|
|
|
[{xmlelement, "starttls",
|
|
|
|
[{"xmlns", ?NS_TLS}], []}]
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "stream:features", [],
|
2006-01-19 03:17:31 +01:00
|
|
|
TLSFeature ++ CompressFeature ++
|
2005-04-17 20:08:34 +02:00
|
|
|
[{xmlelement, "mechanisms",
|
|
|
|
[{"xmlns", ?NS_SASL}],
|
2005-07-15 00:28:21 +02:00
|
|
|
Mechs}] ++
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
c2s_stream_features,
|
|
|
|
Server,
|
|
|
|
[], [])}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request,
|
|
|
|
StateData#state{
|
|
|
|
server = Server,
|
|
|
|
sasl_state = SASLState,
|
|
|
|
lang = Lang});
|
2003-11-07 21:51:23 +01:00
|
|
|
_ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case StateData#state.resource of
|
|
|
|
"" ->
|
|
|
|
send_element(
|
|
|
|
StateData,
|
|
|
|
{xmlelement, "stream:features", [],
|
|
|
|
[{xmlelement, "bind",
|
|
|
|
[{"xmlns", ?NS_BIND}], []},
|
|
|
|
{xmlelement, "session",
|
|
|
|
[{"xmlns", ?NS_SESSION}], []}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_bind,
|
|
|
|
StateData#state{
|
|
|
|
server = Server,
|
|
|
|
lang = Lang});
|
2005-04-17 20:08:34 +02:00
|
|
|
_ ->
|
|
|
|
send_element(
|
|
|
|
StateData,
|
|
|
|
{xmlelement, "stream:features", [], []}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_session,
|
|
|
|
StateData#state{
|
|
|
|
server = Server,
|
|
|
|
lang = Lang})
|
2005-04-17 20:08:34 +02:00
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Header = io_lib:format(
|
|
|
|
?STREAM_HEADER,
|
2005-05-09 21:12:41 +02:00
|
|
|
[StateData#state.streamid, Server, "",
|
|
|
|
DefaultLang]),
|
|
|
|
if
|
|
|
|
(not StateData#state.tls_enabled) and
|
|
|
|
StateData#state.tls_required ->
|
|
|
|
send_text(StateData,
|
|
|
|
Header ++
|
|
|
|
?POLICY_VIOLATION_ERR(
|
|
|
|
Lang,
|
|
|
|
"Use of STARTTLS required") ++
|
|
|
|
?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
send_text(StateData, Header),
|
2007-10-30 18:38:40 +01:00
|
|
|
fsm_next_state(wait_for_auth,
|
2008-04-10 10:14:38 +02:00
|
|
|
StateData#state{
|
|
|
|
server = Server,
|
|
|
|
lang = Lang})
|
2005-05-09 21:12:41 +02:00
|
|
|
end
|
2003-06-07 19:30:25 +02:00
|
|
|
end;
|
2003-03-09 21:46:47 +01:00
|
|
|
_ ->
|
|
|
|
Header = io_lib:format(
|
|
|
|
?STREAM_HEADER,
|
2005-05-09 21:12:41 +02:00
|
|
|
[StateData#state.streamid, ?MYNAME, "",
|
|
|
|
DefaultLang]),
|
2005-04-17 20:08:34 +02:00
|
|
|
send_text(StateData,
|
|
|
|
Header ++ ?HOST_UNKNOWN_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData}
|
2003-03-09 21:46:47 +01:00
|
|
|
end;
|
2002-11-18 21:39:47 +01:00
|
|
|
_ ->
|
2003-03-09 21:46:47 +01:00
|
|
|
Header = io_lib:format(
|
|
|
|
?STREAM_HEADER,
|
2005-04-17 20:08:34 +02:00
|
|
|
[StateData#state.streamid, ?MYNAME, "", DefaultLang]),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData,
|
2003-03-15 22:11:20 +01:00
|
|
|
Header ++ ?INVALID_NS_ERR ++ ?STREAM_TRAILER),
|
2002-11-19 21:58:49 +01:00
|
|
|
{stop, normal, StateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
end;
|
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_stream(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2006-12-08 21:34:34 +01:00
|
|
|
wait_for_stream({xmlstreamelement, _}, StateData) ->
|
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_stream({xmlstreamend, _}, StateData) ->
|
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-03-02 21:55:16 +01:00
|
|
|
wait_for_stream({xmlstreamerror, _}, StateData) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
Header = io_lib:format(?STREAM_HEADER,
|
2004-07-10 00:34:26 +02:00
|
|
|
["none", ?MYNAME, " version='1.0'", ""]),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData,
|
2003-03-09 21:46:47 +01:00
|
|
|
Header ++ ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2003-03-02 21:55:16 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
wait_for_stream(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
wait_for_auth({xmlstreamelement, El}, StateData) ->
|
2002-11-20 21:19:20 +01:00
|
|
|
case is_auth_packet(El) of
|
2003-12-17 21:13:21 +01:00
|
|
|
{auth, _ID, get, {U, _, _, _}} ->
|
|
|
|
{xmlelement, Name, Attrs, _Els} = jlib:make_result_iq_reply(El),
|
2006-04-12 04:54:27 +02:00
|
|
|
case U of
|
|
|
|
"" ->
|
|
|
|
UCdata = [];
|
|
|
|
_ ->
|
|
|
|
UCdata = [{xmlcdata, U}]
|
|
|
|
end,
|
2005-07-13 05:24:13 +02:00
|
|
|
Res = case ejabberd_auth:plain_password_required(
|
|
|
|
StateData#state.server) of
|
2003-11-23 21:11:21 +01:00
|
|
|
false ->
|
|
|
|
{xmlelement, Name, Attrs,
|
|
|
|
[{xmlelement, "query", [{"xmlns", ?NS_AUTH}],
|
2006-04-12 04:54:27 +02:00
|
|
|
[{xmlelement, "username", [], UCdata},
|
2003-11-23 21:11:21 +01:00
|
|
|
{xmlelement, "password", [], []},
|
|
|
|
{xmlelement, "digest", [], []},
|
|
|
|
{xmlelement, "resource", [], []}
|
|
|
|
]}]};
|
|
|
|
true ->
|
|
|
|
{xmlelement, Name, Attrs,
|
|
|
|
[{xmlelement, "query", [{"xmlns", ?NS_AUTH}],
|
2006-04-12 04:54:27 +02:00
|
|
|
[{xmlelement, "username", [], UCdata},
|
2004-02-26 23:00:04 +01:00
|
|
|
{xmlelement, "password", [], []},
|
|
|
|
{xmlelement, "resource", [], []}
|
2003-11-23 21:11:21 +01:00
|
|
|
]}]}
|
|
|
|
end,
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData, Res),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_auth, StateData);
|
2006-01-19 03:17:31 +01:00
|
|
|
{auth, _ID, set, {_U, _P, _D, ""}} ->
|
2004-09-25 22:52:20 +02:00
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
El,
|
|
|
|
?ERR_AUTH_NO_RESOURCE_PROVIDED(StateData#state.lang)),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData, Err),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_auth, StateData);
|
2003-12-17 21:13:21 +01:00
|
|
|
{auth, _ID, set, {U, P, D, R}} ->
|
2003-10-07 22:31:44 +02:00
|
|
|
JID = jlib:make_jid(U, StateData#state.server, R),
|
2003-10-30 15:38:38 +01:00
|
|
|
case (JID /= error) andalso
|
2005-06-20 05:18:13 +02:00
|
|
|
(acl:match_rule(StateData#state.server,
|
|
|
|
StateData#state.access, JID) == allow) of
|
2003-10-30 15:38:38 +01:00
|
|
|
true ->
|
2008-04-22 19:41:30 +02:00
|
|
|
case ejabberd_auth:check_password_with_authmodule(
|
2005-04-17 20:08:34 +02:00
|
|
|
U, StateData#state.server, P,
|
|
|
|
StateData#state.streamid, D) of
|
2008-04-22 19:41:30 +02:00
|
|
|
{true, AuthModule} ->
|
2003-10-09 20:09:05 +02:00
|
|
|
?INFO_MSG(
|
2004-11-05 22:14:31 +01:00
|
|
|
"(~w) Accepted legacy authentication for ~s",
|
2003-10-09 20:09:05 +02:00
|
|
|
[StateData#state.socket,
|
|
|
|
jlib:jid_to_string(JID)]),
|
2006-01-24 00:13:06 +01:00
|
|
|
SID = {now(), self()},
|
2008-02-15 17:35:32 +01:00
|
|
|
Conn = get_conn_type(StateData),
|
2008-04-22 19:41:30 +02:00
|
|
|
Info = [{ip, StateData#state.ip}, {conn, Conn},
|
|
|
|
{auth_module, AuthModule}],
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_sm:open_session(
|
2008-02-15 17:35:32 +01:00
|
|
|
SID, U, StateData#state.server, R, Info),
|
2003-11-28 20:08:51 +01:00
|
|
|
Res1 = jlib:make_result_iq_reply(El),
|
|
|
|
Res = setelement(4, Res1, []),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData, Res),
|
2003-02-09 20:17:23 +01:00
|
|
|
change_shaper(StateData, JID),
|
2004-12-19 21:47:35 +01:00
|
|
|
{Fs, Ts} = ejabberd_hooks:run_fold(
|
|
|
|
roster_get_subscription_lists,
|
2005-06-20 05:18:13 +02:00
|
|
|
StateData#state.server,
|
2004-12-19 21:47:35 +01:00
|
|
|
{[], []},
|
2005-04-17 20:08:34 +02:00
|
|
|
[U, StateData#state.server]),
|
2004-07-05 23:51:23 +02:00
|
|
|
LJID = jlib:jid_tolower(
|
|
|
|
jlib:jid_remove_resource(JID)),
|
|
|
|
Fs1 = [LJID | Fs],
|
|
|
|
Ts1 = [LJID | Ts],
|
2003-08-03 21:09:40 +02:00
|
|
|
PrivList =
|
2006-10-05 05:17:41 +02:00
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
privacy_get_user_list, StateData#state.server,
|
2008-04-10 10:14:38 +02:00
|
|
|
#userlist{},
|
2006-10-05 05:17:41 +02:00
|
|
|
[U, StateData#state.server]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(session_established,
|
|
|
|
StateData#state{
|
|
|
|
user = U,
|
2003-02-01 21:21:28 +01:00
|
|
|
resource = R,
|
2003-10-07 22:31:44 +02:00
|
|
|
jid = JID,
|
2006-01-24 00:13:06 +01:00
|
|
|
sid = SID,
|
2008-02-15 17:35:32 +01:00
|
|
|
conn = Conn,
|
2008-04-22 19:41:30 +02:00
|
|
|
auth_module = AuthModule,
|
2004-07-05 23:51:23 +02:00
|
|
|
pres_f = ?SETS:from_list(Fs1),
|
|
|
|
pres_t = ?SETS:from_list(Ts1),
|
2007-09-14 16:18:01 +02:00
|
|
|
privacy_list = PrivList});
|
2003-02-01 21:21:28 +01:00
|
|
|
_ ->
|
2003-10-09 20:09:05 +02:00
|
|
|
?INFO_MSG(
|
2004-11-05 22:14:31 +01:00
|
|
|
"(~w) Failed legacy authentication for ~s",
|
2003-10-09 20:09:05 +02:00
|
|
|
[StateData#state.socket,
|
|
|
|
jlib:jid_to_string(JID)]),
|
2003-02-01 21:21:28 +01:00
|
|
|
Err = jlib:make_error_reply(
|
2004-09-25 22:52:20 +02:00
|
|
|
El, ?ERR_NOT_AUTHORIZED),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData, Err),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_auth, StateData)
|
2003-02-01 21:21:28 +01:00
|
|
|
end;
|
2002-11-23 21:55:05 +01:00
|
|
|
_ ->
|
2003-11-11 21:51:04 +01:00
|
|
|
if
|
|
|
|
JID == error ->
|
|
|
|
?INFO_MSG(
|
2004-11-05 22:14:31 +01:00
|
|
|
"(~w) Forbidden legacy authentication for "
|
2003-11-11 21:51:04 +01:00
|
|
|
"username '~s' with resource '~s'",
|
|
|
|
[StateData#state.socket, U, R]),
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_JID_MALFORMED),
|
|
|
|
send_element(StateData, Err),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_auth, StateData);
|
2003-11-11 21:51:04 +01:00
|
|
|
true ->
|
|
|
|
?INFO_MSG(
|
2004-11-05 22:14:31 +01:00
|
|
|
"(~w) Forbidden legacy authentication for ~s",
|
2003-11-11 21:51:04 +01:00
|
|
|
[StateData#state.socket,
|
|
|
|
jlib:jid_to_string(JID)]),
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_NOT_ALLOWED),
|
|
|
|
send_element(StateData, Err),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_auth, StateData)
|
2003-11-11 21:51:04 +01:00
|
|
|
end
|
2002-11-23 21:55:05 +01:00
|
|
|
end;
|
2002-11-20 21:19:20 +01:00
|
|
|
_ ->
|
2005-07-15 00:28:21 +02:00
|
|
|
process_unauthenticated_stanza(StateData, El),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_auth, StateData)
|
2002-11-27 21:46:29 +01:00
|
|
|
end;
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_auth(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_auth({xmlstreamend, _Name}, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
2003-03-02 21:55:16 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_auth({xmlstreamerror, _}, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2002-11-18 21:39:47 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_auth(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2004-08-01 22:12:20 +02:00
|
|
|
wait_for_feature_request({xmlstreamelement, El}, StateData) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{xmlelement, Name, Attrs, Els} = El,
|
2006-01-19 03:17:31 +01:00
|
|
|
Zlib = StateData#state.zlib,
|
2004-08-01 22:12:20 +02:00
|
|
|
TLS = StateData#state.tls,
|
|
|
|
TLSEnabled = StateData#state.tls_enabled,
|
2004-12-05 21:54:55 +01:00
|
|
|
TLSRequired = StateData#state.tls_required,
|
2006-10-01 03:53:37 +02:00
|
|
|
SockMod = (StateData#state.sockmod):get_sockmod(StateData#state.socket),
|
2003-03-09 21:46:47 +01:00
|
|
|
case {xml:get_attr_s("xmlns", Attrs), Name} of
|
2004-12-05 21:54:55 +01:00
|
|
|
{?NS_SASL, "auth"} when not ((SockMod == gen_tcp) and TLSRequired) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
Mech = xml:get_attr_s("mechanism", Attrs),
|
|
|
|
ClientIn = jlib:decode_base64(xml:get_cdata(Els)),
|
|
|
|
case cyrsasl:server_start(StateData#state.sasl_state,
|
|
|
|
Mech,
|
|
|
|
ClientIn) of
|
|
|
|
{ok, Props} ->
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):reset_stream(
|
|
|
|
StateData#state.socket),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData,
|
2003-03-09 21:46:47 +01:00
|
|
|
{xmlelement, "success",
|
2003-06-07 19:30:25 +02:00
|
|
|
[{"xmlns", ?NS_SASL}], []}),
|
2003-11-07 21:51:23 +01:00
|
|
|
U = xml:get_attr_s(username, Props),
|
2004-11-05 22:14:31 +01:00
|
|
|
?INFO_MSG("(~w) Accepted authentication for ~s",
|
2003-11-07 21:51:23 +01:00
|
|
|
[StateData#state.socket, U]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{
|
|
|
|
streamid = new_id(),
|
2005-07-15 00:28:21 +02:00
|
|
|
authenticated = true,
|
2007-09-14 16:18:01 +02:00
|
|
|
user = U });
|
2003-03-09 21:46:47 +01:00
|
|
|
{continue, ServerOut, NewSASLState} ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData,
|
2003-03-09 21:46:47 +01:00
|
|
|
{xmlelement, "challenge",
|
2003-06-07 19:30:25 +02:00
|
|
|
[{"xmlns", ?NS_SASL}],
|
2003-03-09 21:46:47 +01:00
|
|
|
[{xmlcdata,
|
|
|
|
jlib:encode_base64(ServerOut)}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_sasl_response,
|
|
|
|
StateData#state{
|
|
|
|
sasl_state = NewSASLState});
|
2007-02-20 00:35:42 +01:00
|
|
|
{error, Error, Username} ->
|
|
|
|
?INFO_MSG(
|
|
|
|
"(~w) Failed authentication for ~s@~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
Username, StateData#state.server]),
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "failure",
|
|
|
|
[{"xmlns", ?NS_SASL}],
|
|
|
|
[{xmlelement, Error, [], []}]}),
|
2007-09-14 16:17:42 +02:00
|
|
|
{next_state, wait_for_feature_request, StateData,
|
|
|
|
?C2S_OPEN_TIMEOUT};
|
2003-06-07 19:30:25 +02:00
|
|
|
{error, Error} ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData,
|
2003-03-09 21:46:47 +01:00
|
|
|
{xmlelement, "failure",
|
2003-06-07 19:30:25 +02:00
|
|
|
[{"xmlns", ?NS_SASL}],
|
|
|
|
[{xmlelement, Error, [], []}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
2003-03-09 21:46:47 +01:00
|
|
|
end;
|
2004-08-01 22:12:20 +02:00
|
|
|
{?NS_TLS, "starttls"} when TLS == true,
|
|
|
|
TLSEnabled == false,
|
|
|
|
SockMod == gen_tcp ->
|
2005-11-05 22:15:53 +01:00
|
|
|
TLSOpts = case ejabberd_config:get_local_option(
|
|
|
|
{domain_certfile, StateData#state.server}) of
|
|
|
|
undefined ->
|
|
|
|
StateData#state.tls_options;
|
|
|
|
CertFile ->
|
|
|
|
[{certfile, CertFile} |
|
|
|
|
lists:keydelete(
|
|
|
|
certfile, 1, StateData#state.tls_options)]
|
|
|
|
end,
|
2006-09-05 06:26:28 +02:00
|
|
|
Socket = StateData#state.socket,
|
2006-10-01 03:53:37 +02:00
|
|
|
TLSSocket = (StateData#state.sockmod):starttls(
|
|
|
|
Socket, TLSOpts,
|
|
|
|
xml:element_to_string(
|
|
|
|
{xmlelement, "proceed", [{"xmlns", ?NS_TLS}], []})),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{socket = TLSSocket,
|
|
|
|
streamid = new_id(),
|
|
|
|
tls_enabled = true
|
|
|
|
});
|
2006-01-19 03:17:31 +01:00
|
|
|
{?NS_COMPRESS, "compress"} when Zlib == true,
|
2006-03-25 22:16:49 +01:00
|
|
|
SockMod == gen_tcp ->
|
2006-01-24 23:52:23 +01:00
|
|
|
case xml:get_subtag(El, "method") of
|
|
|
|
false ->
|
|
|
|
send_element(StateData,
|
2007-12-01 06:16:30 +01:00
|
|
|
{xmlelement, "failure",
|
2006-01-24 23:52:23 +01:00
|
|
|
[{"xmlns", ?NS_COMPRESS}],
|
|
|
|
[{xmlelement, "setup-failed", [], []}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request, StateData);
|
2006-01-24 23:52:23 +01:00
|
|
|
Method ->
|
|
|
|
case xml:get_tag_cdata(Method) of
|
|
|
|
"zlib" ->
|
|
|
|
Socket = StateData#state.socket,
|
2006-10-01 03:53:37 +02:00
|
|
|
ZlibSocket = (StateData#state.sockmod):compress(
|
|
|
|
Socket,
|
|
|
|
xml:element_to_string(
|
|
|
|
{xmlelement, "compressed",
|
|
|
|
[{"xmlns", ?NS_COMPRESS}], []})),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_stream,
|
2006-09-05 06:26:28 +02:00
|
|
|
StateData#state{socket = ZlibSocket,
|
2006-01-24 23:52:23 +01:00
|
|
|
streamid = new_id()
|
2007-09-14 16:18:01 +02:00
|
|
|
});
|
2006-01-24 23:52:23 +01:00
|
|
|
_ ->
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "failure",
|
|
|
|
[{"xmlns", ?NS_COMPRESS}],
|
|
|
|
[{xmlelement, "unsupported-method",
|
|
|
|
[], []}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request,
|
|
|
|
StateData)
|
2006-01-24 23:52:23 +01:00
|
|
|
end
|
|
|
|
end;
|
2003-03-09 21:46:47 +01:00
|
|
|
_ ->
|
2004-12-05 21:54:55 +01:00
|
|
|
if
|
|
|
|
(SockMod == gen_tcp) and TLSRequired ->
|
|
|
|
Lang = StateData#state.lang,
|
|
|
|
send_text(StateData, ?POLICY_VIOLATION_ERR(
|
|
|
|
Lang,
|
|
|
|
"Use of STARTTLS required") ++
|
|
|
|
?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
2005-07-15 00:28:21 +02:00
|
|
|
process_unauthenticated_stanza(StateData, El),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
2003-03-09 21:46:47 +01:00
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_feature_request(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2004-08-01 22:12:20 +02:00
|
|
|
wait_for_feature_request({xmlstreamend, _Name}, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
2003-03-09 21:46:47 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2004-08-01 22:12:20 +02:00
|
|
|
wait_for_feature_request({xmlstreamerror, _}, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2003-03-09 21:46:47 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2004-08-01 22:12:20 +02:00
|
|
|
wait_for_feature_request(closed, StateData) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
wait_for_sasl_response({xmlstreamelement, El}, StateData) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = El,
|
|
|
|
case {xml:get_attr_s("xmlns", Attrs), Name} of
|
2003-06-07 19:30:25 +02:00
|
|
|
{?NS_SASL, "response"} ->
|
2003-03-12 20:48:05 +01:00
|
|
|
ClientIn = jlib:decode_base64(xml:get_cdata(Els)),
|
|
|
|
case cyrsasl:server_step(StateData#state.sasl_state,
|
|
|
|
ClientIn) of
|
|
|
|
{ok, Props} ->
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):reset_stream(
|
|
|
|
StateData#state.socket),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData,
|
2003-03-12 20:48:05 +01:00
|
|
|
{xmlelement, "success",
|
2003-06-07 19:30:25 +02:00
|
|
|
[{"xmlns", ?NS_SASL}], []}),
|
2003-11-07 21:51:23 +01:00
|
|
|
U = xml:get_attr_s(username, Props),
|
2008-04-22 19:41:30 +02:00
|
|
|
AuthModule = xml:get_attr_s(auth_module, Props),
|
2004-11-05 22:14:31 +01:00
|
|
|
?INFO_MSG("(~w) Accepted authentication for ~s",
|
2003-11-07 21:51:23 +01:00
|
|
|
[StateData#state.socket, U]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{
|
|
|
|
streamid = new_id(),
|
2005-07-15 00:28:21 +02:00
|
|
|
authenticated = true,
|
2008-04-22 19:41:30 +02:00
|
|
|
auth_module = AuthModule,
|
2007-09-14 16:18:01 +02:00
|
|
|
user = U});
|
2003-03-12 20:48:05 +01:00
|
|
|
{continue, ServerOut, NewSASLState} ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData,
|
2003-03-12 20:48:05 +01:00
|
|
|
{xmlelement, "challenge",
|
2003-06-07 19:30:25 +02:00
|
|
|
[{"xmlns", ?NS_SASL}],
|
2003-03-12 20:48:05 +01:00
|
|
|
[{xmlcdata,
|
|
|
|
jlib:encode_base64(ServerOut)}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_sasl_response,
|
|
|
|
StateData#state{sasl_state = NewSASLState});
|
2007-02-20 00:35:42 +01:00
|
|
|
{error, Error, Username} ->
|
|
|
|
?INFO_MSG(
|
|
|
|
"(~w) Failed authentication for ~s@~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
Username, StateData#state.server]),
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "failure",
|
|
|
|
[{"xmlns", ?NS_SASL}],
|
|
|
|
[{xmlelement, Error, [], []}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request, StateData);
|
2003-06-07 19:30:25 +02:00
|
|
|
{error, Error} ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData,
|
2003-03-12 20:48:05 +01:00
|
|
|
{xmlelement, "failure",
|
2003-06-07 19:30:25 +02:00
|
|
|
[{"xmlns", ?NS_SASL}],
|
|
|
|
[{xmlelement, Error, [], []}]}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
2003-03-12 20:48:05 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2005-07-15 00:28:21 +02:00
|
|
|
process_unauthenticated_stanza(StateData, El),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
2003-03-12 20:48:05 +01:00
|
|
|
end;
|
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_sasl_response(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_sasl_response({xmlstreamend, _Name}, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
2003-03-12 20:48:05 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_sasl_response({xmlstreamerror, _}, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2003-03-12 20:48:05 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_sasl_response(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
|
2003-11-07 21:51:23 +01:00
|
|
|
wait_for_bind({xmlstreamelement, El}, StateData) ->
|
|
|
|
case jlib:iq_query_info(El) of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = set, xmlns = ?NS_BIND, sub_el = SubEl} = IQ ->
|
2003-11-07 21:51:23 +01:00
|
|
|
U = StateData#state.user,
|
|
|
|
R1 = xml:get_path_s(SubEl, [{elem, "resource"}, cdata]),
|
|
|
|
R = case jlib:resourceprep(R1) of
|
|
|
|
error -> error;
|
|
|
|
"" ->
|
|
|
|
lists:concat(
|
|
|
|
[randoms:get_string() | tuple_to_list(now())]);
|
|
|
|
Resource -> Resource
|
|
|
|
end,
|
|
|
|
case R of
|
|
|
|
error ->
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_BAD_REQUEST),
|
|
|
|
send_element(StateData, Err),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_bind, StateData);
|
2003-11-07 21:51:23 +01:00
|
|
|
_ ->
|
|
|
|
JID = jlib:make_jid(U, StateData#state.server, R),
|
2003-12-17 21:13:21 +01:00
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "bind",
|
|
|
|
[{"xmlns", ?NS_BIND}],
|
|
|
|
[{xmlelement, "jid", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
jlib:jid_to_string(JID)}]}]}]},
|
2003-11-07 21:51:23 +01:00
|
|
|
send_element(StateData, jlib:iq_to_xml(Res)),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_session,
|
|
|
|
StateData#state{resource = R, jid = JID})
|
2003-11-07 21:51:23 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_bind, StateData)
|
2003-11-07 21:51:23 +01:00
|
|
|
end;
|
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_bind(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_bind({xmlstreamend, _Name}, StateData) ->
|
2003-11-07 21:51:23 +01:00
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_bind({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_bind(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-07 19:30:25 +02:00
|
|
|
wait_for_session({xmlstreamelement, El}, StateData) ->
|
|
|
|
case jlib:iq_query_info(El) of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = set, xmlns = ?NS_SESSION} ->
|
2003-06-07 19:30:25 +02:00
|
|
|
U = StateData#state.user,
|
|
|
|
R = StateData#state.resource,
|
2004-07-05 23:51:23 +02:00
|
|
|
JID = StateData#state.jid,
|
2005-06-20 05:18:13 +02:00
|
|
|
case acl:match_rule(StateData#state.server,
|
|
|
|
StateData#state.access, JID) of
|
2003-06-07 19:30:25 +02:00
|
|
|
allow ->
|
2003-10-09 20:09:05 +02:00
|
|
|
?INFO_MSG("(~w) Opened session for ~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
jlib:jid_to_string(JID)]),
|
2006-01-24 00:13:06 +01:00
|
|
|
SID = {now(), self()},
|
2008-02-15 17:35:32 +01:00
|
|
|
Conn = get_conn_type(StateData),
|
2008-04-22 19:41:30 +02:00
|
|
|
Info = [{ip, StateData#state.ip}, {conn, Conn},
|
|
|
|
{auth_module, StateData#state.auth_module}],
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_sm:open_session(
|
2008-02-15 17:35:32 +01:00
|
|
|
SID, U, StateData#state.server, R, Info),
|
2003-06-07 19:30:25 +02:00
|
|
|
Res = jlib:make_result_iq_reply(El),
|
|
|
|
send_element(StateData, Res),
|
|
|
|
change_shaper(StateData, JID),
|
2004-12-19 21:47:35 +01:00
|
|
|
{Fs, Ts} = ejabberd_hooks:run_fold(
|
|
|
|
roster_get_subscription_lists,
|
2005-06-20 05:18:13 +02:00
|
|
|
StateData#state.server,
|
2004-12-19 21:47:35 +01:00
|
|
|
{[], []},
|
2005-04-17 20:08:34 +02:00
|
|
|
[U, StateData#state.server]),
|
2004-07-05 23:51:23 +02:00
|
|
|
LJID = jlib:jid_tolower(jlib:jid_remove_resource(JID)),
|
|
|
|
Fs1 = [LJID | Fs],
|
|
|
|
Ts1 = [LJID | Ts],
|
2003-08-03 21:09:40 +02:00
|
|
|
PrivList =
|
2006-10-05 05:17:41 +02:00
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
privacy_get_user_list, StateData#state.server,
|
2008-04-10 10:14:38 +02:00
|
|
|
#userlist{},
|
2006-10-05 05:17:41 +02:00
|
|
|
[U, StateData#state.server]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(session_established,
|
|
|
|
StateData#state{
|
|
|
|
sid = SID,
|
2008-02-15 17:35:32 +01:00
|
|
|
conn = Conn,
|
2006-01-24 00:13:06 +01:00
|
|
|
pres_f = ?SETS:from_list(Fs1),
|
2004-07-05 23:51:23 +02:00
|
|
|
pres_t = ?SETS:from_list(Ts1),
|
2007-09-14 16:18:01 +02:00
|
|
|
privacy_list = PrivList});
|
2003-06-07 19:30:25 +02:00
|
|
|
_ ->
|
2008-04-25 11:25:31 +02:00
|
|
|
ejabberd_hooks:run(forbidden_session_hook,
|
|
|
|
StateData#state.server, [JID]),
|
2003-10-09 20:09:05 +02:00
|
|
|
?INFO_MSG("(~w) Forbidden session for ~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
jlib:jid_to_string(JID)]),
|
2003-06-07 19:30:25 +02:00
|
|
|
Err = jlib:make_error_reply(El, ?ERR_NOT_ALLOWED),
|
|
|
|
send_element(StateData, Err),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_session, StateData)
|
2003-06-07 19:30:25 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_session, StateData)
|
2003-06-07 19:30:25 +02:00
|
|
|
end;
|
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_session(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_session({xmlstreamend, _Name}, StateData) ->
|
2003-06-07 19:30:25 +02:00
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_session({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_session(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
2002-11-27 21:46:29 +01:00
|
|
|
session_established({xmlstreamelement, El}, StateData) ->
|
2008-12-08 10:21:36 +01:00
|
|
|
FromJID = StateData#state.jid,
|
|
|
|
% Check 'from' attribute in stanza RFC 3920 Section 9.1.2
|
|
|
|
case check_from(El, FromJID) of
|
|
|
|
'invalid-from' ->
|
|
|
|
send_text(StateData, ?INVALID_FROM ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
_NewEl ->
|
|
|
|
session_established2(El, StateData)
|
|
|
|
end;
|
|
|
|
|
|
|
|
%% We hibernate the process to reduce memory consumption after a
|
|
|
|
%% configurable activity timeout
|
|
|
|
session_established(timeout, StateData) ->
|
|
|
|
%% TODO: Options must be stored in state:
|
|
|
|
Options = [],
|
|
|
|
proc_lib:hibernate(gen_fsm, enter_loop,
|
|
|
|
[?MODULE, Options, session_established, StateData]),
|
|
|
|
fsm_next_state(session_established, StateData);
|
|
|
|
|
|
|
|
session_established({xmlstreamend, _Name}, StateData) ->
|
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
session_established({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
session_established(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
session_established2(El, StateData) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
{xmlelement, Name, Attrs, _Els} = El,
|
2003-07-20 22:35:35 +02:00
|
|
|
User = StateData#state.user,
|
2002-12-17 21:49:45 +01:00
|
|
|
Server = StateData#state.server,
|
2003-10-07 22:31:44 +02:00
|
|
|
FromJID = StateData#state.jid,
|
2002-11-30 19:46:16 +01:00
|
|
|
To = xml:get_attr_s("to", Attrs),
|
|
|
|
ToJID = case To of
|
|
|
|
"" ->
|
2003-10-07 22:31:44 +02:00
|
|
|
jlib:make_jid(User, Server, "");
|
2002-11-30 19:46:16 +01:00
|
|
|
_ ->
|
|
|
|
jlib:string_to_jid(To)
|
|
|
|
end,
|
2006-01-25 01:35:12 +01:00
|
|
|
NewEl1 = jlib:remove_attr("xmlns", El),
|
2004-01-18 21:42:09 +01:00
|
|
|
NewEl = case xml:get_attr_s("xml:lang", Attrs) of
|
|
|
|
"" ->
|
|
|
|
case StateData#state.lang of
|
2006-01-25 01:35:12 +01:00
|
|
|
"" -> NewEl1;
|
2004-01-18 21:42:09 +01:00
|
|
|
Lang ->
|
2006-01-25 01:35:12 +01:00
|
|
|
xml:replace_tag_attr("xml:lang", Lang, NewEl1)
|
2004-01-18 21:42:09 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2006-01-25 01:35:12 +01:00
|
|
|
NewEl1
|
2004-01-18 21:42:09 +01:00
|
|
|
end,
|
2002-12-17 21:49:45 +01:00
|
|
|
NewState =
|
|
|
|
case ToJID of
|
|
|
|
error ->
|
2003-10-10 21:01:11 +02:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"error" -> StateData;
|
|
|
|
"result" -> StateData;
|
|
|
|
_ ->
|
2004-01-18 21:42:09 +01:00
|
|
|
Err = jlib:make_error_reply(NewEl, ?ERR_JID_MALFORMED),
|
2003-10-10 21:01:11 +02:00
|
|
|
send_element(StateData, Err),
|
|
|
|
StateData
|
|
|
|
end;
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
|
|
|
case Name of
|
|
|
|
"presence" ->
|
2007-05-16 12:13:04 +02:00
|
|
|
PresenceEl = ejabberd_hooks:run_fold(
|
|
|
|
c2s_update_presence,
|
|
|
|
Server,
|
|
|
|
NewEl,
|
|
|
|
[User, Server]),
|
2008-05-22 12:19:35 +02:00
|
|
|
ejabberd_hooks:run(
|
|
|
|
user_send_packet,
|
|
|
|
Server,
|
|
|
|
[FromJID, ToJID, PresenceEl]),
|
2002-12-17 21:49:45 +01:00
|
|
|
case ToJID of
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User,
|
|
|
|
server = Server,
|
|
|
|
resource = ""} ->
|
2002-12-17 21:49:45 +01:00
|
|
|
?DEBUG("presence_update(~p,~n\t~p,~n\t~p)",
|
2007-05-16 12:13:04 +02:00
|
|
|
[FromJID, PresenceEl, StateData]),
|
|
|
|
presence_update(FromJID, PresenceEl,
|
|
|
|
StateData);
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
2007-05-16 12:13:04 +02:00
|
|
|
presence_track(FromJID, ToJID, PresenceEl,
|
|
|
|
StateData)
|
2002-12-17 21:49:45 +01:00
|
|
|
end;
|
2003-08-03 21:09:40 +02:00
|
|
|
"iq" ->
|
2008-04-10 10:14:38 +02:00
|
|
|
case jlib:iq_query_info(NewEl) of
|
|
|
|
#iq{xmlns = ?NS_PRIVACY} = IQ ->
|
|
|
|
process_privacy_iq(
|
|
|
|
FromJID, ToJID, IQ, StateData);
|
|
|
|
_ ->
|
|
|
|
ejabberd_hooks:run(
|
|
|
|
user_send_packet,
|
|
|
|
Server,
|
|
|
|
[FromJID, ToJID, NewEl]),
|
|
|
|
ejabberd_router:route(
|
|
|
|
FromJID, ToJID, NewEl),
|
|
|
|
StateData
|
2003-08-03 21:09:40 +02:00
|
|
|
end;
|
|
|
|
"message" ->
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_hooks:run(user_send_packet,
|
2005-06-20 05:18:13 +02:00
|
|
|
Server,
|
2004-08-13 00:34:19 +02:00
|
|
|
[FromJID, ToJID, NewEl]),
|
2004-01-18 21:42:09 +01:00
|
|
|
ejabberd_router:route(FromJID, ToJID, NewEl),
|
2003-08-03 21:09:40 +02:00
|
|
|
StateData;
|
|
|
|
_ ->
|
2002-12-17 21:49:45 +01:00
|
|
|
StateData
|
|
|
|
end
|
|
|
|
end,
|
2007-09-14 16:20:56 +02:00
|
|
|
ejabberd_hooks:run(c2s_loop_debug, [{xmlstreamelement, El}]),
|
2008-12-08 10:21:36 +01:00
|
|
|
fsm_next_state(session_established, NewState).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2007-10-30 18:38:40 +01:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2003-01-24 21:18:33 +01:00
|
|
|
%state_name(Event, From, StateData) ->
|
|
|
|
% Reply = ok,
|
|
|
|
% {reply, Reply, state_name, StateData}.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2007-10-30 18:38:40 +01:00
|
|
|
%% {stop, Reason, NewStateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2003-12-17 21:13:21 +01:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, StateData).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2007-10-30 18:38:40 +01:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2004-03-02 22:16:55 +01:00
|
|
|
handle_sync_event({get_presence}, _From, StateName, StateData) ->
|
|
|
|
User = StateData#state.user,
|
2004-03-10 22:54:54 +01:00
|
|
|
PresLast = StateData#state.pres_last,
|
|
|
|
|
|
|
|
Show = get_showtag(PresLast),
|
|
|
|
Status = get_statustag(PresLast),
|
2004-07-25 23:27:56 +02:00
|
|
|
Resource = StateData#state.resource,
|
2004-03-10 22:54:54 +01:00
|
|
|
|
2004-07-25 23:27:56 +02:00
|
|
|
Reply = {User, Resource, Show, Status},
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_reply(Reply, StateName, StateData);
|
2004-03-10 22:54:54 +01:00
|
|
|
|
2008-08-27 11:28:47 +02:00
|
|
|
handle_sync_event(get_subscribed, _From, StateName, StateData) ->
|
2008-12-08 15:10:55 +01:00
|
|
|
Subscribed = ?SETS:to_list(StateData#state.pres_f),
|
|
|
|
{reply, Subscribed, StateName, StateData};
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName, StateData) ->
|
2002-11-18 21:39:47 +01:00
|
|
|
Reply = ok,
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_reply(Reply, StateName, StateData).
|
2004-03-10 22:54:54 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2007-10-30 18:38:40 +01:00
|
|
|
%% {stop, Reason, NewStateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2002-11-30 19:46:16 +01:00
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, Text),
|
2007-09-14 16:20:56 +02:00
|
|
|
ejabberd_hooks:run(c2s_loop_debug, [Text]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, StateData);
|
2003-12-17 21:13:21 +01:00
|
|
|
handle_info(replaced, _StateName, StateData) ->
|
2005-11-22 19:00:56 +01:00
|
|
|
Lang = StateData#state.lang,
|
|
|
|
send_text(StateData,
|
|
|
|
xml:element_to_string(
|
|
|
|
?SERRT_CONFLICT(Lang, "Replaced by new connection"))
|
|
|
|
++ ?STREAM_TRAILER),
|
2005-07-15 00:28:21 +02:00
|
|
|
{stop, normal, StateData#state{authenticated = replaced}};
|
2002-12-17 21:49:45 +01:00
|
|
|
handle_info({route, From, To, Packet}, StateName, StateData) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
|
|
|
{Pass, NewAttrs, NewState} =
|
|
|
|
case Name of
|
|
|
|
"presence" ->
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"probe" ->
|
2006-03-16 02:02:44 +01:00
|
|
|
LFrom = jlib:jid_tolower(From),
|
|
|
|
LBFrom = jlib:jid_remove_resource(LFrom),
|
|
|
|
NewStateData =
|
|
|
|
case ?SETS:is_element(
|
|
|
|
LFrom, StateData#state.pres_a) orelse
|
|
|
|
?SETS:is_element(
|
|
|
|
LBFrom, StateData#state.pres_a) of
|
|
|
|
true ->
|
|
|
|
StateData;
|
|
|
|
false ->
|
|
|
|
case ?SETS:is_element(
|
|
|
|
LFrom, StateData#state.pres_f) of
|
|
|
|
true ->
|
|
|
|
A = ?SETS:add_element(
|
|
|
|
LFrom,
|
|
|
|
StateData#state.pres_a),
|
|
|
|
StateData#state{pres_a = A};
|
|
|
|
false ->
|
|
|
|
case ?SETS:is_element(
|
|
|
|
LBFrom, StateData#state.pres_f) of
|
|
|
|
true ->
|
|
|
|
A = ?SETS:add_element(
|
|
|
|
LBFrom,
|
|
|
|
StateData#state.pres_a),
|
|
|
|
StateData#state{pres_a = A};
|
|
|
|
false ->
|
|
|
|
StateData
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
process_presence_probe(From, To, NewStateData),
|
|
|
|
{false, Attrs, NewStateData};
|
2002-12-17 21:49:45 +01:00
|
|
|
"error" ->
|
2003-10-29 21:09:09 +01:00
|
|
|
NewA = remove_element(jlib:jid_tolower(From),
|
|
|
|
StateData#state.pres_a),
|
2003-01-17 20:58:42 +01:00
|
|
|
{true, Attrs, StateData#state{pres_a = NewA}};
|
2002-12-17 21:49:45 +01:00
|
|
|
"invisible" ->
|
|
|
|
Attrs1 = lists:keydelete("type", 1, Attrs),
|
|
|
|
{true, [{"type", "unavailable"} | Attrs1], StateData};
|
|
|
|
"subscribe" ->
|
2002-12-18 21:26:08 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
"subscribed" ->
|
2002-12-17 21:49:45 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
"unsubscribe" ->
|
2002-12-18 21:26:08 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
"unsubscribed" ->
|
2002-12-17 21:49:45 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
_ ->
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, To, Packet},
|
|
|
|
in]) of
|
2003-10-10 21:01:11 +02:00
|
|
|
allow ->
|
2006-03-16 02:02:44 +01:00
|
|
|
LFrom = jlib:jid_tolower(From),
|
|
|
|
LBFrom = jlib:jid_remove_resource(LFrom),
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Note contact availability
|
2008-12-08 15:10:55 +01:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"unavailable" ->
|
|
|
|
mod_caps:clear_caps(From);
|
|
|
|
_ ->
|
|
|
|
Caps = mod_caps:read_caps(Els),
|
|
|
|
mod_caps:note_caps(StateData#state.server, From, Caps)
|
|
|
|
end,
|
2006-03-16 02:02:44 +01:00
|
|
|
case ?SETS:is_element(
|
2008-12-08 15:10:55 +01:00
|
|
|
LFrom, StateData#state.pres_a) orelse
|
2006-03-16 02:02:44 +01:00
|
|
|
?SETS:is_element(
|
2008-12-08 15:10:55 +01:00
|
|
|
LBFrom, StateData#state.pres_a) of
|
2006-03-16 02:02:44 +01:00
|
|
|
true ->
|
2008-12-08 15:10:55 +01:00
|
|
|
{true, Attrs, StateData};
|
2006-03-16 02:02:44 +01:00
|
|
|
false ->
|
|
|
|
case ?SETS:is_element(
|
2008-12-08 15:10:55 +01:00
|
|
|
LFrom, StateData#state.pres_f) of
|
2006-03-16 02:02:44 +01:00
|
|
|
true ->
|
|
|
|
A = ?SETS:add_element(
|
|
|
|
LFrom,
|
2008-12-08 15:10:55 +01:00
|
|
|
StateData#state.pres_a),
|
2006-03-16 02:02:44 +01:00
|
|
|
{true, Attrs,
|
2008-12-08 15:10:55 +01:00
|
|
|
StateData#state{pres_a = A}};
|
2006-03-16 02:02:44 +01:00
|
|
|
false ->
|
|
|
|
case ?SETS:is_element(
|
2008-12-08 15:10:55 +01:00
|
|
|
LBFrom, StateData#state.pres_f) of
|
2006-03-16 02:02:44 +01:00
|
|
|
true ->
|
|
|
|
A = ?SETS:add_element(
|
|
|
|
LBFrom,
|
2008-12-08 15:10:55 +01:00
|
|
|
StateData#state.pres_a),
|
2006-03-16 02:02:44 +01:00
|
|
|
{true, Attrs,
|
2008-12-08 15:10:55 +01:00
|
|
|
StateData#state{pres_a = A}};
|
2006-03-16 02:02:44 +01:00
|
|
|
false ->
|
2008-12-08 15:10:55 +01:00
|
|
|
{true, Attrs, StateData}
|
2006-03-16 02:02:44 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
2003-10-10 21:01:11 +02:00
|
|
|
deny ->
|
|
|
|
{false, Attrs, StateData}
|
|
|
|
end
|
2002-12-17 21:49:45 +01:00
|
|
|
end;
|
2002-12-22 21:56:29 +01:00
|
|
|
"broadcast" ->
|
2003-08-03 21:09:40 +02:00
|
|
|
?DEBUG("broadcast~n~p~n", [Els]),
|
2003-12-17 21:13:21 +01:00
|
|
|
case Els of
|
|
|
|
[{item, IJID, ISubscription}] ->
|
|
|
|
{false, Attrs,
|
|
|
|
roster_change(IJID, ISubscription,
|
|
|
|
StateData)};
|
|
|
|
[{exit, Reason}] ->
|
|
|
|
{exit, Attrs, Reason};
|
2006-03-17 05:35:03 +01:00
|
|
|
[{privacy_list, PrivList, PrivListName}] ->
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_updated_list, StateData#state.server,
|
|
|
|
false,
|
|
|
|
[StateData#state.privacy_list,
|
|
|
|
PrivList]) of
|
|
|
|
false ->
|
|
|
|
{false, Attrs, StateData};
|
|
|
|
NewPL ->
|
|
|
|
PrivPushIQ =
|
|
|
|
#iq{type = set, xmlns = ?NS_PRIVACY,
|
2008-10-24 20:01:08 +02:00
|
|
|
id = "push" ++ randoms:get_string(),
|
2006-10-05 05:17:41 +02:00
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_PRIVACY}],
|
|
|
|
[{xmlelement, "list",
|
|
|
|
[{"name", PrivListName}],
|
|
|
|
[]}]}]},
|
|
|
|
PrivPushEl =
|
|
|
|
jlib:replace_from_to(
|
|
|
|
jlib:jid_remove_resource(
|
|
|
|
StateData#state.jid),
|
|
|
|
StateData#state.jid,
|
|
|
|
jlib:iq_to_xml(PrivPushIQ)),
|
|
|
|
send_element(StateData, PrivPushEl),
|
|
|
|
{false, Attrs, StateData#state{privacy_list = NewPL}}
|
2006-03-18 21:10:56 +01:00
|
|
|
end;
|
2003-12-17 21:13:21 +01:00
|
|
|
_ ->
|
|
|
|
{false, Attrs, StateData}
|
|
|
|
end;
|
2003-01-02 22:01:12 +01:00
|
|
|
"iq" ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{xmlns = ?NS_VCARD} ->
|
2005-10-15 04:45:26 +02:00
|
|
|
Host = StateData#state.server,
|
|
|
|
case ets:lookup(sm_iqtable, {?NS_VCARD, Host}) of
|
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:handle(Host, Module, Function, Opts,
|
|
|
|
From, To, IQ);
|
|
|
|
[] ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end,
|
2003-01-02 22:01:12 +01:00
|
|
|
{false, Attrs, StateData};
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{} ->
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, To, Packet},
|
|
|
|
in]) of
|
2003-08-18 21:17:28 +02:00
|
|
|
allow ->
|
|
|
|
{true, Attrs, StateData};
|
|
|
|
deny ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
{false, Attrs, StateData}
|
|
|
|
end;
|
2003-01-02 22:01:12 +01:00
|
|
|
_ ->
|
|
|
|
{true, Attrs, StateData}
|
|
|
|
end;
|
2003-08-18 21:17:28 +02:00
|
|
|
"message" ->
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, To, Packet},
|
|
|
|
in]) of
|
2003-08-18 21:17:28 +02:00
|
|
|
allow ->
|
|
|
|
{true, Attrs, StateData};
|
|
|
|
deny ->
|
|
|
|
{false, Attrs, StateData}
|
|
|
|
end;
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
|
|
|
{true, Attrs, StateData}
|
|
|
|
end,
|
2003-01-29 18:12:23 +01:00
|
|
|
if
|
|
|
|
Pass == exit ->
|
2006-07-28 18:18:50 +02:00
|
|
|
catch send_text(StateData, ?STREAM_TRAILER),
|
2003-01-29 18:12:23 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
Pass ->
|
2002-12-17 21:49:45 +01:00
|
|
|
Attrs2 = jlib:replace_from_to_attrs(jlib:jid_to_string(From),
|
|
|
|
jlib:jid_to_string(To),
|
|
|
|
NewAttrs),
|
2004-08-13 00:34:19 +02:00
|
|
|
FixedPacket = {xmlelement, Name, Attrs2, Els},
|
|
|
|
Text = xml:element_to_string(FixedPacket),
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, Text),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_hooks:run(user_receive_packet,
|
2005-06-20 05:18:13 +02:00
|
|
|
StateData#state.server,
|
2004-08-13 00:34:19 +02:00
|
|
|
[StateData#state.jid, From, To, FixedPacket]),
|
2007-09-14 16:20:56 +02:00
|
|
|
ejabberd_hooks:run(c2s_loop_debug, [{route, From, To, Packet}]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, NewState);
|
2003-01-29 18:12:23 +01:00
|
|
|
true ->
|
2007-09-14 16:20:56 +02:00
|
|
|
ejabberd_hooks:run(c2s_loop_debug, [{route, From, To, Packet}]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, NewState)
|
2006-01-13 02:55:20 +01:00
|
|
|
end;
|
2007-04-12 08:08:32 +02:00
|
|
|
handle_info({'DOWN', Monitor, _Type, _Object, _Info}, _StateName, StateData)
|
|
|
|
when Monitor == StateData#state.socket_monitor ->
|
|
|
|
{stop, normal, StateData};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_info(Info, StateName, StateData) ->
|
|
|
|
?ERROR_MSG("Unexpected info: ~p", [Info]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, StateData).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
2003-12-17 21:13:21 +01:00
|
|
|
terminate(_Reason, StateName, StateData) ->
|
2003-11-07 21:51:23 +01:00
|
|
|
case StateName of
|
|
|
|
session_established ->
|
2005-07-15 00:28:21 +02:00
|
|
|
case StateData#state.authenticated of
|
2003-12-02 21:20:53 +01:00
|
|
|
replaced ->
|
|
|
|
?INFO_MSG("(~w) Replaced session for ~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
jlib:jid_to_string(StateData#state.jid)]),
|
|
|
|
From = StateData#state.jid,
|
|
|
|
Packet = {xmlelement, "presence",
|
|
|
|
[{"type", "unavailable"}],
|
|
|
|
[{xmlelement, "status", [],
|
|
|
|
[{xmlcdata, "Replaced by new connection"}]}]},
|
2006-01-24 00:13:06 +01:00
|
|
|
ejabberd_sm:close_session_unset_presence(
|
|
|
|
StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource,
|
|
|
|
"Replaced by new connection"),
|
2003-12-02 21:20:53 +01:00
|
|
|
presence_broadcast(
|
|
|
|
StateData, From, StateData#state.pres_a, Packet),
|
|
|
|
presence_broadcast(
|
|
|
|
StateData, From, StateData#state.pres_i, Packet);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("(~w) Close session for ~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
jlib:jid_to_string(StateData#state.jid)]),
|
2004-07-11 22:51:54 +02:00
|
|
|
|
2006-01-24 00:13:06 +01:00
|
|
|
EmptySet = ?SETS:new(),
|
2004-07-11 22:51:54 +02:00
|
|
|
case StateData of
|
|
|
|
#state{pres_last = undefined,
|
2006-01-24 00:13:06 +01:00
|
|
|
pres_a = EmptySet,
|
|
|
|
pres_i = EmptySet,
|
2004-07-11 22:51:54 +02:00
|
|
|
pres_invis = false} ->
|
2006-04-07 02:39:24 +02:00
|
|
|
ejabberd_sm:close_session(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource);
|
2004-07-11 22:51:54 +02:00
|
|
|
_ ->
|
|
|
|
From = StateData#state.jid,
|
|
|
|
Packet = {xmlelement, "presence",
|
|
|
|
[{"type", "unavailable"}], []},
|
2006-01-24 00:13:06 +01:00
|
|
|
ejabberd_sm:close_session_unset_presence(
|
|
|
|
StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource,
|
|
|
|
""),
|
2004-07-11 22:51:54 +02:00
|
|
|
presence_broadcast(
|
|
|
|
StateData, From, StateData#state.pres_a, Packet),
|
|
|
|
presence_broadcast(
|
|
|
|
StateData, From, StateData#state.pres_i, Packet)
|
|
|
|
end
|
2003-12-02 21:20:53 +01:00
|
|
|
end;
|
2003-11-07 21:51:23 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
2002-11-30 19:46:16 +01:00
|
|
|
end,
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):close(StateData#state.socket),
|
2002-11-18 21:39:47 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-02-09 20:17:23 +01:00
|
|
|
change_shaper(StateData, JID) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Shaper = acl:match_rule(StateData#state.server,
|
|
|
|
StateData#state.shaper, JID),
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):change_shaper(StateData#state.socket, Shaper).
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, Text) ->
|
2006-04-01 18:05:20 +02:00
|
|
|
?DEBUG("Send XML on stream = ~p", [lists:flatten(Text)]),
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):send(StateData#state.socket, Text).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData, El) ->
|
|
|
|
send_text(StateData, xml:element_to_string(El)).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2002-11-20 21:19:20 +01:00
|
|
|
new_id() ->
|
2002-12-13 21:58:27 +01:00
|
|
|
randoms:get_string().
|
2002-11-20 21:19:20 +01:00
|
|
|
|
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
is_auth_packet(El) ->
|
|
|
|
case jlib:iq_query_info(El) of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{id = ID, type = Type, xmlns = ?NS_AUTH, sub_el = SubEl} ->
|
2003-01-16 21:24:53 +01:00
|
|
|
{xmlelement, _, _, Els} = SubEl,
|
2003-03-09 21:46:47 +01:00
|
|
|
{auth, ID, Type,
|
2003-01-16 21:24:53 +01:00
|
|
|
get_auth_tags(Els, "", "", "", "")};
|
|
|
|
_ ->
|
2002-11-20 21:19:20 +01:00
|
|
|
false
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
get_auth_tags([{xmlelement, Name, _Attrs, Els}| L], U, P, D, R) ->
|
2002-11-20 21:19:20 +01:00
|
|
|
CData = xml:get_cdata(Els),
|
|
|
|
case Name of
|
|
|
|
"username" ->
|
|
|
|
get_auth_tags(L, CData, P, D, R);
|
|
|
|
"password" ->
|
|
|
|
get_auth_tags(L, U, CData, D, R);
|
|
|
|
"digest" ->
|
|
|
|
get_auth_tags(L, U, P, CData, R);
|
|
|
|
"resource" ->
|
|
|
|
get_auth_tags(L, U, P, D, CData);
|
|
|
|
_ ->
|
|
|
|
get_auth_tags(L, U, P, D, R)
|
|
|
|
end;
|
|
|
|
get_auth_tags([_ | L], U, P, D, R) ->
|
|
|
|
get_auth_tags(L, U, P, D, R);
|
|
|
|
get_auth_tags([], U, P, D, R) ->
|
|
|
|
{U, P, D, R}.
|
|
|
|
|
2008-02-15 17:35:32 +01:00
|
|
|
get_conn_type(StateData) ->
|
2008-02-18 10:55:06 +01:00
|
|
|
case (StateData#state.sockmod):get_sockmod(StateData#state.socket) of
|
|
|
|
gen_tcp -> c2s;
|
|
|
|
tls -> c2s_tls;
|
|
|
|
ejabberd_zlib -> c2s_compressed;
|
2008-02-15 17:35:32 +01:00
|
|
|
ejabberd_http_poll -> http_poll;
|
|
|
|
ejabberd_http_bind -> http_bind;
|
2008-02-18 10:55:06 +01:00
|
|
|
_ -> unknown
|
2008-02-15 17:35:32 +01:00
|
|
|
end.
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
process_presence_probe(From, To, StateData) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LFrom = jlib:jid_tolower(From),
|
2003-11-26 20:18:42 +01:00
|
|
|
LBFrom = setelement(3, LFrom, ""),
|
2002-12-17 21:49:45 +01:00
|
|
|
case StateData#state.pres_last of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
Cond1 = (not StateData#state.pres_invis)
|
2003-11-26 20:18:42 +01:00
|
|
|
andalso (?SETS:is_element(LFrom, StateData#state.pres_f)
|
|
|
|
orelse
|
|
|
|
((LFrom /= LBFrom) andalso
|
|
|
|
?SETS:is_element(LBFrom, StateData#state.pres_f)))
|
|
|
|
andalso (not
|
|
|
|
(?SETS:is_element(LFrom, StateData#state.pres_i)
|
|
|
|
orelse
|
|
|
|
((LFrom /= LBFrom) andalso
|
|
|
|
?SETS:is_element(LBFrom, StateData#state.pres_i)))),
|
2002-12-17 21:49:45 +01:00
|
|
|
Cond2 = StateData#state.pres_invis
|
2003-11-26 20:18:42 +01:00
|
|
|
andalso ?SETS:is_element(LFrom, StateData#state.pres_f)
|
|
|
|
andalso ?SETS:is_element(LFrom, StateData#state.pres_a),
|
2002-12-17 21:49:45 +01:00
|
|
|
if
|
|
|
|
Cond1 ->
|
2003-10-10 21:01:11 +02:00
|
|
|
Packet = StateData#state.pres_last,
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{To, From, Packet},
|
|
|
|
out]) of
|
2003-10-10 21:01:11 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2008-01-11 02:39:10 +01:00
|
|
|
Pid=element(2, StateData#state.sid),
|
|
|
|
ejabberd_hooks:run(presence_probe_hook, StateData#state.server, [From, To, Pid]),
|
2008-04-04 00:35:26 +02:00
|
|
|
%% Don't route a presence probe to oneself
|
|
|
|
case From == To of
|
|
|
|
false ->
|
|
|
|
ejabberd_router:route(To, From, Packet);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
2003-10-10 21:01:11 +02:00
|
|
|
end;
|
2002-12-17 21:49:45 +01:00
|
|
|
Cond2 ->
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[],
|
|
|
|
[]});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
presence_update(From, Packet, StateData) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
{xmlelement, _Name, Attrs, _Els} = Packet,
|
2002-12-17 21:49:45 +01:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"unavailable" ->
|
2004-07-11 22:51:54 +02:00
|
|
|
Status = case xml:get_subtag(Packet, "status") of
|
|
|
|
false ->
|
|
|
|
"";
|
|
|
|
StatusTag ->
|
|
|
|
xml:get_tag_cdata(StatusTag)
|
|
|
|
end,
|
2008-07-16 20:05:46 +02:00
|
|
|
Info = [{ip, StateData#state.ip}, {conn, StateData#state.conn},
|
|
|
|
{auth_module, StateData#state.auth_module}],
|
2006-01-24 00:13:06 +01:00
|
|
|
ejabberd_sm:unset_presence(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.server,
|
2004-07-11 22:51:54 +02:00
|
|
|
StateData#state.resource,
|
2007-05-21 05:21:34 +02:00
|
|
|
Status,
|
2008-02-15 17:35:32 +01:00
|
|
|
Info),
|
2003-10-10 21:01:11 +02:00
|
|
|
presence_broadcast(StateData, From, StateData#state.pres_a, Packet),
|
|
|
|
presence_broadcast(StateData, From, StateData#state.pres_i, Packet),
|
2002-12-17 21:49:45 +01:00
|
|
|
StateData#state{pres_last = undefined,
|
|
|
|
pres_a = ?SETS:new(),
|
|
|
|
pres_i = ?SETS:new(),
|
|
|
|
pres_invis = false};
|
|
|
|
"invisible" ->
|
2008-06-10 01:43:47 +02:00
|
|
|
NewPriority = get_priority_from_presence(Packet),
|
|
|
|
update_priority(NewPriority, Packet, StateData),
|
2002-12-17 21:49:45 +01:00
|
|
|
NewState =
|
|
|
|
if
|
|
|
|
not StateData#state.pres_invis ->
|
2003-10-10 21:01:11 +02:00
|
|
|
presence_broadcast(StateData, From,
|
|
|
|
StateData#state.pres_a,
|
2002-12-17 21:49:45 +01:00
|
|
|
Packet),
|
2003-10-10 21:01:11 +02:00
|
|
|
presence_broadcast(StateData, From,
|
|
|
|
StateData#state.pres_i,
|
2002-12-17 21:49:45 +01:00
|
|
|
Packet),
|
|
|
|
S1 = StateData#state{pres_last = undefined,
|
|
|
|
pres_a = ?SETS:new(),
|
|
|
|
pres_i = ?SETS:new(),
|
2003-01-17 20:58:42 +01:00
|
|
|
pres_invis = true},
|
2002-12-17 21:49:45 +01:00
|
|
|
presence_broadcast_first(From, S1, Packet);
|
|
|
|
true ->
|
|
|
|
StateData
|
|
|
|
end,
|
2003-01-17 20:58:42 +01:00
|
|
|
NewState;
|
2002-12-17 21:49:45 +01:00
|
|
|
"error" ->
|
|
|
|
StateData;
|
2003-11-26 20:18:42 +01:00
|
|
|
"probe" ->
|
|
|
|
StateData;
|
2002-12-20 21:42:08 +01:00
|
|
|
"subscribe" ->
|
|
|
|
StateData;
|
|
|
|
"subscribed" ->
|
|
|
|
StateData;
|
|
|
|
"unsubscribe" ->
|
|
|
|
StateData;
|
|
|
|
"unsubscribed" ->
|
|
|
|
StateData;
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
2006-04-23 16:57:37 +02:00
|
|
|
OldPriority = case StateData#state.pres_last of
|
|
|
|
undefined ->
|
|
|
|
0;
|
|
|
|
OldPresence ->
|
|
|
|
get_priority_from_presence(OldPresence)
|
|
|
|
end,
|
|
|
|
NewPriority = get_priority_from_presence(Packet),
|
2007-05-03 17:45:24 +02:00
|
|
|
update_priority(NewPriority, Packet, StateData),
|
2002-12-17 21:49:45 +01:00
|
|
|
FromUnavail = (StateData#state.pres_last == undefined) or
|
|
|
|
StateData#state.pres_invis,
|
|
|
|
?DEBUG("from unavail = ~p~n", [FromUnavail]),
|
|
|
|
NewState =
|
|
|
|
if
|
|
|
|
FromUnavail ->
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_hooks:run(user_available_hook,
|
2005-06-20 05:18:13 +02:00
|
|
|
StateData#state.server,
|
2004-08-13 00:34:19 +02:00
|
|
|
[StateData#state.jid]),
|
2006-11-28 10:59:29 +01:00
|
|
|
if NewPriority >= 0 ->
|
|
|
|
resend_offline_messages(StateData),
|
|
|
|
resend_subscription_requests(StateData);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2003-01-17 20:58:42 +01:00
|
|
|
presence_broadcast_first(
|
|
|
|
From, StateData#state{pres_last = Packet,
|
|
|
|
pres_invis = false
|
|
|
|
}, Packet);
|
2002-12-17 21:49:45 +01:00
|
|
|
true ->
|
2003-10-10 21:01:11 +02:00
|
|
|
presence_broadcast_to_trusted(StateData,
|
|
|
|
From,
|
2002-12-17 21:49:45 +01:00
|
|
|
StateData#state.pres_f,
|
|
|
|
StateData#state.pres_a,
|
|
|
|
Packet),
|
2006-04-23 16:57:37 +02:00
|
|
|
if OldPriority < 0, NewPriority >= 0 ->
|
|
|
|
resend_offline_messages(StateData);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2003-01-17 20:58:42 +01:00
|
|
|
StateData#state{pres_last = Packet,
|
|
|
|
pres_invis = false
|
|
|
|
}
|
2002-12-17 21:49:45 +01:00
|
|
|
end,
|
2003-01-17 20:58:42 +01:00
|
|
|
NewState
|
2002-12-17 21:49:45 +01:00
|
|
|
end.
|
|
|
|
|
2002-12-18 21:26:08 +01:00
|
|
|
presence_track(From, To, Packet, StateData) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
{xmlelement, _Name, Attrs, _Els} = Packet,
|
2002-12-20 21:42:08 +01:00
|
|
|
LTo = jlib:jid_tolower(To),
|
|
|
|
User = StateData#state.user,
|
2005-04-17 20:08:34 +02:00
|
|
|
Server = StateData#state.server,
|
2002-12-18 21:26:08 +01:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"unavailable" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(From, To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
I = remove_element(LTo, StateData#state.pres_i),
|
|
|
|
A = remove_element(LTo, StateData#state.pres_a),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData#state{pres_i = I,
|
|
|
|
pres_a = A};
|
|
|
|
"invisible" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(From, To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
I = ?SETS:add_element(LTo, StateData#state.pres_i),
|
|
|
|
A = remove_element(LTo, StateData#state.pres_a),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData#state{pres_i = I,
|
|
|
|
pres_a = A};
|
|
|
|
"subscribe" ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ejabberd_hooks:run(roster_out_subscription,
|
2005-06-20 05:18:13 +02:00
|
|
|
Server,
|
2005-04-17 20:08:34 +02:00
|
|
|
[User, Server, To, subscribe]),
|
2006-03-05 05:55:43 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
|
|
|
"subscribed" ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ejabberd_hooks:run(roster_out_subscription,
|
2005-06-20 05:18:13 +02:00
|
|
|
Server,
|
2005-04-17 20:08:34 +02:00
|
|
|
[User, Server, To, subscribed]),
|
2006-03-05 05:55:43 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
|
|
|
"unsubscribe" ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ejabberd_hooks:run(roster_out_subscription,
|
2005-06-20 05:18:13 +02:00
|
|
|
Server,
|
2005-04-17 20:08:34 +02:00
|
|
|
[User, Server, To, unsubscribe]),
|
2006-03-05 05:55:43 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
|
|
|
"unsubscribed" ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ejabberd_hooks:run(roster_out_subscription,
|
2005-06-20 05:18:13 +02:00
|
|
|
Server,
|
2005-04-17 20:08:34 +02:00
|
|
|
[User, Server, To, unsubscribed]),
|
2006-03-05 05:55:43 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
StateData;
|
|
|
|
"error" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(From, To, Packet),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
2003-11-26 20:18:42 +01:00
|
|
|
"probe" ->
|
|
|
|
ejabberd_router:route(From, To, Packet),
|
|
|
|
StateData;
|
2002-12-18 21:26:08 +01:00
|
|
|
_ ->
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, To, Packet},
|
|
|
|
out]) of
|
2003-10-10 21:01:11 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2003-10-10 21:01:11 +02:00
|
|
|
ejabberd_router:route(From, To, Packet)
|
|
|
|
end,
|
2002-12-20 21:42:08 +01:00
|
|
|
I = remove_element(LTo, StateData#state.pres_i),
|
|
|
|
A = ?SETS:add_element(LTo, StateData#state.pres_a),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData#state{pres_i = I,
|
|
|
|
pres_a = A}
|
|
|
|
end.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2003-10-10 21:01:11 +02:00
|
|
|
presence_broadcast(StateData, From, JIDSet, Packet) ->
|
2002-12-17 21:49:45 +01:00
|
|
|
lists:foreach(fun(JID) ->
|
2003-10-10 21:01:11 +02:00
|
|
|
FJID = jlib:make_jid(JID),
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, FJID, Packet},
|
|
|
|
out]) of
|
2003-10-10 21:01:11 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2003-10-10 21:01:11 +02:00
|
|
|
ejabberd_router:route(From, FJID, Packet)
|
2002-12-17 21:49:45 +01:00
|
|
|
end
|
2003-10-10 21:01:11 +02:00
|
|
|
end, ?SETS:to_list(JIDSet)).
|
|
|
|
|
|
|
|
presence_broadcast_to_trusted(StateData, From, T, A, Packet) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(JID) ->
|
|
|
|
case ?SETS:is_element(JID, T) of
|
|
|
|
true ->
|
|
|
|
FJID = jlib:make_jid(JID),
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, FJID, Packet},
|
|
|
|
out]) of
|
2003-10-10 21:01:11 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2003-10-10 21:01:11 +02:00
|
|
|
ejabberd_router:route(From, FJID, Packet)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, ?SETS:to_list(A)).
|
2002-12-17 21:49:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
presence_broadcast_first(From, StateData, Packet) ->
|
|
|
|
?SETS:fold(fun(JID, X) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
ejabberd_router:route(
|
2003-12-24 22:05:45 +01:00
|
|
|
From,
|
2003-10-07 22:31:44 +02:00
|
|
|
jlib:make_jid(JID),
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "probe"}],
|
|
|
|
[]}),
|
2002-12-17 21:49:45 +01:00
|
|
|
X
|
|
|
|
end,
|
|
|
|
[],
|
|
|
|
StateData#state.pres_t),
|
|
|
|
if
|
|
|
|
StateData#state.pres_invis ->
|
|
|
|
StateData;
|
|
|
|
true ->
|
2003-10-10 21:01:11 +02:00
|
|
|
As = ?SETS:fold(
|
|
|
|
fun(JID, A) ->
|
|
|
|
FJID = jlib:make_jid(JID),
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, FJID, Packet},
|
|
|
|
out]) of
|
2003-10-10 21:01:11 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2003-10-10 21:01:11 +02:00
|
|
|
ejabberd_router:route(From, FJID, Packet)
|
2002-12-17 21:49:45 +01:00
|
|
|
end,
|
2003-10-10 21:01:11 +02:00
|
|
|
?SETS:add_element(JID, A)
|
|
|
|
end,
|
|
|
|
StateData#state.pres_a,
|
|
|
|
StateData#state.pres_f),
|
2002-12-17 21:49:45 +01:00
|
|
|
StateData#state{pres_a = As}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2002-12-18 21:26:08 +01:00
|
|
|
remove_element(E, Set) ->
|
|
|
|
case ?SETS:is_element(E, Set) of
|
|
|
|
true ->
|
|
|
|
?SETS:del_element(E, Set);
|
|
|
|
_ ->
|
|
|
|
Set
|
|
|
|
end.
|
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2002-12-22 21:56:29 +01:00
|
|
|
roster_change(IJID, ISubscription, StateData) ->
|
|
|
|
LIJID = jlib:jid_tolower(IJID),
|
2003-01-10 20:44:35 +01:00
|
|
|
IsFrom = (ISubscription == both) or (ISubscription == from),
|
|
|
|
IsTo = (ISubscription == both) or (ISubscription == to),
|
2006-03-31 01:30:05 +02:00
|
|
|
OldIsFrom = ?SETS:is_element(LIJID, StateData#state.pres_f),
|
2003-01-10 20:44:35 +01:00
|
|
|
FSet = if
|
|
|
|
IsFrom ->
|
|
|
|
?SETS:add_element(LIJID, StateData#state.pres_f);
|
|
|
|
true ->
|
|
|
|
remove_element(LIJID, StateData#state.pres_f)
|
|
|
|
end,
|
|
|
|
TSet = if
|
|
|
|
IsTo ->
|
|
|
|
?SETS:add_element(LIJID, StateData#state.pres_t);
|
|
|
|
true ->
|
|
|
|
remove_element(LIJID, StateData#state.pres_t)
|
|
|
|
end,
|
2002-12-22 21:56:29 +01:00
|
|
|
case StateData#state.pres_last of
|
2003-12-23 12:33:31 +01:00
|
|
|
undefined ->
|
2003-01-10 20:44:35 +01:00
|
|
|
StateData#state{pres_f = FSet, pres_t = TSet};
|
2002-12-22 21:56:29 +01:00
|
|
|
P ->
|
|
|
|
?DEBUG("roster changed for ~p~n", [StateData#state.user]),
|
2003-10-07 22:31:44 +02:00
|
|
|
From = StateData#state.jid,
|
2006-03-31 01:30:05 +02:00
|
|
|
To = jlib:make_jid(IJID),
|
|
|
|
Cond1 = (not StateData#state.pres_invis) and IsFrom
|
|
|
|
and (not OldIsFrom),
|
|
|
|
Cond2 = (not IsFrom) and OldIsFrom
|
2003-01-10 20:44:35 +01:00
|
|
|
and (?SETS:is_element(LIJID, StateData#state.pres_a) or
|
|
|
|
?SETS:is_element(LIJID, StateData#state.pres_i)),
|
2002-12-22 21:56:29 +01:00
|
|
|
if
|
|
|
|
Cond1 ->
|
|
|
|
?DEBUG("C1: ~p~n", [LIJID]),
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, To, P},
|
|
|
|
out]) of
|
2006-03-31 01:30:05 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2006-03-31 01:30:05 +02:00
|
|
|
ejabberd_router:route(From, To, P)
|
|
|
|
end,
|
2002-12-22 21:56:29 +01:00
|
|
|
A = ?SETS:add_element(LIJID,
|
|
|
|
StateData#state.pres_a),
|
2003-01-10 20:44:35 +01:00
|
|
|
StateData#state{pres_a = A,
|
|
|
|
pres_f = FSet,
|
|
|
|
pres_t = TSet};
|
2002-12-22 21:56:29 +01:00
|
|
|
Cond2 ->
|
|
|
|
?DEBUG("C2: ~p~n", [LIJID]),
|
2006-03-31 01:30:05 +02:00
|
|
|
PU = {xmlelement, "presence",
|
|
|
|
[{"type", "unavailable"}], []},
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, To, PU},
|
|
|
|
out]) of
|
2006-03-31 01:30:05 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2006-03-31 01:30:05 +02:00
|
|
|
ejabberd_router:route(From, To, PU)
|
|
|
|
end,
|
2002-12-22 21:56:29 +01:00
|
|
|
I = remove_element(LIJID,
|
|
|
|
StateData#state.pres_i),
|
|
|
|
A = remove_element(LIJID,
|
|
|
|
StateData#state.pres_a),
|
|
|
|
StateData#state{pres_i = I,
|
2003-01-10 20:44:35 +01:00
|
|
|
pres_a = A,
|
|
|
|
pres_f = FSet,
|
|
|
|
pres_t = TSet};
|
2002-12-22 21:56:29 +01:00
|
|
|
true ->
|
2003-01-10 20:44:35 +01:00
|
|
|
StateData#state{pres_f = FSet, pres_t = TSet}
|
2002-12-22 21:56:29 +01:00
|
|
|
end
|
|
|
|
end.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
|
|
|
|
2007-05-03 17:45:24 +02:00
|
|
|
update_priority(Priority, Packet, StateData) ->
|
2008-07-16 20:05:46 +02:00
|
|
|
Info = [{ip, StateData#state.ip}, {conn, StateData#state.conn},
|
|
|
|
{auth_module, StateData#state.auth_module}],
|
2006-01-24 00:13:06 +01:00
|
|
|
ejabberd_sm:set_presence(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.server,
|
2002-12-30 21:25:08 +01:00
|
|
|
StateData#state.resource,
|
2007-05-03 17:45:24 +02:00
|
|
|
Priority,
|
2007-05-21 05:21:34 +02:00
|
|
|
Packet,
|
2008-02-15 17:35:32 +01:00
|
|
|
Info).
|
2004-09-17 21:52:59 +02:00
|
|
|
|
2006-04-23 16:57:37 +02:00
|
|
|
get_priority_from_presence(PresencePacket) ->
|
|
|
|
case xml:get_subtag(PresencePacket, "priority") of
|
|
|
|
false ->
|
|
|
|
0;
|
|
|
|
SubEl ->
|
|
|
|
case catch list_to_integer(xml:get_tag_cdata(SubEl)) of
|
|
|
|
P when is_integer(P) ->
|
|
|
|
P;
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end.
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
process_privacy_iq(From, To,
|
|
|
|
#iq{type = Type, sub_el = SubEl} = IQ,
|
|
|
|
StateData) ->
|
2003-08-03 21:09:40 +02:00
|
|
|
{Res, NewStateData} =
|
|
|
|
case Type of
|
|
|
|
get ->
|
2006-10-05 05:17:41 +02:00
|
|
|
R = ejabberd_hooks:run_fold(
|
|
|
|
privacy_iq_get, StateData#state.server,
|
|
|
|
{error, ?ERR_FEATURE_NOT_IMPLEMENTED},
|
|
|
|
[From, To, IQ, StateData#state.privacy_list]),
|
|
|
|
{R, StateData};
|
2003-08-03 21:09:40 +02:00
|
|
|
set ->
|
2006-10-05 05:17:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_iq_set, StateData#state.server,
|
|
|
|
{error, ?ERR_FEATURE_NOT_IMPLEMENTED},
|
|
|
|
[From, To, IQ]) of
|
2003-08-03 21:09:40 +02:00
|
|
|
{result, R, NewPrivList} ->
|
|
|
|
{{result, R},
|
|
|
|
StateData#state{privacy_list = NewPrivList}};
|
|
|
|
R -> {R, StateData}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
IQRes =
|
|
|
|
case Res of
|
|
|
|
{result, Result} ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result, sub_el = Result};
|
2003-08-03 21:09:40 +02:00
|
|
|
{error, Error} ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
2003-08-03 21:09:40 +02:00
|
|
|
end,
|
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, jlib:iq_to_xml(IQRes)),
|
|
|
|
NewStateData.
|
|
|
|
|
|
|
|
|
2004-08-13 00:34:19 +02:00
|
|
|
resend_offline_messages(#state{user = User,
|
2005-04-17 20:08:34 +02:00
|
|
|
server = Server,
|
2004-08-13 00:34:19 +02:00
|
|
|
privacy_list = PrivList} = StateData) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
case ejabberd_hooks:run_fold(resend_offline_messages_hook,
|
|
|
|
Server,
|
|
|
|
[],
|
2005-04-17 20:08:34 +02:00
|
|
|
[User, Server]) of
|
2004-01-18 21:42:09 +01:00
|
|
|
Rs when list(Rs) ->
|
|
|
|
lists:foreach(
|
2004-08-13 00:34:19 +02:00
|
|
|
fun({route,
|
|
|
|
From, To, {xmlelement, Name, Attrs, Els} = Packet}) ->
|
2006-10-05 05:17:41 +02:00
|
|
|
Pass = case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, Server,
|
|
|
|
allow,
|
|
|
|
[User,
|
|
|
|
Server,
|
|
|
|
PrivList,
|
|
|
|
{From, To, Packet},
|
|
|
|
in]) of
|
2004-08-13 00:34:19 +02:00
|
|
|
allow ->
|
|
|
|
true;
|
|
|
|
deny ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
Pass ->
|
|
|
|
Attrs2 = jlib:replace_from_to_attrs(
|
|
|
|
jlib:jid_to_string(From),
|
|
|
|
jlib:jid_to_string(To),
|
|
|
|
Attrs),
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, Name, Attrs2, Els});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
2004-01-18 21:42:09 +01:00
|
|
|
end, Rs)
|
|
|
|
end.
|
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
resend_subscription_requests(#state{user = User,
|
|
|
|
server = Server} = StateData) ->
|
|
|
|
PendingSubscriptions = ejabberd_hooks:run_fold(
|
|
|
|
resend_subscription_requests_hook,
|
|
|
|
Server,
|
|
|
|
[],
|
|
|
|
[User, Server]),
|
|
|
|
lists:foreach(fun(XMLPacket) ->
|
|
|
|
send_element(StateData,
|
|
|
|
XMLPacket)
|
|
|
|
end,
|
|
|
|
PendingSubscriptions).
|
|
|
|
|
2004-03-10 22:54:54 +01:00
|
|
|
get_showtag(undefined) ->
|
|
|
|
"unavailable";
|
|
|
|
get_showtag(Presence) ->
|
|
|
|
case xml:get_path_s(Presence, [{elem, "show"}, cdata]) of
|
|
|
|
"" -> "available";
|
|
|
|
ShowTag -> ShowTag
|
|
|
|
end.
|
2004-01-18 21:42:09 +01:00
|
|
|
|
2004-03-10 22:54:54 +01:00
|
|
|
get_statustag(undefined) ->
|
|
|
|
"";
|
|
|
|
get_statustag(Presence) ->
|
|
|
|
case xml:get_path_s(Presence, [{elem, "status"}, cdata]) of
|
|
|
|
ShowTag -> ShowTag
|
|
|
|
end.
|
2005-07-15 00:28:21 +02:00
|
|
|
|
|
|
|
process_unauthenticated_stanza(StateData, El) ->
|
|
|
|
case jlib:iq_query_info(El) of
|
|
|
|
#iq{} = IQ ->
|
|
|
|
Res = ejabberd_hooks:run_fold(c2s_unauthenticated_iq,
|
|
|
|
StateData#state.server,
|
|
|
|
empty,
|
2008-04-22 23:51:32 +02:00
|
|
|
[StateData#state.server, IQ,
|
|
|
|
StateData#state.ip]),
|
2005-07-15 00:28:21 +02:00
|
|
|
case Res of
|
|
|
|
empty ->
|
|
|
|
% The only reasonable IQ's here are auth and register IQ's
|
|
|
|
% They contain secrets, so don't include subelements to response
|
|
|
|
ResIQ = IQ#iq{type = error,
|
2006-07-28 18:18:50 +02:00
|
|
|
sub_el = [?ERR_SERVICE_UNAVAILABLE]},
|
2005-07-15 00:28:21 +02:00
|
|
|
Res1 = jlib:replace_from_to(
|
|
|
|
jlib:make_jid("", StateData#state.server, ""),
|
|
|
|
jlib:make_jid("", "", ""),
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
send_element(StateData, jlib:remove_attr("to", Res1));
|
2007-10-30 18:38:40 +01:00
|
|
|
_ ->
|
2005-07-15 00:28:21 +02:00
|
|
|
send_element(StateData, Res)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
% Drop any stanza, which isn't IQ stanza
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2007-05-21 05:21:34 +02:00
|
|
|
peerip(SockMod, Socket) ->
|
|
|
|
IP = case SockMod of
|
|
|
|
gen_tcp -> inet:peername(Socket);
|
|
|
|
_ -> SockMod:peername(Socket)
|
|
|
|
end,
|
|
|
|
case IP of
|
|
|
|
{ok, IPOK} -> IPOK;
|
|
|
|
_ -> undefined
|
|
|
|
end.
|
2007-09-14 16:20:56 +02:00
|
|
|
|
2007-09-14 16:18:01 +02:00
|
|
|
%% fsm_next_state: Generate the next_state FSM tuple with different
|
|
|
|
%% timeout, depending on the future state
|
|
|
|
fsm_next_state(session_established, StateData) ->
|
|
|
|
{next_state, session_established, StateData, ?C2S_HIBERNATE_TIMEOUT};
|
|
|
|
fsm_next_state(StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData, ?C2S_OPEN_TIMEOUT}.
|
|
|
|
|
|
|
|
%% fsm_reply: Generate the reply FSM tuple with different timeout,
|
|
|
|
%% depending on the future state
|
|
|
|
fsm_reply(Reply, session_established, StateData) ->
|
|
|
|
{reply, Reply, session_established, StateData, ?C2S_HIBERNATE_TIMEOUT};
|
|
|
|
fsm_reply(Reply, StateName, StateData) ->
|
|
|
|
{reply, Reply, StateName, StateData, ?C2S_OPEN_TIMEOUT}.
|
2008-05-05 18:22:06 +02:00
|
|
|
|
|
|
|
%% Used by c2s blacklist plugins
|
|
|
|
is_ip_blacklisted({IP,_Port}) ->
|
|
|
|
ejabberd_hooks:run_fold(check_bl_c2s, false, [IP]).
|
2008-12-08 10:21:36 +01:00
|
|
|
|
|
|
|
%% Check from attributes
|
|
|
|
%% returns invalid-from|NewElement
|
|
|
|
check_from(El, FromJID) ->
|
|
|
|
case xml:get_tag_attr("from", El) of
|
|
|
|
false ->
|
|
|
|
jlib:replace_from(FromJID, El);
|
|
|
|
{value, JIDElString} ->
|
|
|
|
JIDEl = jlib:string_to_jid(JIDElString),
|
|
|
|
case JIDEl#jid.lresource of
|
|
|
|
"" ->
|
|
|
|
%% Matching JID: The stanza is ok
|
|
|
|
if JIDEl#jid.luser == FromJID#jid.luser andalso
|
|
|
|
JIDEl#jid.lserver == FromJID#jid.lserver ->
|
|
|
|
El;
|
|
|
|
true ->
|
|
|
|
'invalid-from'
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
%% Matching JID: The stanza is ok
|
|
|
|
if JIDEl#jid.luser == FromJID#jid.luser andalso
|
|
|
|
JIDEl#jid.lserver == FromJID#jid.lserver andalso
|
|
|
|
JIDEl#jid.lresource == FromJID#jid.lresource ->
|
|
|
|
El;
|
|
|
|
true ->
|
|
|
|
'invalid-from'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|