From 8ad56e8eda4a63e9a81c275e4eb26e9239237ad4 Mon Sep 17 00:00:00 2001
From: gcontini <1121667+gcontini@users.noreply.github.com>
Date: 周六, 28 11月 2020 09:31:41 +0800
Subject: [PATCH] replace strncpy with a more safe alternative

---
 src/library/licensecc.cpp |   59 +++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 39 insertions(+), 20 deletions(-)

diff --git a/src/library/licensecc.cpp b/src/library/licensecc.cpp
index 964f362..2642811 100644
--- a/src/library/licensecc.cpp
+++ b/src/library/licensecc.cpp
@@ -1,5 +1,5 @@
 //============================================================================
-// Name        : license-manager-cpp.cpp
+// Name        : licensecc.cpp
 // Author      :
 // Version     :
 // Copyright   : BSD
@@ -17,23 +17,42 @@
 #include <licensecc/licensecc.h>
 #include <licensecc_properties.h>
 
-#include "pc_identifier/pc_identifier_facade.hpp"
+#include "base/logger.h"
+#include "base/string_utils.h"
+#include "hw_identifier/hw_identifier_facade.hpp"
+#include "os/execution_environment.hpp"
 #include "limits/license_verifier.hpp"
-#include "base/StringUtils.h"
+#include "base/string_utils.h"
 #include "LicenseReader.hpp"
 
 using namespace std;
-void print_error(char out_buffer[256], LicenseInfo* licenseInfo) {}
 
-bool identify_pc(IDENTIFICATION_STRATEGY pc_id_method, char* chbuffer, size_t* bufSize) {
-	FUNCTION_RETURN result = FUNC_RET_BUFFER_TOO_SMALL;
-	string pc_id = license::PcIdentifierFacade::generate_user_pc_signature(pc_id_method);
-	if (*bufSize >= pc_id.size() + 1) {
-		strncpy(chbuffer, pc_id.c_str(), *bufSize);
+void print_error(char out_buffer[LCC_API_ERROR_BUFFER_SIZE], LicenseInfo* licenseInfo) {}
+
+bool identify_pc(LCC_API_HW_IDENTIFICATION_STRATEGY pc_id_method, char* chbuffer, size_t* bufSize,
+				 ExecutionEnvironmentInfo* execution_environment_info) {
+	bool result = false;
+	if (*bufSize > LCC_API_PC_IDENTIFIER_SIZE && chbuffer != nullptr) {
+		try {
+			const string pc_id = license::hw_identifier::HwIdentifierFacade::generate_user_pc_signature(pc_id_method);
+			license::mstrlcpy(chbuffer, pc_id.c_str(), *bufSize);
+			result = true;
+		} catch (const std::exception& ex) {
+			LOG_ERROR("Error calculating hw_identifier: %s", ex.what());
+#ifndef NDEBUG
+			cout << "Error occurred: " << ex.what() << std::endl;
+#endif
+		}
 	} else {
-		*bufSize = pc_id.size() + 1;
+		*bufSize = LCC_API_PC_IDENTIFIER_SIZE + 1;
 	}
-	return result == FUNC_RET_OK;
+	static const license::os::ExecutionEnvironment exec_env;
+	if (execution_environment_info != nullptr) {
+		execution_environment_info->cloud_provider = exec_env.cloud_provider();
+		execution_environment_info->virtualization = exec_env.virtualization();
+		execution_environment_info->virtualization_detail = exec_env.virtualization_detail();
+	}
+	return result;
 }
 
 static void mergeLicenses(const vector<LicenseInfo>& licenses, LicenseInfo* license_out) {
@@ -52,20 +71,20 @@
 	}
 }
 
-EVENT_TYPE acquire_license(const CallerInformations* callerInformation, const LicenseLocation* licenseLocation,
-						   LicenseInfo* license_out) {
+LCC_EVENT_TYPE acquire_license(const CallerInformations* callerInformation, const LicenseLocation* licenseLocation,
+							   LicenseInfo* license_out) {
 	const license::LicenseReader lr = license::LicenseReader(licenseLocation);
 	vector<license::FullLicenseInfo> licenses;
 	string project;
 	size_t str_size;
 	if (callerInformation != nullptr &&
-		(str_size = license::mstrnlen_s(callerInformation->project_name, sizeof callerInformation->project_name)) > 0) {
-		project = string(callerInformation->project_name, str_size);
+		(str_size = license::mstrnlen_s(callerInformation->feature_name, sizeof callerInformation->feature_name)) > 0) {
+		project = string(callerInformation->feature_name, str_size);
 	} else {
 		project = string(LCC_PROJECT_NAME);
 	}
 	license::EventRegistry er = lr.readLicenses(string(project), licenses);
-	EVENT_TYPE result;
+	LCC_EVENT_TYPE result;
 	if (licenses.size() > 0) {
 		vector<LicenseInfo> licenses_with_errors;
 		vector<LicenseInfo> licenses_ok;
@@ -104,16 +123,16 @@
 			license_out->days_left = 0;
 		}
 	}
-#ifdef _DEBUG
+#ifndef NDEBUG
 	cout << er << endl;
 #endif
 
 	if (license_out != nullptr) {
-		er.exportLastEvents(license_out->status, AUDIT_EVENT_NUM);
+		er.exportLastEvents(license_out->status, LCC_API_AUDIT_EVENT_NUM);
 	}
 	return result;
 }
 
-EVENT_TYPE confirm_license(char* product, LicenseLocation licenseLocation) { return LICENSE_OK; }
+LCC_EVENT_TYPE confirm_license(char* product, LicenseLocation licenseLocation) { return LICENSE_OK; }
 
-EVENT_TYPE release_license(char* product, LicenseLocation licenseLocation) { return LICENSE_OK; }
+LCC_EVENT_TYPE release_license(char* product, LicenseLocation licenseLocation) { return LICENSE_OK; }

--
Gitblit v1.9.1