SessionInfo.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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. // not to deadlock with TSessionLog::ReflectSettings invoked by FConfiguration->LogFileName setter above
  827. TUnguard Unguard(FCriticalSection);
  828. FUI->HandleExtendedException(&E);
  829. }
  830. }
  831. StateChange();
  832. }
  833. //---------------------------------------------------------------------------
  834. void __fastcall TSessionLog::StateChange()
  835. {
  836. if (FOnStateChange != NULL)
  837. {
  838. FOnStateChange(this);
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TSessionLog::DeleteUnnecessary()
  843. {
  844. BeginUpdate();
  845. try
  846. {
  847. if (!Logging || (FParent != NULL))
  848. {
  849. Clear();
  850. }
  851. else
  852. {
  853. while (!FConfiguration->LogWindowComplete && (Count > FConfiguration->LogWindowLines))
  854. {
  855. Delete(0);
  856. FTopIndex++;
  857. }
  858. }
  859. }
  860. __finally
  861. {
  862. EndUpdate();
  863. }
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TSessionLog::AddSystemInfo()
  867. {
  868. AddStartupInfo(true);
  869. }
  870. //---------------------------------------------------------------------------
  871. void __fastcall TSessionLog::AddStartupInfo()
  872. {
  873. AddStartupInfo(false);
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TSessionLog::AddStartupInfo(bool System)
  877. {
  878. TSessionData * Data = (System ? NULL : FSessionData);
  879. if (Logging)
  880. {
  881. if (FParent != NULL)
  882. {
  883. // do not add session info for secondary session
  884. // (this should better be handled in the TSecondaryTerminal)
  885. }
  886. else
  887. {
  888. DoAddStartupInfo(Data);
  889. }
  890. }
  891. }
  892. //---------------------------------------------------------------------------
  893. UnicodeString __fastcall TSessionLog::GetTlsVersionName(TTlsVersion TlsVersion)
  894. {
  895. switch (TlsVersion)
  896. {
  897. default:
  898. DebugFail();
  899. case ssl2:
  900. return "SSLv2";
  901. case ssl3:
  902. return "SSLv3";
  903. case tls10:
  904. return "TLSv1.0";
  905. case tls11:
  906. return "TLSv1.1";
  907. case tls12:
  908. return "TLSv1.2";
  909. }
  910. }
  911. //---------------------------------------------------------------------------
  912. UnicodeString __fastcall TSessionLog::LogSensitive(const UnicodeString & Str)
  913. {
  914. if (FConfiguration->LogSensitive && !Str.IsEmpty())
  915. {
  916. return Str;
  917. }
  918. else
  919. {
  920. return BooleanToEngStr(!Str.IsEmpty());
  921. }
  922. }
  923. //---------------------------------------------------------------------------
  924. UnicodeString __fastcall TSessionLog::GetCmdLineLog()
  925. {
  926. UnicodeString Result = CmdLine;
  927. if (!Configuration->LogSensitive)
  928. {
  929. TManagementScript Script(StoredSessions, false);
  930. Script.MaskPasswordInCommandLine(Result, true);
  931. }
  932. return Result;
  933. }
  934. //---------------------------------------------------------------------------
  935. template <typename T>
  936. UnicodeString __fastcall EnumName(T Value, UnicodeString Names)
  937. {
  938. int N = int(Value);
  939. do
  940. {
  941. UnicodeString Name = CutToChar(Names, L';', true);
  942. if (N == 0)
  943. {
  944. return Name;
  945. }
  946. N--;
  947. }
  948. while ((N >= 0) && !Names.IsEmpty());
  949. return L"(unknown)";
  950. }
  951. //---------------------------------------------------------------------------
  952. #define ADSTR(S) DoAdd(llMessage, S, DoAddToSelf);
  953. #define ADF(S, F) ADSTR(FORMAT(S, F));
  954. //---------------------------------------------------------------------------
  955. void __fastcall TSessionLog::DoAddStartupInfo(TSessionData * Data)
  956. {
  957. BeginUpdate();
  958. try
  959. {
  960. if (Data == NULL)
  961. {
  962. AddSeparator();
  963. UnicodeString OS = WindowsVersionLong();
  964. AddToList(OS, WindowsProductName(), L" - ");
  965. ADF(L"WinSCP %s (OS %s)", (FConfiguration->VersionStr, OS));
  966. THierarchicalStorage * Storage = FConfiguration->CreateConfigStorage();
  967. try
  968. {
  969. ADF(L"Configuration: %s", (Storage->Source));
  970. }
  971. __finally
  972. {
  973. delete Storage;
  974. }
  975. wchar_t UserName[UNLEN + 1];
  976. unsigned long UserNameSize = LENOF(UserName);
  977. if (DebugAlwaysFalse(!GetUserNameEx(NameSamCompatible, UserName, &UserNameSize)))
  978. {
  979. wcscpy(UserName, L"<Failed to retrieve username>");
  980. }
  981. UnicodeString LogStr;
  982. if (FConfiguration->LogProtocol <= 0)
  983. {
  984. LogStr = L"Normal";
  985. }
  986. else if (FConfiguration->LogProtocol == 1)
  987. {
  988. LogStr = L"Debug 1";
  989. }
  990. else if (FConfiguration->LogProtocol >= 2)
  991. {
  992. LogStr = L"Debug 2";
  993. }
  994. if (FConfiguration->LogSensitive)
  995. {
  996. LogStr += L", Logging passwords";
  997. }
  998. ADF(L"Log level: %s", (LogStr));
  999. ADF(L"Local account: %s", (UserName));
  1000. ADF(L"Working directory: %s", (GetCurrentDir()));
  1001. ADF(L"Process ID: %d", (int(GetCurrentProcessId())));
  1002. ADF(L"Command-line: %s", (GetCmdLineLog()));
  1003. if (FConfiguration->LogProtocol >= 1)
  1004. {
  1005. AddOptions(GetGlobalOptions());
  1006. }
  1007. ADF(L"Time zone: %s", (GetTimeZoneLogString()));
  1008. if (!AdjustClockForDSTEnabled())
  1009. {
  1010. ADSTR(L"Warning: System option \"Automatically adjust clock for Daylight Saving Time\" is disabled, timestamps will not be represented correctly");
  1011. }
  1012. ADF(L"Login time: %s", (FormatDateTime(L"dddddd tt", Now())));
  1013. AddSeparator();
  1014. }
  1015. else
  1016. {
  1017. ADF(L"Session name: %s (%s)", (Data->SessionName, Data->Source));
  1018. ADF(L"Host name: %s (Port: %d)", (Data->HostNameExpanded, Data->PortNumber));
  1019. ADF(L"User name: %s (Password: %s, Key file: %s, Passphrase: %s)",
  1020. (Data->UserNameExpanded, LogSensitive(Data->Password),
  1021. LogSensitive(Data->PublicKeyFile), LogSensitive(Data->Passphrase)));
  1022. if (Data->UsesSsh)
  1023. {
  1024. ADF(L"Tunnel: %s", (BooleanToEngStr(Data->Tunnel)));
  1025. if (Data->Tunnel)
  1026. {
  1027. ADF(L"Tunnel: Host name: %s (Port: %d)", (Data->TunnelHostName, Data->TunnelPortNumber));
  1028. ADF(L"Tunnel: User name: %s (Password: %s, Key file: %s)",
  1029. (Data->TunnelUserName,
  1030. LogSensitive(Data->TunnelPassword),
  1031. LogSensitive(Data->TunnelPublicKeyFile)));
  1032. ADF(L"Tunnel: Local port number: %d", (Data->TunnelLocalPortNumber));
  1033. }
  1034. }
  1035. ADF(L"Transfer Protocol: %s", (Data->FSProtocolStr));
  1036. if (Data->UsesSsh || (Data->FSProtocol == fsFTP))
  1037. {
  1038. TPingType PingType;
  1039. int PingInterval;
  1040. if (Data->FSProtocol == fsFTP)
  1041. {
  1042. PingType = Data->FtpPingType;
  1043. PingInterval = Data->FtpPingInterval;
  1044. }
  1045. else
  1046. {
  1047. PingType = Data->PingType;
  1048. PingInterval = Data->PingInterval;
  1049. }
  1050. ADF(L"Ping type: %s, Ping interval: %d sec; Timeout: %d sec",
  1051. (EnumName(PingType, PingTypeNames), PingInterval, Data->Timeout));
  1052. ADF(L"Disable Nagle: %s",
  1053. (BooleanToEngStr(Data->TcpNoDelay)));
  1054. }
  1055. ADF(L"Proxy: %s",
  1056. ((Data->FtpProxyLogonType != 0) ?
  1057. FORMAT(L"FTP proxy %d", (Data->FtpProxyLogonType)) :
  1058. EnumName(Data->ProxyMethod, ProxyMethodNames)));
  1059. if ((Data->FtpProxyLogonType != 0) || (Data->ProxyMethod != ::pmNone))
  1060. {
  1061. ADF(L"HostName: %s (Port: %d); Username: %s; Passwd: %s",
  1062. (Data->ProxyHost, Data->ProxyPort,
  1063. Data->ProxyUsername, LogSensitive(Data->ProxyPassword)));
  1064. if (Data->ProxyMethod == pmTelnet)
  1065. {
  1066. ADF(L"Telnet command: %s", (Data->ProxyTelnetCommand));
  1067. }
  1068. if (Data->ProxyMethod == pmCmd)
  1069. {
  1070. ADF(L"Local command: %s", (Data->ProxyLocalCommand));
  1071. }
  1072. }
  1073. if (Data->UsesSsh || (Data->FSProtocol == fsFTP))
  1074. {
  1075. ADF(L"Send buffer: %d", (Data->SendBuf));
  1076. }
  1077. if (Data->UsesSsh)
  1078. {
  1079. ADF(L"SSH protocol version: %s; Compression: %s",
  1080. (Data->SshProtStr, BooleanToEngStr(Data->Compression)));
  1081. ADF(L"Bypass authentication: %s",
  1082. (BooleanToEngStr(Data->SshNoUserAuth)));
  1083. ADF(L"Try agent: %s; Agent forwarding: %s; TIS/CryptoCard: %s; KI: %s; GSSAPI: %s",
  1084. (BooleanToEngStr(Data->TryAgent), BooleanToEngStr(Data->AgentFwd), BooleanToEngStr(Data->AuthTIS),
  1085. BooleanToEngStr(Data->AuthKI), BooleanToEngStr(Data->AuthGSSAPI)));
  1086. if (Data->AuthGSSAPI)
  1087. {
  1088. ADF(L"GSSAPI: Forwarding: %s",
  1089. (BooleanToEngStr(Data->GSSAPIFwdTGT)));
  1090. }
  1091. ADF(L"Ciphers: %s; Ssh2DES: %s",
  1092. (Data->CipherList, BooleanToEngStr(Data->Ssh2DES)));
  1093. ADF(L"KEX: %s", (Data->KexList));
  1094. UnicodeString Bugs;
  1095. for (int Index = 0; Index < BUG_COUNT; Index++)
  1096. {
  1097. AddToList(Bugs, EnumName(Data->Bug[(TSshBug)Index], AutoSwitchNames), L",");
  1098. }
  1099. ADF(L"SSH Bugs: %s", (Bugs));
  1100. ADF(L"Simple channel: %s", (BooleanToEngStr(Data->SshSimple)));
  1101. ADF(L"Return code variable: %s; Lookup user groups: %s",
  1102. ((Data->DetectReturnVar ? UnicodeString(L"Autodetect") : Data->ReturnVar),
  1103. EnumName(Data->LookupUserGroups, AutoSwitchNames)));
  1104. ADF(L"Shell: %s", ((Data->Shell.IsEmpty()? UnicodeString(L"default") : Data->Shell)));
  1105. ADF(L"EOL: %s, UTF: %s", (EnumName(Data->EOLType, EOLTypeNames), EnumName(Data->NotUtf, NotAutoSwitchNames))); // NotUtf duplicated in FTP branch
  1106. ADF(L"Clear aliases: %s, Unset nat.vars: %s, Resolve symlinks: %s; Follow directory symlinks: %s",
  1107. (BooleanToEngStr(Data->ClearAliases), BooleanToEngStr(Data->UnsetNationalVars),
  1108. BooleanToEngStr(Data->ResolveSymlinks), BooleanToEngStr(Data->FollowDirectorySymlinks)));
  1109. ADF(L"LS: %s, Ign LS warn: %s, Scp1 Comp: %s",
  1110. (Data->ListingCommand,
  1111. BooleanToEngStr(Data->IgnoreLsWarnings),
  1112. BooleanToEngStr(Data->Scp1Compatibility)));
  1113. }
  1114. if ((Data->FSProtocol == fsSFTP) || (Data->FSProtocol == fsSFTPonly))
  1115. {
  1116. UnicodeString Bugs;
  1117. for (int Index = 0; Index < SFTP_BUG_COUNT; Index++)
  1118. {
  1119. AddToList(Bugs, EnumName(Data->SFTPBug[(TSftpBug)Index], AutoSwitchNames), L",");
  1120. }
  1121. ADF(L"SFTP Bugs: %s", (Bugs));
  1122. ADF(L"SFTP Server: %s", ((Data->SftpServer.IsEmpty()? UnicodeString(L"default") : Data->SftpServer)));
  1123. }
  1124. bool FtpsOn = false;
  1125. if (Data->FSProtocol == fsFTP)
  1126. {
  1127. ADF(L"UTF: %s", (EnumName(Data->NotUtf, NotAutoSwitchNames))); // duplicated in UsesSsh branch
  1128. UnicodeString Ftps;
  1129. switch (Data->Ftps)
  1130. {
  1131. case ftpsImplicit:
  1132. Ftps = L"Implicit TLS/SSL";
  1133. FtpsOn = true;
  1134. break;
  1135. case ftpsExplicitSsl:
  1136. Ftps = L"Explicit SSL/TLS";
  1137. FtpsOn = true;
  1138. break;
  1139. case ftpsExplicitTls:
  1140. Ftps = L"Explicit TLS/SSL";
  1141. FtpsOn = true;
  1142. break;
  1143. default:
  1144. DebugAssert(Data->Ftps == ftpsNone);
  1145. Ftps = L"None";
  1146. break;
  1147. }
  1148. // kind of hidden option, so do not reveal it unless it is set
  1149. if (Data->FtpTransferActiveImmediately != asAuto)
  1150. {
  1151. ADF(L"Transfer active immediately: %s", (EnumName(Data->FtpTransferActiveImmediately, AutoSwitchNames)));
  1152. }
  1153. ADF(L"FTPS: %s [Client certificate: %s]",
  1154. (Ftps, LogSensitive(Data->TlsCertificateFile)));
  1155. ADF(L"FTP: Passive: %s [Force IP: %s]; MLSD: %s [List all: %s]; HOST: %s",
  1156. (BooleanToEngStr(Data->FtpPasvMode),
  1157. EnumName(Data->FtpForcePasvIp, AutoSwitchNames),
  1158. EnumName(Data->FtpUseMlsd, AutoSwitchNames),
  1159. EnumName(Data->FtpListAll, AutoSwitchNames),
  1160. EnumName(Data->FtpHost, AutoSwitchNames)));
  1161. }
  1162. if (Data->FSProtocol == fsWebDAV)
  1163. {
  1164. FtpsOn = (Data->Ftps != ftpsNone);
  1165. ADF(L"HTTPS: %s [Client certificate: %s]",
  1166. (BooleanToEngStr(FtpsOn), LogSensitive(Data->TlsCertificateFile)));
  1167. }
  1168. if (FtpsOn)
  1169. {
  1170. if (Data->FSProtocol == fsFTP)
  1171. {
  1172. ADF(L"Session reuse: %s", (BooleanToEngStr(Data->SslSessionReuse)));
  1173. }
  1174. ADF(L"TLS/SSL versions: %s-%s", (GetTlsVersionName(Data->MinTlsVersion), GetTlsVersionName(Data->MaxTlsVersion)));
  1175. }
  1176. ADF(L"Local directory: %s, Remote directory: %s, Update: %s, Cache: %s",
  1177. ((Data->LocalDirectory.IsEmpty() ? UnicodeString(L"default") : Data->LocalDirectory),
  1178. (Data->RemoteDirectory.IsEmpty() ? UnicodeString(L"home") : Data->RemoteDirectory),
  1179. BooleanToEngStr(Data->UpdateDirectories),
  1180. BooleanToEngStr(Data->CacheDirectories)));
  1181. ADF(L"Cache directory changes: %s, Permanent: %s",
  1182. (BooleanToEngStr(Data->CacheDirectoryChanges),
  1183. BooleanToEngStr(Data->PreserveDirectoryChanges)));
  1184. ADF(L"Recycle bin: Delete to: %s, Overwritten to: %s, Bin path: %s",
  1185. (BooleanToEngStr(Data->DeleteToRecycleBin),
  1186. BooleanToEngStr(Data->OverwrittenToRecycleBin),
  1187. Data->RecycleBinPath));
  1188. if (Data->TrimVMSVersions)
  1189. {
  1190. ADF(L"Trim VMS versions: %s",
  1191. (BooleanToEngStr(Data->TrimVMSVersions)));
  1192. }
  1193. UnicodeString TimeInfo;
  1194. if ((Data->FSProtocol == fsSFTP) || (Data->FSProtocol == fsSFTPonly) || (Data->FSProtocol == fsSCPonly) || (Data->FSProtocol == fsWebDAV))
  1195. {
  1196. AddToList(TimeInfo, FORMAT(L"DST mode: %s", (EnumName(Data->DSTMode, DSTModeNames))), L";");
  1197. }
  1198. if ((Data->FSProtocol == fsSCPonly) || (Data->FSProtocol == fsFTP))
  1199. {
  1200. int TimeDifferenceMin = TimeToMinutes(Data->TimeDifference);
  1201. AddToList(TimeInfo, FORMAT(L"Timezone offset: %dh %dm", ((TimeDifferenceMin / MinsPerHour), (TimeDifferenceMin % MinsPerHour))), L";");
  1202. }
  1203. ADSTR(TimeInfo);
  1204. if (Data->FSProtocol == fsWebDAV)
  1205. {
  1206. ADF(L"Compression: %s",
  1207. (BooleanToEngStr(Data->Compression)));
  1208. }
  1209. AddSeparator();
  1210. }
  1211. }
  1212. __finally
  1213. {
  1214. DeleteUnnecessary();
  1215. EndUpdate();
  1216. }
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. void __fastcall TSessionLog::AddOption(const UnicodeString & LogStr)
  1220. {
  1221. ADSTR(LogStr);
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. void __fastcall TSessionLog::AddOptions(TOptions * Options)
  1225. {
  1226. Options->LogOptions(AddOption);
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. #undef ADF
  1230. #undef ADSTR
  1231. //---------------------------------------------------------------------------
  1232. void __fastcall TSessionLog::AddSeparator()
  1233. {
  1234. Add(llMessage, L"--------------------------------------------------------------------------");
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. int __fastcall TSessionLog::GetBottomIndex()
  1238. {
  1239. return (Count > 0 ? (TopIndex + Count - 1) : -1);
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. bool __fastcall TSessionLog::GetLoggingToFile()
  1243. {
  1244. DebugAssert((FFile == NULL) || LogToFile());
  1245. return (FFile != NULL);
  1246. }
  1247. //---------------------------------------------------------------------------
  1248. void __fastcall TSessionLog::Clear()
  1249. {
  1250. TGuard Guard(FCriticalSection);
  1251. FTopIndex += Count;
  1252. TStringList::Clear();
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. //---------------------------------------------------------------------------
  1256. __fastcall TActionLog::TActionLog(TSessionUI * UI, TSessionData * SessionData,
  1257. TConfiguration * Configuration)
  1258. {
  1259. DebugAssert(UI != NULL);
  1260. DebugAssert(SessionData != NULL);
  1261. Init(UI, SessionData, Configuration);
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. __fastcall TActionLog::TActionLog(TConfiguration * Configuration)
  1265. {
  1266. Init(NULL, NULL, Configuration);
  1267. // not associated with session, so no need to waiting for anything
  1268. ReflectSettings();
  1269. }
  1270. //---------------------------------------------------------------------------
  1271. void __fastcall TActionLog::Init(TSessionUI * UI, TSessionData * SessionData,
  1272. TConfiguration * Configuration)
  1273. {
  1274. FCriticalSection = new TCriticalSection;
  1275. FConfiguration = Configuration;
  1276. FUI = UI;
  1277. FSessionData = SessionData;
  1278. FFile = NULL;
  1279. FCurrentLogFileName = L"";
  1280. FCurrentFileName = L"";
  1281. FLogging = false;
  1282. FClosed = false;
  1283. FFailed = false;
  1284. FPendingActions = new TList();
  1285. FIndent = L" ";
  1286. FInGroup = false;
  1287. FEnabled = true;
  1288. }
  1289. //---------------------------------------------------------------------------
  1290. __fastcall TActionLog::~TActionLog()
  1291. {
  1292. DebugAssert(FPendingActions->Count == 0);
  1293. delete FPendingActions;
  1294. FClosed = true;
  1295. ReflectSettings();
  1296. DebugAssert(FFile == NULL);
  1297. delete FCriticalSection;
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. void __fastcall TActionLog::Add(const UnicodeString & Line)
  1301. {
  1302. DebugAssert(FConfiguration);
  1303. if (FLogging)
  1304. {
  1305. TGuard Guard(FCriticalSection);
  1306. if (FFile == NULL)
  1307. {
  1308. OpenLogFile();
  1309. }
  1310. if (FFile != NULL)
  1311. {
  1312. try
  1313. {
  1314. UTF8String UtfLine = UTF8String(Line);
  1315. size_t Written =
  1316. fwrite(UtfLine.c_str(), 1, UtfLine.Length(), (FILE *)FFile);
  1317. if (Written != static_cast<size_t>(UtfLine.Length()))
  1318. {
  1319. throw ECRTExtException(L"");
  1320. }
  1321. #ifdef _DEBUG
  1322. #endif
  1323. Written =
  1324. fwrite("\n", 1, 1, (FILE *)FFile);
  1325. if (Written != 1)
  1326. {
  1327. throw ECRTExtException(L"");
  1328. }
  1329. }
  1330. catch (Exception &E)
  1331. {
  1332. FCriticalSection->Release();
  1333. // avoid endless loop when trying to close tags when closing log, when logging has failed
  1334. if (!FFailed)
  1335. {
  1336. FFailed = true;
  1337. // We failed logging, turn it off and notify user.
  1338. FConfiguration->LogActions = false;
  1339. if (FConfiguration->LogActionsRequired)
  1340. {
  1341. throw EFatal(&E, LoadStr(LOG_FATAL_ERROR));
  1342. }
  1343. else
  1344. {
  1345. try
  1346. {
  1347. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1348. }
  1349. catch (Exception &E)
  1350. {
  1351. if (FUI != NULL)
  1352. {
  1353. FUI->HandleExtendedException(&E);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. void __fastcall TActionLog::AddIndented(const UnicodeString & Line)
  1364. {
  1365. Add(FIndent + Line);
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. void __fastcall TActionLog::AddFailure(TStrings * Messages)
  1369. {
  1370. AddIndented(L"<failure>");
  1371. AddMessages(L" ", Messages);
  1372. AddIndented(L"</failure>");
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. void __fastcall TActionLog::AddFailure(Exception * E)
  1376. {
  1377. TStrings * Messages = ExceptionToMoreMessages(E);
  1378. if (Messages != NULL)
  1379. {
  1380. try
  1381. {
  1382. AddFailure(Messages);
  1383. }
  1384. __finally
  1385. {
  1386. delete Messages;
  1387. }
  1388. }
  1389. }
  1390. //---------------------------------------------------------------------------
  1391. void __fastcall TActionLog::AddMessages(UnicodeString Indent, TStrings * Messages)
  1392. {
  1393. for (int Index = 0; Index < Messages->Count; Index++)
  1394. {
  1395. AddIndented(
  1396. FORMAT(Indent + L"<message>%s</message>", (XmlEscape(Messages->Strings[Index]))));
  1397. }
  1398. }
  1399. //---------------------------------------------------------------------------
  1400. void __fastcall TActionLog::ReflectSettings()
  1401. {
  1402. TGuard Guard(FCriticalSection);
  1403. bool ALogging =
  1404. !FClosed && FConfiguration->LogActions && Enabled;
  1405. if (ALogging && !FLogging)
  1406. {
  1407. FLogging = true;
  1408. Add(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1409. UnicodeString SessionName =
  1410. (FSessionData != NULL) ? XmlAttributeEscape(FSessionData->SessionName) : UnicodeString(L"nosession");
  1411. Add(FORMAT(L"<session xmlns=\"http://winscp.net/schema/session/1.0\" name=\"%s\" start=\"%s\">",
  1412. (SessionName, StandardTimestamp())));
  1413. }
  1414. else if (!ALogging && FLogging)
  1415. {
  1416. if (FInGroup)
  1417. {
  1418. EndGroup();
  1419. }
  1420. // do not try to close the file, if it has not been opened, to avoid recursion
  1421. if (FFile != NULL)
  1422. {
  1423. Add(L"</session>");
  1424. }
  1425. CloseLogFile();
  1426. FLogging = false;
  1427. }
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. void __fastcall TActionLog::CloseLogFile()
  1431. {
  1432. if (FFile != NULL)
  1433. {
  1434. fclose((FILE *)FFile);
  1435. FFile = NULL;
  1436. }
  1437. FCurrentLogFileName = L"";
  1438. FCurrentFileName = L"";
  1439. }
  1440. //---------------------------------------------------------------------------
  1441. void __fastcall TActionLog::OpenLogFile()
  1442. {
  1443. try
  1444. {
  1445. DebugAssert(FFile == NULL);
  1446. DebugAssert(FConfiguration != NULL);
  1447. FCurrentLogFileName = FConfiguration->ActionsLogFileName;
  1448. FFile = OpenFile(FCurrentLogFileName, FSessionData, false, FCurrentFileName);
  1449. }
  1450. catch (Exception & E)
  1451. {
  1452. // We failed logging to file, turn it off and notify user.
  1453. FCurrentLogFileName = L"";
  1454. FCurrentFileName = L"";
  1455. FConfiguration->LogActions = false;
  1456. if (FConfiguration->LogActionsRequired)
  1457. {
  1458. throw EFatal(&E, LoadStr(LOG_FATAL_ERROR));
  1459. }
  1460. else
  1461. {
  1462. try
  1463. {
  1464. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1465. }
  1466. catch (Exception & E)
  1467. {
  1468. if (FUI != NULL)
  1469. {
  1470. // not to deadlock with TSessionLog::ReflectSettings invoked by FConfiguration->LogFileName setter above
  1471. TUnguard Unguard(FCriticalSection);
  1472. FUI->HandleExtendedException(&E);
  1473. }
  1474. }
  1475. }
  1476. }
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. void __fastcall TActionLog::AddPendingAction(TSessionActionRecord * Action)
  1480. {
  1481. FPendingActions->Add(Action);
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. void __fastcall TActionLog::RecordPendingActions()
  1485. {
  1486. while ((FPendingActions->Count > 0) &&
  1487. static_cast<TSessionActionRecord *>(FPendingActions->Items[0])->Record())
  1488. {
  1489. FPendingActions->Delete(0);
  1490. }
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. void __fastcall TActionLog::BeginGroup(UnicodeString Name)
  1494. {
  1495. DebugAssert(!FInGroup);
  1496. FInGroup = true;
  1497. DebugAssert(FIndent == L" ");
  1498. AddIndented(FORMAT(L"<group name=\"%s\" start=\"%s\">",
  1499. (XmlAttributeEscape(Name), StandardTimestamp())));
  1500. FIndent = L" ";
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. void __fastcall TActionLog::EndGroup()
  1504. {
  1505. DebugAssert(FInGroup);
  1506. FInGroup = false;
  1507. DebugAssert(FIndent == L" ");
  1508. FIndent = L" ";
  1509. // this is called from ReflectSettings that in turn is called when logging fails,
  1510. // so do not try to close the group, if it has not been opened, to avoid recursion
  1511. if (FFile != NULL)
  1512. {
  1513. AddIndented(L"</group>");
  1514. }
  1515. }
  1516. //---------------------------------------------------------------------------
  1517. void __fastcall TActionLog::SetEnabled(bool value)
  1518. {
  1519. if (Enabled != value)
  1520. {
  1521. FEnabled = value;
  1522. ReflectSettings();
  1523. }
  1524. }