dna.c 163 KB

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