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.

373 lines
9.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1998
  5. //
  6. // File: wndctx.cpp
  7. //
  8. // Contents: Implementation of CWindowContext and NT Marta Window Functions
  9. //
  10. // History: 3-31-1999 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <aclpch.hxx>
  14. #pragma hdrstop
  15. #include <wndctx.h>
  16. //+---------------------------------------------------------------------------
  17. //
  18. // Member: CWindowContext::CWindowContext, public
  19. //
  20. // Synopsis: Constructor
  21. //
  22. //----------------------------------------------------------------------------
  23. CWindowContext::CWindowContext ()
  24. {
  25. m_cRefs = 1;
  26. m_hWindowStation = NULL;
  27. m_fNameInitialized = FALSE;
  28. }
  29. //+---------------------------------------------------------------------------
  30. //
  31. // Member: CWindowContext::~CWindowContext, public
  32. //
  33. // Synopsis: Destructor
  34. //
  35. //----------------------------------------------------------------------------
  36. CWindowContext::~CWindowContext ()
  37. {
  38. if ( ( m_hWindowStation != NULL ) && ( m_fNameInitialized == TRUE ) )
  39. {
  40. CloseWindowStation( m_hWindowStation );
  41. }
  42. assert( m_cRefs == 0 );
  43. }
  44. //+---------------------------------------------------------------------------
  45. //
  46. // Member: CWindowContext::InitializeByName, public
  47. //
  48. // Synopsis: initialize the context given the name of the window station
  49. //
  50. //----------------------------------------------------------------------------
  51. DWORD
  52. CWindowContext::InitializeByName (LPCWSTR pObjectName, ACCESS_MASK AccessMask)
  53. {
  54. DWORD dwDesiredAccess = 0;
  55. if ( AccessMask & GENERIC_READ )
  56. {
  57. dwDesiredAccess |= WINSTA_READATTRIBUTES;
  58. }
  59. if ( AccessMask & GENERIC_WRITE )
  60. {
  61. dwDesiredAccess |= WINSTA_WRITEATTRIBUTES;
  62. }
  63. m_hWindowStation = OpenWindowStationW(
  64. pObjectName,
  65. FALSE,
  66. dwDesiredAccess
  67. );
  68. if ( m_hWindowStation == NULL )
  69. {
  70. return( GetLastError() );
  71. }
  72. m_fNameInitialized = TRUE;
  73. return( ERROR_SUCCESS );
  74. }
  75. //+---------------------------------------------------------------------------
  76. //
  77. // Member: CWindowContext::InitializeByHandle, public
  78. //
  79. // Synopsis: initialize the context given a window station handle
  80. //
  81. //----------------------------------------------------------------------------
  82. DWORD
  83. CWindowContext::InitializeByHandle (HANDLE Handle)
  84. {
  85. m_hWindowStation = (HWINSTA)Handle;
  86. assert( m_fNameInitialized == FALSE );
  87. return( ERROR_SUCCESS );
  88. }
  89. //+---------------------------------------------------------------------------
  90. //
  91. // Member: CWindowContext::AddRef, public
  92. //
  93. // Synopsis: add a reference to the context
  94. //
  95. //----------------------------------------------------------------------------
  96. DWORD
  97. CWindowContext::AddRef ()
  98. {
  99. m_cRefs += 1;
  100. return( m_cRefs );
  101. }
  102. //+---------------------------------------------------------------------------
  103. //
  104. // Member: CWindowContext::Release, public
  105. //
  106. // Synopsis: release a reference to the context
  107. //
  108. //----------------------------------------------------------------------------
  109. DWORD
  110. CWindowContext::Release ()
  111. {
  112. m_cRefs -= 1;
  113. if ( m_cRefs == 0 )
  114. {
  115. delete this;
  116. return( 0 );
  117. }
  118. return( m_cRefs );
  119. }
  120. //+---------------------------------------------------------------------------
  121. //
  122. // Member: CWindowContext::GetWindowProperties, public
  123. //
  124. // Synopsis: get properties about the context
  125. //
  126. //----------------------------------------------------------------------------
  127. DWORD
  128. CWindowContext::GetWindowProperties (
  129. PMARTA_OBJECT_PROPERTIES pObjectProperties
  130. )
  131. {
  132. if ( pObjectProperties->cbSize < sizeof( MARTA_OBJECT_PROPERTIES ) )
  133. {
  134. return( ERROR_INVALID_PARAMETER );
  135. }
  136. assert( pObjectProperties->dwFlags == 0 );
  137. return( ERROR_SUCCESS );
  138. }
  139. //+---------------------------------------------------------------------------
  140. //
  141. // Member: CWindowContext::GetWindowRights, public
  142. //
  143. // Synopsis: get the window security descriptor
  144. //
  145. //----------------------------------------------------------------------------
  146. DWORD
  147. CWindowContext::GetWindowRights (
  148. SECURITY_INFORMATION SecurityInfo,
  149. PSECURITY_DESCRIPTOR* ppSecurityDescriptor
  150. )
  151. {
  152. BOOL fResult;
  153. PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
  154. DWORD cb = 0;
  155. assert( m_hWindowStation != NULL );
  156. fResult = GetUserObjectSecurity(
  157. m_hWindowStation,
  158. &SecurityInfo,
  159. pSecurityDescriptor,
  160. 0,
  161. &cb
  162. );
  163. if ( ( fResult == FALSE ) && ( cb > 0 ) )
  164. {
  165. assert( ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) ||
  166. ( GetLastError() == STATUS_BUFFER_TOO_SMALL ) );
  167. pSecurityDescriptor = (PSECURITY_DESCRIPTOR)LocalAlloc( LPTR, cb );
  168. if ( pSecurityDescriptor == NULL )
  169. {
  170. return( ERROR_OUTOFMEMORY );
  171. }
  172. fResult = GetUserObjectSecurity(
  173. m_hWindowStation,
  174. &SecurityInfo,
  175. pSecurityDescriptor,
  176. cb,
  177. &cb
  178. );
  179. }
  180. else
  181. {
  182. assert( fResult == FALSE );
  183. return( GetLastError() );
  184. }
  185. if ( fResult == TRUE )
  186. {
  187. *ppSecurityDescriptor = pSecurityDescriptor;
  188. }
  189. else
  190. {
  191. return( GetLastError() );
  192. }
  193. return( ERROR_SUCCESS );
  194. }
  195. //+---------------------------------------------------------------------------
  196. //
  197. // Member: CWindowContext::SetWindowRights, public
  198. //
  199. // Synopsis: set the window security descriptor
  200. //
  201. //----------------------------------------------------------------------------
  202. DWORD
  203. CWindowContext::SetWindowRights (
  204. SECURITY_INFORMATION SecurityInfo,
  205. PSECURITY_DESCRIPTOR pSecurityDescriptor
  206. )
  207. {
  208. assert( m_hWindowStation != NULL );
  209. if ( SetUserObjectSecurity(
  210. m_hWindowStation,
  211. &SecurityInfo,
  212. pSecurityDescriptor
  213. ) == FALSE )
  214. {
  215. return( GetLastError() );
  216. }
  217. return( ERROR_SUCCESS );
  218. }
  219. //
  220. // Functions from window.h which dispatch unto the CWindowContext class
  221. //
  222. DWORD
  223. MartaAddRefWindowContext(
  224. IN MARTA_CONTEXT Context
  225. )
  226. {
  227. return( ( (CWindowContext *)Context )->AddRef() );
  228. }
  229. DWORD
  230. MartaCloseWindowContext(
  231. IN MARTA_CONTEXT Context
  232. )
  233. {
  234. return( ( (CWindowContext *)Context )->Release() );
  235. }
  236. DWORD
  237. MartaGetWindowProperties(
  238. IN MARTA_CONTEXT Context,
  239. IN OUT PMARTA_OBJECT_PROPERTIES pProperties
  240. )
  241. {
  242. return( ( (CWindowContext *)Context )->GetWindowProperties( pProperties ) );
  243. }
  244. DWORD
  245. MartaGetWindowTypeProperties(
  246. IN OUT PMARTA_OBJECT_TYPE_PROPERTIES pProperties
  247. )
  248. {
  249. if ( pProperties->cbSize < sizeof( MARTA_OBJECT_TYPE_PROPERTIES ) )
  250. {
  251. return( ERROR_INVALID_PARAMETER );
  252. }
  253. assert( pProperties->dwFlags == 0 );
  254. return( ERROR_SUCCESS );
  255. }
  256. DWORD
  257. MartaGetWindowRights(
  258. IN MARTA_CONTEXT Context,
  259. IN SECURITY_INFORMATION SecurityInfo,
  260. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  261. )
  262. {
  263. return( ( (CWindowContext *)Context )->GetWindowRights(
  264. SecurityInfo,
  265. ppSecurityDescriptor
  266. ) );
  267. }
  268. DWORD
  269. MartaOpenWindowNamedObject(
  270. IN LPCWSTR pObjectName,
  271. IN ACCESS_MASK AccessMask,
  272. OUT PMARTA_CONTEXT pContext
  273. )
  274. {
  275. DWORD Result;
  276. CWindowContext* pWindowContext;
  277. pWindowContext = new CWindowContext;
  278. if ( pWindowContext == NULL )
  279. {
  280. return( ERROR_OUTOFMEMORY );
  281. }
  282. Result = pWindowContext->InitializeByName( pObjectName, AccessMask );
  283. if ( Result != ERROR_SUCCESS )
  284. {
  285. pWindowContext->Release();
  286. return( Result );
  287. }
  288. *pContext = pWindowContext;
  289. return( ERROR_SUCCESS );
  290. }
  291. DWORD
  292. MartaOpenWindowHandleObject(
  293. IN HANDLE Handle,
  294. IN ACCESS_MASK AccessMask,
  295. OUT PMARTA_CONTEXT pContext
  296. )
  297. {
  298. DWORD Result;
  299. CWindowContext* pWindowContext;
  300. pWindowContext = new CWindowContext;
  301. if ( pWindowContext == NULL )
  302. {
  303. return( ERROR_OUTOFMEMORY );
  304. }
  305. Result = pWindowContext->InitializeByHandle( Handle );
  306. if ( Result != ERROR_SUCCESS )
  307. {
  308. pWindowContext->Release();
  309. return( Result );
  310. }
  311. *pContext = pWindowContext;
  312. return( ERROR_SUCCESS );
  313. }
  314. DWORD
  315. MartaSetWindowRights(
  316. IN MARTA_CONTEXT Context,
  317. IN SECURITY_INFORMATION SecurityInfo,
  318. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  319. )
  320. {
  321. return( ( (CWindowContext *)Context )->SetWindowRights(
  322. SecurityInfo,
  323. pSecurityDescriptor
  324. ) );
  325. }