2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_config.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2005-06-20 05:18:13 +02:00
|
|
|
%%% Purpose : Load config file
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 14 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 ProcessOne
|
2007-12-24 12:41:41 +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 12:41:41 +01:00
|
|
|
%%%
|
2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
-module(ejabberd_config).
|
2003-01-21 21:36:55 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
%% API
|
|
|
|
-export([get_option/1]).
|
|
|
|
-export([load/0, reload/0, format_error/1, path/0]).
|
|
|
|
-export([env_binary_to_list/2]).
|
|
|
|
-export([get_myname/0, get_uri/0, get_copyright/0]).
|
|
|
|
-export([get_shared_key/0, get_node_start/0]).
|
|
|
|
-export([fsm_limit_opts/1]).
|
2019-06-21 20:06:32 +02:00
|
|
|
-export([codec_options/0]).
|
2019-06-23 11:23:22 +02:00
|
|
|
-export([version/0]).
|
2019-06-15 11:10:35 +02:00
|
|
|
-export([default_db/2, default_db/3, default_ram_db/2, default_ram_db/3]).
|
2019-06-14 11:33:26 +02:00
|
|
|
-export([beams/1, validators/1, globals/0, may_hide_data/1]).
|
|
|
|
-export([dump/0, dump/1, convert_to_yaml/1, convert_to_yaml/2]).
|
2020-01-08 10:24:51 +01:00
|
|
|
-export([callback_modules/1]).
|
2023-01-21 13:59:33 +01:00
|
|
|
-export([set_option/2]).
|
2019-06-14 11:33:26 +02:00
|
|
|
|
|
|
|
%% Deprecated functions
|
2023-01-21 13:59:33 +01:00
|
|
|
-export([get_option/2]).
|
2019-06-14 11:33:26 +02:00
|
|
|
-export([get_version/0, get_myhosts/0]).
|
|
|
|
-export([get_mylang/0, get_lang/1]).
|
|
|
|
-deprecated([{get_option, 2},
|
|
|
|
{get_version, 0},
|
|
|
|
{get_myhosts, 0},
|
|
|
|
{get_mylang, 0},
|
|
|
|
{get_lang, 1}]).
|
2008-04-02 18:31:15 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-include("logger.hrl").
|
|
|
|
-include("ejabberd_stacktrace.hrl").
|
|
|
|
|
|
|
|
-type option() :: atom() | {atom(), global | binary()}.
|
|
|
|
-type error_reason() :: {merge_conflict, atom(), binary()} |
|
|
|
|
{old_config, file:filename_all(), term()} |
|
|
|
|
{write_file, file:filename_all(), term()} |
|
|
|
|
{exception, term(), term(), term()}.
|
|
|
|
-type error_return() :: {error, econf:error_reason(), term()} |
|
|
|
|
{error, error_reason()}.
|
2019-06-27 14:22:27 +02:00
|
|
|
-type host_config() :: #{{atom(), binary() | global} => term()}.
|
2019-06-14 11:33:26 +02:00
|
|
|
|
|
|
|
-callback opt_type(atom()) -> econf:validator().
|
|
|
|
-callback options() -> [atom() | {atom(), term()}].
|
|
|
|
-callback globals() -> [atom()].
|
2020-01-08 10:24:51 +01:00
|
|
|
-callback doc() -> any().
|
2019-06-14 11:33:26 +02:00
|
|
|
|
2019-06-17 19:51:04 +02:00
|
|
|
-optional_callbacks([globals/0]).
|
2019-06-14 11:33:26 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
-spec load() -> ok | error_return().
|
|
|
|
load() ->
|
|
|
|
load(path()).
|
|
|
|
|
2019-07-10 22:35:53 +02:00
|
|
|
-spec load(file:filename_all()) -> ok | error_return().
|
|
|
|
load(Path) ->
|
|
|
|
ConfigFile = unicode:characters_to_binary(Path),
|
2019-06-14 11:33:26 +02:00
|
|
|
UnixTime = erlang:monotonic_time(second),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Loading configuration from ~ts", [ConfigFile]),
|
2019-06-14 11:33:26 +02:00
|
|
|
_ = ets:new(ejabberd_options,
|
|
|
|
[named_table, public, {read_concurrency, true}]),
|
2018-05-09 10:44:24 +02:00
|
|
|
case load_file(ConfigFile) of
|
2019-06-14 11:33:26 +02:00
|
|
|
ok ->
|
|
|
|
set_shared_key(),
|
|
|
|
set_node_start(UnixTime),
|
|
|
|
?INFO_MSG("Configuration loaded successfully", []);
|
|
|
|
Err ->
|
2018-05-09 10:44:24 +02:00
|
|
|
Err
|
|
|
|
end.
|
2016-03-24 10:02:13 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec reload() -> ok | error_return().
|
|
|
|
reload() ->
|
2021-01-06 00:20:12 +01:00
|
|
|
ejabberd_systemd:reloading(),
|
2019-06-14 11:33:26 +02:00
|
|
|
ConfigFile = path(),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Reloading configuration from ~ts", [ConfigFile]),
|
2017-02-23 08:12:19 +01:00
|
|
|
OldHosts = get_myhosts(),
|
2021-01-06 00:20:12 +01:00
|
|
|
Res = case load_file(ConfigFile) of
|
|
|
|
ok ->
|
|
|
|
NewHosts = get_myhosts(),
|
|
|
|
AddHosts = NewHosts -- OldHosts,
|
|
|
|
DelHosts = OldHosts -- NewHosts,
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
ejabberd_hooks:run(host_up, [Host])
|
|
|
|
end, AddHosts),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
ejabberd_hooks:run(host_down, [Host])
|
|
|
|
end, DelHosts),
|
|
|
|
ejabberd_hooks:run(config_reloaded, []),
|
|
|
|
delete_host_options(DelHosts),
|
|
|
|
?INFO_MSG("Configuration reloaded successfully", []);
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("Configuration reload aborted: ~ts",
|
|
|
|
[format_error(Err)]),
|
|
|
|
Err
|
|
|
|
end,
|
|
|
|
ejabberd_systemd:ready(),
|
|
|
|
Res.
|
2015-01-21 15:18:46 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec dump() -> ok | error_return().
|
|
|
|
dump() ->
|
|
|
|
dump(stdout).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec dump(stdout | file:filename_all()) -> ok | error_return().
|
|
|
|
dump(Output) ->
|
|
|
|
Y = get_option(yaml_config),
|
|
|
|
dump(Y, Output).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec dump(term(), stdout | file:filename_all()) -> ok | error_return().
|
|
|
|
dump(Y, Output) ->
|
|
|
|
Data = fast_yaml:encode(Y),
|
2013-08-12 14:25:05 +02:00
|
|
|
case Output of
|
2019-06-14 11:33:26 +02:00
|
|
|
stdout ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io:format("~ts~n", [Data]);
|
2019-06-14 11:33:26 +02:00
|
|
|
FileName ->
|
|
|
|
try
|
|
|
|
ok = filelib:ensure_dir(FileName),
|
|
|
|
ok = file:write_file(FileName, Data)
|
|
|
|
catch _:{badmatch, {error, Reason}} ->
|
|
|
|
{error, {write_file, FileName, Reason}}
|
|
|
|
end
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_option(option(), term()) -> term().
|
|
|
|
get_option(Opt, Default) ->
|
2019-06-23 15:54:01 +02:00
|
|
|
try get_option(Opt)
|
|
|
|
catch _:badarg -> Default
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec get_option(option()) -> term().
|
|
|
|
get_option(Opt) when is_atom(Opt) ->
|
|
|
|
get_option({Opt, global});
|
2019-07-12 12:59:33 +02:00
|
|
|
get_option({O, Host} = Opt) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Tab = case get_tmp_config() of
|
|
|
|
undefined -> ejabberd_options;
|
|
|
|
T -> T
|
|
|
|
end,
|
2019-07-12 12:59:33 +02:00
|
|
|
try ets:lookup_element(Tab, Opt, 2)
|
|
|
|
catch ?EX_RULE(error, badarg, St) when Host /= global ->
|
|
|
|
StackTrace = ?EX_STACK(St),
|
|
|
|
Val = get_option({O, global}),
|
2019-09-25 12:58:05 +02:00
|
|
|
?DEBUG("Option '~ts' is not defined for virtual host '~ts'. "
|
|
|
|
"This is a bug, please report it with the following "
|
|
|
|
"stacktrace included:~n** ~ts",
|
|
|
|
[O, Host, misc:format_exception(2, error, badarg, StackTrace)]),
|
2019-07-12 12:59:33 +02:00
|
|
|
Val
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec set_option(option(), term()) -> ok.
|
|
|
|
set_option(Opt, Val) when is_atom(Opt) ->
|
|
|
|
set_option({Opt, global}, Val);
|
|
|
|
set_option(Opt, Val) ->
|
|
|
|
Tab = case get_tmp_config() of
|
|
|
|
undefined -> ejabberd_options;
|
|
|
|
T -> T
|
|
|
|
end,
|
|
|
|
ets:insert(Tab, {Opt, Val}),
|
|
|
|
ok.
|
2003-01-21 21:36:55 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_version() -> binary().
|
|
|
|
get_version() ->
|
|
|
|
get_option(version).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_myhosts() -> [binary(), ...].
|
|
|
|
get_myhosts() ->
|
|
|
|
get_option(hosts).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_myname() -> binary().
|
|
|
|
get_myname() ->
|
|
|
|
get_option(host).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_mylang() -> binary().
|
|
|
|
get_mylang() ->
|
|
|
|
get_lang(global).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_lang(global | binary()) -> binary().
|
|
|
|
get_lang(Host) ->
|
|
|
|
get_option({language, Host}).
|
2013-07-27 14:07:17 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_uri() -> binary().
|
|
|
|
get_uri() ->
|
|
|
|
<<"http://www.process-one.net/en/ejabberd/">>.
|
2013-07-27 14:07:17 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_copyright() -> binary().
|
|
|
|
get_copyright() ->
|
|
|
|
<<"Copyright (c) ProcessOne">>.
|
2017-04-29 10:39:40 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_shared_key() -> binary().
|
|
|
|
get_shared_key() ->
|
|
|
|
get_option(shared_key).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_node_start() -> integer().
|
|
|
|
get_node_start() ->
|
|
|
|
get_option(node_start).
|
2016-05-09 07:36:30 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec fsm_limit_opts([proplists:property()]) -> [{max_queue, pos_integer()}].
|
|
|
|
fsm_limit_opts(Opts) ->
|
|
|
|
case lists:keyfind(max_fsm_queue, 1, Opts) of
|
|
|
|
{_, I} when is_integer(I), I>0 ->
|
|
|
|
[{max_queue, I}];
|
|
|
|
false ->
|
|
|
|
case get_option(max_fsm_queue) of
|
|
|
|
undefined -> [];
|
|
|
|
N -> [{max_queue, N}]
|
|
|
|
end
|
2017-04-27 18:44:58 +02:00
|
|
|
end.
|
2016-04-27 16:10:50 +02:00
|
|
|
|
2019-06-21 20:06:32 +02:00
|
|
|
-spec codec_options() -> [xmpp:decode_option()].
|
|
|
|
codec_options() ->
|
|
|
|
case get_option(validate_stream) of
|
2019-06-14 11:33:26 +02:00
|
|
|
true -> [];
|
|
|
|
false -> [ignore_els]
|
2016-04-27 16:10:50 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-23 11:23:22 +02:00
|
|
|
%% Do not use this function in runtime:
|
|
|
|
%% It's slow and doesn't read 'version' option from the config.
|
|
|
|
%% Use ejabberd_option:version() instead.
|
|
|
|
-spec version() -> binary().
|
|
|
|
version() ->
|
|
|
|
case application:get_env(ejabberd, custom_vsn) of
|
|
|
|
{ok, Vsn0} when is_list(Vsn0) ->
|
|
|
|
list_to_binary(Vsn0);
|
|
|
|
{ok, Vsn1} when is_binary(Vsn1) ->
|
|
|
|
Vsn1;
|
|
|
|
_ ->
|
|
|
|
case application:get_key(ejabberd, vsn) of
|
|
|
|
undefined -> <<"">>;
|
|
|
|
{ok, Vsn} -> list_to_binary(Vsn)
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec default_db(binary() | global, module()) -> atom().
|
2016-04-27 16:10:50 +02:00
|
|
|
default_db(Host, Module) ->
|
2019-06-15 11:10:35 +02:00
|
|
|
default_db(default_db, Host, Module, mnesia).
|
2017-01-11 14:25:43 +01:00
|
|
|
|
2019-06-15 11:10:35 +02:00
|
|
|
-spec default_db(binary() | global, module(), atom()) -> atom().
|
|
|
|
default_db(Host, Module, Default) ->
|
|
|
|
default_db(default_db, Host, Module, Default).
|
2017-01-11 14:25:43 +01:00
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec default_ram_db(binary() | global, module()) -> atom().
|
2017-01-11 14:25:43 +01:00
|
|
|
default_ram_db(Host, Module) ->
|
2019-06-15 11:10:35 +02:00
|
|
|
default_db(default_ram_db, Host, Module, mnesia).
|
|
|
|
|
|
|
|
-spec default_ram_db(binary() | global, module(), atom()) -> atom().
|
|
|
|
default_ram_db(Host, Module, Default) ->
|
|
|
|
default_db(default_ram_db, Host, Module, Default).
|
2017-01-11 14:25:43 +01:00
|
|
|
|
2019-06-15 11:10:35 +02:00
|
|
|
-spec default_db(default_db | default_ram_db, binary() | global, module(), atom()) -> atom().
|
|
|
|
default_db(Opt, Host, Mod, Default) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Type = get_option({Opt, Host}),
|
|
|
|
DBMod = list_to_atom(atom_to_list(Mod) ++ "_" ++ atom_to_list(Type)),
|
|
|
|
case code:ensure_loaded(DBMod) of
|
|
|
|
{module, _} -> Type;
|
|
|
|
{error, _} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Module ~ts doesn't support database '~ts' "
|
|
|
|
"defined in option '~ts', using "
|
|
|
|
"'~ts' as fallback", [Mod, Type, Opt, Default]),
|
2019-06-15 11:10:35 +02:00
|
|
|
Default
|
2019-06-14 11:33:26 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec beams(local | external | all) -> [module()].
|
|
|
|
beams(local) ->
|
2015-06-01 14:38:27 +02:00
|
|
|
{ok, Mods} = application:get_key(ejabberd, modules),
|
2019-06-14 11:33:26 +02:00
|
|
|
Mods;
|
|
|
|
beams(external) ->
|
2015-09-10 20:11:02 +02:00
|
|
|
ExtMods = [Name || {Name, _Details} <- ext_mod:installed()],
|
2019-11-07 08:47:11 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(ExtMod) ->
|
|
|
|
ExtModPath = ext_mod:module_ebin_dir(ExtMod),
|
|
|
|
case lists:member(ExtModPath, code:get_path()) of
|
|
|
|
true -> ok;
|
|
|
|
false -> code:add_patha(ExtModPath)
|
|
|
|
end
|
|
|
|
end, ExtMods),
|
2017-11-16 14:48:47 +01:00
|
|
|
case application:get_env(ejabberd, external_beams) of
|
2019-06-14 11:33:26 +02:00
|
|
|
{ok, Path} ->
|
|
|
|
case lists:member(Path, code:get_path()) of
|
|
|
|
true -> ok;
|
|
|
|
false -> code:add_patha(Path)
|
|
|
|
end,
|
|
|
|
Beams = filelib:wildcard(filename:join(Path, "*\.beam")),
|
|
|
|
CustMods = [list_to_atom(filename:rootname(filename:basename(Beam)))
|
|
|
|
|| Beam <- Beams],
|
|
|
|
CustMods ++ ExtMods;
|
|
|
|
_ ->
|
|
|
|
ExtMods
|
|
|
|
end;
|
|
|
|
beams(all) ->
|
|
|
|
beams(local) ++ beams(external).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec may_hide_data(term()) -> term().
|
|
|
|
may_hide_data(Data) ->
|
|
|
|
case get_option(hide_sensitive_log_data) of
|
|
|
|
false -> Data;
|
|
|
|
true -> "hidden_by_ejabberd"
|
2009-02-16 18:57:30 +01:00
|
|
|
end.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
%% Some Erlang apps expects env parameters to be list and not binary.
|
|
|
|
%% For example, Mnesia is not able to start if mnesia dir is passed as a binary.
|
|
|
|
%% However, binary is most common on Elixir, so it is easy to make a setup mistake.
|
|
|
|
-spec env_binary_to_list(atom(), atom()) -> {ok, any()} | undefined.
|
|
|
|
env_binary_to_list(Application, Parameter) ->
|
|
|
|
%% Application need to be loaded to allow setting parameters
|
|
|
|
application:load(Application),
|
|
|
|
case application:get_env(Application, Parameter) of
|
|
|
|
{ok, Val} when is_binary(Val) ->
|
|
|
|
BVal = binary_to_list(Val),
|
|
|
|
application:set_env(Application, Parameter, BVal),
|
|
|
|
{ok, BVal};
|
|
|
|
Other ->
|
|
|
|
Other
|
2015-04-04 17:42:12 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec validators([atom()]) -> {econf:validators(), [atom()]}.
|
|
|
|
validators(Disallowed) ->
|
|
|
|
Modules = callback_modules(all),
|
|
|
|
Validators = lists:foldl(
|
|
|
|
fun(M, Vs) ->
|
|
|
|
maps:merge(Vs, validators(M, Disallowed))
|
|
|
|
end, #{}, Modules),
|
|
|
|
Required = lists:flatmap(
|
|
|
|
fun(M) ->
|
|
|
|
[O || O <- M:options(), is_atom(O)]
|
|
|
|
end, Modules),
|
|
|
|
{Validators, Required}.
|
|
|
|
|
|
|
|
-spec convert_to_yaml(file:filename()) -> ok | error_return().
|
|
|
|
convert_to_yaml(File) ->
|
|
|
|
convert_to_yaml(File, stdout).
|
2015-01-29 18:43:47 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec convert_to_yaml(file:filename(),
|
|
|
|
stdout | file:filename()) -> ok | error_return().
|
|
|
|
convert_to_yaml(File, Output) ->
|
|
|
|
case read_erlang_file(File, []) of
|
|
|
|
{ok, Y} ->
|
|
|
|
dump(Y, Output);
|
|
|
|
Err ->
|
|
|
|
Err
|
2016-09-08 16:29:19 +02:00
|
|
|
end.
|
2016-09-08 11:34:42 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec format_error(error_return()) -> string().
|
|
|
|
format_error({error, Reason, Ctx}) ->
|
|
|
|
econf:format_error(Reason, Ctx);
|
|
|
|
format_error({error, {merge_conflict, Opt, Host}}) ->
|
|
|
|
lists:flatten(
|
|
|
|
io_lib:format(
|
2019-09-23 14:17:20 +02:00
|
|
|
"Cannot merge value of option '~ts' defined in append_host_config "
|
|
|
|
"for virtual host ~ts: only options of type list or map are allowed "
|
2019-06-14 11:33:26 +02:00
|
|
|
"in append_host_config. Hint: specify the option in host_config",
|
|
|
|
[Opt, Host]));
|
|
|
|
format_error({error, {old_config, Path, Reason}}) ->
|
|
|
|
lists:flatten(
|
|
|
|
io_lib:format(
|
2019-09-23 14:17:20 +02:00
|
|
|
"Failed to read configuration from '~ts': ~ts~ts",
|
|
|
|
[Path,
|
2019-06-14 11:33:26 +02:00
|
|
|
case Reason of
|
|
|
|
{_, _, _} -> "at line ";
|
|
|
|
_ -> ""
|
|
|
|
end, file:format_error(Reason)]));
|
|
|
|
format_error({error, {write_file, Path, Reason}}) ->
|
|
|
|
lists:flatten(
|
|
|
|
io_lib:format(
|
2019-09-23 14:17:20 +02:00
|
|
|
"Failed to write to '~ts': ~ts",
|
|
|
|
[Path,
|
2019-07-10 22:35:53 +02:00
|
|
|
file:format_error(Reason)]));
|
2019-06-14 11:33:26 +02:00
|
|
|
format_error({error, {exception, Class, Reason, St}}) ->
|
|
|
|
lists:flatten(
|
|
|
|
io_lib:format(
|
2019-07-16 21:07:39 +02:00
|
|
|
"Exception occurred during configuration processing. "
|
2019-06-14 11:33:26 +02:00
|
|
|
"This is most likely due to faulty/incompatible validator in "
|
|
|
|
"third-party code. If you are not running any third-party "
|
|
|
|
"code, please report the bug with ejabberd configuration "
|
2019-09-23 14:17:20 +02:00
|
|
|
"file attached and the following stacktrace included:~n** ~ts",
|
2019-06-14 11:33:26 +02:00
|
|
|
[misc:format_exception(2, Class, Reason, St)])).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2019-07-10 22:35:53 +02:00
|
|
|
-spec path() -> binary().
|
2019-06-14 11:33:26 +02:00
|
|
|
path() ->
|
2019-07-10 22:35:53 +02:00
|
|
|
unicode:characters_to_binary(
|
|
|
|
case get_env_config() of
|
|
|
|
{ok, Path} ->
|
|
|
|
Path;
|
|
|
|
undefined ->
|
|
|
|
case os:getenv("EJABBERD_CONFIG_PATH") of
|
|
|
|
false ->
|
|
|
|
"ejabberd.yml";
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end
|
|
|
|
end).
|
2015-01-29 18:43:47 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_env_config() -> {ok, string()} | undefined.
|
|
|
|
get_env_config() ->
|
|
|
|
%% First case: the filename can be specified with: erl -config "/path/to/ejabberd.yml".
|
|
|
|
case application:get_env(ejabberd, config) of
|
|
|
|
R = {ok, _Path} -> R;
|
|
|
|
undefined ->
|
|
|
|
%% Second case for embbeding ejabberd in another app, for example for Elixir:
|
|
|
|
%% config :ejabberd,
|
|
|
|
%% file: "config/ejabberd.yml"
|
|
|
|
application:get_env(ejabberd, file)
|
2015-01-29 18:43:47 +01:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec create_tmp_config() -> ok.
|
|
|
|
create_tmp_config() ->
|
|
|
|
T = ets:new(options, [private]),
|
|
|
|
put(ejabberd_options, T),
|
|
|
|
ok.
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_tmp_config() -> ets:tid() | undefined.
|
|
|
|
get_tmp_config() ->
|
|
|
|
get(ejabberd_options).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec delete_tmp_config() -> ok.
|
|
|
|
delete_tmp_config() ->
|
|
|
|
case get_tmp_config() of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
T ->
|
|
|
|
erase(ejabberd_options),
|
|
|
|
ets:delete(T),
|
|
|
|
ok
|
2015-01-29 18:43:47 +01:00
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec callback_modules(local | external | all) -> [module()].
|
|
|
|
callback_modules(local) ->
|
|
|
|
[ejabberd_options];
|
|
|
|
callback_modules(external) ->
|
|
|
|
lists:filter(
|
|
|
|
fun(M) ->
|
|
|
|
case code:ensure_loaded(M) of
|
|
|
|
{module, _} ->
|
2019-06-17 19:51:04 +02:00
|
|
|
erlang:function_exported(M, options, 0)
|
|
|
|
andalso erlang:function_exported(M, opt_type, 1);
|
2019-06-14 11:33:26 +02:00
|
|
|
{error, _} ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end, beams(external));
|
|
|
|
callback_modules(all) ->
|
|
|
|
callback_modules(local) ++ callback_modules(external).
|
|
|
|
|
|
|
|
-spec validators(module(), [atom()]) -> econf:validators().
|
|
|
|
validators(Mod, Disallowed) ->
|
|
|
|
maps:from_list(
|
|
|
|
lists:filtermap(
|
|
|
|
fun(O) ->
|
|
|
|
case lists:member(O, Disallowed) of
|
|
|
|
true -> false;
|
|
|
|
false ->
|
|
|
|
{true,
|
|
|
|
try {O, Mod:opt_type(O)}
|
|
|
|
catch _:_ ->
|
|
|
|
{O, ejabberd_options:opt_type(O)}
|
|
|
|
end}
|
|
|
|
end
|
|
|
|
end, proplists:get_keys(Mod:options()))).
|
|
|
|
|
|
|
|
read_file(File) ->
|
|
|
|
read_file(File, [replace_macros, include_files, include_modules_configs]).
|
2015-06-17 11:32:42 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
read_file(File, Opts) ->
|
|
|
|
{Opts1, Opts2} = proplists:split(Opts, [replace_macros, include_files]),
|
|
|
|
Ret = case filename:extension(File) of
|
2019-07-10 22:35:53 +02:00
|
|
|
Ex when Ex == <<".yml">> orelse Ex == <<".yaml">> ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Files = case proplists:get_bool(include_modules_configs, Opts2) of
|
2019-11-07 08:47:11 +01:00
|
|
|
true -> ext_mod:modules_configs();
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> []
|
|
|
|
end,
|
2019-10-20 17:14:33 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(F) ->
|
|
|
|
?INFO_MSG("Loading third-party configuration from ~ts", [F])
|
|
|
|
end, Files),
|
2019-06-14 11:33:26 +02:00
|
|
|
read_yaml_files([File|Files], lists:flatten(Opts1));
|
|
|
|
_ ->
|
|
|
|
read_erlang_file(File, lists:flatten(Opts1))
|
|
|
|
end,
|
|
|
|
case Ret of
|
|
|
|
{ok, Y} ->
|
2023-05-17 17:09:09 +02:00
|
|
|
InstalledModules = maybe_install_contrib_modules(Y),
|
|
|
|
ValResult = validate(Y),
|
|
|
|
case InstalledModules of
|
|
|
|
[] -> ok;
|
|
|
|
_ -> spawn(fun() -> timer:sleep(5000), ?MODULE:reload() end)
|
|
|
|
end,
|
|
|
|
ValResult;
|
2019-06-14 11:33:26 +02:00
|
|
|
Err ->
|
|
|
|
Err
|
2015-06-17 11:32:42 +02:00
|
|
|
end.
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
read_yaml_files(Files, Opts) ->
|
|
|
|
ParseOpts = [plain_as_atom | lists:flatten(Opts)],
|
|
|
|
lists:foldl(
|
|
|
|
fun(File, {ok, Y1}) ->
|
|
|
|
case econf:parse(File, #{'_' => econf:any()}, ParseOpts) of
|
|
|
|
{ok, Y2} -> {ok, Y1 ++ Y2};
|
|
|
|
Err -> Err
|
|
|
|
end;
|
|
|
|
(_, Err) ->
|
|
|
|
Err
|
|
|
|
end, {ok, []}, Files).
|
|
|
|
|
|
|
|
read_erlang_file(File, _) ->
|
|
|
|
case ejabberd_old_config:read_file(File) of
|
|
|
|
{ok, Y} ->
|
|
|
|
econf:replace_macros(Y);
|
|
|
|
Err ->
|
|
|
|
Err
|
2016-12-28 07:47:11 +01:00
|
|
|
end.
|
2017-03-10 13:12:43 +01:00
|
|
|
|
2023-05-17 17:09:09 +02:00
|
|
|
-spec maybe_install_contrib_modules(term()) -> [atom()].
|
|
|
|
maybe_install_contrib_modules(Options) ->
|
|
|
|
case {lists:keysearch(allow_contrib_modules, 1, Options),
|
|
|
|
lists:keysearch(install_contrib_modules, 1, Options)} of
|
|
|
|
{Allow, {value, {_, InstallContribModules}}}
|
|
|
|
when (Allow == false) or
|
|
|
|
(Allow == {value, {allow_contrib_modules, true}}) ->
|
|
|
|
ext_mod:install_contrib_modules(InstallContribModules, Options);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2019-06-21 14:37:51 +02:00
|
|
|
-spec validate(term()) -> {ok, [{atom(), term()}]} | error_return().
|
2019-06-14 11:33:26 +02:00
|
|
|
validate(Y1) ->
|
2019-06-21 14:37:51 +02:00
|
|
|
case pre_validate(Y1) of
|
2019-06-14 11:33:26 +02:00
|
|
|
{ok, Y2} ->
|
Replace lager with built-in new logging API
This change requires Erlang/OTP-21.0 or higher.
The commit also deprecates the following options:
- log_rotate_date
- log_rate_limit
Furthermore, these options have no effect. The logger now fully
relies on log_rotate_size, that cannot be 0 anymore.
The loglevel option now accepts levels in literal formats.
Those are: none, emergency, alert, critical, error, warning, notice, info, debug.
Old integer values (0-5) are still supported and automatically converted
into literal format.
2019-10-18 18:12:32 +02:00
|
|
|
set_loglevel(proplists:get_value(loglevel, Y2, info)),
|
2022-12-01 13:24:46 +01:00
|
|
|
ejabberd_logger:set_modules_fully_logged(proplists:get_value(log_modules_fully, Y2, [])),
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_config_transformer:map_reduce(Y2) of
|
|
|
|
{ok, Y3} ->
|
|
|
|
Hosts = proplists:get_value(hosts, Y3),
|
2019-06-23 11:23:22 +02:00
|
|
|
Version = proplists:get_value(version, Y3, version()),
|
2019-06-14 11:33:26 +02:00
|
|
|
create_tmp_config(),
|
|
|
|
set_option(hosts, Hosts),
|
|
|
|
set_option(host, hd(Hosts)),
|
2019-06-23 11:23:22 +02:00
|
|
|
set_option(version, Version),
|
2019-06-14 11:33:26 +02:00
|
|
|
set_option(yaml_config, Y3),
|
|
|
|
{Validators, Required} = validators([]),
|
|
|
|
Validator = econf:options(Validators,
|
|
|
|
[{required, Required},
|
|
|
|
unique]),
|
|
|
|
econf:validate(Validator, Y3);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2017-04-14 12:57:52 +02:00
|
|
|
|
2019-06-21 14:37:51 +02:00
|
|
|
-spec pre_validate(term()) -> {ok, [{atom(), term()}]} | error_return().
|
|
|
|
pre_validate(Y1) ->
|
2019-11-07 10:14:08 +01:00
|
|
|
econf:validate(
|
|
|
|
econf:and_then(
|
|
|
|
econf:options(
|
|
|
|
#{hosts => ejabberd_options:opt_type(hosts),
|
|
|
|
loglevel => ejabberd_options:opt_type(loglevel),
|
|
|
|
version => ejabberd_options:opt_type(version),
|
|
|
|
'_' => econf:any()},
|
|
|
|
[{required, [hosts]}]),
|
|
|
|
fun econf:group_dups/1), Y1).
|
2019-06-21 14:37:51 +02:00
|
|
|
|
2019-07-10 22:35:53 +02:00
|
|
|
-spec load_file(binary()) -> ok | error_return().
|
2019-06-14 11:33:26 +02:00
|
|
|
load_file(File) ->
|
|
|
|
try
|
|
|
|
case read_file(File) of
|
|
|
|
{ok, Terms} ->
|
|
|
|
case set_host_config(Terms) of
|
|
|
|
{ok, Map} ->
|
|
|
|
T = get_tmp_config(),
|
|
|
|
Hosts = get_myhosts(),
|
|
|
|
apply_defaults(T, Hosts, Map),
|
|
|
|
case validate_modules(Hosts) of
|
|
|
|
{ok, ModOpts} ->
|
|
|
|
ets:insert(T, ModOpts),
|
|
|
|
set_option(host, hd(Hosts)),
|
|
|
|
commit(),
|
|
|
|
set_fqdn();
|
|
|
|
Err ->
|
|
|
|
abort(Err)
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
abort(Err)
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
abort(Err)
|
|
|
|
end
|
|
|
|
catch ?EX_RULE(Class, Reason, St) ->
|
|
|
|
{error, {exception, Class, Reason, ?EX_STACK(St)}}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec commit() -> ok.
|
|
|
|
commit() ->
|
|
|
|
T = get_tmp_config(),
|
|
|
|
NewOpts = ets:tab2list(T),
|
|
|
|
ets:insert(ejabberd_options, NewOpts),
|
|
|
|
delete_tmp_config().
|
|
|
|
|
|
|
|
-spec abort(error_return()) -> error_return().
|
|
|
|
abort(Err) ->
|
|
|
|
delete_tmp_config(),
|
|
|
|
try ets:lookup_element(ejabberd_options, {loglevel, global}, 2) of
|
|
|
|
Level -> set_loglevel(Level)
|
|
|
|
catch _:badarg ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
Err.
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec set_host_config([{atom(), term()}]) -> {ok, host_config()} | error_return().
|
2019-06-14 11:33:26 +02:00
|
|
|
set_host_config(Opts) ->
|
|
|
|
Map1 = lists:foldl(
|
|
|
|
fun({Opt, Val}, M) when Opt /= host_config,
|
|
|
|
Opt /= append_host_config ->
|
|
|
|
maps:put({Opt, global}, Val, M);
|
|
|
|
(_, M) ->
|
|
|
|
M
|
|
|
|
end, #{}, Opts),
|
|
|
|
HostOpts = proplists:get_value(host_config, Opts, []),
|
|
|
|
AppendHostOpts = proplists:get_value(append_host_config, Opts, []),
|
|
|
|
Map2 = lists:foldl(
|
|
|
|
fun({Host, Opts1}, M1) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun({Opt, Val}, M2) ->
|
|
|
|
maps:put({Opt, Host}, Val, M2)
|
|
|
|
end, M1, Opts1)
|
|
|
|
end, Map1, HostOpts),
|
|
|
|
Map3 = lists:foldl(
|
|
|
|
fun(_, {error, _} = Err) ->
|
|
|
|
Err;
|
|
|
|
({Host, Opts1}, M1) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(_, {error, _} = Err) ->
|
|
|
|
Err;
|
|
|
|
({Opt, L1}, M2) when is_list(L1) ->
|
|
|
|
L2 = try maps:get({Opt, Host}, M2)
|
|
|
|
catch _:{badkey, _} ->
|
|
|
|
maps:get({Opt, global}, M2, [])
|
|
|
|
end,
|
|
|
|
L3 = L2 ++ L1,
|
|
|
|
maps:put({Opt, Host}, L3, M2);
|
|
|
|
({Opt, _}, _) ->
|
|
|
|
{error, {merge_conflict, Opt, Host}}
|
|
|
|
end, M1, Opts1)
|
|
|
|
end, Map2, AppendHostOpts),
|
|
|
|
case Map3 of
|
|
|
|
{error, _} -> Map3;
|
|
|
|
_ -> {ok, Map3}
|
|
|
|
end.
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec apply_defaults(ets:tid(), [binary()], host_config()) -> ok.
|
2019-06-14 11:33:26 +02:00
|
|
|
apply_defaults(Tab, Hosts, Map) ->
|
|
|
|
Defaults1 = defaults(),
|
|
|
|
apply_defaults(Tab, global, Map, Defaults1),
|
|
|
|
{_, Defaults2} = proplists:split(Defaults1, globals()),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
set_option(host, Host),
|
|
|
|
apply_defaults(Tab, Host, Map, Defaults2)
|
|
|
|
end, Hosts).
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec apply_defaults(ets:tid(), global | binary(),
|
|
|
|
host_config(),
|
2019-06-14 11:33:26 +02:00
|
|
|
[atom() | {atom(), term()}]) -> ok.
|
|
|
|
apply_defaults(Tab, Host, Map, Defaults) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Opt, Default}) ->
|
|
|
|
try maps:get({Opt, Host}, Map) of
|
|
|
|
Val ->
|
|
|
|
ets:insert(Tab, {{Opt, Host}, Val})
|
|
|
|
catch _:{badkey, _} when Host == global ->
|
|
|
|
Default1 = compute_default(Default, Host),
|
|
|
|
ets:insert(Tab, {{Opt, Host}, Default1});
|
|
|
|
_:{badkey, _} ->
|
|
|
|
try maps:get({Opt, global}, Map) of
|
|
|
|
V -> ets:insert(Tab, {{Opt, Host}, V})
|
|
|
|
catch _:{badkey, _} ->
|
|
|
|
Default1 = compute_default(Default, Host),
|
|
|
|
ets:insert(Tab, {{Opt, Host}, Default1})
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
(Opt) when Host == global ->
|
|
|
|
Val = maps:get({Opt, Host}, Map),
|
|
|
|
ets:insert(Tab, {{Opt, Host}, Val});
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, Defaults).
|
|
|
|
|
|
|
|
-spec defaults() -> [atom() | {atom(), term()}].
|
|
|
|
defaults() ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Mod, Acc) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun({Opt, Val}, Acc1) ->
|
|
|
|
lists:keystore(Opt, 1, Acc1, {Opt, Val});
|
|
|
|
(Opt, Acc1) ->
|
|
|
|
case lists:member(Opt, Acc1) of
|
|
|
|
true -> Acc1;
|
|
|
|
false -> [Opt|Acc1]
|
|
|
|
end
|
|
|
|
end, Acc, Mod:options())
|
|
|
|
end, ejabberd_options:options(), callback_modules(external)).
|
|
|
|
|
|
|
|
-spec globals() -> [atom()].
|
|
|
|
globals() ->
|
|
|
|
lists:usort(
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Mod) ->
|
|
|
|
case erlang:function_exported(Mod, globals, 0) of
|
|
|
|
true -> Mod:globals();
|
|
|
|
false -> []
|
|
|
|
end
|
|
|
|
end, callback_modules(all))).
|
|
|
|
|
|
|
|
%% The module validator depends on virtual host, so we have to
|
|
|
|
%% validate modules in this separate function.
|
|
|
|
-spec validate_modules([binary()]) -> {ok, list()} | error_return().
|
|
|
|
validate_modules(Hosts) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Host, {ok, Acc}) ->
|
|
|
|
set_option(host, Host),
|
|
|
|
ModOpts = get_option({modules, Host}),
|
|
|
|
case gen_mod:validate(Host, ModOpts) of
|
|
|
|
{ok, ModOpts1} ->
|
|
|
|
{ok, [{{modules, Host}, ModOpts1}|Acc]};
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
(_, Err) ->
|
|
|
|
Err
|
|
|
|
end, {ok, []}, Hosts).
|
|
|
|
|
|
|
|
-spec delete_host_options([binary()]) -> ok.
|
|
|
|
delete_host_options(Hosts) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
ets:match_delete(ejabberd_options, {{'_', Host}, '_'})
|
|
|
|
end, Hosts).
|
|
|
|
|
|
|
|
-spec compute_default(fun((global | binary()) -> T) | T, global | binary()) -> T.
|
|
|
|
compute_default(F, Host) when is_function(F, 1) ->
|
|
|
|
F(Host);
|
|
|
|
compute_default(Val, _) ->
|
|
|
|
Val.
|
2018-02-09 16:12:50 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec set_fqdn() -> ok.
|
|
|
|
set_fqdn() ->
|
|
|
|
FQDNs = get_option(fqdn),
|
|
|
|
xmpp:set_config([{fqdn, FQDNs}]).
|
2018-02-20 09:38:00 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec set_shared_key() -> ok.
|
|
|
|
set_shared_key() ->
|
|
|
|
Key = case erlang:get_cookie() of
|
|
|
|
nocookie ->
|
|
|
|
str:sha(p1_rand:get_string());
|
|
|
|
Cookie ->
|
|
|
|
str:sha(erlang:atom_to_binary(Cookie, latin1))
|
|
|
|
end,
|
|
|
|
set_option(shared_key, Key).
|
|
|
|
|
|
|
|
-spec set_node_start(integer()) -> ok.
|
|
|
|
set_node_start(UnixTime) ->
|
|
|
|
set_option(node_start, UnixTime).
|
|
|
|
|
Replace lager with built-in new logging API
This change requires Erlang/OTP-21.0 or higher.
The commit also deprecates the following options:
- log_rotate_date
- log_rate_limit
Furthermore, these options have no effect. The logger now fully
relies on log_rotate_size, that cannot be 0 anymore.
The loglevel option now accepts levels in literal formats.
Those are: none, emergency, alert, critical, error, warning, notice, info, debug.
Old integer values (0-5) are still supported and automatically converted
into literal format.
2019-10-18 18:12:32 +02:00
|
|
|
-spec set_loglevel(logger:level()) -> ok.
|
2019-06-14 11:33:26 +02:00
|
|
|
set_loglevel(Level) ->
|
|
|
|
ejabberd_logger:set(Level).
|