IAccessControl Writer.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

  1. #define _WIN32_DCOM
  2. #include <windows.h>
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <iaccess.h>  // For IAccessControl
  6. // Need to define this ourselves!
  7. const IID IID_IAccessControl = {0xEEDD23E0,0x8410,0x11CE,{0xA1,0xC3,0x08,0x00,0x2B,0x2B,0x8D,0x8F}};
  8. typedef struct
  9. {
  10.     WORD version;
  11.     WORD pad;
  12.     GUID classid;
  13. } SPermissionHeader;
  14. void main()
  15. {
  16.     HRESULT hr = CoInitialize(NULL);
  17.     if(FAILED(hr))
  18. cout << "Couldn't initialize COM" << endl;
  19. // Print out the CLSID of CLSID_DCOMAccessControl
  20. char buffer[39];
  21. OLECHAR ppsz[39];
  22. StringFromGUID2(CLSID_DCOMAccessControl, ppsz, 39);
  23. WideCharToMultiByte(CP_ACP, 0, ppsz, 39, buffer, 39, NULL, NULL);
  24. cout << "CLSID_DCOMAccessControl is " << buffer << endl;
  25.     // Create an DCOM access control object and get its IAccessControl interface
  26.     cout << "Creating an instance of CLSID_DCOMAccessControl" << endl;
  27.     IAccessControl* pAccessControl = NULL;     
  28.     hr = CoCreateInstance(CLSID_DCOMAccessControl, NULL, CLSCTX_INPROC_SERVER,
  29. IID_IAccessControl, (void**)&pAccessControl);
  30.     if(FAILED(hr))
  31. cout << "Couldn't create DCOM access control object" << endl;
  32.     // Setup the property list. We use the NULL property because we are
  33.     // trying to adjust the security of the object itself
  34.     ACTRL_ACCESSW access;
  35.     ACTRL_PROPERTY_ENTRYW propEntry;
  36.     access.cEntries = 1;
  37.     access.pPropertyAccessList = &propEntry;
  38.     
  39.     ACTRL_ACCESS_ENTRY_LISTW entryList;
  40.     propEntry.lpProperty = NULL;
  41.     propEntry.pAccessEntryList = &entryList;
  42.     propEntry.fListFlags = 0;
  43.     // Setup the access control list for the default property
  44.     ACTRL_ACCESS_ENTRYW entry;
  45.     entryList.cEntries = 1;
  46.     entryList.pAccessList = &entry;
  47.     // Setup the access control entry
  48.     entry.fAccessFlags = ACTRL_ACCESS_ALLOWED;
  49.     entry.Access = COM_RIGHTS_EXECUTE;
  50.     entry.ProvSpecificAccess = 0;
  51.     entry.Inheritance = NO_INHERITANCE;
  52.     entry.lpInheritProperty = NULL;
  53.     // NT requires the system account to have access (for launching)
  54.     entry.Trustee.pMultipleTrustee = NULL;
  55.     entry.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  56.     entry.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  57.     entry.Trustee.TrusteeType = TRUSTEE_IS_USER;
  58.     entry.Trustee.ptstrName = L"NT Authority\System";
  59.     cout << "Setting access rights: Allow access to NT Authority\System" << endl;
  60.     hr = pAccessControl->SetAccessRights(&access);
  61.     if(FAILED(hr))
  62. cout << "Couldn't set access" << endl;
  63.     // Deny access to a user
  64.     entry.fAccessFlags = ACTRL_ACCESS_DENIED;
  65.     entry.Trustee.TrusteeType = TRUSTEE_IS_USER;
  66.     entry.Trustee.ptstrName = L"Domain\Administrator";
  67.     wprintf(L"Setting access rights: Deny access to %sn",  entry.Trustee.ptstrName);
  68.     hr = pAccessControl->GrantAccessRights(&access);
  69.     if(FAILED(hr))
  70. cout << "Couldn't deny access" << endl;
  71.     // Grant access to everyone
  72.     entry.fAccessFlags = ACTRL_ACCESS_ALLOWED;
  73.     entry.Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  74.     entry.Trustee.ptstrName = L"*";
  75.     cout << "Setting access rights: Allow access to *" << endl;
  76.     hr = pAccessControl->GrantAccessRights(&access);
  77.     if(FAILED(hr))
  78. cout << "Couldn't allow access" << endl;
  79.     // Get IPersistStream interface from the DCOM access control object
  80.     cout << "Saving access list to registry key" << endl;
  81. IPersistStream* pPersistStream = NULL;
  82. hr = pAccessControl->QueryInterface(IID_IPersistStream, (void**)&pPersistStream);
  83.     if(FAILED(hr))
  84. cout << "Couldn't get IPersistStream" << endl;
  85.     // Find out how large the access control security buffer is
  86.     ULARGE_INTEGER size;
  87.     hr = pPersistStream->GetSizeMax(&size);
  88.     if(FAILED(hr))
  89. cout << "Couldn't get size of security buffer" << endl;
  90.     SPermissionHeader header;
  91.     size.QuadPart += sizeof(SPermissionHeader);
  92.     
  93.     // Create a stream where we can place the access control's security buffer
  94.     void* memory = CoTaskMemAlloc(size.LowPart);
  95.     if(memory == 0)
  96.     {
  97.         cout << "Couldn't allocate memory for security buffer" << endl;
  98.         exit(0);
  99.     }
  100.     
  101.     IStream* pStream;
  102.     hr = CreateStreamOnHGlobal(memory, TRUE, &pStream);
  103.     if(FAILED(hr))
  104. cout << "Couldn't create security buffer stream" << endl;
  105.     // Write the header to the stream
  106.     header.version = 2;
  107.     header.classid = CLSID_DCOMAccessControl;
  108.     hr = pStream->Write(&header, sizeof(header), NULL);
  109.     if(FAILED(hr))
  110.     cout << "Couldn't write header to stream" << endl;
  111.     // Write the access control security buffer to the stream
  112.     hr = pPersistStream->Save(pStream, TRUE);
  113.     if(FAILED(hr))
  114.     cout << "Couldn't persist access control information" << endl;
  115. // Your AppID below...
  116.     DWORD ignore;
  117.     HKEY key = 0;
  118.     hr = RegCreateKeyEx(HKEY_CLASSES_ROOT, "AppID\{10000002-0000-0000-0000-000000000001}",
  119. NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &key, &ignore);
  120.     if(FAILED(hr))
  121.     cout << "Couldn't create the AppID key" << endl;
  122.     // Throw everything into the registry
  123.     hr = RegSetValueEx(key, "AccessPermission", NULL, REG_BINARY, (UCHAR*)memory, size.LowPart);
  124.     if(FAILED(hr))
  125.     cout << "Couldn't write security buffer to registry" << endl;
  126.     // Release everything and bail out
  127.     pPersistStream->Release();
  128.     pStream->Release();
  129.     pAccessControl->Release();
  130.     RegCloseKey(key);
  131.     CoUninitialize();
  132. }