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.
23 #include "MetaRecord.h"
24 #include <security_utilities/trackingallocator.h>
25 #include <security_cdsa_utilities/cssmbridge.h>
28 MetaRecord::MetaRecord(CSSM_DB_RECORDTYPE inRecordType
) :
29 mRecordType(inRecordType
)
33 MetaRecord::MetaRecord(const CSSM_DB_RECORD_ATTRIBUTE_INFO
&inInfo
)
34 : mRecordType(inInfo
.DataRecordType
)
38 setRecordAttributeInfo(inInfo
);
42 for_each_delete(mAttributeVector
.begin(), mAttributeVector
.end());
46 MetaRecord::MetaRecord(CSSM_DB_RECORDTYPE inRelationID
,
47 uint32 inNumberOfAttributes
,
48 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*inAttributeInfo
) :
49 mRecordType(inRelationID
)
52 for (uint32 anIndex
= 0; anIndex
< inNumberOfAttributes
; anIndex
++)
55 if (inAttributeInfo
[anIndex
].AttributeName
)
56 aName
= string(inAttributeInfo
[anIndex
].AttributeName
);
58 const CssmData
*aNameID
= NULL
;
59 if (inAttributeInfo
[anIndex
].AttributeNameID
.Length
> 0)
60 aNameID
= &CssmData::overlay(inAttributeInfo
[anIndex
].AttributeNameID
);
62 uint32 aNumber
= inAttributeInfo
[anIndex
].AttributeId
;
64 inAttributeInfo
[anIndex
].AttributeName
? &aName
: NULL
,
66 inAttributeInfo
[anIndex
].DataType
);
71 for_each_delete(mAttributeVector
.begin(), mAttributeVector
.end());
75 MetaRecord::~MetaRecord()
77 // for_each_delete(mAttributeVector.begin(), mAttributeVector.end());
78 AttributeVector::iterator it
= mAttributeVector
.begin();
79 while (it
!= mAttributeVector
.end())
81 MetaAttribute
* mat
= *it
++;
90 MetaRecord::setRecordAttributeInfo(const CSSM_DB_RECORD_ATTRIBUTE_INFO
&inInfo
)
92 for (uint32 anIndex
= 0; anIndex
< inInfo
.NumberOfAttributes
; anIndex
++)
94 switch (inInfo
.AttributeInfo
[anIndex
].AttributeNameFormat
)
96 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING
:
98 string
aName(inInfo
.AttributeInfo
[anIndex
].Label
.AttributeName
);
99 createAttribute(&aName
, nil
, anIndex
,
100 inInfo
.AttributeInfo
[anIndex
].AttributeFormat
);
103 case CSSM_DB_ATTRIBUTE_NAME_AS_OID
:
105 const CssmData
&aNameID
= CssmOid::overlay(inInfo
.AttributeInfo
[anIndex
].Label
.AttributeOID
);
106 createAttribute(nil
, &aNameID
, anIndex
,
107 inInfo
.AttributeInfo
[anIndex
].AttributeFormat
);
110 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
:
112 uint32 aNumber
= inInfo
.AttributeInfo
[anIndex
].Label
.AttributeID
;
113 createAttribute(nil
, nil
, aNumber
,
114 inInfo
.AttributeInfo
[anIndex
].AttributeFormat
);
118 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
125 MetaRecord::createAttribute(const string
*inAttributeName
,
126 const CssmOid
*inAttributeOID
,
127 uint32 inAttributeID
,
128 CSSM_DB_ATTRIBUTE_FORMAT inAttributeFormat
)
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;
138 if (!mNameStringMap
.insert(NameStringMap::value_type(*inAttributeName
, anAttributeIndex
)).second
)
139 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
140 aInsertedAttributeName
= true;
146 if (!mNameOIDMap
.insert(NameOIDMap::value_type(*inAttributeOID
, anAttributeIndex
)).second
)
147 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
148 aInsertedAttributeOID
= true;
151 if (!mNameIntMap
.insert(NameIntMap::value_type(inAttributeID
, anAttributeIndex
)).second
)
152 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
153 aInsertedAttributeID
= true;
155 // Note: this no longer throws INVALID_FIELD_NAME since the attribute will always have
156 // an attribute ID by which it is known
158 mAttributeVector
.push_back(MetaAttribute::create(inAttributeFormat
,
159 anAttributeIndex
, inAttributeID
));
163 if (aInsertedAttributeName
)
164 mNameStringMap
.erase(*inAttributeName
);
165 if (aInsertedAttributeOID
)
166 mNameOIDMap
.erase(*inAttributeOID
);
168 mNameIntMap
.erase(inAttributeID
);
175 // Create a packed record from the given inputs.
177 MetaRecord::packRecord(WriteSection
&inWriteSection
,
178 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributes
,
179 const CssmData
*inData
) const
183 aDataSize
= inData
->Length
;
187 inWriteSection
.put(OffsetDataSize
, aDataSize
);
188 uint32 anOffset
= OffsetAttributeOffsets
+ AtomSize
* mAttributeVector
.size();
190 anOffset
= inWriteSection
.put(anOffset
, aDataSize
, inData
->Data
);
192 vector
<uint32
> aNumValues(mAttributeVector
.size(), ~(uint32
)0);
193 vector
<CSSM_DATA_PTR
> aValues(mAttributeVector
.size());
196 if (inAttributes
== NULL
)
197 inWriteSection
.put(OffsetSemanticInformation
, 0);
200 inWriteSection
.put(OffsetSemanticInformation
, inAttributes
->SemanticInformation
);
202 // Put the supplied attribute values into the list of attributes
204 anIndex
= inAttributes
->NumberOfAttributes
;
205 // Make sure that AttributeData is a valid array.
207 Required(inAttributes
->AttributeData
);
209 while (anIndex
-- > 0)
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
);
217 // If this attribute was specified before, throw.
218 if (aNumValues
[anAttributeIndex
] != ~(uint32
)0)
219 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
221 aNumValues
[anAttributeIndex
] = anAttribute
.NumberOfValues
;
222 aValues
[anAttributeIndex
] = anAttribute
.Value
;
226 for (anIndex
= 0; anIndex
< mAttributeVector
.size(); ++anIndex
)
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
== ~(uint32
)0)
233 aNumberOfValues
= aDataSize
== 0 ? 0 : aMetaAttribute
.parse(*inData
, aValues
[anIndex
]);
235 // XXX When do we throw CSSMERR_DL_MISSING_VALUE? Maybe if an
236 // attribute is part of a unique index.
238 // Now we have a valuelist for this attribute. Let's encode it.
239 aMetaAttribute
.packAttribute(inWriteSection
, anOffset
, aNumberOfValues
, aValues
[anIndex
]);
242 inWriteSection
.put(OffsetRecordSize
, anOffset
);
243 inWriteSection
.size(anOffset
);
247 MetaRecord::unpackAttribute(const ReadSection
&inReadSection
,
248 Allocator
&inAllocator
,
249 CSSM_DB_ATTRIBUTE_DATA
&inoutAttribute
) const
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
);
260 MetaRecord::unpackRecord(const ReadSection
&inReadSection
,
261 Allocator
&inAllocator
,
262 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
264 CSSM_QUERY_FLAGS inQueryFlags
) const
266 // XXX Use POD wrapper for inoutAttributes here.
267 TrackingAllocator
anAllocator(inAllocator
);
273 // XXX Treat KEY records specially.
275 // If inQueryFlags & CSSM_QUERY_RETURN_DATA is true return the raw
276 // key bits in the CSSM_KEY structure
277 Range aDataRange
= dataRange(inReadSection
);
278 inoutData
->Length
= aDataRange
.mSize
;
279 inoutData
->Data
= inReadSection
.allocCopyRange(aDataRange
, anAllocator
);
284 inoutAttributes
->DataRecordType
= dataRecordType();
285 inoutAttributes
->SemanticInformation
= semanticInformation(inReadSection
);
286 uint32 anIndex
= inoutAttributes
->NumberOfAttributes
;
288 // Make sure that AttributeData is a valid array.
289 if (anIndex
> 0 && inoutAttributes
->AttributeData
== NULL
)
290 CssmError::throwMe(CSSM_ERRCODE_INVALID_POINTER
);
292 while (anIndex
-- > 0)
294 unpackAttribute(inReadSection
, anAllocator
,
295 inoutAttributes
->AttributeData
[anIndex
]);
301 if (e
.osStatus() != CSSMERR_DL_DATABASE_CORRUPT
)
303 // clear all pointers so that nothing dangles back to the user
306 inoutData
->Data
= NULL
;
312 for (i
= 0; i
< inoutAttributes
->NumberOfAttributes
; ++i
)
314 CSSM_DB_ATTRIBUTE_DATA
& data
= inoutAttributes
->AttributeData
[i
];
317 for (j
= 0; j
< data
.NumberOfValues
; ++j
)
319 data
.Value
[j
].Data
= NULL
;
324 if (data
.Info
.AttributeNameFormat
== CSSM_DB_ATTRIBUTE_NAME_AS_STRING
)
326 data
.Info
.Label
.AttributeName
= NULL
;
336 // clear all pointers so that nothing dangles back to the user
339 inoutData
->Data
= NULL
;
345 for (i
= 0; i
< inoutAttributes
->NumberOfAttributes
; ++i
)
347 CSSM_DB_ATTRIBUTE_DATA
& data
= inoutAttributes
->AttributeData
[i
];
350 for (j
= 0; j
< data
.NumberOfValues
; ++j
)
352 data
.Value
[j
].Data
= NULL
;
357 if (data
.Info
.AttributeNameFormat
== CSSM_DB_ATTRIBUTE_NAME_AS_STRING
)
359 data
.Info
.Label
.AttributeName
= NULL
;
368 // Don't free anything the trackingAllocator allocated when it is destructed.
369 anAllocator
.commit();
372 // Return the index (0 though NumAttributes - 1) of the attribute
373 // represented by inAttributeInfo
376 #define LOG_NAME_AS_STRING_FAIL
379 MetaRecord::attributeIndex(const CSSM_DB_ATTRIBUTE_INFO
&inAttributeInfo
) const
382 switch (inAttributeInfo
.AttributeNameFormat
)
384 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING
:
386 string
aName(inAttributeInfo
.Label
.AttributeName
);
387 assert(aName
.size() < 500); // MDS leak debug
388 NameStringMap::const_iterator it
= mNameStringMap
.find(aName
);
389 if (it
== mNameStringMap
.end()) {
390 #ifdef LOG_NAME_AS_STRING_FAIL
391 printf("NAME_AS_STRING failure; attrName %s\n",
392 inAttributeInfo
.Label
.AttributeName
);
393 for(it
= mNameStringMap
.begin();
394 it
!= mNameStringMap
.end();
396 printf("name %s val %d\n", it
->first
.c_str(), it
->second
);
399 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
401 anIndex
= it
->second
;
404 case CSSM_DB_ATTRIBUTE_NAME_AS_OID
:
406 const CssmOid
&aName
= CssmOid::overlay(inAttributeInfo
.Label
.AttributeOID
);
407 NameOIDMap::const_iterator it
= mNameOIDMap
.find(aName
);
408 if (it
== mNameOIDMap
.end())
409 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
410 anIndex
= it
->second
;
413 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
:
415 uint32 aName
= inAttributeInfo
.Label
.AttributeID
;
416 NameIntMap::const_iterator it
= mNameIntMap
.find(aName
);
417 if (it
== mNameIntMap
.end())
418 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
419 anIndex
= it
->second
;
423 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
430 const MetaAttribute
&
431 MetaRecord::metaAttribute(const CSSM_DB_ATTRIBUTE_INFO
&inAttributeInfo
) const
433 return *mAttributeVector
[attributeIndex(inAttributeInfo
)];
436 // Create a packed record from the given inputs and the old packed record inReadSection.
438 MetaRecord::updateRecord(const ReadSection
&inReadSection
,
439 WriteSection
&inWriteSection
,
440 const CssmDbRecordAttributeData
*inAttributes
,
441 const CssmData
*inData
,
442 CSSM_DB_MODIFY_MODE inModifyMode
) const
444 TrackingAllocator
anAllocator(Allocator::standard());
446 // modify the opaque data associated with the record
449 const uint8
*aDataData
= NULL
;
453 // prepare to write new data
454 aDataSize
= inData
->Length
;
455 aDataData
= inData
->Data
;
459 // prepare to copy old data
460 Range aDataRange
= dataRange(inReadSection
);
461 aDataSize
= aDataRange
.mSize
;
463 aDataData
= inReadSection
.range(aDataRange
);
466 // compute the data offset; this will keep a running total of the record size
467 uint32 anOffset
= OffsetAttributeOffsets
+ AtomSize
* mAttributeVector
.size();
469 // write the appropriate data to the new record
470 inWriteSection
.put(OffsetDataSize
, aDataSize
);
472 anOffset
= inWriteSection
.put(anOffset
, aDataSize
, aDataData
);
474 // unpack the old attributes since some of them may need to be preserved
476 auto_array
<CssmDbAttributeData
> attributeData(mAttributeVector
.size());
478 for (uint32 anAttributeIndex
= mAttributeVector
.size(); anAttributeIndex
-- > 0; )
480 // unpack the old attribute data for this attribute index
481 const MetaAttribute
&attribute
= *mAttributeVector
[anAttributeIndex
];
482 attribute
.unpackAttribute(inReadSection
, anAllocator
,
483 attributeData
[anAttributeIndex
].NumberOfValues
,
484 attributeData
[anAttributeIndex
].Value
);
487 // retrieve the currrent semantic information
489 uint32 oldSemanticInformation
= semanticInformation(inReadSection
);
491 // process each input attribute as necessary, based on the modification mode
493 if (inAttributes
== NULL
)
495 // make sure the modification mode is NONE, otherwise it's an
496 // error accordining to the spec
497 if (inModifyMode
!= CSSM_DB_MODIFY_ATTRIBUTE_NONE
)
498 CssmError::throwMe(CSSMERR_DL_INVALID_MODIFY_MODE
);
503 // modify the semantic information
505 uint32 inSemanticInformation
= inAttributes
? inAttributes
->SemanticInformation
: 0;
507 if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_ADD
)
508 oldSemanticInformation
|= inSemanticInformation
;
510 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_DELETE
)
511 oldSemanticInformation
&= ~inSemanticInformation
;
513 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_REPLACE
)
514 oldSemanticInformation
= inSemanticInformation
;
516 uint32 anIndex
= inAttributes
->NumberOfAttributes
;
518 Required(inAttributes
->AttributeData
);
520 // modify the attributes
522 while (anIndex
-- > 0) {
524 const CssmDbAttributeData
&anAttribute
= inAttributes
->at(anIndex
);
525 uint32 anAttributeIndex
= attributeIndex(anAttribute
.info());
526 if (anAttribute
.format() != mAttributeVector
[anAttributeIndex
]->attributeFormat())
527 CssmError::throwMe(CSSMERR_DL_INCOMPATIBLE_FIELD_FORMAT
);
529 CssmDbAttributeData
&oldAttribute
= attributeData
[anAttributeIndex
];
531 // if the modify mode is ADD, merge new values with pre-existing values
533 if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_ADD
)
534 oldAttribute
.add(anAttribute
, anAllocator
);
536 // if the modify mode is DELETE, remove the indicated values, or remove
537 // all values if none are specified
539 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_DELETE
)
541 if (anAttribute
.size() == 0)
542 oldAttribute
.deleteValues(anAllocator
);
544 oldAttribute
.deleteValues(anAttribute
, anAllocator
);
547 // if the modify mode is REPLACE, then replace the specified values, or
548 // delete all values if no values are specified
550 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_REPLACE
)
552 oldAttribute
.deleteValues(anAllocator
);
553 if (anAttribute
.size() > 0)
554 oldAttribute
.add(anAttribute
, anAllocator
);
556 // The spec says "all values are deleted or the the value is replaced
557 // with the default" but doesn't say which. We could call the parsing
558 // module for the attribute here...if they were implemented! But instead
559 // we choose "all values are deleted" and leave it at that.
565 // write the resulting attributes into the new record
567 inWriteSection
.put(OffsetSemanticInformation
, oldSemanticInformation
);
569 for (uint32 anIndex
= 0; anIndex
< mAttributeVector
.size(); ++anIndex
)
571 const MetaAttribute
&metaAttribute
= *mAttributeVector
[anIndex
];
572 metaAttribute
.packAttribute(inWriteSection
, anOffset
,
573 attributeData
[anIndex
].NumberOfValues
,
574 attributeData
[anIndex
].Value
);
577 inWriteSection
.put(OffsetRecordSize
, anOffset
);
578 inWriteSection
.size(anOffset
);