AuroraRuntime/Source/Processes/AuOpen.Unix.cpp

225 lines
7.2 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2022-11-17 07:46:07 +00:00
File: AuOpen.Unix.cpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
2022-11-17 07:46:07 +00:00
#include "AuProcesses.hpp"
#include "AuOpen.Unix.hpp"
2021-06-27 21:25:29 +00:00
#include <unistd.h>
#include <Source/IO/FS/FS.hpp>
2021-06-27 21:25:29 +00:00
namespace Aurora::Processes
{
static void UnixOpenAsyncThread(AuString uri, int iType)
2021-06-27 21:25:29 +00:00
{
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;
}
}
2024-01-21 18:30:14 +00:00
if constexpr (AuBuild::kIsXnuDerived)
2021-06-27 21:25:29 +00:00
{
2024-01-21 18:30:14 +00:00
// 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)
2024-01-21 18:30:14 +00:00
{
setsid();
// isn't posix fun?
PosixDoForkHooks();
PosixShutup();
2024-04-14 18:48:40 +00:00
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)
2024-01-21 18:30:14 +00:00
{
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
2024-01-21 18:30:14 +00:00
execlp("gdbus",
"gdbus",
"call",
"--session",
"--dest", "org.freedesktop.portal.Desktop",
"--object-path", "/org/freedesktop/portal/desktop",
"--method", pExecString,
2024-01-21 18:30:14 +00:00
"",
pBaseURI,
2024-01-21 18:30:14 +00:00
"{}",
(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"))
2024-01-21 18:30:14 +00:00
{
execl("/usr/bin/xdg-open", "xdg-open", pBaseURI, (char *)nullptr);
2024-01-21 18:30:14 +00:00
}
else if (AuFS::FileExists("/usr/local/bin/xdg-open"))
{
execl("/usr/local/bin/xdg-open", "xdg-open", pBaseURI, (char *)nullptr);
2024-01-21 18:30:14 +00:00
}
else
{
execlp("xdg-open", "xdg-open", pBaseURI, (char *)nullptr);
2024-01-21 18:30:14 +00:00
}
}
exit(0);
}
else if (iFork > 0)
{
semaphore->Lock();
}
2021-06-27 21:25:29 +00:00
}
}
static void UnixOpenAsync(const AuROString &uri, int iType)
2021-06-27 21:25:29 +00:00
{
if (uri.empty())
2023-09-09 22:57:01 +00:00
{
return;
}
AuThreads::Spawn(std::bind(&UnixOpenAsyncThread, AuString(uri), iType), true);
}
2023-09-09 22:57:01 +00:00
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);
2021-06-27 21:25:29 +00:00
}
}