2 * Copyright (c) 2000-2004,2011-2014 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@
28 Contains: Code that communicates with processes that install a callback
29 with the Keychain Manager to receive keychain events.
33 #include "CCallbackMgr.h"
39 #include <security_keychain/SecCFTypes.h>
40 #include <securityd_client/SharedMemoryCommon.h>
41 #include <securityd_client/ssnotify.h>
44 using namespace KeychainCore;
45 using namespace CssmClient;
46 using namespace SecurityServer;
48 #pragma mark ÑÑÑÑ CallbackInfo ÑÑÑÑ
50 CallbackInfo::CallbackInfo() : mCallback(NULL),mEventMask(0),mContext(NULL)
54 CallbackInfo::CallbackInfo(SecKeychainCallback inCallbackFunction,
55 SecKeychainEventMask inEventMask, void *inContext)
56 : mCallback(inCallbackFunction), mEventMask(inEventMask), mContext(inContext)
60 CallbackInfo::~CallbackInfo()
64 bool CallbackInfo::operator==(const CallbackInfo& other) const
66 return mCallback==other.mCallback;
69 bool CallbackInfo::operator!=(const CallbackInfo& other) const
71 return !(*this==other);
75 #pragma mark ÑÑÑÑ CCallbackMgr ÑÑÑÑ
81 RefPointer<CCallbackMgr> mCallbackManager;
85 CCallbackMgr& instance() {return *mCallbackManager;}
89 CallbackMaker::CallbackMaker()
91 CCallbackMgr* manager = new CCallbackMgr();
92 mCallbackManager = manager;
97 ModuleNexus<CallbackMaker> gCallbackMaker;
99 CCallbackMgr::CCallbackMgr() : EventListener (kNotificationDomainDatabase, kNotificationAllEvents)
101 EventListener::FinishedInitialization(this);
104 CCallbackMgr::~CCallbackMgr()
108 CCallbackMgr& CCallbackMgr::Instance()
110 return gCallbackMaker().instance();
113 void CCallbackMgr::AddCallback( SecKeychainCallback inCallbackFunction,
114 SecKeychainEventMask inEventMask,
118 CallbackInfo info( inCallbackFunction, inEventMask, inContext );
119 CallbackInfo existingInfo;
122 CallbackInfoListIterator ix = find( CCallbackMgr::Instance().mEventCallbacks.begin(),
123 CCallbackMgr::Instance().mEventCallbacks.end(), info );
125 // make sure it is not already there
126 if ( ix!=CCallbackMgr::Instance().mEventCallbacks.end() )
128 // It's already there. This could mean that the old process died unexpectedly,
129 // so we need to validate the process ID of the existing callback.
130 // On Mac OS X this list is per process so this is always a duplicate
131 MacOSError::throwMe(errSecDuplicateCallback);
134 CCallbackMgr::Instance().mEventCallbacks.push_back(info);
140 SecKeychainCallback mCallbackFunction;
142 Predicate(SecKeychainCallback inCallbackFunction) : mCallbackFunction(inCallbackFunction) {}
143 bool operator()(const CallbackInfo &cbInfo) { return cbInfo.mCallback == mCallbackFunction; }
146 void CCallbackMgr::RemoveCallback(SecKeychainCallback inCallbackFunction)
148 size_t oldSize = CCallbackMgr::Instance().mEventCallbacks.size();
149 Predicate predicate(inCallbackFunction);
150 CCallbackMgr::Instance().mEventCallbacks.remove_if(predicate);
152 if (oldSize == CCallbackMgr::Instance().mEventCallbacks.size())
153 MacOSError::throwMe(errSecInvalidCallback);
156 void CCallbackMgr::AlertClients(const list<CallbackInfo> &eventCallbacks,
157 SecKeychainEvent inEvent,
159 const Keychain &inKeychain,
162 secdebug("kcnotify", "dispatch event %ld pid %d keychain %p item %p",
163 (unsigned long)inEvent, inPid, &inKeychain, !!inItem ? &*inItem : NULL);
165 // Iterate through callbacks, looking for those registered for inEvent
166 const SecKeychainEventMask theMask = 1U << inEvent;
168 for (ConstCallbackInfoListIterator ix = eventCallbacks.begin(); ix != eventCallbacks.end(); ++ix)
170 if (!(ix->mEventMask & theMask))
173 SecKeychainCallbackInfo cbInfo;
174 cbInfo.version = 0; // @@@ kKeychainAPIVersion;
175 cbInfo.item = inItem ? inItem->handle() : 0;
176 cbInfo.keychain = inKeychain ? inKeychain->handle() : 0;
179 ix->mCallback(inEvent, &cbInfo, ix->mContext);
180 if (cbInfo.item) CFRelease(cbInfo.item);
181 if (cbInfo.keychain) CFRelease(cbInfo.keychain);
187 void CCallbackMgr::consume (SecurityServer::NotificationDomain domain, SecurityServer::NotificationEvent whichEvent, const CssmData &data)
189 NameValueDictionary dictionary (data);
191 // Decode from userInfo the event type, 'keychain' CFDict, and 'item' CFDict
192 SecKeychainEvent thisEvent = whichEvent;
195 const NameValuePair* pidRef = dictionary.FindByName(PID_KEY);
202 thisPid = n2h(*reinterpret_cast<pid_t*>(pidRef->Value().data ()));
205 Keychain thisKeychain;
207 list<CallbackInfo> eventCallbacks;
209 // Lock the global API lock before doing stuff with StorageManager.
210 // make sure we have a database identifier
211 if (dictionary.FindByName (SSUID_KEY) != 0)
213 StLock<Mutex>_(*globals().storageManager.getStorageManagerMutex());
214 DLDbIdentifier dbid = NameValueDictionary::MakeDLDbIdentifierFromNameValueDictionary(dictionary);
215 thisKeychain = globals().storageManager.keychain(dbid);
218 const NameValuePair* item = dictionary.FindByName(ITEM_KEY);
220 if (item && thisKeychain)
222 PrimaryKey pk(item->Value());
223 thisItem = thisKeychain->item(pk);
226 // Deal with events that we care about ourselves first.
227 if (thisEvent == kSecDeleteEvent && thisKeychain.get() && thisItem.get())
228 thisKeychain->didDeleteItem(thisItem.get());
229 else if (thisEvent == kSecKeychainListChangedEvent)
230 globals().storageManager.forceUserSearchListReread();
232 eventCallbacks = CCallbackMgr::Instance().mEventCallbacks;
233 // We can safely release the global API lock now since thisKeychain and thisItem
234 // are CFRetained and will be until they go out of scope.
237 // Notify our process of this event.
238 CCallbackMgr::AlertClients(eventCallbacks, thisEvent, thisPid, thisKeychain, thisItem);