]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Tool/keychain_log.m
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Tool / keychain_log.m
1 //
2 // keychain_log.c
3 // sec
4 //
5 // Created by Richard Murphy on 1/26/16.
6 //
7 //
8
9 #include "keychain_log.h"
10
11 /*
12 * Copyright (c) 2003-2007,2009-2010,2013-2014 Apple Inc. All Rights Reserved.
13 *
14 * @APPLE_LICENSE_HEADER_START@
15 *
16 * This file contains Original Code and/or Modifications of Original Code
17 * as defined in and that are subject to the Apple Public Source License
18 * Version 2.0 (the 'License'). You may not use this file except in
19 * compliance with the License. Please obtain a copy of the License at
20 * http://www.opensource.apple.com/apsl/ and read it before using this
21 * file.
22 *
23 * The Original Code and all software distributed under the License are
24 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
25 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
26 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
28 * Please see the License for the specific language governing rights and
29 * limitations under the License.
30 *
31 * @APPLE_LICENSE_HEADER_END@
32 *
33 * keychain_add.c
34 */
35
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/utsname.h>
42 #include <sys/stat.h>
43 #include <time.h>
44
45 #include <Security/SecItem.h>
46
47 #include <CoreFoundation/CoreFoundation.h>
48 #include <CoreFoundation/CFPriv.h>
49
50 #include <Security/SecureObjectSync/SOSCloudCircle.h>
51 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
52 #include <Security/SecureObjectSync/SOSPeerInfo.h>
53 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
54 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
55 #include <Security/SecureObjectSync/SOSUserKeygen.h>
56 #include <Security/SecureObjectSync/SOSKVSKeys.h>
57 #include <securityd/SOSCloudCircleServer.h>
58 #include <Security/SecOTRSession.h>
59 #include <SOSCircle/CKBridge/SOSCloudKeychainClient.h>
60
61 #include <utilities/SecCFWrappers.h>
62 #include <utilities/debugging.h>
63
64 #include <SecurityTool/readline.h>
65 #include <notify.h>
66
67 #include "SOSSysdiagnose.h"
68 #include "keychain_log.h"
69 #include "secToolFileIO.h"
70 #include "secViewDisplay.h"
71 #include "accountCirclesViewsPrint.h"
72 #include <utilities/debugging.h>
73
74
75 #include <Security/SecPasswordGenerate.h>
76
77 #define MAXKVSKEYTYPE kUnknownKey
78 #define DATE_LENGTH 18
79
80 #define USE_NEW_SPI 1
81 #if ! USE_NEW_SPI
82
83 static char *createDateStrNow() {
84 char *retval = NULL;
85 time_t clock;
86
87 struct tm *tmstruct;
88
89 time(&clock);
90 tmstruct = localtime(&clock);
91
92 retval = malloc(15);
93 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);
94 return retval;
95 }
96
97 static char *CFDictionaryCopyCString(CFDictionaryRef dict, const void *key) {
98 CFStringRef val = CFDictionaryGetValue(dict, key);
99 char *retval = CFStringToCString(val);
100 return retval;
101 }
102
103 #include <pwd.h>
104
105 static void sysdiagnose_dump() {
106 char *outputBase = NULL;
107 char *outputParent = NULL;
108 char *outputDir = NULL;
109 char hostname[80];
110 char *productName = "NA";
111 char *productVersion = "NA";
112 char *buildVersion = "NA";
113 char *keysToRegister = NULL;
114 char *cloudkeychainproxy3 = NULL;
115 char *now = createDateStrNow();
116 size_t length = 0;
117 int status = 0;
118 CFDictionaryRef sysfdef = _CFCopySystemVersionDictionary();
119
120 if(gethostname(hostname, 80)) {
121 strcpy(hostname, "unknownhost");
122 }
123
124 if(sysfdef) {
125 productName = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionProductNameKey);
126 productVersion = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionProductVersionKey);
127 buildVersion = CFDictionaryCopyCString(sysfdef, _kCFSystemVersionBuildVersionKey);
128 }
129
130 // OUTPUTBASE=ckcdiagnose_snapshot_${HOSTNAME}_${PRODUCT_VERSION}_${NOW}
131 length = strlen("ckcdiagnose_snapshot___") + strlen(hostname) + strlen(productVersion) + strlen(now) + 1;
132 outputBase = malloc(length);
133 status = snprintf(outputBase, length, "ckcdiagnose_snapshot_%s_%s_%s", hostname, productVersion, now);
134 if(status < 0) outputBase = "";
135
136 #if TARGET_OS_EMBEDDED
137 outputParent = "/Library/Logs/CrashReporter";
138 keysToRegister = "/private/var/preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
139 cloudkeychainproxy3 = "/var/mobile/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
140 #else
141 outputParent = "/var/tmp";
142 {
143 char *homeDir = "";
144 struct passwd* pwd = getpwuid(getuid());
145 if (pwd) homeDir = pwd->pw_dir;
146
147 char *k2regfmt = "%s/Library/Preferences/com.apple.security.cloudkeychainproxy3.keysToRegister.plist";
148 char *ckp3fmt = "%s/Library/SyncedPreferences/com.apple.security.cloudkeychainproxy3.plist";
149 size_t k2rlen = strlen(homeDir) + strlen(k2regfmt) + 2;
150 size_t ckp3len = strlen(homeDir) + strlen(ckp3fmt) + 2;
151 keysToRegister = malloc(k2rlen);
152 cloudkeychainproxy3 = malloc(ckp3len);
153 snprintf(keysToRegister, k2rlen, k2regfmt, homeDir);
154 snprintf(cloudkeychainproxy3, ckp3len, ckp3fmt, homeDir);
155 }
156 #endif
157
158 length = strlen(outputParent) + strlen(outputBase) + 2;
159 outputDir = malloc(length);
160 status = snprintf(outputDir, length, "%s/%s", outputParent, outputBase);
161 if(status < 0) return;
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 SOSCCDumpEngineInformation();
182 closeOutput();
183
184 SOSLogSetOutputTo(outputDir, "syncL.log");
185 // do sync -L
186 listviewcmd(NULL);
187 closeOutput();
188
189 copyFileToOutputDir(outputDir, keysToRegister);
190 copyFileToOutputDir(outputDir, cloudkeychainproxy3);
191
192 free(now);
193 CFReleaseNull(sysfdef);
194 #if ! TARGET_OS_EMBEDDED
195 free(keysToRegister);
196 free(cloudkeychainproxy3);
197 #endif
198
199 }
200 #else
201 static void sysdiagnose_dump() {
202 SOSCCSysdiagnose(NULL);
203 }
204
205 #endif /* USE_NEW_SPI */
206
207 static bool logmark(const char *optarg) {
208 if(!optarg) return false;
209 secnotice("mark", "%s", optarg);
210 return true;
211 }
212
213
214 // enable, disable, accept, reject, status, Reset, Clear
215 int
216 keychain_log(int argc, char * const *argv)
217 {
218 /*
219 "Keychain Logging"
220 " -i info (current status)"
221 " -D [itemName] dump contents of KVS"
222 " -L list all known view and their status"
223 " -s sysdiagnose log dumps"
224 " -M string place a mark in the syslog - category \"mark\""
225
226 */
227 SOSLogSetOutputTo(NULL, NULL);
228
229 int ch, result = 0;
230 CFErrorRef error = NULL;
231 bool hadError = false;
232
233 while ((ch = getopt(argc, argv, "DiLM:s")) != -1)
234 switch (ch) {
235
236 case 'i':
237 SOSCCDumpCircleInformation();
238 SOSCCDumpEngineInformation();
239 break;
240
241
242 case 's':
243 sysdiagnose_dump();
244 break;
245
246 case 'D':
247 (void)SOSCCDumpCircleKVSInformation(optarg);
248 break;
249
250 case 'L':
251 hadError = !listviewcmd(&error);
252 break;
253
254 case 'M':
255 hadError = !logmark(optarg);
256 break;
257
258 case '?':
259 default:
260 return SHOW_USAGE_MESSAGE;
261 }
262
263 if (hadError)
264 printerr(CFSTR("Error: %@\n"), error);
265
266 return result;
267 }