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/CoreFoundation.h>
24 #include <CoreFoundation/CFPriv.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 static char *CFDictionaryCopyCStringWithDefault(CFDictionaryRef dict, const void *key, char *defaultString) {
54 require_quiet(dict, use_default);
55 CFStringRef val = CFDictionaryGetValue(dict, key);
56 retval = CFStringToCString(val);
58 if(!retval) retval = strdup(defaultString);
62 static char *createDateStrNow() {
69 tmstruct = localtime(&clock);
72 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);
76 #if !TARGET_OS_EMBEDDED
77 static char *assemblePath(char *dir, char *fname) {
78 size_t length = strlen(dir) + strlen(fname) + 2;
79 char *outputDir = malloc(length);
80 int status = snprintf(outputDir, length, "%s/%s", dir, fname);
82 if(outputDir) free(outputDir);
88 static char *homedirPath() {
90 struct passwd* pwd = getpwuid(getuid());
91 if (pwd) homeDir = pwd->pw_dir;
96 static char *sysdiagnose_dir(const char *passedIn, const char *hostname, const char *productVersion, const char *now) {
97 if(passedIn) return (char *) passedIn;
99 // OUTPUTBASE=ckcdiagnose_snapshot_${HOSTNAME}_${PRODUCT_VERSION}_${NOW}
100 char *outputParent = NULL;
101 size_t length = strlen("ckcdiagnose_snapshot___") + strlen(hostname) + strlen(productVersion) + strlen(now) + 1;
102 char *outputBase = malloc(length);
103 int status = snprintf(outputBase, length, "ckcdiagnose_snapshot_%s_%s_%s", hostname, productVersion, now);
104 if(status < 0) outputBase = "";
106 #if TARGET_OS_EMBEDDED
107 outputParent = "/Library/Logs/CrashReporter";
109 outputParent = "/var/tmp";
111 length = strlen(outputParent) + strlen(outputBase) + 2;
112 char *outputDir = malloc(length);
113 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
115 if(outputDir) free(outputDir);
122 static char *sysdiagnose_dump(const char *dirname) {
123 char *outputDir = NULL;
125 char *productName = NULL;
126 char *productVersion = NULL;
127 char *buildVersion = NULL;
128 char *keysToRegister = NULL;
129 char *cloudkeychainproxy3 = NULL;
130 char *now = createDateStrNow();
132 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
133 productName = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductNameKey, "unknownProduct");
134 productVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductVersionKey, "unknownProductVersion");
135 buildVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionBuildVersionKey, "unknownVersion");
137 if(gethostname(hostname, 80)) {
138 strcpy(hostname, "unknownhost");
141 #if TARGET_OS_EMBEDDED
142 keysToRegister = "/private/var/preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
143 cloudkeychainproxy3 = "/var/mobile/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
145 char *homeDir = homedirPath();
146 keysToRegister = assemblePath(homeDir, "Library/Preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
147 cloudkeychainproxy3 = assemblePath(homeDir, "Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist");
150 outputDir = sysdiagnose_dir(dirname, hostname, productVersion, now);
151 if(!outputDir) goto errOut;
153 mkdir(outputDir, 0700);
155 SOSLogSetOutputTo(outputDir, "sw_vers.log");
156 // report uname stuff + hostname
157 fprintf(outFile, "HostName: %s\n", hostname);
158 fprintf(outFile, "ProductName: %s\n", productName);
159 fprintf(outFile, "ProductVersion: %s\n", productVersion);
160 fprintf(outFile, "BuildVersion: %s\n", buildVersion);
163 SOSLogSetOutputTo(outputDir, "syncD.log");
165 SOSCCDumpCircleKVSInformation(optarg);
168 SOSLogSetOutputTo(outputDir, "synci.log");
170 SOSCCDumpCircleInformation();
173 SOSLogSetOutputTo(outputDir, "syncL.log");
178 copyFileToOutputDir(outputDir, keysToRegister);
179 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
183 CFReleaseNull(sysfdef);
184 #if ! TARGET_OS_EMBEDDED
185 free(keysToRegister);
186 free(cloudkeychainproxy3);
188 if(productName) free(productName);
189 if(productVersion) free(productVersion);
190 if(buildVersion) free(buildVersion);
195 char *SOSCCSysdiagnose(const char *directoryname) {
196 sysdiagnose_dump(directoryname);