1/*
2 * Copyright (c) 2000-2001,2011,2013-2014 Apple 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#include "MDSModule.h"
20#include "MDSSession.h"
21#include <Security/mds_schema.h>
22#include <memory>
23
24namespace Security
25{
26
27ModuleNexus<MDSModule> MDSModule::mModuleNexus;
28
29// Names and IDs of tables used in the MDS databases
30
31#define TABLE(t) { t, #t }
32
33/*
34 * For now, to allow compatibility with AppleFileDL, we use the same record IDs
35 * it uses when constructing an AppleDatabaseManager. See Radar 2817921 for details.
36 * The fix requires that AppleDatabase be able to fetch its meta-table relationIDs
37 * from an existing DB at DbOpen time; I'm not sure that's possible.
38 */
39#define USE_FILE_DL_TABLES		1
40
41static const AppleDatabaseTableName kTableNames[] = {
42    // the meta-tables. the parsing module is not used by MDS, but is required
43    // by the implementation of the database
44	#if USE_FILE_DL_TABLES
45    TABLE(CSSM_DL_DB_SCHEMA_INFO),
46    TABLE(CSSM_DL_DB_SCHEMA_ATTRIBUTES),
47    TABLE(CSSM_DL_DB_SCHEMA_INDEXES),
48	#else
49    TABLE(MDS_CDSADIR_MDS_SCHEMA_RELATIONS),
50    TABLE(MDS_CDSADIR_MDS_SCHEMA_ATTRIBUTES),
51    TABLE(MDS_CDSADIR_MDS_SCHEMA_INDEXES),
52	#endif
53    TABLE(CSSM_DL_DB_SCHEMA_PARSING_MODULE),
54
55    // the MDS-specific tables
56    TABLE(MDS_OBJECT_RECORDTYPE),
57    TABLE(MDS_CDSADIR_CSSM_RECORDTYPE),
58    TABLE(MDS_CDSADIR_KRMM_RECORDTYPE),
59    TABLE(MDS_CDSADIR_EMM_RECORDTYPE),
60    TABLE(MDS_CDSADIR_COMMON_RECORDTYPE),
61    TABLE(MDS_CDSADIR_CSP_PRIMARY_RECORDTYPE),
62    TABLE(MDS_CDSADIR_CSP_CAPABILITY_RECORDTYPE),
63    TABLE(MDS_CDSADIR_CSP_ENCAPSULATED_PRODUCT_RECORDTYPE),
64    TABLE(MDS_CDSADIR_CSP_SC_INFO_RECORDTYPE),
65    TABLE(MDS_CDSADIR_DL_PRIMARY_RECORDTYPE),
66    TABLE(MDS_CDSADIR_DL_ENCAPSULATED_PRODUCT_RECORDTYPE),
67    TABLE(MDS_CDSADIR_CL_PRIMARY_RECORDTYPE),
68    TABLE(MDS_CDSADIR_CL_ENCAPSULATED_PRODUCT_RECORDTYPE),
69    TABLE(MDS_CDSADIR_TP_PRIMARY_RECORDTYPE),
70    TABLE(MDS_CDSADIR_TP_OIDS_RECORDTYPE),
71    TABLE(MDS_CDSADIR_TP_ENCAPSULATED_PRODUCT_RECORDTYPE),
72    TABLE(MDS_CDSADIR_EMM_PRIMARY_RECORDTYPE),
73    TABLE(MDS_CDSADIR_AC_PRIMARY_RECORDTYPE),
74    TABLE(MDS_CDSADIR_KR_PRIMARY_RECORDTYPE),
75
76    // marker for the end of the list
77    { ~0U, NULL }
78};
79
80MDSModule &
81MDSModule::get ()
82{
83    return mModuleNexus ();
84}
85
86MDSModule::MDSModule ()
87    :	mDatabaseManager(kTableNames),
88	    mLastScanTime((time_t)0),
89		mServerMode(false)
90{
91	mDbPath[0] = '\0';
92}
93
94/*
95 * Called upon unload or process death by CleanModuleNexus.
96 */
97MDSModule::~MDSModule ()
98{
99	/* TBD - close all DBs */
100}
101
102void MDSModule::lastScanIsNow()
103{
104	mLastScanTime = Time::now();
105}
106
107double MDSModule::timeSinceLastScan()
108{
109	Time::Interval delta = Time::now() - mLastScanTime;
110	return delta.seconds();
111}
112
113void MDSModule::getDbPath(
114	char *path)
115{
116	StLock<Mutex> _(mDbPathLock);
117	strcpy(path, mDbPath);
118}
119
120void MDSModule::setDbPath(const char *path)
121{
122	StLock<Mutex> _(mDbPathLock);
123	/* caller assures this, and this is private to this module */
124	assert(strlen(path) <= MAXPATHLEN);
125	strcpy(mDbPath, path);
126}
127
128void MDSModule::setServerMode()
129{
130	secdebug("MDSModule", "setting global server mode");
131	mServerMode = true;
132}
133
134} // end namespace Security
135