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
23 #ifndef _H_CDSA_CLIENT_DLCLIENT
24 #define _H_CDSA_CLIENT_DLCLIENT 1
26 #include <Security/cssmclient.h>
27 #include <Security/DLDBList.h>
28 #include <Security/cssmacl.h>
29 #include <Security/cssmdb.h>
30 #include <Security/cssmdata.h>
39 #define CSSM_DB_ATTR(ATTR) ATTR
40 #define CSSM_DB_ATTR_SCHEMA(ATTR) ATTR ## Schema
42 #define CSSM_DB_INDEX(ATTR) ATTR ## Index
43 #define CSSM_DB_UNIQUE(ATTR) ATTR ## Unique
46 // Helper macro for declaring and defining a Db index unique and non-unique attributes
48 #define CSSM_DB_INDEX_DECL(ATTR) static const CSSM_DB_INDEX_INFO CSSM_DB_INDEX(ATTR)
49 #define CSSM_DB_UNIQUE_DECL(ATTR) static const CSSM_DB_INDEX_INFO CSSM_DB_UNIQUE(ATTR)
53 // Use this macro for defining a non-unique attribute
55 #define CSSM_DB_INDEX_DEF(ATTR) \
56 const CSSM_DB_INDEX_INFO CSSM_DB_INDEX(ATTR) = \
58 CSSM_DB_INDEX_NONUNIQUE, \
59 CSSM_DB_INDEX_ON_ATTRIBUTE, \
64 // Use this macro for defining a unique attribute
67 #define CSSM_DB_UNIQUE_DEF(ATTR) \
68 const CSSM_DB_INDEX_INFO CSSM_DB_UNIQUE(ATTR) = \
70 CSSM_DB_INDEX_UNIQUE, \
71 CSSM_DB_INDEX_ON_ATTRIBUTE, \
78 // Helper macro for declaring and defining a Db schema attributes
79 // Use this macro in your header to declare each attribute you require.
81 #define CSSM_DB_ATTR_DECL(ATTR) \
82 static const CSSM_DB_ATTRIBUTE_INFO CSSM_DB_ATTR(ATTR); \
83 static const CSSM_DB_SCHEMA_ATTRIBUTE_INFO CSSM_DB_ATTR_SCHEMA(ATTR)
86 // Don't directly use this macro use one of the below instead.
88 #define CSSM_DB_ATTR_DEFINE_SCHEMA(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE) \
89 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO CSSM_DB_ATTR_SCHEMA(ATTR) = \
93 { OID_LEN, OID_DATA }, \
94 CSSM_DB_ATTRIBUTE_FORMAT_ ## VALUETYPE \
99 // Use one of the following macros to defined each declared attribute required by your application.
102 // Use this macro to define attributes which are looked up by integer AttributeID.
104 #define CSSM_DB_INTEGER_ATTR(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE) \
105 const CSSM_DB_ATTRIBUTE_INFO ATTR = \
107 CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER, \
109 CSSM_DB_ATTRIBUTE_FORMAT_ ## VALUETYPE \
112 CSSM_DB_ATTR_DEFINE_SCHEMA(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE)
115 // Use this macro to define attributes which are looked up by string AttributeName.
117 #define CSSM_DB_NAME_ATTR(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE) \
118 const CSSM_DB_ATTRIBUTE_INFO ATTR = \
120 CSSM_DB_ATTRIBUTE_NAME_AS_STRING, \
122 CSSM_DB_ATTRIBUTE_FORMAT_ ## VALUETYPE \
125 CSSM_DB_ATTR_DEFINE_SCHEMA(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE)
128 // Use this macro to define attributes which are looked up by OID AttributeNameID.
129 // XXX This does not work yet.
131 #define CSSM_DB_OID_ATTR(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE) \
132 const CSSM_DB_ATTRIBUTE_INFO ATTR = \
134 CSSM_DB_ATTRIBUTE_NAME_AS_OID, \
135 {{OID_LEN, OID_DATA}}, \
136 CSSM_DB_ATTRIBUTE_FORMAT_ ## VALUETYPE \
139 CSSM_DB_ATTR_DEFINE_SCHEMA(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE)
143 // Use this macro to define attributes which are part of the primary key.
145 #define CSSM_DB_PRIMARKEY_ATTR(ATTR, NAME) \
146 const CSSM_DB_ATTRIBUTE_INFO ATTR = \
148 CSSM_DB_INDEX_UNIQUE, \
149 CSSM_DB_INDEX_ON_ATTRIBUTE, \
150 CSSM_DB_ATTRIBUTE_FORMAT_ ## VALUETYPE \
153 CSSM_DB_ATTR_DEFINE_SCHEMA(ATTR, INTEGER, NAME, OID_LEN, OID_DATA, VALUETYPE)
158 // Maker interfaces used by various Impl objects
161 // DbMaker -- someone who can create a new DbImpl.
166 virtual DbImpl
*newDb(const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
) = 0;
169 // DbCursorMaker -- someone who can create a new DbCursorImpl.
174 virtual DbCursorImpl
*newDbCursor(const CSSM_QUERY
&query
, CssmAllocator
&allocator
) = 0;
175 virtual DbCursorImpl
*newDbCursor(uint32 capacity
, CssmAllocator
&allocator
) = 0;
178 // DbUniqueRecordMaker -- someone who can create a new DbUniqueRecordImpl.
179 class DbUniqueRecordImpl
;
180 class DbUniqueRecordMaker
183 virtual DbUniqueRecordImpl
*newDbUniqueRecord() = 0;
190 class DLImpl
: public AttachmentImpl
, public DbMaker
193 DLImpl(const Guid
&guid
);
194 DLImpl(const Module
&module);
197 virtual void getDbNames(char **);
198 virtual void freeNameList(char **);
201 virtual DbImpl
*newDb(const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
);
205 class DL
: public Attachment
210 explicit DL(Impl
*impl
) : Attachment(impl
) {}
211 DL() : Attachment(NULL
) {}
212 DL(const Guid
&guid
) : Attachment(new Impl(guid
)) {}
213 DL(const Module
&module) : Attachment(new Impl(module)) {}
215 Impl
*operator ->() const { return &impl
<Impl
>(); }
216 Impl
&operator *() const { return impl
<Impl
>(); }
218 // Conversion to DbMaker.
219 operator DbMaker
&() const { return impl
<Impl
>(); }
224 class DbUniqueRecord
;
228 // A CSSM_DLDB handle.
229 // Dbs always belong to DLs (DL attachments)
231 class DbImpl
: public ObjectImpl
, public DbCursorMaker
, public DbUniqueRecordMaker
234 DbImpl(const DL
&dl
, const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
);
237 DL
dl() const { return parent
<DL
>(); }
238 Module
module() const { return dl()->module(); }
241 virtual void create();
242 virtual void close();
243 virtual void deleteDb();
244 virtual void authenticate(CSSM_DB_ACCESS_TYPE inAccessRequest
,
245 const CSSM_ACCESS_CREDENTIALS
*inAccessCredentials
);
246 virtual void name(char *&outName
); // CSSM_DL_GetDbNameFromHandle()
248 virtual void createRelation(CSSM_DB_RECORDTYPE inRelationID
,
249 const char *inRelationName
,
250 uint32 inNumberOfAttributes
,
251 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*pAttributeInfo
,
252 uint32 inNumberOfIndexes
,
253 const CSSM_DB_SCHEMA_INDEX_INFO
*pIndexInfo
);
254 virtual void destroyRelation(CSSM_DB_RECORDTYPE inRelationID
);
256 virtual DbUniqueRecord
insert(CSSM_DB_RECORDTYPE recordType
,
257 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
258 const CSSM_DATA
*data
);
261 // @@@ These methods have been moved to DbUniqueRecord.
262 virtual void deleteRecord(const DbUniqueRecord
&uniqueId
);
263 virtual void modify(CSSM_DB_RECORDTYPE recordType
, DbUniqueRecord
&uniqueId
,
264 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
265 const CSSM_DATA
*data
,
266 CSSM_DB_MODIFY_MODE modifyMode
);
267 virtual void get(const DbUniqueRecord
&uniqueId
, DbAttributes
*attributes
,
268 ::CssmDataContainer
*data
);
271 const CSSM_DL_DB_HANDLE
&handle() { activate(); return mHandle
; }
273 const DbName
&dbName() { return mDbName
; }
274 void dbName(const DbName
&dbName
) { mDbName
= dbName
; }
276 const char *name() const { return mDbName
.dbName().c_str(); }
277 const CSSM_NET_ADDRESS
*dbLocation() const { return mDbName
.dbLocation(); }
279 CSSM_DB_ACCESS_TYPE
accessRequest() const { return mAccessRequest
; }
280 void accessRequest(CSSM_DB_ACCESS_TYPE inAccessRequest
)
281 { mAccessRequest
= inAccessRequest
; }
283 const CSSM_ACCESS_CREDENTIALS
*accessCredentials() const
284 { return mAccessCredentials
; }
285 void accessCredentials(const CSSM_ACCESS_CREDENTIALS
*inAccessCredentials
)
286 { mAccessCredentials
= inAccessCredentials
; }
288 const void *openParameters() const { return mOpenParameters
; }
289 void openParameters(const void *inOpenParameters
)
290 { mOpenParameters
= inOpenParameters
; }
292 const CSSM_DBINFO
*dbInfo() const { return mDbInfo
; }
293 void dbInfo(const CSSM_DBINFO
*inDbInfo
) { mDbInfo
= inDbInfo
; }
295 const CSSM_RESOURCE_CONTROL_CONTEXT
*resourceControlContext() const
296 { return mResourceControlContext
; }
297 void resourceControlContext(const CSSM_RESOURCE_CONTROL_CONTEXT
*inResourceControlContext
)
298 { mResourceControlContext
= inResourceControlContext
; }
300 // Passthrough functions (only implemented by AppleCSPDL).
302 virtual void unlock();
303 virtual void unlock(const CSSM_DATA
&password
);
304 virtual void getSettings(uint32
&outIdleTimeout
, bool &outLockOnSleep
);
305 virtual void setSettings(uint32 inIdleTimeout
, bool inLockOnSleep
);
306 virtual bool isLocked();
307 virtual void changePassphrase(const CSSM_ACCESS_CREDENTIALS
*cred
);
310 virtual DLDbIdentifier
dlDbIdentifier() const;
313 virtual DbCursorImpl
*newDbCursor(const CSSM_QUERY
&query
, CssmAllocator
&allocator
);
314 virtual DbCursorImpl
*newDbCursor(uint32 capacity
, CssmAllocator
&allocator
);
316 // DbUniqueRecordMaker
317 virtual DbUniqueRecordImpl
*newDbUniqueRecord();
320 virtual void activate();
321 virtual void deactivate();
324 CSSM_DL_DB_HANDLE mHandle
; // CSSM DLDB handle
327 CSSM_DB_ACCESS_TYPE mAccessRequest
;
328 const CSSM_ACCESS_CREDENTIALS
*mAccessCredentials
;
329 const void *mOpenParameters
;
331 // Arguments to create
332 const CSSM_DBINFO
*mDbInfo
;
333 const CSSM_RESOURCE_CONTROL_CONTEXT
*mResourceControlContext
;
337 class Db
: public Object
342 explicit Db(Impl
*impl
) : Object(impl
) {}
343 Db() : Object(NULL
) {}
344 Db(DbMaker
&maker
, const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
= NULL
)
345 : Object(maker
.newDb(inDbName
, inDbLocation
)) {}
347 Impl
*operator ->() const { return &impl
<Impl
>(); }
348 Impl
&operator *() const { return impl
<Impl
>(); }
350 // Conversion to DbCursorMaker.
351 operator DbCursorMaker
&() const { return impl
<Impl
>(); }
352 // Conversion to DbUniqueRecordMaker.
353 operator DbUniqueRecordMaker
&() const { return impl
<Impl
>(); }
360 // This class is still abstract. You must subclass it in order to be able to instantiate an instance.
361 class DbCursorImpl
: public ObjectImpl
, public CssmAutoQuery
364 DbCursorImpl(const Object
&parent
, const CSSM_QUERY
&query
, CssmAllocator
&allocator
);
365 DbCursorImpl(const Object
&parent
, uint32 capacity
, CssmAllocator
&allocator
);
367 virtual CssmAllocator
&allocator() const;
368 virtual void allocator(CssmAllocator
&alloc
);
370 virtual bool next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
) = 0;
371 void abort() { deactivate(); }
374 class DbCursor
: public Object
377 typedef DbCursorImpl Impl
;
379 explicit DbCursor(Impl
*impl
) : Object(impl
) {}
380 DbCursor() : Object(NULL
) {}
381 DbCursor(DbCursorMaker
&maker
, const CSSM_QUERY
&query
,
382 CssmAllocator
&allocator
= CssmAllocator::standard())
383 : Object(maker
.newDbCursor(query
, allocator
)) {}
384 DbCursor(DbCursorMaker
&maker
, uint32 capacity
= 0,
385 CssmAllocator
&allocator
= CssmAllocator::standard())
386 : Object(maker
.newDbCursor(capacity
, allocator
)) {}
388 Impl
*operator ->() const { return &impl
<Impl
>(); }
389 Impl
&operator *() const { return impl
<Impl
>(); }
396 class DbUniqueRecordImpl
: public ObjectImpl
399 DbUniqueRecordImpl(const Db
&db
);
400 virtual ~DbUniqueRecordImpl();
402 virtual void deleteRecord();
403 virtual void modify(CSSM_DB_RECORDTYPE recordType
,
404 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
405 const CSSM_DATA
*data
,
406 CSSM_DB_MODIFY_MODE modifyMode
);
407 virtual void get(DbAttributes
*attributes
, ::CssmDataContainer
*data
);
409 Db
database() const { return parent
<Db
>(); }
411 void free() { deactivate(); }
413 // Client must call activate() after calling this function if mUniqueId is successfully set.
414 operator CSSM_DB_UNIQUE_RECORD_PTR
*() { if (mActive
) free(); return &mUniqueId
; }
416 operator CSSM_DB_UNIQUE_RECORD
*() { return mUniqueId
; }
417 operator const CSSM_DB_UNIQUE_RECORD
*() const { return mUniqueId
; }
424 CSSM_DB_UNIQUE_RECORD_PTR mUniqueId
;
427 class DbUniqueRecord
: public Object
430 typedef DbUniqueRecordImpl Impl
;
432 explicit DbUniqueRecord(Impl
*impl
) : Object(impl
) {}
433 DbUniqueRecord() : Object(NULL
) {}
434 DbUniqueRecord(DbUniqueRecordMaker
&maker
) : Object(maker
.newDbUniqueRecord()) {}
436 Impl
*operator ->() { return &impl
<Impl
>(); }
437 Impl
&operator *() { return impl
<Impl
>(); }
438 const Impl
&operator *() const { return impl
<Impl
>(); }
440 // Conversion operators must be here.
442 // Client must activate after calling this function if mUniqueId is successfully set.
443 operator CSSM_DB_UNIQUE_RECORD_PTR
*() { return **this; }
445 operator CSSM_DB_UNIQUE_RECORD
*() { return **this; }
446 operator const CSSM_DB_UNIQUE_RECORD
*() const { return **this; }
453 class DbAttributes
: public CssmAutoDbRecordAttributeData
457 DbAttributes(const Db
&db
, uint32 capacity
= 0, CssmAllocator
&allocator
= CssmAllocator::standard());
462 // DbDbCursor -- concrete subclass of DbCursorImpl for querying Db's
464 class DbDbCursorImpl
: public DbCursorImpl
467 DbDbCursorImpl(const Db
&db
, const CSSM_QUERY
&query
, CssmAllocator
&allocator
);
468 DbDbCursorImpl(const Db
&db
, uint32 capacity
, CssmAllocator
&allocator
);
469 virtual ~DbDbCursorImpl();
471 bool next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
);
474 Db
database() { return parent
<Db
>(); }
480 CSSM_HANDLE mResultsHandle
;
483 } // end namespace CssmClient
485 } // end namespace Security
487 #endif // _H_CDSA_CLIENT_DLCLIENT