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 // SSDatabase.cpp - Security Server database object
22 #include "SSDatabase.h"
24 #include "KeySchema.h"
26 using namespace CssmClient
;
27 using namespace SecurityServer
;
29 const char *const SSDatabaseImpl::DBBlobRelationName
= "DBBlob";
32 SSDatabaseImpl::SSDatabaseImpl(ClientSession
&inClientSession
,
33 const CssmClient::DL
&dl
,
34 const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
35 : Db::Impl(dl
, inDbName
, inDbLocation
), mClientSession(inClientSession
), mSSDbHandle(noDb
)
39 SSDatabaseImpl::~SSDatabaseImpl()
41 if (mSSDbHandle
!= noDb
)
42 mClientSession
.releaseDb(mSSDbHandle
);
46 SSDatabaseImpl::insert(CSSM_DB_RECORDTYPE recordType
,
47 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
48 const CSSM_DATA
*data
, bool)
50 SSUniqueRecord
uniqueId(SSDatabase(this));
51 check(CSSM_DL_DataInsert(handle(), recordType
,
54 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
60 SSDatabaseImpl::lock()
62 mClientSession
.lock(dbHandle());
67 SSDatabaseImpl::unlock()
69 mClientSession
.unlock(dbHandle());
73 SSDatabaseImpl::unlock(const CSSM_DATA
&password
)
75 mClientSession
.unlock(dbHandle(), CssmData::overlay(password
));
79 SSDatabaseImpl::getSettings(uint32
&outIdleTimeout
, bool &outLockOnSleep
)
81 DBParameters parameters
;
82 mClientSession
.getDbParameters(dbHandle(), parameters
);
83 outIdleTimeout
= parameters
.idleTimeout
;
84 outLockOnSleep
= parameters
.lockOnSleep
;
88 SSDatabaseImpl::setSettings(uint32 inIdleTimeout
, bool inLockOnSleep
)
90 DBParameters parameters
;
91 parameters
.idleTimeout
= inIdleTimeout
;
92 parameters
.lockOnSleep
= inLockOnSleep
;
93 mClientSession
.setDbParameters(dbHandle(), parameters
);
95 // Reencode the db blob.
96 CssmDataContainer
dbb(allocator());
97 mClientSession
.encodeDb(mSSDbHandle
, dbb
, allocator());
98 mDbBlobId
->modify(DBBlobRelationID
, NULL
, &dbb
, CSSM_DB_MODIFY_ATTRIBUTE_NONE
);
102 SSDatabaseImpl::isLocked()
104 return mClientSession
.isLocked(dbHandle());
108 SSDatabaseImpl::changePassphrase(const CSSM_ACCESS_CREDENTIALS
*cred
)
110 mClientSession
.changePassphrase(dbHandle(), AccessCredentials::overlay(cred
));
112 // Reencode the db blob.
113 CssmDataContainer
dbb(allocator());
114 mClientSession
.encodeDb(mSSDbHandle
, dbb
, allocator());
115 mDbBlobId
->modify(DBBlobRelationID
, NULL
, &dbb
, CSSM_DB_MODIFY_ATTRIBUTE_NONE
);
119 SSDatabaseImpl::dbHandle()
126 SSDatabaseImpl::create(const DLDbIdentifier
&dlDbIdentifier
)
132 // @@@ The CSSM_DB_SCHEMA_ATTRIBUTE_INFO and CSSM_DB_SCHEMA_INDEX_INFO
133 // arguments should be optional.
134 createRelation(DBBlobRelationID
, DBBlobRelationName
,
135 0, (CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*)42,
136 0, (CSSM_DB_SCHEMA_INDEX_INFO
*)42);
138 // @@@ Only iff not already in mDbInfo
139 createRelation(CSSM_DL_DB_RECORD_PUBLIC_KEY
, "CSSM_DL_DB_RECORD_PUBLIC_KEY",
140 KeySchema::KeySchemaAttributeCount
, KeySchema::KeySchemaAttributeList
,
141 KeySchema::KeySchemaIndexCount
, KeySchema::KeySchemaIndexList
);
143 // @@@ Only iff not already in mDbInfo
144 createRelation(CSSM_DL_DB_RECORD_PRIVATE_KEY
, "CSSM_DL_DB_RECORD_PRIVATE_KEY",
145 KeySchema::KeySchemaAttributeCount
, KeySchema::KeySchemaAttributeList
,
146 KeySchema::KeySchemaIndexCount
, KeySchema::KeySchemaIndexList
);
148 // @@@ Only iff not already in mDbInfo
149 createRelation(CSSM_DL_DB_RECORD_SYMMETRIC_KEY
, "CSSM_DL_DB_RECORD_SYMMETRIC_KEY",
150 KeySchema::KeySchemaAttributeCount
, KeySchema::KeySchemaAttributeList
,
151 KeySchema::KeySchemaIndexCount
, KeySchema::KeySchemaIndexList
);
153 DBParameters dbParameters
;
154 memset(&dbParameters
, 0, sizeof(DBParameters
));
155 dbParameters
.idleTimeout
= kDefaultIdleTimeout
;
156 dbParameters
.lockOnSleep
= kDefaultLockOnSleep
;
158 const AccessCredentials
*cred
= NULL
;
159 const AclEntryInput
*owner
= NULL
;
160 if (resourceControlContext())
162 cred
= AccessCredentials::overlay(resourceControlContext()->AccessCred
);
163 owner
= &AclEntryInput::overlay(resourceControlContext()->InitialAclEntry
);
165 mSSDbHandle
= mClientSession
.createDb(dlDbIdentifier
, cred
, owner
, dbParameters
);
166 CssmDataContainer
dbb(allocator());
167 mClientSession
.encodeDb(mSSDbHandle
, dbb
, allocator());
168 mDbBlobId
= Db::Impl::insert(DBBlobRelationID
, NULL
, &dbb
);
178 SSDatabaseImpl::open(const DLDbIdentifier
&dlDbIdentifier
)
182 DbCursor
cursor(SSDatabase(this));
183 cursor
->recordType(DBBlobRelationID
);
184 CssmDataContainer
dbb(allocator());
185 if (!cursor
->next(NULL
, &dbb
, mDbBlobId
))
186 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT
);
188 mSSDbHandle
= mClientSession
.decodeDb(dlDbIdentifier
, AccessCredentials::overlay(accessCredentials()), dbb
);
192 SSDatabaseImpl::newDbUniqueRecord()
194 return new SSUniqueRecordImpl(SSDatabase(this));
198 SSUniqueRecordImpl::SSUniqueRecordImpl(const SSDatabase
&db
)
199 : DbUniqueRecord::Impl(db
)
203 SSUniqueRecordImpl::~SSUniqueRecordImpl()
208 SSUniqueRecordImpl::database() const
210 return parent
<SSDatabase
>();