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.

508 lines
12 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. LUA_TrackFS.cpp
  5. Abstract:
  6. Track the directories the app looks at and record them into a file.
  7. Notes:
  8. This is a general purpose shim.
  9. History:
  10. 04/04/2001 maonis Created
  11. --*/
  12. #include "precomp.h"
  13. #include "utils.h"
  14. HFILE LuatOpenFile(LPCSTR lpFileName, LPOFSTRUCT lpReOpenBuff, UINT uStyle);
  15. HFILE Luat_lopen(LPCSTR, int);
  16. HFILE Luat_lcreat(LPCSTR, int);
  17. class CTrackObject;
  18. extern CTrackObject g_td;
  19. IMPLEMENT_SHIM_BEGIN(LUATrackFS)
  20. #include "ShimHookMacro.h"
  21. APIHOOK_ENUM_BEGIN
  22. APIHOOK_ENUM_ENTRY(CreateFileA)
  23. APIHOOK_ENUM_ENTRY(CreateFileW)
  24. APIHOOK_ENUM_ENTRY(CopyFileA)
  25. APIHOOK_ENUM_ENTRY(CopyFileW)
  26. APIHOOK_ENUM_ENTRY(OpenFile)
  27. APIHOOK_ENUM_ENTRY(_lopen)
  28. APIHOOK_ENUM_ENTRY(_lcreat)
  29. APIHOOK_ENUM_ENTRY(CreateDirectoryA)
  30. APIHOOK_ENUM_ENTRY(CreateDirectoryW)
  31. APIHOOK_ENUM_ENTRY(SetFileAttributesA)
  32. APIHOOK_ENUM_ENTRY(SetFileAttributesW)
  33. APIHOOK_ENUM_ENTRY(DeleteFileA)
  34. APIHOOK_ENUM_ENTRY(DeleteFileW)
  35. APIHOOK_ENUM_ENTRY(MoveFileA)
  36. APIHOOK_ENUM_ENTRY(MoveFileW)
  37. APIHOOK_ENUM_ENTRY(RemoveDirectoryA)
  38. APIHOOK_ENUM_ENTRY(RemoveDirectoryW)
  39. APIHOOK_ENUM_ENTRY(GetTempFileNameA)
  40. APIHOOK_ENUM_ENTRY(GetTempFileNameW)
  41. APIHOOK_ENUM_ENTRY(WritePrivateProfileStringA)
  42. APIHOOK_ENUM_ENTRY(WritePrivateProfileStringW)
  43. APIHOOK_ENUM_ENTRY(WritePrivateProfileSectionA)
  44. APIHOOK_ENUM_ENTRY(WritePrivateProfileSectionW)
  45. APIHOOK_ENUM_ENTRY(WritePrivateProfileStructA)
  46. APIHOOK_ENUM_ENTRY(WritePrivateProfileStructW)
  47. APIHOOK_ENUM_END
  48. HANDLE
  49. APIHOOK(CreateFileW)(
  50. LPCWSTR lpFileName,
  51. DWORD dwDesiredAccess,
  52. DWORD dwShareMode,
  53. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  54. DWORD dwCreationDisposition,
  55. DWORD dwFlagsAndAttributes,
  56. HANDLE hTemplateFile
  57. )
  58. {
  59. return LuatCreateFileW(
  60. lpFileName,
  61. dwDesiredAccess,
  62. dwShareMode,
  63. lpSecurityAttributes,
  64. dwCreationDisposition,
  65. dwFlagsAndAttributes,
  66. hTemplateFile);
  67. }
  68. HANDLE
  69. APIHOOK(CreateFileA)(
  70. LPCSTR lpFileName,
  71. DWORD dwDesiredAccess,
  72. DWORD dwShareMode,
  73. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  74. DWORD dwCreationDisposition,
  75. DWORD dwFlagsAndAttributes,
  76. HANDLE hTemplateFile
  77. )
  78. {
  79. STRINGA2W wstrFileName(lpFileName);
  80. return (wstrFileName.m_fIsOutOfMemory ?
  81. ORIGINAL_API(CreateFileA)(
  82. lpFileName,
  83. dwDesiredAccess,
  84. dwShareMode,
  85. lpSecurityAttributes,
  86. dwCreationDisposition,
  87. dwFlagsAndAttributes,
  88. hTemplateFile) :
  89. LuatCreateFileW(
  90. wstrFileName,
  91. dwDesiredAccess,
  92. dwShareMode,
  93. lpSecurityAttributes,
  94. dwCreationDisposition,
  95. dwFlagsAndAttributes,
  96. hTemplateFile));
  97. }
  98. BOOL
  99. APIHOOK(CopyFileW)(
  100. LPCWSTR lpExistingFileName,
  101. LPCWSTR lpNewFileName,
  102. BOOL bFailIfExists
  103. )
  104. {
  105. return LuatCopyFileW(lpExistingFileName, lpNewFileName, bFailIfExists);
  106. }
  107. BOOL
  108. APIHOOK(CopyFileA)(
  109. LPCSTR lpExistingFileName,
  110. LPCSTR lpNewFileName,
  111. BOOL bFailIfExists
  112. )
  113. {
  114. STRINGA2W wstrExistingFileName(lpExistingFileName);
  115. STRINGA2W wstrNewFileName(lpNewFileName);
  116. return ((wstrExistingFileName.m_fIsOutOfMemory || wstrNewFileName.m_fIsOutOfMemory) ?
  117. ORIGINAL_API(CopyFileA)(lpExistingFileName, lpNewFileName, bFailIfExists) :
  118. LuatCopyFileW(wstrExistingFileName, wstrNewFileName, bFailIfExists));
  119. }
  120. HFILE
  121. APIHOOK(OpenFile)(
  122. LPCSTR lpFileName,
  123. LPOFSTRUCT lpReOpenBuff,
  124. UINT uStyle
  125. )
  126. {
  127. return LuatOpenFile(lpFileName, lpReOpenBuff, uStyle);
  128. }
  129. HFILE
  130. APIHOOK(_lopen)(
  131. LPCSTR lpPathName,
  132. int iReadWrite
  133. )
  134. {
  135. return Luat_lopen(lpPathName, iReadWrite);
  136. }
  137. HFILE
  138. APIHOOK(_lcreat)(
  139. LPCSTR lpPathName,
  140. int iAttribute
  141. )
  142. {
  143. return Luat_lcreat(lpPathName, iAttribute);
  144. }
  145. BOOL
  146. APIHOOK(CreateDirectoryW)(
  147. LPCWSTR lpPathName,
  148. LPSECURITY_ATTRIBUTES lpSecurityAttributes
  149. )
  150. {
  151. return LuatCreateDirectoryW(lpPathName, lpSecurityAttributes);
  152. }
  153. BOOL
  154. APIHOOK(CreateDirectoryA)(
  155. LPCSTR lpPathName,
  156. LPSECURITY_ATTRIBUTES lpSecurityAttributes
  157. )
  158. {
  159. STRINGA2W wstrPathName(lpPathName);
  160. return (wstrPathName.m_fIsOutOfMemory ?
  161. ORIGINAL_API(CreateDirectoryA)(lpPathName, lpSecurityAttributes) :
  162. LuatCreateDirectoryW(wstrPathName, lpSecurityAttributes));
  163. }
  164. BOOL
  165. APIHOOK(SetFileAttributesW)(
  166. LPCWSTR lpFileName,
  167. DWORD dwFileAttributes
  168. )
  169. {
  170. return LuatSetFileAttributesW(lpFileName, dwFileAttributes);
  171. }
  172. BOOL
  173. APIHOOK(SetFileAttributesA)(
  174. LPCSTR lpFileName,
  175. DWORD dwFileAttributes
  176. )
  177. {
  178. STRINGA2W wstrFileName(lpFileName);
  179. return (wstrFileName.m_fIsOutOfMemory ?
  180. ORIGINAL_API(SetFileAttributesA)(lpFileName, dwFileAttributes) :
  181. LuatSetFileAttributesW(wstrFileName, dwFileAttributes));
  182. }
  183. BOOL
  184. APIHOOK(DeleteFileW)(
  185. LPCWSTR lpFileName
  186. )
  187. {
  188. return LuatDeleteFileW(lpFileName);
  189. }
  190. BOOL
  191. APIHOOK(DeleteFileA)(
  192. LPCSTR lpFileName
  193. )
  194. {
  195. STRINGA2W wstrFileName(lpFileName);
  196. return (wstrFileName.m_fIsOutOfMemory ?
  197. ORIGINAL_API(DeleteFileA)(lpFileName) :
  198. LuatDeleteFileW(wstrFileName));
  199. }
  200. BOOL
  201. APIHOOK(MoveFileW)(
  202. LPCWSTR lpExistingFileName,
  203. LPCWSTR lpNewFileName
  204. )
  205. {
  206. return LuatMoveFileW(lpExistingFileName, lpNewFileName);
  207. }
  208. BOOL
  209. APIHOOK(MoveFileA)(
  210. LPCSTR lpExistingFileName,
  211. LPCSTR lpNewFileName
  212. )
  213. {
  214. STRINGA2W wstrExistingFileName(lpExistingFileName);
  215. STRINGA2W wstrNewFileName(lpNewFileName);
  216. return ((wstrExistingFileName.m_fIsOutOfMemory || wstrNewFileName.m_fIsOutOfMemory) ?
  217. ORIGINAL_API(MoveFileA)(lpExistingFileName, lpNewFileName) :
  218. LuatMoveFileW(wstrExistingFileName, wstrNewFileName));
  219. }
  220. BOOL
  221. APIHOOK(RemoveDirectoryW)(
  222. LPCWSTR lpPathName
  223. )
  224. {
  225. return LuatRemoveDirectoryW(lpPathName);
  226. }
  227. BOOL
  228. APIHOOK(RemoveDirectoryA)(
  229. LPCSTR lpPathName
  230. )
  231. {
  232. STRINGA2W wstrPathName(lpPathName);
  233. return (wstrPathName.m_fIsOutOfMemory ?
  234. ORIGINAL_API(RemoveDirectoryA)(lpPathName) :
  235. LuatRemoveDirectoryW(wstrPathName));
  236. }
  237. UINT
  238. APIHOOK(GetTempFileNameW)(
  239. LPCWSTR lpPathName,
  240. LPCWSTR lpPrefixString,
  241. UINT uUnique,
  242. LPWSTR lpTempFileName
  243. )
  244. {
  245. return LuatGetTempFileNameW(lpPathName, lpPrefixString, uUnique, lpTempFileName);
  246. }
  247. UINT
  248. APIHOOK(GetTempFileNameA)(
  249. LPCSTR lpPathName,
  250. LPCSTR lpPrefixString,
  251. UINT uUnique,
  252. LPSTR lpTempFileName
  253. )
  254. {
  255. STRINGA2W wstrPathName(lpPathName);
  256. STRINGA2W wstrPrefixString(lpPrefixString);
  257. if (wstrPathName.m_fIsOutOfMemory || wstrPrefixString.m_fIsOutOfMemory)
  258. {
  259. return ORIGINAL_API(GetTempFileNameA)(
  260. lpPathName,
  261. lpPrefixString,
  262. uUnique,
  263. lpTempFileName);
  264. }
  265. WCHAR wstrTempFileName[MAX_PATH];
  266. UINT uiRes;
  267. if (uiRes = LuatGetTempFileNameW(
  268. wstrPathName,
  269. wstrPrefixString,
  270. uUnique,
  271. wstrTempFileName))
  272. {
  273. UnicodeToAnsi(wstrTempFileName, lpTempFileName);
  274. }
  275. return uiRes;
  276. }
  277. BOOL
  278. APIHOOK(WritePrivateProfileStringW)(
  279. LPCWSTR lpAppName,
  280. LPCWSTR lpKeyName,
  281. LPCWSTR lpString,
  282. LPCWSTR lpFileName
  283. )
  284. {
  285. return LuatWritePrivateProfileStringW(
  286. lpAppName,
  287. lpKeyName,
  288. lpString,
  289. lpFileName);
  290. }
  291. BOOL
  292. APIHOOK(WritePrivateProfileStringA)(
  293. LPCSTR lpAppName,
  294. LPCSTR lpKeyName,
  295. LPCSTR lpString,
  296. LPCSTR lpFileName
  297. )
  298. {
  299. STRINGA2W wstrAppName(lpAppName);
  300. STRINGA2W wstrKeyName(lpKeyName);
  301. STRINGA2W wstrString(lpString);
  302. STRINGA2W wstrFileName(lpFileName);
  303. return ((wstrAppName.m_fIsOutOfMemory ||
  304. wstrKeyName.m_fIsOutOfMemory ||
  305. wstrString.m_fIsOutOfMemory ||
  306. wstrFileName.m_fIsOutOfMemory) ?
  307. ORIGINAL_API(WritePrivateProfileStringA)(
  308. lpAppName,
  309. lpKeyName,
  310. lpString,
  311. lpFileName) :
  312. LuatWritePrivateProfileStringW(
  313. wstrAppName,
  314. wstrKeyName,
  315. wstrString,
  316. wstrFileName));
  317. }
  318. BOOL
  319. APIHOOK(WritePrivateProfileSectionW)(
  320. LPCWSTR lpAppName,
  321. LPCWSTR lpString,
  322. LPCWSTR lpFileName
  323. )
  324. {
  325. return LuatWritePrivateProfileSectionW(
  326. lpAppName,
  327. lpString,
  328. lpFileName);
  329. }
  330. BOOL
  331. APIHOOK(WritePrivateProfileSectionA)(
  332. LPCSTR lpAppName,
  333. LPCSTR lpString,
  334. LPCSTR lpFileName
  335. )
  336. {
  337. STRINGA2W wstrAppName(lpAppName);
  338. STRINGA2W wstrString(lpString);
  339. STRINGA2W wstrFileName(lpFileName);
  340. return ((wstrAppName.m_fIsOutOfMemory ||
  341. wstrString.m_fIsOutOfMemory ||
  342. wstrFileName.m_fIsOutOfMemory) ?
  343. ORIGINAL_API(WritePrivateProfileSectionA)(
  344. lpAppName,
  345. lpString,
  346. lpFileName) :
  347. LuatWritePrivateProfileSectionW(
  348. wstrAppName,
  349. wstrString,
  350. wstrFileName));
  351. }
  352. BOOL
  353. APIHOOK(WritePrivateProfileStructW)(
  354. LPCWSTR lpszSection,
  355. LPCWSTR lpszKey,
  356. LPVOID lpStruct,
  357. UINT uSizeStruct,
  358. LPCWSTR szFile
  359. )
  360. {
  361. return LuatWritePrivateProfileStructW(
  362. lpszSection,
  363. lpszKey,
  364. lpStruct,
  365. uSizeStruct,
  366. szFile);
  367. }
  368. BOOL
  369. APIHOOK(WritePrivateProfileStructA)(
  370. LPCSTR lpszSection,
  371. LPCSTR lpszKey,
  372. LPVOID lpStruct,
  373. UINT uSizeStruct,
  374. LPCSTR szFile
  375. )
  376. {
  377. STRINGA2W wstrSection(lpszSection);
  378. STRINGA2W wstrKey(lpszKey);
  379. STRINGA2W wstrFile(szFile);
  380. return ((wstrSection.m_fIsOutOfMemory ||
  381. wstrKey.m_fIsOutOfMemory ||
  382. wstrFile.m_fIsOutOfMemory) ?
  383. ORIGINAL_API(WritePrivateProfileStructA)(
  384. lpszSection,
  385. lpszKey,
  386. lpStruct,
  387. uSizeStruct,
  388. szFile) :
  389. LuatWritePrivateProfileStructW(
  390. wstrSection,
  391. wstrKey,
  392. lpStruct,
  393. uSizeStruct,
  394. wstrFile));
  395. }
  396. BOOL
  397. NOTIFY_FUNCTION(
  398. DWORD fdwReason)
  399. {
  400. if (fdwReason == DLL_PROCESS_ATTACH)
  401. {
  402. return LuatFSInit();
  403. }
  404. else if (fdwReason == DLL_PROCESS_DETACH)
  405. {
  406. LuatFSCleanup();
  407. }
  408. return TRUE;
  409. }
  410. HOOK_BEGIN
  411. CALL_NOTIFY_FUNCTION
  412. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileA)
  413. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileW)
  414. APIHOOK_ENTRY(KERNEL32.DLL, CopyFileA)
  415. APIHOOK_ENTRY(KERNEL32.DLL, CopyFileW)
  416. APIHOOK_ENTRY(KERNEL32.DLL, OpenFile)
  417. APIHOOK_ENTRY(KERNEL32.DLL, _lopen)
  418. APIHOOK_ENTRY(KERNEL32.DLL, _lcreat)
  419. APIHOOK_ENTRY(KERNEL32.DLL, CreateDirectoryA)
  420. APIHOOK_ENTRY(KERNEL32.DLL, CreateDirectoryW)
  421. APIHOOK_ENTRY(KERNEL32.DLL, SetFileAttributesA)
  422. APIHOOK_ENTRY(KERNEL32.DLL, SetFileAttributesW)
  423. APIHOOK_ENTRY(KERNEL32.DLL, DeleteFileA)
  424. APIHOOK_ENTRY(KERNEL32.DLL, DeleteFileW)
  425. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileA)
  426. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileW)
  427. APIHOOK_ENTRY(KERNEL32.DLL, RemoveDirectoryA)
  428. APIHOOK_ENTRY(KERNEL32.DLL, RemoveDirectoryW)
  429. APIHOOK_ENTRY(KERNEL32.DLL, GetTempFileNameA)
  430. APIHOOK_ENTRY(KERNEL32.DLL, GetTempFileNameW)
  431. APIHOOK_ENTRY(KERNEL32.DLL, WritePrivateProfileStringA)
  432. APIHOOK_ENTRY(KERNEL32.DLL, WritePrivateProfileStringW)
  433. APIHOOK_ENTRY(KERNEL32.DLL, WritePrivateProfileSectionA)
  434. APIHOOK_ENTRY(KERNEL32.DLL, WritePrivateProfileSectionW)
  435. APIHOOK_ENTRY(KERNEL32.DLL, WritePrivateProfileStructA)
  436. APIHOOK_ENTRY(KERNEL32.DLL, WritePrivateProfileStructW)
  437. HOOK_END
  438. IMPLEMENT_SHIM_END