dna.c 168 KB

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