forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSCronJob.m
595 lines (510 loc) · 18.7 KB
/
SSCronJob.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
The cron utility uses two different types of configuration files called "crontabs": "system" and "user".
system crontab
There is only one such file
is in /private/etc/crontab (aka /etc/crontab)
user crontabs
Each user, including root, may or may not have one.
are in /private/var/cron/tabs/aName (aka /var/cron/tabs/aName)
aName of each file is the short name of the user who created it
The system crontab [has] the ability to run commands as any user. In a user crontab, commands run as the user who created the crontab; this is an important security feature.
Note: User crontabs allow individual users to schedule tasks without the need for root privileges. Commands in a user's crontab run with the permissions of the user who owns the crontab.
The root user can have a user crontab just like any other user. This one is different from /etc/crontab (the system crontab). Because of the system crontab, there is usually no need to create a user crontab for root.
*/
#import "SSAuthorizationTool.h"
#import "SSAuthorizedFunctions.h"
#import "SSAuthorizedTool.h"
#import "SSAppGlobals.h"
#import "SSUtils.h"
#import "SSCronJob.h"
#import "SSUtilityCategories.h"
// globals
extern int gLogging ;
NSString* SSLocalizedDayOfWeekFromCronNumber(int n) {
NSString* dayName = nil ;
switch (n) {
case 0:
case 7:
dayName = @"weekdaySun" ;
break;
case 1:
dayName = @"weekdayMon" ;
break;
case 2:
dayName = @"weekdayTue" ;
break;
case 3:
dayName = @"weekdayWed" ;
break;
case 4:
dayName = @"weekdayThu" ;
break;
case 5:
dayName = @"weekdayFri" ;
break;
case 6:
dayName = @"weekdaySat" ;
}
return [NSString localize:dayName] ;
}
NSArray* SSLocalizedDaysOfWeekFromCronNumbers(NSArray* numbers) {
NSMutableArray* days = [[NSMutableArray alloc] init] ;
NSEnumerator* e = [numbers objectEnumerator] ;
NSNumber* number ;
while ((number = [e nextObject])) {
int dayNumber = [number intValue] ;
NSString* dayName = SSLocalizedDayOfWeekFromCronNumber(dayNumber) ;
[days addObject:dayName] ;
}
NSArray* output = [days copy] ;
[days release] ;
return [output autorelease] ;
}
NSString* SSReadUserCrontabForCurrentUser() {
NSArray* args = [NSArray arrayWithObject:@"-l"] ;
NSData* cronData ;
NSData* stdErrData ;
int taskResult = SSDoShellTask(@"/usr/bin/crontab", args, nil, nil, &cronData, &stdErrData, YES) ;
NSString* stdErrString = [[NSString alloc] initWithData:stdErrData encoding:[NSString defaultCStringEncoding]] ;
// Check for "no crontab for" in stderr, and if so over-ride the taskResult to OK, since it
// is OK if the user does not have a crontab yet.
if ( [stdErrString isLike: @"*no crontab for*" ] ) {
taskResult = 0; // 0=OK
}
else if ([stdErrString length] > 0) {
NSLog(@"SSReadUserCrontabForCurrentUser read crontab, got stderr: \"%@\"", stdErrString) ;
}
[stdErrString release] ;
NSString* crontabString = [[NSString alloc] initWithData:cronData encoding:[NSString defaultCStringEncoding]] ;
SSLog(5, "got user crontabString:%@", crontabString) ;
return [crontabString autorelease] ;
}
NSString* SSReadSystemCrontab() {
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath: @"/etc/crontab"] ;
NSData *data ;
NSString* string ;
if (fh) {
data = [fh readDataToEndOfFile] ;
string = [[NSString alloc] initWithData:data encoding:[NSString defaultCStringEncoding]] ;
}
return [string autorelease] ;
}
@implementation SSCronJob
SSAOm(NSString*, commentOut, setCommentOut)
SSAOm(NSArray*, minutes, setMinutes)
SSAOm(NSArray*, hours, setHours)
SSAOm(NSArray*, days, setDays)
SSAOm(NSArray*, dates, setDates)
SSAOm(NSArray*, months, setMonths)
SSAOm(NSString*, user, setUser)
SSAOm(NSString*, directory, setDirectory)
SSAOm(NSString*, filename, setFilename)
// Methods for parsing crontab numeric fields
// Base Method
// if no numbers or indicators found in field, returns empty array
// if wildcard * is found, returns an array containing one object, the string @"*"
// If array cannot be parsed, returns nil
- (NSArray*)arrayFromCrontabField:(NSString*)field {
NSMutableArray* array = [[NSMutableArray alloc] init] ;
NSException* exception = [NSException
exceptionWithName:@"parse error"
reason:@"can't parse"
userInfo:nil] ;
NS_DURING
if ([field rangeOfString:@"*"].location != NSNotFound) {
// field contains the wildcard "*"
[array addObject:@"*"] ;
}
else {
NSArray* clauses = [field componentsSeparatedByString:@","] ;
NSEnumerator* e = [clauses objectEnumerator] ;
NSString* clause ;
while ((clause = [e nextObject]))
{
NSRange stepValue = [clause rangeOfString:@"/"] ;
if (stepValue.location != NSNotFound) {
// We don't support step values
[exception raise] ;
}
NSRange dash = [clause rangeOfString:@"-"] ;
if (dash.location == NSNotFound) {
// This is a single value
[array addObject:[NSNumber numberWithInt:[clause intValue]]] ;
}
else {
// This is a range, such as "1-5"
NSRange lowerLimitRange, upperLimitRange ;
lowerLimitRange.location = 0 ;
lowerLimitRange.length = dash.location ;
upperLimitRange.location = dash.location + 1 ;
upperLimitRange.length = [clause length] - 1 - lowerLimitRange.length ;
int lowerLimit = [[clause substringWithRange:lowerLimitRange] intValue] ;
int upperLimit = [[clause substringWithRange:upperLimitRange] intValue] ;
int i ;
for (i=lowerLimit; i<=upperLimit; i++) {
[array addObject:[NSNumber numberWithInt:i]] ;
}
}
}
}
NS_HANDLER
[array release] ;
array = nil ;
NS_ENDHANDLER
NSArray* output = [array copy] ;
[array release] ;
return [output autorelease] ;
}
// Specialized Methods
// If the field cannot be parsed, these methods will set the subject instance variable
// to nil and return NO. If all goes well, they return YES
- (BOOL)setMinutesFromCronField:(NSString*)field {
NSArray* array = [self arrayFromCrontabField:field] ;
BOOL ok = YES;
if (!array) {
ok = NO ;
}
else if (([array count] ==1) && ([[array objectAtIndex:0] isEqual:@"*"])) {
array = [NSArray arrayOfIntegersStartingWith:0 endingWith:59] ;
}
[self setMinutes:array] ;
return ok ;
}
- (BOOL)setHoursFromCronField:(NSString*)field {
NSArray* array = [self arrayFromCrontabField:field] ;
BOOL ok = YES;
if (!array) {
ok = NO ;
}
else if (([array count] ==1) && ([[array objectAtIndex:0] isEqual:@"*"])) {
array = [NSArray arrayOfIntegersStartingWith:0 endingWith:23] ;
}
[self setHours:array] ;
return ok ;
}
- (BOOL)setDaysFromCronField:(NSString*)field {
NSArray* array = [self arrayFromCrontabField:field] ;
NSNumber* o7 = [NSNumber numberWithInt:7] ;
BOOL ok = YES;
if (!array) {
ok = NO ;
}
else if (([array count] ==1) && ([[array objectAtIndex:0] isEqual:@"*"])) {
array = [NSArray arrayOfIntegersStartingWith:0 endingWith:6] ;
}
else if ([array indexOfObject:o7] != NSNotFound) { // If someone used 7 for Sunday
NSMutableArray* mutableArray = [array mutableCopy] ;
[mutableArray removeObject:o7] ; // remove the 7
[mutableArray insertObject:[NSNumber numberWithInt:0] atIndex:0] ; // add in 0 to replace it
array = [[mutableArray copy] autorelease] ;
[mutableArray release] ;
}
[self setDays:array] ;
return ok ;
}
- (BOOL)setDatesFromCronField:(NSString*)field {
NSArray* array = [self arrayFromCrontabField:field] ;
BOOL ok = YES;
if (!array) {
ok = NO ;
}
else if (([array count] ==1) && ([[array objectAtIndex:0] isEqual:@"*"])) {
array = [NSArray arrayOfIntegersStartingWith:0 endingWith:31] ;
}
[self setDates:array] ;
return ok ;
}
- (BOOL)setMonthsFromCronField:(NSString*)field {
NSArray* array = [self arrayFromCrontabField:field] ;
BOOL ok = YES ;
if (!array) {
ok = NO ;
}
else if (([array count] ==1) && ([[array objectAtIndex:0] isEqual:@"*"])) {
array = [NSArray arrayOfIntegersStartingWith:1 endingWith:12] ;
}
[self setMonths:array] ;
return ok ;
}
// Methods for generating crontab numeric fields
// Base Method
- (NSString*)crontabFieldFromArray:(NSArray*)array
{
NSMutableString* field = [[NSMutableString alloc] init] ;
BOOL firstEntry = YES ;
NSEnumerator* e = [array objectEnumerator] ;
NSNumber* number ;
while ((number = [e nextObject])) {
if (!firstEntry)
[field appendString:@","] ;
[field appendString:[number stringValue]] ;
firstEntry = NO ;
}
NSArray* output = [array copy] ;
[array release] ;
return [output autorelease] ;
}
// Specialized Methods
- (NSString*)crontabMinutesField {
return [self crontabFieldFromArray:[self minutes]] ;
}
- (NSString*)crontabHoursField {
return [self crontabFieldFromArray:[self hours]] ;
}
- (NSString*)crontabDaysField {
return [self crontabFieldFromArray:[self days]] ;
}
- (NSString*)crontabDatesField {
return [self crontabFieldFromArray:[self dates]] ;
}
- (NSString*)crontabMonthsField {
return [self crontabFieldFromArray:[self months]] ;
}
- (NSString*)description {
return [NSString stringWithFormat:@"SSCronJob:\n minutes: %@ (%@)\n hours: %@ (%@)\n days: %@ (%@)\n dates: %@ (%@)\n months: %@ (%@)\ncommentOut: %@\n user: %@\n directory: %@\n filename: %@",
[self crontabMinutesField], [self minutes],
[self crontabHoursField], [self hours],
[self crontabDaysField], [self days],
[self crontabDatesField], [self dates],
[self crontabMonthsField], [self months],
[self commentOut] ,
[self user] ,
[self directory],
[self filename] ] ;
}
- (void)appendToFileWhichCrontab:(enum SSWhichCrontab)which ;
// Appends this cron job to the desired crontab
// if which=NSUserCrontab, appends to crontab of current user
{
// Part 1. Generate ourNewLine
NSString* ourNewLine = [NSString stringWithFormat:@"\n%@\t%@\t%@\t%@\t%@",
[self crontabMinutesField],
[self crontabHoursField],
[self crontabDatesField],
[self crontabMonthsField],
[self crontabDaysField] ] ;
if (which == SSSystemCrontab) {
ourNewLine = [ourNewLine stringByAppendingFormat:@"\t%@", [self user]] ;
}
NSString* command = [[self directory] stringByAppendingPathComponent:[self filename]] ;
ourNewLine = [ourNewLine stringByAppendingFormat:@"\t%@", command] ;
NSString* commentOut = [self commentOut] ;
if (commentOut) {
NSString* commentField = [NSString stringWithFormat:@"#%@\t", commentOut] ;
ourNewLine = [commentField stringByAppendingString:ourNewLine] ;
}
// Part 2. Read the existing crontab string from file
NSString* existingCrontabString ;
if (which == SSUserCrontab) {
existingCrontabString = SSReadUserCrontabForCurrentUser() ;
}
else {
existingCrontabString = SSReadSystemCrontab() ;
}
// Part 3. Since we don't know who was there last, clean it up by trimming whitespace
// and newlines from both ends, then add ONE newline at the end.
existingCrontabString = [existingCrontabString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;
existingCrontabString = [existingCrontabString stringByAppendingString:@"\n"] ;
// Part 4. Generate newCrontabData to be written to file
NSString* newCrontabString = [existingCrontabString stringByAppendingString: ourNewLine] ;
NSData* newCrontabData = [newCrontabString dataUsingEncoding:[NSString defaultCStringEncoding]] ;
// Part 5. Write to file
int err ;
if (which == SSUserCrontab) {
NSArray* args = [NSArray arrayWithObjects: @"-", nil] ;
// The argument "-" tells crontab to take data from stdin
NSData* stdErrData ;
err = SSDoShellTask(@"usr/bin/crontab", args, nil, newCrontabData, nil, &stdErrData, YES) ;
if (err) {
NSLog(@"Error %i writing user crontab", err) ;
}
}
else {
if (!SSAuthorizedWriteDataToFile(newCrontabData, @"/etc/crontab")) {
NSLog(@"Error %i writing system crontab", err) ;
}
}
}
+ (NSArray*)readCronJobsFromWhichCrontab:(enum SSWhichCrontab)which user:(NSString*)user {
// if which==SSUserCrontab, will ignore user field and return jobs in current user's crontab
// if which==SSSytemCrontab && user==nil, will return all jobs in system crontab
// The returned value may contain SSCronJobs and/or NSStrings.
// An attempt will be made to parse each line in the crontab
// If a line can be parsed, an SSCronJob will be created and added to the returned array.
// If a line cannot be parsed, the line itself, as an NSString with no trailing \n, will be added to the returned array.
NSString* crontabString ;
if (which == SSUserCrontab) {
if (![user isEqualToString:NSUserName()]) {
NSLog(@"WARNING!! readCronJobsFromWhichFile:SSUserCrontab can only get crontab for current user!") ;
}
crontabString = SSReadUserCrontabForCurrentUser() ;
}
else {
crontabString = SSReadSystemCrontab() ;
}
NSMutableArray* jobs = [[NSMutableArray alloc] init] ;
if (crontabString) {
NSMutableArray* cronLines = [[crontabString componentsSeparatedByString: @"\n"] mutableCopy] ;
NSEnumerator* e = [cronLines objectEnumerator] ;
NSString* cronLine ;
while ((cronLine = [e nextObject])) {
NSException* exception = [NSException
exceptionWithName:@"parse error"
reason:@"can't parse"
userInfo:nil];
SSCronJob* job ;
NS_DURING
// Break up into its tab-delimited fields
cronLine = [cronLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] ;
NSArray* cronJobFields = [cronLine componentsSeparatedByString:@"\t"] ;
SSLog(5, "cronJobFields:\n%@", cronJobFields) ;
// Reasons why I start from the last field instead of the first:
// There may or may not be comment field at the beginning
// Get command and user quicker, needed to see if cronLine qualifies
int i = [cronJobFields count] ;
// First, we make sure it is a "well-formed" line so we don't crash
if ((which == SSUserCrontab) && (i != 6)) {
[exception raise] ;
}
if ((which == SSSystemCrontab) && (i != 7)) {
[exception raise] ;
}
i-- ; // start at last field
NSString* command = [cronJobFields objectAtIndex:i--] ;
NSString* aDirectory = [command stringByDeletingLastPathComponent] ;
// System crontab has a "user" field, but User crontab omits this field
NSString* aUser ;
if (which == SSSystemCrontab) {
aUser = [cronJobFields objectAtIndex:i--] ;
}
else {
aUser = user ;
}
// Create a SSCronJob
job = [[SSCronJob alloc] init ] ;
// set its instance variables
[job setUser:aUser] ;
[job setDirectory:aDirectory] ;
[job setFilename:[command lastPathComponent]] ;
if (![job setDaysFromCronField:[cronJobFields objectAtIndex:i--]]) {
[exception raise] ;
}
if (![job setMonthsFromCronField:[cronJobFields objectAtIndex:i--]]) {
[exception raise] ;
}
if (![job setDatesFromCronField:[cronJobFields objectAtIndex:i--]]) {
[exception raise] ;
}
if (![job setHoursFromCronField:[cronJobFields objectAtIndex:i--]] ) {
[exception raise] ;
}
if (![job setMinutesFromCronField:[cronJobFields objectAtIndex:i--]] ) {
[exception raise] ;
}
if (i >=0) {
// Commenter has politely used a tab to separate comments from minutes field
NSString* commentField = [cronJobFields objectAtIndex:0] ;
commentField = [commentField stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] ;
if ([commentField hasPrefix:@"#"]) {
[job setCommentOut:[commentField substringFromIndex:1]] ;
// substringFromIndex:1 will omit the "#"
}
else {
[exception raise] ;
}
}
else {
// CronniX uses a space instead of a tab between its comment and the minutes field
NSString* firstField = [cronJobFields objectAtIndex:++i] ;
NSArray* commentAndMinutes = [firstField componentsSeparatedByString:@" " ] ;
NSMutableArray* commentFields = [commentAndMinutes mutableCopy] ;
[commentFields removeLastObject] ;
NSMutableString* comments = [[NSMutableString alloc] init] ;
NSEnumerator* f = [commentFields objectEnumerator] ;
NSString* commentField ;
BOOL continuation = NO ;
while ((commentField = [f nextObject])) {
if (continuation) {
[comments appendString:@" "] ;
}
[comments appendString:commentField] ;
continuation = YES ;
}
[commentFields release] ;
NSString* concatenatedCommentOut = [NSString stringWithString:comments] ;
[comments release] ;
if ([concatenatedCommentOut hasPrefix:@"#"]) {
[job setCommentOut:[concatenatedCommentOut substringFromIndex:1]] ;
// substringFromIndex:1 will omit the "#"
}
else {
[exception raise] ;
}
}
// add it to the array
[jobs addObject:job] ;
NS_HANDLER
[jobs addObject:cronLine] ;
NS_ENDHANDLER
[job release] ;
}
[cronLines release] ;
}
SSLog(5, "Found %i cron jobs", [jobs count]) ;
NSArray* output = [jobs copy] ;
[jobs release] ;
return [output autorelease] ;
}
- (id)init {
if ((self = [super init])) {
[self setCommentOut:nil] ; // This field may not be found
}
return self ;
}
- (void)dealloc
{
[self setHours:nil] ;
[self setMinutes:nil] ;
[self setDates:nil] ;
[self setDays:nil] ;
[self setMonths:nil] ;
[self setCommentOut:nil] ;
[self setUser:nil] ;
[self setDirectory:nil] ;
[self setFilename:nil] ;
[super dealloc] ;
}
@end
/*
Not used:
//NSDictionary* myEnviro = [NSDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]] ;
//NSRunInformationalAlertPanel([NSString localize:@"Environment..."], [myEnviro description], [NSString localize:@"ok"], nil, nil) ;
//NSRunInformationalAlertPanel([NSString localize:@"Arguments..."], [[NSArray arrayWithArray:[[NSProcessInfo processInfo] arguments]] description], [NSString localize:@"ok"], nil, nil) ;
//NSString* theCmd ;
//if ([myEnviro objectForKey:@"PWD"])
//{
// theCmd = [NSString stringWithString:[myEnviro objectForKey:@"PWD"]] ;
//}
//else
//{
NSOpenPanel* thePanel = [NSOpenPanel openPanel] ;
[thePanel setTitle:[NSString localize:@"Find the SS Application"]] ;
[thePanel setPrompt:[NSString localize:@"Bingo"]] ;
[thePanel setRequiredFileType:nil] ;
if ([thePanel respondsToSelector:@selector(setMessage:)]) // Jaguar does not
{
[thePanel setMessage:[NSString localize:@"Find and select the SS Application, then click \"Bingo\""]] ;
}
if ([thePanel respondsToSelector:@selector(setCanCreateDirectories:)]) // Jaguar does not
{
[thePanel setCanCreateDirectories:NO ] ;
}
NSArray* appType = [NSArray arrayWithObject:[NSString stringWithString:@"app"]] ;
int reply = [thePanel runModalForDirectory:@"/Applications" file:@"SS" types:appType] ; // the file:@"SS" part does not work. Bug in MacOS?
if (reply == NSCancelButton)
aOK = NO ;
else if (reply == NSOKButton)
{
gAppLaunchPath = [NSMutableString stringWithString:[thePanel filename]] ;
}
*/