ws_accessService.cpp 123 KB

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