MySQLAuthenticationData.cs 8.2 KB

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