WebDAVFileSystem.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <io.h>
  5. #include <fcntl.h>
  6. #include <wincrypt.h>
  7. #define NE_LFS
  8. #define WINSCP
  9. #include <ne_basic.h>
  10. #include <ne_auth.h>
  11. #include <ne_props.h>
  12. #include <ne_uri.h>
  13. #include <ne_session.h>
  14. #include <ne_request.h>
  15. #include <ne_xml.h>
  16. #include <ne_redirect.h>
  17. #include <ne_xmlreq.h>
  18. #include <ne_locks.h>
  19. #include <expat.h>
  20. #include "WebDAVFileSystem.h"
  21. #include "Interface.h"
  22. #include "Common.h"
  23. #include "Exceptions.h"
  24. #include "Terminal.h"
  25. #include "TextsCore.h"
  26. #include "SecureShell.h"
  27. #include "HelpCore.h"
  28. #include "CoreMain.h"
  29. #include "Security.h"
  30. #include <StrUtils.hpp>
  31. #include <NeonIntf.h>
  32. //---------------------------------------------------------------------------
  33. #pragma package(smart_init)
  34. //---------------------------------------------------------------------------
  35. #define FILE_OPERATION_LOOP_TERMINAL FTerminal
  36. //---------------------------------------------------------------------------
  37. #define SESSION_FS_KEY "filesystem"
  38. static const UnicodeString CONST_WEBDAV_PROTOCOL_BASE_NAME = L"WebDAV";
  39. static const int HttpUnauthorized = 401;
  40. //---------------------------------------------------------------------------
  41. #define DAV_PROP_NAMESPACE "DAV:"
  42. #define MODDAV_PROP_NAMESPACE "http://apache.org/dav/props/"
  43. #define PROP_CONTENT_LENGTH "getcontentlength"
  44. #define PROP_LAST_MODIFIED "getlastmodified"
  45. #define PROP_RESOURCE_TYPE "resourcetype"
  46. #define PROP_HIDDEN "ishidden"
  47. #define PROP_QUOTA_AVAILABLE "quota-available-bytes"
  48. #define PROP_QUOTA_USED "quota-used-bytes"
  49. #define PROP_EXECUTABLE "executable"
  50. #define PROP_OWNER "owner"
  51. #define PROP_DISPLAY_NAME "displayname"
  52. //------------------------------------------------------------------------------
  53. //---------------------------------------------------------------------------
  54. // ne_path_escape returns 7-bit string, so it does not really matter if we use
  55. // AnsiString or UTF8String here, though UTF8String might be more safe
  56. static AnsiString PathEscape(const char * Path)
  57. {
  58. char * EscapedPath = ne_path_escape(Path);
  59. AnsiString Result = EscapedPath;
  60. ne_free(EscapedPath);
  61. return Result;
  62. }
  63. //---------------------------------------------------------------------------
  64. static UTF8String PathUnescape(const char * Path)
  65. {
  66. char * UnescapedPath = ne_path_unescape(Path);
  67. UTF8String Result = UnescapedPath;
  68. ne_free(UnescapedPath);
  69. return Result;
  70. }
  71. //---------------------------------------------------------------------------
  72. #define AbsolutePathToNeon(P) PathEscape(StrToNeon(P)).c_str()
  73. #define PathToNeonStatic(THIS, P) AbsolutePathToNeon((THIS)->AbsolutePath(P, false))
  74. #define PathToNeon(P) PathToNeonStatic(this, P)
  75. //---------------------------------------------------------------------------
  76. //---------------------------------------------------------------------------
  77. static bool NeonInitialized = false;
  78. static bool NeonSspiInitialized = false;
  79. //---------------------------------------------------------------------------
  80. void __fastcall NeonInitialize()
  81. {
  82. // Even if this fails, we do not want to interrupt WinSCP starting for that.
  83. // Anyway, it can hardly fail.
  84. // Though it fails on Wine on Debian VM, because of ne_sspi_init():
  85. // sspi: QuerySecurityPackageInfo [failed] [80090305].
  86. // sspi: Unable to get negotiate maximum packet size
  87. int NeonResult = ne_sock_init();
  88. if (NeonResult == 0)
  89. {
  90. NeonInitialized = true;
  91. NeonSspiInitialized = true;
  92. }
  93. else if (NeonResult == -2)
  94. {
  95. NeonInitialized = true;
  96. NeonSspiInitialized = false;
  97. }
  98. else
  99. {
  100. NeonInitialized = false;
  101. NeonSspiInitialized = false;
  102. }
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall NeonFinalize()
  106. {
  107. if (NeonInitialized)
  108. {
  109. ne_sock_exit();
  110. NeonInitialized = false;
  111. }
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall RequireNeon(TTerminal * Terminal)
  115. {
  116. if (!NeonInitialized)
  117. {
  118. throw Exception(LoadStr(NEON_INIT_FAILED2));
  119. }
  120. if (!NeonSspiInitialized)
  121. {
  122. Terminal->LogEvent(L"Warning: SSPI initialization failed.");
  123. }
  124. }
  125. //---------------------------------------------------------------------------
  126. UnicodeString __fastcall NeonVersion()
  127. {
  128. UnicodeString Str = StrFromNeon(ne_version_string());
  129. CutToChar(Str, L' ', true); // "neon"
  130. UnicodeString Result = CutToChar(Str, L':', true);
  131. return Result;
  132. }
  133. //---------------------------------------------------------------------------
  134. UnicodeString __fastcall ExpatVersion()
  135. {
  136. return FORMAT(L"%d.%d.%d", (XML_MAJOR_VERSION, XML_MINOR_VERSION, XML_MICRO_VERSION));
  137. }
  138. //---------------------------------------------------------------------------
  139. //---------------------------------------------------------------------------
  140. TWebDAVFileSystem::TWebDAVFileSystem(TTerminal * ATerminal) :
  141. TCustomFileSystem(ATerminal),
  142. FActive(false),
  143. FHasTrailingSlash(false),
  144. FNeonSession(NULL),
  145. FNeonLockStore(NULL),
  146. FNeonLockStoreSection(new TCriticalSection()),
  147. FUploading(false),
  148. FDownloading(false),
  149. FInitialHandshake(false),
  150. FIgnoreAuthenticationFailure(iafNo)
  151. {
  152. FFileSystemInfo.ProtocolBaseName = CONST_WEBDAV_PROTOCOL_BASE_NAME;
  153. FFileSystemInfo.ProtocolName = FFileSystemInfo.ProtocolBaseName;
  154. }
  155. //---------------------------------------------------------------------------
  156. __fastcall TWebDAVFileSystem::~TWebDAVFileSystem()
  157. {
  158. UnregisterFromNeonDebug(FTerminal);
  159. {
  160. TGuard Guard(FNeonLockStoreSection);
  161. if (FNeonLockStore != NULL)
  162. {
  163. ne_lockstore_destroy(FNeonLockStore);
  164. FNeonLockStore = NULL;
  165. }
  166. }
  167. delete FNeonLockStoreSection;
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TWebDAVFileSystem::Open()
  171. {
  172. RequireNeon(FTerminal);
  173. RegisterForNeonDebug(FTerminal);
  174. FCurrentDirectory = L"";
  175. FHasTrailingSlash = true;
  176. FStoredPasswordTried = false;
  177. FTlsVersionStr = L"";
  178. FCapabilities = 0;
  179. TSessionData * Data = FTerminal->SessionData;
  180. FSessionInfo.LoginTime = Now();
  181. FSessionInfo.CertificateVerifiedManually = false;
  182. UnicodeString HostName = Data->HostNameExpanded;
  183. size_t Port = Data->PortNumber;
  184. UnicodeString ProtocolName = (FTerminal->SessionData->Ftps == ftpsNone) ? HttpProtocol : HttpsProtocol;
  185. UnicodeString Path = Data->RemoteDirectory;
  186. // PathToNeon is not used as we cannot call AbsolutePath here
  187. UnicodeString EscapedPath = StrFromNeon(PathEscape(StrToNeon(Path)).c_str());
  188. UnicodeString Url = FORMAT(L"%s://%s:%d%s", (ProtocolName, HostName, Port, EscapedPath));
  189. FTerminal->Information(LoadStr(STATUS_CONNECT), true);
  190. FActive = false;
  191. try
  192. {
  193. OpenUrl(Url);
  194. }
  195. catch (Exception & E)
  196. {
  197. CloseNeonSession();
  198. FTerminal->Closed();
  199. FTerminal->FatalError(&E, LoadStr(CONNECTION_FAILED));
  200. }
  201. FActive = true;
  202. }
  203. //---------------------------------------------------------------------------
  204. UnicodeString __fastcall TWebDAVFileSystem::ParsePathFromUrl(const UnicodeString & Url)
  205. {
  206. UnicodeString Result;
  207. ne_uri ParsedUri;
  208. if (ne_uri_parse(StrToNeon(Url), &ParsedUri) == 0)
  209. {
  210. Result = StrFromNeon(PathUnescape(ParsedUri.path));
  211. ne_uri_free(&ParsedUri);
  212. }
  213. return Result;
  214. }
  215. //---------------------------------------------------------------------------
  216. void TWebDAVFileSystem::OpenUrl(const UnicodeString & Url)
  217. {
  218. UnicodeString CorrectedUrl;
  219. NeonClientOpenSessionInternal(CorrectedUrl, Url);
  220. if (CorrectedUrl.IsEmpty())
  221. {
  222. CorrectedUrl = Url;
  223. }
  224. UnicodeString ParsedPath = ParsePathFromUrl(CorrectedUrl);
  225. if (!ParsedPath.IsEmpty())
  226. {
  227. // this is most likely pointless as it get overwritten by
  228. // call to ChangeDirectory() from TTerminal::DoStartup
  229. FCurrentDirectory = ParsedPath;
  230. }
  231. }
  232. //---------------------------------------------------------------------------
  233. void TWebDAVFileSystem::NeonClientOpenSessionInternal(UnicodeString & CorrectedUrl, UnicodeString Url)
  234. {
  235. std::unique_ptr<TStringList> AttemptedUrls(CreateSortedStringList());
  236. AttemptedUrls->Add(Url);
  237. while (true)
  238. {
  239. CorrectedUrl = L"";
  240. NeonOpen(CorrectedUrl, Url);
  241. // No error and no corrected URL? We're done here.
  242. if (CorrectedUrl.IsEmpty())
  243. {
  244. break;
  245. }
  246. CloseNeonSession();
  247. CheckRedirectLoop(CorrectedUrl, AttemptedUrls.get());
  248. // Our caller will want to know what our final corrected URL was.
  249. Url = CorrectedUrl;
  250. }
  251. CorrectedUrl = Url;
  252. }
  253. //---------------------------------------------------------------------------
  254. void __fastcall TWebDAVFileSystem::SetSessionTls(ne_session_s * Session, bool Aux)
  255. {
  256. SetNeonTlsInit(Session, InitSslSession);
  257. // When the CA certificate or server certificate has
  258. // verification problems, neon will call our verify function before
  259. // outright rejection of the connection.
  260. ne_ssl_verify_fn Callback = Aux ? NeonServerSSLCallbackAux : NeonServerSSLCallbackMain;
  261. ne_ssl_set_verify(Session, Callback, this);
  262. ne_ssl_trust_default_ca(Session);
  263. }
  264. //---------------------------------------------------------------------------
  265. void __fastcall TWebDAVFileSystem::InitSession(ne_session_s * Session)
  266. {
  267. TSessionData * Data = FTerminal->SessionData;
  268. InitNeonSession(
  269. Session, Data->ProxyMethod, Data->ProxyHost, Data->ProxyPort,
  270. Data->ProxyUsername, Data->ProxyPassword, FTerminal);
  271. ne_set_read_timeout(Session, Data->Timeout);
  272. ne_set_connect_timeout(Session, Data->Timeout);
  273. ne_set_session_private(Session, SESSION_FS_KEY, this);
  274. }
  275. //---------------------------------------------------------------------------
  276. void TWebDAVFileSystem::NeonOpen(UnicodeString & CorrectedUrl, const UnicodeString & Url)
  277. {
  278. ne_uri uri;
  279. NeonParseUrl(Url, uri);
  280. FHostName = StrFromNeon(uri.host);
  281. FPortNumber = uri.port;
  282. FSessionInfo.CSCipher = UnicodeString();
  283. FSessionInfo.SCCipher = UnicodeString();
  284. bool Ssl = IsTlsUri(uri);
  285. FSessionInfo.SecurityProtocolName = Ssl ? LoadStr(FTPS_IMPLICIT) : UnicodeString();
  286. if (Ssl != (FTerminal->SessionData->Ftps != ftpsNone))
  287. {
  288. FTerminal->LogEvent(FORMAT(L"Warning: %s", (LoadStr(UNENCRYPTED_REDIRECT))));
  289. }
  290. DebugAssert(FNeonSession == NULL);
  291. FNeonSession = CreateNeonSession(uri);
  292. InitSession(FNeonSession);
  293. UTF8String Path = uri.path;
  294. ne_uri_free(&uri);
  295. ne_set_aux_request_init(FNeonSession, NeonAuxRequestInit, this);
  296. UpdateNeonDebugMask();
  297. NeonAddAuthentiation(Ssl);
  298. if (Ssl)
  299. {
  300. SetSessionTls(FNeonSession, false);
  301. ne_ssl_provide_clicert(FNeonSession, NeonProvideClientCert, this);
  302. }
  303. ne_set_notifier(FNeonSession, NeonNotifier, this);
  304. ne_hook_create_request(FNeonSession, NeonCreateRequest, this);
  305. ne_hook_pre_send(FNeonSession, NeonPreSend, this);
  306. ne_hook_post_send(FNeonSession, NeonPostSend, this);
  307. ne_hook_post_headers(FNeonSession, NeonPostHeaders, this);
  308. TAutoFlag Flag(FInitialHandshake);
  309. ExchangeCapabilities(Path.c_str(), CorrectedUrl);
  310. }
  311. //---------------------------------------------------------------------------
  312. void TWebDAVFileSystem::NeonAuxRequestInit(ne_session * Session, ne_request * /*Request*/, void * UserData)
  313. {
  314. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  315. FileSystem->InitSession(Session);
  316. ne_uri uri = {0};
  317. ne_fill_server_uri(Session, &uri);
  318. bool Tls = IsTlsUri(uri);
  319. ne_uri_free(&uri);
  320. if (Tls)
  321. {
  322. FileSystem->SetSessionTls(Session, true);
  323. }
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TWebDAVFileSystem::NeonAddAuthentiation(bool UseNegotiate)
  327. {
  328. unsigned int NeonAuthTypes = NE_AUTH_BASIC | NE_AUTH_DIGEST | NE_AUTH_PASSPORT;
  329. if (UseNegotiate)
  330. {
  331. NeonAuthTypes |= NE_AUTH_NEGOTIATE;
  332. }
  333. ne_add_server_auth(FNeonSession, NeonAuthTypes, NeonRequestAuth, this);
  334. }
  335. //---------------------------------------------------------------------------
  336. UnicodeString __fastcall TWebDAVFileSystem::GetRedirectUrl()
  337. {
  338. UnicodeString Result = GetNeonRedirectUrl(FNeonSession);
  339. FTerminal->LogEvent(FORMAT(L"Redirected to \"%s\".", (Result)));
  340. return Result;
  341. }
  342. //---------------------------------------------------------------------------
  343. void TWebDAVFileSystem::ExchangeCapabilities(const char * Path, UnicodeString & CorrectedUrl)
  344. {
  345. ClearNeonError();
  346. int NeonStatus;
  347. FAuthenticationRetry = false;
  348. do
  349. {
  350. NeonStatus = ne_options2(FNeonSession, Path, &FCapabilities);
  351. }
  352. while ((NeonStatus == NE_AUTH) && FAuthenticationRetry);
  353. if (NeonStatus == NE_REDIRECT)
  354. {
  355. CorrectedUrl = GetRedirectUrl();
  356. }
  357. else if (NeonStatus == NE_OK)
  358. {
  359. if (FCapabilities > 0)
  360. {
  361. UnicodeString Str;
  362. unsigned int Capability = 0x01;
  363. unsigned int Capabilities = FCapabilities;
  364. while (Capabilities > 0)
  365. {
  366. if (FLAGSET(Capabilities, Capability))
  367. {
  368. AddToList(Str, StrFromNeon(ne_capability_name(Capability)), L", ");
  369. Capabilities -= Capability;
  370. }
  371. Capability <<= 1;
  372. }
  373. FTerminal->LogEvent(FORMAT(L"Server capabilities: %s", (Str)));
  374. FFileSystemInfo.AdditionalInfo +=
  375. LoadStr(WEBDAV_EXTENSION_INFO) + sLineBreak +
  376. L" " + Str + sLineBreak;
  377. }
  378. }
  379. else
  380. {
  381. CheckStatus(NeonStatus);
  382. }
  383. FTerminal->SaveCapabilities(FFileSystemInfo);
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TWebDAVFileSystem::CloseNeonSession()
  387. {
  388. if (FNeonSession != NULL)
  389. {
  390. DestroyNeonSession(FNeonSession);
  391. FNeonSession = NULL;
  392. }
  393. }
  394. //---------------------------------------------------------------------------
  395. void __fastcall TWebDAVFileSystem::Close()
  396. {
  397. DebugAssert(FActive);
  398. CloseNeonSession();
  399. FTerminal->Closed();
  400. FActive = false;
  401. UnregisterFromNeonDebug(FTerminal);
  402. }
  403. //---------------------------------------------------------------------------
  404. bool __fastcall TWebDAVFileSystem::GetActive()
  405. {
  406. return FActive;
  407. }
  408. //---------------------------------------------------------------------------
  409. void __fastcall TWebDAVFileSystem::CollectUsage()
  410. {
  411. if (!FTlsVersionStr.IsEmpty())
  412. {
  413. FTerminal->CollectTlsUsage(FTlsVersionStr);
  414. }
  415. if (!FTerminal->SessionData->TlsCertificateFile.IsEmpty())
  416. {
  417. Configuration->Usage->Inc(L"OpenedSessionsWebDAVSCertificate");
  418. }
  419. // The Authorization header for passport method is included only in the first request,
  420. // so we have to use FLastAuthorizationProtocol
  421. if (SameText(FLastAuthorizationProtocol, L"Passport1.4"))
  422. {
  423. Configuration->Usage->Inc(L"OpenedSessionsWebDAVSPassport");
  424. }
  425. UnicodeString RemoteSystem = FFileSystemInfo.RemoteSystem;
  426. if (ContainsText(RemoteSystem, L"Microsoft-IIS"))
  427. {
  428. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsWebDAVIIS");
  429. }
  430. else if (ContainsText(RemoteSystem, L"IT Hit WebDAV Server"))
  431. {
  432. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsWebDAVITHit");
  433. }
  434. // e.g. brickftp.com
  435. else if (ContainsText(RemoteSystem, L"nginx"))
  436. {
  437. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsWebDAVNginx");
  438. }
  439. else
  440. {
  441. // We also know OpenDrive, Yandex, iFiles (iOS), Swapper (iOS), SafeSync
  442. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsWebDAVOther");
  443. }
  444. }
  445. //---------------------------------------------------------------------------
  446. const TSessionInfo & __fastcall TWebDAVFileSystem::GetSessionInfo()
  447. {
  448. return FSessionInfo;
  449. }
  450. //---------------------------------------------------------------------------
  451. const TFileSystemInfo & __fastcall TWebDAVFileSystem::GetFileSystemInfo(bool /*Retrieve*/)
  452. {
  453. return FFileSystemInfo;
  454. }
  455. //---------------------------------------------------------------------------
  456. bool __fastcall TWebDAVFileSystem::TemporaryTransferFile(const UnicodeString & /*FileName*/)
  457. {
  458. return false;
  459. }
  460. //---------------------------------------------------------------------------
  461. bool __fastcall TWebDAVFileSystem::GetStoredCredentialsTried()
  462. {
  463. return FStoredPasswordTried;
  464. }
  465. //---------------------------------------------------------------------------
  466. UnicodeString __fastcall TWebDAVFileSystem::GetUserName()
  467. {
  468. return FUserName;
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall TWebDAVFileSystem::Idle()
  472. {
  473. // noop
  474. }
  475. //---------------------------------------------------------------------------
  476. UnicodeString __fastcall TWebDAVFileSystem::AbsolutePath(const UnicodeString Path, bool /*Local*/)
  477. {
  478. bool AddTrailingBackslash;
  479. if (Path == L"/")
  480. {
  481. // does not really matter as path "/" is still "/" when absolute,
  482. // no slash needed
  483. AddTrailingBackslash = FHasTrailingSlash;
  484. }
  485. else
  486. {
  487. AddTrailingBackslash = (Path[Path.Length()] == L'/');
  488. }
  489. UnicodeString Result = ::AbsolutePath(GetCurrentDirectory(), Path);
  490. // We must preserve trailing slash, because particularly for mod_dav,
  491. // it really matters if the slash in there or not
  492. if (AddTrailingBackslash)
  493. {
  494. Result = UnixIncludeTrailingBackslash(Result);
  495. }
  496. return Result;
  497. }
  498. //---------------------------------------------------------------------------
  499. bool __fastcall TWebDAVFileSystem::IsCapable(int Capability) const
  500. {
  501. DebugAssert(FTerminal);
  502. switch (Capability)
  503. {
  504. case fcRename:
  505. case fcRemoteMove:
  506. case fcMoveToQueue:
  507. case fcPreservingTimestampUpload:
  508. case fcCheckingSpaceAvailable:
  509. // Only to make double-click on file edit/open the file,
  510. // instead of trying to open it as directory
  511. case fcResolveSymlink:
  512. case fcSkipTransfer:
  513. case fcParallelTransfers:
  514. case fcRemoteCopy:
  515. return true;
  516. case fcUserGroupListing:
  517. case fcModeChanging:
  518. case fcModeChangingUpload:
  519. case fcGroupChanging:
  520. case fcOwnerChanging:
  521. case fcAnyCommand:
  522. case fcShellAnyCommand:
  523. case fcHardLink:
  524. case fcSymbolicLink:
  525. case fcTextMode:
  526. case fcNativeTextMode:
  527. case fcNewerOnlyUpload:
  528. case fcTimestampChanging:
  529. case fcLoadingAdditionalProperties:
  530. case fcIgnorePermErrors:
  531. case fcCalculatingChecksum:
  532. case fcSecondaryShell:
  533. case fcGroupOwnerChangingByID:
  534. case fcRemoveCtrlZUpload:
  535. case fcRemoveBOMUpload:
  536. case fcPreservingTimestampDirs:
  537. case fcResumeSupport:
  538. case fcChangePassword:
  539. case fcTransferOut:
  540. case fcTransferIn:
  541. return false;
  542. case fcLocking:
  543. return FLAGSET(FCapabilities, NE_CAP_DAV_CLASS2);
  544. default:
  545. DebugFail();
  546. return false;
  547. }
  548. }
  549. //---------------------------------------------------------------------------
  550. UnicodeString __fastcall TWebDAVFileSystem::GetCurrentDirectory()
  551. {
  552. return FCurrentDirectory;
  553. }
  554. //---------------------------------------------------------------------------
  555. void __fastcall TWebDAVFileSystem::DoStartup()
  556. {
  557. FTerminal->SetExceptionOnFail(true);
  558. // retrieve initialize working directory to save it as home directory
  559. ReadCurrentDirectory();
  560. FTerminal->SetExceptionOnFail(false);
  561. }
  562. //---------------------------------------------------------------------------
  563. void __fastcall TWebDAVFileSystem::ClearNeonError()
  564. {
  565. FCancelled = false;
  566. FSkipped = false;
  567. FAuthenticationRequested = false;
  568. ne_set_error(FNeonSession, "");
  569. }
  570. //---------------------------------------------------------------------------
  571. UnicodeString __fastcall TWebDAVFileSystem::GetNeonError()
  572. {
  573. return ::GetNeonError(FNeonSession);
  574. }
  575. //---------------------------------------------------------------------------
  576. void __fastcall TWebDAVFileSystem::CheckStatus(int NeonStatus)
  577. {
  578. if ((NeonStatus == NE_ERROR) && (FCancelled || FSkipped))
  579. {
  580. if (FCancelled)
  581. {
  582. FCancelled = false;
  583. FSkipped = false; // just in case
  584. Abort();
  585. }
  586. else
  587. {
  588. DebugAssert(FSkipped);
  589. FSkipped = false;
  590. throw ESkipFile();
  591. }
  592. }
  593. else
  594. {
  595. CheckNeonStatus(FNeonSession, NeonStatus, FHostName);
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. void __fastcall TWebDAVFileSystem::LookupUsersGroups()
  600. {
  601. DebugFail();
  602. }
  603. //---------------------------------------------------------------------------
  604. void __fastcall TWebDAVFileSystem::ReadCurrentDirectory()
  605. {
  606. if (FCachedDirectoryChange.IsEmpty())
  607. {
  608. FCurrentDirectory = FCurrentDirectory.IsEmpty() ? UnicodeString(L"/") : FCurrentDirectory;
  609. }
  610. else
  611. {
  612. FCurrentDirectory = FCachedDirectoryChange;
  613. FCachedDirectoryChange = L"";
  614. }
  615. }
  616. //---------------------------------------------------------------------------
  617. void __fastcall TWebDAVFileSystem::HomeDirectory()
  618. {
  619. ChangeDirectory(L"/");
  620. }
  621. //---------------------------------------------------------------------------
  622. UnicodeString __fastcall TWebDAVFileSystem::DirectoryPath(UnicodeString Path)
  623. {
  624. if (FHasTrailingSlash)
  625. {
  626. Path = ::UnixIncludeTrailingBackslash(Path);
  627. }
  628. return Path;
  629. }
  630. //---------------------------------------------------------------------------
  631. UnicodeString __fastcall TWebDAVFileSystem::FilePath(const TRemoteFile * File)
  632. {
  633. UnicodeString Result = File->FullFileName;
  634. if (File->IsDirectory)
  635. {
  636. Result = DirectoryPath(Result);
  637. }
  638. return Result;
  639. }
  640. //---------------------------------------------------------------------------
  641. void __fastcall TWebDAVFileSystem::TryOpenDirectory(UnicodeString Directory)
  642. {
  643. Directory = DirectoryPath(Directory);
  644. FTerminal->LogEvent(FORMAT(L"Trying to open directory \"%s\".", (Directory)));
  645. TRemoteFile * File;
  646. ReadFile(Directory, File);
  647. delete File;
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TWebDAVFileSystem::AnnounceFileListOperation()
  651. {
  652. // noop
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TWebDAVFileSystem::ChangeDirectory(const UnicodeString ADirectory)
  656. {
  657. UnicodeString Path = AbsolutePath(ADirectory, false);
  658. // to verify existence of directory try to open it
  659. TryOpenDirectory(Path);
  660. // if open dir did not fail, directory exists -> success.
  661. FCachedDirectoryChange = Path;
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TWebDAVFileSystem::CachedChangeDirectory(const UnicodeString Directory)
  665. {
  666. FCachedDirectoryChange = UnixExcludeTrailingBackslash(Directory);
  667. }
  668. //---------------------------------------------------------------------------
  669. struct TReadFileData
  670. {
  671. TWebDAVFileSystem * FileSystem;
  672. TRemoteFile * File;
  673. TRemoteFileList * FileList;
  674. };
  675. //---------------------------------------------------------------------------
  676. int __fastcall TWebDAVFileSystem::ReadDirectoryInternal(
  677. const UnicodeString & Path, TRemoteFileList * FileList)
  678. {
  679. TReadFileData Data;
  680. Data.FileSystem = this;
  681. Data.File = NULL;
  682. Data.FileList = FileList;
  683. ClearNeonError();
  684. ne_propfind_handler * PropFindHandler = ne_propfind_create(FNeonSession, PathToNeon(Path), NE_DEPTH_ONE);
  685. void * DiscoveryContext = ne_lock_register_discovery(PropFindHandler);
  686. int Result;
  687. try
  688. {
  689. Result = ne_propfind_allprop(PropFindHandler, NeonPropsResult, &Data);
  690. }
  691. __finally
  692. {
  693. ne_lock_discovery_free(DiscoveryContext);
  694. ne_propfind_destroy(PropFindHandler);
  695. }
  696. return Result;
  697. }
  698. //---------------------------------------------------------------------------
  699. bool __fastcall TWebDAVFileSystem::IsValidRedirect(int NeonStatus, UnicodeString & Path)
  700. {
  701. bool Result = (NeonStatus == NE_REDIRECT);
  702. if (Result)
  703. {
  704. // What PathToNeon does
  705. UnicodeString OriginalPath = AbsolutePath(Path, false);
  706. // Handle one-step redirect
  707. // (for more steps we would have to implement loop detection).
  708. // This is mainly to handle "folder" => "folder/" redirects of Apache/mod_dav.
  709. UnicodeString RedirectUrl = GetRedirectUrl();
  710. // We should test if the redirect is not for another server,
  711. // though not sure how to do this reliably (domain aliases, IP vs. domain, etc.)
  712. UnicodeString RedirectPath = ParsePathFromUrl(RedirectUrl);
  713. Result =
  714. !RedirectPath.IsEmpty() &&
  715. (RedirectPath != OriginalPath);
  716. if (Result)
  717. {
  718. Path = RedirectPath;
  719. }
  720. }
  721. return Result;
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TWebDAVFileSystem::ReadDirectory(TRemoteFileList * FileList)
  725. {
  726. UnicodeString Path = DirectoryPath(FileList->Directory);
  727. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  728. int NeonStatus = ReadDirectoryInternal(Path, FileList);
  729. if (IsValidRedirect(NeonStatus, Path))
  730. {
  731. NeonStatus = ReadDirectoryInternal(Path, FileList);
  732. }
  733. CheckStatus(NeonStatus);
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TWebDAVFileSystem::ReadSymlink(TRemoteFile * /*SymlinkFile*/,
  737. TRemoteFile *& /*File*/)
  738. {
  739. // we never set SymLink flag, so we should never get here
  740. DebugFail();
  741. }
  742. //---------------------------------------------------------------------------
  743. void __fastcall TWebDAVFileSystem::ReadFile(const UnicodeString FileName,
  744. TRemoteFile *& File)
  745. {
  746. CustomReadFile(FileName, File, NULL);
  747. }
  748. //---------------------------------------------------------------------------
  749. void TWebDAVFileSystem::NeonPropsResult(
  750. void * UserData, const ne_uri * Uri, const ne_prop_result_set * Results)
  751. {
  752. UnicodeString Path = StrFromNeon(PathUnescape(Uri->path).c_str());
  753. TReadFileData & Data = *static_cast<TReadFileData *>(UserData);
  754. if (Data.FileList != NULL)
  755. {
  756. UnicodeString FileListPath = Data.FileSystem->AbsolutePath(Data.FileList->Directory, false);
  757. if (UnixSamePath(Path, FileListPath))
  758. {
  759. Path = UnixIncludeTrailingBackslash(UnixIncludeTrailingBackslash(Path) + PARENTDIRECTORY);
  760. }
  761. std::unique_ptr<TRemoteFile> File(new TRemoteFile(NULL));
  762. File->Terminal = Data.FileSystem->FTerminal;
  763. Data.FileSystem->ParsePropResultSet(File.get(), Path, Results);
  764. Data.FileList->AddFile(File.release());
  765. }
  766. else
  767. {
  768. Data.FileSystem->ParsePropResultSet(Data.File, Path, Results);
  769. }
  770. }
  771. //---------------------------------------------------------------------------
  772. const char * __fastcall TWebDAVFileSystem::GetProp(
  773. const ne_prop_result_set * Results, const char * Name, const char * NameSpace)
  774. {
  775. ne_propname Prop;
  776. Prop.nspace = (NameSpace == NULL) ? DAV_PROP_NAMESPACE : NameSpace;
  777. Prop.name = Name;
  778. return ne_propset_value(Results, &Prop);
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TWebDAVFileSystem::ParsePropResultSet(TRemoteFile * File,
  782. const UnicodeString & Path, const ne_prop_result_set * Results)
  783. {
  784. File->FullFileName = UnixExcludeTrailingBackslash(Path);
  785. // Some servers do not use DAV:collection tag, but indicate the folder by trailing slash only.
  786. // It seems that all servers actually use the trailing slash, including IIS, mod_Dav, IT Hit, OpenDrive, etc.
  787. bool Collection = (File->FullFileName != Path);
  788. File->FileName = UnixExtractFileName(File->FullFileName);
  789. const char * ContentLength = GetProp(Results, PROP_CONTENT_LENGTH);
  790. // some servers, for example iFiles, do not provide "getcontentlength" for folders
  791. if (ContentLength != NULL)
  792. {
  793. File->Size = StrToInt64Def(ContentLength, 0);
  794. }
  795. const char * LastModified = GetProp(Results, PROP_LAST_MODIFIED);
  796. // We've seen a server (t=24891) that does not set "getlastmodified" for the "this" folder entry.
  797. if (LastModified != NULL)
  798. {
  799. char WeekDay[4] = { L'\0' };
  800. int Year = 0;
  801. char MonthStr[4] = { L'\0' };
  802. int Day = 0;
  803. int Hour = 0;
  804. int Min = 0;
  805. int Sec = 0;
  806. #define RFC1123_FORMAT "%3s, %02d %3s %4d %02d:%02d:%02d GMT"
  807. int Filled =
  808. sscanf(LastModified, RFC1123_FORMAT, WeekDay, &Day, MonthStr, &Year, &Hour, &Min, &Sec);
  809. // we need at least a complete date
  810. if (Filled >= 4)
  811. {
  812. int Month = ParseShortEngMonthName(MonthStr);
  813. if (Month >= 1)
  814. {
  815. TDateTime Modification =
  816. EncodeDateVerbose((unsigned short)Year, (unsigned short)Month, (unsigned short)Day) +
  817. EncodeTimeVerbose((unsigned short)Hour, (unsigned short)Min, (unsigned short)Sec, 0);
  818. File->Modification = ConvertTimestampFromUTC(Modification);
  819. File->ModificationFmt = mfFull;
  820. }
  821. }
  822. }
  823. // optimization
  824. if (!Collection)
  825. {
  826. // This is possibly redundant code as all servers we know (see a comment above)
  827. // indicate the folder by trailing slash too
  828. const char * ResourceType = GetProp(Results, PROP_RESOURCE_TYPE);
  829. if (ResourceType != NULL)
  830. {
  831. // property has XML value
  832. UnicodeString AResourceType = ResourceType;
  833. // this is very poor parsing
  834. if (ContainsText(ResourceType, L"<DAV:collection"))
  835. {
  836. Collection = true;
  837. }
  838. }
  839. }
  840. File->Type = Collection ? FILETYPE_DIRECTORY : FILETYPE_DEFAULT;
  841. // this is MS extension (draft-hopmann-collection-props-00)
  842. const char * IsHidden = GetProp(Results, PROP_HIDDEN);
  843. if (IsHidden != NULL)
  844. {
  845. File->IsHidden = (StrToIntDef(IsHidden, 0) != 0);
  846. }
  847. const char * Owner = GetProp(Results, PROP_OWNER);
  848. if (Owner != NULL)
  849. {
  850. File->Owner.Name = Owner;
  851. }
  852. const char * DisplayName = GetProp(Results, PROP_DISPLAY_NAME);
  853. if (DisplayName != NULL)
  854. {
  855. File->DisplayName = StrFromNeon(DisplayName);
  856. }
  857. const UnicodeString RightsDelimiter(L", ");
  858. UnicodeString HumanRights;
  859. // Proprietary property of mod_dav
  860. // http://www.webdav.org/mod_dav/#imp
  861. const char * Executable = GetProp(Results, PROP_EXECUTABLE, MODDAV_PROP_NAMESPACE);
  862. if (Executable != NULL)
  863. {
  864. if (strcmp(Executable, "T") == NULL)
  865. {
  866. UnicodeString ExecutableRights;
  867. // The "gear" character is supported since Windows 8
  868. if (IsWin8())
  869. {
  870. ExecutableRights = L"\u2699";
  871. }
  872. else
  873. {
  874. ExecutableRights = LoadStr(EXECUTABLE);
  875. }
  876. AddToList(HumanRights, ExecutableRights, RightsDelimiter);
  877. }
  878. }
  879. struct ne_lock * Lock = static_cast<struct ne_lock *>(ne_propset_private(Results));
  880. if ((Lock != NULL) && (Lock->token != NULL))
  881. {
  882. UnicodeString Owner;
  883. if (Lock->owner != NULL)
  884. {
  885. Owner = StrFromNeon(Lock->owner).Trim();
  886. }
  887. UnicodeString LockRights;
  888. if (IsWin8())
  889. {
  890. // The "lock" character is supported since Windows 8
  891. LockRights = L"\uD83D\uDD12" + Owner;
  892. }
  893. else
  894. {
  895. LockRights = LoadStr(LOCKED);
  896. if (!Owner.IsEmpty())
  897. {
  898. LockRights = FORMAT(L"%s (%s)", (LockRights, Owner));
  899. }
  900. }
  901. AddToList(HumanRights, LockRights, RightsDelimiter);
  902. }
  903. File->HumanRights = HumanRights;
  904. }
  905. //---------------------------------------------------------------------------
  906. int __fastcall TWebDAVFileSystem::CustomReadFileInternal(const UnicodeString FileName,
  907. TRemoteFile *& File, TRemoteFile * ALinkedByFile)
  908. {
  909. std::unique_ptr<TRemoteFile> AFile(new TRemoteFile(ALinkedByFile));
  910. TReadFileData Data;
  911. Data.FileSystem = this;
  912. Data.File = AFile.get();
  913. Data.FileList = NULL;
  914. ClearNeonError();
  915. int Result =
  916. ne_simple_propfind(FNeonSession, PathToNeon(FileName), NE_DEPTH_ZERO, NULL,
  917. NeonPropsResult, &Data);
  918. if (Result == NE_OK)
  919. {
  920. File = AFile.release();
  921. }
  922. return Result;
  923. }
  924. //---------------------------------------------------------------------------
  925. void __fastcall TWebDAVFileSystem::CustomReadFile(UnicodeString FileName,
  926. TRemoteFile *& File, TRemoteFile * ALinkedByFile)
  927. {
  928. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  929. int NeonStatus = CustomReadFileInternal(FileName, File, ALinkedByFile);
  930. if (IsValidRedirect(NeonStatus, FileName))
  931. {
  932. NeonStatus = CustomReadFileInternal(FileName, File, ALinkedByFile);
  933. }
  934. CheckStatus(NeonStatus);
  935. }
  936. //---------------------------------------------------------------------------
  937. void __fastcall TWebDAVFileSystem::DeleteFile(const UnicodeString FileName,
  938. const TRemoteFile * File, int /*Params*/, TRmSessionAction & Action)
  939. {
  940. Action.Recursive();
  941. ClearNeonError();
  942. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  943. RawByteString Path = PathToNeon(FilePath(File));
  944. // WebDAV does not allow non-recursive delete:
  945. // RFC 4918, section 9.6.1:
  946. // "A client MUST NOT submit a Depth header with a DELETE on a collection with any value but infinity."
  947. // We should check that folder is empty when called with FLAGSET(Params, dfNoRecursive)
  948. CheckStatus(ne_delete(FNeonSession, Path.c_str()));
  949. // The lock is removed with the file, but if a file with the same name gets created,
  950. // we would try to use obsoleted lock token with it, what the server would reject
  951. // (mod_dav returns "412 Precondition Failed")
  952. DiscardLock(Path);
  953. }
  954. //---------------------------------------------------------------------------
  955. int __fastcall TWebDAVFileSystem::RenameFileInternal(const UnicodeString & FileName,
  956. const UnicodeString & NewName)
  957. {
  958. // 0 = no overwrite
  959. return ne_move(FNeonSession, 0, PathToNeon(FileName), PathToNeon(NewName));
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TWebDAVFileSystem::RenameFile(const UnicodeString FileName, const TRemoteFile * /*File*/,
  963. const UnicodeString NewName)
  964. {
  965. ClearNeonError();
  966. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  967. UnicodeString Path = FileName;
  968. int NeonStatus = RenameFileInternal(Path, NewName);
  969. if (IsValidRedirect(NeonStatus, Path))
  970. {
  971. NeonStatus = RenameFileInternal(Path, NewName);
  972. }
  973. CheckStatus(NeonStatus);
  974. // See a comment in DeleteFile
  975. DiscardLock(PathToNeon(Path));
  976. }
  977. //---------------------------------------------------------------------------
  978. int __fastcall TWebDAVFileSystem::CopyFileInternal(const UnicodeString & FileName,
  979. const UnicodeString & NewName)
  980. {
  981. // 0 = no overwrite
  982. return ne_copy(FNeonSession, 0, NE_DEPTH_INFINITE, PathToNeon(FileName), PathToNeon(NewName));
  983. }
  984. //---------------------------------------------------------------------------
  985. void __fastcall TWebDAVFileSystem::CopyFile(const UnicodeString FileName, const TRemoteFile * /*File*/,
  986. const UnicodeString NewName)
  987. {
  988. ClearNeonError();
  989. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  990. UnicodeString Path = FileName;
  991. int NeonStatus = CopyFileInternal(Path, NewName);
  992. if (IsValidRedirect(NeonStatus, Path))
  993. {
  994. NeonStatus = CopyFileInternal(Path, NewName);
  995. }
  996. CheckStatus(NeonStatus);
  997. }
  998. //---------------------------------------------------------------------------
  999. void __fastcall TWebDAVFileSystem::CreateDirectory(const UnicodeString & DirName, bool /*Encrypt*/)
  1000. {
  1001. ClearNeonError();
  1002. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  1003. CheckStatus(ne_mkcol(FNeonSession, PathToNeon(DirName)));
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TWebDAVFileSystem::CreateLink(const UnicodeString FileName,
  1007. const UnicodeString PointTo, bool /*Symbolic*/)
  1008. {
  1009. DebugFail();
  1010. }
  1011. //---------------------------------------------------------------------------
  1012. void __fastcall TWebDAVFileSystem::ChangeFileProperties(const UnicodeString FileName,
  1013. const TRemoteFile * /*File*/, const TRemoteProperties * /*Properties*/,
  1014. TChmodSessionAction & /*Action*/)
  1015. {
  1016. DebugFail();
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. bool __fastcall TWebDAVFileSystem::LoadFilesProperties(TStrings * /*FileList*/)
  1020. {
  1021. DebugFail();
  1022. return false;
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TWebDAVFileSystem::CalculateFilesChecksum(const UnicodeString & /*Alg*/,
  1026. TStrings * /*FileList*/, TStrings * /*Checksums*/,
  1027. TCalculatedChecksumEvent /*OnCalculatedChecksum*/)
  1028. {
  1029. DebugFail();
  1030. }
  1031. //---------------------------------------------------------------------------
  1032. void __fastcall TWebDAVFileSystem::ConfirmOverwrite(
  1033. const UnicodeString & SourceFullFileName, UnicodeString & TargetFileName,
  1034. TFileOperationProgressType * OperationProgress,
  1035. const TOverwriteFileParams * FileParams, const TCopyParamType * CopyParam,
  1036. int Params)
  1037. {
  1038. // all = "yes to newer"
  1039. int Answers = qaYes | qaNo | qaCancel | qaYesToAll | qaNoToAll | qaAll;
  1040. TQueryButtonAlias Aliases[3];
  1041. Aliases[0] = TQueryButtonAlias::CreateAllAsYesToNewerGrouppedWithYes();
  1042. Aliases[1] = TQueryButtonAlias::CreateYesToAllGrouppedWithYes();
  1043. Aliases[2] = TQueryButtonAlias::CreateNoToAllGrouppedWithNo();
  1044. TQueryParams QueryParams(qpNeverAskAgainCheck);
  1045. QueryParams.Aliases = Aliases;
  1046. QueryParams.AliasesCount = LENOF(Aliases);
  1047. unsigned int Answer;
  1048. {
  1049. TSuspendFileOperationProgress Suspend(OperationProgress);
  1050. Answer =
  1051. FTerminal->ConfirmFileOverwrite(
  1052. SourceFullFileName, TargetFileName, FileParams, Answers, &QueryParams,
  1053. ReverseOperationSide(OperationProgress->Side),
  1054. CopyParam, Params, OperationProgress);
  1055. }
  1056. switch (Answer)
  1057. {
  1058. case qaYes:
  1059. // noop
  1060. break;
  1061. case qaNo:
  1062. throw ESkipFile();
  1063. default:
  1064. DebugFail();
  1065. case qaCancel:
  1066. OperationProgress->SetCancelAtLeast(csCancel);
  1067. Abort();
  1068. break;
  1069. }
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. void __fastcall TWebDAVFileSystem::CustomCommandOnFile(const UnicodeString FileName,
  1073. const TRemoteFile * /*File*/, UnicodeString Command, int /*Params*/, TCaptureOutputEvent /*OutputEvent*/)
  1074. {
  1075. DebugFail();
  1076. }
  1077. //---------------------------------------------------------------------------
  1078. void __fastcall TWebDAVFileSystem::AnyCommand(const UnicodeString Command,
  1079. TCaptureOutputEvent /*OutputEvent*/)
  1080. {
  1081. DebugFail();
  1082. }
  1083. //---------------------------------------------------------------------------
  1084. TStrings * __fastcall TWebDAVFileSystem::GetFixedPaths()
  1085. {
  1086. return NULL;
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void TWebDAVFileSystem::NeonQuotaResult(
  1090. void * UserData, const ne_uri * /*Uri*/, const ne_prop_result_set * Results)
  1091. {
  1092. TSpaceAvailable & SpaceAvailable = *static_cast<TSpaceAvailable *>(UserData);
  1093. const char * Value = GetProp(Results, PROP_QUOTA_AVAILABLE);
  1094. if (Value != NULL)
  1095. {
  1096. SpaceAvailable.UnusedBytesAvailableToUser = StrToInt64(StrFromNeon(Value));
  1097. const char * Value = GetProp(Results, PROP_QUOTA_USED);
  1098. if (Value != NULL)
  1099. {
  1100. SpaceAvailable.BytesAvailableToUser =
  1101. StrToInt64(StrFromNeon(Value)) + SpaceAvailable.UnusedBytesAvailableToUser;
  1102. }
  1103. }
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. void __fastcall TWebDAVFileSystem::SpaceAvailable(const UnicodeString Path,
  1107. TSpaceAvailable & ASpaceAvailable)
  1108. {
  1109. // RFC4331: https://tools.ietf.org/html/rfc4331
  1110. // This is known to be supported by:
  1111. // OpenDrive: for a root drive only (and contrary to the spec, it sends the properties
  1112. // unconditionally, even when not explicitly requested)
  1113. // Server: Apache/2.2.17 (Fedora)
  1114. // X-Powered-By: PHP/5.5.7
  1115. // X-DAV-Powered-By: OpenDrive
  1116. // WWW-Authenticate: Basic realm="PHP WebDAV"
  1117. // IT Hit WebDAV Server:
  1118. // Server: Microsoft-HTTPAPI/1.0
  1119. // X-Engine: IT Hit WebDAV Server .Net v3.8.1877.0 (Evaluation License)
  1120. // Yandex disk:
  1121. // WWW-Authenticate: Basic realm="Yandex.Disk"
  1122. // Server: MochiWeb/1.0
  1123. // OneDrive:
  1124. // it sends the properties unconditionally, even when not explicitly requested
  1125. UnicodeString APath = DirectoryPath(Path);
  1126. ne_propname QuotaProps[3];
  1127. memset(QuotaProps, 0, sizeof(QuotaProps));
  1128. QuotaProps[0].nspace = DAV_PROP_NAMESPACE;
  1129. QuotaProps[0].name = PROP_QUOTA_AVAILABLE;
  1130. QuotaProps[1].nspace = DAV_PROP_NAMESPACE;
  1131. QuotaProps[1].name = PROP_QUOTA_USED;
  1132. QuotaProps[2].nspace = NULL;
  1133. QuotaProps[2].name = NULL;
  1134. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  1135. CheckStatus(
  1136. ne_simple_propfind(FNeonSession, PathToNeon(APath), NE_DEPTH_ZERO, QuotaProps,
  1137. NeonQuotaResult, &ASpaceAvailable));
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TWebDAVFileSystem::CopyToRemote(TStrings * FilesToCopy,
  1141. const UnicodeString TargetDir, const TCopyParamType * CopyParam,
  1142. int Params, TFileOperationProgressType * OperationProgress,
  1143. TOnceDoneOperation & OnceDoneOperation)
  1144. {
  1145. Params &= ~cpAppend;
  1146. FTerminal->DoCopyToRemote(FilesToCopy, TargetDir, CopyParam, Params, OperationProgress, tfPreCreateDir, OnceDoneOperation);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TWebDAVFileSystem::Source(
  1150. TLocalFileHandle & Handle, const UnicodeString & TargetDir, UnicodeString & DestFileName,
  1151. const TCopyParamType * CopyParam, int Params,
  1152. TFileOperationProgressType * OperationProgress, unsigned int /*Flags*/,
  1153. TUploadSessionAction & Action, bool & ChildError)
  1154. {
  1155. int FD = -1;
  1156. try
  1157. {
  1158. UnicodeString DestFullName = TargetDir + DestFileName;
  1159. TRemoteFile * RemoteFile = NULL;
  1160. try
  1161. {
  1162. TValueRestorer<TIgnoreAuthenticationFailure> IgnoreAuthenticationFailureRestorer(FIgnoreAuthenticationFailure);
  1163. FIgnoreAuthenticationFailure = iafWaiting;
  1164. // this should not throw
  1165. CustomReadFileInternal(DestFullName, RemoteFile, NULL);
  1166. }
  1167. catch (...)
  1168. {
  1169. if (!FTerminal->Active)
  1170. {
  1171. throw;
  1172. }
  1173. }
  1174. if (RemoteFile != NULL)
  1175. {
  1176. TOverwriteFileParams FileParams;
  1177. FileParams.SourceSize = Handle.Size;
  1178. FileParams.SourceTimestamp = Handle.Modification;
  1179. FileParams.DestSize = RemoteFile->Size;
  1180. FileParams.DestTimestamp = RemoteFile->Modification;
  1181. delete RemoteFile;
  1182. ConfirmOverwrite(Handle.FileName, DestFileName, OperationProgress,
  1183. &FileParams, CopyParam, Params);
  1184. }
  1185. DestFullName = TargetDir + DestFileName;
  1186. // only now, we know the final destination
  1187. // (not really true as we do not support changing file name on overwrite dialog)
  1188. Action.Destination(DestFullName);
  1189. FUploadMimeType = Configuration->GetFileMimeType(DestFileName);
  1190. FILE_OPERATION_LOOP_BEGIN
  1191. {
  1192. SetFilePointer(Handle.Handle, 0, NULL, FILE_BEGIN);
  1193. FD = _open_osfhandle((intptr_t)Handle.Handle, O_BINARY);
  1194. if (FD < 0)
  1195. {
  1196. throw ESkipFile();
  1197. }
  1198. TAutoFlag UploadingFlag(FUploading);
  1199. ClearNeonError();
  1200. CheckStatus(ne_put(FNeonSession, PathToNeon(DestFullName), FD));
  1201. }
  1202. FILE_OPERATION_LOOP_END(FMTLOAD(TRANSFER_ERROR, (Handle.FileName)));
  1203. if (CopyParam->PreserveTime)
  1204. {
  1205. FTerminal->LogEvent(FORMAT(L"Preserving timestamp [%s]",
  1206. (StandardTimestamp(Handle.Modification))));
  1207. TTouchSessionAction TouchAction(FTerminal->ActionLog, DestFullName, Handle.Modification);
  1208. try
  1209. {
  1210. TDateTime ModificationUTC = ConvertTimestampToUTC(Handle.Modification);
  1211. TFormatSettings FormatSettings = GetEngFormatSettings();
  1212. UnicodeString LastModified =
  1213. FormatDateTime(L"ddd, d mmm yyyy hh:nn:ss 'GMT'", ModificationUTC, FormatSettings);
  1214. UTF8String NeonLastModified(LastModified);
  1215. // second element is "NULL-terminating"
  1216. ne_proppatch_operation Operations[2];
  1217. memset(Operations, 0, sizeof(Operations));
  1218. ne_propname LastModifiedProp;
  1219. LastModifiedProp.nspace = DAV_PROP_NAMESPACE;
  1220. LastModifiedProp.name = PROP_LAST_MODIFIED;
  1221. Operations[0].name = &LastModifiedProp;
  1222. Operations[0].type = ne_propset;
  1223. Operations[0].value = NeonLastModified.c_str();
  1224. int Status = ne_proppatch(FNeonSession, PathToNeon(DestFullName), Operations);
  1225. if (Status == NE_ERROR)
  1226. {
  1227. FTerminal->LogEvent(FORMAT(L"Preserving timestamp failed, ignoring: %s",
  1228. (GetNeonError())));
  1229. // Ignore errors as major WebDAV servers (like IIS), do not support
  1230. // changing getlastmodified.
  1231. // The only server we found that supports this is TradeMicro SafeSync.
  1232. // But it announces itself as "Server: Apache",
  1233. // so it's not reliable to autodetect the support.
  1234. // Microsoft Office alegedly uses <Win32LastModifiedTime>
  1235. // https://sabre.io/dav/clients/msoffice/
  1236. // Carot DAV does that too. But we do not know what server does support this.
  1237. TouchAction.Cancel();
  1238. }
  1239. else
  1240. {
  1241. CheckStatus(Status);
  1242. }
  1243. }
  1244. catch (Exception & E)
  1245. {
  1246. TouchAction.Rollback(&E);
  1247. ChildError = true;
  1248. throw;
  1249. }
  1250. }
  1251. }
  1252. __finally
  1253. {
  1254. if (FD >= 0)
  1255. {
  1256. // _close calls CloseHandle internally (even doc states, we should not call CloseHandle),
  1257. // but it crashes code guard
  1258. _close(FD);
  1259. Handle.Dismiss();
  1260. }
  1261. }
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. void __fastcall TWebDAVFileSystem::CopyToLocal(TStrings * FilesToCopy,
  1265. const UnicodeString TargetDir, const TCopyParamType * CopyParam,
  1266. int Params, TFileOperationProgressType * OperationProgress,
  1267. TOnceDoneOperation & OnceDoneOperation)
  1268. {
  1269. Params &= ~cpAppend;
  1270. FTerminal->DoCopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, OperationProgress, tfNone, OnceDoneOperation);
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void TWebDAVFileSystem::NeonCreateRequest(
  1274. ne_request * Request, void * UserData, const char * /*Method*/, const char * /*Uri*/)
  1275. {
  1276. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1277. ne_set_request_private(Request, SESSION_FS_KEY, FileSystem);
  1278. ne_add_response_body_reader(Request, NeonBodyAccepter, NeonBodyReader, Request);
  1279. FileSystem->FNtlmAuthenticationFailed = false;
  1280. }
  1281. //---------------------------------------------------------------------------
  1282. void TWebDAVFileSystem::NeonPreSend(
  1283. ne_request * Request, void * UserData, ne_buffer * Header)
  1284. {
  1285. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1286. FileSystem->FAuthorizationProtocol = L"";
  1287. UnicodeString HeaderBuf(StrFromNeon(AnsiString(Header->data, Header->used)));
  1288. const UnicodeString AuthorizationHeaderName(L"Authorization:");
  1289. int P = HeaderBuf.Pos(AuthorizationHeaderName);
  1290. if (P > 0)
  1291. {
  1292. P += AuthorizationHeaderName.Length();
  1293. int P2 = PosEx(L"\n", HeaderBuf, P);
  1294. if (DebugAlwaysTrue(P2 > 0))
  1295. {
  1296. UnicodeString AuthorizationHeader = HeaderBuf.SubString(P, P2 - P).Trim();
  1297. FileSystem->FAuthorizationProtocol = CutToChar(AuthorizationHeader, L' ', false);
  1298. FileSystem->FLastAuthorizationProtocol = FileSystem->FAuthorizationProtocol;
  1299. }
  1300. }
  1301. if (FileSystem->FDownloading)
  1302. {
  1303. // Needed by IIS server to make it download source code, not code output,
  1304. // and mainly to even allow downloading file with unregistered extensions.
  1305. // Without it files like .001 return 404 (Not found) HTTP code.
  1306. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wdv/e37a9543-9290-4843-8c04-66457c60fa0a
  1307. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wdvse/501879f9-3875-4d7a-ab88-3cecab440034
  1308. // http://lists.manyfish.co.uk/pipermail/neon/2012-April/001452.html
  1309. // It's also supported by Oracle server:
  1310. // https://docs.oracle.com/cd/E19146-01/821-1828/gczya/index.html
  1311. // We do not know yet of any server that fails when the header is used,
  1312. // so it's added unconditionally.
  1313. ne_buffer_zappend(Header, "Translate: f\r\n");
  1314. }
  1315. const UnicodeString ContentTypeHeaderPrefix(L"Content-Type: ");
  1316. if (FileSystem->FTerminal->Log->Logging)
  1317. {
  1318. const char * Buffer;
  1319. size_t Size;
  1320. if (ne_get_request_body_buffer(Request, &Buffer, &Size))
  1321. {
  1322. // all neon request types that use ne_add_request_header
  1323. // use XML content-type, so it's text-based
  1324. DebugAssert(ContainsStr(HeaderBuf, ContentTypeHeaderPrefix + NE_XML_MEDIA_TYPE));
  1325. FileSystem->FTerminal->Log->Add(llInput, UnicodeString(UTF8String(Buffer, Size)));
  1326. }
  1327. }
  1328. if (FileSystem->FUploading)
  1329. {
  1330. ne_set_request_body_provider_pre(Request,
  1331. FileSystem->NeonUploadBodyProvider, FileSystem);
  1332. if (!FileSystem->FUploadMimeType.IsEmpty())
  1333. {
  1334. UnicodeString ContentTypeHeader = ContentTypeHeaderPrefix + FileSystem->FUploadMimeType + L"\r\n";
  1335. ne_buffer_zappend(Header, AnsiString(ContentTypeHeader).c_str());
  1336. }
  1337. }
  1338. FileSystem->FResponse = L"";
  1339. }
  1340. //---------------------------------------------------------------------------
  1341. int TWebDAVFileSystem::NeonPostSend(ne_request * /*Req*/, void * UserData,
  1342. const ne_status * /*Status*/)
  1343. {
  1344. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1345. if (!FileSystem->FResponse.IsEmpty())
  1346. {
  1347. FileSystem->FTerminal->Log->Add(llOutput, FileSystem->FResponse);
  1348. }
  1349. return NE_OK;
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. bool __fastcall TWebDAVFileSystem::IsNtlmAuthentication()
  1353. {
  1354. return
  1355. SameText(FAuthorizationProtocol, L"NTLM") ||
  1356. SameText(FAuthorizationProtocol, L"Negotiate");
  1357. }
  1358. //---------------------------------------------------------------------------
  1359. void __fastcall TWebDAVFileSystem::HttpAuthenticationFailed()
  1360. {
  1361. // NTLM/GSSAPI failed
  1362. if (IsNtlmAuthentication())
  1363. {
  1364. if (FNtlmAuthenticationFailed)
  1365. {
  1366. // Next time do not try Negotiate (NTLM/GSSAPI),
  1367. // otherwise we end up in an endless loop.
  1368. // If the server returns all other challenges in the response, removing the Negotiate
  1369. // protocol will itself ensure that other protocols are tried (we haven't seen this behaviour).
  1370. // IIS will return only Negotiate response if the request was Negotiate, so there's no fallback.
  1371. // We have to retry with a fresh request. That's what FAuthenticationRetry does.
  1372. FTerminal->LogEvent(FORMAT(L"%s challenge failed, will try different challenge", (FAuthorizationProtocol)));
  1373. ne_remove_server_auth(FNeonSession);
  1374. NeonAddAuthentiation(false);
  1375. FAuthenticationRetry = true;
  1376. }
  1377. else
  1378. {
  1379. // The first 401 is expected, the server is using it to send WWW-Authenticate header with data.
  1380. FNtlmAuthenticationFailed = true;
  1381. }
  1382. }
  1383. }
  1384. //---------------------------------------------------------------------------
  1385. void TWebDAVFileSystem::NeonPostHeaders(ne_request * /*Req*/, void * UserData, const ne_status * Status)
  1386. {
  1387. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1388. if (Status->code == HttpUnauthorized)
  1389. {
  1390. FileSystem->HttpAuthenticationFailed();
  1391. }
  1392. }
  1393. //---------------------------------------------------------------------------
  1394. ssize_t TWebDAVFileSystem::NeonUploadBodyProvider(void * UserData, char * /*Buffer*/, size_t /*BufLen*/)
  1395. {
  1396. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1397. ssize_t Result;
  1398. if (FileSystem->CancelTransfer())
  1399. {
  1400. Result = -1;
  1401. }
  1402. else
  1403. {
  1404. Result = 1;
  1405. }
  1406. return Result;
  1407. }
  1408. //---------------------------------------------------------------------------
  1409. static void __fastcall AddHeaderValueToList(UnicodeString & List, ne_request * Request, const char * Name)
  1410. {
  1411. const char * Value = ne_get_response_header(Request, Name);
  1412. if (Value != NULL)
  1413. {
  1414. AddToList(List, StrFromNeon(Value), L"; ");
  1415. }
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. int TWebDAVFileSystem::NeonBodyAccepter(void * UserData, ne_request * Request, const ne_status * Status)
  1419. {
  1420. DebugAssert(UserData == Request);
  1421. TWebDAVFileSystem * FileSystem =
  1422. static_cast<TWebDAVFileSystem *>(ne_get_request_private(Request, SESSION_FS_KEY));
  1423. bool AuthenticationFailureCode = (Status->code == HttpUnauthorized);
  1424. bool PasswordAuthenticationFailed = AuthenticationFailureCode && FileSystem->FAuthenticationRequested;
  1425. bool AuthenticationFailed = PasswordAuthenticationFailed || (AuthenticationFailureCode && FileSystem->IsNtlmAuthentication());
  1426. bool AuthenticationNeeded = AuthenticationFailureCode && !AuthenticationFailed;
  1427. if (FileSystem->FInitialHandshake)
  1428. {
  1429. UnicodeString Line;
  1430. if (AuthenticationNeeded)
  1431. {
  1432. Line = LoadStr(STATUS_AUTHENTICATE);
  1433. }
  1434. else if (AuthenticationFailed)
  1435. {
  1436. Line = LoadStr(FTP_ACCESS_DENIED);
  1437. }
  1438. else if (Status->klass == 2)
  1439. {
  1440. Line = LoadStr(STATUS_AUTHENTICATED);
  1441. }
  1442. if (!Line.IsEmpty())
  1443. {
  1444. FileSystem->FTerminal->Information(Line, true);
  1445. }
  1446. UnicodeString RemoteSystem;
  1447. // Used by IT Hit WebDAV Server:
  1448. // Server: Microsoft-HTTPAPI/1.0
  1449. // X-Engine: IT Hit WebDAV Server .Net v3.8.1877.0 (Evaluation License)
  1450. AddHeaderValueToList(RemoteSystem, Request, "X-Engine");
  1451. // Used by OpenDrive:
  1452. // Server: Apache/2.2.17 (Fedora)
  1453. // X-Powered-By: PHP/5.5.7
  1454. // X-DAV-Powered-By: OpenDrive
  1455. AddHeaderValueToList(RemoteSystem, Request, "X-DAV-Powered-By");
  1456. // Used by IIS:
  1457. // Server: Microsoft-IIS/8.5
  1458. AddHeaderValueToList(RemoteSystem, Request, "Server");
  1459. // Not really useful.
  1460. // Can be e.g. "PleskLin"
  1461. AddHeaderValueToList(RemoteSystem, Request, "X-Powered-By");
  1462. FileSystem->FFileSystemInfo.RemoteSystem = RemoteSystem;
  1463. }
  1464. // When we explicitly fail authentication of request
  1465. // with FIgnoreAuthenticationFailure flag (after it failed with password),
  1466. // neon resets its internal password store and tries the next request
  1467. // without calling our authentication hook first
  1468. // (note AuthenticationFailed vs. AuthenticationNeeded)
  1469. // what likely fails, but we do not want to reset out password
  1470. // (as it was not even tried yet for this request).
  1471. if (PasswordAuthenticationFailed)
  1472. {
  1473. if (FileSystem->FIgnoreAuthenticationFailure == iafNo)
  1474. {
  1475. FileSystem->FPassword = RawByteString();
  1476. }
  1477. else
  1478. {
  1479. FileSystem->FIgnoreAuthenticationFailure = iafPasswordFailed;
  1480. }
  1481. }
  1482. return ne_accept_2xx(UserData, Request, Status);
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. bool __fastcall TWebDAVFileSystem::CancelTransfer()
  1486. {
  1487. bool Result = false;
  1488. if ((FUploading || FDownloading) &&
  1489. (FTerminal->OperationProgress != NULL) &&
  1490. (FTerminal->OperationProgress->Cancel != csContinue))
  1491. {
  1492. if (FTerminal->OperationProgress->ClearCancelFile())
  1493. {
  1494. FSkipped = true;
  1495. }
  1496. else
  1497. {
  1498. FCancelled = true;
  1499. }
  1500. Result = true;
  1501. }
  1502. return Result;
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. int TWebDAVFileSystem::NeonBodyReader(void * UserData, const char * Buf, size_t Len)
  1506. {
  1507. ne_request * Request = static_cast<ne_request *>(UserData);
  1508. TWebDAVFileSystem * FileSystem =
  1509. static_cast<TWebDAVFileSystem *>(ne_get_request_private(Request, SESSION_FS_KEY));
  1510. if (FileSystem->FTerminal->Log->Logging)
  1511. {
  1512. ne_content_type ContentType;
  1513. if (ne_get_content_type(Request, &ContentType) == 0)
  1514. {
  1515. // The main point of the content-type check was to exclude
  1516. // GET responses (with file contents).
  1517. // But this won't work when downloading text files that have text
  1518. // content type on their own, hence the additional not-downloading test.
  1519. if (!FileSystem->FDownloading &&
  1520. ((ne_strcasecmp(ContentType.type, "text") == 0) ||
  1521. media_type_is_xml(&ContentType)))
  1522. {
  1523. UnicodeString Content = UnicodeString(UTF8String(Buf, Len)).Trim();
  1524. FileSystem->FResponse += Content;
  1525. }
  1526. ne_free(ContentType.value);
  1527. }
  1528. }
  1529. int Result = FileSystem->CancelTransfer() ? 1 : 0;
  1530. return Result;
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. void __fastcall TWebDAVFileSystem::Sink(
  1534. const UnicodeString & FileName, const TRemoteFile * File,
  1535. const UnicodeString & TargetDir, UnicodeString & DestFileName, int Attrs,
  1536. const TCopyParamType * CopyParam, int Params, TFileOperationProgressType * OperationProgress,
  1537. unsigned int /*Flags*/, TDownloadSessionAction & Action)
  1538. {
  1539. UnicodeString DestFullName = TargetDir + DestFileName;
  1540. if (FileExists(ApiPath(DestFullName)))
  1541. {
  1542. __int64 Size;
  1543. __int64 MTime;
  1544. FTerminal->OpenLocalFile(DestFullName, GENERIC_READ, NULL, NULL, NULL, &MTime, NULL, &Size);
  1545. TOverwriteFileParams FileParams;
  1546. FileParams.SourceSize = File->Size;
  1547. FileParams.SourceTimestamp = File->Modification;
  1548. FileParams.DestSize = Size;
  1549. FileParams.DestTimestamp = UnixToDateTime(MTime, FTerminal->SessionData->DSTMode);
  1550. ConfirmOverwrite(FileName, DestFileName, OperationProgress, &FileParams, CopyParam, Params);
  1551. }
  1552. UnicodeString ExpandedDestFullName = ExpandUNCFileName(DestFullName);
  1553. Action.Destination(ExpandedDestFullName);
  1554. FILE_OPERATION_LOOP_BEGIN
  1555. {
  1556. HANDLE LocalHandle;
  1557. if (!FTerminal->CreateLocalFile(DestFullName, OperationProgress, &LocalHandle, FLAGSET(Params, cpNoConfirmation)))
  1558. {
  1559. throw ESkipFile();
  1560. }
  1561. bool DeleteLocalFile = true;
  1562. int FD = -1;
  1563. try
  1564. {
  1565. FD = _open_osfhandle((intptr_t)LocalHandle, O_BINARY);
  1566. if (FD < 0)
  1567. {
  1568. throw ESkipFile();
  1569. }
  1570. TAutoFlag DownloadingFlag(FDownloading);
  1571. ClearNeonError();
  1572. CheckStatus(ne_get(FNeonSession, PathToNeon(FileName), FD));
  1573. DeleteLocalFile = false;
  1574. if (CopyParam->PreserveTime)
  1575. {
  1576. FTerminal->UpdateTargetTime(LocalHandle, File->Modification, FTerminal->SessionData->DSTMode);
  1577. }
  1578. }
  1579. __finally
  1580. {
  1581. if (FD >= 0)
  1582. {
  1583. // _close calls CloseHandle internally (even doc states, we should not call CloseHandle),
  1584. // but it crashes code guard
  1585. _close(FD);
  1586. }
  1587. else
  1588. {
  1589. CloseHandle(LocalHandle);
  1590. }
  1591. if (DeleteLocalFile)
  1592. {
  1593. FILE_OPERATION_LOOP_BEGIN
  1594. {
  1595. THROWOSIFFALSE(Sysutils::DeleteFile(ApiPath(DestFullName)));
  1596. }
  1597. FILE_OPERATION_LOOP_END(FMTLOAD(DELETE_LOCAL_FILE_ERROR, (DestFullName)));
  1598. }
  1599. }
  1600. }
  1601. FILE_OPERATION_LOOP_END(FMTLOAD(TRANSFER_ERROR, (FileName)));
  1602. FTerminal->UpdateTargetAttrs(DestFullName, File, CopyParam, Attrs);
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. // Similar to TS3FileSystem::VerifyCertificate
  1606. bool TWebDAVFileSystem::VerifyCertificate(TNeonCertificateData Data, bool Aux)
  1607. {
  1608. FSessionInfo.CertificateFingerprintSHA1 = Data.FingerprintSHA1;
  1609. FSessionInfo.CertificateFingerprintSHA256 = Data.FingerprintSHA256;
  1610. bool Result;
  1611. if (FTerminal->SessionData->FingerprintScan)
  1612. {
  1613. Result = false;
  1614. }
  1615. else
  1616. {
  1617. FTerminal->LogEvent(0, CertificateVerificationMessage(Data));
  1618. UnicodeString SiteKey = TSessionData::FormatSiteKey(FHostName, FPortNumber);
  1619. Result =
  1620. FTerminal->VerifyCertificate(
  1621. HttpsCertificateStorageKey, SiteKey, Data.FingerprintSHA1, Data.FingerprintSHA256, Data.Subject, Data.Failures);
  1622. if (Result)
  1623. {
  1624. FSessionInfo.CertificateVerifiedManually = true;
  1625. }
  1626. else
  1627. {
  1628. UnicodeString Message;
  1629. Result = NeonWindowsValidateCertificateWithMessage(Data, Message);
  1630. FTerminal->LogEvent(0, Message);
  1631. }
  1632. FSessionInfo.Certificate = CertificateSummary(Data, FHostName);
  1633. if (!Result)
  1634. {
  1635. if (FTerminal->ConfirmCertificate(FSessionInfo, Data.Failures, HttpsCertificateStorageKey, !Aux))
  1636. {
  1637. Result = true;
  1638. FSessionInfo.CertificateVerifiedManually = true;
  1639. }
  1640. }
  1641. if (Result && !Aux)
  1642. {
  1643. CollectTLSSessionInfo();
  1644. }
  1645. }
  1646. return Result;
  1647. }
  1648. //------------------------------------------------------------------------------
  1649. void __fastcall TWebDAVFileSystem::CollectTLSSessionInfo()
  1650. {
  1651. // See also TFTPFileSystem::Open().
  1652. // Have to cache the value as the connection (the neon HTTP session, not "our" session)
  1653. // can be closed at the time we need it in CollectUsage().
  1654. UnicodeString Message = NeonTlsSessionInfo(FNeonSession, FSessionInfo, FTlsVersionStr);
  1655. FTerminal->LogEvent(0, Message);
  1656. }
  1657. //------------------------------------------------------------------------------
  1658. // A neon-session callback to validate the SSL certificate when the CA
  1659. // is unknown (e.g. a self-signed cert), or there are other SSL
  1660. // certificate problems.
  1661. int TWebDAVFileSystem::DoNeonServerSSLCallback(void * UserData, int Failures, const ne_ssl_certificate * Certificate, bool Aux)
  1662. {
  1663. TNeonCertificateData Data;
  1664. RetrieveNeonCertificateData(Failures, Certificate, Data);
  1665. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1666. return FileSystem->VerifyCertificate(Data, Aux) ? NE_OK : NE_ERROR;
  1667. }
  1668. //------------------------------------------------------------------------------
  1669. int TWebDAVFileSystem::NeonServerSSLCallbackMain(void * UserData, int Failures, const ne_ssl_certificate * Certificate)
  1670. {
  1671. return DoNeonServerSSLCallback(UserData, Failures, Certificate, false);
  1672. }
  1673. //------------------------------------------------------------------------------
  1674. int TWebDAVFileSystem::NeonServerSSLCallbackAux(void * UserData, int Failures, const ne_ssl_certificate * Certificate)
  1675. {
  1676. return DoNeonServerSSLCallback(UserData, Failures, Certificate, true);
  1677. }
  1678. //------------------------------------------------------------------------------
  1679. void TWebDAVFileSystem::NeonProvideClientCert(void * UserData, ne_session * Sess,
  1680. const ne_ssl_dname * const * /*DNames*/, int /*DNCount*/)
  1681. {
  1682. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1683. FileSystem->FTerminal->LogEvent(LoadStr(NEED_CLIENT_CERTIFICATE));
  1684. X509 * Certificate;
  1685. EVP_PKEY * PrivateKey;
  1686. if (FileSystem->FTerminal->LoadTlsCertificate(Certificate, PrivateKey))
  1687. {
  1688. ne_ssl_client_cert * NeonCertificate = ne_ssl_clicert_create(Certificate, PrivateKey);
  1689. ne_ssl_set_clicert(Sess, NeonCertificate);
  1690. ne_ssl_clicert_free(NeonCertificate);
  1691. }
  1692. }
  1693. //------------------------------------------------------------------------------
  1694. int TWebDAVFileSystem::NeonRequestAuth(
  1695. void * UserData, const char * Realm, int Attempt, char * UserName, char * Password)
  1696. {
  1697. DebugUsedParam(Realm);
  1698. DebugUsedParam(Attempt);
  1699. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1700. TTerminal * Terminal = FileSystem->FTerminal;
  1701. TSessionData * SessionData = Terminal->SessionData;
  1702. bool Result = true;
  1703. // will ask for username only once
  1704. if (FileSystem->FUserName.IsEmpty())
  1705. {
  1706. if (!SessionData->UserName.IsEmpty())
  1707. {
  1708. FileSystem->FUserName = SessionData->UserNameExpanded;
  1709. }
  1710. else
  1711. {
  1712. Terminal->LogEvent(L"Username prompt");
  1713. if (!Terminal->PromptUser(SessionData, pkUserName, LoadStr(USERNAME_TITLE), L"",
  1714. LoadStr(USERNAME_PROMPT2), true, NE_ABUFSIZ, FileSystem->FUserName))
  1715. {
  1716. // note that we never get here actually
  1717. Result = false;
  1718. }
  1719. }
  1720. }
  1721. UnicodeString APassword;
  1722. if (Result)
  1723. {
  1724. // Some servers (Gallery2 on discontinued g2.pixi.me)
  1725. // return authentication error (401) on PROPFIND request for
  1726. // non-existing files.
  1727. // When we already tried password before, do not try anymore.
  1728. // When we did not try password before (possible only when
  1729. // server does not require authentication for any previous request,
  1730. // such as when read access is not authenticated), try it now,
  1731. // but use special flag for the try, because when it fails
  1732. // we still want to try password for future requests (such as PUT).
  1733. if (!FileSystem->FPassword.IsEmpty())
  1734. {
  1735. if (FileSystem->FIgnoreAuthenticationFailure == iafPasswordFailed)
  1736. {
  1737. // Fail PROPFIND /nonexising request...
  1738. Result = false;
  1739. }
  1740. else
  1741. {
  1742. APassword = Terminal->DecryptPassword(FileSystem->FPassword);
  1743. }
  1744. }
  1745. else
  1746. {
  1747. if (!SessionData->Password.IsEmpty() && !FileSystem->FStoredPasswordTried)
  1748. {
  1749. APassword = NormalizeString(SessionData->Password);
  1750. FileSystem->FStoredPasswordTried = true;
  1751. }
  1752. else
  1753. {
  1754. // Asking for password (or using configured password) the first time,
  1755. // and asking for password.
  1756. // Note that we never get false here actually
  1757. Terminal->LogEvent(L"Password prompt");
  1758. Result =
  1759. Terminal->PromptUser(
  1760. SessionData, pkPassword, LoadStr(PASSWORD_TITLE), L"",
  1761. LoadStr(PASSWORD_PROMPT), false, NE_ABUFSIZ, APassword);
  1762. }
  1763. if (Result)
  1764. {
  1765. // While neon remembers the password on its own,
  1766. // we need to keep a copy in case neon store gets reset by
  1767. // 401 response to PROPFIND /nonexisting on G2, see above.
  1768. // Possibly we can do this for G2 servers only.
  1769. FileSystem->FPassword = Terminal->EncryptPassword(APassword);
  1770. }
  1771. }
  1772. }
  1773. if (Result)
  1774. {
  1775. strncpy(UserName, StrToNeon(FileSystem->FUserName), NE_ABUFSIZ);
  1776. strncpy(Password, StrToNeon(APassword), NE_ABUFSIZ);
  1777. }
  1778. FileSystem->FAuthenticationRequested = true;
  1779. return Result ? 0 : -1;
  1780. }
  1781. //------------------------------------------------------------------------------
  1782. void TWebDAVFileSystem::NeonNotifier(void * UserData, ne_session_status Status, const ne_session_status_info * StatusInfo)
  1783. {
  1784. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1785. TFileOperationProgressType * OperationProgress = FileSystem->FTerminal->OperationProgress;
  1786. // We particularly have to filter out response to "put" request,
  1787. // handling that would reset the upload progress back to low number (response is small).
  1788. if (((FileSystem->FUploading && (Status == ne_status_sending)) ||
  1789. (FileSystem->FDownloading && (Status == ne_status_recving))) &&
  1790. DebugAlwaysTrue(OperationProgress != NULL))
  1791. {
  1792. __int64 Progress = StatusInfo->sr.progress;
  1793. __int64 Diff = Progress - OperationProgress->TransferredSize;
  1794. if (Diff > 0)
  1795. {
  1796. OperationProgress->ThrottleToCPSLimit(static_cast<unsigned long>(Diff));
  1797. }
  1798. __int64 Total = StatusInfo->sr.total;
  1799. // Total size unknown
  1800. if (Total < 0)
  1801. {
  1802. if (Diff >= 0)
  1803. {
  1804. OperationProgress->AddTransferred(Diff);
  1805. }
  1806. else
  1807. {
  1808. // Session total has been reset. A new stream started
  1809. OperationProgress->AddTransferred(Progress);
  1810. }
  1811. }
  1812. else
  1813. {
  1814. OperationProgress->SetTransferSize(Total);
  1815. OperationProgress->AddTransferred(Diff);
  1816. }
  1817. }
  1818. }
  1819. //------------------------------------------------------------------------------
  1820. void TWebDAVFileSystem::InitSslSession(ssl_st * Ssl, ne_session * /*Session*/)
  1821. {
  1822. SetupSsl(Ssl, FTerminal->SessionData->MinTlsVersion, FTerminal->SessionData->MaxTlsVersion);
  1823. }
  1824. //---------------------------------------------------------------------------
  1825. void __fastcall TWebDAVFileSystem::GetSupportedChecksumAlgs(TStrings * /*Algs*/)
  1826. {
  1827. // NOOP
  1828. }
  1829. //---------------------------------------------------------------------------
  1830. void __fastcall TWebDAVFileSystem::LockFile(const UnicodeString & /*FileName*/, const TRemoteFile * File)
  1831. {
  1832. ClearNeonError();
  1833. struct ne_lock * Lock = ne_lock_create();
  1834. try
  1835. {
  1836. Lock->uri.path = ne_strdup(PathToNeon(FilePath(File)));
  1837. Lock->depth = NE_DEPTH_INFINITE;
  1838. Lock->timeout = NE_TIMEOUT_INFINITE;
  1839. Lock->owner = ne_strdup(StrToNeon(FTerminal->UserName));
  1840. CheckStatus(ne_lock(FNeonSession, Lock));
  1841. {
  1842. TGuard Guard(FNeonLockStoreSection);
  1843. RequireLockStore();
  1844. ne_lockstore_add(FNeonLockStore, Lock);
  1845. }
  1846. // ownership passed
  1847. Lock = NULL;
  1848. }
  1849. __finally
  1850. {
  1851. if (Lock != NULL)
  1852. {
  1853. ne_lock_destroy(Lock);
  1854. }
  1855. }
  1856. }
  1857. //---------------------------------------------------------------------------
  1858. void __fastcall TWebDAVFileSystem::RequireLockStore()
  1859. {
  1860. // Create store only when needed,
  1861. // to limit the use of cross-thread code in UpdateFromMain
  1862. if (FNeonLockStore == NULL)
  1863. {
  1864. FNeonLockStore = ne_lockstore_create();
  1865. ne_lockstore_register(FNeonLockStore, FNeonSession);
  1866. }
  1867. }
  1868. //---------------------------------------------------------------------------
  1869. void TWebDAVFileSystem::LockResult(void * UserData, const struct ne_lock * Lock,
  1870. const ne_uri * /*Uri*/, const ne_status * /*Status*/)
  1871. {
  1872. // Is NULL on failure (Status is not NULL then)
  1873. if (Lock != NULL)
  1874. {
  1875. RawByteString & LockToken = *static_cast<RawByteString *>(UserData);
  1876. LockToken = Lock->token;
  1877. }
  1878. }
  1879. //---------------------------------------------------------------------------
  1880. struct ne_lock * __fastcall TWebDAVFileSystem::FindLock(const RawByteString & Path)
  1881. {
  1882. ne_uri Uri = {0};
  1883. Uri.path = Path.c_str();
  1884. return ne_lockstore_findbyuri(FNeonLockStore, &Uri);
  1885. }
  1886. //---------------------------------------------------------------------------
  1887. void __fastcall TWebDAVFileSystem::DiscardLock(const RawByteString & Path)
  1888. {
  1889. TGuard Guard(FNeonLockStoreSection);
  1890. if (FNeonLockStore != NULL)
  1891. {
  1892. struct ne_lock * Lock = FindLock(Path);
  1893. if (Lock != NULL)
  1894. {
  1895. ne_lockstore_remove(FNeonLockStore, Lock);
  1896. }
  1897. }
  1898. }
  1899. //---------------------------------------------------------------------------
  1900. void __fastcall TWebDAVFileSystem::UnlockFile(const UnicodeString & FileName, const TRemoteFile * File)
  1901. {
  1902. ClearNeonError();
  1903. struct ne_lock * Lock = ne_lock_create();
  1904. try
  1905. {
  1906. RawByteString Path = PathToNeon(FilePath(File));
  1907. RawByteString LockToken;
  1908. struct ne_lock * Lock = NULL;
  1909. {
  1910. TGuard Guard(FNeonLockStoreSection);
  1911. if (FNeonLockStore != NULL)
  1912. {
  1913. Lock = FindLock(Path);
  1914. }
  1915. }
  1916. // we are not aware of the file being locked,
  1917. // though it can be locked from another (previous and already closed)
  1918. // session, so query the server.
  1919. if (Lock == NULL)
  1920. {
  1921. CheckStatus(ne_lock_discover(FNeonSession, Path.c_str(), LockResult, &LockToken));
  1922. }
  1923. if ((Lock == NULL) && (LockToken.IsEmpty()))
  1924. {
  1925. throw Exception(FMTLOAD(NOT_LOCKED, (FileName)));
  1926. }
  1927. else
  1928. {
  1929. struct ne_lock * Unlock;
  1930. if (Lock == NULL)
  1931. {
  1932. DebugAssert(!LockToken.IsEmpty());
  1933. Unlock = ne_lock_create();
  1934. Unlock->uri.path = ne_strdup(Path.c_str());
  1935. Unlock->token = ne_strdup(LockToken.c_str());
  1936. }
  1937. else
  1938. {
  1939. Unlock = Lock;
  1940. }
  1941. CheckStatus(ne_unlock(FNeonSession, Unlock));
  1942. DiscardLock(Path);
  1943. }
  1944. }
  1945. __finally
  1946. {
  1947. ne_lock_destroy(Lock);
  1948. }
  1949. }
  1950. //---------------------------------------------------------------------------
  1951. void __fastcall TWebDAVFileSystem::UpdateFromMain(TCustomFileSystem * AMainFileSystem)
  1952. {
  1953. TWebDAVFileSystem * MainFileSystem = dynamic_cast<TWebDAVFileSystem *>(AMainFileSystem);
  1954. if (DebugAlwaysTrue(MainFileSystem != NULL))
  1955. {
  1956. TGuard Guard(FNeonLockStoreSection);
  1957. TGuard MainGuard(MainFileSystem->FNeonLockStoreSection);
  1958. if (FNeonLockStore != NULL)
  1959. {
  1960. struct ne_lock * Lock;
  1961. while ((Lock = ne_lockstore_first(FNeonLockStore)) != NULL)
  1962. {
  1963. ne_lockstore_remove(FNeonLockStore, Lock);
  1964. }
  1965. }
  1966. if (MainFileSystem->FNeonLockStore != NULL)
  1967. {
  1968. RequireLockStore();
  1969. struct ne_lock * Lock = ne_lockstore_first(MainFileSystem->FNeonLockStore);
  1970. while (Lock != NULL)
  1971. {
  1972. ne_lockstore_add(FNeonLockStore, ne_lock_copy(Lock));
  1973. Lock = ne_lockstore_next(MainFileSystem->FNeonLockStore);
  1974. }
  1975. }
  1976. }
  1977. }
  1978. //------------------------------------------------------------------------------
  1979. void __fastcall TWebDAVFileSystem::ClearCaches()
  1980. {
  1981. // noop
  1982. }
  1983. //---------------------------------------------------------------------------