FileZillaIntf.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. // set afx resource handles, taken from AfxWinInit (mfc/appinit.cpp)
  24. AFX_MODULE_STATE * ModuleState = AfxGetModuleState();
  25. ModuleState->m_hCurrentInstanceHandle = (HINSTANCE)ResourceHandle;
  26. ModuleState->m_hCurrentResourceHandle = (HINSTANCE)ResourceHandle;
  27. }
  28. //---------------------------------------------------------------------------
  29. __fastcall TFileZillaIntf::TFileZillaIntf() :
  30. FFileZillaApi(NULL),
  31. FIntern(new TFileZillaIntern(this)),
  32. FServer(new t_server)
  33. {
  34. }
  35. //---------------------------------------------------------------------------
  36. __fastcall TFileZillaIntf::~TFileZillaIntf()
  37. {
  38. DebugAssert(FFileZillaApi == NULL);
  39. delete FIntern;
  40. FIntern = NULL;
  41. delete FServer;
  42. FServer = NULL;
  43. }
  44. //---------------------------------------------------------------------------
  45. bool __fastcall TFileZillaIntf::Init()
  46. {
  47. DebugAssert(FFileZillaApi == NULL);
  48. FFileZillaApi = new CFileZillaApi();
  49. bool Result = Check(FFileZillaApi->Init(FIntern, this), L"init");
  50. if (!Result)
  51. {
  52. delete FFileZillaApi;
  53. FFileZillaApi = NULL;
  54. }
  55. return Result;
  56. }
  57. //---------------------------------------------------------------------------
  58. void __fastcall TFileZillaIntf::Destroying()
  59. {
  60. // need to close FZAPI before calling destructor as it in turn post messages
  61. // back while being destroyed, what may result in calling virtual methods
  62. // of already destroyed descendants
  63. delete FFileZillaApi;
  64. FFileZillaApi = NULL;
  65. }
  66. //---------------------------------------------------------------------------
  67. bool __fastcall TFileZillaIntf::SetCurrentPath(const wchar_t * APath)
  68. {
  69. DebugAssert(FFileZillaApi != NULL);
  70. CServerPath Path(APath, false);
  71. return Check(FFileZillaApi->SetCurrentPath(Path), L"setcurrentpath");
  72. }
  73. //---------------------------------------------------------------------------
  74. bool __fastcall TFileZillaIntf::GetCurrentPath(wchar_t * Path, size_t MaxLen)
  75. {
  76. CServerPath APath;
  77. bool Result = Check(FFileZillaApi->GetCurrentPath(APath), L"getcurrentpath");
  78. if (Result)
  79. {
  80. wcsncpy(Path, APath.GetPath(), MaxLen);
  81. Path[MaxLen - 1] = L'\0';
  82. }
  83. return Result;
  84. }
  85. //---------------------------------------------------------------------------
  86. bool __fastcall TFileZillaIntf::Cancel()
  87. {
  88. DebugAssert(FFileZillaApi != NULL);
  89. // tolerate even "idle" state, quite possible in MT environment
  90. return Check(FFileZillaApi->Cancel(), L"cancel", FZ_REPLY_WOULDBLOCK | FZ_REPLY_IDLE);
  91. }
  92. //---------------------------------------------------------------------------
  93. bool __fastcall TFileZillaIntf::Connect(const wchar_t * Host, int Port, const wchar_t * User,
  94. const wchar_t * Pass, const wchar_t * Account,
  95. const wchar_t * Path, int ServerType, int Pasv, int TimeZoneOffset, int UTF8,
  96. int iForcePasvIp, int iUseMlsd,
  97. X509 * Certificate, EVP_PKEY * PrivateKey)
  98. {
  99. DebugAssert(FFileZillaApi != NULL);
  100. DebugAssert((ServerType & FZ_SERVERTYPE_HIGHMASK) == FZ_SERVERTYPE_FTP);
  101. t_server Server;
  102. Server.host = Host;
  103. Server.port = Port;
  104. Server.user = User;
  105. Server.pass = Pass;
  106. Server.account = Account;
  107. Server.path = Path;
  108. Server.nServerType = ServerType;
  109. Server.nPasv = Pasv;
  110. Server.nTimeZoneOffset = TimeZoneOffset;
  111. Server.nUTF8 = UTF8;
  112. Server.iForcePasvIp = iForcePasvIp;
  113. Server.iUseMlsd = iUseMlsd;
  114. Server.Certificate = Certificate;
  115. Server.PrivateKey = PrivateKey;
  116. *FServer = Server;
  117. return Check(FFileZillaApi->Connect(Server), L"connect");
  118. }
  119. //---------------------------------------------------------------------------
  120. bool __fastcall TFileZillaIntf::Close(bool AllowBusy)
  121. {
  122. bool Result;
  123. int ReturnCode = FFileZillaApi->Disconnect();
  124. switch (ReturnCode)
  125. {
  126. // If the connection terminated itself meanwhile,
  127. // do not try to wait for close response.
  128. case FZ_REPLY_NOTCONNECTED:
  129. // We might check AllowBusy here, as it's actually similar scenario,
  130. // as we expect this to happen during authentication only
  131. Result = false;
  132. break;
  133. // waiting for disconnect
  134. case FZ_REPLY_WOULDBLOCK:
  135. Result = true;
  136. break;
  137. // allowing busy while opening, not sure if it safe,
  138. // but we need it, when cancelling password prompt
  139. case FZ_REPLY_BUSY:
  140. if (AllowBusy)
  141. {
  142. Result = false;
  143. break;
  144. }
  145. case FZ_REPLY_NOTINITIALIZED:
  146. default:
  147. Result = Check(ReturnCode, L"disconnect");
  148. break;
  149. }
  150. return Result;
  151. }
  152. //---------------------------------------------------------------------------
  153. bool __fastcall TFileZillaIntf::CustomCommand(const wchar_t * Command)
  154. {
  155. DebugAssert(FFileZillaApi != NULL);
  156. return Check(FFileZillaApi->CustomCommand(Command), L"customcommand");
  157. }
  158. //---------------------------------------------------------------------------
  159. bool __fastcall TFileZillaIntf::MakeDir(const wchar_t* APath)
  160. {
  161. DebugAssert(FFileZillaApi != NULL);
  162. CServerPath Path(APath, false);
  163. return Check(FFileZillaApi->MakeDir(Path), L"makedir");
  164. }
  165. //---------------------------------------------------------------------------
  166. bool __fastcall TFileZillaIntf::Chmod(int Value, const wchar_t* FileName,
  167. const wchar_t* APath)
  168. {
  169. DebugAssert(FFileZillaApi != NULL);
  170. CServerPath Path(APath, false);
  171. return Check(FFileZillaApi->Chmod(Value, FileName, Path), L"chmod");
  172. }
  173. //---------------------------------------------------------------------------
  174. bool __fastcall TFileZillaIntf::Delete(const wchar_t* FileName, const wchar_t* APath, bool FileNameOnly)
  175. {
  176. DebugAssert(FFileZillaApi != NULL);
  177. CServerPath Path(APath, false);
  178. return Check(FFileZillaApi->Delete(FileName, Path, FileNameOnly), L"delete");
  179. }
  180. //---------------------------------------------------------------------------
  181. bool __fastcall TFileZillaIntf::RemoveDir(const wchar_t* FileName, const wchar_t* APath)
  182. {
  183. DebugAssert(FFileZillaApi != NULL);
  184. CServerPath Path(APath, false);
  185. return Check(FFileZillaApi->RemoveDir(FileName, Path), L"removedir");
  186. }
  187. //---------------------------------------------------------------------------
  188. bool __fastcall TFileZillaIntf::Rename(const wchar_t* OldName,
  189. const wchar_t* NewName, const wchar_t* APath, const wchar_t* ANewPath)
  190. {
  191. DebugAssert(FFileZillaApi != NULL);
  192. CServerPath Path(APath, false);
  193. CServerPath NewPath(ANewPath, false);
  194. return Check(FFileZillaApi->Rename(OldName, NewName, Path, NewPath), L"rename");
  195. }
  196. //---------------------------------------------------------------------------
  197. bool __fastcall TFileZillaIntf::List(const wchar_t * APath)
  198. {
  199. DebugAssert(FFileZillaApi != NULL);
  200. CServerPath Path(APath, false);
  201. return Check(FFileZillaApi->List(Path), L"list");
  202. }
  203. //---------------------------------------------------------------------------
  204. bool __fastcall TFileZillaIntf::ListFile(const wchar_t * FileName, const wchar_t * APath)
  205. {
  206. DebugAssert(FFileZillaApi != NULL);
  207. CServerPath Path(APath, false);
  208. return Check(FFileZillaApi->ListFile(FileName, Path), L"listfile");
  209. }
  210. //---------------------------------------------------------------------------
  211. bool __fastcall TFileZillaIntf::FileTransfer(const wchar_t * LocalFile,
  212. const wchar_t * RemoteFile, const wchar_t * RemotePath, bool Get, __int64 Size,
  213. int Type, void * UserData)
  214. {
  215. t_transferfile Transfer;
  216. Transfer.localfile = LocalFile;
  217. Transfer.remotefile = RemoteFile;
  218. Transfer.remotepath = CServerPath(RemotePath, false);
  219. Transfer.get = Get;
  220. Transfer.size = Size;
  221. Transfer.server = *FServer;
  222. // 1 = ascii, 2 = binary
  223. Transfer.nType = Type;
  224. Transfer.nUserData = reinterpret_cast<int>(UserData);
  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.HasSeconds = Source.hasseconds;
  284. Dest.Utc = Source.utc;
  285. }
  286. //---------------------------------------------------------------------------
  287. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  288. {
  289. bool Result;
  290. CString a;
  291. unsigned int MessageID = FZ_MSG_ID(wParam);
  292. switch (MessageID)
  293. {
  294. case FZ_MSG_STATUS:
  295. {
  296. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  297. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  298. DebugAssert(Status->post);
  299. Result = HandleStatus(Status->status, Status->type);
  300. delete Status;
  301. }
  302. break;
  303. case FZ_MSG_ASYNCREQUEST:
  304. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  305. {
  306. int RequestResult;
  307. wchar_t FileName1[MAX_PATH];
  308. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  309. try
  310. {
  311. DebugAssert(Data != NULL);
  312. wcsncpy(FileName1, Data->FileName1, LENOF(FileName1));
  313. FileName1[LENOF(FileName1) - 1] = L'\0';
  314. TRemoteFileTime RemoteTime;
  315. CopyFileTime(RemoteTime, Data->remotetime);
  316. Result = HandleAsynchRequestOverwrite(
  317. FileName1, LENOF(FileName1), Data->FileName2, Data->path1, Data->path2,
  318. Data->size1, Data->size2,
  319. (Data->localtime != NULL) ? Data->localtime->GetTime() : 0,
  320. (Data->localtime != NULL) && ((Data->localtime->GetHour() != 0) || (Data->localtime->GetMinute() != 0)),
  321. RemoteTime,
  322. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  323. }
  324. catch(...)
  325. {
  326. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  327. throw;
  328. }
  329. if (Result)
  330. {
  331. Data->FileName1 = FileName1;
  332. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  333. L"setasyncrequestresult");
  334. }
  335. }
  336. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  337. {
  338. int RequestResult;
  339. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  340. try
  341. {
  342. DebugAssert(AData != NULL);
  343. TFtpsCertificateData Data;
  344. CopyContact(Data.Subject, AData->pCertData->subject);
  345. CopyContact(Data.Issuer, AData->pCertData->issuer);
  346. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  347. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  348. Data.SubjectAltName = AData->pCertData->subjectAltName;
  349. Data.HashSha1 = AData->pCertData->hashSha1;
  350. DebugAssert(Data.HashSha1Len == sizeof(AData->pCertData->hashSha1));
  351. Data.HashSha256 = AData->pCertData->hashSha256;
  352. DebugAssert(Data.HashSha256Len == sizeof(AData->pCertData->hashSha256));
  353. Data.Certificate = AData->pCertData->certificate;
  354. Data.CertificateLen = AData->pCertData->certificateLen;
  355. Data.VerificationResult = AData->pCertData->verificationResult;
  356. Data.VerificationDepth = AData->pCertData->verificationDepth;
  357. Result = HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  358. }
  359. catch(...)
  360. {
  361. FFileZillaApi->SetAsyncRequestResult(0, AData);
  362. throw;
  363. }
  364. if (Result)
  365. {
  366. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  367. L"setasyncrequestresult");
  368. }
  369. }
  370. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_NEEDPASS)
  371. {
  372. int RequestResult = 0;
  373. CNeedPassRequestData * AData = (CNeedPassRequestData *)lParam;
  374. try
  375. {
  376. TNeedPassRequestData Data;
  377. Data.Password = AData->Password.GetBuffer(AData->Password.GetLength());
  378. Result = HandleAsynchRequestNeedPass(Data, RequestResult);
  379. AData->Password.ReleaseBuffer(AData->Password.GetLength());
  380. if (Result && (RequestResult == TFileZillaIntf::REPLY_OK))
  381. {
  382. AData->Password = Data.Password;
  383. free(Data.Password);
  384. Data.Password = NULL;
  385. }
  386. }
  387. catch(...)
  388. {
  389. FFileZillaApi->SetAsyncRequestResult(0, AData);
  390. throw;
  391. }
  392. if (Result)
  393. {
  394. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  395. L"setasyncrequestresult");
  396. }
  397. }
  398. else
  399. {
  400. // FZ_ASYNCREQUEST_GSS_AUTHFAILED
  401. // FZ_ASYNCREQUEST_GSS_NEEDUSER
  402. // FZ_ASYNCREQUEST_GSS_NEEDPASS
  403. DebugFail();
  404. Result = false;
  405. }
  406. break;
  407. case FZ_MSG_LISTDATA:
  408. {
  409. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  410. t_directory * Directory = (t_directory *)lParam;
  411. CString Path = Directory->path.GetPath();
  412. std::vector<TListDataEntry> Entries(Directory->num);
  413. for (int Index = 0; Index < Directory->num; Index++)
  414. {
  415. t_directory::t_direntry & Source = Directory->direntry[Index];
  416. TListDataEntry & Dest = Entries[Index];
  417. Dest.Name = Source.name;
  418. Dest.Permissions = Source.permissionstr;
  419. Dest.HumanPerm = Source.humanpermstr;
  420. Dest.OwnerGroup = Source.ownergroup;
  421. Dest.Owner = Source.owner;
  422. Dest.Group = Source.group;
  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. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  438. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  439. if (Status != NULL)
  440. {
  441. Result = HandleTransferStatus(
  442. true, Status->transfersize, Status->bytes, Status->bFileTransfer);
  443. delete Status;
  444. }
  445. else
  446. {
  447. Result = HandleTransferStatus(false, -1, -1, false);
  448. }
  449. }
  450. break;
  451. case FZ_MSG_REPLY:
  452. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  453. break;
  454. case FZ_MSG_CAPABILITIES:
  455. Result = HandleCapabilities((TFTPServerCapabilities *)lParam);
  456. break;
  457. default:
  458. DebugFail();
  459. Result = false;
  460. break;
  461. }
  462. return Result;
  463. }
  464. //---------------------------------------------------------------------------
  465. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const wchar_t * /*Context*/)
  466. {
  467. return false;
  468. }
  469. //---------------------------------------------------------------------------
  470. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  471. const wchar_t * Context, int Expected)
  472. {
  473. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  474. {
  475. return true;
  476. }
  477. else
  478. {
  479. return CheckError(ReturnCode, Context);
  480. }
  481. }
  482. //---------------------------------------------------------------------------
  483. bool __fastcall TFileZillaIntf::UsingMlsd()
  484. {
  485. return FFileZillaApi->UsingMlsd();
  486. }
  487. //---------------------------------------------------------------------------
  488. bool __fastcall TFileZillaIntf::UsingUtf8()
  489. {
  490. return FFileZillaApi->UsingUtf8();
  491. }
  492. //---------------------------------------------------------------------------
  493. std::string __fastcall TFileZillaIntf::GetTlsVersionStr()
  494. {
  495. return FFileZillaApi->GetTlsVersionStr();
  496. }
  497. //---------------------------------------------------------------------------
  498. std::string __fastcall TFileZillaIntf::GetCipherName()
  499. {
  500. return FFileZillaApi->GetCipherName();
  501. }