espcfg.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. #pragma warning( disable : 4786 )
  14. #ifdef _WIN32
  15. //#define ESP_SUPPORT_DALI_CONFIG
  16. //CRT
  17. #include <process.h>
  18. #endif
  19. //Jlib
  20. #include "jliball.hpp"
  21. //SCM Interfaces
  22. #include "esp.hpp"
  23. #include "espplugin.hpp"
  24. #include "espplugin.ipp"
  25. #include "espcfg.ipp"
  26. #include "xslprocessor.hpp"
  27. #include "espcontext.hpp"
  28. #include <dalienv.hpp>
  29. /*
  30. #if defined(USING_MPATROL)
  31. #define ESP_BUILTIN
  32. #endif
  33. */
  34. //#define ESP_BUILTIN
  35. extern "C" {
  36. ESP_FACTORY IEspService * esp_service_factory(const char *name, const char* type, IPropertyTree *cfg, const char *process);
  37. ESP_FACTORY IEspRpcBinding * esp_binding_factory(const char *name, const char* type, IPropertyTree *cfg, const char *process);
  38. ESP_FACTORY IEspProtocol * esp_protocol_factory(const char *name, const char* type, IPropertyTree *cfg, const char *process);
  39. };
  40. #ifdef ESP_BUILTIN
  41. builtin espdirect;
  42. #endif
  43. // add suffix and prefix when necessary
  44. void fixPlugin(StringBuffer& plugin)
  45. {
  46. if (stricmp(plugin.str()+plugin.length()-sizeof(SharedObjectExtension)+1,SharedObjectExtension)==0)
  47. return;
  48. plugin.insert(0,SharedObjectPrefix);
  49. plugin.append(SharedObjectExtension);
  50. }
  51. void CEspConfig::loadBuiltIns()
  52. {
  53. #ifdef ESP_BUILTIN
  54. espdirect.prot = esp_protocol_factory;
  55. espdirect.bind = esp_binding_factory;
  56. espdirect.serv = esp_service_factory;
  57. #endif
  58. }
  59. builtin *CEspConfig::getBuiltIn(string name)
  60. {
  61. #ifdef ESP_BUILTIN
  62. //if (name.compare("pixall.dll")==0 || name.compare("pixall.so")==0)
  63. return &espdirect;
  64. #else //ESP_DIRECT
  65. return NULL;
  66. #endif
  67. }
  68. StringBuffer &CVSBuildToEspVersion(char const * tag, StringBuffer & out)
  69. {
  70. unsigned build = 0;
  71. unsigned subbuild = 0;
  72. while(!isdigit(*tag))
  73. {
  74. if(!*tag) break;
  75. tag++;
  76. }
  77. while(isdigit(*tag))
  78. {
  79. if(!*tag) break;
  80. build = 10*build + (*tag-'0');
  81. tag++;
  82. }
  83. if(isalpha(*tag))
  84. {
  85. if(islower(*tag))
  86. subbuild = *tag-'a'+1;
  87. else
  88. subbuild = *tag-'A'+1;
  89. }
  90. out.append(build/10).append('.').append(build%10).append(subbuild);
  91. return out;
  92. }
  93. int CSessionCleaner::run()
  94. {
  95. try
  96. {
  97. PROGLOG("CSessionCleaner Thread started.");
  98. VStringBuffer xpath("%s*", PathSessionSession);
  99. int checkSessionTimeoutMillSeconds = checkSessionTimeoutSeconds * 60;
  100. while(!stopping)
  101. {
  102. Owned<IRemoteConnection> conn = querySDS().connect(espSessionSDSPath.get(), myProcessSession(), RTM_LOCK_WRITE, SESSION_SDS_LOCK_TIMEOUT);
  103. if (!conn)
  104. throw MakeStringException(-1, "Failed to connect to %s.", PathSessionRoot);
  105. CDateTime now;
  106. now.setNow();
  107. time_t timeNow = now.getSimple();
  108. Owned<IPropertyTreeIterator> iter1 = conn->queryRoot()->getElements(PathSessionApplication);
  109. ForEach(*iter1)
  110. {
  111. ICopyArrayOf<IPropertyTree> toRemove;
  112. Owned<IPropertyTreeIterator> iter2 = iter1->query().getElements(xpath.str());
  113. ForEach(*iter2)
  114. {
  115. IPropertyTree& item = iter2->query();
  116. if (timeNow >= item.getPropInt64(PropSessionTimeoutAt, 0))
  117. toRemove.append(item);
  118. }
  119. ForEachItemIn(i, toRemove)
  120. {
  121. iter1->query().removeTree(&toRemove.item(i));
  122. }
  123. }
  124. sem.wait(checkSessionTimeoutMillSeconds);
  125. }
  126. }
  127. catch(IException *e)
  128. {
  129. StringBuffer msg;
  130. ERRLOG("CSessionCleaner::run() Exception %d:%s", e->errorCode(), e->errorMessage(msg).str());
  131. e->Release();
  132. }
  133. catch(...)
  134. {
  135. ERRLOG("Unknown CSessionCleaner::run() Exception");
  136. }
  137. return 0;
  138. }
  139. void CEspConfig::ensureSDSSessionDomains()
  140. {
  141. bool hasAuthDomainSettings = false;
  142. bool hasSessionAuth = false;
  143. bool hasDefaultSessionDomain = false;
  144. int serverSessionTimeoutSeconds = 120 * ESP_SESSION_TIMEOUT;
  145. Owned<IPropertyTree> proc_cfg = getProcessConfig(m_envpt, m_process.str());
  146. Owned<IPropertyTreeIterator> it = proc_cfg->getElements("AuthDomains/AuthDomain");
  147. ForEach(*it)
  148. {
  149. hasAuthDomainSettings = true;
  150. IPropertyTree& authDomain = it->query();
  151. const char* authType = authDomain.queryProp("@authType");
  152. if (isEmptyString(authType) || (!strieq(authType, "AuthPerSessionOnly") && !strieq(authType, "AuthTypeMixed")))
  153. continue;
  154. hasSessionAuth = true;
  155. int clientSessionTimeoutSeconds;
  156. int clientSessionTimeoutMinutes = authDomain.getPropInt("@clientSessionTimeoutMinutes", ESP_SESSION_TIMEOUT);
  157. if (clientSessionTimeoutMinutes < 0)
  158. clientSessionTimeoutSeconds = ESP_SESSION_NEVER_TIMEOUT;
  159. else
  160. clientSessionTimeoutSeconds = clientSessionTimeoutMinutes * 60;
  161. //The serverSessionTimeoutMinutes is used to clean the sessions by ESP server after the sessions have been timed out on ESP clients.
  162. //Considering possible network delay, serverSessionTimeoutMinutes should be greater than clientSessionTimeoutMinutes.
  163. int serverSessionTimeoutMinutes = authDomain.getPropInt("@serverSessionTimeoutMinutes", 0);
  164. if ((serverSessionTimeoutMinutes < 0) || (clientSessionTimeoutMinutes < 0))
  165. serverSessionTimeoutSeconds = ESP_SESSION_NEVER_TIMEOUT;
  166. else
  167. serverSessionTimeoutSeconds = serverSessionTimeoutMinutes * 60;
  168. if (serverSessionTimeoutSeconds < clientSessionTimeoutSeconds)
  169. serverSessionTimeoutSeconds = 2 * clientSessionTimeoutSeconds;
  170. const char* authDomainName = authDomain.queryProp("@domainName");
  171. if (isEmptyString(authDomainName) || strieq(authDomainName, "default"))
  172. {
  173. if (hasDefaultSessionDomain)
  174. throw MakeStringException(-1, ">1 AuthDomains are not named.");
  175. hasDefaultSessionDomain = true;
  176. }
  177. }
  178. //Ensure SDS Session tree if there is session auth or there is no AuthDomain setting (ex. old environment.xml)
  179. if (hasSessionAuth || !hasAuthDomainSettings)
  180. {
  181. Owned<IRemoteConnection> conn = querySDS().connect(PathSessionRoot, myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SESSION_SDS_LOCK_TIMEOUT);
  182. if (!conn)
  183. throw MakeStringException(-1, "Failed to connect to %s.", PathSessionRoot);
  184. ensureESPSessionInTree(conn->queryRoot(), m_process.str());
  185. if (serverSessionTimeoutSeconds != ESP_SESSION_NEVER_TIMEOUT)
  186. {
  187. VStringBuffer espSessionSDSPath("%s/%s[@name=\"%s\"]", PathSessionRoot, PathSessionProcess, m_process.str());
  188. m_sessionCleaner.setown(new CSessionCleaner(espSessionSDSPath.str(), proc_cfg->getPropInt("@checkSessionTimeoutSeconds",
  189. ESP_CHECK_SESSION_TIMEOUT)));
  190. m_sessionCleaner->start();
  191. }
  192. }
  193. }
  194. void CEspConfig::ensureESPSessionInTree(IPropertyTree* sessionRoot, const char* procName)
  195. {
  196. VStringBuffer xpath("%s[@name=\"%s\"]", PathSessionProcess, procName);
  197. IPropertyTree* procSessionTree = sessionRoot->queryBranch(xpath.str());
  198. if (!procSessionTree)
  199. {
  200. IPropertyTree* processSessionTree = sessionRoot->addPropTree(PathSessionProcess);
  201. processSessionTree->setProp("@name", procName);
  202. }
  203. }
  204. CEspConfig::CEspConfig(IProperties* inputs, IPropertyTree* envpt, IPropertyTree* procpt, bool isDali)
  205. {
  206. hsami_=0;
  207. serverstatus=NULL;
  208. useDali=false;
  209. if(inputs)
  210. m_inputs.setown(inputs);
  211. if(!envpt || !procpt)
  212. return;
  213. m_envpt.setown(envpt);
  214. m_cfg.setown(procpt);
  215. loadBuiltIns();
  216. // load options
  217. const char* level = m_cfg->queryProp("@logLevel");
  218. m_options.logLevel = level ? atoi(level) : LogMin;
  219. m_options.logReq = m_cfg->getPropBool("@logRequests", true);
  220. m_options.logResp = m_cfg->getPropBool("@logResponses", false);
  221. m_options.txSummaryLevel = m_cfg->getPropInt("@txSummaryLevel", LogMin);
  222. m_options.txSummaryResourceReq = m_cfg->getPropBool("@txSummaryResourceReq", false);
  223. m_options.frameTitle.set(m_cfg->queryProp("@name"));
  224. m_options.slowProcessingTime = m_cfg->getPropInt("@slowProcessingTime", 30) * 1000; //in msec
  225. if (!m_cfg->getProp("@name", m_process))
  226. {
  227. ERRLOG("EspProcess name not found");
  228. }
  229. else
  230. {
  231. DBGLOG("ESP process name [%s]", m_process.str());
  232. IPropertyTreeIterator *pt_iter = NULL;
  233. StringBuffer daliservers;
  234. if (m_cfg->getProp("@daliServers", daliservers))
  235. initDali(daliservers.str());
  236. #ifndef _DEBUG
  237. startPerformanceMonitor(m_cfg->getPropInt("@perfReportDelay", 60)*1000);
  238. #endif
  239. //get the local computer name:
  240. m_cfg->getProp("@computer", m_computer);
  241. //get the local computer information:
  242. StringBuffer xpath;
  243. xpath.appendf("Hardware/Computer[@name=\"%s\"]", m_computer.str());
  244. IPropertyTree *computer = m_envpt->queryPropTree(xpath.str());
  245. if (computer)
  246. {
  247. StringBuffer address;
  248. computer->getProp("@netAddress", address);
  249. int port = m_cfg->getPropInt("@port", 1500);
  250. if(strcmp(address.str(), ".") == 0)
  251. {
  252. GetHostName(address.clear());
  253. }
  254. m_address.set(address.str(), (unsigned short) port);
  255. }
  256. xpath.clear();
  257. xpath.append("EspService");
  258. pt_iter = m_cfg->getElements(xpath.str());
  259. if (pt_iter!=NULL)
  260. {
  261. IPropertyTree *ptree = NULL;
  262. pt_iter->first();
  263. while(pt_iter->isValid())
  264. {
  265. ptree = &pt_iter->query();
  266. if (ptree)
  267. {
  268. srv_cfg *svcfg = new srv_cfg;
  269. ptree->getProp("@name", svcfg->name);
  270. ptree->getProp("@type", svcfg->type);
  271. ptree->getProp("@plugin", svcfg->plugin);
  272. fixPlugin(svcfg->plugin);
  273. map<string, srv_cfg*>::value_type en(svcfg->name.str(), svcfg);
  274. m_services.insert(en);
  275. }
  276. pt_iter->next();
  277. }
  278. pt_iter->Release();
  279. pt_iter=NULL;
  280. }
  281. xpath.clear();
  282. xpath.append("EspProtocol");
  283. pt_iter = m_cfg->getElements(xpath.str());
  284. if (pt_iter!=NULL)
  285. {
  286. IPropertyTree *ptree = NULL;
  287. pt_iter->first();
  288. while(pt_iter->isValid())
  289. {
  290. ptree = &pt_iter->query();
  291. if (ptree)
  292. {
  293. protocol_cfg *pcfg = new protocol_cfg;
  294. ptree->getProp("@name", pcfg->name);
  295. ptree->getProp("@plugin", pcfg->plugin);
  296. fixPlugin(pcfg->plugin);
  297. ptree->getProp("@type", pcfg->type);
  298. map<string, protocol_cfg*>::value_type en(pcfg->name.str(), pcfg);
  299. m_protocols.insert(en);
  300. }
  301. pt_iter->next();
  302. }
  303. pt_iter->Release();
  304. pt_iter=NULL;
  305. }
  306. xpath.clear();
  307. xpath.append("EspBinding");
  308. pt_iter = m_cfg->getElements(xpath.str());
  309. if (pt_iter!=NULL)
  310. {
  311. IPropertyTree *ptree = NULL;
  312. pt_iter->first();
  313. while(pt_iter->isValid())
  314. {
  315. ptree = &pt_iter->query();
  316. if (ptree)
  317. {
  318. binding_cfg *bcfg = new binding_cfg;
  319. ptree->getProp("@name", bcfg->name);
  320. ptree->getProp("@type", bcfg->type);
  321. ptree->getProp("@plugin", bcfg->plugin);
  322. fixPlugin(bcfg->plugin);
  323. bcfg->isDefault = ptree->getPropBool("@defaultBinding", false);
  324. StringBuffer addr;
  325. ptree->getProp("@netAddress", addr);
  326. if(strcmp(addr.str(), ".") == 0)
  327. {
  328. bcfg->address.append("0.0.0.0");
  329. }
  330. else
  331. {
  332. bcfg->address.append(addr.str());
  333. }
  334. StringBuffer portstr;
  335. ptree->getProp("@port", portstr);
  336. bcfg->port = atoi(portstr.str());
  337. ptree->getProp("@service", bcfg->service_name);
  338. ptree->getProp("@protocol", bcfg->protocol_name);
  339. m_bindings.push_back(bcfg);
  340. }
  341. pt_iter->next();
  342. }
  343. pt_iter->Release();
  344. pt_iter=NULL;
  345. }
  346. }
  347. }
  348. void CEspConfig::sendAlert(int severity, char const * descr, char const * subject) const
  349. {
  350. }
  351. void CEspConfig::initDali(const char *servers)
  352. {
  353. if (servers!=NULL && *servers!=0 && !daliClientActive())
  354. {
  355. DBGLOG("Initializing DALI client [servers = %s]", servers);
  356. useDali=true;
  357. // Create server group
  358. Owned<IGroup> serverGroup = createIGroup(servers, DALI_SERVER_PORT);
  359. if (!serverGroup)
  360. throw MakeStringException(0, "Could not instantiate dali IGroup");
  361. // Initialize client process
  362. if (!initClientProcess(serverGroup, DCR_EspServer))
  363. throw MakeStringException(0, "Could not initialize dali client");
  364. setPasswordsFromSDS();
  365. serverstatus = new CSDSServerStatus("ESPserver");
  366. ensureSDSSessionDomains();
  367. }
  368. }
  369. void CEspConfig::initPtree(const char *location, bool isDali)
  370. {
  371. IPropertyTree* cfg = createPTreeFromXMLFile(location, ipt_caseInsensitive);
  372. if (cfg)
  373. {
  374. cfg->addProp("@config", location);
  375. m_envpt.setown(cfg);
  376. }
  377. }
  378. void CEspConfig::loadBinding(binding_cfg &xcfg)
  379. {
  380. map<string, srv_cfg*>::iterator sit = m_services.find(xcfg.service_name.str());
  381. map<string, protocol_cfg*>::iterator pit = m_protocols.find(xcfg.protocol_name.str());
  382. IEspService *isrv = NULL;
  383. IEspProtocol *iprot = NULL;
  384. if(sit == m_services.end())
  385. {
  386. DBGLOG("Warning: Service %s not found for the binding", xcfg.service_name.str());
  387. }
  388. else
  389. {
  390. isrv = (*sit).second->srv;
  391. }
  392. if(pit == m_protocols.end())
  393. {
  394. throw MakeStringException(-1, "Protocol %s not found for the binding", xcfg.protocol_name.str());
  395. }
  396. else
  397. {
  398. iprot = (*pit).second->prot;
  399. if (iprot)
  400. {
  401. esp_binding_factory_t xproc = NULL;
  402. if(isrv != NULL)
  403. xcfg.service.setown(LINK(isrv));
  404. xcfg.protocol.setown(LINK(iprot));
  405. builtin *pdirect = getBuiltIn(xcfg.plugin.str());
  406. if (pdirect)
  407. {
  408. xproc = pdirect->bind;
  409. }
  410. else
  411. {
  412. Owned<IEspPlugin> pplg = getPlugin(xcfg.plugin.str());
  413. if (pplg)
  414. {
  415. xproc = (esp_binding_factory_t) pplg->getProcAddress("esp_binding_factory");
  416. }
  417. }
  418. if (xproc)
  419. {
  420. IEspRpcBinding* bind = xproc(xcfg.name.str(), xcfg.type.str(), m_envpt.get(), m_process.str());
  421. if (bind)
  422. DBGLOG("Load binding %s (type: %s, process: %s) succeeded", xcfg.name.str(), xcfg.type.str(), m_process.str());
  423. else
  424. ERRLOG("Failed to load binding %s (type: %s, process: %s)", xcfg.name.str(), xcfg.type.str(), m_process.str());
  425. xcfg.bind.setown(bind);
  426. if (serverstatus)
  427. {
  428. IPropertyTree *stTree= serverstatus->queryProperties()->addPropTree("ESPservice", createPTree("ESPservice", ipt_caseInsensitive));
  429. if (stTree)
  430. {
  431. stTree->setProp("@type", xcfg.service->getServiceType());
  432. stTree->setProp("@name", xcfg.service_name.str());
  433. stTree->setPropInt("@port", xcfg.port);
  434. }
  435. serverstatus->commitProperties();
  436. }
  437. }
  438. else
  439. throw MakeStringException(-1, "procedure esp_binding_factory can't be loaded");
  440. }
  441. else
  442. {
  443. throw MakeStringException(-1, "Protocol %s wasn't loaded correctly for the binding", xcfg.protocol_name.str());
  444. }
  445. }
  446. }
  447. void CEspConfig::loadProtocol(protocol_cfg &xcfg)
  448. {
  449. esp_protocol_factory_t xproc = NULL;
  450. builtin *pdirect = getBuiltIn(xcfg.plugin.str());
  451. if (pdirect)
  452. xproc = pdirect->prot;
  453. else
  454. {
  455. Owned<IEspPlugin> pplg = getPlugin(xcfg.plugin.str());
  456. if (pplg)
  457. {
  458. xproc = (esp_protocol_factory_t) pplg->getProcAddress("esp_protocol_factory");
  459. }
  460. }
  461. if (xproc)
  462. {
  463. xcfg.prot.setown(xproc(xcfg.name.str(), xcfg.type.str(), m_envpt.get(), m_process.str()));
  464. if (xcfg.prot)
  465. xcfg.prot->init(m_envpt.get(), m_process.str(), xcfg.name.str());
  466. }
  467. else
  468. throw MakeStringException(-1, "procedure esp_protocol_factory can't be loaded");
  469. }
  470. void CEspConfig::loadService(srv_cfg &xcfg)
  471. {
  472. esp_service_factory_t xproc = NULL;
  473. builtin *pdirect = getBuiltIn(xcfg.plugin.str());
  474. if (pdirect)
  475. xproc = pdirect->serv;
  476. else
  477. {
  478. Owned<IEspPlugin> pplg = getPlugin(xcfg.plugin.str());
  479. if (pplg)
  480. xproc = (esp_service_factory_t) pplg->getProcAddress("esp_service_factory");
  481. }
  482. if (xproc)
  483. xcfg.srv.setown(xproc(xcfg.name.str(), xcfg.type.str(), m_envpt.get(), m_process.str()));
  484. else
  485. throw MakeStringException(-1, "procedure esp_service_factory can't be loaded");
  486. }
  487. void CEspConfig::loadServices()
  488. {
  489. map<string, srv_cfg*>::iterator iter = m_services.begin();
  490. while (iter!=m_services.end())
  491. {
  492. #ifndef _USE_OPENLDAP
  493. const string svcName = iter->first;
  494. if (!strstr(svcName.data(), "ws_access"))
  495. #endif
  496. loadService(*(iter->second));
  497. #ifndef _USE_OPENLDAP
  498. else
  499. DBGLOG("Not loading service %s, platform built without LDAP", svcName.data());
  500. #endif
  501. iter++;
  502. }
  503. }
  504. void CEspConfig::loadProtocols()
  505. {
  506. map<string, protocol_cfg*>::iterator iter = m_protocols.begin();
  507. while (iter!=m_protocols.end())
  508. {
  509. loadProtocol(*(iter->second));
  510. iter++;
  511. }
  512. }
  513. void CEspConfig::loadBindings()
  514. {
  515. list<binding_cfg*>::iterator iter = m_bindings.begin();
  516. while (iter!=m_bindings.end())
  517. {
  518. #ifndef _USE_OPENLDAP
  519. const char * bindingName = (**iter).name.str();
  520. if (!strstr(bindingName, "ws_access"))
  521. #endif
  522. loadBinding(**iter);
  523. #ifndef _USE_OPENLDAP
  524. else
  525. DBGLOG("Not binding %s, platform built without LDAP", bindingName);
  526. #endif
  527. iter++;
  528. }
  529. }
  530. class ESPxsltIncludeHandler : public CInterface, implements IIncludeHandler
  531. {
  532. public:
  533. // IMPLEMENT_IINTERFACE;
  534. virtual void Link() const
  535. {
  536. CInterface::Link();
  537. }
  538. virtual bool Release() const
  539. {
  540. return CInterface::Release();
  541. }
  542. ESPxsltIncludeHandler()
  543. {
  544. }
  545. ~ESPxsltIncludeHandler()
  546. {
  547. }
  548. inline bool fileExists(StringBuffer &filename)
  549. {
  550. return (checkFileExists(filename.str()) || checkFileExists(filename.toUpperCase().str()) || checkFileExists(filename.toLowerCase().str()));
  551. }
  552. inline bool fileRead(const char *filename, MemoryBuffer &buff)
  553. {
  554. Owned<IFile> fi=createIFile(filename);
  555. if (fi)
  556. {
  557. Owned<IFileIO> fio=fi->open(IFOread);
  558. if (fio)
  559. {
  560. offset_t len=fio->size();
  561. size32_t memlen = (size32_t)len;
  562. assertex(len == memlen);
  563. if (fio->read(0, memlen, buff.reserveTruncate(memlen))==len)
  564. return true;
  565. }
  566. }
  567. buff.clear();
  568. return false;
  569. }
  570. const char *pastLast(const char *src, const char *fnd)
  571. {
  572. int fndlen=(fnd) ? strlen(fnd) : 0;
  573. int srclen=(src) ? strlen(src) : 0;
  574. if (fndlen && srclen)
  575. {
  576. while (srclen--)
  577. {
  578. if (!strnicmp(src+srclen, fnd, fndlen))
  579. return src+srclen+fndlen;
  580. }
  581. }
  582. return NULL;
  583. }
  584. //IIncludeHandler
  585. bool getInclude(const char* includename, MemoryBuffer& includebuf, bool& pathOnly)
  586. {
  587. if(!includename)
  588. return false;
  589. pathOnly = true;
  590. includebuf.clear();
  591. const char *finger=pastLast(includename, "esp/xslt/");
  592. if (finger)
  593. {
  594. StringBuffer filepath;
  595. if (fileExists(filepath.append(getCFD()).append("smc_xslt/").append(finger)) || fileExists(filepath.clear().append(getCFD()).append("xslt/").append(finger)))
  596. {
  597. includebuf.append(filepath.length(), filepath.str());
  598. return true;
  599. }
  600. }
  601. else
  602. {
  603. // First of all, it's better to use absolute path to specify the include, like /esp/xslt/ui_overrides.xslt.
  604. // When you specify the include as relative path, for example ./ui_overrides.xslt
  605. // the path will be expanded (by xmllib's source resolver) to its full path, beginning with file://
  606. // on windows it looks like: file:///C:/playground/esp_lsb2/xslt/ui_overrides.xslt
  607. // on linux: file:///home/yma/playground/esp_lsb2/xslt/ui_overrides.xslt
  608. // If current path not found, use root
  609. char dir[_MAX_PATH];
  610. if (!GetCurrentDirectory(sizeof(dir), dir)) {
  611. ERRLOG("ESPxsltIncludeHandler::getInclude: Current directory path too big, setting local path to null");
  612. dir[0] = 0;
  613. }
  614. #ifdef _WIN32
  615. for(int i = 0; i < _MAX_PATH; i++)
  616. {
  617. if(dir[i] == '\0')
  618. break;
  619. else if(dir[i] == PATHSEPCHAR)
  620. dir[i] = '/';
  621. }
  622. #endif
  623. finger = strstr(includename, dir);
  624. if(finger)
  625. {
  626. finger += strlen(dir) + 1;
  627. StringBuffer filepath(finger);
  628. if (fileExists(filepath))
  629. {
  630. includebuf.append(filepath.length(), filepath.str());
  631. return true;
  632. }
  633. }
  634. }
  635. return false;
  636. }
  637. };
  638. ESPxsltIncludeHandler g_includeHandler;
  639. void CEspConfig::bindServer(IEspServer &server, IEspContainer &container)
  640. {
  641. list<binding_cfg*>::iterator bit = m_bindings.begin();
  642. while (bit != m_bindings.end())
  643. {
  644. binding_cfg *pbfg = *bit;
  645. if (pbfg && pbfg->bind && pbfg->service && pbfg->protocol)
  646. {
  647. map<string, protocol_cfg*>::iterator pit = m_protocols.find(pbfg->protocol_name.str());
  648. if(pit == m_protocols.end())
  649. DBGLOG("Protocol %s not found for binding %s", pbfg->protocol_name.str(), pbfg->name.str());
  650. else
  651. {
  652. Owned<IXslProcessor> xslp=getXslProcessor();
  653. if (xslp)
  654. {
  655. xslp->setDefIncludeHandler(dynamic_cast<IIncludeHandler*>(&g_includeHandler));
  656. pbfg->bind->setXslProcessor(xslp);
  657. }
  658. pbfg->bind->setContainer(&container);
  659. pbfg->service->setContainer(&container);
  660. pbfg->protocol->setContainer(&container);
  661. pbfg->bind->addProtocol(pbfg->protocol->getProtocolName(), *pbfg->protocol.get());
  662. if(pbfg->service != NULL)
  663. pbfg->bind->addService(pbfg->service->getServiceType(), pbfg->address.str(), pbfg->port, *pbfg->service.get());
  664. IEspProtocol* prot = (*pit).second->prot;
  665. server.addBinding(pbfg->name.str(), pbfg->address.str(), pbfg->port, *prot, *(pbfg->bind.get()), pbfg->isDefault, m_cfg.get());
  666. }
  667. }
  668. else
  669. {
  670. ERRLOG("Binding %s wasn't loaded correctly", pbfg->name.str());
  671. }
  672. bit++;
  673. }
  674. }
  675. void CEspConfig::unloadBindings()
  676. {
  677. list<binding_cfg*>::iterator iter = m_bindings.begin();
  678. while (iter!=m_bindings.end())
  679. {
  680. binding_cfg *bcfg = *iter;
  681. if(bcfg!=NULL)
  682. {
  683. bcfg->protocol.clear();
  684. bcfg->bind.clear();
  685. bcfg->service.clear();
  686. delete bcfg;
  687. }
  688. iter++;
  689. }
  690. m_bindings.clear();
  691. }
  692. void CEspConfig::unloadServices()
  693. {
  694. map<string, srv_cfg*>::iterator srvi = m_services.begin();
  695. while (srvi!=m_services.end())
  696. {
  697. srv_cfg* scfg = srvi->second;
  698. if(scfg)
  699. {
  700. scfg->cfg.clear();
  701. scfg->srv.clear();
  702. delete scfg;
  703. }
  704. srvi++;
  705. }
  706. m_services.clear();
  707. }
  708. void CEspConfig::unloadProtocols()
  709. {
  710. map<string, protocol_cfg*>::iterator proti = m_protocols.begin();
  711. while (proti!=m_protocols.end())
  712. {
  713. protocol_cfg *pcfg = proti->second;
  714. if(pcfg)
  715. {
  716. pcfg->prot.clear();
  717. pcfg->cfg.clear();
  718. delete pcfg;
  719. }
  720. proti++;
  721. }
  722. m_protocols.clear();
  723. }
  724. IEspPlugin* CEspConfig::getPlugin(const char* name)
  725. {
  726. if(!name || !*name)
  727. return NULL;
  728. ForEachItemIn(x, m_plugins)
  729. {
  730. IEspPlugin* plgn = &m_plugins.item(x);
  731. if(plgn && stricmp(name, plgn->getName()) == 0)
  732. {
  733. return LINK(plgn);
  734. }
  735. }
  736. Owned<IEspPlugin> pplg = loadPlugin(name);
  737. if(pplg)
  738. {
  739. pplg->Link(); //YMA: intentional leak. Unloading DLLs during ESP shutdown causes all kinds of issues.
  740. m_plugins.append(*LINK(pplg));
  741. return LINK(pplg);
  742. }
  743. return NULL;
  744. }
  745. bool CEspConfig::checkESPCache()
  746. {
  747. bool espCacheAvailable = false ;
  748. list<binding_cfg*>::iterator iter = m_bindings.begin();
  749. while (iter!=m_bindings.end())
  750. {
  751. binding_cfg& xcfg = **iter;
  752. if (xcfg.bind->getCacheMethodCount() > 0)
  753. {
  754. Owned<IEspCache> espCache = createESPCache(m_cfg->queryProp("@espCacheInitString"));
  755. espCacheAvailable = (espCache != nullptr);
  756. break;
  757. }
  758. iter++;
  759. }
  760. return espCacheAvailable;
  761. }