2018-08-12 16:45:39 +02:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QSettings>
|
2020-08-24 21:36:45 +02:00
|
|
|
#include <QMessageBox>
|
2021-08-07 12:03:35 +02:00
|
|
|
#include <QStandardPaths>
|
2021-09-28 21:44:09 +02:00
|
|
|
#include <QProcess>
|
2022-01-04 23:27:39 +01:00
|
|
|
#include <QDir>
|
2022-01-04 23:44:37 +01:00
|
|
|
#include <QCommandLineParser>
|
2022-05-29 09:50:47 +02:00
|
|
|
#include <QFileInfo>
|
2021-09-28 21:44:09 +02:00
|
|
|
|
2020-05-23 22:52:42 +02:00
|
|
|
#include "mainwindow.h"
|
2018-08-12 16:45:39 +02:00
|
|
|
#include "searchresult.h"
|
2022-01-03 23:02:21 +01:00
|
|
|
#include "previewresultpdf.h"
|
2020-05-23 22:52:42 +02:00
|
|
|
#include "../shared/common.h"
|
2022-04-14 15:04:16 +02:00
|
|
|
#include "../shared/sandboxedprocessor.h"
|
2021-12-26 18:21:48 +01:00
|
|
|
#include "../submodules/exile.h/exile.h"
|
2021-09-28 21:44:09 +02:00
|
|
|
#include "ipcserver.h"
|
2020-05-23 22:52:42 +02:00
|
|
|
|
2022-05-29 09:50:47 +02:00
|
|
|
void enableIpcSandbox()
|
2022-05-29 09:23:37 +02:00
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_create_policy();
|
|
|
|
if(policy == NULL)
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to init policy for sandbox";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2022-06-06 22:11:58 +02:00
|
|
|
policy->namespace_options = EXILE_UNSHARE_USER | EXILE_UNSHARE_MOUNT | EXILE_UNSHARE_NETWORK;
|
2022-05-29 09:23:37 +02:00
|
|
|
policy->no_new_privs = 1;
|
|
|
|
policy->drop_caps = 1;
|
2022-06-09 08:11:40 +02:00
|
|
|
policy->vow_promises = exile_vows_from_str("thread cpath rpath unix stdio proc error");
|
2022-06-06 22:11:58 +02:00
|
|
|
policy->mount_path_policies_to_chroot = 1;
|
2022-05-29 09:23:37 +02:00
|
|
|
|
2022-05-29 09:50:47 +02:00
|
|
|
QString ipcSocketPath = Common::ipcSocketPath();
|
|
|
|
QFileInfo info{ipcSocketPath};
|
|
|
|
QString ipcSocketPathDir = info.absolutePath();
|
|
|
|
std::string stdIpcSocketPath = ipcSocketPathDir.toStdString();
|
|
|
|
|
2022-06-09 08:11:40 +02:00
|
|
|
/* we only need the 'server' side of the 'unix' vow (for unix sockets)'. The process
|
|
|
|
* has no business to connect anywhere.
|
|
|
|
*
|
|
|
|
* Maybe this case should be handled by exile at some point, but for now deal with it here */
|
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(connect), EXILE_SYSCALL_DENY_RET_ERROR, NULL, 0);
|
|
|
|
|
2022-06-06 22:11:58 +02:00
|
|
|
/* ALLOW_EXEC is needed for fallback, not in landlock mode. It does not allow executing anything though here
|
|
|
|
* due to the vows */
|
|
|
|
exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ | EXILE_FS_ALLOW_EXEC, "/");
|
|
|
|
exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ | EXILE_FS_ALLOW_ALL_WRITE | EXILE_FS_ALLOW_EXEC,
|
|
|
|
stdIpcSocketPath.c_str());
|
2022-05-29 09:23:37 +02:00
|
|
|
int ret = exile_enable_policy(policy);
|
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-06-09 08:11:40 +02:00
|
|
|
qDebug() << "Failed to establish sandbox" << Qt::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Arguments are irrelevant for sandbox test, just want to silence analyzer/compiler warnings */
|
|
|
|
ret = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(ret != -1 || errno != EACCES)
|
|
|
|
{
|
|
|
|
qCritical() << "Sandbox sanity check failed" << Qt::endl;
|
2022-05-29 09:23:37 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2022-06-09 08:11:40 +02:00
|
|
|
|
|
|
|
const struct sockaddr *addr = {};
|
|
|
|
ret = connect(3, addr, sizeof(*addr));
|
|
|
|
if(ret != -1 || errno != EACCES)
|
|
|
|
{
|
|
|
|
qCritical() << "Sandbox sanity check failed" << Qt::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2022-05-29 09:23:37 +02:00
|
|
|
exile_free_policy(policy);
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:27:39 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2022-05-29 09:50:47 +02:00
|
|
|
QString socketPath = Common::ipcSocketPath();
|
2022-01-04 23:27:39 +01:00
|
|
|
if(argc > 1)
|
|
|
|
{
|
|
|
|
QString arg = argv[1];
|
|
|
|
if(arg == "ipc")
|
|
|
|
{
|
2022-01-04 23:44:37 +01:00
|
|
|
Common::setupAppInfo();
|
2022-06-06 23:18:58 +02:00
|
|
|
if(Common::noSandboxModeRequested())
|
|
|
|
{
|
|
|
|
qInfo() << "Launching with no sandbox!" << Qt::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enableIpcSandbox();
|
|
|
|
}
|
2022-06-09 08:11:40 +02:00
|
|
|
QCoreApplication a(argc, argv);
|
2022-01-04 23:44:37 +01:00
|
|
|
|
2022-01-04 23:27:39 +01:00
|
|
|
IpcServer *ipcserver = new IpcServer();
|
2022-01-04 23:44:37 +01:00
|
|
|
qDebug() << "Launching IPC Server";
|
2022-01-04 23:27:39 +01:00
|
|
|
if(!ipcserver->startSpawner(socketPath))
|
|
|
|
{
|
2022-06-06 22:11:58 +02:00
|
|
|
qCritical() << "Error failed to spawn" << Qt::endl;
|
2022-01-04 23:27:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2022-01-04 23:44:37 +01:00
|
|
|
qDebug() << "Launched IPC Server";
|
|
|
|
return a.exec();
|
2022-01-04 23:27:39 +01:00
|
|
|
}
|
2022-04-14 15:04:16 +02:00
|
|
|
if(arg == "process")
|
|
|
|
{
|
|
|
|
Common::setupAppInfo();
|
2022-05-30 19:39:43 +02:00
|
|
|
QCoreApplication a(argc, argv);
|
2022-04-14 15:04:16 +02:00
|
|
|
|
|
|
|
QStringList args = a.arguments();
|
2022-05-30 19:39:43 +02:00
|
|
|
if(args.length() < 3)
|
2022-04-14 15:04:16 +02:00
|
|
|
{
|
|
|
|
qDebug() << "Filename is required";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-30 19:39:43 +02:00
|
|
|
QString file = args.at(2);
|
2022-04-14 15:04:16 +02:00
|
|
|
SandboxedProcessor processor(file);
|
|
|
|
return processor.process();
|
|
|
|
}
|
2022-01-04 23:27:39 +01:00
|
|
|
}
|
2022-05-29 09:50:47 +02:00
|
|
|
QString ipcSocketPath = Common::ipcSocketPath();
|
|
|
|
QFileInfo info{ipcSocketPath};
|
|
|
|
QString ipcSocketPathDir = info.absolutePath();
|
|
|
|
|
|
|
|
QDir dir;
|
|
|
|
if(!dir.mkpath(ipcSocketPathDir))
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to create dir for ipc socket" << Qt::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:27:39 +01:00
|
|
|
QProcess process;
|
|
|
|
QStringList args;
|
|
|
|
args << "ipc";
|
2022-05-27 10:51:02 +02:00
|
|
|
process.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
|
|
|
|
process.start("/proc/self/exe", args);
|
|
|
|
if(!process.waitForStarted(5000))
|
2022-01-04 23:27:39 +01:00
|
|
|
{
|
|
|
|
QString errorMsg = "Failed to start IPC server";
|
|
|
|
qDebug() << errorMsg;
|
|
|
|
QMessageBox::critical(nullptr, "Error", errorMsg);
|
|
|
|
}
|
2020-05-23 22:52:42 +02:00
|
|
|
Common::setupAppInfo();
|
2022-01-04 23:44:37 +01:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.addOption({{"s", "no-sandbox"}, "Disable sandboxing"});
|
|
|
|
QStringList appArgs;
|
|
|
|
for(int i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
appArgs.append(argv[i]);
|
|
|
|
}
|
|
|
|
parser.parse(appArgs);
|
|
|
|
|
2020-08-24 21:36:45 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Common::ensureConfigured();
|
|
|
|
}
|
2021-06-12 14:59:58 +02:00
|
|
|
catch(LooqsGeneralException &e)
|
2020-08-24 21:36:45 +02:00
|
|
|
{
|
|
|
|
qDebug() << e.message;
|
|
|
|
QMessageBox::critical(nullptr, "Error", e.message);
|
|
|
|
return 1;
|
|
|
|
}
|
2022-04-24 17:13:43 +02:00
|
|
|
QApplication a(argc, argv);
|
2022-06-06 15:45:59 +02:00
|
|
|
a.setWindowIcon(QIcon(":/looqs.svg"));
|
2022-05-27 10:51:02 +02:00
|
|
|
QObject::connect(&a, &QApplication::aboutToQuit, &process, &QProcess::kill);
|
2022-04-24 17:13:43 +02:00
|
|
|
|
2018-08-12 16:45:39 +02:00
|
|
|
qRegisterMetaType<QVector<SearchResult>>("QVector<SearchResult>");
|
2022-01-03 23:02:21 +01:00
|
|
|
qRegisterMetaType<QVector<PreviewResultPdf>>("QVector<PreviewResultPdf>");
|
|
|
|
qRegisterMetaType<PreviewResultPdf>("PreviewResultPdf");
|
2022-04-14 15:04:16 +02:00
|
|
|
qRegisterMetaType<FileScanResult>("FileScanResult");
|
2022-05-27 09:31:21 +02:00
|
|
|
qRegisterMetaType<RenderConfig>("RenderConfig");
|
|
|
|
qRegisterMetaType<QVector<RenderTarget>>("QVector<RenderTarget>");
|
|
|
|
qRegisterMetaType<QSharedPointer<PreviewResult>>("QSharedPointer<PreviewResult>");
|
2022-06-04 15:47:06 +02:00
|
|
|
MainWindow *w = new MainWindow{0, socketPath};
|
|
|
|
w->showMaximized();
|
|
|
|
int ret = a.exec();
|
|
|
|
process.waitForFinished(1000);
|
|
|
|
return ret;
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|