SQLiteGridData.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenGrid.Framework.Data;
  5. namespace OpenGrid.Framework.Data.SQLite
  6. {
  7. public class SQLiteGridData : IGridData
  8. {
  9. private SQLiteManager database;
  10. /// <summary>
  11. /// Initialises the Grid Interface
  12. /// </summary>
  13. public void Initialise()
  14. {
  15. database = new SQLiteManager("localhost", "db", "user", "password", "false");
  16. }
  17. /// <summary>
  18. /// Shuts down the grid interface
  19. /// </summary>
  20. public void Close()
  21. {
  22. database.Close();
  23. }
  24. public string getName()
  25. {
  26. return "SQLite OpenGridData";
  27. }
  28. public string getVersion()
  29. {
  30. return "0.1";
  31. }
  32. /// <summary>
  33. /// Returns a sim profile from it's location
  34. /// </summary>
  35. /// <param name="handle">Region location handle</param>
  36. /// <returns>Sim profile</returns>
  37. public SimProfileData GetProfileByHandle(ulong handle)
  38. {
  39. Dictionary<string, string> param = new Dictionary<string, string>();
  40. param["handle"] = handle.ToString();
  41. System.Data.IDbCommand result = database.Query("SELECT * FROM regions WHERE handle = @handle", param);
  42. System.Data.IDataReader reader = result.ExecuteReader();
  43. SimProfileData row = database.getRow(reader);
  44. reader.Close();
  45. result.Dispose();
  46. return row;
  47. }
  48. /// <summary>
  49. /// Returns a sim profile from it's UUID
  50. /// </summary>
  51. /// <param name="uuid">The region UUID</param>
  52. /// <returns>The sim profile</returns>
  53. public SimProfileData GetProfileByLLUUID(libsecondlife.LLUUID uuid)
  54. {
  55. Dictionary<string, string> param = new Dictionary<string, string>();
  56. param["uuid"] = uuid.ToStringHyphenated();
  57. System.Data.IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = @uuid", param);
  58. System.Data.IDataReader reader = result.ExecuteReader();
  59. SimProfileData row = database.getRow(reader);
  60. reader.Close();
  61. result.Dispose();
  62. return row;
  63. }
  64. public DataResponse AddProfile(SimProfileData profile)
  65. {
  66. if (database.insertRow(profile))
  67. {
  68. return DataResponse.RESPONSE_OK;
  69. }
  70. else
  71. {
  72. return DataResponse.RESPONSE_ERROR;
  73. }
  74. }
  75. /// <summary>
  76. /// DEPRECIATED. Attempts to authenticate a region by comparing a shared secret.
  77. /// </summary>
  78. /// <param name="uuid">The UUID of the challenger</param>
  79. /// <param name="handle">The attempted regionHandle of the challenger</param>
  80. /// <param name="authkey">The secret</param>
  81. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  82. public bool AuthenticateSim(libsecondlife.LLUUID uuid, ulong handle, string authkey)
  83. {
  84. bool throwHissyFit = false; // Should be true by 1.0
  85. if (throwHissyFit)
  86. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  87. SimProfileData data = GetProfileByLLUUID(uuid);
  88. return (handle == data.regionHandle && authkey == data.regionSecret);
  89. }
  90. /// <summary>
  91. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  92. /// </summary>
  93. /// <remarks>This requires a security audit.</remarks>
  94. /// <param name="uuid"></param>
  95. /// <param name="handle"></param>
  96. /// <param name="authhash"></param>
  97. /// <param name="challenge"></param>
  98. /// <returns></returns>
  99. public bool AuthenticateSim(libsecondlife.LLUUID uuid, ulong handle, string authhash, string challenge)
  100. {
  101. System.Security.Cryptography.SHA512Managed HashProvider = new System.Security.Cryptography.SHA512Managed();
  102. System.Text.ASCIIEncoding TextProvider = new ASCIIEncoding();
  103. byte[] stream = TextProvider.GetBytes(uuid.ToStringHyphenated() + ":" + handle.ToString() + ":" + challenge);
  104. byte[] hash = HashProvider.ComputeHash(stream);
  105. return false;
  106. }
  107. }
  108. }