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.

238 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. persistLayers.c
  5. Abstract:
  6. This module implements routines to persist layer
  7. information for shortcuts.
  8. Author:
  9. dmunsil created sometime in 2000
  10. Revision History:
  11. --*/
  12. #include "apphelp.h"
  13. BOOL
  14. AllowPermLayer(
  15. IN LPCWSTR pwszPath // path to the file to check whether you
  16. // can set a permanent layer on
  17. )
  18. /*++
  19. Return: TRUE if a permanent setting of a layer is allowed for the
  20. specified file, FALSE otherwise.
  21. Desc: Returns wether a permanent layer setting can be persisted
  22. for the specified file.
  23. --*/
  24. {
  25. WCHAR wszDrive[5];
  26. UINT unType;
  27. if (pwszPath == NULL) {
  28. DBGPRINT((sdlError, "AllowPermLayer", "Invalid argument\n"));
  29. return FALSE;
  30. }
  31. if (pwszPath[1] != L':' && pwszPath[1] != L'\\') {
  32. //
  33. // Not a path we recognize.
  34. //
  35. DBGPRINT((sdlInfo,
  36. "AllowPermLayer",
  37. "\"%S\" not a full path we can operate on.\n",
  38. pwszPath));
  39. return FALSE;
  40. }
  41. if (pwszPath[1] == L'\\') {
  42. //
  43. // Network path. Not allowed.
  44. //
  45. DBGPRINT((sdlInfo,
  46. "AllowPermLayer",
  47. "\"%S\" is a network path.\n",
  48. pwszPath));
  49. return FALSE;
  50. }
  51. wcscpy(wszDrive, L"c:\\");
  52. wszDrive[0] = pwszPath[0];
  53. unType = GetDriveTypeW(wszDrive);
  54. if (unType == DRIVE_REMOTE) {
  55. DBGPRINT((sdlInfo,
  56. "AllowPermLayer",
  57. "\"%S\" is on CDROM or other removable media.\n",
  58. pwszPath));
  59. return FALSE;
  60. }
  61. return TRUE;
  62. }
  63. //
  64. // Semi-exported api from SDBAPI (ntbase.c)
  65. //
  66. BOOL
  67. SDBAPI
  68. SdbpGetLongPathName(
  69. IN LPCWSTR pwszPath,
  70. OUT PRTL_UNICODE_STRING_BUFFER pBuffer
  71. );
  72. BOOL
  73. SDBAPI
  74. ApphelpUpdateCacheEntry(
  75. LPCWSTR pwszPath, // nt path
  76. HANDLE hFile, // file handle
  77. BOOL bDeleteEntry, // TRUE if we are to delete the entry
  78. BOOL bNTPath // if TRUE -- NT path, FALSE - dos path
  79. )
  80. {
  81. RTL_UNICODE_STRING_BUFFER Path;
  82. UCHAR PathBuffer[MAX_PATH*2];
  83. BOOL TranslationStatus;
  84. BOOL bSuccess = FALSE;
  85. UNICODE_STRING NtPath = { 0 };
  86. UNICODE_STRING DosPath = { 0 };
  87. BOOL bFreeNtPath = FALSE;
  88. NTSTATUS Status;
  89. RtlInitUnicodeStringBuffer(&Path, PathBuffer, sizeof(PathBuffer));
  90. if (bNTPath) { // if this is NT Path name, convert to dos
  91. RtlInitUnicodeString(&NtPath, pwszPath);
  92. Status = RtlAssignUnicodeStringBuffer(&Path, &NtPath); // NT path
  93. if (!NT_SUCCESS(Status)) {
  94. DBGPRINT((sdlError, "ApphelpUpdateCacheEntry", "Failed to allocate temp buffer for %s\n", pwszPath));
  95. goto Cleanup;
  96. }
  97. Status = RtlNtPathNameToDosPathName(0, &Path, NULL, NULL);
  98. if (!NT_SUCCESS(Status)) {
  99. DBGPRINT((sdlError, "ApphelpUpdateCacheEntry",
  100. "Failed to convert Path \"%s\" to dos path status 0x%lx\n", pwszPath, Status));
  101. goto Cleanup;
  102. }
  103. Status = RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, &Path.String, &DosPath);
  104. if (!NT_SUCCESS(Status)) {
  105. DBGPRINT((sdlError, "ApphelpUpdateCacheEntry",
  106. "Failed to Duplicate Path \"%s\" status 0x%lx\n", Path.String.Buffer, Status));
  107. goto Cleanup;
  108. }
  109. pwszPath = DosPath.Buffer;
  110. }
  111. //
  112. // at this point we have both NT and DOS path - buffer is available to us now
  113. //
  114. if (!SdbpGetLongPathName(pwszPath, &Path)) { // in - DosPath // out -- Long DOS Path
  115. goto Cleanup;
  116. }
  117. //
  118. // convert long path name to NT Path name
  119. //
  120. TranslationStatus = RtlDosPathNameToNtPathName_U(Path.String.Buffer,
  121. &NtPath,
  122. NULL,
  123. NULL);
  124. if (!TranslationStatus) {
  125. DBGPRINT((sdlError, "ApphelpUpdateCacheEntry",
  126. "Failed to Convert Path \"%s\" to NT path\n", Path.String.Buffer));
  127. goto Cleanup;
  128. }
  129. //
  130. // update the cache (use NT Path here)
  131. //
  132. bSuccess = BaseUpdateAppcompatCache(NtPath.Buffer, hFile, bDeleteEntry);
  133. //
  134. // we only free this string when we successfully navigated through RtlDosPathNameToNtPathName_U
  135. //
  136. RtlFreeUnicodeString(&NtPath);
  137. Cleanup:
  138. if (bNTPath) {
  139. //
  140. // Free DosPath if we had to convert from NT Path to DosPath first
  141. //
  142. RtlFreeUnicodeString(&DosPath);
  143. }
  144. RtlFreeUnicodeStringBuffer(&Path);
  145. return bSuccess;
  146. }
  147. BOOL
  148. SetPermLayers(
  149. IN LPCWSTR pwszPath, // path to the file to set a permanent layer on (dos path)
  150. IN LPCWSTR pwszLayers, // layers to apply to the file, separated by spaces
  151. IN BOOL bMachine // TRUE if the layers should be persisted per machine
  152. )
  153. /*++
  154. Return: TRUE on success, FALSE otherwise.
  155. Desc: Sets a permanent layer setting for the specified file.
  156. --*/
  157. {
  158. BOOL bSuccess = FALSE;
  159. if (pwszPath == NULL || pwszLayers == NULL) {
  160. DBGPRINT((sdlError, "SetPermLayers", "Invalid argument\n"));
  161. return FALSE;
  162. }
  163. bSuccess = SdbSetPermLayerKeys(pwszPath, pwszLayers, bMachine);
  164. // we do not care whether we were successful in the call above, clean the
  165. // cache always (just in case)
  166. ApphelpUpdateCacheEntry(pwszPath, INVALID_HANDLE_VALUE, TRUE, FALSE);
  167. return bSuccess;
  168. }
  169. BOOL
  170. GetPermLayers(
  171. IN LPCWSTR pwszPath, // path to the file to set a permanent layer on
  172. OUT LPWSTR pwszLayers, // layers to apply to the file, separated by spaces
  173. OUT DWORD* pdwBytes, // input: number of bytes available; output is number
  174. // of bytes needed.
  175. IN DWORD dwFlags
  176. )
  177. /*++
  178. Return: TRUE on success, FALSE otherwise.
  179. Desc: Returns the permanent layer setting for the specified file.
  180. --*/
  181. {
  182. if (pwszPath == NULL || pwszLayers == NULL || pdwBytes == NULL) {
  183. DBGPRINT((sdlError, "GetPermLayers", "Invalid argument\n"));
  184. return FALSE;
  185. }
  186. return SdbGetPermLayerKeys(pwszPath, pwszLayers, pdwBytes, dwFlags);
  187. }