FileZillaIntf.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //---------------------------------------------------------------------------
  2. #include "stdafx.h"
  3. //---------------------------------------------------------------------------
  4. #include "FileZillaIntf.h"
  5. #include "FileZillaIntern.h"
  6. //---------------------------------------------------------------------------
  7. #ifndef _DEBUG
  8. #pragma comment(lib, "uafxcw.lib")
  9. #else
  10. #pragma comment(lib, "uafxcwd.lib")
  11. #endif
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. void __fastcall TFileZillaIntf::Initialize()
  16. {
  17. // noop
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TFileZillaIntf::Finalize()
  21. {
  22. // noop
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TFileZillaIntf::SetResourceModule(void * ResourceHandle)
  26. {
  27. // set afx resource handles, taken from AfxWinInit (mfc/appinit.cpp)
  28. AFX_MODULE_STATE * ModuleState = AfxGetModuleState();
  29. ModuleState->m_hCurrentInstanceHandle = (HINSTANCE)ResourceHandle;
  30. ModuleState->m_hCurrentResourceHandle = (HINSTANCE)ResourceHandle;
  31. }
  32. //---------------------------------------------------------------------------
  33. __fastcall TFileZillaIntf::TFileZillaIntf() :
  34. FFileZillaApi(NULL),
  35. FIntern(new TFileZillaIntern(this)),
  36. FServer(new t_server)
  37. {
  38. }
  39. //---------------------------------------------------------------------------
  40. __fastcall TFileZillaIntf::~TFileZillaIntf()
  41. {
  42. ASSERT(FFileZillaApi == NULL);
  43. delete FIntern;
  44. FIntern = NULL;
  45. delete FServer;
  46. FServer = NULL;
  47. }
  48. //---------------------------------------------------------------------------
  49. bool __fastcall TFileZillaIntf::Init()
  50. {
  51. ASSERT(FFileZillaApi == NULL);
  52. FFileZillaApi = new CFileZillaApi();
  53. bool Result = Check(FFileZillaApi->Init(FIntern, this), L"init");
  54. if (!Result)
  55. {
  56. delete FFileZillaApi;
  57. FFileZillaApi = NULL;
  58. }
  59. return Result;
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall TFileZillaIntf::Destroying()
  63. {
  64. // need to close FZAPI before calling destructor as it in turn post messages
  65. // back while being destroyed, what may result in calling virtual methods
  66. // of already destroyed descendants
  67. delete FFileZillaApi;
  68. FFileZillaApi = NULL;
  69. }
  70. //---------------------------------------------------------------------------
  71. bool __fastcall TFileZillaIntf::SetCurrentPath(const wchar_t * APath)
  72. {
  73. ASSERT(FFileZillaApi != NULL);
  74. CServerPath Path(APath);
  75. return Check(FFileZillaApi->SetCurrentPath(Path), L"setcurrentpath");
  76. }
  77. //---------------------------------------------------------------------------
  78. bool __fastcall TFileZillaIntf::GetCurrentPath(wchar_t * Path, size_t MaxLen)
  79. {
  80. CServerPath APath;
  81. bool Result = Check(FFileZillaApi->GetCurrentPath(APath), L"getcurrentpath");
  82. if (Result)
  83. {
  84. wcsncpy(Path, APath.GetPath(), MaxLen);
  85. Path[MaxLen - 1] = L'\0';
  86. }
  87. return Result;
  88. }
  89. //---------------------------------------------------------------------------
  90. bool __fastcall TFileZillaIntf::Cancel()
  91. {
  92. ASSERT(FFileZillaApi != NULL);
  93. // tolerate even "idle" state, quite possible in MT environment
  94. return Check(FFileZillaApi->Cancel(), L"cancel", FZ_REPLY_WOULDBLOCK | FZ_REPLY_IDLE);
  95. }
  96. //---------------------------------------------------------------------------
  97. bool __fastcall TFileZillaIntf::Connect(const wchar_t * Host, int Port, const wchar_t * User,
  98. const wchar_t * Pass, const wchar_t * Account, bool FwByPass,
  99. const wchar_t * Path, int ServerType, int Pasv, int TimeZoneOffset, int UTF8,
  100. int iForcePasvIp, int iUseMlsd)
  101. {
  102. ASSERT(FFileZillaApi != NULL);
  103. ASSERT((ServerType & FZ_SERVERTYPE_HIGHMASK) == FZ_SERVERTYPE_FTP);
  104. t_server Server;
  105. Server.host = Host;
  106. Server.port = Port;
  107. Server.user = User;
  108. Server.pass = Pass;
  109. Server.account = Account;
  110. Server.fwbypass = FwByPass;
  111. Server.path = Path;
  112. Server.nServerType = ServerType;
  113. Server.nPasv = Pasv;
  114. Server.nTimeZoneOffset = TimeZoneOffset;
  115. Server.nUTF8 = UTF8;
  116. Server.iForcePasvIp = iForcePasvIp;
  117. Server.iUseMlsd = iUseMlsd;
  118. *FServer = Server;
  119. return Check(FFileZillaApi->Connect(Server), L"connect");
  120. }
  121. //---------------------------------------------------------------------------
  122. bool __fastcall TFileZillaIntf::Close(bool AllowBusy)
  123. {
  124. bool Result;
  125. int ReturnCode = FFileZillaApi->Disconnect();
  126. switch (ReturnCode)
  127. {
  128. // it the connection terminated itself meanwhile
  129. case FZ_REPLY_NOTCONNECTED:
  130. Result = true;
  131. break;
  132. // waiting for disconnect
  133. case FZ_REPLY_WOULDBLOCK:
  134. Result = true;
  135. break;
  136. // allowing busy while opening, not sure if it safe,
  137. // but we need it, when cancelling password prompt
  138. case FZ_REPLY_BUSY:
  139. if (AllowBusy)
  140. {
  141. Result = false;
  142. break;
  143. }
  144. case FZ_REPLY_NOTINITIALIZED:
  145. default:
  146. Result = Check(ReturnCode, L"disconnect");
  147. break;
  148. }
  149. return Result;
  150. }
  151. //---------------------------------------------------------------------------
  152. bool __fastcall TFileZillaIntf::CustomCommand(const wchar_t * Command)
  153. {
  154. ASSERT(FFileZillaApi != NULL);
  155. return Check(FFileZillaApi->CustomCommand(Command), L"customcommand");
  156. }
  157. //---------------------------------------------------------------------------
  158. bool __fastcall TFileZillaIntf::MakeDir(const wchar_t* APath)
  159. {
  160. ASSERT(FFileZillaApi != NULL);
  161. CServerPath Path(APath);
  162. return Check(FFileZillaApi->MakeDir(Path), L"makedir");
  163. }
  164. //---------------------------------------------------------------------------
  165. bool __fastcall TFileZillaIntf::Chmod(int Value, const wchar_t* FileName,
  166. const wchar_t* APath)
  167. {
  168. ASSERT(FFileZillaApi != NULL);
  169. CServerPath Path(APath);
  170. return Check(FFileZillaApi->Chmod(Value, FileName, Path), L"chmod");
  171. }
  172. //---------------------------------------------------------------------------
  173. bool __fastcall TFileZillaIntf::Delete(const wchar_t* FileName, const wchar_t* APath)
  174. {
  175. ASSERT(FFileZillaApi != NULL);
  176. CServerPath Path(APath);
  177. return Check(FFileZillaApi->Delete(FileName, Path), L"delete");
  178. }
  179. //---------------------------------------------------------------------------
  180. bool __fastcall TFileZillaIntf::RemoveDir(const wchar_t* FileName, const wchar_t* APath)
  181. {
  182. ASSERT(FFileZillaApi != NULL);
  183. CServerPath Path(APath);
  184. return Check(FFileZillaApi->RemoveDir(FileName, Path), L"removedir");
  185. }
  186. //---------------------------------------------------------------------------
  187. bool __fastcall TFileZillaIntf::Rename(const wchar_t* OldName,
  188. const wchar_t* NewName, const wchar_t* APath, const wchar_t* ANewPath)
  189. {
  190. ASSERT(FFileZillaApi != NULL);
  191. CServerPath Path(APath);
  192. CServerPath NewPath(ANewPath);
  193. return Check(FFileZillaApi->Rename(OldName, NewName, Path, NewPath), L"rename");
  194. }
  195. //---------------------------------------------------------------------------
  196. bool __fastcall TFileZillaIntf::List()
  197. {
  198. ASSERT(FFileZillaApi != NULL);
  199. return Check(FFileZillaApi->List(), L"list");
  200. }
  201. //---------------------------------------------------------------------------
  202. bool __fastcall TFileZillaIntf::List(const wchar_t * APath)
  203. {
  204. ASSERT(FFileZillaApi != NULL);
  205. CServerPath Path(APath);
  206. return Check(FFileZillaApi->List(Path), L"list");
  207. }
  208. //---------------------------------------------------------------------------
  209. #ifdef MPEXT
  210. bool __fastcall TFileZillaIntf::ListFile(const wchar_t * AFullFileName)
  211. {
  212. ASSERT(FFileZillaApi != NULL);
  213. CString FileName(AFullFileName);
  214. CServerPath Path(FServer->nServerType);
  215. Path.SetPath(FileName, TRUE);
  216. return Check(FFileZillaApi->ListFile(Path, FileName), L"listfile");
  217. }
  218. #endif
  219. //---------------------------------------------------------------------------
  220. bool __fastcall TFileZillaIntf::FileTransfer(const wchar_t * LocalFile,
  221. const wchar_t * RemoteFile, const wchar_t * RemotePath, bool Get, __int64 Size,
  222. int Type, void * UserData)
  223. {
  224. t_transferfile Transfer;
  225. Transfer.localfile = LocalFile;
  226. Transfer.remotefile = RemoteFile;
  227. Transfer.remotepath = CServerPath(RemotePath);
  228. Transfer.get = Get;
  229. Transfer.size = Size;
  230. Transfer.server = *FServer;
  231. // 1 = ascii, 2 = binary
  232. Transfer.nType = Type;
  233. Transfer.nUserData = reinterpret_cast<int>(UserData);
  234. return Check(FFileZillaApi->FileTransfer(Transfer), L"filetransfer");
  235. }
  236. //---------------------------------------------------------------------------
  237. void __fastcall TFileZillaIntf::SetDebugLevel(TLogLevel Level)
  238. {
  239. FIntern->SetDebugLevel(Level - LOG_APIERROR + 1);
  240. }
  241. //---------------------------------------------------------------------------
  242. bool __fastcall TFileZillaIntf::PostMessage(WPARAM wParam, LPARAM lParam)
  243. {
  244. unsigned int MessageID = FZ_MSG_ID(wParam);
  245. TMessageType Type;
  246. switch (MessageID)
  247. {
  248. case FZ_MSG_TRANSFERSTATUS:
  249. Type = MSG_TRANSFERSTATUS;
  250. break;
  251. default:
  252. Type = MSG_OTHER;
  253. break;
  254. }
  255. return DoPostMessage(Type, wParam, lParam);
  256. }
  257. //---------------------------------------------------------------------------
  258. void __fastcall CopyContact(TFtpsCertificateData::TContact & Dest,
  259. const t_SslCertData::t_Contact& Source)
  260. {
  261. Dest.Organization = Source.Organization;
  262. Dest.Unit = Source.Unit;
  263. Dest.CommonName = Source.CommonName;
  264. Dest.Mail = Source.Mail;
  265. Dest.Country = Source.Country;
  266. Dest.StateProvince = Source.StateProvince;
  267. Dest.Town = Source.Town;
  268. Dest.Other = Source.Other;
  269. }
  270. //---------------------------------------------------------------------------
  271. void __fastcall CopyValidityTime(TFtpsCertificateData::TValidityTime & Dest,
  272. const t_SslCertData::t_validTime& Source)
  273. {
  274. Dest.Year = Source.y;
  275. Dest.Month = Source.M;
  276. Dest.Day = Source.d;
  277. Dest.Hour = Source.h;
  278. Dest.Min = Source.m;
  279. Dest.Sec = Source.s;
  280. }
  281. //---------------------------------------------------------------------------
  282. void __fastcall CopyFileTime(TRemoteFileTime & Dest, const t_directory::t_direntry::t_date & Source)
  283. {
  284. Dest.Year = Source.year;
  285. Dest.Month = Source.month;
  286. Dest.Day = Source.day;
  287. Dest.Hour = Source.hour;
  288. Dest.Minute = Source.minute;
  289. Dest.Second = Source.second;
  290. Dest.HasTime = Source.hastime;
  291. Dest.HasDate = Source.hasdate;
  292. Dest.HasSeconds = Source.hasseconds;
  293. Dest.Utc = Source.utc;
  294. }
  295. //---------------------------------------------------------------------------
  296. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  297. {
  298. bool Result;
  299. CString a;
  300. unsigned int MessageID = FZ_MSG_ID(wParam);
  301. switch (MessageID)
  302. {
  303. case FZ_MSG_STATUS:
  304. {
  305. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  306. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  307. ASSERT(Status->post);
  308. Result = HandleStatus(Status->status, Status->type);
  309. delete Status;
  310. }
  311. break;
  312. case FZ_MSG_ASYNCREQUEST:
  313. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  314. {
  315. int RequestResult;
  316. wchar_t FileName1[MAX_PATH];
  317. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  318. try
  319. {
  320. ASSERT(Data != NULL);
  321. wcsncpy(FileName1, Data->FileName1, LENOF(FileName1));
  322. FileName1[LENOF(FileName1) - 1] = L'\0';
  323. TRemoteFileTime RemoteTime;
  324. CopyFileTime(RemoteTime, Data->remotetime);
  325. Result = HandleAsynchRequestOverwrite(
  326. FileName1, LENOF(FileName1), Data->FileName2, Data->path1, Data->path2,
  327. Data->size1, Data->size2,
  328. (Data->localtime != NULL) ? Data->localtime->GetTime() : 0,
  329. (Data->localtime != NULL) && ((Data->localtime->GetHour() != 0) || (Data->localtime->GetMinute() != 0)),
  330. RemoteTime,
  331. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  332. }
  333. catch(...)
  334. {
  335. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  336. throw;
  337. }
  338. if (Result)
  339. {
  340. Data->FileName1 = FileName1;
  341. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  342. L"setasyncrequestresult");
  343. }
  344. }
  345. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  346. {
  347. int RequestResult;
  348. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  349. try
  350. {
  351. ASSERT(AData != NULL);
  352. TFtpsCertificateData Data;
  353. CopyContact(Data.Subject, AData->pCertData->subject);
  354. CopyContact(Data.Issuer, AData->pCertData->issuer);
  355. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  356. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  357. Data.Hash = AData->pCertData->hash;
  358. Data.VerificationResult = AData->pCertData->verificationResult;
  359. Data.VerificationDepth = AData->pCertData->verificationDepth;
  360. Result = HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  361. }
  362. catch(...)
  363. {
  364. FFileZillaApi->SetAsyncRequestResult(0, AData);
  365. throw;
  366. }
  367. if (Result)
  368. {
  369. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  370. L"setasyncrequestresult");
  371. }
  372. }
  373. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_NEEDPASS)
  374. {
  375. int RequestResult = 0;
  376. CNeedPassRequestData * AData = (CNeedPassRequestData *)lParam;
  377. try
  378. {
  379. TNeedPassRequestData Data;
  380. Data.Password = AData->Password.GetBuffer(AData->Password.GetLength());
  381. Result = HandleAsynchRequestNeedPass(Data, RequestResult);
  382. AData->Password.ReleaseBuffer(AData->Password.GetLength());
  383. if (Result && (RequestResult == TFileZillaIntf::REPLY_OK))
  384. {
  385. AData->Password = Data.Password;
  386. free(Data.Password);
  387. Data.Password = NULL;
  388. }
  389. }
  390. catch(...)
  391. {
  392. FFileZillaApi->SetAsyncRequestResult(0, AData);
  393. throw;
  394. }
  395. if (Result)
  396. {
  397. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  398. L"setasyncrequestresult");
  399. }
  400. }
  401. else
  402. {
  403. // FZ_ASYNCREQUEST_GSS_AUTHFAILED
  404. // FZ_ASYNCREQUEST_GSS_NEEDUSER
  405. // FZ_ASYNCREQUEST_GSS_NEEDPASS
  406. ASSERT(FALSE);
  407. Result = false;
  408. }
  409. break;
  410. case FZ_MSG_LISTDATA:
  411. {
  412. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  413. t_directory * Directory = (t_directory *)lParam;
  414. CString Path = Directory->path.GetPath();
  415. std::vector<TListDataEntry> Entries(Directory->num);
  416. for (int Index = 0; Index < Directory->num; Index++)
  417. {
  418. t_directory::t_direntry & Source = Directory->direntry[Index];
  419. TListDataEntry & Dest = Entries[Index];
  420. Dest.Name = Source.name;
  421. Dest.Permissions = Source.permissionstr;
  422. Dest.OwnerGroup = Source.ownergroup;
  423. Dest.Size = Source.size;
  424. Dest.Dir = Source.dir;
  425. Dest.Link = Source.bLink;
  426. CopyFileTime(Dest.Time, Source.date);
  427. Dest.LinkTarget = Source.linkTarget;
  428. }
  429. int Num = Directory->num;
  430. TListDataEntry * pEntries = Num > 0 ? &Entries[0] : NULL;
  431. Result = HandleListData(Path, pEntries, Num);
  432. delete Directory;
  433. }
  434. break;
  435. case FZ_MSG_TRANSFERSTATUS:
  436. {
  437. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  438. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  439. if (Status != NULL)
  440. {
  441. Result = HandleTransferStatus(true, Status->transfersize, Status->bytes,
  442. Status->percent, Status->timeelapsed, Status->timeleft,
  443. Status->transferrate, Status->bFileTransfer);
  444. delete Status;
  445. }
  446. else
  447. {
  448. Result = HandleTransferStatus(false, -1, -1, -1, -1, -1, -1, false);
  449. }
  450. }
  451. break;
  452. case FZ_MSG_REPLY:
  453. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  454. break;
  455. case FZ_MSG_CAPABILITIES:
  456. Result = HandleCapabilities((TFTPServerCapabilities *)lParam);
  457. break;
  458. case FZ_MSG_SOCKETSTATUS:
  459. case FZ_MSG_SECURESERVER:
  460. case FZ_MSG_QUITCOMPLETE:
  461. default:
  462. ASSERT(false);
  463. Result = false;
  464. break;
  465. }
  466. return Result;
  467. }
  468. //---------------------------------------------------------------------------
  469. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const wchar_t * /*Context*/)
  470. {
  471. return false;
  472. }
  473. //---------------------------------------------------------------------------
  474. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  475. const wchar_t * Context, int Expected)
  476. {
  477. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  478. {
  479. return true;
  480. }
  481. else
  482. {
  483. return CheckError(ReturnCode, Context);
  484. }
  485. }
  486. //---------------------------------------------------------------------------
  487. bool __fastcall TFileZillaIntf::UsingMlsd()
  488. {
  489. return FFileZillaApi->UsingMlsd();
  490. }
  491. //---------------------------------------------------------------------------
  492. bool __fastcall TFileZillaIntf::UsingUtf8()
  493. {
  494. return FFileZillaApi->UsingUtf8();
  495. }
  496. //---------------------------------------------------------------------------
  497. std::string __fastcall TFileZillaIntf::GetTlsVersionStr()
  498. {
  499. return FFileZillaApi->GetTlsVersionStr();
  500. }
  501. //---------------------------------------------------------------------------
  502. std::string __fastcall TFileZillaIntf::GetCipherName()
  503. {
  504. return FFileZillaApi->GetCipherName();
  505. }