dna.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2007 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /**
  12. * Distributed Numeric Assignment plug-in
  13. */
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include "portable.h"
  19. #include "nspr.h"
  20. #include "slapi-private.h"
  21. #include "prclist.h"
  22. /* Required to get portable printf/scanf format macros */
  23. #ifdef HAVE_INTTYPES_H
  24. #include <inttypes.h>
  25. #else
  26. #error Need to define portable format macros such as PRIu64
  27. #endif /* HAVE_INTTYPES_H */
  28. #include <sys/stat.h>
  29. #define DNA_PLUGIN_SUBSYSTEM "dna-plugin"
  30. #define DNA_PLUGIN_VERSION 0x00020000
  31. #define DNA_DN "cn=Distributed Numeric Assignment Plugin,cn=plugins,cn=config" /* temporary */
  32. #define DNA_SUCCESS SLAPI_PLUGIN_SUCCESS
  33. #define DNA_FAILURE SLAPI_PLUGIN_FAILURE
  34. /* Default range request timeout */
  35. /* use the default replication timeout */
  36. #define DNA_DEFAULT_TIMEOUT 600 * 1000 /* 600 seconds in milliseconds */
  37. /**
  38. * DNA config types
  39. */
  40. #define DNA_TYPE "dnaType"
  41. #define DNA_PREFIX "dnaPrefix"
  42. #define DNA_NEXTVAL "dnaNextValue"
  43. #define DNA_INTERVAL "dnaInterval"
  44. #define DNA_GENERATE "dnaMagicRegen"
  45. #define DNA_FILTER "dnaFilter"
  46. #define DNA_SCOPE "dnaScope"
  47. #define DNA_EXCLUDE_SCOPE "dnaExcludeScope"
  48. #define DNA_REMOTE_BIND_DN "dnaRemoteBindDN"
  49. #define DNA_REMOTE_BIND_PW "dnaRemoteBindCred"
  50. /* since v2 */
  51. #define DNA_MAXVAL "dnaMaxValue"
  52. #define DNA_SHARED_CFG_DN "dnaSharedCfgDN"
  53. /* Shared Config */
  54. #define DNA_SHAREDCONFIG "dnaSharedConfig"
  55. #define DNA_REMAINING "dnaRemainingValues"
  56. #define DNA_THRESHOLD "dnaThreshold"
  57. #define DNA_HOSTNAME "dnaHostname"
  58. #define DNA_PORTNUM "dnaPortNum"
  59. #define DNA_SECURE_PORTNUM "dnaSecurePortNum"
  60. #define DNA_REMOTE_BUFSIZ 15 /* max size for bind method & protocol */
  61. #define DNA_REMOTE_BIND_METHOD "dnaRemoteBindMethod"
  62. #define DNA_REMOTE_CONN_PROT "dnaRemoteConnProtocol"
  63. /* Bind Methods & Protocols */
  64. #define DNA_METHOD_SIMPLE "SIMPLE"
  65. #define DNA_METHOD_SSL "SSL"
  66. #define DNA_METHOD_GSSAPI "SASL/GSSAPI"
  67. #define DNA_METHOD_DIGESTMD5 "SASL/DIGEST-MD5"
  68. #define DNA_PROT_LDAP "LDAP"
  69. #define DNA_PROT_TLS "TLS"
  70. #define DNA_PROT_SSL "SSL"
  71. /* For transferred ranges */
  72. #define DNA_NEXT_RANGE "dnaNextRange"
  73. #define DNA_RANGE_REQUEST_TIMEOUT "dnaRangeRequestTimeout"
  74. /* Replication types */
  75. #define DNA_REPL_BIND_DN "nsds5ReplicaBindDN"
  76. #define DNA_REPL_BIND_DNGROUP "nsds5ReplicaBindDNGroup"
  77. #define DNA_REPL_CREDS "nsds5ReplicaCredentials"
  78. #define DNA_REPL_BIND_METHOD "nsds5ReplicaBindMethod"
  79. #define DNA_REPL_TRANSPORT "nsds5ReplicaTransportInfo"
  80. #define DNA_REPL_PORT "nsds5ReplicaPort"
  81. #define DNA_FEATURE_DESC "Distributed Numeric Assignment"
  82. #define DNA_EXOP_FEATURE_DESC "DNA Range Extension Request"
  83. #define DNA_PLUGIN_DESC "Distributed Numeric Assignment plugin"
  84. #define DNA_INT_PREOP_DESC "Distributed Numeric Assignment internal preop plugin"
  85. #define DNA_POSTOP_DESC "Distributed Numeric Assignment postop plugin"
  86. #define DNA_EXOP_DESC "Distributed Numeric Assignment range extension extop plugin"
  87. #define DNA_BE_TXN_PREOP_DESC "Distributed Numeric Assignment backend txn preop plugin"
  88. #define DNA_NEEDS_UPDATE "-2"
  89. #define INTEGER_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.27"
  90. #define DNA_EXTEND_EXOP_REQUEST_OID "2.16.840.1.113730.3.5.10"
  91. #define DNA_EXTEND_EXOP_RESPONSE_OID "2.16.840.1.113730.3.5.11"
  92. static Slapi_PluginDesc pdesc = { DNA_FEATURE_DESC,
  93. VENDOR,
  94. DS_PACKAGE_VERSION,
  95. DNA_PLUGIN_DESC };
  96. static Slapi_PluginDesc exop_pdesc = { DNA_EXOP_FEATURE_DESC,
  97. VENDOR,
  98. DS_PACKAGE_VERSION,
  99. DNA_EXOP_DESC };
  100. /**
  101. * linked list of config entries
  102. */
  103. struct configEntry {
  104. PRCList list;
  105. char *dn;
  106. char **types;
  107. char *prefix;
  108. char *filter;
  109. Slapi_Filter *slapi_filter;
  110. char *generate;
  111. char *scope;
  112. Slapi_DN **excludescope;
  113. PRUint64 interval;
  114. PRUint64 threshold;
  115. char *shared_cfg_base;
  116. char *shared_cfg_dn;
  117. char *remote_binddn;
  118. char *remote_bindpw;
  119. PRUint64 timeout;
  120. /* This lock protects the 5 members below. All
  121. * of the above members are safe to read as long
  122. * as you call dna_read_lock() first. */
  123. Slapi_Mutex *lock;
  124. PRUint64 nextval;
  125. PRUint64 maxval;
  126. PRUint64 remaining;
  127. PRUint64 next_range_lower;
  128. PRUint64 next_range_upper;
  129. /* This lock protects the extend_in_progress
  130. * member. This is used to prevent us from
  131. * processing a range extention request and
  132. * trying to extend out own range at the same
  133. * time. */
  134. Slapi_Mutex *extend_lock;
  135. int extend_in_progress;
  136. };
  137. static PRCList *dna_global_config = NULL;
  138. static Slapi_RWLock *g_dna_cache_lock;
  139. static struct dnaServer *dna_global_servers = NULL;
  140. static Slapi_RWLock *g_dna_cache_server_lock;
  141. static void *_PluginID = NULL;
  142. static const char *_PluginDN = NULL;
  143. static char *hostname = NULL;
  144. static char *portnum = NULL;
  145. static char *secureportnum = NULL;
  146. static Slapi_Eq_Context eq_ctx = {0};
  147. /**
  148. * server struct for shared ranges
  149. */
  150. struct dnaServer {
  151. PRCList list;
  152. Slapi_DN *sdn;
  153. char *host;
  154. unsigned int port;
  155. unsigned int secureport;
  156. PRUint64 remaining;
  157. /* Remote replica settings from config */
  158. PRUint64 remote_defined;
  159. char *remote_bind_method;
  160. char *remote_conn_prot;
  161. char *remote_binddn; /* contains pointer to main config binddn */
  162. char *remote_bindpw; /* contains pointer to main config bindpw */
  163. struct dnaServer *next; /* used for the global server list */
  164. };
  165. static char *dna_extend_exop_oid_list[] = {
  166. DNA_EXTEND_EXOP_REQUEST_OID,
  167. NULL
  168. };
  169. /**
  170. *
  171. * DNA plug-in management functions
  172. *
  173. */
  174. int dna_init(Slapi_PBlock * pb);
  175. static int dna_start(Slapi_PBlock * pb);
  176. static int dna_close(Slapi_PBlock * pb);
  177. static int dna_postop_init(Slapi_PBlock * pb);
  178. static int dna_exop_init(Slapi_PBlock * pb);
  179. static int dna_be_txn_preop_init(Slapi_PBlock *pb);
  180. /**
  181. *
  182. * Local operation functions
  183. *
  184. */
  185. static int dna_load_plugin_config(Slapi_PBlock *pb, int use_eventq);
  186. static int dna_parse_config_entry(Slapi_PBlock *pb, Slapi_Entry * e, int apply);
  187. static void dna_delete_config(PRCList *list);
  188. static void dna_free_config_entry(struct configEntry ** entry);
  189. static int dna_load_host_port();
  190. /**
  191. *
  192. * helpers
  193. *
  194. */
  195. static char *dna_get_dn(Slapi_PBlock * pb);
  196. static int dna_dn_is_config(char *dn);
  197. static int dna_get_next_value(struct configEntry * config_entry,
  198. char **next_value_ret);
  199. static int dna_first_free_value(struct configEntry *config_entry,
  200. PRUint64 *newval);
  201. static int dna_fix_maxval(struct configEntry *config_entry,
  202. int skip_range_request);
  203. static void dna_notice_allocation(struct configEntry *config_entry,
  204. PRUint64 new, PRUint64 last);
  205. static int dna_update_shared_config(struct configEntry * config_entry);
  206. static void dna_update_config_event(time_t event_time, void *arg);
  207. static int dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers, int get_all);
  208. static void dna_free_shared_server(struct dnaServer **server);
  209. static void dna_delete_shared_servers(PRCList **servers);
  210. static int dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper);
  211. static int dna_request_range(struct configEntry *config_entry,
  212. struct dnaServer *server,
  213. PRUint64 *lower, PRUint64 *upper);
  214. static struct berval *dna_create_range_request(char *range_dn);
  215. static int dna_update_next_range(struct configEntry *config_entry,
  216. PRUint64 lower, PRUint64 upper);
  217. static int dna_activate_next_range(struct configEntry *config_entry);
  218. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn);
  219. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  220. char **bind_dn, char **bind_passwd,
  221. char **bind_method, int *is_ssl, int *port);
  222. static int dna_list_contains_type(char **list, char *type);
  223. static int dna_list_contains_types(char **list, char **types);
  224. static void dna_list_remove_type(char **list, char *type);
  225. static int dna_is_multitype_range(struct configEntry *config_entry);
  226. static void dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter);
  227. static int dna_isrepl(Slapi_PBlock *pb);
  228. static int dna_get_remote_config_info( struct dnaServer *server, char **bind_dn, char **bind_passwd,
  229. char **bind_method, int *is_ssl, int *port);
  230. static int dna_load_shared_servers();
  231. static void dna_delete_global_servers();
  232. static int dna_get_shared_config_attr_val(struct configEntry *config_entry, char *attr, char *value);
  233. static PRCList *dna_config_copy();
  234. /**
  235. *
  236. * the ops (where the real work is done)
  237. *
  238. */
  239. static int dna_config_check_post_op(Slapi_PBlock * pb);
  240. static int dna_pre_op(Slapi_PBlock * pb, int modtype);
  241. static int dna_mod_pre_op(Slapi_PBlock * pb);
  242. static int dna_add_pre_op(Slapi_PBlock * pb);
  243. static int dna_extend_exop(Slapi_PBlock *pb);
  244. static int dna_be_txn_pre_op(Slapi_PBlock *pb, int modtype);
  245. static int dna_be_txn_add_pre_op(Slapi_PBlock *pb);
  246. static int dna_be_txn_mod_pre_op(Slapi_PBlock *pb);
  247. /**
  248. * debug functions - global, for the debugger
  249. */
  250. void dna_dump_config();
  251. void dna_dump_config_entry(struct configEntry *);
  252. /*
  253. * During the plugin startup we delay the creation of the shared config entries
  254. * so all the other betxn plugins have time to start. During the "delayed"
  255. * update config event we will need a copy of the global config, as we can not
  256. * hold the read lock while starting a transaction(potential deadlock).
  257. */
  258. static PRCList *
  259. dna_config_copy()
  260. {
  261. PRCList *copy = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  262. PRCList *config_list;
  263. PR_INIT_CLIST(copy);
  264. int first = 1;
  265. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  266. config_list = PR_LIST_HEAD(dna_global_config);
  267. while (config_list != dna_global_config) {
  268. struct configEntry *new_entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  269. struct configEntry *config_entry = (struct configEntry *) config_list;
  270. new_entry->dn = slapi_ch_strdup(config_entry->dn);
  271. new_entry->types = slapi_ch_array_dup(config_entry->types);
  272. new_entry->prefix = slapi_ch_strdup(config_entry->prefix);
  273. new_entry->filter = slapi_ch_strdup(config_entry->filter);
  274. new_entry->slapi_filter = slapi_filter_dup(config_entry->slapi_filter);
  275. new_entry->generate = slapi_ch_strdup(config_entry->generate);
  276. new_entry->scope = slapi_ch_strdup(config_entry->scope);
  277. if (config_entry->excludescope == NULL) {
  278. new_entry->excludescope = NULL;
  279. } else {
  280. int i;
  281. for (i = 0; config_entry->excludescope[i]; i++);
  282. new_entry->excludescope = (Slapi_DN **) slapi_ch_calloc(sizeof (Slapi_DN *), i + 1);
  283. for (i = 0; new_entry->excludescope[i]; i++) {
  284. new_entry->excludescope[i] = slapi_sdn_dup(config_entry->excludescope[i]);
  285. }
  286. }
  287. new_entry->shared_cfg_base = slapi_ch_strdup(config_entry->shared_cfg_base);
  288. new_entry->shared_cfg_dn = slapi_ch_strdup(config_entry->shared_cfg_dn);
  289. new_entry->remote_binddn = slapi_ch_strdup(config_entry->remote_binddn);
  290. new_entry->remote_bindpw = slapi_ch_strdup(config_entry->remote_bindpw);
  291. new_entry->timeout = config_entry->timeout;
  292. new_entry->interval = config_entry->interval;
  293. new_entry->threshold = config_entry->threshold;
  294. new_entry->nextval = config_entry->nextval;
  295. new_entry->maxval = config_entry->maxval;
  296. new_entry->remaining = config_entry->remaining;
  297. new_entry->extend_in_progress = config_entry->extend_in_progress;
  298. new_entry->next_range_lower = config_entry->next_range_lower;
  299. new_entry->next_range_upper = config_entry->next_range_upper;
  300. new_entry->lock = NULL;
  301. new_entry->extend_lock = NULL;
  302. if(first){
  303. PR_INSERT_LINK(&(new_entry->list), copy);
  304. first = 0;
  305. } else {
  306. PR_INSERT_BEFORE(&(new_entry->list), copy);
  307. }
  308. config_list = PR_NEXT_LINK(config_list);
  309. }
  310. }
  311. return copy;
  312. }
  313. /**
  314. *
  315. * Deal with cache locking
  316. *
  317. */
  318. void dna_read_lock()
  319. {
  320. slapi_rwlock_rdlock(g_dna_cache_lock);
  321. }
  322. void dna_write_lock()
  323. {
  324. slapi_rwlock_wrlock(g_dna_cache_lock);
  325. }
  326. void dna_unlock()
  327. {
  328. slapi_rwlock_unlock(g_dna_cache_lock);
  329. }
  330. void dna_server_read_lock()
  331. {
  332. slapi_rwlock_rdlock(g_dna_cache_server_lock);
  333. }
  334. void dna_server_write_lock()
  335. {
  336. slapi_rwlock_wrlock(g_dna_cache_server_lock);
  337. }
  338. void dna_server_unlock()
  339. {
  340. slapi_rwlock_unlock(g_dna_cache_server_lock);
  341. }
  342. /**
  343. *
  344. * Get the dna plug-in version
  345. *
  346. */
  347. int dna_version()
  348. {
  349. return DNA_PLUGIN_VERSION;
  350. }
  351. /**
  352. * Plugin identity mgmt
  353. */
  354. void setPluginID(void *pluginID)
  355. {
  356. _PluginID = pluginID;
  357. }
  358. void *getPluginID()
  359. {
  360. return _PluginID;
  361. }
  362. void setPluginDN(const char *pluginDN)
  363. {
  364. _PluginDN = pluginDN;
  365. }
  366. const char *getPluginDN()
  367. {
  368. return _PluginDN;
  369. }
  370. /*
  371. dna_init
  372. -------------
  373. adds our callbacks to the list
  374. */
  375. int
  376. dna_init(Slapi_PBlock *pb)
  377. {
  378. int status = DNA_SUCCESS;
  379. char *plugin_identity = NULL;
  380. char *plugin_type = NULL;
  381. int preadd = SLAPI_PLUGIN_BE_PRE_ADD_FN;
  382. int premod = SLAPI_PLUGIN_BE_PRE_MODIFY_FN;
  383. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  384. "--> dna_init\n");
  385. /**
  386. * Store the plugin identity for later use.
  387. * Used for internal operations
  388. */
  389. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  390. PR_ASSERT(plugin_identity);
  391. setPluginID(plugin_identity);
  392. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  393. SLAPI_PLUGIN_VERSION_01) != 0 ||
  394. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  395. (void *) dna_start) != 0 ||
  396. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  397. (void *) dna_close) != 0 ||
  398. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  399. (void *) &pdesc) != 0 ||
  400. slapi_pblock_set(pb, premod, (void *) dna_mod_pre_op) != 0 ||
  401. slapi_pblock_set(pb, preadd, (void *) dna_add_pre_op) != 0) {
  402. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  403. "dna_init: failed to register plugin\n");
  404. status = DNA_FAILURE;
  405. }
  406. if (status == DNA_SUCCESS) {
  407. plugin_type = "betxnpostoperation";
  408. /* the config change checking post op */
  409. if (slapi_register_plugin(plugin_type, /* op type */
  410. 1, /* Enabled */
  411. "dna_init", /* this function desc */
  412. dna_postop_init, /* init func for post op */
  413. DNA_POSTOP_DESC, /* plugin desc */
  414. NULL, /* ? */
  415. plugin_identity /* access control */
  416. )) {
  417. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  418. "dna_init: failed to register postop plugin\n");
  419. status = DNA_FAILURE;
  420. }
  421. }
  422. if ((status == DNA_SUCCESS) &&
  423. /* the range extension extended operation */
  424. slapi_register_plugin("extendedop", /* op type */
  425. 1, /* Enabled */
  426. "dna_init", /* this function desc */
  427. dna_exop_init, /* init func for exop */
  428. DNA_EXOP_DESC, /* plugin desc */
  429. NULL, /* ? */
  430. plugin_identity /* access control */
  431. )
  432. ) {
  433. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  434. "dna_init: failed to register plugin\n");
  435. status = DNA_FAILURE;
  436. }
  437. if (status == DNA_SUCCESS) {
  438. plugin_type = "betxnpreoperation";
  439. /* the config change checking post op */
  440. if (slapi_register_plugin(plugin_type, /* op type */
  441. 1, /* Enabled */
  442. "dna_init", /* this function desc */
  443. dna_be_txn_preop_init, /* init func for post op */
  444. DNA_BE_TXN_PREOP_DESC, /* plugin desc */
  445. NULL, /* ? */
  446. plugin_identity /* access control */
  447. )) {
  448. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  449. "dna_init: failed to register be_txn_pre_op plugin\n");
  450. status = DNA_FAILURE;
  451. }
  452. }
  453. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  454. "<-- dna_init\n");
  455. return status;
  456. }
  457. static int
  458. dna_postop_init(Slapi_PBlock *pb)
  459. {
  460. int status = DNA_SUCCESS;
  461. int addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  462. int delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  463. int modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  464. int mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  465. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  466. SLAPI_PLUGIN_VERSION_01) != 0 ||
  467. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  468. (void *) &pdesc) != 0 ||
  469. slapi_pblock_set(pb, addfn, (void *) dna_config_check_post_op) != 0 ||
  470. slapi_pblock_set(pb, mdnfn, (void *) dna_config_check_post_op) != 0 ||
  471. slapi_pblock_set(pb, delfn, (void *) dna_config_check_post_op) != 0 ||
  472. slapi_pblock_set(pb, modfn, (void *) dna_config_check_post_op) != 0) {
  473. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  474. "dna_postop_init: failed to register plugin\n");
  475. status = DNA_FAILURE;
  476. }
  477. return status;
  478. }
  479. static int
  480. dna_exop_init(Slapi_PBlock * pb)
  481. {
  482. int status = DNA_SUCCESS;
  483. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  484. SLAPI_PLUGIN_VERSION_01) != 0 ||
  485. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  486. (void *) &exop_pdesc) != 0 ||
  487. slapi_pblock_set(pb, SLAPI_PLUGIN_EXT_OP_OIDLIST,
  488. (void *) dna_extend_exop_oid_list) != 0 ||
  489. slapi_pblock_set(pb, SLAPI_PLUGIN_EXT_OP_FN,
  490. (void *) dna_extend_exop) != 0) {
  491. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  492. "dna_exop_init: failed to register plugin\n");
  493. status = DNA_FAILURE;
  494. }
  495. return status;
  496. }
  497. static int
  498. dna_be_txn_preop_init(Slapi_PBlock *pb){
  499. int status = DNA_SUCCESS;
  500. if( slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01) != 0 ||
  501. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *) &pdesc) != 0 ||
  502. slapi_pblock_set(pb, SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN, (void *)dna_be_txn_add_pre_op) != 0 ||
  503. slapi_pblock_set(pb, SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN, (void *)dna_be_txn_mod_pre_op) != 0){
  504. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  505. "dna_init: failed to register be_txn_pre_op plugin\n");
  506. status = DNA_FAILURE;
  507. }
  508. return status;
  509. }
  510. /*
  511. dna_start
  512. --------------
  513. Kicks off the config cache.
  514. It is called after dna_init.
  515. */
  516. static int
  517. dna_start(Slapi_PBlock * pb)
  518. {
  519. Slapi_DN *pluginsdn = NULL;
  520. const char *plugindn = NULL;
  521. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  522. "--> dna_start\n");
  523. g_dna_cache_lock = slapi_new_rwlock();
  524. if (!g_dna_cache_lock) {
  525. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  526. "dna_start: global config lock creation failed\n");
  527. return DNA_FAILURE;
  528. }
  529. g_dna_cache_server_lock = slapi_new_rwlock();
  530. if (!g_dna_cache_server_lock) {
  531. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  532. "dna_start: global server lock creation failed\n");
  533. return DNA_FAILURE;
  534. }
  535. /**
  536. * Get the plug-in target dn from the system
  537. * and store it for future use. This should avoid
  538. * hardcoding of DN's in the code.
  539. */
  540. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &pluginsdn);
  541. if (NULL == pluginsdn || 0 == slapi_sdn_get_ndn_len(pluginsdn)) {
  542. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  543. "dna_start: had to use hard coded config dn\n");
  544. plugindn = DNA_DN;
  545. } else {
  546. plugindn = slapi_sdn_get_dn(pluginsdn);
  547. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  548. "dna_start: config at %s\n", plugindn);
  549. }
  550. setPluginDN(plugindn);
  551. /* We need the host and port number of this server
  552. * in case shared config is enabled for any of the
  553. * ranges we are managing. */
  554. if (dna_load_host_port() != DNA_SUCCESS) {
  555. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  556. "dna_start: unable to load host and port information\n");
  557. }
  558. /*
  559. * Load the config for our plug-in
  560. */
  561. dna_global_config = (PRCList *)
  562. slapi_ch_calloc(1, sizeof(struct configEntry));
  563. PR_INIT_CLIST(dna_global_config);
  564. if (dna_load_plugin_config(pb, 1/* use eventq */) != DNA_SUCCESS) {
  565. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  566. "dna_start: unable to load plug-in configuration\n");
  567. return DNA_FAILURE;
  568. }
  569. /*
  570. * Load all shared server configs
  571. */
  572. if (dna_load_shared_servers() ) {
  573. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  574. "dna_start: shared config server initialization failed.\n");
  575. return DNA_FAILURE;
  576. }
  577. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  578. "dna: ready for service\n");
  579. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  580. "<-- dna_start\n");
  581. return DNA_SUCCESS;
  582. }
  583. /*
  584. dna_close
  585. --------------
  586. closes down the cache
  587. */
  588. static int
  589. dna_close(Slapi_PBlock * pb)
  590. {
  591. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  592. "--> dna_close\n");
  593. slapi_eq_cancel(eq_ctx);
  594. dna_delete_config(NULL);
  595. slapi_ch_free((void **)&dna_global_config);
  596. slapi_destroy_rwlock(g_dna_cache_lock);
  597. g_dna_cache_lock = NULL;
  598. dna_delete_global_servers();
  599. slapi_destroy_rwlock(g_dna_cache_server_lock);
  600. g_dna_cache_server_lock = NULL;
  601. slapi_ch_free_string(&hostname);
  602. slapi_ch_free_string(&portnum);
  603. slapi_ch_free_string(&secureportnum);
  604. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  605. "<-- dna_close\n");
  606. return DNA_SUCCESS;
  607. }
  608. /*
  609. * Free the global linkedl ist of shared servers
  610. */
  611. static void
  612. dna_delete_global_servers()
  613. {
  614. struct dnaServer *server, *next;
  615. if(dna_global_servers){
  616. server = dna_global_servers;
  617. while(server){
  618. next = server->next;
  619. dna_free_shared_server(&server);
  620. server = next;
  621. }
  622. dna_global_servers = NULL;
  623. }
  624. }
  625. /*
  626. * Look through the global config entries, and build a global
  627. * shared server config list.
  628. */
  629. static int
  630. dna_load_shared_servers()
  631. {
  632. struct configEntry *config_entry = NULL;
  633. struct dnaServer *server = NULL, *global_servers = NULL;
  634. PRCList *server_list = NULL;
  635. PRCList *config_list = NULL;
  636. int freed_servers = 0;
  637. int ret = 0;
  638. /* Now build the new list. */
  639. dna_write_lock();
  640. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  641. config_list = PR_LIST_HEAD(dna_global_config);
  642. while (config_list != dna_global_config) {
  643. PRCList *shared_list = NULL;
  644. config_entry = (struct configEntry *) config_list;
  645. if(dna_get_shared_servers(config_entry,
  646. &shared_list,
  647. 1 /* get all the servers */))
  648. {
  649. dna_unlock();
  650. return -1;
  651. }
  652. dna_server_write_lock();
  653. if(!freed_servers){
  654. dna_delete_global_servers();
  655. freed_servers = 1;
  656. }
  657. if (shared_list) {
  658. server_list = PR_LIST_HEAD(shared_list);
  659. while (server_list != shared_list) {
  660. server = (struct dnaServer *)server_list;
  661. if(global_servers == NULL){
  662. dna_global_servers = global_servers = server;
  663. } else {
  664. global_servers->next = server;
  665. global_servers = server;
  666. }
  667. server_list = PR_NEXT_LINK(server_list);
  668. }
  669. slapi_ch_free((void **)&shared_list);
  670. }
  671. dna_server_unlock();
  672. config_list = PR_NEXT_LINK(config_list);
  673. }
  674. }
  675. dna_unlock();
  676. return ret;
  677. }
  678. /*
  679. * config looks like this
  680. * - cn=myplugin
  681. * --- cn=posix
  682. * ------ cn=accounts
  683. * ------ cn=groups
  684. * --- cn=samba
  685. * --- cn=etc
  686. * ------ cn=etc etc
  687. */
  688. static int
  689. dna_load_plugin_config(Slapi_PBlock *pb, int use_eventq)
  690. {
  691. int status = DNA_SUCCESS;
  692. int result;
  693. int i;
  694. time_t now;
  695. Slapi_PBlock *search_pb;
  696. Slapi_Entry **entries = NULL;
  697. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  698. "--> dna_load_plugin_config %s\n",
  699. use_eventq?"using event queue":"");
  700. dna_write_lock();
  701. dna_delete_config(NULL);
  702. search_pb = slapi_pblock_new();
  703. slapi_search_internal_set_pb(search_pb, getPluginDN(),
  704. LDAP_SCOPE_SUBTREE, "objectclass=*",
  705. NULL, 0, NULL, NULL, getPluginID(), 0);
  706. slapi_search_internal_pb(search_pb);
  707. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  708. if (LDAP_SUCCESS != result) {
  709. status = DNA_FAILURE;
  710. dna_unlock();
  711. goto cleanup;
  712. }
  713. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  714. &entries);
  715. if (NULL == entries || NULL == entries[0]) {
  716. status = DNA_SUCCESS;
  717. dna_unlock();
  718. goto cleanup;
  719. }
  720. for (i = 0; (entries[i] != NULL); i++) {
  721. /* We don't care about the status here because we may have
  722. * some invalid config entries, but we just want to continue
  723. * looking for valid ones. */
  724. dna_parse_config_entry(pb, entries[i], 1);
  725. }
  726. dna_unlock();
  727. if (use_eventq) {
  728. /* Setup an event to update the shared config 30
  729. * seconds from now. We need to do this since
  730. * performing the operation at this point when
  731. * starting up would cause the change to not
  732. * get changelogged. */
  733. time(&now);
  734. eq_ctx = slapi_eq_once(dna_update_config_event, NULL, now + 30);
  735. } else {
  736. dna_update_config_event(0, NULL);
  737. }
  738. cleanup:
  739. slapi_free_search_results_internal(search_pb);
  740. slapi_pblock_destroy(search_pb);
  741. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  742. "<-- dna_load_plugin_config\n");
  743. return status;
  744. }
  745. /*
  746. * dna_parse_config_entry()
  747. *
  748. * Parses a single config entry. If apply is non-zero, then
  749. * we will load and start using the new config. You can simply
  750. * validate config without making any changes by setting apply
  751. * to 0.
  752. *
  753. * Returns DNA_SUCCESS if the entry is valid and DNA_FAILURE
  754. * if it is invalid.
  755. */
  756. static int
  757. dna_parse_config_entry(Slapi_PBlock *pb, Slapi_Entry * e, int apply)
  758. {
  759. char *value;
  760. struct configEntry *entry = NULL;
  761. struct configEntry *config_entry;
  762. PRCList *list;
  763. int entry_added = 0;
  764. int i = 0;
  765. int ret = DNA_SUCCESS;
  766. char **plugin_attr_values;
  767. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  768. "--> dna_parse_config_entry\n");
  769. /* If this is the main DNA plug-in
  770. * config entry, just bail. */
  771. if (strcasecmp(getPluginDN(), slapi_entry_get_ndn(e)) == 0) {
  772. ret = DNA_SUCCESS;
  773. goto bail;
  774. }
  775. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  776. value = slapi_entry_get_ndn(e);
  777. if (value) {
  778. entry->dn = slapi_ch_strdup(value);
  779. }
  780. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  781. "----------> dn [%s]\n", entry->dn);
  782. entry->types = slapi_entry_attr_get_charray(e, DNA_TYPE);
  783. if (entry->types == NULL) {
  784. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  785. "dna_parse_config_entry: The %s config "
  786. "setting is required for range %s.\n",
  787. DNA_TYPE, entry->dn);
  788. ret = DNA_FAILURE;
  789. goto bail;
  790. }
  791. for (i = 0; entry->types && entry->types[i]; i++) {
  792. if (!slapi_attr_syntax_exists(entry->types[i])){
  793. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  794. "dna_parse_config_entry: dnaType (%s) does "
  795. "not exist.\n",
  796. entry->types[i]);
  797. ret = DNA_FAILURE;
  798. goto bail;
  799. }
  800. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  801. "----------> %s [%s]\n", DNA_TYPE, entry->types[i]);
  802. }
  803. value = slapi_entry_attr_get_charptr(e, DNA_NEXTVAL);
  804. if (value) {
  805. entry->nextval = strtoull(value, 0, 0);
  806. slapi_ch_free_string(&value);
  807. } else {
  808. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  809. "dna_parse_config_entry: The %s config "
  810. "setting is required for range %s.\n",
  811. DNA_NEXTVAL, entry->dn);
  812. ret = DNA_FAILURE;
  813. goto bail;
  814. }
  815. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  816. "----------> %s [%" NSPRIu64 "]\n", DNA_NEXTVAL,
  817. entry->nextval);
  818. value = slapi_entry_attr_get_charptr(e, DNA_PREFIX);
  819. if (value && value[0]) {
  820. entry->prefix = value;
  821. } else {
  822. /* TODO - If a prefix is not defined, then we need to ensure
  823. * that the proper matching rule is in place for this
  824. * attribute type. We require this since we internally
  825. * perform a sorted range search on what we assume to
  826. * be an INTEGER syntax. */
  827. slapi_ch_free_string(&value);
  828. }
  829. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  830. "----------> %s [%s]\n", DNA_PREFIX, entry->prefix);
  831. /* Set the default interval to 1 */
  832. entry->interval = 1;
  833. #ifdef DNA_ENABLE_INTERVAL
  834. value = slapi_entry_attr_get_charptr(e, DNA_INTERVAL);
  835. if (value) {
  836. entry->interval = strtoull(value, 0, 0);
  837. slapi_ch_free_string(&value);
  838. }
  839. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  840. "----------> %s [%" NSPRIu64 "]\n", DNA_INTERVAL, entry->interval);
  841. #endif
  842. value = slapi_entry_attr_get_charptr(e, DNA_GENERATE);
  843. if (value) {
  844. entry->generate = value;
  845. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  846. "----------> %s [%s]\n", DNA_GENERATE, entry->generate);
  847. }
  848. value = slapi_entry_attr_get_charptr(e, DNA_FILTER);
  849. if (value) {
  850. entry->filter = value;
  851. if (NULL == (entry->slapi_filter = slapi_str2filter(value))) {
  852. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM ,
  853. "Error: Invalid search filter in entry [%s]: [%s]\n",
  854. entry->dn, value);
  855. ret = DNA_FAILURE;
  856. goto bail;
  857. }
  858. } else {
  859. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  860. "dna_parse_config_entry: The %s config "
  861. "setting is required for range %s.\n",
  862. DNA_FILTER, entry->dn);
  863. ret = DNA_FAILURE;
  864. goto bail;
  865. }
  866. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  867. "----------> %s [%s]\n", DNA_FILTER, value);
  868. value = slapi_entry_attr_get_charptr(e, DNA_SCOPE);
  869. if (value) {
  870. Slapi_DN *test_dn = NULL;
  871. /* TODO - Allow multiple scope settings for a single range. This may
  872. * make ordering the scopes tough when we put them in the clist. */
  873. entry->scope = value;
  874. /* Check if the scope is a valid DN. We want to normalize the DN
  875. * first to allow old config entries with things like spaces between
  876. * RDN elements to still work. */
  877. test_dn = slapi_sdn_new_dn_byref(value);
  878. if (slapi_dn_syntax_check(NULL, (char *)slapi_sdn_get_ndn(test_dn), 1) == 1) {
  879. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  880. "Error: Invalid DN used as scope in entry [%s]: [%s]\n",
  881. entry->dn, value);
  882. ret = DNA_FAILURE;
  883. slapi_sdn_free(&test_dn);
  884. goto bail;
  885. }
  886. slapi_sdn_free(&test_dn);
  887. } else {
  888. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  889. "dna_parse_config_entry: The %s config "
  890. "config setting is required for range %s.\n",
  891. DNA_SCOPE, entry->dn);
  892. ret = DNA_FAILURE;
  893. goto bail;
  894. }
  895. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  896. "----------> %s [%s]\n", DNA_SCOPE, entry->scope);
  897. plugin_attr_values = slapi_entry_attr_get_charray(e, DNA_EXCLUDE_SCOPE);
  898. if (plugin_attr_values) {
  899. int j = 0;
  900. /* Allocate the array of excluded scopes */
  901. for (i = 0; plugin_attr_values[i]; i++);
  902. entry->excludescope = (Slapi_DN **) slapi_ch_calloc(sizeof (Slapi_DN *), i + 1);
  903. /* Copy them in the config at the condition they are valid DN */
  904. for (i = 0; plugin_attr_values[i]; i++) {
  905. if (slapi_dn_syntax_check(NULL, plugin_attr_values[i], 1) == 1) {
  906. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  907. "Error: Ignoring invalid DN used as excluded scope: [%s]\n", plugin_attr_values[i]);
  908. slapi_ch_free_string(&plugin_attr_values[i]);
  909. } else {
  910. entry->excludescope[j++] = slapi_sdn_new_dn_passin(plugin_attr_values[i]);
  911. }
  912. }
  913. slapi_ch_free((void**) &plugin_attr_values);
  914. }
  915. for (i = 0; entry->excludescope && entry->excludescope[i]; i++) {
  916. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  917. "----------> %s[%d] [%s]\n", DNA_EXCLUDE_SCOPE, i, slapi_sdn_get_dn(entry->excludescope[i]));
  918. }
  919. /* optional, if not specified set -1 which is converted to the max unsigned
  920. * value */
  921. value = slapi_entry_attr_get_charptr(e, DNA_MAXVAL);
  922. if (value) {
  923. entry->maxval = strtoull(value, 0, 0);
  924. slapi_ch_free_string(&value);
  925. } else {
  926. entry->maxval = -1;
  927. }
  928. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  929. "----------> %s [%" NSPRIu64 "]\n", DNA_MAXVAL,
  930. entry->maxval);
  931. /* get the global bind dn and password(if any) */
  932. value = slapi_entry_attr_get_charptr(e, DNA_REMOTE_BIND_DN);
  933. if (value) {
  934. Slapi_DN *sdn = NULL;
  935. char *normdn = NULL;
  936. sdn = slapi_sdn_new_dn_passin(value);
  937. if (!sdn) {
  938. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  939. "dna_parse_config_entry: Unable to create "
  940. "slapi_dn from dnaRemoteBindDN (%s)\n", value);
  941. ret = DNA_FAILURE;
  942. slapi_ch_free_string(&value);
  943. goto bail;
  944. }
  945. normdn = (char *)slapi_sdn_get_dn(sdn);
  946. if (NULL == normdn) {
  947. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  948. "dna_parse_config_entry: failed to normalize dn: "
  949. "%s\n", value);
  950. ret = DNA_FAILURE;
  951. slapi_sdn_free(&sdn);
  952. goto bail;
  953. }
  954. entry->remote_binddn = slapi_ch_strdup(normdn);
  955. slapi_sdn_free(&sdn);
  956. }
  957. /* now grab the password */
  958. entry->remote_bindpw = slapi_entry_attr_get_charptr(e, DNA_REMOTE_BIND_PW);
  959. /* validate that we have both a bind dn or password, or we have none */
  960. if((entry->remote_bindpw != NULL && entry->remote_binddn == NULL) ||
  961. (entry->remote_binddn != NULL && entry->remote_bindpw == NULL)){
  962. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  963. "dna_parse_config_entry: Invalid remote bind DN and password settings.\n");
  964. ret = DNA_FAILURE;
  965. goto bail;
  966. }
  967. value = slapi_entry_attr_get_charptr(e, DNA_SHARED_CFG_DN);
  968. if (value) {
  969. Slapi_Entry *shared_e = NULL;
  970. Slapi_DN *sdn = NULL;
  971. char *normdn = NULL;
  972. char *attrs[2];
  973. sdn = slapi_sdn_new_dn_passin(value);
  974. if (!sdn) {
  975. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  976. "dna_parse_config_entry: Unable to create "
  977. "slapi_dn (%s)\n", value);
  978. ret = DNA_FAILURE;
  979. slapi_ch_free_string(&value);
  980. goto bail;
  981. }
  982. /* We don't need attributes */
  983. attrs[0] = "cn";
  984. attrs[1] = NULL;
  985. slapi_search_internal_get_entry(sdn, attrs, &shared_e, getPluginID());
  986. /* Make sure that the shared config entry exists. */
  987. if (!shared_e) {
  988. /* We didn't locate the shared config container entry. Log
  989. * a message and skip this config entry. */
  990. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  991. "dna_parse_config_entry: Unable to locate "
  992. "shared configuration entry (%s)\n", value);
  993. ret = DNA_FAILURE;
  994. slapi_sdn_free(&sdn);
  995. goto bail;
  996. } else {
  997. slapi_entry_free(shared_e);
  998. shared_e = NULL;
  999. }
  1000. normdn = (char *)slapi_sdn_get_dn(sdn);
  1001. if (NULL == normdn) {
  1002. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1003. "dna_parse_config_entry: failed to normalize dn: "
  1004. "%s\n", value);
  1005. ret = DNA_FAILURE;
  1006. slapi_sdn_free(&sdn);
  1007. goto bail;
  1008. }
  1009. entry->shared_cfg_base = slapi_ch_strdup(normdn);
  1010. slapi_sdn_free(&sdn);
  1011. /* We prepend the host & port of this instance as a
  1012. * multi-part RDN for the shared config entry. */
  1013. normdn = slapi_create_dn_string("%s=%s+%s=%s,%s", DNA_HOSTNAME,
  1014. hostname, DNA_PORTNUM, portnum,
  1015. entry->shared_cfg_base);
  1016. if (NULL == normdn) {
  1017. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1018. "dna_parse_config_entry: failed to create dn: "
  1019. "%s=%s+%s=%s,%s", DNA_HOSTNAME,
  1020. hostname, DNA_PORTNUM, portnum, value);
  1021. ret = DNA_FAILURE;
  1022. goto bail;
  1023. }
  1024. entry->shared_cfg_dn = normdn;
  1025. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  1026. "----------> %s [%s]\n", DNA_SHARED_CFG_DN,
  1027. entry->shared_cfg_base);
  1028. }
  1029. value = slapi_entry_attr_get_charptr(e, DNA_THRESHOLD);
  1030. if (value) {
  1031. entry->threshold = strtoull(value, 0, 0);
  1032. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  1033. "----------> %s [%s]\n", DNA_THRESHOLD, value);
  1034. slapi_ch_free_string(&value);
  1035. } else {
  1036. entry->threshold = 1;
  1037. }
  1038. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  1039. "----------> %s [%" NSPRIu64 "]\n", DNA_THRESHOLD,
  1040. entry->threshold);
  1041. value = slapi_entry_attr_get_charptr(e, DNA_RANGE_REQUEST_TIMEOUT);
  1042. if (value) {
  1043. entry->timeout = strtoull(value, 0, 0);
  1044. slapi_ch_free_string(&value);
  1045. } else {
  1046. entry->timeout = DNA_DEFAULT_TIMEOUT;
  1047. }
  1048. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  1049. "----------> %s [%" NSPRIu64 "]\n", DNA_RANGE_REQUEST_TIMEOUT,
  1050. entry->timeout);
  1051. value = slapi_entry_attr_get_charptr(e, DNA_NEXT_RANGE);
  1052. if (value) {
  1053. char *p = NULL;
  1054. /* the next range value is in the form "<lower>-<upper>" */
  1055. if ((p = strchr(value, '-')) != NULL) {
  1056. *p = '\0';
  1057. ++p;
  1058. entry->next_range_lower = strtoull(value, 0, 0);
  1059. entry->next_range_upper = strtoull(p, 0, 0);
  1060. /* validate that upper is greater than lower */
  1061. if (entry->next_range_upper <= entry->next_range_lower) {
  1062. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1063. "dna_parse_config_entry: Illegal %s "
  1064. "setting specified for range %s. Legal "
  1065. "format is <lower>-<upper>.\n",
  1066. DNA_NEXT_RANGE, entry->dn);
  1067. ret = DNA_FAILURE;
  1068. entry->next_range_lower = 0;
  1069. entry->next_range_upper = 0;
  1070. }
  1071. /* make sure next range doesn't overlap with
  1072. * the active range */
  1073. if (((entry->next_range_upper <= entry->maxval) &&
  1074. (entry->next_range_upper >= entry->nextval)) ||
  1075. ((entry->next_range_lower <= entry->maxval) &&
  1076. (entry->next_range_lower >= entry->nextval))) {
  1077. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1078. "dna_parse_config_entry: Illegal %s "
  1079. "setting specified for range %s. %s "
  1080. "overlaps with the active range.\n",
  1081. DNA_NEXT_RANGE, entry->dn, DNA_NEXT_RANGE);
  1082. ret = DNA_FAILURE;
  1083. entry->next_range_lower = 0;
  1084. entry->next_range_upper = 0;
  1085. }
  1086. } else {
  1087. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1088. "dna_parse_config_entry: Illegal %s "
  1089. "setting specified for range %s. Legal "
  1090. "format is <lower>-<upper>.\n",
  1091. DNA_NEXT_RANGE, entry->dn);
  1092. ret = DNA_FAILURE;
  1093. }
  1094. slapi_ch_free_string(&value);
  1095. }
  1096. /* If we were only called to validate config, we can
  1097. * just bail out before applying the config changes */
  1098. if (apply == 0) {
  1099. goto bail;
  1100. }
  1101. /* Calculate number of remaining values. */
  1102. if (entry->next_range_lower != 0) {
  1103. entry->remaining = ((entry->next_range_upper - entry->next_range_lower + 1) /
  1104. entry->interval) + ((entry->maxval - entry->nextval + 1) /
  1105. entry->interval);
  1106. } else if (entry->nextval >= entry->maxval) {
  1107. entry->remaining = 0;
  1108. } else {
  1109. entry->remaining = ((entry->maxval - entry->nextval + 1) /
  1110. entry->interval);
  1111. }
  1112. /* create the new value lock for this range */
  1113. entry->lock = slapi_new_mutex();
  1114. if (!entry->lock) {
  1115. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1116. "dna_parse_config_entry: Unable to create lock "
  1117. "for range %s.\n", entry->dn);
  1118. ret = DNA_FAILURE;
  1119. goto bail;
  1120. }
  1121. /* Check if the shared config base matches the config scope and filter */
  1122. if (entry->scope && slapi_dn_issuffix(entry->shared_cfg_base, entry->scope)){
  1123. if (entry->slapi_filter) {
  1124. ret = slapi_vattr_filter_test(pb, e, entry->slapi_filter, 0);
  1125. if (LDAP_SUCCESS == ret) {
  1126. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM, "dna_parse_config_entry: "
  1127. "Error: shared config entry (%s) matches scope \"%s\", and filter \"%s\" "
  1128. "of the DNA config entry (%s).\n", entry->shared_cfg_base,
  1129. entry->scope, entry->filter, entry->dn);
  1130. ret = DNA_FAILURE;
  1131. goto bail;
  1132. }
  1133. }
  1134. }
  1135. /**
  1136. * Finally add the entry to the list.
  1137. * We sort by scope dn length with longer
  1138. * dn's first - this allows the scope
  1139. * checking code to be simple and quick and
  1140. * cunningly linear.
  1141. */
  1142. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1143. list = PR_LIST_HEAD(dna_global_config);
  1144. while (list != dna_global_config) {
  1145. config_entry = (struct configEntry *) list;
  1146. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  1147. PR_INSERT_BEFORE(&(entry->list), list);
  1148. slapi_log_error(SLAPI_LOG_CONFIG,
  1149. DNA_PLUGIN_SUBSYSTEM,
  1150. "store [%s] before [%s] \n", entry->scope,
  1151. config_entry->scope);
  1152. entry_added = 1;
  1153. break;
  1154. }
  1155. list = PR_NEXT_LINK(list);
  1156. if (dna_global_config == list) {
  1157. /* add to tail */
  1158. PR_INSERT_BEFORE(&(entry->list), list);
  1159. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  1160. "store [%s] at tail\n", entry->scope);
  1161. entry_added = 1;
  1162. break;
  1163. }
  1164. }
  1165. } else {
  1166. /* first entry */
  1167. PR_INSERT_LINK(&(entry->list), dna_global_config);
  1168. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  1169. "store [%s] at head \n", entry->scope);
  1170. entry_added = 1;
  1171. }
  1172. bail:
  1173. if (0 == entry_added) {
  1174. /* Don't log error if we weren't asked to apply config */
  1175. if ((apply != 0) && (entry != NULL)) {
  1176. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1177. "dna_parse_config_entry: Invalid config entry "
  1178. "[%s] skipped\n", entry->dn);
  1179. }
  1180. dna_free_config_entry(&entry);
  1181. } else {
  1182. ret = DNA_SUCCESS;
  1183. }
  1184. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1185. "<-- dna_parse_config_entry\n");
  1186. return ret;
  1187. }
  1188. static void
  1189. dna_free_config_entry(struct configEntry ** entry)
  1190. {
  1191. struct configEntry *e;
  1192. if ((entry == NULL) || (*entry == NULL)) {
  1193. return;
  1194. }
  1195. e = *entry;
  1196. if (e->dn) {
  1197. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  1198. "freeing config entry [%s]\n", e->dn);
  1199. slapi_ch_free_string(&e->dn);
  1200. }
  1201. slapi_ch_array_free(e->types);
  1202. slapi_ch_free_string(&e->prefix);
  1203. slapi_ch_free_string(&e->filter);
  1204. slapi_filter_free(e->slapi_filter, 1);
  1205. slapi_ch_free_string(&e->generate);
  1206. slapi_ch_free_string(&e->scope);
  1207. if (e->excludescope) {
  1208. int i;
  1209. for (i = 0; e->excludescope[i]; i++) {
  1210. slapi_sdn_free(&e->excludescope[i]);
  1211. }
  1212. slapi_ch_free((void **)&e->excludescope);
  1213. }
  1214. slapi_ch_free_string(&e->shared_cfg_base);
  1215. slapi_ch_free_string(&e->shared_cfg_dn);
  1216. slapi_ch_free_string(&e->remote_binddn);
  1217. slapi_ch_free_string(&e->remote_bindpw);
  1218. slapi_destroy_mutex(e->lock);
  1219. slapi_ch_free((void **) entry);
  1220. }
  1221. static void
  1222. dna_delete_configEntry(PRCList *entry)
  1223. {
  1224. PR_REMOVE_LINK(entry);
  1225. dna_free_config_entry((struct configEntry **) &entry);
  1226. }
  1227. static void
  1228. dna_delete_config(PRCList *list)
  1229. {
  1230. PRCList *list_entry, *delete_list;
  1231. if(list){
  1232. delete_list = list;
  1233. } else {
  1234. delete_list = dna_global_config;
  1235. }
  1236. while (!PR_CLIST_IS_EMPTY(delete_list)) {
  1237. list_entry = PR_LIST_HEAD(delete_list);
  1238. dna_delete_configEntry(list_entry);
  1239. }
  1240. return;
  1241. }
  1242. static void
  1243. dna_free_shared_server(struct dnaServer **server)
  1244. {
  1245. struct dnaServer *s;
  1246. if ((NULL == server) || (NULL == *server)) {
  1247. return;
  1248. }
  1249. s = *server;
  1250. slapi_sdn_free(&s->sdn);
  1251. slapi_ch_free_string(&s->host);
  1252. slapi_ch_free_string(&s->remote_bind_method);
  1253. slapi_ch_free_string(&s->remote_conn_prot);
  1254. slapi_ch_free((void **)server);
  1255. }
  1256. static void
  1257. dna_delete_shared_servers(PRCList **servers)
  1258. {
  1259. PRCList *server;
  1260. while (!PR_CLIST_IS_EMPTY(*servers)) {
  1261. server = PR_LIST_HEAD(*servers);
  1262. PR_REMOVE_LINK(server);
  1263. dna_free_shared_server((struct dnaServer **)&server);
  1264. }
  1265. slapi_ch_free((void **)servers);
  1266. return;
  1267. }
  1268. static int
  1269. dna_load_host_port()
  1270. {
  1271. int status = DNA_SUCCESS;
  1272. Slapi_Entry *e = NULL;
  1273. Slapi_DN *config_dn = NULL;
  1274. char *attrs[4];
  1275. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1276. "--> dna_load_host_port\n");
  1277. attrs[0] = "nsslapd-localhost";
  1278. attrs[1] = "nsslapd-port";
  1279. attrs[2] = "nsslapd-secureport";
  1280. attrs[3] = NULL;
  1281. config_dn = slapi_sdn_new_ndn_byref("cn=config");
  1282. if (config_dn) {
  1283. slapi_search_internal_get_entry(config_dn, attrs, &e, getPluginID());
  1284. slapi_sdn_free(&config_dn);
  1285. }
  1286. if (e) {
  1287. hostname = slapi_entry_attr_get_charptr(e, "nsslapd-localhost");
  1288. portnum = slapi_entry_attr_get_charptr(e, "nsslapd-port");
  1289. secureportnum = slapi_entry_attr_get_charptr(e, "nsslapd-secureport");
  1290. slapi_entry_free(e);
  1291. }
  1292. if (!hostname || !portnum) {
  1293. status = DNA_FAILURE;
  1294. }
  1295. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1296. "<-- dna_load_host_port\n");
  1297. return status;
  1298. }
  1299. /*
  1300. * dna_update_config_event()
  1301. *
  1302. * Event queue callback that we use to do the initial
  1303. * update of the shared config entries shortly after
  1304. * startup.
  1305. *
  1306. * Since we need to start a backend transaction, a copy/snapshot of the global
  1307. * config is used, and then freed.
  1308. */
  1309. static void
  1310. dna_update_config_event(time_t event_time, void *arg)
  1311. {
  1312. Slapi_PBlock *pb = NULL;
  1313. struct configEntry *config_entry = NULL;
  1314. PRCList *copy = NULL, *list = NULL;
  1315. /* Get the read lock so we can copy the config */
  1316. dna_read_lock();
  1317. /* Loop through all config entries and update the shared config entries. */
  1318. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1319. /*
  1320. * We need to use a copy of the config because we can not hold
  1321. * the read lock and start a backend transaction.
  1322. */
  1323. copy = dna_config_copy();
  1324. dna_unlock();
  1325. /* Create the pblock. We'll reuse it for all shared config updates. */
  1326. if ((pb = slapi_pblock_new()) == NULL)
  1327. goto bail;
  1328. list = PR_LIST_HEAD(copy);
  1329. while (list != copy) {
  1330. config_entry = (struct configEntry *) list;
  1331. /* If a shared config dn is set, update the shared config. */
  1332. if (config_entry->shared_cfg_dn != NULL) {
  1333. int rc = 0;
  1334. Slapi_PBlock *dna_pb = NULL;
  1335. Slapi_DN *sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_dn);
  1336. Slapi_Backend *be = slapi_be_select(sdn);
  1337. slapi_sdn_free(&sdn);
  1338. if (be) {
  1339. dna_pb = slapi_pblock_new();
  1340. slapi_pblock_set(dna_pb, SLAPI_BACKEND, be);
  1341. /* We need to start transaction to avoid the deadlock */
  1342. rc = slapi_back_transaction_begin(dna_pb);
  1343. if (rc) {
  1344. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1345. "dna_update_config_event: failed to start transaction\n");
  1346. }
  1347. }
  1348. /* First delete the existing shared config entry. This
  1349. * will allow the entry to be updated for things like
  1350. * port number changes, etc. */
  1351. slapi_delete_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1352. NULL, NULL, getPluginID(), 0);
  1353. /* We don't care about the results */
  1354. slapi_delete_internal_pb(pb);
  1355. /* Now force the entry to be recreated */
  1356. dna_update_shared_config(config_entry);
  1357. if (dna_pb) {
  1358. if (0 == rc) {
  1359. slapi_back_transaction_commit(dna_pb);
  1360. }
  1361. slapi_pblock_destroy(dna_pb);
  1362. }
  1363. slapi_pblock_init(pb);
  1364. }
  1365. list = PR_NEXT_LINK(list);
  1366. }
  1367. dna_delete_config(copy);
  1368. slapi_ch_free((void **)&copy);
  1369. } else {
  1370. dna_unlock();
  1371. }
  1372. bail:
  1373. slapi_pblock_destroy(pb);
  1374. }
  1375. /****************************************************
  1376. Distributed ranges Helpers
  1377. ****************************************************/
  1378. /*
  1379. * dna_fix_maxval()
  1380. *
  1381. * Attempts to extend the range represented by
  1382. * config_entry. If skip_range_request is set,
  1383. * we will only attempt to activate the next
  1384. * range. No attempt will be made to transfer
  1385. * range from another server.
  1386. *
  1387. * The lock for configEntry should be obtained
  1388. * before calling this function.
  1389. */
  1390. static int dna_fix_maxval(struct configEntry *config_entry,
  1391. int skip_range_request)
  1392. {
  1393. PRCList *servers = NULL;
  1394. PRCList *server = NULL;
  1395. PRUint64 lower = 0;
  1396. PRUint64 upper = 0;
  1397. int ret = LDAP_OPERATIONS_ERROR;
  1398. if (config_entry == NULL) {
  1399. goto bail;
  1400. }
  1401. /* If we already have a next range we only need
  1402. * to activate it. */
  1403. if (config_entry->next_range_lower != 0) {
  1404. ret = dna_activate_next_range(config_entry);
  1405. if (ret != 0) {
  1406. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1407. "dna_fix_maxval: Unable to activate the "
  1408. "next range for range %s.\n", config_entry->dn);
  1409. }
  1410. } else if (!skip_range_request && config_entry->shared_cfg_base) {
  1411. /* Find out if there are any other servers to request
  1412. * range from. */
  1413. dna_get_shared_servers(config_entry, &servers, 0 );
  1414. if (servers) {
  1415. /* We have other servers we can try to extend
  1416. * our range from. Loop through them and
  1417. * request range until someone gives us some
  1418. * values, or we hit the end of the list. */
  1419. server = PR_LIST_HEAD(servers);
  1420. while (server != servers) {
  1421. if (((struct dnaServer *)server)->remaining == 0) {
  1422. /* This server has no values left, no need to ping it */
  1423. server = PR_NEXT_LINK(server);
  1424. } else if (dna_request_range(config_entry, (struct dnaServer *)server,
  1425. &lower, &upper) != 0) {
  1426. server = PR_NEXT_LINK(server);
  1427. } else {
  1428. /* Someone provided us with a new range. Attempt
  1429. * to update the config. */
  1430. if ((ret = dna_update_next_range(config_entry, lower, upper)) == 0) {
  1431. break;
  1432. }
  1433. server = PR_NEXT_LINK(server);
  1434. }
  1435. }
  1436. /* free the list of servers */
  1437. dna_delete_shared_servers(&servers);
  1438. }
  1439. }
  1440. bail:
  1441. return ret;
  1442. }
  1443. /* dna_notice_allocation()
  1444. *
  1445. * Called after a new value has been allocated from the range.
  1446. * This function will update the config entries and cached info
  1447. * appropriately. This includes activating the next range if
  1448. * we've exhausted the current range.
  1449. *
  1450. * The last parameter is the value that has just been allocated.
  1451. * The new parameter should be the next available value. If you
  1452. * set both of these parameters to 0, then this function will
  1453. * just check if the next range needs to be activated and update
  1454. * the config accordingly.
  1455. *
  1456. * The lock for configEntry should be obtained before calling
  1457. * this function. */
  1458. static void
  1459. dna_notice_allocation(struct configEntry *config_entry, PRUint64 new,
  1460. PRUint64 last)
  1461. {
  1462. /* update our cached config entry */
  1463. if ((new != 0) && (new <= (config_entry->maxval + config_entry->interval))) {
  1464. config_entry->nextval = new;
  1465. }
  1466. /* check if we've exhausted our active range */
  1467. if ((last == config_entry->maxval) ||
  1468. (config_entry->nextval > config_entry->maxval)) {
  1469. /* If we already have a next range set, make it the
  1470. * new active range. */
  1471. if (config_entry->next_range_lower != 0) {
  1472. /* Make the next range active */
  1473. if (dna_activate_next_range(config_entry) != 0) {
  1474. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1475. "dna_notice_allocation: Unable to activate "
  1476. "the next range for range %s.\n", config_entry->dn);
  1477. }
  1478. } else {
  1479. config_entry->remaining = 0;
  1480. /* update the shared configuration */
  1481. dna_update_shared_config(config_entry);
  1482. }
  1483. } else {
  1484. if (config_entry->next_range_lower != 0) {
  1485. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1486. config_entry->interval) + ((config_entry->next_range_upper -
  1487. config_entry->next_range_lower +1) / config_entry->interval);
  1488. } else {
  1489. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1490. config_entry->interval);
  1491. }
  1492. /* update the shared configuration */
  1493. dna_update_shared_config(config_entry);
  1494. }
  1495. return;
  1496. }
  1497. static int
  1498. dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers, int get_all)
  1499. {
  1500. int ret = LDAP_SUCCESS;
  1501. Slapi_PBlock *pb = NULL;
  1502. Slapi_Entry **entries = NULL;
  1503. char *attrs[7];
  1504. /* First do a search in the shared config area for this
  1505. * range to find other servers who are managing this range. */
  1506. attrs[0] = DNA_HOSTNAME;
  1507. attrs[1] = DNA_PORTNUM;
  1508. attrs[2] = DNA_SECURE_PORTNUM;
  1509. attrs[3] = DNA_REMAINING;
  1510. attrs[4] = DNA_REMOTE_BIND_METHOD;
  1511. attrs[5] = DNA_REMOTE_CONN_PROT;
  1512. attrs[6] = NULL;
  1513. pb = slapi_pblock_new();
  1514. if (NULL == pb) {
  1515. ret = LDAP_OPERATIONS_ERROR;
  1516. goto cleanup;
  1517. }
  1518. slapi_search_internal_set_pb(pb, config_entry->shared_cfg_base,
  1519. LDAP_SCOPE_ONELEVEL, "objectclass=*",
  1520. attrs, 0, NULL,
  1521. NULL, getPluginID(), 0);
  1522. slapi_search_internal_pb(pb);
  1523. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1524. if (LDAP_SUCCESS != ret) {
  1525. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1526. "dna_get_shared_servers: search failed for shared "
  1527. "config: %s [error %d]\n", config_entry->shared_cfg_base,
  1528. ret);
  1529. goto cleanup;
  1530. }
  1531. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1532. &entries);
  1533. if (entries && entries[0]) {
  1534. Slapi_DN *cfg_sdn = NULL;
  1535. int i;
  1536. cfg_sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_dn);
  1537. /* We found some entries. Go through them and
  1538. * order them based off of remaining values. */
  1539. for (i = 0; entries[i]; i++) {
  1540. /* skip our own shared config entry, unless we want all the servers */
  1541. if (get_all || slapi_sdn_compare(cfg_sdn, slapi_entry_get_sdn(entries[i]))) {
  1542. struct dnaServer *server = NULL;
  1543. /* set up the server list entry */
  1544. server = (struct dnaServer *) slapi_ch_calloc(1,
  1545. sizeof(struct dnaServer));
  1546. server->sdn = slapi_sdn_new_dn_byval(slapi_entry_get_ndn(entries[i]));
  1547. server->host = slapi_entry_attr_get_charptr(entries[i],
  1548. DNA_HOSTNAME);
  1549. server->port = slapi_entry_attr_get_uint(entries[i], DNA_PORTNUM);
  1550. server->secureport = slapi_entry_attr_get_uint(entries[i], DNA_SECURE_PORTNUM);
  1551. server->remaining = slapi_entry_attr_get_ulonglong(entries[i],
  1552. DNA_REMAINING);
  1553. server->remote_binddn = config_entry->remote_binddn;
  1554. server->remote_bindpw = config_entry->remote_bindpw;
  1555. server->remote_bind_method = slapi_entry_attr_get_charptr(entries[i],
  1556. DNA_REMOTE_BIND_METHOD);
  1557. server->remote_conn_prot = slapi_entry_attr_get_charptr(entries[i],
  1558. DNA_REMOTE_CONN_PROT);
  1559. /* validate the entry */
  1560. if (!server->host || (server->port == 0 && server->secureport == 0))
  1561. {
  1562. /* free and skip this one */
  1563. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1564. "dna_get_shared_servers: skipping invalid "
  1565. "shared config entry (%s)\n", slapi_entry_get_dn(entries[i]));
  1566. dna_free_shared_server(&server);
  1567. continue;
  1568. }
  1569. /* see if we defined a server manually */
  1570. if(server->remote_bind_method){
  1571. char *reason = NULL;
  1572. int err = 0;
  1573. if(strcasecmp(server->remote_bind_method, DNA_METHOD_DIGESTMD5) == 0 ||
  1574. strcasecmp(server->remote_bind_method, DNA_METHOD_SIMPLE) == 0){
  1575. /* requires a DN and password */
  1576. if(!server->remote_binddn || !server->remote_bindpw){
  1577. reason = "missing bind DN and/or password.";
  1578. err = 1;
  1579. }
  1580. }
  1581. if(strcasecmp(server->remote_bind_method, DNA_METHOD_SSL) == 0){
  1582. /* requires a bind DN */
  1583. if(strcasecmp(server->remote_conn_prot, DNA_PROT_SSL) != 0 &&
  1584. strcasecmp(server->remote_conn_prot, DNA_PROT_TLS) != 0 )
  1585. {
  1586. reason = "bind method (SSL) requires either SSL or TLS connection "
  1587. "protocol.";
  1588. err = 1;
  1589. }
  1590. }
  1591. if(err){
  1592. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1593. "dna_get_shared_servers: skipping invalid "
  1594. "shared config entry (%s). Reason: %s\n",
  1595. slapi_entry_get_dn(entries[i]), reason);
  1596. dna_free_shared_server(&server);
  1597. continue;
  1598. }
  1599. /* everything is ok */
  1600. server->remote_defined = 1;
  1601. }
  1602. /* add a server entry to the list */
  1603. if (*servers == NULL) {
  1604. /* first entry */
  1605. *servers = (PRCList *) slapi_ch_calloc(1,
  1606. sizeof(struct dnaServer));
  1607. PR_INIT_CLIST(*servers);
  1608. PR_INSERT_LINK(&(server->list), *servers);
  1609. } else {
  1610. /* Find the right slot for this entry. We
  1611. * want to order the entries based off of
  1612. * the remaining number of values, highest
  1613. * to lowest. */
  1614. struct dnaServer *sitem;
  1615. PRCList* item = PR_LIST_HEAD(*servers);
  1616. int inserted = 0;
  1617. while (item != *servers) {
  1618. sitem = (struct dnaServer *)item;
  1619. if (server->remaining > sitem->remaining) {
  1620. PR_INSERT_BEFORE(&(server->list), item);
  1621. inserted = 1;
  1622. break;
  1623. }
  1624. item = PR_NEXT_LINK(item);
  1625. if (*servers == item) {
  1626. /* add to tail */
  1627. PR_INSERT_BEFORE(&(server->list), item);
  1628. inserted = 1;
  1629. break;
  1630. }
  1631. }
  1632. if(!inserted){
  1633. dna_free_shared_server(&server);
  1634. }
  1635. }
  1636. }
  1637. }
  1638. slapi_sdn_free(&cfg_sdn);
  1639. }
  1640. cleanup:
  1641. slapi_free_search_results_internal(pb);
  1642. slapi_pblock_destroy(pb);
  1643. return ret;
  1644. }
  1645. /*
  1646. * dna_request_range()
  1647. *
  1648. * Requests range extension from another server.
  1649. * Returns 0 on success and will fill in upper
  1650. * and lower. Returns non-0 on failure and will
  1651. * zero out upper and lower.
  1652. */
  1653. static int dna_request_range(struct configEntry *config_entry,
  1654. struct dnaServer *server,
  1655. PRUint64 *lower, PRUint64 *upper)
  1656. {
  1657. char *bind_dn = NULL;
  1658. char *bind_passwd = NULL;
  1659. char *bind_method = NULL;
  1660. int is_ssl = 0;
  1661. struct berval *request = NULL;
  1662. char *retoid = NULL;
  1663. struct berval *responsedata = NULL;
  1664. BerElement *respber = NULL;
  1665. LDAP *ld = NULL;
  1666. char *lower_str = NULL;
  1667. char *upper_str = NULL;
  1668. int set_extend_flag = 0;
  1669. int ret = LDAP_OPERATIONS_ERROR;
  1670. int port = 0;
  1671. int timelimit;
  1672. #if defined(USE_OPENLDAP)
  1673. struct timeval timeout;
  1674. #endif
  1675. /* See if we're allowed to send a range request now */
  1676. slapi_lock_mutex(config_entry->extend_lock);
  1677. if (config_entry->extend_in_progress) {
  1678. /* We're already processing a range extention, so bail. */
  1679. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1680. "dna_request_range: Already processing a "
  1681. "range extension request. Skipping request.\n");
  1682. slapi_unlock_mutex(config_entry->extend_lock);
  1683. goto bail;
  1684. } else {
  1685. /* Set a flag indicating that we're attempting to extend this range */
  1686. config_entry->extend_in_progress = 1;
  1687. set_extend_flag = 1;
  1688. slapi_unlock_mutex(config_entry->extend_lock);
  1689. }
  1690. /* Fetch the replication bind dn info */
  1691. if (dna_get_replica_bind_creds(config_entry->shared_cfg_base, server,
  1692. &bind_dn, &bind_passwd, &bind_method,
  1693. &is_ssl, &port) != 0) {
  1694. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1695. "dna_request_range: Unable to retrieve "
  1696. "replica bind credentials.\n");
  1697. goto bail;
  1698. }
  1699. if ((request = dna_create_range_request(config_entry->shared_cfg_base)) == NULL) {
  1700. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1701. "dna_request_range: Failed to create "
  1702. "range extension extended operation request.\n");
  1703. goto bail;
  1704. }
  1705. if ((ld = slapi_ldap_init(server->host, port, is_ssl, 0)) == NULL) {
  1706. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1707. "dna_request_range: Unable to "
  1708. "initialize LDAP session to server %s:%u.\n",
  1709. server->host, server->port);
  1710. goto bail;
  1711. }
  1712. /* Disable referrals and set timelimit and a connect timeout */
  1713. ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1714. timelimit = config_entry->timeout / 1000; /* timeout is in msec */
  1715. ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &timelimit);
  1716. #if defined(USE_OPENLDAP)
  1717. timeout.tv_sec = config_entry->timeout / 1000;
  1718. timeout.tv_usec = (config_entry->timeout % 1000) * 1000;
  1719. ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &timeout);
  1720. #else
  1721. ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &config_entry->timeout);
  1722. #endif
  1723. /* Bind to the replica server */
  1724. ret = slapi_ldap_bind(ld, bind_dn, bind_passwd, bind_method,
  1725. NULL, NULL, NULL, NULL);
  1726. if (ret != LDAP_SUCCESS) {
  1727. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1728. "dna_request_range: Error binding "
  1729. " to replica server %s:%u. [error %d]\n",
  1730. server->host, server->port, ret);
  1731. goto bail;
  1732. }
  1733. /* Send range extension request */
  1734. ret = ldap_extended_operation_s(ld, DNA_EXTEND_EXOP_REQUEST_OID,
  1735. request, NULL, NULL, &retoid, &responsedata);
  1736. if (ret != LDAP_SUCCESS) {
  1737. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1738. "dna_request_range: Error sending "
  1739. "range extension extended operation request "
  1740. "to server %s:%u [error %d]\n", server->host,
  1741. server->port, ret);
  1742. goto bail;
  1743. }
  1744. /* Verify that the OID is correct. */
  1745. if (strcmp(retoid, DNA_EXTEND_EXOP_RESPONSE_OID) != 0) {
  1746. ret = LDAP_OPERATIONS_ERROR;
  1747. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1748. "dna_request_range: Received incorrect response OID.\n");
  1749. goto bail;
  1750. }
  1751. /* Parse response */
  1752. if (BV_HAS_DATA(responsedata)) {
  1753. respber = ber_init(responsedata);
  1754. if (ber_scanf(respber, "{aa}", &lower_str, &upper_str) == LBER_ERROR) {
  1755. ret = LDAP_PROTOCOL_ERROR;
  1756. goto bail;
  1757. }
  1758. }
  1759. /* Fill in upper and lower */
  1760. if (upper_str && lower_str) {
  1761. *upper = strtoull(upper_str, 0, 0);
  1762. *lower = strtoull(lower_str, 0, 0);
  1763. ret = 0;
  1764. } else {
  1765. ret = LDAP_OPERATIONS_ERROR;
  1766. }
  1767. bail:
  1768. if (set_extend_flag) {
  1769. slapi_lock_mutex(config_entry->extend_lock);
  1770. config_entry->extend_in_progress = 0;
  1771. slapi_unlock_mutex(config_entry->extend_lock);
  1772. }
  1773. slapi_ldap_unbind(ld);
  1774. slapi_ch_free_string(&bind_dn);
  1775. slapi_ch_free_string(&bind_passwd);
  1776. slapi_ch_free_string(&bind_method);
  1777. slapi_ch_free_string(&retoid);
  1778. slapi_ch_free_string(&lower_str);
  1779. slapi_ch_free_string(&upper_str);
  1780. ber_free(respber, 1);
  1781. ber_bvfree(request);
  1782. ber_bvfree(responsedata);
  1783. if (ret != 0) {
  1784. *upper = 0;
  1785. *lower = 0;
  1786. }
  1787. return ret;
  1788. }
  1789. static struct berval *dna_create_range_request(char *range_dn)
  1790. {
  1791. struct berval *requestdata = NULL;
  1792. struct berval shared_dn = { 0, NULL };
  1793. BerElement *ber = NULL;
  1794. shared_dn.bv_val = range_dn;
  1795. shared_dn.bv_len = strlen(shared_dn.bv_val);
  1796. if((ber = ber_alloc()) == NULL) {
  1797. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1798. "dna_create_range_request: Error "
  1799. "allocating request data.\n");
  1800. goto bail;
  1801. }
  1802. if (LBER_ERROR == (ber_printf(ber, "{o}", shared_dn.bv_val, shared_dn.bv_len))) {
  1803. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1804. "dna_create_range_request: Error "
  1805. "encoding request data.\n");
  1806. goto bail;
  1807. }
  1808. if (ber_flatten(ber, &requestdata) == -1) {
  1809. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1810. "dna_create_range_request: Error "
  1811. "encoding request data.\n");
  1812. goto bail;
  1813. }
  1814. bail:
  1815. ber_free(ber, 1);
  1816. return requestdata;
  1817. }
  1818. /****************************************************
  1819. Helpers
  1820. ****************************************************/
  1821. static char *dna_get_dn(Slapi_PBlock * pb)
  1822. {
  1823. Slapi_DN *sdn = 0;
  1824. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1825. "--> dna_get_dn\n");
  1826. if (slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn)) {
  1827. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1828. "dna_get_dn: failed to get dn of changed entry");
  1829. goto bail;
  1830. }
  1831. bail:
  1832. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1833. "<-- dna_get_dn\n");
  1834. return (char *)slapi_sdn_get_dn(sdn);
  1835. }
  1836. /* config check
  1837. matching config dn or a descendent reloads config
  1838. */
  1839. static int dna_dn_is_config(char *dn)
  1840. {
  1841. int ret = 0;
  1842. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1843. "--> dna_is_config\n");
  1844. if (slapi_dn_issuffix(dn, getPluginDN())) {
  1845. ret = 1;
  1846. }
  1847. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1848. "<-- dna_is_config\n");
  1849. return ret;
  1850. }
  1851. static int
  1852. dna_dn_is_shared_config(Slapi_PBlock *pb, char *dn)
  1853. {
  1854. struct configEntry *config_entry = NULL;
  1855. Slapi_Entry *entry = NULL;
  1856. Slapi_Attr *attr = NULL;
  1857. PRCList *list = NULL;
  1858. int ret = 0;
  1859. dna_read_lock();
  1860. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1861. list = PR_LIST_HEAD(dna_global_config);
  1862. while (list != dna_global_config) {
  1863. config_entry = (struct configEntry *) list;
  1864. if (slapi_dn_issuffix(dn, config_entry->shared_cfg_base)) {
  1865. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &entry);
  1866. /* If the entry has the dnaHost attribute, it is a shared entry */
  1867. if (slapi_entry_attr_find(entry, DNA_HOSTNAME, &attr) == 0) {
  1868. ret = 1;
  1869. break;
  1870. }
  1871. }
  1872. list = PR_NEXT_LINK(list);
  1873. }
  1874. }
  1875. dna_unlock();
  1876. return ret;
  1877. }
  1878. #define DNA_LDAP_TAG_SK_REVERSE 0x81L
  1879. static LDAPControl *dna_build_sort_control(const char *attr)
  1880. {
  1881. LDAPControl *ctrl;
  1882. BerElement *ber;
  1883. int rc;
  1884. ber = ber_alloc();
  1885. if (NULL == ber)
  1886. return NULL;
  1887. rc = ber_printf(ber, "{{stb}}", attr, DNA_LDAP_TAG_SK_REVERSE, 1);
  1888. if (-1 == rc) {
  1889. ber_free(ber, 1);
  1890. return NULL;
  1891. }
  1892. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  1893. ber_free(ber, 1);
  1894. if (LDAP_SUCCESS != rc)
  1895. return NULL;
  1896. return ctrl;
  1897. }
  1898. /****************************************************
  1899. Functions that actually do things other
  1900. than config and startup
  1901. ****************************************************/
  1902. /*
  1903. * dna_first_free_value()
  1904. *
  1905. * We do search all values between nextval and maxval asking the
  1906. * server to sort them, then we check the first free spot and
  1907. * use it as newval. If we go past the end of the range, we
  1908. * return LDAP_OPERATIONS_ERROR and set newval to be > the
  1909. * maximum configured value for this range. */
  1910. static int
  1911. dna_first_free_value(struct configEntry *config_entry,
  1912. PRUint64 *newval)
  1913. {
  1914. Slapi_Entry **entries = NULL;
  1915. Slapi_PBlock *pb = NULL;
  1916. LDAPControl **ctrls = NULL;
  1917. char *filter = NULL;
  1918. char *prefix = NULL;
  1919. int multitype = 0;
  1920. int result, status;
  1921. PRUint64 tmpval, sval, i;
  1922. char *strval = NULL;
  1923. /* check if the config is already out of range */
  1924. if (config_entry->nextval > config_entry->maxval) {
  1925. *newval = config_entry->nextval;
  1926. return LDAP_OPERATIONS_ERROR;
  1927. }
  1928. prefix = config_entry->prefix;
  1929. tmpval = config_entry->nextval;
  1930. if (dna_is_multitype_range(config_entry)) {
  1931. multitype = 1;
  1932. }
  1933. /* We don't sort if we're using a prefix (non integer type) or if this
  1934. * is a multi-type range. Instead, we just search to see if the next
  1935. * value is free, and keep incrementing until we find the next free value. */
  1936. if (prefix || multitype) {
  1937. /* This will allocate the filter string for us. */
  1938. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1939. } else {
  1940. /* This is a single-type range, so just use the first (only)
  1941. * type from the list. */
  1942. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  1943. if (NULL == ctrls)
  1944. return LDAP_OPERATIONS_ERROR;
  1945. ctrls[0] = dna_build_sort_control(config_entry->types[0]);
  1946. if (NULL == ctrls[0]) {
  1947. slapi_ch_free((void **)&ctrls);
  1948. return LDAP_OPERATIONS_ERROR;
  1949. }
  1950. filter = slapi_ch_smprintf("(&%s(&(%s>=%" NSPRIu64 ")(%s<=%" NSPRIu64 ")))",
  1951. config_entry->filter,
  1952. config_entry->types[0], tmpval,
  1953. config_entry->types[0], config_entry->maxval);
  1954. }
  1955. if (NULL == filter) {
  1956. ldap_controls_free(ctrls);
  1957. ctrls = NULL;
  1958. return LDAP_OPERATIONS_ERROR;
  1959. }
  1960. pb = slapi_pblock_new();
  1961. if (NULL == pb) {
  1962. ldap_controls_free(ctrls);
  1963. ctrls = NULL;
  1964. slapi_ch_free_string(&filter);
  1965. return LDAP_OPERATIONS_ERROR;
  1966. }
  1967. slapi_search_internal_set_pb(pb, config_entry->scope,
  1968. LDAP_SCOPE_SUBTREE, filter,
  1969. config_entry->types, 0, ctrls,
  1970. NULL, getPluginID(), 0);
  1971. slapi_search_internal_pb(pb);
  1972. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1973. if (LDAP_SUCCESS != result) {
  1974. status = LDAP_OPERATIONS_ERROR;
  1975. goto cleanup;
  1976. }
  1977. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1978. &entries);
  1979. if (NULL == entries || NULL == entries[0]) {
  1980. /* no values means we already have a good value */
  1981. *newval = tmpval;
  1982. status = LDAP_SUCCESS;
  1983. goto cleanup;
  1984. }
  1985. if (prefix || multitype) {
  1986. /* The next value identified in the config entry has already
  1987. * been taken. We just iterate through the values until we
  1988. * (hopefully) find a free one. */
  1989. for (tmpval += config_entry->interval; tmpval <= config_entry->maxval;
  1990. tmpval += config_entry->interval) {
  1991. /* This will reuse the old memory for the previous filter. It is
  1992. * guaranteed to have enough space since the filter is the same
  1993. * aside from the assertion value (we allocated enough for the
  1994. * largest supported integer). */
  1995. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1996. /* clear out the pblock so we can re-use it */
  1997. slapi_free_search_results_internal(pb);
  1998. slapi_pblock_init(pb);
  1999. slapi_search_internal_set_pb(pb, config_entry->scope,
  2000. LDAP_SCOPE_SUBTREE, filter,
  2001. config_entry->types, 0, 0,
  2002. NULL, getPluginID(), 0);
  2003. slapi_search_internal_pb(pb);
  2004. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2005. if (LDAP_SUCCESS != result) {
  2006. status = LDAP_OPERATIONS_ERROR;
  2007. goto cleanup;
  2008. }
  2009. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2010. &entries);
  2011. if (NULL == entries || NULL == entries[0]) {
  2012. /* no values means we already have a good value */
  2013. *newval = tmpval;
  2014. status = LDAP_SUCCESS;
  2015. goto cleanup;
  2016. }
  2017. }
  2018. } else {
  2019. /* Entries are sorted and filtered for value >= tval therefore if the
  2020. * first one does not match tval it means that the value is free,
  2021. * otherwise we need to cycle through values until we find a mismatch,
  2022. * the first mismatch is the first free pit. This is guaranteed to
  2023. * be a single-type range, so we can just use the first (only)
  2024. * type from the list of types directly. */
  2025. sval = 0;
  2026. for (i = 0; NULL != entries[i]; i++) {
  2027. strval = slapi_entry_attr_get_charptr(entries[i], config_entry->types[0]);
  2028. errno = 0;
  2029. sval = strtoull(strval, 0, 0);
  2030. if (errno) {
  2031. /* something very wrong here ... */
  2032. status = LDAP_OPERATIONS_ERROR;
  2033. goto cleanup;
  2034. }
  2035. slapi_ch_free_string(&strval);
  2036. if (tmpval != sval)
  2037. break;
  2038. if (config_entry->maxval < sval)
  2039. break;
  2040. tmpval += config_entry->interval;
  2041. }
  2042. }
  2043. /* check if we went past the end of the range */
  2044. if (tmpval <= config_entry->maxval) {
  2045. *newval = tmpval;
  2046. status = LDAP_SUCCESS;
  2047. } else {
  2048. /* we set newval past the end of the range
  2049. * so the caller can easily detect that we
  2050. * overflowed the configured range. */
  2051. *newval = tmpval;
  2052. status = LDAP_OPERATIONS_ERROR;
  2053. }
  2054. cleanup:
  2055. slapi_ch_free_string(&filter);
  2056. slapi_ch_free_string(&strval);
  2057. slapi_free_search_results_internal(pb);
  2058. slapi_pblock_destroy(pb);
  2059. return status;
  2060. }
  2061. /*
  2062. * Perform ldap operationally atomic increment
  2063. * Return the next value to be assigned
  2064. */
  2065. static int dna_get_next_value(struct configEntry *config_entry,
  2066. char **next_value_ret)
  2067. {
  2068. Slapi_PBlock *pb = NULL;
  2069. LDAPMod mod_replace;
  2070. LDAPMod *mods[2];
  2071. char *replace_val[2];
  2072. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2073. char next_value[17];
  2074. PRUint64 setval = 0;
  2075. PRUint64 nextval = 0;
  2076. int ret;
  2077. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2078. "--> dna_get_next_value\n");
  2079. /* get the lock to prevent contention with other threads over
  2080. * the next new value for this range. */
  2081. slapi_lock_mutex(config_entry->lock);
  2082. /* get the first value */
  2083. ret = dna_first_free_value(config_entry, &setval);
  2084. if (LDAP_SUCCESS != ret) {
  2085. /* check if we overflowed the configured range */
  2086. if (setval > config_entry->maxval) {
  2087. /* This should not happen, as pre_op should of allocated the next range.
  2088. * In case this does occur, we will attempt to activate the next range if
  2089. * one is available. We tell dna_fix_maxval() to skip sending a range
  2090. * transfer request, we we don't want to perform any network operations
  2091. * while within a transaction. */
  2092. ret = dna_fix_maxval(config_entry, 1 /* skip range transfer request */ );
  2093. if (LDAP_SUCCESS != ret) {
  2094. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2095. "dna_get_next_value: no more values available!!\n");
  2096. goto done;
  2097. }
  2098. /* get the first value from our newly extended range */
  2099. ret = dna_first_free_value(config_entry, &setval);
  2100. if (LDAP_SUCCESS != ret)
  2101. goto done;
  2102. } else {
  2103. /* dna_first_free_value() failed for some unknown reason */
  2104. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2105. "dna_get_next_value: failed to allocate a new ID!!\n");
  2106. goto done;
  2107. }
  2108. }
  2109. nextval = setval + config_entry->interval;
  2110. /* update nextval if we have not reached the end
  2111. * of our current range */
  2112. if ((config_entry->maxval == -1) ||
  2113. (nextval <= (config_entry->maxval + config_entry->interval))) {
  2114. /* try to set the new next value in the config entry */
  2115. PR_snprintf(next_value, sizeof(next_value),"%" NSPRIu64, nextval);
  2116. /* set up our replace modify operation */
  2117. replace_val[0] = next_value;
  2118. replace_val[1] = 0;
  2119. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2120. mod_replace.mod_type = DNA_NEXTVAL;
  2121. mod_replace.mod_values = replace_val;
  2122. mods[0] = &mod_replace;
  2123. mods[1] = 0;
  2124. pb = slapi_pblock_new();
  2125. if (NULL == pb) {
  2126. ret = LDAP_OPERATIONS_ERROR;
  2127. goto done;
  2128. }
  2129. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2130. mods, 0, 0, getPluginID(), 0);
  2131. slapi_modify_internal_pb(pb);
  2132. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2133. }
  2134. if (LDAP_SUCCESS == ret) {
  2135. slapi_ch_free_string(next_value_ret);
  2136. *next_value_ret = slapi_ch_smprintf("%" NSPRIu64, setval);
  2137. if (NULL == *next_value_ret) {
  2138. ret = LDAP_OPERATIONS_ERROR;
  2139. goto done;
  2140. }
  2141. /* update our cached config */
  2142. dna_notice_allocation(config_entry, nextval, setval);
  2143. }
  2144. done:
  2145. slapi_unlock_mutex(config_entry->lock);
  2146. if (pb) {
  2147. slapi_pblock_destroy(pb);
  2148. }
  2149. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2150. "<-- dna_get_next_value\n");
  2151. return ret;
  2152. }
  2153. /*
  2154. * Get a value from the global server list. The dna_server_read_lock()
  2155. * should be held prior to calling this function.
  2156. */
  2157. static int
  2158. dna_get_shared_config_attr_val(struct configEntry *config_entry, char *attr, char *value)
  2159. {
  2160. struct dnaServer *server = NULL;
  2161. Slapi_DN *server_sdn = NULL;
  2162. int found = 0;
  2163. server_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_dn);
  2164. if (dna_global_servers) {
  2165. server = dna_global_servers;
  2166. while (server) {
  2167. if(slapi_sdn_compare(server->sdn, server_sdn) == 0){
  2168. if(strcmp(attr, DNA_REMOTE_BIND_METHOD) == 0){
  2169. if (server->remote_bind_method) {
  2170. PR_snprintf(value, DNA_REMOTE_BUFSIZ, "%s", server->remote_bind_method);
  2171. found = 1;
  2172. }
  2173. break;
  2174. } else if(strcmp(attr, DNA_REMOTE_CONN_PROT) == 0){
  2175. if (server->remote_conn_prot) {
  2176. PR_snprintf(value, DNA_REMOTE_BUFSIZ, "%s", server->remote_conn_prot);
  2177. found = 1;
  2178. }
  2179. break;
  2180. }
  2181. }
  2182. server = server->next;
  2183. }
  2184. }
  2185. slapi_sdn_free(&server_sdn);
  2186. return found;
  2187. }
  2188. /*
  2189. * dna_update_shared_config()
  2190. *
  2191. * Updates the shared config entry if one is
  2192. * configured. Returns the LDAP result code.
  2193. *
  2194. * The lock for configEntry should be obtained
  2195. * before calling this function.
  2196. * */
  2197. static int
  2198. dna_update_shared_config(struct configEntry *config_entry)
  2199. {
  2200. int ret = LDAP_SUCCESS;
  2201. if (config_entry && config_entry->shared_cfg_dn) {
  2202. /* Update the shared config entry if one is configured */
  2203. Slapi_PBlock *pb = NULL;
  2204. LDAPMod mod_replace;
  2205. LDAPMod *mods[2];
  2206. char *replace_val[2];
  2207. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2208. char remaining_vals[17];
  2209. /* We store the number of remaining assigned values
  2210. * in the shared config entry. */
  2211. PR_snprintf(remaining_vals, sizeof(remaining_vals),"%" NSPRIu64,
  2212. config_entry->remaining);
  2213. /* set up our replace modify operation */
  2214. replace_val[0] = remaining_vals;
  2215. replace_val[1] = 0;
  2216. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2217. mod_replace.mod_type = DNA_REMAINING;
  2218. mod_replace.mod_values = replace_val;
  2219. mods[0] = &mod_replace;
  2220. mods[1] = 0;
  2221. pb = slapi_pblock_new();
  2222. if (NULL == pb) {
  2223. ret = LDAP_OPERATIONS_ERROR;
  2224. } else {
  2225. slapi_modify_internal_set_pb(pb, config_entry->shared_cfg_dn,
  2226. mods, NULL, NULL, getPluginID(), 0);
  2227. slapi_modify_internal_pb(pb);
  2228. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2229. /* If the shared config for this instance doesn't
  2230. * already exist, we add it. */
  2231. if (ret == LDAP_NO_SUCH_OBJECT) {
  2232. Slapi_Entry *e = NULL;
  2233. Slapi_DN *sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_dn);
  2234. char bind_meth[DNA_REMOTE_BUFSIZ];
  2235. char conn_prot[DNA_REMOTE_BUFSIZ];
  2236. /* Set up the new shared config entry */
  2237. e = slapi_entry_alloc();
  2238. /* the entry now owns the dup'd dn */
  2239. slapi_entry_init_ext(e, sdn, NULL); /* sdn is copied into e */
  2240. slapi_sdn_free(&sdn);
  2241. slapi_entry_add_string(e, SLAPI_ATTR_OBJECTCLASS, DNA_SHAREDCONFIG);
  2242. slapi_entry_add_string(e, DNA_HOSTNAME, hostname);
  2243. slapi_entry_add_string(e, DNA_PORTNUM, portnum);
  2244. if (secureportnum) {
  2245. slapi_entry_add_string(e, DNA_SECURE_PORTNUM, secureportnum);
  2246. }
  2247. slapi_entry_add_string(e, DNA_REMAINING, remaining_vals);
  2248. /* Grab the remote server settings */
  2249. dna_server_read_lock();
  2250. if(dna_get_shared_config_attr_val(config_entry, DNA_REMOTE_BIND_METHOD, bind_meth)) {
  2251. slapi_entry_add_string(e, DNA_REMOTE_BIND_METHOD, bind_meth);
  2252. }
  2253. if(dna_get_shared_config_attr_val(config_entry, DNA_REMOTE_CONN_PROT, conn_prot)){
  2254. slapi_entry_add_string(e, DNA_REMOTE_CONN_PROT,conn_prot);
  2255. }
  2256. dna_server_unlock();
  2257. /* clear pb for re-use */
  2258. slapi_pblock_init(pb);
  2259. /* e will be consumed by slapi_add_internal() */
  2260. slapi_add_entry_internal_set_pb(pb, e, NULL, getPluginID(), 0);
  2261. slapi_add_internal_pb(pb);
  2262. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2263. }
  2264. if (ret != LDAP_SUCCESS) {
  2265. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2266. "dna_update_shared_config: Unable to update shared config entry: %s [error %d]\n",
  2267. config_entry->shared_cfg_dn, ret);
  2268. }
  2269. slapi_pblock_destroy(pb);
  2270. pb = NULL;
  2271. }
  2272. }
  2273. return ret;
  2274. }
  2275. /*
  2276. * dna_update_next_range()
  2277. *
  2278. * Sets the proper value for the next range in
  2279. * all configuration entries and in-memory cache.
  2280. *
  2281. * The range you are updating should be locked
  2282. * before calling this function.
  2283. */
  2284. static int
  2285. dna_update_next_range(struct configEntry *config_entry,
  2286. PRUint64 lower, PRUint64 upper)
  2287. {
  2288. Slapi_PBlock *pb = NULL;
  2289. LDAPMod mod_replace;
  2290. LDAPMod *mods[2];
  2291. char *replace_val[2];
  2292. /* 32 for the two numbers, 1 for the '-', and one for the '\0' */
  2293. char nextrange_value[34];
  2294. int ret = 0;
  2295. /* Try to set the new next range in the config entry. */
  2296. PR_snprintf(nextrange_value, sizeof(nextrange_value), "%" NSPRIu64 "-%" NSPRIu64,
  2297. lower, upper);
  2298. /* set up our replace modify operation */
  2299. replace_val[0] = nextrange_value;
  2300. replace_val[1] = 0;
  2301. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2302. mod_replace.mod_type = DNA_NEXT_RANGE;
  2303. mod_replace.mod_values = replace_val;
  2304. mods[0] = &mod_replace;
  2305. mods[1] = 0;
  2306. pb = slapi_pblock_new();
  2307. if (NULL == pb) {
  2308. ret = LDAP_OPERATIONS_ERROR;
  2309. goto bail;
  2310. }
  2311. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2312. mods, 0, 0, getPluginID(), 0);
  2313. slapi_modify_internal_pb(pb);
  2314. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2315. slapi_pblock_destroy(pb);
  2316. pb = NULL;
  2317. if (ret != LDAP_SUCCESS) {
  2318. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2319. "dna_update_next_range: Error updating "
  2320. "configuration entry [err=%d]\n", ret);
  2321. } else {
  2322. /* update the cached config and the shared config */
  2323. config_entry->next_range_lower = lower;
  2324. config_entry->next_range_upper = upper;
  2325. dna_notice_allocation(config_entry, 0, 0);
  2326. }
  2327. bail:
  2328. return ret;
  2329. }
  2330. /* dna_activate_next_range()
  2331. *
  2332. * Makes the next range the active range. This
  2333. * will update the config entry, the in-memory
  2334. * config info, and the shared config entry.
  2335. *
  2336. * The lock for configEntry should
  2337. * be obtained before calling this function.
  2338. */
  2339. static int
  2340. dna_activate_next_range(struct configEntry *config_entry)
  2341. {
  2342. Slapi_PBlock *pb = NULL;
  2343. LDAPMod mod_maxval;
  2344. LDAPMod mod_nextval;
  2345. LDAPMod mod_nextrange;
  2346. LDAPMod *mods[4];
  2347. char *maxval_vals[2];
  2348. char *nextval_vals[2];
  2349. char *nextrange_vals[1];
  2350. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2351. char maxval_val[17];
  2352. char nextval_val[17];
  2353. int ret = 0;
  2354. /* Setup the modify operation for the config entry */
  2355. PR_snprintf(maxval_val, sizeof(maxval_val),"%" NSPRIu64, config_entry->next_range_upper);
  2356. PR_snprintf(nextval_val, sizeof(nextval_val),"%" NSPRIu64, config_entry->next_range_lower);
  2357. maxval_vals[0] = maxval_val;
  2358. maxval_vals[1] = 0;
  2359. nextval_vals[0] = nextval_val;
  2360. nextval_vals[1] = 0;
  2361. nextrange_vals[0] = 0;
  2362. mod_maxval.mod_op = LDAP_MOD_REPLACE;
  2363. mod_maxval.mod_type = DNA_MAXVAL;
  2364. mod_maxval.mod_values = maxval_vals;
  2365. mod_nextval.mod_op = LDAP_MOD_REPLACE;
  2366. mod_nextval.mod_type = DNA_NEXTVAL;
  2367. mod_nextval.mod_values = nextval_vals;
  2368. mod_nextrange.mod_op = LDAP_MOD_DELETE;
  2369. mod_nextrange.mod_type = DNA_NEXT_RANGE;
  2370. mod_nextrange.mod_values = nextrange_vals;
  2371. mods[0] = &mod_maxval;
  2372. mods[1] = &mod_nextval;
  2373. mods[2] = &mod_nextrange;
  2374. mods[3] = 0;
  2375. /* Update the config entry first */
  2376. pb = slapi_pblock_new();
  2377. if (NULL == pb) {
  2378. ret = LDAP_OPERATIONS_ERROR;
  2379. goto bail;
  2380. }
  2381. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2382. mods, 0, 0, getPluginID(), 0);
  2383. slapi_modify_internal_pb(pb);
  2384. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2385. slapi_pblock_destroy(pb);
  2386. pb = NULL;
  2387. if (ret != LDAP_SUCCESS) {
  2388. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2389. "dna_activate_next_range: Error updating "
  2390. "configuration entry [err=%d]\n", ret);
  2391. } else {
  2392. /* Update the in-memory config info */
  2393. config_entry->maxval = config_entry->next_range_upper;
  2394. config_entry->nextval = config_entry->next_range_lower;
  2395. config_entry->next_range_upper = 0;
  2396. config_entry->next_range_lower = 0;
  2397. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  2398. config_entry->interval);
  2399. /* update the shared configuration */
  2400. dna_update_shared_config(config_entry);
  2401. }
  2402. bail:
  2403. return ret;
  2404. }
  2405. /*
  2406. * dna_is_replica_bind_dn()
  2407. *
  2408. * Checks if the passed in DN is the replica bind DN. This
  2409. * is used to check if a user is allowed to request range
  2410. * from us.
  2411. *
  2412. * Returns 1 if bind_dn matches the replica bind dn, 0 otherwise. */
  2413. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn)
  2414. {
  2415. char *replica_dn = NULL;
  2416. Slapi_DN *replica_sdn = NULL;
  2417. Slapi_DN *range_sdn = NULL;
  2418. Slapi_Entry *e = NULL;
  2419. char *attrs[3];
  2420. Slapi_Backend *be = NULL;
  2421. const char *be_suffix = NULL;
  2422. int ret = 0;
  2423. /* Find the backend suffix where the shared config is stored. */
  2424. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2425. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2426. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2427. }
  2428. /* Fetch the "cn=replica" entry for the backend that stores
  2429. * the shared config. We need to see what the configured
  2430. * replica bind DN is or if a bind DN group is defined. */
  2431. if (be_suffix) {
  2432. /* This function converts the old DN style to the new one. */
  2433. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2434. if (NULL == replica_dn) {
  2435. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2436. "dna_is_replica_bind_dn: failed to create "
  2437. "replica dn for %s\n", be_suffix);
  2438. ret = 1;
  2439. goto done;
  2440. }
  2441. replica_sdn = slapi_sdn_new_normdn_passin(replica_dn);
  2442. attrs[0] = DNA_REPL_BIND_DN;
  2443. attrs[1] = DNA_REPL_BIND_DNGROUP;
  2444. attrs[2] = 0;
  2445. /* Find cn=replica entry via search */
  2446. slapi_search_internal_get_entry(replica_sdn, attrs, &e, getPluginID());
  2447. if (e) {
  2448. /* Check if the passed in bind dn matches any of the replica bind dns. */
  2449. Slapi_Value *bind_dn_sv = slapi_value_new_string(bind_dn);
  2450. ret = slapi_entry_attr_has_syntax_value(e, DNA_REPL_BIND_DN, bind_dn_sv);
  2451. if (ret == 0) {
  2452. /* check if binddn is member of binddn group */
  2453. int i = 0;
  2454. Slapi_DN *bind_group_sdn = NULL;
  2455. Slapi_Entry *bind_group_entry = NULL;
  2456. char **bind_group_dn = slapi_entry_attr_get_charray(e, DNA_REPL_BIND_DNGROUP);
  2457. attrs[0] = "member";
  2458. attrs[1] = "uniquemember";
  2459. attrs[2] = 0;
  2460. for (i=0; bind_group_dn != NULL && bind_group_dn[i] != NULL; i++) {
  2461. if (ret) {
  2462. /* already found a member, just free group */
  2463. slapi_ch_free_string(&bind_group_dn[i]);
  2464. continue;
  2465. }
  2466. bind_group_sdn = slapi_sdn_new_normdn_passin(bind_group_dn[i]);
  2467. slapi_search_internal_get_entry(bind_group_sdn, attrs, &bind_group_entry, getPluginID());
  2468. if (bind_group_entry) {
  2469. ret = slapi_entry_attr_has_syntax_value(bind_group_entry, "member", bind_dn_sv);
  2470. if (ret == 0) {
  2471. ret = slapi_entry_attr_has_syntax_value(bind_group_entry, "uniquemember", bind_dn_sv);
  2472. }
  2473. }
  2474. slapi_entry_free(bind_group_entry);
  2475. slapi_sdn_free(&bind_group_sdn);
  2476. }
  2477. slapi_ch_free((void **) &bind_group_dn);
  2478. }
  2479. slapi_value_free(&bind_dn_sv);
  2480. } else {
  2481. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2482. "dna_is_replica_bind_dn: Failed to fetch replica entry "
  2483. "for range %s\n", range_dn);
  2484. }
  2485. }
  2486. done:
  2487. slapi_entry_free(e);
  2488. slapi_sdn_free(&range_sdn);
  2489. slapi_sdn_free(&replica_sdn);
  2490. return ret;
  2491. }
  2492. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  2493. char **bind_dn, char **bind_passwd,
  2494. char **bind_method, int *is_ssl, int *port)
  2495. {
  2496. Slapi_PBlock *pb = NULL;
  2497. Slapi_DN *range_sdn = NULL;
  2498. char *replica_dn = NULL;
  2499. Slapi_Backend *be = NULL;
  2500. const char *be_suffix = NULL;
  2501. char *attrs[6];
  2502. char *filter = NULL;
  2503. char *bind_cred = NULL;
  2504. char *transport = NULL;
  2505. Slapi_Entry **entries = NULL;
  2506. int ret = LDAP_OPERATIONS_ERROR;
  2507. /* Find the backend suffix where the shared config is stored. */
  2508. range_sdn = slapi_sdn_new_normdn_byref(range_dn);
  2509. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2510. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2511. }
  2512. /* Fetch the replication agreement entry */
  2513. if (be_suffix) {
  2514. /* This function converts the old DN style to the new one. */
  2515. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2516. if (NULL == replica_dn) {
  2517. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2518. "dna_get_replica_bind_creds: failed to create "
  2519. "replica dn for %s\n", be_suffix);
  2520. ret = LDAP_PARAM_ERROR;
  2521. goto bail;
  2522. }
  2523. filter = slapi_ch_smprintf("(&(nsds5ReplicaHost=%s)(|(" DNA_REPL_PORT "=%u)"
  2524. "(" DNA_REPL_PORT "=%u)))",
  2525. server->host, server->port, server->secureport);
  2526. attrs[0] = DNA_REPL_BIND_DN;
  2527. attrs[1] = DNA_REPL_CREDS;
  2528. attrs[2] = DNA_REPL_BIND_METHOD;
  2529. attrs[3] = DNA_REPL_TRANSPORT;
  2530. attrs[4] = DNA_REPL_PORT;
  2531. attrs[5] = 0;
  2532. pb = slapi_pblock_new();
  2533. if (NULL == pb) {
  2534. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2535. "dna_get_replica_bind_creds: Failed to "
  2536. "allocate pblock\n");
  2537. goto bail;
  2538. }
  2539. slapi_search_internal_set_pb(pb, replica_dn,
  2540. LDAP_SCOPE_ONELEVEL, filter,
  2541. attrs, 0, NULL, NULL, getPluginID(), 0);
  2542. slapi_search_internal_pb(pb);
  2543. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2544. if (LDAP_SUCCESS != ret) {
  2545. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2546. "dna_get_replica_bind_creds: Failed to fetch replica "
  2547. "bind credentials for range %s, server %s, port %u [error %d]\n",
  2548. range_dn, server->host,
  2549. server->port ? server->port : server->secureport, ret);
  2550. goto bail;
  2551. }
  2552. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2553. &entries);
  2554. if (NULL == entries || NULL == entries[0]) {
  2555. if(server->remote_defined){
  2556. /*
  2557. * Ok there are no replication agreements for this shared server, but we
  2558. * do have custom defined authentication settings we can use.
  2559. */
  2560. ret = dna_get_remote_config_info(server, bind_dn, bind_passwd, bind_method, is_ssl, port);
  2561. goto bail;
  2562. }
  2563. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2564. "dna_get_replica_bind_creds: Failed to fetch replication "
  2565. "agreement for range %s, server %s, port %u\n", range_dn,
  2566. server->host, server->port ? server->port : server->secureport);
  2567. ret = LDAP_OPERATIONS_ERROR;
  2568. goto bail;
  2569. }
  2570. /* Get the replication bind dn and password from the agreement. It
  2571. * is up to the caller to free these when they are finished. */
  2572. slapi_ch_free_string(bind_dn);
  2573. slapi_ch_free_string(bind_method);
  2574. *bind_dn = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_DN);
  2575. *bind_method = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_METHOD);
  2576. bind_cred = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_CREDS);
  2577. transport = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_TRANSPORT);
  2578. *port = slapi_entry_attr_get_int(entries[0], DNA_REPL_PORT);
  2579. /* Check if we should use SSL */
  2580. if (transport && (strcasecmp(transport, "SSL") == 0)) {
  2581. *is_ssl = 1;
  2582. } else if (transport && (strcasecmp(transport, "TLS") == 0)) {
  2583. *is_ssl = 2;
  2584. } else {
  2585. *is_ssl = 0;
  2586. }
  2587. /* fix up the bind method */
  2588. if ((NULL == *bind_method) || (strcasecmp(*bind_method, "SIMPLE") == 0)) {
  2589. slapi_ch_free_string(bind_method);
  2590. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2591. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2592. slapi_ch_free_string(bind_method);
  2593. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2594. } else if (strcasecmp(*bind_method, "SASL/GSSAPI") == 0) {
  2595. slapi_ch_free_string(bind_method);
  2596. *bind_method = slapi_ch_strdup("GSSAPI");
  2597. } else if (strcasecmp(*bind_method, "SASL/DIGEST-MD5") == 0) {
  2598. slapi_ch_free_string(bind_method);
  2599. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2600. } else { /* some other weird value */
  2601. ; /* just use it directly */
  2602. }
  2603. /* Decode the password */
  2604. if (bind_cred) {
  2605. int pw_ret = 0;
  2606. slapi_ch_free_string(bind_passwd);
  2607. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2608. if (pw_ret == -1) {
  2609. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2610. "dna_get_replica_bind_creds: Failed to decode "
  2611. "replica bind credentials for range %s, server %s, "
  2612. "port %u\n", range_dn, server->host, server->port);
  2613. goto bail;
  2614. } else if (pw_ret != 0) {
  2615. /* The password was already in clear text, so pw_rever_decode
  2616. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2617. * to prevent a double free. The memory is now owned by
  2618. * bind_passwd, which is the callers responsibility to free. */
  2619. bind_cred = NULL;
  2620. }
  2621. }
  2622. }
  2623. /* If we got here, we succesfully got the
  2624. * creds. Set the success return value. */
  2625. ret = 0;
  2626. bail:
  2627. slapi_ch_free_string(&transport);
  2628. slapi_ch_free_string(&filter);
  2629. slapi_sdn_free(&range_sdn);
  2630. slapi_ch_free_string(&replica_dn);
  2631. slapi_ch_free_string(&bind_cred);
  2632. slapi_free_search_results_internal(pb);
  2633. slapi_pblock_destroy(pb);
  2634. return ret;
  2635. }
  2636. static int
  2637. dna_get_remote_config_info( struct dnaServer *server, char **bind_dn, char **bind_passwd,
  2638. char **bind_method, int *is_ssl, int *port)
  2639. {
  2640. int rc = 0;
  2641. /* populate the bind info */
  2642. slapi_ch_free_string(bind_dn);
  2643. slapi_ch_free_string(bind_method);
  2644. *bind_dn = slapi_ch_strdup(server->remote_binddn);
  2645. *bind_method = slapi_ch_strdup(server->remote_bind_method);
  2646. /* fix up the bind method */
  2647. if ((NULL == *bind_method) || (strcasecmp(*bind_method, DNA_METHOD_SIMPLE) == 0)) {
  2648. slapi_ch_free_string(bind_method);
  2649. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2650. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2651. slapi_ch_free_string(bind_method);
  2652. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2653. } else if (strcasecmp(*bind_method, DNA_METHOD_GSSAPI) == 0) {
  2654. slapi_ch_free_string(bind_method);
  2655. *bind_method = slapi_ch_strdup("GSSAPI");
  2656. } else if (strcasecmp(*bind_method, DNA_METHOD_DIGESTMD5) == 0) {
  2657. slapi_ch_free_string(bind_method);
  2658. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2659. } else { /* some other weird value */
  2660. ; /* just use it directly */
  2661. }
  2662. if(server->remote_conn_prot && strcasecmp(server->remote_conn_prot, DNA_PROT_SSL) == 0){
  2663. *is_ssl = 1;
  2664. } else if(server->remote_conn_prot && strcasecmp(server->remote_conn_prot, DNA_PROT_TLS) == 0){
  2665. *is_ssl = 2;
  2666. } else {
  2667. *is_ssl = 0;
  2668. }
  2669. if(*is_ssl == 1){ /* SSL(covers TLS over ssl) */
  2670. if (server->secureport){
  2671. *port = server->secureport;
  2672. } else {
  2673. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2674. "dna_get_remote_config_info: Using SSL protocol, but the secure "
  2675. "port is not defined.\n");
  2676. return -1;
  2677. }
  2678. } else { /* LDAP/TLS(non secure port) */
  2679. if(server->port){
  2680. *port = server->port;
  2681. } else {
  2682. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2683. "dna_get_remote_config_info: Using %s protocol, but the non-secure "
  2684. "port is not defined.\n", server->remote_conn_prot);
  2685. return -1;
  2686. }
  2687. }
  2688. /* Decode the password */
  2689. if (server->remote_bindpw) {
  2690. char *bind_cred = slapi_ch_strdup(server->remote_bindpw);
  2691. int pw_ret = 0;
  2692. slapi_ch_free_string(bind_passwd);
  2693. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2694. if (pw_ret == -1) {
  2695. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2696. "dna_get_remote_config_info: Failed to decode "
  2697. "replica bind credentials for server %s, "
  2698. "port %u\n", server->host,
  2699. server->port ? server->port : server->secureport);
  2700. rc = -1;
  2701. } else if (pw_ret != 0) {
  2702. /*
  2703. * The password was already in clear text, so pw_rever_decode
  2704. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2705. * to prevent a double free. The memory is now owned by
  2706. * bind_passwd, which is the callers responsibility to free.
  2707. */
  2708. bind_cred = NULL;
  2709. }
  2710. slapi_ch_free_string(&bind_cred);
  2711. }
  2712. return rc;
  2713. }
  2714. /*
  2715. * dna_list_contains_type()
  2716. *
  2717. * Checks if a type is contained in a list of types.
  2718. * Returns 1 if the type is found, 0 otherwise.
  2719. */
  2720. static int
  2721. dna_list_contains_type(char **list, char *type)
  2722. {
  2723. int ret = 0;
  2724. int i = 0;
  2725. if (list && type) {
  2726. for (i = 0; list[i]; i++) {
  2727. if (slapi_attr_types_equivalent(type, list[i])) {
  2728. ret = 1;
  2729. break;
  2730. }
  2731. }
  2732. }
  2733. return ret;
  2734. }
  2735. /*
  2736. * dna_list_contains_types()
  2737. *
  2738. * Checks if all types in one list (types) are contained
  2739. * in another list of types (list). Returns 1 if all
  2740. * types are found, 0 otherwise.
  2741. */
  2742. static int
  2743. dna_list_contains_types(char **list, char **types)
  2744. {
  2745. int ret = 1;
  2746. int i = 0;
  2747. int j = 0;
  2748. if (list && types) {
  2749. for (i = 0; types[i]; i++) {
  2750. int found = 0;
  2751. for (j = 0; list[j]; j++) {
  2752. if (slapi_attr_types_equivalent(types[i], list[i])) {
  2753. found = 1;
  2754. break;
  2755. }
  2756. }
  2757. if (!found) {
  2758. ret = 0;
  2759. break;
  2760. }
  2761. }
  2762. } else {
  2763. ret = 0;
  2764. }
  2765. return ret;
  2766. }
  2767. /*
  2768. * dna_list_remove_type()
  2769. *
  2770. * Removes a type from a list of types.
  2771. */
  2772. static void
  2773. dna_list_remove_type(char **list, char *type)
  2774. {
  2775. int i = 0;
  2776. int found_type = 0;
  2777. if (list && type) {
  2778. /* Go through the list until we find the type that
  2779. * we want to remove. We simply free the type we
  2780. * want to remove and shift the remaining array
  2781. * elements down by one index. This will leave us
  2782. * with two NULL elements at the end of the list,
  2783. * but this should not cause any problems. */
  2784. for (i = 0; list[i]; i++) {
  2785. if (found_type) {
  2786. list[i] = list[i + 1];
  2787. } else if (slapi_attr_types_equivalent(type, list[i])) {
  2788. slapi_ch_free_string(&list[i]);
  2789. list[i] = list[i + 1];
  2790. found_type = 1;
  2791. }
  2792. }
  2793. }
  2794. }
  2795. /*
  2796. * dna_is_multitype_range()
  2797. *
  2798. * Returns 1 if the range has multiple types configured.
  2799. * Returns 0 otherwise.
  2800. */
  2801. static int dna_is_multitype_range(struct configEntry *config_entry)
  2802. {
  2803. int ret = 0;
  2804. if (config_entry && config_entry->types && config_entry->types[1]) {
  2805. ret = 1;
  2806. }
  2807. return ret;
  2808. }
  2809. /*
  2810. * dna_create_valcheck_filter()
  2811. *
  2812. * Creates a filter string used to check if a value is free. If
  2813. * filter already holds a valid pointer, it is assumed to have enough
  2814. * space to hold the filter. This allows the same memory to be used
  2815. * over and over when you only want to change the assertion value.
  2816. * If filter contains a NULL pointer, a new string of the appropriate
  2817. * size will be allocated. The caller must free this when finished.
  2818. */
  2819. static void
  2820. dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter)
  2821. {
  2822. int filterlen = 0;
  2823. int typeslen = 0;
  2824. int i = 0;
  2825. int bytes_out = 0;
  2826. int multitype = 0;
  2827. /* Just return if we didn't get an address for the filter. */
  2828. if (filter == NULL) {
  2829. return;
  2830. }
  2831. /* To determine the filter length, we add together the following:
  2832. *
  2833. * - the string length of the filter in the config
  2834. * - the string length sum of all configured types
  2835. * - 23 bytes for each type (20 for the max string
  2836. * representation of a NSPRIu64, 3 for "(=)"
  2837. * - 3 bytes for the beginning and end of the filter - "(&" and ")"
  2838. * - 3 bytes to OR together multiple types (if present) - "(|" and ")"
  2839. * - the string length of the prefix (if one is configured) for each type
  2840. * - 1 byte for the trailing \0
  2841. *
  2842. * The filter length should be the same every time if the config struct
  2843. * has not been changed. We need to calculate the filter length even
  2844. * if we are reusing memory since we have no other way of knowing the
  2845. * length used when it was originally allocated. We trust the caller
  2846. * to only reuse the pointer with the same config struct.
  2847. */
  2848. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2849. typeslen += strlen(config_entry->types[i]);
  2850. }
  2851. if (i > 1) {
  2852. multitype = 1;
  2853. }
  2854. filterlen = strlen(config_entry->filter) + typeslen +
  2855. (i * 23) + 3 + 1 +
  2856. (config_entry->prefix ? (i * strlen(config_entry->prefix)) : 0) +
  2857. (multitype ? 3 : 0);
  2858. /* Allocate space for the filter if it hasn't been allocated yet. */
  2859. if (*filter == NULL) {
  2860. *filter = slapi_ch_malloc(filterlen);
  2861. }
  2862. /* Write out the beginning of the filter. If multiple types
  2863. * are configured, we need to OR together the search clauses
  2864. * for the types. */
  2865. if (multitype) {
  2866. bytes_out = snprintf(*filter, filterlen, "(&%s(|", config_entry->filter);
  2867. } else {
  2868. bytes_out = snprintf(*filter, filterlen, "(&%s", config_entry->filter);
  2869. }
  2870. /* Loop through the types and append each filter clause. */
  2871. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2872. bytes_out += snprintf(*filter + bytes_out, filterlen - bytes_out,
  2873. "(%s=%s%" PRIu64 ")", config_entry->types[i],
  2874. config_entry->prefix ? config_entry->prefix : "",
  2875. value);
  2876. }
  2877. /* Append the end of the filter. We need an extra paren
  2878. * to close out the OR if we have multiple types. */
  2879. if (multitype) {
  2880. strncat(*filter, "))", filterlen - bytes_out);
  2881. } else {
  2882. strncat(*filter, ")", filterlen - bytes_out);
  2883. }
  2884. }
  2885. /* This function is called at BEPREOP timing to add uid/gidNumber
  2886. * if modtype is missing */
  2887. static int
  2888. _dna_pre_op_add(Slapi_PBlock *pb, Slapi_Entry *e, char **errstr)
  2889. {
  2890. int ret = DNA_SUCCESS;
  2891. PRCList *list = NULL;
  2892. struct configEntry *config_entry = NULL;
  2893. char *dn = NULL;
  2894. char *value = NULL;
  2895. char **types_to_generate = NULL;
  2896. char **generated_types = NULL;
  2897. PRUint64 setval = 0;
  2898. int i;
  2899. if (0 == (dn = dna_get_dn(pb))) {
  2900. goto bail;
  2901. }
  2902. /*
  2903. * Find the config that matches this entry, Set the types that need to be
  2904. * generated to DNA_NEEDS_UPDATE. The be_txn_preop will set the values if
  2905. * the operation hasn't been rejected by that point.
  2906. *
  2907. * We also check if we need to get the next range of values, and grab them.
  2908. * We do this here so we don't have to do it in the be_txn_preop.
  2909. */
  2910. dna_read_lock();
  2911. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2912. list = PR_LIST_HEAD(dna_global_config);
  2913. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  2914. config_entry = (struct configEntry *) list;
  2915. /* Did we already service all of these configured types? */
  2916. if (dna_list_contains_types(generated_types, config_entry->types)) {
  2917. goto next;
  2918. }
  2919. /* is the entry in scope? */
  2920. if (config_entry->scope &&
  2921. !slapi_dn_issuffix(dn, config_entry->scope)) {
  2922. goto next;
  2923. }
  2924. /* is this entry in an excluded scope? */
  2925. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  2926. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  2927. goto next;
  2928. }
  2929. }
  2930. /* does the entry match the filter? */
  2931. if (config_entry->slapi_filter) {
  2932. ret = slapi_vattr_filter_test(pb, e, config_entry->slapi_filter, 0);
  2933. if (LDAP_SUCCESS != ret) {
  2934. goto next;
  2935. }
  2936. }
  2937. if (dna_is_multitype_range(config_entry)) {
  2938. /* For a multi-type range, we only generate a value
  2939. * for types where the magic value is set. We do not
  2940. * generate a value for missing types. */
  2941. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2942. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  2943. if (value){
  2944. if(config_entry->generate == NULL || !slapi_UTF8CASECMP(config_entry->generate, value)){
  2945. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[i]));
  2946. }
  2947. slapi_ch_free_string(&value);
  2948. }
  2949. }
  2950. } else {
  2951. /* For a single type range, we generate the value if
  2952. * the magic value is set or if the type is missing. */
  2953. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  2954. if((config_entry->generate == NULL) || (0 == value) ||
  2955. (value && !slapi_UTF8CASECMP(config_entry->generate, value))){
  2956. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[0]));
  2957. }
  2958. slapi_ch_free_string(&value);
  2959. }
  2960. if (types_to_generate && types_to_generate[0]) {
  2961. /* add - add to entry */
  2962. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2963. slapi_entry_attr_set_charptr(e, types_to_generate[i],
  2964. /* no need to dup */
  2965. DNA_NEEDS_UPDATE);
  2966. }
  2967. /* Update the internalModifiersname for this add op */
  2968. add_internal_modifiersname(pb, e);
  2969. /* Make sure we don't generate for this
  2970. * type again by keeping a list of types
  2971. * we have generated for already.
  2972. */
  2973. if (generated_types == NULL) {
  2974. /* If we don't have a list of generated types yet,
  2975. * we can just use the types_to_generate list so
  2976. * we don't have to allocate anything. */
  2977. generated_types = types_to_generate;
  2978. types_to_generate = NULL;
  2979. } else {
  2980. /* Just reuse the elements out of types_to_generate for the
  2981. * generated types list to avoid allocating them again. */
  2982. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  2983. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  2984. types_to_generate[i] = NULL;
  2985. }
  2986. }
  2987. /* free up */
  2988. slapi_ch_array_free(types_to_generate);
  2989. types_to_generate = NULL;
  2990. /*
  2991. * Now grab the next value and see if we need to get the next range
  2992. */
  2993. slapi_lock_mutex(config_entry->lock);
  2994. ret = dna_first_free_value(config_entry, &setval);
  2995. if (LDAP_SUCCESS != ret) {
  2996. /* check if we overflowed the configured range */
  2997. if (setval > config_entry->maxval) {
  2998. /* try for a new range or fail */
  2999. ret = dna_fix_maxval(config_entry, 0);
  3000. if (LDAP_SUCCESS != ret) {
  3001. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3002. "dna_pre_op: no more values available!!\n");
  3003. /* Set an error string to be returned to the client. */
  3004. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3005. " %s failed! Unable to proceed.",
  3006. config_entry->dn);
  3007. slapi_unlock_mutex(config_entry->lock);
  3008. break;
  3009. }
  3010. /* Make sure dna_first_free_value() doesn't error out */
  3011. ret = dna_first_free_value(config_entry, &setval);
  3012. if (LDAP_SUCCESS != ret){
  3013. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3014. "dna_pre_op: failed to allocate a new ID 1\n");
  3015. /* Set an error string to be returned to the client. */
  3016. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3017. " %s failed! Unable to proceed.",
  3018. config_entry->dn);
  3019. slapi_unlock_mutex(config_entry->lock);
  3020. break;
  3021. }
  3022. } else {
  3023. /* dna_first_free_value() failed for some unknown reason */
  3024. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3025. "dna_pre_op: failed to allocate a new ID!! 2\n");
  3026. /* Set an error string to be returned to the client. */
  3027. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3028. " %s failed! Unable to proceed.",
  3029. config_entry->dn);
  3030. slapi_unlock_mutex(config_entry->lock);
  3031. break;
  3032. }
  3033. }
  3034. /* Check if we passed the threshold and try to fix maxval if so.
  3035. * We don't need to do this if we already have a next range on
  3036. * deck. We don't check the result of dna_fix_maxval() since
  3037. * we aren't completely out of values yet. Any failure here is
  3038. * really a soft failure. */
  3039. if ((config_entry->next_range_lower == 0) &&
  3040. (config_entry->remaining <= config_entry->threshold)) {
  3041. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3042. "dna_pre_op: Passed threshold of %"
  3043. NSPRIu64 " remaining values "
  3044. "for range %s. (%" NSPRIu64 " values remain)\n",
  3045. config_entry->threshold, config_entry->dn,
  3046. config_entry->remaining);
  3047. dna_fix_maxval(config_entry, 0);
  3048. }
  3049. slapi_unlock_mutex(config_entry->lock);
  3050. } else if (types_to_generate) {
  3051. slapi_ch_free((void **)&types_to_generate);
  3052. }
  3053. next:
  3054. ret = DNA_SUCCESS;
  3055. list = PR_NEXT_LINK(list);
  3056. }
  3057. }
  3058. dna_unlock();
  3059. slapi_ch_array_free(generated_types);
  3060. bail:
  3061. return ret;
  3062. }
  3063. /* This function is called at BEPREOP timing to add uid/gidNumber
  3064. * if modtype is missing */
  3065. static int
  3066. _dna_pre_op_modify(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Mods *smods, char **errstr)
  3067. {
  3068. int ret = DNA_SUCCESS;
  3069. PRCList *list = NULL;
  3070. struct configEntry *config_entry = NULL;
  3071. char *dn = NULL;
  3072. char *value = NULL;
  3073. Slapi_Mod *next_mod = NULL;
  3074. Slapi_Mod *smod = NULL;
  3075. Slapi_Attr *attr = NULL;
  3076. char *type = NULL;
  3077. int e_numvals = 0;
  3078. int numvals = 0;
  3079. struct berval *bv = NULL;
  3080. char **types_to_generate = NULL;
  3081. char **generated_types = NULL;
  3082. PRUint64 setval = 0;
  3083. int len = 0;
  3084. int i;
  3085. if (0 == (dn = dna_get_dn(pb))) {
  3086. goto bail;
  3087. }
  3088. /*
  3089. * Find the config that matches this entry, Set the types that need to be
  3090. * generated to DNA_NEEDS_UPDATE. The be_txn_preop will set the values if
  3091. * the operation hasn't been rejected by that point.
  3092. *
  3093. * We also check if we need to get the next range of values, and grab them.
  3094. * We do this here so we don't have to do it in the be_txn_preop.
  3095. */
  3096. dna_read_lock();
  3097. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3098. list = PR_LIST_HEAD(dna_global_config);
  3099. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  3100. config_entry = (struct configEntry *) list;
  3101. /* Did we already service all of these configured types? */
  3102. if (dna_list_contains_types(generated_types, config_entry->types)) {
  3103. goto next;
  3104. }
  3105. /* is the entry in scope? */
  3106. if (config_entry->scope &&
  3107. !slapi_dn_issuffix(dn, config_entry->scope)) {
  3108. goto next;
  3109. }
  3110. /* is this entry in an excluded scope? */
  3111. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  3112. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  3113. goto next;
  3114. }
  3115. }
  3116. /* does the entry match the filter? */
  3117. if (config_entry->slapi_filter) {
  3118. ret = slapi_vattr_filter_test(pb, e,
  3119. config_entry->slapi_filter, 0);
  3120. if (LDAP_SUCCESS != ret) {
  3121. goto next;
  3122. }
  3123. }
  3124. /* check mods for magic value */
  3125. next_mod = slapi_mod_new();
  3126. smod = slapi_mods_get_first_smod(smods, next_mod);
  3127. while (smod) {
  3128. type = (char *)slapi_mod_get_type(smod);
  3129. /* See if the type matches any configured type. */
  3130. if (dna_list_contains_type(config_entry->types, type)) {
  3131. /* If all values are being deleted, we need to
  3132. * generate a new value. We don't do this for
  3133. * multi-type ranges since they require the magic
  3134. * value to be specified to trigger generation. */
  3135. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  3136. !dna_is_multitype_range(config_entry)) {
  3137. numvals = slapi_mod_get_num_values(smod);
  3138. if (numvals == 0) {
  3139. slapi_ch_array_add(&types_to_generate,
  3140. slapi_ch_strdup(type));
  3141. } else {
  3142. e_numvals = 0;
  3143. slapi_entry_attr_find(e, type, &attr);
  3144. if (attr) {
  3145. slapi_attr_get_numvalues(attr, &e_numvals);
  3146. if (numvals >= e_numvals) {
  3147. slapi_ch_array_add(&types_to_generate,
  3148. slapi_ch_strdup(type));
  3149. }
  3150. }
  3151. }
  3152. } else {
  3153. /* This is either adding or replacing a value */
  3154. bv = slapi_mod_get_first_value(smod);
  3155. /* If this type is already in the to be generated
  3156. * list, a previous mod in this same modify operation
  3157. * either removed all values or set the magic value.
  3158. * It's possible that this mod is adding a valid value,
  3159. * which means we would not want to generate a new value.
  3160. * It is safe to remove this type from the to be
  3161. * generated list since it will be re-added here if
  3162. * necessary. */
  3163. if (dna_list_contains_type(types_to_generate, type)) {
  3164. dna_list_remove_type(types_to_generate, type);
  3165. }
  3166. /* If we have a value, see if it's the magic value. */
  3167. if (bv) {
  3168. if(config_entry->generate == NULL){
  3169. /* we don't have a magic number set, so apply the update */
  3170. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3171. } else {
  3172. len = strlen(config_entry->generate);
  3173. if (len == bv->bv_len) {
  3174. if (!slapi_UTF8NCASECMP(bv->bv_val, config_entry->generate,len)){
  3175. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3176. }
  3177. }
  3178. }
  3179. } else if (!dna_is_multitype_range(config_entry)) {
  3180. /* This is a replace with no new values,
  3181. * so we need to generate a new value if this
  3182. * is not a multi-type range. */
  3183. slapi_ch_array_add(&types_to_generate,slapi_ch_strdup(type));
  3184. }
  3185. }
  3186. }
  3187. slapi_mod_done(next_mod);
  3188. smod = slapi_mods_get_next_smod(smods, next_mod);
  3189. }
  3190. slapi_mod_free(&next_mod);
  3191. /* We need to perform one last check for modify operations. If an
  3192. * entry within the scope has not triggered generation yet, we need
  3193. * to see if a value exists for the managed type in the resulting
  3194. * entry. This will catch a modify operation that brings an entry
  3195. * into scope for a managed range, but doesn't supply a value for
  3196. * the managed type. We don't do this for multi-type ranges. */
  3197. if ((!types_to_generate ||
  3198. (types_to_generate && !types_to_generate[0])) &&
  3199. !dna_is_multitype_range(config_entry)) {
  3200. if (slapi_entry_attr_find(e, config_entry->types[0], &attr)
  3201. != 0) {
  3202. slapi_ch_array_add(&types_to_generate,
  3203. slapi_ch_strdup(config_entry->types[0]));
  3204. }
  3205. }
  3206. if (types_to_generate && types_to_generate[0]) {
  3207. /* mod - add to mods */
  3208. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3209. slapi_mods_add_string(smods, LDAP_MOD_REPLACE,
  3210. types_to_generate[i],
  3211. /* no need to dup */
  3212. DNA_NEEDS_UPDATE);
  3213. }
  3214. /* Update the internalModifersname for this mod op */
  3215. modify_update_last_modified_attr(pb, smods);
  3216. /* Make sure we don't generate for this
  3217. * type again by keeping a list of types
  3218. * we have generated for already.
  3219. */
  3220. if (generated_types == NULL) {
  3221. /* If we don't have a list of generated types yet,
  3222. * we can just use the types_to_generate list so
  3223. * we don't have to allocate anything. */
  3224. generated_types = types_to_generate;
  3225. types_to_generate = NULL;
  3226. } else {
  3227. /* Just reuse the elements out of types_to_generate for the
  3228. * generated types list to avoid allocating them again. */
  3229. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  3230. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  3231. types_to_generate[i] = NULL;
  3232. }
  3233. }
  3234. /* free up */
  3235. slapi_ch_free_string(&value);
  3236. slapi_ch_array_free(types_to_generate);
  3237. types_to_generate = NULL;
  3238. /*
  3239. * Now grab the next value and see if we need to get the next range
  3240. */
  3241. slapi_lock_mutex(config_entry->lock);
  3242. ret = dna_first_free_value(config_entry, &setval);
  3243. if (LDAP_SUCCESS != ret) {
  3244. /* check if we overflowed the configured range */
  3245. if (setval > config_entry->maxval) {
  3246. /* try for a new range or fail */
  3247. ret = dna_fix_maxval(config_entry, 0);
  3248. if (LDAP_SUCCESS != ret) {
  3249. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3250. "dna_pre_op: no more values available!!\n");
  3251. /* Set an error string to be returned to the client. */
  3252. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3253. " %s failed! Unable to proceed.",
  3254. config_entry->dn);
  3255. slapi_unlock_mutex(config_entry->lock);
  3256. break;
  3257. }
  3258. /* Make sure dna_first_free_value() doesn't error out */
  3259. ret = dna_first_free_value(config_entry, &setval);
  3260. if (LDAP_SUCCESS != ret){
  3261. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3262. "dna_pre_op: failed to allocate a new ID\n");
  3263. /* Set an error string to be returned to the client. */
  3264. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3265. " %s failed! Unable to proceed.",
  3266. config_entry->dn);
  3267. slapi_unlock_mutex(config_entry->lock);
  3268. break;
  3269. }
  3270. } else {
  3271. /* dna_first_free_value() failed for some unknown reason */
  3272. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3273. "dna_pre_op: failed to allocate a new ID!!\n");
  3274. /* Set an error string to be returned to the client. */
  3275. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3276. " %s failed! Unable to proceed.",
  3277. config_entry->dn);
  3278. slapi_unlock_mutex(config_entry->lock);
  3279. break;
  3280. }
  3281. }
  3282. /* Check if we passed the threshold and try to fix maxval if so.
  3283. * We don't need to do this if we already have a next range on
  3284. * deck. We don't check the result of dna_fix_maxval() since
  3285. * we aren't completely out of values yet. Any failure here is
  3286. * really a soft failure. */
  3287. if ((config_entry->next_range_lower == 0) &&
  3288. (config_entry->remaining <= config_entry->threshold)) {
  3289. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3290. "dna_pre_op: Passed threshold of %"
  3291. NSPRIu64 " remaining values "
  3292. "for range %s. (%" NSPRIu64 " values remain)\n",
  3293. config_entry->threshold, config_entry->dn,
  3294. config_entry->remaining);
  3295. dna_fix_maxval(config_entry, 0);
  3296. }
  3297. slapi_unlock_mutex(config_entry->lock);
  3298. } else if (types_to_generate) {
  3299. slapi_ch_free((void **)&types_to_generate);
  3300. }
  3301. next:
  3302. ret = 0;
  3303. list = PR_NEXT_LINK(list);
  3304. }
  3305. }
  3306. dna_unlock();
  3307. slapi_ch_array_free(generated_types);
  3308. bail:
  3309. return ret;
  3310. }
  3311. /* for mods and adds:
  3312. where dn's are supplied, the closest in scope
  3313. is used as long as the type filter matches
  3314. and the type has not been generated yet.
  3315. */
  3316. static int
  3317. dna_pre_op(Slapi_PBlock * pb, int modtype)
  3318. {
  3319. struct slapi_entry *e = NULL;
  3320. Slapi_Entry *test_e = NULL;
  3321. Slapi_Entry *resulting_e = NULL;
  3322. char *errstr = NULL;
  3323. char *dn = NULL;
  3324. Slapi_Mods *smods = NULL;
  3325. LDAPMod **mods;
  3326. int ret = 0;
  3327. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3328. "--> dna_pre_op\n");
  3329. if (0 == (dn = dna_get_dn(pb))) {
  3330. goto bail;
  3331. }
  3332. if (dna_isrepl(pb)) {
  3333. /* if repl, the dna values should be already in the entry. */
  3334. goto bail;
  3335. }
  3336. if (LDAP_CHANGETYPE_ADD == modtype) {
  3337. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  3338. if (NULL == e) {
  3339. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3340. "dna_pre_op: no add entry set for add\n");
  3341. goto bail;
  3342. }
  3343. } else {
  3344. slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  3345. if (NULL == e) {
  3346. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3347. "dna_pre_op: no pre op entry set for modify\n");
  3348. goto bail;
  3349. }
  3350. /* grab the mods - we'll put them back later with our modifications appended */
  3351. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  3352. /* We need the resulting entry after the mods are applied to
  3353. * see if the entry is within the scope. */
  3354. resulting_e = slapi_entry_dup(e);
  3355. if (mods &&
  3356. (slapi_entry_apply_mods(resulting_e, mods) != LDAP_SUCCESS)) {
  3357. /* The mods don't apply cleanly, so we just let this op go
  3358. * to let the main server handle it. */
  3359. goto bail;
  3360. }
  3361. smods = slapi_mods_new();
  3362. slapi_mods_init_passin(smods, mods);
  3363. }
  3364. /* For a MOD, we need to check the resulting entry */
  3365. if (LDAP_CHANGETYPE_ADD == modtype) {
  3366. test_e = e;
  3367. } else {
  3368. test_e = resulting_e;
  3369. }
  3370. if (dna_dn_is_config(dn)) {
  3371. /* Validate config changes, but don't apply them.
  3372. * This allows us to reject invalid config changes
  3373. * here at the pre-op stage. Applying the config
  3374. * needs to be done at the post-op stage. */
  3375. if (dna_parse_config_entry(pb, test_e, 0) != DNA_SUCCESS) {
  3376. /* Refuse the operation if config parsing failed. */
  3377. ret = LDAP_UNWILLING_TO_PERFORM;
  3378. if (LDAP_CHANGETYPE_ADD == modtype) {
  3379. errstr = slapi_ch_smprintf("Not a valid DNA configuration entry.");
  3380. } else {
  3381. errstr = slapi_ch_smprintf("Changes result in an invalid "
  3382. "DNA configuration.");
  3383. }
  3384. }
  3385. } else {
  3386. if (LDAP_CHANGETYPE_ADD == modtype) {
  3387. ret = _dna_pre_op_add(pb, test_e, &errstr);
  3388. } else {
  3389. if((ret = _dna_pre_op_modify(pb, test_e, smods, &errstr))){
  3390. slapi_mods_free(&smods);
  3391. }
  3392. }
  3393. if (ret) {
  3394. goto bail;
  3395. }
  3396. }
  3397. /* We're done. */
  3398. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3399. /* Put the updated mods back into place. */
  3400. mods = slapi_mods_get_ldapmods_passout(smods);
  3401. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  3402. slapi_mods_free(&smods);
  3403. }
  3404. bail:
  3405. if (resulting_e)
  3406. slapi_entry_free(resulting_e);
  3407. slapi_mods_free(&smods);
  3408. if (ret) {
  3409. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3410. "dna_pre_op: operation failure [%d]\n", ret);
  3411. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  3412. slapi_ch_free((void **)&errstr);
  3413. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  3414. ret = DNA_FAILURE;
  3415. }
  3416. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3417. "<-- dna_pre_op\n");
  3418. return ret;
  3419. }
  3420. static int dna_add_pre_op(Slapi_PBlock * pb)
  3421. {
  3422. return dna_pre_op(pb, LDAP_CHANGETYPE_ADD);
  3423. }
  3424. static int dna_mod_pre_op(Slapi_PBlock * pb)
  3425. {
  3426. return dna_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  3427. }
  3428. static int dna_be_txn_add_pre_op(Slapi_PBlock *pb)
  3429. {
  3430. return dna_be_txn_pre_op(pb, LDAP_CHANGETYPE_ADD);
  3431. }
  3432. static int dna_be_txn_mod_pre_op(Slapi_PBlock *pb)
  3433. {
  3434. return dna_be_txn_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  3435. }
  3436. /*
  3437. * dna_be_txn_pre_op()
  3438. *
  3439. * In the preop if we found that we need to update a DNA attribute,
  3440. * We set the value to "-2" or DNA_NEEDS_UPDATE, because we don't want
  3441. * to do the value allocation in the preop as the operation could fail -
  3442. * resulting in lost values from the range. So we need to to ensure
  3443. * that the value will not be lost by performing the value allocation
  3444. * in the backend txn preop.
  3445. *
  3446. * Although the modifications have already been applied in backend,
  3447. * we still need to add the modification of the real value to the
  3448. * existing Slapi_Mods, so that the value gets indexed correctly.
  3449. *
  3450. * Also, since the modifications have already been applied to the entry
  3451. * in the backend, we need to manually update the entry with the new value.
  3452. */
  3453. static int dna_be_txn_pre_op(Slapi_PBlock *pb, int modtype)
  3454. {
  3455. struct configEntry *config_entry = NULL;
  3456. struct slapi_entry *e = NULL;
  3457. Slapi_Mods *smods = NULL;
  3458. Slapi_Mod *smod = NULL;
  3459. Slapi_Mod *next_mod = NULL;
  3460. Slapi_Attr *attr = NULL;
  3461. LDAPMod **mods = NULL;
  3462. struct berval *bv = NULL;
  3463. PRCList *list = NULL;
  3464. char *value = NULL;
  3465. char **types_to_generate = NULL;
  3466. char **generated_types = NULL;
  3467. char *new_value = NULL;
  3468. char *errstr = NULL;
  3469. char *dn = NULL;
  3470. char *type = NULL;
  3471. int numvals, e_numvals = 0;
  3472. int i, len, ret = DNA_SUCCESS;
  3473. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3474. "--> dna_be_txn_pre_op\n");
  3475. if (!slapi_plugin_running(pb)) {
  3476. goto bail;
  3477. }
  3478. if (0 == (dn = dna_get_dn(pb))) {
  3479. goto bail;
  3480. }
  3481. if (dna_dn_is_config(dn)) {
  3482. goto bail;
  3483. }
  3484. if (dna_isrepl(pb)) {
  3485. /* if repl, the dna values should be already in the entry. */
  3486. goto bail;
  3487. }
  3488. if (LDAP_CHANGETYPE_ADD == modtype) {
  3489. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  3490. } else {
  3491. slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  3492. }
  3493. if (e == NULL) {
  3494. goto bail;
  3495. } else if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3496. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  3497. smods = slapi_mods_new();
  3498. slapi_mods_init_passin(smods, mods);
  3499. }
  3500. dna_read_lock();
  3501. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3502. list = PR_LIST_HEAD(dna_global_config);
  3503. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  3504. config_entry = (struct configEntry *) list;
  3505. /* Did we already service all of these configured types? */
  3506. if (dna_list_contains_types(generated_types, config_entry->types)) {
  3507. goto next;
  3508. }
  3509. /* is the entry in scope? */
  3510. if (config_entry->scope) {
  3511. if (!slapi_dn_issuffix(dn, config_entry->scope))
  3512. goto next;
  3513. }
  3514. /* is this entry in an excluded scope? */
  3515. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  3516. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  3517. goto next;
  3518. }
  3519. }
  3520. /* does the entry match the filter? */
  3521. if (config_entry->slapi_filter) {
  3522. if(LDAP_SUCCESS != slapi_vattr_filter_test(pb,e,config_entry->slapi_filter, 0))
  3523. goto next;
  3524. }
  3525. if (LDAP_CHANGETYPE_ADD == modtype) {
  3526. if (dna_is_multitype_range(config_entry)) {
  3527. /* For a multi-type range, we only generate a value
  3528. * for types where the magic value is set. We do not
  3529. * generate a value for missing types. */
  3530. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  3531. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  3532. if (value && !slapi_UTF8CASECMP(value, DNA_NEEDS_UPDATE)) {
  3533. slapi_ch_array_add(&types_to_generate,
  3534. slapi_ch_strdup(config_entry->types[i]));
  3535. /* Need to remove DNA_NEEDS_UPDATE */
  3536. slapi_entry_attr_delete(e, config_entry->types[i]);
  3537. }
  3538. slapi_ch_free_string(&value);
  3539. }
  3540. } else {
  3541. /* For a single type range, we generate the value if
  3542. * the magic value is set or if the type is missing. */
  3543. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  3544. if (0 == value || (value && !slapi_UTF8CASECMP(value, DNA_NEEDS_UPDATE)) ) {
  3545. slapi_ch_array_add(&types_to_generate,
  3546. slapi_ch_strdup(config_entry->types[0]));
  3547. /* Need to remove DNA_NEEDS_UPDATE */
  3548. slapi_entry_attr_delete(e, config_entry->types[0]);
  3549. }
  3550. slapi_ch_free_string(&value);
  3551. }
  3552. } else {
  3553. /* check mods for DNA_NEEDS_UPDATE*/
  3554. next_mod = slapi_mod_new();
  3555. smod = slapi_mods_get_first_smod(smods, next_mod);
  3556. while (smod) {
  3557. type = (char *)slapi_mod_get_type(smod);
  3558. /* See if the type matches any configured type. */
  3559. if (dna_list_contains_type(config_entry->types, type)) {
  3560. /* If all values are being deleted, we need to
  3561. * generate a new value. */
  3562. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  3563. !dna_is_multitype_range(config_entry)) {
  3564. numvals = slapi_mod_get_num_values(smod);
  3565. if (numvals == 0) {
  3566. slapi_ch_array_add(&types_to_generate,slapi_ch_strdup(type));
  3567. } else {
  3568. slapi_entry_attr_find(e, type, &attr);
  3569. if (attr) {
  3570. slapi_attr_get_numvalues(attr, &e_numvals);
  3571. if (numvals >= e_numvals) {
  3572. slapi_ch_array_add(&types_to_generate,
  3573. slapi_ch_strdup(type));
  3574. }
  3575. }
  3576. }
  3577. } else {
  3578. /* This is either adding or replacing a value */
  3579. bv = slapi_mod_get_first_value(smod);
  3580. if (dna_list_contains_type(types_to_generate, type)) {
  3581. dna_list_remove_type(types_to_generate, type);
  3582. }
  3583. /* If we have a value, see if it's the magic value. */
  3584. if (bv) {
  3585. if (!slapi_UTF8CASECMP(bv->bv_val,
  3586. DNA_NEEDS_UPDATE)) {
  3587. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3588. }
  3589. } else if (!dna_is_multitype_range(config_entry)) {
  3590. /* This is a replace with no new values, so we need
  3591. * to generate a new value if this is not a multi-type range. */
  3592. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3593. }
  3594. }
  3595. }
  3596. slapi_mod_done(next_mod);
  3597. smod = slapi_mods_get_next_smod(smods, next_mod);
  3598. }
  3599. slapi_mod_free(&next_mod);
  3600. }
  3601. /* We need to perform one last check for modify operations. If an
  3602. * entry within the scope has not triggered generation yet, we need
  3603. * to see if a value exists for the managed type in the resulting
  3604. * entry. This will catch a modify operation that brings an entry
  3605. * into scope for a managed range, but doesn't supply a value for
  3606. * the managed type. We don't do this for multi-type ranges. */
  3607. if ((LDAP_CHANGETYPE_MODIFY == modtype) && (!types_to_generate ||
  3608. (types_to_generate && !types_to_generate[0])) && !dna_is_multitype_range(config_entry)) {
  3609. if (slapi_entry_attr_find(e, config_entry->types[0], &attr) != 0) {
  3610. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[0]));
  3611. }
  3612. }
  3613. if (types_to_generate && types_to_generate[0]) {
  3614. /* create the value to add */
  3615. ret = dna_get_next_value(config_entry, &value);
  3616. if (DNA_SUCCESS != ret) {
  3617. errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3618. " %s failed! Unable to proceed.",
  3619. config_entry->dn);
  3620. slapi_ch_array_free(types_to_generate);
  3621. break;
  3622. }
  3623. len = strlen(value) + 1;
  3624. if (config_entry->prefix) {
  3625. len += strlen(config_entry->prefix);
  3626. }
  3627. new_value = slapi_ch_malloc(len);
  3628. if (config_entry->prefix) {
  3629. strcpy(new_value, config_entry->prefix);
  3630. strcat(new_value, value);
  3631. } else
  3632. strcpy(new_value, value);
  3633. /* do the mod */
  3634. if (LDAP_CHANGETYPE_ADD == modtype) {
  3635. /* add - add to entry */
  3636. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3637. slapi_entry_attr_set_charptr(e, types_to_generate[i], new_value);
  3638. }
  3639. } else {
  3640. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3641. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, types_to_generate[i], new_value);
  3642. /* we need to directly update the entry in be_txn_preop */
  3643. slapi_entry_attr_set_charptr(e, types_to_generate[i], new_value);
  3644. }
  3645. }
  3646. /*
  3647. * Make sure we don't generate for this
  3648. * type again by keeping a list of types
  3649. * we have generated for already.
  3650. */
  3651. if (LDAP_SUCCESS == ret) {
  3652. if (generated_types == NULL) {
  3653. /* If we don't have a list of generated types yet,
  3654. * we can just use the types_to_generate list so
  3655. * we don't have to allocate anything. */
  3656. generated_types = types_to_generate;
  3657. types_to_generate = NULL;
  3658. } else {
  3659. /* Just reuse the elements out of types_to_generate for the
  3660. * generated types list to avoid allocating them again. */
  3661. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  3662. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  3663. types_to_generate[i] = NULL;
  3664. }
  3665. }
  3666. }
  3667. /* free up */
  3668. slapi_ch_free_string(&value);
  3669. slapi_ch_free_string(&new_value);
  3670. slapi_ch_array_free(types_to_generate);
  3671. types_to_generate = NULL;
  3672. } else if (types_to_generate) {
  3673. slapi_ch_free((void **)&types_to_generate);
  3674. }
  3675. next:
  3676. list = PR_NEXT_LINK(list);
  3677. }
  3678. }
  3679. dna_unlock();
  3680. bail:
  3681. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3682. /* Put the updated mods back into place. */
  3683. if (smods) { /* smods == NULL if we bailed before initializing it */
  3684. mods = slapi_mods_get_ldapmods_passout(smods);
  3685. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  3686. slapi_mods_free(&smods);
  3687. }
  3688. }
  3689. slapi_ch_array_free(generated_types);
  3690. if (ret) {
  3691. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3692. "dna_be_txn_pre_op: operation failure [%d]\n", ret);
  3693. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  3694. slapi_ch_free((void **)&errstr);
  3695. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  3696. ret = DNA_FAILURE;
  3697. }
  3698. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3699. "<-- dna_be_txn_pre_op\n");
  3700. return ret;
  3701. }
  3702. static int dna_config_check_post_op(Slapi_PBlock * pb)
  3703. {
  3704. char *dn;
  3705. if(!slapi_plugin_running(pb)){
  3706. return DNA_SUCCESS;
  3707. }
  3708. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3709. "--> dna_config_check_post_op\n");
  3710. if (!slapi_op_internal(pb)) { /* If internal, no need to check. */
  3711. if ((dn = dna_get_dn(pb))) {
  3712. if (dna_dn_is_config(dn)) {
  3713. dna_load_plugin_config(pb, 0);
  3714. }
  3715. if(dna_dn_is_shared_config(pb, dn)){
  3716. dna_load_shared_servers();
  3717. }
  3718. }
  3719. }
  3720. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3721. "<-- dna_config_check_post_op\n");
  3722. return DNA_SUCCESS;
  3723. }
  3724. /****************************************************
  3725. * Range Extension Extended Operation
  3726. ***************************************************/
  3727. static int dna_extend_exop(Slapi_PBlock *pb)
  3728. {
  3729. int ret = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  3730. struct berval *reqdata = NULL;
  3731. BerElement *tmp_bere = NULL;
  3732. char *shared_dn = NULL;
  3733. char *bind_dn = NULL;
  3734. char *oid = NULL;
  3735. PRUint64 lower = 0;
  3736. PRUint64 upper = 0;
  3737. if(!slapi_plugin_running(pb)){
  3738. return ret;
  3739. }
  3740. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3741. "--> dna_extend_exop\n");
  3742. /* Fetch the request OID */
  3743. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &oid);
  3744. if (!oid) {
  3745. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3746. "dna_extend_exop: Unable to retrieve request OID.\n");
  3747. goto free_and_return;
  3748. }
  3749. /* Make sure the request OID is correct. */
  3750. if (strcmp(oid, DNA_EXTEND_EXOP_REQUEST_OID) != 0) {
  3751. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3752. "dna_extend_exop: Received incorrect request OID.\n");
  3753. goto free_and_return;
  3754. }
  3755. /* Fetch the request data */
  3756. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &reqdata);
  3757. if (!BV_HAS_DATA(reqdata)) {
  3758. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3759. "dna_extend_exop: No request data received.\n");
  3760. goto free_and_return;
  3761. }
  3762. /* decode the exop */
  3763. tmp_bere = ber_init(reqdata);
  3764. if (tmp_bere == NULL) {
  3765. goto free_and_return;
  3766. }
  3767. if (ber_scanf(tmp_bere, "{a}", &shared_dn) == LBER_ERROR) {
  3768. ret = LDAP_PROTOCOL_ERROR;
  3769. goto free_and_return;
  3770. }
  3771. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3772. "dna_extend_exop: received range extension "
  3773. "request for range [%s]\n", shared_dn);
  3774. /* Only allow range requests from the replication bind DN user */
  3775. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
  3776. if (!dna_is_replica_bind_dn(shared_dn, bind_dn)) {
  3777. ret = LDAP_INSUFFICIENT_ACCESS;
  3778. goto free_and_return;
  3779. }
  3780. /* See if we have the req. range configured.
  3781. * If so, we need to see if we have range to provide. */
  3782. ret = dna_release_range(shared_dn, &lower, &upper);
  3783. if (ret == LDAP_SUCCESS) {
  3784. /* We have range to give away, so construct
  3785. * and send the response. */
  3786. BerElement *respber = NULL;
  3787. struct berval *respdata = NULL;
  3788. struct berval range_low = {0, NULL};
  3789. struct berval range_high = {0, NULL};
  3790. char lowstr[16];
  3791. char highstr[16];
  3792. /* Create the exop response */
  3793. PR_snprintf(lowstr, sizeof(lowstr), "%" NSPRIu64, lower);
  3794. PR_snprintf(highstr, sizeof(highstr), "%" NSPRIu64, upper);
  3795. range_low.bv_val = lowstr;
  3796. range_low.bv_len = strlen(range_low.bv_val);
  3797. range_high.bv_val = highstr;
  3798. range_high.bv_len = strlen(range_high.bv_val);
  3799. if ((respber = ber_alloc()) == NULL) {
  3800. ret = LDAP_NO_MEMORY;
  3801. goto free_and_return;
  3802. }
  3803. if (LBER_ERROR == (ber_printf(respber, "{oo}",
  3804. range_low.bv_val, range_low.bv_len,
  3805. range_high.bv_val, range_high.bv_len))) {
  3806. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3807. "dna_extend_exop: Unable to encode exop response.\n");
  3808. ber_free(respber, 1);
  3809. ret = LDAP_ENCODING_ERROR;
  3810. goto free_and_return;
  3811. }
  3812. ber_flatten(respber, &respdata);
  3813. ber_free(respber, 1);
  3814. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, DNA_EXTEND_EXOP_RESPONSE_OID);
  3815. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, respdata);
  3816. /* send the response ourselves */
  3817. slapi_send_ldap_result( pb, ret, NULL, NULL, 0, NULL );
  3818. ret = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  3819. ber_bvfree(respdata);
  3820. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3821. "dna_extend_exop: Released range %" NSPRIu64 "-%" NSPRIu64 ".\n",
  3822. lower, upper);
  3823. }
  3824. free_and_return:
  3825. slapi_ch_free_string(&shared_dn);
  3826. slapi_ch_free_string(&bind_dn);
  3827. if (NULL != tmp_bere) {
  3828. ber_free(tmp_bere, 1);
  3829. tmp_bere = NULL;
  3830. }
  3831. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3832. "<-- dna_extend_exop\n");
  3833. return ret;
  3834. }
  3835. /*
  3836. * dna_release_range()
  3837. *
  3838. * Checks if we have any values that we can release
  3839. * for the range specified by range_dn.
  3840. */
  3841. static int
  3842. dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper)
  3843. {
  3844. int ret = 0;
  3845. int match = 0;
  3846. PRCList *list = NULL;
  3847. Slapi_DN *cfg_base_sdn = NULL;
  3848. Slapi_DN *range_sdn = NULL;
  3849. struct configEntry *config_entry = NULL;
  3850. int set_extend_flag = 0;
  3851. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3852. "--> dna_release_range\n");
  3853. if (range_dn) {
  3854. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  3855. dna_read_lock();
  3856. /* Go through the config entries to see if we
  3857. * have a shared range configured that matches
  3858. * the range from the exop request. */
  3859. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3860. list = PR_LIST_HEAD(dna_global_config);
  3861. while ((list != dna_global_config) && match != 1) {
  3862. config_entry = (struct configEntry *)list;
  3863. cfg_base_sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_base);
  3864. if (slapi_sdn_compare(cfg_base_sdn, range_sdn) == 0) {
  3865. /* We found a match. Set match flag to
  3866. * break out of the loop. */
  3867. match = 1;
  3868. } else {
  3869. config_entry = NULL;
  3870. list = PR_NEXT_LINK(list);
  3871. }
  3872. slapi_sdn_free(&cfg_base_sdn);
  3873. }
  3874. }
  3875. /* config_entry will point to our match if we found one */
  3876. if (config_entry) {
  3877. int release = 0;
  3878. Slapi_PBlock *pb = NULL;
  3879. LDAPMod mod_replace;
  3880. LDAPMod *mods[2];
  3881. char *replace_val[2];
  3882. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  3883. char max_value[17];
  3884. /* Need to bail if we're performing a range request
  3885. * for this range. This is to prevent the case where two
  3886. * servers are asking each other for more range for the
  3887. * same managed range. This would result in a network
  3888. * deadlock until the idletimeout kills one of the
  3889. * connections. */
  3890. slapi_lock_mutex(config_entry->extend_lock);
  3891. if (config_entry->extend_in_progress) {
  3892. /* We're already processing a range extention, so bail. */
  3893. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3894. "dna_release_range: Already processing a "
  3895. "range extension request. Skipping request.\n");
  3896. slapi_unlock_mutex(config_entry->extend_lock);
  3897. ret = LDAP_UNWILLING_TO_PERFORM;
  3898. goto bail;
  3899. } else {
  3900. /* Set a flag indicating that we're attempting to extend this range */
  3901. config_entry->extend_in_progress = 1;
  3902. set_extend_flag = 1;
  3903. slapi_unlock_mutex(config_entry->extend_lock);
  3904. }
  3905. /* Obtain the lock for this range */
  3906. slapi_lock_mutex(config_entry->lock);
  3907. /* Refuse if we're at or below our threshold */
  3908. if (config_entry->remaining <= config_entry->threshold) {
  3909. ret = LDAP_UNWILLING_TO_PERFORM;
  3910. goto bail;
  3911. }
  3912. /* If we have a next range, we need to give up values from
  3913. * it instead of from the active range */
  3914. if (config_entry->next_range_lower != 0) {
  3915. /* Release up to half of our values from the next range. */
  3916. release = (((config_entry->next_range_upper - config_entry->next_range_lower + 1) /
  3917. 2) / config_entry->threshold) * config_entry->threshold;
  3918. if (release == 0) {
  3919. ret = LDAP_UNWILLING_TO_PERFORM;
  3920. goto bail;
  3921. }
  3922. *upper = config_entry->next_range_upper;
  3923. *lower = *upper - release + 1;
  3924. /* Try to set the new next range in the config */
  3925. ret = dna_update_next_range(config_entry, config_entry->next_range_lower,
  3926. *lower - 1);
  3927. } else {
  3928. /* We release up to half of our remaining values,
  3929. * but we'll only release a range that is a multiple
  3930. * of our threshold. */
  3931. release = ((config_entry->remaining / 2) /
  3932. config_entry->threshold) * config_entry->threshold;
  3933. if (release == 0) {
  3934. ret = LDAP_UNWILLING_TO_PERFORM;
  3935. goto bail;
  3936. }
  3937. /* We give away values from the upper end of our range. */
  3938. *upper = config_entry->maxval;
  3939. *lower = *upper - release + 1;
  3940. /* try to set the new maxval in the config entry */
  3941. PR_snprintf(max_value, sizeof(max_value),"%" NSPRIu64, (*lower - 1));
  3942. /* set up our replace modify operation */
  3943. replace_val[0] = max_value;
  3944. replace_val[1] = 0;
  3945. mod_replace.mod_op = LDAP_MOD_REPLACE;
  3946. mod_replace.mod_type = DNA_MAXVAL;
  3947. mod_replace.mod_values = replace_val;
  3948. mods[0] = &mod_replace;
  3949. mods[1] = 0;
  3950. pb = slapi_pblock_new();
  3951. if (NULL == pb) {
  3952. ret = LDAP_OPERATIONS_ERROR;
  3953. goto bail;
  3954. }
  3955. slapi_modify_internal_set_pb(pb, config_entry->dn,
  3956. mods, 0, 0, getPluginID(), 0);
  3957. slapi_modify_internal_pb(pb);
  3958. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  3959. slapi_pblock_destroy(pb);
  3960. pb = NULL;
  3961. if (ret == LDAP_SUCCESS) {
  3962. /* Adjust maxval in our cached config and shared config */
  3963. config_entry->maxval = *lower - 1;
  3964. dna_notice_allocation(config_entry, config_entry->nextval, 0);
  3965. }
  3966. }
  3967. if (ret != LDAP_SUCCESS) {
  3968. /* Updating the config failed, so reset. We don't
  3969. * want to give the caller any range */
  3970. *lower = 0;
  3971. *upper = 0;
  3972. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3973. "dna_release_range: Error updating "
  3974. "configuration entry [err=%d]\n", ret);
  3975. }
  3976. }
  3977. bail:
  3978. if (set_extend_flag) {
  3979. slapi_lock_mutex(config_entry->extend_lock);
  3980. config_entry->extend_in_progress = 0;
  3981. slapi_unlock_mutex(config_entry->extend_lock);
  3982. }
  3983. if (config_entry) {
  3984. slapi_unlock_mutex(config_entry->lock);
  3985. }
  3986. slapi_sdn_free(&range_sdn);
  3987. dna_unlock();
  3988. }
  3989. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3990. "<-- dna_release_range\n");
  3991. return ret;
  3992. }
  3993. /****************************************************
  3994. End of
  3995. Functions that actually do things other
  3996. than config and startup
  3997. ****************************************************/
  3998. /**
  3999. * debug functions to print config
  4000. */
  4001. void dna_dump_config()
  4002. {
  4003. PRCList *list;
  4004. dna_read_lock();
  4005. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  4006. list = PR_LIST_HEAD(dna_global_config);
  4007. while (list != dna_global_config) {
  4008. dna_dump_config_entry((struct configEntry *) list);
  4009. list = PR_NEXT_LINK(list);
  4010. }
  4011. }
  4012. dna_unlock();
  4013. }
  4014. /*
  4015. * dna_isrepl()
  4016. *
  4017. * Returns 1 if the operation associated with pb
  4018. * is a replicated op. Returns 0 otherwise.
  4019. */
  4020. static int
  4021. dna_isrepl(Slapi_PBlock *pb)
  4022. {
  4023. int is_repl = 0;
  4024. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  4025. return is_repl;
  4026. }
  4027. void dna_dump_config_entry(struct configEntry * entry)
  4028. {
  4029. int i = 0;
  4030. for (i = 0; entry->types && entry->types[i]; i++) {
  4031. printf("<---- type -----------> %s\n", entry->types[i]);
  4032. }
  4033. printf("<---- filter ---------> %s\n", entry->filter);
  4034. printf("<---- prefix ---------> %s\n", entry->prefix);
  4035. printf("<---- scope ----------> %s\n", entry->scope);
  4036. for (i = 0; entry->excludescope && entry->excludescope[i]; i++) {
  4037. printf("<---- excluded scope -> %s\n", slapi_sdn_get_dn(entry->excludescope[i]));
  4038. }
  4039. printf("<---- next value -----> %" PRIu64 "\n", entry->nextval);
  4040. printf("<---- max value ------> %" PRIu64 "\n", entry->maxval);
  4041. printf("<---- interval -------> %" PRIu64 "\n", entry->interval);
  4042. printf("<---- generate flag --> %s\n", entry->generate);
  4043. printf("<---- shared cfg base > %s\n", entry->shared_cfg_base);
  4044. printf("<---- shared cfg DN --> %s\n", entry->shared_cfg_dn);
  4045. printf("<---- threshold ------> %" PRIu64 "", entry->threshold);
  4046. }