]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 | 1 | /* |
d8f41ccd | 2 | * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved. |
b1ab9ed8 A |
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 | ||
5c19dc3a | 30 | #include <unordered_map> |
b1ab9ed8 A |
31 | |
32 | namespace Security { | |
33 | ||
34 | ||
35 | // | |
36 | // Inherit from this (abstract) class to implement your plugin | |
37 | // | |
38 | class CssmPlugin { | |
39 | NOCOPY(CssmPlugin) | |
40 | public: | |
41 | CssmPlugin(); | |
42 | virtual ~CssmPlugin(); | |
43 | ||
44 | void moduleLoad(const Guid &cssmGuid, | |
45 | const Guid &moduleGuid, | |
46 | const ModuleCallback &callback); | |
47 | void moduleUnload(const Guid &cssmGuid, | |
48 | const Guid &moduleGuid, | |
49 | const ModuleCallback &callback); | |
50 | ||
51 | void moduleAttach(CSSM_MODULE_HANDLE theHandle, | |
52 | const Guid &cssmGuid, | |
53 | const Guid &moduleGuid, | |
54 | const Guid &moduleManagerGuid, | |
55 | const Guid &callerGuid, | |
56 | const CSSM_VERSION &Version, | |
57 | uint32 SubserviceID, | |
58 | CSSM_SERVICE_TYPE SubServiceType, | |
59 | CSSM_ATTACH_FLAGS AttachFlags, | |
60 | CSSM_KEY_HIERARCHY KeyHierarchy, | |
61 | const CSSM_UPCALLS &Upcalls, | |
62 | CSSM_MODULE_FUNCS_PTR &FuncTbl); | |
63 | void moduleDetach(CSSM_MODULE_HANDLE handle); | |
64 | ||
65 | const Guid &myGuid() const { return mMyGuid; } | |
66 | ||
67 | void sendCallback(CSSM_MODULE_EVENT event, | |
68 | uint32 ssid, | |
69 | CSSM_SERVICE_TYPE serviceType) const; | |
70 | ||
71 | void sendInsertion(uint32 subId, CSSM_SERVICE_TYPE serviceType) const | |
72 | { sendCallback(CSSM_NOTIFY_INSERT, subId, serviceType); } | |
73 | ||
74 | void sendRemoval(uint32 subId, CSSM_SERVICE_TYPE serviceType) const | |
75 | { sendCallback(CSSM_NOTIFY_REMOVE, subId, serviceType); } | |
76 | ||
77 | void sendFault(uint32 subId, CSSM_SERVICE_TYPE serviceType) const | |
78 | { sendCallback(CSSM_NOTIFY_FAULT, subId, serviceType); } | |
79 | ||
80 | protected: | |
81 | // subclass-defined methods | |
82 | virtual void load(); | |
83 | virtual void unload(); | |
84 | ||
85 | // make a session object for your plugin | |
86 | virtual PluginSession *makeSession(CSSM_MODULE_HANDLE handle, | |
87 | const CSSM_VERSION &version, | |
88 | uint32 subserviceId, | |
89 | CSSM_SERVICE_TYPE subserviceType, | |
90 | CSSM_ATTACH_FLAGS attachFlags, | |
91 | const CSSM_UPCALLS &upcalls) = 0; | |
92 | ||
93 | private: | |
94 | // map of (CSSM) handles to attachment objects | |
95 | struct SessionMap : | |
5c19dc3a | 96 | public std::unordered_map<CSSM_MODULE_HANDLE, PluginSession *>, |
b1ab9ed8 A |
97 | public Mutex { }; |
98 | ||
99 | static ModuleNexus<SessionMap> sessionMap; | |
100 | ||
101 | Guid mMyGuid; | |
102 | ||
103 | // the registered callback. Set during load processing, unset during unload | |
104 | ModuleCallback mCallback; | |
105 | bool mLoaded; | |
106 | ||
107 | public: | |
108 | static PluginSession *find(CSSM_MODULE_HANDLE h) | |
109 | { | |
110 | StLock<Mutex> _(sessionMap()); | |
111 | SessionMap::iterator it = sessionMap().find(h); | |
112 | if (it == sessionMap().end()) | |
113 | CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_HANDLE); | |
114 | return it->second; | |
115 | } | |
116 | }; | |
117 | ||
118 | template <class SessionClass> | |
119 | inline SessionClass &findSession(CSSM_MODULE_HANDLE h) | |
120 | { | |
121 | SessionClass *session = dynamic_cast<SessionClass *>(CssmPlugin::find(h)); | |
122 | if (session == NULL) | |
123 | CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_HANDLE); | |
124 | assert(session->handle() == h); | |
125 | return *session; | |
126 | } | |
127 | ||
128 | } // end namespace Security | |
129 | ||
130 | #endif //_H_CSSMPLUGIN |