./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 42 General = general(),
84 42 Listen = listen(),
85 42 Pools = outgoing_pools(),
86 42 Auth = auth(),
87 42 Modules = modules(),
88 42 S2S = s2s(),
89 42 #section{
90 items = #{<<"general">> => General#section{required = [<<"default_server_domain">>],
91 process = fun ?MODULE:process_general/1,
92 defaults = general_defaults()},
93 <<"listen">> => Listen#section{include = always},
94 <<"auth">> => Auth#section{include = always},
95 <<"outgoing_pools">> => Pools#section{include = always},
96 <<"internal_databases">> => internal_databases(),
97 <<"services">> => services(),
98 <<"modules">> => Modules#section{include = always},
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 42 #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 84 #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 42 #{<<"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 84 #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 42 ListenerTypes = [<<"c2s">>, <<"s2s">>, <<"service">>, <<"http">>],
241 42 #section{
242 168 items = maps:from_list([{Listener, #list{items = listener(Listener), wrap = none}}
243 42 || Listener <- ListenerTypes]),
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 168 mongoose_config_utils:merge_sections(listener_common(), listener_extra(Type)).
252
253 listener_common() ->
254 168 #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 42 #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 126 mongoose_config_utils:merge_sections(xmpp_listener_common(), xmpp_listener_extra(Type)).
276
277 xmpp_listener_common() ->
278 126 #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 42 #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 42 TLSSection = tls([server], [fast_tls]),
321 42 #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 42 #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 42 #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 42 #section{
364 items = #{<<"compress">> => #option{type = boolean}},
365 defaults = #{<<"compress">> => false},
366 include = always
367 }.
368
369 %% path: (host_config[].)auth
370 auth() ->
371 84 Items = maps:from_list([{a2b(Method), ejabberd_auth:config_spec(Method)} ||
372 84 Method <- all_auth_methods()]),
373 84 #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 84 #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 42 Items = #{<<"cets">> => internal_database_cets(),
415 <<"mnesia">> => internal_database_mnesia()},
416 42 #section{items = Items,
417 format_items = map,
418 wrap = global_config}.
419
420 default_internal_databases() ->
421 42 #{mnesia => #{}}.
422
423 %% path: internal_databases.cets
424 internal_database_cets() ->
425 42 #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 42 #section{}.
439
440 %% path: outgoing_pools
441 outgoing_pools() ->
442 42 PoolTypes = [<<"cassandra">>, <<"elastic">>, <<"http">>, <<"ldap">>,
443 <<"rabbit">>, <<"rdbms">>, <<"redis">>],
444 42 Items = maps:from_list([{PoolType, #list{items = outgoing_pool(PoolType), wrap = none}}
445 42 || PoolType <- PoolTypes]),
446 42 #section{
447 items = Items,
448 wrap = global_config,
449 format_items = list
450 }.
451
452 %% path: outgoing_pools.*[]
453 outgoing_pool(Type) ->
454 294 ExtraDefaults = extra_wpool_defaults(Type),
455 294 Pool = mongoose_config_utils:merge_sections(wpool(ExtraDefaults), outgoing_pool_extra(Type)),
456 294 Pool#section{wrap = item}.
457
458 extra_wpool_defaults(<<"cassandra">>) ->
459 42 #{<<"workers">> => 20};
460 extra_wpool_defaults(<<"rdbms">>) ->
461 42 #{<<"call_timeout">> => 60000};
462 extra_wpool_defaults(_) ->
463 210 #{}.
464
465 wpool(ExtraDefaults) ->
466 462 #section{items = #{<<"workers">> => #option{type = integer,
467 validate = positive},
468 <<"strategy">> => #option{type = atom,
469 validate = {enum, wpool_strategy_values()}},
470 <<"call_timeout">> => #option{type = integer,
471 validate = positive}
472 },
473 defaults = maps:merge(#{<<"workers">> => 10,
474 <<"strategy">> => best_worker,
475 <<"call_timeout">> => 5000}, ExtraDefaults)}.
476
477 outgoing_pool_extra(Type) ->
478 294 Scopes = [global, host_type, single_host_type, host, single_host], %% TODO deprecated
479 294 #section{items = #{<<"tag">> => #option{type = atom,
480 validate = non_empty},
481 <<"scope">> => #option{type = atom,
482 validate = {enum, Scopes}},
483 <<"host_type">> => #option{type = binary,
484 validate = non_empty},
485 <<"host">> => #option{type = binary, %% TODO deprecated
486 validate = non_empty},
487 <<"connection">> => outgoing_pool_connection(Type)
488 },
489 process = fun ?MODULE:process_pool/2,
490 defaults = #{<<"tag">> => default,
491 <<"scope">> => global}
492 }.
493
494 %% path: outgoing_pools.*[].connection
495 outgoing_pool_connection(<<"cassandra">>) ->
496 42 #section{
497 items = #{<<"servers">> => #list{items = cassandra_server(),
498 validate = unique_non_empty},
499 <<"keyspace">> => #option{type = atom,
500 validate = non_empty},
501 <<"auth">> => #section{items = #{<<"plain">> => cassandra_auth_plain()},
502 required = all},
503 <<"tls">> => tls([client], [just_tls])
504 },
505 include = always,
506 defaults = #{<<"servers">> => [#{host => "localhost", port => 9042}],
507 <<"keyspace">> => mongooseim}
508 };
509 outgoing_pool_connection(<<"elastic">>) ->
510 42 #section{
511 items = #{<<"host">> => #option{type = binary,
512 validate = non_empty},
513 <<"port">> => #option{type = integer,
514 validate = port}
515 },
516 include = always,
517 defaults = #{<<"host">> => <<"localhost">>,
518 <<"port">> => 9200}
519 };
520 outgoing_pool_connection(<<"http">>) ->
521 42 #section{
522 items = #{<<"host">> => #option{type = string,
523 validate = non_empty},
524 <<"path_prefix">> => #option{type = binary,
525 validate = non_empty},
526 <<"request_timeout">> => #option{type = integer,
527 validate = non_negative},
528 <<"tls">> => tls([client], [just_tls])
529 },
530 include = always,
531 required = [<<"host">>],
532 defaults = #{<<"path_prefix">> => <<"/">>,
533 <<"request_timeout">> => 2000}
534 };
535 outgoing_pool_connection(<<"ldap">>) ->
536 42 #section{
537 items = #{<<"servers">> => #list{items = #option{type = string},
538 validate = unique_non_empty},
539 <<"port">> => #option{type = integer,
540 validate = port},
541 <<"root_dn">> => #option{type = binary},
542 <<"password">> => #option{type = binary},
543 <<"connect_interval">> => #option{type = integer,
544 validate = positive},
545 <<"tls">> => tls([client], [just_tls])
546 },
547 include = always,
548 defaults = #{<<"servers">> => ["localhost"],
549 <<"root_dn">> => <<>>,
550 <<"password">> => <<>>,
551 <<"connect_interval">> => 10000},
552 process = fun ?MODULE:process_ldap_connection/1
553 };
554 outgoing_pool_connection(<<"rabbit">>) ->
555 42 #section{
556 items = #{<<"host">> => #option{type = string,
557 validate = non_empty},
558 <<"port">> => #option{type = integer,
559 validate = port},
560 <<"username">> => #option{type = binary,
561 validate = non_empty},
562 <<"password">> => #option{type = binary,
563 validate = non_empty},
564 <<"confirms_enabled">> => #option{type = boolean},
565 <<"max_worker_queue_len">> => #option{type = int_or_infinity,
566 validate = non_negative}
567 },
568 include = always,
569 defaults = #{<<"host">> => "localhost",
570 <<"port">> => 5672,
571 <<"username">> => <<"guest">>,
572 <<"password">> => <<"guest">>,
573 <<"confirms_enabled">> => false,
574 <<"max_worker_queue_len">> => 1000}
575 };
576 outgoing_pool_connection(<<"rdbms">>) ->
577 42 #section{
578 items = #{<<"driver">> => #option{type = atom,
579 validate = {enum, [odbc, pgsql, mysql]}},
580 <<"keepalive_interval">> => #option{type = integer,
581 validate = positive},
582 <<"query_timeout">> => #option{type = integer,
583 validate = non_negative},
584 <<"max_start_interval">> => #option{type = integer,
585 validate = positive},
586
587 % odbc
588 <<"settings">> => #option{type = string},
589
590 % mysql, pgsql
591 <<"host">> => #option{type = string,
592 validate = non_empty},
593 <<"database">> => #option{type = string,
594 validate = non_empty},
595 <<"username">> => #option{type = string,
596 validate = non_empty},
597 <<"password">> => #option{type = string,
598 validate = non_empty},
599 <<"port">> => #option{type = integer,
600 validate = port},
601 <<"tls">> => sql_tls()
602 },
603 required = [<<"driver">>],
604 defaults = #{<<"query_timeout">> => 5000,
605 <<"max_start_interval">> => 30},
606 process = fun mongoose_rdbms:process_options/1
607 };
608 outgoing_pool_connection(<<"redis">>) ->
609 42 #section{
610 items = #{<<"host">> => #option{type = string,
611 validate = non_empty},
612 <<"port">> => #option{type = integer,
613 validate = port},
614 <<"database">> => #option{type = integer,
615 validate = non_negative},
616 <<"password">> => #option{type = string}
617 },
618 include = always,
619 defaults = #{<<"host">> => "127.0.0.1",
620 <<"port">> => 6379,
621 <<"database">> => 0,
622 <<"password">> => ""}
623 }.
624
625 cassandra_server() ->
626 42 #section{
627 items = #{<<"host">> => #option{type = string,
628 validate = non_empty},
629 <<"port">> => #option{type = integer,
630 validate = port}},
631 required = [<<"host">>],
632 defaults = #{<<"port">> => 9042}
633 }.
634
635 %% path: outgoing_pools.cassandra.connection.auth.plain
636 cassandra_auth_plain() ->
637 42 #section{
638 items = #{<<"username">> => #option{type = binary},
639 <<"password">> => #option{type = binary}},
640 required = all
641 }.
642
643 %% path: outgoing_pools.rdbms.connection.tls
644 sql_tls() ->
645 42 mongoose_config_utils:merge_sections(tls([client], [just_tls]), sql_tls_extra()).
646
647 sql_tls_extra() ->
648 42 #section{items = #{<<"required">> => #option{type = boolean}}}.
649
650 %% TLS options
651
652 tls(Entities, Modules) when is_list(Entities), is_list(Modules) ->
653 462 Sections = [tls(Entity, Module) || Entity <- [common | Entities],
654 1134 Module <- [common | Modules]],
655 462 lists:foldl(fun mongoose_config_utils:merge_sections/2, hd(Sections), tl(Sections));
656 tls(common, common) ->
657 462 #section{items = #{<<"verify_mode">> => #option{type = atom,
658 validate = {enum, [peer, selfsigned_peer, none]}},
659 <<"certfile">> => #option{type = string,
660 validate = filename},
661 <<"cacertfile">> => #option{type = string,
662 validate = filename},
663 <<"ciphers">> => #option{type = string}
664 },
665 defaults = #{<<"verify_mode">> => peer}};
666 tls(common, fast_tls) ->
667 239 #section{items = #{<<"protocol_options">> => #list{items = #option{type = string,
668 validate = non_empty}}},
669 process = fun ?MODULE:process_fast_tls/1};
670 tls(common, just_tls) ->
671 265 #section{items = #{<<"keyfile">> => #option{type = string,
672 validate = filename},
673 <<"password">> => #option{type = string},
674 <<"versions">> => #list{items = #option{type = atom}}},
675 process = fun ?MODULE:process_just_tls/1};
676 tls(server, common) ->
677 294 #section{items = #{<<"dhfile">> => #option{type = string,
678 validate = filename}}};
679 tls(server, _) ->
680 336 #section{};
681 tls(client, common) ->
682 252 #section{};
683 tls(client, fast_tls) ->
684 84 #section{};
685 tls(client, just_tls) ->
686 168 #section{items = #{<<"server_name_indication">> => server_name_indication()}};
687 tls(c2s, common) ->
688 126 #section{items = #{<<"module">> => #option{type = atom,
689 validate = {enum, [fast_tls, just_tls]}},
690 <<"mode">> => #option{type = atom,
691 validate = {enum, [tls, starttls, starttls_required]}}},
692 defaults = #{<<"module">> => fast_tls,
693 <<"mode">> => starttls},
694 process = fun ?MODULE:process_c2s_tls/1};
695 tls(c2s, just_tls) ->
696 55 #section{items = #{<<"disconnect_on_failure">> => #option{type = boolean},
697 <<"crl_files">> => #list{items = #option{type = string,
698 validate = filename}}},
699 process = fun ?MODULE:process_c2s_just_tls/1};
700 tls(c2s, fast_tls) ->
701 113 #section{}.
702
703 server_name_indication() ->
704 168 #section{items = #{<<"enabled">> => #option{type = boolean},
705 <<"host">> => #option{type = string,
706 validate = non_empty},
707 <<"protocol">> => #option{type = atom,
708 validate = {enum, [default, https]}}
709 },
710 defaults = #{<<"enabled">> => true,
711 <<"protocol">> => default},
712 include = always}.
713
714 %% path: (host_config[].)services
715 services() ->
716 42 Services = [{a2b(Service), mongoose_service:config_spec(Service)}
717 42 || Service <- configurable_services()],
718 42 #section{
719 items = maps:from_list(Services),
720 wrap = global_config,
721 include = always
722 }.
723
724 configurable_services() ->
725 42 [service_mongoose_system_metrics,
726 service_domain_db].
727
728 %% path: (host_config[].)modules
729 modules() ->
730 84 Modules = [{a2b(Module), gen_mod:config_spec(Module)}
731 84 || Module <- configurable_modules()],
732 84 Items = maps:from_list(Modules),
733 84 #section{
734 items = Items#{default => #section{}},
735 validate_keys = module,
736 wrap = host_config
737 }.
738
739 configurable_modules() ->
740 84 [mod_adhoc,
741 mod_auth_token,
742 mod_blocking,
743 mod_bosh,
744 mod_cache_users,
745 mod_caps,
746 mod_carboncopy,
747 mod_csi,
748 mod_disco,
749 mod_event_pusher,
750 mod_extdisco,
751 mod_global_distrib,
752 mod_http_upload,
753 mod_inbox,
754 mod_jingle_sip,
755 mod_keystore,
756 mod_last,
757 mod_mam,
758 mod_muc,
759 mod_muc_light,
760 mod_muc_log,
761 mod_offline,
762 mod_offline_chatmarkers,
763 mod_ping,
764 mod_privacy,
765 mod_private,
766 mod_pubsub,
767 mod_push_service_mongoosepush,
768 mod_register,
769 mod_roster,
770 mod_shared_roster_ldap,
771 mod_smart_markers,
772 mod_sic,
773 mod_stream_management,
774 mod_time,
775 mod_vcard,
776 mod_version,
777 mod_domain_isolation].
778
779 %% path: (host_config[].)modules.*.iqdisc
780 iqdisc() ->
781 1596 #section{
782 items = #{<<"type">> => #option{type = atom,
783 validate = {enum, [no_queue, one_queue, parallel, queues]}},
784 <<"workers">> => #option{type = integer,
785 validate = positive}},
786 required = [<<"type">>],
787 process = fun ?MODULE:process_iqdisc/1
788 }.
789
790
:-(
process_iqdisc(#{type := Type, workers := N}) -> {queues = Type, N};
791
:-(
process_iqdisc(#{type := Type}) -> Type.
792
793 %% path: shaper
794 shaper() ->
795 42 #section{
796 items = #{default =>
797 #section{
798 items = #{<<"max_rate">> => #option{type = integer,
799 validate = positive}},
800 required = all
801 }
802 },
803 validate_keys = non_empty,
804 wrap = global_config
805 }.
806
807 %% path: (host_config[].)acl
808 acl() ->
809 84 #section{
810 items = #{default => #list{items = acl_item()}},
811 wrap = host_config
812 }.
813
814 %% path: (host_config[].)acl.*[]
815 acl_item() ->
816 84 Match = #option{type = atom,
817 validate = {enum, [all, none, current_domain, any_hosted_domain]}},
818 84 Cond = #option{type = binary,
819 process = fun ?MODULE:process_acl_condition/1},
820 84 #section{
821 items = #{<<"match">> => Match,
822 <<"user">> => Cond,
823 <<"server">> => Cond,
824 <<"resource">> => Cond,
825 <<"user_regexp">> => Cond,
826 <<"server_regexp">> => Cond,
827 <<"resource_regexp">> => Cond,
828 <<"user_glob">> => Cond,
829 <<"server_glob">> => Cond,
830 <<"resource_glob">> => Cond
831 },
832 defaults = #{<<"match">> => current_domain}
833 }.
834
835 %% path: (host_config[].)access
836 access() ->
837 84 #section{
838 items = #{default => #list{items = access_rule_item()}},
839 wrap = host_config
840 }.
841
842 %% path: (host_config[].)access.*[]
843 access_rule_item() ->
844 84 #section{
845 items = #{<<"acl">> => #option{type = atom,
846 validate = non_empty},
847 <<"value">> => #option{type = int_or_atom}
848 },
849 required = all
850 }.
851
852 %% path: (host_config[].)s2s
853 s2s() ->
854 84 #section{
855 items = #{<<"default_policy">> => #option{type = atom,
856 validate = {enum, [allow, deny]}},
857 <<"host_policy">> => #list{items = s2s_host_policy(),
858 format_items = map},
859 <<"use_starttls">> => #option{type = atom,
860 validate = {enum, [false, optional, required,
861 required_trusted]}},
862 <<"certfile">> => #option{type = string,
863 validate = filename},
864 <<"shared">> => #option{type = binary,
865 validate = non_empty},
866 <<"address">> => #list{items = s2s_address(),
867 format_items = map},
868 <<"ciphers">> => #option{type = string},
869 <<"max_retry_delay">> => #option{type = integer,
870 validate = positive},
871 <<"outgoing">> => s2s_outgoing(),
872 <<"dns">> => s2s_dns()},
873 defaults = #{<<"default_policy">> => allow,
874 <<"use_starttls">> => false,
875 <<"ciphers">> => mongoose_tls:default_ciphers(),
876 <<"max_retry_delay">> => 300},
877 wrap = host_config
878 }.
879
880 %% path: (host_config[].)s2s.dns
881 s2s_dns() ->
882 84 #section{
883 items = #{<<"timeout">> => #option{type = integer,
884 validate = positive},
885 <<"retries">> => #option{type = integer,
886 validate = positive}},
887 include = always,
888 defaults = #{<<"timeout">> => 10,
889 <<"retries">> => 2}
890 }.
891
892 %% path: (host_config[].)s2s.outgoing
893 s2s_outgoing() ->
894 84 #section{
895 items = #{<<"port">> => #option{type = integer,
896 validate = port},
897 <<"ip_versions">> =>
898 #list{items = #option{type = integer,
899 validate = {enum, [4, 6]}},
900 validate = unique_non_empty},
901 <<"connection_timeout">> => #option{type = int_or_infinity,
902 validate = positive}
903 },
904 include = always,
905 defaults = #{<<"port">> => 5269,
906 <<"ip_versions">> => [4, 6],
907 <<"connection_timeout">> => 10000}
908 }.
909
910 %% path: (host_config[].)s2s.host_policy[]
911 s2s_host_policy() ->
912 84 #section{
913 items = #{<<"host">> => #option{type = binary,
914 validate = non_empty},
915 <<"policy">> => #option{type = atom,
916 validate = {enum, [allow, deny]}}
917 },
918 required = all,
919 process = fun ?MODULE:process_s2s_host_policy/1
920 }.
921
922 %% path: (host_config[].)s2s.address[]
923 s2s_address() ->
924 84 #section{
925 items = #{<<"host">> => #option{type = binary,
926 validate = non_empty},
927 <<"ip_address">> => #option{type = string,
928 validate = ip_address},
929 <<"port">> => #option{type = integer,
930 validate = port}
931 },
932 required = [<<"host">>, <<"ip_address">>],
933 process = fun ?MODULE:process_s2s_address/1
934 }.
935
936 %% Callbacks for 'process'
937
938 %% Check that all auth methods and modules enabled for any host type support dynamic domains
939 process_root(Items) ->
940 42 case proplists:lookup(host_types, Items) of
941 {_, [_|_] = HostTypes} ->
942 41 HTItems = lists:filter(fun(Item) -> is_host_type_item(Item, HostTypes) end, Items),
943 41 case {unsupported_auth_methods(HTItems), unsupported_modules(HTItems)} of
944 {[], []} ->
945 41 Items;
946 {Methods, Modules} ->
947
:-(
error(#{what => dynamic_domains_not_supported,
948 text => ("Dynamic modules not supported by the specified authentication "
949 "methods and/or extension modules"),
950 unsupported_auth_methods => Methods,
951 unsupported_modules => Modules})
952 end;
953 _ ->
954 1 Items
955 end.
956
957 unsupported_auth_methods(KVs) ->
958 41 [Method || Method <- extract_auth_methods(KVs),
959 107 not ejabberd_auth:does_method_support(Method, dynamic_domains)].
960
961 unsupported_modules(KVs) ->
962 41 [Module || Module <- extract_modules(KVs),
963 460 not gen_mod:does_module_support(Module, dynamic_domains)].
964
965 extract_auth_methods(KVs) ->
966 41 lists:usort(lists:flatmap(fun({{auth, _}, Auth}) -> maps:get(methods, Auth);
967 271 (_) -> []
968 end, KVs)).
969
970 extract_modules(KVs) ->
971 41 lists:usort(lists:flatmap(fun({{modules, _}, Modules}) -> maps:keys(Modules);
972 271 (_) -> []
973 end, KVs)).
974
975 is_host_type_item({{_, HostType}, _}, HostTypes) ->
976 419 HostType =:= global orelse lists:member(HostType, HostTypes);
977 is_host_type_item(_, _) ->
978 795 false.
979
980 process_host(Host) ->
981 168 Node = jid:nodeprep(Host),
982 168 true = Node =/= error,
983 168 Node.
984
985 process_general(General) ->
986 42 hosts_and_host_types_are_unique_and_non_empty(General),
987 42 General.
988
989 hosts_and_host_types_are_unique_and_non_empty(General) ->
990 42 AllHostTypes = get_all_hosts_and_host_types(General),
991 42 true = lists:sort(AllHostTypes) =:= lists:usort(AllHostTypes),
992 42 true = [] =/= AllHostTypes.
993
994 get_all_hosts_and_host_types(General) ->
995 42 lists:flatmap(fun({Key, Value}) when Key =:= hosts;
996 Key =:= host_types ->
997 84 Value;
998 (_) ->
999 562 []
1000 end, General).
1001
1002 %% User chooses just_tls or fast_tls, and this choice limits the allowed keys
1003 process_c2s_tls(M = #{module := Module}) ->
1004 84 AllowedItems = (tls([server, c2s], [Module]))#section.items,
1005 84 AllowedKeys = [binary_to_atom(Key) || Key <- maps:keys(AllowedItems)] ++ [module, mode],
1006 84 case maps:keys(M) -- AllowedKeys of
1007 84 [] -> M;
1008
:-(
UnexpectedKeys -> error(#{what => unexpected_tls_options,
1009 tls_module => Module,
1010 unexpected_keys => UnexpectedKeys})
1011 end.
1012
1013 process_c2s_just_tls(#{module := just_tls} = M) ->
1014 13 maps:merge(just_tls_c2s_defaults(), M);
1015 process_c2s_just_tls(M) ->
1016 71 M.
1017
1018 just_tls_c2s_defaults() ->
1019 13 #{crl_files => [],
1020 disconnect_on_failure => true}.
1021
1022 process_just_tls(M = #{module := fast_tls}) ->
1023 71 M;
1024 process_just_tls(M = #{cacertfile := _}) ->
1025 91 M;
1026 process_just_tls(M = #{verify_mode := none}) ->
1027 48 M;
1028 process_just_tls(_) ->
1029
:-(
error(#{what => missing_cacertfile,
1030 text => <<"You need to provide CA certificate (cacertfile) "
1031 "or disable peer verification (verify_mode)">>}).
1032
1033 process_fast_tls(M = #{module := just_tls}) ->
1034 13 M;
1035 process_fast_tls(#{verify_mode := selfsigned_peer}) ->
1036
:-(
error(#{what => invalid_tls_verify_mode,
1037 text => <<"fast_tls does not support self-signed certificate verification">>});
1038 process_fast_tls(M) ->
1039 113 maps:merge(fast_tls_defaults(), M).
1040
1041 fast_tls_defaults() ->
1042 113 #{ciphers => mongoose_tls:default_ciphers(),
1043 protocol_options => ["no_sslv2", "no_sslv3", "no_tlsv1", "no_tlsv1_1"]}.
1044
1045 process_listener([item, Type | _], Opts) ->
1046 511 mongoose_listener_config:ensure_ip_options(Opts#{module => listener_module(Type),
1047 connection_type => connection_type(Type)}).
1048
1049 294 listener_module(<<"http">>) -> ejabberd_cowboy;
1050 84 listener_module(<<"c2s">>) -> mongoose_c2s_listener;
1051 42 listener_module(<<"s2s">>) -> ejabberd_s2s_in;
1052 91 listener_module(<<"service">>) -> ejabberd_service.
1053
1054 %% required for correct metrics reporting by mongoose_transport module
1055 42 connection_type(<<"s2s">>) -> s2s;
1056 91 connection_type(<<"service">>) -> component;
1057 378 connection_type(_) -> undefined.
1058
1059 process_sasl_external(V) when V =:= standard;
1060 V =:= common_name;
1061 V =:= auth_id ->
1062 21 V;
1063 process_sasl_external(M) ->
1064 3 mongoose_config_validator:validate(M, atom, module),
1065 3 {mod, M}.
1066
1067 process_sasl_mechanism(V) ->
1068 18 list_to_atom("cyrsasl_" ++ atom_to_list(V)).
1069
1070 process_auth(Opts = #{methods := Methods}) ->
1071
:-(
[check_auth_method(Method, Opts) || Method <- Methods],
1072
:-(
Opts;
1073 process_auth(Opts) ->
1074 150 MethodsFromSections = lists:filter(fun(K) -> maps:is_key(K, Opts) end, all_auth_methods()),
1075 150 Opts#{methods => MethodsFromSections}.
1076
1077 all_auth_methods() ->
1078 234 [anonymous, dummy, external, http, internal, jwt, ldap, pki, rdbms].
1079
1080 check_auth_method(Method, Opts) ->
1081
:-(
case maps:is_key(Method, Opts) of
1082
:-(
true -> ok;
1083
:-(
false -> error(#{what => missing_section_for_auth_method, auth_method => Method})
1084 end.
1085
1086 process_pool([item, Type | _], AllOpts = #{tag := Tag, scope := ScopeIn, connection := Connection}) ->
1087 126 Scope = pool_scope(ScopeIn, maps:get(host_type, AllOpts, maps:get(host, AllOpts, none))),
1088 126 Opts = maps:without([tag, scope, host, host_type, connection], AllOpts),
1089 126 #{type => b2a(Type),
1090 scope => Scope,
1091 tag => Tag,
1092 opts => Opts,
1093 conn_opts => Connection}.
1094
1095 pool_scope(single_host_type, none) ->
1096
:-(
error(#{what => pool_single_host_type_not_specified,
1097 text => <<"\"host_type\" option is required if \"single_host_type\" is used.">>});
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_type, HostType) -> HostType;
1102
:-(
pool_scope(single_host, Host) -> Host;
1103
:-(
pool_scope(host, none) -> host_type;
1104
:-(
pool_scope(host_type, none) -> host_type;
1105 126 pool_scope(global, none) -> global.
1106
1107
:-(
process_ldap_connection(ConnOpts = #{port := _}) -> ConnOpts;
1108
:-(
process_ldap_connection(ConnOpts = #{tls := _}) -> ConnOpts#{port => 636};
1109
:-(
process_ldap_connection(ConnOpts) -> ConnOpts#{port => 389}.
1110
1111 126 b2a(B) -> binary_to_atom(B, utf8).
1112
1113 4032 a2b(A) -> atom_to_binary(A, utf8).
1114
1115 wpool_strategy_values() ->
1116 462 [best_worker, random_worker, next_worker, available_worker, next_available_worker].
1117
1118 process_acl_condition(Value) ->
1119
:-(
case jid:nodeprep(Value) of
1120
:-(
error -> error(#{what => incorrect_acl_condition_value,
1121 text => <<"Value could not be parsed as a JID node part">>});
1122
:-(
Node -> Node
1123 end.
1124
1125 process_s2s_host_policy(#{host := S2SHost, policy := Policy}) ->
1126
:-(
{S2SHost, Policy}.
1127
1128 process_s2s_address(M) ->
1129 42 maps:take(host, M).
1130
1131 process_domain_cert(#{domain := Domain, certfile := Certfile}) ->
1132
:-(
{Domain, Certfile}.
1133
1134
:-(
process_infinity_as_zero(infinity) -> 0;
1135 126 process_infinity_as_zero(Num) -> Num.
Line Hits Source