]>
Commit | Line | Data |
---|---|---|
fa7225c8 A |
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" | |
d64be36e | 37 | #include "SecTranslocateEnumUtils.hpp" |
fa7225c8 A |
38 | |
39 | namespace Security { | |
40 | namespace SecTranslocate { | |
41 | ||
42 | static void doCreate(xpc_object_t msg, xpc_object_t reply) | |
43 | { | |
44 | const char* original = xpc_dictionary_get_string(msg, kSecTranslocateXPCMessageOriginalPath); | |
45 | const char* dest = xpc_dictionary_get_string(msg, kSecTranslocateXPCMessageDestinationPath); | |
d64be36e | 46 | const int64_t opts = xpc_dictionary_get_int64(msg, kSecTranslocateXPCMessageOptions); |
fa7225c8 A |
47 | |
48 | string originalPath = original ? original : ""; | |
49 | string destPath = dest ? dest: ""; | |
d64be36e | 50 | TranslocationOptions options = static_cast<TranslocationOptions>(opts); |
fa7225c8 | 51 | |
d64be36e | 52 | if (originalPath.empty()) |
fa7225c8 A |
53 | { |
54 | Syslog::error("SecTranslocate: XPCServer, doCreate no path to translocate"); | |
55 | UnixError::throwMe(EINVAL); | |
56 | } | |
57 | ||
d64be36e A |
58 | string result = originalPath; |
59 | ||
60 | if ((options & TranslocationOptions::Generic) == TranslocationOptions::Generic) { | |
61 | GenericTranslocationPath tPath(originalPath, TranslocationOptions::Unveil); | |
fa7225c8 | 62 | |
d64be36e A |
63 | if(tPath.shouldTranslocate()) |
64 | { | |
65 | result = Security::SecTranslocate::translocatePathForUser(tPath, destPath); | |
66 | } | |
67 | } else { | |
68 | TranslocationPath tPath(originalPath, TranslocationOptions::Default); | |
fa7225c8 | 69 | |
d64be36e A |
70 | if(tPath.shouldTranslocate()) |
71 | { | |
72 | result = Security::SecTranslocate::translocatePathForUser(tPath, destPath); | |
73 | } | |
fa7225c8 | 74 | } |
d64be36e | 75 | |
fa7225c8 A |
76 | xpc_dictionary_set_string(reply, kSecTranslocateXPCReplySecurePath, result.c_str()); |
77 | } | |
78 | ||
79 | static void doCheckIn(xpc_object_t msg) | |
80 | { | |
81 | if (xpc_dictionary_get_value(msg, kSecTranslocateXPCMessagePid) == NULL) | |
82 | { | |
83 | Syslog::error("SecTranslocate, XpcServer, doCheckin, no pid provided"); | |
84 | UnixError::throwMe(EINVAL); | |
85 | } | |
86 | int64_t pid = xpc_dictionary_get_int64(msg, kSecTranslocateXPCMessagePid); | |
87 | Translocator * t = getTranslocator(); | |
88 | if(t) | |
89 | { | |
90 | t->appLaunchCheckin((pid_t)pid); | |
91 | } | |
92 | else | |
93 | { | |
94 | Syslog::critical("SecTranslocate, XpcServer, doCheckin, No top level translocator"); | |
95 | UnixError::throwMe(EINVAL); | |
96 | } | |
97 | } | |
98 | ||
99 | XPCServer::XPCServer(dispatch_queue_t q):notificationQ(q) | |
100 | { | |
101 | if(q == NULL) | |
102 | { | |
103 | Syslog::critical("SecTranslocate: XPCServer, no dispatch queue provided"); | |
104 | UnixError::throwMe(EINVAL); | |
105 | } | |
106 | //notificationQ is assumed to be serial | |
107 | service = xpc_connection_create_mach_service(SECTRANSLOCATE_XPC_SERVICE_NAME, | |
108 | notificationQ, | |
109 | XPC_CONNECTION_MACH_SERVICE_LISTENER); | |
110 | if (service == NULL) | |
111 | { | |
112 | Syslog::critical("SecTranslocate: XPCServer, failed to create xpc mach service"); | |
113 | UnixError::throwMe(ENOMEM); | |
114 | } | |
115 | ||
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) { | |
123 | xpc_retain(msg); | |
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); | |
128 | try { | |
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)) { | |
134 | doCheckIn(msg); | |
135 | } else { | |
136 | xpc_dictionary_set_int64(reply, kSecTranslocateXPCReplyError, EINVAL); | |
137 | } | |
138 | } catch (Security::UnixError err) { | |
139 | xpc_dictionary_set_int64(reply, kSecTranslocateXPCReplyError, err.unixError()); | |
140 | } catch (...) { | |
141 | xpc_dictionary_set_int64(reply, kSecTranslocateXPCReplyError, EINVAL); | |
142 | } | |
143 | xpc_release(msg); | |
144 | if (reply) { | |
145 | xpc_connection_send_message(connection, reply); | |
146 | xpc_release(reply); | |
147 | } | |
148 | }); | |
149 | } | |
150 | }); | |
151 | xpc_connection_resume(connection); | |
152 | } else { | |
153 | const char *s = xpc_copy_description(cmsg); | |
154 | Syslog::error("SecTranslocate: XPCServer, unepxected incoming message - %s", s); | |
155 | free((char*)s); | |
156 | } | |
157 | }); | |
158 | xpc_connection_resume(service); | |
159 | } | |
160 | ||
161 | XPCServer::~XPCServer() | |
162 | { | |
163 | xpc_connection_cancel(service); | |
164 | dispatch_release(notificationQ); | |
165 | } | |
166 | ||
167 | } //namespace Security | |
168 | } //namespace SecTranslocate |