FileZillaIntf.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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(const wchar_t * LocalFile,
  209. const wchar_t * RemoteFile, const wchar_t * RemotePath, bool Get, __int64 Size,
  210. int Type, void * UserData)
  211. {
  212. t_transferfile Transfer;
  213. Transfer.localfile = LocalFile;
  214. Transfer.remotefile = RemoteFile;
  215. Transfer.remotepath = CServerPath(RemotePath, false);
  216. Transfer.get = Get;
  217. Transfer.size = Size;
  218. Transfer.server = *FServer;
  219. // 1 = ascii, 2 = binary
  220. Transfer.nType = Type;
  221. Transfer.nUserData = reinterpret_cast<int>(UserData);
  222. return Check(FFileZillaApi->FileTransfer(Transfer), L"filetransfer");
  223. }
  224. //---------------------------------------------------------------------------
  225. void __fastcall TFileZillaIntf::SetDebugLevel(TLogLevel Level)
  226. {
  227. FIntern->SetDebugLevel(Level - LOG_APIERROR + 1);
  228. }
  229. //---------------------------------------------------------------------------
  230. bool __fastcall TFileZillaIntf::PostMessage(WPARAM wParam, LPARAM lParam)
  231. {
  232. unsigned int MessageID = FZ_MSG_ID(wParam);
  233. TMessageType Type;
  234. switch (MessageID)
  235. {
  236. case FZ_MSG_TRANSFERSTATUS:
  237. Type = MSG_TRANSFERSTATUS;
  238. break;
  239. default:
  240. Type = MSG_OTHER;
  241. break;
  242. }
  243. return DoPostMessage(Type, wParam, lParam);
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall CopyContact(TFtpsCertificateData::TContact & Dest,
  247. const t_SslCertData::t_Contact& Source)
  248. {
  249. Dest.Organization = Source.Organization;
  250. Dest.Unit = Source.Unit;
  251. Dest.CommonName = Source.CommonName;
  252. Dest.Mail = Source.Mail;
  253. Dest.Country = Source.Country;
  254. Dest.StateProvince = Source.StateProvince;
  255. Dest.Town = Source.Town;
  256. Dest.Other = Source.Other;
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall CopyValidityTime(TFtpsCertificateData::TValidityTime & Dest,
  260. const t_SslCertData::t_validTime& Source)
  261. {
  262. Dest.Year = Source.y;
  263. Dest.Month = Source.M;
  264. Dest.Day = Source.d;
  265. Dest.Hour = Source.h;
  266. Dest.Min = Source.m;
  267. Dest.Sec = Source.s;
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall CopyFileTime(TRemoteFileTime & Dest, const t_directory::t_direntry::t_date & Source)
  271. {
  272. Dest.Year = Source.year;
  273. Dest.Month = Source.month;
  274. Dest.Day = Source.day;
  275. Dest.Hour = Source.hour;
  276. Dest.Minute = Source.minute;
  277. Dest.Second = Source.second;
  278. Dest.HasTime = Source.hastime;
  279. Dest.HasDate = Source.hasdate;
  280. Dest.HasYear = Source.hasyear;
  281. Dest.HasSeconds = Source.hasseconds;
  282. Dest.Utc = Source.utc;
  283. }
  284. //---------------------------------------------------------------------------
  285. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  286. {
  287. bool Result;
  288. CString a;
  289. unsigned int MessageID = FZ_MSG_ID(wParam);
  290. switch (MessageID)
  291. {
  292. case FZ_MSG_STATUS:
  293. {
  294. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  295. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  296. DebugAssert(Status->post);
  297. Result = HandleStatus(Status->status, Status->type);
  298. delete Status;
  299. }
  300. break;
  301. case FZ_MSG_ASYNCREQUEST:
  302. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  303. {
  304. int RequestResult;
  305. wchar_t FileName1[MAX_PATH];
  306. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  307. try
  308. {
  309. DebugAssert(Data != NULL);
  310. wcsncpy(FileName1, Data->FileName1, LENOF(FileName1));
  311. FileName1[LENOF(FileName1) - 1] = L'\0';
  312. TRemoteFileTime RemoteTime;
  313. CopyFileTime(RemoteTime, Data->remotetime);
  314. Result = HandleAsynchRequestOverwrite(
  315. FileName1, LENOF(FileName1), Data->FileName2, Data->path1, Data->path2,
  316. Data->size1, Data->size2,
  317. (Data->localtime != NULL) ? Data->localtime->GetTime() : 0,
  318. (Data->localtime != NULL) && ((Data->localtime->GetHour() != 0) || (Data->localtime->GetMinute() != 0)),
  319. RemoteTime,
  320. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  321. }
  322. catch(...)
  323. {
  324. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  325. throw;
  326. }
  327. if (Result)
  328. {
  329. Data->FileName1 = FileName1;
  330. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  331. L"setasyncrequestresult");
  332. }
  333. }
  334. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  335. {
  336. int RequestResult;
  337. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  338. try
  339. {
  340. DebugAssert(AData != NULL);
  341. TFtpsCertificateData Data;
  342. CopyContact(Data.Subject, AData->pCertData->subject);
  343. CopyContact(Data.Issuer, AData->pCertData->issuer);
  344. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  345. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  346. Data.SubjectAltName = AData->pCertData->subjectAltName;
  347. Data.HashSha1 = AData->pCertData->hashSha1;
  348. DebugAssert(Data.HashSha1Len == sizeof(AData->pCertData->hashSha1));
  349. Data.HashSha256 = AData->pCertData->hashSha256;
  350. DebugAssert(Data.HashSha256Len == sizeof(AData->pCertData->hashSha256));
  351. Data.Certificate = AData->pCertData->certificate;
  352. Data.CertificateLen = AData->pCertData->certificateLen;
  353. Data.VerificationResult = AData->pCertData->verificationResult;
  354. Data.VerificationDepth = AData->pCertData->verificationDepth;
  355. Result = HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  356. }
  357. catch(...)
  358. {
  359. FFileZillaApi->SetAsyncRequestResult(0, AData);
  360. throw;
  361. }
  362. if (Result)
  363. {
  364. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  365. L"setasyncrequestresult");
  366. }
  367. }
  368. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_NEEDPASS)
  369. {
  370. int RequestResult = 0;
  371. CNeedPassRequestData * AData = (CNeedPassRequestData *)lParam;
  372. try
  373. {
  374. TNeedPassRequestData Data;
  375. Data.Password = AData->Password.GetBuffer(AData->Password.GetLength());
  376. Result = HandleAsynchRequestNeedPass(Data, RequestResult);
  377. AData->Password.ReleaseBuffer(AData->Password.GetLength());
  378. if (Result && (RequestResult == TFileZillaIntf::REPLY_OK))
  379. {
  380. AData->Password = Data.Password;
  381. free(Data.Password);
  382. Data.Password = NULL;
  383. }
  384. }
  385. catch(...)
  386. {
  387. FFileZillaApi->SetAsyncRequestResult(0, AData);
  388. throw;
  389. }
  390. if (Result)
  391. {
  392. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  393. L"setasyncrequestresult");
  394. }
  395. }
  396. else
  397. {
  398. // FZ_ASYNCREQUEST_GSS_AUTHFAILED
  399. // FZ_ASYNCREQUEST_GSS_NEEDUSER
  400. // FZ_ASYNCREQUEST_GSS_NEEDPASS
  401. DebugFail();
  402. Result = false;
  403. }
  404. break;
  405. case FZ_MSG_LISTDATA:
  406. {
  407. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  408. t_directory * Directory = (t_directory *)lParam;
  409. CString Path = Directory->path.GetPath();
  410. std::vector<TListDataEntry> Entries(Directory->num);
  411. for (int Index = 0; Index < Directory->num; Index++)
  412. {
  413. t_directory::t_direntry & Source = Directory->direntry[Index];
  414. TListDataEntry & Dest = Entries[Index];
  415. Dest.Name = Source.name;
  416. Dest.Permissions = Source.permissionstr;
  417. Dest.HumanPerm = Source.humanpermstr;
  418. Dest.OwnerGroup = Source.ownergroup;
  419. Dest.Owner = Source.owner;
  420. Dest.Group = Source.group;
  421. Dest.Size = Source.size;
  422. Dest.Dir = Source.dir;
  423. Dest.Link = Source.bLink;
  424. CopyFileTime(Dest.Time, Source.date);
  425. Dest.LinkTarget = Source.linkTarget;
  426. }
  427. int Num = Directory->num;
  428. TListDataEntry * pEntries = Num > 0 ? &Entries[0] : NULL;
  429. Result = HandleListData(Path, pEntries, Num);
  430. delete Directory;
  431. }
  432. break;
  433. case FZ_MSG_TRANSFERSTATUS:
  434. {
  435. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  436. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  437. if (Status != NULL)
  438. {
  439. Result = HandleTransferStatus(
  440. true, Status->transfersize, Status->bytes, Status->bFileTransfer);
  441. delete Status;
  442. }
  443. else
  444. {
  445. Result = HandleTransferStatus(false, -1, -1, false);
  446. }
  447. }
  448. break;
  449. case FZ_MSG_REPLY:
  450. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  451. break;
  452. case FZ_MSG_CAPABILITIES:
  453. Result = HandleCapabilities((TFTPServerCapabilities *)lParam);
  454. break;
  455. default:
  456. DebugFail();
  457. Result = false;
  458. break;
  459. }
  460. return Result;
  461. }
  462. //---------------------------------------------------------------------------
  463. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const wchar_t * /*Context*/)
  464. {
  465. return false;
  466. }
  467. //---------------------------------------------------------------------------
  468. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  469. const wchar_t * Context, int Expected)
  470. {
  471. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  472. {
  473. return true;
  474. }
  475. else
  476. {
  477. return CheckError(ReturnCode, Context);
  478. }
  479. }
  480. //---------------------------------------------------------------------------
  481. bool __fastcall TFileZillaIntf::UsingMlsd()
  482. {
  483. return FFileZillaApi->UsingMlsd();
  484. }
  485. //---------------------------------------------------------------------------
  486. bool __fastcall TFileZillaIntf::UsingUtf8()
  487. {
  488. return FFileZillaApi->UsingUtf8();
  489. }
  490. //---------------------------------------------------------------------------
  491. std::string __fastcall TFileZillaIntf::GetTlsVersionStr()
  492. {
  493. return FFileZillaApi->GetTlsVersionStr();
  494. }
  495. //---------------------------------------------------------------------------
  496. std::string __fastcall TFileZillaIntf::GetCipherName()
  497. {
  498. return FFileZillaApi->GetCipherName();
  499. }