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 DbContext
&aDbContext
= findDbContext(inDbHandle
);
267 outUniqueId
= aDbContext
.mDatabase
.dataInsert(aDbContext
, inRecordType
, inAttributes
, inData
);
272 DatabaseSession::DataDelete(CSSM_DB_HANDLE inDbHandle
,
273 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
)
275 DbContext
&aDbContext
= findDbContext(inDbHandle
);
276 aDbContext
.mDatabase
.dataDelete(aDbContext
, inUniqueRecordIdentifier
);
281 DatabaseSession::DataModify(CSSM_DB_HANDLE inDbHandle
,
282 CSSM_DB_RECORDTYPE inRecordType
,
283 CSSM_DB_UNIQUE_RECORD
&inoutUniqueRecordIdentifier
,
284 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributesToBeModified
,
285 const CssmData
*inDataToBeModified
,
286 CSSM_DB_MODIFY_MODE inModifyMode
)
288 DbContext
&aDbContext
= findDbContext(inDbHandle
);
289 aDbContext
.mDatabase
.dataModify(aDbContext
, inRecordType
, inoutUniqueRecordIdentifier
,
290 inAttributesToBeModified
, inDataToBeModified
, inModifyMode
);
294 DatabaseSession::DataGetFirst(CSSM_DB_HANDLE inDbHandle
,
295 const DLQuery
*inQuery
,
296 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
298 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueId
)
300 DbContext
&aDbContext
= findDbContext(inDbHandle
);
302 return aDbContext
.mDatabase
.dataGetFirst(aDbContext
, inQuery
,
303 inoutAttributes
, inoutData
, outUniqueId
);
307 DatabaseSession::DataGetNext(CSSM_DB_HANDLE inDbHandle
,
308 CSSM_HANDLE inResultsHandle
,
309 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
311 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
)
313 DbContext
&aDbContext
= findDbContext(inDbHandle
);
315 return aDbContext
.mDatabase
.dataGetNext(aDbContext
, inResultsHandle
, inoutAttributes
,
316 inoutData
, outUniqueRecord
);
320 DatabaseSession::DataAbortQuery(CSSM_DB_HANDLE inDbHandle
,
321 CSSM_HANDLE inResultsHandle
)
323 DbContext
&aDbContext
= findDbContext(inDbHandle
);
324 aDbContext
.mDatabase
.dataAbortQuery(aDbContext
, inResultsHandle
);
328 DatabaseSession::DataGetFromUniqueRecordId(CSSM_DB_HANDLE inDbHandle
,
329 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
,
330 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
333 DbContext
&aDbContext
= findDbContext(inDbHandle
);
334 aDbContext
.mDatabase
.dataGetFromUniqueRecordId(aDbContext
, inUniqueRecord
,
335 inoutAttributes
, inoutData
);
339 DatabaseSession::FreeUniqueRecord(CSSM_DB_HANDLE inDbHandle
,
340 CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
)
342 DbContext
&aDbContext
= findDbContext(inDbHandle
);
343 aDbContext
.mDatabase
.freeUniqueRecord(aDbContext
, inUniqueRecordIdentifier
);
347 DatabaseSession::PassThrough(CSSM_DB_HANDLE inDbHandle
,
348 uint32 passThroughId
,
349 const void *inputParams
,
352 DbContext
&aDbContext
= findDbContext(inDbHandle
);
353 aDbContext
.mDatabase
.passThrough(aDbContext
, passThroughId
, inputParams
, outputParams
);