]> granicus.if.org Git - ejabberd/commitdiff
Add support for XEP-0328: JID Prep
authorHolger Weiss <holger@zedat.fu-berlin.de>
Thu, 12 Sep 2019 07:26:45 +0000 (09:26 +0200)
committerHolger Weiss <holger@zedat.fu-berlin.de>
Thu, 12 Sep 2019 07:26:45 +0000 (09:26 +0200)
The mod_jidprep module implements XEP-0328: JID Prep, version 0.1.

rebar.config
src/mod_jidprep.erl [new file with mode: 0644]
test/ejabberd_SUITE.erl
test/ejabberd_SUITE_data/ejabberd.yml
test/jidprep_tests.erl [new file with mode: 0644]

index e05fe84e6e5da8d1d81935ebdb93c8a4c44b4420..ec068db1768c1ad1633ce6495aa18b91873d3cd4 100644 (file)
@@ -24,7 +24,7 @@
         {fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.1.2"}}},
         {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.17"}}},
         {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.37"}}},
-        {xmpp, ".*", {git, "https://github.com/processone/xmpp", {tag, "1.4.0"}}},
+        {xmpp, ".*", {git, "https://github.com/processone/xmpp", "e3181a5"}},
         {fast_yaml, ".*", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.20"}}},
        {yconf, ".*", {git, "https://github.com/processone/yconf", {tag, "1.0.0"}}},
         {jiffy, ".*", {git, "https://github.com/davisp/jiffy", {tag, "0.14.8"}}},
diff --git a/src/mod_jidprep.erl b/src/mod_jidprep.erl
new file mode 100644 (file)
index 0000000..d530b50
--- /dev/null
@@ -0,0 +1,148 @@
+%%%----------------------------------------------------------------------
+%%% File    : mod_jidprep.erl
+%%% Author  : Holger Weiss <holger@zedat.fu-berlin.de>
+%%% Purpose : JID Prep (XEP-0328)
+%%% Created : 11 Sep 2019 by Holger Weiss <holger@zedat.fu-berlin.de>
+%%%
+%%%
+%%% ejabberd, Copyright (C) 2019 ProcessOne
+%%%
+%%% 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.,
+%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+%%%
+%%%----------------------------------------------------------------------
+
+-module(mod_jidprep).
+-author('holger@zedat.fu-berlin.de').
+-protocol({xep, 328, '0.1'}).
+
+-behaviour(gen_mod).
+
+%% gen_mod callbacks.
+-export([start/2, stop/1, reload/3, mod_opt_type/1, mod_options/1, depends/2]).
+
+%% ejabberd_hooks callbacks.
+-export([disco_local_features/5]).
+
+%% gen_iq_handler callback.
+-export([process_iq/1]).
+
+-include("logger.hrl").
+-include("translate.hrl").
+-include("xmpp.hrl").
+
+%%--------------------------------------------------------------------
+%% gen_mod callbacks.
+%%--------------------------------------------------------------------
+-spec start(binary(), gen_mod:opts()) -> ok.
+start(Host, _Opts) ->
+    register_iq_handlers(Host),
+    register_hooks(Host).
+
+-spec stop(binary()) -> ok.
+stop(Host) ->
+    unregister_hooks(Host),
+    unregister_iq_handlers(Host).
+
+-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok.
+reload(_Host, _NewOpts, _OldOpts) ->
+    ok.
+
+-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
+depends(_Host, _Opts) ->
+    [].
+
+-spec mod_opt_type(atom()) -> econf:validator().
+mod_opt_type(access) ->
+    econf:acl().
+
+-spec mod_options(binary()) -> [{atom(), any()}].
+mod_options(_Host) ->
+    [{access, local}].
+
+%%--------------------------------------------------------------------
+%% Register/unregister hooks.
+%%--------------------------------------------------------------------
+-spec register_hooks(binary()) -> ok.
+register_hooks(Host) ->
+    ejabberd_hooks:add(disco_local_features, Host, ?MODULE,
+                      disco_local_features, 50).
+
+-spec unregister_hooks(binary()) -> ok.
+unregister_hooks(Host) ->
+    ejabberd_hooks:delete(disco_local_features, Host, ?MODULE,
+                         disco_local_features, 50).
+
+%%--------------------------------------------------------------------
+%% Service discovery.
+%%--------------------------------------------------------------------
+-spec disco_local_features(mod_disco:features_acc(), jid(), jid(), binary(),
+                          binary()) -> mod_disco:features_acc().
+disco_local_features(empty, From, To, Node, Lang) ->
+    disco_local_features({result, []}, From, To, Node, Lang);
+disco_local_features({result, OtherFeatures} = Acc, From,
+                    #jid{lserver = LServer}, <<"">>, _Lang) ->
+    Access = gen_mod:get_module_opt(LServer, ?MODULE, access),
+    case acl:match_rule(LServer, Access, From) of
+       allow ->
+           {result, [?NS_JIDPREP_0 | OtherFeatures]};
+       deny ->
+           Acc
+    end;
+disco_local_features(Acc, _From, _To, _Node, _Lang) ->
+    Acc.
+
+%%--------------------------------------------------------------------
+%% IQ handlers.
+%%--------------------------------------------------------------------
+-spec register_iq_handlers(binary()) -> ok.
+register_iq_handlers(Host) ->
+    gen_iq_handler:add_iq_handler(ejabberd_local, Host,
+                                 ?NS_JIDPREP_0, ?MODULE, process_iq).
+
+-spec unregister_iq_handlers(binary()) -> ok.
+unregister_iq_handlers(Host) ->
+    gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_JIDPREP_0).
+
+-spec process_iq(iq()) -> iq().
+process_iq(#iq{type = set, lang = Lang} = IQ) ->
+    Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
+    xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
+process_iq(#iq{from = From, to = #jid{lserver = LServer}, lang = Lang,
+              sub_els = [#jidprep{jid = #jid{luser = U,
+                                             lserver = S,
+                                             lresource = R} = JID}]} = IQ) ->
+    Access = gen_mod:get_module_opt(LServer, ?MODULE, access),
+    case acl:match_rule(LServer, Access, From) of
+       allow ->
+           case jid:make(U, S, R) of
+               #jid{} = Normalized ->
+                   ?DEBUG("Normalized JID for ~s: ~s",
+                          [jid:encode(From), jid:encode(JID)]),
+                   xmpp:make_iq_result(IQ, #jidprep{jid = Normalized});
+               error -> % Cannot happen.
+                   ?DEBUG("Normalizing JID failed for ~s: ~s",
+                          [jid:encode(From), jid:encode(JID)]),
+                   Txt = ?T("JID normalization failed"),
+                   xmpp:make_error(IQ, xmpp:err_jid_malformed(Txt, Lang))
+           end;
+       deny ->
+           ?DEBUG("Won't return normalized JID to ~s: ~s",
+                  [jid:encode(From), jid:encode(JID)]),
+           Txt = ?T("JID normalization denied by service policy"),
+            xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
+    end;
+process_iq(#iq{lang = Lang} = IQ) ->
+    Txt = ?T("No module is handling this query"),
+    xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
index 77d767a84fa5b57c10b269e99105827c50becaae..e67c6ca409ec1c0e9928d35ad92045719df74e1b 100644 (file)
@@ -352,6 +352,7 @@ no_db_tests() ->
      auth_external_wrong_jid,
      auth_external_wrong_server,
      auth_external_invalid_cert,
+     jidprep_tests:single_cases(),
      sm_tests:single_cases(),
      sm_tests:master_slave_cases(),
      muc_tests:single_cases(),
index 93c540b839998679ff0265bdac7828330a4c2de5..2bf090d5cfe76be7c7757c07c8669da24267cb8f 100644 (file)
@@ -106,6 +106,7 @@ modules:
     vcard: VCARD
   mod_muc_admin: []
   mod_carboncopy: []
+  mod_jidprep: []
   mod_mam: []
   mod_last: []
   mod_register:
diff --git a/test/jidprep_tests.erl b/test/jidprep_tests.erl
new file mode 100644 (file)
index 0000000..046f17b
--- /dev/null
@@ -0,0 +1,62 @@
+%%%-------------------------------------------------------------------
+%%% Author  : Holger Weiss <holger@zedat.fu-berlin.de>
+%%% Created : 11 Sep 2019 by Holger Weiss <holger@zedat.fu-berlin.de>
+%%%
+%%%
+%%% ejabberd, Copyright (C) 2019   ProcessOne
+%%%
+%%% 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.,
+%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+%%%
+%%%----------------------------------------------------------------------
+
+-module(jidprep_tests).
+
+%% API
+-compile(export_all).
+-import(suite, [send_recv/2, disconnect/1, is_feature_advertised/2,
+               server_jid/1]).
+
+-include("suite.hrl").
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+%%%===================================================================
+%%% Single user tests
+%%%===================================================================
+single_cases() ->
+    {jidprep_single, [sequence],
+     [single_test(feature_enabled),
+      single_test(normalize_jid)]}.
+
+feature_enabled(Config) ->
+    true = is_feature_advertised(Config, ?NS_JIDPREP_0),
+    disconnect(Config).
+
+normalize_jid(Config) ->
+    ServerJID = server_jid(Config),
+    OrigJID = jid:decode(<<"Romeo@Example.COM/Orchard">>),
+    NormJID = jid:decode(<<"romeo@example.com/Orchard">>),
+    Request = #jidprep{jid = OrigJID},
+    #iq{type = result, sub_els = [#jidprep{jid = NormJID}]} =
+        send_recv(Config, #iq{type = get, to = ServerJID,
+                             sub_els = [Request]}),
+    disconnect(Config).
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================
+single_test(T) ->
+    list_to_atom("jidprep_" ++ atom_to_list(T)).