2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <dispatch/dispatch.h>
30 #include <security_utilities/unix++.h>
31 #include <security_utilities/logging.h>
33 #include "SecTranslocateClient.hpp"
34 #include "SecTranslocateShared.hpp"
35 #include "SecTranslocateInterface.hpp"
39 namespace SecTranslocate
{
43 TranslocatorClient::TranslocatorClient(dispatch_queue_t q
):syncQ(q
)
47 Syslog::critical("SecTranslocate::TranslocatorClient initialized without a queue.");
48 UnixError::throwMe(EINVAL
);
52 uid_t euid
= geteuid();
54 /* 0 - is root so it gets the root lsd
55 1-300 = are treated by launch services as "role users" They share a copy of the LS Database with root
56 and thus must be sent to the root lsd. */
59 flags
|= XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
; //forces call to the root lsd
62 service
= xpc_connection_create_mach_service(SECTRANSLOCATE_XPC_SERVICE_NAME
,
67 Syslog::critical("SecTranslocate: TranslocatorClient, failed to create xpc mach service");
68 UnixError::throwMe(ENOMEM
);
70 xpc_connection_set_event_handler(service
, ^(xpc_object_t event
) {
71 xpc_type_t type
= xpc_get_type(event
);
72 if (type
== XPC_TYPE_ERROR
)
74 Syslog::error("SecTranslocate, client, xpc error: %s", xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
78 char* description
= xpc_copy_description(event
);
79 Syslog::error("SecTranslocate, client, xpc unexpected type: %s", description
);
84 dispatch_retain(syncQ
);
85 xpc_connection_resume(service
);
88 TranslocatorClient::~TranslocatorClient()
90 xpc_connection_cancel(service
);
91 dispatch_release(syncQ
);
94 string
TranslocatorClient::translocatePathForUser(const TranslocationPath
&originalPath
, const string
&destPath
)
98 if (!originalPath
.shouldTranslocate())
100 return originalPath
.getOriginalRealPath(); //return original path if we shouldn't translocate
103 //We should run translocated, so get a translocation point
104 xpc_object_t msg
= xpc_dictionary_create(NULL
, NULL
, 0);
108 Syslog::error("SecTranslocate: TranslocatorClient, failed to allocate message to send");
109 UnixError::throwMe(ENOMEM
);
112 xpc_dictionary_set_string(msg
, kSecTranslocateXPCMessageFunction
, kSecTranslocateXPCFuncCreate
);
113 /* send the original real path rather than the calculated path to let the server do all the work */
114 xpc_dictionary_set_string(msg
, kSecTranslocateXPCMessageOriginalPath
, originalPath
.getOriginalRealPath().c_str());
115 if(!destPath
.empty())
117 xpc_dictionary_set_string(msg
, kSecTranslocateXPCMessageDestinationPath
, destPath
.c_str());
120 xpc_object_t reply
= xpc_connection_send_message_with_reply_sync(service
, msg
);
125 Syslog::error("SecTranslocate, TranslocatorClient, create, no reply returned");
126 UnixError::throwMe(ENOMEM
);
129 xpc_type_t type
= xpc_get_type(reply
);
130 if (type
== XPC_TYPE_DICTIONARY
)
132 if(int64_t error
= xpc_dictionary_get_int64(reply
, kSecTranslocateXPCReplyError
))
134 Syslog::error("SecTranslocate, TranslocatorClient, create, error received %lld", error
);
136 UnixError::throwMe((int)error
);
138 const char * result
= xpc_dictionary_get_string(reply
, kSecTranslocateXPCReplySecurePath
);
141 Syslog::error("SecTranslocate, TranslocatorClient, create, no result path received");
143 UnixError::throwMe(EINVAL
);
150 const char* errorMsg
= NULL
;
151 if (type
== XPC_TYPE_ERROR
)
153 errorMsg
= "SecTranslocate, TranslocatorClient, create, xpc error returned: %s";
157 errorMsg
= "SecTranslocate, TranslocatorClient, create, unexpected type of return object: %s";
159 const char *s
= xpc_copy_description(reply
);
160 Syslog::error(errorMsg
, s
);
163 UnixError::throwMe(EINVAL
);
169 void TranslocatorClient::appLaunchCheckin(pid_t pid
)
171 xpc_object_t msg
= xpc_dictionary_create(NULL
, NULL
, 0);
173 xpc_dictionary_set_string(msg
, kSecTranslocateXPCMessageFunction
, kSecTranslocateXPCFuncCheckIn
);
174 xpc_dictionary_set_int64(msg
, kSecTranslocateXPCMessagePid
, pid
);
176 /* no reply expected so just send the message and move along */
177 xpc_connection_send_message(service
, msg
);
182 bool TranslocatorClient::destroyTranslocatedPathForUser(const string
&translocatedPath
)
184 Syslog::error("SecTranslocate, TranslocatorClient, delete operation not allowed");
185 UnixError::throwMe(EPERM
);
189 } //namespace SecTranslocate
190 } //namespace Security