-
Notifications
You must be signed in to change notification settings - Fork 2
/
libTimeMachine.c
261 lines (228 loc) · 7.47 KB
/
libTimeMachine.c
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
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <regex.h>
#include <removefile.h>
#include <sys/snapshot.h>
#include "utils.h"
__attribute__((aligned(4)))
typedef struct val_attrs {
uint32_t length;
attribute_set_t returned;
attrreference_t name_info;
char name[MAXPATHLEN];
} val_attrs_t;
bool is_number(const char *num) {
if (strcmp(num, "0") == 0) {
return true;
}
const char* p = num;
if (*p < '1' || *p > '9') {
return false;
} else {
p++;
}
while (*p) {
if(*p < '0' || *p > '9') {
return false;
} else {
p++;
}
}
return true;
}
bool is_sealed(const char *mntpt) {
io_registry_entry_t chosen = IORegistryEntryFromPath(kIOMasterPortDefault, "IODeviceTree:/chosen");
assert(MACH_PORT_VALID(chosen));
CFTypeRef firmware = IORegistryEntryCreateCFProperty(chosen, CFSTR("firmware-version"), kCFAllocatorDefault, 0);
IOObjectRelease(chosen);
assert(firmware != NULL);
assert(CFGetTypeID(firmware) == CFDataGetTypeID());
CFStringRef bootloader = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, firmware, kCFStringEncodingUTF8);
CFRelease(firmware);
assert(bootloader != NULL);
if (CFStringHasPrefix(bootloader, CFSTR("pongoOS-"))) {
CFRelease(bootloader);
return false;
}
CFRelease(bootloader);
struct vol_attr {
uint32_t len;
vol_capabilities_attr_t vol_cap;
} vol_attrs = {};
struct attrlist vol_attr_list = {
.bitmapcount = ATTR_BIT_MAP_COUNT,
.volattr = ATTR_VOL_CAPABILITIES
};
if (!getattrlist(mntpt, &vol_attr_list, &vol_attrs, sizeof(vol_attrs), 0) &&
vol_attrs.vol_cap.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_SEALED) {
return (vol_attrs.vol_cap.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_SEALED);
}
return false;
}
int snapshot_create(const char *vol, const char *snap) {
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_create(dirfd, snap, 0);
close(dirfd);
if (ret != 0) {
perror("fs_snapshot_create");
printf("Failure\n");
} else {
printf("Success\n");
}
return ret;
}
bool snapshot_check(const char *vol, const char *snap) {
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
struct attrlist attr_list = { 0 };
attr_list.commonattr = ATTR_BULK_REQUIRED;
val_attrs_t buf;
bzero(&buf, sizeof(buf));
int retcount;
while ((retcount = fs_snapshot_list(dirfd, &attr_list, &buf, sizeof(buf), 0))>0) {
val_attrs_t *entry = &buf;
for (int i = 0; i < retcount; i++) {
if (entry->returned.commonattr & ATTR_CMN_NAME) {
if (strcmp(entry->name, snap) == 0) {
close(dirfd);
return true;
}
}
entry = (val_attrs_t *)((char *)entry + entry->length);
}
bzero(&buf, sizeof(buf));
}
close(dirfd);
if (retcount < 0) {
perror("fs_snapshot_list");
exit(1);
}
return false;
}
int snapshot_delete(const char *vol, const char *snap) {
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_delete(dirfd, snap, 0);
close(dirfd);
if (ret != 0) {
perror("fs_snapshot_delete");
printf("Failure\n");
} else {
printf("Success\n");
}
return ret;
}
int snapshot_rename(const char *vol, const char *snap, const char *nw) {
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_rename(dirfd, snap, nw, 0);
close(dirfd);
if (ret != 0) {
perror("fs_snapshot_rename");
printf("Failure\n");
} else {
printf("Success\n");
}
return ret;
}
void run_system(const char *cmd) {
int status = system(cmd);
if (WEXITSTATUS(status) != 0) {
perror(cmd);
exit(WEXITSTATUS(status));
}
}
CFDictionaryRef loadPrefs() {
CFArrayRef keyList = CFPreferencesCopyKeyList(bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
if (keyList != NULL) {
CFDictionaryRef prefs = CFPreferencesCopyMultiple(keyList, bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
CFRelease(keyList);
return prefs;
} else {
removefile("/private/var/mobile/Library/Preferences/com.michael.TimeMachine.plist", NULL, REMOVEFILE_RECURSIVE);
CFPreferencesSynchronize(bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
return NULL;
}
}
CFNumberRef newInt(const int value) {
return CFAutorelease(CFNumberCreate(NULL, kCFNumberIntType, &value));
}
int do_timemachine(const char *vol, const bool create, const int max_snapshot) {
if (create && max_snapshot != 0) {
time_t time_T = time(NULL);
struct tm *tmTime = localtime(&time_T);
const char *format = "com.apple.TimeMachine.%Y-%m-%d-%H:%M:%S";
char *cre_snapshot = (char *)calloc(42, sizeof(char));
strftime(cre_snapshot, 42, format, tmTime);
printf("Will create snapshot \"%s\" on fs \"%s\"...\n", cre_snapshot, vol);
if (strcmp(vol, "/") == 0) {
removefile("/.com.michael.TimeMachine", NULL, REMOVEFILE_RECURSIVE);
FILE *fp = fopen("/.com.michael.TimeMachine", "w");
fprintf(fp, "%s", cre_snapshot);
fclose(fp);
snapshot_create(vol, cre_snapshot);
removefile("/.com.michael.TimeMachine", NULL, REMOVEFILE_RECURSIVE);
} else {
snapshot_create(vol, cre_snapshot);
}
free(cre_snapshot);
}
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
struct attrlist attr_list = { 0 };
attr_list.commonattr = ATTR_BULK_REQUIRED;
val_attrs_t buf;
bzero(&buf, sizeof(buf));
int number = 0;
char **snapshots = (char**)malloc(number * sizeof(char*));
int retcount;
while ((retcount = fs_snapshot_list(dirfd, &attr_list, &buf, sizeof(buf), 0))>0) {
val_attrs_t *entry = &buf;
for (int i = 0; i < retcount; i++) {
if (entry->returned.commonattr & ATTR_CMN_NAME) {
regex_t predicate;
regcomp(&predicate, "^(com.apple.TimeMachine.)[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}:[0-9]{2}:[0-9]{2}$", REG_EXTENDED | REG_NEWLINE | REG_NOSUB);
if (regexec(&predicate, entry->name, 0, NULL, 0) == 0) {
snapshots = (char**)realloc(snapshots, ++number * sizeof(char*));
snapshots[number - 1] = (char*)calloc(42, sizeof(char));
strcpy(snapshots[number - 1], entry->name);
}
regfree(&predicate);
}
entry = (val_attrs_t *)((char *)entry + entry->length);
}
bzero(&buf, sizeof(buf));
}
close(dirfd);
if (retcount < 0) {
perror("fs_snapshot_list");
exit(1);
}
int ret = 1;
for (int no = 0; no < number - max_snapshot; no++) {
printf("Will delete snapshot \"%s\" on fs \"%s\"...\n", snapshots[no], vol);
snapshot_delete(vol, snapshots[no]);
ret = 0;
}
for (int no = 0; no < number; no++) {
free(snapshots[no]);
}
free(snapshots);
return ret;
}