WebDAVFileSystem.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  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. // Keep is sync with S3
  809. int Filled =
  810. sscanf(LastModified, RFC1123_FORMAT, WeekDay, &Day, MonthStr, &Year, &Hour, &Min, &Sec);
  811. // we need at least a complete date
  812. if (Filled >= 4)
  813. {
  814. int Month = ParseShortEngMonthName(MonthStr);
  815. if (Month >= 1)
  816. {
  817. TDateTime Modification =
  818. EncodeDateVerbose((unsigned short)Year, (unsigned short)Month, (unsigned short)Day) +
  819. EncodeTimeVerbose((unsigned short)Hour, (unsigned short)Min, (unsigned short)Sec, 0);
  820. File->Modification = ConvertTimestampFromUTC(Modification);
  821. File->ModificationFmt = mfFull;
  822. }
  823. else
  824. {
  825. File->ModificationFmt = mfNone;
  826. }
  827. }
  828. else
  829. {
  830. File->ModificationFmt = mfNone;
  831. }
  832. }
  833. // optimization
  834. if (!Collection)
  835. {
  836. // This is possibly redundant code as all servers we know (see a comment above)
  837. // indicate the folder by trailing slash too
  838. const char * ResourceType = GetProp(Results, PROP_RESOURCE_TYPE);
  839. if (ResourceType != NULL)
  840. {
  841. // property has XML value
  842. UnicodeString AResourceType = ResourceType;
  843. // this is very poor parsing
  844. if (ContainsText(ResourceType, L"<DAV:collection"))
  845. {
  846. Collection = true;
  847. }
  848. }
  849. }
  850. File->Type = Collection ? FILETYPE_DIRECTORY : FILETYPE_DEFAULT;
  851. // this is MS extension (draft-hopmann-collection-props-00)
  852. const char * IsHidden = GetProp(Results, PROP_HIDDEN);
  853. if (IsHidden != NULL)
  854. {
  855. File->IsHidden = (StrToIntDef(IsHidden, 0) != 0);
  856. }
  857. const char * Owner = GetProp(Results, PROP_OWNER);
  858. if (Owner != NULL)
  859. {
  860. File->Owner.Name = Owner;
  861. }
  862. const char * DisplayName = GetProp(Results, PROP_DISPLAY_NAME);
  863. if (DisplayName != NULL)
  864. {
  865. File->DisplayName = StrFromNeon(DisplayName);
  866. }
  867. const UnicodeString RightsDelimiter(L", ");
  868. UnicodeString HumanRights;
  869. // Proprietary property of mod_dav
  870. // http://www.webdav.org/mod_dav/#imp
  871. const char * Executable = GetProp(Results, PROP_EXECUTABLE, MODDAV_PROP_NAMESPACE);
  872. if (Executable != NULL)
  873. {
  874. if (strcmp(Executable, "T") == NULL)
  875. {
  876. UnicodeString ExecutableRights;
  877. // The "gear" character is supported since Windows 8
  878. if (IsWin8())
  879. {
  880. ExecutableRights = L"\u2699";
  881. }
  882. else
  883. {
  884. ExecutableRights = LoadStr(EXECUTABLE);
  885. }
  886. AddToList(HumanRights, ExecutableRights, RightsDelimiter);
  887. }
  888. }
  889. struct ne_lock * Lock = static_cast<struct ne_lock *>(ne_propset_private(Results));
  890. if ((Lock != NULL) && (Lock->token != NULL))
  891. {
  892. UnicodeString Owner;
  893. if (Lock->owner != NULL)
  894. {
  895. Owner = StrFromNeon(Lock->owner).Trim();
  896. }
  897. UnicodeString LockRights;
  898. if (IsWin8())
  899. {
  900. // The "lock" character is supported since Windows 8
  901. LockRights = L"\uD83D\uDD12" + Owner;
  902. }
  903. else
  904. {
  905. LockRights = LoadStr(LOCKED);
  906. if (!Owner.IsEmpty())
  907. {
  908. LockRights = FORMAT(L"%s (%s)", (LockRights, Owner));
  909. }
  910. }
  911. AddToList(HumanRights, LockRights, RightsDelimiter);
  912. }
  913. File->HumanRights = HumanRights;
  914. }
  915. //---------------------------------------------------------------------------
  916. int __fastcall TWebDAVFileSystem::CustomReadFileInternal(const UnicodeString FileName,
  917. TRemoteFile *& File, TRemoteFile * ALinkedByFile)
  918. {
  919. std::unique_ptr<TRemoteFile> AFile(new TRemoteFile(ALinkedByFile));
  920. TReadFileData Data;
  921. Data.FileSystem = this;
  922. Data.File = AFile.get();
  923. Data.FileList = NULL;
  924. ClearNeonError();
  925. int Result =
  926. ne_simple_propfind(FNeonSession, PathToNeon(FileName), NE_DEPTH_ZERO, NULL,
  927. NeonPropsResult, &Data);
  928. if (Result == NE_OK)
  929. {
  930. File = AFile.release();
  931. }
  932. return Result;
  933. }
  934. //---------------------------------------------------------------------------
  935. void __fastcall TWebDAVFileSystem::CustomReadFile(UnicodeString FileName,
  936. TRemoteFile *& File, TRemoteFile * ALinkedByFile)
  937. {
  938. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  939. int NeonStatus = CustomReadFileInternal(FileName, File, ALinkedByFile);
  940. if (IsValidRedirect(NeonStatus, FileName))
  941. {
  942. NeonStatus = CustomReadFileInternal(FileName, File, ALinkedByFile);
  943. }
  944. CheckStatus(NeonStatus);
  945. }
  946. //---------------------------------------------------------------------------
  947. void __fastcall TWebDAVFileSystem::DeleteFile(const UnicodeString FileName,
  948. const TRemoteFile * File, int /*Params*/, TRmSessionAction & Action)
  949. {
  950. Action.Recursive();
  951. ClearNeonError();
  952. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  953. RawByteString Path = PathToNeon(FilePath(File));
  954. // WebDAV does not allow non-recursive delete:
  955. // RFC 4918, section 9.6.1:
  956. // "A client MUST NOT submit a Depth header with a DELETE on a collection with any value but infinity."
  957. // We should check that folder is empty when called with FLAGSET(Params, dfNoRecursive)
  958. CheckStatus(ne_delete(FNeonSession, Path.c_str()));
  959. // The lock is removed with the file, but if a file with the same name gets created,
  960. // we would try to use obsoleted lock token with it, what the server would reject
  961. // (mod_dav returns "412 Precondition Failed")
  962. DiscardLock(Path);
  963. }
  964. //---------------------------------------------------------------------------
  965. int __fastcall TWebDAVFileSystem::RenameFileInternal(const UnicodeString & FileName,
  966. const UnicodeString & NewName)
  967. {
  968. // 0 = no overwrite
  969. return ne_move(FNeonSession, 0, PathToNeon(FileName), PathToNeon(NewName));
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall TWebDAVFileSystem::RenameFile(const UnicodeString FileName, const TRemoteFile * /*File*/,
  973. const UnicodeString NewName)
  974. {
  975. ClearNeonError();
  976. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  977. UnicodeString Path = FileName;
  978. int NeonStatus = RenameFileInternal(Path, NewName);
  979. if (IsValidRedirect(NeonStatus, Path))
  980. {
  981. NeonStatus = RenameFileInternal(Path, NewName);
  982. }
  983. CheckStatus(NeonStatus);
  984. // See a comment in DeleteFile
  985. DiscardLock(PathToNeon(Path));
  986. }
  987. //---------------------------------------------------------------------------
  988. int __fastcall TWebDAVFileSystem::CopyFileInternal(const UnicodeString & FileName,
  989. const UnicodeString & NewName)
  990. {
  991. // 0 = no overwrite
  992. return ne_copy(FNeonSession, 0, NE_DEPTH_INFINITE, PathToNeon(FileName), PathToNeon(NewName));
  993. }
  994. //---------------------------------------------------------------------------
  995. void __fastcall TWebDAVFileSystem::CopyFile(const UnicodeString FileName, const TRemoteFile * /*File*/,
  996. const UnicodeString NewName)
  997. {
  998. ClearNeonError();
  999. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  1000. UnicodeString Path = FileName;
  1001. int NeonStatus = CopyFileInternal(Path, NewName);
  1002. if (IsValidRedirect(NeonStatus, Path))
  1003. {
  1004. NeonStatus = CopyFileInternal(Path, NewName);
  1005. }
  1006. CheckStatus(NeonStatus);
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. void __fastcall TWebDAVFileSystem::CreateDirectory(const UnicodeString & DirName, bool /*Encrypt*/)
  1010. {
  1011. ClearNeonError();
  1012. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  1013. CheckStatus(ne_mkcol(FNeonSession, PathToNeon(DirName)));
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. void __fastcall TWebDAVFileSystem::CreateLink(const UnicodeString FileName,
  1017. const UnicodeString PointTo, bool /*Symbolic*/)
  1018. {
  1019. DebugFail();
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TWebDAVFileSystem::ChangeFileProperties(const UnicodeString FileName,
  1023. const TRemoteFile * /*File*/, const TRemoteProperties * /*Properties*/,
  1024. TChmodSessionAction & /*Action*/)
  1025. {
  1026. DebugFail();
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. bool __fastcall TWebDAVFileSystem::LoadFilesProperties(TStrings * /*FileList*/)
  1030. {
  1031. DebugFail();
  1032. return false;
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. void __fastcall TWebDAVFileSystem::CalculateFilesChecksum(const UnicodeString & /*Alg*/,
  1036. TStrings * /*FileList*/, TStrings * /*Checksums*/,
  1037. TCalculatedChecksumEvent /*OnCalculatedChecksum*/)
  1038. {
  1039. DebugFail();
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void __fastcall TWebDAVFileSystem::ConfirmOverwrite(
  1043. const UnicodeString & SourceFullFileName, UnicodeString & TargetFileName,
  1044. TFileOperationProgressType * OperationProgress,
  1045. const TOverwriteFileParams * FileParams, const TCopyParamType * CopyParam,
  1046. int Params)
  1047. {
  1048. // all = "yes to newer"
  1049. int Answers = qaYes | qaNo | qaCancel | qaYesToAll | qaNoToAll | qaAll;
  1050. TQueryButtonAlias Aliases[3];
  1051. Aliases[0] = TQueryButtonAlias::CreateAllAsYesToNewerGrouppedWithYes();
  1052. Aliases[1] = TQueryButtonAlias::CreateYesToAllGrouppedWithYes();
  1053. Aliases[2] = TQueryButtonAlias::CreateNoToAllGrouppedWithNo();
  1054. TQueryParams QueryParams(qpNeverAskAgainCheck);
  1055. QueryParams.Aliases = Aliases;
  1056. QueryParams.AliasesCount = LENOF(Aliases);
  1057. unsigned int Answer;
  1058. {
  1059. TSuspendFileOperationProgress Suspend(OperationProgress);
  1060. Answer =
  1061. FTerminal->ConfirmFileOverwrite(
  1062. SourceFullFileName, TargetFileName, FileParams, Answers, &QueryParams,
  1063. ReverseOperationSide(OperationProgress->Side),
  1064. CopyParam, Params, OperationProgress);
  1065. }
  1066. switch (Answer)
  1067. {
  1068. case qaYes:
  1069. // noop
  1070. break;
  1071. case qaNo:
  1072. throw ESkipFile();
  1073. default:
  1074. DebugFail();
  1075. case qaCancel:
  1076. OperationProgress->SetCancelAtLeast(csCancel);
  1077. Abort();
  1078. break;
  1079. }
  1080. }
  1081. //---------------------------------------------------------------------------
  1082. void __fastcall TWebDAVFileSystem::CustomCommandOnFile(const UnicodeString FileName,
  1083. const TRemoteFile * /*File*/, UnicodeString Command, int /*Params*/, TCaptureOutputEvent /*OutputEvent*/)
  1084. {
  1085. DebugFail();
  1086. }
  1087. //---------------------------------------------------------------------------
  1088. void __fastcall TWebDAVFileSystem::AnyCommand(const UnicodeString Command,
  1089. TCaptureOutputEvent /*OutputEvent*/)
  1090. {
  1091. DebugFail();
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. TStrings * __fastcall TWebDAVFileSystem::GetFixedPaths()
  1095. {
  1096. return NULL;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void TWebDAVFileSystem::NeonQuotaResult(
  1100. void * UserData, const ne_uri * /*Uri*/, const ne_prop_result_set * Results)
  1101. {
  1102. TSpaceAvailable & SpaceAvailable = *static_cast<TSpaceAvailable *>(UserData);
  1103. const char * Value = GetProp(Results, PROP_QUOTA_AVAILABLE);
  1104. if (Value != NULL)
  1105. {
  1106. SpaceAvailable.UnusedBytesAvailableToUser = StrToInt64(StrFromNeon(Value));
  1107. const char * Value = GetProp(Results, PROP_QUOTA_USED);
  1108. if (Value != NULL)
  1109. {
  1110. SpaceAvailable.BytesAvailableToUser =
  1111. StrToInt64(StrFromNeon(Value)) + SpaceAvailable.UnusedBytesAvailableToUser;
  1112. }
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TWebDAVFileSystem::SpaceAvailable(const UnicodeString Path,
  1117. TSpaceAvailable & ASpaceAvailable)
  1118. {
  1119. // RFC4331: https://tools.ietf.org/html/rfc4331
  1120. // This is known to be supported by:
  1121. // OpenDrive: for a root drive only (and contrary to the spec, it sends the properties
  1122. // unconditionally, even when not explicitly requested)
  1123. // Server: Apache/2.2.17 (Fedora)
  1124. // X-Powered-By: PHP/5.5.7
  1125. // X-DAV-Powered-By: OpenDrive
  1126. // WWW-Authenticate: Basic realm="PHP WebDAV"
  1127. // IT Hit WebDAV Server:
  1128. // Server: Microsoft-HTTPAPI/1.0
  1129. // X-Engine: IT Hit WebDAV Server .Net v3.8.1877.0 (Evaluation License)
  1130. // Yandex disk:
  1131. // WWW-Authenticate: Basic realm="Yandex.Disk"
  1132. // Server: MochiWeb/1.0
  1133. // OneDrive:
  1134. // it sends the properties unconditionally, even when not explicitly requested
  1135. UnicodeString APath = DirectoryPath(Path);
  1136. ne_propname QuotaProps[3];
  1137. memset(QuotaProps, 0, sizeof(QuotaProps));
  1138. QuotaProps[0].nspace = DAV_PROP_NAMESPACE;
  1139. QuotaProps[0].name = PROP_QUOTA_AVAILABLE;
  1140. QuotaProps[1].nspace = DAV_PROP_NAMESPACE;
  1141. QuotaProps[1].name = PROP_QUOTA_USED;
  1142. QuotaProps[2].nspace = NULL;
  1143. QuotaProps[2].name = NULL;
  1144. TOperationVisualizer Visualizer(FTerminal->UseBusyCursor);
  1145. CheckStatus(
  1146. ne_simple_propfind(FNeonSession, PathToNeon(APath), NE_DEPTH_ZERO, QuotaProps,
  1147. NeonQuotaResult, &ASpaceAvailable));
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. void __fastcall TWebDAVFileSystem::CopyToRemote(TStrings * FilesToCopy,
  1151. const UnicodeString TargetDir, const TCopyParamType * CopyParam,
  1152. int Params, TFileOperationProgressType * OperationProgress,
  1153. TOnceDoneOperation & OnceDoneOperation)
  1154. {
  1155. Params &= ~cpAppend;
  1156. FTerminal->DoCopyToRemote(FilesToCopy, TargetDir, CopyParam, Params, OperationProgress, tfPreCreateDir, OnceDoneOperation);
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TWebDAVFileSystem::Source(
  1160. TLocalFileHandle & Handle, const UnicodeString & TargetDir, UnicodeString & DestFileName,
  1161. const TCopyParamType * CopyParam, int Params,
  1162. TFileOperationProgressType * OperationProgress, unsigned int /*Flags*/,
  1163. TUploadSessionAction & Action, bool & ChildError)
  1164. {
  1165. int FD = -1;
  1166. try
  1167. {
  1168. UnicodeString DestFullName = TargetDir + DestFileName;
  1169. TRemoteFile * RemoteFile = NULL;
  1170. try
  1171. {
  1172. TValueRestorer<TIgnoreAuthenticationFailure> IgnoreAuthenticationFailureRestorer(FIgnoreAuthenticationFailure);
  1173. FIgnoreAuthenticationFailure = iafWaiting;
  1174. // this should not throw
  1175. CustomReadFileInternal(DestFullName, RemoteFile, NULL);
  1176. }
  1177. catch (...)
  1178. {
  1179. if (!FTerminal->Active)
  1180. {
  1181. throw;
  1182. }
  1183. }
  1184. if (RemoteFile != NULL)
  1185. {
  1186. TOverwriteFileParams FileParams;
  1187. FileParams.SourceSize = Handle.Size;
  1188. FileParams.SourceTimestamp = Handle.Modification;
  1189. FileParams.DestSize = RemoteFile->Size;
  1190. FileParams.DestTimestamp = RemoteFile->Modification;
  1191. delete RemoteFile;
  1192. ConfirmOverwrite(Handle.FileName, DestFileName, OperationProgress,
  1193. &FileParams, CopyParam, Params);
  1194. }
  1195. DestFullName = TargetDir + DestFileName;
  1196. // only now, we know the final destination
  1197. // (not really true as we do not support changing file name on overwrite dialog)
  1198. Action.Destination(DestFullName);
  1199. FUploadMimeType = Configuration->GetFileMimeType(DestFileName);
  1200. FILE_OPERATION_LOOP_BEGIN
  1201. {
  1202. SetFilePointer(Handle.Handle, 0, NULL, FILE_BEGIN);
  1203. FD = _open_osfhandle((intptr_t)Handle.Handle, O_BINARY);
  1204. if (FD < 0)
  1205. {
  1206. throw ESkipFile();
  1207. }
  1208. TAutoFlag UploadingFlag(FUploading);
  1209. ClearNeonError();
  1210. CheckStatus(ne_put(FNeonSession, PathToNeon(DestFullName), FD));
  1211. }
  1212. FILE_OPERATION_LOOP_END(FMTLOAD(TRANSFER_ERROR, (Handle.FileName)));
  1213. if (CopyParam->PreserveTime)
  1214. {
  1215. FTerminal->LogEvent(FORMAT(L"Preserving timestamp [%s]",
  1216. (StandardTimestamp(Handle.Modification))));
  1217. TTouchSessionAction TouchAction(FTerminal->ActionLog, DestFullName, Handle.Modification);
  1218. try
  1219. {
  1220. TDateTime ModificationUTC = ConvertTimestampToUTC(Handle.Modification);
  1221. TFormatSettings FormatSettings = GetEngFormatSettings();
  1222. UnicodeString LastModified =
  1223. FormatDateTime(L"ddd, d mmm yyyy hh:nn:ss 'GMT'", ModificationUTC, FormatSettings);
  1224. UTF8String NeonLastModified(LastModified);
  1225. // second element is "NULL-terminating"
  1226. ne_proppatch_operation Operations[2];
  1227. memset(Operations, 0, sizeof(Operations));
  1228. ne_propname LastModifiedProp;
  1229. LastModifiedProp.nspace = DAV_PROP_NAMESPACE;
  1230. LastModifiedProp.name = PROP_LAST_MODIFIED;
  1231. Operations[0].name = &LastModifiedProp;
  1232. Operations[0].type = ne_propset;
  1233. Operations[0].value = NeonLastModified.c_str();
  1234. int Status = ne_proppatch(FNeonSession, PathToNeon(DestFullName), Operations);
  1235. if (Status == NE_ERROR)
  1236. {
  1237. FTerminal->LogEvent(FORMAT(L"Preserving timestamp failed, ignoring: %s",
  1238. (GetNeonError())));
  1239. // Ignore errors as major WebDAV servers (like IIS), do not support
  1240. // changing getlastmodified.
  1241. // The only server we found that supports this is TradeMicro SafeSync.
  1242. // But it announces itself as "Server: Apache",
  1243. // so it's not reliable to autodetect the support.
  1244. // Microsoft Office alegedly uses <Win32LastModifiedTime>
  1245. // https://sabre.io/dav/clients/msoffice/
  1246. // Carot DAV does that too. But we do not know what server does support this.
  1247. TouchAction.Cancel();
  1248. }
  1249. else
  1250. {
  1251. CheckStatus(Status);
  1252. }
  1253. }
  1254. catch (Exception & E)
  1255. {
  1256. TouchAction.Rollback(&E);
  1257. ChildError = true;
  1258. throw;
  1259. }
  1260. }
  1261. }
  1262. __finally
  1263. {
  1264. if (FD >= 0)
  1265. {
  1266. // _close calls CloseHandle internally (even doc states, we should not call CloseHandle),
  1267. // but it crashes code guard
  1268. _close(FD);
  1269. Handle.Dismiss();
  1270. }
  1271. }
  1272. }
  1273. //---------------------------------------------------------------------------
  1274. void __fastcall TWebDAVFileSystem::CopyToLocal(TStrings * FilesToCopy,
  1275. const UnicodeString TargetDir, const TCopyParamType * CopyParam,
  1276. int Params, TFileOperationProgressType * OperationProgress,
  1277. TOnceDoneOperation & OnceDoneOperation)
  1278. {
  1279. Params &= ~cpAppend;
  1280. FTerminal->DoCopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, OperationProgress, tfNone, OnceDoneOperation);
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. void TWebDAVFileSystem::NeonCreateRequest(
  1284. ne_request * Request, void * UserData, const char * /*Method*/, const char * /*Uri*/)
  1285. {
  1286. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1287. ne_set_request_private(Request, SESSION_FS_KEY, FileSystem);
  1288. ne_add_response_body_reader(Request, NeonBodyAccepter, NeonBodyReader, Request);
  1289. FileSystem->FNtlmAuthenticationFailed = false;
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. void TWebDAVFileSystem::NeonPreSend(
  1293. ne_request * Request, void * UserData, ne_buffer * Header)
  1294. {
  1295. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1296. FileSystem->FAuthorizationProtocol = L"";
  1297. UnicodeString HeaderBuf(StrFromNeon(AnsiString(Header->data, Header->used)));
  1298. const UnicodeString AuthorizationHeaderName(L"Authorization:");
  1299. int P = HeaderBuf.Pos(AuthorizationHeaderName);
  1300. if (P > 0)
  1301. {
  1302. P += AuthorizationHeaderName.Length();
  1303. int P2 = PosEx(L"\n", HeaderBuf, P);
  1304. if (DebugAlwaysTrue(P2 > 0))
  1305. {
  1306. UnicodeString AuthorizationHeader = HeaderBuf.SubString(P, P2 - P).Trim();
  1307. FileSystem->FAuthorizationProtocol = CutToChar(AuthorizationHeader, L' ', false);
  1308. FileSystem->FLastAuthorizationProtocol = FileSystem->FAuthorizationProtocol;
  1309. }
  1310. }
  1311. if (FileSystem->FDownloading)
  1312. {
  1313. // Needed by IIS server to make it download source code, not code output,
  1314. // and mainly to even allow downloading file with unregistered extensions.
  1315. // Without it files like .001 return 404 (Not found) HTTP code.
  1316. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wdv/e37a9543-9290-4843-8c04-66457c60fa0a
  1317. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wdvse/501879f9-3875-4d7a-ab88-3cecab440034
  1318. // http://lists.manyfish.co.uk/pipermail/neon/2012-April/000582.html
  1319. // It's also supported by Oracle server:
  1320. // https://docs.oracle.com/cd/E19146-01/821-1828/gczya/index.html
  1321. // We do not know yet of any server that fails when the header is used,
  1322. // so it's added unconditionally.
  1323. ne_buffer_zappend(Header, "Translate: f\r\n");
  1324. }
  1325. const UnicodeString ContentTypeHeaderPrefix(L"Content-Type: ");
  1326. if (FileSystem->FTerminal->Log->Logging)
  1327. {
  1328. const char * Buffer;
  1329. size_t Size;
  1330. if (ne_get_request_body_buffer(Request, &Buffer, &Size))
  1331. {
  1332. // all neon request types that use ne_add_request_header
  1333. // use XML content-type, so it's text-based
  1334. DebugAssert(ContainsStr(HeaderBuf, ContentTypeHeaderPrefix + NE_XML_MEDIA_TYPE));
  1335. FileSystem->FTerminal->Log->Add(llInput, UnicodeString(UTF8String(Buffer, Size)));
  1336. }
  1337. }
  1338. if (FileSystem->FUploading)
  1339. {
  1340. ne_set_request_body_provider_pre(Request,
  1341. FileSystem->NeonUploadBodyProvider, FileSystem);
  1342. if (!FileSystem->FUploadMimeType.IsEmpty())
  1343. {
  1344. UnicodeString ContentTypeHeader = ContentTypeHeaderPrefix + FileSystem->FUploadMimeType + L"\r\n";
  1345. ne_buffer_zappend(Header, AnsiString(ContentTypeHeader).c_str());
  1346. }
  1347. }
  1348. FileSystem->FResponse = L"";
  1349. }
  1350. //---------------------------------------------------------------------------
  1351. int TWebDAVFileSystem::NeonPostSend(ne_request * /*Req*/, void * UserData,
  1352. const ne_status * /*Status*/)
  1353. {
  1354. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1355. if (!FileSystem->FResponse.IsEmpty())
  1356. {
  1357. FileSystem->FTerminal->Log->Add(llOutput, FileSystem->FResponse);
  1358. }
  1359. return NE_OK;
  1360. }
  1361. //---------------------------------------------------------------------------
  1362. bool __fastcall TWebDAVFileSystem::IsNtlmAuthentication()
  1363. {
  1364. return
  1365. SameText(FAuthorizationProtocol, L"NTLM") ||
  1366. SameText(FAuthorizationProtocol, L"Negotiate");
  1367. }
  1368. //---------------------------------------------------------------------------
  1369. void __fastcall TWebDAVFileSystem::HttpAuthenticationFailed()
  1370. {
  1371. // NTLM/GSSAPI failed
  1372. if (IsNtlmAuthentication())
  1373. {
  1374. if (FNtlmAuthenticationFailed)
  1375. {
  1376. // Next time do not try Negotiate (NTLM/GSSAPI),
  1377. // otherwise we end up in an endless loop.
  1378. // If the server returns all other challenges in the response, removing the Negotiate
  1379. // protocol will itself ensure that other protocols are tried (we haven't seen this behaviour).
  1380. // IIS will return only Negotiate response if the request was Negotiate, so there's no fallback.
  1381. // We have to retry with a fresh request. That's what FAuthenticationRetry does.
  1382. FTerminal->LogEvent(FORMAT(L"%s challenge failed, will try different challenge", (FAuthorizationProtocol)));
  1383. ne_remove_server_auth(FNeonSession);
  1384. NeonAddAuthentiation(false);
  1385. FAuthenticationRetry = true;
  1386. }
  1387. else
  1388. {
  1389. // The first 401 is expected, the server is using it to send WWW-Authenticate header with data.
  1390. FNtlmAuthenticationFailed = true;
  1391. }
  1392. }
  1393. }
  1394. //---------------------------------------------------------------------------
  1395. void TWebDAVFileSystem::NeonPostHeaders(ne_request * /*Req*/, void * UserData, const ne_status * Status)
  1396. {
  1397. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1398. if (Status->code == HttpUnauthorized)
  1399. {
  1400. FileSystem->HttpAuthenticationFailed();
  1401. }
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. ssize_t TWebDAVFileSystem::NeonUploadBodyProvider(void * UserData, char * /*Buffer*/, size_t /*BufLen*/)
  1405. {
  1406. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1407. ssize_t Result;
  1408. if (FileSystem->CancelTransfer())
  1409. {
  1410. Result = -1;
  1411. }
  1412. else
  1413. {
  1414. Result = 1;
  1415. }
  1416. return Result;
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. static void __fastcall AddHeaderValueToList(UnicodeString & List, ne_request * Request, const char * Name)
  1420. {
  1421. const char * Value = ne_get_response_header(Request, Name);
  1422. if (Value != NULL)
  1423. {
  1424. AddToList(List, StrFromNeon(Value), L"; ");
  1425. }
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. int TWebDAVFileSystem::NeonBodyAccepter(void * UserData, ne_request * Request, const ne_status * Status)
  1429. {
  1430. DebugAssert(UserData == Request);
  1431. TWebDAVFileSystem * FileSystem =
  1432. static_cast<TWebDAVFileSystem *>(ne_get_request_private(Request, SESSION_FS_KEY));
  1433. bool AuthenticationFailureCode = (Status->code == HttpUnauthorized);
  1434. bool PasswordAuthenticationFailed = AuthenticationFailureCode && FileSystem->FAuthenticationRequested;
  1435. bool AuthenticationFailed = PasswordAuthenticationFailed || (AuthenticationFailureCode && FileSystem->IsNtlmAuthentication());
  1436. bool AuthenticationNeeded = AuthenticationFailureCode && !AuthenticationFailed;
  1437. if (FileSystem->FInitialHandshake)
  1438. {
  1439. UnicodeString Line;
  1440. if (AuthenticationNeeded)
  1441. {
  1442. Line = LoadStr(STATUS_AUTHENTICATE);
  1443. }
  1444. else if (AuthenticationFailed)
  1445. {
  1446. Line = LoadStr(FTP_ACCESS_DENIED);
  1447. }
  1448. else if (Status->klass == 2)
  1449. {
  1450. Line = LoadStr(STATUS_AUTHENTICATED);
  1451. }
  1452. if (!Line.IsEmpty())
  1453. {
  1454. FileSystem->FTerminal->Information(Line, true);
  1455. }
  1456. UnicodeString RemoteSystem;
  1457. // Used by IT Hit WebDAV Server:
  1458. // Server: Microsoft-HTTPAPI/1.0
  1459. // X-Engine: IT Hit WebDAV Server .Net v3.8.1877.0 (Evaluation License)
  1460. AddHeaderValueToList(RemoteSystem, Request, "X-Engine");
  1461. // Used by OpenDrive:
  1462. // Server: Apache/2.2.17 (Fedora)
  1463. // X-Powered-By: PHP/5.5.7
  1464. // X-DAV-Powered-By: OpenDrive
  1465. AddHeaderValueToList(RemoteSystem, Request, "X-DAV-Powered-By");
  1466. // Used by IIS:
  1467. // Server: Microsoft-IIS/8.5
  1468. AddHeaderValueToList(RemoteSystem, Request, "Server");
  1469. // Not really useful.
  1470. // Can be e.g. "PleskLin"
  1471. AddHeaderValueToList(RemoteSystem, Request, "X-Powered-By");
  1472. FileSystem->FFileSystemInfo.RemoteSystem = RemoteSystem;
  1473. }
  1474. // When we explicitly fail authentication of request
  1475. // with FIgnoreAuthenticationFailure flag (after it failed with password),
  1476. // neon resets its internal password store and tries the next request
  1477. // without calling our authentication hook first
  1478. // (note AuthenticationFailed vs. AuthenticationNeeded)
  1479. // what likely fails, but we do not want to reset out password
  1480. // (as it was not even tried yet for this request).
  1481. if (PasswordAuthenticationFailed)
  1482. {
  1483. if (FileSystem->FIgnoreAuthenticationFailure == iafNo)
  1484. {
  1485. FileSystem->FPassword = RawByteString();
  1486. }
  1487. else
  1488. {
  1489. FileSystem->FIgnoreAuthenticationFailure = iafPasswordFailed;
  1490. }
  1491. }
  1492. return ne_accept_2xx(UserData, Request, Status);
  1493. }
  1494. //---------------------------------------------------------------------------
  1495. bool __fastcall TWebDAVFileSystem::CancelTransfer()
  1496. {
  1497. bool Result = false;
  1498. if ((FUploading || FDownloading) &&
  1499. (FTerminal->OperationProgress != NULL) &&
  1500. (FTerminal->OperationProgress->Cancel != csContinue))
  1501. {
  1502. if (FTerminal->OperationProgress->ClearCancelFile())
  1503. {
  1504. FSkipped = true;
  1505. }
  1506. else
  1507. {
  1508. FCancelled = true;
  1509. }
  1510. Result = true;
  1511. }
  1512. return Result;
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. int TWebDAVFileSystem::NeonBodyReader(void * UserData, const char * Buf, size_t Len)
  1516. {
  1517. ne_request * Request = static_cast<ne_request *>(UserData);
  1518. TWebDAVFileSystem * FileSystem =
  1519. static_cast<TWebDAVFileSystem *>(ne_get_request_private(Request, SESSION_FS_KEY));
  1520. if (FileSystem->FTerminal->Log->Logging)
  1521. {
  1522. ne_content_type ContentType;
  1523. if (ne_get_content_type(Request, &ContentType) == 0)
  1524. {
  1525. // The main point of the content-type check was to exclude
  1526. // GET responses (with file contents).
  1527. // But this won't work when downloading text files that have text
  1528. // content type on their own, hence the additional not-downloading test.
  1529. if (!FileSystem->FDownloading &&
  1530. ((ne_strcasecmp(ContentType.type, "text") == 0) ||
  1531. media_type_is_xml(&ContentType)))
  1532. {
  1533. UnicodeString Content = UnicodeString(UTF8String(Buf, Len)).Trim();
  1534. FileSystem->FResponse += Content;
  1535. }
  1536. ne_free(ContentType.value);
  1537. }
  1538. }
  1539. int Result = FileSystem->CancelTransfer() ? 1 : 0;
  1540. return Result;
  1541. }
  1542. //---------------------------------------------------------------------------
  1543. void __fastcall TWebDAVFileSystem::Sink(
  1544. const UnicodeString & FileName, const TRemoteFile * File,
  1545. const UnicodeString & TargetDir, UnicodeString & DestFileName, int Attrs,
  1546. const TCopyParamType * CopyParam, int Params, TFileOperationProgressType * OperationProgress,
  1547. unsigned int /*Flags*/, TDownloadSessionAction & Action)
  1548. {
  1549. UnicodeString DestFullName = TargetDir + DestFileName;
  1550. if (FileExists(ApiPath(DestFullName)))
  1551. {
  1552. __int64 Size;
  1553. __int64 MTime;
  1554. FTerminal->OpenLocalFile(DestFullName, GENERIC_READ, NULL, NULL, NULL, &MTime, NULL, &Size);
  1555. TOverwriteFileParams FileParams;
  1556. FileParams.SourceSize = File->Size;
  1557. FileParams.SourceTimestamp = File->Modification;
  1558. FileParams.DestSize = Size;
  1559. FileParams.DestTimestamp = UnixToDateTime(MTime, FTerminal->SessionData->DSTMode);
  1560. ConfirmOverwrite(FileName, DestFileName, OperationProgress, &FileParams, CopyParam, Params);
  1561. }
  1562. UnicodeString ExpandedDestFullName = ExpandUNCFileName(DestFullName);
  1563. Action.Destination(ExpandedDestFullName);
  1564. FILE_OPERATION_LOOP_BEGIN
  1565. {
  1566. HANDLE LocalHandle;
  1567. if (!FTerminal->CreateLocalFile(DestFullName, OperationProgress, &LocalHandle, FLAGSET(Params, cpNoConfirmation)))
  1568. {
  1569. throw ESkipFile();
  1570. }
  1571. bool DeleteLocalFile = true;
  1572. int FD = -1;
  1573. try
  1574. {
  1575. FD = _open_osfhandle((intptr_t)LocalHandle, O_BINARY);
  1576. if (FD < 0)
  1577. {
  1578. throw ESkipFile();
  1579. }
  1580. TAutoFlag DownloadingFlag(FDownloading);
  1581. ClearNeonError();
  1582. CheckStatus(ne_get(FNeonSession, PathToNeon(FileName), FD));
  1583. DeleteLocalFile = false;
  1584. if (CopyParam->PreserveTime)
  1585. {
  1586. FTerminal->UpdateTargetTime(LocalHandle, File->Modification, FTerminal->SessionData->DSTMode);
  1587. }
  1588. }
  1589. __finally
  1590. {
  1591. if (FD >= 0)
  1592. {
  1593. // _close calls CloseHandle internally (even doc states, we should not call CloseHandle),
  1594. // but it crashes code guard
  1595. _close(FD);
  1596. }
  1597. else
  1598. {
  1599. CloseHandle(LocalHandle);
  1600. }
  1601. if (DeleteLocalFile)
  1602. {
  1603. FILE_OPERATION_LOOP_BEGIN
  1604. {
  1605. THROWOSIFFALSE(Sysutils::DeleteFile(ApiPath(DestFullName)));
  1606. }
  1607. FILE_OPERATION_LOOP_END(FMTLOAD(DELETE_LOCAL_FILE_ERROR, (DestFullName)));
  1608. }
  1609. }
  1610. }
  1611. FILE_OPERATION_LOOP_END(FMTLOAD(TRANSFER_ERROR, (FileName)));
  1612. FTerminal->UpdateTargetAttrs(DestFullName, File, CopyParam, Attrs);
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. // Similar to TS3FileSystem::VerifyCertificate
  1616. bool TWebDAVFileSystem::VerifyCertificate(TNeonCertificateData Data, bool Aux)
  1617. {
  1618. FSessionInfo.CertificateFingerprintSHA1 = Data.FingerprintSHA1;
  1619. FSessionInfo.CertificateFingerprintSHA256 = Data.FingerprintSHA256;
  1620. bool Result;
  1621. if (FTerminal->SessionData->FingerprintScan)
  1622. {
  1623. Result = false;
  1624. }
  1625. else
  1626. {
  1627. FTerminal->LogEvent(0, CertificateVerificationMessage(Data));
  1628. UnicodeString SiteKey = TSessionData::FormatSiteKey(FHostName, FPortNumber);
  1629. Result =
  1630. FTerminal->VerifyCertificate(
  1631. HttpsCertificateStorageKey, SiteKey, Data.FingerprintSHA1, Data.FingerprintSHA256, Data.Subject, Data.Failures);
  1632. if (Result)
  1633. {
  1634. FSessionInfo.CertificateVerifiedManually = true;
  1635. }
  1636. else
  1637. {
  1638. UnicodeString Message;
  1639. Result = NeonWindowsValidateCertificateWithMessage(Data, Message);
  1640. FTerminal->LogEvent(0, Message);
  1641. }
  1642. FSessionInfo.Certificate = CertificateSummary(Data, FHostName);
  1643. if (!Result)
  1644. {
  1645. if (FTerminal->ConfirmCertificate(FSessionInfo, Data.Failures, HttpsCertificateStorageKey, !Aux))
  1646. {
  1647. Result = true;
  1648. FSessionInfo.CertificateVerifiedManually = true;
  1649. }
  1650. }
  1651. if (Result && !Aux)
  1652. {
  1653. CollectTLSSessionInfo();
  1654. }
  1655. }
  1656. return Result;
  1657. }
  1658. //------------------------------------------------------------------------------
  1659. void __fastcall TWebDAVFileSystem::CollectTLSSessionInfo()
  1660. {
  1661. // See also TFTPFileSystem::Open().
  1662. // Have to cache the value as the connection (the neon HTTP session, not "our" session)
  1663. // can be closed at the time we need it in CollectUsage().
  1664. UnicodeString Message = NeonTlsSessionInfo(FNeonSession, FSessionInfo, FTlsVersionStr);
  1665. FTerminal->LogEvent(0, Message);
  1666. }
  1667. //------------------------------------------------------------------------------
  1668. // A neon-session callback to validate the SSL certificate when the CA
  1669. // is unknown (e.g. a self-signed cert), or there are other SSL
  1670. // certificate problems.
  1671. int TWebDAVFileSystem::DoNeonServerSSLCallback(void * UserData, int Failures, const ne_ssl_certificate * Certificate, bool Aux)
  1672. {
  1673. TNeonCertificateData Data;
  1674. RetrieveNeonCertificateData(Failures, Certificate, Data);
  1675. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1676. return FileSystem->VerifyCertificate(Data, Aux) ? NE_OK : NE_ERROR;
  1677. }
  1678. //------------------------------------------------------------------------------
  1679. int TWebDAVFileSystem::NeonServerSSLCallbackMain(void * UserData, int Failures, const ne_ssl_certificate * Certificate)
  1680. {
  1681. return DoNeonServerSSLCallback(UserData, Failures, Certificate, false);
  1682. }
  1683. //------------------------------------------------------------------------------
  1684. int TWebDAVFileSystem::NeonServerSSLCallbackAux(void * UserData, int Failures, const ne_ssl_certificate * Certificate)
  1685. {
  1686. return DoNeonServerSSLCallback(UserData, Failures, Certificate, true);
  1687. }
  1688. //------------------------------------------------------------------------------
  1689. void TWebDAVFileSystem::NeonProvideClientCert(void * UserData, ne_session * Sess,
  1690. const ne_ssl_dname * const * /*DNames*/, int /*DNCount*/)
  1691. {
  1692. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1693. FileSystem->FTerminal->LogEvent(LoadStr(NEED_CLIENT_CERTIFICATE));
  1694. X509 * Certificate;
  1695. EVP_PKEY * PrivateKey;
  1696. if (FileSystem->FTerminal->LoadTlsCertificate(Certificate, PrivateKey))
  1697. {
  1698. ne_ssl_client_cert * NeonCertificate = ne_ssl_clicert_create(Certificate, PrivateKey);
  1699. ne_ssl_set_clicert(Sess, NeonCertificate);
  1700. ne_ssl_clicert_free(NeonCertificate);
  1701. }
  1702. }
  1703. //------------------------------------------------------------------------------
  1704. int TWebDAVFileSystem::NeonRequestAuth(
  1705. void * UserData, const char * Realm, int Attempt, char * UserName, char * Password)
  1706. {
  1707. DebugUsedParam(Realm);
  1708. DebugUsedParam(Attempt);
  1709. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1710. TTerminal * Terminal = FileSystem->FTerminal;
  1711. TSessionData * SessionData = Terminal->SessionData;
  1712. bool Result = true;
  1713. // will ask for username only once
  1714. if (FileSystem->FUserName.IsEmpty())
  1715. {
  1716. if (!SessionData->UserName.IsEmpty())
  1717. {
  1718. FileSystem->FUserName = SessionData->UserNameExpanded;
  1719. }
  1720. else
  1721. {
  1722. Terminal->LogEvent(L"Username prompt");
  1723. if (!Terminal->PromptUser(SessionData, pkUserName, LoadStr(USERNAME_TITLE), L"",
  1724. LoadStr(USERNAME_PROMPT2), true, NE_ABUFSIZ, FileSystem->FUserName))
  1725. {
  1726. // note that we never get here actually
  1727. Result = false;
  1728. }
  1729. }
  1730. }
  1731. UnicodeString APassword;
  1732. if (Result)
  1733. {
  1734. // Some servers (Gallery2 on discontinued g2.pixi.me)
  1735. // return authentication error (401) on PROPFIND request for
  1736. // non-existing files.
  1737. // When we already tried password before, do not try anymore.
  1738. // When we did not try password before (possible only when
  1739. // server does not require authentication for any previous request,
  1740. // such as when read access is not authenticated), try it now,
  1741. // but use special flag for the try, because when it fails
  1742. // we still want to try password for future requests (such as PUT).
  1743. if (!FileSystem->FPassword.IsEmpty())
  1744. {
  1745. if (FileSystem->FIgnoreAuthenticationFailure == iafPasswordFailed)
  1746. {
  1747. // Fail PROPFIND /nonexising request...
  1748. Result = false;
  1749. }
  1750. else
  1751. {
  1752. APassword = Terminal->DecryptPassword(FileSystem->FPassword);
  1753. }
  1754. }
  1755. else
  1756. {
  1757. if (!SessionData->Password.IsEmpty() && !FileSystem->FStoredPasswordTried)
  1758. {
  1759. APassword = NormalizeString(SessionData->Password);
  1760. FileSystem->FStoredPasswordTried = true;
  1761. }
  1762. else
  1763. {
  1764. // Asking for password (or using configured password) the first time,
  1765. // and asking for password.
  1766. // Note that we never get false here actually
  1767. Terminal->LogEvent(L"Password prompt");
  1768. Result =
  1769. Terminal->PromptUser(
  1770. SessionData, pkPassword, LoadStr(PASSWORD_TITLE), L"",
  1771. LoadStr(PASSWORD_PROMPT), false, NE_ABUFSIZ, APassword);
  1772. }
  1773. if (Result)
  1774. {
  1775. // While neon remembers the password on its own,
  1776. // we need to keep a copy in case neon store gets reset by
  1777. // 401 response to PROPFIND /nonexisting on G2, see above.
  1778. // Possibly we can do this for G2 servers only.
  1779. FileSystem->FPassword = Terminal->EncryptPassword(APassword);
  1780. }
  1781. }
  1782. }
  1783. if (Result)
  1784. {
  1785. strncpy(UserName, StrToNeon(FileSystem->FUserName), NE_ABUFSIZ);
  1786. strncpy(Password, StrToNeon(APassword), NE_ABUFSIZ);
  1787. }
  1788. FileSystem->FAuthenticationRequested = true;
  1789. return Result ? 0 : -1;
  1790. }
  1791. //------------------------------------------------------------------------------
  1792. void TWebDAVFileSystem::NeonNotifier(void * UserData, ne_session_status Status, const ne_session_status_info * StatusInfo)
  1793. {
  1794. TWebDAVFileSystem * FileSystem = static_cast<TWebDAVFileSystem *>(UserData);
  1795. TFileOperationProgressType * OperationProgress = FileSystem->FTerminal->OperationProgress;
  1796. // We particularly have to filter out response to "put" request,
  1797. // handling that would reset the upload progress back to low number (response is small).
  1798. if (((FileSystem->FUploading && (Status == ne_status_sending)) ||
  1799. (FileSystem->FDownloading && (Status == ne_status_recving))) &&
  1800. DebugAlwaysTrue(OperationProgress != NULL))
  1801. {
  1802. __int64 Progress = StatusInfo->sr.progress;
  1803. __int64 Diff = Progress - OperationProgress->TransferredSize;
  1804. if (Diff > 0)
  1805. {
  1806. OperationProgress->ThrottleToCPSLimit(static_cast<unsigned long>(Diff));
  1807. }
  1808. __int64 Total = StatusInfo->sr.total;
  1809. // Total size unknown
  1810. if (Total < 0)
  1811. {
  1812. if (Diff >= 0)
  1813. {
  1814. OperationProgress->AddTransferred(Diff);
  1815. }
  1816. else
  1817. {
  1818. // Session total has been reset. A new stream started
  1819. OperationProgress->AddTransferred(Progress);
  1820. }
  1821. }
  1822. else
  1823. {
  1824. OperationProgress->SetTransferSize(Total);
  1825. OperationProgress->AddTransferred(Diff);
  1826. }
  1827. }
  1828. }
  1829. //------------------------------------------------------------------------------
  1830. void TWebDAVFileSystem::InitSslSession(ssl_st * Ssl, ne_session * /*Session*/)
  1831. {
  1832. SetupSsl(Ssl, FTerminal->SessionData->MinTlsVersion, FTerminal->SessionData->MaxTlsVersion);
  1833. }
  1834. //---------------------------------------------------------------------------
  1835. void __fastcall TWebDAVFileSystem::GetSupportedChecksumAlgs(TStrings * /*Algs*/)
  1836. {
  1837. // NOOP
  1838. }
  1839. //---------------------------------------------------------------------------
  1840. void __fastcall TWebDAVFileSystem::LockFile(const UnicodeString & /*FileName*/, const TRemoteFile * File)
  1841. {
  1842. ClearNeonError();
  1843. struct ne_lock * Lock = ne_lock_create();
  1844. try
  1845. {
  1846. Lock->uri.path = ne_strdup(PathToNeon(FilePath(File)));
  1847. Lock->depth = NE_DEPTH_INFINITE;
  1848. Lock->timeout = NE_TIMEOUT_INFINITE;
  1849. Lock->owner = ne_strdup(StrToNeon(FTerminal->UserName));
  1850. CheckStatus(ne_lock(FNeonSession, Lock));
  1851. {
  1852. TGuard Guard(FNeonLockStoreSection);
  1853. RequireLockStore();
  1854. ne_lockstore_add(FNeonLockStore, Lock);
  1855. }
  1856. // ownership passed
  1857. Lock = NULL;
  1858. }
  1859. __finally
  1860. {
  1861. if (Lock != NULL)
  1862. {
  1863. ne_lock_destroy(Lock);
  1864. }
  1865. }
  1866. }
  1867. //---------------------------------------------------------------------------
  1868. void __fastcall TWebDAVFileSystem::RequireLockStore()
  1869. {
  1870. // Create store only when needed,
  1871. // to limit the use of cross-thread code in UpdateFromMain
  1872. if (FNeonLockStore == NULL)
  1873. {
  1874. FNeonLockStore = ne_lockstore_create();
  1875. ne_lockstore_register(FNeonLockStore, FNeonSession);
  1876. }
  1877. }
  1878. //---------------------------------------------------------------------------
  1879. void TWebDAVFileSystem::LockResult(void * UserData, const struct ne_lock * Lock,
  1880. const ne_uri * /*Uri*/, const ne_status * /*Status*/)
  1881. {
  1882. // Is NULL on failure (Status is not NULL then)
  1883. if (Lock != NULL)
  1884. {
  1885. RawByteString & LockToken = *static_cast<RawByteString *>(UserData);
  1886. LockToken = Lock->token;
  1887. }
  1888. }
  1889. //---------------------------------------------------------------------------
  1890. struct ne_lock * __fastcall TWebDAVFileSystem::FindLock(const RawByteString & Path)
  1891. {
  1892. ne_uri Uri = {0};
  1893. Uri.path = Path.c_str();
  1894. return ne_lockstore_findbyuri(FNeonLockStore, &Uri);
  1895. }
  1896. //---------------------------------------------------------------------------
  1897. void __fastcall TWebDAVFileSystem::DiscardLock(const RawByteString & Path)
  1898. {
  1899. TGuard Guard(FNeonLockStoreSection);
  1900. if (FNeonLockStore != NULL)
  1901. {
  1902. struct ne_lock * Lock = FindLock(Path);
  1903. if (Lock != NULL)
  1904. {
  1905. ne_lockstore_remove(FNeonLockStore, Lock);
  1906. }
  1907. }
  1908. }
  1909. //---------------------------------------------------------------------------
  1910. void __fastcall TWebDAVFileSystem::UnlockFile(const UnicodeString & FileName, const TRemoteFile * File)
  1911. {
  1912. ClearNeonError();
  1913. struct ne_lock * Lock = ne_lock_create();
  1914. try
  1915. {
  1916. RawByteString Path = PathToNeon(FilePath(File));
  1917. RawByteString LockToken;
  1918. struct ne_lock * Lock = NULL;
  1919. {
  1920. TGuard Guard(FNeonLockStoreSection);
  1921. if (FNeonLockStore != NULL)
  1922. {
  1923. Lock = FindLock(Path);
  1924. }
  1925. }
  1926. // we are not aware of the file being locked,
  1927. // though it can be locked from another (previous and already closed)
  1928. // session, so query the server.
  1929. if (Lock == NULL)
  1930. {
  1931. CheckStatus(ne_lock_discover(FNeonSession, Path.c_str(), LockResult, &LockToken));
  1932. }
  1933. if ((Lock == NULL) && (LockToken.IsEmpty()))
  1934. {
  1935. throw Exception(FMTLOAD(NOT_LOCKED, (FileName)));
  1936. }
  1937. else
  1938. {
  1939. struct ne_lock * Unlock;
  1940. if (Lock == NULL)
  1941. {
  1942. DebugAssert(!LockToken.IsEmpty());
  1943. Unlock = ne_lock_create();
  1944. Unlock->uri.path = ne_strdup(Path.c_str());
  1945. Unlock->token = ne_strdup(LockToken.c_str());
  1946. }
  1947. else
  1948. {
  1949. Unlock = Lock;
  1950. }
  1951. CheckStatus(ne_unlock(FNeonSession, Unlock));
  1952. DiscardLock(Path);
  1953. }
  1954. }
  1955. __finally
  1956. {
  1957. ne_lock_destroy(Lock);
  1958. }
  1959. }
  1960. //---------------------------------------------------------------------------
  1961. void __fastcall TWebDAVFileSystem::UpdateFromMain(TCustomFileSystem * AMainFileSystem)
  1962. {
  1963. TWebDAVFileSystem * MainFileSystem = dynamic_cast<TWebDAVFileSystem *>(AMainFileSystem);
  1964. if (DebugAlwaysTrue(MainFileSystem != NULL))
  1965. {
  1966. TGuard Guard(FNeonLockStoreSection);
  1967. TGuard MainGuard(MainFileSystem->FNeonLockStoreSection);
  1968. if (FNeonLockStore != NULL)
  1969. {
  1970. struct ne_lock * Lock;
  1971. while ((Lock = ne_lockstore_first(FNeonLockStore)) != NULL)
  1972. {
  1973. ne_lockstore_remove(FNeonLockStore, Lock);
  1974. }
  1975. }
  1976. if (MainFileSystem->FNeonLockStore != NULL)
  1977. {
  1978. RequireLockStore();
  1979. struct ne_lock * Lock = ne_lockstore_first(MainFileSystem->FNeonLockStore);
  1980. while (Lock != NULL)
  1981. {
  1982. ne_lockstore_add(FNeonLockStore, ne_lock_copy(Lock));
  1983. Lock = ne_lockstore_next(MainFileSystem->FNeonLockStore);
  1984. }
  1985. }
  1986. }
  1987. }
  1988. //------------------------------------------------------------------------------
  1989. void __fastcall TWebDAVFileSystem::ClearCaches()
  1990. {
  1991. // noop
  1992. }
  1993. //---------------------------------------------------------------------------