FileZillaIntf.cpp 18 KB

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