From 7b5931685caf55b7b974cb759e5018dbcf1a75c9 Mon Sep 17 00:00:00 2001
From: open-license-manager <rillf@maildrop.cc>
Date: 周一, 04 8月 2014 06:01:22 +0800
Subject: [PATCH] windows

---
 src/library/LicenseReader.cpp |  114 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 68 insertions(+), 46 deletions(-)

diff --git a/src/library/LicenseReader.cpp b/src/library/LicenseReader.cpp
index 7ad0ede..f9f7531 100644
--- a/src/library/LicenseReader.cpp
+++ b/src/library/LicenseReader.cpp
@@ -18,20 +18,21 @@
 #include <fstream>
 #include <sstream>
 #include <stdlib.h>
+#include "pc-identifiers.h"
 #include "LicenseReader.h"
 #include "base/StringUtils.h"
 #include "base/public-key.h"
 #include <build_properties.h>
 
-#include "os/os.hpp"
+#include "os/os-cpp.h"
 
 namespace license {
 
 FullLicenseInfo::FullLicenseInfo(const string& source, const string& product,
-		const string& license_signature, int licenseVersion, //
-		time_t from_date, time_t to_date, //
-		const string& client_signature, unsigned int from_sw_version,
-		unsigned int to_sw_version, const string& extra_data) :
+		const string& license_signature, int licenseVersion, string from_date,
+		string to_date, const string& client_signature,
+		unsigned int from_sw_version, unsigned int to_sw_version,
+		const string& extra_data) :
 		source(source), product(product), //
 		license_signature(license_signature), license_version(licenseVersion), //
 		from_date(from_date), to_date(to_date), //
@@ -47,20 +48,29 @@
 
 EventRegistry FullLicenseInfo::validate(int sw_version) {
 	EventRegistry er;
+	os_initialize();
 	bool sigVerified = OsFunctions::verifySignature(printForSign().c_str(),
 			license_signature.c_str());
 	if (sigVerified) {
-		er.addEvent(LICENSE_VERIFIED, INFO);
+		er.addEvent(LICENSE_VERIFIED, SVRT_INFO);
 	} else {
-		er.addEvent(LICENSE_CORRUPTED, SEVERITY_ERROR);
+		er.addEvent(LICENSE_CORRUPTED, SVRT_ERROR);
 	}
 	if (has_expiry) {
 		time_t now = time(NULL);
-		if (this->to_date < now) {
-			er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR, "");
+		if (expires_on() < now) {
+			er.addEvent(PRODUCT_EXPIRED, SVRT_ERROR, "");
 		}
-		if (this->from_date > now) {
-			er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR);
+		if (valid_from() > now) {
+			er.addEvent(PRODUCT_EXPIRED, SVRT_ERROR);
+		}
+	}
+	if (has_client_sig) {
+		PcSignature str_code;
+		strncpy(str_code, client_signature.c_str(), sizeof(str_code));
+		EVENT_TYPE event = validate_pc_signature(str_code);
+		if (event != LICENSE_OK) {
+			er.addEvent(event, SVRT_ERROR);
 		}
 	}
 	return er;
@@ -77,9 +87,9 @@
 			license->expiry_date[0] = '\0';
 			license->days_left = 999999;
 		} else {
-			tm * timeinfo = localtime(&to_date);
-			strftime(license->expiry_date, 11, "%Y-%m-%d", timeinfo);
-			double secs = difftime(time(NULL), to_date);
+			strncpy(license->expiry_date, to_date.c_str(), 11);
+			double secs = difftime(time(NULL),
+					seconds_from_epoch(to_date.c_str()));
 			license->days_left = (int) secs / 60 * 60 * 24;
 		}
 	}
