Advertisement
YauhenMardan

release

Nov 20th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "TemplateDLL.h"
  3. #include <iomanip>
  4. #include <filesystem>
  5.  
  6. #include <filesystem>
  7.  
  8. #include <vector>
  9. #include "User.h"
  10.  
  11. std::vector<User> readFile(std::wstring fileName); //read file
  12. std::wifstream & operator >> (std::wifstream & wifs, User & user); //read each user
  13. void tryToOpenFile(std::wstring& logText, std::wstring filePath);
  14. void tryToCreateFile(std::wstring& logText, std::wstring filePath);
  15. void tryToReadFile(std::wstring& logText, std::wstring filePath);
  16. void tryToReadAllFiles(std::wstring& logText, std::wstring filePath);
  17.  
  18. namespace fs = std::experimental::filesystem;
  19.  
  20. int foo(int a) {
  21. return a + a;
  22. }
  23. extern "C"
  24. {
  25. LPCWSTR GetName()
  26. {
  27. return m_sName;
  28. }
  29.  
  30. LPCWSTR GetDescription()
  31. {
  32. return m_sDescription;
  33. }
  34.  
  35. bool RunTest(std::function<void(LPCWSTR)> pFnLog)
  36. {
  37. std::wstring logText = L"log text:\n"; //test output
  38. // test code
  39. std::wstring confFileName = L"conf.ini"; //configuration file
  40. std::vector<User> data; //data: users
  41. data = readFile(confFileName); //read conf file
  42. //impersonation
  43. HANDLE hToken; //receives a handle to a token that represents the specified user
  44. for (int i = 0; i < data.size(); i++) {
  45. std::wstring name = data[i].getUserName(); //temp variables
  46. std::wstring pass = data[i].getPassword();
  47. LogonUserW( //log on user
  48. (LPCWSTR)name.c_str(),
  49. L".", //using only the local account database
  50. (LPCWSTR)pass.c_str(),
  51. LOGON32_LOGON_INTERACTIVE,
  52. LOGON32_PROVIDER_DEFAULT,
  53. &hToken
  54. );
  55. SecureZeroMemory(&pass, sizeof(pass));
  56. logText += name + L"\n"; //print name of user
  57. ImpersonateLoggedOnUser(hToken); //impersonate user
  58. //try to read all files
  59. std::wstring folderFullPath = data[i].getFullPath();
  60. tryToReadAllFiles(logText, folderFullPath);
  61. ////try to read file FileToRead.txt
  62. //std::wstring fileToReadFullName = folderFullPath + L"\\FileToRead.txt";
  63. //tryToOpenFile(logText, data, fileToReadFullName);
  64. //try to create a file FileToCreate.txt
  65. std::wstring fileToCreateFullName = folderFullPath + L"\\FileToCreate.txt";
  66. tryToCreateFile(logText, fileToCreateFullName);
  67. //try to read FileToCreate.txt
  68. tryToReadFile(logText, fileToCreateFullName);
  69. RevertToSelf(); //session revertion
  70. }
  71. pFnLog(logText.c_str());
  72. return true;
  73. }
  74. }
  75.  
  76. std::vector<User> readFile(std::wstring fileName) {
  77. std::vector<User> data; //vectror of users
  78. std::wifstream fin(fileName);
  79. while (!fin.eof())
  80. {
  81. User tmp_user;
  82. fin >> tmp_user;
  83. data.push_back(tmp_user);
  84. }
  85. return data;
  86. }
  87. std::wifstream & operator >> (std::wifstream & wifs, User & user) {
  88. wifs >> user.userName >> user.domainName >> user.password >> user.fullPath;
  89. return wifs;
  90. }
  91. void tryToOpenFile(std::wstring& logText, std::wstring filePath) {
  92. std::wifstream fin;
  93. fin.open(filePath.c_str());
  94. if (fin.is_open()) {
  95. logText += L"File is opened " + filePath + L"\n";
  96. }
  97. else {
  98. logText += L"Cannot open the file " + filePath + L"\n";
  99. }
  100. fin.close();
  101. }
  102. void tryToCreateFile(std::wstring& logText, std::wstring filePath) {
  103. std::wofstream fout;
  104. fout.open(filePath);
  105. if (fout.is_open()) {
  106. logText += L"File is created " + filePath + L"\n";
  107. //try to write to file FileToCreate.txt
  108. std::wstring testText = L"Try to write text";
  109. fout << testText;
  110. logText += L"Trying to write: " + testText + L"\n";
  111. }
  112. else {
  113. logText += L"Cannot create the file " + filePath + L"\n";
  114. }
  115. logText += L"**********\n";
  116. fout.close();
  117. }
  118. void tryToReadFile(std::wstring& logText, std::wstring filePath) {
  119. std::wifstream fin;
  120. fin.open(filePath);
  121. if (fin.is_open()) {
  122. logText += L"Reading the file " + filePath + L"\n" + L"File contains: ";
  123. while (!fin.eof())
  124. {
  125. std::wstring temp;
  126. fin >> temp;
  127. logText+=temp +L" ";
  128. }
  129. logText += L"\n";
  130. }
  131. else {
  132. logText+=L"Cannot open " + filePath + L"\n";
  133. }
  134. fin.close();
  135. logText += L"**********\n";
  136. }
  137. void tryToReadAllFiles(std::wstring& logText, std::wstring filePath) {
  138. logText += L"Folder contains:\n";
  139. for (auto & p : fs::recursive_directory_iterator(filePath)) {
  140. logText += p.path().c_str();
  141. logText += L"\n";
  142. }
  143. for (auto & p : fs::recursive_directory_iterator(filePath)) {
  144. if (!fs::is_directory(p)) {
  145. tryToOpenFile(logText, p.path().c_str());
  146. }
  147. }
  148. logText += L"**********\n";
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement