SessionInfo.cpp 51 KB

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