ws_accessService.cpp 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #pragma warning (disable : 4786)
  15. #include <stdlib.h>
  16. #include "ws_accessService.hpp"
  17. #include "exception_util.hpp"
  18. #include <set>
  19. #define MSG_SEC_MANAGER_IS_NULL "Security manager is not found. Please check if the system authentication is set up correctly"
  20. #define MAX_USERS_DISPLAY 400
  21. #define MAX_RESOURCES_DISPLAY 3000
  22. static const long MAXXLSTRANSFER = 5000000;
  23. void checkUser(IEspContext& context)
  24. {
  25. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  26. if(secmgr == NULL)
  27. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  28. if(!secmgr->isSuperUser(context.queryUser()))
  29. throw MakeStringException(ECLWATCH_ADMIN_ACCESS_DENIED, "Access denied, administrators only.");
  30. }
  31. void Cws_accessEx::init(IPropertyTree *cfg, const char *process, const char *service)
  32. {
  33. if(cfg == NULL)
  34. throw MakeStringException(-1, "can't initialize Cws_accessEx, cfg is NULL");
  35. StringBuffer xpath;
  36. xpath.appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]", process, service);
  37. IPropertyTree* servicecfg = cfg->getPropTree(xpath.str());
  38. if(servicecfg == NULL)
  39. {
  40. WARNLOG(-1, "config not found for service %s/%s",process, service);
  41. return;
  42. }
  43. m_servicecfg.setown(LINK(servicecfg));
  44. /* Config is like -
  45. <Modules basedn="ou=le,ou=ecl,dc=le">
  46. <Eclserver name="eclserver" basedn="ou=le,ou=ecl,dc=le" templateName="repository.newmoduletemplate"/>
  47. </Modules>
  48. <Files basedn="ou=Files,ou=ecl"/>
  49. <Resources>
  50. <Binding name="EspBinding" service="espsmc" port="8010" basedn="ou=SMC,ou=EspServices,ou=ecl" workunitsBasedn="ou=workunits,ou=ecl"/>
  51. </Resources>
  52. */
  53. Owned<IPropertyTreeIterator> eclservers = m_servicecfg->getElements("Modules/Eclserver");
  54. for (eclservers->first(); eclservers->isValid(); eclservers->next())
  55. {
  56. const char *templatename = eclservers->query().queryProp("@templateName");
  57. const char* basedn = eclservers->query().queryProp("@basedn");
  58. if(basedn && *basedn)
  59. {
  60. StringBuffer name, head;
  61. const char* eclservername = eclservers->query().queryProp("@name");
  62. name.append("Repository Modules for ").append(eclservername);
  63. Owned<IEspDnStruct> onedn = createDnStruct();
  64. onedn->setBasedn(basedn);
  65. onedn->setName(name.str());
  66. onedn->setRtype("module");
  67. onedn->setRtitle("Module");
  68. if(templatename != NULL)
  69. {
  70. onedn->setTemplatename(templatename);
  71. }
  72. m_rawbasedns.append(*onedn.getLink());
  73. }
  74. }
  75. const char* modules_basedn = m_servicecfg->queryProp("Modules/@basedn");
  76. if(modules_basedn && *modules_basedn)
  77. {
  78. Owned<IEspDnStruct> onedn = createDnStruct();
  79. onedn->setBasedn(modules_basedn);
  80. onedn->setName("Repository Modules");
  81. onedn->setRtype("module");
  82. onedn->setRtitle("Module");
  83. m_rawbasedns.append(*onedn.getLink());
  84. }
  85. const char* files_basedn = m_servicecfg->queryProp("Files/@basedn");
  86. if(files_basedn && *files_basedn)
  87. {
  88. Owned<IEspDnStruct> onedn = createDnStruct();
  89. onedn->setBasedn(files_basedn);
  90. onedn->setName("File Scopes");
  91. onedn->setRtype("file");
  92. m_rawbasedns.append(*onedn.getLink());
  93. onedn->setRtitle("FileScope");
  94. }
  95. StringBuffer workunits_basedn;
  96. Owned<IPropertyTreeIterator> bindings = m_servicecfg->getElements("Resources/Binding");
  97. for (bindings->first(); bindings->isValid(); bindings->next())
  98. {
  99. const char *service = bindings->query().queryProp("@service");
  100. const char* basedn = bindings->query().queryProp("@basedn");
  101. if(workunits_basedn.length() == 0)
  102. {
  103. const char* wubasedn = bindings->query().queryProp("@workunitsBasedn");
  104. if(wubasedn != NULL)
  105. workunits_basedn.append(wubasedn);
  106. }
  107. if(basedn && *basedn)
  108. {
  109. StringBuffer name, head;
  110. name.append("Esp Features for ");
  111. const char* bptr = basedn;
  112. while(*bptr != '\0' && *bptr != '=')
  113. bptr++;
  114. if(*bptr != '\0')
  115. bptr++;
  116. const char* colon = strstr(bptr, ",");
  117. if(colon == NULL)
  118. head.append(bptr);
  119. else
  120. head.append(colon - bptr, bptr);
  121. if(stricmp(head.str(), "WsAttributesAccess") == 0)
  122. continue;
  123. Owned<IEspDnStruct> onedn = createDnStruct();
  124. onedn->setBasedn(basedn);
  125. name.append(head.str());
  126. onedn->setName(name.str());
  127. onedn->setRtype("service");
  128. head.append(" Feature");
  129. onedn->setRtitle(head.str());
  130. m_rawbasedns.append(*onedn.getLink());
  131. }
  132. }
  133. if(workunits_basedn.length() > 0)
  134. {
  135. Owned<IEspDnStruct> onedn = createDnStruct();
  136. onedn->setBasedn(workunits_basedn.str());
  137. onedn->setName("Workunit Scopes");
  138. onedn->setRtype("workunit");
  139. onedn->setRtitle("WorkunitScope");
  140. m_rawbasedns.append(*onedn.getLink());
  141. }
  142. }
  143. void Cws_accessEx::setBasedns(IEspContext &context)
  144. {
  145. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  146. if(secmgr == NULL)
  147. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  148. set<string> alreadythere;
  149. ForEachItemInRev(x, m_rawbasedns)
  150. {
  151. IEspDnStruct* basedn = &(m_rawbasedns.popGet());
  152. const char* tname = basedn->getTemplatename();
  153. StringBuffer nbasedn;
  154. secmgr->normalizeDn(basedn->getBasedn(), nbasedn);
  155. if(alreadythere.find(nbasedn.str()) == alreadythere.end())
  156. {
  157. alreadythere.insert(nbasedn.str());
  158. Owned<IEspDnStruct> onedn = createDnStruct();
  159. onedn->setBasedn(nbasedn.str());
  160. onedn->setName(basedn->getName());
  161. onedn->setRtype(basedn->getRtype());
  162. onedn->setRtitle(basedn->getRtitle());
  163. if(tname != NULL && *tname != '\0')
  164. onedn->setTemplatename(tname);
  165. m_basedns.append(*onedn.getLink());
  166. }
  167. else
  168. {
  169. ForEachItemIn(y, m_basedns)
  170. {
  171. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  172. if(stricmp(curbasedn->getBasedn(), nbasedn.str()) == 0)
  173. {
  174. const char* curtname = curbasedn->getTemplatename();
  175. if((curtname == NULL || *curtname == '\0') && (tname != NULL && *tname != '\0'))
  176. curbasedn->setTemplatename(tname);
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. return;
  183. }
  184. bool Cws_accessEx::getNewFileScopePermissions(ISecManager* secmgr, IEspResourceAddRequest &req, StringBuffer& existingResource, StringArray& newResources)
  185. {
  186. if (!secmgr)
  187. return false;
  188. const char* name0 = req.getName();
  189. if (!name0 || !*name0)
  190. return false;
  191. char* pStr0 = (char*) name0;
  192. while (pStr0[0] == ':') //in case of some ':' by mistake
  193. pStr0++;
  194. if (pStr0[0] == 0)
  195. return false;
  196. StringBuffer lastFileScope;
  197. char* pStr = strstr(pStr0, "::");
  198. while (pStr)
  199. {
  200. char fileScope[10240];
  201. strncpy(fileScope, pStr0, pStr-pStr0);
  202. fileScope[pStr-pStr0] = 0;
  203. if (lastFileScope.length() < 1)
  204. lastFileScope.append(fileScope);
  205. else
  206. lastFileScope.appendf("::%s", fileScope);
  207. newResources.append(lastFileScope.str());
  208. pStr0 = pStr+2;
  209. while (pStr0[0] == ':') //in case of more than two ':' by mistake
  210. pStr0++;
  211. if (pStr0[0] == 0)
  212. break;
  213. pStr = strstr(pStr0, "::");
  214. }
  215. if (pStr0[0] != 0)
  216. {
  217. if (lastFileScope.length() < 1)
  218. lastFileScope.append(pStr0);
  219. else
  220. lastFileScope.appendf("::%s", pStr0);
  221. newResources.append(lastFileScope.str());
  222. }
  223. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  224. while (newResources.ordinality())
  225. {
  226. StringBuffer namebuf = newResources.item(0);
  227. try
  228. {
  229. IArrayOf<CPermission> permissions;
  230. ldapsecmgr->getPermissionsArray(req.getBasedn(), str2type(req.getRtype()), namebuf.str(), permissions);
  231. if (!permissions.ordinality())
  232. {
  233. break;
  234. }
  235. }
  236. catch(IException* e) //exception may be thrown when no permission for the resource
  237. {
  238. e->Release();
  239. break;
  240. }
  241. existingResource.clear().append(namebuf);
  242. newResources.remove(0);
  243. }
  244. return true;
  245. }
  246. bool Cws_accessEx::setNewFileScopePermissions(ISecManager* secmgr, IEspResourceAddRequest &req, StringBuffer& existingResource, StringArray& newResources)
  247. {
  248. if (!secmgr || !newResources.ordinality())
  249. {
  250. return false;
  251. }
  252. const char* basedn = req.getBasedn();
  253. if (!basedn || !*basedn)
  254. {
  255. return false;
  256. }
  257. StringBuffer basednBuf;
  258. basednBuf.append(basedn);
  259. if (existingResource.length() < 1)
  260. {
  261. existingResource.append("files");
  262. const char* comma = strchr(basedn, ',');
  263. const char* eqsign = strchr(basedn, '=');
  264. if(eqsign && comma && (strlen(comma) > 1))
  265. {
  266. basednBuf.clear().append(comma + 1);
  267. }
  268. }
  269. IArrayOf<CPermission> requiredPermissions;
  270. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  271. ldapsecmgr->getPermissionsArray(basednBuf, str2type(req.getRtype()), existingResource.str(), requiredPermissions);
  272. if (!requiredPermissions.ordinality())
  273. {
  274. return false;
  275. }
  276. ForEachItemIn(x, requiredPermissions)
  277. {
  278. CPermission& perm = requiredPermissions.item(x);
  279. int accType = perm.getAccount_type(); //0-individual, 1 - group
  280. const char* actname = perm.getAccount_name();
  281. if (!actname || !*actname)
  282. continue;
  283. CPermissionAction paction;
  284. paction.m_basedn.append(req.getBasedn());
  285. paction.m_rtype = str2type(req.getRtype());
  286. paction.m_account_type = (ACT_TYPE)accType;
  287. paction.m_account_name.append(actname);
  288. paction.m_allows = perm.getAllows();
  289. paction.m_denies = perm.getDenies();
  290. if ((accType != GROUP_ACT) || ((stricmp(actname, "Administrators") != 0) && (stricmp(actname, "Authenticated Users") != 0)))
  291. {
  292. paction.m_action.append("add");
  293. }
  294. else
  295. {
  296. paction.m_action.append("update");
  297. }
  298. ForEachItemIn(y, newResources)
  299. {
  300. StringBuffer namebuf = newResources.item(y);
  301. paction.m_rname.clear().append(namebuf.str());
  302. ldapsecmgr->changePermission(paction);
  303. }
  304. }
  305. return true;
  306. }
  307. bool Cws_accessEx::onUsers(IEspContext &context, IEspUserRequest &req, IEspUserResponse &resp)
  308. {
  309. try
  310. {
  311. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  312. double version = context.getClientVersion();
  313. if (version > 1.03)
  314. {
  315. if(secmgr == NULL)
  316. {
  317. resp.setNoSecMngr(true);
  318. return true;
  319. }
  320. }
  321. else
  322. {
  323. if(secmgr == NULL)
  324. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  325. }
  326. checkUser(context);
  327. const char* searchstr = req.getSearchinput();
  328. int numusers = secmgr->countUsers(searchstr, MAX_USERS_DISPLAY);
  329. if(numusers == -1)
  330. {
  331. resp.setToomany(true);
  332. return true;
  333. }
  334. resp.setToomany(false);
  335. /*
  336. LdapServerType servertype = secmgr->getLdapServerType();
  337. if(servertype != ACTIVE_DIRECTORY)
  338. resp.setPosixok(true);
  339. else
  340. resp.setPosixok(false);
  341. */
  342. resp.setPosixok(false);
  343. IArrayOf<IEspUserInfo> espusers;
  344. IUserArray users;
  345. secmgr->searchUsers(searchstr, users);
  346. ForEachItemIn(x, users)
  347. {
  348. ISecUser* usr = &users.item(x);
  349. if(usr)
  350. {
  351. Owned<IEspUserInfo> oneusr = createUserInfo();
  352. oneusr->setUsername(usr->getName());
  353. oneusr->setFullname(usr->getFullName());
  354. espusers.append(*oneusr.getLink());
  355. }
  356. }
  357. resp.setUsers(espusers);
  358. }
  359. catch(IException* e)
  360. {
  361. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  362. }
  363. return true;
  364. }
  365. bool Cws_accessEx::onUserEdit(IEspContext &context, IEspUserEditRequest &req, IEspUserEditResponse &resp)
  366. {
  367. try
  368. {
  369. checkUser(context);
  370. ISecManager* secmgr = context.querySecManager();
  371. if(secmgr == NULL)
  372. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  373. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  374. resp.setUsername(req.getUsername());
  375. StringArray groupnames;
  376. ldapsecmgr->getGroups(req.getUsername(), groupnames);
  377. IArrayOf<IEspGroupInfo> groups;
  378. for(unsigned i = 0; i < groupnames.length(); i++)
  379. {
  380. const char* grpname = groupnames.item(i);
  381. if(grpname == NULL || grpname[0] == '\0')
  382. continue;
  383. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  384. onegrp->setName(grpname);
  385. groups.append(*onegrp.getLink());
  386. }
  387. resp.setGroups(groups);
  388. }
  389. catch(IException* e)
  390. {
  391. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  392. }
  393. return true;
  394. }
  395. bool Cws_accessEx::onUserGroupEditInput(IEspContext &context, IEspUserGroupEditInputRequest &req, IEspUserGroupEditInputResponse &resp)
  396. {
  397. try
  398. {
  399. checkUser(context);
  400. ISecManager* secmgr = context.querySecManager();
  401. if(secmgr == NULL)
  402. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  403. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  404. resp.setUsername(req.getUsername());
  405. set<string> ogrps;
  406. ogrps.insert("Authenticated Users");
  407. StringArray grps;
  408. ldapsecmgr->getGroups(req.getUsername(), grps);
  409. unsigned i = 0;
  410. for(i = 0; i < grps.length(); i++)
  411. {
  412. const char* grp = grps.item(i);
  413. if(grp != NULL && *grp != '\0')
  414. {
  415. ogrps.insert(grp);
  416. }
  417. }
  418. StringArray groupnames;
  419. ldapsecmgr->getAllGroups(groupnames);
  420. IArrayOf<IEspGroupInfo> groups;
  421. for(i = 0; i < groupnames.length(); i++)
  422. {
  423. const char* grpname = groupnames.item(i);
  424. if(grpname == NULL || grpname[0] == '\0')
  425. continue;
  426. if(ogrps.find(grpname) == ogrps.end())
  427. {
  428. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  429. onegrp->setName(grpname);
  430. groups.append(*onegrp.getLink());
  431. }
  432. }
  433. resp.setGroups(groups);
  434. }
  435. catch(IException* e)
  436. {
  437. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  438. }
  439. return true;
  440. }
  441. bool Cws_accessEx::onUserGroupEdit(IEspContext &context, IEspUserGroupEditRequest &req, IEspUserGroupEditResponse &resp)
  442. {
  443. try
  444. {
  445. checkUser(context);
  446. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  447. if(secmgr == NULL)
  448. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  449. const char* username = req.getUsername();
  450. if(username == NULL || *username == '\0')
  451. {
  452. resp.setRetcode(-1);
  453. resp.setRetmsg("username can't be empty");
  454. return false;
  455. }
  456. StringArray& groupnames = req.getGroupnames();
  457. try
  458. {
  459. for(unsigned i = 0; i < groupnames.length(); i++)
  460. {
  461. const char* grpname = groupnames.item(i);
  462. if(grpname == NULL || *grpname == '\0')
  463. continue;
  464. secmgr->changeUserGroup(req.getAction(), username, grpname);
  465. }
  466. }
  467. catch(IException* e)
  468. {
  469. StringBuffer errmsg;
  470. e->errorMessage(errmsg);
  471. DBGLOG("error changing user's group membership: %s", errmsg.str());
  472. resp.setRetcode(e->errorCode());
  473. resp.setRetmsg(errmsg.str());
  474. return false;
  475. }
  476. resp.setRetcode(0);
  477. resp.setUsername(username);
  478. resp.setAction(req.getAction());
  479. if(stricmp(req.getAction(), "add") == 0)
  480. resp.setRetmsg("user successfully added to groups");
  481. else
  482. resp.setRetmsg("user successfully deleted from groups");
  483. }
  484. catch(IException* e)
  485. {
  486. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  487. }
  488. return true;
  489. }
  490. bool Cws_accessEx::onGroups(IEspContext &context, IEspGroupRequest &req, IEspGroupResponse &resp)
  491. {
  492. try
  493. {
  494. CLdapSecManager* secmgr0 = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  495. double version = context.getClientVersion();
  496. if (version > 1.03)
  497. {
  498. if(secmgr0 == NULL)
  499. {
  500. //throw MakeStringException(-1, "SecManager is NULL, please check if the binding's authentication is set up correctly");
  501. resp.setNoSecMngr(true);
  502. return true;
  503. }
  504. }
  505. checkUser(context);
  506. StringArray groupnames;
  507. ISecManager* secmgr = context.querySecManager();
  508. if(secmgr == NULL)
  509. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  510. secmgr->getAllGroups(groupnames);
  511. ///groupnames.append("Administrators");
  512. ///groupnames.append("Full_Access_TestingOnly");
  513. //groupnames.kill();
  514. if (groupnames.length() > 0)
  515. {
  516. IArrayOf<IEspGroupInfo> groups;
  517. for(unsigned i = 0; i < groupnames.length(); i++)
  518. {
  519. const char* grpname = groupnames.item(i);
  520. //if(grpname == NULL || grpname[0] == '\0' || stricmp(grpname, "Authenticated Users") == 0)
  521. if(grpname == NULL || grpname[0] == '\0')
  522. continue;
  523. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  524. onegrp->setName(grpname);
  525. groups.append(*onegrp.getLink());
  526. }
  527. resp.setGroups(groups);
  528. }
  529. /*
  530. IArrayOf<IEspGroupInfo> groups;
  531. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  532. onegrp->setName("grpname");
  533. groups.append(*onegrp.getLink());
  534. resp.setGroups(groups);
  535. */
  536. }
  537. catch(IException* e)
  538. {
  539. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  540. }
  541. return true;
  542. }
  543. bool Cws_accessEx::onAddUser(IEspContext &context, IEspAddUserRequest &req, IEspAddUserResponse &resp)
  544. {
  545. try
  546. {
  547. checkUser(context);
  548. ISecManager* secmgr = context.querySecManager();
  549. if(secmgr == NULL)
  550. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  551. const char* username = req.getUsername();
  552. if(username == NULL || *username == '\0')
  553. {
  554. resp.setRetcode(-1);
  555. resp.setRetmsg("username can't be empty");
  556. return false;
  557. }
  558. if(strchr(username, ' '))
  559. {
  560. resp.setRetcode(-1);
  561. resp.setRetmsg("Username can't contain spaces");
  562. return false;
  563. }
  564. CLdapSecManager* secmgr0 = (CLdapSecManager*)secmgr;
  565. if((secmgr0->getLdapServerType() == ACTIVE_DIRECTORY) && (strlen(username) > 20))
  566. {
  567. resp.setRetcode(-1);
  568. resp.setRetmsg("Username can't be more than 20 characters.");
  569. return false;
  570. }
  571. const char* pass1 = req.getPassword1();
  572. const char* pass2 = req.getPassword2();
  573. if(pass1 == NULL || pass2 == NULL || *pass1 == '\0' || *pass2 == '\0' || strcmp(pass1, pass2) != 0)
  574. {
  575. resp.setRetcode(-1);
  576. resp.setRetmsg("password and retype can't be empty and must match.");
  577. return false;
  578. }
  579. Owned<ISecUser> user = secmgr->createUser(username);
  580. ISecCredentials& cred = user->credentials();
  581. const char* firstname = req.getFirstname();
  582. const char* lastname = req.getLastname();
  583. if(firstname != NULL)
  584. user->setFirstName(firstname);
  585. if(lastname != NULL)
  586. user->setLastName(lastname);
  587. if(pass1 != NULL)
  588. cred.setPassword(pass1);
  589. try
  590. {
  591. secmgr->addUser(*user.get());
  592. }
  593. catch(IException* e)
  594. {
  595. resp.setRetcode(-1);
  596. StringBuffer errmsg;
  597. resp.setRetmsg(e->errorMessage(errmsg).str());
  598. return false;
  599. }
  600. resp.setRetcode(0);
  601. resp.setRetmsg("User successfully added");
  602. }
  603. catch(IException* e)
  604. {
  605. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  606. }
  607. return true;
  608. }
  609. bool Cws_accessEx::onUserAction(IEspContext &context, IEspUserActionRequest &req, IEspUserActionResponse &resp)
  610. {
  611. try
  612. {
  613. checkUser(context);
  614. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  615. if(secmgr == NULL)
  616. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  617. const char* action = req.getActionType();
  618. if (!action || !*action)
  619. throw MakeStringException(ECLWATCH_INVALID_ACTION, "Action not specified.");
  620. if (!stricmp(action, "delete"))
  621. {
  622. StringArray& usernames = req.getUsernames();
  623. for(unsigned i = 0; i < usernames.length(); i++)
  624. {
  625. const char* username = usernames.item(i);
  626. Owned<ISecUser> user = secmgr->createUser(username);
  627. secmgr->deleteUser(user.get());
  628. }
  629. }
  630. else if (!stricmp(action, "export"))
  631. {
  632. StringBuffer users;
  633. StringArray& usernames = req.getUsernames();
  634. for(unsigned i = 0; i < usernames.length(); i++)
  635. {
  636. const char* username = usernames.item(i);
  637. if (i > 0)
  638. users.appendf("&usernames_i%d=%s", i+1, username);
  639. else
  640. users.append(username);
  641. }
  642. resp.setRedirectUrl(StringBuffer("/ws_access/UserAccountExport?usernames_i1=").append(users).str());
  643. }
  644. resp.setAction(action);
  645. }
  646. catch(IException* e)
  647. {
  648. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  649. }
  650. return true;
  651. }
  652. bool Cws_accessEx::onGroupAdd(IEspContext &context, IEspGroupAddRequest &req, IEspGroupAddResponse &resp)
  653. {
  654. try
  655. {
  656. checkUser(context);
  657. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  658. if(secmgr == NULL)
  659. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  660. const char* groupname = req.getGroupname();
  661. if(groupname == NULL || *groupname == '\0')
  662. {
  663. resp.setRetcode(-1);
  664. resp.setRetmsg("Group name can't be empty");
  665. return false;
  666. }
  667. resp.setGroupname(groupname);
  668. try
  669. {
  670. secmgr->addGroup(groupname);
  671. }
  672. catch(IException* e)
  673. {
  674. StringBuffer emsg;
  675. e->errorMessage(emsg);
  676. resp.setRetcode(e->errorCode());
  677. resp.setRetmsg(emsg.str());
  678. return false;
  679. }
  680. catch(...)
  681. {
  682. resp.setRetcode(-1);
  683. resp.setRetmsg("Unknown error");
  684. return false;
  685. }
  686. resp.setRetcode(0);
  687. }
  688. catch(IException* e)
  689. {
  690. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  691. }
  692. return true;
  693. }
  694. bool Cws_accessEx::onGroupAction(IEspContext &context, IEspGroupActionRequest &req, IEspGroupActionResponse &resp)
  695. {
  696. try
  697. {
  698. checkUser(context);
  699. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  700. if(secmgr == NULL)
  701. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  702. const char* action = req.getActionType();
  703. if (!action || !*action)
  704. throw MakeStringException(ECLWATCH_INVALID_ACTION, "Action not specified.");
  705. if (!stricmp(action, "export"))
  706. {
  707. StringBuffer groups;
  708. StringArray& groupnames = req.getGroupnames();
  709. for(unsigned i = 0; i < groupnames.length(); i++)
  710. {
  711. const char* group = groupnames.item(i);
  712. if (i > 0)
  713. groups.appendf("&groupnames_i%d=%s", i+1, group);
  714. else
  715. groups.append(group);
  716. }
  717. resp.setRedirectUrl(StringBuffer("/ws_access/UserAccountExport?groupnames_i1=").append(groups).str());
  718. }
  719. else if (!stricmp(action, "delete"))
  720. {
  721. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  722. StringArray& groupnames = req.getGroupnames();
  723. IArrayOf<IEspAccountPermission> accountPermissions;
  724. double version = context.getClientVersion();
  725. if (version > 1.01)
  726. {
  727. bool bDeletePermission = false;
  728. if(!req.getDeletePermission_isNull())
  729. bDeletePermission = req.getDeletePermission();
  730. if(m_basedns.length() == 0)
  731. {
  732. setBasedns(context);
  733. }
  734. ForEachItemIn(y, m_basedns)
  735. {
  736. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  737. const char *aBasedn = curbasedn->getBasedn();
  738. const char *aRtype = curbasedn->getRtype();
  739. if (!aBasedn || !*aBasedn ||!aRtype || !*aRtype)
  740. continue;
  741. SecResourceType rtype = str2type(aRtype);
  742. IArrayOf<IEspResource> ResourceArray;
  743. if(rtype == RT_WORKUNIT_SCOPE)
  744. {
  745. StringBuffer deft_basedn, deft_name;
  746. const char* comma = strchr(aBasedn, ',');
  747. const char* eqsign = strchr(aBasedn, '=');
  748. if(eqsign != NULL)
  749. {
  750. if(comma == NULL)
  751. deft_name.append(eqsign+1);
  752. else
  753. {
  754. deft_name.append(comma - eqsign - 1, eqsign+1);
  755. deft_basedn.append(comma + 1);
  756. }
  757. }
  758. if (deft_name.length() > 0)
  759. {
  760. Owned<IEspResource> oneresource = createResource();
  761. oneresource->setName(deft_name);
  762. oneresource->setDescription(deft_basedn);
  763. ResourceArray.append(*oneresource.getLink());
  764. }
  765. }
  766. IArrayOf<ISecResource> resources;
  767. if(secmgr->getResources(rtype, aBasedn, resources))
  768. {
  769. ForEachItemIn(y1, resources)
  770. {
  771. ISecResource& r = resources.item(y1);
  772. const char* rname = r.getName();
  773. if(rname == NULL || *rname == '\0')
  774. continue;
  775. Owned<IEspResource> oneresource = createResource();
  776. oneresource->setName(rname);
  777. oneresource->setDescription(aBasedn);
  778. ResourceArray.append(*oneresource.getLink());
  779. }
  780. }
  781. ForEachItemIn(y2, ResourceArray)
  782. {
  783. IEspResource& r = ResourceArray.item(y2);
  784. const char* rname = r.getName();
  785. const char* bnname = r.getDescription();
  786. if(rname == NULL || *rname == '\0')
  787. continue;
  788. StringBuffer namebuf(rname);
  789. //const char* prefix = req.getPrefix();
  790. //if(prefix && *prefix)
  791. // namebuf.insert(0, prefix);
  792. try
  793. {
  794. IArrayOf<CPermission> permissions;
  795. ldapsecmgr->getPermissionsArray(bnname, rtype, namebuf.str(), permissions);
  796. ForEachItemIn(x, permissions)
  797. {
  798. CPermission& perm = permissions.item(x);
  799. const char* actname = perm.getAccount_name();
  800. int accountType = perm.getAccount_type(); //0-individual, 1 - group
  801. //if ((bGroupAccount && accountType < 1) || (!bGroupAccount && accountType > 0))
  802. if (accountType < 1 || !actname || !*actname) //Support Group only
  803. continue;
  804. ForEachItemIn(x1, groupnames)
  805. {
  806. const char* groupname = groupnames.item(x1);
  807. if (groupname && !strcmp(actname, groupname))
  808. {
  809. ///bDeletePermission = true;
  810. if (!bDeletePermission)
  811. {
  812. Owned<IEspAccountPermission> onepermission = createAccountPermission();
  813. onepermission->setBasedn(bnname);
  814. onepermission->setRType(aRtype);
  815. onepermission->setResourceName(namebuf.str());
  816. onepermission->setPermissionName(groupname);
  817. accountPermissions.append(*onepermission.getLink());
  818. }
  819. else
  820. {
  821. CPermissionAction paction;
  822. paction.m_basedn.append(bnname);
  823. paction.m_rtype = rtype;
  824. paction.m_rname.append(namebuf.str());
  825. paction.m_account_name.append(actname);
  826. paction.m_account_type = (ACT_TYPE) accountType;
  827. paction.m_allows = perm.getAllows();
  828. paction.m_denies = perm.getDenies();
  829. paction.m_action.append("delete");
  830. if(!ldapsecmgr->changePermission(paction))
  831. {
  832. resp.setRetcode(-1);
  833. resp.setRetmsg("Unknown error");
  834. return false;
  835. }
  836. }
  837. break;
  838. }
  839. }
  840. }
  841. }
  842. catch(IException* e)
  843. {
  844. e->Release();
  845. }
  846. }
  847. }
  848. }
  849. try
  850. {
  851. if (accountPermissions.length() < 1)
  852. {
  853. ForEachItemIn(x1, groupnames)
  854. {
  855. const char* groupname = groupnames.item(x1);
  856. secmgr->deleteGroup(groupname);
  857. }
  858. }
  859. else
  860. {
  861. StringBuffer groupnamestr;
  862. groupnamestr.append("DeletePermission=1");
  863. ForEachItemIn(x1, groupnames)
  864. {
  865. const char* groupname = groupnames.item(x1);
  866. groupnamestr.appendf("&groupnames_i%d=%s", x1+1, groupname);
  867. }
  868. resp.setPermissions(accountPermissions);
  869. resp.setGroupnames(groupnamestr.str());
  870. resp.setRetcode(0);
  871. }
  872. }
  873. catch(IException* e)
  874. {
  875. StringBuffer emsg;
  876. e->errorMessage(emsg);
  877. resp.setRetcode(e->errorCode());
  878. resp.setRetmsg(emsg.str());
  879. return false;
  880. }
  881. catch(...)
  882. {
  883. resp.setRetcode(-1);
  884. resp.setRetmsg("Unknown error");
  885. return false;
  886. }
  887. }
  888. resp.setRetcode(0);
  889. }
  890. catch(IException* e)
  891. {
  892. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  893. }
  894. return true;
  895. }
  896. bool Cws_accessEx::onGroupEdit(IEspContext &context, IEspGroupEditRequest &req, IEspGroupEditResponse &resp)
  897. {
  898. try
  899. {
  900. checkUser(context);
  901. ISecManager* secmgr = context.querySecManager();
  902. if(secmgr == NULL)
  903. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  904. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  905. resp.setGroupname(req.getGroupname());
  906. StringArray usernames;
  907. ldapsecmgr->getGroupMembers(req.getGroupname(), usernames);
  908. IArrayOf<IEspUserInfo> users;
  909. unsigned i = 0;
  910. for(i = 0; i < usernames.length(); i++)
  911. {
  912. const char* usrname = usernames.item(i);
  913. if(usrname == NULL || usrname[0] == '\0')
  914. continue;
  915. ///////////////////////////////////////BUG#41536///////////////
  916. bool bFound = false;
  917. IUserArray usersInBaseDN;
  918. ldapsecmgr->searchUsers(usrname, usersInBaseDN);
  919. ForEachItemIn(x, usersInBaseDN)
  920. {
  921. ISecUser* usr = &usersInBaseDN.item(x);
  922. if(usr)
  923. {
  924. const char* usrname = usr->getName();
  925. if(usrname == NULL || usrname[0] == '\0')
  926. continue;
  927. bFound = true;
  928. break;
  929. }
  930. }
  931. if (!bFound)
  932. continue;
  933. //////////////////////////////////////////////////////////////
  934. Owned<IEspUserInfo> oneusr = createUserInfo();
  935. oneusr->setUsername(usrname);
  936. users.append(*oneusr.getLink());
  937. }
  938. resp.setUsers(users);
  939. }
  940. catch(IException* e)
  941. {
  942. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  943. }
  944. return true;
  945. }
  946. bool Cws_accessEx::onGroupMemberEditInput(IEspContext &context, IEspGroupMemberEditInputRequest &req, IEspGroupMemberEditInputResponse &resp)
  947. {
  948. try
  949. {
  950. checkUser(context);
  951. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  952. if(secmgr == NULL)
  953. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  954. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  955. resp.setGroupname(req.getGroupname());
  956. set<string> ousrs;
  957. StringArray ousernames;
  958. ldapsecmgr->getGroupMembers(req.getGroupname(), ousernames);
  959. unsigned i = 0;
  960. for(i = 0; i < ousernames.length(); i++)
  961. {
  962. const char* username = ousernames.item(i);
  963. if(username != NULL && *username != '\0')
  964. {
  965. ousrs.insert(username);
  966. }
  967. }
  968. const char* searchstr = req.getSearchinput();
  969. int numusers = secmgr->countUsers(searchstr, MAX_USERS_DISPLAY+ousernames.ordinality());
  970. if(numusers == -1)
  971. {
  972. resp.setToomany(true);
  973. return true;
  974. }
  975. resp.setToomany(false);
  976. IArrayOf<IEspUserInfo> espusers;
  977. IUserArray users;
  978. secmgr->searchUsers(searchstr, users);
  979. ForEachItemIn(x, users)
  980. {
  981. ISecUser* usr = &users.item(x);
  982. if(usr)
  983. {
  984. const char* usrname = usr->getName();
  985. if(usrname == NULL || usrname[0] == '\0')
  986. continue;
  987. if(ousrs.find(usrname) == ousrs.end())
  988. {
  989. Owned<IEspUserInfo> oneusr = createUserInfo();
  990. oneusr->setUsername(usr->getName());
  991. espusers.append(*oneusr.getLink());
  992. }
  993. }
  994. }
  995. resp.setUsers(espusers);
  996. }
  997. catch(IException* e)
  998. {
  999. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1000. }
  1001. return true;
  1002. }
  1003. bool Cws_accessEx::onGroupMemberEdit(IEspContext &context, IEspGroupMemberEditRequest &req, IEspGroupMemberEditResponse &resp)
  1004. {
  1005. try
  1006. {
  1007. checkUser(context);
  1008. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  1009. if(secmgr == NULL)
  1010. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1011. const char* groupname = req.getGroupname();
  1012. if(groupname == NULL || *groupname == '\0')
  1013. {
  1014. resp.setRetcode(-1);
  1015. resp.setRetmsg("group can't be empty");
  1016. return false;
  1017. }
  1018. ///////////////////////////////////////BUG#41536///////////////
  1019. StringArray existing_usernames;
  1020. if (!stricmp(req.getAction(), "add"))
  1021. secmgr->getGroupMembers(groupname, existing_usernames);
  1022. //////////////////////////////////////////////////////
  1023. StringArray& usernames = req.getUsernames();
  1024. try
  1025. {
  1026. for(unsigned i = 0; i < usernames.length(); i++)
  1027. {
  1028. const char* usrname = usernames.item(i);
  1029. if(usrname == NULL || *usrname == '\0')
  1030. continue;
  1031. ///////////////////////////////////////BUG#41536///////////////
  1032. bool bFound = false;
  1033. if (existing_usernames.length() > 0)
  1034. {
  1035. for(unsigned i = 0; i < existing_usernames.length(); i++)
  1036. {
  1037. const char* existing_usrname = existing_usernames.item(i);
  1038. if(existing_usrname == NULL || existing_usrname[0] == '\0')
  1039. continue;
  1040. if (!strcmp(usrname, existing_usrname))
  1041. {
  1042. bFound = true;
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. if (!bFound)
  1048. //////////////////////////////////////////////////////
  1049. secmgr->changeUserGroup(req.getAction(), usrname, groupname);
  1050. }
  1051. }
  1052. catch(IException* e)
  1053. {
  1054. StringBuffer errmsg;
  1055. e->errorMessage(errmsg);
  1056. DBGLOG("error changing user's group membership: %s", errmsg.str());
  1057. resp.setRetcode(e->errorCode());
  1058. resp.setRetmsg(errmsg.str());
  1059. return false;
  1060. }
  1061. resp.setRetcode(0);
  1062. resp.setGroupname(groupname);
  1063. resp.setAction(req.getAction());
  1064. if(stricmp(req.getAction(), "add") == 0)
  1065. resp.setRetmsg("members successfully added to group");
  1066. else
  1067. resp.setRetmsg("members successfully deleted from group");
  1068. }
  1069. catch(IException* e)
  1070. {
  1071. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1072. }
  1073. return true;
  1074. }
  1075. bool Cws_accessEx::onPermissions(IEspContext &context, IEspBasednsRequest &req, IEspBasednsResponse &resp)
  1076. {
  1077. try
  1078. {
  1079. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  1080. double version = context.getClientVersion();
  1081. if (version > 1.03)
  1082. {
  1083. if(secmgr == NULL)
  1084. {
  1085. resp.setNoSecMngr(true);
  1086. return true;
  1087. }
  1088. }
  1089. else
  1090. {
  1091. if(secmgr == NULL)
  1092. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1093. }
  1094. checkUser(context);
  1095. if(m_basedns.length() == 0)
  1096. {
  1097. setBasedns(context);
  1098. }
  1099. resp.setBasedns(m_basedns);
  1100. }
  1101. catch(IException* e)
  1102. {
  1103. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1104. }
  1105. return true;
  1106. }
  1107. bool Cws_accessEx::onResources(IEspContext &context, IEspResourcesRequest &req, IEspResourcesResponse &resp)
  1108. {
  1109. try
  1110. {
  1111. checkUser(context);
  1112. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  1113. if(secmgr == NULL)
  1114. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1115. double version = context.getClientVersion();
  1116. const char* filterInput = req.getSearchinput();
  1117. const char* basedn = req.getBasedn();
  1118. const char* rtypestr = req.getRtype();
  1119. const char* moduletemplate = NULL;
  1120. ForEachItemIn(x, m_basedns)
  1121. {
  1122. IEspDnStruct* curbasedn = &(m_basedns.item(x));
  1123. if(stricmp(curbasedn->getBasedn(), basedn) == 0)
  1124. {
  1125. moduletemplate = curbasedn->getTemplatename();
  1126. }
  1127. }
  1128. resp.setBasedn(basedn);
  1129. resp.setRtype(rtypestr);
  1130. resp.setRtitle(req.getRtitle());
  1131. SecResourceType rtype = str2type(rtypestr);
  1132. if(rtype == RT_FILE_SCOPE || rtype == RT_WORKUNIT_SCOPE)
  1133. {
  1134. StringBuffer deft_basedn, deft_name;
  1135. const char* comma = strchr(basedn, ',');
  1136. const char* eqsign = strchr(basedn, '=');
  1137. if(eqsign != NULL)
  1138. {
  1139. if(comma == NULL)
  1140. deft_name.append(eqsign+1);
  1141. else
  1142. {
  1143. deft_name.append(comma - eqsign - 1, eqsign+1);
  1144. deft_basedn.append(comma + 1);
  1145. }
  1146. resp.setDefault_basedn(deft_basedn.str());
  1147. resp.setDefault_name(deft_name.str());
  1148. }
  1149. }
  1150. IArrayOf<IEspResource> rarray;
  1151. IArrayOf<ISecResource> resources;
  1152. const char* prefix = req.getPrefix();
  1153. int prefixlen = 0;
  1154. if(prefix && *prefix)
  1155. {
  1156. prefixlen = strlen(prefix);
  1157. resp.setPrefix(prefix);
  1158. }
  1159. if (version > 1.04)
  1160. {
  1161. int numResources = -1;
  1162. if (req.getRtitle() && !stricmp(req.getRtitle(), "CodeGenerator Permission"))
  1163. numResources = secmgr->countResources(basedn, prefix, MAX_RESOURCES_DISPLAY);
  1164. else
  1165. numResources = secmgr->countResources(basedn, filterInput, MAX_RESOURCES_DISPLAY);
  1166. if(numResources == -1)
  1167. {
  1168. resp.setToomany(true);
  1169. return true;
  1170. }
  1171. else
  1172. {
  1173. resp.setToomany(false);
  1174. }
  1175. }
  1176. if ((!filterInput || !*filterInput) && req.getRtitle() && !stricmp(req.getRtitle(), "CodeGenerator Permission"))
  1177. {
  1178. if(!secmgr->getResourcesEx(rtype, basedn, prefix, resources))
  1179. return false;
  1180. }
  1181. else
  1182. {
  1183. if(!secmgr->getResourcesEx(rtype, basedn, filterInput, resources))
  1184. return false;
  1185. }
  1186. ILdapConfig* cfg = secmgr->queryConfig();
  1187. for(unsigned i = 0; i < resources.length(); i++)
  1188. {
  1189. ISecResource& r = resources.item(i);
  1190. Owned<IEspResource> oneresource = createResource();
  1191. oneresource->setIsSpecial(false);
  1192. const char* rname = r.getName();
  1193. if(rname == NULL || *rname == '\0')
  1194. continue;
  1195. if(prefix && *prefix)
  1196. {
  1197. if(strncmp(prefix, rname, prefixlen) != 0)
  1198. continue;
  1199. else
  1200. rname += prefixlen;
  1201. }
  1202. if(rtype == RT_MODULE)
  1203. {
  1204. if(stricmp(rname, "repository") != 0)
  1205. {
  1206. if(moduletemplate != NULL && stricmp(rname, moduletemplate) == 0)
  1207. oneresource->setIsSpecial(true);
  1208. if(Utils::strncasecmp(rname, "repository.", 11) == 0)
  1209. rname = rname + 11;
  1210. else
  1211. continue;
  1212. }
  1213. else
  1214. {
  1215. oneresource->setIsSpecial(true);
  1216. }
  1217. }
  1218. else if(rtype == RT_FILE_SCOPE && stricmp(rname, "file") == 0)
  1219. {
  1220. //oneresource->setIsSpecial(true); //33067
  1221. continue;
  1222. }
  1223. oneresource->setName(rname);
  1224. oneresource->setDescription(r.getDescription());
  1225. rarray.append(*oneresource.getLink());
  1226. }
  1227. resp.setResources(rarray);
  1228. }
  1229. catch(IException* e)
  1230. {
  1231. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1232. }
  1233. return true;
  1234. }
  1235. bool Cws_accessEx::onResourceAddInput(IEspContext &context, IEspResourceAddInputRequest &req, IEspResourceAddInputResponse &resp)
  1236. {
  1237. try
  1238. {
  1239. checkUser(context);
  1240. resp.setBasedn(req.getBasedn());
  1241. resp.setRtype(req.getRtype());
  1242. resp.setRtitle(req.getRtitle());
  1243. resp.setPrefix(req.getPrefix());
  1244. }
  1245. catch(IException* e)
  1246. {
  1247. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1248. }
  1249. return true;
  1250. }
  1251. SecResourceType Cws_accessEx::str2type(const char* rtstr)
  1252. {
  1253. if(rtstr == NULL || *rtstr == '\0')
  1254. return RT_DEFAULT;
  1255. else if(stricmp(rtstr, "module") == 0)
  1256. return RT_MODULE;
  1257. else if(stricmp(rtstr, "service") == 0)
  1258. return RT_SERVICE;
  1259. else if(stricmp(rtstr, "file") == 0)
  1260. return RT_FILE_SCOPE;
  1261. else if(stricmp(rtstr, "workunit") == 0)
  1262. return RT_WORKUNIT_SCOPE;
  1263. else
  1264. return RT_DEFAULT;
  1265. }
  1266. bool Cws_accessEx::onResourceAdd(IEspContext &context, IEspResourceAddRequest &req, IEspResourceAddResponse &resp)
  1267. {
  1268. try
  1269. {
  1270. checkUser(context);
  1271. ISecManager* secmgr = context.querySecManager();
  1272. if(secmgr == NULL)
  1273. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1274. resp.setBasedn(req.getBasedn());
  1275. resp.setRtype(req.getRtype());
  1276. resp.setRtitle(req.getRtitle());
  1277. resp.setPrefix(req.getPrefix());
  1278. StringBuffer lastResource;
  1279. StringArray newResources;
  1280. if(str2type(req.getRtype()) == RT_FILE_SCOPE)
  1281. {
  1282. getNewFileScopePermissions(secmgr, req, lastResource, newResources);
  1283. }
  1284. SecResourceType rtype = str2type(req.getRtype());
  1285. try
  1286. {
  1287. ISecUser* usr = NULL;
  1288. Owned<ISecResourceList> rlist = secmgr->createResourceList("ws_access");
  1289. const char* name = req.getName();
  1290. if(name == NULL || *name == '\0')
  1291. {
  1292. resp.setRetcode(-1);
  1293. StringBuffer errmsg;
  1294. errmsg.append(req.getRtitle()).append(" name can't be empty");
  1295. resp.setRetmsg(errmsg.str());
  1296. return false;
  1297. }
  1298. if(strchr(name, '\\') != NULL || strchr(name, '/') != NULL)
  1299. {
  1300. resp.setRetcode(-1);
  1301. StringBuffer errmsg;
  1302. errmsg.append(" you can't have '\\' or '/' in the name");
  1303. resp.setRetmsg(errmsg.str());
  1304. return false;
  1305. }
  1306. const char* ptr = strchr(name, ':');
  1307. while(ptr != NULL)
  1308. {
  1309. if(*(ptr+1) != ':')
  1310. throw MakeStringException(ECLWATCH_SINGLE_COLON_NOT_ALLOWED, "Single colon is not allowed in scope names. Please use double colon");
  1311. ptr = strchr(ptr+2, ':');
  1312. }
  1313. StringBuffer namebuf(name);
  1314. if(rtype == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  1315. namebuf.insert(0, "repository.");
  1316. const char* prefix = req.getPrefix();
  1317. if(prefix && *prefix)
  1318. namebuf.insert(0, prefix);
  1319. ISecResource* r = rlist->addResource(namebuf.str());
  1320. r->setDescription(req.getDescription());
  1321. secmgr->addResourcesEx(rtype, *usr, rlist, PT_DEFAULT, req.getBasedn());
  1322. if(str2type(req.getRtype()) == RT_FILE_SCOPE && newResources.ordinality())
  1323. {
  1324. setNewFileScopePermissions(secmgr, req, lastResource, newResources);
  1325. StringBuffer retmsg;
  1326. ForEachItemIn(y, newResources)
  1327. {
  1328. StringBuffer namebuf = newResources.item(y);
  1329. if (retmsg.length() < 1)
  1330. retmsg.append(namebuf);
  1331. else
  1332. retmsg.appendf(", %s", namebuf.str());
  1333. }
  1334. resp.setRetmsg(retmsg.str());
  1335. }
  1336. }
  1337. catch(IException* e)
  1338. {
  1339. StringBuffer emsg;
  1340. e->errorMessage(emsg);
  1341. resp.setRetcode(e->errorCode());
  1342. resp.setRetmsg(emsg.str());
  1343. return false;
  1344. }
  1345. catch(...)
  1346. {
  1347. resp.setRetcode(-1);
  1348. resp.setRetmsg("unknown error");
  1349. return false;
  1350. }
  1351. resp.setRetcode(0);
  1352. }
  1353. catch(IException* e)
  1354. {
  1355. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1356. }
  1357. return true;
  1358. }
  1359. bool Cws_accessEx::onResourceDelete(IEspContext &context, IEspResourceDeleteRequest &req, IEspResourceDeleteResponse &resp)
  1360. {
  1361. try
  1362. {
  1363. checkUser(context);
  1364. CLdapSecManager* secmgr = (CLdapSecManager*)(context.querySecManager());
  1365. if(secmgr == NULL)
  1366. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1367. StringArray& names = req.getNames();
  1368. int doUpdate = req.getDoUpdate();
  1369. if (doUpdate)
  1370. {
  1371. const char* basedn = req.getBasedn();
  1372. const char* rtype = req.getRtype();
  1373. const char* rtitle = req.getRtitle();
  1374. const char* prefix = req.getPrefix();
  1375. StringBuffer url("/ws_access/PermissionsResetInput");
  1376. url.appendf("?basedn=%s", basedn);
  1377. url.appendf("&rtype=%s", rtype);
  1378. url.appendf("&rtitle=%s", rtitle);
  1379. url.appendf("&prefix=%s", prefix);
  1380. if (names.length() < 1)
  1381. throw MakeStringException(ECLWATCH_INVALID_RESOURCE_NAME, "Please select a resource name.");
  1382. for(unsigned i = 0; i < names.length(); i++)
  1383. {
  1384. const char* name = names.item(i);
  1385. if(name == NULL || *name == '\0')
  1386. continue;
  1387. url.appendf("&names_i%d=%s", i, name);
  1388. }
  1389. resp.setRedirectUrl(url);
  1390. return true;
  1391. }
  1392. resp.setBasedn(req.getBasedn());
  1393. resp.setRtype(req.getRtype());
  1394. resp.setRtitle(req.getRtitle());
  1395. resp.setPrefix(req.getPrefix());
  1396. SecResourceType rtype = str2type(req.getRtype());
  1397. try
  1398. {
  1399. for(unsigned i = 0; i < names.length(); i++)
  1400. {
  1401. const char* name = names.item(i);
  1402. if(name == NULL || *name == '\0')
  1403. continue;
  1404. StringBuffer namebuf(name);
  1405. if(rtype == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  1406. namebuf.insert(0, "repository.");
  1407. const char* prefix = req.getPrefix();
  1408. if(prefix && *prefix)
  1409. namebuf.insert(0, prefix);
  1410. secmgr->deleteResource(rtype, namebuf.str(), req.getBasedn());
  1411. }
  1412. }
  1413. catch(IException* e)
  1414. {
  1415. StringBuffer emsg;
  1416. e->errorMessage(emsg);
  1417. resp.setRetcode(e->errorCode());
  1418. resp.setRetmsg(emsg.str());
  1419. return false;
  1420. }
  1421. catch(...)
  1422. {
  1423. resp.setRetcode(-1);
  1424. resp.setRetmsg("Unknown error");
  1425. return false;
  1426. }
  1427. resp.setRetcode(0);
  1428. }
  1429. catch(IException* e)
  1430. {
  1431. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1432. }
  1433. return true;
  1434. }
  1435. bool Cws_accessEx::onResourcePermissions(IEspContext &context, IEspResourcePermissionsRequest &req, IEspResourcePermissionsResponse &resp)
  1436. {
  1437. try
  1438. {
  1439. checkUser(context);
  1440. ISecManager* secmgr = context.querySecManager();
  1441. if(secmgr == NULL)
  1442. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1443. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  1444. const char* name = req.getName();
  1445. StringBuffer namebuf(name);
  1446. if(str2type(req.getRtype()) == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  1447. namebuf.insert(0, "repository.");
  1448. const char* prefix = req.getPrefix();
  1449. if(prefix && *prefix)
  1450. namebuf.insert(0, prefix);
  1451. IArrayOf<CPermission> permissions;
  1452. ldapsecmgr->getPermissionsArray(req.getBasedn(), str2type(req.getRtype()), namebuf.str(), permissions);
  1453. IArrayOf<IEspResourcePermission> parray;
  1454. ForEachItemIn(x, permissions)
  1455. {
  1456. CPermission& perm = permissions.item(x);
  1457. Owned<IEspResourcePermission> onepermission = createResourcePermission();
  1458. const char* actname = perm.getAccount_name();
  1459. if(actname != NULL && *actname != '\0')
  1460. {
  1461. StringBuffer escapedname;
  1462. int i = 0;
  1463. char c;
  1464. while((c = actname[i++]) != '\0')
  1465. {
  1466. if(c == '\'')
  1467. escapedname.append('\\').append('\'');
  1468. else
  1469. escapedname.append(c);
  1470. }
  1471. onepermission->setAccount_name(actname);
  1472. onepermission->setEscaped_account_name(escapedname.str());
  1473. }
  1474. onepermission->setAccount_type(perm.getAccount_type());
  1475. int allows = perm.getAllows();
  1476. int denies = perm.getDenies();
  1477. if((allows & NewSecAccess_Access) == NewSecAccess_Access)
  1478. onepermission->setAllow_access(true);
  1479. if((allows & NewSecAccess_Read) == NewSecAccess_Read)
  1480. onepermission->setAllow_read(true);
  1481. if((allows & NewSecAccess_Write) == NewSecAccess_Write)
  1482. onepermission->setAllow_write(true);
  1483. if((allows & NewSecAccess_Full) == NewSecAccess_Full)
  1484. onepermission->setAllow_full(true);
  1485. if((denies & NewSecAccess_Access) == NewSecAccess_Access)
  1486. onepermission->setDeny_access(true);
  1487. if((denies & NewSecAccess_Read) == NewSecAccess_Read)
  1488. onepermission->setDeny_read(true);
  1489. if((denies & NewSecAccess_Write) == NewSecAccess_Write)
  1490. onepermission->setDeny_write(true);
  1491. if((denies & NewSecAccess_Full) == NewSecAccess_Full)
  1492. onepermission->setDeny_full(true);
  1493. parray.append(*onepermission.getLink());
  1494. }
  1495. resp.setBasedn(req.getBasedn());
  1496. resp.setRtype(req.getRtype());
  1497. resp.setRtitle(req.getRtitle());
  1498. resp.setName(req.getName());
  1499. resp.setPrefix(req.getPrefix());
  1500. resp.setPermissions(parray);
  1501. }
  1502. catch(IException* e)
  1503. {
  1504. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1505. }
  1506. return true;
  1507. }
  1508. bool Cws_accessEx::onPermissionAddInput(IEspContext &context, IEspPermissionAddRequest &req, IEspPermissionAddResponse &resp)
  1509. {
  1510. try
  1511. {
  1512. checkUser(context);
  1513. resp.setBasedn(req.getBasedn());
  1514. resp.setRname(req.getRname());
  1515. resp.setRtype(req.getRtype());
  1516. resp.setRtitle(req.getRtitle());
  1517. resp.setPrefix(req.getPrefix());
  1518. double version = context.getClientVersion();
  1519. if (version < 1.01)
  1520. {
  1521. return permissionAddInputOnResource(context, req, resp);
  1522. }
  1523. else
  1524. {
  1525. const char* accountName = req.getAccountName();
  1526. if (!accountName || !*accountName)
  1527. {
  1528. return permissionAddInputOnResource(context, req, resp);
  1529. }
  1530. else
  1531. {
  1532. return permissionAddInputOnAccount(context, accountName, req, resp);
  1533. }
  1534. }
  1535. }
  1536. catch(IException* e)
  1537. {
  1538. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1539. }
  1540. return true;
  1541. }
  1542. bool Cws_accessEx::onPermissionsResetInput(IEspContext &context, IEspPermissionsResetInputRequest &req, IEspPermissionsResetInputResponse &resp)
  1543. {
  1544. try
  1545. {
  1546. checkUser(context);
  1547. resp.setBasedn(req.getBasedn());
  1548. //resp.setRname(req.getRname());
  1549. resp.setRname("Test");
  1550. resp.setRtype(req.getRtype());
  1551. resp.setRtitle(req.getRtitle());
  1552. resp.setPrefix(req.getPrefix());
  1553. StringArray& names = req.getNames();
  1554. if (names.length() < 1)
  1555. throw MakeStringException(ECLWATCH_INVALID_PERMISSION_NAME, "Please select a permission.");
  1556. StringBuffer nameList; //For forwarding to Submit page
  1557. StringArray names1;
  1558. ForEachItemIn(k, names)
  1559. {
  1560. const char* name1 = names.item(k);
  1561. nameList.appendf("%s,", name1);
  1562. names1.append(name1);
  1563. }
  1564. resp.setResourceList(nameList.str());
  1565. resp.setResources(names);
  1566. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  1567. if(secmgr == NULL)
  1568. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1569. int numusers = secmgr->countUsers("", MAX_USERS_DISPLAY);
  1570. if(numusers == -1)
  1571. {
  1572. resp.setToomany(true);
  1573. }
  1574. else
  1575. {
  1576. resp.setToomany(false);
  1577. IArrayOf<IEspUserInfo> espusers;
  1578. IUserArray users;
  1579. secmgr->getAllUsers(users);
  1580. ForEachItemIn(x, users)
  1581. {
  1582. CLdapSecUser* usr = dynamic_cast<CLdapSecUser*>(&users.item(x));
  1583. if(usr)
  1584. {
  1585. Owned<IEspUserInfo> oneusr = createUserInfo();
  1586. oneusr->setUsername(usr->getName());
  1587. oneusr->setFullname(usr->getFullName());
  1588. espusers.append(*oneusr.getLink());
  1589. }
  1590. }
  1591. resp.setUsers(espusers);
  1592. }
  1593. IArrayOf<IEspGroupInfo> groups;
  1594. if(secmgr->getLdapServerType() != ACTIVE_DIRECTORY)
  1595. {
  1596. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  1597. onegrp->setName("anyone");
  1598. groups.append(*onegrp.getLink());
  1599. }
  1600. StringArray grpnames;
  1601. secmgr->getAllGroups(grpnames);
  1602. for(unsigned i = 0; i < grpnames.length(); i++)
  1603. {
  1604. const char* grpname = grpnames.item(i);
  1605. if(grpname == NULL || *grpname == '\0')
  1606. continue;
  1607. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  1608. onegrp->setName(grpname);
  1609. groups.append(*onegrp.getLink());
  1610. }
  1611. resp.setGroups(groups);
  1612. }
  1613. catch(IException* e)
  1614. {
  1615. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1616. }
  1617. return true;
  1618. }
  1619. bool Cws_accessEx::permissionsReset(CLdapSecManager* ldapsecmgr, const char* basedn, const char* rtype0, const char* prefix,
  1620. const char* resourceName, ACT_TYPE accountType, const char* accountName,
  1621. bool allow_access, bool allow_read, bool allow_write, bool allow_full,
  1622. bool deny_access, bool deny_read, bool deny_write, bool deny_full)
  1623. {
  1624. CPermissionAction paction;
  1625. paction.m_basedn.append(basedn);
  1626. //const char* name = req.getRname();
  1627. StringBuffer namebuf(resourceName);
  1628. SecResourceType rtype = str2type(rtype0);
  1629. if(rtype == RT_MODULE && stricmp(resourceName, "repository") != 0 && Utils::strncasecmp(resourceName, "repository.", 11) != 0)
  1630. namebuf.insert(0, "repository.");
  1631. if(prefix && *prefix)
  1632. namebuf.insert(0, prefix);
  1633. paction.m_rname.append(namebuf.str());
  1634. paction.m_rtype = str2type(rtype0);
  1635. paction.m_allows = 0;
  1636. paction.m_denies = 0;
  1637. if(allow_full)
  1638. paction.m_allows |= NewSecAccess_Full;
  1639. if(allow_read)
  1640. paction.m_allows |= NewSecAccess_Read;
  1641. if(allow_write)
  1642. paction.m_allows |= NewSecAccess_Write;
  1643. if(allow_access)
  1644. paction.m_allows |= NewSecAccess_Access;
  1645. if(deny_full)
  1646. paction.m_denies |= NewSecAccess_Full;
  1647. if(deny_read)
  1648. paction.m_denies |= NewSecAccess_Read;
  1649. if(deny_write)
  1650. paction.m_denies |= NewSecAccess_Write;
  1651. if(deny_access)
  1652. paction.m_denies |= NewSecAccess_Access;
  1653. paction.m_action.append("update");
  1654. paction.m_account_type = accountType;
  1655. paction.m_account_name.append(accountName);
  1656. bool ret = ldapsecmgr->changePermission(paction);
  1657. return ret;
  1658. }
  1659. bool Cws_accessEx::onPermissionsReset(IEspContext &context, IEspPermissionsResetRequest &req, IEspPermissionsResetResponse &resp)
  1660. {
  1661. try
  1662. {
  1663. checkUser(context);
  1664. resp.setBasedn(req.getBasedn());
  1665. resp.setRname(req.getRname());
  1666. resp.setRtype(req.getRtype());
  1667. resp.setRtitle(req.getRtitle());
  1668. resp.setPrefix(req.getPrefix());
  1669. ISecManager* secmgr = context.querySecManager();
  1670. if(secmgr == NULL)
  1671. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1672. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  1673. const char* users = req.getUserarray();
  1674. const char* groups = req.getGrouparray();
  1675. if ((!users || !*users) && (!groups || !*groups))
  1676. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "A user or group must be specified.");
  1677. StringArray& resources = req.getNames();
  1678. if (resources.length() < 1)
  1679. throw MakeStringException(ECLWATCH_INVALID_RESOURCE_NAME, "A resource name must be specified.");
  1680. StringArray userAccounts, groupAccounts;
  1681. if (users && *users)
  1682. {
  1683. char* pTr = (char*) users;
  1684. while (pTr)
  1685. {
  1686. char* ppTr = strchr(pTr, ',');
  1687. if (!ppTr)
  1688. break;
  1689. if (ppTr - pTr > 1)
  1690. {
  1691. char userName[255];
  1692. strncpy(userName, pTr, ppTr - pTr);
  1693. userName[ppTr - pTr] = 0;
  1694. userAccounts.append(userName);
  1695. }
  1696. pTr = ppTr+1;
  1697. }
  1698. }
  1699. if (groups && *groups)
  1700. {
  1701. char* pTr = (char*) groups;
  1702. while (pTr)
  1703. {
  1704. char* ppTr = strchr(pTr, ',');
  1705. if (!ppTr)
  1706. break;
  1707. if (ppTr - pTr > 1)
  1708. {
  1709. char userName[255];
  1710. strncpy(userName, pTr, ppTr - pTr);
  1711. userName[ppTr - pTr] = 0;
  1712. groupAccounts.append(userName);
  1713. }
  1714. pTr = ppTr+1;
  1715. }
  1716. }
  1717. if (userAccounts.length() < 1 && groupAccounts.length() < 1)
  1718. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "A user or group must be specified.");
  1719. for(unsigned i = 0; i < resources.length(); i++)
  1720. {
  1721. const char* name = resources.item(i);
  1722. if (!name || !*name)
  1723. continue;
  1724. bool ret = true;
  1725. StringBuffer retmsg;
  1726. try
  1727. {
  1728. if (userAccounts.length() > 0)
  1729. {
  1730. for(unsigned j = 0; j < userAccounts.length(); j++)
  1731. {
  1732. const char* name0 = userAccounts.item(j);
  1733. if (!name0 || !*name0)
  1734. continue;
  1735. ret = permissionsReset(ldapsecmgr, req.getBasedn(), req.getRtype(), req.getPrefix(), name, USER_ACT, name0,
  1736. req.getAllow_access(), req.getAllow_read(), req.getAllow_write(), req.getAllow_full(),
  1737. req.getDeny_access(), req.getDeny_read(), req.getDeny_write(), req.getDeny_full());
  1738. if(!ret)
  1739. {
  1740. resp.setRetcode(-1);
  1741. resp.setRetmsg("Unknown error");
  1742. return false;
  1743. }
  1744. }
  1745. }
  1746. if (groupAccounts.length() > 0)
  1747. {
  1748. for(unsigned j = 0; j < groupAccounts.length(); j++)
  1749. {
  1750. const char* name0 = groupAccounts.item(j);
  1751. if (!name0 || !*name0)
  1752. continue;
  1753. ret = permissionsReset(ldapsecmgr, req.getBasedn(), req.getRtype(), req.getPrefix(), name, GROUP_ACT, name0,
  1754. req.getAllow_access(), req.getAllow_read(), req.getAllow_write(), req.getAllow_full(),
  1755. req.getDeny_access(), req.getDeny_read(), req.getDeny_write(), req.getDeny_full());
  1756. if(!ret)
  1757. {
  1758. resp.setRetcode(-1);
  1759. resp.setRetmsg("Unknown error");
  1760. return false;
  1761. }
  1762. }
  1763. }
  1764. }
  1765. catch(IException* e)
  1766. {
  1767. resp.setRetcode(e->errorCode());
  1768. e->errorMessage(retmsg);
  1769. resp.setRetmsg(retmsg.str());
  1770. return false;
  1771. }
  1772. }
  1773. resp.setRetcode(0);
  1774. }
  1775. catch(IException* e)
  1776. {
  1777. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1778. }
  1779. return true;
  1780. }
  1781. //For every resources inside a baseDN, if there is no permission for this account, add the baseDN name to the basednNames list
  1782. void Cws_accessEx::getBaseDNsForAddingPermssionToAccount(CLdapSecManager* secmgr, const char* prefix, const char* accountName,
  1783. int accountType, StringArray& basednNames)
  1784. {
  1785. if(secmgr == NULL)
  1786. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1787. ForEachItemIn(i, m_basedns)
  1788. {
  1789. IEspDnStruct* curbasedn = &(m_basedns.item(i));
  1790. const char *basednName = curbasedn->getName();
  1791. if (!basednName || !*basednName)
  1792. continue;
  1793. const char *basedn = curbasedn->getBasedn();
  1794. const char *rtypestr = curbasedn->getRtype();
  1795. if (!basedn || !*basedn || !rtypestr || !*rtypestr)
  1796. continue;
  1797. IArrayOf<ISecResource> resources;
  1798. SecResourceType rtype = str2type(rtypestr);
  1799. if(!secmgr->getResources(rtype, basedn, resources))
  1800. continue;
  1801. ForEachItemIn(j, resources)
  1802. {
  1803. ISecResource& r = resources.item(j);
  1804. const char* rname = r.getName();
  1805. if(!rname || !*rname)
  1806. continue;
  1807. if(prefix && *prefix)
  1808. {
  1809. int prefixlen = strlen(prefix);
  1810. if(strncmp(prefix, rname, prefixlen) == 0)
  1811. rname += prefixlen;
  1812. }
  1813. StringBuffer namebuf(rname);
  1814. if((rtype == RT_MODULE) && !strieq(rname, "repository") && Utils::strncasecmp(rname, "repository.", 11) != 0)
  1815. namebuf.insert(0, "repository.");
  1816. if(prefix && *prefix)
  1817. namebuf.insert(0, prefix);
  1818. try
  1819. {
  1820. IArrayOf<CPermission> permissions;
  1821. secmgr->getPermissionsArray(basedn, rtype, namebuf.str(), permissions);
  1822. bool foundPermissionInThisAccount = false;
  1823. ForEachItemIn(k, permissions)
  1824. {
  1825. CPermission& perm = permissions.item(k);
  1826. if ((accountType == perm.getAccount_type()) && perm.getAccount_name() && streq(perm.getAccount_name(), accountName))
  1827. {
  1828. foundPermissionInThisAccount = true;
  1829. break;
  1830. }
  1831. }
  1832. if (!foundPermissionInThisAccount)
  1833. {
  1834. basednNames.append(basednName);
  1835. break;
  1836. }
  1837. }
  1838. catch(IException* e) //exception may be thrown when no permission for the resource
  1839. {
  1840. e->Release();
  1841. break;
  1842. }
  1843. }
  1844. }
  1845. return;
  1846. }
  1847. bool Cws_accessEx::permissionAddInputOnResource(IEspContext &context, IEspPermissionAddRequest &req, IEspPermissionAddResponse &resp)
  1848. {
  1849. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  1850. if(secmgr == NULL)
  1851. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1852. int numusers = secmgr->countUsers("", MAX_USERS_DISPLAY);
  1853. if(numusers == -1)
  1854. {
  1855. resp.setToomany(true);
  1856. }
  1857. else
  1858. {
  1859. resp.setToomany(false);
  1860. IArrayOf<IEspUserInfo> espusers;
  1861. IUserArray users;
  1862. secmgr->getAllUsers(users);
  1863. ForEachItemIn(x, users)
  1864. {
  1865. CLdapSecUser* usr = dynamic_cast<CLdapSecUser*>(&users.item(x));
  1866. if(usr)
  1867. {
  1868. Owned<IEspUserInfo> oneusr = createUserInfo();
  1869. oneusr->setUsername(usr->getName());
  1870. oneusr->setFullname(usr->getFullName());
  1871. espusers.append(*oneusr.getLink());
  1872. }
  1873. }
  1874. resp.setUsers(espusers);
  1875. }
  1876. IArrayOf<IEspGroupInfo> groups;
  1877. if(secmgr->getLdapServerType() != ACTIVE_DIRECTORY)
  1878. {
  1879. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  1880. onegrp->setName("anyone");
  1881. groups.append(*onegrp.getLink());
  1882. }
  1883. StringArray grpnames;
  1884. secmgr->getAllGroups(grpnames);
  1885. for(unsigned i = 0; i < grpnames.length(); i++)
  1886. {
  1887. const char* grpname = grpnames.item(i);
  1888. if(grpname == NULL || *grpname == '\0')
  1889. continue;
  1890. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  1891. onegrp->setName(grpname);
  1892. groups.append(*onegrp.getLink());
  1893. }
  1894. resp.setGroups(groups);
  1895. return true;
  1896. }
  1897. bool Cws_accessEx::permissionAddInputOnAccount(IEspContext &context, const char* accountName, IEspPermissionAddRequest &req, IEspPermissionAddResponse &resp)
  1898. {
  1899. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  1900. if(secmgr == NULL)
  1901. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1902. resp.setBasednName(req.getBasednName());
  1903. resp.setAccountName(req.getAccountName());
  1904. resp.setAccountType(req.getAccountType());
  1905. const char* prefix = req.getPrefix();
  1906. const char* basednName = req.getBasednName();
  1907. int accountType = req.getAccountType();
  1908. if (basednName && *basednName)
  1909. {
  1910. ForEachItemIn(y, m_basedns)
  1911. {
  1912. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  1913. const char *aName = curbasedn->getName();
  1914. if (!aName || stricmp(basednName, aName))
  1915. continue;
  1916. const char *basedn = curbasedn->getBasedn();
  1917. const char *rtypestr = curbasedn->getRtype();
  1918. if (!basedn || !*basedn || !rtypestr || !*rtypestr)
  1919. continue;
  1920. IArrayOf<ISecResource> resources;
  1921. SecResourceType rtype = str2type(rtypestr);
  1922. if(secmgr->getResources(rtype, basedn, resources))
  1923. {
  1924. StringArray resourcenames;
  1925. for(unsigned i = 0; i < resources.length(); i++)
  1926. {
  1927. ISecResource& r = resources.item(i);
  1928. const char* rname = r.getName();
  1929. if(rname == NULL || *rname == '\0')
  1930. continue;
  1931. if(prefix && *prefix)
  1932. {
  1933. int prefixlen = strlen(prefix);
  1934. if(strncmp(prefix, rname, prefixlen) == 0)
  1935. rname += prefixlen;
  1936. }
  1937. if((rtype == RT_MODULE) && stricmp(rname, "repository"))
  1938. {
  1939. if(Utils::strncasecmp(rname, "repository.", 11) == 0)
  1940. rname = rname + 11;
  1941. else
  1942. continue;
  1943. }
  1944. StringBuffer namebuf(rname);
  1945. if((rtype == RT_MODULE) && stricmp(rname, "repository") != 0 && Utils::strncasecmp(rname, "repository.", 11) != 0)
  1946. namebuf.insert(0, "repository.");
  1947. if(prefix && *prefix)
  1948. namebuf.insert(0, prefix);
  1949. try
  1950. {
  1951. IArrayOf<CPermission> permissions;
  1952. secmgr->getPermissionsArray(basedn, rtype, namebuf.str(), permissions);
  1953. bool found = false;
  1954. ForEachItemIn(x, permissions)
  1955. {
  1956. CPermission& perm = permissions.item(x);
  1957. const char* actname = perm.getAccount_name();
  1958. int accType = perm.getAccount_type(); //0-individual, 1 - group
  1959. if ((accountType == accType) && actname && !strcmp(actname, accountName))
  1960. {
  1961. found = true;
  1962. break;
  1963. }
  1964. }
  1965. if (!found)
  1966. resourcenames.append(rname);
  1967. }
  1968. catch(IException* e) //exception may be thrown when no permission for the resource
  1969. {
  1970. e->Release();
  1971. break;
  1972. }
  1973. }
  1974. if (resourcenames.length() > 0)
  1975. resp.setResources(resourcenames);
  1976. }
  1977. }
  1978. }
  1979. return true;
  1980. }
  1981. bool Cws_accessEx::onPermissionAction(IEspContext &context, IEspPermissionActionRequest &req, IEspPermissionActionResponse &resp)
  1982. {
  1983. try
  1984. {
  1985. checkUser(context);
  1986. resp.setBasedn(req.getBasedn());
  1987. resp.setRname(req.getRname());
  1988. resp.setRtype(req.getRtype());
  1989. resp.setRtitle(req.getRtitle());
  1990. resp.setPrefix(req.getPrefix());
  1991. ISecManager* secmgr = context.querySecManager();
  1992. if(secmgr == NULL)
  1993. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  1994. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  1995. CPermissionAction paction;
  1996. paction.m_basedn.append(req.getBasedn());
  1997. const char* name = req.getRname();
  1998. StringBuffer namebuf(name);
  1999. SecResourceType rtype = str2type(req.getRtype());
  2000. if(rtype == RT_MODULE && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  2001. namebuf.insert(0, "repository.");
  2002. const char* prefix = req.getPrefix();
  2003. if(prefix && *prefix)
  2004. namebuf.insert(0, prefix);
  2005. double version = context.getClientVersion();
  2006. paction.m_rname.append(namebuf.str());
  2007. paction.m_rtype = str2type(req.getRtype());
  2008. paction.m_account_type = (ACT_TYPE)req.getAccount_type();
  2009. if(stricmp(req.getAction(), "add") == 0)
  2010. {
  2011. StringBuffer basednNameStr, resourceNameStr;
  2012. if (version >= 1.01)
  2013. {
  2014. const char* basedn_name = req.getBasednName();
  2015. const char* resource_name = req.getResourceName();
  2016. if (basedn_name && *basedn_name)
  2017. basednNameStr.append(basedn_name);
  2018. if (resource_name && *resource_name)
  2019. resourceNameStr.append(resource_name);
  2020. }
  2021. const char* user = req.getUser();
  2022. const char* grp = req.getGroup();
  2023. if(user != NULL && *user != '\0')
  2024. {
  2025. paction.m_account_name.append(user);
  2026. paction.m_account_type = USER_ACT;
  2027. }
  2028. else if(grp != NULL && *grp != '\0')
  2029. {
  2030. paction.m_account_name.append(grp);
  2031. // anyone is actually treated as a virtual "user" by sun and open ldap.
  2032. if((ldapsecmgr->getLdapServerType() != ACTIVE_DIRECTORY) && (stricmp(grp, "anyone") == 0))
  2033. paction.m_account_type = USER_ACT;
  2034. else
  2035. paction.m_account_type = GROUP_ACT;
  2036. }
  2037. else if((basednNameStr.length() > 0) && (resourceNameStr.length() > 0))
  2038. {
  2039. const char* account_name = req.getAccount_name();
  2040. if (!account_name || !*account_name)
  2041. {
  2042. resp.setRetcode(-1);
  2043. resp.setRetmsg("Please input or select user/group");
  2044. return false;
  2045. }
  2046. paction.m_account_name.clear().append(account_name);
  2047. ForEachItemIn(y, m_basedns)
  2048. {
  2049. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  2050. const char *aName = curbasedn->getName();
  2051. if (!aName || stricmp(basednNameStr.str(), aName))
  2052. continue;
  2053. const char *basedn = curbasedn->getBasedn();
  2054. const char *rtypestr = curbasedn->getRtype();
  2055. if (!basedn || !*basedn || !rtypestr || !*rtypestr)
  2056. continue;
  2057. StringBuffer namebuf(resourceNameStr);
  2058. SecResourceType rtype = str2type(rtypestr);
  2059. if(rtype == RT_MODULE && (stricmp(namebuf.str(), "codegenerator.cpp")) && stricmp(name, "repository") != 0 && Utils::strncasecmp(name, "repository.", 11) != 0)
  2060. namebuf.insert(0, "repository.");
  2061. if(prefix && *prefix)
  2062. namebuf.insert(0, prefix);
  2063. paction.m_basedn.clear().append(basedn);
  2064. paction.m_rname.clear().append(namebuf.str());
  2065. paction.m_rtype = rtype;
  2066. break;
  2067. }
  2068. resp.setAccountName(account_name);
  2069. if (req.getAccount_type() < 1)
  2070. resp.setIsGroup(false);
  2071. else
  2072. resp.setIsGroup(true);
  2073. }
  2074. else
  2075. {
  2076. resp.setRetcode(-1);
  2077. resp.setRetmsg("Please input or select user/group");
  2078. return false;
  2079. }
  2080. }
  2081. else
  2082. {
  2083. paction.m_account_name.append(req.getAccount_name());
  2084. if (version >= 1.01)
  2085. {
  2086. resp.setAccountName(req.getAccount_name());
  2087. if (req.getAccount_type() < 1)
  2088. resp.setIsGroup(false);
  2089. else
  2090. resp.setIsGroup(true);
  2091. }
  2092. }
  2093. paction.m_allows = 0;
  2094. paction.m_denies = 0;
  2095. if(req.getAllow_full())
  2096. paction.m_allows |= NewSecAccess_Full;
  2097. if(req.getAllow_read())
  2098. paction.m_allows |= NewSecAccess_Read;
  2099. if(req.getAllow_write())
  2100. paction.m_allows |= NewSecAccess_Write;
  2101. if(req.getAllow_access())
  2102. paction.m_allows |= NewSecAccess_Access;
  2103. if(req.getDeny_full())
  2104. paction.m_denies |= NewSecAccess_Full;
  2105. if(req.getDeny_read())
  2106. paction.m_denies |= NewSecAccess_Read;
  2107. if(req.getDeny_write())
  2108. paction.m_denies |= NewSecAccess_Write;
  2109. if(req.getDeny_access())
  2110. paction.m_denies |= NewSecAccess_Access;
  2111. paction.m_action.append(req.getAction());
  2112. bool ret = true;
  2113. StringBuffer retmsg;
  2114. try
  2115. {
  2116. ret = ldapsecmgr->changePermission(paction);
  2117. }
  2118. catch(IException* e)
  2119. {
  2120. resp.setRetcode(e->errorCode());
  2121. e->errorMessage(retmsg);
  2122. resp.setRetmsg(retmsg.str());
  2123. return false;
  2124. }
  2125. if(!ret)
  2126. {
  2127. resp.setRetcode(-1);
  2128. resp.setRetmsg("Unknown error");
  2129. return false;
  2130. }
  2131. resp.setRetcode(0);
  2132. }
  2133. catch(IException* e)
  2134. {
  2135. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2136. }
  2137. return true;
  2138. }
  2139. bool Cws_accessEx::onUserResetPassInput(IEspContext &context, IEspUserResetPassInputRequest &req, IEspUserResetPassInputResponse &resp)
  2140. {
  2141. try
  2142. {
  2143. checkUser(context);
  2144. resp.setUsername(req.getUsername());
  2145. }
  2146. catch(IException* e)
  2147. {
  2148. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2149. }
  2150. return true;
  2151. }
  2152. bool Cws_accessEx::onUserResetPass(IEspContext &context, IEspUserResetPassRequest &req, IEspUserResetPassResponse &resp)
  2153. {
  2154. try
  2155. {
  2156. checkUser(context);
  2157. resp.setUsername(req.getUsername());
  2158. ISecManager* secmgr = context.querySecManager();
  2159. if(secmgr == NULL)
  2160. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2161. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  2162. const char* username = req.getUsername();
  2163. if(username == NULL)
  2164. {
  2165. resp.setRetcode(-1);
  2166. resp.setRetmsg("username can't be empty");
  2167. return false;
  2168. }
  2169. const char* newpass1 = req.getNewPassword();
  2170. const char* newpass2 = req.getNewPasswordRetype();
  2171. if(newpass1 == NULL || newpass2 == NULL || *newpass1 == '\0' || *newpass2 == '\0' || strcmp(newpass1, newpass2) != 0)
  2172. {
  2173. resp.setRetcode(-1);
  2174. resp.setRetmsg("new password and retype can't be empty and must match");
  2175. return false;
  2176. }
  2177. bool ret = ldapsecmgr->updateUser(username, req.getNewPassword());
  2178. if(ret)
  2179. {
  2180. resp.setRetcode(0);
  2181. resp.setRetmsg("");
  2182. return false;
  2183. }
  2184. else
  2185. {
  2186. resp.setRetcode(-1);
  2187. }
  2188. }
  2189. catch(IException* e)
  2190. {
  2191. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2192. }
  2193. return true;
  2194. }
  2195. bool Cws_accessEx::onUserPosix(IEspContext &context, IEspUserPosixRequest &req, IEspUserPosixResponse &resp)
  2196. {
  2197. try
  2198. {
  2199. checkUser(context);
  2200. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2201. if(secmgr == NULL)
  2202. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2203. const char* username = req.getUsername();
  2204. if(username == NULL || *username == '\0')
  2205. {
  2206. resp.setRetcode(-1);
  2207. resp.setRetmsg("username can't be empty");
  2208. return false;
  2209. }
  2210. bool enable = req.getPosixenabled();
  2211. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  2212. if(enable)
  2213. {
  2214. const char* gidnumber = req.getGidnumber();
  2215. const char* uidnumber = req.getUidnumber();
  2216. const char* homedirectory = req.getHomedirectory();
  2217. const char* loginshell = req.getLoginshell();
  2218. if(!gidnumber || !*gidnumber || !uidnumber || !*uidnumber || !homedirectory || !*homedirectory)
  2219. {
  2220. resp.setRetcode(-1);
  2221. resp.setRetmsg("gidnumber, uidnumber and homedirectory are required.");
  2222. return false;
  2223. }
  2224. unsigned i;
  2225. for(i = 0; i < strlen(gidnumber); i++)
  2226. {
  2227. if(!isdigit(gidnumber[i]))
  2228. throw MakeStringException(ECLWATCH_ID_MUST_BE_ALL_DIGITS, "Group ID Number should be all digits");
  2229. }
  2230. for(i = 0; i < strlen(uidnumber); i++)
  2231. {
  2232. if(!isdigit(uidnumber[i]))
  2233. throw MakeStringException(ECLWATCH_ID_MUST_BE_ALL_DIGITS, "User ID Number should be all digits");
  2234. }
  2235. user->setGidnumber(gidnumber);
  2236. user->setUidnumber(uidnumber);
  2237. user->setHomedirectory(homedirectory);
  2238. user->setLoginshell(loginshell);
  2239. }
  2240. try
  2241. {
  2242. secmgr->updateUser(enable?"posixenable":"posixdisable", *user.get());
  2243. }
  2244. catch(IException* e)
  2245. {
  2246. resp.setRetcode(-1);
  2247. StringBuffer errmsg;
  2248. resp.setRetmsg(e->errorMessage(errmsg).str());
  2249. return false;
  2250. }
  2251. resp.setUsername(username);
  2252. resp.setRetcode(0);
  2253. resp.setRetmsg("User's posix account info has been successfully updated");
  2254. }
  2255. catch(IException* e)
  2256. {
  2257. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2258. }
  2259. return true;
  2260. }
  2261. bool Cws_accessEx::onUserPosixInput(IEspContext &context, IEspUserPosixInputRequest &req, IEspUserPosixInputResponse &resp)
  2262. {
  2263. try
  2264. {
  2265. checkUser(context);
  2266. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2267. if(secmgr == NULL)
  2268. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2269. const char* username = req.getUsername();
  2270. if(username == NULL || *username == '\0')
  2271. {
  2272. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Please specify a username.");
  2273. }
  2274. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  2275. secmgr->getUserInfo(*user.get());
  2276. resp.setUsername(username);
  2277. resp.setPosixenabled(user->getPosixenabled());
  2278. if(user->getGidnumber())
  2279. resp.setGidnumber(user->getGidnumber());
  2280. if(user->getUidnumber())
  2281. resp.setUidnumber(user->getUidnumber());
  2282. if(user->getHomedirectory())
  2283. resp.setHomedirectory(user->getHomedirectory());
  2284. if(user->getLoginshell())
  2285. resp.setLoginshell(user->getLoginshell());
  2286. }
  2287. catch(IException* e)
  2288. {
  2289. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2290. }
  2291. return true;
  2292. }
  2293. bool Cws_accessEx::onUserInfoEdit(IEspContext &context, IEspUserInfoEditRequest &req, IEspUserInfoEditResponse &resp)
  2294. {
  2295. try
  2296. {
  2297. checkUser(context);
  2298. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2299. if(secmgr == NULL)
  2300. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2301. const char* username = req.getUsername();
  2302. if(username == NULL || *username == '\0')
  2303. {
  2304. resp.setRetcode(-1);
  2305. resp.setRetmsg("username can't be empty");
  2306. return false;
  2307. }
  2308. const char* firstname = req.getFirstname();
  2309. const char* lastname = req.getLastname();
  2310. if((!firstname || !*firstname) && (!lastname || !*lastname))
  2311. {
  2312. resp.setRetcode(-1);
  2313. resp.setRetmsg("Please specify both firstname and lastname");
  2314. return false;
  2315. }
  2316. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  2317. user->setFirstName(firstname);
  2318. user->setLastName(lastname);
  2319. try
  2320. {
  2321. secmgr->updateUser("names", *user.get());
  2322. }
  2323. catch(IException* e)
  2324. {
  2325. resp.setRetcode(-1);
  2326. StringBuffer errmsg;
  2327. resp.setRetmsg(e->errorMessage(errmsg).str());
  2328. return false;
  2329. }
  2330. resp.setUsername(username);
  2331. resp.setRetcode(0);
  2332. resp.setRetmsg("User's account info has been successfully updated");
  2333. }
  2334. catch(IException* e)
  2335. {
  2336. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2337. }
  2338. return true;
  2339. }
  2340. bool Cws_accessEx::onUserInfoEditInput(IEspContext &context, IEspUserInfoEditInputRequest &req, IEspUserInfoEditInputResponse &resp)
  2341. {
  2342. try
  2343. {
  2344. checkUser(context);
  2345. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2346. if(secmgr == NULL)
  2347. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2348. const char* username = req.getUsername();
  2349. if(username == NULL || *username == '\0')
  2350. {
  2351. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Please specify a username.");
  2352. }
  2353. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  2354. secmgr->getUserInfo(*user.get());
  2355. resp.setUsername(username);
  2356. resp.setFirstname(user->getFirstName());
  2357. resp.setLastname(user->getLastName());
  2358. }
  2359. catch(IException* e)
  2360. {
  2361. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2362. }
  2363. return true;
  2364. }
  2365. bool Cws_accessEx::onUserSudoersInput(IEspContext &context, IEspUserSudoersInputRequest &req, IEspUserSudoersInputResponse &resp)
  2366. {
  2367. try
  2368. {
  2369. checkUser(context);
  2370. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2371. if(secmgr == NULL)
  2372. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2373. const char* username = req.getUsername();
  2374. if(username == NULL || *username == '\0')
  2375. {
  2376. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Please specify a username.");
  2377. }
  2378. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  2379. secmgr->getUserInfo(*user.get(), "sudoers");
  2380. resp.setUsername(username);
  2381. resp.setInsudoers(user->getInSudoers());
  2382. if(user->getInSudoers())
  2383. {
  2384. resp.setSudoHost(user->getSudoHost());
  2385. resp.setSudoCommand(user->getSudoCommand());
  2386. resp.setSudoOption(user->getSudoOption());
  2387. }
  2388. else
  2389. {
  2390. resp.setSudoHost("ALL");
  2391. resp.setSudoCommand("ALL");
  2392. resp.setSudoOption("!authenticate");
  2393. }
  2394. }
  2395. catch(IException* e)
  2396. {
  2397. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2398. }
  2399. return true;
  2400. }
  2401. bool Cws_accessEx::onUserSudoers(IEspContext &context, IEspUserSudoersRequest &req, IEspUserSudoersResponse &resp)
  2402. {
  2403. try
  2404. {
  2405. checkUser(context);
  2406. CLdapSecManager* secmgr = (CLdapSecManager*)context.querySecManager();
  2407. if(secmgr == NULL)
  2408. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2409. const char* username = req.getUsername();
  2410. if(username == NULL || *username == '\0')
  2411. {
  2412. resp.setRetcode(-1);
  2413. resp.setRetmsg("username can't be empty");
  2414. return false;
  2415. }
  2416. resp.setUsername(username);
  2417. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  2418. const char* action = req.getAction();
  2419. if(!action || !*action)
  2420. {
  2421. resp.setRetcode(-1);
  2422. resp.setRetmsg("Action can't be empty");
  2423. return false;
  2424. }
  2425. user->setSudoHost(req.getSudoHost());
  2426. user->setSudoCommand(req.getSudoCommand());
  2427. user->setSudoOption(req.getSudoOption());
  2428. bool ok = false;
  2429. StringBuffer retmsg;
  2430. try
  2431. {
  2432. if(stricmp(action, "add") == 0)
  2433. ok = secmgr->updateUser("sudoersadd", *user.get());
  2434. else if(stricmp(action, "delete") == 0)
  2435. ok = secmgr->updateUser("sudoersdelete", *user.get());
  2436. else if(stricmp(action, "update") == 0)
  2437. ok = secmgr->updateUser("sudoersupdate", *user.get());
  2438. }
  2439. catch(IException* e)
  2440. {
  2441. ok = false;
  2442. e->errorMessage(retmsg);
  2443. e->Release();
  2444. }
  2445. catch(...)
  2446. {
  2447. ok = false;
  2448. retmsg.append("unknown exception");
  2449. }
  2450. if(!ok)
  2451. {
  2452. resp.setRetcode(-1);
  2453. resp.setRetmsg(retmsg.str());
  2454. }
  2455. else
  2456. {
  2457. resp.setRetcode(0);
  2458. resp.setRetmsg("succeeded.");
  2459. }
  2460. }
  2461. catch(IException* e)
  2462. {
  2463. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2464. }
  2465. return true;
  2466. }
  2467. bool Cws_accessEx::onAccountPermissions(IEspContext &context, IEspAccountPermissionsRequest &req, IEspAccountPermissionsResponse &resp)
  2468. {
  2469. try
  2470. {
  2471. checkUser(context);
  2472. double version = context.getClientVersion();
  2473. ISecManager* secmgr = context.querySecManager();
  2474. if(secmgr == NULL)
  2475. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2476. const char* username = req.getAccountName();
  2477. if(username == NULL || *username == '\0')
  2478. {
  2479. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Please specify an account name.");
  2480. }
  2481. bool bGroupAccount = req.getIsGroup();
  2482. bool bIncludeGroup = req.getIncludeGroup();
  2483. if(m_basedns.length() == 0)
  2484. {
  2485. setBasedns(context);
  2486. }
  2487. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  2488. StringArray groupnames;
  2489. if (version > 1.02 && !bGroupAccount && bIncludeGroup)
  2490. {
  2491. ldapsecmgr->getGroups(username, groupnames);
  2492. }
  2493. groupnames.append("Authenticated Users");
  2494. groupnames.append("everyone");
  2495. IArrayOf<IEspAccountPermission> accountPermissions;
  2496. bool bAuthUsersPerm = false;
  2497. Owned<IEspGroupAccountPermission> grouppermission1 = createGroupAccountPermission();
  2498. grouppermission1->setGroupName("Authenticated Users");
  2499. if (version > 1.05)
  2500. {
  2501. StringArray basednNames;
  2502. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, "Authenticated Users", 1, basednNames);
  2503. if (basednNames.length() > 0)
  2504. grouppermission1->setBasednNames(basednNames);
  2505. }
  2506. IArrayOf<IConstAccountPermission>& authUsersPermissions = grouppermission1->getPermissions();
  2507. bool bEveryonePerm = false;
  2508. Owned<IEspGroupAccountPermission> grouppermission2 = createGroupAccountPermission();
  2509. grouppermission2->setGroupName("Everyone");
  2510. if (version > 1.05)
  2511. {
  2512. StringArray basednNames;
  2513. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, "Everyone", 1, basednNames);
  2514. if (basednNames.length() > 0)
  2515. grouppermission2->setBasednNames(basednNames);
  2516. }
  2517. IArrayOf<IConstAccountPermission>& everyonePermissions = grouppermission2->getPermissions();
  2518. IArrayOf<IEspGroupAccountPermission> groupAccountPermissions;
  2519. StringBuffer moduleBasedn; //To be used by the Permission: codegenerator.cpp
  2520. ForEachItemIn(y1, m_basedns)
  2521. {
  2522. IEspDnStruct* curbasedn = &(m_basedns.item(y1));
  2523. const char *aName = curbasedn->getName();
  2524. const char *aBasedn = curbasedn->getBasedn();
  2525. const char *aRtype = curbasedn->getRtype();
  2526. const char *aRtitle = curbasedn->getRtitle();
  2527. if (!aName || !*aName ||!aBasedn || !*aBasedn ||!aRtype || !*aRtype ||!aRtitle || !*aRtitle)
  2528. continue;
  2529. SecResourceType rtype = str2type(aRtype);
  2530. if (rtype == RT_MODULE)
  2531. {
  2532. moduleBasedn.append(aBasedn);
  2533. break;
  2534. }
  2535. }
  2536. ForEachItemIn(y, m_basedns)
  2537. {
  2538. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  2539. const char *aName = curbasedn->getName();
  2540. const char *aBasedn = curbasedn->getBasedn();
  2541. const char *aRtype = curbasedn->getRtype();
  2542. const char *aRtitle = curbasedn->getRtitle();
  2543. if (!aName || !*aName ||!aBasedn || !*aBasedn ||!aRtype || !*aRtype ||!aRtitle || !*aRtitle)
  2544. continue;
  2545. SecResourceType rtype = str2type(aRtype);
  2546. IArrayOf<IEspResource> ResourceArray;
  2547. //if(rtype == RT_FILE_SCOPE || rtype == RT_WORKUNIT_SCOPE)
  2548. if(rtype == RT_WORKUNIT_SCOPE)
  2549. {
  2550. StringBuffer deft_basedn, deft_name;
  2551. const char* comma = strchr(aBasedn, ',');
  2552. const char* eqsign = strchr(aBasedn, '=');
  2553. if(eqsign != NULL)
  2554. {
  2555. if(comma == NULL)
  2556. deft_name.append(eqsign+1);
  2557. else
  2558. {
  2559. deft_name.append(comma - eqsign - 1, eqsign+1);
  2560. deft_basedn.append(comma + 1);
  2561. }
  2562. }
  2563. if (deft_name.length() > 0)
  2564. {
  2565. Owned<IEspResource> oneresource = createResource();
  2566. oneresource->setName(deft_name);
  2567. oneresource->setDescription(deft_basedn);
  2568. ResourceArray.append(*oneresource.getLink());
  2569. }
  2570. }
  2571. IArrayOf<ISecResource> resources;
  2572. if(secmgr->getResources(rtype, aBasedn, resources))
  2573. {
  2574. ForEachItemIn(y1, resources)
  2575. {
  2576. ISecResource& r = resources.item(y1);
  2577. const char* rname = r.getName();
  2578. if(rname == NULL || *rname == '\0')
  2579. continue;
  2580. //permission codegenerator.cpp is saved as a service permission (not a module permission)
  2581. //when it is added for a user
  2582. if ((rtype == RT_MODULE) && (!stricmp(rname, "codegenerator.cpp")))
  2583. continue;
  2584. if((rtype == RT_MODULE) && Utils::strncasecmp(rname, "repository", 10))
  2585. {
  2586. continue;
  2587. }
  2588. Owned<IEspResource> oneresource = createResource();
  2589. oneresource->setName(rname);
  2590. oneresource->setDescription(aBasedn);
  2591. ResourceArray.append(*oneresource.getLink());
  2592. }
  2593. }
  2594. if(rtype == RT_SERVICE && moduleBasedn.length() > 0)
  2595. { //permission codegenerator.cpp is saved as a service permission when it is added for a user
  2596. Owned<IEspResource> oneresource = createResource();
  2597. oneresource->setName("codegenerator.cpp");
  2598. oneresource->setDescription(moduleBasedn.str());
  2599. ResourceArray.append(*oneresource.getLink());
  2600. moduleBasedn.clear();
  2601. }
  2602. ForEachItemIn(y2, ResourceArray)
  2603. {
  2604. IEspResource& r = ResourceArray.item(y2);
  2605. const char* rname = r.getName();
  2606. const char* dnname = r.getDescription();
  2607. if(rname == NULL || *rname == '\0')
  2608. continue;
  2609. StringBuffer namebuf(rname);
  2610. //const char* prefix = req.getPrefix();
  2611. //if(prefix && *prefix)
  2612. // namebuf.insert(0, prefix);
  2613. try
  2614. {
  2615. IArrayOf<CPermission> permissions;
  2616. ldapsecmgr->getPermissionsArray(dnname, rtype, namebuf.str(), permissions);
  2617. ForEachItemIn(x, permissions)
  2618. {
  2619. CPermission& perm = permissions.item(x);
  2620. int accountType = perm.getAccount_type(); //0-individual, 1 - group
  2621. if (bGroupAccount && accountType < 1)
  2622. continue;
  2623. if (!bGroupAccount && (accountType > 0) && (groupnames.length() < 1))
  2624. continue;
  2625. StringBuffer escapedname;
  2626. const char* actname = perm.getAccount_name();
  2627. if ((!bGroupAccount && accountType < 1) || (bGroupAccount && accountType > 0))
  2628. {
  2629. if(!actname || strcmp(actname, username))
  2630. continue;
  2631. }
  2632. else if (version > 1.02)
  2633. {
  2634. if(!actname || groupnames.length() < 1)
  2635. continue;
  2636. bool bFound = false;
  2637. for(unsigned i = 0; i < groupnames.length(); i++)
  2638. {
  2639. const char* group = groupnames.item(i);
  2640. if (!group || strcmp(actname, group))
  2641. continue;
  2642. bFound = true;
  2643. break;
  2644. }
  2645. if (!bFound)
  2646. continue;
  2647. }
  2648. Owned<IEspAccountPermission> onepermission = createAccountPermission();
  2649. onepermission->setBasedn(dnname);
  2650. onepermission->setRType(aRtype);
  2651. onepermission->setResourceName(aRtitle);
  2652. onepermission->setPermissionName(namebuf.str());
  2653. int allows = perm.getAllows();
  2654. int denies = perm.getDenies();
  2655. if((allows & NewSecAccess_Access) == NewSecAccess_Access)
  2656. onepermission->setAllow_access(true);
  2657. if((allows & NewSecAccess_Read) == NewSecAccess_Read)
  2658. onepermission->setAllow_read(true);
  2659. if((allows & NewSecAccess_Write) == NewSecAccess_Write)
  2660. onepermission->setAllow_write(true);
  2661. if((allows & NewSecAccess_Full) == NewSecAccess_Full)
  2662. onepermission->setAllow_full(true);
  2663. if((denies & NewSecAccess_Access) == NewSecAccess_Access)
  2664. onepermission->setDeny_access(true);
  2665. if((denies & NewSecAccess_Read) == NewSecAccess_Read)
  2666. onepermission->setDeny_read(true);
  2667. if((denies & NewSecAccess_Write) == NewSecAccess_Write)
  2668. onepermission->setDeny_write(true);
  2669. if((denies & NewSecAccess_Full) == NewSecAccess_Full)
  2670. onepermission->setDeny_full(true);
  2671. if ((!bGroupAccount && accountType < 1) || (bGroupAccount && accountType > 0))
  2672. accountPermissions.append(*onepermission.getLink());
  2673. else if (version > 1.02)
  2674. {
  2675. if (!strcmp(actname, "Authenticated Users"))
  2676. {
  2677. authUsersPermissions.append(*onepermission.getLink());
  2678. bAuthUsersPerm = true;
  2679. }
  2680. else if (!strcmp(actname, "everyone"))
  2681. {
  2682. everyonePermissions.append(*onepermission.getLink());
  2683. bEveryonePerm = true;
  2684. }
  2685. else
  2686. {
  2687. bool bFound = false;
  2688. ForEachItemIn(k, groupAccountPermissions)
  2689. {
  2690. IEspGroupAccountPermission& grouppermission0 = groupAccountPermissions.item(k);
  2691. const char* g_name = grouppermission0.getGroupName();
  2692. if (!g_name || strcmp(actname, g_name))
  2693. continue;
  2694. IArrayOf<IConstAccountPermission>& g_permission = grouppermission0.getPermissions();
  2695. g_permission.append(*onepermission.getLink());
  2696. bFound = true;
  2697. break;
  2698. }
  2699. if (!bFound)
  2700. {
  2701. Owned<IEspGroupAccountPermission> grouppermission = createGroupAccountPermission();
  2702. grouppermission->setGroupName(actname);
  2703. if (version > 1.05)
  2704. {
  2705. StringArray basednNames;
  2706. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, actname, 1, basednNames);
  2707. if (basednNames.length() > 0)
  2708. grouppermission->setBasednNames(basednNames);
  2709. }
  2710. IArrayOf<IConstAccountPermission>& g_permission = grouppermission->getPermissions();
  2711. g_permission.append(*onepermission.getLink());
  2712. groupAccountPermissions.append(*grouppermission.getLink());
  2713. }
  2714. }
  2715. }
  2716. }
  2717. }
  2718. catch(IException* e) //exception may be thrown when no permission for the resource
  2719. {
  2720. e->Release();
  2721. }
  2722. }
  2723. }
  2724. StringArray basednNames;
  2725. getBaseDNsForAddingPermssionToAccount(ldapsecmgr, NULL, username, bGroupAccount? 1:0, basednNames);
  2726. if (basednNames.length() > 0)
  2727. {
  2728. resp.setBasednNames(basednNames);
  2729. }
  2730. if (accountPermissions.length() > 0)
  2731. {
  2732. resp.setPermissions(accountPermissions);
  2733. }
  2734. if (version > 1.02)
  2735. {
  2736. if (bAuthUsersPerm)
  2737. {
  2738. groupAccountPermissions.append(*grouppermission1.getLink());
  2739. }
  2740. if (bEveryonePerm)
  2741. {
  2742. groupAccountPermissions.append(*grouppermission2.getLink());
  2743. }
  2744. if (groupAccountPermissions.length() > 0)
  2745. {
  2746. resp.setGroupPermissions(groupAccountPermissions);
  2747. }
  2748. }
  2749. resp.setAccountName(req.getAccountName());
  2750. resp.setIsGroup(req.getIsGroup());
  2751. }
  2752. catch(IException* e)
  2753. {
  2754. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2755. }
  2756. return true;
  2757. }
  2758. bool Cws_accessEx::onFilePermission(IEspContext &context, IEspFilePermissionRequest &req, IEspFilePermissionResponse &resp)
  2759. {
  2760. try
  2761. {
  2762. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  2763. double version = context.getClientVersion();
  2764. if (version > 1.03)
  2765. {
  2766. if(secmgr == NULL)
  2767. {
  2768. resp.setNoSecMngr(true);
  2769. return true;
  2770. }
  2771. }
  2772. else
  2773. {
  2774. if(secmgr == NULL)
  2775. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2776. }
  2777. checkUser(context);
  2778. //Get all users for input form
  2779. int numusers = secmgr->countUsers("", MAX_USERS_DISPLAY);
  2780. if(numusers == -1)
  2781. {
  2782. resp.setToomany(true);
  2783. }
  2784. else
  2785. {
  2786. resp.setToomany(false);
  2787. IArrayOf<IEspUserInfo> espusers;
  2788. IUserArray users;
  2789. secmgr->getAllUsers(users);
  2790. ForEachItemIn(x, users)
  2791. {
  2792. CLdapSecUser* usr = dynamic_cast<CLdapSecUser*>(&users.item(x));
  2793. if(usr)
  2794. {
  2795. Owned<IEspUserInfo> oneusr = createUserInfo();
  2796. oneusr->setUsername(usr->getName());
  2797. oneusr->setFullname(usr->getFullName());
  2798. espusers.append(*oneusr.getLink());
  2799. }
  2800. }
  2801. resp.setUsers(espusers);
  2802. }
  2803. //Get all groups for input form
  2804. StringArray groupnames;
  2805. secmgr->getAllGroups(groupnames);
  2806. ///groupnames.append("Authenticated Users");
  2807. ///groupnames.append("Administrators");
  2808. if (groupnames.length() > 0)
  2809. {
  2810. IArrayOf<IEspGroupInfo> groups;
  2811. for(unsigned i = 0; i < groupnames.length(); i++)
  2812. {
  2813. const char* grpname = groupnames.item(i);
  2814. if(grpname == NULL || grpname[0] == '\0')
  2815. continue;
  2816. Owned<IEspGroupInfo> onegrp = createGroupInfo();
  2817. onegrp->setName(grpname);
  2818. groups.append(*onegrp.getLink());
  2819. }
  2820. resp.setGroups(groups);
  2821. }
  2822. const char* fileName = req.getFileName();
  2823. const char* userName = req.getUserName();
  2824. const char* groupName = req.getGroupName();
  2825. if (!fileName || !*fileName)
  2826. return true; //no file name is set when the input form is launched first time
  2827. if ((!groupName || !*groupName) && (!userName || !*userName))
  2828. throw MakeStringException(ECLWATCH_INVALID_ACCOUNT_NAME, "Either user name or group name has to be specified.");
  2829. int access = -1;
  2830. if (userName && *userName) //for user
  2831. {
  2832. resp.setFileName(fileName);
  2833. resp.setUserName(userName);
  2834. ISecUser* sec_user = secmgr->findUser(userName);
  2835. if (sec_user)
  2836. {
  2837. StringBuffer accessStr;
  2838. access = secmgr->authorizeEx(RT_FILE_SCOPE, *sec_user, fileName, false);
  2839. switch (access)
  2840. {
  2841. case SecAccess_Full:
  2842. resp.setUserPermission("Full Access Permission");
  2843. break;
  2844. case SecAccess_Write:
  2845. resp.setUserPermission("Write Access Permission");
  2846. break;
  2847. case SecAccess_Read:
  2848. resp.setUserPermission("Read Access Permission");
  2849. break;
  2850. case SecAccess_Access:
  2851. resp.setUserPermission("Access Permission");
  2852. break;
  2853. case SecAccess_None:
  2854. resp.setUserPermission("None Access Permission");
  2855. break;
  2856. default:
  2857. resp.setUserPermission("Permission Unknown");
  2858. break;
  2859. }
  2860. }
  2861. }
  2862. else //for group
  2863. {
  2864. resp.setFileName(fileName);
  2865. resp.setGroupName(groupName);
  2866. if(m_basedns.length() == 0) //basedns may never be set
  2867. {
  2868. setBasedns(context);
  2869. }
  2870. //Find out the basedn for RT_FILE_SCOPE
  2871. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  2872. StringBuffer basednStr;
  2873. ForEachItemIn(y, m_basedns)
  2874. {
  2875. IEspDnStruct* curbasedn = &(m_basedns.item(y));
  2876. const char *aBasedn = curbasedn->getBasedn();
  2877. const char *aRtype = curbasedn->getRtype();
  2878. if (!aBasedn || !*aBasedn || !aRtype || !*aRtype)
  2879. continue;
  2880. SecResourceType rtype = str2type(aRtype);
  2881. if (rtype != RT_FILE_SCOPE)
  2882. continue;
  2883. basednStr.append(aBasedn);
  2884. }
  2885. char* pStr0 = (char*) fileName;
  2886. while (pStr0[0] == ':') //in case of some ':' by mistake
  2887. pStr0++;
  2888. //Check the permissin for the file and the group
  2889. if (*pStr0 && basednStr.length() > 0)
  2890. {
  2891. StringBuffer lastFileScope;
  2892. StringArray scopes;
  2893. char* pStr = strstr(pStr0, "::");
  2894. while (pStr)
  2895. {
  2896. char fileScope[10240];
  2897. strncpy(fileScope, pStr0, pStr-pStr0);
  2898. fileScope[pStr-pStr0] = 0;
  2899. if (lastFileScope.length() < 1)
  2900. lastFileScope.append(fileScope);
  2901. else
  2902. lastFileScope.appendf("::%s", fileScope);
  2903. scopes.add(lastFileScope.str(), 0);
  2904. pStr0 = pStr+2;
  2905. while (pStr0[0] == ':') //in case of more than two ':' by mistake
  2906. pStr0++;
  2907. if (pStr0[0] == 0)
  2908. break;
  2909. pStr = strstr(pStr0, "::");
  2910. }
  2911. if (pStr0[0] != 0)
  2912. {
  2913. if (lastFileScope.length() < 1)
  2914. lastFileScope.append(pStr0);
  2915. else
  2916. lastFileScope.appendf("::%s", pStr0);
  2917. scopes.add(lastFileScope.str(), 0);
  2918. }
  2919. access = 0;
  2920. ForEachItemIn(y, scopes)
  2921. {
  2922. StringBuffer namebuf = scopes.item(y);
  2923. try
  2924. {
  2925. IArrayOf<CPermission> permissions;
  2926. ldapsecmgr->getPermissionsArray(basednStr.str(), RT_FILE_SCOPE, namebuf.str(), permissions);
  2927. ForEachItemIn(x, permissions)
  2928. {
  2929. CPermission& perm = permissions.item(x);
  2930. int accountType = perm.getAccount_type(); //0-individual, 1 - group
  2931. if (accountType < 1)
  2932. continue;
  2933. const char* actname = perm.getAccount_name();
  2934. if(!actname || strcmp(actname, groupName))
  2935. continue;
  2936. int allows = perm.getAllows();
  2937. int denies = perm.getDenies();
  2938. access = allows & (~denies);
  2939. break;
  2940. }
  2941. }
  2942. catch(IException* e) //exception may be thrown when no permission for the resource
  2943. {
  2944. e->Release();
  2945. }
  2946. if (access != 0)
  2947. break;
  2948. }
  2949. }
  2950. //Convert permission type to display string
  2951. if((access & NewSecAccess_Full) == NewSecAccess_Full)
  2952. resp.setUserPermission("Full Access Permission");
  2953. else if((access & NewSecAccess_Write) == NewSecAccess_Write)
  2954. resp.setUserPermission("Write Access Permission");
  2955. else if((access & NewSecAccess_Read) == NewSecAccess_Read)
  2956. resp.setUserPermission("Read Access Permission");
  2957. else if((access & NewSecAccess_Access) == NewSecAccess_Access)
  2958. resp.setUserPermission("Access Permission");
  2959. else if (access == 0)
  2960. resp.setUserPermission("None Access Permission");
  2961. else
  2962. resp.setUserPermission("Permission Unknown");
  2963. }
  2964. }
  2965. catch(IException* e)
  2966. {
  2967. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2968. }
  2969. return true;
  2970. }
  2971. bool Cws_accessEx::onUserAccountExport(IEspContext &context, IEspUserAccountExportRequest &req, IEspUserAccountExportResponse &resp)
  2972. {
  2973. try
  2974. {
  2975. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  2976. if(secmgr == NULL)
  2977. {
  2978. throw MakeStringException(ECLWATCH_INVALID_SEC_MANAGER, MSG_SEC_MANAGER_IS_NULL);
  2979. }
  2980. CLdapSecManager* ldapsecmgr = (CLdapSecManager*)secmgr;
  2981. checkUser(context);
  2982. StringBuffer xls;
  2983. xls.append("<html xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">");
  2984. xls.append("<head>");
  2985. xls.append("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
  2986. xls.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
  2987. xls.append("<title>User Account Information</title>");
  2988. xls.append("</head>");
  2989. xls.append("<body>");
  2990. xls.append("<table xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" cellspacing=\"0\" frame=\"box\" rules=\"all\">");
  2991. xls.append("<thead>");
  2992. xls.append("<tr valign=\"bottom\">");
  2993. xls.append("<th>Login Name</th>");
  2994. xls.append("<th>First Name</th>");
  2995. xls.append("<th>Last Name</th>");
  2996. xls.append("<th>Group Name</th>");
  2997. xls.append("</tr>");
  2998. StringArray& usernames = req.getUsernames();
  2999. StringArray& groupnames = req.getGroupnames();
  3000. if (usernames.length() > 0)
  3001. {
  3002. for(unsigned i = 0; i < usernames.length(); i++)
  3003. {
  3004. const char* username = usernames.item(i);
  3005. if (!username || !*username)
  3006. continue;
  3007. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(username);
  3008. secmgr->getUserInfo(*user.get());
  3009. const char* firstname = user->getFirstName();
  3010. const char* lastname = user->getLastName();
  3011. StringArray groupnames1;
  3012. ldapsecmgr->getGroups(username, groupnames1);
  3013. ///groupnames1.append("TestGroup1");
  3014. ///groupnames1.append("TestGroup2");
  3015. if (groupnames1.length() < 1)
  3016. {
  3017. xls.append("<tr>");
  3018. xls.appendf("<td>%s</td>", username);
  3019. if (!firstname || !*firstname)
  3020. xls.append("<td></td>");
  3021. else
  3022. xls.appendf("<td>%s</td>", firstname);
  3023. if (!lastname || !*lastname)
  3024. xls.append("<td></td>");
  3025. else
  3026. xls.appendf("<td>%s</td>", lastname);
  3027. xls.append("<td></td>");
  3028. xls.append("</tr>");
  3029. }
  3030. else
  3031. {
  3032. for(unsigned i = 0; i < groupnames1.length(); i++)
  3033. {
  3034. const char* grpname = groupnames1.item(i);
  3035. if(grpname == NULL || grpname[0] == '\0')
  3036. continue;
  3037. xls.append("<tr>");
  3038. xls.appendf("<td>%s</td>", username);
  3039. if (!firstname || !*firstname)
  3040. xls.append("<td></td>");
  3041. else
  3042. xls.appendf("<td>%s</td>", firstname);
  3043. if (!lastname || !*lastname)
  3044. xls.append("<td></td>");
  3045. else
  3046. xls.appendf("<td>%s</td>", lastname);
  3047. xls.appendf("<td>%s</td>", grpname);
  3048. xls.append("</tr>");
  3049. }
  3050. }
  3051. }
  3052. }
  3053. else if (groupnames.length() > 0)
  3054. {
  3055. for(unsigned i = 0; i < groupnames.length(); i++)
  3056. {
  3057. const char* groupname = groupnames.item(i);
  3058. if (!groupname || !*groupname)
  3059. continue;
  3060. StringArray usernames1;
  3061. ldapsecmgr->getGroupMembers(groupname, usernames1);
  3062. ///usernames1.append("_clo");
  3063. ///usernames1.append("_rkc");
  3064. for(unsigned j = 0; j < usernames1.length(); j++)
  3065. {
  3066. const char* usrname = usernames1.item(j);
  3067. if(usrname == NULL || usrname[0] == '\0')
  3068. continue;
  3069. Owned<CLdapSecUser> user = (CLdapSecUser*)secmgr->createUser(usrname);
  3070. secmgr->getUserInfo(*user.get());
  3071. const char* firstname = user->getFirstName();
  3072. const char* lastname = user->getLastName();
  3073. xls.append("<tr>");
  3074. xls.appendf("<td>%s</td>", usrname);
  3075. if (!firstname || !*firstname)
  3076. xls.append("<td></td>");
  3077. else
  3078. xls.appendf("<td>%s</td>", firstname);
  3079. if (!lastname || !*lastname)
  3080. xls.append("<td></td>");
  3081. else
  3082. xls.appendf("<td>%s</td>", lastname);
  3083. xls.appendf("<td>%s</td>", groupname);
  3084. xls.append("</tr>");
  3085. }
  3086. }
  3087. }
  3088. xls.append("</thead>");
  3089. xls.append("</table>");
  3090. xls.append("</body>");
  3091. xls.append("</html>");
  3092. MemoryBuffer buff;
  3093. buff.setBuffer(xls.length(), (void*)xls.str());
  3094. resp.setResult(buff);
  3095. resp.setResult_mimetype("application/vnd.ms-excel");
  3096. }
  3097. catch(IException* e)
  3098. {
  3099. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3100. }
  3101. return true;
  3102. }
  3103. int Cws_accessSoapBindingEx::onGetForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, const char *service, const char *method)
  3104. {
  3105. try
  3106. {
  3107. if(stricmp(method,"SecurityNotEnabled")==0)
  3108. {
  3109. StringBuffer page;
  3110. page.append(
  3111. "<html>"
  3112. "<head>"
  3113. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
  3114. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/default.css\"/>"
  3115. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/yui/build/fonts/fonts-min.css\" />"
  3116. "<title>Security Not Enabled</title>"
  3117. "</head>"
  3118. "<body>"
  3119. "<p style=\"text-align:centre;\">In order to use this feature, authentication should be enabled.");
  3120. page.append("</p></body>"
  3121. "</html>");
  3122. response->setContent(page.str());
  3123. response->setContentType("text/html");
  3124. response->send();
  3125. return 0;
  3126. }
  3127. else if(stricmp(method,"FirefoxNotSupport")==0)
  3128. {
  3129. StringBuffer page;
  3130. page.append(
  3131. "<html>"
  3132. "<head>"
  3133. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
  3134. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/default.css\"/>"
  3135. "<link rel=\"stylesheet\" type=\"text/css\" href=\"/esp/files/yui/build/fonts/fonts-min.css\" />"
  3136. "<title>Firefox Not Support</title>"
  3137. "</head>"
  3138. "<body>"
  3139. "<p style=\"text-align:centre;\">This feature is not supported under Firefox.");
  3140. page.append("</p></body>"
  3141. "</html>");
  3142. response->setContent(page.str());
  3143. response->setContentType("text/html");
  3144. response->send();
  3145. return 0;
  3146. }
  3147. }
  3148. catch(IException* e)
  3149. {
  3150. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3151. }
  3152. return onGetForm(context, request, response, service, method);
  3153. }