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 // This call does not require the receiver to be active.
140 check(CSSM_DL_DbDelete(dl()->handle(), name(), dbLocation(),
141 mAccessCredentials
));
145 DbImpl::authenticate(CSSM_DB_ACCESS_TYPE inAccessRequest
,
146 const CSSM_ACCESS_CREDENTIALS
*inAccessCredentials
)
150 // XXX Could do the same for create but this would require sticking
151 // inAccessCredentials into mResourceControlContext.
154 // We were not yet active. Just do an open.
155 accessRequest(inAccessRequest
);
156 accessCredentials(inAccessCredentials
);
162 check(CSSM_DL_Authenticate(handle(), inAccessRequest
, inAccessCredentials
));
166 DbImpl::name(char *&outDbName
)
168 check(CSSM_DL_GetDbNameFromHandle(handle(), &outDbName
));
172 DbImpl::createRelation(CSSM_DB_RECORDTYPE inRelationID
,
173 const char *inRelationName
,
174 uint32 inNumberOfAttributes
,
175 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*pAttributeInfo
,
176 uint32 inNumberOfIndexes
,
177 const CSSM_DB_SCHEMA_INDEX_INFO
*pIndexInfo
)
179 check(CSSM_DL_CreateRelation(handle(), inRelationID
, inRelationName
,
180 inNumberOfAttributes
, pAttributeInfo
,
181 inNumberOfIndexes
, pIndexInfo
));
185 DbImpl::destroyRelation(CSSM_DB_RECORDTYPE inRelationID
)
187 check(CSSM_DL_DestroyRelation(handle(), inRelationID
));
191 DbImpl::insert(CSSM_DB_RECORDTYPE recordType
, const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
192 const CSSM_DATA
*data
)
194 DbUniqueRecord
uniqueId(Db(this));
195 check(CSSM_DL_DataInsert(handle(), recordType
,
198 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
199 uniqueId
->activate();
204 // @@@ These methods have been moved to DbUniqueRecord.
206 DbImpl::deleteRecord(const DbUniqueRecord
&uniqueId
)
208 check(CSSM_DL_DataDelete(handle(), uniqueId
));
212 DbImpl::modify(CSSM_DB_RECORDTYPE recordType
, DbUniqueRecord
&uniqueId
,
213 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
214 const CSSM_DATA
*data
,
215 CSSM_DB_MODIFY_MODE modifyMode
)
217 check(CSSM_DL_DataModify(handle(), recordType
, uniqueId
,
223 DbImpl::get(const DbUniqueRecord
&uniqueId
, DbAttributes
*attributes
,
224 ::CssmDataContainer
*data
)
227 attributes
->deleteValues();
232 // @@@ Fix the const_cast here.
233 check(CSSM_DL_DataGetFromUniqueRecordId(handle(), uniqueId
,
240 // Passthrough functions (only implemented by AppleCSPDL).
245 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_LOCK
, NULL
, NULL
));
251 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, NULL
, NULL
));
255 DbImpl::unlock(const CSSM_DATA
&password
)
257 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, &password
, NULL
));
261 DbImpl::getSettings(uint32
&outIdleTimeout
, bool &outLockOnSleep
)
263 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS_PTR settings
;
264 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_GET_SETTINGS
,
265 NULL
, reinterpret_cast<void **>(&settings
)));
266 outIdleTimeout
= settings
->idleTimeout
;
267 outLockOnSleep
= settings
->lockOnSleep
;
268 allocator().free(settings
);
272 DbImpl::setSettings(uint32 inIdleTimeout
, bool inLockOnSleep
)
274 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS settings
;
275 settings
.idleTimeout
= inIdleTimeout
;
276 settings
.lockOnSleep
= inLockOnSleep
;
277 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_SET_SETTINGS
, &settings
, NULL
));
283 CSSM_APPLECSPDL_DB_IS_LOCKED_PARAMETERS_PTR params
;
284 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_IS_LOCKED
,
285 NULL
, reinterpret_cast<void **>(¶ms
)));
286 bool isLocked
= params
->isLocked
;
287 allocator().free(params
);
292 DbImpl::changePassphrase(const CSSM_ACCESS_CREDENTIALS
*cred
)
294 CSSM_APPLECSPDL_DB_CHANGE_PASSWORD_PARAMETERS params
;
295 params
.accessCredentials
= const_cast<CSSM_ACCESS_CREDENTIALS
*>(cred
);
296 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_CHANGE_PASSWORD
, ¶ms
, NULL
));
304 DbImpl::newDbCursor(const CSSM_QUERY
&query
, CssmAllocator
&allocator
)
306 return new DbDbCursorImpl(Db(this), query
, allocator
);
310 DbImpl::newDbCursor(uint32 capacity
, CssmAllocator
&allocator
)
312 return new DbDbCursorImpl(Db(this), capacity
, allocator
);
316 // DbUniqueRecordMaker
319 DbImpl::newDbUniqueRecord()
321 return new DbUniqueRecordImpl(Db(this));
329 DbImpl::dlDbIdentifier() const
331 return DLDbIdentifier(dl()->subserviceUid(), name(), dbLocation());
338 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, const CSSM_QUERY
&query
, CssmAllocator
&allocator
)
339 : DbCursorImpl(db
, query
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
343 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, uint32 capacity
, CssmAllocator
&allocator
)
344 : DbCursorImpl(db
, capacity
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
348 DbDbCursorImpl::~DbDbCursorImpl()
358 DbDbCursorImpl::next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
)
361 attributes
->deleteValues();
368 DbUniqueRecord
unique(db
);
371 result
= CSSM_DL_DataGetFirst(db
->handle(),
377 if (result
== CSSM_OK
)
382 result
= CSSM_DL_DataGetNext(db
->handle(),
389 if (result
== CSSMERR_DL_ENDOFDATA
)
397 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
404 DbDbCursorImpl::activate()
409 DbDbCursorImpl::deactivate()
414 check(CSSM_DL_DataAbortQuery(database()->handle(), mResultsHandle
));
422 DbCursorImpl::DbCursorImpl(const Object
&parent
, const CSSM_QUERY
&query
, CssmAllocator
&allocator
) :
423 ObjectImpl(parent
), CssmAutoQuery(query
, allocator
)
427 DbCursorImpl::DbCursorImpl(const Object
&parent
, uint32 capacity
, CssmAllocator
&allocator
) :
428 ObjectImpl(parent
), CssmAutoQuery(capacity
, allocator
)
436 DbUniqueRecordImpl::DbUniqueRecordImpl(const Db
&db
) : ObjectImpl(db
)
440 DbUniqueRecordImpl::~DbUniqueRecordImpl()
450 DbUniqueRecordImpl::deleteRecord()
452 check(CSSM_DL_DataDelete(database()->handle(), mUniqueId
));
456 DbUniqueRecordImpl::modify(CSSM_DB_RECORDTYPE recordType
,
457 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
458 const CSSM_DATA
*data
,
459 CSSM_DB_MODIFY_MODE modifyMode
)
461 check(CSSM_DL_DataModify(database()->handle(), recordType
, mUniqueId
,
467 DbUniqueRecordImpl::get(DbAttributes
*attributes
,
468 ::CssmDataContainer
*data
)
471 attributes
->deleteValues();
476 // @@@ Fix the allocators for attributes and data.
477 check(CSSM_DL_DataGetFromUniqueRecordId(database()->handle(), mUniqueId
,
483 DbUniqueRecordImpl::activate()
489 DbUniqueRecordImpl::deactivate()
494 check(CSSM_DL_FreeUniqueRecord(database()->handle(), mUniqueId
));
502 DbAttributes::DbAttributes()
503 : CssmAutoDbRecordAttributeData(0, CssmAllocator::standard(), CssmAllocator::standard())
507 DbAttributes::DbAttributes(const Db
&db
, uint32 capacity
, CssmAllocator
&allocator
)
508 : CssmAutoDbRecordAttributeData(capacity
, db
->allocator(), allocator
)