SQLiteGridData.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Security.Cryptography;
  32. using System.Text;
  33. using libsecondlife;
  34. namespace OpenSim.Framework.Data.SQLite
  35. {
  36. /// <summary>
  37. /// A Grid Interface to the SQLite database
  38. /// </summary>
  39. public class SQLiteGridData : IGridData
  40. {
  41. /// <summary>
  42. /// A database manager
  43. /// </summary>
  44. private SQLiteManager database;
  45. /// <summary>
  46. /// Initialises the Grid Interface
  47. /// </summary>
  48. public void Initialise()
  49. {
  50. database = new SQLiteManager("localhost", "db", "user", "password", "false");
  51. }
  52. /// <summary>
  53. /// Shuts down the grid interface
  54. /// </summary>
  55. public void Close()
  56. {
  57. database.Close();
  58. }
  59. /// <summary>
  60. /// Returns the name of this grid interface
  61. /// </summary>
  62. /// <returns>A string containing the grid interface</returns>
  63. public string getName()
  64. {
  65. return "SQLite OpenGridData";
  66. }
  67. /// <summary>
  68. /// Returns the version of this grid interface
  69. /// </summary>
  70. /// <returns>A string containing the version</returns>
  71. public string getVersion()
  72. {
  73. return "0.1";
  74. }
  75. /// <summary>
  76. /// Returns a list of regions within the specified ranges
  77. /// </summary>
  78. /// <param name="a">minimum X coordinate</param>
  79. /// <param name="b">minimum Y coordinate</param>
  80. /// <param name="c">maximum X coordinate</param>
  81. /// <param name="d">maximum Y coordinate</param>
  82. /// <returns>An array of region profiles</returns>
  83. public SimProfileData[] GetProfilesInRange(uint a, uint b, uint c, uint d)
  84. {
  85. return null;
  86. }
  87. /// <summary>
  88. /// Returns a sim profile from it's location
  89. /// </summary>
  90. /// <param name="handle">Region location handle</param>
  91. /// <returns>Sim profile</returns>
  92. public SimProfileData GetProfileByHandle(ulong handle)
  93. {
  94. Dictionary<string, string> param = new Dictionary<string, string>();
  95. param["handle"] = handle.ToString();
  96. IDbCommand result = database.Query("SELECT * FROM regions WHERE handle = @handle", param);
  97. IDataReader reader = result.ExecuteReader();
  98. SimProfileData row = database.getRow(reader);
  99. reader.Close();
  100. result.Dispose();
  101. return row;
  102. }
  103. /// <summary>
  104. /// Returns a sim profile from it's UUID
  105. /// </summary>
  106. /// <param name="uuid">The region UUID</param>
  107. /// <returns>The sim profile</returns>
  108. public SimProfileData GetProfileByLLUUID(LLUUID uuid)
  109. {
  110. Dictionary<string, string> param = new Dictionary<string, string>();
  111. param["uuid"] = uuid.ToStringHyphenated();
  112. IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = @uuid", param);
  113. IDataReader reader = result.ExecuteReader();
  114. SimProfileData row = database.getRow(reader);
  115. reader.Close();
  116. result.Dispose();
  117. return row;
  118. }
  119. /// <summary>
  120. /// Adds a new specified region to the database
  121. /// </summary>
  122. /// <param name="profile">The profile to add</param>
  123. /// <returns>A dataresponse enum indicating success</returns>
  124. public DataResponse AddProfile(SimProfileData profile)
  125. {
  126. if (database.insertRow(profile))
  127. {
  128. return DataResponse.RESPONSE_OK;
  129. }
  130. else
  131. {
  132. return DataResponse.RESPONSE_ERROR;
  133. }
  134. }
  135. /// <summary>
  136. /// DEPRECIATED. Attempts to authenticate a region by comparing a shared secret.
  137. /// </summary>
  138. /// <param name="uuid">The UUID of the challenger</param>
  139. /// <param name="handle">The attempted regionHandle of the challenger</param>
  140. /// <param name="authkey">The secret</param>
  141. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  142. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey)
  143. {
  144. bool throwHissyFit = false; // Should be true by 1.0
  145. if (throwHissyFit)
  146. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  147. SimProfileData data = GetProfileByLLUUID(uuid);
  148. return (handle == data.regionHandle && authkey == data.regionSecret);
  149. }
  150. /// <summary>
  151. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  152. /// </summary>
  153. /// <remarks>This requires a security audit.</remarks>
  154. /// <param name="uuid"></param>
  155. /// <param name="handle"></param>
  156. /// <param name="authhash"></param>
  157. /// <param name="challenge"></param>
  158. /// <returns></returns>
  159. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge)
  160. {
  161. SHA512Managed HashProvider = new SHA512Managed();
  162. ASCIIEncoding TextProvider = new ASCIIEncoding();
  163. byte[] stream = TextProvider.GetBytes(uuid.ToStringHyphenated() + ":" + handle.ToString() + ":" + challenge);
  164. byte[] hash = HashProvider.ComputeHash(stream);
  165. return false;
  166. }
  167. public ReservationData GetReservationAtPoint(uint x, uint y)
  168. {
  169. return null;
  170. }
  171. }
  172. }