SessionInfo.cpp 47 KB

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