WebDAVFileSystem.cpp 72 KB

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