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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
-export([start/1, stop/0, mech_new/4, mech_step/2, parse/1]).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
-behaviour(cyrsasl).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(state, {check_password}).
|
|
|
|
|
|
|
|
start(_Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
cyrsasl:register_mechanism(<<"PLAIN">>, ?MODULE, plain),
|
2003-03-09 21:46:47 +01:00
|
|
|
ok.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
stop() -> ok.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
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] ->
|
|
|
|
case (State#state.check_password)(User, Password) of
|
|
|
|
{true, AuthModule} ->
|
|
|
|
{ok,
|
|
|
|
[{username, User}, {authzid, AuthzId},
|
|
|
|
{auth_module, AuthModule}]};
|
|
|
|
_ -> {error, <<"not-authorized">>, User}
|
|
|
|
end;
|
|
|
|
_ -> {error, <<"bad-protocol">>}
|
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
|
|
|
|
[User, _Domain] -> [UserMaybeDomain, User, Password];
|
|
|
|
%% <NUL>login<NUL>pwd
|
|
|
|
[User] -> [<<"">>, User, Password]
|
|
|
|
end;
|
|
|
|
%% login@domain<NUL>login<NUL>pwd
|
|
|
|
[AuthzId, User, Password] -> [AuthzId, User, Password];
|
|
|
|
_ -> error
|
2009-12-29 19:44:17 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
parse(S) -> parse1(binary_to_list(S), "", []).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
parse1([0 | Cs], S, T) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
parse1(Cs, "", [list_to_binary(lists:reverse(S)) | T]);
|
|
|
|
parse1([C | Cs], S, T) -> parse1(Cs, [C | S], T);
|
2003-03-09 21:46:47 +01:00
|
|
|
%parse1([], [], T) ->
|
|
|
|
% lists:reverse(T);
|
|
|
|
parse1([], S, T) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:reverse([list_to_binary(lists:reverse(S)) | T]).
|
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]).
|