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.

509 lines
15 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. umpdhook.c
  6. Abstract:
  7. This module is to redirect SPOOLSS.DLL functions to the WINSPOOL.DRV
  8. functions for the User Mode Printer Device (UMPD) DLLs.
  9. Author:
  10. Min-Chih Lu Earl (v-mearl) 8-March-99 (Get ideas and implementation from
  11. \private\tsext\admtools\tsappcmp\register.c
  12. by v-johnjr)
  13. Environment:
  14. User Mode - Win32 (in WINSRV.DLL running in the CSRSS.EXE process)
  15. Revision History:
  16. --*/
  17. #define _USER_
  18. #include "precomp.h"
  19. #pragma hdrstop
  20. #include <ntddrdr.h>
  21. #include <stdio.h>
  22. #include <windows.h>
  23. #include <winspool.h>
  24. #if(WINVER >= 0x0500)
  25. #include <winspl.h>
  26. #else
  27. #include "..\..\..\..\..\windows\spooler\spoolss\winspl.h"
  28. #endif
  29. #include <data.h>
  30. #include "wingdip.h"
  31. #if(WINVER >= 0x0500)
  32. #include "musspl.h"
  33. #else
  34. #include "ctxspl.h"
  35. #endif
  36. //*====================================================================*//
  37. //* Local definitions *//
  38. //*====================================================================*//
  39. #define SPOOLSS_DLL_W L"SPOOLSS.DLL"
  40. #define SPOOLSS_DLL_A "SPOOLSS.DLL"
  41. //*====================================================================*//
  42. //* Local function prototypes
  43. //*====================================================================*//
  44. BOOL PlaceHooks(HMODULE hUMPD, HMODULE hSpoolss);
  45. PVOID PlaceHookEntry(HMODULE hSpoolss, PVOID * pProcAddress);
  46. //*====================================================================*//
  47. //* Hook function prototypes *//
  48. //*====================================================================*//
  49. PVOID TSsplHookGetProcAddress(IN HMODULE hModule,IN LPCSTR lpProcName);
  50. //*====================================================================*//
  51. //* Public functions Implementations *//
  52. //*====================================================================*//
  53. BOOL
  54. TSsplHookSplssToWinspool(
  55. IN HMODULE hUMPD
  56. )
  57. /*++
  58. Routine Description:
  59. This routine redirect the statically linked SPOOLSS.DLL address
  60. to the winspool.drv equivalent functions
  61. Arguments:
  62. hUMPD - Supplies the user mode printer driver DLL handle which uses
  63. the SPOOLSS.DLL
  64. Return Value:
  65. TRUE - Success
  66. FAIL - Error. Use GetLastError() to get the error status
  67. --*/
  68. {
  69. BOOL bStatus = TRUE;
  70. HMODULE hSpoolss;
  71. /*
  72. * Load SPOOLSS.DLL
  73. */
  74. hSpoolss = LoadLibrary(SPOOLSS_DLL_W);
  75. if (!hSpoolss) {
  76. DBGMSG(DBG_WARNING,("TSsplHookSplssToWinspool - Cannot load SPOOLSS.DLL\n"));
  77. return FALSE;
  78. }
  79. /*
  80. * Redirect the spoolss.dll call to the winspool.drv call in the
  81. * UMPD.DLL
  82. */
  83. bStatus = PlaceHooks(hUMPD, hSpoolss);
  84. FreeLibrary(hSpoolss);
  85. DBGMSG(DBG_TRACE,("TSsplHookSplssToWinspool - Redirect UMPD.DLL Spoolss.dll to Winspool.dll\n"));
  86. return bStatus;
  87. }
  88. //*====================================================================*//
  89. //* Hook functions Implementations *//
  90. //* These function hooks to the UMPD.DLL *//
  91. //*====================================================================*//
  92. PVOID
  93. TSsplHookGetProcAddress(
  94. IN HMODULE hModule,
  95. IN LPCSTR lpProcName
  96. )
  97. /*++
  98. Routine Description:
  99. Redirect Spoolss.dll function in hUMPD to winspool.drv for dynamic load
  100. --*/
  101. {
  102. PVOID p;
  103. DWORD dllNameCount;
  104. WCHAR dllName[MAX_PATH];
  105. p = GetProcAddress(hModule, lpProcName);
  106. if (p &&
  107. (dllNameCount = GetModuleFileName(hModule, dllName, sizeof(dllName)/sizeof(WCHAR))) &&
  108. (wcsstr(_wcsupr(dllName), SPOOLSS_DLL_W) )
  109. ) {
  110. /*
  111. *This is SPOOLSS.DLL GetProcAddres. We need to redirect the p
  112. */
  113. DBGMSG(DBG_TRACE,("TSsplHookGetProcAddress - Redirect UMPD.DLL GetProcAddress %s\n",lpProcName));
  114. p = PlaceHookEntry(hModule, &p);
  115. }
  116. return p;
  117. }
  118. //*==========================================================================*//
  119. //* Local functions *//
  120. //*==========================================================================*//
  121. BOOL
  122. PlaceHooks(
  123. HMODULE hUMPD,
  124. HMODULE hSpoolss
  125. )
  126. /*++
  127. Routine Description:
  128. Redirect Spoolss.dll function in hUMPD to winspool.drv.
  129. --*/
  130. {
  131. NTSTATUS st;
  132. PVOID IATBase;
  133. SIZE_T BigIATSize;
  134. ULONG LittleIATSize = 0;
  135. PVOID *ProcAddresses;
  136. ULONG NumberOfProcAddresses;
  137. ULONG OldProtect;
  138. IATBase = RtlImageDirectoryEntryToData( hUMPD,
  139. TRUE,
  140. IMAGE_DIRECTORY_ENTRY_IAT,
  141. &LittleIATSize
  142. );
  143. BigIATSize = LittleIATSize;
  144. if (IATBase != NULL) {
  145. st = NtProtectVirtualMemory( NtCurrentProcess(),
  146. &IATBase,
  147. &BigIATSize,
  148. PAGE_READWRITE,
  149. &OldProtect
  150. );
  151. if (!NT_SUCCESS(st)) {
  152. return FALSE;
  153. } else {
  154. ProcAddresses = (PVOID *)IATBase;
  155. NumberOfProcAddresses = (ULONG)(BigIATSize / sizeof(PVOID));
  156. while (NumberOfProcAddresses--) {
  157. /*
  158. * Redirect the LoadLibrary and GetProcAddress function. We will
  159. * have a chance to replace the address when the UMPD is a
  160. * dynamically load
  161. * DLL
  162. */
  163. if (*ProcAddresses == GetProcAddress) {
  164. *ProcAddresses = TSsplHookGetProcAddress;
  165. } else {
  166. /* Replace the (static linked) spoolss.dll entry */
  167. *ProcAddresses = PlaceHookEntry(hSpoolss, ProcAddresses);
  168. }
  169. ProcAddresses += 1;
  170. }
  171. NtProtectVirtualMemory( NtCurrentProcess(),
  172. &IATBase,
  173. &BigIATSize,
  174. OldProtect,
  175. &OldProtect
  176. );
  177. }
  178. }
  179. return TRUE;
  180. }
  181. PVOID
  182. PlaceHookEntry(HMODULE hSpoolss,
  183. PVOID * pProcAddress
  184. )
  185. /*--
  186. Routine Description:
  187. This routine redirect the pProcAddress SPOOLSS.DLL function to the
  188. corresponding winspool.drv function.
  189. Arguments:
  190. Return Value:
  191. The corresponding winspool.drv function if found. Else, the original
  192. function
  193. --*/
  194. {
  195. /*
  196. * Print jobs functions
  197. */
  198. if ((GetProcAddress(hSpoolss,"SetJobW")) == *pProcAddress) {
  199. return (&SetJobW);
  200. }
  201. if ((GetProcAddress(hSpoolss,"GetJobW")) == *pProcAddress) {
  202. return(&GetJobW);
  203. }
  204. if ((GetProcAddress(hSpoolss,"WritePrinter")) == *pProcAddress) {
  205. return(&WritePrinter);
  206. }
  207. if ((GetProcAddress(hSpoolss,"EnumJobsW")) == *pProcAddress) {
  208. return(&EnumJobsW);
  209. }
  210. if ((GetProcAddress(hSpoolss,"AddJobW")) == *pProcAddress) {
  211. return(&AddJobW);
  212. }
  213. if ((GetProcAddress(hSpoolss,"ScheduleJob")) == *pProcAddress) {
  214. return(&ScheduleJob);
  215. }
  216. /*
  217. * Manage Printers
  218. */
  219. if ((GetProcAddress(hSpoolss,"EnumPrintersW")) == *pProcAddress) {
  220. return(&EnumPrintersW);
  221. }
  222. if (((BOOL (*)())GetProcAddress(hSpoolss,"AddPrinterW")) == *pProcAddress) {
  223. return(&AddPrinterW);
  224. }
  225. if ((GetProcAddress(hSpoolss,"DeletePrinter")) == *pProcAddress) {
  226. return(&DeletePrinter);
  227. }
  228. if ((GetProcAddress(hSpoolss,"SetPrinterW")) == *pProcAddress) {
  229. return(&SetPrinterW);
  230. }
  231. if ((GetProcAddress(hSpoolss,"GetPrinterW")) == *pProcAddress) {
  232. return(&GetPrinterW);
  233. }
  234. /*
  235. * Printer Data functions
  236. */
  237. if ((GetProcAddress(hSpoolss,"GetPrinterDataW")) == *pProcAddress) {
  238. return(&GetPrinterDataW);
  239. }
  240. #if(WINVER >= 0x0500)
  241. if ((GetProcAddress(hSpoolss,"GetPrinterDataExW")) == *pProcAddress) {
  242. return(&GetPrinterDataExW);
  243. }
  244. #endif
  245. if ((GetProcAddress(hSpoolss,"EnumPrinterDataW")) == *pProcAddress) {
  246. return(&EnumPrinterDataW);
  247. }
  248. #if(WINVER >= 0x0500)
  249. if ((GetProcAddress(hSpoolss,"EnumPrinterDataExW")) == *pProcAddress) {
  250. return(&EnumPrinterDataExW);
  251. }
  252. if ((GetProcAddress(hSpoolss,"EnumPrinterKeyW")) == *pProcAddress) {
  253. return(&EnumPrinterKeyW);
  254. }
  255. #endif
  256. if ((GetProcAddress(hSpoolss,"DeletePrinterDataW")) == *pProcAddress) {
  257. return(&DeletePrinterDataW);
  258. }
  259. #if(WINVER >= 0x0500)
  260. if ((GetProcAddress(hSpoolss,"DeletePrinterDataExW")) == *pProcAddress) {
  261. return(&DeletePrinterDataExW);
  262. }
  263. if ((GetProcAddress(hSpoolss,"DeletePrinterKeyW")) == *pProcAddress) {
  264. return(&DeletePrinterKeyW);
  265. }
  266. #endif
  267. if ((GetProcAddress(hSpoolss,"SetPrinterDataW")) == *pProcAddress) {
  268. return(&SetPrinterDataW);
  269. }
  270. #if(WINVER >= 0x0500)
  271. if ((GetProcAddress(hSpoolss,"SetPrinterDataExW")) == *pProcAddress) {
  272. return(&SetPrinterDataExW);
  273. }
  274. #endif
  275. /*
  276. * PrinterConnection functions
  277. */
  278. if ((GetProcAddress(hSpoolss,"AddPrinterConnectionW")) == *pProcAddress) {
  279. return(&AddPrinterConnectionW);
  280. }
  281. if ((GetProcAddress(hSpoolss,"DeletePrinterConnectionW")) == *pProcAddress) {
  282. return(&DeletePrinterConnectionW);
  283. }
  284. /*
  285. * Driver functions
  286. */
  287. if ((GetProcAddress(hSpoolss,"GetPrinterDriverDirectoryW")) == *pProcAddress) {
  288. return(&GetPrinterDriverDirectoryW);
  289. }
  290. if ((GetProcAddress(hSpoolss,"GetPrinterDriverW")) == *pProcAddress) {
  291. return(&GetPrinterDriverW);
  292. }
  293. if ((GetProcAddress(hSpoolss,"AddPrinterDriverW")) == *pProcAddress) {
  294. return(&AddPrinterDriverW);
  295. }
  296. #if(WINVER >= 0x0500)
  297. if ((GetProcAddress(hSpoolss,"AddPrinterDriverExW")) == *pProcAddress) {
  298. return(&AddPrinterDriverExW);
  299. }
  300. #endif
  301. if ((GetProcAddress(hSpoolss,"EnumPrinterDriversW")) == *pProcAddress) {
  302. return(&EnumPrinterDriversW);
  303. }
  304. if ((GetProcAddress(hSpoolss,"DeletePrinterDriverW")) == *pProcAddress) {
  305. return(&DeletePrinterDriverW);
  306. }
  307. #if(WINVER >= 0x0500)
  308. if ((GetProcAddress(hSpoolss,"DeletePrinterDriverExW")) == *pProcAddress) {
  309. return(&DeletePrinterDriverExW);
  310. }
  311. #endif
  312. /*
  313. * Print Processors
  314. */
  315. if ((GetProcAddress(hSpoolss,"AddPrintProcessorW")) == *pProcAddress) {
  316. return(&AddPrintProcessorW);
  317. }
  318. if ((GetProcAddress(hSpoolss,"EnumPrintProcessorsW")) == *pProcAddress) {
  319. return(&EnumPrintProcessorsW);
  320. }
  321. if ((GetProcAddress(hSpoolss,"GetPrintProcessorDirectoryW")) == *pProcAddress) {
  322. return(&GetPrintProcessorDirectoryW);
  323. }
  324. if ((GetProcAddress(hSpoolss,"DeletePrintProcessorW")) == *pProcAddress) {
  325. return(&DeletePrintProcessorW);
  326. }
  327. if ((GetProcAddress(hSpoolss,"EnumPrintProcessorDatatypesW")) == *pProcAddress) {
  328. return(&EnumPrintProcessorDatatypesW);
  329. }
  330. if ((GetProcAddress(hSpoolss,"OpenPrinterW")) == *pProcAddress) {
  331. return(&OpenPrinterW);
  332. }
  333. if ((GetProcAddress(hSpoolss,"ResetPrinterW")) == *pProcAddress) {
  334. return(&ResetPrinterW);
  335. }
  336. if ((GetProcAddress(hSpoolss,"ClosePrinter")) == *pProcAddress) {
  337. return(&ClosePrinter);
  338. }
  339. if ((GetProcAddress(hSpoolss,"AddPrintProcessorW")) == *pProcAddress) {
  340. return(&AddPrintProcessorW);
  341. }
  342. /*
  343. * Doc Printer
  344. */
  345. if ((GetProcAddress(hSpoolss,"StartDocPrinterW")) == *pProcAddress) {
  346. return(&StartDocPrinterW);
  347. }
  348. if ((GetProcAddress(hSpoolss,"StartPagePrinter")) == *pProcAddress) {
  349. return(&StartPagePrinter);
  350. }
  351. if ((GetProcAddress(hSpoolss,"EndPagePrinter")) == *pProcAddress) {
  352. return(&EndPagePrinter);
  353. }
  354. if ((GetProcAddress(hSpoolss,"WritePrinter")) == *pProcAddress) {
  355. return(&WritePrinter);
  356. }
  357. #if(WINVER >= 0x0500)
  358. if ((GetProcAddress(hSpoolss,"FlushPrinter")) == *pProcAddress) {
  359. return(&FlushPrinter);
  360. }
  361. #endif
  362. if ((GetProcAddress(hSpoolss,"AbortPrinter")) == *pProcAddress) {
  363. return(&AbortPrinter);
  364. }
  365. if ((GetProcAddress(hSpoolss,"ReadPrinter")) == *pProcAddress) {
  366. return(&ReadPrinter);
  367. }
  368. if ((GetProcAddress(hSpoolss,"EndDocPrinter")) == *pProcAddress) {
  369. return(&EndDocPrinter);
  370. }
  371. /*
  372. * Change functions
  373. */
  374. if ((GetProcAddress(hSpoolss,"WaitForPrinterChange")) == *pProcAddress) {
  375. return(&WaitForPrinterChange);
  376. }
  377. if ((GetProcAddress(hSpoolss,"FindClosePrinterChangeNotification")) == *pProcAddress) {
  378. return(&FindClosePrinterChangeNotification);
  379. }
  380. /*
  381. * Forms and port
  382. */
  383. if ((GetProcAddress(hSpoolss,"AddFormW")) == *pProcAddress) {
  384. return(&AddFormW);
  385. }
  386. if ((GetProcAddress(hSpoolss,"DeleteFormW")) == *pProcAddress) {
  387. return(&DeleteFormW);
  388. }
  389. if ((GetProcAddress(hSpoolss,"GetFormW")) == *pProcAddress) {
  390. return(&GetFormW);
  391. }
  392. if ((GetProcAddress(hSpoolss,"SetFormW")) == *pProcAddress) {
  393. return(&SetFormW);
  394. }
  395. if ((GetProcAddress(hSpoolss,"EnumFormsW")) == *pProcAddress) {
  396. return(&EnumFormsW);
  397. }
  398. if ((GetProcAddress(hSpoolss,"EnumPortsW")) == *pProcAddress) {
  399. return(&EnumPortsW);
  400. }
  401. if ((GetProcAddress(hSpoolss,"EnumMonitorsW")) == *pProcAddress) {
  402. return(&EnumMonitorsW);
  403. }
  404. if ((GetProcAddress(hSpoolss,"AddPortW")) == *pProcAddress) {
  405. return(&AddPortW);
  406. }
  407. if ((GetProcAddress(hSpoolss,"ConfigurePortW")) == *pProcAddress) {
  408. return(&ConfigurePortW);
  409. }
  410. if ((GetProcAddress(hSpoolss,"DeletePortW")) == *pProcAddress) {
  411. return(&DeletePortW);
  412. }
  413. if ((GetProcAddress(hSpoolss,"SetPortW")) == *pProcAddress) {
  414. return(&SetPortW);
  415. }
  416. if ((GetProcAddress(hSpoolss,"AddMonitorW")) == *pProcAddress) {
  417. return(&AddMonitorW);
  418. }
  419. if ((GetProcAddress(hSpoolss,"DeleteMonitorW")) == *pProcAddress) {
  420. return(&DeleteMonitorW);
  421. }
  422. if ((GetProcAddress(hSpoolss,"AddPrintProvidorW")) == *pProcAddress) {
  423. return(&AddPrintProvidorW);
  424. }
  425. return *pProcAddress;
  426. }
  427.