]> git.saurik.com Git - apple/security.git/blob - security-sysdiagnose/security-sysdiagnose.m
Security-57740.51.3.tar.gz
[apple/security.git] / security-sysdiagnose / security-sysdiagnose.m
1 /*
2 * Copyright (c) 2009-2010,2012-2015 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import <Foundation/Foundation.h>
25 #import <Security/Security.h>
26
27 #import <CKBridge/SOSCloudKeychainClient.h>
28
29 #import <dispatch/dispatch.h>
30
31 #import <utilities/debugging.h>
32 #import <utilities/SecCFWrappers.h>
33
34 #import <Security/SecureObjectSync/SOSInternal.h>
35
36 #include <Security/SecureObjectSync/SOSCloudCircle.h>
37
38 #include "secToolFileIO.h"
39 #include "accountCirclesViewsPrint.h"
40
41
42 #include <stdio.h>
43
44 @interface NSString (FileOutput)
45 - (void) writeTo: (FILE*) file;
46 - (void) writeToStdOut;
47 - (void) writeToStdErr;
48 @end
49
50 @implementation NSString (FileOutput)
51
52 - (void) writeTo: (FILE*) file {
53 CFStringPerformWithCString((__bridge CFStringRef) self, ^(const char *utf8String) { fputs(utf8String, file); });
54 }
55
56 - (void) writeToStdOut {
57 [self writeTo: stdout];
58 }
59 - (void) writeToStdErr {
60 [self writeTo: stderr];
61 }
62
63 @end
64
65 @interface NSData (Hexinization)
66
67 - (NSString*) asHexString;
68
69 @end
70
71 @implementation NSData (Hexinization)
72
73 - (NSString*) asHexString {
74 return (__bridge_transfer NSString*) CFDataCopyHexString((__bridge CFDataRef)self);
75 }
76
77 @end
78
79
80 static void
81 circle_sysdiagnose(void)
82 {
83 SOSLogSetOutputTo(NULL,NULL);
84 SOSCCDumpCircleInformation();
85 }
86
87 static void
88 engine_sysdiagnose(void)
89 {
90 [@"Engine state:\n" writeToStdOut];
91
92 CFErrorRef error = NULL;
93
94 if (!SOSCCForEachEngineStateAsString(&error, ^(CFStringRef oneStateString) {
95 [(__bridge NSString*) oneStateString writeToStdOut];
96 [@"\n" writeToStdOut];
97 })) {
98 [[NSString stringWithFormat: @"No engine state, got error: %@", error] writeToStdOut];
99 }
100 }
101
102 static void
103 homekit_sysdiagnose(void)
104 {
105 }
106
107 static void
108 unlock_sysdiagnose(void)
109 {
110 }
111
112 static void idsproxy_print_message(CFDictionaryRef messages)
113 {
114 NSDictionary<NSString*, NSDictionary*> *idsMessages = (__bridge NSDictionary *)messages;
115
116 printf("IDS messages in flight: %d\n", (int)[idsMessages count]);
117
118 [idsMessages enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull identifier, NSDictionary* _Nonnull messageDictionary, BOOL * _Nonnull stop) {
119 printf("message identifier: %s\n", [identifier cStringUsingEncoding:NSUTF8StringEncoding]);
120
121 NSDictionary *messageDataAndPeerID = [messageDictionary valueForKey:(__bridge NSString*)kIDSMessageToSendKey];
122 [messageDataAndPeerID enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull peerID, NSData* _Nonnull messageData, BOOL * _Nonnull stop1) {
123 if(messageData)
124 printf("size of message to recipient: %lu\n", (unsigned long)[messageData length]);
125 }];
126
127 NSString *deviceID = [messageDictionary valueForKey:(__bridge NSString*)kIDSMessageRecipientDeviceID];
128 if(deviceID)
129 printf("recipient device id: %s\n", [deviceID cStringUsingEncoding:NSUTF8StringEncoding]);
130
131 }];
132 }
133
134 static void
135 idsproxy_sysdiagnose(void)
136 {
137
138 dispatch_semaphore_t wait_for = dispatch_semaphore_create(0);
139 __block CFDictionaryRef returned = NULL;
140
141 dispatch_queue_t processQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
142 SOSCloudKeychainRetrievePendingMessageFromProxy(processQueue, ^(CFDictionaryRef returnedValues, CFErrorRef error) {
143 secdebug("SOSCloudKeychainRetrievePendingMessageFromProxy", "returned: %@", returnedValues);
144 CFRetainAssign(returned, returnedValues);
145 dispatch_semaphore_signal(wait_for);
146 });
147
148 dispatch_semaphore_wait(wait_for, dispatch_time(DISPATCH_TIME_NOW, 2ull * NSEC_PER_SEC));
149 secdebug("idsproxy sysdiagnose", "messages: %@", returned);
150
151 idsproxy_print_message(returned);
152 }
153
154 static void
155 kvs_sysdiagnose(void) {
156 SOSLogSetOutputTo(NULL,NULL);
157 SOSCCDumpCircleKVSInformation(NULL);
158 }
159
160
161 int
162 main(int argc, const char ** argv)
163 {
164 @autoreleasepool {
165 printf("sysdiagnose keychain\n");
166
167 circle_sysdiagnose();
168 engine_sysdiagnose();
169 homekit_sysdiagnose();
170 unlock_sysdiagnose();
171 idsproxy_sysdiagnose();
172 // Keep this one last
173 kvs_sysdiagnose();
174 }
175 return 0;
176 }