ldapsecurity.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifdef _WIN32
  15. #define AXA_API __declspec(dllexport)
  16. #endif
  17. #include "ldapsecurity.ipp"
  18. #include "ldapsecurity.hpp"
  19. #include "authmap.ipp"
  20. #include "defaultsecuritymanager.hpp"
  21. /**********************************************************
  22. * CLdapSecUser *
  23. **********************************************************/
  24. CLdapSecUser::CLdapSecUser(const char *name, const char *pw) :
  25. m_pw(pw), m_isAuthenticated(false)
  26. {
  27. setName(name);
  28. }
  29. CLdapSecUser::~CLdapSecUser()
  30. {
  31. }
  32. //non-interfaced functions
  33. void CLdapSecUser::setAuthenticated(bool authenticated)
  34. {
  35. m_isAuthenticated = authenticated;
  36. }
  37. void CLdapSecUser::setUserID(unsigned userid)
  38. {
  39. m_userid = userid;
  40. }
  41. void CLdapSecUser::setUserSid(int sidlen, const char* sid)
  42. {
  43. m_usersid.clear();
  44. m_usersid.append(sidlen, sid);
  45. }
  46. MemoryBuffer& CLdapSecUser::getUserSid()
  47. {
  48. return m_usersid;
  49. }
  50. //interface ISecUser
  51. const char * CLdapSecUser::getName()
  52. {
  53. return m_name.get();
  54. }
  55. bool CLdapSecUser::setName(const char * name)
  56. {
  57. if(name != NULL)
  58. {
  59. const char* atsign = strchr(name, '@');
  60. if(atsign != NULL)
  61. {
  62. m_name.set(name, atsign - name);
  63. m_realm.set(atsign + 1);
  64. }
  65. else
  66. {
  67. m_name.set(name);
  68. }
  69. }
  70. return TRUE;
  71. }
  72. const char * CLdapSecUser::getFullName()
  73. {
  74. return m_fullname.get();
  75. }
  76. bool CLdapSecUser::setFullName(const char * name)
  77. {
  78. if(name != NULL)
  79. {
  80. m_fullname.set(name);
  81. }
  82. return true;
  83. }
  84. const char * CLdapSecUser::getFirstName()
  85. {
  86. return m_firstname.get();
  87. }
  88. bool CLdapSecUser::setFirstName(const char * fname)
  89. {
  90. if(fname != NULL)
  91. {
  92. m_firstname.set(fname);
  93. }
  94. return true;
  95. }
  96. const char * CLdapSecUser::getLastName()
  97. {
  98. return m_lastname.get();
  99. }
  100. bool CLdapSecUser::setLastName(const char * lname)
  101. {
  102. if(lname != NULL)
  103. {
  104. m_lastname.set(lname);
  105. }
  106. return true;
  107. }
  108. const char * CLdapSecUser::getRealm()
  109. {
  110. return m_realm.get();
  111. }
  112. bool CLdapSecUser::setRealm(const char * name)
  113. {
  114. m_realm.set(name);
  115. return TRUE;
  116. }
  117. const char * CLdapSecUser::getFqdn()
  118. {
  119. return m_Fqdn.get();
  120. }
  121. bool CLdapSecUser::setFqdn(const char * Fqdn)
  122. {
  123. m_Fqdn.set(Fqdn);
  124. return true;
  125. }
  126. const char *CLdapSecUser::getPeer()
  127. {
  128. return m_Peer.get();
  129. }
  130. bool CLdapSecUser::setPeer(const char *Peer)
  131. {
  132. m_Peer.set(Peer);
  133. return true;
  134. }
  135. bool CLdapSecUser::isAuthenticated()
  136. {
  137. return m_isAuthenticated;
  138. }
  139. ISecCredentials & CLdapSecUser::credentials()
  140. {
  141. return *this;
  142. }
  143. unsigned CLdapSecUser::getUserID()
  144. {
  145. return m_userid;
  146. }
  147. //interface ISecCredentials
  148. bool CLdapSecUser::setPassword(const char * pw)
  149. {
  150. m_pw.set(pw);
  151. return TRUE;
  152. }
  153. const char* CLdapSecUser::getPassword()
  154. {
  155. return m_pw;
  156. }
  157. bool CLdapSecUser::setEncodedPassword(SecPasswordEncoding enc, void * pw, unsigned length, void * salt, unsigned saltlen)
  158. {
  159. return FALSE; //not supported yet
  160. }
  161. bool CLdapSecUser::addToken(unsigned type, void * data, unsigned length)
  162. {
  163. return FALSE; //not supported yet
  164. }
  165. void CLdapSecUser::copyTo(ISecUser& destination)
  166. {
  167. CLdapSecUser* dest = dynamic_cast<CLdapSecUser*>(&destination);
  168. if(!dest)
  169. return;
  170. dest->setAuthenticated(isAuthenticated());
  171. dest->setName(getName());
  172. dest->setFullName(getFullName());
  173. dest->setFirstName(getFirstName());
  174. dest->setLastName(getLastName());
  175. dest->setRealm(getRealm());
  176. dest->credentials().setPassword(credentials().getPassword());
  177. dest->setUserSid(m_usersid.length(), m_usersid.toByteArray());
  178. dest->setUserID(m_userid);
  179. dest->setPasswordExpiration(m_passwordExpiration);
  180. }
  181. ISecUser * CLdapSecUser::clone()
  182. {
  183. CLdapSecUser* newuser = new CLdapSecUser(m_name.get(), m_pw.get());
  184. if(newuser)
  185. copyTo(*newuser);
  186. return newuser;
  187. }
  188. /**********************************************************
  189. * CLdapSecResource *
  190. **********************************************************/
  191. CLdapSecResource::CLdapSecResource(const char *name) : m_name(name), m_access(0), m_required_access(0)
  192. {
  193. m_resourcetype = RT_DEFAULT;
  194. }
  195. void CLdapSecResource::addAccess(int flags)
  196. {
  197. m_access |= flags;
  198. }
  199. void CLdapSecResource::setAccessFlags(int flags)
  200. {
  201. m_access = flags;
  202. }
  203. void CLdapSecResource::setRequiredAccessFlags(int flags)
  204. {
  205. m_required_access = flags;
  206. }
  207. int CLdapSecResource::getRequiredAccessFlags()
  208. {
  209. return m_required_access;
  210. }
  211. //interface ISecResource : extends IInterface
  212. const char * CLdapSecResource::getName()
  213. {
  214. return m_name.get();
  215. }
  216. int CLdapSecResource::getAccessFlags()
  217. {
  218. return m_access;
  219. }
  220. int CLdapSecResource::addParameter(const char* name, const char* value)
  221. {
  222. if (!m_parameters)
  223. m_parameters.setown(createProperties(false));
  224. m_parameters->setProp(name, value);
  225. return 0;
  226. }
  227. const char * CLdapSecResource::getParameter(const char * name)
  228. {
  229. if (m_parameters)
  230. {
  231. const char *value = m_parameters->queryProp(name);
  232. return value;
  233. }
  234. return NULL;
  235. }
  236. void CLdapSecResource::setDescription(const char* description)
  237. {
  238. m_description.clear().append(description);
  239. }
  240. const char* CLdapSecResource::getDescription()
  241. {
  242. return m_description.str();
  243. }
  244. void CLdapSecResource::setValue(const char* value)
  245. {
  246. m_value.clear();
  247. m_value.append(value);
  248. }
  249. const char* CLdapSecResource::getValue()
  250. {
  251. return m_value.str();
  252. }
  253. ISecResource * CLdapSecResource::clone()
  254. {
  255. CLdapSecResource* _res = new CLdapSecResource(m_name.get());
  256. if(!_res)
  257. return NULL;
  258. _res->setResourceType(m_resourcetype);
  259. _res->setValue(m_value.str());
  260. _res->m_access = m_access;
  261. _res->m_required_access = m_required_access;
  262. _res->setDescription(m_description.str());
  263. if(!m_parameters)
  264. return _res;
  265. Owned<IPropertyIterator> Itr = m_parameters->getIterator();
  266. Itr->first();
  267. while(Itr->isValid())
  268. {
  269. _res->addParameter(Itr->getPropKey(),m_parameters->queryProp(Itr->getPropKey()));
  270. Itr->next();
  271. }
  272. return _res;
  273. }
  274. void CLdapSecResource::copy(ISecResource* from)
  275. {
  276. if(!from)
  277. return;
  278. CLdapSecResource* ldapfrom = dynamic_cast<CLdapSecResource*>(from);
  279. if(!ldapfrom)
  280. return;
  281. m_access = ldapfrom->m_access;
  282. setDescription(ldapfrom->m_description.str());
  283. if(m_parameters.get())
  284. {
  285. m_parameters.clear();
  286. }
  287. if(!ldapfrom->m_parameters.get())
  288. return;
  289. Owned<IPropertyIterator> Itr = ldapfrom->m_parameters->getIterator();
  290. Itr->first();
  291. while(Itr->isValid())
  292. {
  293. addParameter(Itr->getPropKey(), ldapfrom->m_parameters->queryProp(Itr->getPropKey()));
  294. Itr->next();
  295. }
  296. return;
  297. }
  298. SecResourceType CLdapSecResource::getResourceType()
  299. {
  300. return m_resourcetype;
  301. }
  302. void CLdapSecResource::setResourceType(SecResourceType resourcetype)
  303. {
  304. m_resourcetype = resourcetype;
  305. }
  306. /**********************************************************
  307. * CLdapSecResourceList *
  308. **********************************************************/
  309. CLdapSecResourceList::CLdapSecResourceList(const char *name) : m_complete(0)
  310. {
  311. m_name.set(name);
  312. }
  313. void CLdapSecResourceList::setAuthorizationComplete(bool value)
  314. {
  315. m_complete=value;
  316. }
  317. IArrayOf<ISecResource>& CLdapSecResourceList::getResourceList()
  318. {
  319. return m_rlist;
  320. }
  321. //interface ISecResourceList : extends IInterface
  322. bool CLdapSecResourceList::isAuthorizationComplete()
  323. {
  324. return m_complete;
  325. }
  326. ISecResourceList * CLdapSecResourceList::clone()
  327. {
  328. CLdapSecResourceList* _newList = new CLdapSecResourceList(m_name.get());
  329. if(!_newList)
  330. return NULL;
  331. copyTo(*_newList);
  332. return _newList;
  333. }
  334. bool CLdapSecResourceList::copyTo(ISecResourceList& destination)
  335. {
  336. ForEachItemIn(x, m_rlist)
  337. {
  338. CLdapSecResource* res = (CLdapSecResource*)(&(m_rlist.item(x)));
  339. if(res)
  340. destination.addResource(res->clone());
  341. }
  342. return false;
  343. }
  344. ISecResource* CLdapSecResourceList::addResource(const char * name)
  345. {
  346. if(!name || !*name)
  347. return NULL;
  348. ISecResource* resource = m_rmap[name];
  349. if(resource == NULL)
  350. {
  351. resource = new CLdapSecResource(name);
  352. m_rlist.append(*resource);
  353. m_rmap[name] = resource;
  354. }
  355. return resource;
  356. }
  357. void CLdapSecResourceList::addResource(ISecResource * resource)
  358. {
  359. if(resource == NULL)
  360. return;
  361. const char* name = resource->getName();
  362. if(!name || !*name)
  363. return;
  364. ISecResource* r = m_rmap[name];
  365. if(r == NULL)
  366. {
  367. m_rlist.append(*resource);
  368. m_rmap[name] = resource;
  369. }
  370. }
  371. bool CLdapSecResourceList::addCustomResource(const char * name, const char * config)
  372. {
  373. return false;
  374. }
  375. ISecResource * CLdapSecResourceList::getResource(const char * Resource)
  376. {
  377. if(!Resource || !*Resource)
  378. return NULL;
  379. ISecResource* r = m_rmap[Resource];
  380. if(r)
  381. return LINK(r);
  382. else
  383. return NULL;
  384. }
  385. void CLdapSecResourceList::clear()
  386. {
  387. m_rlist.kill();
  388. }
  389. int CLdapSecResourceList::count()
  390. {
  391. return m_rlist.length();
  392. }
  393. const char* CLdapSecResourceList::getName()
  394. {
  395. return m_name.get();
  396. }
  397. ISecResource * CLdapSecResourceList::queryResource(unsigned seq)
  398. {
  399. if(seq < m_rlist.length())
  400. return &(m_rlist.item(seq));
  401. else
  402. return NULL;
  403. }
  404. ISecPropertyIterator * CLdapSecResourceList::getPropertyItr()
  405. {
  406. return new ArrayIIteratorOf<IArrayOf<struct ISecResource>, ISecProperty, ISecPropertyIterator>(m_rlist);
  407. }
  408. ISecProperty* CLdapSecResourceList::findProperty(const char* name)
  409. {
  410. if(!name || !*name)
  411. return NULL;
  412. return m_rmap[name];
  413. }
  414. /**********************************************************
  415. * CLdapSecManager *
  416. **********************************************************/
  417. CLdapSecManager::CLdapSecManager(const char *serviceName, const char *config)
  418. {
  419. IPropertyTree* cfg = createPTreeFromXMLString(config, ipt_caseInsensitive);
  420. if(cfg == NULL)
  421. {
  422. throw MakeStringException(-1, "createPTreeFromXMLString() failed for %s", config);
  423. }
  424. init(serviceName, cfg);
  425. }
  426. void CLdapSecManager::init(const char *serviceName, IPropertyTree* cfg)
  427. {
  428. for(int i = 0; i < RT_SCOPE_MAX; i++)
  429. m_cache_off[i] = false;
  430. m_usercache_off = false;
  431. m_cfg.setown(cfg);
  432. cfg->getProp(".//@ldapAddress", m_server);
  433. cfg->getProp(".//@description", m_description);
  434. ILdapClient* ldap_client = createLdapClient(cfg);
  435. IPermissionProcessor* pp;
  436. if(ldap_client->getServerType() == ACTIVE_DIRECTORY)
  437. pp = new PermissionProcessor(cfg);
  438. else if(ldap_client->getServerType() == IPLANET)
  439. pp = new CIPlanetAciProcessor(cfg);
  440. else if(ldap_client->getServerType() == OPEN_LDAP)
  441. pp = new COpenLdapAciProcessor(cfg);
  442. ldap_client->init(pp);
  443. pp->setLdapClient(ldap_client);
  444. m_ldap_client.setown(ldap_client);
  445. m_pp.setown(pp);
  446. int cachetimeout = cfg->getPropInt("@cacheTimeout", 5);
  447. m_permissionsCache.setCacheTimeout( 60 * cachetimeout);
  448. m_permissionsCache.setTransactionalEnabled(true);
  449. m_passwordExpirationWarningDays = cfg->getPropInt(".//@passwordExpirationWarningDays", 10); //Default to 10 days
  450. };
  451. CLdapSecManager::CLdapSecManager(const char *serviceName, IPropertyTree &config)
  452. {
  453. init(serviceName, &config);
  454. }
  455. CLdapSecManager::~CLdapSecManager()
  456. {
  457. }
  458. //interface ISecManager : extends IInterface
  459. ISecUser * CLdapSecManager::createUser(const char * user_name)
  460. {
  461. return (new CLdapSecUser(user_name, NULL));
  462. }
  463. ISecResourceList * CLdapSecManager::createResourceList(const char * rlname)
  464. {
  465. return (new CLdapSecResourceList(rlname));
  466. }
  467. bool CLdapSecManager::subscribe(ISecAuthenticEvents & events)
  468. {
  469. m_subscriber.set(&events);
  470. return true;
  471. }
  472. bool CLdapSecManager::unsubscribe(ISecAuthenticEvents & events)
  473. {
  474. if (&events == m_subscriber.get())
  475. {
  476. m_subscriber.set(NULL);
  477. }
  478. return true;
  479. }
  480. bool CLdapSecManager::authenticate(ISecUser* user)
  481. {
  482. if(!user)
  483. return false;
  484. if(user->isAuthenticated())
  485. return true;
  486. if(m_permissionsCache.isCacheEnabled() && !m_usercache_off && m_permissionsCache.lookup(*user))
  487. {
  488. user->setAuthenticated(true);
  489. return true;
  490. }
  491. bool ok = m_ldap_client->authenticate(*user);
  492. if(ok)
  493. {
  494. if(m_permissionsCache.isCacheEnabled() && !m_usercache_off)
  495. m_permissionsCache.add(*user);
  496. user->setAuthenticated(true);
  497. }
  498. return ok;
  499. }
  500. bool CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * Resources)
  501. {
  502. if(!authenticate(&sec_user))
  503. {
  504. return false;
  505. }
  506. CLdapSecResourceList * reslist = (CLdapSecResourceList*)Resources;
  507. if(!reslist)
  508. return true;
  509. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  510. int nResources = rlist.length();
  511. int ri;
  512. for(ri = 0; ri < nResources; ri++)
  513. {
  514. ISecResource* res = &rlist.item(ri);
  515. if(res != NULL)
  516. res->setResourceType(rtype);
  517. }
  518. if (nResources <= 0)
  519. return true;
  520. bool rc;
  521. time_t tctime = getThreadCreateTime();
  522. if ((m_permissionsCache.isCacheEnabled() || (m_permissionsCache.isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  523. {
  524. bool* cached_found = (bool*)alloca(nResources*sizeof(bool));
  525. int nFound = m_permissionsCache.lookup(sec_user, rlist, cached_found);
  526. if (nFound < nResources)
  527. {
  528. IArrayOf<ISecResource> rlist2;
  529. int i;
  530. for (i=0; i < nResources; i++)
  531. {
  532. if (*(cached_found+i) == false)
  533. {
  534. ISecResource& secRes = rlist.item(i);
  535. secRes.Link();
  536. rlist2.append(secRes);
  537. //DBGLOG("CACHE: Fetching permissions for %s:%s", sec_user.getName(), secRes.getName());
  538. }
  539. }
  540. rc = m_ldap_client->authorize(rtype, sec_user, rlist2);
  541. if (rc)
  542. m_permissionsCache.add(sec_user, rlist2);
  543. }
  544. else
  545. rc = true;
  546. }
  547. else
  548. {
  549. rc = m_ldap_client->authorize(rtype, sec_user, rlist);
  550. }
  551. return rc;
  552. }
  553. int CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser & user, const char * resourcename)
  554. {
  555. if(!resourcename || !*resourcename)
  556. return SecAccess_Full;
  557. Owned<ISecResourceList> rlist;
  558. rlist.setown(createResourceList("resources"));
  559. rlist->addResource(resourcename);
  560. bool ok = authorizeEx(rtype, user, rlist.get());
  561. if(ok)
  562. return rlist->queryResource(0)->getAccessFlags();
  563. else
  564. return -1;
  565. }
  566. bool CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * Resources, bool doAuthentication)
  567. {
  568. if(doAuthentication && !authenticate(&sec_user))
  569. {
  570. return false;
  571. }
  572. CLdapSecResourceList * reslist = (CLdapSecResourceList*)Resources;
  573. if(!reslist)
  574. return true;
  575. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  576. int nResources = rlist.length();
  577. int ri;
  578. for(ri = 0; ri < nResources; ri++)
  579. {
  580. ISecResource* res = &rlist.item(ri);
  581. if(res != NULL)
  582. res->setResourceType(rtype);
  583. }
  584. if (nResources <= 0)
  585. return true;
  586. bool rc;
  587. time_t tctime = getThreadCreateTime();
  588. if ((m_permissionsCache.isCacheEnabled() || (m_permissionsCache.isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  589. {
  590. bool* cached_found = (bool*)alloca(nResources*sizeof(bool));
  591. int nFound = m_permissionsCache.lookup(sec_user, rlist, cached_found);
  592. if (nFound < nResources)
  593. {
  594. IArrayOf<ISecResource> rlist2;
  595. int i;
  596. for (i=0; i < nResources; i++)
  597. {
  598. if (*(cached_found+i) == false)
  599. {
  600. ISecResource& secRes = rlist.item(i);
  601. secRes.Link();
  602. rlist2.append(secRes);
  603. //DBGLOG("CACHE: Fetching permissions for %s:%s", sec_user.getName(), secRes.getName());
  604. }
  605. }
  606. rc = m_ldap_client->authorize(rtype, sec_user, rlist2);
  607. if (rc)
  608. m_permissionsCache.add(sec_user, rlist2);
  609. }
  610. else
  611. rc = true;
  612. }
  613. else
  614. {
  615. rc = m_ldap_client->authorize(rtype, sec_user, rlist);
  616. }
  617. return rc;
  618. }
  619. int CLdapSecManager::authorizeEx(SecResourceType rtype, ISecUser & user, const char * resourcename, bool doAuthentication)
  620. {
  621. if(!resourcename || !*resourcename)
  622. return SecAccess_Full;
  623. Owned<ISecResourceList> rlist;
  624. rlist.setown(createResourceList("resources"));
  625. rlist->addResource(resourcename);
  626. bool ok = authorizeEx(rtype, user, rlist.get(), doAuthentication);
  627. if(ok)
  628. return rlist->queryResource(0)->getAccessFlags();
  629. else
  630. return -1;
  631. }
  632. int CLdapSecManager::getAccessFlagsEx(SecResourceType rtype, ISecUser & user, const char * resourcename)
  633. {
  634. if(!resourcename || !*resourcename)
  635. return -1;
  636. Owned<ISecResourceList> rlist0;
  637. rlist0.setown(createResourceList("resources"));
  638. rlist0->addResource(resourcename);
  639. CLdapSecResourceList * reslist = (CLdapSecResourceList*)rlist0.get();
  640. if(!reslist)
  641. return -1;
  642. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  643. int nResources = rlist.length();
  644. int ri;
  645. for(ri = 0; ri < nResources; ri++)
  646. {
  647. ISecResource* res = &rlist.item(ri);
  648. if(res != NULL)
  649. res->setResourceType(rtype);
  650. }
  651. if (nResources <= 0)
  652. return -1;
  653. bool ok = false;
  654. time_t tctime = getThreadCreateTime();
  655. if ((m_permissionsCache.isCacheEnabled() || (m_permissionsCache.isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  656. {
  657. bool* cached_found = (bool*)alloca(nResources*sizeof(bool));
  658. int nFound = m_permissionsCache.lookup(user, rlist, cached_found);
  659. if (nFound < nResources)
  660. {
  661. IArrayOf<ISecResource> rlist2;
  662. int i;
  663. for (i=0; i < nResources; i++)
  664. {
  665. if (*(cached_found+i) == false)
  666. {
  667. ISecResource& secRes = rlist.item(i);
  668. secRes.Link();
  669. rlist2.append(secRes);
  670. //DBGLOG("CACHE: Fetching permissions for %s:%s", sec_user.getName(), secRes.getName());
  671. }
  672. }
  673. ok = m_ldap_client->authorize(rtype, user, rlist2);
  674. if (ok)
  675. m_permissionsCache.add(user, rlist2);
  676. }
  677. else
  678. ok = true;
  679. }
  680. else
  681. {
  682. ok = m_ldap_client->authorize(rtype, user, rlist);
  683. }
  684. //bool ok = authorizeEx(rtype, user, rlist.get());
  685. if(ok)
  686. return rlist0->queryResource(0)->getAccessFlags();
  687. else
  688. return -1;
  689. }
  690. bool CLdapSecManager::authorize(ISecUser& sec_user, ISecResourceList * Resources)
  691. {
  692. return authorizeEx(RT_DEFAULT, sec_user, Resources);
  693. }
  694. int CLdapSecManager::authorizeFileScope(ISecUser & user, const char * filescope)
  695. {
  696. if(filescope == 0 || filescope[0] == '\0')
  697. return SecAccess_Full;
  698. Owned<ISecResourceList> rlist;
  699. rlist.setown(createResourceList("FileScope"));
  700. rlist->addResource(filescope);
  701. bool ok = authorizeFileScope(user, rlist.get());
  702. if(ok)
  703. return rlist->queryResource(0)->getAccessFlags();
  704. else
  705. return -1;
  706. }
  707. bool CLdapSecManager::authorizeFileScope(ISecUser & user, ISecResourceList * resources)
  708. {
  709. return authorizeEx(RT_FILE_SCOPE, user, resources);
  710. }
  711. int CLdapSecManager::authorizeWorkunitScope(ISecUser & user, const char * wuscope)
  712. {
  713. if(wuscope == 0 || wuscope[0] == '\0')
  714. return SecAccess_Full;
  715. Owned<ISecResourceList> rlist;
  716. rlist.setown(createResourceList("WorkunitScope"));
  717. rlist->addResource(wuscope);
  718. bool ok = authorizeWorkunitScope(user, rlist.get());
  719. if(ok)
  720. return rlist->queryResource(0)->getAccessFlags();
  721. else
  722. return -1;
  723. }
  724. bool CLdapSecManager::authorizeWorkunitScope(ISecUser & user, ISecResourceList * resources)
  725. {
  726. return authorizeEx(RT_WORKUNIT_SCOPE, user, resources);
  727. }
  728. bool CLdapSecManager::addResourcesEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * resources, SecPermissionType ptype, const char* basedn)
  729. {
  730. CLdapSecResourceList * reslist = (CLdapSecResourceList*)resources;
  731. if(!reslist)
  732. return true;
  733. IArrayOf<ISecResource>& rlist = reslist->getResourceList();
  734. if(rlist.length() <= 0)
  735. return true;
  736. return m_ldap_client->addResources(rtype, sec_user, rlist, ptype, basedn);
  737. }
  738. bool CLdapSecManager::addResourceEx(SecResourceType rtype, ISecUser& user, const char* resourcename, SecPermissionType ptype, const char* basedn)
  739. {
  740. Owned<ISecResourceList> rlist;
  741. rlist.setown(createResourceList("resources"));
  742. rlist->addResource(resourcename);
  743. return addResourcesEx(rtype, user, rlist.get(), ptype, basedn);
  744. }
  745. bool CLdapSecManager::addResources(ISecUser& sec_user, ISecResourceList * resources)
  746. {
  747. return addResourcesEx(RT_DEFAULT, sec_user, resources);
  748. }
  749. bool CLdapSecManager::addUser(ISecUser & user)
  750. {
  751. if(&user == NULL)
  752. {
  753. DBGLOG("CLdapSecManager::addUser - user is NULL");
  754. return false;
  755. }
  756. bool ok = m_ldap_client->addUser(user);
  757. if(!ok)
  758. return false;
  759. return m_pp->retrieveUserInfo(user);
  760. }
  761. ISecUser * CLdapSecManager::lookupUser(unsigned uid)
  762. {
  763. return m_ldap_client->lookupUser(uid);
  764. }
  765. ISecUser * CLdapSecManager::findUser(const char * username)
  766. {
  767. if(username == NULL || strlen(username) == 0)
  768. {
  769. DBGLOG("findUser - username is empty");
  770. return NULL;
  771. }
  772. Owned<ISecUser> user;
  773. user.setown(createUser(username));
  774. try
  775. {
  776. bool ok = m_pp->retrieveUserInfo(*user);
  777. if(ok)
  778. {
  779. return LINK(user.get());
  780. }
  781. else
  782. {
  783. return NULL;
  784. }
  785. }
  786. catch(IException*)
  787. {
  788. return NULL;
  789. }
  790. catch(...)
  791. {
  792. return NULL;
  793. }
  794. }
  795. ISecUserIterator * CLdapSecManager::getAllUsers()
  796. {
  797. synchronized block(m_monitor);
  798. m_user_array.popAll(true);
  799. m_ldap_client->retrieveUsers(m_user_array);
  800. return new ArrayIIteratorOf<IUserArray, ISecUser, ISecUserIterator>(m_user_array);
  801. }
  802. void CLdapSecManager::searchUsers(const char* searchstr, IUserArray& users)
  803. {
  804. m_ldap_client->retrieveUsers(searchstr, users);
  805. }
  806. void CLdapSecManager::getAllUsers(IUserArray& users)
  807. {
  808. m_ldap_client->retrieveUsers(users);
  809. }
  810. bool CLdapSecManager::getResources(SecResourceType rtype, const char * basedn, IArrayOf<ISecResource> & resources)
  811. {
  812. return m_ldap_client->getResources(rtype, basedn, "", resources);
  813. }
  814. bool CLdapSecManager::getResourcesEx(SecResourceType rtype, const char * basedn, const char* searchstr, IArrayOf<ISecResource> & resources)
  815. {
  816. return m_ldap_client->getResourcesEx(rtype, basedn, "", searchstr, resources);
  817. }
  818. void CLdapSecManager::setExtraParam(const char * name, const char * value)
  819. {
  820. if(name == NULL || name[0] == '\0')
  821. return;
  822. if (!m_extraparams)
  823. m_extraparams.setown(createProperties(false));
  824. m_extraparams->setProp(name, value);
  825. if(value != NULL && value[0] != '\0')
  826. {
  827. if(stricmp(name, "resourcesBasedn") == 0)
  828. m_ldap_client->setResourceBasedn(value, RT_DEFAULT);
  829. else if(stricmp(name, "workunitsBasedn") == 0)
  830. m_ldap_client->setResourceBasedn(value, RT_WORKUNIT_SCOPE);
  831. }
  832. }
  833. IAuthMap * CLdapSecManager::createAuthMap(IPropertyTree * authconfig)
  834. {
  835. CAuthMap* authmap = new CAuthMap(this);
  836. IPropertyTreeIterator *loc_iter = NULL;
  837. loc_iter = authconfig->getElements(".//Location");
  838. if (loc_iter != NULL)
  839. {
  840. IPropertyTree *location = NULL;
  841. loc_iter->first();
  842. while(loc_iter->isValid())
  843. {
  844. location = &loc_iter->query();
  845. if (location)
  846. {
  847. StringBuffer pathstr, rstr, required, description;
  848. location->getProp("@path", pathstr);
  849. location->getProp("@resource", rstr);
  850. location->getProp("@required", required);
  851. location->getProp("@description", description);
  852. if(pathstr.length() == 0)
  853. throw MakeStringException(-1, "path empty in Authenticate/Location");
  854. if(rstr.length() == 0)
  855. throw MakeStringException(-1, "resource empty in Authenticate/Location");
  856. ISecResourceList* rlist = authmap->queryResourceList(pathstr.str());
  857. if(rlist == NULL)
  858. {
  859. rlist = createResourceList("ldapsecurity");
  860. authmap->add(pathstr.str(), rlist);
  861. }
  862. ISecResource* rs = rlist->addResource(rstr.str());
  863. unsigned requiredaccess = str2perm(required.str());
  864. rs->setRequiredAccessFlags(requiredaccess);
  865. rs->setDescription(description.str());
  866. }
  867. loc_iter->next();
  868. }
  869. loc_iter->Release();
  870. loc_iter = NULL;
  871. }
  872. authmap->addToBackend();
  873. return authmap;
  874. }
  875. IAuthMap * CLdapSecManager::createFeatureMap(IPropertyTree * authconfig)
  876. {
  877. CAuthMap* feature_authmap = new CAuthMap(this);
  878. IPropertyTreeIterator *feature_iter = NULL;
  879. feature_iter = authconfig->getElements(".//Feature");
  880. if (feature_iter != NULL)
  881. {
  882. IPropertyTree *feature = NULL;
  883. feature_iter->first();
  884. while(feature_iter->isValid())
  885. {
  886. feature = &feature_iter->query();
  887. if (feature)
  888. {
  889. StringBuffer pathstr, rstr, required, description;
  890. feature->getProp("@path", pathstr);
  891. feature->getProp("@resource", rstr);
  892. feature->getProp("@required", required);
  893. feature->getProp("@description", description);
  894. ISecResourceList* rlist = feature_authmap->queryResourceList(pathstr.str());
  895. if(rlist == NULL)
  896. {
  897. rlist = createResourceList(pathstr.str());
  898. feature_authmap->add(pathstr.str(), rlist);
  899. }
  900. ISecResource* rs = rlist->addResource(rstr.str());
  901. unsigned requiredaccess = str2perm(required.str());
  902. rs->setRequiredAccessFlags(requiredaccess);
  903. rs->setDescription(description.str());
  904. }
  905. feature_iter->next();
  906. }
  907. feature_iter->Release();
  908. feature_iter = NULL;
  909. }
  910. feature_authmap->addToBackend();
  911. return feature_authmap;
  912. }
  913. bool CLdapSecManager::updateUser(ISecUser& user, const char* newPassword)
  914. {
  915. // Authenticate User first
  916. if(!authenticate(&user))
  917. {
  918. return false;
  919. }
  920. //Update password if authenticated
  921. bool ok = m_ldap_client->updateUser(user, newPassword);
  922. if(ok && m_permissionsCache.isCacheEnabled() && !m_usercache_off)
  923. {
  924. m_permissionsCache.removeFromUserCache(user);
  925. }
  926. return ok;
  927. }
  928. bool CLdapSecManager::updateUser(const char* type, ISecUser& user)
  929. {
  930. bool ok = m_ldap_client->updateUser(type, user);
  931. if(ok && m_permissionsCache.isCacheEnabled() && !m_usercache_off)
  932. m_permissionsCache.removeFromUserCache(user);
  933. return ok;
  934. }
  935. bool CLdapSecManager::updateUser(const char* username, const char* newPassword)
  936. {
  937. return m_ldap_client->updateUser(username, newPassword);
  938. }
  939. void CLdapSecManager::getAllGroups(StringArray & groups)
  940. {
  941. m_ldap_client->getAllGroups(groups);
  942. }
  943. bool CLdapSecManager::getPermissionsArray(const char* basedn, SecResourceType rtype, const char* name, IArrayOf<CPermission>& permissions)
  944. {
  945. return m_ldap_client->getPermissionsArray(basedn, rtype, name, permissions);
  946. }
  947. void CLdapSecManager::addGroup(const char* groupname)
  948. {
  949. m_ldap_client->addGroup(groupname);
  950. }
  951. void CLdapSecManager::deleteGroup(const char* groupname)
  952. {
  953. m_ldap_client->deleteGroup(groupname);
  954. }
  955. bool CLdapSecManager::changePermission(CPermissionAction& action)
  956. {
  957. return m_ldap_client->changePermission(action);
  958. }
  959. void CLdapSecManager::getGroups(const char* username, StringArray & groups)
  960. {
  961. m_ldap_client->getGroups(username, groups);
  962. }
  963. void CLdapSecManager::changeUserGroup(const char* action, const char* username, const char* groupname)
  964. {
  965. m_ldap_client->changeUserGroup(action, username, groupname);
  966. }
  967. bool CLdapSecManager::deleteUser(ISecUser* user)
  968. {
  969. return m_ldap_client->deleteUser(user);
  970. }
  971. void CLdapSecManager::getGroupMembers(const char* groupname, StringArray & users)
  972. {
  973. m_ldap_client->getGroupMembers(groupname, users);
  974. }
  975. void CLdapSecManager::deleteResource(SecResourceType rtype, const char * name, const char * basedn)
  976. {
  977. m_ldap_client->deleteResource(rtype, name, basedn);
  978. time_t tctime = getThreadCreateTime();
  979. if ((m_permissionsCache.isCacheEnabled() || (m_permissionsCache.isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  980. m_permissionsCache.remove(rtype, name);
  981. }
  982. void CLdapSecManager::renameResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn)
  983. {
  984. m_ldap_client->renameResource(rtype, oldname, newname, basedn);
  985. time_t tctime = getThreadCreateTime();
  986. if ((m_permissionsCache.isCacheEnabled() || (m_permissionsCache.isTransactionalEnabled() && tctime > 0)) && (!m_cache_off[rtype]))
  987. m_permissionsCache.remove(rtype, oldname);
  988. }
  989. void CLdapSecManager::copyResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn)
  990. {
  991. m_ldap_client->copyResource(rtype, oldname, newname, basedn);
  992. }
  993. void CLdapSecManager::normalizeDn(const char* dn, StringBuffer& ndn)
  994. {
  995. m_ldap_client->normalizeDn(dn, ndn);
  996. }
  997. bool CLdapSecManager::isSuperUser(ISecUser* user)
  998. {
  999. return m_ldap_client->isSuperUser(user);
  1000. }
  1001. ILdapConfig* CLdapSecManager::queryConfig()
  1002. {
  1003. return m_ldap_client->queryConfig();
  1004. }
  1005. void CLdapSecManager::cacheSwitch(SecResourceType rtype, bool on)
  1006. {
  1007. m_cache_off[rtype] = !on;
  1008. // To make things simple, turning off any resource type's permission cache turns off the userCache.
  1009. if(!on)
  1010. m_usercache_off = true;
  1011. }
  1012. int CLdapSecManager::countUsers(const char* searchstr, int limit)
  1013. {
  1014. return m_ldap_client->countUsers(searchstr, limit);
  1015. }
  1016. int CLdapSecManager::countResources(const char* basedn, const char* searchstr, int limit)
  1017. {
  1018. return m_ldap_client->countResources(basedn, searchstr, limit);
  1019. }
  1020. bool CLdapSecManager::getUserInfo(ISecUser& user, const char* infotype)
  1021. {
  1022. return m_ldap_client->getUserInfo(user, infotype);
  1023. }
  1024. extern "C"
  1025. {
  1026. LDAPSECURITY_API ISecManager * newLdapSecManager(const char *serviceName, IPropertyTree &config)
  1027. {
  1028. return new CLdapSecManager(serviceName, config);
  1029. }
  1030. LDAPSECURITY_API ISecManager * newDefaultSecManager(const char *serviceName, IPropertyTree &config)
  1031. {
  1032. return new CDefaultSecurityManager(serviceName, &config);
  1033. }
  1034. LDAPSECURITY_API ISecManager * newLocalSecManager(const char *serviceName, IPropertyTree &config)
  1035. {
  1036. return new CLocalSecurityManager(serviceName, &config);
  1037. }
  1038. LDAPSECURITY_API IAuthMap *newDefaultAuthMap(IPropertyTree* config)
  1039. {
  1040. CAuthMap* authmap = new CAuthMap(NULL);
  1041. IPropertyTreeIterator *loc_iter = NULL;
  1042. loc_iter = config->getElements(".//Location");
  1043. if (loc_iter != NULL)
  1044. {
  1045. IPropertyTree *location = NULL;
  1046. loc_iter->first();
  1047. while(loc_iter->isValid())
  1048. {
  1049. location = &loc_iter->query();
  1050. if (location)
  1051. {
  1052. StringBuffer pathstr, rstr;
  1053. location->getProp("@path", pathstr);
  1054. authmap->add(pathstr.str(), NULL);
  1055. }
  1056. loc_iter->next();
  1057. }
  1058. loc_iter->Release();
  1059. loc_iter = NULL;
  1060. }
  1061. return authmap;
  1062. }
  1063. }