2021-07-03 21:57:53 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2021, Grégory Soutadé
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the copyright holder nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
|
|
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2022-04-03 09:36:23 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <libgen.h>
|
2021-07-03 21:57:53 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
2022-03-16 22:45:33 +01:00
|
|
|
#include <algorithm>
|
2021-07-03 21:57:53 +02:00
|
|
|
|
|
|
|
#include <libgourou.h>
|
2022-04-03 09:36:23 +02:00
|
|
|
#include <libgourou_common.h>
|
|
|
|
|
2021-07-03 21:57:53 +02:00
|
|
|
#include "drmprocessorclientimpl.h"
|
2022-03-16 22:45:33 +01:00
|
|
|
#include "utils_common.h"
|
2021-07-03 21:57:53 +02:00
|
|
|
|
|
|
|
static const char* deviceFile = "device.xml";
|
|
|
|
static const char* activationFile = "activation.xml";
|
|
|
|
static const char* devicekeyFile = "devicesalt";
|
|
|
|
static const char* acsmFile = 0;
|
2021-08-25 21:53:54 +02:00
|
|
|
static bool exportPrivateKey = false;
|
2021-07-03 21:57:53 +02:00
|
|
|
static const char* outputFile = 0;
|
|
|
|
static const char* outputDir = 0;
|
2022-03-23 21:05:56 +01:00
|
|
|
static bool resume = false;
|
2023-09-06 21:09:42 +02:00
|
|
|
static bool notify = true;
|
2021-07-03 21:57:53 +02:00
|
|
|
|
|
|
|
|
2022-03-16 22:45:33 +01:00
|
|
|
class ACSMDownloader
|
2021-07-03 21:57:53 +02:00
|
|
|
{
|
|
|
|
public:
|
2022-03-16 22:45:33 +01:00
|
|
|
|
|
|
|
int run()
|
2021-07-03 21:57:53 +02:00
|
|
|
{
|
2021-07-29 21:14:48 +02:00
|
|
|
int ret = 0;
|
2021-07-03 21:57:53 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
gourou::DRMProcessor processor(&client, deviceFile, activationFile, devicekeyFile);
|
2021-08-25 21:53:54 +02:00
|
|
|
gourou::User* user = processor.getUser();
|
2021-07-08 12:59:56 +02:00
|
|
|
|
2021-08-25 21:53:54 +02:00
|
|
|
if (exportPrivateKey)
|
2021-07-03 21:57:53 +02:00
|
|
|
{
|
2021-08-25 21:53:54 +02:00
|
|
|
std::string filename;
|
|
|
|
if (!outputFile)
|
|
|
|
filename = std::string("Adobe_PrivateLicenseKey--") + user->getUsername() + ".der";
|
2021-09-28 15:14:18 +02:00
|
|
|
else
|
|
|
|
filename = outputFile;
|
2021-07-03 21:57:53 +02:00
|
|
|
|
2021-08-25 21:53:54 +02:00
|
|
|
if (outputDir)
|
|
|
|
{
|
2022-03-16 22:45:33 +01:00
|
|
|
if (!fileExists(outputDir))
|
|
|
|
mkpath(outputDir);
|
2021-08-25 21:53:54 +02:00
|
|
|
|
|
|
|
filename = std::string(outputDir) + "/" + filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
processor.exportPrivateLicenseKey(filename);
|
|
|
|
|
|
|
|
std::cout << "Private license key exported to " << filename << std::endl;
|
|
|
|
}
|
|
|
|
else
|
2021-08-21 20:57:31 +02:00
|
|
|
{
|
2023-09-06 21:09:42 +02:00
|
|
|
gourou::FulfillmentItem* item = processor.fulfill(acsmFile, notify);
|
2021-08-25 21:53:54 +02:00
|
|
|
|
|
|
|
std::string filename;
|
|
|
|
if (!outputFile)
|
|
|
|
{
|
|
|
|
filename = item->getMetadata("title");
|
|
|
|
if (filename == "")
|
|
|
|
filename = "output";
|
2021-11-26 20:00:36 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Remove invalid characters
|
|
|
|
std::replace(filename.begin(), filename.end(), '/', '_');
|
|
|
|
}
|
2021-08-25 21:53:54 +02:00
|
|
|
}
|
2021-08-21 20:57:31 +02:00
|
|
|
else
|
2021-08-25 21:53:54 +02:00
|
|
|
filename = outputFile;
|
|
|
|
|
|
|
|
if (outputDir)
|
|
|
|
{
|
2022-03-16 22:45:33 +01:00
|
|
|
if (!fileExists(outputDir))
|
|
|
|
mkpath(outputDir);
|
2021-08-25 21:53:54 +02:00
|
|
|
|
|
|
|
filename = std::string(outputDir) + "/" + filename;
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:05:56 +01:00
|
|
|
gourou::DRMProcessor::ITEM_TYPE type = processor.download(item, filename, resume);
|
2021-08-25 21:53:54 +02:00
|
|
|
|
|
|
|
if (!outputFile)
|
|
|
|
{
|
|
|
|
std::string finalName = filename;
|
|
|
|
if (type == gourou::DRMProcessor::ITEM_TYPE::PDF)
|
|
|
|
finalName += ".pdf";
|
|
|
|
else
|
|
|
|
finalName += ".epub";
|
2022-03-16 22:45:33 +01:00
|
|
|
rename(filename.c_str(), finalName.c_str());
|
2021-08-25 21:53:54 +02:00
|
|
|
filename = finalName;
|
|
|
|
}
|
|
|
|
std::cout << "Created " << filename << std::endl;
|
2022-04-03 09:36:23 +02:00
|
|
|
|
|
|
|
serializeLoanToken(item);
|
2021-08-21 20:57:31 +02:00
|
|
|
}
|
2021-07-03 21:57:53 +02:00
|
|
|
} catch(std::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << std::endl;
|
2021-07-29 21:14:48 +02:00
|
|
|
ret = 1;
|
2021-07-03 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:45:33 +01:00
|
|
|
return ret;
|
2021-07-03 21:57:53 +02:00
|
|
|
}
|
2022-04-03 09:36:23 +02:00
|
|
|
|
|
|
|
void serializeLoanToken(gourou::FulfillmentItem* item)
|
|
|
|
{
|
|
|
|
gourou::LoanToken* token = item->getLoanToken();
|
|
|
|
|
|
|
|
// No loan token available
|
|
|
|
if (!token)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
|
|
|
pugi::xml_node decl = doc.append_child(pugi::node_declaration);
|
|
|
|
decl.append_attribute("version") = "1.0";
|
|
|
|
|
|
|
|
pugi::xml_node root = doc.append_child("loanToken");
|
|
|
|
gourou::appendTextElem(root, "id", (*token)["id"]);
|
|
|
|
gourou::appendTextElem(root, "operatorURL", (*token)["operatorURL"]);
|
|
|
|
gourou::appendTextElem(root, "validity", (*token)["validity"]);
|
|
|
|
gourou::appendTextElem(root, "name", item->getMetadata("title"));
|
|
|
|
|
|
|
|
char * activationDir = strdup(deviceFile);
|
|
|
|
activationDir = dirname(activationDir);
|
|
|
|
|
|
|
|
gourou::StringXMLWriter xmlWriter;
|
|
|
|
doc.save(xmlWriter, " ");
|
|
|
|
std::string xmlStr = xmlWriter.getResult();
|
|
|
|
|
|
|
|
// Use first bytes of SHA1(id) as filename
|
|
|
|
unsigned char sha1[gourou::SHA1_LEN];
|
|
|
|
client.digest("SHA1", (unsigned char*)(*token)["id"].c_str(), (*token)["id"].size(), sha1);
|
|
|
|
gourou::ByteArray tmp(sha1, sizeof(sha1));
|
|
|
|
std::string filenameHex = tmp.toHex();
|
|
|
|
std::string filename(filenameHex.c_str(), ID_HASH_SIZE);
|
|
|
|
std::string fullPath = std::string(activationDir);
|
|
|
|
fullPath += std::string ("/") + std::string(LOANS_DIR);
|
|
|
|
mkpath(fullPath.c_str());
|
|
|
|
fullPath += filename + std::string(".xml");
|
|
|
|
gourou::writeFile(fullPath, xmlStr);
|
|
|
|
|
|
|
|
std::cout << "Loan token serialized into " << fullPath << std::endl;
|
|
|
|
|
|
|
|
free(activationDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DRMProcessorClientImpl client;
|
2021-07-03 21:57:53 +02:00
|
|
|
};
|
|
|
|
|
2021-07-05 20:22:47 +02:00
|
|
|
|
2021-07-03 21:57:53 +02:00
|
|
|
static void usage(const char* cmd)
|
2023-01-08 21:02:59 +01:00
|
|
|
{
|
|
|
|
std::cout << basename((char*)cmd) << " download EPUB file from ACSM request file" << std::endl << std::endl;
|
|
|
|
std::cout << "Usage: " << basename((char*)cmd) << " [OPTIONS] file.acsm" << std::endl << std::endl;
|
|
|
|
std::cout << "Global Options:" << std::endl;
|
2021-07-03 21:57:53 +02:00
|
|
|
std::cout << " " << "-O|--output-dir" << "\t" << "Optional output directory were to put result (default ./)" << std::endl;
|
2021-08-25 21:53:54 +02:00
|
|
|
std::cout << " " << "-o|--output-file" << "\t" << "Optional output filename (default <title.(epub|pdf|der)>)" << std::endl;
|
2023-01-08 21:02:59 +01:00
|
|
|
std::cout << " " << "-f|--acsm-file" << "\t" << "Backward compatibility: ACSM request file for epub download" << std::endl;
|
2021-08-25 21:53:54 +02:00
|
|
|
std::cout << " " << "-e|--export-private-key"<< "\t" << "Export private key in DER format" << std::endl;
|
2022-03-23 21:05:56 +01:00
|
|
|
std::cout << " " << "-r|--resume" << "\t\t" << "Try to resume download (in case of previous failure)" << std::endl;
|
2023-09-06 21:09:42 +02:00
|
|
|
std::cout << " " << "-N|--no-notify" << "\t\t" << "Don't notify server, even if requested" << std::endl;
|
2021-07-03 21:57:53 +02:00
|
|
|
std::cout << " " << "-v|--verbose" << "\t\t" << "Increase verbosity, can be set multiple times" << std::endl;
|
2021-07-05 20:22:47 +02:00
|
|
|
std::cout << " " << "-V|--version" << "\t\t" << "Display libgourou version" << std::endl;
|
2021-07-03 21:57:53 +02:00
|
|
|
std::cout << " " << "-h|--help" << "\t\t" << "This help" << std::endl;
|
|
|
|
|
2023-01-08 21:02:59 +01:00
|
|
|
std::cout << "ADEPT Options:" << std::endl;
|
|
|
|
std::cout << " " << "-D|--adept-directory" << "\t" << ".adept directory that must contains device.xml, activation.xml and devicesalt" << std::endl;
|
|
|
|
std::cout << " " << "-d|--device-file" << "\t" << "device.xml file from eReader" << std::endl;
|
|
|
|
std::cout << " " << "-a|--activation-file" << "\t" << "activation.xml file from eReader" << std::endl;
|
|
|
|
std::cout << " " << "-k|--device-key-file" << "\t" << "private device key file (eg devicesalt/devkey.bin) from eReader" << std::endl;
|
|
|
|
|
2021-07-03 21:57:53 +02:00
|
|
|
std::cout << std::endl;
|
2023-01-08 21:02:59 +01:00
|
|
|
|
|
|
|
std::cout << "Environment:" << std::endl;
|
|
|
|
std::cout << "Device file, activation file and device key file are optionals. If not set, they are looked into :" << std::endl << std::endl;
|
2023-01-05 21:29:55 +01:00
|
|
|
std::cout << " * $ADEPT_DIR environment variable" << std::endl;
|
|
|
|
std::cout << " * /home/<user>/.config/adept" << std::endl;
|
2021-07-03 21:57:53 +02:00
|
|
|
std::cout << " * Current directory" << std::endl;
|
|
|
|
std::cout << " * .adept" << std::endl;
|
|
|
|
std::cout << " * adobe-digital-editions directory" << std::endl;
|
|
|
|
std::cout << " * .adobe-digital-editions directory" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int c, ret = -1;
|
2022-12-21 21:23:42 +01:00
|
|
|
std::string _deviceFile, _activationFile, _devicekeyFile;
|
2021-07-03 21:57:53 +02:00
|
|
|
|
|
|
|
const char** files[] = {&devicekeyFile, &deviceFile, &activationFile};
|
|
|
|
int verbose = gourou::DRMProcessor::getLogLevel();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
|
|
|
static struct option long_options[] = {
|
2022-12-21 21:23:42 +01:00
|
|
|
{"adept-directory", required_argument, 0, 'D' },
|
2021-07-03 21:57:53 +02:00
|
|
|
{"device-file", required_argument, 0, 'd' },
|
|
|
|
{"activation-file", required_argument, 0, 'a' },
|
|
|
|
{"device-key-file", required_argument, 0, 'k' },
|
|
|
|
{"output-dir", required_argument, 0, 'O' },
|
|
|
|
{"output-file", required_argument, 0, 'o' },
|
|
|
|
{"acsm-file", required_argument, 0, 'f' },
|
2021-08-25 21:53:54 +02:00
|
|
|
{"export-private-key",no_argument, 0, 'e' },
|
2022-03-23 21:05:56 +01:00
|
|
|
{"resume", no_argument, 0, 'r' },
|
2023-09-06 21:09:42 +02:00
|
|
|
{"no-notify", no_argument, 0, 'N' },
|
2021-07-03 21:57:53 +02:00
|
|
|
{"verbose", no_argument, 0, 'v' },
|
2021-07-05 20:22:47 +02:00
|
|
|
{"version", no_argument, 0, 'V' },
|
2021-07-03 21:57:53 +02:00
|
|
|
{"help", no_argument, 0, 'h' },
|
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2023-09-06 21:09:42 +02:00
|
|
|
c = getopt_long(argc, argv, "D:d:a:k:O:o:f:erNvVh",
|
2021-07-03 21:57:53 +02:00
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
2022-12-21 21:23:42 +01:00
|
|
|
case 'D':
|
|
|
|
_deviceFile = std::string(optarg) + "/device.xml";
|
|
|
|
_activationFile = std::string(optarg) + "/activation.xml";
|
|
|
|
_devicekeyFile = std::string(optarg) + "/devicesalt";
|
|
|
|
deviceFile = _deviceFile.c_str();
|
|
|
|
activationFile = _activationFile.c_str();
|
|
|
|
devicekeyFile = _devicekeyFile.c_str();
|
|
|
|
break;
|
2021-07-03 21:57:53 +02:00
|
|
|
case 'd':
|
|
|
|
deviceFile = optarg;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
activationFile = optarg;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
devicekeyFile = optarg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
acsmFile = optarg;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
outputDir = optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
outputFile = optarg;
|
|
|
|
break;
|
2021-08-25 21:53:54 +02:00
|
|
|
case 'e':
|
|
|
|
exportPrivateKey = true;
|
|
|
|
break;
|
2022-03-23 21:05:56 +01:00
|
|
|
case 'r':
|
|
|
|
resume = true;
|
|
|
|
break;
|
2023-09-06 21:09:42 +02:00
|
|
|
case 'N':
|
|
|
|
notify = false;
|
|
|
|
break;
|
2021-07-03 21:57:53 +02:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2021-07-05 20:22:47 +02:00
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
return 0;
|
2021-07-03 21:57:53 +02:00
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gourou::DRMProcessor::setLogLevel(verbose);
|
|
|
|
|
2023-01-08 20:58:41 +01:00
|
|
|
if (optind == argc-1)
|
|
|
|
acsmFile = argv[optind];
|
|
|
|
|
2021-08-25 21:53:54 +02:00
|
|
|
if ((!acsmFile && !exportPrivateKey) || (outputDir && !outputDir[0]) ||
|
2021-07-03 21:57:53 +02:00
|
|
|
(outputFile && !outputFile[0]))
|
|
|
|
{
|
|
|
|
usage(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:45:33 +01:00
|
|
|
ACSMDownloader downloader;
|
2021-07-03 21:57:53 +02:00
|
|
|
|
|
|
|
int i;
|
2021-08-26 08:13:02 +02:00
|
|
|
bool hasErrors = false;
|
|
|
|
const char* orig;
|
2021-07-03 21:57:53 +02:00
|
|
|
for (i=0; i<(int)ARRAY_SIZE(files); i++)
|
|
|
|
{
|
2021-08-26 08:13:02 +02:00
|
|
|
orig = *files[i];
|
2021-07-03 21:57:53 +02:00
|
|
|
*files[i] = findFile(*files[i]);
|
|
|
|
if (!*files[i])
|
|
|
|
{
|
2021-08-26 08:13:02 +02:00
|
|
|
std::cout << "Error : " << orig << " doesn't exists, did you activate your device ?" << std::endl;
|
2021-07-03 21:57:53 +02:00
|
|
|
ret = -1;
|
2021-08-26 08:13:02 +02:00
|
|
|
hasErrors = true;
|
2021-07-03 21:57:53 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-25 21:53:54 +02:00
|
|
|
|
2021-08-26 08:13:02 +02:00
|
|
|
if (hasErrors)
|
|
|
|
goto end;
|
|
|
|
|
2021-08-25 21:53:54 +02:00
|
|
|
if (exportPrivateKey)
|
|
|
|
{
|
|
|
|
if (acsmFile)
|
|
|
|
{
|
|
|
|
usage(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-07-03 21:57:53 +02:00
|
|
|
{
|
2022-03-16 22:45:33 +01:00
|
|
|
if (!fileExists(acsmFile))
|
2021-08-25 21:53:54 +02:00
|
|
|
{
|
|
|
|
std::cout << "Error : " << acsmFile << " doesn't exists" << std::endl;
|
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
2021-07-03 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:45:33 +01:00
|
|
|
ret = downloader.run();
|
2021-07-03 21:57:53 +02:00
|
|
|
|
|
|
|
end:
|
|
|
|
for (i=0; i<(int)ARRAY_SIZE(files); i++)
|
|
|
|
{
|
|
|
|
if (*files[i])
|
|
|
|
free((void*)*files[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|