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.

1591 lines
47 KiB

  1. // sfm.cpp : SFM shares, sessions and open resources
  2. /*
  3. History:
  4. 1/24/97 JonN
  5. I have looked over the enumeration API usage in SFMMGR.CPL:
  6. The Users dialog uses AfpAdminSessionEnum for the upper LB and
  7. AfpAdminConnectionEnum for the lower LB.
  8. The Volumesdialog uses AfpAdminVolumeEnum for the upper LB and
  9. AfpAdminConnectionEnum for the lower LB.
  10. The Files dialog uses AfpAdminFileEnum for single LB.
  11. 8/20/97 EricDav
  12. Added code to support the SFM property sheet. The following
  13. functions were added:
  14. DisplaySfmProperties()
  15. CleanupSfmProperties();
  16. UserHasAccess();
  17. SetSfmPropSheet();
  18. The SFM prop sheet is a modeless prop sheet and is global for
  19. a machine running SFM.
  20. 4/16/98 EricDav
  21. Added code to properly check if SFM is installed and running.
  22. Able to start the service as necessary.
  23. */
  24. #include "stdafx.h"
  25. #include "cmponent.h"
  26. #include "safetemp.h"
  27. #include "FileSvc.h"
  28. #include "DynamLnk.h" // DynamicDLL
  29. #include "ShrPgSFM.h" // Share Properties Page
  30. #include "compdata.h"
  31. #include "progress.h" // service control progress dialog
  32. #include "dataobj.h"
  33. #include "sfm.h"
  34. #include "sfmutil.h" // Support for the SFM config prop sheet
  35. #define DONT_WANT_SHELLDEBUG
  36. #include "shlobjp.h" // LPITEMIDLIST
  37. #include "wraps.h" // Wrap_ILCreateFromPath
  38. #include "macros.h"
  39. USE_HANDLE_MACROS("FILEMGMT(sfm.cpp)")
  40. #ifdef _DEBUG
  41. #define new DEBUG_NEW
  42. #undef THIS_FILE
  43. static char THIS_FILE[] = __FILE__;
  44. #endif
  45. DynamicDLL g_SfmDLL( _T("SFMAPI.DLL"), g_apchFunctionNames );
  46. SfmFileServiceProvider::SfmFileServiceProvider( CFileMgmtComponentData* pFileMgmtData )
  47. : m_ulSFMServerConnection( NULL ),
  48. m_pSfmPropSheet(NULL),
  49. // not subject to localization
  50. FileServiceProvider( pFileMgmtData )
  51. {
  52. VERIFY( m_strTransportSFM.LoadString( IDS_TRANSPORT_SFM ) );
  53. }
  54. SfmFileServiceProvider::~SfmFileServiceProvider()
  55. {
  56. CleanupSfmProperties();
  57. SFMDisconnect();
  58. }
  59. typedef DWORD (*CONNECTPROC) (LPWSTR,PAFP_SERVER_HANDLE);
  60. typedef DWORD (*DISCONNECTPROC) (AFP_SERVER_HANDLE);
  61. BOOL SfmFileServiceProvider::SFMConnect(LPCWSTR pwchServerName, BOOL fDisplayError)
  62. {
  63. if (NULL != m_ulSFMServerConnection)
  64. {
  65. if (NULL == pwchServerName)
  66. {
  67. if (0 == m_ulSFMServerConnectionMachine.GetLength() )
  68. return TRUE; // already connected to local machine
  69. }
  70. else
  71. {
  72. if (0 == lstrcmpi(m_ulSFMServerConnectionMachine, pwchServerName))
  73. return TRUE; // already connected to this machine
  74. }
  75. SFMDisconnect();
  76. ASSERT (NULL == m_ulSFMServerConnection);
  77. }
  78. if ( !g_SfmDLL.LoadFunctionPointers() )
  79. return S_OK;
  80. DWORD retval = ((CONNECTPROC)g_SfmDLL[AFP_CONNECT])(
  81. const_cast<LPWSTR>(pwchServerName),
  82. &m_ulSFMServerConnection );
  83. if ( 0 != retval )
  84. {
  85. ASSERT( NULL == m_ulSFMServerConnection );
  86. m_ulSFMServerConnection = NULL;
  87. m_ulSFMServerConnectionMachine = _T("");
  88. if (fDisplayError)
  89. {
  90. (void) DoErrMsgBox(GetActiveWindow(), MB_OK | MB_ICONSTOP, retval, IDS_POPUP_SFM_CONNECT, pwchServerName );
  91. }
  92. return FALSE;
  93. } else
  94. {
  95. m_ulSFMServerConnectionMachine =
  96. (pwchServerName ? pwchServerName : _T(""));
  97. }
  98. return TRUE;
  99. }
  100. void SfmFileServiceProvider::SFMDisconnect()
  101. {
  102. if (NULL == m_ulSFMServerConnection)
  103. return;
  104. if ( !g_SfmDLL.LoadFunctionPointers() )
  105. return;
  106. ((DISCONNECTPROC)g_SfmDLL[AFP_DISCONNECT])(
  107. m_ulSFMServerConnection );
  108. m_ulSFMServerConnection = NULL;
  109. }
  110. /*
  111. DWORD
  112. AfpAdminVolumeEnum(
  113. IN AFP_SERVER_HANDLE hAfpServer,
  114. OUT LPBYTE * lpbBuffer,
  115. IN DWORD dwPrefMaxLen,
  116. OUT LPDWORD lpdwEntriesRead,
  117. OUT LPDWORD lpdwTotalEntries,
  118. IN LPDWORD lpdwResumeHandle
  119. );
  120. */
  121. typedef DWORD (*VOLUMEENUMPROC) (AFP_SERVER_HANDLE,LPBYTE*,DWORD,LPDWORD,LPDWORD,LPDWORD);
  122. HRESULT SfmFileServiceProvider::PopulateShares(
  123. IResultData* pResultData,
  124. CFileMgmtCookie* pcookie)
  125. {
  126. TEST_NONNULL_PTR_PARAM(pcookie);
  127. if ( !g_SfmDLL.LoadFunctionPointers() )
  128. return S_OK;
  129. if ( !SFMConnect(pcookie->QueryTargetServer()) )
  130. return S_OK;
  131. AFP_VOLUME_INFO* pvolumeinfo = NULL;
  132. DWORD dwEntriesRead = 0;
  133. DWORD dwTotalEntries = 0;
  134. DWORD hEnumHandle = 0;
  135. HRESULT hr = S_OK;
  136. NET_API_STATUS retval = NERR_Success;
  137. do {
  138. retval = ((VOLUMEENUMPROC)g_SfmDLL[AFP_VOLUME_ENUM])(
  139. m_ulSFMServerConnection,
  140. (PBYTE*)&pvolumeinfo,
  141. (DWORD)-1L,
  142. &dwEntriesRead,
  143. &dwTotalEntries,
  144. &hEnumHandle );
  145. if (NERR_Success == retval)
  146. {
  147. hr = AddSFMShareItems( pResultData, pcookie, pvolumeinfo, dwEntriesRead );
  148. pvolumeinfo = NULL;
  149. break;
  150. } else if (ERROR_MORE_DATA == retval) {
  151. ASSERT( NULL != hEnumHandle );
  152. hr = AddSFMShareItems( pResultData, pcookie, pvolumeinfo, dwEntriesRead );
  153. pvolumeinfo = NULL;
  154. continue;
  155. } else if (RPC_S_SERVER_UNAVAILABLE == retval && 0 == hEnumHandle) {
  156. // SFM just isn't installed, don't worry about it
  157. retval = NERR_Success;
  158. break;
  159. } else {
  160. if (ERROR_ACCESS_DENIED == retval)
  161. {
  162. (void) DoErrMsgBox(
  163. GetActiveWindow(),
  164. MB_OK | MB_ICONSTOP,
  165. 0,
  166. IDS_POPUP_SFM_SHARES_NOACCESS
  167. );
  168. } else
  169. {
  170. (void) DoErrMsgBox(
  171. GetActiveWindow(),
  172. MB_OK | MB_ICONSTOP,
  173. retval,
  174. IDS_POPUP_SFM_SHARES
  175. );
  176. }
  177. break;
  178. }
  179. } while (S_OK == hr);
  180. return HRESULT_FROM_WIN32(retval);
  181. }
  182. /*
  183. typedef enum _COLNUM_SHARES {
  184. COLNUM_SHARES_SHARED_FOLDER = 0,
  185. COLNUM_SHARES_SHARED_PATH,
  186. COLNUM_SHARES_TRANSPORT,
  187. COLNUM_SHARES_NUM_SESSIONS,
  188. COLNUM_SHARES_COMMENT
  189. } COLNUM_SHARES;
  190. typedef struct _AFP_VOLUME_INFO
  191. {
  192. LPWSTR afpvol_name; // Name of the volume max.
  193. DWORD afpvol_id; // id of this volume. generated by sever
  194. LPWSTR afpvol_password; // Volume password, max. AFP_VOLPASS_LEN
  195. DWORD afpvol_max_uses; // Max opens allowed
  196. DWORD afpvol_props_mask; // Mask of volume properties
  197. DWORD afpvol_curr_uses; // Number of curr open connections.
  198. LPWSTR afpvol_path; // The actual path
  199. // Ignored for VolumeSetInfo
  200. } AFP_VOLUME_INFO, *PAFP_VOLUME_INFO;
  201. */
  202. HRESULT SfmFileServiceProvider::AddSFMShareItems(
  203. IResultData* pResultData,
  204. CFileMgmtCookie* pParentCookie,
  205. PVOID pinfo,
  206. DWORD nItems)
  207. {
  208. TEST_NONNULL_PTR_PARAM(pParentCookie);
  209. TEST_NONNULL_PTR_PARAM(pinfo);
  210. if (0 >= nItems)
  211. return S_OK;
  212. RESULTDATAITEM tRDItem;
  213. ::ZeroMemory( &tRDItem, sizeof(tRDItem) );
  214. // CODEWORK should use MMC_ICON_CALLBACK
  215. tRDItem.nCol = COLNUM_SHARES_SHARED_FOLDER;
  216. tRDItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  217. tRDItem.str = MMC_CALLBACK;
  218. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)pinfo;
  219. DWORD nItemsToAdd = 0;
  220. for (DWORD i = 0; i < nItems; i++ )
  221. {
  222. if (!IsInvalidSharename(pvolumeinfo[i].afpvol_name))
  223. nItemsToAdd++;
  224. }
  225. CSfmShareCookie* pcookiearray = new CSfmShareCookie[nItemsToAdd];
  226. CSfmCookieBlock* pCookieBlock = new CSfmCookieBlock(
  227. pcookiearray,nItemsToAdd,pParentCookie->QueryTargetServer(),pinfo );
  228. pParentCookie->m_listResultCookieBlocks.AddHead( pCookieBlock );
  229. CString str;
  230. for ( ; nItems > 0; nItems--, pvolumeinfo++, pcookiearray++ )
  231. {
  232. if (IsInvalidSharename(pvolumeinfo->afpvol_name))
  233. continue;
  234. pcookiearray->m_pobject = pvolumeinfo;
  235. // WARNING cookie cast
  236. tRDItem.lParam = reinterpret_cast<LPARAM>((CCookie*)pcookiearray);
  237. if (pvolumeinfo->afpvol_path &&
  238. lstrlen(pvolumeinfo->afpvol_path) == 3 &&
  239. *(pvolumeinfo->afpvol_path + 1) == _T(':') &&
  240. *(pvolumeinfo->afpvol_path + 2) == _T('\\') &&
  241. (*pvolumeinfo->afpvol_path >= _T('a') && *pvolumeinfo->afpvol_path <= _T('z') ||
  242. *pvolumeinfo->afpvol_path >= _T('A') && *pvolumeinfo->afpvol_path <= _T('Z')))
  243. {
  244. tRDItem.nImage = iIconSFMShare;
  245. } else
  246. {
  247. tRDItem.nImage = iIconSFMShareFolder;
  248. }
  249. HRESULT hr = pResultData->InsertItem(&tRDItem);
  250. ASSERT(SUCCEEDED(hr));
  251. }
  252. return S_OK;
  253. }
  254. /*
  255. DWORD
  256. AfpAdminSessionEnum(
  257. IN AFP_SERVER_HANDLE hAfpServer,
  258. OUT LPBYTE * lpbBuffer,
  259. IN DWORD dwPrefMaxLen,
  260. OUT LPDWORD lpdwEntriesRead,
  261. OUT LPDWORD lpdwTotalEntries,
  262. IN LPDWORD lpdwResumeHandle
  263. );
  264. */
  265. typedef DWORD (*SESSIONENUMPROC) (AFP_SERVER_HANDLE,LPBYTE*,DWORD,LPDWORD,LPDWORD,LPDWORD);
  266. // if pResultData is not NULL, add sessions/resources to the listbox
  267. // if pResultData is NULL, delete all sessions/resources
  268. // if pResultData is NULL, return SUCCEEDED(hr) to continue or
  269. // FAILED(hr) to abort
  270. HRESULT SfmFileServiceProvider::EnumerateSessions(
  271. IResultData* pResultData,
  272. CFileMgmtCookie* pcookie,
  273. bool bAddToResultPane)
  274. {
  275. TEST_NONNULL_PTR_PARAM(pcookie);
  276. if ( !g_SfmDLL.LoadFunctionPointers() )
  277. return S_OK;
  278. if ( !SFMConnect(pcookie->QueryTargetServer()) )
  279. return S_OK;
  280. AFP_SESSION_INFO* psessioninfo = NULL;
  281. DWORD dwEntriesRead = 0;
  282. DWORD dwTotalEntries = 0;
  283. DWORD hEnumHandle = 0;
  284. HRESULT hr = S_OK;
  285. NET_API_STATUS retval = NERR_Success;
  286. do {
  287. retval = ((SESSIONENUMPROC)g_SfmDLL[AFP_SESSION_ENUM])(
  288. m_ulSFMServerConnection,
  289. (PBYTE*)&psessioninfo,
  290. (DWORD)-1L,
  291. &dwEntriesRead,
  292. &dwTotalEntries,
  293. &hEnumHandle );
  294. if (NERR_Success == retval)
  295. {
  296. hr = HandleSFMSessionItems( pResultData, pcookie, psessioninfo, dwEntriesRead,
  297. bAddToResultPane);
  298. psessioninfo = NULL;
  299. break;
  300. } else if (ERROR_MORE_DATA == retval) {
  301. ASSERT( NULL != hEnumHandle );
  302. hr = HandleSFMSessionItems( pResultData, pcookie, psessioninfo, dwEntriesRead,
  303. bAddToResultPane);
  304. psessioninfo = NULL;
  305. continue;
  306. } else if (RPC_S_SERVER_UNAVAILABLE == retval && 0 == hEnumHandle) {
  307. // SFM just isn't installed, don't worry about it
  308. retval = NERR_Success;
  309. break;
  310. } else {
  311. if (ERROR_ACCESS_DENIED == retval)
  312. {
  313. (void) DoErrMsgBox(
  314. GetActiveWindow(),
  315. MB_OK | MB_ICONSTOP,
  316. 0,
  317. IDS_POPUP_SFM_SESSIONS_NOACCESS
  318. );
  319. } else
  320. {
  321. (void) DoErrMsgBox(
  322. GetActiveWindow(),
  323. MB_OK | MB_ICONSTOP,
  324. retval,
  325. IDS_POPUP_SFM_SESSIONS
  326. );
  327. }
  328. break;
  329. }
  330. } while (S_OK == hr);
  331. return HRESULT_FROM_WIN32(retval);
  332. }
  333. /*
  334. typedef enum _COLNUM_SESSIONS {
  335. COLNUM_SESSIONS_USERNAME = 0,
  336. COLNUM_SESSIONS_COMPUTERNAME,
  337. COLNUM_SESSIONS_NUM_FILES,
  338. COLNUM_SESSIONS_CONNECTED_TIME,
  339. COLNUM_SESSIONS_IDLE_TIME,
  340. COLNUM_SESSIONS_IS_GUEST
  341. } COLNUM_SESSIONS;
  342. typedef struct _AFP_SESSION_INFO
  343. {
  344. DWORD afpsess_id; // Id of the session
  345. LPWSTR afpsess_ws_name; // Workstation Name,
  346. LPWSTR afpsess_username; // User Name, max. UNLEN
  347. DWORD afpsess_num_cons; // Number of open volumes
  348. DWORD afpsess_num_opens; // Number of open files
  349. LONG afpsess_time; // Time session established
  350. DWORD afpsess_logon_type; // How the user logged on
  351. } AFP_SESSION_INFO, *PAFP_SESSION_INFO;
  352. ShirishK 1/24/97:
  353. afpsess_logon_type: tells you how the user is logged on:
  354. 0 -> Guest, 2 -> the standard MS way, 3 -> Admin
  355. */
  356. HRESULT SfmFileServiceProvider::HandleSFMSessionItems(
  357. IResultData* pResultData,
  358. CFileMgmtCookie* pParentCookie,
  359. PVOID pinfo,
  360. DWORD nItems,
  361. BOOL bAddToResultPane)
  362. {
  363. TEST_NONNULL_PTR_PARAM(pParentCookie);
  364. TEST_NONNULL_PTR_PARAM(pinfo);
  365. if (0 >= nItems)
  366. return S_OK;
  367. BOOL fDeleteAllItems = (NULL == pResultData);
  368. RESULTDATAITEM tRDItem;
  369. ::ZeroMemory( &tRDItem, sizeof(tRDItem) );
  370. // CODEWORK should use MMC_ICON_CALLBACK
  371. tRDItem.nImage = iIconSFMSession;
  372. tRDItem.nCol = COLNUM_SESSIONS_USERNAME;
  373. tRDItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  374. tRDItem.str = MMC_CALLBACK;
  375. AFP_SESSION_INFO* psessioninfo = (AFP_SESSION_INFO*)pinfo;
  376. CSfmSessionCookie* pcookiearray = new CSfmSessionCookie[nItems];
  377. CSfmCookieBlock* pCookieBlock = new CSfmCookieBlock(
  378. pcookiearray,nItems,pParentCookie->QueryTargetServer(),pinfo );
  379. bool bAdded = false;
  380. if ( !fDeleteAllItems || !bAddToResultPane )
  381. {
  382. pParentCookie->m_listResultCookieBlocks.AddHead( pCookieBlock );
  383. bAdded = true;
  384. }
  385. for ( ; nItems > 0; nItems--, psessioninfo++, pcookiearray++ )
  386. {
  387. pcookiearray->m_pobject = psessioninfo;
  388. if ( bAddToResultPane )
  389. {
  390. if (fDeleteAllItems)
  391. {
  392. DWORD dwApiResult = CloseSession( pcookiearray );
  393. if (0L != dwApiResult)
  394. {
  395. CString strName;
  396. TranslateIPToComputerName(psessioninfo->afpsess_ws_name, strName);
  397. (void) DoErrMsgBox(GetActiveWindow(), MB_OK | MB_ICONSTOP, dwApiResult,
  398. IDS_POPUP_SFM_DISCONNECTALLSESSION_ERROR,
  399. strName );
  400. //return S_FALSE;
  401. }
  402. continue;
  403. }
  404. // WARNING cookie cast
  405. tRDItem.lParam = reinterpret_cast<LPARAM>((CCookie*)pcookiearray);
  406. HRESULT hr = pResultData->InsertItem(&tRDItem);
  407. ASSERT(SUCCEEDED(hr));
  408. }
  409. }
  410. if ( !bAdded ) // they were not added to the parent cookie's list
  411. delete pCookieBlock;
  412. return S_OK;
  413. }
  414. /*
  415. DWORD
  416. AfpAdminFileEnum(
  417. IN AFP_SERVER_HANDLE hAfpServer,
  418. OUT LPBYTE * lpbBuffer,
  419. IN DWORD dwPrefMaxLen,
  420. OUT LPDWORD lpdwEntriesRead,
  421. OUT LPDWORD lpdwTotalEntries,
  422. IN LPDWORD lpdwResumeHandle
  423. );
  424. */
  425. typedef DWORD (*FILEENUMPROC) (AFP_SERVER_HANDLE,LPBYTE*,DWORD,LPDWORD,LPDWORD,LPDWORD);
  426. // if pResultData is not NULL, add sessions/resources to the listbox
  427. // if pResultData is NULL, delete all sessions/resources
  428. // if pResultData is NULL, return SUCCEEDED(hr) to continue or
  429. // FAILED(hr) to abort
  430. HRESULT SfmFileServiceProvider::EnumerateResources(
  431. IResultData* pResultData,
  432. CFileMgmtCookie* pcookie)
  433. {
  434. TEST_NONNULL_PTR_PARAM(pcookie);
  435. if ( !g_SfmDLL.LoadFunctionPointers() )
  436. return S_OK;
  437. if ( !SFMConnect(pcookie->QueryTargetServer()) )
  438. return S_OK;
  439. AFP_FILE_INFO* pfileinfo = NULL;
  440. DWORD dwEntriesRead = 0;
  441. DWORD dwTotalEntries = 0;
  442. DWORD hEnumHandle = 0;
  443. HRESULT hr = S_OK;
  444. NET_API_STATUS retval = NERR_Success;
  445. do {
  446. retval = ((FILEENUMPROC)g_SfmDLL[AFP_FILE_ENUM])(
  447. m_ulSFMServerConnection,
  448. (PBYTE*)&pfileinfo,
  449. (DWORD)-1L,
  450. &dwEntriesRead,
  451. &dwTotalEntries,
  452. &hEnumHandle );
  453. if (NERR_Success == retval)
  454. {
  455. hr = HandleSFMResourceItems( pResultData, pcookie, pfileinfo, dwEntriesRead );
  456. pfileinfo = NULL;
  457. break;
  458. } else if (ERROR_MORE_DATA == retval) {
  459. ASSERT( NULL != hEnumHandle );
  460. hr = HandleSFMResourceItems( pResultData, pcookie, pfileinfo, dwEntriesRead );
  461. pfileinfo = NULL;
  462. continue;
  463. } else if (RPC_S_SERVER_UNAVAILABLE == retval && 0 == hEnumHandle) {
  464. // SFM just isn't installed, don't worry about it
  465. retval = NERR_Success;
  466. break;
  467. } else {
  468. if (ERROR_ACCESS_DENIED == retval)
  469. {
  470. (void) DoErrMsgBox(
  471. GetActiveWindow(),
  472. MB_OK | MB_ICONSTOP,
  473. 0,
  474. IDS_POPUP_SFM_RESOURCES_NOACCESS
  475. );
  476. } else
  477. {
  478. (void) DoErrMsgBox(
  479. GetActiveWindow(),
  480. MB_OK | MB_ICONSTOP,
  481. retval,
  482. IDS_POPUP_SFM_RESOURCES
  483. );
  484. }
  485. break;
  486. }
  487. } while (S_OK == hr);
  488. return HRESULT_FROM_WIN32(retval);
  489. }
  490. #if 0
  491. typedef enum _COLNUM_RESOURCES {
  492. COLNUM_RESOURCES_FILENAME = 0,
  493. COLNUM_RESOURCES_USERNAME,
  494. COLNUM_RESOURCES_NUM_LOCKS, // we don't try to display sharename for now, since
  495. // only SFM has this information
  496. COLNUM_RESOURCES_OPEN_MODE
  497. } COLNUM_RESOURCES;
  498. typedef struct _AFP_FILE_INFO
  499. {
  500. DWORD afpfile_id; // Id of the open file fork
  501. DWORD afpfile_open_mode; // Mode in which file is opened
  502. DWORD afpfile_num_locks; // Number of locks on the file
  503. DWORD afpfile_fork_type; // Fork type
  504. LPWSTR afpfile_username; // File opened by this user. max UNLEN
  505. LPWSTR afpfile_path; // Absolute canonical path to the file
  506. } AFP_FILE_INFO, *PAFP_FILE_INFO;
  507. #endif
  508. HRESULT SfmFileServiceProvider::HandleSFMResourceItems(
  509. IResultData* pResultData,
  510. CFileMgmtCookie* pParentCookie,
  511. PVOID pinfo,
  512. DWORD nItems)
  513. {
  514. TEST_NONNULL_PTR_PARAM(pParentCookie);
  515. TEST_NONNULL_PTR_PARAM(pinfo);
  516. if (0 >= nItems)
  517. return S_OK;
  518. BOOL fDeleteAllItems = (NULL == pResultData);
  519. RESULTDATAITEM tRDItem;
  520. ::ZeroMemory( &tRDItem, sizeof(tRDItem) );
  521. // CODEWORK should use MMC_ICON_CALLBACK
  522. tRDItem.nImage = iIconSFMResource;
  523. tRDItem.nCol = COLNUM_RESOURCES_FILENAME;
  524. tRDItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  525. tRDItem.str = MMC_CALLBACK;
  526. AFP_FILE_INFO* pfileinfo = (AFP_FILE_INFO*)pinfo;
  527. CSfmResourceCookie* pcookiearray = new CSfmResourceCookie[nItems];
  528. CSfmCookieBlock* pCookieBlock = new CSfmCookieBlock(
  529. pcookiearray,nItems,pParentCookie->QueryTargetServer(),pinfo );
  530. if (!fDeleteAllItems)
  531. {
  532. pParentCookie->m_listResultCookieBlocks.AddHead( pCookieBlock );
  533. }
  534. CString str;
  535. for ( ; nItems > 0; nItems--, pfileinfo++, pcookiearray++ )
  536. {
  537. pcookiearray->m_pobject = pfileinfo;
  538. if (fDeleteAllItems)
  539. {
  540. DWORD dwApiResult = CloseResource( pcookiearray );
  541. if (0L != dwApiResult)
  542. {
  543. (void) DoErrMsgBox(GetActiveWindow(), MB_OK | MB_ICONSTOP, dwApiResult,
  544. IDS_POPUP_SFM_DISCONNECTALLRESOURCE_ERROR,
  545. pfileinfo->afpfile_path );
  546. return S_FALSE;
  547. }
  548. continue;
  549. }
  550. // WARNING cookie cast
  551. tRDItem.lParam = reinterpret_cast<LPARAM>((CCookie*)pcookiearray);
  552. HRESULT hr = pResultData->InsertItem(&tRDItem);
  553. ASSERT(SUCCEEDED(hr));
  554. }
  555. if (fDeleteAllItems) // they were not added to the parent cookie's list
  556. delete pCookieBlock;
  557. return S_OK;
  558. }
  559. /*
  560. DWORD
  561. AfpAdminFileClose(
  562. IN AFP_SERVER_HANDLE hAfpServer,
  563. IN DWORD dwConnectionId
  564. );
  565. DWORD
  566. AfpAdminConnectionEnum(
  567. IN AFP_SERVER_HANDLE hAfpServer,
  568. OUT LPBYTE *ppbBuffer,
  569. IN DWORD dwFilter,
  570. IN DWORD dwId,
  571. IN DWORD dwPrefMaxLen,
  572. OUT LPDWORD lpdwEntriesRead,
  573. OUT LPDWORD lpdwTotalEntries,
  574. IN LPDWORD lpdwResumeHandle
  575. );
  576. DWORD
  577. AfpAdminConnectionClose(
  578. IN AFP_SERVER_HANDLE hAfpServer,
  579. IN DWORD dwConnectionId
  580. );
  581. DWORD
  582. AfpAdminVolumeGetInfo (
  583. IN AFP_SERVER_HANDLE hAfpServer,
  584. IN LPWSTR lpwsVolumeName,
  585. OUT LPBYTE * lpbBuffer
  586. );
  587. typedef struct _AFP_VOLUME_INFO
  588. {
  589. LPWSTR afpvol_name; // Name of the volume max.
  590. DWORD afpvol_id; // id of this volume. generated by sever
  591. LPWSTR afpvol_password; // Volume password, max. AFP_VOLPASS_LEN
  592. DWORD afpvol_max_uses; // Max opens allowed
  593. DWORD afpvol_props_mask; // Mask of volume properties
  594. DWORD afpvol_curr_uses; // Number of curr open connections.
  595. LPWSTR afpvol_path; // The actual path
  596. // Ignored for VolumeSetInfo
  597. } AFP_VOLUME_INFO, *PAFP_VOLUME_INFO;
  598. DWORD
  599. AfpAdminVolumeDelete(
  600. IN AFP_SERVER_HANDLE hAfpServer,
  601. IN LPWSTR lpwsVolumeName
  602. );
  603. */
  604. typedef DWORD (*VOLUMEGETINFOPROC) (AFP_SERVER_HANDLE,LPWSTR,LPBYTE*);
  605. typedef DWORD (*FILECLOSEPROC) (AFP_SERVER_HANDLE,DWORD);
  606. typedef DWORD (*AFPCONNECTIONENUMPROC) (AFP_SERVER_HANDLE,LPBYTE*,DWORD,
  607. DWORD,DWORD,LPDWORD,LPDWORD,LPDWORD);
  608. typedef DWORD (*AFPCONNECTIONCLOSEPROC) (AFP_SERVER_HANDLE,DWORD);
  609. typedef DWORD (*VOLUMEDELPROC) (AFP_SERVER_HANDLE,LPWSTR);
  610. typedef DWORD (*BUFFERFREEPROC) (LPVOID);
  611. // S_OK: user wants to continue
  612. // S_FALSE: user wants to cancel the operation and keep the share
  613. // E_FAIL: share doesn't exist, needs to refresh
  614. HRESULT SfmFileServiceProvider::ConfirmDeleteShare( LPCTSTR lpcszServerName, LPCTSTR lpcszShareName )
  615. {
  616. if ( !g_SfmDLL.LoadFunctionPointers() )
  617. return E_FAIL;
  618. if ( !SFMConnect(lpcszServerName, TRUE) )
  619. return E_FAIL;
  620. PAFP_VOLUME_INFO pAfpVolumeInfo = NULL;
  621. DWORD dwRetCode = ((VOLUMEGETINFOPROC)g_SfmDLL[AFP_VOLUME_GET_INFO])(
  622. m_ulSFMServerConnection,
  623. const_cast<LPTSTR>(lpcszShareName),
  624. (LPBYTE*)&pAfpVolumeInfo);
  625. if (AFPERR_VolumeNonExist == dwRetCode)
  626. return E_FAIL;
  627. if (NO_ERROR != dwRetCode)
  628. return E_FAIL;
  629. ASSERT( NULL != pAfpVolumeInfo);
  630. UINT cOpens = 0;
  631. UINT cConns = 0;
  632. //
  633. // Check if there are any open resources on the volume
  634. //
  635. PAFP_FILE_INFO pAfpFileInfos = NULL;
  636. DWORD dwEntriesRead = 0;
  637. DWORD dwTotalEntries = 0;
  638. dwRetCode = ((FILEENUMPROC)g_SfmDLL[AFP_FILE_ENUM])(
  639. m_ulSFMServerConnection,
  640. (PBYTE*)&pAfpFileInfos,
  641. (DWORD)-1L,
  642. &dwEntriesRead,
  643. &dwTotalEntries,
  644. NULL );
  645. if (NO_ERROR == dwRetCode)
  646. {
  647. PAFP_FILE_INFO pAfpFileInfoIter = pAfpFileInfos;
  648. for ( DWORD dwIndex = 0;
  649. dwIndex < dwEntriesRead;
  650. dwIndex++, pAfpFileInfoIter++ )
  651. {
  652. if (_tcsnicmp(pAfpVolumeInfo->afpvol_path,
  653. pAfpFileInfoIter->afpfile_path,
  654. _tcslen(pAfpVolumeInfo->afpvol_path)) == 0)
  655. {
  656. cOpens++;
  657. }
  658. }
  659. ((BUFFERFREEPROC) g_SfmDLL[AFP_BUFFER_FREE])(pAfpFileInfos);
  660. }
  661. //
  662. // Check if there are any users connected to the volume
  663. // by enumerating the connections to this volume.
  664. //
  665. PAFP_CONNECTION_INFO pAfpConnections = NULL;
  666. dwRetCode = ((AFPCONNECTIONENUMPROC)g_SfmDLL[AFP_CONNECTION_ENUM])(
  667. m_ulSFMServerConnection,
  668. (LPBYTE*)&pAfpConnections,
  669. AFP_FILTER_ON_VOLUME_ID,
  670. pAfpVolumeInfo->afpvol_id,
  671. (DWORD)-1, // Get all conenctions
  672. &dwEntriesRead,
  673. &dwTotalEntries,
  674. NULL );
  675. ((BUFFERFREEPROC) g_SfmDLL[AFP_BUFFER_FREE])(pAfpVolumeInfo);
  676. if (NO_ERROR == dwRetCode)
  677. {
  678. cConns = dwTotalEntries;
  679. ((BUFFERFREEPROC) g_SfmDLL[AFP_BUFFER_FREE])(pAfpConnections);
  680. }
  681. // pop up the proper confirmation message
  682. if (cConns > 0)
  683. {
  684. if (cOpens > 0)
  685. {
  686. if (IDYES != DoErrMsgBox(
  687. GetActiveWindow(),
  688. MB_YESNO,
  689. 0,
  690. IDS_s_CONFIRM_DELETESHARE_FILE,
  691. cOpens,
  692. cConns,
  693. lpcszShareName))
  694. {
  695. return S_FALSE;
  696. }
  697. } else // (cConns > 0)
  698. {
  699. if (IDYES != DoErrMsgBox(
  700. GetActiveWindow(),
  701. MB_YESNO,
  702. 0,
  703. IDS_s_CONFIRM_DELETESHARE_CONN,
  704. cConns,
  705. lpcszShareName))
  706. {
  707. return S_FALSE;
  708. }
  709. }
  710. } else
  711. {
  712. if (IDYES != DoErrMsgBox(
  713. GetActiveWindow(),
  714. MB_YESNO,
  715. 0,
  716. IDS_s_CONFIRM_DELETESHARE,
  717. lpcszShareName))
  718. {
  719. return S_FALSE;
  720. }
  721. }
  722. return S_OK;
  723. }
  724. DWORD SfmFileServiceProvider::DeleteShare( LPCWSTR pwchServerName, LPCWSTR pwchShareName )
  725. {
  726. if ( !g_SfmDLL.LoadFunctionPointers() )
  727. return S_OK;
  728. if ( !SFMConnect(pwchServerName, TRUE) )
  729. return S_OK;
  730. PAFP_VOLUME_INFO pAfpVolumeInfo = NULL;
  731. DWORD dwRetCode = ((VOLUMEGETINFOPROC)g_SfmDLL[AFP_VOLUME_GET_INFO])(
  732. m_ulSFMServerConnection,
  733. const_cast<LPTSTR>(pwchShareName),
  734. (LPBYTE*)&pAfpVolumeInfo);
  735. if (AFPERR_VolumeNonExist == dwRetCode)
  736. return NERR_Success;
  737. if (NO_ERROR == dwRetCode)
  738. {
  739. ASSERT( NULL != pAfpVolumeInfo);
  740. //
  741. // Check if there are any open resources on the volume
  742. //
  743. PAFP_FILE_INFO pAfpFileInfos = NULL;
  744. DWORD dwEntriesRead = 0;
  745. DWORD dwTotalEntries = 0;
  746. dwRetCode = ((FILEENUMPROC)g_SfmDLL[AFP_FILE_ENUM])(
  747. m_ulSFMServerConnection,
  748. (PBYTE*)&pAfpFileInfos,
  749. (DWORD)-1L,
  750. &dwEntriesRead,
  751. &dwTotalEntries,
  752. NULL );
  753. if (NO_ERROR == dwRetCode)
  754. {
  755. PAFP_FILE_INFO pAfpFileInfoIter = pAfpFileInfos;
  756. for ( DWORD dwIndex = 0;
  757. dwIndex < dwEntriesRead;
  758. dwIndex++, pAfpFileInfoIter++ )
  759. {
  760. if (_tcsnicmp(pAfpVolumeInfo->afpvol_path,
  761. pAfpFileInfoIter->afpfile_path,
  762. _tcslen(pAfpVolumeInfo->afpvol_path)) == 0)
  763. {
  764. ((FILECLOSEPROC)g_SfmDLL[AFP_FILE_CLOSE])(
  765. m_ulSFMServerConnection,
  766. pAfpFileInfoIter->afpfile_id );
  767. }
  768. }
  769. ((BUFFERFREEPROC) g_SfmDLL[AFP_BUFFER_FREE])(pAfpFileInfos);
  770. }
  771. //
  772. // Check if there are any users connected to the volume
  773. // by enumerating the connections to this volume.
  774. //
  775. PAFP_CONNECTION_INFO pAfpConnections = NULL;
  776. dwRetCode = ((AFPCONNECTIONENUMPROC)g_SfmDLL[AFP_CONNECTION_ENUM])(
  777. m_ulSFMServerConnection,
  778. (LPBYTE*)&pAfpConnections,
  779. AFP_FILTER_ON_VOLUME_ID,
  780. pAfpVolumeInfo->afpvol_id,
  781. (DWORD)-1, // Get all conenctions
  782. &dwEntriesRead,
  783. &dwTotalEntries,
  784. NULL );
  785. ((BUFFERFREEPROC) g_SfmDLL[AFP_BUFFER_FREE])(pAfpVolumeInfo);
  786. if (NO_ERROR == dwRetCode)
  787. {
  788. PAFP_CONNECTION_INFO pAfpConnInfoIter = pAfpConnections;
  789. for ( DWORD dwIndex = 0;
  790. dwIndex < dwEntriesRead;
  791. dwIndex++, pAfpConnInfoIter++ )
  792. {
  793. ((AFPCONNECTIONCLOSEPROC)g_SfmDLL[AFP_CONNECTION_CLOSE])(
  794. m_ulSFMServerConnection,
  795. pAfpConnInfoIter->afpconn_id );
  796. }
  797. ((BUFFERFREEPROC) g_SfmDLL[AFP_BUFFER_FREE])(pAfpConnections);
  798. }
  799. dwRetCode = ((VOLUMEDELPROC)g_SfmDLL[AFP_VOLUME_DEL])(
  800. m_ulSFMServerConnection,
  801. const_cast<LPTSTR>(pwchShareName) );
  802. if (AFPERR_VolumeNonExist == dwRetCode)
  803. dwRetCode = NERR_Success;
  804. }
  805. return dwRetCode;
  806. }
  807. /*
  808. DWORD
  809. AfpAdminSessionClose(
  810. IN AFP_SERVER_HANDLE hAfpServer,
  811. IN DWORD dwSessionId
  812. );
  813. */
  814. typedef DWORD (*SESSIONCLOSEPROC) (AFP_SERVER_HANDLE,DWORD);
  815. DWORD SfmFileServiceProvider::CloseSession(CFileMgmtResultCookie* pcookie)
  816. {
  817. if ( !g_SfmDLL.LoadFunctionPointers() )
  818. return S_OK;
  819. USES_CONVERSION;
  820. if ( !SFMConnect(T2OLE(const_cast<LPTSTR>(pcookie->QueryTargetServer()))) )
  821. return S_OK;
  822. ASSERT( FILEMGMT_SESSION == pcookie->QueryObjectType() );
  823. AFP_SESSION_INFO* psessioninfo = (AFP_SESSION_INFO*)pcookie->m_pobject;
  824. ASSERT( NULL != psessioninfo );
  825. DWORD dwRet = ((SESSIONCLOSEPROC)g_SfmDLL[AFP_SESSION_CLOSE])(
  826. m_ulSFMServerConnection,
  827. psessioninfo->afpsess_id );
  828. return (AFPERR_InvalidId == dwRet ? NERR_Success : dwRet);
  829. }
  830. DWORD SfmFileServiceProvider::CloseResource(CFileMgmtResultCookie* pcookie)
  831. {
  832. if ( !g_SfmDLL.LoadFunctionPointers() )
  833. return S_OK;
  834. if ( !SFMConnect(T2OLE(const_cast<LPTSTR>(pcookie->QueryTargetServer()))) )
  835. return S_OK;
  836. ASSERT( FILEMGMT_RESOURCE == pcookie->QueryObjectType() );
  837. AFP_FILE_INFO* pfileinfo = (AFP_FILE_INFO*)pcookie->m_pobject;
  838. ASSERT( NULL != pfileinfo );
  839. DWORD dwRet = ((FILECLOSEPROC)g_SfmDLL[AFP_FILE_CLOSE])(
  840. m_ulSFMServerConnection,
  841. pfileinfo->afpfile_id );
  842. return (AFPERR_InvalidId == dwRet ? NERR_Success : dwRet);
  843. }
  844. /*
  845. DWORD
  846. AfpAdminDirectoryGetInfo(
  847. IN AFP_SERVER_HANDLE hAfpServer,
  848. IN LPWSTR lpwsPath,
  849. OUT LPBYTE *ppAfpDirectoryInfo
  850. );
  851. DWORD
  852. AfpAdminDirectorySetInfo(
  853. IN AFP_SERVER_HANDLE hAfpServer,
  854. IN LPBYTE pAfpDirectoryInfo,
  855. IN DWORD dwParmNum
  856. );
  857. */
  858. typedef DWORD (*DIRECTORYGETINFOPROC) (AFP_SERVER_HANDLE,LPWSTR,LPBYTE*);
  859. typedef DWORD (*DIRECTORYSETINFOPROC) (AFP_SERVER_HANDLE,LPBYTE,DWORD);
  860. DWORD SfmFileServiceProvider::GetDirectoryInfo(
  861. LPCTSTR ptchServerName,
  862. LPCWSTR pszPath,
  863. DWORD* pdwPerms,
  864. CString& strOwner,
  865. CString& strGroup )
  866. {
  867. if ( !g_SfmDLL.LoadFunctionPointers() )
  868. return S_OK;
  869. if ( !SFMConnect(ptchServerName, TRUE) )
  870. return S_OK;
  871. AFP_DIRECTORY_INFO* pdirinfo = NULL;
  872. DWORD retval = ((DIRECTORYGETINFOPROC)g_SfmDLL[AFP_DIRECTORY_GET_INFO])(
  873. m_ulSFMServerConnection,
  874. const_cast<LPWSTR>(pszPath),
  875. (LPBYTE*)&pdirinfo );
  876. if (0L != retval)
  877. {
  878. return retval;
  879. }
  880. ASSERT( NULL != pdirinfo );
  881. *pdwPerms = pdirinfo->afpdir_perms;
  882. strOwner = pdirinfo->afpdir_owner;
  883. strGroup = pdirinfo->afpdir_group;
  884. FreeData( pdirinfo );
  885. return 0L;
  886. }
  887. DWORD SfmFileServiceProvider::SetDirectoryInfo(
  888. LPCTSTR ptchServerName,
  889. LPCWSTR pszPath,
  890. DWORD dwPerms,
  891. LPCWSTR pszOwner,
  892. LPCWSTR pszGroup )
  893. {
  894. if ( !g_SfmDLL.LoadFunctionPointers() )
  895. return S_OK;
  896. if ( !SFMConnect(ptchServerName, TRUE) )
  897. return S_OK;
  898. AFP_DIRECTORY_INFO dirinfo;
  899. ::memset( &dirinfo, 0, sizeof(dirinfo) );
  900. dirinfo.afpdir_path = const_cast<LPWSTR>(pszPath);
  901. dirinfo.afpdir_perms = dwPerms;
  902. dirinfo.afpdir_owner = const_cast<LPWSTR>(pszOwner);
  903. dirinfo.afpdir_group = const_cast<LPWSTR>(pszGroup);
  904. dirinfo.afpdir_in_volume = FALSE;
  905. return ((DIRECTORYSETINFOPROC)g_SfmDLL[AFP_DIRECTORY_SET_INFO])(
  906. m_ulSFMServerConnection,
  907. (LPBYTE)&dirinfo,
  908. AFP_DIR_PARMNUM_ALL );
  909. }
  910. /*
  911. DWORD
  912. AfpAdminVolumeSetInfo (
  913. IN AFP_SERVER_HANDLE hAfpServer,
  914. IN LPBYTE pBuffer,
  915. IN DWORD dwParmNum
  916. );
  917. */
  918. typedef DWORD (*VOLUMESETINFOPROC) (AFP_SERVER_HANDLE,LPBYTE,DWORD);
  919. VOID SfmFileServiceProvider::DisplayShareProperties(
  920. LPPROPERTYSHEETCALLBACK pCallBack,
  921. LPDATAOBJECT pDataObject,
  922. LONG_PTR handle)
  923. {
  924. /*
  925. add General page
  926. */
  927. CSharePageGeneralSFM * pPage = new CSharePageGeneralSFM();
  928. if ( !pPage->Load( m_pFileMgmtData, pDataObject ) )
  929. return;
  930. // This mechanism deletes the CSharePageGeneral when the property sheet is finished
  931. pPage->m_pfnOriginalPropSheetPageProc = pPage->m_psp.pfnCallback;
  932. pPage->m_psp.lParam = reinterpret_cast<LPARAM>(pPage);
  933. pPage->m_psp.pfnCallback = &CSharePageGeneralSFM::PropSheetPageProc;
  934. pPage->m_handle = handle;
  935. HPROPSHEETPAGE hPage=MyCreatePropertySheetPage(&pPage->m_psp);
  936. pCallBack->AddPage(hPage);
  937. CreateFolderSecurityPropPage(pCallBack, pDataObject);
  938. }
  939. DWORD SfmFileServiceProvider::ReadShareProperties(
  940. LPCTSTR ptchServerName,
  941. LPCTSTR ptchShareName,
  942. OUT PVOID* ppvPropertyBlock,
  943. OUT CString& /*strDescription*/,
  944. OUT CString& strPath,
  945. OUT BOOL* pfEditDescription,
  946. OUT BOOL* pfEditPath,
  947. OUT DWORD* pdwShareType)
  948. {
  949. if ( !g_SfmDLL.LoadFunctionPointers() )
  950. {
  951. ASSERT(FALSE);
  952. return S_OK;
  953. }
  954. if (ppvPropertyBlock) *ppvPropertyBlock = NULL;
  955. if (pdwShareType) *pdwShareType = 0;
  956. if (pfEditDescription) *pfEditDescription = FALSE;
  957. if (pfEditPath) *pfEditPath = FALSE;
  958. USES_CONVERSION;
  959. if ( !SFMConnect(T2OLE(const_cast<LPTSTR>(ptchServerName))) )
  960. return S_OK;
  961. AFP_VOLUME_INFO* pvolumeinfo = NULL;
  962. NET_API_STATUS retval = ((VOLUMEGETINFOPROC)g_SfmDLL[AFP_VOLUME_GET_INFO])(
  963. m_ulSFMServerConnection,
  964. T2OLE(const_cast<LPTSTR>(ptchShareName)),
  965. (LPBYTE*)&pvolumeinfo);
  966. if (NERR_Success == retval)
  967. {
  968. strPath = pvolumeinfo->afpvol_path;
  969. if (ppvPropertyBlock)
  970. {
  971. *ppvPropertyBlock = pvolumeinfo; // will be freed by the caller
  972. } else
  973. {
  974. FreeData(pvolumeinfo);
  975. }
  976. }
  977. return retval;
  978. }
  979. // the changed values have already been loaded into pvPropertyBlock
  980. DWORD SfmFileServiceProvider::WriteShareProperties(LPCTSTR ptchServerName, LPCTSTR /*ptchShareName*/,
  981. PVOID pvPropertyBlock, LPCTSTR /*ptchDescription*/, LPCTSTR /*ptchPath*/)
  982. {
  983. if ( !g_SfmDLL.LoadFunctionPointers() )
  984. return S_OK;
  985. USES_CONVERSION;
  986. if ( !SFMConnect(T2OLE(const_cast<LPTSTR>(ptchServerName))) )
  987. return S_OK;
  988. ASSERT( NULL != pvPropertyBlock );
  989. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)pvPropertyBlock;
  990. DWORD retval = ((VOLUMESETINFOPROC)g_SfmDLL[AFP_VOLUME_SET_INFO])(
  991. m_ulSFMServerConnection,
  992. (LPBYTE)pvolumeinfo,
  993. AFP_VOL_PARMNUM_ALL);
  994. pvolumeinfo->afpvol_path = NULL;
  995. return retval;
  996. }
  997. VOID SfmFileServiceProvider::FreeShareProperties(PVOID pvPropertyBlock)
  998. {
  999. FreeData( pvPropertyBlock );
  1000. }
  1001. DWORD SfmFileServiceProvider::QueryMaxUsers(PVOID pvPropertyBlock)
  1002. {
  1003. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)pvPropertyBlock;
  1004. ASSERT( NULL != pvolumeinfo );
  1005. return pvolumeinfo->afpvol_max_uses;
  1006. }
  1007. VOID SfmFileServiceProvider::SetMaxUsers(PVOID pvPropertyBlock, DWORD dwMaxUsers)
  1008. {
  1009. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)pvPropertyBlock;
  1010. ASSERT( NULL != pvolumeinfo );
  1011. pvolumeinfo->afpvol_max_uses = dwMaxUsers;
  1012. }
  1013. VOID SfmFileServiceProvider::FreeData(PVOID pv)
  1014. {
  1015. if (pv != NULL)
  1016. {
  1017. ASSERT( NULL != g_SfmDLL[AFP_BUFFER_FREE] );
  1018. (void) ((BUFFERFREEPROC)g_SfmDLL[AFP_BUFFER_FREE])( pv );
  1019. }
  1020. }
  1021. LPCTSTR SfmFileServiceProvider::QueryTransportString()
  1022. {
  1023. return m_strTransportSFM;
  1024. }
  1025. BOOL SfmFileServiceProvider::DisplaySfmProperties(LPDATAOBJECT pDataObject, CFileMgmtCookie* pcookie)
  1026. {
  1027. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1028. if ( !g_SfmDLL.LoadFunctionPointers() )
  1029. return S_OK;
  1030. CString strServerName;
  1031. HRESULT hr = ExtractString( pDataObject, CFileMgmtDataObject::m_CFMachineName, &strServerName, MAX_PATH );
  1032. if ( !SFMConnect(strServerName) )
  1033. return S_OK;
  1034. CString strTitle;
  1035. // check to see if we have a sheet up, if so make it active.
  1036. if (m_pSfmPropSheet)
  1037. {
  1038. m_pSfmPropSheet->SetActiveWindow();
  1039. return TRUE;
  1040. }
  1041. // nothing up, create a new sheet
  1042. strTitle.LoadString(IDS_PROP_SHEET_TITLE);
  1043. SetSfmPropSheet(new CSFMPropertySheet);
  1044. if (!m_pSfmPropSheet->FInit(NULL, m_ulSFMServerConnection, strTitle, this, pcookie->QueryTargetServer()))
  1045. {
  1046. delete m_pSfmPropSheet;
  1047. SetSfmPropSheet(NULL);
  1048. return FALSE;
  1049. }
  1050. return m_pSfmPropSheet->DoModelessSheet(pDataObject);
  1051. }
  1052. void SfmFileServiceProvider::CleanupSfmProperties()
  1053. {
  1054. if (m_pSfmPropSheet)
  1055. {
  1056. // we're going away, so set the pointer to NULL
  1057. // so the property sheet won't try to call back into us.
  1058. m_pSfmPropSheet->SetProvider(NULL);
  1059. m_pSfmPropSheet->CancelSheet();
  1060. m_pSfmPropSheet->Release();
  1061. SetSfmPropSheet(NULL);
  1062. }
  1063. }
  1064. void SfmFileServiceProvider::SetSfmPropSheet(CSFMPropertySheet * pSfmPropSheet)
  1065. {
  1066. if (m_pSfmPropSheet)
  1067. m_pSfmPropSheet->Release();
  1068. m_pSfmPropSheet = pSfmPropSheet;
  1069. }
  1070. DWORD SfmFileServiceProvider::UserHasAccess(LPCWSTR pwchServerName)
  1071. {
  1072. PAFP_SERVER_INFO pAfpServerInfo;
  1073. DWORD err;
  1074. if ( !g_SfmDLL.LoadFunctionPointers() )
  1075. return FALSE;
  1076. if ( !SFMConnect(pwchServerName) )
  1077. return FALSE;
  1078. err = ((SERVERGETINFOPROC) g_SfmDLL[AFP_SERVER_GET_INFO])(m_ulSFMServerConnection,
  1079. (LPBYTE*) &pAfpServerInfo);
  1080. if (err == NO_ERROR)
  1081. {
  1082. ((BUFFERFREEPROC) g_SfmDLL[AFP_BUFFER_FREE])(pAfpServerInfo);
  1083. }
  1084. return err;
  1085. }
  1086. BOOL SfmFileServiceProvider::FSFMInstalled(LPCWSTR pwchServerName)
  1087. {
  1088. BOOL bInstalled = FALSE;
  1089. if ( !g_SfmDLL.LoadFunctionPointers() )
  1090. return FALSE;
  1091. if ( !SFMConnect(pwchServerName) )
  1092. return FALSE;
  1093. // check to make sure the service is there
  1094. SC_HANDLE hScManager = ::OpenSCManager(pwchServerName, NULL, SC_MANAGER_CONNECT);
  1095. if (hScManager)
  1096. {
  1097. SC_HANDLE hService = ::OpenService(hScManager, AFP_SERVICE_NAME, GENERIC_READ);
  1098. if (hService != NULL)
  1099. {
  1100. // the service is there. May or may not be started.
  1101. bInstalled = TRUE;
  1102. ::CloseServiceHandle(hService);
  1103. }
  1104. ::CloseServiceHandle(hScManager);
  1105. }
  1106. return bInstalled;
  1107. }
  1108. // ensures that SFM is started
  1109. BOOL SfmFileServiceProvider::StartSFM(HWND hwndParent, SC_HANDLE hScManager, LPCWSTR pwchServerName)
  1110. {
  1111. BOOL bStarted = FALSE;
  1112. if ( !g_SfmDLL.LoadFunctionPointers() )
  1113. return FALSE;
  1114. if ( !SFMConnect(pwchServerName) )
  1115. return FALSE;
  1116. // open the service
  1117. SC_HANDLE hService = ::OpenService(hScManager, AFP_SERVICE_NAME, GENERIC_READ);
  1118. if (hService != NULL)
  1119. {
  1120. SERVICE_STATUS ss;
  1121. // get the service status
  1122. if (::QueryServiceStatus(hService, &ss))
  1123. {
  1124. APIERR err = NO_ERROR;
  1125. if (ss.dwCurrentState != SERVICE_RUNNING)
  1126. {
  1127. // try to start the service
  1128. CString strSvcDisplayName, strMessageBox;
  1129. strSvcDisplayName.LoadString(IDS_PROP_SHEET_TITLE);
  1130. AfxFormatString1(strMessageBox, IDS_START_SERVICE, strSvcDisplayName);
  1131. if (AfxMessageBox(strMessageBox, MB_YESNO) == IDYES)
  1132. {
  1133. TCHAR szName[MAX_COMPUTERNAME_LENGTH + 1] = {0};
  1134. DWORD dwSize = sizeof(szName);
  1135. if (pwchServerName == NULL)
  1136. {
  1137. GetComputerName(szName, &dwSize);
  1138. pwchServerName = szName;
  1139. }
  1140. // the service control progress handles displaying the error
  1141. err = CServiceControlProgress::S_EStartService(hwndParent,
  1142. hScManager,
  1143. pwchServerName,
  1144. AFP_SERVICE_NAME,
  1145. strSvcDisplayName,
  1146. 0,
  1147. NULL);
  1148. }
  1149. else
  1150. {
  1151. // user chose not to start the service.
  1152. err = 1;
  1153. }
  1154. }
  1155. if (err == NO_ERROR)
  1156. {
  1157. bStarted = TRUE;
  1158. }
  1159. }
  1160. ::CloseServiceHandle(hService);
  1161. }
  1162. return bStarted;
  1163. }
  1164. CSfmCookieBlock::~CSfmCookieBlock()
  1165. {
  1166. if (NULL != m_pvCookieData)
  1167. {
  1168. (void) ((BUFFERFREEPROC)g_SfmDLL[AFP_BUFFER_FREE])( m_pvCookieData );
  1169. m_pvCookieData = NULL;
  1170. }
  1171. }
  1172. DEFINE_COOKIE_BLOCK(CSfmCookie)
  1173. DEFINE_FORWARDS_MACHINE_NAME( CSfmCookie, m_pCookieBlock )
  1174. void CSfmCookie::AddRefCookie() { m_pCookieBlock->AddRef(); }
  1175. void CSfmCookie::ReleaseCookie() { m_pCookieBlock->Release(); }
  1176. HRESULT CSfmCookie::GetTransport( FILEMGMT_TRANSPORT* pTransport )
  1177. {
  1178. *pTransport = FILEMGMT_SFM;
  1179. return S_OK;
  1180. }
  1181. HRESULT CSfmShareCookie::GetShareName( CString& strShareName )
  1182. {
  1183. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)m_pobject;
  1184. ASSERT( NULL != pvolumeinfo );
  1185. USES_CONVERSION;
  1186. strShareName = OLE2T(pvolumeinfo->afpvol_name);
  1187. return S_OK;
  1188. }
  1189. HRESULT CSfmShareCookie::GetSharePIDList( OUT LPITEMIDLIST *ppidl )
  1190. {
  1191. ASSERT(ppidl);
  1192. ASSERT(NULL == *ppidl); // prevent memory leak
  1193. *ppidl = NULL;
  1194. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)m_pobject;
  1195. ASSERT( NULL != pvolumeinfo );
  1196. ASSERT( _tcslen(pvolumeinfo->afpvol_path) >= 3 &&
  1197. _T(':') == *(pvolumeinfo->afpvol_path + 1) );
  1198. USES_CONVERSION;
  1199. PCTSTR pszTargetServer = m_pCookieBlock->QueryTargetServer();
  1200. CString csPath;
  1201. if (pszTargetServer)
  1202. {
  1203. //
  1204. // since MS Windows user cannot see shares created for MAC users,
  1205. // we have to use $ share to retrieve the pidl here.
  1206. //
  1207. CString csTemp = OLE2T(pvolumeinfo->afpvol_path);
  1208. csTemp.SetAt(1, _T('$'));
  1209. if ( _tcslen(pszTargetServer) >= 2 &&
  1210. _T('\\') == *pszTargetServer &&
  1211. _T('\\') == *(pszTargetServer + 1) )
  1212. {
  1213. csPath = pszTargetServer;
  1214. } else
  1215. {
  1216. csPath = _T("\\\\");
  1217. csPath += pszTargetServer;
  1218. }
  1219. csPath += _T("\\");
  1220. csPath += csTemp;
  1221. } else
  1222. {
  1223. csPath = OLE2T(pvolumeinfo->afpvol_path);
  1224. }
  1225. if (FALSE == csPath.IsEmpty())
  1226. *ppidl = ILCreateFromPath(csPath);
  1227. return ((*ppidl) ? S_OK : E_FAIL);
  1228. }
  1229. HRESULT CSfmSessionCookie::GetSessionID( DWORD* pdwSessionID )
  1230. {
  1231. AFP_SESSION_INFO* psessioninfo = (AFP_SESSION_INFO*)m_pobject;
  1232. ASSERT( NULL != pdwSessionID && NULL != psessioninfo );
  1233. *pdwSessionID = psessioninfo->afpsess_id;
  1234. return S_OK;
  1235. }
  1236. HRESULT CSfmResourceCookie::GetFileID( DWORD* pdwFileID )
  1237. {
  1238. AFP_FILE_INFO* pfileinfo = (AFP_FILE_INFO*)m_pobject;
  1239. ASSERT( NULL != pdwFileID && NULL != pfileinfo );
  1240. *pdwFileID = pfileinfo->afpfile_id;
  1241. return S_OK;
  1242. }
  1243. BSTR CSfmShareCookie::GetColumnText( int nCol )
  1244. {
  1245. switch (nCol)
  1246. {
  1247. case COLNUM_SHARES_SHARED_FOLDER:
  1248. return GetShareInfo()->afpvol_name;
  1249. case COLNUM_SHARES_SHARED_PATH:
  1250. return GetShareInfo()->afpvol_path;
  1251. case COLNUM_SHARES_TRANSPORT:
  1252. return const_cast<BSTR>((LPCTSTR)g_strTransportSFM);
  1253. case COLNUM_SHARES_COMMENT:
  1254. break; // not known for SFM
  1255. default:
  1256. ASSERT(FALSE);
  1257. break;
  1258. }
  1259. return L"";
  1260. }
  1261. BSTR CSfmShareCookie::QueryResultColumnText( int nCol, CFileMgmtComponentData& /*refcdata*/ )
  1262. {
  1263. if (COLNUM_SHARES_NUM_SESSIONS == nCol)
  1264. return MakeDwordResult( GetNumOfCurrentUses() );
  1265. return GetColumnText(nCol);
  1266. }
  1267. extern CString g_cstrClientName;
  1268. extern CString g_cstrGuest;
  1269. extern CString g_cstrYes;
  1270. extern CString g_cstrNo;
  1271. BSTR CSfmSessionCookie::GetColumnText( int nCol )
  1272. {
  1273. switch (nCol)
  1274. {
  1275. case COLNUM_SESSIONS_USERNAME:
  1276. if (0 == GetSessionInfo()->afpsess_logon_type &&
  1277. ( !(GetSessionInfo()->afpsess_username) ||
  1278. _T('\0') == *(GetSessionInfo()->afpsess_username) ) )
  1279. {
  1280. return const_cast<BSTR>(((LPCTSTR)g_cstrGuest));
  1281. } else
  1282. {
  1283. return GetSessionInfo()->afpsess_username;
  1284. }
  1285. case COLNUM_SESSIONS_COMPUTERNAME:
  1286. {
  1287. TranslateIPToComputerName(GetSessionInfo()->afpsess_ws_name, g_cstrClientName);
  1288. return const_cast<BSTR>(((LPCTSTR)g_cstrClientName));
  1289. }
  1290. case COLNUM_SESSIONS_TRANSPORT:
  1291. return const_cast<BSTR>((LPCTSTR)g_strTransportSFM);
  1292. case COLNUM_SESSIONS_IS_GUEST:
  1293. if (0 == GetSessionInfo()->afpsess_logon_type)
  1294. return const_cast<BSTR>(((LPCTSTR)g_cstrYes));
  1295. else
  1296. return const_cast<BSTR>(((LPCTSTR)g_cstrNo));
  1297. default:
  1298. ASSERT(FALSE);
  1299. break;
  1300. }
  1301. return L"";
  1302. }
  1303. BSTR CSfmSessionCookie::QueryResultColumnText( int nCol, CFileMgmtComponentData& /*refcdata*/ )
  1304. {
  1305. switch (nCol)
  1306. {
  1307. case COLNUM_SESSIONS_NUM_FILES:
  1308. return MakeDwordResult( GetNumOfOpenFiles() );
  1309. case COLNUM_SESSIONS_CONNECTED_TIME:
  1310. return MakeElapsedTimeResult( GetConnectedTime() );
  1311. case COLNUM_SESSIONS_IDLE_TIME:
  1312. return L""; // not known for SFM sessions
  1313. default:
  1314. break;
  1315. }
  1316. return GetColumnText(nCol);
  1317. }
  1318. BSTR CSfmResourceCookie::GetColumnText( int nCol )
  1319. {
  1320. switch (nCol)
  1321. {
  1322. case COLNUM_RESOURCES_FILENAME:
  1323. return GetFileInfo()->afpfile_path;
  1324. case COLNUM_RESOURCES_USERNAME:
  1325. return GetFileInfo()->afpfile_username;
  1326. case COLNUM_RESOURCES_TRANSPORT:
  1327. return const_cast<BSTR>((LPCTSTR)g_strTransportSFM);
  1328. case COLNUM_RESOURCES_OPEN_MODE:
  1329. {
  1330. return ( MakePermissionsResult(
  1331. ((AFP_OPEN_MODE_WRITE & GetFileInfo()->afpfile_open_mode)
  1332. ? PERM_FILE_WRITE : 0) |
  1333. ((AFP_OPEN_MODE_READ & GetFileInfo()->afpfile_open_mode)
  1334. ? PERM_FILE_READ : 0) ) );
  1335. }
  1336. default:
  1337. ASSERT(FALSE);
  1338. break;
  1339. }
  1340. return L"";
  1341. }
  1342. BSTR CSfmResourceCookie::QueryResultColumnText( int nCol, CFileMgmtComponentData& /*refcdata*/ )
  1343. {
  1344. if (COLNUM_RESOURCES_NUM_LOCKS == nCol)
  1345. return MakeDwordResult( GetNumOfLocks() );
  1346. return GetColumnText(nCol);
  1347. }
  1348. void CSharePageGeneralSFM::ReadSfmSettings()
  1349. {
  1350. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)m_pvPropertyBlock;
  1351. ASSERT( NULL != pvolumeinfo );
  1352. // CODEWORK I may want to use a string of eight spaces rather than
  1353. // putting the actual password on screen. I may also want to
  1354. // use a confirmation password field.
  1355. // See \\kernel\razzle2\src\sfm\afp\ui\afpmgr\volprop.cxx
  1356. m_strSfmPassword = pvolumeinfo->afpvol_password;
  1357. m_bSfmReadonly = !!(pvolumeinfo->afpvol_props_mask & AFP_VOLUME_READONLY);
  1358. }
  1359. void CSharePageGeneralSFM::WriteSfmSettings()
  1360. {
  1361. AFP_VOLUME_INFO* pvolumeinfo = (AFP_VOLUME_INFO*)m_pvPropertyBlock;
  1362. ASSERT( NULL != pvolumeinfo );
  1363. pvolumeinfo->afpvol_password = const_cast<LPWSTR>((LPCWSTR)m_strSfmPassword);
  1364. pvolumeinfo->afpvol_props_mask &=
  1365. ~(AFP_VOLUME_READONLY);
  1366. if (m_bSfmReadonly)
  1367. pvolumeinfo->afpvol_props_mask |= AFP_VOLUME_READONLY;
  1368. }