]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSSysdiagnose.m
Security-58286.1.32.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 "keychain_log.h"
43 #include "secToolFileIO.h"
44 #include "secViewDisplay.h"
45 #include "accountCirclesViewsPrint.h"
46
47
48
49 #include <Security/SecPasswordGenerate.h>
50
51 /* Copied from CFPriv.h */
52 // #include <CoreFoundation/CFPriv.h>
53
54 CF_EXPORT CFDictionaryRef _CFCopySystemVersionDictionary(void);
55 CF_EXPORT const CFStringRef _kCFSystemVersionProductNameKey;
56 CF_EXPORT const CFStringRef _kCFSystemVersionProductVersionKey;
57 CF_EXPORT const CFStringRef _kCFSystemVersionBuildVersionKey;
58
59
60
61 static char *CFDictionaryCopyCStringWithDefault(CFDictionaryRef dict, const void *key, char *defaultString) {
62 char *retval = NULL;
63 require_quiet(dict, use_default);
64 CFStringRef val = CFDictionaryGetValue(dict, key);
65 retval = CFStringToCString(val);
66 use_default:
67 if(!retval) retval = strdup(defaultString);
68 return retval;
69 }
70
71 static char *createDateStrNow() {
72 char *retval = NULL;
73 time_t clock;
74
75 struct tm *tmstruct;
76
77 time(&clock);
78 tmstruct = localtime(&clock);
79
80 retval = malloc(15);
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);
82 return retval;
83 }
84
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;
91 return outputDir;
92 }
93
94 static char *homedirPath() {
95 char *homeDir = "";
96 struct passwd* pwd = getpwuid(getuid());
97 if (pwd) homeDir = pwd->pw_dir;
98 return homeDir;
99 }
100 #endif
101
102 static char *sysdiagnose_dir(const char *passedIn, const char *hostname, const char *productVersion, const char *now) {
103 if(passedIn) return (char *) passedIn;
104
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 = "";
111
112 #if TARGET_OS_EMBEDDED
113 outputParent = "/Library/Logs/CrashReporter";
114 #else
115 outputParent = "/var/tmp";
116 #endif
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;
121 return outputDir;
122 }
123
124
125 static char *sysdiagnose_dump(const char *dirname) {
126 char *outputDir = NULL;
127 char hostname[80];
128 char *productName = NULL;
129 char *productVersion = NULL;
130 char *buildVersion = NULL;
131 char *keysToRegister = NULL;
132 char *cloudkeychainproxy3 = NULL;
133 char *now = createDateStrNow();
134
135 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
136 productName = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductNameKey, "unknownProduct");
137 productVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionProductVersionKey, "unknownProductVersion");
138 buildVersion = CFDictionaryCopyCStringWithDefault(sysfdef, _kCFSystemVersionBuildVersionKey, "unknownVersion");
139
140 if(gethostname(hostname, 80)) {
141 strcpy(hostname, "unknownhost");
142 }
143
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";
147 #else
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");
151 #endif
152
153 outputDir = sysdiagnose_dir(dirname, hostname, productVersion, now);
154 if(!outputDir) goto errOut;
155
156 mkdir(outputDir, 0700);
157
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);
164 closeOutput();
165
166 SOSLogSetOutputTo(outputDir, "syncD.log");
167 // do sync -D
168 SOSCCDumpCircleKVSInformation(optarg);
169 closeOutput();
170
171 SOSLogSetOutputTo(outputDir, "synci.log");
172 // do sync -i
173 SOSCCDumpCircleInformation();
174 closeOutput();
175
176 SOSLogSetOutputTo(outputDir, "syncL.log");
177 // do sync -L
178 listviewcmd(NULL);
179 closeOutput();
180
181 copyFileToOutputDir(outputDir, keysToRegister);
182 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
183
184 errOut:
185 if(now) free(now);
186 CFReleaseNull(sysfdef);
187 #if ! TARGET_OS_EMBEDDED
188 free(keysToRegister);
189 free(cloudkeychainproxy3);
190 #endif
191 if(productName) free(productName);
192 if(productVersion) free(productVersion);
193 if(buildVersion) free(buildVersion);
194 return outputDir;
195 }
196
197
198 char *SOSCCSysdiagnose(const char *directoryname) {
199 sysdiagnose_dump(directoryname);
200 return NULL;
201 }
202