MSSQLGridData.cs 18 KB

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