5 // Created by Richard Murphy on 1/26/16.
9 #include "keychain_log.h"
12 * Copyright (c) 2003-2007,2009-2010,2013-2014 Apple Inc. All Rights Reserved.
14 * @APPLE_LICENSE_HEADER_START@
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
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.
31 * @APPLE_LICENSE_HEADER_END@
41 #include <sys/utsname.h>
45 #include <Security/SecItem.h>
47 #include <CoreFoundation/CoreFoundation.h>
48 #include <CoreFoundation/CFPriv.h>
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>
61 #include <utilities/SecCFWrappers.h>
62 #include <utilities/debugging.h>
64 #include <SecurityTool/readline.h>
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>
75 #include <Security/SecPasswordGenerate.h>
77 #define MAXKVSKEYTYPE kUnknownKey
78 #define DATE_LENGTH 18
83 static char *createDateStrNow() {
90 tmstruct = localtime(&clock);
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);
97 static char *CFDictionaryCopyCString(CFDictionaryRef dict, const void *key) {
98 CFStringRef val = CFDictionaryGetValue(dict, key);
99 char *retval = CFStringToCString(val);
105 static void sysdiagnose_dump() {
106 char *outputBase = NULL;
107 char *outputParent = NULL;
108 char *outputDir = NULL;
110 char *productName = "NA";
111 char *productVersion = "NA";
112 char *buildVersion = "NA";
113 char *keysToRegister = NULL;
114 char *cloudkeychainproxy3 = NULL;
115 char *now = createDateStrNow();
118 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
120 if(gethostname(hostname, 80)) {
121 strcpy(hostname, "unknownhost");
125 productName = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionProductNameKey);
126 productVersion = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionProductVersionKey);
127 buildVersion = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionBuildVersionKey);
130 // OUTPUTBASE=ckcdiagnose_snapshot_${HOSTNAME}_${PRODUCT_VERSION}_${NOW}
131 length = strlen("ckcdiagnose_snapshot___") + strlen(hostname) + strlen(productVersion) + strlen(now) + 1;
132 outputBase = malloc(length);
133 status = snprintf(outputBase, length, "ckcdiagnose_snapshot_%s_%s_%s", hostname, productVersion, now);
134 if(status < 0) outputBase = "";
136 #if TARGET_OS_EMBEDDED
137 outputParent = "/Library/Logs/CrashReporter";
138 keysToRegister = "/private/var/preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
139 cloudkeychainproxy3 = "/var/mobile/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
141 outputParent = "/var/tmp";
144 struct passwd* pwd = getpwuid(getuid());
145 if (pwd) homeDir = pwd->pw_dir;
147 char *k2regfmt = "%s/Library/Preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
148 char *ckp3fmt = "%s/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
149 size_t k2rlen = strlen(homeDir) + strlen(k2regfmt) + 2;
150 size_t ckp3len = strlen(homeDir) + strlen(ckp3fmt) + 2;
151 keysToRegister = malloc(k2rlen);
152 cloudkeychainproxy3 = malloc(ckp3len);
153 snprintf(keysToRegister, k2rlen, k2regfmt, homeDir);
154 snprintf(cloudkeychainproxy3, ckp3len, ckp3fmt, homeDir);
158 length = strlen(outputParent) + strlen(outputBase) + 2;
159 outputDir = malloc(length);
160 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
161 if(status < 0) return;
163 mkdir(outputDir, 0700);
165 SOSLogSetOutputTo(outputDir, "sw_vers.log");
166 // report uname stuff + hostname
167 fprintf(outFile, "HostName: %s\n", hostname);
168 fprintf(outFile, "ProductName: %s\n", productName);
169 fprintf(outFile, "ProductVersion: %s\n", productVersion);
170 fprintf(outFile, "BuildVersion: %s\n", buildVersion);
173 SOSLogSetOutputTo(outputDir, "syncD.log");
175 SOSCCDumpCircleKVSInformation(optarg);
178 SOSLogSetOutputTo(outputDir, "synci.log");
180 SOSCCDumpCircleInformation();
181 SOSCCDumpEngineInformation();
184 SOSLogSetOutputTo(outputDir, "syncL.log");
189 copyFileToOutputDir(outputDir, keysToRegister);
190 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
193 CFReleaseNull(sysfdef);
194 #if ! TARGET_OS_EMBEDDED
195 free(keysToRegister);
196 free(cloudkeychainproxy3);
201 static void sysdiagnose_dump() {
202 SOSCCSysdiagnose(NULL);
205 #endif /* USE_NEW_SPI */
207 static bool logmark(const char *optarg) {
208 if(!optarg) return false;
209 secnotice("mark", "%s", optarg);
214 // enable, disable, accept, reject, status, Reset, Clear
216 keychain_log(int argc, char * const *argv)
220 " -i info (current status)"
221 " -D [itemName] dump contents of KVS"
222 " -L list all known view and their status"
223 " -s sysdiagnose log dumps"
224 " -M string place a mark in the syslog - category \"mark\""
227 SOSLogSetOutputTo(NULL, NULL);
230 CFErrorRef error = NULL;
231 bool hadError = false;
233 while ((ch = getopt(argc, argv, "DiLM:s")) != -1)
237 SOSCCDumpCircleInformation();
238 SOSCCDumpEngineInformation();
247 (void)SOSCCDumpCircleKVSInformation(optarg);
251 hadError = !listviewcmd(&error);
255 hadError = !logmark(optarg);
260 return SHOW_USAGE_MESSAGE;
264 printerr(CFSTR("Error: %@\n"), error);