]> git.saurik.com Git - apple/security.git/blob - utilities/src/iCloudKeychainTrace.c
Security-55471.14.tar.gz
[apple/security.git] / utilities / src / iCloudKeychainTrace.c
1 //
2 // iCloudKeychainTrace.c
3 // utilities
4 //
5 // Created on 7/17/13.
6 // Copyright (c) 2013 Apple Inc. All rights reserved.
7 //
8
9 #include "iCloudKeychainTrace.h"
10 #include <TargetConditionals.h>
11 #include <inttypes.h>
12 #include "SecCFWrappers.h"
13
14 const CFStringRef kCloudKeychainNumbrerOfSyncingConflicts = CFSTR("com.apple.cloudkeychain.conflictsCount");
15 const CFStringRef kCloudKeychainNumberOfTimesSyncFailed = CFSTR("com.apple.cloudkeychain.syncFailureCount");
16 const CFStringRef kCloudKeychainNumberOfConflictsResolved = CFSTR("com.apple.cloudkeychain.conflictsResolved");
17 const CFStringRef kCloudKeychainNumberOfTimesSyncedWithPeers = CFSTR("com.apple.cloudkeychain.syncedWithPeers");
18
19 static const CFStringRef gMessageTracerPrefix = CFSTR("com.apple.message.");
20
21 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
22 #include <asl.h>
23
24 static const char* gMessageTracerDomainField = "com.apple.message.domain";
25 //static const char* gTopLevelKeyForiCloudKeychainTracing = "com.apple.cloudkeychain";
26
27 static bool OSX_SetCloudKeychainTraceValueForKey(CFStringRef key, int64_t value)
28 {
29 bool result = false;
30
31 if (NULL == key)
32 {
33 return result;
34 }
35
36 aslmsg mAsl = NULL;
37 mAsl = asl_new(ASL_TYPE_MSG);
38 if (NULL == mAsl)
39 {
40 return result;
41 }
42
43 CFIndex key_length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(key), kCFStringEncodingUTF8);
44 key_length += 1; // For null
45 char base_key_buffer[key_length];
46 memset(base_key_buffer, 0,key_length);
47 if (!CFStringGetCString(key, base_key_buffer, key_length, kCFStringEncodingUTF8))
48 {
49 asl_free(mAsl);
50 return result;
51 }
52
53
54 CFStringRef key_str = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@%@"), gMessageTracerPrefix, key);
55 if (NULL == key_str)
56 {
57 asl_free(mAsl);
58 return result;
59 }
60
61 CFStringRef value_str = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%lld"), value);
62 if (NULL == value_str)
63 {
64 asl_free(mAsl);
65 CFRelease(key_str);
66 return result;
67 }
68
69 CFIndex key_str_numBytes = CFStringGetMaximumSizeForEncoding(CFStringGetLength(key_str), kCFStringEncodingUTF8);
70 key_str_numBytes += 1; // For null
71 char key_buffer[key_str_numBytes];
72 memset(key_buffer, 0, key_str_numBytes);
73 if (!CFStringGetCString(key_str, key_buffer, key_str_numBytes, kCFStringEncodingUTF8))
74 {
75 asl_free(mAsl);
76 CFRelease(key_str);
77 CFRelease(value_str);
78 return result;
79 }
80 CFRelease(key_str);
81
82 CFIndex value_str_numBytes = CFStringGetMaximumSizeForEncoding(CFStringGetLength(value_str), kCFStringEncodingUTF8);
83 value_str_numBytes += 1; // For null
84 char value_buffer[value_str_numBytes];
85 memset(value_buffer, 0, value_str_numBytes);
86 if (!CFStringGetCString(value_str, value_buffer, value_str_numBytes, kCFStringEncodingUTF8))
87 {
88 asl_free(mAsl);
89 CFRelease(value_str);
90 return result;
91 }
92 CFRelease(value_str);
93
94 asl_set(mAsl, gMessageTracerDomainField, base_key_buffer);
95
96 asl_set(mAsl, key_buffer, value_buffer);
97 asl_log(NULL, mAsl, ASL_LEVEL_NOTICE, "%s is %lld", key_buffer, value);
98 asl_free(mAsl);
99 return true;
100 }
101 #endif
102
103 #if (TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR)
104
105 typedef void (*type_ADClientClearScalarKey)(CFStringRef key);
106 typedef void (*type_ADClientAddValueForScalarKey)(CFStringRef key, int64_t value);
107
108 static type_ADClientClearScalarKey gADClientClearScalarKey = NULL;
109 static type_ADClientAddValueForScalarKey gADClientAddValueForScalarKey = NULL;
110
111 static dispatch_once_t gADFunctionPointersSet = 0;
112 static CFBundleRef gAggdBundleRef = NULL;
113 static bool gFunctionPointersAreLoaded = false;
114
115 static bool InitializeADFunctionPointers()
116 {
117 if (gFunctionPointersAreLoaded)
118 {
119 return gFunctionPointersAreLoaded;
120 }
121
122 dispatch_once(&gADFunctionPointersSet,
123 ^{
124 CFStringRef path_to_aggd_framework = CFSTR("/System/Library/PrivateFrameworks/AggregateDictionary.framework");
125
126 CFURLRef aggd_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path_to_aggd_framework, kCFURLPOSIXPathStyle, true);
127
128 if (NULL != aggd_url)
129 {
130 gAggdBundleRef = CFBundleCreate(kCFAllocatorDefault, aggd_url);
131 if (NULL != gAggdBundleRef)
132 {
133 gADClientClearScalarKey = (type_ADClientClearScalarKey)
134 CFBundleGetFunctionPointerForName(gAggdBundleRef, CFSTR("ADClientClearScalarKey"));
135
136 gADClientAddValueForScalarKey = (type_ADClientAddValueForScalarKey)
137 CFBundleGetFunctionPointerForName(gAggdBundleRef, CFSTR("ADClientAddValueForScalarKey"));
138 }
139 CFRelease(aggd_url);
140 }
141 });
142
143 gFunctionPointersAreLoaded = ((NULL != gADClientClearScalarKey) && (NULL != gADClientAddValueForScalarKey));
144 return gFunctionPointersAreLoaded;
145 }
146
147 static void Internal_ADClientClearScalarKey(CFStringRef key)
148 {
149 if (InitializeADFunctionPointers())
150 {
151 gADClientClearScalarKey(key);
152 }
153 }
154
155 static void Internal_ADClientAddValueForScalarKey(CFStringRef key, int64_t value)
156 {
157 if (InitializeADFunctionPointers())
158 {
159 gADClientAddValueForScalarKey(key, value);
160 }
161 }
162
163 static bool iOS_SetCloudKeychainTraceValueForKey(CFStringRef key, int64_t value)
164 {
165 if (NULL == key)
166 {
167 return false;
168 }
169
170 if (0LL == value)
171 {
172 Internal_ADClientClearScalarKey(key);
173 }
174 else
175 {
176 Internal_ADClientAddValueForScalarKey(key, value);
177 }
178 return true;
179 }
180 #endif
181
182 bool SetCloudKeychainTraceValueForKey(CFStringRef key, int64_t value)
183 {
184 #if (TARGET_IPHONE_SIMULATOR)
185 return false;
186 #endif
187
188 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
189 return OSX_SetCloudKeychainTraceValueForKey(key, value);
190 #endif
191
192 #if (TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR)
193 return iOS_SetCloudKeychainTraceValueForKey(key, value);
194 #endif
195 }