2 * Copyright (c) 2000-2001,2011-2014 Apple 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.h - Security Server database object
22 #ifndef _H_SSDATABASE_
23 #define _H_SSDATABASE_
25 #include <security_cdsa_client/dlclient.h>
26 #include <security_utilities/unix++.h>
27 #include <securityd_client/ssclient.h>
28 #include <securityd_client/ssblob.h>
29 #include <security_utilities/CSPDLTransaction.h>
35 // Protected please ignore this class unless subclassing SSDatabase.
39 class SSDatabaseImpl
: public CssmClient::DbImpl
42 static const char *const DBBlobRelationName
;
43 static const CSSM_DB_RECORDTYPE DBBlobRelationID
=
44 CSSM_DB_RECORDTYPE_APP_DEFINED_START
+ 0x8000;
47 SSDatabaseImpl(SecurityServer::ClientSession
&inClientSession
,
48 const CssmClient::DL
&dl
,
49 const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
);
50 virtual ~SSDatabaseImpl();
52 void ssCreate(const DLDbIdentifier
&dlDbIdentifier
);
53 void ssCreateWithBlob(const DLDbIdentifier
&dlDbIdentifier
, const CSSM_DATA
&blob
);
54 void ssOpen(const DLDbIdentifier
&dlDbIdentifier
);
55 SSUniqueRecord
ssInsert(CSSM_DB_RECORDTYPE recordType
,
56 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
57 const CSSM_DATA
*data
);
58 void authenticate(CSSM_DB_ACCESS_TYPE inAccessRequest
,
59 const CSSM_ACCESS_CREDENTIALS
*inAccessCredentials
);
61 // Passthrough functions (only implemented by AppleCSPDL).
64 void unlock(const CSSM_DATA
&password
);
67 void getSettings(uint32
&outIdleTimeout
, bool &outLockOnSleep
);
68 void setSettings(uint32 inIdleTimeout
, bool inLockOnSleep
);
70 void changePassphrase(const CSSM_ACCESS_CREDENTIALS
*cred
);
71 void ssRecode(const CssmData
&data
, const CssmData
&extraData
);
75 // Attempt to recode this database to the new blob version
76 // Returns new version
77 uint32
recodeDbToVersion(uint32 newBlobVersion
);
79 // Tell securityd that we're done with the upgrade operation
80 void recodeFinished();
82 // Try to take or release the file lock on the underlying database.
83 // You _must_ call these as a pair. They start a transaction on the
84 // underlying DL object, and that transaction is only finished when release
85 // is called. Pass success=true if you want the transaction to commit; otherwise
88 void releaseFileLock(bool success
);
91 // DbUniqueRecordMaker
92 CssmClient::DbUniqueRecordImpl
*newDbUniqueRecord();
94 // New methods not inherited from DbImpl
95 SecurityServer::DbHandle
dbHandle();
97 void getRecordIdentifier(const CSSM_DB_UNIQUE_RECORD_PTR uniqueRecord
, CSSM_DATA
&data
);
98 void ssCopyBlob(CSSM_DATA
& blob
);
100 // Get the version of this database's encoding
101 uint32
dbBlobVersion();
103 // Try to make a backup copy of this database on the filesystem
106 // Try to make a backup copy of this database on the filesystem
107 void makeCopy(const char* path
);
109 // Try to delete the backing file of this database
110 // AFter you've done this, operations might fail in strange ways.
113 // Duplicate this database to this location, and return the clone.
114 // For best results, use on an unlocked SSDatabase, but it should work on a locked one as well.
115 SSDatabase
ssCloneTo(const DLDbIdentifier
& dldbidentifier
);
118 CssmClient::DbUniqueRecord
getDbBlobId(CssmDataContainer
*dbb
= NULL
);
119 void commonCreate (const DLDbIdentifier
&dlDbIdentifier
, bool &autocommit
);
121 // Load the database from disk, but don't talk with securityd about it
122 void load(const DLDbIdentifier
&dlDbIdentifier
);
124 static uint32
getDbVersionFromBlob(const CssmData
& dbb
);
125 uint32
recodeHelper(SecurityServer::DbHandle clonedDbHandle
, CssmClient::DbUniqueRecord
& dbBlobId
);
128 // 5 minute default autolock time
129 static const uint32 kDefaultIdleTimeout
= 5 * 60;
130 static const uint8 kDefaultLockOnSleep
= true;
131 static const unsigned kNumIDWords
= 4;
133 DLDbIdentifier mIdentifier
;
134 UnixPlusPlus::ForkMonitor mForked
;
136 SecurityServer::ClientSession
&mClientSession
;
137 SecurityServer::DbHandle mSSDbHandle
;
139 // Transaction for remembering if we've taken the file lock
140 DLTransaction
* mTransaction
;
145 // SSDatabase -- A Security Server aware Db object.
147 class SSDatabase
: public CssmClient::Db
150 typedef SSDatabaseImpl Impl
;
152 explicit SSDatabase(SSDatabaseImpl
*impl
) : CssmClient::Db(impl
) {}
153 SSDatabase() : CssmClient::Db(NULL
) {}
154 SSDatabase(SecurityServer::ClientSession
&inClientSession
,
155 const CssmClient::DL
&dl
,
156 const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
157 : CssmClient::Db(new SSDatabaseImpl(inClientSession
, dl
, inDbName
, inDbLocation
)) {}
159 SSDatabaseImpl
*operator ->() const { return &impl
<SSDatabaseImpl
>(); }
160 SSDatabaseImpl
&operator *() const { return impl
<SSDatabaseImpl
>(); }
162 // For convinience only
163 SecurityServer::DbHandle
dbHandle() { return (*this) ? (*this)->dbHandle() : SecurityServer::noDb
; }
167 class SSUniqueRecordImpl
: public CssmClient::DbUniqueRecordImpl
170 SSUniqueRecordImpl(const SSDatabase
&db
);
171 virtual ~SSUniqueRecordImpl();
173 SSDatabase
database() const;
177 class SSUniqueRecord
: public CssmClient::DbUniqueRecord
180 typedef SSUniqueRecordImpl Impl
;
182 explicit SSUniqueRecord(SSUniqueRecordImpl
*impl
) : CssmClient::DbUniqueRecord(impl
) {}
183 SSUniqueRecord() : CssmClient::DbUniqueRecord(NULL
) {}
184 SSUniqueRecord(const SSDatabase
&db
) : CssmClient::DbUniqueRecord(new SSUniqueRecordImpl(db
)) {}
186 SSUniqueRecordImpl
*operator ->() const { return &impl
<SSUniqueRecordImpl
>(); }
187 SSUniqueRecordImpl
&operator *() const { return impl
<SSUniqueRecordImpl
>(); }
191 #endif // _H_SSDATABASE_