From 911e67dc2540e1974ba67faef9dcfd4f27e52ba6 Mon Sep 17 00:00:00 2001
From: open-license-manager <rillf@maildrop.cc>
Date: 周一, 28 7月 2014 05:41:41 +0800
Subject: [PATCH] out licenses

---
 src/library/os/linux/os-linux.cpp           |    3 
 src/library/base/base.h                     |   10 +
 src/library/pc-identifiers.h                |   15 +++
 src/library/LicenseReader.cpp               |    4 
 src/license-generator/license-generator.cpp |   15 ++-
 src/library/api/Licensepp-features.h        |    4 
 src/license-generator/license-generator.h   |    2 
 src/library/pc-identifiers.c                |  143 ++++++++++++++++++++++++++++++-----
 8 files changed, 153 insertions(+), 43 deletions(-)

diff --git a/src/library/LicenseReader.cpp b/src/library/LicenseReader.cpp
index bbddf04..218c2a8 100644
--- a/src/library/LicenseReader.cpp
+++ b/src/library/LicenseReader.cpp
@@ -66,9 +66,9 @@
 		}
 	}
 	if (has_client_sig) {
-		UserPcIdentifier str_code;
+		PcSignature str_code;
 		strncpy(str_code, client_signature.c_str(), sizeof(str_code));
-		EVENT_TYPE event = validate_user_pc_identifier(str_code);
+		EVENT_TYPE event = validate_pc_signature(str_code);
 		if (event != LICENSE_OK) {
 			er.addEvent(event, SEVERITY_ERROR);
 		}
diff --git a/src/library/api/Licensepp-features.h b/src/library/api/Licensepp-features.h
index 7cd54a3..88e41ab 100644
--- a/src/library/api/Licensepp-features.h
+++ b/src/library/api/Licensepp-features.h
@@ -7,7 +7,7 @@
 #endif
 
 enum FEATURE_NAMES {
-  FEATURE1
-};
+  FEATURE1,
+}
 
 #endif
diff --git a/src/library/base/base.h b/src/library/base/base.h
index fb5270f..5656315 100644
--- a/src/library/base/base.h
+++ b/src/library/base/base.h
@@ -7,15 +7,19 @@
 #endif
 
 #ifdef __unix__
+
 #include <limits.h>
 #define DllExport
-#define MAX_PATH PATH_MAX
-#else
+#ifndef MAX_PATH
+	#define MAX_PATH PATH_MAX
+#endif
+
+#else //windows
 #include <windows.h>
 #define DllExport  __declspec( dllexport )
 #endif
 
