/esp/services/ws_access/ws_accessService.cpp

http://github.com/hpcc-systems/HPCC-Platform · C++ · 4715 lines · 4004 code · 632 blank · 79 comment · 1079 complexity · fb4b80db762fa919f43758032576b2b8 MD5 · raw file

  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning (disable : 4786)
  14. #include <stdlib.h>
  15. #include "ws_accessService.hpp"
  16. #include "exception_util.hpp"
  17. #include "dasess.hpp"
  18. #include "dautils.hpp"
  19. #include <set>
  20. #define MSG_SEC_MANAGER_IS_NULL "Security manager is not found. Please check if the system authentication is set up correctly"
  21. #define MSG_SEC_MANAGER_ISNT_LDAP "LDAP Security manager is required for this feature. Please enable LDAP in the system configuration"
  22. #define FILE_SCOPE_URL "FileScopeAccess"
  23. #define FILE_SCOPE_RTYPE "file"
  24. #define FILE_SCOPE_RTITLE "FileScope"
  25. #define MAX_USERS_DISPLAY 400
  26. #define MAX_RESOURCES_DISPLAY 3000
  27. static const long MAXXLSTRANSFER = 5000000;
  28. void checkUser(IEspContext& context, const char* rtype = NULL, const char* rtitle = NULL, unsigned int SecAccessFlags = SecAccess_Full)
  29. {
  30. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  31. if(secmgr == NULL)
  32. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  33. if (rtype && rtitle && strieq(rtype, FILE_SCOPE_RTYPE) && strieq(rtitle, FILE_SCOPE_RTITLE))
  34. {
  35. if (!context.validateFeatureAccess(FILE_SCOPE_URL, SecAccessFlags, false))
  36. {
  37. context.setAuthStatus(AUTH_STATUS_NOACCESS);
  38. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to File Scope is denied.");
  39. }
  40. return;
  41. }
  42. if(!secmgr->isSuperUser(context.queryUser()))
  43. {
  44. context.setAuthStatus(AUTH_STATUS_NOACCESS);
  45. throw MakeStringException(ECLWATCH_ADMIN_ACCESS_DENIED, "Access denied, administrators only.");
  46. }
  47. }
  48. void Cws_accessEx::init(IPropertyTree *cfg, const char *process, const char *service)
  49. {
  50. if(cfg == NULL)
  51. throw MakeStringException(-1, "can't initialize Cws_accessEx, cfg is NULL");
  52. StringBuffer xpath;
  53. xpath.appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]", process, service);
  54. IPropertyTree* servicecfg = cfg->getPropTree(xpath.str());
  55. if(servicecfg == NULL)
  56. {
  57. OWARNLOG("Config not found for service %s/%s",process, service);
  58. return;
  59. }
  60. m_servicecfg.setown(servicecfg);
  61. /* Config is like -
  62. <Modules basedn="ou=le,ou=ecl,dc=le">
  63. <Eclserver name="eclserver" basedn="ou=le,ou=ecl,dc=le" templateName="repository.newmoduletemplate"/>
  64. </Modules>
  65. <Files basedn="ou=Files,ou=ecl"/>
  66. <Resources>
  67. <Binding name="EspBinding" service="espsmc" port="8010" basedn="ou=SMC,ou=EspServices,ou=ecl" workunitsBasedn="ou=workunits,ou=ecl"/>
  68. </Resources>
  69. */
  70. Owned<IPropertyTreeIterator> eclservers = m_servicecfg->getElements("Modules/Eclserver");
  71. for (eclservers->first(); eclservers->isValid(); eclservers->next())
  72. {
  73. const char *templatename = eclservers->query().queryProp("@templateName");
  74. const char* basedn = eclservers->query().queryProp("@basedn");
  75. if(basedn && *basedn)
  76. {
  77. StringBuffer name, head;
  78. const char* eclservername = eclservers->query().queryProp("@name");
  79. name.append("Repository Modules for ").append(eclservername);
  80. Owned<IEspDnStruct> onedn = createDnStruct();
  81. onedn->setBasedn(basedn);
  82. onedn->setName(name.str());
  83. onedn->setRtype("module");
  84. onedn->setRtitle("Module");
  85. if(templatename != NULL)
  86. {
  87. onedn->setTemplatename(templatename);
  88. }
  89. m_rawbasedns.append(*onedn.getLink());
  90. }
  91. }
  92. const char* modules_basedn = m_servicecfg->queryProp("Modules/@basedn");
  93. if(modules_basedn && *modules_basedn)
  94. {
  95. Owned<IEspDnStruct> onedn = createDnStruct();
  96. onedn->setBasedn(modules_basedn);
  97. onedn->setName("Repository Modules");
  98. onedn->setRtype("module");
  99. onedn->setRtitle("Module");
  100. m_rawbasedns.append(*onedn.getLink());
  101. }
  102. const char* files_basedn = m_servicecfg->queryProp("Files/@basedn");
  103. if(files_basedn && *files_basedn)
  104. {
  105. Owned<IEspDnStruct> onedn = createDnStruct();
  106. onedn->setBasedn(files_basedn);
  107. onedn->setName("File Scopes");
  108. onedn->setRtype(FILE_SCOPE_RTYPE);
  109. m_rawbasedns.append(*onedn.getLink());
  110. onedn->setRtitle(FILE_SCOPE_RTITLE);
  111. }
  112. StringBuffer workunits_basedn;
  113. Owned<IPropertyTreeIterator> bindings = m_servicecfg->getElements("Resources/Binding");
  114. for (bindings->first(); bindings->isValid(); bindings->next())
  115. {
  116. const char *service = bindings->query().queryProp("@service");
  117. const char* basedn = bindings->query().queryProp("@basedn");
  118. if(workunits_basedn.length() == 0)
  119. {
  120. const char* wubasedn = bindings->query().queryProp("@workunitsBasedn");
  121. if(wubasedn != NULL)
  122. workunits_basedn.append(wubasedn);
  123. }
  124. if(basedn && *basedn)
  125. {
  126. StringBuffer name, head;
  127. name.append("Esp Features for ");
  128. const char* bptr = basedn;
  129. while(*bptr != '\0' && *bptr != '=')
  130. bptr++;
  131. if(*bptr != '\0')
  132. bptr++;
  133. const char* colon = strstr(bptr, ",");
  134. if(colon == NULL)
  135. head.append(bptr);
  136. else
  137. head.append(colon - bptr, bptr);
  138. if(stricmp(head.str(), "WsAttributesAccess") == 0)
  139. continue;
  140. Owned<IEspDnStruct> onedn = createDnStruct();
  141. onedn->setBasedn(basedn);
  142. name.append(head.str());
  143. onedn->setName(name.str());
  144. onedn->setRtype("service");
  145. head.append(" Feature");
  146. onedn->setRtitle(head.str());
  147. m_rawbasedns.append(*onedn.getLink());
  148. }
  149. }
  150. if(workunits_basedn.length() > 0)
  151. {
  152. Owned<IEspDnStruct> onedn = createDnStruct();
  153. onedn->setBasedn(workunits_basedn.str());
  154. onedn->setName("Workunit Scopes");
  155. onedn->setRtype("workunit");
  156. onedn->setRtitle("WorkunitScope");
  157. m_rawbasedns.append(*onedn.getLink());
  158. }
  159. xpath.setf("Software/EspProcess[@name=\"%s\"]/@PageCacheTimeoutSeconds", process);
  160. if (cfg->hasProp(xpath.str()))
  161. setPageCacheTimeoutMilliSeconds(cfg->getPropInt(xpath.str()));
  162. xpath.setf("Software/EspProcess[@name=\"%s\"]/@MaxPageCacheItems", process);
  163. if (cfg->hasProp(xpath.str()))
  164. setMaxPageCacheItems(cfg->getPropInt(xpath.str()));
  165. }
  166. CLdapSecManager* Cws_accessEx::queryLDAPSecurityManager(IEspContext &context)
  167. {
  168. ISecManager* secMgr = context.querySecManager();
  169. if(secMgr && secMgr->querySecMgrType() != SMT_LDAP)
  170. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_ISNT_LDAP);
  171. return dynamic_cast<CLdapSecManager*>(secMgr);
  172. }
  173. void Cws_accessEx::getBasednReq(IEspContext &context, const char* name, const char* basedn,
  174. const char* rType, const char* rTitle, IEspDnStruct* dn)
  175. {
  176. double version = context.getClientVersion();
  177. if (version >= 1.14)
  178. {
  179. if (isEmptyString(name))
  180. throw MakeStringException(ECLWATCH_INVALID_INPUT, "BaseDN not specified");
  181. if(m_basedns.length() == 0)
  182. setBasedns(context);
  183. ForEachItemIn(i, m_basedns)
  184. {
  185. IEspDnStruct& cur = m_basedns.item(i);
  186. if(strieq(cur.getName(), name))
  187. {
  188. dn->setBasedn(cur.getBasedn());
  189. dn->setRtype(cur.getRtype());
  190. dn->setRtitle(cur.getRtitle());
  191. return;
  192. }
  193. }
  194. throw MakeStringException(ECLWATCH_INVALID_INPUT, "BaseDN %s not found", name);
  195. }
  196. //before version 1.14
  197. if (isEmptyString(basedn))
  198. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Basedn not specified");
  199. if (isEmptyString(rType))
  200. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Rtype not specified");
  201. if (isEmptyString(rTitle))
  202. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Rtitle not specified");
  203. dn->setBasedn(basedn);
  204. dn->setRtype(rType);
  205. dn->setRtitle(rTitle);
  206. }
  207. void Cws_accessEx::setBasedns(IEspContext &context)
  208. {
  209. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  210. if(secmgr == NULL)
  211. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  212. set<string> alreadythere;
  213. ForEachItemInRev(x, m_rawbasedns)
  214. {
  215. IEspDnStruct* basedn = &(m_rawbasedns.popGet());
  216. const char* tname = basedn->getTemplatename();
  217. StringBuffer nbasedn;
  218. secmgr->normalizeDn(basedn->getBasedn(), nbasedn);
  219. if(alreadythere.find(nbasedn.str()) == alreadythere.end())
  220. {
  221. alreadythere.insert(nbasedn.str());
  222. Owned<IEspDnStruct> onedn = createDnStruct();
  223. onedn->setBasedn(nbasedn.str());
  224. onedn->setName(basedn->getName());
  225. onedn->setRtype(basedn->getRtype());
  226. onedn->setRtitle(basedn->getRtitle());
  227. if(tname != NULL && *tname != '\0')
  228. onedn->setTemplatename(tname);
  229. m_basedns.append(*onedn.getLink());
  230. }
  231. else
  232. {
  233. ForEachItemIn(y, m_basedns)
  234. {
  235. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  236. if(stricmp(curbasedn->getBasedn(), nbasedn.str()) == 0)
  237. {
  238. const char* curtname = curbasedn->getTemplatename();
  239. if((curtname == NULL || *curtname == '\0') && (tname != NULL && *tname != '\0'))
  240. curbasedn->setTemplatename(tname);
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. return;
  247. }
  248. bool Cws_accessEx::getNewFileScopePermissions(ISecManager* secmgr, const char* name, IEspDnStruct* basednReq, StringBuffer& existingResource, StringArray& newResources)
  249. {
  250. if (!secmgr)
  251. return false;
  252. if (isEmptyString(name))
  253. return false;
  254. char* pStr0 = (char*) name;
  255. while (pStr0[0] == ':') //in case of some ':' by mistake
  256. pStr0++;
  257. if (pStr0[0] == 0)
  258. return false;
  259. StringBuffer lastFileScope;
  260. char* pStr = strstr(pStr0, "::");
  261. while (pStr)
  262. {
  263. char fileScope[10240];
  264. strncpy(fileScope, pStr0, pStr-pStr0);
  265. fileScope[pStr-pStr0] = 0;
  266. if (lastFileScope.length() < 1)
  267. lastFileScope.append(fileScope);
  268. else
  269. lastFileScope.appendf("::%s", fileScope);
  270. newResources.append(lastFileScope.str());
  271. pStr0 = pStr+2;
  272. while (pStr0[0] == ':') //in case of more than two ':' by mistake
  273. pStr0++;
  274. if (pStr0[0] == 0)
  275. break;
  276. pStr = strstr(pStr0, "::");
  277. }
  278. if (pStr0[0] != 0)
  279. {
  280. if (lastFileScope.length() < 1)
  281. lastFileScope.append(pStr0);
  282. else
  283. lastFileScope.appendf("::%s", pStr0);
  284. newResources.append(lastFileScope.str());
  285. }
  286. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  287. while (newResources.ordinality())
  288. {
  289. StringBuffer namebuf(newResources.item(0));
  290. try
  291. {
  292. IArrayOf<CPermission> permissions;
  293. ldapsecmgr->getPermissionsArray(basednReq->getBasedn(), str2type(basednReq->getRtype()), namebuf.str(), permissions);
  294. if (!permissions.ordinality())
  295. {
  296. break;
  297. }
  298. }
  299. catch(IException* e) //exception may be thrown when no permission for the resource
  300. {
  301. e->Release();
  302. break;
  303. }
  304. existingResource.clear().append(namebuf);
  305. newResources.remove(0);
  306. }
  307. return true;
  308. }
  309. bool Cws_accessEx::setNewFileScopePermissions(ISecManager* secmgr, IEspDnStruct* basednReq, StringBuffer& existingResource, StringArray& newResources)
  310. {
  311. if (!secmgr || !newResources.ordinality())
  312. {
  313. return false;
  314. }
  315. const char* basedn = basednReq->getBasedn();
  316. if (!basedn || !*basedn)
  317. {
  318. return false;
  319. }
  320. StringBuffer basednBuf;
  321. basednBuf.append(basedn);
  322. if (existingResource.length() < 1)
  323. {
  324. existingResource.append("files");
  325. const char* comma = strchr(basedn, ',');
  326. const char* eqsign = strchr(basedn, '=');
  327. if(eqsign && comma && (strlen(comma) > 1))
  328. {
  329. basednBuf.clear().append(comma + 1);
  330. }
  331. }
  332. IArrayOf<CPermission> requiredPermissions;
  333. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  334. ldapsecmgr->getPermissionsArray(basednBuf, str2type(basednReq->getRtype()), existingResource.str(), requiredPermissions);
  335. if (!requiredPermissions.ordinality())
  336. {
  337. return false;
  338. }
  339. ForEachItemIn(x, requiredPermissions)
  340. {
  341. CPermission& perm = requiredPermissions.item(x);
  342. int accType = perm.getAccount_type(); //0-individual, 1 - group
  343. const char* actname = perm.getAccount_name();
  344. if (!actname || !*actname)
  345. continue;
  346. CPermissionAction paction;
  347. paction.m_basedn.append(basednReq->getBasedn());
  348. paction.m_rtype = str2type(basednReq->getRtype());
  349. paction.m_account_type = (ACT_TYPE)accType;
  350. paction.m_account_name.append(actname);
  351. paction.m_allows = perm.getAllows();
  352. paction.m_denies = perm.getDenies();
  353. if ((accType != GROUP_ACT) || ((stricmp(actname, "Administrators") != 0) && (stricmp(actname, "Authenticated Users") != 0)))
  354. {
  355. paction.m_action.append("add");
  356. }
  357. else
  358. {
  359. paction.m_action.append("update");
  360. }
  361. ForEachItemIn(y, newResources)
  362. {
  363. StringBuffer namebuf(newResources.item(y));
  364. paction.m_rname.clear().append(namebuf.str());
  365. ldapsecmgr->changePermission(paction);
  366. }
  367. }
  368. return true;
  369. }
  370. bool Cws_accessEx::onUsers(IEspContext &context, IEspUserRequest &req, IEspUserResponse &resp)
  371. {
  372. try
  373. {
  374. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  375. double version = context.getClientVersion();
  376. if (version > 1.03)
  377. {
  378. if(secmgr == NULL)
  379. {
  380. resp.setNoSecMngr(true);
  381. return true;
  382. }
  383. }
  384. else
  385. {
  386. if(secmgr == NULL)
  387. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  388. }
  389. checkUser(context);
  390. const char* searchstr = req.getSearchinput();
  391. int numusers = secmgr->countUsers(searchstr, MAX_USERS_DISPLAY);
  392. if(numusers == -1)
  393. {
  394. resp.setToomany(true);
  395. return true;
  396. }
  397. resp.setToomany(false);
  398. /*
  399. LdapServerType servertype = secmgr->getLdapServerType();
  400. if(servertype != ACTIVE_DIRECTORY)
  401. resp.setPosixok(true);
  402. else
  403. resp.setPosixok(false);
  404. */
  405. resp.setPosixok(false);
  406. IArrayOf<IEspUserInfo> espusers;
  407. IUserArray users;
  408. secmgr->searchUsers(searchstr, users);
  409. ForEachItemIn(x, users)
  410. {
  411. ISecUser* usr = &users.item(x);
  412. if(usr)
  413. {
  414. Owned<IEspUserInfo> oneusr = createUserInfo();
  415. oneusr->setUsername(usr->getName());
  416. oneusr->setFullname(usr->getFullName());
  417. double version = context.getClientVersion();
  418. if (version >= 1.10)
  419. {
  420. oneusr->setEmployeeID(usr->getEmployeeID());
  421. }
  422. if (version >= 1.07)
  423. {
  424. StringBuffer sb;
  425. oneusr->setPasswordexpiration(getPasswordExpiration(usr, sb));
  426. }
  427. if (version >= 1.16)
  428. {
  429. oneusr->setEmployeeNumber(usr->getEmployeeNumber());
  430. }
  431. espusers.append(*oneusr.getLink());
  432. }
  433. }
  434. resp.setUsers(espusers);
  435. }
  436. catch(IException* e)
  437. {
  438. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  439. }
  440. return true;
  441. }
  442. const char *Cws_accessEx::getPasswordExpiration(ISecUser *usr, StringBuffer &passwordExpiration)
  443. {
  444. switch (usr->getPasswordDaysRemaining())//-1 if expired, -2 if never expires
  445. {
  446. case scPasswordExpired:
  447. passwordExpiration.set("Expired");
  448. break;
  449. case scPasswordNeverExpires:
  450. passwordExpiration.set("Never");
  451. break;
  452. default:
  453. {
  454. CDateTime dt;
  455. usr->getPasswordExpiration(dt);
  456. dt.getDateString(passwordExpiration);
  457. break;
  458. }
  459. }
  460. return passwordExpiration.str();
  461. }
  462. bool Cws_accessEx::onUserQuery(IEspContext &context, IEspUserQueryRequest &req, IEspUserQueryResponse &resp)
  463. {
  464. try
  465. {
  466. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  467. if(!secmgr)
  468. {
  469. resp.setNoSecMngr(true);
  470. return true;
  471. }
  472. checkUser(context);
  473. __int64 pageStartFrom = 0;
  474. unsigned pageSize = 100;
  475. if (!req.getPageSize_isNull())
  476. pageSize = req.getPageSize();
  477. if (!req.getPageStartFrom_isNull())
  478. pageStartFrom = req.getPageStartFrom();
  479. UserField sortOrder[2] = {UFName, UFterm};
  480. CUserSortBy sortBy = req.getSortBy();
  481. switch (sortBy)
  482. {
  483. case CUserSortBy_FullName:
  484. sortOrder[0] = UFFullName;
  485. break;
  486. case CUserSortBy_PasswordExpiration:
  487. sortOrder[0] = UFPasswordExpiration;
  488. break;
  489. case CUserSortBy_EmployeeID:
  490. sortOrder[0] = UFEmployeeID;
  491. break;
  492. case CUserSortBy_EmployeeNumber:
  493. sortOrder[0] = UFEmployeeNumber;
  494. break;
  495. default:
  496. break;
  497. }
  498. sortOrder[0] = (UserField) (sortOrder[0] | UFnocase);
  499. bool descending = req.getDescending();
  500. if (descending)
  501. sortOrder[0] = (UserField) (sortOrder[0] | UFreverse);
  502. unsigned total;
  503. __int64 cacheHint;
  504. IArrayOf<IEspUserInfo> espUsers;
  505. Owned<ISecItemIterator> it = secmgr->getUsersSorted(req.getName(), sortOrder, (const __int64) pageStartFrom, (const unsigned) pageSize, &total, &cacheHint);
  506. ForEach(*it)
  507. {
  508. IPropertyTree& usr = it->query();
  509. const char* userName = usr.queryProp(getUserFieldNames(UFName));
  510. if (!userName || !*userName)
  511. continue;
  512. Owned<IEspUserInfo> userInfo = createUserInfo();
  513. userInfo->setUsername(userName);
  514. const char* fullName = usr.queryProp(getUserFieldNames(UFFullName));
  515. if (fullName && *fullName)
  516. userInfo->setFullname(fullName);
  517. const char* passwordExpiration = usr.queryProp(getUserFieldNames(UFPasswordExpiration));
  518. if (passwordExpiration && *passwordExpiration)
  519. userInfo->setPasswordexpiration(passwordExpiration);
  520. const char* employeeID = usr.queryProp(getUserFieldNames(UFEmployeeID));
  521. if (employeeID && *employeeID)
  522. userInfo->setEmployeeID(employeeID);
  523. const char* employeeNumber = usr.queryProp(getUserFieldNames(UFEmployeeNumber));
  524. if (employeeNumber && *employeeNumber)
  525. userInfo->setEmployeeNumber(employeeNumber);
  526. espUsers.append(*userInfo.getClear());
  527. }
  528. resp.setUsers(espUsers);
  529. resp.setTotalUsers(total);
  530. resp.setCacheHint(cacheHint);
  531. }
  532. catch(IException* e)
  533. {
  534. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  535. }
  536. return true;
  537. }
  538. bool Cws_accessEx::onUserEdit(IEspContext &context, IEspUserEditRequest &req, IEspUserEditResponse &resp)
  539. {
  540. try
  541. {
  542. checkUser(context);
  543. ISecManager* secmgr = context.querySecManager();
  544. if(secmgr == NULL)
  545. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  546. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  547. resp.setUsername(req.getUsername());
  548. double version = context.getClientVersion();
  549. if (version >= 1.13)
  550. resp.setIsLDAPAdmin(ldapsecmgr->isSuperUser(context.queryUser()));
  551. StringArray groupnames;
  552. ldapsecmgr->getGroups(req.getUsername(), groupnames);
  553. IArrayOf<IEspGroupInfo> groups;
  554. for(unsigned i = 0; i < groupnames.length(); i++)
  555. {
  556. const char* grpname = groupnames.item(i);
  557. if(grpname == NULL || grpname[0] == '\0')
  558. continue;
  559. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  560. onegrp->setName(grpname);
  561. groups.append(*onegrp.getLink());
  562. }
  563. resp.setGroups(groups);
  564. }
  565. catch(IException* e)
  566. {
  567. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  568. }
  569. return true;
  570. }
  571. bool Cws_accessEx::onUserGroupEditInput(IEspContext &context, IEspUserGroupEditInputRequest &req, IEspUserGroupEditInputResponse &resp)
  572. {
  573. try
  574. {
  575. checkUser(context);
  576. ISecManager* secmgr = context.querySecManager();
  577. if(secmgr == NULL)
  578. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  579. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  580. resp.setUsername(req.getUsername());
  581. set<string> ogrps;
  582. ogrps.insert("Authenticated Users");
  583. StringArray grps;
  584. ldapsecmgr->getGroups(req.getUsername(), grps);
  585. unsigned i = 0;
  586. for(i = 0; i < grps.length(); i++)
  587. {
  588. const char* grp = grps.item(i);
  589. if(grp != NULL && *grp != '\0')
  590. {
  591. ogrps.insert(grp);
  592. }
  593. }
  594. StringArray groupnames;
  595. StringArray managedBy;
  596. StringArray descriptions;
  597. ldapsecmgr->getAllGroups(groupnames, managedBy, descriptions);
  598. IArrayOf<IEspGroupInfo> groups;
  599. for(i = 0; i < groupnames.length(); i++)
  600. {
  601. const char* grpname = groupnames.item(i);
  602. if(grpname == NULL || grpname[0] == '\0')
  603. continue;
  604. if(ogrps.find(grpname) == ogrps.end())
  605. {
  606. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  607. onegrp->setName(grpname);
  608. onegrp->setGroupDesc(descriptions.item(i));
  609. onegrp->setGroupOwner(managedBy.item(i));
  610. groups.append(*onegrp.getLink());
  611. }
  612. }
  613. resp.setGroups(groups);
  614. }
  615. catch(IException* e)
  616. {
  617. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  618. }
  619. return true;
  620. }
  621. bool Cws_accessEx::onUserGroupEdit(IEspContext &context, IEspUserGroupEditRequest &req, IEspUserGroupEditResponse &resp)
  622. {
  623. try
  624. {
  625. checkUser(context);
  626. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  627. if(secmgr == NULL)
  628. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  629. const char* username = req.getUsername();
  630. if(username == NULL || *username == '\0')
  631. {
  632. resp.setRetcode(-1);
  633. resp.setRetmsg("username can't be empty");
  634. return false;
  635. }
  636. StringArray& groupnames = req.getGroupnames();
  637. try
  638. {
  639. for(unsigned i = 0; i < groupnames.length(); i++)
  640. {
  641. const char* grpname = groupnames.item(i);
  642. if(grpname == NULL || *grpname == '\0')
  643. continue;
  644. secmgr->changeUserGroup(req.getAction(), username, grpname);
  645. }
  646. }
  647. catch(IException* e)
  648. {
  649. StringBuffer errmsg;
  650. e->errorMessage(errmsg);
  651. OERRLOG("error changing user's group membership: %s", errmsg.str());
  652. resp.setRetcode(e->errorCode());
  653. resp.setRetmsg(errmsg.str());
  654. return false;
  655. }
  656. resp.setRetcode(0);
  657. resp.setUsername(username);
  658. resp.setAction(req.getAction());
  659. if(stricmp(req.getAction(), "add") == 0)
  660. resp.setRetmsg("user successfully added to groups");
  661. else
  662. resp.setRetmsg("user successfully deleted from groups");
  663. }
  664. catch(IException* e)
  665. {
  666. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  667. }
  668. return true;
  669. }
  670. bool Cws_accessEx::onGroups(IEspContext &context, IEspGroupRequest &req, IEspGroupResponse &resp)
  671. {
  672. try
  673. {
  674. CLdapSecManager* secmgr0 = queryLDAPSecurityManager(context);
  675. double version = context.getClientVersion();
  676. if (version > 1.03)
  677. {
  678. if(secmgr0 == NULL)
  679. {
  680. //throw MakeStringException(-1, "SecManager is NULL, please check if the binding's authentication is set up correctly");
  681. resp.setNoSecMngr(true);
  682. return true;
  683. }
  684. }
  685. checkUser(context);
  686. StringArray groupnames;
  687. StringArray groupManagedBy;
  688. StringArray groupDescriptions;
  689. ISecManager* secmgr = context.querySecManager();
  690. if(secmgr == NULL)
  691. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  692. secmgr->getAllGroups(groupnames, groupManagedBy, groupDescriptions);
  693. ///groupnames.append("Administrators");
  694. ///groupnames.append("Full_Access_TestingOnly");
  695. //groupnames.kill();
  696. if (groupnames.length() > 0)
  697. {
  698. IArrayOf<IEspGroupInfo> groups;
  699. for(unsigned i = 0; i < groupnames.length(); i++)
  700. {
  701. const char* grpname = groupnames.item(i);
  702. //if(grpname == NULL || grpname[0] == '\0' || stricmp(grpname, "Authenticated Users") == 0)
  703. if(grpname == NULL || grpname[0] == '\0')
  704. continue;
  705. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  706. onegrp->setName(grpname);
  707. onegrp->setGroupDesc(groupDescriptions.item(i));
  708. onegrp->setGroupOwner(groupManagedBy.item(i));
  709. groups.append(*onegrp.getLink());
  710. }
  711. resp.setGroups(groups);
  712. }
  713. /*
  714. IArrayOf<IEspGroupInfo> groups;
  715. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  716. onegrp->setName("grpname");
  717. groups.append(*onegrp.getLink());
  718. resp.setGroups(groups);
  719. */
  720. }
  721. catch(IException* e)
  722. {
  723. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  724. }
  725. return true;
  726. }
  727. bool Cws_accessEx::onGroupQuery(IEspContext &context, IEspGroupQueryRequest &req, IEspGroupQueryResponse &resp)
  728. {
  729. try
  730. {
  731. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  732. if(!secmgr)
  733. {
  734. resp.setNoSecMngr(true);
  735. return true;
  736. }
  737. checkUser(context);
  738. __int64 pageStartFrom = 0;
  739. unsigned pageSize = 100;
  740. if (!req.getPageSize_isNull())
  741. pageSize = req.getPageSize();
  742. if (!req.getPageStartFrom_isNull())
  743. pageStartFrom = req.getPageStartFrom();
  744. GroupField sortOrder[2] = {GFName, GFterm};
  745. CGroupSortBy sortBy = req.getSortBy();
  746. switch (sortBy)
  747. {
  748. case CGroupSortBy_ManagedBy:
  749. sortOrder[0] = GFManagedBy;
  750. break;
  751. default:
  752. break;
  753. }
  754. sortOrder[0] = (GroupField) (sortOrder[0] | GFnocase);
  755. bool descending = req.getDescending();
  756. if (descending)
  757. sortOrder[0] = (GroupField) (sortOrder[0] | GFreverse);
  758. unsigned total;
  759. __int64 cacheHint;
  760. IArrayOf<IEspGroupInfo> groups;
  761. Owned<ISecItemIterator> it = secmgr->getGroupsSorted(sortOrder, (const __int64) pageStartFrom, (const unsigned) pageSize, &total, &cacheHint);
  762. ForEach(*it)
  763. {
  764. IPropertyTree& g = it->query();
  765. const char* groupName = g.queryProp(getGroupFieldNames(GFName));
  766. if (!groupName || !*groupName)
  767. continue;
  768. Owned<IEspGroupInfo> groupInfo = createGroupInfo();
  769. groupInfo->setName(groupName);
  770. const char* managedBy = g.queryProp(getGroupFieldNames(GFManagedBy));
  771. if (managedBy && *managedBy)
  772. groupInfo->setGroupOwner(managedBy);
  773. const char* desc = g.queryProp(getGroupFieldNames(GFDesc));
  774. if (desc && *desc)
  775. groupInfo->setGroupDesc(desc);
  776. groups.append(*groupInfo.getClear());
  777. }
  778. resp.setGroups(groups);
  779. resp.setTotalGroups(total);
  780. resp.setCacheHint(cacheHint);
  781. }
  782. catch(IException* e)
  783. {
  784. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  785. }
  786. return true;
  787. }
  788. bool Cws_accessEx::onAddUser(IEspContext &context, IEspAddUserRequest &req, IEspAddUserResponse &resp)
  789. {
  790. try
  791. {
  792. checkUser(context);
  793. ISecManager* secmgr = context.querySecManager();
  794. if(secmgr == NULL)
  795. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  796. const char* username = req.getUsername();
  797. if(username == NULL || *username == '\0')
  798. {
  799. resp.setRetcode(-1);
  800. resp.setRetmsg("username can't be empty");
  801. return false;
  802. }
  803. if(strchr(username, ' '))
  804. {
  805. resp.setRetcode(-1);
  806. resp.setRetmsg("Username can't contain spaces");
  807. return false;
  808. }
  809. CLdapSecManager* secmgr0 = (CLdapSecManager*)secmgr;
  810. if((secmgr0->getLdapServerType() == ACTIVE_DIRECTORY) && (strlen(username) > 20))
  811. {
  812. resp.setRetcode(-1);
  813. resp.setRetmsg("Username can't be more than 20 characters.");
  814. return false;
  815. }
  816. const char* pass1 = req.getPassword1();
  817. const char* pass2 = req.getPassword2();
  818. if(pass1 == NULL || pass2 == NULL || *pass1 == '\0' || *pass2 == '\0' || strcmp(pass1, pass2) != 0)
  819. {
  820. resp.setRetcode(-1);
  821. resp.setRetmsg("password and retype can't be empty and must match.");
  822. return false;
  823. }
  824. const char * employeeID = NULL;
  825. if (context.getClientVersion() >= 1.10)
  826. {
  827. employeeID = req.getEmployeeID();
  828. }
  829. const char * employeeNumber = nullptr;
  830. if (context.getClientVersion() >= 1.16)
  831. {
  832. employeeNumber = req.getEmployeeNumber();
  833. }
  834. Owned<ISecUser> user = secmgr->createUser(username);
  835. ISecCredentials& cred = user->credentials();
  836. const char* firstname = req.getFirstname();
  837. const char* lastname = req.getLastname();
  838. if(firstname != NULL)
  839. user->setFirstName(firstname);
  840. if(lastname != NULL)
  841. user->setLastName(lastname);
  842. if(employeeID != NULL)
  843. user->setEmployeeID(employeeID);
  844. if(employeeNumber != nullptr)
  845. user->setEmployeeNumber(employeeNumber);
  846. if(pass1 != NULL)
  847. cred.setPassword(pass1);
  848. try
  849. {
  850. if (user.get())
  851. secmgr->addUser(*user.get());
  852. }
  853. catch(IException* e)
  854. {
  855. resp.setRetcode(-1);
  856. StringBuffer errmsg;
  857. resp.setRetmsg(e->errorMessage(errmsg).str());
  858. return false;
  859. }
  860. resp.setRetcode(0);
  861. resp.setRetmsg("User successfully added");
  862. }
  863. catch(IException* e)
  864. {
  865. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  866. }
  867. return true;
  868. }
  869. bool Cws_accessEx::onUserAction(IEspContext &context, IEspUserActionRequest &req, IEspUserActionResponse &resp)
  870. {
  871. try
  872. {
  873. checkUser(context);
  874. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  875. if(secmgr == NULL)
  876. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  877. const char* action = req.getActionType();
  878. if (!action || !*action)
  879. throw MakeStringException(ECLWATCH_INVALID_ACTION, "Action not specified.");
  880. if (!stricmp(action, "delete"))
  881. {
  882. StringArray& usernames = req.getUsernames();
  883. for(unsigned i = 0; i < usernames.length(); i++)
  884. {
  885. const char* username = usernames.item(i);
  886. Owned<ISecUser> user = secmgr->createUser(username);
  887. secmgr->deleteUser(user.get());
  888. }
  889. }
  890. else if (!stricmp(action, "export"))
  891. {
  892. StringBuffer users;
  893. StringArray& usernames = req.getUsernames();
  894. for(unsigned i = 0; i < usernames.length(); i++)
  895. {
  896. const char* username = usernames.item(i);
  897. if (i > 0)
  898. users.appendf("&usernames_i%d=%s", i+1, username);
  899. else
  900. users.append(username);
  901. }
  902. resp.setRedirectUrl(StringBuffer("/ws_access/UserAccountExport?usernames_i1=").append(users).str());
  903. }
  904. resp.setAction(action);
  905. }
  906. catch(IException* e)
  907. {
  908. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  909. }
  910. return true;
  911. }
  912. bool Cws_accessEx::onGroupAdd(IEspContext &context, IEspGroupAddRequest &req, IEspGroupAddResponse &resp)
  913. {
  914. try
  915. {
  916. checkUser(context);
  917. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  918. if(secmgr == NULL)
  919. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  920. const char* groupname = req.getGroupname();
  921. if(groupname == NULL || *groupname == '\0')
  922. {
  923. resp.setRetcode(-1);
  924. resp.setRetmsg("Group name can't be empty");
  925. return false;
  926. }
  927. resp.setGroupname(groupname);
  928. double version = context.getClientVersion();
  929. const char * groupDesc = NULL;
  930. const char * groupOwner = NULL;
  931. if (version >= 1.09)
  932. {
  933. groupDesc = req.getGroupDesc();
  934. groupOwner = req.getGroupOwner();
  935. }
  936. try
  937. {
  938. secmgr->addGroup(groupname, groupOwner, groupDesc);
  939. }
  940. catch(IException* e)
  941. {
  942. StringBuffer emsg;
  943. e->errorMessage(emsg);
  944. resp.setRetcode(e->errorCode());
  945. resp.setRetmsg(emsg.str());
  946. return false;
  947. }
  948. catch(...)
  949. {
  950. resp.setRetcode(-1);
  951. resp.setRetmsg("Unknown error");
  952. return false;
  953. }
  954. resp.setRetcode(0);
  955. }
  956. catch(IException* e)
  957. {
  958. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  959. }
  960. return true;
  961. }
  962. bool Cws_accessEx::onGroupAction(IEspContext &context, IEspGroupActionRequest &req, IEspGroupActionResponse &resp)
  963. {
  964. try
  965. {
  966. checkUser(context);
  967. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  968. if(secmgr == NULL)
  969. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  970. const char* action = req.getActionType();
  971. if (!action || !*action)
  972. throw MakeStringException(ECLWATCH_INVALID_ACTION, "Action not specified.");
  973. if (!stricmp(action, "export"))
  974. {
  975. StringBuffer groups;
  976. StringArray& groupnames = req.getGroupnames();
  977. for(unsigned i = 0; i < groupnames.length(); i++)
  978. {
  979. const char* group = groupnames.item(i);
  980. if (i > 0)
  981. groups.appendf("&groupnames_i%d=%s", i+1, group);
  982. else
  983. groups.append(group);
  984. }
  985. resp.setRedirectUrl(StringBuffer("/ws_access/UserAccountExport?groupnames_i1=").append(groups).str());
  986. }
  987. else if (!stricmp(action, "delete"))
  988. {
  989. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  990. StringArray& groupnames = req.getGroupnames();
  991. IArrayOf<IEspAccountPermission> accountPermissions;
  992. double version = context.getClientVersion();
  993. if (version > 1.01)
  994. {
  995. bool bDeletePermission = false;
  996. if(!req.getDeletePermission_isNull())
  997. bDeletePermission = req.getDeletePermission();
  998. if(m_basedns.length() == 0)
  999. {
  1000. setBasedns(context);
  1001. }
  1002. ForEachItemIn(y, m_basedns)
  1003. {
  1004. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  1005. const char *basednName = curbasedn->getName();
  1006. const char *aBasedn = curbasedn->getBasedn();
  1007. const char *aRtype = curbasedn->getRtype();
  1008. if (!aBasedn || !*aBasedn ||!aRtype || !*aRtype)
  1009. continue;
  1010. SecResourceType rtype = str2type(aRtype);
  1011. IArrayOf<IEspResource> ResourceArray;
  1012. if(rtype == RT_WORKUNIT_SCOPE)
  1013. {
  1014. StringBuffer deft_basedn, deft_name;
  1015. const char* comma = strchr(aBasedn, ',');
  1016. const char* eqsign = strchr(aBasedn, '=');
  1017. if(eqsign != NULL)
  1018. {
  1019. if(comma == NULL)
  1020. deft_name.append(eqsign+1);
  1021. else
  1022. {
  1023. deft_name.append(comma - eqsign - 1, eqsign+1);
  1024. deft_basedn.append(comma + 1);
  1025. }
  1026. }
  1027. if (deft_name.length() > 0)
  1028. {
  1029. Owned<IEspResource> oneresource = createResource();
  1030. oneresource->setName(deft_name);
  1031. oneresource->setDescription(deft_basedn);
  1032. ResourceArray.append(*oneresource.getLink());
  1033. }
  1034. }
  1035. IArrayOf<ISecResource> resources;
  1036. if(secmgr->getResources(rtype, aBasedn, resources))
  1037. {
  1038. ForEachItemIn(y1, resources)
  1039. {
  1040. ISecResource& r = resources.item(y1);
  1041. const char* rname = r.getName();
  1042. if(rname == NULL || *rname == '\0')
  1043. continue;
  1044. Owned<IEspResource> oneresource = createResource();
  1045. oneresource->setName(rname);
  1046. oneresource->setDescription(aBasedn);
  1047. ResourceArray.append(*oneresource.getLink());
  1048. }
  1049. }
  1050. ForEachItemIn(y2, ResourceArray)
  1051. {
  1052. IEspResource& r = ResourceArray.item(y2);
  1053. const char* resourceName = r.getName();
  1054. const char* bnname = r.getDescription();
  1055. if (isEmptyString(resourceName))
  1056. continue;
  1057. try
  1058. {
  1059. IArrayOf<CPermission> permissions;
  1060. ldapsecmgr->getPermissionsArray(bnname, rtype, resourceName, permissions);
  1061. ForEachItemIn(x, permissions)
  1062. {
  1063. CPermission& perm = permissions.item(x);
  1064. const char* actname = perm.getAccount_name();
  1065. int accountType = perm.getAccount_type(); //0-individual, 1 - group
  1066. //if ((bGroupAccount && accountType < 1) || (!bGroupAccount && accountType > 0))
  1067. if (accountType < 1 || !actname || !*actname) //Support Group only
  1068. continue;
  1069. ForEachItemIn(x1, groupnames)
  1070. {
  1071. const char* groupname = groupnames.item(x1);
  1072. if (groupname && !strcmp(actname, groupname))
  1073. {
  1074. ///bDeletePermission = true;
  1075. if (!bDeletePermission)
  1076. {
  1077. Owned<IEspAccountPermission> onepermission = createAccountPermission();
  1078. if (version < 1.15)
  1079. {
  1080. onepermission->setBasedn(bnname);
  1081. onepermission->setRType(aRtype);
  1082. }
  1083. else
  1084. {
  1085. onepermission->setBasednName(basednName);
  1086. }
  1087. onepermission->setResourceName(resourceName);
  1088. onepermission->setPermissionName(groupname);
  1089. accountPermissions.append(*onepermission.getLink());
  1090. }
  1091. else
  1092. {
  1093. CPermissionAction paction;
  1094. paction.m_basedn.append(bnname);
  1095. paction.m_rtype = rtype;
  1096. paction.m_rname.append(resourceName);
  1097. paction.m_account_name.append(actname);
  1098. paction.m_account_type = (ACT_TYPE) accountType;
  1099. paction.m_allows = perm.getAllows();
  1100. paction.m_denies = perm.getDenies();
  1101. paction.m_action.append("delete");
  1102. if(!ldapsecmgr->changePermission(paction))
  1103. {
  1104. resp.setRetcode(-1);
  1105. resp.setRetmsg("Unknown error");
  1106. return false;
  1107. }
  1108. }
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. catch(IException* e)
  1115. {
  1116. e->Release();
  1117. }
  1118. }
  1119. }
  1120. }
  1121. try
  1122. {
  1123. if (accountPermissions.length() < 1)
  1124. {
  1125. ForEachItemIn(x1, groupnames)
  1126. {
  1127. const char* groupname = groupnames.item(x1);
  1128. secmgr->deleteGroup(groupname);
  1129. }
  1130. }
  1131. else
  1132. {
  1133. StringBuffer groupnamestr;
  1134. groupnamestr.append("DeletePermission=1");
  1135. ForEachItemIn(x1, groupnames)
  1136. {
  1137. const char* groupname = groupnames.item(x1);
  1138. groupnamestr.appendf("&groupnames_i%d=%s", x1+1, groupname);
  1139. }
  1140. resp.setPermissions(accountPermissions);
  1141. resp.setGroupnames(groupnamestr.str());
  1142. resp.setRetcode(0);
  1143. }
  1144. }
  1145. catch(IException* e)
  1146. {
  1147. StringBuffer emsg;
  1148. e->errorMessage(emsg);
  1149. resp.setRetcode(e->errorCode());
  1150. resp.setRetmsg(emsg.str());
  1151. return false;
  1152. }
  1153. catch(...)
  1154. {
  1155. resp.setRetcode(-1);
  1156. resp.setRetmsg("Unknown error");
  1157. return false;
  1158. }
  1159. }
  1160. resp.setRetcode(0);
  1161. }
  1162. catch(IException* e)
  1163. {
  1164. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1165. }
  1166. return true;
  1167. }
  1168. bool Cws_accessEx::onGroupEdit(IEspContext &context, IEspGroupEditRequest &req, IEspGroupEditResponse &resp)
  1169. {
  1170. try
  1171. {
  1172. checkUser(context);
  1173. ISecManager* secmgr = context.querySecManager();
  1174. if(secmgr == NULL)
  1175. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1176. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  1177. resp.setGroupname(req.getGroupname());
  1178. StringArray usernames;
  1179. ldapsecmgr->getGroupMembers(req.getGroupname(), usernames);
  1180. IArrayOf<IEspUserInfo> users;
  1181. unsigned i = 0;
  1182. for(i = 0; i < usernames.length(); i++)
  1183. {
  1184. const char* usrname = usernames.item(i);
  1185. if(usrname == NULL || usrname[0] == '\0')
  1186. continue;
  1187. ///////////////////////////////////////BUG#41536///////////////
  1188. bool bFound = false;
  1189. IUserArray usersInBaseDN;
  1190. ldapsecmgr->searchUsers(usrname, usersInBaseDN);
  1191. ForEachItemIn(x, usersInBaseDN)
  1192. {
  1193. ISecUser* usr = &usersInBaseDN.item(x);
  1194. if(usr)
  1195. {
  1196. const char* usrname = usr->getName();
  1197. if(usrname == NULL || usrname[0] == '\0')
  1198. continue;
  1199. bFound = true;
  1200. break;
  1201. }
  1202. }
  1203. if (!bFound)
  1204. continue;
  1205. //////////////////////////////////////////////////////////////
  1206. Owned<IEspUserInfo> oneusr = createUserInfo();
  1207. oneusr->setUsername(usrname);
  1208. users.append(*oneusr.getLink());
  1209. }
  1210. resp.setUsers(users);
  1211. }
  1212. catch(IException* e)
  1213. {
  1214. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1215. }
  1216. return true;
  1217. }
  1218. bool Cws_accessEx::onGroupMemberQuery(IEspContext &context, IEspGroupMemberQueryRequest &req, IEspGroupMemberQueryResponse &resp)
  1219. {
  1220. try
  1221. {
  1222. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  1223. if(!secmgr)
  1224. {
  1225. resp.setNoSecMngr(true);
  1226. return true;
  1227. }
  1228. checkUser(context);
  1229. __int64 pageStartFrom = 0;
  1230. unsigned pageSize = 100;
  1231. if (!req.getPageSize_isNull())
  1232. pageSize = req.getPageSize();
  1233. if (!req.getPageStartFrom_isNull())
  1234. pageStartFrom = req.getPageStartFrom();
  1235. UserField sortOrder[2] = {UFName, UFterm};
  1236. CUserSortBy sortBy = req.getSortBy();
  1237. switch (sortBy)
  1238. {
  1239. case CUserSortBy_FullName:
  1240. sortOrder[0] = UFFullName;
  1241. break;
  1242. case CUserSortBy_PasswordExpiration:
  1243. sortOrder[0] = UFPasswordExpiration;
  1244. break;
  1245. case CUserSortBy_EmployeeID:
  1246. sortOrder[0] = UFEmployeeID;
  1247. break;
  1248. case CUserSortBy_EmployeeNumber:
  1249. sortOrder[0] = UFEmployeeNumber;
  1250. break;
  1251. default:
  1252. break;
  1253. }
  1254. sortOrder[0] = (UserField) (sortOrder[0] | UFnocase);
  1255. bool descending = req.getDescending();
  1256. if (descending)
  1257. sortOrder[0] = (UserField) (sortOrder[0] | UFreverse);
  1258. unsigned total;
  1259. __int64 cacheHint;
  1260. IArrayOf<IEspUserInfo> users;
  1261. Owned<ISecItemIterator> it = secmgr->getGroupMembersSorted(req.getGroupName(), sortOrder, (const __int64) pageStartFrom, (const unsigned) pageSize, &total, &cacheHint);
  1262. ForEach(*it)
  1263. {
  1264. IPropertyTree& usr = it->query();
  1265. const char* userName = usr.queryProp(getUserFieldNames(UFName));
  1266. if (!userName || !*userName)
  1267. continue;
  1268. Owned<IEspUserInfo> userInfo = createUserInfo();
  1269. userInfo->setUsername(userName);
  1270. const char* fullName = usr.queryProp(getUserFieldNames(UFFullName));
  1271. if (fullName && *fullName)
  1272. userInfo->setFullname(fullName);
  1273. const char* passwordExpiration = usr.queryProp(getUserFieldNames(UFPasswordExpiration));
  1274. if (passwordExpiration && *passwordExpiration)
  1275. userInfo->setPasswordexpiration(passwordExpiration);
  1276. const char* employeeID = usr.queryProp(getUserFieldNames(UFEmployeeID));
  1277. if (employeeID && *employeeID)
  1278. userInfo->setEmployeeID(employeeID);
  1279. const char* employeeNumber = usr.queryProp(getUserFieldNames(UFEmployeeNumber));
  1280. if (employeeNumber && *employeeNumber)
  1281. userInfo->setEmployeeNumber(employeeNumber);
  1282. users.append(*userInfo.getLink());
  1283. }
  1284. resp.setUsers(users);
  1285. resp.setTotalUsers(total);
  1286. resp.setCacheHint(cacheHint);
  1287. }
  1288. catch(IException* e)
  1289. {
  1290. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1291. }
  1292. return true;
  1293. }
  1294. bool Cws_accessEx::onGroupMemberEditInput(IEspContext &context, IEspGroupMemberEditInputRequest &req, IEspGroupMemberEditInputResponse &resp)
  1295. {
  1296. try
  1297. {
  1298. checkUser(context);
  1299. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  1300. if(secmgr == NULL)
  1301. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1302. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  1303. resp.setGroupname(req.getGroupname());
  1304. set<string> ousrs;
  1305. StringArray ousernames;
  1306. ldapsecmgr->getGroupMembers(req.getGroupname(), ousernames);
  1307. unsigned i = 0;
  1308. for(i = 0; i < ousernames.length(); i++)
  1309. {
  1310. const char* username = ousernames.item(i);
  1311. if(username != NULL && *username != '\0')
  1312. {
  1313. ousrs.insert(username);
  1314. }
  1315. }
  1316. const char* searchstr = req.getSearchinput();
  1317. int numusers = secmgr->countUsers(searchstr, MAX_USERS_DISPLAY+ousernames.ordinality());
  1318. if(numusers == -1)
  1319. {
  1320. resp.setToomany(true);
  1321. return true;
  1322. }
  1323. resp.setToomany(false);
  1324. IArrayOf<IEspUserInfo> espusers;
  1325. IUserArray users;
  1326. secmgr->searchUsers(searchstr, users);
  1327. ForEachItemIn(x, users)
  1328. {
  1329. ISecUser* usr = &users.item(x);
  1330. if(usr)
  1331. {
  1332. const char* usrname = usr->getName();
  1333. if(usrname == NULL || usrname[0] == '\0')
  1334. continue;
  1335. if(ousrs.find(usrname) == ousrs.end())
  1336. {
  1337. Owned<IEspUserInfo> oneusr = createUserInfo();
  1338. oneusr->setUsername(usr->getName());
  1339. espusers.append(*oneusr.getLink());
  1340. }
  1341. }
  1342. }
  1343. resp.setUsers(espusers);
  1344. }
  1345. catch(IException* e)
  1346. {
  1347. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1348. }
  1349. return true;
  1350. }
  1351. bool Cws_accessEx::onGroupMemberEdit(IEspContext &context, IEspGroupMemberEditRequest &req, IEspGroupMemberEditResponse &resp)
  1352. {
  1353. try
  1354. {
  1355. checkUser(context);
  1356. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  1357. if(secmgr == NULL)
  1358. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1359. const char* groupname = req.getGroupname();
  1360. if(groupname == NULL || *groupname == '\0')
  1361. {
  1362. resp.setRetcode(-1);
  1363. resp.setRetmsg("group can't be empty");
  1364. return false;
  1365. }
  1366. ///////////////////////////////////////BUG#41536///////////////
  1367. StringArray existing_usernames;
  1368. if (!stricmp(req.getAction(), "add"))
  1369. secmgr->getGroupMembers(groupname, existing_usernames);
  1370. //////////////////////////////////////////////////////
  1371. StringArray& usernames = req.getUsernames();
  1372. try
  1373. {
  1374. for(unsigned i = 0; i < usernames.length(); i++)
  1375. {
  1376. const char* usrname = usernames.item(i);
  1377. if(usrname == NULL || *usrname == '\0')
  1378. continue;
  1379. ///////////////////////////////////////BUG#41536///////////////
  1380. bool bFound = false;
  1381. if (existing_usernames.length() > 0)
  1382. {
  1383. for(unsigned i = 0; i < existing_usernames.length(); i++)
  1384. {
  1385. const char* existing_usrname = existing_usernames.item(i);
  1386. if(existing_usrname == NULL || existing_usrname[0] == '\0')
  1387. continue;
  1388. if (!strcmp(usrname, existing_usrname))
  1389. {
  1390. bFound = true;
  1391. break;
  1392. }
  1393. }
  1394. }
  1395. if (!bFound)
  1396. //////////////////////////////////////////////////////
  1397. secmgr->changeUserGroup(req.getAction(), usrname, groupname);
  1398. }
  1399. }
  1400. catch(IException* e)
  1401. {
  1402. StringBuffer errmsg;
  1403. e->errorMessage(errmsg);
  1404. OERRLOG("error changing user's group membership: %s", errmsg.str());
  1405. resp.setRetcode(e->errorCode());
  1406. resp.setRetmsg(errmsg.str());
  1407. return false;
  1408. }
  1409. resp.setRetcode(0);
  1410. resp.setGroupname(groupname);
  1411. resp.setAction(req.getAction());
  1412. if(stricmp(req.getAction(), "add") == 0)
  1413. resp.setRetmsg("members successfully added to group");
  1414. else
  1415. resp.setRetmsg("members successfully deleted from group");
  1416. }
  1417. catch(IException* e)
  1418. {
  1419. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1420. }
  1421. return true;
  1422. }
  1423. bool Cws_accessEx::onPermissions(IEspContext &context, IEspBasednsRequest &req, IEspBasednsResponse &resp)
  1424. {
  1425. try
  1426. {
  1427. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  1428. double version = context.getClientVersion();
  1429. if (version > 1.03)
  1430. {
  1431. if(secmgr == NULL)
  1432. {
  1433. resp.setNoSecMngr(true);
  1434. return true;
  1435. }
  1436. }
  1437. else
  1438. {
  1439. if(secmgr == NULL)
  1440. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1441. }
  1442. checkUser(context);
  1443. if(m_basedns.length() == 0)
  1444. {
  1445. setBasedns(context);
  1446. }
  1447. resp.setBasedns(m_basedns);
  1448. }
  1449. catch(IException* e)
  1450. {
  1451. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1452. }
  1453. return true;
  1454. }
  1455. bool Cws_accessEx::onResources(IEspContext &context, IEspResourcesRequest &req, IEspResourcesResponse &resp)
  1456. {
  1457. try
  1458. {
  1459. Owned<IEspDnStruct> basednReq = createDnStruct();
  1460. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  1461. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Read);
  1462. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  1463. if(secmgr == NULL)
  1464. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1465. double version = context.getClientVersion();
  1466. const char* filterInput = req.getSearchinput();
  1467. const char* basedn = basednReq->getBasedn();
  1468. const char* moduletemplate = NULL;
  1469. ForEachItemIn(x, m_basedns)
  1470. {
  1471. IEspDnStruct* curbasedn = &(m_basedns.item(x));
  1472. if(stricmp(curbasedn->getBasedn(), basedn) == 0)
  1473. {
  1474. moduletemplate = curbasedn->getTemplatename();
  1475. }
  1476. }
  1477. if (version < 1.14)
  1478. {
  1479. resp.setBasedn(basedn);
  1480. resp.setRtype(basednReq->getRtype());
  1481. resp.setRtitle(basednReq->getRtitle());
  1482. }
  1483. SecResourceType rtype = str2type(basednReq->getRtype());
  1484. if(rtype == RT_FILE_SCOPE || rtype == RT_WORKUNIT_SCOPE)
  1485. {
  1486. StringBuffer deft_basedn, deft_name;
  1487. const char* comma = strchr(basedn, ',');
  1488. const char* eqsign = strchr(basedn, '=');
  1489. if(eqsign != NULL)
  1490. {
  1491. if(comma == NULL)
  1492. deft_name.append(eqsign+1);
  1493. else
  1494. {
  1495. deft_name.append(comma - eqsign - 1, eqsign+1);
  1496. deft_basedn.append(comma + 1);
  1497. }
  1498. resp.setDefault_basedn(deft_basedn.str());
  1499. resp.setDefault_name(deft_name.str());
  1500. }
  1501. }
  1502. IArrayOf<IEspResource> rarray;
  1503. IArrayOf<ISecResource> resources;
  1504. const char* prefix = req.getPrefix();
  1505. int prefixlen = 0;
  1506. if(prefix && *prefix)
  1507. {
  1508. prefixlen = strlen(prefix);
  1509. if (version < 1.14)
  1510. resp.setPrefix(prefix);
  1511. }
  1512. if (version > 1.04)
  1513. {
  1514. int numResources = -1;
  1515. if (req.getRtitle() && !stricmp(req.getRtitle(), "CodeGenerator Permission"))
  1516. numResources = secmgr->countResources(basedn, prefix, MAX_RESOURCES_DISPLAY);
  1517. else
  1518. numResources = secmgr->countResources(basedn, filterInput, MAX_RESOURCES_DISPLAY);
  1519. if(numResources == -1)
  1520. {
  1521. resp.setToomany(true);
  1522. return true;
  1523. }
  1524. else
  1525. {
  1526. resp.setToomany(false);
  1527. }
  1528. }
  1529. if ((!filterInput || !*filterInput) && req.getRtitle() && !stricmp(req.getRtitle(), "CodeGenerator Permission"))
  1530. {
  1531. if(!secmgr->getResourcesEx(rtype, basedn, prefix, resources))
  1532. return false;
  1533. }
  1534. else
  1535. {
  1536. if(!secmgr->getResourcesEx(rtype, basedn, filterInput, resources))
  1537. return false;
  1538. }
  1539. ILdapConfig* cfg = secmgr->queryConfig();
  1540. for(unsigned i = 0; i < resources.length(); i++)
  1541. {
  1542. ISecResource& r = resources.item(i);
  1543. Owned<IEspResource> oneresource = createResource();
  1544. oneresource->setIsSpecial(false);
  1545. const char* rname = r.getName();
  1546. if(rname == NULL || *rname == '\0')
  1547. continue;
  1548. if(prefix && *prefix)
  1549. {
  1550. if(strncmp(prefix, rname, prefixlen) != 0)
  1551. continue;
  1552. else
  1553. rname += prefixlen;
  1554. }
  1555. if(rtype == RT_MODULE)
  1556. {
  1557. if(stricmp(rname, "repository") != 0)
  1558. {
  1559. if(moduletemplate != NULL && stricmp(rname, moduletemplate) == 0)
  1560. oneresource->setIsSpecial(true);
  1561. if(Utils::strncasecmp(rname, "repository.", 11) == 0)
  1562. rname = rname + 11;
  1563. else
  1564. continue;
  1565. }
  1566. else
  1567. {
  1568. oneresource->setIsSpecial(true);
  1569. }
  1570. }
  1571. else if(rtype == RT_FILE_SCOPE && stricmp(rname, "file") == 0)
  1572. {
  1573. //oneresource->setIsSpecial(true); //33067
  1574. continue;
  1575. }
  1576. oneresource->setName(rname);
  1577. oneresource->setDescription(r.getDescription());
  1578. rarray.append(*oneresource.getLink());
  1579. }
  1580. if (version >= 1.08)
  1581. {
  1582. Owned<IUserDescriptor> userdesc;
  1583. userdesc.setown(createUserDescriptor());
  1584. userdesc->set(context.queryUserId(), context.queryPassword(), context.querySignature());
  1585. int retCode;
  1586. StringBuffer retMsg;
  1587. bool isEnabled = querySessionManager().queryScopeScansEnabled(userdesc, &retCode, retMsg);
  1588. if (retCode != 0)
  1589. DBGLOG("Error %d querying scope scan status : %s", retCode, retMsg.str());
  1590. resp.updateScopeScansStatus().setIsEnabled(isEnabled);
  1591. resp.updateScopeScansStatus().setRetcode(retCode);
  1592. resp.updateScopeScansStatus().setRetmsg(retMsg.str());
  1593. }
  1594. resp.setResources(rarray);
  1595. }
  1596. catch(IException* e)
  1597. {
  1598. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1599. }
  1600. return true;
  1601. }
  1602. bool Cws_accessEx::onResourceQuery(IEspContext &context, IEspResourceQueryRequest &req, IEspResourceQueryResponse &resp)
  1603. {
  1604. try
  1605. {
  1606. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  1607. if(!secmgr)
  1608. {
  1609. resp.setNoSecMngr(true);
  1610. return true;
  1611. }
  1612. Owned<IEspDnStruct> basednReq = createDnStruct();
  1613. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  1614. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Read);
  1615. SecResourceType rtype = str2type(basednReq->getRtype());
  1616. const char* moduleTemplate = NULL;
  1617. ForEachItemIn(x, m_basedns)
  1618. {
  1619. IEspDnStruct* curbasedn = &(m_basedns.item(x));
  1620. if(strieq(curbasedn->getBasedn(), basednReq->getBasedn()))
  1621. {
  1622. moduleTemplate = curbasedn->getTemplatename();
  1623. break;
  1624. }
  1625. }
  1626. StringBuffer nameReq(req.getName());
  1627. const char* prefix = req.getPrefix();
  1628. if (!nameReq.length() && basednReq->getRtitle() && !stricmp(basednReq->getRtitle(), "CodeGenerator Permission"))
  1629. nameReq.set(prefix);
  1630. __int64 pageStartFrom = 0;
  1631. unsigned pageSize = 100;
  1632. if (!req.getPageSize_isNull())
  1633. pageSize = req.getPageSize();
  1634. if (!req.getPageStartFrom_isNull())
  1635. pageStartFrom = req.getPageStartFrom();
  1636. ResourceField sortOrder[2] = {(ResourceField) (RFName | RFnocase), RFterm};
  1637. bool descending = req.getDescending();
  1638. if (descending)
  1639. sortOrder[0] = (ResourceField) (sortOrder[0] | RFreverse);
  1640. unsigned total;
  1641. __int64 cacheHint;
  1642. IArrayOf<IEspResource> rarray;
  1643. Owned<ISecItemIterator> it = secmgr->getResourcesSorted(rtype, basednReq->getBasedn(), nameReq.str(),
  1644. RF_RT_FILE_SCOPE_FILE | RF_RT_MODULE_NO_REPOSITORY, sortOrder,
  1645. (const __int64) pageStartFrom, (const unsigned) pageSize, &total, &cacheHint);
  1646. ForEach(*it)
  1647. {
  1648. IPropertyTree& r = it->query();
  1649. const char* rname = r.queryProp(getResourceFieldNames(RFName));
  1650. if(!rname || !*rname)
  1651. continue;
  1652. if(prefix && *prefix)
  1653. rname += strlen(prefix); //Remove the prefix from the name
  1654. bool isSpecial = false;
  1655. if(rtype == RT_MODULE)
  1656. {
  1657. if(strieq(rname, "repository"))
  1658. isSpecial = true;
  1659. else
  1660. {
  1661. if(moduleTemplate != NULL && stricmp(rname, moduleTemplate) == 0)
  1662. isSpecial = true;
  1663. rname = rname + 11; //Remove "repository." from the name
  1664. }
  1665. }
  1666. Owned<IEspResource> oneresource = createResource();
  1667. oneresource->setName(rname);
  1668. oneresource->setIsSpecial(isSpecial);
  1669. const char* desc = r.queryProp(getResourceFieldNames(RFDesc));
  1670. if (desc && *desc)
  1671. oneresource->setDescription(desc);
  1672. rarray.append(*oneresource.getClear());
  1673. }
  1674. resp.setResources(rarray);
  1675. resp.setTotalResources(total);
  1676. resp.setCacheHint(cacheHint);
  1677. }
  1678. catch(IException* e)
  1679. {
  1680. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1681. }
  1682. return true;
  1683. }
  1684. bool Cws_accessEx::onResourceAddInput(IEspContext &context, IEspResourceAddInputRequest &req, IEspResourceAddInputResponse &resp)
  1685. {
  1686. try
  1687. {
  1688. checkUser(context, req.getRtype(), req.getRtitle(), SecAccess_Full);
  1689. resp.setBasedn(req.getBasedn());
  1690. resp.setRtype(req.getRtype());
  1691. resp.setRtitle(req.getRtitle());
  1692. resp.setPrefix(req.getPrefix());
  1693. }
  1694. catch(IException* e)
  1695. {
  1696. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1697. }
  1698. return true;
  1699. }
  1700. SecResourceType Cws_accessEx::str2type(const char* rtstr)
  1701. {
  1702. if(rtstr == NULL || *rtstr == '\0')
  1703. return RT_DEFAULT;
  1704. else if(stricmp(rtstr, "module") == 0)
  1705. return RT_MODULE;
  1706. else if(stricmp(rtstr, "service") == 0)
  1707. return RT_SERVICE;
  1708. else if(stricmp(rtstr, "file") == 0)
  1709. return RT_FILE_SCOPE;
  1710. else if(stricmp(rtstr, "workunit") == 0)
  1711. return RT_WORKUNIT_SCOPE;
  1712. else
  1713. return RT_DEFAULT;
  1714. }
  1715. bool Cws_accessEx::onResourceAdd(IEspContext &context, IEspResourceAddRequest &req, IEspResourceAddResponse &resp)
  1716. {
  1717. try
  1718. {
  1719. Owned<IEspDnStruct> basednReq = createDnStruct();
  1720. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  1721. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Full);
  1722. ISecManager* secmgr = context.querySecManager();
  1723. if(secmgr == NULL)
  1724. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1725. double version = context.getClientVersion();
  1726. if (version < 1.14)
  1727. {
  1728. resp.setBasedn(basednReq->getBasedn());
  1729. resp.setRtype(basednReq->getRtype());
  1730. resp.setRtitle(basednReq->getRtitle());
  1731. resp.setPrefix(req.getPrefix());
  1732. }
  1733. StringBuffer lastResource;
  1734. StringArray newResources;
  1735. if(str2type(basednReq->getRtype()) == RT_FILE_SCOPE)
  1736. {
  1737. getNewFileScopePermissions(secmgr, req.getName(), basednReq, lastResource, newResources);
  1738. }
  1739. SecResourceType rtype = str2type(basednReq->getRtype());
  1740. try
  1741. {
  1742. ISecUser* usr = NULL;
  1743. Owned<ISecResourceList> rlist = secmgr->createResourceList("ws_access");
  1744. const char* name = req.getName();
  1745. if(name == NULL || *name == '\0')
  1746. {
  1747. resp.setRetcode(-1);
  1748. StringBuffer errmsg;
  1749. errmsg.append(basednReq->getRtitle()).append(" name can't be empty");
  1750. resp.setRetmsg(errmsg.str());
  1751. return false;
  1752. }
  1753. if(strchr(name, '\\') != NULL || strchr(name, '/') != NULL)
  1754. {
  1755. resp.setRetcode(-1);
  1756. StringBuffer errmsg;
  1757. errmsg.append(" you can't have '\\' or '/' in the name");
  1758. resp.setRetmsg(errmsg.str());
  1759. return false;
  1760. }
  1761. const char* ptr = strchr(name, ':');
  1762. while(ptr != NULL)
  1763. {
  1764. if(*(ptr+1) != ':')
  1765. throw MakeStringException(ECLWATCH_SINGLE_COLON_NOT_ALLOWED, "Single colon is not allowed in scope names. Please use double colon");
  1766. ptr = strchr(ptr+2, ':');
  1767. }
  1768. StringBuffer namebuf(name);
  1769. if(rtype == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  1770. namebuf.insert(0, "repository.");
  1771. const char* prefix = req.getPrefix();
  1772. if(prefix && *prefix)
  1773. namebuf.insert(0, prefix);
  1774. ISecResource* r = rlist->addResource(namebuf.str());
  1775. r->setDescription(req.getDescription());
  1776. secmgr->addResourcesEx(rtype, *usr, rlist, PT_DEFAULT, basednReq->getBasedn());
  1777. if(str2type(basednReq->getRtype()) == RT_FILE_SCOPE && newResources.ordinality())
  1778. {
  1779. setNewFileScopePermissions(secmgr, basednReq, lastResource, newResources);
  1780. StringBuffer retmsg;
  1781. ForEachItemIn(y, newResources)
  1782. {
  1783. StringBuffer namebuf(newResources.item(y));
  1784. if (retmsg.length() < 1)
  1785. retmsg.append(namebuf);
  1786. else
  1787. retmsg.appendf(", %s", namebuf.str());
  1788. }
  1789. resp.setRetmsg(retmsg.str());
  1790. }
  1791. }
  1792. catch(IException* e)
  1793. {
  1794. StringBuffer emsg;
  1795. e->errorMessage(emsg);
  1796. resp.setRetcode(e->errorCode());
  1797. resp.setRetmsg(emsg.str());
  1798. return false;
  1799. }
  1800. catch(...)
  1801. {
  1802. resp.setRetcode(-1);
  1803. resp.setRetmsg("unknown error");
  1804. return false;
  1805. }
  1806. resp.setRetcode(0);
  1807. }
  1808. catch(IException* e)
  1809. {
  1810. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1811. }
  1812. return true;
  1813. }
  1814. bool Cws_accessEx::onResourceDelete(IEspContext &context, IEspResourceDeleteRequest &req, IEspResourceDeleteResponse &resp)
  1815. {
  1816. try
  1817. {
  1818. Owned<IEspDnStruct> basednReq = createDnStruct();
  1819. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  1820. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Full);
  1821. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  1822. if(secmgr == NULL)
  1823. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1824. StringArray& names = req.getNames();
  1825. int doUpdate = req.getDoUpdate();
  1826. if (doUpdate)
  1827. {
  1828. const char* basedn = basednReq->getBasedn();
  1829. const char* rtype = basednReq->getRtype();
  1830. const char* rtitle = basednReq->getRtitle();
  1831. const char* prefix = req.getPrefix();
  1832. StringBuffer url("/ws_access/PermissionsResetInput");
  1833. url.appendf("?basedn=%s", basedn);
  1834. url.appendf("&rtype=%s", rtype);
  1835. url.appendf("&rtitle=%s", rtitle);
  1836. url.appendf("&prefix=%s", prefix);
  1837. if (names.length() < 1)
  1838. throw MakeStringException(ECLWATCH_INVALID_RESOURCE_NAME, "Please select a resource name.");
  1839. for(unsigned i = 0; i < names.length(); i++)
  1840. {
  1841. const char* name = names.item(i);
  1842. if(name == NULL || *name == '\0')
  1843. continue;
  1844. url.appendf("&names_i%d=%s", i, name);
  1845. }
  1846. resp.setRedirectUrl(url);
  1847. return true;
  1848. }
  1849. double version = context.getClientVersion();
  1850. if (version < 1.14)
  1851. {
  1852. resp.setBasedn(basednReq->getBasedn());
  1853. resp.setRtype(basednReq->getRtype());
  1854. resp.setRtitle(basednReq->getRtitle());
  1855. resp.setPrefix(req.getPrefix());
  1856. }
  1857. SecResourceType rtype = str2type(basednReq->getRtype());
  1858. try
  1859. {
  1860. for(unsigned i = 0; i < names.length(); i++)
  1861. {
  1862. const char* name = names.item(i);
  1863. if(name == NULL || *name == '\0')
  1864. continue;
  1865. StringBuffer namebuf(name);
  1866. if(rtype == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  1867. namebuf.insert(0, "repository.");
  1868. const char* prefix = req.getPrefix();
  1869. if(prefix && *prefix)
  1870. namebuf.insert(0, prefix);
  1871. secmgr->deleteResource(rtype, namebuf.str(), basednReq->getBasedn());
  1872. }
  1873. }
  1874. catch(IException* e)
  1875. {
  1876. StringBuffer emsg;
  1877. e->errorMessage(emsg);
  1878. resp.setRetcode(e->errorCode());
  1879. resp.setRetmsg(emsg.str());
  1880. return false;
  1881. }
  1882. catch(...)
  1883. {
  1884. resp.setRetcode(-1);
  1885. resp.setRetmsg("Unknown error");
  1886. return false;
  1887. }
  1888. resp.setRetcode(0);
  1889. }
  1890. catch(IException* e)
  1891. {
  1892. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1893. }
  1894. return true;
  1895. }
  1896. void Cws_accessEx::addResourcePermission(const char *name, int type, int allows, int denies, IArrayOf<IEspResourcePermission> &permissions)
  1897. {
  1898. if (isEmptyString(name))
  1899. return;
  1900. StringBuffer nameIn(name);
  1901. Owned<IEspResourcePermission> permission = createResourcePermission();
  1902. permission->setAccount_name(name);
  1903. permission->setEscaped_account_name(nameIn.replaceString("\'", "\\\'").str());
  1904. permission->setAccount_type(type);
  1905. if((allows & NewSecAccess_Access) == NewSecAccess_Access)
  1906. permission->setAllow_access(true);
  1907. if((allows & NewSecAccess_Read) == NewSecAccess_Read)
  1908. permission->setAllow_read(true);
  1909. if((allows & NewSecAccess_Write) == NewSecAccess_Write)
  1910. permission->setAllow_write(true);
  1911. if((allows & NewSecAccess_Full) == NewSecAccess_Full)
  1912. permission->setAllow_full(true);
  1913. if((denies & NewSecAccess_Access) == NewSecAccess_Access)
  1914. permission->setDeny_access(true);
  1915. if((denies & NewSecAccess_Read) == NewSecAccess_Read)
  1916. permission->setDeny_read(true);
  1917. if((denies & NewSecAccess_Write) == NewSecAccess_Write)
  1918. permission->setDeny_write(true);
  1919. if((denies & NewSecAccess_Full) == NewSecAccess_Full)
  1920. permission->setDeny_full(true);
  1921. permissions.append(*permission.getClear());
  1922. }
  1923. bool Cws_accessEx::onResourcePermissions(IEspContext &context, IEspResourcePermissionsRequest &req, IEspResourcePermissionsResponse &resp)
  1924. {
  1925. try
  1926. {
  1927. Owned<IEspDnStruct> basednReq = createDnStruct();
  1928. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  1929. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Read);
  1930. ISecManager* secmgr = context.querySecManager();
  1931. if(secmgr == NULL)
  1932. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1933. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  1934. double version = context.getClientVersion();
  1935. SecResourceType rtype = str2type(basednReq->getRtype());
  1936. const char* name = req.getName();
  1937. StringBuffer namebuf(name);
  1938. if (rtype == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  1939. namebuf.insert(0, "repository.");
  1940. const char* basedn = basednReq->getBasedn();
  1941. if (isEmptyString(name) && (rtype == RT_FILE_SCOPE || rtype == RT_WORKUNIT_SCOPE))
  1942. { //Since resource name is not specified, this is the request to check default resource permissions for file
  1943. //scope or workunit scope. We need to parse file scope basedn (example: ou=files,ou=ecl,dc=dev,dc=local)
  1944. //or workunit scope basedn (example: ou=workunits,ou=ecl,dc=dev,dc=local) to get the resource name
  1945. //(example: files or workunits) and its basedn (example: ou=ecl,dc=dev,dc=local).
  1946. const char* comma = strchr(basedn, ',');
  1947. const char* eqsign = strchr(basedn, '=');
  1948. if (!comma || !eqsign)
  1949. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, "Invalid basedn: %s", basedn);
  1950. namebuf.clear().append(comma - eqsign - 1, eqsign + 1);
  1951. basedn = comma + 1;
  1952. }
  1953. const char* prefix = req.getPrefix();
  1954. if(prefix && *prefix)
  1955. namebuf.insert(0, prefix);
  1956. IArrayOf<CPermission> permissions;
  1957. ldapsecmgr->getPermissionsArray(basedn, rtype, namebuf, permissions);
  1958. IArrayOf<IEspResourcePermission> parray;
  1959. ForEachItemIn(x, permissions)
  1960. {
  1961. CPermission& perm = permissions.item(x);
  1962. addResourcePermission(perm.getAccount_name(), perm.getAccount_type(), perm.getAllows(), perm.getDenies(), parray);
  1963. }
  1964. if (version < 1.14)
  1965. {
  1966. resp.setBasedn(basednReq->getBasedn());
  1967. resp.setRtype(basednReq->getRtype());
  1968. resp.setRtitle(basednReq->getRtitle());
  1969. resp.setPrefix(req.getPrefix());
  1970. resp.setName(req.getName());
  1971. }
  1972. resp.setPermissions(parray);
  1973. }
  1974. catch(IException* e)
  1975. {
  1976. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1977. }
  1978. return true;
  1979. }
  1980. bool Cws_accessEx::onResourcePermissionQuery(IEspContext &context, IEspResourcePermissionQueryRequest &req, IEspResourcePermissionQueryResponse &resp)
  1981. {
  1982. try
  1983. {
  1984. CLdapSecManager* ldapSecMgr = queryLDAPSecurityManager(context);
  1985. if(!ldapSecMgr)
  1986. {
  1987. resp.setNoSecMngr(true);
  1988. return true;
  1989. }
  1990. Owned<IEspDnStruct> basednReq = createDnStruct();
  1991. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  1992. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Read);
  1993. __int64 pageStartFrom = 0;
  1994. unsigned pageSize = 100;
  1995. if (!req.getPageSize_isNull())
  1996. pageSize = req.getPageSize();
  1997. if (!req.getPageStartFrom_isNull())
  1998. pageStartFrom = req.getPageStartFrom();
  1999. ResourcePermissionField sortOrder[2] = {RPFName, RPFterm};
  2000. if (req.getSortBy() == CResourcePermissionSortBy_Type)
  2001. sortOrder[0] = RPFType;
  2002. sortOrder[0] = (ResourcePermissionField) (sortOrder[0] | RPFnocase);
  2003. bool descending = req.getDescending();
  2004. if (descending)
  2005. sortOrder[0] = (ResourcePermissionField) (sortOrder[0] | RPFreverse);
  2006. ACCOUNT_TYPE_REQ accountTypeReq = REQ_ANY_ACT;
  2007. CAccountTypeReq accountType = req.getAccountType();
  2008. switch(accountType)
  2009. {
  2010. case CAccountTypeReq_User:
  2011. accountTypeReq = REQ_USER_ACT;
  2012. break;
  2013. case CAccountTypeReq_Group:
  2014. accountTypeReq = REQ_GROUP_ACT;
  2015. break;
  2016. }
  2017. unsigned total;
  2018. __int64 cacheHint;
  2019. IArrayOf<IEspResourcePermission> permissions;
  2020. Owned<ISecItemIterator> it = ldapSecMgr->getResourcePermissionsSorted(req.getName(), accountTypeReq, basednReq->getBasedn(),
  2021. basednReq->getRtype(), req.getPrefix(), sortOrder, (const __int64) pageStartFrom, (const unsigned) pageSize, &total, &cacheHint);
  2022. ForEach(*it)
  2023. {
  2024. IPropertyTree& r = it->query();
  2025. addResourcePermission(r.queryProp(getResourcePermissionFieldNames(RPFName)), r.getPropInt(getResourcePermissionFieldNames(RPFType)),
  2026. r.getPropInt(getResourcePermissionFieldNames(RPFAllow)), r.getPropInt(getResourcePermissionFieldNames(RPFDeny)), permissions);
  2027. }
  2028. resp.setPermissions(permissions);
  2029. resp.setTotalResourcePermissions(total);
  2030. resp.setCacheHint(cacheHint);
  2031. }
  2032. catch(IException* e)
  2033. {
  2034. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2035. }
  2036. return true;
  2037. }
  2038. bool Cws_accessEx::onQueryViews(IEspContext &context, IEspQueryViewsRequest &req, IEspQueryViewsResponse &resp)
  2039. {
  2040. try
  2041. {
  2042. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2043. if(secmgr == NULL)
  2044. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2045. checkUser(context);
  2046. IArrayOf<IEspView> views;
  2047. StringArray names, descriptions, viewManagedBy;
  2048. secmgr->queryAllViews(names, descriptions, viewManagedBy);
  2049. ForEachItemIn(i, names)
  2050. {
  2051. Owned<IEspView> oneView = createView();
  2052. oneView->setViewname(names.item(i));
  2053. oneView->setDescription(descriptions.item(i));
  2054. views.append(*oneView.getLink());
  2055. }
  2056. resp.setViews(views);
  2057. }
  2058. catch (IException* e)
  2059. {
  2060. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2061. }
  2062. return true;
  2063. }
  2064. bool Cws_accessEx::onAddView(IEspContext &context, IEspAddViewRequest &req, IEspAddViewResponse &resp)
  2065. {
  2066. try
  2067. {
  2068. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2069. if(secmgr == NULL)
  2070. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2071. checkUser(context);
  2072. const char* viewname = req.getViewname();
  2073. const char* description = req.getDescription();
  2074. secmgr->createView(viewname, description);
  2075. resp.setViewname(viewname);
  2076. resp.setDescription(description);
  2077. }
  2078. catch (IException* e)
  2079. {
  2080. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2081. }
  2082. return true;
  2083. }
  2084. bool Cws_accessEx::onDeleteView(IEspContext &context, IEspDeleteViewRequest &req, IEspDeleteViewResponse &resp)
  2085. {
  2086. try
  2087. {
  2088. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2089. if(secmgr == NULL)
  2090. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2091. checkUser(context);
  2092. const char* viewname = req.getViewname();
  2093. secmgr->deleteView(req.getViewname());
  2094. resp.setViewname(viewname);
  2095. }
  2096. catch (IException* e)
  2097. {
  2098. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2099. }
  2100. return true;
  2101. }
  2102. bool Cws_accessEx::onQueryViewColumns(IEspContext &context, IEspQueryViewColumnsRequest &req, IEspQueryViewColumnsResponse &resp)
  2103. {
  2104. try
  2105. {
  2106. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2107. if(secmgr == NULL)
  2108. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2109. checkUser(context);
  2110. IArrayOf<IEspViewColumn> viewColumns;
  2111. StringArray files, columns;
  2112. const char* viewname = req.getViewname();
  2113. secmgr->queryViewColumns(viewname, files, columns);
  2114. ForEachItemIn(i, files)
  2115. {
  2116. Owned<IEspViewColumn> oneViewColumn = createViewColumn();
  2117. oneViewColumn->setViewname(req.getViewname());
  2118. oneViewColumn->setFilename(files.item(i));
  2119. oneViewColumn->setColumnname(columns.item(i));
  2120. viewColumns.append(*oneViewColumn.getLink());
  2121. }
  2122. resp.setViewname(viewname);
  2123. resp.setViewcolumns(viewColumns);
  2124. }
  2125. catch (IException* e)
  2126. {
  2127. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2128. }
  2129. return true;
  2130. }
  2131. bool Cws_accessEx::onAddViewColumn(IEspContext &context, IEspAddViewColumnRequest &req, IEspAddViewColumnResponse &resp)
  2132. {
  2133. try
  2134. {
  2135. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2136. if(secmgr == NULL)
  2137. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2138. checkUser(context);
  2139. const char* filename = req.getFilename();
  2140. const char* columnname = req.getColumnname();
  2141. if (!filename || *filename == '\0')
  2142. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Filename cannot be empty.");
  2143. if (!columnname || *columnname == '\0')
  2144. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Columnname cannot be empty.");
  2145. // View column filename MUST be a full path including the scope, with a leading tilde (~)
  2146. if (filename[0] != '~')
  2147. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Filename must include a scope name, with a leading tilde (~)");
  2148. StringArray files, columns;
  2149. const char* viewname = req.getViewname();
  2150. files.append(filename);
  2151. columns.append(columnname);
  2152. secmgr->addViewColumns(viewname, files, columns);
  2153. resp.setViewname(viewname);
  2154. resp.setFilename(filename);
  2155. resp.setColumnname(columnname);
  2156. }
  2157. catch (IException* e)
  2158. {
  2159. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2160. }
  2161. return true;
  2162. }
  2163. bool Cws_accessEx::onDeleteViewColumn(IEspContext &context, IEspDeleteViewColumnRequest &req, IEspDeleteViewColumnResponse &resp)
  2164. {
  2165. try
  2166. {
  2167. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2168. if(secmgr == NULL)
  2169. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2170. checkUser(context);
  2171. StringArray files, columns;
  2172. const char* viewname = req.getViewname();
  2173. const char* filename = req.getFilename();
  2174. const char* columnname = req.getColumnname();
  2175. files.append(filename);
  2176. columns.append(columnname);
  2177. secmgr->removeViewColumns(req.getViewname(), files, columns);
  2178. resp.setViewname(viewname);
  2179. resp.setFilename(filename);
  2180. resp.setColumnname(columnname);
  2181. }
  2182. catch (IException* e)
  2183. {
  2184. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2185. }
  2186. return true;
  2187. }
  2188. bool Cws_accessEx::onQueryViewMembers(IEspContext &context, IEspQueryViewMembersRequest &req, IEspQueryViewMembersResponse &resp)
  2189. {
  2190. try
  2191. {
  2192. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2193. if(secmgr == NULL)
  2194. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2195. checkUser(context);
  2196. const char* reqViewname = req.getViewname();
  2197. StringArray users, groups;
  2198. IArrayOf<IEspViewMember> viewMembers;
  2199. secmgr->queryViewMembers(reqViewname, users, groups);
  2200. ForEachItemIn(i, users)
  2201. {
  2202. Owned<IEspViewMember> oneViewMember = createViewMember();
  2203. oneViewMember->setViewname(reqViewname);
  2204. oneViewMember->setName(users.item(i));
  2205. oneViewMember->setMembertype(CViewMemberType_User);
  2206. viewMembers.append(*oneViewMember.getLink());
  2207. }
  2208. ForEachItemIn(j, groups)
  2209. {
  2210. Owned<IEspViewMember> oneViewMember = createViewMember();
  2211. oneViewMember->setViewname(reqViewname);
  2212. oneViewMember->setName(groups.item(j));
  2213. oneViewMember->setMembertype(CViewMemberType_Group);
  2214. viewMembers.append(*oneViewMember.getLink());
  2215. }
  2216. resp.setViewname(reqViewname);
  2217. resp.setViewmembers(viewMembers);
  2218. }
  2219. catch (IException* e)
  2220. {
  2221. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2222. }
  2223. return true;
  2224. }
  2225. bool Cws_accessEx::onAddViewMember(IEspContext &context, IEspAddViewMemberRequest &req, IEspAddViewMemberResponse &resp)
  2226. {
  2227. try
  2228. {
  2229. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2230. if(secmgr == NULL)
  2231. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2232. checkUser(context);
  2233. StringArray users, groups;
  2234. const char* viewname = req.getViewname();
  2235. const char* membername = req.getMembername();
  2236. CViewMemberType membertype = req.getMembertype();
  2237. if (membertype == CViewMemberType_User)
  2238. {
  2239. users.append(membername);
  2240. }
  2241. else if (membertype == CViewMemberType_Group)
  2242. {
  2243. groups.append(membername);
  2244. }
  2245. else
  2246. {
  2247. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Unknown view member type specified (Must be User or Group)");
  2248. }
  2249. secmgr->addViewMembers(viewname, users, groups);
  2250. resp.setViewname(viewname);
  2251. resp.setMembername(membername);
  2252. resp.setMembertype(membertype);
  2253. }
  2254. catch (IException* e)
  2255. {
  2256. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2257. }
  2258. return true;
  2259. }
  2260. bool Cws_accessEx::onDeleteViewMember(IEspContext &context, IEspDeleteViewMemberRequest &req, IEspDeleteViewMemberResponse &resp)
  2261. {
  2262. try
  2263. {
  2264. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2265. if(secmgr == NULL)
  2266. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2267. checkUser(context);
  2268. StringArray users, groups;
  2269. const char* viewname = req.getViewname();
  2270. const char* membername = req.getMembername();
  2271. CViewMemberType membertype = req.getMembertype();
  2272. if (membertype == CViewMemberType_User)
  2273. {
  2274. users.append(membername);
  2275. }
  2276. else if (membertype == CViewMemberType_Group)
  2277. {
  2278. groups.append(membername);
  2279. }
  2280. else
  2281. {
  2282. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Unknown view member type specified (Must be User' or Group)");
  2283. }
  2284. secmgr->removeViewMembers(req.getViewname(), users, groups);
  2285. resp.setViewname(viewname);
  2286. resp.setMembername(membername);
  2287. resp.setMembertype(membertype);
  2288. }
  2289. catch (IException* e)
  2290. {
  2291. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2292. }
  2293. return true;
  2294. }
  2295. bool Cws_accessEx::onQueryUserViewColumns(IEspContext &context, IEspQueryUserViewColumnsRequest &req, IEspQueryUserViewColumnsResponse &resp)
  2296. {
  2297. try
  2298. {
  2299. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  2300. if(secmgr == NULL)
  2301. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2302. checkUser(context);
  2303. const char* username = req.getUsername();
  2304. IArrayOf<IEspViewColumn> viewColumns;
  2305. StringArray viewnames, viewdescriptions, viewManagedBy;
  2306. secmgr->queryAllViews(viewnames, viewdescriptions, viewManagedBy);
  2307. ForEachItemIn(i, viewnames)
  2308. {
  2309. const char* viewname = viewnames.item(i);
  2310. if (secmgr->userInView(username, viewname))
  2311. {
  2312. StringArray files, columns;
  2313. secmgr->queryViewColumns(viewname, files, columns);
  2314. ForEachItemIn(j, files)
  2315. {
  2316. Owned<IEspViewColumn> oneViewColumn = createViewColumn();
  2317. oneViewColumn->setViewname(viewname);
  2318. oneViewColumn->setFilename(files.item(j));
  2319. oneViewColumn->setColumnname(columns.item(j));
  2320. viewColumns.append(*oneViewColumn.getLink());
  2321. }
  2322. }
  2323. }
  2324. resp.setUsername(username);
  2325. resp.setViewcolumns(viewColumns);
  2326. }
  2327. catch (IException* e)
  2328. {
  2329. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2330. }
  2331. return true;
  2332. }
  2333. bool Cws_accessEx::onPermissionAddInput(IEspContext &context, IEspPermissionAddRequest &req, IEspPermissionAddResponse &resp)
  2334. {
  2335. try
  2336. {
  2337. checkUser(context, req.getRtype(), req.getRtitle(), SecAccess_Full);
  2338. resp.setBasedn(req.getBasedn());
  2339. resp.setRname(req.getRname());
  2340. resp.setRtype(req.getRtype());
  2341. resp.setRtitle(req.getRtitle());
  2342. resp.setPrefix(req.getPrefix());
  2343. double version = context.getClientVersion();
  2344. if (version < 1.01)
  2345. {
  2346. return permissionAddInputOnResource(context, req, resp);
  2347. }
  2348. else
  2349. {
  2350. const char* accountName = req.getAccountName();
  2351. if (!accountName || !*accountName)
  2352. {
  2353. return permissionAddInputOnResource(context, req, resp);
  2354. }
  2355. else
  2356. {
  2357. return permissionAddInputOnAccount(context, accountName, req, resp);
  2358. }
  2359. }
  2360. }
  2361. catch(IException* e)
  2362. {
  2363. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2364. }
  2365. return true;
  2366. }
  2367. bool Cws_accessEx::onPermissionsResetInput(IEspContext &context, IEspPermissionsResetInputRequest &req, IEspPermissionsResetInputResponse &resp)
  2368. {
  2369. try
  2370. {
  2371. checkUser(context, req.getRtype(), req.getRtitle(), SecAccess_Full);
  2372. resp.setBasedn(req.getBasedn());
  2373. //resp.setRname(req.getRname());
  2374. resp.setRname("Test");
  2375. resp.setRtype(req.getRtype());
  2376. resp.setRtitle(req.getRtitle());
  2377. resp.setPrefix(req.getPrefix());
  2378. StringArray& names = req.getNames();
  2379. if (names.length() < 1)
  2380. throw MakeStringException(ECLWATCH_INVALID_PERMISSION_NAME, "Please select a permission.");
  2381. StringBuffer nameList; //For forwarding to Submit page
  2382. StringArray names1;
  2383. ForEachItemIn(k, names)
  2384. {
  2385. const char* name1 = names.item(k);
  2386. nameList.appendf("%s,", name1);
  2387. names1.append(name1);
  2388. }
  2389. resp.setResourceList(nameList.str());
  2390. resp.setResources(names);
  2391. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2392. if(secmgr == NULL)
  2393. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2394. int numusers = secmgr->countUsers("", MAX_USERS_DISPLAY);
  2395. if(numusers == -1)
  2396. {
  2397. resp.setToomany(true);
  2398. }
  2399. else
  2400. {
  2401. resp.setToomany(false);
  2402. IArrayOf<IEspUserInfo> espusers;
  2403. IUserArray users;
  2404. secmgr->getAllUsers(users);
  2405. ForEachItemIn(x, users)
  2406. {
  2407. CLdapSecUser* usr = dynamic_cast<CLdapSecUser*>(&users.item(x));
  2408. if(usr)
  2409. {
  2410. Owned<IEspUserInfo> oneusr = createUserInfo();
  2411. oneusr->setUsername(usr->getName());
  2412. oneusr->setFullname(usr->getFullName());
  2413. espusers.append(*oneusr.getLink());
  2414. }
  2415. }
  2416. resp.setUsers(espusers);
  2417. }
  2418. IArrayOf<IEspGroupInfo> groups;
  2419. if(secmgr->getLdapServerType() != ACTIVE_DIRECTORY)
  2420. {
  2421. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  2422. onegrp->setName("anyone");
  2423. groups.append(*onegrp.getLink());
  2424. }
  2425. StringArray grpnames;
  2426. StringArray managedBy;
  2427. StringArray descriptions;
  2428. secmgr->getAllGroups(grpnames, managedBy, descriptions);
  2429. for(unsigned i = 0; i < grpnames.length(); i++)
  2430. {
  2431. const char* grpname = grpnames.item(i);
  2432. if(grpname == NULL || *grpname == '\0')
  2433. continue;
  2434. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  2435. onegrp->setName(grpname);
  2436. onegrp->setGroupDesc(descriptions.item(i));
  2437. onegrp->setGroupOwner(managedBy.item(i));
  2438. groups.append(*onegrp.getLink());
  2439. }
  2440. resp.setGroups(groups);
  2441. }
  2442. catch(IException* e)
  2443. {
  2444. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2445. }
  2446. return true;
  2447. }
  2448. bool Cws_accessEx::onClearPermissionsCache(IEspContext &context, IEspClearPermissionsCacheRequest &req, IEspClearPermissionsCacheResponse &resp)
  2449. {
  2450. checkUser(context);
  2451. ISecManager* secmgr = context.querySecManager();
  2452. if(secmgr == NULL)
  2453. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2454. //Clear local cache
  2455. Owned<ISecUser> user = secmgr->createUser(context.queryUserId());
  2456. ISecCredentials& cred = user->credentials();
  2457. cred.setPassword(context.queryPassword());
  2458. cred.setSessionToken(context.querySessionToken());
  2459. bool ok = secmgr->clearPermissionsCache(*user);
  2460. //Request DALI to clear its cache
  2461. if (ok)
  2462. {
  2463. Owned<IUserDescriptor> userdesc;
  2464. userdesc.setown(createUserDescriptor());
  2465. userdesc->set(context.queryUserId(), context.queryPassword(), context.querySignature());
  2466. ok = querySessionManager().clearPermissionsCache(userdesc);
  2467. }
  2468. resp.setRetcode(ok ? 0 : -1);
  2469. return true;
  2470. }
  2471. bool Cws_accessEx::onQueryScopeScansEnabled(IEspContext &context, IEspQueryScopeScansEnabledRequest &req, IEspQueryScopeScansEnabledResponse &resp)
  2472. {
  2473. ISecManager* secmgr = context.querySecManager();
  2474. if(secmgr == NULL)
  2475. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2476. Owned<IUserDescriptor> userdesc;
  2477. userdesc.setown(createUserDescriptor());
  2478. userdesc->set(context.queryUserId(), context.queryPassword(), context.querySignature());
  2479. int retCode;
  2480. StringBuffer retMsg;
  2481. bool isEnabled = querySessionManager().queryScopeScansEnabled(userdesc, &retCode, retMsg);
  2482. if (retCode != 0)
  2483. throw MakeStringException(ECLWATCH_OLD_CLIENT_VERSION, "Error %d querying scope scan status : %s", retCode, retMsg.str());
  2484. resp.updateScopeScansStatus().setIsEnabled(isEnabled);
  2485. resp.updateScopeScansStatus().setRetcode(retCode);
  2486. resp.updateScopeScansStatus().setRetmsg(retMsg.str());
  2487. return true;
  2488. }
  2489. bool Cws_accessEx::onEnableScopeScans(IEspContext &context, IEspEnableScopeScansRequest &req, IEspEnableScopeScansResponse &resp)
  2490. {
  2491. checkUser(context, FILE_SCOPE_RTYPE, FILE_SCOPE_RTITLE, SecAccess_Full);
  2492. StringBuffer retMsg;
  2493. int rc = enableDisableScopeScans(context, true, retMsg);
  2494. resp.updateScopeScansStatus().setIsEnabled(rc == 0);
  2495. resp.updateScopeScansStatus().setRetcode(rc);
  2496. resp.updateScopeScansStatus().setRetmsg(retMsg.str());
  2497. return true;
  2498. }
  2499. bool Cws_accessEx::onDisableScopeScans(IEspContext &context, IEspDisableScopeScansRequest &req, IEspDisableScopeScansResponse &resp)
  2500. {
  2501. checkUser(context, FILE_SCOPE_RTYPE, FILE_SCOPE_RTITLE, SecAccess_Full);
  2502. StringBuffer retMsg;
  2503. int rc = enableDisableScopeScans(context, false, retMsg);
  2504. resp.updateScopeScansStatus().setIsEnabled(rc != 0);
  2505. resp.updateScopeScansStatus().setRetcode(rc);
  2506. resp.updateScopeScansStatus().setRetmsg(retMsg.str());
  2507. return true;
  2508. }
  2509. int Cws_accessEx::enableDisableScopeScans(IEspContext &context, bool doEnable, StringBuffer &retMsg)
  2510. {
  2511. ISecManager* secmgr = context.querySecManager();
  2512. if(secmgr == NULL)
  2513. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2514. Owned<IUserDescriptor> userdesc;
  2515. userdesc.setown(createUserDescriptor());
  2516. userdesc->set(context.queryUserId(), context.queryPassword(), context.querySignature());
  2517. int retCode;
  2518. bool rc = querySessionManager().enableScopeScans(userdesc, doEnable, &retCode, retMsg);
  2519. if (!rc || retCode != 0)
  2520. IERRLOG("Error %d enabling Scope Scans : %s", retCode, retMsg.str());
  2521. return retCode;
  2522. }
  2523. bool Cws_accessEx::permissionsReset(CLdapSecManager* ldapsecmgr, const char* basedn, const char* rtype0, const char* prefix,
  2524. const char* resourceName, ACT_TYPE accountType, const char* accountName,
  2525. bool allow_access, bool allow_read, bool allow_write, bool allow_full,
  2526. bool deny_access, bool deny_read, bool deny_write, bool deny_full)
  2527. {
  2528. CPermissionAction paction;
  2529. paction.m_basedn.append(basedn);
  2530. //const char* name = req.getRname();
  2531. StringBuffer namebuf(resourceName);
  2532. SecResourceType rtype = str2type(rtype0);
  2533. if(rtype == RT_MODULE && stricmp(resourceName, "repository") != 0 && Utils::strncasecmp(resourceName, "repository.", 11) != 0)
  2534. namebuf.insert(0, "repository.");
  2535. if(prefix && *prefix)
  2536. namebuf.insert(0, prefix);
  2537. paction.m_rname.append(namebuf.str());
  2538. paction.m_rtype = str2type(rtype0);
  2539. paction.m_allows = 0;
  2540. paction.m_denies = 0;
  2541. if(allow_full)
  2542. paction.m_allows |= NewSecAccess_Full;
  2543. if(allow_read)
  2544. paction.m_allows |= NewSecAccess_Read;
  2545. if(allow_write)
  2546. paction.m_allows |= NewSecAccess_Write;
  2547. if(allow_access)
  2548. paction.m_allows |= NewSecAccess_Access;
  2549. if(deny_full)
  2550. paction.m_denies |= NewSecAccess_Full;
  2551. if(deny_read)
  2552. paction.m_denies |= NewSecAccess_Read;
  2553. if(deny_write)
  2554. paction.m_denies |= NewSecAccess_Write;
  2555. if(deny_access)
  2556. paction.m_denies |= NewSecAccess_Access;
  2557. paction.m_action.append("update");
  2558. paction.m_account_type = accountType;
  2559. paction.m_account_name.append(accountName);
  2560. bool ret = ldapsecmgr->changePermission(paction);
  2561. return ret;
  2562. }
  2563. bool Cws_accessEx::onPermissionsReset(IEspContext &context, IEspPermissionsResetRequest &req, IEspPermissionsResetResponse &resp)
  2564. {
  2565. try
  2566. {
  2567. Owned<IEspDnStruct> basednReq = createDnStruct();
  2568. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  2569. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Full);
  2570. double version = context.getClientVersion();
  2571. if (version < 1.14)
  2572. {
  2573. resp.setBasedn(basednReq->getBasedn());
  2574. resp.setRname(req.getRname());
  2575. resp.setRtype(basednReq->getRtype());
  2576. resp.setRtitle(basednReq->getRtitle());
  2577. resp.setPrefix(req.getPrefix());
  2578. }
  2579. ISecManager* secmgr = context.querySecManager();
  2580. if(secmgr == NULL)
  2581. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2582. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  2583. const char* users = req.getUserarray();
  2584. const char* groups = req.getGrouparray();
  2585. if ((!users || !*users) && (!groups || !*groups))
  2586. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "A user or group must be specified.");
  2587. StringArray& resources = req.getNames();
  2588. if (resources.length() < 1)
  2589. throw MakeStringException(ECLWATCH_INVALID_RESOURCE_NAME, "A resource name must be specified.");
  2590. StringArray userAccounts, groupAccounts;
  2591. if (!isEmptyString(users))
  2592. userAccounts.appendListUniq(users, ",");
  2593. if (!isEmptyString(groups))
  2594. groupAccounts.appendListUniq(groups, ",");
  2595. if (userAccounts.length() < 1 && groupAccounts.length() < 1)
  2596. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "A user or group must be specified.");
  2597. for(unsigned i = 0; i < resources.length(); i++)
  2598. {
  2599. const char* name = resources.item(i);
  2600. if (!name || !*name)
  2601. continue;
  2602. bool ret = true;
  2603. StringBuffer retmsg;
  2604. try
  2605. {
  2606. if (userAccounts.length() > 0)
  2607. {
  2608. for(unsigned j = 0; j < userAccounts.length(); j++)
  2609. {
  2610. const char* name0 = userAccounts.item(j);
  2611. if (!name0 || !*name0)
  2612. continue;
  2613. ret = permissionsReset(ldapsecmgr, basednReq->getBasedn(), basednReq->getRtype(), req.getPrefix(), name, USER_ACT, name0,
  2614. req.getAllow_access(), req.getAllow_read(), req.getAllow_write(), req.getAllow_full(),
  2615. req.getDeny_access(), req.getDeny_read(), req.getDeny_write(), req.getDeny_full());
  2616. if(!ret)
  2617. {
  2618. resp.setRetcode(-1);
  2619. resp.setRetmsg("Unknown error");
  2620. return false;
  2621. }
  2622. }
  2623. }
  2624. if (groupAccounts.length() > 0)
  2625. {
  2626. for(unsigned j = 0; j < groupAccounts.length(); j++)
  2627. {
  2628. const char* name0 = groupAccounts.item(j);
  2629. if (!name0 || !*name0)
  2630. continue;
  2631. ret = permissionsReset(ldapsecmgr, basednReq->getBasedn(), basednReq->getRtype(), req.getPrefix(), name, GROUP_ACT, name0,
  2632. req.getAllow_access(), req.getAllow_read(), req.getAllow_write(), req.getAllow_full(),
  2633. req.getDeny_access(), req.getDeny_read(), req.getDeny_write(), req.getDeny_full());
  2634. if(!ret)
  2635. {
  2636. resp.setRetcode(-1);
  2637. resp.setRetmsg("Unknown error");
  2638. return false;
  2639. }
  2640. }
  2641. }
  2642. }
  2643. catch(IException* e)
  2644. {
  2645. resp.setRetcode(e->errorCode());
  2646. e->errorMessage(retmsg);
  2647. resp.setRetmsg(retmsg.str());
  2648. return false;
  2649. }
  2650. }
  2651. resp.setRetcode(0);
  2652. }
  2653. catch(IException* e)
  2654. {
  2655. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2656. }
  2657. return true;
  2658. }
  2659. //For every resources inside a baseDN, if there is no permission for this account, add the baseDN name to the basednNames list
  2660. void Cws_accessEx::getBaseDNsForAddingPermssionToAccount(CLdapSecManager* secmgr, const char* prefix, const char* accountName,
  2661. int accountType, StringArray& basednNames)
  2662. {
  2663. if(secmgr == NULL)
  2664. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2665. ForEachItemIn(i, m_basedns)
  2666. {
  2667. IEspDnStruct* curbasedn = &(m_basedns.item(i));
  2668. const char *basednName = curbasedn->getName();
  2669. if (!basednName || !*basednName)
  2670. continue;
  2671. const char *basedn = curbasedn->getBasedn();
  2672. const char *rtypestr = curbasedn->getRtype();
  2673. if (!basedn || !*basedn || !rtypestr || !*rtypestr)
  2674. continue;
  2675. IArrayOf<ISecResource> resources;
  2676. SecResourceType rtype = str2type(rtypestr);
  2677. if(!secmgr->getResources(rtype, basedn, resources))
  2678. continue;
  2679. ForEachItemIn(j, resources)
  2680. {
  2681. ISecResource& r = resources.item(j);
  2682. const char* rname = r.getName();
  2683. if(!rname || !*rname)
  2684. continue;
  2685. if(prefix && *prefix)
  2686. {
  2687. int prefixlen = strlen(prefix);
  2688. if(strncmp(prefix, rname, prefixlen) == 0)
  2689. rname += prefixlen;
  2690. }
  2691. StringBuffer namebuf(rname);
  2692. if((rtype == RT_MODULE) && !strieq(rname, "repository") && Utils::strncasecmp(rname, "repository.", 11) != 0)
  2693. namebuf.insert(0, "repository.");
  2694. if(prefix && *prefix)
  2695. namebuf.insert(0, prefix);
  2696. try
  2697. {
  2698. IArrayOf<CPermission> permissions;
  2699. secmgr->getPermissionsArray(basedn, rtype, namebuf.str(), permissions);
  2700. bool foundPermissionInThisAccount = false;
  2701. ForEachItemIn(k, permissions)
  2702. {
  2703. CPermission& perm = permissions.item(k);
  2704. if ((accountType == perm.getAccount_type()) && perm.getAccount_name() && streq(perm.getAccount_name(), accountName))
  2705. {
  2706. foundPermissionInThisAccount = true;
  2707. break;
  2708. }
  2709. }
  2710. if (!foundPermissionInThisAccount)
  2711. {
  2712. basednNames.append(basednName);
  2713. break;
  2714. }
  2715. }
  2716. catch(IException* e) //exception may be thrown when no permission for the resource
  2717. {
  2718. e->Release();
  2719. break;
  2720. }
  2721. }
  2722. }
  2723. return;
  2724. }
  2725. bool Cws_accessEx::permissionAddInputOnResource(IEspContext &context, IEspPermissionAddRequest &req, IEspPermissionAddResponse &resp)
  2726. {
  2727. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2728. if(secmgr == NULL)
  2729. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2730. int numusers = secmgr->countUsers("", MAX_USERS_DISPLAY);
  2731. if(numusers == -1)
  2732. {
  2733. resp.setToomany(true);
  2734. }
  2735. else
  2736. {
  2737. resp.setToomany(false);
  2738. IArrayOf<IEspUserInfo> espusers;
  2739. IUserArray users;
  2740. secmgr->getAllUsers(users);
  2741. ForEachItemIn(x, users)
  2742. {
  2743. CLdapSecUser* usr = dynamic_cast<CLdapSecUser*>(&users.item(x));
  2744. if(usr)
  2745. {
  2746. Owned<IEspUserInfo> oneusr = createUserInfo();
  2747. oneusr->setUsername(usr->getName());
  2748. oneusr->setFullname(usr->getFullName());
  2749. espusers.append(*oneusr.getLink());
  2750. }
  2751. }
  2752. resp.setUsers(espusers);
  2753. }
  2754. IArrayOf<IEspGroupInfo> groups;
  2755. if(secmgr->getLdapServerType() != ACTIVE_DIRECTORY)
  2756. {
  2757. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  2758. onegrp->setName("anyone");
  2759. groups.append(*onegrp.getLink());
  2760. }
  2761. StringArray grpnames;
  2762. StringArray managedBy;
  2763. StringArray descriptions;
  2764. secmgr->getAllGroups(grpnames, managedBy, descriptions);
  2765. for(unsigned i = 0; i < grpnames.length(); i++)
  2766. {
  2767. const char* grpname = grpnames.item(i);
  2768. if(grpname == NULL || *grpname == '\0')
  2769. continue;
  2770. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  2771. onegrp->setName(grpname);
  2772. onegrp->setGroupDesc(descriptions.item(i));
  2773. onegrp->setGroupOwner(managedBy.item(i));
  2774. groups.append(*onegrp.getLink());
  2775. }
  2776. resp.setGroups(groups);
  2777. return true;
  2778. }
  2779. bool Cws_accessEx::permissionAddInputOnAccount(IEspContext &context, const char* accountName, IEspPermissionAddRequest &req, IEspPermissionAddResponse &resp)
  2780. {
  2781. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2782. if(secmgr == NULL)
  2783. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2784. double version = context.getClientVersion();
  2785. if (version < 1.14)
  2786. {
  2787. resp.setBasednName(req.getBasednName());
  2788. resp.setAccountName(req.getAccountName());
  2789. resp.setAccountType(req.getAccountType());
  2790. }
  2791. const char* prefix = req.getPrefix();
  2792. const char* basednName = req.getBasednName();
  2793. int accountType = req.getAccountType();
  2794. if (basednName && *basednName)
  2795. {
  2796. ForEachItemIn(y, m_basedns)
  2797. {
  2798. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  2799. const char *aName = curbasedn->getName();
  2800. if (!aName || stricmp(basednName, aName))
  2801. continue;
  2802. const char *basedn = curbasedn->getBasedn();
  2803. const char *rtypestr = curbasedn->getRtype();
  2804. if (!basedn || !*basedn || !rtypestr || !*rtypestr)
  2805. continue;
  2806. IArrayOf<ISecResource> resources;
  2807. SecResourceType rtype = str2type(rtypestr);
  2808. if(secmgr->getResources(rtype, basedn, resources))
  2809. {
  2810. StringArray resourcenames;
  2811. for(unsigned i = 0; i < resources.length(); i++)
  2812. {
  2813. ISecResource& r = resources.item(i);
  2814. const char* rname = r.getName();
  2815. if(rname == NULL || *rname == '\0')
  2816. continue;
  2817. if(prefix && *prefix)
  2818. {
  2819. int prefixlen = strlen(prefix);
  2820. if(strncmp(prefix, rname, prefixlen) == 0)
  2821. rname += prefixlen;
  2822. }
  2823. if((rtype == RT_MODULE) && stricmp(rname, "repository"))
  2824. {
  2825. if(Utils::strncasecmp(rname, "repository.", 11) == 0)
  2826. rname = rname + 11;
  2827. else
  2828. continue;
  2829. }
  2830. StringBuffer namebuf(rname);
  2831. if((rtype == RT_MODULE) && stricmp(rname, "repository") != 0 && Utils::strncasecmp(rname, "repository.", 11) != 0)
  2832. namebuf.insert(0, "repository.");
  2833. if(prefix && *prefix)
  2834. namebuf.insert(0, prefix);
  2835. try
  2836. {
  2837. IArrayOf<CPermission> permissions;
  2838. secmgr->getPermissionsArray(basedn, rtype, namebuf.str(), permissions);
  2839. bool found = false;
  2840. ForEachItemIn(x, permissions)
  2841. {
  2842. CPermission& perm = permissions.item(x);
  2843. const char* actname = perm.getAccount_name();
  2844. int accType = perm.getAccount_type(); //0-individual, 1 - group
  2845. if ((accountType == accType) && actname && !strcmp(actname, accountName))
  2846. {
  2847. found = true;
  2848. break;
  2849. }
  2850. }
  2851. if (!found)
  2852. resourcenames.append(rname);
  2853. }
  2854. catch(IException* e) //exception may be thrown when no permission for the resource
  2855. {
  2856. e->Release();
  2857. break;
  2858. }
  2859. }
  2860. if (resourcenames.length() > 0)
  2861. resp.setResources(resourcenames);
  2862. }
  2863. }
  2864. }
  2865. return true;
  2866. }
  2867. bool Cws_accessEx::onPermissionAction(IEspContext &context, IEspPermissionActionRequest &req, IEspPermissionActionResponse &resp)
  2868. {
  2869. try
  2870. {
  2871. Owned<IEspDnStruct> basednReq = createDnStruct();
  2872. getBasednReq(context, req.getBasednName(), req.getBasedn(), req.getRtype(), req.getRtitle(), basednReq);
  2873. checkUser(context, basednReq->getRtype(), basednReq->getRtitle(), SecAccess_Full);
  2874. double version = context.getClientVersion();
  2875. if (version < 1.14)
  2876. {
  2877. resp.setBasedn(req.getBasedn());
  2878. resp.setRname(req.getRname());
  2879. resp.setRtype(req.getRtype());
  2880. resp.setRtitle(req.getRtitle());
  2881. resp.setPrefix(req.getPrefix());
  2882. }
  2883. CLdapSecManager* ldapsecmgr = queryLDAPSecurityManager(context);
  2884. if(ldapsecmgr == NULL)
  2885. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2886. CPermissionAction paction;
  2887. paction.m_basedn.append(basednReq->getBasedn());
  2888. const char* name = req.getRname();
  2889. StringBuffer namebuf(name);
  2890. SecResourceType rtype = str2type(basednReq->getRtype());
  2891. if(rtype == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  2892. namebuf.insert(0, "repository.");
  2893. const char* prefix = req.getPrefix();
  2894. if(prefix && *prefix)
  2895. namebuf.insert(0, prefix);
  2896. paction.m_rname.append(namebuf.str());
  2897. paction.m_rtype = rtype;
  2898. paction.m_account_type = (ACT_TYPE)req.getAccount_type();
  2899. if(stricmp(req.getAction(), "add") == 0)
  2900. {
  2901. StringBuffer basednNameStr, resourceNameStr;
  2902. if (version >= 1.01)
  2903. {
  2904. const char* basedn_name = req.getBasednName();
  2905. const char* resource_name = req.getResourceName();
  2906. if (basedn_name && *basedn_name)
  2907. basednNameStr.append(basedn_name);
  2908. if (resource_name && *resource_name)
  2909. resourceNameStr.append(resource_name);
  2910. }
  2911. const char* user = req.getUser();
  2912. const char* grp = req.getGroup();
  2913. if(user != NULL && *user != '\0')
  2914. {
  2915. paction.m_account_name.append(user);
  2916. paction.m_account_type = USER_ACT;
  2917. }
  2918. else if(grp != NULL && *grp != '\0')
  2919. {
  2920. paction.m_account_name.append(grp);
  2921. // anyone is actually treated as a virtual "user" by sun and open ldap.
  2922. if((ldapsecmgr->getLdapServerType() != ACTIVE_DIRECTORY) && (stricmp(grp, "anyone") == 0))
  2923. paction.m_account_type = USER_ACT;
  2924. else
  2925. paction.m_account_type = GROUP_ACT;
  2926. }
  2927. else if((basednNameStr.length() > 0) && (resourceNameStr.length() > 0))
  2928. {
  2929. const char* account_name = req.getAccount_name();
  2930. if (!account_name || !*account_name)
  2931. {
  2932. resp.setRetcode(-1);
  2933. resp.setRetmsg("Please input or select user/group");
  2934. return false;
  2935. }
  2936. paction.m_account_name.clear().append(account_name);
  2937. ForEachItemIn(y, m_basedns)
  2938. {
  2939. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  2940. const char *aName = curbasedn->getName();
  2941. if (!aName || stricmp(basednNameStr.str(), aName))
  2942. continue;
  2943. const char *basedn = curbasedn->getBasedn();
  2944. const char *rtypestr = curbasedn->getRtype();
  2945. if (!basedn || !*basedn || !rtypestr || !*rtypestr)
  2946. continue;
  2947. StringBuffer namebuf(resourceNameStr);
  2948. SecResourceType rtype = str2type(rtypestr);
  2949. if(rtype == RT_MODULE && stricmp(namebuf.str(), "codegenerator.cpp") && stricmp(namebuf.str(), "repository") != 0 && Utils::strncasecmp(namebuf.str(), "repository.", 11) != 0)
  2950. namebuf.insert(0, "repository.");
  2951. if(prefix && *prefix)
  2952. namebuf.insert(0, prefix);
  2953. paction.m_basedn.clear().append(basedn);
  2954. paction.m_rname.clear().append(namebuf.str());
  2955. paction.m_rtype = rtype;
  2956. break;
  2957. }
  2958. resp.setAccountName(account_name);
  2959. if (req.getAccount_type() < 1)
  2960. resp.setIsGroup(false);
  2961. else
  2962. resp.setIsGroup(true);
  2963. }
  2964. else
  2965. {
  2966. resp.setRetcode(-1);
  2967. resp.setRetmsg("Please input or select user/group");
  2968. return false;
  2969. }
  2970. }
  2971. else
  2972. {
  2973. paction.m_account_name.append(req.getAccount_name());
  2974. if (version >= 1.01)
  2975. {
  2976. resp.setAccountName(req.getAccount_name());
  2977. if (req.getAccount_type() < 1)
  2978. resp.setIsGroup(false);
  2979. else
  2980. resp.setIsGroup(true);
  2981. }
  2982. }
  2983. paction.m_allows = 0;
  2984. paction.m_denies = 0;
  2985. if(req.getAllow_full())
  2986. paction.m_allows |= NewSecAccess_Full;
  2987. if(req.getAllow_read())
  2988. paction.m_allows |= NewSecAccess_Read;
  2989. if(req.getAllow_write())
  2990. paction.m_allows |= NewSecAccess_Write;
  2991. if(req.getAllow_access())
  2992. paction.m_allows |= NewSecAccess_Access;
  2993. if(req.getDeny_full())
  2994. paction.m_denies |= NewSecAccess_Full;
  2995. if(req.getDeny_read())
  2996. paction.m_denies |= NewSecAccess_Read;
  2997. if(req.getDeny_write())
  2998. paction.m_denies |= NewSecAccess_Write;
  2999. if(req.getDeny_access())
  3000. paction.m_denies |= NewSecAccess_Access;
  3001. paction.m_action.append(req.getAction());
  3002. bool ret = true;
  3003. StringBuffer retmsg;
  3004. try
  3005. {
  3006. ret = ldapsecmgr->changePermission(paction);
  3007. }
  3008. catch(IException* e)
  3009. {
  3010. resp.setRetcode(e->errorCode());
  3011. e->errorMessage(retmsg);
  3012. resp.setRetmsg(retmsg.str());
  3013. return false;
  3014. }
  3015. if(!ret)
  3016. {
  3017. resp.setRetcode(-1);
  3018. resp.setRetmsg("Unknown error");
  3019. return false;
  3020. }
  3021. resp.setRetcode(0);
  3022. }
  3023. catch(IException* e)
  3024. {
  3025. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3026. }
  3027. return true;
  3028. }
  3029. bool Cws_accessEx::onUserResetPassInput(IEspContext &context, IEspUserResetPassInputRequest &req, IEspUserResetPassInputResponse &resp)
  3030. {
  3031. try
  3032. {
  3033. checkUser(context);
  3034. resp.setUsername(req.getUsername());
  3035. }
  3036. catch(IException* e)
  3037. {
  3038. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3039. }
  3040. return true;
  3041. }
  3042. bool Cws_accessEx::onUserResetPass(IEspContext &context, IEspUserResetPassRequest &req, IEspUserResetPassResponse &resp)
  3043. {
  3044. try
  3045. {
  3046. checkUser(context);
  3047. resp.setUsername(req.getUsername());
  3048. ISecManager* secmgr = context.querySecManager();
  3049. if(secmgr == NULL)
  3050. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3051. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  3052. const char* username = req.getUsername();
  3053. if(username == NULL)
  3054. {
  3055. resp.setRetcode(-1);
  3056. resp.setRetmsg("username can't be empty");
  3057. return false;
  3058. }
  3059. const char* newpass1 = req.getNewPassword();
  3060. const char* newpass2 = req.getNewPasswordRetype();
  3061. if(newpass1 == NULL || newpass2 == NULL || *newpass1 == '\0' || *newpass2 == '\0' || strcmp(newpass1, newpass2) != 0)
  3062. {
  3063. resp.setRetcode(-1);
  3064. resp.setRetmsg("new password and retype can't be empty and must match");
  3065. return false;
  3066. }
  3067. bool ret = ldapsecmgr->updateUserPassword(username, req.getNewPassword());
  3068. if(ret)
  3069. {
  3070. resp.setRetcode(0);
  3071. resp.setRetmsg("");
  3072. return false;
  3073. }
  3074. else
  3075. {
  3076. resp.setRetcode(-1);
  3077. }
  3078. }
  3079. catch(IException* e)
  3080. {
  3081. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3082. }
  3083. return true;
  3084. }
  3085. bool Cws_accessEx::onUserPosix(IEspContext &context, IEspUserPosixRequest &req, IEspUserPosixResponse &resp)
  3086. {
  3087. try
  3088. {
  3089. checkUser(context);
  3090. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  3091. if(secmgr == NULL)
  3092. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3093. const char* username = req.getUsername();
  3094. if(username == NULL || *username == '\0')
  3095. {
  3096. resp.setRetcode(-1);
  3097. resp.setRetmsg("username can't be empty");
  3098. return false;
  3099. }
  3100. bool enable = req.getPosixenabled();
  3101. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3102. if(enable)
  3103. {
  3104. const char* gidnumber = req.getGidnumber();
  3105. const char* uidnumber = req.getUidnumber();
  3106. const char* homedirectory = req.getHomedirectory();
  3107. const char* loginshell = req.getLoginshell();
  3108. if(!gidnumber || !*gidnumber || !uidnumber || !*uidnumber || !homedirectory || !*homedirectory)
  3109. {
  3110. resp.setRetcode(-1);
  3111. resp.setRetmsg("gidnumber, uidnumber and homedirectory are required.");
  3112. return false;
  3113. }
  3114. unsigned i;
  3115. for(i = 0; i < strlen(gidnumber); i++)
  3116. {
  3117. if(!isdigit(gidnumber[i]))
  3118. throw MakeStringException(ECLWATCH_ID_MUST_BE_ALL_DIGITS, "Group ID Number should be all digits");
  3119. }
  3120. for(i = 0; i < strlen(uidnumber); i++)
  3121. {
  3122. if(!isdigit(uidnumber[i]))
  3123. throw MakeStringException(ECLWATCH_ID_MUST_BE_ALL_DIGITS, "User ID Number should be all digits");
  3124. }
  3125. user->setGidnumber(gidnumber);
  3126. user->setUidnumber(uidnumber);
  3127. user->setHomedirectory(homedirectory);
  3128. user->setLoginshell(loginshell);
  3129. }
  3130. try
  3131. {
  3132. secmgr->updateUser(enable?"posixenable":"posixdisable", *user.get());
  3133. }
  3134. catch(IException* e)
  3135. {
  3136. resp.setRetcode(-1);
  3137. StringBuffer errmsg;
  3138. resp.setRetmsg(e->errorMessage(errmsg).str());
  3139. return false;
  3140. }
  3141. resp.setUsername(username);
  3142. resp.setRetcode(0);
  3143. resp.setRetmsg("User's posix account info has been successfully updated");
  3144. }
  3145. catch(IException* e)
  3146. {
  3147. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3148. }
  3149. return true;
  3150. }
  3151. bool Cws_accessEx::onUserPosixInput(IEspContext &context, IEspUserPosixInputRequest &req, IEspUserPosixInputResponse &resp)
  3152. {
  3153. try
  3154. {
  3155. checkUser(context);
  3156. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  3157. if(secmgr == NULL)
  3158. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3159. const char* username = req.getUsername();
  3160. if(username == NULL || *username == '\0')
  3161. {
  3162. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Please specify a username.");
  3163. }
  3164. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3165. secmgr->getUserInfo(*user.get());
  3166. resp.setUsername(username);
  3167. resp.setPosixenabled(user->getPosixenabled());
  3168. if(user->getGidnumber())
  3169. resp.setGidnumber(user->getGidnumber());
  3170. if(user->getUidnumber())
  3171. resp.setUidnumber(user->getUidnumber());
  3172. if(user->getHomedirectory())
  3173. resp.setHomedirectory(user->getHomedirectory());
  3174. if(user->getLoginshell())
  3175. resp.setLoginshell(user->getLoginshell());
  3176. }
  3177. catch(IException* e)
  3178. {
  3179. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3180. }
  3181. return true;
  3182. }
  3183. bool Cws_accessEx::onUserInfoEdit(IEspContext &context, IEspUserInfoEditRequest &req, IEspUserInfoEditResponse &resp)
  3184. {
  3185. try
  3186. {
  3187. checkUser(context);
  3188. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  3189. if(secmgr == NULL)
  3190. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3191. const char* username = req.getUsername();
  3192. if(username == NULL || *username == '\0')
  3193. {
  3194. resp.setRetcode(-1);
  3195. resp.setRetmsg("username can't be empty");
  3196. return false;
  3197. }
  3198. const char* firstname = req.getFirstname();
  3199. const char* lastname = req.getLastname();
  3200. if((!firstname || !*firstname) && (!lastname || !*lastname))
  3201. {
  3202. resp.setRetcode(-1);
  3203. resp.setRetmsg("Please specify both firstname and lastname");
  3204. return false;
  3205. }
  3206. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3207. user->setFirstName(firstname);
  3208. user->setLastName(lastname);
  3209. if (context.getClientVersion() >= 1.10)
  3210. {
  3211. user->setEmployeeID(req.getEmployeeID());
  3212. }
  3213. if (context.getClientVersion() >= 1.16)
  3214. {
  3215. user->setEmployeeNumber(req.getEmployeeNumber());
  3216. }
  3217. try
  3218. {
  3219. secmgr->updateUser("names", *user.get());
  3220. }
  3221. catch(IException* e)
  3222. {
  3223. resp.setRetcode(-1);
  3224. StringBuffer errmsg;
  3225. resp.setRetmsg(e->errorMessage(errmsg).str());
  3226. return false;
  3227. }
  3228. resp.setUsername(username);
  3229. resp.setRetcode(0);
  3230. resp.setRetmsg("User's account info has been successfully updated");
  3231. }
  3232. catch(IException* e)
  3233. {
  3234. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3235. }
  3236. return true;
  3237. }
  3238. bool Cws_accessEx::onUserInfoEditInput(IEspContext &context, IEspUserInfoEditInputRequest &req, IEspUserInfoEditInputResponse &resp)
  3239. {
  3240. try
  3241. {
  3242. checkUser(context);
  3243. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  3244. if(secmgr == NULL)
  3245. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3246. const char* username = req.getUsername();
  3247. if(username == NULL || *username == '\0')
  3248. {
  3249. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Please specify a username.");
  3250. }
  3251. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3252. secmgr->getUserInfo(*user.get());
  3253. resp.setUsername(username);
  3254. resp.setFirstname(user->getFirstName());
  3255. resp.setLastname(user->getLastName());
  3256. double version = context.getClientVersion();
  3257. if (version >= 1.10)
  3258. {
  3259. resp.setEmployeeID(user->getEmployeeID());
  3260. if (version >= 1.12)
  3261. {
  3262. StringBuffer sb;
  3263. resp.setPasswordExpiration(getPasswordExpiration(user, sb));
  3264. }
  3265. if (version >= 1.16)
  3266. {
  3267. resp.setEmployeeNumber(user->getEmployeeNumber());
  3268. }
  3269. }
  3270. }
  3271. catch(IException* e)
  3272. {
  3273. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3274. }
  3275. return true;
  3276. }
  3277. bool Cws_accessEx::onUserSudoersInput(IEspContext &context, IEspUserSudoersInputRequest &req, IEspUserSudoersInputResponse &resp)
  3278. {
  3279. try
  3280. {
  3281. checkUser(context);
  3282. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  3283. if(secmgr == NULL)
  3284. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3285. const char* username = req.getUsername();
  3286. if(username == NULL || *username == '\0')
  3287. {
  3288. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Please specify a username.");
  3289. }
  3290. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3291. secmgr->getUserInfo(*user.get(), "sudoers");
  3292. resp.setUsername(username);
  3293. resp.setInsudoers(user->getInSudoers());
  3294. if(user->getInSudoers())
  3295. {
  3296. resp.setSudoHost(user->getSudoHost());
  3297. resp.setSudoCommand(user->getSudoCommand());
  3298. resp.setSudoOption(user->getSudoOption());
  3299. }
  3300. else
  3301. {
  3302. resp.setSudoHost("ALL");
  3303. resp.setSudoCommand("ALL");
  3304. resp.setSudoOption("!authenticate");
  3305. }
  3306. }
  3307. catch(IException* e)
  3308. {
  3309. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3310. }
  3311. return true;
  3312. }
  3313. bool Cws_accessEx::onUserSudoers(IEspContext &context, IEspUserSudoersRequest &req, IEspUserSudoersResponse &resp)
  3314. {
  3315. try
  3316. {
  3317. checkUser(context);
  3318. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  3319. if(secmgr == NULL)
  3320. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3321. const char* username = req.getUsername();
  3322. if(username == NULL || *username == '\0')
  3323. {
  3324. resp.setRetcode(-1);
  3325. resp.setRetmsg("username can't be empty");
  3326. return false;
  3327. }
  3328. resp.setUsername(username);
  3329. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3330. const char* action = req.getAction();
  3331. if(!action || !*action)
  3332. {
  3333. resp.setRetcode(-1);
  3334. resp.setRetmsg("Action can't be empty");
  3335. return false;
  3336. }
  3337. user->setSudoHost(req.getSudoHost());
  3338. user->setSudoCommand(req.getSudoCommand());
  3339. user->setSudoOption(req.getSudoOption());
  3340. bool ok = false;
  3341. StringBuffer retmsg;
  3342. try
  3343. {
  3344. if(stricmp(action, "add") == 0)
  3345. ok = secmgr->updateUser("sudoersadd", *user.get());
  3346. else if(stricmp(action, "delete") == 0)
  3347. ok = secmgr->updateUser("sudoersdelete", *user.get());
  3348. else if(stricmp(action, "update") == 0)
  3349. ok = secmgr->updateUser("sudoersupdate", *user.get());
  3350. }
  3351. catch(IException* e)
  3352. {
  3353. ok = false;
  3354. e->errorMessage(retmsg);
  3355. e->Release();
  3356. }
  3357. catch(...)
  3358. {
  3359. ok = false;
  3360. retmsg.append("unknown exception");
  3361. }
  3362. if(!ok)
  3363. {
  3364. resp.setRetcode(-1);
  3365. resp.setRetmsg(retmsg.str());
  3366. }
  3367. else
  3368. {
  3369. resp.setRetcode(0);
  3370. resp.setRetmsg("succeeded.");
  3371. }
  3372. }
  3373. catch(IException* e)
  3374. {
  3375. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3376. }
  3377. return true;
  3378. }
  3379. bool Cws_accessEx::onAccountPermissions(IEspContext &context, IEspAccountPermissionsRequest &req, IEspAccountPermissionsResponse &resp)
  3380. {
  3381. try
  3382. {
  3383. StringBuffer userID;
  3384. bool bGroupAccount = req.getIsGroup();
  3385. const char* username = req.getAccountName();
  3386. if(!username || !*username)
  3387. {//send back the permissions for the current user.
  3388. context.getUserID(userID);
  3389. if (!userID.length())
  3390. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Could not get user ID.");
  3391. username = userID.str();
  3392. bGroupAccount = false;
  3393. }
  3394. else
  3395. checkUser(context);
  3396. double version = context.getClientVersion();
  3397. CLdapSecManager* ldapsecmgr = queryLDAPSecurityManager(context);
  3398. if(ldapsecmgr == NULL)
  3399. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3400. bool bIncludeGroup = req.getIncludeGroup();
  3401. if(m_basedns.length() == 0)
  3402. {
  3403. setBasedns(context);
  3404. }
  3405. StringArray groupnames;
  3406. if (version > 1.02 && !bGroupAccount && bIncludeGroup)
  3407. {
  3408. ldapsecmgr->getGroups(username, groupnames);
  3409. }
  3410. groupnames.append("Authenticated Users");
  3411. groupnames.append("everyone");
  3412. IArrayOf<IEspAccountPermission> accountPermissions;
  3413. bool bAuthUsersPerm = false;
  3414. Owned<IEspGroupAccountPermission> grouppermission1 = createGroupAccountPermission();
  3415. grouppermission1->setGroupName("Authenticated Users");
  3416. if (version > 1.05)
  3417. {
  3418. StringArray basednNames;
  3419. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, "Authenticated Users", 1, basednNames);
  3420. if (basednNames.length() > 0)
  3421. grouppermission1->setBasednNames(basednNames);
  3422. }
  3423. IArrayOf<IConstAccountPermission>& authUsersPermissions = grouppermission1->getPermissions();
  3424. bool bEveryonePerm = false;
  3425. Owned<IEspGroupAccountPermission> grouppermission2 = createGroupAccountPermission();
  3426. grouppermission2->setGroupName("Everyone");
  3427. if (version > 1.05)
  3428. {
  3429. StringArray basednNames;
  3430. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, "Everyone", 1, basednNames);
  3431. if (basednNames.length() > 0)
  3432. grouppermission2->setBasednNames(basednNames);
  3433. }
  3434. IArrayOf<IConstAccountPermission>& everyonePermissions = grouppermission2->getPermissions();
  3435. IArrayOf<IEspGroupAccountPermission> groupAccountPermissions;
  3436. StringBuffer moduleBasedn; //To be used by the Permission: codegenerator.cpp
  3437. ForEachItemIn(y1, m_basedns)
  3438. {
  3439. IEspDnStruct* curbasedn = &(m_basedns.item(y1));
  3440. const char *aName = curbasedn->getName();
  3441. const char *aBasedn = curbasedn->getBasedn();
  3442. const char *aRtype = curbasedn->getRtype();
  3443. const char *aRtitle = curbasedn->getRtitle();
  3444. if (!aName || !*aName ||!aBasedn || !*aBasedn ||!aRtype || !*aRtype ||!aRtitle || !*aRtitle)
  3445. continue;
  3446. SecResourceType rtype = str2type(aRtype);
  3447. if (rtype == RT_MODULE)
  3448. {
  3449. moduleBasedn.append(aBasedn);
  3450. break;
  3451. }
  3452. }
  3453. ForEachItemIn(y, m_basedns)
  3454. {
  3455. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  3456. const char *aName = curbasedn->getName();
  3457. const char *aBasedn = curbasedn->getBasedn();
  3458. const char *aRtype = curbasedn->getRtype();
  3459. const char *aRtitle = curbasedn->getRtitle();
  3460. if (!aName || !*aName ||!aBasedn || !*aBasedn ||!aRtype || !*aRtype ||!aRtitle || !*aRtitle)
  3461. continue;
  3462. SecResourceType rtype = str2type(aRtype);
  3463. IArrayOf<IEspResource> ResourceArray;
  3464. //if(rtype == RT_FILE_SCOPE || rtype == RT_WORKUNIT_SCOPE)
  3465. if(rtype == RT_WORKUNIT_SCOPE)
  3466. {
  3467. StringBuffer deft_basedn, deft_name;
  3468. const char* comma = strchr(aBasedn, ',');
  3469. const char* eqsign = strchr(aBasedn, '=');
  3470. if(eqsign != NULL)
  3471. {
  3472. if(comma == NULL)
  3473. deft_name.append(eqsign+1);
  3474. else
  3475. {
  3476. deft_name.append(comma - eqsign - 1, eqsign+1);
  3477. deft_basedn.append(comma + 1);
  3478. }
  3479. }
  3480. if (deft_name.length() > 0)
  3481. {
  3482. Owned<IEspResource> oneresource = createResource();
  3483. oneresource->setName(deft_name);
  3484. oneresource->setDescription(deft_basedn);
  3485. ResourceArray.append(*oneresource.getLink());
  3486. }
  3487. }
  3488. IArrayOf<ISecResource> resources;
  3489. if(ldapsecmgr->getResources(rtype, aBasedn, resources))
  3490. {
  3491. ForEachItemIn(y1, resources)
  3492. {
  3493. ISecResource& r = resources.item(y1);
  3494. const char* rname = r.getName();
  3495. if(rname == NULL || *rname == '\0')
  3496. continue;
  3497. //permission codegenerator.cpp is saved as a service permission (not a module permission)
  3498. //when it is added for a user
  3499. if ((rtype == RT_MODULE) && (!stricmp(rname, "codegenerator.cpp")))
  3500. continue;
  3501. if((rtype == RT_MODULE) && Utils::strncasecmp(rname, "repository", 10))
  3502. {
  3503. continue;
  3504. }
  3505. Owned<IEspResource> oneresource = createResource();
  3506. oneresource->setName(rname);
  3507. oneresource->setDescription(aBasedn);
  3508. ResourceArray.append(*oneresource.getLink());
  3509. }
  3510. }
  3511. if(rtype == RT_SERVICE && moduleBasedn.length() > 0)
  3512. { //permission codegenerator.cpp is saved as a service permission when it is added for a user
  3513. Owned<IEspResource> oneresource = createResource();
  3514. oneresource->setName("codegenerator.cpp");
  3515. oneresource->setDescription(moduleBasedn.str());
  3516. ResourceArray.append(*oneresource.getLink());
  3517. moduleBasedn.clear();
  3518. }
  3519. ForEachItemIn(y2, ResourceArray)
  3520. {
  3521. IEspResource& r = ResourceArray.item(y2);
  3522. const char* resourceName = r.getName();
  3523. const char* dnname = r.getDescription();
  3524. if (isEmptyString(resourceName))
  3525. continue;
  3526. try
  3527. {
  3528. IArrayOf<CPermission> permissions;
  3529. ldapsecmgr->getPermissionsArray(dnname, rtype, resourceName, permissions);
  3530. ForEachItemIn(x, permissions)
  3531. {
  3532. CPermission& perm = permissions.item(x);
  3533. int accountType = perm.getAccount_type(); //0-individual, 1 - group
  3534. if (bGroupAccount && accountType < 1)
  3535. continue;
  3536. if (!bGroupAccount && (accountType > 0) && (groupnames.length() < 1))
  3537. continue;
  3538. StringBuffer escapedname;
  3539. const char* actname = perm.getAccount_name();
  3540. if ((!bGroupAccount && accountType < 1) || (bGroupAccount && accountType > 0))
  3541. {
  3542. if(!actname || strcmp(actname, username))
  3543. continue;
  3544. }
  3545. else if (version > 1.02)
  3546. {
  3547. if(!actname || groupnames.length() < 1)
  3548. continue;
  3549. bool bFound = false;
  3550. for(unsigned i = 0; i < groupnames.length(); i++)
  3551. {
  3552. const char* group = groupnames.item(i);
  3553. if (!group || strcmp(actname, group))
  3554. continue;
  3555. bFound = true;
  3556. break;
  3557. }
  3558. if (!bFound)
  3559. continue;
  3560. }
  3561. Owned<IEspAccountPermission> onepermission = createAccountPermission();
  3562. if (version < 1.15)
  3563. {
  3564. onepermission->setBasedn(dnname);
  3565. onepermission->setRType(aRtype);
  3566. onepermission->setResourceName(aRtitle);
  3567. onepermission->setPermissionName(resourceName);
  3568. }
  3569. else
  3570. {
  3571. onepermission->setBasednName(aName);
  3572. onepermission->setResourceName(resourceName);
  3573. }
  3574. int allows = perm.getAllows();
  3575. int denies = perm.getDenies();
  3576. if((allows & NewSecAccess_Access) == NewSecAccess_Access)
  3577. onepermission->setAllow_access(true);
  3578. if((allows & NewSecAccess_Read) == NewSecAccess_Read)
  3579. onepermission->setAllow_read(true);
  3580. if((allows & NewSecAccess_Write) == NewSecAccess_Write)
  3581. onepermission->setAllow_write(true);
  3582. if((allows & NewSecAccess_Full) == NewSecAccess_Full)
  3583. onepermission->setAllow_full(true);
  3584. if((denies & NewSecAccess_Access) == NewSecAccess_Access)
  3585. onepermission->setDeny_access(true);
  3586. if((denies & NewSecAccess_Read) == NewSecAccess_Read)
  3587. onepermission->setDeny_read(true);
  3588. if((denies & NewSecAccess_Write) == NewSecAccess_Write)
  3589. onepermission->setDeny_write(true);
  3590. if((denies & NewSecAccess_Full) == NewSecAccess_Full)
  3591. onepermission->setDeny_full(true);
  3592. if ((!bGroupAccount && accountType < 1) || (bGroupAccount && accountType > 0))
  3593. accountPermissions.append(*onepermission.getLink());
  3594. else if (version > 1.02)
  3595. {
  3596. if (!strcmp(actname, "Authenticated Users"))
  3597. {
  3598. authUsersPermissions.append(*onepermission.getLink());
  3599. bAuthUsersPerm = true;
  3600. }
  3601. else if (!strcmp(actname, "everyone"))
  3602. {
  3603. everyonePermissions.append(*onepermission.getLink());
  3604. bEveryonePerm = true;
  3605. }
  3606. else
  3607. {
  3608. bool bFound = false;
  3609. ForEachItemIn(k, groupAccountPermissions)
  3610. {
  3611. IEspGroupAccountPermission& grouppermission0 = groupAccountPermissions.item(k);
  3612. const char* g_name = grouppermission0.getGroupName();
  3613. if (!g_name || strcmp(actname, g_name))
  3614. continue;
  3615. IArrayOf<IConstAccountPermission>& g_permission = grouppermission0.getPermissions();
  3616. g_permission.append(*onepermission.getLink());
  3617. bFound = true;
  3618. break;
  3619. }
  3620. if (!bFound)
  3621. {
  3622. Owned<IEspGroupAccountPermission> grouppermission = createGroupAccountPermission();
  3623. grouppermission->setGroupName(actname);
  3624. if (version > 1.05)
  3625. {
  3626. StringArray basednNames;
  3627. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, actname, 1, basednNames);
  3628. if (basednNames.length() > 0)
  3629. grouppermission->setBasednNames(basednNames);
  3630. }
  3631. IArrayOf<IConstAccountPermission>& g_permission = grouppermission->getPermissions();
  3632. g_permission.append(*onepermission.getLink());
  3633. groupAccountPermissions.append(*grouppermission.getLink());
  3634. }
  3635. }
  3636. }
  3637. }
  3638. }
  3639. catch(IException* e) //exception may be thrown when no permission for the resource
  3640. {
  3641. e->Release();
  3642. }
  3643. }
  3644. }
  3645. StringArray basednNames;
  3646. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, username, bGroupAccount? 1:0, basednNames);
  3647. if (basednNames.length() > 0)
  3648. {
  3649. resp.setBasednNames(basednNames);
  3650. }
  3651. if (accountPermissions.length() > 0)
  3652. {
  3653. resp.setPermissions(accountPermissions);
  3654. }
  3655. if (version > 1.02)
  3656. {
  3657. if (bAuthUsersPerm)
  3658. {
  3659. groupAccountPermissions.append(*grouppermission1.getLink());
  3660. }
  3661. if (bEveryonePerm)
  3662. {
  3663. groupAccountPermissions.append(*grouppermission2.getLink());
  3664. }
  3665. if (groupAccountPermissions.length() > 0)
  3666. {
  3667. resp.setGroupPermissions(groupAccountPermissions);
  3668. }
  3669. }
  3670. resp.setAccountName(username);
  3671. resp.setIsGroup(bGroupAccount);
  3672. }
  3673. catch(IException* e)
  3674. {
  3675. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3676. }
  3677. return true;
  3678. }
  3679. bool Cws_accessEx::onFilePermission(IEspContext &context, IEspFilePermissionRequest &req, IEspFilePermissionResponse &resp)
  3680. {
  3681. try
  3682. {
  3683. CLdapSecManager* secmgr = queryLDAPSecurityManager(context);
  3684. double version = context.getClientVersion();
  3685. if (version > 1.03)
  3686. {
  3687. if(secmgr == NULL)
  3688. {
  3689. resp.setNoSecMngr(true);
  3690. return true;
  3691. }
  3692. }
  3693. else
  3694. {
  3695. if(secmgr == NULL)
  3696. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3697. }
  3698. checkUser(context, FILE_SCOPE_RTYPE, FILE_SCOPE_RTITLE, SecAccess_Read);
  3699. //Get all users for input form
  3700. int numusers = secmgr->countUsers("", MAX_USERS_DISPLAY);
  3701. if(numusers == -1)
  3702. {
  3703. resp.setToomany(true);
  3704. }
  3705. else
  3706. {
  3707. resp.setToomany(false);
  3708. IArrayOf<IEspUserInfo> espusers;
  3709. IUserArray users;
  3710. secmgr->getAllUsers(users);
  3711. ForEachItemIn(x, users)
  3712. {
  3713. CLdapSecUser* usr = dynamic_cast<CLdapSecUser*>(&users.item(x));
  3714. if(usr)
  3715. {
  3716. Owned<IEspUserInfo> oneusr = createUserInfo();
  3717. oneusr->setUsername(usr->getName());
  3718. oneusr->setFullname(usr->getFullName());
  3719. espusers.append(*oneusr.getLink());
  3720. }
  3721. }
  3722. resp.setUsers(espusers);
  3723. }
  3724. //Get all groups for input form
  3725. StringArray groupnames;
  3726. StringArray managedBy;
  3727. StringArray descriptions;
  3728. secmgr->getAllGroups(groupnames, managedBy, descriptions);
  3729. ///groupnames.append("Authenticated Users");
  3730. ///groupnames.append("Administrators");
  3731. if (groupnames.length() > 0)
  3732. {
  3733. IArrayOf<IEspGroupInfo> groups;
  3734. for(unsigned i = 0; i < groupnames.length(); i++)
  3735. {
  3736. const char* grpname = groupnames.item(i);
  3737. if(grpname == NULL || grpname[0] == '\0')
  3738. continue;
  3739. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  3740. onegrp->setName(grpname);
  3741. onegrp->setGroupDesc(descriptions.item(i));
  3742. onegrp->setGroupOwner(managedBy.item(i));
  3743. groups.append(*onegrp.getLink());
  3744. }
  3745. resp.setGroups(groups);
  3746. }
  3747. const char* fileName = req.getFileName();
  3748. const char* userName = req.getUserName();
  3749. const char* groupName = req.getGroupName();
  3750. if (!fileName || !*fileName)
  3751. return true; //no file name is set when the input form is launched first time
  3752. if ((!groupName || !*groupName) && (!userName || !*userName))
  3753. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Either user name or group name has to be specified.");
  3754. SecAccessFlags access = SecAccess_Unavailable;
  3755. if (userName && *userName) //for user
  3756. {
  3757. resp.setFileName(fileName);
  3758. resp.setUserName(userName);
  3759. Owned<ISecUser> sec_user = secmgr->findUser(userName);
  3760. if (sec_user)
  3761. {
  3762. StringBuffer accessStr;
  3763. access = secmgr->authorizeEx(RT_FILE_SCOPE, *sec_user, fileName, false);
  3764. switch (access)
  3765. {
  3766. case SecAccess_Full:
  3767. resp.setUserPermission("Full Access Permission");
  3768. break;
  3769. case SecAccess_Write:
  3770. resp.setUserPermission("Write Access Permission");
  3771. break;
  3772. case SecAccess_Read:
  3773. resp.setUserPermission("Read Access Permission");
  3774. break;
  3775. case SecAccess_Access:
  3776. resp.setUserPermission("Access Permission");
  3777. break;
  3778. case SecAccess_None:
  3779. resp.setUserPermission("None Access Permission");
  3780. break;
  3781. default:
  3782. resp.setUserPermission("Permission Unknown");
  3783. break;
  3784. }
  3785. }
  3786. }
  3787. else //for group
  3788. {
  3789. resp.setFileName(fileName);
  3790. resp.setGroupName(groupName);
  3791. if(m_basedns.length() == 0) //basedns may never be set
  3792. {
  3793. setBasedns(context);
  3794. }
  3795. //Find out the basedn for RT_FILE_SCOPE
  3796. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  3797. StringBuffer basednStr;
  3798. ForEachItemIn(y, m_basedns)
  3799. {
  3800. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  3801. const char *aBasedn = curbasedn->getBasedn();
  3802. const char *aRtype = curbasedn->getRtype();
  3803. if (!aBasedn || !*aBasedn || !aRtype || !*aRtype)
  3804. continue;
  3805. SecResourceType rtype = str2type(aRtype);
  3806. if (rtype != RT_FILE_SCOPE)
  3807. continue;
  3808. basednStr.append(aBasedn);
  3809. }
  3810. char* pStr0 = (char*) fileName;
  3811. while (pStr0[0] == ':') //in case of some ':' by mistake
  3812. pStr0++;
  3813. //Check the permissin for the file and the group
  3814. if (*pStr0 && basednStr.length() > 0)
  3815. {
  3816. StringBuffer lastFileScope;
  3817. StringArray scopes;
  3818. char* pStr = strstr(pStr0, "::");
  3819. while (pStr)
  3820. {
  3821. char fileScope[10240];
  3822. strncpy(fileScope, pStr0, pStr-pStr0);
  3823. fileScope[pStr-pStr0] = 0;
  3824. if (lastFileScope.length() < 1)
  3825. lastFileScope.append(fileScope);
  3826. else
  3827. lastFileScope.appendf("::%s", fileScope);
  3828. scopes.add(lastFileScope.str(), 0);
  3829. pStr0 = pStr+2;
  3830. while (pStr0[0] == ':') //in case of more than two ':' by mistake
  3831. pStr0++;
  3832. if (pStr0[0] == 0)
  3833. break;
  3834. pStr = strstr(pStr0, "::");
  3835. }
  3836. if (pStr0[0] != 0)
  3837. {
  3838. if (lastFileScope.length() < 1)
  3839. lastFileScope.append(pStr0);
  3840. else
  3841. lastFileScope.appendf("::%s", pStr0);
  3842. scopes.add(lastFileScope.str(), 0);
  3843. }
  3844. access = SecAccess_None;
  3845. ForEachItemIn(y, scopes)
  3846. {
  3847. StringBuffer namebuf(scopes.item(y));
  3848. try
  3849. {
  3850. IArrayOf<CPermission> permissions;
  3851. ldapsecmgr->getPermissionsArray(basednStr.str(), RT_FILE_SCOPE, namebuf.str(), permissions);
  3852. ForEachItemIn(x, permissions)
  3853. {
  3854. CPermission& perm = permissions.item(x);
  3855. int accountType = perm.getAccount_type(); //0-individual, 1 - group
  3856. if (accountType < 1)
  3857. continue;
  3858. const char* actname = perm.getAccount_name();
  3859. if(!actname || strcmp(actname, groupName))
  3860. continue;
  3861. int allows = perm.getAllows();
  3862. int denies = perm.getDenies();
  3863. access = (SecAccessFlags)(allows & (~denies));
  3864. break;
  3865. }
  3866. }
  3867. catch(IException* e) //exception may be thrown when no permission for the resource
  3868. {
  3869. e->Release();
  3870. }
  3871. if (access != SecAccess_None)
  3872. break;
  3873. }
  3874. }
  3875. //Convert permission type to display string
  3876. if((access & NewSecAccess_Full) == NewSecAccess_Full)
  3877. resp.setUserPermission("Full Access Permission");
  3878. else if((access & NewSecAccess_Write) == NewSecAccess_Write)
  3879. resp.setUserPermission("Write Access Permission");
  3880. else if((access & NewSecAccess_Read) == NewSecAccess_Read)
  3881. resp.setUserPermission("Read Access Permission");
  3882. else if((access & NewSecAccess_Access) == NewSecAccess_Access)
  3883. resp.setUserPermission("Access Permission");
  3884. else if (access == (SecAccessFlags)NewSecAccess_None)
  3885. resp.setUserPermission("None Access Permission");
  3886. else
  3887. resp.setUserPermission("Permission Unknown");
  3888. }
  3889. }
  3890. catch(IException* e)
  3891. {
  3892. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3893. }
  3894. return true;
  3895. }
  3896. bool Cws_accessEx::onUserAccountExport(IEspContext &context, IEspUserAccountExportRequest &req, IEspUserAccountExportResponse &resp)
  3897. {
  3898. try
  3899. {
  3900. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  3901. if(secmgr == NULL)
  3902. {
  3903. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  3904. }
  3905. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  3906. checkUser(context);
  3907. StringBuffer xls;
  3908. xls.append("<html xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">");
  3909. xls.append("<head>");
  3910. xls.append("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
  3911. xls.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
  3912. xls.append("<title>User Account Information</title>");
  3913. xls.append("</head>");
  3914. xls.append("<body>");
  3915. xls.append("<table xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" cellspacing=\"0\" frame=\"box\" rules=\"all\">");
  3916. xls.append("<thead>");
  3917. xls.append("<tr valign=\"bottom\">");
  3918. xls.append("<th>Login Name</th>");
  3919. xls.append("<th>First Name</th>");
  3920. xls.append("<th>Last Name</th>");
  3921. xls.append("<th>Group Name</th>");
  3922. xls.append("</tr>");
  3923. StringArray& usernames = req.getUsernames();
  3924. StringArray& groupnames = req.getGroupnames();
  3925. if (usernames.length() > 0)
  3926. {
  3927. for(unsigned i = 0; i < usernames.length(); i++)
  3928. {
  3929. const char* username = usernames.item(i);
  3930. if (!username || !*username)
  3931. continue;
  3932. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3933. secmgr->getUserInfo(*user.get());
  3934. const char* firstname = user->getFirstName();
  3935. const char* lastname = user->getLastName();
  3936. StringArray groupnames1;
  3937. ldapsecmgr->getGroups(username, groupnames1);
  3938. ///groupnames1.append("TestGroup1");
  3939. ///groupnames1.append("TestGroup2");
  3940. if (groupnames1.length() < 1)
  3941. {
  3942. xls.append("<tr>");
  3943. xls.appendf("<td>%s</td>", username);
  3944. if (!firstname || !*firstname)
  3945. xls.append("<td></td>");
  3946. else
  3947. xls.appendf("<td>%s</td>", firstname);
  3948. if (!lastname || !*lastname)
  3949. xls.append("<td></td>");
  3950. else
  3951. xls.appendf("<td>%s</td>", lastname);
  3952. xls.append("<td></td>");
  3953. xls.append("</tr>");
  3954. }
  3955. else
  3956. {
  3957. for(unsigned i = 0; i < groupnames1.length(); i++)
  3958. {
  3959. const char* grpname = groupnames1.item(i);
  3960. if(grpname == NULL || grpname[0] == '\0')
  3961. continue;
  3962. xls.append("<tr>");
  3963. xls.appendf("<td>%s</td>", username);
  3964. if (!firstname || !*firstname)
  3965. xls.append("<td></td>");
  3966. else
  3967. xls.appendf("<td>%s</td>", firstname);
  3968. if (!lastname || !*lastname)
  3969. xls.append("<td></td>");
  3970. else
  3971. xls.appendf("<td>%s</td>", lastname);
  3972. xls.appendf("<td>%s</td>", grpname);
  3973. xls.append("</tr>");
  3974. }
  3975. }
  3976. }
  3977. }
  3978. else if (groupnames.length() > 0)
  3979. {
  3980. for(unsigned i = 0; i < groupnames.length(); i++)
  3981. {
  3982. const char* groupname = groupnames.item(i);
  3983. if (!groupname || !*groupname)
  3984. continue;
  3985. StringArray usernames1;
  3986. ldapsecmgr->getGroupMembers(groupname, usernames1);
  3987. ///usernames1.append("_clo");
  3988. ///usernames1.append("_rkc");
  3989. for(unsigned j = 0; j < usernames1.length(); j++)
  3990. {
  3991. const char* usrname = usernames1.item(j);
  3992. if(usrname == NULL || usrname[0] == '\0')
  3993. continue;
  3994. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(usrname);
  3995. secmgr->getUserInfo(*user.get());
  3996. const char* firstname = user->getFirstName();
  3997. const char* lastname = user->getLastName();
  3998. xls.append("<tr>");
  3999. xls.appendf("<td>%s</td>", usrname);
  4000. if (!firstname || !*firstname)
  4001. xls.append("<td></td>");
  4002. else
  4003. xls.appendf("<td>%s</td>", firstname);
  4004. if (!lastname || !*lastname)
  4005. xls.append("<td></td>");
  4006. else
  4007. xls.appendf("<td>%s</td>", lastname);
  4008. xls.appendf("<td>%s</td>", groupname);
  4009. xls.append("</tr>");
  4010. }
  4011. }
  4012. }
  4013. xls.append("</thead>");
  4014. xls.append("</table>");
  4015. xls.append("</body>");
  4016. xls.append("</html>");
  4017. MemoryBuffer buff;
  4018. buff.setBuffer(xls.length(), (void*)xls.str());
  4019. resp.setResult(buff);
  4020. resp.setResult_mimetype("application/vnd.ms-excel");
  4021. }
  4022. catch(IException* e)
  4023. {
  4024. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  4025. }
  4026. return true;
  4027. }
  4028. int Cws_accessSoapBindingEx::onGetForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, const char *service, const char *method)
  4029. {
  4030. try
  4031. {
  4032. if(stricmp(method,"SecurityNotEnabled")==0)
  4033. {
  4034. StringBuffer page;
  4035. page.append(
  4036. "<html>"
  4037. "<head>"
  4038. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
  4039. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/default.css\"/>"
  4040. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/yui/build/fonts/fonts-min.css\" />"
  4041. "<title>Security Not Enabled</title>"
  4042. "</head>"
  4043. "<body>"
  4044. "<p style=\"text-align:centre;\">In order to use this feature, authentication should be enabled.");
  4045. page.append("</p></body>"
  4046. "</html>");
  4047. response->setContent(page.str());
  4048. response->setContentType("text/html");
  4049. response->send();
  4050. return 0;
  4051. }
  4052. else if(stricmp(method,"FirefoxNotSupport")==0)
  4053. {
  4054. StringBuffer page;
  4055. page.append(
  4056. "<html>"
  4057. "<head>"
  4058. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
  4059. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/default.css\"/>"
  4060. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/yui/build/fonts/fonts-min.css\" />"
  4061. "<title>Firefox Not Support</title>"
  4062. "</head>"
  4063. "<body>"
  4064. "<p style=\"text-align:centre;\">This feature is not supported under Firefox.");
  4065. page.append("</p></body>"
  4066. "</html>");
  4067. response->setContent(page.str());
  4068. response->setContentType("text/html");
  4069. response->send();
  4070. return 0;
  4071. }
  4072. }
  4073. catch(IException* e)
  4074. {
  4075. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  4076. }
  4077. return onGetForm(context, request, response, service, method);
  4078. }