]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_translocate/lib/SecTranslocateServer.cpp
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / libsecurity_translocate / lib / SecTranslocateServer.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 #include <exception>
27 #include <dispatch/dispatch.h>
28
29 #include "SecTranslocateShared.hpp"
30 #include "SecTranslocateServer.hpp"
31 #include "SecTranslocateUtilities.hpp"
32 #include "SecTranslocateDANotification.hpp"
33 #include "SecTranslocateXPCServer.hpp"
34 #include "SecTranslocateLSNotification.hpp"
35 #undef check //The CoreServices code pulls in a check macro that we don't want
36
37 #include <security_utilities/unix++.h>
38 #include <security_utilities/logging.h>
39
40 namespace Security {
41
42 using namespace Security::UnixPlusPlus;
43
44 namespace SecTranslocate {
45
46 using namespace std;
47
48 /* Try to cleanup every 12 hrs */
49 #define TRANSLOCATION_CLEANUP_INTERVAL 12ULL * 60ULL * 60ULL * NSEC_PER_SEC
50 #define TRANSLOCATION_CLEANUP_LEEWAY TRANSLOCATION_CLEANUP_INTERVAL/2ULL
51
52 /* Initialize a dispatch queue to serialize operations */
53 TranslocatorServer::TranslocatorServer(dispatch_queue_t q):syncQ(q), da(q), ls(q),xpc(q)
54 {
55 if (!q)
56 {
57 Syslog::critical("SecTranslocate: TranslocatorServer failed to create the dispatch queue");
58 UnixError::throwMe(ENOMEM);
59 }
60 dispatch_retain(syncQ);
61
62 setupPeriodicCleanup();
63
64 Syslog::warning("SecTranslocate: Server started");
65 }
66
67 /* Destroy the dispatch queue and listeners when they are no longer needed */
68 TranslocatorServer::~TranslocatorServer()
69 {
70 if( syncQ )
71 {
72 dispatch_release(syncQ);
73 }
74
75 if(cleanupTimer)
76 {
77 dispatch_source_cancel(cleanupTimer);
78 cleanupTimer = NULL;
79 }
80 }
81
82 // This is intended for use by the host process of the server if necessary
83 // Create a translocation for original path if appropriate
84 string TranslocatorServer::translocatePathForUser(const TranslocationPath &originalPath, const string &destPath)
85 {
86 __block string newPath;
87 __block exception_ptr exception(0);
88
89 dispatch_sync(syncQ, ^{
90 try
91 {
92 newPath = Security::SecTranslocate::translocatePathForUser(originalPath,destPath);
93 }
94 catch (...)
95 {
96 exception = current_exception();
97 }
98 });
99 if (exception)
100 {
101 rethrow_exception(exception);
102 }
103 return newPath;
104 }
105
106 // This is intended for use by the host process of the server if necessary
107 // Destroy the translocation mount at translocatedPath if allowed
108 bool TranslocatorServer::destroyTranslocatedPathForUser(const string &translocatedPath)
109 {
110 __block bool result = false;
111 __block exception_ptr exception(0);
112 dispatch_sync(syncQ, ^{
113 try
114 {
115 result = Security::SecTranslocate::destroyTranslocatedPathForUser(translocatedPath);
116 }
117 catch (...)
118 {
119 exception = current_exception();
120 }
121 });
122 if (exception)
123 {
124 rethrow_exception(exception);
125 }
126 return result;
127 }
128
129 void TranslocatorServer::appLaunchCheckin(pid_t pid)
130 {
131 //This is thrown on the queue as an async task in the call so don't need to do anything extra.
132 ls.checkIn(pid);
133 }
134
135 void TranslocatorServer::setupPeriodicCleanup()
136 {
137 cleanupTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, syncQ);
138
139 dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, TRANSLOCATION_CLEANUP_INTERVAL);
140 dispatch_source_set_timer(cleanupTimer, when, TRANSLOCATION_CLEANUP_INTERVAL, TRANSLOCATION_CLEANUP_LEEWAY);
141
142 dispatch_source_set_cancel_handler(cleanupTimer, ^{
143 dispatch_release(cleanupTimer);
144 });
145
146 dispatch_source_set_event_handler(cleanupTimer, ^{
147 try
148 {
149 Syslog::notice("SecTranslocate: attempting to cleanup unused translocation points");
150 tryToDestroyUnusedTranslocationMounts();
151 }
152 catch (Security::UnixError err)
153 {
154 int error = err.unixError();
155 Syslog::error("SecTranslocate: got unix error[ %d : %s ] while trying to cleanup translocation points.",error, strerror(error));
156 }
157 catch (...)
158 {
159 Syslog::error("SecTranslocate: unknown error while trying to cleanup translocation points.");
160 }
161 });
162
163 dispatch_resume(cleanupTimer);
164 }
165
166 } //namespace SecTranslocate
167 } //namespace SecTranslocate