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.
23 Contains: Module Directory Services Data Types and API.
25 Copyright (c) 1999,2011,2014 Apple Inc. All Rights Reserved.
27 This is the C API wrapper for the C++ MDS implementation. Most of this file
28 could also be generated by the same perl script that generates the plugin
33 #include "MDSSession.h"
35 #include <security_cdsa_utilities/cssmbridge.h>
37 #include <security_utilities/globalizer.h>
38 #include <security_utilities/threading.h>
39 #include "LegacyAPICounts.h"
41 #define MSApiDebug(args...) secinfo("MDS_API", ## args)
43 /* Protects access to AppleDataBase */
44 ModuleNexus
<Mutex
> adbMutex
;
48 static CSSM_RETURN CSSMAPI
mds_DataGetFirst(CSSM_DL_DB_HANDLE DLDBHandle
,
49 const CSSM_QUERY
*Query
,
50 CSSM_HANDLE_PTR ResultsHandle
,
51 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR Attributes
,
53 CSSM_DB_UNIQUE_RECORD_PTR
*UniqueId
)
56 MSApiDebug("mds_DataGetFirst");
57 StLock
<Mutex
> _(adbMutex());
58 if (!(Required(ResultsHandle
) = HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DataGetFirst(DLDBHandle
.DBHandle
,
59 CssmQuery::optional(Query
),
61 CssmData::optional(Data
),
63 return CSSMERR_DL_ENDOFDATA
;
67 static CSSM_RETURN CSSMAPI
mds_DataModify(CSSM_DL_DB_HANDLE DLDBHandle
,
68 CSSM_DB_RECORDTYPE RecordType
,
69 CSSM_DB_UNIQUE_RECORD_PTR UniqueRecordIdentifier
,
70 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*AttributesToBeModified
,
71 const CSSM_DATA
*DataToBeModified
,
72 CSSM_DB_MODIFY_MODE ModifyMode
)
75 StLock
<Mutex
> _(adbMutex());
76 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DataModify(DLDBHandle
.DBHandle
,
78 Required(UniqueRecordIdentifier
),
79 AttributesToBeModified
,
80 CssmData::optional(DataToBeModified
),
85 static CSSM_RETURN CSSMAPI
mds_GetDbNameFromHandle(CSSM_DL_DB_HANDLE DLDBHandle
,
89 StLock
<Mutex
> _(adbMutex());
90 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).GetDbNameFromHandle(DLDBHandle
.DBHandle
,
95 static CSSM_RETURN CSSMAPI
mds_DataAbortQuery(CSSM_DL_DB_HANDLE DLDBHandle
,
96 CSSM_HANDLE ResultsHandle
)
99 MSApiDebug("mds_DataAbortQuery");
100 StLock
<Mutex
> _(adbMutex());
101 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DataAbortQuery(DLDBHandle
.DBHandle
,
106 static CSSM_RETURN CSSMAPI
mds_DestroyRelation(CSSM_DL_DB_HANDLE DLDBHandle
,
107 CSSM_DB_RECORDTYPE RelationID
)
110 StLock
<Mutex
> _(adbMutex());
111 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DestroyRelation(DLDBHandle
.DBHandle
,
116 static CSSM_RETURN CSSMAPI
mds_DataDelete(CSSM_DL_DB_HANDLE DLDBHandle
,
117 const CSSM_DB_UNIQUE_RECORD
*UniqueRecordIdentifier
)
120 StLock
<Mutex
> _(adbMutex());
121 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DataDelete(DLDBHandle
.DBHandle
,
122 Required(UniqueRecordIdentifier
));
126 static CSSM_RETURN CSSMAPI
mds_DataInsert(CSSM_DL_DB_HANDLE DLDBHandle
,
127 CSSM_DB_RECORDTYPE RecordType
,
128 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*Attributes
,
129 const CSSM_DATA
*Data
,
130 CSSM_DB_UNIQUE_RECORD_PTR
*UniqueId
)
133 StLock
<Mutex
> _(adbMutex());
134 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DataInsert(DLDBHandle
.DBHandle
,
137 CssmData::optional(Data
),
142 static CSSM_RETURN CSSMAPI
mds_DataGetFromUniqueRecordId(CSSM_DL_DB_HANDLE DLDBHandle
,
143 const CSSM_DB_UNIQUE_RECORD
*UniqueRecord
,
144 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR Attributes
,
148 StLock
<Mutex
> _(adbMutex());
149 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DataGetFromUniqueRecordId(DLDBHandle
.DBHandle
,
150 Required(UniqueRecord
),
152 CssmData::optional(Data
));
156 static CSSM_RETURN CSSMAPI
mds_CreateRelation(CSSM_DL_DB_HANDLE DLDBHandle
,
157 CSSM_DB_RECORDTYPE RelationID
,
158 const char *RelationName
,
159 uint32 NumberOfAttributes
,
160 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*pAttributeInfo
,
161 uint32 NumberOfIndexes
,
162 const CSSM_DB_SCHEMA_INDEX_INFO
*pIndexInfo
)
165 StLock
<Mutex
> _(adbMutex());
166 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).CreateRelation(DLDBHandle
.DBHandle
,
172 Required(pIndexInfo
));
176 static CSSM_RETURN CSSMAPI
mds_FreeUniqueRecord(CSSM_DL_DB_HANDLE DLDBHandle
,
177 CSSM_DB_UNIQUE_RECORD_PTR UniqueRecord
)
180 StLock
<Mutex
> _(adbMutex());
181 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).FreeUniqueRecord(DLDBHandle
.DBHandle
,
182 Required(UniqueRecord
));
186 static CSSM_RETURN CSSMAPI
mds_DbOpen(CSSM_DL_HANDLE DLHandle
,
188 const CSSM_NET_ADDRESS
*DbLocation
,
189 CSSM_DB_ACCESS_TYPE AccessRequest
,
190 const CSSM_ACCESS_CREDENTIALS
*AccessCred
,
191 const void *OpenParameters
,
192 CSSM_DB_HANDLE
*DbHandle
)
195 MSApiDebug("mds_DbOpen %s", DbName
);
196 StLock
<Mutex
> _(adbMutex());
197 HandleObject::find
<MDSSession
>(DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DbOpen(DbName
,
200 AccessCredentials::optional(AccessCred
),
206 static CSSM_RETURN CSSMAPI
mds_DataGetNext(CSSM_DL_DB_HANDLE DLDBHandle
,
207 CSSM_HANDLE ResultsHandle
,
208 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR Attributes
,
210 CSSM_DB_UNIQUE_RECORD_PTR
*UniqueId
)
213 MSApiDebug("mds_DataGetNext");
214 StLock
<Mutex
> _(adbMutex());
215 if (!HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DataGetNext(DLDBHandle
.DBHandle
,
218 CssmData::optional(Data
),
220 return CSSMERR_DL_ENDOFDATA
;
224 static CSSM_RETURN CSSMAPI
mds_GetDbNames(CSSM_DL_HANDLE DLHandle
,
225 CSSM_NAME_LIST_PTR
*NameList
)
228 HandleObject::find
<MDSSession
>(DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).GetDbNames(Required(NameList
));
232 static CSSM_RETURN CSSMAPI
mds_DbClose(CSSM_DL_DB_HANDLE DLDBHandle
)
235 MSApiDebug("mds_DbClose");
236 StLock
<Mutex
> _(adbMutex());
237 HandleObject::find
<MDSSession
>(DLDBHandle
.DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).DbClose(DLDBHandle
.DBHandle
);
241 static CSSM_RETURN CSSMAPI
mds_FreeNameList(CSSM_DL_HANDLE DLHandle
,
242 CSSM_NAME_LIST_PTR NameList
)
245 HandleObject::find
<MDSSession
>(DLHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).FreeNameList(Required(NameList
));
249 static MDS_FUNCS gMDSFunctionTable
=
254 mds_GetDbNameFromHandle
,
262 mds_DataGetFromUniqueRecordId
,
263 mds_FreeUniqueRecord
,
270 MDS_Initialize (const CSSM_GUID
*inCallerGuid
,
271 const CSSM_MEMORY_FUNCS
*inMemoryFunctions
,
272 MDS_FUNCS_PTR outDlFunctions
,
273 MDS_HANDLE
*outMDSHandle
)
275 // The clang analyzer is not a fan of handing handles to your caller and trusting them to release later.
276 #ifndef __clang_analyzer__
278 Required (outDlFunctions
);
279 Required (outMDSHandle
) = (new MDSSession (Guid::optional(inCallerGuid
),
280 Required(inMemoryFunctions
)))->handle ();
281 *outDlFunctions
= gMDSFunctionTable
;
287 MDS_Terminate (MDS_HANDLE inMDSHandle
)
290 unique_ptr
<MDSSession
> aMDSSession (&HandleObject::findAndKill
<MDSSession
> (inMDSHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
));
291 aMDSSession
->terminate (); // Even if terminate throws the MDSSession object will be deleted.
296 MDS_Install (MDS_HANDLE inMDSHandle
)
299 HandleObject::find
<MDSSession
> (inMDSHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).install ();
304 MDS_Uninstall (MDS_HANDLE inMDSHandle
)
307 HandleObject::find
<MDSSession
> (inMDSHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).uninstall ();
313 // Private APIs for subsystem registration (called from securityd as root ONLY)
316 MDS_InstallFile(MDS_HANDLE inMDSHandle
, const MDS_InstallDefaults
*defaults
,
317 const char *bundlePath
, const char *subdir
, const char *file
) // file(s)
320 HandleObject::find
<MDSSession
>(inMDSHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).installFile(defaults
, bundlePath
, subdir
, file
);
328 MDS_RemoveSubservice(MDS_HANDLE inMDSHandle
, const char *guid
, uint32 ssid
)
331 HandleObject::find
<MDSSession
>(inMDSHandle
, CSSMERR_CSSM_INVALID_ADDIN_HANDLE
).removeSubservice(guid
, ssid
);