aboutsummaryrefslogtreecommitdiff
path: root/src/econf.erl
blob: ec127c4e18f7210202ae45aa1456f5249b9f5d2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
%%%----------------------------------------------------------------------
%%% File    : econf.erl
%%% Purpose : Validator for ejabberd configuration options
%%%
%%%
%%% ejabberd, Copyright (C) 2002-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(econf).

%% API
-export([parse/3, validate/2, fail/1, format_error/2, replace_macros/1]).
%% Simple types
-export([pos_int/0, pos_int/1, non_neg_int/0, non_neg_int/1]).
-export([int/0, int/2, number/1, octal/0]).
-export([binary/0, binary/1, binary/2]).
-export([string/0, string/1, string/2]).
-export([enum/1, bool/0, atom/0, any/0]).
%% Complex types
-export([url/0, url/1]).
-export([file/0, file/1]).
-export([directory/0, directory/1]).
-export([ip/0, ipv4/0, ipv6/0, ip_mask/0, port/0]).
-export([re/0, re/1, glob/0, glob/1]).
-export([path/0, binary_sep/1]).
-export([beam/0, beam/1]).
-export([timeout/1, timeout/2]).
%% Composite types
-export([list/1, list/2]).
-export([list_or_single/1, list_or_single/2]).
-export([map/2, map/3]).
-export([either/2, and_then/2, non_empty/1]).
-export([options/1, options/2]).
%% Custom types
-export([acl/0, shaper/0, url_or_file/0, lang/0]).
-export([pem/0, queue_type/0]).
-export([jid/0, user/0, domain/0, resource/0]).
-export([db_type/1, ldap_filter/0]).
-export([host/0, hosts/0]).
-ifdef(SIP).
-export([sip_uri/0]).
-endif.

-type error_reason() :: term().
-type error_return() :: {error, error_reason(), yconf:ctx()}.
-type validator() :: yconf:validator().
-type validator(T) :: yconf:validator(T).
-type validators() :: yconf:validators().
-export_type([validator/0, validator/1, validators/0]).
-export_type([error_reason/0, error_return/0]).

%%%===================================================================
%%% API
%%%===================================================================
parse(File, Validators, Options) ->
    try yconf:parse(File, Validators, Options)
    catch _:{?MODULE, Reason, Ctx} ->
	    {error, Reason, Ctx}
    end.

validate(Validator, Y) ->
    try yconf:validate(Validator, Y)
    catch _:{?MODULE, Reason, Ctx} ->
	    {error, Reason, Ctx}
    end.

replace_macros(Y) ->
    yconf:replace_macros(Y).

-spec fail(error_reason()) -> no_return().
fail(Reason) ->
    yconf:fail(?MODULE, Reason).

format_error({bad_module, Mod}, Ctx)
  when Ctx == [listen, module];
       Ctx == [listen, request_handlers] ->
    Mods = ejabberd_config:beams(all),
    format("~s: unknown ~s: ~s. Did you mean ~s?",
	   [yconf:format_ctx(Ctx),
	    format_module_type(Ctx),
	    format_module(Mod),
	    format_module(misc:best_match(Mod, Mods))]);
format_error({bad_module, Mod}, Ctx)
  when Ctx == [modules] ->
    Mods = lists:filter(
	     fun(M) ->
		     case atom_to_list(M) of
			 "mod_" ++ _ -> true;
			 "Elixir.Mod" ++ _ -> true;
			 _ -> false
		     end
	     end, ejabberd_config:beams(all)),
    format("~s: unknown ~s: ~s. Did you mean ~s?",
	   [yconf:format_ctx(Ctx),
	    format_module_type(Ctx),
	    format_module(Mod),
	    format_module(misc:best_match(Mod, Mods))]);
