]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_pluginlib/cssmplugin.h
Security-163.tar.gz
[apple/security.git] / cdsa / cdsa_pluginlib / cssmplugin.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, 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/c++plugin.h>
26 #include <Security/globalizer.h>
27 #include <Security/callback.h>
28 #include <set>
29
30 #if __GNUC__ > 2
31 #include <ext/hash_map>
32 using __gnu_cxx::hash_map;
33 #else
34 #include <hash_map>
35 #endif
36
37 namespace Security
38 {
39
40 //
41 // Inherit from this (abstract) class to implement your plugin
42 //
43 class CssmPlugin {
44 NOCOPY(CssmPlugin)
45 public:
46 CssmPlugin();
47 virtual ~CssmPlugin();
48
49 void moduleLoad(const Guid &cssmGuid,
50 const Guid &moduleGuid,
51 const ModuleCallback &callback);
52 void moduleUnload(const Guid &cssmGuid,
53 const Guid &moduleGuid,
54 const ModuleCallback &callback);
55
56 void moduleAttach(CSSM_MODULE_HANDLE theHandle,
57 const Guid &cssmGuid,
58 const Guid &moduleGuid,
59 const Guid &moduleManagerGuid,
60 const Guid &callerGuid,
61 const CSSM_VERSION &Version,
62 uint32 SubserviceID,
63 CSSM_SERVICE_TYPE SubServiceType,
64 CSSM_ATTACH_FLAGS AttachFlags,
65 CSSM_KEY_HIERARCHY KeyHierarchy,
66 const CSSM_UPCALLS &Upcalls,
67 CSSM_MODULE_FUNCS_PTR &FuncTbl);
68 void moduleDetach(CSSM_MODULE_HANDLE handle);
69
70 const Guid &myGuid() const { return mMyGuid; }
71
72 void sendCallback(CSSM_MODULE_EVENT event,
73 uint32 subId,
74 CSSM_SERVICE_TYPE serviceType) const;
75
76 void sendInsertion(uint32 subId, CSSM_SERVICE_TYPE serviceType) const
77 { sendCallback(CSSM_NOTIFY_INSERT, subId, serviceType); }
78
79 void sendRemoval(uint32 subId, CSSM_SERVICE_TYPE serviceType) const
80 { sendCallback(CSSM_NOTIFY_REMOVE, subId, serviceType); }
81
82 void sendFault(uint32 subId, CSSM_SERVICE_TYPE serviceType) const
83 { sendCallback(CSSM_NOTIFY_FAULT, subId, serviceType); }
84
85 protected:
86 // subclass-defined methods
87 virtual void load();
88 virtual void unload();
89
90 // make a session object for your plugin
91 virtual PluginSession *makeSession(CSSM_MODULE_HANDLE handle,
92 const CSSM_VERSION &version,
93 uint32 subserviceId,
94 CSSM_SERVICE_TYPE subserviceType,
95 CSSM_ATTACH_FLAGS attachFlags,
96 const CSSM_UPCALLS &upcalls) = 0;
97
98 private:
99 // map of (CSSM) handles to attachment objects
100 struct SessionMap :
101 public hash_map<CSSM_MODULE_HANDLE, PluginSession *>,
102 public Mutex { };
103
104 static ModuleNexus<SessionMap> sessionMap;
105
106 Guid mMyGuid;
107
108 // the registered callback. We currently allow only one
109 ModuleCallback callback;
110 bool haveCallback;
111
112 public:
113 static PluginSession *find(CSSM_MODULE_HANDLE h)
114 {
115 StLock<Mutex> _(sessionMap());
116 SessionMap::iterator it = sessionMap().find(h);
117 if (it == sessionMap().end())
118 CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_HANDLE);
119 return it->second;
120 }
121 };
122
123 template <class SessionClass>
124 inline SessionClass &findSession(CSSM_MODULE_HANDLE h)
125 {
126 SessionClass *session = dynamic_cast<SessionClass *>(CssmPlugin::find(h));
127 if (session == NULL)
128 CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_HANDLE);
129 assert(session->handle() == h);
130 return *session;
131 }
132
133 } // end namespace Security
134
135 #endif //_H_CSSMPLUGIN