]> git.saurik.com Git - apple/security.git/blob - libsecurity_keychain/lib/CCallbackMgr.cp
Security-55179.13.tar.gz
[apple/security.git] / libsecurity_keychain / lib / CCallbackMgr.cp
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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
25 /*
26 File: CCallbackMgr.cp
27
28 Contains: Code that communicates with processes that install a callback
29 with the Keychain Manager to receive keychain events.
30
31 */
32
33 #include "CCallbackMgr.h"
34
35 #include <algorithm>
36 #include <list>
37
38 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
39 #include "Globals.h"
40 #include <security_keychain/SecCFTypes.h>
41 #include <securityd_client/SharedMemoryCommon.h>
42 #include <securityd_client/ssnotify.h>
43 #include <notify.h>
44
45 using namespace KeychainCore;
46 using namespace CssmClient;
47 using namespace SecurityServer;
48
49 #pragma mark ÑÑÑÑ CallbackInfo ÑÑÑÑ
50
51 CallbackInfo::CallbackInfo() : mCallback(NULL),mEventMask(0),mContext(NULL)
52 {
53 }
54
55 CallbackInfo::CallbackInfo(SecKeychainCallback inCallbackFunction,
56 SecKeychainEventMask inEventMask, void *inContext)
57 : mCallback(inCallbackFunction), mEventMask(inEventMask), mContext(inContext)
58 {
59 }
60
61 CallbackInfo::~CallbackInfo()
62 {
63 }
64
65 bool CallbackInfo::operator==(const CallbackInfo& other) const
66 {
67 return mCallback==other.mCallback;
68 }
69
70 bool CallbackInfo::operator!=(const CallbackInfo& other) const
71 {
72 return !(*this==other);
73 }
74
75
76 #pragma mark ÑÑÑÑ CCallbackMgr ÑÑÑÑ
77
78
79 class CallbackMaker
80 {
81 protected:
82 RefPointer<CCallbackMgr> mCallbackManager;
83
84 public:
85 CallbackMaker();
86 CCallbackMgr& instance() {return *mCallbackManager;}
87 };
88
89
90 CallbackMaker::CallbackMaker()
91 {
92 CCallbackMgr* manager = new CCallbackMgr();
93 mCallbackManager = manager;
94 }
95
96
97
98 ModuleNexus<CallbackMaker> gCallbackMaker;
99
100 CCallbackMgr::CCallbackMgr() : EventListener (kNotificationDomainDatabase, kNotificationAllEvents)
101 {
102 EventListener::FinishedInitialization(this);
103 }
104
105 CCallbackMgr::~CCallbackMgr()
106 {
107 }
108
109 CCallbackMgr& CCallbackMgr::Instance()
110 {
111 return gCallbackMaker().instance();
112 }
113
114 void CCallbackMgr::AddCallback( SecKeychainCallback inCallbackFunction,
115 SecKeychainEventMask inEventMask,
116 void* inContext)
117
118 {
119 CallbackInfo info( inCallbackFunction, inEventMask, inContext );
120 CallbackInfo existingInfo;
121
122
123 CallbackInfoListIterator ix = find( CCallbackMgr::Instance().mEventCallbacks.begin(),
124 CCallbackMgr::Instance().mEventCallbacks.end(), info );
125
126 // make sure it is not already there
127 if ( ix!=CCallbackMgr::Instance().mEventCallbacks.end() )
128 {
129 // It's already there. This could mean that the old process died unexpectedly,
130 // so we need to validate the process ID of the existing callback.
131 // On Mac OS X this list is per process so this is always a duplicate
132 MacOSError::throwMe(errSecDuplicateCallback);
133 }
134
135 CCallbackMgr::Instance().mEventCallbacks.push_back(info);
136 }
137
138
139 class Predicate
140 {
141 SecKeychainCallback mCallbackFunction;
142 public:
143 Predicate(SecKeychainCallback inCallbackFunction) : mCallbackFunction(inCallbackFunction) {}
144 bool operator()(const CallbackInfo &cbInfo) { return cbInfo.mCallback == mCallbackFunction; }
145 };
146
147 void CCallbackMgr::RemoveCallback(SecKeychainCallback inCallbackFunction)
148 {
149 size_t oldSize = CCallbackMgr::Instance().mEventCallbacks.size();
150 Predicate predicate(inCallbackFunction);
151 CCallbackMgr::Instance().mEventCallbacks.remove_if(predicate);
152
153 if (oldSize == CCallbackMgr::Instance().mEventCallbacks.size())
154 MacOSError::throwMe(errSecInvalidCallback);
155 }
156
157 void CCallbackMgr::AlertClients(const list<CallbackInfo> &eventCallbacks,
158 SecKeychainEvent inEvent,
159 pid_t inPid,
160 const Keychain &inKeychain,
161 const Item &inItem)
162 {
163 secdebug("kcnotify", "dispatch event %ld pid %d keychain %p item %p",
164 inEvent, inPid, &inKeychain, !!inItem ? &*inItem : NULL);
165
166 // Iterate through callbacks, looking for those registered for inEvent
167 const SecKeychainEventMask theMask = 1U << inEvent;
168
169 for (ConstCallbackInfoListIterator ix = eventCallbacks.begin(); ix != eventCallbacks.end(); ++ix)
170 {
171 if (!(ix->mEventMask & theMask))
172 continue;
173
174 SecKeychainCallbackInfo cbInfo;
175 cbInfo.version = 0; // @@@ kKeychainAPIVersion;
176 cbInfo.item = inItem ? inItem->handle() : 0;
177 cbInfo.keychain = inKeychain ? inKeychain->handle() : 0;
178 cbInfo.pid = inPid;
179
180 ix->mCallback(inEvent, &cbInfo, ix->mContext);
181 if (cbInfo.item) CFRelease(cbInfo.item);
182 if (cbInfo.keychain) CFRelease(cbInfo.keychain);
183 }
184 }
185
186
187
188 void CCallbackMgr::consume (SecurityServer::NotificationDomain domain, SecurityServer::NotificationEvent whichEvent, const CssmData &data)
189 {
190 NameValueDictionary dictionary (data);
191
192 // Decode from userInfo the event type, 'keychain' CFDict, and 'item' CFDict
193 SecKeychainEvent thisEvent = whichEvent;
194
195 pid_t thisPid;
196 const NameValuePair* pidRef = dictionary.FindByName(PID_KEY);
197 if (pidRef == 0)
198 {
199 thisPid = 0;
200 }
201 else
202 {
203 thisPid = n2h(*reinterpret_cast<pid_t*>(pidRef->Value().data ()));
204 }
205
206 Keychain thisKeychain;
207 Item thisItem;
208 list<CallbackInfo> eventCallbacks;
209 {
210 // Lock the global API lock before doing stuff with StorageManager.
211 // make sure we have a database identifier
212 if (dictionary.FindByName (SSUID_KEY) != 0)
213 {
214 StLock<Mutex>_(*globals().storageManager.getStorageManagerMutex());
215 DLDbIdentifier dbid = NameValueDictionary::MakeDLDbIdentifierFromNameValueDictionary(dictionary);
216 thisKeychain = globals().storageManager.keychain(dbid);
217 }
218
219 const NameValuePair* item = dictionary.FindByName(ITEM_KEY);
220
221 if (item && thisKeychain)
222 {
223 PrimaryKey pk(item->Value());
224 thisItem = thisKeychain->item(pk);
225 }
226
227 // Deal with events that we care about ourselves first.
228 if (thisEvent == kSecDeleteEvent && thisKeychain.get() && thisItem.get())
229 thisKeychain->didDeleteItem(thisItem.get());
230 else if (thisEvent == kSecKeychainListChangedEvent)
231 globals().storageManager.forceUserSearchListReread();
232
233 eventCallbacks = CCallbackMgr::Instance().mEventCallbacks;
234 // We can safely release the global API lock now since thisKeychain and thisItem
235 // are CFRetained and will be until they go out of scope.
236 }
237
238 // Notify our process of this event.
239 CCallbackMgr::AlertClients(eventCallbacks, thisEvent, thisPid, thisKeychain, thisItem);
240 }