MSSQLGridData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Security.Cryptography;
  31. using System.Text;
  32. using libsecondlife;
  33. using OpenSim.Framework.Console;
  34. namespace OpenSim.Framework.Data.MSSQL
  35. {
  36. /// <summary>
  37. /// A grid data interface for Microsoft SQL Server
  38. /// </summary>
  39. public class MSSQLGridData : GridDataBase
  40. {
  41. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  42. /// <summary>
  43. /// Database manager
  44. /// </summary>
  45. private MSSQLManager database;
  46. private string m_regionsTableName;
  47. /// <summary>
  48. /// Initialises the Grid Interface
  49. /// </summary>
  50. override public void Initialise()
  51. {
  52. IniFile iniFile = new IniFile("mssql_connection.ini");
  53. string settingDataSource = iniFile.ParseFileReadValue("data_source");
  54. string settingInitialCatalog = iniFile.ParseFileReadValue("initial_catalog");
  55. string settingPersistSecurityInfo = iniFile.ParseFileReadValue("persist_security_info");
  56. string settingUserId = iniFile.ParseFileReadValue("user_id");
  57. string settingPassword = iniFile.ParseFileReadValue("password");
  58. m_regionsTableName = iniFile.ParseFileReadValue("regionstablename");
  59. if (m_regionsTableName == null)
  60. {
  61. m_regionsTableName = "regions";
  62. }
  63. database =
  64. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  65. settingPassword);
  66. TestTables();
  67. }
  68. private void TestTables()
  69. {
  70. IDbCommand cmd = database.Query("SELECT TOP 1 * FROM "+m_regionsTableName, new Dictionary<string, string>());
  71. try
  72. {
  73. cmd.ExecuteNonQuery();
  74. cmd.Dispose();
  75. }
  76. catch (Exception)
  77. {
  78. m_log.Info("[DATASTORE]: MSSQL Database doesn't exist... creating");
  79. database.ExecuteResourceSql("Mssql-regions.sql");
  80. }
  81. }
  82. /// <summary>
  83. /// Shuts down the grid interface
  84. /// </summary>
  85. override public void Close()
  86. {
  87. database.Close();
  88. }
  89. /// <summary>
  90. /// Returns the storage system name
  91. /// </summary>
  92. /// <returns>A string containing the storage system name</returns>
  93. override public string getName()
  94. {
  95. return "Sql OpenGridData";
  96. }
  97. /// <summary>
  98. /// Returns the storage system version
  99. /// </summary>
  100. /// <returns>A string containing the storage system version</returns>
  101. override public string getVersion()
  102. {
  103. return "0.1";
  104. }
  105. /// <summary>
  106. /// Returns a list of regions within the specified ranges
  107. /// </summary>
  108. /// <param name="a">minimum X coordinate</param>
  109. /// <param name="b">minimum Y coordinate</param>
  110. /// <param name="c">maximum X coordinate</param>
  111. /// <param name="d">maximum Y coordinate</param>
  112. /// <returns>An array of region profiles</returns>
  113. override public RegionProfileData[] GetProfilesInRange(uint a, uint b, uint c, uint d)
  114. {
  115. return null;
  116. }
  117. /// <summary>
  118. /// Returns a sim profile from its location
  119. /// </summary>
  120. /// <param name="handle">Region location handle</param>
  121. /// <returns>Sim profile</returns>
  122. override public RegionProfileData GetProfileByHandle(ulong handle)
  123. {
  124. IDataReader reader = null;
  125. try
  126. {
  127. Dictionary<string, string> param = new Dictionary<string, string>();
  128. param["handle"] = handle.ToString();
  129. IDbCommand result = database.Query("SELECT * FROM " + m_regionsTableName + " WHERE regionHandle = @handle", param);
  130. reader = result.ExecuteReader();
  131. RegionProfileData row = database.getRegionRow(reader);
  132. reader.Close();
  133. result.Dispose();
  134. return row;
  135. }
  136. catch (Exception)
  137. {
  138. if (reader != null)
  139. {
  140. reader.Close();
  141. }
  142. }
  143. return null;
  144. }
  145. /// <summary>
  146. /// Returns a sim profile from its UUID
  147. /// </summary>
  148. /// <param name="uuid">The region UUID</param>
  149. /// <returns>The sim profile</returns>
  150. override public RegionProfileData GetProfileByLLUUID(LLUUID uuid)
  151. {
  152. Dictionary<string, string> param = new Dictionary<string, string>();
  153. param["uuid"] = uuid.ToString();
  154. IDbCommand result = database.Query("SELECT * FROM " + m_regionsTableName + " WHERE uuid = @uuid", param);
  155. IDataReader reader = result.ExecuteReader();
  156. RegionProfileData row = database.getRegionRow(reader);
  157. reader.Close();
  158. result.Dispose();
  159. return row;
  160. }
  161. /// <summary>
  162. /// Returns a sim profile from it's Region name string
  163. /// </summary>
  164. /// <param name="uuid">The region name search query</param>
  165. /// <returns>The sim profile</returns>
  166. override public RegionProfileData GetProfileByString(string regionName)
  167. {
  168. if (regionName.Length > 2)
  169. {
  170. try
  171. {
  172. lock (database)
  173. {
  174. Dictionary<string, string> param = new Dictionary<string, string>();
  175. // Add % because this is a like query.
  176. param["?regionName"] = regionName + "%";
  177. // Order by statement will return shorter matches first. Only returns one record or no record.
  178. IDbCommand result = database.Query("SELECT top 1 * FROM " + m_regionsTableName + " WHERE regionName like ?regionName order by regionName", param);
  179. IDataReader reader = result.ExecuteReader();
  180. RegionProfileData row = database.getRegionRow(reader);
  181. reader.Close();
  182. result.Dispose();
  183. return row;
  184. }
  185. }
  186. catch (Exception e)
  187. {
  188. database.Reconnect();
  189. m_log.Error(e.ToString());
  190. return null;
  191. }
  192. }
  193. else
  194. {
  195. m_log.Error("[DATABASE]: Searched for a Region Name shorter then 3 characters");
  196. return null;
  197. }
  198. }
  199. /// <summary>
  200. /// Adds a new specified region to the database
  201. /// </summary>
  202. /// <param name="profile">The profile to add</param>
  203. /// <returns>A dataresponse enum indicating success</returns>
  204. override public DataResponse AddProfile(RegionProfileData profile)
  205. {
  206. try
  207. {
  208. if (GetProfileByLLUUID(profile.UUID) != null)
  209. {
  210. return DataResponse.RESPONSE_OK;
  211. }
  212. }
  213. catch (Exception)
  214. {
  215. System.Console.WriteLine("No regions found. Create new one.");
  216. }
  217. if (insertRegionRow(profile))
  218. {
  219. return DataResponse.RESPONSE_OK;
  220. }
  221. else
  222. {
  223. return DataResponse.RESPONSE_ERROR;
  224. }
  225. }
  226. /// <summary>
  227. /// Creates a new region in the database
  228. /// </summary>
  229. /// <param name="profile">The region profile to insert</param>
  230. /// <returns>Successful?</returns>
  231. public bool insertRegionRow(RegionProfileData profile)
  232. {
  233. //Insert new region
  234. string sql =
  235. "INSERT INTO " + m_regionsTableName + " ([regionHandle], [regionName], [uuid], [regionRecvKey], [regionSecret], [regionSendKey], [regionDataURI], ";
  236. sql +=
  237. "[serverIP], [serverPort], [serverURI], [locX], [locY], [locZ], [eastOverrideHandle], [westOverrideHandle], [southOverrideHandle], [northOverrideHandle], [regionAssetURI], [regionAssetRecvKey], ";
  238. sql +=
  239. "[regionAssetSendKey], [regionUserURI], [regionUserRecvKey], [regionUserSendKey], [regionMapTexture], [serverHttpPort], [serverRemotingPort], [owner_uuid]) VALUES ";
  240. sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, ";
  241. sql +=
  242. "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, ";
  243. sql +=
  244. "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey, @regionMapTexture, @serverHttpPort, @serverRemotingPort, @owner_uuid);";
  245. Dictionary<string, string> parameters = new Dictionary<string, string>();
  246. parameters["regionHandle"] = profile.regionHandle.ToString();
  247. parameters["regionName"] = profile.regionName;
  248. parameters["uuid"] = profile.UUID.ToString();
  249. parameters["regionRecvKey"] = profile.regionRecvKey;
  250. parameters["regionSecret"] = profile.regionSecret;
  251. parameters["regionSendKey"] = profile.regionSendKey;
  252. parameters["regionDataURI"] = profile.regionDataURI;
  253. parameters["serverIP"] = profile.serverIP;
  254. parameters["serverPort"] = profile.serverPort.ToString();
  255. parameters["serverURI"] = profile.serverURI;
  256. parameters["locX"] = profile.regionLocX.ToString();
  257. parameters["locY"] = profile.regionLocY.ToString();
  258. parameters["locZ"] = profile.regionLocZ.ToString();
  259. parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString();
  260. parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString();
  261. parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString();
  262. parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString();
  263. parameters["regionAssetURI"] = profile.regionAssetURI;
  264. parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey;
  265. parameters["regionAssetSendKey"] = profile.regionAssetSendKey;
  266. parameters["regionUserURI"] = profile.regionUserURI;
  267. parameters["regionUserRecvKey"] = profile.regionUserRecvKey;
  268. parameters["regionUserSendKey"] = profile.regionUserSendKey;
  269. parameters["regionMapTexture"] = profile.regionMapTextureID.ToString();
  270. parameters["serverHttpPort"] = profile.httpPort.ToString();
  271. parameters["serverRemotingPort"] = profile.remotingPort.ToString();
  272. parameters["owner_uuid"] = profile.owner_uuid.ToString();
  273. bool returnval = false;
  274. try
  275. {
  276. IDbCommand result = database.Query(sql, parameters);
  277. if (result.ExecuteNonQuery() == 1)
  278. returnval = true;
  279. result.Dispose();
  280. }
  281. catch (Exception e)
  282. {
  283. m_log.Error("MSSQLManager : " + e.ToString());
  284. }
  285. return returnval;
  286. }
  287. /// <summary>
  288. /// DEPRECATED. Attempts to authenticate a region by comparing a shared secret.
  289. /// </summary>
  290. /// <param name="uuid">The UUID of the challenger</param>
  291. /// <param name="handle">The attempted regionHandle of the challenger</param>
  292. /// <param name="authkey">The secret</param>
  293. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  294. override public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey)
  295. {
  296. bool throwHissyFit = false; // Should be true by 1.0
  297. if (throwHissyFit)
  298. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  299. RegionProfileData data = GetProfileByLLUUID(uuid);
  300. return (handle == data.regionHandle && authkey == data.regionSecret);
  301. }
  302. /// <summary>
  303. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  304. /// </summary>
  305. /// <remarks>This requires a security audit.</remarks>
  306. /// <param name="uuid"></param>
  307. /// <param name="handle"></param>
  308. /// <param name="authhash"></param>
  309. /// <param name="challenge"></param>
  310. /// <returns></returns>
  311. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge)
  312. {
  313. SHA512Managed HashProvider = new SHA512Managed();
  314. ASCIIEncoding TextProvider = new ASCIIEncoding();
  315. byte[] stream = TextProvider.GetBytes(uuid.ToString() + ":" + handle.ToString() + ":" + challenge);
  316. byte[] hash = HashProvider.ComputeHash(stream);
  317. return false;
  318. }
  319. override public ReservationData GetReservationAtPoint(uint x, uint y)
  320. {
  321. return null;
  322. }
  323. }
  324. }