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/dlclient.h>
24 using namespace CssmClient
;
28 // Manage DL attachments
30 DLImpl::DLImpl(const Guid
&guid
) : AttachmentImpl(guid
, CSSM_SERVICE_DL
)
34 DLImpl::DLImpl(const Module
&module) : AttachmentImpl(module, CSSM_SERVICE_DL
)
43 DLImpl::getDbNames(char **)
45 CssmError::throwMe(CSSMERR_DL_FUNCTION_NOT_IMPLEMENTED
);
49 DLImpl::freeNameList(char **)
51 CssmError::throwMe(CSSMERR_DL_FUNCTION_NOT_IMPLEMENTED
);
55 DLImpl::newDb(const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
57 return new DbImpl(DL(this), inDbName
, inDbLocation
);
64 DbImpl::DbImpl(const DL
&dl
, const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
65 : ObjectImpl(dl
), mDbName(inDbName
, inDbLocation
),
66 mAccessRequest(CSSM_DB_ACCESS_READ
), mAccessCredentials(NULL
),
67 mOpenParameters(NULL
), mDbInfo(NULL
), mResourceControlContext(NULL
)
85 assert(mDbInfo
== nil
);
86 mHandle
.DLHandle
= dl()->handle();
87 check(CSSM_DL_DbOpen(mHandle
.DLHandle
, name(), dbLocation(),
88 mAccessRequest
, mAccessCredentials
,
89 mOpenParameters
, &mHandle
.DBHandle
));
98 CssmError::throwMe(CSSMERR_DL_DATASTORE_ALREADY_EXISTS
);
100 assert(mDbInfo
!= nil
);
101 mHandle
.DLHandle
= dl()->handle();
102 check(CSSM_DL_DbCreate(mHandle
.DLHandle
, name(), dbLocation(), mDbInfo
,
103 mAccessRequest
, mResourceControlContext
,
104 mOpenParameters
, &mHandle
.DBHandle
));
111 check(CSSM_DL_DbClose(mHandle
));
139 // Deactivate so the db gets closed if it was open.
141 // This call does not require the receiver to be active.
142 check(CSSM_DL_DbDelete(dl()->handle(), name(), dbLocation(),
143 mAccessCredentials
));
147 DbImpl::authenticate(CSSM_DB_ACCESS_TYPE inAccessRequest
,
148 const CSSM_ACCESS_CREDENTIALS
*inAccessCredentials
)
152 // XXX Could do the same for create but this would require sticking
153 // inAccessCredentials into mResourceControlContext.
156 // We were not yet active. Just do an open.
157 accessRequest(inAccessRequest
);
158 accessCredentials(inAccessCredentials
);
164 check(CSSM_DL_Authenticate(handle(), inAccessRequest
, inAccessCredentials
));
168 DbImpl::name(char *&outDbName
)
170 check(CSSM_DL_GetDbNameFromHandle(handle(), &outDbName
));
174 DbImpl::createRelation(CSSM_DB_RECORDTYPE inRelationID
,
175 const char *inRelationName
,
176 uint32 inNumberOfAttributes
,
177 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*pAttributeInfo
,
178 uint32 inNumberOfIndexes
,
179 const CSSM_DB_SCHEMA_INDEX_INFO
*pIndexInfo
)
181 check(CSSM_DL_CreateRelation(handle(), inRelationID
, inRelationName
,
182 inNumberOfAttributes
, pAttributeInfo
,
183 inNumberOfIndexes
, pIndexInfo
));
187 DbImpl::destroyRelation(CSSM_DB_RECORDTYPE inRelationID
)
189 check(CSSM_DL_DestroyRelation(handle(), inRelationID
));
193 DbImpl::insert(CSSM_DB_RECORDTYPE recordType
, const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
194 const CSSM_DATA
*data
)
196 DbUniqueRecord
uniqueId(Db(this));
197 check(CSSM_DL_DataInsert(handle(), recordType
,
200 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
201 uniqueId
->activate();
206 // @@@ These methods have been moved to DbUniqueRecord.
208 DbImpl::deleteRecord(const DbUniqueRecord
&uniqueId
)
210 check(CSSM_DL_DataDelete(handle(), uniqueId
));
214 DbImpl::modify(CSSM_DB_RECORDTYPE recordType
, DbUniqueRecord
&uniqueId
,
215 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
216 const CSSM_DATA
*data
,
217 CSSM_DB_MODIFY_MODE modifyMode
)
219 check(CSSM_DL_DataModify(handle(), recordType
, uniqueId
,
225 DbImpl::get(const DbUniqueRecord
&uniqueId
, DbAttributes
*attributes
,
226 ::CssmDataContainer
*data
)
229 attributes
->deleteValues();
234 // @@@ Fix the const_cast here.
235 check(CSSM_DL_DataGetFromUniqueRecordId(handle(), uniqueId
,
242 // Passthrough functions (only implemented by AppleCSPDL).
247 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_LOCK
, NULL
, NULL
));
253 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, NULL
, NULL
));
257 DbImpl::unlock(const CSSM_DATA
&password
)
259 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, &password
, NULL
));
263 DbImpl::getSettings(uint32
&outIdleTimeout
, bool &outLockOnSleep
)
265 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS_PTR settings
;
266 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_GET_SETTINGS
,
267 NULL
, reinterpret_cast<void **>(&settings
)));
268 outIdleTimeout
= settings
->idleTimeout
;
269 outLockOnSleep
= settings
->lockOnSleep
;
270 allocator().free(settings
);
274 DbImpl::setSettings(uint32 inIdleTimeout
, bool inLockOnSleep
)
276 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS settings
;
277 settings
.idleTimeout
= inIdleTimeout
;
278 settings
.lockOnSleep
= inLockOnSleep
;
279 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_SET_SETTINGS
, &settings
, NULL
));
285 CSSM_APPLECSPDL_DB_IS_LOCKED_PARAMETERS_PTR params
;
286 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_IS_LOCKED
,
287 NULL
, reinterpret_cast<void **>(¶ms
)));
288 bool isLocked
= params
->isLocked
;
289 allocator().free(params
);
294 DbImpl::changePassphrase(const CSSM_ACCESS_CREDENTIALS
*cred
)
296 CSSM_APPLECSPDL_DB_CHANGE_PASSWORD_PARAMETERS params
;
297 params
.accessCredentials
= const_cast<CSSM_ACCESS_CREDENTIALS
*>(cred
);
298 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_CHANGE_PASSWORD
, ¶ms
, NULL
));
306 DbImpl::newDbCursor(const CSSM_QUERY
&query
, CssmAllocator
&allocator
)
308 return new DbDbCursorImpl(Db(this), query
, allocator
);
312 DbImpl::newDbCursor(uint32 capacity
, CssmAllocator
&allocator
)
314 return new DbDbCursorImpl(Db(this), capacity
, allocator
);
318 // DbUniqueRecordMaker
321 DbImpl::newDbUniqueRecord()
323 return new DbUniqueRecordImpl(Db(this));
331 DbImpl::dlDbIdentifier() const
333 return DLDbIdentifier(dl()->subserviceUid(), name(), dbLocation());
340 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, const CSSM_QUERY
&query
, CssmAllocator
&allocator
)
341 : DbCursorImpl(db
, query
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
345 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, uint32 capacity
, CssmAllocator
&allocator
)
346 : DbCursorImpl(db
, capacity
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
350 DbDbCursorImpl::~DbDbCursorImpl()
360 DbDbCursorImpl::next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
)
363 attributes
->deleteValues();
370 DbUniqueRecord
unique(db
);
373 result
= CSSM_DL_DataGetFirst(db
->handle(),
379 if (result
== CSSM_OK
)
384 result
= CSSM_DL_DataGetNext(db
->handle(),
391 if (result
== CSSMERR_DL_ENDOFDATA
)
399 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
406 DbDbCursorImpl::activate()
411 DbDbCursorImpl::deactivate()
416 check(CSSM_DL_DataAbortQuery(database()->handle(), mResultsHandle
));
424 DbCursorImpl::DbCursorImpl(const Object
&parent
, const CSSM_QUERY
&query
, CssmAllocator
&allocator
) :
425 ObjectImpl(parent
), CssmAutoQuery(query
, allocator
)
429 DbCursorImpl::DbCursorImpl(const Object
&parent
, uint32 capacity
, CssmAllocator
&allocator
) :
430 ObjectImpl(parent
), CssmAutoQuery(capacity
, allocator
)
435 DbCursorImpl::allocator() const
437 return ObjectImpl::allocator();
441 DbCursorImpl::allocator(CssmAllocator
&alloc
)
443 ObjectImpl::allocator(alloc
);
450 DbUniqueRecordImpl::DbUniqueRecordImpl(const Db
&db
) : ObjectImpl(db
)
454 DbUniqueRecordImpl::~DbUniqueRecordImpl()
464 DbUniqueRecordImpl::deleteRecord()
466 check(CSSM_DL_DataDelete(database()->handle(), mUniqueId
));
470 DbUniqueRecordImpl::modify(CSSM_DB_RECORDTYPE recordType
,
471 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
472 const CSSM_DATA
*data
,
473 CSSM_DB_MODIFY_MODE modifyMode
)
475 check(CSSM_DL_DataModify(database()->handle(), recordType
, mUniqueId
,
481 DbUniqueRecordImpl::get(DbAttributes
*attributes
,
482 ::CssmDataContainer
*data
)
485 attributes
->deleteValues();
490 // @@@ Fix the allocators for attributes and data.
491 check(CSSM_DL_DataGetFromUniqueRecordId(database()->handle(), mUniqueId
,
497 DbUniqueRecordImpl::activate()
503 DbUniqueRecordImpl::deactivate()
508 check(CSSM_DL_FreeUniqueRecord(database()->handle(), mUniqueId
));
516 DbAttributes::DbAttributes()
517 : CssmAutoDbRecordAttributeData(0, CssmAllocator::standard(), CssmAllocator::standard())
521 DbAttributes::DbAttributes(const Db
&db
, uint32 capacity
, CssmAllocator
&allocator
)
522 : CssmAutoDbRecordAttributeData(capacity
, db
->allocator(), allocator
)