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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 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-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2003-03-09 21:46:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(cyrsasl).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2017-02-26 13:10:59 +01:00
|
|
|
-behaviour(gen_server).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-export([start_link/0, register_mechanism/3, listmech/1,
|
2015-06-01 14:38:27 +02:00
|
|
|
server_new/7, server_start/3, server_step/2,
|
2017-01-09 15:02:17 +01:00
|
|
|
get_mech/1, format_error/2]).
|
2017-02-26 13:10:59 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2011-08-16 00:26:49 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2011-08-16 00:26:49 +02:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(sasl_mechanism,
|
|
|
|
{mechanism = <<"">> :: mechanism() | '$1',
|
|
|
|
module :: atom(),
|
|
|
|
password_type = plain :: password_type() | '$2'}).
|
|
|
|
|
|
|
|
-type(mechanism() :: binary()).
|
|
|
|
-type(mechanisms() :: [mechanism(),...]).
|
|
|
|
-type(password_type() :: plain | digest | scram).
|
2016-12-31 11:47:35 +01:00
|
|
|
-type sasl_property() :: {username, binary()} |
|
|
|
|
{authzid, binary()} |
|
|
|
|
{mechanism, binary()} |
|
|
|
|
{auth_module, atom()}.
|
|
|
|
-type sasl_return() :: {ok, [sasl_property()]} |
|
|
|
|
{ok, [sasl_property()], binary()} |
|
|
|
|
{continue, binary(), sasl_state()} |
|
|
|
|
{error, atom(), binary()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-type(sasl_mechanism() :: #sasl_mechanism{}).
|
2016-12-31 11:47:35 +01:00
|
|
|
-type error_reason() :: cyrsasl_digest:error_reason() |
|
|
|
|
cyrsasl_oauth:error_reason() |
|
|
|
|
cyrsasl_plain:error_reason() |
|
|
|
|
cyrsasl_scram:error_reason() |
|
|
|
|
unsupported_mechanism | nodeprep_failed |
|
|
|
|
empty_username | aborted.
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(sasl_state,
|
|
|
|
{
|
|
|
|
service,
|
|
|
|
myname,
|
|
|
|
realm,
|
|
|
|
get_password,
|
|
|
|
check_password,
|
|
|
|
check_password_digest,
|
2016-12-31 11:47:35 +01:00
|
|
|
mech_name = <<"">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
mech_mod,
|
|
|
|
mech_state
|
|
|
|
}).
|
2016-12-31 11:47:35 +01:00
|
|
|
-type sasl_state() :: #sasl_state{}.
|
|
|
|
-export_type([mechanism/0, mechanisms/0, sasl_mechanism/0, error_reason/0,
|
|
|
|
sasl_state/0, sasl_return/0, sasl_property/0]).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-callback start(list()) -> any().
|
|
|
|
-callback stop() -> any().
|
2013-03-14 10:33:02 +01:00
|
|
|
-callback mech_new(binary(), fun(), fun(), fun()) -> any().
|
2016-12-31 11:47:35 +01:00
|
|
|
-callback mech_step(any(), binary()) -> sasl_return().
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ets:new(sasl_mechanism,
|
|
|
|
[named_table, public,
|
|
|
|
{keypos, #sasl_mechanism.mechanism}]),
|
2003-03-09 21:46:47 +01:00
|
|
|
cyrsasl_plain:start([]),
|
2003-03-12 20:48:05 +01:00
|
|
|
cyrsasl_digest:start([]),
|
2011-08-16 00:25:03 +02:00
|
|
|
cyrsasl_scram:start([]),
|
2006-04-07 02:39:24 +02:00
|
|
|
cyrsasl_anonymous:start([]),
|
2015-09-25 14:53:25 +02:00
|
|
|
cyrsasl_oauth:start([]),
|
2017-02-26 13:10:59 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
cyrsasl_plain:stop(),
|
|
|
|
cyrsasl_digest:stop(),
|
|
|
|
cyrsasl_scram:stop(),
|
|
|
|
cyrsasl_anonymous:stop(),
|
|
|
|
cyrsasl_oauth:stop().
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2016-12-31 11:47:35 +01:00
|
|
|
-spec format_error(mechanism() | sasl_state(), error_reason()) -> {atom(), binary()}.
|
|
|
|
format_error(_, unsupported_mechanism) ->
|
|
|
|
{'invalid-mechanism', <<"Unsupported mechanism">>};
|
|
|
|
format_error(_, nodeprep_failed) ->
|
|
|
|
{'bad-protocol', <<"Nodeprep failed">>};
|
|
|
|
format_error(_, empty_username) ->
|
|
|
|
{'bad-protocol', <<"Empty username">>};
|
|
|
|
format_error(_, aborted) ->
|
|
|
|
{'aborted', <<"Aborted">>};
|
|
|
|
format_error(#sasl_state{mech_mod = Mod}, Reason) ->
|
|
|
|
Mod:format_error(Reason);
|
|
|
|
format_error(Mech, Reason) ->
|
|
|
|
case ets:lookup(sasl_mechanism, Mech) of
|
|
|
|
[#sasl_mechanism{module = Mod}] ->
|
|
|
|
Mod:format_error(Reason);
|
|
|
|
[] ->
|
|
|
|
{'invalid-mechanism', <<"Unsupported mechanism">>}
|
|
|
|
end.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec register_mechanism(Mechanim :: mechanism(), Module :: module(),
|
|
|
|
PasswordType :: password_type()) -> any().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2011-08-16 00:25:03 +02:00
|
|
|
register_mechanism(Mechanism, Module, PasswordType) ->
|
2014-11-10 01:10:04 +01:00
|
|
|
ets:insert(sasl_mechanism,
|
|
|
|
#sasl_mechanism{mechanism = Mechanism, module = Module,
|
2017-01-09 15:02:17 +01:00
|
|
|
password_type = PasswordType}).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
check_credentials(_State, Props) ->
|
2015-04-09 03:21:09 +02:00
|
|
|
User = proplists:get_value(authzid, Props, <<>>),
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nodeprep(User) of
|
2016-12-31 11:47:35 +01:00
|
|
|
error -> {error, nodeprep_failed};
|
|
|
|
<<"">> -> {error, empty_username};
|
2013-03-14 10:33:02 +01:00
|
|
|
_LUser -> ok
|
2003-11-07 21:51:23 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec listmech(Host ::binary()) -> Mechanisms::mechanisms().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
listmech(Host) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ets:select(sasl_mechanism,
|
2006-04-07 02:39:24 +02:00
|
|
|
[{#sasl_mechanism{mechanism = '$1',
|
2013-03-14 10:33:02 +01:00
|
|
|
password_type = '$2', _ = '_'},
|
2011-08-16 00:26:49 +02:00
|
|
|
case catch ejabberd_auth:store_type(Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
external -> [{'==', '$2', plain}];
|
|
|
|
scram -> [{'/=', '$2', digest}];
|
|
|
|
{'EXIT', {undef, [{Module, store_type, []} | _]}} ->
|
|
|
|
?WARNING_MSG("~p doesn't implement the function store_type/0",
|
|
|
|
[Module]),
|
|
|
|
[];
|
|
|
|
_Else -> []
|
2006-04-07 02:39:24 +02:00
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
['$1']}]).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2016-12-31 11:47:35 +01:00
|
|
|
-spec server_new(binary(), binary(), binary(), term(),
|
|
|
|
fun(), fun(), fun()) -> sasl_state().
|
2007-12-07 00:15:04 +01:00
|
|
|
server_new(Service, ServerFQDN, UserRealm, _SecFlags,
|
2009-04-22 13:44:03 +02:00
|
|
|
GetPassword, CheckPassword, CheckPasswordDigest) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#sasl_state{service = Service, myname = ServerFQDN,
|
|
|
|
realm = UserRealm, get_password = GetPassword,
|
2009-04-22 13:44:03 +02:00
|
|
|
check_password = CheckPassword,
|
2013-03-14 10:33:02 +01:00
|
|
|
check_password_digest = CheckPasswordDigest}.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2016-12-31 11:47:35 +01:00
|
|
|
-spec server_start(sasl_state(), mechanism(), binary()) -> sasl_return().
|
2003-03-09 21:46:47 +01:00
|
|
|
server_start(State, Mech, ClientIn) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:member(Mech,
|
|
|
|
listmech(State#sasl_state.myname))
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
case ets:lookup(sasl_mechanism, Mech) of
|
|
|
|
[#sasl_mechanism{module = Module}] ->
|
|
|
|
{ok, MechState} =
|
|
|
|
Module:mech_new(State#sasl_state.myname,
|
|
|
|
State#sasl_state.get_password,
|
|
|
|
State#sasl_state.check_password,
|
|
|
|
State#sasl_state.check_password_digest),
|
|
|
|
server_step(State#sasl_state{mech_mod = Module,
|
2016-12-31 11:47:35 +01:00
|
|
|
mech_name = Mech,
|
2013-03-14 10:33:02 +01:00
|
|
|
mech_state = MechState},
|
|
|
|
ClientIn);
|
2016-12-31 11:47:35 +01:00
|
|
|
_ -> {error, unsupported_mechanism, <<"">>}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
2016-12-31 11:47:35 +01:00
|
|
|
false -> {error, unsupported_mechanism, <<"">>}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2016-12-31 11:47:35 +01:00
|
|
|
-spec server_step(sasl_state(), binary()) -> sasl_return().
|
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
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Props} ->
|
|
|
|
case check_credentials(State, Props) of
|
|
|
|
ok -> {ok, Props};
|
2016-12-31 11:47:35 +01:00
|
|
|
{error, Error} -> {error, Error, <<"">>}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
{ok, Props, ServerOut} ->
|
|
|
|
case check_credentials(State, Props) of
|
|
|
|
ok -> {ok, Props, ServerOut};
|
2016-12-31 11:47:35 +01:00
|
|
|
{error, Error} -> {error, Error, <<"">>}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
{continue, ServerOut, NewMechState} ->
|
|
|
|
{continue, ServerOut, State#sasl_state{mech_state = NewMechState}};
|
|
|
|
{error, Error, Username} ->
|
|
|
|
{error, Error, Username};
|
|
|
|
{error, Error} ->
|
2016-12-31 11:47:35 +01:00
|
|
|
{error, Error, <<"">>}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2016-12-31 11:47:35 +01:00
|
|
|
-spec get_mech(sasl_state()) -> binary().
|
|
|
|
get_mech(#sasl_state{mech_name = Mech}) ->
|
|
|
|
Mech.
|