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"
39 namespace SecTranslocate
{
41 static void doCreate(xpc_object_t msg
, xpc_object_t reply
)
43 const char* original
= xpc_dictionary_get_string(msg
, kSecTranslocateXPCMessageOriginalPath
);
44 const char* dest
= xpc_dictionary_get_string(msg
, kSecTranslocateXPCMessageDestinationPath
);
46 string originalPath
= original
? original
: "";
47 string destPath
= dest
? dest
: "";
49 if( originalPath
.empty())
51 Syslog::error("SecTranslocate: XPCServer, doCreate no path to translocate");
52 UnixError::throwMe(EINVAL
);
55 TranslocationPath
tPath(originalPath
);
57 string result
= tPath
.getOriginalRealPath();
59 if(tPath
.shouldTranslocate())
61 result
= Security::SecTranslocate::translocatePathForUser(tPath
, destPath
);
63 xpc_dictionary_set_string(reply
, kSecTranslocateXPCReplySecurePath
, result
.c_str());
66 static void doCheckIn(xpc_object_t msg
)
68 if (xpc_dictionary_get_value(msg
, kSecTranslocateXPCMessagePid
) == NULL
)
70 Syslog::error("SecTranslocate, XpcServer, doCheckin, no pid provided");
71 UnixError::throwMe(EINVAL
);
73 int64_t pid
= xpc_dictionary_get_int64(msg
, kSecTranslocateXPCMessagePid
);
74 Translocator
* t
= getTranslocator();
77 t
->appLaunchCheckin((pid_t
)pid
);
81 Syslog::critical("SecTranslocate, XpcServer, doCheckin, No top level translocator");
82 UnixError::throwMe(EINVAL
);
86 XPCServer::XPCServer(dispatch_queue_t q
):notificationQ(q
)
90 Syslog::critical("SecTranslocate: XPCServer, no dispatch queue provided");
91 UnixError::throwMe(EINVAL
);
93 //notificationQ is assumed to be serial
94 service
= xpc_connection_create_mach_service(SECTRANSLOCATE_XPC_SERVICE_NAME
,
96 XPC_CONNECTION_MACH_SERVICE_LISTENER
);
99 Syslog::critical("SecTranslocate: XPCServer, failed to create xpc mach service");
100 UnixError::throwMe(ENOMEM
);
103 dispatch_retain(notificationQ
);
104 xpc_connection_set_event_handler(service
, ^(xpc_object_t cmsg
) {
105 if (xpc_get_type(cmsg
) == XPC_TYPE_CONNECTION
) {
106 xpc_connection_t connection
= xpc_connection_t(cmsg
);
107 Syslog::debug("SecTranslocate: XPCServer, Connection from pid %d", xpc_connection_get_pid(connection
));
108 xpc_connection_set_event_handler(connection
, ^(xpc_object_t msg
) {
109 if (xpc_get_type(msg
) == XPC_TYPE_DICTIONARY
) {
111 dispatch_async(notificationQ
, ^{ // async from here
112 const char *function
= xpc_dictionary_get_string(msg
, kSecTranslocateXPCMessageFunction
);
113 Syslog::debug("SecTranslocate: XPCServer, pid %d requested %s", xpc_connection_get_pid(connection
), function
);
114 xpc_object_t reply
= xpc_dictionary_create_reply(msg
);
116 if (function
== NULL
) {
117 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, EINVAL
);
118 } else if (!strcmp(function
, kSecTranslocateXPCFuncCreate
)) {
119 doCreate(msg
, reply
);
120 } else if (!strcmp(function
, kSecTranslocateXPCFuncCheckIn
)) {
123 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, EINVAL
);
125 } catch (Security::UnixError err
) {
126 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, err
.unixError());
128 xpc_dictionary_set_int64(reply
, kSecTranslocateXPCReplyError
, EINVAL
);
132 xpc_connection_send_message(connection
, reply
);
138 xpc_connection_resume(connection
);
140 const char *s
= xpc_copy_description(cmsg
);
141 Syslog::error("SecTranslocate: XPCServer, unepxected incoming message - %s", s
);
145 xpc_connection_resume(service
);
148 XPCServer::~XPCServer()
150 xpc_connection_cancel(service
);
151 dispatch_release(notificationQ
);
154 } //namespace Security
155 } //namespace SecTranslocate