SQLiteAuthenticationData.cs 8.9 KB

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