MSSQLGridData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 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. using OpenSim.Framework.Console;
  35. namespace OpenSim.Framework.Data.MSSQL
  36. {
  37. /// <summary>
  38. /// A grid data interface for Microsoft SQL Server
  39. /// </summary>
  40. public class SqlGridData : IGridData
  41. {
  42. /// <summary>
  43. /// Database manager
  44. /// </summary>
  45. private MSSQLManager database;
  46. /// <summary>
  47. /// Initialises the Grid Interface
  48. /// </summary>
  49. public void Initialise()
  50. {
  51. IniFile GridDataMySqlFile = new IniFile("mssql_connection.ini");
  52. string settingDataSource = GridDataMySqlFile.ParseFileReadValue("data_source");
  53. string settingInitialCatalog = GridDataMySqlFile.ParseFileReadValue("initial_catalog");
  54. string settingPersistSecurityInfo = GridDataMySqlFile.ParseFileReadValue("persist_security_info");
  55. string settingUserId = GridDataMySqlFile.ParseFileReadValue("user_id");
  56. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  57. database =
  58. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  59. settingPassword);
  60. }
  61. /// <summary>
  62. /// Shuts down the grid interface
  63. /// </summary>
  64. public void Close()
  65. {
  66. database.Close();
  67. }
  68. /// <summary>
  69. /// Returns the storage system name
  70. /// </summary>
  71. /// <returns>A string containing the storage system name</returns>
  72. public string getName()
  73. {
  74. return "Sql OpenGridData";
  75. }
  76. /// <summary>
  77. /// Returns the storage system version
  78. /// </summary>
  79. /// <returns>A string containing the storage system version</returns>
  80. public string getVersion()
  81. {
  82. return "0.1";
  83. }
  84. /// <summary>
  85. /// Returns a list of regions within the specified ranges
  86. /// </summary>
  87. /// <param name="a">minimum X coordinate</param>
  88. /// <param name="b">minimum Y coordinate</param>
  89. /// <param name="c">maximum X coordinate</param>
  90. /// <param name="d">maximum Y coordinate</param>
  91. /// <returns>An array of region profiles</returns>
  92. public RegionProfileData[] GetProfilesInRange(uint a, uint b, uint c, uint d)
  93. {
  94. return null;
  95. }
  96. /// <summary>
  97. /// Returns a sim profile from it's location
  98. /// </summary>
  99. /// <param name="handle">Region location handle</param>
  100. /// <returns>Sim profile</returns>
  101. public RegionProfileData GetProfileByHandle(ulong handle)
  102. {
  103. IDataReader reader = null;
  104. try
  105. {
  106. Dictionary<string, string> param = new Dictionary<string, string>();
  107. param["handle"] = handle.ToString();
  108. IDbCommand result = database.Query("SELECT * FROM regions WHERE regionHandle = @handle", param);
  109. reader = result.ExecuteReader();
  110. RegionProfileData row = database.getRegionRow(reader);
  111. reader.Close();
  112. result.Dispose();
  113. return row;
  114. }
  115. catch (Exception)
  116. {
  117. if (reader != null)
  118. {
  119. reader.Close();
  120. }
  121. }
  122. return null;
  123. }
  124. /// <summary>
  125. /// // Returns a list of avatar and UUIDs that match the query
  126. /// </summary>
  127. public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  128. {
  129. List<AvatarPickerAvatar> returnlist = new List<AvatarPickerAvatar>();
  130. string[] querysplit;
  131. querysplit = query.Split(' ');
  132. if (querysplit.Length == 2)
  133. {
  134. try
  135. {
  136. lock (database)
  137. {
  138. Dictionary<string, string> param = new Dictionary<string, string>();
  139. param["first"] = querysplit[0];
  140. param["second"] = querysplit[1];
  141. IDbCommand result =
  142. database.Query(
  143. "SELECT UUID,username,surname FROM users WHERE username = @first AND lastname = @second",
  144. param);
  145. IDataReader reader = result.ExecuteReader();
  146. while (reader.Read())
  147. {
  148. AvatarPickerAvatar user = new AvatarPickerAvatar();
  149. user.AvatarID = new LLUUID((string) reader["UUID"]);
  150. user.firstName = (string) reader["username"];
  151. user.lastName = (string) reader["surname"];
  152. returnlist.Add(user);
  153. }
  154. reader.Close();
  155. result.Dispose();
  156. }
  157. }
  158. catch (Exception e)
  159. {
  160. database.Reconnect();
  161. MainLog.Instance.Error(e.ToString());
  162. return returnlist;
  163. }
  164. }
  165. else if (querysplit.Length == 1)
  166. {
  167. try
  168. {
  169. lock (database)
  170. {
  171. Dictionary<string, string> param = new Dictionary<string, string>();
  172. param["first"] = querysplit[0];
  173. param["second"] = querysplit[1];
  174. IDbCommand result =
  175. database.Query(
  176. "SELECT UUID,username,surname FROM users WHERE username = @first OR lastname = @second",
  177. param);
  178. IDataReader reader = result.ExecuteReader();
  179. while (reader.Read())
  180. {
  181. AvatarPickerAvatar user = new AvatarPickerAvatar();
  182. user.AvatarID = new LLUUID((string) reader["UUID"]);
  183. user.firstName = (string) reader["username"];
  184. user.lastName = (string) reader["surname"];
  185. returnlist.Add(user);
  186. }
  187. reader.Close();
  188. result.Dispose();
  189. }
  190. }
  191. catch (Exception e)
  192. {
  193. database.Reconnect();
  194. MainLog.Instance.Error(e.ToString());
  195. return returnlist;
  196. }
  197. }
  198. return returnlist;
  199. }
  200. /// <summary>
  201. /// Returns a sim profile from it's UUID
  202. /// </summary>
  203. /// <param name="uuid">The region UUID</param>
  204. /// <returns>The sim profile</returns>
  205. public RegionProfileData GetProfileByLLUUID(LLUUID uuid)
  206. {
  207. Dictionary<string, string> param = new Dictionary<string, string>();
  208. param["uuid"] = uuid.ToString();
  209. IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = @uuid", param);
  210. IDataReader reader = result.ExecuteReader();
  211. RegionProfileData row = database.getRegionRow(reader);
  212. reader.Close();
  213. result.Dispose();
  214. return row;
  215. }
  216. /// <summary>
  217. /// Adds a new specified region to the database
  218. /// </summary>
  219. /// <param name="profile">The profile to add</param>
  220. /// <returns>A dataresponse enum indicating success</returns>
  221. public DataResponse AddProfile(RegionProfileData profile)
  222. {
  223. try
  224. {
  225. if (GetProfileByLLUUID(profile.UUID) != null)
  226. {
  227. return DataResponse.RESPONSE_OK;
  228. }
  229. }
  230. catch (Exception)
  231. {
  232. System.Console.WriteLine("No regions found. Create new one.");
  233. }
  234. if (database.insertRegionRow(profile))
  235. {
  236. return DataResponse.RESPONSE_OK;
  237. }
  238. else
  239. {
  240. return DataResponse.RESPONSE_ERROR;
  241. }
  242. }
  243. /// <summary>
  244. /// DEPRECIATED. Attempts to authenticate a region by comparing a shared secret.
  245. /// </summary>
  246. /// <param name="uuid">The UUID of the challenger</param>
  247. /// <param name="handle">The attempted regionHandle of the challenger</param>
  248. /// <param name="authkey">The secret</param>
  249. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  250. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey)
  251. {
  252. bool throwHissyFit = false; // Should be true by 1.0
  253. if (throwHissyFit)
  254. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  255. RegionProfileData data = GetProfileByLLUUID(uuid);
  256. return (handle == data.regionHandle && authkey == data.regionSecret);
  257. }
  258. /// <summary>
  259. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  260. /// </summary>
  261. /// <remarks>This requires a security audit.</remarks>
  262. /// <param name="uuid"></param>
  263. /// <param name="handle"></param>
  264. /// <param name="authhash"></param>
  265. /// <param name="challenge"></param>
  266. /// <returns></returns>
  267. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge)
  268. {
  269. SHA512Managed HashProvider = new SHA512Managed();
  270. ASCIIEncoding TextProvider = new ASCIIEncoding();
  271. byte[] stream = TextProvider.GetBytes(uuid.ToString() + ":" + handle.ToString() + ":" + challenge);
  272. byte[] hash = HashProvider.ComputeHash(stream);
  273. return false;
  274. }
  275. public ReservationData GetReservationAtPoint(uint x, uint y)
  276. {
  277. return null;
  278. }
  279. }
  280. }