From 59521087f7c80c40525b17431f2988a815a842a0 Mon Sep 17 00:00:00 2001 From: Maximilien Siavelis <m@siav.pw> Date: 周一, 06 5月 2019 00:51:57 +0800 Subject: [PATCH] Merge pull request #20 from willamowius/master --- src/library/base/logger.c | 10 +- src/library/os/os-linux.c | 18 +++-- src/tools/license-generator/license-generator.cpp | 60 ++++++++++--------- src/library/os/network_id.c | 6 +- src/library/os/os.h | 4 + src/library/ini/SimpleIni.h | 2 src/tools/license-generator/license-generator-main.cpp | 1 test/functional/volid_test.cpp | 2 src/library/base/logger.h | 2 src/library/pc-identifiers.c | 9 ++- src/library/LicenseReader.cpp | 9 ++- test/license-generator/license-generator_test.cpp | 2 src/tools/license-generator/license-generator.h | 2 13 files changed, 71 insertions(+), 56 deletions(-) diff --git a/src/library/LicenseReader.cpp b/src/library/LicenseReader.cpp index 37274f0..06a068c 100644 --- a/src/library/LicenseReader.cpp +++ b/src/library/LicenseReader.cpp @@ -18,6 +18,7 @@ #include <fstream> #include <sstream> #include <stdlib.h> +#include <math.h> #include "pc-identifiers.h" #include "LicenseReader.h" #include "base/StringUtils.h" @@ -69,7 +70,7 @@ } if (has_client_sig) { PcSignature str_code; - strncpy(str_code, client_signature.c_str(), sizeof(str_code)); + strncpy(str_code, client_signature.c_str(), sizeof(str_code)-1); EVENT_TYPE event = validate_pc_signature(str_code); if (event != LICENSE_OK) { er.addEvent(event, SVRT_ERROR); @@ -93,7 +94,7 @@ double secs = difftime( seconds_from_epoch(to_date.c_str()), time(NULL)); - license->days_left = (int) secs / 60 * 60 * 24; + license->days_left = round(secs / (60 * 60 * 24)); } } } @@ -160,9 +161,11 @@ FullLicenseInfo::UNUSED_SOFTWARE_VERSION); int to_sw_version = ini.GetLongValue(productNamePtr, "to_sw_version", FullLicenseInfo::UNUSED_SOFTWARE_VERSION); + string extra_data = trim_copy( + ini.GetValue(productNamePtr, "extra_data", "")); FullLicenseInfo licInfo(*it, product, license_signature, (int) license_version, from_date, to_date, client_signature, - from_sw_version, to_sw_version); + from_sw_version, to_sw_version, extra_data); licenseInfoOut.push_back(licInfo); atLeastOneLicenseComplete = true; } else { diff --git a/src/library/base/logger.c b/src/library/base/logger.c index 47510c2..089fc85 100644 --- a/src/library/base/logger.c +++ b/src/library/base/logger.c @@ -30,18 +30,18 @@ if (folder == 0) { folder = "/tmp"; } - strcpy(logpath, folder); - strcat(logpath, "/open-license.log"); + strncpy(logpath, folder, MAX_PATH); + strncat(logpath, "/open-license.log", MAX_PATH - strlen(logpath)); #else - int plen=GetTempPath(MAX_PATH,logpath); + int plen = GetTempPath(MAX_PATH, logpath); if(plen == 0) { fprintf(stderr, "Error getting temporary directory path"); } - strcat(logpath,"open-license.log"); + strncat(logpath, "open-license.log", MAX_PATH - strlen(logpath)); #endif } -void _log(char* format, ...) { +void _log(const char* format, ...) { char logpath[MAX_PATH]; va_list args; char * buffer; diff --git a/src/library/base/logger.h b/src/library/base/logger.h index cf08405..0803685 100644 --- a/src/library/base/logger.h +++ b/src/library/base/logger.h @@ -25,7 +25,7 @@ #define LOG_ERROR(M, ...) #endif -void _log(char* format, ...); +void _log(const char* format, ...); void _shutdown_log(); #ifdef __cplusplus diff --git a/src/library/ini/SimpleIni.h b/src/library/ini/SimpleIni.h index 05e2631..5b11f4f 100644 --- a/src/library/ini/SimpleIni.h +++ b/src/library/ini/SimpleIni.h @@ -1912,7 +1912,7 @@ if (pComment) { DeleteString(a_pComment); a_pComment = pComment; - CopyString(a_pComment); + (void)CopyString(a_pComment); } Delete(a_pSection, a_pKey); iKey = keyval.end(); diff --git a/src/library/os/network_id.c b/src/library/os/network_id.c index 1c112b2..f50dc64 100644 --- a/src/library/os/network_id.c +++ b/src/library/os/network_id.c @@ -58,7 +58,7 @@ FUNCTION_RETURN f_return = FUNC_RET_OK; struct ifaddrs *ifaddr, *ifa; - int family, i, n, if_name_position; + int family, n = 0, if_name_position; unsigned int if_num, if_max; //char host[NI_MAXHOST]; char *ifnames; @@ -96,7 +96,7 @@ strncpy(&ifnames[if_num * NI_MAXHOST], ifa->ifa_name, NI_MAXHOST); if (adapterInfos != NULL && if_num < *adapter_info_size) { strncpy(adapterInfos[if_num].description, ifa->ifa_name, - NI_MAXHOST); + NI_MAXHOST-1); } if_name_position = if_num; if_num++; @@ -148,7 +148,7 @@ } else if (family == AF_PACKET && ifa->ifa_data != NULL) { struct sockaddr_ll *s1 = (struct sockaddr_ll*) ifa->ifa_addr; if (adapterInfos != NULL && if_name_position < *adapter_info_size) { - for (i = 0; i < 6; i++) { + for (int i = 0; i < 6; i++) { adapterInfos[if_name_position].mac_address[i] = s1->sll_addr[i]; #ifdef _DEBUG diff --git a/src/library/os/os-linux.c b/src/library/os/os-linux.c index 717373a..b4d9441 100644 --- a/src/library/os/os-linux.c +++ b/src/library/os/os-linux.c @@ -65,11 +65,11 @@ struct mntent *ent; int maxDrives, currentDrive, i, drive_found; - __ino64_t *statDrives; - DiskInfo *tmpDrives; - FILE *aFile; - DIR *disk_by_uuid_dir, *disk_by_label; - struct dirent *dir; + __ino64_t *statDrives = NULL; + DiskInfo *tmpDrives = NULL; + FILE *aFile = NULL; + DIR *disk_by_uuid_dir = NULL, *disk_by_label = NULL; + struct dirent *dir = NULL; FUNCTION_RETURN result; if (diskInfos != NULL) { @@ -87,6 +87,8 @@ aFile = setmntent("/proc/mounts", "r"); if (aFile == NULL) { /*proc not mounted*/ + free(tmpDrives); + free(statDrives); return FUNC_RET_ERROR; } @@ -107,7 +109,7 @@ if (drive_found == -1) { LOG_DEBUG("mntent: %s %s %d\n", ent->mnt_fsname, ent->mnt_dir, (unsigned long int)mount_stat.st_ino); - strcpy(tmpDrives[currentDrive].device, ent->mnt_fsname); + strncpy(tmpDrives[currentDrive].device, ent->mnt_fsname, 255-1); statDrives[currentDrive] = mount_stat.st_ino; drive_found = currentDrive; currentDrive++; @@ -167,7 +169,7 @@ if (stat(cur_dir, &sym_stat) == 0) { for (i = 0; i < currentDrive; i++) { if (sym_stat.st_ino == statDrives[i]) { - strncpy(tmpDrives[i].label, dir->d_name, 255); + strncpy(tmpDrives[i].label, dir->d_name, 255-1); printf("label %d %s %s\n", i, tmpDrives[i].label, tmpDrives[i].device); } @@ -288,7 +290,7 @@ strcat(proc_path, pidStr); strcat(proc_path, "/exe"); - int ch = readlink(proc_path, path, MAX_PATH); + int ch = readlink(proc_path, path, MAX_PATH-1); if (ch != -1) { path[ch] = '\0'; strncpy(buffer, path, ch); diff --git a/src/library/os/os.h b/src/library/os/os.h index bd19f9d..d953d1a 100644 --- a/src/library/os/os.h +++ b/src/library/os/os.h @@ -14,6 +14,10 @@ #include "../base/base.h" #include <stddef.h> +#include <string.h> +#include <ctype.h> +#include <sys/types.h> +#include <unistd.h> //definition of size_t #include <stdlib.h> #ifdef __unix__ diff --git a/src/library/pc-identifiers.c b/src/library/pc-identifiers.c index 7332f4f..8353f55 100644 --- a/src/library/pc-identifiers.c +++ b/src/library/pc-identifiers.c @@ -179,7 +179,7 @@ if (use_label) { if (diskInfos[i].label[0] != 0) { memset(identifiers[j], 0, sizeof(PcIdentifier)); //!!!!!!! - strncpy(identifiers[j], diskInfos[i].label, + strncpy((char*)identifiers[j], diskInfos[i].label, sizeof(PcIdentifier)); j++; } @@ -281,7 +281,7 @@ PcSignature pc_identifier_out) { //TODO base62 encoding, now uses base64 PcIdentifier concat_identifiers[2]; - char* b64_data; + char* b64_data = NULL; int b64_size = 0; size_t concatIdentifiersSize = sizeof(PcIdentifier) * 2; //concat_identifiers = (PcIdentifier *) malloc(concatIdentifiersSize); @@ -289,6 +289,7 @@ memcpy(&concat_identifiers[1], identifier2, sizeof(PcIdentifier)); b64_data = base64(concat_identifiers, concatIdentifiersSize, &b64_size); if (b64_size > sizeof(PcSignature)) { + free(b64_data); return FUNC_RET_BUFFER_TOO_SMALL; } sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0], @@ -345,7 +346,7 @@ PcIdentifier identifier2_out, PcSignature pc_signature_in) { //TODO base62 encoding, now uses base64 - unsigned char * concat_identifiers; + unsigned char * concat_identifiers = NULL; char base64ids[17]; int identifiers_size; @@ -353,6 +354,7 @@ &base64ids[8], &base64ids[12]); concat_identifiers = unbase64(base64ids, 16, &identifiers_size); if (identifiers_size > sizeof(PcIdentifier) * 2) { + free(concat_identifiers); return FUNC_RET_BUFFER_TOO_SMALL; } memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier)); @@ -386,6 +388,7 @@ for (i = 0; i < 2; i++) { current_strategy_id = strategy_from_pc_id(user_identifiers[i]); if (current_strategy_id == STRATEGY_UNKNOWN) { + free(calculated_identifiers); return LICENSE_MALFORMED; } if (current_strategy_id != previous_strategy_id) { diff --git a/src/tools/license-generator/license-generator-main.cpp b/src/tools/license-generator/license-generator-main.cpp index f167678..39dc455 100644 --- a/src/tools/license-generator/license-generator-main.cpp +++ b/src/tools/license-generator/license-generator-main.cpp @@ -2,7 +2,6 @@ #include "license-generator.h" int main(int argc, const char *argv[]) { - //license::LicenseGenerator lic; return license::LicenseGenerator::generateLicense(argc, argv); } diff --git a/src/tools/license-generator/license-generator.cpp b/src/tools/license-generator/license-generator.cpp index 3b331ef..f6efc9c 100644 --- a/src/tools/license-generator/license-generator.cpp +++ b/src/tools/license-generator/license-generator.cpp @@ -40,38 +40,43 @@ po::options_description LicenseGenerator::configureProgramOptions() { po::options_description common("General options"); - common.add_options()("help,h", "print help message and exit.") // - ("verbose,v", "print more information.") // - ("output,o", po::value<string>(), "Output file name. If not specified the " - "license will be printed in standard output"); // + common.add_options() + ("help,h", "print help message and exit.") + ("verbose,v", "print more information.") + ("output,o", po::value<string>(), "Output file name. If not specified the " + "license will be printed in standard output") + ; po::options_description licenseGeneration("License Generation"); - licenseGeneration.add_options()("private_key,p", po::value<string>(), + licenseGeneration.add_options() + ("private_key,p", po::value<string>(), "Specify an alternate file for the primary key to be used. " - "If not specified the internal primary key will be used.") // - ("begin_date,b", po::value<string>(), + "If not specified the internal primary key will be used.") + ("begin_date,b", po::value<string>(), "Specify the start of the validity for this license. " - " Format YYYYMMDD. If not specified defaults to today") // - ("expire_date,e", po::value<string>(), + " Format YYYYMMDD. If not specified defaults to today") + ("expire_date,e", po::value<string>(), "Specify the expire date for this license. " - " Format YYYYMMDD. If not specified the license won't expire") // - ("client_signature,s", po::value<string>(), + " 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 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 - /*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"), - "Specify the first version of the software this license apply to.") // - ("end_version,n", po::value<unsigned int>()->default_value(0 - /*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"), - "Specify the last version of the software this license apply to."); // + "won't be linked to a specific pc.") + ("start_version,t", po::value<unsigned int>()->default_value(0 + /*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"), + "Specify the first version of the software this license apply to.") + ("end_version,n", po::value<unsigned int>()->default_value(0 + /*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"), + "Specify the last version of the software this license apply to.") + ("extra_data,x", po::value<string>(), "Specify extra data to be included into the license") + ; po::options_description visibleOptions; visibleOptions.add(common).add(licenseGeneration); return visibleOptions; } vector<FullLicenseInfo> LicenseGenerator::parseLicenseInfo( - po::variables_map vm) { + const po::variables_map& vm) { string begin_date = FullLicenseInfo::UNUSED_TIME; string end_date = FullLicenseInfo::UNUSED_TIME; if (vm.count("expire_date")) { @@ -102,15 +107,14 @@ string client_signature = ""; if (vm.count("client_signature")) { client_signature = vm["client_signature"].as<string>(); - //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 XXXX-XXXX-XXXX-XXXX" - << endl; - exit(2); - }*/ + 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 XXXX-XXXX-XXXX-XXXX" + << endl; + exit(2); + } } string extra_data = ""; if (vm.count("extra_data")) { diff --git a/src/tools/license-generator/license-generator.h b/src/tools/license-generator/license-generator.h index 91bccea..361f263 100644 --- a/src/tools/license-generator/license-generator.h +++ b/src/tools/license-generator/license-generator.h @@ -23,7 +23,7 @@ LicenseGenerator(); 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 vector<FullLicenseInfo> parseLicenseInfo(const po::variables_map& vm); static void generateAndOutputLicenses(const po::variables_map& vm, ostream& outputFile); static string normalize_date(const std::string& s); diff --git a/test/functional/volid_test.cpp b/test/functional/volid_test.cpp index 3de6bbf..2de9a04 100644 --- a/test/functional/volid_test.cpp +++ b/test/functional/volid_test.cpp @@ -109,7 +109,7 @@ continue; PcSignature pcsig; strncpy(pcsig, reference_signatures[i].c_str(), - sizeof(PcSignature)); + sizeof(PcSignature)-1); EVENT_TYPE val_result = validate_pc_signature(pcsig); BOOST_TEST_CHECKPOINT("Verifying signature: "); BOOST_CHECK_EQUAL(val_result, LICENSE_OK); diff --git a/test/license-generator/license-generator_test.cpp b/test/license-generator/license-generator_test.cpp index 3cf21b3..7410dc9 100644 --- a/test/license-generator/license-generator_test.cpp +++ b/test/license-generator/license-generator_test.cpp @@ -19,7 +19,7 @@ argv[2] = fname.c_str(); argv[3] = "test"; int retCode = LicenseGenerator::generateLicense(argc, argv); - delete (argv); + delete[] (argv); BOOST_CHECK_EQUAL(retCode, 0); BOOST_ASSERT(fs::exists(fname)); CSimpleIniA ini; -- Gitblit v1.9.1