]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSSysdiagnose.m
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSSysdiagnose.m
1 //
2 // SOSSysdiagnose.c
3 // sec
4 //
5 // Created by Richard Murphy on 1/27/16.
6 //
7 //
8
9
10 #include "SOSCloudCircleInternal.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/utsname.h>
16 #include <sys/stat.h>
17 #include <time.h>
18 #include <notify.h>
19 #include <pwd.h>
20
21 #include <Security/SecItem.h>
22
23 #include <CoreFoundation/CoreFoundation.h>
24 #include <CoreFoundation/CFPriv.h>
25
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>
36
37 #include <utilities/SecCFWrappers.h>
38 #include <utilities/debugging.h>
39
40 #include <SecurityTool/readline.h>
41
42 #include "SOSSysdiagnose.h"
43 #include "keychain_log.h"
44 #include "secToolFileIO.h"
45 #include "secViewDisplay.h"
46 #include "accountCirclesViewsPrint.h"
47
48
49
50 #include <Security/SecPasswordGenerate.h>
51
52 static char *CFDictionaryCopyCStringWithDefault(CFDictionaryRef dict, const void *key, char *defaultString) {
53 char *retval = NULL;
54 require_quiet(dict, use_default);
55 CFStringRef val = CFDictionaryGetValue(dict, key);
56 retval = CFStringToCString(val);
57 use_default:
58 if(!retval) retval = strdup(defaultString);
59 return retval;
60 }
61
62 static char *createDateStrNow() {
63 char *retval = NULL;
64 time_t clock;
65
66 struct tm *tmstruct;
67
68 time(&clock);
69 tmstruct = localtime(&clock);
70
71 retval = malloc(15);
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);
73 return retval;
74 }
75
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);
81 if(status < 0) {
82 if(outputDir) free(outputDir);
83 return NULL;
84 }
85 return outputDir;
86 }
87
88 static char *homedirPath() {
89 char *homeDir = "";
90 struct passwd* pwd = getpwuid(getuid());
91 if (pwd) homeDir = pwd->pw_dir;
92 return homeDir;
93 }
94 #endif
95
96 static char *sysdiagnose_dir(const char *passedIn, const char *hostname, const char *productVersion, const char *now) {
97 if(passedIn) return (char *) passedIn;
98
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 = "";
105
106 #if TARGET_OS_EMBEDDED
107 outputParent = "/Library/Logs/CrashReporter";
108 #else
109 outputParent = "/var/tmp";
110 #endif
111 length = strlen(outputParent) + strlen(outputBase) + 2;
112 char *outputDir = malloc(length);
113 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
114 if(status < 0) {
115 if(outputDir) free(outputDir);
116 return NULL;
117 }
118 return outputDir;
119 }
120
121
122 static char *sysdiagnose_dump(const char *dirname) {
123 char *outputDir = NULL;
124 char hostname[80];
125 char *productName = NULL;
126 char *productVersion = NULL;
127 char *buildVersion = NULL;
128 char *keysToRegister = NULL;
129 char *cloudkeychainproxy3 = NULL;
130 char *now = createDateStrNow();
131
132 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
133 productName = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductNameKey, "unknownProduct");
134 productVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductVersionKey, "unknownProductVersion");
135 buildVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionBuildVersionKey, "unknownVersion");
136
137 if(gethostname(hostname, 80)) {
138 strcpy(hostname, "unknownhost");
139 }
140
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";
144 #else
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");
148 #endif
149
150 outputDir = sysdiagnose_dir(dirname, hostname, productVersion, now);
151 if(!outputDir) goto errOut;
152
153 mkdir(outputDir, 0700);
154
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);
161 closeOutput();
162
163 SOSLogSetOutputTo(outputDir, "syncD.log");
164 // do sync -D
165 SOSCCDumpCircleKVSInformation(optarg);
166 closeOutput();
167
168 SOSLogSetOutputTo(outputDir, "synci.log");
169 // do sync -i
170 SOSCCDumpCircleInformation();
171 closeOutput();
172
173 SOSLogSetOutputTo(outputDir, "syncL.log");
174 // do sync -L
175 listviewcmd(NULL);
176 closeOutput();
177
178 copyFileToOutputDir(outputDir, keysToRegister);
179 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
180
181 errOut:
182 if(now) free(now);
183 CFReleaseNull(sysfdef);
184 #if ! TARGET_OS_EMBEDDED
185 free(keysToRegister);
186 free(cloudkeychainproxy3);
187 #endif
188 if(productName) free(productName);
189 if(productVersion) free(productVersion);
190 if(buildVersion) free(buildVersion);
191 return outputDir;
192 }
193
194
195 char *SOSCCSysdiagnose(const char *directoryname) {
196 sysdiagnose_dump(directoryname);
197 return NULL;
198 }
199