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.

147 lines
5.2 KiB

  1. //
  2. // SafeFile.h
  3. //
  4. // Functions to help prevent opening unsafe files.
  5. //
  6. // History:
  7. //
  8. // 2002-03-18 KenSh Created
  9. //
  10. // Copyright (c) 2002 Microsoft Corporation
  11. //
  12. #pragma once
  13. //
  14. // You can override these allocators in your stdafx.h if necessary
  15. //
  16. #ifndef SafeFileMalloc
  17. #define SafeFileMalloc malloc
  18. #endif
  19. #ifndef SafeFileFree
  20. #define SafeFileFree(p) ((p) ? free(p) : NULL) // allow null to avoid confusion w/ "safe" in name
  21. #endif
  22. //
  23. // "Safe file flags", used by various public API's.
  24. //
  25. // Note that they don't overlap, to avoid errors where one function's
  26. // flags are passed to another function by accident.
  27. //
  28. // SafeCreateFile flags
  29. //
  30. #define SCF_ALLOW_NETWORK_DRIVE 0x00000001 // file can be on a network drive
  31. #define SCF_ALLOW_REMOVABLE_DRIVE 0x00000002 // file can be on a removable drive (incl. CD-ROM & others)
  32. #define SCF_ALLOW_ALTERNATE_STREAM 0x00000004 // allow filename to refer to alternate stream such as ":foo:$DATA"
  33. // SafePathCombine flags
  34. //
  35. #define SPC_FILE_MUST_EXIST 0x00000010 // return an error if path or file doesn't exist
  36. #define SPC_ALLOW_ALTERNATE_STREAM 0x00000020 // allow filename to refer to alternate stream such as ":foo:$DATA"
  37. // SafeFileCheckForReparsePoint flags
  38. //
  39. #define SRP_FILE_MUST_EXIST 0x00000100 // return an error if path or file doesn't exist
  40. // SafeDeleteFolderAndContents flags
  41. //
  42. #define SDF_ALLOW_NETWORK_DRIVE 0x00001000 // ok to delete files on a network drive
  43. #define SDF_DELETE_READONLY_FILES 0x00002000 // delete files even if read-only
  44. #define SDF_CONTINUE_IF_ERROR 0x00004000 // keep deleting files even if one fails
  45. //
  46. // Public function declarations. See SafeFile.cpp for detailed descriptions.
  47. //
  48. BOOL WINAPI IsFullPathName
  49. (
  50. IN LPCTSTR pszFileName, // full or relative path to a file
  51. OUT OPTIONAL BOOL* pfUNC = NULL, // TRUE path is UNC (int incl mapped drive)
  52. OUT OPTIONAL BOOL* pfExtendedSyntax = NULL // TRUE if path is \\?\ syntax
  53. );
  54. HRESULT WINAPI GetReparsePointType
  55. (
  56. IN LPCTSTR pszFileName, // full path to folder to check
  57. OUT DWORD* pdwReparsePointType // set to reparse point type, or 0 if none
  58. );
  59. HRESULT WINAPI SafeFileCheckForReparsePoint
  60. (
  61. IN LPCTSTR pszFileName, // full path of a file
  62. IN int nFirstUntrustedOffset, // char offset of first path component to check
  63. IN DWORD dwSafeFlags // zero or more SRP_* flags
  64. );
  65. HRESULT WINAPI SafePathCombine
  66. (
  67. OUT LPTSTR pszBuf, // buffer where combined path will be stored
  68. IN int cchBuf, // size of output buffer, in TCHARs
  69. IN LPCTSTR pszTrustedBasePath, // first half of path, all trusted
  70. IN LPCTSTR pszUntrustedFileName, // second half of path, not trusted
  71. IN DWORD dwSafeFlags // zero or more SPC_* flags
  72. );
  73. HRESULT WINAPI SafePathCombineAlloc
  74. (
  75. OUT LPTSTR* ppszResult, // ptr to newly alloc'd buffer stored here
  76. IN LPCTSTR pszTrustedBasePath, // first half of path, all trusted
  77. IN LPCTSTR pszUntrustedFileName, // second half of path, not trusted
  78. IN DWORD dwSafeFlags // zero or more SPC_* flags
  79. );
  80. HRESULT WINAPI SafeCreateFile
  81. (
  82. OUT HANDLE* phFileResult, // receives handle to opened file, or INVALID_HANDLE_VALUE
  83. IN DWORD dwSafeFlags, // zero or more SCF_* flags
  84. IN LPCTSTR pszFileName, // same as CreateFile
  85. IN DWORD dwDesiredAccess, // same as CreateFile
  86. IN DWORD dwShareMode, // same as CreateFile
  87. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes, // same as CreateFile
  88. IN DWORD dwCreationDisposition, // same as CreateFile
  89. IN DWORD dwFlagsAndAttributes, // same as CreateFile + (SECURITY_SQOS_PRESENT|SECURITY_ANONYMOUS)
  90. IN HANDLE hTemplateFile // same as CreateFile
  91. );
  92. HRESULT WINAPI SafeRemoveFileAttributes
  93. (
  94. IN LPCTSTR pszFileName, // full path to file whose attributes we will change
  95. IN DWORD dwCurAttrib, // current attributes of the file
  96. IN DWORD dwRemoveAttrib // attribute bits to remove
  97. );
  98. HRESULT WINAPI SafeDeleteFolderAndContents
  99. (
  100. IN LPCTSTR pszFolderToDelete, // full path of folder to delete
  101. IN DWORD dwSafeFlags // zero or more SDF_* flags
  102. );
  103. //
  104. // Limited ansi/unicode support
  105. //
  106. #ifdef UNICODE
  107. #define IsFullPathNameW IsFullPathName
  108. #define GetReparsePointTypeW GetReparsePointType
  109. #define SafeFileCheckForReparsePointW SafeFileCheckForReparsePoint
  110. #define SafePathCombineW SafePathCombine
  111. #define SafePathCombineAllocW SafePathCombineAlloc
  112. #define SafeCreateFileW SafeCreateFile
  113. #define SafeRemoveFileAttributesW SafeRemoveFileAttributes
  114. #define SafeDeleteFolderAndContentsW SafeDeleteFolderAndContents
  115. #else // !UNICODE
  116. #define IsFullPathNameA IsFullPathName
  117. #define GetReparsePointTypeA GetReparsePointType
  118. #define SafeFileCheckForReparsePointA SafeFileCheckForReparsePoint
  119. #define SafePathCombineA SafePathCombine
  120. #define SafePathCombineAllocA SafePathCombineAlloc
  121. #define SafeCreateFileA SafeCreateFile
  122. #define SafeRemoveFileAttributesA SafeRemoveFileAttributes
  123. #define SafeDeleteFolderAndContentsA SafeDeleteFolderAndContents
  124. #endif // !UNICODE