-#define _DEBUG
+/* #define _DEBUG */
 /*
 #define cmax(a,b) \
    ({ __typeof__ (a) _a = (a); \
diff --git a/src/library/os/linux/os-linux.cpp b/src/library/os/linux/os-linux.cpp
index 722c8ad..ed3427b 100644
--- a/src/library/os/linux/os-linux.cpp
+++ b/src/library/os/linux/os-linux.cpp
@@ -57,8 +57,7 @@
 		const char* signatureB64) {
 	EVP_MD_CTX *mdctx = NULL;
 
-	char *pubKey = PUBLIC_KEY
-	;
+	char *pubKey = PUBLIC_KEY;
 
 	BIO* bio = BIO_new_mem_buf((void*) (pubKey), strlen(pubKey));
 	RSA *rsa = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL);
diff --git a/src/library/pc-identifiers.c b/src/library/pc-identifiers.c
index e60a5b0..a2f7ad8 100644
--- a/src/library/pc-identifiers.c
+++ b/src/library/pc-identifiers.c
@@ -8,6 +8,7 @@
 #include "os/os.h"
 #include "pc-identifiers.h"
 #include <stdlib.h>
+#include <string.h>
 #include <stdbool.h>
 #include "base/base64.h"
 
@@ -30,11 +31,11 @@
 	} else {
 		required_id_size = disk_num;
 	}
+	int defined_identifiers = *num_identifiers;
+	*num_identifiers = required_id_size;
 	if (identifiers == NULL) {
-		*num_identifiers = required_id_size;
 		return OK;
-	} else if (*num_identifiers < required_id_size) {
-		*num_identifiers = required_id_size;
+	} else if (required_id_size > defined_identifiers) {
 		return BUFFER_TOO_SMALL;
 	}
 	diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo));
@@ -42,7 +43,7 @@
 	adapterInfos = (AdapterInfo*) malloc(adapter_num * sizeof(AdapterInfo));
 	result_adapterInfos = getAdapterInfos(adapterInfos, &adapter_num);
 	for (i = 0; i < disk_num; i++) {
-		for (j = 0; j < adapter_num; i++) {
+		for (j = 0; j < adapter_num; j++) {
 			for (k = 0; k < 6; k++)
 				identifiers[i * adapter_num + j][k] =
 						diskInfos[i].disk_sn[k + 2]
@@ -66,13 +67,15 @@
 	if (result_adapterInfos != OK) {
 		return result_adapterInfos;
 	}
+
+	int defined_adapters = *num_identifiers;
+	*num_identifiers = adapters;
 	if (identifiers == NULL) {
-		*num_identifiers = adapters;
 		return OK;
-	} else if (*num_identifiers < adapters) {
-		*num_identifiers = adapters;
+	} else if (adapters > defined_adapters) {
 		return BUFFER_TOO_SMALL;
 	}
+
 	adapterInfos = (AdapterInfo*) malloc(adapters * sizeof(AdapterInfo));
 	result_adapterInfos = getAdapterInfos(adapterInfos, &adapters);
 	for (j = 0; j < adapters; i++) {
@@ -97,20 +100,22 @@
 		unsigned int * num_identifiers, bool use_label) {
 	size_t disk_num;
 	FUNCTION_RETURN result_diskinfos;
-	unsigned int required_id_size, i, k;
+	unsigned int i, k;
 	DiskInfo * diskInfos;
 
 	result_diskinfos = getDiskInfos(NULL, &disk_num);
 	if (result_diskinfos != OK) {
 		return result_diskinfos;
 	}
+
+	int defined_identifiers = *num_identifiers;
+	*num_identifiers = disk_num;
 	if (identifiers == NULL) {
-		*num_identifiers = disk_num;
 		return OK;
-	} else if (*num_identifiers < disk_num) {
-		*num_identifiers = disk_num;
+	} else if (disk_num > defined_identifiers) {
 		return BUFFER_TOO_SMALL;
 	}
+
 	diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo));
 	result_diskinfos = getDiskInfos(diskInfos, &disk_num);
 
@@ -134,6 +139,9 @@
  * for instance a machine with more than one disk and one network interface has
  * usually multiple identifiers.
  *
+ * First 4 bit of each pc identifier are reserved 3 for the type of strategy
+ * used in calculation and 1 for parity checks (not implemented here)
+ *
  * @param identifiers
  * @param array_size
  * @param
@@ -142,7 +150,8 @@
 FUNCTION_RETURN generate_pc_id(PcIdentifier * identifiers,
 		unsigned int * array_size, IDENTIFICATION_STRATEGY strategy) {
 	FUNCTION_RETURN result;
-	unsigned int i;
+	unsigned int i, j;
+	const unsigned int original_array_size = *array_size;
 	unsigned char strategy_num;
 	switch (strategy) {
 	case DEFAULT:
@@ -168,19 +177,90 @@
 		strategy_num = strategy << 5;
 		for (i = 0; i < *array_size; i++) {
 			//encode strategy in the first three bits of the pc_identifier
-			identifiers[i][0] = (identifiers[i][0] & 31) | strategy_num;
+			identifiers[i][0] = (identifiers[i][0] & 15) | strategy_num;
+		}
+		//fill array if larger
+		for (i = *array_size; i < original_array_size; i++) {
+			identifiers[i][0] = STRATEGY_UNKNOWN;
+			for (j = 1; j < sizeof(PcIdentifier); j++) {
+				identifiers[i][j] = 42; //padding
+			}
 		}
 	}
 	return result;
 }
 
-FUNCTION_RETURN encode_pc_id(PcIdentifier identifier1, PcIdentifier identifier2,
-		UserPcIdentifier* pc_identifier_out) {
+char *MakeCRC(char *BitString) {
+	static char Res[3];                                 // CRC Result
+	char CRC[2];
+	int i;
+	char DoInvert;
 
+	for (i = 0; i < 2; ++i)
+		CRC[i] = 0;                    // Init before calculation
+
+	for (i = 0; i < strlen(BitString); ++i) {
+		DoInvert = ('1' == BitString[i]) ^ CRC[1];         // XOR required?
+
+		CRC[1] = CRC[0];
+		CRC[0] = DoInvert;
+	}
+
+	for (i = 0; i < 2; ++i)
+		Res[1 - i] = CRC[i] ? '1' : '0'; // Convert binary to ASCII
+	Res[2] = 0;                                         // Set string terminator
+
+	return (Res);
 }
 
-FUNCTION_RETURN parity_check_id(UserPcIdentifier pc_identifier) {
+FUNCTION_RETURN encode_pc_id(PcIdentifier identifier1, PcIdentifier identifier2,
+		PcSignature pc_identifier_out) {
+	//TODO base62 encoding, now uses base64
+	PcIdentifier concat_identifiers[2];
+	int b64_size = 0;
+	size_t concatIdentifiersSize = sizeof(PcIdentifier) * 2;
+	//concat_identifiers = (PcIdentifier *) malloc(concatIdentifiersSize);
+	memcpy(&concat_identifiers[0], identifier1, sizeof(PcIdentifier));
+	memcpy(&concat_identifiers[1], identifier2, sizeof(PcIdentifier));
+	char* b64_data = base64(concat_identifiers, concatIdentifiersSize,
+			&b64_size);
+	if (b64_size > sizeof(PcSignature)) {
+		return BUFFER_TOO_SMALL;
+	}
+	sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0],
+			&b64_data[4], &b64_data[8], &b64_data[12]);
+	//free(concat_identifiers);
+	free(b64_data);
+	return OK;
+}
 
+FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) {
+	return OK;
+}
+
+FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out,
+		IDENTIFICATION_STRATEGY strategy) {
+	FUNCTION_RETURN result;
+	PcIdentifier* identifiers;
+	unsigned int req_buffer_size = 0;
+	result = generate_pc_id(NULL, &req_buffer_size, strategy);
+	if (result != OK) {
+		return result;
+	}
+	if (req_buffer_size == 0) {
+		return ERROR;
+	}
+	req_buffer_size = req_buffer_size < 2 ? 2 : req_buffer_size;
+	identifiers = (PcIdentifier *) malloc(
+			sizeof(PcIdentifier) * req_buffer_size);
+	result = generate_pc_id(identifiers, &req_buffer_size, strategy);
+	if (result != OK) {
+		free(identifiers);
+		return result;
+	}
+	result = encode_pc_id(identifiers[0], identifiers[1], identifier_out);
+	free(identifiers);
+	return result;
 }
 
 /**
@@ -190,30 +270,47 @@
  * @param str_code: the code in the string format XXXX-XXXX-XXXX-XXXX
  * @return
  */
