Source code of Windows XP (NT5)
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.

216 lines
5.9 KiB

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