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.

128 lines
3.7 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. hr = ::GetSDForDsObjectPath(IN const_cast<LPWSTR>(pszObjectLADPPath),
  36. NULL,
  37. &pSecurityDescriptor);
  38. if(FAILED(hr))
  39. {
  40. WCHAR szMsg[512];
  41. LoadStringHelper(IDS_DELEGWIZ_ERR_GET_SEC_INFO, szMsg, 512);
  42. DisplayMessageBox(hwndParent, szMsg);
  43. goto exit_gracefully;
  44. }
  45. // Bind to the object
  46. hr = ADsOpenObjectHelper(pszObjectLADPPath,
  47. IID_IDirectoryObject,
  48. ADS_FAST_BIND,
  49. (LPVOID*)&pDsObject);
  50. if( hr != S_OK )
  51. goto exit_gracefully;
  52. // Read the sDRightsEffective property to determine writability
  53. pDsObject->GetObjectAttributes( &pProp,
  54. 1,
  55. &pSDRightsInfo,
  56. &dwAttributesReturned);
  57. if (pSDRightsInfo)
  58. {
  59. si = pSDRightsInfo->pADsValues->Integer;
  60. FreeADsMem(pSDRightsInfo);
  61. }
  62. else
  63. {
  64. //
  65. // Note that GetObjectAttributes commonly returns S_OK even when
  66. // it fails, so the HRESULT is basically useless here.
  67. //
  68. // This can fail if we don't have read_property access, which can
  69. // happen when an admin is trying to restore access to an object
  70. // that has had all access removed or denied
  71. //
  72. // Assume we can write the Owner and DACL. If not, the worst that
  73. // happens is the user gets an "Access Denied" message when trying
  74. // to save changes.
  75. //
  76. si = DACL_SECURITY_INFORMATION;
  77. }
  78. if( !(si & DACL_SECURITY_INFORMATION) )
  79. {
  80. WCHAR szMsg[512];
  81. LoadStringHelper(IDS_DELEGWIZ_ERR_ACCESS_DENIED, szMsg, 512);
  82. DisplayMessageBox(hwndParent, szMsg);
  83. //NTRAID#NTBUG9-530206-2002/06/18-ronmart-PREFAST: Use E_FAIL
  84. //hr = !S_OK;
  85. hr = E_FAIL;
  86. }
  87. exit_gracefully:
  88. if( pSecurityDescriptor )
  89. LocalFree(pSecurityDescriptor);
  90. if( pDsObject )
  91. pDsObject->Release();
  92. return hr;
  93. }
  94. DWORD
  95. FormatStringID(LPTSTR *ppszResult, UINT idStr , ...)
  96. {
  97. va_list args;
  98. va_start(args, idStr);
  99. TCHAR szFormat[1024];
  100. LoadStringHelper(idStr, szFormat, ARRAYSIZE(szFormat));
  101. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  102. szFormat,
  103. 0,
  104. 0,
  105. (LPTSTR)ppszResult,
  106. 1,
  107. &args);
  108. }