1
0

FileZillaApi.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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::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())
  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())
  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 /*=CServerPath()*/)
  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. m_pMainThread->Command(command);
  295. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  296. }
  297. int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
  298. {
  299. //Check if call allowed
  300. if (!m_bInitialized)
  301. return FZ_REPLY_NOTINITIALIZED;
  302. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  303. return FZ_REPLY_NOTCONNECTED;
  304. if (IsBusy()==FZ_REPLY_BUSY)
  305. return FZ_REPLY_BUSY;
  306. if (DirName==L"")
  307. return FZ_REPLY_INVALIDPARAM;
  308. CServerPath path2=path;
  309. if (path2.IsEmpty())
  310. {
  311. m_pMainThread->GetCurrentPath(path2);
  312. if (path2.IsEmpty())
  313. return FZ_REPLY_INVALIDPARAM;
  314. }
  315. t_command command;
  316. command.id=FZ_COMMAND_REMOVEDIR;
  317. command.param1=DirName;
  318. command.path=path2;
  319. m_pMainThread->Command(command);
  320. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  321. }
  322. int CFileZillaApi::MakeDir(const CServerPath &path)
  323. {
  324. //Check if call allowed
  325. if (!m_bInitialized)
  326. return FZ_REPLY_NOTINITIALIZED;
  327. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  328. return FZ_REPLY_NOTCONNECTED;
  329. if (IsBusy()==FZ_REPLY_BUSY)
  330. return FZ_REPLY_BUSY;
  331. if (path.IsEmpty() || !path.HasParent())
  332. return FZ_REPLY_INVALIDPARAM;
  333. t_command command;
  334. command.id=FZ_COMMAND_MAKEDIR;
  335. command.path=path;
  336. m_pMainThread->Command(command);
  337. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  338. }
  339. int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
  340. {
  341. //Check if call allowed
  342. if (!m_bInitialized)
  343. return FZ_REPLY_NOTINITIALIZED;
  344. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  345. return FZ_REPLY_NOTCONNECTED;
  346. if (IsBusy()==FZ_REPLY_BUSY)
  347. return FZ_REPLY_BUSY;
  348. if (oldName==L"" || newName==L"")
  349. return FZ_REPLY_INVALIDPARAM;
  350. CServerPath path2 = path;
  351. if (path2.IsEmpty())
  352. {
  353. m_pMainThread->GetCurrentPath(path2);
  354. if (path2.IsEmpty())
  355. return FZ_REPLY_INVALIDPARAM;
  356. }
  357. t_command command;
  358. command.id = FZ_COMMAND_RENAME;
  359. command.param1 = oldName;
  360. command.param2 = newName;
  361. command.path = path2;
  362. command.newPath = newPath;
  363. m_pMainThread->Command(command);
  364. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  365. }
  366. int CFileZillaApi::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  367. {
  368. if (!this || !pData)
  369. return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;
  370. if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
  371. return FZ_REPLY_CRITICALERROR;
  372. if (!m_bInitialized)
  373. {
  374. delete pData;
  375. return FZ_REPLY_NOTINITIALIZED;
  376. }
  377. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  378. {
  379. delete pData;
  380. return FZ_REPLY_NOTCONNECTED;
  381. }
  382. switch(pData->nRequestType)
  383. {
  384. case FZ_ASYNCREQUEST_OVERWRITE:
  385. break;
  386. case FZ_ASYNCREQUEST_VERIFYCERT:
  387. if (!((CVerifyCertRequestData *)pData)->pCertData)
  388. {
  389. delete pData;
  390. return FZ_REPLY_INVALIDPARAM;
  391. }
  392. break;
  393. case FZ_ASYNCREQUEST_NEEDPASS:
  394. break;
  395. #ifndef MPEXT_NO_GSS
  396. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  397. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  398. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  399. break;
  400. #endif
  401. default:
  402. delete pData;
  403. return FZ_REPLY_INVALIDPARAM;
  404. }
  405. pData->nRequestResult = nAction;
  406. if (!m_pMainThread)
  407. {
  408. delete pData;
  409. return FZ_REPLY_NOTINITIALIZED;
  410. }
  411. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY, (LPARAM)pData);
  412. return FZ_REPLY_OK;
  413. }
  414. int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
  415. {
  416. //Check if call allowed
  417. if (!m_bInitialized)
  418. return FZ_REPLY_NOTINITIALIZED;
  419. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  420. return FZ_REPLY_NOTCONNECTED;
  421. if (IsBusy()==FZ_REPLY_BUSY)
  422. return FZ_REPLY_BUSY;
  423. if (FileName==L"")
  424. return FZ_REPLY_INVALIDPARAM;
  425. t_command command;
  426. command.id=FZ_COMMAND_CHMOD;
  427. command.param1=FileName;
  428. command.param4=nValue;
  429. command.path=path;
  430. m_pMainThread->Command(command);
  431. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  432. }
  433. void CFileZillaApi::SetDebugLevel(int nDebugLevel)
  434. {
  435. m_pMainThread->GetIntern()->SetDebugLevel(nDebugLevel);
  436. }
  437. //CAsyncRequestData derived classes
  438. CAsyncRequestData::CAsyncRequestData()
  439. {
  440. nRequestType = 0;
  441. nRequestID = 0;
  442. nRequestResult = 0;
  443. }
  444. CAsyncRequestData::~CAsyncRequestData()
  445. {
  446. }
  447. COverwriteRequestData::COverwriteRequestData()
  448. {
  449. size1 = 0;
  450. size2 = 0;
  451. nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
  452. localtime=0;
  453. remotetime.hasdate = false;
  454. pTransferFile=0;
  455. }
  456. COverwriteRequestData::~COverwriteRequestData()
  457. {
  458. delete pTransferFile;
  459. delete localtime;
  460. }
  461. CVerifyCertRequestData::CVerifyCertRequestData()
  462. {
  463. nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
  464. pCertData=0;
  465. }
  466. CVerifyCertRequestData::~CVerifyCertRequestData()
  467. {
  468. delete pCertData;
  469. }
  470. CNeedPassRequestData::CNeedPassRequestData()
  471. {
  472. nRequestType=FZ_ASYNCREQUEST_NEEDPASS;
  473. nOldOpState=0;
  474. }
  475. CNeedPassRequestData::~CNeedPassRequestData()
  476. {
  477. }
  478. #ifndef MPEXT_NO_GSS
  479. CGssNeedPassRequestData::CGssNeedPassRequestData()
  480. {
  481. nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
  482. }
  483. CGssNeedPassRequestData::~CGssNeedPassRequestData()
  484. {
  485. }
  486. CGssNeedUserRequestData::CGssNeedUserRequestData()
  487. {
  488. nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
  489. }
  490. CGssNeedUserRequestData::~CGssNeedUserRequestData()
  491. {
  492. }
  493. #endif