Skip to content

Commit

Permalink
fix macos elevation
Browse files Browse the repository at this point in the history
  • Loading branch information
moonshadow565 committed Aug 8, 2023
1 parent 1b4143c commit c128777
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 81 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/builditmac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ jobs:
- name: "Package"
shell: bash
run: |
mkdir cslol-manager-macos
cp LICENSE cslol-manager-macos/
cp dist/SOURCE.URL cslol-manager-macos/
cp build/cslol-tools/wad-* cslol-manager-macos/
cp -R build/cslol-manager.app cslol-manager-macos/
cp build/cslol-manager-elevate cslol-manager-macos/cslol-manager.app/Contents/MacOS/
plutil -replace CFBundleExecutable -string cslol-manager-elevate cslol-manager-macos/cslol-manager.app/Contents/Info.plist
plutil -replace CFBundleName -string cslol-manager-elevate cslol-manager-macos/cslol-manager.app/Contents/Info.plist
mkdir cslol-manager-macos/cslol-manager.app/Contents/MacOS/cslol-tools
cp build/cslol-tools/mod-tools cslol-manager-macos/cslol-manager.app/Contents/MacOS/cslol-tools
./make-release-mac.sh "build" "cslol-manager-macos"
tar caf cslol-manager-macos.tar.xz cslol-manager-macos/
- name: 'Upload Artifact'
uses: actions/upload-artifact@v2
Expand Down
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ elseif(APPLE)
src/main.cpp
src/res/icon.icns
)
add_executable(cslol-manager-elevate
src/main_elevate.c
)
target_link_libraries(cslol-manager PRIVATE "-framework Security")
set_target_properties(cslol-manager
PROPERTIES
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/src/res/MacOSXBundleInfo.plist.in"
Expand Down
2 changes: 1 addition & 1 deletion cslol-tools/lib/lol/io/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static struct ResourceLimit {
public: ResourceLimit() {
rlimit resourceLimit;
getrlimit(RLIMIT_NOFILE, &resourceLimit);
resourceLimit.rlim_cur = resourceLimit.rlim_max;
resourceLimit.rlim_cur = resourceLimit.rlim_max == RLIM_INFINITY ? 65000 : resourceLimit.rlim_max - 1;
setrlimit(RLIMIT_NOFILE, &resourceLimit);
}
} resourceLimit_ = {};
Expand Down
4 changes: 2 additions & 2 deletions cslol-tools/lib/lol/patcher/utility/delay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace lol {

Intervals() -> Intervals<1>;

template <std::size_t S>
inline auto run_until_or(auto deadline, Intervals<S> intervals, auto&& poll, auto&& fail) {
template <typename D, std::size_t S, typename P, typename F>
inline auto run_until_or(D deadline, Intervals<S> intervals, P&& poll, F&& fail) {
auto total = static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(deadline).count());
for (auto interval : intervals.intervals) {
auto slice = total / S;
Expand Down
2 changes: 1 addition & 1 deletion cslol-tools/lib/lol/utility/zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ auto utility::unzip(fs::path const& src, fs::path const& dst) -> void {
mz_zip_archive_file_stat stat = {};
lol_throw_if_zip(mz_zip_reader_file_stat, zip.get(), index, &stat);
if (stat.m_is_directory || !stat.m_is_supported) continue;
auto file_path = fs::path((char8_t const*)stat.m_filename).lexically_normal();
auto file_path = fs::path((char const*)stat.m_filename).lexically_normal();
for (auto const& component : file_path) {
lol_throw_if(component == "..");
}
Expand Down
8 changes: 6 additions & 2 deletions cslol-tools/lib/lol/wad/toc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ namespace lol::wad {

inline bool is_wad() const noexcept { return magic == std::array{'R', 'W'}; }

constexpr bool operator==(Version const&) const noexcept = default;
constexpr auto operator<=>(Version const&) const noexcept = default;
bool operator==(Version const& other) const noexcept {
return magic == other.magic && major == other.major && minor == other.minor;
}
bool operator!=(Version const& other) const noexcept {
return !(*this == other);
}
};

using Signature = std::array<std::uint8_t, 16>;
Expand Down
2 changes: 1 addition & 1 deletion cslol-tools/src/main_wad_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static auto wad_extract(fs::path src, fs::path dst, fs::path hashdict) -> void {
if (dst.empty()) {
dst = src;
if (dst.extension().empty()) {
dst.replace_extension(u8".wad");
dst.replace_extension(".wad");
} else {
dst.replace_extension();
}
Expand Down
24 changes: 24 additions & 0 deletions make-release-mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

copy2folder() {
mkdir -p "$2"

cp "./dist/SOURCE.URL" "$2"
cp "./LICENSE" "$2"

cp -R "$1/cslol-manager.app" "$2"
mkdir -p "$2/cslol-manager.app/Contents/MacOS/cslol-tools"
cp "$1/cslol-tools/mod-tools" "$2/cslol-manager.app/Contents/MacOS/cslol-tools"
cp "$1/cslol-tools/wad-"* "$2"
}

VERSION=$(git log --date=short --format="%ad-%h" -1)
echo "Version: $VERSION"

if [ "$#" -gt 0 ] && [ -d "$1" ]; then
copy2folder "$1" "cslol-manager-macos"
echo "Version: $VERSION" > "cslol-manager-macos/version.txt"
else
echo "Error: Provide at least one valid path."
exit
fi;
8 changes: 4 additions & 4 deletions src/CSLOLToolsImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ void CSLOLToolsImpl::init() {
if (state_ == CSLOLState::StateUnitialized) {
setState(CSLOLState::StateBusy);

if (CSLOLUtils utils{}; utils.isUnnecessaryAdmin()) {
doReportError("Unnecessary admin",
"Try running without admin at least once.\nIf this issue still persist import FIX-ADMIN.reg",
"Running as admin is disabled by default");
if (QString error = CSLOLUtils::isPlatformUnsuported(); !error.isEmpty()) {
doReportError("Unsupported platform",
error,
"Application launched on unsupported machine or configuration.");
setState(CSLOLState::StateCriticalError);
return;
}
Expand Down
81 changes: 73 additions & 8 deletions src/CSLOLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,38 @@ QString CSLOLUtils::detectGamePath() {
return "";
}

bool CSLOLUtils::isUnnecessaryAdmin() {
return false;
QString CSLOLUtils::isPlatformUnsuported() {
if (QFileInfo info(QCoreApplication::applicationDirPath() + "/admin_allowed.txt"); info.exists()) {
return false;
return "";
}
bool result = 0;

QString result{""};
return result;

HANDLE token = {};
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
TOKEN_ELEVATION elevation = {};
DWORD size = sizeof(TOKEN_ELEVATION);
if (GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &size)) {
result = elevation.TokenIsElevated;
if (elevation.TokenIsElevated) {
result = "Try running without admin at least once.\nIf this issue still persist import FIX-ADMIN.reg";
}
}
CloseHandle(token);
}
return result;
}

void CSLOLUtils::relaunchAdmin(int argc, char *argv[]) {}
#elif defined(__APPLE__)
# include <libproc.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <mach-o/dyld.h>
# include <Security/Authorization.h>
# include <Security/AuthorizationTags.h>
# include <sys/sysctl.h>

QString CSLOLUtils::detectGamePath() {
pid_t *pid_list;
Expand Down Expand Up @@ -200,11 +212,64 @@ QString CSLOLUtils::detectGamePath() {
return "";
}

// TODO: macos implementation
bool CSLOLUtils::isUnnecessaryAdmin() { return false; }
QString CSLOLUtils::isPlatformUnsuported() {
int ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == 0 && ret == 1) {
return QString{"Apple silicon (M1, M2...) macs are not supported.\nOnly intel based macs are supported."};
}
return QString{""};
}

void CSLOLUtils::relaunchAdmin(int argc, char *argv[]) {
QCoreApplication::setSetuidAllowed(true);

char path[PATH_MAX];
uint32_t path_max_size = PATH_MAX;
if (_NSGetExecutablePath(path, &path_max_size) != KERN_SUCCESS) {
return;
}

if (argc > 1 && strcmp(argv[1], "admin") == 0) {
puts("Authed!");
return;
}

AuthorizationRef authorizationRef;
OSStatus createStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
if (createStatus != errAuthorizationSuccess) {
fprintf(stderr, "Failed to create auth!\n");
return;
}

AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults
| kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagExtendRights;
OSStatus copyStatus = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (copyStatus != errAuthorizationSuccess) {
fprintf(stderr, "Failed to create copy!\n");
return;
}

char* args[] = { strdup("admin"), NULL };
FILE* pipe = NULL;

OSStatus execStatus = AuthorizationExecuteWithPrivileges(authorizationRef, path, kAuthorizationFlagDefaults, args, &pipe);
if (execStatus != errAuthorizationSuccess) {
fprintf(stderr, "Failed to exec auth: %x\n", execStatus);
return;
}

AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);

exit(0);
}
#else
QString CSLOLUtils::detectGamePath() { return ""; }

bool CSLOLUtils::isUnnecessaryAdmin() { return false; }
QString CSLOLUtils::isPlatformUnsuported() { return QString{""}; }

void CSLOLUtils::relaunchAdmin(int argc, char *argv[]) {}
#endif
3 changes: 2 additions & 1 deletion src/CSLOLUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class CSLOLUtils : public QObject {
Q_INVOKABLE QString toFile(QString file);
Q_INVOKABLE QString checkGamePath(QString path);
Q_INVOKABLE QString detectGamePath();
Q_INVOKABLE bool isUnnecessaryAdmin();

static QString isPlatformUnsuported();
static void relaunchAdmin(int argc, char *argv[]);
private:
};

Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "CSLOLVersion.h"

int main(int argc, char *argv[]) {
CSLOLUtils::relaunchAdmin(argc, argv);

qmlRegisterType<CSLOLTools>("customskinlol.tools", 1, 0, "CSLOLTools");

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Expand Down
48 changes: 0 additions & 48 deletions src/main_elevate.c

This file was deleted.

0 comments on commit c128777

Please sign in to comment.