format_error({bad_export, {F, A}, Mod}, Ctx)
  when Ctx == [listen, module];
       Ctx == [listen, request_handlers];
       Ctx == [modules] ->
    Type = format_module_type(Ctx),
    Slogan = yconf:format_ctx(Ctx),
    case lists:member(Mod, ejabberd_config:beams(local)) of
	true ->
	    format("~s: '~s' is not a ~s",
		   [Slogan, format_module(Mod), Type]);
	false ->
	    case lists:member(Mod, ejabberd_config:beams(external)) of
		true ->
		    format("~s: third-party ~s '~s' doesn't export "
			   "function ~s/~B. If it's really a ~s, "
			   "consider to upgrade it",
			   [Slogan, Type, format_module(Mod),F, A, Type]);
		false ->
		    format("~s: '~s' doesn't match any known ~s",
			   [Slogan, format_module(Mod), Type])
	    end
    end;
format_error({unknown_option, [], _} = Why, Ctx) ->
    format("~s. There are no available options",
	   [yconf:format_error(Why, Ctx)]);
format_error({unknown_option, Known, Opt} = Why, Ctx) ->
    format("~s. Did you mean ~s? ~s",
	   [yconf:format_error(Why, Ctx),
	    misc:best_match(Opt, Known),
	    format_known("Available options", Known)]);
format_error({bad_enum, Known, Bad} = Why, Ctx) ->
    format("~s. Did you mean ~s? ~s",
	   [yconf:format_error(Why, Ctx),
	    misc:best_match(Bad, Known),
	    format_known("Possible values", Known)]);
format_error({bad_yaml, _, _} = Why, _) ->
    format_error(Why);
format_error(Reason, Ctx) ->
    [H|T] = format_error(Reason),
    yconf:format_ctx(Ctx) ++ ": " ++ [string:to_lower(H)|T].

format_error({bad_db_type, _, Atom}) ->
    format("unsupported database: ~s", [Atom]);
format_error({bad_lang, Lang}) ->
    format("Invalid language tag: ~s", [Lang]);
format_error({bad_pem, Why, Path}) ->
    format("Failed to read PEM file '~s': ~s",
	   [Path, pkix:format_error(Why)]);
format_error({bad_cert, Why, Path}) ->
    format_error({bad_pem, Why, Path});
format_error({bad_jwt_key, Path}) ->
    format("No valid JWT key found in file: ~s", [Path]);
format_error({bad_jid, Bad}) ->
    format("Invalid XMPP address: ~s", [Bad]);
format_error({bad_user, Bad}) ->
    format("Invalid user part: ~s", [Bad]);
format_error({bad_domain, Bad}) ->
    format("Invalid domain: ~s", [Bad]);
format_error({bad_resource, Bad}) ->
    format("Invalid resource part: ~s", [Bad]);
format_error({bad_ldap_filter, Bad}) ->
    format("Invalid LDAP filter: ~s", [Bad]);
format_error({bad_sip_uri, Bad}) ->
    format("Invalid SIP URI: ~s", [Bad]);
format_error({route_conflict, R}) ->
    format("Failed to reuse route '~s' because it's "
	   "already registered on a virtual host",
	   [R]);
format_error({listener_dup, AddrPort}) ->
    format("Overlapping listeners found at ~s",
	   [format_addr_port(AddrPort)]);
format_error({listener_conflict, AddrPort1, AddrPort2}) ->
    format("Overlapping listeners found at ~s and ~s",
	   [format_addr_port(AddrPort1),
	    format_addr_port(AddrPort2)]);
format_error({invalid_syntax, Reason}) ->
    format("~s", [Reason]);
format_error({missing_module_dep, Mod, DepMod}) ->
    format("module ~s depends on module ~s, "
	   "which is not found in the config",
	   [Mod, DepMod]);
format_error(eimp_error) ->
    format("ejabberd is built without image converter support", []);
format_error({mqtt_codec, Reason}) ->
    mqtt_codec:format_error(Reason);
format_error(Reason) ->
    yconf:format_error(Reason).

-spec format_module(atom()) -> string().
format_module(Mod) ->
    case atom_to_list(Mod) of
	"Elixir." ++ M -> M;
	M -> M
    end.

format_module_type([listen, module]) ->
    "listening module";
