SQLiteAuthenticationData.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 Mono.Data.SqliteClient;
  34. namespace OpenSim.Data.SQLiteLegacy
  35. {
  36. public class SQLiteAuthenticationData : SQLiteFramework, IAuthenticationData
  37. {
  38. private string m_Realm;
  39. private List<string> m_ColumnNames;
  40. private int m_LastExpire;
  41. private string m_connectionString;
  42. protected static SqliteConnection m_Connection;
  43. private static bool m_initialized = false;
  44. public SQLiteAuthenticationData(string connectionString, string realm)
  45. : base(connectionString)
  46. {
  47. m_Realm = realm;
  48. m_connectionString = connectionString;
  49. if (!m_initialized)
  50. {
  51. m_Connection = new SqliteConnection(connectionString);
  52. m_Connection.Open();
  53. using (SqliteConnection dbcon = (SqliteConnection)((ICloneable)m_Connection).Clone())
  54. {
  55. dbcon.Open();
  56. Migration m = new Migration(dbcon, GetType().Assembly, "AuthStore");
  57. m.Update();
  58. dbcon.Close();
  59. }
  60. m_initialized = true;
  61. }
  62. }
  63. public AuthenticationData Get(UUID principalID)
  64. {
  65. AuthenticationData ret = new AuthenticationData();
  66. ret.Data = new Dictionary<string, object>();
  67. SqliteCommand cmd = new SqliteCommand("select * from `" + m_Realm + "` where UUID = :PrincipalID");
  68. cmd.Parameters.Add(new SqliteParameter(":PrincipalID", principalID.ToString()));
  69. IDataReader result = ExecuteReader(cmd, m_Connection);
  70. try
  71. {
  72. if (result.Read())
  73. {
  74. ret.PrincipalID = principalID;
  75. if (m_ColumnNames == null)
  76. {
  77. m_ColumnNames = new List<string>();
  78. DataTable schemaTable = result.GetSchemaTable();
  79. foreach (DataRow row in schemaTable.Rows)
  80. m_ColumnNames.Add(row["ColumnName"].ToString());
  81. }
  82. foreach (string s in m_ColumnNames)
  83. {
  84. if (s == "UUID")
  85. continue;
  86. ret.Data[s] = result[s].ToString();
  87. }
  88. return ret;
  89. }
  90. else
  91. {
  92. return null;
  93. }
  94. }
  95. catch
  96. {
  97. }
  98. finally
  99. {
  100. CloseCommand(cmd);
  101. }
  102. return null;
  103. }
  104. public bool Store(AuthenticationData data)
  105. {
  106. if (data.Data.ContainsKey("UUID"))
  107. data.Data.Remove("UUID");
  108. string[] fields = new List<string>(data.Data.Keys).ToArray();
  109. string[] values = new string[data.Data.Count];
  110. int i = 0;
  111. foreach (object o in data.Data.Values)
  112. values[i++] = o.ToString();
  113. SqliteCommand cmd = new SqliteCommand();
  114. if (Get(data.PrincipalID) != null)
  115. {
  116. string update = "update `" + m_Realm + "` set ";
  117. bool first = true;
  118. foreach (string field in fields)
  119. {
  120. if (!first)
  121. update += ", ";
  122. update += "`" + field + "` = :" + field;
  123. cmd.Parameters.Add(new SqliteParameter(":" + field, data.Data[field]));
  124. first = false;
  125. }
  126. update += " where UUID = :UUID";
  127. cmd.Parameters.Add(new SqliteParameter(":UUID", data.PrincipalID.ToString()));
  128. cmd.CommandText = update;
  129. try
  130. {
  131. if (ExecuteNonQuery(cmd, m_Connection) < 1)
  132. {
  133. CloseCommand(cmd);
  134. return false;
  135. }
  136. }
  137. catch (Exception e)
  138. {
  139. Console.WriteLine(e.ToString());
  140. CloseCommand(cmd);
  141. return false;
  142. }
  143. }
  144. else
  145. {
  146. string insert = "insert into `" + m_Realm + "` (`UUID`, `" +
  147. String.Join("`, `", fields) +
  148. "`) values (:UUID, :" + String.Join(", :", fields) + ")";
  149. cmd.Parameters.Add(new SqliteParameter(":UUID", data.PrincipalID.ToString()));
  150. foreach (string field in fields)
  151. cmd.Parameters.Add(new SqliteParameter(":" + field, data.Data[field]));
  152. cmd.CommandText = insert;
  153. try
  154. {
  155. if (ExecuteNonQuery(cmd, m_Connection) < 1)
  156. {
  157. CloseCommand(cmd);
  158. return false;
  159. }
  160. }
  161. catch (Exception e)
  162. {
  163. Console.WriteLine(e.ToString());
  164. CloseCommand(cmd);
  165. return false;
  166. }
  167. }
  168. CloseCommand(cmd);
  169. return true;
  170. }
  171. public bool SetDataItem(UUID principalID, string item, string value)
  172. {
  173. SqliteCommand cmd = new SqliteCommand("update `" + m_Realm +
  174. "` set `" + item + "` = " + value + " where UUID = '" + principalID.ToString() + "'");
  175. if (ExecuteNonQuery(cmd, m_Connection) > 0)
  176. return true;
  177. return false;
  178. }
  179. public bool SetToken(UUID principalID, string token, int lifetime)
  180. {
  181. if (System.Environment.TickCount - m_LastExpire > 30000)
  182. DoExpire();
  183. SqliteCommand cmd = new SqliteCommand("insert into tokens (UUID, token, validity) values ('" + principalID.ToString() +
  184. "', '" + token + "', datetime('now', 'localtime', '+" + lifetime.ToString() + " minutes'))");
  185. if (ExecuteNonQuery(cmd, m_Connection) > 0)
  186. {
  187. cmd.Dispose();
  188. return true;
  189. }
  190. cmd.Dispose();
  191. return false;
  192. }
  193. public bool CheckToken(UUID principalID, string token, int lifetime)
  194. {
  195. if (System.Environment.TickCount - m_LastExpire > 30000)
  196. DoExpire();
  197. SqliteCommand cmd = new SqliteCommand("update tokens set validity = datetime('now', 'localtime', '+" + lifetime.ToString() +
  198. " minutes') where UUID = '" + principalID.ToString() + "' and token = '" + token + "' and validity > datetime('now', 'localtime')");
  199. if (ExecuteNonQuery(cmd, m_Connection) > 0)
  200. {
  201. cmd.Dispose();
  202. return true;
  203. }
  204. cmd.Dispose();
  205. return false;
  206. }
  207. private void DoExpire()
  208. {
  209. SqliteCommand cmd = new SqliteCommand("delete from tokens where validity < datetime('now', 'localtime')");
  210. ExecuteNonQuery(cmd, m_Connection);
  211. cmd.Dispose();
  212. m_LastExpire = System.Environment.TickCount;
  213. }
  214. }
  215. }