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.

629 lines
14 KiB

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