/ OSX / libsecurity_cssm / lib / modloader.cpp
modloader.cpp
  1  /*
  2   * Copyright (c) 2000-2001,2003-2004,2011,2014 Apple Inc. All Rights Reserved.
  3   * 
  4   * @APPLE_LICENSE_HEADER_START@
  5   * 
  6   * This file contains Original Code and/or Modifications of Original Code
  7   * as defined in and that are subject to the Apple Public Source License
  8   * Version 2.0 (the 'License'). You may not use this file except in
  9   * compliance with the License. Please obtain a copy of the License at
 10   * http://www.opensource.apple.com/apsl/ and read it before using this
 11   * file.
 12   * 
 13   * The Original Code and all software distributed under the License are
 14   * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 15   * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 16   * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 17   * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 18   * Please see the License for the specific language governing rights and
 19   * limitations under the License.
 20   * 
 21   * @APPLE_LICENSE_HEADER_END@
 22   */
 23  
 24  
 25  //
 26  // cssm module loader interface - MACOS X (CFBundle/DYLD) version.
 27  //
 28  // This file provides a C++-style interface to CFBundles as managed by the CF-style
 29  // system interfaces. The implementation looks a bit, well, hybrid - but the visible
 30  // interfaces are pure C++.
 31  //
 32  #include "modloader.h"
 33  #include "modload_plugin.h"
 34  # include "modload_static.h"
 35  
 36  
 37  namespace Security {
 38  
 39  
 40  //
 41  // Pull in functions for built-in plugin modules
 42  //
 43  #define BUILTIN(suffix) \
 44  	extern "C" CSSM_SPI_ModuleLoadFunction CSSM_SPI_ModuleLoad ## suffix; \
 45  	extern "C" CSSM_SPI_ModuleUnloadFunction CSSM_SPI_ModuleUnload ## suffix; \
 46  	extern "C" CSSM_SPI_ModuleAttachFunction CSSM_SPI_ModuleAttach ## suffix; \
 47  	extern "C" CSSM_SPI_ModuleDetachFunction CSSM_SPI_ModuleDetach ## suffix; \
 48  	static const PluginFunctions builtin ## suffix = { \
 49  		CSSM_SPI_ModuleLoad ## suffix, CSSM_SPI_ModuleUnload ## suffix, \
 50  		CSSM_SPI_ModuleAttach ## suffix, CSSM_SPI_ModuleDetach ## suffix \
 51  	};
 52  
 53  BUILTIN(__apple_csp)
 54  BUILTIN(__apple_file_dl)
 55  BUILTIN(__apple_cspdl)
 56  BUILTIN(__apple_x509_cl)
 57  BUILTIN(__apple_x509_tp)
 58  BUILTIN(__sd_cspdl)
 59  
 60  
 61  //
 62  // Construct the canonical ModuleLoader object
 63  //
 64  ModuleLoader::ModuleLoader()
 65  {
 66  #if !defined(NO_BUILTIN_PLUGINS)
 67      mPlugins["*AppleCSP"] = new StaticPlugin(builtin__apple_csp);
 68      mPlugins["*AppleDL"] = new StaticPlugin(builtin__apple_file_dl);
 69      mPlugins["*AppleCSPDL"] = new StaticPlugin(builtin__apple_cspdl);
 70      mPlugins["*AppleX509CL"] = new StaticPlugin(builtin__apple_x509_cl);
 71      mPlugins["*AppleX509TP"] = new StaticPlugin(builtin__apple_x509_tp);
 72      mPlugins["*SDCSPDL"] = new StaticPlugin(builtin__sd_cspdl);
 73  #endif //NO_BUILTIN_PLUGINS
 74  }
 75  
 76  
 77  //
 78  // "Load" a plugin, given its MDS path. At this layer, we are performing
 79  // a purely physical load operation. No code in the plugin is called.
 80  // If "built-in plugins" are enabled, the moduleTable will come pre-initialized
 81  // with certain paths. Since we consult this table before going to disk, this
 82  // means that we'll pick these up first *as long as the paths match exactly*.
 83  // There is nothing magical in the path strings themselves, other than by
 84  // convention. (The convention is "*NAME", which conveniently does not match
 85  // any actual file path.)
 86  //
 87  Plugin *ModuleLoader::operator () (const string &path)
 88  {
 89      Plugin * &plugin = mPlugins[path];
 90      if (!plugin) {
 91  		secinfo("cssm", "ModuleLoader(): creating plugin %s", path.c_str());
 92          plugin = new LoadablePlugin(path.c_str());
 93  	}
 94  	else {
 95  		secinfo("cssm", "ModuleLoader(): FOUND plugin %s, isLoaded %s", 
 96  			path.c_str(), plugin->isLoaded() ? "TRUE" : "FALSE");
 97  		if(!plugin->isLoaded()) {
 98  			plugin->load();
 99  		}
100  	}
101      return plugin;
102  }
103  
104  
105  }	// end namespace Security