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.
22 #include <Security/Database.h>
24 #include <Security/cssmerr.h>
25 #include <Security/DbContext.h>
28 DatabaseManager::DatabaseManager ()
32 DatabaseManager::~DatabaseManager ()
37 DatabaseManager::get (const DbName
&inDbName
)
39 StLock
<Mutex
> _(mDatabaseMapLock
);
40 DatabaseMap::iterator anIterator
= mDatabaseMap
.find (inDbName
);
41 if (anIterator
== mDatabaseMap
.end())
43 auto_ptr
<Database
> aDatabase(make(inDbName
));
44 mDatabaseMap
.insert(DatabaseMap::value_type(aDatabase
->mDbName
, aDatabase
.get()));
45 return aDatabase
.release();
48 return anIterator
->second
;
52 DatabaseManager::removeIfUnused(Database
&inDatabase
)
54 StLock
<Mutex
> _(mDatabaseMapLock
);
55 if (!inDatabase
.hasDbContexts()) {
56 mDatabaseMap
.erase(inDatabase
.mDbName
);
62 DatabaseManager::dbOpen(DatabaseSession
&inDatabaseSession
,
63 const DbName
&inDbName
,
64 CSSM_DB_ACCESS_TYPE inAccessRequest
,
65 const AccessCredentials
*inAccessCred
,
66 const void *inOpenParameters
)
68 Database
&aDatabase
= *get(inDbName
);
71 return aDatabase
._dbOpen(inDatabaseSession
, inAccessRequest
, inAccessCred
, inOpenParameters
);
75 removeIfUnused(aDatabase
);
81 DatabaseManager::dbCreate(DatabaseSession
&inDatabaseSession
,
82 const DbName
&inDbName
,
83 const CSSM_DBINFO
&inDBInfo
,
84 CSSM_DB_ACCESS_TYPE inAccessRequest
,
85 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
86 const void *inOpenParameters
)
88 Database
&aDatabase
= *get(inDbName
);
91 return aDatabase
._dbCreate(inDatabaseSession
, inDBInfo
, inAccessRequest
,
92 inCredAndAclEntry
, inOpenParameters
);
96 removeIfUnused(aDatabase
);
101 // Delete a DbContext instance created by calling dbOpen or dbCreate.
103 DatabaseManager::dbClose(DbContext
&inDbContext
)
105 Database
&aDatabase
= inDbContext
.mDatabase
;
106 aDatabase
._dbClose(inDbContext
);
107 removeIfUnused(aDatabase
);
110 // Delete a database.
112 DatabaseManager::dbDelete(DatabaseSession
&inDatabaseSession
,
113 const DbName
&inDbName
,
114 const AccessCredentials
*inAccessCred
)
116 Database
&aDatabase
= *get(inDbName
);
119 aDatabase
.dbDelete(inDatabaseSession
, inAccessCred
);
123 removeIfUnused(aDatabase
);
127 removeIfUnused(aDatabase
);
130 // List all available databases.
132 DatabaseManager::getDbNames(DatabaseSession
&inDatabaseSession
)
134 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED
);
138 DatabaseManager::freeNameList(DatabaseSession
&inDatabaseSession
,
139 CSSM_NAME_LIST
&inNameList
)
141 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED
);
144 // Start of Database implementation.
146 Database::Database (const DbName
&inDbName
)
151 Database::~Database ()
156 Database::hasDbContexts()
158 StLock
<Mutex
> _(mDbContextSetLock
);
159 return !mDbContextSet
.empty();
163 Database::_dbOpen(DatabaseSession
&inDatabaseSession
,
164 CSSM_DB_ACCESS_TYPE inAccessRequest
,
165 const AccessCredentials
*inAccessCred
,
166 const void *inOpenParameters
)
168 auto_ptr
<DbContext
>aDbContext(makeDbContext(inDatabaseSession
,
173 StLock
<Mutex
> _(mDbContextSetLock
);
174 mDbContextSet
.insert(aDbContext
.get());
175 // Release the mDbContextSetLock
184 StLock
<Mutex
> _(mDbContextSetLock
);
185 mDbContextSet
.erase(aDbContext
.get());
189 return *aDbContext
.release();
193 Database::_dbCreate(DatabaseSession
&inDatabaseSession
,
194 const CSSM_DBINFO
&inDBInfo
,
195 CSSM_DB_ACCESS_TYPE inAccessRequest
,
196 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
197 const void *inOpenParameters
)
199 auto_ptr
<DbContext
>aDbContext(makeDbContext(inDatabaseSession
,
202 ? AccessCredentials::optional(inCredAndAclEntry
->AccessCred
)
206 StLock
<Mutex
> _(mDbContextSetLock
);
207 mDbContextSet
.insert(aDbContext
.get());
208 // Release the mDbContextSetLock
213 dbCreate(*aDbContext
, inDBInfo
,
214 inCredAndAclEntry
? &inCredAndAclEntry
->InitialAclEntry
: NULL
);
218 StLock
<Mutex
> _(mDbContextSetLock
);
219 mDbContextSet
.erase(aDbContext
.get());
223 return *aDbContext
.release();
227 Database::_dbClose(DbContext
&dbContext
)
229 StLock
<Mutex
> _(mDbContextSetLock
);
230 mDbContextSet
.erase(&dbContext
);
231 if (mDbContextSet
.empty())