2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_internal.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-12-12 22:00:34 +01:00
|
|
|
%%% Purpose : Authentification via mnesia
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% 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.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% 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
|
|
|
|
%%%
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_internal).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-12 22:00:34 +01:00
|
|
|
|
|
|
|
%% 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
|
|
|
|
%%%----------------------------------------------------------------------
|
2008-10-13 12:11:19 +02: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(),
|
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) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
2004-12-12 22:00:34 +01:00
|
|
|
[#passwd{password = Password}] ->
|
2008-02-11 19:19:42 +01:00
|
|
|
Password /= "";
|
2004-12-12 22:00:34 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password, StreamID, Digest) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
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.
|
|
|
|
|
2008-09-16 16:39:57 +02:00
|
|
|
%% @spec (User::string(), Server::string(), Password::string()) ->
|
|
|
|
%% ok | {error, invalid_jid}
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
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,
|
2008-09-16 16:39:57 +02:00
|
|
|
{atomic, ok} = mnesia:transaction(F),
|
|
|
|
ok
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server, Password) -> {atomic, ok} | {atomic, exists} | {error, invalid_jid} | {aborted, Reason}
|
2005-04-17 20:08:34 +02:00
|
|
|
try_register(User, Server, Password) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
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.
|
|
|
|
|
2008-02-19 14:49:29 +01:00
|
|
|
%% Get all registered users in Mnesia
|
2004-12-12 22:00:34 +01:00
|
|
|
dirty_get_registered_users() ->
|
|
|
|
mnesia:dirty_all_keys(passwd).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
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) ->
|
2008-10-10 16:37:26 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read(passwd, US) of
|
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
catch
|
2004-12-12 22:00:34 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password_s(User, Server) ->
|
2008-10-10 16:37:26 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read(passwd, US) of
|
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
catch
|
2004-12-12 22:00:34 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2008-10-10 16:37:26 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
|
|
|
[] ->
|
|
|
|
false;
|
|
|
|
[_] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
catch
|
2004-12-12 22:00:34 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server) -> ok
|
2009-01-19 12:59:40 +01:00
|
|
|
%% @doc Remove user.
|
2009-01-19 11:14:04 +01:00
|
|
|
%% Note: it returns ok even if there was some problem removing the user.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2008-10-10 16:37:26 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({passwd, US})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2009-01-19 11:14:04 +01:00
|
|
|
ok
|
2008-10-10 16:37:26 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | not_exists | not_allowed | bad_request
|
2009-01-19 12:59:40 +01:00
|
|
|
%% @doc Remove user if the provided password is correct.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server, Password) ->
|
2008-10-10 16:37:26 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun() ->
|
|
|
|
case mnesia:read({passwd, US}) of
|
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
mnesia:delete({passwd, US}),
|
|
|
|
ok;
|
|
|
|
[_] ->
|
|
|
|
not_allowed;
|
|
|
|
_ ->
|
|
|
|
not_exists
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
ok;
|
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
bad_request
|
|
|
|
end
|
|
|
|
catch
|
2004-12-12 22:00:34 +01:00
|
|
|
_ ->
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|