MDSSession.h
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 #ifndef _MDSSESSION_H_ 20 #define _MDSSESSION_H_ 1 21 22 #include <security_cdsa_plugin/DatabaseSession.h> 23 #include <security_cdsa_utilities/handleobject.h> 24 #include <security_cdsa_utilities/cssmdb.h> 25 #include <Security/mdspriv.h> 26 #include "MDSModule.h" 27 #include "MDSSchema.h" 28 #include <map> 29 #include <sys/stat.h> 30 #include <sys/param.h> 31 #include <sys/types.h> 32 #include <list> 33 34 namespace Security 35 { 36 37 class MDSSession: public DatabaseSession, public HandleObject 38 { 39 NOCOPY(MDSSession) 40 public: 41 MDSSession (const Guid *inCallerGuid, 42 const CSSM_MEMORY_FUNCS &inMemoryFunctions); 43 virtual ~MDSSession (); 44 45 void terminate (); 46 void install (); 47 void uninstall (); 48 49 CSSM_DB_HANDLE dbOpen(const char *dbName, bool batched = false); 50 51 // some DatabaseSession routines we need to override 52 void DbOpen(const char *DbName, 53 const CSSM_NET_ADDRESS *DbLocation, 54 CSSM_DB_ACCESS_TYPE AccessRequest, 55 const AccessCredentials *AccessCred, 56 const void *OpenParameters, 57 CSSM_DB_HANDLE &DbHandle); 58 CSSM_HANDLE DataGetFirst(CSSM_DB_HANDLE DBHandle, 59 const CssmQuery *Query, 60 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR Attributes, 61 CssmData *Data, 62 CSSM_DB_UNIQUE_RECORD_PTR &UniqueId); 63 void GetDbNames(CSSM_NAME_LIST_PTR &NameList); 64 void FreeNameList(CSSM_NAME_LIST &NameList); 65 void GetDbNameFromHandle(CSSM_DB_HANDLE DBHandle, 66 char **DbName); 67 68 // additional public (or private API) methods 69 void installFile(const MDS_InstallDefaults *defaults, 70 const char *inBundlePath, const char *subdir, const char *file); 71 void removeSubservice(const char *guid, uint32 ssid); 72 73 // implement CssmHeap::Allocator 74 void *malloc(size_t size) 75 { return mCssmMemoryFunctions.malloc(size); } 76 void free(void *addr) _NOEXCEPT 77 { mCssmMemoryFunctions.free(addr); } 78 void *realloc(void *addr, size_t size) 79 { return mCssmMemoryFunctions.realloc(addr, size); } 80 81 MDSModule &module() { return mModule; } 82 void removeRecordsForGuid( 83 const char *guid, 84 CSSM_DB_HANDLE dbHand); 85 86 87 /* 88 * represents two DB files in any location and state 89 */ 90 class DbFilesInfo 91 { 92 public: 93 DbFilesInfo(MDSSession &session, const char *dbPath); 94 ~DbFilesInfo(); 95 /* these three may not be needed */ 96 CSSM_DB_HANDLE objDbHand(); 97 CSSM_DB_HANDLE directDbHand(); 98 time_t laterTimestamp() { return mLaterTimestamp; } 99 100 /* public functions used by MDSSession */ 101 void updateSystemDbInfo( 102 const char *systemPath, // e.g., /System/Library/Frameworks 103 const char *bundlePath); // e.g., /System/Library/Security 104 void removeOutdatedPlugins(); 105 void updateForBundleDir( 106 const char *bundleDirPath); 107 void updateForBundle( 108 const char *bundlePath); 109 private: 110 bool lookupForPath( 111 const char *path); 112 113 /* object and list to keep track of "to be deleted" records */ 114 #define MAX_GUID_LEN 64 /* normally 37 */ 115 class TbdRecord 116 { 117 public: 118 TbdRecord(const CSSM_DATA &guid); 119 ~TbdRecord() { } 120 const char *guid() { return mGuid; } 121 private: 122 char mGuid[MAX_GUID_LEN]; 123 }; 124 typedef vector<TbdRecord *> TbdVector; 125 126 void checkOutdatedPlugin( 127 const CSSM_DATA &pathValue, 128 const CSSM_DATA &guidValue, 129 TbdVector &tbdVector); 130 131 MDSSession &mSession; 132 char mDbPath[MAXPATHLEN]; 133 CSSM_DB_HANDLE mObjDbHand; 134 CSSM_DB_HANDLE mDirectDbHand; 135 time_t mLaterTimestamp; 136 }; /* DbFilesInfo */ 137 private: 138 class LockHelper 139 { 140 private: 141 int mFD; 142 143 public: 144 LockHelper() : mFD(-1) {} 145 ~LockHelper(); 146 147 bool obtainLock( 148 const char *lockFile, 149 int timeout = 0); 150 }; 151 152 /* given DB file name, fill in fully specified path */ 153 void dbFullPath( 154 const char *dbName, 155 char fullPath[MAXPATHLEN+1]); 156 157 void updateDataBases(); 158 159 void clearRecords(CSSM_DB_HANDLE dbHand, const CssmQuery &query); 160 161 bool systemDatabasesPresent(bool purge); 162 void createSystemDatabase( 163 const char *dbName, 164 const RelationInfo *relationInfo, 165 unsigned numRelations, 166 CSSM_BOOL autoCommit, 167 mode_t mode, 168 CSSM_DB_HANDLE &dbHand); // RETURNED 169 bool createSystemDatabases( 170 CSSM_BOOL autoCommit, 171 mode_t mode); 172 173 RecursionBlock mUpdating; // updateDatabases() in progress 174 175 const CssmMemoryFunctions mCssmMemoryFunctions; 176 Guid mCallerGuid; 177 bool mCallerGuidPresent; 178 179 MDSModule &mModule; 180 }; 181 182 } // end namespace Security 183 184 #endif //_MDSSESSION_H_