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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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_plain).
|
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).
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @type mechstate() = {state, CheckPassword}
|
|
|
|
%% CheckPassword = function().
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(state, {check_password}).
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (Opts) -> true
|
|
|
|
%% Opts = term()
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
start(_Opts) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
cyrsasl:register_mechanism("PLAIN", ?MODULE, false),
|
2003-03-09 21:46:47 +01:00
|
|
|
ok.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec () -> ok
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
stop() ->
|
|
|
|
ok.
|
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
%% @spec (Host, GetPassword, CheckPassword, CheckPasswordDigest) -> {ok, State}
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Host = string()
|
|
|
|
%% GetPassword = function()
|
|
|
|
%% CheckPassword = function()
|
|
|
|
%% State = mechstate()
|
|
|
|
|
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
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (State, ClientIn) -> Ok | Error
|
|
|
|
%% State = mechstate()
|
|
|
|
%% ClientIn = string()
|
|
|
|
%% Ok = {ok, Props}
|
|
|
|
%% Props = [Prop]
|
|
|
|
%% Prop = {username, Username} | {authzid, AuthzId} | {auth_module, AuthModule}
|
|
|
|
%% Username = string()
|
|
|
|
%% AuthzId = string()
|
|
|
|
%% AuthModule = atom()
|
|
|
|
%% Error = {error, Reason} | {error, Reason, Username}
|
|
|
|
%% Reason = term()
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
mech_step(State, ClientIn) ->
|
2009-12-29 19:45:02 +01:00
|
|
|
case prepare(ClientIn) of
|
2003-06-07 19:30:25 +02:00
|
|
|
[AuthzId, User, Password] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case (State#state.check_password)(User, Password) of
|
2008-04-22 19:41:30 +02:00
|
|
|
{true, AuthModule} ->
|
|
|
|
{ok, [{username, User}, {authzid, AuthzId},
|
|
|
|
{auth_module, AuthModule}]};
|
2003-03-09 21:46:47 +01:00
|
|
|
_ ->
|
2008-07-08 17:43:52 +02:00
|
|
|
{error, 'not-authorized', User}
|
2003-03-09 21:46:47 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2008-07-08 17:43:52 +02:00
|
|
|
{error, 'bad-protocol'}
|
2003-03-09 21:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2009-12-29 19:45:02 +01:00
|
|
|
prepare(ClientIn) ->
|
|
|
|
case parse(ClientIn) of
|
|
|
|
[[], UserMaybeDomain, Password] ->
|
|
|
|
case parse_domain(UserMaybeDomain) of
|
|
|
|
%% <NUL>login@domain<NUL>pwd
|
2009-12-30 00:13:15 +01:00
|
|
|
[User, _Domain] ->
|
2009-12-29 19:45:02 +01:00
|
|
|
[UserMaybeDomain, User, Password];
|
|
|
|
%% <NUL>login<NUL>pwd
|
|
|
|
[User] ->
|
|
|
|
["", User, Password]
|
|
|
|
end;
|
|
|
|
%% login@domain<NUL>login<NUL>pwd
|
|
|
|
[AuthzId, User, Password] ->
|
|
|
|
[AuthzId, User, Password];
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
parse(S) ->
|
|
|
|
parse1(S, "", []).
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
parse1([0 | Cs], S, T) ->
|
|
|
|
parse1(Cs, "", [lists:reverse(S) | T]);
|
|
|
|
parse1([C | Cs], S, T) ->
|
|
|
|
parse1(Cs, [C | S], T);
|
|
|
|
%parse1([], [], T) ->
|
|
|
|
% lists:reverse(T);
|
|
|
|
parse1([], S, T) ->
|
|
|
|
lists:reverse([lists:reverse(S) | T]).
|
|
|
|
|
|
|
|
|
2009-12-29 19:45:02 +01:00
|
|
|
parse_domain(S) ->
|
|
|
|
parse_domain1(S, "", []).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2009-12-29 19:45:02 +01:00
|
|
|
parse_domain1([$@ | Cs], S, T) ->
|
|
|
|
parse_domain1(Cs, "", [lists:reverse(S) | T]);
|
|
|
|
parse_domain1([C | Cs], S, T) ->
|
|
|
|
parse_domain1(Cs, [C | S], T);
|
|
|
|
parse_domain1([], S, T) ->
|
|
|
|
lists:reverse([lists:reverse(S) | T]).
|