Advertisement
Guest User

Untitled

a guest
May 18th, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. * @ This file is created by http://DeZender.Net
  5. * @ deZender (PHP7 Decoder for SourceGuardian Encoder)
  6. *
  7. * @ Version : 4.1.0.1
  8. * @ Author : DeZender
  9. * @ Release on : 29.08.2020
  10. * @ Official site : http://DeZender.Net
  11. *
  12. */
  13.  
  14. class LicenseLib
  15. {
  16. const FEATURES_ALL_CUSTOMERS = '00000000000000000000000000000000000000';
  17. const LICENSE_UNMATCHABLE_HASH = 'XXXXX';
  18. const LICENSE_EXPIRES_NEVER = 'never';
  19.  
  20. static public function licenseDetails($filename = NULL, $remoteHostname = NULL, $licenseCheckType = LICENSE_NORMAL_CHECK)
  21. {
  22. $root = $GLOBALS['root'];
  23. $licenseDetails = [];
  24.  
  25. if (empty($filename)) {
  26. $filename = '/opt/tenable/' . LICENSE_FILE;
  27.  
  28. if (!file_exists($filename)) {
  29. $filename = $root . '/daemons/' . LICENSE_FILE;
  30. }
  31. }
  32.  
  33. if (!file_exists($filename)) {
  34. ErrorHandler::setErrorHandler('License file \'' . $filename . '\' not found.', RESPONSE_NOT_FOUND);
  35. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getErrorMessage());
  36. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  37. return ERROR;
  38. }
  39.  
  40. if (($licenseDetails = LicenseLib::licenseCheck($filename, $remoteHostname, $licenseCheckType)) === ERROR) {
  41. $msg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage());
  42. SCLog::log($msg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  43. return ERROR;
  44. }
  45.  
  46. return $licenseDetails;
  47. }
  48.  
  49. static public function getForeignRepositoriesForLicense($license, $context = CONTEXT_ALL, $repFormat = REPOSITORY_DATAFORMAT_BF_ALL)
  50. {
  51. $root = $GLOBALS['root'];
  52. $directory = $root . '/daemons';
  53. $repositories = [];
  54.  
  55. if (($reps = RepositoryLib::getRepositories(REPOSITORY_TYPE_ALL, RESULT_IDXANNOTATIONS, $context, $repFormat)) === ERROR) {
  56. return ERROR;
  57. }
  58.  
  59. if (($paramDigest = @md5_file($license)) === false) {
  60. ErrorHandler::setErrorHandler('Unable to digest license \'' . $license . '\'.', RESPONSE_FILE_READ_ERROR);
  61. return ERROR;
  62. }
  63.  
  64. if (($handle = @opendir($directory)) === false) {
  65. ErrorHandler::setErrorHandler('Unable to open the daemon directory.', RESPONSE_FILE_READ_ERROR);
  66. return ERROR;
  67. }
  68.  
  69. while (($file = readdir($handle)) !== false) {
  70. $match = [];
  71.  
  72. if (preg_match('/(?<repID>\\d+)\\.license\\.key/', $file, $match)) {
  73. if (($licenseDigest = @md5_file($directory . '/' . $file)) === false) {
  74. ErrorHandler::setErrorHandler('Unable to digest stored license \'' . $directory . '/' . $file . '\'.', RESPONSE_FILE_READ_ERROR);
  75. return ERROR;
  76. }
  77.  
  78. if ($paramDigest === $licenseDigest) {
  79. }
  80. }
  81. }
  82.  
  83. return $repositories;
  84. }
  85.  
  86. static public function store($filename)
  87. {
  88. if (!file_exists($filename)) {
  89. ErrorHandler::setErrorHandler('License file \'' . $filename . '\' not found.', RESPONSE_NOT_FOUND);
  90. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  91. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  92. return ERROR;
  93. }
  94.  
  95. $currentLicenseDetails = LicenseLib::licenseDetails(NULL, NULL, LICENSE_UPGRADE_CHECK);
  96. if (($currentLicenseDetails === ERROR) || ($currentLicenseDetails['status'] == RESPONSE_DENIED)) {
  97. $currentLicenseDetails = [];
  98. $currentLicenseDetails['type'] = LICENSE_MODE_NONE;
  99. }
  100.  
  101. ErrorHandler::resetError();
  102. $newLicenseDetails = LicenseLib::licenseDetails($filename);
  103. if (($newLicenseDetails === ERROR) || ($newLicenseDetails['status'] != RESPONSE_OK)) {
  104. if ($newLicenseDetails['status'] != RESPONSE_OK) {
  105. ErrorHandler::setErrorHandler('License is invalid.', RESPONSE_LICENSE_ERROR);
  106. }
  107. else {
  108. ErrorHandler::addErrorMessage('License is invalid.');
  109. }
  110.  
  111. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  112. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  113. return ERROR;
  114. }
  115.  
  116. switch ($currentLicenseDetails['type']) {
  117. case LICENSE_MODE_SC:
  118. case LICENSE_MODE_MANAGED_SC:
  119. if ($newLicenseDetails['type'] == LICENSE_MODE_DIRECTOR) {
  120. ErrorHandler::setErrorHandler('New license is not a Tenable.sc license.', RESPONSE_INVALID_DATA);
  121. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  122. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  123. return ERROR;
  124. }
  125.  
  126. break;
  127. case LICENSE_MODE_DIRECTOR:
  128. if ($newLicenseDetails['type'] != LICENSE_MODE_DIRECTOR) {
  129. ErrorHandler::setErrorHandler('New license is not a Tenable.sc Director license.', RESPONSE_INVALID_DATA);
  130. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  131. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  132. return ERROR;
  133. }
  134.  
  135. break;
  136. case LICENSE_MODE_NONE:
  137. break;
  138. default:
  139. ErrorHandler::setErrorHandler('New license is not a valid Tenable.sc license.', RESPONSE_INVALID_DATA);
  140. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  141. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  142. return ERROR;
  143. }
  144.  
  145. $licenseFile = $GLOBALS['root'] . '/daemons/' . LICENSE_FILE;
  146.  
  147. if (!@rename($filename, $licenseFile)) {
  148. ErrorHandler::setErrorHandler('Error installing License file.', RESPONSE_FILESYSTEM_ERROR);
  149. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  150. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  151. return ERROR;
  152. }
  153.  
  154. if (LicenseLib::setLicenseFields(0) === ERROR) {
  155. ErrorHandler::addErrorMessage('Error initializing new License.');
  156. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  157. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  158. return ERROR;
  159. }
  160.  
  161. $job['objectID'] = NOT_SET;
  162. $job['objectType'] = JOB_TYPE_LICENSE_REPORT;
  163. $job['ownerID'] = USER_ID_ORGHEAD;
  164.  
  165. if (JobLib::insertIntoJobQueue($job, ORG_NONE, time(), IMMEDIATE) === ERROR) {
  166. ErrorHandler::addErrorMessage('Error queueing licenseReport job.');
  167. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_LOGMSG, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  168. SCLog::log($errorMsg, LOG_LEVEL_WARN, LOG_TYPE_ALL);
  169. }
  170.  
  171. if (($rc = LicenseLib::verifyLicenseStatus()) === ERROR) {
  172. $copyErrorCode = ErrorHandler::getErrorCode();
  173. $copyErrorMessage = ErrorHandler::getErrorMessage();
  174. }
  175.  
  176. if (isset($newLicenseDetails['features']['ACAS'])) {
  177. ReportDefinitionLib::enableACASReports();
  178. }
  179.  
  180. $settings = [];
  181. $settings['FreshInstall'] = 'no';
  182. ConfigurationLib::updateConfig($settings);
  183. dbLib::getDBH()->commit();
  184. DaemonsLib::restartJobd();
  185. ErrorHandler::resetError();
  186.  
  187. if ($newLicenseDetails['type'] == LICENSE_MODE_DIRECTOR) {
  188. if (LicenseLib::createDirectorDirectories() === ERROR) {
  189. ErrorHandler::addErrorMessage('Error initializing Filesystem for Director License.');
  190. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_ERROR, ErrorHandler::getLogMessage(), ErrorHandler::getErrorCode());
  191. SCLog::log($errorMsg, LOG_LEVEL_CRITICAL, LOG_TYPE_ALL);
  192. return ERROR;
  193. }
  194. }
  195.  
  196. if ($rc == ERROR) {
  197. ErrorHandler::setErrorHandler($copyErrorMessage, $copyErrorCode);
  198. }
  199. else {
  200. $errorMsg = MessageLib::createStaticMessage(SCMSG_STATIC_LOGMSG, 'New License uploaded.');
  201. SCLog::log($errorMsg, LOG_LEVEL_INFO, LOG_TYPE_ADMIN);
  202. }
  203.  
  204. return $rc;
  205. }
  206.  
  207. static public function validateRemoteLicenseLiteForMVP($license, $licenseHostname)
  208. {
  209. if (($repsWithLicense = LicenseLib::getForeignRepositoriesForLicense($license)) === ERROR) {
  210. ErrorHandler::addErrorMessage('Failed to validate remote license while retrieving associated Repositories.');
  211. return ERROR;
  212. }
  213.  
  214. $repsWithLicense = RepositoryLib::organizeByDataFormat($repsWithLicense);
  215. $ipRepositories = array_merge($repsWithLicense[REPOSITORY_DATAFORMAT_IPV4], $repsWithLicense[REPOSITORY_DATAFORMAT_IPV6]);
  216. $uuidRepositories = $repsWithLicense[REPOSITORY_DATAFORMAT_AGENT];
  217. $counts = [];
  218.  
  219. if (($ipCount = LicenseLib::countDevicesForRepositories($ipRepositories)) === ERROR) {
  220. ErrorHandler::addErrorMessage('Failed to validate remote license for while retrieving total IP count of repositories.');
  221. return ERROR;
  222. }
  223.  
  224. $counts[] = $ipCount;
  225.  
  226. if (($uuidCount = LicenseLib::countDevicesForRepositories($uuidRepositories)) === ERROR) {
  227. ErrorHandler::addErrorMessage('Failed to validate remote license for while retrieving total agent count of repositories.');
  228. return ERROR;
  229. }
  230.  
  231. $counts[] = $uuidCount;
  232. $deviceCount = ($counts ? max($counts) : NULL);
  233. if ((($license = LicenseLib::licenseDetails($license, $licenseHostname)) === ERROR) || ($license['status'] != RESPONSE_OK)) {
  234. ErrorHandler::addErrorMessage('Failed to validate license for repositories.');
  235. return ERROR;
  236. }
  237.  
  238. if ($license['ipCount'] < $deviceCount) {
  239. ErrorHandler::setErrorHandler('License device count is \'' . $deviceCount . '\' but Repository device count for this license is \'' . $license['ipCount'] . '\'.', RESPONSE_STOPPED);
  240. return ERROR;
  241. }
  242.  
  243. return SUCCESS;
  244. }
  245.  
  246. static public function validateRemoteLicense($license, $licenseHostname)
  247. {
  248. if (($repsWithLicense = LicenseLib::getForeignRepositoriesForLicense($license)) === ERROR) {
  249. ErrorHandler::addErrorMessage('Failed to validate remote license while retrieving associated Repositories.');
  250. return ERROR;
  251. }
  252.  
  253. if (($deviceCount = LicenseLib::countDevicesForRepositories($repsWithLicense)) === ERROR) {
  254. ErrorHandler::addErrorMessage('Failed to validate remote license for while retrieving total device count of repositories.');
  255. return ERROR;
  256. }
  257. if ((($license = LicenseLib::licenseDetails($license, $licenseHostname)) === ERROR) || ($license['status'] != RESPONSE_OK)) {
  258. ErrorHandler::addErrorMessage('Failed to validate license for repositories.');
  259. return ERROR;
  260. }
  261.  
  262. if ($license['ipCount'] < $deviceCount) {
  263. ErrorHandler::setErrorHandler('License device count is \'' . $deviceCount . '\' but Repository device count for this license is \'' . $license['ipCount'] . '\'.', RESPONSE_STOPPED);
  264. return ERROR;
  265. }
  266.  
  267. return SUCCESS;
  268. }
  269.  
  270. static public function countDevicesLiteForMVP($repositoryType = REPOSITORY_TYPE_ALL, $repositoryFormatBF = REPOSITORY_DATAFORMAT_BF_DEVICE, $lock = ACQUIRE_REPOSITORY_LOCK)
  271. {
  272. $counts = [];
  273.  
  274. if ($repositoryFormatBF & REPOSITORY_DATAFORMAT_BF_IP) {
  275. if (($ipCount = LicenseLib::countSpecificLicenseQualifyingDevices($repositoryType, REPOSITORY_DATAFORMAT_BF_IP, $lock)) === ERROR) {
  276. return ERROR;
  277. }
  278.  
  279. $counts[] = $ipCount;
  280. }
  281.  
  282. if ($repositoryFormatBF & REPOSITORY_DATAFORMAT_BF_AGENT) {
  283. if (($uuidCount = LicenseLib::countSpecificLicenseQualifyingDevices($repositoryType, REPOSITORY_DATAFORMAT_BF_AGENT, $lock)) === ERROR) {
  284. return ERROR;
  285. }
  286.  
  287. $counts[] = $uuidCount;
  288. }
  289.  
  290. return $counts ? max($counts) : NULL;
  291. .................................................................
  292. ..........................................
  293. ......................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement