dna.c 163 KB

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