secloader.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef _SECLOADER_HPP__
  14. #define _SECLOADER_HPP__
  15. #include "seclib.hpp"
  16. typedef IAuthMap* (*createDefaultAuthMap_t_)(IPropertyTree* config);
  17. typedef ISecManager* (*newSecManager_t_)(const char *serviceName, IPropertyTree &config);
  18. typedef ISecManager* (*newPluggableSecManager_t_)(const char *serviceName, IPropertyTree &secMgrCfg, IPropertyTree &authCfg);
  19. class SecLoader
  20. {
  21. public:
  22. ///
  23. /// Method: loadPluggableSecManager
  24. ///
  25. /// Using the given configuration property trees, this method loads the specified
  26. /// Security Manager DLL/SO implemented in the specified library file and calls
  27. /// its instance factory to create and return an ISecManager security manager instance
  28. /// for the given ESP service
  29. ///
  30. /// @param bindingName Binding name ie 'WsTopology_smc_myesp'
  31. /// @param bindingCfg 'Binding' IPropertyTree associated with ESPService
  32. /// @param secMgrCfg 'SecurityManager' IPropertyTree from component config file
  33. ///
  34. /// @return an ISecManager Security Manager instance
  35. ///
  36. static ISecManager* loadPluggableSecManager(const char * bindingName, IPropertyTree* bindingCfg, IPropertyTree* secMgrCfg)
  37. {
  38. const char * lsm = "Load Security Manager :";
  39. StringBuffer libName, instFactory;
  40. secMgrCfg->getProp("@LibName", libName);
  41. if (libName.isEmpty())
  42. throw MakeStringException(-1, "%s library name not specified for %s", lsm, bindingName);
  43. //TODO Search for LibName in plugins folder, or in specified location
  44. instFactory.set(secMgrCfg->queryProp("@InstanceFactoryName"));
  45. if (instFactory.isEmpty())
  46. instFactory.set("createInstance");
  47. //Load the DLL/SO
  48. HINSTANCE pluggableSecLib = LoadSharedObject(libName.str(), true, false);
  49. if(pluggableSecLib == NULL)
  50. throw MakeStringException(-1, "%s can't load library %s for %s", lsm, libName.str(), bindingName);
  51. //Retrieve address of exported ISecManager instance factory
  52. newPluggableSecManager_t_ xproc = NULL;
  53. xproc = (newPluggableSecManager_t_)GetSharedProcedure(pluggableSecLib, instFactory.str());
  54. if (xproc == NULL)
  55. throw MakeStringException(-1, "%s cannot locate procedure %s of '%s'", lsm, instFactory.str(), libName.str());
  56. //Call ISecManager instance factory and return the new instance
  57. DBGLOG("Calling '%s' in pluggable security manager '%s'", instFactory.str(), libName.str());
  58. return xproc(bindingName, *secMgrCfg, *bindingCfg);
  59. }
  60. static ISecManager* loadSecManager(const char* model_name, const char* servicename, IPropertyTree* cfg)
  61. {
  62. if (!model_name || !*model_name)
  63. throw MakeStringExceptionDirect(-1, "Security model not specified");
  64. StringBuffer realName;
  65. if(stricmp(model_name, "LdapSecurity") == 0)
  66. {
  67. realName.append(SharedObjectPrefix).append(LDAPSECLIB).append(SharedObjectExtension);
  68. HINSTANCE ldapseclib = LoadSharedObject(realName.str(), true, false);
  69. if(ldapseclib == NULL)
  70. throw MakeStringException(-1, "can't load library %s", realName.str());
  71. newSecManager_t_ xproc = NULL;
  72. xproc = (newSecManager_t_)GetSharedProcedure(ldapseclib, "newLdapSecManager");
  73. if (xproc)
  74. return xproc(servicename, *cfg);
  75. else
  76. throw MakeStringException(-1, "procedure newLdapSecManager of %s can't be loaded", realName.str());
  77. }
  78. else
  79. throw MakeStringException(-1, "Security model %s not supported", model_name);
  80. }
  81. static IAuthMap* loadTheDefaultAuthMap(IPropertyTree* cfg)
  82. {
  83. HINSTANCE seclib = LoadSharedObject(LDAPSECLIB, true, false); // ,false,true may actually be more helpful.
  84. if(seclib == NULL)
  85. throw MakeStringException(-1, "can't load library %s", LDAPSECLIB);
  86. createDefaultAuthMap_t_ xproc = NULL;
  87. xproc = (createDefaultAuthMap_t_)GetSharedProcedure(seclib, "newDefaultAuthMap");
  88. if (xproc)
  89. return xproc(cfg);
  90. else
  91. throw MakeStringException(-1, "procedure newDefaultAuthMap of %s can't be loaded", LDAPSECLIB);
  92. }
  93. };
  94. #endif