/TvEngine3/TVLibrary/TVLibrary/Implementations/Helper/SidHelper.cs

https://github.com/MediaPortal/MediaPortal-1 · C# · 205 lines · 131 code · 23 blank · 51 comment · 14 complexity · ea66fb08d9104ffd0fbcbc79ce3f242a MD5 · raw file

  1. #region Copyright (C) 2005-2011 Team MediaPortal
  2. // Copyright (C) 2005-2011 Team MediaPortal
  3. // http://www.team-mediaportal.com
  4. //
  5. // MediaPortal is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // MediaPortal is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
  17. #endregion
  18. using System;
  19. using System.Runtime.InteropServices;
  20. using System.Text;
  21. using System.ComponentModel;
  22. namespace TvLibrary.Helper
  23. {
  24. /// <summary>
  25. /// SID Helper methods
  26. /// </summary>
  27. public class SidHelper
  28. {
  29. #region imports
  30. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  31. private static extern bool LookupAccountSid(
  32. [In, MarshalAs(UnmanagedType.LPTStr)] string systemName,
  33. IntPtr sid,
  34. [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder name,
  35. ref int cbName,
  36. StringBuilder referencedDomainName,
  37. ref int cbReferencedDomainName,
  38. out int use);
  39. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  40. private static extern bool LookupAccountName(
  41. [In, MarshalAs(UnmanagedType.LPTStr)] string systemName,
  42. [In, MarshalAs(UnmanagedType.LPTStr)] string accountName,
  43. IntPtr sid,
  44. ref int cbSid,
  45. StringBuilder referencedDomainName,
  46. ref int cbReferencedDomainName,
  47. out int use);
  48. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  49. internal static extern bool ConvertSidToStringSid(
  50. IntPtr sid,
  51. [In, Out, MarshalAs(UnmanagedType.LPTStr)] ref string pStringSid);
  52. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  53. internal static extern bool ConvertStringSidToSid(
  54. [In, MarshalAs(UnmanagedType.LPTStr)] string pStringSid,
  55. ref IntPtr sid);
  56. #endregion
  57. ///<summary>
  58. /// Gets the SID pointer
  59. ///</summary>
  60. ///<param name="name">Name</param>
  61. ///<returns>Pointer to the SID</returns>
  62. public static IntPtr GetSidPtr(string name)
  63. {
  64. IntPtr _sid = IntPtr.Zero; //pointer to binary form of SID string.
  65. int _sidLength = 0; //size of SID buffer.
  66. int _domainLength = 0; //size of domain name buffer.
  67. int _use; //type of object.
  68. //stringBuilder for domain name.
  69. StringBuilder _domain = new StringBuilder();
  70. //first call of the function only returns the size
  71. //of buffers (SID, domain name)
  72. LookupAccountName(null, name, _sid, ref _sidLength, _domain,
  73. ref _domainLength, out _use);
  74. int _error = Marshal.GetLastWin32Error();
  75. //error 122 (The data area passed to a system call is too small)
  76. // normal behaviour.
  77. if (_error != 122)
  78. {
  79. return IntPtr.Zero;
  80. }
  81. //allocates memory for domain name
  82. _domain = new StringBuilder(_domainLength);
  83. //allocates memory for SID
  84. _sid = Marshal.AllocHGlobal(_sidLength);
  85. bool _rc = LookupAccountName(null, name, _sid, ref _sidLength, _domain,
  86. ref _domainLength, out _use);
  87. if (_rc == false)
  88. {
  89. Marshal.GetLastWin32Error();
  90. Marshal.FreeHGlobal(_sid);
  91. return IntPtr.Zero;
  92. }
  93. return _sid;
  94. }
  95. /// <summary>
  96. /// Gets the SID
  97. /// </summary>
  98. /// <param name="name">Name</param>
  99. /// <returns>SID</returns>
  100. public static string GetSid(string name)
  101. {
  102. IntPtr _sid = IntPtr.Zero; //pointer to binary form of SID string.
  103. int _sidLength = 0; //size of SID buffer.
  104. int _domainLength = 0; //size of domain name buffer.
  105. int _use; //type of object.
  106. //stringBuilder for domain name.
  107. StringBuilder _domain = new StringBuilder();
  108. string _sidString = "";
  109. //first call of the function only returns the size
  110. //of buffers (SID, domain name)
  111. LookupAccountName(null, name, _sid, ref _sidLength, _domain,
  112. ref _domainLength, out _use);
  113. int _error = Marshal.GetLastWin32Error();
  114. //error 122 (The data area passed to a system call is too small)
  115. // normal behaviour.
  116. if (_error != 122)
  117. {
  118. throw (new Exception(new Win32Exception(_error).Message));
  119. }
  120. //allocates memory for domain name
  121. _domain = new StringBuilder(_domainLength);
  122. //allocates memory for SID
  123. _sid = Marshal.AllocHGlobal(_sidLength);
  124. bool _rc = LookupAccountName(null, name, _sid, ref _sidLength, _domain,
  125. ref _domainLength, out _use);
  126. if (_rc == false)
  127. {
  128. _error = Marshal.GetLastWin32Error();
  129. Marshal.FreeHGlobal(_sid);
  130. throw (new Exception(new Win32Exception(_error).Message));
  131. }
  132. // converts binary SID into string
  133. _rc = ConvertSidToStringSid(_sid, ref _sidString);
  134. if (_rc == false)
  135. {
  136. _error = Marshal.GetLastWin32Error();
  137. Marshal.FreeHGlobal(_sid);
  138. throw (new Exception(new Win32Exception(_error).Message));
  139. }
  140. Marshal.FreeHGlobal(_sid);
  141. return _sidString;
  142. }
  143. /// <summary>
  144. /// Gets the name to the given sid
  145. /// </summary>
  146. /// <param name="sid">The SID</param>
  147. /// <returns>Name to the SID</returns>
  148. public static string GetName(string sid)
  149. {
  150. IntPtr _sid = IntPtr.Zero; //pointer to binary form of SID string.
  151. int _nameLength = 0; //size of object name buffer
  152. int _domainLength = 0; //size of domain name buffer
  153. int _use; //type of object
  154. StringBuilder _domain = new StringBuilder(); //domain name variable
  155. StringBuilder _name = new StringBuilder(); //object name variable
  156. //converts SID string into the binary form
  157. bool _rc0 = ConvertStringSidToSid(sid, ref _sid);
  158. if (_rc0 == false)
  159. {
  160. Marshal.GetLastWin32Error();
  161. Marshal.FreeHGlobal(_sid);
  162. return String.Empty;
  163. }
  164. //first call of method returns the size of domain name
  165. //and object name buffers
  166. LookupAccountSid(null, _sid, _name, ref _nameLength, _domain,
  167. ref _domainLength, out _use);
  168. _domain = new StringBuilder(_domainLength); //allocates memory for domain name
  169. _name = new StringBuilder(_nameLength); //allocates memory for object name
  170. bool _rc = LookupAccountSid(null, _sid, _name, ref _nameLength, _domain,
  171. ref _domainLength, out _use);
  172. if (_rc == false)
  173. {
  174. Marshal.GetLastWin32Error();
  175. Marshal.FreeHGlobal(_sid);
  176. return String.Empty;
  177. }
  178. Marshal.FreeHGlobal(_sid);
  179. return _domain + "\\" + _name;
  180. }
  181. }
  182. }