Index: ssh_proto.erl
===================================================================
RCS file: /REPOSITORY/prod/lib/ssh/src/ssh_proto.erl,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -5 -r1.7 -r1.8
--- ssh_proto.erl	25 Oct 2005 22:52:34 -0000	1.7
+++ ssh_proto.erl	18 Dec 2007 08:06:03 -0000	1.8
@@ -3,13 +3,13 @@
 %%% Description : SSH Protocol for erlang
 %%% Created : 16 Aug 2004 by Tony Rogvall <tony@bix.hemma.se>
 
 -module(ssh_proto).
 
--vsn("$Revision: 1.7 $ ").
+-vsn("$Revision: 1.8 $ ").
 
--rcsid("$Id: ssh_proto.erl,v 1.7 2005/10/25 22:52:34 tony Exp $\n").
+-rcsid("$Id: ssh_proto.erl,v 1.8 2007/12/18 08:06:03 tony Exp $\n").
 
 -compile(export_all).
 
 -import(lists, [reverse/1, map/2, foreach/2, foldl/3, member/2]).
 
@@ -505,15 +505,13 @@
     {ok, {RecvPdu,RecvAlg}} = recv_algorithms(S, SSH),
     kex_negotiate(S, User, SSH, UserAck, SendAlg, SendPdu, RecvAlg, RecvPdu).
 
 
 %% Other side started re-negotiate
-recv_negotiate(S, User, SSH, RecvAlg, UserAck) ->
-    RecvPdu = ssh_bits:encode(RecvAlg),
+recv_negotiate(S, User, SSH, RecvAlg, RecvPdu, UserAck) ->
     SendAlg = kex_init(SSH),
     {ok, SendPdu} = send_algorithms(S, SSH, SendAlg),
-    send_msg(S, SSH, SendAlg),
     kex_negotiate(S, User, SSH, UserAck, SendAlg, SendPdu, RecvAlg, RecvPdu).
 
 %% Select algorithms
 kex_negotiate(S, User, SSH, UserAck, SendAlg, SendPdu, RecvAlg, RecvPdu) ->
     case SSH#ssh.role of
@@ -566,11 +564,14 @@
 	    {error, bad_message};
 	Error ->
 	    Error
     end.
 
-
+%% select session id
+sid(SSH, H) when SSH#ssh.session_id == undefined -> H;
+sid(SSH, _) -> SSH#ssh.session_id.
+    
 
 client_kex(S, SSH, 'diffie-hellman-group1-sha1') ->
     ssh_bits:install_messages(kexdh_messages()),
     {G,P} = dh_group1(),
     {Private, Public} = dh_gen_key(G,P,1024),
@@ -587,11 +588,11 @@
 	    ?DBG(SSH, ?DBG_KEX, "hash: ~s\n", [fmt_binary(H, 16, 4)]),
 	    case verify_host_key(S, SSH, K_S, H, H_SIG) of
 		ok ->
 		    {ok, SSH#ssh { shared_secret  = K,
 				   exchanged_hash = H,
-				   session_id = H }};
+				   session_id = sid(SSH, H) }};
 		Error ->
 		    Error
 	    end;
 	{ok,_} ->
 	    {error, bad_message};
@@ -626,11 +627,11 @@
 			 [fmt_binary(H, 16, 4)]),
 		    case verify_host_key(S, SSH, K_S, H, H_SIG) of
 			ok ->
 			    {ok,  SSH#ssh { shared_secret  = K,
 					    exchanged_hash = H,
-					    session_id = H }};
+					    session_id = sid(SSH, H) }};
 			Error ->
 			    Error
 		    end;
 		{ok,_} ->
 		    {error, bad_message};
@@ -665,19 +666,19 @@
 					   }),
 	    ?DBG(SSH,?DBG_KEX, "shared_secret: ~s\n", [fmt_binary(K, 16, 4)]),
 	    ?DBG(SSH,?DBG_KEX, "hash: ~s\n", [fmt_binary(H, 16, 4)]),
 	    {ok, SSH#ssh { shared_secret = K,
 			   exchanged_hash = H,
-			   session_id = H }};
+			   session_id = sid(SSH, H) }};
 	{ok,_} ->
 	    {error, bad_message};
 	Error ->
 	    Error
     end;
 server_kex(S, SSH, 'diffie-hellman-group-exchange-sha1') ->
     ssh_bits:install_messages(kex_dh_gex_messages()),
-    R0 = recv_msg(S, SSH),
+    {ok,R0} = recv_msg(S, SSH),
     #ssh_msg_kex_dh_gex_request { min = Min,
 				  n   = NBits,
 				  max = Max } = R0,
     {G,P} = dh_group1(), %% FIX ME!!!
     send_msg(S, SSH, #ssh_msg_kex_dh_gex_group { p = P, g = G }),
@@ -697,11 +698,11 @@
 						}),
 	    ?DBG(SSH,?DBG_KEX, "shared_secret: ~s\n", [fmt_binary(K, 16, 4)]),
 	    ?DBG(SSH,?DBG_KEX, "hash: ~s\n", [fmt_binary(H, 16, 4)]),
 	    {ok, SSH#ssh { shared_secret = K,
 			   exchanged_hash = H,
-			   session_id = H }};
+			   session_id = sid(SSH, H) }};
 	{ok,_} ->
 	    {error, bad_message};
 	Error ->
 	    Error
     end;
@@ -728,11 +729,11 @@
 			 [M#ssh_msg_disconnect.code,
 			  M#ssh_msg_disconnect.description]),
 		    gen_tcp:close(S);
 
 		{ok,M} when record(M, ssh_msg_kexinit) ->
-		    recv_negotiate(S, User, SSH, M, false);
+		    recv_negotiate(S, User, SSH, M, get(last_packet), false);
 
 		{ok,M} ->
 		    User ! {ssh_msg, self(), M},
 		    inet:setopts(S, [{active, once}]),
 		    ssh_main(S, User, SSH);
@@ -1233,10 +1234,11 @@
 		    recv_msg(S, SSH, Tmo);
 		{ok, M} when record(M, ssh_msg_ignore) ->
 		    recv_msg(S, SSH, Tmo);
 
 		{ok, Msg} ->
+		    put(last_packet, Packet),
 		    ?DBG(SSH, ?DBG_MESSAGE, "RECV_MSG: ~70p\n", [Msg]),
 		    {ok, Msg};
 		Error ->
 		    ?DBG(SSH, ?DBG_MESSAGE, "RECV_MSG: ~70p\n", [Error]),
 		    Error
