2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_proxy65_service.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <xram@jabber.ru>
|
|
|
|
%%% Purpose : SOCKS5 Bytestreams XMPP service.
|
|
|
|
%%% Created : 12 Oct 2006 by Evgeniy Khramtsov <xram@jabber.ru>
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
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.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_proxy65_service).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-author('xram@jabber.ru').
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% gen_server callbacks.
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_info/2, handle_call/3,
|
|
|
|
handle_cast/2, terminate/2, code_change/3]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2016-07-29 12:21:00 +02:00
|
|
|
-export([start_link/2, add_listener/2, process_disco_info/1,
|
|
|
|
process_disco_items/1, process_vcard/1, process_bytestreams/1,
|
2015-06-03 15:05:17 +02:00
|
|
|
transform_module_options/1, delete_listener/1]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2008-07-13 21:10:01 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-29 12:21:00 +02:00
|
|
|
-include("xmpp.hrl").
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_proxy65_service).
|
|
|
|
|
2016-07-29 12:21:00 +02:00
|
|
|
-record(state, {myhost = <<"">> :: binary()}).
|
2008-10-12 13:17:35 +02:00
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%------------------------
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts], []).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
init([Host, Opts]) ->
|
2016-07-29 12:21:00 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
|
|
|
MyHost = gen_mod:get_opt_host(Host, Opts, <<"proxy.@HOST@">>),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_INFO,
|
|
|
|
?MODULE, process_disco_info, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_ITEMS,
|
|
|
|
?MODULE, process_disco_items, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_VCARD,
|
|
|
|
?MODULE, process_vcard, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_BYTESTREAMS,
|
|
|
|
?MODULE, process_bytestreams, IQDisc),
|
|
|
|
ejabberd_router:register_route(MyHost, Host),
|
|
|
|
{ok, #state{myhost = MyHost}}.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason, #state{myhost = MyHost}) ->
|
2016-07-29 12:21:00 +02:00
|
|
|
ejabberd_router:unregister_route(MyHost),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_INFO),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_ITEMS),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_VCARD),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_BYTESTREAMS).
|
|
|
|
|
|
|
|
handle_info({route, From, To, #iq{} = Packet}, State) ->
|
|
|
|
ejabberd_router:process_iq(From, To, Packet),
|
2006-10-28 04:04:55 +02:00
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2008-10-12 13:17:35 +02:00
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, ok, State}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_cast(_Request, State) -> {noreply, State}.
|
2008-10-12 13:17:35 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2008-10-12 13:17:35 +02:00
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% Listener management
|
|
|
|
%%%------------------------
|
|
|
|
|
|
|
|
add_listener(Host, Opts) ->
|
2009-01-12 21:03:02 +01:00
|
|
|
NewOpts = [Host | Opts],
|
2016-07-29 12:21:00 +02:00
|
|
|
ejabberd_listener:add_listener(get_port_ip(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_proxy65_stream, NewOpts).
|
2008-10-12 13:17:35 +02:00
|
|
|
|
|
|
|
delete_listener(Host) ->
|
2016-07-29 12:21:00 +02:00
|
|
|
catch ejabberd_listener:delete_listener(get_port_ip(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_proxy65_stream).
|
2008-10-12 13:17:35 +02:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%------------------------
|
|
|
|
%%% IQ Processing
|
|
|
|
%%%------------------------
|
2016-07-29 12:21:00 +02:00
|
|
|
-spec process_disco_info(iq()) -> iq().
|
|
|
|
process_disco_info(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_disco_info(#iq{type = get, to = To, lang = Lang} = IQ) ->
|
|
|
|
Host = ejabberd_router:host_of_route(To#jid.lserver),
|
|
|
|
Name = gen_mod:get_module_opt(Host, mod_proxy65, name,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"SOCKS5 Bytestreams">>),
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, Host,
|
|
|
|
[], [Host, ?MODULE, <<"">>, <<"">>]),
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ, #disco_info{xdata = Info,
|
|
|
|
identities = [#identity{category = <<"proxy">>,
|
|
|
|
type = <<"bytestreams">>,
|
|
|
|
name = translate:translate(Lang, Name)}],
|
|
|
|
features = [?NS_DISCO_INFO, ?NS_DISCO_ITEMS,
|
|
|
|
?NS_VCARD, ?NS_BYTESTREAMS]}).
|
|
|
|
|
|
|
|
-spec process_disco_items(iq()) -> iq().
|
|
|
|
process_disco_items(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_disco_items(#iq{type = get} = IQ) ->
|
|
|
|
xmpp:make_iq_result(IQ, #disco_items{}).
|
|
|
|
|
|
|
|
-spec process_vcard(iq()) -> iq().
|
|
|
|
process_vcard(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_vcard(#iq{type = get, lang = Lang} = IQ) ->
|
|
|
|
Desc = translate:translate(Lang, <<"ejabberd SOCKS5 Bytestreams module">>),
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ, #vcard_temp{fn = <<"ejabberd/mod_proxy65">>,
|
|
|
|
url = ?EJABBERD_URI,
|
2016-11-23 13:51:48 +01:00
|
|
|
desc = <<Desc/binary, $\n, ?COPYRIGHT>>}).
|
2016-07-29 12:21:00 +02:00
|
|
|
|
|
|
|
-spec process_bytestreams(iq()) -> iq().
|
|
|
|
process_bytestreams(#iq{type = get, from = JID, to = To, lang = Lang} = IQ) ->
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
ACL = gen_mod:get_module_opt(ServerHost, mod_proxy65, access,
|
|
|
|
fun acl:access_rules_validator/1,
|
|
|
|
all),
|
2006-10-28 04:04:55 +02:00
|
|
|
case acl:match_rule(ServerHost, ACL, JID) of
|
2016-07-29 12:21:00 +02:00
|
|
|
allow ->
|
|
|
|
StreamHost = get_streamhost(Host, ServerHost),
|
|
|
|
xmpp:make_iq_result(IQ, #bytestreams{hosts = [StreamHost]});
|
|
|
|
deny ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(<<"Denied by ACL">>, Lang))
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
2016-07-29 12:21:00 +02:00
|
|
|
process_bytestreams(#iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#bytestreams{sid = SID}]} = IQ)
|
|
|
|
when SID == <<"">> orelse length(SID) > 128 ->
|
|
|
|
Why = {bad_attr_value, <<"sid">>, <<"query">>, ?NS_BYTESTREAMS},
|
|
|
|
Txt = xmpp:format_error(Why),
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
|
|
|
process_bytestreams(#iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#bytestreams{activate = undefined}]} = IQ) ->
|
|
|
|
Why = {missing_cdata, <<"">>, <<"activate">>, ?NS_BYTESTREAMS},
|
|
|
|
Txt = xmpp:format_error(Why),
|
|
|
|
xmpp:make_error(IQ, xmpp:err_jid_malformed(Txt, Lang));
|
|
|
|
process_bytestreams(#iq{type = set, lang = Lang, from = InitiatorJID, to = To,
|
|
|
|
sub_els = [#bytestreams{activate = TargetJID,
|
|
|
|
sid = SID}]} = IQ) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(To#jid.lserver),
|
|
|
|
ACL = gen_mod:get_module_opt(ServerHost, mod_proxy65, access,
|
|
|
|
fun acl:access_rules_validator/1,
|
|
|
|
all),
|
2006-10-28 04:04:55 +02:00
|
|
|
case acl:match_rule(ServerHost, ACL, InitiatorJID) of
|
2016-07-29 12:21:00 +02:00
|
|
|
allow ->
|
|
|
|
Target = jid:to_string(jid:tolower(TargetJID)),
|
|
|
|
Initiator = jid:to_string(jid:tolower(InitiatorJID)),
|
|
|
|
SHA1 = p1_sha:sha(<<SID/binary, Initiator/binary, Target/binary>>),
|
|
|
|
case mod_proxy65_sm:activate_stream(SHA1, InitiatorJID,
|
|
|
|
TargetJID, ServerHost) of
|
|
|
|
ok ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
false ->
|
|
|
|
Txt = <<"Failed to activate bytestream">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang));
|
|
|
|
limit ->
|
|
|
|
Txt = <<"Too many active bytestreams">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_resource_constraint(Txt, Lang));
|
|
|
|
conflict ->
|
|
|
|
Txt = <<"Bytestream already activated">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_conflict(Txt, Lang));
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to activate bytestream from ~s to ~s: ~p",
|
|
|
|
[Initiator, Target, Err]),
|
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error())
|
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
Txt = <<"Denied by ACL">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
|
|
|
|
end.
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%-------------------------
|
|
|
|
%%% Auxiliary functions.
|
|
|
|
%%%-------------------------
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_module_options(Opts) ->
|
|
|
|
lists:map(
|
|
|
|
fun({ip, IP}) when is_tuple(IP) ->
|
|
|
|
{ip, jlib:ip_to_list(IP)};
|
|
|
|
({hostname, IP}) when is_tuple(IP) ->
|
|
|
|
{hostname, jlib:ip_to_list(IP)};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Opts).
|
|
|
|
|
2016-07-29 12:21:00 +02:00
|
|
|
-spec get_streamhost(binary(), binary()) -> streamhost().
|
|
|
|
get_streamhost(Host, ServerHost) ->
|
|
|
|
{Port, IP} = get_port_ip(ServerHost),
|
|
|
|
HostName = gen_mod:get_module_opt(ServerHost, mod_proxy65, hostname,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
jlib:ip_to_list(IP)),
|
|
|
|
#streamhost{jid = jid:make(Host),
|
|
|
|
host = HostName,
|
|
|
|
port = Port}.
|
|
|
|
|
|
|
|
-spec get_port_ip(binary()) -> {pos_integer(), inet:ip_address()}.
|
|
|
|
get_port_ip(Host) ->
|
|
|
|
Port = gen_mod:get_module_opt(Host, mod_proxy65, port,
|
|
|
|
fun(P) when is_integer(P), P>0, P<65536 ->
|
|
|
|
P
|
|
|
|
end,
|
|
|
|
7777),
|
|
|
|
IP = gen_mod:get_module_opt(Host, mod_proxy65, ip,
|
|
|
|
fun(S) ->
|
|
|
|
{ok, Addr} = inet_parse:address(
|
|
|
|
binary_to_list(
|
|
|
|
iolist_to_binary(S))),
|
|
|
|
Addr
|
|
|
|
end, get_my_ip()),
|
|
|
|
{Port, IP}.
|
|
|
|
|
|
|
|
-spec get_my_ip() -> inet:ip_address().
|
2009-02-27 04:22:40 +01:00
|
|
|
get_my_ip() ->
|
|
|
|
{ok, MyHostName} = inet:gethostname(),
|
|
|
|
case inet:getaddr(MyHostName, inet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Addr} -> Addr;
|
|
|
|
{error, _} -> {127, 0, 0, 1}
|
2007-08-25 19:24:00 +02:00
|
|
|
end.
|