2003-03-09 21:46:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : cyrsasl.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-03-09 21:46:47 +01:00
|
|
|
%%% Purpose : Cyrus SASL-like library
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 8 Mar 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 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
|
|
|
|
%%%
|
2003-03-09 21:46:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(cyrsasl).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
-export([start/0,
|
2005-07-13 05:24:13 +02:00
|
|
|
register_mechanism/3,
|
|
|
|
listmech/1,
|
2010-04-15 17:20:16 +02:00
|
|
|
server_new/8,
|
2003-03-09 21:46:47 +01:00
|
|
|
server_start/3,
|
|
|
|
server_step/2]).
|
|
|
|
|
2010-04-15 17:20:16 +02:00
|
|
|
-include("cyrsasl.hrl").
|
2010-07-31 01:08:11 +02:00
|
|
|
-include("ejabberd.hrl").
|
2010-04-15 17:20:16 +02:00
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @type saslmechanism() = {sasl_mechanism, Mechanism, Module, Require_Plain}
|
|
|
|
%% Mechanism = string()
|
|
|
|
%% Module = atom()
|
|
|
|
%% Require_Plain = bool().
|
|
|
|
%% Registry entry of a supported SASL mechanism.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
-record(sasl_mechanism, {mechanism, module, require_plain_password}).
|
2009-01-23 11:10:33 +01:00
|
|
|
|
2009-08-17 19:17:34 +02:00
|
|
|
%% @type saslstate() = {sasl_state, Service, Myname, Realm, GetPassword, CheckPassword, CheckPasswordDigest, Mech_Mod, Mech_State}
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Service = string()
|
|
|
|
%% Myname = string()
|
|
|
|
%% Realm = string()
|
|
|
|
%% GetPassword = function()
|
|
|
|
%% CheckPassword = function()
|
2009-08-17 19:17:34 +02:00
|
|
|
%% CheckPasswordDigest = any()
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Mech_Mod = atom()
|
|
|
|
%% Mech_State = term().
|
|
|
|
%% State of this process.
|
|
|
|
|
2010-04-15 17:20:16 +02:00
|
|
|
-record(sasl_state, {service, myname,
|
|
|
|
mech_mod, mech_state, params}).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
-export([behaviour_info/1]).
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
behaviour_info(callbacks) ->
|
2010-04-15 17:20:16 +02:00
|
|
|
[{mech_new, 1}, {mech_step, 2}];
|
2007-12-07 00:15:04 +01:00
|
|
|
behaviour_info(_Other) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
undefined.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec () -> ok
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
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([]),
|
2006-04-07 02:39:24 +02:00
|
|
|
cyrsasl_anonymous:start([]),
|
2010-07-31 01:08:11 +02:00
|
|
|
maybe_try_start_gssapi(),
|
2003-03-09 21:46:47 +01:00
|
|
|
ok.
|
|
|
|
|
2010-07-31 01:08:11 +02:00
|
|
|
maybe_try_start_gssapi() ->
|
|
|
|
case os:getenv("KRB5_KTNAME") of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
_String ->
|
|
|
|
try_start_gssapi()
|
|
|
|
end.
|
|
|
|
|
|
|
|
try_start_gssapi() ->
|
|
|
|
case code:load_file(esasl) of
|
|
|
|
{module, _Module} ->
|
|
|
|
cyrsasl_gssapi:start([]);
|
|
|
|
{error, What} ->
|
|
|
|
?ERROR_MSG("Support for GSSAPI not started because esasl.beam was not found: ~p", [What])
|
|
|
|
end.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (Mechanism, Module, Require_Plain) -> true
|
|
|
|
%% Mechanism = string()
|
|
|
|
%% Module = atom()
|
|
|
|
%% Require_Plain = bool()
|
|
|
|
|
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
|
|
|
|
2009-01-23 11:10:33 +01: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 ->
|
|
|
|
% LUser = jlib:nodeprep(xml:get_attr_s(username, Props)),
|
2010-07-22 18:48:23 +02:00
|
|
|
% {U, S, R} = jlib:short_prepd_jid(JID),
|
2009-01-23 11:10:33 +01:00
|
|
|
% case R of
|
|
|
|
% "" ->
|
|
|
|
% {error, "invalid-authzid"};
|
|
|
|
% _ ->
|
|
|
|
% case {LUser, ?MYNAME} of
|
|
|
|
% {U, S} ->
|
|
|
|
% ok;
|
|
|
|
% _ ->
|
|
|
|
% {error, "invalid-authzid"}
|
|
|
|
% end
|
|
|
|
% end
|
|
|
|
% end.
|
|
|
|
|
|
|
|
%% @spec (State, Props) -> ok | {error, 'not-authorized'}
|
|
|
|
%% State = saslstate()
|
|
|
|
%% Props = [{Key, Value}]
|
|
|
|
%% Key = atom()
|
|
|
|
%% Value = string()
|
2003-06-07 19:30:25 +02:00
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
check_credentials(_State, Props) ->
|
2008-07-08 17:43:52 +02:00
|
|
|
case proplists:get_value(username, Props) of
|
|
|
|
undefined ->
|
|
|
|
{error, 'not-authorized'};
|
|
|
|
User ->
|
|
|
|
case exmpp_stringprep:is_node(User) of
|
|
|
|
false -> {error, 'not-authorized'};
|
|
|
|
true -> ok
|
|
|
|
end
|
2003-11-07 21:51:23 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (Host) -> [Mechanism]
|
|
|
|
%% Host = string()
|
|
|
|
%% Mechanism = string()
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
listmech(Host) ->
|
|
|
|
RequirePlainPassword = ejabberd_auth:plain_password_required(Host),
|
2007-12-07 00:15:04 +01:00
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
Mechs = ets:select(sasl_mechanism,
|
|
|
|
[{#sasl_mechanism{mechanism = '$1',
|
|
|
|
require_plain_password = '$2',
|
|
|
|
_ = '_'},
|
|
|
|
if
|
|
|
|
RequirePlainPassword ->
|
|
|
|
[{'==', '$2', false}];
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
['$1']}]),
|
|
|
|
filter_anonymous(Host, Mechs).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2010-07-23 13:36:35 +02:00
|
|
|
%% @spec (Service, ServerFQDN, UserRealm, SecFlags, GetPassword, CheckPassword, CheckPasswordDigest, Socket) -> saslstate()
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Service = string()
|
|
|
|
%% ServerFQDN = string()
|
|
|
|
%% UserRealm = string()
|
|
|
|
%% SecFlags = [term()]
|
|
|
|
%% GetPassword = function()
|
|
|
|
%% CheckPassword = function()
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
server_new(Service, ServerFQDN, UserRealm, _SecFlags,
|
2010-04-15 17:20:16 +02:00
|
|
|
GetPassword, CheckPassword, CheckPasswordDigest, Socket) ->
|
|
|
|
Params = #sasl_params{
|
|
|
|
host = ServerFQDN,
|
|
|
|
realm = UserRealm,
|
|
|
|
get_password = GetPassword,
|
|
|
|
check_password = CheckPassword,
|
|
|
|
check_password_digest= CheckPasswordDigest,
|
|
|
|
socket = Socket
|
|
|
|
},
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
#sasl_state{service = Service,
|
|
|
|
myname = ServerFQDN,
|
2010-04-15 17:20:16 +02:00
|
|
|
params = Params}.
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (State, Mech, ClientIn) -> Ok | Continue | Error
|
|
|
|
%% State = saslstate()
|
|
|
|
%% Mech = string()
|
|
|
|
%% ClientIn = string()
|
|
|
|
%% Ok = {ok, Props}
|
|
|
|
%% Props = [Prop]
|
|
|
|
%% Prop = [{Key, Value}]
|
|
|
|
%% Key = atom()
|
|
|
|
%% Value = string()
|
|
|
|
%% Continue = {continue, ServerOut, New_State}
|
|
|
|
%% ServerOut = string()
|
|
|
|
%% New_State = saslstate()
|
2011-03-16 18:38:44 +01:00
|
|
|
%% Error = {error, Reason} | {error, Reason, Username}
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Reason = term()
|
|
|
|
%% Username = string()
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
server_start(State, Mech, ClientIn) ->
|
2006-04-27 23:24:30 +02:00
|
|
|
case lists:member(Mech, listmech(State#sasl_state.myname)) of
|
|
|
|
true ->
|
|
|
|
case ets:lookup(sasl_mechanism, Mech) of
|
|
|
|
[#sasl_mechanism{module = Module}] ->
|
2010-04-15 17:20:16 +02:00
|
|
|
{ok, MechState} =
|
|
|
|
Module:mech_new(State#sasl_state.params),
|
2006-04-27 23:24:30 +02:00
|
|
|
server_step(State#sasl_state{mech_mod = Module,
|
|
|
|
mech_state = MechState},
|
|
|
|
ClientIn);
|
|
|
|
_ ->
|
2009-01-21 14:07:55 +01:00
|
|
|
{error, 'invalid-mechanism'}
|
2006-04-27 23:24:30 +02:00
|
|
|
end;
|
|
|
|
false ->
|
2009-01-21 14:07:55 +01:00
|
|
|
{error, 'invalid-mechanism'}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (State, ClientIn) -> Ok | Continue | Error
|
|
|
|
%% State = saslstate()
|
|
|
|
%% ClientIn = string()
|
|
|
|
%% Ok = {ok, Props}
|
|
|
|
%% Props = [Prop]
|
|
|
|
%% Prop = [{Key, Value}]
|
|
|
|
%% Key = atom()
|
|
|
|
%% Value = string()
|
|
|
|
%% Continue = {continue, ServerOut, New_State}
|
|
|
|
%% ServerOut = string()
|
|
|
|
%% New_State = saslstate()
|
2011-03-16 18:38:44 +01:00
|
|
|
%% Error = {error, Reason} | {error, Reason, Text, Username}
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Reason = term()
|
2011-03-16 18:38:44 +01:00
|
|
|
%% Text = string()
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Username = string()
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
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}};
|
2011-03-16 18:38:44 +01:00
|
|
|
{error, Error, Text, Username} ->
|
|
|
|
{error, Error, Text, Username};
|
2003-06-07 19:30:25 +02:00
|
|
|
{error, Error} ->
|
|
|
|
{error, Error}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (Host, Mechs) -> [Filtered_Mechs]
|
|
|
|
%% Host = string()
|
|
|
|
%% Mechs = [Mech]
|
|
|
|
%% Mech = string()
|
|
|
|
%% Filtered_Mechs = [Mech]
|
|
|
|
%%
|
|
|
|
%% @doc Remove the anonymous mechanism from the list if not enabled for
|
|
|
|
%% the given host.
|
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
filter_anonymous(Host, Mechs) ->
|
|
|
|
case ejabberd_auth_anonymous:is_sasl_anonymous_enabled(Host) of
|
|
|
|
true -> Mechs;
|
2006-04-27 23:24:30 +02:00
|
|
|
false -> Mechs -- ["ANONYMOUS"]
|
2006-04-07 02:39:24 +02:00
|
|
|
end.
|