2 * Copyright (c) 2000-2004,2011,2014 Apple 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_cdsa_utilities/cssmacl.h>
23 #include <security_utilities/threading.h>
24 #include <security_cdsa_utilities/cssmdb.h>
30 // @@@ Should not use using in headers.
37 class DatabaseFactory
;
38 class DatabaseSession
;
41 /* DatabaseManager class. */
44 NOCOPY(DatabaseManager
)
47 virtual ~DatabaseManager ();
49 // Create and return a new DbContext instance which is owned by us and must be discared by calling dbClose.
50 virtual DbContext
&dbOpen(DatabaseSession
&inDatabaseSession
,
51 const DbName
&inDbName
,
52 CSSM_DB_ACCESS_TYPE inAccessRequest
,
53 const AccessCredentials
*inAccessCred
,
54 const void *inOpenParameters
);
55 virtual DbContext
&dbCreate(DatabaseSession
&inDatabaseSession
,
56 const DbName
&inDbName
,
57 const CSSM_DBINFO
&inDBInfo
,
58 CSSM_DB_ACCESS_TYPE inAccessRequest
,
59 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
60 const void *inOpenParameters
);
62 // Delete a DbContext instance created by calling dbOpen or dbCreate.
63 virtual void dbClose(DbContext
&inDbContext
);
66 virtual void dbDelete(DatabaseSession
&inDatabaseSession
,
67 const DbName
&inDbName
,
68 const AccessCredentials
*inAccessCred
);
70 // List all available databases.
71 virtual CSSM_NAME_LIST_PTR
getDbNames(DatabaseSession
&inDatabaseSession
);
72 virtual void freeNameList(DatabaseSession
&inDatabaseSession
,
73 CSSM_NAME_LIST
&inNameList
);
75 virtual void removeIfUnused(Database
&inDatabase
);
76 virtual Database
*get (const DbName
&inDbName
); // Get existing instance or make a new one.
77 virtual Database
*make (const DbName
&inDbName
) = 0; // Create a new database instance subclass must implement.
79 typedef map
<DbName
, Database
*> DatabaseMap
;
80 DatabaseMap mDatabaseMap
;
81 Mutex mDatabaseMapLock
;
85 /* Database is an abstract class. Each Database subclass should implement all the
86 pure virtual methods listed below. The constructor for a particular Database
87 subclass should create the Database object. A subsequent call to dBOpen or
88 dBCreate should be is made. This returns a DbContext. All other methods take
89 a DbContext as an argument.
95 dbCreate (DbContext
&inDbContext
, const CSSM_DBINFO
&inDBInfo
,
96 const CSSM_ACL_ENTRY_INPUT
*inInitialAclEntry
) = 0;
98 // Don't override this method in subclasses.
100 _dbCreate(DatabaseSession
&inDatabaseSession
,
101 const CSSM_DBINFO
&inDBInfo
,
102 CSSM_DB_ACCESS_TYPE inAccessRequest
,
103 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
104 const void *inOpenParameters
);
107 dbOpen (DbContext
&inDbContext
) = 0;
109 // Don't override this method in subclasses.
111 _dbOpen (DatabaseSession
&inDatabaseSession
,
112 CSSM_DB_ACCESS_TYPE inAccessRequest
,
113 const AccessCredentials
*inAccessCred
,
114 const void *inOpenParameters
);
119 // Don't override this method in subclasses.
121 _dbClose (DbContext
&dbContext
);
124 dbDelete(DatabaseSession
&inDatabaseSession
,
125 const AccessCredentials
*inAccessCred
) = 0;
128 createRelation (DbContext
&dbContext
,
129 CSSM_DB_RECORDTYPE inRelationID
,
130 const char *inRelationName
,
131 uint32 inNumberOfAttributes
,
132 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*inAttributeInfo
,
133 uint32 inNumberOfIndexes
,
134 const CSSM_DB_SCHEMA_INDEX_INFO
&inIndexInfo
) = 0;
137 destroyRelation (DbContext
&dbContext
,
138 CSSM_DB_RECORDTYPE inRelationID
) = 0;
141 authenticate(DbContext
&dbContext
,
142 CSSM_DB_ACCESS_TYPE inAccessRequest
,
143 const AccessCredentials
&inAccessCred
) = 0;
146 getDbAcl(DbContext
&dbContext
,
147 const CSSM_STRING
*inSelectionTag
,
148 uint32
&outNumberOfAclInfos
,
149 CSSM_ACL_ENTRY_INFO_PTR
&outAclInfos
) = 0;
152 changeDbAcl(DbContext
&dbContext
,
153 const AccessCredentials
&inAccessCred
,
154 const CSSM_ACL_EDIT
&inAclEdit
) = 0;
157 getDbOwner(DbContext
&dbContext
, CSSM_ACL_OWNER_PROTOTYPE
&outOwner
) = 0;
160 changeDbOwner(DbContext
&dbContext
,
161 const AccessCredentials
&inAccessCred
,
162 const CSSM_ACL_OWNER_PROTOTYPE
&inNewOwner
) = 0;
165 getDbNameFromHandle (const DbContext
&dbContext
) const = 0;
167 virtual CSSM_DB_UNIQUE_RECORD_PTR
168 dataInsert (DbContext
&dbContext
,
169 CSSM_DB_RECORDTYPE RecordType
,
170 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributes
,
171 const CssmData
*inData
) = 0;
174 dataDelete (DbContext
&dbContext
,
175 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
) = 0;
178 dataModify (DbContext
&dbContext
,
179 CSSM_DB_RECORDTYPE RecordType
,
180 CSSM_DB_UNIQUE_RECORD
&inoutUniqueRecordIdentifier
,
181 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributesToBeModified
,
182 const CssmData
*inDataToBeModified
,
183 CSSM_DB_MODIFY_MODE ModifyMode
) = 0;
186 dataGetFirst (DbContext
&dbContext
,
187 const CssmQuery
*inQuery
,
188 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
190 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
) = 0;
193 dataGetNext (DbContext
&dbContext
,
194 CSSM_HANDLE inResultsHandle
,
195 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
197 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
) = 0;
200 dataAbortQuery (DbContext
&dbContext
,
201 CSSM_HANDLE inResultsHandle
) = 0;
204 dataGetFromUniqueRecordId (DbContext
&dbContext
,
205 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
,
206 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
207 CssmData
*inoutData
) = 0;
210 freeUniqueRecord (DbContext
&dbContext
,
211 CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
) = 0;
214 passThrough(DbContext
&dbContext
,
215 uint32 passThroughId
,
216 const void *inputParams
,
217 void **outputParams
) = 0;
219 Database (const DbName
&inDbName
);
220 virtual ~Database ();
222 virtual bool hasDbContexts();
224 // XXX @@@ Think about consequences of race conditions between DbOpen/DbCreate/DbDelete/DbClose
225 // on databases with the same name at the same time.
226 //virtual DbContext &insertDbContext();
227 //virtual void removeDbContext(DbContext &inDbContext);
229 const DbName mDbName
;
231 // Subclasses must implement this method.
232 virtual DbContext
*makeDbContext(DatabaseSession
&inDatabaseSession
,
233 CSSM_DB_ACCESS_TYPE inAccessRequest
,
234 const AccessCredentials
*inAccessCred
,
235 const void *inOpenParameters
) = 0;
237 typedef set
<DbContext
*> DbContextSet
;
238 DbContextSet mDbContextSet
;
239 Mutex mDbContextSetLock
;
242 } // end namespace Security
248 #endif //_DATABASE_H_