dna.c 160 KB

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