Permalink
Browse files

Added bind mod. Added reset stream during stream setup (tls->sasl->bind)

  • Loading branch information...
1 parent 5c08536 commit bd62d3c1284257808fc02d6e6be48db70fe37328 @kaos committed Apr 24, 2009
View
@@ -5,7 +5,7 @@
{ modules, [
yaxs, yaxs_app, yaxs_sup, yaxs_con, yaxs_client,
yaxs_core_stream, yaxs_core_tls, yaxs_core_sasl,
- yaxs_core_sasl_digest_md5
+ yaxs_core_sasl_digest_md5, yaxs_core_bind
]},
{ registered, [
yaxs_sup, yaxs_con, yaxs_client_sup,
@@ -21,7 +21,8 @@
yaxs_core_stream,
yaxs_core_tls,
yaxs_core_sasl,
- yaxs_core_sasl_digest_md5
+ yaxs_core_sasl_digest_md5,
+ yaxs_core_bind
]}
]}
]
@@ -0,0 +1,42 @@
+%%%-------------------------------------------------------------------
+%%% File : yaxs_core_stream.erl
+%%% Author : Andreas Stenius <kaos@astekk.se>
+%%% Description :
+%%%
+%%% Created : 22 Apr 2009 by Andreas Stenius <kaos@astekk.se>
+%%%-------------------------------------------------------------------
+-module(yaxs_core_bind).
+
+-include("yaxs.hrl").
+
+%% API
+-behaviour(yaxs_mod).
+-export([
+ init/0,
+ handle/2
+]).
+
+%%====================================================================
+%% API
+%%====================================================================
+%%--------------------------------------------------------------------
+%% Function:
+%% Description:
+%%--------------------------------------------------------------------
+
+init() ->
+ yaxs_mod:register(?MODULE, [stream_features]).
+
+handle(stream_features,
+ #yaxs_client{ response=R, tags=Tags } = _Client) ->
+ case proplists:get_value(sasl, Tags) of
+ ok ->
+ R("<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>");
+ _ ->
+ ok
+ end.
+
+
+%%====================================================================
+%% Internal functions
+%%====================================================================
@@ -30,11 +30,17 @@ init() ->
"urn:ietf:params:xml:ns:xmpp-sasl"
]).
-handle(stream_features, #yaxs_client{ response=R } = Client) ->
- R("<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"),
- Tags = yaxs_event:publish(sasl_mechanisms, Client),
- R("</mechanisms>"),
- {tag, Tags};
+handle(stream_features,
+ #yaxs_client{ response=R, tags=Tags } = Client) ->
+ case proplists:get_value(sasl, Tags) of
+ ok ->
+ ok;
+ _ ->
+ R("<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"),
+ NewTags = yaxs_event:publish(sasl_mechanisms, Client),
+ R("</mechanisms>"),
+ {tag, NewTags}
+ end;
handle(#tag{ tag = {"urn:ietf:params:xml:ns:xmpp-sasl",
"", "auth", Attrs} } = Event, Client) ->
@@ -34,7 +34,9 @@ handle(sasl_mechanisms, #yaxs_client{ response=R } = _Client) ->
R("<mechanism>DIGEST-MD5</mechanism>");
handle({'SASL/DIGEST-MD5', _Tag},
#yaxs_client{ response=R } = _Client) ->
- R("challenge").
+ R("challenge"),
+ R(reset_stream),
+ {tag, {sasl, ok}}.
%%====================================================================
@@ -25,12 +25,27 @@
%%--------------------------------------------------------------------
init() ->
- yaxs_mod:register(?MODULE, [stream_features]).
+ yaxs_mod:register(?MODULE, [
+ stream_features,
+ "urn:ietf:params:xml:ns:xmpp-tls"
+ ]).
-handle(stream_features, #yaxs_client{ response=R } = _Client) ->
- R("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>"),
- R("<required/>"),
- R("</starttls>").
+handle(stream_features,
+ #yaxs_client{ response=R, tags=Tags } = _Client) ->
+ case proplists:get_value(tls, Tags) of
+ ok ->
+ ok;
+ _ ->
+ R("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>"),
+ R("<required/>"),
+ R("</starttls>")
+ end;
+
+handle(#tag{ tag={"urn:ietf:params:xml:ns:xmpp-tls", "", "starttls", _} },
+ #yaxs_client{ response=R } = _Client) ->
+ R("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"),
+ R(reset_stream),
+ {tag, {tls, ok}}.
%%====================================================================
@@ -177,37 +177,40 @@ test_server_socket(_Config) ->
test_stream(_Config) ->
{ok, Sock} = gen_tcp:connect("localhost", 5222, [list, {active, false}]),
- ok = gen_tcp:send(Sock, "
-<?xml version='1.0'?>
- <stream:stream
- to='example.com'
- xmlns='jabber:client'
- xmlns:stream='http://etherx.jabber.org/streams'
- version='1.0'>
-"),
+ open_stream(Sock, "initial"),
-%% Expect = "\
-%% <?xml version='1.0'?>\
-%% <stream:stream\
-%% from='example.com'\
-%% id='someid'\
-%% xmlns='jabber:client'\
-%% xmlns:stream='http://etherx.jabber.org/streams'\
-%% version='1.0'>",
+ ok = gen_tcp:send(Sock, "
+<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
+ ),
+ {ok, Tls} = gen_tcp:recv(Sock, 0, 1000),
+ ct:pal("TLS Response:~n~s~n", [Tls]),
- {ok, Data} = gen_tcp:recv(Sock, 0, 1000),
- ct:pal("Response:~n~s~n", [Data]),
-%% case Data of
-%% Expect -> ok;
-%% Else -> ct:fail("Expected~n~p~n~nGot~n~p~n", [Expect, Data])
-%% end,
+ open_stream(Sock, "secured (fake)"),
ok = gen_tcp:send(Sock, "
<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>"
),
-
{ok, Auth} = gen_tcp:recv(Sock, 0, 1000),
ct:pal("Auth Response:~n~s~n", [Auth]),
+ open_stream(Sock, "authenticated (fake)"),
+
{error, timeout} = gen_tcp:recv(Sock, 0, 500),
ok = gen_tcp:close(Sock).
+
+
+
+
+%% Utils
+
+open_stream(Sock, Comment) ->
+ ok = gen_tcp:send(Sock, "
+<?xml version='1.0'?>
+ <stream:stream
+ to='example.com'
+ xmlns='jabber:client'
+ xmlns:stream='http://etherx.jabber.org/streams'
+ version='1.0'>
+"),
+ {ok, Data} = gen_tcp:recv(Sock, 0, 1000),
+ ct:pal("Response{~s}:~n~s~n", [Comment, Data]).
View
@@ -77,7 +77,8 @@ init([]) ->
#state{
client=#yaxs_client{
pid=Pid,
- response=fun(Data) -> gen_fsm:send_all_state_event(Pid, {response, Data}) end
+ response=fun(reset_stream) -> gen_fsm:send_event(Pid, reset_stream);
+ (Data) -> gen_fsm:send_all_state_event(Pid, {response, Data}) end
}
}
}.
@@ -118,6 +119,9 @@ wait_for_stream({sax, {open, {"http://etherx.jabber.org/streams",
wait_for_stream({sax, _Event}, State) ->
{next_state, wait_for_stream, State}.
+setup_stream(reset_stream, State) ->
+ {next_state, wait_for_stream, State#state{ sax=undefined, open_tags=[] }};
+
setup_stream({sax, {open, Tag}}, #state{ open_tags = Tags } = State) ->
{next_state, setup_stream,
State#state{ open_tags = [#tag{ tag=Tag }|Tags] }

0 comments on commit bd62d3c

Please sign in to comment.