ldapsecurity.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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. #define AXA_API DECL_EXPORT
  14. #include "ldapsecurity.ipp"
  15. #include "ldapsecurity.hpp"
  16. #include "authmap.ipp"
  17. /**********************************************************
  18. * CLdapSecUser *
  19. **********************************************************/
  20. CLdapSecUser::CLdapSecUser(const char *name, const char *pw) :
  21. m_pw(pw), m_authenticateStatus(AS_UNKNOWN)
  22. {
  23. setName(name);
  24. setUserID(0);
  25. setPosixenabled(false);
  26. setSudoersEnabled(false);
  27. setInSudoers(false);
  28. }
  29. CLdapSecUser::~CLdapSecUser()
  30. {
  31. }
  32. //non-interfaced functions
  33. void CLdapSecUser::setUserID(unsigned userid)
  34. {
  35. m_userid = userid;
  36. }
  37. void CLdapSecUser::setUserSid(int sidlen, const char* sid)
  38. {
  39. m_usersid.clear();
  40. m_usersid.append(sidlen, sid);
  41. }
  42. MemoryBuffer& CLdapSecUser::getUserSid()
  43. {
  44. return m_usersid;
  45. }
  46. //interface ISecUser
  47. const char * CLdapSecUser::getName()
  48. {
  49. return m_name.get();
  50. }
  51. bool CLdapSecUser::setName(const char * name)
  52. {
  53. if(name != NULL)
  54. {
  55. const char* atsign = strchr(name, '@');
  56. if(atsign != NULL)
  57. {
  58. m_name.set(name, atsign - name);
  59. m_realm.set(atsign + 1);
  60. }
  61. else
  62. {
  63. m_name.set(name);
  64. }
  65. }
  66. return TRUE;
  67. }
  68. const char * CLdapSecUser::getFullName()
  69. {
  70. return m_fullname.get();
  71. }
  72. bool CLdapSecUser::setFullName(const char * name)
  73. {
  74. if(name != NULL)
  75. {
  76. m_fullname.set(name);
  77. }
  78. return true;
  79. }
  80. const char * CLdapSecUser::getFirstName()
  81. {
  82. return m_firstname.get();
  83. }
  84. bool CLdapSecUser::setFirstName(const char * fname)
  85. {
  86. if(fname != NULL)
  87. {
  88. m_firstname.set(fname);
  89. }
  90. return true;
  91. }
  92. const char * CLdapSecUser::getLastName()
  93. {
  94. return m_lastname.get();
  95. }
  96. bool CLdapSecUser::setLastName(const char * lname)
  97. {
  98. if(lname != NULL)
  99. {
  100. m_lastname.set(lname);
  101. }
  102. return true;
  103. }
  104. const char * CLdapSecUser::getEmployeeID()
  105. {
  106. return m_employeeID.get();
  107. }
  108. bool CLdapSecUser::setEmployeeID(const char * emplID)
  109. {
  110. m_employeeID.set(emplID);
  111. return true;
  112. }
  113. const char * CLdapSecUser::getDistinguishedName()
  114. {
  115. return m_distinguishedName.get();
  116. }
  117. bool CLdapSecUser::setDistinguishedName(const char * dn)
  118. {
  119. m_distinguishedName.set(dn);
  120. return true;
  121. }
  122. const char * CLdapSecUser::getRealm()
  123. {
  124. return m_realm.get();
  125. }
  126. bool CLdapSecUser::setRealm(const char * name)
  127. {
  128. m_realm.set(name);
  129. return TRUE;
  130. }
  131. const char * CLdapSecUser::getFqdn()
  132. {
  133. return m_Fqdn.get();
  134. }
  135. bool CLdapSecUser::setFqdn(const char * Fqdn)
  136. {
  137. m_Fqdn.set(Fqdn);
  138. return true;
  139. }
  140. const char *CLdapSecUser::getPeer()
  141. {
  142. return m_Peer.get();
  143. }
  144. bool CLdapSecUser::setPeer(const char *Peer)
  145. {
  146. m_Peer.set(Peer);
  147. return true;
  148. }
  149. ISecCredentials & CLdapSecUser::credentials()
  150. {
  151. return *this;
  152. }
  153. unsigned CLdapSecUser::getUserID()
  154. {
  155. return m_userid;
  156. }
  157. //interface ISecCredentials
  158. bool CLdapSecUser::setPassword(const char * pw)
  159. {
  160. m_pw.set(pw);
  161. return TRUE;
  162. }
  163. const char* CLdapSecUser::getPassword()
  164. {
  165. return m_pw;
  166. }
  167. bool CLdapSecUser::setEncodedPassword(SecPasswordEncoding enc, void * pw, unsigned length, void * salt, unsigned saltlen)
  168. {
  169. return FALSE; //not supported yet
  170. }
  171. bool CLdapSecUser::addToken(unsigned type, void * data, unsigned length)
  172. {
  173. return FALSE; //not supported yet
  174. }
  175. void CLdapSecUser::copyTo(ISecUser& destination)
  176. {
  177. if (this == &destination)
  178. return;
  179. CLdapSecUser* dest = dynamic_cast<CLdapSecUser*>(&destination);
  180. if(!dest)
  181. return;
  182. dest->setAuthenticateStatus(getAuthenticateStatus());
  183. dest->setName(getName());
  184. dest->setFullName(getFullName());
  185. dest->setFirstName(getFirstName());
  186. dest->setLastName(getLastName());
  187. dest->setEmployeeID(getEmployeeID());
  188. dest->setRealm(getRealm());
  189. dest->credentials().setPassword(credentials().getPassword());
  190. dest->setUserSid(m_usersid.length(), m_usersid.toByteArray());
  191. dest->setUserID(m_userid);
  192. dest->setPasswordExpiration(m_passwordExpiration);
  193. dest->setDistinguishedName(m_distinguishedName);
  194. }
  195. ISecUser * CLdapSecUser::clone()
  196. {
  197. CLdapSecUser* newuser = new CLdapSecUser(m_name.get(), m_pw.get());
  198. if(newuser)
  199. copyTo(*newuser);
  200. return newuser;
  201. }
  202. /**********************************************************
  203. * CLdapSecResource *
  204. **********************************************************/
  205. CLdapSecResource::CLdapSecResource(const char *name) : m_name(name), m_access(SecAccess_None), m_required_access(SecAccess_None)
  206. {
  207. m_resourcetype = RT_DEFAULT;
  208. }
  209. void CLdapSecResource::addAccess(SecAccessFlags flags)
  210. {
  211. m_access = (SecAccessFlags)((int)m_access | (int)flags);
  212. }
  213. void CLdapSecResource::setAccessFlags(SecAccessFlags flags)
  214. {
  215. m_access = flags;
  216. }
  217. void CLdapSecResource::setRequiredAccessFlags(SecAccessFlags flags)
  218. {
  219. m_required_access = flags;
  220. }
  221. SecAccessFlags CLdapSecResource::getRequiredAccessFlags()
  222. {
  223. return m_required_access;
  224. }
  225. //interface ISecResource : extends IInterface
  226. const char * CLdapSecResource::getName()
  227. {
  228. return m_name.get();
  229. }
  230. SecAccessFlags CLdapSecResource::getAccessFlags()
  231. {
  232. return m_access;
  233. }
  234. int CLdapSecResource::addParameter(const char* name, const char* value)
  235. {
  236. if (!m_parameters)
  237. m_parameters.setown(createProperties(false));
  238. m_parameters->setProp(name, value);
  239. return 0;
  240. }
  241. const char * CLdapSecResource::getParameter(const char * name)
  242. {
  243. if (m_parameters)
  244. {
  245. const char *value = m_parameters->queryProp(name);
  246. return value;
  247. }
  248. return NULL;
  249. }
  250. void CLdapSecResource::setDescription(const char* description)
  251. {
  252. m_description.clear().append(description);
  253. }
  254. const char* CLdapSecResource::getDescription()
  255. {
  256. return m_description.str();
  257. }
  258. void CLdapSecResource::setValue(const char* value)
  259. {
  260. m_value.clear();
  261. m_value.append(value);
  262. }
  263. const char* CLdapSecResource::getValue()
  264. {
  265. return m_value.str();
  266. }
  267. ISecResource * CLdapSecResource::clone()
  268. {
  269. CLdapSecResource* _res = new CLdapSecResource(m_name.get());
  270. if(!_res)
  271. return NULL;
  272. _res->setResourceType(m_resourcetype);
  273. _res->setValue(m_value.str());
  274. _res->m_access = m_access;
  275. _res->m_required_access = m_required_access;
  276. _res->setDescription(m_description.str());
  277. if(!m_parameters)
  278. return _res;
  279. Owned<IPropertyIterator> Itr = m_parameters->getIterator();
  280. Itr->first();
  281. while(Itr->isValid())
  282. {
  283. _res->addParameter(Itr->getPropKey(),m_parameters->queryProp(Itr->getPropKey()));
  284. Itr->next();
  285. }
  286. return _res;
  287. }
  288. void CLdapSecResource::copy(ISecResource* from)
  289. {
  290. if(!from)
  291. return;
  292. CLdapSecResource* ldapfrom = dynamic_cast<CLdapSecResource*>(from);
  293. if(!ldapfrom)
  294. return;
  295. m_access = ldapfrom->m_access;
  296. setDescription(ldapfrom->m_description.str());
  297. if(m_parameters.get())
  298. {
  299. m_parameters.clear();
  300. }
  301. if(!ldapfrom->m_parameters.get())
  302. return;
  303. Owned<IPropertyIterator> Itr = ldapfrom->m_parameters->getIterator();
  304. Itr->first();
  305. while(Itr->isValid())
  306. {
  307. addParameter(Itr->getPropKey(), ldapfrom->m_parameters->queryProp(Itr->getPropKey()));
  308. Itr->next();
  309. }
  310. return;
  311. }
  312. SecResourceType CLdapSecResource::getResourceType()
  313. {
  314. return m_resourcetype;
  315. }
  316. void CLdapSecResource::setResourceType(SecResourceType resourcetype)
  317. {
  318. m_resourcetype = resourcetype;
  319. }
  320. /**********************************************************
  321. * CLdapSecResourceList *
  322. **********************************************************/
  323. CLdapSecResourceList::CLdapSecResourceList(const char *name) : m_complete(0)
  324. {
  325. m_name.set(name);
  326. }
  327. void CLdapSecResourceList::setAuthorizationComplete(bool value)
  328. {
  329. m_complete=value;
  330. }
  331. IArrayOf<ISecResource>& CLdapSecResourceList::getResourceList()
  332. {
  333. return m_rlist;
  334. }
  335. //interface ISecResourceList : extends IInterface
  336. bool CLdapSecResourceList::isAuthorizationComplete()
  337. {
  338. return m_complete;
  339. }
  340. ISecResourceList * CLdapSecResourceList::clone()
  341. {
  342. CLdapSecResourceList* _newList = new CLdapSecResourceList(m_name.get());
  343. if(!_newList)
  344. return NULL;
  345. copyTo(*_newList);
  346. return _newList;
  347. }
  348. bool CLdapSecResourceList::copyTo(ISecResourceList& destination)
  349. {
  350. ForEachItemIn(x, m_rlist)
  351. {
  352. CLdapSecResource* res = (CLdapSecResource*)(&(m_rlist.item(x)));
  353. if(res)
  354. destination.addResource(res->clone());
  355. }
  356. return false;
  357. }
  358. ISecResource* CLdapSecResourceList::addResource(const char * name)
  359. {
  360. if(!name || !*name)
  361. {
  362. DBGLOG("CLdapSecResourceList::addResource resource name must be provided");
  363. return NULL;
  364. }
  365. ISecResource* resource = m_rmap[name];
  366. if(resource == NULL)
  367. {
  368. resource = new CLdapSecResource(name);
  369. m_rlist.append(*resource);
  370. m_rmap[name] = resource;
  371. }
  372. return resource;
  373. }
  374. void CLdapSecResourceList::addResource(ISecResource * resource)
  375. {
  376. if(resource == NULL)
  377. {
  378. DBGLOG("CLdapSecResourceList::addResource2 ISecResource cannot be NULL");
  379. return;
  380. }
  381. const char* name = resource->getName();
  382. if(!name || !*name)
  383. {
  384. DBGLOG("CLdapSecResourceList::addResource2 resource name must be provided");
  385. return;
  386. }
  387. ISecResource* r = m_rmap[name];
  388. if(r == NULL)
  389. {
  390. m_rlist.append(*resource);
  391. m_rmap[name] = resource;
  392. }
  393. }
  394. bool CLdapSecResourceList::addCustomResource(const char * name, const char * config)
  395. {
  396. return false;
  397. }
  398. ISecResource * CLdapSecResourceList::getResource(const char * Resource)
  399. {
  400. if(!Resource || !*Resource)
  401. {
  402. DBGLOG("CLdapSecResourceList::getResource resource name must be provided");
  403. return NULL;
  404. }
  405. ISecResource* r = m_rmap[Resource];
  406. if(r)
  407. return LINK(r);
  408. else
  409. return NULL;
  410. }
  411. void CLdapSecResourceList::clear()
  412. {
  413. m_rlist.kill();
  414. }
  415. int CLdapSecResourceList::count()
  416. {
  417. return m_rlist.length();
  418. }
  419. const char* CLdapSecResourceList::getName()
  420. {
  421. return m_name.get();
  422. }
  423. ISecResource * CLdapSecResourceList::queryResource(unsigned seq)
  424. {
  425. if(seq < m_rlist.length())
  426. return &(m_rlist.item(seq));
  427. else
  428. return NULL;
  429. }
  430. ISecPropertyIterator * CLdapSecResourceList::getPropertyItr()
  431. {
  432. return new ArrayIIteratorOf<IArrayOf<struct ISecResource>, ISecProperty, ISecPropertyIterator>(m_rlist);
  433. }
  434. ISecProperty* CLdapSecResourceList::findProperty(const char* name)
  435. {
  436. if(!name || !*name)
  437. {
  438. DBGLOG("CLdapSecResourceList::findProperty property name must be provided");
  439. return NULL;
  440. }
  441. return m_rmap[name];
  442. }
  443. /**********************************************************
  444. * CLdapSecManager *
  445. **********************************************************/
  446. CLdapSecManager::CLdapSecManager(const char *serviceName, const char *config)
  447. {
  448. IPropertyTree* cfg = createPTreeFromXMLString(config, ipt_caseInsensitive);
  449. if(cfg == NULL)
  450. {
  451. throw MakeStringException(-1, "createPTreeFromXMLString() failed for %s", config);
  452. }
  453. init(serviceName, cfg);
  454. }
  455. void CLdapSecManager::init(const char *serviceName, IPropertyTree* cfg)
  456. {
  457. for(int i = 0; i < RT_SCOPE_MAX; i++)
  458. m_cache_off[i] = false;
  459. m_cache_off[RT_VIEW_SCOPE] = true;
  460. m_usercache_off = false;
  461. m_cfg.setown(cfg);
  462. cfg->getProp(".//@ldapAddress", m_server);
  463. cfg->getProp(".//@description", m_description);
  464. ILdapClient* ldap_client = createLdapClient(cfg);
  465. IPermissionProcessor* pp;
  466. if(ldap_client->getServerType() == ACTIVE_DIRECTORY)
  467. pp = new PermissionProcessor(cfg);
  468. else if(ldap_client->getServerType() == IPLANET)
  469. pp = new CIPlanetAciProcessor(cfg);
  470. else if(ldap_client->getServerType() == OPEN_LDAP)
  471. {
  472. if (0 == stricmp(ldap_client->getLdapConfig()->getCfgServerType(), "389DirectoryServer"))//uses iPlanet style ACI
  473. pp = new CIPlanetAciProcessor(cfg);
  474. else
  475. pp = new COpenLdapAciProcessor(cfg);
  476. }
  477. else
  478. throwUnexpected();
  479. pp->setLdapClient(ldap_client);
  480. ldap_client->init(pp);
  481. m_ldap_client.setown(ldap_client);
  482. m_pp.setown(pp);
  483. int cachetimeout = cfg->getPropInt("@cacheTimeout", 5);
  484. if (cfg->getPropBool("@sharedCache", true))
  485. m_permissionsCache.setown(CPermissionsCache::getInstance(cfg->queryProp("@name")));
  486. else
  487. m_permissionsCache.setown(new CPermissionsCache());
  488. m_permissionsCache->setCacheTimeout( 60 * cachetimeout);
  489. m_permissionsCache->setTransactionalEnabled(true);
  490. m_permissionsCache->setSecManager(this);
  491. m_passwordExpirationWarningDays = cfg->getPropInt(".//@passwordExpirationWarningDays", 10); //Default to 10 days
  492. m_checkViewPermissions = cfg->getPropBool(".//@checkViewPermissions", false);
  493. };
  494. CLdapSecManager::CLdapSecManager(const char *serviceName, IPropertyTree &config)
  495. {
  496. init(serviceName, &config);
  497. }
  498. CLdapSecManager::~CLdapSecManager()
  499. {
  500. }
  501. //interface ISecManager : extends IInterface
  502. ISecUser * CLdapSecManager::createUser(const char * user_name)
  503. {
  504. return (new CLdapSecUser(user_name, NULL));
  505. }
  506. ISecResourceList * CLdapSecManager::createResourceList(const char * rlname)
  507. {
  508. return (new CLdapSecResourceList(rlname));
  509. }
  510. bool CLdapSecManager::subscribe(ISecAuthenticEvents & events)
  511. {
  512. m_subscriber.set(&events);
  513. return true;
  514. }
  515. bool CLdapSecManager::unsubscribe(ISecAuthenticEvents & events)
  516. {
  517. if (&events == m_subscriber.get())
  518. {
  519. m_subscriber.set(NULL);
  520. }
  521. return true;
  522. }
  523. bool CLdapSecManager::authenticate(ISecUser* user)
  524. {
  525. if(!user)
  526. {
  527. DBGLOG("CLdapSecManager::authenticate user cannot be NULL");
  528. return false;
  529. }
  530. if(user->getAuthenticateStatus() == AS_AUTHENTICATED)
  531. return true;
  532. if(m_permissionsCache->isCacheEnabled() && !m_usercache_off && m_permissionsCache->lookup(*user))
  533. {
  534. user->setAuthenticateStatus(AS_AUTHENTICATED);
  535. return true;
  536. }
  537. bool ok = m_ldap_client->authenticate(*user);
  538. if(ok)
  539. {
  540. if(m_permissionsCache->isCacheEnabled() && !m_usercache_off)
  541. m_permissionsCache->add(*user);
  542. user->setAuthenticateStatus(AS_AUTHENTICATED);
  543. }
  544. return ok;
  545. }
  546. bool CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * Resources, IEspSecureContext* secureContext)
  547. {
  548. if(!authenticate(&sec_user))
  549. {
  550. return false;
  551. }
  552. CLdapSecResourceList * reslist = (CLdapSecResourceList*)Resources;
  553. if(!reslist)
  554. return true;
  555. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  556. int nResources = rlist.length();
  557. int ri;
  558. for(ri = 0; ri < nResources; ri++)
  559. {
  560. ISecResource* res = &rlist.item(ri);
  561. if(res != NULL)
  562. res->setResourceType(rtype);
  563. }
  564. if (nResources <= 0)
  565. return true;
  566. bool rc;
  567. time_t tctime = getThreadCreateTime();
  568. if ((m_permissionsCache->isCacheEnabled() || (m_permissionsCache->isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  569. {
  570. bool* cached_found = (bool*)alloca(nResources*sizeof(bool));
  571. int nFound = m_permissionsCache->lookup(sec_user, rlist, cached_found);
  572. if (nFound < nResources)
  573. {
  574. IArrayOf<ISecResource> rlist2;
  575. int i;
  576. for (i=0; i < nResources; i++)
  577. {
  578. if (*(cached_found+i) == false)
  579. {
  580. ISecResource& secRes = rlist.item(i);
  581. secRes.Link();
  582. rlist2.append(secRes);
  583. //DBGLOG("CACHE: Fetching permissions for %s:%s", sec_user.getName(), secRes.getName());
  584. }
  585. }
  586. rc = m_ldap_client->authorize(rtype, sec_user, rlist2);
  587. if (rc)
  588. m_permissionsCache->add(sec_user, rlist2);
  589. }
  590. else
  591. rc = true;
  592. }
  593. else
  594. {
  595. rc = m_ldap_client->authorize(rtype, sec_user, rlist, reslist->getName());
  596. }
  597. return rc;
  598. }
  599. SecAccessFlags CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser & user, const char * resourcename, IEspSecureContext* secureContext)
  600. {
  601. if(!resourcename || !*resourcename)
  602. return SecAccess_Full;
  603. Owned<ISecResourceList> rlist;
  604. rlist.setown(createResourceList("resources"));
  605. rlist->addResource(resourcename);
  606. bool ok = authorizeEx(rtype, user, rlist.get(), secureContext);
  607. if(ok)
  608. return rlist->queryResource(0)->getAccessFlags();
  609. else
  610. return SecAccess_Unavailable;
  611. }
  612. bool CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * Resources, bool doAuthentication)
  613. {
  614. if(doAuthentication && !authenticate(&sec_user))
  615. {
  616. return false;
  617. }
  618. CLdapSecResourceList * reslist = (CLdapSecResourceList*)Resources;
  619. if(!reslist)
  620. return true;
  621. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  622. int nResources = rlist.length();
  623. int ri;
  624. for(ri = 0; ri < nResources; ri++)
  625. {
  626. ISecResource* res = &rlist.item(ri);
  627. if(res != NULL)
  628. res->setResourceType(rtype);
  629. }
  630. if (nResources <= 0)
  631. return true;
  632. bool rc;
  633. time_t tctime = getThreadCreateTime();
  634. if ((m_permissionsCache->isCacheEnabled() || (m_permissionsCache->isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  635. {
  636. bool* cached_found = (bool*)alloca(nResources*sizeof(bool));
  637. int nFound = m_permissionsCache->lookup(sec_user, rlist, cached_found);
  638. if (nFound < nResources)
  639. {
  640. IArrayOf<ISecResource> rlist2;
  641. int i;
  642. for (i=0; i < nResources; i++)
  643. {
  644. if (*(cached_found+i) == false)
  645. {
  646. ISecResource& secRes = rlist.item(i);
  647. secRes.Link();
  648. rlist2.append(secRes);
  649. //DBGLOG("CACHE: Fetching permissions for %s:%s", sec_user.getName(), secRes.getName());
  650. }
  651. }
  652. rc = m_ldap_client->authorize(rtype, sec_user, rlist2);
  653. if (rc)
  654. m_permissionsCache->add(sec_user, rlist2);
  655. }
  656. else
  657. rc = true;
  658. }
  659. else
  660. {
  661. rc = m_ldap_client->authorize(rtype, sec_user, rlist);
  662. }
  663. return rc;
  664. }
  665. SecAccessFlags CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser & user, const char * resourcename, bool doAuthentication)
  666. {
  667. if(!resourcename || !*resourcename)
  668. return SecAccess_Full;
  669. Owned<ISecResourceList> rlist;
  670. rlist.setown(createResourceList("resources"));
  671. rlist->addResource(resourcename);
  672. bool ok = authorizeEx(rtype, user, rlist.get(), doAuthentication);
  673. if(ok)
  674. return rlist->queryResource(0)->getAccessFlags();
  675. else
  676. return SecAccess_Unavailable;
  677. }
  678. SecAccessFlags CLdapSecManager::getAccessFlagsEx(SecResourceType rtype, ISecUser & user, const char * resourcename)
  679. {
  680. if(!resourcename || !*resourcename)
  681. return SecAccess_Unavailable;
  682. Owned<ISecResourceList> rlist0;
  683. rlist0.setown(createResourceList("resources"));
  684. rlist0->addResource(resourcename);
  685. CLdapSecResourceList * reslist = (CLdapSecResourceList*)rlist0.get();
  686. if(!reslist)
  687. return SecAccess_Unavailable;
  688. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  689. int nResources = rlist.length();
  690. int ri;
  691. for(ri = 0; ri < nResources; ri++)
  692. {
  693. ISecResource* res = &rlist.item(ri);
  694. if(res != NULL)
  695. res->setResourceType(rtype);
  696. }
  697. if (nResources <= 0)
  698. return SecAccess_Unavailable;
  699. bool ok = false;
  700. time_t tctime = getThreadCreateTime();
  701. if ((m_permissionsCache->isCacheEnabled() || (m_permissionsCache->isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  702. {
  703. bool* cached_found = (bool*)alloca(nResources*sizeof(bool));
  704. int nFound = m_permissionsCache->lookup(user, rlist, cached_found);
  705. if (nFound < nResources)
  706. {
  707. IArrayOf<ISecResource> rlist2;
  708. int i;
  709. for (i=0; i < nResources; i++)
  710. {
  711. if (*(cached_found+i) == false)
  712. {
  713. ISecResource& secRes = rlist.item(i);
  714. secRes.Link();
  715. rlist2.append(secRes);
  716. //DBGLOG("CACHE: Fetching permissions for %s:%s", sec_user.getName(), secRes.getName());
  717. }
  718. }
  719. ok = m_ldap_client->authorize(rtype, user, rlist2);
  720. if (ok)
  721. m_permissionsCache->add(user, rlist2);
  722. }
  723. else
  724. ok = true;
  725. }
  726. else
  727. {
  728. ok = m_ldap_client->authorize(rtype, user, rlist);
  729. }
  730. //bool ok = authorizeEx(rtype, user, rlist.get());
  731. if(ok)
  732. return rlist0->queryResource(0)->getAccessFlags();
  733. else
  734. return SecAccess_Unavailable;
  735. }
  736. bool CLdapSecManager::authorize(ISecUser& sec_user, ISecResourceList * Resources, IEspSecureContext* secureContext)
  737. {
  738. return authorizeEx(RT_DEFAULT, sec_user, Resources, secureContext);
  739. }
  740. SecAccessFlags CLdapSecManager::authorizeFileScope(ISecUser & user, const char * filescope)
  741. {
  742. if(filescope == 0 || filescope[0] == '\0')
  743. return SecAccess_Full;
  744. StringBuffer managedFilescope;
  745. if(m_permissionsCache->isCacheEnabled() && !m_usercache_off)
  746. {
  747. SecAccessFlags accessFlags;
  748. //See if file scope in question is managed by LDAP permissions.
  749. // If not, return default file permission (dont call out to LDAP)
  750. // If is, look in cache for permission of longest matching managed scope strings. If found return that permission (no call to LDAP),
  751. // otherwise a call to LDAP "authorizeFileScope" is necessary, specifying the longest matching managed scope string
  752. bool gotPerms = m_permissionsCache->queryPermsManagedFileScope(user, filescope, managedFilescope, &accessFlags);
  753. if (gotPerms)
  754. return accessFlags;
  755. }
  756. Owned<ISecResourceList> rlist;
  757. rlist.setown(createResourceList("FileScope"));
  758. rlist->addResource(managedFilescope.length() ? managedFilescope.str() : filescope );
  759. bool ok = authorizeFileScope(user, rlist.get());
  760. if(ok)
  761. return rlist->queryResource(0)->getAccessFlags();
  762. else
  763. return SecAccess_Unavailable;
  764. }
  765. bool CLdapSecManager::authorizeFileScope(ISecUser & user, ISecResourceList * resources)
  766. {
  767. return authorizeEx(RT_FILE_SCOPE, user, resources);
  768. }
  769. bool CLdapSecManager::authorizeViewScope(ISecUser & user, StringArray & filenames, StringArray & columnnames)
  770. {
  771. if (filenames.length() != columnnames.length())
  772. {
  773. PROGLOG("Error authorizing view scope: number of filenames (%d) do not match number of columnnames (%d).", filenames.length(), columnnames.length());
  774. return false;
  775. }
  776. const char* username = user.getName();
  777. StringArray viewnames, viewdescriptions, viewManagedBy;
  778. queryAllViews(viewnames, viewdescriptions, viewManagedBy);
  779. // All views where user belongs must pass
  780. ForEachItemIn(i, viewnames)
  781. {
  782. const char* viewname = viewnames.item(i);
  783. if (userInView(username, viewname))
  784. {
  785. Owned<ISecResourceList> resList;
  786. resList.setown(new CLdapSecResourceList(viewname));
  787. // Inefficient loop because we are adding same used columns all over again for each views.
  788. // we can improve the performance later if there is a way to rename and reuse same ISecResourceList for each view.
  789. ForEachItemIn(j, filenames)
  790. {
  791. StringBuffer resourceName;
  792. resourceName.append("QueryAccessedColumns");
  793. resourceName.append(j);
  794. ISecResource* res = resList->addResource(resourceName);
  795. res->addParameter("file", filenames.item(j));
  796. res->addParameter("column", columnnames.item(j));
  797. }
  798. if (!authorizeEx(RT_VIEW_SCOPE, user, resList.get()))
  799. {
  800. PROGLOG("View scope authorization denied by a view %s for a user %s", viewname, username);
  801. return false;
  802. }
  803. }
  804. }
  805. return true;
  806. }
  807. SecAccessFlags CLdapSecManager::authorizeWorkunitScope(ISecUser & user, const char * wuscope)
  808. {
  809. if(wuscope == 0 || wuscope[0] == '\0')
  810. return SecAccess_Full;
  811. Owned<ISecResourceList> rlist;
  812. rlist.setown(createResourceList("WorkunitScope"));
  813. rlist->addResource(wuscope);
  814. bool ok = authorizeWorkunitScope(user, rlist.get());
  815. if(ok)
  816. return rlist->queryResource(0)->getAccessFlags();
  817. else
  818. return SecAccess_Unavailable;
  819. }
  820. bool CLdapSecManager::authorizeWorkunitScope(ISecUser & user, ISecResourceList * resources)
  821. {
  822. return authorizeEx(RT_WORKUNIT_SCOPE, user, resources);
  823. }
  824. bool CLdapSecManager::addResourcesEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * resources, SecPermissionType ptype, const char* basedn)
  825. {
  826. CLdapSecResourceList * reslist = (CLdapSecResourceList*)resources;
  827. if(!reslist)
  828. return true;
  829. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  830. if(rlist.length() <= 0)
  831. return true;
  832. return m_ldap_client->addResources(rtype, sec_user, rlist, ptype, basedn);
  833. }
  834. bool CLdapSecManager::addResourceEx(SecResourceType rtype, ISecUser& user, const char* resourcename, SecPermissionType ptype, const char* basedn)
  835. {
  836. Owned<ISecResourceList> rlist;
  837. rlist.setown(createResourceList("resources"));
  838. rlist->addResource(resourcename);
  839. return addResourcesEx(rtype, user, rlist.get(), ptype, basedn);
  840. }
  841. bool CLdapSecManager::addResources(ISecUser& sec_user, ISecResourceList * resources)
  842. {
  843. return addResourcesEx(RT_DEFAULT, sec_user, resources);
  844. }
  845. bool CLdapSecManager::addUser(ISecUser & user)
  846. {
  847. bool ok = m_ldap_client->addUser(user);
  848. if(!ok)
  849. return false;
  850. return m_pp->retrieveUserInfo(user);
  851. }
  852. ISecUser * CLdapSecManager::lookupUser(unsigned uid)
  853. {
  854. return m_ldap_client->lookupUser(uid);
  855. }
  856. ISecUser * CLdapSecManager::findUser(const char * username)
  857. {
  858. if(username == NULL || strlen(username) == 0)
  859. {
  860. DBGLOG("findUser - username is empty");
  861. return NULL;
  862. }
  863. Owned<ISecUser> user;
  864. user.setown(createUser(username));
  865. try
  866. {
  867. bool ok = m_pp->retrieveUserInfo(*user);
  868. if(ok)
  869. {
  870. return LINK(user.get());
  871. }
  872. else
  873. {
  874. return NULL;
  875. }
  876. }
  877. catch(IException*)
  878. {
  879. return NULL;
  880. }
  881. catch(...)
  882. {
  883. return NULL;
  884. }
  885. }
  886. ISecUserIterator * CLdapSecManager::getAllUsers()
  887. {
  888. synchronized block(m_monitor);
  889. m_user_array.popAll(true);
  890. m_ldap_client->retrieveUsers(m_user_array);
  891. return new ArrayIIteratorOf<IUserArray, ISecUser, ISecUserIterator>(m_user_array);
  892. }
  893. void CLdapSecManager::searchUsers(const char* searchstr, IUserArray& users)
  894. {
  895. m_ldap_client->retrieveUsers(searchstr, users);
  896. }
  897. ISecItemIterator* CLdapSecManager::getUsersSorted(const char* userName, UserField* sortOrder, const unsigned pageStartFrom,
  898. const unsigned pageSize, unsigned* total, __int64* cacheHint)
  899. {
  900. return m_ldap_client->getUsersSorted(userName, sortOrder, pageStartFrom, pageSize, total, cacheHint);
  901. }
  902. void CLdapSecManager::getAllUsers(IUserArray& users)
  903. {
  904. m_ldap_client->retrieveUsers(users);
  905. }
  906. bool CLdapSecManager::getResources(SecResourceType rtype, const char * basedn, IArrayOf<ISecResource> & resources)
  907. {
  908. return m_ldap_client->getResources(rtype, basedn, "", resources);
  909. }
  910. bool CLdapSecManager::getResourcesEx(SecResourceType rtype, const char * basedn, const char* searchstr, IArrayOf<ISecResource> & resources)
  911. {
  912. return m_ldap_client->getResourcesEx(rtype, basedn, "", searchstr, resources);
  913. }
  914. ISecItemIterator* CLdapSecManager::getResourcesSorted(SecResourceType rtype, const char * basedn, const char * resourceName, unsigned extraNameFilter,
  915. ResourceField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize, unsigned *total, __int64 *cachehint)
  916. {
  917. return m_ldap_client->getResourcesSorted(rtype, basedn, resourceName, extraNameFilter, sortOrder, pageStartFrom, pageSize, total, cachehint);
  918. }
  919. void CLdapSecManager::setExtraParam(const char * name, const char * value)
  920. {
  921. if(name == NULL || name[0] == '\0')
  922. {
  923. DBGLOG("CLdapSecManager::setExtraParam name must be provided");
  924. return;
  925. }
  926. if (!m_extraparams)
  927. m_extraparams.setown(createProperties(false));
  928. m_extraparams->setProp(name, value);
  929. if(value != NULL && value[0] != '\0')
  930. {
  931. if(stricmp(name, "resourcesBasedn") == 0)
  932. m_ldap_client->setResourceBasedn(value, RT_DEFAULT);
  933. else if(stricmp(name, "workunitsBasedn") == 0)
  934. m_ldap_client->setResourceBasedn(value, RT_WORKUNIT_SCOPE);
  935. }
  936. }
  937. IAuthMap * CLdapSecManager::createAuthMap(IPropertyTree * authconfig)
  938. {
  939. CAuthMap* authmap = new CAuthMap(this);
  940. IPropertyTreeIterator *loc_iter = NULL;
  941. loc_iter = authconfig->getElements(".//Location");
  942. if (loc_iter != NULL)
  943. {
  944. IPropertyTree *location = NULL;
  945. loc_iter->first();
  946. while(loc_iter->isValid())
  947. {
  948. location = &loc_iter->query();
  949. if (location)
  950. {
  951. StringBuffer pathstr, rstr, required, description;
  952. location->getProp("@path", pathstr);
  953. location->getProp("@resource", rstr);
  954. location->getProp("@required", required);
  955. location->getProp("@description", description);
  956. if(pathstr.length() == 0)
  957. throw MakeStringException(-1, "path empty in Authenticate/Location");
  958. if(rstr.length() == 0)
  959. throw MakeStringException(-1, "resource empty in Authenticate/Location");
  960. ISecResourceList* rlist = authmap->queryResourceList(pathstr.str());
  961. if(rlist == NULL)
  962. {
  963. rlist = createResourceList("ldapsecurity");
  964. authmap->add(pathstr.str(), rlist);
  965. }
  966. ISecResource* rs = rlist->addResource(rstr.str());
  967. SecAccessFlags requiredaccess = str2perm(required.str());
  968. rs->setRequiredAccessFlags(requiredaccess);
  969. rs->setDescription(description.str());
  970. }
  971. loc_iter->next();
  972. }
  973. loc_iter->Release();
  974. loc_iter = NULL;
  975. }
  976. authmap->addToBackend();
  977. return authmap;
  978. }
  979. IAuthMap * CLdapSecManager::createFeatureMap(IPropertyTree * authconfig)
  980. {
  981. CAuthMap* feature_authmap = new CAuthMap(this);
  982. IPropertyTreeIterator *feature_iter = NULL;
  983. feature_iter = authconfig->getElements(".//Feature");
  984. if (feature_iter != NULL)
  985. {
  986. IPropertyTree *feature = NULL;
  987. feature_iter->first();
  988. while(feature_iter->isValid())
  989. {
  990. feature = &feature_iter->query();
  991. if (feature)
  992. {
  993. StringBuffer pathstr, rstr, required, description;
  994. feature->getProp("@path", pathstr);
  995. feature->getProp("@resource", rstr);
  996. feature->getProp("@required", required);
  997. feature->getProp("@description", description);
  998. ISecResourceList* rlist = feature_authmap->queryResourceList(pathstr.str());
  999. if(rlist == NULL)
  1000. {
  1001. rlist = createResourceList(pathstr.str());
  1002. feature_authmap->add(pathstr.str(), rlist);
  1003. }
  1004. ISecResource* rs = rlist->addResource(rstr.str());
  1005. SecAccessFlags requiredaccess = str2perm(required.str());
  1006. rs->setRequiredAccessFlags(requiredaccess);
  1007. rs->setDescription(description.str());
  1008. }
  1009. feature_iter->next();
  1010. }
  1011. feature_iter->Release();
  1012. feature_iter = NULL;
  1013. }
  1014. feature_authmap->addToBackend();
  1015. return feature_authmap;
  1016. }
  1017. bool CLdapSecManager::updateUserPassword(ISecUser& user, const char* newPassword, const char* currPassword)
  1018. {
  1019. // Authenticate User first
  1020. if(!authenticate(&user) && user.getAuthenticateStatus() != AS_PASSWORD_VALID_BUT_EXPIRED)
  1021. {
  1022. return false;
  1023. }
  1024. //Update password if authenticated
  1025. bool ok = m_ldap_client->updateUserPassword(user, newPassword, currPassword);
  1026. if(ok && m_permissionsCache->isCacheEnabled() && !m_usercache_off)
  1027. {
  1028. m_permissionsCache->removeFromUserCache(user);
  1029. }
  1030. return ok;
  1031. }
  1032. bool CLdapSecManager::updateUser(const char* type, ISecUser& user)
  1033. {
  1034. bool ok = m_ldap_client->updateUser(type, user);
  1035. if(ok && m_permissionsCache->isCacheEnabled() && !m_usercache_off)
  1036. m_permissionsCache->removeFromUserCache(user);
  1037. return ok;
  1038. }
  1039. bool CLdapSecManager::updateUserPassword(const char* username, const char* newPassword)
  1040. {
  1041. return m_ldap_client->updateUserPassword(username, newPassword);
  1042. }
  1043. void CLdapSecManager::getAllGroups(StringArray & groups, StringArray & managedBy, StringArray & descriptions)
  1044. {
  1045. m_ldap_client->getAllGroups(groups, managedBy, descriptions);
  1046. }
  1047. ISecItemIterator* CLdapSecManager::getGroupsSorted(GroupField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize,
  1048. unsigned *total, __int64 *cachehint)
  1049. {
  1050. return m_ldap_client->getGroupsSorted(sortOrder, pageStartFrom, pageSize, total, cachehint);
  1051. }
  1052. ISecItemIterator* CLdapSecManager::getGroupMembersSorted(const char* groupName, UserField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize,
  1053. unsigned *total, __int64 *cachehint)
  1054. {
  1055. return m_ldap_client->getGroupMembersSorted(groupName, sortOrder, pageStartFrom, pageSize, total, cachehint);
  1056. }
  1057. bool CLdapSecManager::getPermissionsArray(const char* basedn, SecResourceType rtype, const char* name, IArrayOf<CPermission>& permissions)
  1058. {
  1059. return m_ldap_client->getPermissionsArray(basedn, rtype, name, permissions);
  1060. }
  1061. void CLdapSecManager::addGroup(const char* groupname, const char * groupOwner, const char * groupDesc)
  1062. {
  1063. m_ldap_client->addGroup(groupname, groupOwner, groupDesc);
  1064. }
  1065. void CLdapSecManager::deleteGroup(const char* groupname)
  1066. {
  1067. m_ldap_client->deleteGroup(groupname);
  1068. }
  1069. bool CLdapSecManager::changePermission(CPermissionAction& action)
  1070. {
  1071. return m_ldap_client->changePermission(action);
  1072. }
  1073. void CLdapSecManager::getGroups(const char* username, StringArray & groups)
  1074. {
  1075. m_ldap_client->getGroups(username, groups);
  1076. }
  1077. void CLdapSecManager::changeUserGroup(const char* action, const char* username, const char* groupname)
  1078. {
  1079. m_ldap_client->changeUserGroup(action, username, groupname);
  1080. }
  1081. bool CLdapSecManager::deleteUser(ISecUser* user)
  1082. {
  1083. return m_ldap_client->deleteUser(user);
  1084. }
  1085. void CLdapSecManager::getGroupMembers(const char* groupname, StringArray & users)
  1086. {
  1087. m_ldap_client->getGroupMembers(groupname, users);
  1088. }
  1089. void CLdapSecManager::deleteResource(SecResourceType rtype, const char * name, const char * basedn)
  1090. {
  1091. m_ldap_client->deleteResource(rtype, name, basedn);
  1092. time_t tctime = getThreadCreateTime();
  1093. if ((m_permissionsCache->isCacheEnabled() || (m_permissionsCache->isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  1094. m_permissionsCache->remove(rtype, name);
  1095. }
  1096. void CLdapSecManager::renameResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn)
  1097. {
  1098. m_ldap_client->renameResource(rtype, oldname, newname, basedn);
  1099. time_t tctime = getThreadCreateTime();
  1100. if ((m_permissionsCache->isCacheEnabled() || (m_permissionsCache->isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  1101. m_permissionsCache->remove(rtype, oldname);
  1102. }
  1103. void CLdapSecManager::copyResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn)
  1104. {
  1105. m_ldap_client->copyResource(rtype, oldname, newname, basedn);
  1106. }
  1107. void CLdapSecManager::normalizeDn(const char* dn, StringBuffer& ndn)
  1108. {
  1109. m_ldap_client->normalizeDn(dn, ndn);
  1110. }
  1111. bool CLdapSecManager::isSuperUser(ISecUser* user)
  1112. {
  1113. return m_ldap_client->isSuperUser(user);
  1114. }
  1115. ILdapConfig* CLdapSecManager::queryConfig()
  1116. {
  1117. return m_ldap_client->queryConfig();
  1118. }
  1119. void CLdapSecManager::cacheSwitch(SecResourceType rtype, bool on)
  1120. {
  1121. m_cache_off[rtype] = !on;
  1122. // To make things simple, turning off any resource type's permission cache turns off the userCache.
  1123. if(!on)
  1124. m_usercache_off = true;
  1125. }
  1126. int CLdapSecManager::countUsers(const char* searchstr, int limit)
  1127. {
  1128. return m_ldap_client->countUsers(searchstr, limit);
  1129. }
  1130. int CLdapSecManager::countResources(const char* basedn, const char* searchstr, int limit)
  1131. {
  1132. return m_ldap_client->countResources(basedn, searchstr, limit);
  1133. }
  1134. bool CLdapSecManager::getUserInfo(ISecUser& user, const char* infotype)
  1135. {
  1136. return m_ldap_client->getUserInfo(user, infotype);
  1137. }
  1138. bool CLdapSecManager::createUserScopes()
  1139. {
  1140. Owned<ISecUserIterator> it = getAllUsers();
  1141. it->first();
  1142. bool rc = true;
  1143. while(it->isValid())
  1144. {
  1145. ISecUser &user = it->get();
  1146. if (!m_ldap_client->createUserScope(user))
  1147. {
  1148. PROGLOG("CLdapSecManager::createUserScopes Error creating user scope for user '%s'", user.getName());
  1149. rc = false;
  1150. }
  1151. it->next();
  1152. }
  1153. return rc;
  1154. }
  1155. aindex_t CLdapSecManager::getManagedFileScopes(IArrayOf<ISecResource>& scopes)
  1156. {
  1157. return m_ldap_client->getManagedFileScopes(scopes);
  1158. }
  1159. SecAccessFlags CLdapSecManager::queryDefaultPermission(ISecUser& user)
  1160. {
  1161. return m_ldap_client->queryDefaultPermission(user);
  1162. }
  1163. bool CLdapSecManager::clearPermissionsCache(ISecUser& user)
  1164. {
  1165. if(m_permissionsCache->isCacheEnabled())
  1166. {
  1167. if (!authenticate(&user))
  1168. {
  1169. PROGLOG("User %s not authorized to clear permissions cache", user.getName());
  1170. return false;
  1171. }
  1172. if (!isSuperUser(&user))
  1173. {
  1174. PROGLOG("User %s denied, only a superuser can clear permissions cache", user.getName());
  1175. return false;
  1176. }
  1177. m_permissionsCache->flush();
  1178. }
  1179. return true;
  1180. }
  1181. bool CLdapSecManager::authenticateUser(ISecUser & user, bool *superUser)
  1182. {
  1183. if (!authenticate(&user))
  1184. return false;
  1185. if (superUser)
  1186. *superUser = isSuperUser(&user);
  1187. return true;
  1188. }
  1189. //Data View related interfaces
  1190. void CLdapSecManager::createView(const char* viewName, const char * viewDescription)
  1191. {
  1192. m_ldap_client->createView(viewName, viewDescription);
  1193. }
  1194. void CLdapSecManager::deleteView(const char* viewName)
  1195. {
  1196. m_ldap_client->deleteView(viewName);
  1197. }
  1198. void CLdapSecManager::queryAllViews(StringArray & viewNames, StringArray & viewDescriptions, StringArray & viewManagedBy)
  1199. {
  1200. m_ldap_client->queryAllViews(viewNames, viewDescriptions, viewManagedBy);
  1201. }
  1202. void CLdapSecManager::addViewColumns(const char* viewName, StringArray & files, StringArray & columns)
  1203. {
  1204. m_ldap_client->addViewColumns(viewName, files, columns);
  1205. }
  1206. void CLdapSecManager::removeViewColumns(const char* viewName, StringArray & files, StringArray & columns)
  1207. {
  1208. m_ldap_client->removeViewColumns(viewName, files, columns);
  1209. }
  1210. void CLdapSecManager::queryViewColumns(const char* viewName, StringArray & files, StringArray & columns)
  1211. {
  1212. m_ldap_client->queryViewColumns(viewName, files, columns);
  1213. }
  1214. void CLdapSecManager::addViewMembers(const char* viewName, StringArray & viewUsers, StringArray & viewGroups)
  1215. {
  1216. m_ldap_client->addViewMembers(viewName, viewUsers, viewGroups);
  1217. }
  1218. void CLdapSecManager::removeViewMembers(const char* viewName, StringArray & viewUsers, StringArray & viewGroups)
  1219. {
  1220. m_ldap_client->removeViewMembers(viewName, viewUsers, viewGroups);
  1221. }
  1222. void CLdapSecManager::queryViewMembers(const char* viewName, StringArray & viewUsers, StringArray & viewGroups)
  1223. {
  1224. m_ldap_client->queryViewMembers(viewName, viewUsers, viewGroups);
  1225. }
  1226. bool CLdapSecManager::userInView(const char * user, const char* viewName)
  1227. {
  1228. return m_ldap_client->userInView(user, viewName);
  1229. }
  1230. extern "C"
  1231. {
  1232. LDAPSECURITY_API ISecManager * newLdapSecManager(const char *serviceName, IPropertyTree &config)
  1233. {
  1234. return new CLdapSecManager(serviceName, config);
  1235. }
  1236. LDAPSECURITY_API IAuthMap *newDefaultAuthMap(IPropertyTree* config)
  1237. {
  1238. CAuthMap* authmap = new CAuthMap(NULL);
  1239. IPropertyTreeIterator *loc_iter = NULL;
  1240. loc_iter = config->getElements(".//Location");
  1241. if (loc_iter != NULL)
  1242. {
  1243. IPropertyTree *location = NULL;
  1244. loc_iter->first();
  1245. while(loc_iter->isValid())
  1246. {
  1247. location = &loc_iter->query();
  1248. if (location)
  1249. {
  1250. StringBuffer pathstr, rstr;
  1251. location->getProp("@path", pathstr);
  1252. authmap->add(pathstr.str(), NULL);
  1253. }
  1254. loc_iter->next();
  1255. }
  1256. loc_iter->Release();
  1257. loc_iter = NULL;
  1258. }
  1259. return authmap;
  1260. }
  1261. }