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.

493 lines
13 KiB

  1. //=================================================================
  2. //
  3. // NtDllApi.cpp
  4. //
  5. // Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include <nt.h>
  9. #include <ntrtl.h>
  10. #include <nturtl.h>
  11. #include <ntobapi.h>
  12. #define _WINNT_ // have what is needed from above
  13. #include "precomp.h"
  14. #include <ntseapi.h>
  15. #include <cominit.h>
  16. #include <ntsecapi.h>
  17. #include "DllWrapperBase.h"
  18. #include "NtDllApi.h"
  19. #include "DllWrapperCreatorReg.h"
  20. // {77609C22-CDAA-11d2-911E-0060081A46FD}
  21. static const GUID g_guidNtDllApi =
  22. {0x77609c22, 0xcdaa, 0x11d2, {0x91, 0x1e, 0x0, 0x60, 0x8, 0x1a, 0x46, 0xfd}};
  23. static const TCHAR g_tstrNtDll[] = _T("NTDLL.DLL");
  24. /******************************************************************************
  25. * Register this class with the CResourceManager.
  26. *****************************************************************************/
  27. CDllApiWraprCreatrReg<CNtDllApi, &g_guidNtDllApi, g_tstrNtDll> MyRegisteredNtDllWrapper;
  28. /******************************************************************************
  29. * Constructor
  30. *****************************************************************************/
  31. CNtDllApi::CNtDllApi(LPCTSTR a_tstrWrappedDllName)
  32. : CDllWrapperBase(a_tstrWrappedDllName),
  33. m_pfnRtlInitUnicodeString(NULL),
  34. m_pfnNtSetSystemEnvironmentValue(NULL),
  35. m_pfnNtQuerySystemEnvironmentValue(NULL),
  36. #if defined(_IA64_) //EFI_NVRAM_ENABLED)
  37. m_pfnNtQueryBootOptions(NULL),
  38. m_pfnNtSetBootOptions(NULL),
  39. m_pfnNtQueryBootEntryOrder(NULL),
  40. m_pfnNtSetBootEntryOrder(NULL),
  41. m_pfnNtEnumerateBootEntries(NULL),
  42. #endif // defined(EFI_NVRAM_ENABLED)
  43. m_pfnNtCreateFile(NULL),
  44. m_pfnNtQuerySystemInformation(NULL),
  45. m_pfnNtQueryDirectoryObject(NULL),
  46. m_pfnNtQueryObject(NULL),
  47. m_pfnNtOpenDirectoryObject(NULL) ,
  48. m_pfnNtQueryInformationProcess(NULL),
  49. m_pfnNtQueryInformationToken(NULL),
  50. m_pfnNtOpenFile(NULL),
  51. m_pfnNtClose(NULL),
  52. m_pfnNtFsControlFile(NULL)
  53. {
  54. }
  55. /******************************************************************************
  56. * Destructor
  57. *****************************************************************************/
  58. CNtDllApi::~CNtDllApi()
  59. {
  60. }
  61. /******************************************************************************
  62. * Initialization function to check that we obtained function addresses.
  63. ******************************************************************************/
  64. bool CNtDllApi::Init()
  65. {
  66. bool fRet = LoadLibrary();
  67. if(fRet)
  68. {
  69. m_pfnRtlInitUnicodeString = (PFN_NTDLL_RTL_INIT_UNICODE_STRING)
  70. GetProcAddress("RtlInitUnicodeString");
  71. m_pfnRtlFreeUnicodeString = (PFN_NTDLL_RTL_FREE_UNICODE_STRING)
  72. GetProcAddress("RtlFreeUnicodeString");
  73. m_pfnNtSetSystemEnvironmentValue =
  74. (PFN_NTDLL_NT_SET_SYSTEM_ENVIRONMENT_VALUE)
  75. GetProcAddress("NtSetSystemEnvironmentValue");
  76. m_pfnNtQuerySystemEnvironmentValue =
  77. (PFN_NTDLL_NT_QUERY_SYSTEM_ENVIRONMENT_VALUE)
  78. GetProcAddress("NtQuerySystemEnvironmentValue");
  79. #if defined(_IA64_)//EFI_NVRAM_ENABLED)
  80. m_pfnNtQueryBootOptions =
  81. (PFN_NTDLL_NT_QUERY_BOOT_OPTIONS)
  82. GetProcAddress("NtQueryBootOptions");
  83. m_pfnNtSetBootOptions =
  84. (PFN_NTDLL_NT_SET_BOOT_OPTIONS)
  85. GetProcAddress("NtSetBootOptions");
  86. m_pfnNtQueryBootEntryOrder =
  87. (PFN_NTDLL_NT_QUERY_BOOT_ENTRY_ORDER)
  88. GetProcAddress("NtQueryBootEntryOrder");
  89. m_pfnNtSetBootEntryOrder =
  90. (PFN_NTDLL_NT_SET_BOOT_ENTRY_ORDER)
  91. GetProcAddress("NtSetBootEntryOrder");
  92. m_pfnNtEnumerateBootEntries =
  93. (PFN_NTDLL_NT_ENUMERATE_BOOT_ENTRIES)
  94. GetProcAddress("NtEnumerateBootEntries");
  95. #endif // defined(EFI_NVRAM_ENABLED)
  96. m_pfnNtCreateFile = (PFN_NTDLL_NT_CREATE_FILE)
  97. GetProcAddress("NtCreateFile");
  98. m_pfnNtQuerySystemInformation = (PFN_NT_QUERY_SYSTEM_INFORMATION)
  99. GetProcAddress("NtQuerySystemInformation");
  100. m_pfnNtQueryDirectoryObject = (PFN_NT_QUERY_DIRECTORY_OBJECT)
  101. GetProcAddress("NtQueryDirectoryObject");
  102. m_pfnNtQueryObject = (PFN_NT_QUERY_OBJECT)
  103. GetProcAddress("NtQueryObject");
  104. m_pfnNtOpenDirectoryObject = (PFN_NT_OPEN_DIRECTORY_OBJECT)
  105. GetProcAddress("NtOpenDirectoryObject");
  106. m_pfnNtQueryInformationProcess = ( PFN_NTQUERYINFORMATIONPROCESS )
  107. GetProcAddress("NtQueryInformationProcess");
  108. m_pfnNtQueryInformationToken = ( PFN_NT_QUERY_INFORMATION_TOKEN )
  109. GetProcAddress("NtQueryInformationToken");
  110. m_pfnNtOpenFile = ( PFN_NT_OPEN_FILE ) GetProcAddress("NtOpenFile");
  111. m_pfnNtClose = ( PFN_NT_CLOSE ) GetProcAddress("NtClose");
  112. m_pfnNtFsControlFile = ( PFN_NT_FS_CONTROL_FILE ) GetProcAddress("NtFsControlFile") ;
  113. m_pfnNtQueryVolumeInformationFile = (PFN_NT_QUERY_VOLUME_INFORMATION_FILE)
  114. GetProcAddress("NtQueryVolumeInformationFile");
  115. }
  116. // We require these function for all versions of this dll.
  117. if (
  118. m_pfnRtlInitUnicodeString == NULL ||
  119. m_pfnNtSetSystemEnvironmentValue == NULL ||
  120. m_pfnNtQuerySystemEnvironmentValue == NULL ||
  121. #if defined(_IA64_)//(EFI_NVRAM_ENABLED)
  122. m_pfnNtQueryBootOptions == NULL ||
  123. m_pfnNtSetBootOptions == NULL ||
  124. m_pfnNtQueryBootEntryOrder == NULL ||
  125. m_pfnNtSetBootEntryOrder == NULL ||
  126. m_pfnNtEnumerateBootEntries == NULL ||
  127. #endif // defined(EFI_NVRAM_ENABLED)
  128. m_pfnNtCreateFile == NULL ||
  129. m_pfnNtQuerySystemInformation == NULL ||
  130. m_pfnNtQueryDirectoryObject == NULL ||
  131. m_pfnNtQueryObject == NULL ||
  132. m_pfnNtOpenDirectoryObject == NULL ||
  133. m_pfnNtQueryInformationProcess == NULL ||
  134. m_pfnNtQueryInformationToken == NULL ||
  135. m_pfnNtOpenFile == NULL ||
  136. m_pfnNtClose == NULL ||
  137. m_pfnNtFsControlFile == NULL ||
  138. m_pfnRtlFreeUnicodeString == NULL
  139. )
  140. {
  141. fRet = false;
  142. LogErrorMessage(L"Failed find entrypoint in ntdllapi");
  143. }
  144. return fRet;
  145. }
  146. /******************************************************************************
  147. * Member functions wrapping NtDll api functions. Add new functions here
  148. * as required.
  149. *****************************************************************************/
  150. DWORD CNtDllApi::RtlInitUnicodeString
  151. (
  152. UNICODE_STRING* a_pustr,
  153. LPCWSTR a_wstr
  154. )
  155. {
  156. return m_pfnRtlInitUnicodeString(a_pustr,
  157. a_wstr);
  158. }
  159. VOID CNtDllApi::RtlFreeUnicodeString (
  160. PUNICODE_STRING UnicodeString
  161. )
  162. {
  163. m_pfnRtlFreeUnicodeString (
  164. UnicodeString
  165. ) ;
  166. }
  167. DWORD CNtDllApi::NtSetSystemEnvironmentValue
  168. (
  169. UNICODE_STRING* a_pustr1,
  170. UNICODE_STRING* a_pustr2
  171. )
  172. {
  173. return m_pfnNtSetSystemEnvironmentValue(a_pustr1,
  174. a_pustr2);
  175. }
  176. DWORD CNtDllApi::NtQuerySystemEnvironmentValue
  177. (
  178. PUNICODE_STRING a_pustr,
  179. PWSTR a_pwstr,
  180. USHORT a_us,
  181. PUSHORT a_pus
  182. )
  183. {
  184. return m_pfnNtQuerySystemEnvironmentValue(a_pustr,
  185. a_pwstr,
  186. a_us,
  187. a_pus);
  188. }
  189. #if defined(_IA64_)//(EFI_NVRAM_ENABLED)
  190. DWORD CNtDllApi::NtQueryBootOptions
  191. (
  192. PBOOT_OPTIONS BootOptions,
  193. PULONG BootOptionsLength
  194. )
  195. {
  196. return m_pfnNtQueryBootOptions(BootOptions,
  197. BootOptionsLength);
  198. }
  199. DWORD CNtDllApi::NtSetBootOptions
  200. (
  201. PBOOT_OPTIONS BootOptions,
  202. ULONG FieldsToChange
  203. )
  204. {
  205. return m_pfnNtSetBootOptions(BootOptions,
  206. FieldsToChange);
  207. }
  208. DWORD CNtDllApi::NtQueryBootEntryOrder
  209. (
  210. PULONG Ids,
  211. PULONG Count
  212. )
  213. {
  214. return m_pfnNtQueryBootEntryOrder(Ids,
  215. Count);
  216. }
  217. DWORD CNtDllApi::NtSetBootEntryOrder
  218. (
  219. PULONG Ids,
  220. ULONG Count
  221. )
  222. {
  223. return m_pfnNtSetBootEntryOrder(Ids,
  224. Count);
  225. }
  226. DWORD CNtDllApi::NtEnumerateBootEntries
  227. (
  228. PVOID Buffer,
  229. PULONG BufferLength
  230. )
  231. {
  232. return m_pfnNtEnumerateBootEntries(Buffer,
  233. BufferLength);
  234. }
  235. #endif // defined(EFI_NVRAM_ENABLED)
  236. NTSTATUS CNtDllApi::NtCreateFile
  237. (
  238. HANDLE *a_ph,
  239. ACCESS_MASK a_am,
  240. POBJECT_ATTRIBUTES a_pa,
  241. PIO_STATUS_BLOCK a_sb,
  242. PLARGE_INTEGER a_pla,
  243. ULONG a_ul1,
  244. ULONG a_ul2,
  245. ULONG a_ul3,
  246. ULONG a_ul4,
  247. PVOID a_pv,
  248. ULONG a_ul5
  249. )
  250. {
  251. return m_pfnNtCreateFile(a_ph, a_am, a_pa, a_sb, a_pla, a_ul1, a_ul2,
  252. a_ul3, a_ul4, a_pv, a_ul5);
  253. }
  254. NTSTATUS CNtDllApi::NtQuerySystemInformation
  255. (
  256. SYSTEM_INFORMATION_CLASS a_SystemInformationClass,
  257. PVOID a_SystemInformation,
  258. ULONG a_SystemInformationLength,
  259. PULONG a_ReturnLength
  260. )
  261. {
  262. return m_pfnNtQuerySystemInformation(a_SystemInformationClass,
  263. a_SystemInformation,
  264. a_SystemInformationLength,
  265. a_ReturnLength);
  266. }
  267. NTSTATUS CNtDllApi::NtQueryDirectoryObject
  268. (
  269. HANDLE a_DirectoryHandle,
  270. PVOID a_Buffer,
  271. ULONG a_Length,
  272. BOOLEAN a_ReturnSingleEntry,
  273. BOOLEAN a_RestartScan,
  274. PULONG a_Context,
  275. PULONG a_ReturnLength
  276. )
  277. {
  278. return m_pfnNtQueryDirectoryObject(a_DirectoryHandle,
  279. a_Buffer,
  280. a_Length,
  281. a_ReturnSingleEntry,
  282. a_RestartScan,
  283. a_Context,
  284. a_ReturnLength);
  285. }
  286. NTSTATUS CNtDllApi::NtQueryObject
  287. (
  288. HANDLE a_Handle,
  289. OBJECT_INFORMATION_CLASS a_ObjectInformationClass,
  290. PVOID a_ObjectInformation,
  291. ULONG a_Length,
  292. PULONG a_ReturnLength
  293. )
  294. {
  295. return m_pfnNtQueryObject(a_Handle,
  296. a_ObjectInformationClass,
  297. a_ObjectInformation,
  298. a_Length,
  299. a_ReturnLength);
  300. }
  301. NTSTATUS CNtDllApi::NtOpenDirectoryObject
  302. (
  303. PHANDLE a_DirectoryHandle,
  304. ACCESS_MASK a_DesiredAccess,
  305. POBJECT_ATTRIBUTES a_ObjectAttributes
  306. )
  307. {
  308. return m_pfnNtOpenDirectoryObject(a_DirectoryHandle,
  309. a_DesiredAccess,
  310. a_ObjectAttributes);
  311. }
  312. NTSTATUS CNtDllApi::NtQueryInformationProcess (
  313. IN HANDLE ProcessHandle,
  314. IN PROCESSINFOCLASS ProcessInformationClass,
  315. OUT PVOID ProcessInformation,
  316. IN ULONG ProcessInformationLength,
  317. OUT PULONG ReturnLength OPTIONAL
  318. )
  319. {
  320. return m_pfnNtQueryInformationProcess (
  321. ProcessHandle,
  322. ProcessInformationClass,
  323. ProcessInformation,
  324. ProcessInformationLength,
  325. ReturnLength
  326. ) ;
  327. }
  328. NTSTATUS CNtDllApi::NtQueryInformationToken (
  329. IN HANDLE TokenHandle,
  330. IN TOKEN_INFORMATION_CLASS TokenInformationClass,
  331. OUT PVOID TokenInformation,
  332. IN ULONG TokenInformationLength,
  333. OUT PULONG ReturnLength
  334. )
  335. {
  336. return m_pfnNtQueryInformationToken (
  337. TokenHandle,
  338. TokenInformationClass,
  339. TokenInformation,
  340. TokenInformationLength,
  341. ReturnLength
  342. ) ;
  343. }
  344. NTSTATUS CNtDllApi::NtOpenFile (
  345. OUT PHANDLE FileHandle,
  346. IN ACCESS_MASK DesiredAccess,
  347. IN POBJECT_ATTRIBUTES ObjectAttributes,
  348. OUT PIO_STATUS_BLOCK IoStatusBlock,
  349. IN ULONG ShareAccess,
  350. IN ULONG OpenOptions
  351. )
  352. {
  353. return m_pfnNtOpenFile (
  354. FileHandle,
  355. DesiredAccess,
  356. ObjectAttributes,
  357. IoStatusBlock,
  358. ShareAccess,
  359. OpenOptions
  360. ) ;
  361. }
  362. NTSTATUS CNtDllApi::NtClose (
  363. IN HANDLE Handle
  364. )
  365. {
  366. return m_pfnNtClose (
  367. Handle
  368. ) ;
  369. }
  370. NTSTATUS CNtDllApi::NtFsControlFile (
  371. IN HANDLE FileHandle,
  372. IN HANDLE Event OPTIONAL,
  373. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  374. IN PVOID ApcContext OPTIONAL,
  375. OUT PIO_STATUS_BLOCK IoStatusBlock,
  376. IN ULONG FsControlCode,
  377. IN PVOID InputBuffer OPTIONAL,
  378. IN ULONG InputBufferLength,
  379. OUT PVOID OutputBuffer OPTIONAL,
  380. IN ULONG OutputBufferLength
  381. )
  382. {
  383. return m_pfnNtFsControlFile (
  384. FileHandle,
  385. Event ,
  386. ApcRoutine ,
  387. ApcContext ,
  388. IoStatusBlock,
  389. FsControlCode,
  390. InputBuffer ,
  391. InputBufferLength,
  392. OutputBuffer ,
  393. OutputBufferLength
  394. ) ;
  395. }
  396. NTSTATUS CNtDllApi::NtQueryVolumeInformationFile(
  397. IN HANDLE FileHandle,
  398. OUT PIO_STATUS_BLOCK IoStatusBlock,
  399. OUT PVOID FsInformation,
  400. IN ULONG Length,
  401. IN FS_INFORMATION_CLASS FsInformationClass)
  402. {
  403. DWORD dwStatus = -1L;
  404. if(m_pfnNtQueryVolumeInformationFile)
  405. {
  406. dwStatus = m_pfnNtQueryVolumeInformationFile(
  407. FileHandle,
  408. IoStatusBlock,
  409. FsInformation,
  410. Length,
  411. FsInformationClass);
  412. }
  413. return dwStatus;
  414. }