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.

1419 lines
48 KiB

  1. /*****************************************************************************\
  2. * ftpsite.cpp - Internal object that manages a single FTP site
  3. \*****************************************************************************/
  4. #include "priv.h"
  5. #include "ftpsite.h"
  6. #include "ftpinet.h"
  7. #include "ftpurl.h"
  8. #include "statusbr.h"
  9. #include "offline.h"
  10. #include <ratings.h>
  11. #include <wininet.h>
  12. #ifdef DEBUG
  13. DWORD g_dwOpenConnections = 0; // Ref Counting Open Connections
  14. #endif // DEBUG
  15. /*****************************************************************************\
  16. * CFtpSite
  17. *
  18. * EEK! RFC 1738 is really scary. FTP sites don't necessarily
  19. * start you at the root, and RFC1738 says that ftp://foo/bar asks
  20. * for the file bar in the DEFAULT directory, not the root!
  21. \*****************************************************************************/
  22. CFtpList * g_FtpSiteCache = NULL; /* The list of all open FTP sites */
  23. void CFtpSite::FlushHint(void)
  24. {
  25. HINTERNET hint = m_hint;
  26. m_hint = NULL;
  27. if (hint)
  28. {
  29. // Our caller needs to be holding the critical section
  30. // while we modify m_hint
  31. ASSERTCRITICAL;
  32. InternetCloseHandle(hint);
  33. // DEBUG_CODE(g_dwOpenConnections--;);
  34. }
  35. }
  36. void CFtpSite::FlushHintCritial(void)
  37. {
  38. ASSERTNONCRITICAL;
  39. ENTERCRITICAL;
  40. FlushHint();
  41. LEAVECRITICAL;
  42. }
  43. void CFtpSite::FlushHintCB(LPVOID pvFtpSite)
  44. {
  45. CFtpSite * pfs = (CFtpSite *) pvFtpSite;
  46. if (pfs)
  47. {
  48. pfs->FlushHint();
  49. pfs->Release();
  50. }
  51. }
  52. /*****************************************************************************\
  53. * An InternetConnect has just completed. Get the motd and cache it.
  54. *
  55. * hint - the connected handle, possibly 0 if error
  56. \*****************************************************************************/
  57. void CFtpSite::CollectMotd(HINTERNET hint)
  58. {
  59. CFtpGlob * pfg = GetFtpResponse(&m_cwe);
  60. ENTERCRITICAL;
  61. m_fMotd = m_pfgMotd ? TRUE : FALSE; // We have a motd
  62. IUnknown_Set(&m_pfgMotd, NULL);
  63. m_pfgMotd = pfg;
  64. LEAVECRITICAL;
  65. }
  66. /*****************************************************************************\
  67. FUNCTION: ReleaseHint
  68. DESCRIPTION:
  69. An FtpDir client is finished with a handle to the FTP site.
  70. Put it into the cache, and throw away what used to be there.
  71. We always keep the most recent handle, because that reduces the
  72. likelihood that the server will close the connection due to extended
  73. inactivity.
  74. The critical section around this entire procedure is important,
  75. else we open up all sorts of really ugly race conditions. E.g.,
  76. the timeout might trigger before we're finished initializing it.
  77. Or somebody might ask for the handle before we're ready.
  78. \*****************************************************************************/
  79. void CFtpSite::ReleaseHint(LPCITEMIDLIST pidlFtpPath, HINTERNET hint)
  80. {
  81. ENTERCRITICAL;
  82. TriggerDelayedAction(&m_hgti); // Kick out the old one
  83. _SetPidl(pidlFtpPath);
  84. m_hint = hint;
  85. if (SUCCEEDED(SetDelayedAction(FlushHintCB, (LPVOID) this, &m_hgti)))
  86. AddRef(); // We just gave away a ref.
  87. else
  88. FlushHint(); // Oh well, can't cache it
  89. LEAVECRITICAL;
  90. }
  91. // NT #362108: We need to set the redirect password for the CFtpSite that
  92. // contains the server, the user name, but a blank password to be redirected
  93. // to the CFtpSite that does have the correct password. This way, if a user
  94. // logs in and doesn't save the password in the URL or the secure cache, we
  95. // then put it in the in memory password cache so it stays valid for that
  96. // "browser" session (defined by process lifetime). We then need to redirect
  97. // future navigations that go to that
  98. HRESULT CFtpSite::_SetRedirPassword(LPCTSTR pszServer, INTERNET_PORT ipPortNum, LPCTSTR pszUser, LPCTSTR pszPassword, LPCITEMIDLIST pidlFtpPath, LPCTSTR pszFragment)
  99. {
  100. TCHAR szUrl[MAX_URL_STRING];
  101. HRESULT hr;
  102. hr = UrlCreate(pszServer, pszUser, TEXT(""), TEXT(""), pszFragment, ipPortNum, NULL, szUrl, ARRAYSIZE(szUrl));
  103. if (EVAL(SUCCEEDED(hr)))
  104. {
  105. LPITEMIDLIST pidlServer;
  106. hr = CreateFtpPidlFromUrl(szUrl, GetCWireEncoding(), NULL, &pidlServer, m_pm, TRUE);
  107. if (SUCCEEDED(hr))
  108. {
  109. LPITEMIDLIST pidl = ILCombine(pidlServer, pidlFtpPath);
  110. if (pidl)
  111. {
  112. CFtpSite * pfsDest = NULL;
  113. // The user name has changed so we need to update the
  114. // CFtpSite with the new user name also.
  115. hr = SiteCache_PidlLookup(pidl, FALSE, m_pm, &pfsDest);
  116. if (SUCCEEDED(hr))
  117. {
  118. pfsDest->SetRedirPassword(pszPassword);
  119. pfsDest->Release();
  120. }
  121. ILFree(pidl);
  122. }
  123. ILFree(pidlServer);
  124. }
  125. }
  126. return hr;
  127. }
  128. HRESULT CFtpSite::_RedirectAndUpdate(LPCTSTR pszServer, INTERNET_PORT ipPortNum, LPCTSTR pszUser, LPCTSTR pszPassword, LPCITEMIDLIST pidlFtpPath, LPCTSTR pszFragment, IUnknown * punkSite, CFtpFolder * pff)
  129. {
  130. TCHAR szUrl[MAX_URL_STRING];
  131. TCHAR szUser[INTERNET_MAX_USER_NAME_LENGTH];
  132. HRESULT hr;
  133. StrCpyN(szUser, pszUser, ARRAYSIZE(szUser)); // Copy because of possible reentrancy
  134. hr = UrlCreate(pszServer, szUser, pszPassword, TEXT(""), pszFragment, ipPortNum, NULL, szUrl, ARRAYSIZE(szUrl));
  135. if (EVAL(SUCCEEDED(hr) && pff))
  136. {
  137. LPITEMIDLIST pidlServer;
  138. hr = CreateFtpPidlFromUrl(szUrl, GetCWireEncoding(), NULL, &pidlServer, m_pm, TRUE);
  139. if (SUCCEEDED(hr))
  140. {
  141. LPITEMIDLIST pidl = ILCombine(pidlServer, pidlFtpPath);
  142. if (pidl)
  143. {
  144. // If the user changed the password, we need to setup a redirect so
  145. // they can return later. (NT #362108)
  146. if (m_pszUser && !StrCmp(m_pszUser, szUser) && StrCmp(m_pszPassword, pszPassword))
  147. {
  148. _SetRedirPassword(pszServer, ipPortNum, szUser, pszPassword, pidlFtpPath, pszFragment);
  149. }
  150. // If the user name changed, set a redirect.
  151. if (!m_pszUser || StrCmp(m_pszUser, szUser))
  152. {
  153. CFtpSite * pfsDest = NULL;
  154. // The user name has changed so we need to update the
  155. // CFtpSite with the new user name also.
  156. hr = SiteCache_PidlLookup(pidl, FALSE, m_pm, &pfsDest);
  157. if (SUCCEEDED(hr))
  158. {
  159. pfsDest->SetRedirPassword(pszPassword);
  160. pfsDest->Release();
  161. }
  162. }
  163. hr = _Redirect(pidl, punkSite, pff);
  164. ILFree(pidl);
  165. }
  166. ILFree(pidlServer);
  167. }
  168. }
  169. return hr;
  170. }
  171. HRESULT CFtpSite::_Redirect(LPITEMIDLIST pidl, IUnknown * punkSite, CFtpFolder * pff)
  172. {
  173. LPITEMIDLIST pidlFull = pff->CreateFullPublicPidl(pidl);
  174. HRESULT hr = E_INVALIDARG;
  175. if (pidlFull)
  176. {
  177. hr = IUnknown_PidlNavigate(punkSite, pidlFull, FALSE);
  178. ASSERT(SUCCEEDED(hr));
  179. ILFree(pidlFull);
  180. }
  181. return hr;
  182. }
  183. /*****************************************************************************\
  184. FUNCTION: _SetDirectory
  185. DESCRIPTION:
  186. When the caller wants a handle to the server, they often want a different
  187. directory than what's in the cache. This function needs to change into
  188. the new directory.
  189. \*****************************************************************************/
  190. HRESULT CFtpSite::_SetDirectory(HINTERNET hint, HWND hwnd, LPCITEMIDLIST pidlNewDir, CStatusBar * psb, int * pnTriesLeft)
  191. {
  192. HRESULT hr = S_OK;
  193. if (pidlNewDir && FtpID_IsServerItemID(pidlNewDir))
  194. pidlNewDir = _ILNext(pidlNewDir); // Skip the server.
  195. ASSERT(m_pidl);
  196. // NT #300889: I would like to cache the dir but sometimes it gets
  197. // out of wack and m_pidl doesn't match the HINTERNET's
  198. // cwd. PERF: This could be fixed in the future but
  199. // this perf tweak isn't work the work now (small gain).
  200. // if (m_pidl && !FtpPidl_IsPathEqual(_ILNext(m_pidl), pidlNewDir))
  201. {
  202. LPITEMIDLIST pidlWithVirtualRoot = NULL;
  203. if (psb)
  204. {
  205. WCHAR wzDisplayPath[MAX_PATH]; // For Statusbar.
  206. if (pidlNewDir && SUCCEEDED(GetDisplayPathFromPidl(pidlNewDir, wzDisplayPath, ARRAYSIZE(wzDisplayPath), TRUE)))
  207. psb->SetStatusMessage(IDS_CHDIR, wzDisplayPath);
  208. else
  209. psb->SetStatusMessage(IDS_CHDIR, L"\\");
  210. }
  211. hr = PidlInsertVirtualRoot(pidlNewDir, &pidlWithVirtualRoot);
  212. if (SUCCEEDED(hr))
  213. {
  214. hr = FtpSetCurrentDirectoryPidlWrap(hint, TRUE, pidlWithVirtualRoot, TRUE, TRUE);
  215. if (SUCCEEDED(hr)) // Ok if failed. (No Access?)
  216. {
  217. hr = _SetPidl(pidlNewDir);
  218. }
  219. else
  220. {
  221. ReleaseHint(NULL, hint); // Nowhere
  222. if (hr == HRESULT_FROM_WIN32(ERROR_FTP_DROPPED))
  223. FlushHintCritial(); // Don't cache dead hint
  224. else
  225. {
  226. DisplayWininetError(hwnd, TRUE, HRESULT_CODE(hr), IDS_FTPERR_TITLE_ERROR, IDS_FTPERR_CHANGEDIR, IDS_FTPERR_WININET, MB_OK, NULL);
  227. *pnTriesLeft = 0; // Make sure we don't keep display UI.
  228. hr = HRESULT_FROM_WIN32(ERROR_CANCELLED);
  229. }
  230. hint = 0;
  231. }
  232. ILFree(pidlWithVirtualRoot);
  233. }
  234. if (psb)
  235. psb->SetStatusMessage(IDS_EMPTY, 0);
  236. }
  237. return hr;
  238. }
  239. /*****************************************************************************\
  240. FUNCTION: _LoginToTheServer
  241. DESCRIPTION:
  242. We want an HINTERNET to do some FTP operation but we don't have one
  243. cached. So, login to create it.
  244. WARNING: This function will be called in a critical section and needs to
  245. return in one. However, it may leave the critical section for a
  246. while.
  247. \*****************************************************************************/
  248. HRESULT CFtpSite::_LoginToTheServer(HWND hwnd, HINTERNET hintDll, HINTERNET * phint, LPCITEMIDLIST pidlFtpPath, CStatusBar * psb, IUnknown * punkSite, CFtpFolder * pff)
  249. {
  250. HRESULT hr = S_OK;
  251. ASSERTCRITICAL;
  252. BOOL fKeepTryingToLogin = FALSE;
  253. BOOL fTryOldPassword = TRUE;
  254. LEAVECRITICALNOASSERT;
  255. TCHAR szUser[INTERNET_MAX_USER_NAME_LENGTH];
  256. TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH];
  257. StrCpyN(szUser, m_pszUser, ARRAYSIZE(szUser));
  258. StrCpyN(szPassword, m_pszPassword, ARRAYSIZE(szPassword));
  259. ASSERT(m_pszServer);
  260. if (psb)
  261. psb->SetStatusMessage(IDS_CONNECTING, m_pszServer);
  262. do
  263. {
  264. hr = InternetConnectWrap(hintDll, TRUE, HANDLE_NULLSTR(m_pszServer), m_ipPortNum, NULL_FOR_EMPTYSTR(szUser), NULL_FOR_EMPTYSTR(szPassword), INTERNET_SERVICE_FTP, 0, 0, phint);
  265. if (*phint)
  266. fKeepTryingToLogin = FALSE; // Move up.
  267. else
  268. {
  269. BOOL fSkipLoginDialog = FALSE;
  270. // Display Login dialog to get new user name/password to try again or cancel login.
  271. // fKeepTryingToLogin = TRUE if Dialog said [LOGIN].
  272. if (((ERROR_INTERNET_LOGIN_FAILURE == HRESULT_CODE(hr)) ||
  273. (ERROR_INTERNET_INCORRECT_USER_NAME == HRESULT_CODE(hr)) ||
  274. (ERROR_INTERNET_INCORRECT_PASSWORD == HRESULT_CODE(hr))) && hwnd)
  275. {
  276. BOOL fIsAnonymous = (!szUser[0] || !StrCmpI(szUser, TEXT("anonymous")) ? TRUE : FALSE);
  277. DWORD dwLoginFlags = (fIsAnonymous ? LOGINFLAGS_ANON_LOGINJUSTFAILED : LOGINFLAGS_USER_LOGINJUSTFAILED);
  278. if (fTryOldPassword)
  279. {
  280. hr = m_cAccount.GetUserName(HANDLE_NULLSTR(m_pszServer), szUser, ARRAYSIZE(szUser));
  281. if (S_OK == hr)
  282. {
  283. hr = m_cAccount.GetPassword(HANDLE_NULLSTR(m_pszServer), szUser, szPassword, ARRAYSIZE(szPassword));
  284. if (S_OK == hr)
  285. {
  286. fKeepTryingToLogin = TRUE;
  287. fSkipLoginDialog = TRUE;
  288. }
  289. }
  290. }
  291. if (!fSkipLoginDialog)
  292. {
  293. // If the user tried to log in anonymously and failed, we want to try
  294. // logging in with a password. If the user tried logging in with a password
  295. // and failed, we want to keep trying to log in with a password.
  296. //
  297. // DisplayLoginDialog returns S_OK for OK pressed, S_FALSE for Cancel button, and
  298. // FAILED() for something is really messed up.
  299. hr = m_cAccount.DisplayLoginDialog(hwnd, dwLoginFlags, HANDLE_NULLSTR(m_pszServer),
  300. szUser, ARRAYSIZE(szUser), szPassword, ARRAYSIZE(szPassword));
  301. }
  302. // S_FALSE means the user cancelled out of the Login dialog.
  303. // We need to turn this into an error value so the caller,
  304. // CFtpDir::WithHint() won't call the callback.
  305. if (S_FALSE == hr)
  306. hr = HRESULT_FROM_WIN32(ERROR_CANCELLED);
  307. fKeepTryingToLogin = (SUCCEEDED(hr) ? TRUE : FALSE);
  308. if (fKeepTryingToLogin)
  309. {
  310. // We need to set the cancelled error so we don't display the
  311. // error message after this.
  312. hr = HRESULT_FROM_WIN32(ERROR_CANCELLED);
  313. }
  314. fTryOldPassword = FALSE;
  315. }
  316. else
  317. fKeepTryingToLogin = FALSE;
  318. }
  319. }
  320. while (fKeepTryingToLogin);
  321. if (!*phint)
  322. {
  323. ASSERT(2 != HRESULT_CODE(hr)); // error 2 = wininet not configured
  324. #ifdef DEBUG
  325. // Gee, I wonder why I couldn't connect, let's find out.
  326. TCHAR szBuff[1500];
  327. InternetGetLastResponseInfoDisplayWrap(FALSE, NULL, szBuff, ARRAYSIZE(szBuff));
  328. // This may happen if the server has too many connections. We may want to sniff
  329. // for this and offer to keep trying. These are the response from the various
  330. // FTP Servers in this case:
  331. // IIS v5: 421 Too many people are connected. Please come back when the server is less busy.
  332. // UNIX: ???
  333. #endif // DEBUG
  334. }
  335. // Was a different login name or password needed in order to login successfully?
  336. else
  337. {
  338. LPITEMIDLIST pidlVirtualDir;
  339. CollectMotd(*phint);
  340. _QueryServerFeatures(*phint);
  341. // Ref Count the open connections.
  342. // DEBUG_CODE(g_dwOpenConnections++;);
  343. // Is it a VMS Server?
  344. if (m_fIsServerVMS)
  345. {
  346. // Yes, so skip getting pidlVirtualDir because wininet gives us
  347. // garbage for FtpGetCurrentDirectoryA().
  348. }
  349. else
  350. {
  351. // NOTE: If the connection isn't annonymous, the server may put the user
  352. // into a sub directory called a virtual root. We need to squirel that
  353. // directory away because it may be needed when going into sub directories
  354. // relative to this virtual root.
  355. // Example: ftp://user1:password@server/ puts you into /users/user1/
  356. // Then: ftp://user1:password@server/dir1 really should be /users/user1/dir1/
  357. hr = FtpGetCurrentDirectoryPidlWrap(*phint, TRUE, GetCWireEncoding(), &pidlVirtualDir);
  358. if (SUCCEEDED(hr))
  359. {
  360. // Are we rooted at '/'? (Meaning no virtual root)
  361. Pidl_Set(&m_pidlVirtualDir, pidlVirtualDir);
  362. ILFree(pidlVirtualDir);
  363. }
  364. }
  365. //DEBUG_CODE(TraceMsg(TF_WININET_DEBUG, "CFtpSite::GetHint() FtpGetCurrentDirectory() returned %#08lx", hr));
  366. if (StrCmp(HANDLE_NULLSTR(m_pszUser), szUser) || StrCmp(HANDLE_NULLSTR(m_pszPassword), szPassword))
  367. {
  368. // Yes, so redirect so the AddressBand and User Status Bar pane update.
  369. // We normally log in with m_pidl because normally we login with
  370. // a default directory ('\') and then change directories to the final location.
  371. // we do this so isolate access denied to the server and access denied to the
  372. // directory.
  373. //
  374. // We pass pidlFtpPath instead in this case because it will tell the browser
  375. // to re-direct and we won't get a chance to do the ChangeDir later.
  376. Str_SetPtr(&m_pszRedirPassword, szPassword);
  377. _RedirectAndUpdate(m_pszServer, m_ipPortNum, szUser, szPassword, pidlFtpPath, m_pszFragment, punkSite, pff);
  378. hr = HRESULT_FROM_WIN32(ERROR_NETWORK_ACCESS_DENIED);
  379. }
  380. }
  381. // Can we assume annonymous logins don't use virtual roots?
  382. ASSERT(FAILED(hr) || (m_pidlVirtualDir && szUser[0]) || !(m_pidlVirtualDir && szUser[0]));
  383. if (psb)
  384. psb->SetStatusMessage(IDS_EMPTY, NULL);
  385. ENTERCRITICALNOASSERT;
  386. // The directory is empty.
  387. _SetPidl(NULL);
  388. return hr;
  389. }
  390. /*****************************************************************************\
  391. FUNCTION: GetHint
  392. DESCRIPTION:
  393. An IShellFolder client wants a handle to the FTP site.
  394. Pull it from the cache if possible.
  395. The caller should have marked the IShellFolder as busy.
  396. EEK! RFC 1738 is really scary. FTP sites don't necessarily
  397. start you at the root, and RFC1738 says that ftp://foo/bar asks
  398. for the file bar in the DEFAULT directory, not the root!
  399. \*****************************************************************************/
  400. HRESULT CFtpSite::GetHint(HWND hwnd, LPCITEMIDLIST pidlFtpPath, CStatusBar * psb, HINTERNET * phint, IUnknown * punkSite, CFtpFolder * pff)
  401. {
  402. HINTERNET hint = NULL;
  403. HINTERNET hintDll = GetWininetSessionHandle();
  404. HRESULT hr = S_OK;
  405. if (!hintDll)
  406. {
  407. // No point in retrying if we can't init Wininet
  408. hr = HRESULT_FROM_WIN32(GetLastError()); // Save error code
  409. }
  410. else
  411. {
  412. int cTriesLeft = 1; // This is a feature that would be cool to implement.
  413. hr = AssureNetConnection(NULL, hwnd, m_pszServer, NULL, TRUE);
  414. if (ILIsEmpty(pidlFtpPath))
  415. pidlFtpPath = NULL;
  416. if (SUCCEEDED(hr))
  417. {
  418. // The CS is protecting m_hint. First guy gets to remove and use the cached internet session.
  419. // subsequent callers need create new ones since internet sessions have state while in use. (Current dir, etc.)
  420. ASSERTNONCRITICAL;
  421. ENTERCRITICALNOASSERT;
  422. do
  423. {
  424. BOOL fReuseExistingConnection = FALSE;
  425. hr = E_FAIL; // We don't have our hint yet...
  426. ASSERTCRITICAL;
  427. hint = (HINTERNET) InterlockedExchangePointer(&m_hint, 0);
  428. if (hint)
  429. {
  430. HINTERNET hintResponse;
  431. TriggerDelayedAction(&m_hgti); // Nothing will happen
  432. fReuseExistingConnection = TRUE; // We will need to change it for the current user.
  433. // We want (S_OK == hr) if our login session is still good. Else, we want to
  434. // re-login.
  435. hr = FtpCommandWrap(hint, FALSE, FALSE, FTP_TRANSFER_TYPE_ASCII, FTP_CMD_NO_OP, NULL, &hintResponse);
  436. if (SUCCEEDED(hr))
  437. {
  438. TraceMsg(TF_FTPOPERATION, "CFtpSite::GetHint() We are going to use a cached HINTERNET.");
  439. InternetCloseHandleWrap(hintResponse, TRUE);
  440. }
  441. else
  442. {
  443. TraceMsg(TF_FTPOPERATION, "CFtpSite::GetHint() Can't used cached HINTERNET because server didn't respond to NOOP.");
  444. InternetCloseHandleWrap(hint, TRUE);
  445. }
  446. }
  447. if (FAILED(hr))
  448. {
  449. hr = _LoginToTheServer(hwnd, hintDll, &hint, pidlFtpPath, psb, punkSite, pff);
  450. TraceMsg(TF_FTPOPERATION, "CFtpSite::GetHint() We had to login because we didn't have a cached HINTERNET.");
  451. }
  452. ASSERTCRITICAL;
  453. LEAVECRITICALNOASSERT;
  454. // Do we need to CD into a specific directory? Yes, if...
  455. // 1. We succeeded above, AND
  456. // 2. We are already using a connection so the dir may be incorrect, OR
  457. // 3. We need a non-default dir.
  458. if (SUCCEEDED(hr) && (fReuseExistingConnection || pidlFtpPath)) // pidlFtpPath may be NULL.
  459. hr = _SetDirectory(hint, hwnd, pidlFtpPath, psb, &cTriesLeft);
  460. ENTERCRITICALNOASSERT;
  461. ASSERTCRITICAL;
  462. }
  463. while (hr == HRESULT_FROM_WIN32(ERROR_FTP_DROPPED) && --cTriesLeft);
  464. LEAVECRITICALNOASSERT;
  465. }
  466. }
  467. *phint = hint;
  468. return hr;
  469. }
  470. HRESULT CFtpSite::_CheckToEnableCHMOD(LPCWIRESTR pwResponse)
  471. {
  472. HRESULT hr = S_FALSE;
  473. // TODO: We should probably be more restictive in how we parse the
  474. // response. We should probably verify there is some kind of
  475. // white space before and after the command.
  476. LPCWIRESTR pwCommand = StrStrIA(pwResponse, FTP_UNIXCMD_CHMODA);
  477. // Does this FTP server support the "SITE CHMOD" command?
  478. if (pwCommand)
  479. {
  480. // Yes, so we may want to use it later.
  481. m_fIsCHMODSupported = TRUE;
  482. // We can later respond with:
  483. // "SITE chmod xyz FileName.txt"
  484. // x is for Owner, (4=Read, 2=Write, 1=Execute)
  485. // y is for Owner, (4=Read, 2=Write, 1=Execute)
  486. // z is for Owner, (4=Read, 2=Write, 1=Execute)
  487. }
  488. return hr;
  489. }
  490. /*****************************************************************************\
  491. FUNCTION: _QueryServerFeatures
  492. DESCRIPTION:
  493. Find out what the server is and isn't capable of. Information we could
  494. use:
  495. SITE: Find out OS specific commands that may be useful. "chmod" is one
  496. of them.
  497. HELP SITE: Find out what the OS supports.
  498. SYST: Find out the OS type.
  499. NOOP: See if the connection is still alive.
  500. MLST: Unambiguous Directory listing with dates in UTC.
  501. MLSD:
  502. FEAT: Features supported. UTF8 is the one we care about.
  503. Response to "SITE HELP" for these servers:
  504. UNIX Type: L8 Version: BSD-199506
  505. UNIX Type: L8
  506. UMASK CHMOD GROUP NEWER INDEX ALIAS GROUPS
  507. IDLE HELP GPASS MINFO EXEC CDPATH
  508. Windows_NT version 4.0
  509. CKM DIRSTYLE HELP STATS
  510. \*****************************************************************************/
  511. HRESULT CFtpSite::_QueryServerFeatures(HINTERNET hint)
  512. {
  513. HRESULT hr = E_FAIL;
  514. HINTERNET hintResponse;
  515. // Can we turn on 'UTF8' encoding?
  516. if (SUCCEEDED(FtpCommandWrap(hint, FALSE, FALSE, FTP_TRANSFER_TYPE_ASCII, FTP_CMD_UTF8, NULL, &hintResponse)))
  517. {
  518. m_fInUTF8Mode = TRUE;
  519. m_cwe.SetUTF8Support(TRUE);
  520. TraceMsg(TF_FTP_OTHER, "_QueryServerFeatures() in UTF8 Mode");
  521. InternetCloseHandleWrap(hintResponse, TRUE);
  522. }
  523. else
  524. {
  525. TraceMsg(TF_FTP_OTHER, "_QueryServerFeatures() NOT in UTF8 Mode");
  526. m_fInUTF8Mode = FALSE;
  527. }
  528. if (!m_fFeaturesQueried)
  529. {
  530. // Is type of server software is running? We want to know if we are running
  531. // on VMS, because in that case we want to fall back to HTML view (URLMON).
  532. // This is because the wininet guys don't want to support it.
  533. if (SUCCEEDED(FtpCommandWrap(hint, FALSE, FALSE, FTP_TRANSFER_TYPE_ASCII, FTP_CMD_SYSTEM, NULL, &hintResponse)))
  534. {
  535. DWORD dwError;
  536. WIRECHAR wResponse[MAX_URL_STRING];
  537. DWORD cchSize = ARRAYSIZE(wResponse);
  538. if (SUCCEEDED(InternetGetLastResponseInfoWrap(TRUE, &dwError, wResponse, &cchSize)))
  539. {
  540. // Is this a VMS server?
  541. if (StrStrIA(wResponse, FTP_SYST_VMS))
  542. m_fIsServerVMS = TRUE;
  543. TraceMsg(TF_FTP_OTHER, "_QueryServerFeatures() SYSTM returned %hs.", wResponse);
  544. }
  545. InternetCloseHandleWrap(hintResponse, TRUE);
  546. }
  547. #ifdef FEATURE_CHANGE_PERMISSIONS
  548. // Is the server capable of supporting the UNIX "chmod" command
  549. // to change permissions on the file?
  550. if (SUCCEEDED(FtpCommandWrap(hint, FALSE, FALSE, FTP_TRANSFER_TYPE_ASCII, FTP_CMD_SITE_HELP, NULL, &hintResponse)))
  551. {
  552. DWORD dwError;
  553. WIRECHAR wResponse[MAX_URL_STRING];
  554. DWORD cchSize = ARRAYSIZE(wResponse);
  555. if (SUCCEEDED(InternetGetLastResponseInfoWrap(TRUE, &dwError, wResponse, &cchSize)))
  556. {
  557. _CheckToEnableCHMOD(wResponse);
  558. // TraceMsg(TF_FTP_OTHER, "_QueryServerFeatures() SITE HELP returned success");
  559. }
  560. InternetCloseHandleWrap(hintResponse, TRUE);
  561. }
  562. #endif // FEATURE_CHANGE_PERMISSIONS
  563. /*
  564. // Is the server capable of supporting the UNIX "chmod" command
  565. // to change permissions on the file?
  566. if (SUCCEEDED(FtpCommandWrap(hint, FALSE, FALSE, FTP_TRANSFER_TYPE_ASCII, FTP_CMD_SITE, NULL, &hintResponse)))
  567. {
  568. DWORD dwError;
  569. WIRECHAR wResponse[MAX_URL_STRING];
  570. DWORD cchSize = ARRAYSIZE(wResponse);
  571. if (SUCCEEDED(InternetGetLastResponseInfoWrap(TRUE, &dwError, wResponse, &cchSize)))
  572. {
  573. TraceMsg(TF_FTP_OTHER, "_QueryServerFeatures() SITE returned succeess");
  574. }
  575. InternetCloseHandleWrap(hintResponse, TRUE);
  576. }
  577. */
  578. m_fFeaturesQueried = TRUE;
  579. }
  580. return S_OK; // This shouldn't fail.
  581. }
  582. LPITEMIDLIST CFtpSite::GetPidl(void)
  583. {
  584. return ILClone(m_pidl);
  585. }
  586. /*****************************************************************************\
  587. FUNCTION: _SetPidl
  588. DESCRIPTION:
  589. m_pidl contains the ServerID and the ItemIDs making up the path of where
  590. m_hint is currently located. This function will take a new path in pidlFtpPath
  591. and update m_pidl so it still has the server.
  592. \*****************************************************************************/
  593. HRESULT CFtpSite::_SetPidl(LPCITEMIDLIST pidlFtpPath)
  594. {
  595. HRESULT hr = E_FAIL;
  596. LPITEMIDLIST pidlServer = FtpCloneServerID(m_pidl);
  597. if (pidlServer)
  598. {
  599. LPITEMIDLIST pidlNew = ILCombine(pidlServer, pidlFtpPath);
  600. if (pidlNew)
  601. {
  602. ILFree(m_pidl);
  603. m_pidl = pidlNew;
  604. hr = S_OK;
  605. }
  606. ILFree(pidlServer);
  607. }
  608. return hr;
  609. }
  610. /*****************************************************************************\
  611. FUNCTION: QueryMotd
  612. DESCRIPTION:
  613. Determine whether there is a motd at all.
  614. \*****************************************************************************/
  615. BOOL CFtpSite::QueryMotd(void)
  616. {
  617. return m_fMotd;
  618. }
  619. HRESULT CFtpSite::GetVirtualRoot(LPITEMIDLIST * ppidl)
  620. {
  621. HRESULT hr = S_FALSE;
  622. *ppidl = NULL;
  623. if (m_pidlVirtualDir)
  624. {
  625. *ppidl = ILClone(m_pidlVirtualDir);
  626. hr = S_OK;
  627. }
  628. return S_OK;
  629. }
  630. HRESULT CFtpSite::PidlInsertVirtualRoot(LPCITEMIDLIST pidlFtpPath, LPITEMIDLIST * ppidl)
  631. {
  632. HRESULT hr = S_OK;
  633. *ppidl = NULL;
  634. if (!m_pidlVirtualDir)
  635. *ppidl = ILClone(pidlFtpPath);
  636. else
  637. {
  638. LPITEMIDLIST pidlTemp = NULL;
  639. if (pidlFtpPath && FtpID_IsServerItemID(pidlFtpPath))
  640. {
  641. pidlTemp = FtpCloneServerID(pidlFtpPath);
  642. pidlFtpPath = _ILNext(pidlFtpPath);
  643. }
  644. LPITEMIDLIST pidlWithVRoot = ILCombine(pidlTemp, m_pidlVirtualDir);
  645. if (pidlWithVRoot)
  646. {
  647. *ppidl = ILCombine(pidlWithVRoot, pidlFtpPath);
  648. ILFree(pidlWithVRoot);
  649. }
  650. ILFree(pidlTemp);
  651. }
  652. return S_OK;
  653. }
  654. BOOL CFtpSite::HasVirtualRoot(void)
  655. {
  656. return (m_pidlVirtualDir ? TRUE : FALSE);
  657. }
  658. /*****************************************************************************\
  659. GetMotd
  660. Returns the HFGLOB that babysits the motd. The refcount has been
  661. incremented.
  662. \*****************************************************************************/
  663. CFtpGlob * CFtpSite::GetMotd(void)
  664. {
  665. if (m_pfgMotd)
  666. m_pfgMotd->AddRef();
  667. return m_pfgMotd;
  668. }
  669. /*****************************************************************************\
  670. GetCFtpList
  671. Return the CFtpList * that remembers which folders live in this CFtpSite *.
  672. WARNING! The caller must own the critical section when calling
  673. this routine, because the returned CFtpList * is not refcounted!
  674. \*****************************************************************************/
  675. CFtpList * CFtpSite::GetCFtpList(void)
  676. {
  677. return m_FtpDirList;
  678. }
  679. /*****************************************************************************\
  680. _CompareSites
  681. Callback during SiteCache_PrivSearch to see if the site is already in the
  682. list.
  683. \*****************************************************************************/
  684. int CALLBACK _CompareSites(LPVOID pvStrSite, LPVOID pvFtpSite, LPARAM lParam)
  685. {
  686. CFtpSite * pfs = (CFtpSite *) pvFtpSite;
  687. LPCTSTR pszLookupStrNew = (LPCTSTR) pvStrSite;
  688. LPCTSTR pszLookupStr = (pfs->m_pszLookupStr ? pfs->m_pszLookupStr : TEXT(""));
  689. ASSERT(pszLookupStr && pszLookupStr);
  690. return StrCmpI(pszLookupStr, pszLookupStrNew);
  691. }
  692. /*****************************************************************************\
  693. FUNCTION: SiteCache_PrivSearch
  694. DESCRIPTION:
  695. We cache information about an FTP Server to prevent hitting the net all
  696. the time. This state is stored in CFtpSite objects and we use 'lookup strings'
  697. to find them. This is what makes one server different from another. Since
  698. we store password state in a CFtpSite object, we need to have one per
  699. user/password combo.
  700. \*****************************************************************************/
  701. HRESULT SiteCache_PrivSearch(LPCTSTR pszLookup, LPCITEMIDLIST pidl, IMalloc * pm, CFtpSite ** ppfs)
  702. {
  703. CFtpSite * pfs = NULL;
  704. HRESULT hr = S_OK;
  705. ENTERCRITICAL;
  706. // CFtpSite_Init() can fail in low memory
  707. if (SUCCEEDED(CFtpSite_Init()))
  708. {
  709. pfs = (CFtpSite *) g_FtpSiteCache->Find(_CompareSites, (LPVOID)pszLookup); // Add CFtpSite:: ?
  710. if (!pfs)
  711. {
  712. // We need to hold the critical section while setting up
  713. // the new CFtpSite structure, lest somebody else come in
  714. // and try to create the same CFtpSite while we are busy.
  715. hr = CFtpSite_Create(pidl, pszLookup, pm, &pfs);
  716. if (SUCCEEDED(hr))
  717. {
  718. hr = g_FtpSiteCache->AppendItem(pfs);
  719. if (!(SUCCEEDED(hr)))
  720. IUnknown_Set(&pfs, NULL);
  721. }
  722. }
  723. }
  724. *ppfs = pfs;
  725. if (pfs)
  726. pfs->AddRef();
  727. LEAVECRITICAL;
  728. ASSERT_POINTER_MATCHES_HRESULT(*ppfs, hr);
  729. return hr;
  730. }
  731. /*****************************************************************************\
  732. FUNCTION: SiteCache_PidlLookupPrivHelper
  733. DESCRIPTION:
  734. We cache information about an FTP Server to prevent hitting the net all
  735. the time. This state is stored in CFtpSite objects and we use 'lookup strings'
  736. to find them. This is what makes one server different from another. Since
  737. we store password state in a CFtpSite object, we need to have one per
  738. user/password combo.
  739. SiteCache_PidlLookup() does the high level work of deciding if we want
  740. to do a password redirect. This function just wraps the creating of the
  741. lookup string and the fetching of the site.
  742. \*****************************************************************************/
  743. HRESULT SiteCache_PidlLookupPrivHelper(LPCITEMIDLIST pidl, IMalloc * pm, CFtpSite ** ppfs)
  744. {
  745. HRESULT hr = E_FAIL;
  746. TCHAR szLookup[MAX_PATH];
  747. *ppfs = NULL;
  748. hr = PidlGenerateSiteLookupStr(pidl, szLookup, ARRAYSIZE(szLookup));
  749. // May fail w/Outofmemory
  750. if (SUCCEEDED(hr))
  751. hr = SiteCache_PrivSearch((pidl ? szLookup : TEXT('\0')), pidl, pm, ppfs);
  752. ASSERT_POINTER_MATCHES_HRESULT(*ppfs, hr);
  753. return hr;
  754. }
  755. /*****************************************************************************\
  756. FUNCTION: SiteCache_PidlLookupPrivHelper
  757. DESCRIPTION:
  758. We cache information about an FTP Server to prevent hitting the net all
  759. the time. This state is stored in CFtpSite objects and we use 'lookup strings'
  760. to find them. This is what makes one server different from another. Since
  761. we store password state in a CFtpSite object, we need to have one per
  762. user/password combo.
  763. \*****************************************************************************/
  764. HRESULT SiteCache_PidlLookup(LPCITEMIDLIST pidl, BOOL fPasswordRedir, IMalloc * pm, CFtpSite ** ppfs)
  765. {
  766. HRESULT hr = E_FAIL;
  767. if (pidl && !ILIsEmpty(pidl))
  768. {
  769. hr = SiteCache_PidlLookupPrivHelper(pidl, pm, ppfs);
  770. // Okay, we found a site but we may need to redirect to another site
  771. // because the password is wrong. This happens if a user goes to
  772. // ServerA w/UserA and PasswordA but PasswordA is invalid. So,
  773. // PasswordB is entered and the navigation completes successfully.
  774. // Now either the navigation occurs again with PasswordA or w/o
  775. // a password (because the addrbar removes it), then we need to
  776. // look it up again and get it.
  777. if (SUCCEEDED(hr) && (*ppfs)->m_pszRedirPassword && fPasswordRedir)
  778. {
  779. LPITEMIDLIST pidlNew; // with new (redirected) password
  780. if (FtpPidl_IsAnonymous(pidl))
  781. {
  782. pidlNew = ILClone(pidl);
  783. if (!pidlNew)
  784. hr = E_OUTOFMEMORY;
  785. }
  786. else
  787. {
  788. // We need to redirect to get that CFtpSite.
  789. hr = PidlReplaceUserPassword(pidl, &pidlNew, pm, NULL, (*ppfs)->m_pszRedirPassword);
  790. }
  791. (*ppfs)->Release();
  792. *ppfs = NULL;
  793. if (SUCCEEDED(hr))
  794. {
  795. hr = SiteCache_PidlLookupPrivHelper(pidlNew, pm, ppfs);
  796. ILFree(pidlNew);
  797. }
  798. }
  799. }
  800. ASSERT_POINTER_MATCHES_HRESULT(*ppfs, hr);
  801. return hr;
  802. }
  803. /*****************************************************************************\
  804. FUNCTION: UpdateHiddenPassword
  805. DESCRIPTION:
  806. Since our IShellFolder::GetDisplayNameOf() will hide the password in some
  807. cases, we need to 'patch' display names that come thru our
  808. IShellFolder::GetDisplayName(). If a display name is coming in, we will
  809. see if the CFtpSite has a m_pszRedirPassword. If it did, then the user entered
  810. a password via the 'Login As...' dialog in place of the empty password,
  811. which made it hidden. If this is the case, we then have IShellFolder::ParseDisplayName()
  812. patch back in the password.
  813. \*****************************************************************************/
  814. HRESULT CFtpSite::UpdateHiddenPassword(LPITEMIDLIST pidl)
  815. {
  816. HRESULT hr = S_FALSE;
  817. TCHAR szUserName[INTERNET_MAX_USER_NAME_LENGTH];
  818. TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH];
  819. // Is it a candidate to a password to be inserted?
  820. if (m_pszPassword &&
  821. EVAL(SUCCEEDED(FtpPidl_GetUserName(pidl, szUserName, ARRAYSIZE(szUserName)))) &&
  822. szUserName[0] &&
  823. SUCCEEDED(FtpPidl_GetPassword(pidl, szPassword, ARRAYSIZE(szPassword), TRUE)) &&
  824. !szPassword[0])
  825. {
  826. // Yes...
  827. hr = FtpServerID_SetHiddenPassword(pidl, m_pszPassword);
  828. }
  829. return hr;
  830. }
  831. /*****************************************************************************\
  832. CFtpSite::GetFtpDir
  833. \*****************************************************************************/
  834. HRESULT CFtpSite::GetFtpDir(LPCTSTR pszServer, LPCWSTR pszUrlPath, CFtpDir ** ppfd)
  835. {
  836. HRESULT hr = S_OK;
  837. TCHAR szUrl[MAX_URL_STRING];
  838. *ppfd = NULL;
  839. hr = UrlCreate(pszServer, NULL, NULL, pszUrlPath, NULL, INTERNET_DEFAULT_FTP_PORT, NULL, szUrl, ARRAYSIZE(szUrl)); // Cannot fail on valid URLs.
  840. if (EVAL(SUCCEEDED(hr)))
  841. {
  842. LPITEMIDLIST pidl;
  843. // We know this is a path.
  844. hr = CreateFtpPidlFromUrlEx(szUrl, GetCWireEncoding(), NULL, &pidl, m_pm, FALSE, TRUE, TRUE); // Can fail in out of memory
  845. if (SUCCEEDED(hr))
  846. {
  847. hr = GetFtpDir(pidl, ppfd);
  848. ILFree(pidl);
  849. }
  850. }
  851. return hr;
  852. }
  853. /*****************************************************************************\
  854. FUNCTION: GetFtpDir
  855. DESCRIPTION:
  856. Obtain the FtpDir structure for an FTP site, creating one if
  857. necessary. It is the caller's responsibility to Release the
  858. FtpDir when finished.
  859. \*****************************************************************************/
  860. HRESULT CFtpSite::GetFtpDir(LPCITEMIDLIST pidl, CFtpDir ** ppfd)
  861. {
  862. HRESULT hr = S_OK;
  863. CFtpDir * pfd = NULL;
  864. ENTERCRITICAL;
  865. ASSERT(ppfd && m_FtpDirList);
  866. pfd = (CFtpDir *) m_FtpDirList->Find(_CompareDirs, (LPVOID) pidl);
  867. if (!pfd)
  868. {
  869. // We need to hold the critical section while setting up
  870. // the new FtpDir structure, lest somebody else come in
  871. // and try to create the same FtpDir while we are busy.
  872. hr = CFtpDir_Create(this, pidl, &pfd);
  873. if (SUCCEEDED(hr))
  874. {
  875. // NOTE: REF-COUNTING
  876. // Note that CFtpDir has a pointer (m_pfs) to a CFtpSite.
  877. // We just added a back pointer in CFtpSite's list of CFtpDir(s),
  878. // so it's necessary for that back pointer to not have a ref.
  879. // This will not be a problem because the back pointers will
  880. // always be valid because: 1) CFtpDir's destructor removes the backpointer,
  881. // and 2) CFtpDir holds a ref on CFtpSite, so it won't go away until
  882. // all the CFtpDir(s) are good and ready. -BryanSt
  883. hr = m_FtpDirList->AppendItem(pfd);
  884. if (FAILED(hr))
  885. IUnknown_Set(&pfd, NULL);
  886. }
  887. }
  888. LEAVECRITICAL;
  889. *ppfd = pfd;
  890. if (pfd)
  891. pfd->AddRef();
  892. return hr;
  893. }
  894. /*****************************************************************************\
  895. FUNCTION: FlushSubDirs
  896. DESCRIPTION:
  897. Every subdir of pidl is no longer valid so flush them. This is done
  898. because the parent dir may have changed names so they are invalid.
  899. PARAMETERS:
  900. pidl: Path of ItemIDs (no-ServerID) that includes the full path w/o
  901. the virtual root. This matches CFtpDir::m_pidlFtpDir
  902. \*****************************************************************************/
  903. HRESULT CFtpSite::FlushSubDirs(LPCITEMIDLIST pidl)
  904. {
  905. HRESULT hr = S_OK;
  906. CFtpDir * pfd = NULL;
  907. int nIndex;
  908. ENTERCRITICAL;
  909. // Count down so deleting items won't mess up the indicies.
  910. for (nIndex = (m_FtpDirList->GetCount() - 1); nIndex >= 0; nIndex--)
  911. {
  912. pfd = (CFtpDir *) m_FtpDirList->GetItemPtr(nIndex);
  913. if (pfd)
  914. {
  915. // Is this a child?
  916. if (FtpItemID_IsParent(pidl, pfd->GetPathPidlReference()))
  917. {
  918. // Yes, pfd is a child of pidl so delete it.
  919. m_FtpDirList->DeletePtrByIndex(nIndex);
  920. pfd->Release();
  921. }
  922. }
  923. }
  924. LEAVECRITICAL;
  925. return hr;
  926. }
  927. BOOL CFtpSite::IsSiteBlockedByRatings(HWND hwndDialogOwner)
  928. {
  929. if (!m_fRatingsChecked)
  930. {
  931. void * pvRatingDetails = NULL;
  932. TCHAR szUrl[MAX_URL_STRING];
  933. CHAR szUrlAnsi[MAX_URL_STRING];
  934. HRESULT hr = S_OK; // Assume allowed (in case no ratings)
  935. EVAL(SUCCEEDED(UrlCreateFromPidlW(m_pidl, SHGDN_FORPARSING, szUrl, ARRAYSIZE(szUrl), (ICU_ESCAPE | ICU_USERNAME), FALSE)));
  936. SHTCharToAnsi(szUrl, szUrlAnsi, ARRAYSIZE(szUrlAnsi));
  937. if (IS_RATINGS_ENABLED())
  938. {
  939. // S_OK - Allowed, S_FALSE - Not Allowed, FAILED() - not rated.
  940. hr = RatingCheckUserAccess(NULL, szUrlAnsi, NULL, NULL, 0, &pvRatingDetails);
  941. if (S_OK != hr) // Does user want to override with parent password in dialog?
  942. hr = RatingAccessDeniedDialog2(hwndDialogOwner, NULL, pvRatingDetails);
  943. if (pvRatingDetails)
  944. RatingFreeDetails(pvRatingDetails);
  945. }
  946. if (S_OK == hr) // It's off by default.
  947. m_fRatingsAllow = TRUE;
  948. m_fRatingsChecked = TRUE;
  949. }
  950. return !m_fRatingsAllow;
  951. }
  952. /*****************************************************************************\
  953. CFtpSite_Init
  954. Initialize the global list of FTP sites.
  955. Note that the DLL refcount is decremented after this is created,
  956. so that this internal list doesn't prevent us from unloading.
  957. \*****************************************************************************/
  958. HRESULT CFtpSite_Init(void)
  959. {
  960. HRESULT hr = S_OK;
  961. if (!g_FtpSiteCache)
  962. hr = CFtpList_Create(10, NULL, 10, &g_FtpSiteCache);
  963. return hr;
  964. }
  965. /*****************************************************************************\
  966. FtpSitePurge_CallBack
  967. Purge the global list of FTP sites.
  968. \*****************************************************************************/
  969. int FtpSitePurge_CallBack(LPVOID pvPunk, LPVOID pv)
  970. {
  971. IUnknown * punk = (IUnknown *) pvPunk;
  972. if (punk)
  973. punk->Release();
  974. return 1;
  975. }
  976. /*****************************************************************************\
  977. CFtpPunkList_Purge
  978. Purge the global list of FTP sites.
  979. \*****************************************************************************/
  980. HRESULT CFtpPunkList_Purge(CFtpList ** pfl)
  981. {
  982. TraceMsg(TF_FTP_DLLLOADING, "CFtpPunkList_Purge() Purging our cache.");
  983. if (*pfl)
  984. {
  985. (*pfl)->Enum(FtpSitePurge_CallBack, NULL);
  986. IUnknown_Set(pfl, NULL);
  987. }
  988. return S_OK;
  989. }
  990. /*****************************************************************************\
  991. CFtpSite_Create
  992. Create a brand new CFtpSite given a name.
  993. \*****************************************************************************/
  994. HRESULT CFtpSite_Create(LPCITEMIDLIST pidl, LPCTSTR pszLookupStr, IMalloc * pm, CFtpSite ** ppfs)
  995. {
  996. CFtpSite * pfs = new CFtpSite();
  997. HRESULT hr = E_OUTOFMEMORY;
  998. ASSERT(pidl && pszLookupStr && ppfs);
  999. *ppfs = NULL;
  1000. if (pfs)
  1001. {
  1002. Str_SetPtr(&pfs->m_pszLookupStr, pszLookupStr);
  1003. IUnknown_Set((IUnknown **) &(pfs->m_pm), pm);
  1004. hr = CFtpList_Create(10, NULL, 10, &pfs->m_FtpDirList);
  1005. if (SUCCEEDED(hr))
  1006. {
  1007. // Did someone give us an empty URL?
  1008. if (EVAL(pidl) && EVAL(FtpPidl_IsValid(pidl)))
  1009. {
  1010. TCHAR szServer[INTERNET_MAX_HOST_NAME_LENGTH];
  1011. TCHAR szUser[INTERNET_MAX_USER_NAME_LENGTH];
  1012. TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH];
  1013. TCHAR szFragment[INTERNET_MAX_PASSWORD_LENGTH];
  1014. EVAL(SUCCEEDED(FtpPidl_GetServer(pidl, szServer, ARRAYSIZE(szServer))));
  1015. Str_SetPtr(&pfs->m_pszServer, szServer);
  1016. Pidl_Set(&pfs->m_pidl, pidl);
  1017. EVAL(SUCCEEDED(FtpPidl_GetUserName(pidl, szUser, ARRAYSIZE(szUser))));
  1018. Str_SetPtr(&pfs->m_pszUser, szUser);
  1019. if (FAILED(FtpPidl_GetPassword(pidl, szPassword, ARRAYSIZE(szPassword), TRUE)))
  1020. {
  1021. // Password expired
  1022. szPassword[0] = 0;
  1023. }
  1024. Str_SetPtr(&pfs->m_pszPassword, szPassword);
  1025. FtpPidl_GetFragment(pidl, szFragment, ARRAYSIZE(szFragment));
  1026. Str_SetPtr(&pfs->m_pszFragment, szFragment);
  1027. pfs->m_ipPortNum = FtpPidl_GetPortNum(pidl);
  1028. switch (FtpPidl_GetDownloadType(pidl))
  1029. {
  1030. case FTP_TRANSFER_TYPE_UNKNOWN:
  1031. pfs->m_fDLTypeSpecified = FALSE;
  1032. pfs->m_fASCIIDownload = FALSE;
  1033. break;
  1034. case FTP_TRANSFER_TYPE_ASCII:
  1035. pfs->m_fDLTypeSpecified = TRUE;
  1036. pfs->m_fASCIIDownload = TRUE;
  1037. break;
  1038. case FTP_TRANSFER_TYPE_BINARY:
  1039. pfs->m_fDLTypeSpecified = TRUE;
  1040. pfs->m_fASCIIDownload = FALSE;
  1041. break;
  1042. default:
  1043. ASSERT(0);
  1044. }
  1045. }
  1046. else
  1047. {
  1048. Str_SetPtr(&pfs->m_pszServer, NULL);
  1049. Str_SetPtr(&pfs->m_pszUser, NULL);
  1050. Str_SetPtr(&pfs->m_pszPassword, NULL);
  1051. Str_SetPtr(&pfs->m_pszFragment, NULL);
  1052. Pidl_Set(&pfs->m_pidl, NULL);
  1053. pfs->m_fDLTypeSpecified = FALSE;
  1054. }
  1055. *ppfs = pfs;
  1056. }
  1057. else
  1058. {
  1059. hr = E_FAIL;
  1060. pfs->Release();
  1061. }
  1062. }
  1063. return hr;
  1064. }
  1065. /****************************************************\
  1066. Constructor
  1067. \****************************************************/
  1068. CFtpSite::CFtpSite() : m_cRef(1)
  1069. {
  1070. DllAddRef();
  1071. // This needs to be allocated in Zero Inited Memory.
  1072. // Assert that all Member Variables are inited to Zero.
  1073. ASSERT(!m_pszServer);
  1074. ASSERT(!m_pidl);
  1075. ASSERT(!m_pszUser);
  1076. ASSERT(!m_pszPassword);
  1077. ASSERT(!m_pszFragment);
  1078. ASSERT(!m_pszLookupStr);
  1079. ASSERT(!m_pidlVirtualDir);
  1080. ASSERT(!m_fMotd);
  1081. ASSERT(!m_hint);
  1082. ASSERT(!m_hgti);
  1083. ASSERT(!m_FtpDirList);
  1084. ASSERT(!m_fRatingsChecked);
  1085. ASSERT(!m_fRatingsAllow);
  1086. LEAK_ADDREF(LEAK_CFtpSite);
  1087. }
  1088. /****************************************************\
  1089. Destructor
  1090. \****************************************************/
  1091. CFtpSite::~CFtpSite()
  1092. {
  1093. FlushHint(); // Frees m_hgti
  1094. Str_SetPtr(&m_pszServer, NULL);
  1095. Str_SetPtr(&m_pszUser, NULL);
  1096. Str_SetPtr(&m_pszPassword, NULL);
  1097. Str_SetPtr(&m_pszFragment, NULL);
  1098. Str_SetPtr(&m_pszLookupStr, NULL);
  1099. Str_SetPtr(&m_pszRedirPassword, NULL);
  1100. Pidl_Set(&m_pidlVirtualDir, NULL);
  1101. Pidl_Set(&m_pidl, NULL);
  1102. IUnknown_Set(&m_pfgMotd, NULL);
  1103. ASSERTCRITICAL;
  1104. CFtpPunkList_Purge(&m_FtpDirList);
  1105. TriggerDelayedAction(&m_hgti); // Out goes the cached handle
  1106. ASSERT(m_hint == 0); // Make sure he's gone
  1107. ATOMICRELEASE(m_pm);
  1108. DllRelease();
  1109. LEAK_DELREF(LEAK_CFtpSite);
  1110. }
  1111. //===========================
  1112. // *** IUnknown Interface ***
  1113. ULONG CFtpSite::AddRef()
  1114. {
  1115. m_cRef++;
  1116. return m_cRef;
  1117. }
  1118. ULONG CFtpSite::Release()
  1119. {
  1120. ASSERT(m_cRef > 0);
  1121. m_cRef--;
  1122. if (m_cRef > 0)
  1123. return m_cRef;
  1124. delete this;
  1125. return 0;
  1126. }
  1127. HRESULT CFtpSite::QueryInterface(REFIID riid, void **ppvObj)
  1128. {
  1129. if (IsEqualIID(riid, IID_IUnknown))
  1130. {
  1131. *ppvObj = SAFECAST(this, IUnknown*);
  1132. }
  1133. else
  1134. {
  1135. TraceMsg(TF_FTPQI, "CFtpSite::QueryInterface() failed.");
  1136. *ppvObj = NULL;
  1137. return E_NOINTERFACE;
  1138. }
  1139. AddRef();
  1140. return S_OK;
  1141. }