format_module_type([listen, request_handlers]) ->
    "HTTP request handler";
format_module_type([modules]) ->
    "ejabberd module".

format_known(_, Known) when length(Known) > 20 ->
    "";
format_known(Prefix, Known) ->
    [Prefix, " are: ", format_join(Known)].

format_join([]) ->
    "(empty)";
format_join([H|_] = L) when is_atom(H) ->
    format_join([atom_to_binary(A, utf8) || A <- L]);
format_join(L) ->
    str:join(lists:sort(L), <<", ">>).

%%%===================================================================
%%% Validators from yconf
%%%===================================================================
pos_int() ->
    yconf:pos_int().

pos_int(Inf) ->
    yconf:pos_int(Inf).

non_neg_int() ->
    yconf:non_neg_int().

non_neg_int(Inf) ->
    yconf:non_neg_int(Inf).

int() ->
    yconf:int().

int(Min, Max) ->
    yconf:int(Min, Max).

number(Min) ->
    yconf:number(Min).

octal() ->
    yconf:octal().

binary() ->
    yconf:binary().

binary(Re) ->
    yconf:binary(Re).

binary(Re, Opts) ->
    yconf:binary(Re, Opts).

enum(L) ->
    yconf:enum(L).

bool() ->
    yconf:bool().

atom() ->
    yconf:atom().

string() ->
    yconf:string().

string(Re) ->
    yconf:string(Re).

string(Re, Opts) ->
    yconf:string(Re, Opts).

any() ->
    yconf:any().

url() ->
    yconf:url().

url(Schemes) ->
    yconf:url(Schemes).

file() ->
    yconf:file().

file(Type) ->
    yconf:file(Type).

directory() ->
    yconf:directory().

directory(Type) ->
    yconf:directory(Type).

ip() ->
    yconf:ip().

ipv4() ->
    yconf:ipv4().

ipv6() ->
    yconf:ipv6().

ip_mask() ->
    yconf:ip_mask().

port() ->
    yconf:port().

re() ->
    yconf:re().

re(Opts) ->
    yconf:re(Opts).

glob() ->
    yconf:glob().

glob(Opts) ->
    yconf:glob(Opts).

path() ->
    yconf:path().

binary_sep(Sep) ->
    yconf:binary_sep(Sep).

timeout(Units) ->
    yconf:timeout(Units).

timeout(Units, Inf) ->
    yconf:timeout(Units, Inf).

non_empty(F) ->
    yconf:non_empty(F).

list(F) ->
    yconf:list(F).

list(F, Opts) ->
    yconf:list(F, Opts).

list_or_single(F) ->
    yconf:list_or_single(F).

list_or_single(F, Opts) ->
    yconf:list_or_single(F, Opts).

map(F1, F2) ->
    yconf:map(F1, F2).

map(F1, F2, Opts) ->
    yconf:map(F1, F2, Opts).

either(F1, F2) ->
    yconf:either(F1, F2).

and_then(F1, F2) ->
    yconf:and_then(F1, F2).

options(V) ->
    yconf:options(V).

options(V, O) ->
    yconf:options(V, O).

%%%===================================================================
%%% Custom validators
%%%===================================================================
beam() ->
    beam([]).

beam(Exports) ->
    and_then(
      non_empty(binary()),
      fun(<<"Elixir.", _/binary>> = Val) ->
	      (yconf:beam(Exports))(Val);
	 (<<C, _/binary>> = Val) when C >= $A, C =< $Z ->
	      (yconf:beam(Exports))(<<"Elixir.", Val/binary>>);
	 (Val) ->
	      (yconf:beam(Exports))(Val)
      end).

acl() ->
    either(
      atom(),
      acl:access_rules_validator()).

shaper() ->
    either(
      atom(),
      ejabberd_shaper:shaper_rules_validator()).

-spec url_or_file() -> yconf:validator({file | url, binary()}).
url_or_file() ->
    either(
      and_then(url(), fun(URL) -> {url, URL} end),
      and_then(file(), fun(File) -> {file, File} end)).

