]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_translocate/lib/SecTranslocateXPCServer.cpp
Security-59306.101.1.tar.gz
[apple/security.git] / OSX / libsecurity_translocate / lib / SecTranslocateXPCServer.cpp
1 /*
2 * Copyright (c) 2016 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 #include <string>
25 #include <vector>
26
27 #include <dispatch/dispatch.h>
28 #include <xpc/xpc.h>
29
30 #include <security_utilities/unix++.h>
31 #include <security_utilities/logging.h>
32
33 #include "SecTranslocateInterface.hpp"
34 #include "SecTranslocateXPCServer.hpp"
35 #include "SecTranslocateUtilities.hpp"
36 #include "SecTranslocateShared.hpp"
37
38 namespace Security {
39 namespace SecTranslocate {
40
41 static void doCreate(xpc_object_t msg, xpc_object_t reply)
42 {
43 const char* original = xpc_dictionary_get_string(msg, kSecTranslocateXPCMessageOriginalPath);
44 const char* dest = xpc_dictionary_get_string(msg, kSecTranslocateXPCMessageDestinationPath);
45
46 string originalPath = original ? original : "";
47 string destPath = dest ? dest: "";
48
49 if( originalPath.empty())
50 {
51 Syslog::error("SecTranslocate: XPCServer, doCreate no path to translocate");
52 UnixError::throwMe(EINVAL);
53 }
54
55 TranslocationPath tPath(originalPath);
56
57 string result = tPath.getOriginalRealPath();
58
59 if(tPath.shouldTranslocate())
60 {
61 result = Security::SecTranslocate::translocatePathForUser(tPath, destPath);
62 }
63 xpc_dictionary_set_string(reply, kSecTranslocateXPCReplySecurePath, result.c_str());
64 }
65
66 static void doCheckIn(xpc_object_t msg)
67 {
68 if (xpc_dictionary_get_value(msg, kSecTranslocateXPCMessagePid) == NULL)
69 {
70 Syslog::error("SecTranslocate, XpcServer, doCheckin, no pid provided");
71 UnixError::throwMe(EINVAL);
72 }
73 int64_t pid = xpc_dictionary_get_int64(msg, kSecTranslocateXPCMessagePid);
74 Translocator * t = getTranslocator();
75 if(t)
76 {
77 t->appLaunchCheckin((pid_t)pid);
78 }
79 else
80 {
81 Syslog::critical("SecTranslocate, XpcServer, doCheckin, No top level translocator");
82 UnixError::throwMe(EINVAL);
83 }
84 }
85
86 XPCServer::XPCServer(dispatch_queue_t q):notificationQ(q)
87 {
88 if(q == NULL)
89 {
90 Syslog::critical("SecTranslocate: XPCServer, no dispatch queue provided");
91 UnixError::throwMe(EINVAL);
92 }
93 //notificationQ is assumed to be serial
94 service = xpc_connection_create_mach_service(SECTRANSLOCATE_XPC_SERVICE_NAME,
95 notificationQ,
96 XPC_CONNECTION_MACH_SERVICE_LISTENER);
97 if (service == NULL)
98 {
99 Syslog::critical("SecTranslocate: XPCServer, failed to create xpc mach service");
100 UnixError::throwMe(ENOMEM);
101 }
102
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) {
110 xpc_retain(msg);
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);
115 try {
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)) {
121 doCheckIn(msg);
122 } else {
123 xpc_dictionary_set_int64(reply, kSecTranslocateXPCReplyError, EINVAL);
124 }
125 } catch (Security::UnixError err) {
126 xpc_dictionary_set_int64(reply, kSecTranslocateXPCReplyError, err.unixError());
127 } catch (...) {
128 xpc_dictionary_set_int64(reply, kSecTranslocateXPCReplyError, EINVAL);
129 }
130 xpc_release(msg);
131 if (reply) {
132 xpc_connection_send_message(connection, reply);
133 xpc_release(reply);
134 }
135 });
136 }
137 });
138 xpc_connection_resume(connection);
139 } else {
140 const char *s = xpc_copy_description(cmsg);
141 Syslog::error("SecTranslocate: XPCServer, unepxected incoming message - %s", s);
142 free((char*)s);
143 }
144 });
145 xpc_connection_resume(service);
146 }
147
148 XPCServer::~XPCServer()
149 {
150 xpc_connection_cancel(service);
151 dispatch_release(notificationQ);
152 }
153
154 } //namespace Security
155 } //namespace SecTranslocate