MSSQLAuthenticationData.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ''AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using System.Data.SqlClient;
  34. using System.Reflection;
  35. using System.Text;
  36. namespace OpenSim.Data.MSSQL
  37. {
  38. public class MSSQLAuthenticationData : IAuthenticationData
  39. {
  40. private string m_Realm;
  41. private List<string> m_ColumnNames = null;
  42. private int m_LastExpire = 0;
  43. private string m_ConnectionString;
  44. private MSSQLManager m_database;
  45. public MSSQLAuthenticationData(string connectionString, string realm)
  46. {
  47. m_Realm = realm;
  48. m_ConnectionString = connectionString;
  49. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  50. {
  51. conn.Open();
  52. Migration m = new Migration(conn, GetType().Assembly, "AuthStore");
  53. m.Update();
  54. }
  55. }
  56. public AuthenticationData Get(UUID principalID)
  57. {
  58. AuthenticationData ret = new AuthenticationData();
  59. ret.Data = new Dictionary<string, object>();
  60. string sql = string.Format("select * from {0} where UUID = @principalID", m_Realm);
  61. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  62. using (SqlCommand cmd = new SqlCommand(sql, conn))
  63. {
  64. cmd.Parameters.Add(m_database.CreateParameter("@principalID", principalID));
  65. conn.Open();
  66. using (SqlDataReader result = cmd.ExecuteReader())
  67. {
  68. if (result.Read())
  69. {
  70. ret.PrincipalID = principalID;
  71. if (m_ColumnNames == null)
  72. {
  73. m_ColumnNames = new List<string>();
  74. DataTable schemaTable = result.GetSchemaTable();
  75. foreach (DataRow row in schemaTable.Rows)
  76. m_ColumnNames.Add(row["ColumnName"].ToString());
  77. }
  78. foreach (string s in m_ColumnNames)
  79. {
  80. if (s == "UUID")
  81. continue;
  82. ret.Data[s] = result[s].ToString();
  83. }
  84. return ret;
  85. }
  86. }
  87. }
  88. return null;
  89. }
  90. public bool Store(AuthenticationData data)
  91. {
  92. if (data.Data.ContainsKey("UUID"))
  93. data.Data.Remove("UUID");
  94. string[] fields = new List<string>(data.Data.Keys).ToArray();
  95. StringBuilder updateBuilder = new StringBuilder();
  96. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  97. using (SqlCommand cmd = new SqlCommand())
  98. {
  99. updateBuilder.AppendFormat("update {0} set ", m_Realm);
  100. bool first = true;
  101. foreach (string field in fields)
  102. {
  103. if (!first)
  104. updateBuilder.Append(", ");
  105. updateBuilder.AppendFormat("{0} = @{0}",field);
  106. first = false;
  107. cmd.Parameters.Add(m_database.CreateParameter("@" + field, data.Data[field]));
  108. }
  109. updateBuilder.Append(" where UUID = @principalID");
  110. cmd.CommandText = updateBuilder.ToString();
  111. cmd.Connection = conn;
  112. cmd.Parameters.Add(m_database.CreateParameter("@principalID", data.PrincipalID));
  113. conn.Open();
  114. if (cmd.ExecuteNonQuery() < 1)
  115. {
  116. StringBuilder insertBuilder = new StringBuilder();
  117. insertBuilder.AppendFormat("insert into {0} (UUID, ", m_Realm);
  118. insertBuilder.Append(String.Join(", ", fields));
  119. insertBuilder.Append(") values (@principalID, @");
  120. insertBuilder.Append(String.Join(", @", fields));
  121. insertBuilder.Append(")");
  122. cmd.CommandText = insertBuilder.ToString();
  123. if (cmd.ExecuteNonQuery() < 1)
  124. {
  125. return false;
  126. }
  127. }
  128. }
  129. return true;
  130. }
  131. public bool SetDataItem(UUID principalID, string item, string value)
  132. {
  133. string sql = string.Format("update {0} set {1} = @{1} where UUID = @UUID", m_Realm, item);
  134. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  135. using (SqlCommand cmd = new SqlCommand(sql, conn))
  136. {
  137. cmd.Parameters.Add(m_database.CreateParameter("@" + item, value));
  138. conn.Open();
  139. if (cmd.ExecuteNonQuery() > 0)
  140. return true;
  141. }
  142. return false;
  143. }
  144. public bool SetToken(UUID principalID, string token, int lifetime)
  145. {
  146. if (System.Environment.TickCount - m_LastExpire > 30000)
  147. DoExpire();
  148. string sql = "insert into tokens (UUID, token, validity) values (@principalID, @token, date_add(now(), interval @lifetime minute))";
  149. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  150. using (SqlCommand cmd = new SqlCommand(sql, conn))
  151. {
  152. cmd.Parameters.Add(m_database.CreateParameter("@principalID", principalID));
  153. cmd.Parameters.Add(m_database.CreateParameter("@token", token));
  154. cmd.Parameters.Add(m_database.CreateParameter("@lifetime", lifetime));
  155. conn.Open();
  156. if (cmd.ExecuteNonQuery() > 0)
  157. {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. public bool CheckToken(UUID principalID, string token, int lifetime)
  164. {
  165. if (System.Environment.TickCount - m_LastExpire > 30000)
  166. DoExpire();
  167. string sql = "update tokens set validity = date_add(now(), interval @lifetime minute) where UUID = @principalID and token = @token and validity > now()";
  168. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  169. using (SqlCommand cmd = new SqlCommand(sql, conn))
  170. {
  171. cmd.Parameters.Add(m_database.CreateParameter("@principalID", principalID));
  172. cmd.Parameters.Add(m_database.CreateParameter("@token", token));
  173. cmd.Parameters.Add(m_database.CreateParameter("@lifetime", lifetime));
  174. conn.Open();
  175. if (cmd.ExecuteNonQuery() > 0)
  176. {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. private void DoExpire()
  183. {
  184. string sql = "delete from tokens where validity < now()";
  185. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  186. using (SqlCommand cmd = new SqlCommand(sql, conn))
  187. {
  188. conn.Open();
  189. cmd.ExecuteNonQuery();
  190. }
  191. m_LastExpire = System.Environment.TickCount;
  192. }
  193. }
  194. }