ldapsecurity.cpp 46 KB

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