-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathappbundle.cpp
More file actions
361 lines (288 loc) · 10.6 KB
/
appbundle.cpp
File metadata and controls
361 lines (288 loc) · 10.6 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
#include "minimal/stdlib.h"
#include "minimal/mapping.h"
#include "appbundle.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <plist/plist.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstring>
extern "C" {
#include "sha1.h"
}
using namespace std;
static bool generateCodeResourcesFile(const string &binaryName, const string &bundleDirectory, const string &destinationFile);
bool isAppBundle(const std::string &p)
{
string path = p;
while (path[path.size() - 1] == '/')
path = path.substr(0, path.size() - 1);
struct stat st;
if (stat(path.c_str(), &st))
return false;
if (!(st.st_mode & S_IFDIR))
return false;
if (path.size() < 5)
return false;
if (path.substr(path.size() - 4, 4) != ".app")
return false;
return true;
}
void AppBundle::loadInfoPlist()
{
string path = _fpath + "/Info.plist";
char buf[4096];
FILE *file;
vector<char> data;
size_t read;
plist_t root = 0;
char *ptr = 0;
file = fopen(path.c_str(), "r");
while ((read = fread(buf, 1, sizeof(buf), file)))
data.insert(data.end(), buf, &buf[read]);
_assert(!ferror(file));
fclose(file);
if (!data.size())
return;
if (data.size() > 6 && !memcmp(&data[0], "bplist", 6))
plist_from_bin(data.data(), data.size(), &root);
else
plist_from_xml(data.data(), data.size(), &root);
plist_t bundleIdentifierNode = plist_dict_get_item(root, "CFBundleIdentifier");
plist_get_string_val(bundleIdentifierNode, &ptr);
this->identifier = ptr;
free(ptr);
ptr = NULL;
plist_t bundleExecutableNode = plist_dict_get_item(root, "CFBundleExecutable");
plist_get_string_val(bundleExecutableNode, &ptr);
this->_fbinary = _fpath + "/" + ptr;
free(ptr);
ptr = NULL;
plist_free(root);
}
bool AppBundle::generateCodeSignatureDirectory()
{
string fullpath = _fpath;
string basename = _basename;
string signatureDirectory = fullpath + "/_CodeSignature";
string codeResourcesFile = signatureDirectory + "/CodeResources";
string appname = basename.substr(0, basename.size() - 4);
string rmcmd = "rm -rf " + signatureDirectory;
if (system(rmcmd.c_str()))
return false;
mkdir(signatureDirectory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (!generateCodeResourcesFile(appname, fullpath, codeResourcesFile))
return false;
// Move these to a place that makes more sense?
hashInfoPlist();
hashCodeResources();
return true;
}
// TODO: Figure out what other files are never optional.
static bool fileIsOptional(const string &relpath)
{
const char *str = relpath.c_str();
if (!strcmp(str, "Info.plist"))
return false;
if (!strcmp(str, "embedded.mobileprovision"))
return false;
if (!strcmp(str, "PkgInfo"))
return false;
if (relpath.size() >= 23)
{
// Plists in the settings bundles are required.
if (!strncmp(str, "Settings.bundle/", 16) && (relpath.substr(relpath.size() - 6, 6) == ".plist"))
return false;
}
return true;
}
static void loadFileHash(const string &fullpath, const string &relpath, plist_t files)
{
size_t filesize = _not(size_t);
const uint8_t *memregion = NULL;
uint8_t hash[SHA1HashSize];
SHA1Context hash_ctx;
int result;
plist_t hashValue;
bool optional;
memregion = (uint8_t*)map(fullpath.c_str(), 0, _not(size_t), &filesize, true);
SHA1Reset(&hash_ctx);
SHA1Input(&hash_ctx, memregion, (unsigned int)filesize);
result = SHA1Result(&hash_ctx, hash);
_assert(!result);
optional = fileIsOptional(relpath);
hashValue = plist_new_data((const char *)hash, SHA1HashSize);
if (!optional)
plist_dict_insert_item(files, relpath.c_str(), hashValue);
else
{
plist_t hash_dict = plist_new_dict();
plist_dict_insert_item(hash_dict, "hash", hashValue);
plist_dict_insert_item(hash_dict, "optional", plist_new_bool(true));
plist_dict_insert_item(files, relpath.c_str(), hash_dict);
}
}
void addResourceFilesInDirectory(const string &bundleDirectory, const string &subdir,
plist_t files)
{
DIR *dir;
dirent *entry;
string dirpath = bundleDirectory + "/" + subdir;
dir = opendir(dirpath.c_str());
_assert(dir);
while ((entry = readdir(dir))) {
if (*entry->d_name == '.')
continue;
// Ignore the code signing directory. We'll also want to get rid of the binary.
if (!subdir.size() && !strcmp(entry->d_name, "_CodeSignature"))
continue;
string relpath = subdir + "/" + entry->d_name;
string fullpath = bundleDirectory + "/" + relpath;
if (!subdir.size())
relpath = entry->d_name;
cout << "Found: " << relpath << endl;
struct stat st;
if (stat(fullpath.c_str(), &st))
continue;
if (st.st_mode & S_IFDIR)
{
addResourceFilesInDirectory(bundleDirectory, relpath, files);
continue;
}
else
{
loadFileHash(fullpath, relpath, files);
}
}
closedir(dir);
}
static void addCodeResourceRules(plist_t file)
{
plist_t rules, dict;
rules = plist_new_dict();
plist_dict_insert_item(file, "rules", rules);
plist_dict_insert_item(rules, "^", plist_new_bool(true));
dict = plist_new_dict();
plist_dict_insert_item(dict, "optional", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(1000.0));
plist_dict_insert_item(rules, "^.*\\.lproj/", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "omit", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(1100.0));
plist_dict_insert_item(rules, "^.*\\.lproj/locversion.plist$", dict);
plist_dict_insert_item(rules, "^version.plist$", plist_new_bool(true));
rules = plist_new_dict();
plist_dict_insert_item(file, "rules2", rules);
dict = plist_new_dict();
plist_dict_insert_item(dict, "weight", plist_new_real(11.0));
plist_dict_insert_item(rules, ".*\\.dSYM($|/)", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "weight", plist_new_real(11.0));
plist_dict_insert_item(rules, "^", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "omit", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(2000.0));
plist_dict_insert_item(rules, "^(.*/)?\\.DS_Store$", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "nested", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(10.0));
plist_dict_insert_item(rules, "^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/", dict);
plist_dict_insert_item(rules, "^.*", plist_new_bool(true));
dict = plist_new_dict();
plist_dict_insert_item(dict, "optional", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(1000.0));
plist_dict_insert_item(rules, "^.*\\.lproj/", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "omit", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(1100.0));
plist_dict_insert_item(rules, "^.*\\.lproj/locversion.plist$", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "omit", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(20.0));
plist_dict_insert_item(rules, "^Info\\.plist$", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "omit", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(20.0));
plist_dict_insert_item(rules, "^PkgInfo$", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "nested", plist_new_bool(true));
plist_dict_insert_item(dict, "weight", plist_new_real(10.0));
plist_dict_insert_item(rules, "^[^/]+$", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "weight", plist_new_real(20.0));
plist_dict_insert_item(rules, "^embedded\\.provisionprofile$", dict);
dict = plist_new_dict();
plist_dict_insert_item(dict, "weight", plist_new_real(20.0));
plist_dict_insert_item(rules, "^version\\.plist$", dict);
}
static bool generateCodeResourcesFile(const string &binaryName, const string &bundleDirectory, const string &destinationFile)
{
plist_t toplevel, files, files2;
toplevel = plist_new_dict();
_assert(toplevel);
files = plist_new_dict();
plist_dict_insert_item(toplevel, "files", files);
addResourceFilesInDirectory(bundleDirectory, "", files);
plist_dict_remove_item(files, binaryName.c_str());
files2 = plist_copy(files);
plist_dict_insert_item(toplevel, "files2", files2);
addCodeResourceRules(toplevel);
char *xmldata = NULL;
uint32_t xmllen = 0;
plist_to_xml(toplevel, &xmldata, &xmllen);
ofstream outfile(destinationFile.c_str());
outfile.write(xmldata, xmllen);
free(xmldata);
plist_free(toplevel);
return true;
}
AppBundle::AppBundle(const string &p)
{
memset(specialHashes, 0, sizeof(specialHashes));
_valid = isAppBundle(p);
if (!_valid)
return;
findPaths(p);
loadInfoPlist();
}
void AppBundle::findPaths(const string &path)
{
string fullpath, basename;
{
char buf[4096] = {0};
realpath(path.c_str(), buf);
fullpath = buf;
char buf2[4096];
strcpy(buf2, ::basename(buf));
basename = buf2;
}
_fpath = fullpath;
_basename = basename;
_appname = basename.substr(0, basename.size() - 4);
_fbinary = "";
}
static void hashFile(const char *filename, char *hash)
{
size_t filesize = _not(size_t);
const uint8_t *memregion = NULL;
SHA1Context hash_ctx;
int result;
memregion = (uint8_t*)map(filename, 0, _not(size_t), &filesize, true);
SHA1Reset(&hash_ctx);
SHA1Input(&hash_ctx, memregion, (unsigned int)filesize);
result = SHA1Result(&hash_ctx, (uint8_t*)hash);
_assert(!result);
}
void AppBundle::hashInfoPlist()
{
string plistpath = _fpath + "/Info.plist";
hashFile(plistpath.c_str(), specialHashes[1]); // cdInfoSlot
}
void AppBundle::hashCodeResources()
{
string resources = _fpath + "/_CodeSignature/CodeResources";
hashFile(resources.c_str(), specialHashes[3]); // cdResourceDirSlot
}