ws_accessService.cpp 131 KB

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