-spec lang() -> yconf:validator(binary()).
lang() ->
    and_then(
      binary(),
      fun(Lang) ->
	      try xmpp_lang:check(Lang)
	      catch _:_ -> fail({bad_lang, Lang})
	      end
      end).

-spec pem() -> yconf:validator(binary()).
pem() ->
    and_then(
      path(),
      fun(Path) ->
	      case pkix:is_pem_file(Path) of
		  true -> Path;
		  {false, Reason} ->
		      fail({bad_pem, Reason, Path})
	      end
      end).

-spec jid() -> yconf:validator(jid:jid()).
jid() ->
    and_then(
      binary(),
      fun(Val) ->
	      try jid:decode(Val)
	      catch _:{bad_jid, _} = Reason -> fail(Reason)
	      end
      end).

-spec user() -> yconf:validator(binary()).
user() ->
    and_then(
      binary(),
      fun(Val) ->
	      case jid:nodeprep(Val) of
		  error -> fail({bad_user, Val});
		  U -> U
	      end
      end).

-spec domain() -> yconf:validator(binary()).
domain() ->
    and_then(
      non_empty(binary()),
      fun(Val) ->
	      try jid:tolower(jid:decode(Val)) of
		  {<<"">>, Domain, <<"">>} -> Domain;
		  _ -> fail({bad_domain, Val})
	      catch _:{bad_jid, _} ->
		      fail({bad_domain, Val})
	      end
      end).

-spec resource() -> yconf:validator(binary()).
resource() ->
    and_then(
      binary(),
      fun(Val) ->
	      case jid:resourceprep(Val) of
		  error -> fail({bad_resource, Val});
		  R -> R
	      end
      end).

-spec db_type(module()) -> yconf:validator(atom()).
db_type(M) ->
    and_then(
      atom(),
      fun(T) ->
	      case code:ensure_loaded(db_module(M, T)) of
		  {module, _} -> T;
		  {error, _} -> fail({bad_db_type, M, T})
	      end
      end).

-spec queue_type() -> yconf:validator(ram | file).
queue_type() ->
    enum([ram, file]).

-spec ldap_filter() -> yconf:validator(binary()).
ldap_filter() ->
    and_then(
      binary(),
      fun(Val) ->
	      case eldap_filter:parse(Val) of
		  {ok, _} -> Val;
		  _ -> fail({bad_ldap_filter, Val})
	      end
      end).

-ifdef(SIP).
sip_uri() ->
    and_then(
      binary(),
      fun(Val) ->
	      case esip:decode_uri(Val) of
		  error -> fail({bad_sip_uri, Val});
		  URI -> URI
	      end
      end).
-endif.

-spec host() -> yconf:validator(binary()).
host() ->
    fun(Domain) ->
	    Host = ejabberd_config:get_myname(),
	    Hosts = ejabberd_config:get_option(hosts),
	    Domain1 = (binary())(Domain),
	    Domain2 = misc:expand_keyword(<<"@HOST@">>, Domain1, Host),
	    Domain3 = (domain())(Domain2),
	    case lists:member(Domain3, Hosts) of
		true -> fail({route_conflict, Domain3});
		false -> Domain3
	    end
    end.

-spec hosts() -> yconf:validator([binary()]).
hosts() ->
    list(host(), [unique]).

%%%===================================================================
%%% Internal functions
%%%===================================================================
-spec db_module(module(), atom()) -> module().
db_module(M, Type) ->
    try list_to_atom(atom_to_list(M) ++ "_" ++ atom_to_list(Type))
    catch _:system_limit ->
	    fail({bad_length, 255})
    end.

format_addr_port({IP, Port}) ->
    IPStr = case tuple_size(IP) of
		4 -> inet:ntoa(IP);
		8 -> "[" ++ inet:ntoa(IP) ++ "]"
	    end,
    IPStr ++ ":" ++ integer_to_list(Port).

-spec format(iolist(), list()) -> string().
format(Fmt, Args) ->
    lists:flatten(io_lib:format(Fmt, Args)).