Skip to content

Commit

Permalink
mostly working roothelper except writing data, struct Path {
Browse files Browse the repository at this point in the history
  • Loading branch information
NSAntoine committed Feb 10, 2023
1 parent 21380d3 commit e51ca04
Show file tree
Hide file tree
Showing 28 changed files with 781 additions and 206 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
packages: ldid
cache: true
cache-path: ~/__cache
mirror: 'https://repo.quiprr.dev/procursus/'

- name: Select Xcode version (14.0)
run: |
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ APP_HELPER_PATH = $(APP_TMP)/Build/Products/Release-iphoneos/RootHelper

package:
@set -o pipefail; \
xcodebuild -jobs $(shell sysctl -n hw.ncpu) -project 'Santander.xcodeproj' -scheme Santander -configuration Release -arch arm64 -sdk iphoneos -derivedDataPath $(APP_TMP) \
xcodebuild -quiet -jobs $(shell sysctl -n hw.ncpu) -project 'Santander.xcodeproj' -scheme Santander -configuration Release -arch arm64 -sdk iphoneos -derivedDataPath $(APP_TMP) \
CODE_SIGNING_ALLOWED=NO DSTROOT=$(APP_TMP)/install ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES=NO
@set -o pipefail; \
xcodebuild -jobs $(shell sysctl -n hw.ncpu) -project 'Santander.xcodeproj' -scheme RootHelper -configuration Release -arch arm64 -sdk iphoneos -derivedDataPath $(APP_TMP) \
xcodebuild -quiet -jobs $(shell sysctl -n hw.ncpu) -project 'Santander.xcodeproj' -scheme RootHelper -configuration Release -arch arm64 -sdk iphoneos -derivedDataPath $(APP_TMP) \
CODE_SIGNING_ALLOWED=NO DSTROOT=$(APP_TMP)/install ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES=NO
@rm -rf Payload

Expand Down
31 changes: 22 additions & 9 deletions RootHelper/Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
//
// Created by Serena on 10/11/2022
//


import Foundation
import ArgumentParser
import CompressionWrapper
import os
import AssetCatalogWrapper

struct Delete: ParsableCommand {
Expand All @@ -18,7 +18,6 @@ struct Delete: ParsableCommand {
func run() throws {
for path in paths {
try FileManager.default.removeItem(at: path)

}
}
}
Expand Down Expand Up @@ -58,8 +57,12 @@ struct Create: ParsableCommand {

for file in files {
// mode a: create if doesn't exist
guard let fPtr = fopen((file as NSURL).fileSystemRepresentation, "a") else {
throw StringError("Failed to create file \(file): \(String(cString: strerror(errno)))")
let fPtr = try file.withUnsafeFileSystemRepresentation { cPathPointer in
guard let cPathPointer, let fPtr = fopen(cPathPointer, "a") else {
throw StringError("Failed to create file \(file): \(String(cString: strerror(errno)))")
}

return fPtr
}

fclose(fPtr)
Expand Down Expand Up @@ -95,6 +98,18 @@ struct Copy: ParsableCommand {
}
}

struct Rename: ParsableCommand {
@Argument(help: "The path to rename.")
var path: URL

@Argument(help: "The new path.")
var destination: URL

func run() throws {
try FileManager.default.moveItem(at: path, to: destination)
}
}

struct Link: ParsableCommand {
@Argument(help: "The paths to link.")
var paths: [URL]
Expand Down Expand Up @@ -126,8 +141,7 @@ struct WriteData: ParsableCommand {
var path: URL

func run() throws {
let data = FileHandle.standardInput.availableData
try data.write(to: path)
NSLog("availableData: \(FileHandle.standardInput.availableData)")
}
}

Expand Down Expand Up @@ -210,8 +224,7 @@ struct GetContents: ParsableCommand {
var path: URL

func run() throws {
let contents = try FileManager.default.contentsOfDirectory(at: path,
includingPropertiesForKeys: nil)
print(contents.map(\.path).joined(separator: " "))
let contents = try FileManager.default.contentsOfDirectory(at: path, includingPropertiesForKeys: nil)
fputs(contents.map(\.path).joined(separator: " "), stdout)
}
}
31 changes: 21 additions & 10 deletions RootHelper/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
// Created by Serena on 17/10/2022
//

import Foundation
import FSOperations
import ArgumentParser
import NSTaskBridge
import Foundation
import NSTask // proc_pidpath

// get the parent caller, and make sure it's Santander, otherwise, gtfo
/*
var buffer = [CChar](repeating: 0, count: 1024)
proc_pidpath(getppid(), &buffer, 1024)

let path = String(cString: buffer)
let path = URL(fileURLWithPath: String(cString: buffer))

guard path == "/Applications/Santander.app/Santander" else {
fatalError("incorrect parent calling, goodbye!")
// We don't verify the whole path as /Applications/Santander.app/Santander, if we did that
// then this root helper would have to be modified on forks like the TrollStore one,
// where the .app name & path are different
// instead, we make sure that the binary name (which should ALWAYS be 'Santander') is correct.
guard path.lastPathComponent == "Santander" else {
fatalError("Incorrect parent calling, goodbye!")
}
*/

//NSLog("FileHandle.standardInput.availableData.count: \(FileHandle.standardInput.availableData.count)")

setuid(0)
setgid(0)

guard getuid() == 0 else {
fatalError("getuid() returned a uid that wasn't 0, in other words, we werent able to get root.")
fputs("getuid() returned a uid that wasn't 0, in other words, we werent able to get root.", stderr)
exit(-1)
}

struct Program: ParsableCommand {
Expand All @@ -38,6 +42,7 @@ struct Program: ParsableCommand {
Move.self,
Copy.self,
Link.self,
Rename.self,

SetOwnerOrGroup.self,
SetPermissions.self,
Expand All @@ -53,4 +58,10 @@ struct Program: ParsableCommand {
)
}

Program.main()
do {
var command = try Program.parseAsRoot(nil)
try command.run()
} catch {
fputs(error.localizedDescription, stderr)
exit(-1)
}
18 changes: 14 additions & 4 deletions Santander.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
3789B5E228622B7A00058688 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3789B5E128622B7A00058688 /* SceneDelegate.swift */; };
3789B5E928622B7C00058688 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3789B5E828622B7C00058688 /* Assets.xcassets */; };
3789B5EC28622B7C00058688 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3789B5EA28622B7C00058688 /* LaunchScreen.storyboard */; };
3789B5F428622E0D00058688 /* SubPathsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3789B5F328622E0D00058688 /* SubPathsTableViewController.swift */; };
3789B5F428622E0D00058688 /* PathListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3789B5F328622E0D00058688 /* PathListViewController.swift */; };
37932F052863205F00BF48C8 /* UserPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37932F042863205F00BF48C8 /* UserPreferences.swift */; };
37934DB728707F4500D1248A /* TextFileEditorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37934DB628707F4500D1248A /* TextFileEditorViewController.swift */; };
37963F132869C96C00C4B72A /* PathType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37963F122869C96C00C4B72A /* PathType.swift */; };
Expand All @@ -59,12 +59,15 @@
CE2D13DE291D8F8A0023F16D /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = CE2D13DD291D8F8A0023F16D /* ArgumentParser */; };
CE37904628DB44C300124029 /* AssetCatalogCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE37904528DB44C300124029 /* AssetCatalogCell.swift */; };
CE37904A28DB523500124029 /* AssetCatalogSectionHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE37904928DB523500124029 /* AssetCatalogSectionHeader.swift */; };
CE41CDD0299695700090B616 /* Path.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE41CDCF299695700090B616 /* Path.swift */; };
CE41CDD1299695700090B616 /* Path.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE41CDCF299695700090B616 /* Path.swift */; };
CE42B204290ABB6400FD9AC9 /* AssetCatalogSidebarListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE42B203290ABB6400FD9AC9 /* AssetCatalogSidebarListView.swift */; };
CE42E24C291A920400187333 /* LoadingValueState.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE42E24B291A920400187333 /* LoadingValueState.swift */; };
CE5DFE8328C7BC89003E1095 /* BinaryExecutionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE5DFE8228C7BC89003E1095 /* BinaryExecutionViewController.swift */; };
CE7228ED28E8188D00918C5F /* AssetCatalogRenditionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7228EC28E8188D00918C5F /* AssetCatalogRenditionViewController.swift */; };
CE7E02E0290006C70046A7D4 /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE7E02DE290006C70046A7D4 /* MobileCoreServices.framework */; };
CE7E02E1290006C70046A7D4 /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE7E02DF290006C70046A7D4 /* CoreServices.framework */; };
CE99BC6829951DE0008E31DA /* KeyboardSearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE99BC6729951DE0008E31DA /* KeyboardSearchView.swift */; };
CEA33B37291619EB00DD7BAC /* ApplicationsWrapper in Frameworks */ = {isa = PBXBuildFile; productRef = CEA33B36291619EB00DD7BAC /* ApplicationsWrapper */; };
CEA33B39291619EB00DD7BAC /* AssetCatalogWrapper in Frameworks */ = {isa = PBXBuildFile; productRef = CEA33B38291619EB00DD7BAC /* AssetCatalogWrapper */; };
CEA33B3B291619EB00DD7BAC /* CompressionWrapper in Frameworks */ = {isa = PBXBuildFile; productRef = CEA33B3A291619EB00DD7BAC /* CompressionWrapper */; };
Expand Down Expand Up @@ -124,7 +127,7 @@
3789B5E828622B7C00058688 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
3789B5EB28622B7C00058688 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
3789B5ED28622B7C00058688 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3789B5F328622E0D00058688 /* SubPathsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubPathsTableViewController.swift; sourceTree = "<group>"; };
3789B5F328622E0D00058688 /* PathListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PathListViewController.swift; sourceTree = "<group>"; };
37932F042863205F00BF48C8 /* UserPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserPreferences.swift; sourceTree = "<group>"; };
37934DB628707F4500D1248A /* TextFileEditorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFileEditorViewController.swift; sourceTree = "<group>"; };
37963F122869C96C00C4B72A /* PathType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PathType.swift; sourceTree = "<group>"; };
Expand All @@ -148,12 +151,14 @@
CE37904528DB44C300124029 /* AssetCatalogCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssetCatalogCell.swift; sourceTree = "<group>"; };
CE37904928DB523500124029 /* AssetCatalogSectionHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetCatalogSectionHeader.swift; sourceTree = "<group>"; };
CE3E8FC228FF24D3001B8FAA /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
CE41CDCF299695700090B616 /* Path.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Path.swift; sourceTree = "<group>"; };
CE42B203290ABB6400FD9AC9 /* AssetCatalogSidebarListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetCatalogSidebarListView.swift; sourceTree = "<group>"; };
CE42E24B291A920400187333 /* LoadingValueState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingValueState.swift; sourceTree = "<group>"; };
CE5DFE8228C7BC89003E1095 /* BinaryExecutionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryExecutionViewController.swift; sourceTree = "<group>"; };
CE7228EC28E8188D00918C5F /* AssetCatalogRenditionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetCatalogRenditionViewController.swift; sourceTree = "<group>"; };
CE7E02DE290006C70046A7D4 /* MobileCoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MobileCoreServices.framework; path = System/Library/Frameworks/MobileCoreServices.framework; sourceTree = SDKROOT; };
CE7E02DF290006C70046A7D4 /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; };
CE99BC6729951DE0008E31DA /* KeyboardSearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardSearchView.swift; sourceTree = "<group>"; };
CEB0566228F156F500B71017 /* BaseLayoutAnchorSupporting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseLayoutAnchorSupporting.swift; sourceTree = "<group>"; };
CEC433C028FF20B400C18BD8 /* RootHelper */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = RootHelper; sourceTree = BUILT_PRODUCTS_DIR; };
CECB3A0B28E330BC00328434 /* AssetCatalogDetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetCatalogDetailsView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -220,6 +225,7 @@
37934DB628707F4500D1248A /* TextFileEditorViewController.swift */,
37198B9B28721275000C8CDF /* Themes.swift */,
37198B9E2872DDA6000C8CDF /* KeyboardToolsView.swift */,
CE99BC6729951DE0008E31DA /* KeyboardSearchView.swift */,
);
path = TextEditor;
sourceTree = "<group>";
Expand Down Expand Up @@ -250,6 +256,7 @@
CE42E24B291A920400187333 /* LoadingValueState.swift */,
CE1F1F6F28F7211700E44DF2 /* PathTransitioning.swift */,
CEFF556029153B9B002C1B99 /* DiffableDataSourceItem.swift */,
CE41CDCF299695700090B616 /* Path.swift */,
);
path = Other;
sourceTree = "<group>";
Expand Down Expand Up @@ -332,7 +339,7 @@
37C6E0C628662C2900BDDA16 /* Path */ = {
isa = PBXGroup;
children = (
3789B5F328622E0D00058688 /* SubPathsTableViewController.swift */,
3789B5F328622E0D00058688 /* PathListViewController.swift */,
37C6E0CF28673AFF00BDDA16 /* Search.swift */,
37177E4B289F12640025042E /* PathGroupOwnerViewController.swift */,
375CF896289C394E00BB7C60 /* ToolbarItems.swift */,
Expand Down Expand Up @@ -525,7 +532,7 @@
CECB3A0C28E330BC00328434 /* AssetCatalogDetailsView.swift in Sources */,
CECF6EDA28C37F660080B805 /* FontViewerController.swift in Sources */,
374ECDC82875A3940066E9DD /* Storage.swift in Sources */,
3789B5F428622E0D00058688 /* SubPathsTableViewController.swift in Sources */,
3789B5F428622E0D00058688 /* PathListViewController.swift in Sources */,
3789B5E028622B7A00058688 /* AppDelegate.swift in Sources */,
37137F7828AED89F00E28069 /* SerializedArrayViewController.swift in Sources */,
3712D319286F7F2E00BCD034 /* TypeSelectionViewController.swift in Sources */,
Expand All @@ -534,6 +541,7 @@
CE37904628DB44C300124029 /* AssetCatalogCell.swift in Sources */,
CE1FDA2A28F20302000A16BE /* AssetCatalogGridPreviewCell.swift in Sources */,
CEFF556129153B9B002C1B99 /* DiffableDataSourceItem.swift in Sources */,
CE99BC6829951DE0008E31DA /* KeyboardSearchView.swift in Sources */,
37963F17286A0F0B00C4B72A /* DirectoryMonitor.swift in Sources */,
CE37904A28DB523500124029 /* AssetCatalogSectionHeader.swift in Sources */,
CE5DFE8328C7BC89003E1095 /* BinaryExecutionViewController.swift in Sources */,
Expand All @@ -549,6 +557,7 @@
CE0D0B002906D6F300D17307 /* GoToItem.swift in Sources */,
CE42B204290ABB6400FD9AC9 /* AssetCatalogSidebarListView.swift in Sources */,
37CA828A28AA675B000236D7 /* AppInfoViewController.swift in Sources */,
CE41CDD0299695700090B616 /* Path.swift in Sources */,
37ECADA728B23ECC00B95733 /* ImageViewerController.swift in Sources */,
3771D37F2862493E00E200B6 /* PathInformationTableViewController.swift in Sources */,
37198B9A28721069000C8CDF /* TextEditorThemeSettingsViewController.swift in Sources */,
Expand All @@ -563,6 +572,7 @@
files = (
CEF032BD291ED00700B6F768 /* main.swift in Sources */,
CE2D13DC291D8F460023F16D /* Commands.swift in Sources */,
CE41CDD1299695700090B616 /* Path.swift in Sources */,
CEF032BB291ED00700B6F768 /* Extensions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
15 changes: 7 additions & 8 deletions Santander/Other/DirectoryMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,23 @@ class DirectoryMonitor {
var directoryMonitorSource: DispatchSource?

/// URL for the directory being monitored.
var url: URL

// MARK: Initializers
init(url: URL) {
self.url = url
var path: Path

init(path: Path) {
self.path = path
}

// MARK: Monitoring

func startMonitoring() {
// Listen for changes to the directory (if we are not already).
if directoryMonitorSource == nil && monitoredDirectoryFileDescriptor == -1 {
// Open the directory referenced by URL for monitoring only.
monitoredDirectoryFileDescriptor = open((url as NSURL).fileSystemRepresentation, O_EVTONLY)
monitoredDirectoryFileDescriptor = open((path.url as NSURL).fileSystemRepresentation, O_EVTONLY)

// We initialize directoryMonitorSource only if the path is readable
// otherwise, we'd encounter a crash
if url.isReadable {
if path.isReadable {
// Define a dispatch source monitoring the directory for additions, deletions, and renamings.
directoryMonitorSource = DispatchSource.makeFileSystemObjectSource(fileDescriptor: monitoredDirectoryFileDescriptor, eventMask: .all, queue: directoryMonitorQueue) as? DispatchSource
}
Expand Down
Loading

0 comments on commit e51ca04

Please sign in to comment.