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.

138 lines
4.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: util.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #include "resource.h"
  12. #include "util.h"
  13. #include "delegWiz.h"
  14. #include <_util.cpp>
  15. VOID DisplayMessageBox(HWND hwnd, LPWSTR lpszText)
  16. {
  17. CWString szTitle;
  18. szTitle.LoadFromResource(IDS_DELEGWIZ_WIZ_TITLE);
  19. ::MessageBox(hwnd,lpszText, szTitle, MB_OK);
  20. }
  21. //This function checks if current user has read and write
  22. //access to the szObjectPath. If not it shows appropriate
  23. //Message box.
  24. HRESULT InitCheckAccess( HWND hwndParent, LPCWSTR pszObjectLADPPath )
  25. {
  26. HRESULT hr = S_OK;
  27. WCHAR szSDRightsProp[] = L"sDRightsEffective";
  28. LPWSTR pProp = (LPWSTR)szSDRightsProp;
  29. PADS_ATTR_INFO pSDRightsInfo = NULL;
  30. PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
  31. DWORD dwAttributesReturned;
  32. IDirectoryObject *pDsObject = NULL;
  33. SECURITY_INFORMATION si = 0;
  34. //Check Permission to "Read Permission"
  35. DWORD dwErr = ::GetNamedSecurityInfo(IN const_cast<LPWSTR>(pszObjectLADPPath),
  36. SE_DS_OBJECT_ALL,
  37. DACL_SECURITY_INFORMATION,
  38. NULL,
  39. NULL,
  40. NULL,
  41. NULL,
  42. &pSecurityDescriptor);
  43. TRACE(L"GetNamedSecurityInfo() returned dwErr = 0x%x\n", dwErr);
  44. if (dwErr != ERROR_SUCCESS)
  45. {
  46. TRACE(L"failed on GetNamedSecurityInfo(): dwErr = 0x%x\n", dwErr);
  47. WCHAR szMsg[512];
  48. LoadStringHelper(IDS_DELEGWIZ_ERR_GET_SEC_INFO, szMsg, 512);
  49. DisplayMessageBox(hwndParent, szMsg);
  50. hr = HRESULT_FROM_WIN32(dwErr);
  51. goto exit_gracefully;
  52. }
  53. // Bind to the object
  54. hr = ADsOpenObject(pszObjectLADPPath,
  55. (LPWSTR)NULL,
  56. (LPWSTR)NULL,
  57. ADS_SECURE_AUTHENTICATION | ADS_FAST_BIND,
  58. IID_IDirectoryObject,
  59. (LPVOID*)&pDsObject);
  60. if( hr != S_OK )
  61. goto exit_gracefully;
  62. // Read the sDRightsEffective property to determine writability
  63. pDsObject->GetObjectAttributes( &pProp,
  64. 1,
  65. &pSDRightsInfo,
  66. &dwAttributesReturned);
  67. if (pSDRightsInfo)
  68. {
  69. si = pSDRightsInfo->pADsValues->Integer;
  70. FreeADsMem(pSDRightsInfo);
  71. }
  72. else
  73. {
  74. //
  75. // Note that GetObjectAttributes commonly returns S_OK even when
  76. // it fails, so the HRESULT is basically useless here.
  77. //
  78. // This can fail if we don't have read_property access, which can
  79. // happen when an admin is trying to restore access to an object
  80. // that has had all access removed or denied
  81. //
  82. // Assume we can write the Owner and DACL. If not, the worst that
  83. // happens is the user gets an "Access Denied" message when trying
  84. // to save changes.
  85. //
  86. si = DACL_SECURITY_INFORMATION;
  87. }
  88. if( !(si & DACL_SECURITY_INFORMATION) )
  89. {
  90. TRACE(L"failed on SetNamedSecurityInfo(): dwErr = 0x%x\n", dwErr);
  91. WCHAR szMsg[512];
  92. LoadStringHelper(IDS_DELEGWIZ_ERR_ACCESS_DENIED, szMsg, 512);
  93. DisplayMessageBox(hwndParent, szMsg);
  94. hr = !S_OK;
  95. }
  96. exit_gracefully:
  97. if( pSecurityDescriptor )
  98. LocalFree(pSecurityDescriptor);
  99. if( pDsObject )
  100. pDsObject->Release();
  101. return hr;
  102. }
  103. DWORD
  104. FormatStringID(LPTSTR *ppszResult, UINT idStr , ...)
  105. {
  106. va_list args;
  107. va_start(args, idStr);
  108. TCHAR szFormat[1024];
  109. LoadStringHelper(idStr, szFormat, ARRAYSIZE(szFormat));
  110. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  111. szFormat,
  112. 0,
  113. 0,
  114. (LPTSTR)ppszResult,
  115. 1,
  116. &args);
  117. }