1
0

SQLiteAuthenticationData.cs 9.0 KB

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