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 // dlclient - client interface to CSSM DLs and their operations
22 #include <security_cdsa_client/dlclient.h>
23 #include <security_cdsa_client/aclclient.h>
24 #include <Security/cssmapple.h>
25 #include <Security/cssmapplePriv.h>
27 using namespace CssmClient
;
30 // blob type for blobs created by these classes -- done so that we can change the formats later
31 const uint32 kBlobType
= 0x1;
40 DbCursorMaker::~DbCursorMaker()
43 DbUniqueRecordMaker::~DbUniqueRecordMaker()
48 // Manage DL attachments
50 DLImpl::DLImpl(const Guid
&guid
) : AttachmentImpl(guid
, CSSM_SERVICE_DL
)
54 DLImpl::DLImpl(const Module
&module) : AttachmentImpl(module, CSSM_SERVICE_DL
)
63 DLImpl::getDbNames(char **)
65 CssmError::throwMe(CSSMERR_DL_FUNCTION_NOT_IMPLEMENTED
);
69 DLImpl::freeNameList(char **)
71 CssmError::throwMe(CSSMERR_DL_FUNCTION_NOT_IMPLEMENTED
);
75 DLImpl::newDb(const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
77 return new DbImpl(DL(this), inDbName
, inDbLocation
);
84 DbImpl::DbImpl(const DL
&dl
, const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
85 : ObjectImpl(dl
), mDbName(inDbName
, inDbLocation
),
86 mUseNameFromHandle(!inDbName
), mNameFromHandle(NULL
),
87 mAccessRequest(CSSM_DB_ACCESS_READ
), mAccessCredentials(NULL
),
88 mDefaultCredentials(NULL
), mOpenParameters(NULL
), mDbInfo(NULL
),
89 mResourceControlContext(NULL
)
98 allocator().free(mNameFromHandle
);
109 assert(mDbInfo
== nil
);
110 mHandle
.DLHandle
= dl()->handle();
111 check(CSSM_DL_DbOpen(mHandle
.DLHandle
, mDbName
.canonicalName(), dbLocation(),
112 mAccessRequest
, mAccessCredentials
,
113 mOpenParameters
, &mHandle
.DBHandle
));
116 if (!mAccessCredentials
&& mDefaultCredentials
)
117 if (const AccessCredentials
*creds
= mDefaultCredentials
->makeCredentials())
118 CSSM_DL_Authenticate(handle(), mAccessRequest
, creds
); // ignore error
123 DbImpl::createWithBlob(CssmData
&blob
)
126 CssmError::throwMe(CSSMERR_DL_DATASTORE_ALREADY_EXISTS
);
128 if (mDbInfo
== nil
) {
129 // handle a missing (null) mDbInfo as an all-zero one
130 static const CSSM_DBINFO nullDbInfo
= { };
131 mDbInfo
= &nullDbInfo
;
134 mHandle
.DLHandle
= dl()->handle();
136 // create a parameter block for our call to the passthrough
137 CSSM_APPLE_CSPDL_DB_CREATE_WITH_BLOB_PARAMETERS params
;
139 params
.dbName
= mDbName
.canonicalName ();
140 params
.dbLocation
= dbLocation ();
141 params
.dbInfo
= mDbInfo
;
142 params
.accessRequest
= mAccessRequest
;
143 params
.credAndAclEntry
= NULL
;
144 params
.openParameters
= mOpenParameters
;
147 check(CSSM_DL_PassThrough (mHandle
, CSSM_APPLECSPDL_DB_CREATE_WITH_BLOB
, ¶ms
, (void**) &mHandle
.DBHandle
));
154 CssmError::throwMe(CSSMERR_DL_DATASTORE_ALREADY_EXISTS
);
156 if (mDbInfo
== nil
) {
157 // handle a missing (null) mDbInfo as an all-zero one
158 static const CSSM_DBINFO nullDbInfo
= { };
159 mDbInfo
= &nullDbInfo
;
161 mHandle
.DLHandle
= dl()->handle();
163 if (!mResourceControlContext
&& mAccessCredentials
) {
164 AclFactory::AnyResourceContext
ctx(mAccessCredentials
);
165 check(CSSM_DL_DbCreate(mHandle
.DLHandle
, mDbName
.canonicalName(), dbLocation(),
166 mDbInfo
, mAccessRequest
, &ctx
,
167 mOpenParameters
, &mHandle
.DBHandle
));
169 check(CSSM_DL_DbCreate(mHandle
.DLHandle
, mDbName
.canonicalName(), dbLocation(),
170 mDbInfo
, mAccessRequest
, mResourceControlContext
,
171 mOpenParameters
, &mHandle
.DBHandle
));
181 check(CSSM_DL_DbClose (mHandle
));
211 // Deactivate so the db gets closed if it was open.
213 // This call does not require the receiver to be active.
214 check(CSSM_DL_DbDelete(dl()->handle(), mDbName
.canonicalName(), dbLocation(),
215 mAccessCredentials
));
219 DbImpl::rename(const char *newName
)
221 // Deactivate so the db gets closed if it was open.
223 if (::rename(mDbName
.canonicalName(), newName
))
224 UnixError::throwMe(errno
);
226 // Change our DbName to reflect this rename.
227 mDbName
= DbName(newName
, dbLocation());
231 DbImpl::authenticate(CSSM_DB_ACCESS_TYPE inAccessRequest
,
232 const CSSM_ACCESS_CREDENTIALS
*inAccessCredentials
)
236 // XXX Could do the same for create but this would require sticking
237 // inAccessCredentials into mResourceControlContext.
240 // We were not yet active. Just do an open.
241 accessRequest(inAccessRequest
);
242 accessCredentials(inAccessCredentials
);
248 check(CSSM_DL_Authenticate(handle(), inAccessRequest
, inAccessCredentials
));
252 DbImpl::name(char *&outDbName
)
254 check(CSSM_DL_GetDbNameFromHandle(handle(), &outDbName
));
260 if (mUseNameFromHandle
)
263 || !CSSM_DL_GetDbNameFromHandle(handle(), &mNameFromHandle
))
265 return mNameFromHandle
;
268 // We failed to get the name from the handle so use the passed
270 mUseNameFromHandle
= false;
273 return mDbName
.canonicalName();
277 DbImpl::createRelation(CSSM_DB_RECORDTYPE inRelationID
,
278 const char *inRelationName
,
279 uint32 inNumberOfAttributes
,
280 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*pAttributeInfo
,
281 uint32 inNumberOfIndexes
,
282 const CSSM_DB_SCHEMA_INDEX_INFO
*pIndexInfo
)
284 check(CSSM_DL_CreateRelation(handle(), inRelationID
, inRelationName
,
285 inNumberOfAttributes
, pAttributeInfo
,
286 inNumberOfIndexes
, pIndexInfo
));
290 DbImpl::destroyRelation(CSSM_DB_RECORDTYPE inRelationID
)
292 check(CSSM_DL_DestroyRelation(handle(), inRelationID
));
296 DbImpl::insert(CSSM_DB_RECORDTYPE recordType
, const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
297 const CSSM_DATA
*data
)
299 DbUniqueRecord
uniqueId(Db(this));
300 check(CSSM_DL_DataInsert(handle(), recordType
,
303 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
304 uniqueId
->activate();
310 DbImpl::insertWithoutEncryption(CSSM_DB_RECORDTYPE recordType
, const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
313 DbUniqueRecord
uniqueId(Db(this));
315 // fill out the parameters
316 CSSM_APPLECSPDL_DB_INSERT_WITHOUT_ENCRYPTION_PARAMETERS params
;
317 params
.recordType
= recordType
;
318 params
.attributes
= const_cast<CSSM_DB_RECORD_ATTRIBUTE_DATA
*>(attributes
);
321 // for clarity, call the overloaded operator to produce a unique record pointer
322 CSSM_DB_UNIQUE_RECORD_PTR
*uniquePtr
= uniqueId
;
325 passThrough (CSSM_APPLECSPDL_DB_INSERT_WITHOUT_ENCRYPTION
, ¶ms
, (void**) uniquePtr
);
327 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
328 uniqueId
->activate();
334 // Generic Passthrough interface
336 void DbImpl::passThrough(uint32 passThroughId
, const void *in
, void **out
)
338 check(CSSM_DL_PassThrough(handle(), passThroughId
, in
, out
));
343 // Passthrough functions (only implemented by AppleCSPDL).
348 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_LOCK
, NULL
, NULL
));
354 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, NULL
, NULL
));
358 DbImpl::unlock(const CSSM_DATA
&password
)
360 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, &password
, NULL
));
364 DbImpl::getSettings(uint32
&outIdleTimeout
, bool &outLockOnSleep
)
366 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS_PTR settings
;
367 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_GET_SETTINGS
,
368 NULL
, reinterpret_cast<void **>(&settings
)));
369 outIdleTimeout
= settings
->idleTimeout
;
370 outLockOnSleep
= settings
->lockOnSleep
;
371 allocator().free(settings
);
375 DbImpl::setSettings(uint32 inIdleTimeout
, bool inLockOnSleep
)
377 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS settings
;
378 settings
.idleTimeout
= inIdleTimeout
;
379 settings
.lockOnSleep
= inLockOnSleep
;
380 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_SET_SETTINGS
, &settings
, NULL
));
386 CSSM_APPLECSPDL_DB_IS_LOCKED_PARAMETERS_PTR params
;
387 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_IS_LOCKED
,
388 NULL
, reinterpret_cast<void **>(¶ms
)));
389 bool isLocked
= params
->isLocked
;
390 allocator().free(params
);
395 DbImpl::changePassphrase(const CSSM_ACCESS_CREDENTIALS
*cred
)
397 CSSM_APPLECSPDL_DB_CHANGE_PASSWORD_PARAMETERS params
;
398 params
.accessCredentials
= const_cast<CSSM_ACCESS_CREDENTIALS
*>(cred
);
399 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_CHANGE_PASSWORD
, ¶ms
, NULL
));
402 void DbImpl::recode(const CSSM_DATA
&data
, const CSSM_DATA
&extraData
)
404 // setup parameters for the recode call
405 CSSM_APPLECSPDL_RECODE_PARAMETERS params
;
406 params
.dbBlob
= data
;
407 params
.extraData
= extraData
;
410 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_CSP_RECODE
, ¶ms
, NULL
));
413 void DbImpl::copyBlob (CssmData
&data
)
416 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_COPY_BLOB
, NULL
, (void**) (CSSM_DATA
*) &data
));
419 void DbImpl::setBatchMode(Boolean mode
, Boolean rollback
)
422 // We need the DL_DB_Handle of the underyling DL in order to use CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT
425 CSSM_DL_DB_HANDLE dldbHandleOfUnderlyingDL
;
426 result
= CSSM_DL_PassThrough(handle(),
427 CSSM_APPLECSPDL_DB_GET_HANDLE
,
429 (void **)&dldbHandleOfUnderlyingDL
);
431 // Now, toggle the autocommit...
433 if ( result
== noErr
)
435 CSSM_BOOL modeToUse
= !mode
;
438 result
= (OSStatus
)CSSM_DL_PassThrough(dldbHandleOfUnderlyingDL
,
439 CSSM_APPLEFILEDL_ROLLBACK
, NULL
, NULL
);
442 result
= CSSM_DL_PassThrough(dldbHandleOfUnderlyingDL
,
443 CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT
,
446 if (!rollback
&& modeToUse
)
447 result
= CSSM_DL_PassThrough(dldbHandleOfUnderlyingDL
,
448 CSSM_APPLEFILEDL_COMMIT
,
458 DbImpl::newDbCursor(const CSSM_QUERY
&query
, Allocator
&allocator
)
460 return new DbDbCursorImpl(Db(this), query
, allocator
);
464 DbImpl::newDbCursor(uint32 capacity
, Allocator
&allocator
)
466 return new DbDbCursorImpl(Db(this), capacity
, allocator
);
471 // Db adapters for AclBearer
473 void DbImpl::getAcl(AutoAclEntryInfoList
&aclInfos
, const char *selectionTag
) const
475 aclInfos
.allocator(allocator());
476 check(CSSM_DL_GetDbAcl(const_cast<DbImpl
*>(this)->handle(),
477 reinterpret_cast<const CSSM_STRING
*>(selectionTag
), aclInfos
, aclInfos
));
480 void DbImpl::changeAcl(const CSSM_ACL_EDIT
&aclEdit
,
481 const CSSM_ACCESS_CREDENTIALS
*accessCred
)
483 check(CSSM_DL_ChangeDbAcl(handle(), AccessCredentials::needed(accessCred
), &aclEdit
));
486 void DbImpl::getOwner(AutoAclOwnerPrototype
&owner
) const
488 owner
.allocator(allocator());
489 check(CSSM_DL_GetDbOwner(const_cast<DbImpl
*>(this)->handle(), owner
));
492 void DbImpl::changeOwner(const CSSM_ACL_OWNER_PROTOTYPE
&newOwner
,
493 const CSSM_ACCESS_CREDENTIALS
*accessCred
)
495 check(CSSM_DL_ChangeDbOwner(handle(),
496 AccessCredentials::needed(accessCred
), &newOwner
));
499 void DbImpl::defaultCredentials(DefaultCredentialsMaker
*maker
)
501 mDefaultCredentials
= maker
;
506 // Abstract DefaultCredentialsMakers
508 DbImpl::DefaultCredentialsMaker::~DefaultCredentialsMaker()
513 // Db adapters for DLAccess
515 CSSM_HANDLE
Db::dlGetFirst(const CSSM_QUERY
&query
, CSSM_DB_RECORD_ATTRIBUTE_DATA
&attributes
,
516 CSSM_DATA
*data
, CSSM_DB_UNIQUE_RECORD
*&id
)
519 switch (CSSM_RETURN rc
= CSSM_DL_DataGetFirst(handle(), &query
, &result
, &attributes
, data
, &id
)) {
522 case CSSMERR_DL_ENDOFDATA
:
523 return CSSM_INVALID_HANDLE
;
525 CssmError::throwMe(rc
);
526 return CSSM_INVALID_HANDLE
; // placebo
530 bool Db::dlGetNext(CSSM_HANDLE query
, CSSM_DB_RECORD_ATTRIBUTE_DATA
&attributes
,
531 CSSM_DATA
*data
, CSSM_DB_UNIQUE_RECORD
*&id
)
533 CSSM_RETURN rc
= CSSM_DL_DataGetNext(handle(), query
, &attributes
, data
, &id
);
537 case CSSMERR_DL_ENDOFDATA
:
540 CssmError::throwMe(rc
);
541 return false; // placebo
545 void Db::dlAbortQuery(CSSM_HANDLE query
)
547 CssmError::check(CSSM_DL_DataAbortQuery(handle(), query
));
550 void Db::dlFreeUniqueId(CSSM_DB_UNIQUE_RECORD
*id
)
552 CssmError::check(CSSM_DL_FreeUniqueRecord(handle(), id
));
555 void Db::dlDeleteRecord(CSSM_DB_UNIQUE_RECORD
*id
)
557 CssmError::check(CSSM_DL_DataDelete(handle(), id
));
560 Allocator
&Db::allocator()
562 return Object::allocator();
567 // DbUniqueRecordMaker
570 DbImpl::newDbUniqueRecord()
572 return new DbUniqueRecordImpl(Db(this));
580 DbImpl::dlDbIdentifier()
582 // Always use the same dbName and dbLocation that were passed in during
584 return DLDbIdentifier(dl()->subserviceUid(), mDbName
.canonicalName(), dbLocation());
591 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, const CSSM_QUERY
&query
, Allocator
&allocator
)
592 : DbCursorImpl(db
, query
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
596 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, uint32 capacity
, Allocator
&allocator
)
597 : DbCursorImpl(db
, capacity
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
601 DbDbCursorImpl::~DbDbCursorImpl()
611 DbDbCursorImpl::next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
)
614 attributes
->deleteValues();
621 DbUniqueRecord
unique(db
);
624 // ask the CSP/DL if the requested record type exists
625 CSSM_BOOL boolResult
;
626 CSSM_DL_PassThrough(db
->handle(), CSSM_APPLECSPDL_DB_RELATION_EXISTS
, &RecordType
, (void**) &boolResult
);
637 result
= CSSM_DL_DataGetFirst(db
->handle(),
643 if (result
== CSSM_OK
)
645 else if (data
!= NULL
)
650 result
= CSSM_DL_DataGetNext(db
->handle(),
656 if (result
!= CSSM_OK
&& data
!= NULL
)
662 if (result
!= CSSM_OK
&& attributes
!= NULL
)
664 attributes
->invalidate();
667 if (result
== CSSMERR_DL_ENDOFDATA
)
675 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
682 DbDbCursorImpl::activate()
687 DbDbCursorImpl::deactivate()
692 check(CSSM_DL_DataAbortQuery(database()->handle(), mResultsHandle
));
700 DbCursorImpl::DbCursorImpl(const Object
&parent
, const CSSM_QUERY
&query
, Allocator
&allocator
) :
701 ObjectImpl(parent
), CssmAutoQuery(query
, allocator
)
705 DbCursorImpl::DbCursorImpl(const Object
&parent
, uint32 capacity
, Allocator
&allocator
) :
706 ObjectImpl(parent
), CssmAutoQuery(capacity
, allocator
)
711 DbCursorImpl::allocator() const
713 return ObjectImpl::allocator();
717 DbCursorImpl::allocator(Allocator
&alloc
)
719 ObjectImpl::allocator(alloc
);
726 DbUniqueRecordImpl::DbUniqueRecordImpl(const Db
&db
) : ObjectImpl(db
), mDestroyID (false)
730 DbUniqueRecordImpl::~DbUniqueRecordImpl()
736 allocator ().free (mUniqueId
);
745 DbUniqueRecordImpl::deleteRecord()
747 check(CSSM_DL_DataDelete(database()->handle(), mUniqueId
));
751 DbUniqueRecordImpl::modify(CSSM_DB_RECORDTYPE recordType
,
752 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
753 const CSSM_DATA
*data
,
754 CSSM_DB_MODIFY_MODE modifyMode
)
756 check(CSSM_DL_DataModify(database()->handle(), recordType
, mUniqueId
,
762 DbUniqueRecordImpl::modifyWithoutEncryption(CSSM_DB_RECORDTYPE recordType
,
763 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
764 const CSSM_DATA
*data
,
765 CSSM_DB_MODIFY_MODE modifyMode
)
767 // fill out the parameters
768 CSSM_APPLECSPDL_DB_MODIFY_WITHOUT_ENCRYPTION_PARAMETERS params
;
769 params
.recordType
= recordType
;
770 params
.uniqueID
= mUniqueId
;
771 params
.attributes
= const_cast<CSSM_DB_RECORD_ATTRIBUTE_DATA
*>(attributes
);
772 params
.data
= (CSSM_DATA
*) data
;
773 params
.modifyMode
= modifyMode
;
776 check(CSSM_DL_PassThrough(database()->handle(),
777 CSSM_APPLECSPDL_DB_MODIFY_WITHOUT_ENCRYPTION
,
783 DbUniqueRecordImpl::get(DbAttributes
*attributes
,
784 ::CssmDataContainer
*data
)
787 attributes
->deleteValues();
792 // @@@ Fix the allocators for attributes and data.
794 result
= CSSM_DL_DataGetFromUniqueRecordId(database()->handle(), mUniqueId
,
798 if (result
!= CSSM_OK
)
801 attributes
->invalidate();
802 if (data
!= NULL
) // the data returned is no longer valid
812 DbUniqueRecordImpl::getWithoutEncryption(DbAttributes
*attributes
,
813 ::CssmDataContainer
*data
)
816 attributes
->deleteValues();
821 // @@@ Fix the allocators for attributes and data.
824 // make the parameter block
825 CSSM_APPLECSPDL_DB_GET_WITHOUT_ENCRYPTION_PARAMETERS params
;
826 params
.uniqueID
= mUniqueId
;
827 params
.attributes
= attributes
;
830 ::CssmDataContainer recordData
;
831 result
= CSSM_DL_PassThrough(database()->handle(), CSSM_APPLECSPDL_DB_GET_WITHOUT_ENCRYPTION
, ¶ms
,
837 DbUniqueRecordImpl::activate()
843 DbUniqueRecordImpl::deactivate()
848 check(CSSM_DL_FreeUniqueRecord(database()->handle(), mUniqueId
));
853 DbUniqueRecordImpl::getRecordIdentifier(CSSM_DATA
&data
)
855 check(CSSM_DL_PassThrough(database()->handle(), CSSM_APPLECSPDL_DB_GET_RECORD_IDENTIFIER
,
856 mUniqueId
, (void**) &data
));
859 void DbUniqueRecordImpl::setUniqueRecordPtr(CSSM_DB_UNIQUE_RECORD_PTR uniquePtr
)
862 mUniqueId
= (CSSM_DB_UNIQUE_RECORD_PTR
) allocator ().malloc (sizeof (CSSM_DB_UNIQUE_RECORD
));
863 *mUniqueId
= *uniquePtr
;
870 DbAttributes::DbAttributes()
871 : CssmAutoDbRecordAttributeData(0, Allocator::standard(), Allocator::standard())
875 DbAttributes::DbAttributes(const Db
&db
, uint32 capacity
, Allocator
&allocator
)
876 : CssmAutoDbRecordAttributeData(capacity
, db
->allocator(), allocator
)