FileZillaIntf.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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, this), 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, int iUseMlsd)
  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. Server.iUseMlsd = iUseMlsd;
  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. // it the connection terminated itself meanwhile
  131. case FZ_REPLY_NOTCONNECTED:
  132. Result = true;
  133. break;
  134. // waiting for disconnect
  135. case FZ_REPLY_WOULDBLOCK:
  136. Result = true;
  137. break;
  138. // allowing busy while opening, not sure if it safe,
  139. // but we need it, when cancelling password prompt
  140. case FZ_REPLY_BUSY:
  141. if (AllowBusy)
  142. {
  143. Result = false;
  144. break;
  145. }
  146. case FZ_REPLY_NOTINITIALIZED:
  147. default:
  148. Result = Check(ReturnCode, L"disconnect");
  149. break;
  150. }
  151. return Result;
  152. }
  153. //---------------------------------------------------------------------------
  154. bool __fastcall TFileZillaIntf::CustomCommand(const wchar_t * Command)
  155. {
  156. ASSERT(FFileZillaApi != NULL);
  157. return Check(FFileZillaApi->CustomCommand(Command), L"customcommand");
  158. }
  159. //---------------------------------------------------------------------------
  160. bool __fastcall TFileZillaIntf::MakeDir(const wchar_t* APath)
  161. {
  162. ASSERT(FFileZillaApi != NULL);
  163. CServerPath Path(APath, FServer->nServerType);
  164. return Check(FFileZillaApi->MakeDir(Path), L"makedir");
  165. }
  166. //---------------------------------------------------------------------------
  167. bool __fastcall TFileZillaIntf::Chmod(int Value, const wchar_t* FileName,
  168. const wchar_t* APath)
  169. {
  170. ASSERT(FFileZillaApi != NULL);
  171. CServerPath Path(APath, FServer->nServerType);
  172. return Check(FFileZillaApi->Chmod(Value, FileName, Path), L"chmod");
  173. }
  174. //---------------------------------------------------------------------------
  175. bool __fastcall TFileZillaIntf::Delete(const wchar_t* FileName, const wchar_t* APath)
  176. {
  177. ASSERT(FFileZillaApi != NULL);
  178. CServerPath Path(APath, FServer->nServerType);
  179. return Check(FFileZillaApi->Delete(FileName, Path), L"delete");
  180. }
  181. //---------------------------------------------------------------------------
  182. bool __fastcall TFileZillaIntf::RemoveDir(const wchar_t* FileName, const wchar_t* APath)
  183. {
  184. ASSERT(FFileZillaApi != NULL);
  185. CServerPath Path(APath, FServer->nServerType);
  186. return Check(FFileZillaApi->RemoveDir(FileName, Path), L"removedir");
  187. }
  188. //---------------------------------------------------------------------------
  189. bool __fastcall TFileZillaIntf::Rename(const wchar_t* OldName,
  190. const wchar_t* NewName, const wchar_t* APath, const wchar_t* ANewPath)
  191. {
  192. ASSERT(FFileZillaApi != NULL);
  193. CServerPath Path(APath, FServer->nServerType);
  194. CServerPath NewPath(ANewPath, FServer->nServerType);
  195. return Check(FFileZillaApi->Rename(OldName, NewName, Path, NewPath), L"rename");
  196. }
  197. //---------------------------------------------------------------------------
  198. bool __fastcall TFileZillaIntf::List()
  199. {
  200. ASSERT(FFileZillaApi != NULL);
  201. return Check(FFileZillaApi->List(), L"list");
  202. }
  203. //---------------------------------------------------------------------------
  204. bool __fastcall TFileZillaIntf::List(const wchar_t * APath)
  205. {
  206. ASSERT(FFileZillaApi != NULL);
  207. CServerPath Path(APath, FServer->nServerType);
  208. return Check(FFileZillaApi->List(Path), L"list");
  209. }
  210. //---------------------------------------------------------------------------
  211. #ifdef MPEXT
  212. bool __fastcall TFileZillaIntf::ListFile(const wchar_t * AFullFileName)
  213. {
  214. ASSERT(FFileZillaApi != NULL);
  215. CString FileName(AFullFileName);
  216. CServerPath Path(FServer->nServerType);
  217. Path.SetPath(FileName, TRUE);
  218. return Check(FFileZillaApi->ListFile(Path, FileName), L"listfile");
  219. }
  220. #endif
  221. //---------------------------------------------------------------------------
  222. bool __fastcall TFileZillaIntf::FileTransfer(const wchar_t * LocalFile,
  223. const wchar_t * RemoteFile, const wchar_t * RemotePath, bool Get, __int64 Size,
  224. int Type, void * UserData)
  225. {
  226. t_transferfile Transfer;
  227. Transfer.localfile = LocalFile;
  228. Transfer.remotefile = RemoteFile;
  229. Transfer.remotepath = CServerPath(RemotePath, FServer->nServerType);
  230. Transfer.get = Get;
  231. Transfer.size = Size;
  232. Transfer.server = *FServer;
  233. // 1 = ascii, 2 = binary
  234. Transfer.nType = Type;
  235. Transfer.nUserData = reinterpret_cast<int>(UserData);
  236. return Check(FFileZillaApi->FileTransfer(Transfer), L"filetransfer");
  237. }
  238. //---------------------------------------------------------------------------
  239. void __fastcall TFileZillaIntf::SetDebugLevel(TLogLevel Level)
  240. {
  241. FIntern->SetDebugLevel(Level - LOG_APIERROR + 1);
  242. }
  243. //---------------------------------------------------------------------------
  244. bool __fastcall TFileZillaIntf::PostMessage(WPARAM wParam, LPARAM lParam)
  245. {
  246. unsigned int MessageID = FZ_MSG_ID(wParam);
  247. TMessageType Type;
  248. switch (MessageID)
  249. {
  250. case FZ_MSG_TRANSFERSTATUS:
  251. Type = MSG_TRANSFERSTATUS;
  252. break;
  253. default:
  254. Type = MSG_OTHER;
  255. break;
  256. }
  257. return DoPostMessage(Type, wParam, lParam);
  258. }
  259. //---------------------------------------------------------------------------
  260. void __fastcall CopyContact(TFtpsCertificateData::TContact & Dest,
  261. const t_SslCertData::t_Contact& Source)
  262. {
  263. Dest.Organization = Source.Organization;
  264. Dest.Unit = Source.Unit;
  265. Dest.CommonName = Source.CommonName;
  266. Dest.Mail = Source.Mail;
  267. Dest.Country = Source.Country;
  268. Dest.StateProvince = Source.StateProvince;
  269. Dest.Town = Source.Town;
  270. Dest.Other = Source.Other;
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall CopyValidityTime(TFtpsCertificateData::TValidityTime & Dest,
  274. const t_SslCertData::t_validTime& Source)
  275. {
  276. Dest.Year = Source.y;
  277. Dest.Month = Source.M;
  278. Dest.Day = Source.d;
  279. Dest.Hour = Source.h;
  280. Dest.Min = Source.m;
  281. Dest.Sec = Source.s;
  282. }
  283. //---------------------------------------------------------------------------
  284. void __fastcall CopyFileTime(TRemoteFileTime & Dest, const t_directory::t_direntry::t_date & Source)
  285. {
  286. Dest.Year = Source.year;
  287. Dest.Month = Source.month;
  288. Dest.Day = Source.day;
  289. Dest.Hour = Source.hour;
  290. Dest.Minute = Source.minute;
  291. Dest.Second = Source.second;
  292. Dest.HasTime = Source.hastime;
  293. Dest.HasDate = Source.hasdate;
  294. Dest.HasSeconds = Source.hasseconds;
  295. Dest.Utc = Source.utc;
  296. }
  297. //---------------------------------------------------------------------------
  298. bool __fastcall TFileZillaIntf::HandleMessage(WPARAM wParam, LPARAM lParam)
  299. {
  300. bool Result;
  301. CString a;
  302. unsigned int MessageID = FZ_MSG_ID(wParam);
  303. switch (MessageID)
  304. {
  305. case FZ_MSG_STATUS:
  306. {
  307. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  308. t_ffam_statusmessage * Status = (t_ffam_statusmessage *)lParam;
  309. ASSERT(Status->post);
  310. Result = HandleStatus(Status->status, Status->type);
  311. delete Status;
  312. }
  313. break;
  314. case FZ_MSG_ASYNCREQUEST:
  315. if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_OVERWRITE)
  316. {
  317. int RequestResult;
  318. wchar_t FileName1[MAX_PATH];
  319. COverwriteRequestData * Data = (COverwriteRequestData *)lParam;
  320. try
  321. {
  322. ASSERT(Data != NULL);
  323. wcsncpy(FileName1, Data->FileName1, LENOF(FileName1));
  324. FileName1[LENOF(FileName1) - 1] = L'\0';
  325. TRemoteFileTime RemoteTime;
  326. CopyFileTime(RemoteTime, Data->remotetime);
  327. Result = HandleAsynchRequestOverwrite(
  328. FileName1, LENOF(FileName1), Data->FileName2, Data->path1, Data->path2,
  329. Data->size1, Data->size2,
  330. (Data->localtime != NULL) ? Data->localtime->GetTime() : 0,
  331. (Data->localtime != NULL) && ((Data->localtime->GetHour() != 0) || (Data->localtime->GetMinute() != 0)),
  332. RemoteTime,
  333. reinterpret_cast<void*>(Data->pTransferFile->nUserData), RequestResult);
  334. }
  335. catch(...)
  336. {
  337. FFileZillaApi->SetAsyncRequestResult(FILEEXISTS_SKIP, Data);
  338. throw;
  339. }
  340. if (Result)
  341. {
  342. Data->FileName1 = FileName1;
  343. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, Data),
  344. L"setasyncrequestresult");
  345. }
  346. }
  347. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_VERIFYCERT)
  348. {
  349. int RequestResult;
  350. CVerifyCertRequestData * AData = (CVerifyCertRequestData *)lParam;
  351. try
  352. {
  353. ASSERT(AData != NULL);
  354. TFtpsCertificateData Data;
  355. CopyContact(Data.Subject, AData->pCertData->subject);
  356. CopyContact(Data.Issuer, AData->pCertData->issuer);
  357. CopyValidityTime(Data.ValidFrom, AData->pCertData->validFrom);
  358. CopyValidityTime(Data.ValidUntil, AData->pCertData->validUntil);
  359. Data.Hash = AData->pCertData->hash;
  360. Data.VerificationResult = AData->pCertData->verificationResult;
  361. Data.VerificationDepth = AData->pCertData->verificationDepth;
  362. Result = HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  363. }
  364. catch(...)
  365. {
  366. FFileZillaApi->SetAsyncRequestResult(0, AData);
  367. throw;
  368. }
  369. if (Result)
  370. {
  371. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  372. L"setasyncrequestresult");
  373. }
  374. }
  375. else if (FZ_MSG_PARAM(wParam) == FZ_ASYNCREQUEST_NEEDPASS)
  376. {
  377. int RequestResult = 0;
  378. CNeedPassRequestData * AData = (CNeedPassRequestData *)lParam;
  379. try
  380. {
  381. TNeedPassRequestData Data;
  382. Data.Password = AData->Password.GetBuffer(AData->Password.GetLength());
  383. Result = HandleAsynchRequestNeedPass(Data, RequestResult);
  384. AData->Password.ReleaseBuffer(AData->Password.GetLength());
  385. if (Result && (RequestResult == TFileZillaIntf::REPLY_OK))
  386. {
  387. AData->Password = Data.Password;
  388. free(Data.Password);
  389. Data.Password = NULL;
  390. }
  391. }
  392. catch(...)
  393. {
  394. FFileZillaApi->SetAsyncRequestResult(0, AData);
  395. throw;
  396. }
  397. if (Result)
  398. {
  399. Result = Check(FFileZillaApi->SetAsyncRequestResult(RequestResult, AData),
  400. L"setasyncrequestresult");
  401. }
  402. }
  403. else
  404. {
  405. // FZ_ASYNCREQUEST_GSS_AUTHFAILED
  406. // FZ_ASYNCREQUEST_GSS_NEEDUSER
  407. // FZ_ASYNCREQUEST_GSS_NEEDPASS
  408. ASSERT(FALSE);
  409. Result = false;
  410. }
  411. break;
  412. case FZ_MSG_LISTDATA:
  413. {
  414. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  415. t_directory * Directory = (t_directory *)lParam;
  416. CString Path = Directory->path.GetPath();
  417. std::vector<TListDataEntry> Entries(Directory->num);
  418. for (int Index = 0; Index < Directory->num; Index++)
  419. {
  420. t_directory::t_direntry & Source = Directory->direntry[Index];
  421. TListDataEntry & Dest = Entries[Index];
  422. Dest.Name = Source.name;
  423. Dest.Permissions = Source.permissionstr;
  424. Dest.OwnerGroup = Source.ownergroup;
  425. Dest.Size = Source.size;
  426. Dest.Dir = Source.dir;
  427. Dest.Link = Source.bLink;
  428. CopyFileTime(Dest.Time, Source.date);
  429. Dest.LinkTarget = Source.linkTarget;
  430. }
  431. int Num = Directory->num;
  432. TListDataEntry * pEntries = Num > 0 ? &Entries[0] : NULL;
  433. Result = HandleListData(Path, pEntries, Num);
  434. delete Directory;
  435. }
  436. break;
  437. case FZ_MSG_TRANSFERSTATUS:
  438. {
  439. ASSERT(FZ_MSG_PARAM(wParam) == 0);
  440. t_ffam_transferstatus * Status = (t_ffam_transferstatus *)lParam;
  441. if (Status != NULL)
  442. {
  443. Result = HandleTransferStatus(true, Status->transfersize, Status->bytes,
  444. Status->percent, Status->timeelapsed, Status->timeleft,
  445. Status->transferrate, Status->bFileTransfer);
  446. delete Status;
  447. }
  448. else
  449. {
  450. Result = HandleTransferStatus(false, -1, -1, -1, -1, -1, -1, false);
  451. }
  452. }
  453. break;
  454. case FZ_MSG_REPLY:
  455. Result = HandleReply(FZ_MSG_PARAM(wParam), lParam);
  456. break;
  457. case FZ_MSG_CAPABILITIES:
  458. Result = HandleCapabilities((TFTPServerCapabilities *)lParam);
  459. break;
  460. case FZ_MSG_SOCKETSTATUS:
  461. case FZ_MSG_SECURESERVER:
  462. case FZ_MSG_QUITCOMPLETE:
  463. default:
  464. ASSERT(false);
  465. Result = false;
  466. break;
  467. }
  468. return Result;
  469. }
  470. //---------------------------------------------------------------------------
  471. bool __fastcall TFileZillaIntf::CheckError(int /*ReturnCode*/, const wchar_t * /*Context*/)
  472. {
  473. return false;
  474. }
  475. //---------------------------------------------------------------------------
  476. inline bool __fastcall TFileZillaIntf::Check(int ReturnCode,
  477. const wchar_t * Context, int Expected)
  478. {
  479. if ((ReturnCode & (Expected == -1 ? FZ_REPLY_OK : Expected)) == ReturnCode)
  480. {
  481. return true;
  482. }
  483. else
  484. {
  485. return CheckError(ReturnCode, Context);
  486. }
  487. }