]> git.saurik.com Git - apple/security.git/blame - securityd/src/database.cpp
Security-57336.10.29.tar.gz
[apple/security.git] / securityd / src / database.cpp
CommitLineData
d8f41ccd
A
1/*
2 * Copyright (c) 2000-2008 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// database - database session management
27//
28#include "database.h"
29#include "agentquery.h"
30#include "key.h"
31#include "server.h"
32#include "session.h"
33#include "notifications.h"
d8f41ccd
A
34#include <securityd_client/dictionary.h>
35#include <security_cdsa_utilities/acl_any.h> // for default owner ACLs
36#include <security_cdsa_client/wrapkey.h>
37#include <security_utilities/endian.h>
38
39using namespace UnixPlusPlus;
40
41
42//
43// DbCommon basics
44//
45DbCommon::DbCommon(Session &session)
46{
47 referent(session);
48}
49
50Session &DbCommon::session() const
51{
52 return referent<Session>();
53}
54
55
56//
57// Database basics
58//
59Database::Database(Process &proc)
60{
61 referent(proc);
62}
63
64
65Process& Database::process() const
66{
67 return referent<Process>();
68}
69
70
71//
72// Send a keychain-related notification event about this database
73//
74void DbCommon::notify(NotificationEvent event, const DLDbIdentifier &ident)
75{
76 // form the data (encoded DLDbIdentifier)
77 NameValueDictionary nvd;
78 NameValueDictionary::MakeNameValueDictionaryFromDLDbIdentifier(ident, nvd);
79 CssmData data;
80 nvd.Export(data);
81
82 // inject notification into Security event system
83 Listener::notify(kNotificationDomainDatabase, event, data);
84
85 // clean up
86 free (data.data());
87}
88
89
90//
91// Default behaviors
92//
93void DbCommon::sleepProcessing()
94{
95 // nothing
96}
97
98void DbCommon::lockProcessing()
99{
100 // nothing
101}
102
103bool DbCommon::belongsToSystem() const
104{
105 return false;
106}
107
108
109void Database::releaseKey(Key &key)
110{
111 kill(key);
112}
113
114void Database::releaseSearch(Search &search)
115{
116 kill(search);
117}
118
119void Database::releaseRecord(Record &record)
120{
121 kill(record);
122}
123
124void Database::dbName(const char *name)
125{
126 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
127}
128
129
130//
131// Functions that aren't implemented at the Database level but can stay that way
132//
133void Database::findFirst(const CssmQuery &query,
134 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
135 CssmData *data, RefPointer<Key> &key, RefPointer<Search> &search, RefPointer<Record> &record,
136 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength)
137{
138 secdebug("database", "%p calling unimplemented findFirst", this);
139 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
140}
141
142void Database::findNext(Search *search,
143 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
144 CssmData *data, RefPointer<Key> &key, RefPointer<Record> &record,
145 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength)
146{
147 secdebug("database", "%p calling unimplemented findNext", this);
148 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
149}
150
151void Database::findRecordHandle(Record *record,
152 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
153 CssmData *data, RefPointer<Key> &key,
154 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength)
155{
156 secdebug("database", "%p calling unimplemented findRecordHandle", this);
157 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
158}
159
160void Database::insertRecord(CSSM_DB_RECORDTYPE recordtype,
161 const CssmDbRecordAttributeData *attributes, mach_msg_type_number_t inAttributesLength,
162 const CssmData &data, RecordHandle &record)
163{
164 secdebug("database", "%p calling unimplemented insertRecord", this);
165 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
166}
167
168void Database::modifyRecord(CSSM_DB_RECORDTYPE recordtype, Record *record,
169 const CssmDbRecordAttributeData *attributes, mach_msg_type_number_t inAttributesLength,
170 const CssmData *data, CSSM_DB_MODIFY_MODE modifyMode)
171{
172 secdebug("database", "%p calling unimplemented modifyRecord", this);
173 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
174}
175
176void Database::deleteRecord(Database::Record *record)
177{
178 secdebug("database", "%p calling unimplemented deleteRecord", this);
179 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
180}
181
182void Database::authenticate(CSSM_DB_ACCESS_TYPE, const AccessCredentials *)
183{
184 secdebug("database", "%p calling unimplemented authenticate", this);
185 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
186}
187
188SecurityServerAcl &Database::acl()
189{
190 secdebug("database", "%p has no ACL implementation", this);
191 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
192}
193
194bool Database::isLocked()
195{
196 secdebug("database", "%p calling unimplemented isLocked", this);
197 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
198}
199
200
201//
202// SecurityServerAcl personality implementation.
203// This is the trivial (type coding) stuff. The hard stuff is virtually mixed in.
204//
205Database *Database::relatedDatabase()
206{
207 return this;
208}
209
210AclKind Database::aclKind() const
211{
212 return dbAcl;
213}
214
215
216//
217// Remote validation is not, by default, supported
218//
219bool Database::validateSecret(const AclSubject *, const AccessCredentials *)
220{
221 return false;
222}
223
224
225//
226// Implementation of a "system keychain unlock key store"
227//
228SystemKeychainKey::SystemKeychainKey(const char *path)
229 : mPath(path), mValid(false)
230{
231 // explicitly set up a key header for a raw 3DES key
232 CssmKey::Header &hdr = mKey.header();
233 hdr.blobType(CSSM_KEYBLOB_RAW);
234 hdr.blobFormat(CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING);
235 hdr.keyClass(CSSM_KEYCLASS_SESSION_KEY);
236 hdr.algorithm(CSSM_ALGID_3DES_3KEY_EDE);
237 hdr.KeyAttr = 0;
238 hdr.KeyUsage = CSSM_KEYUSE_ANY;
239 mKey = CssmData::wrap(mBlob.masterKey);
240}
241
242SystemKeychainKey::~SystemKeychainKey()
243{
244}
245
246bool SystemKeychainKey::matches(const DbBlob::Signature &signature)
247{
248 return update() && signature == mBlob.signature;
249}
250
251bool SystemKeychainKey::update()
252{
253 // if we checked recently, just assume it's okay
254 if (mValid && mUpdateThreshold > Time::now())
255 return mValid;
256
257 // check the file
258 struct stat st;
259 if (::stat(mPath.c_str(), &st)) {
260 // something wrong with the file; can't use it
261 mUpdateThreshold = Time::now() + Time::Interval(checkDelay);
262 return mValid = false;
263 }
264 if (mValid && Time::Absolute(st.st_mtimespec) == mCachedDate)
265 return true;
266 mUpdateThreshold = Time::now() + Time::Interval(checkDelay);
267
268 try {
269 secdebug("syskc", "reading system unlock record from %s", mPath.c_str());
270 AutoFileDesc fd(mPath, O_RDONLY);
271 if (fd.read(mBlob) != sizeof(mBlob))
272 return false;
273 if (mBlob.isValid()) {
274 mCachedDate = st.st_mtimespec;
275 return mValid = true;
276 } else
277 return mValid = false;
278 } catch (...) {
279 secdebug("syskc", "system unlock record not available");
280 return false;
281 }
282}