MSSQLGridData.cs 19 KB

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