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.

261 lines
6.2 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. datafilt.c
  5. Abstract:
  6. Routines to filter registry values.
  7. Author:
  8. Jim Schmidt (jimschm) 11-Mar-1997
  9. Revision History:
  10. jimschm 23-Sep-1998 Debugging message fix
  11. jimschm 10-Sep-1998 Mapping mechanism
  12. jimschm 25-Mar-1998 Added FilterRegValue
  13. --*/
  14. #include "pch.h"
  15. #include "mergep.h"
  16. #define DBG_DATAFILTER "Data Filter"
  17. VOID
  18. SpecialFileFixup (
  19. PTSTR Buffer
  20. )
  21. {
  22. PTSTR p;
  23. // check for rundll32 with no .exe extension
  24. p = (PTSTR) _tcsistr (Buffer, TEXT("rundll32"));
  25. if (p) {
  26. // replace foo\rundll32 <args> with rundll32.exe <args>
  27. p += 8; // go beyond the characters "rundll32"
  28. if (_tcsnextc (p) != TEXT('.')) {
  29. // p points to start of arg list or a nul if no args
  30. MoveMemory (Buffer + 12, p, SizeOfString (p));
  31. _tcsncpy (Buffer, TEXT("rundll32.exe"), 12);
  32. }
  33. return;
  34. }
  35. }
  36. VOID
  37. AddQuotesIfNecessary (
  38. PTSTR File
  39. )
  40. {
  41. if (_tcspbrk (File, TEXT(" ;,"))) {
  42. MoveMemory (File + 1, File, SizeOfString (File));
  43. *File = TEXT('\"');
  44. StringCat (File, TEXT("\""));
  45. }
  46. }
  47. BOOL
  48. CanFileBeInRegistryData (
  49. IN PCTSTR Data
  50. )
  51. {
  52. //
  53. // Scan registry data for a colon or a dot
  54. //
  55. if (_tcspbrk (Data, TEXT(":.\\"))) {
  56. return TRUE;
  57. }
  58. return FALSE;
  59. }
  60. BOOL
  61. FilterObject (
  62. IN OUT PDATAOBJECT SrcObPtr
  63. )
  64. {
  65. if (IsWin95Object (SrcObPtr) &&
  66. (SrcObPtr->Value.Size < MAX_TCHAR_PATH * sizeof (TCHAR)) &&
  67. (SrcObPtr->Value.Size > 4 * sizeof (TCHAR)) // must have drive letter
  68. ) {
  69. TCHAR Buffer[MAX_CMDLINE];
  70. switch (SrcObPtr->Type) {
  71. case REG_NONE:
  72. if (*((PCTSTR) (SrcObPtr->Value.Buffer + SrcObPtr->Value.Size - sizeof (TCHAR)))) {
  73. // don't process unless it is nul-terminated
  74. break;
  75. }
  76. // fall through
  77. case REG_SZ:
  78. // Require the data to contain a basic symbol that a path or file requires
  79. if (!CanFileBeInRegistryData ((PCTSTR) SrcObPtr->Value.Buffer)) {
  80. break;
  81. }
  82. _tcssafecpy (Buffer, (PCTSTR) SrcObPtr->Value.Buffer, MAX_CMDLINE);
  83. if (ConvertWin9xCmdLine (Buffer, DEBUGENCODER(SrcObPtr), NULL)) {
  84. // cmd line has changed
  85. ReplaceValue (SrcObPtr, (PBYTE) Buffer, SizeOfString (Buffer));
  86. }
  87. break;
  88. case REG_EXPAND_SZ:
  89. ExpandEnvironmentStrings ((PCTSTR) SrcObPtr->Value.Buffer, Buffer, sizeof (Buffer) / sizeof (TCHAR));
  90. // Require the data to contain a basic symbol that a path or file requires
  91. if (!CanFileBeInRegistryData ((PCTSTR) Buffer)) {
  92. break;
  93. }
  94. if (ConvertWin9xCmdLine (Buffer, DEBUGENCODER(SrcObPtr), NULL)) {
  95. // cmd line has changed
  96. DEBUGMSG ((DBG_VERBOSE, "%s was expanded from %s", Buffer, SrcObPtr->Value.Buffer));
  97. ReplaceValue (SrcObPtr, (PBYTE) Buffer, SizeOfString (Buffer));
  98. }
  99. break;
  100. }
  101. }
  102. return TRUE;
  103. }
  104. PBYTE
  105. FilterRegValue (
  106. IN PBYTE Data,
  107. IN DWORD DataSize,
  108. IN DWORD DataType,
  109. IN PCTSTR KeyForDbgMsg, OPTIONAL
  110. OUT PDWORD NewDataSize
  111. )
  112. /*++
  113. Routine Description:
  114. FilterRegValue examines the specified registry data and updates any paths
  115. that have moved.
  116. Arguments:
  117. Data - Specifies a ReuseAlloc'd buffer containing the registry value data.
  118. DataSize - Specifies the size of the registry value data, as returned by
  119. the registry APIs.
  120. DataType - Specifies the type of the registry data, as specified by the
  121. registry APIs.
  122. KeyForDbgMsg - Specifies registry key, used only for debug messages
  123. NewDataSize - Receives the size of the data returned
  124. Return Value:
  125. Returns Data if no changes were made, or a reallocated pointer if changes
  126. were made. If NULL is returned, an error occurred.
  127. --*/
  128. {
  129. TCHAR Buffer[MAX_CMDLINE];
  130. PBYTE NewData = Data;
  131. DWORD Size;
  132. *NewDataSize = DataSize;
  133. switch (DataType) {
  134. case REG_NONE:
  135. if (*((PCTSTR) (Data + DataSize - sizeof (TCHAR)))) {
  136. // don't process unless it is nul-terminated
  137. break;
  138. }
  139. // fall through
  140. case REG_SZ:
  141. // Require the data to contain a basic symbol that a path or file requires
  142. if (!CanFileBeInRegistryData ((PCTSTR) Data)) {
  143. break;
  144. }
  145. _tcssafecpy (Buffer, (PCTSTR) Data, MAX_CMDLINE);
  146. if (ConvertWin9xCmdLine (Buffer, KeyForDbgMsg, NULL)) {
  147. // cmd line has changed
  148. Size = SizeOfString (Buffer);
  149. NewData = (PBYTE) ReuseAlloc (g_hHeap, Data, Size);
  150. if (NewData) {
  151. StringCopy ((PTSTR) NewData, Buffer);
  152. *NewDataSize = Size;
  153. } else {
  154. NewData = Data;
  155. DEBUGMSG ((DBG_ERROR, "FilterRegValue: ReuseAlloc failed"));
  156. }
  157. }
  158. break;
  159. case REG_EXPAND_SZ:
  160. ExpandEnvironmentStrings ((PCTSTR) Data, Buffer, sizeof (Buffer) / sizeof (TCHAR));
  161. // Require the data to contain a basic symbol that a path or file requires
  162. if (!CanFileBeInRegistryData ((PCTSTR) Buffer)) {
  163. break;
  164. }
  165. if (ConvertWin9xCmdLine (Buffer, KeyForDbgMsg, NULL)) {
  166. // cmd line has changed
  167. DEBUGMSG ((DBG_VERBOSE, "%s was expanded from %s", Buffer, Data));
  168. Size = SizeOfString (Buffer);
  169. NewData = (PBYTE) ReuseAlloc (g_hHeap, Data, Size);
  170. if (NewData) {
  171. StringCopy ((PTSTR) NewData, Buffer);
  172. *NewDataSize = Size;
  173. } else {
  174. NewData = Data;
  175. DEBUGMSG ((DBG_ERROR, "FilterRegValue: ReuseAlloc failed"));
  176. }
  177. }
  178. break;
  179. }
  180. return NewData;
  181. }