]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/Tool/syncbackup.m
Security-59306.80.4.tar.gz
[apple/security.git] / keychain / SecureObjectSync / Tool / syncbackup.m
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 "keychain/SecureObjectSync/SOSBackupInformation.h"
42 #include "keychain/SecureObjectSync/SOSRecoveryKeyBag.h"
43 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
44
45 #include <utilities/SecCFWrappers.h>
46
47 #include "SecurityTool/sharedTool/readline.h"
48 #include "secToolFileIO.h"
49
50
51 static bool dumpBackupInfo(CFErrorRef *error) {
52 CFReleaseNull(*error);
53 bool isLast = SOSCCIsThisDeviceLastBackup(error);
54
55 printmsg(CFSTR("This %s the last backup peer.\n"), (isLast) ? "is": "isn't");
56 return *error != NULL;
57 }
58
59 static bool longListing(CFErrorRef *error) {
60 CFDataRef rkbgder = NULL;
61 CFDictionaryRef bskbders = NULL;
62
63 CFDictionaryRef backupInfo = SOSCCCopyBackupInformation(error);
64 SOSRecoveryKeyBagRef rkbg = NULL;
65 CFNumberRef status = CFDictionaryGetValue(backupInfo, kSOSBkpInfoStatus);
66 int infoStatus;
67 CFNumberGetValue(status, kCFNumberIntType, &infoStatus);
68
69 switch(infoStatus) {
70 case noError:
71 rkbgder = CFDictionaryGetValue(backupInfo, kSOSBkpInfoRKBG);
72 bskbders = CFDictionaryGetValue(backupInfo, kSOSBkpInfoBSKB);
73 break;
74 case noTxnorAcct:
75 break;
76 case noAlloc:
77 break;
78 case noTrustedPubKey:
79 break;
80 case noBSKBs:
81 rkbgder = CFDictionaryGetValue(backupInfo, kSOSBkpInfoRKBG);
82 break;
83 default:
84 break;
85 }
86
87 if(rkbgder) {
88 rkbg = SOSRecoveryKeyBagCreateFromData(kCFAllocatorDefault, rkbgder, NULL);
89 printmsg(CFSTR("Recovery Keybag: %@\n"), rkbg);
90 }
91
92 if(bskbders) {
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);
98 if(bskb) {
99 bool reckeyPresent = (rkPub && SOSBKSBPrefixedKeyIsInKeyBag(bskb, bskbRkbgPrefix, rkPub));
100 printmsg(CFSTR("BackupSliceKeybag %@: Recovery Key %s; %@\n"), key, (reckeyPresent) ? "Present": "Absent ", bskb);
101 CFReleaseNull(bskb);
102 }
103 });
104 }
105 CFReleaseNull(backupInfo);
106 CFReleaseNull(rkbg);
107 return *error != NULL;
108 }
109
110
111
112 int
113 syncbackup(int argc, char * const *argv)
114 {
115 /*
116 "Circle Backup Information"
117 " -i info (current status)"
118
119 */
120 SOSLogSetOutputTo(NULL, NULL);
121
122 int ch, result = 0;
123 CFErrorRef error = NULL;
124 bool hadError = false;
125
126 while ((ch = getopt(argc, argv, "il")) != -1)
127 switch (ch) {
128
129 case 'i':
130 hadError = dumpBackupInfo(&error);
131 break;
132
133 case 'l':
134 hadError = longListing(&error);
135 break;
136
137 case '?':
138 default:
139 return SHOW_USAGE_MESSAGE;
140 }
141
142 if (hadError)
143 printerr(CFSTR("Error: %@\n"), error);
144
145 return result;
146 }