1 %%%----------------------------------------------------------------------
2 %%% File    : cyrsasl_anonymous.erl
3 %%% Author  : Magnus Henoch <henoch@dtek.chalmers.se>
4 %%% Purpose : ANONYMOUS SASL mechanism
5 %%%  See http://www.ietf.org/internet-drafts/draft-ietf-sasl-anon-05.txt
6 %%% Created : 23 Aug 2005 by Magnus Henoch <henoch@dtek.chalmers.se>
7 %%%
8 %%%
9 %%% ejabberd, Copyright (C) 2002-2012   ProcessOne
10 %%%
11 %%% This program is free software; you can redistribute it and/or
12 %%% modify it under the terms of the GNU General Public License as
13 %%% published by the Free Software Foundation; either version 2 of the
14 %%% License, or (at your option) any later version.
15 %%%
16 %%% This program is distributed in the hope that it will be useful,
17 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
18 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 %%% General Public License for more details.
20 %%%
21 %%% You should have received a copy of the GNU General Public License
22 %%% along with this program; if not, write to the Free Software
23 %%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 %%% 02111-1307 USA
25 %%%
26 %%%----------------------------------------------------------------------
27 
28 
29 -module(cyrsasl_anonymous).
30 
31 -export([start/1, stop/0, mech_new/1, mech_step/2]).
32 
33 -include("cyrsasl.hrl").
34 
35 -behaviour(cyrsasl).
36 
37 %% @type mechstate() = {state, Server}
38 %%     Server = string().
39 
40 
41 -record(state, {server}).
42 
43 %% @spec (Opts) -> true
44 %%     Opts = term()
45 
46 
47 start(_Opts) -> cyrsasl:register_mechanism("ANONYMOUS", ?MODULE, plain), ok.
48 
49 %% @spec () -> ok
50 
51 
52 stop() -> ok.
53 
54 mech_new(#sasl_params{host = Host}) -> {ok, #state{server = Host}}.
55 
56 %% @spec (State, ClientIn) -> Ok | Error
57 %%     State = mechstate()
58 %%     ClientIn = string()
59 %%     Ok = {ok, Props}
60 %%         Props = [Prop]
61 %%         Prop = {username, Username} | {auth_module, AuthModule}
62 %%         Username = string()
63 %%         AuthModule = ejabberd_auth_anonymous
64 %%     Error = {error, 'not-authorized'}
65 
66 
67 mech_step(State, _ClientIn) ->
68         %% We generate a random username:
69     User = lists:concat([randoms:get_string() | tuple_to_list(now())]),
70     Server = State#state.server,
71         %% Checks that the username is available
72     case ejabberd_auth:is_user_exists(User, Server) of
73       true -> {error, 'not-authorized'};
74       false -> {ok, [{username, User}, {auth_module, ejabberd_auth_anonymous}]}
75     end.