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.

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