FileZillaIntf.cpp 18 KB

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