Skip to content

Commit

Permalink
Merge pull request #3845 from esl/c2s/connect
Browse files Browse the repository at this point in the history
C2s/connect
  • Loading branch information
NelsonVides authored Nov 16, 2022
2 parents a354678 + 2a1d01a commit 1f1e4f9
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 484 deletions.
2 changes: 1 addition & 1 deletion big_tests/default.spec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{suites, "tests", carboncopy_SUITE}.
{suites, "tests", cluster_commands_SUITE}.
{suites, "tests", component_SUITE}.
% {suites, "tests", connect_SUITE}.
{suites, "tests", connect_SUITE}.
{suites, "tests", disco_and_caps_SUITE}.
{suites, "tests", extdisco_SUITE}.
{suites, "tests", gdpr_SUITE}.
Expand Down
2 changes: 0 additions & 2 deletions big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
{server, <<"domain.example.com">>},
{host, <<"localhost">>},
{password, <<"break_me">>},
{compression, <<"zlib">>},
{starttls, required}]},
{clusterguy, [
{username, <<"clusterguy">>},
Expand Down Expand Up @@ -166,7 +165,6 @@
{server, <<"domain.example.com">>},
{host, <<"localhost">>},
{password, <<"bringdowntheserver">>},
{compression, <<"zlib">>},
{port, 5223}]}
]}.

Expand Down
2 changes: 1 addition & 1 deletion big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

{suites, "tests", cluster_commands_SUITE}.

% {suites, "tests", connect_SUITE}.
{suites, "tests", connect_SUITE}.

{suites, "tests", disco_and_caps_SUITE}.

