-
Notifications
You must be signed in to change notification settings - Fork 2
/
TotalSpacesInjector.m
190 lines (159 loc) · 6.87 KB
/
TotalSpacesInjector.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
#import <Foundation/Foundation.h>
#import "TSStandardVersionComparator.h"
#define TOTALSPACES_STANDARD_INSTALL_LOCATION "/Applications/TotalSpaces2.app"
// SIMBL-compatible interface
@interface TotalSpacesPlugin: NSObject {
}
- (void) install;
@end
// just a dummy class for locating our bundle
@interface TotalSpacesInjector: NSObject {
}
@end
@implementation TotalSpacesInjector {
}
@end
static bool alreadyLoaded = false;
typedef struct {
NSString* location;
} configuration;
OSErr AEPutParamString(AppleEvent *event, AEKeyword keyword, NSString* string) {
UInt8 *textBuf;
CFIndex length, maxBytes, actualBytes;
length = CFStringGetLength((CFStringRef)string);
maxBytes = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
textBuf = malloc(maxBytes);
if (textBuf) {
CFStringGetBytes((CFStringRef)string, CFRangeMake(0, length), kCFStringEncodingUTF8, 0, true, (UInt8 *)textBuf, maxBytes, &actualBytes);
OSErr err = AEPutParamPtr(event, keyword, typeUTF8Text, textBuf, actualBytes);
free(textBuf);
return err;
} else {
return memFullErr;
}
}
static void reportError(AppleEvent *reply, NSString* msg) {
NSLog(@"ts2: TotalSpacesInjector: %@", msg);
AEPutParamString(reply, keyErrorString, msg);
}
static NSString* checkSignature(CFURLRef bundleURL, CFStringRef requirementString) {
CFErrorRef error = NULL;
SecStaticCodeRef staticCode = NULL;
SecStaticCodeCreateWithPath(bundleURL, kSecCSDefaultFlags, &staticCode);
if (!staticCode) {
return @"SecStaticCodeCreateWithPath returned no staticCode";
}
SecRequirementRef requirementRef = NULL;
OSStatus requirementCreateStatus = SecRequirementCreateWithStringAndErrors(requirementString, kSecCSDefaultFlags, &error, &requirementRef);
if (error) {
if (requirementRef) {
CFRelease(requirementRef);
}
NSString* result = [NSString stringWithFormat:@"SecRequirementCreateWithStringAndErrors reported %@", error];
CFRelease(error);
return result;
}
if (requirementCreateStatus != errSecSuccess) {
if (requirementRef) {
CFRelease(requirementRef);
}
return [NSString stringWithFormat:@"SecRequirementCreateWithString returned error %d)", (int)requirementCreateStatus];
}
SecCSFlags flags = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures | kSecCSCheckNestedCode);
OSStatus signatureCheckResult = SecStaticCodeCheckValidityWithErrors(staticCode, flags, requirementRef, &error);
CFRelease(requirementRef);
CFRelease(staticCode);
if (error) {
NSString* result = [NSString stringWithFormat:@"SecStaticCodeCheckValidityWithErrors reported %@", error];
CFRelease(error);
return result;
}
if (signatureCheckResult != errSecSuccess) {
return [NSString stringWithFormat:@"SecStaticCodeCheckValidityWithErrors returned %d", (int)signatureCheckResult];
}
return nil;
}
NSBundle *TSAddBundle(NSString *bundleName, AppleEvent *reply)
{
NSBundle* pluginBundle = nil;
NSString *resource = nil;
CFBundleRef tsBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.binaryage.TotalSpaces2"));
if (tsBundle) {
CFURLRef pathURL = CFBundleCopyBundleURL(tsBundle);
if (pathURL) {
CFStringRef path = CFURLCopyFileSystemPath(pathURL, kCFURLPOSIXPathStyle);
NSString *resource = [NSString stringWithFormat:@"%@/Contents/Plugins/%@.bundle", path, bundleName];
pluginBundle = [NSBundle bundleWithPath:resource];
}
}
if (!pluginBundle) {
NSString *path = @TOTALSPACES_STANDARD_INSTALL_LOCATION; // try the default location
NSString *resource = [NSString stringWithFormat:@"%@/Contents/Plugins/%@.bundle", path, bundleName];
pluginBundle = [NSBundle bundleWithPath:resource];
if (!pluginBundle) {
reportError(reply, [NSString stringWithFormat:@"Unable to create bundle from path: %@ [%@]", resource, path]);
return nil;
}
}
NSString *errStr = checkSignature((CFURLRef)pluginBundle.bundleURL, CFSTR("anchor apple generic and certificate leaf[subject.O] = \"Stephen Sykes\""));
if (errStr) {
reportError(reply, [NSString stringWithFormat:@"Bundle failed checks: %@ [%@]", errStr, pluginBundle.bundlePath]);
return nil;
}
NSError* error;
if (![pluginBundle loadAndReturnError:&error]) {
reportError(reply, [NSString stringWithFormat:@"Unable to load bundle from path: %@ error: %@", resource, [error localizedDescription]]);
return nil;
} else {
NSLog(@"ts2: [TotalSpaces] Loaded bundle from path: %@", resource);
}
return pluginBundle;
}
OSErr HandleInitEvent(const AppleEvent *ev, AppleEvent *reply, long refcon) {
NSBundle* injectorBundle = [NSBundle bundleForClass:[TotalSpacesInjector class]];
NSString* injectorVersion = [injectorBundle objectForInfoDictionaryKey:@"CFBundleVersion"];
if (!injectorVersion || ![injectorVersion isKindOfClass:[NSString class]]) {
reportError(reply, [NSString stringWithFormat:@"Unable to determine TotalSpacesInjector version!"]);
return 7;
}
NSLog(@"ts2: [TotalSpaces] TotalSpacesInjector v%@ received init event", injectorVersion);
if (alreadyLoaded) {
NSLog(@"ts2: [TotalSpaces] TotalSpacesInjector: TotalSpaces has been already loaded. Ignoring this request.");
return noErr;
}
@try {
NSBundle* dockBundle = [NSBundle mainBundle];
if (!dockBundle) {
reportError(reply, [NSString stringWithFormat:@"Unable to locate main Dock bundle!"]);
return 4;
}
NSString* dockVersion = [dockBundle objectForInfoDictionaryKey:@"CFBundleVersion"];
if (!dockVersion || ![dockVersion isKindOfClass:[NSString class]]) {
reportError(reply, [NSString stringWithFormat:@"Unable to determine Dock version!"]);
return 5;
}
NSBundle *tsBundle = TSAddBundle(@"TotalSpaces", reply);
if (!tsBundle) return 2;
TotalSpacesPlugin* principalClass = (TotalSpacesPlugin*)[tsBundle principalClass];
if (!principalClass) {
reportError(reply, [NSString stringWithFormat:@"Unable to retrieve principalClass for bundle: %@", tsBundle]);
return 3;
}
if ([principalClass respondsToSelector:@selector(install)]) {
NSLog(@"ts2: [TotalSpaces] TotalSpacesInjector: Installing TotalSpaces ...");
[principalClass install];
}
alreadyLoaded = true;
return noErr;
} @catch (NSException* exception) {
reportError(reply, [NSString stringWithFormat:@"Failed to load TotalSpaces with exception: %@", exception]);
}
return 1;
}
OSErr HandleCheckEvent(const AppleEvent *ev, AppleEvent *reply, long refcon) {
if (alreadyLoaded) {
return noErr;
}
reportError(reply, @"TotalSpaces not loaded");
return 1;
}