@@ -87,16 +97,6 @@
 LicenseReader::LicenseReader(const LicenseLocation& licenseLocation) :
 		licenseLocation(licenseLocation) {
 
-}
-
-time_t LicenseReader::read_date(const char * productName, const char * ini_key,
-		const CSimpleIniA& ini) const {
-	string from_date_str = ini.GetValue(productName, ini_key);
-	time_t from_date = FullLicenseInfo::UNUSED_TIME;
-	if (from_date_str.length() > 0) {
-		from_date = seconds_from_epoch(from_date_str.c_str());
-	}
-	return from_date;
 }
 
 EventRegistry LicenseReader::readLicenses(const string &product,
@@ -114,7 +114,7 @@
 		ini.Reset();
 		SI_Error rc = ini.LoadFile((*it).c_str());
 		if (rc < 0) {
-			result.addEvent(FILE_FORMAT_NOT_RECOGNIZED, SEVERITY_WARN, *it);
+			result.addEvent(FILE_FORMAT_NOT_RECOGNIZED, SVRT_WARN, *it);
 			continue;
 		} else {
 			loadAtLeastOneFile = true;
@@ -122,7 +122,7 @@
 		const char* productNamePtr = product.c_str();
 		int sectionSize = ini.GetSectionSize(productNamePtr);
 		if (sectionSize <= 0) {
-			result.addEvent(PRODUCT_NOT_LICENSED, SEVERITY_WARN, *it);
+			result.addEvent(PRODUCT_NOT_LICENSED, SVRT_WARN, *it);
 			continue;
 		} else {
 			atLeastOneProductLicensed = true;
@@ -132,7 +132,7 @@
 		 *  sw_version_to = (optional int)
 		 *  from_date = YYYY-MM-DD (optional)
 		 *  to_date  = YYYY-MM-DD (optional)
-		 *  client_signature = XXXXXXXX (optional string 16)
+		 *  client_signature = XXXX-XXXX-XXXX-XXXX (optional string 16)
 		 *  license_signature = XXXXXXXXXX (mandatory, 1024)
 		 *  application_data = xxxxxxxxx (optional string 16)
 		 */
@@ -141,14 +141,28 @@
 		long license_version = ini.GetLongValue(productNamePtr,
 				"license_version", -1);
 		if (license_signature != NULL && license_version > 0) {
-			time_t from_date = read_date(productNamePtr, "from_date", ini);
-			time_t to_date = read_date(productNamePtr, "to_date", ini);
+			string from_date = trim_copy(
+					ini.GetValue(productNamePtr, "from_date",
+							FullLicenseInfo::UNUSED_TIME));
+			string to_date = trim_copy(
+					ini.GetValue(productNamePtr, "to_date",
+							FullLicenseInfo::UNUSED_TIME));
+			string client_signature = trim_copy(
+					ini.GetValue(productNamePtr, "client_signature", ""));
+			/*client_signature.erase(
+					std::remove(client_signature.begin(), client_signature.end(), '-'),
+					client_signature.end());*/
+			int from_sw_version = ini.GetLongValue(productNamePtr,
+					"from_sw_version", FullLicenseInfo::UNUSED_SOFTWARE_VERSION);
+			int to_sw_version = ini.GetLongValue(productNamePtr,
+					"to_sw_version", FullLicenseInfo::UNUSED_SOFTWARE_VERSION);
 			FullLicenseInfo licInfo(*it, product, license_signature,
-					(int) license_version, from_date, to_date);
+					(int) license_version, from_date, to_date,
+					client_signature,from_sw_version,to_sw_version);
 			licenseInfoOut.push_back(licInfo);
 			atLeastOneLicenseComplete = true;
 		} else {
-			result.addEvent(LICENSE_MALFORMED, SEVERITY_WARN, *it);
+			result.addEvent(LICENSE_MALFORMED, SVRT_WARN, *it);
 		}
 	}
 	if (!loadAtLeastOneFile) {
@@ -179,11 +193,11 @@
 				for (auto it = existing_pos.begin(); it != existing_pos.end();
 						++it) {
 					diskFiles.push_back(*it);
-					eventRegistry.addEvent(LICENSE_FILE_FOUND, INFO, *it);
+					eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO, *it);
 				}
 			}
 		} else {
-			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SEVERITY_WARN,
+			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN,
 					varName);
 		}
 	}
@@ -209,19 +223,19 @@
 					for (auto it = existing_pos.begin();
 							it != existing_pos.end(); ++it) {
 						diskFiles.push_back(*it);
-						eventRegistry.addEvent(LICENSE_FILE_FOUND, INFO, *it);
+						eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO, *it);
 					}
 				} else {
 					eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND,
-							SEVERITY_WARN, env_var_value);
+							SVRT_WARN, env_var_value);
 				}
 			} else {
 				eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED,
-						SEVERITY_WARN);
+						SVRT_WARN);
 			}
 		} else {
 			eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED,
-					SEVERITY_WARN);
+					SVRT_WARN);
 		}
 	}
 	return licenseFileFoundWithEnvVariable;
@@ -238,9 +252,9 @@
 		if (f.good()) {
 			foundNearModule = true;
 			diskFiles.push_back(temptativeLicense);
-			eventRegistry.addEvent(LICENSE_FILE_FOUND, INFO, temptativeLicense);
+			eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO, temptativeLicense);
 		} else {
-			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SEVERITY_WARN,
+			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN,
 					temptativeLicense);
 		}
 		f.close();
@@ -297,12 +311,16 @@
 		oss << "|" << this->from_sw_version << "-" << this->to_sw_version;
 	}
 	if (has_expiry) {
-		oss << "|" << this->from_date << "-" << this->to_date;
+		oss << "|" << this->from_date << "|" << this->to_date;
 	}
 	if (this->extra_data.length() > 0) {
 		oss << "|" << extra_data;
 	}
+#ifdef _DEBUG
+	cout << "[" << oss.str() << "]" << endl;
+#endif
 	return oss.str();
+
 }
 
 void FullLicenseInfo::printAsIni(ostream & a_ostream) const {
@@ -323,15 +341,11 @@
 		ini.SetLongValue(product.c_str(), "to_sw_version", to_sw_version);
 	}
 
-	char buff[20];
 	if (this->from_date != UNUSED_TIME) {
-		strftime(buff, 20, "%Y-%m-%d", localtime(&this->from_date));
-		ini.SetValue(product.c_str(), "from_date", buff);
+		ini.SetValue(product.c_str(), "from_date", from_date.c_str());
 	}
-	char buff2[20];
 	if (this->to_date != UNUSED_TIME) {
-		strftime(buff2, 20, "%Y-%m-%d", localtime(&this->to_date));
-		ini.SetValue(product.c_str(), "to_date", buff2);
+		ini.SetValue(product.c_str(), "to_date", to_date.c_str());
 	}
 	if (this->extra_data.length() > 0) {
 		ini.SetValue(product.c_str(), "extra_data", this->extra_data.c_str());
@@ -339,5 +353,13 @@
 	ini.Save(sw);
 }
 
+time_t FullLicenseInfo::expires_on() const {
+	return seconds_from_epoch(this->to_date.c_str());
+}
+
+time_t FullLicenseInfo::valid_from() const {
+	return seconds_from_epoch(this->from_date.c_str());
+}
+
 }
 

--
Gitblit v1.9.1