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>
43 #include <utilities/SecCFWrappers.h>
45 #include <SecurityTool/readline.h>
46 #include "secToolFileIO.h"
49 static bool dumpBackupInfo(CFErrorRef
*error
) {
50 CFReleaseNull(*error
);
51 bool isLast
= SOSCCIsThisDeviceLastBackup(error
);
53 printmsg(CFSTR("This %s the last backup peer.\n"), (isLast
) ? "is": "isn't");
54 return *error
!= NULL
;
57 static bool longListing(CFErrorRef
*error
) {
58 CFDataRef rkbgder
= NULL
;
59 CFDictionaryRef bskbders
= NULL
;
61 CFDictionaryRef backupInfo
= SOSCCCopyBackupInformation(error
);
62 SOSRecoveryKeyBagRef rkbg
= NULL
;
63 CFNumberRef status
= CFDictionaryGetValue(backupInfo
, kSOSBkpInfoStatus
);
65 CFNumberGetValue(status
, kCFNumberIntType
, &infoStatus
);
69 rkbgder
= CFDictionaryGetValue(backupInfo
, kSOSBkpInfoRKBG
);
70 bskbders
= CFDictionaryGetValue(backupInfo
, kSOSBkpInfoBSKB
);
79 rkbgder
= CFDictionaryGetValue(backupInfo
, kSOSBkpInfoRKBG
);
86 rkbg
= SOSRecoveryKeyBagCreateFromData(kCFAllocatorDefault
, rkbgder
, NULL
);
87 printmsg(CFSTR("Recovery Keybag: %@\n"), rkbg
);
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
);
97 bool reckeyPresent
= (rkPub
&& SOSBKSBPrefixedKeyIsInKeyBag(bskb
, bskbRkbgPrefix
, rkPub
));
98 printmsg(CFSTR("BackupSliceKeybag %@: Recovery Key %s; %@\n"), key
, (reckeyPresent
) ? "Present": "Absent ", bskb
);
103 CFReleaseNull(backupInfo
);
105 return *error
!= NULL
;
111 syncbackup(int argc
, char * const *argv
)
114 "Circle Backup Information"
115 " -i info (current status)"
118 SOSLogSetOutputTo(NULL
, NULL
);
121 CFErrorRef error
= NULL
;
122 bool hadError
= false;
124 while ((ch
= getopt(argc
, argv
, "il")) != -1)
128 hadError
= dumpBackupInfo(&error
);
132 hadError
= longListing(&error
);
137 return 2; /* Return 2 triggers usage message. */
141 printerr(CFSTR("Error: %@\n"), error
);