25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Cope with malformed values in 'rosterusers' SQL table (#1466)

This commit is contained in:
Evgeniy Khramtsov 2017-01-18 11:59:46 +03:00
parent 2a68591181
commit 2de2d00f14

View File

@ -38,6 +38,7 @@
-include("mod_roster.hrl"). -include("mod_roster.hrl").
-include("ejabberd_sql_pt.hrl"). -include("ejabberd_sql_pt.hrl").
-include("logger.hrl").
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -117,7 +118,13 @@ get_roster_by_jid(LUser, LServer, LJID) ->
get_only_items(LUser, LServer) -> get_only_items(LUser, LServer) ->
case catch sql_queries:get_roster(LServer, LUser) of case catch sql_queries:get_roster(LServer, LUser) of
{selected, Is} when is_list(Is) -> {selected, Is} when is_list(Is) ->
lists:map(fun(I) -> raw_to_record(LServer, I) end, Is); lists:flatmap(
fun(I) ->
case raw_to_record(LServer, I) of
error -> [];
R -> [R]
end
end, Is);
_ -> [] _ -> []
end. end.
@ -132,14 +139,19 @@ get_roster_by_jid_with_groups(LUser, LServer, LJID) ->
SJID = jid:to_string(LJID), SJID = jid:to_string(LJID),
case sql_queries:get_roster_by_jid(LServer, LUser, SJID) of case sql_queries:get_roster_by_jid(LServer, LUser, SJID) of
{selected, [I]} -> {selected, [I]} ->
R = raw_to_record(LServer, I), case raw_to_record(LServer, I) of
Groups = error ->
case sql_queries:get_roster_groups(LServer, LUser, SJID) of #roster{usj = {LUser, LServer, LJID},
{selected, JGrps} when is_list(JGrps) -> us = {LUser, LServer}, jid = LJID};
[JGrp || {JGrp} <- JGrps]; R ->
_ -> [] Groups =
end, case sql_queries:get_roster_groups(LServer, LUser, SJID) of
R#roster{groups = Groups}; {selected, JGrps} when is_list(JGrps) ->
[JGrp || {JGrp} <- JGrps];
_ -> []
end,
R#roster{groups = Groups}
end;
{selected, []} -> {selected, []} ->
#roster{usj = {LUser, LServer, LJID}, #roster{usj = {LUser, LServer, LJID},
us = {LUser, LServer}, jid = LJID} us = {LUser, LServer}, jid = LJID}
@ -168,7 +180,13 @@ read_subscription_and_groups(LUser, LServer, LJID) ->
<<"B">> -> both; <<"B">> -> both;
<<"T">> -> to; <<"T">> -> to;
<<"F">> -> from; <<"F">> -> from;
_ -> none <<"N">> -> none;
<<"">> -> none;
_ ->
?ERROR_MSG("~s", [format_row_error(
LUser, LServer,
{subscription, SSubscription})]),
none
end, end,
Groups = case catch sql_queries:get_rostergroup_by_jid( Groups = case catch sql_queries:get_rostergroup_by_jid(
LServer, LUser, SJID) of LServer, LUser, SJID) of
@ -217,22 +235,34 @@ raw_to_record(LServer,
{User, SJID, Nick, SSubscription, SAsk, SAskMessage, {User, SJID, Nick, SSubscription, SAsk, SAskMessage,
_SServer, _SSubscribe, _SType}) -> _SServer, _SSubscribe, _SType}) ->
case jid:from_string(SJID) of case jid:from_string(SJID) of
error -> error; error ->
?ERROR_MSG("~s", [format_row_error(User, LServer, {jid, SJID})]),
error;
JID -> JID ->
LJID = jid:tolower(JID), LJID = jid:tolower(JID),
Subscription = case SSubscription of Subscription = case SSubscription of
<<"B">> -> both; <<"B">> -> both;
<<"T">> -> to; <<"T">> -> to;
<<"F">> -> from; <<"F">> -> from;
_ -> none <<"N">> -> none;
<<"">> -> none;
_ ->
?ERROR_MSG("~s", [format_row_error(
User, LServer,
{subscription, SSubscription})]),
none
end, end,
Ask = case SAsk of Ask = case SAsk of
<<"S">> -> subscribe; <<"S">> -> subscribe;
<<"U">> -> unsubscribe; <<"U">> -> unsubscribe;
<<"B">> -> both; <<"B">> -> both;
<<"O">> -> out; <<"O">> -> out;
<<"I">> -> in; <<"I">> -> in;
_ -> none <<"N">> -> none;
<<"">> -> none;
_ ->
?ERROR_MSG("~s", [format_row_error(User, LServer, {ask, SAsk})]),
none
end, end,
#roster{usj = {User, LServer, LJID}, #roster{usj = {User, LServer, LJID},
us = {User, LServer}, jid = LJID, name = Nick, us = {User, LServer}, jid = LJID, name = Nick,
@ -260,3 +290,11 @@ record_to_row(
none -> <<"N">> none -> <<"N">>
end, end,
{LUser, SJID, Name, SSubscription, SAsk, AskMessage}. {LUser, SJID, Name, SSubscription, SAsk, AskMessage}.
format_row_error(User, Server, Why) ->
[case Why of
{jid, JID} -> ["Malformed 'jid' field with value '", JID, "'"];
{subscription, Sub} -> ["Malformed 'subscription' field with value '", Sub, "'"];
{ask, Ask} -> ["Malformed 'ask' field with value '", Ask, "'"]
end,
" detected for ", User, "@", Server, " in table 'rosterusers'"].