FileZillaIntf.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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(const wchar_t * APath)
  202. {
  203. DebugAssert(FFileZillaApi != NULL);
  204. CServerPath Path(APath);
  205. return Check(FFileZillaApi->List(Path), L"list");
  206. }
  207. //---------------------------------------------------------------------------
  208. bool __fastcall TFileZillaIntf::ListFile(const wchar_t * FileName, const wchar_t * APath)
  209. {
  210. DebugAssert(FFileZillaApi != NULL);
  211. CServerPath Path(APath);
  212. return Check(FFileZillaApi->ListFile(FileName, Path), L"listfile");
  213. }
  214. //---------------------------------------------------------------------------
  215. bool __fastcall TFileZillaIntf::FileTransfer(const wchar_t * LocalFile,
  216. const wchar_t * RemoteFile, const wchar_t * RemotePath, bool Get, __int64 Size,
  217. int Type, void * UserData)
  218. {
  219. t_transferfile Transfer;
  220. Transfer.localfile = LocalFile;
  221. Transfer.remotefile = RemoteFile;
  222. Transfer.remotepath = CServerPath(RemotePath);
  223. Transfer.get = Get;
  224. Transfer.size = Size;
  225. Transfer.server = *FServer;
  226. // 1 = ascii, 2 = binary
  227. Transfer.nType = Type;
  228. Transfer.nUserData = reinterpret_cast<int>(UserData);
  229. return Check(FFileZillaApi->FileTransfer(Transfer), L"filetransfer");
  230. }
  231. //---------------------------------------------------------------------------
  232. void __fastcall TFileZillaIntf::SetDebugLevel(TLogLevel Level)
  233. {
  234. FIntern->SetDebugLevel(Level - LOG_APIERROR + 1);
  235. }
  236. //---------------------------------------------------------------------------
  237. bool __fastcall TFileZillaIntf::PostMessage(WPARAM wParam, LPARAM lParam)
  238. {
  239. unsigned int MessageID = FZ_MSG_ID(wParam);
  240. TMessageType Type;
  241. switch (MessageID)
  242. {
  243. case FZ_MSG_TRANSFERSTATUS:
  244. Type = MSG_TRANSFERSTATUS;
  245. break;
  246. default:
  247. Type = MSG_OTHER;
  248. break;
  249. }
  250. return DoPostMessage(Type, wParam, lParam);
  251. }
  252. //---------------------------------------------------------------------------
  253. void __fastcall CopyContact(TFtpsCertificateData::TContact & Dest,
  254. const t_SslCertData::t_Contact& Source)
  255. {
  256. Dest.Organization = Source.Organization;
  257. Dest.Unit = Source.Unit;
  258. Dest.CommonName = Source.CommonName;
  259. Dest.Mail = Source.Mail;
  260. Dest.Country = Source.Country;
  261. Dest.StateProvince = Source.StateProvince;
  262. Dest.Town = Source.Town;
  263. Dest.Other = Source.Other;
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall CopyValidityTime(TFtpsCertificateData::TValidityTime & Dest,
  267. const t_SslCertData::t_validTime& Source)
  268. {
  269. Dest.Year = Source.y;
  270. Dest.Month = Source.M;
  271. Dest.Day = Source.d;
  272. Dest.Hour = Source.h;
  273. Dest.Min = Source.m;
  274. Dest.Sec = Source.s;
  275. }
  276. //---------------------------------------------------------------------------
  277. void __fastcall CopyFileTime(TRemoteFileTime & Dest, const t_directory::t_direntry::t_date & Source)
  278. {
  279. Dest.Year = Source.year;
  280. Dest.Month = Source.month;
  281. Dest.Day = Source.day;
  282. Dest.Hour = Source.hour;
  283. Dest.Minute = Source.minute;
  284. Dest.Second = Source.second;
  285. Dest.HasTime = Source.hastime;
  286. Dest.HasDate = Source.hasdate;
  287. Dest.HasSeconds = Source.hasseconds;
  288. Dest.Utc = Source.utc;
  289. }
  290. //---------------------------------------------------------------------------
  291. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  292. {
  293. bool Result;
  294. CString a;
  295. unsigned int MessageID = FZ_MSG_ID(wParam);
  296. switch (MessageID)
  297. {
  298. case FZ_MSG_STATUS:
  299. {
  300. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  301. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  302. DebugAssert(Status->post);
  303. Result = HandleStatus(Status->status, Status->type);
  304. delete Status;
  305. }
  306. break;
  307. case FZ_MSG_ASYNCREQUEST:
  308. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  309. {
  310. int RequestResult;
  311. wchar_t FileName1[MAX_PATH];
  312. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  313. try
  314. {
  315. DebugAssert(Data != NULL);
  316. wcsncpy(FileName1, Data->FileName1, LENOF(FileName1));
  317. FileName1[LENOF(FileName1) - 1] = L'\0';
  318. TRemoteFileTime RemoteTime;
  319. CopyFileTime(RemoteTime, Data->remotetime);
  320. Result = HandleAsynchRequestOverwrite(
  321. FileName1, LENOF(FileName1), Data->FileName2, Data->path1, Data->path2,
  322. Data->size1, Data->size2,
  323. (Data->localtime != NULL) ? Data->localtime->GetTime() : 0,
  324. (Data->localtime != NULL) && ((Data->localtime->GetHour() != 0) || (Data->localtime->GetMinute() != 0)),
  325. RemoteTime,
  326. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  327. }
  328. catch(...)
  329. {
  330. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  331. throw;
  332. }
  333. if (Result)
  334. {
  335. Data->FileName1 = FileName1;
  336. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  337. L"setasyncrequestresult");
  338. }
  339. }
  340. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  341. {
  342. int RequestResult;
  343. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  344. try
  345. {
  346. DebugAssert(AData != NULL);
  347. TFtpsCertificateData Data;
  348. CopyContact(Data.Subject, AData->pCertData->subject);
  349. CopyContact(Data.Issuer, AData->pCertData->issuer);
  350. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  351. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  352. Data.SubjectAltName = AData->pCertData->subjectAltName;
  353. Data.Hash = AData->pCertData->hash;
  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.Size = Source.size;
  423. Dest.Dir = Source.dir;
  424. Dest.Link = Source.bLink;
  425. CopyFileTime(Dest.Time, Source.date);
  426. Dest.LinkTarget = Source.linkTarget;
  427. }
  428. int Num = Directory->num;
  429. TListDataEntry * pEntries = Num > 0 ? &Entries[0] : NULL;
  430. Result = HandleListData(Path, pEntries, Num);
  431. delete Directory;
  432. }
  433. break;
  434. case FZ_MSG_TRANSFERSTATUS:
  435. {
  436. DebugAssert(FZ_MSG_PARAM(wParam) == 0);
  437. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  438. if (Status != NULL)
  439. {
  440. Result = HandleTransferStatus(
  441. true, Status->transfersize, Status->bytes, Status->bFileTransfer);
  442. delete Status;
  443. }
  444. else
  445. {
  446. Result = HandleTransferStatus(false, -1, -1, false);
  447. }
  448. }
  449. break;
  450. case FZ_MSG_REPLY:
  451. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  452. break;
  453. case FZ_MSG_CAPABILITIES:
  454. Result = HandleCapabilities((TFTPServerCapabilities *)lParam);
  455. break;
  456. default:
  457. DebugFail();
  458. Result = false;
  459. break;
  460. }
  461. return Result;
  462. }
  463. //---------------------------------------------------------------------------
  464. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const wchar_t * /*Context*/)
  465. {
  466. return false;
  467. }
  468. //---------------------------------------------------------------------------
  469. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  470. const wchar_t * Context, int Expected)
  471. {
  472. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  473. {
  474. return true;
  475. }
  476. else
  477. {
  478. return CheckError(ReturnCode, Context);
  479. }
  480. }
  481. //---------------------------------------------------------------------------
  482. bool __fastcall TFileZillaIntf::UsingMlsd()
  483. {
  484. return FFileZillaApi->UsingMlsd();
  485. }
  486. //---------------------------------------------------------------------------
  487. bool __fastcall TFileZillaIntf::UsingUtf8()
  488. {
  489. return FFileZillaApi->UsingUtf8();
  490. }
  491. //---------------------------------------------------------------------------
  492. std::string __fastcall TFileZillaIntf::GetTlsVersionStr()
  493. {
  494. return FFileZillaApi->GetTlsVersionStr();
  495. }
  496. //---------------------------------------------------------------------------
  497. std::string __fastcall TFileZillaIntf::GetCipherName()
  498. {
  499. return FFileZillaApi->GetCipherName();
  500. }