Source code of Windows XP (NT5)
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.

407 lines
10 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1998
  5. //
  6. // File: prtctx.cpp
  7. //
  8. // Contents: Implementation of CPrinterContext and NT Marta Printer Functions
  9. //
  10. // History: 3-31-1999 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <aclpch.hxx>
  14. #pragma hdrstop
  15. #include <prtctx.h>
  16. //+---------------------------------------------------------------------------
  17. //
  18. // Member: CPrinterContext::CPrinterContext, public
  19. //
  20. // Synopsis: Constructor
  21. //
  22. //----------------------------------------------------------------------------
  23. CPrinterContext::CPrinterContext ()
  24. {
  25. m_cRefs = 1;
  26. m_hPrinter = NULL;
  27. m_fNameInitialized = FALSE;
  28. }
  29. //+---------------------------------------------------------------------------
  30. //
  31. // Member: CPrinterContext::~CPrinterContext, public
  32. //
  33. // Synopsis: Destructor
  34. //
  35. //----------------------------------------------------------------------------
  36. CPrinterContext::~CPrinterContext ()
  37. {
  38. if ( ( m_hPrinter != NULL ) && ( m_fNameInitialized == TRUE ) )
  39. {
  40. ClosePrinter( m_hPrinter );
  41. }
  42. assert( m_cRefs == 0 );
  43. }
  44. //+---------------------------------------------------------------------------
  45. //
  46. // Member: CPrinterContext::InitializeByName, public
  47. //
  48. // Synopsis: initialize the context given the name of the printer
  49. //
  50. //----------------------------------------------------------------------------
  51. DWORD
  52. CPrinterContext::InitializeByName (LPCWSTR pObjectName, ACCESS_MASK AccessMask)
  53. {
  54. PRINTER_DEFAULTSW PrinterDefaults;
  55. PrinterDefaults.pDatatype = NULL;
  56. PrinterDefaults.pDevMode = NULL;
  57. PrinterDefaults.DesiredAccess = AccessMask;
  58. if ( OpenPrinterW(
  59. (LPWSTR)pObjectName,
  60. &m_hPrinter,
  61. &PrinterDefaults
  62. ) == FALSE )
  63. {
  64. return( GetLastError() );
  65. }
  66. m_fNameInitialized = TRUE;
  67. return( ERROR_SUCCESS );
  68. }
  69. //+---------------------------------------------------------------------------
  70. //
  71. // Member: CPrinterContext::InitializeByHandle, public
  72. //
  73. // Synopsis: initialize the context given a printer handle
  74. //
  75. //----------------------------------------------------------------------------
  76. DWORD
  77. CPrinterContext::InitializeByHandle (HANDLE Handle)
  78. {
  79. m_hPrinter = Handle;
  80. assert( m_fNameInitialized == FALSE );
  81. return( ERROR_SUCCESS );
  82. }
  83. //+---------------------------------------------------------------------------
  84. //
  85. // Member: CPrinterContext::AddRef, public
  86. //
  87. // Synopsis: add a reference to the context
  88. //
  89. //----------------------------------------------------------------------------
  90. DWORD
  91. CPrinterContext::AddRef ()
  92. {
  93. m_cRefs += 1;
  94. return( m_cRefs );
  95. }
  96. //+---------------------------------------------------------------------------
  97. //
  98. // Member: CPrinterContext::Release, public
  99. //
  100. // Synopsis: release a reference to the context
  101. //
  102. //----------------------------------------------------------------------------
  103. DWORD
  104. CPrinterContext::Release ()
  105. {
  106. m_cRefs -= 1;
  107. if ( m_cRefs == 0 )
  108. {
  109. delete this;
  110. return( 0 );
  111. }
  112. return( m_cRefs );
  113. }
  114. //+---------------------------------------------------------------------------
  115. //
  116. // Member: CPrinterContext::GetPrinterProperties, public
  117. //
  118. // Synopsis: get properties about the context
  119. //
  120. //----------------------------------------------------------------------------
  121. DWORD
  122. CPrinterContext::GetPrinterProperties (
  123. PMARTA_OBJECT_PROPERTIES pObjectProperties
  124. )
  125. {
  126. if ( pObjectProperties->cbSize < sizeof( MARTA_OBJECT_PROPERTIES ) )
  127. {
  128. return( ERROR_INVALID_PARAMETER );
  129. }
  130. assert( pObjectProperties->dwFlags == 0 );
  131. return( ERROR_SUCCESS );
  132. }
  133. //+---------------------------------------------------------------------------
  134. //
  135. // Member: CPrinterContext::GetPrinterRights, public
  136. //
  137. // Synopsis: get the Printer security descriptor
  138. //
  139. //----------------------------------------------------------------------------
  140. DWORD
  141. CPrinterContext::GetPrinterRights (
  142. SECURITY_INFORMATION SecurityInfo,
  143. PSECURITY_DESCRIPTOR* ppSecurityDescriptor
  144. )
  145. {
  146. PISECURITY_DESCRIPTOR pisd = NULL;
  147. PSECURITY_DESCRIPTOR psd = NULL;
  148. DWORD cb = 0;
  149. PPRINTER_INFO_3 pPrinterInfo = NULL;
  150. assert( m_hPrinter != NULL );
  151. if ( ( GetPrinterW(
  152. m_hPrinter,
  153. 3,
  154. (LPBYTE)pPrinterInfo,
  155. cb,
  156. &cb
  157. ) == FALSE ) &&
  158. ( cb > 0 ) )
  159. {
  160. pPrinterInfo = (PPRINTER_INFO_3)new BYTE [ cb ];
  161. if ( pPrinterInfo != NULL )
  162. {
  163. if ( GetPrinterW(
  164. m_hPrinter,
  165. 3,
  166. (LPBYTE)pPrinterInfo,
  167. cb,
  168. &cb
  169. ) == FALSE )
  170. {
  171. delete pPrinterInfo;
  172. return( GetLastError() );
  173. }
  174. }
  175. else
  176. {
  177. return( E_OUTOFMEMORY );
  178. }
  179. }
  180. else
  181. {
  182. return( GetLastError() );
  183. }
  184. pisd = (PISECURITY_DESCRIPTOR)pPrinterInfo->pSecurityDescriptor;
  185. if ( pisd->Control & SE_SELF_RELATIVE )
  186. {
  187. cb = GetSecurityDescriptorLength( pPrinterInfo->pSecurityDescriptor );
  188. psd = (PSECURITY_DESCRIPTOR)LocalAlloc( LPTR, cb );
  189. if ( psd == NULL )
  190. {
  191. delete pPrinterInfo;
  192. return( ERROR_OUTOFMEMORY );
  193. }
  194. memcpy( psd, pPrinterInfo->pSecurityDescriptor, cb );
  195. }
  196. else
  197. {
  198. if ( MakeSelfRelativeSD(
  199. pPrinterInfo->pSecurityDescriptor,
  200. NULL,
  201. &cb
  202. ) == FALSE )
  203. {
  204. if ( cb > 0 )
  205. {
  206. psd = (PSECURITY_DESCRIPTOR)LocalAlloc( LPTR, cb );
  207. if ( psd != NULL )
  208. {
  209. if ( MakeSelfRelativeSD(
  210. pPrinterInfo->pSecurityDescriptor,
  211. psd,
  212. &cb
  213. ) == FALSE )
  214. {
  215. LocalFree( psd );
  216. delete pPrinterInfo;
  217. return( GetLastError() );
  218. }
  219. }
  220. }
  221. else
  222. {
  223. delete pPrinterInfo;
  224. return( GetLastError() );
  225. }
  226. }
  227. }
  228. delete pPrinterInfo;
  229. *ppSecurityDescriptor = psd;
  230. return( ERROR_SUCCESS );
  231. }
  232. //+---------------------------------------------------------------------------
  233. //
  234. // Member: CServiceContext::SetPrinterRights, public
  235. //
  236. // Synopsis: set the window security descriptor
  237. //
  238. //----------------------------------------------------------------------------
  239. DWORD
  240. CPrinterContext::SetPrinterRights (
  241. SECURITY_INFORMATION SecurityInfo,
  242. PSECURITY_DESCRIPTOR pSecurityDescriptor
  243. )
  244. {
  245. PRINTER_INFO_3 PrinterInfo;
  246. assert( m_hPrinter != NULL );
  247. PrinterInfo.pSecurityDescriptor = pSecurityDescriptor;
  248. if ( SetPrinterW( m_hPrinter, 3, (LPBYTE)&PrinterInfo, 0 ) == FALSE )
  249. {
  250. return( GetLastError() );
  251. }
  252. return( ERROR_SUCCESS );
  253. }
  254. //
  255. // Functions from printer.h which dispatch unto the CPrinterContext class
  256. //
  257. DWORD
  258. MartaAddRefPrinterContext(
  259. IN MARTA_CONTEXT Context
  260. )
  261. {
  262. return( ( (CPrinterContext *)Context )->AddRef() );
  263. }
  264. DWORD
  265. MartaClosePrinterContext(
  266. IN MARTA_CONTEXT Context
  267. )
  268. {
  269. return( ( (CPrinterContext *)Context )->Release() );
  270. }
  271. DWORD
  272. MartaGetPrinterProperties(
  273. IN MARTA_CONTEXT Context,
  274. IN OUT PMARTA_OBJECT_PROPERTIES pProperties
  275. )
  276. {
  277. return( ( (CPrinterContext *)Context )->GetPrinterProperties( pProperties ) );
  278. }
  279. DWORD
  280. MartaGetPrinterTypeProperties(
  281. IN OUT PMARTA_OBJECT_TYPE_PROPERTIES pProperties
  282. )
  283. {
  284. if ( pProperties->cbSize < sizeof( MARTA_OBJECT_TYPE_PROPERTIES ) )
  285. {
  286. return( ERROR_INVALID_PARAMETER );
  287. }
  288. assert( pProperties->dwFlags == 0 );
  289. return( ERROR_SUCCESS );
  290. }
  291. DWORD
  292. MartaGetPrinterRights(
  293. IN MARTA_CONTEXT Context,
  294. IN SECURITY_INFORMATION SecurityInfo,
  295. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  296. )
  297. {
  298. return( ( (CPrinterContext *)Context )->GetPrinterRights(
  299. SecurityInfo,
  300. ppSecurityDescriptor
  301. ) );
  302. }
  303. DWORD
  304. MartaOpenPrinterNamedObject(
  305. IN LPCWSTR pObjectName,
  306. IN ACCESS_MASK AccessMask,
  307. OUT PMARTA_CONTEXT pContext
  308. )
  309. {
  310. DWORD Result;
  311. CPrinterContext* pPrinterContext;
  312. pPrinterContext = new CPrinterContext;
  313. if ( pPrinterContext == NULL )
  314. {
  315. return( ERROR_OUTOFMEMORY );
  316. }
  317. Result = pPrinterContext->InitializeByName( pObjectName, AccessMask );
  318. if ( Result != ERROR_SUCCESS )
  319. {
  320. pPrinterContext->Release();
  321. return( Result );
  322. }
  323. *pContext = pPrinterContext;
  324. return( ERROR_SUCCESS );
  325. }
  326. DWORD
  327. MartaOpenPrinterHandleObject(
  328. IN HANDLE Handle,
  329. IN ACCESS_MASK AccessMask,
  330. OUT PMARTA_CONTEXT pContext
  331. )
  332. {
  333. DWORD Result;
  334. CPrinterContext* pPrinterContext;
  335. pPrinterContext = new CPrinterContext;
  336. if ( pPrinterContext == NULL )
  337. {
  338. return( ERROR_OUTOFMEMORY );
  339. }
  340. Result = pPrinterContext->InitializeByHandle( Handle );
  341. if ( Result != ERROR_SUCCESS )
  342. {
  343. pPrinterContext->Release();
  344. return( Result );
  345. }
  346. *pContext = pPrinterContext;
  347. return( ERROR_SUCCESS );
  348. }
  349. DWORD
  350. MartaSetPrinterRights(
  351. IN MARTA_CONTEXT Context,
  352. IN SECURITY_INFORMATION SecurityInfo,
  353. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  354. )
  355. {
  356. return( ( (CPrinterContext *)Context )->SetPrinterRights(
  357. SecurityInfo,
  358. pSecurityDescriptor
  359. ) );
  360. }