1 |
|
%% Main module other parts of MongooseIM should use to access the domain |
2 |
|
%% management. |
3 |
|
-module(mongoose_domain_api). |
4 |
|
|
5 |
|
-export([init/0, |
6 |
|
stop/0, |
7 |
|
get_host_type/1]). |
8 |
|
|
9 |
|
%% domain API |
10 |
|
-export([insert_domain/2, |
11 |
|
delete_domain/2, |
12 |
|
disable_domain/1, |
13 |
|
enable_domain/1, |
14 |
|
get_domain_host_type/1, |
15 |
|
get_all_static/0, |
16 |
|
get_domains_by_host_type/1]). |
17 |
|
|
18 |
|
%% domain admin API |
19 |
|
-export([check_domain_password/2, |
20 |
|
set_domain_password/2, |
21 |
|
delete_domain_password/1]). |
22 |
|
|
23 |
|
%% subdomain API |
24 |
|
-export([register_subdomain/3, |
25 |
|
unregister_subdomain/2, |
26 |
|
get_subdomain_host_type/1, |
27 |
|
get_subdomain_info/1, |
28 |
|
get_all_subdomains_for_domain/1]). |
29 |
|
|
30 |
|
%% For testing |
31 |
|
-export([get_all_dynamic/0]). |
32 |
|
|
33 |
|
-ignore_xref([get_all_static/0]). |
34 |
|
-ignore_xref([get_all_dynamic/0]). |
35 |
|
-ignore_xref([stop/0]). |
36 |
|
|
37 |
|
-type domain() :: jid:lserver(). |
38 |
|
-type host_type() :: mongooseim:host_type(). |
39 |
|
-type subdomain_pattern() :: mongoose_subdomain_utils:subdomain_pattern(). |
40 |
|
|
41 |
|
|
42 |
|
-spec init() -> ok | {error, term()}. |
43 |
|
init() -> |
44 |
76 |
mongoose_domain_core:start(), |
45 |
76 |
mongoose_subdomain_core:start(), |
46 |
76 |
mongoose_lazy_routing:start(). |
47 |
|
|
48 |
|
%% Stops gen_servers, that are started from init/0 |
49 |
|
%% Does not fail, even if servers are already stopped |
50 |
|
-spec stop() -> ok. |
51 |
|
stop() -> |
52 |
:-( |
catch mongoose_domain_core:stop(), |
53 |
:-( |
catch mongoose_subdomain_core:stop(), |
54 |
:-( |
catch mongoose_lazy_routing:stop(), |
55 |
:-( |
ok. |
56 |
|
|
57 |
|
%% Domain should be nameprepped using `jid:nameprep'. |
58 |
|
-spec insert_domain(domain(), host_type()) -> |
59 |
|
ok | {error, duplicate} | {error, static} | {error, {db_error, term()}} |
60 |
|
| {error, service_disabled} | {error, unknown_host_type}. |
61 |
|
insert_domain(Domain, HostType) -> |
62 |
99 |
case check_domain(Domain, HostType) of |
63 |
|
ok -> |
64 |
86 |
check_db(mongoose_domain_sql:insert_domain(Domain, HostType)); |
65 |
|
Other -> |
66 |
13 |
Other |
67 |
|
end. |
68 |
|
|
69 |
|
%% Returns ok, if domain not found. |
70 |
|
%% Domain should be nameprepped using `jid:nameprep'. |
71 |
|
-spec delete_domain(domain(), host_type()) -> |
72 |
|
ok | {error, static} | {error, {db_error, term()}} |
73 |
|
| {error, service_disabled} | {error, wrong_host_type} | {error, unknown_host_type}. |
74 |
|
delete_domain(Domain, HostType) -> |
75 |
39 |
case check_domain(Domain, HostType) of |
76 |
|
ok -> |
77 |
29 |
Res = check_db(mongoose_domain_sql:delete_domain(Domain, HostType)), |
78 |
29 |
case Res of |
79 |
|
ok -> |
80 |
21 |
delete_domain_password(Domain), |
81 |
21 |
mongoose_hooks:remove_domain(HostType, Domain); |
82 |
|
_ -> |
83 |
8 |
ok |
84 |
|
end, |
85 |
29 |
Res; |
86 |
|
Other -> |
87 |
10 |
Other |
88 |
|
end. |
89 |
|
|
90 |
|
-spec disable_domain(domain()) -> |
91 |
|
ok | {error, not_found} | {error, static} | {error, service_disabled} |
92 |
|
| {error, {db_error, term()}}. |
93 |
|
disable_domain(Domain) -> |
94 |
21 |
case mongoose_domain_core:is_static(Domain) of |
95 |
|
true -> |
96 |
2 |
{error, static}; |
97 |
|
false -> |
98 |
19 |
case service_domain_db:enabled() of |
99 |
|
true -> |
100 |
18 |
check_db(mongoose_domain_sql:disable_domain(Domain)); |
101 |
|
false -> |
102 |
1 |
{error, service_disabled} |
103 |
|
end |
104 |
|
end. |
105 |
|
|
106 |
|
-spec enable_domain(domain()) -> |
107 |
|
ok | {error, not_found} | {error, static} | {error, service_disabled} |
108 |
|
| {error, {db_error, term()}}. |
109 |
|
enable_domain(Domain) -> |
110 |
21 |
case mongoose_domain_core:is_static(Domain) of |
111 |
|
true -> |
112 |
4 |
{error, static}; |
113 |
|
false -> |
114 |
17 |
case service_domain_db:enabled() of |
115 |
|
true -> |
116 |
14 |
check_db(mongoose_domain_sql:enable_domain(Domain)); |
117 |
|
false -> |
118 |
3 |
{error, service_disabled} |
119 |
|
end |
120 |
|
end. |
121 |
|
|
122 |
|
check_db(ok) -> |
123 |
|
%% Speedup the next check. %% It's async. |
124 |
117 |
service_domain_db:force_check_for_updates(), |
125 |
117 |
ok; |
126 |
|
check_db(Result) -> |
127 |
30 |
Result. |
128 |
|
|
129 |
|
%% Domain should be nameprepped using `jid:nameprep' |
130 |
|
-spec get_host_type(domain()) -> |
131 |
|
{ok, host_type()} | {error, not_found}. |
132 |
|
get_host_type(Domain) -> |
133 |
35540 |
case get_domain_host_type(Domain) of |
134 |
32573 |
{ok, HostType} -> {ok, HostType}; |
135 |
|
{error, not_found} -> |
136 |
2967 |
get_subdomain_host_type(Domain) |
137 |
|
end. |
138 |
|
|
139 |
|
%% Domain should be nameprepped using `jid:nameprep' |
140 |
|
-spec get_domain_host_type(domain()) -> |
141 |
|
{ok, host_type()} | {error, not_found}. |
142 |
|
get_domain_host_type(Domain) -> |
143 |
71793 |
mongoose_domain_core:get_host_type(Domain). |
144 |
|
|
145 |
|
%% Subdomain should be nameprepped using `jid:nameprep' |
146 |
|
-spec get_subdomain_host_type(domain()) -> |
147 |
|
{ok, host_type()} | {error, not_found}. |
148 |
|
get_subdomain_host_type(Subdomain) -> |
149 |
5791 |
mongoose_subdomain_core:get_host_type(Subdomain). |
150 |
|
|
151 |
|
%% Subdomain should be nameprepped using `jid:nameprep' |
152 |
|
-spec get_subdomain_info(domain()) -> |
153 |
|
{ok, mongoose_subdomain_core:subdomain_info()} | {error, not_found}. |
154 |
|
get_subdomain_info(Subdomain) -> |
155 |
2419 |
mongoose_subdomain_core:get_subdomain_info(Subdomain). |
156 |
|
|
157 |
|
%% Get the list of the host_types provided during initialisation |
158 |
|
%% This has complexity N, where N is the number of online domains. |
159 |
|
-spec get_all_static() -> [{domain(), host_type()}]. |
160 |
|
get_all_static() -> |
161 |
2 |
mongoose_domain_core:get_all_static(). |
162 |
|
|
163 |
|
%% Get domains, loaded from DB to this node |
164 |
|
-spec get_all_dynamic() -> [{domain(), host_type()}]. |
165 |
|
get_all_dynamic() -> |
166 |
1 |
mongoose_domain_core:get_all_dynamic(). |
167 |
|
|
168 |
|
%% Get the list of the host_types provided during initialisation |
169 |
|
%% This has complexity N, where N is the number of online domains. |
170 |
|
-spec get_domains_by_host_type(host_type()) -> [domain()]. |
171 |
|
get_domains_by_host_type(HostType) -> |
172 |
33 |
mongoose_domain_core:get_domains_by_host_type(HostType). |
173 |
|
|
174 |
|
check_domain(Domain, HostType) -> |
175 |
138 |
Static = mongoose_domain_core:is_static(Domain), |
176 |
138 |
Allowed = mongoose_domain_core:is_host_type_allowed(HostType), |
177 |
138 |
HasDb = service_domain_db:enabled(), |
178 |
138 |
if |
179 |
|
Static -> |
180 |
8 |
{error, static}; |
181 |
|
not Allowed -> |
182 |
9 |
{error, unknown_host_type}; |
183 |
|
not HasDb -> |
184 |
6 |
{error, service_disabled}; |
185 |
|
true -> |
186 |
115 |
ok |
187 |
|
end. |
188 |
|
|
189 |
|
-type password() :: binary(). |
190 |
|
|
191 |
|
-spec check_domain_password(domain(), password()) -> ok | {error, wrong_password | not_found}. |
192 |
|
check_domain_password(Domain, Password) -> |
193 |
8 |
case mongoose_domain_sql:select_domain_admin(Domain) of |
194 |
|
{ok, {Domain, PassDetails}} -> |
195 |
7 |
case do_check_domain_password(Password, PassDetails) of |
196 |
|
true -> |
197 |
6 |
ok; |
198 |
|
false -> |
199 |
1 |
{error, wrong_password} |
200 |
|
end; |
201 |
|
{error, not_found} -> |
202 |
1 |
{error, not_found} |
203 |
|
end. |
204 |
|
|
205 |
|
do_check_domain_password(Password, PassDetails) -> |
206 |
7 |
case mongoose_scram:deserialize(PassDetails) of |
207 |
|
{ok, Scram} -> |
208 |
7 |
mongoose_scram:check_password(Password, Scram); |
209 |
|
{error, _Reason} -> |
210 |
:-( |
false |
211 |
|
end. |
212 |
|
|
213 |
|
-spec set_domain_password(domain(), password()) -> ok | {error, not_found}. |
214 |
|
set_domain_password(Domain, Password) -> |
215 |
10 |
case get_host_type(Domain) of |
216 |
|
{ok, _} -> |
217 |
8 |
mongoose_domain_sql:set_domain_admin(Domain, Password); |
218 |
|
{error, not_found} -> |
219 |
2 |
{error, not_found} |
220 |
|
end. |
221 |
|
|
222 |
|
-spec delete_domain_password(domain()) -> ok. |
223 |
|
delete_domain_password(Domain) -> |
224 |
25 |
mongoose_domain_sql:delete_domain_admin(Domain). |
225 |
|
|
226 |
|
-spec register_subdomain(host_type(), subdomain_pattern(), |
227 |
|
mongoose_packet_handler:t()) -> |
228 |
|
ok | {error, already_registered | subdomain_already_exists}. |
229 |
|
register_subdomain(HostType, SubdomainPattern, PacketHandler) -> |
230 |
390 |
mongoose_subdomain_core:register_subdomain(HostType, SubdomainPattern, |
231 |
|
PacketHandler). |
232 |
|
|
233 |
|
-spec unregister_subdomain(host_type(), subdomain_pattern()) -> ok. |
234 |
|
unregister_subdomain(HostType, SubdomainPattern) -> |
235 |
390 |
mongoose_subdomain_core:unregister_subdomain(HostType, SubdomainPattern). |
236 |
|
|
237 |
|
-spec get_all_subdomains_for_domain(domain()) -> |
238 |
|
[mongoose_subdomain_core:subdomain_info()]. |
239 |
|
get_all_subdomains_for_domain(Domain) -> |
240 |
27 |
mongoose_subdomain_core:get_all_subdomains_for_domain(Domain). |