-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathHelper.m
290 lines (234 loc) · 9.34 KB
/
Helper.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
/*
This file is part of the TVShows source code.
http://github.com/mattprice/TVShows
TVShows is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
#import "Helper.h"
#import <Security/Security.h>
/* Thanks to http://boinc.berkeley.edu/source_code.php */
static AuthorizationRef gOurAuthRef = NULL;
static char chownPath[] = "/usr/sbin/chown";
static OSStatus getAuthorization(void) {
static Boolean sIsAuthorized = false;
AuthorizationRights ourAuthRights;
AuthorizationFlags ourAuthFlags;
AuthorizationItem ourAuthRightsItem[1];
AuthorizationEnvironment ourAuthEnvironment;
AuthorizationItem ourAuthEnvItem[1];
char prompt[] = "TVShows needs to access to the ~/Library/LaunchAgents folder.\n\n";
OSStatus err = noErr;
if (sIsAuthorized)
return noErr;
ourAuthRights.count = 0;
ourAuthRights.items = NULL;
err = AuthorizationCreate (&ourAuthRights, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &gOurAuthRef);
if (err != noErr) {
return err;
}
ourAuthRightsItem[0].name = kAuthorizationRightExecute;
ourAuthRightsItem[0].value = chownPath;
ourAuthRightsItem[0].valueLength = strlen (chownPath);
ourAuthRightsItem[0].flags = 0;
ourAuthRights.count = 1;
ourAuthRights.items = ourAuthRightsItem;
ourAuthEnvItem[0].name = kAuthorizationEnvironmentPrompt;
ourAuthEnvItem[0].value = prompt;
ourAuthEnvItem[0].valueLength = strlen(prompt);
ourAuthEnvItem[0].flags = 0;
ourAuthEnvironment.count = 1;
ourAuthEnvironment.items = ourAuthEnvItem;
ourAuthFlags = kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights;
err = AuthorizationCopyRights (gOurAuthRef, &ourAuthRights, &ourAuthEnvironment, ourAuthFlags, NULL);
if (err == noErr)
sIsAuthorized = true;
return err;
}
OSStatus chownToUidAndGidAtPath(uid_t uid, gid_t gid, char *path) {
short i;
char *args[3];
OSStatus err;
FILE *ioPipe;
char *p, junk[256];
err = getAuthorization();
if (err != noErr) {
//if (err == errAuthorizationCanceled)
return err;
} else {
for (i=0; i<5; i++) { // Retry 5 times if error
char *arg0;
asprintf(&arg0,"%d:%d",uid,gid);
args[0] = arg0;
args[1] = path;
args[2] = NULL;
err = AuthorizationExecuteWithPrivileges(gOurAuthRef, chownPath, 0, args, &ioPipe);
// We use the pipe to signal us when the command has completed
do {
p = fgets(junk, sizeof(junk), ioPipe);
} while (p);
fclose (ioPipe);
if (err == noErr)
break;
}
}
return err;
}
@implementation Helper
- (id) init {
self = [super init];
if (self != nil) {
NSFileManager *fm = [NSFileManager defaultManager];
NSString *bundleName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
NSString *bundleNamePlist = [bundleName stringByAppendingPathExtension:@"plist"];
bundleIdentifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]; // Don't move this line, it is needed for the next one
NSString *bundleIdentifierPlist = [bundleIdentifier stringByAppendingPathExtension:@"plist"];
NSString *resourcesScriptFolder = [[NSBundle mainBundle] pathForResource:@"TVShowsScript" ofType:nil];
NSString *launchAgentsFolder;
//NSString *script = [[NSBundle mainBundle] pathForResource:@"TVShows" ofType:@"rb"];
libraryFolder = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) objectAtIndex:0];
applicationSupportFolder = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask,YES) objectAtIndex:0];
applicationApplicationSupportFolder = [applicationSupportFolder stringByAppendingPathComponent:bundleName];
preferencesFolder = [libraryFolder stringByAppendingPathComponent:@"Preferences"];
preferencesFile = [preferencesFolder stringByAppendingPathComponent:bundleIdentifierPlist];
showsPath = [applicationApplicationSupportFolder stringByAppendingPathComponent:bundleNamePlist];
launchAgentsFolder = [libraryFolder stringByAppendingPathComponent:@"LaunchAgents"];
launchdPlistPath = [launchAgentsFolder stringByAppendingPathComponent:bundleIdentifierPlist];
scriptFolder = [applicationApplicationSupportFolder stringByAppendingPathComponent:[resourcesScriptFolder lastPathComponent]];
scriptPath = [[applicationApplicationSupportFolder stringByAppendingPathComponent:[resourcesScriptFolder lastPathComponent]] stringByAppendingPathComponent:@"TVShows.rb"];
logPath = [applicationApplicationSupportFolder stringByAppendingPathComponent:[bundleName stringByAppendingPathExtension:@"log"]];
[libraryFolder retain];
[applicationSupportFolder retain];
[applicationApplicationSupportFolder retain];
[preferencesFolder retain];
[preferencesFile retain];
[showsPath retain];
[launchdPlistPath retain];
[scriptPath retain];
[logPath retain];
[scriptFolder retain];
[bundleIdentifier retain];
[self createFolderAtPath:applicationSupportFolder];
[self createFolderAtPath:applicationApplicationSupportFolder];
[self createFolderAtPath:launchAgentsFolder];
[self chmodFolderAtPath:launchAgentsFolder];
// Remove scriptFolder if we have a newer version to install
if ([fm fileExistsAtPath:scriptPath]) {
NSString *scriptVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"ScriptVersion"];
if (!scriptVersion || [scriptVersion compare:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] options:NSCaseInsensitiveSearch] )
if (![fm removeFileAtPath:scriptFolder handler:nil])
[Helper dieWithErrorMessage:@"Could not remove the TVShows script folder in the Application Support folder in order to install the new one."];
}
// Install the script in the Application Support folder
if (![fm fileExistsAtPath:scriptFolder]) {
if ( ![fm copyPath:resourcesScriptFolder toPath:scriptFolder handler:nil] ) {
[Helper dieWithErrorMessage:@"Could not copy the TVShows script in the Application Support folder."];
} else {
// Chmod the scripts to make sure they're executable
[fm changeFileAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0744] forKey:NSFilePosixPermissions] atPath:scriptPath];
// Set the new version
[[NSUserDefaults standardUserDefaults] setObject:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] forKey:@"ScriptVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
// Chmod the ruby scripts in the resource folder
NSEnumerator *scripts = [[[NSBundle mainBundle] pathsForResourcesOfType:@"rb" inDirectory:@""] objectEnumerator];
NSString *script;
while ( script = [scripts nextObject] ) {
[fm changeFileAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0744] forKey:NSFilePosixPermissions] atPath:script];
}
}
return self;
}
- (void) dealloc {
[libraryFolder release];
[applicationSupportFolder release];
[applicationApplicationSupportFolder release];
[preferencesFolder release];
[preferencesFile release];
[showsPath release];
[launchdPlistPath release];
[scriptPath release];
[logPath release];
[scriptFolder release];
[bundleIdentifier release];
[super dealloc];
}
- (void)createFolderAtPath: (NSString *)path
{
NSFileManager *fm = [NSFileManager defaultManager];
if ( ![fm fileExistsAtPath:path] )
if ( ![fm createDirectoryAtPath:path attributes:nil] )
[Helper dieWithErrorMessage:[NSString stringWithFormat:@"The folder %@ doesn't exist and I can't create it.",path]];
}
- (void)chmodFolderAtPath: (NSString *)path
{
NSFileManager *fm = [NSFileManager defaultManager];
if ( ![fm changeFileAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0755] forKey:@"NSFilePosixPermissions"] atPath:path] ) {
if ( noErr == chownToUidAndGidAtPath(getuid(),getgid(),(char *)[path cStringUsingEncoding:NSUTF8StringEncoding]) ) {
if ( ![fm changeFileAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0755] forKey:@"NSFilePosixPermissions"] atPath:path] ) {
[Helper dieWithErrorMessage:[NSString stringWithFormat:@"Could not change permissions for %@",path]];
}
} else {
[Helper dieWithErrorMessage:[NSString stringWithFormat:@"I'm not authorized to change permissions for %@.",path]];
}
}
}
- (NSString *)libraryFolder
{
return libraryFolder;
}
- (NSString *)applicationSupportFolder
{
return applicationSupportFolder;
}
- (NSString *)applicationApplicationSupportFolder
{
return applicationApplicationSupportFolder;
}
- (NSString *)preferencesFolder
{
return preferencesFolder;
}
- (NSString *)preferencesFile
{
return preferencesFile;
}
- (NSString *)showsPath
{
return showsPath;
}
- (NSString *)launchdPlistPath
{
return launchdPlistPath;
}
- (NSString *)scriptPath
{
return scriptPath;
}
- (NSString *)logPath
{
return logPath;
}
- (NSString *)scriptFolder
{
return scriptFolder;
}
- (NSString *)bundleIdentifier
{
return bundleIdentifier;
}
+ (void)dieWithErrorMessage: (NSString *)message
{
NSRunAlertPanel(@"Alert", message, @"Quit", nil, nil);
[[NSApplication sharedApplication] terminate:self];
}
+ (NSNumber *)negate: (NSNumber *)n
{
if ( [n boolValue] ) {
return [NSNumber numberWithBool:NO];
}
return [NSNumber numberWithBool:YES];
}
@end