1 #import <Foundation/Foundation.h>
2 #import <Foundation/NSXPCConnection_Private.h>
3 #import <Security/SecEntitlements.h>
4 #import <ipc/securityd_client.h>
6 #import "SOSControlHelper.h"
7 #import "SOSControlServer.h"
9 @interface SOSControlServer : NSObject <NSXPCListenerDelegate>
12 @interface SOSClient : NSObject <SOSControlProtocol>
13 @property (weak) NSXPCConnection * connection;
14 @property (strong) SOSAccount * account;
16 - (instancetype)initWithConnection:(NSXPCConnection *)connection account:(SOSAccount *)account;
19 @implementation SOSControlServer
21 - (BOOL)listener:(__unused NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
23 NSNumber *num = [newConnection valueForEntitlement:(__bridge NSString *)kSecEntitlementKeychainCloudCircle];
24 if (![num isKindOfClass:[NSNumber class]] || ![num boolValue]) {
25 secerror("sos: Client pid: %d doesn't have entitlement: %@",
26 [newConnection processIdentifier], kSecEntitlementKeychainCloudCircle);
31 SOSClient *sosClient = [[SOSClient alloc] initWithConnection:newConnection account:(__bridge SOSAccount *)SOSKeychainAccountGetSharedAccount()];
33 newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SOSControlProtocol)];
34 _SOSControlSetupInterface(newConnection.exportedInterface);
35 newConnection.exportedObject = sosClient;
37 [newConnection resume];
44 @implementation SOSClient
46 @synthesize account = _account;
47 @synthesize connection = _connection;
49 - (instancetype)initWithConnection:(NSXPCConnection *)connection account:(SOSAccount *)account
51 if ((self = [super init])) {
52 _connection = connection;
58 - (bool)checkEntitlement:(NSString *)entitlement
60 NSXPCConnection *strongConnection = _connection;
62 NSNumber *num = [strongConnection valueForEntitlement:entitlement];
63 if (![num isKindOfClass:[NSNumber class]] || ![num boolValue]) {
64 secerror("sos: Client pid: %d doesn't have entitlement: %@",
65 [strongConnection processIdentifier], entitlement);
71 - (void)userPublicKey:(void ((^))(BOOL trusted, NSData *spki, NSError *error))reply
73 [self.account userPublicKey:reply];
76 - (void)kvsPerformanceCounters:(void(^)(NSDictionary <NSString *, NSNumber *> *))reply
78 [self.account kvsPerformanceCounters:reply];
81 - (void)rateLimitingPerformanceCounters:(void(^)(NSDictionary <NSString *, NSString *> *))reply
83 [self.account rateLimitingPerformanceCounters:reply];
86 - (void)stashedCredentialPublicKey:(void(^)(NSData *, NSError *error))reply
88 [self.account stashedCredentialPublicKey:reply];
91 - (void)assertStashedAccountCredential:(void(^)(BOOL result, NSError *error))reply
93 [self.account assertStashedAccountCredential:reply];
96 - (void)validatedStashedAccountCredential:(void(^)(NSData *credential, NSError *error))complete
98 [self.account validatedStashedAccountCredential:complete];
101 - (void)stashAccountCredential:(NSData *)credential complete:(void(^)(bool success, NSError *error))complete
103 [self.account stashAccountCredential:credential complete:complete];
106 - (void)myPeerInfo:(void (^)(NSData *, NSError *))complete
108 [self.account myPeerInfo:complete];
111 - (void)circleJoiningBlob:(NSData *)applicant complete:(void (^)(NSData *blob, NSError *))complete
113 [self.account circleJoiningBlob:applicant complete:complete];
116 - (void)joinCircleWithBlob:(NSData *)blob version:(PiggyBackProtocolVersion)version complete:(void (^)(bool success, NSError *))complete
118 [self.account joinCircleWithBlob:blob version:version complete:complete];
121 - (void)initialSyncCredentials:(uint32_t)flags complete:(void (^)(NSArray *, NSError *))complete
123 if (![self checkEntitlement:(__bridge NSString *)kSecEntitlementKeychainInitialSync]) {
124 complete(@[], [NSError errorWithDomain:(__bridge NSString *)kSOSErrorDomain code:kSOSEntitlementMissing userInfo:NULL]);
128 [self.account initialSyncCredentials:flags complete:complete];
131 - (void)importInitialSyncCredentials:(NSArray *)items complete:(void (^)(bool success, NSError *))complete
133 if (![self checkEntitlement:(__bridge NSString *)kSecEntitlementKeychainInitialSync]) {
134 complete(false, [NSError errorWithDomain:(__bridge NSString *)kSOSErrorDomain code:kSOSEntitlementMissing userInfo:NULL]);
138 [self.account importInitialSyncCredentials:items complete:complete];
141 - (void)triggerSync:(NSArray <NSString *> *)peers complete:(void(^)(bool success, NSError *))complete
143 if (![self checkEntitlement:(__bridge NSString *)kSecEntitlementKeychainCloudCircle]) {
144 complete(false, [NSError errorWithDomain:(__bridge NSString *)kSOSErrorDomain code:kSOSEntitlementMissing userInfo:NULL]);
148 [self.account triggerSync:peers complete:complete];
151 - (void)getWatchdogParameters:(void (^)(NSDictionary* parameters, NSError* error))complete
153 [self.account getWatchdogParameters:complete];
156 - (void)setWatchdogParmeters:(NSDictionary*)parameters complete:(void (^)(NSError* error))complete
158 [self.account setWatchdogParmeters:parameters complete:complete];
164 SOSControlServerInitialize(void)
166 static dispatch_once_t once;
167 static SOSControlServer *server;
168 static NSXPCListener *listener;
170 dispatch_once(&once, ^{
172 server = [SOSControlServer new];
174 listener = [[NSXPCListener alloc] initWithMachServiceName:@(kSecuritydSOSServiceName)];
175 listener.delegate = server;