dna.c 166 KB

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