5 // Created by Richard Murphy on 1/27/16.
10 #include "SOSCloudCircleInternal.h"
15 #include <sys/utsname.h>
21 #include <Security/SecItem.h>
23 #include <CoreFoundation/CFNumber.h>
24 #include <CoreFoundation/CFString.h>
26 #include <Security/SecureObjectSync/SOSCloudCircle.h>
27 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
28 #include <Security/SecureObjectSync/SOSPeerInfo.h>
29 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
30 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
31 #include <Security/SecureObjectSync/SOSUserKeygen.h>
32 #include <Security/SecureObjectSync/SOSKVSKeys.h>
33 #include <securityd/SOSCloudCircleServer.h>
34 #include <Security/SecOTRSession.h>
35 #include <SOSCircle/CKBridge/SOSCloudKeychainClient.h>
37 #include <utilities/SecCFWrappers.h>
38 #include <utilities/debugging.h>
40 #include <SecurityTool/readline.h>
42 #include "keychain_log.h"
43 #include "secToolFileIO.h"
44 #include "secViewDisplay.h"
45 #include "accountCirclesViewsPrint.h"
49 #include <Security/SecPasswordGenerate.h>
51 /* Copied from CFPriv.h */
52 // #include <CoreFoundation/CFPriv.h>
54 CF_EXPORT CFDictionaryRef _CFCopySystemVersionDictionary(void);
55 CF_EXPORT const CFStringRef _kCFSystemVersionProductNameKey;
56 CF_EXPORT const CFStringRef _kCFSystemVersionProductVersionKey;
57 CF_EXPORT const CFStringRef _kCFSystemVersionBuildVersionKey;
61 static char *CFDictionaryCopyCStringWithDefault(CFDictionaryRef dict, const void *key, char *defaultString) {
63 require_quiet(dict, use_default);
64 CFStringRef val = CFDictionaryGetValue(dict, key);
65 retval = CFStringToCString(val);
67 if(!retval) retval = strdup(defaultString);
71 static char *createDateStrNow() {
78 tmstruct = localtime(&clock);
81 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);
85 #if !TARGET_OS_EMBEDDED
86 static char *assemblePath(char *dir, char *fname) {
87 size_t length = strlen(dir) + strlen(fname) + 2;
88 char *outputDir = malloc(length);
89 int status = snprintf(outputDir, length, "%s/%s", dir, fname);
90 if(status < 0) return NULL;
94 static char *homedirPath() {
96 struct passwd* pwd = getpwuid(getuid());
97 if (pwd) homeDir = pwd->pw_dir;
102 static char *sysdiagnose_dir(const char *passedIn, const char *hostname, const char *productVersion, const char *now) {
103 if(passedIn) return (char *) passedIn;
105 // OUTPUTBASE=ckcdiagnose_snapshot_${HOSTNAME}_${PRODUCT_VERSION}_${NOW}
106 char *outputParent = NULL;
107 size_t length = strlen("ckcdiagnose_snapshot___") + strlen(hostname) + strlen(productVersion) + strlen(now) + 1;
108 char *outputBase = malloc(length);
109 int status = snprintf(outputBase, length, "ckcdiagnose_snapshot_%s_%s_%s", hostname, productVersion, now);
110 if(status < 0) outputBase = "";
112 #if TARGET_OS_EMBEDDED
113 outputParent = "/Library/Logs/CrashReporter";
115 outputParent = "/var/tmp";
117 length = strlen(outputParent) + strlen(outputBase) + 2;
118 char *outputDir = malloc(length);
119 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
120 if(status < 0) return NULL;
125 static char *sysdiagnose_dump(const char *dirname) {
126 char *outputDir = NULL;
128 char *productName = NULL;
129 char *productVersion = NULL;
130 char *buildVersion = NULL;
131 char *keysToRegister = NULL;
132 char *cloudkeychainproxy3 = NULL;
133 char *now = createDateStrNow();
135 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
136 productName = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductNameKey, "unknownProduct");
137 productVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductVersionKey, "unknownProductVersion");
138 buildVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionBuildVersionKey, "unknownVersion");
140 if(gethostname(hostname, 80)) {
141 strcpy(hostname, "unknownhost");
144 #if TARGET_OS_EMBEDDED
145 keysToRegister = "/private/var/preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
146 cloudkeychainproxy3 = "/var/mobile/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
148 char *homeDir = homedirPath();
149 keysToRegister = assemblePath(homeDir, "Library/Preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
150 cloudkeychainproxy3 = assemblePath(homeDir, "Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist");
153 outputDir = sysdiagnose_dir(dirname, hostname, productVersion, now);
154 if(!outputDir) goto errOut;
156 mkdir(outputDir, 0700);
158 SOSLogSetOutputTo(outputDir, "sw_vers.log");
159 // report uname stuff + hostname
160 fprintf(outFile, "HostName: %s\n", hostname);
161 fprintf(outFile, "ProductName: %s\n", productName);
162 fprintf(outFile, "ProductVersion: %s\n", productVersion);
163 fprintf(outFile, "BuildVersion: %s\n", buildVersion);
166 SOSLogSetOutputTo(outputDir, "syncD.log");
168 SOSCCDumpCircleKVSInformation(optarg);
171 SOSLogSetOutputTo(outputDir, "synci.log");
173 SOSCCDumpCircleInformation();
176 SOSLogSetOutputTo(outputDir, "syncL.log");
181 copyFileToOutputDir(outputDir, keysToRegister);
182 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
186 CFReleaseNull(sysfdef);
187 #if ! TARGET_OS_EMBEDDED
188 free(keysToRegister);
189 free(cloudkeychainproxy3);
191 if(productName) free(productName);
192 if(productVersion) free(productVersion);
193 if(buildVersion) free(buildVersion);
198 char *SOSCCSysdiagnose(const char *directoryname) {
199 sysdiagnose_dump(directoryname);