1
0

OspResolver.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 OpenSimulator 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.Reflection;
  28. using System.Text;
  29. using log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Services.Interfaces;
  33. namespace OpenSim.Framework.Serialization
  34. {
  35. /// <summary>
  36. /// Resolves OpenSim Profile Anchors (OSPA). An OSPA is a string used to provide information for
  37. /// identifying user profiles or supplying a simple name if no profile is available.
  38. /// </summary>
  39. public class OspResolver
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. public const string OSPA_PREFIX = "ospa:";
  43. public const string OSPA_NAME_KEY = "n";
  44. public const string OSPA_NAME_VALUE_SEPARATOR = " ";
  45. public const string OSPA_TUPLE_SEPARATOR = "|";
  46. public static readonly char[] OSPA_TUPLE_SEPARATOR_ARRAY = OSPA_TUPLE_SEPARATOR.ToCharArray();
  47. public const string OSPA_PAIR_SEPARATOR = "=";
  48. /// <summary>
  49. /// Make an OSPA given a user UUID
  50. /// </summary>
  51. /// <param name="userId"></param>
  52. /// <param name="commsManager"></param>
  53. /// <returns>The OSPA. Null if a user with the given UUID could not be found.</returns>
  54. public static string MakeOspa(UUID userId, IUserAccountService userService)
  55. {
  56. if (userService == null)
  57. {
  58. m_log.Warn("[OSP RESOLVER]: UserService is null");
  59. return userId.ToString();
  60. }
  61. UserAccount account = userService.GetUserAccount(UUID.Zero, userId);
  62. if (account != null)
  63. {
  64. return MakeOspa(account.FirstName, account.LastName);
  65. }
  66. // else
  67. // {
  68. // m_log.WarnFormat("[OSP RESOLVER]: No user account for {0}", userId);
  69. // System.Console.WriteLine("[OSP RESOLVER]: No user account for {0}", userId);
  70. // }
  71. return null;
  72. }
  73. /// <summary>
  74. /// Make an OSPA given a user name
  75. /// </summary>
  76. /// <param name="name"></param>
  77. /// <returns></returns>
  78. public static string MakeOspa(string firstName, string lastName)
  79. {
  80. string ospa
  81. = OSPA_PREFIX + OSPA_NAME_KEY + OSPA_PAIR_SEPARATOR + firstName + OSPA_NAME_VALUE_SEPARATOR + lastName;
  82. // m_log.DebugFormat("[OSP RESOLVER]: Made OSPA {0} for {1} {2}", ospa, firstName, lastName);
  83. // System.Console.WriteLine("[OSP RESOLVER]: Made OSPA {0} for {1} {2}", ospa, firstName, lastName);
  84. return ospa;
  85. }
  86. /// <summary>
  87. /// Resolve an osp string into the most suitable internal OpenSim identifier.
  88. /// </summary>
  89. ///
  90. /// In some cases this will be a UUID if a suitable profile exists on the system. In other cases, this may
  91. /// just return the same identifier after creating a temporary profile.
  92. ///
  93. /// <param name="ospa"></param>
  94. /// <param name="commsManager"></param>
  95. /// <returns>
  96. /// A suitable UUID for use in Second Life client communication. If the string was not a valid ospa, then UUID.Zero
  97. /// is returned.
  98. /// </returns>
  99. public static UUID ResolveOspa(string ospa, IUserAccountService userService)
  100. {
  101. if (!ospa.StartsWith(OSPA_PREFIX))
  102. {
  103. // m_log.DebugFormat("[OSP RESOLVER]: ResolveOspa() got unrecognized format [{0}]", ospa);
  104. return UUID.Zero;
  105. }
  106. // m_log.DebugFormat("[OSP RESOLVER]: Resolving {0}", ospa);
  107. string ospaMeat = ospa.Substring(OSPA_PREFIX.Length);
  108. string[] ospaTuples = ospaMeat.Split(OSPA_TUPLE_SEPARATOR_ARRAY);
  109. foreach (string tuple in ospaTuples)
  110. {
  111. int tupleSeparatorIndex = tuple.IndexOf(OSPA_PAIR_SEPARATOR);
  112. if (tupleSeparatorIndex < 0)
  113. {
  114. m_log.WarnFormat("[OSP RESOLVER]: Ignoring non-tuple component {0} in OSPA {1}", tuple, ospa);
  115. continue;
  116. }
  117. string key = tuple.Remove(tupleSeparatorIndex).Trim();
  118. string value = tuple.Substring(tupleSeparatorIndex + 1).Trim();
  119. if (OSPA_NAME_KEY == key)
  120. return ResolveOspaName(value, userService);
  121. }
  122. return UUID.Zero;
  123. }
  124. /// <summary>
  125. /// Hash a profile name into a UUID
  126. /// </summary>
  127. /// <param name="name"></param>
  128. /// <returns></returns>
  129. public static UUID HashName(string name)
  130. {
  131. return new UUID(Utils.MD5(Encoding.Unicode.GetBytes(name)), 0);
  132. }
  133. /// <summary>
  134. /// Resolve an OSPI name by querying existing persistent user profiles. If there is no persistent user profile
  135. /// then a temporary user profile is inserted in the cache.
  136. /// </summary>
  137. /// <param name="name"></param>
  138. /// <param name="commsManager"></param>
  139. /// <returns>
  140. /// An OpenSim internal identifier for the name given. Returns null if the name was not valid
  141. /// </returns>
  142. protected static UUID ResolveOspaName(string name, IUserAccountService userService)
  143. {
  144. if (userService == null)
  145. return UUID.Zero;
  146. int nameSeparatorIndex = name.IndexOf(OSPA_NAME_VALUE_SEPARATOR);
  147. if (nameSeparatorIndex < 0)
  148. {
  149. m_log.WarnFormat("[OSP RESOLVER]: Ignoring unseparated name {0}", name);
  150. return UUID.Zero;
  151. }
  152. string firstName = name.Remove(nameSeparatorIndex).TrimEnd();
  153. string lastName = name.Substring(nameSeparatorIndex + 1).TrimStart();
  154. UserAccount account = userService.GetUserAccount(UUID.Zero, firstName, lastName);
  155. if (account != null)
  156. {
  157. // m_log.DebugFormat(
  158. // "[OSP RESOLVER]: Found user account with uuid {0} for {1} {2}",
  159. // account.PrincipalID, firstName, lastName);
  160. return account.PrincipalID;
  161. }
  162. // else
  163. // {
  164. // m_log.DebugFormat("[OSP RESOLVER]: No resolved OSPA user account for {0}", name);
  165. // }
  166. // XXX: Disable temporary user profile creation for now as implementation is incomplete - justincc
  167. /*
  168. UserProfileData tempUserProfile = new UserProfileData();
  169. tempUserProfile.FirstName = firstName;
  170. tempUserProfile.SurName = lastName;
  171. tempUserProfile.ID = HashName(tempUserProfile.Name);
  172. m_log.DebugFormat(
  173. "[OSP RESOLVER]: Adding temporary user profile for {0} {1}", tempUserProfile.Name, tempUserProfile.ID);
  174. commsManager.UserService.AddTemporaryUserProfile(tempUserProfile);
  175. return tempUserProfile.ID;
  176. */
  177. return UUID.Zero;
  178. }
  179. }
  180. }