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.

68 lines
2.7 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Function: HasSpecifiedAccessToFileOrDir
  4. //
  5. // Synopsis: This function checks to see if the current user (or any of the groups
  6. // that the user belongs to) has the requested access to the given
  7. // file or directory object. If the user has access then the function
  8. // returns TRUE, otherwise FALSE.
  9. //
  10. // Arguments: LPTSTR pszFile - full path to the file or dir to check permissions for
  11. // DWORD dwDesiredAccess - the desired access to check for
  12. //
  13. // Returns: BOOL - TRUE if access is granted, FALSE otherwise
  14. //
  15. // History: quintinb Created 7/21/99
  16. // quintinb Rewrote to use AccessCheck (389246) 08/18/99
  17. // quintinb made common to cmak and cmdial 03/03/00
  18. // quintinb Rewrote using CreateFile 05/19/00
  19. //
  20. //+----------------------------------------------------------------------------
  21. BOOL HasSpecifiedAccessToFileOrDir(LPTSTR pszFile, DWORD dwDesiredAccess)
  22. {
  23. BOOL bReturn = FALSE;
  24. if (pszFile && (TEXT('\0') != pszFile[0]))
  25. {
  26. if (OS_NT)
  27. {
  28. //
  29. // Use FILE_FLAG_BACKUP_SEMANTICS so that we can open directories as well as files.
  30. //
  31. HANDLE hFileOrDir = CreateFileU(pszFile, dwDesiredAccess,
  32. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  33. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  34. if (INVALID_HANDLE_VALUE != hFileOrDir)
  35. {
  36. bReturn = TRUE;
  37. CloseHandle(hFileOrDir);
  38. }
  39. }
  40. else
  41. {
  42. //
  43. // There is no NTFS on win9x and thus all users will have access. Furthermore, FILE_FLAG_BACKUP_SEMANTICS
  44. // isn't supported on win9x and thus CreateFile will return INVALID_HANDLE_VALUE.
  45. //
  46. LPSTR pszAnsiFile = WzToSzWithAlloc(pszFile);
  47. if (pszAnsiFile)
  48. {
  49. DWORD dwAttrib = GetFileAttributesA(pszAnsiFile);
  50. //
  51. // Note that we are only checking for failure of the API (-1) and that the
  52. // file is not marked Read only (+r). I checked +s, +h, etc. and found that
  53. // only the read only attribute prevented CM from writing to the cmp.
  54. //
  55. bReturn = ((-1 != dwAttrib) && (0 == (FILE_ATTRIBUTE_READONLY & dwAttrib)));
  56. CmFree(pszAnsiFile);
  57. }
  58. }
  59. }
  60. return bReturn;
  61. }