Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ejabberd_auth.erl « src - github.com/processone/ejabberd.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 344232c8688c04dbedd2cd81cf156c1107eca572 (plain)
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
%%%----------------------------------------------------------------------
%%% File    : ejabberd_auth.erl
%%% Author  : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Authentification
%%% Created : 23 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2011   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., 59 Temple Place, Suite 330, Boston, MA
%%% 02111-1307 USA
%%%
%%%----------------------------------------------------------------------

%% TODO: Use the functions in ejabberd auth to add and remove users.

-module(ejabberd_auth).
-author('alexey@process-one.net').

%% External exports
-export([start/0,
	 set_password/3,
	 check_password/3,
	 check_password/5,
	 check_password_with_authmodule/3,
	 check_password_with_authmodule/5,
	 try_register/3,
	 dirty_get_registered_users/0,
	 get_vh_registered_users/1,
	 get_vh_registered_users/2,
	 get_vh_registered_users_number/1,
	 get_vh_registered_users_number/2,
	 get_password/2,
	 get_password_s/2,
	 get_password_with_authmodule/2,
	 is_user_exists/2,
	 is_user_exists_in_other_modules/3,
	 remove_user/2,
	 remove_user/3,
	 plain_password_required/1,
	 store_type/1,
	 entropy/1
	]).

-export([start/1
         ,start_modules/2
         ,stop_modules/2
         ,start_methods/2
         ,stop_methods/2
        ]).

-export([auth_modules/1]).

-include("ejabberd.hrl").

%% @type authmodule() = ejabberd_auth_anonymous | ejabberd_auth_external |
%%                      ejabberd_auth_ldap | ejabberd_auth_pam |
%%                      ejabberd_auth_storage | atom().

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------

%% @spec () -> term()

start() ->
    ?DEBUG("About to start auth modules. Hosts: ~p", [?MYHOSTS]),
    lists:foreach(fun start/1, ?MYHOSTS).

start(Host) ->
    start_modules(Host, auth_modules(Host)).

start_modules(Host, Modules) when is_list(Modules) ->
    lists:foreach(fun (M) -> start_modules(Host, M) end, Modules);
start_modules(Host, Module) when is_atom(Module) ->
    Module:start(Host).

stop_modules(Host, Modules) when is_list(Modules) ->
    lists:foreach(fun (M) -> stop_modules(Host, M) end, Modules);
stop_modules(Host, Module) when is_atom(Module) ->
    Module:stop(Host).

start_methods(Host, Methods) when is_list(Methods) ->
    lists:foreach(fun (M) -> start_methods(Host, M) end, Methods);
start_methods(Host, Method) when is_atom(Method) ->
    start_modules(Host, module_name(Method)).

stop_methods(Host, Methods) when is_list(Methods) ->
    lists:foreach(fun (M) -> stop_methods(Host, M) end, Methods);
stop_methods(Host, Method) when is_atom(Method) ->
    stop_modules(Host, module_name(Method)).


%% @spec (Server) -> bool()
%%     Server = string()

%% This is only executed by ejabberd_c2s for non-SASL auth client
plain_password_required(Server) when is_list(Server) ->
    lists:any(
      fun(M) ->
	      M:plain_password_required()
      end, auth_modules(Server)).

%% @spec (Server) -> bool()
%%     Server = string()

store_type(Server) ->
    lists:foldl(
      fun(_, external) ->
	      external;
	  (M, scram) ->
	      case M:store_type() of
		  external ->
		      external;
		  _Else ->
		      scram
		  end;
	  (M, plain) ->
	      M:store_type()
      end, plain, auth_modules(Server)).

%% @spec (User, Server, Password) -> bool()
%%     User = string()
%%     Server = string()
%%     Password = string()
%% @doc Check if the user and password can login in server.

check_password(User, Server, Password)
  when is_list(User), is_list(Server), is_list(Password) ->
    case check_password_with_authmodule(User, Server, Password) of
	{true, _AuthModule} -> true;
	{false, _Reason} -> false
    end.

%% @spec (User, Server, Password, Digest, DigestGen) -> bool()
%%     User = string()
%%     Server = string()
%%     Password = string()
%%     Digest = string()
%%     DigestGen = function()
%% @doc Check if the user and password can login in server.

