3 * Copyright (c) 2003-2007,2009-2010,2013-2016 Apple Inc. All Rights Reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
33 #include "syncbackup.h"
37 #include <CoreFoundation/CoreFoundation.h>
39 #include <Security/SecureObjectSync/SOSCloudCircle.h>
40 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
41 #include <Security/SecureObjectSync/SOSBackupInformation.h>
42 #include <Security/SecureObjectSync/SOSRecoveryKeyBag.h>
43 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
45 #include <utilities/SecCFWrappers.h>
47 #include <SecurityTool/readline.h>
48 #include "secToolFileIO.h"
51 static bool dumpBackupInfo(CFErrorRef *error) {
52 CFReleaseNull(*error);
53 bool isLast = SOSCCIsThisDeviceLastBackup(error);
55 printmsg(CFSTR("This %s the last backup peer.\n"), (isLast) ? "is": "isn't");
56 return *error != NULL;
59 static bool longListing(CFErrorRef *error) {
60 CFDataRef rkbgder = NULL;
61 CFDictionaryRef bskbders = NULL;
63 CFDictionaryRef backupInfo = SOSCCCopyBackupInformation(error);
64 SOSRecoveryKeyBagRef rkbg = NULL;
65 CFNumberRef status = CFDictionaryGetValue(backupInfo, kSOSBkpInfoStatus);
67 CFNumberGetValue(status, kCFNumberIntType, &infoStatus);
71 rkbgder = CFDictionaryGetValue(backupInfo, kSOSBkpInfoRKBG);
72 bskbders = CFDictionaryGetValue(backupInfo, kSOSBkpInfoBSKB);
81 rkbgder = CFDictionaryGetValue(backupInfo, kSOSBkpInfoRKBG);
88 rkbg = SOSRecoveryKeyBagCreateFromData(kCFAllocatorDefault, rkbgder, NULL);
89 printmsg(CFSTR("Recovery Keybag: %@\n"), rkbg);
93 CFDataRef rkPub = NULL;
94 if(rkbg) rkPub = SOSRecoveryKeyBagGetKeyData(rkbg, NULL);
95 CFDictionaryForEach(bskbders, ^(const void *key, const void *value) {
96 CFDataRef bskbder = asData(value, NULL);
97 SOSBackupSliceKeyBagRef bskb = SOSBackupSliceKeyBagCreateFromData(kCFAllocatorDefault, bskbder, NULL);
99 bool reckeyPresent = (rkPub && SOSBKSBPrefixedKeyIsInKeyBag(bskb, bskbRkbgPrefix, rkPub));
100 printmsg(CFSTR("BackupSliceKeybag %@: Recovery Key %s; %@\n"), key, (reckeyPresent) ? "Present": "Absent ", bskb);
105 CFReleaseNull(backupInfo);
107 return *error != NULL;
113 syncbackup(int argc, char * const *argv)
116 "Circle Backup Information"
117 " -i info (current status)"
120 SOSLogSetOutputTo(NULL, NULL);
123 CFErrorRef error = NULL;
124 bool hadError = false;
126 while ((ch = getopt(argc, argv, "il")) != -1)
130 hadError = dumpBackupInfo(&error);
134 hadError = longListing(&error);
139 return SHOW_USAGE_MESSAGE;
143 printerr(CFSTR("Error: %@\n"), error);