Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.3 KiB

  1. //+------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1993, Microsoft Corporation.
  4. //
  5. // File: account.cxx
  6. //
  7. // Contents: Class wrapping account sid and name
  8. //
  9. // Classes: CAccount
  10. //
  11. // History: Nov-93 DaveMont Created.
  12. //
  13. //-------------------------------------------------------------------
  14. #include "pch.h"
  15. #include "account.hxx"
  16. //+---------------------------------------------------------------------------
  17. //
  18. // Member: CAccount::CAccount, public
  19. //
  20. // Synopsis: initializes data members
  21. //
  22. // Arguments: IN [Name] - principal
  23. // IN [System] - server/domain
  24. //
  25. //----------------------------------------------------------------------------
  26. CAccount::CAccount(WCHAR *Name, WCHAR *System)
  27. : _name(Name),
  28. _system(System),
  29. _domain(NULL),
  30. _psid(NULL),
  31. _fsid(TRUE)
  32. {
  33. }
  34. //+---------------------------------------------------------------------------
  35. //
  36. // Member: CAccount::CAccount, public
  37. //
  38. // Synopsis: Initializes data members
  39. //
  40. // Arguments: IN [pSid] - SID of principal
  41. // IN [System] - server/domain
  42. //
  43. //----------------------------------------------------------------------------
  44. CAccount::CAccount(SID *pSid, WCHAR *System)
  45. : _name(NULL),
  46. _system(System),
  47. _domain(NULL),
  48. _psid(pSid),
  49. _fsid(FALSE)
  50. {
  51. }
  52. //+---------------------------------------------------------------------------
  53. //
  54. // Member: Dtor, public
  55. //
  56. // Synopsis: frees sid or name and domain
  57. //
  58. // Arguments: none
  59. //
  60. //----------------------------------------------------------------------------
  61. CAccount::~CAccount()
  62. {
  63. if (_fsid)
  64. {
  65. if (_psid)
  66. {
  67. LocalFree(_psid);
  68. }
  69. }
  70. /* if (_name)
  71. {
  72. LocalFree(_name);
  73. }
  74. */
  75. if (_domain)
  76. {
  77. LocalFree(_domain);
  78. }
  79. if (_system)
  80. {
  81. LocalFree(_system);
  82. }
  83. }
  84. //+---------------------------------------------------------------------------
  85. //
  86. // Member: CAccount::GetAccountName, public
  87. //
  88. // Synopsis: returns the Name associated with the instance of the class
  89. //
  90. // Arguments: OUT [name] address of the principal name
  91. //
  92. //----------------------------------------------------------------------------
  93. ULONG CAccount::GetAccountName(WCHAR **name)
  94. {
  95. ULONG ret = ERROR_SUCCESS;
  96. if (_name == NULL)
  97. {
  98. DWORD can = 0, crd = 0;
  99. SID_NAME_USE esnu;
  100. if (!LookupAccountSid( NULL,
  101. _psid,
  102. NULL,
  103. &can,
  104. NULL,
  105. &crd,
  106. &esnu))
  107. {
  108. if (ERROR_INSUFFICIENT_BUFFER == (ret = GetLastError()))
  109. {
  110. ret = ERROR_SUCCESS;
  111. if (NULL == (_name = (LPWSTR)LocalAlloc(LMEM_FIXED, can * sizeof(WCHAR))))
  112. {
  113. return(ERROR_NOT_ENOUGH_MEMORY);
  114. }
  115. if (NULL == (_domain = (LPWSTR)LocalAlloc(LMEM_FIXED, crd * sizeof(WCHAR))))
  116. {
  117. return(ERROR_NOT_ENOUGH_MEMORY);
  118. }
  119. if ( !LookupAccountSid( NULL,
  120. _psid,
  121. (LPTSTR)_name,
  122. &can,
  123. (LPTSTR)_domain,
  124. &crd,
  125. &esnu) )
  126. {
  127. ret = GetLastError();
  128. }
  129. }
  130. }
  131. }
  132. *name = _name;
  133. return(ret);
  134. }
  135. //+---------------------------------------------------------------------------
  136. //
  137. // Member: CAccount::GetAccountSid, public
  138. //
  139. // Synopsis: returns the Sid
  140. //
  141. // Arguments: OUT [psid] - sid associated with instance of the class
  142. //
  143. //----------------------------------------------------------------------------
  144. ULONG CAccount::GetAccountSid(SID **psid)
  145. {
  146. ULONG ret = ERROR_SUCCESS;
  147. if (_psid == NULL && _name != NULL)
  148. {
  149. DWORD cusid = 0, crd = 0;
  150. SID_NAME_USE esnu;
  151. if (!LookupAccountName( (LPCTSTR)_system,
  152. (LPCTSTR)_name,
  153. NULL,
  154. &cusid,
  155. NULL,
  156. &crd,
  157. &esnu))
  158. {
  159. if (ERROR_INSUFFICIENT_BUFFER == (ret = GetLastError()))
  160. {
  161. ret = ERROR_SUCCESS;
  162. if (NULL == (_psid = (SID *)LocalAlloc(LMEM_FIXED, cusid)))
  163. {
  164. return(ERROR_NOT_ENOUGH_MEMORY);
  165. }
  166. if (NULL == (_domain = (LPWSTR)LocalAlloc(LMEM_FIXED, crd * sizeof(WCHAR))))
  167. {
  168. return(ERROR_NOT_ENOUGH_MEMORY);
  169. }
  170. if ( !LookupAccountName( (LPCTSTR)_system,
  171. (LPCTSTR)_name,
  172. _psid,
  173. &cusid,
  174. (LPTSTR)_domain,
  175. &crd,
  176. &esnu) )
  177. {
  178. ret = GetLastError();
  179. }
  180. }
  181. }
  182. }
  183. *psid = _psid;
  184. return(ret);
  185. }
  186. //+---------------------------------------------------------------------------
  187. //
  188. // Member: CAccount::GetAccountDomain, public
  189. //
  190. // Synopsis: returns the domain for the class
  191. //
  192. // Arguments: [domain] - returns the domain associated with the instance of
  193. // the class
  194. //
  195. //----------------------------------------------------------------------------
  196. ULONG CAccount::GetAccountDomain(LPWSTR *domain)
  197. {
  198. ULONG ret = ERROR_SUCCESS;
  199. if (_domain == NULL)
  200. {
  201. if (_fsid)
  202. {
  203. SID *psid;
  204. ret = GetAccountSid(&psid);
  205. } else
  206. {
  207. LPWSTR name = NULL;
  208. ret = GetAccountName(&name);
  209. }
  210. }
  211. *domain = _domain;
  212. return(ret);
  213. }