home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / cacls / account.cxx < prev    next >
C/C++ Source or Header  |  1995-03-13  |  6KB  |  217 lines

  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.  
  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.     } else if (_name)
  70.     {
  71.         LocalFree(_name);
  72.     }
  73.     if (_domain)
  74.         LocalFree(_domain);
  75. }
  76. //+---------------------------------------------------------------------------
  77. //
  78. //  Member:     CAccount::GetAccountName, public
  79. //
  80. //  Synopsis:   returns the Name associated with the instance of the class
  81. //
  82. //  Arguments:  OUT [name] address of the principal name
  83. //
  84. //----------------------------------------------------------------------------
  85. ULONG CAccount::GetAccountName(WCHAR **name)
  86. {
  87.  
  88.     ULONG ret = ERROR_SUCCESS;
  89.  
  90.     if (_name == NULL)
  91.     {
  92.         DWORD can = 0, crd = 0;
  93.         SID_NAME_USE esnu;
  94.  
  95.         if (!LookupAccountSid( NULL,
  96.                                _psid,
  97.                                NULL,
  98.                                &can,
  99.                                NULL,
  100.                                &crd,
  101.                                &esnu))
  102.         {
  103.             if (ERROR_INSUFFICIENT_BUFFER == (ret = GetLastError()))
  104.             {
  105.                 ret = ERROR_SUCCESS;
  106.                 if (NULL == (_name = (WCHAR *)LocalAlloc(LMEM_FIXED, can * sizeof(WCHAR))))
  107.                 {
  108.                     return(ERROR_NOT_ENOUGH_MEMORY);
  109.                 }
  110.                 if (NULL == (_domain = (WCHAR *)LocalAlloc(LMEM_FIXED, crd * sizeof(WCHAR))))
  111.                 {
  112.                     return(ERROR_NOT_ENOUGH_MEMORY);
  113.                 }
  114.  
  115.                 if ( !LookupAccountSid( NULL,
  116.                                        _psid,
  117.                                        _name,
  118.                                        &can,
  119.                                        _domain,
  120.                                        &crd,
  121.                                        &esnu) )
  122.                 {
  123.                    ret = GetLastError();
  124.                 }
  125.             }
  126.         }
  127.      }
  128.      *name = _name;
  129.      return(ret);
  130. }
  131. //+---------------------------------------------------------------------------
  132. //
  133. //  Member:     CAccount::GetAccountSid, public
  134. //
  135. //  Synopsis:   returns the Sid
  136. //
  137. //  Arguments:  OUT [psid] - sid associated with instance of the class
  138. //
  139. //----------------------------------------------------------------------------
  140. ULONG CAccount::GetAccountSid(SID **psid)
  141. {
  142.  
  143.     ULONG ret = ERROR_SUCCESS;
  144.  
  145.     if (_psid == NULL && _name != NULL)
  146.     {
  147.         DWORD cusid = 0, crd = 0;
  148.         SID_NAME_USE esnu;
  149.  
  150.         if (!LookupAccountName( _system,
  151.                                 _name,
  152.                                NULL,
  153.                                &cusid,
  154.                                NULL,
  155.                                &crd,
  156.                                &esnu))
  157.         {
  158.             if (ERROR_INSUFFICIENT_BUFFER == (ret = GetLastError()))
  159.             {
  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 = (WCHAR *)LocalAlloc(LMEM_FIXED, crd * sizeof(WCHAR))))
  167.                 {
  168.                     return(ERROR_NOT_ENOUGH_MEMORY);
  169.                 }
  170.  
  171.                 if ( !LookupAccountName( _system,
  172.                                          _name,
  173.                                          _psid,
  174.                                          &cusid,
  175.                                          _domain,
  176.                                          &crd,
  177.                                          &esnu) )
  178.  
  179.                 {
  180.                    ret = GetLastError();
  181.                 }
  182.             }
  183.         }
  184.      }
  185.      *psid = _psid;
  186.      return(ret);
  187. }
  188. //+---------------------------------------------------------------------------
  189. //
  190. //  Member:     CAccount::GetAccountDomain, public
  191. //
  192. //  Synopsis:   returns the domain for the class
  193. //
  194. //  Arguments:  [domain] - returns the domain associated with the instance of
  195. //                         the class
  196. //
  197. //----------------------------------------------------------------------------
  198. ULONG CAccount::GetAccountDomain(WCHAR **domain)
  199. {
  200.     ULONG ret = ERROR_SUCCESS;
  201.  
  202.     if (_domain == NULL)
  203.     {
  204.         if (_fsid)
  205.         {
  206.             SID *psid;
  207.             ret = GetAccountSid(&psid);
  208.         } else
  209.         {
  210.             WCHAR *name;
  211.             ret = GetAccountName(&name);
  212.         }
  213.     }
  214.     *domain = _domain;
  215.     return(ret);
  216. }
  217.