dna.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629
  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 (dna_request_range(config_entry, (struct dnaServer *)server,
  1422. &lower, &upper) != 0) {
  1423. server = PR_NEXT_LINK(server);
  1424. } else {
  1425. /* Someone provided us with a new range. Attempt
  1426. * to update the config. */
  1427. if ((ret = dna_update_next_range(config_entry, lower, upper)) == 0) {
  1428. break;
  1429. }
  1430. server = PR_NEXT_LINK(server);
  1431. }
  1432. }
  1433. /* free the list of servers */
  1434. dna_delete_shared_servers(&servers);
  1435. }
  1436. }
  1437. bail:
  1438. return ret;
  1439. }
  1440. /* dna_notice_allocation()
  1441. *
  1442. * Called after a new value has been allocated from the range.
  1443. * This function will update the config entries and cached info
  1444. * appropriately. This includes activating the next range if
  1445. * we've exhausted the current range.
  1446. *
  1447. * The last parameter is the value that has just been allocated.
  1448. * The new parameter should be the next available value. If you
  1449. * set both of these parameters to 0, then this function will
  1450. * just check if the next range needs to be activated and update
  1451. * the config accordingly.
  1452. *
  1453. * The lock for configEntry should be obtained before calling
  1454. * this function. */
  1455. static void
  1456. dna_notice_allocation(struct configEntry *config_entry, PRUint64 new,
  1457. PRUint64 last)
  1458. {
  1459. /* update our cached config entry */
  1460. if ((new != 0) && (new <= (config_entry->maxval + config_entry->interval))) {
  1461. config_entry->nextval = new;
  1462. }
  1463. /* check if we've exhausted our active range */
  1464. if ((last == config_entry->maxval) ||
  1465. (config_entry->nextval > config_entry->maxval)) {
  1466. /* If we already have a next range set, make it the
  1467. * new active range. */
  1468. if (config_entry->next_range_lower != 0) {
  1469. /* Make the next range active */
  1470. if (dna_activate_next_range(config_entry) != 0) {
  1471. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1472. "dna_notice_allocation: Unable to activate "
  1473. "the next range for range %s.\n", config_entry->dn);
  1474. }
  1475. } else {
  1476. config_entry->remaining = 0;
  1477. /* update the shared configuration */
  1478. dna_update_shared_config(config_entry);
  1479. }
  1480. } else {
  1481. if (config_entry->next_range_lower != 0) {
  1482. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1483. config_entry->interval) + ((config_entry->next_range_upper -
  1484. config_entry->next_range_lower +1) / config_entry->interval);
  1485. } else {
  1486. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1487. config_entry->interval);
  1488. }
  1489. /* update the shared configuration */
  1490. dna_update_shared_config(config_entry);
  1491. }
  1492. return;
  1493. }
  1494. static int
  1495. dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers, int get_all)
  1496. {
  1497. int ret = LDAP_SUCCESS;
  1498. Slapi_PBlock *pb = NULL;
  1499. Slapi_Entry **entries = NULL;
  1500. char *attrs[7];
  1501. /* First do a search in the shared config area for this
  1502. * range to find other servers who are managing this range. */
  1503. attrs[0] = DNA_HOSTNAME;
  1504. attrs[1] = DNA_PORTNUM;
  1505. attrs[2] = DNA_SECURE_PORTNUM;
  1506. attrs[3] = DNA_REMAINING;
  1507. attrs[4] = DNA_REMOTE_BIND_METHOD;
  1508. attrs[5] = DNA_REMOTE_CONN_PROT;
  1509. attrs[6] = NULL;
  1510. pb = slapi_pblock_new();
  1511. if (NULL == pb) {
  1512. ret = LDAP_OPERATIONS_ERROR;
  1513. goto cleanup;
  1514. }
  1515. slapi_search_internal_set_pb(pb, config_entry->shared_cfg_base,
  1516. LDAP_SCOPE_ONELEVEL, "objectclass=*",
  1517. attrs, 0, NULL,
  1518. NULL, getPluginID(), 0);
  1519. slapi_search_internal_pb(pb);
  1520. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1521. if (LDAP_SUCCESS != ret) {
  1522. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1523. "dna_get_shared_servers: search failed for shared "
  1524. "config: %s [error %d]\n", config_entry->shared_cfg_base,
  1525. ret);
  1526. goto cleanup;
  1527. }
  1528. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1529. &entries);
  1530. if (entries && entries[0]) {
  1531. Slapi_DN *cfg_sdn = NULL;
  1532. int i;
  1533. cfg_sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_dn);
  1534. /* We found some entries. Go through them and
  1535. * order them based off of remaining values. */
  1536. for (i = 0; entries[i]; i++) {
  1537. /* skip our own shared config entry, unless we want all the servers */
  1538. if (get_all || slapi_sdn_compare(cfg_sdn, slapi_entry_get_sdn(entries[i]))) {
  1539. struct dnaServer *server = NULL;
  1540. /* set up the server list entry */
  1541. server = (struct dnaServer *) slapi_ch_calloc(1,
  1542. sizeof(struct dnaServer));
  1543. server->sdn = slapi_sdn_new_dn_byval(slapi_entry_get_ndn(entries[i]));
  1544. server->host = slapi_entry_attr_get_charptr(entries[i],
  1545. DNA_HOSTNAME);
  1546. server->port = slapi_entry_attr_get_uint(entries[i], DNA_PORTNUM);
  1547. server->secureport = slapi_entry_attr_get_uint(entries[i], DNA_SECURE_PORTNUM);
  1548. server->remaining = slapi_entry_attr_get_ulonglong(entries[i],
  1549. DNA_REMAINING);
  1550. server->remote_binddn = config_entry->remote_binddn;
  1551. server->remote_bindpw = config_entry->remote_bindpw;
  1552. server->remote_bind_method = slapi_entry_attr_get_charptr(entries[i],
  1553. DNA_REMOTE_BIND_METHOD);
  1554. server->remote_conn_prot = slapi_entry_attr_get_charptr(entries[i],
  1555. DNA_REMOTE_CONN_PROT);
  1556. /* validate the entry */
  1557. if (!server->host || (server->port == 0 && server->secureport == 0) || server->remaining == 0)
  1558. {
  1559. /* free and skip this one */
  1560. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1561. "dna_get_shared_servers: skipping invalid "
  1562. "shared config entry (%s)\n", slapi_entry_get_dn(entries[i]));
  1563. dna_free_shared_server(&server);
  1564. continue;
  1565. }
  1566. /* see if we defined a server manually */
  1567. if(server->remote_bind_method){
  1568. char *reason = NULL;
  1569. int err = 0;
  1570. if(strcasecmp(server->remote_bind_method, DNA_METHOD_DIGESTMD5) == 0 ||
  1571. strcasecmp(server->remote_bind_method, DNA_METHOD_SIMPLE) == 0){
  1572. /* requires a DN and password */
  1573. if(!server->remote_binddn || !server->remote_bindpw){
  1574. reason = "missing bind DN and/or password.";
  1575. err = 1;
  1576. }
  1577. }
  1578. if(strcasecmp(server->remote_bind_method, DNA_METHOD_SSL) == 0){
  1579. /* requires a bind DN */
  1580. if(strcasecmp(server->remote_conn_prot, DNA_PROT_SSL) != 0 &&
  1581. strcasecmp(server->remote_conn_prot, DNA_PROT_TLS) != 0 )
  1582. {
  1583. reason = "bind method (SSL) requires either SSL or TLS connection "
  1584. "protocol.";
  1585. err = 1;
  1586. }
  1587. }
  1588. if(err){
  1589. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1590. "dna_get_shared_servers: skipping invalid "
  1591. "shared config entry (%s). Reason: %s\n",
  1592. slapi_entry_get_dn(entries[i]), reason);
  1593. dna_free_shared_server(&server);
  1594. continue;
  1595. }
  1596. /* everything is ok */
  1597. server->remote_defined = 1;
  1598. }
  1599. /* add a server entry to the list */
  1600. if (*servers == NULL) {
  1601. /* first entry */
  1602. *servers = (PRCList *) slapi_ch_calloc(1,
  1603. sizeof(struct dnaServer));
  1604. PR_INIT_CLIST(*servers);
  1605. PR_INSERT_LINK(&(server->list), *servers);
  1606. } else {
  1607. /* Find the right slot for this entry. We
  1608. * want to order the entries based off of
  1609. * the remaining number of values, highest
  1610. * to lowest. */
  1611. struct dnaServer *sitem;
  1612. PRCList* item = PR_LIST_HEAD(*servers);
  1613. int inserted = 0;
  1614. while (item != *servers) {
  1615. sitem = (struct dnaServer *)item;
  1616. if (server->remaining > sitem->remaining) {
  1617. PR_INSERT_BEFORE(&(server->list), item);
  1618. inserted = 1;
  1619. break;
  1620. }
  1621. item = PR_NEXT_LINK(item);
  1622. if (*servers == item) {
  1623. /* add to tail */
  1624. PR_INSERT_BEFORE(&(server->list), item);
  1625. inserted = 1;
  1626. break;
  1627. }
  1628. }
  1629. if(!inserted){
  1630. dna_free_shared_server(&server);
  1631. }
  1632. }
  1633. }
  1634. }
  1635. slapi_sdn_free(&cfg_sdn);
  1636. }
  1637. cleanup:
  1638. slapi_free_search_results_internal(pb);
  1639. slapi_pblock_destroy(pb);
  1640. return ret;
  1641. }
  1642. /*
  1643. * dna_request_range()
  1644. *
  1645. * Requests range extension from another server.
  1646. * Returns 0 on success and will fill in upper
  1647. * and lower. Returns non-0 on failure and will
  1648. * zero out upper and lower.
  1649. */
  1650. static int dna_request_range(struct configEntry *config_entry,
  1651. struct dnaServer *server,
  1652. PRUint64 *lower, PRUint64 *upper)
  1653. {
  1654. char *bind_dn = NULL;
  1655. char *bind_passwd = NULL;
  1656. char *bind_method = NULL;
  1657. int is_ssl = 0;
  1658. struct berval *request = NULL;
  1659. char *retoid = NULL;
  1660. struct berval *responsedata = NULL;
  1661. BerElement *respber = NULL;
  1662. LDAP *ld = NULL;
  1663. char *lower_str = NULL;
  1664. char *upper_str = NULL;
  1665. int set_extend_flag = 0;
  1666. int ret = LDAP_OPERATIONS_ERROR;
  1667. int port = 0;
  1668. int timelimit;
  1669. #if defined(USE_OPENLDAP)
  1670. struct timeval timeout;
  1671. #endif
  1672. /* See if we're allowed to send a range request now */
  1673. slapi_lock_mutex(config_entry->extend_lock);
  1674. if (config_entry->extend_in_progress) {
  1675. /* We're already processing a range extention, so bail. */
  1676. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1677. "dna_request_range: Already processing a "
  1678. "range extension request. Skipping request.\n");
  1679. slapi_unlock_mutex(config_entry->extend_lock);
  1680. goto bail;
  1681. } else {
  1682. /* Set a flag indicating that we're attempting to extend this range */
  1683. config_entry->extend_in_progress = 1;
  1684. set_extend_flag = 1;
  1685. slapi_unlock_mutex(config_entry->extend_lock);
  1686. }
  1687. /* Fetch the replication bind dn info */
  1688. if (dna_get_replica_bind_creds(config_entry->shared_cfg_base, server,
  1689. &bind_dn, &bind_passwd, &bind_method,
  1690. &is_ssl, &port) != 0) {
  1691. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1692. "dna_request_range: Unable to retrieve "
  1693. "replica bind credentials.\n");
  1694. goto bail;
  1695. }
  1696. if ((request = dna_create_range_request(config_entry->shared_cfg_base)) == NULL) {
  1697. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1698. "dna_request_range: Failed to create "
  1699. "range extension extended operation request.\n");
  1700. goto bail;
  1701. }
  1702. if ((ld = slapi_ldap_init(server->host, port, is_ssl, 0)) == NULL) {
  1703. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1704. "dna_request_range: Unable to "
  1705. "initialize LDAP session to server %s:%u.\n",
  1706. server->host, server->port);
  1707. goto bail;
  1708. }
  1709. /* Disable referrals and set timelimit and a connect timeout */
  1710. ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1711. timelimit = config_entry->timeout / 1000; /* timeout is in msec */
  1712. ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &timelimit);
  1713. #if defined(USE_OPENLDAP)
  1714. timeout.tv_sec = config_entry->timeout / 1000;
  1715. timeout.tv_usec = (config_entry->timeout % 1000) * 1000;
  1716. ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &timeout);
  1717. #else
  1718. ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &config_entry->timeout);
  1719. #endif
  1720. /* Bind to the replica server */
  1721. ret = slapi_ldap_bind(ld, bind_dn, bind_passwd, bind_method,
  1722. NULL, NULL, NULL, NULL);
  1723. if (ret != LDAP_SUCCESS) {
  1724. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1725. "dna_request_range: Error binding "
  1726. " to replica server %s:%u. [error %d]\n",
  1727. server->host, server->port, ret);
  1728. goto bail;
  1729. }
  1730. /* Send range extension request */
  1731. ret = ldap_extended_operation_s(ld, DNA_EXTEND_EXOP_REQUEST_OID,
  1732. request, NULL, NULL, &retoid, &responsedata);
  1733. if (ret != LDAP_SUCCESS) {
  1734. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1735. "dna_request_range: Error sending "
  1736. "range extension extended operation request "
  1737. "to server %s:%u [error %d]\n", server->host,
  1738. server->port, ret);
  1739. goto bail;
  1740. }
  1741. /* Verify that the OID is correct. */
  1742. if (strcmp(retoid, DNA_EXTEND_EXOP_RESPONSE_OID) != 0) {
  1743. ret = LDAP_OPERATIONS_ERROR;
  1744. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1745. "dna_request_range: Received incorrect response OID.\n");
  1746. goto bail;
  1747. }
  1748. /* Parse response */
  1749. if (BV_HAS_DATA(responsedata)) {
  1750. respber = ber_init(responsedata);
  1751. if (ber_scanf(respber, "{aa}", &lower_str, &upper_str) == LBER_ERROR) {
  1752. ret = LDAP_PROTOCOL_ERROR;
  1753. goto bail;
  1754. }
  1755. }
  1756. /* Fill in upper and lower */
  1757. if (upper_str && lower_str) {
  1758. *upper = strtoull(upper_str, 0, 0);
  1759. *lower = strtoull(lower_str, 0, 0);
  1760. ret = 0;
  1761. } else {
  1762. ret = LDAP_OPERATIONS_ERROR;
  1763. }
  1764. bail:
  1765. if (set_extend_flag) {
  1766. slapi_lock_mutex(config_entry->extend_lock);
  1767. config_entry->extend_in_progress = 0;
  1768. slapi_unlock_mutex(config_entry->extend_lock);
  1769. }
  1770. slapi_ldap_unbind(ld);
  1771. slapi_ch_free_string(&bind_dn);
  1772. slapi_ch_free_string(&bind_passwd);
  1773. slapi_ch_free_string(&bind_method);
  1774. slapi_ch_free_string(&retoid);
  1775. slapi_ch_free_string(&lower_str);
  1776. slapi_ch_free_string(&upper_str);
  1777. ber_free(respber, 1);
  1778. ber_bvfree(request);
  1779. ber_bvfree(responsedata);
  1780. if (ret != 0) {
  1781. *upper = 0;
  1782. *lower = 0;
  1783. }
  1784. return ret;
  1785. }
  1786. static struct berval *dna_create_range_request(char *range_dn)
  1787. {
  1788. struct berval *requestdata = NULL;
  1789. struct berval shared_dn = { 0, NULL };
  1790. BerElement *ber = NULL;
  1791. shared_dn.bv_val = range_dn;
  1792. shared_dn.bv_len = strlen(shared_dn.bv_val);
  1793. if((ber = ber_alloc()) == NULL) {
  1794. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1795. "dna_create_range_request: Error "
  1796. "allocating request data.\n");
  1797. goto bail;
  1798. }
  1799. if (LBER_ERROR == (ber_printf(ber, "{o}", shared_dn.bv_val, shared_dn.bv_len))) {
  1800. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1801. "dna_create_range_request: Error "
  1802. "encoding request data.\n");
  1803. goto bail;
  1804. }
  1805. if (ber_flatten(ber, &requestdata) == -1) {
  1806. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1807. "dna_create_range_request: Error "
  1808. "encoding request data.\n");
  1809. goto bail;
  1810. }
  1811. bail:
  1812. ber_free(ber, 1);
  1813. return requestdata;
  1814. }
  1815. /****************************************************
  1816. Helpers
  1817. ****************************************************/
  1818. static char *dna_get_dn(Slapi_PBlock * pb)
  1819. {
  1820. Slapi_DN *sdn = 0;
  1821. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1822. "--> dna_get_dn\n");
  1823. if (slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn)) {
  1824. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1825. "dna_get_dn: failed to get dn of changed entry");
  1826. goto bail;
  1827. }
  1828. bail:
  1829. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1830. "<-- dna_get_dn\n");
  1831. return (char *)slapi_sdn_get_dn(sdn);
  1832. }
  1833. /* config check
  1834. matching config dn or a descendent reloads config
  1835. */
  1836. static int dna_dn_is_config(char *dn)
  1837. {
  1838. int ret = 0;
  1839. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1840. "--> dna_is_config\n");
  1841. if (slapi_dn_issuffix(dn, getPluginDN())) {
  1842. ret = 1;
  1843. }
  1844. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1845. "<-- dna_is_config\n");
  1846. return ret;
  1847. }
  1848. static int
  1849. dna_dn_is_shared_config(Slapi_PBlock *pb, char *dn)
  1850. {
  1851. struct configEntry *config_entry = NULL;
  1852. Slapi_Entry *entry = NULL;
  1853. Slapi_Attr *attr = NULL;
  1854. PRCList *list = NULL;
  1855. int ret = 0;
  1856. dna_read_lock();
  1857. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1858. list = PR_LIST_HEAD(dna_global_config);
  1859. while (list != dna_global_config) {
  1860. config_entry = (struct configEntry *) list;
  1861. if (slapi_dn_issuffix(dn, config_entry->shared_cfg_base)) {
  1862. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &entry);
  1863. /* If the entry has the dnaHost attribute, it is a shared entry */
  1864. if (slapi_entry_attr_find(entry, DNA_HOSTNAME, &attr) == 0) {
  1865. ret = 1;
  1866. break;
  1867. }
  1868. }
  1869. list = PR_NEXT_LINK(list);
  1870. }
  1871. }
  1872. dna_unlock();
  1873. return ret;
  1874. }
  1875. #define DNA_LDAP_TAG_SK_REVERSE 0x81L
  1876. static LDAPControl *dna_build_sort_control(const char *attr)
  1877. {
  1878. LDAPControl *ctrl;
  1879. BerElement *ber;
  1880. int rc;
  1881. ber = ber_alloc();
  1882. if (NULL == ber)
  1883. return NULL;
  1884. rc = ber_printf(ber, "{{stb}}", attr, DNA_LDAP_TAG_SK_REVERSE, 1);
  1885. if (-1 == rc) {
  1886. ber_free(ber, 1);
  1887. return NULL;
  1888. }
  1889. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  1890. ber_free(ber, 1);
  1891. if (LDAP_SUCCESS != rc)
  1892. return NULL;
  1893. return ctrl;
  1894. }
  1895. /****************************************************
  1896. Functions that actually do things other
  1897. than config and startup
  1898. ****************************************************/
  1899. /*
  1900. * dna_first_free_value()
  1901. *
  1902. * We do search all values between nextval and maxval asking the
  1903. * server to sort them, then we check the first free spot and
  1904. * use it as newval. If we go past the end of the range, we
  1905. * return LDAP_OPERATIONS_ERROR and set newval to be > the
  1906. * maximum configured value for this range. */
  1907. static int
  1908. dna_first_free_value(struct configEntry *config_entry,
  1909. PRUint64 *newval)
  1910. {
  1911. Slapi_Entry **entries = NULL;
  1912. Slapi_PBlock *pb = NULL;
  1913. LDAPControl **ctrls = NULL;
  1914. char *filter = NULL;
  1915. char *prefix = NULL;
  1916. int multitype = 0;
  1917. int result, status;
  1918. PRUint64 tmpval, sval, i;
  1919. char *strval = NULL;
  1920. /* check if the config is already out of range */
  1921. if (config_entry->nextval > config_entry->maxval) {
  1922. *newval = config_entry->nextval;
  1923. return LDAP_OPERATIONS_ERROR;
  1924. }
  1925. prefix = config_entry->prefix;
  1926. tmpval = config_entry->nextval;
  1927. if (dna_is_multitype_range(config_entry)) {
  1928. multitype = 1;
  1929. }
  1930. /* We don't sort if we're using a prefix (non integer type) or if this
  1931. * is a multi-type range. Instead, we just search to see if the next
  1932. * value is free, and keep incrementing until we find the next free value. */
  1933. if (prefix || multitype) {
  1934. /* This will allocate the filter string for us. */
  1935. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1936. } else {
  1937. /* This is a single-type range, so just use the first (only)
  1938. * type from the list. */
  1939. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  1940. if (NULL == ctrls)
  1941. return LDAP_OPERATIONS_ERROR;
  1942. ctrls[0] = dna_build_sort_control(config_entry->types[0]);
  1943. if (NULL == ctrls[0]) {
  1944. slapi_ch_free((void **)&ctrls);
  1945. return LDAP_OPERATIONS_ERROR;
  1946. }
  1947. filter = slapi_ch_smprintf("(&%s(&(%s>=%" NSPRIu64 ")(%s<=%" NSPRIu64 ")))",
  1948. config_entry->filter,
  1949. config_entry->types[0], tmpval,
  1950. config_entry->types[0], config_entry->maxval);
  1951. }
  1952. if (NULL == filter) {
  1953. ldap_controls_free(ctrls);
  1954. ctrls = NULL;
  1955. return LDAP_OPERATIONS_ERROR;
  1956. }
  1957. pb = slapi_pblock_new();
  1958. if (NULL == pb) {
  1959. ldap_controls_free(ctrls);
  1960. ctrls = NULL;
  1961. slapi_ch_free_string(&filter);
  1962. return LDAP_OPERATIONS_ERROR;
  1963. }
  1964. slapi_search_internal_set_pb(pb, config_entry->scope,
  1965. LDAP_SCOPE_SUBTREE, filter,
  1966. config_entry->types, 0, ctrls,
  1967. NULL, getPluginID(), 0);
  1968. slapi_search_internal_pb(pb);
  1969. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1970. if (LDAP_SUCCESS != result) {
  1971. status = LDAP_OPERATIONS_ERROR;
  1972. goto cleanup;
  1973. }
  1974. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1975. &entries);
  1976. if (NULL == entries || NULL == entries[0]) {
  1977. /* no values means we already have a good value */
  1978. *newval = tmpval;
  1979. status = LDAP_SUCCESS;
  1980. goto cleanup;
  1981. }
  1982. if (prefix || multitype) {
  1983. /* The next value identified in the config entry has already
  1984. * been taken. We just iterate through the values until we
  1985. * (hopefully) find a free one. */
  1986. for (tmpval += config_entry->interval; tmpval <= config_entry->maxval;
  1987. tmpval += config_entry->interval) {
  1988. /* This will reuse the old memory for the previous filter. It is
  1989. * guaranteed to have enough space since the filter is the same
  1990. * aside from the assertion value (we allocated enough for the
  1991. * largest supported integer). */
  1992. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1993. /* clear out the pblock so we can re-use it */
  1994. slapi_free_search_results_internal(pb);
  1995. slapi_pblock_init(pb);
  1996. slapi_search_internal_set_pb(pb, config_entry->scope,
  1997. LDAP_SCOPE_SUBTREE, filter,
  1998. config_entry->types, 0, 0,
  1999. NULL, getPluginID(), 0);
  2000. slapi_search_internal_pb(pb);
  2001. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2002. if (LDAP_SUCCESS != result) {
  2003. status = LDAP_OPERATIONS_ERROR;
  2004. goto cleanup;
  2005. }
  2006. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2007. &entries);
  2008. if (NULL == entries || NULL == entries[0]) {
  2009. /* no values means we already have a good value */
  2010. *newval = tmpval;
  2011. status = LDAP_SUCCESS;
  2012. goto cleanup;
  2013. }
  2014. }
  2015. } else {
  2016. /* Entries are sorted and filtered for value >= tval therefore if the
  2017. * first one does not match tval it means that the value is free,
  2018. * otherwise we need to cycle through values until we find a mismatch,
  2019. * the first mismatch is the first free pit. This is guaranteed to
  2020. * be a single-type range, so we can just use the first (only)
  2021. * type from the list of types directly. */
  2022. sval = 0;
  2023. for (i = 0; NULL != entries[i]; i++) {
  2024. strval = slapi_entry_attr_get_charptr(entries[i], config_entry->types[0]);
  2025. errno = 0;
  2026. sval = strtoull(strval, 0, 0);
  2027. if (errno) {
  2028. /* something very wrong here ... */
  2029. status = LDAP_OPERATIONS_ERROR;
  2030. goto cleanup;
  2031. }
  2032. slapi_ch_free_string(&strval);
  2033. if (tmpval != sval)
  2034. break;
  2035. if (config_entry->maxval < sval)
  2036. break;
  2037. tmpval += config_entry->interval;
  2038. }
  2039. }
  2040. /* check if we went past the end of the range */
  2041. if (tmpval <= config_entry->maxval) {
  2042. *newval = tmpval;
  2043. status = LDAP_SUCCESS;
  2044. } else {
  2045. /* we set newval past the end of the range
  2046. * so the caller can easily detect that we
  2047. * overflowed the configured range. */
  2048. *newval = tmpval;
  2049. status = LDAP_OPERATIONS_ERROR;
  2050. }
  2051. cleanup:
  2052. slapi_ch_free_string(&filter);
  2053. slapi_ch_free_string(&strval);
  2054. slapi_free_search_results_internal(pb);
  2055. slapi_pblock_destroy(pb);
  2056. return status;
  2057. }
  2058. /*
  2059. * Perform ldap operationally atomic increment
  2060. * Return the next value to be assigned
  2061. */
  2062. static int dna_get_next_value(struct configEntry *config_entry,
  2063. char **next_value_ret)
  2064. {
  2065. Slapi_PBlock *pb = NULL;
  2066. LDAPMod mod_replace;
  2067. LDAPMod *mods[2];
  2068. char *replace_val[2];
  2069. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2070. char next_value[17];
  2071. PRUint64 setval = 0;
  2072. PRUint64 nextval = 0;
  2073. int ret;
  2074. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2075. "--> dna_get_next_value\n");
  2076. /* get the lock to prevent contention with other threads over
  2077. * the next new value for this range. */
  2078. slapi_lock_mutex(config_entry->lock);
  2079. /* get the first value */
  2080. ret = dna_first_free_value(config_entry, &setval);
  2081. if (LDAP_SUCCESS != ret) {
  2082. /* check if we overflowed the configured range */
  2083. if (setval > config_entry->maxval) {
  2084. /* This should not happen, as pre_op should of allocated the next range.
  2085. * In case this does occur, we will attempt to activate the next range if
  2086. * one is available. We tell dna_fix_maxval() to skip sending a range
  2087. * transfer request, we we don't want to perform any network operations
  2088. * while within a transaction. */
  2089. ret = dna_fix_maxval(config_entry, 1 /* skip range transfer request */ );
  2090. if (LDAP_SUCCESS != ret) {
  2091. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2092. "dna_get_next_value: no more values available!!\n");
  2093. goto done;
  2094. }
  2095. /* get the first value from our newly extended range */
  2096. ret = dna_first_free_value(config_entry, &setval);
  2097. if (LDAP_SUCCESS != ret)
  2098. goto done;
  2099. } else {
  2100. /* dna_first_free_value() failed for some unknown reason */
  2101. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2102. "dna_get_next_value: failed to allocate a new ID!!\n");
  2103. goto done;
  2104. }
  2105. }
  2106. nextval = setval + config_entry->interval;
  2107. /* update nextval if we have not reached the end
  2108. * of our current range */
  2109. if ((config_entry->maxval == -1) ||
  2110. (nextval <= (config_entry->maxval + config_entry->interval))) {
  2111. /* try to set the new next value in the config entry */
  2112. PR_snprintf(next_value, sizeof(next_value),"%" NSPRIu64, nextval);
  2113. /* set up our replace modify operation */
  2114. replace_val[0] = next_value;
  2115. replace_val[1] = 0;
  2116. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2117. mod_replace.mod_type = DNA_NEXTVAL;
  2118. mod_replace.mod_values = replace_val;
  2119. mods[0] = &mod_replace;
  2120. mods[1] = 0;
  2121. pb = slapi_pblock_new();
  2122. if (NULL == pb) {
  2123. ret = LDAP_OPERATIONS_ERROR;
  2124. goto done;
  2125. }
  2126. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2127. mods, 0, 0, getPluginID(), 0);
  2128. slapi_modify_internal_pb(pb);
  2129. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2130. }
  2131. if (LDAP_SUCCESS == ret) {
  2132. slapi_ch_free_string(next_value_ret);
  2133. *next_value_ret = slapi_ch_smprintf("%" NSPRIu64, setval);
  2134. if (NULL == *next_value_ret) {
  2135. ret = LDAP_OPERATIONS_ERROR;
  2136. goto done;
  2137. }
  2138. /* update our cached config */
  2139. dna_notice_allocation(config_entry, nextval, setval);
  2140. }
  2141. done:
  2142. slapi_unlock_mutex(config_entry->lock);
  2143. if (pb) {
  2144. slapi_pblock_destroy(pb);
  2145. }
  2146. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2147. "<-- dna_get_next_value\n");
  2148. return ret;
  2149. }
  2150. /*
  2151. * Get a value from the global server list. The dna_server_read_lock()
  2152. * should be held prior to calling this function.
  2153. */
  2154. static int
  2155. dna_get_shared_config_attr_val(struct configEntry *config_entry, char *attr, char *value)
  2156. {
  2157. struct dnaServer *server = NULL;
  2158. Slapi_DN *server_sdn = NULL;
  2159. int found = 0;
  2160. server_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_dn);
  2161. if (dna_global_servers) {
  2162. server = dna_global_servers;
  2163. while (server) {
  2164. if(slapi_sdn_compare(server->sdn, server_sdn) == 0){
  2165. if(strcmp(attr, DNA_REMOTE_BIND_METHOD) == 0){
  2166. if (server->remote_bind_method) {
  2167. PR_snprintf(value, DNA_REMOTE_BUFSIZ, "%s", server->remote_bind_method);
  2168. found = 1;
  2169. }
  2170. break;
  2171. } else if(strcmp(attr, DNA_REMOTE_CONN_PROT) == 0){
  2172. if (server->remote_conn_prot) {
  2173. PR_snprintf(value, DNA_REMOTE_BUFSIZ, "%s", server->remote_conn_prot);
  2174. found = 1;
  2175. }
  2176. break;
  2177. }
  2178. }
  2179. server = server->next;
  2180. }
  2181. }
  2182. slapi_sdn_free(&server_sdn);
  2183. return found;
  2184. }
  2185. /*
  2186. * dna_update_shared_config()
  2187. *
  2188. * Updates the shared config entry if one is
  2189. * configured. Returns the LDAP result code.
  2190. *
  2191. * The lock for configEntry should be obtained
  2192. * before calling this function.
  2193. * */
  2194. static int
  2195. dna_update_shared_config(struct configEntry *config_entry)
  2196. {
  2197. int ret = LDAP_SUCCESS;
  2198. if (config_entry && config_entry->shared_cfg_dn) {
  2199. /* Update the shared config entry if one is configured */
  2200. Slapi_PBlock *pb = NULL;
  2201. LDAPMod mod_replace;
  2202. LDAPMod *mods[2];
  2203. char *replace_val[2];
  2204. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2205. char remaining_vals[17];
  2206. /* We store the number of remaining assigned values
  2207. * in the shared config entry. */
  2208. PR_snprintf(remaining_vals, sizeof(remaining_vals),"%" NSPRIu64,
  2209. config_entry->remaining);
  2210. /* set up our replace modify operation */
  2211. replace_val[0] = remaining_vals;
  2212. replace_val[1] = 0;
  2213. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2214. mod_replace.mod_type = DNA_REMAINING;
  2215. mod_replace.mod_values = replace_val;
  2216. mods[0] = &mod_replace;
  2217. mods[1] = 0;
  2218. pb = slapi_pblock_new();
  2219. if (NULL == pb) {
  2220. ret = LDAP_OPERATIONS_ERROR;
  2221. } else {
  2222. slapi_modify_internal_set_pb(pb, config_entry->shared_cfg_dn,
  2223. mods, NULL, NULL, getPluginID(), 0);
  2224. slapi_modify_internal_pb(pb);
  2225. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2226. /* If the shared config for this instance doesn't
  2227. * already exist, we add it. */
  2228. if (ret == LDAP_NO_SUCH_OBJECT) {
  2229. Slapi_Entry *e = NULL;
  2230. Slapi_DN *sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_dn);
  2231. char bind_meth[DNA_REMOTE_BUFSIZ];
  2232. char conn_prot[DNA_REMOTE_BUFSIZ];
  2233. /* Set up the new shared config entry */
  2234. e = slapi_entry_alloc();
  2235. /* the entry now owns the dup'd dn */
  2236. slapi_entry_init_ext(e, sdn, NULL); /* sdn is copied into e */
  2237. slapi_sdn_free(&sdn);
  2238. slapi_entry_add_string(e, SLAPI_ATTR_OBJECTCLASS, DNA_SHAREDCONFIG);
  2239. slapi_entry_add_string(e, DNA_HOSTNAME, hostname);
  2240. slapi_entry_add_string(e, DNA_PORTNUM, portnum);
  2241. if (secureportnum) {
  2242. slapi_entry_add_string(e, DNA_SECURE_PORTNUM, secureportnum);
  2243. }
  2244. slapi_entry_add_string(e, DNA_REMAINING, remaining_vals);
  2245. /* Grab the remote server settings */
  2246. dna_server_read_lock();
  2247. if(dna_get_shared_config_attr_val(config_entry, DNA_REMOTE_BIND_METHOD, bind_meth)) {
  2248. slapi_entry_add_string(e, DNA_REMOTE_BIND_METHOD, bind_meth);
  2249. }
  2250. if(dna_get_shared_config_attr_val(config_entry, DNA_REMOTE_CONN_PROT, conn_prot)){
  2251. slapi_entry_add_string(e, DNA_REMOTE_CONN_PROT,conn_prot);
  2252. }
  2253. dna_server_unlock();
  2254. /* clear pb for re-use */
  2255. slapi_pblock_init(pb);
  2256. /* e will be consumed by slapi_add_internal() */
  2257. slapi_add_entry_internal_set_pb(pb, e, NULL, getPluginID(), 0);
  2258. slapi_add_internal_pb(pb);
  2259. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2260. }
  2261. if (ret != LDAP_SUCCESS) {
  2262. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2263. "dna_update_shared_config: Unable to update shared config entry: %s [error %d]\n",
  2264. config_entry->shared_cfg_dn, ret);
  2265. }
  2266. slapi_pblock_destroy(pb);
  2267. pb = NULL;
  2268. }
  2269. }
  2270. return ret;
  2271. }
  2272. /*
  2273. * dna_update_next_range()
  2274. *
  2275. * Sets the proper value for the next range in
  2276. * all configuration entries and in-memory cache.
  2277. *
  2278. * The range you are updating should be locked
  2279. * before calling this function.
  2280. */
  2281. static int
  2282. dna_update_next_range(struct configEntry *config_entry,
  2283. PRUint64 lower, PRUint64 upper)
  2284. {
  2285. Slapi_PBlock *pb = NULL;
  2286. LDAPMod mod_replace;
  2287. LDAPMod *mods[2];
  2288. char *replace_val[2];
  2289. /* 32 for the two numbers, 1 for the '-', and one for the '\0' */
  2290. char nextrange_value[34];
  2291. int ret = 0;
  2292. /* Try to set the new next range in the config entry. */
  2293. PR_snprintf(nextrange_value, sizeof(nextrange_value), "%" NSPRIu64 "-%" NSPRIu64,
  2294. lower, upper);
  2295. /* set up our replace modify operation */
  2296. replace_val[0] = nextrange_value;
  2297. replace_val[1] = 0;
  2298. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2299. mod_replace.mod_type = DNA_NEXT_RANGE;
  2300. mod_replace.mod_values = replace_val;
  2301. mods[0] = &mod_replace;
  2302. mods[1] = 0;
  2303. pb = slapi_pblock_new();
  2304. if (NULL == pb) {
  2305. ret = LDAP_OPERATIONS_ERROR;
  2306. goto bail;
  2307. }
  2308. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2309. mods, 0, 0, getPluginID(), 0);
  2310. slapi_modify_internal_pb(pb);
  2311. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2312. slapi_pblock_destroy(pb);
  2313. pb = NULL;
  2314. if (ret != LDAP_SUCCESS) {
  2315. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2316. "dna_update_next_range: Error updating "
  2317. "configuration entry [err=%d]\n", ret);
  2318. } else {
  2319. /* update the cached config and the shared config */
  2320. config_entry->next_range_lower = lower;
  2321. config_entry->next_range_upper = upper;
  2322. dna_notice_allocation(config_entry, 0, 0);
  2323. }
  2324. bail:
  2325. return ret;
  2326. }
  2327. /* dna_activate_next_range()
  2328. *
  2329. * Makes the next range the active range. This
  2330. * will update the config entry, the in-memory
  2331. * config info, and the shared config entry.
  2332. *
  2333. * The lock for configEntry should
  2334. * be obtained before calling this function.
  2335. */
  2336. static int
  2337. dna_activate_next_range(struct configEntry *config_entry)
  2338. {
  2339. Slapi_PBlock *pb = NULL;
  2340. LDAPMod mod_maxval;
  2341. LDAPMod mod_nextval;
  2342. LDAPMod mod_nextrange;
  2343. LDAPMod *mods[4];
  2344. char *maxval_vals[2];
  2345. char *nextval_vals[2];
  2346. char *nextrange_vals[1];
  2347. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2348. char maxval_val[17];
  2349. char nextval_val[17];
  2350. int ret = 0;
  2351. /* Setup the modify operation for the config entry */
  2352. PR_snprintf(maxval_val, sizeof(maxval_val),"%" NSPRIu64, config_entry->next_range_upper);
  2353. PR_snprintf(nextval_val, sizeof(nextval_val),"%" NSPRIu64, config_entry->next_range_lower);
  2354. maxval_vals[0] = maxval_val;
  2355. maxval_vals[1] = 0;
  2356. nextval_vals[0] = nextval_val;
  2357. nextval_vals[1] = 0;
  2358. nextrange_vals[0] = 0;
  2359. mod_maxval.mod_op = LDAP_MOD_REPLACE;
  2360. mod_maxval.mod_type = DNA_MAXVAL;
  2361. mod_maxval.mod_values = maxval_vals;
  2362. mod_nextval.mod_op = LDAP_MOD_REPLACE;
  2363. mod_nextval.mod_type = DNA_NEXTVAL;
  2364. mod_nextval.mod_values = nextval_vals;
  2365. mod_nextrange.mod_op = LDAP_MOD_DELETE;
  2366. mod_nextrange.mod_type = DNA_NEXT_RANGE;
  2367. mod_nextrange.mod_values = nextrange_vals;
  2368. mods[0] = &mod_maxval;
  2369. mods[1] = &mod_nextval;
  2370. mods[2] = &mod_nextrange;
  2371. mods[3] = 0;
  2372. /* Update the config entry first */
  2373. pb = slapi_pblock_new();
  2374. if (NULL == pb) {
  2375. ret = LDAP_OPERATIONS_ERROR;
  2376. goto bail;
  2377. }
  2378. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2379. mods, 0, 0, getPluginID(), 0);
  2380. slapi_modify_internal_pb(pb);
  2381. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2382. slapi_pblock_destroy(pb);
  2383. pb = NULL;
  2384. if (ret != LDAP_SUCCESS) {
  2385. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2386. "dna_activate_next_range: Error updating "
  2387. "configuration entry [err=%d]\n", ret);
  2388. } else {
  2389. /* Update the in-memory config info */
  2390. config_entry->maxval = config_entry->next_range_upper;
  2391. config_entry->nextval = config_entry->next_range_lower;
  2392. config_entry->next_range_upper = 0;
  2393. config_entry->next_range_lower = 0;
  2394. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  2395. config_entry->interval);
  2396. /* update the shared configuration */
  2397. dna_update_shared_config(config_entry);
  2398. }
  2399. bail:
  2400. return ret;
  2401. }
  2402. /*
  2403. * dna_is_replica_bind_dn()
  2404. *
  2405. * Checks if the passed in DN is the replica bind DN. This
  2406. * is used to check if a user is allowed to request range
  2407. * from us.
  2408. *
  2409. * Returns 1 if bind_dn matches the replica bind dn, 0 otherwise. */
  2410. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn)
  2411. {
  2412. char *replica_dn = NULL;
  2413. Slapi_DN *replica_sdn = NULL;
  2414. Slapi_DN *range_sdn = NULL;
  2415. Slapi_Entry *e = NULL;
  2416. char *attrs[3];
  2417. Slapi_Backend *be = NULL;
  2418. const char *be_suffix = NULL;
  2419. int ret = 0;
  2420. /* Find the backend suffix where the shared config is stored. */
  2421. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2422. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2423. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2424. }
  2425. /* Fetch the "cn=replica" entry for the backend that stores
  2426. * the shared config. We need to see what the configured
  2427. * replica bind DN is or if a bind DN group is defined. */
  2428. if (be_suffix) {
  2429. /* This function converts the old DN style to the new one. */
  2430. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2431. if (NULL == replica_dn) {
  2432. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2433. "dna_is_replica_bind_dn: failed to create "
  2434. "replica dn for %s\n", be_suffix);
  2435. ret = 1;
  2436. goto done;
  2437. }
  2438. replica_sdn = slapi_sdn_new_normdn_passin(replica_dn);
  2439. attrs[0] = DNA_REPL_BIND_DN;
  2440. attrs[1] = DNA_REPL_BIND_DNGROUP;
  2441. attrs[2] = 0;
  2442. /* Find cn=replica entry via search */
  2443. slapi_search_internal_get_entry(replica_sdn, attrs, &e, getPluginID());
  2444. if (e) {
  2445. /* Check if the passed in bind dn matches any of the replica bind dns. */
  2446. Slapi_Value *bind_dn_sv = slapi_value_new_string(bind_dn);
  2447. ret = slapi_entry_attr_has_syntax_value(e, DNA_REPL_BIND_DN, bind_dn_sv);
  2448. if (ret == 0) {
  2449. /* check if binddn is member of binddn group */
  2450. int i = 0;
  2451. Slapi_DN *bind_group_sdn = NULL;
  2452. Slapi_Entry *bind_group_entry = NULL;
  2453. char **bind_group_dn = slapi_entry_attr_get_charray(e, DNA_REPL_BIND_DNGROUP);
  2454. attrs[0] = "member";
  2455. attrs[1] = "uniquemember";
  2456. attrs[2] = 0;
  2457. for (i=0; bind_group_dn != NULL && bind_group_dn[i] != NULL; i++) {
  2458. if (ret) {
  2459. /* already found a member, just free group */
  2460. slapi_ch_free_string(&bind_group_dn[i]);
  2461. continue;
  2462. }
  2463. bind_group_sdn = slapi_sdn_new_normdn_passin(bind_group_dn[i]);
  2464. slapi_search_internal_get_entry(bind_group_sdn, attrs, &bind_group_entry, getPluginID());
  2465. if (bind_group_entry) {
  2466. ret = slapi_entry_attr_has_syntax_value(bind_group_entry, "member", bind_dn_sv);
  2467. if (ret == 0) {
  2468. ret = slapi_entry_attr_has_syntax_value(bind_group_entry, "uniquemember", bind_dn_sv);
  2469. }
  2470. }
  2471. slapi_entry_free(bind_group_entry);
  2472. slapi_sdn_free(&bind_group_sdn);
  2473. }
  2474. slapi_ch_free((void **) &bind_group_dn);
  2475. }
  2476. slapi_value_free(&bind_dn_sv);
  2477. } else {
  2478. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2479. "dna_is_replica_bind_dn: Failed to fetch replica entry "
  2480. "for range %s\n", range_dn);
  2481. }
  2482. }
  2483. done:
  2484. slapi_entry_free(e);
  2485. slapi_sdn_free(&range_sdn);
  2486. slapi_sdn_free(&replica_sdn);
  2487. return ret;
  2488. }
  2489. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  2490. char **bind_dn, char **bind_passwd,
  2491. char **bind_method, int *is_ssl, int *port)
  2492. {
  2493. Slapi_PBlock *pb = NULL;
  2494. Slapi_DN *range_sdn = NULL;
  2495. char *replica_dn = NULL;
  2496. Slapi_Backend *be = NULL;
  2497. const char *be_suffix = NULL;
  2498. char *attrs[6];
  2499. char *filter = NULL;
  2500. char *bind_cred = NULL;
  2501. char *transport = NULL;
  2502. Slapi_Entry **entries = NULL;
  2503. int ret = LDAP_OPERATIONS_ERROR;
  2504. /* Find the backend suffix where the shared config is stored. */
  2505. range_sdn = slapi_sdn_new_normdn_byref(range_dn);
  2506. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2507. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2508. }
  2509. /* Fetch the replication agreement entry */
  2510. if (be_suffix) {
  2511. /* This function converts the old DN style to the new one. */
  2512. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2513. if (NULL == replica_dn) {
  2514. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2515. "dna_get_replica_bind_creds: failed to create "
  2516. "replica dn for %s\n", be_suffix);
  2517. ret = LDAP_PARAM_ERROR;
  2518. goto bail;
  2519. }
  2520. filter = slapi_ch_smprintf("(&(nsds5ReplicaHost=%s)(|(" DNA_REPL_PORT "=%u)"
  2521. "(" DNA_REPL_PORT "=%u)))",
  2522. server->host, server->port, server->secureport);
  2523. attrs[0] = DNA_REPL_BIND_DN;
  2524. attrs[1] = DNA_REPL_CREDS;
  2525. attrs[2] = DNA_REPL_BIND_METHOD;
  2526. attrs[3] = DNA_REPL_TRANSPORT;
  2527. attrs[4] = DNA_REPL_PORT;
  2528. attrs[5] = 0;
  2529. pb = slapi_pblock_new();
  2530. if (NULL == pb) {
  2531. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2532. "dna_get_replica_bind_creds: Failed to "
  2533. "allocate pblock\n");
  2534. goto bail;
  2535. }
  2536. slapi_search_internal_set_pb(pb, replica_dn,
  2537. LDAP_SCOPE_ONELEVEL, filter,
  2538. attrs, 0, NULL, NULL, getPluginID(), 0);
  2539. slapi_search_internal_pb(pb);
  2540. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2541. if (LDAP_SUCCESS != ret) {
  2542. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2543. "dna_get_replica_bind_creds: Failed to fetch replica "
  2544. "bind credentials for range %s, server %s, port %u [error %d]\n",
  2545. range_dn, server->host,
  2546. server->port ? server->port : server->secureport, ret);
  2547. goto bail;
  2548. }
  2549. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2550. &entries);
  2551. if (NULL == entries || NULL == entries[0]) {
  2552. if(server->remote_defined){
  2553. /*
  2554. * Ok there are no replication agreements for this shared server, but we
  2555. * do have custom defined authentication settings we can use.
  2556. */
  2557. ret = dna_get_remote_config_info(server, bind_dn, bind_passwd, bind_method, is_ssl, port);
  2558. goto bail;
  2559. }
  2560. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2561. "dna_get_replica_bind_creds: Failed to fetch replication "
  2562. "agreement for range %s, server %s, port %u\n", range_dn,
  2563. server->host, server->port ? server->port : server->secureport);
  2564. ret = LDAP_OPERATIONS_ERROR;
  2565. goto bail;
  2566. }
  2567. /* Get the replication bind dn and password from the agreement. It
  2568. * is up to the caller to free these when they are finished. */
  2569. slapi_ch_free_string(bind_dn);
  2570. slapi_ch_free_string(bind_method);
  2571. *bind_dn = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_DN);
  2572. *bind_method = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_METHOD);
  2573. bind_cred = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_CREDS);
  2574. transport = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_TRANSPORT);
  2575. *port = slapi_entry_attr_get_int(entries[0], DNA_REPL_PORT);
  2576. /* Check if we should use SSL */
  2577. if (transport && (strcasecmp(transport, "SSL") == 0)) {
  2578. *is_ssl = 1;
  2579. } else if (transport && (strcasecmp(transport, "TLS") == 0)) {
  2580. *is_ssl = 2;
  2581. } else {
  2582. *is_ssl = 0;
  2583. }
  2584. /* fix up the bind method */
  2585. if ((NULL == *bind_method) || (strcasecmp(*bind_method, "SIMPLE") == 0)) {
  2586. slapi_ch_free_string(bind_method);
  2587. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2588. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2589. slapi_ch_free_string(bind_method);
  2590. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2591. } else if (strcasecmp(*bind_method, "SASL/GSSAPI") == 0) {
  2592. slapi_ch_free_string(bind_method);
  2593. *bind_method = slapi_ch_strdup("GSSAPI");
  2594. } else if (strcasecmp(*bind_method, "SASL/DIGEST-MD5") == 0) {
  2595. slapi_ch_free_string(bind_method);
  2596. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2597. } else { /* some other weird value */
  2598. ; /* just use it directly */
  2599. }
  2600. /* Decode the password */
  2601. if (bind_cred) {
  2602. int pw_ret = 0;
  2603. slapi_ch_free_string(bind_passwd);
  2604. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2605. if (pw_ret == -1) {
  2606. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2607. "dna_get_replica_bind_creds: Failed to decode "
  2608. "replica bind credentials for range %s, server %s, "
  2609. "port %u\n", range_dn, server->host, server->port);
  2610. goto bail;
  2611. } else if (pw_ret != 0) {
  2612. /* The password was already in clear text, so pw_rever_decode
  2613. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2614. * to prevent a double free. The memory is now owned by
  2615. * bind_passwd, which is the callers responsibility to free. */
  2616. bind_cred = NULL;
  2617. }
  2618. }
  2619. }
  2620. /* If we got here, we succesfully got the
  2621. * creds. Set the success return value. */
  2622. ret = 0;
  2623. bail:
  2624. slapi_ch_free_string(&transport);
  2625. slapi_ch_free_string(&filter);
  2626. slapi_sdn_free(&range_sdn);
  2627. slapi_ch_free_string(&replica_dn);
  2628. slapi_ch_free_string(&bind_cred);
  2629. slapi_free_search_results_internal(pb);
  2630. slapi_pblock_destroy(pb);
  2631. return ret;
  2632. }
  2633. static int
  2634. dna_get_remote_config_info( struct dnaServer *server, char **bind_dn, char **bind_passwd,
  2635. char **bind_method, int *is_ssl, int *port)
  2636. {
  2637. int rc = 0;
  2638. /* populate the bind info */
  2639. slapi_ch_free_string(bind_dn);
  2640. slapi_ch_free_string(bind_method);
  2641. *bind_dn = slapi_ch_strdup(server->remote_binddn);
  2642. *bind_method = slapi_ch_strdup(server->remote_bind_method);
  2643. /* fix up the bind method */
  2644. if ((NULL == *bind_method) || (strcasecmp(*bind_method, DNA_METHOD_SIMPLE) == 0)) {
  2645. slapi_ch_free_string(bind_method);
  2646. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2647. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2648. slapi_ch_free_string(bind_method);
  2649. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2650. } else if (strcasecmp(*bind_method, DNA_METHOD_GSSAPI) == 0) {
  2651. slapi_ch_free_string(bind_method);
  2652. *bind_method = slapi_ch_strdup("GSSAPI");
  2653. } else if (strcasecmp(*bind_method, DNA_METHOD_DIGESTMD5) == 0) {
  2654. slapi_ch_free_string(bind_method);
  2655. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2656. } else { /* some other weird value */
  2657. ; /* just use it directly */
  2658. }
  2659. if(server->remote_conn_prot && strcasecmp(server->remote_conn_prot, DNA_PROT_SSL) == 0){
  2660. *is_ssl = 1;
  2661. } else if(server->remote_conn_prot && strcasecmp(server->remote_conn_prot, DNA_PROT_TLS) == 0){
  2662. *is_ssl = 2;
  2663. } else {
  2664. *is_ssl = 0;
  2665. }
  2666. if(*is_ssl == 1){ /* SSL(covers TLS over ssl) */
  2667. if (server->secureport){
  2668. *port = server->secureport;
  2669. } else {
  2670. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2671. "dna_get_remote_config_info: Using SSL protocol, but the secure "
  2672. "port is not defined.\n");
  2673. return -1;
  2674. }
  2675. } else { /* LDAP/TLS(non secure port) */
  2676. if(server->port){
  2677. *port = server->port;
  2678. } else {
  2679. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2680. "dna_get_remote_config_info: Using %s protocol, but the non-secure "
  2681. "port is not defined.\n", server->remote_conn_prot);
  2682. return -1;
  2683. }
  2684. }
  2685. /* Decode the password */
  2686. if (server->remote_bindpw) {
  2687. char *bind_cred = slapi_ch_strdup(server->remote_bindpw);
  2688. int pw_ret = 0;
  2689. slapi_ch_free_string(bind_passwd);
  2690. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2691. if (pw_ret == -1) {
  2692. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2693. "dna_get_remote_config_info: Failed to decode "
  2694. "replica bind credentials for server %s, "
  2695. "port %u\n", server->host,
  2696. server->port ? server->port : server->secureport);
  2697. rc = -1;
  2698. } else if (pw_ret != 0) {
  2699. /*
  2700. * The password was already in clear text, so pw_rever_decode
  2701. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2702. * to prevent a double free. The memory is now owned by
  2703. * bind_passwd, which is the callers responsibility to free.
  2704. */
  2705. bind_cred = NULL;
  2706. }
  2707. slapi_ch_free_string(&bind_cred);
  2708. }
  2709. return rc;
  2710. }
  2711. /*
  2712. * dna_list_contains_type()
  2713. *
  2714. * Checks if a type is contained in a list of types.
  2715. * Returns 1 if the type is found, 0 otherwise.
  2716. */
  2717. static int
  2718. dna_list_contains_type(char **list, char *type)
  2719. {
  2720. int ret = 0;
  2721. int i = 0;
  2722. if (list && type) {
  2723. for (i = 0; list[i]; i++) {
  2724. if (slapi_attr_types_equivalent(type, list[i])) {
  2725. ret = 1;
  2726. break;
  2727. }
  2728. }
  2729. }
  2730. return ret;
  2731. }
  2732. /*
  2733. * dna_list_contains_types()
  2734. *
  2735. * Checks if all types in one list (types) are contained
  2736. * in another list of types (list). Returns 1 if all
  2737. * types are found, 0 otherwise.
  2738. */
  2739. static int
  2740. dna_list_contains_types(char **list, char **types)
  2741. {
  2742. int ret = 1;
  2743. int i = 0;
  2744. int j = 0;
  2745. if (list && types) {
  2746. for (i = 0; types[i]; i++) {
  2747. int found = 0;
  2748. for (j = 0; list[j]; j++) {
  2749. if (slapi_attr_types_equivalent(types[i], list[i])) {
  2750. found = 1;
  2751. break;
  2752. }
  2753. }
  2754. if (!found) {
  2755. ret = 0;
  2756. break;
  2757. }
  2758. }
  2759. } else {
  2760. ret = 0;
  2761. }
  2762. return ret;
  2763. }
  2764. /*
  2765. * dna_list_remove_type()
  2766. *
  2767. * Removes a type from a list of types.
  2768. */
  2769. static void
  2770. dna_list_remove_type(char **list, char *type)
  2771. {
  2772. int i = 0;
  2773. int found_type = 0;
  2774. if (list && type) {
  2775. /* Go through the list until we find the type that
  2776. * we want to remove. We simply free the type we
  2777. * want to remove and shift the remaining array
  2778. * elements down by one index. This will leave us
  2779. * with two NULL elements at the end of the list,
  2780. * but this should not cause any problems. */
  2781. for (i = 0; list[i]; i++) {
  2782. if (found_type) {
  2783. list[i] = list[i + 1];
  2784. } else if (slapi_attr_types_equivalent(type, list[i])) {
  2785. slapi_ch_free_string(&list[i]);
  2786. list[i] = list[i + 1];
  2787. found_type = 1;
  2788. }
  2789. }
  2790. }
  2791. }
  2792. /*
  2793. * dna_is_multitype_range()
  2794. *
  2795. * Returns 1 if the range has multiple types configured.
  2796. * Returns 0 otherwise.
  2797. */
  2798. static int dna_is_multitype_range(struct configEntry *config_entry)
  2799. {
  2800. int ret = 0;
  2801. if (config_entry && config_entry->types && config_entry->types[1]) {
  2802. ret = 1;
  2803. }
  2804. return ret;
  2805. }
  2806. /*
  2807. * dna_create_valcheck_filter()
  2808. *
  2809. * Creates a filter string used to check if a value is free. If
  2810. * filter already holds a valid pointer, it is assumed to have enough
  2811. * space to hold the filter. This allows the same memory to be used
  2812. * over and over when you only want to change the assertion value.
  2813. * If filter contains a NULL pointer, a new string of the appropriate
  2814. * size will be allocated. The caller must free this when finished.
  2815. */
  2816. static void
  2817. dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter)
  2818. {
  2819. int filterlen = 0;
  2820. int typeslen = 0;
  2821. int i = 0;
  2822. int bytes_out = 0;
  2823. int multitype = 0;
  2824. /* Just return if we didn't get an address for the filter. */
  2825. if (filter == NULL) {
  2826. return;
  2827. }
  2828. /* To determine the filter length, we add together the following:
  2829. *
  2830. * - the string length of the filter in the config
  2831. * - the string length sum of all configured types
  2832. * - 23 bytes for each type (20 for the max string
  2833. * representation of a NSPRIu64, 3 for "(=)"
  2834. * - 3 bytes for the beginning and end of the filter - "(&" and ")"
  2835. * - 3 bytes to OR together multiple types (if present) - "(|" and ")"
  2836. * - the string length of the prefix (if one is configured) for each type
  2837. * - 1 byte for the trailing \0
  2838. *
  2839. * The filter length should be the same every time if the config struct
  2840. * has not been changed. We need to calculate the filter length even
  2841. * if we are reusing memory since we have no other way of knowing the
  2842. * length used when it was originally allocated. We trust the caller
  2843. * to only reuse the pointer with the same config struct.
  2844. */
  2845. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2846. typeslen += strlen(config_entry->types[i]);
  2847. }
  2848. if (i > 1) {
  2849. multitype = 1;
  2850. }
  2851. filterlen = strlen(config_entry->filter) + typeslen +
  2852. (i * 23) + 3 + 1 +
  2853. (config_entry->prefix ? (i * strlen(config_entry->prefix)) : 0) +
  2854. (multitype ? 3 : 0);
  2855. /* Allocate space for the filter if it hasn't been allocated yet. */
  2856. if (*filter == NULL) {
  2857. *filter = slapi_ch_malloc(filterlen);
  2858. }
  2859. /* Write out the beginning of the filter. If multiple types
  2860. * are configured, we need to OR together the search clauses
  2861. * for the types. */
  2862. if (multitype) {
  2863. bytes_out = snprintf(*filter, filterlen, "(&%s(|", config_entry->filter);
  2864. } else {
  2865. bytes_out = snprintf(*filter, filterlen, "(&%s", config_entry->filter);
  2866. }
  2867. /* Loop through the types and append each filter clause. */
  2868. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2869. bytes_out += snprintf(*filter + bytes_out, filterlen - bytes_out,
  2870. "(%s=%s%" PRIu64 ")", config_entry->types[i],
  2871. config_entry->prefix ? config_entry->prefix : "",
  2872. value);
  2873. }
  2874. /* Append the end of the filter. We need an extra paren
  2875. * to close out the OR if we have multiple types. */
  2876. if (multitype) {
  2877. strncat(*filter, "))", filterlen - bytes_out);
  2878. } else {
  2879. strncat(*filter, ")", filterlen - bytes_out);
  2880. }
  2881. }
  2882. /* This function is called at BEPREOP timing to add uid/gidNumber
  2883. * if modtype is missing */
  2884. static int
  2885. _dna_pre_op_add(Slapi_PBlock *pb, Slapi_Entry *e, char **errstr)
  2886. {
  2887. int ret = DNA_SUCCESS;
  2888. PRCList *list = NULL;
  2889. struct configEntry *config_entry = NULL;
  2890. char *dn = NULL;
  2891. char *value = NULL;
  2892. char **types_to_generate = NULL;
  2893. char **generated_types = NULL;
  2894. PRUint64 setval = 0;
  2895. int i;
  2896. if (0 == (dn = dna_get_dn(pb))) {
  2897. goto bail;
  2898. }
  2899. /*
  2900. * Find the config that matches this entry, Set the types that need to be
  2901. * generated to DNA_NEEDS_UPDATE. The be_txn_preop will set the values if
  2902. * the operation hasn't been rejected by that point.
  2903. *
  2904. * We also check if we need to get the next range of values, and grab them.
  2905. * We do this here so we don't have to do it in the be_txn_preop.
  2906. */
  2907. dna_read_lock();
  2908. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2909. list = PR_LIST_HEAD(dna_global_config);
  2910. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  2911. config_entry = (struct configEntry *) list;
  2912. /* Did we already service all of these configured types? */
  2913. if (dna_list_contains_types(generated_types, config_entry->types)) {
  2914. goto next;
  2915. }
  2916. /* is the entry in scope? */
  2917. if (config_entry->scope &&
  2918. !slapi_dn_issuffix(dn, config_entry->scope)) {
  2919. goto next;
  2920. }
  2921. /* is this entry in an excluded scope? */
  2922. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  2923. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  2924. goto next;
  2925. }
  2926. }
  2927. /* does the entry match the filter? */
  2928. if (config_entry->slapi_filter) {
  2929. ret = slapi_vattr_filter_test(pb, e, config_entry->slapi_filter, 0);
  2930. if (LDAP_SUCCESS != ret) {
  2931. goto next;
  2932. }
  2933. }
  2934. if (dna_is_multitype_range(config_entry)) {
  2935. /* For a multi-type range, we only generate a value
  2936. * for types where the magic value is set. We do not
  2937. * generate a value for missing types. */
  2938. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2939. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  2940. if (value){
  2941. if(config_entry->generate == NULL || !slapi_UTF8CASECMP(config_entry->generate, value)){
  2942. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[i]));
  2943. }
  2944. slapi_ch_free_string(&value);
  2945. }
  2946. }
  2947. } else {
  2948. /* For a single type range, we generate the value if
  2949. * the magic value is set or if the type is missing. */
  2950. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  2951. if((config_entry->generate == NULL) || (0 == value) ||
  2952. (value && !slapi_UTF8CASECMP(config_entry->generate, value))){
  2953. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[0]));
  2954. }
  2955. slapi_ch_free_string(&value);
  2956. }
  2957. if (types_to_generate && types_to_generate[0]) {
  2958. /* add - add to entry */
  2959. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2960. slapi_entry_attr_set_charptr(e, types_to_generate[i],
  2961. /* no need to dup */
  2962. DNA_NEEDS_UPDATE);
  2963. }
  2964. /* Update the internalModifiersname for this add op */
  2965. add_internal_modifiersname(pb, e);
  2966. /* Make sure we don't generate for this
  2967. * type again by keeping a list of types
  2968. * we have generated for already.
  2969. */
  2970. if (generated_types == NULL) {
  2971. /* If we don't have a list of generated types yet,
  2972. * we can just use the types_to_generate list so
  2973. * we don't have to allocate anything. */
  2974. generated_types = types_to_generate;
  2975. types_to_generate = NULL;
  2976. } else {
  2977. /* Just reuse the elements out of types_to_generate for the
  2978. * generated types list to avoid allocating them again. */
  2979. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  2980. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  2981. types_to_generate[i] = NULL;
  2982. }
  2983. }
  2984. /* free up */
  2985. slapi_ch_array_free(types_to_generate);
  2986. types_to_generate = NULL;
  2987. /*
  2988. * Now grab the next value and see if we need to get the next range
  2989. */
  2990. slapi_lock_mutex(config_entry->lock);
  2991. ret = dna_first_free_value(config_entry, &setval);
  2992. if (LDAP_SUCCESS != ret) {
  2993. /* check if we overflowed the configured range */
  2994. if (setval > config_entry->maxval) {
  2995. /* try for a new range or fail */
  2996. ret = dna_fix_maxval(config_entry, 0);
  2997. if (LDAP_SUCCESS != ret) {
  2998. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2999. "dna_pre_op: no more values available!!\n");
  3000. /* Set an error string to be returned to the client. */
  3001. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3002. " %s failed! Unable to proceed.",
  3003. config_entry->dn);
  3004. slapi_unlock_mutex(config_entry->lock);
  3005. break;
  3006. }
  3007. /* Make sure dna_first_free_value() doesn't error out */
  3008. ret = dna_first_free_value(config_entry, &setval);
  3009. if (LDAP_SUCCESS != ret){
  3010. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3011. "dna_pre_op: failed to allocate a new ID 1\n");
  3012. /* Set an error string to be returned to the client. */
  3013. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3014. " %s failed! Unable to proceed.",
  3015. config_entry->dn);
  3016. slapi_unlock_mutex(config_entry->lock);
  3017. break;
  3018. }
  3019. } else {
  3020. /* dna_first_free_value() failed for some unknown reason */
  3021. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3022. "dna_pre_op: failed to allocate a new ID!! 2\n");
  3023. /* Set an error string to be returned to the client. */
  3024. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3025. " %s failed! Unable to proceed.",
  3026. config_entry->dn);
  3027. slapi_unlock_mutex(config_entry->lock);
  3028. break;
  3029. }
  3030. }
  3031. /* Check if we passed the threshold and try to fix maxval if so.
  3032. * We don't need to do this if we already have a next range on
  3033. * deck. We don't check the result of dna_fix_maxval() since
  3034. * we aren't completely out of values yet. Any failure here is
  3035. * really a soft failure. */
  3036. if ((config_entry->next_range_lower == 0) &&
  3037. (config_entry->remaining <= config_entry->threshold)) {
  3038. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3039. "dna_pre_op: Passed threshold of %"
  3040. NSPRIu64 " remaining values "
  3041. "for range %s. (%" NSPRIu64 " values remain)\n",
  3042. config_entry->threshold, config_entry->dn,
  3043. config_entry->remaining);
  3044. dna_fix_maxval(config_entry, 0);
  3045. }
  3046. slapi_unlock_mutex(config_entry->lock);
  3047. } else if (types_to_generate) {
  3048. slapi_ch_free((void **)&types_to_generate);
  3049. }
  3050. next:
  3051. ret = DNA_SUCCESS;
  3052. list = PR_NEXT_LINK(list);
  3053. }
  3054. }
  3055. dna_unlock();
  3056. slapi_ch_array_free(generated_types);
  3057. bail:
  3058. return ret;
  3059. }
  3060. /* This function is called at BEPREOP timing to add uid/gidNumber
  3061. * if modtype is missing */
  3062. static int
  3063. _dna_pre_op_modify(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Mods *smods, char **errstr)
  3064. {
  3065. int ret = DNA_SUCCESS;
  3066. PRCList *list = NULL;
  3067. struct configEntry *config_entry = NULL;
  3068. char *dn = NULL;
  3069. char *value = NULL;
  3070. Slapi_Mod *next_mod = NULL;
  3071. Slapi_Mod *smod = NULL;
  3072. Slapi_Attr *attr = NULL;
  3073. char *type = NULL;
  3074. int e_numvals = 0;
  3075. int numvals = 0;
  3076. struct berval *bv = NULL;
  3077. char **types_to_generate = NULL;
  3078. char **generated_types = NULL;
  3079. PRUint64 setval = 0;
  3080. int len = 0;
  3081. int i;
  3082. if (0 == (dn = dna_get_dn(pb))) {
  3083. goto bail;
  3084. }
  3085. /*
  3086. * Find the config that matches this entry, Set the types that need to be
  3087. * generated to DNA_NEEDS_UPDATE. The be_txn_preop will set the values if
  3088. * the operation hasn't been rejected by that point.
  3089. *
  3090. * We also check if we need to get the next range of values, and grab them.
  3091. * We do this here so we don't have to do it in the be_txn_preop.
  3092. */
  3093. dna_read_lock();
  3094. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3095. list = PR_LIST_HEAD(dna_global_config);
  3096. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  3097. config_entry = (struct configEntry *) list;
  3098. /* Did we already service all of these configured types? */
  3099. if (dna_list_contains_types(generated_types, config_entry->types)) {
  3100. goto next;
  3101. }
  3102. /* is the entry in scope? */
  3103. if (config_entry->scope &&
  3104. !slapi_dn_issuffix(dn, config_entry->scope)) {
  3105. goto next;
  3106. }
  3107. /* is this entry in an excluded scope? */
  3108. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  3109. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  3110. goto next;
  3111. }
  3112. }
  3113. /* does the entry match the filter? */
  3114. if (config_entry->slapi_filter) {
  3115. ret = slapi_vattr_filter_test(pb, e,
  3116. config_entry->slapi_filter, 0);
  3117. if (LDAP_SUCCESS != ret) {
  3118. goto next;
  3119. }
  3120. }
  3121. /* check mods for magic value */
  3122. next_mod = slapi_mod_new();
  3123. smod = slapi_mods_get_first_smod(smods, next_mod);
  3124. while (smod) {
  3125. type = (char *)slapi_mod_get_type(smod);
  3126. /* See if the type matches any configured type. */
  3127. if (dna_list_contains_type(config_entry->types, type)) {
  3128. /* If all values are being deleted, we need to
  3129. * generate a new value. We don't do this for
  3130. * multi-type ranges since they require the magic
  3131. * value to be specified to trigger generation. */
  3132. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  3133. !dna_is_multitype_range(config_entry)) {
  3134. numvals = slapi_mod_get_num_values(smod);
  3135. if (numvals == 0) {
  3136. slapi_ch_array_add(&types_to_generate,
  3137. slapi_ch_strdup(type));
  3138. } else {
  3139. e_numvals = 0;
  3140. slapi_entry_attr_find(e, type, &attr);
  3141. if (attr) {
  3142. slapi_attr_get_numvalues(attr, &e_numvals);
  3143. if (numvals >= e_numvals) {
  3144. slapi_ch_array_add(&types_to_generate,
  3145. slapi_ch_strdup(type));
  3146. }
  3147. }
  3148. }
  3149. } else {
  3150. /* This is either adding or replacing a value */
  3151. bv = slapi_mod_get_first_value(smod);
  3152. /* If this type is already in the to be generated
  3153. * list, a previous mod in this same modify operation
  3154. * either removed all values or set the magic value.
  3155. * It's possible that this mod is adding a valid value,
  3156. * which means we would not want to generate a new value.
  3157. * It is safe to remove this type from the to be
  3158. * generated list since it will be re-added here if
  3159. * necessary. */
  3160. if (dna_list_contains_type(types_to_generate, type)) {
  3161. dna_list_remove_type(types_to_generate, type);
  3162. }
  3163. /* If we have a value, see if it's the magic value. */
  3164. if (bv) {
  3165. if(config_entry->generate == NULL){
  3166. /* we don't have a magic number set, so apply the update */
  3167. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3168. } else {
  3169. len = strlen(config_entry->generate);
  3170. if (len == bv->bv_len) {
  3171. if (!slapi_UTF8NCASECMP(bv->bv_val, config_entry->generate,len)){
  3172. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3173. }
  3174. }
  3175. }
  3176. } else if (!dna_is_multitype_range(config_entry)) {
  3177. /* This is a replace with no new values,
  3178. * so we need to generate a new value if this
  3179. * is not a multi-type range. */
  3180. slapi_ch_array_add(&types_to_generate,slapi_ch_strdup(type));
  3181. }
  3182. }
  3183. }
  3184. slapi_mod_done(next_mod);
  3185. smod = slapi_mods_get_next_smod(smods, next_mod);
  3186. }
  3187. slapi_mod_free(&next_mod);
  3188. /* We need to perform one last check for modify operations. If an
  3189. * entry within the scope has not triggered generation yet, we need
  3190. * to see if a value exists for the managed type in the resulting
  3191. * entry. This will catch a modify operation that brings an entry
  3192. * into scope for a managed range, but doesn't supply a value for
  3193. * the managed type. We don't do this for multi-type ranges. */
  3194. if ((!types_to_generate ||
  3195. (types_to_generate && !types_to_generate[0])) &&
  3196. !dna_is_multitype_range(config_entry)) {
  3197. if (slapi_entry_attr_find(e, config_entry->types[0], &attr)
  3198. != 0) {
  3199. slapi_ch_array_add(&types_to_generate,
  3200. slapi_ch_strdup(config_entry->types[0]));
  3201. }
  3202. }
  3203. if (types_to_generate && types_to_generate[0]) {
  3204. /* mod - add to mods */
  3205. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3206. slapi_mods_add_string(smods, LDAP_MOD_REPLACE,
  3207. types_to_generate[i],
  3208. /* no need to dup */
  3209. DNA_NEEDS_UPDATE);
  3210. }
  3211. /* Update the internalModifersname for this mod op */
  3212. modify_update_last_modified_attr(pb, smods);
  3213. /* Make sure we don't generate for this
  3214. * type again by keeping a list of types
  3215. * we have generated for already.
  3216. */
  3217. if (generated_types == NULL) {
  3218. /* If we don't have a list of generated types yet,
  3219. * we can just use the types_to_generate list so
  3220. * we don't have to allocate anything. */
  3221. generated_types = types_to_generate;
  3222. types_to_generate = NULL;
  3223. } else {
  3224. /* Just reuse the elements out of types_to_generate for the
  3225. * generated types list to avoid allocating them again. */
  3226. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  3227. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  3228. types_to_generate[i] = NULL;
  3229. }
  3230. }
  3231. /* free up */
  3232. slapi_ch_free_string(&value);
  3233. slapi_ch_array_free(types_to_generate);
  3234. types_to_generate = NULL;
  3235. /*
  3236. * Now grab the next value and see if we need to get the next range
  3237. */
  3238. slapi_lock_mutex(config_entry->lock);
  3239. ret = dna_first_free_value(config_entry, &setval);
  3240. if (LDAP_SUCCESS != ret) {
  3241. /* check if we overflowed the configured range */
  3242. if (setval > config_entry->maxval) {
  3243. /* try for a new range or fail */
  3244. ret = dna_fix_maxval(config_entry, 0);
  3245. if (LDAP_SUCCESS != ret) {
  3246. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3247. "dna_pre_op: no more values available!!\n");
  3248. /* Set an error string to be returned to the client. */
  3249. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3250. " %s failed! Unable to proceed.",
  3251. config_entry->dn);
  3252. slapi_unlock_mutex(config_entry->lock);
  3253. break;
  3254. }
  3255. /* Make sure dna_first_free_value() doesn't error out */
  3256. ret = dna_first_free_value(config_entry, &setval);
  3257. if (LDAP_SUCCESS != ret){
  3258. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3259. "dna_pre_op: failed to allocate a new ID\n");
  3260. /* Set an error string to be returned to the client. */
  3261. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3262. " %s failed! Unable to proceed.",
  3263. config_entry->dn);
  3264. slapi_unlock_mutex(config_entry->lock);
  3265. break;
  3266. }
  3267. } else {
  3268. /* dna_first_free_value() failed for some unknown reason */
  3269. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3270. "dna_pre_op: failed to allocate a new ID!!\n");
  3271. /* Set an error string to be returned to the client. */
  3272. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3273. " %s failed! Unable to proceed.",
  3274. config_entry->dn);
  3275. slapi_unlock_mutex(config_entry->lock);
  3276. break;
  3277. }
  3278. }
  3279. /* Check if we passed the threshold and try to fix maxval if so.
  3280. * We don't need to do this if we already have a next range on
  3281. * deck. We don't check the result of dna_fix_maxval() since
  3282. * we aren't completely out of values yet. Any failure here is
  3283. * really a soft failure. */
  3284. if ((config_entry->next_range_lower == 0) &&
  3285. (config_entry->remaining <= config_entry->threshold)) {
  3286. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3287. "dna_pre_op: Passed threshold of %"
  3288. NSPRIu64 " remaining values "
  3289. "for range %s. (%" NSPRIu64 " values remain)\n",
  3290. config_entry->threshold, config_entry->dn,
  3291. config_entry->remaining);
  3292. dna_fix_maxval(config_entry, 0);
  3293. }
  3294. slapi_unlock_mutex(config_entry->lock);
  3295. } else if (types_to_generate) {
  3296. slapi_ch_free((void **)&types_to_generate);
  3297. }
  3298. next:
  3299. ret = 0;
  3300. list = PR_NEXT_LINK(list);
  3301. }
  3302. }
  3303. dna_unlock();
  3304. slapi_ch_array_free(generated_types);
  3305. bail:
  3306. return ret;
  3307. }
  3308. /* for mods and adds:
  3309. where dn's are supplied, the closest in scope
  3310. is used as long as the type filter matches
  3311. and the type has not been generated yet.
  3312. */
  3313. static int
  3314. dna_pre_op(Slapi_PBlock * pb, int modtype)
  3315. {
  3316. struct slapi_entry *e = NULL;
  3317. Slapi_Entry *test_e = NULL;
  3318. Slapi_Entry *resulting_e = NULL;
  3319. char *errstr = NULL;
  3320. char *dn = NULL;
  3321. Slapi_Mods *smods = NULL;
  3322. LDAPMod **mods;
  3323. int ret = 0;
  3324. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3325. "--> dna_pre_op\n");
  3326. if (0 == (dn = dna_get_dn(pb))) {
  3327. goto bail;
  3328. }
  3329. if (dna_isrepl(pb)) {
  3330. /* if repl, the dna values should be already in the entry. */
  3331. goto bail;
  3332. }
  3333. if (LDAP_CHANGETYPE_ADD == modtype) {
  3334. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  3335. if (NULL == e) {
  3336. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3337. "dna_pre_op: no add entry set for add\n");
  3338. goto bail;
  3339. }
  3340. } else {
  3341. slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  3342. if (NULL == e) {
  3343. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3344. "dna_pre_op: no pre op entry set for modify\n");
  3345. goto bail;
  3346. }
  3347. /* grab the mods - we'll put them back later with our modifications appended */
  3348. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  3349. /* We need the resulting entry after the mods are applied to
  3350. * see if the entry is within the scope. */
  3351. resulting_e = slapi_entry_dup(e);
  3352. if (mods &&
  3353. (slapi_entry_apply_mods(resulting_e, mods) != LDAP_SUCCESS)) {
  3354. /* The mods don't apply cleanly, so we just let this op go
  3355. * to let the main server handle it. */
  3356. goto bail;
  3357. }
  3358. smods = slapi_mods_new();
  3359. slapi_mods_init_passin(smods, mods);
  3360. }
  3361. /* For a MOD, we need to check the resulting entry */
  3362. if (LDAP_CHANGETYPE_ADD == modtype) {
  3363. test_e = e;
  3364. } else {
  3365. test_e = resulting_e;
  3366. }
  3367. if (dna_dn_is_config(dn)) {
  3368. /* Validate config changes, but don't apply them.
  3369. * This allows us to reject invalid config changes
  3370. * here at the pre-op stage. Applying the config
  3371. * needs to be done at the post-op stage. */
  3372. if (dna_parse_config_entry(pb, test_e, 0) != DNA_SUCCESS) {
  3373. /* Refuse the operation if config parsing failed. */
  3374. ret = LDAP_UNWILLING_TO_PERFORM;
  3375. if (LDAP_CHANGETYPE_ADD == modtype) {
  3376. errstr = slapi_ch_smprintf("Not a valid DNA configuration entry.");
  3377. } else {
  3378. errstr = slapi_ch_smprintf("Changes result in an invalid "
  3379. "DNA configuration.");
  3380. }
  3381. }
  3382. } else {
  3383. if (LDAP_CHANGETYPE_ADD == modtype) {
  3384. ret = _dna_pre_op_add(pb, test_e, &errstr);
  3385. } else {
  3386. if((ret = _dna_pre_op_modify(pb, test_e, smods, &errstr))){
  3387. slapi_mods_free(&smods);
  3388. }
  3389. }
  3390. if (ret) {
  3391. goto bail;
  3392. }
  3393. }
  3394. /* We're done. */
  3395. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3396. /* Put the updated mods back into place. */
  3397. mods = slapi_mods_get_ldapmods_passout(smods);
  3398. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  3399. slapi_mods_free(&smods);
  3400. }
  3401. bail:
  3402. if (resulting_e)
  3403. slapi_entry_free(resulting_e);
  3404. slapi_mods_free(&smods);
  3405. if (ret) {
  3406. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3407. "dna_pre_op: operation failure [%d]\n", ret);
  3408. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  3409. slapi_ch_free((void **)&errstr);
  3410. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  3411. ret = DNA_FAILURE;
  3412. }
  3413. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3414. "<-- dna_pre_op\n");
  3415. return ret;
  3416. }
  3417. static int dna_add_pre_op(Slapi_PBlock * pb)
  3418. {
  3419. return dna_pre_op(pb, LDAP_CHANGETYPE_ADD);
  3420. }
  3421. static int dna_mod_pre_op(Slapi_PBlock * pb)
  3422. {
  3423. return dna_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  3424. }
  3425. static int dna_be_txn_add_pre_op(Slapi_PBlock *pb)
  3426. {
  3427. return dna_be_txn_pre_op(pb, LDAP_CHANGETYPE_ADD);
  3428. }
  3429. static int dna_be_txn_mod_pre_op(Slapi_PBlock *pb)
  3430. {
  3431. return dna_be_txn_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  3432. }
  3433. /*
  3434. * dna_be_txn_pre_op()
  3435. *
  3436. * In the preop if we found that we need to update a DNA attribute,
  3437. * We set the value to "-2" or DNA_NEEDS_UPDATE, because we don't want
  3438. * to do the value allocation in the preop as the operation could fail -
  3439. * resulting in lost values from the range. So we need to to ensure
  3440. * that the value will not be lost by performing the value allocation
  3441. * in the backend txn preop.
  3442. *
  3443. * Although the modifications have already been applied in backend,
  3444. * we still need to add the modification of the real value to the
  3445. * existing Slapi_Mods, so that the value gets indexed correctly.
  3446. *
  3447. * Also, since the modifications have already been applied to the entry
  3448. * in the backend, we need to manually update the entry with the new value.
  3449. */
  3450. static int dna_be_txn_pre_op(Slapi_PBlock *pb, int modtype)
  3451. {
  3452. struct configEntry *config_entry = NULL;
  3453. struct slapi_entry *e = NULL;
  3454. Slapi_Mods *smods = NULL;
  3455. Slapi_Mod *smod = NULL;
  3456. Slapi_Mod *next_mod = NULL;
  3457. Slapi_Attr *attr = NULL;
  3458. LDAPMod **mods = NULL;
  3459. struct berval *bv = NULL;
  3460. PRCList *list = NULL;
  3461. char *value = NULL;
  3462. char **types_to_generate = NULL;
  3463. char **generated_types = NULL;
  3464. char *new_value = NULL;
  3465. char *errstr = NULL;
  3466. char *dn = NULL;
  3467. char *type = NULL;
  3468. int numvals, e_numvals = 0;
  3469. int i, len, ret = DNA_SUCCESS;
  3470. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3471. "--> dna_be_txn_pre_op\n");
  3472. if (!slapi_plugin_running(pb)) {
  3473. goto bail;
  3474. }
  3475. if (0 == (dn = dna_get_dn(pb))) {
  3476. goto bail;
  3477. }
  3478. if (dna_dn_is_config(dn)) {
  3479. goto bail;
  3480. }
  3481. if (dna_isrepl(pb)) {
  3482. /* if repl, the dna values should be already in the entry. */
  3483. goto bail;
  3484. }
  3485. if (LDAP_CHANGETYPE_ADD == modtype) {
  3486. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  3487. } else {
  3488. slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  3489. }
  3490. if (e == NULL) {
  3491. goto bail;
  3492. } else if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3493. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  3494. smods = slapi_mods_new();
  3495. slapi_mods_init_passin(smods, mods);
  3496. }
  3497. dna_read_lock();
  3498. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3499. list = PR_LIST_HEAD(dna_global_config);
  3500. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  3501. config_entry = (struct configEntry *) list;
  3502. /* Did we already service all of these configured types? */
  3503. if (dna_list_contains_types(generated_types, config_entry->types)) {
  3504. goto next;
  3505. }
  3506. /* is the entry in scope? */
  3507. if (config_entry->scope) {
  3508. if (!slapi_dn_issuffix(dn, config_entry->scope))
  3509. goto next;
  3510. }
  3511. /* is this entry in an excluded scope? */
  3512. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  3513. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  3514. goto next;
  3515. }
  3516. }
  3517. /* does the entry match the filter? */
  3518. if (config_entry->slapi_filter) {
  3519. if(LDAP_SUCCESS != slapi_vattr_filter_test(pb,e,config_entry->slapi_filter, 0))
  3520. goto next;
  3521. }
  3522. if (LDAP_CHANGETYPE_ADD == modtype) {
  3523. if (dna_is_multitype_range(config_entry)) {
  3524. /* For a multi-type range, we only generate a value
  3525. * for types where the magic value is set. We do not
  3526. * generate a value for missing types. */
  3527. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  3528. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  3529. if (value && !slapi_UTF8CASECMP(value, DNA_NEEDS_UPDATE)) {
  3530. slapi_ch_array_add(&types_to_generate,
  3531. slapi_ch_strdup(config_entry->types[i]));
  3532. /* Need to remove DNA_NEEDS_UPDATE */
  3533. slapi_entry_attr_delete(e, config_entry->types[i]);
  3534. }
  3535. slapi_ch_free_string(&value);
  3536. }
  3537. } else {
  3538. /* For a single type range, we generate the value if
  3539. * the magic value is set or if the type is missing. */
  3540. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  3541. if (0 == value || (value && !slapi_UTF8CASECMP(value, DNA_NEEDS_UPDATE)) ) {
  3542. slapi_ch_array_add(&types_to_generate,
  3543. slapi_ch_strdup(config_entry->types[0]));
  3544. /* Need to remove DNA_NEEDS_UPDATE */
  3545. slapi_entry_attr_delete(e, config_entry->types[0]);
  3546. }
  3547. slapi_ch_free_string(&value);
  3548. }
  3549. } else {
  3550. /* check mods for DNA_NEEDS_UPDATE*/
  3551. next_mod = slapi_mod_new();
  3552. smod = slapi_mods_get_first_smod(smods, next_mod);
  3553. while (smod) {
  3554. type = (char *)slapi_mod_get_type(smod);
  3555. /* See if the type matches any configured type. */
  3556. if (dna_list_contains_type(config_entry->types, type)) {
  3557. /* If all values are being deleted, we need to
  3558. * generate a new value. */
  3559. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  3560. !dna_is_multitype_range(config_entry)) {
  3561. numvals = slapi_mod_get_num_values(smod);
  3562. if (numvals == 0) {
  3563. slapi_ch_array_add(&types_to_generate,slapi_ch_strdup(type));
  3564. } else {
  3565. slapi_entry_attr_find(e, type, &attr);
  3566. if (attr) {
  3567. slapi_attr_get_numvalues(attr, &e_numvals);
  3568. if (numvals >= e_numvals) {
  3569. slapi_ch_array_add(&types_to_generate,
  3570. slapi_ch_strdup(type));
  3571. }
  3572. }
  3573. }
  3574. } else {
  3575. /* This is either adding or replacing a value */
  3576. bv = slapi_mod_get_first_value(smod);
  3577. if (dna_list_contains_type(types_to_generate, type)) {
  3578. dna_list_remove_type(types_to_generate, type);
  3579. }
  3580. /* If we have a value, see if it's the magic value. */
  3581. if (bv) {
  3582. if (!slapi_UTF8CASECMP(bv->bv_val,
  3583. DNA_NEEDS_UPDATE)) {
  3584. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3585. }
  3586. } else if (!dna_is_multitype_range(config_entry)) {
  3587. /* This is a replace with no new values, so we need
  3588. * to generate a new value if this is not a multi-type range. */
  3589. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3590. }
  3591. }
  3592. }
  3593. slapi_mod_done(next_mod);
  3594. smod = slapi_mods_get_next_smod(smods, next_mod);
  3595. }
  3596. slapi_mod_free(&next_mod);
  3597. }
  3598. /* We need to perform one last check for modify operations. If an
  3599. * entry within the scope has not triggered generation yet, we need
  3600. * to see if a value exists for the managed type in the resulting
  3601. * entry. This will catch a modify operation that brings an entry
  3602. * into scope for a managed range, but doesn't supply a value for
  3603. * the managed type. We don't do this for multi-type ranges. */
  3604. if ((LDAP_CHANGETYPE_MODIFY == modtype) && (!types_to_generate ||
  3605. (types_to_generate && !types_to_generate[0])) && !dna_is_multitype_range(config_entry)) {
  3606. if (slapi_entry_attr_find(e, config_entry->types[0], &attr) != 0) {
  3607. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[0]));
  3608. }
  3609. }
  3610. if (types_to_generate && types_to_generate[0]) {
  3611. /* create the value to add */
  3612. ret = dna_get_next_value(config_entry, &value);
  3613. if (DNA_SUCCESS != ret) {
  3614. errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3615. " %s failed! Unable to proceed.",
  3616. config_entry->dn);
  3617. slapi_ch_array_free(types_to_generate);
  3618. break;
  3619. }
  3620. len = strlen(value) + 1;
  3621. if (config_entry->prefix) {
  3622. len += strlen(config_entry->prefix);
  3623. }
  3624. new_value = slapi_ch_malloc(len);
  3625. if (config_entry->prefix) {
  3626. strcpy(new_value, config_entry->prefix);
  3627. strcat(new_value, value);
  3628. } else
  3629. strcpy(new_value, value);
  3630. /* do the mod */
  3631. if (LDAP_CHANGETYPE_ADD == modtype) {
  3632. /* add - add to entry */
  3633. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3634. slapi_entry_attr_set_charptr(e, types_to_generate[i], new_value);
  3635. }
  3636. } else {
  3637. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3638. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, types_to_generate[i], new_value);
  3639. /* we need to directly update the entry in be_txn_preop */
  3640. slapi_entry_attr_set_charptr(e, types_to_generate[i], new_value);
  3641. }
  3642. }
  3643. /*
  3644. * Make sure we don't generate for this
  3645. * type again by keeping a list of types
  3646. * we have generated for already.
  3647. */
  3648. if (LDAP_SUCCESS == ret) {
  3649. if (generated_types == NULL) {
  3650. /* If we don't have a list of generated types yet,
  3651. * we can just use the types_to_generate list so
  3652. * we don't have to allocate anything. */
  3653. generated_types = types_to_generate;
  3654. types_to_generate = NULL;
  3655. } else {
  3656. /* Just reuse the elements out of types_to_generate for the
  3657. * generated types list to avoid allocating them again. */
  3658. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  3659. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  3660. types_to_generate[i] = NULL;
  3661. }
  3662. }
  3663. }
  3664. /* free up */
  3665. slapi_ch_free_string(&value);
  3666. slapi_ch_free_string(&new_value);
  3667. slapi_ch_array_free(types_to_generate);
  3668. types_to_generate = NULL;
  3669. } else if (types_to_generate) {
  3670. slapi_ch_free((void **)&types_to_generate);
  3671. }
  3672. next:
  3673. list = PR_NEXT_LINK(list);
  3674. }
  3675. }
  3676. dna_unlock();
  3677. bail:
  3678. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3679. /* Put the updated mods back into place. */
  3680. if (smods) { /* smods == NULL if we bailed before initializing it */
  3681. mods = slapi_mods_get_ldapmods_passout(smods);
  3682. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  3683. slapi_mods_free(&smods);
  3684. }
  3685. }
  3686. slapi_ch_array_free(generated_types);
  3687. if (ret) {
  3688. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3689. "dna_be_txn_pre_op: operation failure [%d]\n", ret);
  3690. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  3691. slapi_ch_free((void **)&errstr);
  3692. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  3693. ret = DNA_FAILURE;
  3694. }
  3695. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3696. "<-- dna_be_txn_pre_op\n");
  3697. return ret;
  3698. }
  3699. static int dna_config_check_post_op(Slapi_PBlock * pb)
  3700. {
  3701. char *dn;
  3702. if(!slapi_plugin_running(pb)){
  3703. return DNA_SUCCESS;
  3704. }
  3705. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3706. "--> dna_config_check_post_op\n");
  3707. if (!slapi_op_internal(pb)) { /* If internal, no need to check. */
  3708. if ((dn = dna_get_dn(pb))) {
  3709. if (dna_dn_is_config(dn)) {
  3710. dna_load_plugin_config(pb, 0);
  3711. }
  3712. if(dna_dn_is_shared_config(pb, dn)){
  3713. dna_load_shared_servers();
  3714. }
  3715. }
  3716. }
  3717. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3718. "<-- dna_config_check_post_op\n");
  3719. return DNA_SUCCESS;
  3720. }
  3721. /****************************************************
  3722. * Range Extension Extended Operation
  3723. ***************************************************/
  3724. static int dna_extend_exop(Slapi_PBlock *pb)
  3725. {
  3726. int ret = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  3727. struct berval *reqdata = NULL;
  3728. BerElement *tmp_bere = NULL;
  3729. char *shared_dn = NULL;
  3730. char *bind_dn = NULL;
  3731. char *oid = NULL;
  3732. PRUint64 lower = 0;
  3733. PRUint64 upper = 0;
  3734. if(!slapi_plugin_running(pb)){
  3735. return ret;
  3736. }
  3737. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3738. "--> dna_extend_exop\n");
  3739. /* Fetch the request OID */
  3740. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &oid);
  3741. if (!oid) {
  3742. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3743. "dna_extend_exop: Unable to retrieve request OID.\n");
  3744. goto free_and_return;
  3745. }
  3746. /* Make sure the request OID is correct. */
  3747. if (strcmp(oid, DNA_EXTEND_EXOP_REQUEST_OID) != 0) {
  3748. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3749. "dna_extend_exop: Received incorrect request OID.\n");
  3750. goto free_and_return;
  3751. }
  3752. /* Fetch the request data */
  3753. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &reqdata);
  3754. if (!BV_HAS_DATA(reqdata)) {
  3755. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3756. "dna_extend_exop: No request data received.\n");
  3757. goto free_and_return;
  3758. }
  3759. /* decode the exop */
  3760. tmp_bere = ber_init(reqdata);
  3761. if (tmp_bere == NULL) {
  3762. goto free_and_return;
  3763. }
  3764. if (ber_scanf(tmp_bere, "{a}", &shared_dn) == LBER_ERROR) {
  3765. ret = LDAP_PROTOCOL_ERROR;
  3766. goto free_and_return;
  3767. }
  3768. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3769. "dna_extend_exop: received range extension "
  3770. "request for range [%s]\n", shared_dn);
  3771. /* Only allow range requests from the replication bind DN user */
  3772. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
  3773. if (!dna_is_replica_bind_dn(shared_dn, bind_dn)) {
  3774. ret = LDAP_INSUFFICIENT_ACCESS;
  3775. goto free_and_return;
  3776. }
  3777. /* See if we have the req. range configured.
  3778. * If so, we need to see if we have range to provide. */
  3779. ret = dna_release_range(shared_dn, &lower, &upper);
  3780. if (ret == LDAP_SUCCESS) {
  3781. /* We have range to give away, so construct
  3782. * and send the response. */
  3783. BerElement *respber = NULL;
  3784. struct berval *respdata = NULL;
  3785. struct berval range_low = {0, NULL};
  3786. struct berval range_high = {0, NULL};
  3787. char lowstr[16];
  3788. char highstr[16];
  3789. /* Create the exop response */
  3790. PR_snprintf(lowstr, sizeof(lowstr), "%" NSPRIu64, lower);
  3791. PR_snprintf(highstr, sizeof(highstr), "%" NSPRIu64, upper);
  3792. range_low.bv_val = lowstr;
  3793. range_low.bv_len = strlen(range_low.bv_val);
  3794. range_high.bv_val = highstr;
  3795. range_high.bv_len = strlen(range_high.bv_val);
  3796. if ((respber = ber_alloc()) == NULL) {
  3797. ret = LDAP_NO_MEMORY;
  3798. goto free_and_return;
  3799. }
  3800. if (LBER_ERROR == (ber_printf(respber, "{oo}",
  3801. range_low.bv_val, range_low.bv_len,
  3802. range_high.bv_val, range_high.bv_len))) {
  3803. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3804. "dna_extend_exop: Unable to encode exop response.\n");
  3805. ber_free(respber, 1);
  3806. ret = LDAP_ENCODING_ERROR;
  3807. goto free_and_return;
  3808. }
  3809. ber_flatten(respber, &respdata);
  3810. ber_free(respber, 1);
  3811. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, DNA_EXTEND_EXOP_RESPONSE_OID);
  3812. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, respdata);
  3813. /* send the response ourselves */
  3814. slapi_send_ldap_result( pb, ret, NULL, NULL, 0, NULL );
  3815. ret = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  3816. ber_bvfree(respdata);
  3817. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3818. "dna_extend_exop: Released range %" NSPRIu64 "-%" NSPRIu64 ".\n",
  3819. lower, upper);
  3820. }
  3821. free_and_return:
  3822. slapi_ch_free_string(&shared_dn);
  3823. slapi_ch_free_string(&bind_dn);
  3824. if (NULL != tmp_bere) {
  3825. ber_free(tmp_bere, 1);
  3826. tmp_bere = NULL;
  3827. }
  3828. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3829. "<-- dna_extend_exop\n");
  3830. return ret;
  3831. }
  3832. /*
  3833. * dna_release_range()
  3834. *
  3835. * Checks if we have any values that we can release
  3836. * for the range specified by range_dn.
  3837. */
  3838. static int
  3839. dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper)
  3840. {
  3841. int ret = 0;
  3842. int match = 0;
  3843. PRCList *list = NULL;
  3844. Slapi_DN *cfg_base_sdn = NULL;
  3845. Slapi_DN *range_sdn = NULL;
  3846. struct configEntry *config_entry = NULL;
  3847. int set_extend_flag = 0;
  3848. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3849. "--> dna_release_range\n");
  3850. if (range_dn) {
  3851. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  3852. dna_read_lock();
  3853. /* Go through the config entries to see if we
  3854. * have a shared range configured that matches
  3855. * the range from the exop request. */
  3856. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3857. list = PR_LIST_HEAD(dna_global_config);
  3858. while ((list != dna_global_config) && match != 1) {
  3859. config_entry = (struct configEntry *)list;
  3860. cfg_base_sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_base);
  3861. if (slapi_sdn_compare(cfg_base_sdn, range_sdn) == 0) {
  3862. /* We found a match. Set match flag to
  3863. * break out of the loop. */
  3864. match = 1;
  3865. } else {
  3866. config_entry = NULL;
  3867. list = PR_NEXT_LINK(list);
  3868. }
  3869. slapi_sdn_free(&cfg_base_sdn);
  3870. }
  3871. }
  3872. /* config_entry will point to our match if we found one */
  3873. if (config_entry) {
  3874. int release = 0;
  3875. Slapi_PBlock *pb = NULL;
  3876. LDAPMod mod_replace;
  3877. LDAPMod *mods[2];
  3878. char *replace_val[2];
  3879. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  3880. char max_value[17];
  3881. /* Need to bail if we're performing a range request
  3882. * for this range. This is to prevent the case where two
  3883. * servers are asking each other for more range for the
  3884. * same managed range. This would result in a network
  3885. * deadlock until the idletimeout kills one of the
  3886. * connections. */
  3887. slapi_lock_mutex(config_entry->extend_lock);
  3888. if (config_entry->extend_in_progress) {
  3889. /* We're already processing a range extention, so bail. */
  3890. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3891. "dna_release_range: Already processing a "
  3892. "range extension request. Skipping request.\n");
  3893. slapi_unlock_mutex(config_entry->extend_lock);
  3894. ret = LDAP_UNWILLING_TO_PERFORM;
  3895. goto bail;
  3896. } else {
  3897. /* Set a flag indicating that we're attempting to extend this range */
  3898. config_entry->extend_in_progress = 1;
  3899. set_extend_flag = 1;
  3900. slapi_unlock_mutex(config_entry->extend_lock);
  3901. }
  3902. /* Obtain the lock for this range */
  3903. slapi_lock_mutex(config_entry->lock);
  3904. /* Refuse if we're at or below our threshold */
  3905. if (config_entry->remaining <= config_entry->threshold) {
  3906. ret = LDAP_UNWILLING_TO_PERFORM;
  3907. goto bail;
  3908. }
  3909. /* If we have a next range, we need to give up values from
  3910. * it instead of from the active range */
  3911. if (config_entry->next_range_lower != 0) {
  3912. /* Release up to half of our values from the next range. */
  3913. release = (((config_entry->next_range_upper - config_entry->next_range_lower + 1) /
  3914. 2) / config_entry->threshold) * config_entry->threshold;
  3915. if (release == 0) {
  3916. ret = LDAP_UNWILLING_TO_PERFORM;
  3917. goto bail;
  3918. }
  3919. *upper = config_entry->next_range_upper;
  3920. *lower = *upper - release + 1;
  3921. /* Try to set the new next range in the config */
  3922. ret = dna_update_next_range(config_entry, config_entry->next_range_lower,
  3923. *lower - 1);
  3924. } else {
  3925. /* We release up to half of our remaining values,
  3926. * but we'll only release a range that is a multiple
  3927. * of our threshold. */
  3928. release = ((config_entry->remaining / 2) /
  3929. config_entry->threshold) * config_entry->threshold;
  3930. if (release == 0) {
  3931. ret = LDAP_UNWILLING_TO_PERFORM;
  3932. goto bail;
  3933. }
  3934. /* We give away values from the upper end of our range. */
  3935. *upper = config_entry->maxval;
  3936. *lower = *upper - release + 1;
  3937. /* try to set the new maxval in the config entry */
  3938. PR_snprintf(max_value, sizeof(max_value),"%" NSPRIu64, (*lower - 1));
  3939. /* set up our replace modify operation */
  3940. replace_val[0] = max_value;
  3941. replace_val[1] = 0;
  3942. mod_replace.mod_op = LDAP_MOD_REPLACE;
  3943. mod_replace.mod_type = DNA_MAXVAL;
  3944. mod_replace.mod_values = replace_val;
  3945. mods[0] = &mod_replace;
  3946. mods[1] = 0;
  3947. pb = slapi_pblock_new();
  3948. if (NULL == pb) {
  3949. ret = LDAP_OPERATIONS_ERROR;
  3950. goto bail;
  3951. }
  3952. slapi_modify_internal_set_pb(pb, config_entry->dn,
  3953. mods, 0, 0, getPluginID(), 0);
  3954. slapi_modify_internal_pb(pb);
  3955. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  3956. slapi_pblock_destroy(pb);
  3957. pb = NULL;
  3958. if (ret == LDAP_SUCCESS) {
  3959. /* Adjust maxval in our cached config and shared config */
  3960. config_entry->maxval = *lower - 1;
  3961. dna_notice_allocation(config_entry, config_entry->nextval, 0);
  3962. }
  3963. }
  3964. if (ret != LDAP_SUCCESS) {
  3965. /* Updating the config failed, so reset. We don't
  3966. * want to give the caller any range */
  3967. *lower = 0;
  3968. *upper = 0;
  3969. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3970. "dna_release_range: Error updating "
  3971. "configuration entry [err=%d]\n", ret);
  3972. }
  3973. }
  3974. bail:
  3975. if (set_extend_flag) {
  3976. slapi_lock_mutex(config_entry->extend_lock);
  3977. config_entry->extend_in_progress = 0;
  3978. slapi_unlock_mutex(config_entry->extend_lock);
  3979. }
  3980. if (config_entry) {
  3981. slapi_unlock_mutex(config_entry->lock);
  3982. }
  3983. slapi_sdn_free(&range_sdn);
  3984. dna_unlock();
  3985. }
  3986. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3987. "<-- dna_release_range\n");
  3988. return ret;
  3989. }
  3990. /****************************************************
  3991. End of
  3992. Functions that actually do things other
  3993. than config and startup
  3994. ****************************************************/
  3995. /**
  3996. * debug functions to print config
  3997. */
  3998. void dna_dump_config()
  3999. {
  4000. PRCList *list;
  4001. dna_read_lock();
  4002. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  4003. list = PR_LIST_HEAD(dna_global_config);
  4004. while (list != dna_global_config) {
  4005. dna_dump_config_entry((struct configEntry *) list);
  4006. list = PR_NEXT_LINK(list);
  4007. }
  4008. }
  4009. dna_unlock();
  4010. }
  4011. /*
  4012. * dna_isrepl()
  4013. *
  4014. * Returns 1 if the operation associated with pb
  4015. * is a replicated op. Returns 0 otherwise.
  4016. */
  4017. static int
  4018. dna_isrepl(Slapi_PBlock *pb)
  4019. {
  4020. int is_repl = 0;
  4021. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  4022. return is_repl;
  4023. }
  4024. void dna_dump_config_entry(struct configEntry * entry)
  4025. {
  4026. int i = 0;
  4027. for (i = 0; entry->types && entry->types[i]; i++) {
  4028. printf("<---- type -----------> %s\n", entry->types[i]);
  4029. }
  4030. printf("<---- filter ---------> %s\n", entry->filter);
  4031. printf("<---- prefix ---------> %s\n", entry->prefix);
  4032. printf("<---- scope ----------> %s\n", entry->scope);
  4033. for (i = 0; entry->excludescope && entry->excludescope[i]; i++) {
  4034. printf("<---- excluded scope -> %s\n", slapi_sdn_get_dn(entry->excludescope[i]));
  4035. }
  4036. printf("<---- next value -----> %" PRIu64 "\n", entry->nextval);
  4037. printf("<---- max value ------> %" PRIu64 "\n", entry->maxval);
  4038. printf("<---- interval -------> %" PRIu64 "\n", entry->interval);
  4039. printf("<---- generate flag --> %s\n", entry->generate);
  4040. printf("<---- shared cfg base > %s\n", entry->shared_cfg_base);
  4041. printf("<---- shared cfg DN --> %s\n", entry->shared_cfg_dn);
  4042. printf("<---- threshold ------> %" PRIu64 "", entry->threshold);
  4043. }