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.

2919 lines
79 KiB

  1. /*++
  2. Copyright (c) 1990 - 1995 Microsoft Corporation
  3. Module Name:
  4. prndata.c
  5. Abstract:
  6. This module provides all the public exported APIs relating to Printer
  7. and Job management for the Local Print Providor
  8. Author:
  9. Dave Snipp (DaveSn) 15-Mar-1991
  10. Revision History:
  11. mattfe Apr 5 95 - we keep the driver data key open
  12. and then just do the read / write operations here.
  13. Steve Wilson (SWilson) Jan 11 96 - Added Server handle functionality to Get & setprinterdata
  14. and pretty much changed everything in the process.
  15. Steve Wilson (SWilson) May 31 96 - Added SplEnumPrinterData and SplDeletePrinterData
  16. Steve Wilson (SWilson) Dec 96 - Added SetPrinterDataEx, GetPrinterDataEx, EnumPrinterDataEx,
  17. EnumPrinterKey, DeletePrinterDataEx, and DeleteKey
  18. --*/
  19. #include <precomp.h>
  20. #pragma hdrstop
  21. #include "clusspl.h"
  22. #include "filepool.hxx"
  23. #include <lmcons.h>
  24. #include <lmwksta.h>
  25. #include <lmerr.h>
  26. #include <lmapibuf.h>
  27. #define SECURITY_WIN32
  28. #include <security.h>
  29. #define OPEN_PORT_TIMEOUT_VALUE 3000 // 3 seconds
  30. #define DELETE_PRINTER_DATA 0
  31. #define SET_PRINTER_DATA 1
  32. #define DELETE_PRINTER_KEY 2
  33. extern DWORD dwMajorVersion;
  34. extern DWORD dwMinorVersion;
  35. extern BOOL gbRemoteFax;
  36. extern HANDLE ghDsUpdateThread;
  37. extern DWORD gdwDsUpdateThreadId;
  38. DWORD
  39. SetPrinterDataPrinter(
  40. HANDLE hPrinter,
  41. HKEY hParentKey,
  42. HKEY hKey,
  43. LPWSTR pValueName,
  44. DWORD Type,
  45. LPBYTE pData,
  46. DWORD cbData,
  47. DWORD bSet
  48. );
  49. typedef enum {
  50. REG_PRINT,
  51. REG_PRINTERS,
  52. REG_PROVIDERS
  53. } REG_PRINT_KEY;
  54. DWORD
  55. GetServerKeyHandle(
  56. PINISPOOLER pIniSpooler,
  57. REG_PRINT_KEY eKey,
  58. HKEY *hPrintKey,
  59. PINISPOOLER* ppIniSpoolerOut
  60. );
  61. DWORD
  62. CloseServerKeyHandle(
  63. REG_PRINT_KEY eKey,
  64. HKEY hPrintKey,
  65. PINISPOOLER pIniSpooler
  66. );
  67. DWORD
  68. NonRegDsPresent(
  69. PINISPOOLER pIniSpooler,
  70. LPDWORD pType,
  71. LPBYTE pData,
  72. DWORD nSize,
  73. LPDWORD pcbNeeded
  74. );
  75. DWORD
  76. NonRegDsPresentForUser(
  77. PINISPOOLER pIniSpooler,
  78. LPDWORD pType,
  79. LPBYTE pData,
  80. DWORD nSize,
  81. LPDWORD pcbNeeded
  82. );
  83. DWORD
  84. NonRegGetDNSMachineName(
  85. PINISPOOLER pIniSpooler,
  86. LPDWORD pType,
  87. LPBYTE pData,
  88. DWORD nSize,
  89. LPDWORD pcbNeeded
  90. );
  91. DWORD
  92. NonRegWebPrintingInstalled(
  93. PINISPOOLER pIniSpooler,
  94. LPDWORD pType,
  95. LPBYTE pData,
  96. DWORD nSize,
  97. LPDWORD pcbNeeded
  98. );
  99. DWORD
  100. NonRegWebShareManagement(
  101. PINISPOOLER pIniSpooler,
  102. DWORD dwType,
  103. LPBYTE pData,
  104. DWORD cbData
  105. );
  106. DWORD
  107. PrinterNonRegGetDefaultSpoolDirectory(
  108. PSPOOL pSpool,
  109. LPDWORD pType,
  110. LPBYTE pData,
  111. DWORD nSize,
  112. LPDWORD pcbNeeded
  113. );
  114. DWORD
  115. PrinterNonRegGetChangeId(
  116. PSPOOL pSpool,
  117. LPDWORD pType,
  118. LPBYTE pData,
  119. DWORD nSize,
  120. LPDWORD pcbNeeded
  121. );
  122. DWORD
  123. RegSetDefaultSpoolDirectory(
  124. LPWSTR pValueName,
  125. DWORD dwType,
  126. LPBYTE pData,
  127. DWORD cbData,
  128. HKEY hKey,
  129. PINISPOOLER pIniSpooler
  130. );
  131. DWORD
  132. RegSetPortThreadPriority(
  133. LPWSTR pValueName,
  134. DWORD dwType,
  135. LPBYTE pData,
  136. DWORD cbData,
  137. HKEY hKey,
  138. PINISPOOLER pIniSpooler
  139. );
  140. DWORD
  141. RegSetSchedulerThreadPriority(
  142. LPWSTR pValueName,
  143. DWORD dwType,
  144. LPBYTE pData,
  145. DWORD cbData,
  146. HKEY hKey,
  147. PINISPOOLER pIniSpooler
  148. );
  149. DWORD
  150. RegSetNoRemoteDriver(
  151. LPWSTR pValueName,
  152. DWORD dwType,
  153. LPBYTE pData,
  154. DWORD cbData,
  155. HKEY hKey,
  156. PINISPOOLER pIniSpooler
  157. );
  158. DWORD
  159. RegSetNetPopupToComputer(
  160. LPWSTR pValueName,
  161. DWORD dwType,
  162. LPBYTE pData,
  163. DWORD cbData,
  164. HKEY hKey,
  165. PINISPOOLER pIniSpooler
  166. );
  167. DWORD
  168. RegSetRestartJobOnPoolError(
  169. LPWSTR pValueName,
  170. DWORD dwType,
  171. LPBYTE pData,
  172. DWORD cbData,
  173. HKEY hKey,
  174. PINISPOOLER pIniSpooler
  175. );
  176. DWORD
  177. RegSetRestartJobOnPoolEnabled(
  178. LPWSTR pValueName,
  179. DWORD dwType,
  180. LPBYTE pData,
  181. DWORD cbData,
  182. HKEY hKey,
  183. PINISPOOLER pIniSpooler
  184. );
  185. DWORD
  186. RegSetBeepEnabled(
  187. LPWSTR pValueName,
  188. DWORD dwType,
  189. LPBYTE pData,
  190. DWORD cbData,
  191. HKEY hKey,
  192. PINISPOOLER pIniSpooler
  193. );
  194. DWORD
  195. RegSetEventLog(
  196. LPWSTR pValueName,
  197. DWORD dwType,
  198. LPBYTE pData,
  199. DWORD cbData,
  200. HKEY hKey,
  201. PINISPOOLER pIniSpooler
  202. );
  203. DWORD
  204. RegSetNetPopup(
  205. LPWSTR pValueName,
  206. DWORD dwType,
  207. LPBYTE pData,
  208. DWORD cbData,
  209. HKEY hKey,
  210. PINISPOOLER pIniSpooler
  211. );
  212. DWORD
  213. RegSetRetryPopup(
  214. LPWSTR pValueName,
  215. DWORD dwType,
  216. LPBYTE pData,
  217. DWORD cbData,
  218. HKEY hKey,
  219. PINISPOOLER pIniSpooler
  220. );
  221. DWORD dwDefaultServerThreadPriority = DEFAULT_SERVER_THREAD_PRIORITY;
  222. DWORD dwDefaultSchedulerThreadPriority = DEFAULT_SCHEDULER_THREAD_PRIORITY;
  223. OSVERSIONINFO OsVersionInfo;
  224. OSVERSIONINFOEX OsVersionInfoEx;
  225. typedef struct {
  226. LPWSTR pValue;
  227. BOOL (*pSet) ( LPWSTR pValueName,
  228. DWORD dwType,
  229. LPBYTE pData,
  230. DWORD cbData,
  231. HKEY *hKey,
  232. PINISPOOLER pIniSpooler
  233. );
  234. REG_PRINT_KEY eKey;
  235. } SERVER_DATA, *PSERVER_DATA;
  236. typedef struct {
  237. LPWSTR pValue;
  238. LPBYTE pData;
  239. DWORD dwType;
  240. DWORD dwSize;
  241. } NON_REGISTRY_DATA, *PNON_REGISTRY_DATA;
  242. typedef struct {
  243. PWSTR pValue;
  244. DWORD (*pSet)( PINISPOOLER pIniSpooler,
  245. DWORD dwType,
  246. LPBYTE pData,
  247. DWORD cbData
  248. );
  249. } NON_REGSET_FCN, *PNON_REGSET_FCN;
  250. typedef struct {
  251. PWSTR pValue;
  252. DWORD (*pGet)( PINISPOOLER pIniSpooler,
  253. LPDWORD pType,
  254. LPBYTE pData,
  255. DWORD nSize,
  256. LPDWORD pcbNeeded
  257. );
  258. } NON_REGISTRY_FCN, *PNON_REGISTRY_FCN;
  259. typedef struct {
  260. PWSTR pValue;
  261. DWORD (*pGet)( PSPOOL pSpool,
  262. LPDWORD pType,
  263. LPBYTE pData,
  264. DWORD nSize,
  265. LPDWORD pcbNeeded
  266. );
  267. } PRINTER_NON_REGISTRY_FCN, *PPRINTER_NON_REGISTRY_FCN;
  268. SERVER_DATA gpServerRegistry[] = {{SPLREG_DEFAULT_SPOOL_DIRECTORY, RegSetDefaultSpoolDirectory, REG_PRINTERS},
  269. {SPLREG_PORT_THREAD_PRIORITY, RegSetPortThreadPriority, REG_PRINT},
  270. {SPLREG_SCHEDULER_THREAD_PRIORITY, RegSetSchedulerThreadPriority, REG_PRINT},
  271. {SPLREG_BEEP_ENABLED, RegSetBeepEnabled, REG_PRINT},
  272. {SPLREG_NET_POPUP, RegSetNetPopup, REG_PROVIDERS},
  273. {SPLREG_RETRY_POPUP, RegSetRetryPopup, REG_PROVIDERS},
  274. {SPLREG_EVENT_LOG, RegSetEventLog, REG_PROVIDERS},
  275. {SPLREG_NO_REMOTE_PRINTER_DRIVERS, RegSetNoRemoteDriver, REG_PRINT},
  276. {SPLREG_NET_POPUP_TO_COMPUTER, RegSetNetPopupToComputer, REG_PROVIDERS},
  277. {SPLREG_RESTART_JOB_ON_POOL_ERROR, RegSetRestartJobOnPoolError, REG_PROVIDERS},
  278. {SPLREG_RESTART_JOB_ON_POOL_ENABLED, RegSetRestartJobOnPoolEnabled, REG_PROVIDERS},
  279. {0,0,0}};
  280. NON_REGSET_FCN gpNonRegSetFcn[] = { {SPLREG_WEBSHAREMGMT, NonRegWebShareManagement},
  281. {0,0}};
  282. NON_REGISTRY_DATA gpNonRegistryData[] = {{SPLREG_PORT_THREAD_PRIORITY_DEFAULT, (LPBYTE)&dwDefaultServerThreadPriority, REG_DWORD, sizeof(DWORD)},
  283. {SPLREG_SCHEDULER_THREAD_PRIORITY_DEFAULT, (LPBYTE)&dwDefaultSchedulerThreadPriority, REG_DWORD, sizeof(DWORD)},
  284. {SPLREG_ARCHITECTURE, (LPBYTE)&LOCAL_ENVIRONMENT, REG_SZ, 0},
  285. {SPLREG_MAJOR_VERSION, (LPBYTE)&dwMajorVersion, REG_DWORD, sizeof(DWORD)},
  286. {SPLREG_MINOR_VERSION, (LPBYTE)&dwMinorVersion, REG_DWORD, sizeof(DWORD)},
  287. {SPLREG_OS_VERSION, (LPBYTE)&OsVersionInfo, REG_BINARY, sizeof(OsVersionInfo)},
  288. {SPLREG_OS_VERSIONEX, (LPBYTE)&OsVersionInfoEx, REG_BINARY, sizeof(OsVersionInfoEx)},
  289. {SPLREG_REMOTE_FAX, (LPBYTE)&gbRemoteFax, REG_BINARY, sizeof(gbRemoteFax)},
  290. {0,0,0,0}};
  291. NON_REGISTRY_FCN gpNonRegistryFcn[] = { {SPLREG_DS_PRESENT, NonRegDsPresent},
  292. {SPLREG_DS_PRESENT_FOR_USER, NonRegDsPresentForUser},
  293. {SPLREG_DNS_MACHINE_NAME, NonRegGetDNSMachineName},
  294. {SPLREG_W3SVCINSTALLED, NonRegWebPrintingInstalled},
  295. {0,0}};
  296. PRINTER_NON_REGISTRY_FCN gpPrinterNonRegistryFcn[] =
  297. {
  298. { SPLREG_DEFAULT_SPOOL_DIRECTORY, PrinterNonRegGetDefaultSpoolDirectory },
  299. { SPLREG_CHANGE_ID, PrinterNonRegGetChangeId },
  300. { 0, 0 }
  301. };
  302. extern WCHAR *szPrinterData;
  303. BOOL
  304. AvailableBidiPort(
  305. PINIPORT pIniPort,
  306. PINIMONITOR pIniLangMonitor
  307. )
  308. {
  309. //
  310. // File ports and ports with no monitor are useless
  311. //
  312. if ( (pIniPort->Status & PP_FILE) || !(pIniPort->Status & PP_MONITOR) )
  313. return FALSE;
  314. //
  315. // If no LM then PM should support pfnGetPrinterDataFromPort
  316. //
  317. if ( !pIniLangMonitor &&
  318. !pIniPort->pIniMonitor->Monitor2.pfnGetPrinterDataFromPort )
  319. return FALSE;
  320. //
  321. // A port with no jobs or same monitor is printing then it is ok
  322. //
  323. return !pIniPort->pIniJob ||
  324. pIniLangMonitor == pIniPort->pIniLangMonitor;
  325. }
  326. DWORD
  327. GetPrinterDataFromPort(
  328. PINIPRINTER pIniPrinter,
  329. LPWSTR pszValueName,
  330. LPBYTE pData,
  331. DWORD cbBuf,
  332. LPDWORD pcbNeeded
  333. )
  334. /*++
  335. Routine Description:
  336. Tries to use GetPrinterDataFromPort monitor function to satisfy a
  337. GetPrinterData call
  338. Arguments:
  339. pIniPrinter - Points to an INIPRINTER
  340. Return Value:
  341. Win32 error code
  342. --*/
  343. {
  344. DWORD rc = ERROR_INVALID_PARAMETER;
  345. DWORD i, dwFirstPortWithNoJobs, dwFirstPortHeld;
  346. PINIMONITOR pIniLangMonitor = NULL;
  347. PINIMONITOR pIniMonitor = NULL;
  348. PINIPORT pIniPort;
  349. LPTSTR pszPrinter = NULL;
  350. TCHAR szFullPrinter[ MAX_UNC_PRINTER_NAME ];
  351. SplInSem();
  352. //
  353. // Is the printer bidi enabled with the LM supporting
  354. // pfnGetPrinterDataFromPort? (Note: even PM can support this function)
  355. //
  356. if ( pIniPrinter->Attributes & PRINTER_ATTRIBUTE_ENABLE_BIDI ) {
  357. pIniLangMonitor = pIniPrinter->pIniDriver->pIniLangMonitor;
  358. // SPLASSERT(pIniLangMonitor);
  359. if ( pIniLangMonitor &&
  360. !pIniLangMonitor->Monitor2.pfnGetPrinterDataFromPort )
  361. pIniLangMonitor = NULL;
  362. }
  363. //
  364. // Initialize to max
  365. //
  366. dwFirstPortWithNoJobs = dwFirstPortHeld = pIniPrinter->cPorts;
  367. for ( i = 0 ; i < pIniPrinter->cPorts ; ++i ) {
  368. pIniPort = pIniPrinter->ppIniPorts[i];
  369. //
  370. // Skip ports that can't be used
  371. //
  372. if ( !AvailableBidiPort(pIniPort, pIniLangMonitor) )
  373. continue;
  374. //
  375. // Port does not need closing?
  376. //
  377. if ( pIniLangMonitor == pIniPort->pIniLangMonitor ) {
  378. //
  379. // If no jobs also then great let's use it
  380. //
  381. if ( !pIniPort->pIniJob )
  382. goto PortFound;
  383. if ( dwFirstPortHeld == pIniPrinter->cPorts ) {
  384. dwFirstPortHeld = i;
  385. }
  386. } else if ( !pIniPort->pIniJob &&
  387. dwFirstPortWithNoJobs == pIniPrinter->cPorts ) {
  388. dwFirstPortWithNoJobs = i;
  389. }
  390. }
  391. //
  392. // If all ports need closing as well as have jobs let's quit
  393. //
  394. if ( dwFirstPortWithNoJobs == pIniPrinter->cPorts &&
  395. dwFirstPortHeld == pIniPrinter->cPorts ) {
  396. return rc; //Didn't leave CS and did not unset event
  397. }
  398. //
  399. // We will prefer a port with no jobs (even thought it requires closing)
  400. //
  401. if ( dwFirstPortWithNoJobs < pIniPrinter->cPorts )
  402. pIniPort = pIniPrinter->ppIniPorts[dwFirstPortWithNoJobs];
  403. else
  404. pIniPort = pIniPrinter->ppIniPorts[dwFirstPortHeld];
  405. PortFound:
  406. SPLASSERT(AvailableBidiPort(pIniPort, pIniLangMonitor));
  407. //
  408. // Port always need to be opened.
  409. //
  410. if(pIniPrinter->pIniSpooler->SpoolerFlags & SPL_TYPE_CLUSTER)
  411. {
  412. pszPrinter = szFullPrinter;
  413. StringCchPrintf(szFullPrinter,
  414. COUNTOF(szFullPrinter),
  415. L"%ws\\%ws",
  416. pIniPrinter->pIniSpooler->pMachineName,
  417. pIniPrinter->pName);
  418. }
  419. else
  420. {
  421. pszPrinter = pIniPrinter->pName;
  422. }
  423. if ((rc = StatusFromHResult(OpenMonitorPort(pIniPort,
  424. pIniLangMonitor,
  425. pszPrinter))) == ERROR_SUCCESS)
  426. {
  427. pIniMonitor = GetOpenedMonitor(pIniPort);
  428. if (!pIniMonitor->Monitor2.pfnGetPrinterDataFromPort)
  429. {
  430. rc = ERROR_NOT_SUPPORTED;
  431. }
  432. else
  433. {
  434. LeaveSplSem();
  435. SplOutSem();
  436. if ((*pIniMonitor->Monitor2.pfnGetPrinterDataFromPort)(GetMonitorHandle(pIniPort),
  437. 0,
  438. pszValueName,
  439. NULL,
  440. 0,
  441. (LPWSTR)pData,
  442. cbBuf,
  443. pcbNeeded))
  444. {
  445. rc = ERROR_SUCCESS;
  446. }
  447. else
  448. {
  449. //
  450. // If monitor fails the call but did not do a SetLastError()
  451. // we do not want to corrupt the registry
  452. //
  453. if ((rc = GetLastError()) == ERROR_SUCCESS)
  454. {
  455. ASSERT(rc != ERROR_SUCCESS);
  456. rc = ERROR_INVALID_PARAMETER;
  457. }
  458. }
  459. EnterSplSem();
  460. SplInSem();
  461. }
  462. ReleaseMonitorPort(pIniPort);
  463. }
  464. return rc;
  465. }
  466. DWORD
  467. SplGetPrintProcCaps(
  468. PSPOOL pSpool,
  469. LPWSTR pDatatype,
  470. LPBYTE pData,
  471. DWORD nSize,
  472. LPDWORD pcbNeeded
  473. )
  474. /*++
  475. Function Description: SplGetPrintProcCaps calls the GetPrintProcCaps function of the
  476. Print processor that supports the given datatype.
  477. Parameters: pSpool -- handle to the printer
  478. pDatatype -- string containing the datatype
  479. pData -- pointer to buffer
  480. nSize -- size of the buffer
  481. pcbNeeded -- pointer to the variable to store the required size of
  482. buffer.
  483. Return Value: Error Code
  484. --*/
  485. {
  486. PINIPRINTPROC pIniPrintProc;
  487. PINIPRINTER pIniPrinter;
  488. DWORD dwAttributes, dwIndex, dwReturn;
  489. // Find the print processor that supports this datatype
  490. pIniPrintProc = pSpool->pIniPrintProc ? pSpool->pIniPrintProc
  491. : pSpool->pIniPrinter->pIniPrintProc;
  492. pIniPrintProc = FindDatatype( pIniPrintProc, pDatatype);
  493. if (!pIniPrintProc)
  494. {
  495. return ERROR_INVALID_DATATYPE;
  496. }
  497. // Get the features supported by that print processor
  498. if (!pIniPrintProc->GetPrintProcCaps)
  499. {
  500. return ERROR_NOT_SUPPORTED;
  501. }
  502. else
  503. {
  504. pIniPrinter = pSpool->pIniPrinter;
  505. dwAttributes = pIniPrinter->Attributes;
  506. // Check for FILE: port which forces RAW spooling
  507. for (dwIndex = 0;
  508. dwIndex < pIniPrinter->cPorts;
  509. ++dwIndex)
  510. {
  511. if (!lstrcmpi(pIniPrinter->ppIniPorts[dwIndex]->pName,
  512. L"FILE:"))
  513. {
  514. // Found a FILE: port
  515. dwAttributes |= PRINTER_ATTRIBUTE_RAW_ONLY;
  516. break;
  517. }
  518. }
  519. // Disable EMF simulated features for version < 3 drivers
  520. if (pIniPrinter->pIniDriver &&
  521. (pIniPrinter->pIniDriver->cVersion < 3))
  522. {
  523. dwAttributes |= PRINTER_ATTRIBUTE_RAW_ONLY;
  524. }
  525. LeaveSplSem();
  526. dwReturn = (*(pIniPrintProc->GetPrintProcCaps))(pDatatype,
  527. dwAttributes,
  528. pData,
  529. nSize,
  530. pcbNeeded);
  531. EnterSplSem();
  532. return dwReturn;
  533. }
  534. }
  535. DWORD
  536. SplGetNonRegData(
  537. PINISPOOLER pIniSpooler,
  538. LPDWORD pType,
  539. LPBYTE pData,
  540. DWORD nSize,
  541. LPDWORD pcbNeeded,
  542. PNON_REGISTRY_DATA pNonRegData
  543. )
  544. {
  545. if ( pNonRegData->dwType == REG_SZ && pNonRegData->dwSize == 0 )
  546. *pcbNeeded = wcslen((LPWSTR) pNonRegData->pData) * sizeof(WCHAR) + sizeof(WCHAR);
  547. else
  548. *pcbNeeded = pNonRegData->dwSize;
  549. if ( *pcbNeeded > nSize )
  550. return ERROR_MORE_DATA;
  551. CopyMemory(pData, (LPBYTE)pNonRegData->pData, *pcbNeeded);
  552. *pType = pNonRegData->dwType;
  553. return ERROR_SUCCESS;
  554. }
  555. DWORD
  556. SplGetPrinterData(
  557. HANDLE hPrinter,
  558. LPWSTR pValueName,
  559. LPDWORD pType,
  560. LPBYTE pData,
  561. DWORD nSize,
  562. LPDWORD pcbNeeded
  563. )
  564. {
  565. PSPOOL pSpool=(PSPOOL)hPrinter;
  566. DWORD rc = ERROR_INVALID_HANDLE;
  567. DWORD dwResult;
  568. PSERVER_DATA pRegistry; // points to table of Print Server registry entries
  569. PNON_REGISTRY_DATA pNonReg;
  570. PNON_REGISTRY_FCN pNonRegFcn;
  571. HKEY hPrintKey;
  572. PINIPRINTER pIniPrinter;
  573. HKEY hKey = NULL;
  574. DWORD dwType;
  575. PINISPOOLER pIniSpoolerOut;
  576. HANDLE hToken = NULL;
  577. WCHAR szPrintProcKey[] = L"PrintProcCaps_";
  578. LPWSTR pDatatype;
  579. if (!ValidateSpoolHandle(pSpool, 0)) {
  580. return rc;
  581. }
  582. if (!pValueName || !pcbNeeded) {
  583. rc = ERROR_INVALID_PARAMETER;
  584. return rc;
  585. }
  586. if (pType)
  587. dwType = *pType; // pType may be NULL
  588. //
  589. // Server Handle
  590. //
  591. if (pSpool->TypeofHandle & PRINTER_HANDLE_SERVER) {
  592. //
  593. // Check Registry Table
  594. //
  595. for (pRegistry = gpServerRegistry ; pRegistry->pValue ; ++pRegistry) {
  596. if (!_wcsicmp(pRegistry->pValue, pValueName)) {
  597. //
  598. // Retrieve the handle for the Get.
  599. if ((rc = GetServerKeyHandle(pSpool->pIniSpooler,
  600. pRegistry->eKey,
  601. &hPrintKey,
  602. &pIniSpoolerOut)) == ERROR_SUCCESS) {
  603. *pcbNeeded = nSize;
  604. rc = SplRegQueryValue(hPrintKey, pValueName, pType, pData, pcbNeeded, pIniSpoolerOut);
  605. CloseServerKeyHandle( pRegistry->eKey,
  606. hPrintKey,
  607. pIniSpoolerOut );
  608. }
  609. break;
  610. }
  611. }
  612. if (!pRegistry->pValue) { // May be a non-registry entry
  613. for (pNonReg = gpNonRegistryData ; pNonReg->pValue ; ++pNonReg) {
  614. if (!_wcsicmp(pNonReg->pValue, pValueName)) {
  615. rc = SplGetNonRegData(pSpool->pIniSpooler,
  616. &dwType,
  617. pData,
  618. nSize,
  619. pcbNeeded,
  620. pNonReg);
  621. if (pType)
  622. *pType = dwType;
  623. goto FinishNonReg;
  624. }
  625. }
  626. for (pNonRegFcn = gpNonRegistryFcn ; pNonRegFcn->pValue ; ++pNonRegFcn) {
  627. if (!_wcsicmp(pNonRegFcn->pValue, pValueName)) {
  628. rc = (*pNonRegFcn->pGet)(pSpool->pIniSpooler, &dwType, pData, nSize, pcbNeeded);
  629. if (pType)
  630. *pType = dwType;
  631. goto FinishNonReg;
  632. }
  633. }
  634. FinishNonReg:
  635. if (!pNonReg->pValue && !pNonRegFcn->pValue) {
  636. rc = ERROR_INVALID_PARAMETER;
  637. }
  638. }
  639. // Printer handle
  640. } else {
  641. PPRINTER_NON_REGISTRY_FCN pPrinterNonRegFcn;
  642. EnterSplSem();
  643. pIniPrinter = pSpool->pIniPrinter;
  644. SPLASSERT(pIniPrinter && pIniPrinter->signature == IP_SIGNATURE);
  645. //
  646. // If the pValueName is "PrintProcCaps_[datatype]" call the print processor which
  647. // supports that datatype and return the options that it supports.
  648. //
  649. if (pValueName == wcsstr(pValueName, szPrintProcKey)) {
  650. pDatatype = (LPWSTR) (pValueName+(wcslen(szPrintProcKey)));
  651. if (!pDatatype) {
  652. LeaveSplSem();
  653. return ERROR_INVALID_DATATYPE;
  654. } else {
  655. rc = SplGetPrintProcCaps(pSpool,
  656. pDatatype,
  657. pData,
  658. nSize,
  659. pcbNeeded);
  660. LeaveSplSem();
  661. return rc;
  662. }
  663. }
  664. //
  665. // Check for PrinterNonReg calls.
  666. //
  667. for (pPrinterNonRegFcn = gpPrinterNonRegistryFcn ;
  668. pPrinterNonRegFcn->pValue ;
  669. ++pPrinterNonRegFcn) {
  670. if (!_wcsicmp(pPrinterNonRegFcn->pValue, pValueName)) {
  671. rc = (*pPrinterNonRegFcn->pGet)( pSpool,
  672. &dwType,
  673. pData,
  674. nSize,
  675. pcbNeeded );
  676. if( pType ){
  677. *pType = dwType;
  678. }
  679. LeaveSplSem();
  680. return rc;
  681. }
  682. }
  683. if (pIniPrinter->Status & PRINTER_PENDING_CREATION) {
  684. LeaveSplSem();
  685. rc = ERROR_INVALID_PRINTER_STATE;
  686. } else {
  687. //
  688. // During upgrade do not try to talk to the port since we
  689. // will not be on the net
  690. //
  691. if ( dwUpgradeFlag == 0 &&
  692. AccessGranted(SPOOLER_OBJECT_PRINTER,
  693. PRINTER_ACCESS_ADMINISTER,
  694. pSpool ) ) {
  695. rc = GetPrinterDataFromPort(pIniPrinter,
  696. pValueName,
  697. pData,
  698. nSize,
  699. pcbNeeded);
  700. }
  701. hToken = RevertToPrinterSelf();
  702. dwResult = OpenPrinterKey(pIniPrinter,
  703. KEY_READ | KEY_WRITE,
  704. &hKey,
  705. szPrinterData,
  706. FALSE);
  707. if (hToken && !ImpersonatePrinterClient(hToken))
  708. {
  709. dwResult = GetLastError();
  710. }
  711. if (dwResult != ERROR_SUCCESS) {
  712. LeaveSplSem();
  713. return dwResult;
  714. }
  715. if ( rc == ERROR_SUCCESS ) {
  716. *pType = REG_BINARY;
  717. (VOID)SetPrinterDataPrinter(hPrinter,
  718. NULL,
  719. hKey,
  720. pValueName,
  721. *pType,
  722. pData,
  723. *pcbNeeded,
  724. SET_PRINTER_DATA);
  725. } else if ( rc != ERROR_INSUFFICIENT_BUFFER ) {
  726. *pcbNeeded = nSize;
  727. rc = SplRegQueryValue( hKey,
  728. pValueName,
  729. pType,
  730. pData,
  731. pcbNeeded,
  732. pIniPrinter->pIniSpooler );
  733. }
  734. LeaveSplSem();
  735. }
  736. }
  737. if (hKey)
  738. SplRegCloseKey(hKey, pIniPrinter->pIniSpooler);
  739. SplOutSem();
  740. return rc;
  741. }
  742. DWORD
  743. SplGetPrinterDataEx(
  744. HANDLE hPrinter,
  745. LPCWSTR pKeyName,
  746. LPCWSTR pValueName,
  747. LPDWORD pType,
  748. LPBYTE pData,
  749. DWORD nSize,
  750. LPDWORD pcbNeeded
  751. )
  752. {
  753. PSPOOL pSpool=(PSPOOL)hPrinter;
  754. DWORD rc = ERROR_INVALID_HANDLE;
  755. PSERVER_DATA pRegistry; // points to table of Print Server registry entries
  756. PINIPRINTER pIniPrinter;
  757. HKEY hKey = NULL;
  758. HANDLE hToken = NULL;
  759. if (!ValidateSpoolHandle(pSpool, 0)) {
  760. goto Cleanup;
  761. }
  762. if (!pValueName || !pcbNeeded) {
  763. rc = ERROR_INVALID_PARAMETER;
  764. goto Cleanup;
  765. }
  766. if (pSpool->TypeofHandle & PRINTER_HANDLE_SERVER) {
  767. rc = SplGetPrinterData( hPrinter,
  768. (LPWSTR) pValueName,
  769. pType,
  770. pData,
  771. nSize,
  772. pcbNeeded);
  773. } else {
  774. if (!pKeyName || !*pKeyName) {
  775. rc = ERROR_INVALID_PARAMETER;
  776. goto Cleanup;
  777. }
  778. EnterSplSem();
  779. pIniPrinter = pSpool->pIniPrinter;
  780. INCPRINTERREF(pIniPrinter);
  781. SPLASSERT(pIniPrinter && pIniPrinter->signature == IP_SIGNATURE);
  782. if (pIniPrinter->Status & PRINTER_PENDING_CREATION) {
  783. LeaveSplSem();
  784. rc = ERROR_INVALID_PRINTER_STATE;
  785. } else if (!_wcsicmp(pKeyName, szPrinterData)) {
  786. LeaveSplSem();
  787. rc = SplGetPrinterData( hPrinter,
  788. (LPWSTR) pValueName,
  789. pType,
  790. pData,
  791. nSize,
  792. pcbNeeded);
  793. } else {
  794. hToken = RevertToPrinterSelf();
  795. rc = OpenPrinterKey(pIniPrinter, KEY_READ, &hKey, pKeyName, TRUE);
  796. if (hToken && !ImpersonatePrinterClient(hToken)) {
  797. rc = GetLastError();
  798. }
  799. LeaveSplSem();
  800. if (rc == ERROR_SUCCESS) {
  801. *pcbNeeded = nSize;
  802. rc = SplRegQueryValue(hKey,
  803. pValueName,
  804. pType,
  805. pData,
  806. pcbNeeded,
  807. pIniPrinter->pIniSpooler);
  808. }
  809. }
  810. EnterSplSem();
  811. if (hKey)
  812. SplRegCloseKey(hKey, pIniPrinter->pIniSpooler);
  813. DECPRINTERREF(pIniPrinter);
  814. LeaveSplSem();
  815. }
  816. Cleanup:
  817. SplOutSem();
  818. return rc;
  819. }
  820. DWORD
  821. SplEnumPrinterData(
  822. HANDLE hPrinter,
  823. DWORD dwIndex, // index of value to query
  824. LPWSTR pValueName, // address of buffer for value string
  825. DWORD cbValueName, // size of buffer for value string
  826. LPDWORD pcbValueName, // address for size of value buffer
  827. LPDWORD pType, // address of buffer for type code
  828. LPBYTE pData, // address of buffer for value data
  829. DWORD cbData, // size of buffer for value data
  830. LPDWORD pcbData // address for size of data buffer
  831. )
  832. {
  833. PSPOOL pSpool=(PSPOOL)hPrinter;
  834. DWORD rc = ERROR_INVALID_HANDLE;
  835. HKEY hKey = NULL;
  836. PINIPRINTER pIniPrinter;
  837. HANDLE hToken = NULL;
  838. if (!ValidateSpoolHandle(pSpool, PRINTER_HANDLE_SERVER)) {
  839. return rc;
  840. }
  841. if (!pValueName || !pcbValueName) {
  842. rc = ERROR_INVALID_PARAMETER;
  843. return rc;
  844. }
  845. EnterSplSem();
  846. pIniPrinter = pSpool->pIniPrinter;
  847. SPLASSERT(pIniPrinter && pIniPrinter->signature == IP_SIGNATURE);
  848. if (pIniPrinter->Status & PRINTER_PENDING_CREATION) {
  849. LeaveSplSem();
  850. rc = ERROR_INVALID_PRINTER_STATE;
  851. } else {
  852. hToken = RevertToPrinterSelf();
  853. rc = OpenPrinterKey(pSpool->pIniPrinter, KEY_READ, &hKey, szPrinterData, TRUE);
  854. if (hToken && !ImpersonatePrinterClient(hToken))
  855. {
  856. rc = GetLastError();
  857. }
  858. LeaveSplSem();
  859. if (rc == ERROR_SUCCESS) {
  860. if (!cbValueName && !cbData) { // Both sizes are NULL, so user wants to get buffer sizes
  861. rc = SplRegQueryInfoKey( hKey,
  862. NULL,
  863. NULL,
  864. NULL,
  865. pcbValueName,
  866. pcbData,
  867. NULL,
  868. NULL,
  869. pIniPrinter->pIniSpooler );
  870. } else {
  871. *pcbValueName = cbValueName/sizeof(WCHAR);
  872. *pcbData = cbData;
  873. rc = SplRegEnumValue( hKey,
  874. dwIndex,
  875. pValueName,
  876. pcbValueName,
  877. pType,
  878. pData,
  879. pcbData,
  880. pIniPrinter->pIniSpooler );
  881. *pcbValueName = (*pcbValueName + 1)*sizeof(WCHAR);
  882. }
  883. }
  884. if (hKey)
  885. {
  886. SplRegCloseKey(hKey, pIniPrinter->pIniSpooler);
  887. }
  888. }
  889. return rc;
  890. }
  891. DWORD
  892. SplEnumPrinterDataEx(
  893. HANDLE hPrinter,
  894. LPCWSTR pKeyName, // key name
  895. LPBYTE pEnumValueStart,
  896. DWORD cbEnumValues,
  897. LPDWORD pcbEnumValues,
  898. LPDWORD pnEnumValues // number of values returned
  899. )
  900. {
  901. PSPOOL pSpool=(PSPOOL)hPrinter;
  902. BOOL bRet = FALSE;
  903. DWORD rc = ERROR_SUCCESS;
  904. PINIPRINTER pIniPrinter;
  905. HKEY hKey = NULL;
  906. DWORD i;
  907. LPWSTR pNextValueName, pValueName = NULL;
  908. LPBYTE pData = NULL;
  909. PPRINTER_ENUM_VALUES pEnumValue;
  910. DWORD cchValueName, cbData, cchValueNameTemp, cbDataTemp;
  911. DWORD dwType, cbSourceDir=0, cbTargetDir=0;
  912. HANDLE hToken = NULL;
  913. LPWSTR pszSourceDir = NULL, pszTargetDir = NULL;
  914. EnterSplSem();
  915. if (!ValidateSpoolHandle(pSpool, PRINTER_HANDLE_SERVER)) {
  916. LeaveSplSem();
  917. return ERROR_INVALID_HANDLE;
  918. }
  919. if (!pKeyName || !*pKeyName) {
  920. LeaveSplSem();
  921. return ERROR_INVALID_PARAMETER;
  922. }
  923. *pcbEnumValues = 0;
  924. *pnEnumValues = 0;
  925. pIniPrinter = pSpool->pIniPrinter;
  926. SPLASSERT(pIniPrinter && pIniPrinter->signature == IP_SIGNATURE);
  927. if (pIniPrinter->Status & PRINTER_PENDING_CREATION) {
  928. LeaveSplSem();
  929. rc = ERROR_INVALID_PRINTER_STATE;
  930. goto Cleanup;
  931. }
  932. //
  933. // open specified key
  934. //
  935. hToken = RevertToPrinterSelf();
  936. rc = OpenPrinterKey(pIniPrinter, KEY_READ, &hKey, pKeyName, TRUE);
  937. if (hToken && !ImpersonatePrinterClient(hToken))
  938. {
  939. rc = GetLastError();
  940. }
  941. LeaveSplSem();
  942. if (rc != ERROR_SUCCESS) {
  943. goto Cleanup;
  944. }
  945. do {
  946. // Get the max size
  947. rc = SplRegQueryInfoKey( hKey,
  948. NULL,
  949. NULL,
  950. pnEnumValues,
  951. &cchValueName,
  952. &cbData,
  953. NULL,
  954. NULL,
  955. pIniPrinter->pIniSpooler );
  956. if (rc != ERROR_SUCCESS)
  957. goto Cleanup;
  958. cchValueName = (cchValueName + 1);
  959. cbData = (cbData + 1) & ~1;
  960. // Allocate temporary buffers to determine true required size
  961. if (!(pValueName = AllocSplMem(cchValueName * sizeof (WCHAR)))) {
  962. rc = GetLastError();
  963. goto Cleanup;
  964. }
  965. if (!(pData = AllocSplMem(cbData))) {
  966. rc = GetLastError();
  967. goto Cleanup;
  968. }
  969. // Run through Values and accumulate sizes
  970. for (i = 0 ; rc == ERROR_SUCCESS && i < *pnEnumValues ; ++i) {
  971. cchValueNameTemp = cchValueName;
  972. cbDataTemp = cbData;
  973. rc = SplRegEnumValue( hKey,
  974. i,
  975. pValueName,
  976. &cchValueNameTemp,
  977. &dwType,
  978. pData,
  979. &cbDataTemp,
  980. pIniPrinter->pIniSpooler);
  981. *pcbEnumValues = (DWORD) AlignToRegType(*pcbEnumValues, REG_SZ) +
  982. (cchValueNameTemp + 1)*sizeof(WCHAR);
  983. *pcbEnumValues = (DWORD) AlignToRegType(*pcbEnumValues, dwType) +
  984. cbDataTemp;
  985. }
  986. //
  987. // If the key is a sub key of "CopyFiles" we need to generate
  988. // the paths for the source/target directories if the call is remote
  989. //
  990. if ( (pSpool->TypeofHandle & PRINTER_HANDLE_REMOTE_DATA) &&
  991. !wcsncmp(pKeyName, L"CopyFiles\\", wcslen(L"CopyFiles\\")) ) {
  992. if ( !GenerateDirectoryNamesForCopyFilesKey(pSpool,
  993. hKey,
  994. &pszSourceDir,
  995. &pszTargetDir,
  996. cchValueName*sizeof (WCHAR)) ) {
  997. rc = GetLastError();
  998. goto Cleanup;
  999. } else {
  1000. SPLASSERT(pszSourceDir && pszTargetDir);
  1001. if ( pszSourceDir ) {
  1002. cbSourceDir = (wcslen(pszSourceDir) + 1)*sizeof(WCHAR);
  1003. *pcbEnumValues = (DWORD) AlignToRegType(*pcbEnumValues, REG_SZ) +
  1004. sizeof(L"SourceDir") + sizeof(WCHAR);
  1005. *pcbEnumValues = (DWORD) AlignToRegType(*pcbEnumValues, REG_SZ) +
  1006. cbSourceDir;
  1007. (*pnEnumValues)++;
  1008. }
  1009. if ( pszTargetDir ) {
  1010. cbTargetDir = (wcslen(pszTargetDir) + 1)*sizeof(WCHAR);
  1011. *pcbEnumValues = (DWORD)AlignToRegType(*pcbEnumValues, REG_SZ) +
  1012. sizeof(L"TargetDir") + sizeof(WCHAR);
  1013. *pcbEnumValues = (DWORD)AlignToRegType(*pcbEnumValues, REG_SZ) +
  1014. cbTargetDir;
  1015. (*pnEnumValues)++;
  1016. }
  1017. }
  1018. }
  1019. *pcbEnumValues += sizeof(PRINTER_ENUM_VALUES) * *pnEnumValues;
  1020. if (rc == ERROR_SUCCESS) {
  1021. if (*pcbEnumValues > cbEnumValues) {
  1022. rc = ERROR_MORE_DATA;
  1023. break;
  1024. } else {
  1025. //
  1026. // Adjust pointers & Get data
  1027. //
  1028. pEnumValue = (PPRINTER_ENUM_VALUES) pEnumValueStart;
  1029. pNextValueName = (LPWSTR) (pEnumValueStart + *pnEnumValues*sizeof(PRINTER_ENUM_VALUES));
  1030. pNextValueName = (LPWSTR) AlignToRegType((ULONG_PTR)pNextValueName, REG_SZ);
  1031. for(i = 0 ; rc == ERROR_SUCCESS && i < *pnEnumValues ; ++i, ++pEnumValue) {
  1032. //
  1033. // bytes left in the allocated buffer
  1034. //
  1035. DWORD cbRemaining = (DWORD)(pEnumValueStart + cbEnumValues - (LPBYTE)pNextValueName);
  1036. pEnumValue->pValueName = pNextValueName;
  1037. //
  1038. // use minimum of cbRemaining and max size
  1039. //
  1040. pEnumValue->cbValueName = (cbRemaining < cchValueName*sizeof (WCHAR)) ? cbRemaining : cchValueName*sizeof (WCHAR);
  1041. pEnumValue->cbData = cbData;
  1042. if (i == *pnEnumValues - 2 && cbSourceDir) {
  1043. pEnumValue->dwType = REG_SZ;
  1044. pEnumValue->cbData = cbSourceDir;
  1045. pEnumValue->cbValueName = sizeof(L"SourceDir") + sizeof(WCHAR);
  1046. pEnumValue->pData = (LPBYTE) pEnumValue->pValueName +
  1047. pEnumValue->cbValueName;
  1048. pEnumValue->pData = (LPBYTE) AlignToRegType((ULONG_PTR)pEnumValue->pData,
  1049. pEnumValue->dwType);
  1050. //
  1051. // We have calculated the minimum buffer size necessary already.
  1052. //
  1053. StringCbCopy(pEnumValue->pValueName, pEnumValue->cbValueName, L"SourceDir");
  1054. StringCbCopy((LPWSTR)pEnumValue->pData, cbRemaining, pszSourceDir);
  1055. } else if ( i == *pnEnumValues - 1 && cbTargetDir ) {
  1056. pEnumValue->dwType = REG_SZ;
  1057. pEnumValue->cbData = cbTargetDir;
  1058. pEnumValue->cbValueName = sizeof(L"TargetDir") +
  1059. sizeof(WCHAR);
  1060. pEnumValue->pData = (LPBYTE) pEnumValue->pValueName +
  1061. pEnumValue->cbValueName;
  1062. pEnumValue->pData = (LPBYTE) AlignToRegType((ULONG_PTR )pEnumValue->pData,
  1063. pEnumValue->dwType);
  1064. StringCbCopy(pEnumValue->pValueName, pEnumValue->cbValueName, L"TargetDir");
  1065. StringCbCopy((LPWSTR)pEnumValue->pData, cbRemaining, pszTargetDir);
  1066. } else {
  1067. DWORD cchValueName = pEnumValue->cbValueName / sizeof (WCHAR);
  1068. //
  1069. // adjust to count of characters
  1070. //
  1071. rc = SplRegEnumValue(hKey,
  1072. i,
  1073. pEnumValue->pValueName,
  1074. &cchValueName,
  1075. &pEnumValue->dwType,
  1076. pData,
  1077. &pEnumValue->cbData,
  1078. pIniPrinter->pIniSpooler);
  1079. pEnumValue->cbValueName = (cchValueName + 1)*sizeof(WCHAR);
  1080. pEnumValue->pData = (LPBYTE) pEnumValue->pValueName + pEnumValue->cbValueName;
  1081. pEnumValue->pData = (LPBYTE) AlignToRegType((ULONG_PTR)pEnumValue->pData,
  1082. pEnumValue->dwType);
  1083. CopyMemory(pEnumValue->pData, pData, pEnumValue->cbData);
  1084. }
  1085. if (i + 1 < *pnEnumValues) {
  1086. pNextValueName = (LPWSTR) AlignToRegType((ULONG_PTR)(pEnumValue->pData +
  1087. pEnumValue->cbData), REG_SZ);
  1088. }
  1089. if (pEnumValue->cbData == 0) {
  1090. pEnumValue->pData = NULL;
  1091. }
  1092. }
  1093. if (rc == ERROR_NO_MORE_ITEMS)
  1094. rc = ERROR_SUCCESS;
  1095. }
  1096. }
  1097. FreeSplMem(pValueName);
  1098. FreeSplMem(pData);
  1099. pValueName = (LPWSTR) pData = NULL;
  1100. } while(rc == ERROR_MORE_DATA);
  1101. if ( rc == ERROR_SUCCESS )
  1102. bRet = TRUE;
  1103. Cleanup:
  1104. SplOutSem();
  1105. FreeSplStr(pszTargetDir);
  1106. FreeSplStr(pszSourceDir);
  1107. FreeSplMem(pValueName);
  1108. FreeSplMem(pData);
  1109. // Close handle
  1110. if (hKey)
  1111. SplRegCloseKey(hKey, pIniPrinter->pIniSpooler);
  1112. if ( !bRet && rc == ERROR_SUCCESS ) {
  1113. // SPLASSERT(dwLastError == ERROR_SUCCESS); -- after ICM is fixed
  1114. rc = ERROR_INVALID_PARAMETER;
  1115. }
  1116. return rc;
  1117. }
  1118. DWORD
  1119. SplEnumPrinterKey(
  1120. HANDLE hPrinter,
  1121. LPCWSTR pKeyName, // key name
  1122. LPWSTR pSubKey, // address of buffer for value string
  1123. DWORD cbSubKey, // size of buffer for value string
  1124. LPDWORD pcbSubKey // address for size of value buffer
  1125. )
  1126. {
  1127. HKEY hKey = NULL;
  1128. PSPOOL pSpool=(PSPOOL)hPrinter;
  1129. DWORD rc = ERROR_SUCCESS;
  1130. PINIPRINTER pIniPrinter;
  1131. PINISPOOLER pIniSpooler;
  1132. LPWSTR pRootKeyName;
  1133. DWORD cbSubKeyMax;
  1134. DWORD cwSubKeyMax;
  1135. DWORD cwSubKey, cwSubKeyTotal, cbSubKeyTotal, cwSubKeyOutput;
  1136. DWORD dwIndex;
  1137. DWORD nSubKeys;
  1138. LPWSTR pKeys = NULL;
  1139. HANDLE hToken = NULL;
  1140. if (!ValidateSpoolHandle(pSpool, PRINTER_HANDLE_SERVER)) {
  1141. return ERROR_INVALID_HANDLE;
  1142. }
  1143. if (!pKeyName || !pcbSubKey) {
  1144. return ERROR_INVALID_PARAMETER;
  1145. }
  1146. EnterSplSem();
  1147. pIniPrinter = pSpool->pIniPrinter;
  1148. SPLASSERT(pIniPrinter && pIniPrinter->signature == IP_SIGNATURE);
  1149. if (pIniPrinter->Status & PRINTER_PENDING_CREATION) {
  1150. LeaveSplSem();
  1151. rc = ERROR_INVALID_PRINTER_STATE;
  1152. goto Cleanup;
  1153. }
  1154. //
  1155. // open specified key
  1156. //
  1157. hToken = RevertToPrinterSelf();
  1158. rc = OpenPrinterKey(pIniPrinter, KEY_READ, &hKey, pKeyName, TRUE);
  1159. if (hToken && !ImpersonatePrinterClient(hToken))
  1160. {
  1161. rc = GetLastError();
  1162. }
  1163. LeaveSplSem();
  1164. if (rc != ERROR_SUCCESS)
  1165. goto Cleanup;
  1166. do {
  1167. // Get the max size
  1168. rc = SplRegQueryInfoKey( hKey, // Key
  1169. &nSubKeys, // lpcSubKeys
  1170. &cwSubKeyMax, // lpcbMaxSubKeyLen
  1171. NULL,
  1172. NULL,
  1173. NULL,
  1174. NULL,
  1175. NULL,
  1176. pIniPrinter->pIniSpooler );
  1177. if (rc != ERROR_SUCCESS)
  1178. goto Cleanup;
  1179. ++cwSubKeyMax; // Add terminating NULL
  1180. cbSubKeyMax = (cwSubKeyMax + 1)*sizeof(WCHAR);
  1181. if (!(pKeys = AllocSplMem(cbSubKeyMax))) {
  1182. rc = GetLastError();
  1183. goto Cleanup;
  1184. }
  1185. // Enumerate keys to get exact size
  1186. for(dwIndex = cwSubKeyTotal = 0 ; dwIndex < nSubKeys && rc == ERROR_SUCCESS ; ++dwIndex) {
  1187. cwSubKey = cwSubKeyMax;
  1188. rc = SplRegEnumKey( hKey,
  1189. dwIndex,
  1190. pKeys,
  1191. &cwSubKey,
  1192. NULL,
  1193. pIniPrinter->pIniSpooler );
  1194. cwSubKeyTotal += cwSubKey + 1;
  1195. }
  1196. //
  1197. // cwSubKeyTotal is being reset in the initialization list of the foor loop. Thus
  1198. // its value is not accurate if we do not enter the loop at all (when nSubKeys is 0)
  1199. //
  1200. *pcbSubKey = nSubKeys ? cwSubKeyTotal*sizeof(WCHAR) + sizeof(WCHAR) : 2*sizeof(WCHAR);
  1201. if (rc == ERROR_SUCCESS) {
  1202. if(*pcbSubKey > cbSubKey) {
  1203. rc = ERROR_MORE_DATA;
  1204. break;
  1205. } else {
  1206. //
  1207. // cwSubKeyOutput is the size of the output buffer in wchar
  1208. //
  1209. cwSubKeyOutput = cbSubKey/sizeof(WCHAR);
  1210. for(dwIndex = cwSubKeyTotal = 0 ; dwIndex < nSubKeys && rc == ERROR_SUCCESS ; ++dwIndex) {
  1211. //
  1212. // Calculate the remaining output buffer size in characters.
  1213. // If we're out of room, exit with ERROR_MORE_DATA.
  1214. // This is needed since it is possible the registry has changed.
  1215. //
  1216. if (cwSubKeyOutput < cwSubKeyTotal + 1) {
  1217. rc = ERROR_MORE_DATA;
  1218. break;
  1219. }
  1220. cwSubKey = cwSubKeyOutput - cwSubKeyTotal;
  1221. rc = SplRegEnumKey( hKey,
  1222. dwIndex,
  1223. pSubKey + cwSubKeyTotal,
  1224. &cwSubKey,
  1225. NULL,
  1226. pIniPrinter->pIniSpooler );
  1227. cwSubKeyTotal += cwSubKey + 1;
  1228. }
  1229. //
  1230. // cwSubKeyTotal is being reset in the initialization list of the foor loop. Thus
  1231. // its value is not accurate if we do not enter the loop at all (when nSubKeys is 0)
  1232. // If we don't enter the for loop, then we don't need to update *pcbSubKey
  1233. //
  1234. if (nSubKeys && (dwIndex == nSubKeys || rc == ERROR_NO_MORE_ITEMS)) {
  1235. //
  1236. // Get the most recent data size just in case something changed
  1237. //
  1238. *pcbSubKey = cwSubKeyTotal*sizeof(WCHAR) + sizeof(WCHAR);
  1239. rc = ERROR_SUCCESS;
  1240. }
  1241. }
  1242. }
  1243. FreeSplMem(pKeys);
  1244. pKeys = NULL;
  1245. } while(rc == ERROR_MORE_DATA);
  1246. Cleanup:
  1247. //
  1248. // Close handles
  1249. //
  1250. if (hKey)
  1251. SplRegCloseKey(hKey, pIniPrinter->pIniSpooler);
  1252. FreeSplMem(pKeys);
  1253. return rc;
  1254. }
  1255. DWORD
  1256. SplDeletePrinterData(
  1257. HANDLE hPrinter,
  1258. LPWSTR pValueName
  1259. )
  1260. {
  1261. PSPOOL pSpool = (PSPOOL)hPrinter;
  1262. DWORD rc = ERROR_INVALID_HANDLE;
  1263. HKEY hKey = NULL;
  1264. HANDLE hToken = NULL;
  1265. EnterSplSem();
  1266. if (ValidateSpoolHandle(pSpool, PRINTER_HANDLE_SERVER)) {
  1267. hToken = RevertToPrinterSelf();
  1268. rc = OpenPrinterKey(pSpool->pIniPrinter, KEY_WRITE, &hKey, szPrinterData, FALSE);
  1269. if (hToken && !ImpersonatePrinterClient(hToken))
  1270. {
  1271. rc = GetLastError();
  1272. }
  1273. if (rc == ERROR_SUCCESS) {
  1274. rc = SetPrinterDataPrinter( hPrinter,
  1275. NULL,
  1276. hKey,
  1277. pValueName,
  1278. 0, NULL, 0, DELETE_PRINTER_DATA);
  1279. }
  1280. }
  1281. LeaveSplSem();
  1282. if (hKey)
  1283. {
  1284. SplRegCloseKey(hKey, pSpool->pIniPrinter->pIniSpooler);
  1285. }
  1286. return rc;
  1287. }
  1288. DWORD
  1289. SplDeletePrinterDataEx(
  1290. HANDLE hPrinter,
  1291. LPCWSTR pKeyName,
  1292. LPCWSTR pValueName
  1293. )
  1294. {
  1295. PSPOOL pSpool = (PSPOOL)hPrinter;
  1296. DWORD rc = ERROR_INVALID_HANDLE;
  1297. HANDLE hToken = NULL;
  1298. HKEY hKey = NULL;
  1299. if (!pKeyName || !*pKeyName) {
  1300. return ERROR_INVALID_PARAMETER;
  1301. }
  1302. EnterSplSem();
  1303. if (ValidateSpoolHandle(pSpool, PRINTER_HANDLE_SERVER)) {
  1304. hToken = RevertToPrinterSelf();
  1305. rc = OpenPrinterKey(pSpool->pIniPrinter, KEY_WRITE, &hKey, pKeyName, TRUE);
  1306. if (hToken && !ImpersonatePrinterClient(hToken))
  1307. {
  1308. rc = GetLastError();
  1309. }
  1310. if (rc == ERROR_SUCCESS)
  1311. rc = SetPrinterDataPrinter(hPrinter,
  1312. NULL,
  1313. hKey,
  1314. (LPWSTR) pValueName,
  1315. 0, NULL, 0, DELETE_PRINTER_DATA);
  1316. }
  1317. LeaveSplSem();
  1318. if (hKey)
  1319. SplRegCloseKey(hKey, pSpool->pIniSpooler);
  1320. return rc;
  1321. }
  1322. DWORD
  1323. SplDeletePrinterKey(
  1324. HANDLE hPrinter,
  1325. LPCWSTR pKeyName
  1326. )
  1327. {
  1328. PSPOOL pSpool = (PSPOOL)hPrinter;
  1329. DWORD rc = ERROR_INVALID_HANDLE;
  1330. HANDLE hToken = NULL;
  1331. HKEY hKey = NULL, hPrinterKey = NULL;
  1332. if (!pKeyName)
  1333. return ERROR_INVALID_PARAMETER;
  1334. EnterSplSem();
  1335. if (ValidateSpoolHandle(pSpool, PRINTER_HANDLE_SERVER)) {
  1336. hToken = RevertToPrinterSelf();
  1337. rc = OpenPrinterKey(pSpool->pIniPrinter, KEY_WRITE | KEY_READ | DELETE, &hKey, pKeyName, TRUE);
  1338. if (rc == ERROR_SUCCESS)
  1339. rc = OpenPrinterKey(pSpool->pIniPrinter, KEY_WRITE | KEY_READ | DELETE, &hPrinterKey, NULL, TRUE);
  1340. if (hToken)
  1341. ImpersonatePrinterClient(hToken);
  1342. if (rc == ERROR_SUCCESS) {
  1343. rc = SetPrinterDataPrinter(hPrinter,
  1344. hPrinterKey,
  1345. hKey,
  1346. (LPWSTR) pKeyName,
  1347. 0, NULL, 0, DELETE_PRINTER_KEY);
  1348. }
  1349. }
  1350. LeaveSplSem();
  1351. if (hPrinterKey)
  1352. SplRegCloseKey(hPrinterKey, pSpool->pIniSpooler);
  1353. return rc;
  1354. }
  1355. DWORD
  1356. SplSetPrinterData(
  1357. HANDLE hPrinter,
  1358. LPWSTR pValueName,
  1359. DWORD Type,
  1360. LPBYTE pData,
  1361. DWORD cbData
  1362. )
  1363. {
  1364. PSPOOL pSpool = (PSPOOL)hPrinter;
  1365. DWORD rc = ERROR_INVALID_HANDLE;
  1366. HANDLE hToken = NULL;
  1367. HKEY hKey = NULL;
  1368. EnterSplSem();
  1369. if (!ValidateSpoolHandle(pSpool, 0)) {
  1370. LeaveSplSem();
  1371. return rc;
  1372. }
  1373. if (pSpool->TypeofHandle & PRINTER_HANDLE_SERVER) {
  1374. if ( !ValidateObjectAccess( SPOOLER_OBJECT_SERVER,
  1375. SERVER_ACCESS_ADMINISTER,
  1376. NULL, NULL, pSpool->pIniSpooler)) {
  1377. rc = ERROR_ACCESS_DENIED;
  1378. } else {
  1379. rc = SetPrinterDataServer(pSpool->pIniSpooler, pValueName, Type, pData, cbData);
  1380. }
  1381. } else {
  1382. hToken = RevertToPrinterSelf();
  1383. rc = OpenPrinterKey(pSpool->pIniPrinter, KEY_READ | KEY_WRITE, &hKey, szPrinterData, FALSE);
  1384. if (hToken && !ImpersonatePrinterClient(hToken))
  1385. {
  1386. rc = GetLastError();
  1387. }
  1388. if (rc == ERROR_SUCCESS) {
  1389. rc = SetPrinterDataPrinter( hPrinter,
  1390. NULL,
  1391. hKey,
  1392. pValueName,
  1393. Type, pData, cbData, SET_PRINTER_DATA);
  1394. }
  1395. }
  1396. LeaveSplSem();
  1397. if (hKey)
  1398. {
  1399. SplRegCloseKey(hKey, pSpool->pIniPrinter->pIniSpooler);
  1400. }
  1401. return rc;
  1402. }
  1403. DWORD
  1404. SplSetPrinterDataEx(
  1405. HANDLE hPrinter,
  1406. LPCWSTR pKeyName,
  1407. LPCWSTR pValueName,
  1408. DWORD Type,
  1409. LPBYTE pData,
  1410. DWORD cbData
  1411. )
  1412. {
  1413. PSPOOL pSpool = (PSPOOL)hPrinter;
  1414. DWORD rc = ERROR_INVALID_HANDLE;
  1415. HANDLE hToken = NULL;
  1416. PINIPRINTER pIniPrinter;
  1417. PINIJOB pIniJob;
  1418. HKEY hKey = NULL;
  1419. PINISPOOLER pIniSpooler;
  1420. LPWSTR pPrinterKeyName;
  1421. DWORD DsUpdate = 0;
  1422. if (!ValidateSpoolHandle(pSpool, 0)){
  1423. goto Done;
  1424. }
  1425. if (!pValueName) {
  1426. rc = ERROR_INVALID_PARAMETER;
  1427. goto Done;
  1428. }
  1429. if (pSpool->TypeofHandle & PRINTER_HANDLE_SERVER) {
  1430. return SplSetPrinterData(hPrinter, (LPWSTR) pValueName, Type, pData, cbData);
  1431. }
  1432. if (!pKeyName || !*pKeyName) {
  1433. rc = ERROR_INVALID_PARAMETER;
  1434. goto Done;
  1435. }
  1436. if (!_wcsicmp(szPrinterData, pKeyName)) {
  1437. return SplSetPrinterData(hPrinter, (LPWSTR) pValueName, Type, pData, cbData);
  1438. }
  1439. EnterSplSem();
  1440. pIniPrinter = pSpool->pIniPrinter;
  1441. pIniSpooler = pIniPrinter->pIniSpooler;
  1442. DBGMSG( DBG_EXEC, ("SetPrinterDataEx: %ws %ws %ws %d cbSize=cbData\n",
  1443. pIniPrinter->pName,
  1444. pKeyName,
  1445. pValueName,
  1446. Type,
  1447. cbData ));
  1448. SPLASSERT(pIniPrinter &&
  1449. pIniPrinter->signature == IP_SIGNATURE);
  1450. if ( !AccessGranted( SPOOLER_OBJECT_PRINTER,
  1451. PRINTER_ACCESS_ADMINISTER,
  1452. pSpool ) ) {
  1453. rc = ERROR_ACCESS_DENIED;
  1454. goto DoneFromSplSem;
  1455. }
  1456. hToken = RevertToPrinterSelf();
  1457. //
  1458. // If this is a DS Key then parse out OID, if any, and write to Registry
  1459. // Also check that data type is correct
  1460. //
  1461. if (!wcscmp(pKeyName, SPLDS_SPOOLER_KEY)){
  1462. DsUpdate = DS_KEY_SPOOLER;
  1463. } else if (!wcscmp(pKeyName, SPLDS_DRIVER_KEY)){
  1464. DsUpdate = DS_KEY_DRIVER;
  1465. } else if (!wcscmp(pKeyName, SPLDS_USER_KEY)){
  1466. DsUpdate = DS_KEY_USER;
  1467. }
  1468. if (DsUpdate) {
  1469. if (Type != REG_SZ && Type != REG_MULTI_SZ && Type != REG_DWORD && !(Type == REG_BINARY && cbData == 1)) {
  1470. rc = ERROR_INVALID_PARAMETER;
  1471. goto DoneFromSplSem;
  1472. }
  1473. }
  1474. //
  1475. // Open or Create the key
  1476. // Create the hPrinterKey if it doesn't exist
  1477. //
  1478. rc = OpenPrinterKey(pIniPrinter,
  1479. KEY_READ | KEY_WRITE,
  1480. &hKey,
  1481. pKeyName,
  1482. FALSE);
  1483. if (rc != ERROR_SUCCESS)
  1484. goto DoneFromSplSem;
  1485. //
  1486. // Set the value
  1487. //
  1488. rc = SplRegSetValue(hKey,
  1489. pValueName,
  1490. Type,
  1491. pData,
  1492. cbData,
  1493. pIniPrinter->pIniSpooler );
  1494. if (rc != ERROR_SUCCESS)
  1495. goto DoneFromSplSem;
  1496. //
  1497. // Set Data succeeded. If the color profiles assocaiated with the
  1498. // print queue were updated we send a notification. TS listens for it
  1499. // and saves print queues settings. Updating color profiles implies
  1500. // touching 4 regitry keys. We want to send the notify only after the
  1501. // last key is updated.
  1502. //
  1503. if (!_wcsicmp(pKeyName, L"CopyFiles\\ICM") &&
  1504. !_wcsicmp(pValueName, L"Module"))
  1505. {
  1506. UpdatePrinterIni(pIniPrinter, CHANGEID_ONLY);
  1507. SetPrinterChange(pIniPrinter,
  1508. NULL,
  1509. NULL,
  1510. PRINTER_CHANGE_SET_PRINTER_DRIVER,
  1511. pSpool->pIniSpooler );
  1512. }
  1513. if (hToken) {
  1514. ImpersonatePrinterClient(hToken);
  1515. hToken = NULL;
  1516. }
  1517. if (ghDsUpdateThread && gdwDsUpdateThreadId == GetCurrentThreadId()) {
  1518. // We are in the background thread
  1519. pIniPrinter->DsKeyUpdate |= DsUpdate;
  1520. } else {
  1521. pIniPrinter->DsKeyUpdateForeground |= DsUpdate;
  1522. }
  1523. UpdatePrinterIni(pIniPrinter, UPDATE_DS_ONLY);
  1524. DoneFromSplSem:
  1525. if (hToken) {
  1526. ImpersonatePrinterClient(hToken);
  1527. }
  1528. LeaveSplSem();
  1529. if ( rc == ERROR_SUCCESS &&
  1530. !wcsncmp(pKeyName, L"CopyFiles\\", wcslen(L"CopyFiles\\")) ) {
  1531. (VOID)SplCopyFileEvent(pSpool,
  1532. (LPWSTR)pKeyName,
  1533. COPYFILE_EVENT_SET_PRINTER_DATAEX);
  1534. }
  1535. Done:
  1536. DBGMSG( DBG_EXEC, ("SetPrinterDataEx: return %d\n", rc));
  1537. if (hKey) {
  1538. SplRegCloseKey(hKey, pIniPrinter->pIniSpooler);
  1539. }
  1540. return rc;
  1541. }
  1542. //
  1543. // SetPrinterDataServer - also called during initialization
  1544. //
  1545. DWORD
  1546. SetPrinterDataServer(
  1547. PINISPOOLER pIniSpooler,
  1548. LPWSTR pValueName,
  1549. DWORD Type,
  1550. LPBYTE pData,
  1551. DWORD cbData
  1552. )
  1553. {
  1554. LPWSTR pKeyName;
  1555. DWORD rc;
  1556. HANDLE hToken = NULL;
  1557. PINIPRINTER pIniPrinter;
  1558. PINIJOB pIniJob;
  1559. PSERVER_DATA pRegistry; // points to table of Print Server registry entries
  1560. PNON_REGSET_FCN pNonRegSetFcn;
  1561. HKEY hKey;
  1562. PINISPOOLER pIniSpoolerOut;
  1563. //
  1564. // Server Handle
  1565. //
  1566. if (!pValueName) {
  1567. rc = ERROR_INVALID_PARAMETER;
  1568. } else {
  1569. for (pRegistry = gpServerRegistry ; pRegistry->pValue ; ++pRegistry) {
  1570. if (!_wcsicmp(pRegistry->pValue, pValueName)) {
  1571. if ((rc = GetServerKeyHandle(pIniSpooler,
  1572. pRegistry->eKey,
  1573. &hKey,
  1574. &pIniSpoolerOut)) == ERROR_SUCCESS) {
  1575. hToken = RevertToPrinterSelf();
  1576. if (pRegistry->pSet) {
  1577. rc = (*pRegistry->pSet)(pValueName, Type, pData, cbData, hKey, pIniSpoolerOut);
  1578. }
  1579. else {
  1580. rc = ERROR_INVALID_PARAMETER;
  1581. }
  1582. CloseServerKeyHandle( pRegistry->eKey,
  1583. hKey,
  1584. pIniSpoolerOut );
  1585. if (hToken)
  1586. ImpersonatePrinterClient(hToken);
  1587. }
  1588. break;
  1589. }
  1590. }
  1591. if (!pRegistry->pValue) {
  1592. for (pNonRegSetFcn = gpNonRegSetFcn ; pNonRegSetFcn->pValue ; ++pNonRegSetFcn) {
  1593. if (!_wcsicmp(pNonRegSetFcn->pValue, pValueName)) {
  1594. rc = (*pNonRegSetFcn->pSet)(pIniSpooler, Type, pData, cbData);
  1595. goto FinishNonReg;
  1596. }
  1597. }
  1598. FinishNonReg:
  1599. if (!pNonRegSetFcn->pValue) {
  1600. rc = ERROR_INVALID_PARAMETER;
  1601. }
  1602. }
  1603. }
  1604. return rc;
  1605. }
  1606. DWORD
  1607. SetPrinterDataPrinter(
  1608. HANDLE hPrinter,
  1609. HKEY hParentKey,
  1610. HKEY hKey,
  1611. LPWSTR pValueName,
  1612. DWORD Type,
  1613. LPBYTE pData,
  1614. DWORD cbData,
  1615. DWORD dwSet // SET_PRINTER_DATA, DELETE_PRINTER_DATA, or DELETE_PRINTER_KEY
  1616. )
  1617. {
  1618. PSPOOL pSpool = (PSPOOL)hPrinter;
  1619. LPWSTR pKeyName;
  1620. DWORD rc = ERROR_INVALID_HANDLE;
  1621. HANDLE hToken = NULL;
  1622. PINIPRINTER pIniPrinter;
  1623. PINIJOB pIniJob;
  1624. SplInSem();
  1625. if (!ValidateSpoolHandle(pSpool, PRINTER_HANDLE_SERVER )){
  1626. goto Done;
  1627. }
  1628. pIniPrinter = pSpool->pIniPrinter;
  1629. SPLASSERT(pIniPrinter &&
  1630. pIniPrinter->signature == IP_SIGNATURE && hKey);
  1631. if ( !AccessGranted( SPOOLER_OBJECT_PRINTER,
  1632. PRINTER_ACCESS_ADMINISTER,
  1633. pSpool ) ) {
  1634. rc = ERROR_ACCESS_DENIED;
  1635. goto Done;
  1636. }
  1637. hToken = RevertToPrinterSelf();
  1638. if (dwSet == SET_PRINTER_DATA) {
  1639. rc = SplRegSetValue(hKey,
  1640. pValueName,
  1641. Type,
  1642. pData,
  1643. cbData,
  1644. pIniPrinter->pIniSpooler );
  1645. } else if (dwSet == DELETE_PRINTER_DATA) {
  1646. rc = SplRegDeleteValue(hKey, pValueName, pIniPrinter->pIniSpooler );
  1647. } else if (dwSet == DELETE_PRINTER_KEY) {
  1648. rc = SplDeleteThisKey(hParentKey,
  1649. hKey,
  1650. pValueName,
  1651. FALSE,
  1652. pIniPrinter->pIniSpooler);
  1653. }
  1654. if (hToken)
  1655. ImpersonatePrinterClient(hToken);
  1656. if ( rc == ERROR_SUCCESS ) {
  1657. UpdatePrinterIni(pIniPrinter, CHANGEID_ONLY);
  1658. SetPrinterChange(pIniPrinter,
  1659. NULL,
  1660. NULL,
  1661. PRINTER_CHANGE_SET_PRINTER_DRIVER,
  1662. pSpool->pIniSpooler );
  1663. }
  1664. //
  1665. // Now if there are any Jobs waiting for these changes because of
  1666. // DevQueryPrint fix them as well
  1667. //
  1668. pIniJob = pIniPrinter->pIniFirstJob;
  1669. while (pIniJob) {
  1670. if (pIniJob->Status & JOB_BLOCKED_DEVQ) {
  1671. InterlockedAnd((LONG*)&(pIniJob->Status), ~JOB_BLOCKED_DEVQ);
  1672. FreeSplStr(pIniJob->pStatus);
  1673. pIniJob->pStatus = NULL;
  1674. SetPrinterChange(pIniJob->pIniPrinter,
  1675. pIniJob,
  1676. NVJobStatusAndString,
  1677. PRINTER_CHANGE_SET_JOB,
  1678. pIniJob->pIniPrinter->pIniSpooler );
  1679. }
  1680. pIniJob = pIniJob->pIniNextJob;
  1681. }
  1682. CHECK_SCHEDULER();
  1683. Done:
  1684. return rc;
  1685. }
  1686. DWORD
  1687. GetServerKeyHandle(
  1688. PINISPOOLER pIniSpooler,
  1689. REG_PRINT_KEY eKey,
  1690. HKEY *phKey,
  1691. PINISPOOLER* ppIniSpoolerOut
  1692. )
  1693. {
  1694. DWORD rc = ERROR_SUCCESS;
  1695. HANDLE hToken;
  1696. *ppIniSpoolerOut = NULL;
  1697. hToken = RevertToPrinterSelf();
  1698. switch (eKey) {
  1699. case REG_PRINT:
  1700. *phKey = pIniSpooler->hckRoot;
  1701. *ppIniSpoolerOut = pIniSpooler;
  1702. break;
  1703. case REG_PRINTERS:
  1704. *phKey = pIniSpooler->hckPrinters;
  1705. *ppIniSpoolerOut = pIniSpooler;
  1706. break;
  1707. case REG_PROVIDERS:
  1708. rc = SplRegCreateKey( pIniSpooler->hckRoot,
  1709. pIniSpooler->pszRegistryProviders,
  1710. 0,
  1711. KEY_READ | KEY_WRITE,
  1712. NULL,
  1713. phKey,
  1714. NULL,
  1715. pIniSpooler);
  1716. *ppIniSpoolerOut = pIniSpooler;
  1717. break;
  1718. default:
  1719. rc = ERROR_INVALID_PARAMETER;
  1720. break;
  1721. }
  1722. if (hToken && !ImpersonatePrinterClient(hToken))
  1723. {
  1724. rc = GetLastError();
  1725. CloseServerKeyHandle( eKey,
  1726. *phKey,
  1727. pIniSpooler );
  1728. *phKey = NULL;
  1729. *ppIniSpoolerOut = NULL;
  1730. }
  1731. return rc;
  1732. }
  1733. DWORD
  1734. CloseServerKeyHandle(
  1735. REG_PRINT_KEY eKey,
  1736. HKEY hKey,
  1737. PINISPOOLER pIniSpooler
  1738. )
  1739. {
  1740. DWORD rc = ERROR_SUCCESS;
  1741. HANDLE hToken = NULL;
  1742. hToken = RevertToPrinterSelf();
  1743. switch (eKey) {
  1744. case REG_PRINT:
  1745. break;
  1746. case REG_PRINTERS:
  1747. break;
  1748. case REG_PROVIDERS:
  1749. SplRegCloseKey( hKey, pIniSpooler );
  1750. break;
  1751. default:
  1752. rc = ERROR_INVALID_PARAMETER;
  1753. break;
  1754. }
  1755. if (hToken && !ImpersonatePrinterClient(hToken))
  1756. {
  1757. rc = GetLastError();
  1758. }
  1759. return rc;
  1760. }
  1761. DWORD
  1762. RegSetDefaultSpoolDirectory(
  1763. LPWSTR pValueName,
  1764. DWORD dwType,
  1765. LPBYTE pData,
  1766. DWORD cbData,
  1767. HKEY hKey,
  1768. PINISPOOLER pIniSpooler
  1769. )
  1770. {
  1771. DWORD rc = ERROR_SUCCESS;
  1772. LPWSTR pszNewSpoolDir = NULL;
  1773. SECURITY_ATTRIBUTES SecurityAttributes;
  1774. if ( pIniSpooler == NULL )
  1775. {
  1776. //
  1777. // This check is probably not needed.
  1778. // Old code was checking for NULL so instead of just removing it I
  1779. // changed it to an assert and fail gracefully w/o crash
  1780. //
  1781. rc = ERROR_INVALID_PARAMETER;
  1782. SPLASSERT(pIniSpooler != NULL);
  1783. }
  1784. else if (!pData || wcslen((LPWSTR)pData) > MAX_PATH - 12)
  1785. {
  1786. rc = ERROR_INVALID_PARAMETER;
  1787. }
  1788. else if ( !(pszNewSpoolDir = AllocSplStr((LPWSTR) pData)) )
  1789. {
  1790. rc = ERROR_OUTOFMEMORY;
  1791. }
  1792. if ( rc == ERROR_SUCCESS )
  1793. {
  1794. //
  1795. // Create the directory with the proper security, or fail trying
  1796. //
  1797. SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
  1798. SecurityAttributes.lpSecurityDescriptor = CreateEverybodySecurityDescriptor();
  1799. SecurityAttributes.bInheritHandle = FALSE;
  1800. if ( !CreateDirectory(pszNewSpoolDir, &SecurityAttributes) )
  1801. {
  1802. rc = GetLastError();
  1803. //
  1804. // If the directory already exists it is not a failure
  1805. //
  1806. if ( rc == ERROR_ALREADY_EXISTS )
  1807. {
  1808. rc = ERROR_SUCCESS;
  1809. }
  1810. else if ( rc == ERROR_SUCCESS )
  1811. {
  1812. //
  1813. // Don't rely on last error being set
  1814. //
  1815. rc = ERROR_OUTOFMEMORY;
  1816. }
  1817. }
  1818. LocalFree(SecurityAttributes.lpSecurityDescriptor);
  1819. }
  1820. if ( rc == ERROR_SUCCESS )
  1821. {
  1822. EnterSplSem();
  1823. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1824. if ( rc == ERROR_SUCCESS ) {
  1825. FreeSplStr(pIniSpooler->pDefaultSpoolDir);
  1826. pIniSpooler->pDefaultSpoolDir = pszNewSpoolDir;
  1827. pszNewSpoolDir = NULL;
  1828. if ( pIniSpooler->hFilePool != INVALID_HANDLE_VALUE )
  1829. {
  1830. (VOID) ChangeFilePoolBasePath(pIniSpooler->hFilePool,
  1831. pIniSpooler->pDefaultSpoolDir);
  1832. }
  1833. }
  1834. LeaveSplSem();
  1835. }
  1836. FreeSplStr(pszNewSpoolDir);
  1837. return rc;
  1838. }
  1839. DWORD
  1840. RegSetPortThreadPriority(
  1841. LPWSTR pValueName,
  1842. DWORD dwType,
  1843. LPBYTE pData,
  1844. DWORD cbData,
  1845. HKEY hKey,
  1846. PINISPOOLER pIniSpooler
  1847. )
  1848. {
  1849. BOOL rc;
  1850. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1851. if ((rc == ERROR_SUCCESS) &&
  1852. (cbData >= sizeof(DWORD))) {
  1853. dwPortThreadPriority = *(LPDWORD)pData;
  1854. }
  1855. return rc;
  1856. }
  1857. DWORD
  1858. RegSetSchedulerThreadPriority(
  1859. LPWSTR pValueName,
  1860. DWORD dwType,
  1861. LPBYTE pData,
  1862. DWORD cbData,
  1863. HKEY hKey,
  1864. PINISPOOLER pIniSpooler
  1865. )
  1866. {
  1867. BOOL rc;
  1868. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1869. if ((rc == ERROR_SUCCESS) &&
  1870. (cbData >= sizeof(DWORD))) {
  1871. dwSchedulerThreadPriority = *(LPDWORD)pData;
  1872. }
  1873. return rc;
  1874. }
  1875. DWORD
  1876. RegSetBeepEnabled(
  1877. LPWSTR pValueName,
  1878. DWORD dwType,
  1879. LPBYTE pData,
  1880. DWORD cbData,
  1881. HKEY hKey,
  1882. PINISPOOLER pIniSpooler
  1883. )
  1884. {
  1885. BOOL rc;
  1886. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1887. if ((rc == ERROR_SUCCESS) &&
  1888. (cbData >= sizeof(DWORD)) &&
  1889. pIniSpooler) {
  1890. pIniSpooler->dwBeepEnabled = *(LPDWORD)pData;
  1891. // Make it 1 or 0
  1892. pIniSpooler->dwBeepEnabled = !!pIniSpooler->dwBeepEnabled;
  1893. }
  1894. return rc;
  1895. }
  1896. DWORD
  1897. RegSetRetryPopup(
  1898. LPWSTR pValueName,
  1899. DWORD dwType,
  1900. LPBYTE pData,
  1901. DWORD cbData,
  1902. HKEY hKey,
  1903. PINISPOOLER pIniSpooler
  1904. )
  1905. {
  1906. BOOL rc;
  1907. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1908. if ((rc == ERROR_SUCCESS) &&
  1909. (cbData >= sizeof(DWORD)) &&
  1910. pIniSpooler) {
  1911. pIniSpooler->bEnableRetryPopups = *(LPDWORD) pData;
  1912. // Make it 1 or 0
  1913. pIniSpooler->bEnableRetryPopups = !!pIniSpooler->bEnableRetryPopups;
  1914. }
  1915. return rc;
  1916. }
  1917. DWORD
  1918. RegSetNetPopup(
  1919. LPWSTR pValueName,
  1920. DWORD dwType,
  1921. LPBYTE pData,
  1922. DWORD cbData,
  1923. HKEY hKey,
  1924. PINISPOOLER pIniSpooler
  1925. )
  1926. {
  1927. BOOL rc;
  1928. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1929. if ((rc == ERROR_SUCCESS) &&
  1930. (cbData >= sizeof(DWORD)) &&
  1931. pIniSpooler) {
  1932. pIniSpooler->bEnableNetPopups = *(LPDWORD) pData;
  1933. //
  1934. // Make it 1 or 0
  1935. //
  1936. pIniSpooler->bEnableNetPopups = !!pIniSpooler->bEnableNetPopups;
  1937. }
  1938. return rc;
  1939. }
  1940. DWORD
  1941. RegSetEventLog(
  1942. LPWSTR pValueName,
  1943. DWORD dwType,
  1944. LPBYTE pData,
  1945. DWORD cbData,
  1946. HKEY hKey,
  1947. PINISPOOLER pIniSpooler
  1948. )
  1949. {
  1950. BOOL rc;
  1951. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1952. if ((rc == ERROR_SUCCESS) &&
  1953. (cbData >= sizeof(DWORD)) &&
  1954. pIniSpooler) {
  1955. pIniSpooler->dwEventLogging = *(LPDWORD) pData;
  1956. }
  1957. return rc;
  1958. }
  1959. DWORD
  1960. RegSetNetPopupToComputer(
  1961. LPWSTR pValueName,
  1962. DWORD dwType,
  1963. LPBYTE pData,
  1964. DWORD cbData,
  1965. HKEY hKey,
  1966. PINISPOOLER pIniSpooler
  1967. )
  1968. {
  1969. BOOL rc;
  1970. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1971. if ((rc == ERROR_SUCCESS) &&
  1972. (cbData >= sizeof(DWORD)) &&
  1973. pIniSpooler) {
  1974. pIniSpooler->bEnableNetPopupToComputer = *(LPDWORD) pData;
  1975. // Make it 1 or 0
  1976. pIniSpooler->bEnableNetPopupToComputer = !!pIniSpooler->bEnableNetPopupToComputer;
  1977. }
  1978. return rc;
  1979. }
  1980. DWORD
  1981. RegSetRestartJobOnPoolError(
  1982. LPWSTR pValueName,
  1983. DWORD dwType,
  1984. LPBYTE pData,
  1985. DWORD cbData,
  1986. HKEY hKey,
  1987. PINISPOOLER pIniSpooler
  1988. )
  1989. {
  1990. BOOL rc;
  1991. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  1992. if ((rc == ERROR_SUCCESS) &&
  1993. (cbData >= sizeof(DWORD)) &&
  1994. pIniSpooler) {
  1995. pIniSpooler->dwRestartJobOnPoolTimeout = *(LPDWORD) pData;
  1996. }
  1997. return rc;
  1998. }
  1999. DWORD
  2000. RegSetRestartJobOnPoolEnabled(
  2001. LPWSTR pValueName,
  2002. DWORD dwType,
  2003. LPBYTE pData,
  2004. DWORD cbData,
  2005. HKEY hKey,
  2006. PINISPOOLER pIniSpooler
  2007. )
  2008. {
  2009. BOOL rc;
  2010. rc = SplRegSetValue(hKey, pValueName, dwType, pData, cbData, pIniSpooler);
  2011. if ((rc == ERROR_SUCCESS) &&
  2012. (cbData >= sizeof(DWORD)) &&
  2013. pIniSpooler) {
  2014. pIniSpooler->bRestartJobOnPoolEnabled = *(LPDWORD) pData;
  2015. // Make it 1 or 0
  2016. pIniSpooler->bRestartJobOnPoolEnabled = !!pIniSpooler->bRestartJobOnPoolEnabled;
  2017. }
  2018. return rc;
  2019. }
  2020. DWORD
  2021. RegSetNoRemoteDriver(
  2022. LPWSTR pValueName,
  2023. DWORD dwType,
  2024. LPBYTE pData,
  2025. DWORD cbData,
  2026. HKEY hKey,
  2027. PINISPOOLER pIniSpooler
  2028. )
  2029. {
  2030. return ERROR_NOT_SUPPORTED;
  2031. }
  2032. DWORD
  2033. PrinterNonRegGetDefaultSpoolDirectory(
  2034. PSPOOL pSpool,
  2035. LPDWORD pType,
  2036. LPBYTE pData,
  2037. DWORD nSize,
  2038. LPDWORD pcbNeeded
  2039. )
  2040. {
  2041. WCHAR szDefaultSpoolDirectory[MAX_PATH];
  2042. DWORD cch;
  2043. cch = GetPrinterDirectory( pSpool->pIniPrinter,
  2044. FALSE,
  2045. szDefaultSpoolDirectory,
  2046. COUNTOF(szDefaultSpoolDirectory),
  2047. pSpool->pIniSpooler );
  2048. if(!cch) {
  2049. return GetLastError();
  2050. }
  2051. *pcbNeeded = ( cch + 1 ) * sizeof( szDefaultSpoolDirectory[0] );
  2052. *pType = REG_SZ;
  2053. if( nSize < *pcbNeeded ){
  2054. return ERROR_MORE_DATA;
  2055. }
  2056. StringCbCopy((LPWSTR)pData, nSize, szDefaultSpoolDirectory );
  2057. return ERROR_SUCCESS;
  2058. }
  2059. DWORD
  2060. PrinterNonRegGetChangeId(
  2061. PSPOOL pSpool,
  2062. LPDWORD pType,
  2063. LPBYTE pData,
  2064. DWORD nSize,
  2065. LPDWORD pcbNeeded
  2066. )
  2067. {
  2068. LPDWORD pdwChangeID = (LPDWORD)pData;
  2069. DWORD dwRetval = ERROR_INVALID_PARAMETER;
  2070. //
  2071. // We need a valid handle, piniPrinter
  2072. //
  2073. if (pSpool && pSpool->pIniPrinter)
  2074. {
  2075. if (pcbNeeded)
  2076. {
  2077. *pcbNeeded = sizeof(pSpool->pIniPrinter->cChangeID);
  2078. }
  2079. //
  2080. // The type is optional.
  2081. //
  2082. if (pType)
  2083. {
  2084. *pType = REG_DWORD;
  2085. }
  2086. //
  2087. // Is the provided buffer large enough.
  2088. //
  2089. if (nSize < sizeof(pSpool->pIniPrinter->cChangeID))
  2090. {
  2091. dwRetval = ERROR_MORE_DATA;
  2092. }
  2093. else
  2094. {
  2095. //
  2096. // Is the provided buffer valid.
  2097. //
  2098. if (pdwChangeID)
  2099. {
  2100. //
  2101. // Get the printer change id. We really would like
  2102. // more granularity on this. Just knowing if something
  2103. // changed about this printer is very general.
  2104. //
  2105. *pdwChangeID = pSpool->pIniPrinter->cChangeID;
  2106. dwRetval = ERROR_SUCCESS;
  2107. }
  2108. }
  2109. }
  2110. return dwRetval;
  2111. }
  2112. DWORD
  2113. NonRegGetDNSMachineName(
  2114. PINISPOOLER pIniSpooler,
  2115. LPDWORD pType,
  2116. LPBYTE pData,
  2117. DWORD nSize,
  2118. LPDWORD pcbNeeded
  2119. )
  2120. {
  2121. DWORD cChars;
  2122. if(!pIniSpooler || !pIniSpooler->pszFullMachineName) {
  2123. return ERROR_INVALID_PARAMETER;
  2124. }
  2125. cChars = wcslen(pIniSpooler->pszFullMachineName);
  2126. *pcbNeeded = ( cChars + 1 ) * sizeof( WCHAR );
  2127. *pType = REG_SZ;
  2128. if( nSize < *pcbNeeded ){
  2129. return ERROR_MORE_DATA;
  2130. }
  2131. StringCbCopy((LPWSTR)pData, nSize, _wcslwr(pIniSpooler->pszFullMachineName));
  2132. return ERROR_SUCCESS;
  2133. }
  2134. DWORD
  2135. NonRegDsPresent(
  2136. PINISPOOLER pIniSpooler,
  2137. LPDWORD pType,
  2138. LPBYTE pData,
  2139. DWORD nSize,
  2140. LPDWORD pcbNeeded
  2141. )
  2142. {
  2143. HANDLE hToken = NULL;
  2144. DWORD rc = ERROR_SUCCESS;
  2145. *pcbNeeded = sizeof(DWORD);
  2146. *pType = REG_DWORD;
  2147. if (nSize < sizeof(DWORD))
  2148. rc = ERROR_MORE_DATA;
  2149. if (rc == ERROR_SUCCESS)
  2150. {
  2151. hToken = RevertToPrinterSelf();
  2152. *(PDWORD) pData = IsDsPresent();
  2153. if (hToken && !ImpersonatePrinterClient(hToken))
  2154. {
  2155. rc = GetLastError();
  2156. }
  2157. }
  2158. return rc;
  2159. }
  2160. BOOL
  2161. IsDsPresent(
  2162. )
  2163. {
  2164. DOMAIN_CONTROLLER_INFO *pDCI = NULL;
  2165. BOOL bDsPresent;
  2166. DWORD dwRet;
  2167. PDSROLE_PRIMARY_DOMAIN_INFO_BASIC pDsRole = NULL;
  2168. // Get Domain name
  2169. dwRet = DsRoleGetPrimaryDomainInformation(NULL, DsRolePrimaryDomainInfoBasic, (PBYTE *) &pDsRole);
  2170. bDsPresent = (dwRet == ERROR_SUCCESS &&
  2171. pDsRole->MachineRole != DsRole_RoleStandaloneServer &&
  2172. pDsRole->MachineRole != DsRole_RoleStandaloneWorkstation);
  2173. if (pDsRole) {
  2174. DsRoleFreeMemory((PVOID) pDsRole);
  2175. }
  2176. if (bDsPresent) {
  2177. if (DsGetDcName(NULL, NULL, NULL, NULL, DS_DIRECTORY_SERVICE_PREFERRED, &pDCI) == ERROR_SUCCESS)
  2178. bDsPresent = !!(pDCI->Flags & DS_DS_FLAG);
  2179. else
  2180. bDsPresent = FALSE;
  2181. if (pDCI)
  2182. NetApiBufferFree(pDCI);
  2183. }
  2184. return bDsPresent;
  2185. }
  2186. DWORD
  2187. NonRegDsPresentForUser(
  2188. PINISPOOLER pIniSpooler,
  2189. LPDWORD pType,
  2190. LPBYTE pData,
  2191. DWORD nSize,
  2192. LPDWORD pcbNeeded
  2193. )
  2194. {
  2195. WCHAR pUserName[MAX_PATH + 1];
  2196. PWSTR pszUserName = pUserName;
  2197. DWORD cchUserName = MAX_PATH + 1;
  2198. DWORD dwError = ERROR_SUCCESS;
  2199. PWSTR pszDomain;
  2200. DOMAIN_CONTROLLER_INFO *pDCI = NULL;
  2201. WCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  2202. *pcbNeeded = sizeof(DWORD);
  2203. *pType = REG_DWORD;
  2204. if (nSize < sizeof(DWORD))
  2205. return ERROR_MORE_DATA;
  2206. // GetUserNameEx returns "Domain\User" in pszUserName.
  2207. if (!GetUserNameEx(NameSamCompatible, pszUserName, &cchUserName)) {
  2208. if (cchUserName > MAX_PATH + 1) {
  2209. pszUserName = AllocSplMem(cchUserName);
  2210. if (!pszUserName || !GetUserNameEx(NameSamCompatible, pszUserName, &cchUserName)) {
  2211. dwError = GetLastError();
  2212. goto error;
  2213. }
  2214. } else {
  2215. dwError = GetLastError();
  2216. goto error;
  2217. }
  2218. }
  2219. // Chop off user name
  2220. pszDomain = wcschr(pszUserName, L'\\');
  2221. SPLASSERT(pszDomain);
  2222. if (pszDomain) { // pszDomain should never be NULL, but just in case...
  2223. *pszDomain = L'\0';
  2224. } else {
  2225. *(PDWORD) pData = 0;
  2226. goto error;
  2227. }
  2228. // If domain is same a machine name, then we're logged on locally
  2229. nSize = COUNTOF(szComputerName);
  2230. if (GetComputerName(szComputerName, &nSize) && !wcscmp(szComputerName, pszUserName)) {
  2231. *(PDWORD) pData = 0;
  2232. goto error;
  2233. }
  2234. pszDomain = pszUserName;
  2235. if (DsGetDcName(NULL, pszDomain, NULL, NULL, DS_DIRECTORY_SERVICE_PREFERRED, &pDCI) == ERROR_SUCCESS)
  2236. *(PDWORD) pData = !!(pDCI->Flags & DS_DS_FLAG);
  2237. error:
  2238. if (pDCI)
  2239. NetApiBufferFree(pDCI);
  2240. if (pszUserName != pUserName)
  2241. FreeSplMem(pszUserName);
  2242. return dwError;
  2243. }
  2244. DWORD
  2245. NonRegWebPrintingInstalled(
  2246. PINISPOOLER pIniSpooler,
  2247. LPDWORD pType,
  2248. LPBYTE pData,
  2249. DWORD nSize,
  2250. LPDWORD pcbNeeded
  2251. )
  2252. {
  2253. DWORD rc = ERROR_SUCCESS;
  2254. BOOL bWebPrintingInstalled;
  2255. *pcbNeeded = sizeof(DWORD);
  2256. *pType = REG_DWORD;
  2257. if (nSize < sizeof(DWORD))
  2258. rc = ERROR_MORE_DATA;
  2259. if (rc == ERROR_SUCCESS)
  2260. {
  2261. rc = CheckWebPrinting( &bWebPrintingInstalled);
  2262. if (rc == ERROR_SUCCESS)
  2263. *(PDWORD) pData = bWebPrintingInstalled;
  2264. }
  2265. return rc;
  2266. }
  2267. DWORD
  2268. NonRegWebShareManagement(
  2269. PINISPOOLER pIniSpooler,
  2270. DWORD dwType,
  2271. LPBYTE pData,
  2272. DWORD cbData
  2273. )
  2274. {
  2275. DWORD dwRet = ERROR_SUCCESS,
  2276. dwWebShareOn;
  2277. BOOL bSharePrinters;
  2278. //
  2279. // Make sure the DataType & Size is correct
  2280. //
  2281. if ((dwType != REG_DWORD) &&
  2282. (cbData != sizeof(DWORD)))
  2283. {
  2284. dwRet = ERROR_INVALID_PARAMETER;
  2285. }
  2286. dwWebShareOn = *((DWORD*)pData);
  2287. bSharePrinters = (dwWebShareOn != 0);
  2288. dwRet = WebShareManager( pIniSpooler, bSharePrinters );
  2289. return dwRet;
  2290. }