Expand Down
2 changes: 0 additions & 2 deletions big_tests/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
{username, <<"hacker">>},
{server, <<"localhost">>},
{password, <<"bringdowntheserver">>},
{compression, <<"zlib">>},
{port, 5223}]},
{admin, [
{username, <<"admin">>},
Expand All @@ -160,7 +159,6 @@
{username, <<"secure_joe">>},
{server, <<"localhost">>},
{password, <<"break_me">>},
{compression, <<"zlib">>},
{starttls, required}]},
{astrid, [
{username, <<"astrid">>},
Expand Down
322 changes: 80 additions & 242 deletions big_tests/tests/connect_SUITE.erl

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions big_tests/tests/sasl_external_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ end_per_suite(Config) ->
escalus:end_per_suite(Config).

init_per_group(just_tls, Config) ->
[{tls_module, "just_tls"} | Config];
[{tls_module, just_tls} | Config];
init_per_group(fast_tls, Config) ->
[{tls_module, "fast_tls"} | Config];
[{tls_module, fast_tls} | Config];
init_per_group(ca_signed, Config) ->
[{signed, ca},
{ssl_options, "\n tls.disconnect_on_failure = false"},
Expand Down Expand Up @@ -143,11 +143,11 @@ init_per_group(_, Config) ->
Config.

modify_config_and_restart(CyrsaslExternalConfig, Config) ->
TLSModule = escalus_config:get_config(tls_module, Config, "just_tls"),
TLSModule = escalus_config:get_config(tls_module, Config, just_tls),
VerifyMode = escalus_config:get_config(verify_mode, Config, ""),
SSLOpts = case TLSModule of
"just_tls" -> escalus_config:get_config(ssl_options, Config, "") ++ VerifyMode;
"fast_tls" -> ""
just_tls -> escalus_config:get_config(ssl_options, Config, "") ++ VerifyMode;
fast_tls -> ""
end,
AuthMethods = escalus_config:get_config(auth_methods, Config,
[{auth_method, "pki"}, {auth_method_opts, false}]),
Expand Down
7 changes: 0 additions & 7 deletions doc/configuration/release-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,3 @@ These options are inserted into the `rel/files/mongooseim.toml` template.
* **Option:** [`auth.methods`](auth.md#authmethods)
* **Syntax:** comma-separated list of strings
* **Example:** `{auth_method, "\"internal\""}.`

### zlib

* **Type:** parameter
* **Option:** [`listen.c2s.zlib`](listen.md#listenc2szlib)
* **Syntax:** positive integer
* **Example:** `{zlib, "10_000"}.`
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
[
%%% Stateless parsers
{base16, "2.0.1"},
{exml, "3.2.1", {pkg, hexml}},
{exml, "3.2.2", {pkg, hexml}},
{jiffy, "1.1.1"},
{jid, "2.0.0", {pkg, mongoose_jid}},
{tomerl, "0.5.0"},
Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{<<"eredis">>,{pkg,<<"eredis">>,<<"1.2.0">>},0},
{<<"erl_csv">>,{pkg,<<"erl_csv">>,<<"0.2.0">>},0},
{<<"erlcloud">>,{pkg,<<"erlcloud">>,<<"3.5.11">>},0},
{<<"exml">>,{pkg,<<"hexml">>,<<"3.2.1">>},0},
{<<"exml">>,{pkg,<<"hexml">>,<<"3.2.2">>},0},
{<<"exometer_core">>,
{git,"https://github.com/esl/exometer_core.git",
{ref,"123daa053a4abb3ff4bdbf52f08344da535294e9"}},
Expand Down Expand Up @@ -155,7 +155,7 @@
{<<"eredis">>, <<"0B8E9CFC2C00FA1374CD107EA63B49BE08D933DF2CF175E6A89B73DD9C380DE4">>},
{<<"erl_csv">>, <<"3006C931DFD9FC40AD6CC01D2971768B09E51BD345E90162C9241A31FCFE8EF0">>},
{<<"erlcloud">>, <<"F5ED26042A7A020B9E718DCDC21D3005AE08FED66857C0FD92012B3E21081871">>},
{<<"exml">>, <<"2B5E288658C92ACD4791E95838422CC70B51FD4AAC48355B4CF3E780404E6A7B">>},
{<<"exml">>, <<"64D99A630DC0161645DFF0D28B1C089A90827B7849232616636BC32AEBA76723">>},
{<<"fast_pbkdf2">>, <<"4F09D6C6C20DBEE1970E0A6AE91432E1B7731F88426C671D083BAC31FFA1FDAD">>},
{<<"fast_scram">>, <<"299A2D430955A62A94CB43B1A727C5D21A5C4BD11AEBA476AE2F3A24CFBE89C3">>},
{<<"fast_tls">>, <<"828CDC75E1E8FCE8158846D2B971D8B4FE2B2DDCC75B759E88D751079BF78AFD">>},
Expand Down Expand Up @@ -218,7 +218,7 @@
{<<"eredis">>, <<"D9B5ABEF2C2C8ABA8F32AA018203E0B3DC8B1157773B254AB1D4C2002317F1E1">>},
{<<"erl_csv">>, <<"175E413BE631C5D9B5D14282FE3A0AB712A0A5964AEB6ED3F1C0880DA2C89AFD">>},
{<<"erlcloud">>, <<"5D10F35D60436A7CF09A45A1E310C7A215CA0C7A0641435ABD880FA1638718D0">>},
{<<"exml">>, <<"CC63B8A0CD96C1CA20B5F0C0CCCA75DED99D681385896A10E3BC848423D0AAC9">>},
{<<"exml">>, <<"8AF1F6BEDECFD0137DC42887BFB9A3F80BB46D390178D6FEDA6ACDCFD0E14D87">>},
{<<"fast_pbkdf2">>, <<"2900431E2E6402F23A92754448BBD949DA366BC9C984FDC791DDCFCC41042434">>},
{<<"fast_scram">>, <<"4B30084E3BDB39158076381FC871035BEFD157D5EE614BDA5E19EA482855E5D5">>},
{<<"fast_tls">>, <<"D1F422AF40C7777FE534496F508EE86515CB929AD10F7D1D56AA94CE899B44A0">>},
Expand Down
6 changes: 5 additions & 1 deletion src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ stream_start_features_after_auth(#c2s_data{host_type = HostType, lserver = LServ

-spec handle_bind_resource(c2s_data(), c2s_state(), exml:element(), jlib:iq()) -> fsm_res().
handle_bind_resource(StateData, C2SState, El, #iq{sub_el = SubEl} = IQ) ->
case jid:resourceprep(exml_query:path(SubEl, [{element, <<"resource">>}, cdata])) of
Resource = case exml_query:path(SubEl, [{element, <<"resource">>}, cdata]) of
undefined -> <<>>;
Val -> jid:resourceprep(Val)
end,
case Resource of
error ->
Err = jlib:make_error_reply(El, mongoose_xmpp_errors:bad_request()),
send_element_from_server_jid(StateData, Err),
Expand Down
19 changes: 13 additions & 6 deletions src/c2s/mongoose_c2s_ranch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}).

-type state() :: #state{}.
-type transport() :: ranch_tcp | ranch_ssl | fast_tls.
-type transport() :: ranch_tcp | just_tls | fast_tls. % just_tls = ranch_ssl

-spec socket_new(term(), mongoose_listener:options()) -> state().
socket_new({ranch_tcp, RanchRef}, #{proxy_protocol := true}) ->
Expand Down Expand Up @@ -78,7 +78,7 @@ socket_handle_data(#state{transport = fast_tls, socket = TlsSocket}, {tcp, _Sock
{error, Reason} ->
{error, Reason}
end;
socket_handle_data(#state{transport = ranch_ssl, socket = Socket}, {ssl, Socket, Data}) ->
socket_handle_data(#state{transport = just_tls, socket = Socket}, {ssl, Socket, Data}) ->
mongoose_metrics:update(global, [data, xmpp, received, encrypted_size], iolist_size(Data)),
Data;
socket_handle_data(#state{transport = ranch_tcp, socket = Socket}, {tcp, Socket, Data}) ->
Expand All @@ -87,15 +87,15 @@ socket_handle_data(#state{transport = ranch_tcp, socket = Socket}, {tcp, Socket,
-spec socket_activate(state()) -> ok.
socket_activate(#state{transport = fast_tls, socket = Socket}) ->
fast_tls:setopts(Socket, [{active, once}]);
socket_activate(#state{transport = ranch_ssl, socket = Socket}) ->
socket_activate(#state{transport = just_tls, socket = Socket}) ->
ranch_ssl:setopts(Socket, [{active, once}]);
socket_activate(#state{transport = ranch_tcp, socket = Socket}) ->
ranch_tcp:setopts(Socket, [{active, once}]).

-spec socket_close(state()) -> ok.
socket_close(#state{transport = fast_tls, socket = Socket}) ->
fast_tls:close(Socket);
socket_close(#state{transport = ranch_ssl, socket = Socket}) ->
socket_close(#state{transport = just_tls, socket = Socket}) ->
ranch_ssl:close(Socket);
socket_close(#state{transport = ranch_tcp, socket = Socket}) ->
ranch_tcp:close(Socket).
Expand All @@ -104,14 +104,21 @@ socket_close(#state{transport = ranch_tcp, socket = Socket}) ->
ok | {error, term()}.
socket_send_xml(#state{transport = Transport, socket = Socket}, XML) ->
Text = exml:to_iolist(XML),
case Transport:send(Socket, Text) of
case send(Transport, Socket, Text) of
ok ->
mongoose_metrics:update(global, [data, xmpp, sent, xml_stanza_size], iolist_size(Text)),
ok;
Error ->
Error
end.

send(fast_tls, Socket, Data) ->
fast_tls:send(Socket, Data);
send(just_tls, Socket, Data) ->
ranch_ssl:send(Socket, Data);
send(ranch_tcp, Socket, Data) ->
ranch_tcp:send(Socket, Data).

-spec has_peer_cert(mongoose_c2s_socket:state(), mongoose_listener:options()) -> boolean().
has_peer_cert(#state{transport = fast_tls, socket = Socket}, #{tls := TlsOpts}) ->
case {fast_tls:get_verify_result(Socket), fast_tls:get_peer_certificate(Socket), TlsOpts} of
Expand All @@ -121,7 +128,7 @@ has_peer_cert(#state{transport = fast_tls, socket = Socket}, #{tls := TlsOpts})
{_, {ok, _}, _} -> false;
{_, error, _} -> false
end;
has_peer_cert(#state{transport = ranch_ssl, socket = Socket}, _) ->
has_peer_cert(#state{transport = just_tls, socket = Socket}, _) ->
case ssl:peercert(Socket) of
{ok, _PeerCert} -> true;
_ -> false
Expand Down
2 changes: 1 addition & 1 deletion src/c2s/mongoose_c2s_stanzas.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ stream_features_before_auth(HostType, LServer, LOpts, StateData) ->
%% http://xmpp.org/rfcs/rfc6120.html#tls-rules-mtn
determine_features(_, _, #{tls := #{mode := starttls_required}}, false, _StateData) ->
[starttls_stanza(required)];
determine_features(HostType, LServer, #{tls := #{mode := tls}}, _, StateData) ->
determine_features(HostType, LServer, _, true, StateData) ->
mongoose_hooks:c2s_stream_features(HostType, LServer) ++ maybe_sasl_mechanisms(HostType, StateData);
determine_features(HostType, LServer, _, _, StateData) ->
[starttls_stanza(optional)
Expand Down
3 changes: 1 addition & 2 deletions src/ejabberd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
| ejabberd_socket
| mod_bosh_socket
| mod_websockets
| mongoose_tls
| ejabberd_zlib.
| mongoose_tls.

%% Incoming event from XML stream. Used everywhere in xmlstream fsm modules
-type xml_stream_item() :: 'closed'
Expand Down
37 changes: 0 additions & 37 deletions src/ejabberd_receiver.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
change_shaper/2,
starttls/2,
get_socket/1,
compress/2,
become_controller/2,
close/1]).

Expand Down Expand Up @@ -90,9 +89,6 @@ starttls(Pid, TLSOpts) ->
get_socket(Pid) ->
gen_server_call_or_noproc(Pid, get_socket).

compress(Pid, ZlibSocket) ->
gen_server_call_or_noproc(Pid, {compress, ZlibSocket}).

become_controller(Pid, C2SPid) ->
gen_server:call(Pid, {become_controller, C2SPid}).

Expand Down Expand Up @@ -168,22 +164,6 @@ handle_call({starttls, TLSOpts}, From, #state{socket = TCPSocket} = State) ->
c2s_pid => State#state.c2s_pid}),
{stop, normal, State}
end;
handle_call({compress, ZlibSocket}, _From,
#state{c2s_pid = C2SPid} = State) ->
StateAfterReset = reset_parser(State),
NewState = StateAfterReset#state{socket = ZlibSocket,
sock_mod = ejabberd_zlib},
case ejabberd_zlib:recv_data(ZlibSocket, "") of
{ok, ZlibData} ->
NewState2 = process_data(ZlibData, NewState),
{reply, ok, NewState2, maybe_hibernate(NewState2)};
{error, inflate_size_exceeded} ->
apply(gen_fsm(), send_event,
[C2SPid, {xmlstreamerror, <<"child element too big">>}]),
{reply, ok, NewState, maybe_hibernate(NewState)};
{error, inflate_error} ->
{stop, normal, ok, NewState}
end;
handle_call({become_controller, C2SPid}, _From, State) ->
StateAfterReset = reset_parser(State),
NewState = StateAfterReset#state{c2s_pid = C2SPid},
Expand Down Expand Up @@ -217,7 +197,6 @@ handle_cast(_Msg, State) ->
%%--------------------------------------------------------------------
handle_info({Tag, _TCPSocket, Data},
#state{socket = Socket,
c2s_pid = C2SPid,
sock_mod = SockMod} = State)
when (Tag == tcp) or (Tag == ssl) ->
case SockMod of
Expand All @@ -231,20 +210,6 @@ handle_info({Tag, _TCPSocket, Data},
{error, _Reason} ->
{stop, normal, State}
end;
ejabberd_zlib ->
mongoose_metrics:update(global,
[data, xmpp, received, compressed_size], size(Data)),
case ejabberd_zlib:recv_data(Socket, Data) of
{ok, ZlibData} ->
NewState = process_data(ZlibData, State),
{noreply, NewState, maybe_hibernate(NewState)};
{error, inflate_size_exceeded} ->
apply(gen_fsm(), send_event,
[C2SPid, {xmlstreamerror, <<"child element too big">>}]),
{noreply, State, maybe_hibernate(State)};
{error, inflate_error} ->
{stop, normal, State}
end;
_ ->
NewState = process_data(Data, State),
{noreply, NewState, maybe_hibernate(NewState)}
Expand Down Expand Up @@ -418,8 +383,6 @@ gen_server_call_or_noproc(Pid, Message) ->
{error, {died, Extra}}
end.

gen_fsm() -> p1_fsm.

-spec hibernate() -> hibernate | infinity.
hibernate() ->
case process_info(self(), message_queue_len) of
Expand Down
13 changes: 1 addition & 12 deletions src/ejabberd_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
connect/4,
starttls/2,
starttls/3,
compress/3,
send/2,
send_xml/2,
change_shaper/2,
Expand Down Expand Up @@ -211,17 +210,7 @@ starttls(SocketData, TLSOpts, Data) ->
NewSocket = get_tls_socket(SocketData),
SocketData#socket_state{socket = NewSocket, sockmod = mongoose_tls}.

-spec compress(socket_state(), integer(), _) -> socket_state().
compress(SocketData, InflateSizeLimit, Data) ->
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
SocketData#socket_state.sockmod,
SocketData#socket_state.socket,
InflateSizeLimit),
ejabberd_receiver:compress(SocketData#socket_state.receiver, ZlibSocket),
send(SocketData, Data),
SocketData#socket_state{socket = ZlibSocket, sockmod = ejabberd_zlib}.

%% @doc sockmod=gen_tcp|fast_tls|ejabberd_zlib (ejabberd:sockmod())
%% @doc sockmod=gen_tcp|fast_tls (ejabberd:sockmod())
send(SocketData, Data) ->
case catch (SocketData#socket_state.sockmod):send(
SocketData#socket_state.socket, Data) of
Expand Down
Loading

0 comments on commit 1f1e4f9

Please sign in to comment.