FileZillaIntf.cpp 15 KB

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