FileZillaIntf.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. // If the connection terminated itself meanwhile,
  129. // do not try to wait for close response.
  130. case FZ_REPLY_NOTCONNECTED:
  131. // We might check AllowBusy here, as it's actually similar scenario,
  132. // as we expect this to happen during authentication only
  133. Result = false;
  134. break;
  135. // waiting for disconnect
  136. case FZ_REPLY_WOULDBLOCK:
  137. Result = true;
  138. break;
  139. // allowing busy while opening, not sure if it safe,
  140. // but we need it, when cancelling password prompt
  141. case FZ_REPLY_BUSY:
  142. if (AllowBusy)
  143. {
  144. Result = false;
  145. break;
  146. }
  147. case FZ_REPLY_NOTINITIALIZED:
  148. default:
  149. Result = Check(ReturnCode, L"disconnect");
  150. break;
  151. }
  152. return Result;
  153. }
  154. //---------------------------------------------------------------------------
  155. bool __fastcall TFileZillaIntf::CustomCommand(const wchar_t * Command)
  156. {
  157. ASSERT(FFileZillaApi != NULL);
  158. return Check(FFileZillaApi->CustomCommand(Command), L"customcommand");
  159. }
  160. //---------------------------------------------------------------------------
  161. bool __fastcall TFileZillaIntf::MakeDir(const wchar_t* APath)
  162. {
  163. ASSERT(FFileZillaApi != NULL);
  164. CServerPath Path(APath);
  165. return Check(FFileZillaApi->MakeDir(Path), L"makedir");
  166. }
  167. //---------------------------------------------------------------------------
  168. bool __fastcall TFileZillaIntf::Chmod(int Value, const wchar_t* FileName,
  169. const wchar_t* APath)
  170. {
  171. ASSERT(FFileZillaApi != NULL);
  172. CServerPath Path(APath);
  173. return Check(FFileZillaApi->Chmod(Value, FileName, Path), L"chmod");
  174. }
  175. //---------------------------------------------------------------------------
  176. bool __fastcall TFileZillaIntf::Delete(const wchar_t* FileName, const wchar_t* APath)
  177. {
  178. ASSERT(FFileZillaApi != NULL);
  179. CServerPath Path(APath);
  180. return Check(FFileZillaApi->Delete(FileName, Path), L"delete");
  181. }
  182. //---------------------------------------------------------------------------
  183. bool __fastcall TFileZillaIntf::RemoveDir(const wchar_t* FileName, const wchar_t* APath)
  184. {
  185. ASSERT(FFileZillaApi != NULL);
  186. CServerPath Path(APath);
  187. return Check(FFileZillaApi->RemoveDir(FileName, Path), L"removedir");
  188. }
  189. //---------------------------------------------------------------------------
  190. bool __fastcall TFileZillaIntf::Rename(const wchar_t* OldName,
  191. const wchar_t* NewName, const wchar_t* APath, const wchar_t* ANewPath)
  192. {
  193. ASSERT(FFileZillaApi != NULL);
  194. CServerPath Path(APath);
  195. CServerPath NewPath(ANewPath);
  196. return Check(FFileZillaApi->Rename(OldName, NewName, Path, NewPath), L"rename");
  197. }
  198. //---------------------------------------------------------------------------
  199. bool __fastcall TFileZillaIntf::List()
  200. {
  201. ASSERT(FFileZillaApi != NULL);
  202. return Check(FFileZillaApi->List(), L"list");
  203. }
  204. //---------------------------------------------------------------------------
  205. bool __fastcall TFileZillaIntf::List(const wchar_t * APath)
  206. {
  207. ASSERT(FFileZillaApi != NULL);
  208. CServerPath Path(APath);
  209. return Check(FFileZillaApi->List(Path), L"list");
  210. }
  211. //---------------------------------------------------------------------------
  212. #ifdef MPEXT
  213. bool __fastcall TFileZillaIntf::ListFile(const wchar_t * FileName, const wchar_t * APath)
  214. {
  215. ASSERT(FFileZillaApi != NULL);
  216. CServerPath Path(APath);
  217. return Check(FFileZillaApi->ListFile(FileName, Path), L"listfile");
  218. }
  219. #endif
  220. //---------------------------------------------------------------------------
  221. bool __fastcall TFileZillaIntf::FileTransfer(const wchar_t * LocalFile,
  222. const wchar_t * RemoteFile, const wchar_t * RemotePath, bool Get, __int64 Size,
  223. int Type, void * UserData)
  224. {
  225. t_transferfile Transfer;
  226. Transfer.localfile = LocalFile;
  227. Transfer.remotefile = RemoteFile;
  228. Transfer.remotepath = CServerPath(RemotePath);
  229. Transfer.get = Get;
  230. Transfer.size = Size;
  231. Transfer.server = *FServer;
  232. // 1 = ascii, 2 = binary
  233. Transfer.nType = Type;
  234. Transfer.nUserData = reinterpret_cast<int>(UserData);
  235. return Check(FFileZillaApi->FileTransfer(Transfer), L"filetransfer");
  236. }
  237. //---------------------------------------------------------------------------
  238. void __fastcall TFileZillaIntf::SetDebugLevel(TLogLevel Level)
  239. {
  240. FIntern->SetDebugLevel(Level - LOG_APIERROR + 1);
  241. }
  242. //---------------------------------------------------------------------------
  243. bool __fastcall TFileZillaIntf::PostMessage(WPARAM wParam, LPARAM lParam)
  244. {
  245. unsigned int MessageID = FZ_MSG_ID(wParam);
  246. TMessageType Type;
  247. switch (MessageID)
  248. {
  249. case FZ_MSG_TRANSFERSTATUS:
  250. Type = MSG_TRANSFERSTATUS;
  251. break;
  252. default:
  253. Type = MSG_OTHER;
  254. break;
  255. }
  256. return DoPostMessage(Type, wParam, lParam);
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall CopyContact(TFtpsCertificateData::TContact & Dest,
  260. const t_SslCertData::t_Contact& Source)
  261. {
  262. Dest.Organization = Source.Organization;
  263. Dest.Unit = Source.Unit;
  264. Dest.CommonName = Source.CommonName;
  265. Dest.Mail = Source.Mail;
  266. Dest.Country = Source.Country;
  267. Dest.StateProvince = Source.StateProvince;
  268. Dest.Town = Source.Town;
  269. Dest.Other = Source.Other;
  270. }
  271. //---------------------------------------------------------------------------
  272. void __fastcall CopyValidityTime(TFtpsCertificateData::TValidityTime & Dest,
  273. const t_SslCertData::t_validTime& Source)
  274. {
  275. Dest.Year = Source.y;
  276. Dest.Month = Source.M;
  277. Dest.Day = Source.d;
  278. Dest.Hour = Source.h;
  279. Dest.Min = Source.m;
  280. Dest.Sec = Source.s;
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall CopyFileTime(TRemoteFileTime & Dest, const t_directory::t_direntry::t_date & Source)
  284. {
  285. Dest.Year = Source.year;
  286. Dest.Month = Source.month;
  287. Dest.Day = Source.day;
  288. Dest.Hour = Source.hour;
  289. Dest.Minute = Source.minute;
  290. Dest.Second = Source.second;
  291. Dest.HasTime = Source.hastime;
  292. Dest.HasDate = Source.hasdate;
  293. Dest.HasSeconds = Source.hasseconds;
  294. Dest.Utc = Source.utc;
  295. }
  296. //---------------------------------------------------------------------------
  297. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  298. {
  299. bool Result;
  300. CString a;
  301. unsigned int MessageID = FZ_MSG_ID(wParam);
  302. switch (MessageID)
  303. {
  304. case FZ_MSG_STATUS:
  305. {
  306. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  307. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  308. ASSERT(Status->post);
  309. Result = HandleStatus(Status->status, Status->type);
  310. delete Status;
  311. }
  312. break;
  313. case FZ_MSG_ASYNCREQUEST:
  314. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  315. {
  316. int RequestResult;
  317. wchar_t FileName1[MAX_PATH];
  318. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  319. try
  320. {
  321. ASSERT(Data != NULL);
  322. wcsncpy(FileName1, Data->FileName1, LENOF(FileName1));
  323. FileName1[LENOF(FileName1) - 1] = L'\0';
  324. TRemoteFileTime RemoteTime;
  325. CopyFileTime(RemoteTime, Data->remotetime);
  326. Result = HandleAsynchRequestOverwrite(
  327. FileName1, LENOF(FileName1), Data->FileName2, Data->path1, Data->path2,
  328. Data->size1, Data->size2,
  329. (Data->localtime != NULL) ? Data->localtime->GetTime() : 0,
  330. (Data->localtime != NULL) && ((Data->localtime->GetHour() != 0) || (Data->localtime->GetMinute() != 0)),
  331. RemoteTime,
  332. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  333. }
  334. catch(...)
  335. {
  336. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  337. throw;
  338. }
  339. if (Result)
  340. {
  341. Data->FileName1 = FileName1;
  342. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  343. L"setasyncrequestresult");
  344. }
  345. }
  346. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  347. {
  348. int RequestResult;
  349. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  350. try
  351. {
  352. ASSERT(AData != NULL);
  353. TFtpsCertificateData Data;
  354. CopyContact(Data.Subject, AData->pCertData->subject);
  355. CopyContact(Data.Issuer, AData->pCertData->issuer);
  356. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  357. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  358. Data.SubjectAltName = AData->pCertData->subjectAltName;
  359. Data.Hash = AData->pCertData->hash;
  360. Data.Certificate = AData->pCertData->certificate;
  361. Data.CertificateLen = AData->pCertData->certificateLen;
  362. Data.VerificationResult = AData->pCertData->verificationResult;
  363. Data.VerificationDepth = AData->pCertData->verificationDepth;
  364. Result = HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  365. }
  366. catch(...)
  367. {
  368. FFileZillaApi->SetAsyncRequestResult(0, AData);
  369. throw;
  370. }
  371. if (Result)
  372. {
  373. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  374. L"setasyncrequestresult");
  375. }
  376. }
  377. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_NEEDPASS)
  378. {
  379. int RequestResult = 0;
  380. CNeedPassRequestData * AData = (CNeedPassRequestData *)lParam;
  381. try
  382. {
  383. TNeedPassRequestData Data;
  384. Data.Password = AData->Password.GetBuffer(AData->Password.GetLength());
  385. Result = HandleAsynchRequestNeedPass(Data, RequestResult);
  386. AData->Password.ReleaseBuffer(AData->Password.GetLength());
  387. if (Result && (RequestResult == TFileZillaIntf::REPLY_OK))
  388. {
  389. AData->Password = Data.Password;
  390. free(Data.Password);
  391. Data.Password = NULL;
  392. }
  393. }
  394. catch(...)
  395. {
  396. FFileZillaApi->SetAsyncRequestResult(0, AData);
  397. throw;
  398. }
  399. if (Result)
  400. {
  401. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  402. L"setasyncrequestresult");
  403. }
  404. }
  405. else
  406. {
  407. // FZ_ASYNCREQUEST_GSS_AUTHFAILED
  408. // FZ_ASYNCREQUEST_GSS_NEEDUSER
  409. // FZ_ASYNCREQUEST_GSS_NEEDPASS
  410. ASSERT(FALSE);
  411. Result = false;
  412. }
  413. break;
  414. case FZ_MSG_LISTDATA:
  415. {
  416. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  417. t_directory * Directory = (t_directory *)lParam;
  418. CString Path = Directory->path.GetPath();
  419. std::vector<TListDataEntry> Entries(Directory->num);
  420. for (int Index = 0; Index < Directory->num; Index++)
  421. {
  422. t_directory::t_direntry & Source = Directory->direntry[Index];
  423. TListDataEntry & Dest = Entries[Index];
  424. Dest.Name = Source.name;
  425. Dest.Permissions = Source.permissionstr;
  426. Dest.HumanPerm = Source.humanpermstr;
  427. Dest.OwnerGroup = Source.ownergroup;
  428. Dest.Size = Source.size;
  429. Dest.Dir = Source.dir;
  430. Dest.Link = Source.bLink;
  431. CopyFileTime(Dest.Time, Source.date);
  432. Dest.LinkTarget = Source.linkTarget;
  433. }
  434. int Num = Directory->num;
  435. TListDataEntry * pEntries = Num > 0 ? &Entries[0] : NULL;
  436. Result = HandleListData(Path, pEntries, Num);
  437. delete Directory;
  438. }
  439. break;
  440. case FZ_MSG_TRANSFERSTATUS:
  441. {
  442. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  443. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  444. if (Status != NULL)
  445. {
  446. Result = HandleTransferStatus(true, Status->transfersize, Status->bytes,
  447. Status->percent, Status->timeelapsed, Status->timeleft,
  448. Status->transferrate, Status->bFileTransfer);
  449. delete Status;
  450. }
  451. else
  452. {
  453. Result = HandleTransferStatus(false, -1, -1, -1, -1, -1, -1, false);
  454. }
  455. }
  456. break;
  457. case FZ_MSG_REPLY:
  458. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  459. break;
  460. case FZ_MSG_CAPABILITIES:
  461. Result = HandleCapabilities((TFTPServerCapabilities *)lParam);
  462. break;
  463. case FZ_MSG_SOCKETSTATUS:
  464. case FZ_MSG_SECURESERVER:
  465. case FZ_MSG_QUITCOMPLETE:
  466. default:
  467. ASSERT(false);
  468. Result = false;
  469. break;
  470. }
  471. return Result;
  472. }
  473. //---------------------------------------------------------------------------
  474. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const wchar_t * /*Context*/)
  475. {
  476. return false;
  477. }
  478. //---------------------------------------------------------------------------
  479. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  480. const wchar_t * Context, int Expected)
  481. {
  482. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  483. {
  484. return true;
  485. }
  486. else
  487. {
  488. return CheckError(ReturnCode, Context);
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. bool __fastcall TFileZillaIntf::UsingMlsd()
  493. {
  494. return FFileZillaApi->UsingMlsd();
  495. }
  496. //---------------------------------------------------------------------------
  497. bool __fastcall TFileZillaIntf::UsingUtf8()
  498. {
  499. return FFileZillaApi->UsingUtf8();
  500. }
  501. //---------------------------------------------------------------------------
  502. std::string __fastcall TFileZillaIntf::GetTlsVersionStr()
  503. {
  504. return FFileZillaApi->GetTlsVersionStr();
  505. }
  506. //---------------------------------------------------------------------------
  507. std::string __fastcall TFileZillaIntf::GetCipherName()
  508. {
  509. return FFileZillaApi->GetCipherName();
  510. }