FileZillaIntf.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. #define LENOF(x) ( (sizeof((x))) / (sizeof(*(x))))
  14. //---------------------------------------------------------------------------
  15. #pragma package(smart_init)
  16. //---------------------------------------------------------------------------
  17. void __fastcall TFileZillaIntf::Initialize()
  18. {
  19. // noop
  20. }
  21. //---------------------------------------------------------------------------
  22. void __fastcall TFileZillaIntf::Finalize()
  23. {
  24. // noop
  25. }
  26. //---------------------------------------------------------------------------
  27. void __fastcall TFileZillaIntf::SetResourceModule(void * ResourceHandle)
  28. {
  29. // set afx resource handles, taken from AfxWinInit (mfc/appinit.cpp)
  30. AFX_MODULE_STATE * ModuleState = AfxGetModuleState();
  31. ModuleState->m_hCurrentInstanceHandle = (HINSTANCE)ResourceHandle;
  32. ModuleState->m_hCurrentResourceHandle = (HINSTANCE)ResourceHandle;
  33. }
  34. //---------------------------------------------------------------------------
  35. __fastcall TFileZillaIntf::TFileZillaIntf() :
  36. FFileZillaApi(NULL),
  37. FIntern(new TFileZillaIntern(this)),
  38. FServer(new t_server)
  39. {
  40. }
  41. //---------------------------------------------------------------------------
  42. __fastcall TFileZillaIntf::~TFileZillaIntf()
  43. {
  44. ASSERT(FFileZillaApi == NULL);
  45. delete FIntern;
  46. FIntern = NULL;
  47. delete FServer;
  48. FServer = NULL;
  49. }
  50. //---------------------------------------------------------------------------
  51. bool __fastcall TFileZillaIntf::Init()
  52. {
  53. ASSERT(FFileZillaApi == NULL);
  54. FFileZillaApi = new CFileZillaApi();
  55. bool Result = Check(FFileZillaApi->Init(FIntern), L"init");
  56. if (!Result)
  57. {
  58. delete FFileZillaApi;
  59. FFileZillaApi = NULL;
  60. }
  61. return Result;
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TFileZillaIntf::Destroying()
  65. {
  66. // need to close FZAPI before calling destructor as it in turn post messages
  67. // back while being destroyed, what may result in calling virtual methods
  68. // of already destroyed descendants
  69. delete FFileZillaApi;
  70. FFileZillaApi = NULL;
  71. }
  72. //---------------------------------------------------------------------------
  73. bool __fastcall TFileZillaIntf::SetCurrentPath(const wchar_t * APath)
  74. {
  75. ASSERT(FFileZillaApi != NULL);
  76. CServerPath Path(APath, FServer->nServerType);
  77. return Check(FFileZillaApi->SetCurrentPath(Path), L"setcurrentpath");
  78. }
  79. //---------------------------------------------------------------------------
  80. bool __fastcall TFileZillaIntf::GetCurrentPath(wchar_t * Path, size_t MaxLen)
  81. {
  82. CServerPath APath;
  83. bool Result = Check(FFileZillaApi->GetCurrentPath(APath), L"getcurrentpath");
  84. if (Result)
  85. {
  86. wcsncpy(Path, APath.GetPath(), MaxLen);
  87. Path[MaxLen - 1] = L'\0';
  88. }
  89. return Result;
  90. }
  91. //---------------------------------------------------------------------------
  92. bool __fastcall TFileZillaIntf::Cancel()
  93. {
  94. ASSERT(FFileZillaApi != NULL);
  95. // tolerate even "idle" state, quite possible in MT environment
  96. return Check(FFileZillaApi->Cancel(), L"cancel", FZ_REPLY_WOULDBLOCK | FZ_REPLY_IDLE);
  97. }
  98. //---------------------------------------------------------------------------
  99. bool __fastcall TFileZillaIntf::Connect(const wchar_t * Host, int Port, const wchar_t * User,
  100. const wchar_t * Pass, const wchar_t * Account, bool FwByPass,
  101. const wchar_t * Path, int ServerType, int Pasv, int TimeZoneOffset, int UTF8,
  102. int iForcePasvIp)
  103. {
  104. ASSERT(FFileZillaApi != NULL);
  105. ASSERT((ServerType & FZ_SERVERTYPE_HIGHMASK) == FZ_SERVERTYPE_FTP);
  106. t_server Server;
  107. Server.host = Host;
  108. Server.port = Port;
  109. Server.user = User;
  110. Server.pass = Pass;
  111. Server.account = Account;
  112. Server.fwbypass = FwByPass;
  113. Server.path = Path;
  114. Server.nServerType = ServerType;
  115. Server.nPasv = Pasv;
  116. Server.nTimeZoneOffset = TimeZoneOffset;
  117. Server.nUTF8 = UTF8;
  118. Server.iForcePasvIp = iForcePasvIp;
  119. *FServer = Server;
  120. return Check(FFileZillaApi->Connect(Server), L"connect");
  121. }
  122. //---------------------------------------------------------------------------
  123. bool __fastcall TFileZillaIntf::Close(bool AllowBusy)
  124. {
  125. bool Result;
  126. int ReturnCode = FFileZillaApi->Disconnect();
  127. switch (ReturnCode)
  128. {
  129. // it the connection terminated itself meanwhile
  130. case FZ_REPLY_NOTCONNECTED:
  131. Result = true;
  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. ASSERT(FFileZillaApi != NULL);
  156. return Check(FFileZillaApi->CustomCommand(Command), L"customcommand");
  157. }
  158. //---------------------------------------------------------------------------
  159. bool __fastcall TFileZillaIntf::MakeDir(const wchar_t* APath)
  160. {
  161. ASSERT(FFileZillaApi != NULL);
  162. CServerPath Path(APath, FServer->nServerType);
  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. ASSERT(FFileZillaApi != NULL);
  170. CServerPath Path(APath, FServer->nServerType);
  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)
  175. {
  176. ASSERT(FFileZillaApi != NULL);
  177. CServerPath Path(APath, FServer->nServerType);
  178. return Check(FFileZillaApi->Delete(FileName, Path), L"delete");
  179. }
  180. //---------------------------------------------------------------------------
  181. bool __fastcall TFileZillaIntf::RemoveDir(const wchar_t* FileName, const wchar_t* APath)
  182. {
  183. ASSERT(FFileZillaApi != NULL);
  184. CServerPath Path(APath, FServer->nServerType);
  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. ASSERT(FFileZillaApi != NULL);
  192. CServerPath Path(APath, FServer->nServerType);
  193. CServerPath NewPath(ANewPath, FServer->nServerType);
  194. return Check(FFileZillaApi->Rename(OldName, NewName, Path, NewPath), L"rename");
  195. }
  196. //---------------------------------------------------------------------------
  197. bool __fastcall TFileZillaIntf::List()
  198. {
  199. ASSERT(FFileZillaApi != NULL);
  200. return Check(FFileZillaApi->List(), L"list");
  201. }
  202. //---------------------------------------------------------------------------
  203. bool __fastcall TFileZillaIntf::List(const wchar_t * APath)
  204. {
  205. ASSERT(FFileZillaApi != NULL);
  206. CServerPath Path(APath, FServer->nServerType);
  207. return Check(FFileZillaApi->List(Path), L"list");
  208. }
  209. //---------------------------------------------------------------------------
  210. #ifdef MPEXT
  211. bool __fastcall TFileZillaIntf::ListFile(const wchar_t * AFullFileName)
  212. {
  213. ASSERT(FFileZillaApi != NULL);
  214. CString FileName(AFullFileName);
  215. CServerPath Path(FServer->nServerType);
  216. Path.SetPath(FileName, TRUE);
  217. return Check(FFileZillaApi->ListFile(Path, FileName), L"listfile");
  218. }
  219. #endif
  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, FServer->nServerType);
  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. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  284. {
  285. bool Result;
  286. CString a;
  287. unsigned int MessageID = FZ_MSG_ID(wParam);
  288. switch (MessageID)
  289. {
  290. case FZ_MSG_STATUS:
  291. {
  292. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  293. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  294. ASSERT(Status->post);
  295. Result = HandleStatus(Status->status, Status->type);
  296. delete Status;
  297. }
  298. break;
  299. case FZ_MSG_ASYNCREQUEST:
  300. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  301. {
  302. int RequestResult;
  303. wchar_t FileName1[MAX_PATH];
  304. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  305. try
  306. {
  307. ASSERT(Data != NULL);
  308. wcsncpy(FileName1, Data->FileName1, LENOF(FileName1));
  309. FileName1[LENOF(FileName1) - 1] = L'\0';
  310. Result = HandleAsynchRequestOverwrite(
  311. FileName1, LENOF(FileName1), Data->FileName2, Data->path1, Data->path2,
  312. Data->size1, Data->size2,
  313. (Data->time1 != NULL) ? Data->time1->GetTime() : 0,
  314. (Data->time2 != NULL) ? Data->time2->GetTime() : 0,
  315. (Data->time1 != NULL) && ((Data->time1->GetHour() != 0) || (Data->time1->GetMinute() != 0)),
  316. (Data->time2 != NULL) && ((Data->time2->GetHour() != 0) || (Data->time2->GetMinute() != 0)),
  317. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  318. }
  319. catch(...)
  320. {
  321. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  322. throw;
  323. }
  324. if (Result)
  325. {
  326. Data->FileName1 = FileName1;
  327. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  328. L"setasyncrequestresult");
  329. }
  330. }
  331. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  332. {
  333. int RequestResult;
  334. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  335. try
  336. {
  337. ASSERT(AData != NULL);
  338. TFtpsCertificateData Data;
  339. CopyContact(Data.Subject, AData->pCertData->subject);
  340. CopyContact(Data.Issuer, AData->pCertData->issuer);
  341. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  342. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  343. Data.Hash = AData->pCertData->hash;
  344. Data.VerificationResult = AData->pCertData->verificationResult;
  345. Data.VerificationDepth = AData->pCertData->verificationDepth;
  346. Result = HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  347. }
  348. catch(...)
  349. {
  350. FFileZillaApi->SetAsyncRequestResult(0, AData);
  351. throw;
  352. }
  353. if (Result)
  354. {
  355. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  356. L"setasyncrequestresult");
  357. }
  358. }
  359. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_NEEDPASS)
  360. {
  361. int RequestResult = 0;
  362. CNeedPassRequestData * AData = (CNeedPassRequestData *)lParam;
  363. try
  364. {
  365. TNeedPassRequestData Data;
  366. Data.Password = NULL;
  367. Data.Password = AData->Password.GetBuffer(AData->Password.GetLength());
  368. Result = HandleAsynchRequestNeedPass(Data, RequestResult);
  369. AData->Password.ReleaseBuffer(AData->Password.GetLength());
  370. if (Result)
  371. {
  372. AData->Password = Data.Password;
  373. free(Data.Password);
  374. Data.Password = NULL;
  375. }
  376. }
  377. catch(...)
  378. {
  379. FFileZillaApi->SetAsyncRequestResult(0, AData);
  380. throw;
  381. }
  382. if (Result)
  383. {
  384. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  385. L"setasyncrequestresult");
  386. }
  387. }
  388. else
  389. {
  390. // FZ_ASYNCREQUEST_GSS_AUTHFAILED
  391. // FZ_ASYNCREQUEST_GSS_NEEDUSER
  392. // FZ_ASYNCREQUEST_GSS_NEEDPASS
  393. ASSERT(FALSE);
  394. Result = false;
  395. }
  396. break;
  397. case FZ_MSG_LISTDATA:
  398. {
  399. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  400. t_directory * Directory = (t_directory *)lParam;
  401. CString Path = Directory->path.GetPath();
  402. std::vector<TListDataEntry> Entries(Directory->num);
  403. for (int Index = 0; Index < Directory->num; Index++)
  404. {
  405. t_directory::t_direntry & Source = Directory->direntry[Index];
  406. TListDataEntry & Dest = Entries[Index];
  407. Dest.Name = Source.name;
  408. Dest.Permissions = Source.permissionstr;
  409. Dest.OwnerGroup = Source.ownergroup;
  410. Dest.Size = Source.size;
  411. Dest.Dir = Source.dir;
  412. Dest.Link = Source.bLink;
  413. Dest.Year = Source.date.year;
  414. Dest.Month = Source.date.month;
  415. Dest.Day = Source.date.day;
  416. Dest.Hour = Source.date.hour;
  417. Dest.Minute = Source.date.minute;
  418. Dest.Second = Source.date.second;
  419. Dest.HasTime = Source.date.hastime;
  420. Dest.HasDate = Source.date.hasdate;
  421. Dest.HasSeconds = Source.date.hasseconds;
  422. Dest.Utc = Source.date.utc;
  423. Dest.LinkTarget = Source.linkTarget;
  424. }
  425. int Num = Directory->num;
  426. TListDataEntry * pEntries = Num > 0 ? &Entries[0] : NULL;
  427. Result = HandleListData(Path, pEntries, Num);
  428. delete Directory;
  429. }
  430. break;
  431. case FZ_MSG_TRANSFERSTATUS:
  432. {
  433. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  434. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  435. if (Status != NULL)
  436. {
  437. Result = HandleTransferStatus(true, Status->transfersize, Status->bytes,
  438. Status->percent, Status->timeelapsed, Status->timeleft,
  439. Status->transferrate, Status->bFileTransfer);
  440. delete Status;
  441. }
  442. else
  443. {
  444. Result = HandleTransferStatus(false, -1, -1, -1, -1, -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. case FZ_MSG_SOCKETSTATUS:
  455. case FZ_MSG_SECURESERVER:
  456. case FZ_MSG_QUITCOMPLETE:
  457. default:
  458. ASSERT(false);
  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. }