1/* 2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved. 3 * 4 * The contents of this file constitute Original Code as defined in and are 5 * subject to the Apple Public Source License Version 1.2 (the 'License'). 6 * You may not use this file except in compliance with the License. Please obtain 7 * a copy of the License at http://www.apple.com/publicsource and read it before 8 * using this file. 9 * 10 * This Original Code and all software distributed under the License are 11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS 12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT 13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the 15 * specific language governing rights and limitations under the License. 16 */ 17 18 19// 20// cssmplugin - common header for CSSM plugin modules 21// 22#ifndef _H_CSSMPLUGIN 23#define _H_CSSMPLUGIN 24 25#include <security_cdsa_plugin/c++plugin.h> 26#include <security_utilities/globalizer.h> 27#include <security_cdsa_utilities/callback.h> 28#include <set> 29 30#include <ext/hash_map> 31using __gnu_cxx::hash_map; 32 33 34namespace Security { 35 36 37// 38// Inherit from this (abstract) class to implement your plugin 39// 40class CssmPlugin { 41 NOCOPY(CssmPlugin) 42public: 43 CssmPlugin(); 44 virtual ~CssmPlugin(); 45 46 void moduleLoad(const Guid &cssmGuid, 47 const Guid &moduleGuid, 48 const ModuleCallback &callback); 49 void moduleUnload(const Guid &cssmGuid, 50 const Guid &moduleGuid, 51 const ModuleCallback &callback); 52 53 void moduleAttach(CSSM_MODULE_HANDLE theHandle, 54 const Guid &cssmGuid, 55 const Guid &moduleGuid, 56 const Guid &moduleManagerGuid, 57 const Guid &callerGuid, 58 const CSSM_VERSION &Version, 59 uint32 SubserviceID, 60 CSSM_SERVICE_TYPE SubServiceType, 61 CSSM_ATTACH_FLAGS AttachFlags, 62 CSSM_KEY_HIERARCHY KeyHierarchy, 63 const CSSM_UPCALLS &Upcalls, 64 CSSM_MODULE_FUNCS_PTR &FuncTbl); 65 void moduleDetach(CSSM_MODULE_HANDLE handle); 66 67 const Guid &myGuid() const { return mMyGuid; } 68 69 void sendCallback(CSSM_MODULE_EVENT event, 70 uint32 ssid, 71 CSSM_SERVICE_TYPE serviceType) const; 72 73 void sendInsertion(uint32 subId, CSSM_SERVICE_TYPE serviceType) const 74 { sendCallback(CSSM_NOTIFY_INSERT, subId, serviceType); } 75 76 void sendRemoval(uint32 subId, CSSM_SERVICE_TYPE serviceType) const 77 { sendCallback(CSSM_NOTIFY_REMOVE, subId, serviceType); } 78 79 void sendFault(uint32 subId, CSSM_SERVICE_TYPE serviceType) const 80 { sendCallback(CSSM_NOTIFY_FAULT, subId, serviceType); } 81 82protected: 83 // subclass-defined methods 84 virtual void load(); 85 virtual void unload(); 86 87 // make a session object for your plugin 88 virtual PluginSession *makeSession(CSSM_MODULE_HANDLE handle, 89 const CSSM_VERSION &version, 90 uint32 subserviceId, 91 CSSM_SERVICE_TYPE subserviceType, 92 CSSM_ATTACH_FLAGS attachFlags, 93 const CSSM_UPCALLS &upcalls) = 0; 94 95private: 96 // map of (CSSM) handles to attachment objects 97 struct SessionMap : 98 public hash_map<CSSM_MODULE_HANDLE, PluginSession *>, 99 public Mutex { }; 100 101 static ModuleNexus<SessionMap> sessionMap; 102 103 Guid mMyGuid; 104 105 // the registered callback. Set during load processing, unset during unload 106 ModuleCallback mCallback; 107 bool mLoaded; 108 109public: 110 static PluginSession *find(CSSM_MODULE_HANDLE h) 111 { 112 StLock<Mutex> _(sessionMap()); 113 SessionMap::iterator it = sessionMap().find(h); 114 if (it == sessionMap().end()) 115 CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_HANDLE); 116 return it->second; 117 } 118}; 119 120template <class SessionClass> 121inline SessionClass &findSession(CSSM_MODULE_HANDLE h) 122{ 123 SessionClass *session = dynamic_cast<SessionClass *>(CssmPlugin::find(h)); 124 if (session == NULL) 125 CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_HANDLE); 126 assert(session->handle() == h); 127 return *session; 128} 129 130} // end namespace Security 131 132#endif //_H_CSSMPLUGIN 133