ws_accessService.cpp 130 KB

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