check_password(User, Server, Password, Digest, DigestGen)
  when is_list(User), is_list(Server), is_list(Password),
  is_list(Digest), is_function(DigestGen) ->
    case check_password_with_authmodule(User, Server, Password,
					Digest, DigestGen) of
	{true, _AuthModule} -> true;
	{false, _Reason} -> false
    end.

%% @spec (User::string(), Server::string(), Password::string()) ->
%%     {true, AuthModule} | {false, Reason::string()}
%% where
%%   AuthModule = ejabberd_auth_anonymous | ejabberd_auth_external
%%                 | ejabberd_auth_internal | ejabberd_auth_ldap
%%                 | ejabberd_auth_odbc | ejabberd_auth_pam
%% @doc Check if the user and password can login in server.
%% The user can login if at least an authentication method accepts the user
%% and the password.
%% The first authentication method that accepts the credentials is returned.
check_password_with_authmodule(User, Server, Password)
  when is_list(User), is_list(Server), is_list(Password) ->
    check_password_loop(auth_modules(Server), [User, Server, Password], "").

%% @spec (User, Server, Password, Digest, DigestGen) -> {true, AuthModule} | false
%%     User = string()
%%     Server = string()
%%     Password = string() | undefined
%%     Digest = string() | undefined
%%     DigestGen = function()
%%     AuthModule = authmodule()
%% @doc Check the password is valid and also return the authentication module that accepts it.
%% The password is 'undefined' if the client
%% authenticates using the digest method as defined in
%% XEP-0078: Non-SASL Authentication

check_password_with_authmodule(User, Server, Password, Digest, DigestGen)
  when is_list(User), is_list(Server), (is_list(Password) orelse Password == 'undefined'),
  is_function(DigestGen), (is_list(Digest) orelse Digest == 'undefined')->
    check_password_loop(auth_modules(Server), [User, Server, Password,
					       Digest, DigestGen], "").

check_password_loop([], _Args, LastReason) ->
    {false, LastReason};
check_password_loop([AuthModule | AuthModules], Args, PreviousReason) ->
    case apply(AuthModule, check_password, Args) of
	true ->
	    {true, AuthModule};
	{false, Reason} when Reason /= "" ->
	    check_password_loop(AuthModules, Args, Reason);
	{false, ""} ->
	    check_password_loop(AuthModules, Args, PreviousReason);
	false ->
	    check_password_loop(AuthModules, Args, PreviousReason)
    end.

%% @spec (User, Server, Password) -> ok | {error, ErrorType}
%%     User = string()
%%     Server = string()
%%     Password = string()
%%     ErrorType = empty_password | not_allowed | invalid_jid
set_password(_User, _Server, "") ->
    %% We do not allow empty password
    {error, empty_password};
set_password(User, Server, Password)
  when is_list(User), is_list(Server), is_list(Password) ->
    lists:foldl(
      fun(M, {error, _}) ->
	      M:set_password(User, Server, Password);
	 (_M, Res) ->
	      Res
      end, {error, not_allowed}, auth_modules(Server)).

%% @spec (User, Server, Password) -> {atomic, ok} | {atomic, exists} | {error, not_allowed}
%%     User = string()
%%     Server = string()
%%     Password = string() | nil()
try_register(_User, _Server, "") ->
    %% We do not allow empty password
    {error, not_allowed};
try_register(User, Server, Password)
  when is_list(User), is_list(Server), is_list(Password) ->
    case is_user_exists(User, Server) of
	true ->
	    {atomic, exists};
	false ->
	    case ?IS_MY_HOST(exmpp_stringprep:nameprep(Server)) of
		true ->
		    Res = lists:foldl(
			    fun (_M, {atomic, ok} = Res) ->
				    Res;
				(M, _) ->
				    M:try_register(User, Server, Password)
			    end, {error, not_allowed}, auth_modules(Server)),
		    trigger_register_hooks(Res, User, Server);
		false ->
		    {error, not_allowed}
	    end
    end.

trigger_register_hooks({atomic, ok} = Res, User, Server) ->
    ejabberd_hooks:run(register_user, list_to_binary(Server),
		       [User, Server]),
    Res;
trigger_register_hooks(Res, _User, _Server) ->
    Res.

%% @spec () -> [{LUser, LServer}]
%%     LUser = string()
%%     LServer = string()
%% @doc Registered users list do not include anonymous users logged.

