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.

1738 lines
54 KiB

  1. /******************************************************************
  2. DskQuota.CPP -- WMI provider class implementation
  3. Description: Disk Quota Provider
  4. Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  5. ******************************************************************/
  6. #include "precomp.h"
  7. // #include for DiskQuota Provider Class
  8. #include "DiskQuota.h"
  9. #include "dllutils.h"
  10. CDiskQuota MyCDiskQuota (
  11. IDS_DiskQuotaClass ,
  12. NameSpace
  13. ) ;
  14. /*****************************************************************************
  15. *
  16. * FUNCTION : CDiskQuota::CDiskQuota
  17. *
  18. * DESCRIPTION : Constructor
  19. *
  20. * COMMENTS : Calls the Provider constructor.
  21. *
  22. *****************************************************************************/
  23. CDiskQuota :: CDiskQuota (
  24. LPCWSTR lpwszName,
  25. LPCWSTR lpwszNameSpace
  26. ) : Provider ( lpwszName , lpwszNameSpace )
  27. {
  28. m_ComputerName = GetLocalComputerName();
  29. }
  30. /*****************************************************************************
  31. *
  32. * FUNCTION : CDiskQuota::~CDiskQuota
  33. *
  34. * DESCRIPTION : Destructor
  35. *
  36. * COMMENTS :
  37. *
  38. *****************************************************************************/
  39. CDiskQuota :: ~CDiskQuota ()
  40. {
  41. }
  42. HRESULT FindUser(CHString& user, IDiskQuotaControlPtr& controler)
  43. {
  44. IEnumDiskQuotaUsersPtr userEnum;
  45. HRESULT hr = controler->CreateEnumUsers(0,0,DISKQUOTA_USERNAME_RESOLVE_SYNC, &userEnum);
  46. if (FAILED(hr)) return hr;
  47. CHString logonName;
  48. LPWSTR logonNameBuffer = logonName.GetBuffer(MAX_PATH + 1);
  49. IDiskQuotaUserPtr userQuota;
  50. while((hr = userEnum->Next(1, &userQuota, 0)) == NOERROR)
  51. {
  52. if (SUCCEEDED(userQuota->GetName( 0, 0, logonNameBuffer, MAX_PATH, 0, 0))
  53. && logonName == user) return S_OK;
  54. userQuota.Release();
  55. };
  56. if (hr== S_FALSE) // Enumeration completed
  57. return WBEM_E_NOT_FOUND;
  58. else
  59. return hr;
  60. };
  61. /*****************************************************************************
  62. *
  63. * FUNCTION : CDiskQuota::EnumerateInstances
  64. *
  65. * DESCRIPTION : Returns all the instances of this class.
  66. *
  67. * COMMENTS : All instances of Disk Quota users on all Logical Disks that
  68. * supports disk quota on a machine with all the properties of
  69. * DiskQuota users should be returned here.
  70. *
  71. *****************************************************************************/
  72. HRESULT CDiskQuota :: EnumerateInstances (
  73. MethodContext *pMethodContext,
  74. long lFlags
  75. )
  76. {
  77. DWORD dwPropertiesReq = DSKQUOTA_ALL_PROPS;
  78. HRESULT hRes = WBEM_S_NO_ERROR;
  79. hRes = EnumerateUsersOfAllVolumes ( pMethodContext, dwPropertiesReq );
  80. return hRes;
  81. }
  82. /*****************************************************************************
  83. *
  84. * FUNCTION : CDiskQuota::GetObject
  85. *
  86. * DESCRIPTION : Find a single instance based on the key properties for the
  87. * class.
  88. *
  89. *****************************************************************************/
  90. HRESULT CDiskQuota :: GetObject (
  91. CInstance *pInstance,
  92. long lFlags ,
  93. CFrameworkQuery &Query
  94. )
  95. {
  96. HRESULT hRes = WBEM_S_NO_ERROR;
  97. CHString t_Key1;
  98. CHString t_Key2;
  99. // Obtain the keys.
  100. if ( pInstance->GetCHString ( IDS_LogicalDiskObjectPath , t_Key1 ) == FALSE )
  101. {
  102. hRes = WBEM_E_FAILED ;
  103. }
  104. if ( SUCCEEDED ( hRes ) )
  105. {
  106. if ( pInstance->GetCHString ( IDS_UserObjectPath , t_Key2 ) == FALSE )
  107. {
  108. hRes = WBEM_E_FAILED ;
  109. }
  110. }
  111. if ( SUCCEEDED ( hRes ) )
  112. {
  113. CHString t_DiskPropVolumePath;
  114. GetKeyValue ( t_DiskPropVolumePath,t_Key1 );
  115. if (t_DiskPropVolumePath.IsEmpty())
  116. {
  117. hRes = WBEM_E_NOT_FOUND;
  118. }
  119. else
  120. {
  121. // verify this logical drives actually exists
  122. WCHAR lpDriveStrings[(MAX_PATH * 2) + 1];
  123. DWORD dwDLength = GetLogicalDriveStrings ( (MAX_PATH * 2), lpDriveStrings );
  124. hRes = m_CommonRoutine.SearchLogicalDisk ( t_DiskPropVolumePath.GetAt ( 0 ), lpDriveStrings );
  125. }
  126. if ( SUCCEEDED ( hRes ) )
  127. {
  128. WCHAR w_VolumePathName [ MAX_PATH + 1 ];
  129. t_DiskPropVolumePath += L"\\";
  130. if ( GetVolumeNameForVolumeMountPoint(
  131. t_DiskPropVolumePath,
  132. w_VolumePathName,
  133. MAX_PATH
  134. ))
  135. {
  136. // Get the key values, which will be the object path.
  137. // Now from the Volume Object path, parse out the volumename
  138. // from the User object path extract out the user Id.
  139. // for the volume specified check whether the given volume Supports Disk Quotas
  140. CHString t_VolumeName;
  141. hRes = m_CommonRoutine.VolumeSupportsDiskQuota ( w_VolumePathName, t_VolumeName );
  142. if ( SUCCEEDED ( hRes ) )
  143. {
  144. // Get IDIskQuotaCOntrol for this interface pointer
  145. IDiskQuotaControlPtr pIQuotaControl;
  146. if ( SUCCEEDED ( CoCreateInstance(
  147. CLSID_DiskQuotaControl,
  148. NULL,
  149. CLSCTX_INPROC_SERVER,
  150. IID_IDiskQuotaControl,
  151. (void **)&pIQuotaControl ) ) )
  152. {
  153. // Initialise the pIQuotaControl with the given volume
  154. hRes = m_CommonRoutine.InitializeInterfacePointer ( pIQuotaControl, w_VolumePathName );
  155. if ( SUCCEEDED ( hRes ) )
  156. {
  157. IDiskQuotaUserPtr pIQuotaUser;
  158. CHString t_UserLogName;
  159. GetKeyValue ( t_UserLogName, t_Key2 );
  160. HRESULT hrTemp = WBEM_E_NOT_FOUND;
  161. if (!t_UserLogName.IsEmpty())
  162. {
  163. hrTemp = pIQuotaControl->FindUserName(
  164. t_UserLogName,
  165. &pIQuotaUser);
  166. // Certain Win32_Account instances report
  167. // the Domain as computername instead of
  168. // builtin, so change domain to builtin and
  169. // try again.
  170. CHString chstrBuiltIn;
  171. if(FAILED(hrTemp) && GetLocalizedBuiltInString(chstrBuiltIn))
  172. {
  173. int iWhackPos = t_UserLogName.Find(L"\\");
  174. CHString chstrDomain = t_UserLogName.Left(iWhackPos);
  175. if(chstrDomain.CompareNoCase(GetLocalComputerName()) == 0)
  176. {
  177. CHString chstrUNameOnly = t_UserLogName.Mid(iWhackPos);
  178. CHString chstrDomWhackName = chstrBuiltIn;
  179. chstrDomWhackName += chstrUNameOnly;
  180. hrTemp = pIQuotaControl->FindUserName(
  181. chstrDomWhackName,
  182. &pIQuotaUser);
  183. }
  184. }
  185. // Certain Win32_Account instances report
  186. // the Domain as computername instead of
  187. // NT AUTHORITY, so change domain to NT AUTHORITY and
  188. // try again.
  189. if(FAILED(hrTemp))
  190. {
  191. int iWhackPos = t_UserLogName.Find(L"\\");
  192. CHString chstrDomain = t_UserLogName.Left(iWhackPos);
  193. if(chstrDomain.CompareNoCase(GetLocalComputerName()) == 0)
  194. {
  195. CHString chstrUNameOnly = t_UserLogName.Mid(iWhackPos);
  196. CHString chstrNT_AUTHORITY;
  197. CHString chstrDomWhackName;
  198. if(GetLocalizedNTAuthorityString(chstrNT_AUTHORITY))
  199. {
  200. chstrDomWhackName = chstrNT_AUTHORITY;
  201. }
  202. else
  203. {
  204. chstrDomWhackName = L"NT AUTHORITY";
  205. }
  206. chstrDomWhackName += chstrUNameOnly;
  207. hrTemp = pIQuotaControl->FindUserName(
  208. chstrDomWhackName,
  209. &pIQuotaUser);
  210. }
  211. }
  212. }
  213. if(SUCCEEDED(hrTemp))
  214. {
  215. // Put this Instance
  216. DWORD dwPropertiesReq;
  217. if ( Query.AllPropertiesAreRequired() )
  218. {
  219. dwPropertiesReq = DSKQUOTA_ALL_PROPS;
  220. }
  221. else
  222. {
  223. SetPropertiesReq ( &Query, dwPropertiesReq );
  224. }
  225. hRes = LoadDiskQuotaUserProperties ( pIQuotaUser, pInstance, dwPropertiesReq );
  226. }
  227. else
  228. {
  229. hRes = WBEM_E_NOT_FOUND;
  230. }
  231. }
  232. }
  233. else
  234. {
  235. hRes = WBEM_E_FAILED;
  236. }
  237. }
  238. }
  239. else
  240. {
  241. hRes = WBEM_E_NOT_FOUND;
  242. }
  243. }
  244. }
  245. return hRes;
  246. }
  247. /*****************************************************************************
  248. *
  249. * FUNCTION : CDiskQuota::ExecQuery
  250. *
  251. * DESCRIPTION : You are passed a method context to use in the creation of
  252. * instances that satisfy the query, and a CFrameworkQuery
  253. * which describes the query. Create and populate all
  254. * instances which satisfy the query.
  255. * a) Queries involving Properties other than Win32_LogicalDisk
  256. * are not optimized. Since that would involve enumerating
  257. * every user on all volumes
  258. *
  259. *****************************************************************************/
  260. HRESULT CDiskQuota :: ExecQuery (
  261. MethodContext *pMethodContext,
  262. CFrameworkQuery &Query,
  263. long lFlags
  264. )
  265. {
  266. HRESULT hRes = WBEM_S_NO_ERROR;
  267. DWORD dwPropertiesReq;
  268. CHStringArray t_Values;
  269. // Now a check for the attribute which if present in where clause the query optimization is supported
  270. // We need not care for the other attributes for which Optimization is not supported, winmgmt will take
  271. // care of those.
  272. hRes = Query.GetValuesForProp(
  273. IDS_LogicalDiskObjectPath,
  274. t_Values
  275. );
  276. if ( Query.AllPropertiesAreRequired() )
  277. {
  278. dwPropertiesReq = DSKQUOTA_ALL_PROPS;
  279. }
  280. else
  281. {
  282. SetPropertiesReq ( &Query, dwPropertiesReq );
  283. }
  284. if ( SUCCEEDED ( hRes ) )
  285. {
  286. if ( t_Values.GetSize() == 0 )
  287. {
  288. hRes = EnumerateUsersOfAllVolumes ( pMethodContext, dwPropertiesReq );
  289. }
  290. else
  291. {
  292. // Only Volume in QuotaVolume properties are needed to be enumerated
  293. int iSize = t_Values.GetSize ();
  294. // verify this logical drives actually exists
  295. WCHAR lpDriveStrings [(MAX_PATH * 2) + 1];
  296. DWORD dwDLength = GetLogicalDriveStrings ( (MAX_PATH * 2), lpDriveStrings );
  297. for ( int i = 0; i < iSize; i++ )
  298. {
  299. CHString t_VolumePath;
  300. //Here we need to parse the VolumeObject path and extract VolumePath from it.
  301. GetKeyValue ( t_VolumePath, t_Values.GetAt(i) );
  302. if (( t_VolumePath.GetLength() == 2 ) && ( t_VolumePath.GetAt ( 1 ) == _L(':') ) )
  303. {
  304. HRESULT tmpHR = m_CommonRoutine.SearchLogicalDisk ( t_VolumePath.GetAt ( 0 ), lpDriveStrings );
  305. if ( SUCCEEDED ( tmpHR ) )
  306. {
  307. t_VolumePath += L"\\";
  308. hRes = EnumerateUsers ( pMethodContext, t_VolumePath, dwPropertiesReq );
  309. }
  310. }
  311. }
  312. }
  313. }
  314. return hRes;
  315. }
  316. /*****************************************************************************
  317. *
  318. * FUNCTION : CDiskQuota::PutInstance
  319. *
  320. * DESCRIPTION : If the instance is already existing, we only modify the instance
  321. * if it doesnt exist, we will add the instance based on the flags.
  322. *
  323. *****************************************************************************/
  324. HRESULT CDiskQuota :: PutInstance (
  325. const CInstance &Instance,
  326. long lFlags
  327. )
  328. {
  329. HRESULT hRes = WBEM_S_NO_ERROR;
  330. CHString t_Key1;
  331. CHString t_Key2;
  332. BOOL bFound = TRUE;
  333. if ( Instance.GetCHString ( IDS_LogicalDiskObjectPath , t_Key1 ) == FALSE )
  334. {
  335. hRes = WBEM_E_FAILED ;
  336. }
  337. if ( SUCCEEDED ( hRes ) )
  338. {
  339. if ( Instance.GetCHString ( IDS_UserObjectPath , t_Key2 ) == FALSE )
  340. {
  341. hRes = WBEM_E_FAILED ;
  342. }
  343. }
  344. if ( SUCCEEDED ( hRes ) )
  345. {
  346. WCHAR t_VolumePathName[MAX_PATH + 1];
  347. CHString t_UserLogonName;
  348. CHString t_VolumePath;
  349. GetKeyValue ( t_VolumePath,t_Key1 );
  350. if (t_VolumePath.IsEmpty())
  351. {
  352. hRes = WBEM_E_NOT_FOUND ;
  353. }
  354. else
  355. {
  356. // verify this logical drives actually exists
  357. WCHAR lpDriveStrings [(MAX_PATH * 2) + 1];
  358. DWORD dwDLength = GetLogicalDriveStrings ( (MAX_PATH * 2), lpDriveStrings );
  359. hRes = m_CommonRoutine.SearchLogicalDisk ( t_VolumePath.GetAt ( 0 ), lpDriveStrings );
  360. }
  361. if ( SUCCEEDED ( hRes ) )
  362. {
  363. GetKeyValue ( t_UserLogonName, t_Key2 );
  364. if (t_UserLogonName.IsEmpty())
  365. {
  366. hRes = WBEM_E_NOT_FOUND;
  367. }
  368. else
  369. {
  370. t_VolumePath += L"\\";
  371. if ( GetVolumeNameForVolumeMountPoint(
  372. t_VolumePath,
  373. t_VolumePathName,
  374. MAX_PATH
  375. ))
  376. {
  377. // Check if the user already exists
  378. hRes = m_CommonRoutine.VolumeSupportsDiskQuota ( t_VolumePathName, t_VolumePath );
  379. if ( SUCCEEDED ( hRes ) )
  380. {
  381. // Get IDIskQuotaCOntrol for this interface pointer
  382. IDiskQuotaControlPtr pIQuotaControl;
  383. if ( SUCCEEDED ( CoCreateInstance(
  384. CLSID_DiskQuotaControl,
  385. NULL,
  386. CLSCTX_INPROC_SERVER,
  387. IID_IDiskQuotaControl,
  388. (void **)&pIQuotaControl ) ) )
  389. {
  390. // Initialise the pIQuotaControl with the given volume
  391. hRes = m_CommonRoutine.InitializeInterfacePointer ( pIQuotaControl, t_VolumePathName );
  392. if ( SUCCEEDED ( hRes ) )
  393. {
  394. hRes = FindUser(t_UserLogonName,pIQuotaControl );
  395. // Certain Win32_Account instances report
  396. // the Domain as computername instead of
  397. // builtin, so change domain to builtin and
  398. // try again.
  399. CHString chstrBuiltIn;
  400. if( hRes == WBEM_E_NOT_FOUND && GetLocalizedBuiltInString(chstrBuiltIn))
  401. {
  402. int iWhackPos = t_UserLogonName.Find(L"\\");
  403. CHString chstrDomain = t_UserLogonName.Left(iWhackPos);
  404. if(chstrDomain.CompareNoCase(GetLocalComputerName()) == 0)
  405. {
  406. CHString chstrUNameOnly = t_UserLogonName.Mid(iWhackPos);
  407. CHString chstrDomWhackName = chstrBuiltIn;
  408. chstrDomWhackName += chstrUNameOnly;
  409. hRes = FindUser(chstrDomWhackName,pIQuotaControl);
  410. if(SUCCEEDED(hRes))
  411. {
  412. t_UserLogonName = chstrDomWhackName;
  413. }
  414. else if (hRes == WBEM_E_NOT_FOUND)
  415. {
  416. CHString chstrNT_AUTHORITY;
  417. CHString chstrDomWhackName;
  418. if(GetLocalizedNTAuthorityString(chstrNT_AUTHORITY))
  419. {
  420. chstrDomWhackName = chstrNT_AUTHORITY;
  421. }
  422. else
  423. {
  424. chstrDomWhackName = L"NT AUTHORITY";
  425. }
  426. chstrDomWhackName += chstrUNameOnly;
  427. hRes = FindUser(chstrDomWhackName,pIQuotaControl);
  428. if(SUCCEEDED(hRes))
  429. {
  430. t_UserLogonName = chstrDomWhackName;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. else
  438. {
  439. hRes = WBEM_E_FAILED;
  440. }
  441. }
  442. }
  443. else
  444. {
  445. hRes = WBEM_E_NOT_FOUND;
  446. }
  447. }
  448. }
  449. if ( SUCCEEDED ( hRes ) || ( hRes == WBEM_E_NOT_FOUND ) )
  450. {
  451. BOOL bCreate = FALSE;
  452. BOOL bUpdate = FALSE;
  453. switch ( lFlags & 3 )
  454. {
  455. case WBEM_FLAG_CREATE_OR_UPDATE:
  456. {
  457. if ( hRes == WBEM_E_NOT_FOUND )
  458. {
  459. bCreate = TRUE;
  460. hRes = WBEM_S_NO_ERROR;
  461. }
  462. else
  463. bUpdate = TRUE;
  464. }
  465. break;
  466. case WBEM_FLAG_UPDATE_ONLY:
  467. {
  468. bUpdate = TRUE;
  469. }
  470. break;
  471. case WBEM_FLAG_CREATE_ONLY:
  472. {
  473. if ( hRes == WBEM_E_NOT_FOUND )
  474. {
  475. bCreate = TRUE;
  476. hRes = WBEM_S_NO_ERROR;
  477. }
  478. else
  479. {
  480. hRes = WBEM_E_ALREADY_EXISTS ;
  481. }
  482. }
  483. break;
  484. default:
  485. {
  486. hRes = WBEM_E_PROVIDER_NOT_CAPABLE;
  487. }
  488. }
  489. if (SUCCEEDED(hRes))
  490. {
  491. if ( bCreate )
  492. {
  493. hRes = AddUserOnVolume ( Instance,
  494. t_VolumePathName,
  495. t_UserLogonName );
  496. }
  497. else
  498. if ( bUpdate )
  499. {
  500. hRes = UpdateUserQuotaProperties ( Instance,
  501. t_VolumePathName,
  502. t_UserLogonName);
  503. }
  504. }
  505. }
  506. }
  507. return hRes ;
  508. }
  509. /*****************************************************************************
  510. *
  511. * FUNCTION : CDiskQuota:: DeleteInstance
  512. *
  513. * DESCRIPTION : If the given instance of a user exists on the Volume,
  514. * the user is deleted, meaning diskquota properties
  515. * will no t be applicable to this user.
  516. *
  517. *****************************************************************************/
  518. HRESULT CDiskQuota :: DeleteInstance (
  519. const CInstance &Instance,
  520. long lFlags
  521. )
  522. {
  523. HRESULT hRes = WBEM_S_NO_ERROR;
  524. CHString t_Key1;
  525. CHString t_Key2;
  526. if ( Instance.GetCHString ( IDS_LogicalDiskObjectPath , t_Key1 ) == FALSE )
  527. {
  528. hRes = WBEM_E_FAILED ;
  529. }
  530. if ( SUCCEEDED ( hRes ) )
  531. {
  532. if ( Instance.GetCHString ( IDS_UserObjectPath , t_Key2 ) == FALSE )
  533. {
  534. hRes = WBEM_E_FAILED ;
  535. }
  536. }
  537. if ( SUCCEEDED ( hRes ) )
  538. {
  539. CHString t_VolumePath;
  540. GetKeyValue ( t_VolumePath,t_Key1 );
  541. if (t_VolumePath.IsEmpty())
  542. {
  543. hRes = WBEM_E_NOT_FOUND;
  544. }
  545. else
  546. {
  547. // verify this logical drives actually exists
  548. WCHAR lpDriveStrings [ (MAX_PATH * 2) + 1 ];
  549. DWORD dwDLength = GetLogicalDriveStrings ( (MAX_PATH * 2), lpDriveStrings );
  550. if ( ( t_VolumePath.GetLength() == 2 ) && ( t_VolumePath.GetAt ( 1 ) == L':') )
  551. {
  552. hRes = m_CommonRoutine.SearchLogicalDisk ( t_VolumePath.GetAt ( 0 ), lpDriveStrings );
  553. if ( SUCCEEDED ( hRes ) )
  554. {
  555. t_VolumePath += L"\\";
  556. CHString t_UserLogonName;
  557. GetKeyValue ( t_UserLogonName, t_Key2 );
  558. if (t_UserLogonName.IsEmpty())
  559. {
  560. hRes = WBEM_E_NOT_FOUND;
  561. }
  562. else
  563. {
  564. WCHAR t_VolumePathName[MAX_PATH + 1];
  565. if ( GetVolumeNameForVolumeMountPoint(
  566. t_VolumePath,
  567. t_VolumePathName,
  568. MAX_PATH
  569. ) )
  570. {
  571. // for the volume specified check whether the given volume Supports Disk Quotas
  572. CHString t_TempVolumeName;
  573. hRes = m_CommonRoutine.VolumeSupportsDiskQuota ( t_VolumePathName, t_TempVolumeName );
  574. if ( SUCCEEDED ( hRes ) )
  575. {
  576. // Get IDIskQuotaCOntrol for this interface pointer
  577. IDiskQuotaControlPtr pIQuotaControl;
  578. if ( SUCCEEDED ( CoCreateInstance(
  579. CLSID_DiskQuotaControl,
  580. NULL,
  581. CLSCTX_INPROC_SERVER,
  582. IID_IDiskQuotaControl,
  583. (void **)&pIQuotaControl ) ) )
  584. {
  585. // Initialise the pIQuotaControl with the given volume
  586. hRes = m_CommonRoutine.InitializeInterfacePointer ( pIQuotaControl, t_VolumePathName );
  587. if ( SUCCEEDED ( hRes ) )
  588. {
  589. IDiskQuotaUserPtr pIQuotaUser;
  590. hRes = pIQuotaControl->FindUserName(
  591. t_UserLogonName,
  592. &pIQuotaUser
  593. );
  594. // Certain Win32_Account instances report
  595. // the Domain as computername instead of
  596. // builtin, so change domain to builtin and
  597. // try again.
  598. CHString chstrBuiltIn;
  599. if(FAILED(hRes) && GetLocalizedBuiltInString(chstrBuiltIn))
  600. {
  601. int iWhackPos = t_UserLogonName.Find(L"\\");
  602. CHString chstrDomain = t_UserLogonName.Left(iWhackPos);
  603. if(chstrDomain.CompareNoCase(GetLocalComputerName()) == 0)
  604. {
  605. CHString chstrUNameOnly = t_UserLogonName.Mid(iWhackPos);
  606. CHString chstrDomWhackName = chstrBuiltIn;
  607. chstrDomWhackName += chstrUNameOnly;
  608. hRes = pIQuotaControl->FindUserName(
  609. chstrDomWhackName,
  610. &pIQuotaUser);
  611. }
  612. }
  613. // Get the user properties
  614. if ( SUCCEEDED ( hRes ) )
  615. {
  616. // Since the user is found delete the user.
  617. hRes = pIQuotaControl->DeleteUser ( pIQuotaUser );
  618. if (FAILED(hRes))
  619. {
  620. if (SCODE_CODE(hRes) == ERROR_ACCESS_DENIED)
  621. {
  622. hRes = WBEM_E_ACCESS_DENIED;
  623. }
  624. else
  625. {
  626. hRes = WBEM_E_FAILED;
  627. }
  628. }
  629. }
  630. else
  631. {
  632. hRes = WBEM_E_NOT_FOUND;
  633. }
  634. }
  635. }
  636. else
  637. {
  638. hRes = WBEM_E_FAILED;
  639. }
  640. }
  641. }
  642. else
  643. {
  644. hRes = WBEM_E_NOT_FOUND;
  645. }
  646. }
  647. }
  648. }
  649. else
  650. {
  651. hRes = WBEM_E_NOT_FOUND;
  652. }
  653. }
  654. }
  655. return hRes;
  656. }
  657. /*****************************************************************************
  658. *
  659. * FUNCTION : CDiskQuota::EnumerateUsersOfAllVolumes
  660. *
  661. * DESCRIPTION : In this method enumerating volumes and calling enumerate users
  662. * for that volume
  663. *
  664. *****************************************************************************/
  665. HRESULT CDiskQuota :: EnumerateUsersOfAllVolumes (
  666. MethodContext *pMethodContext,
  667. DWORD a_PropertiesReq
  668. )
  669. {
  670. HRESULT hRes = WBEM_S_NO_ERROR;
  671. WCHAR t_VolumeName[MAX_PATH + 1];
  672. SmartCloseVolumeHandle hVol;
  673. hVol = FindFirstVolume(
  674. t_VolumeName, // output buffer
  675. MAX_PATH // size of output buffer
  676. );
  677. if ( hVol != INVALID_HANDLE_VALUE )
  678. {
  679. BOOL bNextVol = TRUE;
  680. // verify this logical drives actually exists
  681. WCHAR lpDriveStrings[(MAX_PATH * 2) + 1];
  682. DWORD dwDLength = GetLogicalDriveStrings ( (MAX_PATH * 2), lpDriveStrings );
  683. CHString t_VolumePath;
  684. while ( bNextVol )
  685. {
  686. m_CommonRoutine.GetVolumeDrive ( t_VolumeName, lpDriveStrings, t_VolumePath );
  687. EnumerateUsers ( pMethodContext, t_VolumePath, a_PropertiesReq );
  688. bNextVol = FindNextVolume(
  689. hVol, // volume search handle
  690. t_VolumeName, // output buffer
  691. MAX_PATH // size of output buffer
  692. );
  693. }
  694. }
  695. else
  696. {
  697. hRes = WBEM_E_FAILED;
  698. }
  699. return hRes;
  700. }
  701. /*****************************************************************************
  702. *
  703. * FUNCTION : CDiskQuota::EnumerateUsers
  704. *
  705. * DESCRIPTION : In this method Enumerating all the users of a given volume that
  706. * Supports DiskQuotas
  707. *
  708. *****************************************************************************/
  709. HRESULT CDiskQuota :: EnumerateUsers (
  710. MethodContext *pMethodContext,
  711. LPCWSTR a_VolumeName,
  712. DWORD a_PropertiesReq
  713. )
  714. {
  715. HRESULT hRes = WBEM_S_NO_ERROR;
  716. // checking whether the given volume supports disk quotas, and getting the Volume name which is readable to the
  717. // user, i.e. not containing the GUID.
  718. CHString t_VolumeName;
  719. hRes = m_CommonRoutine.VolumeSupportsDiskQuota ( a_VolumeName, t_VolumeName );
  720. if ( SUCCEEDED ( hRes ) )
  721. {
  722. // Get the QuotaInterface Pointer
  723. IDiskQuotaControlPtr pIQuotaControl;
  724. if ( SUCCEEDED ( CoCreateInstance(
  725. CLSID_DiskQuotaControl,
  726. NULL,
  727. CLSCTX_INPROC_SERVER,
  728. IID_IDiskQuotaControl,
  729. (void **)&pIQuotaControl ) ) )
  730. {
  731. // initializing the Interface pointer for a particular volume
  732. hRes = m_CommonRoutine.InitializeInterfacePointer ( pIQuotaControl, a_VolumeName );
  733. if ( SUCCEEDED ( hRes ) )
  734. {
  735. // need to update the cache, else we can get old names
  736. pIQuotaControl->InvalidateSidNameCache();
  737. IEnumDiskQuotaUsersPtr pIEnumDiskQuotaUsers;
  738. if ( SUCCEEDED ( pIQuotaControl->CreateEnumUsers(
  739. NULL, //All the users will be enumerated
  740. 0, // Ignored for enumerating all users
  741. DISKQUOTA_USERNAME_RESOLVE_SYNC,
  742. &pIEnumDiskQuotaUsers
  743. ) ) )
  744. {
  745. if ( pIEnumDiskQuotaUsers != NULL )
  746. {
  747. hRes = pIEnumDiskQuotaUsers->Reset();
  748. if ( SUCCEEDED(hRes))
  749. {
  750. IDiskQuotaUserPtr pIQuotaUser;
  751. DWORD dwNoOfUsers = 0;
  752. HRESULT hRes = S_OK;
  753. hRes = pIEnumDiskQuotaUsers->Next(
  754. 1,
  755. &pIQuotaUser,
  756. &dwNoOfUsers
  757. );
  758. CInstancePtr pInstance;
  759. while ( SUCCEEDED ( hRes ) )
  760. {
  761. if ( dwNoOfUsers == 0 )
  762. {
  763. break;
  764. }
  765. if ( pIQuotaUser != NULL )
  766. {
  767. pInstance.Attach(CreateNewInstance ( pMethodContext ));
  768. hRes = LoadDiskQuotaUserProperties ( pIQuotaUser, pInstance, a_PropertiesReq );
  769. if ( SUCCEEDED ( hRes ) )
  770. {
  771. if(SUCCEEDED(SetKeys( pInstance, a_VolumeName[0], a_PropertiesReq, pIQuotaUser )))
  772. {
  773. hRes = pInstance->Commit ();
  774. }
  775. if (SUCCEEDED(hRes))
  776. {
  777. dwNoOfUsers = 0;
  778. hRes = pIEnumDiskQuotaUsers->Next(
  779. 1,
  780. &pIQuotaUser,
  781. &dwNoOfUsers
  782. );
  783. }
  784. }
  785. else
  786. {
  787. break;
  788. }
  789. }
  790. else
  791. {
  792. // No more Users
  793. break;
  794. }
  795. }
  796. }
  797. }
  798. }
  799. else
  800. {
  801. hRes = WBEM_E_FAILED;
  802. }
  803. }
  804. }
  805. }
  806. return hRes;
  807. }
  808. /*****************************************************************************
  809. *
  810. * FUNCTION : CDiskQuota::LoadDiskQuotaUserProperties
  811. *
  812. * DESCRIPTION : In this method Getting the User properties into a Given Structure
  813. *
  814. *****************************************************************************/
  815. HRESULT CDiskQuota :: LoadDiskQuotaUserProperties (
  816. IDiskQuotaUser* pIQuotaUser,
  817. CInstance* pInstance,
  818. DWORD a_PropertiesReq
  819. )
  820. {
  821. HRESULT hRes = WBEM_S_NO_ERROR;
  822. if ( ( ( a_PropertiesReq & DSKQUOTA_PROP_Status ) == DSKQUOTA_PROP_Status )
  823. || ( ( a_PropertiesReq & DSKQUOTA_PROP_WarningLimit ) == DSKQUOTA_PROP_WarningLimit )
  824. || ( ( a_PropertiesReq & DSKQUOTA_PROP_Limit ) == DSKQUOTA_PROP_Limit )
  825. || ( ( a_PropertiesReq & DSKQUOTA_PROP_DiskSpaceUsed ) == DSKQUOTA_PROP_DiskSpaceUsed ) )
  826. {
  827. DISKQUOTA_USER_INFORMATION t_QuotaInformation;
  828. if ( SUCCEEDED ( pIQuotaUser->GetQuotaInformation ( &t_QuotaInformation, sizeof ( DISKQUOTA_USER_INFORMATION ) ) ) )
  829. {
  830. LONGLONG llLimit = -1;
  831. LONGLONG llWarningLimit = -1;
  832. UINT64 ullDiskSpaceUsed = 0;
  833. DWORD dwStatus;
  834. if ( t_QuotaInformation.QuotaLimit >= 0 )
  835. {
  836. llLimit = t_QuotaInformation.QuotaLimit;
  837. }
  838. if ( t_QuotaInformation.QuotaThreshold >= 0 )
  839. {
  840. llWarningLimit = t_QuotaInformation.QuotaThreshold;
  841. }
  842. ullDiskSpaceUsed = t_QuotaInformation.QuotaUsed;
  843. if ( t_QuotaInformation.QuotaThreshold > -1 )
  844. {
  845. // Since -1 means no Warning limit is set for the user, the deault is the complete Volume Space
  846. if ( t_QuotaInformation.QuotaUsed < t_QuotaInformation.QuotaThreshold )
  847. {
  848. dwStatus = 0;
  849. }
  850. }
  851. else
  852. {
  853. dwStatus = 0;
  854. }
  855. if ( t_QuotaInformation.QuotaThreshold > -1 )
  856. {
  857. // Since -1 means no Warning limit is set for the user, the deault is the complete Volume Space
  858. if ( t_QuotaInformation.QuotaUsed >= t_QuotaInformation.QuotaThreshold )
  859. {
  860. dwStatus = 1;
  861. }
  862. }
  863. if ( t_QuotaInformation.QuotaLimit > -1 )
  864. {
  865. if ( t_QuotaInformation.QuotaUsed >= t_QuotaInformation.QuotaLimit )
  866. {
  867. dwStatus = 2;
  868. }
  869. }
  870. if ( ( a_PropertiesReq & DSKQUOTA_PROP_Status ) == DSKQUOTA_PROP_Status )
  871. {
  872. if ( pInstance->SetDWORD ( IDS_QuotaStatus, dwStatus ) == FALSE )
  873. {
  874. hRes = WBEM_E_FAILED;
  875. }
  876. }
  877. if ( ( a_PropertiesReq & DSKQUOTA_PROP_WarningLimit ) == DSKQUOTA_PROP_WarningLimit )
  878. {
  879. if ( pInstance->SetWBEMINT64 ( IDS_QuotaWarningLimit, (ULONGLONG)llWarningLimit ) == FALSE )
  880. {
  881. hRes = WBEM_E_FAILED;
  882. }
  883. }
  884. if ( ( a_PropertiesReq & DSKQUOTA_PROP_Limit ) == DSKQUOTA_PROP_Limit )
  885. {
  886. if ( pInstance->SetWBEMINT64 ( IDS_QuotaLimit, (ULONGLONG)llLimit ) == FALSE )
  887. {
  888. hRes = WBEM_E_FAILED;
  889. }
  890. }
  891. if ( ( a_PropertiesReq & DSKQUOTA_PROP_DiskSpaceUsed ) == DSKQUOTA_PROP_DiskSpaceUsed )
  892. {
  893. if ( pInstance->SetWBEMINT64 ( IDS_DiskSpaceUsed, ullDiskSpaceUsed ) == FALSE )
  894. {
  895. hRes = WBEM_E_FAILED;
  896. }
  897. }
  898. }
  899. else
  900. {
  901. hRes = WBEM_E_FAILED;
  902. }
  903. }
  904. return hRes;
  905. }
  906. /*****************************************************************************
  907. *
  908. * FUNCTION : CDiskQuota::SetKeys
  909. *
  910. * DESCRIPTION : In this method Setting the User properties in a Given Instance
  911. *
  912. *****************************************************************************/
  913. HRESULT CDiskQuota :: SetKeys(
  914. CInstance* pInstance,
  915. WCHAR w_Drive,
  916. DWORD a_PropertiesReq,
  917. IDiskQuotaUser* pIQuotaUser
  918. )
  919. {
  920. LPWSTR lpLogicalDiskObjectPath;
  921. LPWSTR lpUserObjectPath;
  922. HRESULT hRes = WBEM_S_NO_ERROR;
  923. if ( ( a_PropertiesReq & DSKQUOTA_PROP_LogicalDiskObjectPath ) == DSKQUOTA_PROP_LogicalDiskObjectPath )
  924. {
  925. LPWSTR lpLogicalDiskObjectPath;
  926. WCHAR t_DeviceId[3];
  927. t_DeviceId[0] = w_Drive;
  928. t_DeviceId[1] = L':';
  929. t_DeviceId[2] = L'\0';
  930. m_CommonRoutine.MakeObjectPath ( lpLogicalDiskObjectPath, IDS_LogicalDiskClass, IDS_DeviceID, t_DeviceId );
  931. if ( lpLogicalDiskObjectPath != NULL )
  932. {
  933. try
  934. {
  935. if ( pInstance->SetWCHARSplat ( IDS_LogicalDiskObjectPath, lpLogicalDiskObjectPath ) == FALSE )
  936. {
  937. hRes = WBEM_E_FAILED;
  938. }
  939. }
  940. catch ( ... )
  941. {
  942. delete [] lpLogicalDiskObjectPath;
  943. throw;
  944. }
  945. delete [] lpLogicalDiskObjectPath;
  946. }
  947. }
  948. if (SUCCEEDED(hRes) && (( a_PropertiesReq & DSKQUOTA_PROP_UserObjectPath ) == DSKQUOTA_PROP_UserObjectPath) )
  949. {
  950. // Obtaining the users logon Name
  951. CHString t_LogonName;
  952. WCHAR w_AccountContainer [ MAX_PATH + 1 ];
  953. WCHAR w_DisplayName [ MAX_PATH + 1 ];
  954. LPWSTR t_LogonNamePtr = t_LogonName.GetBuffer(MAX_PATH + 1);
  955. if ( SUCCEEDED ( pIQuotaUser->GetName (
  956. w_AccountContainer,
  957. MAX_PATH,
  958. t_LogonNamePtr,
  959. MAX_PATH,
  960. w_DisplayName,
  961. MAX_PATH
  962. ) ) )
  963. {
  964. t_LogonName.ReleaseBuffer();
  965. // Have seen cases where GetName succeeds, but
  966. // the t_LogonName variable contains an empty string.
  967. if(t_LogonName.GetLength() > 0)
  968. {
  969. CHString t_DomainName;
  970. ExtractUserLogOnName ( t_LogonName, t_DomainName );
  971. // BUILTIN and NT AUTHORITY accounts are represented
  972. // by Win32_Account and its children with the domain
  973. // name being the name of the machine, instead of
  974. // either of these strings. Hence the change below:
  975. CHString chstrNT_AUTHORITY;
  976. CHString chstrBuiltIn;
  977. if(!GetLocalizedNTAuthorityString(chstrNT_AUTHORITY) || !GetLocalizedBuiltInString(chstrBuiltIn))
  978. {
  979. hRes = WBEM_E_FAILED;
  980. }
  981. if(SUCCEEDED(hRes))
  982. {
  983. if(t_DomainName.CompareNoCase(chstrBuiltIn) == 0 ||
  984. t_DomainName.CompareNoCase(chstrNT_AUTHORITY) == 0)
  985. {
  986. t_DomainName = m_ComputerName;
  987. }
  988. m_CommonRoutine.MakeObjectPath ( lpUserObjectPath, IDS_AccountClass, IDS_Domain, t_DomainName );
  989. if ( lpUserObjectPath != NULL )
  990. {
  991. m_CommonRoutine.AddToObjectPath ( lpUserObjectPath, IDS_Name, t_LogonName );
  992. }
  993. if ( lpUserObjectPath != NULL )
  994. {
  995. try
  996. {
  997. if ( pInstance->SetWCHARSplat ( IDS_UserObjectPath, lpUserObjectPath ) == FALSE )
  998. {
  999. hRes = WBEM_E_FAILED;
  1000. }
  1001. }
  1002. catch ( ... )
  1003. {
  1004. delete [] lpUserObjectPath;
  1005. throw;
  1006. }
  1007. delete [] lpUserObjectPath;
  1008. }
  1009. }
  1010. }
  1011. else
  1012. {
  1013. hRes = WBEM_E_FAILED;
  1014. }
  1015. }
  1016. }
  1017. return hRes;
  1018. }
  1019. /*****************************************************************************
  1020. *
  1021. * FUNCTION : CDiskQuota::AddUserOnVolume
  1022. *
  1023. * DESCRIPTION : In this method Adding a user on a volume that supports disk quota
  1024. *
  1025. *****************************************************************************/
  1026. HRESULT CDiskQuota :: AddUserOnVolume (
  1027. const CInstance &Instance,
  1028. LPCWSTR a_VolumePathName,
  1029. LPCWSTR a_UserLogonName
  1030. )
  1031. {
  1032. HRESULT hRes = WBEM_S_NO_ERROR;
  1033. // Get all the properties and check for their validity.
  1034. // all the properties should be provided
  1035. // if the properties like limit and warning limit are not supplied
  1036. // then they should be taken as default values specified on that volume.
  1037. // Also disk space used will not be defined, only the user logon name will be given that will include
  1038. // the domain name so that the logon name would be uniquely defined.
  1039. CHString t_Key1;
  1040. CHString t_Key2;
  1041. if ( Instance.GetCHString ( IDS_LogicalDiskObjectPath , t_Key1 ) == FALSE )
  1042. {
  1043. hRes = WBEM_E_FAILED ;
  1044. }
  1045. if ( SUCCEEDED ( hRes ) )
  1046. {
  1047. if ( Instance.GetCHString ( IDS_UserObjectPath , t_Key2 ) == FALSE )
  1048. {
  1049. hRes = WBEM_E_FAILED ;
  1050. }
  1051. }
  1052. if ( SUCCEEDED ( hRes ) )
  1053. {
  1054. CHString t_VolumePath;
  1055. GetKeyValue ( t_VolumePath,t_Key1 );
  1056. if (!t_VolumePath.IsEmpty())
  1057. {
  1058. hRes = CheckParameters (
  1059. Instance
  1060. );
  1061. }
  1062. else
  1063. {
  1064. hRes = WBEM_E_FAILED;
  1065. }
  1066. if ( SUCCEEDED ( hRes ) )
  1067. {
  1068. CHString t_VolumeName;
  1069. // Get the key values, which will be the object path.
  1070. // Now from the Volume Object path, parse out the volumename
  1071. // from the User object path extract out the user Id.
  1072. // for the volume specified check whether the given volume Supports Disk Quota
  1073. if ( SUCCEEDED(m_CommonRoutine.VolumeSupportsDiskQuota ( a_VolumePathName, t_VolumeName ) ) )
  1074. {
  1075. // Get IDIskQuotaCOntrol for this interface pointer
  1076. IDiskQuotaControlPtr pIQuotaControl;
  1077. if ( SUCCEEDED ( CoCreateInstance(
  1078. CLSID_DiskQuotaControl,
  1079. NULL,
  1080. CLSCTX_INPROC_SERVER,
  1081. IID_IDiskQuotaControl,
  1082. (void **)&pIQuotaControl ) ) )
  1083. {
  1084. // Initialise the pIQuotaControl with the given volume
  1085. hRes = m_CommonRoutine.InitializeInterfacePointer ( pIQuotaControl, a_VolumePathName );
  1086. if ( SUCCEEDED ( hRes ) )
  1087. {
  1088. IDiskQuotaUserPtr pIQuotaUser = NULL;
  1089. hRes = pIQuotaControl->AddUserName(
  1090. a_UserLogonName ,
  1091. DISKQUOTA_USERNAME_RESOLVE_SYNC,
  1092. &pIQuotaUser
  1093. );
  1094. if ( SUCCEEDED ( hRes ) )
  1095. {
  1096. LONGLONG llLimit;
  1097. Instance.GetWBEMINT64 ( IDS_QuotaLimit, llLimit );
  1098. hRes = pIQuotaUser->SetQuotaLimit ( llLimit, TRUE);
  1099. if (SUCCEEDED(hRes))
  1100. {
  1101. // Set the User Warning Limit
  1102. Instance.GetWBEMINT64 ( IDS_QuotaWarningLimit, llLimit );
  1103. hRes = pIQuotaUser->SetQuotaThreshold ( llLimit, TRUE );
  1104. }
  1105. }
  1106. else
  1107. if ( hRes == S_FALSE )
  1108. {
  1109. hRes = WBEM_E_ALREADY_EXISTS ;
  1110. }
  1111. else
  1112. {
  1113. hRes = WBEM_E_INVALID_PARAMETER;
  1114. }
  1115. }
  1116. }
  1117. else
  1118. {
  1119. hRes = WBEM_E_FAILED;
  1120. }
  1121. }
  1122. else
  1123. {
  1124. hRes = WBEM_E_FAILED;
  1125. }
  1126. }
  1127. else
  1128. {
  1129. hRes = WBEM_E_FAILED;
  1130. }
  1131. }
  1132. return hRes;
  1133. }
  1134. /*****************************************************************************
  1135. *
  1136. * FUNCTION : CDiskQuota::UpdateUserQuotaProperties
  1137. *
  1138. * DESCRIPTION : In this method modifying a disk quota properties of a given user
  1139. * on a given volume that supports disk quota
  1140. *
  1141. *****************************************************************************/
  1142. HRESULT CDiskQuota :: UpdateUserQuotaProperties (
  1143. const CInstance &Instance,
  1144. LPCWSTR a_VolumePathName,
  1145. LPCWSTR a_UserLogonName
  1146. )
  1147. {
  1148. HRESULT hRes = WBEM_S_NO_ERROR;
  1149. CHString t_Key1;
  1150. CHString t_Key2;
  1151. if ( Instance.GetCHString ( IDS_LogicalDiskObjectPath , t_Key1 ) == FALSE )
  1152. {
  1153. hRes = WBEM_E_FAILED ;
  1154. }
  1155. if ( SUCCEEDED ( hRes ) )
  1156. {
  1157. if ( Instance.GetCHString ( IDS_UserObjectPath , t_Key2 ) == FALSE )
  1158. {
  1159. hRes = WBEM_E_FAILED ;
  1160. }
  1161. }
  1162. if ( SUCCEEDED ( hRes ) )
  1163. {
  1164. CHString t_VolumePath;
  1165. GetKeyValue ( t_VolumePath ,t_Key1 );
  1166. if (!t_VolumePath.IsEmpty())
  1167. {
  1168. hRes = CheckParameters (
  1169. Instance
  1170. );
  1171. }
  1172. else
  1173. {
  1174. hRes = WBEM_E_FAILED ;
  1175. }
  1176. if ( SUCCEEDED ( hRes ) )
  1177. {
  1178. CHString t_VolumeName;
  1179. if ( SUCCEEDED(m_CommonRoutine.VolumeSupportsDiskQuota ( a_VolumePathName, t_VolumeName )) )
  1180. {
  1181. // Get IDIskQuotaCOntrol for this interface pointer
  1182. IDiskQuotaControlPtr pIQuotaControl;
  1183. if ( SUCCEEDED ( CoCreateInstance(
  1184. CLSID_DiskQuotaControl,
  1185. NULL,
  1186. CLSCTX_INPROC_SERVER,
  1187. IID_IDiskQuotaControl,
  1188. (void **)&pIQuotaControl ) ) )
  1189. {
  1190. // Initialise the pIQuotaControl with the given volume
  1191. hRes = m_CommonRoutine.InitializeInterfacePointer ( pIQuotaControl, a_VolumePathName );
  1192. if ( SUCCEEDED ( hRes ) )
  1193. {
  1194. IDiskQuotaUserPtr pIQuotaUser;
  1195. hRes = pIQuotaControl->FindUserName(
  1196. a_UserLogonName ,
  1197. &pIQuotaUser
  1198. );
  1199. if ( SUCCEEDED ( hRes ) )
  1200. {
  1201. LONGLONG llLimit;
  1202. if (Instance.GetWBEMINT64 ( IDS_QuotaLimit, llLimit ))
  1203. {
  1204. hRes = pIQuotaUser->SetQuotaLimit ( llLimit, TRUE);
  1205. }
  1206. // Set the User Warning Limit
  1207. if (SUCCEEDED(hRes) && Instance.GetWBEMINT64 ( IDS_QuotaWarningLimit, llLimit ))
  1208. {
  1209. hRes = pIQuotaUser->SetQuotaThreshold ( llLimit, TRUE );
  1210. }
  1211. }
  1212. else
  1213. {
  1214. hRes = WBEM_E_NOT_FOUND;
  1215. }
  1216. }
  1217. }
  1218. else
  1219. {
  1220. hRes = WBEM_E_FAILED;
  1221. }
  1222. }
  1223. else
  1224. {
  1225. hRes = WBEM_E_FAILED;
  1226. }
  1227. }
  1228. }
  1229. return hRes;
  1230. }
  1231. /*****************************************************************************
  1232. *
  1233. * FUNCTION : CDiskQuota::CheckParameters
  1234. *
  1235. * DESCRIPTION : In this method verifying the validity of the parameters
  1236. * which are supplied in PutInstance by the user.
  1237. *
  1238. *****************************************************************************/
  1239. HRESULT CDiskQuota :: CheckParameters (
  1240. const CInstance &a_Instance
  1241. )
  1242. {
  1243. // Get all the Properties from the Instance to Verify
  1244. HRESULT hRes = WBEM_S_NO_ERROR ;
  1245. bool t_Exists ;
  1246. VARTYPE t_Type ;
  1247. if ( a_Instance.GetStatus ( IDS_QuotaLimit , t_Exists , t_Type ) )
  1248. {
  1249. if ( t_Exists && ( t_Type == VT_BSTR ) )
  1250. {
  1251. LONGLONG llLimit;
  1252. if ( a_Instance.GetWBEMINT64 ( IDS_QuotaLimit , llLimit ) == FALSE )
  1253. {
  1254. hRes = WBEM_E_INVALID_PARAMETER ;
  1255. }
  1256. }
  1257. else
  1258. if ( t_Exists == false )
  1259. {
  1260. hRes = WBEM_E_INVALID_PARAMETER ;
  1261. }
  1262. }
  1263. if ( a_Instance.GetStatus ( IDS_QuotaWarningLimit , t_Exists , t_Type ) )
  1264. {
  1265. if ( t_Exists && ( t_Type == VT_BSTR ) )
  1266. {
  1267. LONGLONG llLimit;
  1268. if ( a_Instance.GetWBEMINT64 ( IDS_QuotaWarningLimit , llLimit ) == FALSE )
  1269. {
  1270. hRes = WBEM_E_INVALID_PARAMETER ;
  1271. }
  1272. }
  1273. else
  1274. if ( t_Exists == false )
  1275. {
  1276. hRes = WBEM_E_INVALID_PARAMETER ;
  1277. }
  1278. }
  1279. return hRes;
  1280. }
  1281. /*****************************************************************************
  1282. *
  1283. * FUNCTION : CDiskQuota::SetPropertiesReq
  1284. *
  1285. * DESCRIPTION : In this method setting the properties required requested
  1286. * by the user.
  1287. *
  1288. *****************************************************************************/
  1289. void CDiskQuota :: SetPropertiesReq (
  1290. CFrameworkQuery *Query,
  1291. DWORD &a_PropertiesReq
  1292. )
  1293. {
  1294. a_PropertiesReq = 0;
  1295. // being key this property needs to be delivered
  1296. if ( Query->IsPropertyRequired ( IDS_LogicalDiskObjectPath ) )
  1297. {
  1298. a_PropertiesReq |= DSKQUOTA_PROP_LogicalDiskObjectPath;
  1299. }
  1300. if ( Query->IsPropertyRequired ( IDS_UserObjectPath ) )
  1301. {
  1302. a_PropertiesReq |= DSKQUOTA_PROP_UserObjectPath;
  1303. }
  1304. if ( Query->IsPropertyRequired ( IDS_QuotaStatus ) )
  1305. {
  1306. a_PropertiesReq |= DSKQUOTA_PROP_Status;
  1307. }
  1308. if ( Query->IsPropertyRequired ( IDS_QuotaWarningLimit ) )
  1309. {
  1310. a_PropertiesReq |= DSKQUOTA_PROP_WarningLimit;
  1311. }
  1312. if ( Query->IsPropertyRequired ( IDS_QuotaLimit ) )
  1313. {
  1314. a_PropertiesReq |= DSKQUOTA_PROP_Limit;
  1315. }
  1316. if ( Query->IsPropertyRequired ( IDS_DiskSpaceUsed ) )
  1317. {
  1318. a_PropertiesReq |= DSKQUOTA_PROP_DiskSpaceUsed;
  1319. }
  1320. }
  1321. /*****************************************************************************
  1322. *
  1323. * FUNCTION : CDiskQuota::ExtractUserLogOnName
  1324. *
  1325. * DESCRIPTION : Here the user logon name is in the form
  1326. * ComputerName\userlogonname or Domainname\Userlogonname
  1327. * or eg like
  1328. * builtin\adminitrator, where buildin is treated as a
  1329. * domain name by the Win32_UserAccount class. Hence
  1330. * we need to seperate the userlogon name and a domain name,
  1331. * so that the keys will match the Win32_UserAccount class.
  1332. * OR...
  1333. * the name may be of the form: User@domain.something.com
  1334. *
  1335. *****************************************************************************/
  1336. void CDiskQuota :: ExtractUserLogOnName ( CHString &a_UserLogonName, CHString &a_DomainName )
  1337. {
  1338. // Need the string "NT AUTHORITY". However, on non-english
  1339. // builds, this is something else. Hence, get if from the
  1340. // sid.
  1341. PSID pSidNTAuthority = NULL;
  1342. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  1343. CHString cstrAuthorityDomain;
  1344. if (AllocateAndInitializeSid (&sia ,1,SECURITY_LOCAL_SYSTEM_RID,0,0,0,0,0,0,0,&pSidNTAuthority))
  1345. {
  1346. try
  1347. {
  1348. CHString cstrName;
  1349. GetDomainAndNameFromSid(pSidNTAuthority, cstrAuthorityDomain, cstrName);
  1350. }
  1351. catch(...)
  1352. {
  1353. FreeSid(pSidNTAuthority);
  1354. throw;
  1355. }
  1356. FreeSid(pSidNTAuthority);
  1357. }
  1358. int iPos = a_UserLogonName.Find( L'\\');
  1359. // found backwhack: name is of the form domain\user
  1360. if ( iPos != -1 )
  1361. {
  1362. a_DomainName = a_UserLogonName.Left ( iPos );
  1363. // Win32_SystemAccount domain names are always the computer
  1364. // name, never BUILTIN. The string BUILTIN is not localized.
  1365. CHString chstrBuiltIn;
  1366. if (GetLocalizedBuiltInString(chstrBuiltIn) &&
  1367. (a_DomainName.CompareNoCase(chstrBuiltIn) == 0))
  1368. {
  1369. a_DomainName = m_ComputerName;
  1370. }
  1371. if(a_DomainName.CompareNoCase(cstrAuthorityDomain) == 0)
  1372. {
  1373. a_DomainName = m_ComputerName;
  1374. }
  1375. a_UserLogonName = a_UserLogonName.Right ( a_UserLogonName.GetLength() - (iPos + 1) );
  1376. }
  1377. else
  1378. {
  1379. iPos = a_UserLogonName.Find( L'@');
  1380. // found 'at' - name is of the form [email protected]
  1381. if (iPos != -1)
  1382. {
  1383. // start by slicing off everything after the @
  1384. a_DomainName = a_UserLogonName.Right(a_UserLogonName.GetLength() - (iPos + 1));
  1385. // keep the user name portion before we step on iPos
  1386. a_UserLogonName = a_UserLogonName.Left ( iPos );
  1387. // now cut out everything after the first dot
  1388. iPos = a_DomainName.Find(L'.');
  1389. if (iPos != -1)
  1390. a_DomainName = a_DomainName.Left(iPos);
  1391. }
  1392. else
  1393. // else we have a local account
  1394. a_DomainName = m_ComputerName;
  1395. }
  1396. }
  1397. /*****************************************************************************
  1398. *
  1399. * FUNCTION : CDiskQuota::GetKeyValue
  1400. *
  1401. * DESCRIPTION : From the object path we extract the key value
  1402. *
  1403. *****************************************************************************/
  1404. void CDiskQuota::GetKeyValue (
  1405. CHString &a_VolumePath,
  1406. LPCWSTR a_ObjectPath
  1407. )
  1408. {
  1409. ParsedObjectPath *t_ObjPath;
  1410. CObjectPathParser t_PathParser;
  1411. a_VolumePath.Empty();
  1412. if ( ( t_PathParser.Parse( a_ObjectPath, &t_ObjPath ) ) == CObjectPathParser::NoError )
  1413. {
  1414. try
  1415. {
  1416. if (t_ObjPath->m_dwNumKeys == 1)
  1417. {
  1418. if((V_VT(&t_ObjPath->m_paKeys [ 0 ]->m_vValue) == VT_BSTR) &&
  1419. (t_ObjPath->m_paKeys [ 0 ]->m_vValue.bstrVal != NULL)
  1420. )
  1421. {
  1422. a_VolumePath = t_ObjPath->m_paKeys [ 0 ]->m_vValue.bstrVal;
  1423. }
  1424. }
  1425. else if (t_ObjPath->m_dwNumKeys == 2)
  1426. {
  1427. if ((V_VT(&t_ObjPath->m_paKeys [ 0 ]->m_vValue) == VT_BSTR) &&
  1428. (V_VT(&t_ObjPath->m_paKeys [ 1 ]->m_vValue) == VT_BSTR) &&
  1429. (t_ObjPath->m_paKeys [ 0 ]->m_vValue.bstrVal != NULL) &&
  1430. (t_ObjPath->m_paKeys [ 1 ]->m_vValue.bstrVal != NULL)
  1431. )
  1432. {
  1433. a_VolumePath.Format ( L"%s%s%s", t_ObjPath->m_paKeys [ 0 ]->m_vValue.bstrVal,
  1434. L"\\", t_ObjPath->m_paKeys [ 1 ]->m_vValue.bstrVal );
  1435. }
  1436. }
  1437. }
  1438. catch ( ... )
  1439. {
  1440. t_PathParser.Free( t_ObjPath );
  1441. throw;
  1442. }
  1443. t_PathParser.Free( t_ObjPath );
  1444. }
  1445. }
  1446. BOOL CDiskQuota::GetDomainAndNameFromSid(
  1447. PSID pSid,
  1448. CHString& chstrDomain,
  1449. CHString& chstrName)
  1450. {
  1451. BOOL fRet = FALSE;
  1452. // Initialize account name and domain name
  1453. LPTSTR pszAccountName = NULL;
  1454. LPTSTR pszDomainName = NULL;
  1455. DWORD dwAccountNameSize = 0;
  1456. DWORD dwDomainNameSize = 0;
  1457. SID_NAME_USE snuAccountType;
  1458. try
  1459. {
  1460. // This call should fail
  1461. fRet = ::LookupAccountSid(NULL,
  1462. pSid,
  1463. pszAccountName,
  1464. &dwAccountNameSize,
  1465. pszDomainName,
  1466. &dwDomainNameSize,
  1467. &snuAccountType );
  1468. if(fRet && (ERROR_INSUFFICIENT_BUFFER == ::GetLastError()))
  1469. {
  1470. // Allocate buffers
  1471. if ( dwAccountNameSize != 0 )
  1472. {
  1473. pszAccountName = (LPTSTR) malloc( dwAccountNameSize * sizeof(TCHAR));
  1474. if (pszAccountName == NULL)
  1475. {
  1476. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  1477. }
  1478. }
  1479. if ( dwDomainNameSize != 0 )
  1480. {
  1481. pszDomainName = (LPTSTR) malloc( dwDomainNameSize * sizeof(TCHAR));
  1482. if (pszDomainName == NULL)
  1483. {
  1484. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  1485. }
  1486. }
  1487. // Make second call
  1488. fRet = ::LookupAccountSid(
  1489. NULL,
  1490. pSid,
  1491. pszAccountName,
  1492. &dwAccountNameSize,
  1493. pszDomainName,
  1494. &dwDomainNameSize,
  1495. &snuAccountType );
  1496. if ( fRet == TRUE )
  1497. {
  1498. chstrName = pszAccountName;
  1499. chstrDomain = pszDomainName;
  1500. }
  1501. if ( NULL != pszAccountName )
  1502. {
  1503. free ( pszAccountName );
  1504. pszAccountName = NULL;
  1505. }
  1506. if ( NULL != pszDomainName )
  1507. {
  1508. free ( pszDomainName );
  1509. pszDomainName = NULL;
  1510. }
  1511. } // If ERROR_INSUFFICIENT_BUFFER
  1512. } // try
  1513. catch(...)
  1514. {
  1515. if ( NULL != pszAccountName )
  1516. {
  1517. free ( pszAccountName );
  1518. pszAccountName = NULL;
  1519. }
  1520. if ( NULL != pszDomainName )
  1521. {
  1522. free ( pszDomainName );
  1523. pszDomainName = NULL;
  1524. }
  1525. throw;
  1526. }
  1527. return fRet;
  1528. }