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 // DatabaseSession.cpp - DL Session.
23 #define _CPP_DATABASESESSION
25 #include <Security/DatabaseSession.h>
27 #include <Security/Database.h>
28 #include <Security/DbContext.h>
30 #include <Security/debugging.h>
32 /* log open/close events */
33 #define DOCDebug(args...) debug("DBOpen", ## args)
39 // Session constructor
41 DatabaseSession::DatabaseSession(DatabaseManager
&inDatabaseManager
)
42 : mDatabaseManager(inDatabaseManager
)
46 DatabaseSession::~DatabaseSession()
53 DatabaseSession::GetDbNames(CSSM_NAME_LIST_PTR
&outNameList
)
55 outNameList
= mDatabaseManager
.getDbNames (*this);
60 DatabaseSession::FreeNameList(CSSM_NAME_LIST
&inNameList
)
62 mDatabaseManager
.freeNameList (*this, inNameList
);
67 DatabaseSession::DbDelete(const char *inDbName
,
68 const CSSM_NET_ADDRESS
*inDbLocation
,
69 const AccessCredentials
*inAccessCred
)
71 // The databaseManager will notify all its DbContext instances
72 // that the database is question is being deleted.
73 mDatabaseManager
.dbDelete(*this, DbName(inDbName
, CssmNetAddress::optional(inDbLocation
)), inAccessCred
);
76 // DbContext creation and destruction.
78 DatabaseSession::DbCreate(const char *inDbName
,
79 const CSSM_NET_ADDRESS
*inDbLocation
,
80 const CSSM_DBINFO
&inDBInfo
,
81 CSSM_DB_ACCESS_TYPE inAccessRequest
,
82 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
83 const void *inOpenParameters
,
84 CSSM_DB_HANDLE
&outDbHandle
)
86 outDbHandle
= CSSM_INVALID_HANDLE
; // CDSA 2.0 says to set this if we fail
87 outDbHandle
= insertDbContext(mDatabaseManager
.dbCreate(*this,
88 DbName(inDbName
, CssmNetAddress::optional(inDbLocation
)),
97 DatabaseSession::DbOpen(const char *inDbName
,
98 const CSSM_NET_ADDRESS
*inDbLocation
,
99 CSSM_DB_ACCESS_TYPE inAccessRequest
,
100 const AccessCredentials
*inAccessCred
,
101 const void *inOpenParameters
,
102 CSSM_DB_HANDLE
&outDbHandle
)
104 DOCDebug("DatabaseSession::DbOpen: dbName %s", inDbName
);
105 outDbHandle
= CSSM_INVALID_HANDLE
; // CDSA 2.0 says to set this if we fail
106 outDbHandle
= insertDbContext(mDatabaseManager
.dbOpen(*this,
107 DbName(inDbName
, CssmNetAddress::optional(inDbLocation
)),
114 DatabaseSession::insertDbContext(DbContext
&inDbContext
)
116 CSSM_DB_HANDLE aDbHandle
;
119 aDbHandle
= inDbContext
.handle ();
120 StLock
<Mutex
> _(mDbContextMapLock
);
121 mDbContextMap
.insert(DbContextMap::value_type(aDbHandle
, &inDbContext
));
126 mDatabaseManager
.dbClose(inDbContext
);
134 DatabaseSession::findDbContext(CSSM_DB_HANDLE inDbHandle
)
136 StLock
<Mutex
> _(mDbContextMapLock
);
137 DbContextMap::iterator it
= mDbContextMap
.find(inDbHandle
);
138 if (it
== mDbContextMap
.end())
139 CssmError::throwMe(CSSM_ERRCODE_INVALID_DB_HANDLE
);
144 DatabaseSession::closeAll()
146 StLock
<Mutex
> _(mDbContextMapLock
);
147 for (DbContextMap::iterator it
= mDbContextMap
.begin();
148 it
!= mDbContextMap
.end();
151 DbContext
*aDbContext
= it
->second
;
154 mDatabaseManager
.dbClose(*aDbContext
);
155 // This is done by the database itself which owns the context.
160 // Ignore exceptions since we want to close as many DBs as possible.
161 // XXX @@@ log an error or something.
165 mDbContextMap
.clear();
168 // Operations using DbContext instances.
170 DatabaseSession::DbClose(CSSM_DB_HANDLE inDbHandle
)
172 StLock
<Mutex
> _(mDbContextMapLock
);
173 DOCDebug("DatabaseSession::Close");
174 DbContextMap::iterator it
= mDbContextMap
.find(inDbHandle
);
175 if (it
== mDbContextMap
.end())
176 CssmError::throwMe(CSSM_ERRCODE_INVALID_DB_HANDLE
);
177 auto_ptr
<DbContext
> aDbContext(it
->second
);
178 mDbContextMap
.erase(it
);
179 mDatabaseManager
.dbClose(*aDbContext
);
183 DatabaseSession::CreateRelation(CSSM_DB_HANDLE inDbHandle
,
184 CSSM_DB_RECORDTYPE inRelationID
,
185 const char *inRelationName
,
186 uint32 inNumberOfAttributes
,
187 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
&inAttributeInfo
,
188 uint32 inNumberOfIndexes
,
189 const CSSM_DB_SCHEMA_INDEX_INFO
&inIndexInfo
)
191 DbContext
&aDbContext
= findDbContext(inDbHandle
);
192 return aDbContext
.mDatabase
.createRelation(aDbContext
, inRelationID
, inRelationName
,
193 inNumberOfAttributes
, inAttributeInfo
,
194 inNumberOfIndexes
, inIndexInfo
);
198 DatabaseSession::DestroyRelation(CSSM_DB_HANDLE inDbHandle
,
199 CSSM_DB_RECORDTYPE inRelationID
)
201 DbContext
&aDbContext
= findDbContext(inDbHandle
);
202 return aDbContext
.mDatabase
.destroyRelation(aDbContext
, inRelationID
);
206 DatabaseSession::Authenticate(CSSM_DB_HANDLE inDbHandle
,
207 CSSM_DB_ACCESS_TYPE inAccessRequest
,
208 const AccessCredentials
&inAccessCred
)
210 DbContext
&aDbContext
= findDbContext(inDbHandle
);
211 aDbContext
.mDatabase
.authenticate(aDbContext
, inAccessRequest
, inAccessCred
);
216 DatabaseSession::GetDbAcl(CSSM_DB_HANDLE inDbHandle
,
217 const CSSM_STRING
*inSelectionTag
,
218 uint32
&outNumberOfAclInfos
,
219 CSSM_ACL_ENTRY_INFO_PTR
&outAclInfos
)
221 DbContext
&aDbContext
= findDbContext(inDbHandle
);
222 aDbContext
.mDatabase
.getDbAcl(aDbContext
, inSelectionTag
, outNumberOfAclInfos
, outAclInfos
);
226 DatabaseSession::ChangeDbAcl(CSSM_DB_HANDLE inDbHandle
,
227 const AccessCredentials
&inAccessCred
,
228 const CSSM_ACL_EDIT
&inAclEdit
)
230 DbContext
&aDbContext
= findDbContext(inDbHandle
);
231 aDbContext
.mDatabase
.changeDbAcl(aDbContext
, inAccessCred
, inAclEdit
);
235 DatabaseSession::GetDbOwner(CSSM_DB_HANDLE inDbHandle
,
236 CSSM_ACL_OWNER_PROTOTYPE
&outOwner
)
238 DbContext
&aDbContext
= findDbContext(inDbHandle
);
239 aDbContext
.mDatabase
.getDbOwner(aDbContext
, outOwner
);
243 DatabaseSession::ChangeDbOwner(CSSM_DB_HANDLE inDbHandle
,
244 const AccessCredentials
&inAccessCred
,
245 const CSSM_ACL_OWNER_PROTOTYPE
&inNewOwner
)
247 DbContext
&aDbContext
= findDbContext(inDbHandle
);
248 aDbContext
.mDatabase
.changeDbOwner(aDbContext
, inAccessCred
, inNewOwner
);
252 DatabaseSession::GetDbNameFromHandle(CSSM_DB_HANDLE inDbHandle
,
255 DbContext
&aDbContext
= findDbContext(inDbHandle
);
256 Required(outDbName
) = aDbContext
.mDatabase
.getDbNameFromHandle(aDbContext
);
260 DatabaseSession::DataInsert(CSSM_DB_HANDLE inDbHandle
,
261 CSSM_DB_RECORDTYPE inRecordType
,
262 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributes
,
263 const CssmData
*inData
,
264 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueId
)
266 debug("dbsession", "%p DataInsert(%lx,%lx)", this, inDbHandle
, inRecordType
);
267 DbContext
&aDbContext
= findDbContext(inDbHandle
);
268 outUniqueId
= aDbContext
.mDatabase
.dataInsert(aDbContext
, inRecordType
, inAttributes
, inData
);
273 DatabaseSession::DataDelete(CSSM_DB_HANDLE inDbHandle
,
274 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
)
276 debug("dbsession", "%p DataDelete(%lx)", this, inDbHandle
);
277 DbContext
&aDbContext
= findDbContext(inDbHandle
);
278 aDbContext
.mDatabase
.dataDelete(aDbContext
, inUniqueRecordIdentifier
);
283 DatabaseSession::DataModify(CSSM_DB_HANDLE inDbHandle
,
284 CSSM_DB_RECORDTYPE inRecordType
,
285 CSSM_DB_UNIQUE_RECORD
&inoutUniqueRecordIdentifier
,
286 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributesToBeModified
,
287 const CssmData
*inDataToBeModified
,
288 CSSM_DB_MODIFY_MODE inModifyMode
)
290 debug("dbsession", "%p DataModify(%lx,%lx)", this, inDbHandle
, inRecordType
);
291 DbContext
&aDbContext
= findDbContext(inDbHandle
);
292 aDbContext
.mDatabase
.dataModify(aDbContext
, inRecordType
, inoutUniqueRecordIdentifier
,
293 inAttributesToBeModified
, inDataToBeModified
, inModifyMode
);
297 DatabaseSession::DataGetFirst(CSSM_DB_HANDLE inDbHandle
,
298 const DLQuery
*inQuery
,
299 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
301 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueId
)
303 debug("dbsession", "%p DataGetFirst(%lx)", this, inDbHandle
);
304 DbContext
&aDbContext
= findDbContext(inDbHandle
);
306 return aDbContext
.mDatabase
.dataGetFirst(aDbContext
, inQuery
,
307 inoutAttributes
, inoutData
, outUniqueId
);
311 DatabaseSession::DataGetNext(CSSM_DB_HANDLE inDbHandle
,
312 CSSM_HANDLE inResultsHandle
,
313 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
315 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
)
317 debug("dbsession", "%p DataGetNext(%lx)", this, inDbHandle
);
318 DbContext
&aDbContext
= findDbContext(inDbHandle
);
320 return aDbContext
.mDatabase
.dataGetNext(aDbContext
, inResultsHandle
, inoutAttributes
,
321 inoutData
, outUniqueRecord
);
325 DatabaseSession::DataAbortQuery(CSSM_DB_HANDLE inDbHandle
,
326 CSSM_HANDLE inResultsHandle
)
328 debug("dbsession", "%p DataAbortQuery(%lx)", this, inDbHandle
);
329 DbContext
&aDbContext
= findDbContext(inDbHandle
);
330 aDbContext
.mDatabase
.dataAbortQuery(aDbContext
, inResultsHandle
);
334 DatabaseSession::DataGetFromUniqueRecordId(CSSM_DB_HANDLE inDbHandle
,
335 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
,
336 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
339 debug("dbsession", "%p DataGetFromUniqueId(%lx)", this, inDbHandle
);
340 DbContext
&aDbContext
= findDbContext(inDbHandle
);
341 aDbContext
.mDatabase
.dataGetFromUniqueRecordId(aDbContext
, inUniqueRecord
,
342 inoutAttributes
, inoutData
);
346 DatabaseSession::FreeUniqueRecord(CSSM_DB_HANDLE inDbHandle
,
347 CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
)
349 DbContext
&aDbContext
= findDbContext(inDbHandle
);
350 aDbContext
.mDatabase
.freeUniqueRecord(aDbContext
, inUniqueRecordIdentifier
);
354 DatabaseSession::PassThrough(CSSM_DB_HANDLE inDbHandle
,
355 uint32 passThroughId
,
356 const void *inputParams
,
359 DbContext
&aDbContext
= findDbContext(inDbHandle
);
360 aDbContext
.mDatabase
.passThrough(aDbContext
, passThroughId
, inputParams
, outputParams
);