]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Tool/syncbackup.c
acb04d92b1e32429a9a50212d3bfd25d1d5b0662
[apple/security.git] / OSX / sec / SOSCircle / Tool / syncbackup.c
1
2 /*
3 * Copyright (c) 2003-2007,2009-2010,2013-2016 Apple Inc. All Rights Reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 *
24 */
25
26 //
27 // syncbackup.c
28 // sec
29 //
30 //
31 //
32
33 #include "syncbackup.h"
34
35
36 #include <stdio.h>
37 #include <CoreFoundation/CoreFoundation.h>
38
39 #include <Security/SecureObjectSync/SOSCloudCircle.h>
40 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
41 #include <Security/SecureObjectSync/SOSBackupInformation.h>
42
43 #include <utilities/SecCFWrappers.h>
44
45 #include <SecurityTool/readline.h>
46 #include "secToolFileIO.h"
47
48
49 static bool dumpBackupInfo(CFErrorRef *error) {
50 CFReleaseNull(*error);
51 bool isLast = SOSCCIsThisDeviceLastBackup(error);
52
53 printmsg(CFSTR("This %s the last backup peer.\n"), (isLast) ? "is": "isn't");
54 return *error != NULL;
55 }
56
57 static bool longListing(CFErrorRef *error) {
58 CFDataRef rkbgder = NULL;
59 CFDictionaryRef bskbders = NULL;
60
61 CFDictionaryRef backupInfo = SOSCCCopyBackupInformation(error);
62 SOSRecoveryKeyBagRef rkbg = NULL;
63 CFNumberRef status = CFDictionaryGetValue(backupInfo, kSOSBkpInfoStatus);
64 int infoStatus;
65 CFNumberGetValue(status, kCFNumberIntType, &infoStatus);
66
67 switch(infoStatus) {
68 case noError:
69 rkbgder = CFDictionaryGetValue(backupInfo, kSOSBkpInfoRKBG);
70 bskbders = CFDictionaryGetValue(backupInfo, kSOSBkpInfoBSKB);
71 break;
72 case noTxnorAcct:
73 break;
74 case noAlloc:
75 break;
76 case noTrustedPubKey:
77 break;
78 case noBSKBs:
79 rkbgder = CFDictionaryGetValue(backupInfo, kSOSBkpInfoRKBG);
80 break;
81 default:
82 break;
83 }
84
85 if(rkbgder) {
86 rkbg = SOSRecoveryKeyBagCreateFromData(kCFAllocatorDefault, rkbgder, NULL);
87 printmsg(CFSTR("Recovery Keybag: %@\n"), rkbg);
88 }
89
90 if(bskbders) {
91 CFDataRef rkPub = NULL;
92 if(rkbg) rkPub = SOSRecoveryKeyBagGetKeyData(rkbg, NULL);
93 CFDictionaryForEach(bskbders, ^(const void *key, const void *value) {
94 CFDataRef bskbder = asData(value, NULL);
95 SOSBackupSliceKeyBagRef bskb = SOSBackupSliceKeyBagCreateFromData(kCFAllocatorDefault, bskbder, NULL);
96 if(bskb) {
97 bool reckeyPresent = (rkPub && SOSBKSBPrefixedKeyIsInKeyBag(bskb, bskbRkbgPrefix, rkPub));
98 printmsg(CFSTR("BackupSliceKeybag %@: Recovery Key %s; %@\n"), key, (reckeyPresent) ? "Present": "Absent ", bskb);
99 CFReleaseNull(bskb);
100 }
101 });
102 }
103 CFReleaseNull(backupInfo);
104 CFReleaseNull(rkbg);
105 return *error != NULL;
106 }
107
108
109
110 int
111 syncbackup(int argc, char * const *argv)
112 {
113 /*
114 "Circle Backup Information"
115 " -i info (current status)"
116
117 */
118 SOSLogSetOutputTo(NULL, NULL);
119
120 int ch, result = 0;
121 CFErrorRef error = NULL;
122 bool hadError = false;
123
124 while ((ch = getopt(argc, argv, "il")) != -1)
125 switch (ch) {
126
127 case 'i':
128 hadError = dumpBackupInfo(&error);
129 break;
130
131 case 'l':
132 hadError = longListing(&error);
133 break;
134
135 case '?':
136 default:
137 return 2; /* Return 2 triggers usage message. */
138 }
139
140 if (hadError)
141 printerr(CFSTR("Error: %@\n"), error);
142
143 return result;
144 }