MySQLAuthenticationData.cs 7.1 KB

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