FileZillaIntf.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. DebugAssert(FFileZillaApi == NULL);
  43. delete FIntern;
  44. FIntern = NULL;
  45. delete FServer;
  46. FServer = NULL;
  47. }
  48. //---------------------------------------------------------------------------
  49. bool __fastcall TFileZillaIntf::Init()
  50. {
  51. DebugAssert(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. DebugAssert(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. DebugAssert(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,
  99. const wchar_t * Path, int ServerType, int Pasv, int TimeZoneOffset, int UTF8,
  100. int iForcePasvIp, int iUseMlsd,
  101. X509 * Certificate, EVP_PKEY * PrivateKey)
  102. {
  103. DebugAssert(FFileZillaApi != NULL);
  104. DebugAssert((ServerType & FZ_SERVERTYPE_HIGHMASK) == FZ_SERVERTYPE_FTP);
  105. t_server Server;
  106. Server.host = Host;
  107. Server.port = Port;
  108. Server.user = User;
  109. Server.pass = Pass;
  110. Server.account = Account;
  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. Server.Certificate = Certificate;
  119. Server.PrivateKey = PrivateKey;
  120. *FServer = Server;
  121. return Check(FFileZillaApi->Connect(Server), L"connect");
  122. }
  123. //---------------------------------------------------------------------------
  124. bool __fastcall TFileZillaIntf::Close(bool AllowBusy)
  125. {
  126. bool Result;
  127. int ReturnCode = FFileZillaApi->Disconnect();
  128. switch (ReturnCode)
  129. {
  130. // If the connection terminated itself meanwhile,
  131. // do not try to wait for close response.
  132. case FZ_REPLY_NOTCONNECTED:
  133. // We might check AllowBusy here, as it's actually similar scenario,
  134. // as we expect this to happen during authentication only
  135. Result = false;
  136. break;
  137. // waiting for disconnect
  138. case FZ_REPLY_WOULDBLOCK:
  139. Result = true;
  140. break;
  141. // allowing busy while opening, not sure if it safe,
  142. // but we need it, when cancelling password prompt
  143. case FZ_REPLY_BUSY:
  144. if (AllowBusy)
  145. {
  146. Result = false;
  147. break;
  148. }
  149. case FZ_REPLY_NOTINITIALIZED:
  150. default:
  151. Result = Check(ReturnCode, L"disconnect");
  152. break;
  153. }
  154. return Result;
  155. }
  156. //---------------------------------------------------------------------------
  157. bool __fastcall TFileZillaIntf::CustomCommand(const wchar_t * Command)
  158. {
  159. DebugAssert(FFileZillaApi != NULL);
  160. return Check(FFileZillaApi->CustomCommand(Command), L"customcommand");
  161. }
  162. //---------------------------------------------------------------------------
  163. bool __fastcall TFileZillaIntf::MakeDir(const wchar_t* APath)
  164. {
  165. DebugAssert(FFileZillaApi != NULL);
  166. CServerPath Path(APath);
  167. return Check(FFileZillaApi->MakeDir(Path), L"makedir");
  168. }
  169. //---------------------------------------------------------------------------
  170. bool __fastcall TFileZillaIntf::Chmod(int Value, const wchar_t* FileName,
  171. const wchar_t* APath)
  172. {
  173. DebugAssert(FFileZillaApi != NULL);
  174. CServerPath Path(APath);
  175. return Check(FFileZillaApi->Chmod(Value, FileName, Path), L"chmod");
  176. }
  177. //---------------------------------------------------------------------------
  178. bool __fastcall TFileZillaIntf::Delete(const wchar_t* FileName, const wchar_t* APath)
  179. {
  180. DebugAssert(FFileZillaApi != NULL);
  181. CServerPath Path(APath);
  182. return Check(FFileZillaApi->Delete(FileName, Path), L"delete");
  183. }
  184. //---------------------------------------------------------------------------
  185. bool __fastcall TFileZillaIntf::RemoveDir(const wchar_t* FileName, const wchar_t* APath)
  186. {
  187. DebugAssert(FFileZillaApi != NULL);
  188. CServerPath Path(APath);
  189. return Check(FFileZillaApi->RemoveDir(FileName, Path), L"removedir");
  190. }
  191. //---------------------------------------------------------------------------
  192. bool __fastcall TFileZillaIntf::Rename(const wchar_t* OldName,
  193. const wchar_t* NewName, const wchar_t* APath, const wchar_t* ANewPath)
  194. {
  195. DebugAssert(FFileZillaApi != NULL);
  196. CServerPath Path(APath);
  197. CServerPath NewPath(ANewPath);
  198. return Check(FFileZillaApi->Rename(OldName, NewName, Path, NewPath), L"rename");
  199. }
  200. //---------------------------------------------------------------------------
  201. bool __fastcall TFileZillaIntf::List()
  202. {
  203. DebugAssert(FFileZillaApi != NULL);
  204. return Check(FFileZillaApi->List(), L"list");
  205. }
  206. //---------------------------------------------------------------------------
  207. bool __fastcall TFileZillaIntf::List(const wchar_t * APath)
  208. {
  209. DebugAssert(FFileZillaApi != NULL);
  210. CServerPath Path(APath);
  211. return Check(FFileZillaApi->List(Path), L"list");
  212. }
  213. //---------------------------------------------------------------------------
  214. bool __fastcall TFileZillaIntf::ListFile(const wchar_t * FileName, const wchar_t * APath)
  215. {
  216. DebugAssert(FFileZillaApi != NULL);
  217. CServerPath Path(APath);
  218. return Check(FFileZillaApi->ListFile(FileName, Path), L"listfile");
  219. }
  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. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  307. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  308. DebugAssert(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. DebugAssert(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. DebugAssert(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. DebugAssert(FALSE);
  411. Result = false;
  412. }
  413. break;
  414. case FZ_MSG_LISTDATA:
  415. {
  416. DebugAssert(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. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  443. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  444. if (Status != NULL)
  445. {
  446. Result = HandleTransferStatus(
  447. true, Status->transfersize, Status->bytes, Status->bFileTransfer);
  448. delete Status;
  449. }
  450. else
  451. {
  452. Result = HandleTransferStatus(false, -1, -1, false);
  453. }
  454. }
  455. break;
  456. case FZ_MSG_REPLY:
  457. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  458. break;
  459. case FZ_MSG_CAPABILITIES:
  460. Result = HandleCapabilities((TFTPServerCapabilities *)lParam);
  461. break;
  462. default:
  463. DebugAssert(false);
  464. Result = false;
  465. break;
  466. }
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const wchar_t * /*Context*/)
  471. {
  472. return false;
  473. }
  474. //---------------------------------------------------------------------------
  475. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  476. const wchar_t * Context, int Expected)
  477. {
  478. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  479. {
  480. return true;
  481. }
  482. else
  483. {
  484. return CheckError(ReturnCode, Context);
  485. }
  486. }
  487. //---------------------------------------------------------------------------
  488. bool __fastcall TFileZillaIntf::UsingMlsd()
  489. {
  490. return FFileZillaApi->UsingMlsd();
  491. }
  492. //---------------------------------------------------------------------------
  493. bool __fastcall TFileZillaIntf::UsingUtf8()
  494. {
  495. return FFileZillaApi->UsingUtf8();
  496. }
  497. //---------------------------------------------------------------------------
  498. std::string __fastcall TFileZillaIntf::GetTlsVersionStr()
  499. {
  500. return FFileZillaApi->GetTlsVersionStr();
  501. }
  502. //---------------------------------------------------------------------------
  503. std::string __fastcall TFileZillaIntf::GetCipherName()
  504. {
  505. return FFileZillaApi->GetCipherName();
  506. }