-
Notifications
You must be signed in to change notification settings - Fork 36
/
postinst.mm
269 lines (212 loc) · 7.05 KB
/
postinst.mm
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "CyteKit/UCPlatform.h"
#include <dirent.h>
#include <strings.h>
#include <Sources.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <Menes/ObjectHandle.h>
/* Set platform binary flag */
#include <dlfcn.h>
#define FLAG_PLATFORMIZE (1 << 1)
void platformize_me() {
void* handle = dlopen("/usr/lib/libjailbreak.dylib", RTLD_LAZY);
if (!handle) return;
// Reset errors
dlerror();
typedef void (*fix_entitle_prt_t)(pid_t pid, uint32_t what);
fix_entitle_prt_t ptr = (fix_entitle_prt_t)dlsym(handle, "jb_oneshot_entitle_now");
const char *dlsym_error = dlerror();
if (dlsym_error) return;
ptr(getpid(), FLAG_PLATFORMIZE);
}
void Finish(const char *finish) {
if (finish == NULL)
return;
const char *cydia(getenv("CYDIA"));
if (cydia == NULL)
return;
int fd([[[[NSString stringWithUTF8String:cydia] componentsSeparatedByString:@" "] objectAtIndex:0] intValue]);
FILE *fout(fdopen(fd, "w"));
fprintf(fout, "finish:%s\n", finish);
fclose(fout);
}
static bool setnsfpn(const char *path) {
return system([[NSString stringWithFormat:@"/usr/libexec/cydia/setnsfpn %s", path] UTF8String]) == 0;
}
enum StashStatus {
StashDone,
StashFail,
StashGood,
};
static StashStatus MoveStash() {
struct stat stat;
if (lstat("/var/stash", &stat) == -1)
return errno == ENOENT ? StashGood : StashFail;
else if (S_ISLNK(stat.st_mode))
return StashGood;
else if (!S_ISDIR(stat.st_mode))
return StashFail;
if (lstat("/var/db/stash", &stat) == -1) {
if (errno == ENOENT)
goto move;
else return StashFail;
} else if (S_ISLNK(stat.st_mode))
// XXX: this is fixable
return StashFail;
else if (!S_ISDIR(stat.st_mode))
return StashFail;
else {
if (!setnsfpn("/var/db/stash"))
return StashFail;
if (system("mv -t /var/stash /var/db/stash/*") != 0)
return StashFail;
if (rmdir("/var/db/stash") == -1)
return StashFail;
} move:
if (!setnsfpn("/var/stash"))
return StashFail;
if (rename("/var/stash", "/var/db/stash") == -1)
return StashFail;
if (symlink("/var/db/stash", "/var/stash") != -1)
return StashDone;
if (rename("/var/db/stash", "/var/stash") != -1)
return StashFail;
fprintf(stderr, "/var/stash misplaced -- DO NOT REBOOT\n");
return StashFail;
}
static bool FixProtections() {
const char *path("/var/lib");
mkdir(path, 0755);
if (!setnsfpn(path)) {
fprintf(stderr, "failed to setnsfpn %s\n", path);
return false;
}
return true;
}
static void FixPermissions() {
DIR *stash(opendir("/var/stash"));
if (stash == NULL)
return;
while (dirent *entry = readdir(stash)) {
const char *folder(entry->d_name);
if (strlen(folder) != 8)
continue;
if (strncmp(folder, "_.", 2) != 0)
continue;
char path[1024];
sprintf(path, "/var/stash/%s", folder);
struct stat stat;
if (lstat(path, &stat) == -1)
continue;
if (!S_ISDIR(stat.st_mode))
continue;
chmod(path, 0755);
}
closedir(stash);
}
#define APPLICATIONS "/Applications"
static bool FixApplications() {
char target[1024];
ssize_t length(readlink(APPLICATIONS, target, sizeof(target)));
if (length == -1)
return false;
if (length >= sizeof(target)) // >= "just in case" (I'm nervous)
return false;
target[length] = '\0';
if (strlen(target) != 30)
return false;
if (memcmp(target, "/var/stash/Applications.", 24) != 0)
return false;
if (strchr(target + 24, '/') != NULL)
return false;
struct stat stat;
if (lstat(target, &stat) == -1)
return false;
if (!S_ISDIR(stat.st_mode))
return false;
char temp[] = "/var/stash/_.XXXXXX";
if (mkdtemp(temp) == NULL)
return false;
if (false) undo: {
unlink(temp);
return false;
}
if (chmod(temp, 0755) == -1)
goto undo;
char destiny[strlen(temp) + 32];
sprintf(destiny, "%s%s", temp, APPLICATIONS);
if (unlink(APPLICATIONS) == -1)
goto undo;
if (rename(target, destiny) == -1) {
if (symlink(target, APPLICATIONS) == -1)
fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
goto undo;
} else {
bool success;
if (symlink(destiny, APPLICATIONS) != -1)
success = true;
else {
fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
success = false;
}
// unneccessary, but feels better (I'm nervous)
symlink(destiny, target);
[@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
return success;
}
}
int main(int argc, const char *argv[]) {
if (argc < 2 || strcmp(argv[1], "configure") != 0)
return 0;
platformize_me();
NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
bool restart(false);
if (kCFCoreFoundationVersionNumber >= 1000) {
if (!FixProtections())
return 1;
switch (MoveStash()) {
case StashDone:
restart = true;
break;
case StashFail:
fprintf(stderr, "failed to move stash\n");
return 1;
case StashGood:
break;
}
}
#define OldCache_ "/var/root/Library/Caches/com.saurik.Cydia"
if (access(OldCache_, F_OK) == 0)
system("rm -rf " OldCache_);
#define NewCache_ "/var/mobile/Library/Caches/com.saurik.Cydia"
system("cd /; su -c 'mkdir -p " NewCache_ "' mobile");
if (access(NewCache_ "/lists", F_OK) != 0 && errno == ENOENT)
system("cp -at " NewCache_ " /var/lib/apt/lists");
system("chown -R 501.501 " NewCache_);
#define OldLibrary_ "/var/lib/cydia"
#define NewLibrary_ "/var/mobile/Library/Cydia"
system("cd /; su -c 'mkdir -p " NewLibrary_ "' mobile");
#define Cytore_ "/metadata.cb0"
#define CYDIA_LIST "/etc/apt/sources.list.d/cydia.list"
unlink(CYDIA_LIST);
[[NSString stringWithFormat:@
"deb http://apt.saurik.com/ ios/%.2f main\n"
"deb http://apt.thebigboss.org/repofiles/cydia/ stable main\n"
"deb http://cydia.zodttd.com/repo/cydia/ stable main\n"
"deb http://apt.modmyi.com/ stable main\n"
, kCFCoreFoundationVersionNumber] writeToFile:@ CYDIA_LIST atomically:YES];
if (access(NewLibrary_ Cytore_, F_OK) != 0 && errno == ENOENT) {
if (access(NewCache_ Cytore_, F_OK) == 0)
system("mv -f " NewCache_ Cytore_ " " NewLibrary_);
else if (access(OldLibrary_ Cytore_, F_OK) == 0)
system("mv -f " OldLibrary_ Cytore_ " " NewLibrary_);
chown(NewLibrary_ Cytore_, 501, 501);
}
FixPermissions();
restart |= FixApplications();
if (restart)
Finish("restart");
[pool release];
return 0;
}