-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYFirefoxProfiler.m
512 lines (444 loc) · 20.5 KB
/
SSYFirefoxProfiler.m
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#import "SSYFirefoxProfiler.h"
#import "NSString+MorePaths.h"
#import "NSFileManager+TempFile.h"
#import "NSError+MoreDescriptions.h"
#import "NSString+Base64.h"
#import "NSData+FileAlias.h"
#import "NSError+MyDomain.h"
#import "NSString+SSYDotSuffix.h"
@implementation SSYFirefoxProfiler
+ (NSString*)appSupportRelativePath {
return @"Firefox" ;
}
+ (NSString*)defaultProfileName {
return @"default" ;
}
+ (NSString*)browserSupportPathForHomePath:(NSString*)homePath {
if (!homePath) {
// Default to the current user's Mac account
homePath = NSHomeDirectory() ;
}
return [[NSString applicationSupportPathForHomePath:homePath] stringByAppendingPathComponent:[self appSupportRelativePath]] ;
}
+ (NSString*)profilesFilePathForHomePath:(NSString*)homePath {
return [[self browserSupportPathForHomePath:homePath] stringByAppendingPathComponent:@"profiles.ini"] ;
}
+ (NSArray*)profilePseudonymsForHomePath:(NSString*)homePath {
NSString* filepathProfilesIni = [self profilesFilePathForHomePath:homePath] ;
NSString* profilesIniContents = [[NSString alloc] initWithContentsOfFile:filepathProfilesIni
usedEncoding:NULL
error:NULL] ;
NSArray* pseudonyms = nil ;
if (profilesIniContents) {
// There is a profiles.ini file. Parse it.
NSScanner* scanner = nil ;
NSMutableArray* outbasket = [[NSMutableArray alloc] init] ;
NSString* path ;
if(profilesIniContents) {
scanner = [[NSScanner alloc] initWithString:profilesIniContents] ;
while((![scanner isAtEnd])) {
[scanner scanUpToString:@"Path=" intoString:NULL] ;
if ([scanner scanString:@"Path=" intoString:NULL]) {
// Without the "if" above, it will add the last name found to outbasket twice
[scanner scanUpToString:@"\n" intoString:&path] ;
NSString* dirName = [path lastPathComponent] ;
NSScanner* innerScanner = [[NSScanner alloc] initWithString:dirName] ;
[innerScanner scanUpToString:@"." intoString:NULL] ;
NSInteger location = [innerScanner scanLocation] + 1 ;
if (location < ([dirName length] - 1)) {
NSString* pseudonym = [dirName substringFromIndex:location] ;
[outbasket addObject:pseudonym] ;
}
[innerScanner release];
}
}
}
pseudonyms = [NSArray arrayWithArray:outbasket] ;
[outbasket release] ;
[scanner release] ;
}
[profilesIniContents release] ;
return pseudonyms ;
}
+ (NSArray*)profileNames {
NSString* filepathProfilesIni = [self profilesFilePathForHomePath:NSHomeDirectory()] ;
NSString* profilesIniContents = [[NSString alloc] initWithContentsOfFile:filepathProfilesIni
usedEncoding:NULL
error:NULL] ;
NSArray* names = nil ;
if (profilesIniContents) {
// There is a profiles.ini file. Parse it.
NSScanner* scanner = nil ;
NSMutableArray* outbasket = [[NSMutableArray alloc] init] ;
NSString* name ;
if(profilesIniContents) {
scanner = [[NSScanner alloc] initWithString:profilesIniContents] ;
while((![scanner isAtEnd])) {
[scanner scanUpToString:@"Name=" intoString:NULL] ;
if ([scanner scanString:@"Name=" intoString:NULL]) {
[scanner scanUpToString:@"\n" intoString:&name] ;
[outbasket addObject:name] ;
// Without the "if" above, it will add the last name found to outbasket twice
}
}
}
names = [NSArray arrayWithArray:outbasket] ;
[outbasket release] ;
[scanner release] ;
}
[profilesIniContents release] ;
return names ;
}
+ (NSString*)displayedSuffixForProfileName:(NSString*)profileName
homePath:(NSString*)homePath {
NSString* suffix ;
if ([[self profileNames] count] > 1) {
suffix = [NSString stringWithFormat:
@" (%@)",
profileName] ;
}
else {
suffix = @"" ;
}
return suffix ;
}
+ (NSString*)randomProfilePrefix {
// Emulate what Firfox does; 8 characters randomly selected from the set
// of lower-case Roman alphas, and decimal digits. These are ASCII values
// 97-122 and 48-57.
unichar chars[8] ;
for (NSInteger i=0; i<8; i++) {
NSInteger c = random() ;
// There are 36 characters in our alpha+num alphabet
c = c % 36 ;
if (c<10) {
// Make it a decimal digit
c += 48 ;
}
else {
// Make it a lower-case Roman
c += (97 - 10) ;
}
chars[i] = (unichar)c ;
}
return [NSString stringWithCharacters:chars
length:8] ;
}
+ (NSString*)profilesIniContentsForHomePath:(NSString*)homePath
error_p:(NSError**)error_p {
BOOL ok = YES ;
NSError* error = nil ;
NSString* filepathProfilesIni = [self profilesFilePathForHomePath:homePath] ;
NSString* contents = [[NSString alloc] initWithContentsOfFile:filepathProfilesIni
usedEncoding:NULL
error:NULL] ;
if (!contents && ![homePath isEqualToString:NSHomeDirectory()]) {
// That second qualification above was added in BookMacster 1.13.2,
// so that we can handle having the profiles.ini file not exist at this
// at this point, if we're talking about this user's home bookmarks.
// This may happen if desired profile is on an Other Mac Account,
// where we don't have read permission.
// Try copying the file to a temporary location using
// a privileged-if-necessary Helper tool
// If we are in a Firefox extension, crashers are ahead,
// so we need to exit.
NSString* executablePath = [[NSBundle mainBundle] executablePath] ;
NSString* executableName = [executablePath lastPathComponent] ;
if ([[executableName lowercaseString] hasPrefix:@"firefox"]) {
NSLog(@"Internal Error 620-5951") ;
error = SSYMakeError(529484, @"Attempted Cross-Home in Firefox extension") ;
}
else {
// OK, we're not in Firefox
NSString* tempPath = [[NSFileManager defaultManager] temporaryFilePath] ;
ok = [[NSFileManager defaultManager] copyItemAtPath:filepathProfilesIni
toPath:tempPath
error:&error] ;
if (!ok) {
goto end ;
}
// Now try reading the copied temporary file
contents = [[NSString alloc] initWithContentsOfFile:tempPath
usedEncoding:NULL
error:NULL] ;
// Delete the temporary file
BOOL trivialOk = [[NSFileManager defaultManager] removeItemAtPath:tempPath
error:&error] ;
if (!trivialOk) {
// Unable to delete temporary file. Unimportant error.
// Do not display to user, just log it.
NSLog(@"Internal Error 635-8292 %@", [error longDescription]) ;
}
}
}
end:
if (error && error_p) {
*error_p = error ;
}
return [contents autorelease] ;
}
+ (NSString*)pathForProfileName:(NSString*)profileName
homePath:(NSString*)homePath
error_p:(NSError**)error_p {
NSString* fullPath = nil ;
BOOL ok = YES ;
NSError* error = nil ;
NSString* profilesIniContents = [self profilesIniContentsForHomePath:homePath
error_p:&error] ;
if (error) {
goto end ;
}
if (profilesIniContents) {
NSScanner* scanner = [[NSScanner alloc] initWithString:profilesIniContents] ;
NSString* nameLine = [[NSString alloc] initWithFormat:@"Name=%@\n", profileName] ;
NSInteger isRelative = 1 ; // Try and fail safe; since I have always seen it as = 1.
NSString* pathFromDisk = nil;
[scanner scanUpToString:nameLine intoString:NULL] ;
[scanner scanString:nameLine intoString:NULL] ;
[scanner scanString:@"IsRelative=" intoString:NULL] ;
[scanner scanInteger:&isRelative] ;
[scanner scanString:@"Path=" intoString:NULL] ;
[scanner scanUpToString:@"\n" intoString:&pathFromDisk] ;
if (isRelative) {
// In this case, pathFromDisk is a "plain text" path
fullPath = [[self browserSupportPathForHomePath:homePath] stringByAppendingPathComponent:pathFromDisk] ;
}
else {
if ([pathFromDisk hasPrefix:@"/"]) {
fullPath = pathFromDisk;
}
else {
/* Well, then, maybe pathFromDisk is a base64 encoded alias
record. I don't remember this today (20171102), but I must
seen this at one time or I would not have written this code.
So, let's give it a try: */
NSData* aliasRecord = [pathFromDisk dataBase64Decoded];
fullPath = [aliasRecord pathFromAliasRecordWithTimeout:3.0
error_p:NULL];
if (!fullPath) {
NSString* appSupportRelativePath = [self appSupportRelativePath] ;
NSLog(@"Internal Error 234-5245 Could not get path from alias record in %@ profile %@.\nPossible corruption in %@",
appSupportRelativePath,
profileName,
[self profilesFilePathForHomePath:homePath]) ;
}
}
}
[scanner release] ;
[nameLine release] ;
}
else {
// Make one up.
fullPath = [self browserSupportPathForHomePath:homePath] ;
fullPath = [fullPath stringByAppendingPathComponent:@"Profiles"] ;
NSString* folderName = [[self randomProfilePrefix] stringByAppendingDotSuffix:[self defaultProfileName]] ;
fullPath = [fullPath stringByAppendingPathComponent:folderName] ;
// Also make up and install a profiles.ini file which points to it.
NSString* profileText = [NSString stringWithFormat:
@"[General]\n"
@"StartWithLastProfile=1\n"
@"\n"
@"[Profile0]\n"
@"Name=%@\n"
@"IsRelative=1\n"
@"Path=Profiles/%@\n"
@"Default=1\n",
[self defaultProfileName],
folderName] ;
NSString* filepathProfilesIni = [self profilesFilePathForHomePath:homePath] ;
[[NSFileManager defaultManager] createDirectoryAtPath:[filepathProfilesIni stringByDeletingLastPathComponent]
withIntermediateDirectories:YES
attributes:nil
error:NULL] ;
[profileText writeToFile:filepathProfilesIni
atomically:YES
encoding:NSUTF8StringEncoding
error:NULL] ;
}
end:;
if (!ok && !error) {
NSString* msg = [NSString stringWithFormat:
@"%@ cannot access file at path:\n%@",
[[NSProcessInfo processInfo] processName],
[self profilesFilePathForHomePath:homePath]] ;
error = [NSError errorWithDomain:[NSError myDomain]
code:254938
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
msg, NSLocalizedDescriptionKey,
@"The above file is a required piece of Firefox. Make sure it is there. If not, launch Firefox to create a new user profile.", NSLocalizedRecoverySuggestionErrorKey,
nil]] ;
}
if (error_p) {
*error_p = error ;
}
return fullPath ;
}
+ (NSString*)profileNameForPath:(NSString*)path
error_p:(NSError**)error_p {
NSString* profileName = nil ;
BOOL ok = YES ;
NSError* error = nil ;
/*
We need to extract the "home" path from the given path. We assume it
will be a substring of the given path, starting from the beginning and
including one path component beyond "Users". OK, if the home path is
on a mounted volume that is named "Users", for example /Volumes/Users/,
so that the correct answer is, for example, "/Volumes/Users/Users/suzie",
then this code will give the wrong answer, "/Volumes/Users/Users".
Does anyone know a better way to do this?
*/
NSString* homePath = nil ;
NSArray* pathComponents = [path pathComponents] ;
NSMutableArray* homePathComponents = [[NSMutableArray alloc] init] ;
BOOL gotHome = NO ;
for (NSInteger i=0; i<([pathComponents count] - 1);) {
NSString* component = [pathComponents objectAtIndex:i++] ;
[homePathComponents addObject:component] ;
if ([component isEqualToString:@"Users"]) {
[homePathComponents addObject:[pathComponents objectAtIndex:i]] ;
gotHome = YES ;
break ;
}
}
if (gotHome) {
homePath = [homePathComponents componentsJoinedByString:@"/"] ;
if ([homePath hasPrefix:@"//"]) {
homePath = [homePath substringFromIndex:1] ;
}
}
[homePathComponents release] ;
NSString* profilesIniContents = [self profilesIniContentsForHomePath:homePath
error_p:&error] ;
if (error) {
goto end ;
}
if (profilesIniContents) {
NSScanner* scanner = [[NSScanner alloc] initWithString:profilesIniContents] ;
[scanner setCharactersToBeSkipped:nil] ;
while (![scanner isAtEnd] && !profileName) {
NSString* thisProfileString = nil ;
[scanner scanUpToString:@"[Profile" intoString:NULL] ;
[scanner scanUpToString:@"\n" intoString:NULL] ;
[scanner scanString:@"\n" intoString:NULL] ;
[scanner scanUpToString:@"[Profile" intoString:&thisProfileString] ;
NSScanner* subscanner = [[NSScanner alloc] initWithString:thisProfileString] ;
BOOL thisProfileDone = NO ;
NSString* thisProfileName = nil ;
NSString* thisRawPath = nil ;
NSInteger thisIsRelative = -1 ;
while (!thisProfileDone) {
if (!thisProfileName) {
[subscanner scanUpToString:@"Name=" intoString:NULL] ;
[subscanner scanString:@"Name=" intoString:NULL] ;
[subscanner scanUpToString:@"\n" intoString:&thisProfileName] ;
[subscanner scanString:@"\n" intoString:NULL] ;
}
if (!thisRawPath) {
[subscanner setScanLocation:0] ;
[subscanner scanUpToString:@"Path=" intoString:NULL] ;
[subscanner scanString:@"Path=" intoString:NULL] ;
[subscanner scanUpToString:@"\n" intoString:&thisRawPath] ;
[subscanner scanString:@"\n" intoString:NULL] ;
}
if (thisIsRelative == -1) {
[subscanner setScanLocation:0] ;
[subscanner scanUpToString:@"IsRelative=" intoString:NULL] ;
[subscanner scanString:@"IsRelative=" intoString:NULL] ;
[subscanner scanInteger:&thisIsRelative] ;
[subscanner scanString:@"\n" intoString:NULL] ;
}
if (thisProfileName && thisRawPath && (thisIsRelative != -1)) {
thisProfileDone = YES ;
NSString* thisFullPath = nil ;
NSString* filepathProfilesIni = [self profilesFilePathForHomePath:homePath] ;
if (thisIsRelative == 1) {
// In this case, pathFromDisk is a "plain text" path
thisFullPath = [[self browserSupportPathForHomePath:homePath] stringByAppendingPathComponent:thisRawPath] ;
}
else if (thisIsRelative == 0) {
// In this case, pathFromDisk is a base64 encoded alias record
NSData* aliasRecord = [thisRawPath dataBase64Decoded] ;
thisFullPath = [aliasRecord pathFromAliasRecordWithTimeout:3.0
error_p:NULL] ;
if (!thisFullPath) {
NSString* appSupportRelativePath = [self appSupportRelativePath] ;
NSLog(@"Internal Error 234-5897 Could not get path from alias record in %@ profile %@.\nPossible corruption in %@",
appSupportRelativePath,
thisProfileName,
filepathProfilesIni) ;
}
}
else {
NSLog(@"Internal Error 502-9595 for %@", filepathProfilesIni) ;
}
const char* path1 = [thisFullPath fileSystemRepresentation] ;
const char* path2 = [path fileSystemRepresentation] ;
if (path1 && path2) {
if (strcmp(path1, path2) == 0) {
profileName = thisProfileName ;
}
}
}
if ([subscanner isAtEnd]) {
thisProfileDone = YES ;
}
}
[subscanner release] ;
}
[scanner release] ;
}
end:;
if (!ok && !error) {
NSString* msg = [NSString stringWithFormat:
@"%@ cannot access file at path:\n%@",
[[NSProcessInfo processInfo] processName],
[self profilesFilePathForHomePath:homePath]] ;
error = [NSError errorWithDomain:[NSError myDomain]
code:254938
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
msg, NSLocalizedDescriptionKey,
@"The above file is a required piece of Firefox. Make sure it is there. If not, launch Firefox to create a new user profile.", NSLocalizedRecoverySuggestionErrorKey,
nil]] ;
}
if (error_p) {
*error_p = error ;
}
return profileName ;
}
#if 0
// Old code which is 99% simpler but only works for 99+% of users
/*
I've seen the last path component can come in three different forms…
* lastPathComponent
of profile path We want to extract
- ----------------- ------------------
* Form 1: ieri3hz1.default default
* Form 2: odi44oee.Joe-Doe Joe-Doe
* Form 3: Joe-Doe Joe-Doe
Those names could also have spaces instead of dashes.
Until version 286 (May 2012, BookMacster 1.11), we examined the
-pathExtension and if it was "default", extracted the profile name as
"default", otherwise simply passed through the lastPathComponent.
That worked for Forms 1 and 3, but not 2 where we got an unwanted prefix.
Then until version 295, we always extracted the path extension. That
worked for Forms 1 and 2 but broke it for Form 3, where we got an empty
string. Starting with version 295, for God's sake this seems to cover all
3 cases.
*/
DEFUNCT CODE:
NSString* profileNameFromPath(NSString* path) {
NSString* lastPathComponent = [path lastPathComponent] ;
NSString* extension = [lastPathComponent pathExtension] ;
NSString* extractedProfileName ;
if ([extension length] > 0) {
// Form 1 or 2
extractedProfileName = extension ;
}
else {
// Form 3
extractedProfileName = lastPathComponent ;
}
return extractedProfileName ;
}
#endif
@end