MSSQLGridData.cs 18 KB

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