MySQLAuthenticationData.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 MySql.Data.MySqlClient;
  34. namespace OpenSim.Data.MySQL
  35. {
  36. public class MySqlAuthenticationData : MySqlFramework, IAuthenticationData
  37. {
  38. private string m_Realm;
  39. private List<string> m_ColumnNames;
  40. private int m_LastExpire;
  41. // private string m_connectionString;
  42. public MySqlAuthenticationData(string connectionString, string realm)
  43. : base(connectionString)
  44. {
  45. m_Realm = realm;
  46. m_connectionString = connectionString;
  47. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  48. {
  49. dbcon.Open();
  50. Migration m = new Migration(dbcon, GetType().Assembly, "AuthStore");
  51. m.Update();
  52. }
  53. }
  54. public AuthenticationData Get(UUID principalID)
  55. {
  56. AuthenticationData ret = new AuthenticationData();
  57. ret.Data = new Dictionary<string, object>();
  58. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  59. {
  60. dbcon.Open();
  61. MySqlCommand cmd = new MySqlCommand("select * from `" + m_Realm + "` where UUID = ?principalID", dbcon);
  62. cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
  63. IDataReader result = cmd.ExecuteReader();
  64. if (result.Read())
  65. {
  66. ret.PrincipalID = principalID;
  67. if (m_ColumnNames == null)
  68. {
  69. m_ColumnNames = new List<string>();
  70. DataTable schemaTable = result.GetSchemaTable();
  71. foreach (DataRow row in schemaTable.Rows)
  72. m_ColumnNames.Add(row["ColumnName"].ToString());
  73. }
  74. foreach (string s in m_ColumnNames)
  75. {
  76. if (s == "UUID")
  77. continue;
  78. ret.Data[s] = result[s].ToString();
  79. }
  80. return ret;
  81. }
  82. else
  83. {
  84. return null;
  85. }
  86. }
  87. }
  88. public bool Store(AuthenticationData data)
  89. {
  90. if (data.Data.ContainsKey("UUID"))
  91. data.Data.Remove("UUID");
  92. string[] fields = new List<string>(data.Data.Keys).ToArray();
  93. MySqlCommand cmd = new MySqlCommand();
  94. string update = "update `"+m_Realm+"` set ";
  95. bool first = true;
  96. foreach (string field in fields)
  97. {
  98. if (!first)
  99. update += ", ";
  100. update += "`" + field + "` = ?"+field;
  101. first = false;
  102. cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
  103. }
  104. update += " where UUID = ?principalID";
  105. cmd.CommandText = update;
  106. cmd.Parameters.AddWithValue("?principalID", data.PrincipalID.ToString());
  107. if (ExecuteNonQuery(cmd) < 1)
  108. {
  109. string insert = "insert into `" + m_Realm + "` (`UUID`, `" +
  110. String.Join("`, `", fields) +
  111. "`) values (?principalID, ?" + String.Join(", ?", fields) + ")";
  112. cmd.CommandText = insert;
  113. if (ExecuteNonQuery(cmd) < 1)
  114. {
  115. cmd.Dispose();
  116. return false;
  117. }
  118. }
  119. cmd.Dispose();
  120. return true;
  121. }
  122. public bool SetDataItem(UUID principalID, string item, string value)
  123. {
  124. MySqlCommand cmd = new MySqlCommand("update `" + m_Realm +
  125. "` set `" + item + "` = ?" + item + " where UUID = ?UUID");
  126. cmd.Parameters.AddWithValue("?"+item, value);
  127. cmd.Parameters.AddWithValue("?UUID", principalID.ToString());
  128. if (ExecuteNonQuery(cmd) > 0)
  129. return true;
  130. return false;
  131. }
  132. public bool SetToken(UUID principalID, string token, int lifetime)
  133. {
  134. if (System.Environment.TickCount - m_LastExpire > 30000)
  135. DoExpire();
  136. MySqlCommand cmd = new MySqlCommand("insert into tokens (UUID, token, validity) values (?principalID, ?token, date_add(now(), interval ?lifetime minute))");
  137. cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
  138. cmd.Parameters.AddWithValue("?token", token);
  139. cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
  140. if (ExecuteNonQuery(cmd) > 0)
  141. {
  142. cmd.Dispose();
  143. return true;
  144. }
  145. cmd.Dispose();
  146. return false;
  147. }
  148. public bool CheckToken(UUID principalID, string token, int lifetime)
  149. {
  150. if (System.Environment.TickCount - m_LastExpire > 30000)
  151. DoExpire();
  152. MySqlCommand cmd = new MySqlCommand("update tokens set validity = date_add(now(), interval ?lifetime minute) where UUID = ?principalID and token = ?token and validity > now()");
  153. cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
  154. cmd.Parameters.AddWithValue("?token", token);
  155. cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
  156. if (ExecuteNonQuery(cmd) > 0)
  157. {
  158. cmd.Dispose();
  159. return true;
  160. }
  161. cmd.Dispose();
  162. return false;
  163. }
  164. private void DoExpire()
  165. {
  166. MySqlCommand cmd = new MySqlCommand("delete from tokens where validity < now()");
  167. ExecuteNonQuery(cmd);
  168. cmd.Dispose();
  169. m_LastExpire = System.Environment.TickCount;
  170. }
  171. }
  172. }