ws_accessService.cpp 124 KB

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