-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsnappy.c
More file actions
378 lines (362 loc) · 9.15 KB
/
snappy.c
File metadata and controls
378 lines (362 loc) · 9.15 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Copyright 2018 Sam Bingner All Rights Reserved
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/snapshot.h>
#include <sys/fcntl.h>
#include <getopt.h>
#ifdef __arm64__
#include <dlfcn.h>
#endif
#include <IOKit/IOKitLib.h>
#include "snappy.h"
enum operation {
OP_UNDEFINED = 0,
OP_LIST,
OP_CREATE,
OP_DELETE,
OP_MOUNT,
OP_RENAME,
OP_REVERT,
OP_SHOWHASH,
OP_ORIG
};
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"filesystem", required_argument, 0, 'f'},
{"flags", required_argument, 0, 'g'},
{"list", no_argument, 0, 'l'},
{"create", required_argument, 0, 'c'},
{"delete", required_argument, 0, 'd'},
{"rename", required_argument, 0, 'r'},
{"showhash", no_argument, 0, 's'},
{"to", required_argument, 0, 't'},
{"to-system", no_argument, 0, 'x'},
{"mount", required_argument, 0, 'm'},
{"revert", required_argument, 0, 'v'},
{"orig", no_argument, 0, 'o'},
{0, 0, 0, 0 }
};
void usage(void);
int main(int argc, char **argv, char **envp);
#ifdef __arm64__
void patch_setuid() {
void* libjb = dlopen("/usr/lib/libjailbreak.dylib", RTLD_LAZY);
if (!libjb)
return;
// Reset errors
dlerror();
typedef void (*fix_setuid_prt_t)(pid_t pid);
fix_setuid_prt_t jb_oneshot_fix_setuid_now = (fix_setuid_prt_t)dlsym(libjb, "jb_oneshot_fix_setuid_now");
const char *dlsym_error = dlerror();
if (dlsym_error || jb_oneshot_fix_setuid_now == NULL)
return;
jb_oneshot_fix_setuid_now(getpid());
setuid(0);
}
#endif
void usage(void)
{
printf("Usage: snappy -f DIR [OPTIONS...]\n"
"\t-h, --help\t\tPrint this help\n"
"\t-f, --filesystem DIR\tFilesystem to operate on (mountpoint)\n"
"\t-l, --list\t\tList snapshots on filesystem\n"
"\t-c, --create NAME\tCreate a snapshot named NAME\n"
"\t-d, --delete NAME\tDelete a snapshot named NAME\n"
"\t-r, --rename NAME\tRename a snapshot named NAME to name supplied by --to\n"
"\t-m, --mount NAME\tMount snapshot named NAME to path specified by --to\n"
"\t-g, --flags FLAGS\tMount flags (integer) to use when mounting\n"
#if TARGET_IPHONE
"\t\t\t\t\t(Mount currently not working on iOS)\n"
#endif
"\t-t, --to PATH\n"
"\t-v, --revert NAME\tRevert to snapshot named NAME\n"
"\t-s, --showhash\t\tShow the name of the system snapshot for this boot-manifest-hash\n"
"\t-x, --to-system\t\tSet the target snapshot name to be the iOS system-snapshot\n"
"\t-o, --orig\t\tRevert to the original pre-jailbreak snapshot\n"
);
}
int main(int argc, char **argv, char **envp)
{
#ifdef __arm64__
if (geteuid()!=0)
patch_setuid();
#endif
int option_index = 0;
int dirfd = -1;
char *hashsnap = NULL;
char *hash=NULL;
char *fspath = NULL;
const char *snapName = NULL;
char *to = NULL;
int flags = 0;
enum operation op = OP_UNDEFINED;
int c;
if (argc<2) {
usage();
exit(1);
}
while ((c = getopt_long(argc, argv, "hf:c:d:g:r:st:lm:v:xo", long_options, &option_index)) != -1) {
switch (c) {
case 'c':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_CREATE;
snapName = optarg;
break;
case 'd':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_DELETE;
snapName = optarg;
break;
case 'f':
if (fspath != NULL) {
fprintf(stderr, "Multiple --filesystem statements\n");
usage();
exit(1);
}
fspath = optarg;
break;
case 'g':
flags = (int)strtol(optarg, NULL, 0);
if (errno == EINVAL || errno == ERANGE) {
perror("Error parsing flags:");
usage();
exit(1);
}
break;
case 'h':
usage();
exit(0);
break;
case 'l':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_LIST;
break;
case 'm':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_MOUNT;
snapName = optarg;
break;
case 'r':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_RENAME;
snapName = optarg;
break;
case 's':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_SHOWHASH;
break;
case 't':
if (to != NULL) {
fprintf(stderr, "Multiple --to statements\n");
usage();
exit(1);
}
to = optarg;
break;
case 'v':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_REVERT;
snapName = optarg;
break;
case 'x':
if (to != NULL) {
fprintf(stderr, "Multiple --to statements\n");
usage();
exit(1);
}
hashsnap = copy_system_snapshot();
if (hashsnap == NULL) {
fprintf(stderr, "Error: Unable to generate destination snapshot name\n");
exit(1);
}
to = hashsnap;
break;
case 'o':
if (op != OP_UNDEFINED) {
fprintf(stderr, "Error: multiple operations not supported\n");
usage();
exit(1);
}
op = OP_ORIG;
hashsnap = copy_system_snapshot();
if (hashsnap == NULL) {
fprintf(stderr, "Error: Unable to generate destination snapshot name\n");
exit(1);
}
to = hashsnap;
break;
default:
usage();
exit(1);
break;
}
}
bool error=false;
setuid(0);
if (op != OP_SHOWHASH) {
if (op == OP_ORIG && fspath == NULL) {
fspath = "/";
}
if (fspath != NULL) {
dirfd = open(fspath, O_RDONLY);
if (dirfd == -1) {
fprintf(stderr, "Error unable to open path %s: %s", fspath, strerror(errno));
exit(1);
}
} else {
fprintf(stderr, "Error: no --fspath specified\n");
usage();
exit(1);
}
}
switch (op) {
case OP_CREATE:
printf("Will create snapshot named \"%s\" on fs \"%s\"...\n", snapName, fspath);
if (fs_snapshot_create(dirfd, snapName, 0) == ERR_SUCCESS) {
printf("Success\n");
} else {
perror("fs_snapshot_create");
printf("Failure\n");
error=true;
}
break;
case OP_DELETE:
printf("Will delete snapshot named %s from fs %s\n", snapName, fspath);
if (fs_snapshot_delete(dirfd, snapName, 0) == ERR_SUCCESS) {
printf("Success\n");
} else {
perror("fs_snapshot_delete");
printf("Failure\n");
error=true;
}
break;
case OP_RENAME:
if (to == NULL) {
fprintf(stderr, "Error: rename requested but no new name (--to) provided\n");
usage();
error=true;
break;
}
printf("Will rename snapshot %s on fs %s to %s\n", snapName, fspath, to);
if (fs_snapshot_rename(dirfd, snapName, to, 0) == ERR_SUCCESS) {
printf("Success\n");
} else {
perror("fs_snapshot_rename");
printf("Failure\n");
error=true;
}
break;
case OP_ORIG:
snapName = copy_first_snapshot(dirfd);
if (snapName == NULL) {
fprintf(stderr, "Error: You have no snapshots?\n");
exit(1);
}
printf("Will rename snapshot %s on fs %s to %s\n", snapName, fspath, to);
if (fs_snapshot_rename(dirfd, snapName, to, 0) == ERR_SUCCESS) {
printf("Rename Success\nReboot to stock to complete reversion.\n");
} else {
perror("fs_snapshot_rename");
printf("Failure\n");
error=true;
}
break;
case OP_SHOWHASH:
hash = copy_system_snapshot();
if (hash) {
printf("System Snapshot: %s\n", hash);
free(hash);
} else {
perror("Unable to get boot-manifest-hash");
error=true;
}
break;
case OP_MOUNT:
if (to == NULL) {
fprintf(stderr, "Error: mount requested but no mount path (--to) provided\n");
usage();
error=true;
break;
}
printf("Will mount snapshot %s on fs %s to %s\n", snapName, fspath, to);
if (fs_snapshot_mount(dirfd, to, snapName, flags) == ERR_SUCCESS) {
printf("Success\n");
} else {
perror("fs_snapshot_mount");
printf("Failure\n");
error=true;
}
break;
case OP_REVERT:
#if (TARGET_IPHONE || TARGET_OS_TV)
fprintf(stderr, "Cowardly refusing to revert snapshot on iOS because it would permanently corrupt the filesystem (something is broken on the devices)\n");
error=true;
break;
#endif
printf("Will revert to snapshot %s on fs %s\n", snapName, fspath);
if (fs_snapshot_revert(dirfd, snapName, 0) == ERR_SUCCESS) {
printf("Success\n");
} else {
perror("fs_snapshot_revert");
printf("Failure\n");
error=true;
}
break;
case OP_LIST:
printf("Will list snapshots on %s fs\n", fspath);
const char **snapshots = copy_snapshot_list(dirfd);
if (snapshots==NULL || *snapshots == NULL) {
error=true;
} else {
for (const char **snapshot = snapshots; *snapshot; snapshot++) {
printf("%s\n", *snapshot);
}
}
if (snapshots != NULL) {
free(snapshots);
}
break;
default:
fprintf(stderr, "Error: please provide an operation (create, delete, rename, mount, revert, list)\n");
error=true;
break;
}
if (hashsnap != NULL)
free(hashsnap);
if (op != OP_SHOWHASH)
close(dirfd);
return(error);
}
// vim:ft=objc