-static FUNCTION_RETURN decode_pc_id(PcIdentifier* identifier1_out,
-		PcIdentifier* identifier2_out, UserPcIdentifier str_code) {
+static FUNCTION_RETURN decode_pc_id(PcIdentifier identifier1_out,
+		PcIdentifier identifier2_out, PcSignature pc_signature_in) {
+	//TODO base62 encoding, now uses base64
 
+	unsigned char * concat_identifiers;
+	char base64ids[17];
+	int identifiers_size;
+
+	sscanf(pc_signature_in, "%.4s-%.4s-%.4s-%.4s", &base64ids[0], &base64ids[4],
+			&base64ids[8], &base64ids[12]);
+	concat_identifiers = unbase64(base64ids, 16,
+			&identifiers_size);
+	if (identifiers_size > sizeof(PcIdentifier) * 2) {
+		return BUFFER_TOO_SMALL;
+	}
+	memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier));
+	memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier),
+			sizeof(PcIdentifier));
+	free(concat_identifiers);
+	return OK;
 }
 
 static IDENTIFICATION_STRATEGY strategy_from_pc_id(PcIdentifier identifier) {
 	return (IDENTIFICATION_STRATEGY) identifier[0] >> 5;
 }
 
-EVENT_TYPE validate_user_pc_identifier(UserPcIdentifier str_code) {
+EVENT_TYPE validate_pc_signature(PcSignature str_code) {
 	PcIdentifier user_identifiers[2];
 	FUNCTION_RETURN result;
 	IDENTIFICATION_STRATEGY previous_strategy_id, current_strategy_id;
 	PcIdentifier* calculated_identifiers;
-	size_t calc_identifiers_size;
+	unsigned int calc_identifiers_size;
 	int i, j;
-	bool found;
+	//bool found;
 
-	result = decode_pc_id(&user_identifiers[0], &user_identifiers[1], str_code);
+	result = decode_pc_id(user_identifiers[0], user_identifiers[1], str_code);
 	if (result != OK) {
 		return result;
 	}
 	previous_strategy_id = STRATEGY_UNKNOWN;
-	found = false;
+	//found = false;
 	for (i = 0; i < 2; i++) {
 		current_strategy_id = strategy_from_pc_id(user_identifiers[i]);
 		if (current_strategy_id == STRATEGY_UNKNOWN) {
diff --git a/src/library/pc-identifiers.h b/src/library/pc-identifiers.h
index 1b25936..1492395 100644
--- a/src/library/pc-identifiers.h
+++ b/src/library/pc-identifiers.h
@@ -15,13 +15,22 @@
 #endif
 
 typedef unsigned char PcIdentifier[6];
-typedef char UserPcIdentifier[21];
-
+typedef char PcSignature[21];
 
 FUNCTION_RETURN generate_pc_id(PcIdentifier * identifiers, unsigned int * array_size,
 		IDENTIFICATION_STRATEGY strategy);
 
-EVENT_TYPE validate_user_pc_identifier(UserPcIdentifier str_code);
+EVENT_TYPE validate_pc_signature(PcSignature str_code);
+
+/**
+ * Generates an UserPcIdentifier.
+ *
+ * @param identifier_out
+ * @param strategy
+ * @return
+ */
+FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out,
+		IDENTIFICATION_STRATEGY strategy);
 
 #ifdef __cplusplus
 }
diff --git a/src/license-generator/license-generator.cpp b/src/license-generator/license-generator.cpp
index e1656f3..466d5d7 100644
--- a/src/license-generator/license-generator.cpp
+++ b/src/license-generator/license-generator.cpp
@@ -55,7 +55,7 @@
 					" Format YYYYMMDD. If not specified the license won't expire") //
 	("client_signature,s", po::value<string>(),
 			"The signature of the pc that requires the license. "
-					"It should be in the format XXX-XXXX-XXXX."
+					"It should be in the format XXXX-XXXX-XXXX-XXXX."
 					" If not specified the license "
 					"won't be linked to a specific pc.") //
 	("start_version,t", po::value<unsigned int>()->default_value(0
@@ -101,14 +101,15 @@
 	string client_signature = "";
 	if (vm.count("client_signature")) {
 		client_signature = vm["client_signature"].as<string>();
-		regex e("\\d{3}-[:alnum:]{4}-[:alnum:]{4}");
+		//fixme match + and /
+		/*regex e("(A-Za-z0-9){4}-(A-Za-z0-9){4}-(A-Za-z0-9){4}-(A-Za-z0-9){4}");
 		if (!regex_match(client_signature, e)) {
 			cerr << endl << "Client signature not recognized: "
 					<< client_signature
-					<< " Please enter a valid signature in format XXX-XXXX-XXX"
+					<< " Please enter a valid signature in format XXXX-XXXX-XXXX-XXXX"
 					<< endl;
 			exit(2);
-		}
+		}*/
 	}
 	string extra_data = "";
 	if (vm.count("extra_data")) {
@@ -136,7 +137,7 @@
 	return licInfo;
 }
 
-void LicenseGenerator::generateAndOutptuLicenses(const po::variables_map& vm,
+void LicenseGenerator::generateAndOutputLicenses(const po::variables_map& vm,
 		ostream& outputFile) {
 	vector<FullLicenseInfo> licenseInfo = parseLicenseInfo(vm);
 	license::LicenseSigner licSigner =
@@ -179,10 +180,10 @@
 					<< " error: " << strerror( errno);
 			exit(3);
 		}
-		generateAndOutptuLicenses(vm, ofstream);
+		generateAndOutputLicenses(vm, ofstream);
 		ofstream.close();
 	} else {
-		generateAndOutptuLicenses(vm, cout);
+		generateAndOutputLicenses(vm, cout);
 	}
 	return 0;
 }
diff --git a/src/license-generator/license-generator.h b/src/license-generator/license-generator.h
index 5df7744..612c9a6 100644
--- a/src/license-generator/license-generator.h
+++ b/src/license-generator/license-generator.h
@@ -24,7 +24,7 @@
 	static void printHelp(const char* prog_name, const po::options_description& options);
 	static po::options_description configureProgramOptions();
 	static vector<FullLicenseInfo> parseLicenseInfo(po::variables_map vm);
-	static void generateAndOutptuLicenses(const po::variables_map& vm,
+	static void generateAndOutputLicenses(const po::variables_map& vm,
 			ostream& outputFile);
 	static string normalize_date(const std::string& s);
 public:

--
Gitblit v1.9.1