2003-03-09 21:46:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : cyrsasl.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : Cyrus SASL-like library
|
|
|
|
%%% Created : 8 Mar 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(cyrsasl).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-export([start/0,
|
2005-07-13 05:24:13 +02:00
|
|
|
register_mechanism/3,
|
|
|
|
listmech/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
server_new/6,
|
2003-03-09 21:46:47 +01:00
|
|
|
server_start/3,
|
|
|
|
server_step/2]).
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
-record(sasl_mechanism, {mechanism, module, require_plain_password}).
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(sasl_state, {service, myname, realm,
|
|
|
|
get_password, check_password,
|
|
|
|
mech_mod, mech_state}).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
-export([behaviour_info/1]).
|
|
|
|
|
|
|
|
behaviour_info(callbacks) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
[{mech_new, 2},
|
2003-03-09 21:46:47 +01:00
|
|
|
{mech_step, 2}];
|
|
|
|
behaviour_info(Other) ->
|
|
|
|
undefined.
|
|
|
|
|
|
|
|
start() ->
|
|
|
|
ets:new(sasl_mechanism, [named_table,
|
|
|
|
public,
|
|
|
|
{keypos, #sasl_mechanism.mechanism}]),
|
|
|
|
cyrsasl_plain:start([]),
|
2003-03-12 20:48:05 +01:00
|
|
|
cyrsasl_digest:start([]),
|
2003-03-09 21:46:47 +01:00
|
|
|
ok.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
register_mechanism(Mechanism, Module, RequirePlainPassword) ->
|
|
|
|
ets:insert(sasl_mechanism,
|
|
|
|
#sasl_mechanism{mechanism = Mechanism,
|
|
|
|
module = Module,
|
|
|
|
require_plain_password = RequirePlainPassword}).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2003-06-07 19:30:25 +02:00
|
|
|
% TODO: use callbacks
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("jlib.hrl").
|
|
|
|
check_authzid(State, Props) ->
|
|
|
|
AuthzId = xml:get_attr_s(authzid, Props),
|
|
|
|
case jlib:string_to_jid(AuthzId) of
|
|
|
|
error ->
|
|
|
|
{error, "invalid-authzid"};
|
|
|
|
JID ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(xml:get_attr_s(username, Props)),
|
2003-06-07 19:30:25 +02:00
|
|
|
{U, S, R} = jlib:jid_tolower(JID),
|
|
|
|
case R of
|
|
|
|
"" ->
|
|
|
|
{error, "invalid-authzid"};
|
|
|
|
_ ->
|
|
|
|
case {LUser, ?MYNAME} of
|
|
|
|
{U, S} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
{error, "invalid-authzid"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2003-11-07 21:51:23 +01:00
|
|
|
check_credentials(State, Props) ->
|
|
|
|
User = xml:get_attr_s(username, Props),
|
|
|
|
case jlib:nodeprep(User) of
|
|
|
|
error ->
|
|
|
|
{error, "not-authorized"};
|
|
|
|
"" ->
|
|
|
|
{error, "not-authorized"};
|
|
|
|
LUser ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
listmech(Host) ->
|
|
|
|
RequirePlainPassword = ejabberd_auth:plain_password_required(Host),
|
2003-03-09 21:46:47 +01:00
|
|
|
ets:select(sasl_mechanism,
|
2005-07-13 05:24:13 +02:00
|
|
|
[{#sasl_mechanism{mechanism = '$1',
|
|
|
|
require_plain_password = '$2',
|
|
|
|
_ = '_'},
|
|
|
|
if
|
|
|
|
RequirePlainPassword ->
|
|
|
|
[{'==', '$2', false}];
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
['$1']}]).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
server_new(Service, ServerFQDN, UserRealm, SecFlags,
|
|
|
|
GetPassword, CheckPassword) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
#sasl_state{service = Service,
|
|
|
|
myname = ServerFQDN,
|
2005-04-17 20:08:34 +02:00
|
|
|
realm = UserRealm,
|
|
|
|
get_password = GetPassword,
|
|
|
|
check_password = CheckPassword}.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
server_start(State, Mech, ClientIn) ->
|
|
|
|
case ets:lookup(sasl_mechanism, Mech) of
|
|
|
|
[#sasl_mechanism{module = Module}] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
{ok, MechState} = Module:mech_new(State#sasl_state.get_password,
|
|
|
|
State#sasl_state.check_password),
|
2003-03-09 21:46:47 +01:00
|
|
|
server_step(State#sasl_state{mech_mod = Module,
|
|
|
|
mech_state = MechState},
|
|
|
|
ClientIn);
|
|
|
|
_ ->
|
2003-06-07 19:30:25 +02:00
|
|
|
{error, "no-mechanism"}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
server_step(State, ClientIn) ->
|
|
|
|
Module = State#sasl_state.mech_mod,
|
|
|
|
MechState = State#sasl_state.mech_state,
|
|
|
|
case Module:mech_step(MechState, ClientIn) of
|
|
|
|
{ok, Props} ->
|
2003-11-07 21:51:23 +01:00
|
|
|
case check_credentials(State, Props) of
|
2003-06-07 19:30:25 +02:00
|
|
|
ok ->
|
|
|
|
{ok, Props};
|
|
|
|
{error, Error} ->
|
|
|
|
{error, Error}
|
|
|
|
end;
|
2003-03-09 21:46:47 +01:00
|
|
|
{continue, ServerOut, NewMechState} ->
|
|
|
|
{continue, ServerOut,
|
|
|
|
State#sasl_state{mech_state = NewMechState}};
|
2003-06-07 19:30:25 +02:00
|
|
|
{error, Error} ->
|
|
|
|
{error, Error}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|