1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : cyrsasl_plain.erl |
3 |
|
%%% Author : Alexey Shchepin <alexey@process-one.net> |
4 |
|
%%% Purpose : PLAIN SASL mechanism |
5 |
|
%%% Created : 8 Mar 2003 by Alexey Shchepin <alexey@process-one.net> |
6 |
|
%%% |
7 |
|
%%% |
8 |
|
%%% ejabberd, Copyright (C) 2002-2011 ProcessOne |
9 |
|
%%% |
10 |
|
%%% This program is free software; you can redistribute it and/or |
11 |
|
%%% modify it under the terms of the GNU General Public License as |
12 |
|
%%% published by the Free Software Foundation; either version 2 of the |
13 |
|
%%% License, or (at your option) any later version. |
14 |
|
%%% |
15 |
|
%%% This program is distributed in the hope that it will be useful, |
16 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
|
%%% General Public License for more details. |
19 |
|
%%% |
20 |
|
%%% You should have received a copy of the GNU General Public License |
21 |
|
%%% along with this program; if not, write to the Free Software |
22 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
|
%%% |
24 |
|
%%%---------------------------------------------------------------------- |
25 |
|
|
26 |
|
-module(cyrsasl_plain). |
27 |
|
-author('alexey@process-one.net'). |
28 |
|
|
29 |
|
-export([mechanism/0, mech_new/3, mech_step/2]). |
30 |
|
|
31 |
|
-ignore_xref([mech_new/3]). |
32 |
|
|
33 |
|
-behaviour(cyrsasl). |
34 |
|
|
35 |
|
-include("mongoose.hrl"). |
36 |
|
|
37 |
|
-spec mechanism() -> cyrsasl:mechanism(). |
38 |
|
mechanism() -> |
39 |
9986 |
<<"PLAIN">>. |
40 |
|
|
41 |
|
-spec mech_new(Host :: jid:server(), |
42 |
|
Creds :: mongoose_credentials:t(), |
43 |
|
Socket :: term()) -> {ok, tuple()}. |
44 |
|
mech_new(_Host, Creds, _Socket) -> |
45 |
3230 |
{ok, Creds}. |
46 |
|
|
47 |
|
-spec mech_step(Creds :: mongoose_credentials:t(), |
48 |
|
ClientIn :: binary()) -> {ok, mongoose_credentials:t()} |
49 |
|
| {error, binary()}. |
50 |
|
mech_step(Creds, ClientIn) -> |
51 |
3230 |
case prepare(ClientIn) of |
52 |
|
[AuthzId, User, Password] -> |
53 |
3230 |
Request = mongoose_credentials:extend(Creds, |
54 |
|
[{username, User}, |
55 |
|
{password, Password}, |
56 |
|
{authzid, AuthzId}]), |
57 |
3230 |
authorize(Request, User); |
58 |
|
_ -> |
59 |
:-( |
{error, <<"bad-protocol">>} |
60 |
|
end. |
61 |
|
|
62 |
|
authorize(Request, User) -> |
63 |
3230 |
case ejabberd_auth:authorize(Request) of |
64 |
|
{ok, Result} -> |
65 |
3214 |
{ok, Result}; |
66 |
|
{error, not_authorized} -> |
67 |
16 |
{error, <<"not-authorized">>, User} |
68 |
|
end. |
69 |
|
|
70 |
|
-spec prepare(binary()) -> 'error' | [binary(), ...]. |
71 |
|
prepare(ClientIn) -> |
72 |
3230 |
case parse(ClientIn) of |
73 |
|
[<<>>, UserMaybeDomain, Password] -> |
74 |
3230 |
case parse_domain(UserMaybeDomain) of |
75 |
|
%% <NUL>login@domain<NUL>pwd |
76 |
|
[User, _Domain] -> |
77 |
:-( |
[UserMaybeDomain, |
78 |
|
User, |
79 |
|
Password]; |
80 |
|
%% <NUL>login<NUL>pwd |
81 |
|
[User] -> |
82 |
3230 |
[<<>>, User, Password] |
83 |
|
end; |
84 |
|
%% login@domain<NUL>login<NUL>pwd |
85 |
|
[AuthzId, User, Password] -> |
86 |
:-( |
[AuthzId, User, Password]; |
87 |
|
_ -> |
88 |
:-( |
error |
89 |
|
end. |
90 |
|
|
91 |
|
|
92 |
|
-spec parse(binary()) -> [binary(), ...]. |
93 |
|
parse(S) -> |
94 |
3230 |
binary:split(S, <<0>>, [global, trim]). |
95 |
|
|
96 |
|
-spec parse_domain(binary()) -> [binary(), ...]. |
97 |
|
parse_domain(S) -> |
98 |
3230 |
binary:split(S, <<$@>>, [global, trim]). |