2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_internal.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : Authentification via mnesia
|
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_internal).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
%% External exports
|
2005-07-13 05:24:13 +02:00
|
|
|
-export([start/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password/3,
|
|
|
|
check_password/3,
|
|
|
|
check_password/5,
|
|
|
|
try_register/3,
|
2004-12-12 22:00:34 +01:00
|
|
|
dirty_get_registered_users/0,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_registered_users/1,
|
2007-11-16 14:58:00 +01:00
|
|
|
get_vh_registered_users/2,
|
|
|
|
get_vh_registered_users_number/1,
|
|
|
|
get_vh_registered_users_number/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password/2,
|
|
|
|
get_password_s/2,
|
|
|
|
is_user_exists/2,
|
2004-12-12 22:00:34 +01:00
|
|
|
remove_user/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/3,
|
2004-12-12 22:00:34 +01:00
|
|
|
plain_password_required/0
|
|
|
|
]).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
|
|
|
-record(passwd, {us, password}).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2006-02-20 05:07:42 +01:00
|
|
|
start(Host) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:create_table(passwd, [{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, passwd)}]),
|
|
|
|
update_table(),
|
2006-02-20 05:07:42 +01:00
|
|
|
ejabberd_ctl:register_commands(
|
|
|
|
Host,
|
|
|
|
[{"registered-users", "list all registered users"}],
|
|
|
|
ejabberd_auth, ctl_process_get_registered),
|
2004-12-12 22:00:34 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
plain_password_required() ->
|
|
|
|
false.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password, StreamID, Digest) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[#passwd{password = Passwd}] ->
|
|
|
|
DigRes = if
|
|
|
|
Digest /= "" ->
|
|
|
|
Digest == sha:sha(StreamID ++ Passwd);
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if DigRes ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
(Passwd == Password) and (Password /= "")
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password(User, Server, Password) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
if
|
|
|
|
(LUser == error) or (LServer == error) ->
|
|
|
|
{error, invalid_jid};
|
|
|
|
true ->
|
2004-12-12 22:00:34 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:write(#passwd{us = US,
|
2004-12-12 22:00:34 +01:00
|
|
|
password = Password})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
try_register(User, Server, Password) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
if
|
|
|
|
(LUser == error) or (LServer == error) ->
|
|
|
|
{error, invalid_jid};
|
|
|
|
true ->
|
2004-12-12 22:00:34 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:read({passwd, US}) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:write(#passwd{us = US,
|
2004-12-12 22:00:34 +01:00
|
|
|
password = Password}),
|
|
|
|
ok;
|
|
|
|
[_E] ->
|
|
|
|
exists
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
|
|
|
|
|
|
|
dirty_get_registered_users() ->
|
|
|
|
mnesia:dirty_all_keys(passwd).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
mnesia:dirty_select(
|
|
|
|
passwd,
|
2007-11-16 14:58:00 +01:00
|
|
|
[{#passwd{us = '$1', _ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, LServer}],
|
2005-04-17 20:08:34 +02:00
|
|
|
['$1']}]).
|
|
|
|
|
2007-11-16 14:58:00 +01:00
|
|
|
get_vh_registered_users(Server, [{from, Start}, {to, End}])
|
|
|
|
when is_integer(Start) and is_integer(End) ->
|
|
|
|
get_vh_registered_users(Server, [{limit, End-Start+1}, {offset, Start}]);
|
|
|
|
|
|
|
|
get_vh_registered_users(Server, [{limit, Limit}, {offset, Offset}])
|
|
|
|
when is_integer(Limit) and is_integer(Offset) ->
|
|
|
|
case get_vh_registered_users(Server) of
|
|
|
|
[] ->
|
|
|
|
[];
|
|
|
|
Users ->
|
|
|
|
Set = lists:keysort(1, Users),
|
|
|
|
L = length(Set),
|
|
|
|
Start = if Offset < 1 -> 1;
|
|
|
|
Offset > L -> L;
|
|
|
|
true -> Offset
|
|
|
|
end,
|
|
|
|
lists:sublist(Set, Start, Limit)
|
|
|
|
end;
|
|
|
|
|
|
|
|
get_vh_registered_users(Server, [{prefix, Prefix}])
|
|
|
|
when is_list(Prefix) ->
|
|
|
|
Set = [{U,S} || {U, S} <- get_vh_registered_users(Server), lists:prefix(Prefix, U)],
|
|
|
|
lists:keysort(1, Set);
|
|
|
|
|
|
|
|
get_vh_registered_users(Server, [{prefix, Prefix}, {from, Start}, {to, End}])
|
|
|
|
when is_list(Prefix) and is_integer(Start) and is_integer(End) ->
|
|
|
|
get_vh_registered_users(Server, [{prefix, Prefix}, {limit, End-Start+1}, {offset, Start}]);
|
|
|
|
|
|
|
|
get_vh_registered_users(Server, [{prefix, Prefix}, {limit, Limit}, {offset, Offset}])
|
|
|
|
when is_list(Prefix) and is_integer(Limit) and is_integer(Offset) ->
|
|
|
|
case [{U,S} || {U, S} <- get_vh_registered_users(Server), lists:prefix(Prefix, U)] of
|
|
|
|
[] ->
|
|
|
|
[];
|
|
|
|
Users ->
|
|
|
|
Set = lists:keysort(1, Users),
|
|
|
|
L = length(Set),
|
|
|
|
Start = if Offset < 1 -> 1;
|
|
|
|
Offset > L -> L;
|
|
|
|
true -> Offset
|
|
|
|
end,
|
|
|
|
lists:sublist(Set, Start, Limit)
|
|
|
|
end;
|
|
|
|
|
|
|
|
get_vh_registered_users(Server, _) ->
|
|
|
|
get_vh_registered_users(Server).
|
|
|
|
|
|
|
|
get_vh_registered_users_number(Server) ->
|
|
|
|
Set = get_vh_registered_users(Server),
|
|
|
|
length(Set).
|
|
|
|
|
|
|
|
get_vh_registered_users_number(Server, [{prefix, Prefix}]) when is_list(Prefix) ->
|
|
|
|
Set = [{U, S} || {U, S} <- get_vh_registered_users(Server), lists:prefix(Prefix, U)],
|
|
|
|
length(Set);
|
|
|
|
|
|
|
|
get_vh_registered_users_number(Server, _) ->
|
|
|
|
get_vh_registered_users_number(Server).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password(User, Server) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read(passwd, US) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password_s(User, Server) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read(passwd, US) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[] ->
|
|
|
|
false;
|
|
|
|
[_] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2004-12-12 22:00:34 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete({passwd, US})
|
2004-12-12 22:00:34 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:run(remove_user, LServer, [User, Server]).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server, Password) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2004-12-12 22:00:34 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:read({passwd, US}) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[#passwd{password = Password}] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete({passwd, US}),
|
2004-12-12 22:00:34 +01:00
|
|
|
ok;
|
|
|
|
[_] ->
|
|
|
|
not_allowed;
|
|
|
|
_ ->
|
|
|
|
not_exists
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:run(remove_user, LServer, [User, Server]),
|
2004-12-12 22:00:34 +01:00
|
|
|
ok;
|
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
bad_request
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, passwd),
|
|
|
|
case mnesia:table_info(passwd, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[user, password] ->
|
|
|
|
?INFO_MSG("Converting passwd table from "
|
|
|
|
"{user, password} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
ejabberd_auth_internal_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, passwd},
|
|
|
|
{attributes, record_info(fields, passwd)}]),
|
|
|
|
mnesia:transform_table(passwd, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(ejabberd_auth_internal_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#passwd{us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
ejabberd_auth_internal_tmp_table,
|
|
|
|
R#passwd{us = {U, Host}})
|
|
|
|
end, ok, passwd)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(passwd),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(passwd),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, ejabberd_auth_internal_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(ejabberd_auth_internal_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating passwd table", []),
|
|
|
|
mnesia:transform_table(passwd, ignore, Fields)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|