]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/MetaRecord.cpp
cc84191d5910059578ddb30b1ad19799c66314b6
[apple/security.git] / cdsa / cdsa_utilities / MetaRecord.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // MetaRecord.cpp
21 //
22
23 #include "MetaRecord.h"
24
25 MetaRecord::MetaRecord(CSSM_DB_RECORDTYPE inRecordType) :
26 mRecordType(inRecordType)
27 {
28 }
29
30 MetaRecord::MetaRecord(const CSSM_DB_RECORD_ATTRIBUTE_INFO &inInfo)
31 : mRecordType(inInfo.DataRecordType)
32 {
33 try
34 {
35 setRecordAttributeInfo(inInfo);
36 }
37 catch (...)
38 {
39 for_each_delete(mAttributeVector.begin(), mAttributeVector.end());
40 }
41 }
42
43 MetaRecord::MetaRecord(CSSM_DB_RECORDTYPE inRelationID,
44 uint32 inNumberOfAttributes,
45 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO *inAttributeInfo) :
46 mRecordType(inRelationID)
47 {
48 // XXX Is there any particular reason not to allow this?
49 #if 0
50 if (inNumberOfAttributes == 0 || inAttributeInfo == NULL)
51 CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_NUM_ATTRIBUTES);
52 #endif
53
54 try {
55 for (uint32 anIndex = 0; anIndex < inNumberOfAttributes; anIndex++)
56 {
57 string aName;
58 if (inAttributeInfo[anIndex].AttributeName)
59 aName = string(inAttributeInfo[anIndex].AttributeName);
60
61 const CssmData *aNameID = NULL;
62 if (inAttributeInfo[anIndex].AttributeNameID.Length > 0)
63 aNameID = &CssmData::overlay(inAttributeInfo[anIndex].AttributeNameID);
64
65 uint32 aNumber = inAttributeInfo[anIndex].AttributeId;
66 createAttribute(
67 inAttributeInfo[anIndex].AttributeName ? &aName : NULL,
68 aNameID, aNumber,
69 inAttributeInfo[anIndex].DataType);
70 }
71 }
72 catch (...)
73 {
74 for_each_delete(mAttributeVector.begin(), mAttributeVector.end());
75 }
76 }
77
78 MetaRecord::~MetaRecord()
79 {
80 for_each_delete(mAttributeVector.begin(), mAttributeVector.end());
81 }
82
83 void
84 MetaRecord::setRecordAttributeInfo(const CSSM_DB_RECORD_ATTRIBUTE_INFO &inInfo)
85 {
86 // XXX Is there any particular reason not to allow this?
87 #if 0
88 if (inInfo.NumberOfAttributes == 0 || inInfo.AttributeInfo == NULL)
89 CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_NUM_ATTRIBUTES);
90 #endif
91
92 for (uint32 anIndex = 0; anIndex < inInfo.NumberOfAttributes; anIndex++)
93 {
94 switch (inInfo.AttributeInfo[anIndex].AttributeNameFormat)
95 {
96 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING:
97 {
98 string aName(inInfo.AttributeInfo[anIndex].Label.AttributeName);
99 createAttribute(&aName, nil, anIndex,
100 inInfo.AttributeInfo[anIndex].AttributeFormat);
101 break;
102 }
103 case CSSM_DB_ATTRIBUTE_NAME_AS_OID:
104 {
105 const CssmData &aNameID = CssmOid::overlay(inInfo.AttributeInfo[anIndex].Label.AttributeOID);
106 createAttribute(nil, &aNameID, anIndex,
107 inInfo.AttributeInfo[anIndex].AttributeFormat);
108 break;
109 }
110 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER:
111 {
112 uint32 aNumber = inInfo.AttributeInfo[anIndex].Label.AttributeID;
113 createAttribute(nil, nil, aNumber,
114 inInfo.AttributeInfo[anIndex].AttributeFormat);
115 break;
116 }
117 default:
118 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME);
119 break;
120 }
121 }
122 }
123
124 void
125 MetaRecord::createAttribute(const string *inAttributeName,
126 const CssmOid *inAttributeOID,
127 uint32 inAttributeID,
128 CSSM_DB_ATTRIBUTE_FORMAT inAttributeFormat)
129 {
130 // Index of new element is current size of vector
131 uint32 anAttributeIndex = mAttributeVector.size();
132 bool aInsertedAttributeName = false;
133 bool aInsertedAttributeOID = false;
134 bool aInsertedAttributeID = false;
135
136 if (inAttributeName)
137 {
138 if (!mNameStringMap.insert(NameStringMap::value_type(*inAttributeName, anAttributeIndex)).second)
139 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE);
140 aInsertedAttributeName = true;
141 }
142 try
143 {
144 if (inAttributeOID)
145 {
146 if (!mNameOIDMap.insert(NameOIDMap::value_type(*inAttributeOID, anAttributeIndex)).second)
147 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE);
148 aInsertedAttributeOID = true;
149 }
150
151 if (!mNameIntMap.insert(NameIntMap::value_type(inAttributeID, anAttributeIndex)).second)
152 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE);
153 aInsertedAttributeID = true;
154
155 // Note: this no longer throws INVALID_FIELD_NAME since the attribute will always have
156 // an attribute ID by which it is known
157
158 mAttributeVector.push_back(MetaAttribute::create(inAttributeFormat,
159 anAttributeIndex, inAttributeID));
160 }
161 catch(...)
162 {
163 if (aInsertedAttributeName)
164 mNameStringMap.erase(*inAttributeName);
165 if (aInsertedAttributeOID)
166 mNameOIDMap.erase(*inAttributeOID);
167 if (inAttributeID)
168 mNameIntMap.erase(inAttributeID);
169
170 throw;
171 }
172 }
173
174
175 // Create a packed record from the given inputs.
176 void
177 MetaRecord::packRecord(WriteSection &inWriteSection,
178 const CSSM_DB_RECORD_ATTRIBUTE_DATA *inAttributes,
179 const CssmData *inData) const
180 {
181 uint32 aDataSize;
182 if (inData)
183 aDataSize = inData->Length;
184 else
185 aDataSize = 0;
186
187 inWriteSection.put(OffsetDataSize, aDataSize);
188 uint32 anOffset = OffsetAttributeOffsets + AtomSize * mAttributeVector.size();
189 if (aDataSize)
190 anOffset = inWriteSection.put(anOffset, aDataSize, inData->Data);
191
192 vector<uint32> aNumValues(mAttributeVector.size(), ~0UL);
193 vector<CSSM_DATA_PTR> aValues(mAttributeVector.size());
194 uint32 anIndex;
195
196 if (inAttributes == NULL)
197 inWriteSection.put(OffsetSemanticInformation, 0);
198 else
199 {
200 inWriteSection.put(OffsetSemanticInformation, inAttributes->SemanticInformation);
201
202 // Put the supplied attribute values into the list of attributes
203 // and values.
204 anIndex = inAttributes->NumberOfAttributes;
205 // Make sure that AttributeData is a valid array.
206 if (anIndex > 0)
207 Required(inAttributes->AttributeData);
208
209 while (anIndex-- > 0)
210 {
211 CSSM_DB_ATTRIBUTE_DATA &anAttribute = inAttributes->AttributeData[anIndex];
212 uint32 anAttributeIndex = attributeIndex(anAttribute.Info);
213 // Make sure that the caller specified the attribute values in the correct format.
214 if (anAttribute.Info.AttributeFormat != mAttributeVector[anAttributeIndex]->attributeFormat())
215 CssmError::throwMe(CSSMERR_DL_INCOMPATIBLE_FIELD_FORMAT);
216
217 // If this attribute was specified before, throw.
218 if (aNumValues[anAttributeIndex] != ~0UL)
219 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE);
220
221 aNumValues[anAttributeIndex] = anAttribute.NumberOfValues;
222 aValues[anAttributeIndex] = anAttribute.Value;
223 }
224 }
225
226 for (anIndex = 0; anIndex < mAttributeVector.size(); ++anIndex)
227 {
228 const MetaAttribute &aMetaAttribute = *mAttributeVector[anIndex];
229 uint32 aNumberOfValues = aNumValues[anIndex];
230 // Now call the parsingmodule for each attribute that
231 // wasn't explicitly specified and that has a parsingmodule.
232 if (aNumberOfValues == ~0UL)
233 aNumberOfValues = aDataSize == 0 ? 0 : aMetaAttribute.parse(*inData, aValues[anIndex]);
234
235 // XXX When do we throw CSSMERR_DL_MISSING_VALUE? Maybe if an
236 // attribute is part of a unique index.
237
238 // Now we have a valuelist for this attribute. Let's encode it.
239 aMetaAttribute.packAttribute(inWriteSection, anOffset, aNumberOfValues, aValues[anIndex]);
240 }
241
242 inWriteSection.put(OffsetRecordSize, anOffset);
243 inWriteSection.size(anOffset);
244 }
245
246 inline void
247 MetaRecord::unpackAttribute(const ReadSection &inReadSection,
248 CssmAllocator &inAllocator,
249 CSSM_DB_ATTRIBUTE_DATA &inoutAttribute) const
250 {
251 const MetaAttribute &aMetaAttribute = metaAttribute(inoutAttribute.Info);
252 // XXX: See ISSUES on whether AttributeFormat should be an outputvalue or not.
253 inoutAttribute.Info.AttributeFormat = aMetaAttribute.attributeFormat();
254 aMetaAttribute.unpackAttribute(inReadSection, inAllocator,
255 inoutAttribute.NumberOfValues,
256 inoutAttribute.Value);
257 }
258
259 void
260 MetaRecord::unpackRecord(const ReadSection &inReadSection,
261 CssmAllocator &inAllocator,
262 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes,
263 CssmData *inoutData,
264 CSSM_QUERY_FLAGS inQueryFlags) const
265 {
266 // XXX Use POD wrapper for inoutAttributes here.
267 TrackingAllocator anAllocator(inAllocator);
268 if (inoutData)
269 {
270 // XXX Treat KEY records specially.
271
272 // If inQueryFlags & CSSM_QUERY_RETURN_DATA is true return the raw
273 // key bits in the CSSM_KEY structure
274 Range aDataRange = dataRange(inReadSection);
275 inoutData->Length = aDataRange.mSize;
276 inoutData->Data = inReadSection.allocCopyRange(aDataRange, anAllocator);
277 }
278
279 if (inoutAttributes)
280 {
281 inoutAttributes->DataRecordType = dataRecordType();
282 inoutAttributes->SemanticInformation = semanticInformation(inReadSection);
283 uint32 anIndex = inoutAttributes->NumberOfAttributes;
284
285 // Make sure that AttributeData is a valid array.
286 if (anIndex > 0 && inoutAttributes->AttributeData == NULL)
287 CssmError::throwMe(CSSM_ERRCODE_INVALID_POINTER);
288
289 while (anIndex-- > 0)
290 {
291 unpackAttribute(inReadSection, anAllocator,
292 inoutAttributes->AttributeData[anIndex]);
293 }
294 }
295
296 // Don't free anything the trackingAllocator allocated when it is destructed.
297 anAllocator.commit();
298 }
299
300 // Return the index (0 though NumAttributes - 1) of the attribute
301 // represented by inAttributeInfo
302 uint32
303 MetaRecord::attributeIndex(const CSSM_DB_ATTRIBUTE_INFO &inAttributeInfo) const
304 {
305 uint32 anIndex;
306 switch (inAttributeInfo.AttributeNameFormat)
307 {
308 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING:
309 {
310 string aName(inAttributeInfo.Label.AttributeName);
311 NameStringMap::const_iterator it = mNameStringMap.find(aName);
312 if (it == mNameStringMap.end())
313 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME);
314 anIndex = it->second;
315 break;
316 }
317 case CSSM_DB_ATTRIBUTE_NAME_AS_OID:
318 {
319 const CssmOid &aName = CssmOid::overlay(inAttributeInfo.Label.AttributeOID);
320 NameOIDMap::const_iterator it = mNameOIDMap.find(aName);
321 if (it == mNameOIDMap.end())
322 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME);
323 anIndex = it->second;
324 break;
325 }
326 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER:
327 {
328 uint32 aName = inAttributeInfo.Label.AttributeID;
329 NameIntMap::const_iterator it = mNameIntMap.find(aName);
330 if (it == mNameIntMap.end())
331 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME);
332 anIndex = it->second;
333 break;
334 }
335 default:
336 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME);
337 break;
338 }
339
340 return anIndex;
341 }
342
343 const MetaAttribute &
344 MetaRecord::metaAttribute(const CSSM_DB_ATTRIBUTE_INFO &inAttributeInfo) const
345 {
346 return *mAttributeVector[attributeIndex(inAttributeInfo)];
347 }
348
349 // Create a packed record from the given inputs and the old packed record inReadSection.
350 void
351 MetaRecord::updateRecord(const ReadSection &inReadSection,
352 WriteSection &inWriteSection,
353 const CssmDbRecordAttributeData *inAttributes,
354 const CssmData *inData,
355 CSSM_DB_MODIFY_MODE inModifyMode) const
356 {
357 TrackingAllocator anAllocator(CssmAllocator::standard());
358
359 // modify the opaque data associated with the record
360
361 uint32 aDataSize;
362 const uint8 *aDataData = NULL;
363
364 if (inData)
365 {
366 // prepare to write new data
367 aDataSize = inData->Length;
368 aDataData = inData->Data;
369 }
370 else
371 {
372 // prepare to copy old data
373 Range aDataRange = dataRange(inReadSection);
374 aDataSize = aDataRange.mSize;
375 if (aDataSize)
376 aDataData = inReadSection.range(aDataRange);
377 }
378
379 // compute the data offset; this will keep a running total of the record size
380 uint32 anOffset = OffsetAttributeOffsets + AtomSize * mAttributeVector.size();
381
382 // write the appropriate data to the new record
383 inWriteSection.put(OffsetDataSize, aDataSize);
384 if (aDataSize)
385 anOffset = inWriteSection.put(anOffset, aDataSize, aDataData);
386
387 // unpack the old attributes since some of them may need to be preserved
388
389 auto_array<CssmDbAttributeData> attributeData(mAttributeVector.size());
390
391 for (uint32 anAttributeIndex = mAttributeVector.size(); anAttributeIndex-- > 0; )
392 {
393 // unpack the old attribute data for this attribute index
394 const MetaAttribute &attribute = *mAttributeVector[anAttributeIndex];
395 attribute.unpackAttribute(inReadSection, anAllocator,
396 attributeData[anAttributeIndex].NumberOfValues,
397 attributeData[anAttributeIndex].Value);
398 }
399
400 // retrieve the currrent semantic information
401
402 uint32 oldSemanticInformation = semanticInformation(inReadSection);
403
404 // process each input attribute as necessary, based on the modification mode
405
406 if (inAttributes == NULL)
407 {
408 // make sure the modification mode is NONE, otherwise it's an
409 // error accordining to the spec
410 if (inModifyMode != CSSM_DB_MODIFY_ATTRIBUTE_NONE)
411 CssmError::throwMe(CSSMERR_DL_INVALID_MODIFY_MODE);
412 }
413
414 else {
415
416 // modify the semantic information
417
418 uint32 inSemanticInformation = inAttributes ? inAttributes->SemanticInformation : 0;
419
420 if (inModifyMode == CSSM_DB_MODIFY_ATTRIBUTE_ADD)
421 oldSemanticInformation |= inSemanticInformation;
422
423 else if (inModifyMode == CSSM_DB_MODIFY_ATTRIBUTE_DELETE)
424 oldSemanticInformation &= ~inSemanticInformation;
425
426 else if (inModifyMode == CSSM_DB_MODIFY_ATTRIBUTE_REPLACE)
427 oldSemanticInformation = inSemanticInformation;
428
429 uint32 anIndex = inAttributes->NumberOfAttributes;
430 if (anIndex > 0)
431 Required(inAttributes->AttributeData);
432
433 // modify the attributes
434
435 while (anIndex-- > 0) {
436
437 const CssmDbAttributeData &anAttribute = inAttributes->at(anIndex);
438 uint32 anAttributeIndex = attributeIndex(anAttribute.info());
439 if (anAttribute.format() != mAttributeVector[anAttributeIndex]->attributeFormat())
440 CssmError::throwMe(CSSMERR_DL_INCOMPATIBLE_FIELD_FORMAT);
441
442 CssmDbAttributeData &oldAttribute = attributeData[anAttributeIndex];
443
444 // if the modify mode is ADD, merge new values with pre-existing values
445
446 if (inModifyMode == CSSM_DB_MODIFY_ATTRIBUTE_ADD)
447 oldAttribute.add(anAttribute, anAllocator);
448
449 // if the modify mode is DELETE, remove the indicated values, or remove
450 // all values if none are specified
451
452 else if (inModifyMode == CSSM_DB_MODIFY_ATTRIBUTE_DELETE)
453 {
454 if (anAttribute.size() == 0)
455 oldAttribute.deleteValues(anAllocator);
456 else
457 oldAttribute.deleteValues(anAttribute, anAllocator);
458 }
459
460 // if the modify mode is REPLACE, then replace the specified values, or
461 // delete all values if no values are specified
462
463 else if (inModifyMode == CSSM_DB_MODIFY_ATTRIBUTE_REPLACE)
464 {
465 oldAttribute.deleteValues(anAllocator);
466 if (anAttribute.size() > 0)
467 oldAttribute.add(anAttribute, anAllocator);
468 else
469 // The spec says "all values are deleted or the the value is replaced
470 // with the default" but doesn't say which. We could call the parsing
471 // module for the attribute here...if they were implemented! But instead
472 // we choose "all values are deleted" and leave it at that.
473 ;
474 }
475 }
476 }
477
478 // write the resulting attributes into the new record
479
480 inWriteSection.put(OffsetSemanticInformation, oldSemanticInformation);
481
482 for (uint32 anIndex = 0; anIndex < mAttributeVector.size(); ++anIndex)
483 {
484 const MetaAttribute &metaAttribute = *mAttributeVector[anIndex];
485 metaAttribute.packAttribute(inWriteSection, anOffset,
486 attributeData[anIndex].NumberOfValues,
487 attributeData[anIndex].Value);
488 }
489
490 inWriteSection.put(OffsetRecordSize, anOffset);
491 inWriteSection.size(anOffset);
492 }
493