AuroraRuntime/Source/Processes/AuOpen.Unix.cpp

225 lines
7.2 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuOpen.Unix.cpp
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "AuProcesses.hpp"
#include "AuOpen.Unix.hpp"
#include <unistd.h>
#include <Source/IO/FS/FS.hpp>
namespace Aurora::Processes
{
static void UnixOpenAsyncThread(AuString uri, int iType)
{
bool bDirExists {};
bool bFileExists {};
bFileExists = AuIOFS::FileExists(uri);
if (iType)
{
bDirExists = AuIOFS::DirExists(uri);
if (!bFileExists &&
!bDirExists)
{
SysPushErrorGeneric("Exploit attempt? Attempted to open non-existent file/directory. (request: {})", uri);
return;
}
if (bFileExists && !gRuntimeConfig.fio.bBypassInternetBlockCheckOpenFile)
{
if (AuFS::IsFileBlocked(uri))
{
SysPushErrorGeneric("Exploit attempt? Attempted to open untrusted file/directory. (request: {})", uri);
return;
}
}
}
else
{
if (bFileExists)
{
SysPushErrorGeneric("Exploit attempt? Attempted to open existing file/directory via URI ({})", uri);
return;
}
}
if constexpr (AuBuild::kIsXnuDerived)
{
// TODO: MacOS/iOS support
}
else
{
static AuInitOnce gInitOnce;
static bool gUseDShid {};
gInitOnce.Call([]()
{
auto optStringA = AuProcess::EnvironmentGetOne("container");
auto optStringB = AuProcess::EnvironmentGetOne("AURORA_RUNTIME_USE_GDBUS_BIN_TO_PORTAL");
auto optStringC = AuProcess::EnvironmentGetOne("AURORA_RUNTIME_NO_MICROSOFT_REDHAT_SABOTAGE");
bool bIsFireJail = optStringA && optStringA.Value() == "firejail";
bool bIsForcedDBUS = optStringB && optStringB.Value() == "YES";
bool bAllowDbus = !optStringC && (AuFS::FileExists("/usr/bin/gdbus") || AuFS::FileExists("/bin/gdbus"));
if (bIsFireJail || bIsForcedDBUS || bAllowDbus)
{
gUseDShid = true;
}
else
{
gUseDShid = false;
}
});
if (!gUseDShid)
{
if (iType == 2)
{
AuROString out;
if (AuEndsWith(uri, '/'))
{
AuFS::GoUpToSeparator(out, uri);
AuFS::GoUpToSeparator(out, out);
}
else
{
AuFS::GoUpToSeparator(out, uri);
}
uri = AuString(out);
}
}
AuSemaphore semaphore;
volatile int type2 = iType;
auto iFork = fork();
if (iFork == 0)
{
setsid();
// isn't posix fun?
PosixDoForkHooks();
PosixShutup();
PosixFDYeetus();
auto pBaseURI = (char *)SysAllocateLarge(uri.size() + 1);
if (pBaseURI)
{
AuMemcpy(pBaseURI, uri.c_str(), uri.size() + 1);
}
int iType = type2;
// ...super fun
semaphore->Unlock(1);
// ...the original iType and uri buffer will be trashed from this point onwards
// and as if dealing with posix isn't bad enough...
// here's some more redhat/lennart poettering/dbus/xdg bullshid
if (gUseDShid)
{
const char *pExecString {};
bool bFuckRedHat { true };
char incompetentRedCunts[32];
if (iType == 2)
{
pExecString = "org.freedesktop.portal.OpenURI.OpenDirectory";
}
else if (bFileExists || bDirExists)
{
pExecString = "org.freedesktop.portal.OpenURI.OpenFile";
}
else
{
pExecString = "org.freedesktop.portal.OpenURI.OpenURI";
bFuckRedHat = false;
}
// it only gets worse, doesn't it?
if (bFuckRedHat)
{
int fu = PosixOpen(pBaseURI,
O_RDONLY,
0664); // dont care. use a real os that isnt controlled by redhat
snprintf(incompetentRedCunts, 32, "%i", fu);
pBaseURI = incompetentRedCunts;
}
// and now we pray to the demons of dshid
execlp("gdbus",
"gdbus",
"call",
"--session",
"--dest", "org.freedesktop.portal.Desktop",
"--object-path", "/org/freedesktop/portal/desktop",
"--method", pExecString,
"",
pBaseURI,
"{}",
(char *)nullptr);
}
else
{
// how did we go from this to the above redhat bs?
// freetards have no standards whatsoever - in both a literal and figurative sense
if (AuFS::FileExists("/bin/xdg-open"))
{
execl("/bin/xdg-open", "xdg-open", pBaseURI, (char *)nullptr);
}
else if (AuFS::FileExists("/usr/bin/xdg-open"))
{
execl("/usr/bin/xdg-open", "xdg-open", pBaseURI, (char *)nullptr);
}
else if (AuFS::FileExists("/usr/local/bin/xdg-open"))
{
execl("/usr/local/bin/xdg-open", "xdg-open", pBaseURI, (char *)nullptr);
}
else
{
execlp("xdg-open", "xdg-open", pBaseURI, (char *)nullptr);
}
}
exit(0);
}
else if (iFork > 0)
{
semaphore->Lock();
}
}
}
static void UnixOpenAsync(const AuROString &uri, int iType)
{
if (uri.empty())
{
return;
}
AuThreads::Spawn(std::bind(&UnixOpenAsyncThread, AuString(uri), iType), true);
}
AUKN_SYM void OpenUri(const AuROString &uri)
{
UnixOpenAsync(uri, 0);
}
AUKN_SYM void OpenFile(const AuROString &file)
{
UnixOpenAsync(file, 1);
}
AUKN_SYM void RevealInDirectory(const AuROString &file)
{
UnixOpenAsync(file, 2);
}
}