dirty_get_registered_users() ->
    lists:flatmap(
      fun(M) ->
	      M:dirty_get_registered_users()
      end, auth_modules()).

%% @spec (Server) -> [{LUser, LServer}]
%%     Server = string()
%%     LUser = string()
%%     LServer = string()
%% @doc Registered users list do not include anonymous users logged.

get_vh_registered_users(Server) when is_list(Server) ->
    lists:flatmap(
      fun(M) ->
	      M:get_vh_registered_users(Server)
      end, auth_modules(Server)).

%% @spec (Server, Opts) -> [{LUser, LServer}]
%%     Server = string()
%%     Opts = [{Opt, Val}]
%%         Opt = atom()
%%         Val = term()
%%     LUser = string()
%%     LServer = string()

get_vh_registered_users(Server, Opts) when is_list(Server) ->
    lists:flatmap(
      fun(M) ->
		case erlang:function_exported(
		       M, get_vh_registered_users, 2) of
		    true ->
			M:get_vh_registered_users(Server, Opts);
		    false ->
			M:get_vh_registered_users(Server)
		end
      end, auth_modules(Server)).

%% @spec (Server) -> Users_Number
%%     Server = string()
%%     Users_Number = integer()

get_vh_registered_users_number(Server) when is_list(Server) ->
    lists:sum(
      lists:map(
	fun(M) ->
		case erlang:function_exported(
		       M, get_vh_registered_users_number, 1) of
		    true ->
			M:get_vh_registered_users_number(Server);
		    false ->
			length(M:get_vh_registered_users(Server))
		end
	end, auth_modules(Server))).

%% @spec (Server, Opts) -> Users_Number
%%     Server = string()
%%     Opts = [{Opt, Val}]
%%         Opt = atom()
%%         Val = term()
%%     Users_Number = integer()

get_vh_registered_users_number(Server, Opts) when is_list(Server) ->
    lists:sum(
      lists:map(
	fun(M) ->
		case erlang:function_exported(
		       M, get_vh_registered_users_number, 2) of
		    true ->
			M:get_vh_registered_users_number(Server, Opts);
		    false ->
			length(M:get_vh_registered_users(Server))
		end
	end, auth_modules(Server))).

%% @spec (User, Server) -> Password | false
%%     User = string()
%%     Server = string()
%%     Password = string()
%% @doc Get the password of the user.

get_password(User, Server) when is_list(User), is_list(Server) ->
    lists:foldl(
      fun(M, false) ->
	      M:get_password(User, Server);
	 (_M, Password) ->
	      Password
      end, false, auth_modules(Server)).

%% @spec (User, Server) -> Password | nil()
%%     User = string()
%%     Server = string()
%%     Password = string()
%% @doc Get the password of the user.

get_password_s(User, Server) when is_list(User), is_list(Server) ->
    case get_password(User, Server) of
	false ->
	    "";
	Password when is_list(Password) ->
	    Password;
	_ ->
	    ""
    end.

%% @spec (User, Server) -> {Password, AuthModule} | {false, none}
%%     User = string()
%%     Server = string()
%%     Password = string()
%%     AuthModule = authmodule()
%% @doc Get the password of the user and the auth module.

get_password_with_authmodule(User, Server)
  when is_list(User), is_list(Server) ->
    lists:foldl(
      fun(M, {false, _}) ->
	      {M:get_password(User, Server), M};
	 (_M, {Password, AuthModule}) ->
	      {Password, AuthModule}
      end, {false, none}, auth_modules(Server)).

%% @spec (User, Server) -> bool()
%%     User = string()
%%     Server = string()
%% @doc Returns true if the user exists in the DB or if an anonymous
%% user is logged under the given name.

is_user_exists(User, Server) when is_list(User), is_list(Server) ->
    lists:any(
      fun(M) ->
	      case M:is_user_exists(User, Server) of
		  {error, Error} ->
		      ?ERROR_MSG("The authentication module ~p returned an "
				 "error~nwhen checking user ~p in server ~p~n"
				 "Error message: ~p",
				 [M, User, Server, Error]),
		      false;
		  Else ->
		      Else
	      end
      end, auth_modules(Server)).

%% @spec (Module, User, Server) -> true | false | maybe
%%     Module = authmodule()
%%     User = string()
%%     Server = string()
%% @doc Check if the user exists in all authentications module except
%% the module passed as parameter.

