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.

716 lines
19 KiB

  1. // OpenF.cpp : Implementation of COpenF
  2. /***************************************************************************************
  3. Copyright information : Microsoft Corp. 1981-1999. All rights reserved
  4. File Name : OpenF.cpp
  5. Created By : A.V. Kiran Kumar
  6. Date of Creation (dd/mm/yy) : 13/02/01
  7. Version Number : 0.1
  8. Brief Description : This file implements COpenF. This file is intended to
  9. have the functionality for getting the list of open files.
  10. ***************************************************************************************/
  11. #include "stdafx.h"
  12. #include "OpenFiles.h"
  13. #include "OpenF.h"
  14. /////////////////////////////////////////////////////////////////////////////
  15. // COpenF
  16. // ***************************************************************************
  17. //
  18. // Name : getOpenFiles
  19. //
  20. // Synopsis : This function gets the list of open files.
  21. //
  22. // Parameters : VARIANT*(out, retval) pOpenFiles - List of open files
  23. //
  24. // Return Type : DWORD
  25. //
  26. //
  27. // ***************************************************************************
  28. STDMETHODIMP COpenF::getOpenFiles(VARIANT *pOpenFiles)
  29. {
  30. DWORD dwEntriesRead = 0;// Receives the total number of openfiles
  31. DWORD dwTotalEntries = 0;//Receives the total number of entries read
  32. DWORD dwResumeHandle = 0;//Contains a resume handle which is used to
  33. //continue an existing file search.
  34. LPFILE_INFO_3 pFileInfo3_1 = NULL;// LPFILE_INFO_3 structure contains the
  35. // pertinent information about files.
  36. DWORD dwError = 0;
  37. DWORD dwRetval = S_OK;
  38. DWORD dwCount = 0;//Count which indicates the number of openfiles
  39. LPFILE_INFO_3 dummyPtr = NULL;
  40. //Get information about some or all open files on a server
  41. dwError = NetFileEnum( NULL,
  42. NULL,
  43. NULL,
  44. FILE_INFO_3,
  45. (LPBYTE*)&pFileInfo3_1,
  46. MAX_PREFERRED_LENGTH,
  47. &dwEntriesRead,
  48. &dwTotalEntries,
  49. NULL );
  50. if(dwError == ERROR_ACCESS_DENIED || dwError == ERROR_NOT_ENOUGH_MEMORY)
  51. return dwError; // The user does not have access to the requested information.
  52. //Get the count of OpenFiles on Macinthosh machine
  53. DWORD dwMacCount = 0;
  54. if ( dwError = GetMacOpenFileCount(&dwMacCount) )
  55. dwRetval = dwError;
  56. //Get the count of OpenFiles on Netware machine
  57. DWORD dwNwCount = 0;
  58. if ( dwError = GetNwOpenFileCount(&dwNwCount) )
  59. dwRetval = dwError;
  60. //Fill the safearray bounds structure with the dimensions of the safe array. The lower bound
  61. //is 0 and the number of rows is dwTotalEntries and number of columns is 3
  62. pSab[0].lLbound = 0;
  63. pSab[0].cElements = dwTotalEntries + dwMacCount + dwNwCount;
  64. pSab[1].lLbound = 0;
  65. pSab[1].cElements = 3;
  66. //Create the safe array descriptor, allocate and initialize the data for the array
  67. pSa = SafeArrayCreate( VT_VARIANT, 2, pSab );
  68. if(pSa == NULL)
  69. return ERROR_NOT_ENOUGH_MEMORY;
  70. //Enumerate all the openfiles
  71. do
  72. {
  73. //Some more files are to be enumerated get them by calling NetFileEnum again
  74. dwError = NetFileEnum( NULL, NULL, NULL, FILE_INFO_3,
  75. (LPBYTE*)&pFileInfo3_1,
  76. MAX_PREFERRED_LENGTH,
  77. &dwEntriesRead,
  78. &dwTotalEntries,
  79. (unsigned long*)&dwResumeHandle );
  80. if(dwError == ERROR_ACCESS_DENIED || dwError == ERROR_NOT_ENOUGH_MEMORY)
  81. return dwError;
  82. dummyPtr = pFileInfo3_1;
  83. // Get the open files once NetFileEnum is successully called
  84. if( dwError == NERR_Success || dwError == ERROR_MORE_DATA )
  85. {
  86. for ( DWORD dwFile = 0; dwFile < dwEntriesRead; dwFile++, pFileInfo3_1++ )
  87. {
  88. BSTR userName;
  89. BSTR openMode;
  90. BSTR pathName;
  91. VARIANT vuserName;
  92. VARIANT vopenMode;
  93. VARIANT vpathName;
  94. // Accessed By
  95. if(lstrlen(pFileInfo3_1->fi3_username))
  96. userName = (BSTR)pFileInfo3_1->fi3_username;
  97. else
  98. userName = L"NOT_AVAILABLE"; //User name is not available
  99. // Checks for open file mode
  100. const DWORD READWRITE = PERM_FILE_READ | PERM_FILE_WRITE;
  101. const DWORD READCREATE = PERM_FILE_READ | PERM_FILE_CREATE;
  102. const DWORD WRITECREATE = PERM_FILE_WRITE | PERM_FILE_CREATE;
  103. switch(pFileInfo3_1->fi3_permissions)
  104. {
  105. case PERM_FILE_READ:
  106. openMode = L"READ";
  107. break;
  108. case PERM_FILE_WRITE:
  109. openMode = L"WRITE";
  110. break;
  111. case PERM_FILE_CREATE:
  112. openMode = L"CREATE";
  113. break;
  114. case READWRITE:
  115. openMode = L"READ+WRITE";
  116. break;
  117. case READCREATE:
  118. openMode = L"READ+CREATE";
  119. break;
  120. case WRITECREATE:
  121. openMode = L"WRITE+CREATE";
  122. break;
  123. default:
  124. openMode = L"NOACCESS";
  125. }
  126. //Get the filename from the structure
  127. pathName = (BSTR)pFileInfo3_1->fi3_pathname;
  128. //Initialize the row index and column index at which filename is to be stored in the Safearray
  129. long index[2] = {dwCount, 0};
  130. VariantInit( &vpathName ); //Initialize the variant
  131. vpathName.vt = VT_BSTR; //Data type to be stored is BSTR
  132. vpathName.bstrVal = SysAllocString(pathName);
  133. //Store the filename in Safearray
  134. HRESULT hr;
  135. hr = SafeArrayPutElement( pSa, index, &vpathName );
  136. if( FAILED(hr) )
  137. return hr;
  138. //Store username in the second column
  139. index[ 1 ] = 1;
  140. VariantInit( &vuserName );
  141. vuserName.vt = VT_BSTR;
  142. vuserName.bstrVal = SysAllocString(userName);
  143. //Store the username in the safearray
  144. hr = SafeArrayPutElement( pSa, index, &vuserName );
  145. if( FAILED(hr) )
  146. return hr;
  147. //Store OpenMode in the third column
  148. index[ 1 ] = 2;
  149. VariantInit( &vopenMode );
  150. vopenMode.vt = VT_BSTR;
  151. vopenMode.bstrVal = SysAllocString(openMode);
  152. //Store the OpenMode in the safearray
  153. hr = SafeArrayPutElement( pSa, index, &vopenMode );
  154. if( FAILED(hr) )
  155. return hr;
  156. //Clear all the variants that are initilized
  157. VariantClear(&vuserName);
  158. VariantClear(&vopenMode);
  159. VariantClear(&vpathName);
  160. dwCount++;
  161. }// End for loop
  162. }
  163. // Free the block allocated for retrieving the OpenFile info
  164. if( dummyPtr !=NULL)
  165. {
  166. NetApiBufferFree( dummyPtr );
  167. pFileInfo3_1 = NULL;
  168. }
  169. } while ( dwError == ERROR_MORE_DATA );
  170. //Get the list of Open Files on Macinthosh OS
  171. if( dwMacCount > 0 )
  172. {
  173. if ( dwError = GetMacOpenF(pSa, dwTotalEntries ) )
  174. dwRetval = dwError;
  175. }
  176. //Get the list of Open Files on Netware OS
  177. if( dwNwCount > 0 )
  178. {
  179. if ( dwError = GetNwOpenF(pSa, dwTotalEntries + dwMacCount ) )
  180. dwRetval = dwError;
  181. }
  182. //Return the safe array to the calling function
  183. VariantInit( pOpenFiles );
  184. pOpenFiles->vt = VT_VARIANT | VT_ARRAY;
  185. pOpenFiles->parray = pSa;
  186. return dwRetval;
  187. }
  188. // ***************************************************************************
  189. //
  190. // Name : GetMacOpenF
  191. //
  192. // Synopsis : This function gets the list of open files on Machinthosh OS.
  193. //
  194. // Parameters : SAFEARRAY* (out, retval) - List of open files
  195. // : DWORD dwIndex Safe array Index
  196. //
  197. // Return Type : DWORD
  198. //
  199. //
  200. // ***************************************************************************
  201. DWORD COpenF::GetMacOpenF(SAFEARRAY *pSa, DWORD dwIndex)
  202. {
  203. DWORD dwEntriesRead = 0;// Receives the count of elements
  204. DWORD dwTotalEntries = 0;//Receives the total number of entries
  205. DWORD hEnumHandle = 0;//Contains a resume handle which is used to
  206. //continue an existing file search.
  207. AFP_FILE_INFO* pfileinfo = NULL; // Structure contains the
  208. // pertinent information about files
  209. HRESULT hr = S_OK;
  210. NET_API_STATUS retval = NERR_Success;
  211. DWORD ulSFMServerConnection = 0;
  212. DWORD retval_connect = 0;
  213. LPWSTR ServerName = NULL;
  214. retval_connect = AfpAdminConnect(
  215. ServerName,
  216. &ulSFMServerConnection );
  217. if(retval_connect)
  218. return retval_connect;
  219. DWORD dwCount = dwIndex;
  220. DWORD retval_FileEnum;
  221. //Enumerate all the openfiles
  222. do
  223. {
  224. //Some more files are to be enumerated get them by calling AfpAdminFileEnum again
  225. retval_FileEnum = AfpAdminFileEnum(
  226. ulSFMServerConnection,
  227. (PBYTE*)&pfileinfo,
  228. (DWORD)-1L,
  229. &dwEntriesRead,
  230. &dwTotalEntries,
  231. &hEnumHandle );
  232. if( retval_FileEnum == ERROR_ACCESS_DENIED || retval_FileEnum == ERROR_NOT_ENOUGH_MEMORY )
  233. return retval_FileEnum; // The user does not have access to the requested information.
  234. AFP_FILE_INFO* dummyPtr = pfileinfo;
  235. // Get the open files once NetFileEnum is successully called
  236. if( retval_FileEnum == NERR_Success || retval_FileEnum == ERROR_MORE_DATA )
  237. {
  238. for ( DWORD dwFile = 0; dwFile < dwEntriesRead; dwFile++, pfileinfo++ )
  239. {
  240. BSTR userName;
  241. BSTR openMode;
  242. BSTR pathName;
  243. VARIANT vuserName;
  244. VARIANT vopenMode;
  245. VARIANT vpathName;
  246. // Accessed By
  247. if(lstrlen(pfileinfo->afpfile_username))
  248. userName = (BSTR)pfileinfo->afpfile_username;
  249. else
  250. userName = L"NOT_AVAILABLE"; //User name is not available
  251. // Checks for open file mode
  252. const DWORD READWRITE = PERM_FILE_READ | PERM_FILE_WRITE;
  253. const DWORD READCREATE = PERM_FILE_READ | PERM_FILE_CREATE;
  254. const DWORD WRITECREATE = PERM_FILE_WRITE | PERM_FILE_CREATE;
  255. switch(pfileinfo->afpfile_open_mode)
  256. {
  257. case PERM_FILE_READ:
  258. openMode = L"READ";
  259. break;
  260. case PERM_FILE_WRITE:
  261. openMode = L"WRITE";
  262. break;
  263. case PERM_FILE_CREATE:
  264. openMode = L"CREATE";
  265. break;
  266. case READWRITE:
  267. openMode = L"READ+WRITE";
  268. break;
  269. case READCREATE:
  270. openMode = L"READ+CREATE";
  271. break;
  272. case WRITECREATE:
  273. openMode = L"WRITE+CREATE";
  274. break;
  275. default:
  276. openMode = L"NOACCESS";
  277. }
  278. //Get the filename from the structure
  279. pathName = (BSTR)pfileinfo->afpfile_path;
  280. //Initialize the row index and column index filename to be stored in the Safearray
  281. long index[2] = {dwCount, 0};
  282. VariantInit( &vpathName ); //Initialize the variant
  283. vpathName.vt = VT_BSTR; //Data type to be stored is BSTR
  284. vpathName.bstrVal = SysAllocString(pathName);
  285. //Store the filename in Safearray
  286. hr = SafeArrayPutElement( pSa, index, &vpathName );
  287. if( FAILED(hr) )
  288. return hr;
  289. //Store filename in the second column
  290. index[ 1 ] = 1;
  291. VariantInit( &vuserName );
  292. vuserName.vt = VT_BSTR;
  293. vuserName.bstrVal = SysAllocString(userName);
  294. //Store the username in the safearray
  295. hr = SafeArrayPutElement( pSa, index, &vuserName );
  296. if( FAILED(hr) )
  297. return hr;
  298. //Store OpenMode in the third column
  299. index[ 1 ] = 2;
  300. VariantInit( &vopenMode );
  301. vopenMode.vt = VT_BSTR;
  302. vopenMode.bstrVal = SysAllocString(openMode);
  303. //Store the OpenMode in the safearray
  304. hr = SafeArrayPutElement( pSa, index, &vopenMode );
  305. if( FAILED(hr) )
  306. return hr;
  307. //Clear all the variants that are initilized
  308. VariantClear(&vuserName);
  309. VariantClear(&vopenMode);
  310. VariantClear(&vpathName);
  311. dwCount++;
  312. }// End for loop
  313. }
  314. // Free the block allocated for retrieving the OpenFile info
  315. if( dummyPtr !=NULL)
  316. {
  317. NetApiBufferFree( dummyPtr );
  318. pfileinfo = NULL;
  319. }
  320. } while ( retval_FileEnum == ERROR_MORE_DATA );
  321. return 0;
  322. }
  323. // ***************************************************************************
  324. //
  325. // Name : GetMacOpenFileCount
  326. //
  327. // Synopsis : This function gets the count of open files on Machinthosh OS.
  328. //
  329. // Parameters : DWORD dwIndex Safe array Index
  330. //
  331. // Return Type : DWORD
  332. //
  333. //
  334. // ***************************************************************************
  335. DWORD COpenF::GetMacOpenFileCount(LPDWORD lpdwCount)
  336. {
  337. DWORD dwEntriesRead = 0;// Receives the count of elements
  338. DWORD dwTotalEntries = 0;//Receives the total number of entries
  339. AFP_FILE_INFO* pfileinfo = NULL; // Structure contains the
  340. // identification number and other
  341. // pertinent information about files
  342. HRESULT hr = S_OK;
  343. NET_API_STATUS retval = NERR_Success;
  344. DWORD ulSFMServerConnection = 0;
  345. hMacModule = ::LoadLibrary (_TEXT("sfmapi.dll"));
  346. if(hMacModule==NULL)
  347. return ERROR_DLL_INIT_FAILED;
  348. AfpAdminConnect = (CONNECTPROC)::GetProcAddress (hMacModule,"AfpAdminConnect");
  349. if(AfpAdminConnect==NULL)
  350. return ERROR_DLL_INIT_FAILED;
  351. DWORD retval_connect = AfpAdminConnect(
  352. NULL,
  353. &ulSFMServerConnection );
  354. if(retval_connect!=0)
  355. return retval_connect;
  356. AfpAdminFileEnum = (FILEENUMPROCMAC)::GetProcAddress (hMacModule,"AfpAdminFileEnum");
  357. if(AfpAdminFileEnum==NULL)
  358. return ERROR_DLL_INIT_FAILED;
  359. //Get information about some or all open files on a server
  360. DWORD retval_FileEnum = AfpAdminFileEnum(
  361. ulSFMServerConnection,
  362. (PBYTE*)&pfileinfo,
  363. (DWORD)-1L,
  364. &dwEntriesRead,
  365. &dwTotalEntries,
  366. NULL );
  367. if( retval_FileEnum == ERROR_ACCESS_DENIED || retval_FileEnum == ERROR_NOT_ENOUGH_MEMORY )
  368. return retval_FileEnum; // The user does not have access to the requested information.
  369. *lpdwCount = dwTotalEntries;
  370. if( pfileinfo !=NULL)
  371. {
  372. NetApiBufferFree( pfileinfo );
  373. pfileinfo = NULL;
  374. }
  375. return 0;
  376. }
  377. // ***************************************************************************
  378. //
  379. // Name : GetNwOpenFileCount
  380. //
  381. // Synopsis : This function gets the count of open files on Netware OS.
  382. //
  383. // Parameters : DWORD dwIndex Safe array Index
  384. //
  385. // Return Type : DWORD
  386. //
  387. //
  388. // ***************************************************************************
  389. DWORD COpenF::GetNwOpenFileCount(LPDWORD lpdwCount)
  390. {
  391. DWORD dwEntriesRead = 0;// Receives the count of elements
  392. FPNWFILEINFO* pfileinfo = NULL; // FPNWFILEINFO structure contains the
  393. // identification number and other
  394. // pertinent information about files,
  395. // devices, and pipes.
  396. NET_API_STATUS retval = NERR_Success;
  397. DWORD dwError = 0;//Contains return value for "NetFileEnum" function
  398. DWORD dwCount = 0;//Count which indicates the number of openfiles
  399. *lpdwCount = 0; //Initialize the count to zero
  400. hNwModule = ::LoadLibrary (_TEXT("FPNWCLNT.DLL"));
  401. if(hNwModule==NULL)
  402. return ERROR_DLL_INIT_FAILED;
  403. FpnwFileEnum = (FILEENUMPROC)::GetProcAddress (hNwModule,"FpnwFileEnum");
  404. if(FpnwFileEnum==NULL)
  405. return ERROR_DLL_INIT_FAILED;
  406. do
  407. {
  408. //Get information about some or all open files on a server
  409. retval = FpnwFileEnum(
  410. NULL,
  411. 1,
  412. NULL,
  413. (PBYTE*)&pfileinfo,
  414. &dwEntriesRead,
  415. NULL );
  416. if( retval == ERROR_ACCESS_DENIED || retval == ERROR_NOT_ENOUGH_MEMORY )
  417. return retval; // The user does not have access to the requested information.
  418. *lpdwCount += dwEntriesRead;
  419. }while( retval == ERROR_MORE_DATA );
  420. if( pfileinfo !=NULL)
  421. {
  422. NetApiBufferFree( pfileinfo );
  423. pfileinfo = NULL;
  424. }
  425. return 0;
  426. }
  427. // ***************************************************************************
  428. //
  429. // Name : GetNwOpenF
  430. //
  431. // Synopsis : This function gets the list of open files on Netware OS.
  432. //
  433. // Parameters : SAFEARRAY* (out, retval) - List of open files
  434. // : DWORD dwIndex Safe array Index
  435. //
  436. // Return Type : DWORD
  437. //
  438. //
  439. // ***************************************************************************
  440. DWORD COpenF::GetNwOpenF(SAFEARRAY *pSa, DWORD dwIndex)
  441. {
  442. DWORD dwEntriesRead = 0;// Receives the count of elements
  443. DWORD hEnumHandle = 0;//Contains a resume handle which is used to
  444. //continue an existing file search.
  445. FPNWFILEINFO* pfileinfo = NULL; // FPNWFILEINFO structure contains the
  446. // pertinent information about files.
  447. HRESULT hr = S_OK;
  448. NET_API_STATUS retval = NERR_Success;
  449. DWORD dwCount = dwIndex;
  450. //Enumerate all the openfiles
  451. do
  452. {
  453. //Some more files are to be enumerated get them by calling NetFileEnum again
  454. retval = FpnwFileEnum(
  455. NULL,
  456. 1,
  457. NULL,
  458. (PBYTE*)&pfileinfo,
  459. &dwEntriesRead,
  460. NULL );
  461. if( retval == ERROR_ACCESS_DENIED || retval == ERROR_NOT_ENOUGH_MEMORY )
  462. return retval; // The user does not have access to the requested information.
  463. FPNWFILEINFO* dummyPtr = pfileinfo;
  464. // Get the open files once NetFileEnum is successully called
  465. if( retval == NERR_Success || retval == ERROR_MORE_DATA )
  466. {
  467. for ( DWORD dwFile = 0; dwFile < dwEntriesRead; dwFile++, pfileinfo++ )
  468. {
  469. BSTR userName;
  470. BSTR openMode;
  471. BSTR pathName;
  472. VARIANT vuserName;
  473. VARIANT vopenMode;
  474. VARIANT vpathName;
  475. // Accessed By
  476. if(lstrlen(pfileinfo->lpUserName))
  477. userName = (BSTR)pfileinfo->lpUserName;
  478. else
  479. userName = L"NOT_AVAILABLE"; //User name is not available
  480. // Checks for open file mode
  481. const DWORD READWRITE = FPNWFILE_PERM_READ | FPNWFILE_PERM_WRITE;
  482. const DWORD READCREATE = FPNWFILE_PERM_READ | FPNWFILE_PERM_CREATE;
  483. const DWORD WRITECREATE = FPNWFILE_PERM_WRITE | FPNWFILE_PERM_CREATE;
  484. switch(pfileinfo->dwPermissions)
  485. {
  486. case FPNWFILE_PERM_READ:
  487. openMode = L"READ";
  488. break;
  489. case FPNWFILE_PERM_WRITE:
  490. openMode = L"WRITE";
  491. break;
  492. case FPNWFILE_PERM_CREATE:
  493. openMode = L"CREATE";
  494. break;
  495. case READWRITE:
  496. openMode = L"READ+WRITE";
  497. break;
  498. case READCREATE:
  499. openMode = L"READ+CREATE";
  500. break;
  501. case WRITECREATE:
  502. openMode = L"WRITE+CREATE";
  503. break;
  504. default:
  505. openMode = L"NOACCESS";
  506. }
  507. //Get the filename from the structure
  508. pathName = (BSTR)pfileinfo->lpPathName;
  509. //Initialize the row index and column index filename to be stored in the Safearray
  510. long index[2] = {dwCount, 0};
  511. VariantInit( &vpathName ); //Initialize the variant
  512. vpathName.vt = VT_BSTR; //Data type to be stored is BSTR
  513. vpathName.bstrVal = SysAllocString(pathName);
  514. //Store the filename in Safearray
  515. hr = SafeArrayPutElement( pSa, index, &vpathName );
  516. if( FAILED(hr) )
  517. return hr;
  518. //Store filename in the second column
  519. index[ 1 ] = 1;
  520. VariantInit( &vuserName );
  521. vuserName.vt = VT_BSTR;
  522. vuserName.bstrVal = SysAllocString(userName);
  523. //Store the username in the safearray
  524. hr = SafeArrayPutElement( pSa, index, &vuserName );
  525. if( FAILED(hr) )
  526. return hr;
  527. //Store OpenMode in the third column
  528. index[ 1 ] = 2;
  529. VariantInit( &vopenMode );
  530. vopenMode.vt = VT_BSTR;
  531. vopenMode.bstrVal = SysAllocString(openMode);
  532. //Store the OpenMode in the safearray
  533. hr = SafeArrayPutElement( pSa, index, &vopenMode );
  534. if( FAILED(hr) )
  535. return hr;
  536. //Clear all the variants that are initilized
  537. VariantClear(&vuserName);
  538. VariantClear(&vopenMode);
  539. VariantClear(&vpathName);
  540. dwCount++;
  541. }// End for loop
  542. }
  543. // Free the block allocated for retrieving the OpenFile info
  544. if( dummyPtr !=NULL)
  545. {
  546. NetApiBufferFree( dummyPtr );
  547. pfileinfo = NULL;
  548. }
  549. }while( retval == ERROR_MORE_DATA );
  550. return 0;
  551. }