| | |
| | | #include <iostream> |
| | | #include <string.h> |
| | | #include <iostream> |
| | | #include <string.h> |
| | | #include <boost/date_time.hpp> |
| | | #include <boost/program_options.hpp> |
| | | #include <boost/algorithm/string.hpp> |
| | |
| | | |
| | | 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")) { |
| | | const std::string dt_end = vm["expire_date"].as<string>(); |
| | | try { |
| | | end_date = normalize_date(dt_end.c_str()); |
| | | end_date = normalize_date(dt_end); |
| | | char curdate[20]; |
| | | time_t curtime = time(NULL); |
| | | strftime(curdate, 20, "%Y-%m-%d", localtime(&curtime)); |
| | |
| | | if (vm.count("begin_date")) { |
| | | const std::string begin_date_str = vm["begin_date"].as<string>(); |
| | | try { |
| | | begin_date = normalize_date(begin_date_str.c_str()); |
| | | begin_date = normalize_date(begin_date_str); |
| | | } catch (invalid_argument &e) { |
| | | cerr << endl << "Begin date not recognized: " << begin_date_str |
| | | << " Please enter a valid date in format YYYYMMDD" << endl; |
| | |
| | | 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")) { |
| | |
| | | int LicenseGenerator::generateLicense(int argc, const char **argv) { |
| | | |
| | | po::options_description visibleOptions = configureProgramOptions(); |
| | | //positional options must be addedd to standard options |
| | | //positional options must be added to standard options |
| | | po::options_description allOptions; |
| | | allOptions.add(visibleOptions).add_options()("product", |
| | | po::value<vector<string>>(), "product names"); |
| | |
| | | const std::string formats[] = { "%4u-%2u-%2u", "%4u/%2u/%2u", "%4u%2u%2u" }; |
| | | const size_t formats_n = 3; |
| | | |
| | | string LicenseGenerator::normalize_date(const char * s) { |
| | | string LicenseGenerator::normalize_date(const std::string& sDate) { |
| | | if(sDate.size()<8) |
| | | throw invalid_argument("Date string too small for known formats"); |
| | | unsigned int year, month, day; |
| | | int chread; |
| | | bool found = false; |
| | | for (size_t i = 0; i < formats_n && !found; ++i) { |
| | | chread = sscanf(s, formats[i].c_str(), &year, &month, &day); |
| | | if (chread == 3) { |
| | | const int chread = sscanf(sDate.c_str(),formats[i].c_str(),&year,&month,&day); |
| | | if(chread==3) { |
| | | found = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!found) { |
| | | throw invalid_argument("Date not recognized."); |
| | | } |
| | | if(!found) |
| | | throw invalid_argument("Date string did not match a known format"); |
| | | ostringstream oss; |
| | | oss << year << "-" << setfill('0') << std::setw(2) << month << "-" << day; |
| | | //delete (facet); |
| | | oss << year << "-" << setfill('0') << std::setw(2) << month << "-" << setfill('0') << std::setw(2) << day; |
| | | return oss.str(); |
| | | } |
| | | } |