FileZillaIntf.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //---------------------------------------------------------------------------
  2. #include "stdafx.h"
  3. //---------------------------------------------------------------------------
  4. #include "FileZillaIntf.h"
  5. #include "FileZillaIntern.h"
  6. //---------------------------------------------------------------------------
  7. #ifndef _DEBUG
  8. #pragma comment(lib, "nafxcw.lib")
  9. #else
  10. #pragma comment(lib, "nafxcwd.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. ASSERT(FFileZillaApi == NULL);
  43. delete FIntern;
  44. FIntern = NULL;
  45. delete FServer;
  46. FServer = NULL;
  47. }
  48. //---------------------------------------------------------------------------
  49. bool __fastcall TFileZillaIntf::Init()
  50. {
  51. ASSERT(FFileZillaApi == NULL);
  52. FFileZillaApi = new CFileZillaApi();
  53. bool Result = Check(FFileZillaApi->Init(FIntern), "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 char * APath)
  72. {
  73. ASSERT(FFileZillaApi != NULL);
  74. CServerPath Path(APath, FServer->nServerType);
  75. return Check(FFileZillaApi->SetCurrentPath(Path), "setcurrentpath");
  76. }
  77. //---------------------------------------------------------------------------
  78. bool __fastcall TFileZillaIntf::GetCurrentPath(char * Path, size_t MaxLen)
  79. {
  80. CServerPath APath;
  81. bool Result = Check(FFileZillaApi->GetCurrentPath(APath), "getcurrentpath");
  82. if (Result)
  83. {
  84. strncpy(Path, APath.GetPath(), MaxLen);
  85. Path[MaxLen - 1] = '\0';
  86. }
  87. return Result;
  88. }
  89. //---------------------------------------------------------------------------
  90. bool __fastcall TFileZillaIntf::Cancel()
  91. {
  92. ASSERT(FFileZillaApi != NULL);
  93. // tolerate even "idle" state, quite possible in MT environment
  94. return Check(FFileZillaApi->Cancel(), "cancel", FZ_REPLY_WOULDBLOCK | FZ_REPLY_IDLE);
  95. }
  96. //---------------------------------------------------------------------------
  97. bool __fastcall TFileZillaIntf::Connect(const char * Host, int Port, const char * User,
  98. const char * Pass, const char * Account, bool FwByPass,
  99. const char * Path, int ServerType, int Pasv, int TimeZoneOffset, int UTF8,
  100. int iForcePasvIp)
  101. {
  102. ASSERT(FFileZillaApi != NULL);
  103. ASSERT((ServerType & FZ_SERVERTYPE_HIGHMASK) == FZ_SERVERTYPE_FTP);
  104. t_server Server;
  105. Server.host = Host;
  106. Server.port = Port;
  107. Server.user = User;
  108. Server.pass = Pass;
  109. Server.account = Account;
  110. Server.fwbypass = FwByPass;
  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. *FServer = Server;
  118. return Check(FFileZillaApi->Connect(Server), "connect");
  119. }
  120. //---------------------------------------------------------------------------
  121. bool __fastcall TFileZillaIntf::Close()
  122. {
  123. bool Result;
  124. int ReturnCode = FFileZillaApi->Disconnect();
  125. switch (ReturnCode)
  126. {
  127. // it the connection terminated itself meanwhile
  128. case FZ_REPLY_NOTCONNECTED:
  129. Result = true;
  130. break;
  131. // waiting for disconnect
  132. case FZ_REPLY_WOULDBLOCK:
  133. Result = true;
  134. break;
  135. case FZ_REPLY_NOTINITIALIZED:
  136. default:
  137. Result = Check(ReturnCode, "disconnect");
  138. break;
  139. }
  140. return Result;
  141. }
  142. //---------------------------------------------------------------------------
  143. bool __fastcall TFileZillaIntf::CustomCommand(const char * Command)
  144. {
  145. ASSERT(FFileZillaApi != NULL);
  146. return Check(FFileZillaApi->CustomCommand(Command), "customcommand");
  147. }
  148. //---------------------------------------------------------------------------
  149. bool __fastcall TFileZillaIntf::MakeDir(const char* APath)
  150. {
  151. ASSERT(FFileZillaApi != NULL);
  152. CServerPath Path(APath, FServer->nServerType);
  153. return Check(FFileZillaApi->MakeDir(Path), "makedir");
  154. }
  155. //---------------------------------------------------------------------------
  156. bool __fastcall TFileZillaIntf::Chmod(int Value, const char* FileName,
  157. const char* APath)
  158. {
  159. ASSERT(FFileZillaApi != NULL);
  160. CServerPath Path(APath, FServer->nServerType);
  161. return Check(FFileZillaApi->Chmod(Value, FileName, Path), "chmod");
  162. }
  163. //---------------------------------------------------------------------------
  164. bool __fastcall TFileZillaIntf::Delete(const char* FileName, const char* APath)
  165. {
  166. ASSERT(FFileZillaApi != NULL);
  167. CServerPath Path(APath, FServer->nServerType);
  168. return Check(FFileZillaApi->Delete(FileName, Path), "delete");
  169. }
  170. //---------------------------------------------------------------------------
  171. bool __fastcall TFileZillaIntf::RemoveDir(const char* FileName, const char* APath)
  172. {
  173. ASSERT(FFileZillaApi != NULL);
  174. CServerPath Path(APath, FServer->nServerType);
  175. return Check(FFileZillaApi->RemoveDir(FileName, Path), "removedir");
  176. }
  177. //---------------------------------------------------------------------------
  178. bool __fastcall TFileZillaIntf::Rename(const char* OldName,
  179. const char* NewName, const char* APath, const char* ANewPath)
  180. {
  181. ASSERT(FFileZillaApi != NULL);
  182. CServerPath Path(APath, FServer->nServerType);
  183. CServerPath NewPath(ANewPath, FServer->nServerType);
  184. return Check(FFileZillaApi->Rename(OldName, NewName, Path, NewPath), "rename");
  185. }
  186. //---------------------------------------------------------------------------
  187. bool __fastcall TFileZillaIntf::List()
  188. {
  189. ASSERT(FFileZillaApi != NULL);
  190. return Check(FFileZillaApi->List(), "list");
  191. }
  192. //---------------------------------------------------------------------------
  193. bool __fastcall TFileZillaIntf::List(const char * APath)
  194. {
  195. ASSERT(FFileZillaApi != NULL);
  196. CServerPath Path(APath, FServer->nServerType);
  197. return Check(FFileZillaApi->List(Path), "list");
  198. }
  199. //---------------------------------------------------------------------------
  200. bool __fastcall TFileZillaIntf::FileTransfer(const char * LocalFile,
  201. const char * RemoteFile, const char * RemotePath, bool Get, __int64 Size,
  202. int Type, void * UserData)
  203. {
  204. t_transferfile Transfer;
  205. Transfer.localfile = LocalFile;
  206. Transfer.remotefile = RemoteFile;
  207. Transfer.remotepath = CServerPath(RemotePath, FServer->nServerType);
  208. Transfer.get = Get;
  209. Transfer.size = Size;
  210. Transfer.server = *FServer;
  211. // 1 = ascii, 2 = binary
  212. Transfer.nType = Type;
  213. Transfer.nUserData = reinterpret_cast<int>(UserData);
  214. return Check(FFileZillaApi->FileTransfer(Transfer), "filetransfer");
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TFileZillaIntf::SetDebugLevel(TLogLevel Level)
  218. {
  219. FIntern->SetDebugLevel(Level - LOG_APIERROR + 1);
  220. }
  221. //---------------------------------------------------------------------------
  222. bool __fastcall TFileZillaIntf::PostMessage(WPARAM wParam, LPARAM lParam)
  223. {
  224. unsigned int MessageID = FZ_MSG_ID(wParam);
  225. TMessageType Type;
  226. switch (MessageID)
  227. {
  228. case FZ_MSG_TRANSFERSTATUS:
  229. Type = MSG_TRANSFERSTATUS;
  230. break;
  231. default:
  232. Type = MSG_OTHER;
  233. break;
  234. }
  235. return DoPostMessage(Type, wParam, lParam);
  236. }
  237. //---------------------------------------------------------------------------
  238. void __fastcall CopyContact(TFtpsCertificateData::TContact & Dest,
  239. const t_SslCertData::t_Contact& Source)
  240. {
  241. Dest.Organization = Source.Organization;
  242. Dest.Unit = Source.Unit;
  243. Dest.CommonName = Source.CommonName;
  244. Dest.Mail = Source.Mail;
  245. Dest.Country = Source.Country;
  246. Dest.StateProvince = Source.StateProvince;
  247. Dest.Town = Source.Town;
  248. Dest.Other = Source.Other;
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall CopyValidityTime(TFtpsCertificateData::TValidityTime & Dest,
  252. const t_SslCertData::t_validTime& Source)
  253. {
  254. Dest.Year = Source.y;
  255. Dest.Month = Source.M;
  256. Dest.Day = Source.d;
  257. Dest.Hour = Source.h;
  258. Dest.Min = Source.m;
  259. Dest.Sec = Source.s;
  260. }
  261. //---------------------------------------------------------------------------
  262. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  263. {
  264. bool Result;
  265. unsigned int MessageID = FZ_MSG_ID(wParam);
  266. switch (MessageID)
  267. {
  268. case FZ_MSG_STATUS:
  269. {
  270. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  271. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  272. ASSERT(Status->post);
  273. Result = HandleStatus(Status->status, Status->type);
  274. delete Status;
  275. }
  276. break;
  277. case FZ_MSG_ASYNCREQUEST:
  278. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  279. {
  280. int RequestResult;
  281. char FileName1[MAX_PATH];
  282. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  283. try
  284. {
  285. ASSERT(Data != NULL);
  286. strncpy(FileName1, Data->FileName1, sizeof(FileName1));
  287. FileName1[sizeof(FileName1) - 1] = '\0';
  288. Result = HandleAsynchRequestOverwrite(
  289. FileName1, sizeof(FileName1), Data->FileName2, Data->path1, Data->path2,
  290. Data->size1, Data->size2,
  291. (Data->time1 != NULL) ? Data->time1->GetTime() : 0,
  292. (Data->time2 != NULL) ? Data->time2->GetTime() : 0,
  293. (Data->time1 != NULL) && ((Data->time1->GetHour() != 0) || (Data->time1->GetMinute() != 0)),
  294. (Data->time2 != NULL) && ((Data->time2->GetHour() != 0) || (Data->time2->GetMinute() != 0)),
  295. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  296. }
  297. catch(...)
  298. {
  299. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  300. throw;
  301. }
  302. if (Result)
  303. {
  304. Data->FileName1 = FileName1;
  305. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  306. "setasyncrequestresult");
  307. }
  308. }
  309. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  310. {
  311. int RequestResult;
  312. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  313. try
  314. {
  315. ASSERT(AData != NULL);
  316. TFtpsCertificateData Data;
  317. CopyContact(Data.Subject, AData->pCertData->subject);
  318. CopyContact(Data.Issuer, AData->pCertData->issuer);
  319. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  320. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  321. Data.Hash = AData->pCertData->hash;
  322. Data.VerificationResult = AData->pCertData->verificationResult;
  323. Data.VerificationDepth = AData->pCertData->verificationDepth;
  324. Result = HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  325. }
  326. catch(...)
  327. {
  328. FFileZillaApi->SetAsyncRequestResult(0, AData);
  329. throw;
  330. }
  331. if (Result)
  332. {
  333. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  334. "setasyncrequestresult");
  335. }
  336. }
  337. else
  338. {
  339. // FZ_ASYNCREQUEST_GSS_AUTHFAILED
  340. // FZ_ASYNCREQUEST_GSS_NEEDUSER
  341. // FZ_ASYNCREQUEST_GSS_NEEDPASS
  342. ASSERT(FALSE);
  343. Result = false;
  344. }
  345. break;
  346. case FZ_MSG_LISTDATA:
  347. {
  348. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  349. t_directory * Directory = (t_directory *)lParam;
  350. CString Path = Directory->path.GetPath();
  351. std::vector<TListDataEntry> Entries(Directory->num);
  352. for (int Index = 0; Index < Directory->num; Index++)
  353. {
  354. t_directory::t_direntry & Source = Directory->direntry[Index];
  355. TListDataEntry & Dest = Entries[Index];
  356. Dest.Name = Source.name;
  357. Dest.Permissions = Source.permissionstr;
  358. Dest.OwnerGroup = Source.ownergroup;
  359. Dest.Size = Source.size;
  360. Dest.Dir = Source.dir;
  361. Dest.Link = Source.bLink;
  362. Dest.Year = Source.date.year;
  363. Dest.Month = Source.date.month;
  364. Dest.Day = Source.date.day;
  365. Dest.Hour = Source.date.hour;
  366. Dest.Minute = Source.date.minute;
  367. Dest.HasTime = Source.date.hastime;
  368. Dest.HasDate = Source.date.hasdate;
  369. Dest.LinkTarget = Source.linkTarget;
  370. }
  371. int Num = Directory->num;
  372. delete Directory;
  373. Result = HandleListData(Path, &Entries[0], Num);
  374. }
  375. break;
  376. case FZ_MSG_TRANSFERSTATUS:
  377. {
  378. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  379. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  380. if (Status != NULL)
  381. {
  382. Result = HandleTransferStatus(true, Status->transfersize, Status->bytes,
  383. Status->percent, Status->timeelapsed, Status->timeleft,
  384. Status->transferrate, Status->bFileTransfer);
  385. delete Status;
  386. }
  387. else
  388. {
  389. Result = HandleTransferStatus(false, -1, -1, -1, -1, -1, -1, false);
  390. }
  391. }
  392. break;
  393. case FZ_MSG_REPLY:
  394. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  395. break;
  396. case FZ_MSG_CAPABILITIES:
  397. Result = HandleCapabilities(lParam & FZ_CAPABILITIES_MFMT);
  398. break;
  399. case FZ_MSG_SOCKETSTATUS:
  400. case FZ_MSG_SECURESERVER:
  401. case FZ_MSG_QUITCOMPLETE:
  402. default:
  403. ASSERT(false);
  404. Result = false;
  405. break;
  406. }
  407. return Result;
  408. }
  409. //---------------------------------------------------------------------------
  410. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const char * /*Context*/)
  411. {
  412. return false;
  413. }
  414. //---------------------------------------------------------------------------
  415. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  416. const char * Context, int Expected)
  417. {
  418. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  419. {
  420. return true;
  421. }
  422. else
  423. {
  424. return CheckError(ReturnCode, Context);
  425. }
  426. }