2021-08-07 18:38:23 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDataStream>
|
|
|
|
#include "sandboxedprocessor.h"
|
|
|
|
#include "pdfprocessor.h"
|
|
|
|
#include "defaulttextprocessor.h"
|
|
|
|
#include "tagstripperprocessor.h"
|
|
|
|
#include "nothingprocessor.h"
|
|
|
|
#include "odtprocessor.h"
|
|
|
|
#include "odsprocessor.h"
|
2021-12-26 18:21:48 +01:00
|
|
|
#include "../submodules/exile.h/exile.h"
|
2022-05-30 20:03:40 +02:00
|
|
|
#include "common.h"
|
2021-08-07 18:38:23 +02:00
|
|
|
#include "logger.h"
|
|
|
|
|
|
|
|
static DefaultTextProcessor *defaultTextProcessor = new DefaultTextProcessor();
|
|
|
|
static TagStripperProcessor *tagStripperProcessor = new TagStripperProcessor();
|
|
|
|
static NothingProcessor *nothingProcessor = new NothingProcessor();
|
|
|
|
static OdtProcessor *odtProcessor = new OdtProcessor();
|
|
|
|
static OdsProcessor *odsProcessor = new OdsProcessor();
|
|
|
|
|
|
|
|
static QMap<QString, Processor *> processors{
|
|
|
|
{"pdf", new PdfProcessor()}, {"txt", defaultTextProcessor}, {"md", defaultTextProcessor},
|
|
|
|
{"py", defaultTextProcessor}, {"xml", nothingProcessor}, {"html", tagStripperProcessor},
|
|
|
|
{"java", defaultTextProcessor}, {"js", defaultTextProcessor}, {"cpp", defaultTextProcessor},
|
|
|
|
{"c", defaultTextProcessor}, {"sql", defaultTextProcessor}, {"odt", odtProcessor},
|
2022-05-30 19:51:13 +02:00
|
|
|
{"ods", odsProcessor}, {"svg", nothingProcessor}};
|
2021-08-07 18:38:23 +02:00
|
|
|
|
|
|
|
void SandboxedProcessor::enableSandbox(QString readablePath)
|
|
|
|
{
|
2022-06-06 23:18:58 +02:00
|
|
|
if(Common::noSandboxModeRequested())
|
|
|
|
{
|
|
|
|
qInfo() << "Sandbox is disabled!" << Qt::endl;
|
|
|
|
return;
|
|
|
|
}
|
2021-12-26 18:21:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-12-30 12:31:32 +01:00
|
|
|
if(policy == NULL)
|
|
|
|
{
|
2022-06-09 09:37:10 +02:00
|
|
|
qCritical() << "Could not init exile" << Qt::endl;
|
2021-12-30 12:31:32 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-12-26 18:21:48 +01:00
|
|
|
policy->namespace_options = EXILE_UNSHARE_NETWORK | EXILE_UNSHARE_USER;
|
2021-08-07 18:38:23 +02:00
|
|
|
|
2022-05-31 11:23:21 +02:00
|
|
|
std::string readablePathLocation;
|
2021-08-07 18:38:23 +02:00
|
|
|
if(!readablePath.isEmpty())
|
|
|
|
{
|
2022-06-09 09:37:10 +02:00
|
|
|
policy->namespace_options |= EXILE_UNSHARE_MOUNT;
|
|
|
|
policy->mount_path_policies_to_chroot = 1;
|
2022-05-31 11:23:21 +02:00
|
|
|
readablePathLocation = readablePath.toStdString();
|
2022-04-14 14:57:16 +02:00
|
|
|
if(exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ, readablePathLocation.c_str()) != 0)
|
2021-12-30 12:31:32 +01:00
|
|
|
{
|
|
|
|
qCritical() << "Failed to add path policies";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-08-07 18:38:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
policy->no_fs = 1;
|
|
|
|
}
|
2021-12-26 18:21:48 +01:00
|
|
|
int ret = exile_enable_policy(policy);
|
2021-08-07 18:38:23 +02:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-05-30 19:51:13 +02:00
|
|
|
qCritical() << "Failed to establish sandbox: " << ret;
|
2021-08-07 18:38:23 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-12-26 18:21:48 +01:00
|
|
|
exile_free_policy(policy);
|
2021-08-07 18:38:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SandboxedProcessor::printResults(const QVector<PageData> &pageData)
|
|
|
|
{
|
|
|
|
QFile fsstdout;
|
|
|
|
fsstdout.open(stdout, QIODevice::WriteOnly);
|
|
|
|
QDataStream stream(&fsstdout);
|
|
|
|
|
|
|
|
for(const PageData &data : pageData)
|
|
|
|
{
|
|
|
|
stream << data;
|
|
|
|
// fsstdout.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
fsstdout.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SandboxedProcessor::process()
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo(this->filePath);
|
2022-05-30 19:51:13 +02:00
|
|
|
Processor *processor = processors.value(fileInfo.suffix(), nullptr);
|
|
|
|
if(processor == nullptr)
|
|
|
|
{
|
2022-05-30 20:03:40 +02:00
|
|
|
/* TODO: Not sandboxed */
|
|
|
|
if(Common::isTextFile(fileInfo))
|
2022-05-30 19:51:13 +02:00
|
|
|
{
|
|
|
|
processor = defaultTextProcessor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(processor == nullptr || processor == nothingProcessor)
|
2021-08-07 18:38:23 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
return NOTHING_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<PageData> pageData;
|
|
|
|
QString absPath = fileInfo.absoluteFilePath();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(processor->PREFERED_DATA_SOURCE == FILEPATH)
|
|
|
|
{
|
|
|
|
/* Read access to FS needed... doh..*/
|
|
|
|
enableSandbox(absPath);
|
|
|
|
pageData = processor->process(absPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QByteArray data = Utils::readFile(absPath);
|
|
|
|
enableSandbox();
|
|
|
|
pageData = processor->process(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(LooqsGeneralException &e)
|
|
|
|
{
|
2022-04-14 14:57:16 +02:00
|
|
|
Logger::error() << "SandboxedProcessor: Error while processing" << absPath << ":" << e.message << Qt::endl;
|
2021-08-07 18:38:23 +02:00
|
|
|
return 3 /* PROCESSFAIL */;
|
|
|
|
}
|
|
|
|
|
|
|
|
printResults(pageData);
|
|
|
|
return 0;
|
|
|
|
}
|