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.

382 lines
9.7 KiB

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