FileZillaApi.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. //---------------------------------------------------------------------------
  2. #include "FileZillaPCH.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::Cancel()
  97. {
  98. //Check if call allowed
  99. if (!m_bInitialized)
  100. return FZ_REPLY_NOTINITIALIZED;
  101. if (IsBusy()!=FZ_REPLY_BUSY)
  102. return FZ_REPLY_NOTBUSY;
  103. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_CANCEL, 0);
  104. return FZ_REPLY_WOULDBLOCK;
  105. }
  106. void CFileZillaApi::Destroy()
  107. {
  108. if (!m_bInitialized)
  109. return;
  110. DebugAssert(m_pMainThread);
  111. HANDLE tmp=m_pMainThread->m_hThread;
  112. m_pMainThread->Quit();
  113. //Wait for the main thread to quit
  114. WaitForSingleObject(tmp, 10000);
  115. m_pMainThread=0;
  116. m_bInitialized=FALSE;
  117. }
  118. int CFileZillaApi::Disconnect()
  119. {
  120. //Check if call allowed
  121. if (!m_bInitialized)
  122. return FZ_REPLY_NOTINITIALIZED;
  123. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  124. return FZ_REPLY_NOTCONNECTED;
  125. if (IsBusy()==FZ_REPLY_BUSY)
  126. return FZ_REPLY_BUSY;
  127. m_pMainThread->PostThreadMessage(m_nInternalMessageID,FZAPI_THREADMSG_DISCONNECT,0);
  128. return FZ_REPLY_WOULDBLOCK;
  129. }
  130. int CFileZillaApi::List(const CServerPath& path)
  131. {
  132. //Check if call allowed
  133. if (!m_bInitialized)
  134. return FZ_REPLY_NOTINITIALIZED;
  135. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  136. return FZ_REPLY_NOTCONNECTED;
  137. if (path.IsEmpty() && !m_pMainThread->GetIntern()->GetOptionVal(OPTION_MPEXT_WORK_FROM_CWD))
  138. return FZ_REPLY_INVALIDPARAM;
  139. if (m_pMainThread->IsBusy())
  140. return FZ_REPLY_BUSY;
  141. t_command command;
  142. command.id=FZ_COMMAND_LIST;
  143. command.path=path;
  144. m_pMainThread->Command(command);
  145. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  146. }
  147. int CFileZillaApi::ListFile(CString FileName, const CServerPath & path)
  148. {
  149. //Check if call allowed
  150. if (!m_bInitialized)
  151. return FZ_REPLY_NOTINITIALIZED;
  152. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  153. return FZ_REPLY_NOTCONNECTED;
  154. if (path.IsEmpty())
  155. return FZ_REPLY_INVALIDPARAM;
  156. if (FileName=="")
  157. return FZ_REPLY_INVALIDPARAM;
  158. if (m_pMainThread->IsBusy())
  159. return FZ_REPLY_BUSY;
  160. t_command command;
  161. command.id=FZ_COMMAND_LISTFILE;
  162. command.param1=FileName;
  163. command.path=path;
  164. m_pMainThread->Command(command);
  165. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  166. }
  167. int CFileZillaApi::FileTransfer(const t_transferfile &TransferFile)
  168. {
  169. //Check if call allowed
  170. if (!m_bInitialized)
  171. return FZ_REPLY_NOTINITIALIZED;
  172. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  173. return FZ_REPLY_NOTCONNECTED;
  174. if (TransferFile.remotefile==L"" || TransferFile.localfile==L"" || (TransferFile.remotepath.IsEmpty() && !m_pMainThread->GetIntern()->GetOptionVal(OPTION_MPEXT_WORK_FROM_CWD)))
  175. return FZ_REPLY_INVALIDPARAM;
  176. if (IsBusy()==FZ_REPLY_BUSY)
  177. return FZ_REPLY_BUSY;
  178. t_command command;
  179. command.id=FZ_COMMAND_FILETRANSFER;
  180. command.transferfile=TransferFile;
  181. m_pMainThread->Command(command);
  182. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  183. }
  184. int CFileZillaApi::GetCurrentServer(t_server &server)
  185. {
  186. //Check if call allowed
  187. if (!m_bInitialized)
  188. return FZ_REPLY_NOTINITIALIZED;
  189. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  190. return FZ_REPLY_NOTCONNECTED;
  191. if (m_pMainThread->GetCurrentServer(server))
  192. return FZ_REPLY_OK;
  193. else
  194. return FZ_REPLY_NOTCONNECTED;
  195. }
  196. int CFileZillaApi::SetCurrentPath(CServerPath path)
  197. {
  198. //Check if call allowed
  199. if (!m_bInitialized)
  200. return FZ_REPLY_NOTINITIALIZED;
  201. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  202. return FZ_REPLY_NOTCONNECTED;
  203. m_pMainThread->SetCurrentPath(path);
  204. return FZ_REPLY_OK;
  205. }
  206. int CFileZillaApi::GetCurrentPath(CServerPath & path)
  207. {
  208. //Check if call allowed
  209. if (!m_bInitialized)
  210. return FZ_REPLY_NOTINITIALIZED;
  211. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  212. return FZ_REPLY_NOTCONNECTED;
  213. return (m_pMainThread->GetCurrentPath(path) ? FZ_REPLY_OK : FZ_REPLY_NOTCONNECTED);
  214. }
  215. bool CFileZillaApi::UsingMlsd()
  216. {
  217. //Check if call allowed
  218. if (!m_bInitialized)
  219. return false;
  220. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  221. return false;
  222. return m_pMainThread->UsingMlsd();
  223. }
  224. bool CFileZillaApi::UsingUtf8()
  225. {
  226. //Check if call allowed
  227. if (!m_bInitialized)
  228. return false;
  229. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  230. return false;
  231. return m_pMainThread->UsingUtf8();
  232. }
  233. std::string CFileZillaApi::GetTlsVersionStr()
  234. {
  235. //Check if call allowed
  236. if (!m_bInitialized)
  237. return std::string();
  238. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  239. return std::string();
  240. return m_pMainThread->GetTlsVersionStr();
  241. }
  242. std::string CFileZillaApi::GetCipherName()
  243. {
  244. //Check if call allowed
  245. if (!m_bInitialized)
  246. return std::string();
  247. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  248. return std::string();
  249. return m_pMainThread->GetCipherName();
  250. }
  251. int CFileZillaApi::CustomCommand(CString CustomCommand)
  252. {
  253. //Check if call allowed
  254. if (!m_bInitialized)
  255. return FZ_REPLY_NOTINITIALIZED;
  256. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  257. return FZ_REPLY_NOTCONNECTED;
  258. if (IsBusy()==FZ_REPLY_BUSY)
  259. return FZ_REPLY_BUSY;
  260. t_server server;
  261. int res=GetCurrentServer(server);
  262. if (res!=FZ_REPLY_OK)
  263. return res;
  264. if (CustomCommand==L"")
  265. return FZ_REPLY_INVALIDPARAM;
  266. t_command command;
  267. command.id=FZ_COMMAND_CUSTOMCOMMAND;
  268. command.param1=CustomCommand;
  269. m_pMainThread->Command(command);
  270. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  271. }
  272. int CFileZillaApi::Delete(CString FileName, const CServerPath &path, bool filenameOnly)
  273. {
  274. //Check if call allowed
  275. if (!m_bInitialized)
  276. return FZ_REPLY_NOTINITIALIZED;
  277. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  278. return FZ_REPLY_NOTCONNECTED;
  279. if (IsBusy()==FZ_REPLY_BUSY)
  280. return FZ_REPLY_BUSY;
  281. if (FileName=="")
  282. return FZ_REPLY_INVALIDPARAM;
  283. CServerPath path2=path;
  284. if (path2.IsEmpty())
  285. {
  286. m_pMainThread->GetCurrentPath(path2);
  287. if (path2.IsEmpty())
  288. return FZ_REPLY_INVALIDPARAM;
  289. }
  290. t_command command;
  291. command.id=FZ_COMMAND_DELETE;
  292. command.param1=FileName;
  293. command.path=path2;
  294. command.param4 = (filenameOnly ? 1 : 0);
  295. m_pMainThread->Command(command);
  296. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  297. }
  298. int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
  299. {
  300. //Check if call allowed
  301. if (!m_bInitialized)
  302. return FZ_REPLY_NOTINITIALIZED;
  303. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  304. return FZ_REPLY_NOTCONNECTED;
  305. if (IsBusy()==FZ_REPLY_BUSY)
  306. return FZ_REPLY_BUSY;
  307. if (DirName==L"")
  308. return FZ_REPLY_INVALIDPARAM;
  309. CServerPath path2=path;
  310. if (path2.IsEmpty())
  311. {
  312. m_pMainThread->GetCurrentPath(path2);
  313. if (path2.IsEmpty())
  314. return FZ_REPLY_INVALIDPARAM;
  315. }
  316. t_command command;
  317. command.id=FZ_COMMAND_REMOVEDIR;
  318. command.param1=DirName;
  319. command.path=path2;
  320. m_pMainThread->Command(command);
  321. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  322. }
  323. int CFileZillaApi::MakeDir(const CServerPath &path)
  324. {
  325. //Check if call allowed
  326. if (!m_bInitialized)
  327. return FZ_REPLY_NOTINITIALIZED;
  328. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  329. return FZ_REPLY_NOTCONNECTED;
  330. if (IsBusy()==FZ_REPLY_BUSY)
  331. return FZ_REPLY_BUSY;
  332. if (path.IsEmpty() || !path.HasParent())
  333. return FZ_REPLY_INVALIDPARAM;
  334. t_command command;
  335. command.id=FZ_COMMAND_MAKEDIR;
  336. command.path=path;
  337. m_pMainThread->Command(command);
  338. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  339. }
  340. int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
  341. {
  342. //Check if call allowed
  343. if (!m_bInitialized)
  344. return FZ_REPLY_NOTINITIALIZED;
  345. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  346. return FZ_REPLY_NOTCONNECTED;
  347. if (IsBusy()==FZ_REPLY_BUSY)
  348. return FZ_REPLY_BUSY;
  349. if (oldName==L"" || newName==L"")
  350. return FZ_REPLY_INVALIDPARAM;
  351. CServerPath path2 = path;
  352. if (path2.IsEmpty())
  353. {
  354. m_pMainThread->GetCurrentPath(path2);
  355. if (path2.IsEmpty())
  356. return FZ_REPLY_INVALIDPARAM;
  357. }
  358. t_command command;
  359. command.id = FZ_COMMAND_RENAME;
  360. command.param1 = oldName;
  361. command.param2 = newName;
  362. command.path = path2;
  363. command.newPath = newPath;
  364. m_pMainThread->Command(command);
  365. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  366. }
  367. int CFileZillaApi::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  368. {
  369. if (!pData)
  370. return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;
  371. if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
  372. return FZ_REPLY_CRITICALERROR;
  373. if (!m_bInitialized)
  374. {
  375. delete pData;
  376. return FZ_REPLY_NOTINITIALIZED;
  377. }
  378. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  379. {
  380. delete pData;
  381. return FZ_REPLY_NOTCONNECTED;
  382. }
  383. switch(pData->nRequestType)
  384. {
  385. case FZ_ASYNCREQUEST_OVERWRITE:
  386. break;
  387. case FZ_ASYNCREQUEST_VERIFYCERT:
  388. if (!((CVerifyCertRequestData *)pData)->pCertData)
  389. {
  390. delete pData;
  391. return FZ_REPLY_INVALIDPARAM;
  392. }
  393. break;
  394. case FZ_ASYNCREQUEST_NEEDPASS:
  395. break;
  396. #ifndef MPEXT_NO_GSS
  397. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  398. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  399. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  400. break;
  401. #endif
  402. default:
  403. delete pData;
  404. return FZ_REPLY_INVALIDPARAM;
  405. }
  406. pData->nRequestResult = nAction;
  407. if (!m_pMainThread)
  408. {
  409. delete pData;
  410. return FZ_REPLY_NOTINITIALIZED;
  411. }
  412. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY, (LPARAM)pData);
  413. return FZ_REPLY_OK;
  414. }
  415. int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
  416. {
  417. //Check if call allowed
  418. if (!m_bInitialized)
  419. return FZ_REPLY_NOTINITIALIZED;
  420. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  421. return FZ_REPLY_NOTCONNECTED;
  422. if (IsBusy()==FZ_REPLY_BUSY)
  423. return FZ_REPLY_BUSY;
  424. if (FileName==L"")
  425. return FZ_REPLY_INVALIDPARAM;
  426. t_command command;
  427. command.id=FZ_COMMAND_CHMOD;
  428. command.param1=FileName;
  429. command.param4=nValue;
  430. command.path=path;
  431. m_pMainThread->Command(command);
  432. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  433. }
  434. void CFileZillaApi::SetDebugLevel(int nDebugLevel)
  435. {
  436. m_pMainThread->GetIntern()->SetDebugLevel(nDebugLevel);
  437. }
  438. //CAsyncRequestData derived classes
  439. CAsyncRequestData::CAsyncRequestData()
  440. {
  441. nRequestType = 0;
  442. nRequestID = 0;
  443. nRequestResult = 0;
  444. }
  445. CAsyncRequestData::~CAsyncRequestData()
  446. {
  447. }
  448. COverwriteRequestData::COverwriteRequestData()
  449. {
  450. size1 = 0;
  451. size2 = 0;
  452. nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
  453. localtime=0;
  454. remotetime.hasdate = false;
  455. pTransferFile=0;
  456. }
  457. COverwriteRequestData::~COverwriteRequestData()
  458. {
  459. delete pTransferFile;
  460. delete localtime;
  461. }
  462. CVerifyCertRequestData::CVerifyCertRequestData()
  463. {
  464. nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
  465. pCertData=0;
  466. }
  467. CVerifyCertRequestData::~CVerifyCertRequestData()
  468. {
  469. delete pCertData;
  470. }
  471. CNeedPassRequestData::CNeedPassRequestData()
  472. {
  473. nRequestType=FZ_ASYNCREQUEST_NEEDPASS;
  474. nOldOpState=0;
  475. }
  476. CNeedPassRequestData::~CNeedPassRequestData()
  477. {
  478. }
  479. #ifndef MPEXT_NO_GSS
  480. CGssNeedPassRequestData::CGssNeedPassRequestData()
  481. {
  482. nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
  483. }
  484. CGssNeedPassRequestData::~CGssNeedPassRequestData()
  485. {
  486. }
  487. CGssNeedUserRequestData::CGssNeedUserRequestData()
  488. {
  489. nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
  490. }
  491. CGssNeedUserRequestData::~CGssNeedUserRequestData()
  492. {
  493. }
  494. #endif