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@
27 #include <dispatch/dispatch.h>
30 #include <security_utilities/unix++.h>
31 #include <security_utilities/logging.h>
33 #include "SecTranslocateInterface.hpp"
34 #include "SecTranslocateXPCServer.hpp"
35 #include "SecTranslocateUtilities.hpp"
36 #include "SecTranslocateShared.hpp"
37 #include "SecTranslocateEnumUtils.hpp"
40 namespace SecTranslocate
{
42 static void doCreate(xpc_object_t msg
, xpc_object_t reply
)
44 const char* original
= xpc_dictionary_get_string(msg
, kSecTranslocateXPCMessageOriginalPath
);
45 const char* dest
= xpc_dictionary_get_string(msg
, kSecTranslocateXPCMessageDestinationPath
);
46 const int64_t opts
= xpc_dictionary_get_int64(msg
, kSecTranslocateXPCMessageOptions
);
48 string originalPath
= original
? original
: "";
49 string destPath
= dest
? dest
: "";
50 TranslocationOptions options
= static_cast<TranslocationOptions
>(opts
);
52 if (originalPath
.empty())
54 Syslog::error("SecTranslocate: XPCServer, doCreate no path to translocate");
55 UnixError::throwMe(EINVAL
);
58 string result
= originalPath
;
60 if ((options
& TranslocationOptions::Generic
) == TranslocationOptions::Generic
) {
61 GenericTranslocationPath
tPath(originalPath
, TranslocationOptions::Unveil
);
63 if(tPath
.shouldTranslocate())
65 result
= Security::SecTranslocate::translocatePathForUser(tPath
, destPath
);
68 TranslocationPath
tPath(originalPath
, TranslocationOptions::Default
);
70 if(tPath
.shouldTranslocate())
72 result
= Security::SecTranslocate::translocatePathForUser(tPath
, destPath
);
76 xpc_dictionary_set_string(reply
, kSecTranslocateXPCReplySecurePath
, result
.c_str());
79 static void doCheckIn(xpc_object_t msg
)
81 if (xpc_dictionary_get_value(msg
, kSecTranslocateXPCMessagePid
) == NULL
)
83 Syslog::error("SecTranslocate, XpcServer, doCheckin, no pid provided");
84 UnixError::throwMe(EINVAL
);
86 int64_t pid
= xpc_dictionary_get_int64(msg
, kSecTranslocateXPCMessagePid
);
87 Translocator
* t
= getTranslocator();
90 t
->appLaunchCheckin((pid_t
)pid
);
94 Syslog::critical("SecTranslocate, XpcServer, doCheckin, No top level translocator");
95 UnixError::throwMe(EINVAL
);
99 XPCServer::XPCServer(dispatch_queue_t q
):notificationQ(q
)
103 Syslog::critical("SecTranslocate: XPCServer, no dispatch queue provided");
104 UnixError::throwMe(EINVAL
);
106 //notificationQ is assumed to be serial
107 service
= xpc_connection_create_mach_service(SECTRANSLOCATE_XPC_SERVICE_NAME
,
109 XPC_CONNECTION_MACH_SERVICE_LISTENER
);
112 Syslog::critical("SecTranslocate: XPCServer, failed to create xpc mach service");
113 UnixError::throwMe(ENOMEM
);
116 dispatch_retain(notificationQ
);
117 xpc_connection_set_event_handler(service
, ^(xpc_object_t cmsg
) {
118 if (xpc_get_type(cmsg
) == XPC_TYPE_CONNECTION
) {
119 xpc_connection_t connection
= xpc_connection_t(cmsg
);
120 Syslog::debug("SecTranslocate: XPCServer, Connection from pid %d", xpc_connection_get_pid(connection
));
121 xpc_connection_set_event_handler(connection
, ^(xpc_object_t msg
) {
122 if (xpc_get_type(msg
) == XPC_TYPE_DICTIONARY
) {
124 dispatch_async(notificationQ
, ^{ // async from here
125 const char *function
= xpc_dictionary_get_string(msg
, kSecTranslocateXPCMessageFunction
);
126 Syslog::debug("SecTranslocate: XPCServer, pid %d requested %s", xpc_connection_get_pid(connection
), function
);
127 xpc_object_t reply
= xpc_dictionary_create_reply(msg
);
129 if (function
== NULL
) {
130 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, EINVAL
);
131 } else if (!strcmp(function
, kSecTranslocateXPCFuncCreate
)) {
132 doCreate(msg
, reply
);
133 } else if (!strcmp(function
, kSecTranslocateXPCFuncCheckIn
)) {
136 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, EINVAL
);
138 } catch (Security::UnixError err
) {
139 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, err
.unixError());
141 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, EINVAL
);
145 xpc_connection_send_message(connection
, reply
);
151 xpc_connection_resume(connection
);
153 const char *s
= xpc_copy_description(cmsg
);
154 Syslog::error("SecTranslocate: XPCServer, unepxected incoming message - %s", s
);
158 xpc_connection_resume(service
);
161 XPCServer::~XPCServer()
163 xpc_connection_cancel(service
);
164 dispatch_release(notificationQ
);
167 } //namespace Security
168 } //namespace SecTranslocate