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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2012-02-23 16:52:34 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2012 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
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_config).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
-export([start/0, load_file/1,
|
|
|
|
add_global_option/2, add_local_option/2,
|
|
|
|
get_global_option/1, get_local_option/1]).
|
2008-02-19 14:49:29 +01:00
|
|
|
-export([get_vh_by_auth_method/1]).
|
2009-02-16 18:57:30 +01:00
|
|
|
-export([is_file_readable/1]).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2007-11-09 11:18:38 +01:00
|
|
|
-include("ejabberd_config.hrl").
|
2009-02-16 18:57:30 +01:00
|
|
|
-include_lib("kernel/include/file.hrl").
|
2003-01-21 21:36:55 +01:00
|
|
|
|
2008-04-02 18:31:15 +02:00
|
|
|
|
|
|
|
%% @type macro() = {macro_key(), macro_value()}
|
|
|
|
|
|
|
|
%% @type macro_key() = atom().
|
|
|
|
%% The atom must have all characters in uppercase.
|
|
|
|
|
|
|
|
%% @type macro_value() = term().
|
|
|
|
|
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
start() ->
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:create_table(config,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, config)}]),
|
|
|
|
mnesia:add_table_copy(config, node(), ram_copies),
|
|
|
|
mnesia:create_table(local_config,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes, record_info(fields, local_config)}]),
|
|
|
|
mnesia:add_table_copy(local_config, node(), ram_copies),
|
2008-04-02 18:31:15 +02:00
|
|
|
Config = get_ejabberd_config_path(),
|
2008-12-23 02:34:52 +01:00
|
|
|
load_file(Config),
|
|
|
|
%% This start time is used by mod_last:
|
|
|
|
add_local_option(node_start, now()),
|
2011-07-25 15:53:33 +02:00
|
|
|
SharedKey = case erlang:get_cookie() of
|
|
|
|
nocookie ->
|
|
|
|
sha:sha(randoms:get_string());
|
|
|
|
Cookie ->
|
|
|
|
sha:sha(atom_to_list(Cookie))
|
|
|
|
end,
|
|
|
|
add_local_option(shared_key, SharedKey),
|
2008-12-23 02:34:52 +01:00
|
|
|
ok.
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2008-04-02 18:31:15 +02:00
|
|
|
%% @doc Get the filename of the ejabberd configuration file.
|
|
|
|
%% The filename can be specified with: erl -config "/path/to/ejabberd.cfg".
|
|
|
|
%% It can also be specified with the environtment variable EJABBERD_CONFIG_PATH.
|
|
|
|
%% If not specified, the default value 'ejabberd.cfg' is assumed.
|
|
|
|
%% @spec () -> string()
|
|
|
|
get_ejabberd_config_path() ->
|
|
|
|
case application:get_env(config) of
|
|
|
|
{ok, Path} -> Path;
|
|
|
|
undefined ->
|
|
|
|
case os:getenv("EJABBERD_CONFIG_PATH") of
|
|
|
|
false ->
|
|
|
|
?CONFIG_PATH;
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end
|
|
|
|
end.
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2008-04-02 18:31:15 +02:00
|
|
|
%% @doc Load the ejabberd configuration file.
|
|
|
|
%% It also includes additional configuration files and replaces macros.
|
2009-02-16 18:57:30 +01:00
|
|
|
%% This function will crash if finds some error in the configuration file.
|
2008-12-23 02:34:52 +01:00
|
|
|
%% @spec (File::string()) -> ok
|
2003-01-07 20:10:35 +01:00
|
|
|
load_file(File) ->
|
2008-04-02 18:31:15 +02:00
|
|
|
Terms = get_plain_terms_file(File),
|
|
|
|
State = lists:foldl(fun search_hosts/2, #state{}, Terms),
|
|
|
|
Terms_macros = replace_macros(Terms),
|
|
|
|
Res = lists:foldl(fun process_term/2, State, Terms_macros),
|
|
|
|
set_opts(Res).
|
|
|
|
|
|
|
|
%% @doc Read an ejabberd configuration file and return the terms.
|
|
|
|
%% Input is an absolute or relative path to an ejabberd config file.
|
|
|
|
%% Returns a list of plain terms,
|
|
|
|
%% in which the options 'include_config_file' were parsed
|
|
|
|
%% and the terms in those files were included.
|
|
|
|
%% @spec(string()) -> [term()]
|
|
|
|
get_plain_terms_file(File1) ->
|
|
|
|
File = get_absolute_path(File1),
|
2003-01-16 21:24:53 +01:00
|
|
|
case file:consult(File) of
|
|
|
|
{ok, Terms} ->
|
2008-04-02 18:31:15 +02:00
|
|
|
include_config_files(Terms);
|
2009-12-29 23:48:25 +01:00
|
|
|
{error, {LineNumber, erl_parse, _ParseMessage} = Reason} ->
|
|
|
|
ExitText = describe_config_problem(File, Reason, LineNumber),
|
|
|
|
?ERROR_MSG(ExitText, []),
|
|
|
|
exit_or_halt(ExitText);
|
2008-07-22 18:08:57 +02:00
|
|
|
{error, Reason} ->
|
2009-12-29 23:48:25 +01:00
|
|
|
ExitText = describe_config_problem(File, Reason),
|
|
|
|
?ERROR_MSG(ExitText, []),
|
|
|
|
exit_or_halt(ExitText)
|
2003-01-07 20:10:35 +01:00
|
|
|
end.
|
|
|
|
|
2008-04-02 18:31:15 +02:00
|
|
|
%% @doc Convert configuration filename to absolute path.
|
|
|
|
%% Input is an absolute or relative path to an ejabberd configuration file.
|
|
|
|
%% And returns an absolute path to the configuration file.
|
|
|
|
%% @spec (string()) -> string()
|
|
|
|
get_absolute_path(File) ->
|
|
|
|
case filename:pathtype(File) of
|
|
|
|
absolute ->
|
|
|
|
File;
|
|
|
|
relative ->
|
|
|
|
Config_path = get_ejabberd_config_path(),
|
|
|
|
Config_dir = filename:dirname(Config_path),
|
|
|
|
filename:absname_join(Config_dir, File)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
search_hosts(Term, State) ->
|
|
|
|
case Term of
|
|
|
|
{host, Host} ->
|
|
|
|
if
|
|
|
|
State#state.hosts == [] ->
|
2007-06-29 19:27:42 +02:00
|
|
|
add_hosts_to_option([Host], State);
|
2005-06-20 05:18:13 +02:00
|
|
|
true ->
|
|
|
|
?ERROR_MSG("Can't load config file: "
|
|
|
|
"too many hosts definitions", []),
|
|
|
|
exit("too many hosts definitions")
|
|
|
|
end;
|
|
|
|
{hosts, Hosts} ->
|
|
|
|
if
|
|
|
|
State#state.hosts == [] ->
|
2007-06-29 19:27:42 +02:00
|
|
|
add_hosts_to_option(Hosts, State);
|
2005-06-20 05:18:13 +02:00
|
|
|
true ->
|
|
|
|
?ERROR_MSG("Can't load config file: "
|
|
|
|
"too many hosts definitions", []),
|
|
|
|
exit("too many hosts definitions")
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
State
|
|
|
|
end.
|
|
|
|
|
2007-06-29 19:27:42 +02:00
|
|
|
add_hosts_to_option(Hosts, State) ->
|
|
|
|
PrepHosts = normalize_hosts(Hosts),
|
|
|
|
add_option(hosts, PrepHosts, State#state{hosts = PrepHosts}).
|
|
|
|
|
|
|
|
normalize_hosts(Hosts) ->
|
|
|
|
normalize_hosts(Hosts,[]).
|
|
|
|
normalize_hosts([], PrepHosts) ->
|
|
|
|
lists:reverse(PrepHosts);
|
|
|
|
normalize_hosts([Host|Hosts], PrepHosts) ->
|
|
|
|
case jlib:nodeprep(Host) of
|
|
|
|
error ->
|
|
|
|
?ERROR_MSG("Can't load config file: "
|
|
|
|
"invalid host name [~p]", [Host]),
|
|
|
|
exit("invalid hostname");
|
|
|
|
PrepHost ->
|
|
|
|
normalize_hosts(Hosts, [PrepHost|PrepHosts])
|
|
|
|
end.
|
|
|
|
|
2008-04-02 18:31:15 +02:00
|
|
|
|
2009-12-29 23:48:25 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% Errors reading the config file
|
|
|
|
|
|
|
|
describe_config_problem(Filename, Reason) ->
|
|
|
|
Text1 = lists:flatten("Problem loading ejabberd config file " ++ Filename),
|
|
|
|
Text2 = lists:flatten(" : " ++ file:format_error(Reason)),
|
|
|
|
ExitText = Text1 ++ Text2,
|
|
|
|
ExitText.
|
|
|
|
|
|
|
|
describe_config_problem(Filename, Reason, LineNumber) ->
|
|
|
|
Text1 = lists:flatten("Problem loading ejabberd config file " ++ Filename),
|
|
|
|
Text2 = lists:flatten(" approximately in the line "
|
|
|
|
++ file:format_error(Reason)),
|
|
|
|
ExitText = Text1 ++ Text2,
|
|
|
|
Lines = get_config_lines(Filename, LineNumber, 10, 3),
|
2009-12-30 02:36:07 +01:00
|
|
|
?ERROR_MSG("The following lines from your configuration file might be"
|
|
|
|
" relevant to the error: ~n~s", [Lines]),
|
2009-12-29 23:48:25 +01:00
|
|
|
ExitText.
|
|
|
|
|
|
|
|
get_config_lines(Filename, TargetNumber, PreContext, PostContext) ->
|
|
|
|
{ok, Fd} = file:open(Filename, [read]),
|
|
|
|
LNumbers = lists:seq(TargetNumber-PreContext, TargetNumber+PostContext),
|
2010-02-20 19:31:36 +01:00
|
|
|
NextL = io:get_line(Fd, no_prompt),
|
2009-12-29 23:48:25 +01:00
|
|
|
R = get_config_lines2(Fd, NextL, 1, LNumbers, []),
|
|
|
|
file:close(Fd),
|
|
|
|
R.
|
|
|
|
|
|
|
|
get_config_lines2(_Fd, eof, _CurrLine, _LNumbers, R) ->
|
|
|
|
lists:reverse(R);
|
|
|
|
get_config_lines2(_Fd, _NewLine, _CurrLine, [], R) ->
|
|
|
|
lists:reverse(R);
|
2010-02-20 19:31:36 +01:00
|
|
|
get_config_lines2(Fd, Data, CurrLine, [NextWanted | LNumbers], R) when is_list(Data) ->
|
|
|
|
NextL = io:get_line(Fd, no_prompt),
|
2009-12-29 23:48:25 +01:00
|
|
|
if
|
|
|
|
CurrLine >= NextWanted ->
|
|
|
|
Line2 = [integer_to_list(CurrLine), ": " | Data],
|
|
|
|
get_config_lines2(Fd, NextL, CurrLine+1, LNumbers, [Line2 | R]);
|
|
|
|
true ->
|
|
|
|
get_config_lines2(Fd, NextL, CurrLine+1, [NextWanted | LNumbers], R)
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% If ejabberd isn't yet running in this node, then halt the node
|
|
|
|
exit_or_halt(ExitText) ->
|
|
|
|
case [Vsn || {ejabberd, _Desc, Vsn} <- application:which_applications()] of
|
|
|
|
[] ->
|
|
|
|
timer:sleep(1000),
|
2011-05-30 17:11:32 +02:00
|
|
|
halt(string:substr(ExitText, 1, 199));
|
2009-12-29 23:48:25 +01:00
|
|
|
[_] ->
|
|
|
|
exit(ExitText)
|
|
|
|
end.
|
|
|
|
|
2008-04-02 18:31:15 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% Support for 'include_config_file'
|
|
|
|
|
|
|
|
%% @doc Include additional configuration files in the list of terms.
|
|
|
|
%% @spec ([term()]) -> [term()]
|
|
|
|
include_config_files(Terms) ->
|
|
|
|
include_config_files(Terms, []).
|
|
|
|
|
|
|
|
include_config_files([], Res) ->
|
|
|
|
Res;
|
|
|
|
include_config_files([{include_config_file, Filename} | Terms], Res) ->
|
|
|
|
include_config_files([{include_config_file, Filename, []} | Terms], Res);
|
|
|
|
include_config_files([{include_config_file, Filename, Options} | Terms], Res) ->
|
|
|
|
Included_terms = get_plain_terms_file(Filename),
|
|
|
|
Disallow = proplists:get_value(disallow, Options, []),
|
|
|
|
Included_terms2 = delete_disallowed(Disallow, Included_terms),
|
|
|
|
Allow_only = proplists:get_value(allow_only, Options, all),
|
|
|
|
Included_terms3 = keep_only_allowed(Allow_only, Included_terms2),
|
|
|
|
include_config_files(Terms, Res ++ Included_terms3);
|
|
|
|
include_config_files([Term | Terms], Res) ->
|
|
|
|
include_config_files(Terms, Res ++ [Term]).
|
|
|
|
|
|
|
|
%% @doc Filter from the list of terms the disallowed.
|
|
|
|
%% Returns a sublist of Terms without the ones which first element is
|
|
|
|
%% included in Disallowed.
|
|
|
|
%% @spec (Disallowed::[atom()], Terms::[term()]) -> [term()]
|
|
|
|
delete_disallowed(Disallowed, Terms) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Dis, Ldis) ->
|
|
|
|
delete_disallowed2(Dis, Ldis)
|
|
|
|
end,
|
|
|
|
Terms,
|
|
|
|
Disallowed).
|
|
|
|
|
|
|
|
delete_disallowed2(Disallowed, [H|T]) ->
|
|
|
|
case element(1, H) of
|
|
|
|
Disallowed ->
|
|
|
|
?WARNING_MSG("The option '~p' is disallowed, "
|
|
|
|
"and will not be accepted", [Disallowed]),
|
|
|
|
delete_disallowed2(Disallowed, T);
|
|
|
|
_ ->
|
|
|
|
[H|delete_disallowed2(Disallowed, T)]
|
|
|
|
end;
|
|
|
|
delete_disallowed2(_, []) ->
|
|
|
|
[].
|
|
|
|
|
|
|
|
%% @doc Keep from the list only the allowed terms.
|
|
|
|
%% Returns a sublist of Terms with only the ones which first element is
|
|
|
|
%% included in Allowed.
|
|
|
|
%% @spec (Allowed::[atom()], Terms::[term()]) -> [term()]
|
|
|
|
keep_only_allowed(all, Terms) ->
|
|
|
|
Terms;
|
|
|
|
keep_only_allowed(Allowed, Terms) ->
|
|
|
|
{As, NAs} = lists:partition(
|
|
|
|
fun(Term) ->
|
|
|
|
lists:member(element(1, Term), Allowed)
|
|
|
|
end,
|
|
|
|
Terms),
|
|
|
|
[?WARNING_MSG("This option is not allowed, "
|
|
|
|
"and will not be accepted:~n~p", [NA])
|
|
|
|
|| NA <- NAs],
|
|
|
|
As.
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% Support for Macro
|
|
|
|
|
|
|
|
%% @doc Replace the macros with their defined values.
|
|
|
|
%% @spec (Terms::[term()]) -> [term()]
|
|
|
|
replace_macros(Terms) ->
|
|
|
|
{TermsOthers, Macros} = split_terms_macros(Terms),
|
|
|
|
replace(TermsOthers, Macros).
|
|
|
|
|
|
|
|
%% @doc Split Terms into normal terms and macro definitions.
|
|
|
|
%% @spec (Terms) -> {Terms, Macros}
|
|
|
|
%% Terms = [term()]
|
|
|
|
%% Macros = [macro()]
|
|
|
|
split_terms_macros(Terms) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Term, {TOs, Ms}) ->
|
|
|
|
case Term of
|
2008-04-11 07:52:10 +02:00
|
|
|
{define_macro, Key, Value} ->
|
|
|
|
case is_atom(Key) and is_all_uppercase(Key) of
|
|
|
|
true ->
|
|
|
|
{TOs, Ms++[{Key, Value}]};
|
|
|
|
false ->
|
|
|
|
exit({macro_not_properly_defined, Term})
|
|
|
|
end;
|
|
|
|
Term ->
|
|
|
|
{TOs ++ [Term], Ms}
|
2008-04-02 18:31:15 +02:00
|
|
|
end
|
|
|
|
end,
|
2008-04-11 07:52:10 +02:00
|
|
|
{[], []},
|
2008-04-02 18:31:15 +02:00
|
|
|
Terms).
|
|
|
|
|
|
|
|
%% @doc Recursively replace in Terms macro usages with the defined value.
|
|
|
|
%% @spec (Terms, Macros) -> Terms
|
|
|
|
%% Terms = [term()]
|
|
|
|
%% Macros = [macro()]
|
|
|
|
replace([], _) ->
|
|
|
|
[];
|
|
|
|
replace([Term|Terms], Macros) ->
|
|
|
|
[replace_term(Term, Macros) | replace(Terms, Macros)].
|
|
|
|
|
|
|
|
replace_term(Key, Macros) when is_atom(Key) ->
|
2008-04-11 07:52:10 +02:00
|
|
|
case is_all_uppercase(Key) of
|
|
|
|
true ->
|
|
|
|
case proplists:get_value(Key, Macros) of
|
|
|
|
undefined -> exit({undefined_macro, Key});
|
|
|
|
Value -> Value
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Key
|
|
|
|
end;
|
2008-04-02 18:31:15 +02:00
|
|
|
replace_term({use_macro, Key, Value}, Macros) ->
|
|
|
|
proplists:get_value(Key, Macros, Value);
|
|
|
|
replace_term(Term, Macros) when is_list(Term) ->
|
|
|
|
replace(Term, Macros);
|
|
|
|
replace_term(Term, Macros) when is_tuple(Term) ->
|
|
|
|
List = tuple_to_list(Term),
|
|
|
|
List2 = replace(List, Macros),
|
|
|
|
list_to_tuple(List2);
|
|
|
|
replace_term(Term, _) ->
|
|
|
|
Term.
|
|
|
|
|
|
|
|
is_all_uppercase(Atom) ->
|
2008-04-11 07:52:10 +02:00
|
|
|
String = erlang:atom_to_list(Atom),
|
|
|
|
lists:all(fun(C) when C >= $a, C =< $z -> false;
|
|
|
|
(_) -> true
|
|
|
|
end, String).
|
2008-04-02 18:31:15 +02:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% Process terms
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
process_term(Term, State) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case Term of
|
2003-02-01 21:21:28 +01:00
|
|
|
override_global ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{override_global = true};
|
2003-02-01 21:21:28 +01:00
|
|
|
override_local ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{override_local = true};
|
2003-02-01 21:21:28 +01:00
|
|
|
override_acls ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{override_acls = true};
|
2007-12-07 00:15:04 +01:00
|
|
|
{acl, _ACLName, _ACLData} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
process_host_term(Term, global, State);
|
2007-12-07 00:15:04 +01:00
|
|
|
{access, _RuleName, _Rules} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
process_host_term(Term, global, State);
|
2007-12-07 00:15:04 +01:00
|
|
|
{shaper, _Name, _Data} ->
|
|
|
|
%%lists:foldl(fun(Host, S) -> process_host_term(Term, Host, S) end,
|
|
|
|
%% State, State#state.hosts);
|
2006-10-02 20:54:10 +02:00
|
|
|
process_host_term(Term, global, State);
|
2007-12-07 00:15:04 +01:00
|
|
|
{host, _Host} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State;
|
2007-12-07 00:15:04 +01:00
|
|
|
{hosts, _Hosts} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State;
|
2012-03-16 14:16:17 +01:00
|
|
|
{fqdn, HostFQDN} ->
|
|
|
|
?DEBUG("FQDN set to: ~p", [HostFQDN]),
|
|
|
|
add_option(fqdn, HostFQDN, State);
|
2005-07-31 21:51:52 +02:00
|
|
|
{host_config, Host, Terms} ->
|
|
|
|
lists:foldl(fun(T, S) -> process_host_term(T, Host, S) end,
|
|
|
|
State, Terms);
|
2009-01-12 21:03:02 +01:00
|
|
|
{listen, Listeners} ->
|
|
|
|
Listeners2 =
|
|
|
|
lists:map(
|
|
|
|
fun({PortIP, Module, Opts}) ->
|
2009-08-11 09:15:36 +02:00
|
|
|
{Port, IPT, _, _, Proto, OptsClean} =
|
2009-01-12 21:03:02 +01:00
|
|
|
ejabberd_listener:parse_listener_portip(PortIP, Opts),
|
2009-08-11 09:15:36 +02:00
|
|
|
{{Port, IPT, Proto}, Module, OptsClean}
|
2009-01-12 21:03:02 +01:00
|
|
|
end,
|
|
|
|
Listeners),
|
|
|
|
add_option(listen, Listeners2, State);
|
2007-06-07 20:27:35 +02:00
|
|
|
{language, Val} ->
|
|
|
|
add_option(language, Val, State);
|
2005-07-13 05:24:13 +02:00
|
|
|
{outgoing_s2s_port, Port} ->
|
|
|
|
add_option(outgoing_s2s_port, Port, State);
|
2008-12-26 12:45:08 +01:00
|
|
|
{outgoing_s2s_options, Methods, Timeout} ->
|
|
|
|
add_option(outgoing_s2s_options, {Methods, Timeout}, State);
|
2009-07-30 14:34:59 +02:00
|
|
|
{s2s_dns_options, PropList} ->
|
|
|
|
add_option(s2s_dns_options, PropList, State);
|
2005-10-25 03:08:37 +02:00
|
|
|
{s2s_use_starttls, Port} ->
|
|
|
|
add_option(s2s_use_starttls, Port, State);
|
2005-11-05 22:15:53 +01:00
|
|
|
{s2s_certfile, CertFile} ->
|
2009-02-16 18:57:30 +01:00
|
|
|
case ejabberd_config:is_file_readable(CertFile) of
|
|
|
|
true -> add_option(s2s_certfile, CertFile, State);
|
|
|
|
false ->
|
|
|
|
ErrorText = "There is a problem in the configuration: "
|
|
|
|
"the specified file is not readable: ",
|
|
|
|
throw({error, ErrorText ++ CertFile})
|
|
|
|
end;
|
2005-11-05 22:15:53 +01:00
|
|
|
{domain_certfile, Domain, CertFile} ->
|
2009-02-16 18:57:30 +01:00
|
|
|
case ejabberd_config:is_file_readable(CertFile) of
|
|
|
|
true -> add_option({domain_certfile, Domain}, CertFile, State);
|
|
|
|
false ->
|
|
|
|
ErrorText = "There is a problem in the configuration: "
|
|
|
|
"the specified file is not readable: ",
|
|
|
|
throw({error, ErrorText ++ CertFile})
|
|
|
|
end;
|
2006-11-07 03:08:51 +01:00
|
|
|
{node_type, NodeType} ->
|
|
|
|
add_option(node_type, NodeType, State);
|
|
|
|
{cluster_nodes, Nodes} ->
|
|
|
|
add_option(cluster_nodes, Nodes, State);
|
|
|
|
{domain_balancing, Domain, Balancing} ->
|
|
|
|
add_option({domain_balancing, Domain}, Balancing, State);
|
2007-01-19 05:46:44 +01:00
|
|
|
{domain_balancing_component_number, Domain, N} ->
|
|
|
|
add_option({domain_balancing_component_number, Domain}, N, State);
|
2007-04-12 07:31:53 +02:00
|
|
|
{watchdog_admins, Admins} ->
|
|
|
|
add_option(watchdog_admins, Admins, State);
|
2009-02-13 20:27:56 +01:00
|
|
|
{watchdog_large_heap, LH} ->
|
|
|
|
add_option(watchdog_large_heap, LH, State);
|
2008-04-22 23:51:32 +02:00
|
|
|
{registration_timeout, Timeout} ->
|
|
|
|
add_option(registration_timeout, Timeout, State);
|
2009-03-13 17:01:46 +01:00
|
|
|
{captcha_cmd, Cmd} ->
|
|
|
|
add_option(captcha_cmd, Cmd, State);
|
|
|
|
{captcha_host, Host} ->
|
|
|
|
add_option(captcha_host, Host, State);
|
2011-04-14 10:03:02 +02:00
|
|
|
{captcha_limit, Limit} ->
|
|
|
|
add_option(captcha_limit, Limit, State);
|
2009-04-17 15:48:59 +02:00
|
|
|
{ejabberdctl_access_commands, ACs} ->
|
|
|
|
add_option(ejabberdctl_access_commands, ACs, State);
|
2007-01-19 05:46:44 +01:00
|
|
|
{loglevel, Loglevel} ->
|
|
|
|
ejabberd_loglevel:set(Loglevel),
|
|
|
|
State;
|
2009-10-07 16:24:09 +02:00
|
|
|
{max_fsm_queue, N} ->
|
|
|
|
add_option(max_fsm_queue, N, State);
|
2011-05-09 05:43:54 +02:00
|
|
|
{hostname, Host} ->
|
|
|
|
add_option(hostname, Host, State);
|
2012-02-13 14:49:44 +01:00
|
|
|
{rehash_timeout, Secs} ->
|
|
|
|
add_option(rehash_timeout, Secs, State);
|
|
|
|
{migrate_timeout, Secs} ->
|
|
|
|
add_option(migrate_timeout, Secs, State);
|
2007-12-07 00:15:04 +01:00
|
|
|
{_Opt, _Val} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
lists:foldl(fun(Host, S) -> process_host_term(Term, Host, S) end,
|
|
|
|
State, State#state.hosts)
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_host_term(Term, Host, State) ->
|
|
|
|
case Term of
|
2003-01-18 20:42:48 +01:00
|
|
|
{acl, ACLName, ACLData} ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{opts =
|
2007-12-07 00:15:04 +01:00
|
|
|
[acl:to_record(Host, ACLName, ACLData) | State#state.opts]};
|
2003-01-21 21:36:55 +01:00
|
|
|
{access, RuleName, Rules} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State#state{opts = [#config{key = {access, RuleName, Host},
|
2003-02-02 20:49:19 +01:00
|
|
|
value = Rules} |
|
|
|
|
State#state.opts]};
|
2003-02-09 20:17:23 +01:00
|
|
|
{shaper, Name, Data} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State#state{opts = [#config{key = {shaper, Name, Host},
|
2003-02-09 20:17:23 +01:00
|
|
|
value = Data} |
|
|
|
|
State#state.opts]};
|
2005-04-17 20:08:34 +02:00
|
|
|
{host, Host} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State;
|
2007-12-07 00:15:04 +01:00
|
|
|
{hosts, _Hosts} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State;
|
2007-11-26 20:52:09 +01:00
|
|
|
{odbc_server, ODBC_server} ->
|
|
|
|
add_option({odbc_server, Host}, ODBC_server, State);
|
2012-04-27 11:52:05 +02:00
|
|
|
{modules, Modules} ->
|
|
|
|
add_option({modules, Host}, replace_modules(Modules), State);
|
2003-01-18 20:42:48 +01:00
|
|
|
{Opt, Val} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
add_option({Opt, Host}, Val, State)
|
2003-01-21 21:36:55 +01:00
|
|
|
end.
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
add_option(Opt, Val, State) ->
|
2003-01-21 21:36:55 +01:00
|
|
|
Table = case Opt of
|
2005-04-17 20:08:34 +02:00
|
|
|
hosts ->
|
2003-01-21 21:36:55 +01:00
|
|
|
config;
|
2004-07-10 00:34:26 +02:00
|
|
|
language ->
|
|
|
|
config;
|
2003-01-21 21:36:55 +01:00
|
|
|
_ ->
|
|
|
|
local_config
|
|
|
|
end,
|
|
|
|
case Table of
|
|
|
|
config ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{opts = [#config{key = Opt, value = Val} |
|
|
|
|
State#state.opts]};
|
2003-01-21 21:36:55 +01:00
|
|
|
local_config ->
|
2007-07-31 06:13:29 +02:00
|
|
|
case Opt of
|
|
|
|
{{add, OptName}, Host} ->
|
|
|
|
State#state{opts = compact({OptName, Host}, Val,
|
|
|
|
State#state.opts, [])};
|
|
|
|
_ ->
|
|
|
|
State#state{opts = [#local_config{key = Opt, value = Val} |
|
|
|
|
State#state.opts]}
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2008-03-04 11:36:57 +01:00
|
|
|
compact({OptName, Host} = Opt, Val, [], Os) ->
|
|
|
|
?WARNING_MSG("The option '~p' is defined for the host ~p using host_config "
|
|
|
|
"before the global '~p' option. This host_config option may get overwritten.", [OptName, Host, OptName]),
|
2007-07-31 06:13:29 +02:00
|
|
|
[#local_config{key = Opt, value = Val}] ++ Os;
|
2008-03-04 11:36:57 +01:00
|
|
|
%% Traverse the list of the options already parsed
|
2007-07-31 06:13:29 +02:00
|
|
|
compact(Opt, Val, [O | Os1], Os2) ->
|
2008-03-04 11:36:57 +01:00
|
|
|
case catch O#local_config.key of
|
|
|
|
%% If the key of a local_config matches the Opt that wants to be added
|
2007-07-31 06:13:29 +02:00
|
|
|
Opt ->
|
2008-03-04 11:36:57 +01:00
|
|
|
%% Then prepend the new value to the list of old values
|
2007-07-31 06:13:29 +02:00
|
|
|
Os2 ++ [#local_config{key = Opt,
|
|
|
|
value = Val++O#local_config.value}
|
|
|
|
] ++ Os1;
|
|
|
|
_ ->
|
|
|
|
compact(Opt, Val, Os1, Os2++[O])
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
set_opts(State) ->
|
|
|
|
Opts = lists:reverse(State#state.opts),
|
2003-02-02 20:49:19 +01:00
|
|
|
F = fun() ->
|
|
|
|
if
|
|
|
|
State#state.override_global ->
|
|
|
|
Ksg = mnesia:all_keys(config),
|
|
|
|
lists:foreach(fun(K) ->
|
|
|
|
mnesia:delete({config, K})
|
|
|
|
end, Ksg);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
State#state.override_local ->
|
|
|
|
Ksl = mnesia:all_keys(local_config),
|
|
|
|
lists:foreach(fun(K) ->
|
|
|
|
mnesia:delete({local_config, K})
|
|
|
|
end, Ksl);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
State#state.override_acls ->
|
|
|
|
Ksa = mnesia:all_keys(acl),
|
|
|
|
lists:foreach(fun(K) ->
|
|
|
|
mnesia:delete({acl, K})
|
|
|
|
end, Ksa);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
mnesia:write(R)
|
|
|
|
end, Opts)
|
|
|
|
end,
|
2007-12-08 19:39:23 +01:00
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, _} -> ok;
|
|
|
|
{aborted,{no_exists,Table}} ->
|
|
|
|
MnesiaDirectory = mnesia:system_info(directory),
|
|
|
|
?ERROR_MSG("Error reading Mnesia database spool files:~n"
|
|
|
|
"The Mnesia database couldn't read the spool file for the table '~p'.~n"
|
|
|
|
"ejabberd needs read and write access in the directory:~n ~s~n"
|
|
|
|
"Maybe the problem is a change in the computer hostname,~n"
|
|
|
|
"or a change in the Erlang node name, which is currently:~n ~p~n"
|
|
|
|
"Check the ejabberd guide for details about changing the~n"
|
|
|
|
"computer hostname or Erlang node name.~n",
|
|
|
|
[Table, MnesiaDirectory, node()]),
|
|
|
|
exit("Error reading Mnesia database")
|
|
|
|
end.
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
add_global_option(Opt, Val) ->
|
|
|
|
mnesia:transaction(fun() ->
|
|
|
|
mnesia:write(#config{key = Opt,
|
|
|
|
value = Val})
|
|
|
|
end).
|
|
|
|
|
|
|
|
add_local_option(Opt, Val) ->
|
|
|
|
mnesia:transaction(fun() ->
|
|
|
|
mnesia:write(#local_config{key = Opt,
|
|
|
|
value = Val})
|
|
|
|
end).
|
|
|
|
|
|
|
|
|
|
|
|
get_global_option(Opt) ->
|
|
|
|
case ets:lookup(config, Opt) of
|
|
|
|
[#config{value = Val}] ->
|
|
|
|
Val;
|
|
|
|
_ ->
|
|
|
|
undefined
|
|
|
|
end.
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
get_local_option(Opt) ->
|
|
|
|
case ets:lookup(local_config, Opt) of
|
|
|
|
[#local_config{value = Val}] ->
|
2003-01-07 20:10:35 +01:00
|
|
|
Val;
|
|
|
|
_ ->
|
|
|
|
undefined
|
|
|
|
end.
|
|
|
|
|
2008-02-19 14:49:29 +01:00
|
|
|
%% Return the list of hosts handled by a given module
|
|
|
|
get_vh_by_auth_method(AuthMethod) ->
|
|
|
|
mnesia:dirty_select(local_config,
|
|
|
|
[{#local_config{key = {auth_method, '$1'},
|
|
|
|
value=AuthMethod},[],['$1']}]).
|
2009-02-16 18:57:30 +01:00
|
|
|
|
|
|
|
%% @spec (Path::string()) -> true | false
|
|
|
|
is_file_readable(Path) ->
|
|
|
|
case file:read_file_info(Path) of
|
|
|
|
{ok, FileInfo} ->
|
|
|
|
case {FileInfo#file_info.type, FileInfo#file_info.access} of
|
|
|
|
{regular, read} -> true;
|
|
|
|
{regular, read_write} -> true;
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
{error, _Reason} ->
|
|
|
|
false
|
|
|
|
end.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
replace_module(mod_announce_odbc) -> {mod_announce, odbc};
|
|
|
|
replace_module(mod_blocking_odbc) -> {mod_blocking, odbc};
|
|
|
|
replace_module(mod_irc_odbc) -> {mod_irc, odbc};
|
|
|
|
replace_module(mod_last_odbc) -> {mod_last, odbc};
|
|
|
|
replace_module(mod_muc_odbc) -> {mod_muc, odbc};
|
|
|
|
replace_module(mod_offline_odbc) -> {mod_offline, odbc};
|
|
|
|
replace_module(mod_privacy_odbc) -> {mod_privacy, odbc};
|
|
|
|
replace_module(mod_private_odbc) -> {mod_private, odbc};
|
|
|
|
replace_module(mod_roster_odbc) -> {mod_roster, odbc};
|
|
|
|
replace_module(mod_shared_roster_odbc) -> {mod_shared_roster, odbc};
|
|
|
|
replace_module(mod_vcard_odbc) -> {mod_vcard, odbc};
|
|
|
|
replace_module(mod_vcard_xupdate_odbc) -> {mod_vcard_xupdate, odbc};
|
|
|
|
replace_module(Module) -> Module.
|
|
|
|
|
|
|
|
replace_modules(Modules) ->
|
|
|
|
lists:map(
|
|
|
|
fun({Module, Opts}) ->
|
|
|
|
case replace_module(Module) of
|
|
|
|
{NewModule, DBType} ->
|
|
|
|
NewOpts = [{db_type, DBType} |
|
|
|
|
lists:keydelete(db_type, 1, Opts)],
|
|
|
|
{NewModule, NewOpts};
|
|
|
|
NewModule ->
|
|
|
|
{NewModule, Opts}
|
|
|
|
end
|
|
|
|
end, Modules).
|