SessionInfo.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <stdio.h>
  5. #include <lmcons.h>
  6. #define SECURITY_WIN32
  7. #include <sspi.h>
  8. #include <secext.h>
  9. #include "Common.h"
  10. #include "SessionInfo.h"
  11. #include "Exceptions.h"
  12. #include "TextsCore.h"
  13. #include "CoreMain.h"
  14. #include "Script.h"
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. //---------------------------------------------------------------------------
  18. UnicodeString __fastcall DoXmlEscape(UnicodeString Str, bool NewLine)
  19. {
  20. for (int i = 1; i <= Str.Length(); i++)
  21. {
  22. const wchar_t * Repl = NULL;
  23. switch (Str[i])
  24. {
  25. case L'&':
  26. Repl = L"amp;";
  27. break;
  28. case L'>':
  29. Repl = L"gt;";
  30. break;
  31. case L'<':
  32. Repl = L"lt;";
  33. break;
  34. case L'"':
  35. Repl = L"quot;";
  36. break;
  37. case L'\n':
  38. if (NewLine)
  39. {
  40. Repl = L"#10;";
  41. }
  42. break;
  43. case L'\r':
  44. Str.Delete(i, 1);
  45. i--;
  46. break;
  47. }
  48. if (Repl != NULL)
  49. {
  50. Str[i] = L'&';
  51. Str.Insert(Repl, i + 1);
  52. i += wcslen(Repl);
  53. }
  54. }
  55. return Str;
  56. }
  57. //---------------------------------------------------------------------------
  58. UnicodeString __fastcall XmlEscape(UnicodeString Str)
  59. {
  60. return DoXmlEscape(Str, false);
  61. }
  62. //---------------------------------------------------------------------------
  63. UnicodeString __fastcall XmlAttributeEscape(UnicodeString Str)
  64. {
  65. return DoXmlEscape(Str, true);
  66. }
  67. //---------------------------------------------------------------------------
  68. //---------------------------------------------------------------------------
  69. #pragma warn -inl
  70. class TSessionActionRecord
  71. {
  72. public:
  73. __fastcall TSessionActionRecord(TActionLog * Log, TLogAction Action) :
  74. FLog(Log),
  75. FAction(Action),
  76. FState(Opened),
  77. FRecursive(false),
  78. FErrorMessages(NULL),
  79. FNames(new TStringList()),
  80. FValues(new TStringList()),
  81. FFileList(NULL),
  82. FFile(NULL)
  83. {
  84. FLog->AddPendingAction(this);
  85. }
  86. __fastcall ~TSessionActionRecord()
  87. {
  88. delete FErrorMessages;
  89. delete FNames;
  90. delete FValues;
  91. delete FFileList;
  92. delete FFile;
  93. }
  94. void __fastcall Restart()
  95. {
  96. FState = Opened;
  97. FRecursive = false;
  98. delete FErrorMessages;
  99. FErrorMessages = NULL;
  100. delete FFileList;
  101. FFileList = NULL;
  102. delete FFile;
  103. FFile = NULL;
  104. FNames->Clear();
  105. FValues->Clear();
  106. }
  107. bool __fastcall Record()
  108. {
  109. bool Result = (FState != Opened);
  110. if (Result)
  111. {
  112. if (FLog->FLogging && (FState != Cancelled))
  113. {
  114. const wchar_t * Name = ActionName();
  115. UnicodeString Attrs;
  116. if (FRecursive)
  117. {
  118. Attrs = L" recursive=\"true\"";
  119. }
  120. FLog->AddIndented(FORMAT(L"<%s%s>", (Name, Attrs)));
  121. for (int Index = 0; Index < FNames->Count; Index++)
  122. {
  123. UnicodeString Value = FValues->Strings[Index];
  124. if (Value.IsEmpty())
  125. {
  126. FLog->AddIndented(FORMAT(L" <%s />", (FNames->Strings[Index])));
  127. }
  128. else
  129. {
  130. FLog->AddIndented(FORMAT(L" <%s value=\"%s\" />",
  131. (FNames->Strings[Index], XmlAttributeEscape(Value))));
  132. }
  133. }
  134. if (FFileList != NULL)
  135. {
  136. FLog->AddIndented(L" <files>");
  137. for (int Index = 0; Index < FFileList->Count; Index++)
  138. {
  139. TRemoteFile * File = FFileList->Files[Index];
  140. RecordFile(L" ", File, true);
  141. }
  142. FLog->AddIndented(L" </files>");
  143. }
  144. if (FFile != NULL)
  145. {
  146. RecordFile(L" ", FFile, false);
  147. }
  148. if (FState == RolledBack)
  149. {
  150. if (FErrorMessages != NULL)
  151. {
  152. FLog->AddIndented(L" <result success=\"false\">");
  153. FLog->AddMessages(L" ", FErrorMessages);
  154. FLog->AddIndented(L" </result>");
  155. }
  156. else
  157. {
  158. FLog->AddIndented(L" <result success=\"false\" />");
  159. }
  160. }
  161. else
  162. {
  163. FLog->AddIndented(L" <result success=\"true\" />");
  164. }
  165. FLog->AddIndented(FORMAT(L"</%s>", (Name)));
  166. }
  167. delete this;
  168. }
  169. return Result;
  170. }
  171. void __fastcall Commit()
  172. {
  173. Close(Committed);
  174. }
  175. void __fastcall Rollback(Exception * E)
  176. {
  177. DebugAssert(FErrorMessages == NULL);
  178. FErrorMessages = ExceptionToMoreMessages(E);
  179. Close(RolledBack);
  180. }
  181. void __fastcall Cancel()
  182. {
  183. Close(Cancelled);
  184. }
  185. void __fastcall FileName(const UnicodeString & FileName)
  186. {
  187. Parameter(L"filename", FileName);
  188. }
  189. void __fastcall Destination(const UnicodeString & Destination)
  190. {
  191. Parameter(L"destination", Destination);
  192. }
  193. void __fastcall Rights(const TRights & Rights)
  194. {
  195. Parameter(L"permissions", Rights.Text);
  196. }
  197. void __fastcall Modification(const TDateTime & DateTime)
  198. {
  199. Parameter(L"modification", StandardTimestamp(DateTime));
  200. }
  201. void __fastcall Recursive()
  202. {
  203. FRecursive = true;
  204. }
  205. void __fastcall Command(const UnicodeString & Command)
  206. {
  207. Parameter(L"command", Command);
  208. }
  209. void __fastcall AddOutput(UnicodeString Output, bool StdError)
  210. {
  211. const wchar_t * Name = (StdError ? L"erroroutput" : L"output");
  212. int Index = FNames->IndexOf(Name);
  213. if (Index >= 0)
  214. {
  215. FValues->Strings[Index] = FValues->Strings[Index] + L"\r\n" + Output;
  216. }
  217. else
  218. {
  219. Parameter(Name, Output);
  220. }
  221. }
  222. void __fastcall ExitCode(int ExitCode)
  223. {
  224. Parameter(L"exitcode", IntToStr(ExitCode));
  225. }
  226. void __fastcall Checksum(const UnicodeString & Alg, const UnicodeString & Checksum)
  227. {
  228. Parameter(L"algorithm", Alg);
  229. Parameter(L"checksum", Checksum);
  230. }
  231. void __fastcall Cwd(const UnicodeString & Path)
  232. {
  233. Parameter(L"cwd", Path);
  234. }
  235. void __fastcall FileList(TRemoteFileList * FileList)
  236. {
  237. if (FFileList == NULL)
  238. {
  239. FFileList = new TRemoteFileList();
  240. }
  241. FileList->DuplicateTo(FFileList);
  242. }
  243. void __fastcall File(TRemoteFile * File)
  244. {
  245. if (FFile != NULL)
  246. {
  247. delete FFile;
  248. }
  249. FFile = File->Duplicate(true);
  250. }
  251. protected:
  252. enum TState { Opened, Committed, RolledBack, Cancelled };
  253. inline void __fastcall Close(TState State)
  254. {
  255. DebugAssert(FState == Opened);
  256. FState = State;
  257. FLog->RecordPendingActions();
  258. }
  259. const wchar_t * __fastcall ActionName()
  260. {
  261. switch (FAction)
  262. {
  263. case laUpload: return L"upload";
  264. case laDownload: return L"download";
  265. case laTouch: return L"touch";
  266. case laChmod: return L"chmod";
  267. case laMkdir: return L"mkdir";
  268. case laRm: return L"rm";
  269. case laMv: return L"mv";
  270. case laCp: return L"cp";
  271. case laCall: return L"call";
  272. case laLs: return L"ls";
  273. case laStat: return L"stat";
  274. case laChecksum: return L"checksum";
  275. case laCwd: return L"cwd";
  276. default: DebugFail(); return L"";
  277. }
  278. }
  279. void __fastcall Parameter(const UnicodeString & Name, const UnicodeString & Value = L"")
  280. {
  281. FNames->Add(Name);
  282. FValues->Add(Value);
  283. }
  284. void __fastcall RecordFile(const UnicodeString & Indent, TRemoteFile * File, bool IncludeFileName)
  285. {
  286. FLog->AddIndented(Indent + L"<file>");
  287. FLog->AddIndented(Indent + FORMAT(L" <filename value=\"%s\" />", (XmlAttributeEscape(File->FileName))));
  288. FLog->AddIndented(Indent + FORMAT(L" <type value=\"%s\" />", (XmlAttributeEscape(File->Type))));
  289. if (!File->IsDirectory)
  290. {
  291. FLog->AddIndented(Indent + FORMAT(L" <size value=\"%s\" />", (IntToStr(File->Size))));
  292. }
  293. if (File->ModificationFmt != mfNone)
  294. {
  295. FLog->AddIndented(Indent + FORMAT(L" <modification value=\"%s\" />", (StandardTimestamp(File->Modification))));
  296. }
  297. if (!File->Rights->Unknown)
  298. {
  299. FLog->AddIndented(Indent + FORMAT(L" <permissions value=\"%s\" />", (XmlAttributeEscape(File->Rights->Text))));
  300. }
  301. if (File->Owner.IsSet)
  302. {
  303. FLog->AddIndented(Indent + FORMAT(L" <owner value=\"%s\" />", (XmlAttributeEscape(File->Owner.DisplayText))));
  304. }
  305. if (File->Group.IsSet)
  306. {
  307. FLog->AddIndented(Indent + FORMAT(L" <group value=\"%s\" />", (XmlAttributeEscape(File->Group.DisplayText))));
  308. }
  309. FLog->AddIndented(Indent + L"</file>");
  310. }
  311. private:
  312. TActionLog * FLog;
  313. TLogAction FAction;
  314. TState FState;
  315. bool FRecursive;
  316. TStrings * FErrorMessages;
  317. TStrings * FNames;
  318. TStrings * FValues;
  319. TRemoteFileList * FFileList;
  320. TRemoteFile * FFile;
  321. };
  322. #pragma warn .inl
  323. //---------------------------------------------------------------------------
  324. //---------------------------------------------------------------------------
  325. __fastcall TSessionAction::TSessionAction(TActionLog * Log, TLogAction Action)
  326. {
  327. if (Log->FLogging)
  328. {
  329. FRecord = new TSessionActionRecord(Log, Action);
  330. }
  331. else
  332. {
  333. FRecord = NULL;
  334. }
  335. }
  336. //---------------------------------------------------------------------------
  337. __fastcall TSessionAction::~TSessionAction()
  338. {
  339. if (FRecord != NULL)
  340. {
  341. Commit();
  342. }
  343. }
  344. //---------------------------------------------------------------------------
  345. void __fastcall TSessionAction::Restart()
  346. {
  347. if (FRecord != NULL)
  348. {
  349. FRecord->Restart();
  350. }
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TSessionAction::Commit()
  354. {
  355. if (FRecord != NULL)
  356. {
  357. TSessionActionRecord * Record = FRecord;
  358. FRecord = NULL;
  359. Record->Commit();
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TSessionAction::Rollback(Exception * E)
  364. {
  365. if (FRecord != NULL)
  366. {
  367. TSessionActionRecord * Record = FRecord;
  368. FRecord = NULL;
  369. Record->Rollback(E);
  370. }
  371. }
  372. //---------------------------------------------------------------------------
  373. void __fastcall TSessionAction::Cancel()
  374. {
  375. if (FRecord != NULL)
  376. {
  377. TSessionActionRecord * Record = FRecord;
  378. FRecord = NULL;
  379. Record->Cancel();
  380. }
  381. }
  382. //---------------------------------------------------------------------------
  383. //---------------------------------------------------------------------------
  384. __fastcall TFileSessionAction::TFileSessionAction(TActionLog * Log, TLogAction Action) :
  385. TSessionAction(Log, Action)
  386. {
  387. }
  388. //---------------------------------------------------------------------------
  389. __fastcall TFileSessionAction::TFileSessionAction(
  390. TActionLog * Log, TLogAction Action, const UnicodeString & AFileName) :
  391. TSessionAction(Log, Action)
  392. {
  393. FileName(AFileName);
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TFileSessionAction::FileName(const UnicodeString & FileName)
  397. {
  398. if (FRecord != NULL)
  399. {
  400. FRecord->FileName(FileName);
  401. }
  402. }
  403. //---------------------------------------------------------------------------
  404. //---------------------------------------------------------------------------
  405. __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
  406. TActionLog * Log, TLogAction Action) :
  407. TFileSessionAction(Log, Action)
  408. {
  409. }
  410. //---------------------------------------------------------------------------
  411. __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
  412. TActionLog * Log, TLogAction Action, const UnicodeString & FileName) :
  413. TFileSessionAction(Log, Action, FileName)
  414. {
  415. }
  416. //---------------------------------------------------------------------------
  417. void __fastcall TFileLocationSessionAction::Destination(const UnicodeString & Destination)
  418. {
  419. if (FRecord != NULL)
  420. {
  421. FRecord->Destination(Destination);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. //---------------------------------------------------------------------------
  426. __fastcall TUploadSessionAction::TUploadSessionAction(TActionLog * Log) :
  427. TFileLocationSessionAction(Log, laUpload)
  428. {
  429. }
  430. //---------------------------------------------------------------------------
  431. //---------------------------------------------------------------------------
  432. __fastcall TDownloadSessionAction::TDownloadSessionAction(TActionLog * Log) :
  433. TFileLocationSessionAction(Log, laDownload)
  434. {
  435. }
  436. //---------------------------------------------------------------------------
  437. //---------------------------------------------------------------------------
  438. __fastcall TChmodSessionAction::TChmodSessionAction(
  439. TActionLog * Log, const UnicodeString & FileName) :
  440. TFileSessionAction(Log, laChmod, FileName)
  441. {
  442. }
  443. //---------------------------------------------------------------------------
  444. void __fastcall TChmodSessionAction::Recursive()
  445. {
  446. if (FRecord != NULL)
  447. {
  448. FRecord->Recursive();
  449. }
  450. }
  451. //---------------------------------------------------------------------------
  452. __fastcall TChmodSessionAction::TChmodSessionAction(
  453. TActionLog * Log, const UnicodeString & FileName, const TRights & ARights) :
  454. TFileSessionAction(Log, laChmod, FileName)
  455. {
  456. Rights(ARights);
  457. }
  458. //---------------------------------------------------------------------------
  459. void __fastcall TChmodSessionAction::Rights(const TRights & Rights)
  460. {
  461. if (FRecord != NULL)
  462. {
  463. FRecord->Rights(Rights);
  464. }
  465. }
  466. //---------------------------------------------------------------------------
  467. __fastcall TTouchSessionAction::TTouchSessionAction(
  468. TActionLog * Log, const UnicodeString & FileName, const TDateTime & Modification) :
  469. TFileSessionAction(Log, laTouch, FileName)
  470. {
  471. if (FRecord != NULL)
  472. {
  473. FRecord->Modification(Modification);
  474. }
  475. }
  476. //---------------------------------------------------------------------------
  477. __fastcall TMkdirSessionAction::TMkdirSessionAction(
  478. TActionLog * Log, const UnicodeString & FileName) :
  479. TFileSessionAction(Log, laMkdir, FileName)
  480. {
  481. }
  482. //---------------------------------------------------------------------------
  483. __fastcall TRmSessionAction::TRmSessionAction(
  484. TActionLog * Log, const UnicodeString & FileName) :
  485. TFileSessionAction(Log, laRm, FileName)
  486. {
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TRmSessionAction::Recursive()
  490. {
  491. if (FRecord != NULL)
  492. {
  493. FRecord->Recursive();
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. __fastcall TMvSessionAction::TMvSessionAction(TActionLog * Log,
  498. const UnicodeString & FileName, const UnicodeString & ADestination) :
  499. TFileLocationSessionAction(Log, laMv, FileName)
  500. {
  501. Destination(ADestination);
  502. }
  503. //---------------------------------------------------------------------------
  504. __fastcall TCpSessionAction::TCpSessionAction(TActionLog * Log,
  505. const UnicodeString & FileName, const UnicodeString & ADestination) :
  506. TFileLocationSessionAction(Log, laCp, FileName)
  507. {
  508. Destination(ADestination);
  509. }
  510. //---------------------------------------------------------------------------
  511. __fastcall TCallSessionAction::TCallSessionAction(TActionLog * Log,
  512. const UnicodeString & Command, const UnicodeString & Destination) :
  513. TSessionAction(Log, laCall)
  514. {
  515. if (FRecord != NULL)
  516. {
  517. FRecord->Command(Command);
  518. FRecord->Destination(Destination);
  519. }
  520. }
  521. //---------------------------------------------------------------------------
  522. void __fastcall TCallSessionAction::AddOutput(const UnicodeString & Output, bool StdError)
  523. {
  524. if (FRecord != NULL)
  525. {
  526. FRecord->AddOutput(Output, StdError);
  527. }
  528. }
  529. //---------------------------------------------------------------------------
  530. void __fastcall TCallSessionAction::ExitCode(int ExitCode)
  531. {
  532. if (FRecord != NULL)
  533. {
  534. FRecord->ExitCode(ExitCode);
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. __fastcall TLsSessionAction::TLsSessionAction(TActionLog * Log,
  539. const UnicodeString & Destination) :
  540. TSessionAction(Log, laLs)
  541. {
  542. if (FRecord != NULL)
  543. {
  544. FRecord->Destination(Destination);
  545. }
  546. }
  547. //---------------------------------------------------------------------------
  548. void __fastcall TLsSessionAction::FileList(TRemoteFileList * FileList)
  549. {
  550. if (FRecord != NULL)
  551. {
  552. FRecord->FileList(FileList);
  553. }
  554. }
  555. //---------------------------------------------------------------------------
  556. //---------------------------------------------------------------------------
  557. __fastcall TStatSessionAction::TStatSessionAction(TActionLog * Log, const UnicodeString & FileName) :
  558. TFileSessionAction(Log, laStat, FileName)
  559. {
  560. }
  561. //---------------------------------------------------------------------------
  562. void __fastcall TStatSessionAction::File(TRemoteFile * File)
  563. {
  564. if (FRecord != NULL)
  565. {
  566. FRecord->File(File);
  567. }
  568. }
  569. //---------------------------------------------------------------------------
  570. //---------------------------------------------------------------------------
  571. __fastcall TChecksumSessionAction::TChecksumSessionAction(TActionLog * Log) :
  572. TFileSessionAction(Log, laChecksum)
  573. {
  574. }
  575. //---------------------------------------------------------------------------
  576. void __fastcall TChecksumSessionAction::Checksum(const UnicodeString & Alg, const UnicodeString & Checksum)
  577. {
  578. if (FRecord != NULL)
  579. {
  580. FRecord->Checksum(Alg, Checksum);
  581. }
  582. }
  583. //---------------------------------------------------------------------------
  584. //---------------------------------------------------------------------------
  585. __fastcall TCwdSessionAction::TCwdSessionAction(TActionLog * Log, const UnicodeString & Path) :
  586. TSessionAction(Log, laCwd)
  587. {
  588. if (FRecord != NULL)
  589. {
  590. FRecord->Cwd(Path);
  591. }
  592. }
  593. //---------------------------------------------------------------------------
  594. //---------------------------------------------------------------------------
  595. TSessionInfo::TSessionInfo()
  596. {
  597. LoginTime = Now();
  598. }
  599. //---------------------------------------------------------------------------
  600. TFileSystemInfo::TFileSystemInfo()
  601. {
  602. memset(&IsCapable, false, sizeof(IsCapable));
  603. }
  604. //---------------------------------------------------------------------------
  605. //---------------------------------------------------------------------------
  606. FILE * __fastcall OpenFile(UnicodeString LogFileName, TDateTime Started, TSessionData * SessionData, bool Append, UnicodeString & NewFileName)
  607. {
  608. FILE * Result;
  609. UnicodeString ANewFileName = GetExpandedLogFileName(LogFileName, Started, SessionData);
  610. Result = _wfopen(ApiPath(ANewFileName).c_str(), (Append ? L"ab" : L"wb"));
  611. if (Result != NULL)
  612. {
  613. setvbuf(Result, NULL, _IONBF, BUFSIZ);
  614. NewFileName = ANewFileName;
  615. }
  616. else
  617. {
  618. throw ECRTExtException(FMTLOAD(LOG_OPENERROR, (ANewFileName)));
  619. }
  620. return Result;
  621. }
  622. //---------------------------------------------------------------------------
  623. //---------------------------------------------------------------------------
  624. const wchar_t *LogLineMarks = L"<>!.*";
  625. __fastcall TSessionLog::TSessionLog(TSessionUI* UI, TDateTime Started, TSessionData * SessionData,
  626. TConfiguration * Configuration)
  627. {
  628. FCriticalSection = new TCriticalSection;
  629. FLogging = false;
  630. FConfiguration = Configuration;
  631. FParent = NULL;
  632. FUI = UI;
  633. FSessionData = SessionData;
  634. FStarted = Started;
  635. FFile = NULL;
  636. FCurrentLogFileName = L"";
  637. FCurrentFileName = L"";
  638. FClosed = false;
  639. }
  640. //---------------------------------------------------------------------------
  641. __fastcall TSessionLog::~TSessionLog()
  642. {
  643. FClosed = true;
  644. ReflectSettings();
  645. DebugAssert(FFile == NULL);
  646. delete FCriticalSection;
  647. }
  648. //---------------------------------------------------------------------------
  649. void __fastcall TSessionLog::SetParent(TSessionLog * Parent, const UnicodeString & Name)
  650. {
  651. FParent = Parent;
  652. FName = Name;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TSessionLog::DoAddToParent(TLogLineType Type, const UnicodeString & Line)
  656. {
  657. DebugAssert(FParent != NULL);
  658. FParent->Add(Type, Line);
  659. }
  660. //---------------------------------------------------------------------------
  661. void __fastcall TSessionLog::DoAddToSelf(TLogLineType Type, const UnicodeString & Line)
  662. {
  663. if (LogToFile())
  664. {
  665. if (FFile == NULL)
  666. {
  667. OpenLogFile();
  668. }
  669. if (FFile != NULL)
  670. {
  671. UnicodeString Timestamp = FormatDateTime(L" yyyy-mm-dd hh:nn:ss.zzz ", Now());
  672. UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[Type]) + Timestamp + Line + L"\r\n");
  673. for (int Index = 1; Index <= UtfLine.Length(); Index++)
  674. {
  675. if ((UtfLine[Index] == '\n') &&
  676. ((Index == 1) || (UtfLine[Index - 1] != '\r')))
  677. {
  678. UtfLine.Insert('\r', Index);
  679. }
  680. }
  681. int Writting = UtfLine.Length();
  682. CheckSize(Writting);
  683. FCurrentFileSize += fwrite(UtfLine.c_str(), 1, Writting, (FILE *)FFile);
  684. }
  685. }
  686. }
  687. //---------------------------------------------------------------------------
  688. UnicodeString __fastcall TSessionLog::LogPartFileName(const UnicodeString & BaseName, int Index)
  689. {
  690. UnicodeString Result;
  691. if (Index >= 1)
  692. {
  693. Result = FORMAT(L"%s.%d", (BaseName, Index));
  694. }
  695. else
  696. {
  697. Result = BaseName;
  698. }
  699. return Result;
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall TSessionLog::CheckSize(__int64 Addition)
  703. {
  704. __int64 MaxSize = FConfiguration->LogMaxSize;
  705. if ((MaxSize > 0) && (FCurrentFileSize + Addition >= MaxSize))
  706. {
  707. // Before we close it
  708. UnicodeString BaseName = FCurrentFileName;
  709. CloseLogFile();
  710. FCurrentFileSize = 0;
  711. int Index = 0;
  712. while (FileExists(LogPartFileName(BaseName, Index + 1)))
  713. {
  714. Index++;
  715. }
  716. int MaxCount = FConfiguration->LogMaxCount;
  717. do
  718. {
  719. UnicodeString LogPart = LogPartFileName(BaseName, Index);
  720. if ((MaxCount > 0) && (Index >= MaxCount))
  721. {
  722. DeleteFileChecked(LogPart);
  723. }
  724. else
  725. {
  726. THROWOSIFFALSE(RenameFile(LogPart, LogPartFileName(BaseName, Index + 1)));
  727. }
  728. Index--;
  729. }
  730. while (Index >= 0);
  731. OpenLogFile();
  732. }
  733. }
  734. //---------------------------------------------------------------------------
  735. void __fastcall TSessionLog::DoAdd(TLogLineType Type, UnicodeString Line,
  736. void __fastcall (__closure *f)(TLogLineType Type, const UnicodeString & Line))
  737. {
  738. UnicodeString Prefix;
  739. if (!FName.IsEmpty())
  740. {
  741. Prefix = L"[" + FName + L"] ";
  742. }
  743. while (!Line.IsEmpty())
  744. {
  745. f(Type, Prefix + CutToChar(Line, L'\n', false));
  746. }
  747. }
  748. //---------------------------------------------------------------------------
  749. void __fastcall TSessionLog::Add(TLogLineType Type, const UnicodeString & Line)
  750. {
  751. DebugAssert(FConfiguration);
  752. if (Logging)
  753. {
  754. try
  755. {
  756. if (FParent != NULL)
  757. {
  758. DoAdd(Type, Line, DoAddToParent);
  759. }
  760. else
  761. {
  762. TGuard Guard(FCriticalSection);
  763. DoAdd(Type, Line, DoAddToSelf);
  764. }
  765. }
  766. catch (Exception &E)
  767. {
  768. // We failed logging, turn it off and notify user.
  769. FConfiguration->Logging = false;
  770. try
  771. {
  772. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  773. }
  774. catch (Exception &E)
  775. {
  776. AddException(&E);
  777. FUI->HandleExtendedException(&E);
  778. }
  779. }
  780. }
  781. }
  782. //---------------------------------------------------------------------------
  783. void __fastcall TSessionLog::AddException(Exception * E)
  784. {
  785. if (E != NULL)
  786. {
  787. Add(llException, ExceptionLogString(E));
  788. }
  789. }
  790. //---------------------------------------------------------------------------
  791. void __fastcall TSessionLog::ReflectSettings()
  792. {
  793. TGuard Guard(FCriticalSection);
  794. FLogging =
  795. !FClosed &&
  796. ((FParent != NULL) || FConfiguration->Logging);
  797. // if logging to file was turned off or log file was changed -> close current log file
  798. if ((FFile != NULL) &&
  799. (!LogToFile() || (FCurrentLogFileName != FConfiguration->LogFileName)))
  800. {
  801. CloseLogFile();
  802. }
  803. if (FFile != NULL)
  804. {
  805. CheckSize(0);
  806. }
  807. }
  808. //---------------------------------------------------------------------------
  809. bool __fastcall TSessionLog::LogToFile()
  810. {
  811. return Logging && FConfiguration->LogToFile && (FParent == NULL);
  812. }
  813. //---------------------------------------------------------------------------
  814. void __fastcall TSessionLog::CloseLogFile()
  815. {
  816. if (FFile != NULL)
  817. {
  818. fclose((FILE *)FFile);
  819. FFile = NULL;
  820. }
  821. FCurrentLogFileName = L"";
  822. FCurrentFileName = L"";
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TSessionLog::OpenLogFile()
  826. {
  827. try
  828. {
  829. DebugAssert(FFile == NULL);
  830. DebugAssert(FConfiguration != NULL);
  831. FCurrentLogFileName = FConfiguration->LogFileName;
  832. FFile = OpenFile(FCurrentLogFileName, FStarted, FSessionData, FConfiguration->LogFileAppend, FCurrentFileName);
  833. TSearchRec SearchRec;
  834. if (FileSearchRec(FCurrentFileName, SearchRec))
  835. {
  836. FCurrentFileSize = SearchRec.Size;
  837. }
  838. else
  839. {
  840. FCurrentFileSize = 0;
  841. }
  842. }
  843. catch (Exception & E)
  844. {
  845. // We failed logging to file, turn it off and notify user.
  846. FCurrentLogFileName = L"";
  847. FCurrentFileName = L"";
  848. FConfiguration->LogFileName = UnicodeString();
  849. try
  850. {
  851. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  852. }
  853. catch (Exception & E)
  854. {
  855. AddException(&E);
  856. // not to deadlock with TSessionLog::ReflectSettings invoked by FConfiguration->LogFileName setter above
  857. TUnguard Unguard(FCriticalSection);
  858. FUI->HandleExtendedException(&E);
  859. }
  860. }
  861. // in case we are appending and the existing log file is already too large
  862. if (FFile != NULL)
  863. {
  864. CheckSize(0);
  865. }
  866. }
  867. //---------------------------------------------------------------------------
  868. void __fastcall TSessionLog::AddSystemInfo()
  869. {
  870. AddStartupInfo(true);
  871. }
  872. //---------------------------------------------------------------------------
  873. void __fastcall TSessionLog::AddStartupInfo()
  874. {
  875. AddStartupInfo(false);
  876. }
  877. //---------------------------------------------------------------------------
  878. void __fastcall TSessionLog::AddStartupInfo(bool System)
  879. {
  880. TSessionData * Data = (System ? NULL : FSessionData);
  881. if (Logging)
  882. {
  883. if (FParent != NULL)
  884. {
  885. // do not add session info for secondary session
  886. // (this should better be handled in the TSecondaryTerminal)
  887. }
  888. else
  889. {
  890. DoAddStartupInfo(Data);
  891. }
  892. }
  893. }
  894. //---------------------------------------------------------------------------
  895. UnicodeString __fastcall TSessionLog::GetTlsVersionName(TTlsVersion TlsVersion)
  896. {
  897. switch (TlsVersion)
  898. {
  899. default:
  900. DebugFail();
  901. case ssl2:
  902. return "SSLv2";
  903. case ssl3:
  904. return "SSLv3";
  905. case tls10:
  906. return "TLSv1.0";
  907. case tls11:
  908. return "TLSv1.1";
  909. case tls12:
  910. return "TLSv1.2";
  911. }
  912. }
  913. //---------------------------------------------------------------------------
  914. UnicodeString __fastcall TSessionLog::LogSensitive(const UnicodeString & Str)
  915. {
  916. if (FConfiguration->LogSensitive && !Str.IsEmpty())
  917. {
  918. return Str;
  919. }
  920. else
  921. {
  922. return BooleanToEngStr(!Str.IsEmpty());
  923. }
  924. }
  925. //---------------------------------------------------------------------------
  926. UnicodeString __fastcall TSessionLog::GetCmdLineLog()
  927. {
  928. UnicodeString Result = CmdLine;
  929. if (!Configuration->LogSensitive)
  930. {
  931. TManagementScript Script(StoredSessions, false);
  932. Script.MaskPasswordInCommandLine(Result, true);
  933. }
  934. return Result;
  935. }
  936. //---------------------------------------------------------------------------
  937. template <typename T>
  938. UnicodeString __fastcall EnumName(T Value, UnicodeString Names)
  939. {
  940. int N = int(Value);
  941. do
  942. {
  943. UnicodeString Name = CutToChar(Names, L';', true);
  944. if (N == 0)
  945. {
  946. return Name;
  947. }
  948. N--;
  949. }
  950. while ((N >= 0) && !Names.IsEmpty());
  951. return L"(unknown)";
  952. }
  953. //---------------------------------------------------------------------------
  954. #define ADSTR(S) DoAdd(llMessage, S, DoAddToSelf);
  955. #define ADF(S, F) ADSTR(FORMAT(S, F));
  956. //---------------------------------------------------------------------------
  957. void __fastcall TSessionLog::DoAddStartupInfo(TSessionData * Data)
  958. {
  959. if (Data == NULL)
  960. {
  961. AddSeparator();
  962. UnicodeString OS = WindowsVersionLong();
  963. AddToList(OS, WindowsProductName(), L" - ");
  964. ADF(L"WinSCP %s (OS %s)", (FConfiguration->VersionStr, OS));
  965. THierarchicalStorage * Storage = FConfiguration->CreateConfigStorage();
  966. try
  967. {
  968. ADF(L"Configuration: %s", (Storage->Source));
  969. }
  970. __finally
  971. {
  972. delete Storage;
  973. }
  974. wchar_t UserName[UNLEN + 1];
  975. unsigned long UserNameSize = LENOF(UserName);
  976. if (DebugAlwaysFalse(!GetUserNameEx(NameSamCompatible, UserName, &UserNameSize)))
  977. {
  978. wcscpy(UserName, L"<Failed to retrieve username>");
  979. }
  980. UnicodeString LogStr;
  981. if (FConfiguration->LogProtocol <= 0)
  982. {
  983. LogStr = L"Normal";
  984. }
  985. else if (FConfiguration->LogProtocol == 1)
  986. {
  987. LogStr = L"Debug 1";
  988. }
  989. else if (FConfiguration->LogProtocol >= 2)
  990. {
  991. LogStr = L"Debug 2";
  992. }
  993. if (FConfiguration->LogSensitive)
  994. {
  995. LogStr += L", Logging passwords";
  996. }
  997. if (FConfiguration->LogMaxSize > 0)
  998. {
  999. LogStr += FORMAT(L", Rotating after: %s", (SizeToStr(FConfiguration->LogMaxSize)));
  1000. if (FConfiguration->LogMaxCount > 0)
  1001. {
  1002. LogStr += FORMAT(L", Keeping at most %d logs", (FConfiguration->LogMaxCount));
  1003. }
  1004. }
  1005. ADF(L"Log level: %s", (LogStr));
  1006. ADF(L"Local account: %s", (UserName));
  1007. ADF(L"Working directory: %s", (GetCurrentDir()));
  1008. ADF(L"Process ID: %d", (int(GetCurrentProcessId())));
  1009. ADF(L"Command-line: %s", (GetCmdLineLog()));
  1010. if (FConfiguration->LogProtocol >= 1)
  1011. {
  1012. AddOptions(GetGlobalOptions());
  1013. }
  1014. ADF(L"Time zone: %s", (GetTimeZoneLogString()));
  1015. if (!AdjustClockForDSTEnabled())
  1016. {
  1017. ADSTR(L"Warning: System option \"Automatically adjust clock for Daylight Saving Time\" is disabled, timestamps will not be represented correctly");
  1018. }
  1019. ADF(L"Login time: %s", (FormatDateTime(L"dddddd tt", Now())));
  1020. AddSeparator();
  1021. }
  1022. else
  1023. {
  1024. ADF(L"Session name: %s (%s)", (Data->SessionName, Data->Source));
  1025. ADF(L"Host name: %s (Port: %d)", (Data->HostNameExpanded, Data->PortNumber));
  1026. ADF(L"User name: %s (Password: %s, Key file: %s, Passphrase: %s)",
  1027. (Data->UserNameExpanded, LogSensitive(Data->Password),
  1028. LogSensitive(Data->PublicKeyFile), LogSensitive(Data->Passphrase)));
  1029. if (Data->UsesSsh)
  1030. {
  1031. ADF(L"Tunnel: %s", (BooleanToEngStr(Data->Tunnel)));
  1032. if (Data->Tunnel)
  1033. {
  1034. ADF(L"Tunnel: Host name: %s (Port: %d)", (Data->TunnelHostName, Data->TunnelPortNumber));
  1035. ADF(L"Tunnel: User name: %s (Password: %s, Key file: %s)",
  1036. (Data->TunnelUserName,
  1037. LogSensitive(Data->TunnelPassword),
  1038. LogSensitive(Data->TunnelPublicKeyFile)));
  1039. ADF(L"Tunnel: Local port number: %d", (Data->TunnelLocalPortNumber));
  1040. }
  1041. }
  1042. ADF(L"Transfer Protocol: %s", (Data->FSProtocolStr));
  1043. if (Data->UsesSsh || (Data->FSProtocol == fsFTP))
  1044. {
  1045. TPingType PingType;
  1046. int PingInterval;
  1047. if (Data->FSProtocol == fsFTP)
  1048. {
  1049. PingType = Data->FtpPingType;
  1050. PingInterval = Data->FtpPingInterval;
  1051. }
  1052. else
  1053. {
  1054. PingType = Data->PingType;
  1055. PingInterval = Data->PingInterval;
  1056. }
  1057. ADF(L"Ping type: %s, Ping interval: %d sec; Timeout: %d sec",
  1058. (EnumName(PingType, PingTypeNames), PingInterval, Data->Timeout));
  1059. ADF(L"Disable Nagle: %s",
  1060. (BooleanToEngStr(Data->TcpNoDelay)));
  1061. }
  1062. ADF(L"Proxy: %s",
  1063. ((Data->FtpProxyLogonType != 0) ?
  1064. FORMAT(L"FTP proxy %d", (Data->FtpProxyLogonType)) :
  1065. EnumName(Data->ProxyMethod, ProxyMethodNames)));
  1066. if ((Data->FtpProxyLogonType != 0) || (Data->ProxyMethod != ::pmNone))
  1067. {
  1068. ADF(L"HostName: %s (Port: %d); Username: %s; Passwd: %s",
  1069. (Data->ProxyHost, Data->ProxyPort,
  1070. Data->ProxyUsername, LogSensitive(Data->ProxyPassword)));
  1071. if (Data->ProxyMethod == pmTelnet)
  1072. {
  1073. ADF(L"Telnet command: %s", (Data->ProxyTelnetCommand));
  1074. }
  1075. if (Data->ProxyMethod == pmCmd)
  1076. {
  1077. ADF(L"Local command: %s", (Data->ProxyLocalCommand));
  1078. }
  1079. }
  1080. if (Data->UsesSsh || (Data->FSProtocol == fsFTP))
  1081. {
  1082. ADF(L"Send buffer: %d", (Data->SendBuf));
  1083. }
  1084. if (Data->UsesSsh)
  1085. {
  1086. ADF(L"SSH protocol version: %s; Compression: %s",
  1087. (Data->SshProtStr, BooleanToEngStr(Data->Compression)));
  1088. ADF(L"Bypass authentication: %s",
  1089. (BooleanToEngStr(Data->SshNoUserAuth)));
  1090. ADF(L"Try agent: %s; Agent forwarding: %s; TIS/CryptoCard: %s; KI: %s; GSSAPI: %s",
  1091. (BooleanToEngStr(Data->TryAgent), BooleanToEngStr(Data->AgentFwd), BooleanToEngStr(Data->AuthTIS),
  1092. BooleanToEngStr(Data->AuthKI), BooleanToEngStr(Data->AuthGSSAPI)));
  1093. if (Data->AuthGSSAPI)
  1094. {
  1095. ADF(L"GSSAPI: Forwarding: %s",
  1096. (BooleanToEngStr(Data->GSSAPIFwdTGT)));
  1097. }
  1098. ADF(L"Ciphers: %s; Ssh2DES: %s",
  1099. (Data->CipherList, BooleanToEngStr(Data->Ssh2DES)));
  1100. ADF(L"KEX: %s", (Data->KexList));
  1101. UnicodeString Bugs;
  1102. for (int Index = 0; Index < BUG_COUNT; Index++)
  1103. {
  1104. AddToList(Bugs, EnumName(Data->Bug[(TSshBug)Index], AutoSwitchNames), L",");
  1105. }
  1106. ADF(L"SSH Bugs: %s", (Bugs));
  1107. ADF(L"Simple channel: %s", (BooleanToEngStr(Data->SshSimple)));
  1108. ADF(L"Return code variable: %s; Lookup user groups: %s",
  1109. ((Data->DetectReturnVar ? UnicodeString(L"Autodetect") : Data->ReturnVar),
  1110. EnumName(Data->LookupUserGroups, AutoSwitchNames)));
  1111. ADF(L"Shell: %s", ((Data->Shell.IsEmpty()? UnicodeString(L"default") : Data->Shell)));
  1112. ADF(L"EOL: %s, UTF: %s", (EnumName(Data->EOLType, EOLTypeNames), EnumName(Data->NotUtf, NotAutoSwitchNames))); // NotUtf duplicated in FTP branch
  1113. ADF(L"Clear aliases: %s, Unset nat.vars: %s, Resolve symlinks: %s; Follow directory symlinks: %s",
  1114. (BooleanToEngStr(Data->ClearAliases), BooleanToEngStr(Data->UnsetNationalVars),
  1115. BooleanToEngStr(Data->ResolveSymlinks), BooleanToEngStr(Data->FollowDirectorySymlinks)));
  1116. ADF(L"LS: %s, Ign LS warn: %s, Scp1 Comp: %s",
  1117. (Data->ListingCommand,
  1118. BooleanToEngStr(Data->IgnoreLsWarnings),
  1119. BooleanToEngStr(Data->Scp1Compatibility)));
  1120. }
  1121. if ((Data->FSProtocol == fsSFTP) || (Data->FSProtocol == fsSFTPonly))
  1122. {
  1123. UnicodeString Bugs;
  1124. for (int Index = 0; Index < SFTP_BUG_COUNT; Index++)
  1125. {
  1126. AddToList(Bugs, EnumName(Data->SFTPBug[(TSftpBug)Index], AutoSwitchNames), L",");
  1127. }
  1128. ADF(L"SFTP Bugs: %s", (Bugs));
  1129. ADF(L"SFTP Server: %s", ((Data->SftpServer.IsEmpty()? UnicodeString(L"default") : Data->SftpServer)));
  1130. }
  1131. bool FtpsOn = false;
  1132. if (Data->FSProtocol == fsFTP)
  1133. {
  1134. ADF(L"UTF: %s", (EnumName(Data->NotUtf, NotAutoSwitchNames))); // duplicated in UsesSsh branch
  1135. UnicodeString Ftps;
  1136. switch (Data->Ftps)
  1137. {
  1138. case ftpsImplicit:
  1139. Ftps = L"Implicit TLS/SSL";
  1140. FtpsOn = true;
  1141. break;
  1142. case ftpsExplicitSsl:
  1143. Ftps = L"Explicit SSL/TLS";
  1144. FtpsOn = true;
  1145. break;
  1146. case ftpsExplicitTls:
  1147. Ftps = L"Explicit TLS/SSL";
  1148. FtpsOn = true;
  1149. break;
  1150. default:
  1151. DebugAssert(Data->Ftps == ftpsNone);
  1152. Ftps = L"None";
  1153. break;
  1154. }
  1155. // kind of hidden option, so do not reveal it unless it is set
  1156. if (Data->FtpTransferActiveImmediately != asAuto)
  1157. {
  1158. ADF(L"Transfer active immediately: %s", (EnumName(Data->FtpTransferActiveImmediately, AutoSwitchNames)));
  1159. }
  1160. ADF(L"FTPS: %s [Client certificate: %s]",
  1161. (Ftps, LogSensitive(Data->TlsCertificateFile)));
  1162. ADF(L"FTP: Passive: %s [Force IP: %s]; MLSD: %s [List all: %s]; HOST: %s",
  1163. (BooleanToEngStr(Data->FtpPasvMode),
  1164. EnumName(Data->FtpForcePasvIp, AutoSwitchNames),
  1165. EnumName(Data->FtpUseMlsd, AutoSwitchNames),
  1166. EnumName(Data->FtpListAll, AutoSwitchNames),
  1167. EnumName(Data->FtpHost, AutoSwitchNames)));
  1168. }
  1169. if (Data->FSProtocol == fsWebDAV)
  1170. {
  1171. FtpsOn = (Data->Ftps != ftpsNone);
  1172. ADF(L"HTTPS: %s [Client certificate: %s]",
  1173. (BooleanToEngStr(FtpsOn), LogSensitive(Data->TlsCertificateFile)));
  1174. }
  1175. if (Data->FSProtocol == fsS3)
  1176. {
  1177. FtpsOn = (Data->Ftps != ftpsNone);
  1178. ADF(L"HTTPS: %s", (BooleanToEngStr(FtpsOn)));
  1179. }
  1180. if (FtpsOn)
  1181. {
  1182. if (Data->FSProtocol == fsFTP)
  1183. {
  1184. ADF(L"Session reuse: %s", (BooleanToEngStr(Data->SslSessionReuse)));
  1185. }
  1186. ADF(L"TLS/SSL versions: %s-%s", (GetTlsVersionName(Data->MinTlsVersion), GetTlsVersionName(Data->MaxTlsVersion)));
  1187. }
  1188. ADF(L"Local directory: %s, Remote directory: %s, Update: %s, Cache: %s",
  1189. ((Data->LocalDirectory.IsEmpty() ? UnicodeString(L"default") : Data->LocalDirectory),
  1190. (Data->RemoteDirectory.IsEmpty() ? UnicodeString(L"home") : Data->RemoteDirectory),
  1191. BooleanToEngStr(Data->UpdateDirectories),
  1192. BooleanToEngStr(Data->CacheDirectories)));
  1193. ADF(L"Cache directory changes: %s, Permanent: %s",
  1194. (BooleanToEngStr(Data->CacheDirectoryChanges),
  1195. BooleanToEngStr(Data->PreserveDirectoryChanges)));
  1196. ADF(L"Recycle bin: Delete to: %s, Overwritten to: %s, Bin path: %s",
  1197. (BooleanToEngStr(Data->DeleteToRecycleBin),
  1198. BooleanToEngStr(Data->OverwrittenToRecycleBin),
  1199. Data->RecycleBinPath));
  1200. if (Data->TrimVMSVersions)
  1201. {
  1202. ADF(L"Trim VMS versions: %s",
  1203. (BooleanToEngStr(Data->TrimVMSVersions)));
  1204. }
  1205. UnicodeString TimeInfo;
  1206. if ((Data->FSProtocol == fsSFTP) || (Data->FSProtocol == fsSFTPonly) || (Data->FSProtocol == fsSCPonly) || (Data->FSProtocol == fsWebDAV))
  1207. {
  1208. AddToList(TimeInfo, FORMAT(L"DST mode: %s", (EnumName(Data->DSTMode, DSTModeNames))), L";");
  1209. }
  1210. if ((Data->FSProtocol == fsSCPonly) || (Data->FSProtocol == fsFTP))
  1211. {
  1212. int TimeDifferenceMin = TimeToMinutes(Data->TimeDifference);
  1213. AddToList(TimeInfo, FORMAT(L"Timezone offset: %dh %dm", ((TimeDifferenceMin / MinsPerHour), (TimeDifferenceMin % MinsPerHour))), L";");
  1214. }
  1215. ADSTR(TimeInfo);
  1216. if (Data->FSProtocol == fsWebDAV)
  1217. {
  1218. ADF(L"Compression: %s",
  1219. (BooleanToEngStr(Data->Compression)));
  1220. }
  1221. AddSeparator();
  1222. }
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TSessionLog::AddOption(const UnicodeString & LogStr)
  1226. {
  1227. ADSTR(LogStr);
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void __fastcall TSessionLog::AddOptions(TOptions * Options)
  1231. {
  1232. Options->LogOptions(AddOption);
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. #undef ADF
  1236. #undef ADSTR
  1237. //---------------------------------------------------------------------------
  1238. void __fastcall TSessionLog::AddSeparator()
  1239. {
  1240. Add(llMessage, L"--------------------------------------------------------------------------");
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. //---------------------------------------------------------------------------
  1244. __fastcall TActionLog::TActionLog(TSessionUI * UI, TDateTime Started, TSessionData * SessionData,
  1245. TConfiguration * Configuration)
  1246. {
  1247. DebugAssert(UI != NULL);
  1248. DebugAssert(SessionData != NULL);
  1249. Init(UI, Started, SessionData, Configuration);
  1250. }
  1251. //---------------------------------------------------------------------------
  1252. __fastcall TActionLog::TActionLog(TDateTime Started, TConfiguration * Configuration)
  1253. {
  1254. Init(NULL, Started, NULL, Configuration);
  1255. // not associated with session, so no need to waiting for anything
  1256. ReflectSettings();
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TActionLog::Init(TSessionUI * UI, TDateTime Started, TSessionData * SessionData,
  1260. TConfiguration * Configuration)
  1261. {
  1262. FCriticalSection = new TCriticalSection;
  1263. FConfiguration = Configuration;
  1264. FUI = UI;
  1265. FSessionData = SessionData;
  1266. FStarted = Started;
  1267. FFile = NULL;
  1268. FCurrentLogFileName = L"";
  1269. FCurrentFileName = L"";
  1270. FLogging = false;
  1271. FClosed = false;
  1272. FFailed = false;
  1273. FPendingActions = new TList();
  1274. FIndent = L" ";
  1275. FInGroup = false;
  1276. FEnabled = true;
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. __fastcall TActionLog::~TActionLog()
  1280. {
  1281. DebugAssert(FPendingActions->Count == 0);
  1282. delete FPendingActions;
  1283. FClosed = true;
  1284. ReflectSettings();
  1285. DebugAssert(FFile == NULL);
  1286. delete FCriticalSection;
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. void __fastcall TActionLog::Add(const UnicodeString & Line)
  1290. {
  1291. DebugAssert(FConfiguration);
  1292. if (FLogging)
  1293. {
  1294. TGuard Guard(FCriticalSection);
  1295. if (FFile == NULL)
  1296. {
  1297. OpenLogFile();
  1298. }
  1299. if (FFile != NULL)
  1300. {
  1301. try
  1302. {
  1303. UTF8String UtfLine = UTF8String(Line);
  1304. size_t Written =
  1305. fwrite(UtfLine.c_str(), 1, UtfLine.Length(), (FILE *)FFile);
  1306. if (Written != static_cast<size_t>(UtfLine.Length()))
  1307. {
  1308. throw ECRTExtException(L"");
  1309. }
  1310. #ifdef _DEBUG
  1311. #endif
  1312. Written =
  1313. fwrite("\n", 1, 1, (FILE *)FFile);
  1314. if (Written != 1)
  1315. {
  1316. throw ECRTExtException(L"");
  1317. }
  1318. }
  1319. catch (Exception &E)
  1320. {
  1321. FCriticalSection->Release();
  1322. // avoid endless loop when trying to close tags when closing log, when logging has failed
  1323. if (!FFailed)
  1324. {
  1325. FFailed = true;
  1326. // We failed logging, turn it off and notify user.
  1327. FConfiguration->LogActions = false;
  1328. if (FConfiguration->LogActionsRequired)
  1329. {
  1330. throw EFatal(&E, LoadStr(LOG_FATAL_ERROR));
  1331. }
  1332. else
  1333. {
  1334. try
  1335. {
  1336. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1337. }
  1338. catch (Exception &E)
  1339. {
  1340. if (FUI != NULL)
  1341. {
  1342. FUI->HandleExtendedException(&E);
  1343. }
  1344. }
  1345. }
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. void __fastcall TActionLog::AddIndented(const UnicodeString & Line)
  1353. {
  1354. Add(FIndent + Line);
  1355. }
  1356. //---------------------------------------------------------------------------
  1357. void __fastcall TActionLog::AddFailure(TStrings * Messages)
  1358. {
  1359. AddIndented(L"<failure>");
  1360. AddMessages(L" ", Messages);
  1361. AddIndented(L"</failure>");
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. void __fastcall TActionLog::AddFailure(Exception * E)
  1365. {
  1366. TStrings * Messages = ExceptionToMoreMessages(E);
  1367. if (Messages != NULL)
  1368. {
  1369. try
  1370. {
  1371. AddFailure(Messages);
  1372. }
  1373. __finally
  1374. {
  1375. delete Messages;
  1376. }
  1377. }
  1378. }
  1379. //---------------------------------------------------------------------------
  1380. void __fastcall TActionLog::AddMessages(UnicodeString Indent, TStrings * Messages)
  1381. {
  1382. for (int Index = 0; Index < Messages->Count; Index++)
  1383. {
  1384. AddIndented(
  1385. FORMAT(Indent + L"<message>%s</message>", (XmlEscape(Messages->Strings[Index]))));
  1386. }
  1387. }
  1388. //---------------------------------------------------------------------------
  1389. void __fastcall TActionLog::ReflectSettings()
  1390. {
  1391. TGuard Guard(FCriticalSection);
  1392. bool ALogging =
  1393. !FClosed && FConfiguration->LogActions && Enabled;
  1394. if (ALogging && !FLogging)
  1395. {
  1396. FLogging = true;
  1397. Add(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1398. UnicodeString SessionName =
  1399. (FSessionData != NULL) ? XmlAttributeEscape(FSessionData->SessionName) : UnicodeString(L"nosession");
  1400. Add(FORMAT(L"<session xmlns=\"http://winscp.net/schema/session/1.0\" name=\"%s\" start=\"%s\">",
  1401. (SessionName, StandardTimestamp())));
  1402. }
  1403. else if (!ALogging && FLogging)
  1404. {
  1405. if (FInGroup)
  1406. {
  1407. EndGroup();
  1408. }
  1409. // do not try to close the file, if it has not been opened, to avoid recursion
  1410. if (FFile != NULL)
  1411. {
  1412. Add(L"</session>");
  1413. }
  1414. CloseLogFile();
  1415. FLogging = false;
  1416. }
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void __fastcall TActionLog::CloseLogFile()
  1420. {
  1421. if (FFile != NULL)
  1422. {
  1423. fclose((FILE *)FFile);
  1424. FFile = NULL;
  1425. }
  1426. FCurrentLogFileName = L"";
  1427. FCurrentFileName = L"";
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. void __fastcall TActionLog::OpenLogFile()
  1431. {
  1432. try
  1433. {
  1434. DebugAssert(FFile == NULL);
  1435. DebugAssert(FConfiguration != NULL);
  1436. FCurrentLogFileName = FConfiguration->ActionsLogFileName;
  1437. FFile = OpenFile(FCurrentLogFileName, FStarted, FSessionData, false, FCurrentFileName);
  1438. }
  1439. catch (Exception & E)
  1440. {
  1441. // We failed logging to file, turn it off and notify user.
  1442. FCurrentLogFileName = L"";
  1443. FCurrentFileName = L"";
  1444. FConfiguration->LogActions = false;
  1445. if (FConfiguration->LogActionsRequired)
  1446. {
  1447. throw EFatal(&E, LoadStr(LOG_FATAL_ERROR));
  1448. }
  1449. else
  1450. {
  1451. try
  1452. {
  1453. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1454. }
  1455. catch (Exception & E)
  1456. {
  1457. if (FUI != NULL)
  1458. {
  1459. // not to deadlock with TSessionLog::ReflectSettings invoked by FConfiguration->LogFileName setter above
  1460. TUnguard Unguard(FCriticalSection);
  1461. FUI->HandleExtendedException(&E);
  1462. }
  1463. }
  1464. }
  1465. }
  1466. }
  1467. //---------------------------------------------------------------------------
  1468. void __fastcall TActionLog::AddPendingAction(TSessionActionRecord * Action)
  1469. {
  1470. FPendingActions->Add(Action);
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. void __fastcall TActionLog::RecordPendingActions()
  1474. {
  1475. while ((FPendingActions->Count > 0) &&
  1476. static_cast<TSessionActionRecord *>(FPendingActions->Items[0])->Record())
  1477. {
  1478. FPendingActions->Delete(0);
  1479. }
  1480. }
  1481. //---------------------------------------------------------------------------
  1482. void __fastcall TActionLog::BeginGroup(UnicodeString Name)
  1483. {
  1484. DebugAssert(!FInGroup);
  1485. FInGroup = true;
  1486. DebugAssert(FIndent == L" ");
  1487. AddIndented(FORMAT(L"<group name=\"%s\" start=\"%s\">",
  1488. (XmlAttributeEscape(Name), StandardTimestamp())));
  1489. FIndent = L" ";
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. void __fastcall TActionLog::EndGroup()
  1493. {
  1494. DebugAssert(FInGroup);
  1495. FInGroup = false;
  1496. DebugAssert(FIndent == L" ");
  1497. FIndent = L" ";
  1498. // this is called from ReflectSettings that in turn is called when logging fails,
  1499. // so do not try to close the group, if it has not been opened, to avoid recursion
  1500. if (FFile != NULL)
  1501. {
  1502. AddIndented(L"</group>");
  1503. }
  1504. }
  1505. //---------------------------------------------------------------------------
  1506. void __fastcall TActionLog::SetEnabled(bool value)
  1507. {
  1508. if (Enabled != value)
  1509. {
  1510. FEnabled = value;
  1511. ReflectSettings();
  1512. }
  1513. }