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 "SOSSysdiagnose.h"
43 #include "keychain_log.h"
44 #include "secToolFileIO.h"
45 #include "secViewDisplay.h"
46 #include "accountCirclesViewsPrint.h"
50 #include <Security/SecPasswordGenerate.h>
52 /* Copied from CFPriv.h */
53 // #include <CoreFoundation/CFPriv.h>
55 CF_EXPORT CFDictionaryRef _CFCopySystemVersionDictionary(void);
56 CF_EXPORT const CFStringRef _kCFSystemVersionProductNameKey;
57 CF_EXPORT const CFStringRef _kCFSystemVersionProductVersionKey;
58 CF_EXPORT const CFStringRef _kCFSystemVersionBuildVersionKey;
62 static char *CFDictionaryCopyCStringWithDefault(CFDictionaryRef dict, const void *key, char *defaultString) {
64 require_quiet(dict, use_default);
65 CFStringRef val = CFDictionaryGetValue(dict, key);
66 retval = CFStringToCString(val);
68 if(!retval) retval = strdup(defaultString);
72 static char *createDateStrNow() {
79 tmstruct = localtime(&clock);
82 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);
86 #if !TARGET_OS_EMBEDDED
87 static char *assemblePath(char *dir, char *fname) {
88 size_t length = strlen(dir) + strlen(fname) + 2;
89 char *outputDir = malloc(length);
90 int status = snprintf(outputDir, length, "%s/%s", dir, fname);
92 if(outputDir) free(outputDir);
98 static char *homedirPath() {
100 struct passwd* pwd = getpwuid(getuid());
101 if (pwd) homeDir = pwd->pw_dir;
106 static char *sysdiagnose_dir(const char *passedIn, const char *hostname, const char *productVersion, const char *now) {
107 if(passedIn) return (char *) passedIn;
109 // OUTPUTBASE=ckcdiagnose_snapshot_${HOSTNAME}_${PRODUCT_VERSION}_${NOW}
110 char *outputParent = NULL;
111 size_t length = strlen("ckcdiagnose_snapshot___") + strlen(hostname) + strlen(productVersion) + strlen(now) + 1;
112 char *outputBase = malloc(length);
113 int status = snprintf(outputBase, length, "ckcdiagnose_snapshot_%s_%s_%s", hostname, productVersion, now);
114 if(status < 0) outputBase = "";
116 #if TARGET_OS_EMBEDDED
117 outputParent = "/Library/Logs/CrashReporter";
119 outputParent = "/var/tmp";
121 length = strlen(outputParent) + strlen(outputBase) + 2;
122 char *outputDir = malloc(length);
123 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
125 if(outputDir) free(outputDir);
132 static char *sysdiagnose_dump(const char *dirname) {
133 char *outputDir = NULL;
135 char *productName = NULL;
136 char *productVersion = NULL;
137 char *buildVersion = NULL;
138 char *keysToRegister = NULL;
139 char *cloudkeychainproxy3 = NULL;
140 char *now = createDateStrNow();
142 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
143 productName = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductNameKey, "unknownProduct");
144 productVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductVersionKey, "unknownProductVersion");
145 buildVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionBuildVersionKey, "unknownVersion");
147 if(gethostname(hostname, 80)) {
148 strcpy(hostname, "unknownhost");
151 #if TARGET_OS_EMBEDDED
152 keysToRegister = "/private/var/preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
153 cloudkeychainproxy3 = "/var/mobile/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
155 char *homeDir = homedirPath();
156 keysToRegister = assemblePath(homeDir, "Library/Preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
157 cloudkeychainproxy3 = assemblePath(homeDir, "Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist");
160 outputDir = sysdiagnose_dir(dirname, hostname, productVersion, now);
161 if(!outputDir) goto errOut;
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();
183 SOSLogSetOutputTo(outputDir, "syncL.log");
188 copyFileToOutputDir(outputDir, keysToRegister);
189 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
193 CFReleaseNull(sysfdef);
194 #if ! TARGET_OS_EMBEDDED
195 free(keysToRegister);
196 free(cloudkeychainproxy3);
198 if(productName) free(productName);
199 if(productVersion) free(productVersion);
200 if(buildVersion) free(buildVersion);
205 char *SOSCCSysdiagnose(const char *directoryname) {
206 sysdiagnose_dump(directoryname);