./ct_report/coverage/mongoose_config_spec.COVER.html

1 -module(mongoose_config_spec).
2
3 %% entry point - returns the entire spec
4 -export([root/0]).
5
6 %% spec parts used by http handlers, modules and services
7 -export([wpool/1,
8 iqdisc/0,
9 tls/2]).
10
11 %% callbacks for the 'process' step
12 -export([process_root/1,
13 process_host/1,
14 process_general/1,
15 process_listener/2,
16 process_c2s_tls/1,
17 process_c2s_just_tls/1,
18 process_just_tls/1,
19 process_fast_tls/1,
20 process_sasl_external/1,
21 process_sasl_mechanism/1,
22 process_auth/1,
23 process_pool/2,
24 process_ldap_connection/1,
25 process_iqdisc/1,
26 process_acl_condition/1,
27 process_s2s_host_policy/1,
28 process_s2s_address/1,
29 process_domain_cert/1,
30 process_infinity_as_zero/1]).
31
32 -include("mongoose_config_spec.hrl").
33
34 -type config_node() :: config_section() | config_list() | config_option().
35 -type config_section() :: #section{}.
36 -type config_list() :: #list{}.
37 -type config_option() :: #option{}.
38
39 -type option_type() :: boolean | binary | string | atom | int_or_infinity
40 | int_or_atom | integer | float.
41
42 -type wrapper() :: top_level_config_wrapper() | config_part_wrapper().
43
44 %% Wrap the value in a top-level config option
45 -type top_level_config_wrapper() ::
46 global_config % [{Key, Value}]
47 | host_config. % Inside host_config: [{{Key, Host}, Value}]
48 % Otherwise: one such option for each configured host
49
50 %% Wrap the value in a nested config part - key-value pair or just a value
51 -type config_part_wrapper() ::
52 default % [{Key, Value}] for section items, [Value] for list items
53 | item % [Value]
54 | remove % [] - the item is ignored
55 | none. % just Value - injects elements of Value into the parent section/list
56
57 %% This option allows to put list/section items in a map
58 -type format_items() ::
59 list % keep the processed items in a list
60 | map. % convert the processed items (which have to be a KV list) to a map
61
62 -export_type([config_node/0, config_section/0, config_list/0, config_option/0,
63 wrapper/0, format_items/0, option_type/0]).
64
65 %% Config processing functions are annotated with TOML paths
66 %% Path syntax: dotted, like TOML keys with the following additions:
67 %% - '[]' denotes an element in a list
68 %% - '( ... )' encloses an optional prefix
69 %% - '*' is a wildcard for names - usually that name is passed as an argument
70 %% If the path is the same as for the previous function, it is not repeated.
71 %%
72 %% Example: (host_config[].)access.*
73 %% Meaning: either a key in the 'access' section, e.g.
74 %% [access]
75 %% local = ...
76 %% or the same, but prefixed for a specific host, e.g.
77 %% [[host_config]]
78 %% host = "myhost"
79 %% host_config.access
80 %% local = ...
81
82 root() ->
83 93 General = general(),
84 93 Listen = listen(),
85 93 Auth = auth(),
86 93 Modules = modules(),
87 93 S2S = s2s(),
88 93 #section{
89 items = #{<<"general">> => General#section{required = [<<"default_server_domain">>],
90 process = fun ?MODULE:process_general/1,
91 defaults = general_defaults()},
92 <<"listen">> => Listen#section{include = always},
93 <<"auth">> => Auth#section{include = always},
94 <<"outgoing_pools">> => outgoing_pools(),
95 <<"internal_databases">> => internal_databases(),
96 <<"services">> => services(),
97 <<"modules">> => Modules#section{include = always},
98 <<"instrumentation">> => instrumentation(),
99 <<"shaper">> => shaper(),
100 <<"acl">> => acl(),
101 <<"access">> => access(),
102 <<"s2s">> => S2S#section{include = always},
103 <<"host_config">> => #list{items = host_config(),
104 wrap = none}
105 },
106 defaults = #{<<"internal_databases">> => default_internal_databases()},
107 required = [<<"general">>],
108 process = fun ?MODULE:process_root/1,
109 wrap = none,
110 format_items = list
111 }.
112
113 %% path: host_config[]
114 host_config() ->
115 93 #section{
116 items = #{%% Host is only validated here - it is stored in the path,
117 %% see mongoose_config_parser_toml:item_key/1
118 %%
119 %% for every configured host the host_type of the same name
120 %% is declared automatically. As host_config section is now
121 %% used for changing configuration of the host_type, we don't
122 %% need host option any more. but to stay compatible with an
123 %% old config format we keep host option as well. now it is
124 %% just a synonym to host_type.
125 <<"host">> => #option{type = binary,
126 validate = non_empty,
127 wrap = remove},
128
129 <<"host_type">> => #option{type = binary,
130 validate = non_empty,
131 wrap = remove},
132
133 %% Sections below are allowed in host_config,
134 %% but only options with 'wrap = host_config' are accepted.
135 %% Options with 'wrap = global_config' would be caught by
136 %% mongoose_config_parser_toml:wrap/3
137 <<"general">> => general(),
138 <<"auth">> => auth(),
139 <<"modules">> => modules(),
140 <<"acl">> => acl(),
141 <<"access">> => access(),
142 <<"s2s">> => s2s()
143 },
144 wrap = none,
145 format_items = list
146 }.
147
148 %% path: general
149 general() ->
150 186 #section{
151 items = #{<<"loglevel">> => #option{type = atom,
152 validate = loglevel,
153 wrap = global_config},
154 <<"hosts">> => #list{items = #option{type = binary,
155 validate = non_empty,
156 process = fun ?MODULE:process_host/1},
157 validate = unique,
158 wrap = global_config},
159 <<"host_types">> => #list{items = #option{type = binary,
160 validate = non_empty},
161 validate = unique,
162 wrap = global_config},
163 <<"default_server_domain">> => #option{type = binary,
164 validate = non_empty,
165 process = fun ?MODULE:process_host/1,
166 wrap = global_config},
167 <<"registration_timeout">> => #option{type = int_or_infinity,
168 validate = positive,
169 wrap = global_config},
170 <<"language">> => #option{type = binary,
171 validate = non_empty,
172 wrap = global_config},
173 <<"all_metrics_are_global">> => #option{type = boolean,
174 wrap = global_config},
175 <<"sm_backend">> => #option{type = atom,
176 validate = {module, ejabberd_sm},
177 wrap = global_config},
178 <<"component_backend">> => #option{type = atom,
179 validate = {module, mongoose_component},
180 wrap = global_config},
181 <<"s2s_backend">> => #option{type = atom,
182 validate = {module, mongoose_s2s},
183 wrap = global_config},
184 <<"max_fsm_queue">> => #option{type = integer,
185 validate = positive,
186 wrap = global_config},
187 <<"http_server_name">> => #option{type = string,
188 wrap = global_config},
189 <<"rdbms_server_type">> => #option{type = atom,
190 validate = {enum, [mssql, pgsql]},
191 wrap = global_config},
192 <<"route_subdomains">> => #option{type = atom,
193 validate = {enum, [s2s]},
194 wrap = host_config},
195 <<"routing_modules">> => #list{items = #option{type = atom,
196 validate = module},
197 process = fun xmpp_router:expand_routing_modules/1,
198 wrap = global_config},
199 <<"replaced_wait_timeout">> => #option{type = integer,
200 validate = positive,
201 wrap = host_config},
202 <<"hide_service_name">> => #option{type = boolean,
203 wrap = global_config},
204 <<"domain_certfile">> => #list{items = domain_cert(),
205 format_items = map,
206 wrap = global_config}
207 },
208 wrap = none,
209 format_items = list
210 }.
211
212 general_defaults() ->
213 93 #{<<"loglevel">> => warning,
214 <<"hosts">> => [],
215 <<"host_types">> => [],
216 <<"registration_timeout">> => 600,
217 <<"language">> => <<"en">>,
218 <<"all_metrics_are_global">> => false,
219 <<"sm_backend">> => mnesia,
220 <<"component_backend">> => mnesia,
221 <<"s2s_backend">> => mnesia,
222 <<"rdbms_server_type">> => generic,
223 <<"routing_modules">> => mongoose_router:default_routing_modules(),
224 <<"replaced_wait_timeout">> => 2000,
225 <<"hide_service_name">> => false}.
226
227 %% path: general.domain_certfile
228 domain_cert() ->
229 186 #section{
230 items = #{<<"domain">> => #option{type = binary,
231 validate = non_empty},
232 <<"certfile">> => #option{type = string,
233 validate = filename}},
234 required = all,
235 process = fun ?MODULE:process_domain_cert/1
236 }.
237
238 %% path: listen
239 listen() ->
240 93 Keys = [c2s, s2s, service, http],
241 93 #section{
242 372 items = maps:from_list([{atom_to_binary(Key), #list{items = listener(Key), wrap = none}}
243 93 || Key <- Keys]),
244 process = fun mongoose_listener_config:verify_unique_listeners/1,
245 wrap = global_config,
246 format_items = list
247 }.
248
249 %% path: listen.*[]
250 listener(Type) ->
251 372 mongoose_config_utils:merge_sections(listener_common(), listener_extra(Type)).
252
253 listener_common() ->
254 372 #section{items = #{<<"port">> => #option{type = integer,
255 validate = port},
256 <<"ip_address">> => #option{type = string,
257 validate = ip_address},
258 <<"proto">> => #option{type = atom,
259 validate = {enum, [tcp]}},
260 <<"ip_version">> => #option{type = integer,
261 validate = {enum, [4, 6]}}
262 },
263 required = [<<"port">>],
264 defaults = #{<<"proto">> => tcp},
265 process = fun ?MODULE:process_listener/2
266 }.
267
268 listener_extra(http) ->
269 %% tls options passed to ranch_ssl (with verify_mode translated to verify_fun)
270 93 #section{items = #{<<"tls">> => tls([server], [just_tls]),
271 <<"transport">> => http_transport(),
272 <<"protocol">> => http_protocol(),
273 <<"handlers">> => mongoose_http_handler:config_spec()}};
274 listener_extra(Type) ->
275 279 mongoose_config_utils:merge_sections(xmpp_listener_common(), xmpp_listener_extra(Type)).
276
277 xmpp_listener_common() ->
278 279 #section{items = #{<<"backlog">> => #option{type = integer,
279 validate = non_negative},
280 <<"proxy_protocol">> => #option{type = boolean},
281 <<"hibernate_after">> => #option{type = int_or_infinity,
282 validate = non_negative},
283 <<"max_stanza_size">> => #option{type = int_or_infinity,
284 validate = positive,
285 process = fun ?MODULE:process_infinity_as_zero/1},
286 <<"num_acceptors">> => #option{type = integer,
287 validate = positive}
288 },
289 defaults = #{<<"backlog">> => 1024,
290 <<"proxy_protocol">> => false,
291 <<"hibernate_after">> => 0,
292 <<"max_stanza_size">> => 0,
293 <<"num_acceptors">> => 100}
294 }.
295
296 xmpp_listener_extra(c2s) ->
297 93 #section{items = #{<<"access">> => #option{type = atom,
298 validate = non_empty},
299 <<"shaper">> => #option{type = atom,
300 validate = non_empty},
301 <<"max_connections">> => #option{type = int_or_infinity,
302 validate = positive},
303 <<"c2s_state_timeout">> => #option{type = int_or_infinity,
304 validate = non_negative},
305 <<"reuse_port">> => #option{type = boolean},
306 <<"backwards_compatible_session">> => #option{type = boolean},
307 <<"allowed_auth_methods">> =>
308 #list{items = #option{type = atom,
309 validate = {module, ejabberd_auth}},
310 validate = unique},
311 <<"tls">> => tls([server, c2s], [fast_tls, just_tls])},
312 defaults = #{<<"access">> => all,
313 <<"shaper">> => none,
314 <<"max_connections">> => infinity,
315 <<"c2s_state_timeout">> => 5000,
316 <<"reuse_port">> => false,
317 <<"backwards_compatible_session">> => true}
318 };
319 xmpp_listener_extra(s2s) ->
320 93 TLSSection = tls([server], [fast_tls]),
321 93 #section{items = #{<<"shaper">> => #option{type = atom,
322 validate = non_empty},
323 <<"tls">> => TLSSection#section{include = always}},
324 defaults = #{<<"shaper">> => none}
325 };
326 xmpp_listener_extra(service) ->
327 93 #section{items = #{<<"access">> => #option{type = atom,
328 validate = non_empty},
329 <<"shaper_rule">> => #option{type = atom,
330 validate = non_empty},
331 <<"check_from">> => #option{type = boolean},
332 <<"hidden_components">> => #option{type = boolean},
333 <<"conflict_behaviour">> => #option{type = atom,
334 validate = {enum, [kick_old, disconnect]}},
335 <<"password">> => #option{type = string,
336 validate = non_empty},
337 <<"max_fsm_queue">> => #option{type = integer,
338 validate = positive}
339 },
340 required = [<<"password">>],
341 defaults = #{<<"access">> => all,
342 <<"shaper_rule">> => none,
343 <<"check_from">> => true,
344 <<"hidden_components">> => false,
345 <<"conflict_behaviour">> => disconnect}
346 }.
347
348 %% path: listen.http[].transport
349 http_transport() ->
350 93 #section{
351 items = #{<<"num_acceptors">> => #option{type = integer,
352 validate = positive},
353 <<"max_connections">> => #option{type = int_or_infinity,
354 validate = non_negative}
355 },
356 defaults = #{<<"num_acceptors">> => 100,
357 <<"max_connections">> => 1024},
358 include = always
359 }.
360
361 %% path: listen.http[].protocol
362 http_protocol() ->
363 93 #section{
364 items = #{<<"compress">> => #option{type = boolean}},
365 defaults = #{<<"compress">> => false},
366 include = always
367 }.
368
369 %% path: (host_config[].)auth
370 auth() ->
371 186 Items = maps:from_list([{a2b(Method), ejabberd_auth:config_spec(Method)} ||
372 186 Method <- all_auth_methods()]),
373 186 #section{
374 items = Items#{<<"methods">> => #list{items = #option{type = atom,
375 validate = {module, ejabberd_auth}}},
376 <<"password">> => auth_password(),
377 <<"sasl_external">> =>
378 #list{items = #option{type = atom,
379 process = fun ?MODULE:process_sasl_external/1}},
380 <<"sasl_mechanisms">> =>
381 #list{items = #option{type = atom,
382 validate = {module, cyrsasl},
383 process = fun ?MODULE:process_sasl_mechanism/1}},
384 <<"max_users_per_domain">> => #option{type = int_or_infinity,
385 validate = positive}
386 },
387 defaults = #{<<"sasl_external">> => [standard],
388 <<"sasl_mechanisms">> => cyrsasl:default_modules(),
389 <<"max_users_per_domain">> => infinity},
390 process = fun ?MODULE:process_auth/1,
391 wrap = host_config
392 }.
393
394 %% path: (host_config[].)auth.password
395 auth_password() ->
396 186 #section{
397 items = #{<<"format">> => #option{type = atom,
398 validate = {enum, [scram, plain]}},
399 <<"hash">> => #list{items = #option{type = atom,
400 validate = {enum, [sha, sha224, sha256,
401 sha384, sha512]}},
402 validate = unique_non_empty
403 },
404 <<"scram_iterations">> => #option{type = integer,
405 validate = positive}
406 },
407 defaults = #{<<"format">> => scram,
408 <<"scram_iterations">> => mongoose_scram:iterations()},
409 include = always
410 }.
411
412 %% path: internal_databases
413 internal_databases() ->
414 93 Items = #{<<"cets">> => internal_database_cets(),
415 <<"mnesia">> => internal_database_mnesia()},
416 93 #section{items = Items,
417 format_items = map,
418 wrap = global_config}.
419
420 default_internal_databases() ->
421 93 #{mnesia => #{}}.
422
423 %% path: internal_databases.cets
424 internal_database_cets() ->
425 93 #section{
426 items = #{<<"backend">> => #option{type = atom,
427 validate = {enum, [file, rdbms]}},
428 <<"cluster_name">> => #option{type = atom, validate = non_empty},
429 %% Relative to the release directory (or an absolute name)
430 <<"node_list_file">> => #option{type = string,
431 validate = filename}
432 },
433 defaults = #{<<"backend">> => rdbms, <<"cluster_name">> => mongooseim}
434 }.
435
436 %% path: internal_databases.mnesia
437 internal_database_mnesia() ->
438 93 #section{}.
439
440 %% path: outgoing_pools
441 outgoing_pools() ->
442 93 PoolTypes = [<<"cassandra">>, <<"elastic">>, <<"http">>, <<"ldap">>,
443 <<"rabbit">>, <<"rdbms">>, <<"redis">>],
444 93 Items = [{Type, #section{items = #{default => outgoing_pool(Type)},
445 validate_keys = non_empty,
446 wrap = none,
447 93 format_items = list}} || Type <- PoolTypes],
448 93 #section{items = maps:from_list(Items),
449 format_items = list,
450 wrap = global_config,
451 include = always}.
452
453 %% path: outgoing_pools.*.*
454 outgoing_pool(Type) ->
455 651 ExtraDefaults = extra_wpool_defaults(Type),
456 651 Pool = mongoose_config_utils:merge_sections(wpool(ExtraDefaults), outgoing_pool_extra(Type)),
457 651 Pool#section{wrap = item}.
458
459 extra_wpool_defaults(<<"cassandra">>) ->
460 93 #{<<"workers">> => 20};
461 extra_wpool_defaults(<<"rdbms">>) ->
462 93 #{<<"call_timeout">> => 60000};
463 extra_wpool_defaults(_) ->
464 465 #{}.
465
466 wpool(ExtraDefaults) ->
467 1023 #section{items = #{<<"workers">> => #option{type = integer,
468 validate = positive},
469 <<"strategy">> => #option{type = atom,
470 validate = {enum, wpool_strategy_values()}},
471 <<"call_timeout">> => #option{type = integer,
472 validate = positive}
473 },
474 defaults = maps:merge(#{<<"workers">> => 10,
475 <<"strategy">> => best_worker,
476 <<"call_timeout">> => 5000}, ExtraDefaults)}.
477
478 outgoing_pool_extra(Type) ->
479 651 #section{items = #{<<"scope">> => #option{type = atom,
480 validate = {enum, [global, host, single_host]}},
481 <<"host">> => #option{type = binary,
482 validate = non_empty},
483 <<"connection">> => outgoing_pool_connection(Type)
484 },
485 process = fun ?MODULE:process_pool/2,
486 defaults = #{<<"scope">> => global}
487 }.
488
489 %% path: outgoing_pools.*.*.connection
490 outgoing_pool_connection(<<"cassandra">>) ->
491 93 #section{
492 items = #{<<"servers">> => #list{items = cassandra_server(),
493 validate = unique_non_empty},
494 <<"keyspace">> => #option{type = atom,
495 validate = non_empty},
496 <<"auth">> => #section{items = #{<<"plain">> => cassandra_auth_plain()},
497 required = all},
498 <<"tls">> => tls([client], [just_tls])
499 },
500 include = always,
501 defaults = #{<<"servers">> => [#{host => "localhost", port => 9042}],
502 <<"keyspace">> => mongooseim}
503 };
504 outgoing_pool_connection(<<"elastic">>) ->
505 93 #section{
506 items = #{<<"host">> => #option{type = binary,
507 validate = non_empty},
508 <<"port">> => #option{type = integer,
509 validate = port}
510 },
511 include = always,
512 defaults = #{<<"host">> => <<"localhost">>,
513 <<"port">> => 9200}
514 };
515 outgoing_pool_connection(<<"http">>) ->
516 93 #section{
517 items = #{<<"host">> => #option{type = string,
518 validate = non_empty},
519 <<"path_prefix">> => #option{type = binary,
520 validate = non_empty},
521 <<"request_timeout">> => #option{type = integer,
522 validate = non_negative},
523 <<"tls">> => tls([client], [just_tls])
524 },
525 include = always,
526 required = [<<"host">>],
527 defaults = #{<<"path_prefix">> => <<"/">>,
528 <<"request_timeout">> => 2000}
529 };
530 outgoing_pool_connection(<<"ldap">>) ->
531 93 #section{
532 items = #{<<"servers">> => #list{items = #option{type = string},
533 validate = unique_non_empty},
534 <<"port">> => #option{type = integer,
535 validate = port},
536 <<"root_dn">> => #option{type = binary},
537 <<"password">> => #option{type = binary},
538 <<"connect_interval">> => #option{type = integer,
539 validate = positive},
540 <<"tls">> => tls([client], [just_tls])
541 },
542 include = always,
543 defaults = #{<<"servers">> => ["localhost"],
544 <<"root_dn">> => <<>>,
545 <<"password">> => <<>>,
546 <<"connect_interval">> => 10000},
547 process = fun ?MODULE:process_ldap_connection/1
548 };
549 outgoing_pool_connection(<<"rabbit">>) ->
550 93 #section{
551 items = #{<<"host">> => #option{type = string,
552 validate = non_empty},
553 <<"port">> => #option{type = integer,
554 validate = port},
555 <<"username">> => #option{type = binary,
556 validate = non_empty},
557 <<"password">> => #option{type = binary,
558 validate = non_empty},
559 <<"confirms_enabled">> => #option{type = boolean},
560 <<"max_worker_queue_len">> => #option{type = int_or_infinity,
561 validate = non_negative}
562 },
563 include = always,
564 defaults = #{<<"host">> => "localhost",
565 <<"port">> => 5672,
566 <<"username">> => <<"guest">>,
567 <<"password">> => <<"guest">>,
568 <<"confirms_enabled">> => false,
569 <<"max_worker_queue_len">> => 1000}
570 };
571 outgoing_pool_connection(<<"rdbms">>) ->
572 93 #section{
573 items = #{<<"driver">> => #option{type = atom,
574 validate = {enum, [odbc, pgsql, mysql]}},
575 <<"keepalive_interval">> => #option{type = integer,
576 validate = positive},
577 <<"query_timeout">> => #option{type = integer,
578 validate = non_negative},
579 <<"max_start_interval">> => #option{type = integer,
580 validate = positive},
581
582 % odbc
583 <<"settings">> => #option{type = string},
584
585 % mysql, pgsql
586 <<"host">> => #option{type = string,
587 validate = non_empty},
588 <<"database">> => #option{type = string,
589 validate = non_empty},
590 <<"username">> => #option{type = string,
591 validate = non_empty},
592 <<"password">> => #option{type = string,
593 validate = non_empty},
594 <<"port">> => #option{type = integer,
595 validate = port},
596 <<"tls">> => sql_tls()
597 },
598 required = [<<"driver">>],
599 defaults = #{<<"query_timeout">> => 5000,
600 <<"max_start_interval">> => 30},
601 process = fun mongoose_rdbms:process_options/1
602 };
603 outgoing_pool_connection(<<"redis">>) ->
604 93 #section{
605 items = #{<<"host">> => #option{type = string,
606 validate = non_empty},
607 <<"port">> => #option{type = integer,
608 validate = port},
609 <<"database">> => #option{type = integer,
610 validate = non_negative},
611 <<"password">> => #option{type = string}
612 },
613 include = always,
614 defaults = #{<<"host">> => "127.0.0.1",
615 <<"port">> => 6379,
616 <<"database">> => 0,
617 <<"password">> => ""}
618 }.
619
620 cassandra_server() ->
621 93 #section{
622 items = #{<<"host">> => #option{type = string,
623 validate = non_empty},
624 <<"port">> => #option{type = integer,
625 validate = port}},
626 required = [<<"host">>],
627 defaults = #{<<"port">> => 9042}
628 }.
629
630 %% path: outgoing_pools.cassandra.*.connection.auth.plain
631 cassandra_auth_plain() ->
632 93 #section{
633 items = #{<<"username">> => #option{type = binary},
634 <<"password">> => #option{type = binary}},
635 required = all
636 }.
637
638 %% path: outgoing_pools.rdbms.*.connection.tls
639 sql_tls() ->
640 93 mongoose_config_utils:merge_sections(tls([client], [just_tls]), sql_tls_extra()).
641
642 sql_tls_extra() ->
643 93 #section{items = #{<<"required">> => #option{type = boolean}}}.
644
645 %% TLS options
646
647 tls(Entities, Modules) when is_list(Entities), is_list(Modules) ->
648 1023 Sections = [tls(Entity, Module) || Entity <- [common | Entities],
649 2511 Module <- [common | Modules]],
650 1023 lists:foldl(fun mongoose_config_utils:merge_sections/2, hd(Sections), tl(Sections));
651 tls(common, common) ->
652 1023 #section{items = #{<<"verify_mode">> => #option{type = atom,
653 validate = {enum, [peer, selfsigned_peer, none]}},
654 <<"certfile">> => #option{type = string,
655 validate = filename},
656 <<"cacertfile">> => #option{type = string,
657 validate = filename},
658 <<"ciphers">> => #option{type = string}
659 },
660 defaults = #{<<"verify_mode">> => peer}};
661 tls(common, fast_tls) ->
662 546 #section{items = #{<<"protocol_options">> => #list{items = #option{type = string,
663 validate = non_empty}}},
664 process = fun ?MODULE:process_fast_tls/1};
665 tls(common, just_tls) ->
666 570 #section{items = #{<<"keyfile">> => #option{type = string,
667 validate = filename},
668 <<"password">> => #option{type = string},
669 <<"versions">> => #list{items = #option{type = atom}}},
670 process = fun ?MODULE:process_just_tls/1};
671 tls(server, common) ->
672 651 #section{items = #{<<"dhfile">> => #option{type = string,
673 validate = filename}}};
674 tls(server, _) ->
675 744 #section{};
676 tls(client, common) ->
677 558 #section{};
678 tls(client, fast_tls) ->
679 186 #section{};
680 tls(client, just_tls) ->
681 372 #section{items = #{<<"server_name_indication">> => server_name_indication()}};
682 tls(c2s, common) ->
683 279 #section{items = #{<<"module">> => #option{type = atom,
684 validate = {enum, [fast_tls, just_tls]}},
685 <<"mode">> => #option{type = atom,
686 validate = {enum, [tls, starttls, starttls_required]}}},
687 defaults = #{<<"module">> => fast_tls,
688 <<"mode">> => starttls},
689 process = fun ?MODULE:process_c2s_tls/1};
690 tls(c2s, just_tls) ->
691 105 #section{items = #{<<"disconnect_on_failure">> => #option{type = boolean},
692 <<"crl_files">> => #list{items = #option{type = string,
693 validate = filename}}},
694 process = fun ?MODULE:process_c2s_just_tls/1};
695 tls(c2s, fast_tls) ->
696 267 #section{}.
697
698 server_name_indication() ->
699 372 #section{items = #{<<"enabled">> => #option{type = boolean},
700 <<"host">> => #option{type = string,
701 validate = non_empty},
702 <<"protocol">> => #option{type = atom,
703 validate = {enum, [default, https]}}
704 },
705 defaults = #{<<"enabled">> => true,
706 <<"protocol">> => default},
707 include = always}.
708
709 %% path: (host_config[].)services
710 services() ->
711 93 Services = [{a2b(Service), mongoose_service:config_spec(Service)}
712 93 || Service <- configurable_services()],
713 93 #section{
714 items = maps:from_list(Services),
715 wrap = global_config,
716 include = always
717 }.
718
719 configurable_services() ->
720 93 [service_mongoose_system_metrics,
721 service_domain_db].
722
723 %% path: (host_config[].)modules
724 modules() ->
725 186 Modules = [{a2b(Module), gen_mod:config_spec(Module)}
726 186 || Module <- configurable_modules()],
727 186 Items = maps:from_list(Modules),
728 186 #section{
729 items = Items#{default => #section{}},
730 validate_keys = module,
731 wrap = host_config
732 }.
733
734 configurable_modules() ->
735 186 [mod_adhoc,
736 mod_auth_token,
737 mod_blocking,
738 mod_bosh,
739 mod_cache_users,
740 mod_caps,
741 mod_carboncopy,
742 mod_csi,
743 mod_disco,
744 mod_event_pusher,
745 mod_extdisco,
746 mod_global_distrib,
747 mod_http_upload,
748 mod_inbox,
749 mod_jingle_sip,
750 mod_keystore,
751 mod_last,
752 mod_mam,
753 mod_muc,
754 mod_muc_light,
755 mod_muc_log,
756 mod_offline,
757 mod_offline_chatmarkers,
758 mod_ping,
759 mod_privacy,
760 mod_private,
761 mod_pubsub,
762 mod_push_service_mongoosepush,
763 mod_register,
764 mod_roster,
765 mod_shared_roster_ldap,
766 mod_smart_markers,
767 mod_sic,
768 mod_stream_management,
769 mod_time,
770 mod_vcard,
771 mod_version,
772 mod_domain_isolation].
773
774 %% path: (host_config[].)modules.*.iqdisc
775 iqdisc() ->
776 3534 #section{
777 items = #{<<"type">> => #option{type = atom,
778 validate = {enum, [no_queue, one_queue, parallel, queues]}},
779 <<"workers">> => #option{type = integer,
780 validate = positive}},
781 required = [<<"type">>],
782 process = fun ?MODULE:process_iqdisc/1
783 }.
784
785
:-(
process_iqdisc(#{type := Type, workers := N}) -> {queues = Type, N};
786
:-(
process_iqdisc(#{type := Type}) -> Type.
787
788 %% path: instrumentation
789 instrumentation() ->
790 93 #section{items = #{default => #section{}},
791 validate_keys = {module, mongoose_instrument},
792 wrap = global_config,
793 include = always
794 }.
795
796 %% path: shaper
797 shaper() ->
798 93 #section{
799 items = #{default =>
800 #section{
801 items = #{<<"max_rate">> => #option{type = integer,
802 validate = positive}},
803 required = all
804 }
805 },
806 validate_keys = non_empty,
807 wrap = global_config
808 }.
809
810 %% path: (host_config[].)acl
811 acl() ->
812 186 #section{
813 items = #{default => #list{items = acl_item()}},
814 wrap = host_config
815 }.
816
817 %% path: (host_config[].)acl.*[]
818 acl_item() ->
819 186 Match = #option{type = atom,
820 validate = {enum, [all, none, current_domain, any_hosted_domain]}},
821 186 Cond = #option{type = binary,
822 process = fun ?MODULE:process_acl_condition/1},
823 186 #section{
824 items = #{<<"match">> => Match,
825 <<"user">> => Cond,
826 <<"server">> => Cond,
827 <<"resource">> => Cond,
828 <<"user_regexp">> => Cond,
829 <<"server_regexp">> => Cond,
830 <<"resource_regexp">> => Cond,
831 <<"user_glob">> => Cond,
832 <<"server_glob">> => Cond,
833 <<"resource_glob">> => Cond
834 },
835 defaults = #{<<"match">> => current_domain}
836 }.
837
838 %% path: (host_config[].)access
839 access() ->
840 186 #section{
841 items = #{default => #list{items = access_rule_item()}},
842 wrap = host_config
843 }.
844
845 %% path: (host_config[].)access.*[]
846 access_rule_item() ->
847 186 #section{
848 items = #{<<"acl">> => #option{type = atom,
849 validate = non_empty},
850 <<"value">> => #option{type = int_or_atom}
851 },
852 required = all
853 }.
854
855 %% path: (host_config[].)s2s
856 s2s() ->
857 186 #section{
858 items = #{<<"default_policy">> => #option{type = atom,
859 validate = {enum, [allow, deny]}},
860 <<"host_policy">> => #list{items = s2s_host_policy(),
861 format_items = map},
862 <<"use_starttls">> => #option{type = atom,
863 validate = {enum, [false, optional, required,
864 required_trusted]}},
865 <<"certfile">> => #option{type = string,
866 validate = filename},
867 <<"shared">> => #option{type = binary,
868 validate = non_empty},
869 <<"address">> => #list{items = s2s_address(),
870 format_items = map},
871 <<"ciphers">> => #option{type = string},
872 <<"max_retry_delay">> => #option{type = integer,
873 validate = positive},
874 <<"outgoing">> => s2s_outgoing(),
875 <<"dns">> => s2s_dns()},
876 defaults = #{<<"default_policy">> => allow,
877 <<"use_starttls">> => false,
878 <<"ciphers">> => mongoose_tls:default_ciphers(),
879 <<"max_retry_delay">> => 300},
880 wrap = host_config
881 }.
882
883 %% path: (host_config[].)s2s.dns
884 s2s_dns() ->
885 186 #section{
886 items = #{<<"timeout">> => #option{type = integer,
887 validate = positive},
888 <<"retries">> => #option{type = integer,
889 validate = positive}},
890 include = always,
891 defaults = #{<<"timeout">> => 10,
892 <<"retries">> => 2}
893 }.
894
895 %% path: (host_config[].)s2s.outgoing
896 s2s_outgoing() ->
897 186 #section{
898 items = #{<<"port">> => #option{type = integer,
899 validate = port},
900 <<"ip_versions">> =>
901 #list{items = #option{type = integer,
902 validate = {enum, [4, 6]}},
903 validate = unique_non_empty},
904 <<"connection_timeout">> => #option{type = int_or_infinity,
905 validate = positive}
906 },
907 include = always,
908 defaults = #{<<"port">> => 5269,
909 <<"ip_versions">> => [4, 6],
910 <<"connection_timeout">> => 10000}
911 }.
912
913 %% path: (host_config[].)s2s.host_policy[]
914 s2s_host_policy() ->
915 186 #section{
916 items = #{<<"host">> => #option{type = binary,
917 validate = non_empty},
918 <<"policy">> => #option{type = atom,
919 validate = {enum, [allow, deny]}}
920 },
921 required = all,
922 process = fun ?MODULE:process_s2s_host_policy/1
923 }.
924
925 %% path: (host_config[].)s2s.address[]
926 s2s_address() ->
927 186 #section{
928 items = #{<<"host">> => #option{type = binary,
929 validate = non_empty},
930 <<"ip_address">> => #option{type = string,
931 validate = ip_address},
932 <<"port">> => #option{type = integer,
933 validate = port}
934 },
935 required = [<<"host">>, <<"ip_address">>],
936 process = fun ?MODULE:process_s2s_address/1
937 }.
938
939 %% Callbacks for 'process'
940
941 %% Check that all auth methods and modules enabled for any host type support dynamic domains
942 process_root(Items) ->
943 93 case proplists:lookup(host_types, Items) of
944 {_, [_|_] = HostTypes} ->
945 93 HTItems = lists:filter(fun(Item) -> is_host_type_item(Item, HostTypes) end, Items),
946 93 case {unsupported_auth_methods(HTItems), unsupported_modules(HTItems)} of
947 {[], []} ->
948 93 Items;
949 {Methods, Modules} ->
950
:-(
error(#{what => dynamic_domains_not_supported,
951 text => ("Dynamic modules not supported by the specified authentication "
952 "methods and/or extension modules"),
953 unsupported_auth_methods => Methods,
954 unsupported_modules => Modules})
955 end;
956 _ ->
957
:-(
Items
958 end.
959
960 unsupported_auth_methods(KVs) ->
961 93 [Method || Method <- extract_auth_methods(KVs),
962 229 not ejabberd_auth:does_method_support(Method, dynamic_domains)].
963
964 unsupported_modules(KVs) ->
965 93 [Module || Module <- extract_modules(KVs),
966 1016 not gen_mod:does_module_support(Module, dynamic_domains)].
967
968 extract_auth_methods(KVs) ->
969 93 lists:usort(lists:flatmap(fun({{auth, _}, Auth}) -> maps:get(methods, Auth);
970 601 (_) -> []
971 end, KVs)).
972
973 extract_modules(KVs) ->
974 93 lists:usort(lists:flatmap(fun({{modules, _}, Modules}) -> maps:keys(Modules);
975 601 (_) -> []
976 end, KVs)).
977
978 is_host_type_item({{_, HostType}, _}, HostTypes) ->
979 923 HostType =:= global orelse lists:member(HostType, HostTypes);
980 is_host_type_item(_, _) ->
981 1910 false.
982
983 process_host(Host) ->
984 372 Node = jid:nodeprep(Host),
985 372 true = Node =/= error,
986 372 Node.
987
988 process_general(General) ->
989 93 hosts_and_host_types_are_unique_and_non_empty(General),
990 93 General.
991
992 hosts_and_host_types_are_unique_and_non_empty(General) ->
993 93 AllHostTypes = get_all_hosts_and_host_types(General),
994 93 true = lists:sort(AllHostTypes) =:= lists:usort(AllHostTypes),
995 93 true = [] =/= AllHostTypes.
996
997 get_all_hosts_and_host_types(General) ->
998 93 lists:flatmap(fun({Key, Value}) when Key =:= hosts;
999 Key =:= host_types ->
1000 186 Value;
1001 (_) ->
1002 1259 []
1003 end, General).
1004
1005 %% User chooses just_tls or fast_tls, and this choice limits the allowed keys
1006 process_c2s_tls(M = #{module := Module}) ->
1007 186 AllowedItems = (tls([server, c2s], [Module]))#section.items,
1008 186 AllowedKeys = [binary_to_atom(Key) || Key <- maps:keys(AllowedItems)] ++ [module, mode],
1009 186 case maps:keys(M) -- AllowedKeys of
1010 186 [] -> M;
1011
:-(
UnexpectedKeys -> error(#{what => unexpected_tls_options,
1012 tls_module => Module,
1013 unexpected_keys => UnexpectedKeys})
1014 end.
1015
1016 process_c2s_just_tls(#{module := just_tls} = M) ->
1017 12 maps:merge(just_tls_c2s_defaults(), M);
1018 process_c2s_just_tls(M) ->
1019 174 M.
1020
1021 just_tls_c2s_defaults() ->
1022 12 #{crl_files => [],
1023 disconnect_on_failure => true}.
1024
1025 process_just_tls(M = #{module := fast_tls}) ->
1026 174 M;
1027 process_just_tls(M = #{cacertfile := _}) ->
1028 48 M;
1029 process_just_tls(M = #{verify_mode := none}) ->
1030 150 M;
1031 process_just_tls(_) ->
1032
:-(
error(#{what => missing_cacertfile,
1033 text => <<"You need to provide CA certificate (cacertfile) "
1034 "or disable peer verification (verify_mode)">>}).
1035
1036 process_fast_tls(M = #{module := just_tls}) ->
1037 12 M;
1038 process_fast_tls(#{verify_mode := selfsigned_peer}) ->
1039
:-(
error(#{what => invalid_tls_verify_mode,
1040 text => <<"fast_tls does not support self-signed certificate verification">>});
1041 process_fast_tls(M) ->
1042 267 maps:merge(fast_tls_defaults(), M).
1043
1044 fast_tls_defaults() ->
1045 267 #{ciphers => mongoose_tls:default_ciphers(),
1046 protocol_options => ["no_sslv2", "no_sslv3", "no_tlsv1", "no_tlsv1_1"]}.
1047
1048 process_listener([item, Type | _], Opts) ->
1049 1202 mongoose_listener_config:ensure_ip_options(Opts#{module => listener_module(Type),
1050 connection_type => connection_type(Type)}).
1051
1052 744 listener_module(<<"http">>) -> ejabberd_cowboy;
1053 186 listener_module(<<"c2s">>) -> mongoose_c2s_listener;
1054 93 listener_module(<<"s2s">>) -> ejabberd_s2s_in;
1055 179 listener_module(<<"service">>) -> ejabberd_service.
1056
1057 %% required for correct metrics reporting by mongoose_transport module
1058 93 connection_type(<<"s2s">>) -> s2s;
1059 179 connection_type(<<"service">>) -> component;
1060 930 connection_type(_) -> undefined.
1061
1062 process_sasl_external(V) when V =:= standard;
1063 V =:= common_name;
1064 V =:= auth_id ->
1065 21 V;
1066 process_sasl_external(M) ->
1067 3 mongoose_config_validator:validate(M, atom, module),
1068 3 {mod, M}.
1069
1070 process_sasl_mechanism(V) ->
1071 18 list_to_atom("cyrsasl_" ++ atom_to_list(V)).
1072
1073 process_auth(Opts = #{methods := Methods}) ->
1074
:-(
[check_auth_method(Method, Opts) || Method <- Methods],
1075
:-(
Opts;
1076 process_auth(Opts) ->
1077 322 MethodsFromSections = lists:filter(fun(K) -> maps:is_key(K, Opts) end, all_auth_methods()),
1078 322 Opts#{methods => MethodsFromSections}.
1079
1080 all_auth_methods() ->
1081 508 [anonymous, dummy, external, http, internal, jwt, ldap, pki, rdbms].
1082
1083 check_auth_method(Method, Opts) ->
1084
:-(
case maps:is_key(Method, Opts) of
1085
:-(
true -> ok;
1086
:-(
false -> error(#{what => missing_section_for_auth_method, auth_method => Method})
1087 end.
1088
1089 process_pool([Tag, Type|_], AllOpts = #{scope := ScopeIn, connection := Connection}) ->
1090 186 Scope = pool_scope(ScopeIn, maps:get(host, AllOpts, none)),
1091 186 Opts = maps:without([scope, host, connection], AllOpts),
1092 186 #{type => b2a(Type),
1093 scope => Scope,
1094 tag => b2a(Tag),
1095 opts => Opts,
1096 conn_opts => Connection}.
1097
1098 pool_scope(single_host, none) ->
1099
:-(
error(#{what => pool_single_host_not_specified,
1100 text => <<"\"host\" option is required if \"single_host\" is used.">>});
1101
:-(
pool_scope(single_host, Host) -> Host;
1102
:-(
pool_scope(host, none) -> host;
1103 186 pool_scope(global, none) -> global.
1104
1105
:-(
process_ldap_connection(ConnOpts = #{port := _}) -> ConnOpts;
1106
:-(
process_ldap_connection(ConnOpts = #{tls := _}) -> ConnOpts#{port => 636};
1107
:-(
process_ldap_connection(ConnOpts) -> ConnOpts#{port => 389}.
1108
1109 372 b2a(B) -> binary_to_atom(B, utf8).
1110
1111 8928 a2b(A) -> atom_to_binary(A, utf8).
1112
1113 wpool_strategy_values() ->
1114 1023 [best_worker, random_worker, next_worker, available_worker, next_available_worker].
1115
1116 process_acl_condition(Value) ->
1117
:-(
case jid:nodeprep(Value) of
1118
:-(
error -> error(#{what => incorrect_acl_condition_value,
1119 text => <<"Value could not be parsed as a JID node part">>});
1120
:-(
Node -> Node
1121 end.
1122
1123 process_s2s_host_policy(#{host := S2SHost, policy := Policy}) ->
1124
:-(
{S2SHost, Policy}.
1125
1126 process_s2s_address(M) ->
1127 93 maps:take(host, M).
1128
1129 process_domain_cert(#{domain := Domain, certfile := Certfile}) ->
1130
:-(
{Domain, Certfile}.
1131
1132
:-(
process_infinity_as_zero(infinity) -> 0;
1133 279 process_infinity_as_zero(Num) -> Num.
Line Hits Source