]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSSysdiagnose.m
Security-58286.51.6.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/CFNumber.h>
24 #include <CoreFoundation/CFString.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 /* Copied from CFPriv.h */
53 // #include <CoreFoundation/CFPriv.h>
54
55 CF_EXPORT CFDictionaryRef _CFCopySystemVersionDictionary(void);
56 CF_EXPORT const CFStringRef _kCFSystemVersionProductNameKey;
57 CF_EXPORT const CFStringRef _kCFSystemVersionProductVersionKey;
58 CF_EXPORT const CFStringRef _kCFSystemVersionBuildVersionKey;
59
60
61
62 static char *CFDictionaryCopyCStringWithDefault(CFDictionaryRef dict, const void *key, char *defaultString) {
63 char *retval = NULL;
64 require_quiet(dict, use_default);
65 CFStringRef val = CFDictionaryGetValue(dict, key);
66 retval = CFStringToCString(val);
67 use_default:
68 if(!retval) retval = strdup(defaultString);
69 return retval;
70 }
71
72 static char *createDateStrNow() {
73 char *retval = NULL;
74 time_t clock;
75
76 struct tm *tmstruct;
77
78 time(&clock);
79 tmstruct = localtime(&clock);
80
81 retval = malloc(15);
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);
83 return retval;
84 }
85
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);
91 if(status < 0) {
92 if(outputDir) free(outputDir);
93 return NULL;
94 }
95 return outputDir;
96 }
97
98 static char *homedirPath() {
99 char *homeDir = "";
100 struct passwd* pwd = getpwuid(getuid());
101 if (pwd) homeDir = pwd->pw_dir;
102 return homeDir;
103 }
104 #endif
105
106 static char *sysdiagnose_dir(const char *passedIn, const char *hostname, const char *productVersion, const char *now) {
107 if(passedIn) return (char *) passedIn;
108
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 = "";
115
116 #if TARGET_OS_EMBEDDED
117 outputParent = "/Library/Logs/CrashReporter";
118 #else
119 outputParent = "/var/tmp";
120 #endif
121 length = strlen(outputParent) + strlen(outputBase) + 2;
122 char *outputDir = malloc(length);
123 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
124 if(status < 0) {
125 if(outputDir) free(outputDir);
126 return NULL;
127 }
128 return outputDir;
129 }
130
131
132 static char *sysdiagnose_dump(const char *dirname) {
133 char *outputDir = NULL;
134 char hostname[80];
135 char *productName = NULL;
136 char *productVersion = NULL;
137 char *buildVersion = NULL;
138 char *keysToRegister = NULL;
139 char *cloudkeychainproxy3 = NULL;
140 char *now = createDateStrNow();
141
142 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
143 productName = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductNameKey, "unknownProduct");
144 productVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductVersionKey, "unknownProductVersion");
145 buildVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionBuildVersionKey, "unknownVersion");
146
147 if(gethostname(hostname, 80)) {
148 strcpy(hostname, "unknownhost");
149 }
150
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";
154 #else
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");
158 #endif
159
160 outputDir = sysdiagnose_dir(dirname, hostname, productVersion, now);
161 if(!outputDir) goto errOut;
162
163 mkdir(outputDir, 0700);
164
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);
171 closeOutput();
172
173 SOSLogSetOutputTo(outputDir, "syncD.log");
174 // do sync -D
175 SOSCCDumpCircleKVSInformation(optarg);
176 closeOutput();
177
178 SOSLogSetOutputTo(outputDir, "synci.log");
179 // do sync -i
180 SOSCCDumpCircleInformation();
181 closeOutput();
182
183 SOSLogSetOutputTo(outputDir, "syncL.log");
184 // do sync -L
185 listviewcmd(NULL);
186 closeOutput();
187
188 copyFileToOutputDir(outputDir, keysToRegister);
189 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
190
191 errOut:
192 if(now) free(now);
193 CFReleaseNull(sysfdef);
194 #if ! TARGET_OS_EMBEDDED
195 free(keysToRegister);
196 free(cloudkeychainproxy3);
197 #endif
198 if(productName) free(productName);
199 if(productVersion) free(productVersion);
200 if(buildVersion) free(buildVersion);
201 return outputDir;
202 }
203
204
205 char *SOSCCSysdiagnose(const char *directoryname) {
206 sysdiagnose_dump(directoryname);
207 return NULL;
208 }
209