is_user_exists_in_other_modules(Module, User, Server)
  when is_list(User), is_list(Server) ->
    is_user_exists_in_other_modules_loop(
      auth_modules(Server)--[Module],
      User, Server).
is_user_exists_in_other_modules_loop([], _User, _Server) ->
    false;
is_user_exists_in_other_modules_loop([AuthModule|AuthModules], User, Server) ->
    case AuthModule:is_user_exists(User, Server) of
	true ->
	    true;
	false ->
	    is_user_exists_in_other_modules_loop(AuthModules, User, Server);
	{error, Error} ->
	    ?DEBUG("The authentication module ~p returned an error~nwhen "
		   "checking user ~p in server ~p~nError message: ~p",
		   [AuthModule, User, Server, Error]),
	    maybe
    end.


%% @spec (User, Server) -> ok | error | {error, not_allowed}
%%     User = string()
%%     Server = string()
%% @doc Remove user.
%% TODO: Fix me: It always return ok even if there was some problem removing the user.
%% dialyzer warning
%% ejabberd_auth.erl:388: The variable _ can never match since previous clauses completely covered the type 'ok'

remove_user(User, Server) when is_list(User), is_list(Server) ->
    R = lists:foreach(
	  fun(M) ->
		  M:remove_user(User, Server)
	  end, auth_modules(Server)),
    ejabberd_hooks:run(remove_user, list_to_binary(exmpp_stringprep:nameprep(Server)),
				 [list_to_binary(User), list_to_binary(Server)]),
    R.

%% @spec (User, Server, Password) -> ok | not_exists | not_allowed | bad_request | error
%%     User = string()
%%     Server = string()
%%     Password = string()
%% @doc Try to remove user if the provided password is correct.
%% The removal is attempted in each auth method provided:
%% when one returns 'ok' the loop stops;
%% if no method returns 'ok' then it returns the error message indicated by the last method attempted.

remove_user(User, Server, Password)
  when is_list(User), is_list(Server), is_list(Password) ->
    R = lists:foldl(
      fun(_M, ok = Res) ->
	      Res;
	 (M, _) ->
	      M:remove_user(User, Server, Password)
      end, error, auth_modules(Server)),
    case R of
		ok -> ejabberd_hooks:run(remove_user, list_to_binary(exmpp_stringprep:nameprep(Server)),
                                    [list_to_binary(User), list_to_binary(Server)]);
		_ -> none
    end,
    R.

%% @spec (IOList) -> non_negative_float()
%% @doc Calculate informational entropy.
entropy(IOList) ->
    case binary_to_list(iolist_to_binary(IOList)) of
	"" ->
	    0.0;
	S ->
	    Set = lists:foldl(
		    fun(C, [Digit, Printable, LowLetter, HiLetter, Other]) ->
			    if C >= $a, C =< $z ->
				    [Digit, Printable, 26, HiLetter, Other];
			       C >= $0, C =< $9 ->
				    [9, Printable, LowLetter, HiLetter, Other];
			       C >= $A, C =< $Z ->
				    [Digit, Printable, LowLetter, 26, Other];
			       C >= 16#21, C =< 16#7e ->
				    [Digit, 33, LowLetter, HiLetter, Other];
			       true ->
				    [Digit, Printable, LowLetter, HiLetter, 128]
			    end
		    end, [0, 0, 0, 0, 0], S),
	    length(S) * math:log(lists:sum(Set))/math:log(2)
    end.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

%% @spec () -> [authmodule()]
%% @doc Return the lists of all the auth modules actually used in the
%% configuration.

auth_modules() ->
    lists:usort(
      lists:flatmap(
	fun(Server) ->
		auth_modules(Server)
	end, ?MYHOSTS)).

%% @spec (Server) -> [authmodule()]
%%     Server = string()
%% @doc Return the list of authenticated modules for a given host.

auth_modules(Server) when is_list(Server) ->
    LServer = exmpp_stringprep:nameprep(Server),
    Method = ejabberd_config:get_local_option({auth_method, ejabberd:normalize_host(LServer)}),
    Methods = if
		  Method == undefined -> [];
		  is_list(Method) -> Method;
		  is_atom(Method) -> [Method]
	      end,
    [module_name(M) || M <- Methods].

module_name(Method) when is_atom(Method) ->
    list_to_atom("ejabberd_auth_" ++ atom_to_list(Method)).