secloader.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. template <class SECMGR>
  37. static SECMGR* loadPluggableSecManager(const char * bindingName, IPropertyTree* bindingCfg, IPropertyTree* secMgrCfg)
  38. {
  39. const char * lsm = "Load Security Manager :";
  40. StringBuffer libName, instFactory;
  41. secMgrCfg->getProp("@LibName", libName);
  42. if (libName.isEmpty())
  43. {
  44. // @libName is commonly used
  45. secMgrCfg->getProp("@libName", libName);
  46. if (libName.isEmpty())
  47. throw MakeStringException(-1, "%s library name not specified for %s", lsm, bindingName);
  48. }
  49. //TODO Search for LibName in plugins folder, or in specified location
  50. instFactory.set(secMgrCfg->queryProp("@InstanceFactoryName"));
  51. if (instFactory.isEmpty())
  52. {
  53. // @instanceFactoryName is commonly used
  54. instFactory.set(secMgrCfg->queryProp("@instanceFactoryName"));
  55. if (instFactory.isEmpty())
  56. instFactory.set("createInstance");
  57. }
  58. //Load the DLL/SO
  59. HINSTANCE pluggableSecLib = LoadSharedObject(libName.str(), true, false);
  60. if(pluggableSecLib == NULL)
  61. throw MakeStringException(-1, "%s can't load library %s for %s", lsm, libName.str(), bindingName);
  62. //Retrieve address of exported SECMGR instance factory
  63. newPluggableSecManager_t_ xproc = NULL;
  64. xproc = (newPluggableSecManager_t_)GetSharedProcedure(pluggableSecLib, instFactory.str());
  65. if (xproc == NULL)
  66. throw MakeStringException(-1, "%s cannot locate procedure %s of '%s'", lsm, instFactory.str(), libName.str());
  67. //Call SECMGR instance factory and return the new instance
  68. DBGLOG("Calling '%s' in pluggable security manager '%s'", instFactory.str(), libName.str());
  69. SECMGR* pPSM = dynamic_cast<SECMGR*>(xproc(bindingName, *secMgrCfg, *bindingCfg));
  70. if (pPSM == nullptr)
  71. throw MakeStringException(-1, "%s Security Manager %s failed to instantiate in call to %s", lsm, libName.str(), instFactory.str());
  72. return pPSM;
  73. }
  74. static ISecManager* loadSecManager(const char* model_name, const char* servicename, IPropertyTree* cfg)
  75. {
  76. if (!model_name || !*model_name)
  77. throw MakeStringExceptionDirect(-1, "Security model not specified");
  78. StringBuffer realName;
  79. if(stricmp(model_name, "LdapSecurity") == 0)
  80. {
  81. realName.append(SharedObjectPrefix).append(LDAPSECLIB).append(SharedObjectExtension);
  82. HINSTANCE ldapseclib = LoadSharedObject(realName.str(), true, false);
  83. if(ldapseclib == NULL)
  84. throw MakeStringException(-1, "can't load library %s", realName.str());
  85. newSecManager_t_ xproc = NULL;
  86. xproc = (newSecManager_t_)GetSharedProcedure(ldapseclib, "newLdapSecManager");
  87. if (xproc)
  88. return xproc(servicename, *cfg);
  89. else
  90. throw MakeStringException(-1, "procedure newLdapSecManager of %s can't be loaded", realName.str());
  91. }
  92. else
  93. throw MakeStringException(-1, "Security model %s not supported", model_name);
  94. }
  95. static IAuthMap* loadTheDefaultAuthMap(IPropertyTree* cfg)
  96. {
  97. HINSTANCE seclib = LoadSharedObject(LDAPSECLIB, true, false); // ,false,true may actually be more helpful.
  98. if(seclib == NULL)
  99. throw MakeStringException(-1, "can't load library %s", LDAPSECLIB);
  100. createDefaultAuthMap_t_ xproc = NULL;
  101. xproc = (createDefaultAuthMap_t_)GetSharedProcedure(seclib, "newDefaultAuthMap");
  102. if (xproc)
  103. return xproc(cfg);
  104. else
  105. throw MakeStringException(-1, "procedure newDefaultAuthMap of %s can't be loaded", LDAPSECLIB);
  106. }
  107. };
  108. #endif