SessionInfo.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. try
  1304. {
  1305. TGuard Guard(FCriticalSection);
  1306. if (FFile == NULL)
  1307. {
  1308. OpenLogFile();
  1309. }
  1310. if (FFile != NULL)
  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. }
  1317. catch (Exception &E)
  1318. {
  1319. // We failed logging, turn it off and notify user.
  1320. FConfiguration->LogActions = false;
  1321. try
  1322. {
  1323. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1324. }
  1325. catch (Exception &E)
  1326. {
  1327. if (FUI != NULL)
  1328. {
  1329. FUI->HandleExtendedException(&E);
  1330. }
  1331. }
  1332. }
  1333. }
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TActionLog::AddIndented(const UnicodeString & Line)
  1337. {
  1338. Add(FIndent + Line);
  1339. }
  1340. //---------------------------------------------------------------------------
  1341. void __fastcall TActionLog::AddFailure(TStrings * Messages)
  1342. {
  1343. AddIndented(L"<failure>");
  1344. AddMessages(L" ", Messages);
  1345. AddIndented(L"</failure>");
  1346. }
  1347. //---------------------------------------------------------------------------
  1348. void __fastcall TActionLog::AddFailure(Exception * E)
  1349. {
  1350. TStrings * Messages = ExceptionToMoreMessages(E);
  1351. if (Messages != NULL)
  1352. {
  1353. try
  1354. {
  1355. AddFailure(Messages);
  1356. }
  1357. __finally
  1358. {
  1359. delete Messages;
  1360. }
  1361. }
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. void __fastcall TActionLog::AddMessages(UnicodeString Indent, TStrings * Messages)
  1365. {
  1366. for (int Index = 0; Index < Messages->Count; Index++)
  1367. {
  1368. AddIndented(
  1369. FORMAT(Indent + L"<message>%s</message>", (XmlEscape(Messages->Strings[Index]))));
  1370. }
  1371. }
  1372. //---------------------------------------------------------------------------
  1373. void __fastcall TActionLog::ReflectSettings()
  1374. {
  1375. TGuard Guard(FCriticalSection);
  1376. bool ALogging =
  1377. !FClosed && FConfiguration->LogActions && Enabled;
  1378. if (ALogging && !FLogging)
  1379. {
  1380. FLogging = true;
  1381. Add(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1382. UnicodeString SessionName =
  1383. (FSessionData != NULL) ? XmlAttributeEscape(FSessionData->SessionName) : UnicodeString(L"nosession");
  1384. Add(FORMAT(L"<session xmlns=\"http://winscp.net/schema/session/1.0\" name=\"%s\" start=\"%s\">",
  1385. (SessionName, StandardTimestamp())));
  1386. }
  1387. else if (!ALogging && FLogging)
  1388. {
  1389. if (FInGroup)
  1390. {
  1391. EndGroup();
  1392. }
  1393. // do not try to close the file, if it has not been opened, to avoid recursion
  1394. if (FFile != NULL)
  1395. {
  1396. Add(L"</session>");
  1397. }
  1398. CloseLogFile();
  1399. FLogging = false;
  1400. }
  1401. }
  1402. //---------------------------------------------------------------------------
  1403. void __fastcall TActionLog::CloseLogFile()
  1404. {
  1405. if (FFile != NULL)
  1406. {
  1407. fclose((FILE *)FFile);
  1408. FFile = NULL;
  1409. }
  1410. FCurrentLogFileName = L"";
  1411. FCurrentFileName = L"";
  1412. }
  1413. //---------------------------------------------------------------------------
  1414. void __fastcall TActionLog::OpenLogFile()
  1415. {
  1416. try
  1417. {
  1418. DebugAssert(FFile == NULL);
  1419. DebugAssert(FConfiguration != NULL);
  1420. FCurrentLogFileName = FConfiguration->ActionsLogFileName;
  1421. FFile = OpenFile(FCurrentLogFileName, FSessionData, false, FCurrentFileName);
  1422. }
  1423. catch (Exception & E)
  1424. {
  1425. // We failed logging to file, turn it off and notify user.
  1426. FCurrentLogFileName = L"";
  1427. FCurrentFileName = L"";
  1428. FConfiguration->LogActions = false;
  1429. try
  1430. {
  1431. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1432. }
  1433. catch (Exception & E)
  1434. {
  1435. if (FUI != NULL)
  1436. {
  1437. FUI->HandleExtendedException(&E);
  1438. }
  1439. }
  1440. }
  1441. }
  1442. //---------------------------------------------------------------------------
  1443. void __fastcall TActionLog::AddPendingAction(TSessionActionRecord * Action)
  1444. {
  1445. FPendingActions->Add(Action);
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. void __fastcall TActionLog::RecordPendingActions()
  1449. {
  1450. while ((FPendingActions->Count > 0) &&
  1451. static_cast<TSessionActionRecord *>(FPendingActions->Items[0])->Record())
  1452. {
  1453. FPendingActions->Delete(0);
  1454. }
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void __fastcall TActionLog::BeginGroup(UnicodeString Name)
  1458. {
  1459. DebugAssert(!FInGroup);
  1460. FInGroup = true;
  1461. DebugAssert(FIndent == L" ");
  1462. AddIndented(FORMAT(L"<group name=\"%s\" start=\"%s\">",
  1463. (XmlAttributeEscape(Name), StandardTimestamp())));
  1464. FIndent = L" ";
  1465. }
  1466. //---------------------------------------------------------------------------
  1467. void __fastcall TActionLog::EndGroup()
  1468. {
  1469. DebugAssert(FInGroup);
  1470. FInGroup = false;
  1471. DebugAssert(FIndent == L" ");
  1472. FIndent = L" ";
  1473. // this is called from ReflectSettings that in turn is called when logging fails,
  1474. // so do not try to close the group, if it has not been opened, to avoid recursion
  1475. if (FFile != NULL)
  1476. {
  1477. AddIndented(L"</group>");
  1478. }
  1479. }
  1480. //---------------------------------------------------------------------------
  1481. void __fastcall TActionLog::SetEnabled(bool value)
  1482. {
  1483. if (Enabled != value)
  1484. {
  1485. FEnabled = value;
  1486. ReflectSettings();
  1487. }
  1488. }