-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
212 lines (172 loc) · 7.42 KB
/
main.m
File metadata and controls
212 lines (172 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <spawn.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/stat.h>
#include <mach-o/dyld.h>
#include <Foundation/Foundation.h>
#include "incbin.h"
INCBIN(TROLLSTORE_TAR, "trollstore/TrollStore.tar");
INCBIN(TROLLSTORE_HELPER, "trollstore/trollstorehelper");
extern char **environ;
@interface _LSApplicationState : NSObject
- (BOOL)isValid;
@end
@interface LSBundleProxy : NSObject
-(BOOL)isContainerized;
- (NSURL *)bundleURL;
- (NSURL *)containerURL;
- (NSURL *)dataContainerURL;
- (NSString *)bundleExecutable;
- (NSString *)bundleIdentifier;
@end
@interface LSPlugInKitProxy : LSBundleProxy
@end
@interface LSApplicationProxy : LSBundleProxy
+ (id)applicationProxyForIdentifier:(id)arg1;
- (id)localizedNameForContext:(id)arg1;
- (_LSApplicationState *)appState;
- (NSString *)vendorName;
- (NSString *)teamID;
- (NSString *)applicationType;
- (NSSet *)claimedURLSchemes;
- (BOOL)isDeletable;
- (NSDictionary*)environmentVariables;
@property (nonatomic,readonly) NSDictionary *groupContainerURLs;
@property (nonatomic,readonly) NSArray<LSPlugInKitProxy *> *plugInKitPlugins;
@end
@interface LSApplicationWorkspace : NSObject
+ (LSApplicationWorkspace*)defaultWorkspace;
- (BOOL)_LSPrivateRebuildApplicationDatabasesForSystemApps:(BOOL)arg1
internal:(BOOL)arg2
user:(BOOL)arg3;
- (BOOL)registerApplicationDictionary:(NSDictionary *)applicationDictionary;
- (BOOL)registerBundleWithInfo:(NSDictionary *)bundleInfo
options:(NSDictionary *)options
type:(unsigned long long)arg3
progress:(id)arg4;
- (BOOL)registerApplication:(NSURL *)url;
- (BOOL)registerPlugin:(NSURL *)url;
- (BOOL)unregisterApplication:(NSURL *)url;
- (NSArray *)installedPlugins;
- (void)_LSPrivateSyncWithMobileInstallation;
- (NSArray<LSApplicationProxy *> *)allApplications;
@end
void InstallTrollStore()
{
NSString* tmpDir = [NSTemporaryDirectory() stringByAppendingPathComponent:NSUUID.UUID.UUIDString];
[[NSFileManager defaultManager] createDirectoryAtPath:tmpDir withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"tmpDir: %@", tmpDir);
NSData* trollstoreTarData = [NSData dataWithBytes:gTROLLSTORE_TARData length:gTROLLSTORE_TARSize];
NSString* trollstoreTarPath = [tmpDir stringByAppendingPathComponent:@"TrollStore.tar"];
[trollstoreTarData writeToFile:trollstoreTarPath atomically:YES];
NSData* trollstoreHelperData = [NSData dataWithBytes:gTROLLSTORE_HELPERData length:gTROLLSTORE_HELPERSize];
NSString* trollstoreHelperPath = [tmpDir stringByAppendingPathComponent:@"trollstorehelper"];
[trollstoreHelperData writeToFile:trollstoreHelperPath atomically:YES];
chmod(trollstoreHelperPath.fileSystemRepresentation, 0755);
const char* argv[] = {trollstoreHelperPath.fileSystemRepresentation, "install-trollstore", trollstoreTarPath.fileSystemRepresentation, NULL};
pid_t pid = -1;
int ret = posix_spawn(&pid, argv[0], NULL, NULL, (char*const*)argv, environ);
NSLog(@"posix_spawn returned %d, pid=%d", ret, pid);
waitpid(pid, NULL, 0);
[NSFileManager.defaultManager removeItemAtPath:tmpDir error:nil];
}
#define POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE 1
extern int posix_spawnattr_set_persona_np(const posix_spawnattr_t* __restrict, uid_t, uint32_t);
extern int posix_spawnattr_set_persona_uid_np(const posix_spawnattr_t* __restrict, uid_t);
extern int posix_spawnattr_set_persona_gid_np(const posix_spawnattr_t* __restrict, uid_t);
int main(int argc, char *argv[], char *envp[])
{
NSLog(@"jbinit uid=%d,gid=%d,pid=%d,ppid=%d argv=%p envp=%p\n", getuid(), getgid(), getpid(), getppid(), argv, envp);
if(fork() > 0) {
// exit parent process so that launchd will respawn the real daemon
return 0;
}
char path[PATH_MAX]={0};
uint32_t pathSize = sizeof(path);
_NSGetExecutablePath(path, &pathSize);
if(getuid() != 0)
{
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
posix_spawnattr_set_persona_np(&attr, 99, POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE);
posix_spawnattr_set_persona_uid_np(&attr, 0);
posix_spawnattr_set_persona_gid_np(&attr, 0);
pid_t pid=0;
int ret = posix_spawn(&pid, path, NULL, &attr, argv, envp);
NSLog(@"posix_spawn returned %d, pid=%d", ret, pid);
return ret;
}
//remove all jbinit files
NSString* dirpath = @(dirname(path));
for(NSString* item in [NSFileManager.defaultManager contentsOfDirectoryAtPath:dirpath error:nil]) {
if([item hasPrefix:@".jbinit-"]) {
NSError* error = nil;
NSLog(@"Removing jbinit file: %@", item);
[NSFileManager.defaultManager removeItemAtPath:[dirpath stringByAppendingPathComponent:item] error:&error];
if(error) {
NSLog(@"Error removing jbinit file: %@", error);
}
}
}
BOOL TrollStoreInstalled = NO;
NSString* trollStorePath = nil;
for(LSApplicationProxy* app in LSApplicationWorkspace.defaultWorkspace.allApplications)
{
if([app.bundleIdentifier hasPrefix:@"com.opa334.TrollStore"])
{
NSLog(@"Found TrollStore at %@", app.bundleURL);
trollStorePath = app.bundleURL.path;
TrollStoreInstalled = YES;
// break;
}
if([app.bundleURL.path hasPrefix:@"/private/preboot/"]) {
[LSApplicationWorkspace.defaultWorkspace unregisterApplication:app.bundleURL];
}
}
NSString* message = @"\nPalera1n(roothide) boot successful.\n\n";
NSString* buttonText = nil;
if(TrollStoreInstalled) {
message = [message stringByAppendingString:@"Please install and run the jailbreak app in TrollStore to continue."];
} else {
buttonText = @"Continue";
message = [message stringByAppendingString:@"Press [Continue] to install TrollStore ..."];
}
//this is a sync call
CFUserNotificationDisplayAlert(0, kCFUserNotificationCautionAlertLevel, NULL, NULL, NULL, CFSTR("Palera1n(roothide)"), (__bridge CFStringRef)message, (__bridge CFStringRef)buttonText, NULL, NULL, NULL);
if(!TrollStoreInstalled)
{
NSMutableDictionary *alert = [[NSMutableDictionary alloc] init];
CFOptionFlags flags = kCFUserNotificationCautionAlertLevel | kCFUserNotificationNoDefaultButtonFlag;
alert[(__bridge NSString *)kCFUserNotificationAlertHeaderKey] = @"Palera1n(roothide)";
alert[(__bridge NSString *)kCFUserNotificationAlertMessageKey] = @"\nInstalling TrollStore...\n";
CFUserNotificationRef notif = CFUserNotificationCreate(kCFAllocatorDefault, 0, flags, NULL, (__bridge CFMutableDictionaryRef)alert);
sleep(1);
InstallTrollStore();
sleep(1);
CFUserNotificationCancel(notif);
CFRelease(notif);
buttonText = nil;
message = @"\nTrollStore installed successful.\n\nPlease install and run the jailbreak app in TrollStore to continue.";
CFUserNotificationDisplayAlert(0, kCFUserNotificationCautionAlertLevel, NULL, NULL, NULL, CFSTR("Palera1n(roothide)"), (__bridge CFStringRef)message, (__bridge CFStringRef)buttonText, NULL, NULL, NULL);
}
else
{
NSString* trollstoreHelperPath = [trollStorePath stringByAppendingPathComponent:@"trollstorehelper"];
const char* argv[] = {trollstoreHelperPath.fileSystemRepresentation, "refresh", NULL};
pid_t pid = -1;
int ret = posix_spawn(&pid, argv[0], NULL, NULL, (char*const*)argv, environ);
NSLog(@"posix_spawn returned %d, pid=%d", ret, pid);
waitpid(pid, NULL, 0);
}
// while(true) {
// NSLog(@"jbinit...uid=%d,gid=%d,pid=%d,ppid=%d argv=%p envp=%p\n", getuid(), getgid(), getpid(), getppid(), argv, envp);
// FILE* f = fopen("/var/jbinit.txt", "w+");
// if(f) {
// fprintf(f, "[%lu] jbinit...uid=%d,gid=%d,pid=%d,ppid=%d argv=%p envp=%p\n", time(NULL), getuid(), getgid(), getpid(), getppid(), argv, envp);
// fclose(f);
// }
// sleep(1);
// };
return 0;
}