This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.m
697 lines (542 loc) · 27.1 KB
/
main.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//
// main.m
// KeychainEditor
//
// Created by Nitin Jami on 3/26/15.
// Copyright (c) 2015 NCC Group. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Security/Security.h>
/*
Undocumented functions that are used by the keychain. I need to declare these functions
in order to use in my tool. Not all three functions are useful now except for
SecAccessControlGetConstraints() which returns a Dict containing a value for
UserPresence. I will leave the other two functions for future use.
https://opensource.apple.com/source/Security/Security-57031.1.35/Security/sec/Security/SecItem.c
*/
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000
CFDictionaryRef SecAccessControlGetConstraints(SecAccessControlRef access_control);
CFTypeRef SecAccessControlGetProtection(SecAccessControlRef access_control);
CFDataRef SecAccessControlCopyData(SecAccessControlRef access_control);
#endif
/*
The flag will help in determining, if idb has called or it was command line invocation.
The primary difference for idb version is kSecValueData needs to be in base64, whether
it is editing an item or dumping the keychain. For a command line invocation, for the
sake of user expierence I would go with strings.
TODO: I haven't implemented this yet.
*/
unsigned int IF_IDB = 0;
/*
Function that prints out usage information when a --help command
is passed as the first argument of cmd line.
*/
void printUsage() {
fprintf(stdout, "\n\e[4;30mUsage:\e[0;31m ./keychaineditor commands \
\n\e[4;30mCommands can be:\e[0;31m\
\n\t--help: Prints the usage.\
\n\t--action: Can be either min-dump, dump, edit, delete.\
\n\t--find: uses 'CONTAINS' to find strings from the dump.\
\n\t--account: Account name for the keychain item you want to edit/delete.\
\n\t--service: Service name for the keychain item you want to edit/delete.\
\n\t--agroup: Optional. Access group for the keychain item you want to edit/delete. \
\n\t--data: Base64 encoded data that is used to update the keychain item.\
\n\e[0;32mAccount and service is used to uniquely identify a keychain item.\
\n\e[0;32mNote: If there is no account name, pass a '' string.\
\n\e[0;32mNote: --find is an optional command for dump. It search from\
\n\e[0;32m{Account, Service, EntitlementGroup, Protection}.\
\n\e[4;30mExamples:\e[0;31m\
\n\t./keychaineditor --action dump --find XXX\
\n\tOr\
\n\t./keychaineditor --action delete --account XXX --service XXX\
\n\tOr\
\n\t./keychaineditor --action edit --account XXX --service XXX --data XXX\
\e[0;30m\n\n");
return;
}
/*
Function to convert OSStatus to human readable error messages.
I need to do this because iOS does not support SecCopyErrorMessageString()
TODO: I am only converting codes that I think are apt for my tool, I will
update as and when required, if I encounter anything new.
*/
OSStatus osstatusToHumanReadableString(OSStatus status) {
switch (status) {
case errSecSuccess:
fprintf(stdout, "\e[0;32mOperation successfully completed.\e[0;30m\n");
break;
case errSecItemNotFound:
fprintf(stderr, "\e[0;31mThe specified item could not be found in the keychain.\e[0;30m\n");
break;
case errSecAuthFailed:
fprintf(stderr, "\e[0;31mDid you turn off the passcode on device? The item is no longer available.\e[0;30m\n");
fprintf(stderr, "\e[0;31mIf that is not the case, UserPresence is required. Check your device for the prompt.\e[0;30m\n");
break;
case errSecInteractionNotAllowed:
fprintf(stderr, "\e[0;31mDevice Locked. Cannot dump WhenUnlocked or WhenPasscodeSet items.\e[0;30m\n");
break;
case -34018:
fprintf(stderr, "\e[0;31mError: Client has neither application-identifier nor keychain-access-groups entitlements. Please refer README for further instructions.\e[0;30m\n");
break;
default:
fprintf(stderr, "\e[0;31mUnhandled Error: Please contact developer to report this error. Error code: %d\e[0;30m\n", (int)status);
break;
}
return status;
}
/*
Helper function that converts pdmn values to Accessible constants
and stringify the constant.
*/
NSString *mapKeychainConstants(NSString *pdmn) {
if ([pdmn isEqualToString:@"ak"])
return @"kSecAttrAccessibleWhenUnlocked";
else if ([pdmn isEqualToString:@"ck"])
return @"kSecAttrAccessibleAfterFirstUnlock";
else if ([pdmn isEqualToString:@"dk"])
return @"kSecAttrAccessibleAlways";
else if ([pdmn isEqualToString:@"aku"])
return @"kSecAttrAccessibleWhenUnlockedThisDeviceOnly";
else if ([pdmn isEqualToString:@"cku"])
return @"kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly";
else if ([pdmn isEqualToString:@"dku"])
return @"kSecAttrAccessibleAlwaysThisDeviceOnly";
else if ([pdmn isEqualToString:@"akpu"])
return @"kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly";
else
return @"Unexpected Error";
}
/*
Helper function for identifying the type of object both Account
and Service return. Developers often just write as NSData or
they write as NSString.
*/
NSString* determineTypeAndReturnNSString(id accountValue) {
// If NSData, convert it to NSString and return...
if ([accountValue isKindOfClass:[NSData class]]) {
return [[NSString alloc] initWithData:accountValue encoding:NSUTF8StringEncoding];
}
return accountValue;
}
/*
Helper function to check if the ACL for the keychain item requires
a UserPresence or not.
A UserPresence further requires a form of user authentication, i.e. either
via TouchID or Device Passcode.
TODO: I will develop this as and when Apple updates their functionality. Right
now it's just whether a UserPresence is required or not.
*/
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000
NSString* checkUserPresence(SecAccessControlRef sacObj) {
/*
SecAccessControlGetConstraints() returns a dictionary with values
for UserPresence. I am not going to specifically check for that, since
Apple's implementation of UserPresence is either you have it or
not, in which case it returns a null.
SecAccessControlGetConstraints() is not exactly a documented API. But,
since Apple Open Sourced part of thier keychain implementation, I learnt
of such methods and used here.
https://opensource.apple.com/source/Security/Security-57031.1.35/Security/sec/Security/SecItem.c
TODO: If the kSecAccessControl is modified later then check for exact
values.
*/
if (SecAccessControlGetConstraints(sacObj)) {
return @"Yes";
}
return @"No";
}
#endif
/*
Helper function to deal with items having no data. The return value for this case is NULL.
And NULL doesn't go well with JSON/Dictionary. https://github.com/NitinJami/keychaineditor/issues/1
Just return a "null" string for no data and base64 encode it for idb to parse it properly.
*/
NSString* checkForNoDataValue(NSData* dataValue) {
if (dataValue != NULL) {
return [dataValue base64EncodedStringWithOptions:0];
}
return [[@"null" dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
}
/*
Helper function to limit the output according to the search query
from the --find cmd line argument. The search operation will perform
on Account array, Service array, Entitlement group array and Protection classes.
*/
NSMutableDictionary* limitOutputFromSearchQuery(NSMutableDictionary *dumpedDict, NSString *query) {
NSMutableDictionary *limitedDict = [[NSMutableDictionary alloc] init];
NSInteger index = 0;
for (NSDictionary *topLevelIterator in [dumpedDict allValues]) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Account CONTAINS[cd] %@ OR \
Service CONTAINS[cd] %@ OR EntitlementGroup CONTAINS[cd] %@ OR \
Protection CONTAINS[cd] %@", query, query, query, query];
if ([predicate evaluateWithObject:topLevelIterator]) {
[limitedDict setObject:topLevelIterator forKey:[NSString stringWithFormat:@"%lu", (unsigned long)++index]];
}
}
return limitedDict;
}
/*
Helper function that prepares JSON output of the keychain dump from
a dictionary of results.
*/
void prepareJsonOutput(NSArray *results, NSString *find) {
/*
The output will be in JSON style for other applications to easily parse it.
The representation would be something like this:
{
index : {
Account :
Service :
Entitlement Group :
Creation Time :
Modified Time :
Protection :
Data :
},
...
}
TODO: After I get iphone 6, I should look into kSecAttrAccessControl.
*/
NSMutableDictionary *parentJSON = [[NSMutableDictionary alloc] init];
NSDictionary *eachItemFromResults = [[NSDictionary alloc] init];
NSInteger index = 0;
/*
When this loop is finished. It will have the above JSON structure that
I explained in the form of dictionary within dictionary. After this, I
just need to JSONify it.
*/
for (eachItemFromResults in results) {
NSMutableDictionary *innerJSON = [[NSMutableDictionary alloc] init];
/*
I have encountered Account as NSData type, but mostly are NSString. I am
checking for thier type and then displaying the result as a string.
In an abudance of caution, I am checking for service as well.
*/
[innerJSON setObject:determineTypeAndReturnNSString([eachItemFromResults \
objectForKey:(__bridge id)kSecAttrService]) forKey:@"Service"];
[innerJSON setObject:determineTypeAndReturnNSString([eachItemFromResults \
objectForKey:(__bridge id)kSecAttrAccount]) forKey:@"Account"];
[innerJSON setObject:[NSString stringWithFormat:@"%@", \
[eachItemFromResults objectForKey:(__bridge id)kSecAttrAccessGroup]] \
forKey:@"EntitlementGroup"];
[innerJSON setObject:mapKeychainConstants([eachItemFromResults \
objectForKey:(__bridge id)kSecAttrAccessible]) forKey:@"Protection"];
[innerJSON setObject:[NSString stringWithFormat:@"%@", \
[eachItemFromResults objectForKey:(__bridge id)kSecAttrModificationDate]] \
forKey:@"Modified Time"];
[innerJSON setObject:[NSString stringWithFormat:@"%@", \
[eachItemFromResults objectForKey:(__bridge id)kSecAttrCreationDate]] \
forKey:@"Creation Time"];
[innerJSON setObject:checkForNoDataValue([eachItemFromResults objectForKey:(__bridge id)kSecValueData]) forKey:@"Data"];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000
[innerJSON setObject:checkUserPresence((__bridge SecAccessControlRef) \
([eachItemFromResults objectForKey:(__bridge id)(kSecAttrAccessControl)])) forKey:@"UserPresence"];
#else
[innerJSON setObject:@"NA" forKey:@"UserPresence"];
#endif
[parentJSON setObject:innerJSON forKey:[NSString stringWithFormat:@"%lu", (unsigned long)++index]];
}
/*
Should we limit the output. Check If --find has any value and perform
appropriate search from the parentJSON dictionary.
*/
if (find != nil) {
parentJSON = limitOutputFromSearchQuery(parentJSON, find);
}
/*
Okay the dictionary is set. Let's convert it to JSON object.
*/
NSError* error = nil;
NSData* json = nil;
json = [NSJSONSerialization dataWithJSONObject:parentJSON \
options:NSJSONWritingPrettyPrinted error:&error];
/*
If I have the JSON data, let's print it.
TODO: Maybe send the error code back to the callee, so that the callee would know there is an error
with JSON. It's just good practice, do it.
*/
if (json != nil && error == nil) {
fprintf(stdout, "%s\n", [[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding] UTF8String]);
}
else {
fprintf(stderr, "\n\e[0;31mInternal error when preparing JSON output. Error: %s\e[0;30m\n", [[error localizedDescription] UTF8String]);
}
}
/*
Helper function to just print the minimal dump.
This would be Account, Service and Entitlement Group.
*/
void prepareMinimumOutput(NSArray *results) {
NSDictionary *eachItemFromResults = [[NSDictionary alloc] init];
NSString *account = [[NSString alloc] init];
NSString *service = [[NSString alloc] init];
fprintf(stdout, "\e[0;31mWarning: The names are truncated to max width of 35 charaters. Please use this dump as a reference, and use --find to get full details.\n\e[0;30m");
// Prettify the output.
fprintf(stdout, "Account%28s | Service\n", "");
fprintf(stdout, "%s | %s\n", [[@"" stringByPaddingToLength:35 withString:@"-" startingAtIndex:0] UTF8String], \
[[@"" stringByPaddingToLength:35 withString:@"-" startingAtIndex:0] UTF8String]);
// This is simple iterate through the results and print out Account and Service.
for (eachItemFromResults in results) {
service = determineTypeAndReturnNSString([eachItemFromResults objectForKey:(__bridge id)kSecAttrService]);
account = determineTypeAndReturnNSString([eachItemFromResults objectForKey:(__bridge id)kSecAttrAccount]);
/*
%*.* --> allows to specify the width and precision for a string.
width = -35 --> tells the field width is 35, if the string is smaller than 35 in
length, trailling spaces are added. Hence, the negative 35.
precision = 35 --> restricts the string to be printed to only 35 characters in length.
*/
fprintf(stdout, "%*.*s %*.*s\n", -35, 35, [account UTF8String], -35, 35, [service UTF8String]);
}
}
/*
Function that dumps all the keychain items.
TODO: Do I want to get rid of the standard apple entries.
*/
OSStatus dumpKeychain(NSString *action, NSString *find) {
NSMutableDictionary* query = [[NSMutableDictionary alloc] init];
NSMutableArray* finalResult = [[NSMutableArray alloc] init];
NSString *eachConstant = [[NSString alloc] init];
OSStatus status = 0;
//NSArray *secClasses = @[(__bridge NSString *)kSecClassGenericPassword, (__bridge NSString *)kSecClassKey];
NSArray *constants = @[(__bridge NSString *)kSecAttrAccessibleAfterFirstUnlock,
(__bridge NSString *)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
(__bridge NSString *)kSecAttrAccessibleAlways,
(__bridge NSString *)kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
(__bridge NSString *)kSecAttrAccessibleAlwaysThisDeviceOnly,
(__bridge NSString *)kSecAttrAccessibleWhenUnlocked,
(__bridge NSString *)kSecAttrAccessibleWhenUnlockedThisDeviceOnly];
/*
Prepare a query to dump all the items.
Class = Generic Password.
kSecMatchLimit = kSecMatchLimitAll, will actually dump all the items.
kSecReturnAttributes = True, will return the Attributes associated with the item.
kSecReturnData = True, will return the data associated with the item.
TODO: I am just dealing with genp class of items, if a requirment occurs
to other items like inetp, certs, I will extend it.
*/
for (eachConstant in constants) {
[query setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
[query setObject:eachConstant forKey:(__bridge id<NSCopying>)(kSecAttrAccessible)];
[query setObject:(__bridge id)kSecMatchLimitAll forKey:(__bridge id)kSecMatchLimit];
[query setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnAttributes];
[query setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
/*
On success, a dictionary of all the items are returned.
*/
CFTypeRef results = nil;
status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &results);
if (status == errSecSuccess) {
[finalResult addObjectsFromArray: (__bridge NSArray *)results];
}
}
/*
Handling the return status would be bit diffrent from other actions.
On Success, I should print out the items instead of "Operation completed" message.
On Failure, regular call to osstatusToHumanReadableString() is called.
*/
if ([finalResult count] != 0) {
if ([action isEqualToString:@"min-dump"]) {
prepareMinimumOutput(finalResult);
}
else {
prepareJsonOutput(finalResult, find);
}
}
else {
status = osstatusToHumanReadableString(status);
}
return status;
}
/*
Function that queries an item in keychain and updates the item value.
Account and service are used to uniquely identify an item.
TODO: To identify an item uniquely {Account, Service} tuple is more
than enough on most cases. There may be a case occasionaly where there
might be two entries with the same {Accoubt, Service} tuple. This happens
espcially when dealing with enterprise app store. So, in this case
{Account, Service, AccessGroup} 3-tuple is necessary to uniquely identify
an item. For the sake of user experience, I am going with an optional --agroup
command. So, users do not have to pass --agroup everytime.
If a need arises in furture, that agroup be mandatory. I will change it.
*/
OSStatus editKeychainItem(NSString* account, NSString* service, NSString* agroup, NSData* data) {
NSMutableDictionary* query = [[NSMutableDictionary alloc] init];
/*
Prepare a query for SecItemUpdate().
Class = Generic Password.
Account = account name provided by the user from cmd line.
Service = service name provided by the user from cmd line.
AccessGroup = Optional. access group name by the user from cmd line.
TODO: I am just dealing with genp class of items, if a requirment occurs
to other items like inetp, certs, I will extend it.
*/
[query setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
[query setObject:account forKey:(__bridge id)kSecAttrAccount];
[query setObject:service forKey:(__bridge id)kSecAttrService];
if (agroup != nil) {
[query setObject:agroup forKey:(__bridge id<NSCopying>)(kSecAttrAccessGroup)];
}
/*
Prepare the second parameter for SecItemUpdate().
kSecValueData = data provided by the user from cmd line.
*/
NSDictionary* updateValue = [NSDictionary dictionaryWithObjectsAndKeys: \
(id)data, (__bridge id)kSecValueData, nil];
OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)query, \
(__bridge CFDictionaryRef)updateValue);
/*
Handle the return status.
*/
status = osstatusToHumanReadableString(status);
return status;
}
/*
Function that queries an item in keychain and deletes the item.
Account and service are used to uniquely identify an item.
TODO: To identify an item uniquely {Account, Service} tuple is more
than enough on most cases. There may be a case occasionaly where there
might be two entries with the same {Accoubt, Service} tuple. This happens
espcially when dealing with enterprise app store. So, in this case
{Account, Service, AccessGroup} 3-tuple is necessary to uniquely identify
an item. For the sake of user experience, I am going with an optional --agroup
command. So, users do not have to pass --agroup everytime.
If a need arises in furture, that agroup be mandatory. I will change it.
*/
OSStatus deleteKeychainItem(NSString* account, NSString* service, NSString* agroup) {
NSMutableDictionary* query = [[NSMutableDictionary alloc] init];
/*
Prepare a query for SecItemDelete().
Class = Generic Password.
Account = account name provided by the user from cmd line.
Service = service name provided by the user from cmd line.
AccessGroup = Optional. access group name by the user from cmd line.
TODO: I am just dealing with genp class of items, if a requirment occurs
to other items like inetp, certs, I will extend it.
*/
[query setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
[query setObject:account forKey:(__bridge id)kSecAttrAccount];
[query setObject:service forKey:(__bridge id)kSecAttrService];
if (agroup != nil) {
[query setObject:agroup forKey:(__bridge id<NSCopying>)(kSecAttrAccessGroup)];
}
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
/*
Handle the return status.
*/
status = osstatusToHumanReadableString(status);
return status;
}
// FOR TESTING PURPOSES.
/*
void additem() {
NSMutableDictionary *attrbs = [[NSMutableDictionary alloc] init];
[attrbs setObject:(__bridge id)(kSecClassGenericPassword) forKey:(__bridge id<NSCopying>)(kSecClass)];
[attrbs setObject:@"testaccount" forKey:(__bridge id<NSCopying>)(kSecAttrAccount)];
[attrbs setObject:@"testservice" forKey:(__bridge id<NSCopying>)(kSecAttrService)];
[attrbs setObject:(__bridge id)(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly) forKey:(__bridge id<NSCopying>)(kSecAttrAccessible)];
//[attrbs setObject:[@"testing" dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id<NSCopying>)(kSecValueData)];
//SecAccessControlRef sac = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, kSecAccessControlUserPresence, nil);
//[attrbs setObject:(__bridge id)(sac) forKey:(__bridge id<NSCopying>)(kSecAttrAccessControl)];
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)(attrbs), nil);
status = osstatusToHumanReadableString(status);
return;
}*/
int main(int argc, char *argv[]) {
@autoreleasepool {
/*
Let's check what we have for cmd line arguments.
*/
if (argc < 2) {
printUsage();
return EXIT_FAILURE;
}
if ([[NSString stringWithFormat:@"%s", argv[1]] isEqualToString:@"--help"]) {
printUsage();
return EXIT_SUCCESS;
}
/*
The first argument should either be --help or --action. I have already taken
care for --help; will deal with --action here.
*/
if (![[NSString stringWithFormat:@"%s", argv[1]] isEqualToString:@"--action"]) {
fprintf(stderr, "\e[0;31mInvalid command passed as first argument. Please use --help to get usage information.\e[0;30m");
return EXIT_FAILURE;
}
/*
Use the NSUserDefaults to process the cmd line args instead
of manually dealing with argv. Note that NSUserDefaults adds a '-' before
the string we specified. So, '-action' becomes '--action'. Also, I don't
need to deal with the order in which commands are passed, expect anyways
'--action' is the first command.
This also allows me to only process commands I want for. Garbage commands
will not enter into my program. YOU HACKERS.
*/
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
NSString *action = [args stringForKey:@"-action"];
NSString *acct = [args stringForKey:@"-account"];
NSString *srvc = [args stringForKey:@"-service"];
NSString *data = [args stringForKey:@"-data"];
NSString *find = [args stringForKey:@"-find"];
NSString *agroup = [args stringForKey:@"-agroup"];
// FOR TESTING PURPOSES.
if ([action isEqualToString:@"test"]) {
//additem();
NSLog(@"%d", __IPHONE_OS_VERSION_MIN_REQUIRED);
return EXIT_SUCCESS;
}
/*
'--action' can only be min-dump, dump, edit or delete.
*/
if (![@[@"min-dump", @"dump", @"edit", @"delete"] containsObject:action]) {
fprintf(stderr, "\e[0;31mInvalid action passed. Please use --help to get usage information.\n\e[0;30m");
return EXIT_FAILURE;
}
/*
At this point I should have valid action command, let's call appropriate functions.
--find is used to narrow down the keychain dump.
*/
if ([action isEqualToString:@"dump"] || [action isEqualToString:@"min-dump"]) {
if (errSecSuccess != dumpKeychain(action, find)) {
return EXIT_FAILURE;
}
}
else if ([action isEqualToString:@"edit"]) {
/*
Edit should have account, service and data commands.
*/
if ( (acct == nil) || (srvc == nil) || (data == nil) ) {
fprintf(stderr, "\e[0;31mEdit requires account, service and data. Please use --help to get usage information.\n\e[0;30m");
return EXIT_FAILURE;
}
/*
Decode the base64 encoded data that I accept. I only accept base64 because
no information will be lost when the data is in xml format, or some other format other than
a string.
TODO: I know this is painful when dealing with simple strings. I will deal with
this when I have better understanding on how can differentiate base64 data with
simple ascii string.
*/
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
if (decodedData == nil) {
fprintf(stderr, "\e[0;31mData only accepts base64 strings. Please use --help to get usage information.\n\e[0;30m");
return EXIT_FAILURE;
}
if (errSecSuccess != editKeychainItem(acct, srvc, agroup, decodedData)) {
return EXIT_FAILURE;
}
}
else if ([action isEqualToString:@"delete"]) {
/*
Delete should have account and service.
*/
if ( (acct == nil) || (srvc == nil) ) {
fprintf(stderr, "\e[0;31mDelete requires account, service. Please use --help to get usage information.\n\e[0;30m");
return EXIT_FAILURE;
}
if (errSecSuccess != deleteKeychainItem(acct, srvc, agroup)) {
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}