]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Tool/keychain_log.m
65b4419be285ce9784c04f6e979431cf72619a9f
[apple/security.git] / OSX / sec / SOSCircle / Tool / keychain_log.m
1 //
2 // keychain_log.c
3 // sec
4 //
5 // Created by Richard Murphy on 1/26/16.
6 //
7 //
8
9 #include "keychain_log.h"
10
11 /*
12 * Copyright (c) 2003-2007,2009-2010,2013-2014 Apple Inc. All Rights Reserved.
13 *
14 * @APPLE_LICENSE_HEADER_START@
15 *
16 * This file contains Original Code and/or Modifications of Original Code
17 * as defined in and that are subject to the Apple Public Source License
18 * Version 2.0 (the 'License'). You may not use this file except in
19 * compliance with the License. Please obtain a copy of the License at
20 * http://www.opensource.apple.com/apsl/ and read it before using this
21 * file.
22 *
23 * The Original Code and all software distributed under the License are
24 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
25 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
26 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
28 * Please see the License for the specific language governing rights and
29 * limitations under the License.
30 *
31 * @APPLE_LICENSE_HEADER_END@
32 *
33 * keychain_add.c
34 */
35
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/utsname.h>
42 #include <sys/stat.h>
43 #include <time.h>
44
45 #include <Security/SecItem.h>
46
47 #include <CoreFoundation/CFNumber.h>
48 #include <CoreFoundation/CFString.h>
49
50 #include <Security/SecureObjectSync/SOSCloudCircle.h>
51 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
52 #include <Security/SecureObjectSync/SOSPeerInfo.h>
53 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
54 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
55 #include <Security/SecureObjectSync/SOSUserKeygen.h>
56 #include <Security/SecureObjectSync/SOSKVSKeys.h>
57 #include <securityd/SOSCloudCircleServer.h>
58 #include <Security/SecOTRSession.h>
59 #include <SOSCircle/CKBridge/SOSCloudKeychainClient.h>
60
61 #include <utilities/SecCFWrappers.h>
62 #include <utilities/debugging.h>
63
64 #include <SecurityTool/readline.h>
65 #include <notify.h>
66
67 #include "SOSSysdiagnose.h"
68 #include "keychain_log.h"
69 #include "secToolFileIO.h"
70 #include "secViewDisplay.h"
71 #include "accountCirclesViewsPrint.h"
72 #include <utilities/debugging.h>
73
74
75 #include <Security/SecPasswordGenerate.h>
76
77 #define MAXKVSKEYTYPE kUnknownKey
78 #define DATE_LENGTH 18
79
80 #define USE_NEW_SPI 1
81 #if ! USE_NEW_SPI
82
83 static char *createDateStrNow() {
84 char *retval = NULL;
85 time_t clock;
86
87 struct tm *tmstruct;
88
89 time(&clock);
90 tmstruct = localtime(&clock);
91
92 retval = malloc(15);
93 sprintf(retval, "%04d%02d%02d%02d%02d%02d", tmstruct->tm_year+1900, tmstruct->tm_mon+1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
94 return retval;
95 }
96
97 // #include <CoreFoundation/CFPriv.h>
98
99 CF_EXPORT CFDictionaryRef _CFCopySystemVersionDictionary(void);
100 CF_EXPORT const CFStringRef _kCFSystemVersionProductNameKey;
101 CF_EXPORT const CFStringRef _kCFSystemVersionProductVersionKey;
102 CF_EXPORT const CFStringRef _kCFSystemVersionBuildVersionKey;
103
104 static char *CFDictionaryCopyCString(CFDictionaryRef dict, const void *key) {
105 CFStringRef val = CFDictionaryGetValue(dict, key);
106 char *retval = CFStringToCString(val);
107 return retval;
108 }
109
110 #include <pwd.h>
111
112 static void sysdiagnose_dump() {
113 char *outputBase = NULL;
114 char *outputParent = NULL;
115 char *outputDir = NULL;
116 char hostname[80];
117 char *productName = "NA";
118 char *productVersion = "NA";
119 char *buildVersion = "NA";
120 char *keysToRegister = NULL;
121 char *cloudkeychainproxy3 = NULL;
122 char *now = createDateStrNow();
123 size_t length = 0;
124 int status = 0;
125 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
126
127 if(gethostname(hostname, 80)) {
128 strcpy(hostname, "unknownhost");
129 }
130
131 if(sysfdef) {
132 productName = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionProductNameKey);
133 productVersion = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionProductVersionKey);
134 buildVersion = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionBuildVersionKey);
135 }
136
137 // OUTPUTBASE=ckcdiagnose_snapshot_${HOSTNAME}_${PRODUCT_VERSION}_${NOW}
138 length = strlen("ckcdiagnose_snapshot___") + strlen(hostname) + strlen(productVersion) + strlen(now) + 1;
139 outputBase = malloc(length);
140 status = snprintf(outputBase, length, "ckcdiagnose_snapshot_%s_%s_%s", hostname, productVersion, now);
141 if(status < 0) outputBase = "";
142
143 #if TARGET_OS_EMBEDDED
144 outputParent = "/Library/Logs/CrashReporter";
145 keysToRegister = "/private/var/preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
146 cloudkeychainproxy3 = "/var/mobile/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
147 #else
148 outputParent = "/var/tmp";
149 {
150 char *homeDir = "";
151 struct passwd* pwd = getpwuid(getuid());
152 if (pwd) homeDir = pwd->pw_dir;
153
154 char *k2regfmt = "%s/Library/Preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
155 char *ckp3fmt = "%s/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
156 size_t k2rlen = strlen(homeDir) + strlen(k2regfmt) + 2;
157 size_t ckp3len = strlen(homeDir) + strlen(ckp3fmt) + 2;
158 keysToRegister = malloc(k2rlen);
159 cloudkeychainproxy3 = malloc(ckp3len);
160 snprintf(keysToRegister, k2rlen, k2regfmt, homeDir);
161 snprintf(cloudkeychainproxy3, ckp3len, ckp3fmt, homeDir);
162 }
163 #endif
164
165 length = strlen(outputParent) + strlen(outputBase) + 2;
166 outputDir = malloc(length);
167 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
168 if(status < 0) return;
169
170 mkdir(outputDir, 0700);
171
172 SOSLogSetOutputTo(outputDir, "sw_vers.log");
173 // report uname stuff + hostname
174 fprintf(outFile, "HostName: %s\n", hostname);
175 fprintf(outFile, "ProductName: %s\n", productName);
176 fprintf(outFile, "ProductVersion: %s\n", productVersion);
177 fprintf(outFile, "BuildVersion: %s\n", buildVersion);
178 closeOutput();
179
180 SOSLogSetOutputTo(outputDir, "syncD.log");
181 // do sync -D
182 SOSCCDumpCircleKVSInformation(optarg);
183 closeOutput();
184
185 SOSLogSetOutputTo(outputDir, "synci.log");
186 // do sync -i
187 SOSCCDumpCircleInformation();
188 SOSCCDumpEngineInformation();
189 closeOutput();
190
191 SOSLogSetOutputTo(outputDir, "syncL.log");
192 // do sync -L
193 listviewcmd(NULL);
194 closeOutput();
195
196 copyFileToOutputDir(outputDir, keysToRegister);
197 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
198
199 free(now);
200 CFReleaseNull(sysfdef);
201 #if ! TARGET_OS_EMBEDDED
202 free(keysToRegister);
203 free(cloudkeychainproxy3);
204 #endif
205
206 }
207 #else
208 static void sysdiagnose_dump() {
209 SOSCCSysdiagnose(NULL);
210 }
211
212 #endif /* USE_NEW_SPI */
213
214 static bool logmark(const char *optarg) {
215 if(!optarg) return false;
216 secnotice("mark", "%s", optarg);
217 return true;
218 }
219
220
221 // enable, disable, accept, reject, status, Reset, Clear
222 int
223 keychain_log(int argc, char * const *argv)
224 {
225 /*
226 "Keychain Logging"
227 " -i info (current status)"
228 " -D [itemName] dump contents of KVS"
229 " -L list all known view and their status"
230 " -s sysdiagnose log dumps"
231 " -M string place a mark in the syslog - category \"mark\""
232
233 */
234 SOSLogSetOutputTo(NULL, NULL);
235
236 int ch, result = 0;
237 CFErrorRef error = NULL;
238 bool hadError = false;
239
240 while ((ch = getopt(argc, argv, "DiLM:s")) != -1)
241 switch (ch) {
242
243 case 'i':
244 SOSCCDumpCircleInformation();
245 SOSCCDumpEngineInformation();
246 break;
247
248
249 case 's':
250 sysdiagnose_dump();
251 break;
252
253 case 'D':
254 (void)SOSCCDumpCircleKVSInformation(optarg);
255 break;
256
257 case 'L':
258 hadError = !listviewcmd(&error);
259 break;
260
261 case 'M':
262 hadError = !logmark(optarg);
263 break;
264
265 case '?':
266 default:
267 return SHOW_USAGE_MESSAGE;
268 }
269
270 if (hadError)
271 printerr(CFSTR("Error: %@\n"), error);
272
273 return result;
274 }