2003-03-09 21:46:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : cyrsasl_plain.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 : PLAIN SASL mechanism
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 8 Mar 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 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_plain).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2016-12-31 11:47:35 +01:00
|
|
|
-export([start/1, stop/0, mech_new/4, mech_step/2, parse/1, format_error/1]).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
-behaviour(cyrsasl).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(state, {check_password}).
|
2016-12-31 11:47:35 +01:00
|
|
|
-type error_reason() :: parser_failed | not_authorized.
|
|
|
|
-export_type([error_reason/0]).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
start(_Opts) ->
|
2017-01-30 14:29:53 +01:00
|
|
|
cyrsasl:register_mechanism(<<"PLAIN">>, ?MODULE, plain).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
stop() -> ok.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2016-12-31 11:47:35 +01:00
|
|
|
-spec format_error(error_reason()) -> {atom(), binary()}.
|
|
|
|
format_error(parser_failed) ->
|
|
|
|
{'bad-protocol', <<"Response decoding failed">>};
|
|
|
|
format_error(not_authorized) ->
|
|
|
|
{'not-authorized', <<"Invalid username or password">>}.
|
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
mech_new(_Host, _GetPassword, CheckPassword, _CheckPasswordDigest) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
{ok, #state{check_password = CheckPassword}}.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
mech_step(State, ClientIn) ->
|
2009-12-29 19:44:17 +01:00
|
|
|
case prepare(ClientIn) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[AuthzId, User, Password] ->
|
2015-04-09 03:21:09 +02:00
|
|
|
case (State#state.check_password)(User, AuthzId, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{true, AuthModule} ->
|
|
|
|
{ok,
|
|
|
|
[{username, User}, {authzid, AuthzId},
|
|
|
|
{auth_module, AuthModule}]};
|
2016-12-31 11:47:35 +01:00
|
|
|
_ -> {error, not_authorized, User}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
2016-12-31 11:47:35 +01:00
|
|
|
_ -> {error, parser_failed}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2009-12-29 19:44:17 +01:00
|
|
|
prepare(ClientIn) ->
|
|
|
|
case parse(ClientIn) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[<<"">>, UserMaybeDomain, Password] ->
|
|
|
|
case parse_domain(UserMaybeDomain) of
|
|
|
|
%% <NUL>login@domain<NUL>pwd
|
2015-04-09 03:21:09 +02:00
|
|
|
[User, _Domain] -> [User, User, Password];
|
2013-03-14 10:33:02 +01:00
|
|
|
%% <NUL>login<NUL>pwd
|
2015-04-09 03:21:09 +02:00
|
|
|
[User] -> [User, User, Password]
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
2015-04-09 03:21:09 +02:00
|
|
|
[AuthzId, User, Password] ->
|
|
|
|
case parse_domain(AuthzId) of
|
2013-03-14 10:33:02 +01:00
|
|
|
%% login@domain<NUL>login<NUL>pwd
|
2015-04-09 03:21:09 +02:00
|
|
|
[AuthzUser, _Domain] -> [AuthzUser, User, Password];
|
|
|
|
%% login<NUL>login<NUL>pwd
|
|
|
|
[AuthzUser] -> [AuthzUser, User, Password]
|
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> error
|
2009-12-29 19:44:17 +01:00
|
|
|
end.
|
|
|
|
|
2018-02-26 09:36:56 +01:00
|
|
|
parse(S) ->
|
|
|
|
binary:split(S, <<0>>, [global]).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
parse_domain(S) -> parse_domain1(binary_to_list(S), "", []).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2009-12-29 19:44:17 +01:00
|
|
|
parse_domain1([$@ | Cs], S, T) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
parse_domain1(Cs, "", [list_to_binary(lists:reverse(S)) | T]);
|
2009-12-29 19:44:17 +01:00
|
|
|
parse_domain1([C | Cs], S, T) ->
|
|
|
|
parse_domain1(Cs, [C | S], T);
|
|
|
|
parse_domain1([], S, T) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:reverse([list_to_binary(lists:reverse(S)) | T]).
|