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.

575 lines
13 KiB

  1. /******************************************************************
  2. ConnectionToShare.CPP -- C provider class implementation
  3. Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  4. Description: Association between Connection and Share class
  5. ******************************************************************/
  6. #include "precomp.h"
  7. #include "ConnectionToShare.h"
  8. CConnectionToShare MyCConnectionToShare (
  9. PROVIDER_NAME_CONNECTIONTOSHARE ,
  10. Namespace
  11. ) ;
  12. /*****************************************************************************
  13. *
  14. * FUNCTION : CConnectionToShare::CConnectionToShare
  15. *
  16. * DESCRIPTION : Constructor
  17. *
  18. *****************************************************************************/
  19. CConnectionToShare :: CConnectionToShare (
  20. LPCWSTR lpwszName,
  21. LPCWSTR lpwszNameSpace
  22. ) : Provider ( lpwszName , lpwszNameSpace )
  23. {
  24. }
  25. /*****************************************************************************
  26. *
  27. * FUNCTION : CConnectionToShare::~CConnectionToShare
  28. *
  29. * DESCRIPTION : Destructor
  30. *
  31. *****************************************************************************/
  32. CConnectionToShare :: ~CConnectionToShare ()
  33. {
  34. }
  35. /*****************************************************************************
  36. *
  37. * FUNCTION : CConnectionToShare::EnumerateInstances
  38. *
  39. * DESCRIPTION : Returns all the instances of this class.
  40. *
  41. *****************************************************************************/
  42. HRESULT CConnectionToShare :: EnumerateInstances (
  43. MethodContext *pMethodContext,
  44. long lFlags
  45. )
  46. {
  47. HRESULT hRes = WBEM_S_NO_ERROR ;
  48. DWORD dwPropertiesReq = CONNECTIONSTOSHARE_ALL_PROPS;
  49. CHString t_ComputerName;
  50. CHString t_ShareName;
  51. hRes = EnumConnectionInfo (
  52. t_ComputerName,
  53. t_ShareName,
  54. pMethodContext,
  55. dwPropertiesReq
  56. ) ;
  57. return hRes ;
  58. }
  59. /*****************************************************************************
  60. *
  61. * FUNCTION : CConnectionToShare::GetObject
  62. *
  63. * DESCRIPTION : Find a single instance based on the key properties for the
  64. * class.
  65. *
  66. *****************************************************************************/
  67. HRESULT CConnectionToShare :: GetObject (
  68. CInstance *pInstance,
  69. long lFlags ,
  70. CFrameworkQuery &Query
  71. )
  72. {
  73. HRESULT hRes = WBEM_S_NO_ERROR;
  74. CHString t_Key1 ;
  75. CHString t_Key2;
  76. if ( pInstance->GetCHString ( IDS_Connection , t_Key1 ) == FALSE )
  77. {
  78. hRes = WBEM_E_INVALID_PARAMETER ;
  79. }
  80. if ( SUCCEEDED ( hRes ) )
  81. {
  82. if ( pInstance->GetCHString ( IDS_Resource , t_Key2 ) == FALSE )
  83. {
  84. hRes = WBEM_E_INVALID_PARAMETER ;
  85. }
  86. }
  87. // here we will need to unparse the keys and check if the instance exist
  88. // We can take the resource (share) key and enumerate all the shares,
  89. // check if the share exists, if the share exists only for that share enumerate connections
  90. // and if the connection user and computer enumerate the connections, if it is found set the keys
  91. // otherwise return not found
  92. CHString t_Share;
  93. hRes = GetShareKeyVal ( t_Key2, t_Share );
  94. if ( SUCCEEDED ( hRes ) )
  95. {
  96. CHString t_ComputerName;
  97. CHString t_ShareName;
  98. CHString t_UserName;
  99. hRes = GetConnectionsKeyVal ( t_Key1, t_ComputerName, t_ShareName, t_UserName );
  100. if ( SUCCEEDED ( hRes ) )
  101. {
  102. // now check the shares in t_key1 and t_key should match
  103. if ( _wcsicmp ( t_Key2, t_ShareName ) == 0 )
  104. {
  105. #ifdef NTONLY
  106. hRes = FindAndSetNTConnection ( t_ShareName, t_ComputerName, t_UserName,
  107. 0, pInstance, NoOp );
  108. #endif
  109. #if 0
  110. #ifdef WIN9XONLY
  111. hRes = FindAndSet9XConnection ( t_ShareName, t_ComputerName, t_UserName,
  112. 0, pInstance, NoOp );
  113. #endif
  114. #endif
  115. }
  116. }
  117. }
  118. return hRes ;
  119. }
  120. #ifdef NTONLY
  121. /*****************************************************************************
  122. *
  123. * FUNCTION : CConnectionToShare::EnumNTConnectionsFromComputerToShare
  124. *
  125. * DESCRIPTION : Enumerates all Connections from a computer to share
  126. *
  127. *****************************************************************************/
  128. HRESULT CConnectionToShare :: EnumNTConnectionsFromComputerToShare (
  129. CHString a_ComputerName,
  130. CHString a_ShareName,
  131. MethodContext *pMethodContext,
  132. DWORD dwPropertiesReq
  133. )
  134. {
  135. HRESULT hRes = WBEM_S_NO_ERROR;
  136. NET_API_STATUS t_Status = NERR_Success;
  137. DWORD dwNoOfEntriesRead = 0;
  138. DWORD dwTotalConnections = 0;
  139. DWORD dwResumeHandle = 0;
  140. CONNECTION_INFO *pBuf = NULL;
  141. CONNECTION_INFO *pTmpBuf = NULL;
  142. LPWSTR t_ComputerName = NULL;
  143. if ( a_ComputerName && a_ComputerName[0] != L'\0' )
  144. {
  145. //let's skip the \\ chars
  146. t_ComputerName = a_ComputerName + 2;
  147. }
  148. // ShareName and COmputer Name both cannot be null at the same time
  149. while ( TRUE )
  150. {
  151. if ( ! a_ShareName.IsEmpty())
  152. {
  153. t_Status = NetConnectionEnum(
  154. NULL,
  155. a_ShareName.GetBuffer ( a_ShareName.GetLength() + 1),
  156. 1,
  157. (LPBYTE *) &pBuf,
  158. -1,
  159. &dwNoOfEntriesRead,
  160. &dwTotalConnections,
  161. &dwResumeHandle
  162. );
  163. }
  164. else
  165. if ( ! a_ComputerName.IsEmpty() )
  166. {
  167. t_Status = NetConnectionEnum(
  168. NULL,
  169. a_ComputerName.GetBuffer ( a_ShareName.GetLength() + 1 ),
  170. 1,
  171. (LPBYTE *) &pBuf,
  172. -1,
  173. &dwNoOfEntriesRead,
  174. &dwTotalConnections,
  175. &dwResumeHandle
  176. );
  177. }
  178. if ( t_Status == NERR_Success )
  179. {
  180. if ( dwNoOfEntriesRead == 0 )
  181. {
  182. break;
  183. }
  184. else if ( dwNoOfEntriesRead > 0 )
  185. {
  186. try
  187. {
  188. pTmpBuf = pBuf;
  189. for ( int i = 0; i < dwNoOfEntriesRead; i++, pTmpBuf++ )
  190. {
  191. CInstancePtr pInstance ( CreateNewInstance ( pMethodContext ), FALSE );
  192. hRes = LoadInstance ( pInstance, a_ShareName, t_ComputerName ? t_ComputerName : a_ComputerName, pTmpBuf, dwPropertiesReq );
  193. if ( SUCCEEDED ( hRes ) )
  194. {
  195. hRes = pInstance->Commit();
  196. if ( FAILED ( hRes ) )
  197. {
  198. break;
  199. }
  200. }
  201. else
  202. {
  203. break;
  204. }
  205. }
  206. }
  207. catch ( ... )
  208. {
  209. NetApiBufferFree ( pBuf );
  210. pBuf = NULL;
  211. throw;
  212. }
  213. NetApiBufferFree ( pBuf );
  214. pBuf = NULL;
  215. }
  216. }
  217. else
  218. {
  219. if ( t_Status != ERROR_MORE_DATA )
  220. {
  221. if ( t_Status == ERROR_ACCESS_DENIED )
  222. {
  223. hRes = WBEM_E_ACCESS_DENIED;
  224. }
  225. else
  226. {
  227. if ( t_Status == ERROR_NOT_ENOUGH_MEMORY )
  228. {
  229. hRes = WBEM_E_OUT_OF_MEMORY;
  230. }
  231. else
  232. {
  233. hRes = WBEM_E_FAILED;
  234. }
  235. }
  236. break;
  237. }
  238. }
  239. }
  240. return hRes;
  241. }
  242. #endif
  243. #if 0
  244. #ifdef WIN9XONLY
  245. /*****************************************************************************
  246. *
  247. * FUNCTION : CConnectionToShare::Enum9XConnectionsFromComputerToShare
  248. *
  249. * DESCRIPTION : Enumerating all the connections made from a computer to
  250. * a given share
  251. *
  252. *****************************************************************************/
  253. HRESULT CConnectionToShare :: Enum9XConnectionsFromComputerToShare (
  254. CHString a_ComputerName,
  255. CHString a_ShareName,
  256. MethodContext *pMethodContext,
  257. DWORD dwPropertiesReq
  258. )
  259. {
  260. HRESULT hRes = WBEM_S_NO_ERROR;
  261. NET_API_STATUS t_Status = NERR_Success;
  262. DWORD dwNoOfEntriesRead = 0;
  263. DWORD dwTotalConnections = 0;
  264. BOOL bFound = FALSE;
  265. CONNECTION_INFO * pBuf = NULL;
  266. CONNECTION_INFO * pTmpBuf = NULL;
  267. DWORD dwBufferSize = MAX_ENTRIES * sizeof( CONNECTION_INFO );
  268. pBuf = ( CONNECTION_INFO *) malloc(dwBufferSize);
  269. if ( pBuf != NULL )
  270. {
  271. try
  272. {
  273. t_Status = NetConnectionEnum(
  274. NULL,
  275. (char FAR *) ( a_ShareName.GetBuffer ( a_ShareName.GetLength () + 1 )), // ShareName
  276. 1,
  277. (char *) pBuf,
  278. ( unsigned short )dwBufferSize,
  279. ( unsigned short *) &dwNoOfEntriesRead,
  280. ( unsigned short *) &dwTotalConnections
  281. );
  282. }
  283. catch ( ... )
  284. {
  285. free ( pBuf );
  286. pBuf = NULL;
  287. throw;
  288. }
  289. // otherwise we are not to frr the buffer, we have use it and then free the buffer.
  290. if ( ( dwNoOfEntriesRead < dwTotalConnections ) && ( t_Status == ERROR_MORE_DATA ) )
  291. {
  292. free ( pBuf );
  293. pBuf = NULL;
  294. pBuf = ( CONNECTION_INFO *) malloc( dwTotalConnections );
  295. if ( pBuf != NULL )
  296. {
  297. try
  298. {
  299. t_Status = NetConnectionEnum(
  300. NULL,
  301. (char FAR *) ( a_ShareName.GetBuffer ( a_ShareName.GetLength () + 1 )), // ShareName
  302. 1,
  303. (char *) pBuf,
  304. ( unsigned short )dwBufferSize,
  305. ( unsigned short *) &dwNoOfEntriesRead,
  306. ( unsigned short *) &dwTotalConnections
  307. );
  308. }
  309. catch ( ... )
  310. {
  311. free ( pBuf );
  312. pBuf = NULL;
  313. throw;
  314. }
  315. // We need to use the buffer before we free it
  316. }
  317. else
  318. {
  319. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  320. }
  321. }
  322. // The buffer is yet to be used
  323. if ( ( t_Status == NERR_Success ) && ( dwNoOfEntriesRead == dwTotalConnections ) )
  324. {
  325. // use the buffer first and then free
  326. if ( pBuf != NULL )
  327. {
  328. try
  329. {
  330. pTmpBuf = pBuf;
  331. for ( int i = 0; i < dwNoOfEntriesRead; i++, pTmpBuf ++)
  332. {
  333. CInstancePtr pInstance ( CreateNewInstance ( pMethodContext ), FALSE );
  334. hRes = LoadInstance ( pInstance, a_ShareName, a_ComputerName, pTmpBuf, dwPropertiesReq );
  335. if ( SUCCEEDED ( hRes ) )
  336. {
  337. hRes = pInstance->Commit();
  338. }
  339. }
  340. }
  341. catch ( ... )
  342. {
  343. free ( pBuf );
  344. pBuf = NULL;
  345. throw;
  346. }
  347. // finally free the buffer
  348. free (pBuf );
  349. pBuf = NULL;
  350. }
  351. else
  352. {
  353. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  354. }
  355. }
  356. else
  357. {
  358. hRes = WBEM_E_FAILED;
  359. }
  360. }
  361. else
  362. {
  363. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  364. }
  365. return hRes;
  366. }
  367. #endif
  368. #endif
  369. /*****************************************************************************
  370. *
  371. * FUNCTION : CConnectionToShare::LoadInstance
  372. *
  373. * DESCRIPTION : Loads the Given Instance
  374. *
  375. *****************************************************************************/
  376. HRESULT CConnectionToShare :: LoadInstance (
  377. CInstance *pInstance,
  378. CHString a_Share,
  379. CHString a_Computer,
  380. CONNECTION_INFO *pBuf,
  381. DWORD dwPropertiesReq
  382. )
  383. {
  384. HRESULT hRes = WBEM_S_NO_ERROR;
  385. LPWSTR ObjPath;
  386. LPWSTR ResObjPath;
  387. CHString t_NetName ( pBuf->coni1_netname );
  388. if ( ! a_Share.IsEmpty() )
  389. {
  390. hRes = MakeObjectPath ( ObjPath, PROVIDER_NAME_CONNECTION, IDS_ComputerName, t_NetName.GetBuffer ( t_NetName.GetLength () + 1) );
  391. if ( SUCCEEDED ( hRes ) )
  392. {
  393. hRes = AddToObjectPath ( ObjPath, IDS_ShareName, a_Share.GetBuffer (a_Share.GetLength () + 1) );
  394. }
  395. if ( SUCCEEDED ( hRes ) )
  396. {
  397. hRes = MakeObjectPath ( ResObjPath, PROVIDER_SHARE, IDS_ShareKeyName, a_Share.GetBuffer (a_Share.GetLength () + 1) );
  398. }
  399. }
  400. else
  401. {
  402. hRes = MakeObjectPath ( ObjPath, PROVIDER_NAME_CONNECTION, IDS_ComputerName, a_Computer.GetBuffer ( a_Computer.GetLength () +1));
  403. if ( SUCCEEDED ( hRes ) )
  404. {
  405. hRes = AddToObjectPath ( ObjPath, IDS_ShareName, t_NetName.GetBuffer ( t_NetName.GetLength () + 1) );
  406. }
  407. if ( SUCCEEDED ( hRes ) )
  408. {
  409. MakeObjectPath ( ResObjPath, PROVIDER_SHARE, IDS_ShareKeyName, t_NetName.GetBuffer ( t_NetName.GetLength () + 1) );
  410. }
  411. }
  412. CHString t_UserName ( pBuf->coni1_username );
  413. if ( SUCCEEDED ( hRes ) )
  414. {
  415. hRes = AddToObjectPath ( ObjPath, IDS_UserName, t_UserName.GetBuffer ( t_UserName.GetLength () + 1 ) );
  416. }
  417. if ( SUCCEEDED ( hRes ) )
  418. {
  419. if ( pInstance->SetCHString ( IDS_Connection, ObjPath ) == FALSE )
  420. {
  421. hRes = WBEM_E_PROVIDER_FAILURE;
  422. }
  423. }
  424. if ( SUCCEEDED ( hRes ) )
  425. {
  426. if ( pInstance->SetCHString ( IDS_Resource, ResObjPath ) == FALSE )
  427. {
  428. hRes = WBEM_E_PROVIDER_FAILURE;
  429. }
  430. }
  431. return hRes;
  432. }
  433. /*****************************************************************************
  434. *
  435. * FUNCTION : CConnectionToShare::GetShareKeyVal
  436. *
  437. * DESCRIPTION : Parsing the key to get the share key value
  438. *
  439. *****************************************************************************/
  440. HRESULT CConnectionToShare::GetShareKeyVal ( CHString a_Key, CHString &a_Share )
  441. {
  442. HRESULT hRes = WBEM_S_NO_ERROR;
  443. ParsedObjectPath *t_ShareObjPath;
  444. CObjectPathParser t_PathParser;
  445. if ( t_PathParser.Parse( a_Key.GetBuffer ( a_Key.GetLength () + 1 ), &t_ShareObjPath ) == t_PathParser.NoError )
  446. {
  447. try
  448. {
  449. if ( t_ShareObjPath->m_dwNumKeys == 1 )
  450. {
  451. a_Share = t_ShareObjPath->GetKeyString();
  452. if ( ! a_Share.IsEmpty() )
  453. {
  454. CHStringArray t_aShares;
  455. #ifdef NTONLY
  456. hRes = GetNTShares ( t_aShares );
  457. #endif
  458. #if 0
  459. #ifdef WIN9XONLY
  460. hRes = Get9XShares ( t_aShares );
  461. #endif
  462. #endif
  463. if ( SUCCEEDED ( hRes ) )
  464. {
  465. int i = 0;
  466. for ( i = 0; i < t_aShares.GetSize(); i++ )
  467. {
  468. if ( _wcsicmp ( a_Share, t_aShares.GetAt(i) ) == 0 )
  469. {
  470. break;
  471. }
  472. }
  473. if ( i >= t_aShares.GetSize() )
  474. {
  475. hRes = WBEM_E_NOT_FOUND;
  476. }
  477. }
  478. }
  479. }
  480. else
  481. {
  482. hRes = WBEM_E_INVALID_PARAMETER;
  483. }
  484. }
  485. catch ( ... )
  486. {
  487. delete t_ShareObjPath;
  488. throw;
  489. }
  490. delete t_ShareObjPath;
  491. }
  492. else
  493. {
  494. hRes = WBEM_E_INVALID_PARAMETER;
  495. }
  496. return hRes;
  497. }