2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 #define _DATABASE_H_ 1
22 #include <Security/cssmacl.h>
23 #include <Security/threading.h>
24 #include <Security/DbName.h>
33 // @@@ Should not use using in headers.
40 class DatabaseFactory
;
41 class DatabaseSession
;
44 /* DatabaseManager class. */
47 NOCOPY(DatabaseManager
)
50 virtual ~DatabaseManager ();
52 // Create and return a new DbContext instance which is owned by us and must be discared by calling dbClose.
53 virtual DbContext
&dbOpen(DatabaseSession
&inDatabaseSession
,
54 const DbName
&inDbName
,
55 CSSM_DB_ACCESS_TYPE inAccessRequest
,
56 const AccessCredentials
*inAccessCred
,
57 const void *inOpenParameters
);
58 virtual DbContext
&dbCreate(DatabaseSession
&inDatabaseSession
,
59 const DbName
&inDbName
,
60 const CSSM_DBINFO
&inDBInfo
,
61 CSSM_DB_ACCESS_TYPE inAccessRequest
,
62 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
63 const void *inOpenParameters
);
65 // Delete a DbContext instance created by calling dbOpen or dbCreate.
66 virtual void dbClose(DbContext
&inDbContext
);
69 virtual void dbDelete(DatabaseSession
&inDatabaseSession
,
70 const DbName
&inDbName
,
71 const AccessCredentials
*inAccessCred
);
73 // List all available databases.
74 virtual CSSM_NAME_LIST_PTR
getDbNames(DatabaseSession
&inDatabaseSession
);
75 virtual void freeNameList(DatabaseSession
&inDatabaseSession
,
76 CSSM_NAME_LIST
&inNameList
);
78 virtual void removeIfUnused(Database
&inDatabase
);
79 virtual Database
*get (const DbName
&inDbName
); // Get existing instance or make a new one.
80 virtual Database
*make (const DbName
&inDbName
) = 0; // Create a new database instance subclass must implement.
82 typedef map
<DbName
, Database
*> DatabaseMap
;
83 DatabaseMap mDatabaseMap
;
84 Mutex mDatabaseMapLock
;
88 /* Database is an abstract class. Each Database subclass should implement all the
89 pure virtual methods listed below. The constructor for a particular Database
90 subclass should create the Database object. A subsequent call to dBOpen or
91 dBCreate should be is made. This returns a DbContext. All other methods take
92 a DbContext as an argument.
98 dbCreate (DbContext
&inDbContext
, const CSSM_DBINFO
&inDBInfo
,
99 const CSSM_ACL_ENTRY_INPUT
*inInitialAclEntry
) = 0;
101 // Don't override this method in subclasses.
103 _dbCreate(DatabaseSession
&inDatabaseSession
,
104 const CSSM_DBINFO
&inDBInfo
,
105 CSSM_DB_ACCESS_TYPE inAccessRequest
,
106 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
107 const void *inOpenParameters
);
110 dbOpen (DbContext
&inDbContext
) = 0;
112 // Don't override this method in subclasses.
114 _dbOpen (DatabaseSession
&inDatabaseSession
,
115 CSSM_DB_ACCESS_TYPE inAccessRequest
,
116 const AccessCredentials
*inAccessCred
,
117 const void *inOpenParameters
);
122 // Don't override this method in subclasses.
124 _dbClose (DbContext
&dbContext
);
127 dbDelete(DatabaseSession
&inDatabaseSession
,
128 const AccessCredentials
*inAccessCred
) = 0;
131 createRelation (DbContext
&dbContext
,
132 CSSM_DB_RECORDTYPE inRelationID
,
133 const char *inRelationName
,
134 uint32 inNumberOfAttributes
,
135 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
&inAttributeInfo
,
136 uint32 inNumberOfIndexes
,
137 const CSSM_DB_SCHEMA_INDEX_INFO
&inIndexInfo
) = 0;
140 destroyRelation (DbContext
&dbContext
,
141 CSSM_DB_RECORDTYPE inRelationID
) = 0;
144 authenticate(DbContext
&dbContext
,
145 CSSM_DB_ACCESS_TYPE inAccessRequest
,
146 const AccessCredentials
&inAccessCred
) = 0;
149 getDbAcl(DbContext
&dbContext
,
150 const CSSM_STRING
*inSelectionTag
,
151 uint32
&outNumberOfAclInfos
,
152 CSSM_ACL_ENTRY_INFO_PTR
&outAclInfos
) = 0;
155 changeDbAcl(DbContext
&dbContext
,
156 const AccessCredentials
&inAccessCred
,
157 const CSSM_ACL_EDIT
&inAclEdit
) = 0;
160 getDbOwner(DbContext
&dbContext
, CSSM_ACL_OWNER_PROTOTYPE
&outOwner
) = 0;
163 changeDbOwner(DbContext
&dbContext
,
164 const AccessCredentials
&inAccessCred
,
165 const CSSM_ACL_OWNER_PROTOTYPE
&inNewOwner
) = 0;
168 getDbNameFromHandle (const DbContext
&dbContext
) const = 0;
170 virtual CSSM_DB_UNIQUE_RECORD_PTR
171 dataInsert (DbContext
&dbContext
,
172 CSSM_DB_RECORDTYPE RecordType
,
173 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributes
,
174 const CssmData
*inData
) = 0;
177 dataDelete (DbContext
&dbContext
,
178 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
) = 0;
181 dataModify (DbContext
&dbContext
,
182 CSSM_DB_RECORDTYPE RecordType
,
183 CSSM_DB_UNIQUE_RECORD
&inoutUniqueRecordIdentifier
,
184 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributesToBeModified
,
185 const CssmData
*inDataToBeModified
,
186 CSSM_DB_MODIFY_MODE ModifyMode
) = 0;
189 dataGetFirst (DbContext
&dbContext
,
190 const DLQuery
*inQuery
,
191 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
193 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
) = 0;
196 dataGetNext (DbContext
&dbContext
,
197 CSSM_HANDLE inResultsHandle
,
198 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
200 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
) = 0;
203 dataAbortQuery (DbContext
&dbContext
,
204 CSSM_HANDLE inResultsHandle
) = 0;
207 dataGetFromUniqueRecordId (DbContext
&dbContext
,
208 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
,
209 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
210 CssmData
*inoutData
) = 0;
213 freeUniqueRecord (DbContext
&dbContext
,
214 CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
) = 0;
217 passThrough(DbContext
&dbContext
,
218 uint32 passThroughId
,
219 const void *inputParams
,
220 void **outputParams
) = 0;
222 Database (const DbName
&inDbName
);
223 virtual ~Database ();
225 virtual bool hasDbContexts();
227 // XXX @@@ Think about consequences of race conditions between DbOpen/DbCreate/DbDelete/DbClose
228 // on databases with the same name at the same time.
229 //virtual DbContext &insertDbContext();
230 //virtual void removeDbContext(DbContext &inDbContext);
232 const DbName mDbName
;
234 // Subclasses must implement this method.
235 virtual DbContext
*makeDbContext(DatabaseSession
&inDatabaseSession
,
236 CSSM_DB_ACCESS_TYPE inAccessRequest
,
237 const AccessCredentials
*inAccessCred
,
238 const void *inOpenParameters
) = 0;
240 typedef set
<DbContext
*> DbContextSet
;
241 DbContextSet mDbContextSet
;
242 Mutex mDbContextSetLock
;
245 } // end namespace Security
251 #endif //_DATABASE_H_