From 31491ebe16706f9cefcc27c6722fc6e8e2b013b1 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Fri, 13 Jan 2017 11:35:47 +0300 Subject: [PATCH] Fix some corner cases while re-reading RFC6120 --- src/ejabberd_c2s.erl | 6 +++--- src/ejabberd_s2s_in.erl | 8 ++++---- src/xmpp_stream_in.erl | 21 +++++++++------------ src/xmpp_stream_out.erl | 6 +++--- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 582327d6c..ecd6321d4 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -224,9 +224,9 @@ handle_unexpected_cast(State, Msg) -> ?WARNING_MSG("got unexpected cast: ~p", [Msg]), State. -reject_unauthenticated_packet(State, Pkt) -> - Err = xmpp:err_not_authorized(), - xmpp_stream_in:send_error(State, Pkt, Err). +reject_unauthenticated_packet(State, _Pkt) -> + Err = xmpp:serr_not_authorized(), + send(State, Err). process_closed(State, Reason) -> stop(State#{stop_reason => Reason}). diff --git a/src/ejabberd_s2s_in.erl b/src/ejabberd_s2s_in.erl index 484d0a55a..f447cf9dd 100644 --- a/src/ejabberd_s2s_in.erl +++ b/src/ejabberd_s2s_in.erl @@ -115,9 +115,9 @@ handle_unexpected_cast(State, Msg) -> ?WARNING_MSG("got unexpected cast: ~p", [Msg]), State. -reject_unauthenticated_packet(State, Pkt) -> - Err = xmpp:err_not_authorized(), - xmpp_stream_in:send_error(State, Pkt, Err). +reject_unauthenticated_packet(State, _Pkt) -> + Err = xmpp:serr_not_authorized(), + send(State, Err). process_closed(State, _Reason) -> stop(State). @@ -299,7 +299,7 @@ check_from_to(From, To, State) -> true -> ok; false -> - {error, xmpp:serr_improper_addressing()} + {error, xmpp:serr_host_unknown()} end; false -> {error, xmpp:serr_invalid_from()} diff --git a/src/xmpp_stream_in.erl b/src/xmpp_stream_in.erl index b2b3b3072..b29848e7b 100644 --- a/src/xmpp_stream_in.erl +++ b/src/xmpp_stream_in.erl @@ -319,8 +319,8 @@ handle_info({'$gen_event', {xmlstreamerror, Reason}}, #{lang := Lang}= State) -> Err = case Reason of <<"XML stanza is too big">> -> xmpp:serr_policy_violation(Reason, Lang); - _ -> - xmpp:serr_not_well_formed() + {_, Txt} -> + xmpp:serr_not_well_formed(Txt, Lang) end, send_pkt(State1, Err) end); @@ -523,6 +523,8 @@ process_element(Pkt, #{stream_state := StateName, lang := Lang} = State) -> send_pkt(State, #sasl_failure{reason = 'encryption-required'}); #sasl_auth{} when StateName == wait_for_sasl_request -> process_sasl_request(Pkt, State); + #sasl_auth{} when StateName == wait_for_sasl_response -> + process_sasl_request(Pkt, maps:remove(sasl_state, State)); #sasl_auth{} -> Txt = <<"SASL negotiation is not allowed in this state">>, send_pkt(State, #sasl_failure{reason = 'not-authorized', @@ -570,8 +572,8 @@ process_unauthenticated_packet(Pkt, #{mod := Mod} = State) -> NewPkt = set_lang(Pkt, State), try Mod:handle_unauthenticated_packet(NewPkt, State) catch _:undef -> - Err = xmpp:err_not_authorized(), - send_error(State, Pkt, Err) + Err = xmpp:serr_not_authorized(), + send(State, Err) end. -spec process_authenticated_packet(xmpp_element(), state()) -> state(). @@ -993,7 +995,7 @@ set_from_to(Pkt, #{lang := Lang}) -> To = xmpp:get_to(Pkt), if From == undefined -> Txt = <<"Missing 'from' attribute">>, - {error, xmpp:serr_invalid_from(Txt, Lang)}; + {error, xmpp:serr_improper_addressing(Txt, Lang)}; To == undefined -> Txt = <<"Missing 'to' attribute">>, {error, xmpp:serr_improper_addressing(Txt, Lang)}; @@ -1010,18 +1012,13 @@ send_header(#{stream_id := StreamID, stream_version := MyVersion, stream_header_sent := false, lang := MyLang, - xmlns := NS, - server := DefaultServer} = State, + xmlns := NS} = State, #stream_start{to = HisTo, from = HisFrom, lang = HisLang, version = HisVersion}) -> Lang = select_lang(MyLang, HisLang), NS_DB = if NS == ?NS_SERVER -> ?NS_SERVER_DIALBACK; true -> <<"">> end, - From = case HisTo of - #jid{} -> HisTo; - undefined -> jid:make(DefaultServer) - end, Version = case HisVersion of undefined -> undefined; {0,_} -> HisVersion; @@ -1034,7 +1031,7 @@ send_header(#{stream_id := StreamID, db_xmlns = NS_DB, id = StreamID, to = HisFrom, - from = From}, + from = HisTo}, State1 = State#{lang => Lang, stream_version => Version, stream_header_sent => true}, diff --git a/src/xmpp_stream_out.erl b/src/xmpp_stream_out.erl index 3dcecf6f6..9ac4934dd 100644 --- a/src/xmpp_stream_out.erl +++ b/src/xmpp_stream_out.erl @@ -333,8 +333,8 @@ handle_info({'$gen_event', {xmlstreamerror, Reason}}, #{lang := Lang}= State) -> Err = case Reason of <<"XML stanza is too big">> -> xmpp:serr_policy_violation(Reason, Lang); - _ -> - xmpp:serr_not_well_formed() + {_, Txt} -> + xmpp:serr_not_well_formed(Txt, Lang) end, send_pkt(State1, Err) end); @@ -520,7 +520,7 @@ process_features(#stream_features{sub_els = Els} = StreamFeatures, send_pkt(State1, xmpp:serr_policy_violation(Txt, Lang)); #starttls{required = true} when not TLSAvailable and not Encrypted -> Txt = <<"Use of STARTTLS forbidden">>, - send_pkt(State1, xmpp:serr_policy_violation(Txt, Lang)); + send_pkt(State1, xmpp:serr_unsupported_feature(Txt, Lang)); #starttls{} when TLSAvailable and not Encrypted -> State2 = State1#{stream_state => wait_for_starttls_response}, send_pkt(State2, #starttls{});