FileZillaApi.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. //---------------------------------------------------------------------------
  2. #include "stdafx.h"
  3. #include "FileZillaApi.h"
  4. #include "mainthread.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Konstruktion/Destruktion
  7. //////////////////////////////////////////////////////////////////////
  8. CFileZillaApi::CFileZillaApi()
  9. {
  10. m_nInternalMessageID=0;
  11. m_pMainThread=0;
  12. m_bInitialized=FALSE;
  13. }
  14. CFileZillaApi::~CFileZillaApi()
  15. {
  16. Destroy();
  17. }
  18. int CFileZillaApi::Init(TFileZillaIntern * Intern, CFileZillaTools * pTools)
  19. {
  20. //Check if call allowed
  21. if (m_bInitialized)
  22. return FZ_REPLY_ALREADYINIZIALIZED;
  23. //Initialize variables
  24. m_nInternalMessageID=RegisterWindowMessage( L"FileZillaInternalApiMessage{F958620E-040C-4b33-A091-7E04E10AA660}" );
  25. if (!m_nInternalMessageID)
  26. return FZ_REPLY_NOTINITIALIZED;
  27. //Create thread object
  28. m_pMainThread = CMainThread::Create(THREAD_PRIORITY_BELOW_NORMAL, CREATE_SUSPENDED);
  29. //Initialize Thread variables
  30. m_pMainThread->m_nInternalMessageID=m_nInternalMessageID;
  31. m_pMainThread->m_pTools=pTools;
  32. m_pMainThread->InitIntern(Intern);
  33. //Resume Thread
  34. m_pMainThread->ResumeThread();
  35. //Initialization OK
  36. m_bInitialized=TRUE;
  37. return FZ_REPLY_OK;
  38. }
  39. int CFileZillaApi::IsConnected()
  40. {
  41. if (!m_bInitialized)
  42. return FZ_REPLY_NOTINITIALIZED;
  43. return m_pMainThread->IsConnected()?FZ_REPLY_OK:FZ_REPLY_NOTCONNECTED;
  44. }
  45. int CFileZillaApi::IsBusy()
  46. {
  47. if (!m_bInitialized)
  48. return FZ_REPLY_NOTINITIALIZED;
  49. return m_pMainThread->IsBusy()?FZ_REPLY_BUSY:FZ_REPLY_IDLE;
  50. }
  51. int CFileZillaApi::Connect(const t_server &server)
  52. {
  53. //Check parameters
  54. if (server.host==L"" || server.port<1 || server.port>65535)
  55. return FZ_REPLY_INVALIDPARAM;
  56. #ifndef MPEXT_NO_GSS
  57. BOOL bUseGSS = FALSE;
  58. if (GetOptionVal(OPTION_USEGSS))
  59. {
  60. USES_CONVERSION;
  61. CString GssServers = GetOption(OPTION_GSSSERVERS);
  62. hostent *fullname = gethostbyname(T2CA(server.host));
  63. CString host;
  64. if (fullname)
  65. host = fullname->h_name;
  66. else
  67. host = server.host;
  68. host.MakeLower();
  69. int i;
  70. while ((i=GssServers.Find( L";" ))!=-1)
  71. {
  72. if ((L"."+GssServers.Left(i))==host.Right(GssServers.Left(i).GetLength()+1) || GssServers.Left(i)==host)
  73. {
  74. bUseGSS = TRUE;
  75. break;
  76. }
  77. GssServers = GssServers.Mid(i+1);
  78. }
  79. }
  80. if (!bUseGSS && server.user == L"")
  81. return FZ_REPLY_INVALIDPARAM;
  82. #endif
  83. if (!(server.nServerType&FZ_SERVERTYPE_HIGHMASK))
  84. return FZ_REPLY_INVALIDPARAM;
  85. //Check if call allowed
  86. if (!m_bInitialized)
  87. return FZ_REPLY_NOTINITIALIZED;
  88. if (m_pMainThread->IsBusy())
  89. return FZ_REPLY_BUSY;
  90. t_command command;
  91. command.id=FZ_COMMAND_CONNECT;
  92. command.server=server;
  93. m_pMainThread->Command(command);
  94. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  95. }
  96. int CFileZillaApi::List()
  97. {
  98. //Check if call allowed
  99. if (!m_bInitialized)
  100. return FZ_REPLY_NOTINITIALIZED;
  101. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  102. return FZ_REPLY_NOTCONNECTED;
  103. CServerPath path;
  104. // seems to be incorrectly skipped when cache is not required
  105. if (!m_pMainThread->GetWorkingDirPath(path) || path.IsEmpty())
  106. m_pMainThread->GetCurrentPath(path);
  107. if (m_pMainThread->IsBusy())
  108. return FZ_REPLY_BUSY;
  109. t_command command;
  110. command.id=FZ_COMMAND_LIST;
  111. command.path = path;
  112. m_pMainThread->Command(command);
  113. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  114. }
  115. int CFileZillaApi::Cancel()
  116. {
  117. //Check if call allowed
  118. if (!m_bInitialized)
  119. return FZ_REPLY_NOTINITIALIZED;
  120. if (IsBusy()!=FZ_REPLY_BUSY)
  121. return FZ_REPLY_NOTBUSY;
  122. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_CANCEL, 0);
  123. return FZ_REPLY_WOULDBLOCK;
  124. }
  125. void CFileZillaApi::Destroy()
  126. {
  127. if (!m_bInitialized)
  128. return;
  129. DebugAssert(m_pMainThread);
  130. HANDLE tmp=m_pMainThread->m_hThread;
  131. m_pMainThread->Quit();
  132. //Wait for the main thread to quit
  133. WaitForSingleObject(tmp, 10000);
  134. m_pMainThread=0;
  135. m_bInitialized=FALSE;
  136. }
  137. int CFileZillaApi::Disconnect()
  138. {
  139. //Check if call allowed
  140. if (!m_bInitialized)
  141. return FZ_REPLY_NOTINITIALIZED;
  142. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  143. return FZ_REPLY_NOTCONNECTED;
  144. if (IsBusy()==FZ_REPLY_BUSY)
  145. return FZ_REPLY_BUSY;
  146. m_pMainThread->PostThreadMessage(m_nInternalMessageID,FZAPI_THREADMSG_DISCONNECT,0);
  147. return FZ_REPLY_WOULDBLOCK;
  148. }
  149. int CFileZillaApi::List(const CServerPath& path)
  150. {
  151. //Check if call allowed
  152. if (!m_bInitialized)
  153. return FZ_REPLY_NOTINITIALIZED;
  154. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  155. return FZ_REPLY_NOTCONNECTED;
  156. if (path.IsEmpty())
  157. return FZ_REPLY_INVALIDPARAM;
  158. if (m_pMainThread->IsBusy())
  159. return FZ_REPLY_BUSY;
  160. t_command command;
  161. command.id=FZ_COMMAND_LIST;
  162. command.path=path;
  163. m_pMainThread->Command(command);
  164. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  165. }
  166. int CFileZillaApi::ListFile(CString FileName, const CServerPath & path)
  167. {
  168. //Check if call allowed
  169. if (!m_bInitialized)
  170. return FZ_REPLY_NOTINITIALIZED;
  171. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  172. return FZ_REPLY_NOTCONNECTED;
  173. if (path.IsEmpty())
  174. return FZ_REPLY_INVALIDPARAM;
  175. if (FileName=="")
  176. return FZ_REPLY_INVALIDPARAM;
  177. if (m_pMainThread->IsBusy())
  178. return FZ_REPLY_BUSY;
  179. t_command command;
  180. command.id=FZ_COMMAND_LISTFILE;
  181. command.param1=FileName;
  182. command.path=path;
  183. m_pMainThread->Command(command);
  184. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  185. }
  186. int CFileZillaApi::FileTransfer(const t_transferfile &TransferFile)
  187. {
  188. //Check if call allowed
  189. if (!m_bInitialized)
  190. return FZ_REPLY_NOTINITIALIZED;
  191. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  192. return FZ_REPLY_NOTCONNECTED;
  193. if (TransferFile.remotefile==L"" || TransferFile.localfile==L"" || TransferFile.remotepath.IsEmpty())
  194. return FZ_REPLY_INVALIDPARAM;
  195. if (IsBusy()==FZ_REPLY_BUSY)
  196. return FZ_REPLY_BUSY;
  197. t_command command;
  198. command.id=FZ_COMMAND_FILETRANSFER;
  199. command.transferfile=TransferFile;
  200. m_pMainThread->Command(command);
  201. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  202. }
  203. int CFileZillaApi::GetCurrentServer(t_server &server)
  204. {
  205. //Check if call allowed
  206. if (!m_bInitialized)
  207. return FZ_REPLY_NOTINITIALIZED;
  208. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  209. return FZ_REPLY_NOTCONNECTED;
  210. if (m_pMainThread->GetCurrentServer(server))
  211. return FZ_REPLY_OK;
  212. else
  213. return FZ_REPLY_NOTCONNECTED;
  214. }
  215. int CFileZillaApi::SetCurrentPath(CServerPath path)
  216. {
  217. //Check if call allowed
  218. if (!m_bInitialized)
  219. return FZ_REPLY_NOTINITIALIZED;
  220. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  221. return FZ_REPLY_NOTCONNECTED;
  222. m_pMainThread->SetCurrentPath(path);
  223. return FZ_REPLY_OK;
  224. }
  225. int CFileZillaApi::GetCurrentPath(CServerPath & path)
  226. {
  227. //Check if call allowed
  228. if (!m_bInitialized)
  229. return FZ_REPLY_NOTINITIALIZED;
  230. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  231. return FZ_REPLY_NOTCONNECTED;
  232. return (m_pMainThread->GetCurrentPath(path) ? FZ_REPLY_OK : FZ_REPLY_NOTCONNECTED);
  233. }
  234. bool CFileZillaApi::UsingMlsd()
  235. {
  236. //Check if call allowed
  237. if (!m_bInitialized)
  238. return false;
  239. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  240. return false;
  241. return m_pMainThread->UsingMlsd();
  242. }
  243. bool CFileZillaApi::UsingUtf8()
  244. {
  245. //Check if call allowed
  246. if (!m_bInitialized)
  247. return false;
  248. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  249. return false;
  250. return m_pMainThread->UsingUtf8();
  251. }
  252. std::string CFileZillaApi::GetTlsVersionStr()
  253. {
  254. //Check if call allowed
  255. if (!m_bInitialized)
  256. return std::string();
  257. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  258. return std::string();
  259. return m_pMainThread->GetTlsVersionStr();
  260. }
  261. std::string CFileZillaApi::GetCipherName()
  262. {
  263. //Check if call allowed
  264. if (!m_bInitialized)
  265. return std::string();
  266. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  267. return std::string();
  268. return m_pMainThread->GetCipherName();
  269. }
  270. int CFileZillaApi::CustomCommand(CString CustomCommand)
  271. {
  272. //Check if call allowed
  273. if (!m_bInitialized)
  274. return FZ_REPLY_NOTINITIALIZED;
  275. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  276. return FZ_REPLY_NOTCONNECTED;
  277. if (IsBusy()==FZ_REPLY_BUSY)
  278. return FZ_REPLY_BUSY;
  279. t_server server;
  280. int res=GetCurrentServer(server);
  281. if (res!=FZ_REPLY_OK)
  282. return res;
  283. if (CustomCommand==L"")
  284. return FZ_REPLY_INVALIDPARAM;
  285. t_command command;
  286. command.id=FZ_COMMAND_CUSTOMCOMMAND;
  287. command.param1=CustomCommand;
  288. m_pMainThread->Command(command);
  289. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  290. }
  291. int CFileZillaApi::Delete(CString FileName, const CServerPath &path /*=CServerPath()*/)
  292. {
  293. //Check if call allowed
  294. if (!m_bInitialized)
  295. return FZ_REPLY_NOTINITIALIZED;
  296. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  297. return FZ_REPLY_NOTCONNECTED;
  298. if (IsBusy()==FZ_REPLY_BUSY)
  299. return FZ_REPLY_BUSY;
  300. if (FileName=="")
  301. return FZ_REPLY_INVALIDPARAM;
  302. CServerPath path2=path;
  303. if (path2.IsEmpty())
  304. {
  305. m_pMainThread->GetCurrentPath(path2);
  306. if (path2.IsEmpty())
  307. return FZ_REPLY_INVALIDPARAM;
  308. }
  309. t_command command;
  310. command.id=FZ_COMMAND_DELETE;
  311. command.param1=FileName;
  312. command.path=path2;
  313. m_pMainThread->Command(command);
  314. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  315. }
  316. int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
  317. {
  318. //Check if call allowed
  319. if (!m_bInitialized)
  320. return FZ_REPLY_NOTINITIALIZED;
  321. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  322. return FZ_REPLY_NOTCONNECTED;
  323. if (IsBusy()==FZ_REPLY_BUSY)
  324. return FZ_REPLY_BUSY;
  325. if (DirName==L"")
  326. return FZ_REPLY_INVALIDPARAM;
  327. CServerPath path2=path;
  328. if (path2.IsEmpty())
  329. {
  330. m_pMainThread->GetCurrentPath(path2);
  331. if (path2.IsEmpty())
  332. return FZ_REPLY_INVALIDPARAM;
  333. }
  334. t_command command;
  335. command.id=FZ_COMMAND_REMOVEDIR;
  336. command.param1=DirName;
  337. command.path=path2;
  338. m_pMainThread->Command(command);
  339. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  340. }
  341. int CFileZillaApi::MakeDir(const CServerPath &path)
  342. {
  343. //Check if call allowed
  344. if (!m_bInitialized)
  345. return FZ_REPLY_NOTINITIALIZED;
  346. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  347. return FZ_REPLY_NOTCONNECTED;
  348. if (IsBusy()==FZ_REPLY_BUSY)
  349. return FZ_REPLY_BUSY;
  350. if (path.IsEmpty() || !path.HasParent())
  351. return FZ_REPLY_INVALIDPARAM;
  352. t_command command;
  353. command.id=FZ_COMMAND_MAKEDIR;
  354. command.path=path;
  355. m_pMainThread->Command(command);
  356. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  357. }
  358. int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
  359. {
  360. //Check if call allowed
  361. if (!m_bInitialized)
  362. return FZ_REPLY_NOTINITIALIZED;
  363. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  364. return FZ_REPLY_NOTCONNECTED;
  365. if (IsBusy()==FZ_REPLY_BUSY)
  366. return FZ_REPLY_BUSY;
  367. if (oldName==L"" || newName==L"")
  368. return FZ_REPLY_INVALIDPARAM;
  369. CServerPath path2 = path;
  370. if (path2.IsEmpty())
  371. {
  372. m_pMainThread->GetCurrentPath(path2);
  373. if (path2.IsEmpty())
  374. return FZ_REPLY_INVALIDPARAM;
  375. }
  376. t_command command;
  377. command.id = FZ_COMMAND_RENAME;
  378. command.param1 = oldName;
  379. command.param2 = newName;
  380. command.path = path2;
  381. command.newPath = newPath;
  382. m_pMainThread->Command(command);
  383. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  384. }
  385. int CFileZillaApi::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  386. {
  387. if (!this || !pData)
  388. return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;
  389. if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
  390. return FZ_REPLY_CRITICALERROR;
  391. if (!m_bInitialized)
  392. {
  393. delete pData;
  394. return FZ_REPLY_NOTINITIALIZED;
  395. }
  396. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  397. {
  398. delete pData;
  399. return FZ_REPLY_NOTCONNECTED;
  400. }
  401. switch(pData->nRequestType)
  402. {
  403. case FZ_ASYNCREQUEST_OVERWRITE:
  404. break;
  405. case FZ_ASYNCREQUEST_VERIFYCERT:
  406. if (!((CVerifyCertRequestData *)pData)->pCertData)
  407. {
  408. delete pData;
  409. return FZ_REPLY_INVALIDPARAM;
  410. }
  411. break;
  412. case FZ_ASYNCREQUEST_NEEDPASS:
  413. break;
  414. #ifndef MPEXT_NO_GSS
  415. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  416. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  417. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  418. break;
  419. #endif
  420. default:
  421. delete pData;
  422. return FZ_REPLY_INVALIDPARAM;
  423. }
  424. pData->nRequestResult = nAction;
  425. if (!m_pMainThread)
  426. {
  427. delete pData;
  428. return FZ_REPLY_NOTINITIALIZED;
  429. }
  430. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY, (LPARAM)pData);
  431. return FZ_REPLY_OK;
  432. }
  433. int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
  434. {
  435. //Check if call allowed
  436. if (!m_bInitialized)
  437. return FZ_REPLY_NOTINITIALIZED;
  438. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  439. return FZ_REPLY_NOTCONNECTED;
  440. if (IsBusy()==FZ_REPLY_BUSY)
  441. return FZ_REPLY_BUSY;
  442. if (FileName==L"")
  443. return FZ_REPLY_INVALIDPARAM;
  444. t_command command;
  445. command.id=FZ_COMMAND_CHMOD;
  446. command.param1=FileName;
  447. command.param4=nValue;
  448. command.path=path;
  449. m_pMainThread->Command(command);
  450. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  451. }
  452. void CFileZillaApi::SetDebugLevel(int nDebugLevel)
  453. {
  454. m_pMainThread->GetIntern()->SetDebugLevel(nDebugLevel);
  455. }
  456. //CAsyncRequestData derived classes
  457. CAsyncRequestData::CAsyncRequestData()
  458. {
  459. nRequestType = 0;
  460. nRequestID = 0;
  461. nRequestResult = 0;
  462. }
  463. CAsyncRequestData::~CAsyncRequestData()
  464. {
  465. }
  466. COverwriteRequestData::COverwriteRequestData()
  467. {
  468. size1 = 0;
  469. size2 = 0;
  470. nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
  471. localtime=0;
  472. remotetime.hasdate = false;
  473. pTransferFile=0;
  474. }
  475. COverwriteRequestData::~COverwriteRequestData()
  476. {
  477. delete pTransferFile;
  478. delete localtime;
  479. }
  480. CVerifyCertRequestData::CVerifyCertRequestData()
  481. {
  482. nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
  483. pCertData=0;
  484. }
  485. CVerifyCertRequestData::~CVerifyCertRequestData()
  486. {
  487. delete pCertData;
  488. }
  489. CNeedPassRequestData::CNeedPassRequestData()
  490. {
  491. nRequestType=FZ_ASYNCREQUEST_NEEDPASS;
  492. nOldOpState=0;
  493. }
  494. CNeedPassRequestData::~CNeedPassRequestData()
  495. {
  496. }
  497. #ifndef MPEXT_NO_GSS
  498. CGssNeedPassRequestData::CGssNeedPassRequestData()
  499. {
  500. nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
  501. }
  502. CGssNeedPassRequestData::~CGssNeedPassRequestData()
  503. {
  504. }
  505. CGssNeedUserRequestData::CGssNeedUserRequestData()
  506. {
  507. nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
  508. }
  509. CGssNeedUserRequestData::~CGssNeedUserRequestData()
  510. {
  511. }
  512. #endif