Index: src/cyrsasl_digest.erl
===================================================================
--- src/cyrsasl_digest.erl	(revision 1259)
+++ src/cyrsasl_digest.erl	(working copy)
@@ -140,23 +140,60 @@
     DigestURI = xml:get_attr_s("digest-uri", KeyVals),
     NC = xml:get_attr_s("nc", KeyVals),
     QOP = xml:get_attr_s("qop", KeyVals),
-    A1 = case AuthzId of
-	     "" ->
-		 binary_to_list(
-		   crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd)) ++
-		     ":" ++ Nonce ++ ":" ++ CNonce;
-	     _ ->
-		 binary_to_list(
-		   crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd)) ++
-		     ":" ++ Nonce ++ ":" ++ CNonce ++ ":" ++ AuthzId
-	 end,
-    A2 = case QOP of
-	     "auth" ->
-		 A2Prefix ++ ":" ++ DigestURI;
-	     _ ->
-		 A2Prefix ++ ":" ++ DigestURI ++
-		     ":00000000000000000000000000000000"
-	 end,
+    ?INFO_MSG("Realm ~p", [Realm]),
+    if
+  	Realm == [] ->
+	    A1 = case AuthzId of
+		     "" ->
+			 binary_to_list(
+			   crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd)) ++
+			     ":" ++ Nonce ++ ":" ++ CNonce;
+		     _ ->
+			 binary_to_list(
+			   crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd)) ++
+			     ":" ++ Nonce ++ ":" ++ CNonce ++ ":" ++ AuthzId
+		 end,
+	    A2 = case QOP of
+		     "auth" ->
+			 A2Prefix ++ ":" ++ DigestURI;
+		     _ ->
+			 A2Prefix ++ ":" ++ DigestURI ++
+			     ":00000000000000000000000000000000"
+		 end;
+	true ->
+	    {selected, ["ha1"], [{Ha1Text}]} = odbc_queries:get_ha("example.com", User),
+	    Ha1 = get_ha1_list(Ha1Text),
+		?INFO_MSG("Ha1Db [~p] Ha1Orig [~p]", [Ha1, binary_to_list(crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd))]),
+	    if
+	    	Ha1 /= null ->
+		    A1 = case AuthzId of
+			     "" ->
+				 Ha1 ++ ":" ++ Nonce ++ ":" ++ CNonce;
+			     _ ->
+				 Ha1 ++ ":" ++ Nonce ++ ":" ++ CNonce ++ ":" ++ AuthzId
+			 end,
+		    A2 = case QOP of
+			     "auth" ->
+				 A2Prefix ++ ":" ++ DigestURI;
+			     _ ->
+				 A2Prefix ++ ":" ++ DigestURI ++ ":00000000000000000000000000000000"
+			 end,
+			?INFO_MSG("Ha1Db [~p] Ha1Orig[~p]", [Ha1, binary_to_list(crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd))]);
+		true ->
+		    A1 = case AuthzId of
+			     "" ->
+				 binary_to_list(crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd)) ++ ":" ++ Nonce ++ ":" ++ CNonce;
+			     _ ->
+				 binary_to_list(crypto:md5(User ++ ":" ++ Realm ++ ":" ++ Passwd)) ++ ":" ++ Nonce ++ ":" ++ CNonce ++ ":" ++ AuthzId
+			 end,
+		    A2 = case QOP of
+			     "auth" ->
+				 A2Prefix ++ ":" ++ DigestURI;
+			     _ ->
+				 A2Prefix ++ ":" ++ DigestURI ++ ":00000000000000000000000000000000"
+			 end
+		end
+	end,
     T = hex(binary_to_list(crypto:md5(A1))) ++ ":" ++ Nonce ++ ":" ++
 	NC ++ ":" ++ CNonce ++ ":" ++ QOP ++ ":" ++
 	hex(binary_to_list(crypto:md5(A2))),
@@ -164,3 +201,13 @@
 
 
 
+get_ha1_list(null) ->
+	null;
+
+get_ha1_list([]) ->
+	[];
+
+get_ha1_list(Ha1Text) ->
+	[Hi,Lo|Rest]= Ha1Text,
+	{ok, [Num], _ } = io_lib:fread("~16u", binary_to_list(<<Hi, Lo>>)),
+	[Num] ++ get_ha1_list(Rest).
Index: src/ejabberd_c2s.erl
===================================================================
--- src/ejabberd_c2s.erl	(revision 1259)
+++ src/ejabberd_c2s.erl	(working copy)
@@ -235,7 +235,7 @@
 				false ->
 				    SASLState =
 					cyrsasl:server_new(
-					  "jabber", Server, "", [],
+					  "jabber", Server,"myrealm", [],
 					  fun(U) ->
 						  ejabberd_auth:get_password(
 						    U, Server)
Index: src/odbc/odbc_queries.erl
===================================================================
--- src/odbc/odbc_queries.erl	(revision 1259)
+++ src/odbc/odbc_queries.erl	(working copy)
@@ -33,6 +33,7 @@
 	 set_last_t/4,
 	 del_last/2,
 	 get_password/2,
+	 get_ha/2,
 	 set_password_t/3,
 	 add_user/3,
 	 del_user/2,
@@ -110,6 +111,12 @@
       ["select password from users "
        "where username='", Username, "';"]).
 
+get_ha(LServer, Username) ->
+    ejabberd_odbc:sql_query(
+      LServer,
+      ["select ha1 from users "
+       "where username='", Username, "';"]).
+
 set_password_t(LServer, Username, Pass) ->
     ejabberd_odbc:sql_transaction(
       LServer,
